blob: e2c6b450ac44ce09b06b1bae69e9a7a97d1c6ad3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * NSA Security-Enhanced Linux (SELinux) security module
3 *
4 * This file contains the SELinux hook function implementations.
5 *
6 * Authors: Stephen Smalley, <sds@epoch.ncsc.mil>
Eric Paris828dfe12008-04-17 13:17:49 -04007 * Chris Vance, <cvance@nai.com>
8 * Wayne Salamon, <wsalamon@nai.com>
9 * James Morris <jmorris@redhat.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * Copyright (C) 2001,2002 Networks Associates Technology, Inc.
Eric Paris2069f452008-07-04 09:47:13 +100012 * Copyright (C) 2003-2008 Red Hat, Inc., James Morris <jmorris@redhat.com>
13 * Eric Paris <eparis@redhat.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
Eric Paris828dfe12008-04-17 13:17:49 -040015 * <dgoeddel@trustedcs.com>
Paul Mooreed6d76e2009-08-28 18:12:49 -040016 * Copyright (C) 2006, 2007, 2009 Hewlett-Packard Development Company, L.P.
Paul Moore82c21bf2011-08-01 11:10:33 +000017 * Paul Moore <paul@paul-moore.com>
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +090018 * Copyright (C) 2007 Hitachi Software Engineering Co., Ltd.
Eric Paris828dfe12008-04-17 13:17:49 -040019 * Yuichi Nakamura <ynakam@hitachisoft.jp>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 *
21 * This program is free software; you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License version 2,
Eric Paris828dfe12008-04-17 13:17:49 -040023 * as published by the Free Software Foundation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 */
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/init.h>
Eric Paris0b24dcb2011-02-25 15:39:20 -050027#include <linux/kd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/kernel.h>
Roland McGrath0d094ef2008-07-25 19:45:49 -070029#include <linux/tracehook.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/errno.h>
31#include <linux/sched.h>
32#include <linux/security.h>
33#include <linux/xattr.h>
34#include <linux/capability.h>
35#include <linux/unistd.h>
36#include <linux/mm.h>
37#include <linux/mman.h>
38#include <linux/slab.h>
39#include <linux/pagemap.h>
Eric Paris0b24dcb2011-02-25 15:39:20 -050040#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/swap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/spinlock.h>
43#include <linux/syscalls.h>
Eric Paris2a7dba32011-02-01 11:05:39 -050044#include <linux/dcache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/file.h>
Al Viro9f3acc32008-04-24 07:44:08 -040046#include <linux/fdtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/namei.h>
48#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/netfilter_ipv4.h>
50#include <linux/netfilter_ipv6.h>
51#include <linux/tty.h>
52#include <net/icmp.h>
Stephen Hemminger227b60f2007-10-10 17:30:46 -070053#include <net/ip.h> /* for local_port_range[] */
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <net/tcp.h> /* struct or_callable used in sock_rcv_skb */
Paul Moore1c5d9d12013-12-04 16:10:45 -050055#include <net/inet_connection_sock.h>
Paul Moore220deb92008-01-29 08:38:23 -050056#include <net/net_namespace.h>
Paul Moored621d352008-01-29 08:43:36 -050057#include <net/netlabel.h>
Eric Parisf5269712008-05-14 11:27:45 -040058#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <asm/ioctls.h>
Arun Sharma60063492011-07-26 16:09:06 -070060#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/bitops.h>
62#include <linux/interrupt.h>
63#include <linux/netdevice.h> /* for network interface checks */
64#include <linux/netlink.h>
65#include <linux/tcp.h>
66#include <linux/udp.h>
James Morris2ee92d42006-11-13 16:09:01 -080067#include <linux/dccp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/quota.h>
69#include <linux/un.h> /* for Unix socket types */
70#include <net/af_unix.h> /* for Unix socket types */
71#include <linux/parser.h>
72#include <linux/nfs_mount.h>
73#include <net/ipv6.h>
74#include <linux/hugetlb.h>
75#include <linux/personality.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include <linux/audit.h>
Eric Paris6931dfc2005-06-30 02:58:51 -070077#include <linux/string.h>
Catherine Zhang877ce7c2006-06-29 12:27:47 -070078#include <linux/selinux.h>
Eric Paris23970742006-09-25 23:32:01 -070079#include <linux/mutex.h>
Frank Mayharf06febc2008-09-12 09:54:39 -070080#include <linux/posix-timers.h>
Kees Cook00234592010-02-03 15:36:43 -080081#include <linux/syslog.h>
Serge E. Hallyn34867402011-03-23 16:43:17 -070082#include <linux/user_namespace.h>
Paul Gortmaker44fc7ea2011-05-26 20:52:10 -040083#include <linux/export.h>
Al Viro40401532012-02-13 03:58:52 +000084#include <linux/msg.h>
85#include <linux/shm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87#include "avc.h"
88#include "objsec.h"
89#include "netif.h"
Paul Moore224dfbd2008-01-29 08:38:13 -050090#include "netnode.h"
Paul Moore3e112172008-04-10 10:48:14 -040091#include "netport.h"
Trent Jaegerd28d1e02005-12-13 23:12:40 -080092#include "xfrm.h"
Paul Moorec60475b2007-02-28 15:14:23 -050093#include "netlabel.h"
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +020094#include "audit.h"
James Morris7b98a582011-08-30 12:52:32 +100095#include "avc_ss.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
David P. Quigley11689d42009-01-16 09:22:03 -050097#define NUM_SEL_MNT_OPTS 5
Eric Parisc9180a52007-11-30 13:00:35 -050098
James Morris20510f22007-10-16 23:31:32 -070099extern struct security_operations *security_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Paul Moored621d352008-01-29 08:43:36 -0500101/* SECMARK reference count */
James Morris56a4ca92011-08-17 11:08:43 +1000102static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0);
Paul Moored621d352008-01-29 08:43:36 -0500103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104#ifdef CONFIG_SECURITY_SELINUX_DEVELOP
Eric Paris828dfe12008-04-17 13:17:49 -0400105int selinux_enforcing;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107static int __init enforcing_setup(char *str)
108{
Eric Parisf5269712008-05-14 11:27:45 -0400109 unsigned long enforcing;
110 if (!strict_strtoul(str, 0, &enforcing))
111 selinux_enforcing = enforcing ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return 1;
113}
114__setup("enforcing=", enforcing_setup);
115#endif
116
117#ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM
118int selinux_enabled = CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE;
119
120static int __init selinux_enabled_setup(char *str)
121{
Eric Parisf5269712008-05-14 11:27:45 -0400122 unsigned long enabled;
123 if (!strict_strtoul(str, 0, &enabled))
124 selinux_enabled = enabled ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 return 1;
126}
127__setup("selinux=", selinux_enabled_setup);
Stephen Smalley30d55282006-05-03 10:52:36 -0400128#else
129int selinux_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130#endif
131
Christoph Lametere18b8902006-12-06 20:33:20 -0800132static struct kmem_cache *sel_inode_cache;
James Morris7cae7e22006-03-22 00:09:22 -0800133
Paul Moored621d352008-01-29 08:43:36 -0500134/**
135 * selinux_secmark_enabled - Check to see if SECMARK is currently enabled
136 *
137 * Description:
138 * This function checks the SECMARK reference counter to see if any SECMARK
139 * targets are currently configured, if the reference counter is greater than
140 * zero SECMARK is considered to be enabled. Returns true (1) if SECMARK is
141 * enabled, false (0) if SECMARK is disabled.
142 *
143 */
144static int selinux_secmark_enabled(void)
145{
146 return (atomic_read(&selinux_secmark_refcount) > 0);
147}
148
David Howellsd84f4f92008-11-14 10:39:23 +1100149/*
150 * initialise the security for the init task
151 */
152static void cred_init_security(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
David Howells3b11a1d2008-11-14 10:39:26 +1100154 struct cred *cred = (struct cred *) current->real_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 struct task_security_struct *tsec;
156
James Morris89d155e2005-10-30 14:59:21 -0800157 tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 if (!tsec)
David Howellsd84f4f92008-11-14 10:39:23 +1100159 panic("SELinux: Failed to initialize initial task.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
David Howellsd84f4f92008-11-14 10:39:23 +1100161 tsec->osid = tsec->sid = SECINITSID_KERNEL;
David Howellsf1752ee2008-11-14 10:39:17 +1100162 cred->security = tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
David Howells275bb412008-11-14 10:39:19 +1100165/*
David Howells88e67f32008-11-14 10:39:21 +1100166 * get the security ID of a set of credentials
167 */
168static inline u32 cred_sid(const struct cred *cred)
169{
170 const struct task_security_struct *tsec;
171
172 tsec = cred->security;
173 return tsec->sid;
174}
175
176/*
David Howells3b11a1d2008-11-14 10:39:26 +1100177 * get the objective security ID of a task
David Howells275bb412008-11-14 10:39:19 +1100178 */
179static inline u32 task_sid(const struct task_struct *task)
180{
David Howells275bb412008-11-14 10:39:19 +1100181 u32 sid;
182
183 rcu_read_lock();
David Howells88e67f32008-11-14 10:39:21 +1100184 sid = cred_sid(__task_cred(task));
David Howells275bb412008-11-14 10:39:19 +1100185 rcu_read_unlock();
186 return sid;
187}
188
189/*
David Howells3b11a1d2008-11-14 10:39:26 +1100190 * get the subjective security ID of the current task
David Howells275bb412008-11-14 10:39:19 +1100191 */
192static inline u32 current_sid(void)
193{
Paul Moore5fb49872010-04-22 14:46:19 -0400194 const struct task_security_struct *tsec = current_security();
David Howells275bb412008-11-14 10:39:19 +1100195
196 return tsec->sid;
197}
198
David Howells88e67f32008-11-14 10:39:21 +1100199/* Allocate and free functions for each kind of security blob. */
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201static int inode_alloc_security(struct inode *inode)
202{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 struct inode_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +1100204 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Josef Bacika02fe132008-04-04 09:35:05 +1100206 isec = kmem_cache_zalloc(sel_inode_cache, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 if (!isec)
208 return -ENOMEM;
209
Eric Paris23970742006-09-25 23:32:01 -0700210 mutex_init(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 INIT_LIST_HEAD(&isec->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 isec->inode = inode;
213 isec->sid = SECINITSID_UNLABELED;
214 isec->sclass = SECCLASS_FILE;
David Howells275bb412008-11-14 10:39:19 +1100215 isec->task_sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 inode->i_security = isec;
217
218 return 0;
219}
220
Steven Rostedt9e74d932014-01-09 21:46:34 -0500221static void inode_free_rcu(struct rcu_head *head)
222{
223 struct inode_security_struct *isec;
224
225 isec = container_of(head, struct inode_security_struct, rcu);
226 kmem_cache_free(sel_inode_cache, isec);
227}
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229static void inode_free_security(struct inode *inode)
230{
231 struct inode_security_struct *isec = inode->i_security;
232 struct superblock_security_struct *sbsec = inode->i_sb->s_security;
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 spin_lock(&sbsec->isec_lock);
235 if (!list_empty(&isec->list))
236 list_del_init(&isec->list);
237 spin_unlock(&sbsec->isec_lock);
238
Steven Rostedt9e74d932014-01-09 21:46:34 -0500239 /*
240 * The inode may still be referenced in a path walk and
241 * a call to selinux_inode_permission() can be made
242 * after inode_free_security() is called. Ideally, the VFS
243 * wouldn't do this, but fixing that is a much harder
244 * job. For now, simply free the i_security via RCU, and
245 * leave the current inode->i_security pointer intact.
246 * The inode will be freed after the RCU grace period too.
247 */
248 call_rcu(&isec->rcu, inode_free_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
251static int file_alloc_security(struct file *file)
252{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 struct file_security_struct *fsec;
David Howells275bb412008-11-14 10:39:19 +1100254 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Stephen Smalley26d2a4b2006-02-01 03:05:55 -0800256 fsec = kzalloc(sizeof(struct file_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 if (!fsec)
258 return -ENOMEM;
259
David Howells275bb412008-11-14 10:39:19 +1100260 fsec->sid = sid;
261 fsec->fown_sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 file->f_security = fsec;
263
264 return 0;
265}
266
267static void file_free_security(struct file *file)
268{
269 struct file_security_struct *fsec = file->f_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 file->f_security = NULL;
271 kfree(fsec);
272}
273
274static int superblock_alloc_security(struct super_block *sb)
275{
276 struct superblock_security_struct *sbsec;
277
James Morris89d155e2005-10-30 14:59:21 -0800278 sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 if (!sbsec)
280 return -ENOMEM;
281
Eric Parisbc7e9822006-09-25 23:32:02 -0700282 mutex_init(&sbsec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 INIT_LIST_HEAD(&sbsec->isec_head);
284 spin_lock_init(&sbsec->isec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 sbsec->sb = sb;
286 sbsec->sid = SECINITSID_UNLABELED;
287 sbsec->def_sid = SECINITSID_FILE;
Eric Parisc312feb2006-07-10 04:43:53 -0700288 sbsec->mntpoint_sid = SECINITSID_UNLABELED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 sb->s_security = sbsec;
290
291 return 0;
292}
293
294static void superblock_free_security(struct super_block *sb)
295{
296 struct superblock_security_struct *sbsec = sb->s_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 sb->s_security = NULL;
298 kfree(sbsec);
299}
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301/* The file system's label must be initialized prior to use. */
302
Stephen Hemminger634a5392010-03-04 21:59:03 -0800303static const char *labeling_behaviors[6] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 "uses xattr",
305 "uses transition SIDs",
306 "uses task SIDs",
307 "uses genfs_contexts",
308 "not configured for labeling",
309 "uses mountpoint labeling",
310};
311
312static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry);
313
314static inline int inode_doinit(struct inode *inode)
315{
316 return inode_doinit_with_dentry(inode, NULL);
317}
318
319enum {
Eric Paris31e87932007-09-19 17:19:12 -0400320 Opt_error = -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 Opt_context = 1,
322 Opt_fscontext = 2,
Eric Parisc9180a52007-11-30 13:00:35 -0500323 Opt_defcontext = 3,
324 Opt_rootcontext = 4,
David P. Quigley11689d42009-01-16 09:22:03 -0500325 Opt_labelsupport = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326};
327
Steven Whitehousea447c092008-10-13 10:46:57 +0100328static const match_table_t tokens = {
Eric Paris832cbd92008-04-01 13:24:09 -0400329 {Opt_context, CONTEXT_STR "%s"},
330 {Opt_fscontext, FSCONTEXT_STR "%s"},
331 {Opt_defcontext, DEFCONTEXT_STR "%s"},
332 {Opt_rootcontext, ROOTCONTEXT_STR "%s"},
David P. Quigley11689d42009-01-16 09:22:03 -0500333 {Opt_labelsupport, LABELSUPP_STR},
Eric Paris31e87932007-09-19 17:19:12 -0400334 {Opt_error, NULL},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335};
336
337#define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n"
338
Eric Parisc312feb2006-07-10 04:43:53 -0700339static int may_context_mount_sb_relabel(u32 sid,
340 struct superblock_security_struct *sbsec,
David Howells275bb412008-11-14 10:39:19 +1100341 const struct cred *cred)
Eric Parisc312feb2006-07-10 04:43:53 -0700342{
David Howells275bb412008-11-14 10:39:19 +1100343 const struct task_security_struct *tsec = cred->security;
Eric Parisc312feb2006-07-10 04:43:53 -0700344 int rc;
345
346 rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
347 FILESYSTEM__RELABELFROM, NULL);
348 if (rc)
349 return rc;
350
351 rc = avc_has_perm(tsec->sid, sid, SECCLASS_FILESYSTEM,
352 FILESYSTEM__RELABELTO, NULL);
353 return rc;
354}
355
Eric Paris08089252006-07-10 04:43:55 -0700356static int may_context_mount_inode_relabel(u32 sid,
357 struct superblock_security_struct *sbsec,
David Howells275bb412008-11-14 10:39:19 +1100358 const struct cred *cred)
Eric Paris08089252006-07-10 04:43:55 -0700359{
David Howells275bb412008-11-14 10:39:19 +1100360 const struct task_security_struct *tsec = cred->security;
Eric Paris08089252006-07-10 04:43:55 -0700361 int rc;
362 rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
363 FILESYSTEM__RELABELFROM, NULL);
364 if (rc)
365 return rc;
366
367 rc = avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM,
368 FILESYSTEM__ASSOCIATE, NULL);
369 return rc;
370}
371
Eric Parisc9180a52007-11-30 13:00:35 -0500372static int sb_finish_set_opts(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
374 struct superblock_security_struct *sbsec = sb->s_security;
375 struct dentry *root = sb->s_root;
Eric Parisc9180a52007-11-30 13:00:35 -0500376 struct inode *root_inode = root->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 int rc = 0;
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
380 /* Make sure that the xattr handler exists and that no
381 error other than -ENODATA is returned by getxattr on
382 the root directory. -ENODATA is ok, as this may be
383 the first boot of the SELinux kernel before we have
384 assigned xattr values to the filesystem. */
Eric Parisc9180a52007-11-30 13:00:35 -0500385 if (!root_inode->i_op->getxattr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 printk(KERN_WARNING "SELinux: (dev %s, type %s) has no "
387 "xattr support\n", sb->s_id, sb->s_type->name);
388 rc = -EOPNOTSUPP;
389 goto out;
390 }
Eric Parisc9180a52007-11-30 13:00:35 -0500391 rc = root_inode->i_op->getxattr(root, XATTR_NAME_SELINUX, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 if (rc < 0 && rc != -ENODATA) {
393 if (rc == -EOPNOTSUPP)
394 printk(KERN_WARNING "SELinux: (dev %s, type "
395 "%s) has no security xattr handler\n",
396 sb->s_id, sb->s_type->name);
397 else
398 printk(KERN_WARNING "SELinux: (dev %s, type "
399 "%s) getxattr errno %d\n", sb->s_id,
400 sb->s_type->name, -rc);
401 goto out;
402 }
403 }
404
David P. Quigley11689d42009-01-16 09:22:03 -0500405 sbsec->flags |= (SE_SBINITIALIZED | SE_SBLABELSUPP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Eric Parisc9180a52007-11-30 13:00:35 -0500407 if (sbsec->behavior > ARRAY_SIZE(labeling_behaviors))
Eric Parisfadcdb42007-02-22 18:11:31 -0500408 printk(KERN_ERR "SELinux: initialized (dev %s, type %s), unknown behavior\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 sb->s_id, sb->s_type->name);
Eric Parisc9180a52007-11-30 13:00:35 -0500410 else
Eric Parisfadcdb42007-02-22 18:11:31 -0500411 printk(KERN_DEBUG "SELinux: initialized (dev %s, type %s), %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 sb->s_id, sb->s_type->name,
413 labeling_behaviors[sbsec->behavior-1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
David P. Quigley11689d42009-01-16 09:22:03 -0500415 if (sbsec->behavior == SECURITY_FS_USE_GENFS ||
416 sbsec->behavior == SECURITY_FS_USE_MNTPOINT ||
417 sbsec->behavior == SECURITY_FS_USE_NONE ||
418 sbsec->behavior > ARRAY_SIZE(labeling_behaviors))
419 sbsec->flags &= ~SE_SBLABELSUPP;
420
David P. Quigleyddd29ec2009-09-09 14:25:37 -0400421 /* Special handling for sysfs. Is genfs but also has setxattr handler*/
422 if (strncmp(sb->s_type->name, "sysfs", sizeof("sysfs")) == 0)
423 sbsec->flags |= SE_SBLABELSUPP;
424
Stephen Smalley90078112013-05-10 10:16:19 -0400425 /*
426 * Special handling for rootfs. Is genfs but supports
427 * setting SELinux context on in-core inodes.
428 */
429 if (strncmp(sb->s_type->name, "rootfs", sizeof("rootfs")) == 0)
430 sbsec->flags |= SE_SBLABELSUPP;
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 /* Initialize the root inode. */
Eric Parisc9180a52007-11-30 13:00:35 -0500433 rc = inode_doinit_with_dentry(root_inode, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 /* Initialize any other inodes associated with the superblock, e.g.
436 inodes created prior to initial policy load or inodes created
437 during get_sb by a pseudo filesystem that directly
438 populates itself. */
439 spin_lock(&sbsec->isec_lock);
440next_inode:
441 if (!list_empty(&sbsec->isec_head)) {
442 struct inode_security_struct *isec =
443 list_entry(sbsec->isec_head.next,
Eric Parisc9180a52007-11-30 13:00:35 -0500444 struct inode_security_struct, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 struct inode *inode = isec->inode;
446 spin_unlock(&sbsec->isec_lock);
447 inode = igrab(inode);
448 if (inode) {
Eric Parisc9180a52007-11-30 13:00:35 -0500449 if (!IS_PRIVATE(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 inode_doinit(inode);
451 iput(inode);
452 }
453 spin_lock(&sbsec->isec_lock);
454 list_del_init(&isec->list);
455 goto next_inode;
456 }
457 spin_unlock(&sbsec->isec_lock);
458out:
Eric Parisc9180a52007-11-30 13:00:35 -0500459 return rc;
460}
461
462/*
463 * This function should allow an FS to ask what it's mount security
464 * options were so it can use those later for submounts, displaying
465 * mount options, or whatever.
466 */
467static int selinux_get_mnt_opts(const struct super_block *sb,
Eric Parise0007522008-03-05 10:31:54 -0500468 struct security_mnt_opts *opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500469{
470 int rc = 0, i;
471 struct superblock_security_struct *sbsec = sb->s_security;
472 char *context = NULL;
473 u32 len;
474 char tmp;
475
Eric Parise0007522008-03-05 10:31:54 -0500476 security_init_mnt_opts(opts);
Eric Parisc9180a52007-11-30 13:00:35 -0500477
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500478 if (!(sbsec->flags & SE_SBINITIALIZED))
Eric Parisc9180a52007-11-30 13:00:35 -0500479 return -EINVAL;
480
481 if (!ss_initialized)
482 return -EINVAL;
483
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500484 tmp = sbsec->flags & SE_MNTMASK;
Eric Parisc9180a52007-11-30 13:00:35 -0500485 /* count the number of mount options for this sb */
486 for (i = 0; i < 8; i++) {
487 if (tmp & 0x01)
Eric Parise0007522008-03-05 10:31:54 -0500488 opts->num_mnt_opts++;
Eric Parisc9180a52007-11-30 13:00:35 -0500489 tmp >>= 1;
490 }
David P. Quigley11689d42009-01-16 09:22:03 -0500491 /* Check if the Label support flag is set */
492 if (sbsec->flags & SE_SBLABELSUPP)
493 opts->num_mnt_opts++;
Eric Parisc9180a52007-11-30 13:00:35 -0500494
Eric Parise0007522008-03-05 10:31:54 -0500495 opts->mnt_opts = kcalloc(opts->num_mnt_opts, sizeof(char *), GFP_ATOMIC);
496 if (!opts->mnt_opts) {
Eric Parisc9180a52007-11-30 13:00:35 -0500497 rc = -ENOMEM;
498 goto out_free;
499 }
500
Eric Parise0007522008-03-05 10:31:54 -0500501 opts->mnt_opts_flags = kcalloc(opts->num_mnt_opts, sizeof(int), GFP_ATOMIC);
502 if (!opts->mnt_opts_flags) {
Eric Parisc9180a52007-11-30 13:00:35 -0500503 rc = -ENOMEM;
504 goto out_free;
505 }
506
507 i = 0;
508 if (sbsec->flags & FSCONTEXT_MNT) {
509 rc = security_sid_to_context(sbsec->sid, &context, &len);
510 if (rc)
511 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500512 opts->mnt_opts[i] = context;
513 opts->mnt_opts_flags[i++] = FSCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500514 }
515 if (sbsec->flags & CONTEXT_MNT) {
516 rc = security_sid_to_context(sbsec->mntpoint_sid, &context, &len);
517 if (rc)
518 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500519 opts->mnt_opts[i] = context;
520 opts->mnt_opts_flags[i++] = CONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500521 }
522 if (sbsec->flags & DEFCONTEXT_MNT) {
523 rc = security_sid_to_context(sbsec->def_sid, &context, &len);
524 if (rc)
525 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500526 opts->mnt_opts[i] = context;
527 opts->mnt_opts_flags[i++] = DEFCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500528 }
529 if (sbsec->flags & ROOTCONTEXT_MNT) {
530 struct inode *root = sbsec->sb->s_root->d_inode;
531 struct inode_security_struct *isec = root->i_security;
532
533 rc = security_sid_to_context(isec->sid, &context, &len);
534 if (rc)
535 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500536 opts->mnt_opts[i] = context;
537 opts->mnt_opts_flags[i++] = ROOTCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500538 }
David P. Quigley11689d42009-01-16 09:22:03 -0500539 if (sbsec->flags & SE_SBLABELSUPP) {
540 opts->mnt_opts[i] = NULL;
541 opts->mnt_opts_flags[i++] = SE_SBLABELSUPP;
542 }
Eric Parisc9180a52007-11-30 13:00:35 -0500543
Eric Parise0007522008-03-05 10:31:54 -0500544 BUG_ON(i != opts->num_mnt_opts);
Eric Parisc9180a52007-11-30 13:00:35 -0500545
546 return 0;
547
548out_free:
Eric Parise0007522008-03-05 10:31:54 -0500549 security_free_mnt_opts(opts);
Eric Parisc9180a52007-11-30 13:00:35 -0500550 return rc;
551}
552
553static int bad_option(struct superblock_security_struct *sbsec, char flag,
554 u32 old_sid, u32 new_sid)
555{
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500556 char mnt_flags = sbsec->flags & SE_MNTMASK;
557
Eric Parisc9180a52007-11-30 13:00:35 -0500558 /* check if the old mount command had the same options */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500559 if (sbsec->flags & SE_SBINITIALIZED)
Eric Parisc9180a52007-11-30 13:00:35 -0500560 if (!(sbsec->flags & flag) ||
561 (old_sid != new_sid))
562 return 1;
563
564 /* check if we were passed the same options twice,
565 * aka someone passed context=a,context=b
566 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500567 if (!(sbsec->flags & SE_SBINITIALIZED))
568 if (mnt_flags & flag)
Eric Parisc9180a52007-11-30 13:00:35 -0500569 return 1;
570 return 0;
571}
Eric Parise0007522008-03-05 10:31:54 -0500572
Eric Parisc9180a52007-11-30 13:00:35 -0500573/*
574 * Allow filesystems with binary mount data to explicitly set mount point
575 * labeling information.
576 */
Eric Parise0007522008-03-05 10:31:54 -0500577static int selinux_set_mnt_opts(struct super_block *sb,
578 struct security_mnt_opts *opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500579{
David Howells275bb412008-11-14 10:39:19 +1100580 const struct cred *cred = current_cred();
Eric Parisc9180a52007-11-30 13:00:35 -0500581 int rc = 0, i;
Eric Parisc9180a52007-11-30 13:00:35 -0500582 struct superblock_security_struct *sbsec = sb->s_security;
583 const char *name = sb->s_type->name;
James Morris089be432008-07-15 18:32:49 +1000584 struct inode *inode = sbsec->sb->s_root->d_inode;
585 struct inode_security_struct *root_isec = inode->i_security;
Eric Parisc9180a52007-11-30 13:00:35 -0500586 u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
587 u32 defcontext_sid = 0;
Eric Parise0007522008-03-05 10:31:54 -0500588 char **mount_options = opts->mnt_opts;
589 int *flags = opts->mnt_opts_flags;
590 int num_opts = opts->num_mnt_opts;
Eric Parisc9180a52007-11-30 13:00:35 -0500591
592 mutex_lock(&sbsec->lock);
593
594 if (!ss_initialized) {
595 if (!num_opts) {
596 /* Defer initialization until selinux_complete_init,
597 after the initial policy is loaded and the security
598 server is ready to handle calls. */
Eric Parisc9180a52007-11-30 13:00:35 -0500599 goto out;
600 }
601 rc = -EINVAL;
Eric Paris744ba352008-04-17 11:52:44 -0400602 printk(KERN_WARNING "SELinux: Unable to set superblock options "
603 "before the security server is initialized\n");
Eric Parisc9180a52007-11-30 13:00:35 -0500604 goto out;
605 }
606
607 /*
Eric Parise0007522008-03-05 10:31:54 -0500608 * Binary mount data FS will come through this function twice. Once
609 * from an explicit call and once from the generic calls from the vfs.
610 * Since the generic VFS calls will not contain any security mount data
611 * we need to skip the double mount verification.
612 *
613 * This does open a hole in which we will not notice if the first
614 * mount using this sb set explict options and a second mount using
615 * this sb does not set any security options. (The first options
616 * will be used for both mounts)
617 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500618 if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
Eric Parise0007522008-03-05 10:31:54 -0500619 && (num_opts == 0))
Eric Parisf5269712008-05-14 11:27:45 -0400620 goto out;
Eric Parise0007522008-03-05 10:31:54 -0500621
622 /*
Eric Parisc9180a52007-11-30 13:00:35 -0500623 * parse the mount options, check if they are valid sids.
624 * also check if someone is trying to mount the same sb more
625 * than once with different security options.
626 */
627 for (i = 0; i < num_opts; i++) {
628 u32 sid;
David P. Quigley11689d42009-01-16 09:22:03 -0500629
630 if (flags[i] == SE_SBLABELSUPP)
631 continue;
Eric Parisc9180a52007-11-30 13:00:35 -0500632 rc = security_context_to_sid(mount_options[i],
633 strlen(mount_options[i]), &sid);
634 if (rc) {
635 printk(KERN_WARNING "SELinux: security_context_to_sid"
636 "(%s) failed for (dev %s, type %s) errno=%d\n",
637 mount_options[i], sb->s_id, name, rc);
638 goto out;
639 }
640 switch (flags[i]) {
641 case FSCONTEXT_MNT:
642 fscontext_sid = sid;
643
644 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
645 fscontext_sid))
646 goto out_double_mount;
647
648 sbsec->flags |= FSCONTEXT_MNT;
649 break;
650 case CONTEXT_MNT:
651 context_sid = sid;
652
653 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
654 context_sid))
655 goto out_double_mount;
656
657 sbsec->flags |= CONTEXT_MNT;
658 break;
659 case ROOTCONTEXT_MNT:
660 rootcontext_sid = sid;
661
662 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
663 rootcontext_sid))
664 goto out_double_mount;
665
666 sbsec->flags |= ROOTCONTEXT_MNT;
667
668 break;
669 case DEFCONTEXT_MNT:
670 defcontext_sid = sid;
671
672 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
673 defcontext_sid))
674 goto out_double_mount;
675
676 sbsec->flags |= DEFCONTEXT_MNT;
677
678 break;
679 default:
680 rc = -EINVAL;
681 goto out;
682 }
683 }
684
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500685 if (sbsec->flags & SE_SBINITIALIZED) {
Eric Parisc9180a52007-11-30 13:00:35 -0500686 /* previously mounted with options, but not on this attempt? */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500687 if ((sbsec->flags & SE_MNTMASK) && !num_opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500688 goto out_double_mount;
689 rc = 0;
690 goto out;
691 }
692
James Morris089be432008-07-15 18:32:49 +1000693 if (strcmp(sb->s_type->name, "proc") == 0)
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500694 sbsec->flags |= SE_SBPROC;
Eric Parisc9180a52007-11-30 13:00:35 -0500695
696 /* Determine the labeling behavior to use for this filesystem type. */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500697 rc = security_fs_use((sbsec->flags & SE_SBPROC) ? "proc" : sb->s_type->name, &sbsec->behavior, &sbsec->sid);
Eric Parisc9180a52007-11-30 13:00:35 -0500698 if (rc) {
699 printk(KERN_WARNING "%s: security_fs_use(%s) returned %d\n",
James Morris089be432008-07-15 18:32:49 +1000700 __func__, sb->s_type->name, rc);
Eric Parisc9180a52007-11-30 13:00:35 -0500701 goto out;
702 }
703
704 /* sets the context of the superblock for the fs being mounted. */
705 if (fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100706 rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500707 if (rc)
708 goto out;
709
710 sbsec->sid = fscontext_sid;
711 }
712
713 /*
714 * Switch to using mount point labeling behavior.
715 * sets the label used on all file below the mountpoint, and will set
716 * the superblock context if not already set.
717 */
718 if (context_sid) {
719 if (!fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100720 rc = may_context_mount_sb_relabel(context_sid, sbsec,
721 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500722 if (rc)
723 goto out;
724 sbsec->sid = context_sid;
725 } else {
David Howells275bb412008-11-14 10:39:19 +1100726 rc = may_context_mount_inode_relabel(context_sid, sbsec,
727 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500728 if (rc)
729 goto out;
730 }
731 if (!rootcontext_sid)
732 rootcontext_sid = context_sid;
733
734 sbsec->mntpoint_sid = context_sid;
735 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
736 }
737
738 if (rootcontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100739 rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec,
740 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500741 if (rc)
742 goto out;
743
744 root_isec->sid = rootcontext_sid;
745 root_isec->initialized = 1;
746 }
747
748 if (defcontext_sid) {
749 if (sbsec->behavior != SECURITY_FS_USE_XATTR) {
750 rc = -EINVAL;
751 printk(KERN_WARNING "SELinux: defcontext option is "
752 "invalid for this filesystem type\n");
753 goto out;
754 }
755
756 if (defcontext_sid != sbsec->def_sid) {
757 rc = may_context_mount_inode_relabel(defcontext_sid,
David Howells275bb412008-11-14 10:39:19 +1100758 sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500759 if (rc)
760 goto out;
761 }
762
763 sbsec->def_sid = defcontext_sid;
764 }
765
766 rc = sb_finish_set_opts(sb);
767out:
Eric Parisbc7e9822006-09-25 23:32:02 -0700768 mutex_unlock(&sbsec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 return rc;
Eric Parisc9180a52007-11-30 13:00:35 -0500770out_double_mount:
771 rc = -EINVAL;
772 printk(KERN_WARNING "SELinux: mount invalid. Same superblock, different "
773 "security settings for (dev %s, type %s)\n", sb->s_id, name);
774 goto out;
775}
776
777static void selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
778 struct super_block *newsb)
779{
780 const struct superblock_security_struct *oldsbsec = oldsb->s_security;
781 struct superblock_security_struct *newsbsec = newsb->s_security;
782
783 int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT);
784 int set_context = (oldsbsec->flags & CONTEXT_MNT);
785 int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT);
786
Eric Paris0f5e6422008-04-21 16:24:11 -0400787 /*
788 * if the parent was able to be mounted it clearly had no special lsm
Al Viroe8c26252010-03-23 06:36:54 -0400789 * mount options. thus we can safely deal with this superblock later
Eric Paris0f5e6422008-04-21 16:24:11 -0400790 */
Al Viroe8c26252010-03-23 06:36:54 -0400791 if (!ss_initialized)
Eric Paris0f5e6422008-04-21 16:24:11 -0400792 return;
Eric Parisc9180a52007-11-30 13:00:35 -0500793
Eric Parisc9180a52007-11-30 13:00:35 -0500794 /* how can we clone if the old one wasn't set up?? */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500795 BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED));
Eric Parisc9180a52007-11-30 13:00:35 -0500796
Eric Paris5a552612008-04-09 14:08:35 -0400797 /* if fs is reusing a sb, just let its options stand... */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500798 if (newsbsec->flags & SE_SBINITIALIZED)
Eric Paris5a552612008-04-09 14:08:35 -0400799 return;
800
Eric Parisc9180a52007-11-30 13:00:35 -0500801 mutex_lock(&newsbsec->lock);
802
803 newsbsec->flags = oldsbsec->flags;
804
805 newsbsec->sid = oldsbsec->sid;
806 newsbsec->def_sid = oldsbsec->def_sid;
807 newsbsec->behavior = oldsbsec->behavior;
808
809 if (set_context) {
810 u32 sid = oldsbsec->mntpoint_sid;
811
812 if (!set_fscontext)
813 newsbsec->sid = sid;
814 if (!set_rootcontext) {
815 struct inode *newinode = newsb->s_root->d_inode;
816 struct inode_security_struct *newisec = newinode->i_security;
817 newisec->sid = sid;
818 }
819 newsbsec->mntpoint_sid = sid;
820 }
821 if (set_rootcontext) {
822 const struct inode *oldinode = oldsb->s_root->d_inode;
823 const struct inode_security_struct *oldisec = oldinode->i_security;
824 struct inode *newinode = newsb->s_root->d_inode;
825 struct inode_security_struct *newisec = newinode->i_security;
826
827 newisec->sid = oldisec->sid;
828 }
829
830 sb_finish_set_opts(newsb);
831 mutex_unlock(&newsbsec->lock);
832}
833
Adrian Bunk2e1479d2008-03-17 22:29:23 +0200834static int selinux_parse_opts_str(char *options,
835 struct security_mnt_opts *opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500836{
Eric Parise0007522008-03-05 10:31:54 -0500837 char *p;
Eric Parisc9180a52007-11-30 13:00:35 -0500838 char *context = NULL, *defcontext = NULL;
839 char *fscontext = NULL, *rootcontext = NULL;
Eric Parise0007522008-03-05 10:31:54 -0500840 int rc, num_mnt_opts = 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500841
Eric Parise0007522008-03-05 10:31:54 -0500842 opts->num_mnt_opts = 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500843
844 /* Standard string-based options. */
845 while ((p = strsep(&options, "|")) != NULL) {
846 int token;
847 substring_t args[MAX_OPT_ARGS];
848
849 if (!*p)
850 continue;
851
852 token = match_token(p, tokens, args);
853
854 switch (token) {
855 case Opt_context:
856 if (context || defcontext) {
857 rc = -EINVAL;
858 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
859 goto out_err;
860 }
861 context = match_strdup(&args[0]);
862 if (!context) {
863 rc = -ENOMEM;
864 goto out_err;
865 }
866 break;
867
868 case Opt_fscontext:
869 if (fscontext) {
870 rc = -EINVAL;
871 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
872 goto out_err;
873 }
874 fscontext = match_strdup(&args[0]);
875 if (!fscontext) {
876 rc = -ENOMEM;
877 goto out_err;
878 }
879 break;
880
881 case Opt_rootcontext:
882 if (rootcontext) {
883 rc = -EINVAL;
884 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
885 goto out_err;
886 }
887 rootcontext = match_strdup(&args[0]);
888 if (!rootcontext) {
889 rc = -ENOMEM;
890 goto out_err;
891 }
892 break;
893
894 case Opt_defcontext:
895 if (context || defcontext) {
896 rc = -EINVAL;
897 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
898 goto out_err;
899 }
900 defcontext = match_strdup(&args[0]);
901 if (!defcontext) {
902 rc = -ENOMEM;
903 goto out_err;
904 }
905 break;
David P. Quigley11689d42009-01-16 09:22:03 -0500906 case Opt_labelsupport:
907 break;
Eric Parisc9180a52007-11-30 13:00:35 -0500908 default:
909 rc = -EINVAL;
910 printk(KERN_WARNING "SELinux: unknown mount option\n");
911 goto out_err;
912
913 }
914 }
915
Eric Parise0007522008-03-05 10:31:54 -0500916 rc = -ENOMEM;
917 opts->mnt_opts = kcalloc(NUM_SEL_MNT_OPTS, sizeof(char *), GFP_ATOMIC);
918 if (!opts->mnt_opts)
919 goto out_err;
920
921 opts->mnt_opts_flags = kcalloc(NUM_SEL_MNT_OPTS, sizeof(int), GFP_ATOMIC);
922 if (!opts->mnt_opts_flags) {
923 kfree(opts->mnt_opts);
924 goto out_err;
Eric Parisc9180a52007-11-30 13:00:35 -0500925 }
926
Eric Parise0007522008-03-05 10:31:54 -0500927 if (fscontext) {
928 opts->mnt_opts[num_mnt_opts] = fscontext;
929 opts->mnt_opts_flags[num_mnt_opts++] = FSCONTEXT_MNT;
930 }
931 if (context) {
932 opts->mnt_opts[num_mnt_opts] = context;
933 opts->mnt_opts_flags[num_mnt_opts++] = CONTEXT_MNT;
934 }
935 if (rootcontext) {
936 opts->mnt_opts[num_mnt_opts] = rootcontext;
937 opts->mnt_opts_flags[num_mnt_opts++] = ROOTCONTEXT_MNT;
938 }
939 if (defcontext) {
940 opts->mnt_opts[num_mnt_opts] = defcontext;
941 opts->mnt_opts_flags[num_mnt_opts++] = DEFCONTEXT_MNT;
942 }
943
944 opts->num_mnt_opts = num_mnt_opts;
945 return 0;
946
Eric Parisc9180a52007-11-30 13:00:35 -0500947out_err:
948 kfree(context);
949 kfree(defcontext);
950 kfree(fscontext);
951 kfree(rootcontext);
952 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953}
Eric Parise0007522008-03-05 10:31:54 -0500954/*
955 * string mount options parsing and call set the sbsec
956 */
957static int superblock_doinit(struct super_block *sb, void *data)
958{
959 int rc = 0;
960 char *options = data;
961 struct security_mnt_opts opts;
962
963 security_init_mnt_opts(&opts);
964
965 if (!data)
966 goto out;
967
968 BUG_ON(sb->s_type->fs_flags & FS_BINARY_MOUNTDATA);
969
970 rc = selinux_parse_opts_str(options, &opts);
971 if (rc)
972 goto out_err;
973
974out:
975 rc = selinux_set_mnt_opts(sb, &opts);
976
977out_err:
978 security_free_mnt_opts(&opts);
979 return rc;
980}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Adrian Bunk3583a712008-07-22 20:21:23 +0300982static void selinux_write_opts(struct seq_file *m,
983 struct security_mnt_opts *opts)
Eric Paris2069f452008-07-04 09:47:13 +1000984{
985 int i;
986 char *prefix;
987
988 for (i = 0; i < opts->num_mnt_opts; i++) {
David P. Quigley11689d42009-01-16 09:22:03 -0500989 char *has_comma;
990
991 if (opts->mnt_opts[i])
992 has_comma = strchr(opts->mnt_opts[i], ',');
993 else
994 has_comma = NULL;
Eric Paris2069f452008-07-04 09:47:13 +1000995
996 switch (opts->mnt_opts_flags[i]) {
997 case CONTEXT_MNT:
998 prefix = CONTEXT_STR;
999 break;
1000 case FSCONTEXT_MNT:
1001 prefix = FSCONTEXT_STR;
1002 break;
1003 case ROOTCONTEXT_MNT:
1004 prefix = ROOTCONTEXT_STR;
1005 break;
1006 case DEFCONTEXT_MNT:
1007 prefix = DEFCONTEXT_STR;
1008 break;
David P. Quigley11689d42009-01-16 09:22:03 -05001009 case SE_SBLABELSUPP:
1010 seq_putc(m, ',');
1011 seq_puts(m, LABELSUPP_STR);
1012 continue;
Eric Paris2069f452008-07-04 09:47:13 +10001013 default:
1014 BUG();
Eric Parisa35c6c82011-04-20 10:21:28 -04001015 return;
Eric Paris2069f452008-07-04 09:47:13 +10001016 };
1017 /* we need a comma before each option */
1018 seq_putc(m, ',');
1019 seq_puts(m, prefix);
1020 if (has_comma)
1021 seq_putc(m, '\"');
1022 seq_puts(m, opts->mnt_opts[i]);
1023 if (has_comma)
1024 seq_putc(m, '\"');
1025 }
1026}
1027
1028static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
1029{
1030 struct security_mnt_opts opts;
1031 int rc;
1032
1033 rc = selinux_get_mnt_opts(sb, &opts);
Eric Paris383795c2008-07-29 17:07:26 -04001034 if (rc) {
1035 /* before policy load we may get EINVAL, don't show anything */
1036 if (rc == -EINVAL)
1037 rc = 0;
Eric Paris2069f452008-07-04 09:47:13 +10001038 return rc;
Eric Paris383795c2008-07-29 17:07:26 -04001039 }
Eric Paris2069f452008-07-04 09:47:13 +10001040
1041 selinux_write_opts(m, &opts);
1042
1043 security_free_mnt_opts(&opts);
1044
1045 return rc;
1046}
1047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048static inline u16 inode_mode_to_security_class(umode_t mode)
1049{
1050 switch (mode & S_IFMT) {
1051 case S_IFSOCK:
1052 return SECCLASS_SOCK_FILE;
1053 case S_IFLNK:
1054 return SECCLASS_LNK_FILE;
1055 case S_IFREG:
1056 return SECCLASS_FILE;
1057 case S_IFBLK:
1058 return SECCLASS_BLK_FILE;
1059 case S_IFDIR:
1060 return SECCLASS_DIR;
1061 case S_IFCHR:
1062 return SECCLASS_CHR_FILE;
1063 case S_IFIFO:
1064 return SECCLASS_FIFO_FILE;
1065
1066 }
1067
1068 return SECCLASS_FILE;
1069}
1070
James Morris13402582005-09-30 14:24:34 -04001071static inline int default_protocol_stream(int protocol)
1072{
1073 return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP);
1074}
1075
1076static inline int default_protocol_dgram(int protocol)
1077{
1078 return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
1079}
1080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081static inline u16 socket_type_to_security_class(int family, int type, int protocol)
1082{
1083 switch (family) {
1084 case PF_UNIX:
1085 switch (type) {
1086 case SOCK_STREAM:
1087 case SOCK_SEQPACKET:
1088 return SECCLASS_UNIX_STREAM_SOCKET;
1089 case SOCK_DGRAM:
1090 return SECCLASS_UNIX_DGRAM_SOCKET;
1091 }
1092 break;
1093 case PF_INET:
1094 case PF_INET6:
1095 switch (type) {
1096 case SOCK_STREAM:
James Morris13402582005-09-30 14:24:34 -04001097 if (default_protocol_stream(protocol))
1098 return SECCLASS_TCP_SOCKET;
1099 else
1100 return SECCLASS_RAWIP_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 case SOCK_DGRAM:
James Morris13402582005-09-30 14:24:34 -04001102 if (default_protocol_dgram(protocol))
1103 return SECCLASS_UDP_SOCKET;
1104 else
1105 return SECCLASS_RAWIP_SOCKET;
James Morris2ee92d42006-11-13 16:09:01 -08001106 case SOCK_DCCP:
1107 return SECCLASS_DCCP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001108 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 return SECCLASS_RAWIP_SOCKET;
1110 }
1111 break;
1112 case PF_NETLINK:
1113 switch (protocol) {
1114 case NETLINK_ROUTE:
1115 return SECCLASS_NETLINK_ROUTE_SOCKET;
1116 case NETLINK_FIREWALL:
1117 return SECCLASS_NETLINK_FIREWALL_SOCKET;
Pavel Emelyanov7f1fb602011-12-06 07:56:43 +00001118 case NETLINK_SOCK_DIAG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 return SECCLASS_NETLINK_TCPDIAG_SOCKET;
1120 case NETLINK_NFLOG:
1121 return SECCLASS_NETLINK_NFLOG_SOCKET;
1122 case NETLINK_XFRM:
1123 return SECCLASS_NETLINK_XFRM_SOCKET;
1124 case NETLINK_SELINUX:
1125 return SECCLASS_NETLINK_SELINUX_SOCKET;
1126 case NETLINK_AUDIT:
1127 return SECCLASS_NETLINK_AUDIT_SOCKET;
1128 case NETLINK_IP6_FW:
1129 return SECCLASS_NETLINK_IP6FW_SOCKET;
1130 case NETLINK_DNRTMSG:
1131 return SECCLASS_NETLINK_DNRT_SOCKET;
James Morris0c9b7942005-04-16 15:24:13 -07001132 case NETLINK_KOBJECT_UEVENT:
1133 return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 default:
1135 return SECCLASS_NETLINK_SOCKET;
1136 }
1137 case PF_PACKET:
1138 return SECCLASS_PACKET_SOCKET;
1139 case PF_KEY:
1140 return SECCLASS_KEY_SOCKET;
Christopher J. PeBenito3e3ff152006-06-09 00:25:03 -07001141 case PF_APPLETALK:
1142 return SECCLASS_APPLETALK_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 }
1144
1145 return SECCLASS_SOCKET;
1146}
1147
1148#ifdef CONFIG_PROC_FS
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001149static int selinux_proc_get_sid(struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 u16 tclass,
1151 u32 *sid)
1152{
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001153 int rc;
1154 char *buffer, *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
Eric Paris828dfe12008-04-17 13:17:49 -04001156 buffer = (char *)__get_free_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 if (!buffer)
1158 return -ENOMEM;
1159
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001160 path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
1161 if (IS_ERR(path))
1162 rc = PTR_ERR(path);
1163 else {
1164 /* each process gets a /proc/PID/ entry. Strip off the
1165 * PID part to get a valid selinux labeling.
1166 * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
1167 while (path[1] >= '0' && path[1] <= '9') {
1168 path[1] = '/';
1169 path++;
1170 }
1171 rc = security_genfs_sid("proc", path, tclass, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 free_page((unsigned long)buffer);
1174 return rc;
1175}
1176#else
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001177static int selinux_proc_get_sid(struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 u16 tclass,
1179 u32 *sid)
1180{
1181 return -EINVAL;
1182}
1183#endif
1184
1185/* The inode's security attributes must be initialized before first use. */
1186static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
1187{
1188 struct superblock_security_struct *sbsec = NULL;
1189 struct inode_security_struct *isec = inode->i_security;
1190 u32 sid;
1191 struct dentry *dentry;
1192#define INITCONTEXTLEN 255
1193 char *context = NULL;
1194 unsigned len = 0;
1195 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
1197 if (isec->initialized)
1198 goto out;
1199
Eric Paris23970742006-09-25 23:32:01 -07001200 mutex_lock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 if (isec->initialized)
Eric Paris23970742006-09-25 23:32:01 -07001202 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
1204 sbsec = inode->i_sb->s_security;
David P. Quigley0d90a7e2009-01-16 09:22:02 -05001205 if (!(sbsec->flags & SE_SBINITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 /* Defer initialization until selinux_complete_init,
1207 after the initial policy is loaded and the security
1208 server is ready to handle calls. */
1209 spin_lock(&sbsec->isec_lock);
1210 if (list_empty(&isec->list))
1211 list_add(&isec->list, &sbsec->isec_head);
1212 spin_unlock(&sbsec->isec_lock);
Eric Paris23970742006-09-25 23:32:01 -07001213 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 }
1215
1216 switch (sbsec->behavior) {
1217 case SECURITY_FS_USE_XATTR:
1218 if (!inode->i_op->getxattr) {
1219 isec->sid = sbsec->def_sid;
1220 break;
1221 }
1222
1223 /* Need a dentry, since the xattr API requires one.
1224 Life would be simpler if we could just pass the inode. */
1225 if (opt_dentry) {
1226 /* Called from d_instantiate or d_splice_alias. */
1227 dentry = dget(opt_dentry);
1228 } else {
1229 /* Called from selinux_complete_init, try to find a dentry. */
1230 dentry = d_find_alias(inode);
1231 }
1232 if (!dentry) {
Eric Parisdf7f54c2009-03-09 14:35:58 -04001233 /*
1234 * this is can be hit on boot when a file is accessed
1235 * before the policy is loaded. When we load policy we
1236 * may find inodes that have no dentry on the
1237 * sbsec->isec_head list. No reason to complain as these
1238 * will get fixed up the next time we go through
1239 * inode_doinit with a dentry, before these inodes could
1240 * be used again by userspace.
1241 */
Eric Paris23970742006-09-25 23:32:01 -07001242 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 }
1244
1245 len = INITCONTEXTLEN;
Eric Paris4cb912f2009-02-12 14:50:05 -05001246 context = kmalloc(len+1, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 if (!context) {
1248 rc = -ENOMEM;
1249 dput(dentry);
Eric Paris23970742006-09-25 23:32:01 -07001250 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 }
Eric Paris4cb912f2009-02-12 14:50:05 -05001252 context[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX,
1254 context, len);
1255 if (rc == -ERANGE) {
James Morris314dabb2009-08-10 22:00:13 +10001256 kfree(context);
1257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 /* Need a larger buffer. Query for the right size. */
1259 rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX,
1260 NULL, 0);
1261 if (rc < 0) {
1262 dput(dentry);
Eric Paris23970742006-09-25 23:32:01 -07001263 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 len = rc;
Eric Paris4cb912f2009-02-12 14:50:05 -05001266 context = kmalloc(len+1, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 if (!context) {
1268 rc = -ENOMEM;
1269 dput(dentry);
Eric Paris23970742006-09-25 23:32:01 -07001270 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 }
Eric Paris4cb912f2009-02-12 14:50:05 -05001272 context[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 rc = inode->i_op->getxattr(dentry,
1274 XATTR_NAME_SELINUX,
1275 context, len);
1276 }
1277 dput(dentry);
1278 if (rc < 0) {
1279 if (rc != -ENODATA) {
Eric Paris744ba352008-04-17 11:52:44 -04001280 printk(KERN_WARNING "SELinux: %s: getxattr returned "
Harvey Harrisondd6f9532008-03-06 10:03:59 +11001281 "%d for dev=%s ino=%ld\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 -rc, inode->i_sb->s_id, inode->i_ino);
1283 kfree(context);
Eric Paris23970742006-09-25 23:32:01 -07001284 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 }
1286 /* Map ENODATA to the default file SID */
1287 sid = sbsec->def_sid;
1288 rc = 0;
1289 } else {
James Morrisf5c1d5b2005-07-28 01:07:37 -07001290 rc = security_context_to_sid_default(context, rc, &sid,
Stephen Smalley869ab512008-04-04 08:46:05 -04001291 sbsec->def_sid,
1292 GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 if (rc) {
Eric Paris4ba0a8a2009-02-12 15:01:10 -05001294 char *dev = inode->i_sb->s_id;
1295 unsigned long ino = inode->i_ino;
1296
1297 if (rc == -EINVAL) {
1298 if (printk_ratelimit())
1299 printk(KERN_NOTICE "SELinux: inode=%lu on dev=%s was found to have an invalid "
1300 "context=%s. This indicates you may need to relabel the inode or the "
1301 "filesystem in question.\n", ino, dev, context);
1302 } else {
1303 printk(KERN_WARNING "SELinux: %s: context_to_sid(%s) "
1304 "returned %d for dev=%s ino=%ld\n",
1305 __func__, context, -rc, dev, ino);
1306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 kfree(context);
1308 /* Leave with the unlabeled SID */
1309 rc = 0;
1310 break;
1311 }
1312 }
1313 kfree(context);
1314 isec->sid = sid;
1315 break;
1316 case SECURITY_FS_USE_TASK:
1317 isec->sid = isec->task_sid;
1318 break;
1319 case SECURITY_FS_USE_TRANS:
1320 /* Default to the fs SID. */
1321 isec->sid = sbsec->sid;
1322
1323 /* Try to obtain a transition SID. */
1324 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Eric Paris652bb9b2011-02-01 11:05:40 -05001325 rc = security_transition_sid(isec->task_sid, sbsec->sid,
1326 isec->sclass, NULL, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 if (rc)
Eric Paris23970742006-09-25 23:32:01 -07001328 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 isec->sid = sid;
1330 break;
Eric Parisc312feb2006-07-10 04:43:53 -07001331 case SECURITY_FS_USE_MNTPOINT:
1332 isec->sid = sbsec->mntpoint_sid;
1333 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 default:
Eric Parisc312feb2006-07-10 04:43:53 -07001335 /* Default to the fs superblock SID. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 isec->sid = sbsec->sid;
1337
David P. Quigley0d90a7e2009-01-16 09:22:02 -05001338 if ((sbsec->flags & SE_SBPROC) && !S_ISLNK(inode->i_mode)) {
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001339 if (opt_dentry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001341 rc = selinux_proc_get_sid(opt_dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 isec->sclass,
1343 &sid);
1344 if (rc)
Eric Paris23970742006-09-25 23:32:01 -07001345 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 isec->sid = sid;
1347 }
1348 }
1349 break;
1350 }
1351
1352 isec->initialized = 1;
1353
Eric Paris23970742006-09-25 23:32:01 -07001354out_unlock:
1355 mutex_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356out:
1357 if (isec->sclass == SECCLASS_FILE)
1358 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 return rc;
1360}
1361
1362/* Convert a Linux signal to an access vector. */
1363static inline u32 signal_to_av(int sig)
1364{
1365 u32 perm = 0;
1366
1367 switch (sig) {
1368 case SIGCHLD:
1369 /* Commonly granted from child to parent. */
1370 perm = PROCESS__SIGCHLD;
1371 break;
1372 case SIGKILL:
1373 /* Cannot be caught or ignored */
1374 perm = PROCESS__SIGKILL;
1375 break;
1376 case SIGSTOP:
1377 /* Cannot be caught or ignored */
1378 perm = PROCESS__SIGSTOP;
1379 break;
1380 default:
1381 /* All other signals. */
1382 perm = PROCESS__SIGNAL;
1383 break;
1384 }
1385
1386 return perm;
1387}
1388
David Howells275bb412008-11-14 10:39:19 +11001389/*
David Howellsd84f4f92008-11-14 10:39:23 +11001390 * Check permission between a pair of credentials
1391 * fork check, ptrace check, etc.
1392 */
1393static int cred_has_perm(const struct cred *actor,
1394 const struct cred *target,
1395 u32 perms)
1396{
1397 u32 asid = cred_sid(actor), tsid = cred_sid(target);
1398
1399 return avc_has_perm(asid, tsid, SECCLASS_PROCESS, perms, NULL);
1400}
1401
1402/*
David Howells88e67f32008-11-14 10:39:21 +11001403 * Check permission between a pair of tasks, e.g. signal checks,
David Howells275bb412008-11-14 10:39:19 +11001404 * fork check, ptrace check, etc.
1405 * tsk1 is the actor and tsk2 is the target
David Howells3b11a1d2008-11-14 10:39:26 +11001406 * - this uses the default subjective creds of tsk1
David Howells275bb412008-11-14 10:39:19 +11001407 */
1408static int task_has_perm(const struct task_struct *tsk1,
1409 const struct task_struct *tsk2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 u32 perms)
1411{
David Howells275bb412008-11-14 10:39:19 +11001412 const struct task_security_struct *__tsec1, *__tsec2;
1413 u32 sid1, sid2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
David Howells275bb412008-11-14 10:39:19 +11001415 rcu_read_lock();
1416 __tsec1 = __task_cred(tsk1)->security; sid1 = __tsec1->sid;
1417 __tsec2 = __task_cred(tsk2)->security; sid2 = __tsec2->sid;
1418 rcu_read_unlock();
1419 return avc_has_perm(sid1, sid2, SECCLASS_PROCESS, perms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420}
1421
David Howells3b11a1d2008-11-14 10:39:26 +11001422/*
1423 * Check permission between current and another task, e.g. signal checks,
1424 * fork check, ptrace check, etc.
1425 * current is the actor and tsk2 is the target
1426 * - this uses current's subjective creds
1427 */
1428static int current_has_perm(const struct task_struct *tsk,
1429 u32 perms)
1430{
1431 u32 sid, tsid;
1432
1433 sid = current_sid();
1434 tsid = task_sid(tsk);
1435 return avc_has_perm(sid, tsid, SECCLASS_PROCESS, perms, NULL);
1436}
1437
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001438#if CAP_LAST_CAP > 63
1439#error Fix SELinux to handle capabilities > 63.
1440#endif
1441
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442/* Check whether a task is allowed to use a capability. */
Eric Paris6a9de492012-01-03 12:25:14 -05001443static int cred_has_capability(const struct cred *cred,
Eric Paris06112162008-11-11 22:02:50 +11001444 int cap, int audit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
Thomas Liu2bf49692009-07-14 12:14:09 -04001446 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001447 struct selinux_audit_data sad = {0,};
Eric Paris06112162008-11-11 22:02:50 +11001448 struct av_decision avd;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001449 u16 sclass;
David Howells3699c532009-01-06 22:27:01 +00001450 u32 sid = cred_sid(cred);
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001451 u32 av = CAP_TO_MASK(cap);
Eric Paris06112162008-11-11 22:02:50 +11001452 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Thomas Liu2bf49692009-07-14 12:14:09 -04001454 COMMON_AUDIT_DATA_INIT(&ad, CAP);
Eric Paris3b3b0e42012-04-03 09:37:02 -07001455 ad.selinux_audit_data = &sad;
Eric Paris6a9de492012-01-03 12:25:14 -05001456 ad.tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 ad.u.cap = cap;
1458
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001459 switch (CAP_TO_INDEX(cap)) {
1460 case 0:
1461 sclass = SECCLASS_CAPABILITY;
1462 break;
1463 case 1:
1464 sclass = SECCLASS_CAPABILITY2;
1465 break;
1466 default:
1467 printk(KERN_ERR
1468 "SELinux: out of range capability %d\n", cap);
1469 BUG();
Eric Parisa35c6c82011-04-20 10:21:28 -04001470 return -EINVAL;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001471 }
Eric Paris06112162008-11-11 22:02:50 +11001472
David Howells275bb412008-11-14 10:39:19 +11001473 rc = avc_has_perm_noaudit(sid, sid, sclass, av, 0, &avd);
Eric Paris9ade0cf2011-04-25 16:26:29 -04001474 if (audit == SECURITY_CAP_AUDIT) {
1475 int rc2 = avc_audit(sid, sid, sclass, av, &avd, rc, &ad, 0);
1476 if (rc2)
1477 return rc2;
1478 }
Eric Paris06112162008-11-11 22:02:50 +11001479 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480}
1481
1482/* Check whether a task is allowed to use a system operation. */
1483static int task_has_system(struct task_struct *tsk,
1484 u32 perms)
1485{
David Howells275bb412008-11-14 10:39:19 +11001486 u32 sid = task_sid(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
David Howells275bb412008-11-14 10:39:19 +11001488 return avc_has_perm(sid, SECINITSID_KERNEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 SECCLASS_SYSTEM, perms, NULL);
1490}
1491
1492/* Check whether a task has a particular permission to an inode.
1493 The 'adp' parameter is optional and allows other audit
1494 data to be passed (e.g. the dentry). */
David Howells88e67f32008-11-14 10:39:21 +11001495static int inode_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 struct inode *inode,
1497 u32 perms,
Eric Paris9ade0cf2011-04-25 16:26:29 -04001498 struct common_audit_data *adp,
1499 unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 struct inode_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +11001502 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
David Howellse0e81732009-09-02 09:13:40 +01001504 validate_creds(cred);
1505
Eric Paris828dfe12008-04-17 13:17:49 -04001506 if (unlikely(IS_PRIVATE(inode)))
Stephen Smalleybbaca6c2007-02-14 00:34:16 -08001507 return 0;
1508
David Howells88e67f32008-11-14 10:39:21 +11001509 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 isec = inode->i_security;
1511
Eric Paris9ade0cf2011-04-25 16:26:29 -04001512 return avc_has_perm_flags(sid, isec->sid, isec->sclass, perms, adp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513}
1514
Linus Torvalds95f4efb2011-06-08 15:11:56 -07001515static int inode_has_perm_noadp(const struct cred *cred,
1516 struct inode *inode,
1517 u32 perms,
1518 unsigned flags)
1519{
1520 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001521 struct selinux_audit_data sad = {0,};
Linus Torvalds95f4efb2011-06-08 15:11:56 -07001522
1523 COMMON_AUDIT_DATA_INIT(&ad, INODE);
1524 ad.u.inode = inode;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001525 ad.selinux_audit_data = &sad;
Linus Torvalds95f4efb2011-06-08 15:11:56 -07001526 return inode_has_perm(cred, inode, perms, &ad, flags);
1527}
1528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529/* Same as inode_has_perm, but pass explicit audit data containing
1530 the dentry to help the auditing code to more easily generate the
1531 pathname if needed. */
David Howells88e67f32008-11-14 10:39:21 +11001532static inline int dentry_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 struct dentry *dentry,
1534 u32 av)
1535{
1536 struct inode *inode = dentry->d_inode;
Thomas Liu2bf49692009-07-14 12:14:09 -04001537 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001538 struct selinux_audit_data sad = {0,};
David Howells88e67f32008-11-14 10:39:21 +11001539
Eric Paris2875fa02011-04-28 16:04:24 -04001540 COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
1541 ad.u.dentry = dentry;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001542 ad.selinux_audit_data = &sad;
Eric Paris2875fa02011-04-28 16:04:24 -04001543 return inode_has_perm(cred, inode, av, &ad, 0);
1544}
1545
1546/* Same as inode_has_perm, but pass explicit audit data containing
1547 the path to help the auditing code to more easily generate the
1548 pathname if needed. */
1549static inline int path_has_perm(const struct cred *cred,
1550 struct path *path,
1551 u32 av)
1552{
1553 struct inode *inode = path->dentry->d_inode;
1554 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001555 struct selinux_audit_data sad = {0,};
Eric Paris2875fa02011-04-28 16:04:24 -04001556
Eric Parisf48b7392011-04-25 12:54:27 -04001557 COMMON_AUDIT_DATA_INIT(&ad, PATH);
Eric Paris2875fa02011-04-28 16:04:24 -04001558 ad.u.path = *path;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001559 ad.selinux_audit_data = &sad;
Eric Paris9ade0cf2011-04-25 16:26:29 -04001560 return inode_has_perm(cred, inode, av, &ad, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561}
1562
1563/* Check whether a task can use an open file descriptor to
1564 access an inode in a given way. Check access to the
1565 descriptor itself, and then use dentry_has_perm to
1566 check a particular permission to the file.
1567 Access to the descriptor is implicitly granted if it
1568 has the same SID as the process. If av is zero, then
1569 access to the file is not checked, e.g. for cases
1570 where only the descriptor is affected like seek. */
David Howells88e67f32008-11-14 10:39:21 +11001571static int file_has_perm(const struct cred *cred,
1572 struct file *file,
1573 u32 av)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 struct file_security_struct *fsec = file->f_security;
Jan Blunck44707fd2008-02-14 19:38:33 -08001576 struct inode *inode = file->f_path.dentry->d_inode;
Thomas Liu2bf49692009-07-14 12:14:09 -04001577 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001578 struct selinux_audit_data sad = {0,};
David Howells88e67f32008-11-14 10:39:21 +11001579 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 int rc;
1581
Eric Parisf48b7392011-04-25 12:54:27 -04001582 COMMON_AUDIT_DATA_INIT(&ad, PATH);
1583 ad.u.path = file->f_path;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001584 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
David Howells275bb412008-11-14 10:39:19 +11001586 if (sid != fsec->sid) {
1587 rc = avc_has_perm(sid, fsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 SECCLASS_FD,
1589 FD__USE,
1590 &ad);
1591 if (rc)
David Howells88e67f32008-11-14 10:39:21 +11001592 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 }
1594
1595 /* av is zero if only checking access to the descriptor. */
David Howells88e67f32008-11-14 10:39:21 +11001596 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 if (av)
Eric Paris9ade0cf2011-04-25 16:26:29 -04001598 rc = inode_has_perm(cred, inode, av, &ad, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
David Howells88e67f32008-11-14 10:39:21 +11001600out:
1601 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602}
1603
1604/* Check whether a task can create a file. */
1605static int may_create(struct inode *dir,
1606 struct dentry *dentry,
1607 u16 tclass)
1608{
Paul Moore5fb49872010-04-22 14:46:19 -04001609 const struct task_security_struct *tsec = current_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 struct inode_security_struct *dsec;
1611 struct superblock_security_struct *sbsec;
David Howells275bb412008-11-14 10:39:19 +11001612 u32 sid, newsid;
Thomas Liu2bf49692009-07-14 12:14:09 -04001613 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001614 struct selinux_audit_data sad = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 int rc;
1616
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 dsec = dir->i_security;
1618 sbsec = dir->i_sb->s_security;
1619
David Howells275bb412008-11-14 10:39:19 +11001620 sid = tsec->sid;
1621 newsid = tsec->create_sid;
1622
Eric Parisa2694342011-04-25 13:10:27 -04001623 COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
1624 ad.u.dentry = dentry;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001625 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
David Howells275bb412008-11-14 10:39:19 +11001627 rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 DIR__ADD_NAME | DIR__SEARCH,
1629 &ad);
1630 if (rc)
1631 return rc;
1632
David P. Quigleycd895962009-01-16 09:22:04 -05001633 if (!newsid || !(sbsec->flags & SE_SBLABELSUPP)) {
Eric Pariscb1e9222011-04-28 15:11:21 -04001634 rc = security_transition_sid(sid, dsec->sid, tclass,
1635 &dentry->d_name, &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 if (rc)
1637 return rc;
1638 }
1639
David Howells275bb412008-11-14 10:39:19 +11001640 rc = avc_has_perm(sid, newsid, tclass, FILE__CREATE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 if (rc)
1642 return rc;
1643
1644 return avc_has_perm(newsid, sbsec->sid,
1645 SECCLASS_FILESYSTEM,
1646 FILESYSTEM__ASSOCIATE, &ad);
1647}
1648
Michael LeMay4eb582c2006-06-26 00:24:57 -07001649/* Check whether a task can create a key. */
1650static int may_create_key(u32 ksid,
1651 struct task_struct *ctx)
1652{
David Howells275bb412008-11-14 10:39:19 +11001653 u32 sid = task_sid(ctx);
Michael LeMay4eb582c2006-06-26 00:24:57 -07001654
David Howells275bb412008-11-14 10:39:19 +11001655 return avc_has_perm(sid, ksid, SECCLASS_KEY, KEY__CREATE, NULL);
Michael LeMay4eb582c2006-06-26 00:24:57 -07001656}
1657
Eric Paris828dfe12008-04-17 13:17:49 -04001658#define MAY_LINK 0
1659#define MAY_UNLINK 1
1660#define MAY_RMDIR 2
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
1662/* Check whether a task can link, unlink, or rmdir a file/directory. */
1663static int may_link(struct inode *dir,
1664 struct dentry *dentry,
1665 int kind)
1666
1667{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 struct inode_security_struct *dsec, *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001669 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001670 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11001671 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 u32 av;
1673 int rc;
1674
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 dsec = dir->i_security;
1676 isec = dentry->d_inode->i_security;
1677
Eric Parisa2694342011-04-25 13:10:27 -04001678 COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
1679 ad.u.dentry = dentry;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001680 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
1682 av = DIR__SEARCH;
1683 av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
David Howells275bb412008-11-14 10:39:19 +11001684 rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 if (rc)
1686 return rc;
1687
1688 switch (kind) {
1689 case MAY_LINK:
1690 av = FILE__LINK;
1691 break;
1692 case MAY_UNLINK:
1693 av = FILE__UNLINK;
1694 break;
1695 case MAY_RMDIR:
1696 av = DIR__RMDIR;
1697 break;
1698 default:
Eric Paris744ba352008-04-17 11:52:44 -04001699 printk(KERN_WARNING "SELinux: %s: unrecognized kind %d\n",
1700 __func__, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 return 0;
1702 }
1703
David Howells275bb412008-11-14 10:39:19 +11001704 rc = avc_has_perm(sid, isec->sid, isec->sclass, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 return rc;
1706}
1707
1708static inline int may_rename(struct inode *old_dir,
1709 struct dentry *old_dentry,
1710 struct inode *new_dir,
1711 struct dentry *new_dentry)
1712{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001714 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001715 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11001716 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 u32 av;
1718 int old_is_dir, new_is_dir;
1719 int rc;
1720
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 old_dsec = old_dir->i_security;
1722 old_isec = old_dentry->d_inode->i_security;
1723 old_is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
1724 new_dsec = new_dir->i_security;
1725
Eric Parisa2694342011-04-25 13:10:27 -04001726 COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
Eric Paris3b3b0e42012-04-03 09:37:02 -07001727 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Eric Parisa2694342011-04-25 13:10:27 -04001729 ad.u.dentry = old_dentry;
David Howells275bb412008-11-14 10:39:19 +11001730 rc = avc_has_perm(sid, old_dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 DIR__REMOVE_NAME | DIR__SEARCH, &ad);
1732 if (rc)
1733 return rc;
David Howells275bb412008-11-14 10:39:19 +11001734 rc = avc_has_perm(sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 old_isec->sclass, FILE__RENAME, &ad);
1736 if (rc)
1737 return rc;
1738 if (old_is_dir && new_dir != old_dir) {
David Howells275bb412008-11-14 10:39:19 +11001739 rc = avc_has_perm(sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 old_isec->sclass, DIR__REPARENT, &ad);
1741 if (rc)
1742 return rc;
1743 }
1744
Eric Parisa2694342011-04-25 13:10:27 -04001745 ad.u.dentry = new_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 av = DIR__ADD_NAME | DIR__SEARCH;
1747 if (new_dentry->d_inode)
1748 av |= DIR__REMOVE_NAME;
David Howells275bb412008-11-14 10:39:19 +11001749 rc = avc_has_perm(sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 if (rc)
1751 return rc;
1752 if (new_dentry->d_inode) {
1753 new_isec = new_dentry->d_inode->i_security;
1754 new_is_dir = S_ISDIR(new_dentry->d_inode->i_mode);
David Howells275bb412008-11-14 10:39:19 +11001755 rc = avc_has_perm(sid, new_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 new_isec->sclass,
1757 (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
1758 if (rc)
1759 return rc;
1760 }
1761
1762 return 0;
1763}
1764
1765/* Check whether a task can perform a filesystem operation. */
David Howells88e67f32008-11-14 10:39:21 +11001766static int superblock_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 struct super_block *sb,
1768 u32 perms,
Thomas Liu2bf49692009-07-14 12:14:09 -04001769 struct common_audit_data *ad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 struct superblock_security_struct *sbsec;
David Howells88e67f32008-11-14 10:39:21 +11001772 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 sbsec = sb->s_security;
David Howells275bb412008-11-14 10:39:19 +11001775 return avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776}
1777
1778/* Convert a Linux mode and permission mask to an access vector. */
1779static inline u32 file_mask_to_av(int mode, int mask)
1780{
1781 u32 av = 0;
1782
Al Virodba19c62011-07-25 20:49:29 -04001783 if (!S_ISDIR(mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 if (mask & MAY_EXEC)
1785 av |= FILE__EXECUTE;
1786 if (mask & MAY_READ)
1787 av |= FILE__READ;
1788
1789 if (mask & MAY_APPEND)
1790 av |= FILE__APPEND;
1791 else if (mask & MAY_WRITE)
1792 av |= FILE__WRITE;
1793
1794 } else {
1795 if (mask & MAY_EXEC)
1796 av |= DIR__SEARCH;
1797 if (mask & MAY_WRITE)
1798 av |= DIR__WRITE;
1799 if (mask & MAY_READ)
1800 av |= DIR__READ;
1801 }
1802
1803 return av;
1804}
1805
1806/* Convert a Linux file to an access vector. */
1807static inline u32 file_to_av(struct file *file)
1808{
1809 u32 av = 0;
1810
1811 if (file->f_mode & FMODE_READ)
1812 av |= FILE__READ;
1813 if (file->f_mode & FMODE_WRITE) {
1814 if (file->f_flags & O_APPEND)
1815 av |= FILE__APPEND;
1816 else
1817 av |= FILE__WRITE;
1818 }
Stephen Smalley0794c662008-03-17 08:55:18 -04001819 if (!av) {
1820 /*
1821 * Special file opened with flags 3 for ioctl-only use.
1822 */
1823 av = FILE__IOCTL;
1824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
1826 return av;
1827}
1828
Eric Paris8b6a5a32008-10-29 17:06:46 -04001829/*
1830 * Convert a file to an access vector and include the correct open
1831 * open permission.
1832 */
1833static inline u32 open_file_to_av(struct file *file)
1834{
1835 u32 av = file_to_av(file);
1836
Eric Paris49b7b8d2010-07-23 11:44:09 -04001837 if (selinux_policycap_openperm)
1838 av |= FILE__OPEN;
1839
Eric Paris8b6a5a32008-10-29 17:06:46 -04001840 return av;
1841}
1842
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843/* Hook functions begin here. */
1844
Stephen Smalley84ab2cd2012-11-05 08:15:34 -05001845static int selinux_binder_set_context_mgr(struct task_struct *mgr)
1846{
1847 u32 mysid = current_sid();
1848 u32 mgrsid = task_sid(mgr);
1849
1850 return avc_has_perm(mysid, mgrsid, SECCLASS_BINDER, BINDER__SET_CONTEXT_MGR, NULL);
1851}
1852
1853static int selinux_binder_transaction(struct task_struct *from, struct task_struct *to)
1854{
1855 u32 mysid = current_sid();
1856 u32 fromsid = task_sid(from);
1857 u32 tosid = task_sid(to);
1858 int rc;
1859
1860 if (mysid != fromsid) {
1861 rc = avc_has_perm(mysid, fromsid, SECCLASS_BINDER, BINDER__IMPERSONATE, NULL);
1862 if (rc)
1863 return rc;
1864 }
1865
1866 return avc_has_perm(fromsid, tosid, SECCLASS_BINDER, BINDER__CALL, NULL);
1867}
1868
1869static int selinux_binder_transfer_binder(struct task_struct *from, struct task_struct *to)
1870{
1871 u32 fromsid = task_sid(from);
1872 u32 tosid = task_sid(to);
1873 return avc_has_perm(fromsid, tosid, SECCLASS_BINDER, BINDER__TRANSFER, NULL);
1874}
1875
1876static int selinux_binder_transfer_file(struct task_struct *from, struct task_struct *to, struct file *file)
1877{
1878 u32 sid = task_sid(to);
1879 struct file_security_struct *fsec = file->f_security;
1880 struct inode *inode = file->f_path.dentry->d_inode;
1881 struct inode_security_struct *isec = inode->i_security;
1882 struct common_audit_data ad;
1883 struct selinux_audit_data sad = {0,};
1884 int rc;
1885
1886 COMMON_AUDIT_DATA_INIT(&ad, PATH);
1887 ad.u.path = file->f_path;
1888 ad.selinux_audit_data = &sad;
1889
1890 if (sid != fsec->sid) {
1891 rc = avc_has_perm(sid, fsec->sid,
1892 SECCLASS_FD,
1893 FD__USE,
1894 &ad);
1895 if (rc)
1896 return rc;
1897 }
1898
1899 if (unlikely(IS_PRIVATE(inode)))
1900 return 0;
1901
1902 return avc_has_perm(sid, isec->sid, isec->sclass, file_to_av(file),
1903 &ad);
1904}
1905
Ingo Molnar9e488582009-05-07 19:26:19 +10001906static int selinux_ptrace_access_check(struct task_struct *child,
David Howells5cd9c582008-08-14 11:37:28 +01001907 unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 int rc;
1910
Ingo Molnar9e488582009-05-07 19:26:19 +10001911 rc = cap_ptrace_access_check(child, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 if (rc)
1913 return rc;
1914
Eric Paris69f594a2012-01-03 12:25:15 -05001915 if (mode & PTRACE_MODE_READ) {
David Howells275bb412008-11-14 10:39:19 +11001916 u32 sid = current_sid();
1917 u32 csid = task_sid(child);
1918 return avc_has_perm(sid, csid, SECCLASS_FILE, FILE__READ, NULL);
Stephen Smalley006ebb42008-05-19 08:32:49 -04001919 }
1920
David Howells3b11a1d2008-11-14 10:39:26 +11001921 return current_has_perm(child, PROCESS__PTRACE);
David Howells5cd9c582008-08-14 11:37:28 +01001922}
1923
1924static int selinux_ptrace_traceme(struct task_struct *parent)
1925{
1926 int rc;
1927
Eric Paris200ac532009-02-12 15:01:04 -05001928 rc = cap_ptrace_traceme(parent);
David Howells5cd9c582008-08-14 11:37:28 +01001929 if (rc)
1930 return rc;
1931
1932 return task_has_perm(parent, current, PROCESS__PTRACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933}
1934
1935static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
Eric Paris828dfe12008-04-17 13:17:49 -04001936 kernel_cap_t *inheritable, kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937{
1938 int error;
1939
David Howells3b11a1d2008-11-14 10:39:26 +11001940 error = current_has_perm(target, PROCESS__GETCAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 if (error)
1942 return error;
1943
Eric Paris200ac532009-02-12 15:01:04 -05001944 return cap_capget(target, effective, inheritable, permitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945}
1946
David Howellsd84f4f92008-11-14 10:39:23 +11001947static int selinux_capset(struct cred *new, const struct cred *old,
1948 const kernel_cap_t *effective,
1949 const kernel_cap_t *inheritable,
1950 const kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951{
1952 int error;
1953
Eric Paris200ac532009-02-12 15:01:04 -05001954 error = cap_capset(new, old,
David Howellsd84f4f92008-11-14 10:39:23 +11001955 effective, inheritable, permitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 if (error)
1957 return error;
1958
David Howellsd84f4f92008-11-14 10:39:23 +11001959 return cred_has_perm(old, new, PROCESS__SETCAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960}
1961
James Morris5626d3e2009-01-30 10:05:06 +11001962/*
1963 * (This comment used to live with the selinux_task_setuid hook,
1964 * which was removed).
1965 *
1966 * Since setuid only affects the current process, and since the SELinux
1967 * controls are not based on the Linux identity attributes, SELinux does not
1968 * need to control this operation. However, SELinux does control the use of
1969 * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
1970 */
1971
Eric Paris6a9de492012-01-03 12:25:14 -05001972static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
1973 int cap, int audit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974{
1975 int rc;
1976
Eric Paris6a9de492012-01-03 12:25:14 -05001977 rc = cap_capable(cred, ns, cap, audit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 if (rc)
1979 return rc;
1980
Eric Paris6a9de492012-01-03 12:25:14 -05001981 return cred_has_capability(cred, cap, audit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982}
1983
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
1985{
David Howells88e67f32008-11-14 10:39:21 +11001986 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 int rc = 0;
1988
1989 if (!sb)
1990 return 0;
1991
1992 switch (cmds) {
Eric Paris828dfe12008-04-17 13:17:49 -04001993 case Q_SYNC:
1994 case Q_QUOTAON:
1995 case Q_QUOTAOFF:
1996 case Q_SETINFO:
1997 case Q_SETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11001998 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04001999 break;
2000 case Q_GETFMT:
2001 case Q_GETINFO:
2002 case Q_GETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11002003 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04002004 break;
2005 default:
2006 rc = 0; /* let the kernel handle invalid cmds */
2007 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 }
2009 return rc;
2010}
2011
2012static int selinux_quota_on(struct dentry *dentry)
2013{
David Howells88e67f32008-11-14 10:39:21 +11002014 const struct cred *cred = current_cred();
2015
Eric Paris2875fa02011-04-28 16:04:24 -04002016 return dentry_has_perm(cred, dentry, FILE__QUOTAON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017}
2018
Eric Paris12b30522010-11-15 18:36:29 -05002019static int selinux_syslog(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020{
2021 int rc;
2022
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 switch (type) {
Kees Cookd78ca3c2010-02-03 15:37:13 -08002024 case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */
2025 case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */
Eric Paris828dfe12008-04-17 13:17:49 -04002026 rc = task_has_system(current, SYSTEM__SYSLOG_READ);
2027 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08002028 case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */
2029 case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */
2030 /* Set level of messages printed to console */
2031 case SYSLOG_ACTION_CONSOLE_LEVEL:
Eric Paris828dfe12008-04-17 13:17:49 -04002032 rc = task_has_system(current, SYSTEM__SYSLOG_CONSOLE);
2033 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08002034 case SYSLOG_ACTION_CLOSE: /* Close log */
2035 case SYSLOG_ACTION_OPEN: /* Open log */
2036 case SYSLOG_ACTION_READ: /* Read from log */
2037 case SYSLOG_ACTION_READ_CLEAR: /* Read/clear last kernel messages */
2038 case SYSLOG_ACTION_CLEAR: /* Clear ring buffer */
Eric Paris828dfe12008-04-17 13:17:49 -04002039 default:
2040 rc = task_has_system(current, SYSTEM__SYSLOG_MOD);
2041 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 }
2043 return rc;
2044}
2045
2046/*
2047 * Check that a process has enough memory to allocate a new virtual
2048 * mapping. 0 means there is enough memory for the allocation to
2049 * succeed and -ENOMEM implies there is not.
2050 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 * Do not audit the selinux permission check, as this is applied to all
2052 * processes that allocate mappings.
2053 */
Alan Cox34b4e4a2007-08-22 14:01:28 -07002054static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055{
2056 int rc, cap_sys_admin = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
Eric Paris6a9de492012-01-03 12:25:14 -05002058 rc = selinux_capable(current_cred(), &init_user_ns, CAP_SYS_ADMIN,
David Howells3699c532009-01-06 22:27:01 +00002059 SECURITY_CAP_NOAUDIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 if (rc == 0)
2061 cap_sys_admin = 1;
2062
Alan Cox34b4e4a2007-08-22 14:01:28 -07002063 return __vm_enough_memory(mm, pages, cap_sys_admin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064}
2065
2066/* binprm security operations */
2067
David Howellsa6f76f22008-11-14 10:39:24 +11002068static int selinux_bprm_set_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069{
David Howellsa6f76f22008-11-14 10:39:24 +11002070 const struct task_security_struct *old_tsec;
2071 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 struct inode_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04002073 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07002074 struct selinux_audit_data sad = {0,};
David Howellsa6f76f22008-11-14 10:39:24 +11002075 struct inode *inode = bprm->file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 int rc;
2077
Eric Paris200ac532009-02-12 15:01:04 -05002078 rc = cap_bprm_set_creds(bprm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 if (rc)
2080 return rc;
2081
David Howellsa6f76f22008-11-14 10:39:24 +11002082 /* SELinux context only depends on initial program or script and not
2083 * the script interpreter */
2084 if (bprm->cred_prepared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 return 0;
2086
David Howellsa6f76f22008-11-14 10:39:24 +11002087 old_tsec = current_security();
2088 new_tsec = bprm->cred->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 isec = inode->i_security;
2090
2091 /* Default to the current task SID. */
David Howellsa6f76f22008-11-14 10:39:24 +11002092 new_tsec->sid = old_tsec->sid;
2093 new_tsec->osid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
Michael LeMay28eba5b2006-06-27 02:53:42 -07002095 /* Reset fs, key, and sock SIDs on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002096 new_tsec->create_sid = 0;
2097 new_tsec->keycreate_sid = 0;
2098 new_tsec->sockcreate_sid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
David Howellsa6f76f22008-11-14 10:39:24 +11002100 if (old_tsec->exec_sid) {
2101 new_tsec->sid = old_tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 /* Reset exec SID on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002103 new_tsec->exec_sid = 0;
Andy Lutomirski9bf75df2012-04-12 16:47:50 -05002104
2105 /*
2106 * Minimize confusion: if no_new_privs and a transition is
2107 * explicitly requested, then fail the exec.
2108 */
2109 if (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS)
2110 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 } else {
2112 /* Check for a default transition on this program. */
David Howellsa6f76f22008-11-14 10:39:24 +11002113 rc = security_transition_sid(old_tsec->sid, isec->sid,
Eric Paris652bb9b2011-02-01 11:05:40 -05002114 SECCLASS_PROCESS, NULL,
2115 &new_tsec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 if (rc)
2117 return rc;
2118 }
2119
Eric Parisf48b7392011-04-25 12:54:27 -04002120 COMMON_AUDIT_DATA_INIT(&ad, PATH);
Eric Paris3b3b0e42012-04-03 09:37:02 -07002121 ad.selinux_audit_data = &sad;
Eric Parisf48b7392011-04-25 12:54:27 -04002122 ad.u.path = bprm->file->f_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
Andy Lutomirski9bf75df2012-04-12 16:47:50 -05002124 if ((bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID) ||
2125 (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS))
David Howellsa6f76f22008-11-14 10:39:24 +11002126 new_tsec->sid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
David Howellsa6f76f22008-11-14 10:39:24 +11002128 if (new_tsec->sid == old_tsec->sid) {
2129 rc = avc_has_perm(old_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
2131 if (rc)
2132 return rc;
2133 } else {
2134 /* Check permissions for the transition. */
David Howellsa6f76f22008-11-14 10:39:24 +11002135 rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
2137 if (rc)
2138 return rc;
2139
David Howellsa6f76f22008-11-14 10:39:24 +11002140 rc = avc_has_perm(new_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
2142 if (rc)
2143 return rc;
2144
David Howellsa6f76f22008-11-14 10:39:24 +11002145 /* Check for shared state */
2146 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
2147 rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
2148 SECCLASS_PROCESS, PROCESS__SHARE,
2149 NULL);
2150 if (rc)
2151 return -EPERM;
2152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
David Howellsa6f76f22008-11-14 10:39:24 +11002154 /* Make sure that anyone attempting to ptrace over a task that
2155 * changes its SID has the appropriate permit */
2156 if (bprm->unsafe &
2157 (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
2158 struct task_struct *tracer;
2159 struct task_security_struct *sec;
2160 u32 ptsid = 0;
2161
2162 rcu_read_lock();
Tejun Heo06d98472011-06-17 16:50:40 +02002163 tracer = ptrace_parent(current);
David Howellsa6f76f22008-11-14 10:39:24 +11002164 if (likely(tracer != NULL)) {
2165 sec = __task_cred(tracer)->security;
2166 ptsid = sec->sid;
2167 }
2168 rcu_read_unlock();
2169
2170 if (ptsid != 0) {
2171 rc = avc_has_perm(ptsid, new_tsec->sid,
2172 SECCLASS_PROCESS,
2173 PROCESS__PTRACE, NULL);
2174 if (rc)
2175 return -EPERM;
2176 }
2177 }
2178
2179 /* Clear any possibly unsafe personality bits on exec: */
2180 bprm->per_clear |= PER_CLEAR_ON_SETID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 }
2182
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 return 0;
2184}
2185
Eric Paris828dfe12008-04-17 13:17:49 -04002186static int selinux_bprm_secureexec(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187{
Paul Moore5fb49872010-04-22 14:46:19 -04002188 const struct task_security_struct *tsec = current_security();
David Howells275bb412008-11-14 10:39:19 +11002189 u32 sid, osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 int atsecure = 0;
2191
David Howells275bb412008-11-14 10:39:19 +11002192 sid = tsec->sid;
2193 osid = tsec->osid;
2194
2195 if (osid != sid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 /* Enable secure mode for SIDs transitions unless
2197 the noatsecure permission is granted between
2198 the two SIDs, i.e. ahp returns 0. */
David Howells275bb412008-11-14 10:39:19 +11002199 atsecure = avc_has_perm(osid, sid,
David Howellsa6f76f22008-11-14 10:39:24 +11002200 SECCLASS_PROCESS,
2201 PROCESS__NOATSECURE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 }
2203
Eric Paris200ac532009-02-12 15:01:04 -05002204 return (atsecure || cap_bprm_secureexec(bprm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205}
2206
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207/* Derived from fs/exec.c:flush_old_files. */
David Howells745ca242008-11-14 10:39:22 +11002208static inline void flush_unauthorized_files(const struct cred *cred,
2209 struct files_struct *files)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210{
Thomas Liu2bf49692009-07-14 12:14:09 -04002211 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07002212 struct selinux_audit_data sad = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 struct file *file, *devnull = NULL;
Stephen Smalleyb20c8122006-09-25 23:32:03 -07002214 struct tty_struct *tty;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07002215 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 long j = -1;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002217 int drop_tty = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002219 tty = get_current_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 if (tty) {
Nick Pigginee2ffa02010-08-18 04:37:35 +10002221 spin_lock(&tty_files_lock);
Eric Paris37dd0bd2008-10-31 17:40:00 -04002222 if (!list_empty(&tty->tty_files)) {
Nick Piggind996b622010-08-18 04:37:36 +10002223 struct tty_file_private *file_priv;
Eric Paris37dd0bd2008-10-31 17:40:00 -04002224 struct inode *inode;
2225
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 /* Revalidate access to controlling tty.
2227 Use inode_has_perm on the tty inode directly rather
2228 than using file_has_perm, as this particular open
2229 file may belong to another process and we are only
2230 interested in the inode-based check here. */
Nick Piggind996b622010-08-18 04:37:36 +10002231 file_priv = list_first_entry(&tty->tty_files,
2232 struct tty_file_private, list);
2233 file = file_priv->file;
Eric Paris37dd0bd2008-10-31 17:40:00 -04002234 inode = file->f_path.dentry->d_inode;
Linus Torvalds95f4efb2011-06-08 15:11:56 -07002235 if (inode_has_perm_noadp(cred, inode,
2236 FILE__READ | FILE__WRITE, 0)) {
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002237 drop_tty = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 }
2239 }
Nick Pigginee2ffa02010-08-18 04:37:35 +10002240 spin_unlock(&tty_files_lock);
Alan Cox452a00d2008-10-13 10:39:13 +01002241 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 }
Eric W. Biederman98a27ba2007-05-08 00:26:56 -07002243 /* Reset controlling tty. */
2244 if (drop_tty)
2245 no_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246
2247 /* Revalidate access to inherited open files. */
2248
Eric Parisf48b7392011-04-25 12:54:27 -04002249 COMMON_AUDIT_DATA_INIT(&ad, INODE);
Eric Paris3b3b0e42012-04-03 09:37:02 -07002250 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
2252 spin_lock(&files->file_lock);
2253 for (;;) {
2254 unsigned long set, i;
2255 int fd;
2256
2257 j++;
Josh Boyer27cd8f52012-07-25 10:40:34 -04002258 i = j * BITS_PER_LONG;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07002259 fdt = files_fdtable(files);
Vadim Lobanovbbea9f62006-12-10 02:21:12 -08002260 if (i >= fdt->max_fds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 break;
David Howells1fd36ad2012-02-16 17:49:54 +00002262 set = fdt->open_fds[j];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 if (!set)
2264 continue;
2265 spin_unlock(&files->file_lock);
Eric Paris828dfe12008-04-17 13:17:49 -04002266 for ( ; set ; i++, set >>= 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 if (set & 1) {
2268 file = fget(i);
2269 if (!file)
2270 continue;
David Howells88e67f32008-11-14 10:39:21 +11002271 if (file_has_perm(cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 file,
2273 file_to_av(file))) {
2274 sys_close(i);
2275 fd = get_unused_fd();
2276 if (fd != i) {
2277 if (fd >= 0)
2278 put_unused_fd(fd);
2279 fput(file);
2280 continue;
2281 }
2282 if (devnull) {
Nick Piggin095975d2006-01-08 01:02:19 -08002283 get_file(devnull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 } else {
David Howells745ca242008-11-14 10:39:22 +11002285 devnull = dentry_open(
2286 dget(selinux_null),
2287 mntget(selinuxfs_mount),
2288 O_RDWR, cred);
Akinobu Mitafc5d81e2006-11-27 15:16:48 +09002289 if (IS_ERR(devnull)) {
2290 devnull = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 put_unused_fd(fd);
2292 fput(file);
2293 continue;
2294 }
2295 }
2296 fd_install(fd, devnull);
2297 }
2298 fput(file);
2299 }
2300 }
2301 spin_lock(&files->file_lock);
2302
2303 }
2304 spin_unlock(&files->file_lock);
2305}
2306
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307/*
David Howellsa6f76f22008-11-14 10:39:24 +11002308 * Prepare a process for imminent new credential changes due to exec
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 */
David Howellsa6f76f22008-11-14 10:39:24 +11002310static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311{
David Howellsa6f76f22008-11-14 10:39:24 +11002312 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 struct rlimit *rlim, *initrlim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 int rc, i;
2315
David Howellsa6f76f22008-11-14 10:39:24 +11002316 new_tsec = bprm->cred->security;
2317 if (new_tsec->sid == new_tsec->osid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 return;
2319
2320 /* Close files for which the new task SID is not authorized. */
David Howellsa6f76f22008-11-14 10:39:24 +11002321 flush_unauthorized_files(bprm->cred, current->files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
David Howellsa6f76f22008-11-14 10:39:24 +11002323 /* Always clear parent death signal on SID transitions. */
2324 current->pdeath_signal = 0;
2325
2326 /* Check whether the new SID can inherit resource limits from the old
2327 * SID. If not, reset all soft limits to the lower of the current
2328 * task's hard limit and the init task's soft limit.
2329 *
2330 * Note that the setting of hard limits (even to lower them) can be
2331 * controlled by the setrlimit check. The inclusion of the init task's
2332 * soft limit into the computation is to avoid resetting soft limits
2333 * higher than the default soft limit for cases where the default is
2334 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2335 */
2336 rc = avc_has_perm(new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
2337 PROCESS__RLIMITINH, NULL);
2338 if (rc) {
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002339 /* protect against do_prlimit() */
2340 task_lock(current);
David Howellsa6f76f22008-11-14 10:39:24 +11002341 for (i = 0; i < RLIM_NLIMITS; i++) {
2342 rlim = current->signal->rlim + i;
2343 initrlim = init_task.signal->rlim + i;
2344 rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
2345 }
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002346 task_unlock(current);
2347 update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
David Howellsa6f76f22008-11-14 10:39:24 +11002348 }
2349}
2350
2351/*
2352 * Clean up the process immediately after the installation of new credentials
2353 * due to exec
2354 */
2355static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
2356{
2357 const struct task_security_struct *tsec = current_security();
2358 struct itimerval itimer;
David Howellsa6f76f22008-11-14 10:39:24 +11002359 u32 osid, sid;
2360 int rc, i;
David Howellsa6f76f22008-11-14 10:39:24 +11002361
David Howellsa6f76f22008-11-14 10:39:24 +11002362 osid = tsec->osid;
2363 sid = tsec->sid;
2364
2365 if (sid == osid)
2366 return;
2367
2368 /* Check whether the new SID can inherit signal state from the old SID.
2369 * If not, clear itimers to avoid subsequent signal generation and
2370 * flush and unblock signals.
2371 *
2372 * This must occur _after_ the task SID has been updated so that any
2373 * kill done after the flush will be checked against the new SID.
2374 */
2375 rc = avc_has_perm(osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 if (rc) {
2377 memset(&itimer, 0, sizeof itimer);
2378 for (i = 0; i < 3; i++)
2379 do_setitimer(i, &itimer, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 spin_lock_irq(&current->sighand->siglock);
David Howells3bcac022009-04-29 13:45:05 +01002381 if (!(current->signal->flags & SIGNAL_GROUP_EXIT)) {
2382 __flush_signals(current);
2383 flush_signal_handlers(current, 1);
2384 sigemptyset(&current->blocked);
2385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 spin_unlock_irq(&current->sighand->siglock);
2387 }
2388
David Howellsa6f76f22008-11-14 10:39:24 +11002389 /* Wake up the parent if it is waiting so that it can recheck
2390 * wait permission to the new task SID. */
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002391 read_lock(&tasklist_lock);
Oleg Nesterov0b7570e2009-09-23 15:56:46 -07002392 __wake_up_parent(current, current->real_parent);
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002393 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394}
2395
2396/* superblock security operations */
2397
2398static int selinux_sb_alloc_security(struct super_block *sb)
2399{
2400 return superblock_alloc_security(sb);
2401}
2402
2403static void selinux_sb_free_security(struct super_block *sb)
2404{
2405 superblock_free_security(sb);
2406}
2407
2408static inline int match_prefix(char *prefix, int plen, char *option, int olen)
2409{
2410 if (plen > olen)
2411 return 0;
2412
2413 return !memcmp(prefix, option, plen);
2414}
2415
2416static inline int selinux_option(char *option, int len)
2417{
Eric Paris832cbd92008-04-01 13:24:09 -04002418 return (match_prefix(CONTEXT_STR, sizeof(CONTEXT_STR)-1, option, len) ||
2419 match_prefix(FSCONTEXT_STR, sizeof(FSCONTEXT_STR)-1, option, len) ||
2420 match_prefix(DEFCONTEXT_STR, sizeof(DEFCONTEXT_STR)-1, option, len) ||
David P. Quigley11689d42009-01-16 09:22:03 -05002421 match_prefix(ROOTCONTEXT_STR, sizeof(ROOTCONTEXT_STR)-1, option, len) ||
2422 match_prefix(LABELSUPP_STR, sizeof(LABELSUPP_STR)-1, option, len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423}
2424
2425static inline void take_option(char **to, char *from, int *first, int len)
2426{
2427 if (!*first) {
2428 **to = ',';
2429 *to += 1;
Cory Olmo3528a952006-09-29 01:58:44 -07002430 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 *first = 0;
2432 memcpy(*to, from, len);
2433 *to += len;
2434}
2435
Eric Paris828dfe12008-04-17 13:17:49 -04002436static inline void take_selinux_option(char **to, char *from, int *first,
2437 int len)
Cory Olmo3528a952006-09-29 01:58:44 -07002438{
2439 int current_size = 0;
2440
2441 if (!*first) {
2442 **to = '|';
2443 *to += 1;
Eric Paris828dfe12008-04-17 13:17:49 -04002444 } else
Cory Olmo3528a952006-09-29 01:58:44 -07002445 *first = 0;
2446
2447 while (current_size < len) {
2448 if (*from != '"') {
2449 **to = *from;
2450 *to += 1;
2451 }
2452 from += 1;
2453 current_size += 1;
2454 }
2455}
2456
Eric Parise0007522008-03-05 10:31:54 -05002457static int selinux_sb_copy_data(char *orig, char *copy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458{
2459 int fnosec, fsec, rc = 0;
2460 char *in_save, *in_curr, *in_end;
2461 char *sec_curr, *nosec_save, *nosec;
Cory Olmo3528a952006-09-29 01:58:44 -07002462 int open_quote = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463
2464 in_curr = orig;
2465 sec_curr = copy;
2466
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 nosec = (char *)get_zeroed_page(GFP_KERNEL);
2468 if (!nosec) {
2469 rc = -ENOMEM;
2470 goto out;
2471 }
2472
2473 nosec_save = nosec;
2474 fnosec = fsec = 1;
2475 in_save = in_end = orig;
2476
2477 do {
Cory Olmo3528a952006-09-29 01:58:44 -07002478 if (*in_end == '"')
2479 open_quote = !open_quote;
2480 if ((*in_end == ',' && open_quote == 0) ||
2481 *in_end == '\0') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 int len = in_end - in_curr;
2483
2484 if (selinux_option(in_curr, len))
Cory Olmo3528a952006-09-29 01:58:44 -07002485 take_selinux_option(&sec_curr, in_curr, &fsec, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 else
2487 take_option(&nosec, in_curr, &fnosec, len);
2488
2489 in_curr = in_end + 1;
2490 }
2491 } while (*in_end++);
2492
Eric Paris6931dfc2005-06-30 02:58:51 -07002493 strcpy(in_save, nosec_save);
Gerald Schaeferda3caa22005-06-21 17:15:18 -07002494 free_page((unsigned long)nosec_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495out:
2496 return rc;
2497}
2498
Eric Paris026eb162011-03-03 16:09:14 -05002499static int selinux_sb_remount(struct super_block *sb, void *data)
2500{
2501 int rc, i, *flags;
2502 struct security_mnt_opts opts;
2503 char *secdata, **mount_options;
2504 struct superblock_security_struct *sbsec = sb->s_security;
2505
2506 if (!(sbsec->flags & SE_SBINITIALIZED))
2507 return 0;
2508
2509 if (!data)
2510 return 0;
2511
2512 if (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
2513 return 0;
2514
2515 security_init_mnt_opts(&opts);
2516 secdata = alloc_secdata();
2517 if (!secdata)
2518 return -ENOMEM;
2519 rc = selinux_sb_copy_data(data, secdata);
2520 if (rc)
2521 goto out_free_secdata;
2522
2523 rc = selinux_parse_opts_str(secdata, &opts);
2524 if (rc)
2525 goto out_free_secdata;
2526
2527 mount_options = opts.mnt_opts;
2528 flags = opts.mnt_opts_flags;
2529
2530 for (i = 0; i < opts.num_mnt_opts; i++) {
2531 u32 sid;
2532 size_t len;
2533
2534 if (flags[i] == SE_SBLABELSUPP)
2535 continue;
2536 len = strlen(mount_options[i]);
2537 rc = security_context_to_sid(mount_options[i], len, &sid);
2538 if (rc) {
2539 printk(KERN_WARNING "SELinux: security_context_to_sid"
2540 "(%s) failed for (dev %s, type %s) errno=%d\n",
2541 mount_options[i], sb->s_id, sb->s_type->name, rc);
2542 goto out_free_opts;
2543 }
2544 rc = -EINVAL;
2545 switch (flags[i]) {
2546 case FSCONTEXT_MNT:
2547 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
2548 goto out_bad_option;
2549 break;
2550 case CONTEXT_MNT:
2551 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
2552 goto out_bad_option;
2553 break;
2554 case ROOTCONTEXT_MNT: {
2555 struct inode_security_struct *root_isec;
2556 root_isec = sb->s_root->d_inode->i_security;
2557
2558 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
2559 goto out_bad_option;
2560 break;
2561 }
2562 case DEFCONTEXT_MNT:
2563 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
2564 goto out_bad_option;
2565 break;
2566 default:
2567 goto out_free_opts;
2568 }
2569 }
2570
2571 rc = 0;
2572out_free_opts:
2573 security_free_mnt_opts(&opts);
2574out_free_secdata:
2575 free_secdata(secdata);
2576 return rc;
2577out_bad_option:
2578 printk(KERN_WARNING "SELinux: unable to change security options "
2579 "during remount (dev %s, type=%s)\n", sb->s_id,
2580 sb->s_type->name);
2581 goto out_free_opts;
2582}
2583
James Morris12204e22008-12-19 10:44:42 +11002584static int selinux_sb_kern_mount(struct super_block *sb, int flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585{
David Howells88e67f32008-11-14 10:39:21 +11002586 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002587 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07002588 struct selinux_audit_data sad = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 int rc;
2590
2591 rc = superblock_doinit(sb, data);
2592 if (rc)
2593 return rc;
2594
James Morris74192242008-12-19 11:41:10 +11002595 /* Allow all mounts performed by the kernel */
2596 if (flags & MS_KERNMOUNT)
2597 return 0;
2598
Eric Parisa2694342011-04-25 13:10:27 -04002599 COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
Eric Paris3b3b0e42012-04-03 09:37:02 -07002600 ad.selinux_audit_data = &sad;
Eric Parisa2694342011-04-25 13:10:27 -04002601 ad.u.dentry = sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002602 return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603}
2604
David Howells726c3342006-06-23 02:02:58 -07002605static int selinux_sb_statfs(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606{
David Howells88e67f32008-11-14 10:39:21 +11002607 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002608 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07002609 struct selinux_audit_data sad = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610
Eric Parisa2694342011-04-25 13:10:27 -04002611 COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
Eric Paris3b3b0e42012-04-03 09:37:02 -07002612 ad.selinux_audit_data = &sad;
Eric Parisa2694342011-04-25 13:10:27 -04002613 ad.u.dentry = dentry->d_sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002614 return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615}
2616
Eric Paris828dfe12008-04-17 13:17:49 -04002617static int selinux_mount(char *dev_name,
Al Virob5266eb2008-03-22 17:48:24 -04002618 struct path *path,
Eric Paris828dfe12008-04-17 13:17:49 -04002619 char *type,
2620 unsigned long flags,
2621 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622{
David Howells88e67f32008-11-14 10:39:21 +11002623 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624
2625 if (flags & MS_REMOUNT)
Al Virod8c95842011-12-07 18:16:57 -05002626 return superblock_has_perm(cred, path->dentry->d_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002627 FILESYSTEM__REMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 else
Eric Paris2875fa02011-04-28 16:04:24 -04002629 return path_has_perm(cred, path, FILE__MOUNTON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630}
2631
2632static int selinux_umount(struct vfsmount *mnt, int flags)
2633{
David Howells88e67f32008-11-14 10:39:21 +11002634 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
David Howells88e67f32008-11-14 10:39:21 +11002636 return superblock_has_perm(cred, mnt->mnt_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002637 FILESYSTEM__UNMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638}
2639
2640/* inode security operations */
2641
2642static int selinux_inode_alloc_security(struct inode *inode)
2643{
2644 return inode_alloc_security(inode);
2645}
2646
2647static void selinux_inode_free_security(struct inode *inode)
2648{
2649 inode_free_security(inode);
2650}
2651
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002652static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
Eric Paris2a7dba32011-02-01 11:05:39 -05002653 const struct qstr *qstr, char **name,
2654 void **value, size_t *len)
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002655{
Paul Moore5fb49872010-04-22 14:46:19 -04002656 const struct task_security_struct *tsec = current_security();
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002657 struct inode_security_struct *dsec;
2658 struct superblock_security_struct *sbsec;
David Howells275bb412008-11-14 10:39:19 +11002659 u32 sid, newsid, clen;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002660 int rc;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002661 char *namep = NULL, *context;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002662
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002663 dsec = dir->i_security;
2664 sbsec = dir->i_sb->s_security;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002665
David Howells275bb412008-11-14 10:39:19 +11002666 sid = tsec->sid;
2667 newsid = tsec->create_sid;
2668
Eric Paris415103f2010-12-02 16:13:40 -05002669 if ((sbsec->flags & SE_SBINITIALIZED) &&
2670 (sbsec->behavior == SECURITY_FS_USE_MNTPOINT))
2671 newsid = sbsec->mntpoint_sid;
2672 else if (!newsid || !(sbsec->flags & SE_SBLABELSUPP)) {
David Howells275bb412008-11-14 10:39:19 +11002673 rc = security_transition_sid(sid, dsec->sid,
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002674 inode_mode_to_security_class(inode->i_mode),
Eric Paris652bb9b2011-02-01 11:05:40 -05002675 qstr, &newsid);
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002676 if (rc) {
2677 printk(KERN_WARNING "%s: "
2678 "security_transition_sid failed, rc=%d (dev=%s "
2679 "ino=%ld)\n",
Harvey Harrisondd6f9532008-03-06 10:03:59 +11002680 __func__,
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002681 -rc, inode->i_sb->s_id, inode->i_ino);
2682 return rc;
2683 }
2684 }
2685
Eric Paris296fddf2006-09-25 23:32:00 -07002686 /* Possibly defer initialization to selinux_complete_init. */
David P. Quigley0d90a7e2009-01-16 09:22:02 -05002687 if (sbsec->flags & SE_SBINITIALIZED) {
Eric Paris296fddf2006-09-25 23:32:00 -07002688 struct inode_security_struct *isec = inode->i_security;
2689 isec->sclass = inode_mode_to_security_class(inode->i_mode);
2690 isec->sid = newsid;
2691 isec->initialized = 1;
2692 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002693
David P. Quigleycd895962009-01-16 09:22:04 -05002694 if (!ss_initialized || !(sbsec->flags & SE_SBLABELSUPP))
Stephen Smalley25a74f32005-11-08 21:34:33 -08002695 return -EOPNOTSUPP;
2696
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002697 if (name) {
Josef Bacika02fe132008-04-04 09:35:05 +11002698 namep = kstrdup(XATTR_SELINUX_SUFFIX, GFP_NOFS);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002699 if (!namep)
2700 return -ENOMEM;
2701 *name = namep;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002702 }
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002703
2704 if (value && len) {
Stephen Smalley12b29f32008-05-07 13:03:20 -04002705 rc = security_sid_to_context_force(newsid, &context, &clen);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002706 if (rc) {
2707 kfree(namep);
2708 return rc;
2709 }
2710 *value = context;
2711 *len = clen;
2712 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002713
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002714 return 0;
2715}
2716
Al Viro4acdaf22011-07-26 01:42:34 -04002717static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718{
2719 return may_create(dir, dentry, SECCLASS_FILE);
2720}
2721
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2723{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 return may_link(dir, old_dentry, MAY_LINK);
2725}
2726
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
2728{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 return may_link(dir, dentry, MAY_UNLINK);
2730}
2731
2732static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
2733{
2734 return may_create(dir, dentry, SECCLASS_LNK_FILE);
2735}
2736
Al Viro18bb1db2011-07-26 01:41:39 -04002737static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738{
2739 return may_create(dir, dentry, SECCLASS_DIR);
2740}
2741
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
2743{
2744 return may_link(dir, dentry, MAY_RMDIR);
2745}
2746
Al Viro1a67aaf2011-07-26 01:52:52 -04002747static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 return may_create(dir, dentry, inode_mode_to_security_class(mode));
2750}
2751
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
Eric Paris828dfe12008-04-17 13:17:49 -04002753 struct inode *new_inode, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754{
2755 return may_rename(old_inode, old_dentry, new_inode, new_dentry);
2756}
2757
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758static int selinux_inode_readlink(struct dentry *dentry)
2759{
David Howells88e67f32008-11-14 10:39:21 +11002760 const struct cred *cred = current_cred();
2761
Eric Paris2875fa02011-04-28 16:04:24 -04002762 return dentry_has_perm(cred, dentry, FILE__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763}
2764
2765static int selinux_inode_follow_link(struct dentry *dentry, struct nameidata *nameidata)
2766{
David Howells88e67f32008-11-14 10:39:21 +11002767 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768
Eric Paris2875fa02011-04-28 16:04:24 -04002769 return dentry_has_perm(cred, dentry, FILE__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770}
2771
Al Viroe74f71e2011-06-20 19:38:15 -04002772static int selinux_inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773{
David Howells88e67f32008-11-14 10:39:21 +11002774 const struct cred *cred = current_cred();
Eric Parisb782e0a2010-07-23 11:44:03 -04002775 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07002776 struct selinux_audit_data sad = {0,};
Eric Parisb782e0a2010-07-23 11:44:03 -04002777 u32 perms;
2778 bool from_access;
Al Virocf1dd1d2011-06-20 19:44:08 -04002779 unsigned flags = mask & MAY_NOT_BLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780
Eric Parisb782e0a2010-07-23 11:44:03 -04002781 from_access = mask & MAY_ACCESS;
Eric Parisd09ca732010-07-23 11:43:57 -04002782 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
2783
Eric Parisb782e0a2010-07-23 11:44:03 -04002784 /* No permission to check. Existence test. */
2785 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787
Eric Parisf48b7392011-04-25 12:54:27 -04002788 COMMON_AUDIT_DATA_INIT(&ad, INODE);
Eric Paris3b3b0e42012-04-03 09:37:02 -07002789 ad.selinux_audit_data = &sad;
Eric Parisf48b7392011-04-25 12:54:27 -04002790 ad.u.inode = inode;
Eric Parisb782e0a2010-07-23 11:44:03 -04002791
2792 if (from_access)
Eric Paris3b3b0e42012-04-03 09:37:02 -07002793 ad.selinux_audit_data->auditdeny |= FILE__AUDIT_ACCESS;
Eric Parisb782e0a2010-07-23 11:44:03 -04002794
2795 perms = file_mask_to_av(inode->i_mode, mask);
2796
Eric Paris9ade0cf2011-04-25 16:26:29 -04002797 return inode_has_perm(cred, inode, perms, &ad, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798}
2799
2800static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
2801{
David Howells88e67f32008-11-14 10:39:21 +11002802 const struct cred *cred = current_cred();
Amerigo Wangbc6a6002009-08-20 19:29:02 -07002803 unsigned int ia_valid = iattr->ia_valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804
Amerigo Wangbc6a6002009-08-20 19:29:02 -07002805 /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
2806 if (ia_valid & ATTR_FORCE) {
2807 ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
2808 ATTR_FORCE);
2809 if (!ia_valid)
2810 return 0;
2811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812
Amerigo Wangbc6a6002009-08-20 19:29:02 -07002813 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
2814 ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
Eric Paris2875fa02011-04-28 16:04:24 -04002815 return dentry_has_perm(cred, dentry, FILE__SETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816
Eric Paris2875fa02011-04-28 16:04:24 -04002817 return dentry_has_perm(cred, dentry, FILE__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818}
2819
2820static int selinux_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
2821{
David Howells88e67f32008-11-14 10:39:21 +11002822 const struct cred *cred = current_cred();
Eric Paris2875fa02011-04-28 16:04:24 -04002823 struct path path;
David Howells88e67f32008-11-14 10:39:21 +11002824
Eric Paris2875fa02011-04-28 16:04:24 -04002825 path.dentry = dentry;
2826 path.mnt = mnt;
2827
2828 return path_has_perm(cred, &path, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829}
2830
David Howells8f0cfa52008-04-29 00:59:41 -07002831static int selinux_inode_setotherxattr(struct dentry *dentry, const char *name)
Serge E. Hallynb5376772007-10-16 23:31:36 -07002832{
David Howells88e67f32008-11-14 10:39:21 +11002833 const struct cred *cred = current_cred();
2834
Serge E. Hallynb5376772007-10-16 23:31:36 -07002835 if (!strncmp(name, XATTR_SECURITY_PREFIX,
2836 sizeof XATTR_SECURITY_PREFIX - 1)) {
2837 if (!strcmp(name, XATTR_NAME_CAPS)) {
2838 if (!capable(CAP_SETFCAP))
2839 return -EPERM;
2840 } else if (!capable(CAP_SYS_ADMIN)) {
2841 /* A different attribute in the security namespace.
2842 Restrict to administrator. */
2843 return -EPERM;
2844 }
2845 }
2846
2847 /* Not an attribute we recognize, so just check the
2848 ordinary setattr permission. */
Eric Paris2875fa02011-04-28 16:04:24 -04002849 return dentry_has_perm(cred, dentry, FILE__SETATTR);
Serge E. Hallynb5376772007-10-16 23:31:36 -07002850}
2851
David Howells8f0cfa52008-04-29 00:59:41 -07002852static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
2853 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 struct inode *inode = dentry->d_inode;
2856 struct inode_security_struct *isec = inode->i_security;
2857 struct superblock_security_struct *sbsec;
Thomas Liu2bf49692009-07-14 12:14:09 -04002858 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07002859 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11002860 u32 newsid, sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 int rc = 0;
2862
Serge E. Hallynb5376772007-10-16 23:31:36 -07002863 if (strcmp(name, XATTR_NAME_SELINUX))
2864 return selinux_inode_setotherxattr(dentry, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865
2866 sbsec = inode->i_sb->s_security;
David P. Quigleycd895962009-01-16 09:22:04 -05002867 if (!(sbsec->flags & SE_SBLABELSUPP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 return -EOPNOTSUPP;
2869
Serge E. Hallyn2e149672011-03-23 16:43:26 -07002870 if (!inode_owner_or_capable(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 return -EPERM;
2872
Eric Parisa2694342011-04-25 13:10:27 -04002873 COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
Eric Paris3b3b0e42012-04-03 09:37:02 -07002874 ad.selinux_audit_data = &sad;
Eric Parisa2694342011-04-25 13:10:27 -04002875 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876
David Howells275bb412008-11-14 10:39:19 +11002877 rc = avc_has_perm(sid, isec->sid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 FILE__RELABELFROM, &ad);
2879 if (rc)
2880 return rc;
2881
2882 rc = security_context_to_sid(value, size, &newsid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04002883 if (rc == -EINVAL) {
2884 if (!capable(CAP_MAC_ADMIN))
2885 return rc;
2886 rc = security_context_to_sid_force(value, size, &newsid);
2887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 if (rc)
2889 return rc;
2890
David Howells275bb412008-11-14 10:39:19 +11002891 rc = avc_has_perm(sid, newsid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 FILE__RELABELTO, &ad);
2893 if (rc)
2894 return rc;
2895
David Howells275bb412008-11-14 10:39:19 +11002896 rc = security_validate_transition(isec->sid, newsid, sid,
Eric Paris828dfe12008-04-17 13:17:49 -04002897 isec->sclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 if (rc)
2899 return rc;
2900
2901 return avc_has_perm(newsid,
2902 sbsec->sid,
2903 SECCLASS_FILESYSTEM,
2904 FILESYSTEM__ASSOCIATE,
2905 &ad);
2906}
2907
David Howells8f0cfa52008-04-29 00:59:41 -07002908static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
Eric Parisf5269712008-05-14 11:27:45 -04002909 const void *value, size_t size,
David Howells8f0cfa52008-04-29 00:59:41 -07002910 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911{
2912 struct inode *inode = dentry->d_inode;
2913 struct inode_security_struct *isec = inode->i_security;
2914 u32 newsid;
2915 int rc;
2916
2917 if (strcmp(name, XATTR_NAME_SELINUX)) {
2918 /* Not an attribute we recognize, so nothing to do. */
2919 return;
2920 }
2921
Stephen Smalley12b29f32008-05-07 13:03:20 -04002922 rc = security_context_to_sid_force(value, size, &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 if (rc) {
Stephen Smalley12b29f32008-05-07 13:03:20 -04002924 printk(KERN_ERR "SELinux: unable to map context to SID"
2925 "for (%s, %lu), rc=%d\n",
2926 inode->i_sb->s_id, inode->i_ino, -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 return;
2928 }
2929
2930 isec->sid = newsid;
2931 return;
2932}
2933
David Howells8f0cfa52008-04-29 00:59:41 -07002934static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935{
David Howells88e67f32008-11-14 10:39:21 +11002936 const struct cred *cred = current_cred();
2937
Eric Paris2875fa02011-04-28 16:04:24 -04002938 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939}
2940
Eric Paris828dfe12008-04-17 13:17:49 -04002941static int selinux_inode_listxattr(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942{
David Howells88e67f32008-11-14 10:39:21 +11002943 const struct cred *cred = current_cred();
2944
Eric Paris2875fa02011-04-28 16:04:24 -04002945 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946}
2947
David Howells8f0cfa52008-04-29 00:59:41 -07002948static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949{
Serge E. Hallynb5376772007-10-16 23:31:36 -07002950 if (strcmp(name, XATTR_NAME_SELINUX))
2951 return selinux_inode_setotherxattr(dentry, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952
2953 /* No one is allowed to remove a SELinux security label.
2954 You can change the label, but all data must be labeled. */
2955 return -EACCES;
2956}
2957
James Morrisd381d8a2005-10-30 14:59:22 -08002958/*
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04002959 * Copy the inode security context value to the user.
James Morrisd381d8a2005-10-30 14:59:22 -08002960 *
2961 * Permission check is handled by selinux_inode_getxattr hook.
2962 */
David P. Quigley42492592008-02-04 22:29:39 -08002963static int selinux_inode_getsecurity(const struct inode *inode, const char *name, void **buffer, bool alloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964{
David P. Quigley42492592008-02-04 22:29:39 -08002965 u32 size;
2966 int error;
2967 char *context = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 struct inode_security_struct *isec = inode->i_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00002970 if (strcmp(name, XATTR_SELINUX_SUFFIX))
2971 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04002973 /*
2974 * If the caller has CAP_MAC_ADMIN, then get the raw context
2975 * value even if it is not defined by current policy; otherwise,
2976 * use the in-core value under current policy.
2977 * Use the non-auditing forms of the permission checks since
2978 * getxattr may be called by unprivileged processes commonly
2979 * and lack of permission just means that we fall back to the
2980 * in-core context value, not a denial.
2981 */
Eric Paris6a9de492012-01-03 12:25:14 -05002982 error = selinux_capable(current_cred(), &init_user_ns, CAP_MAC_ADMIN,
David Howells3699c532009-01-06 22:27:01 +00002983 SECURITY_CAP_NOAUDIT);
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04002984 if (!error)
2985 error = security_sid_to_context_force(isec->sid, &context,
2986 &size);
2987 else
2988 error = security_sid_to_context(isec->sid, &context, &size);
David P. Quigley42492592008-02-04 22:29:39 -08002989 if (error)
2990 return error;
2991 error = size;
2992 if (alloc) {
2993 *buffer = context;
2994 goto out_nofree;
2995 }
2996 kfree(context);
2997out_nofree:
2998 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999}
3000
3001static int selinux_inode_setsecurity(struct inode *inode, const char *name,
Eric Paris828dfe12008-04-17 13:17:49 -04003002 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003{
3004 struct inode_security_struct *isec = inode->i_security;
3005 u32 newsid;
3006 int rc;
3007
3008 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3009 return -EOPNOTSUPP;
3010
3011 if (!value || !size)
3012 return -EACCES;
3013
Eric Paris828dfe12008-04-17 13:17:49 -04003014 rc = security_context_to_sid((void *)value, size, &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 if (rc)
3016 return rc;
3017
3018 isec->sid = newsid;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04003019 isec->initialized = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 return 0;
3021}
3022
3023static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
3024{
3025 const int len = sizeof(XATTR_NAME_SELINUX);
3026 if (buffer && len <= buffer_size)
3027 memcpy(buffer, XATTR_NAME_SELINUX, len);
3028 return len;
3029}
3030
Ahmed S. Darwish713a04a2008-03-01 21:52:30 +02003031static void selinux_inode_getsecid(const struct inode *inode, u32 *secid)
3032{
3033 struct inode_security_struct *isec = inode->i_security;
3034 *secid = isec->sid;
3035}
3036
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037/* file security operations */
3038
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003039static int selinux_revalidate_file_permission(struct file *file, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040{
David Howells88e67f32008-11-14 10:39:21 +11003041 const struct cred *cred = current_cred();
Josef Sipek3d5ff522006-12-08 02:37:38 -08003042 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
3045 if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
3046 mask |= MAY_APPEND;
3047
Paul Moore389fb802009-03-27 17:10:34 -04003048 return file_has_perm(cred, file,
3049 file_mask_to_av(inode->i_mode, mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050}
3051
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003052static int selinux_file_permission(struct file *file, int mask)
3053{
Stephen Smalley20dda182009-06-22 14:54:53 -04003054 struct inode *inode = file->f_path.dentry->d_inode;
3055 struct file_security_struct *fsec = file->f_security;
3056 struct inode_security_struct *isec = inode->i_security;
3057 u32 sid = current_sid();
3058
Paul Moore389fb802009-03-27 17:10:34 -04003059 if (!mask)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003060 /* No permission to check. Existence test. */
3061 return 0;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003062
Stephen Smalley20dda182009-06-22 14:54:53 -04003063 if (sid == fsec->sid && fsec->isid == isec->sid &&
3064 fsec->pseqno == avc_policy_seqno())
3065 /* No change since dentry_open check. */
3066 return 0;
3067
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003068 return selinux_revalidate_file_permission(file, mask);
3069}
3070
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071static int selinux_file_alloc_security(struct file *file)
3072{
3073 return file_alloc_security(file);
3074}
3075
3076static void selinux_file_free_security(struct file *file)
3077{
3078 file_free_security(file);
3079}
3080
3081static int selinux_file_ioctl(struct file *file, unsigned int cmd,
3082 unsigned long arg)
3083{
David Howells88e67f32008-11-14 10:39:21 +11003084 const struct cred *cred = current_cred();
Eric Paris0b24dcb2011-02-25 15:39:20 -05003085 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086
Eric Paris0b24dcb2011-02-25 15:39:20 -05003087 switch (cmd) {
3088 case FIONREAD:
3089 /* fall through */
3090 case FIBMAP:
3091 /* fall through */
3092 case FIGETBSZ:
3093 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003094 case FS_IOC_GETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003095 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003096 case FS_IOC_GETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003097 error = file_has_perm(cred, file, FILE__GETATTR);
3098 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099
Al Viro2f99c362012-03-23 16:04:05 -04003100 case FS_IOC_SETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003101 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003102 case FS_IOC_SETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003103 error = file_has_perm(cred, file, FILE__SETATTR);
3104 break;
3105
3106 /* sys_ioctl() checks */
3107 case FIONBIO:
3108 /* fall through */
3109 case FIOASYNC:
3110 error = file_has_perm(cred, file, 0);
3111 break;
3112
3113 case KDSKBENT:
3114 case KDSKBSENT:
Eric Paris6a9de492012-01-03 12:25:14 -05003115 error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
3116 SECURITY_CAP_AUDIT);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003117 break;
3118
3119 /* default case assumes that the command will go
3120 * to the file's ioctl() function.
3121 */
3122 default:
3123 error = file_has_perm(cred, file, FILE__IOCTL);
3124 }
3125 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126}
3127
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003128static int default_noexec;
3129
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
3131{
David Howells88e67f32008-11-14 10:39:21 +11003132 const struct cred *cred = current_cred();
David Howellsd84f4f92008-11-14 10:39:23 +11003133 int rc = 0;
David Howells88e67f32008-11-14 10:39:21 +11003134
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003135 if (default_noexec &&
3136 (prot & PROT_EXEC) && (!file || (!shared && (prot & PROT_WRITE)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 /*
3138 * We are making executable an anonymous mapping or a
3139 * private file mapping that will also be writable.
3140 * This has an additional check.
3141 */
David Howellsd84f4f92008-11-14 10:39:23 +11003142 rc = cred_has_perm(cred, cred, PROCESS__EXECMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 if (rc)
David Howellsd84f4f92008-11-14 10:39:23 +11003144 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146
3147 if (file) {
3148 /* read access is always possible with a mapping */
3149 u32 av = FILE__READ;
3150
3151 /* write access only matters if the mapping is shared */
3152 if (shared && (prot & PROT_WRITE))
3153 av |= FILE__WRITE;
3154
3155 if (prot & PROT_EXEC)
3156 av |= FILE__EXECUTE;
3157
David Howells88e67f32008-11-14 10:39:21 +11003158 return file_has_perm(cred, file, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 }
David Howellsd84f4f92008-11-14 10:39:23 +11003160
3161error:
3162 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163}
3164
3165static int selinux_file_mmap(struct file *file, unsigned long reqprot,
Eric Parised032182007-06-28 15:55:21 -04003166 unsigned long prot, unsigned long flags,
3167 unsigned long addr, unsigned long addr_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168{
Eric Parised032182007-06-28 15:55:21 -04003169 int rc = 0;
David Howells275bb412008-11-14 10:39:19 +11003170 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171
Eric Paris84336d1a2009-07-31 12:54:05 -04003172 /*
3173 * notice that we are intentionally putting the SELinux check before
3174 * the secondary cap_file_mmap check. This is such a likely attempt
3175 * at bad behaviour/exploit that we always want to get the AVC, even
3176 * if DAC would have also denied the operation.
3177 */
Eric Parisa2551df2009-07-31 12:54:11 -04003178 if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
Eric Parised032182007-06-28 15:55:21 -04003179 rc = avc_has_perm(sid, sid, SECCLASS_MEMPROTECT,
3180 MEMPROTECT__MMAP_ZERO, NULL);
Eric Paris84336d1a2009-07-31 12:54:05 -04003181 if (rc)
3182 return rc;
3183 }
3184
3185 /* do DAC check on address space usage */
3186 rc = cap_file_mmap(file, reqprot, prot, flags, addr, addr_only);
Eric Parised032182007-06-28 15:55:21 -04003187 if (rc || addr_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 return rc;
3189
3190 if (selinux_checkreqprot)
3191 prot = reqprot;
3192
3193 return file_map_prot_check(file, prot,
3194 (flags & MAP_TYPE) == MAP_SHARED);
3195}
3196
3197static int selinux_file_mprotect(struct vm_area_struct *vma,
3198 unsigned long reqprot,
3199 unsigned long prot)
3200{
David Howells88e67f32008-11-14 10:39:21 +11003201 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202
3203 if (selinux_checkreqprot)
3204 prot = reqprot;
3205
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003206 if (default_noexec &&
3207 (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
James Morrisd541bbe2009-01-29 12:19:51 +11003208 int rc = 0;
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003209 if (vma->vm_start >= vma->vm_mm->start_brk &&
3210 vma->vm_end <= vma->vm_mm->brk) {
David Howellsd84f4f92008-11-14 10:39:23 +11003211 rc = cred_has_perm(cred, cred, PROCESS__EXECHEAP);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003212 } else if (!vma->vm_file &&
3213 vma->vm_start <= vma->vm_mm->start_stack &&
3214 vma->vm_end >= vma->vm_mm->start_stack) {
David Howells3b11a1d2008-11-14 10:39:26 +11003215 rc = current_has_perm(current, PROCESS__EXECSTACK);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003216 } else if (vma->vm_file && vma->anon_vma) {
3217 /*
3218 * We are making executable a file mapping that has
3219 * had some COW done. Since pages might have been
3220 * written, check ability to execute the possibly
3221 * modified content. This typically should only
3222 * occur for text relocations.
3223 */
David Howellsd84f4f92008-11-14 10:39:23 +11003224 rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003225 }
Lorenzo Hernandez García-Hierro6b992192005-06-25 14:54:34 -07003226 if (rc)
3227 return rc;
3228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229
3230 return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
3231}
3232
3233static int selinux_file_lock(struct file *file, unsigned int cmd)
3234{
David Howells88e67f32008-11-14 10:39:21 +11003235 const struct cred *cred = current_cred();
3236
3237 return file_has_perm(cred, file, FILE__LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238}
3239
3240static int selinux_file_fcntl(struct file *file, unsigned int cmd,
3241 unsigned long arg)
3242{
David Howells88e67f32008-11-14 10:39:21 +11003243 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 int err = 0;
3245
3246 switch (cmd) {
Eric Paris828dfe12008-04-17 13:17:49 -04003247 case F_SETFL:
3248 if (!file->f_path.dentry || !file->f_path.dentry->d_inode) {
3249 err = -EINVAL;
3250 break;
3251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252
Eric Paris828dfe12008-04-17 13:17:49 -04003253 if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
David Howells88e67f32008-11-14 10:39:21 +11003254 err = file_has_perm(cred, file, FILE__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003256 }
3257 /* fall through */
3258 case F_SETOWN:
3259 case F_SETSIG:
3260 case F_GETFL:
3261 case F_GETOWN:
3262 case F_GETSIG:
3263 /* Just check FD__USE permission */
David Howells88e67f32008-11-14 10:39:21 +11003264 err = file_has_perm(cred, file, 0);
Eric Paris828dfe12008-04-17 13:17:49 -04003265 break;
3266 case F_GETLK:
3267 case F_SETLK:
3268 case F_SETLKW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269#if BITS_PER_LONG == 32
Eric Paris828dfe12008-04-17 13:17:49 -04003270 case F_GETLK64:
3271 case F_SETLK64:
3272 case F_SETLKW64:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273#endif
Eric Paris828dfe12008-04-17 13:17:49 -04003274 if (!file->f_path.dentry || !file->f_path.dentry->d_inode) {
3275 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003277 }
David Howells88e67f32008-11-14 10:39:21 +11003278 err = file_has_perm(cred, file, FILE__LOCK);
Eric Paris828dfe12008-04-17 13:17:49 -04003279 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 }
3281
3282 return err;
3283}
3284
3285static int selinux_file_set_fowner(struct file *file)
3286{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 struct file_security_struct *fsec;
3288
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 fsec = file->f_security;
David Howells275bb412008-11-14 10:39:19 +11003290 fsec->fown_sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291
3292 return 0;
3293}
3294
3295static int selinux_file_send_sigiotask(struct task_struct *tsk,
3296 struct fown_struct *fown, int signum)
3297{
Eric Paris828dfe12008-04-17 13:17:49 -04003298 struct file *file;
Stephen Smalley65c90bc2009-05-04 15:43:18 -04003299 u32 sid = task_sid(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 struct file_security_struct *fsec;
3302
3303 /* struct fown_struct is never outside the context of a struct file */
Eric Paris828dfe12008-04-17 13:17:49 -04003304 file = container_of(fown, struct file, f_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 fsec = file->f_security;
3307
3308 if (!signum)
3309 perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
3310 else
3311 perm = signal_to_av(signum);
3312
David Howells275bb412008-11-14 10:39:19 +11003313 return avc_has_perm(fsec->fown_sid, sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 SECCLASS_PROCESS, perm, NULL);
3315}
3316
3317static int selinux_file_receive(struct file *file)
3318{
David Howells88e67f32008-11-14 10:39:21 +11003319 const struct cred *cred = current_cred();
3320
3321 return file_has_perm(cred, file, file_to_av(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322}
3323
David Howells745ca242008-11-14 10:39:22 +11003324static int selinux_dentry_open(struct file *file, const struct cred *cred)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003325{
3326 struct file_security_struct *fsec;
3327 struct inode *inode;
3328 struct inode_security_struct *isec;
David Howellsd84f4f92008-11-14 10:39:23 +11003329
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003330 inode = file->f_path.dentry->d_inode;
3331 fsec = file->f_security;
3332 isec = inode->i_security;
3333 /*
3334 * Save inode label and policy sequence number
3335 * at open-time so that selinux_file_permission
3336 * can determine whether revalidation is necessary.
3337 * Task label is already saved in the file security
3338 * struct as its SID.
3339 */
3340 fsec->isid = isec->sid;
3341 fsec->pseqno = avc_policy_seqno();
3342 /*
3343 * Since the inode label or policy seqno may have changed
3344 * between the selinux_inode_permission check and the saving
3345 * of state above, recheck that access is still permitted.
3346 * Otherwise, access might never be revalidated against the
3347 * new inode label or new policy.
3348 * This check is not redundant - do not remove.
3349 */
Linus Torvalds95f4efb2011-06-08 15:11:56 -07003350 return inode_has_perm_noadp(cred, inode, open_file_to_av(file), 0);
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003351}
3352
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353/* task security operations */
3354
3355static int selinux_task_create(unsigned long clone_flags)
3356{
David Howells3b11a1d2008-11-14 10:39:26 +11003357 return current_has_perm(current, PROCESS__FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358}
3359
David Howellsf1752ee2008-11-14 10:39:17 +11003360/*
David Howellsee18d642009-09-02 09:14:21 +01003361 * allocate the SELinux part of blank credentials
3362 */
3363static int selinux_cred_alloc_blank(struct cred *cred, gfp_t gfp)
3364{
3365 struct task_security_struct *tsec;
3366
3367 tsec = kzalloc(sizeof(struct task_security_struct), gfp);
3368 if (!tsec)
3369 return -ENOMEM;
3370
3371 cred->security = tsec;
3372 return 0;
3373}
3374
3375/*
David Howellsf1752ee2008-11-14 10:39:17 +11003376 * detach and free the LSM part of a set of credentials
3377 */
3378static void selinux_cred_free(struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379{
David Howellsf1752ee2008-11-14 10:39:17 +11003380 struct task_security_struct *tsec = cred->security;
David Howellse0e81732009-09-02 09:13:40 +01003381
Tetsuo Handa2edeaa32011-02-07 13:36:10 +00003382 /*
3383 * cred->security == NULL if security_cred_alloc_blank() or
3384 * security_prepare_creds() returned an error.
3385 */
3386 BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE);
David Howellse0e81732009-09-02 09:13:40 +01003387 cred->security = (void *) 0x7UL;
David Howellsf1752ee2008-11-14 10:39:17 +11003388 kfree(tsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389}
3390
David Howellsd84f4f92008-11-14 10:39:23 +11003391/*
3392 * prepare a new set of credentials for modification
3393 */
3394static int selinux_cred_prepare(struct cred *new, const struct cred *old,
3395 gfp_t gfp)
3396{
3397 const struct task_security_struct *old_tsec;
3398 struct task_security_struct *tsec;
3399
3400 old_tsec = old->security;
3401
3402 tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp);
3403 if (!tsec)
3404 return -ENOMEM;
3405
3406 new->security = tsec;
3407 return 0;
3408}
3409
3410/*
David Howellsee18d642009-09-02 09:14:21 +01003411 * transfer the SELinux data to a blank set of creds
3412 */
3413static void selinux_cred_transfer(struct cred *new, const struct cred *old)
3414{
3415 const struct task_security_struct *old_tsec = old->security;
3416 struct task_security_struct *tsec = new->security;
3417
3418 *tsec = *old_tsec;
3419}
3420
3421/*
David Howells3a3b7ce2008-11-14 10:39:28 +11003422 * set the security data for a kernel service
3423 * - all the creation contexts are set to unlabelled
3424 */
3425static int selinux_kernel_act_as(struct cred *new, u32 secid)
3426{
3427 struct task_security_struct *tsec = new->security;
3428 u32 sid = current_sid();
3429 int ret;
3430
3431 ret = avc_has_perm(sid, secid,
3432 SECCLASS_KERNEL_SERVICE,
3433 KERNEL_SERVICE__USE_AS_OVERRIDE,
3434 NULL);
3435 if (ret == 0) {
3436 tsec->sid = secid;
3437 tsec->create_sid = 0;
3438 tsec->keycreate_sid = 0;
3439 tsec->sockcreate_sid = 0;
3440 }
3441 return ret;
3442}
3443
3444/*
3445 * set the file creation context in a security record to the same as the
3446 * objective context of the specified inode
3447 */
3448static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
3449{
3450 struct inode_security_struct *isec = inode->i_security;
3451 struct task_security_struct *tsec = new->security;
3452 u32 sid = current_sid();
3453 int ret;
3454
3455 ret = avc_has_perm(sid, isec->sid,
3456 SECCLASS_KERNEL_SERVICE,
3457 KERNEL_SERVICE__CREATE_FILES_AS,
3458 NULL);
3459
3460 if (ret == 0)
3461 tsec->create_sid = isec->sid;
David Howellsef574712010-02-26 01:56:16 +00003462 return ret;
David Howells3a3b7ce2008-11-14 10:39:28 +11003463}
3464
Eric Parisdd8dbf22009-11-03 16:35:32 +11003465static int selinux_kernel_module_request(char *kmod_name)
Eric Paris25354c42009-08-13 09:45:03 -04003466{
Eric Parisdd8dbf22009-11-03 16:35:32 +11003467 u32 sid;
3468 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07003469 struct selinux_audit_data sad = {0,};
Eric Parisdd8dbf22009-11-03 16:35:32 +11003470
3471 sid = task_sid(current);
3472
3473 COMMON_AUDIT_DATA_INIT(&ad, KMOD);
Eric Paris3b3b0e42012-04-03 09:37:02 -07003474 ad.selinux_audit_data = &sad;
Eric Parisdd8dbf22009-11-03 16:35:32 +11003475 ad.u.kmod_name = kmod_name;
3476
3477 return avc_has_perm(sid, SECINITSID_KERNEL, SECCLASS_SYSTEM,
3478 SYSTEM__MODULE_REQUEST, &ad);
Eric Paris25354c42009-08-13 09:45:03 -04003479}
3480
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
3482{
David Howells3b11a1d2008-11-14 10:39:26 +11003483 return current_has_perm(p, PROCESS__SETPGID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484}
3485
3486static int selinux_task_getpgid(struct task_struct *p)
3487{
David Howells3b11a1d2008-11-14 10:39:26 +11003488 return current_has_perm(p, PROCESS__GETPGID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489}
3490
3491static int selinux_task_getsid(struct task_struct *p)
3492{
David Howells3b11a1d2008-11-14 10:39:26 +11003493 return current_has_perm(p, PROCESS__GETSESSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494}
3495
David Quigleyf9008e42006-06-30 01:55:46 -07003496static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
3497{
David Howells275bb412008-11-14 10:39:19 +11003498 *secid = task_sid(p);
David Quigleyf9008e42006-06-30 01:55:46 -07003499}
3500
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501static int selinux_task_setnice(struct task_struct *p, int nice)
3502{
3503 int rc;
3504
Eric Paris200ac532009-02-12 15:01:04 -05003505 rc = cap_task_setnice(p, nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 if (rc)
3507 return rc;
3508
David Howells3b11a1d2008-11-14 10:39:26 +11003509 return current_has_perm(p, PROCESS__SETSCHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510}
3511
James Morris03e68062006-06-23 02:03:58 -07003512static int selinux_task_setioprio(struct task_struct *p, int ioprio)
3513{
Serge E. Hallynb5376772007-10-16 23:31:36 -07003514 int rc;
3515
Eric Paris200ac532009-02-12 15:01:04 -05003516 rc = cap_task_setioprio(p, ioprio);
Serge E. Hallynb5376772007-10-16 23:31:36 -07003517 if (rc)
3518 return rc;
3519
David Howells3b11a1d2008-11-14 10:39:26 +11003520 return current_has_perm(p, PROCESS__SETSCHED);
James Morris03e68062006-06-23 02:03:58 -07003521}
3522
David Quigleya1836a42006-06-30 01:55:49 -07003523static int selinux_task_getioprio(struct task_struct *p)
3524{
David Howells3b11a1d2008-11-14 10:39:26 +11003525 return current_has_perm(p, PROCESS__GETSCHED);
David Quigleya1836a42006-06-30 01:55:49 -07003526}
3527
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003528static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
3529 struct rlimit *new_rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530{
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003531 struct rlimit *old_rlim = p->signal->rlim + resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532
3533 /* Control the ability to change the hard limit (whether
3534 lowering or raising it), so that the hard limit can
3535 later be used as a safe reset point for the soft limit
David Howellsd84f4f92008-11-14 10:39:23 +11003536 upon context transitions. See selinux_bprm_committing_creds. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 if (old_rlim->rlim_max != new_rlim->rlim_max)
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003538 return current_has_perm(p, PROCESS__SETRLIMIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539
3540 return 0;
3541}
3542
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09003543static int selinux_task_setscheduler(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544{
Serge E. Hallynb5376772007-10-16 23:31:36 -07003545 int rc;
3546
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09003547 rc = cap_task_setscheduler(p);
Serge E. Hallynb5376772007-10-16 23:31:36 -07003548 if (rc)
3549 return rc;
3550
David Howells3b11a1d2008-11-14 10:39:26 +11003551 return current_has_perm(p, PROCESS__SETSCHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552}
3553
3554static int selinux_task_getscheduler(struct task_struct *p)
3555{
David Howells3b11a1d2008-11-14 10:39:26 +11003556 return current_has_perm(p, PROCESS__GETSCHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557}
3558
David Quigley35601542006-06-23 02:04:01 -07003559static int selinux_task_movememory(struct task_struct *p)
3560{
David Howells3b11a1d2008-11-14 10:39:26 +11003561 return current_has_perm(p, PROCESS__SETSCHED);
David Quigley35601542006-06-23 02:04:01 -07003562}
3563
David Quigleyf9008e42006-06-30 01:55:46 -07003564static int selinux_task_kill(struct task_struct *p, struct siginfo *info,
3565 int sig, u32 secid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566{
3567 u32 perm;
3568 int rc;
3569
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 if (!sig)
3571 perm = PROCESS__SIGNULL; /* null signal; existence test */
3572 else
3573 perm = signal_to_av(sig);
David Quigleyf9008e42006-06-30 01:55:46 -07003574 if (secid)
David Howells275bb412008-11-14 10:39:19 +11003575 rc = avc_has_perm(secid, task_sid(p),
3576 SECCLASS_PROCESS, perm, NULL);
David Quigleyf9008e42006-06-30 01:55:46 -07003577 else
David Howells3b11a1d2008-11-14 10:39:26 +11003578 rc = current_has_perm(p, perm);
David Quigleyf9008e42006-06-30 01:55:46 -07003579 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580}
3581
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582static int selinux_task_wait(struct task_struct *p)
3583{
Eric Paris8a535142007-10-22 16:10:31 -04003584 return task_has_perm(p, current, PROCESS__SIGCHLD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585}
3586
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587static void selinux_task_to_inode(struct task_struct *p,
3588 struct inode *inode)
3589{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 struct inode_security_struct *isec = inode->i_security;
David Howells275bb412008-11-14 10:39:19 +11003591 u32 sid = task_sid(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592
David Howells275bb412008-11-14 10:39:19 +11003593 isec->sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 isec->initialized = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595}
3596
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003598static int selinux_parse_skb_ipv4(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04003599 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600{
3601 int offset, ihlen, ret = -EINVAL;
3602 struct iphdr _iph, *ih;
3603
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03003604 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
3606 if (ih == NULL)
3607 goto out;
3608
3609 ihlen = ih->ihl * 4;
3610 if (ihlen < sizeof(_iph))
3611 goto out;
3612
Eric Paris48c62af2012-04-02 13:15:44 -04003613 ad->u.net->v4info.saddr = ih->saddr;
3614 ad->u.net->v4info.daddr = ih->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 ret = 0;
3616
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003617 if (proto)
3618 *proto = ih->protocol;
3619
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 switch (ih->protocol) {
Eric Paris828dfe12008-04-17 13:17:49 -04003621 case IPPROTO_TCP: {
3622 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623
Eric Paris828dfe12008-04-17 13:17:49 -04003624 if (ntohs(ih->frag_off) & IP_OFFSET)
3625 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626
3627 offset += ihlen;
3628 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3629 if (th == NULL)
3630 break;
3631
Eric Paris48c62af2012-04-02 13:15:44 -04003632 ad->u.net->sport = th->source;
3633 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636
Eric Paris828dfe12008-04-17 13:17:49 -04003637 case IPPROTO_UDP: {
3638 struct udphdr _udph, *uh;
3639
3640 if (ntohs(ih->frag_off) & IP_OFFSET)
3641 break;
3642
3643 offset += ihlen;
3644 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3645 if (uh == NULL)
3646 break;
3647
Eric Paris48c62af2012-04-02 13:15:44 -04003648 ad->u.net->sport = uh->source;
3649 ad->u.net->dport = uh->dest;
Eric Paris828dfe12008-04-17 13:17:49 -04003650 break;
3651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652
James Morris2ee92d42006-11-13 16:09:01 -08003653 case IPPROTO_DCCP: {
3654 struct dccp_hdr _dccph, *dh;
3655
3656 if (ntohs(ih->frag_off) & IP_OFFSET)
3657 break;
3658
3659 offset += ihlen;
3660 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3661 if (dh == NULL)
3662 break;
3663
Eric Paris48c62af2012-04-02 13:15:44 -04003664 ad->u.net->sport = dh->dccph_sport;
3665 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08003666 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003667 }
James Morris2ee92d42006-11-13 16:09:01 -08003668
Eric Paris828dfe12008-04-17 13:17:49 -04003669 default:
3670 break;
3671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672out:
3673 return ret;
3674}
3675
3676#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3677
3678/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003679static int selinux_parse_skb_ipv6(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04003680 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681{
3682 u8 nexthdr;
3683 int ret = -EINVAL, offset;
3684 struct ipv6hdr _ipv6h, *ip6;
Jesse Gross75f28112011-11-30 17:05:51 -08003685 __be16 frag_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03003687 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3689 if (ip6 == NULL)
3690 goto out;
3691
Eric Paris48c62af2012-04-02 13:15:44 -04003692 ad->u.net->v6info.saddr = ip6->saddr;
3693 ad->u.net->v6info.daddr = ip6->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 ret = 0;
3695
3696 nexthdr = ip6->nexthdr;
3697 offset += sizeof(_ipv6h);
Jesse Gross75f28112011-11-30 17:05:51 -08003698 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 if (offset < 0)
3700 goto out;
3701
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003702 if (proto)
3703 *proto = nexthdr;
3704
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 switch (nexthdr) {
3706 case IPPROTO_TCP: {
Eric Paris828dfe12008-04-17 13:17:49 -04003707 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708
3709 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3710 if (th == NULL)
3711 break;
3712
Eric Paris48c62af2012-04-02 13:15:44 -04003713 ad->u.net->sport = th->source;
3714 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 break;
3716 }
3717
3718 case IPPROTO_UDP: {
3719 struct udphdr _udph, *uh;
3720
3721 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3722 if (uh == NULL)
3723 break;
3724
Eric Paris48c62af2012-04-02 13:15:44 -04003725 ad->u.net->sport = uh->source;
3726 ad->u.net->dport = uh->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 break;
3728 }
3729
James Morris2ee92d42006-11-13 16:09:01 -08003730 case IPPROTO_DCCP: {
3731 struct dccp_hdr _dccph, *dh;
3732
3733 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3734 if (dh == NULL)
3735 break;
3736
Eric Paris48c62af2012-04-02 13:15:44 -04003737 ad->u.net->sport = dh->dccph_sport;
3738 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08003739 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003740 }
James Morris2ee92d42006-11-13 16:09:01 -08003741
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 /* includes fragments */
3743 default:
3744 break;
3745 }
3746out:
3747 return ret;
3748}
3749
3750#endif /* IPV6 */
3751
Thomas Liu2bf49692009-07-14 12:14:09 -04003752static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
David Howellscf9481e2008-07-27 21:31:07 +10003753 char **_addrp, int src, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754{
David Howellscf9481e2008-07-27 21:31:07 +10003755 char *addrp;
3756 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757
Eric Paris48c62af2012-04-02 13:15:44 -04003758 switch (ad->u.net->family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 case PF_INET:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003760 ret = selinux_parse_skb_ipv4(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10003761 if (ret)
3762 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04003763 addrp = (char *)(src ? &ad->u.net->v4info.saddr :
3764 &ad->u.net->v4info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10003765 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766
3767#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3768 case PF_INET6:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003769 ret = selinux_parse_skb_ipv6(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10003770 if (ret)
3771 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04003772 addrp = (char *)(src ? &ad->u.net->v6info.saddr :
3773 &ad->u.net->v6info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10003774 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775#endif /* IPV6 */
3776 default:
David Howellscf9481e2008-07-27 21:31:07 +10003777 addrp = NULL;
3778 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 }
3780
David Howellscf9481e2008-07-27 21:31:07 +10003781parse_error:
3782 printk(KERN_WARNING
3783 "SELinux: failure in selinux_parse_skb(),"
3784 " unable to parse packet\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 return ret;
David Howellscf9481e2008-07-27 21:31:07 +10003786
3787okay:
3788 if (_addrp)
3789 *_addrp = addrp;
3790 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791}
3792
Paul Moore4f6a9932007-03-01 14:35:22 -05003793/**
Paul Moore220deb92008-01-29 08:38:23 -05003794 * selinux_skb_peerlbl_sid - Determine the peer label of a packet
Paul Moore4f6a9932007-03-01 14:35:22 -05003795 * @skb: the packet
Paul Moore75e22912008-01-29 08:38:04 -05003796 * @family: protocol family
Paul Moore220deb92008-01-29 08:38:23 -05003797 * @sid: the packet's peer label SID
Paul Moore4f6a9932007-03-01 14:35:22 -05003798 *
3799 * Description:
Paul Moore220deb92008-01-29 08:38:23 -05003800 * Check the various different forms of network peer labeling and determine
3801 * the peer label/SID for the packet; most of the magic actually occurs in
3802 * the security server function security_net_peersid_cmp(). The function
3803 * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
3804 * or -EACCES if @sid is invalid due to inconsistencies with the different
3805 * peer labels.
Paul Moore4f6a9932007-03-01 14:35:22 -05003806 *
3807 */
Paul Moore220deb92008-01-29 08:38:23 -05003808static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
Paul Moore4f6a9932007-03-01 14:35:22 -05003809{
Paul Moore71f1cb02008-01-29 08:51:16 -05003810 int err;
Paul Moore4f6a9932007-03-01 14:35:22 -05003811 u32 xfrm_sid;
3812 u32 nlbl_sid;
Paul Moore220deb92008-01-29 08:38:23 -05003813 u32 nlbl_type;
Paul Moore4f6a9932007-03-01 14:35:22 -05003814
Paul Moore73ec9552013-12-10 14:57:54 -05003815 selinux_xfrm_skb_sid(skb, &xfrm_sid);
Paul Moore5dbe1eb2008-01-29 08:44:18 -05003816 selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
Paul Moore220deb92008-01-29 08:38:23 -05003817
Paul Moore71f1cb02008-01-29 08:51:16 -05003818 err = security_net_peersid_resolve(nlbl_sid, nlbl_type, xfrm_sid, sid);
3819 if (unlikely(err)) {
3820 printk(KERN_WARNING
3821 "SELinux: failure in selinux_skb_peerlbl_sid(),"
3822 " unable to determine packet's peer label\n");
Paul Moore220deb92008-01-29 08:38:23 -05003823 return -EACCES;
Paul Moore71f1cb02008-01-29 08:51:16 -05003824 }
Paul Moore220deb92008-01-29 08:38:23 -05003825
3826 return 0;
Paul Moore4f6a9932007-03-01 14:35:22 -05003827}
3828
Paul Moore2ea04e52013-12-04 16:10:51 -05003829/**
3830 * selinux_conn_sid - Determine the child socket label for a connection
3831 * @sk_sid: the parent socket's SID
3832 * @skb_sid: the packet's SID
3833 * @conn_sid: the resulting connection SID
3834 *
3835 * If @skb_sid is valid then the user:role:type information from @sk_sid is
3836 * combined with the MLS information from @skb_sid in order to create
3837 * @conn_sid. If @skb_sid is not valid then then @conn_sid is simply a copy
3838 * of @sk_sid. Returns zero on success, negative values on failure.
3839 *
3840 */
3841static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid)
3842{
3843 int err = 0;
3844
3845 if (skb_sid != SECSID_NULL)
3846 err = security_sid_mls_copy(sk_sid, skb_sid, conn_sid);
3847 else
3848 *conn_sid = sk_sid;
3849
3850 return err;
3851}
3852
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853/* socket security operations */
Paul Moored4f2d972010-04-22 14:46:18 -04003854
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003855static int socket_sockcreate_sid(const struct task_security_struct *tsec,
3856 u16 secclass, u32 *socksid)
Paul Moored4f2d972010-04-22 14:46:18 -04003857{
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003858 if (tsec->sockcreate_sid > SECSID_NULL) {
3859 *socksid = tsec->sockcreate_sid;
3860 return 0;
3861 }
3862
3863 return security_transition_sid(tsec->sid, tsec->sid, secclass, NULL,
3864 socksid);
Paul Moored4f2d972010-04-22 14:46:18 -04003865}
3866
Paul Moore253bfae2010-04-22 14:46:19 -04003867static int sock_has_perm(struct task_struct *task, struct sock *sk, u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868{
Paul Moore253bfae2010-04-22 14:46:19 -04003869 struct sk_security_struct *sksec = sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04003870 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07003871 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04003872 struct lsm_network_audit net = {0,};
Paul Moore253bfae2010-04-22 14:46:19 -04003873 u32 tsid = task_sid(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874
Paul Moore253bfae2010-04-22 14:46:19 -04003875 if (sksec->sid == SECINITSID_KERNEL)
3876 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877
Thomas Liu2bf49692009-07-14 12:14:09 -04003878 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07003879 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04003880 ad.u.net = &net;
3881 ad.u.net->sk = sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882
Paul Moore253bfae2010-04-22 14:46:19 -04003883 return avc_has_perm(tsid, sksec->sid, sksec->sclass, perms, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884}
3885
3886static int selinux_socket_create(int family, int type,
3887 int protocol, int kern)
3888{
Paul Moore5fb49872010-04-22 14:46:19 -04003889 const struct task_security_struct *tsec = current_security();
Paul Moored4f2d972010-04-22 14:46:18 -04003890 u32 newsid;
David Howells275bb412008-11-14 10:39:19 +11003891 u16 secclass;
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003892 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893
3894 if (kern)
Paul Moored4f2d972010-04-22 14:46:18 -04003895 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896
David Howells275bb412008-11-14 10:39:19 +11003897 secclass = socket_type_to_security_class(family, type, protocol);
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003898 rc = socket_sockcreate_sid(tsec, secclass, &newsid);
3899 if (rc)
3900 return rc;
3901
Paul Moored4f2d972010-04-22 14:46:18 -04003902 return avc_has_perm(tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903}
3904
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003905static int selinux_socket_post_create(struct socket *sock, int family,
3906 int type, int protocol, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907{
Paul Moore5fb49872010-04-22 14:46:19 -04003908 const struct task_security_struct *tsec = current_security();
Paul Moored4f2d972010-04-22 14:46:18 -04003909 struct inode_security_struct *isec = SOCK_INODE(sock)->i_security;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07003910 struct sk_security_struct *sksec;
David Howells275bb412008-11-14 10:39:19 +11003911 int err = 0;
3912
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003913 isec->sclass = socket_type_to_security_class(family, type, protocol);
3914
David Howells275bb412008-11-14 10:39:19 +11003915 if (kern)
3916 isec->sid = SECINITSID_KERNEL;
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003917 else {
3918 err = socket_sockcreate_sid(tsec, isec->sclass, &(isec->sid));
3919 if (err)
3920 return err;
3921 }
David Howells275bb412008-11-14 10:39:19 +11003922
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 isec->initialized = 1;
3924
Venkat Yekkirala892c1412006-08-04 23:08:56 -07003925 if (sock->sk) {
3926 sksec = sock->sk->sk_security;
3927 sksec->sid = isec->sid;
Paul Moore220deb92008-01-29 08:38:23 -05003928 sksec->sclass = isec->sclass;
Paul Moore389fb802009-03-27 17:10:34 -04003929 err = selinux_netlbl_socket_post_create(sock->sk, family);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07003930 }
3931
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003932 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933}
3934
3935/* Range of port numbers used to automatically bind.
3936 Need to determine whether we should perform a name_bind
3937 permission check between the socket and the port number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938
3939static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
3940{
Paul Moore253bfae2010-04-22 14:46:19 -04003941 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 u16 family;
3943 int err;
3944
Paul Moore253bfae2010-04-22 14:46:19 -04003945 err = sock_has_perm(current, sk, SOCKET__BIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 if (err)
3947 goto out;
3948
3949 /*
3950 * If PF_INET or PF_INET6, check name_bind permission for the port.
James Morris13402582005-09-30 14:24:34 -04003951 * Multiple address binding for SCTP is not supported yet: we just
3952 * check the first address now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 */
Paul Moore253bfae2010-04-22 14:46:19 -04003954 family = sk->sk_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 if (family == PF_INET || family == PF_INET6) {
3956 char *addrp;
Paul Moore253bfae2010-04-22 14:46:19 -04003957 struct sk_security_struct *sksec = sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04003958 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07003959 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04003960 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 struct sockaddr_in *addr4 = NULL;
3962 struct sockaddr_in6 *addr6 = NULL;
3963 unsigned short snum;
James Morrise399f982008-06-12 01:39:58 +10003964 u32 sid, node_perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 if (family == PF_INET) {
3967 addr4 = (struct sockaddr_in *)address;
3968 snum = ntohs(addr4->sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 addrp = (char *)&addr4->sin_addr.s_addr;
3970 } else {
3971 addr6 = (struct sockaddr_in6 *)address;
3972 snum = ntohs(addr6->sin6_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 addrp = (char *)&addr6->sin6_addr.s6_addr;
3974 }
3975
Stephen Hemminger227b60f2007-10-10 17:30:46 -07003976 if (snum) {
3977 int low, high;
3978
3979 inet_get_local_port_range(&low, &high);
3980
3981 if (snum < max(PROT_SOCK, low) || snum > high) {
Paul Moore3e112172008-04-10 10:48:14 -04003982 err = sel_netport_sid(sk->sk_protocol,
3983 snum, &sid);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07003984 if (err)
3985 goto out;
Thomas Liu2bf49692009-07-14 12:14:09 -04003986 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07003987 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04003988 ad.u.net = &net;
3989 ad.u.net->sport = htons(snum);
3990 ad.u.net->family = family;
Paul Moore253bfae2010-04-22 14:46:19 -04003991 err = avc_has_perm(sksec->sid, sid,
3992 sksec->sclass,
Stephen Hemminger227b60f2007-10-10 17:30:46 -07003993 SOCKET__NAME_BIND, &ad);
3994 if (err)
3995 goto out;
3996 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 }
Eric Paris828dfe12008-04-17 13:17:49 -04003998
Paul Moore253bfae2010-04-22 14:46:19 -04003999 switch (sksec->sclass) {
James Morris13402582005-09-30 14:24:34 -04004000 case SECCLASS_TCP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 node_perm = TCP_SOCKET__NODE_BIND;
4002 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004003
James Morris13402582005-09-30 14:24:34 -04004004 case SECCLASS_UDP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005 node_perm = UDP_SOCKET__NODE_BIND;
4006 break;
James Morris2ee92d42006-11-13 16:09:01 -08004007
4008 case SECCLASS_DCCP_SOCKET:
4009 node_perm = DCCP_SOCKET__NODE_BIND;
4010 break;
4011
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 default:
4013 node_perm = RAWIP_SOCKET__NODE_BIND;
4014 break;
4015 }
Eric Paris828dfe12008-04-17 13:17:49 -04004016
Paul Moore224dfbd2008-01-29 08:38:13 -05004017 err = sel_netnode_sid(addrp, family, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 if (err)
4019 goto out;
Eric Paris828dfe12008-04-17 13:17:49 -04004020
Thomas Liu2bf49692009-07-14 12:14:09 -04004021 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004022 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004023 ad.u.net = &net;
4024 ad.u.net->sport = htons(snum);
4025 ad.u.net->family = family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026
4027 if (family == PF_INET)
Eric Paris48c62af2012-04-02 13:15:44 -04004028 ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 else
Eric Paris48c62af2012-04-02 13:15:44 -04004030 ad.u.net->v6info.saddr = addr6->sin6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031
Paul Moore253bfae2010-04-22 14:46:19 -04004032 err = avc_has_perm(sksec->sid, sid,
4033 sksec->sclass, node_perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 if (err)
4035 goto out;
4036 }
4037out:
4038 return err;
4039}
4040
4041static int selinux_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen)
4042{
Paul Moore014ab192008-10-10 10:16:33 -04004043 struct sock *sk = sock->sk;
Paul Moore253bfae2010-04-22 14:46:19 -04004044 struct sk_security_struct *sksec = sk->sk_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 int err;
4046
Paul Moore253bfae2010-04-22 14:46:19 -04004047 err = sock_has_perm(current, sk, SOCKET__CONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 if (err)
4049 return err;
4050
4051 /*
James Morris2ee92d42006-11-13 16:09:01 -08004052 * If a TCP or DCCP socket, check name_connect permission for the port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 */
Paul Moore253bfae2010-04-22 14:46:19 -04004054 if (sksec->sclass == SECCLASS_TCP_SOCKET ||
4055 sksec->sclass == SECCLASS_DCCP_SOCKET) {
Thomas Liu2bf49692009-07-14 12:14:09 -04004056 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004057 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004058 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 struct sockaddr_in *addr4 = NULL;
4060 struct sockaddr_in6 *addr6 = NULL;
4061 unsigned short snum;
James Morris2ee92d42006-11-13 16:09:01 -08004062 u32 sid, perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063
4064 if (sk->sk_family == PF_INET) {
4065 addr4 = (struct sockaddr_in *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004066 if (addrlen < sizeof(struct sockaddr_in))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 return -EINVAL;
4068 snum = ntohs(addr4->sin_port);
4069 } else {
4070 addr6 = (struct sockaddr_in6 *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004071 if (addrlen < SIN6_LEN_RFC2133)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 return -EINVAL;
4073 snum = ntohs(addr6->sin6_port);
4074 }
4075
Paul Moore3e112172008-04-10 10:48:14 -04004076 err = sel_netport_sid(sk->sk_protocol, snum, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 if (err)
4078 goto out;
4079
Paul Moore253bfae2010-04-22 14:46:19 -04004080 perm = (sksec->sclass == SECCLASS_TCP_SOCKET) ?
James Morris2ee92d42006-11-13 16:09:01 -08004081 TCP_SOCKET__NAME_CONNECT : DCCP_SOCKET__NAME_CONNECT;
4082
Thomas Liu2bf49692009-07-14 12:14:09 -04004083 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004084 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004085 ad.u.net = &net;
4086 ad.u.net->dport = htons(snum);
4087 ad.u.net->family = sk->sk_family;
Paul Moore253bfae2010-04-22 14:46:19 -04004088 err = avc_has_perm(sksec->sid, sid, sksec->sclass, perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 if (err)
4090 goto out;
4091 }
4092
Paul Moore014ab192008-10-10 10:16:33 -04004093 err = selinux_netlbl_socket_connect(sk, address);
4094
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095out:
4096 return err;
4097}
4098
4099static int selinux_socket_listen(struct socket *sock, int backlog)
4100{
Paul Moore253bfae2010-04-22 14:46:19 -04004101 return sock_has_perm(current, sock->sk, SOCKET__LISTEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102}
4103
4104static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
4105{
4106 int err;
4107 struct inode_security_struct *isec;
4108 struct inode_security_struct *newisec;
4109
Paul Moore253bfae2010-04-22 14:46:19 -04004110 err = sock_has_perm(current, sock->sk, SOCKET__ACCEPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 if (err)
4112 return err;
4113
4114 newisec = SOCK_INODE(newsock)->i_security;
4115
4116 isec = SOCK_INODE(sock)->i_security;
4117 newisec->sclass = isec->sclass;
4118 newisec->sid = isec->sid;
4119 newisec->initialized = 1;
4120
4121 return 0;
4122}
4123
4124static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
Eric Paris828dfe12008-04-17 13:17:49 -04004125 int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126{
Paul Moore253bfae2010-04-22 14:46:19 -04004127 return sock_has_perm(current, sock->sk, SOCKET__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128}
4129
4130static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
4131 int size, int flags)
4132{
Paul Moore253bfae2010-04-22 14:46:19 -04004133 return sock_has_perm(current, sock->sk, SOCKET__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134}
4135
4136static int selinux_socket_getsockname(struct socket *sock)
4137{
Paul Moore253bfae2010-04-22 14:46:19 -04004138 return sock_has_perm(current, sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139}
4140
4141static int selinux_socket_getpeername(struct socket *sock)
4142{
Paul Moore253bfae2010-04-22 14:46:19 -04004143 return sock_has_perm(current, sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144}
4145
Eric Paris828dfe12008-04-17 13:17:49 -04004146static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147{
Paul Mooref8687af2006-10-30 15:22:15 -08004148 int err;
4149
Paul Moore253bfae2010-04-22 14:46:19 -04004150 err = sock_has_perm(current, sock->sk, SOCKET__SETOPT);
Paul Mooref8687af2006-10-30 15:22:15 -08004151 if (err)
4152 return err;
4153
4154 return selinux_netlbl_socket_setsockopt(sock, level, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155}
4156
4157static int selinux_socket_getsockopt(struct socket *sock, int level,
4158 int optname)
4159{
Paul Moore253bfae2010-04-22 14:46:19 -04004160 return sock_has_perm(current, sock->sk, SOCKET__GETOPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161}
4162
4163static int selinux_socket_shutdown(struct socket *sock, int how)
4164{
Paul Moore253bfae2010-04-22 14:46:19 -04004165 return sock_has_perm(current, sock->sk, SOCKET__SHUTDOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166}
4167
David S. Miller3610cda2011-01-05 15:38:53 -08004168static int selinux_socket_unix_stream_connect(struct sock *sock,
4169 struct sock *other,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 struct sock *newsk)
4171{
David S. Miller3610cda2011-01-05 15:38:53 -08004172 struct sk_security_struct *sksec_sock = sock->sk_security;
4173 struct sk_security_struct *sksec_other = other->sk_security;
Paul Moore4d1e2452010-04-22 14:46:18 -04004174 struct sk_security_struct *sksec_new = newsk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004175 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004176 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004177 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 int err;
4179
Thomas Liu2bf49692009-07-14 12:14:09 -04004180 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004181 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004182 ad.u.net = &net;
4183 ad.u.net->sk = other;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184
Paul Moore4d1e2452010-04-22 14:46:18 -04004185 err = avc_has_perm(sksec_sock->sid, sksec_other->sid,
4186 sksec_other->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 UNIX_STREAM_SOCKET__CONNECTTO, &ad);
4188 if (err)
4189 return err;
4190
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 /* server child socket */
Paul Moore4d1e2452010-04-22 14:46:18 -04004192 sksec_new->peer_sid = sksec_sock->sid;
4193 err = security_sid_mls_copy(sksec_other->sid, sksec_sock->sid,
4194 &sksec_new->sid);
4195 if (err)
4196 return err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004197
Paul Moore4d1e2452010-04-22 14:46:18 -04004198 /* connecting socket */
4199 sksec_sock->peer_sid = sksec_new->sid;
4200
4201 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202}
4203
4204static int selinux_socket_unix_may_send(struct socket *sock,
4205 struct socket *other)
4206{
Paul Moore253bfae2010-04-22 14:46:19 -04004207 struct sk_security_struct *ssec = sock->sk->sk_security;
4208 struct sk_security_struct *osec = other->sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004209 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004210 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004211 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212
Thomas Liu2bf49692009-07-14 12:14:09 -04004213 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004214 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004215 ad.u.net = &net;
4216 ad.u.net->sk = other->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217
Paul Moore253bfae2010-04-22 14:46:19 -04004218 return avc_has_perm(ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
4219 &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220}
4221
Paul Mooreeffad8d2008-01-29 08:49:27 -05004222static int selinux_inet_sys_rcv_skb(int ifindex, char *addrp, u16 family,
4223 u32 peer_sid,
Thomas Liu2bf49692009-07-14 12:14:09 -04004224 struct common_audit_data *ad)
Paul Mooreeffad8d2008-01-29 08:49:27 -05004225{
4226 int err;
4227 u32 if_sid;
4228 u32 node_sid;
4229
4230 err = sel_netif_sid(ifindex, &if_sid);
4231 if (err)
4232 return err;
4233 err = avc_has_perm(peer_sid, if_sid,
4234 SECCLASS_NETIF, NETIF__INGRESS, ad);
4235 if (err)
4236 return err;
4237
4238 err = sel_netnode_sid(addrp, family, &node_sid);
4239 if (err)
4240 return err;
4241 return avc_has_perm(peer_sid, node_sid,
4242 SECCLASS_NODE, NODE__RECVFROM, ad);
4243}
4244
Paul Moore220deb92008-01-29 08:38:23 -05004245static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
Paul Moored8395c82008-10-10 10:16:30 -04004246 u16 family)
Paul Moore220deb92008-01-29 08:38:23 -05004247{
Paul Moore277d3422008-12-31 12:54:11 -05004248 int err = 0;
Paul Moore220deb92008-01-29 08:38:23 -05004249 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004250 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004251 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004252 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004253 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04004254 char *addrp;
4255
Thomas Liu2bf49692009-07-14 12:14:09 -04004256 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004257 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004258 ad.u.net = &net;
4259 ad.u.net->netif = skb->skb_iif;
4260 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004261 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4262 if (err)
4263 return err;
Paul Moore220deb92008-01-29 08:38:23 -05004264
Paul Moore58bfbb52009-03-27 17:10:41 -04004265 if (selinux_secmark_enabled()) {
Paul Moore220deb92008-01-29 08:38:23 -05004266 err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Moored8395c82008-10-10 10:16:30 -04004267 PACKET__RECV, &ad);
Paul Moore58bfbb52009-03-27 17:10:41 -04004268 if (err)
4269 return err;
4270 }
Paul Moore220deb92008-01-29 08:38:23 -05004271
Steffen Klassertb9679a72011-02-23 12:55:21 +01004272 err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
4273 if (err)
4274 return err;
4275 err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004276
James Morris4e5ab4c2006-06-09 00:33:33 -07004277 return err;
4278}
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004279
James Morris4e5ab4c2006-06-09 00:33:33 -07004280static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
4281{
Paul Moore220deb92008-01-29 08:38:23 -05004282 int err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004283 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004284 u16 family = sk->sk_family;
4285 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004286 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004287 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004288 struct lsm_network_audit net = {0,};
Paul Moore220deb92008-01-29 08:38:23 -05004289 char *addrp;
Paul Moored8395c82008-10-10 10:16:30 -04004290 u8 secmark_active;
4291 u8 peerlbl_active;
James Morris4e5ab4c2006-06-09 00:33:33 -07004292
James Morris4e5ab4c2006-06-09 00:33:33 -07004293 if (family != PF_INET && family != PF_INET6)
Paul Moore220deb92008-01-29 08:38:23 -05004294 return 0;
James Morris4e5ab4c2006-06-09 00:33:33 -07004295
4296 /* Handle mapped IPv4 packets arriving via IPv6 sockets */
Al Viro87fcd702006-12-04 22:00:55 +00004297 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
James Morris4e5ab4c2006-06-09 00:33:33 -07004298 family = PF_INET;
4299
Paul Moored8395c82008-10-10 10:16:30 -04004300 /* If any sort of compatibility mode is enabled then handoff processing
4301 * to the selinux_sock_rcv_skb_compat() function to deal with the
4302 * special handling. We do this in an attempt to keep this function
4303 * as fast and as clean as possible. */
Paul Moore58bfbb52009-03-27 17:10:41 -04004304 if (!selinux_policycap_netpeer)
Paul Moored8395c82008-10-10 10:16:30 -04004305 return selinux_sock_rcv_skb_compat(sk, skb, family);
4306
4307 secmark_active = selinux_secmark_enabled();
4308 peerlbl_active = netlbl_enabled() || selinux_xfrm_enabled();
4309 if (!secmark_active && !peerlbl_active)
4310 return 0;
4311
Thomas Liu2bf49692009-07-14 12:14:09 -04004312 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004313 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004314 ad.u.net = &net;
4315 ad.u.net->netif = skb->skb_iif;
4316 ad.u.net->family = family;
Paul Moore224dfbd2008-01-29 08:38:13 -05004317 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
James Morris4e5ab4c2006-06-09 00:33:33 -07004318 if (err)
Paul Moore220deb92008-01-29 08:38:23 -05004319 return err;
James Morris4e5ab4c2006-06-09 00:33:33 -07004320
Paul Moored8395c82008-10-10 10:16:30 -04004321 if (peerlbl_active) {
Paul Moored621d352008-01-29 08:43:36 -05004322 u32 peer_sid;
4323
4324 err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
4325 if (err)
4326 return err;
Eric Dumazet8964be42009-11-20 15:35:04 -08004327 err = selinux_inet_sys_rcv_skb(skb->skb_iif, addrp, family,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004328 peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04004329 if (err) {
4330 selinux_netlbl_err(skb, err, 0);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004331 return err;
Paul Mooredfaebe92008-10-10 10:16:31 -04004332 }
Paul Moored621d352008-01-29 08:43:36 -05004333 err = avc_has_perm(sk_sid, peer_sid, SECCLASS_PEER,
4334 PEER__RECV, &ad);
Chad Hanson351381d2013-12-23 17:45:01 -05004335 if (err) {
Paul Mooredfaebe92008-10-10 10:16:31 -04004336 selinux_netlbl_err(skb, err, 0);
Chad Hanson351381d2013-12-23 17:45:01 -05004337 return err;
4338 }
Paul Moored621d352008-01-29 08:43:36 -05004339 }
4340
Paul Moored8395c82008-10-10 10:16:30 -04004341 if (secmark_active) {
Paul Mooreeffad8d2008-01-29 08:49:27 -05004342 err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
4343 PACKET__RECV, &ad);
4344 if (err)
4345 return err;
4346 }
4347
Paul Moored621d352008-01-29 08:43:36 -05004348 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349}
4350
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004351static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
4352 int __user *optlen, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353{
4354 int err = 0;
4355 char *scontext;
4356 u32 scontext_len;
Paul Moore253bfae2010-04-22 14:46:19 -04004357 struct sk_security_struct *sksec = sock->sk->sk_security;
Paul Moore3de4bab2006-11-17 17:38:54 -05004358 u32 peer_sid = SECSID_NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359
Paul Moore253bfae2010-04-22 14:46:19 -04004360 if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
4361 sksec->sclass == SECCLASS_TCP_SOCKET)
Eric Parisdd3e7832010-04-07 15:08:46 -04004362 peer_sid = sksec->peer_sid;
Paul Moore253bfae2010-04-22 14:46:19 -04004363 if (peer_sid == SECSID_NULL)
4364 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004366 err = security_sid_to_context(peer_sid, &scontext, &scontext_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367 if (err)
Paul Moore253bfae2010-04-22 14:46:19 -04004368 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369
4370 if (scontext_len > len) {
4371 err = -ERANGE;
4372 goto out_len;
4373 }
4374
4375 if (copy_to_user(optval, scontext, scontext_len))
4376 err = -EFAULT;
4377
4378out_len:
4379 if (put_user(scontext_len, optlen))
4380 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 kfree(scontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 return err;
4383}
4384
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004385static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004386{
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004387 u32 peer_secid = SECSID_NULL;
Paul Moore75e22912008-01-29 08:38:04 -05004388 u16 family;
Catherine Zhang877ce7c2006-06-29 12:27:47 -07004389
Paul Mooreaa862902008-10-10 10:16:29 -04004390 if (skb && skb->protocol == htons(ETH_P_IP))
4391 family = PF_INET;
4392 else if (skb && skb->protocol == htons(ETH_P_IPV6))
4393 family = PF_INET6;
4394 else if (sock)
Paul Moore75e22912008-01-29 08:38:04 -05004395 family = sock->sk->sk_family;
Paul Moore75e22912008-01-29 08:38:04 -05004396 else
4397 goto out;
4398
4399 if (sock && family == PF_UNIX)
Ahmed S. Darwish713a04a2008-03-01 21:52:30 +02004400 selinux_inode_getsecid(SOCK_INODE(sock), &peer_secid);
Paul Moore3de4bab2006-11-17 17:38:54 -05004401 else if (skb)
Paul Moore220deb92008-01-29 08:38:23 -05004402 selinux_skb_peerlbl_sid(skb, family, &peer_secid);
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004403
Paul Moore75e22912008-01-29 08:38:04 -05004404out:
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004405 *secid = peer_secid;
Paul Moore75e22912008-01-29 08:38:04 -05004406 if (peer_secid == SECSID_NULL)
4407 return -EINVAL;
4408 return 0;
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004409}
4410
Al Viro7d877f32005-10-21 03:20:43 -04004411static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412{
Paul Moore84914b72010-04-22 14:46:18 -04004413 struct sk_security_struct *sksec;
4414
4415 sksec = kzalloc(sizeof(*sksec), priority);
4416 if (!sksec)
4417 return -ENOMEM;
4418
4419 sksec->peer_sid = SECINITSID_UNLABELED;
4420 sksec->sid = SECINITSID_UNLABELED;
4421 selinux_netlbl_sk_security_reset(sksec);
4422 sk->sk_security = sksec;
4423
4424 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425}
4426
4427static void selinux_sk_free_security(struct sock *sk)
4428{
Paul Moore84914b72010-04-22 14:46:18 -04004429 struct sk_security_struct *sksec = sk->sk_security;
4430
4431 sk->sk_security = NULL;
4432 selinux_netlbl_sk_security_free(sksec);
4433 kfree(sksec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434}
4435
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004436static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
4437{
Eric Parisdd3e7832010-04-07 15:08:46 -04004438 struct sk_security_struct *sksec = sk->sk_security;
4439 struct sk_security_struct *newsksec = newsk->sk_security;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004440
Eric Parisdd3e7832010-04-07 15:08:46 -04004441 newsksec->sid = sksec->sid;
4442 newsksec->peer_sid = sksec->peer_sid;
4443 newsksec->sclass = sksec->sclass;
Paul Moore99f59ed2006-08-29 17:53:48 -07004444
Eric Parisdd3e7832010-04-07 15:08:46 -04004445 selinux_netlbl_sk_security_reset(newsksec);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004446}
4447
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07004448static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004449{
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004450 if (!sk)
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07004451 *secid = SECINITSID_ANY_SOCKET;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004452 else {
4453 struct sk_security_struct *sksec = sk->sk_security;
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004454
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07004455 *secid = sksec->sid;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004456 }
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004457}
4458
Eric Paris828dfe12008-04-17 13:17:49 -04004459static void selinux_sock_graft(struct sock *sk, struct socket *parent)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004460{
4461 struct inode_security_struct *isec = SOCK_INODE(parent)->i_security;
4462 struct sk_security_struct *sksec = sk->sk_security;
4463
David Woodhouse2148ccc2006-09-29 15:50:25 -07004464 if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
4465 sk->sk_family == PF_UNIX)
4466 isec->sid = sksec->sid;
Paul Moore220deb92008-01-29 08:38:23 -05004467 sksec->sclass = isec->sclass;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004468}
4469
Adrian Bunk9a673e52006-08-15 00:03:53 -07004470static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4471 struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004472{
4473 struct sk_security_struct *sksec = sk->sk_security;
4474 int err;
Paul Mooreaa862902008-10-10 10:16:29 -04004475 u16 family = sk->sk_family;
Paul Moore2ea04e52013-12-04 16:10:51 -05004476 u32 connsid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004477 u32 peersid;
4478
Paul Mooreaa862902008-10-10 10:16:29 -04004479 /* handle mapped IPv4 packets arriving via IPv6 sockets */
4480 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4481 family = PF_INET;
4482
4483 err = selinux_skb_peerlbl_sid(skb, family, &peersid);
Paul Moore220deb92008-01-29 08:38:23 -05004484 if (err)
4485 return err;
Paul Moore2ea04e52013-12-04 16:10:51 -05004486 err = selinux_conn_sid(sksec->sid, peersid, &connsid);
4487 if (err)
4488 return err;
4489 req->secid = connsid;
4490 req->peer_secid = peersid;
Venkat Yekkiralaa51c64f2006-07-27 22:01:34 -07004491
Paul Moore389fb802009-03-27 17:10:34 -04004492 return selinux_netlbl_inet_conn_request(req, family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004493}
4494
Adrian Bunk9a673e52006-08-15 00:03:53 -07004495static void selinux_inet_csk_clone(struct sock *newsk,
4496 const struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004497{
4498 struct sk_security_struct *newsksec = newsk->sk_security;
4499
4500 newsksec->sid = req->secid;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004501 newsksec->peer_sid = req->peer_secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004502 /* NOTE: Ideally, we should also get the isec->sid for the
4503 new socket in sync, but we don't have the isec available yet.
4504 So we will wait until sock_graft to do it, by which
4505 time it will have been created and available. */
Paul Moore99f59ed2006-08-29 17:53:48 -07004506
Paul Moore9f2ad662006-11-17 17:38:53 -05004507 /* We don't need to take any sort of lock here as we are the only
4508 * thread with access to newsksec */
Paul Moore389fb802009-03-27 17:10:34 -04004509 selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004510}
4511
Paul Moore014ab192008-10-10 10:16:33 -04004512static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004513{
Paul Mooreaa862902008-10-10 10:16:29 -04004514 u16 family = sk->sk_family;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004515 struct sk_security_struct *sksec = sk->sk_security;
4516
Paul Mooreaa862902008-10-10 10:16:29 -04004517 /* handle mapped IPv4 packets arriving via IPv6 sockets */
4518 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4519 family = PF_INET;
4520
4521 selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004522}
4523
Eric Paris2606fd12010-10-13 16:24:41 -04004524static int selinux_secmark_relabel_packet(u32 sid)
4525{
4526 const struct task_security_struct *__tsec;
4527 u32 tsid;
4528
4529 __tsec = current_security();
4530 tsid = __tsec->sid;
4531
4532 return avc_has_perm(tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO, NULL);
4533}
4534
4535static void selinux_secmark_refcount_inc(void)
4536{
4537 atomic_inc(&selinux_secmark_refcount);
4538}
4539
4540static void selinux_secmark_refcount_dec(void)
4541{
4542 atomic_dec(&selinux_secmark_refcount);
4543}
4544
Adrian Bunk9a673e52006-08-15 00:03:53 -07004545static void selinux_req_classify_flow(const struct request_sock *req,
4546 struct flowi *fl)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004547{
David S. Miller1d28f422011-03-12 00:29:39 -05004548 fl->flowi_secid = req->secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004549}
4550
Paul Mooreed6d76e2009-08-28 18:12:49 -04004551static int selinux_tun_dev_create(void)
4552{
4553 u32 sid = current_sid();
4554
4555 /* we aren't taking into account the "sockcreate" SID since the socket
4556 * that is being created here is not a socket in the traditional sense,
4557 * instead it is a private sock, accessible only to the kernel, and
4558 * representing a wide range of network traffic spanning multiple
4559 * connections unlike traditional sockets - check the TUN driver to
4560 * get a better understanding of why this socket is special */
4561
4562 return avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
4563 NULL);
4564}
4565
4566static void selinux_tun_dev_post_create(struct sock *sk)
4567{
4568 struct sk_security_struct *sksec = sk->sk_security;
4569
4570 /* we don't currently perform any NetLabel based labeling here and it
4571 * isn't clear that we would want to do so anyway; while we could apply
4572 * labeling without the support of the TUN user the resulting labeled
4573 * traffic from the other end of the connection would almost certainly
4574 * cause confusion to the TUN user that had no idea network labeling
4575 * protocols were being used */
4576
4577 /* see the comments in selinux_tun_dev_create() about why we don't use
4578 * the sockcreate SID here */
4579
4580 sksec->sid = current_sid();
4581 sksec->sclass = SECCLASS_TUN_SOCKET;
4582}
4583
4584static int selinux_tun_dev_attach(struct sock *sk)
4585{
4586 struct sk_security_struct *sksec = sk->sk_security;
4587 u32 sid = current_sid();
4588 int err;
4589
4590 err = avc_has_perm(sid, sksec->sid, SECCLASS_TUN_SOCKET,
4591 TUN_SOCKET__RELABELFROM, NULL);
4592 if (err)
4593 return err;
4594 err = avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET,
4595 TUN_SOCKET__RELABELTO, NULL);
4596 if (err)
4597 return err;
4598
4599 sksec->sid = sid;
4600
4601 return 0;
4602}
4603
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
4605{
4606 int err = 0;
4607 u32 perm;
4608 struct nlmsghdr *nlh;
Paul Moore253bfae2010-04-22 14:46:19 -04004609 struct sk_security_struct *sksec = sk->sk_security;
Eric Paris828dfe12008-04-17 13:17:49 -04004610
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 if (skb->len < NLMSG_SPACE(0)) {
4612 err = -EINVAL;
4613 goto out;
4614 }
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -07004615 nlh = nlmsg_hdr(skb);
Eric Paris828dfe12008-04-17 13:17:49 -04004616
Paul Moore253bfae2010-04-22 14:46:19 -04004617 err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 if (err) {
4619 if (err == -EINVAL) {
David Woodhouse9ad9ad32005-06-22 15:04:33 +01004620 audit_log(current->audit_context, GFP_KERNEL, AUDIT_SELINUX_ERR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 "SELinux: unrecognized netlink message"
4622 " type=%hu for sclass=%hu\n",
Paul Moore253bfae2010-04-22 14:46:19 -04004623 nlh->nlmsg_type, sksec->sclass);
Eric Paris39c9aed2008-11-05 09:34:42 -05004624 if (!selinux_enforcing || security_get_allow_unknown())
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 err = 0;
4626 }
4627
4628 /* Ignore */
4629 if (err == -ENOENT)
4630 err = 0;
4631 goto out;
4632 }
4633
Paul Moore253bfae2010-04-22 14:46:19 -04004634 err = sock_has_perm(current, sk, perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635out:
4636 return err;
4637}
4638
4639#ifdef CONFIG_NETFILTER
4640
Paul Mooreeffad8d2008-01-29 08:49:27 -05004641static unsigned int selinux_ip_forward(struct sk_buff *skb, int ifindex,
4642 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643{
Paul Mooredfaebe92008-10-10 10:16:31 -04004644 int err;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004645 char *addrp;
4646 u32 peer_sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004647 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004648 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004649 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05004650 u8 secmark_active;
Paul Moore948bf852008-10-10 10:16:32 -04004651 u8 netlbl_active;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004652 u8 peerlbl_active;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004653
Paul Mooreeffad8d2008-01-29 08:49:27 -05004654 if (!selinux_policycap_netpeer)
4655 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004656
Paul Mooreeffad8d2008-01-29 08:49:27 -05004657 secmark_active = selinux_secmark_enabled();
Paul Moore948bf852008-10-10 10:16:32 -04004658 netlbl_active = netlbl_enabled();
4659 peerlbl_active = netlbl_active || selinux_xfrm_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05004660 if (!secmark_active && !peerlbl_active)
4661 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004662
Paul Moored8395c82008-10-10 10:16:30 -04004663 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
4664 return NF_DROP;
4665
Thomas Liu2bf49692009-07-14 12:14:09 -04004666 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004667 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004668 ad.u.net = &net;
4669 ad.u.net->netif = ifindex;
4670 ad.u.net->family = family;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004671 if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
4672 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673
Paul Mooredfaebe92008-10-10 10:16:31 -04004674 if (peerlbl_active) {
4675 err = selinux_inet_sys_rcv_skb(ifindex, addrp, family,
4676 peer_sid, &ad);
4677 if (err) {
4678 selinux_netlbl_err(skb, err, 1);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004679 return NF_DROP;
Paul Mooredfaebe92008-10-10 10:16:31 -04004680 }
4681 }
Paul Mooreeffad8d2008-01-29 08:49:27 -05004682
4683 if (secmark_active)
4684 if (avc_has_perm(peer_sid, skb->secmark,
4685 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
4686 return NF_DROP;
4687
Paul Moore948bf852008-10-10 10:16:32 -04004688 if (netlbl_active)
4689 /* we do this in the FORWARD path and not the POST_ROUTING
4690 * path because we want to make sure we apply the necessary
4691 * labeling before IPsec is applied so we can leverage AH
4692 * protection */
4693 if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
4694 return NF_DROP;
4695
Paul Mooreeffad8d2008-01-29 08:49:27 -05004696 return NF_ACCEPT;
4697}
4698
4699static unsigned int selinux_ipv4_forward(unsigned int hooknum,
4700 struct sk_buff *skb,
4701 const struct net_device *in,
4702 const struct net_device *out,
4703 int (*okfn)(struct sk_buff *))
4704{
4705 return selinux_ip_forward(skb, in->ifindex, PF_INET);
4706}
4707
4708#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
4709static unsigned int selinux_ipv6_forward(unsigned int hooknum,
4710 struct sk_buff *skb,
4711 const struct net_device *in,
4712 const struct net_device *out,
4713 int (*okfn)(struct sk_buff *))
4714{
4715 return selinux_ip_forward(skb, in->ifindex, PF_INET6);
4716}
4717#endif /* IPV6 */
4718
Paul Moore948bf852008-10-10 10:16:32 -04004719static unsigned int selinux_ip_output(struct sk_buff *skb,
4720 u16 family)
4721{
Paul Moore1c5d9d12013-12-04 16:10:45 -05004722 struct sock *sk;
Paul Moore948bf852008-10-10 10:16:32 -04004723 u32 sid;
4724
4725 if (!netlbl_enabled())
4726 return NF_ACCEPT;
4727
4728 /* we do this in the LOCAL_OUT path and not the POST_ROUTING path
4729 * because we want to make sure we apply the necessary labeling
4730 * before IPsec is applied so we can leverage AH protection */
Paul Moore1c5d9d12013-12-04 16:10:45 -05004731 sk = skb->sk;
4732 if (sk) {
4733 struct sk_security_struct *sksec;
4734
4735 if (sk->sk_state == TCP_LISTEN)
4736 /* if the socket is the listening state then this
4737 * packet is a SYN-ACK packet which means it needs to
4738 * be labeled based on the connection/request_sock and
4739 * not the parent socket. unfortunately, we can't
4740 * lookup the request_sock yet as it isn't queued on
4741 * the parent socket until after the SYN-ACK is sent.
4742 * the "solution" is to simply pass the packet as-is
4743 * as any IP option based labeling should be copied
4744 * from the initial connection request (in the IP
4745 * layer). it is far from ideal, but until we get a
4746 * security label in the packet itself this is the
4747 * best we can do. */
4748 return NF_ACCEPT;
4749
4750 /* standard practice, label using the parent socket */
4751 sksec = sk->sk_security;
Paul Moore948bf852008-10-10 10:16:32 -04004752 sid = sksec->sid;
4753 } else
4754 sid = SECINITSID_KERNEL;
4755 if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
4756 return NF_DROP;
4757
4758 return NF_ACCEPT;
4759}
4760
4761static unsigned int selinux_ipv4_output(unsigned int hooknum,
4762 struct sk_buff *skb,
4763 const struct net_device *in,
4764 const struct net_device *out,
4765 int (*okfn)(struct sk_buff *))
4766{
4767 return selinux_ip_output(skb, PF_INET);
4768}
4769
Paul Mooreeffad8d2008-01-29 08:49:27 -05004770static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
4771 int ifindex,
Paul Moored8395c82008-10-10 10:16:30 -04004772 u16 family)
James Morris4e5ab4c2006-06-09 00:33:33 -07004773{
Paul Mooreeffad8d2008-01-29 08:49:27 -05004774 struct sock *sk = skb->sk;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004775 struct sk_security_struct *sksec;
Thomas Liu2bf49692009-07-14 12:14:09 -04004776 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004777 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004778 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04004779 char *addrp;
4780 u8 proto;
James Morris4e5ab4c2006-06-09 00:33:33 -07004781
Paul Mooreeffad8d2008-01-29 08:49:27 -05004782 if (sk == NULL)
4783 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004784 sksec = sk->sk_security;
James Morris4e5ab4c2006-06-09 00:33:33 -07004785
Thomas Liu2bf49692009-07-14 12:14:09 -04004786 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004787 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004788 ad.u.net = &net;
4789 ad.u.net->netif = ifindex;
4790 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004791 if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
4792 return NF_DROP;
4793
Paul Moore58bfbb52009-03-27 17:10:41 -04004794 if (selinux_secmark_enabled())
Paul Mooreeffad8d2008-01-29 08:49:27 -05004795 if (avc_has_perm(sksec->sid, skb->secmark,
Paul Moored8395c82008-10-10 10:16:30 -04004796 SECCLASS_PACKET, PACKET__SEND, &ad))
Eric Paris2fe66ec2010-11-23 06:28:08 +00004797 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07004798
Steffen Klassertb9679a72011-02-23 12:55:21 +01004799 if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
4800 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07004801
Paul Mooreeffad8d2008-01-29 08:49:27 -05004802 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803}
4804
Paul Mooreeffad8d2008-01-29 08:49:27 -05004805static unsigned int selinux_ip_postroute(struct sk_buff *skb, int ifindex,
4806 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807{
Paul Mooreeffad8d2008-01-29 08:49:27 -05004808 u32 secmark_perm;
4809 u32 peer_sid;
4810 struct sock *sk;
Thomas Liu2bf49692009-07-14 12:14:09 -04004811 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004812 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004813 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05004814 char *addrp;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004815 u8 secmark_active;
4816 u8 peerlbl_active;
4817
Paul Mooreeffad8d2008-01-29 08:49:27 -05004818 /* If any sort of compatibility mode is enabled then handoff processing
4819 * to the selinux_ip_postroute_compat() function to deal with the
4820 * special handling. We do this in an attempt to keep this function
4821 * as fast and as clean as possible. */
Paul Moore58bfbb52009-03-27 17:10:41 -04004822 if (!selinux_policycap_netpeer)
Paul Moored8395c82008-10-10 10:16:30 -04004823 return selinux_ip_postroute_compat(skb, ifindex, family);
Paul Moore420cc6d2013-12-10 14:58:01 -05004824
Paul Mooreeffad8d2008-01-29 08:49:27 -05004825 secmark_active = selinux_secmark_enabled();
4826 peerlbl_active = netlbl_enabled() || selinux_xfrm_enabled();
4827 if (!secmark_active && !peerlbl_active)
4828 return NF_ACCEPT;
4829
Paul Mooreeffad8d2008-01-29 08:49:27 -05004830 sk = skb->sk;
Paul Moore420cc6d2013-12-10 14:58:01 -05004831
4832#ifdef CONFIG_XFRM
4833 /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
4834 * packet transformation so allow the packet to pass without any checks
4835 * since we'll have another chance to perform access control checks
4836 * when the packet is on it's final way out.
4837 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
4838 * is NULL, in this case go ahead and apply access control.
4839 * is NULL, in this case go ahead and apply access control.
4840 * NOTE: if this is a local socket (skb->sk != NULL) that is in the
4841 * TCP listening state we cannot wait until the XFRM processing
4842 * is done as we will miss out on the SA label if we do;
4843 * unfortunately, this means more work, but it is only once per
4844 * connection. */
4845 if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL &&
4846 !(sk != NULL && sk->sk_state == TCP_LISTEN))
4847 return NF_ACCEPT;
4848#endif
4849
Paul Moored8395c82008-10-10 10:16:30 -04004850 if (sk == NULL) {
Paul Moore2ea04e52013-12-04 16:10:51 -05004851 /* Without an associated socket the packet is either coming
4852 * from the kernel or it is being forwarded; check the packet
4853 * to determine which and if the packet is being forwarded
4854 * query the packet directly to determine the security label. */
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01004855 if (skb->skb_iif) {
4856 secmark_perm = PACKET__FORWARD_OUT;
Paul Moored8395c82008-10-10 10:16:30 -04004857 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00004858 return NF_DROP;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01004859 } else {
4860 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04004861 peer_sid = SECINITSID_KERNEL;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01004862 }
Paul Moore2ea04e52013-12-04 16:10:51 -05004863 } else if (sk->sk_state == TCP_LISTEN) {
4864 /* Locally generated packet but the associated socket is in the
4865 * listening state which means this is a SYN-ACK packet. In
4866 * this particular case the correct security label is assigned
4867 * to the connection/request_sock but unfortunately we can't
4868 * query the request_sock as it isn't queued on the parent
4869 * socket until after the SYN-ACK packet is sent; the only
4870 * viable choice is to regenerate the label like we do in
4871 * selinux_inet_conn_request(). See also selinux_ip_output()
4872 * for similar problems. */
4873 u32 skb_sid;
4874 struct sk_security_struct *sksec = sk->sk_security;
4875 if (selinux_skb_peerlbl_sid(skb, family, &skb_sid))
4876 return NF_DROP;
Paul Moore420cc6d2013-12-10 14:58:01 -05004877 /* At this point, if the returned skb peerlbl is SECSID_NULL
4878 * and the packet has been through at least one XFRM
4879 * transformation then we must be dealing with the "final"
4880 * form of labeled IPsec packet; since we've already applied
4881 * all of our access controls on this packet we can safely
4882 * pass the packet. */
4883 if (skb_sid == SECSID_NULL) {
4884 switch (family) {
4885 case PF_INET:
4886 if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
4887 return NF_ACCEPT;
4888 break;
4889 case PF_INET6:
4890 if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED)
4891 return NF_ACCEPT;
4892 default:
4893 return NF_DROP_ERR(-ECONNREFUSED);
4894 }
4895 }
Paul Moore2ea04e52013-12-04 16:10:51 -05004896 if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid))
4897 return NF_DROP;
4898 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04004899 } else {
Paul Moore2ea04e52013-12-04 16:10:51 -05004900 /* Locally generated packet, fetch the security label from the
4901 * associated socket. */
Paul Mooreeffad8d2008-01-29 08:49:27 -05004902 struct sk_security_struct *sksec = sk->sk_security;
4903 peer_sid = sksec->sid;
4904 secmark_perm = PACKET__SEND;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004905 }
4906
Thomas Liu2bf49692009-07-14 12:14:09 -04004907 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004908 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004909 ad.u.net = &net;
4910 ad.u.net->netif = ifindex;
4911 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004912 if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
Eric Paris04f6d702010-11-23 06:28:02 +00004913 return NF_DROP;
Paul Moored8395c82008-10-10 10:16:30 -04004914
Paul Mooreeffad8d2008-01-29 08:49:27 -05004915 if (secmark_active)
4916 if (avc_has_perm(peer_sid, skb->secmark,
4917 SECCLASS_PACKET, secmark_perm, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00004918 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004919
4920 if (peerlbl_active) {
4921 u32 if_sid;
4922 u32 node_sid;
4923
4924 if (sel_netif_sid(ifindex, &if_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00004925 return NF_DROP;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004926 if (avc_has_perm(peer_sid, if_sid,
4927 SECCLASS_NETIF, NETIF__EGRESS, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00004928 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004929
4930 if (sel_netnode_sid(addrp, family, &node_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00004931 return NF_DROP;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004932 if (avc_has_perm(peer_sid, node_sid,
4933 SECCLASS_NODE, NODE__SENDTO, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00004934 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004935 }
4936
4937 return NF_ACCEPT;
4938}
4939
4940static unsigned int selinux_ipv4_postroute(unsigned int hooknum,
4941 struct sk_buff *skb,
4942 const struct net_device *in,
4943 const struct net_device *out,
4944 int (*okfn)(struct sk_buff *))
4945{
4946 return selinux_ip_postroute(skb, out->ifindex, PF_INET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947}
4948
4949#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Paul Mooreeffad8d2008-01-29 08:49:27 -05004950static unsigned int selinux_ipv6_postroute(unsigned int hooknum,
4951 struct sk_buff *skb,
4952 const struct net_device *in,
4953 const struct net_device *out,
4954 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955{
Paul Mooreeffad8d2008-01-29 08:49:27 -05004956 return selinux_ip_postroute(skb, out->ifindex, PF_INET6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958#endif /* IPV6 */
4959
4960#endif /* CONFIG_NETFILTER */
4961
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
4963{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964 int err;
4965
Eric Paris200ac532009-02-12 15:01:04 -05004966 err = cap_netlink_send(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 if (err)
4968 return err;
4969
Stephen Smalley941fc5b2009-10-01 14:48:23 -04004970 return selinux_nlmsg_perm(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971}
4972
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973static int ipc_alloc_security(struct task_struct *task,
4974 struct kern_ipc_perm *perm,
4975 u16 sclass)
4976{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 struct ipc_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +11004978 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979
James Morris89d155e2005-10-30 14:59:21 -08004980 isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 if (!isec)
4982 return -ENOMEM;
4983
David Howells275bb412008-11-14 10:39:19 +11004984 sid = task_sid(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985 isec->sclass = sclass;
David Howells275bb412008-11-14 10:39:19 +11004986 isec->sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987 perm->security = isec;
4988
4989 return 0;
4990}
4991
4992static void ipc_free_security(struct kern_ipc_perm *perm)
4993{
4994 struct ipc_security_struct *isec = perm->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 perm->security = NULL;
4996 kfree(isec);
4997}
4998
4999static int msg_msg_alloc_security(struct msg_msg *msg)
5000{
5001 struct msg_security_struct *msec;
5002
James Morris89d155e2005-10-30 14:59:21 -08005003 msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004 if (!msec)
5005 return -ENOMEM;
5006
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007 msec->sid = SECINITSID_UNLABELED;
5008 msg->security = msec;
5009
5010 return 0;
5011}
5012
5013static void msg_msg_free_security(struct msg_msg *msg)
5014{
5015 struct msg_security_struct *msec = msg->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016
5017 msg->security = NULL;
5018 kfree(msec);
5019}
5020
5021static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
Stephen Smalley6af963f2005-05-01 08:58:39 -07005022 u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005025 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005026 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005027 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029 isec = ipc_perms->security;
5030
Thomas Liu2bf49692009-07-14 12:14:09 -04005031 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005032 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 ad.u.ipc_id = ipc_perms->key;
5034
David Howells275bb412008-11-14 10:39:19 +11005035 return avc_has_perm(sid, isec->sid, isec->sclass, perms, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036}
5037
5038static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
5039{
5040 return msg_msg_alloc_security(msg);
5041}
5042
5043static void selinux_msg_msg_free_security(struct msg_msg *msg)
5044{
5045 msg_msg_free_security(msg);
5046}
5047
5048/* message queue security operations */
5049static int selinux_msg_queue_alloc_security(struct msg_queue *msq)
5050{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005052 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005053 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005054 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055 int rc;
5056
5057 rc = ipc_alloc_security(current, &msq->q_perm, SECCLASS_MSGQ);
5058 if (rc)
5059 return rc;
5060
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061 isec = msq->q_perm.security;
5062
Thomas Liu2bf49692009-07-14 12:14:09 -04005063 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005064 ad.selinux_audit_data = &sad;
Eric Paris828dfe12008-04-17 13:17:49 -04005065 ad.u.ipc_id = msq->q_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066
David Howells275bb412008-11-14 10:39:19 +11005067 rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068 MSGQ__CREATE, &ad);
5069 if (rc) {
5070 ipc_free_security(&msq->q_perm);
5071 return rc;
5072 }
5073 return 0;
5074}
5075
5076static void selinux_msg_queue_free_security(struct msg_queue *msq)
5077{
5078 ipc_free_security(&msq->q_perm);
5079}
5080
5081static int selinux_msg_queue_associate(struct msg_queue *msq, int msqflg)
5082{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005084 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005085 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005086 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088 isec = msq->q_perm.security;
5089
Thomas Liu2bf49692009-07-14 12:14:09 -04005090 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005091 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092 ad.u.ipc_id = msq->q_perm.key;
5093
David Howells275bb412008-11-14 10:39:19 +11005094 return avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 MSGQ__ASSOCIATE, &ad);
5096}
5097
5098static int selinux_msg_queue_msgctl(struct msg_queue *msq, int cmd)
5099{
5100 int err;
5101 int perms;
5102
Eric Paris828dfe12008-04-17 13:17:49 -04005103 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104 case IPC_INFO:
5105 case MSG_INFO:
5106 /* No specific object, just general system-wide information. */
5107 return task_has_system(current, SYSTEM__IPC_INFO);
5108 case IPC_STAT:
5109 case MSG_STAT:
5110 perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
5111 break;
5112 case IPC_SET:
5113 perms = MSGQ__SETATTR;
5114 break;
5115 case IPC_RMID:
5116 perms = MSGQ__DESTROY;
5117 break;
5118 default:
5119 return 0;
5120 }
5121
Stephen Smalley6af963f2005-05-01 08:58:39 -07005122 err = ipc_has_perm(&msq->q_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 return err;
5124}
5125
5126static int selinux_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg, int msqflg)
5127{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128 struct ipc_security_struct *isec;
5129 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005130 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005131 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005132 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133 int rc;
5134
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135 isec = msq->q_perm.security;
5136 msec = msg->security;
5137
5138 /*
5139 * First time through, need to assign label to the message
5140 */
5141 if (msec->sid == SECINITSID_UNLABELED) {
5142 /*
5143 * Compute new sid based on current process and
5144 * message queue this message will be stored in
5145 */
David Howells275bb412008-11-14 10:39:19 +11005146 rc = security_transition_sid(sid, isec->sid, SECCLASS_MSG,
Eric Paris652bb9b2011-02-01 11:05:40 -05005147 NULL, &msec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148 if (rc)
5149 return rc;
5150 }
5151
Thomas Liu2bf49692009-07-14 12:14:09 -04005152 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005153 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154 ad.u.ipc_id = msq->q_perm.key;
5155
5156 /* Can this process write to the queue? */
David Howells275bb412008-11-14 10:39:19 +11005157 rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 MSGQ__WRITE, &ad);
5159 if (!rc)
5160 /* Can this process send the message */
David Howells275bb412008-11-14 10:39:19 +11005161 rc = avc_has_perm(sid, msec->sid, SECCLASS_MSG,
5162 MSG__SEND, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163 if (!rc)
5164 /* Can the message be put in the queue? */
David Howells275bb412008-11-14 10:39:19 +11005165 rc = avc_has_perm(msec->sid, isec->sid, SECCLASS_MSGQ,
5166 MSGQ__ENQUEUE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167
5168 return rc;
5169}
5170
5171static int selinux_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
5172 struct task_struct *target,
5173 long type, int mode)
5174{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175 struct ipc_security_struct *isec;
5176 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005177 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005178 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005179 u32 sid = task_sid(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180 int rc;
5181
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182 isec = msq->q_perm.security;
5183 msec = msg->security;
5184
Thomas Liu2bf49692009-07-14 12:14:09 -04005185 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005186 ad.selinux_audit_data = &sad;
Eric Paris828dfe12008-04-17 13:17:49 -04005187 ad.u.ipc_id = msq->q_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188
David Howells275bb412008-11-14 10:39:19 +11005189 rc = avc_has_perm(sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190 SECCLASS_MSGQ, MSGQ__READ, &ad);
5191 if (!rc)
David Howells275bb412008-11-14 10:39:19 +11005192 rc = avc_has_perm(sid, msec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 SECCLASS_MSG, MSG__RECEIVE, &ad);
5194 return rc;
5195}
5196
5197/* Shared Memory security operations */
5198static int selinux_shm_alloc_security(struct shmid_kernel *shp)
5199{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005201 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005202 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005203 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204 int rc;
5205
5206 rc = ipc_alloc_security(current, &shp->shm_perm, SECCLASS_SHM);
5207 if (rc)
5208 return rc;
5209
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210 isec = shp->shm_perm.security;
5211
Thomas Liu2bf49692009-07-14 12:14:09 -04005212 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005213 ad.selinux_audit_data = &sad;
Eric Paris828dfe12008-04-17 13:17:49 -04005214 ad.u.ipc_id = shp->shm_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215
David Howells275bb412008-11-14 10:39:19 +11005216 rc = avc_has_perm(sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217 SHM__CREATE, &ad);
5218 if (rc) {
5219 ipc_free_security(&shp->shm_perm);
5220 return rc;
5221 }
5222 return 0;
5223}
5224
5225static void selinux_shm_free_security(struct shmid_kernel *shp)
5226{
5227 ipc_free_security(&shp->shm_perm);
5228}
5229
5230static int selinux_shm_associate(struct shmid_kernel *shp, int shmflg)
5231{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005233 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005234 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005235 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237 isec = shp->shm_perm.security;
5238
Thomas Liu2bf49692009-07-14 12:14:09 -04005239 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005240 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005241 ad.u.ipc_id = shp->shm_perm.key;
5242
David Howells275bb412008-11-14 10:39:19 +11005243 return avc_has_perm(sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244 SHM__ASSOCIATE, &ad);
5245}
5246
5247/* Note, at this point, shp is locked down */
5248static int selinux_shm_shmctl(struct shmid_kernel *shp, int cmd)
5249{
5250 int perms;
5251 int err;
5252
Eric Paris828dfe12008-04-17 13:17:49 -04005253 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254 case IPC_INFO:
5255 case SHM_INFO:
5256 /* No specific object, just general system-wide information. */
5257 return task_has_system(current, SYSTEM__IPC_INFO);
5258 case IPC_STAT:
5259 case SHM_STAT:
5260 perms = SHM__GETATTR | SHM__ASSOCIATE;
5261 break;
5262 case IPC_SET:
5263 perms = SHM__SETATTR;
5264 break;
5265 case SHM_LOCK:
5266 case SHM_UNLOCK:
5267 perms = SHM__LOCK;
5268 break;
5269 case IPC_RMID:
5270 perms = SHM__DESTROY;
5271 break;
5272 default:
5273 return 0;
5274 }
5275
Stephen Smalley6af963f2005-05-01 08:58:39 -07005276 err = ipc_has_perm(&shp->shm_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277 return err;
5278}
5279
5280static int selinux_shm_shmat(struct shmid_kernel *shp,
5281 char __user *shmaddr, int shmflg)
5282{
5283 u32 perms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284
5285 if (shmflg & SHM_RDONLY)
5286 perms = SHM__READ;
5287 else
5288 perms = SHM__READ | SHM__WRITE;
5289
Stephen Smalley6af963f2005-05-01 08:58:39 -07005290 return ipc_has_perm(&shp->shm_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291}
5292
5293/* Semaphore security operations */
5294static int selinux_sem_alloc_security(struct sem_array *sma)
5295{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005297 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005298 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005299 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300 int rc;
5301
5302 rc = ipc_alloc_security(current, &sma->sem_perm, SECCLASS_SEM);
5303 if (rc)
5304 return rc;
5305
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306 isec = sma->sem_perm.security;
5307
Thomas Liu2bf49692009-07-14 12:14:09 -04005308 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005309 ad.selinux_audit_data = &sad;
Eric Paris828dfe12008-04-17 13:17:49 -04005310 ad.u.ipc_id = sma->sem_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311
David Howells275bb412008-11-14 10:39:19 +11005312 rc = avc_has_perm(sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313 SEM__CREATE, &ad);
5314 if (rc) {
5315 ipc_free_security(&sma->sem_perm);
5316 return rc;
5317 }
5318 return 0;
5319}
5320
5321static void selinux_sem_free_security(struct sem_array *sma)
5322{
5323 ipc_free_security(&sma->sem_perm);
5324}
5325
5326static int selinux_sem_associate(struct sem_array *sma, int semflg)
5327{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005329 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005330 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005331 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333 isec = sma->sem_perm.security;
5334
Thomas Liu2bf49692009-07-14 12:14:09 -04005335 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005336 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337 ad.u.ipc_id = sma->sem_perm.key;
5338
David Howells275bb412008-11-14 10:39:19 +11005339 return avc_has_perm(sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340 SEM__ASSOCIATE, &ad);
5341}
5342
5343/* Note, at this point, sma is locked down */
5344static int selinux_sem_semctl(struct sem_array *sma, int cmd)
5345{
5346 int err;
5347 u32 perms;
5348
Eric Paris828dfe12008-04-17 13:17:49 -04005349 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350 case IPC_INFO:
5351 case SEM_INFO:
5352 /* No specific object, just general system-wide information. */
5353 return task_has_system(current, SYSTEM__IPC_INFO);
5354 case GETPID:
5355 case GETNCNT:
5356 case GETZCNT:
5357 perms = SEM__GETATTR;
5358 break;
5359 case GETVAL:
5360 case GETALL:
5361 perms = SEM__READ;
5362 break;
5363 case SETVAL:
5364 case SETALL:
5365 perms = SEM__WRITE;
5366 break;
5367 case IPC_RMID:
5368 perms = SEM__DESTROY;
5369 break;
5370 case IPC_SET:
5371 perms = SEM__SETATTR;
5372 break;
5373 case IPC_STAT:
5374 case SEM_STAT:
5375 perms = SEM__GETATTR | SEM__ASSOCIATE;
5376 break;
5377 default:
5378 return 0;
5379 }
5380
Stephen Smalley6af963f2005-05-01 08:58:39 -07005381 err = ipc_has_perm(&sma->sem_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382 return err;
5383}
5384
5385static int selinux_sem_semop(struct sem_array *sma,
5386 struct sembuf *sops, unsigned nsops, int alter)
5387{
5388 u32 perms;
5389
5390 if (alter)
5391 perms = SEM__READ | SEM__WRITE;
5392 else
5393 perms = SEM__READ;
5394
Stephen Smalley6af963f2005-05-01 08:58:39 -07005395 return ipc_has_perm(&sma->sem_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396}
5397
5398static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
5399{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400 u32 av = 0;
5401
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402 av = 0;
5403 if (flag & S_IRUGO)
5404 av |= IPC__UNIX_READ;
5405 if (flag & S_IWUGO)
5406 av |= IPC__UNIX_WRITE;
5407
5408 if (av == 0)
5409 return 0;
5410
Stephen Smalley6af963f2005-05-01 08:58:39 -07005411 return ipc_has_perm(ipcp, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412}
5413
Ahmed S. Darwish713a04a2008-03-01 21:52:30 +02005414static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
5415{
5416 struct ipc_security_struct *isec = ipcp->security;
5417 *secid = isec->sid;
5418}
5419
Eric Paris828dfe12008-04-17 13:17:49 -04005420static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421{
5422 if (inode)
5423 inode_doinit_with_dentry(inode, dentry);
5424}
5425
5426static int selinux_getprocattr(struct task_struct *p,
Al Viro04ff9702007-03-12 16:17:58 +00005427 char *name, char **value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428{
David Howells275bb412008-11-14 10:39:19 +11005429 const struct task_security_struct *__tsec;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00005430 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 int error;
Al Viro04ff9702007-03-12 16:17:58 +00005432 unsigned len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433
5434 if (current != p) {
David Howells3b11a1d2008-11-14 10:39:26 +11005435 error = current_has_perm(p, PROCESS__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 if (error)
5437 return error;
5438 }
5439
David Howells275bb412008-11-14 10:39:19 +11005440 rcu_read_lock();
5441 __tsec = __task_cred(p)->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442
5443 if (!strcmp(name, "current"))
David Howells275bb412008-11-14 10:39:19 +11005444 sid = __tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445 else if (!strcmp(name, "prev"))
David Howells275bb412008-11-14 10:39:19 +11005446 sid = __tsec->osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447 else if (!strcmp(name, "exec"))
David Howells275bb412008-11-14 10:39:19 +11005448 sid = __tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 else if (!strcmp(name, "fscreate"))
David Howells275bb412008-11-14 10:39:19 +11005450 sid = __tsec->create_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07005451 else if (!strcmp(name, "keycreate"))
David Howells275bb412008-11-14 10:39:19 +11005452 sid = __tsec->keycreate_sid;
Eric Paris42c3e032006-06-26 00:26:03 -07005453 else if (!strcmp(name, "sockcreate"))
David Howells275bb412008-11-14 10:39:19 +11005454 sid = __tsec->sockcreate_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455 else
David Howells275bb412008-11-14 10:39:19 +11005456 goto invalid;
5457 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458
5459 if (!sid)
5460 return 0;
5461
Al Viro04ff9702007-03-12 16:17:58 +00005462 error = security_sid_to_context(sid, value, &len);
5463 if (error)
5464 return error;
5465 return len;
David Howells275bb412008-11-14 10:39:19 +11005466
5467invalid:
5468 rcu_read_unlock();
5469 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470}
5471
5472static int selinux_setprocattr(struct task_struct *p,
5473 char *name, void *value, size_t size)
5474{
5475 struct task_security_struct *tsec;
Roland McGrath03563572008-03-26 15:46:39 -07005476 struct task_struct *tracer;
David Howellsd84f4f92008-11-14 10:39:23 +11005477 struct cred *new;
5478 u32 sid = 0, ptsid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479 int error;
5480 char *str = value;
5481
5482 if (current != p) {
5483 /* SELinux only allows a process to change its own
5484 security attributes. */
5485 return -EACCES;
5486 }
5487
5488 /*
5489 * Basic control over ability to set these attributes at all.
5490 * current == p, but we'll pass them separately in case the
5491 * above restriction is ever removed.
5492 */
5493 if (!strcmp(name, "exec"))
David Howells3b11a1d2008-11-14 10:39:26 +11005494 error = current_has_perm(p, PROCESS__SETEXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 else if (!strcmp(name, "fscreate"))
David Howells3b11a1d2008-11-14 10:39:26 +11005496 error = current_has_perm(p, PROCESS__SETFSCREATE);
Michael LeMay4eb582c2006-06-26 00:24:57 -07005497 else if (!strcmp(name, "keycreate"))
David Howells3b11a1d2008-11-14 10:39:26 +11005498 error = current_has_perm(p, PROCESS__SETKEYCREATE);
Eric Paris42c3e032006-06-26 00:26:03 -07005499 else if (!strcmp(name, "sockcreate"))
David Howells3b11a1d2008-11-14 10:39:26 +11005500 error = current_has_perm(p, PROCESS__SETSOCKCREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501 else if (!strcmp(name, "current"))
David Howells3b11a1d2008-11-14 10:39:26 +11005502 error = current_has_perm(p, PROCESS__SETCURRENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503 else
5504 error = -EINVAL;
5505 if (error)
5506 return error;
5507
5508 /* Obtain a SID for the context, if one was specified. */
5509 if (size && str[1] && str[1] != '\n') {
5510 if (str[size-1] == '\n') {
5511 str[size-1] = 0;
5512 size--;
5513 }
5514 error = security_context_to_sid(value, size, &sid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04005515 if (error == -EINVAL && !strcmp(name, "fscreate")) {
5516 if (!capable(CAP_MAC_ADMIN))
5517 return error;
5518 error = security_context_to_sid_force(value, size,
5519 &sid);
5520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521 if (error)
5522 return error;
5523 }
5524
David Howellsd84f4f92008-11-14 10:39:23 +11005525 new = prepare_creds();
5526 if (!new)
5527 return -ENOMEM;
5528
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529 /* Permission checking based on the specified context is
5530 performed during the actual operation (execve,
5531 open/mkdir/...), when we know the full context of the
David Howellsd84f4f92008-11-14 10:39:23 +11005532 operation. See selinux_bprm_set_creds for the execve
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533 checks and may_create for the file creation checks. The
5534 operation will then fail if the context is not permitted. */
David Howellsd84f4f92008-11-14 10:39:23 +11005535 tsec = new->security;
5536 if (!strcmp(name, "exec")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537 tsec->exec_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005538 } else if (!strcmp(name, "fscreate")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539 tsec->create_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005540 } else if (!strcmp(name, "keycreate")) {
Michael LeMay4eb582c2006-06-26 00:24:57 -07005541 error = may_create_key(sid, p);
5542 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11005543 goto abort_change;
Michael LeMay4eb582c2006-06-26 00:24:57 -07005544 tsec->keycreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005545 } else if (!strcmp(name, "sockcreate")) {
Eric Paris42c3e032006-06-26 00:26:03 -07005546 tsec->sockcreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005547 } else if (!strcmp(name, "current")) {
5548 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549 if (sid == 0)
David Howellsd84f4f92008-11-14 10:39:23 +11005550 goto abort_change;
KaiGai Koheid9250de2008-08-28 16:35:57 +09005551
David Howellsd84f4f92008-11-14 10:39:23 +11005552 /* Only allow single threaded processes to change context */
5553 error = -EPERM;
Oleg Nesterov5bb459b2009-07-10 03:48:23 +02005554 if (!current_is_single_threaded()) {
David Howellsd84f4f92008-11-14 10:39:23 +11005555 error = security_bounded_transition(tsec->sid, sid);
5556 if (error)
5557 goto abort_change;
Eric Paris828dfe12008-04-17 13:17:49 -04005558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559
5560 /* Check permissions for the transition. */
5561 error = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS,
Eric Paris828dfe12008-04-17 13:17:49 -04005562 PROCESS__DYNTRANSITION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11005564 goto abort_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565
5566 /* Check for ptracing, and update the task SID if ok.
5567 Otherwise, leave SID unchanged and fail. */
David Howellsd84f4f92008-11-14 10:39:23 +11005568 ptsid = 0;
Oleg Nesterov58c23142013-12-23 17:45:01 -05005569 rcu_read_lock();
Tejun Heo06d98472011-06-17 16:50:40 +02005570 tracer = ptrace_parent(p);
David Howellsd84f4f92008-11-14 10:39:23 +11005571 if (tracer)
5572 ptsid = task_sid(tracer);
Oleg Nesterov58c23142013-12-23 17:45:01 -05005573 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574
David Howellsd84f4f92008-11-14 10:39:23 +11005575 if (tracer) {
5576 error = avc_has_perm(ptsid, sid, SECCLASS_PROCESS,
5577 PROCESS__PTRACE, NULL);
5578 if (error)
5579 goto abort_change;
5580 }
5581
5582 tsec->sid = sid;
5583 } else {
5584 error = -EINVAL;
5585 goto abort_change;
5586 }
5587
5588 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589 return size;
David Howellsd84f4f92008-11-14 10:39:23 +11005590
5591abort_change:
5592 abort_creds(new);
5593 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594}
5595
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005596static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
5597{
5598 return security_sid_to_context(secid, secdata, seclen);
5599}
5600
David Howells7bf570d2008-04-29 20:52:51 +01005601static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
David Howells63cb3442008-01-15 23:47:35 +00005602{
5603 return security_context_to_sid(secdata, seclen, secid);
5604}
5605
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005606static void selinux_release_secctx(char *secdata, u32 seclen)
5607{
Paul Moore088999e2007-08-01 11:12:58 -04005608 kfree(secdata);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005609}
5610
David P. Quigley1ee65e32009-09-03 14:25:57 -04005611/*
5612 * called with inode->i_mutex locked
5613 */
5614static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
5615{
5616 return selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, ctx, ctxlen, 0);
5617}
5618
5619/*
5620 * called with inode->i_mutex locked
5621 */
5622static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
5623{
5624 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0);
5625}
5626
5627static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
5628{
5629 int len = 0;
5630 len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX,
5631 ctx, true);
5632 if (len < 0)
5633 return len;
5634 *ctxlen = len;
5635 return 0;
5636}
Michael LeMayd7200242006-06-22 14:47:17 -07005637#ifdef CONFIG_KEYS
5638
David Howellsd84f4f92008-11-14 10:39:23 +11005639static int selinux_key_alloc(struct key *k, const struct cred *cred,
David Howells7e047ef2006-06-26 00:24:50 -07005640 unsigned long flags)
Michael LeMayd7200242006-06-22 14:47:17 -07005641{
David Howellsd84f4f92008-11-14 10:39:23 +11005642 const struct task_security_struct *tsec;
Michael LeMayd7200242006-06-22 14:47:17 -07005643 struct key_security_struct *ksec;
5644
5645 ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
5646 if (!ksec)
5647 return -ENOMEM;
5648
David Howellsd84f4f92008-11-14 10:39:23 +11005649 tsec = cred->security;
5650 if (tsec->keycreate_sid)
5651 ksec->sid = tsec->keycreate_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07005652 else
David Howellsd84f4f92008-11-14 10:39:23 +11005653 ksec->sid = tsec->sid;
Michael LeMayd7200242006-06-22 14:47:17 -07005654
David Howells275bb412008-11-14 10:39:19 +11005655 k->security = ksec;
Michael LeMayd7200242006-06-22 14:47:17 -07005656 return 0;
5657}
5658
5659static void selinux_key_free(struct key *k)
5660{
5661 struct key_security_struct *ksec = k->security;
5662
5663 k->security = NULL;
5664 kfree(ksec);
5665}
5666
5667static int selinux_key_permission(key_ref_t key_ref,
David Howellsd84f4f92008-11-14 10:39:23 +11005668 const struct cred *cred,
5669 key_perm_t perm)
Michael LeMayd7200242006-06-22 14:47:17 -07005670{
5671 struct key *key;
Michael LeMayd7200242006-06-22 14:47:17 -07005672 struct key_security_struct *ksec;
David Howells275bb412008-11-14 10:39:19 +11005673 u32 sid;
Michael LeMayd7200242006-06-22 14:47:17 -07005674
5675 /* if no specific permissions are requested, we skip the
5676 permission check. No serious, additional covert channels
5677 appear to be created. */
5678 if (perm == 0)
5679 return 0;
5680
David Howellsd84f4f92008-11-14 10:39:23 +11005681 sid = cred_sid(cred);
David Howells275bb412008-11-14 10:39:19 +11005682
5683 key = key_ref_to_ptr(key_ref);
5684 ksec = key->security;
5685
5686 return avc_has_perm(sid, ksec->sid, SECCLASS_KEY, perm, NULL);
Michael LeMayd7200242006-06-22 14:47:17 -07005687}
5688
David Howells70a5bb72008-04-29 01:01:26 -07005689static int selinux_key_getsecurity(struct key *key, char **_buffer)
5690{
5691 struct key_security_struct *ksec = key->security;
5692 char *context = NULL;
5693 unsigned len;
5694 int rc;
5695
5696 rc = security_sid_to_context(ksec->sid, &context, &len);
5697 if (!rc)
5698 rc = len;
5699 *_buffer = context;
5700 return rc;
5701}
5702
Michael LeMayd7200242006-06-22 14:47:17 -07005703#endif
5704
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705static struct security_operations selinux_ops = {
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02005706 .name = "selinux",
5707
Stephen Smalley84ab2cd2012-11-05 08:15:34 -05005708 .binder_set_context_mgr = selinux_binder_set_context_mgr,
5709 .binder_transaction = selinux_binder_transaction,
5710 .binder_transfer_binder = selinux_binder_transfer_binder,
5711 .binder_transfer_file = selinux_binder_transfer_file,
5712
Ingo Molnar9e488582009-05-07 19:26:19 +10005713 .ptrace_access_check = selinux_ptrace_access_check,
David Howells5cd9c582008-08-14 11:37:28 +01005714 .ptrace_traceme = selinux_ptrace_traceme,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715 .capget = selinux_capget,
David Howellsd84f4f92008-11-14 10:39:23 +11005716 .capset = selinux_capset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717 .capable = selinux_capable,
5718 .quotactl = selinux_quotactl,
5719 .quota_on = selinux_quota_on,
5720 .syslog = selinux_syslog,
5721 .vm_enough_memory = selinux_vm_enough_memory,
5722
5723 .netlink_send = selinux_netlink_send,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724
David Howellsa6f76f22008-11-14 10:39:24 +11005725 .bprm_set_creds = selinux_bprm_set_creds,
David Howellsa6f76f22008-11-14 10:39:24 +11005726 .bprm_committing_creds = selinux_bprm_committing_creds,
5727 .bprm_committed_creds = selinux_bprm_committed_creds,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728 .bprm_secureexec = selinux_bprm_secureexec,
5729
5730 .sb_alloc_security = selinux_sb_alloc_security,
5731 .sb_free_security = selinux_sb_free_security,
5732 .sb_copy_data = selinux_sb_copy_data,
Eric Paris026eb162011-03-03 16:09:14 -05005733 .sb_remount = selinux_sb_remount,
Eric Paris828dfe12008-04-17 13:17:49 -04005734 .sb_kern_mount = selinux_sb_kern_mount,
Eric Paris2069f452008-07-04 09:47:13 +10005735 .sb_show_options = selinux_sb_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736 .sb_statfs = selinux_sb_statfs,
5737 .sb_mount = selinux_mount,
5738 .sb_umount = selinux_umount,
Eric Parisc9180a52007-11-30 13:00:35 -05005739 .sb_set_mnt_opts = selinux_set_mnt_opts,
Eric Paris828dfe12008-04-17 13:17:49 -04005740 .sb_clone_mnt_opts = selinux_sb_clone_mnt_opts,
Eric Parise0007522008-03-05 10:31:54 -05005741 .sb_parse_opts_str = selinux_parse_opts_str,
5742
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743
5744 .inode_alloc_security = selinux_inode_alloc_security,
5745 .inode_free_security = selinux_inode_free_security,
Stephen Smalley5e41ff92005-09-09 13:01:35 -07005746 .inode_init_security = selinux_inode_init_security,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747 .inode_create = selinux_inode_create,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748 .inode_link = selinux_inode_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749 .inode_unlink = selinux_inode_unlink,
5750 .inode_symlink = selinux_inode_symlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751 .inode_mkdir = selinux_inode_mkdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752 .inode_rmdir = selinux_inode_rmdir,
5753 .inode_mknod = selinux_inode_mknod,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754 .inode_rename = selinux_inode_rename,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755 .inode_readlink = selinux_inode_readlink,
5756 .inode_follow_link = selinux_inode_follow_link,
5757 .inode_permission = selinux_inode_permission,
5758 .inode_setattr = selinux_inode_setattr,
5759 .inode_getattr = selinux_inode_getattr,
5760 .inode_setxattr = selinux_inode_setxattr,
5761 .inode_post_setxattr = selinux_inode_post_setxattr,
5762 .inode_getxattr = selinux_inode_getxattr,
5763 .inode_listxattr = selinux_inode_listxattr,
5764 .inode_removexattr = selinux_inode_removexattr,
Eric Paris828dfe12008-04-17 13:17:49 -04005765 .inode_getsecurity = selinux_inode_getsecurity,
5766 .inode_setsecurity = selinux_inode_setsecurity,
5767 .inode_listsecurity = selinux_inode_listsecurity,
Eric Parisf5269712008-05-14 11:27:45 -04005768 .inode_getsecid = selinux_inode_getsecid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769
5770 .file_permission = selinux_file_permission,
5771 .file_alloc_security = selinux_file_alloc_security,
5772 .file_free_security = selinux_file_free_security,
5773 .file_ioctl = selinux_file_ioctl,
5774 .file_mmap = selinux_file_mmap,
5775 .file_mprotect = selinux_file_mprotect,
5776 .file_lock = selinux_file_lock,
5777 .file_fcntl = selinux_file_fcntl,
5778 .file_set_fowner = selinux_file_set_fowner,
5779 .file_send_sigiotask = selinux_file_send_sigiotask,
5780 .file_receive = selinux_file_receive,
5781
Eric Paris828dfe12008-04-17 13:17:49 -04005782 .dentry_open = selinux_dentry_open,
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09005783
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784 .task_create = selinux_task_create,
David Howellsee18d642009-09-02 09:14:21 +01005785 .cred_alloc_blank = selinux_cred_alloc_blank,
David Howellsf1752ee2008-11-14 10:39:17 +11005786 .cred_free = selinux_cred_free,
David Howellsd84f4f92008-11-14 10:39:23 +11005787 .cred_prepare = selinux_cred_prepare,
David Howellsee18d642009-09-02 09:14:21 +01005788 .cred_transfer = selinux_cred_transfer,
David Howells3a3b7ce2008-11-14 10:39:28 +11005789 .kernel_act_as = selinux_kernel_act_as,
5790 .kernel_create_files_as = selinux_kernel_create_files_as,
Eric Paris25354c42009-08-13 09:45:03 -04005791 .kernel_module_request = selinux_kernel_module_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792 .task_setpgid = selinux_task_setpgid,
5793 .task_getpgid = selinux_task_getpgid,
Eric Paris828dfe12008-04-17 13:17:49 -04005794 .task_getsid = selinux_task_getsid,
David Quigleyf9008e42006-06-30 01:55:46 -07005795 .task_getsecid = selinux_task_getsecid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796 .task_setnice = selinux_task_setnice,
James Morris03e68062006-06-23 02:03:58 -07005797 .task_setioprio = selinux_task_setioprio,
David Quigleya1836a42006-06-30 01:55:49 -07005798 .task_getioprio = selinux_task_getioprio,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799 .task_setrlimit = selinux_task_setrlimit,
5800 .task_setscheduler = selinux_task_setscheduler,
5801 .task_getscheduler = selinux_task_getscheduler,
David Quigley35601542006-06-23 02:04:01 -07005802 .task_movememory = selinux_task_movememory,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803 .task_kill = selinux_task_kill,
5804 .task_wait = selinux_task_wait,
Eric Paris828dfe12008-04-17 13:17:49 -04005805 .task_to_inode = selinux_task_to_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806
5807 .ipc_permission = selinux_ipc_permission,
Eric Parisf5269712008-05-14 11:27:45 -04005808 .ipc_getsecid = selinux_ipc_getsecid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809
5810 .msg_msg_alloc_security = selinux_msg_msg_alloc_security,
5811 .msg_msg_free_security = selinux_msg_msg_free_security,
5812
5813 .msg_queue_alloc_security = selinux_msg_queue_alloc_security,
5814 .msg_queue_free_security = selinux_msg_queue_free_security,
5815 .msg_queue_associate = selinux_msg_queue_associate,
5816 .msg_queue_msgctl = selinux_msg_queue_msgctl,
5817 .msg_queue_msgsnd = selinux_msg_queue_msgsnd,
5818 .msg_queue_msgrcv = selinux_msg_queue_msgrcv,
5819
5820 .shm_alloc_security = selinux_shm_alloc_security,
5821 .shm_free_security = selinux_shm_free_security,
5822 .shm_associate = selinux_shm_associate,
5823 .shm_shmctl = selinux_shm_shmctl,
5824 .shm_shmat = selinux_shm_shmat,
5825
Eric Paris828dfe12008-04-17 13:17:49 -04005826 .sem_alloc_security = selinux_sem_alloc_security,
5827 .sem_free_security = selinux_sem_free_security,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828 .sem_associate = selinux_sem_associate,
5829 .sem_semctl = selinux_sem_semctl,
5830 .sem_semop = selinux_sem_semop,
5831
Eric Paris828dfe12008-04-17 13:17:49 -04005832 .d_instantiate = selinux_d_instantiate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833
Eric Paris828dfe12008-04-17 13:17:49 -04005834 .getprocattr = selinux_getprocattr,
5835 .setprocattr = selinux_setprocattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005837 .secid_to_secctx = selinux_secid_to_secctx,
David Howells63cb3442008-01-15 23:47:35 +00005838 .secctx_to_secid = selinux_secctx_to_secid,
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005839 .release_secctx = selinux_release_secctx,
David P. Quigley1ee65e32009-09-03 14:25:57 -04005840 .inode_notifysecctx = selinux_inode_notifysecctx,
5841 .inode_setsecctx = selinux_inode_setsecctx,
5842 .inode_getsecctx = selinux_inode_getsecctx,
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005843
Eric Paris828dfe12008-04-17 13:17:49 -04005844 .unix_stream_connect = selinux_socket_unix_stream_connect,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005845 .unix_may_send = selinux_socket_unix_may_send,
5846
5847 .socket_create = selinux_socket_create,
5848 .socket_post_create = selinux_socket_post_create,
5849 .socket_bind = selinux_socket_bind,
5850 .socket_connect = selinux_socket_connect,
5851 .socket_listen = selinux_socket_listen,
5852 .socket_accept = selinux_socket_accept,
5853 .socket_sendmsg = selinux_socket_sendmsg,
5854 .socket_recvmsg = selinux_socket_recvmsg,
5855 .socket_getsockname = selinux_socket_getsockname,
5856 .socket_getpeername = selinux_socket_getpeername,
5857 .socket_getsockopt = selinux_socket_getsockopt,
5858 .socket_setsockopt = selinux_socket_setsockopt,
5859 .socket_shutdown = selinux_socket_shutdown,
5860 .socket_sock_rcv_skb = selinux_socket_sock_rcv_skb,
Catherine Zhang2c7946a2006-03-20 22:41:23 -08005861 .socket_getpeersec_stream = selinux_socket_getpeersec_stream,
5862 .socket_getpeersec_dgram = selinux_socket_getpeersec_dgram,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863 .sk_alloc_security = selinux_sk_alloc_security,
5864 .sk_free_security = selinux_sk_free_security,
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005865 .sk_clone_security = selinux_sk_clone_security,
Eric Paris828dfe12008-04-17 13:17:49 -04005866 .sk_getsecid = selinux_sk_getsecid,
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005867 .sock_graft = selinux_sock_graft,
5868 .inet_conn_request = selinux_inet_conn_request,
5869 .inet_csk_clone = selinux_inet_csk_clone,
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005870 .inet_conn_established = selinux_inet_conn_established,
Eric Paris2606fd12010-10-13 16:24:41 -04005871 .secmark_relabel_packet = selinux_secmark_relabel_packet,
5872 .secmark_refcount_inc = selinux_secmark_refcount_inc,
5873 .secmark_refcount_dec = selinux_secmark_refcount_dec,
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005874 .req_classify_flow = selinux_req_classify_flow,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005875 .tun_dev_create = selinux_tun_dev_create,
5876 .tun_dev_post_create = selinux_tun_dev_post_create,
5877 .tun_dev_attach = selinux_tun_dev_attach,
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005878
5879#ifdef CONFIG_SECURITY_NETWORK_XFRM
5880 .xfrm_policy_alloc_security = selinux_xfrm_policy_alloc,
5881 .xfrm_policy_clone_security = selinux_xfrm_policy_clone,
5882 .xfrm_policy_free_security = selinux_xfrm_policy_free,
Catherine Zhangc8c05a82006-06-08 23:39:49 -07005883 .xfrm_policy_delete_security = selinux_xfrm_policy_delete,
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005884 .xfrm_state_alloc_security = selinux_xfrm_state_alloc,
5885 .xfrm_state_free_security = selinux_xfrm_state_free,
Catherine Zhangc8c05a82006-06-08 23:39:49 -07005886 .xfrm_state_delete_security = selinux_xfrm_state_delete,
Eric Paris828dfe12008-04-17 13:17:49 -04005887 .xfrm_policy_lookup = selinux_xfrm_policy_lookup,
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07005888 .xfrm_state_pol_flow_match = selinux_xfrm_state_pol_flow_match,
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07005889 .xfrm_decode_session = selinux_xfrm_decode_session,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890#endif
Michael LeMayd7200242006-06-22 14:47:17 -07005891
5892#ifdef CONFIG_KEYS
Eric Paris828dfe12008-04-17 13:17:49 -04005893 .key_alloc = selinux_key_alloc,
5894 .key_free = selinux_key_free,
5895 .key_permission = selinux_key_permission,
David Howells70a5bb72008-04-29 01:01:26 -07005896 .key_getsecurity = selinux_key_getsecurity,
Michael LeMayd7200242006-06-22 14:47:17 -07005897#endif
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02005898
5899#ifdef CONFIG_AUDIT
5900 .audit_rule_init = selinux_audit_rule_init,
5901 .audit_rule_known = selinux_audit_rule_known,
5902 .audit_rule_match = selinux_audit_rule_match,
5903 .audit_rule_free = selinux_audit_rule_free,
5904#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905};
5906
5907static __init int selinux_init(void)
5908{
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02005909 if (!security_module_enable(&selinux_ops)) {
5910 selinux_enabled = 0;
5911 return 0;
5912 }
5913
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914 if (!selinux_enabled) {
5915 printk(KERN_INFO "SELinux: Disabled at boot.\n");
5916 return 0;
5917 }
5918
5919 printk(KERN_INFO "SELinux: Initializing.\n");
5920
5921 /* Set the security state for the initial task. */
David Howellsd84f4f92008-11-14 10:39:23 +11005922 cred_init_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04005924 default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
5925
James Morris7cae7e22006-03-22 00:09:22 -08005926 sel_inode_cache = kmem_cache_create("selinux_inode_security",
5927 sizeof(struct inode_security_struct),
Paul Mundt20c2df82007-07-20 10:11:58 +09005928 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005929 avc_init();
5930
Eric Paris828dfe12008-04-17 13:17:49 -04005931 if (register_security(&selinux_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005932 panic("SELinux: Unable to register with kernel.\n");
5933
Eric Paris828dfe12008-04-17 13:17:49 -04005934 if (selinux_enforcing)
Eric Parisfadcdb42007-02-22 18:11:31 -05005935 printk(KERN_DEBUG "SELinux: Starting in enforcing mode\n");
Eric Paris828dfe12008-04-17 13:17:49 -04005936 else
Eric Parisfadcdb42007-02-22 18:11:31 -05005937 printk(KERN_DEBUG "SELinux: Starting in permissive mode\n");
Michael LeMayd7200242006-06-22 14:47:17 -07005938
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939 return 0;
5940}
5941
Al Viroe8c26252010-03-23 06:36:54 -04005942static void delayed_superblock_init(struct super_block *sb, void *unused)
5943{
5944 superblock_doinit(sb, NULL);
5945}
5946
Linus Torvalds1da177e2005-04-16 15:20:36 -07005947void selinux_complete_init(void)
5948{
Eric Parisfadcdb42007-02-22 18:11:31 -05005949 printk(KERN_DEBUG "SELinux: Completing initialization.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005950
5951 /* Set up any superblocks initialized prior to the policy load. */
Eric Parisfadcdb42007-02-22 18:11:31 -05005952 printk(KERN_DEBUG "SELinux: Setting up existing superblocks.\n");
Al Viroe8c26252010-03-23 06:36:54 -04005953 iterate_supers(delayed_superblock_init, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954}
5955
5956/* SELinux requires early initialization in order to label
5957 all processes and objects when they are created. */
5958security_initcall(selinux_init);
5959
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08005960#if defined(CONFIG_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005961
Paul Mooreeffad8d2008-01-29 08:49:27 -05005962static struct nf_hook_ops selinux_ipv4_ops[] = {
5963 {
5964 .hook = selinux_ipv4_postroute,
5965 .owner = THIS_MODULE,
5966 .pf = PF_INET,
5967 .hooknum = NF_INET_POST_ROUTING,
5968 .priority = NF_IP_PRI_SELINUX_LAST,
5969 },
5970 {
5971 .hook = selinux_ipv4_forward,
5972 .owner = THIS_MODULE,
5973 .pf = PF_INET,
5974 .hooknum = NF_INET_FORWARD,
5975 .priority = NF_IP_PRI_SELINUX_FIRST,
Paul Moore948bf852008-10-10 10:16:32 -04005976 },
5977 {
5978 .hook = selinux_ipv4_output,
5979 .owner = THIS_MODULE,
5980 .pf = PF_INET,
5981 .hooknum = NF_INET_LOCAL_OUT,
5982 .priority = NF_IP_PRI_SELINUX_FIRST,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984};
5985
5986#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
5987
Paul Mooreeffad8d2008-01-29 08:49:27 -05005988static struct nf_hook_ops selinux_ipv6_ops[] = {
5989 {
5990 .hook = selinux_ipv6_postroute,
5991 .owner = THIS_MODULE,
5992 .pf = PF_INET6,
5993 .hooknum = NF_INET_POST_ROUTING,
5994 .priority = NF_IP6_PRI_SELINUX_LAST,
5995 },
5996 {
5997 .hook = selinux_ipv6_forward,
5998 .owner = THIS_MODULE,
5999 .pf = PF_INET6,
6000 .hooknum = NF_INET_FORWARD,
6001 .priority = NF_IP6_PRI_SELINUX_FIRST,
6002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006003};
6004
6005#endif /* IPV6 */
6006
6007static int __init selinux_nf_ip_init(void)
6008{
6009 int err = 0;
6010
6011 if (!selinux_enabled)
6012 goto out;
Eric Parisfadcdb42007-02-22 18:11:31 -05006013
6014 printk(KERN_DEBUG "SELinux: Registering netfilter hooks\n");
6015
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07006016 err = nf_register_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops));
6017 if (err)
6018 panic("SELinux: nf_register_hooks for IPv4: error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019
6020#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07006021 err = nf_register_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops));
6022 if (err)
6023 panic("SELinux: nf_register_hooks for IPv6: error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006024#endif /* IPV6 */
Trent Jaegerd28d1e02005-12-13 23:12:40 -08006025
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026out:
6027 return err;
6028}
6029
6030__initcall(selinux_nf_ip_init);
6031
6032#ifdef CONFIG_SECURITY_SELINUX_DISABLE
6033static void selinux_nf_ip_exit(void)
6034{
Eric Parisfadcdb42007-02-22 18:11:31 -05006035 printk(KERN_DEBUG "SELinux: Unregistering netfilter hooks\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07006037 nf_unregister_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07006039 nf_unregister_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040#endif /* IPV6 */
6041}
6042#endif
6043
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08006044#else /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045
6046#ifdef CONFIG_SECURITY_SELINUX_DISABLE
6047#define selinux_nf_ip_exit()
6048#endif
6049
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08006050#endif /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051
6052#ifdef CONFIG_SECURITY_SELINUX_DISABLE
Eric Paris828dfe12008-04-17 13:17:49 -04006053static int selinux_disabled;
6054
Linus Torvalds1da177e2005-04-16 15:20:36 -07006055int selinux_disable(void)
6056{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006057 if (ss_initialized) {
6058 /* Not permitted after initial policy load. */
6059 return -EINVAL;
6060 }
6061
6062 if (selinux_disabled) {
6063 /* Only do this once. */
6064 return -EINVAL;
6065 }
6066
6067 printk(KERN_INFO "SELinux: Disabled at runtime.\n");
6068
6069 selinux_disabled = 1;
Stephen Smalley30d55282006-05-03 10:52:36 -04006070 selinux_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071
wzt.wzt@gmail.com189b3b12010-02-23 23:15:28 +08006072 reset_security_ops();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006073
Eric Parisaf8ff042009-09-20 21:23:01 -04006074 /* Try to destroy the avc node cache */
6075 avc_disable();
6076
Linus Torvalds1da177e2005-04-16 15:20:36 -07006077 /* Unregister netfilter hooks. */
6078 selinux_nf_ip_exit();
6079
6080 /* Unregister selinuxfs. */
6081 exit_sel_fs();
6082
6083 return 0;
6084}
6085#endif