blob: b222e966babeec0037d2dbd631e7f5d2b65eedcb [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[] */
Eric Dumazetca10b9e2013-04-08 17:58:11 +000054#include <net/sock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <net/tcp.h> /* struct or_callable used in sock_rcv_skb */
Paul Moore220deb92008-01-29 08:38:23 -050056#include <net/net_namespace.h>
Paul Moored621d352008-01-29 08:43:36 -050057#include <net/netlabel.h>
Eric Parisf5269712008-05-14 11:27:45 -040058#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <asm/ioctls.h>
Arun Sharma600634972011-07-26 16:09:06 -070060#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/bitops.h>
62#include <linux/interrupt.h>
63#include <linux/netdevice.h> /* for network interface checks */
Hong zhi guo77954982013-03-27 06:49:35 +000064#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <linux/tcp.h>
66#include <linux/udp.h>
James Morris2ee92d42006-11-13 16:09:01 -080067#include <linux/dccp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/quota.h>
69#include <linux/un.h> /* for Unix socket types */
70#include <net/af_unix.h> /* for Unix socket types */
71#include <linux/parser.h>
72#include <linux/nfs_mount.h>
73#include <net/ipv6.h>
74#include <linux/hugetlb.h>
75#include <linux/personality.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include <linux/audit.h>
Eric Paris6931dfc2005-06-30 02:58:51 -070077#include <linux/string.h>
Catherine Zhang877ce7c2006-06-29 12:27:47 -070078#include <linux/selinux.h>
Eric Paris23970742006-09-25 23:32:01 -070079#include <linux/mutex.h>
Frank Mayharf06febc2008-09-12 09:54:39 -070080#include <linux/posix-timers.h>
Kees Cook00234592010-02-03 15:36:43 -080081#include <linux/syslog.h>
Serge E. Hallyn34867402011-03-23 16:43:17 -070082#include <linux/user_namespace.h>
Paul Gortmaker44fc7ea2011-05-26 20:52:10 -040083#include <linux/export.h>
Al Viro40401532012-02-13 03:58:52 +000084#include <linux/msg.h>
85#include <linux/shm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87#include "avc.h"
88#include "objsec.h"
89#include "netif.h"
Paul Moore224dfbd2008-01-29 08:38:13 -050090#include "netnode.h"
Paul Moore3e1121722008-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
James Morris20510f22007-10-16 23:31:32 -070097extern struct security_operations *security_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Paul Moored621d352008-01-29 08:43:36 -050099/* SECMARK reference count */
James Morris56a4ca92011-08-17 11:08:43 +1000100static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0);
Paul Moored621d352008-01-29 08:43:36 -0500101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#ifdef CONFIG_SECURITY_SELINUX_DEVELOP
Eric Paris828dfe12008-04-17 13:17:49 -0400103int selinux_enforcing;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105static int __init enforcing_setup(char *str)
106{
Eric Parisf5269712008-05-14 11:27:45 -0400107 unsigned long enforcing;
108 if (!strict_strtoul(str, 0, &enforcing))
109 selinux_enforcing = enforcing ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 return 1;
111}
112__setup("enforcing=", enforcing_setup);
113#endif
114
115#ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM
116int selinux_enabled = CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE;
117
118static int __init selinux_enabled_setup(char *str)
119{
Eric Parisf5269712008-05-14 11:27:45 -0400120 unsigned long enabled;
121 if (!strict_strtoul(str, 0, &enabled))
122 selinux_enabled = enabled ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 return 1;
124}
125__setup("selinux=", selinux_enabled_setup);
Stephen Smalley30d55282006-05-03 10:52:36 -0400126#else
127int selinux_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128#endif
129
Christoph Lametere18b8902006-12-06 20:33:20 -0800130static struct kmem_cache *sel_inode_cache;
James Morris7cae7e22006-03-22 00:09:22 -0800131
Paul Moored621d352008-01-29 08:43:36 -0500132/**
133 * selinux_secmark_enabled - Check to see if SECMARK is currently enabled
134 *
135 * Description:
136 * This function checks the SECMARK reference counter to see if any SECMARK
137 * targets are currently configured, if the reference counter is greater than
138 * zero SECMARK is considered to be enabled. Returns true (1) if SECMARK is
139 * enabled, false (0) if SECMARK is disabled.
140 *
141 */
142static int selinux_secmark_enabled(void)
143{
144 return (atomic_read(&selinux_secmark_refcount) > 0);
145}
146
David Howellsd84f4f92008-11-14 10:39:23 +1100147/*
148 * initialise the security for the init task
149 */
150static void cred_init_security(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
David Howells3b11a1d2008-11-14 10:39:26 +1100152 struct cred *cred = (struct cred *) current->real_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 struct task_security_struct *tsec;
154
James Morris89d155e2005-10-30 14:59:21 -0800155 tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 if (!tsec)
David Howellsd84f4f92008-11-14 10:39:23 +1100157 panic("SELinux: Failed to initialize initial task.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
David Howellsd84f4f92008-11-14 10:39:23 +1100159 tsec->osid = tsec->sid = SECINITSID_KERNEL;
David Howellsf1752ee2008-11-14 10:39:17 +1100160 cred->security = tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
David Howells275bb412008-11-14 10:39:19 +1100163/*
David Howells88e67f32008-11-14 10:39:21 +1100164 * get the security ID of a set of credentials
165 */
166static inline u32 cred_sid(const struct cred *cred)
167{
168 const struct task_security_struct *tsec;
169
170 tsec = cred->security;
171 return tsec->sid;
172}
173
174/*
David Howells3b11a1d2008-11-14 10:39:26 +1100175 * get the objective security ID of a task
David Howells275bb412008-11-14 10:39:19 +1100176 */
177static inline u32 task_sid(const struct task_struct *task)
178{
David Howells275bb412008-11-14 10:39:19 +1100179 u32 sid;
180
181 rcu_read_lock();
David Howells88e67f32008-11-14 10:39:21 +1100182 sid = cred_sid(__task_cred(task));
David Howells275bb412008-11-14 10:39:19 +1100183 rcu_read_unlock();
184 return sid;
185}
186
187/*
David Howells3b11a1d2008-11-14 10:39:26 +1100188 * get the subjective security ID of the current task
David Howells275bb412008-11-14 10:39:19 +1100189 */
190static inline u32 current_sid(void)
191{
Paul Moore5fb49872010-04-22 14:46:19 -0400192 const struct task_security_struct *tsec = current_security();
David Howells275bb412008-11-14 10:39:19 +1100193
194 return tsec->sid;
195}
196
David Howells88e67f32008-11-14 10:39:21 +1100197/* Allocate and free functions for each kind of security blob. */
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199static int inode_alloc_security(struct inode *inode)
200{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 struct inode_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +1100202 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Josef Bacika02fe132008-04-04 09:35:05 +1100204 isec = kmem_cache_zalloc(sel_inode_cache, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 if (!isec)
206 return -ENOMEM;
207
Eric Paris23970742006-09-25 23:32:01 -0700208 mutex_init(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 INIT_LIST_HEAD(&isec->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 isec->inode = inode;
211 isec->sid = SECINITSID_UNLABELED;
212 isec->sclass = SECCLASS_FILE;
David Howells275bb412008-11-14 10:39:19 +1100213 isec->task_sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 inode->i_security = isec;
215
216 return 0;
217}
218
219static void inode_free_security(struct inode *inode)
220{
221 struct inode_security_struct *isec = inode->i_security;
222 struct superblock_security_struct *sbsec = inode->i_sb->s_security;
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 spin_lock(&sbsec->isec_lock);
225 if (!list_empty(&isec->list))
226 list_del_init(&isec->list);
227 spin_unlock(&sbsec->isec_lock);
228
229 inode->i_security = NULL;
James Morris7cae7e22006-03-22 00:09:22 -0800230 kmem_cache_free(sel_inode_cache, isec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231}
232
233static int file_alloc_security(struct file *file)
234{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 struct file_security_struct *fsec;
David Howells275bb412008-11-14 10:39:19 +1100236 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Stephen Smalley26d2a4b2006-02-01 03:05:55 -0800238 fsec = kzalloc(sizeof(struct file_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 if (!fsec)
240 return -ENOMEM;
241
David Howells275bb412008-11-14 10:39:19 +1100242 fsec->sid = sid;
243 fsec->fown_sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 file->f_security = fsec;
245
246 return 0;
247}
248
249static void file_free_security(struct file *file)
250{
251 struct file_security_struct *fsec = file->f_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 file->f_security = NULL;
253 kfree(fsec);
254}
255
256static int superblock_alloc_security(struct super_block *sb)
257{
258 struct superblock_security_struct *sbsec;
259
James Morris89d155e2005-10-30 14:59:21 -0800260 sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if (!sbsec)
262 return -ENOMEM;
263
Eric Parisbc7e9822006-09-25 23:32:02 -0700264 mutex_init(&sbsec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 INIT_LIST_HEAD(&sbsec->isec_head);
266 spin_lock_init(&sbsec->isec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 sbsec->sb = sb;
268 sbsec->sid = SECINITSID_UNLABELED;
269 sbsec->def_sid = SECINITSID_FILE;
Eric Parisc312feb2006-07-10 04:43:53 -0700270 sbsec->mntpoint_sid = SECINITSID_UNLABELED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 sb->s_security = sbsec;
272
273 return 0;
274}
275
276static void superblock_free_security(struct super_block *sb)
277{
278 struct superblock_security_struct *sbsec = sb->s_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 sb->s_security = NULL;
280 kfree(sbsec);
281}
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283/* The file system's label must be initialized prior to use. */
284
Stephen Hemminger634a5392010-03-04 21:59:03 -0800285static const char *labeling_behaviors[6] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 "uses xattr",
287 "uses transition SIDs",
288 "uses task SIDs",
289 "uses genfs_contexts",
290 "not configured for labeling",
291 "uses mountpoint labeling",
292};
293
294static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry);
295
296static inline int inode_doinit(struct inode *inode)
297{
298 return inode_doinit_with_dentry(inode, NULL);
299}
300
301enum {
Eric Paris31e87932007-09-19 17:19:12 -0400302 Opt_error = -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 Opt_context = 1,
304 Opt_fscontext = 2,
Eric Parisc9180a52007-11-30 13:00:35 -0500305 Opt_defcontext = 3,
306 Opt_rootcontext = 4,
David P. Quigley11689d42009-01-16 09:22:03 -0500307 Opt_labelsupport = 5,
Eric Parisd355987f2012-08-24 15:58:53 -0400308 Opt_nextmntopt = 6,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309};
310
Eric Parisd355987f2012-08-24 15:58:53 -0400311#define NUM_SEL_MNT_OPTS (Opt_nextmntopt - 1)
312
Steven Whitehousea447c092008-10-13 10:46:57 +0100313static const match_table_t tokens = {
Eric Paris832cbd92008-04-01 13:24:09 -0400314 {Opt_context, CONTEXT_STR "%s"},
315 {Opt_fscontext, FSCONTEXT_STR "%s"},
316 {Opt_defcontext, DEFCONTEXT_STR "%s"},
317 {Opt_rootcontext, ROOTCONTEXT_STR "%s"},
David P. Quigley11689d42009-01-16 09:22:03 -0500318 {Opt_labelsupport, LABELSUPP_STR},
Eric Paris31e87932007-09-19 17:19:12 -0400319 {Opt_error, NULL},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320};
321
322#define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n"
323
Eric Parisc312feb2006-07-10 04:43:53 -0700324static int may_context_mount_sb_relabel(u32 sid,
325 struct superblock_security_struct *sbsec,
David Howells275bb412008-11-14 10:39:19 +1100326 const struct cred *cred)
Eric Parisc312feb2006-07-10 04:43:53 -0700327{
David Howells275bb412008-11-14 10:39:19 +1100328 const struct task_security_struct *tsec = cred->security;
Eric Parisc312feb2006-07-10 04:43:53 -0700329 int rc;
330
331 rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
332 FILESYSTEM__RELABELFROM, NULL);
333 if (rc)
334 return rc;
335
336 rc = avc_has_perm(tsec->sid, sid, SECCLASS_FILESYSTEM,
337 FILESYSTEM__RELABELTO, NULL);
338 return rc;
339}
340
Eric Paris08089252006-07-10 04:43:55 -0700341static int may_context_mount_inode_relabel(u32 sid,
342 struct superblock_security_struct *sbsec,
David Howells275bb412008-11-14 10:39:19 +1100343 const struct cred *cred)
Eric Paris08089252006-07-10 04:43:55 -0700344{
David Howells275bb412008-11-14 10:39:19 +1100345 const struct task_security_struct *tsec = cred->security;
Eric Paris08089252006-07-10 04:43:55 -0700346 int rc;
347 rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
348 FILESYSTEM__RELABELFROM, NULL);
349 if (rc)
350 return rc;
351
352 rc = avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM,
353 FILESYSTEM__ASSOCIATE, NULL);
354 return rc;
355}
356
Eric Parisb43e7252012-10-10 14:27:35 -0400357static int selinux_is_sblabel_mnt(struct super_block *sb)
358{
359 struct superblock_security_struct *sbsec = sb->s_security;
360
361 if (sbsec->behavior == SECURITY_FS_USE_XATTR ||
362 sbsec->behavior == SECURITY_FS_USE_TRANS ||
363 sbsec->behavior == SECURITY_FS_USE_TASK)
364 return 1;
365
366 /* Special handling for sysfs. Is genfs but also has setxattr handler*/
367 if (strncmp(sb->s_type->name, "sysfs", sizeof("sysfs")) == 0)
368 return 1;
369
370 /*
371 * Special handling for rootfs. Is genfs but supports
372 * setting SELinux context on in-core inodes.
373 */
374 if (strncmp(sb->s_type->name, "rootfs", sizeof("rootfs")) == 0)
375 return 1;
376
377 return 0;
378}
379
Eric Parisc9180a52007-11-30 13:00:35 -0500380static int sb_finish_set_opts(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
382 struct superblock_security_struct *sbsec = sb->s_security;
383 struct dentry *root = sb->s_root;
Eric Parisc9180a52007-11-30 13:00:35 -0500384 struct inode *root_inode = root->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 int rc = 0;
386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
388 /* Make sure that the xattr handler exists and that no
389 error other than -ENODATA is returned by getxattr on
390 the root directory. -ENODATA is ok, as this may be
391 the first boot of the SELinux kernel before we have
392 assigned xattr values to the filesystem. */
Eric Parisc9180a52007-11-30 13:00:35 -0500393 if (!root_inode->i_op->getxattr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 printk(KERN_WARNING "SELinux: (dev %s, type %s) has no "
395 "xattr support\n", sb->s_id, sb->s_type->name);
396 rc = -EOPNOTSUPP;
397 goto out;
398 }
Eric Parisc9180a52007-11-30 13:00:35 -0500399 rc = root_inode->i_op->getxattr(root, XATTR_NAME_SELINUX, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 if (rc < 0 && rc != -ENODATA) {
401 if (rc == -EOPNOTSUPP)
402 printk(KERN_WARNING "SELinux: (dev %s, type "
403 "%s) has no security xattr handler\n",
404 sb->s_id, sb->s_type->name);
405 else
406 printk(KERN_WARNING "SELinux: (dev %s, type "
407 "%s) getxattr errno %d\n", sb->s_id,
408 sb->s_type->name, -rc);
409 goto out;
410 }
411 }
412
Eric Parisc9180a52007-11-30 13:00:35 -0500413 if (sbsec->behavior > ARRAY_SIZE(labeling_behaviors))
Eric Parisfadcdb42007-02-22 18:11:31 -0500414 printk(KERN_ERR "SELinux: initialized (dev %s, type %s), unknown behavior\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 sb->s_id, sb->s_type->name);
Eric Parisc9180a52007-11-30 13:00:35 -0500416 else
Eric Parisfadcdb42007-02-22 18:11:31 -0500417 printk(KERN_DEBUG "SELinux: initialized (dev %s, type %s), %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 sb->s_id, sb->s_type->name,
419 labeling_behaviors[sbsec->behavior-1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Eric Pariseadcabc2012-08-24 15:59:14 -0400421 sbsec->flags |= SE_SBINITIALIZED;
Eric Parisb43e7252012-10-10 14:27:35 -0400422 if (selinux_is_sblabel_mnt(sb))
Eric Paris12f348b2012-10-09 10:56:25 -0400423 sbsec->flags |= SBLABEL_MNT;
Stephen Smalley5c73fce2013-07-23 17:38:41 -0400424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 /* Initialize the root inode. */
Eric Parisc9180a52007-11-30 13:00:35 -0500426 rc = inode_doinit_with_dentry(root_inode, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 /* Initialize any other inodes associated with the superblock, e.g.
429 inodes created prior to initial policy load or inodes created
430 during get_sb by a pseudo filesystem that directly
431 populates itself. */
432 spin_lock(&sbsec->isec_lock);
433next_inode:
434 if (!list_empty(&sbsec->isec_head)) {
435 struct inode_security_struct *isec =
436 list_entry(sbsec->isec_head.next,
Eric Parisc9180a52007-11-30 13:00:35 -0500437 struct inode_security_struct, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 struct inode *inode = isec->inode;
439 spin_unlock(&sbsec->isec_lock);
440 inode = igrab(inode);
441 if (inode) {
Eric Parisc9180a52007-11-30 13:00:35 -0500442 if (!IS_PRIVATE(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 inode_doinit(inode);
444 iput(inode);
445 }
446 spin_lock(&sbsec->isec_lock);
447 list_del_init(&isec->list);
448 goto next_inode;
449 }
450 spin_unlock(&sbsec->isec_lock);
451out:
Eric Parisc9180a52007-11-30 13:00:35 -0500452 return rc;
453}
454
455/*
456 * This function should allow an FS to ask what it's mount security
457 * options were so it can use those later for submounts, displaying
458 * mount options, or whatever.
459 */
460static int selinux_get_mnt_opts(const struct super_block *sb,
Eric Parise0007522008-03-05 10:31:54 -0500461 struct security_mnt_opts *opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500462{
463 int rc = 0, i;
464 struct superblock_security_struct *sbsec = sb->s_security;
465 char *context = NULL;
466 u32 len;
467 char tmp;
468
Eric Parise0007522008-03-05 10:31:54 -0500469 security_init_mnt_opts(opts);
Eric Parisc9180a52007-11-30 13:00:35 -0500470
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500471 if (!(sbsec->flags & SE_SBINITIALIZED))
Eric Parisc9180a52007-11-30 13:00:35 -0500472 return -EINVAL;
473
474 if (!ss_initialized)
475 return -EINVAL;
476
Eric Parisaf8e50c2012-08-24 15:59:00 -0400477 /* make sure we always check enough bits to cover the mask */
478 BUILD_BUG_ON(SE_MNTMASK >= (1 << NUM_SEL_MNT_OPTS));
479
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500480 tmp = sbsec->flags & SE_MNTMASK;
Eric Parisc9180a52007-11-30 13:00:35 -0500481 /* count the number of mount options for this sb */
Eric Parisaf8e50c2012-08-24 15:59:00 -0400482 for (i = 0; i < NUM_SEL_MNT_OPTS; i++) {
Eric Parisc9180a52007-11-30 13:00:35 -0500483 if (tmp & 0x01)
Eric Parise0007522008-03-05 10:31:54 -0500484 opts->num_mnt_opts++;
Eric Parisc9180a52007-11-30 13:00:35 -0500485 tmp >>= 1;
486 }
487
Eric Parise0007522008-03-05 10:31:54 -0500488 opts->mnt_opts = kcalloc(opts->num_mnt_opts, sizeof(char *), GFP_ATOMIC);
489 if (!opts->mnt_opts) {
Eric Parisc9180a52007-11-30 13:00:35 -0500490 rc = -ENOMEM;
491 goto out_free;
492 }
493
Eric Parise0007522008-03-05 10:31:54 -0500494 opts->mnt_opts_flags = kcalloc(opts->num_mnt_opts, sizeof(int), GFP_ATOMIC);
495 if (!opts->mnt_opts_flags) {
Eric Parisc9180a52007-11-30 13:00:35 -0500496 rc = -ENOMEM;
497 goto out_free;
498 }
499
500 i = 0;
501 if (sbsec->flags & FSCONTEXT_MNT) {
502 rc = security_sid_to_context(sbsec->sid, &context, &len);
503 if (rc)
504 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500505 opts->mnt_opts[i] = context;
506 opts->mnt_opts_flags[i++] = FSCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500507 }
508 if (sbsec->flags & CONTEXT_MNT) {
509 rc = security_sid_to_context(sbsec->mntpoint_sid, &context, &len);
510 if (rc)
511 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500512 opts->mnt_opts[i] = context;
513 opts->mnt_opts_flags[i++] = CONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500514 }
515 if (sbsec->flags & DEFCONTEXT_MNT) {
516 rc = security_sid_to_context(sbsec->def_sid, &context, &len);
517 if (rc)
518 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500519 opts->mnt_opts[i] = context;
520 opts->mnt_opts_flags[i++] = DEFCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500521 }
522 if (sbsec->flags & ROOTCONTEXT_MNT) {
523 struct inode *root = sbsec->sb->s_root->d_inode;
524 struct inode_security_struct *isec = root->i_security;
525
526 rc = security_sid_to_context(isec->sid, &context, &len);
527 if (rc)
528 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500529 opts->mnt_opts[i] = context;
530 opts->mnt_opts_flags[i++] = ROOTCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500531 }
Eric Paris12f348b2012-10-09 10:56:25 -0400532 if (sbsec->flags & SBLABEL_MNT) {
David P. Quigley11689d42009-01-16 09:22:03 -0500533 opts->mnt_opts[i] = NULL;
Eric Paris12f348b2012-10-09 10:56:25 -0400534 opts->mnt_opts_flags[i++] = SBLABEL_MNT;
David P. Quigley11689d42009-01-16 09:22:03 -0500535 }
Eric Parisc9180a52007-11-30 13:00:35 -0500536
Eric Parise0007522008-03-05 10:31:54 -0500537 BUG_ON(i != opts->num_mnt_opts);
Eric Parisc9180a52007-11-30 13:00:35 -0500538
539 return 0;
540
541out_free:
Eric Parise0007522008-03-05 10:31:54 -0500542 security_free_mnt_opts(opts);
Eric Parisc9180a52007-11-30 13:00:35 -0500543 return rc;
544}
545
546static int bad_option(struct superblock_security_struct *sbsec, char flag,
547 u32 old_sid, u32 new_sid)
548{
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500549 char mnt_flags = sbsec->flags & SE_MNTMASK;
550
Eric Parisc9180a52007-11-30 13:00:35 -0500551 /* check if the old mount command had the same options */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500552 if (sbsec->flags & SE_SBINITIALIZED)
Eric Parisc9180a52007-11-30 13:00:35 -0500553 if (!(sbsec->flags & flag) ||
554 (old_sid != new_sid))
555 return 1;
556
557 /* check if we were passed the same options twice,
558 * aka someone passed context=a,context=b
559 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500560 if (!(sbsec->flags & SE_SBINITIALIZED))
561 if (mnt_flags & flag)
Eric Parisc9180a52007-11-30 13:00:35 -0500562 return 1;
563 return 0;
564}
Eric Parise0007522008-03-05 10:31:54 -0500565
Eric Parisc9180a52007-11-30 13:00:35 -0500566/*
567 * Allow filesystems with binary mount data to explicitly set mount point
568 * labeling information.
569 */
Eric Parise0007522008-03-05 10:31:54 -0500570static int selinux_set_mnt_opts(struct super_block *sb,
571 struct security_mnt_opts *opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500572{
David Howells275bb412008-11-14 10:39:19 +1100573 const struct cred *cred = current_cred();
Eric Parisc9180a52007-11-30 13:00:35 -0500574 int rc = 0, i;
Eric Parisc9180a52007-11-30 13:00:35 -0500575 struct superblock_security_struct *sbsec = sb->s_security;
576 const char *name = sb->s_type->name;
James Morris089be432008-07-15 18:32:49 +1000577 struct inode *inode = sbsec->sb->s_root->d_inode;
578 struct inode_security_struct *root_isec = inode->i_security;
Eric Parisc9180a52007-11-30 13:00:35 -0500579 u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
580 u32 defcontext_sid = 0;
Eric Parise0007522008-03-05 10:31:54 -0500581 char **mount_options = opts->mnt_opts;
582 int *flags = opts->mnt_opts_flags;
583 int num_opts = opts->num_mnt_opts;
Eric Parisc9180a52007-11-30 13:00:35 -0500584
585 mutex_lock(&sbsec->lock);
586
587 if (!ss_initialized) {
588 if (!num_opts) {
589 /* Defer initialization until selinux_complete_init,
590 after the initial policy is loaded and the security
591 server is ready to handle calls. */
Eric Parisc9180a52007-11-30 13:00:35 -0500592 goto out;
593 }
594 rc = -EINVAL;
Eric Paris744ba352008-04-17 11:52:44 -0400595 printk(KERN_WARNING "SELinux: Unable to set superblock options "
596 "before the security server is initialized\n");
Eric Parisc9180a52007-11-30 13:00:35 -0500597 goto out;
598 }
599
600 /*
Eric Parise0007522008-03-05 10:31:54 -0500601 * Binary mount data FS will come through this function twice. Once
602 * from an explicit call and once from the generic calls from the vfs.
603 * Since the generic VFS calls will not contain any security mount data
604 * we need to skip the double mount verification.
605 *
606 * This does open a hole in which we will not notice if the first
607 * mount using this sb set explict options and a second mount using
608 * this sb does not set any security options. (The first options
609 * will be used for both mounts)
610 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500611 if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
Eric Parise0007522008-03-05 10:31:54 -0500612 && (num_opts == 0))
Eric Parisf5269712008-05-14 11:27:45 -0400613 goto out;
Eric Parise0007522008-03-05 10:31:54 -0500614
615 /*
Eric Parisc9180a52007-11-30 13:00:35 -0500616 * parse the mount options, check if they are valid sids.
617 * also check if someone is trying to mount the same sb more
618 * than once with different security options.
619 */
620 for (i = 0; i < num_opts; i++) {
621 u32 sid;
David P. Quigley11689d42009-01-16 09:22:03 -0500622
Eric Paris12f348b2012-10-09 10:56:25 -0400623 if (flags[i] == SBLABEL_MNT)
David P. Quigley11689d42009-01-16 09:22:03 -0500624 continue;
Eric Parisc9180a52007-11-30 13:00:35 -0500625 rc = security_context_to_sid(mount_options[i],
626 strlen(mount_options[i]), &sid);
627 if (rc) {
628 printk(KERN_WARNING "SELinux: security_context_to_sid"
629 "(%s) failed for (dev %s, type %s) errno=%d\n",
630 mount_options[i], sb->s_id, name, rc);
631 goto out;
632 }
633 switch (flags[i]) {
634 case FSCONTEXT_MNT:
635 fscontext_sid = sid;
636
637 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
638 fscontext_sid))
639 goto out_double_mount;
640
641 sbsec->flags |= FSCONTEXT_MNT;
642 break;
643 case CONTEXT_MNT:
644 context_sid = sid;
645
646 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
647 context_sid))
648 goto out_double_mount;
649
650 sbsec->flags |= CONTEXT_MNT;
651 break;
652 case ROOTCONTEXT_MNT:
653 rootcontext_sid = sid;
654
655 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
656 rootcontext_sid))
657 goto out_double_mount;
658
659 sbsec->flags |= ROOTCONTEXT_MNT;
660
661 break;
662 case DEFCONTEXT_MNT:
663 defcontext_sid = sid;
664
665 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
666 defcontext_sid))
667 goto out_double_mount;
668
669 sbsec->flags |= DEFCONTEXT_MNT;
670
671 break;
672 default:
673 rc = -EINVAL;
674 goto out;
675 }
676 }
677
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500678 if (sbsec->flags & SE_SBINITIALIZED) {
Eric Parisc9180a52007-11-30 13:00:35 -0500679 /* previously mounted with options, but not on this attempt? */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500680 if ((sbsec->flags & SE_MNTMASK) && !num_opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500681 goto out_double_mount;
682 rc = 0;
683 goto out;
684 }
685
James Morris089be432008-07-15 18:32:49 +1000686 if (strcmp(sb->s_type->name, "proc") == 0)
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500687 sbsec->flags |= SE_SBPROC;
Eric Parisc9180a52007-11-30 13:00:35 -0500688
689 /* Determine the labeling behavior to use for this filesystem type. */
Eric Parisa64c54c2012-08-24 15:59:07 -0400690 rc = security_fs_use(sb);
Eric Parisc9180a52007-11-30 13:00:35 -0500691 if (rc) {
692 printk(KERN_WARNING "%s: security_fs_use(%s) returned %d\n",
James Morris089be432008-07-15 18:32:49 +1000693 __func__, sb->s_type->name, rc);
Eric Parisc9180a52007-11-30 13:00:35 -0500694 goto out;
695 }
696
697 /* sets the context of the superblock for the fs being mounted. */
698 if (fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100699 rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500700 if (rc)
701 goto out;
702
703 sbsec->sid = fscontext_sid;
704 }
705
706 /*
707 * Switch to using mount point labeling behavior.
708 * sets the label used on all file below the mountpoint, and will set
709 * the superblock context if not already set.
710 */
711 if (context_sid) {
712 if (!fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100713 rc = may_context_mount_sb_relabel(context_sid, sbsec,
714 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500715 if (rc)
716 goto out;
717 sbsec->sid = context_sid;
718 } else {
David Howells275bb412008-11-14 10:39:19 +1100719 rc = may_context_mount_inode_relabel(context_sid, sbsec,
720 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500721 if (rc)
722 goto out;
723 }
724 if (!rootcontext_sid)
725 rootcontext_sid = context_sid;
726
727 sbsec->mntpoint_sid = context_sid;
728 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
729 }
730
731 if (rootcontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100732 rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec,
733 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500734 if (rc)
735 goto out;
736
737 root_isec->sid = rootcontext_sid;
738 root_isec->initialized = 1;
739 }
740
741 if (defcontext_sid) {
742 if (sbsec->behavior != SECURITY_FS_USE_XATTR) {
743 rc = -EINVAL;
744 printk(KERN_WARNING "SELinux: defcontext option is "
745 "invalid for this filesystem type\n");
746 goto out;
747 }
748
749 if (defcontext_sid != sbsec->def_sid) {
750 rc = may_context_mount_inode_relabel(defcontext_sid,
David Howells275bb412008-11-14 10:39:19 +1100751 sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500752 if (rc)
753 goto out;
754 }
755
756 sbsec->def_sid = defcontext_sid;
757 }
758
759 rc = sb_finish_set_opts(sb);
760out:
Eric Parisbc7e9822006-09-25 23:32:02 -0700761 mutex_unlock(&sbsec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 return rc;
Eric Parisc9180a52007-11-30 13:00:35 -0500763out_double_mount:
764 rc = -EINVAL;
765 printk(KERN_WARNING "SELinux: mount invalid. Same superblock, different "
766 "security settings for (dev %s, type %s)\n", sb->s_id, name);
767 goto out;
768}
769
Jeff Layton094f7b62013-04-01 08:14:24 -0400770static int selinux_cmp_sb_context(const struct super_block *oldsb,
771 const struct super_block *newsb)
772{
773 struct superblock_security_struct *old = oldsb->s_security;
774 struct superblock_security_struct *new = newsb->s_security;
775 char oldflags = old->flags & SE_MNTMASK;
776 char newflags = new->flags & SE_MNTMASK;
777
778 if (oldflags != newflags)
779 goto mismatch;
780 if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid)
781 goto mismatch;
782 if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid)
783 goto mismatch;
784 if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid)
785 goto mismatch;
786 if (oldflags & ROOTCONTEXT_MNT) {
787 struct inode_security_struct *oldroot = oldsb->s_root->d_inode->i_security;
788 struct inode_security_struct *newroot = newsb->s_root->d_inode->i_security;
789 if (oldroot->sid != newroot->sid)
790 goto mismatch;
791 }
792 return 0;
793mismatch:
794 printk(KERN_WARNING "SELinux: mount invalid. Same superblock, "
795 "different security settings for (dev %s, "
796 "type %s)\n", newsb->s_id, newsb->s_type->name);
797 return -EBUSY;
798}
799
800static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
Eric Parisc9180a52007-11-30 13:00:35 -0500801 struct super_block *newsb)
802{
803 const struct superblock_security_struct *oldsbsec = oldsb->s_security;
804 struct superblock_security_struct *newsbsec = newsb->s_security;
805
806 int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT);
807 int set_context = (oldsbsec->flags & CONTEXT_MNT);
808 int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT);
809
Eric Paris0f5e6422008-04-21 16:24:11 -0400810 /*
811 * if the parent was able to be mounted it clearly had no special lsm
Al Viroe8c26252010-03-23 06:36:54 -0400812 * mount options. thus we can safely deal with this superblock later
Eric Paris0f5e6422008-04-21 16:24:11 -0400813 */
Al Viroe8c26252010-03-23 06:36:54 -0400814 if (!ss_initialized)
Jeff Layton094f7b62013-04-01 08:14:24 -0400815 return 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500816
Eric Parisc9180a52007-11-30 13:00:35 -0500817 /* how can we clone if the old one wasn't set up?? */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500818 BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED));
Eric Parisc9180a52007-11-30 13:00:35 -0500819
Jeff Layton094f7b62013-04-01 08:14:24 -0400820 /* if fs is reusing a sb, make sure that the contexts match */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500821 if (newsbsec->flags & SE_SBINITIALIZED)
Jeff Layton094f7b62013-04-01 08:14:24 -0400822 return selinux_cmp_sb_context(oldsb, newsb);
Eric Paris5a552612008-04-09 14:08:35 -0400823
Eric Parisc9180a52007-11-30 13:00:35 -0500824 mutex_lock(&newsbsec->lock);
825
826 newsbsec->flags = oldsbsec->flags;
827
828 newsbsec->sid = oldsbsec->sid;
829 newsbsec->def_sid = oldsbsec->def_sid;
830 newsbsec->behavior = oldsbsec->behavior;
831
832 if (set_context) {
833 u32 sid = oldsbsec->mntpoint_sid;
834
835 if (!set_fscontext)
836 newsbsec->sid = sid;
837 if (!set_rootcontext) {
838 struct inode *newinode = newsb->s_root->d_inode;
839 struct inode_security_struct *newisec = newinode->i_security;
840 newisec->sid = sid;
841 }
842 newsbsec->mntpoint_sid = sid;
843 }
844 if (set_rootcontext) {
845 const struct inode *oldinode = oldsb->s_root->d_inode;
846 const struct inode_security_struct *oldisec = oldinode->i_security;
847 struct inode *newinode = newsb->s_root->d_inode;
848 struct inode_security_struct *newisec = newinode->i_security;
849
850 newisec->sid = oldisec->sid;
851 }
852
853 sb_finish_set_opts(newsb);
854 mutex_unlock(&newsbsec->lock);
Jeff Layton094f7b62013-04-01 08:14:24 -0400855 return 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500856}
857
Adrian Bunk2e1479d2008-03-17 22:29:23 +0200858static int selinux_parse_opts_str(char *options,
859 struct security_mnt_opts *opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500860{
Eric Parise0007522008-03-05 10:31:54 -0500861 char *p;
Eric Parisc9180a52007-11-30 13:00:35 -0500862 char *context = NULL, *defcontext = NULL;
863 char *fscontext = NULL, *rootcontext = NULL;
Eric Parise0007522008-03-05 10:31:54 -0500864 int rc, num_mnt_opts = 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500865
Eric Parise0007522008-03-05 10:31:54 -0500866 opts->num_mnt_opts = 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500867
868 /* Standard string-based options. */
869 while ((p = strsep(&options, "|")) != NULL) {
870 int token;
871 substring_t args[MAX_OPT_ARGS];
872
873 if (!*p)
874 continue;
875
876 token = match_token(p, tokens, args);
877
878 switch (token) {
879 case Opt_context:
880 if (context || defcontext) {
881 rc = -EINVAL;
882 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
883 goto out_err;
884 }
885 context = match_strdup(&args[0]);
886 if (!context) {
887 rc = -ENOMEM;
888 goto out_err;
889 }
890 break;
891
892 case Opt_fscontext:
893 if (fscontext) {
894 rc = -EINVAL;
895 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
896 goto out_err;
897 }
898 fscontext = match_strdup(&args[0]);
899 if (!fscontext) {
900 rc = -ENOMEM;
901 goto out_err;
902 }
903 break;
904
905 case Opt_rootcontext:
906 if (rootcontext) {
907 rc = -EINVAL;
908 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
909 goto out_err;
910 }
911 rootcontext = match_strdup(&args[0]);
912 if (!rootcontext) {
913 rc = -ENOMEM;
914 goto out_err;
915 }
916 break;
917
918 case Opt_defcontext:
919 if (context || defcontext) {
920 rc = -EINVAL;
921 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
922 goto out_err;
923 }
924 defcontext = match_strdup(&args[0]);
925 if (!defcontext) {
926 rc = -ENOMEM;
927 goto out_err;
928 }
929 break;
David P. Quigley11689d42009-01-16 09:22:03 -0500930 case Opt_labelsupport:
931 break;
Eric Parisc9180a52007-11-30 13:00:35 -0500932 default:
933 rc = -EINVAL;
934 printk(KERN_WARNING "SELinux: unknown mount option\n");
935 goto out_err;
936
937 }
938 }
939
Eric Parise0007522008-03-05 10:31:54 -0500940 rc = -ENOMEM;
941 opts->mnt_opts = kcalloc(NUM_SEL_MNT_OPTS, sizeof(char *), GFP_ATOMIC);
942 if (!opts->mnt_opts)
943 goto out_err;
944
945 opts->mnt_opts_flags = kcalloc(NUM_SEL_MNT_OPTS, sizeof(int), GFP_ATOMIC);
946 if (!opts->mnt_opts_flags) {
947 kfree(opts->mnt_opts);
948 goto out_err;
Eric Parisc9180a52007-11-30 13:00:35 -0500949 }
950
Eric Parise0007522008-03-05 10:31:54 -0500951 if (fscontext) {
952 opts->mnt_opts[num_mnt_opts] = fscontext;
953 opts->mnt_opts_flags[num_mnt_opts++] = FSCONTEXT_MNT;
954 }
955 if (context) {
956 opts->mnt_opts[num_mnt_opts] = context;
957 opts->mnt_opts_flags[num_mnt_opts++] = CONTEXT_MNT;
958 }
959 if (rootcontext) {
960 opts->mnt_opts[num_mnt_opts] = rootcontext;
961 opts->mnt_opts_flags[num_mnt_opts++] = ROOTCONTEXT_MNT;
962 }
963 if (defcontext) {
964 opts->mnt_opts[num_mnt_opts] = defcontext;
965 opts->mnt_opts_flags[num_mnt_opts++] = DEFCONTEXT_MNT;
966 }
967
968 opts->num_mnt_opts = num_mnt_opts;
969 return 0;
970
Eric Parisc9180a52007-11-30 13:00:35 -0500971out_err:
972 kfree(context);
973 kfree(defcontext);
974 kfree(fscontext);
975 kfree(rootcontext);
976 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977}
Eric Parise0007522008-03-05 10:31:54 -0500978/*
979 * string mount options parsing and call set the sbsec
980 */
981static int superblock_doinit(struct super_block *sb, void *data)
982{
983 int rc = 0;
984 char *options = data;
985 struct security_mnt_opts opts;
986
987 security_init_mnt_opts(&opts);
988
989 if (!data)
990 goto out;
991
992 BUG_ON(sb->s_type->fs_flags & FS_BINARY_MOUNTDATA);
993
994 rc = selinux_parse_opts_str(options, &opts);
995 if (rc)
996 goto out_err;
997
998out:
999 rc = selinux_set_mnt_opts(sb, &opts);
1000
1001out_err:
1002 security_free_mnt_opts(&opts);
1003 return rc;
1004}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Adrian Bunk3583a712008-07-22 20:21:23 +03001006static void selinux_write_opts(struct seq_file *m,
1007 struct security_mnt_opts *opts)
Eric Paris2069f452008-07-04 09:47:13 +10001008{
1009 int i;
1010 char *prefix;
1011
1012 for (i = 0; i < opts->num_mnt_opts; i++) {
David P. Quigley11689d42009-01-16 09:22:03 -05001013 char *has_comma;
1014
1015 if (opts->mnt_opts[i])
1016 has_comma = strchr(opts->mnt_opts[i], ',');
1017 else
1018 has_comma = NULL;
Eric Paris2069f452008-07-04 09:47:13 +10001019
1020 switch (opts->mnt_opts_flags[i]) {
1021 case CONTEXT_MNT:
1022 prefix = CONTEXT_STR;
1023 break;
1024 case FSCONTEXT_MNT:
1025 prefix = FSCONTEXT_STR;
1026 break;
1027 case ROOTCONTEXT_MNT:
1028 prefix = ROOTCONTEXT_STR;
1029 break;
1030 case DEFCONTEXT_MNT:
1031 prefix = DEFCONTEXT_STR;
1032 break;
Eric Paris12f348b2012-10-09 10:56:25 -04001033 case SBLABEL_MNT:
David P. Quigley11689d42009-01-16 09:22:03 -05001034 seq_putc(m, ',');
1035 seq_puts(m, LABELSUPP_STR);
1036 continue;
Eric Paris2069f452008-07-04 09:47:13 +10001037 default:
1038 BUG();
Eric Parisa35c6c832011-04-20 10:21:28 -04001039 return;
Eric Paris2069f452008-07-04 09:47:13 +10001040 };
1041 /* we need a comma before each option */
1042 seq_putc(m, ',');
1043 seq_puts(m, prefix);
1044 if (has_comma)
1045 seq_putc(m, '\"');
1046 seq_puts(m, opts->mnt_opts[i]);
1047 if (has_comma)
1048 seq_putc(m, '\"');
1049 }
1050}
1051
1052static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
1053{
1054 struct security_mnt_opts opts;
1055 int rc;
1056
1057 rc = selinux_get_mnt_opts(sb, &opts);
Eric Paris383795c2008-07-29 17:07:26 -04001058 if (rc) {
1059 /* before policy load we may get EINVAL, don't show anything */
1060 if (rc == -EINVAL)
1061 rc = 0;
Eric Paris2069f452008-07-04 09:47:13 +10001062 return rc;
Eric Paris383795c2008-07-29 17:07:26 -04001063 }
Eric Paris2069f452008-07-04 09:47:13 +10001064
1065 selinux_write_opts(m, &opts);
1066
1067 security_free_mnt_opts(&opts);
1068
1069 return rc;
1070}
1071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072static inline u16 inode_mode_to_security_class(umode_t mode)
1073{
1074 switch (mode & S_IFMT) {
1075 case S_IFSOCK:
1076 return SECCLASS_SOCK_FILE;
1077 case S_IFLNK:
1078 return SECCLASS_LNK_FILE;
1079 case S_IFREG:
1080 return SECCLASS_FILE;
1081 case S_IFBLK:
1082 return SECCLASS_BLK_FILE;
1083 case S_IFDIR:
1084 return SECCLASS_DIR;
1085 case S_IFCHR:
1086 return SECCLASS_CHR_FILE;
1087 case S_IFIFO:
1088 return SECCLASS_FIFO_FILE;
1089
1090 }
1091
1092 return SECCLASS_FILE;
1093}
1094
James Morris13402582005-09-30 14:24:34 -04001095static inline int default_protocol_stream(int protocol)
1096{
1097 return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP);
1098}
1099
1100static inline int default_protocol_dgram(int protocol)
1101{
1102 return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
1103}
1104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105static inline u16 socket_type_to_security_class(int family, int type, int protocol)
1106{
1107 switch (family) {
1108 case PF_UNIX:
1109 switch (type) {
1110 case SOCK_STREAM:
1111 case SOCK_SEQPACKET:
1112 return SECCLASS_UNIX_STREAM_SOCKET;
1113 case SOCK_DGRAM:
1114 return SECCLASS_UNIX_DGRAM_SOCKET;
1115 }
1116 break;
1117 case PF_INET:
1118 case PF_INET6:
1119 switch (type) {
1120 case SOCK_STREAM:
James Morris13402582005-09-30 14:24:34 -04001121 if (default_protocol_stream(protocol))
1122 return SECCLASS_TCP_SOCKET;
1123 else
1124 return SECCLASS_RAWIP_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 case SOCK_DGRAM:
James Morris13402582005-09-30 14:24:34 -04001126 if (default_protocol_dgram(protocol))
1127 return SECCLASS_UDP_SOCKET;
1128 else
1129 return SECCLASS_RAWIP_SOCKET;
James Morris2ee92d42006-11-13 16:09:01 -08001130 case SOCK_DCCP:
1131 return SECCLASS_DCCP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001132 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 return SECCLASS_RAWIP_SOCKET;
1134 }
1135 break;
1136 case PF_NETLINK:
1137 switch (protocol) {
1138 case NETLINK_ROUTE:
1139 return SECCLASS_NETLINK_ROUTE_SOCKET;
1140 case NETLINK_FIREWALL:
1141 return SECCLASS_NETLINK_FIREWALL_SOCKET;
Pavel Emelyanov7f1fb602011-12-06 07:56:43 +00001142 case NETLINK_SOCK_DIAG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 return SECCLASS_NETLINK_TCPDIAG_SOCKET;
1144 case NETLINK_NFLOG:
1145 return SECCLASS_NETLINK_NFLOG_SOCKET;
1146 case NETLINK_XFRM:
1147 return SECCLASS_NETLINK_XFRM_SOCKET;
1148 case NETLINK_SELINUX:
1149 return SECCLASS_NETLINK_SELINUX_SOCKET;
1150 case NETLINK_AUDIT:
1151 return SECCLASS_NETLINK_AUDIT_SOCKET;
1152 case NETLINK_IP6_FW:
1153 return SECCLASS_NETLINK_IP6FW_SOCKET;
1154 case NETLINK_DNRTMSG:
1155 return SECCLASS_NETLINK_DNRT_SOCKET;
James Morris0c9b7942005-04-16 15:24:13 -07001156 case NETLINK_KOBJECT_UEVENT:
1157 return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 default:
1159 return SECCLASS_NETLINK_SOCKET;
1160 }
1161 case PF_PACKET:
1162 return SECCLASS_PACKET_SOCKET;
1163 case PF_KEY:
1164 return SECCLASS_KEY_SOCKET;
Christopher J. PeBenito3e3ff152006-06-09 00:25:03 -07001165 case PF_APPLETALK:
1166 return SECCLASS_APPLETALK_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 }
1168
1169 return SECCLASS_SOCKET;
1170}
1171
1172#ifdef CONFIG_PROC_FS
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001173static int selinux_proc_get_sid(struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 u16 tclass,
1175 u32 *sid)
1176{
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001177 int rc;
1178 char *buffer, *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Eric Paris828dfe12008-04-17 13:17:49 -04001180 buffer = (char *)__get_free_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 if (!buffer)
1182 return -ENOMEM;
1183
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001184 path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
1185 if (IS_ERR(path))
1186 rc = PTR_ERR(path);
1187 else {
1188 /* each process gets a /proc/PID/ entry. Strip off the
1189 * PID part to get a valid selinux labeling.
1190 * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
1191 while (path[1] >= '0' && path[1] <= '9') {
1192 path[1] = '/';
1193 path++;
1194 }
1195 rc = security_genfs_sid("proc", path, tclass, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 free_page((unsigned long)buffer);
1198 return rc;
1199}
1200#else
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001201static int selinux_proc_get_sid(struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 u16 tclass,
1203 u32 *sid)
1204{
1205 return -EINVAL;
1206}
1207#endif
1208
1209/* The inode's security attributes must be initialized before first use. */
1210static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
1211{
1212 struct superblock_security_struct *sbsec = NULL;
1213 struct inode_security_struct *isec = inode->i_security;
1214 u32 sid;
1215 struct dentry *dentry;
1216#define INITCONTEXTLEN 255
1217 char *context = NULL;
1218 unsigned len = 0;
1219 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
1221 if (isec->initialized)
1222 goto out;
1223
Eric Paris23970742006-09-25 23:32:01 -07001224 mutex_lock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 if (isec->initialized)
Eric Paris23970742006-09-25 23:32:01 -07001226 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228 sbsec = inode->i_sb->s_security;
David P. Quigley0d90a7e2009-01-16 09:22:02 -05001229 if (!(sbsec->flags & SE_SBINITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 /* Defer initialization until selinux_complete_init,
1231 after the initial policy is loaded and the security
1232 server is ready to handle calls. */
1233 spin_lock(&sbsec->isec_lock);
1234 if (list_empty(&isec->list))
1235 list_add(&isec->list, &sbsec->isec_head);
1236 spin_unlock(&sbsec->isec_lock);
Eric Paris23970742006-09-25 23:32:01 -07001237 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 }
1239
1240 switch (sbsec->behavior) {
1241 case SECURITY_FS_USE_XATTR:
1242 if (!inode->i_op->getxattr) {
1243 isec->sid = sbsec->def_sid;
1244 break;
1245 }
1246
1247 /* Need a dentry, since the xattr API requires one.
1248 Life would be simpler if we could just pass the inode. */
1249 if (opt_dentry) {
1250 /* Called from d_instantiate or d_splice_alias. */
1251 dentry = dget(opt_dentry);
1252 } else {
1253 /* Called from selinux_complete_init, try to find a dentry. */
1254 dentry = d_find_alias(inode);
1255 }
1256 if (!dentry) {
Eric Parisdf7f54c2009-03-09 14:35:58 -04001257 /*
1258 * this is can be hit on boot when a file is accessed
1259 * before the policy is loaded. When we load policy we
1260 * may find inodes that have no dentry on the
1261 * sbsec->isec_head list. No reason to complain as these
1262 * will get fixed up the next time we go through
1263 * inode_doinit with a dentry, before these inodes could
1264 * be used again by userspace.
1265 */
Eric Paris23970742006-09-25 23:32:01 -07001266 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 }
1268
1269 len = INITCONTEXTLEN;
Eric Paris4cb912f2009-02-12 14:50:05 -05001270 context = kmalloc(len+1, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 if (!context) {
1272 rc = -ENOMEM;
1273 dput(dentry);
Eric Paris23970742006-09-25 23:32:01 -07001274 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 }
Eric Paris4cb912f2009-02-12 14:50:05 -05001276 context[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX,
1278 context, len);
1279 if (rc == -ERANGE) {
James Morris314dabb2009-08-10 22:00:13 +10001280 kfree(context);
1281
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 /* Need a larger buffer. Query for the right size. */
1283 rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX,
1284 NULL, 0);
1285 if (rc < 0) {
1286 dput(dentry);
Eric Paris23970742006-09-25 23:32:01 -07001287 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 len = rc;
Eric Paris4cb912f2009-02-12 14:50:05 -05001290 context = kmalloc(len+1, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 if (!context) {
1292 rc = -ENOMEM;
1293 dput(dentry);
Eric Paris23970742006-09-25 23:32:01 -07001294 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 }
Eric Paris4cb912f2009-02-12 14:50:05 -05001296 context[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 rc = inode->i_op->getxattr(dentry,
1298 XATTR_NAME_SELINUX,
1299 context, len);
1300 }
1301 dput(dentry);
1302 if (rc < 0) {
1303 if (rc != -ENODATA) {
Eric Paris744ba352008-04-17 11:52:44 -04001304 printk(KERN_WARNING "SELinux: %s: getxattr returned "
Harvey Harrisondd6f9532008-03-06 10:03:59 +11001305 "%d for dev=%s ino=%ld\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 -rc, inode->i_sb->s_id, inode->i_ino);
1307 kfree(context);
Eric Paris23970742006-09-25 23:32:01 -07001308 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 }
1310 /* Map ENODATA to the default file SID */
1311 sid = sbsec->def_sid;
1312 rc = 0;
1313 } else {
James Morrisf5c1d5b2005-07-28 01:07:37 -07001314 rc = security_context_to_sid_default(context, rc, &sid,
Stephen Smalley869ab512008-04-04 08:46:05 -04001315 sbsec->def_sid,
1316 GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 if (rc) {
Eric Paris4ba0a8a2009-02-12 15:01:10 -05001318 char *dev = inode->i_sb->s_id;
1319 unsigned long ino = inode->i_ino;
1320
1321 if (rc == -EINVAL) {
1322 if (printk_ratelimit())
1323 printk(KERN_NOTICE "SELinux: inode=%lu on dev=%s was found to have an invalid "
1324 "context=%s. This indicates you may need to relabel the inode or the "
1325 "filesystem in question.\n", ino, dev, context);
1326 } else {
1327 printk(KERN_WARNING "SELinux: %s: context_to_sid(%s) "
1328 "returned %d for dev=%s ino=%ld\n",
1329 __func__, context, -rc, dev, ino);
1330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 kfree(context);
1332 /* Leave with the unlabeled SID */
1333 rc = 0;
1334 break;
1335 }
1336 }
1337 kfree(context);
1338 isec->sid = sid;
1339 break;
1340 case SECURITY_FS_USE_TASK:
1341 isec->sid = isec->task_sid;
1342 break;
1343 case SECURITY_FS_USE_TRANS:
1344 /* Default to the fs SID. */
1345 isec->sid = sbsec->sid;
1346
1347 /* Try to obtain a transition SID. */
1348 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Eric Paris652bb9b2011-02-01 11:05:40 -05001349 rc = security_transition_sid(isec->task_sid, sbsec->sid,
1350 isec->sclass, NULL, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 if (rc)
Eric Paris23970742006-09-25 23:32:01 -07001352 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 isec->sid = sid;
1354 break;
Eric Parisc312feb2006-07-10 04:43:53 -07001355 case SECURITY_FS_USE_MNTPOINT:
1356 isec->sid = sbsec->mntpoint_sid;
1357 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 default:
Eric Parisc312feb2006-07-10 04:43:53 -07001359 /* Default to the fs superblock SID. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 isec->sid = sbsec->sid;
1361
David P. Quigley0d90a7e2009-01-16 09:22:02 -05001362 if ((sbsec->flags & SE_SBPROC) && !S_ISLNK(inode->i_mode)) {
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001363 if (opt_dentry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001365 rc = selinux_proc_get_sid(opt_dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 isec->sclass,
1367 &sid);
1368 if (rc)
Eric Paris23970742006-09-25 23:32:01 -07001369 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 isec->sid = sid;
1371 }
1372 }
1373 break;
1374 }
1375
1376 isec->initialized = 1;
1377
Eric Paris23970742006-09-25 23:32:01 -07001378out_unlock:
1379 mutex_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380out:
1381 if (isec->sclass == SECCLASS_FILE)
1382 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 return rc;
1384}
1385
1386/* Convert a Linux signal to an access vector. */
1387static inline u32 signal_to_av(int sig)
1388{
1389 u32 perm = 0;
1390
1391 switch (sig) {
1392 case SIGCHLD:
1393 /* Commonly granted from child to parent. */
1394 perm = PROCESS__SIGCHLD;
1395 break;
1396 case SIGKILL:
1397 /* Cannot be caught or ignored */
1398 perm = PROCESS__SIGKILL;
1399 break;
1400 case SIGSTOP:
1401 /* Cannot be caught or ignored */
1402 perm = PROCESS__SIGSTOP;
1403 break;
1404 default:
1405 /* All other signals. */
1406 perm = PROCESS__SIGNAL;
1407 break;
1408 }
1409
1410 return perm;
1411}
1412
David Howells275bb412008-11-14 10:39:19 +11001413/*
David Howellsd84f4f92008-11-14 10:39:23 +11001414 * Check permission between a pair of credentials
1415 * fork check, ptrace check, etc.
1416 */
1417static int cred_has_perm(const struct cred *actor,
1418 const struct cred *target,
1419 u32 perms)
1420{
1421 u32 asid = cred_sid(actor), tsid = cred_sid(target);
1422
1423 return avc_has_perm(asid, tsid, SECCLASS_PROCESS, perms, NULL);
1424}
1425
1426/*
David Howells88e67f32008-11-14 10:39:21 +11001427 * Check permission between a pair of tasks, e.g. signal checks,
David Howells275bb412008-11-14 10:39:19 +11001428 * fork check, ptrace check, etc.
1429 * tsk1 is the actor and tsk2 is the target
David Howells3b11a1d2008-11-14 10:39:26 +11001430 * - this uses the default subjective creds of tsk1
David Howells275bb412008-11-14 10:39:19 +11001431 */
1432static int task_has_perm(const struct task_struct *tsk1,
1433 const struct task_struct *tsk2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 u32 perms)
1435{
David Howells275bb412008-11-14 10:39:19 +11001436 const struct task_security_struct *__tsec1, *__tsec2;
1437 u32 sid1, sid2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
David Howells275bb412008-11-14 10:39:19 +11001439 rcu_read_lock();
1440 __tsec1 = __task_cred(tsk1)->security; sid1 = __tsec1->sid;
1441 __tsec2 = __task_cred(tsk2)->security; sid2 = __tsec2->sid;
1442 rcu_read_unlock();
1443 return avc_has_perm(sid1, sid2, SECCLASS_PROCESS, perms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444}
1445
David Howells3b11a1d2008-11-14 10:39:26 +11001446/*
1447 * Check permission between current and another task, e.g. signal checks,
1448 * fork check, ptrace check, etc.
1449 * current is the actor and tsk2 is the target
1450 * - this uses current's subjective creds
1451 */
1452static int current_has_perm(const struct task_struct *tsk,
1453 u32 perms)
1454{
1455 u32 sid, tsid;
1456
1457 sid = current_sid();
1458 tsid = task_sid(tsk);
1459 return avc_has_perm(sid, tsid, SECCLASS_PROCESS, perms, NULL);
1460}
1461
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001462#if CAP_LAST_CAP > 63
1463#error Fix SELinux to handle capabilities > 63.
1464#endif
1465
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466/* Check whether a task is allowed to use a capability. */
Eric Paris6a9de492012-01-03 12:25:14 -05001467static int cred_has_capability(const struct cred *cred,
Eric Paris06112162008-11-11 22:02:50 +11001468 int cap, int audit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469{
Thomas Liu2bf49692009-07-14 12:14:09 -04001470 struct common_audit_data ad;
Eric Paris06112162008-11-11 22:02:50 +11001471 struct av_decision avd;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001472 u16 sclass;
David Howells3699c532009-01-06 22:27:01 +00001473 u32 sid = cred_sid(cred);
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001474 u32 av = CAP_TO_MASK(cap);
Eric Paris06112162008-11-11 22:02:50 +11001475 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
Eric Paris50c205f2012-04-04 15:01:43 -04001477 ad.type = LSM_AUDIT_DATA_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 ad.u.cap = cap;
1479
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001480 switch (CAP_TO_INDEX(cap)) {
1481 case 0:
1482 sclass = SECCLASS_CAPABILITY;
1483 break;
1484 case 1:
1485 sclass = SECCLASS_CAPABILITY2;
1486 break;
1487 default:
1488 printk(KERN_ERR
1489 "SELinux: out of range capability %d\n", cap);
1490 BUG();
Eric Parisa35c6c832011-04-20 10:21:28 -04001491 return -EINVAL;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001492 }
Eric Paris06112162008-11-11 22:02:50 +11001493
David Howells275bb412008-11-14 10:39:19 +11001494 rc = avc_has_perm_noaudit(sid, sid, sclass, av, 0, &avd);
Eric Paris9ade0cf2011-04-25 16:26:29 -04001495 if (audit == SECURITY_CAP_AUDIT) {
1496 int rc2 = avc_audit(sid, sid, sclass, av, &avd, rc, &ad, 0);
1497 if (rc2)
1498 return rc2;
1499 }
Eric Paris06112162008-11-11 22:02:50 +11001500 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501}
1502
1503/* Check whether a task is allowed to use a system operation. */
1504static int task_has_system(struct task_struct *tsk,
1505 u32 perms)
1506{
David Howells275bb412008-11-14 10:39:19 +11001507 u32 sid = task_sid(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
David Howells275bb412008-11-14 10:39:19 +11001509 return avc_has_perm(sid, SECINITSID_KERNEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 SECCLASS_SYSTEM, perms, NULL);
1511}
1512
1513/* Check whether a task has a particular permission to an inode.
1514 The 'adp' parameter is optional and allows other audit
1515 data to be passed (e.g. the dentry). */
David Howells88e67f32008-11-14 10:39:21 +11001516static int inode_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 struct inode *inode,
1518 u32 perms,
Eric Paris9ade0cf2011-04-25 16:26:29 -04001519 struct common_audit_data *adp,
1520 unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 struct inode_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +11001523 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
David Howellse0e81732009-09-02 09:13:40 +01001525 validate_creds(cred);
1526
Eric Paris828dfe12008-04-17 13:17:49 -04001527 if (unlikely(IS_PRIVATE(inode)))
Stephen Smalleybbaca6c2007-02-14 00:34:16 -08001528 return 0;
1529
David Howells88e67f32008-11-14 10:39:21 +11001530 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 isec = inode->i_security;
1532
Eric Paris9ade0cf2011-04-25 16:26:29 -04001533 return avc_has_perm_flags(sid, isec->sid, isec->sclass, perms, adp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534}
1535
1536/* Same as inode_has_perm, but pass explicit audit data containing
1537 the dentry to help the auditing code to more easily generate the
1538 pathname if needed. */
David Howells88e67f32008-11-14 10:39:21 +11001539static inline int dentry_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 struct dentry *dentry,
1541 u32 av)
1542{
1543 struct inode *inode = dentry->d_inode;
Thomas Liu2bf49692009-07-14 12:14:09 -04001544 struct common_audit_data ad;
David Howells88e67f32008-11-14 10:39:21 +11001545
Eric Paris50c205f2012-04-04 15:01:43 -04001546 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Paris2875fa02011-04-28 16:04:24 -04001547 ad.u.dentry = dentry;
1548 return inode_has_perm(cred, inode, av, &ad, 0);
1549}
1550
1551/* Same as inode_has_perm, but pass explicit audit data containing
1552 the path to help the auditing code to more easily generate the
1553 pathname if needed. */
1554static inline int path_has_perm(const struct cred *cred,
1555 struct path *path,
1556 u32 av)
1557{
1558 struct inode *inode = path->dentry->d_inode;
1559 struct common_audit_data ad;
1560
Eric Paris50c205f2012-04-04 15:01:43 -04001561 ad.type = LSM_AUDIT_DATA_PATH;
Eric Paris2875fa02011-04-28 16:04:24 -04001562 ad.u.path = *path;
Eric Paris9ade0cf2011-04-25 16:26:29 -04001563 return inode_has_perm(cred, inode, av, &ad, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564}
1565
1566/* Check whether a task can use an open file descriptor to
1567 access an inode in a given way. Check access to the
1568 descriptor itself, and then use dentry_has_perm to
1569 check a particular permission to the file.
1570 Access to the descriptor is implicitly granted if it
1571 has the same SID as the process. If av is zero, then
1572 access to the file is not checked, e.g. for cases
1573 where only the descriptor is affected like seek. */
David Howells88e67f32008-11-14 10:39:21 +11001574static int file_has_perm(const struct cred *cred,
1575 struct file *file,
1576 u32 av)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 struct file_security_struct *fsec = file->f_security;
Al Viro496ad9a2013-01-23 17:07:38 -05001579 struct inode *inode = file_inode(file);
Thomas Liu2bf49692009-07-14 12:14:09 -04001580 struct common_audit_data ad;
David Howells88e67f32008-11-14 10:39:21 +11001581 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 int rc;
1583
Eric Paris50c205f2012-04-04 15:01:43 -04001584 ad.type = LSM_AUDIT_DATA_PATH;
Eric Parisf48b7392011-04-25 12:54:27 -04001585 ad.u.path = file->f_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
David Howells275bb412008-11-14 10:39:19 +11001587 if (sid != fsec->sid) {
1588 rc = avc_has_perm(sid, fsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 SECCLASS_FD,
1590 FD__USE,
1591 &ad);
1592 if (rc)
David Howells88e67f32008-11-14 10:39:21 +11001593 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 }
1595
1596 /* av is zero if only checking access to the descriptor. */
David Howells88e67f32008-11-14 10:39:21 +11001597 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 if (av)
Eric Paris9ade0cf2011-04-25 16:26:29 -04001599 rc = inode_has_perm(cred, inode, av, &ad, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
David Howells88e67f32008-11-14 10:39:21 +11001601out:
1602 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603}
1604
1605/* Check whether a task can create a file. */
1606static int may_create(struct inode *dir,
1607 struct dentry *dentry,
1608 u16 tclass)
1609{
Paul Moore5fb49872010-04-22 14:46:19 -04001610 const struct task_security_struct *tsec = current_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 struct inode_security_struct *dsec;
1612 struct superblock_security_struct *sbsec;
David Howells275bb412008-11-14 10:39:19 +11001613 u32 sid, newsid;
Thomas Liu2bf49692009-07-14 12:14:09 -04001614 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 int rc;
1616
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 dsec = dir->i_security;
1618 sbsec = dir->i_sb->s_security;
1619
David Howells275bb412008-11-14 10:39:19 +11001620 sid = tsec->sid;
1621 newsid = tsec->create_sid;
1622
Eric Paris50c205f2012-04-04 15:01:43 -04001623 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04001624 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
David Howells275bb412008-11-14 10:39:19 +11001626 rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 DIR__ADD_NAME | DIR__SEARCH,
1628 &ad);
1629 if (rc)
1630 return rc;
1631
Eric Paris12f348b2012-10-09 10:56:25 -04001632 if (!newsid || !(sbsec->flags & SBLABEL_MNT)) {
Eric Pariscb1e9222011-04-28 15:11:21 -04001633 rc = security_transition_sid(sid, dsec->sid, tclass,
1634 &dentry->d_name, &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 if (rc)
1636 return rc;
1637 }
1638
David Howells275bb412008-11-14 10:39:19 +11001639 rc = avc_has_perm(sid, newsid, tclass, FILE__CREATE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 if (rc)
1641 return rc;
1642
1643 return avc_has_perm(newsid, sbsec->sid,
1644 SECCLASS_FILESYSTEM,
1645 FILESYSTEM__ASSOCIATE, &ad);
1646}
1647
Michael LeMay4eb582c2006-06-26 00:24:57 -07001648/* Check whether a task can create a key. */
1649static int may_create_key(u32 ksid,
1650 struct task_struct *ctx)
1651{
David Howells275bb412008-11-14 10:39:19 +11001652 u32 sid = task_sid(ctx);
Michael LeMay4eb582c2006-06-26 00:24:57 -07001653
David Howells275bb412008-11-14 10:39:19 +11001654 return avc_has_perm(sid, ksid, SECCLASS_KEY, KEY__CREATE, NULL);
Michael LeMay4eb582c2006-06-26 00:24:57 -07001655}
1656
Eric Paris828dfe12008-04-17 13:17:49 -04001657#define MAY_LINK 0
1658#define MAY_UNLINK 1
1659#define MAY_RMDIR 2
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
1661/* Check whether a task can link, unlink, or rmdir a file/directory. */
1662static int may_link(struct inode *dir,
1663 struct dentry *dentry,
1664 int kind)
1665
1666{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 struct inode_security_struct *dsec, *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001668 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11001669 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 u32 av;
1671 int rc;
1672
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 dsec = dir->i_security;
1674 isec = dentry->d_inode->i_security;
1675
Eric Paris50c205f2012-04-04 15:01:43 -04001676 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04001677 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
1679 av = DIR__SEARCH;
1680 av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
David Howells275bb412008-11-14 10:39:19 +11001681 rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 if (rc)
1683 return rc;
1684
1685 switch (kind) {
1686 case MAY_LINK:
1687 av = FILE__LINK;
1688 break;
1689 case MAY_UNLINK:
1690 av = FILE__UNLINK;
1691 break;
1692 case MAY_RMDIR:
1693 av = DIR__RMDIR;
1694 break;
1695 default:
Eric Paris744ba352008-04-17 11:52:44 -04001696 printk(KERN_WARNING "SELinux: %s: unrecognized kind %d\n",
1697 __func__, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 return 0;
1699 }
1700
David Howells275bb412008-11-14 10:39:19 +11001701 rc = avc_has_perm(sid, isec->sid, isec->sclass, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 return rc;
1703}
1704
1705static inline int may_rename(struct inode *old_dir,
1706 struct dentry *old_dentry,
1707 struct inode *new_dir,
1708 struct dentry *new_dentry)
1709{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001711 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11001712 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 u32 av;
1714 int old_is_dir, new_is_dir;
1715 int rc;
1716
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 old_dsec = old_dir->i_security;
1718 old_isec = old_dentry->d_inode->i_security;
1719 old_is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
1720 new_dsec = new_dir->i_security;
1721
Eric Paris50c205f2012-04-04 15:01:43 -04001722 ad.type = LSM_AUDIT_DATA_DENTRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
Eric Parisa2694342011-04-25 13:10:27 -04001724 ad.u.dentry = old_dentry;
David Howells275bb412008-11-14 10:39:19 +11001725 rc = avc_has_perm(sid, old_dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 DIR__REMOVE_NAME | DIR__SEARCH, &ad);
1727 if (rc)
1728 return rc;
David Howells275bb412008-11-14 10:39:19 +11001729 rc = avc_has_perm(sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 old_isec->sclass, FILE__RENAME, &ad);
1731 if (rc)
1732 return rc;
1733 if (old_is_dir && new_dir != old_dir) {
David Howells275bb412008-11-14 10:39:19 +11001734 rc = avc_has_perm(sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 old_isec->sclass, DIR__REPARENT, &ad);
1736 if (rc)
1737 return rc;
1738 }
1739
Eric Parisa2694342011-04-25 13:10:27 -04001740 ad.u.dentry = new_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 av = DIR__ADD_NAME | DIR__SEARCH;
1742 if (new_dentry->d_inode)
1743 av |= DIR__REMOVE_NAME;
David Howells275bb412008-11-14 10:39:19 +11001744 rc = avc_has_perm(sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 if (rc)
1746 return rc;
1747 if (new_dentry->d_inode) {
1748 new_isec = new_dentry->d_inode->i_security;
1749 new_is_dir = S_ISDIR(new_dentry->d_inode->i_mode);
David Howells275bb412008-11-14 10:39:19 +11001750 rc = avc_has_perm(sid, new_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 new_isec->sclass,
1752 (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
1753 if (rc)
1754 return rc;
1755 }
1756
1757 return 0;
1758}
1759
1760/* Check whether a task can perform a filesystem operation. */
David Howells88e67f32008-11-14 10:39:21 +11001761static int superblock_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 struct super_block *sb,
1763 u32 perms,
Thomas Liu2bf49692009-07-14 12:14:09 -04001764 struct common_audit_data *ad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 struct superblock_security_struct *sbsec;
David Howells88e67f32008-11-14 10:39:21 +11001767 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 sbsec = sb->s_security;
David Howells275bb412008-11-14 10:39:19 +11001770 return avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771}
1772
1773/* Convert a Linux mode and permission mask to an access vector. */
1774static inline u32 file_mask_to_av(int mode, int mask)
1775{
1776 u32 av = 0;
1777
Al Virodba19c62011-07-25 20:49:29 -04001778 if (!S_ISDIR(mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 if (mask & MAY_EXEC)
1780 av |= FILE__EXECUTE;
1781 if (mask & MAY_READ)
1782 av |= FILE__READ;
1783
1784 if (mask & MAY_APPEND)
1785 av |= FILE__APPEND;
1786 else if (mask & MAY_WRITE)
1787 av |= FILE__WRITE;
1788
1789 } else {
1790 if (mask & MAY_EXEC)
1791 av |= DIR__SEARCH;
1792 if (mask & MAY_WRITE)
1793 av |= DIR__WRITE;
1794 if (mask & MAY_READ)
1795 av |= DIR__READ;
1796 }
1797
1798 return av;
1799}
1800
1801/* Convert a Linux file to an access vector. */
1802static inline u32 file_to_av(struct file *file)
1803{
1804 u32 av = 0;
1805
1806 if (file->f_mode & FMODE_READ)
1807 av |= FILE__READ;
1808 if (file->f_mode & FMODE_WRITE) {
1809 if (file->f_flags & O_APPEND)
1810 av |= FILE__APPEND;
1811 else
1812 av |= FILE__WRITE;
1813 }
Stephen Smalley0794c662008-03-17 08:55:18 -04001814 if (!av) {
1815 /*
1816 * Special file opened with flags 3 for ioctl-only use.
1817 */
1818 av = FILE__IOCTL;
1819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
1821 return av;
1822}
1823
Eric Paris8b6a5a32008-10-29 17:06:46 -04001824/*
1825 * Convert a file to an access vector and include the correct open
1826 * open permission.
1827 */
1828static inline u32 open_file_to_av(struct file *file)
1829{
1830 u32 av = file_to_av(file);
1831
Eric Paris49b7b8d2010-07-23 11:44:09 -04001832 if (selinux_policycap_openperm)
1833 av |= FILE__OPEN;
1834
Eric Paris8b6a5a32008-10-29 17:06:46 -04001835 return av;
1836}
1837
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838/* Hook functions begin here. */
1839
Ingo Molnar9e488582009-05-07 19:26:19 +10001840static int selinux_ptrace_access_check(struct task_struct *child,
David Howells5cd9c582008-08-14 11:37:28 +01001841 unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 int rc;
1844
Ingo Molnar9e488582009-05-07 19:26:19 +10001845 rc = cap_ptrace_access_check(child, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 if (rc)
1847 return rc;
1848
Eric Paris69f594a2012-01-03 12:25:15 -05001849 if (mode & PTRACE_MODE_READ) {
David Howells275bb412008-11-14 10:39:19 +11001850 u32 sid = current_sid();
1851 u32 csid = task_sid(child);
1852 return avc_has_perm(sid, csid, SECCLASS_FILE, FILE__READ, NULL);
Stephen Smalley006ebb42008-05-19 08:32:49 -04001853 }
1854
David Howells3b11a1d2008-11-14 10:39:26 +11001855 return current_has_perm(child, PROCESS__PTRACE);
David Howells5cd9c582008-08-14 11:37:28 +01001856}
1857
1858static int selinux_ptrace_traceme(struct task_struct *parent)
1859{
1860 int rc;
1861
Eric Paris200ac532009-02-12 15:01:04 -05001862 rc = cap_ptrace_traceme(parent);
David Howells5cd9c582008-08-14 11:37:28 +01001863 if (rc)
1864 return rc;
1865
1866 return task_has_perm(parent, current, PROCESS__PTRACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867}
1868
1869static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
Eric Paris828dfe12008-04-17 13:17:49 -04001870 kernel_cap_t *inheritable, kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871{
1872 int error;
1873
David Howells3b11a1d2008-11-14 10:39:26 +11001874 error = current_has_perm(target, PROCESS__GETCAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 if (error)
1876 return error;
1877
Eric Paris200ac532009-02-12 15:01:04 -05001878 return cap_capget(target, effective, inheritable, permitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879}
1880
David Howellsd84f4f92008-11-14 10:39:23 +11001881static int selinux_capset(struct cred *new, const struct cred *old,
1882 const kernel_cap_t *effective,
1883 const kernel_cap_t *inheritable,
1884 const kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885{
1886 int error;
1887
Eric Paris200ac532009-02-12 15:01:04 -05001888 error = cap_capset(new, old,
David Howellsd84f4f92008-11-14 10:39:23 +11001889 effective, inheritable, permitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 if (error)
1891 return error;
1892
David Howellsd84f4f92008-11-14 10:39:23 +11001893 return cred_has_perm(old, new, PROCESS__SETCAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894}
1895
James Morris5626d3e2009-01-30 10:05:06 +11001896/*
1897 * (This comment used to live with the selinux_task_setuid hook,
1898 * which was removed).
1899 *
1900 * Since setuid only affects the current process, and since the SELinux
1901 * controls are not based on the Linux identity attributes, SELinux does not
1902 * need to control this operation. However, SELinux does control the use of
1903 * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
1904 */
1905
Eric Paris6a9de492012-01-03 12:25:14 -05001906static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
1907 int cap, int audit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908{
1909 int rc;
1910
Eric Paris6a9de492012-01-03 12:25:14 -05001911 rc = cap_capable(cred, ns, cap, audit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 if (rc)
1913 return rc;
1914
Eric Paris6a9de492012-01-03 12:25:14 -05001915 return cred_has_capability(cred, cap, audit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916}
1917
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
1919{
David Howells88e67f32008-11-14 10:39:21 +11001920 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 int rc = 0;
1922
1923 if (!sb)
1924 return 0;
1925
1926 switch (cmds) {
Eric Paris828dfe12008-04-17 13:17:49 -04001927 case Q_SYNC:
1928 case Q_QUOTAON:
1929 case Q_QUOTAOFF:
1930 case Q_SETINFO:
1931 case Q_SETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11001932 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04001933 break;
1934 case Q_GETFMT:
1935 case Q_GETINFO:
1936 case Q_GETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11001937 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04001938 break;
1939 default:
1940 rc = 0; /* let the kernel handle invalid cmds */
1941 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 }
1943 return rc;
1944}
1945
1946static int selinux_quota_on(struct dentry *dentry)
1947{
David Howells88e67f32008-11-14 10:39:21 +11001948 const struct cred *cred = current_cred();
1949
Eric Paris2875fa02011-04-28 16:04:24 -04001950 return dentry_has_perm(cred, dentry, FILE__QUOTAON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951}
1952
Eric Paris12b30522010-11-15 18:36:29 -05001953static int selinux_syslog(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954{
1955 int rc;
1956
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 switch (type) {
Kees Cookd78ca3c2010-02-03 15:37:13 -08001958 case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */
1959 case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */
Eric Paris828dfe12008-04-17 13:17:49 -04001960 rc = task_has_system(current, SYSTEM__SYSLOG_READ);
1961 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001962 case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */
1963 case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */
1964 /* Set level of messages printed to console */
1965 case SYSLOG_ACTION_CONSOLE_LEVEL:
Eric Paris828dfe12008-04-17 13:17:49 -04001966 rc = task_has_system(current, SYSTEM__SYSLOG_CONSOLE);
1967 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001968 case SYSLOG_ACTION_CLOSE: /* Close log */
1969 case SYSLOG_ACTION_OPEN: /* Open log */
1970 case SYSLOG_ACTION_READ: /* Read from log */
1971 case SYSLOG_ACTION_READ_CLEAR: /* Read/clear last kernel messages */
1972 case SYSLOG_ACTION_CLEAR: /* Clear ring buffer */
Eric Paris828dfe12008-04-17 13:17:49 -04001973 default:
1974 rc = task_has_system(current, SYSTEM__SYSLOG_MOD);
1975 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 }
1977 return rc;
1978}
1979
1980/*
1981 * Check that a process has enough memory to allocate a new virtual
1982 * mapping. 0 means there is enough memory for the allocation to
1983 * succeed and -ENOMEM implies there is not.
1984 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 * Do not audit the selinux permission check, as this is applied to all
1986 * processes that allocate mappings.
1987 */
Alan Cox34b4e4a2007-08-22 14:01:28 -07001988static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989{
1990 int rc, cap_sys_admin = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
Eric Paris6a9de492012-01-03 12:25:14 -05001992 rc = selinux_capable(current_cred(), &init_user_ns, CAP_SYS_ADMIN,
David Howells3699c532009-01-06 22:27:01 +00001993 SECURITY_CAP_NOAUDIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 if (rc == 0)
1995 cap_sys_admin = 1;
1996
Alan Cox34b4e4a2007-08-22 14:01:28 -07001997 return __vm_enough_memory(mm, pages, cap_sys_admin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998}
1999
2000/* binprm security operations */
2001
David Howellsa6f76f22008-11-14 10:39:24 +11002002static int selinux_bprm_set_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003{
David Howellsa6f76f22008-11-14 10:39:24 +11002004 const struct task_security_struct *old_tsec;
2005 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 struct inode_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04002007 struct common_audit_data ad;
Al Viro496ad9a2013-01-23 17:07:38 -05002008 struct inode *inode = file_inode(bprm->file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 int rc;
2010
Eric Paris200ac532009-02-12 15:01:04 -05002011 rc = cap_bprm_set_creds(bprm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 if (rc)
2013 return rc;
2014
David Howellsa6f76f22008-11-14 10:39:24 +11002015 /* SELinux context only depends on initial program or script and not
2016 * the script interpreter */
2017 if (bprm->cred_prepared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 return 0;
2019
David Howellsa6f76f22008-11-14 10:39:24 +11002020 old_tsec = current_security();
2021 new_tsec = bprm->cred->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 isec = inode->i_security;
2023
2024 /* Default to the current task SID. */
David Howellsa6f76f22008-11-14 10:39:24 +11002025 new_tsec->sid = old_tsec->sid;
2026 new_tsec->osid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
Michael LeMay28eba5b2006-06-27 02:53:42 -07002028 /* Reset fs, key, and sock SIDs on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002029 new_tsec->create_sid = 0;
2030 new_tsec->keycreate_sid = 0;
2031 new_tsec->sockcreate_sid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
David Howellsa6f76f22008-11-14 10:39:24 +11002033 if (old_tsec->exec_sid) {
2034 new_tsec->sid = old_tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 /* Reset exec SID on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002036 new_tsec->exec_sid = 0;
Andy Lutomirski259e5e62012-04-12 16:47:50 -05002037
2038 /*
2039 * Minimize confusion: if no_new_privs and a transition is
2040 * explicitly requested, then fail the exec.
2041 */
2042 if (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS)
2043 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 } else {
2045 /* Check for a default transition on this program. */
David Howellsa6f76f22008-11-14 10:39:24 +11002046 rc = security_transition_sid(old_tsec->sid, isec->sid,
Eric Paris652bb9b2011-02-01 11:05:40 -05002047 SECCLASS_PROCESS, NULL,
2048 &new_tsec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 if (rc)
2050 return rc;
2051 }
2052
Eric Paris50c205f2012-04-04 15:01:43 -04002053 ad.type = LSM_AUDIT_DATA_PATH;
Eric Parisf48b7392011-04-25 12:54:27 -04002054 ad.u.path = bprm->file->f_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
Andy Lutomirski259e5e62012-04-12 16:47:50 -05002056 if ((bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID) ||
2057 (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS))
David Howellsa6f76f22008-11-14 10:39:24 +11002058 new_tsec->sid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
David Howellsa6f76f22008-11-14 10:39:24 +11002060 if (new_tsec->sid == old_tsec->sid) {
2061 rc = avc_has_perm(old_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
2063 if (rc)
2064 return rc;
2065 } else {
2066 /* Check permissions for the transition. */
David Howellsa6f76f22008-11-14 10:39:24 +11002067 rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
2069 if (rc)
2070 return rc;
2071
David Howellsa6f76f22008-11-14 10:39:24 +11002072 rc = avc_has_perm(new_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
2074 if (rc)
2075 return rc;
2076
David Howellsa6f76f22008-11-14 10:39:24 +11002077 /* Check for shared state */
2078 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
2079 rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
2080 SECCLASS_PROCESS, PROCESS__SHARE,
2081 NULL);
2082 if (rc)
2083 return -EPERM;
2084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
David Howellsa6f76f22008-11-14 10:39:24 +11002086 /* Make sure that anyone attempting to ptrace over a task that
2087 * changes its SID has the appropriate permit */
2088 if (bprm->unsafe &
2089 (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
2090 struct task_struct *tracer;
2091 struct task_security_struct *sec;
2092 u32 ptsid = 0;
2093
2094 rcu_read_lock();
Tejun Heo06d98472011-06-17 16:50:40 +02002095 tracer = ptrace_parent(current);
David Howellsa6f76f22008-11-14 10:39:24 +11002096 if (likely(tracer != NULL)) {
2097 sec = __task_cred(tracer)->security;
2098 ptsid = sec->sid;
2099 }
2100 rcu_read_unlock();
2101
2102 if (ptsid != 0) {
2103 rc = avc_has_perm(ptsid, new_tsec->sid,
2104 SECCLASS_PROCESS,
2105 PROCESS__PTRACE, NULL);
2106 if (rc)
2107 return -EPERM;
2108 }
2109 }
2110
2111 /* Clear any possibly unsafe personality bits on exec: */
2112 bprm->per_clear |= PER_CLEAR_ON_SETID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 }
2114
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 return 0;
2116}
2117
Eric Paris828dfe12008-04-17 13:17:49 -04002118static int selinux_bprm_secureexec(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119{
Paul Moore5fb49872010-04-22 14:46:19 -04002120 const struct task_security_struct *tsec = current_security();
David Howells275bb412008-11-14 10:39:19 +11002121 u32 sid, osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 int atsecure = 0;
2123
David Howells275bb412008-11-14 10:39:19 +11002124 sid = tsec->sid;
2125 osid = tsec->osid;
2126
2127 if (osid != sid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 /* Enable secure mode for SIDs transitions unless
2129 the noatsecure permission is granted between
2130 the two SIDs, i.e. ahp returns 0. */
David Howells275bb412008-11-14 10:39:19 +11002131 atsecure = avc_has_perm(osid, sid,
David Howellsa6f76f22008-11-14 10:39:24 +11002132 SECCLASS_PROCESS,
2133 PROCESS__NOATSECURE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 }
2135
Eric Paris200ac532009-02-12 15:01:04 -05002136 return (atsecure || cap_bprm_secureexec(bprm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137}
2138
Al Viroc3c073f2012-08-21 22:32:06 -04002139static int match_file(const void *p, struct file *file, unsigned fd)
2140{
2141 return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0;
2142}
2143
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144/* Derived from fs/exec.c:flush_old_files. */
David Howells745ca242008-11-14 10:39:22 +11002145static inline void flush_unauthorized_files(const struct cred *cred,
2146 struct files_struct *files)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 struct file *file, *devnull = NULL;
Stephen Smalleyb20c8122006-09-25 23:32:03 -07002149 struct tty_struct *tty;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002150 int drop_tty = 0;
Al Viroc3c073f2012-08-21 22:32:06 -04002151 unsigned n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002153 tty = get_current_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 if (tty) {
Nick Pigginee2ffa02010-08-18 04:37:35 +10002155 spin_lock(&tty_files_lock);
Eric Paris37dd0bd2008-10-31 17:40:00 -04002156 if (!list_empty(&tty->tty_files)) {
Nick Piggind996b622010-08-18 04:37:36 +10002157 struct tty_file_private *file_priv;
Eric Paris37dd0bd2008-10-31 17:40:00 -04002158
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 /* Revalidate access to controlling tty.
Eric Paris602a8dd2012-04-04 15:01:42 -04002160 Use path_has_perm on the tty path directly rather
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 than using file_has_perm, as this particular open
2162 file may belong to another process and we are only
2163 interested in the inode-based check here. */
Nick Piggind996b622010-08-18 04:37:36 +10002164 file_priv = list_first_entry(&tty->tty_files,
2165 struct tty_file_private, list);
2166 file = file_priv->file;
Eric Paris602a8dd2012-04-04 15:01:42 -04002167 if (path_has_perm(cred, &file->f_path, FILE__READ | FILE__WRITE))
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002168 drop_tty = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 }
Nick Pigginee2ffa02010-08-18 04:37:35 +10002170 spin_unlock(&tty_files_lock);
Alan Cox452a00d2008-10-13 10:39:13 +01002171 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 }
Eric W. Biederman98a27ba2007-05-08 00:26:56 -07002173 /* Reset controlling tty. */
2174 if (drop_tty)
2175 no_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
2177 /* Revalidate access to inherited open files. */
Al Viroc3c073f2012-08-21 22:32:06 -04002178 n = iterate_fd(files, 0, match_file, cred);
2179 if (!n) /* none found? */
2180 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
Al Viroc3c073f2012-08-21 22:32:06 -04002182 devnull = dentry_open(&selinux_null, O_RDWR, cred);
Al Viro45525b22012-10-16 13:30:07 -04002183 if (IS_ERR(devnull))
2184 devnull = NULL;
2185 /* replace all the matching ones with this */
2186 do {
2187 replace_fd(n - 1, devnull, 0);
2188 } while ((n = iterate_fd(files, n, match_file, cred)) != 0);
2189 if (devnull)
Al Viroc3c073f2012-08-21 22:32:06 -04002190 fput(devnull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191}
2192
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193/*
David Howellsa6f76f22008-11-14 10:39:24 +11002194 * Prepare a process for imminent new credential changes due to exec
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 */
David Howellsa6f76f22008-11-14 10:39:24 +11002196static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197{
David Howellsa6f76f22008-11-14 10:39:24 +11002198 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 struct rlimit *rlim, *initrlim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 int rc, i;
2201
David Howellsa6f76f22008-11-14 10:39:24 +11002202 new_tsec = bprm->cred->security;
2203 if (new_tsec->sid == new_tsec->osid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 return;
2205
2206 /* Close files for which the new task SID is not authorized. */
David Howellsa6f76f22008-11-14 10:39:24 +11002207 flush_unauthorized_files(bprm->cred, current->files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
David Howellsa6f76f22008-11-14 10:39:24 +11002209 /* Always clear parent death signal on SID transitions. */
2210 current->pdeath_signal = 0;
2211
2212 /* Check whether the new SID can inherit resource limits from the old
2213 * SID. If not, reset all soft limits to the lower of the current
2214 * task's hard limit and the init task's soft limit.
2215 *
2216 * Note that the setting of hard limits (even to lower them) can be
2217 * controlled by the setrlimit check. The inclusion of the init task's
2218 * soft limit into the computation is to avoid resetting soft limits
2219 * higher than the default soft limit for cases where the default is
2220 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2221 */
2222 rc = avc_has_perm(new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
2223 PROCESS__RLIMITINH, NULL);
2224 if (rc) {
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002225 /* protect against do_prlimit() */
2226 task_lock(current);
David Howellsa6f76f22008-11-14 10:39:24 +11002227 for (i = 0; i < RLIM_NLIMITS; i++) {
2228 rlim = current->signal->rlim + i;
2229 initrlim = init_task.signal->rlim + i;
2230 rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
2231 }
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002232 task_unlock(current);
2233 update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
David Howellsa6f76f22008-11-14 10:39:24 +11002234 }
2235}
2236
2237/*
2238 * Clean up the process immediately after the installation of new credentials
2239 * due to exec
2240 */
2241static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
2242{
2243 const struct task_security_struct *tsec = current_security();
2244 struct itimerval itimer;
David Howellsa6f76f22008-11-14 10:39:24 +11002245 u32 osid, sid;
2246 int rc, i;
David Howellsa6f76f22008-11-14 10:39:24 +11002247
David Howellsa6f76f22008-11-14 10:39:24 +11002248 osid = tsec->osid;
2249 sid = tsec->sid;
2250
2251 if (sid == osid)
2252 return;
2253
2254 /* Check whether the new SID can inherit signal state from the old SID.
2255 * If not, clear itimers to avoid subsequent signal generation and
2256 * flush and unblock signals.
2257 *
2258 * This must occur _after_ the task SID has been updated so that any
2259 * kill done after the flush will be checked against the new SID.
2260 */
2261 rc = avc_has_perm(osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 if (rc) {
2263 memset(&itimer, 0, sizeof itimer);
2264 for (i = 0; i < 3; i++)
2265 do_setitimer(i, &itimer, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 spin_lock_irq(&current->sighand->siglock);
David Howells3bcac022009-04-29 13:45:05 +01002267 if (!(current->signal->flags & SIGNAL_GROUP_EXIT)) {
2268 __flush_signals(current);
2269 flush_signal_handlers(current, 1);
2270 sigemptyset(&current->blocked);
2271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 spin_unlock_irq(&current->sighand->siglock);
2273 }
2274
David Howellsa6f76f22008-11-14 10:39:24 +11002275 /* Wake up the parent if it is waiting so that it can recheck
2276 * wait permission to the new task SID. */
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002277 read_lock(&tasklist_lock);
Oleg Nesterov0b7570e2009-09-23 15:56:46 -07002278 __wake_up_parent(current, current->real_parent);
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002279 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280}
2281
2282/* superblock security operations */
2283
2284static int selinux_sb_alloc_security(struct super_block *sb)
2285{
2286 return superblock_alloc_security(sb);
2287}
2288
2289static void selinux_sb_free_security(struct super_block *sb)
2290{
2291 superblock_free_security(sb);
2292}
2293
2294static inline int match_prefix(char *prefix, int plen, char *option, int olen)
2295{
2296 if (plen > olen)
2297 return 0;
2298
2299 return !memcmp(prefix, option, plen);
2300}
2301
2302static inline int selinux_option(char *option, int len)
2303{
Eric Paris832cbd92008-04-01 13:24:09 -04002304 return (match_prefix(CONTEXT_STR, sizeof(CONTEXT_STR)-1, option, len) ||
2305 match_prefix(FSCONTEXT_STR, sizeof(FSCONTEXT_STR)-1, option, len) ||
2306 match_prefix(DEFCONTEXT_STR, sizeof(DEFCONTEXT_STR)-1, option, len) ||
David P. Quigley11689d42009-01-16 09:22:03 -05002307 match_prefix(ROOTCONTEXT_STR, sizeof(ROOTCONTEXT_STR)-1, option, len) ||
2308 match_prefix(LABELSUPP_STR, sizeof(LABELSUPP_STR)-1, option, len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309}
2310
2311static inline void take_option(char **to, char *from, int *first, int len)
2312{
2313 if (!*first) {
2314 **to = ',';
2315 *to += 1;
Cory Olmo3528a952006-09-29 01:58:44 -07002316 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 *first = 0;
2318 memcpy(*to, from, len);
2319 *to += len;
2320}
2321
Eric Paris828dfe12008-04-17 13:17:49 -04002322static inline void take_selinux_option(char **to, char *from, int *first,
2323 int len)
Cory Olmo3528a952006-09-29 01:58:44 -07002324{
2325 int current_size = 0;
2326
2327 if (!*first) {
2328 **to = '|';
2329 *to += 1;
Eric Paris828dfe12008-04-17 13:17:49 -04002330 } else
Cory Olmo3528a952006-09-29 01:58:44 -07002331 *first = 0;
2332
2333 while (current_size < len) {
2334 if (*from != '"') {
2335 **to = *from;
2336 *to += 1;
2337 }
2338 from += 1;
2339 current_size += 1;
2340 }
2341}
2342
Eric Parise0007522008-03-05 10:31:54 -05002343static int selinux_sb_copy_data(char *orig, char *copy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344{
2345 int fnosec, fsec, rc = 0;
2346 char *in_save, *in_curr, *in_end;
2347 char *sec_curr, *nosec_save, *nosec;
Cory Olmo3528a952006-09-29 01:58:44 -07002348 int open_quote = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
2350 in_curr = orig;
2351 sec_curr = copy;
2352
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 nosec = (char *)get_zeroed_page(GFP_KERNEL);
2354 if (!nosec) {
2355 rc = -ENOMEM;
2356 goto out;
2357 }
2358
2359 nosec_save = nosec;
2360 fnosec = fsec = 1;
2361 in_save = in_end = orig;
2362
2363 do {
Cory Olmo3528a952006-09-29 01:58:44 -07002364 if (*in_end == '"')
2365 open_quote = !open_quote;
2366 if ((*in_end == ',' && open_quote == 0) ||
2367 *in_end == '\0') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 int len = in_end - in_curr;
2369
2370 if (selinux_option(in_curr, len))
Cory Olmo3528a952006-09-29 01:58:44 -07002371 take_selinux_option(&sec_curr, in_curr, &fsec, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 else
2373 take_option(&nosec, in_curr, &fnosec, len);
2374
2375 in_curr = in_end + 1;
2376 }
2377 } while (*in_end++);
2378
Eric Paris6931dfc2005-06-30 02:58:51 -07002379 strcpy(in_save, nosec_save);
Gerald Schaeferda3caa22005-06-21 17:15:18 -07002380 free_page((unsigned long)nosec_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381out:
2382 return rc;
2383}
2384
Eric Paris026eb162011-03-03 16:09:14 -05002385static int selinux_sb_remount(struct super_block *sb, void *data)
2386{
2387 int rc, i, *flags;
2388 struct security_mnt_opts opts;
2389 char *secdata, **mount_options;
2390 struct superblock_security_struct *sbsec = sb->s_security;
2391
2392 if (!(sbsec->flags & SE_SBINITIALIZED))
2393 return 0;
2394
2395 if (!data)
2396 return 0;
2397
2398 if (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
2399 return 0;
2400
2401 security_init_mnt_opts(&opts);
2402 secdata = alloc_secdata();
2403 if (!secdata)
2404 return -ENOMEM;
2405 rc = selinux_sb_copy_data(data, secdata);
2406 if (rc)
2407 goto out_free_secdata;
2408
2409 rc = selinux_parse_opts_str(secdata, &opts);
2410 if (rc)
2411 goto out_free_secdata;
2412
2413 mount_options = opts.mnt_opts;
2414 flags = opts.mnt_opts_flags;
2415
2416 for (i = 0; i < opts.num_mnt_opts; i++) {
2417 u32 sid;
2418 size_t len;
2419
Eric Paris12f348b2012-10-09 10:56:25 -04002420 if (flags[i] == SBLABEL_MNT)
Eric Paris026eb162011-03-03 16:09:14 -05002421 continue;
2422 len = strlen(mount_options[i]);
2423 rc = security_context_to_sid(mount_options[i], len, &sid);
2424 if (rc) {
2425 printk(KERN_WARNING "SELinux: security_context_to_sid"
2426 "(%s) failed for (dev %s, type %s) errno=%d\n",
2427 mount_options[i], sb->s_id, sb->s_type->name, rc);
2428 goto out_free_opts;
2429 }
2430 rc = -EINVAL;
2431 switch (flags[i]) {
2432 case FSCONTEXT_MNT:
2433 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
2434 goto out_bad_option;
2435 break;
2436 case CONTEXT_MNT:
2437 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
2438 goto out_bad_option;
2439 break;
2440 case ROOTCONTEXT_MNT: {
2441 struct inode_security_struct *root_isec;
2442 root_isec = sb->s_root->d_inode->i_security;
2443
2444 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
2445 goto out_bad_option;
2446 break;
2447 }
2448 case DEFCONTEXT_MNT:
2449 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
2450 goto out_bad_option;
2451 break;
2452 default:
2453 goto out_free_opts;
2454 }
2455 }
2456
2457 rc = 0;
2458out_free_opts:
2459 security_free_mnt_opts(&opts);
2460out_free_secdata:
2461 free_secdata(secdata);
2462 return rc;
2463out_bad_option:
2464 printk(KERN_WARNING "SELinux: unable to change security options "
2465 "during remount (dev %s, type=%s)\n", sb->s_id,
2466 sb->s_type->name);
2467 goto out_free_opts;
2468}
2469
James Morris12204e22008-12-19 10:44:42 +11002470static int selinux_sb_kern_mount(struct super_block *sb, int flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471{
David Howells88e67f32008-11-14 10:39:21 +11002472 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002473 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 int rc;
2475
2476 rc = superblock_doinit(sb, data);
2477 if (rc)
2478 return rc;
2479
James Morris74192242008-12-19 11:41:10 +11002480 /* Allow all mounts performed by the kernel */
2481 if (flags & MS_KERNMOUNT)
2482 return 0;
2483
Eric Paris50c205f2012-04-04 15:01:43 -04002484 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002485 ad.u.dentry = sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002486 return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487}
2488
David Howells726c3342006-06-23 02:02:58 -07002489static int selinux_sb_statfs(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490{
David Howells88e67f32008-11-14 10:39:21 +11002491 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002492 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
Eric Paris50c205f2012-04-04 15:01:43 -04002494 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002495 ad.u.dentry = dentry->d_sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002496 return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497}
2498
Al Viro808d4e32012-10-11 11:42:01 -04002499static int selinux_mount(const char *dev_name,
Al Virob5266eb2008-03-22 17:48:24 -04002500 struct path *path,
Al Viro808d4e32012-10-11 11:42:01 -04002501 const char *type,
Eric Paris828dfe12008-04-17 13:17:49 -04002502 unsigned long flags,
2503 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504{
David Howells88e67f32008-11-14 10:39:21 +11002505 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506
2507 if (flags & MS_REMOUNT)
Al Virod8c95842011-12-07 18:16:57 -05002508 return superblock_has_perm(cred, path->dentry->d_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002509 FILESYSTEM__REMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 else
Eric Paris2875fa02011-04-28 16:04:24 -04002511 return path_has_perm(cred, path, FILE__MOUNTON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512}
2513
2514static int selinux_umount(struct vfsmount *mnt, int flags)
2515{
David Howells88e67f32008-11-14 10:39:21 +11002516 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517
David Howells88e67f32008-11-14 10:39:21 +11002518 return superblock_has_perm(cred, mnt->mnt_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002519 FILESYSTEM__UNMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520}
2521
2522/* inode security operations */
2523
2524static int selinux_inode_alloc_security(struct inode *inode)
2525{
2526 return inode_alloc_security(inode);
2527}
2528
2529static void selinux_inode_free_security(struct inode *inode)
2530{
2531 inode_free_security(inode);
2532}
2533
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002534static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
Eric Paris2a7dba32011-02-01 11:05:39 -05002535 const struct qstr *qstr, char **name,
2536 void **value, size_t *len)
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002537{
Paul Moore5fb49872010-04-22 14:46:19 -04002538 const struct task_security_struct *tsec = current_security();
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002539 struct inode_security_struct *dsec;
2540 struct superblock_security_struct *sbsec;
David Howells275bb412008-11-14 10:39:19 +11002541 u32 sid, newsid, clen;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002542 int rc;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002543 char *namep = NULL, *context;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002544
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002545 dsec = dir->i_security;
2546 sbsec = dir->i_sb->s_security;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002547
David Howells275bb412008-11-14 10:39:19 +11002548 sid = tsec->sid;
2549 newsid = tsec->create_sid;
2550
Eric Paris415103f2010-12-02 16:13:40 -05002551 if ((sbsec->flags & SE_SBINITIALIZED) &&
2552 (sbsec->behavior == SECURITY_FS_USE_MNTPOINT))
2553 newsid = sbsec->mntpoint_sid;
Eric Paris12f348b2012-10-09 10:56:25 -04002554 else if (!newsid || !(sbsec->flags & SBLABEL_MNT)) {
David Howells275bb412008-11-14 10:39:19 +11002555 rc = security_transition_sid(sid, dsec->sid,
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002556 inode_mode_to_security_class(inode->i_mode),
Eric Paris652bb9b2011-02-01 11:05:40 -05002557 qstr, &newsid);
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002558 if (rc) {
2559 printk(KERN_WARNING "%s: "
2560 "security_transition_sid failed, rc=%d (dev=%s "
2561 "ino=%ld)\n",
Harvey Harrisondd6f9532008-03-06 10:03:59 +11002562 __func__,
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002563 -rc, inode->i_sb->s_id, inode->i_ino);
2564 return rc;
2565 }
2566 }
2567
Eric Paris296fddf2006-09-25 23:32:00 -07002568 /* Possibly defer initialization to selinux_complete_init. */
David P. Quigley0d90a7e2009-01-16 09:22:02 -05002569 if (sbsec->flags & SE_SBINITIALIZED) {
Eric Paris296fddf2006-09-25 23:32:00 -07002570 struct inode_security_struct *isec = inode->i_security;
2571 isec->sclass = inode_mode_to_security_class(inode->i_mode);
2572 isec->sid = newsid;
2573 isec->initialized = 1;
2574 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002575
Eric Paris12f348b2012-10-09 10:56:25 -04002576 if (!ss_initialized || !(sbsec->flags & SBLABEL_MNT))
Stephen Smalley25a74f32005-11-08 21:34:33 -08002577 return -EOPNOTSUPP;
2578
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002579 if (name) {
Josef Bacika02fe132008-04-04 09:35:05 +11002580 namep = kstrdup(XATTR_SELINUX_SUFFIX, GFP_NOFS);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002581 if (!namep)
2582 return -ENOMEM;
2583 *name = namep;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002584 }
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002585
2586 if (value && len) {
Stephen Smalley12b29f32008-05-07 13:03:20 -04002587 rc = security_sid_to_context_force(newsid, &context, &clen);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002588 if (rc) {
2589 kfree(namep);
2590 return rc;
2591 }
2592 *value = context;
2593 *len = clen;
2594 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002595
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002596 return 0;
2597}
2598
Al Viro4acdaf22011-07-26 01:42:34 -04002599static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600{
2601 return may_create(dir, dentry, SECCLASS_FILE);
2602}
2603
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2605{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 return may_link(dir, old_dentry, MAY_LINK);
2607}
2608
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
2610{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 return may_link(dir, dentry, MAY_UNLINK);
2612}
2613
2614static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
2615{
2616 return may_create(dir, dentry, SECCLASS_LNK_FILE);
2617}
2618
Al Viro18bb1db2011-07-26 01:41:39 -04002619static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620{
2621 return may_create(dir, dentry, SECCLASS_DIR);
2622}
2623
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
2625{
2626 return may_link(dir, dentry, MAY_RMDIR);
2627}
2628
Al Viro1a67aaf2011-07-26 01:52:52 -04002629static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 return may_create(dir, dentry, inode_mode_to_security_class(mode));
2632}
2633
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
Eric Paris828dfe12008-04-17 13:17:49 -04002635 struct inode *new_inode, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636{
2637 return may_rename(old_inode, old_dentry, new_inode, new_dentry);
2638}
2639
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640static int selinux_inode_readlink(struct dentry *dentry)
2641{
David Howells88e67f32008-11-14 10:39:21 +11002642 const struct cred *cred = current_cred();
2643
Eric Paris2875fa02011-04-28 16:04:24 -04002644 return dentry_has_perm(cred, dentry, FILE__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645}
2646
2647static int selinux_inode_follow_link(struct dentry *dentry, struct nameidata *nameidata)
2648{
David Howells88e67f32008-11-14 10:39:21 +11002649 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650
Eric Paris2875fa02011-04-28 16:04:24 -04002651 return dentry_has_perm(cred, dentry, FILE__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652}
2653
Eric Parisd4cf970d2012-04-04 15:01:42 -04002654static noinline int audit_inode_permission(struct inode *inode,
2655 u32 perms, u32 audited, u32 denied,
2656 unsigned flags)
2657{
2658 struct common_audit_data ad;
Eric Parisd4cf970d2012-04-04 15:01:42 -04002659 struct inode_security_struct *isec = inode->i_security;
2660 int rc;
2661
Eric Paris50c205f2012-04-04 15:01:43 -04002662 ad.type = LSM_AUDIT_DATA_INODE;
Eric Parisd4cf970d2012-04-04 15:01:42 -04002663 ad.u.inode = inode;
2664
2665 rc = slow_avc_audit(current_sid(), isec->sid, isec->sclass, perms,
2666 audited, denied, &ad, flags);
2667 if (rc)
2668 return rc;
2669 return 0;
2670}
2671
Al Viroe74f71e2011-06-20 19:38:15 -04002672static int selinux_inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673{
David Howells88e67f32008-11-14 10:39:21 +11002674 const struct cred *cred = current_cred();
Eric Parisb782e0a2010-07-23 11:44:03 -04002675 u32 perms;
2676 bool from_access;
Al Virocf1dd1d2011-06-20 19:44:08 -04002677 unsigned flags = mask & MAY_NOT_BLOCK;
Eric Paris2e334052012-04-04 15:01:42 -04002678 struct inode_security_struct *isec;
2679 u32 sid;
2680 struct av_decision avd;
2681 int rc, rc2;
2682 u32 audited, denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683
Eric Parisb782e0a2010-07-23 11:44:03 -04002684 from_access = mask & MAY_ACCESS;
Eric Parisd09ca732010-07-23 11:43:57 -04002685 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
2686
Eric Parisb782e0a2010-07-23 11:44:03 -04002687 /* No permission to check. Existence test. */
2688 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690
Eric Paris2e334052012-04-04 15:01:42 -04002691 validate_creds(cred);
Eric Parisb782e0a2010-07-23 11:44:03 -04002692
Eric Paris2e334052012-04-04 15:01:42 -04002693 if (unlikely(IS_PRIVATE(inode)))
2694 return 0;
Eric Parisb782e0a2010-07-23 11:44:03 -04002695
2696 perms = file_mask_to_av(inode->i_mode, mask);
2697
Eric Paris2e334052012-04-04 15:01:42 -04002698 sid = cred_sid(cred);
2699 isec = inode->i_security;
2700
2701 rc = avc_has_perm_noaudit(sid, isec->sid, isec->sclass, perms, 0, &avd);
2702 audited = avc_audit_required(perms, &avd, rc,
2703 from_access ? FILE__AUDIT_ACCESS : 0,
2704 &denied);
2705 if (likely(!audited))
2706 return rc;
2707
Eric Parisd4cf970d2012-04-04 15:01:42 -04002708 rc2 = audit_inode_permission(inode, perms, audited, denied, flags);
Eric Paris2e334052012-04-04 15:01:42 -04002709 if (rc2)
2710 return rc2;
2711 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712}
2713
2714static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
2715{
David Howells88e67f32008-11-14 10:39:21 +11002716 const struct cred *cred = current_cred();
Amerigo Wangbc6a6002009-08-20 19:29:02 -07002717 unsigned int ia_valid = iattr->ia_valid;
Eric Paris95dbf732012-04-04 13:45:34 -04002718 __u32 av = FILE__WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719
Amerigo Wangbc6a6002009-08-20 19:29:02 -07002720 /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
2721 if (ia_valid & ATTR_FORCE) {
2722 ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
2723 ATTR_FORCE);
2724 if (!ia_valid)
2725 return 0;
2726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727
Amerigo Wangbc6a6002009-08-20 19:29:02 -07002728 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
2729 ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
Eric Paris2875fa02011-04-28 16:04:24 -04002730 return dentry_has_perm(cred, dentry, FILE__SETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731
Eric Paris3d2195c2012-07-06 14:13:30 -04002732 if (selinux_policycap_openperm && (ia_valid & ATTR_SIZE))
Eric Paris95dbf732012-04-04 13:45:34 -04002733 av |= FILE__OPEN;
2734
2735 return dentry_has_perm(cred, dentry, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736}
2737
2738static int selinux_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
2739{
David Howells88e67f32008-11-14 10:39:21 +11002740 const struct cred *cred = current_cred();
Eric Paris2875fa02011-04-28 16:04:24 -04002741 struct path path;
David Howells88e67f32008-11-14 10:39:21 +11002742
Eric Paris2875fa02011-04-28 16:04:24 -04002743 path.dentry = dentry;
2744 path.mnt = mnt;
2745
2746 return path_has_perm(cred, &path, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747}
2748
David Howells8f0cfa52008-04-29 00:59:41 -07002749static int selinux_inode_setotherxattr(struct dentry *dentry, const char *name)
Serge E. Hallynb5376772007-10-16 23:31:36 -07002750{
David Howells88e67f32008-11-14 10:39:21 +11002751 const struct cred *cred = current_cred();
2752
Serge E. Hallynb5376772007-10-16 23:31:36 -07002753 if (!strncmp(name, XATTR_SECURITY_PREFIX,
2754 sizeof XATTR_SECURITY_PREFIX - 1)) {
2755 if (!strcmp(name, XATTR_NAME_CAPS)) {
2756 if (!capable(CAP_SETFCAP))
2757 return -EPERM;
2758 } else if (!capable(CAP_SYS_ADMIN)) {
2759 /* A different attribute in the security namespace.
2760 Restrict to administrator. */
2761 return -EPERM;
2762 }
2763 }
2764
2765 /* Not an attribute we recognize, so just check the
2766 ordinary setattr permission. */
Eric Paris2875fa02011-04-28 16:04:24 -04002767 return dentry_has_perm(cred, dentry, FILE__SETATTR);
Serge E. Hallynb5376772007-10-16 23:31:36 -07002768}
2769
David Howells8f0cfa52008-04-29 00:59:41 -07002770static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
2771 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 struct inode *inode = dentry->d_inode;
2774 struct inode_security_struct *isec = inode->i_security;
2775 struct superblock_security_struct *sbsec;
Thomas Liu2bf49692009-07-14 12:14:09 -04002776 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11002777 u32 newsid, sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 int rc = 0;
2779
Serge E. Hallynb5376772007-10-16 23:31:36 -07002780 if (strcmp(name, XATTR_NAME_SELINUX))
2781 return selinux_inode_setotherxattr(dentry, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782
2783 sbsec = inode->i_sb->s_security;
Eric Paris12f348b2012-10-09 10:56:25 -04002784 if (!(sbsec->flags & SBLABEL_MNT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 return -EOPNOTSUPP;
2786
Serge E. Hallyn2e149672011-03-23 16:43:26 -07002787 if (!inode_owner_or_capable(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 return -EPERM;
2789
Eric Paris50c205f2012-04-04 15:01:43 -04002790 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002791 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792
David Howells275bb412008-11-14 10:39:19 +11002793 rc = avc_has_perm(sid, isec->sid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 FILE__RELABELFROM, &ad);
2795 if (rc)
2796 return rc;
2797
2798 rc = security_context_to_sid(value, size, &newsid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04002799 if (rc == -EINVAL) {
Eric Parisd6ea83e2012-04-04 13:45:49 -04002800 if (!capable(CAP_MAC_ADMIN)) {
2801 struct audit_buffer *ab;
2802 size_t audit_size;
2803 const char *str;
2804
2805 /* We strip a nul only if it is at the end, otherwise the
2806 * context contains a nul and we should audit that */
Al Viroe3fea3f2012-06-09 08:15:16 +01002807 if (value) {
2808 str = value;
2809 if (str[size - 1] == '\0')
2810 audit_size = size - 1;
2811 else
2812 audit_size = size;
2813 } else {
2814 str = "";
2815 audit_size = 0;
2816 }
Eric Parisd6ea83e2012-04-04 13:45:49 -04002817 ab = audit_log_start(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR);
2818 audit_log_format(ab, "op=setxattr invalid_context=");
2819 audit_log_n_untrustedstring(ab, value, audit_size);
2820 audit_log_end(ab);
2821
Stephen Smalley12b29f32008-05-07 13:03:20 -04002822 return rc;
Eric Parisd6ea83e2012-04-04 13:45:49 -04002823 }
Stephen Smalley12b29f32008-05-07 13:03:20 -04002824 rc = security_context_to_sid_force(value, size, &newsid);
2825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 if (rc)
2827 return rc;
2828
David Howells275bb412008-11-14 10:39:19 +11002829 rc = avc_has_perm(sid, newsid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 FILE__RELABELTO, &ad);
2831 if (rc)
2832 return rc;
2833
David Howells275bb412008-11-14 10:39:19 +11002834 rc = security_validate_transition(isec->sid, newsid, sid,
Eric Paris828dfe12008-04-17 13:17:49 -04002835 isec->sclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 if (rc)
2837 return rc;
2838
2839 return avc_has_perm(newsid,
2840 sbsec->sid,
2841 SECCLASS_FILESYSTEM,
2842 FILESYSTEM__ASSOCIATE,
2843 &ad);
2844}
2845
David Howells8f0cfa52008-04-29 00:59:41 -07002846static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
Eric Parisf5269712008-05-14 11:27:45 -04002847 const void *value, size_t size,
David Howells8f0cfa52008-04-29 00:59:41 -07002848 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849{
2850 struct inode *inode = dentry->d_inode;
2851 struct inode_security_struct *isec = inode->i_security;
2852 u32 newsid;
2853 int rc;
2854
2855 if (strcmp(name, XATTR_NAME_SELINUX)) {
2856 /* Not an attribute we recognize, so nothing to do. */
2857 return;
2858 }
2859
Stephen Smalley12b29f32008-05-07 13:03:20 -04002860 rc = security_context_to_sid_force(value, size, &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 if (rc) {
Stephen Smalley12b29f32008-05-07 13:03:20 -04002862 printk(KERN_ERR "SELinux: unable to map context to SID"
2863 "for (%s, %lu), rc=%d\n",
2864 inode->i_sb->s_id, inode->i_ino, -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 return;
2866 }
2867
2868 isec->sid = newsid;
2869 return;
2870}
2871
David Howells8f0cfa52008-04-29 00:59:41 -07002872static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873{
David Howells88e67f32008-11-14 10:39:21 +11002874 const struct cred *cred = current_cred();
2875
Eric Paris2875fa02011-04-28 16:04:24 -04002876 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877}
2878
Eric Paris828dfe12008-04-17 13:17:49 -04002879static int selinux_inode_listxattr(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880{
David Howells88e67f32008-11-14 10:39:21 +11002881 const struct cred *cred = current_cred();
2882
Eric Paris2875fa02011-04-28 16:04:24 -04002883 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884}
2885
David Howells8f0cfa52008-04-29 00:59:41 -07002886static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887{
Serge E. Hallynb5376772007-10-16 23:31:36 -07002888 if (strcmp(name, XATTR_NAME_SELINUX))
2889 return selinux_inode_setotherxattr(dentry, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890
2891 /* No one is allowed to remove a SELinux security label.
2892 You can change the label, but all data must be labeled. */
2893 return -EACCES;
2894}
2895
James Morrisd381d8a2005-10-30 14:59:22 -08002896/*
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04002897 * Copy the inode security context value to the user.
James Morrisd381d8a2005-10-30 14:59:22 -08002898 *
2899 * Permission check is handled by selinux_inode_getxattr hook.
2900 */
David P. Quigley42492592008-02-04 22:29:39 -08002901static int selinux_inode_getsecurity(const struct inode *inode, const char *name, void **buffer, bool alloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902{
David P. Quigley42492592008-02-04 22:29:39 -08002903 u32 size;
2904 int error;
2905 char *context = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 struct inode_security_struct *isec = inode->i_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00002908 if (strcmp(name, XATTR_SELINUX_SUFFIX))
2909 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04002911 /*
2912 * If the caller has CAP_MAC_ADMIN, then get the raw context
2913 * value even if it is not defined by current policy; otherwise,
2914 * use the in-core value under current policy.
2915 * Use the non-auditing forms of the permission checks since
2916 * getxattr may be called by unprivileged processes commonly
2917 * and lack of permission just means that we fall back to the
2918 * in-core context value, not a denial.
2919 */
Eric Paris6a9de492012-01-03 12:25:14 -05002920 error = selinux_capable(current_cred(), &init_user_ns, CAP_MAC_ADMIN,
David Howells3699c532009-01-06 22:27:01 +00002921 SECURITY_CAP_NOAUDIT);
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04002922 if (!error)
2923 error = security_sid_to_context_force(isec->sid, &context,
2924 &size);
2925 else
2926 error = security_sid_to_context(isec->sid, &context, &size);
David P. Quigley42492592008-02-04 22:29:39 -08002927 if (error)
2928 return error;
2929 error = size;
2930 if (alloc) {
2931 *buffer = context;
2932 goto out_nofree;
2933 }
2934 kfree(context);
2935out_nofree:
2936 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937}
2938
2939static int selinux_inode_setsecurity(struct inode *inode, const char *name,
Eric Paris828dfe12008-04-17 13:17:49 -04002940 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941{
2942 struct inode_security_struct *isec = inode->i_security;
2943 u32 newsid;
2944 int rc;
2945
2946 if (strcmp(name, XATTR_SELINUX_SUFFIX))
2947 return -EOPNOTSUPP;
2948
2949 if (!value || !size)
2950 return -EACCES;
2951
Eric Paris828dfe12008-04-17 13:17:49 -04002952 rc = security_context_to_sid((void *)value, size, &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 if (rc)
2954 return rc;
2955
2956 isec->sid = newsid;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04002957 isec->initialized = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 return 0;
2959}
2960
2961static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
2962{
2963 const int len = sizeof(XATTR_NAME_SELINUX);
2964 if (buffer && len <= buffer_size)
2965 memcpy(buffer, XATTR_NAME_SELINUX, len);
2966 return len;
2967}
2968
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02002969static void selinux_inode_getsecid(const struct inode *inode, u32 *secid)
2970{
2971 struct inode_security_struct *isec = inode->i_security;
2972 *secid = isec->sid;
2973}
2974
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975/* file security operations */
2976
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09002977static int selinux_revalidate_file_permission(struct file *file, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978{
David Howells88e67f32008-11-14 10:39:21 +11002979 const struct cred *cred = current_cred();
Al Viro496ad9a2013-01-23 17:07:38 -05002980 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
2983 if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
2984 mask |= MAY_APPEND;
2985
Paul Moore389fb8002009-03-27 17:10:34 -04002986 return file_has_perm(cred, file,
2987 file_mask_to_av(inode->i_mode, mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988}
2989
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09002990static int selinux_file_permission(struct file *file, int mask)
2991{
Al Viro496ad9a2013-01-23 17:07:38 -05002992 struct inode *inode = file_inode(file);
Stephen Smalley20dda182009-06-22 14:54:53 -04002993 struct file_security_struct *fsec = file->f_security;
2994 struct inode_security_struct *isec = inode->i_security;
2995 u32 sid = current_sid();
2996
Paul Moore389fb8002009-03-27 17:10:34 -04002997 if (!mask)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09002998 /* No permission to check. Existence test. */
2999 return 0;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003000
Stephen Smalley20dda182009-06-22 14:54:53 -04003001 if (sid == fsec->sid && fsec->isid == isec->sid &&
3002 fsec->pseqno == avc_policy_seqno())
Eric Paris83d49852012-04-04 13:45:40 -04003003 /* No change since file_open check. */
Stephen Smalley20dda182009-06-22 14:54:53 -04003004 return 0;
3005
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003006 return selinux_revalidate_file_permission(file, mask);
3007}
3008
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009static int selinux_file_alloc_security(struct file *file)
3010{
3011 return file_alloc_security(file);
3012}
3013
3014static void selinux_file_free_security(struct file *file)
3015{
3016 file_free_security(file);
3017}
3018
3019static int selinux_file_ioctl(struct file *file, unsigned int cmd,
3020 unsigned long arg)
3021{
David Howells88e67f32008-11-14 10:39:21 +11003022 const struct cred *cred = current_cred();
Eric Paris0b24dcb2011-02-25 15:39:20 -05003023 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024
Eric Paris0b24dcb2011-02-25 15:39:20 -05003025 switch (cmd) {
3026 case FIONREAD:
3027 /* fall through */
3028 case FIBMAP:
3029 /* fall through */
3030 case FIGETBSZ:
3031 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003032 case FS_IOC_GETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003033 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003034 case FS_IOC_GETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003035 error = file_has_perm(cred, file, FILE__GETATTR);
3036 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037
Al Viro2f99c362012-03-23 16:04:05 -04003038 case FS_IOC_SETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003039 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003040 case FS_IOC_SETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003041 error = file_has_perm(cred, file, FILE__SETATTR);
3042 break;
3043
3044 /* sys_ioctl() checks */
3045 case FIONBIO:
3046 /* fall through */
3047 case FIOASYNC:
3048 error = file_has_perm(cred, file, 0);
3049 break;
3050
3051 case KDSKBENT:
3052 case KDSKBSENT:
Eric Paris6a9de492012-01-03 12:25:14 -05003053 error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
3054 SECURITY_CAP_AUDIT);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003055 break;
3056
3057 /* default case assumes that the command will go
3058 * to the file's ioctl() function.
3059 */
3060 default:
3061 error = file_has_perm(cred, file, FILE__IOCTL);
3062 }
3063 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064}
3065
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003066static int default_noexec;
3067
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
3069{
David Howells88e67f32008-11-14 10:39:21 +11003070 const struct cred *cred = current_cred();
David Howellsd84f4f92008-11-14 10:39:23 +11003071 int rc = 0;
David Howells88e67f32008-11-14 10:39:21 +11003072
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003073 if (default_noexec &&
3074 (prot & PROT_EXEC) && (!file || (!shared && (prot & PROT_WRITE)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 /*
3076 * We are making executable an anonymous mapping or a
3077 * private file mapping that will also be writable.
3078 * This has an additional check.
3079 */
David Howellsd84f4f92008-11-14 10:39:23 +11003080 rc = cred_has_perm(cred, cred, PROCESS__EXECMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 if (rc)
David Howellsd84f4f92008-11-14 10:39:23 +11003082 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084
3085 if (file) {
3086 /* read access is always possible with a mapping */
3087 u32 av = FILE__READ;
3088
3089 /* write access only matters if the mapping is shared */
3090 if (shared && (prot & PROT_WRITE))
3091 av |= FILE__WRITE;
3092
3093 if (prot & PROT_EXEC)
3094 av |= FILE__EXECUTE;
3095
David Howells88e67f32008-11-14 10:39:21 +11003096 return file_has_perm(cred, file, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 }
David Howellsd84f4f92008-11-14 10:39:23 +11003098
3099error:
3100 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101}
3102
Al Viroe5467852012-05-30 13:30:51 -04003103static int selinux_mmap_addr(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104{
Eric Parised032182007-06-28 15:55:21 -04003105 int rc = 0;
David Howells275bb412008-11-14 10:39:19 +11003106 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107
Eric Paris84336d1a2009-07-31 12:54:05 -04003108 /*
3109 * notice that we are intentionally putting the SELinux check before
3110 * the secondary cap_file_mmap check. This is such a likely attempt
3111 * at bad behaviour/exploit that we always want to get the AVC, even
3112 * if DAC would have also denied the operation.
3113 */
Eric Parisa2551df72009-07-31 12:54:11 -04003114 if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
Eric Parised032182007-06-28 15:55:21 -04003115 rc = avc_has_perm(sid, sid, SECCLASS_MEMPROTECT,
3116 MEMPROTECT__MMAP_ZERO, NULL);
Eric Paris84336d1a2009-07-31 12:54:05 -04003117 if (rc)
3118 return rc;
3119 }
3120
3121 /* do DAC check on address space usage */
Al Viroe5467852012-05-30 13:30:51 -04003122 return cap_mmap_addr(addr);
3123}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124
Al Viroe5467852012-05-30 13:30:51 -04003125static int selinux_mmap_file(struct file *file, unsigned long reqprot,
3126 unsigned long prot, unsigned long flags)
3127{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 if (selinux_checkreqprot)
3129 prot = reqprot;
3130
3131 return file_map_prot_check(file, prot,
3132 (flags & MAP_TYPE) == MAP_SHARED);
3133}
3134
3135static int selinux_file_mprotect(struct vm_area_struct *vma,
3136 unsigned long reqprot,
3137 unsigned long prot)
3138{
David Howells88e67f32008-11-14 10:39:21 +11003139 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140
3141 if (selinux_checkreqprot)
3142 prot = reqprot;
3143
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003144 if (default_noexec &&
3145 (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
James Morrisd541bbe2009-01-29 12:19:51 +11003146 int rc = 0;
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003147 if (vma->vm_start >= vma->vm_mm->start_brk &&
3148 vma->vm_end <= vma->vm_mm->brk) {
David Howellsd84f4f92008-11-14 10:39:23 +11003149 rc = cred_has_perm(cred, cred, PROCESS__EXECHEAP);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003150 } else if (!vma->vm_file &&
3151 vma->vm_start <= vma->vm_mm->start_stack &&
3152 vma->vm_end >= vma->vm_mm->start_stack) {
David Howells3b11a1d2008-11-14 10:39:26 +11003153 rc = current_has_perm(current, PROCESS__EXECSTACK);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003154 } else if (vma->vm_file && vma->anon_vma) {
3155 /*
3156 * We are making executable a file mapping that has
3157 * had some COW done. Since pages might have been
3158 * written, check ability to execute the possibly
3159 * modified content. This typically should only
3160 * occur for text relocations.
3161 */
David Howellsd84f4f92008-11-14 10:39:23 +11003162 rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003163 }
Lorenzo Hernandez García-Hierro6b992192005-06-25 14:54:34 -07003164 if (rc)
3165 return rc;
3166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167
3168 return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
3169}
3170
3171static int selinux_file_lock(struct file *file, unsigned int cmd)
3172{
David Howells88e67f32008-11-14 10:39:21 +11003173 const struct cred *cred = current_cred();
3174
3175 return file_has_perm(cred, file, FILE__LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176}
3177
3178static int selinux_file_fcntl(struct file *file, unsigned int cmd,
3179 unsigned long arg)
3180{
David Howells88e67f32008-11-14 10:39:21 +11003181 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 int err = 0;
3183
3184 switch (cmd) {
Eric Paris828dfe12008-04-17 13:17:49 -04003185 case F_SETFL:
Eric Paris828dfe12008-04-17 13:17:49 -04003186 if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
David Howells88e67f32008-11-14 10:39:21 +11003187 err = file_has_perm(cred, file, FILE__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003189 }
3190 /* fall through */
3191 case F_SETOWN:
3192 case F_SETSIG:
3193 case F_GETFL:
3194 case F_GETOWN:
3195 case F_GETSIG:
Cyrill Gorcunov1d151c32012-07-30 14:43:00 -07003196 case F_GETOWNER_UIDS:
Eric Paris828dfe12008-04-17 13:17:49 -04003197 /* Just check FD__USE permission */
David Howells88e67f32008-11-14 10:39:21 +11003198 err = file_has_perm(cred, file, 0);
Eric Paris828dfe12008-04-17 13:17:49 -04003199 break;
3200 case F_GETLK:
3201 case F_SETLK:
3202 case F_SETLKW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203#if BITS_PER_LONG == 32
Eric Paris828dfe12008-04-17 13:17:49 -04003204 case F_GETLK64:
3205 case F_SETLK64:
3206 case F_SETLKW64:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207#endif
David Howells88e67f32008-11-14 10:39:21 +11003208 err = file_has_perm(cred, file, FILE__LOCK);
Eric Paris828dfe12008-04-17 13:17:49 -04003209 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 }
3211
3212 return err;
3213}
3214
3215static int selinux_file_set_fowner(struct file *file)
3216{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 struct file_security_struct *fsec;
3218
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 fsec = file->f_security;
David Howells275bb412008-11-14 10:39:19 +11003220 fsec->fown_sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221
3222 return 0;
3223}
3224
3225static int selinux_file_send_sigiotask(struct task_struct *tsk,
3226 struct fown_struct *fown, int signum)
3227{
Eric Paris828dfe12008-04-17 13:17:49 -04003228 struct file *file;
Stephen Smalley65c90bc2009-05-04 15:43:18 -04003229 u32 sid = task_sid(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 struct file_security_struct *fsec;
3232
3233 /* struct fown_struct is never outside the context of a struct file */
Eric Paris828dfe12008-04-17 13:17:49 -04003234 file = container_of(fown, struct file, f_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 fsec = file->f_security;
3237
3238 if (!signum)
3239 perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
3240 else
3241 perm = signal_to_av(signum);
3242
David Howells275bb412008-11-14 10:39:19 +11003243 return avc_has_perm(fsec->fown_sid, sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 SECCLASS_PROCESS, perm, NULL);
3245}
3246
3247static int selinux_file_receive(struct file *file)
3248{
David Howells88e67f32008-11-14 10:39:21 +11003249 const struct cred *cred = current_cred();
3250
3251 return file_has_perm(cred, file, file_to_av(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252}
3253
Eric Paris83d49852012-04-04 13:45:40 -04003254static int selinux_file_open(struct file *file, const struct cred *cred)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003255{
3256 struct file_security_struct *fsec;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003257 struct inode_security_struct *isec;
David Howellsd84f4f92008-11-14 10:39:23 +11003258
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003259 fsec = file->f_security;
Al Viro496ad9a2013-01-23 17:07:38 -05003260 isec = file_inode(file)->i_security;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003261 /*
3262 * Save inode label and policy sequence number
3263 * at open-time so that selinux_file_permission
3264 * can determine whether revalidation is necessary.
3265 * Task label is already saved in the file security
3266 * struct as its SID.
3267 */
3268 fsec->isid = isec->sid;
3269 fsec->pseqno = avc_policy_seqno();
3270 /*
3271 * Since the inode label or policy seqno may have changed
3272 * between the selinux_inode_permission check and the saving
3273 * of state above, recheck that access is still permitted.
3274 * Otherwise, access might never be revalidated against the
3275 * new inode label or new policy.
3276 * This check is not redundant - do not remove.
3277 */
Eric Paris602a8dd2012-04-04 15:01:42 -04003278 return path_has_perm(cred, &file->f_path, open_file_to_av(file));
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003279}
3280
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281/* task security operations */
3282
3283static int selinux_task_create(unsigned long clone_flags)
3284{
David Howells3b11a1d2008-11-14 10:39:26 +11003285 return current_has_perm(current, PROCESS__FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286}
3287
David Howellsf1752ee2008-11-14 10:39:17 +11003288/*
David Howellsee18d642009-09-02 09:14:21 +01003289 * allocate the SELinux part of blank credentials
3290 */
3291static int selinux_cred_alloc_blank(struct cred *cred, gfp_t gfp)
3292{
3293 struct task_security_struct *tsec;
3294
3295 tsec = kzalloc(sizeof(struct task_security_struct), gfp);
3296 if (!tsec)
3297 return -ENOMEM;
3298
3299 cred->security = tsec;
3300 return 0;
3301}
3302
3303/*
David Howellsf1752ee2008-11-14 10:39:17 +11003304 * detach and free the LSM part of a set of credentials
3305 */
3306static void selinux_cred_free(struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307{
David Howellsf1752ee2008-11-14 10:39:17 +11003308 struct task_security_struct *tsec = cred->security;
David Howellse0e81732009-09-02 09:13:40 +01003309
Tetsuo Handa2edeaa32011-02-07 13:36:10 +00003310 /*
3311 * cred->security == NULL if security_cred_alloc_blank() or
3312 * security_prepare_creds() returned an error.
3313 */
3314 BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE);
David Howellse0e81732009-09-02 09:13:40 +01003315 cred->security = (void *) 0x7UL;
David Howellsf1752ee2008-11-14 10:39:17 +11003316 kfree(tsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317}
3318
David Howellsd84f4f92008-11-14 10:39:23 +11003319/*
3320 * prepare a new set of credentials for modification
3321 */
3322static int selinux_cred_prepare(struct cred *new, const struct cred *old,
3323 gfp_t gfp)
3324{
3325 const struct task_security_struct *old_tsec;
3326 struct task_security_struct *tsec;
3327
3328 old_tsec = old->security;
3329
3330 tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp);
3331 if (!tsec)
3332 return -ENOMEM;
3333
3334 new->security = tsec;
3335 return 0;
3336}
3337
3338/*
David Howellsee18d642009-09-02 09:14:21 +01003339 * transfer the SELinux data to a blank set of creds
3340 */
3341static void selinux_cred_transfer(struct cred *new, const struct cred *old)
3342{
3343 const struct task_security_struct *old_tsec = old->security;
3344 struct task_security_struct *tsec = new->security;
3345
3346 *tsec = *old_tsec;
3347}
3348
3349/*
David Howells3a3b7ce2008-11-14 10:39:28 +11003350 * set the security data for a kernel service
3351 * - all the creation contexts are set to unlabelled
3352 */
3353static int selinux_kernel_act_as(struct cred *new, u32 secid)
3354{
3355 struct task_security_struct *tsec = new->security;
3356 u32 sid = current_sid();
3357 int ret;
3358
3359 ret = avc_has_perm(sid, secid,
3360 SECCLASS_KERNEL_SERVICE,
3361 KERNEL_SERVICE__USE_AS_OVERRIDE,
3362 NULL);
3363 if (ret == 0) {
3364 tsec->sid = secid;
3365 tsec->create_sid = 0;
3366 tsec->keycreate_sid = 0;
3367 tsec->sockcreate_sid = 0;
3368 }
3369 return ret;
3370}
3371
3372/*
3373 * set the file creation context in a security record to the same as the
3374 * objective context of the specified inode
3375 */
3376static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
3377{
3378 struct inode_security_struct *isec = inode->i_security;
3379 struct task_security_struct *tsec = new->security;
3380 u32 sid = current_sid();
3381 int ret;
3382
3383 ret = avc_has_perm(sid, isec->sid,
3384 SECCLASS_KERNEL_SERVICE,
3385 KERNEL_SERVICE__CREATE_FILES_AS,
3386 NULL);
3387
3388 if (ret == 0)
3389 tsec->create_sid = isec->sid;
David Howellsef574712010-02-26 01:56:16 +00003390 return ret;
David Howells3a3b7ce2008-11-14 10:39:28 +11003391}
3392
Eric Parisdd8dbf22009-11-03 16:35:32 +11003393static int selinux_kernel_module_request(char *kmod_name)
Eric Paris25354c42009-08-13 09:45:03 -04003394{
Eric Parisdd8dbf22009-11-03 16:35:32 +11003395 u32 sid;
3396 struct common_audit_data ad;
3397
3398 sid = task_sid(current);
3399
Eric Paris50c205f2012-04-04 15:01:43 -04003400 ad.type = LSM_AUDIT_DATA_KMOD;
Eric Parisdd8dbf22009-11-03 16:35:32 +11003401 ad.u.kmod_name = kmod_name;
3402
3403 return avc_has_perm(sid, SECINITSID_KERNEL, SECCLASS_SYSTEM,
3404 SYSTEM__MODULE_REQUEST, &ad);
Eric Paris25354c42009-08-13 09:45:03 -04003405}
3406
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
3408{
David Howells3b11a1d2008-11-14 10:39:26 +11003409 return current_has_perm(p, PROCESS__SETPGID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410}
3411
3412static int selinux_task_getpgid(struct task_struct *p)
3413{
David Howells3b11a1d2008-11-14 10:39:26 +11003414 return current_has_perm(p, PROCESS__GETPGID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415}
3416
3417static int selinux_task_getsid(struct task_struct *p)
3418{
David Howells3b11a1d2008-11-14 10:39:26 +11003419 return current_has_perm(p, PROCESS__GETSESSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420}
3421
David Quigleyf9008e4c2006-06-30 01:55:46 -07003422static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
3423{
David Howells275bb412008-11-14 10:39:19 +11003424 *secid = task_sid(p);
David Quigleyf9008e4c2006-06-30 01:55:46 -07003425}
3426
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427static int selinux_task_setnice(struct task_struct *p, int nice)
3428{
3429 int rc;
3430
Eric Paris200ac532009-02-12 15:01:04 -05003431 rc = cap_task_setnice(p, nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 if (rc)
3433 return rc;
3434
David Howells3b11a1d2008-11-14 10:39:26 +11003435 return current_has_perm(p, PROCESS__SETSCHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436}
3437
James Morris03e68062006-06-23 02:03:58 -07003438static int selinux_task_setioprio(struct task_struct *p, int ioprio)
3439{
Serge E. Hallynb5376772007-10-16 23:31:36 -07003440 int rc;
3441
Eric Paris200ac532009-02-12 15:01:04 -05003442 rc = cap_task_setioprio(p, ioprio);
Serge E. Hallynb5376772007-10-16 23:31:36 -07003443 if (rc)
3444 return rc;
3445
David Howells3b11a1d2008-11-14 10:39:26 +11003446 return current_has_perm(p, PROCESS__SETSCHED);
James Morris03e68062006-06-23 02:03:58 -07003447}
3448
David Quigleya1836a42006-06-30 01:55:49 -07003449static int selinux_task_getioprio(struct task_struct *p)
3450{
David Howells3b11a1d2008-11-14 10:39:26 +11003451 return current_has_perm(p, PROCESS__GETSCHED);
David Quigleya1836a42006-06-30 01:55:49 -07003452}
3453
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003454static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
3455 struct rlimit *new_rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456{
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003457 struct rlimit *old_rlim = p->signal->rlim + resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458
3459 /* Control the ability to change the hard limit (whether
3460 lowering or raising it), so that the hard limit can
3461 later be used as a safe reset point for the soft limit
David Howellsd84f4f92008-11-14 10:39:23 +11003462 upon context transitions. See selinux_bprm_committing_creds. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 if (old_rlim->rlim_max != new_rlim->rlim_max)
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003464 return current_has_perm(p, PROCESS__SETRLIMIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465
3466 return 0;
3467}
3468
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09003469static int selinux_task_setscheduler(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470{
Serge E. Hallynb5376772007-10-16 23:31:36 -07003471 int rc;
3472
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09003473 rc = cap_task_setscheduler(p);
Serge E. Hallynb5376772007-10-16 23:31:36 -07003474 if (rc)
3475 return rc;
3476
David Howells3b11a1d2008-11-14 10:39:26 +11003477 return current_has_perm(p, PROCESS__SETSCHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478}
3479
3480static int selinux_task_getscheduler(struct task_struct *p)
3481{
David Howells3b11a1d2008-11-14 10:39:26 +11003482 return current_has_perm(p, PROCESS__GETSCHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483}
3484
David Quigley35601542006-06-23 02:04:01 -07003485static int selinux_task_movememory(struct task_struct *p)
3486{
David Howells3b11a1d2008-11-14 10:39:26 +11003487 return current_has_perm(p, PROCESS__SETSCHED);
David Quigley35601542006-06-23 02:04:01 -07003488}
3489
David Quigleyf9008e4c2006-06-30 01:55:46 -07003490static int selinux_task_kill(struct task_struct *p, struct siginfo *info,
3491 int sig, u32 secid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492{
3493 u32 perm;
3494 int rc;
3495
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 if (!sig)
3497 perm = PROCESS__SIGNULL; /* null signal; existence test */
3498 else
3499 perm = signal_to_av(sig);
David Quigleyf9008e4c2006-06-30 01:55:46 -07003500 if (secid)
David Howells275bb412008-11-14 10:39:19 +11003501 rc = avc_has_perm(secid, task_sid(p),
3502 SECCLASS_PROCESS, perm, NULL);
David Quigleyf9008e4c2006-06-30 01:55:46 -07003503 else
David Howells3b11a1d2008-11-14 10:39:26 +11003504 rc = current_has_perm(p, perm);
David Quigleyf9008e4c2006-06-30 01:55:46 -07003505 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506}
3507
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508static int selinux_task_wait(struct task_struct *p)
3509{
Eric Paris8a535142007-10-22 16:10:31 -04003510 return task_has_perm(p, current, PROCESS__SIGCHLD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511}
3512
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513static void selinux_task_to_inode(struct task_struct *p,
3514 struct inode *inode)
3515{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 struct inode_security_struct *isec = inode->i_security;
David Howells275bb412008-11-14 10:39:19 +11003517 u32 sid = task_sid(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518
David Howells275bb412008-11-14 10:39:19 +11003519 isec->sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 isec->initialized = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521}
3522
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003524static int selinux_parse_skb_ipv4(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04003525 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526{
3527 int offset, ihlen, ret = -EINVAL;
3528 struct iphdr _iph, *ih;
3529
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03003530 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
3532 if (ih == NULL)
3533 goto out;
3534
3535 ihlen = ih->ihl * 4;
3536 if (ihlen < sizeof(_iph))
3537 goto out;
3538
Eric Paris48c62af2012-04-02 13:15:44 -04003539 ad->u.net->v4info.saddr = ih->saddr;
3540 ad->u.net->v4info.daddr = ih->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 ret = 0;
3542
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003543 if (proto)
3544 *proto = ih->protocol;
3545
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 switch (ih->protocol) {
Eric Paris828dfe12008-04-17 13:17:49 -04003547 case IPPROTO_TCP: {
3548 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549
Eric Paris828dfe12008-04-17 13:17:49 -04003550 if (ntohs(ih->frag_off) & IP_OFFSET)
3551 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552
3553 offset += ihlen;
3554 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3555 if (th == NULL)
3556 break;
3557
Eric Paris48c62af2012-04-02 13:15:44 -04003558 ad->u.net->sport = th->source;
3559 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562
Eric Paris828dfe12008-04-17 13:17:49 -04003563 case IPPROTO_UDP: {
3564 struct udphdr _udph, *uh;
3565
3566 if (ntohs(ih->frag_off) & IP_OFFSET)
3567 break;
3568
3569 offset += ihlen;
3570 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3571 if (uh == NULL)
3572 break;
3573
Eric Paris48c62af2012-04-02 13:15:44 -04003574 ad->u.net->sport = uh->source;
3575 ad->u.net->dport = uh->dest;
Eric Paris828dfe12008-04-17 13:17:49 -04003576 break;
3577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578
James Morris2ee92d42006-11-13 16:09:01 -08003579 case IPPROTO_DCCP: {
3580 struct dccp_hdr _dccph, *dh;
3581
3582 if (ntohs(ih->frag_off) & IP_OFFSET)
3583 break;
3584
3585 offset += ihlen;
3586 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3587 if (dh == NULL)
3588 break;
3589
Eric Paris48c62af2012-04-02 13:15:44 -04003590 ad->u.net->sport = dh->dccph_sport;
3591 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08003592 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003593 }
James Morris2ee92d42006-11-13 16:09:01 -08003594
Eric Paris828dfe12008-04-17 13:17:49 -04003595 default:
3596 break;
3597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598out:
3599 return ret;
3600}
3601
3602#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3603
3604/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003605static int selinux_parse_skb_ipv6(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04003606 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607{
3608 u8 nexthdr;
3609 int ret = -EINVAL, offset;
3610 struct ipv6hdr _ipv6h, *ip6;
Jesse Gross75f28112011-11-30 17:05:51 -08003611 __be16 frag_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03003613 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3615 if (ip6 == NULL)
3616 goto out;
3617
Eric Paris48c62af2012-04-02 13:15:44 -04003618 ad->u.net->v6info.saddr = ip6->saddr;
3619 ad->u.net->v6info.daddr = ip6->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 ret = 0;
3621
3622 nexthdr = ip6->nexthdr;
3623 offset += sizeof(_ipv6h);
Jesse Gross75f28112011-11-30 17:05:51 -08003624 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 if (offset < 0)
3626 goto out;
3627
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003628 if (proto)
3629 *proto = nexthdr;
3630
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 switch (nexthdr) {
3632 case IPPROTO_TCP: {
Eric Paris828dfe12008-04-17 13:17:49 -04003633 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634
3635 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3636 if (th == NULL)
3637 break;
3638
Eric Paris48c62af2012-04-02 13:15:44 -04003639 ad->u.net->sport = th->source;
3640 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 break;
3642 }
3643
3644 case IPPROTO_UDP: {
3645 struct udphdr _udph, *uh;
3646
3647 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3648 if (uh == NULL)
3649 break;
3650
Eric Paris48c62af2012-04-02 13:15:44 -04003651 ad->u.net->sport = uh->source;
3652 ad->u.net->dport = uh->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 break;
3654 }
3655
James Morris2ee92d42006-11-13 16:09:01 -08003656 case IPPROTO_DCCP: {
3657 struct dccp_hdr _dccph, *dh;
3658
3659 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3660 if (dh == NULL)
3661 break;
3662
Eric Paris48c62af2012-04-02 13:15:44 -04003663 ad->u.net->sport = dh->dccph_sport;
3664 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08003665 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003666 }
James Morris2ee92d42006-11-13 16:09:01 -08003667
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 /* includes fragments */
3669 default:
3670 break;
3671 }
3672out:
3673 return ret;
3674}
3675
3676#endif /* IPV6 */
3677
Thomas Liu2bf49692009-07-14 12:14:09 -04003678static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
David Howellscf9481e2008-07-27 21:31:07 +10003679 char **_addrp, int src, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680{
David Howellscf9481e2008-07-27 21:31:07 +10003681 char *addrp;
3682 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683
Eric Paris48c62af2012-04-02 13:15:44 -04003684 switch (ad->u.net->family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 case PF_INET:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003686 ret = selinux_parse_skb_ipv4(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10003687 if (ret)
3688 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04003689 addrp = (char *)(src ? &ad->u.net->v4info.saddr :
3690 &ad->u.net->v4info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10003691 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692
3693#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3694 case PF_INET6:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003695 ret = selinux_parse_skb_ipv6(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10003696 if (ret)
3697 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04003698 addrp = (char *)(src ? &ad->u.net->v6info.saddr :
3699 &ad->u.net->v6info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10003700 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701#endif /* IPV6 */
3702 default:
David Howellscf9481e2008-07-27 21:31:07 +10003703 addrp = NULL;
3704 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 }
3706
David Howellscf9481e2008-07-27 21:31:07 +10003707parse_error:
3708 printk(KERN_WARNING
3709 "SELinux: failure in selinux_parse_skb(),"
3710 " unable to parse packet\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 return ret;
David Howellscf9481e2008-07-27 21:31:07 +10003712
3713okay:
3714 if (_addrp)
3715 *_addrp = addrp;
3716 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717}
3718
Paul Moore4f6a9932007-03-01 14:35:22 -05003719/**
Paul Moore220deb92008-01-29 08:38:23 -05003720 * selinux_skb_peerlbl_sid - Determine the peer label of a packet
Paul Moore4f6a9932007-03-01 14:35:22 -05003721 * @skb: the packet
Paul Moore75e22912008-01-29 08:38:04 -05003722 * @family: protocol family
Paul Moore220deb92008-01-29 08:38:23 -05003723 * @sid: the packet's peer label SID
Paul Moore4f6a9932007-03-01 14:35:22 -05003724 *
3725 * Description:
Paul Moore220deb92008-01-29 08:38:23 -05003726 * Check the various different forms of network peer labeling and determine
3727 * the peer label/SID for the packet; most of the magic actually occurs in
3728 * the security server function security_net_peersid_cmp(). The function
3729 * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
3730 * or -EACCES if @sid is invalid due to inconsistencies with the different
3731 * peer labels.
Paul Moore4f6a9932007-03-01 14:35:22 -05003732 *
3733 */
Paul Moore220deb92008-01-29 08:38:23 -05003734static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
Paul Moore4f6a9932007-03-01 14:35:22 -05003735{
Paul Moore71f1cb02008-01-29 08:51:16 -05003736 int err;
Paul Moore4f6a9932007-03-01 14:35:22 -05003737 u32 xfrm_sid;
3738 u32 nlbl_sid;
Paul Moore220deb92008-01-29 08:38:23 -05003739 u32 nlbl_type;
Paul Moore4f6a9932007-03-01 14:35:22 -05003740
Paul Moorebed4d7e2013-07-23 17:38:40 -04003741 err = selinux_skb_xfrm_sid(skb, &xfrm_sid);
3742 if (unlikely(err))
3743 return -EACCES;
3744 err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
3745 if (unlikely(err))
3746 return -EACCES;
Paul Moore220deb92008-01-29 08:38:23 -05003747
Paul Moore71f1cb02008-01-29 08:51:16 -05003748 err = security_net_peersid_resolve(nlbl_sid, nlbl_type, xfrm_sid, sid);
3749 if (unlikely(err)) {
3750 printk(KERN_WARNING
3751 "SELinux: failure in selinux_skb_peerlbl_sid(),"
3752 " unable to determine packet's peer label\n");
Paul Moore220deb92008-01-29 08:38:23 -05003753 return -EACCES;
Paul Moore71f1cb02008-01-29 08:51:16 -05003754 }
Paul Moore220deb92008-01-29 08:38:23 -05003755
3756 return 0;
Paul Moore4f6a9932007-03-01 14:35:22 -05003757}
3758
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759/* socket security operations */
Paul Moored4f2d972010-04-22 14:46:18 -04003760
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003761static int socket_sockcreate_sid(const struct task_security_struct *tsec,
3762 u16 secclass, u32 *socksid)
Paul Moored4f2d972010-04-22 14:46:18 -04003763{
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003764 if (tsec->sockcreate_sid > SECSID_NULL) {
3765 *socksid = tsec->sockcreate_sid;
3766 return 0;
3767 }
3768
3769 return security_transition_sid(tsec->sid, tsec->sid, secclass, NULL,
3770 socksid);
Paul Moored4f2d972010-04-22 14:46:18 -04003771}
3772
Paul Moore253bfae2010-04-22 14:46:19 -04003773static int sock_has_perm(struct task_struct *task, struct sock *sk, u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774{
Paul Moore253bfae2010-04-22 14:46:19 -04003775 struct sk_security_struct *sksec = sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04003776 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04003777 struct lsm_network_audit net = {0,};
Paul Moore253bfae2010-04-22 14:46:19 -04003778 u32 tsid = task_sid(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779
Paul Moore253bfae2010-04-22 14:46:19 -04003780 if (sksec->sid == SECINITSID_KERNEL)
3781 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782
Eric Paris50c205f2012-04-04 15:01:43 -04003783 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04003784 ad.u.net = &net;
3785 ad.u.net->sk = sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786
Paul Moore253bfae2010-04-22 14:46:19 -04003787 return avc_has_perm(tsid, sksec->sid, sksec->sclass, perms, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788}
3789
3790static int selinux_socket_create(int family, int type,
3791 int protocol, int kern)
3792{
Paul Moore5fb49872010-04-22 14:46:19 -04003793 const struct task_security_struct *tsec = current_security();
Paul Moored4f2d972010-04-22 14:46:18 -04003794 u32 newsid;
David Howells275bb412008-11-14 10:39:19 +11003795 u16 secclass;
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003796 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797
3798 if (kern)
Paul Moored4f2d972010-04-22 14:46:18 -04003799 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800
David Howells275bb412008-11-14 10:39:19 +11003801 secclass = socket_type_to_security_class(family, type, protocol);
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003802 rc = socket_sockcreate_sid(tsec, secclass, &newsid);
3803 if (rc)
3804 return rc;
3805
Paul Moored4f2d972010-04-22 14:46:18 -04003806 return avc_has_perm(tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807}
3808
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003809static int selinux_socket_post_create(struct socket *sock, int family,
3810 int type, int protocol, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811{
Paul Moore5fb49872010-04-22 14:46:19 -04003812 const struct task_security_struct *tsec = current_security();
Paul Moored4f2d972010-04-22 14:46:18 -04003813 struct inode_security_struct *isec = SOCK_INODE(sock)->i_security;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07003814 struct sk_security_struct *sksec;
David Howells275bb412008-11-14 10:39:19 +11003815 int err = 0;
3816
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003817 isec->sclass = socket_type_to_security_class(family, type, protocol);
3818
David Howells275bb412008-11-14 10:39:19 +11003819 if (kern)
3820 isec->sid = SECINITSID_KERNEL;
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003821 else {
3822 err = socket_sockcreate_sid(tsec, isec->sclass, &(isec->sid));
3823 if (err)
3824 return err;
3825 }
David Howells275bb412008-11-14 10:39:19 +11003826
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 isec->initialized = 1;
3828
Venkat Yekkirala892c1412006-08-04 23:08:56 -07003829 if (sock->sk) {
3830 sksec = sock->sk->sk_security;
3831 sksec->sid = isec->sid;
Paul Moore220deb92008-01-29 08:38:23 -05003832 sksec->sclass = isec->sclass;
Paul Moore389fb8002009-03-27 17:10:34 -04003833 err = selinux_netlbl_socket_post_create(sock->sk, family);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07003834 }
3835
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003836 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837}
3838
3839/* Range of port numbers used to automatically bind.
3840 Need to determine whether we should perform a name_bind
3841 permission check between the socket and the port number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842
3843static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
3844{
Paul Moore253bfae2010-04-22 14:46:19 -04003845 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 u16 family;
3847 int err;
3848
Paul Moore253bfae2010-04-22 14:46:19 -04003849 err = sock_has_perm(current, sk, SOCKET__BIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 if (err)
3851 goto out;
3852
3853 /*
3854 * If PF_INET or PF_INET6, check name_bind permission for the port.
James Morris13402582005-09-30 14:24:34 -04003855 * Multiple address binding for SCTP is not supported yet: we just
3856 * check the first address now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 */
Paul Moore253bfae2010-04-22 14:46:19 -04003858 family = sk->sk_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859 if (family == PF_INET || family == PF_INET6) {
3860 char *addrp;
Paul Moore253bfae2010-04-22 14:46:19 -04003861 struct sk_security_struct *sksec = sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04003862 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04003863 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 struct sockaddr_in *addr4 = NULL;
3865 struct sockaddr_in6 *addr6 = NULL;
3866 unsigned short snum;
James Morrise399f982008-06-12 01:39:58 +10003867 u32 sid, node_perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 if (family == PF_INET) {
3870 addr4 = (struct sockaddr_in *)address;
3871 snum = ntohs(addr4->sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 addrp = (char *)&addr4->sin_addr.s_addr;
3873 } else {
3874 addr6 = (struct sockaddr_in6 *)address;
3875 snum = ntohs(addr6->sin6_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 addrp = (char *)&addr6->sin6_addr.s6_addr;
3877 }
3878
Stephen Hemminger227b60f2007-10-10 17:30:46 -07003879 if (snum) {
3880 int low, high;
3881
3882 inet_get_local_port_range(&low, &high);
3883
3884 if (snum < max(PROT_SOCK, low) || snum > high) {
Paul Moore3e1121722008-04-10 10:48:14 -04003885 err = sel_netport_sid(sk->sk_protocol,
3886 snum, &sid);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07003887 if (err)
3888 goto out;
Eric Paris50c205f2012-04-04 15:01:43 -04003889 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04003890 ad.u.net = &net;
3891 ad.u.net->sport = htons(snum);
3892 ad.u.net->family = family;
Paul Moore253bfae2010-04-22 14:46:19 -04003893 err = avc_has_perm(sksec->sid, sid,
3894 sksec->sclass,
Stephen Hemminger227b60f2007-10-10 17:30:46 -07003895 SOCKET__NAME_BIND, &ad);
3896 if (err)
3897 goto out;
3898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 }
Eric Paris828dfe12008-04-17 13:17:49 -04003900
Paul Moore253bfae2010-04-22 14:46:19 -04003901 switch (sksec->sclass) {
James Morris13402582005-09-30 14:24:34 -04003902 case SECCLASS_TCP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903 node_perm = TCP_SOCKET__NODE_BIND;
3904 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003905
James Morris13402582005-09-30 14:24:34 -04003906 case SECCLASS_UDP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 node_perm = UDP_SOCKET__NODE_BIND;
3908 break;
James Morris2ee92d42006-11-13 16:09:01 -08003909
3910 case SECCLASS_DCCP_SOCKET:
3911 node_perm = DCCP_SOCKET__NODE_BIND;
3912 break;
3913
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 default:
3915 node_perm = RAWIP_SOCKET__NODE_BIND;
3916 break;
3917 }
Eric Paris828dfe12008-04-17 13:17:49 -04003918
Paul Moore224dfbd2008-01-29 08:38:13 -05003919 err = sel_netnode_sid(addrp, family, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920 if (err)
3921 goto out;
Eric Paris828dfe12008-04-17 13:17:49 -04003922
Eric Paris50c205f2012-04-04 15:01:43 -04003923 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04003924 ad.u.net = &net;
3925 ad.u.net->sport = htons(snum);
3926 ad.u.net->family = family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927
3928 if (family == PF_INET)
Eric Paris48c62af2012-04-02 13:15:44 -04003929 ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 else
Eric Paris48c62af2012-04-02 13:15:44 -04003931 ad.u.net->v6info.saddr = addr6->sin6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932
Paul Moore253bfae2010-04-22 14:46:19 -04003933 err = avc_has_perm(sksec->sid, sid,
3934 sksec->sclass, node_perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 if (err)
3936 goto out;
3937 }
3938out:
3939 return err;
3940}
3941
3942static int selinux_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen)
3943{
Paul Moore014ab192008-10-10 10:16:33 -04003944 struct sock *sk = sock->sk;
Paul Moore253bfae2010-04-22 14:46:19 -04003945 struct sk_security_struct *sksec = sk->sk_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 int err;
3947
Paul Moore253bfae2010-04-22 14:46:19 -04003948 err = sock_has_perm(current, sk, SOCKET__CONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 if (err)
3950 return err;
3951
3952 /*
James Morris2ee92d42006-11-13 16:09:01 -08003953 * If a TCP or DCCP socket, check name_connect permission for the port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 */
Paul Moore253bfae2010-04-22 14:46:19 -04003955 if (sksec->sclass == SECCLASS_TCP_SOCKET ||
3956 sksec->sclass == SECCLASS_DCCP_SOCKET) {
Thomas Liu2bf49692009-07-14 12:14:09 -04003957 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04003958 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 struct sockaddr_in *addr4 = NULL;
3960 struct sockaddr_in6 *addr6 = NULL;
3961 unsigned short snum;
James Morris2ee92d42006-11-13 16:09:01 -08003962 u32 sid, perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963
3964 if (sk->sk_family == PF_INET) {
3965 addr4 = (struct sockaddr_in *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07003966 if (addrlen < sizeof(struct sockaddr_in))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 return -EINVAL;
3968 snum = ntohs(addr4->sin_port);
3969 } else {
3970 addr6 = (struct sockaddr_in6 *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07003971 if (addrlen < SIN6_LEN_RFC2133)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 return -EINVAL;
3973 snum = ntohs(addr6->sin6_port);
3974 }
3975
Paul Moore3e1121722008-04-10 10:48:14 -04003976 err = sel_netport_sid(sk->sk_protocol, snum, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 if (err)
3978 goto out;
3979
Paul Moore253bfae2010-04-22 14:46:19 -04003980 perm = (sksec->sclass == SECCLASS_TCP_SOCKET) ?
James Morris2ee92d42006-11-13 16:09:01 -08003981 TCP_SOCKET__NAME_CONNECT : DCCP_SOCKET__NAME_CONNECT;
3982
Eric Paris50c205f2012-04-04 15:01:43 -04003983 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04003984 ad.u.net = &net;
3985 ad.u.net->dport = htons(snum);
3986 ad.u.net->family = sk->sk_family;
Paul Moore253bfae2010-04-22 14:46:19 -04003987 err = avc_has_perm(sksec->sid, sid, sksec->sclass, perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 if (err)
3989 goto out;
3990 }
3991
Paul Moore014ab192008-10-10 10:16:33 -04003992 err = selinux_netlbl_socket_connect(sk, address);
3993
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994out:
3995 return err;
3996}
3997
3998static int selinux_socket_listen(struct socket *sock, int backlog)
3999{
Paul Moore253bfae2010-04-22 14:46:19 -04004000 return sock_has_perm(current, sock->sk, SOCKET__LISTEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001}
4002
4003static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
4004{
4005 int err;
4006 struct inode_security_struct *isec;
4007 struct inode_security_struct *newisec;
4008
Paul Moore253bfae2010-04-22 14:46:19 -04004009 err = sock_has_perm(current, sock->sk, SOCKET__ACCEPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 if (err)
4011 return err;
4012
4013 newisec = SOCK_INODE(newsock)->i_security;
4014
4015 isec = SOCK_INODE(sock)->i_security;
4016 newisec->sclass = isec->sclass;
4017 newisec->sid = isec->sid;
4018 newisec->initialized = 1;
4019
4020 return 0;
4021}
4022
4023static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
Eric Paris828dfe12008-04-17 13:17:49 -04004024 int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025{
Paul Moore253bfae2010-04-22 14:46:19 -04004026 return sock_has_perm(current, sock->sk, SOCKET__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027}
4028
4029static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
4030 int size, int flags)
4031{
Paul Moore253bfae2010-04-22 14:46:19 -04004032 return sock_has_perm(current, sock->sk, SOCKET__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033}
4034
4035static int selinux_socket_getsockname(struct socket *sock)
4036{
Paul Moore253bfae2010-04-22 14:46:19 -04004037 return sock_has_perm(current, sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038}
4039
4040static int selinux_socket_getpeername(struct socket *sock)
4041{
Paul Moore253bfae2010-04-22 14:46:19 -04004042 return sock_has_perm(current, sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043}
4044
Eric Paris828dfe12008-04-17 13:17:49 -04004045static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046{
Paul Mooref8687af2006-10-30 15:22:15 -08004047 int err;
4048
Paul Moore253bfae2010-04-22 14:46:19 -04004049 err = sock_has_perm(current, sock->sk, SOCKET__SETOPT);
Paul Mooref8687af2006-10-30 15:22:15 -08004050 if (err)
4051 return err;
4052
4053 return selinux_netlbl_socket_setsockopt(sock, level, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054}
4055
4056static int selinux_socket_getsockopt(struct socket *sock, int level,
4057 int optname)
4058{
Paul Moore253bfae2010-04-22 14:46:19 -04004059 return sock_has_perm(current, sock->sk, SOCKET__GETOPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060}
4061
4062static int selinux_socket_shutdown(struct socket *sock, int how)
4063{
Paul Moore253bfae2010-04-22 14:46:19 -04004064 return sock_has_perm(current, sock->sk, SOCKET__SHUTDOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065}
4066
David S. Miller3610cda2011-01-05 15:38:53 -08004067static int selinux_socket_unix_stream_connect(struct sock *sock,
4068 struct sock *other,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 struct sock *newsk)
4070{
David S. Miller3610cda2011-01-05 15:38:53 -08004071 struct sk_security_struct *sksec_sock = sock->sk_security;
4072 struct sk_security_struct *sksec_other = other->sk_security;
Paul Moore4d1e2452010-04-22 14:46:18 -04004073 struct sk_security_struct *sksec_new = newsk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004074 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004075 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 int err;
4077
Eric Paris50c205f2012-04-04 15:01:43 -04004078 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004079 ad.u.net = &net;
4080 ad.u.net->sk = other;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081
Paul Moore4d1e2452010-04-22 14:46:18 -04004082 err = avc_has_perm(sksec_sock->sid, sksec_other->sid,
4083 sksec_other->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 UNIX_STREAM_SOCKET__CONNECTTO, &ad);
4085 if (err)
4086 return err;
4087
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 /* server child socket */
Paul Moore4d1e2452010-04-22 14:46:18 -04004089 sksec_new->peer_sid = sksec_sock->sid;
4090 err = security_sid_mls_copy(sksec_other->sid, sksec_sock->sid,
4091 &sksec_new->sid);
4092 if (err)
4093 return err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004094
Paul Moore4d1e2452010-04-22 14:46:18 -04004095 /* connecting socket */
4096 sksec_sock->peer_sid = sksec_new->sid;
4097
4098 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099}
4100
4101static int selinux_socket_unix_may_send(struct socket *sock,
4102 struct socket *other)
4103{
Paul Moore253bfae2010-04-22 14:46:19 -04004104 struct sk_security_struct *ssec = sock->sk->sk_security;
4105 struct sk_security_struct *osec = other->sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004106 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004107 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108
Eric Paris50c205f2012-04-04 15:01:43 -04004109 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004110 ad.u.net = &net;
4111 ad.u.net->sk = other->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112
Paul Moore253bfae2010-04-22 14:46:19 -04004113 return avc_has_perm(ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
4114 &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115}
4116
Paul Mooreeffad8d2008-01-29 08:49:27 -05004117static int selinux_inet_sys_rcv_skb(int ifindex, char *addrp, u16 family,
4118 u32 peer_sid,
Thomas Liu2bf49692009-07-14 12:14:09 -04004119 struct common_audit_data *ad)
Paul Mooreeffad8d2008-01-29 08:49:27 -05004120{
4121 int err;
4122 u32 if_sid;
4123 u32 node_sid;
4124
4125 err = sel_netif_sid(ifindex, &if_sid);
4126 if (err)
4127 return err;
4128 err = avc_has_perm(peer_sid, if_sid,
4129 SECCLASS_NETIF, NETIF__INGRESS, ad);
4130 if (err)
4131 return err;
4132
4133 err = sel_netnode_sid(addrp, family, &node_sid);
4134 if (err)
4135 return err;
4136 return avc_has_perm(peer_sid, node_sid,
4137 SECCLASS_NODE, NODE__RECVFROM, ad);
4138}
4139
Paul Moore220deb92008-01-29 08:38:23 -05004140static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
Paul Moored8395c82008-10-10 10:16:30 -04004141 u16 family)
Paul Moore220deb92008-01-29 08:38:23 -05004142{
Paul Moore277d3422008-12-31 12:54:11 -05004143 int err = 0;
Paul Moore220deb92008-01-29 08:38:23 -05004144 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004145 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004146 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004147 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04004148 char *addrp;
4149
Eric Paris50c205f2012-04-04 15:01:43 -04004150 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004151 ad.u.net = &net;
4152 ad.u.net->netif = skb->skb_iif;
4153 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004154 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4155 if (err)
4156 return err;
Paul Moore220deb92008-01-29 08:38:23 -05004157
Paul Moore58bfbb52009-03-27 17:10:41 -04004158 if (selinux_secmark_enabled()) {
Paul Moore220deb92008-01-29 08:38:23 -05004159 err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Moored8395c82008-10-10 10:16:30 -04004160 PACKET__RECV, &ad);
Paul Moore58bfbb52009-03-27 17:10:41 -04004161 if (err)
4162 return err;
4163 }
Paul Moore220deb92008-01-29 08:38:23 -05004164
Steffen Klassertb9679a72011-02-23 12:55:21 +01004165 err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
4166 if (err)
4167 return err;
4168 err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004169
James Morris4e5ab4c2006-06-09 00:33:33 -07004170 return err;
4171}
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004172
James Morris4e5ab4c2006-06-09 00:33:33 -07004173static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
4174{
Paul Moore220deb92008-01-29 08:38:23 -05004175 int err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004176 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004177 u16 family = sk->sk_family;
4178 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004179 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004180 struct lsm_network_audit net = {0,};
Paul Moore220deb92008-01-29 08:38:23 -05004181 char *addrp;
Paul Moored8395c82008-10-10 10:16:30 -04004182 u8 secmark_active;
4183 u8 peerlbl_active;
James Morris4e5ab4c2006-06-09 00:33:33 -07004184
James Morris4e5ab4c2006-06-09 00:33:33 -07004185 if (family != PF_INET && family != PF_INET6)
Paul Moore220deb92008-01-29 08:38:23 -05004186 return 0;
James Morris4e5ab4c2006-06-09 00:33:33 -07004187
4188 /* Handle mapped IPv4 packets arriving via IPv6 sockets */
Al Viro87fcd702006-12-04 22:00:55 +00004189 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
James Morris4e5ab4c2006-06-09 00:33:33 -07004190 family = PF_INET;
4191
Paul Moored8395c82008-10-10 10:16:30 -04004192 /* If any sort of compatibility mode is enabled then handoff processing
4193 * to the selinux_sock_rcv_skb_compat() function to deal with the
4194 * special handling. We do this in an attempt to keep this function
4195 * as fast and as clean as possible. */
Paul Moore58bfbb52009-03-27 17:10:41 -04004196 if (!selinux_policycap_netpeer)
Paul Moored8395c82008-10-10 10:16:30 -04004197 return selinux_sock_rcv_skb_compat(sk, skb, family);
4198
4199 secmark_active = selinux_secmark_enabled();
4200 peerlbl_active = netlbl_enabled() || selinux_xfrm_enabled();
4201 if (!secmark_active && !peerlbl_active)
4202 return 0;
4203
Eric Paris50c205f2012-04-04 15:01:43 -04004204 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004205 ad.u.net = &net;
4206 ad.u.net->netif = skb->skb_iif;
4207 ad.u.net->family = family;
Paul Moore224dfbd2008-01-29 08:38:13 -05004208 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
James Morris4e5ab4c2006-06-09 00:33:33 -07004209 if (err)
Paul Moore220deb92008-01-29 08:38:23 -05004210 return err;
James Morris4e5ab4c2006-06-09 00:33:33 -07004211
Paul Moored8395c82008-10-10 10:16:30 -04004212 if (peerlbl_active) {
Paul Moored621d352008-01-29 08:43:36 -05004213 u32 peer_sid;
4214
4215 err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
4216 if (err)
4217 return err;
Eric Dumazet8964be42009-11-20 15:35:04 -08004218 err = selinux_inet_sys_rcv_skb(skb->skb_iif, addrp, family,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004219 peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04004220 if (err) {
4221 selinux_netlbl_err(skb, err, 0);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004222 return err;
Paul Mooredfaebe92008-10-10 10:16:31 -04004223 }
Paul Moored621d352008-01-29 08:43:36 -05004224 err = avc_has_perm(sk_sid, peer_sid, SECCLASS_PEER,
4225 PEER__RECV, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04004226 if (err)
4227 selinux_netlbl_err(skb, err, 0);
Paul Moored621d352008-01-29 08:43:36 -05004228 }
4229
Paul Moored8395c82008-10-10 10:16:30 -04004230 if (secmark_active) {
Paul Mooreeffad8d2008-01-29 08:49:27 -05004231 err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
4232 PACKET__RECV, &ad);
4233 if (err)
4234 return err;
4235 }
4236
Paul Moored621d352008-01-29 08:43:36 -05004237 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238}
4239
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004240static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
4241 int __user *optlen, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242{
4243 int err = 0;
4244 char *scontext;
4245 u32 scontext_len;
Paul Moore253bfae2010-04-22 14:46:19 -04004246 struct sk_security_struct *sksec = sock->sk->sk_security;
Paul Moore3de4bab2006-11-17 17:38:54 -05004247 u32 peer_sid = SECSID_NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248
Paul Moore253bfae2010-04-22 14:46:19 -04004249 if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
4250 sksec->sclass == SECCLASS_TCP_SOCKET)
Eric Parisdd3e7832010-04-07 15:08:46 -04004251 peer_sid = sksec->peer_sid;
Paul Moore253bfae2010-04-22 14:46:19 -04004252 if (peer_sid == SECSID_NULL)
4253 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004255 err = security_sid_to_context(peer_sid, &scontext, &scontext_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 if (err)
Paul Moore253bfae2010-04-22 14:46:19 -04004257 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258
4259 if (scontext_len > len) {
4260 err = -ERANGE;
4261 goto out_len;
4262 }
4263
4264 if (copy_to_user(optval, scontext, scontext_len))
4265 err = -EFAULT;
4266
4267out_len:
4268 if (put_user(scontext_len, optlen))
4269 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 kfree(scontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 return err;
4272}
4273
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004274static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004275{
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004276 u32 peer_secid = SECSID_NULL;
Paul Moore75e22912008-01-29 08:38:04 -05004277 u16 family;
Catherine Zhang877ce7c2006-06-29 12:27:47 -07004278
Paul Mooreaa862902008-10-10 10:16:29 -04004279 if (skb && skb->protocol == htons(ETH_P_IP))
4280 family = PF_INET;
4281 else if (skb && skb->protocol == htons(ETH_P_IPV6))
4282 family = PF_INET6;
4283 else if (sock)
Paul Moore75e22912008-01-29 08:38:04 -05004284 family = sock->sk->sk_family;
Paul Moore75e22912008-01-29 08:38:04 -05004285 else
4286 goto out;
4287
4288 if (sock && family == PF_UNIX)
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02004289 selinux_inode_getsecid(SOCK_INODE(sock), &peer_secid);
Paul Moore3de4bab2006-11-17 17:38:54 -05004290 else if (skb)
Paul Moore220deb92008-01-29 08:38:23 -05004291 selinux_skb_peerlbl_sid(skb, family, &peer_secid);
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004292
Paul Moore75e22912008-01-29 08:38:04 -05004293out:
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004294 *secid = peer_secid;
Paul Moore75e22912008-01-29 08:38:04 -05004295 if (peer_secid == SECSID_NULL)
4296 return -EINVAL;
4297 return 0;
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004298}
4299
Al Viro7d877f32005-10-21 03:20:43 -04004300static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301{
Paul Moore84914b72010-04-22 14:46:18 -04004302 struct sk_security_struct *sksec;
4303
4304 sksec = kzalloc(sizeof(*sksec), priority);
4305 if (!sksec)
4306 return -ENOMEM;
4307
4308 sksec->peer_sid = SECINITSID_UNLABELED;
4309 sksec->sid = SECINITSID_UNLABELED;
4310 selinux_netlbl_sk_security_reset(sksec);
4311 sk->sk_security = sksec;
4312
4313 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314}
4315
4316static void selinux_sk_free_security(struct sock *sk)
4317{
Paul Moore84914b72010-04-22 14:46:18 -04004318 struct sk_security_struct *sksec = sk->sk_security;
4319
4320 sk->sk_security = NULL;
4321 selinux_netlbl_sk_security_free(sksec);
4322 kfree(sksec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323}
4324
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004325static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
4326{
Eric Parisdd3e7832010-04-07 15:08:46 -04004327 struct sk_security_struct *sksec = sk->sk_security;
4328 struct sk_security_struct *newsksec = newsk->sk_security;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004329
Eric Parisdd3e7832010-04-07 15:08:46 -04004330 newsksec->sid = sksec->sid;
4331 newsksec->peer_sid = sksec->peer_sid;
4332 newsksec->sclass = sksec->sclass;
Paul Moore99f59ed2006-08-29 17:53:48 -07004333
Eric Parisdd3e7832010-04-07 15:08:46 -04004334 selinux_netlbl_sk_security_reset(newsksec);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004335}
4336
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07004337static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004338{
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004339 if (!sk)
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07004340 *secid = SECINITSID_ANY_SOCKET;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004341 else {
4342 struct sk_security_struct *sksec = sk->sk_security;
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004343
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07004344 *secid = sksec->sid;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004345 }
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004346}
4347
Eric Paris828dfe12008-04-17 13:17:49 -04004348static void selinux_sock_graft(struct sock *sk, struct socket *parent)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004349{
4350 struct inode_security_struct *isec = SOCK_INODE(parent)->i_security;
4351 struct sk_security_struct *sksec = sk->sk_security;
4352
David Woodhouse2148ccc2006-09-29 15:50:25 -07004353 if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
4354 sk->sk_family == PF_UNIX)
4355 isec->sid = sksec->sid;
Paul Moore220deb92008-01-29 08:38:23 -05004356 sksec->sclass = isec->sclass;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004357}
4358
Adrian Bunk9a673e52006-08-15 00:03:53 -07004359static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4360 struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004361{
4362 struct sk_security_struct *sksec = sk->sk_security;
4363 int err;
Paul Mooreaa862902008-10-10 10:16:29 -04004364 u16 family = sk->sk_family;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07004365 u32 newsid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004366 u32 peersid;
4367
Paul Mooreaa862902008-10-10 10:16:29 -04004368 /* handle mapped IPv4 packets arriving via IPv6 sockets */
4369 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4370 family = PF_INET;
4371
4372 err = selinux_skb_peerlbl_sid(skb, family, &peersid);
Paul Moore220deb92008-01-29 08:38:23 -05004373 if (err)
4374 return err;
Venkat Yekkiralaa51c64f2006-07-27 22:01:34 -07004375 if (peersid == SECSID_NULL) {
4376 req->secid = sksec->sid;
Paul Moore3de4bab2006-11-17 17:38:54 -05004377 req->peer_secid = SECSID_NULL;
Paul Moore389fb8002009-03-27 17:10:34 -04004378 } else {
4379 err = security_sid_mls_copy(sksec->sid, peersid, &newsid);
4380 if (err)
4381 return err;
4382 req->secid = newsid;
4383 req->peer_secid = peersid;
Venkat Yekkiralaa51c64f2006-07-27 22:01:34 -07004384 }
4385
Paul Moore389fb8002009-03-27 17:10:34 -04004386 return selinux_netlbl_inet_conn_request(req, family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004387}
4388
Adrian Bunk9a673e52006-08-15 00:03:53 -07004389static void selinux_inet_csk_clone(struct sock *newsk,
4390 const struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004391{
4392 struct sk_security_struct *newsksec = newsk->sk_security;
4393
4394 newsksec->sid = req->secid;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004395 newsksec->peer_sid = req->peer_secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004396 /* NOTE: Ideally, we should also get the isec->sid for the
4397 new socket in sync, but we don't have the isec available yet.
4398 So we will wait until sock_graft to do it, by which
4399 time it will have been created and available. */
Paul Moore99f59ed2006-08-29 17:53:48 -07004400
Paul Moore9f2ad662006-11-17 17:38:53 -05004401 /* We don't need to take any sort of lock here as we are the only
4402 * thread with access to newsksec */
Paul Moore389fb8002009-03-27 17:10:34 -04004403 selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004404}
4405
Paul Moore014ab192008-10-10 10:16:33 -04004406static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004407{
Paul Mooreaa862902008-10-10 10:16:29 -04004408 u16 family = sk->sk_family;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004409 struct sk_security_struct *sksec = sk->sk_security;
4410
Paul Mooreaa862902008-10-10 10:16:29 -04004411 /* handle mapped IPv4 packets arriving via IPv6 sockets */
4412 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4413 family = PF_INET;
4414
4415 selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004416}
4417
Eric Dumazetca10b9e2013-04-08 17:58:11 +00004418static void selinux_skb_owned_by(struct sk_buff *skb, struct sock *sk)
4419{
4420 skb_set_owner_w(skb, sk);
4421}
4422
Eric Paris2606fd12010-10-13 16:24:41 -04004423static int selinux_secmark_relabel_packet(u32 sid)
4424{
4425 const struct task_security_struct *__tsec;
4426 u32 tsid;
4427
4428 __tsec = current_security();
4429 tsid = __tsec->sid;
4430
4431 return avc_has_perm(tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO, NULL);
4432}
4433
4434static void selinux_secmark_refcount_inc(void)
4435{
4436 atomic_inc(&selinux_secmark_refcount);
4437}
4438
4439static void selinux_secmark_refcount_dec(void)
4440{
4441 atomic_dec(&selinux_secmark_refcount);
4442}
4443
Adrian Bunk9a673e52006-08-15 00:03:53 -07004444static void selinux_req_classify_flow(const struct request_sock *req,
4445 struct flowi *fl)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004446{
David S. Miller1d28f422011-03-12 00:29:39 -05004447 fl->flowi_secid = req->secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004448}
4449
Paul Moore5dbbaf22013-01-14 07:12:19 +00004450static int selinux_tun_dev_alloc_security(void **security)
4451{
4452 struct tun_security_struct *tunsec;
4453
4454 tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL);
4455 if (!tunsec)
4456 return -ENOMEM;
4457 tunsec->sid = current_sid();
4458
4459 *security = tunsec;
4460 return 0;
4461}
4462
4463static void selinux_tun_dev_free_security(void *security)
4464{
4465 kfree(security);
4466}
4467
Paul Mooreed6d76e2009-08-28 18:12:49 -04004468static int selinux_tun_dev_create(void)
4469{
4470 u32 sid = current_sid();
4471
4472 /* we aren't taking into account the "sockcreate" SID since the socket
4473 * that is being created here is not a socket in the traditional sense,
4474 * instead it is a private sock, accessible only to the kernel, and
4475 * representing a wide range of network traffic spanning multiple
4476 * connections unlike traditional sockets - check the TUN driver to
4477 * get a better understanding of why this socket is special */
4478
4479 return avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
4480 NULL);
4481}
4482
Paul Moore5dbbaf22013-01-14 07:12:19 +00004483static int selinux_tun_dev_attach_queue(void *security)
Paul Mooreed6d76e2009-08-28 18:12:49 -04004484{
Paul Moore5dbbaf22013-01-14 07:12:19 +00004485 struct tun_security_struct *tunsec = security;
4486
4487 return avc_has_perm(current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET,
4488 TUN_SOCKET__ATTACH_QUEUE, NULL);
4489}
4490
4491static int selinux_tun_dev_attach(struct sock *sk, void *security)
4492{
4493 struct tun_security_struct *tunsec = security;
Paul Mooreed6d76e2009-08-28 18:12:49 -04004494 struct sk_security_struct *sksec = sk->sk_security;
4495
4496 /* we don't currently perform any NetLabel based labeling here and it
4497 * isn't clear that we would want to do so anyway; while we could apply
4498 * labeling without the support of the TUN user the resulting labeled
4499 * traffic from the other end of the connection would almost certainly
4500 * cause confusion to the TUN user that had no idea network labeling
4501 * protocols were being used */
4502
Paul Moore5dbbaf22013-01-14 07:12:19 +00004503 sksec->sid = tunsec->sid;
Paul Mooreed6d76e2009-08-28 18:12:49 -04004504 sksec->sclass = SECCLASS_TUN_SOCKET;
Paul Moore5dbbaf22013-01-14 07:12:19 +00004505
4506 return 0;
Paul Mooreed6d76e2009-08-28 18:12:49 -04004507}
4508
Paul Moore5dbbaf22013-01-14 07:12:19 +00004509static int selinux_tun_dev_open(void *security)
Paul Mooreed6d76e2009-08-28 18:12:49 -04004510{
Paul Moore5dbbaf22013-01-14 07:12:19 +00004511 struct tun_security_struct *tunsec = security;
Paul Mooreed6d76e2009-08-28 18:12:49 -04004512 u32 sid = current_sid();
4513 int err;
4514
Paul Moore5dbbaf22013-01-14 07:12:19 +00004515 err = avc_has_perm(sid, tunsec->sid, SECCLASS_TUN_SOCKET,
Paul Mooreed6d76e2009-08-28 18:12:49 -04004516 TUN_SOCKET__RELABELFROM, NULL);
4517 if (err)
4518 return err;
4519 err = avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET,
4520 TUN_SOCKET__RELABELTO, NULL);
4521 if (err)
4522 return err;
Paul Moore5dbbaf22013-01-14 07:12:19 +00004523 tunsec->sid = sid;
Paul Mooreed6d76e2009-08-28 18:12:49 -04004524
4525 return 0;
4526}
4527
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
4529{
4530 int err = 0;
4531 u32 perm;
4532 struct nlmsghdr *nlh;
Paul Moore253bfae2010-04-22 14:46:19 -04004533 struct sk_security_struct *sksec = sk->sk_security;
Eric Paris828dfe12008-04-17 13:17:49 -04004534
Hong zhi guo77954982013-03-27 06:49:35 +00004535 if (skb->len < NLMSG_HDRLEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536 err = -EINVAL;
4537 goto out;
4538 }
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -07004539 nlh = nlmsg_hdr(skb);
Eric Paris828dfe12008-04-17 13:17:49 -04004540
Paul Moore253bfae2010-04-22 14:46:19 -04004541 err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 if (err) {
4543 if (err == -EINVAL) {
David Woodhouse9ad9ad32005-06-22 15:04:33 +01004544 audit_log(current->audit_context, GFP_KERNEL, AUDIT_SELINUX_ERR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545 "SELinux: unrecognized netlink message"
4546 " type=%hu for sclass=%hu\n",
Paul Moore253bfae2010-04-22 14:46:19 -04004547 nlh->nlmsg_type, sksec->sclass);
Eric Paris39c9aed2008-11-05 09:34:42 -05004548 if (!selinux_enforcing || security_get_allow_unknown())
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549 err = 0;
4550 }
4551
4552 /* Ignore */
4553 if (err == -ENOENT)
4554 err = 0;
4555 goto out;
4556 }
4557
Paul Moore253bfae2010-04-22 14:46:19 -04004558 err = sock_has_perm(current, sk, perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559out:
4560 return err;
4561}
4562
4563#ifdef CONFIG_NETFILTER
4564
Paul Mooreeffad8d2008-01-29 08:49:27 -05004565static unsigned int selinux_ip_forward(struct sk_buff *skb, int ifindex,
4566 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567{
Paul Mooredfaebe92008-10-10 10:16:31 -04004568 int err;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004569 char *addrp;
4570 u32 peer_sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004571 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004572 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05004573 u8 secmark_active;
Paul Moore948bf852008-10-10 10:16:32 -04004574 u8 netlbl_active;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004575 u8 peerlbl_active;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004576
Paul Mooreeffad8d2008-01-29 08:49:27 -05004577 if (!selinux_policycap_netpeer)
4578 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004579
Paul Mooreeffad8d2008-01-29 08:49:27 -05004580 secmark_active = selinux_secmark_enabled();
Paul Moore948bf852008-10-10 10:16:32 -04004581 netlbl_active = netlbl_enabled();
4582 peerlbl_active = netlbl_active || selinux_xfrm_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05004583 if (!secmark_active && !peerlbl_active)
4584 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004585
Paul Moored8395c82008-10-10 10:16:30 -04004586 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
4587 return NF_DROP;
4588
Eric Paris50c205f2012-04-04 15:01:43 -04004589 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004590 ad.u.net = &net;
4591 ad.u.net->netif = ifindex;
4592 ad.u.net->family = family;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004593 if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
4594 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595
Paul Mooredfaebe92008-10-10 10:16:31 -04004596 if (peerlbl_active) {
4597 err = selinux_inet_sys_rcv_skb(ifindex, addrp, family,
4598 peer_sid, &ad);
4599 if (err) {
4600 selinux_netlbl_err(skb, err, 1);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004601 return NF_DROP;
Paul Mooredfaebe92008-10-10 10:16:31 -04004602 }
4603 }
Paul Mooreeffad8d2008-01-29 08:49:27 -05004604
4605 if (secmark_active)
4606 if (avc_has_perm(peer_sid, skb->secmark,
4607 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
4608 return NF_DROP;
4609
Paul Moore948bf852008-10-10 10:16:32 -04004610 if (netlbl_active)
4611 /* we do this in the FORWARD path and not the POST_ROUTING
4612 * path because we want to make sure we apply the necessary
4613 * labeling before IPsec is applied so we can leverage AH
4614 * protection */
4615 if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
4616 return NF_DROP;
4617
Paul Mooreeffad8d2008-01-29 08:49:27 -05004618 return NF_ACCEPT;
4619}
4620
4621static unsigned int selinux_ipv4_forward(unsigned int hooknum,
4622 struct sk_buff *skb,
4623 const struct net_device *in,
4624 const struct net_device *out,
4625 int (*okfn)(struct sk_buff *))
4626{
4627 return selinux_ip_forward(skb, in->ifindex, PF_INET);
4628}
4629
4630#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
4631static unsigned int selinux_ipv6_forward(unsigned int hooknum,
4632 struct sk_buff *skb,
4633 const struct net_device *in,
4634 const struct net_device *out,
4635 int (*okfn)(struct sk_buff *))
4636{
4637 return selinux_ip_forward(skb, in->ifindex, PF_INET6);
4638}
4639#endif /* IPV6 */
4640
Paul Moore948bf852008-10-10 10:16:32 -04004641static unsigned int selinux_ip_output(struct sk_buff *skb,
4642 u16 family)
4643{
4644 u32 sid;
4645
4646 if (!netlbl_enabled())
4647 return NF_ACCEPT;
4648
4649 /* we do this in the LOCAL_OUT path and not the POST_ROUTING path
4650 * because we want to make sure we apply the necessary labeling
4651 * before IPsec is applied so we can leverage AH protection */
4652 if (skb->sk) {
4653 struct sk_security_struct *sksec = skb->sk->sk_security;
4654 sid = sksec->sid;
4655 } else
4656 sid = SECINITSID_KERNEL;
4657 if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
4658 return NF_DROP;
4659
4660 return NF_ACCEPT;
4661}
4662
4663static unsigned int selinux_ipv4_output(unsigned int hooknum,
4664 struct sk_buff *skb,
4665 const struct net_device *in,
4666 const struct net_device *out,
4667 int (*okfn)(struct sk_buff *))
4668{
4669 return selinux_ip_output(skb, PF_INET);
4670}
4671
Paul Mooreeffad8d2008-01-29 08:49:27 -05004672static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
4673 int ifindex,
Paul Moored8395c82008-10-10 10:16:30 -04004674 u16 family)
James Morris4e5ab4c2006-06-09 00:33:33 -07004675{
Paul Mooreeffad8d2008-01-29 08:49:27 -05004676 struct sock *sk = skb->sk;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004677 struct sk_security_struct *sksec;
Thomas Liu2bf49692009-07-14 12:14:09 -04004678 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004679 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04004680 char *addrp;
4681 u8 proto;
James Morris4e5ab4c2006-06-09 00:33:33 -07004682
Paul Mooreeffad8d2008-01-29 08:49:27 -05004683 if (sk == NULL)
4684 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004685 sksec = sk->sk_security;
James Morris4e5ab4c2006-06-09 00:33:33 -07004686
Eric Paris50c205f2012-04-04 15:01:43 -04004687 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004688 ad.u.net = &net;
4689 ad.u.net->netif = ifindex;
4690 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004691 if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
4692 return NF_DROP;
4693
Paul Moore58bfbb52009-03-27 17:10:41 -04004694 if (selinux_secmark_enabled())
Paul Mooreeffad8d2008-01-29 08:49:27 -05004695 if (avc_has_perm(sksec->sid, skb->secmark,
Paul Moored8395c82008-10-10 10:16:30 -04004696 SECCLASS_PACKET, PACKET__SEND, &ad))
Eric Paris2fe66ec2010-11-23 06:28:08 +00004697 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07004698
Steffen Klassertb9679a72011-02-23 12:55:21 +01004699 if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
4700 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07004701
Paul Mooreeffad8d2008-01-29 08:49:27 -05004702 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703}
4704
Paul Mooreeffad8d2008-01-29 08:49:27 -05004705static unsigned int selinux_ip_postroute(struct sk_buff *skb, int ifindex,
4706 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707{
Paul Mooreeffad8d2008-01-29 08:49:27 -05004708 u32 secmark_perm;
4709 u32 peer_sid;
4710 struct sock *sk;
Thomas Liu2bf49692009-07-14 12:14:09 -04004711 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004712 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05004713 char *addrp;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004714 u8 secmark_active;
4715 u8 peerlbl_active;
4716
Paul Mooreeffad8d2008-01-29 08:49:27 -05004717 /* If any sort of compatibility mode is enabled then handoff processing
4718 * to the selinux_ip_postroute_compat() function to deal with the
4719 * special handling. We do this in an attempt to keep this function
4720 * as fast and as clean as possible. */
Paul Moore58bfbb52009-03-27 17:10:41 -04004721 if (!selinux_policycap_netpeer)
Paul Moored8395c82008-10-10 10:16:30 -04004722 return selinux_ip_postroute_compat(skb, ifindex, family);
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -07004723#ifdef CONFIG_XFRM
Paul Mooreeffad8d2008-01-29 08:49:27 -05004724 /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
4725 * packet transformation so allow the packet to pass without any checks
4726 * since we'll have another chance to perform access control checks
4727 * when the packet is on it's final way out.
4728 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
4729 * is NULL, in this case go ahead and apply access control. */
Eric Dumazetadf30902009-06-02 05:19:30 +00004730 if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL)
Paul Mooreeffad8d2008-01-29 08:49:27 -05004731 return NF_ACCEPT;
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -07004732#endif
Paul Mooreeffad8d2008-01-29 08:49:27 -05004733 secmark_active = selinux_secmark_enabled();
4734 peerlbl_active = netlbl_enabled() || selinux_xfrm_enabled();
4735 if (!secmark_active && !peerlbl_active)
4736 return NF_ACCEPT;
4737
Paul Moored8395c82008-10-10 10:16:30 -04004738 /* if the packet is being forwarded then get the peer label from the
4739 * packet itself; otherwise check to see if it is from a local
4740 * application or the kernel, if from an application get the peer label
4741 * from the sending socket, otherwise use the kernel's sid */
Paul Mooreeffad8d2008-01-29 08:49:27 -05004742 sk = skb->sk;
Paul Moored8395c82008-10-10 10:16:30 -04004743 if (sk == NULL) {
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01004744 if (skb->skb_iif) {
4745 secmark_perm = PACKET__FORWARD_OUT;
Paul Moored8395c82008-10-10 10:16:30 -04004746 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00004747 return NF_DROP;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01004748 } else {
4749 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04004750 peer_sid = SECINITSID_KERNEL;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01004751 }
Paul Moored8395c82008-10-10 10:16:30 -04004752 } else {
Paul Mooreeffad8d2008-01-29 08:49:27 -05004753 struct sk_security_struct *sksec = sk->sk_security;
4754 peer_sid = sksec->sid;
4755 secmark_perm = PACKET__SEND;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004756 }
4757
Eric Paris50c205f2012-04-04 15:01:43 -04004758 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004759 ad.u.net = &net;
4760 ad.u.net->netif = ifindex;
4761 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004762 if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
Eric Paris04f6d702010-11-23 06:28:02 +00004763 return NF_DROP;
Paul Moored8395c82008-10-10 10:16:30 -04004764
Paul Mooreeffad8d2008-01-29 08:49:27 -05004765 if (secmark_active)
4766 if (avc_has_perm(peer_sid, skb->secmark,
4767 SECCLASS_PACKET, secmark_perm, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00004768 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004769
4770 if (peerlbl_active) {
4771 u32 if_sid;
4772 u32 node_sid;
4773
4774 if (sel_netif_sid(ifindex, &if_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00004775 return NF_DROP;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004776 if (avc_has_perm(peer_sid, if_sid,
4777 SECCLASS_NETIF, NETIF__EGRESS, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00004778 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004779
4780 if (sel_netnode_sid(addrp, family, &node_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00004781 return NF_DROP;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004782 if (avc_has_perm(peer_sid, node_sid,
4783 SECCLASS_NODE, NODE__SENDTO, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00004784 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004785 }
4786
4787 return NF_ACCEPT;
4788}
4789
4790static unsigned int selinux_ipv4_postroute(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_postroute(skb, out->ifindex, PF_INET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797}
4798
4799#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Paul Mooreeffad8d2008-01-29 08:49:27 -05004800static unsigned int selinux_ipv6_postroute(unsigned int hooknum,
4801 struct sk_buff *skb,
4802 const struct net_device *in,
4803 const struct net_device *out,
4804 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805{
Paul Mooreeffad8d2008-01-29 08:49:27 -05004806 return selinux_ip_postroute(skb, out->ifindex, PF_INET6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808#endif /* IPV6 */
4809
4810#endif /* CONFIG_NETFILTER */
4811
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
4813{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814 int err;
4815
Eric Paris200ac532009-02-12 15:01:04 -05004816 err = cap_netlink_send(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817 if (err)
4818 return err;
4819
Stephen Smalley941fc5b2009-10-01 14:48:23 -04004820 return selinux_nlmsg_perm(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821}
4822
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823static int ipc_alloc_security(struct task_struct *task,
4824 struct kern_ipc_perm *perm,
4825 u16 sclass)
4826{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827 struct ipc_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +11004828 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829
James Morris89d155e2005-10-30 14:59:21 -08004830 isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 if (!isec)
4832 return -ENOMEM;
4833
David Howells275bb412008-11-14 10:39:19 +11004834 sid = task_sid(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835 isec->sclass = sclass;
David Howells275bb412008-11-14 10:39:19 +11004836 isec->sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837 perm->security = isec;
4838
4839 return 0;
4840}
4841
4842static void ipc_free_security(struct kern_ipc_perm *perm)
4843{
4844 struct ipc_security_struct *isec = perm->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845 perm->security = NULL;
4846 kfree(isec);
4847}
4848
4849static int msg_msg_alloc_security(struct msg_msg *msg)
4850{
4851 struct msg_security_struct *msec;
4852
James Morris89d155e2005-10-30 14:59:21 -08004853 msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 if (!msec)
4855 return -ENOMEM;
4856
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857 msec->sid = SECINITSID_UNLABELED;
4858 msg->security = msec;
4859
4860 return 0;
4861}
4862
4863static void msg_msg_free_security(struct msg_msg *msg)
4864{
4865 struct msg_security_struct *msec = msg->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866
4867 msg->security = NULL;
4868 kfree(msec);
4869}
4870
4871static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
Stephen Smalley6af963f2005-05-01 08:58:39 -07004872 u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04004875 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11004876 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 isec = ipc_perms->security;
4879
Eric Paris50c205f2012-04-04 15:01:43 -04004880 ad.type = LSM_AUDIT_DATA_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881 ad.u.ipc_id = ipc_perms->key;
4882
David Howells275bb412008-11-14 10:39:19 +11004883 return avc_has_perm(sid, isec->sid, isec->sclass, perms, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884}
4885
4886static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
4887{
4888 return msg_msg_alloc_security(msg);
4889}
4890
4891static void selinux_msg_msg_free_security(struct msg_msg *msg)
4892{
4893 msg_msg_free_security(msg);
4894}
4895
4896/* message queue security operations */
4897static int selinux_msg_queue_alloc_security(struct msg_queue *msq)
4898{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04004900 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11004901 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902 int rc;
4903
4904 rc = ipc_alloc_security(current, &msq->q_perm, SECCLASS_MSGQ);
4905 if (rc)
4906 return rc;
4907
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908 isec = msq->q_perm.security;
4909
Eric Paris50c205f2012-04-04 15:01:43 -04004910 ad.type = LSM_AUDIT_DATA_IPC;
Eric Paris828dfe12008-04-17 13:17:49 -04004911 ad.u.ipc_id = msq->q_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912
David Howells275bb412008-11-14 10:39:19 +11004913 rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 MSGQ__CREATE, &ad);
4915 if (rc) {
4916 ipc_free_security(&msq->q_perm);
4917 return rc;
4918 }
4919 return 0;
4920}
4921
4922static void selinux_msg_queue_free_security(struct msg_queue *msq)
4923{
4924 ipc_free_security(&msq->q_perm);
4925}
4926
4927static int selinux_msg_queue_associate(struct msg_queue *msq, int msqflg)
4928{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04004930 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11004931 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 isec = msq->q_perm.security;
4934
Eric Paris50c205f2012-04-04 15:01:43 -04004935 ad.type = LSM_AUDIT_DATA_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936 ad.u.ipc_id = msq->q_perm.key;
4937
David Howells275bb412008-11-14 10:39:19 +11004938 return avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939 MSGQ__ASSOCIATE, &ad);
4940}
4941
4942static int selinux_msg_queue_msgctl(struct msg_queue *msq, int cmd)
4943{
4944 int err;
4945 int perms;
4946
Eric Paris828dfe12008-04-17 13:17:49 -04004947 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948 case IPC_INFO:
4949 case MSG_INFO:
4950 /* No specific object, just general system-wide information. */
4951 return task_has_system(current, SYSTEM__IPC_INFO);
4952 case IPC_STAT:
4953 case MSG_STAT:
4954 perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
4955 break;
4956 case IPC_SET:
4957 perms = MSGQ__SETATTR;
4958 break;
4959 case IPC_RMID:
4960 perms = MSGQ__DESTROY;
4961 break;
4962 default:
4963 return 0;
4964 }
4965
Stephen Smalley6af963f2005-05-01 08:58:39 -07004966 err = ipc_has_perm(&msq->q_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 return err;
4968}
4969
4970static int selinux_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg, int msqflg)
4971{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972 struct ipc_security_struct *isec;
4973 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04004974 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11004975 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976 int rc;
4977
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978 isec = msq->q_perm.security;
4979 msec = msg->security;
4980
4981 /*
4982 * First time through, need to assign label to the message
4983 */
4984 if (msec->sid == SECINITSID_UNLABELED) {
4985 /*
4986 * Compute new sid based on current process and
4987 * message queue this message will be stored in
4988 */
David Howells275bb412008-11-14 10:39:19 +11004989 rc = security_transition_sid(sid, isec->sid, SECCLASS_MSG,
Eric Paris652bb9b2011-02-01 11:05:40 -05004990 NULL, &msec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 if (rc)
4992 return rc;
4993 }
4994
Eric Paris50c205f2012-04-04 15:01:43 -04004995 ad.type = LSM_AUDIT_DATA_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996 ad.u.ipc_id = msq->q_perm.key;
4997
4998 /* Can this process write to the queue? */
David Howells275bb412008-11-14 10:39:19 +11004999 rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000 MSGQ__WRITE, &ad);
5001 if (!rc)
5002 /* Can this process send the message */
David Howells275bb412008-11-14 10:39:19 +11005003 rc = avc_has_perm(sid, msec->sid, SECCLASS_MSG,
5004 MSG__SEND, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005 if (!rc)
5006 /* Can the message be put in the queue? */
David Howells275bb412008-11-14 10:39:19 +11005007 rc = avc_has_perm(msec->sid, isec->sid, SECCLASS_MSGQ,
5008 MSGQ__ENQUEUE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009
5010 return rc;
5011}
5012
5013static int selinux_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
5014 struct task_struct *target,
5015 long type, int mode)
5016{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017 struct ipc_security_struct *isec;
5018 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005019 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005020 u32 sid = task_sid(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021 int rc;
5022
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023 isec = msq->q_perm.security;
5024 msec = msg->security;
5025
Eric Paris50c205f2012-04-04 15:01:43 -04005026 ad.type = LSM_AUDIT_DATA_IPC;
Eric Paris828dfe12008-04-17 13:17:49 -04005027 ad.u.ipc_id = msq->q_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028
David Howells275bb412008-11-14 10:39:19 +11005029 rc = avc_has_perm(sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030 SECCLASS_MSGQ, MSGQ__READ, &ad);
5031 if (!rc)
David Howells275bb412008-11-14 10:39:19 +11005032 rc = avc_has_perm(sid, msec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 SECCLASS_MSG, MSG__RECEIVE, &ad);
5034 return rc;
5035}
5036
5037/* Shared Memory security operations */
5038static int selinux_shm_alloc_security(struct shmid_kernel *shp)
5039{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005041 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005042 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 int rc;
5044
5045 rc = ipc_alloc_security(current, &shp->shm_perm, SECCLASS_SHM);
5046 if (rc)
5047 return rc;
5048
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049 isec = shp->shm_perm.security;
5050
Eric Paris50c205f2012-04-04 15:01:43 -04005051 ad.type = LSM_AUDIT_DATA_IPC;
Eric Paris828dfe12008-04-17 13:17:49 -04005052 ad.u.ipc_id = shp->shm_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053
David Howells275bb412008-11-14 10:39:19 +11005054 rc = avc_has_perm(sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055 SHM__CREATE, &ad);
5056 if (rc) {
5057 ipc_free_security(&shp->shm_perm);
5058 return rc;
5059 }
5060 return 0;
5061}
5062
5063static void selinux_shm_free_security(struct shmid_kernel *shp)
5064{
5065 ipc_free_security(&shp->shm_perm);
5066}
5067
5068static int selinux_shm_associate(struct shmid_kernel *shp, int shmflg)
5069{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005071 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005072 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 isec = shp->shm_perm.security;
5075
Eric Paris50c205f2012-04-04 15:01:43 -04005076 ad.type = LSM_AUDIT_DATA_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077 ad.u.ipc_id = shp->shm_perm.key;
5078
David Howells275bb412008-11-14 10:39:19 +11005079 return avc_has_perm(sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080 SHM__ASSOCIATE, &ad);
5081}
5082
5083/* Note, at this point, shp is locked down */
5084static int selinux_shm_shmctl(struct shmid_kernel *shp, int cmd)
5085{
5086 int perms;
5087 int err;
5088
Eric Paris828dfe12008-04-17 13:17:49 -04005089 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090 case IPC_INFO:
5091 case SHM_INFO:
5092 /* No specific object, just general system-wide information. */
5093 return task_has_system(current, SYSTEM__IPC_INFO);
5094 case IPC_STAT:
5095 case SHM_STAT:
5096 perms = SHM__GETATTR | SHM__ASSOCIATE;
5097 break;
5098 case IPC_SET:
5099 perms = SHM__SETATTR;
5100 break;
5101 case SHM_LOCK:
5102 case SHM_UNLOCK:
5103 perms = SHM__LOCK;
5104 break;
5105 case IPC_RMID:
5106 perms = SHM__DESTROY;
5107 break;
5108 default:
5109 return 0;
5110 }
5111
Stephen Smalley6af963f2005-05-01 08:58:39 -07005112 err = ipc_has_perm(&shp->shm_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113 return err;
5114}
5115
5116static int selinux_shm_shmat(struct shmid_kernel *shp,
5117 char __user *shmaddr, int shmflg)
5118{
5119 u32 perms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120
5121 if (shmflg & SHM_RDONLY)
5122 perms = SHM__READ;
5123 else
5124 perms = SHM__READ | SHM__WRITE;
5125
Stephen Smalley6af963f2005-05-01 08:58:39 -07005126 return ipc_has_perm(&shp->shm_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127}
5128
5129/* Semaphore security operations */
5130static int selinux_sem_alloc_security(struct sem_array *sma)
5131{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005133 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005134 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135 int rc;
5136
5137 rc = ipc_alloc_security(current, &sma->sem_perm, SECCLASS_SEM);
5138 if (rc)
5139 return rc;
5140
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 isec = sma->sem_perm.security;
5142
Eric Paris50c205f2012-04-04 15:01:43 -04005143 ad.type = LSM_AUDIT_DATA_IPC;
Eric Paris828dfe12008-04-17 13:17:49 -04005144 ad.u.ipc_id = sma->sem_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145
David Howells275bb412008-11-14 10:39:19 +11005146 rc = avc_has_perm(sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147 SEM__CREATE, &ad);
5148 if (rc) {
5149 ipc_free_security(&sma->sem_perm);
5150 return rc;
5151 }
5152 return 0;
5153}
5154
5155static void selinux_sem_free_security(struct sem_array *sma)
5156{
5157 ipc_free_security(&sma->sem_perm);
5158}
5159
5160static int selinux_sem_associate(struct sem_array *sma, int semflg)
5161{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005163 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005164 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166 isec = sma->sem_perm.security;
5167
Eric Paris50c205f2012-04-04 15:01:43 -04005168 ad.type = LSM_AUDIT_DATA_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169 ad.u.ipc_id = sma->sem_perm.key;
5170
David Howells275bb412008-11-14 10:39:19 +11005171 return avc_has_perm(sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172 SEM__ASSOCIATE, &ad);
5173}
5174
5175/* Note, at this point, sma is locked down */
5176static int selinux_sem_semctl(struct sem_array *sma, int cmd)
5177{
5178 int err;
5179 u32 perms;
5180
Eric Paris828dfe12008-04-17 13:17:49 -04005181 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182 case IPC_INFO:
5183 case SEM_INFO:
5184 /* No specific object, just general system-wide information. */
5185 return task_has_system(current, SYSTEM__IPC_INFO);
5186 case GETPID:
5187 case GETNCNT:
5188 case GETZCNT:
5189 perms = SEM__GETATTR;
5190 break;
5191 case GETVAL:
5192 case GETALL:
5193 perms = SEM__READ;
5194 break;
5195 case SETVAL:
5196 case SETALL:
5197 perms = SEM__WRITE;
5198 break;
5199 case IPC_RMID:
5200 perms = SEM__DESTROY;
5201 break;
5202 case IPC_SET:
5203 perms = SEM__SETATTR;
5204 break;
5205 case IPC_STAT:
5206 case SEM_STAT:
5207 perms = SEM__GETATTR | SEM__ASSOCIATE;
5208 break;
5209 default:
5210 return 0;
5211 }
5212
Stephen Smalley6af963f2005-05-01 08:58:39 -07005213 err = ipc_has_perm(&sma->sem_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214 return err;
5215}
5216
5217static int selinux_sem_semop(struct sem_array *sma,
5218 struct sembuf *sops, unsigned nsops, int alter)
5219{
5220 u32 perms;
5221
5222 if (alter)
5223 perms = SEM__READ | SEM__WRITE;
5224 else
5225 perms = SEM__READ;
5226
Stephen Smalley6af963f2005-05-01 08:58:39 -07005227 return ipc_has_perm(&sma->sem_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228}
5229
5230static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
5231{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232 u32 av = 0;
5233
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234 av = 0;
5235 if (flag & S_IRUGO)
5236 av |= IPC__UNIX_READ;
5237 if (flag & S_IWUGO)
5238 av |= IPC__UNIX_WRITE;
5239
5240 if (av == 0)
5241 return 0;
5242
Stephen Smalley6af963f2005-05-01 08:58:39 -07005243 return ipc_has_perm(ipcp, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244}
5245
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02005246static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
5247{
5248 struct ipc_security_struct *isec = ipcp->security;
5249 *secid = isec->sid;
5250}
5251
Eric Paris828dfe12008-04-17 13:17:49 -04005252static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253{
5254 if (inode)
5255 inode_doinit_with_dentry(inode, dentry);
5256}
5257
5258static int selinux_getprocattr(struct task_struct *p,
Al Viro04ff9702007-03-12 16:17:58 +00005259 char *name, char **value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260{
David Howells275bb412008-11-14 10:39:19 +11005261 const struct task_security_struct *__tsec;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00005262 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 int error;
Al Viro04ff9702007-03-12 16:17:58 +00005264 unsigned len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265
5266 if (current != p) {
David Howells3b11a1d2008-11-14 10:39:26 +11005267 error = current_has_perm(p, PROCESS__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268 if (error)
5269 return error;
5270 }
5271
David Howells275bb412008-11-14 10:39:19 +11005272 rcu_read_lock();
5273 __tsec = __task_cred(p)->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274
5275 if (!strcmp(name, "current"))
David Howells275bb412008-11-14 10:39:19 +11005276 sid = __tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277 else if (!strcmp(name, "prev"))
David Howells275bb412008-11-14 10:39:19 +11005278 sid = __tsec->osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279 else if (!strcmp(name, "exec"))
David Howells275bb412008-11-14 10:39:19 +11005280 sid = __tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281 else if (!strcmp(name, "fscreate"))
David Howells275bb412008-11-14 10:39:19 +11005282 sid = __tsec->create_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07005283 else if (!strcmp(name, "keycreate"))
David Howells275bb412008-11-14 10:39:19 +11005284 sid = __tsec->keycreate_sid;
Eric Paris42c3e032006-06-26 00:26:03 -07005285 else if (!strcmp(name, "sockcreate"))
David Howells275bb412008-11-14 10:39:19 +11005286 sid = __tsec->sockcreate_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287 else
David Howells275bb412008-11-14 10:39:19 +11005288 goto invalid;
5289 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290
5291 if (!sid)
5292 return 0;
5293
Al Viro04ff9702007-03-12 16:17:58 +00005294 error = security_sid_to_context(sid, value, &len);
5295 if (error)
5296 return error;
5297 return len;
David Howells275bb412008-11-14 10:39:19 +11005298
5299invalid:
5300 rcu_read_unlock();
5301 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302}
5303
5304static int selinux_setprocattr(struct task_struct *p,
5305 char *name, void *value, size_t size)
5306{
5307 struct task_security_struct *tsec;
Roland McGrath03563572008-03-26 15:46:39 -07005308 struct task_struct *tracer;
David Howellsd84f4f92008-11-14 10:39:23 +11005309 struct cred *new;
5310 u32 sid = 0, ptsid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311 int error;
5312 char *str = value;
5313
5314 if (current != p) {
5315 /* SELinux only allows a process to change its own
5316 security attributes. */
5317 return -EACCES;
5318 }
5319
5320 /*
5321 * Basic control over ability to set these attributes at all.
5322 * current == p, but we'll pass them separately in case the
5323 * above restriction is ever removed.
5324 */
5325 if (!strcmp(name, "exec"))
David Howells3b11a1d2008-11-14 10:39:26 +11005326 error = current_has_perm(p, PROCESS__SETEXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327 else if (!strcmp(name, "fscreate"))
David Howells3b11a1d2008-11-14 10:39:26 +11005328 error = current_has_perm(p, PROCESS__SETFSCREATE);
Michael LeMay4eb582c2006-06-26 00:24:57 -07005329 else if (!strcmp(name, "keycreate"))
David Howells3b11a1d2008-11-14 10:39:26 +11005330 error = current_has_perm(p, PROCESS__SETKEYCREATE);
Eric Paris42c3e032006-06-26 00:26:03 -07005331 else if (!strcmp(name, "sockcreate"))
David Howells3b11a1d2008-11-14 10:39:26 +11005332 error = current_has_perm(p, PROCESS__SETSOCKCREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333 else if (!strcmp(name, "current"))
David Howells3b11a1d2008-11-14 10:39:26 +11005334 error = current_has_perm(p, PROCESS__SETCURRENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335 else
5336 error = -EINVAL;
5337 if (error)
5338 return error;
5339
5340 /* Obtain a SID for the context, if one was specified. */
5341 if (size && str[1] && str[1] != '\n') {
5342 if (str[size-1] == '\n') {
5343 str[size-1] = 0;
5344 size--;
5345 }
5346 error = security_context_to_sid(value, size, &sid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04005347 if (error == -EINVAL && !strcmp(name, "fscreate")) {
Eric Parisd6ea83e2012-04-04 13:45:49 -04005348 if (!capable(CAP_MAC_ADMIN)) {
5349 struct audit_buffer *ab;
5350 size_t audit_size;
5351
5352 /* We strip a nul only if it is at the end, otherwise the
5353 * context contains a nul and we should audit that */
5354 if (str[size - 1] == '\0')
5355 audit_size = size - 1;
5356 else
5357 audit_size = size;
5358 ab = audit_log_start(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR);
5359 audit_log_format(ab, "op=fscreate invalid_context=");
5360 audit_log_n_untrustedstring(ab, value, audit_size);
5361 audit_log_end(ab);
5362
Stephen Smalley12b29f32008-05-07 13:03:20 -04005363 return error;
Eric Parisd6ea83e2012-04-04 13:45:49 -04005364 }
Stephen Smalley12b29f32008-05-07 13:03:20 -04005365 error = security_context_to_sid_force(value, size,
5366 &sid);
5367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368 if (error)
5369 return error;
5370 }
5371
David Howellsd84f4f92008-11-14 10:39:23 +11005372 new = prepare_creds();
5373 if (!new)
5374 return -ENOMEM;
5375
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376 /* Permission checking based on the specified context is
5377 performed during the actual operation (execve,
5378 open/mkdir/...), when we know the full context of the
David Howellsd84f4f92008-11-14 10:39:23 +11005379 operation. See selinux_bprm_set_creds for the execve
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380 checks and may_create for the file creation checks. The
5381 operation will then fail if the context is not permitted. */
David Howellsd84f4f92008-11-14 10:39:23 +11005382 tsec = new->security;
5383 if (!strcmp(name, "exec")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384 tsec->exec_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005385 } else if (!strcmp(name, "fscreate")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386 tsec->create_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005387 } else if (!strcmp(name, "keycreate")) {
Michael LeMay4eb582c2006-06-26 00:24:57 -07005388 error = may_create_key(sid, p);
5389 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11005390 goto abort_change;
Michael LeMay4eb582c2006-06-26 00:24:57 -07005391 tsec->keycreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005392 } else if (!strcmp(name, "sockcreate")) {
Eric Paris42c3e032006-06-26 00:26:03 -07005393 tsec->sockcreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005394 } else if (!strcmp(name, "current")) {
5395 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396 if (sid == 0)
David Howellsd84f4f92008-11-14 10:39:23 +11005397 goto abort_change;
KaiGai Koheid9250de2008-08-28 16:35:57 +09005398
David Howellsd84f4f92008-11-14 10:39:23 +11005399 /* Only allow single threaded processes to change context */
5400 error = -EPERM;
Oleg Nesterov5bb459b2009-07-10 03:48:23 +02005401 if (!current_is_single_threaded()) {
David Howellsd84f4f92008-11-14 10:39:23 +11005402 error = security_bounded_transition(tsec->sid, sid);
5403 if (error)
5404 goto abort_change;
Eric Paris828dfe12008-04-17 13:17:49 -04005405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406
5407 /* Check permissions for the transition. */
5408 error = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS,
Eric Paris828dfe12008-04-17 13:17:49 -04005409 PROCESS__DYNTRANSITION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11005411 goto abort_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412
5413 /* Check for ptracing, and update the task SID if ok.
5414 Otherwise, leave SID unchanged and fail. */
David Howellsd84f4f92008-11-14 10:39:23 +11005415 ptsid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416 task_lock(p);
Tejun Heo06d98472011-06-17 16:50:40 +02005417 tracer = ptrace_parent(p);
David Howellsd84f4f92008-11-14 10:39:23 +11005418 if (tracer)
5419 ptsid = task_sid(tracer);
5420 task_unlock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421
David Howellsd84f4f92008-11-14 10:39:23 +11005422 if (tracer) {
5423 error = avc_has_perm(ptsid, sid, SECCLASS_PROCESS,
5424 PROCESS__PTRACE, NULL);
5425 if (error)
5426 goto abort_change;
5427 }
5428
5429 tsec->sid = sid;
5430 } else {
5431 error = -EINVAL;
5432 goto abort_change;
5433 }
5434
5435 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 return size;
David Howellsd84f4f92008-11-14 10:39:23 +11005437
5438abort_change:
5439 abort_creds(new);
5440 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441}
5442
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005443static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
5444{
5445 return security_sid_to_context(secid, secdata, seclen);
5446}
5447
David Howells7bf570d2008-04-29 20:52:51 +01005448static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
David Howells63cb3442008-01-15 23:47:35 +00005449{
5450 return security_context_to_sid(secdata, seclen, secid);
5451}
5452
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005453static void selinux_release_secctx(char *secdata, u32 seclen)
5454{
Paul Moore088999e2007-08-01 11:12:58 -04005455 kfree(secdata);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005456}
5457
David P. Quigley1ee65e32009-09-03 14:25:57 -04005458/*
5459 * called with inode->i_mutex locked
5460 */
5461static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
5462{
5463 return selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, ctx, ctxlen, 0);
5464}
5465
5466/*
5467 * called with inode->i_mutex locked
5468 */
5469static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
5470{
5471 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0);
5472}
5473
5474static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
5475{
5476 int len = 0;
5477 len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX,
5478 ctx, true);
5479 if (len < 0)
5480 return len;
5481 *ctxlen = len;
5482 return 0;
5483}
Michael LeMayd7200242006-06-22 14:47:17 -07005484#ifdef CONFIG_KEYS
5485
David Howellsd84f4f92008-11-14 10:39:23 +11005486static int selinux_key_alloc(struct key *k, const struct cred *cred,
David Howells7e047ef2006-06-26 00:24:50 -07005487 unsigned long flags)
Michael LeMayd7200242006-06-22 14:47:17 -07005488{
David Howellsd84f4f92008-11-14 10:39:23 +11005489 const struct task_security_struct *tsec;
Michael LeMayd7200242006-06-22 14:47:17 -07005490 struct key_security_struct *ksec;
5491
5492 ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
5493 if (!ksec)
5494 return -ENOMEM;
5495
David Howellsd84f4f92008-11-14 10:39:23 +11005496 tsec = cred->security;
5497 if (tsec->keycreate_sid)
5498 ksec->sid = tsec->keycreate_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07005499 else
David Howellsd84f4f92008-11-14 10:39:23 +11005500 ksec->sid = tsec->sid;
Michael LeMayd7200242006-06-22 14:47:17 -07005501
David Howells275bb412008-11-14 10:39:19 +11005502 k->security = ksec;
Michael LeMayd7200242006-06-22 14:47:17 -07005503 return 0;
5504}
5505
5506static void selinux_key_free(struct key *k)
5507{
5508 struct key_security_struct *ksec = k->security;
5509
5510 k->security = NULL;
5511 kfree(ksec);
5512}
5513
5514static int selinux_key_permission(key_ref_t key_ref,
David Howellsd84f4f92008-11-14 10:39:23 +11005515 const struct cred *cred,
5516 key_perm_t perm)
Michael LeMayd7200242006-06-22 14:47:17 -07005517{
5518 struct key *key;
Michael LeMayd7200242006-06-22 14:47:17 -07005519 struct key_security_struct *ksec;
David Howells275bb412008-11-14 10:39:19 +11005520 u32 sid;
Michael LeMayd7200242006-06-22 14:47:17 -07005521
5522 /* if no specific permissions are requested, we skip the
5523 permission check. No serious, additional covert channels
5524 appear to be created. */
5525 if (perm == 0)
5526 return 0;
5527
David Howellsd84f4f92008-11-14 10:39:23 +11005528 sid = cred_sid(cred);
David Howells275bb412008-11-14 10:39:19 +11005529
5530 key = key_ref_to_ptr(key_ref);
5531 ksec = key->security;
5532
5533 return avc_has_perm(sid, ksec->sid, SECCLASS_KEY, perm, NULL);
Michael LeMayd7200242006-06-22 14:47:17 -07005534}
5535
David Howells70a5bb72008-04-29 01:01:26 -07005536static int selinux_key_getsecurity(struct key *key, char **_buffer)
5537{
5538 struct key_security_struct *ksec = key->security;
5539 char *context = NULL;
5540 unsigned len;
5541 int rc;
5542
5543 rc = security_sid_to_context(ksec->sid, &context, &len);
5544 if (!rc)
5545 rc = len;
5546 *_buffer = context;
5547 return rc;
5548}
5549
Michael LeMayd7200242006-06-22 14:47:17 -07005550#endif
5551
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552static struct security_operations selinux_ops = {
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02005553 .name = "selinux",
5554
Ingo Molnar9e488582009-05-07 19:26:19 +10005555 .ptrace_access_check = selinux_ptrace_access_check,
David Howells5cd9c582008-08-14 11:37:28 +01005556 .ptrace_traceme = selinux_ptrace_traceme,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557 .capget = selinux_capget,
David Howellsd84f4f92008-11-14 10:39:23 +11005558 .capset = selinux_capset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559 .capable = selinux_capable,
5560 .quotactl = selinux_quotactl,
5561 .quota_on = selinux_quota_on,
5562 .syslog = selinux_syslog,
5563 .vm_enough_memory = selinux_vm_enough_memory,
5564
5565 .netlink_send = selinux_netlink_send,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566
David Howellsa6f76f22008-11-14 10:39:24 +11005567 .bprm_set_creds = selinux_bprm_set_creds,
David Howellsa6f76f22008-11-14 10:39:24 +11005568 .bprm_committing_creds = selinux_bprm_committing_creds,
5569 .bprm_committed_creds = selinux_bprm_committed_creds,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570 .bprm_secureexec = selinux_bprm_secureexec,
5571
5572 .sb_alloc_security = selinux_sb_alloc_security,
5573 .sb_free_security = selinux_sb_free_security,
5574 .sb_copy_data = selinux_sb_copy_data,
Eric Paris026eb162011-03-03 16:09:14 -05005575 .sb_remount = selinux_sb_remount,
Eric Paris828dfe12008-04-17 13:17:49 -04005576 .sb_kern_mount = selinux_sb_kern_mount,
Eric Paris2069f452008-07-04 09:47:13 +10005577 .sb_show_options = selinux_sb_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578 .sb_statfs = selinux_sb_statfs,
5579 .sb_mount = selinux_mount,
5580 .sb_umount = selinux_umount,
Eric Parisc9180a52007-11-30 13:00:35 -05005581 .sb_set_mnt_opts = selinux_set_mnt_opts,
Eric Paris828dfe12008-04-17 13:17:49 -04005582 .sb_clone_mnt_opts = selinux_sb_clone_mnt_opts,
Eric Parise0007522008-03-05 10:31:54 -05005583 .sb_parse_opts_str = selinux_parse_opts_str,
5584
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585
5586 .inode_alloc_security = selinux_inode_alloc_security,
5587 .inode_free_security = selinux_inode_free_security,
Stephen Smalley5e41ff92005-09-09 13:01:35 -07005588 .inode_init_security = selinux_inode_init_security,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589 .inode_create = selinux_inode_create,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590 .inode_link = selinux_inode_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591 .inode_unlink = selinux_inode_unlink,
5592 .inode_symlink = selinux_inode_symlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593 .inode_mkdir = selinux_inode_mkdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594 .inode_rmdir = selinux_inode_rmdir,
5595 .inode_mknod = selinux_inode_mknod,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596 .inode_rename = selinux_inode_rename,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597 .inode_readlink = selinux_inode_readlink,
5598 .inode_follow_link = selinux_inode_follow_link,
5599 .inode_permission = selinux_inode_permission,
5600 .inode_setattr = selinux_inode_setattr,
5601 .inode_getattr = selinux_inode_getattr,
5602 .inode_setxattr = selinux_inode_setxattr,
5603 .inode_post_setxattr = selinux_inode_post_setxattr,
5604 .inode_getxattr = selinux_inode_getxattr,
5605 .inode_listxattr = selinux_inode_listxattr,
5606 .inode_removexattr = selinux_inode_removexattr,
Eric Paris828dfe12008-04-17 13:17:49 -04005607 .inode_getsecurity = selinux_inode_getsecurity,
5608 .inode_setsecurity = selinux_inode_setsecurity,
5609 .inode_listsecurity = selinux_inode_listsecurity,
Eric Parisf5269712008-05-14 11:27:45 -04005610 .inode_getsecid = selinux_inode_getsecid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611
5612 .file_permission = selinux_file_permission,
5613 .file_alloc_security = selinux_file_alloc_security,
5614 .file_free_security = selinux_file_free_security,
5615 .file_ioctl = selinux_file_ioctl,
Al Viroe5467852012-05-30 13:30:51 -04005616 .mmap_file = selinux_mmap_file,
5617 .mmap_addr = selinux_mmap_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618 .file_mprotect = selinux_file_mprotect,
5619 .file_lock = selinux_file_lock,
5620 .file_fcntl = selinux_file_fcntl,
5621 .file_set_fowner = selinux_file_set_fowner,
5622 .file_send_sigiotask = selinux_file_send_sigiotask,
5623 .file_receive = selinux_file_receive,
5624
Eric Paris83d49852012-04-04 13:45:40 -04005625 .file_open = selinux_file_open,
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09005626
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627 .task_create = selinux_task_create,
David Howellsee18d642009-09-02 09:14:21 +01005628 .cred_alloc_blank = selinux_cred_alloc_blank,
David Howellsf1752ee2008-11-14 10:39:17 +11005629 .cred_free = selinux_cred_free,
David Howellsd84f4f92008-11-14 10:39:23 +11005630 .cred_prepare = selinux_cred_prepare,
David Howellsee18d642009-09-02 09:14:21 +01005631 .cred_transfer = selinux_cred_transfer,
David Howells3a3b7ce2008-11-14 10:39:28 +11005632 .kernel_act_as = selinux_kernel_act_as,
5633 .kernel_create_files_as = selinux_kernel_create_files_as,
Eric Paris25354c42009-08-13 09:45:03 -04005634 .kernel_module_request = selinux_kernel_module_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 .task_setpgid = selinux_task_setpgid,
5636 .task_getpgid = selinux_task_getpgid,
Eric Paris828dfe12008-04-17 13:17:49 -04005637 .task_getsid = selinux_task_getsid,
David Quigleyf9008e4c2006-06-30 01:55:46 -07005638 .task_getsecid = selinux_task_getsecid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639 .task_setnice = selinux_task_setnice,
James Morris03e68062006-06-23 02:03:58 -07005640 .task_setioprio = selinux_task_setioprio,
David Quigleya1836a42006-06-30 01:55:49 -07005641 .task_getioprio = selinux_task_getioprio,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642 .task_setrlimit = selinux_task_setrlimit,
5643 .task_setscheduler = selinux_task_setscheduler,
5644 .task_getscheduler = selinux_task_getscheduler,
David Quigley35601542006-06-23 02:04:01 -07005645 .task_movememory = selinux_task_movememory,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646 .task_kill = selinux_task_kill,
5647 .task_wait = selinux_task_wait,
Eric Paris828dfe12008-04-17 13:17:49 -04005648 .task_to_inode = selinux_task_to_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649
5650 .ipc_permission = selinux_ipc_permission,
Eric Parisf5269712008-05-14 11:27:45 -04005651 .ipc_getsecid = selinux_ipc_getsecid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652
5653 .msg_msg_alloc_security = selinux_msg_msg_alloc_security,
5654 .msg_msg_free_security = selinux_msg_msg_free_security,
5655
5656 .msg_queue_alloc_security = selinux_msg_queue_alloc_security,
5657 .msg_queue_free_security = selinux_msg_queue_free_security,
5658 .msg_queue_associate = selinux_msg_queue_associate,
5659 .msg_queue_msgctl = selinux_msg_queue_msgctl,
5660 .msg_queue_msgsnd = selinux_msg_queue_msgsnd,
5661 .msg_queue_msgrcv = selinux_msg_queue_msgrcv,
5662
5663 .shm_alloc_security = selinux_shm_alloc_security,
5664 .shm_free_security = selinux_shm_free_security,
5665 .shm_associate = selinux_shm_associate,
5666 .shm_shmctl = selinux_shm_shmctl,
5667 .shm_shmat = selinux_shm_shmat,
5668
Eric Paris828dfe12008-04-17 13:17:49 -04005669 .sem_alloc_security = selinux_sem_alloc_security,
5670 .sem_free_security = selinux_sem_free_security,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671 .sem_associate = selinux_sem_associate,
5672 .sem_semctl = selinux_sem_semctl,
5673 .sem_semop = selinux_sem_semop,
5674
Eric Paris828dfe12008-04-17 13:17:49 -04005675 .d_instantiate = selinux_d_instantiate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676
Eric Paris828dfe12008-04-17 13:17:49 -04005677 .getprocattr = selinux_getprocattr,
5678 .setprocattr = selinux_setprocattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005680 .secid_to_secctx = selinux_secid_to_secctx,
David Howells63cb3442008-01-15 23:47:35 +00005681 .secctx_to_secid = selinux_secctx_to_secid,
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005682 .release_secctx = selinux_release_secctx,
David P. Quigley1ee65e32009-09-03 14:25:57 -04005683 .inode_notifysecctx = selinux_inode_notifysecctx,
5684 .inode_setsecctx = selinux_inode_setsecctx,
5685 .inode_getsecctx = selinux_inode_getsecctx,
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005686
Eric Paris828dfe12008-04-17 13:17:49 -04005687 .unix_stream_connect = selinux_socket_unix_stream_connect,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688 .unix_may_send = selinux_socket_unix_may_send,
5689
5690 .socket_create = selinux_socket_create,
5691 .socket_post_create = selinux_socket_post_create,
5692 .socket_bind = selinux_socket_bind,
5693 .socket_connect = selinux_socket_connect,
5694 .socket_listen = selinux_socket_listen,
5695 .socket_accept = selinux_socket_accept,
5696 .socket_sendmsg = selinux_socket_sendmsg,
5697 .socket_recvmsg = selinux_socket_recvmsg,
5698 .socket_getsockname = selinux_socket_getsockname,
5699 .socket_getpeername = selinux_socket_getpeername,
5700 .socket_getsockopt = selinux_socket_getsockopt,
5701 .socket_setsockopt = selinux_socket_setsockopt,
5702 .socket_shutdown = selinux_socket_shutdown,
5703 .socket_sock_rcv_skb = selinux_socket_sock_rcv_skb,
Catherine Zhang2c7946a2006-03-20 22:41:23 -08005704 .socket_getpeersec_stream = selinux_socket_getpeersec_stream,
5705 .socket_getpeersec_dgram = selinux_socket_getpeersec_dgram,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706 .sk_alloc_security = selinux_sk_alloc_security,
5707 .sk_free_security = selinux_sk_free_security,
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005708 .sk_clone_security = selinux_sk_clone_security,
Eric Paris828dfe12008-04-17 13:17:49 -04005709 .sk_getsecid = selinux_sk_getsecid,
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005710 .sock_graft = selinux_sock_graft,
5711 .inet_conn_request = selinux_inet_conn_request,
5712 .inet_csk_clone = selinux_inet_csk_clone,
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005713 .inet_conn_established = selinux_inet_conn_established,
Eric Paris2606fd12010-10-13 16:24:41 -04005714 .secmark_relabel_packet = selinux_secmark_relabel_packet,
5715 .secmark_refcount_inc = selinux_secmark_refcount_inc,
5716 .secmark_refcount_dec = selinux_secmark_refcount_dec,
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005717 .req_classify_flow = selinux_req_classify_flow,
Paul Moore5dbbaf22013-01-14 07:12:19 +00005718 .tun_dev_alloc_security = selinux_tun_dev_alloc_security,
5719 .tun_dev_free_security = selinux_tun_dev_free_security,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005720 .tun_dev_create = selinux_tun_dev_create,
Paul Moore5dbbaf22013-01-14 07:12:19 +00005721 .tun_dev_attach_queue = selinux_tun_dev_attach_queue,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005722 .tun_dev_attach = selinux_tun_dev_attach,
Paul Moore5dbbaf22013-01-14 07:12:19 +00005723 .tun_dev_open = selinux_tun_dev_open,
Eric Dumazetca10b9e2013-04-08 17:58:11 +00005724 .skb_owned_by = selinux_skb_owned_by,
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005725
5726#ifdef CONFIG_SECURITY_NETWORK_XFRM
5727 .xfrm_policy_alloc_security = selinux_xfrm_policy_alloc,
5728 .xfrm_policy_clone_security = selinux_xfrm_policy_clone,
5729 .xfrm_policy_free_security = selinux_xfrm_policy_free,
Catherine Zhangc8c05a82006-06-08 23:39:49 -07005730 .xfrm_policy_delete_security = selinux_xfrm_policy_delete,
Paul Moore2e5aa862013-07-23 17:38:38 -04005731 .xfrm_state_alloc = selinux_xfrm_state_alloc,
5732 .xfrm_state_alloc_acquire = selinux_xfrm_state_alloc_acquire,
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005733 .xfrm_state_free_security = selinux_xfrm_state_free,
Catherine Zhangc8c05a82006-06-08 23:39:49 -07005734 .xfrm_state_delete_security = selinux_xfrm_state_delete,
Eric Paris828dfe12008-04-17 13:17:49 -04005735 .xfrm_policy_lookup = selinux_xfrm_policy_lookup,
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07005736 .xfrm_state_pol_flow_match = selinux_xfrm_state_pol_flow_match,
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07005737 .xfrm_decode_session = selinux_xfrm_decode_session,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738#endif
Michael LeMayd7200242006-06-22 14:47:17 -07005739
5740#ifdef CONFIG_KEYS
Eric Paris828dfe12008-04-17 13:17:49 -04005741 .key_alloc = selinux_key_alloc,
5742 .key_free = selinux_key_free,
5743 .key_permission = selinux_key_permission,
David Howells70a5bb72008-04-29 01:01:26 -07005744 .key_getsecurity = selinux_key_getsecurity,
Michael LeMayd7200242006-06-22 14:47:17 -07005745#endif
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02005746
5747#ifdef CONFIG_AUDIT
5748 .audit_rule_init = selinux_audit_rule_init,
5749 .audit_rule_known = selinux_audit_rule_known,
5750 .audit_rule_match = selinux_audit_rule_match,
5751 .audit_rule_free = selinux_audit_rule_free,
5752#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753};
5754
5755static __init int selinux_init(void)
5756{
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02005757 if (!security_module_enable(&selinux_ops)) {
5758 selinux_enabled = 0;
5759 return 0;
5760 }
5761
Linus Torvalds1da177e2005-04-16 15:20:36 -07005762 if (!selinux_enabled) {
5763 printk(KERN_INFO "SELinux: Disabled at boot.\n");
5764 return 0;
5765 }
5766
5767 printk(KERN_INFO "SELinux: Initializing.\n");
5768
5769 /* Set the security state for the initial task. */
David Howellsd84f4f92008-11-14 10:39:23 +11005770 cred_init_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04005772 default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
5773
James Morris7cae7e22006-03-22 00:09:22 -08005774 sel_inode_cache = kmem_cache_create("selinux_inode_security",
5775 sizeof(struct inode_security_struct),
Paul Mundt20c2df82007-07-20 10:11:58 +09005776 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777 avc_init();
5778
Eric Paris828dfe12008-04-17 13:17:49 -04005779 if (register_security(&selinux_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780 panic("SELinux: Unable to register with kernel.\n");
5781
Eric Paris828dfe12008-04-17 13:17:49 -04005782 if (selinux_enforcing)
Eric Parisfadcdb42007-02-22 18:11:31 -05005783 printk(KERN_DEBUG "SELinux: Starting in enforcing mode\n");
Eric Paris828dfe12008-04-17 13:17:49 -04005784 else
Eric Parisfadcdb42007-02-22 18:11:31 -05005785 printk(KERN_DEBUG "SELinux: Starting in permissive mode\n");
Michael LeMayd7200242006-06-22 14:47:17 -07005786
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787 return 0;
5788}
5789
Al Viroe8c26252010-03-23 06:36:54 -04005790static void delayed_superblock_init(struct super_block *sb, void *unused)
5791{
5792 superblock_doinit(sb, NULL);
5793}
5794
Linus Torvalds1da177e2005-04-16 15:20:36 -07005795void selinux_complete_init(void)
5796{
Eric Parisfadcdb42007-02-22 18:11:31 -05005797 printk(KERN_DEBUG "SELinux: Completing initialization.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798
5799 /* Set up any superblocks initialized prior to the policy load. */
Eric Parisfadcdb42007-02-22 18:11:31 -05005800 printk(KERN_DEBUG "SELinux: Setting up existing superblocks.\n");
Al Viroe8c26252010-03-23 06:36:54 -04005801 iterate_supers(delayed_superblock_init, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802}
5803
5804/* SELinux requires early initialization in order to label
5805 all processes and objects when they are created. */
5806security_initcall(selinux_init);
5807
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08005808#if defined(CONFIG_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809
Paul Mooreeffad8d2008-01-29 08:49:27 -05005810static struct nf_hook_ops selinux_ipv4_ops[] = {
5811 {
5812 .hook = selinux_ipv4_postroute,
5813 .owner = THIS_MODULE,
Alban Crequy2597a832012-05-14 03:56:39 +00005814 .pf = NFPROTO_IPV4,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005815 .hooknum = NF_INET_POST_ROUTING,
5816 .priority = NF_IP_PRI_SELINUX_LAST,
5817 },
5818 {
5819 .hook = selinux_ipv4_forward,
5820 .owner = THIS_MODULE,
Alban Crequy2597a832012-05-14 03:56:39 +00005821 .pf = NFPROTO_IPV4,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005822 .hooknum = NF_INET_FORWARD,
5823 .priority = NF_IP_PRI_SELINUX_FIRST,
Paul Moore948bf852008-10-10 10:16:32 -04005824 },
5825 {
5826 .hook = selinux_ipv4_output,
5827 .owner = THIS_MODULE,
Alban Crequy2597a832012-05-14 03:56:39 +00005828 .pf = NFPROTO_IPV4,
Paul Moore948bf852008-10-10 10:16:32 -04005829 .hooknum = NF_INET_LOCAL_OUT,
5830 .priority = NF_IP_PRI_SELINUX_FIRST,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832};
5833
5834#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
5835
Paul Mooreeffad8d2008-01-29 08:49:27 -05005836static struct nf_hook_ops selinux_ipv6_ops[] = {
5837 {
5838 .hook = selinux_ipv6_postroute,
5839 .owner = THIS_MODULE,
Alban Crequy2597a832012-05-14 03:56:39 +00005840 .pf = NFPROTO_IPV6,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005841 .hooknum = NF_INET_POST_ROUTING,
5842 .priority = NF_IP6_PRI_SELINUX_LAST,
5843 },
5844 {
5845 .hook = selinux_ipv6_forward,
5846 .owner = THIS_MODULE,
Alban Crequy2597a832012-05-14 03:56:39 +00005847 .pf = NFPROTO_IPV6,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005848 .hooknum = NF_INET_FORWARD,
5849 .priority = NF_IP6_PRI_SELINUX_FIRST,
5850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851};
5852
5853#endif /* IPV6 */
5854
5855static int __init selinux_nf_ip_init(void)
5856{
5857 int err = 0;
5858
5859 if (!selinux_enabled)
5860 goto out;
Eric Parisfadcdb42007-02-22 18:11:31 -05005861
5862 printk(KERN_DEBUG "SELinux: Registering netfilter hooks\n");
5863
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07005864 err = nf_register_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops));
5865 if (err)
5866 panic("SELinux: nf_register_hooks for IPv4: error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867
5868#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07005869 err = nf_register_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops));
5870 if (err)
5871 panic("SELinux: nf_register_hooks for IPv6: error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005872#endif /* IPV6 */
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005873
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874out:
5875 return err;
5876}
5877
5878__initcall(selinux_nf_ip_init);
5879
5880#ifdef CONFIG_SECURITY_SELINUX_DISABLE
5881static void selinux_nf_ip_exit(void)
5882{
Eric Parisfadcdb42007-02-22 18:11:31 -05005883 printk(KERN_DEBUG "SELinux: Unregistering netfilter hooks\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005884
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07005885 nf_unregister_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07005887 nf_unregister_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888#endif /* IPV6 */
5889}
5890#endif
5891
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08005892#else /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005893
5894#ifdef CONFIG_SECURITY_SELINUX_DISABLE
5895#define selinux_nf_ip_exit()
5896#endif
5897
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08005898#endif /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899
5900#ifdef CONFIG_SECURITY_SELINUX_DISABLE
Eric Paris828dfe12008-04-17 13:17:49 -04005901static int selinux_disabled;
5902
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903int selinux_disable(void)
5904{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905 if (ss_initialized) {
5906 /* Not permitted after initial policy load. */
5907 return -EINVAL;
5908 }
5909
5910 if (selinux_disabled) {
5911 /* Only do this once. */
5912 return -EINVAL;
5913 }
5914
5915 printk(KERN_INFO "SELinux: Disabled at runtime.\n");
5916
5917 selinux_disabled = 1;
Stephen Smalley30d55282006-05-03 10:52:36 -04005918 selinux_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919
wzt.wzt@gmail.com189b3b12010-02-23 23:15:28 +08005920 reset_security_ops();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921
Eric Parisaf8ff042009-09-20 21:23:01 -04005922 /* Try to destroy the avc node cache */
5923 avc_disable();
5924
Linus Torvalds1da177e2005-04-16 15:20:36 -07005925 /* Unregister netfilter hooks. */
5926 selinux_nf_ip_exit();
5927
5928 /* Unregister selinuxfs. */
5929 exit_sel_fs();
5930
5931 return 0;
5932}
5933#endif