blob: 03b57cdb5299dc9391404ff576536a3c8bcf4fda [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 Moore220deb92008-01-29 08:38:23 -050055#include <net/net_namespace.h>
Paul Moored621d352008-01-29 08:43:36 -050056#include <net/netlabel.h>
Eric Parisf5269712008-05-14 11:27:45 -040057#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <asm/ioctls.h>
Arun Sharma60063492011-07-26 16:09:06 -070059#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <linux/bitops.h>
61#include <linux/interrupt.h>
62#include <linux/netdevice.h> /* for network interface checks */
63#include <linux/netlink.h>
64#include <linux/tcp.h>
65#include <linux/udp.h>
James Morris2ee92d42006-11-13 16:09:01 -080066#include <linux/dccp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <linux/quota.h>
68#include <linux/un.h> /* for Unix socket types */
69#include <net/af_unix.h> /* for Unix socket types */
70#include <linux/parser.h>
71#include <linux/nfs_mount.h>
72#include <net/ipv6.h>
73#include <linux/hugetlb.h>
74#include <linux/personality.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#include <linux/audit.h>
Eric Paris6931dfc2005-06-30 02:58:51 -070076#include <linux/string.h>
Catherine Zhang877ce7c2006-06-29 12:27:47 -070077#include <linux/selinux.h>
Eric Paris23970742006-09-25 23:32:01 -070078#include <linux/mutex.h>
Frank Mayharf06febc2008-09-12 09:54:39 -070079#include <linux/posix-timers.h>
Kees Cook00234592010-02-03 15:36:43 -080080#include <linux/syslog.h>
Serge E. Hallyn34867402011-03-23 16:43:17 -070081#include <linux/user_namespace.h>
Paul Gortmaker44fc7ea2011-05-26 20:52:10 -040082#include <linux/export.h>
Al Viro40401532012-02-13 03:58:52 +000083#include <linux/msg.h>
84#include <linux/shm.h>
Amir Samuelov6a22e462014-05-26 11:44:06 +030085#include <linux/pft.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
221static void inode_free_security(struct inode *inode)
222{
223 struct inode_security_struct *isec = inode->i_security;
224 struct superblock_security_struct *sbsec = inode->i_sb->s_security;
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 spin_lock(&sbsec->isec_lock);
227 if (!list_empty(&isec->list))
228 list_del_init(&isec->list);
229 spin_unlock(&sbsec->isec_lock);
230
231 inode->i_security = NULL;
James Morris7cae7e22006-03-22 00:09:22 -0800232 kmem_cache_free(sel_inode_cache, isec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
235static int file_alloc_security(struct file *file)
236{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 struct file_security_struct *fsec;
David Howells275bb412008-11-14 10:39:19 +1100238 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Stephen Smalley26d2a4b2006-02-01 03:05:55 -0800240 fsec = kzalloc(sizeof(struct file_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 if (!fsec)
242 return -ENOMEM;
243
David Howells275bb412008-11-14 10:39:19 +1100244 fsec->sid = sid;
245 fsec->fown_sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 file->f_security = fsec;
247
248 return 0;
249}
250
251static void file_free_security(struct file *file)
252{
253 struct file_security_struct *fsec = file->f_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 file->f_security = NULL;
255 kfree(fsec);
256}
257
258static int superblock_alloc_security(struct super_block *sb)
259{
260 struct superblock_security_struct *sbsec;
261
James Morris89d155e2005-10-30 14:59:21 -0800262 sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 if (!sbsec)
264 return -ENOMEM;
265
Eric Parisbc7e9822006-09-25 23:32:02 -0700266 mutex_init(&sbsec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 INIT_LIST_HEAD(&sbsec->isec_head);
268 spin_lock_init(&sbsec->isec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 sbsec->sb = sb;
270 sbsec->sid = SECINITSID_UNLABELED;
271 sbsec->def_sid = SECINITSID_FILE;
Eric Parisc312feb2006-07-10 04:43:53 -0700272 sbsec->mntpoint_sid = SECINITSID_UNLABELED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 sb->s_security = sbsec;
274
275 return 0;
276}
277
278static void superblock_free_security(struct super_block *sb)
279{
280 struct superblock_security_struct *sbsec = sb->s_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 sb->s_security = NULL;
282 kfree(sbsec);
283}
284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285/* The file system's label must be initialized prior to use. */
286
Stephen Hemminger634a5392010-03-04 21:59:03 -0800287static const char *labeling_behaviors[6] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 "uses xattr",
289 "uses transition SIDs",
290 "uses task SIDs",
291 "uses genfs_contexts",
292 "not configured for labeling",
293 "uses mountpoint labeling",
294};
295
296static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry);
297
298static inline int inode_doinit(struct inode *inode)
299{
300 return inode_doinit_with_dentry(inode, NULL);
301}
302
303enum {
Eric Paris31e87932007-09-19 17:19:12 -0400304 Opt_error = -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 Opt_context = 1,
306 Opt_fscontext = 2,
Eric Parisc9180a52007-11-30 13:00:35 -0500307 Opt_defcontext = 3,
308 Opt_rootcontext = 4,
David P. Quigley11689d42009-01-16 09:22:03 -0500309 Opt_labelsupport = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310};
311
Steven Whitehousea447c092008-10-13 10:46:57 +0100312static const match_table_t tokens = {
Eric Paris832cbd92008-04-01 13:24:09 -0400313 {Opt_context, CONTEXT_STR "%s"},
314 {Opt_fscontext, FSCONTEXT_STR "%s"},
315 {Opt_defcontext, DEFCONTEXT_STR "%s"},
316 {Opt_rootcontext, ROOTCONTEXT_STR "%s"},
David P. Quigley11689d42009-01-16 09:22:03 -0500317 {Opt_labelsupport, LABELSUPP_STR},
Eric Paris31e87932007-09-19 17:19:12 -0400318 {Opt_error, NULL},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319};
320
321#define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n"
322
Eric Parisc312feb2006-07-10 04:43:53 -0700323static int may_context_mount_sb_relabel(u32 sid,
324 struct superblock_security_struct *sbsec,
David Howells275bb412008-11-14 10:39:19 +1100325 const struct cred *cred)
Eric Parisc312feb2006-07-10 04:43:53 -0700326{
David Howells275bb412008-11-14 10:39:19 +1100327 const struct task_security_struct *tsec = cred->security;
Eric Parisc312feb2006-07-10 04:43:53 -0700328 int rc;
329
330 rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
331 FILESYSTEM__RELABELFROM, NULL);
332 if (rc)
333 return rc;
334
335 rc = avc_has_perm(tsec->sid, sid, SECCLASS_FILESYSTEM,
336 FILESYSTEM__RELABELTO, NULL);
337 return rc;
338}
339
Eric Paris08089252006-07-10 04:43:55 -0700340static int may_context_mount_inode_relabel(u32 sid,
341 struct superblock_security_struct *sbsec,
David Howells275bb412008-11-14 10:39:19 +1100342 const struct cred *cred)
Eric Paris08089252006-07-10 04:43:55 -0700343{
David Howells275bb412008-11-14 10:39:19 +1100344 const struct task_security_struct *tsec = cred->security;
Eric Paris08089252006-07-10 04:43:55 -0700345 int rc;
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(sid, sbsec->sid, SECCLASS_FILESYSTEM,
352 FILESYSTEM__ASSOCIATE, NULL);
353 return rc;
354}
355
Eric Parisc9180a52007-11-30 13:00:35 -0500356static int sb_finish_set_opts(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
358 struct superblock_security_struct *sbsec = sb->s_security;
359 struct dentry *root = sb->s_root;
Eric Parisc9180a52007-11-30 13:00:35 -0500360 struct inode *root_inode = root->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 int rc = 0;
362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
364 /* Make sure that the xattr handler exists and that no
365 error other than -ENODATA is returned by getxattr on
366 the root directory. -ENODATA is ok, as this may be
367 the first boot of the SELinux kernel before we have
368 assigned xattr values to the filesystem. */
Eric Parisc9180a52007-11-30 13:00:35 -0500369 if (!root_inode->i_op->getxattr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 printk(KERN_WARNING "SELinux: (dev %s, type %s) has no "
371 "xattr support\n", sb->s_id, sb->s_type->name);
372 rc = -EOPNOTSUPP;
373 goto out;
374 }
Eric Parisc9180a52007-11-30 13:00:35 -0500375 rc = root_inode->i_op->getxattr(root, XATTR_NAME_SELINUX, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 if (rc < 0 && rc != -ENODATA) {
377 if (rc == -EOPNOTSUPP)
378 printk(KERN_WARNING "SELinux: (dev %s, type "
379 "%s) has no security xattr handler\n",
380 sb->s_id, sb->s_type->name);
381 else
382 printk(KERN_WARNING "SELinux: (dev %s, type "
383 "%s) getxattr errno %d\n", sb->s_id,
384 sb->s_type->name, -rc);
385 goto out;
386 }
387 }
388
David P. Quigley11689d42009-01-16 09:22:03 -0500389 sbsec->flags |= (SE_SBINITIALIZED | SE_SBLABELSUPP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Eric Parisc9180a52007-11-30 13:00:35 -0500391 if (sbsec->behavior > ARRAY_SIZE(labeling_behaviors))
Eric Parisfadcdb42007-02-22 18:11:31 -0500392 printk(KERN_ERR "SELinux: initialized (dev %s, type %s), unknown behavior\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 sb->s_id, sb->s_type->name);
Eric Parisc9180a52007-11-30 13:00:35 -0500394 else
Eric Parisfadcdb42007-02-22 18:11:31 -0500395 printk(KERN_DEBUG "SELinux: initialized (dev %s, type %s), %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 sb->s_id, sb->s_type->name,
397 labeling_behaviors[sbsec->behavior-1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
David P. Quigley11689d42009-01-16 09:22:03 -0500399 if (sbsec->behavior == SECURITY_FS_USE_GENFS ||
400 sbsec->behavior == SECURITY_FS_USE_MNTPOINT ||
401 sbsec->behavior == SECURITY_FS_USE_NONE ||
402 sbsec->behavior > ARRAY_SIZE(labeling_behaviors))
403 sbsec->flags &= ~SE_SBLABELSUPP;
404
Mark Salyzyn2c088052015-01-07 09:27:15 -0800405 /* Special handling. Is genfs but also has in-core setxattr handler*/
406 if (!strcmp(sb->s_type->name, "sysfs") ||
407 !strcmp(sb->s_type->name, "pstore") ||
408 !strcmp(sb->s_type->name, "debugfs") ||
409 !strcmp(sb->s_type->name, "rootfs"))
David P. Quigleyddd29ec2009-09-09 14:25:37 -0400410 sbsec->flags |= SE_SBLABELSUPP;
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 /* Initialize the root inode. */
Eric Parisc9180a52007-11-30 13:00:35 -0500413 rc = inode_doinit_with_dentry(root_inode, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 /* Initialize any other inodes associated with the superblock, e.g.
416 inodes created prior to initial policy load or inodes created
417 during get_sb by a pseudo filesystem that directly
418 populates itself. */
419 spin_lock(&sbsec->isec_lock);
420next_inode:
421 if (!list_empty(&sbsec->isec_head)) {
422 struct inode_security_struct *isec =
423 list_entry(sbsec->isec_head.next,
Eric Parisc9180a52007-11-30 13:00:35 -0500424 struct inode_security_struct, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 struct inode *inode = isec->inode;
426 spin_unlock(&sbsec->isec_lock);
427 inode = igrab(inode);
428 if (inode) {
Eric Parisc9180a52007-11-30 13:00:35 -0500429 if (!IS_PRIVATE(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 inode_doinit(inode);
431 iput(inode);
432 }
433 spin_lock(&sbsec->isec_lock);
434 list_del_init(&isec->list);
435 goto next_inode;
436 }
437 spin_unlock(&sbsec->isec_lock);
438out:
Eric Parisc9180a52007-11-30 13:00:35 -0500439 return rc;
440}
441
442/*
443 * This function should allow an FS to ask what it's mount security
444 * options were so it can use those later for submounts, displaying
445 * mount options, or whatever.
446 */
447static int selinux_get_mnt_opts(const struct super_block *sb,
Eric Parise0007522008-03-05 10:31:54 -0500448 struct security_mnt_opts *opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500449{
450 int rc = 0, i;
451 struct superblock_security_struct *sbsec = sb->s_security;
452 char *context = NULL;
453 u32 len;
454 char tmp;
455
Eric Parise0007522008-03-05 10:31:54 -0500456 security_init_mnt_opts(opts);
Eric Parisc9180a52007-11-30 13:00:35 -0500457
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500458 if (!(sbsec->flags & SE_SBINITIALIZED))
Eric Parisc9180a52007-11-30 13:00:35 -0500459 return -EINVAL;
460
461 if (!ss_initialized)
462 return -EINVAL;
463
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500464 tmp = sbsec->flags & SE_MNTMASK;
Eric Parisc9180a52007-11-30 13:00:35 -0500465 /* count the number of mount options for this sb */
466 for (i = 0; i < 8; i++) {
467 if (tmp & 0x01)
Eric Parise0007522008-03-05 10:31:54 -0500468 opts->num_mnt_opts++;
Eric Parisc9180a52007-11-30 13:00:35 -0500469 tmp >>= 1;
470 }
David P. Quigley11689d42009-01-16 09:22:03 -0500471 /* Check if the Label support flag is set */
472 if (sbsec->flags & SE_SBLABELSUPP)
473 opts->num_mnt_opts++;
Eric Parisc9180a52007-11-30 13:00:35 -0500474
Eric Parise0007522008-03-05 10:31:54 -0500475 opts->mnt_opts = kcalloc(opts->num_mnt_opts, sizeof(char *), GFP_ATOMIC);
476 if (!opts->mnt_opts) {
Eric Parisc9180a52007-11-30 13:00:35 -0500477 rc = -ENOMEM;
478 goto out_free;
479 }
480
Eric Parise0007522008-03-05 10:31:54 -0500481 opts->mnt_opts_flags = kcalloc(opts->num_mnt_opts, sizeof(int), GFP_ATOMIC);
482 if (!opts->mnt_opts_flags) {
Eric Parisc9180a52007-11-30 13:00:35 -0500483 rc = -ENOMEM;
484 goto out_free;
485 }
486
487 i = 0;
488 if (sbsec->flags & FSCONTEXT_MNT) {
489 rc = security_sid_to_context(sbsec->sid, &context, &len);
490 if (rc)
491 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500492 opts->mnt_opts[i] = context;
493 opts->mnt_opts_flags[i++] = FSCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500494 }
495 if (sbsec->flags & CONTEXT_MNT) {
496 rc = security_sid_to_context(sbsec->mntpoint_sid, &context, &len);
497 if (rc)
498 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500499 opts->mnt_opts[i] = context;
500 opts->mnt_opts_flags[i++] = CONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500501 }
502 if (sbsec->flags & DEFCONTEXT_MNT) {
503 rc = security_sid_to_context(sbsec->def_sid, &context, &len);
504 if (rc)
505 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500506 opts->mnt_opts[i] = context;
507 opts->mnt_opts_flags[i++] = DEFCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500508 }
509 if (sbsec->flags & ROOTCONTEXT_MNT) {
510 struct inode *root = sbsec->sb->s_root->d_inode;
511 struct inode_security_struct *isec = root->i_security;
512
513 rc = security_sid_to_context(isec->sid, &context, &len);
514 if (rc)
515 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500516 opts->mnt_opts[i] = context;
517 opts->mnt_opts_flags[i++] = ROOTCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500518 }
David P. Quigley11689d42009-01-16 09:22:03 -0500519 if (sbsec->flags & SE_SBLABELSUPP) {
520 opts->mnt_opts[i] = NULL;
521 opts->mnt_opts_flags[i++] = SE_SBLABELSUPP;
522 }
Eric Parisc9180a52007-11-30 13:00:35 -0500523
Eric Parise0007522008-03-05 10:31:54 -0500524 BUG_ON(i != opts->num_mnt_opts);
Eric Parisc9180a52007-11-30 13:00:35 -0500525
526 return 0;
527
528out_free:
Eric Parise0007522008-03-05 10:31:54 -0500529 security_free_mnt_opts(opts);
Eric Parisc9180a52007-11-30 13:00:35 -0500530 return rc;
531}
532
533static int bad_option(struct superblock_security_struct *sbsec, char flag,
534 u32 old_sid, u32 new_sid)
535{
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500536 char mnt_flags = sbsec->flags & SE_MNTMASK;
537
Eric Parisc9180a52007-11-30 13:00:35 -0500538 /* check if the old mount command had the same options */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500539 if (sbsec->flags & SE_SBINITIALIZED)
Eric Parisc9180a52007-11-30 13:00:35 -0500540 if (!(sbsec->flags & flag) ||
541 (old_sid != new_sid))
542 return 1;
543
544 /* check if we were passed the same options twice,
545 * aka someone passed context=a,context=b
546 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500547 if (!(sbsec->flags & SE_SBINITIALIZED))
548 if (mnt_flags & flag)
Eric Parisc9180a52007-11-30 13:00:35 -0500549 return 1;
550 return 0;
551}
Eric Parise0007522008-03-05 10:31:54 -0500552
Eric Parisc9180a52007-11-30 13:00:35 -0500553/*
554 * Allow filesystems with binary mount data to explicitly set mount point
555 * labeling information.
556 */
Eric Parise0007522008-03-05 10:31:54 -0500557static int selinux_set_mnt_opts(struct super_block *sb,
558 struct security_mnt_opts *opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500559{
David Howells275bb412008-11-14 10:39:19 +1100560 const struct cred *cred = current_cred();
Eric Parisc9180a52007-11-30 13:00:35 -0500561 int rc = 0, i;
Eric Parisc9180a52007-11-30 13:00:35 -0500562 struct superblock_security_struct *sbsec = sb->s_security;
563 const char *name = sb->s_type->name;
James Morris089be432008-07-15 18:32:49 +1000564 struct inode *inode = sbsec->sb->s_root->d_inode;
565 struct inode_security_struct *root_isec = inode->i_security;
Eric Parisc9180a52007-11-30 13:00:35 -0500566 u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
567 u32 defcontext_sid = 0;
Eric Parise0007522008-03-05 10:31:54 -0500568 char **mount_options = opts->mnt_opts;
569 int *flags = opts->mnt_opts_flags;
570 int num_opts = opts->num_mnt_opts;
Eric Parisc9180a52007-11-30 13:00:35 -0500571
572 mutex_lock(&sbsec->lock);
573
574 if (!ss_initialized) {
575 if (!num_opts) {
576 /* Defer initialization until selinux_complete_init,
577 after the initial policy is loaded and the security
578 server is ready to handle calls. */
Eric Parisc9180a52007-11-30 13:00:35 -0500579 goto out;
580 }
581 rc = -EINVAL;
Eric Paris744ba352008-04-17 11:52:44 -0400582 printk(KERN_WARNING "SELinux: Unable to set superblock options "
583 "before the security server is initialized\n");
Eric Parisc9180a52007-11-30 13:00:35 -0500584 goto out;
585 }
586
587 /*
Eric Parise0007522008-03-05 10:31:54 -0500588 * Binary mount data FS will come through this function twice. Once
589 * from an explicit call and once from the generic calls from the vfs.
590 * Since the generic VFS calls will not contain any security mount data
591 * we need to skip the double mount verification.
592 *
593 * This does open a hole in which we will not notice if the first
594 * mount using this sb set explict options and a second mount using
595 * this sb does not set any security options. (The first options
596 * will be used for both mounts)
597 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500598 if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
Eric Parise0007522008-03-05 10:31:54 -0500599 && (num_opts == 0))
Eric Parisf5269712008-05-14 11:27:45 -0400600 goto out;
Eric Parise0007522008-03-05 10:31:54 -0500601
602 /*
Eric Parisc9180a52007-11-30 13:00:35 -0500603 * parse the mount options, check if they are valid sids.
604 * also check if someone is trying to mount the same sb more
605 * than once with different security options.
606 */
607 for (i = 0; i < num_opts; i++) {
608 u32 sid;
David P. Quigley11689d42009-01-16 09:22:03 -0500609
610 if (flags[i] == SE_SBLABELSUPP)
611 continue;
Eric Parisc9180a52007-11-30 13:00:35 -0500612 rc = security_context_to_sid(mount_options[i],
613 strlen(mount_options[i]), &sid);
614 if (rc) {
615 printk(KERN_WARNING "SELinux: security_context_to_sid"
616 "(%s) failed for (dev %s, type %s) errno=%d\n",
617 mount_options[i], sb->s_id, name, rc);
618 goto out;
619 }
620 switch (flags[i]) {
621 case FSCONTEXT_MNT:
622 fscontext_sid = sid;
623
624 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
625 fscontext_sid))
626 goto out_double_mount;
627
628 sbsec->flags |= FSCONTEXT_MNT;
629 break;
630 case CONTEXT_MNT:
631 context_sid = sid;
632
633 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
634 context_sid))
635 goto out_double_mount;
636
637 sbsec->flags |= CONTEXT_MNT;
638 break;
639 case ROOTCONTEXT_MNT:
640 rootcontext_sid = sid;
641
642 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
643 rootcontext_sid))
644 goto out_double_mount;
645
646 sbsec->flags |= ROOTCONTEXT_MNT;
647
648 break;
649 case DEFCONTEXT_MNT:
650 defcontext_sid = sid;
651
652 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
653 defcontext_sid))
654 goto out_double_mount;
655
656 sbsec->flags |= DEFCONTEXT_MNT;
657
658 break;
659 default:
660 rc = -EINVAL;
661 goto out;
662 }
663 }
664
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500665 if (sbsec->flags & SE_SBINITIALIZED) {
Eric Parisc9180a52007-11-30 13:00:35 -0500666 /* previously mounted with options, but not on this attempt? */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500667 if ((sbsec->flags & SE_MNTMASK) && !num_opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500668 goto out_double_mount;
669 rc = 0;
670 goto out;
671 }
672
James Morris089be432008-07-15 18:32:49 +1000673 if (strcmp(sb->s_type->name, "proc") == 0)
Stephen Smalleyf9aecb32015-05-19 13:59:12 -0400674 sbsec->flags |= SE_SBPROC | SE_SBGENFS;
675
676 if (strcmp(sb->s_type->name, "debugfs") == 0)
677 sbsec->flags |= SE_SBGENFS;
Eric Parisc9180a52007-11-30 13:00:35 -0500678
679 /* Determine the labeling behavior to use for this filesystem type. */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500680 rc = security_fs_use((sbsec->flags & SE_SBPROC) ? "proc" : sb->s_type->name, &sbsec->behavior, &sbsec->sid);
Eric Parisc9180a52007-11-30 13:00:35 -0500681 if (rc) {
682 printk(KERN_WARNING "%s: security_fs_use(%s) returned %d\n",
James Morris089be432008-07-15 18:32:49 +1000683 __func__, sb->s_type->name, rc);
Eric Parisc9180a52007-11-30 13:00:35 -0500684 goto out;
685 }
686
687 /* sets the context of the superblock for the fs being mounted. */
688 if (fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100689 rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500690 if (rc)
691 goto out;
692
693 sbsec->sid = fscontext_sid;
694 }
695
696 /*
697 * Switch to using mount point labeling behavior.
698 * sets the label used on all file below the mountpoint, and will set
699 * the superblock context if not already set.
700 */
701 if (context_sid) {
702 if (!fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100703 rc = may_context_mount_sb_relabel(context_sid, sbsec,
704 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500705 if (rc)
706 goto out;
707 sbsec->sid = context_sid;
708 } else {
David Howells275bb412008-11-14 10:39:19 +1100709 rc = may_context_mount_inode_relabel(context_sid, sbsec,
710 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500711 if (rc)
712 goto out;
713 }
714 if (!rootcontext_sid)
715 rootcontext_sid = context_sid;
716
717 sbsec->mntpoint_sid = context_sid;
718 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
719 }
720
721 if (rootcontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100722 rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec,
723 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500724 if (rc)
725 goto out;
726
727 root_isec->sid = rootcontext_sid;
728 root_isec->initialized = 1;
729 }
730
731 if (defcontext_sid) {
732 if (sbsec->behavior != SECURITY_FS_USE_XATTR) {
733 rc = -EINVAL;
734 printk(KERN_WARNING "SELinux: defcontext option is "
735 "invalid for this filesystem type\n");
736 goto out;
737 }
738
739 if (defcontext_sid != sbsec->def_sid) {
740 rc = may_context_mount_inode_relabel(defcontext_sid,
David Howells275bb412008-11-14 10:39:19 +1100741 sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500742 if (rc)
743 goto out;
744 }
745
746 sbsec->def_sid = defcontext_sid;
747 }
748
749 rc = sb_finish_set_opts(sb);
750out:
Eric Parisbc7e9822006-09-25 23:32:02 -0700751 mutex_unlock(&sbsec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 return rc;
Eric Parisc9180a52007-11-30 13:00:35 -0500753out_double_mount:
754 rc = -EINVAL;
755 printk(KERN_WARNING "SELinux: mount invalid. Same superblock, different "
756 "security settings for (dev %s, type %s)\n", sb->s_id, name);
757 goto out;
758}
759
760static void selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
761 struct super_block *newsb)
762{
763 const struct superblock_security_struct *oldsbsec = oldsb->s_security;
764 struct superblock_security_struct *newsbsec = newsb->s_security;
765
766 int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT);
767 int set_context = (oldsbsec->flags & CONTEXT_MNT);
768 int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT);
769
Eric Paris0f5e6422008-04-21 16:24:11 -0400770 /*
771 * if the parent was able to be mounted it clearly had no special lsm
Al Viroe8c26252010-03-23 06:36:54 -0400772 * mount options. thus we can safely deal with this superblock later
Eric Paris0f5e6422008-04-21 16:24:11 -0400773 */
Al Viroe8c26252010-03-23 06:36:54 -0400774 if (!ss_initialized)
Eric Paris0f5e6422008-04-21 16:24:11 -0400775 return;
Eric Parisc9180a52007-11-30 13:00:35 -0500776
Eric Parisc9180a52007-11-30 13:00:35 -0500777 /* how can we clone if the old one wasn't set up?? */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500778 BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED));
Eric Parisc9180a52007-11-30 13:00:35 -0500779
Eric Paris5a552612008-04-09 14:08:35 -0400780 /* if fs is reusing a sb, just let its options stand... */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500781 if (newsbsec->flags & SE_SBINITIALIZED)
Eric Paris5a552612008-04-09 14:08:35 -0400782 return;
783
Eric Parisc9180a52007-11-30 13:00:35 -0500784 mutex_lock(&newsbsec->lock);
785
786 newsbsec->flags = oldsbsec->flags;
787
788 newsbsec->sid = oldsbsec->sid;
789 newsbsec->def_sid = oldsbsec->def_sid;
790 newsbsec->behavior = oldsbsec->behavior;
791
792 if (set_context) {
793 u32 sid = oldsbsec->mntpoint_sid;
794
795 if (!set_fscontext)
796 newsbsec->sid = sid;
797 if (!set_rootcontext) {
798 struct inode *newinode = newsb->s_root->d_inode;
799 struct inode_security_struct *newisec = newinode->i_security;
800 newisec->sid = sid;
801 }
802 newsbsec->mntpoint_sid = sid;
803 }
804 if (set_rootcontext) {
805 const struct inode *oldinode = oldsb->s_root->d_inode;
806 const struct inode_security_struct *oldisec = oldinode->i_security;
807 struct inode *newinode = newsb->s_root->d_inode;
808 struct inode_security_struct *newisec = newinode->i_security;
809
810 newisec->sid = oldisec->sid;
811 }
812
813 sb_finish_set_opts(newsb);
814 mutex_unlock(&newsbsec->lock);
815}
816
Adrian Bunk2e1479d2008-03-17 22:29:23 +0200817static int selinux_parse_opts_str(char *options,
818 struct security_mnt_opts *opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500819{
Eric Parise0007522008-03-05 10:31:54 -0500820 char *p;
Eric Parisc9180a52007-11-30 13:00:35 -0500821 char *context = NULL, *defcontext = NULL;
822 char *fscontext = NULL, *rootcontext = NULL;
Eric Parise0007522008-03-05 10:31:54 -0500823 int rc, num_mnt_opts = 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500824
Eric Parise0007522008-03-05 10:31:54 -0500825 opts->num_mnt_opts = 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500826
827 /* Standard string-based options. */
828 while ((p = strsep(&options, "|")) != NULL) {
829 int token;
830 substring_t args[MAX_OPT_ARGS];
831
832 if (!*p)
833 continue;
834
835 token = match_token(p, tokens, args);
836
837 switch (token) {
838 case Opt_context:
839 if (context || defcontext) {
840 rc = -EINVAL;
841 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
842 goto out_err;
843 }
844 context = match_strdup(&args[0]);
845 if (!context) {
846 rc = -ENOMEM;
847 goto out_err;
848 }
849 break;
850
851 case Opt_fscontext:
852 if (fscontext) {
853 rc = -EINVAL;
854 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
855 goto out_err;
856 }
857 fscontext = match_strdup(&args[0]);
858 if (!fscontext) {
859 rc = -ENOMEM;
860 goto out_err;
861 }
862 break;
863
864 case Opt_rootcontext:
865 if (rootcontext) {
866 rc = -EINVAL;
867 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
868 goto out_err;
869 }
870 rootcontext = match_strdup(&args[0]);
871 if (!rootcontext) {
872 rc = -ENOMEM;
873 goto out_err;
874 }
875 break;
876
877 case Opt_defcontext:
878 if (context || defcontext) {
879 rc = -EINVAL;
880 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
881 goto out_err;
882 }
883 defcontext = match_strdup(&args[0]);
884 if (!defcontext) {
885 rc = -ENOMEM;
886 goto out_err;
887 }
888 break;
David P. Quigley11689d42009-01-16 09:22:03 -0500889 case Opt_labelsupport:
890 break;
Eric Parisc9180a52007-11-30 13:00:35 -0500891 default:
892 rc = -EINVAL;
893 printk(KERN_WARNING "SELinux: unknown mount option\n");
894 goto out_err;
895
896 }
897 }
898
Eric Parise0007522008-03-05 10:31:54 -0500899 rc = -ENOMEM;
900 opts->mnt_opts = kcalloc(NUM_SEL_MNT_OPTS, sizeof(char *), GFP_ATOMIC);
901 if (!opts->mnt_opts)
902 goto out_err;
903
904 opts->mnt_opts_flags = kcalloc(NUM_SEL_MNT_OPTS, sizeof(int), GFP_ATOMIC);
905 if (!opts->mnt_opts_flags) {
906 kfree(opts->mnt_opts);
907 goto out_err;
Eric Parisc9180a52007-11-30 13:00:35 -0500908 }
909
Eric Parise0007522008-03-05 10:31:54 -0500910 if (fscontext) {
911 opts->mnt_opts[num_mnt_opts] = fscontext;
912 opts->mnt_opts_flags[num_mnt_opts++] = FSCONTEXT_MNT;
913 }
914 if (context) {
915 opts->mnt_opts[num_mnt_opts] = context;
916 opts->mnt_opts_flags[num_mnt_opts++] = CONTEXT_MNT;
917 }
918 if (rootcontext) {
919 opts->mnt_opts[num_mnt_opts] = rootcontext;
920 opts->mnt_opts_flags[num_mnt_opts++] = ROOTCONTEXT_MNT;
921 }
922 if (defcontext) {
923 opts->mnt_opts[num_mnt_opts] = defcontext;
924 opts->mnt_opts_flags[num_mnt_opts++] = DEFCONTEXT_MNT;
925 }
926
927 opts->num_mnt_opts = num_mnt_opts;
928 return 0;
929
Eric Parisc9180a52007-11-30 13:00:35 -0500930out_err:
931 kfree(context);
932 kfree(defcontext);
933 kfree(fscontext);
934 kfree(rootcontext);
935 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936}
Eric Parise0007522008-03-05 10:31:54 -0500937/*
938 * string mount options parsing and call set the sbsec
939 */
940static int superblock_doinit(struct super_block *sb, void *data)
941{
942 int rc = 0;
943 char *options = data;
944 struct security_mnt_opts opts;
945
946 security_init_mnt_opts(&opts);
947
948 if (!data)
949 goto out;
950
951 BUG_ON(sb->s_type->fs_flags & FS_BINARY_MOUNTDATA);
952
953 rc = selinux_parse_opts_str(options, &opts);
954 if (rc)
955 goto out_err;
956
957out:
958 rc = selinux_set_mnt_opts(sb, &opts);
959
960out_err:
961 security_free_mnt_opts(&opts);
962 return rc;
963}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Adrian Bunk3583a712008-07-22 20:21:23 +0300965static void selinux_write_opts(struct seq_file *m,
966 struct security_mnt_opts *opts)
Eric Paris2069f452008-07-04 09:47:13 +1000967{
968 int i;
969 char *prefix;
970
971 for (i = 0; i < opts->num_mnt_opts; i++) {
David P. Quigley11689d42009-01-16 09:22:03 -0500972 char *has_comma;
973
974 if (opts->mnt_opts[i])
975 has_comma = strchr(opts->mnt_opts[i], ',');
976 else
977 has_comma = NULL;
Eric Paris2069f452008-07-04 09:47:13 +1000978
979 switch (opts->mnt_opts_flags[i]) {
980 case CONTEXT_MNT:
981 prefix = CONTEXT_STR;
982 break;
983 case FSCONTEXT_MNT:
984 prefix = FSCONTEXT_STR;
985 break;
986 case ROOTCONTEXT_MNT:
987 prefix = ROOTCONTEXT_STR;
988 break;
989 case DEFCONTEXT_MNT:
990 prefix = DEFCONTEXT_STR;
991 break;
David P. Quigley11689d42009-01-16 09:22:03 -0500992 case SE_SBLABELSUPP:
993 seq_putc(m, ',');
994 seq_puts(m, LABELSUPP_STR);
995 continue;
Eric Paris2069f452008-07-04 09:47:13 +1000996 default:
997 BUG();
Eric Parisa35c6c82011-04-20 10:21:28 -0400998 return;
Eric Paris2069f452008-07-04 09:47:13 +1000999 };
1000 /* we need a comma before each option */
1001 seq_putc(m, ',');
1002 seq_puts(m, prefix);
1003 if (has_comma)
1004 seq_putc(m, '\"');
1005 seq_puts(m, opts->mnt_opts[i]);
1006 if (has_comma)
1007 seq_putc(m, '\"');
1008 }
1009}
1010
1011static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
1012{
1013 struct security_mnt_opts opts;
1014 int rc;
1015
1016 rc = selinux_get_mnt_opts(sb, &opts);
Eric Paris383795c2008-07-29 17:07:26 -04001017 if (rc) {
1018 /* before policy load we may get EINVAL, don't show anything */
1019 if (rc == -EINVAL)
1020 rc = 0;
Eric Paris2069f452008-07-04 09:47:13 +10001021 return rc;
Eric Paris383795c2008-07-29 17:07:26 -04001022 }
Eric Paris2069f452008-07-04 09:47:13 +10001023
1024 selinux_write_opts(m, &opts);
1025
1026 security_free_mnt_opts(&opts);
1027
1028 return rc;
1029}
1030
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031static inline u16 inode_mode_to_security_class(umode_t mode)
1032{
1033 switch (mode & S_IFMT) {
1034 case S_IFSOCK:
1035 return SECCLASS_SOCK_FILE;
1036 case S_IFLNK:
1037 return SECCLASS_LNK_FILE;
1038 case S_IFREG:
1039 return SECCLASS_FILE;
1040 case S_IFBLK:
1041 return SECCLASS_BLK_FILE;
1042 case S_IFDIR:
1043 return SECCLASS_DIR;
1044 case S_IFCHR:
1045 return SECCLASS_CHR_FILE;
1046 case S_IFIFO:
1047 return SECCLASS_FIFO_FILE;
1048
1049 }
1050
1051 return SECCLASS_FILE;
1052}
1053
James Morris13402582005-09-30 14:24:34 -04001054static inline int default_protocol_stream(int protocol)
1055{
1056 return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP);
1057}
1058
1059static inline int default_protocol_dgram(int protocol)
1060{
1061 return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
1062}
1063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064static inline u16 socket_type_to_security_class(int family, int type, int protocol)
1065{
1066 switch (family) {
1067 case PF_UNIX:
1068 switch (type) {
1069 case SOCK_STREAM:
1070 case SOCK_SEQPACKET:
1071 return SECCLASS_UNIX_STREAM_SOCKET;
1072 case SOCK_DGRAM:
1073 return SECCLASS_UNIX_DGRAM_SOCKET;
1074 }
1075 break;
1076 case PF_INET:
1077 case PF_INET6:
1078 switch (type) {
1079 case SOCK_STREAM:
James Morris13402582005-09-30 14:24:34 -04001080 if (default_protocol_stream(protocol))
1081 return SECCLASS_TCP_SOCKET;
1082 else
1083 return SECCLASS_RAWIP_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 case SOCK_DGRAM:
James Morris13402582005-09-30 14:24:34 -04001085 if (default_protocol_dgram(protocol))
1086 return SECCLASS_UDP_SOCKET;
1087 else
1088 return SECCLASS_RAWIP_SOCKET;
James Morris2ee92d42006-11-13 16:09:01 -08001089 case SOCK_DCCP:
1090 return SECCLASS_DCCP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001091 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 return SECCLASS_RAWIP_SOCKET;
1093 }
1094 break;
1095 case PF_NETLINK:
1096 switch (protocol) {
1097 case NETLINK_ROUTE:
1098 return SECCLASS_NETLINK_ROUTE_SOCKET;
1099 case NETLINK_FIREWALL:
1100 return SECCLASS_NETLINK_FIREWALL_SOCKET;
Pavel Emelyanov7f1fb602011-12-06 07:56:43 +00001101 case NETLINK_SOCK_DIAG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 return SECCLASS_NETLINK_TCPDIAG_SOCKET;
1103 case NETLINK_NFLOG:
1104 return SECCLASS_NETLINK_NFLOG_SOCKET;
1105 case NETLINK_XFRM:
1106 return SECCLASS_NETLINK_XFRM_SOCKET;
1107 case NETLINK_SELINUX:
1108 return SECCLASS_NETLINK_SELINUX_SOCKET;
1109 case NETLINK_AUDIT:
1110 return SECCLASS_NETLINK_AUDIT_SOCKET;
1111 case NETLINK_IP6_FW:
1112 return SECCLASS_NETLINK_IP6FW_SOCKET;
1113 case NETLINK_DNRTMSG:
1114 return SECCLASS_NETLINK_DNRT_SOCKET;
James Morris0c9b7942005-04-16 15:24:13 -07001115 case NETLINK_KOBJECT_UEVENT:
1116 return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 default:
1118 return SECCLASS_NETLINK_SOCKET;
1119 }
1120 case PF_PACKET:
1121 return SECCLASS_PACKET_SOCKET;
1122 case PF_KEY:
1123 return SECCLASS_KEY_SOCKET;
Christopher J. PeBenito3e3ff152006-06-09 00:25:03 -07001124 case PF_APPLETALK:
1125 return SECCLASS_APPLETALK_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 }
1127
1128 return SECCLASS_SOCKET;
1129}
1130
Stephen Smalleyf9aecb32015-05-19 13:59:12 -04001131static int selinux_genfs_get_sid(struct dentry *dentry,
1132 u16 tclass,
1133 u16 flags,
1134 u32 *sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001136 int rc;
Stephen Smalleyf9aecb32015-05-19 13:59:12 -04001137 struct super_block *sb = dentry->d_inode->i_sb;
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001138 char *buffer, *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Eric Paris828dfe12008-04-17 13:17:49 -04001140 buffer = (char *)__get_free_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 if (!buffer)
1142 return -ENOMEM;
1143
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001144 path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
1145 if (IS_ERR(path))
1146 rc = PTR_ERR(path);
1147 else {
Stephen Smalleyf9aecb32015-05-19 13:59:12 -04001148 if (flags & SE_SBPROC) {
1149 /* each process gets a /proc/PID/ entry. Strip off the
1150 * PID part to get a valid selinux labeling.
1151 * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
1152 while (path[1] >= '0' && path[1] <= '9') {
1153 path[1] = '/';
1154 path++;
1155 }
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001156 }
Stephen Smalleyf9aecb32015-05-19 13:59:12 -04001157 rc = security_genfs_sid(sb->s_type->name, path, tclass, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 free_page((unsigned long)buffer);
1160 return rc;
1161}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
1163/* The inode's security attributes must be initialized before first use. */
1164static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
1165{
1166 struct superblock_security_struct *sbsec = NULL;
1167 struct inode_security_struct *isec = inode->i_security;
1168 u32 sid;
1169 struct dentry *dentry;
1170#define INITCONTEXTLEN 255
1171 char *context = NULL;
1172 unsigned len = 0;
1173 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
1175 if (isec->initialized)
1176 goto out;
1177
Eric Paris23970742006-09-25 23:32:01 -07001178 mutex_lock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 if (isec->initialized)
Eric Paris23970742006-09-25 23:32:01 -07001180 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182 sbsec = inode->i_sb->s_security;
David P. Quigley0d90a7e2009-01-16 09:22:02 -05001183 if (!(sbsec->flags & SE_SBINITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 /* Defer initialization until selinux_complete_init,
1185 after the initial policy is loaded and the security
1186 server is ready to handle calls. */
1187 spin_lock(&sbsec->isec_lock);
1188 if (list_empty(&isec->list))
1189 list_add(&isec->list, &sbsec->isec_head);
1190 spin_unlock(&sbsec->isec_lock);
Eric Paris23970742006-09-25 23:32:01 -07001191 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 }
1193
1194 switch (sbsec->behavior) {
1195 case SECURITY_FS_USE_XATTR:
1196 if (!inode->i_op->getxattr) {
1197 isec->sid = sbsec->def_sid;
1198 break;
1199 }
1200
1201 /* Need a dentry, since the xattr API requires one.
1202 Life would be simpler if we could just pass the inode. */
1203 if (opt_dentry) {
1204 /* Called from d_instantiate or d_splice_alias. */
1205 dentry = dget(opt_dentry);
1206 } else {
1207 /* Called from selinux_complete_init, try to find a dentry. */
1208 dentry = d_find_alias(inode);
1209 }
1210 if (!dentry) {
Eric Parisdf7f54c2009-03-09 14:35:58 -04001211 /*
1212 * this is can be hit on boot when a file is accessed
1213 * before the policy is loaded. When we load policy we
1214 * may find inodes that have no dentry on the
1215 * sbsec->isec_head list. No reason to complain as these
1216 * will get fixed up the next time we go through
1217 * inode_doinit with a dentry, before these inodes could
1218 * be used again by userspace.
1219 */
Eric Paris23970742006-09-25 23:32:01 -07001220 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 }
1222
1223 len = INITCONTEXTLEN;
Eric Paris4cb912f2009-02-12 14:50:05 -05001224 context = kmalloc(len+1, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 if (!context) {
1226 rc = -ENOMEM;
1227 dput(dentry);
Eric Paris23970742006-09-25 23:32:01 -07001228 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 }
Eric Paris4cb912f2009-02-12 14:50:05 -05001230 context[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX,
1232 context, len);
1233 if (rc == -ERANGE) {
James Morris314dabb2009-08-10 22:00:13 +10001234 kfree(context);
1235
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 /* Need a larger buffer. Query for the right size. */
1237 rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX,
1238 NULL, 0);
1239 if (rc < 0) {
1240 dput(dentry);
Eric Paris23970742006-09-25 23:32:01 -07001241 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 len = rc;
Eric Paris4cb912f2009-02-12 14:50:05 -05001244 context = kmalloc(len+1, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 if (!context) {
1246 rc = -ENOMEM;
1247 dput(dentry);
Eric Paris23970742006-09-25 23:32:01 -07001248 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 }
Eric Paris4cb912f2009-02-12 14:50:05 -05001250 context[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 rc = inode->i_op->getxattr(dentry,
1252 XATTR_NAME_SELINUX,
1253 context, len);
1254 }
1255 dput(dentry);
1256 if (rc < 0) {
1257 if (rc != -ENODATA) {
Eric Paris744ba352008-04-17 11:52:44 -04001258 printk(KERN_WARNING "SELinux: %s: getxattr returned "
Harvey Harrisondd6f9532008-03-06 10:03:59 +11001259 "%d for dev=%s ino=%ld\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 -rc, inode->i_sb->s_id, inode->i_ino);
1261 kfree(context);
Eric Paris23970742006-09-25 23:32:01 -07001262 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 }
1264 /* Map ENODATA to the default file SID */
1265 sid = sbsec->def_sid;
1266 rc = 0;
1267 } else {
James Morrisf5c1d5b2005-07-28 01:07:37 -07001268 rc = security_context_to_sid_default(context, rc, &sid,
Stephen Smalley869ab512008-04-04 08:46:05 -04001269 sbsec->def_sid,
1270 GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 if (rc) {
Eric Paris4ba0a8a2009-02-12 15:01:10 -05001272 char *dev = inode->i_sb->s_id;
1273 unsigned long ino = inode->i_ino;
1274
1275 if (rc == -EINVAL) {
1276 if (printk_ratelimit())
1277 printk(KERN_NOTICE "SELinux: inode=%lu on dev=%s was found to have an invalid "
1278 "context=%s. This indicates you may need to relabel the inode or the "
1279 "filesystem in question.\n", ino, dev, context);
1280 } else {
1281 printk(KERN_WARNING "SELinux: %s: context_to_sid(%s) "
1282 "returned %d for dev=%s ino=%ld\n",
1283 __func__, context, -rc, dev, ino);
1284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 kfree(context);
1286 /* Leave with the unlabeled SID */
1287 rc = 0;
1288 break;
1289 }
1290 }
1291 kfree(context);
1292 isec->sid = sid;
1293 break;
1294 case SECURITY_FS_USE_TASK:
1295 isec->sid = isec->task_sid;
1296 break;
1297 case SECURITY_FS_USE_TRANS:
1298 /* Default to the fs SID. */
1299 isec->sid = sbsec->sid;
1300
1301 /* Try to obtain a transition SID. */
1302 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Eric Paris652bb9b2011-02-01 11:05:40 -05001303 rc = security_transition_sid(isec->task_sid, sbsec->sid,
1304 isec->sclass, NULL, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 if (rc)
Eric Paris23970742006-09-25 23:32:01 -07001306 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 isec->sid = sid;
1308 break;
Eric Parisc312feb2006-07-10 04:43:53 -07001309 case SECURITY_FS_USE_MNTPOINT:
1310 isec->sid = sbsec->mntpoint_sid;
1311 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 default:
Eric Parisc312feb2006-07-10 04:43:53 -07001313 /* Default to the fs superblock SID. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 isec->sid = sbsec->sid;
1315
Stephen Smalleyf9aecb32015-05-19 13:59:12 -04001316 if ((sbsec->flags & SE_SBGENFS) && !S_ISLNK(inode->i_mode)) {
Paul Moore810be002014-03-19 16:46:18 -04001317 /* We must have a dentry to determine the label on
1318 * procfs inodes */
1319 if (opt_dentry)
1320 /* Called from d_instantiate or
1321 * d_splice_alias. */
1322 dentry = dget(opt_dentry);
1323 else
1324 /* Called from selinux_complete_init, try to
1325 * find a dentry. */
1326 dentry = d_find_alias(inode);
1327 /*
1328 * This can be hit on boot when a file is accessed
1329 * before the policy is loaded. When we load policy we
1330 * may find inodes that have no dentry on the
1331 * sbsec->isec_head list. No reason to complain as
1332 * these will get fixed up the next time we go through
1333 * inode_doinit() with a dentry, before these inodes
1334 * could be used again by userspace.
1335 */
1336 if (!dentry)
1337 goto out_unlock;
1338 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Stephen Smalleyf9aecb32015-05-19 13:59:12 -04001339 rc = selinux_genfs_get_sid(dentry, isec->sclass,
1340 sbsec->flags, &sid);
Paul Moore810be002014-03-19 16:46:18 -04001341 dput(dentry);
1342 if (rc)
1343 goto out_unlock;
1344 isec->sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 }
1346 break;
1347 }
1348
1349 isec->initialized = 1;
1350
Eric Paris23970742006-09-25 23:32:01 -07001351out_unlock:
1352 mutex_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353out:
1354 if (isec->sclass == SECCLASS_FILE)
1355 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 return rc;
1357}
1358
1359/* Convert a Linux signal to an access vector. */
1360static inline u32 signal_to_av(int sig)
1361{
1362 u32 perm = 0;
1363
1364 switch (sig) {
1365 case SIGCHLD:
1366 /* Commonly granted from child to parent. */
1367 perm = PROCESS__SIGCHLD;
1368 break;
1369 case SIGKILL:
1370 /* Cannot be caught or ignored */
1371 perm = PROCESS__SIGKILL;
1372 break;
1373 case SIGSTOP:
1374 /* Cannot be caught or ignored */
1375 perm = PROCESS__SIGSTOP;
1376 break;
1377 default:
1378 /* All other signals. */
1379 perm = PROCESS__SIGNAL;
1380 break;
1381 }
1382
1383 return perm;
1384}
1385
David Howells275bb412008-11-14 10:39:19 +11001386/*
David Howellsd84f4f92008-11-14 10:39:23 +11001387 * Check permission between a pair of credentials
1388 * fork check, ptrace check, etc.
1389 */
1390static int cred_has_perm(const struct cred *actor,
1391 const struct cred *target,
1392 u32 perms)
1393{
1394 u32 asid = cred_sid(actor), tsid = cred_sid(target);
1395
1396 return avc_has_perm(asid, tsid, SECCLASS_PROCESS, perms, NULL);
1397}
1398
1399/*
David Howells88e67f32008-11-14 10:39:21 +11001400 * Check permission between a pair of tasks, e.g. signal checks,
David Howells275bb412008-11-14 10:39:19 +11001401 * fork check, ptrace check, etc.
1402 * tsk1 is the actor and tsk2 is the target
David Howells3b11a1d2008-11-14 10:39:26 +11001403 * - this uses the default subjective creds of tsk1
David Howells275bb412008-11-14 10:39:19 +11001404 */
1405static int task_has_perm(const struct task_struct *tsk1,
1406 const struct task_struct *tsk2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 u32 perms)
1408{
David Howells275bb412008-11-14 10:39:19 +11001409 const struct task_security_struct *__tsec1, *__tsec2;
1410 u32 sid1, sid2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
David Howells275bb412008-11-14 10:39:19 +11001412 rcu_read_lock();
1413 __tsec1 = __task_cred(tsk1)->security; sid1 = __tsec1->sid;
1414 __tsec2 = __task_cred(tsk2)->security; sid2 = __tsec2->sid;
1415 rcu_read_unlock();
1416 return avc_has_perm(sid1, sid2, SECCLASS_PROCESS, perms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417}
1418
David Howells3b11a1d2008-11-14 10:39:26 +11001419/*
1420 * Check permission between current and another task, e.g. signal checks,
1421 * fork check, ptrace check, etc.
1422 * current is the actor and tsk2 is the target
1423 * - this uses current's subjective creds
1424 */
1425static int current_has_perm(const struct task_struct *tsk,
1426 u32 perms)
1427{
1428 u32 sid, tsid;
1429
1430 sid = current_sid();
1431 tsid = task_sid(tsk);
1432 return avc_has_perm(sid, tsid, SECCLASS_PROCESS, perms, NULL);
1433}
1434
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001435#if CAP_LAST_CAP > 63
1436#error Fix SELinux to handle capabilities > 63.
1437#endif
1438
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439/* Check whether a task is allowed to use a capability. */
Eric Paris6a9de492012-01-03 12:25:14 -05001440static int cred_has_capability(const struct cred *cred,
Eric Paris06112162008-11-11 22:02:50 +11001441 int cap, int audit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442{
Thomas Liu2bf49692009-07-14 12:14:09 -04001443 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001444 struct selinux_audit_data sad = {0,};
Eric Paris06112162008-11-11 22:02:50 +11001445 struct av_decision avd;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001446 u16 sclass;
David Howells3699c532009-01-06 22:27:01 +00001447 u32 sid = cred_sid(cred);
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001448 u32 av = CAP_TO_MASK(cap);
Eric Paris06112162008-11-11 22:02:50 +11001449 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Thomas Liu2bf49692009-07-14 12:14:09 -04001451 COMMON_AUDIT_DATA_INIT(&ad, CAP);
Eric Paris3b3b0e42012-04-03 09:37:02 -07001452 ad.selinux_audit_data = &sad;
Eric Paris6a9de492012-01-03 12:25:14 -05001453 ad.tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 ad.u.cap = cap;
1455
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001456 switch (CAP_TO_INDEX(cap)) {
1457 case 0:
1458 sclass = SECCLASS_CAPABILITY;
1459 break;
1460 case 1:
1461 sclass = SECCLASS_CAPABILITY2;
1462 break;
1463 default:
1464 printk(KERN_ERR
1465 "SELinux: out of range capability %d\n", cap);
1466 BUG();
Eric Parisa35c6c82011-04-20 10:21:28 -04001467 return -EINVAL;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001468 }
Eric Paris06112162008-11-11 22:02:50 +11001469
David Howells275bb412008-11-14 10:39:19 +11001470 rc = avc_has_perm_noaudit(sid, sid, sclass, av, 0, &avd);
Eric Paris9ade0cf2011-04-25 16:26:29 -04001471 if (audit == SECURITY_CAP_AUDIT) {
1472 int rc2 = avc_audit(sid, sid, sclass, av, &avd, rc, &ad, 0);
1473 if (rc2)
1474 return rc2;
1475 }
Eric Paris06112162008-11-11 22:02:50 +11001476 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477}
1478
1479/* Check whether a task is allowed to use a system operation. */
1480static int task_has_system(struct task_struct *tsk,
1481 u32 perms)
1482{
David Howells275bb412008-11-14 10:39:19 +11001483 u32 sid = task_sid(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
David Howells275bb412008-11-14 10:39:19 +11001485 return avc_has_perm(sid, SECINITSID_KERNEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 SECCLASS_SYSTEM, perms, NULL);
1487}
1488
1489/* Check whether a task has a particular permission to an inode.
1490 The 'adp' parameter is optional and allows other audit
1491 data to be passed (e.g. the dentry). */
David Howells88e67f32008-11-14 10:39:21 +11001492static int inode_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 struct inode *inode,
1494 u32 perms,
Eric Paris9ade0cf2011-04-25 16:26:29 -04001495 struct common_audit_data *adp,
1496 unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 struct inode_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +11001499 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
David Howellse0e81732009-09-02 09:13:40 +01001501 validate_creds(cred);
1502
Eric Paris828dfe12008-04-17 13:17:49 -04001503 if (unlikely(IS_PRIVATE(inode)))
Stephen Smalleybbaca6c2007-02-14 00:34:16 -08001504 return 0;
1505
David Howells88e67f32008-11-14 10:39:21 +11001506 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 isec = inode->i_security;
1508
Eric Paris9ade0cf2011-04-25 16:26:29 -04001509 return avc_has_perm_flags(sid, isec->sid, isec->sclass, perms, adp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510}
1511
Linus Torvalds95f4efb2011-06-08 15:11:56 -07001512static int inode_has_perm_noadp(const struct cred *cred,
1513 struct inode *inode,
1514 u32 perms,
1515 unsigned flags)
1516{
1517 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001518 struct selinux_audit_data sad = {0,};
Linus Torvalds95f4efb2011-06-08 15:11:56 -07001519
1520 COMMON_AUDIT_DATA_INIT(&ad, INODE);
1521 ad.u.inode = inode;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001522 ad.selinux_audit_data = &sad;
Linus Torvalds95f4efb2011-06-08 15:11:56 -07001523 return inode_has_perm(cred, inode, perms, &ad, flags);
1524}
1525
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526/* Same as inode_has_perm, but pass explicit audit data containing
1527 the dentry to help the auditing code to more easily generate the
1528 pathname if needed. */
David Howells88e67f32008-11-14 10:39:21 +11001529static inline int dentry_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 struct dentry *dentry,
1531 u32 av)
1532{
1533 struct inode *inode = dentry->d_inode;
Thomas Liu2bf49692009-07-14 12:14:09 -04001534 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001535 struct selinux_audit_data sad = {0,};
David Howells88e67f32008-11-14 10:39:21 +11001536
Eric Paris2875fa02011-04-28 16:04:24 -04001537 COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
1538 ad.u.dentry = dentry;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001539 ad.selinux_audit_data = &sad;
Eric Paris2875fa02011-04-28 16:04:24 -04001540 return inode_has_perm(cred, inode, av, &ad, 0);
1541}
1542
1543/* Same as inode_has_perm, but pass explicit audit data containing
1544 the path to help the auditing code to more easily generate the
1545 pathname if needed. */
1546static inline int path_has_perm(const struct cred *cred,
1547 struct path *path,
1548 u32 av)
1549{
1550 struct inode *inode = path->dentry->d_inode;
1551 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001552 struct selinux_audit_data sad = {0,};
Eric Paris2875fa02011-04-28 16:04:24 -04001553
Eric Parisf48b7392011-04-25 12:54:27 -04001554 COMMON_AUDIT_DATA_INIT(&ad, PATH);
Eric Paris2875fa02011-04-28 16:04:24 -04001555 ad.u.path = *path;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001556 ad.selinux_audit_data = &sad;
Eric Paris9ade0cf2011-04-25 16:26:29 -04001557 return inode_has_perm(cred, inode, av, &ad, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558}
1559
1560/* Check whether a task can use an open file descriptor to
1561 access an inode in a given way. Check access to the
1562 descriptor itself, and then use dentry_has_perm to
1563 check a particular permission to the file.
1564 Access to the descriptor is implicitly granted if it
1565 has the same SID as the process. If av is zero, then
1566 access to the file is not checked, e.g. for cases
1567 where only the descriptor is affected like seek. */
David Howells88e67f32008-11-14 10:39:21 +11001568static int file_has_perm(const struct cred *cred,
1569 struct file *file,
1570 u32 av)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 struct file_security_struct *fsec = file->f_security;
Jan Blunck44707fd2008-02-14 19:38:33 -08001573 struct inode *inode = file->f_path.dentry->d_inode;
Thomas Liu2bf49692009-07-14 12:14:09 -04001574 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001575 struct selinux_audit_data sad = {0,};
David Howells88e67f32008-11-14 10:39:21 +11001576 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 int rc;
1578
Eric Parisf48b7392011-04-25 12:54:27 -04001579 COMMON_AUDIT_DATA_INIT(&ad, PATH);
1580 ad.u.path = file->f_path;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001581 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
David Howells275bb412008-11-14 10:39:19 +11001583 if (sid != fsec->sid) {
1584 rc = avc_has_perm(sid, fsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 SECCLASS_FD,
1586 FD__USE,
1587 &ad);
1588 if (rc)
David Howells88e67f32008-11-14 10:39:21 +11001589 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 }
1591
1592 /* av is zero if only checking access to the descriptor. */
David Howells88e67f32008-11-14 10:39:21 +11001593 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 if (av)
Eric Paris9ade0cf2011-04-25 16:26:29 -04001595 rc = inode_has_perm(cred, inode, av, &ad, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
David Howells88e67f32008-11-14 10:39:21 +11001597out:
1598 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599}
1600
1601/* Check whether a task can create a file. */
1602static int may_create(struct inode *dir,
1603 struct dentry *dentry,
1604 u16 tclass)
1605{
Paul Moore5fb49872010-04-22 14:46:19 -04001606 const struct task_security_struct *tsec = current_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 struct inode_security_struct *dsec;
1608 struct superblock_security_struct *sbsec;
David Howells275bb412008-11-14 10:39:19 +11001609 u32 sid, newsid;
Thomas Liu2bf49692009-07-14 12:14:09 -04001610 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001611 struct selinux_audit_data sad = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 int rc;
1613
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 dsec = dir->i_security;
1615 sbsec = dir->i_sb->s_security;
1616
David Howells275bb412008-11-14 10:39:19 +11001617 sid = tsec->sid;
1618 newsid = tsec->create_sid;
1619
Eric Parisa2694342011-04-25 13:10:27 -04001620 COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
1621 ad.u.dentry = dentry;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001622 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
David Howells275bb412008-11-14 10:39:19 +11001624 rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 DIR__ADD_NAME | DIR__SEARCH,
1626 &ad);
1627 if (rc)
1628 return rc;
1629
David P. Quigleycd895962009-01-16 09:22:04 -05001630 if (!newsid || !(sbsec->flags & SE_SBLABELSUPP)) {
Eric Pariscb1e9222011-04-28 15:11:21 -04001631 rc = security_transition_sid(sid, dsec->sid, tclass,
1632 &dentry->d_name, &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 if (rc)
1634 return rc;
1635 }
1636
David Howells275bb412008-11-14 10:39:19 +11001637 rc = avc_has_perm(sid, newsid, tclass, FILE__CREATE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 if (rc)
1639 return rc;
1640
Amir Samuelov6a22e462014-05-26 11:44:06 +03001641 rc = avc_has_perm(newsid, sbsec->sid,
1642 SECCLASS_FILESYSTEM,
1643 FILESYSTEM__ASSOCIATE, &ad);
1644 if (rc)
1645 return rc;
1646
1647 rc = pft_inode_mknod(dir, dentry, 0, 0);
1648
1649 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650}
1651
Michael LeMay4eb582c2006-06-26 00:24:57 -07001652/* Check whether a task can create a key. */
1653static int may_create_key(u32 ksid,
1654 struct task_struct *ctx)
1655{
David Howells275bb412008-11-14 10:39:19 +11001656 u32 sid = task_sid(ctx);
Michael LeMay4eb582c2006-06-26 00:24:57 -07001657
David Howells275bb412008-11-14 10:39:19 +11001658 return avc_has_perm(sid, ksid, SECCLASS_KEY, KEY__CREATE, NULL);
Michael LeMay4eb582c2006-06-26 00:24:57 -07001659}
1660
Eric Paris828dfe12008-04-17 13:17:49 -04001661#define MAY_LINK 0
1662#define MAY_UNLINK 1
1663#define MAY_RMDIR 2
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
1665/* Check whether a task can link, unlink, or rmdir a file/directory. */
1666static int may_link(struct inode *dir,
1667 struct dentry *dentry,
1668 int kind)
1669
1670{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 struct inode_security_struct *dsec, *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001672 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001673 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11001674 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 u32 av;
1676 int rc;
1677
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 dsec = dir->i_security;
1679 isec = dentry->d_inode->i_security;
1680
Eric Parisa2694342011-04-25 13:10:27 -04001681 COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
1682 ad.u.dentry = dentry;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001683 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
1685 av = DIR__SEARCH;
1686 av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
David Howells275bb412008-11-14 10:39:19 +11001687 rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 if (rc)
1689 return rc;
1690
1691 switch (kind) {
1692 case MAY_LINK:
1693 av = FILE__LINK;
1694 break;
1695 case MAY_UNLINK:
1696 av = FILE__UNLINK;
1697 break;
1698 case MAY_RMDIR:
1699 av = DIR__RMDIR;
1700 break;
1701 default:
Eric Paris744ba352008-04-17 11:52:44 -04001702 printk(KERN_WARNING "SELinux: %s: unrecognized kind %d\n",
1703 __func__, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 return 0;
1705 }
1706
David Howells275bb412008-11-14 10:39:19 +11001707 rc = avc_has_perm(sid, isec->sid, isec->sclass, av, &ad);
Amir Samuelov6a22e462014-05-26 11:44:06 +03001708 if (rc)
1709 return rc;
1710
1711 if (kind == MAY_UNLINK)
1712 rc = pft_inode_unlink(dir, dentry);
1713
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 return rc;
1715}
1716
1717static inline int may_rename(struct inode *old_dir,
1718 struct dentry *old_dentry,
1719 struct inode *new_dir,
1720 struct dentry *new_dentry)
1721{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001723 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07001724 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11001725 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 u32 av;
1727 int old_is_dir, new_is_dir;
1728 int rc;
1729
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 old_dsec = old_dir->i_security;
1731 old_isec = old_dentry->d_inode->i_security;
1732 old_is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
1733 new_dsec = new_dir->i_security;
1734
Eric Parisa2694342011-04-25 13:10:27 -04001735 COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
Eric Paris3b3b0e42012-04-03 09:37:02 -07001736 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
Eric Parisa2694342011-04-25 13:10:27 -04001738 ad.u.dentry = old_dentry;
David Howells275bb412008-11-14 10:39:19 +11001739 rc = avc_has_perm(sid, old_dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 DIR__REMOVE_NAME | DIR__SEARCH, &ad);
1741 if (rc)
1742 return rc;
David Howells275bb412008-11-14 10:39:19 +11001743 rc = avc_has_perm(sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 old_isec->sclass, FILE__RENAME, &ad);
1745 if (rc)
1746 return rc;
1747 if (old_is_dir && new_dir != old_dir) {
David Howells275bb412008-11-14 10:39:19 +11001748 rc = avc_has_perm(sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 old_isec->sclass, DIR__REPARENT, &ad);
1750 if (rc)
1751 return rc;
1752 }
1753
Eric Parisa2694342011-04-25 13:10:27 -04001754 ad.u.dentry = new_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 av = DIR__ADD_NAME | DIR__SEARCH;
1756 if (new_dentry->d_inode)
1757 av |= DIR__REMOVE_NAME;
David Howells275bb412008-11-14 10:39:19 +11001758 rc = avc_has_perm(sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 if (rc)
1760 return rc;
1761 if (new_dentry->d_inode) {
1762 new_isec = new_dentry->d_inode->i_security;
1763 new_is_dir = S_ISDIR(new_dentry->d_inode->i_mode);
David Howells275bb412008-11-14 10:39:19 +11001764 rc = avc_has_perm(sid, new_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 new_isec->sclass,
1766 (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
1767 if (rc)
1768 return rc;
1769 }
1770
1771 return 0;
1772}
1773
1774/* Check whether a task can perform a filesystem operation. */
David Howells88e67f32008-11-14 10:39:21 +11001775static int superblock_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 struct super_block *sb,
1777 u32 perms,
Thomas Liu2bf49692009-07-14 12:14:09 -04001778 struct common_audit_data *ad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 struct superblock_security_struct *sbsec;
David Howells88e67f32008-11-14 10:39:21 +11001781 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 sbsec = sb->s_security;
David Howells275bb412008-11-14 10:39:19 +11001784 return avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785}
1786
1787/* Convert a Linux mode and permission mask to an access vector. */
1788static inline u32 file_mask_to_av(int mode, int mask)
1789{
1790 u32 av = 0;
1791
Al Virodba19c62011-07-25 20:49:29 -04001792 if (!S_ISDIR(mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 if (mask & MAY_EXEC)
1794 av |= FILE__EXECUTE;
1795 if (mask & MAY_READ)
1796 av |= FILE__READ;
1797
1798 if (mask & MAY_APPEND)
1799 av |= FILE__APPEND;
1800 else if (mask & MAY_WRITE)
1801 av |= FILE__WRITE;
1802
1803 } else {
1804 if (mask & MAY_EXEC)
1805 av |= DIR__SEARCH;
1806 if (mask & MAY_WRITE)
1807 av |= DIR__WRITE;
1808 if (mask & MAY_READ)
1809 av |= DIR__READ;
1810 }
1811
1812 return av;
1813}
1814
1815/* Convert a Linux file to an access vector. */
1816static inline u32 file_to_av(struct file *file)
1817{
1818 u32 av = 0;
1819
1820 if (file->f_mode & FMODE_READ)
1821 av |= FILE__READ;
1822 if (file->f_mode & FMODE_WRITE) {
1823 if (file->f_flags & O_APPEND)
1824 av |= FILE__APPEND;
1825 else
1826 av |= FILE__WRITE;
1827 }
Stephen Smalley0794c662008-03-17 08:55:18 -04001828 if (!av) {
1829 /*
1830 * Special file opened with flags 3 for ioctl-only use.
1831 */
1832 av = FILE__IOCTL;
1833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
1835 return av;
1836}
1837
Eric Paris8b6a5a32008-10-29 17:06:46 -04001838/*
1839 * Convert a file to an access vector and include the correct open
1840 * open permission.
1841 */
1842static inline u32 open_file_to_av(struct file *file)
1843{
1844 u32 av = file_to_av(file);
1845
Eric Paris49b7b8d2010-07-23 11:44:09 -04001846 if (selinux_policycap_openperm)
1847 av |= FILE__OPEN;
1848
Eric Paris8b6a5a32008-10-29 17:06:46 -04001849 return av;
1850}
1851
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852/* Hook functions begin here. */
1853
Stephen Smalley48a23702012-11-05 08:15:34 -05001854static int selinux_binder_set_context_mgr(struct task_struct *mgr)
1855{
1856 u32 mysid = current_sid();
1857 u32 mgrsid = task_sid(mgr);
1858
1859 return avc_has_perm(mysid, mgrsid, SECCLASS_BINDER, BINDER__SET_CONTEXT_MGR, NULL);
1860}
1861
1862static int selinux_binder_transaction(struct task_struct *from, struct task_struct *to)
1863{
1864 u32 mysid = current_sid();
1865 u32 fromsid = task_sid(from);
1866 u32 tosid = task_sid(to);
1867 int rc;
1868
1869 if (mysid != fromsid) {
1870 rc = avc_has_perm(mysid, fromsid, SECCLASS_BINDER, BINDER__IMPERSONATE, NULL);
1871 if (rc)
1872 return rc;
1873 }
1874
1875 return avc_has_perm(fromsid, tosid, SECCLASS_BINDER, BINDER__CALL, NULL);
1876}
1877
1878static int selinux_binder_transfer_binder(struct task_struct *from, struct task_struct *to)
1879{
1880 u32 fromsid = task_sid(from);
1881 u32 tosid = task_sid(to);
1882 return avc_has_perm(fromsid, tosid, SECCLASS_BINDER, BINDER__TRANSFER, NULL);
1883}
1884
1885static int selinux_binder_transfer_file(struct task_struct *from, struct task_struct *to, struct file *file)
1886{
1887 u32 sid = task_sid(to);
1888 struct file_security_struct *fsec = file->f_security;
1889 struct inode *inode = file->f_path.dentry->d_inode;
1890 struct inode_security_struct *isec = inode->i_security;
1891 struct common_audit_data ad;
1892 struct selinux_audit_data sad = {0,};
1893 int rc;
1894
1895 COMMON_AUDIT_DATA_INIT(&ad, PATH);
1896 ad.u.path = file->f_path;
1897 ad.selinux_audit_data = &sad;
1898
1899 if (sid != fsec->sid) {
1900 rc = avc_has_perm(sid, fsec->sid,
1901 SECCLASS_FD,
1902 FD__USE,
1903 &ad);
1904 if (rc)
1905 return rc;
1906 }
1907
1908 if (unlikely(IS_PRIVATE(inode)))
1909 return 0;
1910
1911 return avc_has_perm(sid, isec->sid, isec->sclass, file_to_av(file),
1912 &ad);
1913}
1914
Ingo Molnar9e488582009-05-07 19:26:19 +10001915static int selinux_ptrace_access_check(struct task_struct *child,
David Howells5cd9c582008-08-14 11:37:28 +01001916 unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 int rc;
1919
Ingo Molnar9e488582009-05-07 19:26:19 +10001920 rc = cap_ptrace_access_check(child, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 if (rc)
1922 return rc;
1923
Eric Paris69f594a2012-01-03 12:25:15 -05001924 if (mode & PTRACE_MODE_READ) {
David Howells275bb412008-11-14 10:39:19 +11001925 u32 sid = current_sid();
1926 u32 csid = task_sid(child);
1927 return avc_has_perm(sid, csid, SECCLASS_FILE, FILE__READ, NULL);
Stephen Smalley006ebb42008-05-19 08:32:49 -04001928 }
1929
David Howells3b11a1d2008-11-14 10:39:26 +11001930 return current_has_perm(child, PROCESS__PTRACE);
David Howells5cd9c582008-08-14 11:37:28 +01001931}
1932
1933static int selinux_ptrace_traceme(struct task_struct *parent)
1934{
1935 int rc;
1936
Eric Paris200ac532009-02-12 15:01:04 -05001937 rc = cap_ptrace_traceme(parent);
David Howells5cd9c582008-08-14 11:37:28 +01001938 if (rc)
1939 return rc;
1940
1941 return task_has_perm(parent, current, PROCESS__PTRACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942}
1943
1944static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
Eric Paris828dfe12008-04-17 13:17:49 -04001945 kernel_cap_t *inheritable, kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946{
1947 int error;
1948
David Howells3b11a1d2008-11-14 10:39:26 +11001949 error = current_has_perm(target, PROCESS__GETCAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 if (error)
1951 return error;
1952
Eric Paris200ac532009-02-12 15:01:04 -05001953 return cap_capget(target, effective, inheritable, permitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954}
1955
David Howellsd84f4f92008-11-14 10:39:23 +11001956static int selinux_capset(struct cred *new, const struct cred *old,
1957 const kernel_cap_t *effective,
1958 const kernel_cap_t *inheritable,
1959 const kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960{
1961 int error;
1962
Eric Paris200ac532009-02-12 15:01:04 -05001963 error = cap_capset(new, old,
David Howellsd84f4f92008-11-14 10:39:23 +11001964 effective, inheritable, permitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 if (error)
1966 return error;
1967
David Howellsd84f4f92008-11-14 10:39:23 +11001968 return cred_has_perm(old, new, PROCESS__SETCAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969}
1970
James Morris5626d3e2009-01-30 10:05:06 +11001971/*
1972 * (This comment used to live with the selinux_task_setuid hook,
1973 * which was removed).
1974 *
1975 * Since setuid only affects the current process, and since the SELinux
1976 * controls are not based on the Linux identity attributes, SELinux does not
1977 * need to control this operation. However, SELinux does control the use of
1978 * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
1979 */
1980
Eric Paris6a9de492012-01-03 12:25:14 -05001981static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
1982 int cap, int audit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983{
1984 int rc;
1985
Eric Paris6a9de492012-01-03 12:25:14 -05001986 rc = cap_capable(cred, ns, cap, audit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 if (rc)
1988 return rc;
1989
Eric Paris6a9de492012-01-03 12:25:14 -05001990 return cred_has_capability(cred, cap, audit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991}
1992
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
1994{
David Howells88e67f32008-11-14 10:39:21 +11001995 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 int rc = 0;
1997
1998 if (!sb)
1999 return 0;
2000
2001 switch (cmds) {
Eric Paris828dfe12008-04-17 13:17:49 -04002002 case Q_SYNC:
2003 case Q_QUOTAON:
2004 case Q_QUOTAOFF:
2005 case Q_SETINFO:
2006 case Q_SETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11002007 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04002008 break;
2009 case Q_GETFMT:
2010 case Q_GETINFO:
2011 case Q_GETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11002012 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04002013 break;
2014 default:
2015 rc = 0; /* let the kernel handle invalid cmds */
2016 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 }
2018 return rc;
2019}
2020
2021static int selinux_quota_on(struct dentry *dentry)
2022{
David Howells88e67f32008-11-14 10:39:21 +11002023 const struct cred *cred = current_cred();
2024
Eric Paris2875fa02011-04-28 16:04:24 -04002025 return dentry_has_perm(cred, dentry, FILE__QUOTAON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026}
2027
Eric Paris12b30522010-11-15 18:36:29 -05002028static int selinux_syslog(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029{
2030 int rc;
2031
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 switch (type) {
Kees Cookd78ca3c2010-02-03 15:37:13 -08002033 case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */
2034 case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */
Eric Paris828dfe12008-04-17 13:17:49 -04002035 rc = task_has_system(current, SYSTEM__SYSLOG_READ);
2036 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08002037 case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */
2038 case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */
2039 /* Set level of messages printed to console */
2040 case SYSLOG_ACTION_CONSOLE_LEVEL:
Eric Paris828dfe12008-04-17 13:17:49 -04002041 rc = task_has_system(current, SYSTEM__SYSLOG_CONSOLE);
2042 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08002043 case SYSLOG_ACTION_CLOSE: /* Close log */
2044 case SYSLOG_ACTION_OPEN: /* Open log */
2045 case SYSLOG_ACTION_READ: /* Read from log */
2046 case SYSLOG_ACTION_READ_CLEAR: /* Read/clear last kernel messages */
2047 case SYSLOG_ACTION_CLEAR: /* Clear ring buffer */
Eric Paris828dfe12008-04-17 13:17:49 -04002048 default:
2049 rc = task_has_system(current, SYSTEM__SYSLOG_MOD);
2050 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 }
2052 return rc;
2053}
2054
2055/*
2056 * Check that a process has enough memory to allocate a new virtual
2057 * mapping. 0 means there is enough memory for the allocation to
2058 * succeed and -ENOMEM implies there is not.
2059 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 * Do not audit the selinux permission check, as this is applied to all
2061 * processes that allocate mappings.
2062 */
Alan Cox34b4e4a2007-08-22 14:01:28 -07002063static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064{
2065 int rc, cap_sys_admin = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
Eric Paris6a9de492012-01-03 12:25:14 -05002067 rc = selinux_capable(current_cred(), &init_user_ns, CAP_SYS_ADMIN,
David Howells3699c532009-01-06 22:27:01 +00002068 SECURITY_CAP_NOAUDIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 if (rc == 0)
2070 cap_sys_admin = 1;
2071
Alan Cox34b4e4a2007-08-22 14:01:28 -07002072 return __vm_enough_memory(mm, pages, cap_sys_admin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073}
2074
2075/* binprm security operations */
2076
David Howellsa6f76f22008-11-14 10:39:24 +11002077static int selinux_bprm_set_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078{
David Howellsa6f76f22008-11-14 10:39:24 +11002079 const struct task_security_struct *old_tsec;
2080 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 struct inode_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04002082 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07002083 struct selinux_audit_data sad = {0,};
David Howellsa6f76f22008-11-14 10:39:24 +11002084 struct inode *inode = bprm->file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 int rc;
2086
Eric Paris200ac532009-02-12 15:01:04 -05002087 rc = cap_bprm_set_creds(bprm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 if (rc)
2089 return rc;
2090
David Howellsa6f76f22008-11-14 10:39:24 +11002091 /* SELinux context only depends on initial program or script and not
2092 * the script interpreter */
2093 if (bprm->cred_prepared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 return 0;
2095
David Howellsa6f76f22008-11-14 10:39:24 +11002096 old_tsec = current_security();
2097 new_tsec = bprm->cred->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 isec = inode->i_security;
2099
2100 /* Default to the current task SID. */
David Howellsa6f76f22008-11-14 10:39:24 +11002101 new_tsec->sid = old_tsec->sid;
2102 new_tsec->osid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
Michael LeMay28eba5b2006-06-27 02:53:42 -07002104 /* Reset fs, key, and sock SIDs on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002105 new_tsec->create_sid = 0;
2106 new_tsec->keycreate_sid = 0;
2107 new_tsec->sockcreate_sid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
David Howellsa6f76f22008-11-14 10:39:24 +11002109 if (old_tsec->exec_sid) {
2110 new_tsec->sid = old_tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 /* Reset exec SID on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002112 new_tsec->exec_sid = 0;
Andy Lutomirski397a85e2012-01-30 08:17:26 -08002113
2114 /*
2115 * Minimize confusion: if no_new_privs and a transition is
2116 * explicitly requested, then fail the exec.
2117 */
2118 if (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS)
2119 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 } else {
2121 /* Check for a default transition on this program. */
David Howellsa6f76f22008-11-14 10:39:24 +11002122 rc = security_transition_sid(old_tsec->sid, isec->sid,
Eric Paris652bb9b2011-02-01 11:05:40 -05002123 SECCLASS_PROCESS, NULL,
2124 &new_tsec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 if (rc)
2126 return rc;
2127 }
2128
Eric Parisf48b7392011-04-25 12:54:27 -04002129 COMMON_AUDIT_DATA_INIT(&ad, PATH);
Eric Paris3b3b0e42012-04-03 09:37:02 -07002130 ad.selinux_audit_data = &sad;
Eric Parisf48b7392011-04-25 12:54:27 -04002131 ad.u.path = bprm->file->f_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
Andy Lutomirski397a85e2012-01-30 08:17:26 -08002133 if ((bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID) ||
2134 (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS))
David Howellsa6f76f22008-11-14 10:39:24 +11002135 new_tsec->sid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
David Howellsa6f76f22008-11-14 10:39:24 +11002137 if (new_tsec->sid == old_tsec->sid) {
2138 rc = avc_has_perm(old_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
2140 if (rc)
2141 return rc;
2142 } else {
2143 /* Check permissions for the transition. */
David Howellsa6f76f22008-11-14 10:39:24 +11002144 rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
2146 if (rc)
2147 return rc;
2148
David Howellsa6f76f22008-11-14 10:39:24 +11002149 rc = avc_has_perm(new_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
2151 if (rc)
2152 return rc;
2153
David Howellsa6f76f22008-11-14 10:39:24 +11002154 /* Check for shared state */
2155 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
2156 rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
2157 SECCLASS_PROCESS, PROCESS__SHARE,
2158 NULL);
2159 if (rc)
2160 return -EPERM;
2161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
David Howellsa6f76f22008-11-14 10:39:24 +11002163 /* Make sure that anyone attempting to ptrace over a task that
2164 * changes its SID has the appropriate permit */
2165 if (bprm->unsafe &
2166 (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
2167 struct task_struct *tracer;
2168 struct task_security_struct *sec;
2169 u32 ptsid = 0;
2170
2171 rcu_read_lock();
Tejun Heo06d98472011-06-17 16:50:40 +02002172 tracer = ptrace_parent(current);
David Howellsa6f76f22008-11-14 10:39:24 +11002173 if (likely(tracer != NULL)) {
2174 sec = __task_cred(tracer)->security;
2175 ptsid = sec->sid;
2176 }
2177 rcu_read_unlock();
2178
2179 if (ptsid != 0) {
2180 rc = avc_has_perm(ptsid, new_tsec->sid,
2181 SECCLASS_PROCESS,
2182 PROCESS__PTRACE, NULL);
2183 if (rc)
2184 return -EPERM;
2185 }
2186 }
2187
2188 /* Clear any possibly unsafe personality bits on exec: */
2189 bprm->per_clear |= PER_CLEAR_ON_SETID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 }
2191
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 return 0;
2193}
2194
Eric Paris828dfe12008-04-17 13:17:49 -04002195static int selinux_bprm_secureexec(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196{
Paul Moore5fb49872010-04-22 14:46:19 -04002197 const struct task_security_struct *tsec = current_security();
David Howells275bb412008-11-14 10:39:19 +11002198 u32 sid, osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 int atsecure = 0;
2200
David Howells275bb412008-11-14 10:39:19 +11002201 sid = tsec->sid;
2202 osid = tsec->osid;
2203
2204 if (osid != sid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 /* Enable secure mode for SIDs transitions unless
2206 the noatsecure permission is granted between
2207 the two SIDs, i.e. ahp returns 0. */
David Howells275bb412008-11-14 10:39:19 +11002208 atsecure = avc_has_perm(osid, sid,
David Howellsa6f76f22008-11-14 10:39:24 +11002209 SECCLASS_PROCESS,
2210 PROCESS__NOATSECURE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 }
2212
Eric Paris200ac532009-02-12 15:01:04 -05002213 return (atsecure || cap_bprm_secureexec(bprm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214}
2215
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216/* Derived from fs/exec.c:flush_old_files. */
David Howells745ca242008-11-14 10:39:22 +11002217static inline void flush_unauthorized_files(const struct cred *cred,
2218 struct files_struct *files)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219{
Thomas Liu2bf49692009-07-14 12:14:09 -04002220 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07002221 struct selinux_audit_data sad = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 struct file *file, *devnull = NULL;
Stephen Smalleyb20c8122006-09-25 23:32:03 -07002223 struct tty_struct *tty;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07002224 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 long j = -1;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002226 int drop_tty = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002228 tty = get_current_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 if (tty) {
Nick Pigginee2ffa02010-08-18 04:37:35 +10002230 spin_lock(&tty_files_lock);
Eric Paris37dd0bd2008-10-31 17:40:00 -04002231 if (!list_empty(&tty->tty_files)) {
Nick Piggind996b622010-08-18 04:37:36 +10002232 struct tty_file_private *file_priv;
Eric Paris37dd0bd2008-10-31 17:40:00 -04002233 struct inode *inode;
2234
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 /* Revalidate access to controlling tty.
2236 Use inode_has_perm on the tty inode directly rather
2237 than using file_has_perm, as this particular open
2238 file may belong to another process and we are only
2239 interested in the inode-based check here. */
Nick Piggind996b622010-08-18 04:37:36 +10002240 file_priv = list_first_entry(&tty->tty_files,
2241 struct tty_file_private, list);
2242 file = file_priv->file;
Eric Paris37dd0bd2008-10-31 17:40:00 -04002243 inode = file->f_path.dentry->d_inode;
Linus Torvalds95f4efb2011-06-08 15:11:56 -07002244 if (inode_has_perm_noadp(cred, inode,
2245 FILE__READ | FILE__WRITE, 0)) {
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002246 drop_tty = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 }
2248 }
Nick Pigginee2ffa02010-08-18 04:37:35 +10002249 spin_unlock(&tty_files_lock);
Alan Cox452a00d2008-10-13 10:39:13 +01002250 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 }
Eric W. Biederman98a27ba2007-05-08 00:26:56 -07002252 /* Reset controlling tty. */
2253 if (drop_tty)
2254 no_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
2256 /* Revalidate access to inherited open files. */
2257
Eric Parisf48b7392011-04-25 12:54:27 -04002258 COMMON_AUDIT_DATA_INIT(&ad, INODE);
Eric Paris3b3b0e42012-04-03 09:37:02 -07002259 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
2261 spin_lock(&files->file_lock);
2262 for (;;) {
2263 unsigned long set, i;
2264 int fd;
2265
2266 j++;
2267 i = j * __NFDBITS;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07002268 fdt = files_fdtable(files);
Vadim Lobanovbbea9f62006-12-10 02:21:12 -08002269 if (i >= fdt->max_fds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 break;
David Howells1fd36ad2012-02-16 17:49:54 +00002271 set = fdt->open_fds[j];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 if (!set)
2273 continue;
2274 spin_unlock(&files->file_lock);
Eric Paris828dfe12008-04-17 13:17:49 -04002275 for ( ; set ; i++, set >>= 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 if (set & 1) {
2277 file = fget(i);
2278 if (!file)
2279 continue;
David Howells88e67f32008-11-14 10:39:21 +11002280 if (file_has_perm(cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 file,
2282 file_to_av(file))) {
2283 sys_close(i);
2284 fd = get_unused_fd();
2285 if (fd != i) {
2286 if (fd >= 0)
2287 put_unused_fd(fd);
2288 fput(file);
2289 continue;
2290 }
2291 if (devnull) {
Nick Piggin095975d2006-01-08 01:02:19 -08002292 get_file(devnull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 } else {
David Howells745ca242008-11-14 10:39:22 +11002294 devnull = dentry_open(
2295 dget(selinux_null),
2296 mntget(selinuxfs_mount),
2297 O_RDWR, cred);
Akinobu Mitafc5d81e2006-11-27 15:16:48 +09002298 if (IS_ERR(devnull)) {
2299 devnull = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 put_unused_fd(fd);
2301 fput(file);
2302 continue;
2303 }
2304 }
2305 fd_install(fd, devnull);
2306 }
2307 fput(file);
2308 }
2309 }
2310 spin_lock(&files->file_lock);
2311
2312 }
2313 spin_unlock(&files->file_lock);
2314}
2315
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316/*
David Howellsa6f76f22008-11-14 10:39:24 +11002317 * Prepare a process for imminent new credential changes due to exec
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 */
David Howellsa6f76f22008-11-14 10:39:24 +11002319static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320{
David Howellsa6f76f22008-11-14 10:39:24 +11002321 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 struct rlimit *rlim, *initrlim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 int rc, i;
2324
David Howellsa6f76f22008-11-14 10:39:24 +11002325 new_tsec = bprm->cred->security;
2326 if (new_tsec->sid == new_tsec->osid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 return;
2328
2329 /* Close files for which the new task SID is not authorized. */
David Howellsa6f76f22008-11-14 10:39:24 +11002330 flush_unauthorized_files(bprm->cred, current->files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
David Howellsa6f76f22008-11-14 10:39:24 +11002332 /* Always clear parent death signal on SID transitions. */
2333 current->pdeath_signal = 0;
2334
2335 /* Check whether the new SID can inherit resource limits from the old
2336 * SID. If not, reset all soft limits to the lower of the current
2337 * task's hard limit and the init task's soft limit.
2338 *
2339 * Note that the setting of hard limits (even to lower them) can be
2340 * controlled by the setrlimit check. The inclusion of the init task's
2341 * soft limit into the computation is to avoid resetting soft limits
2342 * higher than the default soft limit for cases where the default is
2343 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2344 */
2345 rc = avc_has_perm(new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
2346 PROCESS__RLIMITINH, NULL);
2347 if (rc) {
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002348 /* protect against do_prlimit() */
2349 task_lock(current);
David Howellsa6f76f22008-11-14 10:39:24 +11002350 for (i = 0; i < RLIM_NLIMITS; i++) {
2351 rlim = current->signal->rlim + i;
2352 initrlim = init_task.signal->rlim + i;
2353 rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
2354 }
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002355 task_unlock(current);
2356 update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
David Howellsa6f76f22008-11-14 10:39:24 +11002357 }
2358}
2359
2360/*
2361 * Clean up the process immediately after the installation of new credentials
2362 * due to exec
2363 */
2364static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
2365{
2366 const struct task_security_struct *tsec = current_security();
2367 struct itimerval itimer;
David Howellsa6f76f22008-11-14 10:39:24 +11002368 u32 osid, sid;
2369 int rc, i;
David Howellsa6f76f22008-11-14 10:39:24 +11002370
David Howellsa6f76f22008-11-14 10:39:24 +11002371 osid = tsec->osid;
2372 sid = tsec->sid;
2373
2374 if (sid == osid)
2375 return;
2376
2377 /* Check whether the new SID can inherit signal state from the old SID.
2378 * If not, clear itimers to avoid subsequent signal generation and
2379 * flush and unblock signals.
2380 *
2381 * This must occur _after_ the task SID has been updated so that any
2382 * kill done after the flush will be checked against the new SID.
2383 */
2384 rc = avc_has_perm(osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 if (rc) {
2386 memset(&itimer, 0, sizeof itimer);
2387 for (i = 0; i < 3; i++)
2388 do_setitimer(i, &itimer, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 spin_lock_irq(&current->sighand->siglock);
David Howells3bcac022009-04-29 13:45:05 +01002390 if (!(current->signal->flags & SIGNAL_GROUP_EXIT)) {
2391 __flush_signals(current);
2392 flush_signal_handlers(current, 1);
2393 sigemptyset(&current->blocked);
2394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 spin_unlock_irq(&current->sighand->siglock);
2396 }
2397
David Howellsa6f76f22008-11-14 10:39:24 +11002398 /* Wake up the parent if it is waiting so that it can recheck
2399 * wait permission to the new task SID. */
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002400 read_lock(&tasklist_lock);
Oleg Nesterov0b7570e2009-09-23 15:56:46 -07002401 __wake_up_parent(current, current->real_parent);
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002402 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403}
2404
2405/* superblock security operations */
2406
2407static int selinux_sb_alloc_security(struct super_block *sb)
2408{
2409 return superblock_alloc_security(sb);
2410}
2411
2412static void selinux_sb_free_security(struct super_block *sb)
2413{
2414 superblock_free_security(sb);
2415}
2416
2417static inline int match_prefix(char *prefix, int plen, char *option, int olen)
2418{
2419 if (plen > olen)
2420 return 0;
2421
2422 return !memcmp(prefix, option, plen);
2423}
2424
2425static inline int selinux_option(char *option, int len)
2426{
Eric Paris832cbd92008-04-01 13:24:09 -04002427 return (match_prefix(CONTEXT_STR, sizeof(CONTEXT_STR)-1, option, len) ||
2428 match_prefix(FSCONTEXT_STR, sizeof(FSCONTEXT_STR)-1, option, len) ||
2429 match_prefix(DEFCONTEXT_STR, sizeof(DEFCONTEXT_STR)-1, option, len) ||
David P. Quigley11689d42009-01-16 09:22:03 -05002430 match_prefix(ROOTCONTEXT_STR, sizeof(ROOTCONTEXT_STR)-1, option, len) ||
2431 match_prefix(LABELSUPP_STR, sizeof(LABELSUPP_STR)-1, option, len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432}
2433
2434static inline void take_option(char **to, char *from, int *first, int len)
2435{
2436 if (!*first) {
2437 **to = ',';
2438 *to += 1;
Cory Olmo3528a952006-09-29 01:58:44 -07002439 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 *first = 0;
2441 memcpy(*to, from, len);
2442 *to += len;
2443}
2444
Eric Paris828dfe12008-04-17 13:17:49 -04002445static inline void take_selinux_option(char **to, char *from, int *first,
2446 int len)
Cory Olmo3528a952006-09-29 01:58:44 -07002447{
2448 int current_size = 0;
2449
2450 if (!*first) {
2451 **to = '|';
2452 *to += 1;
Eric Paris828dfe12008-04-17 13:17:49 -04002453 } else
Cory Olmo3528a952006-09-29 01:58:44 -07002454 *first = 0;
2455
2456 while (current_size < len) {
2457 if (*from != '"') {
2458 **to = *from;
2459 *to += 1;
2460 }
2461 from += 1;
2462 current_size += 1;
2463 }
2464}
2465
Eric Parise0007522008-03-05 10:31:54 -05002466static int selinux_sb_copy_data(char *orig, char *copy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467{
2468 int fnosec, fsec, rc = 0;
2469 char *in_save, *in_curr, *in_end;
2470 char *sec_curr, *nosec_save, *nosec;
Cory Olmo3528a952006-09-29 01:58:44 -07002471 int open_quote = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
2473 in_curr = orig;
2474 sec_curr = copy;
2475
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 nosec = (char *)get_zeroed_page(GFP_KERNEL);
2477 if (!nosec) {
2478 rc = -ENOMEM;
2479 goto out;
2480 }
2481
2482 nosec_save = nosec;
2483 fnosec = fsec = 1;
2484 in_save = in_end = orig;
2485
2486 do {
Cory Olmo3528a952006-09-29 01:58:44 -07002487 if (*in_end == '"')
2488 open_quote = !open_quote;
2489 if ((*in_end == ',' && open_quote == 0) ||
2490 *in_end == '\0') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 int len = in_end - in_curr;
2492
2493 if (selinux_option(in_curr, len))
Cory Olmo3528a952006-09-29 01:58:44 -07002494 take_selinux_option(&sec_curr, in_curr, &fsec, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 else
2496 take_option(&nosec, in_curr, &fnosec, len);
2497
2498 in_curr = in_end + 1;
2499 }
2500 } while (*in_end++);
2501
Eric Paris6931dfc2005-06-30 02:58:51 -07002502 strcpy(in_save, nosec_save);
Gerald Schaeferda3caa22005-06-21 17:15:18 -07002503 free_page((unsigned long)nosec_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504out:
2505 return rc;
2506}
2507
Eric Paris026eb162011-03-03 16:09:14 -05002508static int selinux_sb_remount(struct super_block *sb, void *data)
2509{
2510 int rc, i, *flags;
2511 struct security_mnt_opts opts;
2512 char *secdata, **mount_options;
2513 struct superblock_security_struct *sbsec = sb->s_security;
2514
2515 if (!(sbsec->flags & SE_SBINITIALIZED))
2516 return 0;
2517
2518 if (!data)
2519 return 0;
2520
2521 if (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
2522 return 0;
2523
2524 security_init_mnt_opts(&opts);
2525 secdata = alloc_secdata();
2526 if (!secdata)
2527 return -ENOMEM;
2528 rc = selinux_sb_copy_data(data, secdata);
2529 if (rc)
2530 goto out_free_secdata;
2531
2532 rc = selinux_parse_opts_str(secdata, &opts);
2533 if (rc)
2534 goto out_free_secdata;
2535
2536 mount_options = opts.mnt_opts;
2537 flags = opts.mnt_opts_flags;
2538
2539 for (i = 0; i < opts.num_mnt_opts; i++) {
2540 u32 sid;
2541 size_t len;
2542
2543 if (flags[i] == SE_SBLABELSUPP)
2544 continue;
2545 len = strlen(mount_options[i]);
2546 rc = security_context_to_sid(mount_options[i], len, &sid);
2547 if (rc) {
2548 printk(KERN_WARNING "SELinux: security_context_to_sid"
2549 "(%s) failed for (dev %s, type %s) errno=%d\n",
2550 mount_options[i], sb->s_id, sb->s_type->name, rc);
2551 goto out_free_opts;
2552 }
2553 rc = -EINVAL;
2554 switch (flags[i]) {
2555 case FSCONTEXT_MNT:
2556 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
2557 goto out_bad_option;
2558 break;
2559 case CONTEXT_MNT:
2560 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
2561 goto out_bad_option;
2562 break;
2563 case ROOTCONTEXT_MNT: {
2564 struct inode_security_struct *root_isec;
2565 root_isec = sb->s_root->d_inode->i_security;
2566
2567 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
2568 goto out_bad_option;
2569 break;
2570 }
2571 case DEFCONTEXT_MNT:
2572 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
2573 goto out_bad_option;
2574 break;
2575 default:
2576 goto out_free_opts;
2577 }
2578 }
2579
2580 rc = 0;
2581out_free_opts:
2582 security_free_mnt_opts(&opts);
2583out_free_secdata:
2584 free_secdata(secdata);
2585 return rc;
2586out_bad_option:
2587 printk(KERN_WARNING "SELinux: unable to change security options "
2588 "during remount (dev %s, type=%s)\n", sb->s_id,
2589 sb->s_type->name);
2590 goto out_free_opts;
2591}
2592
James Morris12204e22008-12-19 10:44:42 +11002593static int selinux_sb_kern_mount(struct super_block *sb, int flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594{
David Howells88e67f32008-11-14 10:39:21 +11002595 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002596 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07002597 struct selinux_audit_data sad = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 int rc;
2599
2600 rc = superblock_doinit(sb, data);
2601 if (rc)
2602 return rc;
2603
James Morris74192242008-12-19 11:41:10 +11002604 /* Allow all mounts performed by the kernel */
2605 if (flags & MS_KERNMOUNT)
2606 return 0;
2607
Eric Parisa2694342011-04-25 13:10:27 -04002608 COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
Eric Paris3b3b0e42012-04-03 09:37:02 -07002609 ad.selinux_audit_data = &sad;
Eric Parisa2694342011-04-25 13:10:27 -04002610 ad.u.dentry = sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002611 return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612}
2613
David Howells726c3342006-06-23 02:02:58 -07002614static int selinux_sb_statfs(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615{
David Howells88e67f32008-11-14 10:39:21 +11002616 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002617 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07002618 struct selinux_audit_data sad = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619
Eric Parisa2694342011-04-25 13:10:27 -04002620 COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
Eric Paris3b3b0e42012-04-03 09:37:02 -07002621 ad.selinux_audit_data = &sad;
Eric Parisa2694342011-04-25 13:10:27 -04002622 ad.u.dentry = dentry->d_sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002623 return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624}
2625
Eric Paris828dfe12008-04-17 13:17:49 -04002626static int selinux_mount(char *dev_name,
Al Virob5266eb2008-03-22 17:48:24 -04002627 struct path *path,
Eric Paris828dfe12008-04-17 13:17:49 -04002628 char *type,
2629 unsigned long flags,
2630 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631{
David Howells88e67f32008-11-14 10:39:21 +11002632 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633
2634 if (flags & MS_REMOUNT)
Al Virod8c95842011-12-07 18:16:57 -05002635 return superblock_has_perm(cred, path->dentry->d_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002636 FILESYSTEM__REMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 else
Eric Paris2875fa02011-04-28 16:04:24 -04002638 return path_has_perm(cred, path, FILE__MOUNTON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639}
2640
2641static int selinux_umount(struct vfsmount *mnt, int flags)
2642{
David Howells88e67f32008-11-14 10:39:21 +11002643 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644
David Howells88e67f32008-11-14 10:39:21 +11002645 return superblock_has_perm(cred, mnt->mnt_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002646 FILESYSTEM__UNMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647}
2648
2649/* inode security operations */
2650
2651static int selinux_inode_alloc_security(struct inode *inode)
2652{
2653 return inode_alloc_security(inode);
2654}
2655
2656static void selinux_inode_free_security(struct inode *inode)
2657{
2658 inode_free_security(inode);
2659}
2660
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002661static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
Eric Paris2a7dba32011-02-01 11:05:39 -05002662 const struct qstr *qstr, char **name,
2663 void **value, size_t *len)
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002664{
Paul Moore5fb49872010-04-22 14:46:19 -04002665 const struct task_security_struct *tsec = current_security();
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002666 struct inode_security_struct *dsec;
2667 struct superblock_security_struct *sbsec;
David Howells275bb412008-11-14 10:39:19 +11002668 u32 sid, newsid, clen;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002669 int rc;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002670 char *namep = NULL, *context;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002671
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002672 dsec = dir->i_security;
2673 sbsec = dir->i_sb->s_security;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002674
David Howells275bb412008-11-14 10:39:19 +11002675 sid = tsec->sid;
2676 newsid = tsec->create_sid;
2677
Eric Paris415103f2010-12-02 16:13:40 -05002678 if ((sbsec->flags & SE_SBINITIALIZED) &&
2679 (sbsec->behavior == SECURITY_FS_USE_MNTPOINT))
2680 newsid = sbsec->mntpoint_sid;
2681 else if (!newsid || !(sbsec->flags & SE_SBLABELSUPP)) {
David Howells275bb412008-11-14 10:39:19 +11002682 rc = security_transition_sid(sid, dsec->sid,
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002683 inode_mode_to_security_class(inode->i_mode),
Eric Paris652bb9b2011-02-01 11:05:40 -05002684 qstr, &newsid);
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002685 if (rc) {
2686 printk(KERN_WARNING "%s: "
2687 "security_transition_sid failed, rc=%d (dev=%s "
2688 "ino=%ld)\n",
Harvey Harrisondd6f9532008-03-06 10:03:59 +11002689 __func__,
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002690 -rc, inode->i_sb->s_id, inode->i_ino);
2691 return rc;
2692 }
2693 }
2694
Eric Paris296fddf2006-09-25 23:32:00 -07002695 /* Possibly defer initialization to selinux_complete_init. */
David P. Quigley0d90a7e2009-01-16 09:22:02 -05002696 if (sbsec->flags & SE_SBINITIALIZED) {
Eric Paris296fddf2006-09-25 23:32:00 -07002697 struct inode_security_struct *isec = inode->i_security;
2698 isec->sclass = inode_mode_to_security_class(inode->i_mode);
2699 isec->sid = newsid;
2700 isec->initialized = 1;
2701 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002702
David P. Quigleycd895962009-01-16 09:22:04 -05002703 if (!ss_initialized || !(sbsec->flags & SE_SBLABELSUPP))
Stephen Smalley25a74f32005-11-08 21:34:33 -08002704 return -EOPNOTSUPP;
2705
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002706 if (name) {
Josef Bacika02fe132008-04-04 09:35:05 +11002707 namep = kstrdup(XATTR_SELINUX_SUFFIX, GFP_NOFS);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002708 if (!namep)
2709 return -ENOMEM;
2710 *name = namep;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002711 }
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002712
2713 if (value && len) {
Stephen Smalley12b29f32008-05-07 13:03:20 -04002714 rc = security_sid_to_context_force(newsid, &context, &clen);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002715 if (rc) {
2716 kfree(namep);
2717 return rc;
2718 }
2719 *value = context;
2720 *len = clen;
2721 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002722
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002723 return 0;
2724}
2725
Al Viro4acdaf22011-07-26 01:42:34 -04002726static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727{
Amir Samuelov6a22e462014-05-26 11:44:06 +03002728 int ret;
2729
2730 ret = pft_inode_create(dir, dentry, mode);
2731 if (ret < 0)
2732 return ret;
2733
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 return may_create(dir, dentry, SECCLASS_FILE);
2735}
2736
Amir Samuelov6a22e462014-05-26 11:44:06 +03002737static int selinux_inode_post_create(struct inode *dir, struct dentry *dentry,
2738 umode_t mode)
2739{
2740 int ret;
2741
2742 ret = pft_inode_post_create(dir, dentry, mode);
2743
2744 return ret;
2745}
2746
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2748{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 return may_link(dir, old_dentry, MAY_LINK);
2750}
2751
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
2753{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 return may_link(dir, dentry, MAY_UNLINK);
2755}
2756
2757static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
2758{
2759 return may_create(dir, dentry, SECCLASS_LNK_FILE);
2760}
2761
Al Viro18bb1db2011-07-26 01:41:39 -04002762static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763{
2764 return may_create(dir, dentry, SECCLASS_DIR);
2765}
2766
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
2768{
2769 return may_link(dir, dentry, MAY_RMDIR);
2770}
2771
Al Viro1a67aaf2011-07-26 01:52:52 -04002772static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 return may_create(dir, dentry, inode_mode_to_security_class(mode));
2775}
2776
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
Eric Paris828dfe12008-04-17 13:17:49 -04002778 struct inode *new_inode, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779{
Amir Samuelov6a22e462014-05-26 11:44:06 +03002780 int rc;
2781
2782 rc = pft_inode_rename(old_inode, old_dentry, new_inode, new_dentry);
2783 if (rc)
2784 return rc;
2785
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 return may_rename(old_inode, old_dentry, new_inode, new_dentry);
2787}
2788
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789static int selinux_inode_readlink(struct dentry *dentry)
2790{
David Howells88e67f32008-11-14 10:39:21 +11002791 const struct cred *cred = current_cred();
2792
Eric Paris2875fa02011-04-28 16:04:24 -04002793 return dentry_has_perm(cred, dentry, FILE__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794}
2795
2796static int selinux_inode_follow_link(struct dentry *dentry, struct nameidata *nameidata)
2797{
David Howells88e67f32008-11-14 10:39:21 +11002798 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799
Eric Paris2875fa02011-04-28 16:04:24 -04002800 return dentry_has_perm(cred, dentry, FILE__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801}
2802
Al Viroe74f71e2011-06-20 19:38:15 -04002803static int selinux_inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804{
David Howells88e67f32008-11-14 10:39:21 +11002805 const struct cred *cred = current_cred();
Eric Parisb782e0a2010-07-23 11:44:03 -04002806 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07002807 struct selinux_audit_data sad = {0,};
Eric Parisb782e0a2010-07-23 11:44:03 -04002808 u32 perms;
2809 bool from_access;
Al Virocf1dd1d2011-06-20 19:44:08 -04002810 unsigned flags = mask & MAY_NOT_BLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811
Eric Parisb782e0a2010-07-23 11:44:03 -04002812 from_access = mask & MAY_ACCESS;
Eric Parisd09ca732010-07-23 11:43:57 -04002813 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
2814
Eric Parisb782e0a2010-07-23 11:44:03 -04002815 /* No permission to check. Existence test. */
2816 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818
Eric Parisf48b7392011-04-25 12:54:27 -04002819 COMMON_AUDIT_DATA_INIT(&ad, INODE);
Eric Paris3b3b0e42012-04-03 09:37:02 -07002820 ad.selinux_audit_data = &sad;
Eric Parisf48b7392011-04-25 12:54:27 -04002821 ad.u.inode = inode;
Eric Parisb782e0a2010-07-23 11:44:03 -04002822
2823 if (from_access)
Eric Paris3b3b0e42012-04-03 09:37:02 -07002824 ad.selinux_audit_data->auditdeny |= FILE__AUDIT_ACCESS;
Eric Parisb782e0a2010-07-23 11:44:03 -04002825
2826 perms = file_mask_to_av(inode->i_mode, mask);
2827
Eric Paris9ade0cf2011-04-25 16:26:29 -04002828 return inode_has_perm(cred, inode, perms, &ad, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829}
2830
2831static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
2832{
David Howells88e67f32008-11-14 10:39:21 +11002833 const struct cred *cred = current_cred();
Amerigo Wangbc6a6002009-08-20 19:29:02 -07002834 unsigned int ia_valid = iattr->ia_valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835
Amerigo Wangbc6a6002009-08-20 19:29:02 -07002836 /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
2837 if (ia_valid & ATTR_FORCE) {
2838 ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
2839 ATTR_FORCE);
2840 if (!ia_valid)
2841 return 0;
2842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843
Amerigo Wangbc6a6002009-08-20 19:29:02 -07002844 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
2845 ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
Eric Paris2875fa02011-04-28 16:04:24 -04002846 return dentry_has_perm(cred, dentry, FILE__SETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847
Eric Paris2875fa02011-04-28 16:04:24 -04002848 return dentry_has_perm(cred, dentry, FILE__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849}
2850
2851static int selinux_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
2852{
David Howells88e67f32008-11-14 10:39:21 +11002853 const struct cred *cred = current_cred();
Eric Paris2875fa02011-04-28 16:04:24 -04002854 struct path path;
David Howells88e67f32008-11-14 10:39:21 +11002855
Eric Paris2875fa02011-04-28 16:04:24 -04002856 path.dentry = dentry;
2857 path.mnt = mnt;
2858
2859 return path_has_perm(cred, &path, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860}
2861
David Howells8f0cfa52008-04-29 00:59:41 -07002862static int selinux_inode_setotherxattr(struct dentry *dentry, const char *name)
Serge E. Hallynb5376772007-10-16 23:31:36 -07002863{
David Howells88e67f32008-11-14 10:39:21 +11002864 const struct cred *cred = current_cred();
2865
Amir Samuelov6a22e462014-05-26 11:44:06 +03002866 if (pft_inode_set_xattr(dentry, name) < 0)
2867 return -EACCES;
2868
2869
Serge E. Hallynb5376772007-10-16 23:31:36 -07002870 if (!strncmp(name, XATTR_SECURITY_PREFIX,
2871 sizeof XATTR_SECURITY_PREFIX - 1)) {
2872 if (!strcmp(name, XATTR_NAME_CAPS)) {
2873 if (!capable(CAP_SETFCAP))
2874 return -EPERM;
2875 } else if (!capable(CAP_SYS_ADMIN)) {
2876 /* A different attribute in the security namespace.
2877 Restrict to administrator. */
2878 return -EPERM;
2879 }
2880 }
2881
2882 /* Not an attribute we recognize, so just check the
2883 ordinary setattr permission. */
Eric Paris2875fa02011-04-28 16:04:24 -04002884 return dentry_has_perm(cred, dentry, FILE__SETATTR);
Serge E. Hallynb5376772007-10-16 23:31:36 -07002885}
2886
David Howells8f0cfa52008-04-29 00:59:41 -07002887static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
2888 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 struct inode *inode = dentry->d_inode;
2891 struct inode_security_struct *isec = inode->i_security;
2892 struct superblock_security_struct *sbsec;
Thomas Liu2bf49692009-07-14 12:14:09 -04002893 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07002894 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11002895 u32 newsid, sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 int rc = 0;
2897
Serge E. Hallynb5376772007-10-16 23:31:36 -07002898 if (strcmp(name, XATTR_NAME_SELINUX))
2899 return selinux_inode_setotherxattr(dentry, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900
2901 sbsec = inode->i_sb->s_security;
David P. Quigleycd895962009-01-16 09:22:04 -05002902 if (!(sbsec->flags & SE_SBLABELSUPP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 return -EOPNOTSUPP;
2904
Serge E. Hallyn2e149672011-03-23 16:43:26 -07002905 if (!inode_owner_or_capable(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 return -EPERM;
2907
Eric Parisa2694342011-04-25 13:10:27 -04002908 COMMON_AUDIT_DATA_INIT(&ad, DENTRY);
Eric Paris3b3b0e42012-04-03 09:37:02 -07002909 ad.selinux_audit_data = &sad;
Eric Parisa2694342011-04-25 13:10:27 -04002910 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911
David Howells275bb412008-11-14 10:39:19 +11002912 rc = avc_has_perm(sid, isec->sid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 FILE__RELABELFROM, &ad);
2914 if (rc)
2915 return rc;
2916
2917 rc = security_context_to_sid(value, size, &newsid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04002918 if (rc == -EINVAL) {
2919 if (!capable(CAP_MAC_ADMIN))
2920 return rc;
2921 rc = security_context_to_sid_force(value, size, &newsid);
2922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 if (rc)
2924 return rc;
2925
David Howells275bb412008-11-14 10:39:19 +11002926 rc = avc_has_perm(sid, newsid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 FILE__RELABELTO, &ad);
2928 if (rc)
2929 return rc;
2930
David Howells275bb412008-11-14 10:39:19 +11002931 rc = security_validate_transition(isec->sid, newsid, sid,
Eric Paris828dfe12008-04-17 13:17:49 -04002932 isec->sclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 if (rc)
2934 return rc;
2935
2936 return avc_has_perm(newsid,
2937 sbsec->sid,
2938 SECCLASS_FILESYSTEM,
2939 FILESYSTEM__ASSOCIATE,
2940 &ad);
2941}
2942
David Howells8f0cfa52008-04-29 00:59:41 -07002943static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
Eric Parisf5269712008-05-14 11:27:45 -04002944 const void *value, size_t size,
David Howells8f0cfa52008-04-29 00:59:41 -07002945 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946{
2947 struct inode *inode = dentry->d_inode;
2948 struct inode_security_struct *isec = inode->i_security;
2949 u32 newsid;
2950 int rc;
2951
2952 if (strcmp(name, XATTR_NAME_SELINUX)) {
2953 /* Not an attribute we recognize, so nothing to do. */
2954 return;
2955 }
2956
Stephen Smalley12b29f32008-05-07 13:03:20 -04002957 rc = security_context_to_sid_force(value, size, &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 if (rc) {
Stephen Smalley12b29f32008-05-07 13:03:20 -04002959 printk(KERN_ERR "SELinux: unable to map context to SID"
2960 "for (%s, %lu), rc=%d\n",
2961 inode->i_sb->s_id, inode->i_ino, -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 return;
2963 }
2964
2965 isec->sid = newsid;
2966 return;
2967}
2968
David Howells8f0cfa52008-04-29 00:59:41 -07002969static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970{
David Howells88e67f32008-11-14 10:39:21 +11002971 const struct cred *cred = current_cred();
2972
Eric Paris2875fa02011-04-28 16:04:24 -04002973 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974}
2975
Eric Paris828dfe12008-04-17 13:17:49 -04002976static int selinux_inode_listxattr(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977{
David Howells88e67f32008-11-14 10:39:21 +11002978 const struct cred *cred = current_cred();
2979
Eric Paris2875fa02011-04-28 16:04:24 -04002980 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981}
2982
David Howells8f0cfa52008-04-29 00:59:41 -07002983static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984{
Serge E. Hallynb5376772007-10-16 23:31:36 -07002985 if (strcmp(name, XATTR_NAME_SELINUX))
2986 return selinux_inode_setotherxattr(dentry, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987
2988 /* No one is allowed to remove a SELinux security label.
2989 You can change the label, but all data must be labeled. */
2990 return -EACCES;
2991}
2992
James Morrisd381d8a2005-10-30 14:59:22 -08002993/*
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04002994 * Copy the inode security context value to the user.
James Morrisd381d8a2005-10-30 14:59:22 -08002995 *
2996 * Permission check is handled by selinux_inode_getxattr hook.
2997 */
David P. Quigley42492592008-02-04 22:29:39 -08002998static int selinux_inode_getsecurity(const struct inode *inode, const char *name, void **buffer, bool alloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999{
David P. Quigley42492592008-02-04 22:29:39 -08003000 u32 size;
3001 int error;
3002 char *context = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 struct inode_security_struct *isec = inode->i_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00003005 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3006 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003008 /*
3009 * If the caller has CAP_MAC_ADMIN, then get the raw context
3010 * value even if it is not defined by current policy; otherwise,
3011 * use the in-core value under current policy.
3012 * Use the non-auditing forms of the permission checks since
3013 * getxattr may be called by unprivileged processes commonly
3014 * and lack of permission just means that we fall back to the
3015 * in-core context value, not a denial.
3016 */
Eric Paris6a9de492012-01-03 12:25:14 -05003017 error = selinux_capable(current_cred(), &init_user_ns, CAP_MAC_ADMIN,
David Howells3699c532009-01-06 22:27:01 +00003018 SECURITY_CAP_NOAUDIT);
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003019 if (!error)
3020 error = security_sid_to_context_force(isec->sid, &context,
3021 &size);
3022 else
3023 error = security_sid_to_context(isec->sid, &context, &size);
David P. Quigley42492592008-02-04 22:29:39 -08003024 if (error)
3025 return error;
3026 error = size;
3027 if (alloc) {
3028 *buffer = context;
3029 goto out_nofree;
3030 }
3031 kfree(context);
3032out_nofree:
3033 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034}
3035
3036static int selinux_inode_setsecurity(struct inode *inode, const char *name,
Eric Paris828dfe12008-04-17 13:17:49 -04003037 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038{
3039 struct inode_security_struct *isec = inode->i_security;
3040 u32 newsid;
3041 int rc;
3042
3043 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3044 return -EOPNOTSUPP;
3045
3046 if (!value || !size)
3047 return -EACCES;
3048
Eric Paris828dfe12008-04-17 13:17:49 -04003049 rc = security_context_to_sid((void *)value, size, &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 if (rc)
3051 return rc;
3052
3053 isec->sid = newsid;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04003054 isec->initialized = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 return 0;
3056}
3057
3058static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
3059{
3060 const int len = sizeof(XATTR_NAME_SELINUX);
3061 if (buffer && len <= buffer_size)
3062 memcpy(buffer, XATTR_NAME_SELINUX, len);
3063 return len;
3064}
3065
Ahmed S. Darwish713a04a2008-03-01 21:52:30 +02003066static void selinux_inode_getsecid(const struct inode *inode, u32 *secid)
3067{
3068 struct inode_security_struct *isec = inode->i_security;
3069 *secid = isec->sid;
3070}
3071
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072/* file security operations */
3073
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003074static int selinux_revalidate_file_permission(struct file *file, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075{
David Howells88e67f32008-11-14 10:39:21 +11003076 const struct cred *cred = current_cred();
Josef Sipek3d5ff522006-12-08 02:37:38 -08003077 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
3080 if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
3081 mask |= MAY_APPEND;
3082
Paul Moore389fb802009-03-27 17:10:34 -04003083 return file_has_perm(cred, file,
3084 file_mask_to_av(inode->i_mode, mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085}
3086
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003087static int selinux_file_permission(struct file *file, int mask)
3088{
Stephen Smalley20dda182009-06-22 14:54:53 -04003089 struct inode *inode = file->f_path.dentry->d_inode;
3090 struct file_security_struct *fsec = file->f_security;
3091 struct inode_security_struct *isec = inode->i_security;
3092 u32 sid = current_sid();
Amir Samuelov6a22e462014-05-26 11:44:06 +03003093 int ret;
Stephen Smalley20dda182009-06-22 14:54:53 -04003094
Paul Moore389fb802009-03-27 17:10:34 -04003095 if (!mask)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003096 /* No permission to check. Existence test. */
3097 return 0;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003098
Amir Samuelov6a22e462014-05-26 11:44:06 +03003099 ret = pft_file_permission(file, mask);
3100 if (ret < 0)
3101 return ret;
3102
Stephen Smalley20dda182009-06-22 14:54:53 -04003103 if (sid == fsec->sid && fsec->isid == isec->sid &&
3104 fsec->pseqno == avc_policy_seqno())
3105 /* No change since dentry_open check. */
3106 return 0;
3107
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003108 return selinux_revalidate_file_permission(file, mask);
3109}
3110
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111static int selinux_file_alloc_security(struct file *file)
3112{
3113 return file_alloc_security(file);
3114}
3115
3116static void selinux_file_free_security(struct file *file)
3117{
3118 file_free_security(file);
3119}
3120
Jeff Vander Stoep581be712015-07-10 17:19:56 -04003121/*
3122 * Check whether a task has the ioctl permission and cmd
3123 * operation to an inode.
3124 */
3125int ioctl_has_perm(const struct cred *cred, struct file *file,
3126 u32 requested, u16 cmd)
3127{
3128 struct common_audit_data ad;
3129 struct file_security_struct *fsec = file->f_security;
3130 struct inode *inode = file->f_path.dentry->d_inode;
3131 struct inode_security_struct *isec = inode->i_security;
3132 struct lsm_ioctlop_audit ioctl;
3133 u32 ssid = cred_sid(cred);
3134 struct selinux_audit_data sad = {0,};
3135 int rc;
3136 u8 driver = cmd >> 8;
3137 u8 xperm = cmd & 0xff;
3138
3139 COMMON_AUDIT_DATA_INIT(&ad, IOCTL_OP);
3140 ad.u.op = &ioctl;
3141 ad.u.op->cmd = cmd;
3142 ad.selinux_audit_data = &sad;
3143 ad.u.op->path = file->f_path;
3144
3145 if (ssid != fsec->sid) {
3146 rc = avc_has_perm(ssid, fsec->sid,
3147 SECCLASS_FD,
3148 FD__USE,
3149 &ad);
3150 if (rc)
3151 goto out;
3152 }
3153
3154 if (unlikely(IS_PRIVATE(inode)))
3155 return 0;
3156
3157 rc = avc_has_extended_perms(ssid, isec->sid, isec->sclass,
3158 requested, driver, xperm, &ad);
3159out:
3160 return rc;
3161}
3162
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163static int selinux_file_ioctl(struct file *file, unsigned int cmd,
3164 unsigned long arg)
3165{
David Howells88e67f32008-11-14 10:39:21 +11003166 const struct cred *cred = current_cred();
Eric Paris0b24dcb2011-02-25 15:39:20 -05003167 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168
Eric Paris0b24dcb2011-02-25 15:39:20 -05003169 switch (cmd) {
3170 case FIONREAD:
3171 /* fall through */
3172 case FIBMAP:
3173 /* fall through */
3174 case FIGETBSZ:
3175 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003176 case FS_IOC_GETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003177 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003178 case FS_IOC_GETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003179 error = file_has_perm(cred, file, FILE__GETATTR);
3180 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181
Al Viro2f99c362012-03-23 16:04:05 -04003182 case FS_IOC_SETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003183 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003184 case FS_IOC_SETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003185 error = file_has_perm(cred, file, FILE__SETATTR);
3186 break;
3187
3188 /* sys_ioctl() checks */
3189 case FIONBIO:
3190 /* fall through */
3191 case FIOASYNC:
3192 error = file_has_perm(cred, file, 0);
3193 break;
3194
3195 case KDSKBENT:
3196 case KDSKBSENT:
Eric Paris6a9de492012-01-03 12:25:14 -05003197 error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
3198 SECURITY_CAP_AUDIT);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003199 break;
3200
3201 /* default case assumes that the command will go
3202 * to the file's ioctl() function.
3203 */
3204 default:
Jeff Vander Stoep581be712015-07-10 17:19:56 -04003205 error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003206 }
3207 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208}
3209
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003210static int default_noexec;
3211
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
3213{
David Howells88e67f32008-11-14 10:39:21 +11003214 const struct cred *cred = current_cred();
David Howellsd84f4f92008-11-14 10:39:23 +11003215 int rc = 0;
David Howells88e67f32008-11-14 10:39:21 +11003216
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003217 if (default_noexec &&
3218 (prot & PROT_EXEC) && (!file || (!shared && (prot & PROT_WRITE)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 /*
3220 * We are making executable an anonymous mapping or a
3221 * private file mapping that will also be writable.
3222 * This has an additional check.
3223 */
David Howellsd84f4f92008-11-14 10:39:23 +11003224 rc = cred_has_perm(cred, cred, PROCESS__EXECMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 if (rc)
David Howellsd84f4f92008-11-14 10:39:23 +11003226 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228
3229 if (file) {
3230 /* read access is always possible with a mapping */
3231 u32 av = FILE__READ;
3232
3233 /* write access only matters if the mapping is shared */
3234 if (shared && (prot & PROT_WRITE))
3235 av |= FILE__WRITE;
3236
3237 if (prot & PROT_EXEC)
3238 av |= FILE__EXECUTE;
3239
David Howells88e67f32008-11-14 10:39:21 +11003240 return file_has_perm(cred, file, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 }
David Howellsd84f4f92008-11-14 10:39:23 +11003242
3243error:
3244 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245}
3246
3247static int selinux_file_mmap(struct file *file, unsigned long reqprot,
Eric Parised032182007-06-28 15:55:21 -04003248 unsigned long prot, unsigned long flags,
3249 unsigned long addr, unsigned long addr_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250{
Eric Parised032182007-06-28 15:55:21 -04003251 int rc = 0;
David Howells275bb412008-11-14 10:39:19 +11003252 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253
Eric Paris84336d1a2009-07-31 12:54:05 -04003254 /*
3255 * notice that we are intentionally putting the SELinux check before
3256 * the secondary cap_file_mmap check. This is such a likely attempt
3257 * at bad behaviour/exploit that we always want to get the AVC, even
3258 * if DAC would have also denied the operation.
3259 */
Eric Parisa2551df2009-07-31 12:54:11 -04003260 if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
Eric Parised032182007-06-28 15:55:21 -04003261 rc = avc_has_perm(sid, sid, SECCLASS_MEMPROTECT,
3262 MEMPROTECT__MMAP_ZERO, NULL);
Eric Paris84336d1a2009-07-31 12:54:05 -04003263 if (rc)
3264 return rc;
3265 }
3266
3267 /* do DAC check on address space usage */
3268 rc = cap_file_mmap(file, reqprot, prot, flags, addr, addr_only);
Eric Parised032182007-06-28 15:55:21 -04003269 if (rc || addr_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 return rc;
3271
3272 if (selinux_checkreqprot)
3273 prot = reqprot;
3274
3275 return file_map_prot_check(file, prot,
3276 (flags & MAP_TYPE) == MAP_SHARED);
3277}
3278
3279static int selinux_file_mprotect(struct vm_area_struct *vma,
3280 unsigned long reqprot,
3281 unsigned long prot)
3282{
David Howells88e67f32008-11-14 10:39:21 +11003283 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284
3285 if (selinux_checkreqprot)
3286 prot = reqprot;
3287
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003288 if (default_noexec &&
3289 (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
James Morrisd541bbe2009-01-29 12:19:51 +11003290 int rc = 0;
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003291 if (vma->vm_start >= vma->vm_mm->start_brk &&
3292 vma->vm_end <= vma->vm_mm->brk) {
David Howellsd84f4f92008-11-14 10:39:23 +11003293 rc = cred_has_perm(cred, cred, PROCESS__EXECHEAP);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003294 } else if (!vma->vm_file &&
3295 vma->vm_start <= vma->vm_mm->start_stack &&
3296 vma->vm_end >= vma->vm_mm->start_stack) {
David Howells3b11a1d2008-11-14 10:39:26 +11003297 rc = current_has_perm(current, PROCESS__EXECSTACK);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003298 } else if (vma->vm_file && vma->anon_vma) {
3299 /*
3300 * We are making executable a file mapping that has
3301 * had some COW done. Since pages might have been
3302 * written, check ability to execute the possibly
3303 * modified content. This typically should only
3304 * occur for text relocations.
3305 */
David Howellsd84f4f92008-11-14 10:39:23 +11003306 rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003307 }
Lorenzo Hernandez García-Hierro6b992192005-06-25 14:54:34 -07003308 if (rc)
3309 return rc;
3310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311
3312 return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
3313}
3314
3315static int selinux_file_lock(struct file *file, unsigned int cmd)
3316{
David Howells88e67f32008-11-14 10:39:21 +11003317 const struct cred *cred = current_cred();
3318
3319 return file_has_perm(cred, file, FILE__LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320}
3321
3322static int selinux_file_fcntl(struct file *file, unsigned int cmd,
3323 unsigned long arg)
3324{
David Howells88e67f32008-11-14 10:39:21 +11003325 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 int err = 0;
3327
3328 switch (cmd) {
Eric Paris828dfe12008-04-17 13:17:49 -04003329 case F_SETFL:
3330 if (!file->f_path.dentry || !file->f_path.dentry->d_inode) {
3331 err = -EINVAL;
3332 break;
3333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334
Eric Paris828dfe12008-04-17 13:17:49 -04003335 if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
David Howells88e67f32008-11-14 10:39:21 +11003336 err = file_has_perm(cred, file, FILE__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003338 }
3339 /* fall through */
3340 case F_SETOWN:
3341 case F_SETSIG:
3342 case F_GETFL:
3343 case F_GETOWN:
3344 case F_GETSIG:
3345 /* Just check FD__USE permission */
David Howells88e67f32008-11-14 10:39:21 +11003346 err = file_has_perm(cred, file, 0);
Eric Paris828dfe12008-04-17 13:17:49 -04003347 break;
3348 case F_GETLK:
3349 case F_SETLK:
3350 case F_SETLKW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351#if BITS_PER_LONG == 32
Eric Paris828dfe12008-04-17 13:17:49 -04003352 case F_GETLK64:
3353 case F_SETLK64:
3354 case F_SETLKW64:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355#endif
Eric Paris828dfe12008-04-17 13:17:49 -04003356 if (!file->f_path.dentry || !file->f_path.dentry->d_inode) {
3357 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003359 }
David Howells88e67f32008-11-14 10:39:21 +11003360 err = file_has_perm(cred, file, FILE__LOCK);
Eric Paris828dfe12008-04-17 13:17:49 -04003361 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 }
3363
3364 return err;
3365}
3366
3367static int selinux_file_set_fowner(struct file *file)
3368{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 struct file_security_struct *fsec;
3370
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 fsec = file->f_security;
David Howells275bb412008-11-14 10:39:19 +11003372 fsec->fown_sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373
3374 return 0;
3375}
3376
3377static int selinux_file_send_sigiotask(struct task_struct *tsk,
3378 struct fown_struct *fown, int signum)
3379{
Eric Paris828dfe12008-04-17 13:17:49 -04003380 struct file *file;
Stephen Smalley65c90bc2009-05-04 15:43:18 -04003381 u32 sid = task_sid(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 struct file_security_struct *fsec;
3384
3385 /* struct fown_struct is never outside the context of a struct file */
Eric Paris828dfe12008-04-17 13:17:49 -04003386 file = container_of(fown, struct file, f_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 fsec = file->f_security;
3389
3390 if (!signum)
3391 perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
3392 else
3393 perm = signal_to_av(signum);
3394
David Howells275bb412008-11-14 10:39:19 +11003395 return avc_has_perm(fsec->fown_sid, sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 SECCLASS_PROCESS, perm, NULL);
3397}
3398
3399static int selinux_file_receive(struct file *file)
3400{
David Howells88e67f32008-11-14 10:39:21 +11003401 const struct cred *cred = current_cred();
3402
3403 return file_has_perm(cred, file, file_to_av(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404}
3405
David Howells745ca242008-11-14 10:39:22 +11003406static int selinux_dentry_open(struct file *file, const struct cred *cred)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003407{
3408 struct file_security_struct *fsec;
3409 struct inode *inode;
3410 struct inode_security_struct *isec;
Amir Samuelov6a22e462014-05-26 11:44:06 +03003411 int ret;
3412
3413 ret = pft_file_open(file, cred);
3414 if (ret < 0)
3415 return ret;
David Howellsd84f4f92008-11-14 10:39:23 +11003416
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003417 inode = file->f_path.dentry->d_inode;
3418 fsec = file->f_security;
3419 isec = inode->i_security;
3420 /*
3421 * Save inode label and policy sequence number
3422 * at open-time so that selinux_file_permission
3423 * can determine whether revalidation is necessary.
3424 * Task label is already saved in the file security
3425 * struct as its SID.
3426 */
3427 fsec->isid = isec->sid;
3428 fsec->pseqno = avc_policy_seqno();
3429 /*
3430 * Since the inode label or policy seqno may have changed
3431 * between the selinux_inode_permission check and the saving
3432 * of state above, recheck that access is still permitted.
3433 * Otherwise, access might never be revalidated against the
3434 * new inode label or new policy.
3435 * This check is not redundant - do not remove.
3436 */
Linus Torvalds95f4efb2011-06-08 15:11:56 -07003437 return inode_has_perm_noadp(cred, inode, open_file_to_av(file), 0);
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003438}
3439
Amir Samuelov6a22e462014-05-26 11:44:06 +03003440static int selinux_file_close(struct file *file)
3441{
3442 return pft_file_close(file);
3443}
3444
3445static bool selinux_allow_merge_bio(struct bio *bio1, struct bio *bio2)
3446{
3447 return pft_allow_merge_bio(bio1, bio2);
3448}
3449
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450/* task security operations */
3451
3452static int selinux_task_create(unsigned long clone_flags)
3453{
David Howells3b11a1d2008-11-14 10:39:26 +11003454 return current_has_perm(current, PROCESS__FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455}
3456
David Howellsf1752ee2008-11-14 10:39:17 +11003457/*
David Howellsee18d642009-09-02 09:14:21 +01003458 * allocate the SELinux part of blank credentials
3459 */
3460static int selinux_cred_alloc_blank(struct cred *cred, gfp_t gfp)
3461{
3462 struct task_security_struct *tsec;
3463
3464 tsec = kzalloc(sizeof(struct task_security_struct), gfp);
3465 if (!tsec)
3466 return -ENOMEM;
3467
3468 cred->security = tsec;
3469 return 0;
3470}
3471
3472/*
David Howellsf1752ee2008-11-14 10:39:17 +11003473 * detach and free the LSM part of a set of credentials
3474 */
3475static void selinux_cred_free(struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476{
David Howellsf1752ee2008-11-14 10:39:17 +11003477 struct task_security_struct *tsec = cred->security;
David Howellse0e81732009-09-02 09:13:40 +01003478
Tetsuo Handa2edeaa32011-02-07 13:36:10 +00003479 /*
3480 * cred->security == NULL if security_cred_alloc_blank() or
3481 * security_prepare_creds() returned an error.
3482 */
3483 BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE);
David Howellse0e81732009-09-02 09:13:40 +01003484 cred->security = (void *) 0x7UL;
David Howellsf1752ee2008-11-14 10:39:17 +11003485 kfree(tsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486}
3487
David Howellsd84f4f92008-11-14 10:39:23 +11003488/*
3489 * prepare a new set of credentials for modification
3490 */
3491static int selinux_cred_prepare(struct cred *new, const struct cred *old,
3492 gfp_t gfp)
3493{
3494 const struct task_security_struct *old_tsec;
3495 struct task_security_struct *tsec;
3496
3497 old_tsec = old->security;
3498
3499 tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp);
3500 if (!tsec)
3501 return -ENOMEM;
3502
3503 new->security = tsec;
3504 return 0;
3505}
3506
3507/*
David Howellsee18d642009-09-02 09:14:21 +01003508 * transfer the SELinux data to a blank set of creds
3509 */
3510static void selinux_cred_transfer(struct cred *new, const struct cred *old)
3511{
3512 const struct task_security_struct *old_tsec = old->security;
3513 struct task_security_struct *tsec = new->security;
3514
3515 *tsec = *old_tsec;
3516}
3517
3518/*
David Howells3a3b7ce2008-11-14 10:39:28 +11003519 * set the security data for a kernel service
3520 * - all the creation contexts are set to unlabelled
3521 */
3522static int selinux_kernel_act_as(struct cred *new, u32 secid)
3523{
3524 struct task_security_struct *tsec = new->security;
3525 u32 sid = current_sid();
3526 int ret;
3527
3528 ret = avc_has_perm(sid, secid,
3529 SECCLASS_KERNEL_SERVICE,
3530 KERNEL_SERVICE__USE_AS_OVERRIDE,
3531 NULL);
3532 if (ret == 0) {
3533 tsec->sid = secid;
3534 tsec->create_sid = 0;
3535 tsec->keycreate_sid = 0;
3536 tsec->sockcreate_sid = 0;
3537 }
3538 return ret;
3539}
3540
3541/*
3542 * set the file creation context in a security record to the same as the
3543 * objective context of the specified inode
3544 */
3545static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
3546{
3547 struct inode_security_struct *isec = inode->i_security;
3548 struct task_security_struct *tsec = new->security;
3549 u32 sid = current_sid();
3550 int ret;
3551
3552 ret = avc_has_perm(sid, isec->sid,
3553 SECCLASS_KERNEL_SERVICE,
3554 KERNEL_SERVICE__CREATE_FILES_AS,
3555 NULL);
3556
3557 if (ret == 0)
3558 tsec->create_sid = isec->sid;
David Howellsef574712010-02-26 01:56:16 +00003559 return ret;
David Howells3a3b7ce2008-11-14 10:39:28 +11003560}
3561
Eric Parisdd8dbf22009-11-03 16:35:32 +11003562static int selinux_kernel_module_request(char *kmod_name)
Eric Paris25354c42009-08-13 09:45:03 -04003563{
Eric Parisdd8dbf22009-11-03 16:35:32 +11003564 u32 sid;
3565 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07003566 struct selinux_audit_data sad = {0,};
Eric Parisdd8dbf22009-11-03 16:35:32 +11003567
3568 sid = task_sid(current);
3569
3570 COMMON_AUDIT_DATA_INIT(&ad, KMOD);
Eric Paris3b3b0e42012-04-03 09:37:02 -07003571 ad.selinux_audit_data = &sad;
Eric Parisdd8dbf22009-11-03 16:35:32 +11003572 ad.u.kmod_name = kmod_name;
3573
3574 return avc_has_perm(sid, SECINITSID_KERNEL, SECCLASS_SYSTEM,
3575 SYSTEM__MODULE_REQUEST, &ad);
Eric Paris25354c42009-08-13 09:45:03 -04003576}
3577
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
3579{
David Howells3b11a1d2008-11-14 10:39:26 +11003580 return current_has_perm(p, PROCESS__SETPGID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581}
3582
3583static int selinux_task_getpgid(struct task_struct *p)
3584{
David Howells3b11a1d2008-11-14 10:39:26 +11003585 return current_has_perm(p, PROCESS__GETPGID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586}
3587
3588static int selinux_task_getsid(struct task_struct *p)
3589{
David Howells3b11a1d2008-11-14 10:39:26 +11003590 return current_has_perm(p, PROCESS__GETSESSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591}
3592
David Quigleyf9008e42006-06-30 01:55:46 -07003593static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
3594{
David Howells275bb412008-11-14 10:39:19 +11003595 *secid = task_sid(p);
David Quigleyf9008e42006-06-30 01:55:46 -07003596}
3597
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598static int selinux_task_setnice(struct task_struct *p, int nice)
3599{
3600 int rc;
3601
Eric Paris200ac532009-02-12 15:01:04 -05003602 rc = cap_task_setnice(p, nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 if (rc)
3604 return rc;
3605
David Howells3b11a1d2008-11-14 10:39:26 +11003606 return current_has_perm(p, PROCESS__SETSCHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607}
3608
James Morris03e68062006-06-23 02:03:58 -07003609static int selinux_task_setioprio(struct task_struct *p, int ioprio)
3610{
Serge E. Hallynb5376772007-10-16 23:31:36 -07003611 int rc;
3612
Eric Paris200ac532009-02-12 15:01:04 -05003613 rc = cap_task_setioprio(p, ioprio);
Serge E. Hallynb5376772007-10-16 23:31:36 -07003614 if (rc)
3615 return rc;
3616
David Howells3b11a1d2008-11-14 10:39:26 +11003617 return current_has_perm(p, PROCESS__SETSCHED);
James Morris03e68062006-06-23 02:03:58 -07003618}
3619
David Quigleya1836a42006-06-30 01:55:49 -07003620static int selinux_task_getioprio(struct task_struct *p)
3621{
David Howells3b11a1d2008-11-14 10:39:26 +11003622 return current_has_perm(p, PROCESS__GETSCHED);
David Quigleya1836a42006-06-30 01:55:49 -07003623}
3624
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003625static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
3626 struct rlimit *new_rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627{
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003628 struct rlimit *old_rlim = p->signal->rlim + resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629
3630 /* Control the ability to change the hard limit (whether
3631 lowering or raising it), so that the hard limit can
3632 later be used as a safe reset point for the soft limit
David Howellsd84f4f92008-11-14 10:39:23 +11003633 upon context transitions. See selinux_bprm_committing_creds. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 if (old_rlim->rlim_max != new_rlim->rlim_max)
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003635 return current_has_perm(p, PROCESS__SETRLIMIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636
3637 return 0;
3638}
3639
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09003640static int selinux_task_setscheduler(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641{
Serge E. Hallynb5376772007-10-16 23:31:36 -07003642 int rc;
3643
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09003644 rc = cap_task_setscheduler(p);
Serge E. Hallynb5376772007-10-16 23:31:36 -07003645 if (rc)
3646 return rc;
3647
David Howells3b11a1d2008-11-14 10:39:26 +11003648 return current_has_perm(p, PROCESS__SETSCHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649}
3650
3651static int selinux_task_getscheduler(struct task_struct *p)
3652{
David Howells3b11a1d2008-11-14 10:39:26 +11003653 return current_has_perm(p, PROCESS__GETSCHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654}
3655
David Quigley35601542006-06-23 02:04:01 -07003656static int selinux_task_movememory(struct task_struct *p)
3657{
David Howells3b11a1d2008-11-14 10:39:26 +11003658 return current_has_perm(p, PROCESS__SETSCHED);
David Quigley35601542006-06-23 02:04:01 -07003659}
3660
David Quigleyf9008e42006-06-30 01:55:46 -07003661static int selinux_task_kill(struct task_struct *p, struct siginfo *info,
3662 int sig, u32 secid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663{
3664 u32 perm;
3665 int rc;
3666
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 if (!sig)
3668 perm = PROCESS__SIGNULL; /* null signal; existence test */
3669 else
3670 perm = signal_to_av(sig);
David Quigleyf9008e42006-06-30 01:55:46 -07003671 if (secid)
David Howells275bb412008-11-14 10:39:19 +11003672 rc = avc_has_perm(secid, task_sid(p),
3673 SECCLASS_PROCESS, perm, NULL);
David Quigleyf9008e42006-06-30 01:55:46 -07003674 else
David Howells3b11a1d2008-11-14 10:39:26 +11003675 rc = current_has_perm(p, perm);
David Quigleyf9008e42006-06-30 01:55:46 -07003676 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677}
3678
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679static int selinux_task_wait(struct task_struct *p)
3680{
Eric Paris8a535142007-10-22 16:10:31 -04003681 return task_has_perm(p, current, PROCESS__SIGCHLD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682}
3683
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684static void selinux_task_to_inode(struct task_struct *p,
3685 struct inode *inode)
3686{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 struct inode_security_struct *isec = inode->i_security;
David Howells275bb412008-11-14 10:39:19 +11003688 u32 sid = task_sid(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689
David Howells275bb412008-11-14 10:39:19 +11003690 isec->sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 isec->initialized = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692}
3693
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003695static int selinux_parse_skb_ipv4(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04003696 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697{
3698 int offset, ihlen, ret = -EINVAL;
3699 struct iphdr _iph, *ih;
3700
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03003701 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
3703 if (ih == NULL)
3704 goto out;
3705
3706 ihlen = ih->ihl * 4;
3707 if (ihlen < sizeof(_iph))
3708 goto out;
3709
Eric Paris48c62af2012-04-02 13:15:44 -04003710 ad->u.net->v4info.saddr = ih->saddr;
3711 ad->u.net->v4info.daddr = ih->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 ret = 0;
3713
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003714 if (proto)
3715 *proto = ih->protocol;
3716
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 switch (ih->protocol) {
Eric Paris828dfe12008-04-17 13:17:49 -04003718 case IPPROTO_TCP: {
3719 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720
Eric Paris828dfe12008-04-17 13:17:49 -04003721 if (ntohs(ih->frag_off) & IP_OFFSET)
3722 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723
3724 offset += ihlen;
3725 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3726 if (th == NULL)
3727 break;
3728
Eric Paris48c62af2012-04-02 13:15:44 -04003729 ad->u.net->sport = th->source;
3730 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003732 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733
Eric Paris828dfe12008-04-17 13:17:49 -04003734 case IPPROTO_UDP: {
3735 struct udphdr _udph, *uh;
3736
3737 if (ntohs(ih->frag_off) & IP_OFFSET)
3738 break;
3739
3740 offset += ihlen;
3741 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3742 if (uh == NULL)
3743 break;
3744
Eric Paris48c62af2012-04-02 13:15:44 -04003745 ad->u.net->sport = uh->source;
3746 ad->u.net->dport = uh->dest;
Eric Paris828dfe12008-04-17 13:17:49 -04003747 break;
3748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749
James Morris2ee92d42006-11-13 16:09:01 -08003750 case IPPROTO_DCCP: {
3751 struct dccp_hdr _dccph, *dh;
3752
3753 if (ntohs(ih->frag_off) & IP_OFFSET)
3754 break;
3755
3756 offset += ihlen;
3757 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3758 if (dh == NULL)
3759 break;
3760
Eric Paris48c62af2012-04-02 13:15:44 -04003761 ad->u.net->sport = dh->dccph_sport;
3762 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08003763 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003764 }
James Morris2ee92d42006-11-13 16:09:01 -08003765
Eric Paris828dfe12008-04-17 13:17:49 -04003766 default:
3767 break;
3768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769out:
3770 return ret;
3771}
3772
3773#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3774
3775/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003776static int selinux_parse_skb_ipv6(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04003777 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778{
3779 u8 nexthdr;
3780 int ret = -EINVAL, offset;
3781 struct ipv6hdr _ipv6h, *ip6;
Jesse Gross75f28112011-11-30 17:05:51 -08003782 __be16 frag_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03003784 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3786 if (ip6 == NULL)
3787 goto out;
3788
Eric Paris48c62af2012-04-02 13:15:44 -04003789 ad->u.net->v6info.saddr = ip6->saddr;
3790 ad->u.net->v6info.daddr = ip6->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 ret = 0;
3792
3793 nexthdr = ip6->nexthdr;
3794 offset += sizeof(_ipv6h);
Jesse Gross75f28112011-11-30 17:05:51 -08003795 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 if (offset < 0)
3797 goto out;
3798
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003799 if (proto)
3800 *proto = nexthdr;
3801
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 switch (nexthdr) {
3803 case IPPROTO_TCP: {
Eric Paris828dfe12008-04-17 13:17:49 -04003804 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805
3806 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3807 if (th == NULL)
3808 break;
3809
Eric Paris48c62af2012-04-02 13:15:44 -04003810 ad->u.net->sport = th->source;
3811 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 break;
3813 }
3814
3815 case IPPROTO_UDP: {
3816 struct udphdr _udph, *uh;
3817
3818 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3819 if (uh == NULL)
3820 break;
3821
Eric Paris48c62af2012-04-02 13:15:44 -04003822 ad->u.net->sport = uh->source;
3823 ad->u.net->dport = uh->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 break;
3825 }
3826
James Morris2ee92d42006-11-13 16:09:01 -08003827 case IPPROTO_DCCP: {
3828 struct dccp_hdr _dccph, *dh;
3829
3830 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3831 if (dh == NULL)
3832 break;
3833
Eric Paris48c62af2012-04-02 13:15:44 -04003834 ad->u.net->sport = dh->dccph_sport;
3835 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08003836 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003837 }
James Morris2ee92d42006-11-13 16:09:01 -08003838
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 /* includes fragments */
3840 default:
3841 break;
3842 }
3843out:
3844 return ret;
3845}
3846
3847#endif /* IPV6 */
3848
Thomas Liu2bf49692009-07-14 12:14:09 -04003849static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
David Howellscf9481e2008-07-27 21:31:07 +10003850 char **_addrp, int src, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851{
David Howellscf9481e2008-07-27 21:31:07 +10003852 char *addrp;
3853 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854
Eric Paris48c62af2012-04-02 13:15:44 -04003855 switch (ad->u.net->family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 case PF_INET:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003857 ret = selinux_parse_skb_ipv4(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10003858 if (ret)
3859 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04003860 addrp = (char *)(src ? &ad->u.net->v4info.saddr :
3861 &ad->u.net->v4info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10003862 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863
3864#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3865 case PF_INET6:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003866 ret = selinux_parse_skb_ipv6(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10003867 if (ret)
3868 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04003869 addrp = (char *)(src ? &ad->u.net->v6info.saddr :
3870 &ad->u.net->v6info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10003871 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872#endif /* IPV6 */
3873 default:
David Howellscf9481e2008-07-27 21:31:07 +10003874 addrp = NULL;
3875 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 }
3877
David Howellscf9481e2008-07-27 21:31:07 +10003878parse_error:
3879 printk(KERN_WARNING
3880 "SELinux: failure in selinux_parse_skb(),"
3881 " unable to parse packet\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882 return ret;
David Howellscf9481e2008-07-27 21:31:07 +10003883
3884okay:
3885 if (_addrp)
3886 *_addrp = addrp;
3887 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888}
3889
Paul Moore4f6a9932007-03-01 14:35:22 -05003890/**
Paul Moore220deb92008-01-29 08:38:23 -05003891 * selinux_skb_peerlbl_sid - Determine the peer label of a packet
Paul Moore4f6a9932007-03-01 14:35:22 -05003892 * @skb: the packet
Paul Moore75e22912008-01-29 08:38:04 -05003893 * @family: protocol family
Paul Moore220deb92008-01-29 08:38:23 -05003894 * @sid: the packet's peer label SID
Paul Moore4f6a9932007-03-01 14:35:22 -05003895 *
3896 * Description:
Paul Moore220deb92008-01-29 08:38:23 -05003897 * Check the various different forms of network peer labeling and determine
3898 * the peer label/SID for the packet; most of the magic actually occurs in
3899 * the security server function security_net_peersid_cmp(). The function
3900 * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
3901 * or -EACCES if @sid is invalid due to inconsistencies with the different
3902 * peer labels.
Paul Moore4f6a9932007-03-01 14:35:22 -05003903 *
3904 */
Paul Moore220deb92008-01-29 08:38:23 -05003905static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
Paul Moore4f6a9932007-03-01 14:35:22 -05003906{
Paul Moore71f1cb02008-01-29 08:51:16 -05003907 int err;
Paul Moore4f6a9932007-03-01 14:35:22 -05003908 u32 xfrm_sid;
3909 u32 nlbl_sid;
Paul Moore220deb92008-01-29 08:38:23 -05003910 u32 nlbl_type;
Paul Moore4f6a9932007-03-01 14:35:22 -05003911
3912 selinux_skb_xfrm_sid(skb, &xfrm_sid);
Paul Moore5dbe1eb2008-01-29 08:44:18 -05003913 selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
Paul Moore220deb92008-01-29 08:38:23 -05003914
Paul Moore71f1cb02008-01-29 08:51:16 -05003915 err = security_net_peersid_resolve(nlbl_sid, nlbl_type, xfrm_sid, sid);
3916 if (unlikely(err)) {
3917 printk(KERN_WARNING
3918 "SELinux: failure in selinux_skb_peerlbl_sid(),"
3919 " unable to determine packet's peer label\n");
Paul Moore220deb92008-01-29 08:38:23 -05003920 return -EACCES;
Paul Moore71f1cb02008-01-29 08:51:16 -05003921 }
Paul Moore220deb92008-01-29 08:38:23 -05003922
3923 return 0;
Paul Moore4f6a9932007-03-01 14:35:22 -05003924}
3925
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926/* socket security operations */
Paul Moored4f2d972010-04-22 14:46:18 -04003927
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003928static int socket_sockcreate_sid(const struct task_security_struct *tsec,
3929 u16 secclass, u32 *socksid)
Paul Moored4f2d972010-04-22 14:46:18 -04003930{
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003931 if (tsec->sockcreate_sid > SECSID_NULL) {
3932 *socksid = tsec->sockcreate_sid;
3933 return 0;
3934 }
3935
3936 return security_transition_sid(tsec->sid, tsec->sid, secclass, NULL,
3937 socksid);
Paul Moored4f2d972010-04-22 14:46:18 -04003938}
3939
Paul Moore253bfae2010-04-22 14:46:19 -04003940static int sock_has_perm(struct task_struct *task, struct sock *sk, u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941{
Paul Moore253bfae2010-04-22 14:46:19 -04003942 struct sk_security_struct *sksec = sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04003943 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07003944 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04003945 struct lsm_network_audit net = {0,};
Paul Moore253bfae2010-04-22 14:46:19 -04003946 u32 tsid = task_sid(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947
Satya Durga Srinivasu Prabhala90280652013-09-24 15:23:48 -07003948 if (unlikely(!sksec)) {
3949 pr_warn("SELinux: sksec is NULL, socket is already freed\n");
3950 return -EINVAL;
3951 }
3952
Paul Moore253bfae2010-04-22 14:46:19 -04003953 if (sksec->sid == SECINITSID_KERNEL)
3954 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955
Thomas Liu2bf49692009-07-14 12:14:09 -04003956 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07003957 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04003958 ad.u.net = &net;
3959 ad.u.net->sk = sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960
Paul Moore253bfae2010-04-22 14:46:19 -04003961 return avc_has_perm(tsid, sksec->sid, sksec->sclass, perms, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962}
3963
3964static int selinux_socket_create(int family, int type,
3965 int protocol, int kern)
3966{
Paul Moore5fb49872010-04-22 14:46:19 -04003967 const struct task_security_struct *tsec = current_security();
Paul Moored4f2d972010-04-22 14:46:18 -04003968 u32 newsid;
David Howells275bb412008-11-14 10:39:19 +11003969 u16 secclass;
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003970 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971
3972 if (kern)
Paul Moored4f2d972010-04-22 14:46:18 -04003973 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974
David Howells275bb412008-11-14 10:39:19 +11003975 secclass = socket_type_to_security_class(family, type, protocol);
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003976 rc = socket_sockcreate_sid(tsec, secclass, &newsid);
3977 if (rc)
3978 return rc;
3979
Paul Moored4f2d972010-04-22 14:46:18 -04003980 return avc_has_perm(tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981}
3982
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003983static int selinux_socket_post_create(struct socket *sock, int family,
3984 int type, int protocol, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985{
Paul Moore5fb49872010-04-22 14:46:19 -04003986 const struct task_security_struct *tsec = current_security();
Paul Moored4f2d972010-04-22 14:46:18 -04003987 struct inode_security_struct *isec = SOCK_INODE(sock)->i_security;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07003988 struct sk_security_struct *sksec;
David Howells275bb412008-11-14 10:39:19 +11003989 int err = 0;
3990
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003991 isec->sclass = socket_type_to_security_class(family, type, protocol);
3992
David Howells275bb412008-11-14 10:39:19 +11003993 if (kern)
3994 isec->sid = SECINITSID_KERNEL;
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003995 else {
3996 err = socket_sockcreate_sid(tsec, isec->sclass, &(isec->sid));
3997 if (err)
3998 return err;
3999 }
David Howells275bb412008-11-14 10:39:19 +11004000
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 isec->initialized = 1;
4002
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004003 if (sock->sk) {
4004 sksec = sock->sk->sk_security;
4005 sksec->sid = isec->sid;
Paul Moore220deb92008-01-29 08:38:23 -05004006 sksec->sclass = isec->sclass;
Paul Moore389fb802009-03-27 17:10:34 -04004007 err = selinux_netlbl_socket_post_create(sock->sk, family);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004008 }
4009
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07004010 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011}
4012
4013/* Range of port numbers used to automatically bind.
4014 Need to determine whether we should perform a name_bind
4015 permission check between the socket and the port number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016
4017static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
4018{
Paul Moore253bfae2010-04-22 14:46:19 -04004019 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 u16 family;
4021 int err;
4022
Paul Moore253bfae2010-04-22 14:46:19 -04004023 err = sock_has_perm(current, sk, SOCKET__BIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 if (err)
4025 goto out;
4026
4027 /*
4028 * If PF_INET or PF_INET6, check name_bind permission for the port.
James Morris13402582005-09-30 14:24:34 -04004029 * Multiple address binding for SCTP is not supported yet: we just
4030 * check the first address now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 */
Paul Moore253bfae2010-04-22 14:46:19 -04004032 family = sk->sk_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033 if (family == PF_INET || family == PF_INET6) {
4034 char *addrp;
Paul Moore253bfae2010-04-22 14:46:19 -04004035 struct sk_security_struct *sksec = sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004036 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004037 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004038 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 struct sockaddr_in *addr4 = NULL;
4040 struct sockaddr_in6 *addr6 = NULL;
4041 unsigned short snum;
James Morrise399f982008-06-12 01:39:58 +10004042 u32 sid, node_perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 if (family == PF_INET) {
4045 addr4 = (struct sockaddr_in *)address;
4046 snum = ntohs(addr4->sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 addrp = (char *)&addr4->sin_addr.s_addr;
4048 } else {
4049 addr6 = (struct sockaddr_in6 *)address;
4050 snum = ntohs(addr6->sin6_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 addrp = (char *)&addr6->sin6_addr.s6_addr;
4052 }
4053
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004054 if (snum) {
4055 int low, high;
4056
4057 inet_get_local_port_range(&low, &high);
4058
4059 if (snum < max(PROT_SOCK, low) || snum > high) {
Paul Moore3e112172008-04-10 10:48:14 -04004060 err = sel_netport_sid(sk->sk_protocol,
4061 snum, &sid);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004062 if (err)
4063 goto out;
Thomas Liu2bf49692009-07-14 12:14:09 -04004064 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004065 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004066 ad.u.net = &net;
4067 ad.u.net->sport = htons(snum);
4068 ad.u.net->family = family;
Paul Moore253bfae2010-04-22 14:46:19 -04004069 err = avc_has_perm(sksec->sid, sid,
4070 sksec->sclass,
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004071 SOCKET__NAME_BIND, &ad);
4072 if (err)
4073 goto out;
4074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 }
Eric Paris828dfe12008-04-17 13:17:49 -04004076
Paul Moore253bfae2010-04-22 14:46:19 -04004077 switch (sksec->sclass) {
James Morris13402582005-09-30 14:24:34 -04004078 case SECCLASS_TCP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 node_perm = TCP_SOCKET__NODE_BIND;
4080 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004081
James Morris13402582005-09-30 14:24:34 -04004082 case SECCLASS_UDP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 node_perm = UDP_SOCKET__NODE_BIND;
4084 break;
James Morris2ee92d42006-11-13 16:09:01 -08004085
4086 case SECCLASS_DCCP_SOCKET:
4087 node_perm = DCCP_SOCKET__NODE_BIND;
4088 break;
4089
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090 default:
4091 node_perm = RAWIP_SOCKET__NODE_BIND;
4092 break;
4093 }
Eric Paris828dfe12008-04-17 13:17:49 -04004094
Paul Moore224dfbd2008-01-29 08:38:13 -05004095 err = sel_netnode_sid(addrp, family, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 if (err)
4097 goto out;
Eric Paris828dfe12008-04-17 13:17:49 -04004098
Thomas Liu2bf49692009-07-14 12:14:09 -04004099 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004100 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004101 ad.u.net = &net;
4102 ad.u.net->sport = htons(snum);
4103 ad.u.net->family = family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104
4105 if (family == PF_INET)
Eric Paris48c62af2012-04-02 13:15:44 -04004106 ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 else
Eric Paris48c62af2012-04-02 13:15:44 -04004108 ad.u.net->v6info.saddr = addr6->sin6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109
Paul Moore253bfae2010-04-22 14:46:19 -04004110 err = avc_has_perm(sksec->sid, sid,
4111 sksec->sclass, node_perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 if (err)
4113 goto out;
4114 }
4115out:
4116 return err;
4117}
4118
4119static int selinux_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen)
4120{
Paul Moore014ab192008-10-10 10:16:33 -04004121 struct sock *sk = sock->sk;
Paul Moore253bfae2010-04-22 14:46:19 -04004122 struct sk_security_struct *sksec = sk->sk_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 int err;
4124
Paul Moore253bfae2010-04-22 14:46:19 -04004125 err = sock_has_perm(current, sk, SOCKET__CONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 if (err)
4127 return err;
4128
4129 /*
James Morris2ee92d42006-11-13 16:09:01 -08004130 * If a TCP or DCCP socket, check name_connect permission for the port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 */
Paul Moore253bfae2010-04-22 14:46:19 -04004132 if (sksec->sclass == SECCLASS_TCP_SOCKET ||
4133 sksec->sclass == SECCLASS_DCCP_SOCKET) {
Thomas Liu2bf49692009-07-14 12:14:09 -04004134 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004135 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004136 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 struct sockaddr_in *addr4 = NULL;
4138 struct sockaddr_in6 *addr6 = NULL;
4139 unsigned short snum;
James Morris2ee92d42006-11-13 16:09:01 -08004140 u32 sid, perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141
4142 if (sk->sk_family == PF_INET) {
4143 addr4 = (struct sockaddr_in *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004144 if (addrlen < sizeof(struct sockaddr_in))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 return -EINVAL;
4146 snum = ntohs(addr4->sin_port);
4147 } else {
4148 addr6 = (struct sockaddr_in6 *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004149 if (addrlen < SIN6_LEN_RFC2133)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 return -EINVAL;
4151 snum = ntohs(addr6->sin6_port);
4152 }
4153
Paul Moore3e112172008-04-10 10:48:14 -04004154 err = sel_netport_sid(sk->sk_protocol, snum, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 if (err)
4156 goto out;
4157
Paul Moore253bfae2010-04-22 14:46:19 -04004158 perm = (sksec->sclass == SECCLASS_TCP_SOCKET) ?
James Morris2ee92d42006-11-13 16:09:01 -08004159 TCP_SOCKET__NAME_CONNECT : DCCP_SOCKET__NAME_CONNECT;
4160
Thomas Liu2bf49692009-07-14 12:14:09 -04004161 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004162 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004163 ad.u.net = &net;
4164 ad.u.net->dport = htons(snum);
4165 ad.u.net->family = sk->sk_family;
Paul Moore253bfae2010-04-22 14:46:19 -04004166 err = avc_has_perm(sksec->sid, sid, sksec->sclass, perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 if (err)
4168 goto out;
4169 }
4170
Paul Moore014ab192008-10-10 10:16:33 -04004171 err = selinux_netlbl_socket_connect(sk, address);
4172
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173out:
4174 return err;
4175}
4176
4177static int selinux_socket_listen(struct socket *sock, int backlog)
4178{
Paul Moore253bfae2010-04-22 14:46:19 -04004179 return sock_has_perm(current, sock->sk, SOCKET__LISTEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180}
4181
4182static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
4183{
4184 int err;
4185 struct inode_security_struct *isec;
4186 struct inode_security_struct *newisec;
4187
Paul Moore253bfae2010-04-22 14:46:19 -04004188 err = sock_has_perm(current, sock->sk, SOCKET__ACCEPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 if (err)
4190 return err;
4191
4192 newisec = SOCK_INODE(newsock)->i_security;
4193
4194 isec = SOCK_INODE(sock)->i_security;
4195 newisec->sclass = isec->sclass;
4196 newisec->sid = isec->sid;
4197 newisec->initialized = 1;
4198
4199 return 0;
4200}
4201
4202static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
Eric Paris828dfe12008-04-17 13:17:49 -04004203 int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204{
Paul Moore253bfae2010-04-22 14:46:19 -04004205 return sock_has_perm(current, sock->sk, SOCKET__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206}
4207
4208static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
4209 int size, int flags)
4210{
Paul Moore253bfae2010-04-22 14:46:19 -04004211 return sock_has_perm(current, sock->sk, SOCKET__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212}
4213
4214static int selinux_socket_getsockname(struct socket *sock)
4215{
Paul Moore253bfae2010-04-22 14:46:19 -04004216 return sock_has_perm(current, sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217}
4218
4219static int selinux_socket_getpeername(struct socket *sock)
4220{
Paul Moore253bfae2010-04-22 14:46:19 -04004221 return sock_has_perm(current, sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222}
4223
Eric Paris828dfe12008-04-17 13:17:49 -04004224static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225{
Paul Mooref8687af2006-10-30 15:22:15 -08004226 int err;
4227
Paul Moore253bfae2010-04-22 14:46:19 -04004228 err = sock_has_perm(current, sock->sk, SOCKET__SETOPT);
Paul Mooref8687af2006-10-30 15:22:15 -08004229 if (err)
4230 return err;
4231
4232 return selinux_netlbl_socket_setsockopt(sock, level, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233}
4234
4235static int selinux_socket_getsockopt(struct socket *sock, int level,
4236 int optname)
4237{
Paul Moore253bfae2010-04-22 14:46:19 -04004238 return sock_has_perm(current, sock->sk, SOCKET__GETOPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239}
4240
4241static int selinux_socket_shutdown(struct socket *sock, int how)
4242{
Paul Moore253bfae2010-04-22 14:46:19 -04004243 return sock_has_perm(current, sock->sk, SOCKET__SHUTDOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244}
4245
David S. Miller3610cda2011-01-05 15:38:53 -08004246static int selinux_socket_unix_stream_connect(struct sock *sock,
4247 struct sock *other,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 struct sock *newsk)
4249{
David S. Miller3610cda2011-01-05 15:38:53 -08004250 struct sk_security_struct *sksec_sock = sock->sk_security;
4251 struct sk_security_struct *sksec_other = other->sk_security;
Paul Moore4d1e2452010-04-22 14:46:18 -04004252 struct sk_security_struct *sksec_new = newsk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004253 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004254 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004255 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 int err;
4257
Thomas Liu2bf49692009-07-14 12:14:09 -04004258 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004259 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004260 ad.u.net = &net;
4261 ad.u.net->sk = other;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262
Paul Moore4d1e2452010-04-22 14:46:18 -04004263 err = avc_has_perm(sksec_sock->sid, sksec_other->sid,
4264 sksec_other->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 UNIX_STREAM_SOCKET__CONNECTTO, &ad);
4266 if (err)
4267 return err;
4268
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 /* server child socket */
Paul Moore4d1e2452010-04-22 14:46:18 -04004270 sksec_new->peer_sid = sksec_sock->sid;
4271 err = security_sid_mls_copy(sksec_other->sid, sksec_sock->sid,
4272 &sksec_new->sid);
4273 if (err)
4274 return err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004275
Paul Moore4d1e2452010-04-22 14:46:18 -04004276 /* connecting socket */
4277 sksec_sock->peer_sid = sksec_new->sid;
4278
4279 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280}
4281
4282static int selinux_socket_unix_may_send(struct socket *sock,
4283 struct socket *other)
4284{
Paul Moore253bfae2010-04-22 14:46:19 -04004285 struct sk_security_struct *ssec = sock->sk->sk_security;
4286 struct sk_security_struct *osec = other->sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004287 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004288 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004289 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290
Thomas Liu2bf49692009-07-14 12:14:09 -04004291 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004292 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004293 ad.u.net = &net;
4294 ad.u.net->sk = other->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295
Paul Moore253bfae2010-04-22 14:46:19 -04004296 return avc_has_perm(ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
4297 &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298}
4299
Paul Mooreeffad8d2008-01-29 08:49:27 -05004300static int selinux_inet_sys_rcv_skb(int ifindex, char *addrp, u16 family,
4301 u32 peer_sid,
Thomas Liu2bf49692009-07-14 12:14:09 -04004302 struct common_audit_data *ad)
Paul Mooreeffad8d2008-01-29 08:49:27 -05004303{
4304 int err;
4305 u32 if_sid;
4306 u32 node_sid;
4307
4308 err = sel_netif_sid(ifindex, &if_sid);
4309 if (err)
4310 return err;
4311 err = avc_has_perm(peer_sid, if_sid,
4312 SECCLASS_NETIF, NETIF__INGRESS, ad);
4313 if (err)
4314 return err;
4315
4316 err = sel_netnode_sid(addrp, family, &node_sid);
4317 if (err)
4318 return err;
4319 return avc_has_perm(peer_sid, node_sid,
4320 SECCLASS_NODE, NODE__RECVFROM, ad);
4321}
4322
Paul Moore220deb92008-01-29 08:38:23 -05004323static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
Paul Moored8395c82008-10-10 10:16:30 -04004324 u16 family)
Paul Moore220deb92008-01-29 08:38:23 -05004325{
Paul Moore277d3422008-12-31 12:54:11 -05004326 int err = 0;
Paul Moore220deb92008-01-29 08:38:23 -05004327 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004328 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004329 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004330 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004331 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04004332 char *addrp;
4333
Thomas Liu2bf49692009-07-14 12:14:09 -04004334 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004335 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004336 ad.u.net = &net;
4337 ad.u.net->netif = skb->skb_iif;
4338 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004339 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4340 if (err)
4341 return err;
Paul Moore220deb92008-01-29 08:38:23 -05004342
Paul Moore58bfbb52009-03-27 17:10:41 -04004343 if (selinux_secmark_enabled()) {
Paul Moore220deb92008-01-29 08:38:23 -05004344 err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Moored8395c82008-10-10 10:16:30 -04004345 PACKET__RECV, &ad);
Paul Moore58bfbb52009-03-27 17:10:41 -04004346 if (err)
4347 return err;
4348 }
Paul Moore220deb92008-01-29 08:38:23 -05004349
Steffen Klassertb9679a72011-02-23 12:55:21 +01004350 err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
4351 if (err)
4352 return err;
4353 err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004354
James Morris4e5ab4c2006-06-09 00:33:33 -07004355 return err;
4356}
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004357
James Morris4e5ab4c2006-06-09 00:33:33 -07004358static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
4359{
Paul Moore220deb92008-01-29 08:38:23 -05004360 int err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004361 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004362 u16 family = sk->sk_family;
4363 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004364 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004365 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004366 struct lsm_network_audit net = {0,};
Paul Moore220deb92008-01-29 08:38:23 -05004367 char *addrp;
Paul Moored8395c82008-10-10 10:16:30 -04004368 u8 secmark_active;
4369 u8 peerlbl_active;
James Morris4e5ab4c2006-06-09 00:33:33 -07004370
James Morris4e5ab4c2006-06-09 00:33:33 -07004371 if (family != PF_INET && family != PF_INET6)
Paul Moore220deb92008-01-29 08:38:23 -05004372 return 0;
James Morris4e5ab4c2006-06-09 00:33:33 -07004373
4374 /* Handle mapped IPv4 packets arriving via IPv6 sockets */
Al Viro87fcd702006-12-04 22:00:55 +00004375 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
James Morris4e5ab4c2006-06-09 00:33:33 -07004376 family = PF_INET;
4377
Paul Moored8395c82008-10-10 10:16:30 -04004378 /* If any sort of compatibility mode is enabled then handoff processing
4379 * to the selinux_sock_rcv_skb_compat() function to deal with the
4380 * special handling. We do this in an attempt to keep this function
4381 * as fast and as clean as possible. */
Paul Moore58bfbb52009-03-27 17:10:41 -04004382 if (!selinux_policycap_netpeer)
Paul Moored8395c82008-10-10 10:16:30 -04004383 return selinux_sock_rcv_skb_compat(sk, skb, family);
4384
4385 secmark_active = selinux_secmark_enabled();
4386 peerlbl_active = netlbl_enabled() || selinux_xfrm_enabled();
4387 if (!secmark_active && !peerlbl_active)
4388 return 0;
4389
Thomas Liu2bf49692009-07-14 12:14:09 -04004390 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004391 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004392 ad.u.net = &net;
4393 ad.u.net->netif = skb->skb_iif;
4394 ad.u.net->family = family;
Paul Moore224dfbd2008-01-29 08:38:13 -05004395 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
James Morris4e5ab4c2006-06-09 00:33:33 -07004396 if (err)
Paul Moore220deb92008-01-29 08:38:23 -05004397 return err;
James Morris4e5ab4c2006-06-09 00:33:33 -07004398
Paul Moored8395c82008-10-10 10:16:30 -04004399 if (peerlbl_active) {
Paul Moored621d352008-01-29 08:43:36 -05004400 u32 peer_sid;
4401
4402 err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
4403 if (err)
4404 return err;
Eric Dumazet8964be42009-11-20 15:35:04 -08004405 err = selinux_inet_sys_rcv_skb(skb->skb_iif, addrp, family,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004406 peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04004407 if (err) {
4408 selinux_netlbl_err(skb, err, 0);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004409 return err;
Paul Mooredfaebe92008-10-10 10:16:31 -04004410 }
Paul Moored621d352008-01-29 08:43:36 -05004411 err = avc_has_perm(sk_sid, peer_sid, SECCLASS_PEER,
4412 PEER__RECV, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04004413 if (err)
4414 selinux_netlbl_err(skb, err, 0);
Paul Moored621d352008-01-29 08:43:36 -05004415 }
4416
Paul Moored8395c82008-10-10 10:16:30 -04004417 if (secmark_active) {
Paul Mooreeffad8d2008-01-29 08:49:27 -05004418 err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
4419 PACKET__RECV, &ad);
4420 if (err)
4421 return err;
4422 }
4423
Paul Moored621d352008-01-29 08:43:36 -05004424 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425}
4426
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004427static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
4428 int __user *optlen, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429{
4430 int err = 0;
4431 char *scontext;
4432 u32 scontext_len;
Paul Moore253bfae2010-04-22 14:46:19 -04004433 struct sk_security_struct *sksec = sock->sk->sk_security;
Paul Moore3de4bab2006-11-17 17:38:54 -05004434 u32 peer_sid = SECSID_NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435
Paul Moore253bfae2010-04-22 14:46:19 -04004436 if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
4437 sksec->sclass == SECCLASS_TCP_SOCKET)
Eric Parisdd3e7832010-04-07 15:08:46 -04004438 peer_sid = sksec->peer_sid;
Paul Moore253bfae2010-04-22 14:46:19 -04004439 if (peer_sid == SECSID_NULL)
4440 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004442 err = security_sid_to_context(peer_sid, &scontext, &scontext_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 if (err)
Paul Moore253bfae2010-04-22 14:46:19 -04004444 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445
4446 if (scontext_len > len) {
4447 err = -ERANGE;
4448 goto out_len;
4449 }
4450
4451 if (copy_to_user(optval, scontext, scontext_len))
4452 err = -EFAULT;
4453
4454out_len:
4455 if (put_user(scontext_len, optlen))
4456 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 kfree(scontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 return err;
4459}
4460
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004461static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004462{
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004463 u32 peer_secid = SECSID_NULL;
Paul Moore75e22912008-01-29 08:38:04 -05004464 u16 family;
Catherine Zhang877ce7c2006-06-29 12:27:47 -07004465
Paul Mooreaa862902008-10-10 10:16:29 -04004466 if (skb && skb->protocol == htons(ETH_P_IP))
4467 family = PF_INET;
4468 else if (skb && skb->protocol == htons(ETH_P_IPV6))
4469 family = PF_INET6;
4470 else if (sock)
Paul Moore75e22912008-01-29 08:38:04 -05004471 family = sock->sk->sk_family;
Paul Moore75e22912008-01-29 08:38:04 -05004472 else
4473 goto out;
4474
4475 if (sock && family == PF_UNIX)
Ahmed S. Darwish713a04a2008-03-01 21:52:30 +02004476 selinux_inode_getsecid(SOCK_INODE(sock), &peer_secid);
Paul Moore3de4bab2006-11-17 17:38:54 -05004477 else if (skb)
Paul Moore220deb92008-01-29 08:38:23 -05004478 selinux_skb_peerlbl_sid(skb, family, &peer_secid);
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004479
Paul Moore75e22912008-01-29 08:38:04 -05004480out:
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004481 *secid = peer_secid;
Paul Moore75e22912008-01-29 08:38:04 -05004482 if (peer_secid == SECSID_NULL)
4483 return -EINVAL;
4484 return 0;
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004485}
4486
Al Viro7d877f32005-10-21 03:20:43 -04004487static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488{
Paul Moore84914b72010-04-22 14:46:18 -04004489 struct sk_security_struct *sksec;
4490
4491 sksec = kzalloc(sizeof(*sksec), priority);
4492 if (!sksec)
4493 return -ENOMEM;
4494
4495 sksec->peer_sid = SECINITSID_UNLABELED;
4496 sksec->sid = SECINITSID_UNLABELED;
4497 selinux_netlbl_sk_security_reset(sksec);
4498 sk->sk_security = sksec;
4499
4500 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501}
4502
4503static void selinux_sk_free_security(struct sock *sk)
4504{
Paul Moore84914b72010-04-22 14:46:18 -04004505 struct sk_security_struct *sksec = sk->sk_security;
4506
4507 sk->sk_security = NULL;
4508 selinux_netlbl_sk_security_free(sksec);
4509 kfree(sksec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510}
4511
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004512static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
4513{
Eric Parisdd3e7832010-04-07 15:08:46 -04004514 struct sk_security_struct *sksec = sk->sk_security;
4515 struct sk_security_struct *newsksec = newsk->sk_security;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004516
Eric Parisdd3e7832010-04-07 15:08:46 -04004517 newsksec->sid = sksec->sid;
4518 newsksec->peer_sid = sksec->peer_sid;
4519 newsksec->sclass = sksec->sclass;
Paul Moore99f59ed2006-08-29 17:53:48 -07004520
Eric Parisdd3e7832010-04-07 15:08:46 -04004521 selinux_netlbl_sk_security_reset(newsksec);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004522}
4523
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07004524static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004525{
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004526 if (!sk)
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07004527 *secid = SECINITSID_ANY_SOCKET;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004528 else {
4529 struct sk_security_struct *sksec = sk->sk_security;
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004530
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07004531 *secid = sksec->sid;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004532 }
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004533}
4534
Eric Paris828dfe12008-04-17 13:17:49 -04004535static void selinux_sock_graft(struct sock *sk, struct socket *parent)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004536{
4537 struct inode_security_struct *isec = SOCK_INODE(parent)->i_security;
4538 struct sk_security_struct *sksec = sk->sk_security;
4539
David Woodhouse2148ccc2006-09-29 15:50:25 -07004540 if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
4541 sk->sk_family == PF_UNIX)
4542 isec->sid = sksec->sid;
Paul Moore220deb92008-01-29 08:38:23 -05004543 sksec->sclass = isec->sclass;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004544}
4545
Adrian Bunk9a673e52006-08-15 00:03:53 -07004546static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4547 struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004548{
4549 struct sk_security_struct *sksec = sk->sk_security;
4550 int err;
Paul Mooreaa862902008-10-10 10:16:29 -04004551 u16 family = sk->sk_family;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07004552 u32 newsid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004553 u32 peersid;
4554
Paul Mooreaa862902008-10-10 10:16:29 -04004555 /* handle mapped IPv4 packets arriving via IPv6 sockets */
4556 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4557 family = PF_INET;
4558
4559 err = selinux_skb_peerlbl_sid(skb, family, &peersid);
Paul Moore220deb92008-01-29 08:38:23 -05004560 if (err)
4561 return err;
Venkat Yekkiralaa51c64f2006-07-27 22:01:34 -07004562 if (peersid == SECSID_NULL) {
4563 req->secid = sksec->sid;
Paul Moore3de4bab2006-11-17 17:38:54 -05004564 req->peer_secid = SECSID_NULL;
Paul Moore389fb802009-03-27 17:10:34 -04004565 } else {
4566 err = security_sid_mls_copy(sksec->sid, peersid, &newsid);
4567 if (err)
4568 return err;
4569 req->secid = newsid;
4570 req->peer_secid = peersid;
Venkat Yekkiralaa51c64f2006-07-27 22:01:34 -07004571 }
4572
Paul Moore389fb802009-03-27 17:10:34 -04004573 return selinux_netlbl_inet_conn_request(req, family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004574}
4575
Adrian Bunk9a673e52006-08-15 00:03:53 -07004576static void selinux_inet_csk_clone(struct sock *newsk,
4577 const struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004578{
4579 struct sk_security_struct *newsksec = newsk->sk_security;
4580
4581 newsksec->sid = req->secid;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004582 newsksec->peer_sid = req->peer_secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004583 /* NOTE: Ideally, we should also get the isec->sid for the
4584 new socket in sync, but we don't have the isec available yet.
4585 So we will wait until sock_graft to do it, by which
4586 time it will have been created and available. */
Paul Moore99f59ed2006-08-29 17:53:48 -07004587
Paul Moore9f2ad662006-11-17 17:38:53 -05004588 /* We don't need to take any sort of lock here as we are the only
4589 * thread with access to newsksec */
Paul Moore389fb802009-03-27 17:10:34 -04004590 selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004591}
4592
Paul Moore014ab192008-10-10 10:16:33 -04004593static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004594{
Paul Mooreaa862902008-10-10 10:16:29 -04004595 u16 family = sk->sk_family;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004596 struct sk_security_struct *sksec = sk->sk_security;
4597
Paul Mooreaa862902008-10-10 10:16:29 -04004598 /* handle mapped IPv4 packets arriving via IPv6 sockets */
4599 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4600 family = PF_INET;
4601
4602 selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004603}
4604
Eric Paris2606fd12010-10-13 16:24:41 -04004605static int selinux_secmark_relabel_packet(u32 sid)
4606{
4607 const struct task_security_struct *__tsec;
4608 u32 tsid;
4609
4610 __tsec = current_security();
4611 tsid = __tsec->sid;
4612
4613 return avc_has_perm(tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO, NULL);
4614}
4615
4616static void selinux_secmark_refcount_inc(void)
4617{
4618 atomic_inc(&selinux_secmark_refcount);
4619}
4620
4621static void selinux_secmark_refcount_dec(void)
4622{
4623 atomic_dec(&selinux_secmark_refcount);
4624}
4625
Adrian Bunk9a673e52006-08-15 00:03:53 -07004626static void selinux_req_classify_flow(const struct request_sock *req,
4627 struct flowi *fl)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004628{
David S. Miller1d28f422011-03-12 00:29:39 -05004629 fl->flowi_secid = req->secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004630}
4631
Paul Mooreed6d76e2009-08-28 18:12:49 -04004632static int selinux_tun_dev_create(void)
4633{
4634 u32 sid = current_sid();
4635
4636 /* we aren't taking into account the "sockcreate" SID since the socket
4637 * that is being created here is not a socket in the traditional sense,
4638 * instead it is a private sock, accessible only to the kernel, and
4639 * representing a wide range of network traffic spanning multiple
4640 * connections unlike traditional sockets - check the TUN driver to
4641 * get a better understanding of why this socket is special */
4642
4643 return avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
4644 NULL);
4645}
4646
4647static void selinux_tun_dev_post_create(struct sock *sk)
4648{
4649 struct sk_security_struct *sksec = sk->sk_security;
4650
4651 /* we don't currently perform any NetLabel based labeling here and it
4652 * isn't clear that we would want to do so anyway; while we could apply
4653 * labeling without the support of the TUN user the resulting labeled
4654 * traffic from the other end of the connection would almost certainly
4655 * cause confusion to the TUN user that had no idea network labeling
4656 * protocols were being used */
4657
4658 /* see the comments in selinux_tun_dev_create() about why we don't use
4659 * the sockcreate SID here */
4660
4661 sksec->sid = current_sid();
4662 sksec->sclass = SECCLASS_TUN_SOCKET;
4663}
4664
4665static int selinux_tun_dev_attach(struct sock *sk)
4666{
4667 struct sk_security_struct *sksec = sk->sk_security;
4668 u32 sid = current_sid();
4669 int err;
4670
4671 err = avc_has_perm(sid, sksec->sid, SECCLASS_TUN_SOCKET,
4672 TUN_SOCKET__RELABELFROM, NULL);
4673 if (err)
4674 return err;
4675 err = avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET,
4676 TUN_SOCKET__RELABELTO, NULL);
4677 if (err)
4678 return err;
4679
4680 sksec->sid = sid;
4681
4682 return 0;
4683}
4684
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
4686{
4687 int err = 0;
4688 u32 perm;
4689 struct nlmsghdr *nlh;
Paul Moore253bfae2010-04-22 14:46:19 -04004690 struct sk_security_struct *sksec = sk->sk_security;
Eric Paris828dfe12008-04-17 13:17:49 -04004691
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 if (skb->len < NLMSG_SPACE(0)) {
4693 err = -EINVAL;
4694 goto out;
4695 }
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -07004696 nlh = nlmsg_hdr(skb);
Eric Paris828dfe12008-04-17 13:17:49 -04004697
Paul Moore253bfae2010-04-22 14:46:19 -04004698 err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 if (err) {
4700 if (err == -EINVAL) {
David Woodhouse9ad9ad32005-06-22 15:04:33 +01004701 audit_log(current->audit_context, GFP_KERNEL, AUDIT_SELINUX_ERR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702 "SELinux: unrecognized netlink message"
4703 " type=%hu for sclass=%hu\n",
Paul Moore253bfae2010-04-22 14:46:19 -04004704 nlh->nlmsg_type, sksec->sclass);
Eric Paris39c9aed2008-11-05 09:34:42 -05004705 if (!selinux_enforcing || security_get_allow_unknown())
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 err = 0;
4707 }
4708
4709 /* Ignore */
4710 if (err == -ENOENT)
4711 err = 0;
4712 goto out;
4713 }
4714
Paul Moore253bfae2010-04-22 14:46:19 -04004715 err = sock_has_perm(current, sk, perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716out:
4717 return err;
4718}
4719
4720#ifdef CONFIG_NETFILTER
4721
Paul Mooreeffad8d2008-01-29 08:49:27 -05004722static unsigned int selinux_ip_forward(struct sk_buff *skb, int ifindex,
4723 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724{
Paul Mooredfaebe92008-10-10 10:16:31 -04004725 int err;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004726 char *addrp;
4727 u32 peer_sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004728 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004729 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004730 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05004731 u8 secmark_active;
Paul Moore948bf852008-10-10 10:16:32 -04004732 u8 netlbl_active;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004733 u8 peerlbl_active;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004734
Paul Mooreeffad8d2008-01-29 08:49:27 -05004735 if (!selinux_policycap_netpeer)
4736 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004737
Paul Mooreeffad8d2008-01-29 08:49:27 -05004738 secmark_active = selinux_secmark_enabled();
Paul Moore948bf852008-10-10 10:16:32 -04004739 netlbl_active = netlbl_enabled();
4740 peerlbl_active = netlbl_active || selinux_xfrm_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05004741 if (!secmark_active && !peerlbl_active)
4742 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004743
Paul Moored8395c82008-10-10 10:16:30 -04004744 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
4745 return NF_DROP;
4746
Thomas Liu2bf49692009-07-14 12:14:09 -04004747 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004748 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004749 ad.u.net = &net;
4750 ad.u.net->netif = ifindex;
4751 ad.u.net->family = family;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004752 if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
4753 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754
Paul Mooredfaebe92008-10-10 10:16:31 -04004755 if (peerlbl_active) {
4756 err = selinux_inet_sys_rcv_skb(ifindex, addrp, family,
4757 peer_sid, &ad);
4758 if (err) {
4759 selinux_netlbl_err(skb, err, 1);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004760 return NF_DROP;
Paul Mooredfaebe92008-10-10 10:16:31 -04004761 }
4762 }
Paul Mooreeffad8d2008-01-29 08:49:27 -05004763
4764 if (secmark_active)
4765 if (avc_has_perm(peer_sid, skb->secmark,
4766 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
4767 return NF_DROP;
4768
Paul Moore948bf852008-10-10 10:16:32 -04004769 if (netlbl_active)
4770 /* we do this in the FORWARD path and not the POST_ROUTING
4771 * path because we want to make sure we apply the necessary
4772 * labeling before IPsec is applied so we can leverage AH
4773 * protection */
4774 if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
4775 return NF_DROP;
4776
Paul Mooreeffad8d2008-01-29 08:49:27 -05004777 return NF_ACCEPT;
4778}
4779
4780static unsigned int selinux_ipv4_forward(unsigned int hooknum,
4781 struct sk_buff *skb,
4782 const struct net_device *in,
4783 const struct net_device *out,
4784 int (*okfn)(struct sk_buff *))
4785{
4786 return selinux_ip_forward(skb, in->ifindex, PF_INET);
4787}
4788
4789#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
4790static unsigned int selinux_ipv6_forward(unsigned int hooknum,
4791 struct sk_buff *skb,
4792 const struct net_device *in,
4793 const struct net_device *out,
4794 int (*okfn)(struct sk_buff *))
4795{
4796 return selinux_ip_forward(skb, in->ifindex, PF_INET6);
4797}
4798#endif /* IPV6 */
4799
Paul Moore948bf852008-10-10 10:16:32 -04004800static unsigned int selinux_ip_output(struct sk_buff *skb,
4801 u16 family)
4802{
4803 u32 sid;
4804
4805 if (!netlbl_enabled())
4806 return NF_ACCEPT;
4807
4808 /* we do this in the LOCAL_OUT path and not the POST_ROUTING path
4809 * because we want to make sure we apply the necessary labeling
4810 * before IPsec is applied so we can leverage AH protection */
4811 if (skb->sk) {
4812 struct sk_security_struct *sksec = skb->sk->sk_security;
4813 sid = sksec->sid;
4814 } else
4815 sid = SECINITSID_KERNEL;
4816 if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
4817 return NF_DROP;
4818
4819 return NF_ACCEPT;
4820}
4821
4822static unsigned int selinux_ipv4_output(unsigned int hooknum,
4823 struct sk_buff *skb,
4824 const struct net_device *in,
4825 const struct net_device *out,
4826 int (*okfn)(struct sk_buff *))
4827{
4828 return selinux_ip_output(skb, PF_INET);
4829}
4830
Paul Mooreeffad8d2008-01-29 08:49:27 -05004831static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
4832 int ifindex,
Paul Moored8395c82008-10-10 10:16:30 -04004833 u16 family)
James Morris4e5ab4c2006-06-09 00:33:33 -07004834{
Paul Mooreeffad8d2008-01-29 08:49:27 -05004835 struct sock *sk = skb->sk;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004836 struct sk_security_struct *sksec;
Thomas Liu2bf49692009-07-14 12:14:09 -04004837 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004838 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004839 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04004840 char *addrp;
4841 u8 proto;
James Morris4e5ab4c2006-06-09 00:33:33 -07004842
Paul Mooreeffad8d2008-01-29 08:49:27 -05004843 if (sk == NULL)
4844 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004845 sksec = sk->sk_security;
James Morris4e5ab4c2006-06-09 00:33:33 -07004846
Thomas Liu2bf49692009-07-14 12:14:09 -04004847 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004848 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004849 ad.u.net = &net;
4850 ad.u.net->netif = ifindex;
4851 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004852 if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
4853 return NF_DROP;
4854
Paul Moore58bfbb52009-03-27 17:10:41 -04004855 if (selinux_secmark_enabled())
Paul Mooreeffad8d2008-01-29 08:49:27 -05004856 if (avc_has_perm(sksec->sid, skb->secmark,
Paul Moored8395c82008-10-10 10:16:30 -04004857 SECCLASS_PACKET, PACKET__SEND, &ad))
Eric Paris2fe66ec2010-11-23 06:28:08 +00004858 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07004859
Steffen Klassertb9679a72011-02-23 12:55:21 +01004860 if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
4861 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07004862
Paul Mooreeffad8d2008-01-29 08:49:27 -05004863 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864}
4865
Paul Mooreeffad8d2008-01-29 08:49:27 -05004866static unsigned int selinux_ip_postroute(struct sk_buff *skb, int ifindex,
4867 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868{
Paul Mooreeffad8d2008-01-29 08:49:27 -05004869 u32 secmark_perm;
4870 u32 peer_sid;
4871 struct sock *sk;
Thomas Liu2bf49692009-07-14 12:14:09 -04004872 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07004873 struct selinux_audit_data sad = {0,};
Eric Paris48c62af2012-04-02 13:15:44 -04004874 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05004875 char *addrp;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004876 u8 secmark_active;
4877 u8 peerlbl_active;
4878
Paul Mooreeffad8d2008-01-29 08:49:27 -05004879 /* If any sort of compatibility mode is enabled then handoff processing
4880 * to the selinux_ip_postroute_compat() function to deal with the
4881 * special handling. We do this in an attempt to keep this function
4882 * as fast and as clean as possible. */
Paul Moore58bfbb52009-03-27 17:10:41 -04004883 if (!selinux_policycap_netpeer)
Paul Moored8395c82008-10-10 10:16:30 -04004884 return selinux_ip_postroute_compat(skb, ifindex, family);
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -07004885#ifdef CONFIG_XFRM
Paul Mooreeffad8d2008-01-29 08:49:27 -05004886 /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
4887 * packet transformation so allow the packet to pass without any checks
4888 * since we'll have another chance to perform access control checks
4889 * when the packet is on it's final way out.
4890 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
4891 * is NULL, in this case go ahead and apply access control. */
Eric Dumazetadf30902009-06-02 05:19:30 +00004892 if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL)
Paul Mooreeffad8d2008-01-29 08:49:27 -05004893 return NF_ACCEPT;
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -07004894#endif
Paul Mooreeffad8d2008-01-29 08:49:27 -05004895 secmark_active = selinux_secmark_enabled();
4896 peerlbl_active = netlbl_enabled() || selinux_xfrm_enabled();
4897 if (!secmark_active && !peerlbl_active)
4898 return NF_ACCEPT;
4899
Paul Moored8395c82008-10-10 10:16:30 -04004900 /* if the packet is being forwarded then get the peer label from the
4901 * packet itself; otherwise check to see if it is from a local
4902 * application or the kernel, if from an application get the peer label
4903 * from the sending socket, otherwise use the kernel's sid */
Paul Mooreeffad8d2008-01-29 08:49:27 -05004904 sk = skb->sk;
Paul Moored8395c82008-10-10 10:16:30 -04004905 if (sk == NULL) {
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01004906 if (skb->skb_iif) {
4907 secmark_perm = PACKET__FORWARD_OUT;
Paul Moored8395c82008-10-10 10:16:30 -04004908 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00004909 return NF_DROP;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01004910 } else {
4911 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04004912 peer_sid = SECINITSID_KERNEL;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01004913 }
Paul Moored8395c82008-10-10 10:16:30 -04004914 } else {
Paul Mooreeffad8d2008-01-29 08:49:27 -05004915 struct sk_security_struct *sksec = sk->sk_security;
4916 peer_sid = sksec->sid;
4917 secmark_perm = PACKET__SEND;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004918 }
4919
Thomas Liu2bf49692009-07-14 12:14:09 -04004920 COMMON_AUDIT_DATA_INIT(&ad, NET);
Eric Paris3b3b0e42012-04-03 09:37:02 -07004921 ad.selinux_audit_data = &sad;
Eric Paris48c62af2012-04-02 13:15:44 -04004922 ad.u.net = &net;
4923 ad.u.net->netif = ifindex;
4924 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004925 if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
Eric Paris04f6d702010-11-23 06:28:02 +00004926 return NF_DROP;
Paul Moored8395c82008-10-10 10:16:30 -04004927
Paul Mooreeffad8d2008-01-29 08:49:27 -05004928 if (secmark_active)
4929 if (avc_has_perm(peer_sid, skb->secmark,
4930 SECCLASS_PACKET, secmark_perm, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00004931 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004932
4933 if (peerlbl_active) {
4934 u32 if_sid;
4935 u32 node_sid;
4936
4937 if (sel_netif_sid(ifindex, &if_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00004938 return NF_DROP;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004939 if (avc_has_perm(peer_sid, if_sid,
4940 SECCLASS_NETIF, NETIF__EGRESS, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00004941 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004942
4943 if (sel_netnode_sid(addrp, family, &node_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00004944 return NF_DROP;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004945 if (avc_has_perm(peer_sid, node_sid,
4946 SECCLASS_NODE, NODE__SENDTO, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00004947 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004948 }
4949
4950 return NF_ACCEPT;
4951}
4952
4953static unsigned int selinux_ipv4_postroute(unsigned int hooknum,
4954 struct sk_buff *skb,
4955 const struct net_device *in,
4956 const struct net_device *out,
4957 int (*okfn)(struct sk_buff *))
4958{
4959 return selinux_ip_postroute(skb, out->ifindex, PF_INET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960}
4961
4962#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Paul Mooreeffad8d2008-01-29 08:49:27 -05004963static unsigned int selinux_ipv6_postroute(unsigned int hooknum,
4964 struct sk_buff *skb,
4965 const struct net_device *in,
4966 const struct net_device *out,
4967 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968{
Paul Mooreeffad8d2008-01-29 08:49:27 -05004969 return selinux_ip_postroute(skb, out->ifindex, PF_INET6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971#endif /* IPV6 */
4972
4973#endif /* CONFIG_NETFILTER */
4974
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
4976{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 int err;
4978
Eric Paris200ac532009-02-12 15:01:04 -05004979 err = cap_netlink_send(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 if (err)
4981 return err;
4982
Stephen Smalley941fc5b2009-10-01 14:48:23 -04004983 return selinux_nlmsg_perm(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984}
4985
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986static int ipc_alloc_security(struct task_struct *task,
4987 struct kern_ipc_perm *perm,
4988 u16 sclass)
4989{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990 struct ipc_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +11004991 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992
James Morris89d155e2005-10-30 14:59:21 -08004993 isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994 if (!isec)
4995 return -ENOMEM;
4996
David Howells275bb412008-11-14 10:39:19 +11004997 sid = task_sid(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998 isec->sclass = sclass;
David Howells275bb412008-11-14 10:39:19 +11004999 isec->sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000 perm->security = isec;
5001
5002 return 0;
5003}
5004
5005static void ipc_free_security(struct kern_ipc_perm *perm)
5006{
5007 struct ipc_security_struct *isec = perm->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008 perm->security = NULL;
5009 kfree(isec);
5010}
5011
5012static int msg_msg_alloc_security(struct msg_msg *msg)
5013{
5014 struct msg_security_struct *msec;
5015
James Morris89d155e2005-10-30 14:59:21 -08005016 msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017 if (!msec)
5018 return -ENOMEM;
5019
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020 msec->sid = SECINITSID_UNLABELED;
5021 msg->security = msec;
5022
5023 return 0;
5024}
5025
5026static void msg_msg_free_security(struct msg_msg *msg)
5027{
5028 struct msg_security_struct *msec = msg->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029
5030 msg->security = NULL;
5031 kfree(msec);
5032}
5033
5034static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
Stephen Smalley6af963f2005-05-01 08:58:39 -07005035 u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005038 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005039 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005040 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042 isec = ipc_perms->security;
5043
Thomas Liu2bf49692009-07-14 12:14:09 -04005044 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005045 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 ad.u.ipc_id = ipc_perms->key;
5047
David Howells275bb412008-11-14 10:39:19 +11005048 return avc_has_perm(sid, isec->sid, isec->sclass, perms, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049}
5050
5051static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
5052{
5053 return msg_msg_alloc_security(msg);
5054}
5055
5056static void selinux_msg_msg_free_security(struct msg_msg *msg)
5057{
5058 msg_msg_free_security(msg);
5059}
5060
5061/* message queue security operations */
5062static int selinux_msg_queue_alloc_security(struct msg_queue *msq)
5063{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005065 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005066 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005067 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068 int rc;
5069
5070 rc = ipc_alloc_security(current, &msq->q_perm, SECCLASS_MSGQ);
5071 if (rc)
5072 return rc;
5073
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 isec = msq->q_perm.security;
5075
Thomas Liu2bf49692009-07-14 12:14:09 -04005076 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005077 ad.selinux_audit_data = &sad;
Eric Paris828dfe12008-04-17 13:17:49 -04005078 ad.u.ipc_id = msq->q_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079
David Howells275bb412008-11-14 10:39:19 +11005080 rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 MSGQ__CREATE, &ad);
5082 if (rc) {
5083 ipc_free_security(&msq->q_perm);
5084 return rc;
5085 }
5086 return 0;
5087}
5088
5089static void selinux_msg_queue_free_security(struct msg_queue *msq)
5090{
5091 ipc_free_security(&msq->q_perm);
5092}
5093
5094static int selinux_msg_queue_associate(struct msg_queue *msq, int msqflg)
5095{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005097 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005098 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005099 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101 isec = msq->q_perm.security;
5102
Thomas Liu2bf49692009-07-14 12:14:09 -04005103 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005104 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105 ad.u.ipc_id = msq->q_perm.key;
5106
David Howells275bb412008-11-14 10:39:19 +11005107 return avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108 MSGQ__ASSOCIATE, &ad);
5109}
5110
5111static int selinux_msg_queue_msgctl(struct msg_queue *msq, int cmd)
5112{
5113 int err;
5114 int perms;
5115
Eric Paris828dfe12008-04-17 13:17:49 -04005116 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117 case IPC_INFO:
5118 case MSG_INFO:
5119 /* No specific object, just general system-wide information. */
5120 return task_has_system(current, SYSTEM__IPC_INFO);
5121 case IPC_STAT:
5122 case MSG_STAT:
5123 perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
5124 break;
5125 case IPC_SET:
5126 perms = MSGQ__SETATTR;
5127 break;
5128 case IPC_RMID:
5129 perms = MSGQ__DESTROY;
5130 break;
5131 default:
5132 return 0;
5133 }
5134
Stephen Smalley6af963f2005-05-01 08:58:39 -07005135 err = ipc_has_perm(&msq->q_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136 return err;
5137}
5138
5139static int selinux_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg, int msqflg)
5140{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 struct ipc_security_struct *isec;
5142 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005143 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005144 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005145 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146 int rc;
5147
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148 isec = msq->q_perm.security;
5149 msec = msg->security;
5150
5151 /*
5152 * First time through, need to assign label to the message
5153 */
5154 if (msec->sid == SECINITSID_UNLABELED) {
5155 /*
5156 * Compute new sid based on current process and
5157 * message queue this message will be stored in
5158 */
David Howells275bb412008-11-14 10:39:19 +11005159 rc = security_transition_sid(sid, isec->sid, SECCLASS_MSG,
Eric Paris652bb9b2011-02-01 11:05:40 -05005160 NULL, &msec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161 if (rc)
5162 return rc;
5163 }
5164
Thomas Liu2bf49692009-07-14 12:14:09 -04005165 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005166 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167 ad.u.ipc_id = msq->q_perm.key;
5168
5169 /* Can this process write to the queue? */
David Howells275bb412008-11-14 10:39:19 +11005170 rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171 MSGQ__WRITE, &ad);
5172 if (!rc)
5173 /* Can this process send the message */
David Howells275bb412008-11-14 10:39:19 +11005174 rc = avc_has_perm(sid, msec->sid, SECCLASS_MSG,
5175 MSG__SEND, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176 if (!rc)
5177 /* Can the message be put in the queue? */
David Howells275bb412008-11-14 10:39:19 +11005178 rc = avc_has_perm(msec->sid, isec->sid, SECCLASS_MSGQ,
5179 MSGQ__ENQUEUE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180
5181 return rc;
5182}
5183
5184static int selinux_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
5185 struct task_struct *target,
5186 long type, int mode)
5187{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188 struct ipc_security_struct *isec;
5189 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005190 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005191 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005192 u32 sid = task_sid(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 int rc;
5194
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195 isec = msq->q_perm.security;
5196 msec = msg->security;
5197
Thomas Liu2bf49692009-07-14 12:14:09 -04005198 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005199 ad.selinux_audit_data = &sad;
Eric Paris828dfe12008-04-17 13:17:49 -04005200 ad.u.ipc_id = msq->q_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201
David Howells275bb412008-11-14 10:39:19 +11005202 rc = avc_has_perm(sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203 SECCLASS_MSGQ, MSGQ__READ, &ad);
5204 if (!rc)
David Howells275bb412008-11-14 10:39:19 +11005205 rc = avc_has_perm(sid, msec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206 SECCLASS_MSG, MSG__RECEIVE, &ad);
5207 return rc;
5208}
5209
5210/* Shared Memory security operations */
5211static int selinux_shm_alloc_security(struct shmid_kernel *shp)
5212{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005214 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005215 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005216 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217 int rc;
5218
5219 rc = ipc_alloc_security(current, &shp->shm_perm, SECCLASS_SHM);
5220 if (rc)
5221 return rc;
5222
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223 isec = shp->shm_perm.security;
5224
Thomas Liu2bf49692009-07-14 12:14:09 -04005225 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005226 ad.selinux_audit_data = &sad;
Eric Paris828dfe12008-04-17 13:17:49 -04005227 ad.u.ipc_id = shp->shm_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228
David Howells275bb412008-11-14 10:39:19 +11005229 rc = avc_has_perm(sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 SHM__CREATE, &ad);
5231 if (rc) {
5232 ipc_free_security(&shp->shm_perm);
5233 return rc;
5234 }
5235 return 0;
5236}
5237
5238static void selinux_shm_free_security(struct shmid_kernel *shp)
5239{
5240 ipc_free_security(&shp->shm_perm);
5241}
5242
5243static int selinux_shm_associate(struct shmid_kernel *shp, int shmflg)
5244{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005246 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005247 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005248 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250 isec = shp->shm_perm.security;
5251
Thomas Liu2bf49692009-07-14 12:14:09 -04005252 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005253 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254 ad.u.ipc_id = shp->shm_perm.key;
5255
David Howells275bb412008-11-14 10:39:19 +11005256 return avc_has_perm(sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257 SHM__ASSOCIATE, &ad);
5258}
5259
5260/* Note, at this point, shp is locked down */
5261static int selinux_shm_shmctl(struct shmid_kernel *shp, int cmd)
5262{
5263 int perms;
5264 int err;
5265
Eric Paris828dfe12008-04-17 13:17:49 -04005266 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267 case IPC_INFO:
5268 case SHM_INFO:
5269 /* No specific object, just general system-wide information. */
5270 return task_has_system(current, SYSTEM__IPC_INFO);
5271 case IPC_STAT:
5272 case SHM_STAT:
5273 perms = SHM__GETATTR | SHM__ASSOCIATE;
5274 break;
5275 case IPC_SET:
5276 perms = SHM__SETATTR;
5277 break;
5278 case SHM_LOCK:
5279 case SHM_UNLOCK:
5280 perms = SHM__LOCK;
5281 break;
5282 case IPC_RMID:
5283 perms = SHM__DESTROY;
5284 break;
5285 default:
5286 return 0;
5287 }
5288
Stephen Smalley6af963f2005-05-01 08:58:39 -07005289 err = ipc_has_perm(&shp->shm_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290 return err;
5291}
5292
5293static int selinux_shm_shmat(struct shmid_kernel *shp,
5294 char __user *shmaddr, int shmflg)
5295{
5296 u32 perms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297
5298 if (shmflg & SHM_RDONLY)
5299 perms = SHM__READ;
5300 else
5301 perms = SHM__READ | SHM__WRITE;
5302
Stephen Smalley6af963f2005-05-01 08:58:39 -07005303 return ipc_has_perm(&shp->shm_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304}
5305
5306/* Semaphore security operations */
5307static int selinux_sem_alloc_security(struct sem_array *sma)
5308{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005310 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005311 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005312 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313 int rc;
5314
5315 rc = ipc_alloc_security(current, &sma->sem_perm, SECCLASS_SEM);
5316 if (rc)
5317 return rc;
5318
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319 isec = sma->sem_perm.security;
5320
Thomas Liu2bf49692009-07-14 12:14:09 -04005321 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005322 ad.selinux_audit_data = &sad;
Eric Paris828dfe12008-04-17 13:17:49 -04005323 ad.u.ipc_id = sma->sem_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324
David Howells275bb412008-11-14 10:39:19 +11005325 rc = avc_has_perm(sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326 SEM__CREATE, &ad);
5327 if (rc) {
5328 ipc_free_security(&sma->sem_perm);
5329 return rc;
5330 }
5331 return 0;
5332}
5333
5334static void selinux_sem_free_security(struct sem_array *sma)
5335{
5336 ipc_free_security(&sma->sem_perm);
5337}
5338
5339static int selinux_sem_associate(struct sem_array *sma, int semflg)
5340{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005342 struct common_audit_data ad;
Eric Paris3b3b0e42012-04-03 09:37:02 -07005343 struct selinux_audit_data sad = {0,};
David Howells275bb412008-11-14 10:39:19 +11005344 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 isec = sma->sem_perm.security;
5347
Thomas Liu2bf49692009-07-14 12:14:09 -04005348 COMMON_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris3b3b0e42012-04-03 09:37:02 -07005349 ad.selinux_audit_data = &sad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350 ad.u.ipc_id = sma->sem_perm.key;
5351
David Howells275bb412008-11-14 10:39:19 +11005352 return avc_has_perm(sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353 SEM__ASSOCIATE, &ad);
5354}
5355
5356/* Note, at this point, sma is locked down */
5357static int selinux_sem_semctl(struct sem_array *sma, int cmd)
5358{
5359 int err;
5360 u32 perms;
5361
Eric Paris828dfe12008-04-17 13:17:49 -04005362 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 case IPC_INFO:
5364 case SEM_INFO:
5365 /* No specific object, just general system-wide information. */
5366 return task_has_system(current, SYSTEM__IPC_INFO);
5367 case GETPID:
5368 case GETNCNT:
5369 case GETZCNT:
5370 perms = SEM__GETATTR;
5371 break;
5372 case GETVAL:
5373 case GETALL:
5374 perms = SEM__READ;
5375 break;
5376 case SETVAL:
5377 case SETALL:
5378 perms = SEM__WRITE;
5379 break;
5380 case IPC_RMID:
5381 perms = SEM__DESTROY;
5382 break;
5383 case IPC_SET:
5384 perms = SEM__SETATTR;
5385 break;
5386 case IPC_STAT:
5387 case SEM_STAT:
5388 perms = SEM__GETATTR | SEM__ASSOCIATE;
5389 break;
5390 default:
5391 return 0;
5392 }
5393
Stephen Smalley6af963f2005-05-01 08:58:39 -07005394 err = ipc_has_perm(&sma->sem_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395 return err;
5396}
5397
5398static int selinux_sem_semop(struct sem_array *sma,
5399 struct sembuf *sops, unsigned nsops, int alter)
5400{
5401 u32 perms;
5402
5403 if (alter)
5404 perms = SEM__READ | SEM__WRITE;
5405 else
5406 perms = SEM__READ;
5407
Stephen Smalley6af963f2005-05-01 08:58:39 -07005408 return ipc_has_perm(&sma->sem_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409}
5410
5411static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
5412{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 u32 av = 0;
5414
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415 av = 0;
5416 if (flag & S_IRUGO)
5417 av |= IPC__UNIX_READ;
5418 if (flag & S_IWUGO)
5419 av |= IPC__UNIX_WRITE;
5420
5421 if (av == 0)
5422 return 0;
5423
Stephen Smalley6af963f2005-05-01 08:58:39 -07005424 return ipc_has_perm(ipcp, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425}
5426
Ahmed S. Darwish713a04a2008-03-01 21:52:30 +02005427static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
5428{
5429 struct ipc_security_struct *isec = ipcp->security;
5430 *secid = isec->sid;
5431}
5432
Eric Paris828dfe12008-04-17 13:17:49 -04005433static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434{
5435 if (inode)
5436 inode_doinit_with_dentry(inode, dentry);
5437}
5438
5439static int selinux_getprocattr(struct task_struct *p,
Al Viro04ff9702007-03-12 16:17:58 +00005440 char *name, char **value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441{
David Howells275bb412008-11-14 10:39:19 +11005442 const struct task_security_struct *__tsec;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00005443 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444 int error;
Al Viro04ff9702007-03-12 16:17:58 +00005445 unsigned len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446
5447 if (current != p) {
David Howells3b11a1d2008-11-14 10:39:26 +11005448 error = current_has_perm(p, PROCESS__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 if (error)
5450 return error;
5451 }
5452
David Howells275bb412008-11-14 10:39:19 +11005453 rcu_read_lock();
5454 __tsec = __task_cred(p)->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455
5456 if (!strcmp(name, "current"))
David Howells275bb412008-11-14 10:39:19 +11005457 sid = __tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458 else if (!strcmp(name, "prev"))
David Howells275bb412008-11-14 10:39:19 +11005459 sid = __tsec->osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460 else if (!strcmp(name, "exec"))
David Howells275bb412008-11-14 10:39:19 +11005461 sid = __tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462 else if (!strcmp(name, "fscreate"))
David Howells275bb412008-11-14 10:39:19 +11005463 sid = __tsec->create_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07005464 else if (!strcmp(name, "keycreate"))
David Howells275bb412008-11-14 10:39:19 +11005465 sid = __tsec->keycreate_sid;
Eric Paris42c3e032006-06-26 00:26:03 -07005466 else if (!strcmp(name, "sockcreate"))
David Howells275bb412008-11-14 10:39:19 +11005467 sid = __tsec->sockcreate_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468 else
David Howells275bb412008-11-14 10:39:19 +11005469 goto invalid;
5470 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471
5472 if (!sid)
5473 return 0;
5474
Al Viro04ff9702007-03-12 16:17:58 +00005475 error = security_sid_to_context(sid, value, &len);
5476 if (error)
5477 return error;
5478 return len;
David Howells275bb412008-11-14 10:39:19 +11005479
5480invalid:
5481 rcu_read_unlock();
5482 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483}
5484
5485static int selinux_setprocattr(struct task_struct *p,
5486 char *name, void *value, size_t size)
5487{
5488 struct task_security_struct *tsec;
Roland McGrath03563572008-03-26 15:46:39 -07005489 struct task_struct *tracer;
David Howellsd84f4f92008-11-14 10:39:23 +11005490 struct cred *new;
5491 u32 sid = 0, ptsid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492 int error;
5493 char *str = value;
5494
5495 if (current != p) {
5496 /* SELinux only allows a process to change its own
5497 security attributes. */
5498 return -EACCES;
5499 }
5500
5501 /*
5502 * Basic control over ability to set these attributes at all.
5503 * current == p, but we'll pass them separately in case the
5504 * above restriction is ever removed.
5505 */
5506 if (!strcmp(name, "exec"))
David Howells3b11a1d2008-11-14 10:39:26 +11005507 error = current_has_perm(p, PROCESS__SETEXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508 else if (!strcmp(name, "fscreate"))
David Howells3b11a1d2008-11-14 10:39:26 +11005509 error = current_has_perm(p, PROCESS__SETFSCREATE);
Michael LeMay4eb582c2006-06-26 00:24:57 -07005510 else if (!strcmp(name, "keycreate"))
David Howells3b11a1d2008-11-14 10:39:26 +11005511 error = current_has_perm(p, PROCESS__SETKEYCREATE);
Eric Paris42c3e032006-06-26 00:26:03 -07005512 else if (!strcmp(name, "sockcreate"))
David Howells3b11a1d2008-11-14 10:39:26 +11005513 error = current_has_perm(p, PROCESS__SETSOCKCREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 else if (!strcmp(name, "current"))
David Howells3b11a1d2008-11-14 10:39:26 +11005515 error = current_has_perm(p, PROCESS__SETCURRENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516 else
5517 error = -EINVAL;
5518 if (error)
5519 return error;
5520
5521 /* Obtain a SID for the context, if one was specified. */
Stephen Smalleye44b57e2017-01-31 11:54:04 -05005522 if (size && str[0] && str[0] != '\n') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523 if (str[size-1] == '\n') {
5524 str[size-1] = 0;
5525 size--;
5526 }
5527 error = security_context_to_sid(value, size, &sid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04005528 if (error == -EINVAL && !strcmp(name, "fscreate")) {
5529 if (!capable(CAP_MAC_ADMIN))
5530 return error;
5531 error = security_context_to_sid_force(value, size,
5532 &sid);
5533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534 if (error)
5535 return error;
5536 }
5537
David Howellsd84f4f92008-11-14 10:39:23 +11005538 new = prepare_creds();
5539 if (!new)
5540 return -ENOMEM;
5541
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542 /* Permission checking based on the specified context is
5543 performed during the actual operation (execve,
5544 open/mkdir/...), when we know the full context of the
David Howellsd84f4f92008-11-14 10:39:23 +11005545 operation. See selinux_bprm_set_creds for the execve
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546 checks and may_create for the file creation checks. The
5547 operation will then fail if the context is not permitted. */
David Howellsd84f4f92008-11-14 10:39:23 +11005548 tsec = new->security;
5549 if (!strcmp(name, "exec")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550 tsec->exec_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005551 } else if (!strcmp(name, "fscreate")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552 tsec->create_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005553 } else if (!strcmp(name, "keycreate")) {
Michael LeMay4eb582c2006-06-26 00:24:57 -07005554 error = may_create_key(sid, p);
5555 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11005556 goto abort_change;
Michael LeMay4eb582c2006-06-26 00:24:57 -07005557 tsec->keycreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005558 } else if (!strcmp(name, "sockcreate")) {
Eric Paris42c3e032006-06-26 00:26:03 -07005559 tsec->sockcreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005560 } else if (!strcmp(name, "current")) {
5561 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562 if (sid == 0)
David Howellsd84f4f92008-11-14 10:39:23 +11005563 goto abort_change;
KaiGai Koheid9250de2008-08-28 16:35:57 +09005564
David Howellsd84f4f92008-11-14 10:39:23 +11005565 /* Only allow single threaded processes to change context */
5566 error = -EPERM;
Oleg Nesterov5bb459b2009-07-10 03:48:23 +02005567 if (!current_is_single_threaded()) {
David Howellsd84f4f92008-11-14 10:39:23 +11005568 error = security_bounded_transition(tsec->sid, sid);
5569 if (error)
5570 goto abort_change;
Eric Paris828dfe12008-04-17 13:17:49 -04005571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572
5573 /* Check permissions for the transition. */
5574 error = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS,
Eric Paris828dfe12008-04-17 13:17:49 -04005575 PROCESS__DYNTRANSITION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11005577 goto abort_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578
5579 /* Check for ptracing, and update the task SID if ok.
5580 Otherwise, leave SID unchanged and fail. */
David Howellsd84f4f92008-11-14 10:39:23 +11005581 ptsid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582 task_lock(p);
Tejun Heo06d98472011-06-17 16:50:40 +02005583 tracer = ptrace_parent(p);
David Howellsd84f4f92008-11-14 10:39:23 +11005584 if (tracer)
5585 ptsid = task_sid(tracer);
5586 task_unlock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587
David Howellsd84f4f92008-11-14 10:39:23 +11005588 if (tracer) {
5589 error = avc_has_perm(ptsid, sid, SECCLASS_PROCESS,
5590 PROCESS__PTRACE, NULL);
5591 if (error)
5592 goto abort_change;
5593 }
5594
5595 tsec->sid = sid;
5596 } else {
5597 error = -EINVAL;
5598 goto abort_change;
5599 }
5600
5601 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602 return size;
David Howellsd84f4f92008-11-14 10:39:23 +11005603
5604abort_change:
5605 abort_creds(new);
5606 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607}
5608
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005609static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
5610{
5611 return security_sid_to_context(secid, secdata, seclen);
5612}
5613
David Howells7bf570d2008-04-29 20:52:51 +01005614static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
David Howells63cb3442008-01-15 23:47:35 +00005615{
5616 return security_context_to_sid(secdata, seclen, secid);
5617}
5618
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005619static void selinux_release_secctx(char *secdata, u32 seclen)
5620{
Paul Moore088999e2007-08-01 11:12:58 -04005621 kfree(secdata);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005622}
5623
David P. Quigley1ee65e32009-09-03 14:25:57 -04005624/*
5625 * called with inode->i_mutex locked
5626 */
5627static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
5628{
5629 return selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, ctx, ctxlen, 0);
5630}
5631
5632/*
5633 * called with inode->i_mutex locked
5634 */
5635static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
5636{
5637 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0);
5638}
5639
5640static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
5641{
5642 int len = 0;
5643 len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX,
5644 ctx, true);
5645 if (len < 0)
5646 return len;
5647 *ctxlen = len;
5648 return 0;
5649}
Michael LeMayd7200242006-06-22 14:47:17 -07005650#ifdef CONFIG_KEYS
5651
David Howellsd84f4f92008-11-14 10:39:23 +11005652static int selinux_key_alloc(struct key *k, const struct cred *cred,
David Howells7e047ef2006-06-26 00:24:50 -07005653 unsigned long flags)
Michael LeMayd7200242006-06-22 14:47:17 -07005654{
David Howellsd84f4f92008-11-14 10:39:23 +11005655 const struct task_security_struct *tsec;
Michael LeMayd7200242006-06-22 14:47:17 -07005656 struct key_security_struct *ksec;
5657
5658 ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
5659 if (!ksec)
5660 return -ENOMEM;
5661
David Howellsd84f4f92008-11-14 10:39:23 +11005662 tsec = cred->security;
5663 if (tsec->keycreate_sid)
5664 ksec->sid = tsec->keycreate_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07005665 else
David Howellsd84f4f92008-11-14 10:39:23 +11005666 ksec->sid = tsec->sid;
Michael LeMayd7200242006-06-22 14:47:17 -07005667
David Howells275bb412008-11-14 10:39:19 +11005668 k->security = ksec;
Michael LeMayd7200242006-06-22 14:47:17 -07005669 return 0;
5670}
5671
5672static void selinux_key_free(struct key *k)
5673{
5674 struct key_security_struct *ksec = k->security;
5675
5676 k->security = NULL;
5677 kfree(ksec);
5678}
5679
5680static int selinux_key_permission(key_ref_t key_ref,
David Howellsd84f4f92008-11-14 10:39:23 +11005681 const struct cred *cred,
5682 key_perm_t perm)
Michael LeMayd7200242006-06-22 14:47:17 -07005683{
5684 struct key *key;
Michael LeMayd7200242006-06-22 14:47:17 -07005685 struct key_security_struct *ksec;
David Howells275bb412008-11-14 10:39:19 +11005686 u32 sid;
Michael LeMayd7200242006-06-22 14:47:17 -07005687
5688 /* if no specific permissions are requested, we skip the
5689 permission check. No serious, additional covert channels
5690 appear to be created. */
5691 if (perm == 0)
5692 return 0;
5693
David Howellsd84f4f92008-11-14 10:39:23 +11005694 sid = cred_sid(cred);
David Howells275bb412008-11-14 10:39:19 +11005695
5696 key = key_ref_to_ptr(key_ref);
5697 ksec = key->security;
5698
5699 return avc_has_perm(sid, ksec->sid, SECCLASS_KEY, perm, NULL);
Michael LeMayd7200242006-06-22 14:47:17 -07005700}
5701
David Howells70a5bb72008-04-29 01:01:26 -07005702static int selinux_key_getsecurity(struct key *key, char **_buffer)
5703{
5704 struct key_security_struct *ksec = key->security;
5705 char *context = NULL;
5706 unsigned len;
5707 int rc;
5708
5709 rc = security_sid_to_context(ksec->sid, &context, &len);
5710 if (!rc)
5711 rc = len;
5712 *_buffer = context;
5713 return rc;
5714}
5715
Michael LeMayd7200242006-06-22 14:47:17 -07005716#endif
5717
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718static struct security_operations selinux_ops = {
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02005719 .name = "selinux",
5720
Stephen Smalley48a23702012-11-05 08:15:34 -05005721 .binder_set_context_mgr = selinux_binder_set_context_mgr,
5722 .binder_transaction = selinux_binder_transaction,
5723 .binder_transfer_binder = selinux_binder_transfer_binder,
5724 .binder_transfer_file = selinux_binder_transfer_file,
5725
Ingo Molnar9e488582009-05-07 19:26:19 +10005726 .ptrace_access_check = selinux_ptrace_access_check,
David Howells5cd9c582008-08-14 11:37:28 +01005727 .ptrace_traceme = selinux_ptrace_traceme,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728 .capget = selinux_capget,
David Howellsd84f4f92008-11-14 10:39:23 +11005729 .capset = selinux_capset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730 .capable = selinux_capable,
5731 .quotactl = selinux_quotactl,
5732 .quota_on = selinux_quota_on,
5733 .syslog = selinux_syslog,
5734 .vm_enough_memory = selinux_vm_enough_memory,
5735
5736 .netlink_send = selinux_netlink_send,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737
David Howellsa6f76f22008-11-14 10:39:24 +11005738 .bprm_set_creds = selinux_bprm_set_creds,
David Howellsa6f76f22008-11-14 10:39:24 +11005739 .bprm_committing_creds = selinux_bprm_committing_creds,
5740 .bprm_committed_creds = selinux_bprm_committed_creds,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741 .bprm_secureexec = selinux_bprm_secureexec,
5742
5743 .sb_alloc_security = selinux_sb_alloc_security,
5744 .sb_free_security = selinux_sb_free_security,
5745 .sb_copy_data = selinux_sb_copy_data,
Eric Paris026eb162011-03-03 16:09:14 -05005746 .sb_remount = selinux_sb_remount,
Eric Paris828dfe12008-04-17 13:17:49 -04005747 .sb_kern_mount = selinux_sb_kern_mount,
Eric Paris2069f452008-07-04 09:47:13 +10005748 .sb_show_options = selinux_sb_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749 .sb_statfs = selinux_sb_statfs,
5750 .sb_mount = selinux_mount,
5751 .sb_umount = selinux_umount,
Eric Parisc9180a52007-11-30 13:00:35 -05005752 .sb_set_mnt_opts = selinux_set_mnt_opts,
Eric Paris828dfe12008-04-17 13:17:49 -04005753 .sb_clone_mnt_opts = selinux_sb_clone_mnt_opts,
Eric Parise0007522008-03-05 10:31:54 -05005754 .sb_parse_opts_str = selinux_parse_opts_str,
5755
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756
5757 .inode_alloc_security = selinux_inode_alloc_security,
5758 .inode_free_security = selinux_inode_free_security,
Stephen Smalley5e41ff92005-09-09 13:01:35 -07005759 .inode_init_security = selinux_inode_init_security,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005760 .inode_create = selinux_inode_create,
Amir Samuelov6a22e462014-05-26 11:44:06 +03005761 .inode_post_create = selinux_inode_post_create,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005762 .inode_link = selinux_inode_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763 .inode_unlink = selinux_inode_unlink,
5764 .inode_symlink = selinux_inode_symlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765 .inode_mkdir = selinux_inode_mkdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005766 .inode_rmdir = selinux_inode_rmdir,
5767 .inode_mknod = selinux_inode_mknod,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768 .inode_rename = selinux_inode_rename,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769 .inode_readlink = selinux_inode_readlink,
5770 .inode_follow_link = selinux_inode_follow_link,
5771 .inode_permission = selinux_inode_permission,
5772 .inode_setattr = selinux_inode_setattr,
5773 .inode_getattr = selinux_inode_getattr,
5774 .inode_setxattr = selinux_inode_setxattr,
5775 .inode_post_setxattr = selinux_inode_post_setxattr,
5776 .inode_getxattr = selinux_inode_getxattr,
5777 .inode_listxattr = selinux_inode_listxattr,
5778 .inode_removexattr = selinux_inode_removexattr,
Eric Paris828dfe12008-04-17 13:17:49 -04005779 .inode_getsecurity = selinux_inode_getsecurity,
5780 .inode_setsecurity = selinux_inode_setsecurity,
5781 .inode_listsecurity = selinux_inode_listsecurity,
Eric Parisf5269712008-05-14 11:27:45 -04005782 .inode_getsecid = selinux_inode_getsecid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005783
5784 .file_permission = selinux_file_permission,
5785 .file_alloc_security = selinux_file_alloc_security,
5786 .file_free_security = selinux_file_free_security,
5787 .file_ioctl = selinux_file_ioctl,
5788 .file_mmap = selinux_file_mmap,
5789 .file_mprotect = selinux_file_mprotect,
5790 .file_lock = selinux_file_lock,
5791 .file_fcntl = selinux_file_fcntl,
5792 .file_set_fowner = selinux_file_set_fowner,
5793 .file_send_sigiotask = selinux_file_send_sigiotask,
5794 .file_receive = selinux_file_receive,
5795
Eric Paris828dfe12008-04-17 13:17:49 -04005796 .dentry_open = selinux_dentry_open,
Amir Samuelov6a22e462014-05-26 11:44:06 +03005797 .file_close = selinux_file_close,
5798 .allow_merge_bio = selinux_allow_merge_bio,
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09005799
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800 .task_create = selinux_task_create,
David Howellsee18d642009-09-02 09:14:21 +01005801 .cred_alloc_blank = selinux_cred_alloc_blank,
David Howellsf1752ee2008-11-14 10:39:17 +11005802 .cred_free = selinux_cred_free,
David Howellsd84f4f92008-11-14 10:39:23 +11005803 .cred_prepare = selinux_cred_prepare,
David Howellsee18d642009-09-02 09:14:21 +01005804 .cred_transfer = selinux_cred_transfer,
David Howells3a3b7ce2008-11-14 10:39:28 +11005805 .kernel_act_as = selinux_kernel_act_as,
5806 .kernel_create_files_as = selinux_kernel_create_files_as,
Eric Paris25354c42009-08-13 09:45:03 -04005807 .kernel_module_request = selinux_kernel_module_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808 .task_setpgid = selinux_task_setpgid,
5809 .task_getpgid = selinux_task_getpgid,
Eric Paris828dfe12008-04-17 13:17:49 -04005810 .task_getsid = selinux_task_getsid,
David Quigleyf9008e42006-06-30 01:55:46 -07005811 .task_getsecid = selinux_task_getsecid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005812 .task_setnice = selinux_task_setnice,
James Morris03e68062006-06-23 02:03:58 -07005813 .task_setioprio = selinux_task_setioprio,
David Quigleya1836a42006-06-30 01:55:49 -07005814 .task_getioprio = selinux_task_getioprio,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005815 .task_setrlimit = selinux_task_setrlimit,
5816 .task_setscheduler = selinux_task_setscheduler,
5817 .task_getscheduler = selinux_task_getscheduler,
David Quigley35601542006-06-23 02:04:01 -07005818 .task_movememory = selinux_task_movememory,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819 .task_kill = selinux_task_kill,
5820 .task_wait = selinux_task_wait,
Eric Paris828dfe12008-04-17 13:17:49 -04005821 .task_to_inode = selinux_task_to_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822
5823 .ipc_permission = selinux_ipc_permission,
Eric Parisf5269712008-05-14 11:27:45 -04005824 .ipc_getsecid = selinux_ipc_getsecid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825
5826 .msg_msg_alloc_security = selinux_msg_msg_alloc_security,
5827 .msg_msg_free_security = selinux_msg_msg_free_security,
5828
5829 .msg_queue_alloc_security = selinux_msg_queue_alloc_security,
5830 .msg_queue_free_security = selinux_msg_queue_free_security,
5831 .msg_queue_associate = selinux_msg_queue_associate,
5832 .msg_queue_msgctl = selinux_msg_queue_msgctl,
5833 .msg_queue_msgsnd = selinux_msg_queue_msgsnd,
5834 .msg_queue_msgrcv = selinux_msg_queue_msgrcv,
5835
5836 .shm_alloc_security = selinux_shm_alloc_security,
5837 .shm_free_security = selinux_shm_free_security,
5838 .shm_associate = selinux_shm_associate,
5839 .shm_shmctl = selinux_shm_shmctl,
5840 .shm_shmat = selinux_shm_shmat,
5841
Eric Paris828dfe12008-04-17 13:17:49 -04005842 .sem_alloc_security = selinux_sem_alloc_security,
5843 .sem_free_security = selinux_sem_free_security,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005844 .sem_associate = selinux_sem_associate,
5845 .sem_semctl = selinux_sem_semctl,
5846 .sem_semop = selinux_sem_semop,
5847
Eric Paris828dfe12008-04-17 13:17:49 -04005848 .d_instantiate = selinux_d_instantiate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849
Eric Paris828dfe12008-04-17 13:17:49 -04005850 .getprocattr = selinux_getprocattr,
5851 .setprocattr = selinux_setprocattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005852
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005853 .secid_to_secctx = selinux_secid_to_secctx,
David Howells63cb3442008-01-15 23:47:35 +00005854 .secctx_to_secid = selinux_secctx_to_secid,
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005855 .release_secctx = selinux_release_secctx,
David P. Quigley1ee65e32009-09-03 14:25:57 -04005856 .inode_notifysecctx = selinux_inode_notifysecctx,
5857 .inode_setsecctx = selinux_inode_setsecctx,
5858 .inode_getsecctx = selinux_inode_getsecctx,
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005859
Eric Paris828dfe12008-04-17 13:17:49 -04005860 .unix_stream_connect = selinux_socket_unix_stream_connect,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005861 .unix_may_send = selinux_socket_unix_may_send,
5862
5863 .socket_create = selinux_socket_create,
5864 .socket_post_create = selinux_socket_post_create,
5865 .socket_bind = selinux_socket_bind,
5866 .socket_connect = selinux_socket_connect,
5867 .socket_listen = selinux_socket_listen,
5868 .socket_accept = selinux_socket_accept,
5869 .socket_sendmsg = selinux_socket_sendmsg,
5870 .socket_recvmsg = selinux_socket_recvmsg,
5871 .socket_getsockname = selinux_socket_getsockname,
5872 .socket_getpeername = selinux_socket_getpeername,
5873 .socket_getsockopt = selinux_socket_getsockopt,
5874 .socket_setsockopt = selinux_socket_setsockopt,
5875 .socket_shutdown = selinux_socket_shutdown,
5876 .socket_sock_rcv_skb = selinux_socket_sock_rcv_skb,
Catherine Zhang2c7946a2006-03-20 22:41:23 -08005877 .socket_getpeersec_stream = selinux_socket_getpeersec_stream,
5878 .socket_getpeersec_dgram = selinux_socket_getpeersec_dgram,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005879 .sk_alloc_security = selinux_sk_alloc_security,
5880 .sk_free_security = selinux_sk_free_security,
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005881 .sk_clone_security = selinux_sk_clone_security,
Eric Paris828dfe12008-04-17 13:17:49 -04005882 .sk_getsecid = selinux_sk_getsecid,
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005883 .sock_graft = selinux_sock_graft,
5884 .inet_conn_request = selinux_inet_conn_request,
5885 .inet_csk_clone = selinux_inet_csk_clone,
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005886 .inet_conn_established = selinux_inet_conn_established,
Eric Paris2606fd12010-10-13 16:24:41 -04005887 .secmark_relabel_packet = selinux_secmark_relabel_packet,
5888 .secmark_refcount_inc = selinux_secmark_refcount_inc,
5889 .secmark_refcount_dec = selinux_secmark_refcount_dec,
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005890 .req_classify_flow = selinux_req_classify_flow,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005891 .tun_dev_create = selinux_tun_dev_create,
5892 .tun_dev_post_create = selinux_tun_dev_post_create,
5893 .tun_dev_attach = selinux_tun_dev_attach,
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005894
5895#ifdef CONFIG_SECURITY_NETWORK_XFRM
5896 .xfrm_policy_alloc_security = selinux_xfrm_policy_alloc,
5897 .xfrm_policy_clone_security = selinux_xfrm_policy_clone,
5898 .xfrm_policy_free_security = selinux_xfrm_policy_free,
Catherine Zhangc8c05a82006-06-08 23:39:49 -07005899 .xfrm_policy_delete_security = selinux_xfrm_policy_delete,
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005900 .xfrm_state_alloc_security = selinux_xfrm_state_alloc,
5901 .xfrm_state_free_security = selinux_xfrm_state_free,
Catherine Zhangc8c05a82006-06-08 23:39:49 -07005902 .xfrm_state_delete_security = selinux_xfrm_state_delete,
Eric Paris828dfe12008-04-17 13:17:49 -04005903 .xfrm_policy_lookup = selinux_xfrm_policy_lookup,
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07005904 .xfrm_state_pol_flow_match = selinux_xfrm_state_pol_flow_match,
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07005905 .xfrm_decode_session = selinux_xfrm_decode_session,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005906#endif
Michael LeMayd7200242006-06-22 14:47:17 -07005907
5908#ifdef CONFIG_KEYS
Eric Paris828dfe12008-04-17 13:17:49 -04005909 .key_alloc = selinux_key_alloc,
5910 .key_free = selinux_key_free,
5911 .key_permission = selinux_key_permission,
David Howells70a5bb72008-04-29 01:01:26 -07005912 .key_getsecurity = selinux_key_getsecurity,
Michael LeMayd7200242006-06-22 14:47:17 -07005913#endif
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02005914
5915#ifdef CONFIG_AUDIT
5916 .audit_rule_init = selinux_audit_rule_init,
5917 .audit_rule_known = selinux_audit_rule_known,
5918 .audit_rule_match = selinux_audit_rule_match,
5919 .audit_rule_free = selinux_audit_rule_free,
5920#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921};
5922
5923static __init int selinux_init(void)
5924{
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02005925 if (!security_module_enable(&selinux_ops)) {
5926 selinux_enabled = 0;
5927 return 0;
5928 }
5929
Linus Torvalds1da177e2005-04-16 15:20:36 -07005930 if (!selinux_enabled) {
5931 printk(KERN_INFO "SELinux: Disabled at boot.\n");
5932 return 0;
5933 }
5934
5935 printk(KERN_INFO "SELinux: Initializing.\n");
5936
5937 /* Set the security state for the initial task. */
David Howellsd84f4f92008-11-14 10:39:23 +11005938 cred_init_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04005940 default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
5941
James Morris7cae7e22006-03-22 00:09:22 -08005942 sel_inode_cache = kmem_cache_create("selinux_inode_security",
5943 sizeof(struct inode_security_struct),
Paul Mundt20c2df82007-07-20 10:11:58 +09005944 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005945 avc_init();
5946
Eric Paris828dfe12008-04-17 13:17:49 -04005947 if (register_security(&selinux_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948 panic("SELinux: Unable to register with kernel.\n");
5949
Eric Paris828dfe12008-04-17 13:17:49 -04005950 if (selinux_enforcing)
Eric Parisfadcdb42007-02-22 18:11:31 -05005951 printk(KERN_DEBUG "SELinux: Starting in enforcing mode\n");
Eric Paris828dfe12008-04-17 13:17:49 -04005952 else
Eric Parisfadcdb42007-02-22 18:11:31 -05005953 printk(KERN_DEBUG "SELinux: Starting in permissive mode\n");
Michael LeMayd7200242006-06-22 14:47:17 -07005954
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955 return 0;
5956}
5957
Al Viroe8c26252010-03-23 06:36:54 -04005958static void delayed_superblock_init(struct super_block *sb, void *unused)
5959{
5960 superblock_doinit(sb, NULL);
5961}
5962
Linus Torvalds1da177e2005-04-16 15:20:36 -07005963void selinux_complete_init(void)
5964{
Eric Parisfadcdb42007-02-22 18:11:31 -05005965 printk(KERN_DEBUG "SELinux: Completing initialization.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966
5967 /* Set up any superblocks initialized prior to the policy load. */
Eric Parisfadcdb42007-02-22 18:11:31 -05005968 printk(KERN_DEBUG "SELinux: Setting up existing superblocks.\n");
Al Viroe8c26252010-03-23 06:36:54 -04005969 iterate_supers(delayed_superblock_init, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005970}
5971
5972/* SELinux requires early initialization in order to label
5973 all processes and objects when they are created. */
5974security_initcall(selinux_init);
5975
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08005976#if defined(CONFIG_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977
Paul Mooreeffad8d2008-01-29 08:49:27 -05005978static struct nf_hook_ops selinux_ipv4_ops[] = {
5979 {
5980 .hook = selinux_ipv4_postroute,
5981 .owner = THIS_MODULE,
5982 .pf = PF_INET,
5983 .hooknum = NF_INET_POST_ROUTING,
5984 .priority = NF_IP_PRI_SELINUX_LAST,
5985 },
5986 {
5987 .hook = selinux_ipv4_forward,
5988 .owner = THIS_MODULE,
5989 .pf = PF_INET,
5990 .hooknum = NF_INET_FORWARD,
5991 .priority = NF_IP_PRI_SELINUX_FIRST,
Paul Moore948bf852008-10-10 10:16:32 -04005992 },
5993 {
5994 .hook = selinux_ipv4_output,
5995 .owner = THIS_MODULE,
5996 .pf = PF_INET,
5997 .hooknum = NF_INET_LOCAL_OUT,
5998 .priority = NF_IP_PRI_SELINUX_FIRST,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006000};
6001
6002#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
6003
Paul Mooreeffad8d2008-01-29 08:49:27 -05006004static struct nf_hook_ops selinux_ipv6_ops[] = {
6005 {
6006 .hook = selinux_ipv6_postroute,
6007 .owner = THIS_MODULE,
6008 .pf = PF_INET6,
6009 .hooknum = NF_INET_POST_ROUTING,
6010 .priority = NF_IP6_PRI_SELINUX_LAST,
6011 },
6012 {
6013 .hook = selinux_ipv6_forward,
6014 .owner = THIS_MODULE,
6015 .pf = PF_INET6,
6016 .hooknum = NF_INET_FORWARD,
6017 .priority = NF_IP6_PRI_SELINUX_FIRST,
6018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019};
6020
6021#endif /* IPV6 */
6022
6023static int __init selinux_nf_ip_init(void)
6024{
6025 int err = 0;
6026
6027 if (!selinux_enabled)
6028 goto out;
Eric Parisfadcdb42007-02-22 18:11:31 -05006029
6030 printk(KERN_DEBUG "SELinux: Registering netfilter hooks\n");
6031
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07006032 err = nf_register_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops));
6033 if (err)
6034 panic("SELinux: nf_register_hooks for IPv4: error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006035
6036#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07006037 err = nf_register_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops));
6038 if (err)
6039 panic("SELinux: nf_register_hooks for IPv6: error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040#endif /* IPV6 */
Trent Jaegerd28d1e02005-12-13 23:12:40 -08006041
Linus Torvalds1da177e2005-04-16 15:20:36 -07006042out:
6043 return err;
6044}
6045
6046__initcall(selinux_nf_ip_init);
6047
6048#ifdef CONFIG_SECURITY_SELINUX_DISABLE
6049static void selinux_nf_ip_exit(void)
6050{
Eric Parisfadcdb42007-02-22 18:11:31 -05006051 printk(KERN_DEBUG "SELinux: Unregistering netfilter hooks\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07006053 nf_unregister_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006054#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07006055 nf_unregister_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056#endif /* IPV6 */
6057}
6058#endif
6059
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08006060#else /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006061
6062#ifdef CONFIG_SECURITY_SELINUX_DISABLE
6063#define selinux_nf_ip_exit()
6064#endif
6065
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08006066#endif /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067
6068#ifdef CONFIG_SECURITY_SELINUX_DISABLE
Eric Paris828dfe12008-04-17 13:17:49 -04006069static int selinux_disabled;
6070
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071int selinux_disable(void)
6072{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006073 if (ss_initialized) {
6074 /* Not permitted after initial policy load. */
6075 return -EINVAL;
6076 }
6077
6078 if (selinux_disabled) {
6079 /* Only do this once. */
6080 return -EINVAL;
6081 }
6082
6083 printk(KERN_INFO "SELinux: Disabled at runtime.\n");
6084
6085 selinux_disabled = 1;
Stephen Smalley30d55282006-05-03 10:52:36 -04006086 selinux_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006087
wzt.wzt@gmail.com189b3b12010-02-23 23:15:28 +08006088 reset_security_ops();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006089
Eric Parisaf8ff042009-09-20 21:23:01 -04006090 /* Try to destroy the avc node cache */
6091 avc_disable();
6092
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093 /* Unregister netfilter hooks. */
6094 selinux_nf_ip_exit();
6095
6096 /* Unregister selinuxfs. */
6097 exit_sel_fs();
6098
6099 return 0;
6100}
6101#endif