blob: 6625699f497c7f3889f7b82b325a315c9e51774a [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 Moore47180062013-12-04 16:10:45 -050056#include <net/inet_connection_sock.h>
Paul Moore220deb92008-01-29 08:38:23 -050057#include <net/net_namespace.h>
Paul Moored621d352008-01-29 08:43:36 -050058#include <net/netlabel.h>
Eric Parisf5269712008-05-14 11:27:45 -040059#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <asm/ioctls.h>
Arun Sharma600634972011-07-26 16:09:06 -070061#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <linux/bitops.h>
63#include <linux/interrupt.h>
64#include <linux/netdevice.h> /* for network interface checks */
Hong zhi guo77954982013-03-27 06:49:35 +000065#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <linux/tcp.h>
67#include <linux/udp.h>
James Morris2ee92d42006-11-13 16:09:01 -080068#include <linux/dccp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include <linux/quota.h>
70#include <linux/un.h> /* for Unix socket types */
71#include <net/af_unix.h> /* for Unix socket types */
72#include <linux/parser.h>
73#include <linux/nfs_mount.h>
74#include <net/ipv6.h>
75#include <linux/hugetlb.h>
76#include <linux/personality.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#include <linux/audit.h>
Eric Paris6931dfc2005-06-30 02:58:51 -070078#include <linux/string.h>
Catherine Zhang877ce7c2006-06-29 12:27:47 -070079#include <linux/selinux.h>
Eric Paris23970742006-09-25 23:32:01 -070080#include <linux/mutex.h>
Frank Mayharf06febc2008-09-12 09:54:39 -070081#include <linux/posix-timers.h>
Kees Cook00234592010-02-03 15:36:43 -080082#include <linux/syslog.h>
Serge E. Hallyn34867402011-03-23 16:43:17 -070083#include <linux/user_namespace.h>
Paul Gortmaker44fc7ea2011-05-26 20:52:10 -040084#include <linux/export.h>
David Quigleyeb9ae682013-05-22 12:50:37 -040085#include <linux/security.h>
Al Viro40401532012-02-13 03:58:52 +000086#include <linux/msg.h>
87#include <linux/shm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89#include "avc.h"
90#include "objsec.h"
91#include "netif.h"
Paul Moore224dfbd2008-01-29 08:38:13 -050092#include "netnode.h"
Paul Moore3e112172008-04-10 10:48:14 -040093#include "netport.h"
Trent Jaegerd28d1e02005-12-13 23:12:40 -080094#include "xfrm.h"
Paul Moorec60475b2007-02-28 15:14:23 -050095#include "netlabel.h"
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +020096#include "audit.h"
James Morris7b98a582011-08-30 12:52:32 +100097#include "avc_ss.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
James Morris20510f22007-10-16 23:31:32 -070099extern struct security_operations *security_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Paul Moored621d352008-01-29 08:43:36 -0500101/* SECMARK reference count */
James Morris56a4ca92011-08-17 11:08:43 +1000102static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0);
Paul Moored621d352008-01-29 08:43:36 -0500103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104#ifdef CONFIG_SECURITY_SELINUX_DEVELOP
Eric Paris828dfe12008-04-17 13:17:49 -0400105int selinux_enforcing;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107static int __init enforcing_setup(char *str)
108{
Eric Parisf5269712008-05-14 11:27:45 -0400109 unsigned long enforcing;
110 if (!strict_strtoul(str, 0, &enforcing))
111 selinux_enforcing = enforcing ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return 1;
113}
114__setup("enforcing=", enforcing_setup);
115#endif
116
117#ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM
118int selinux_enabled = CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE;
119
120static int __init selinux_enabled_setup(char *str)
121{
Eric Parisf5269712008-05-14 11:27:45 -0400122 unsigned long enabled;
123 if (!strict_strtoul(str, 0, &enabled))
124 selinux_enabled = enabled ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 return 1;
126}
127__setup("selinux=", selinux_enabled_setup);
Stephen Smalley30d55282006-05-03 10:52:36 -0400128#else
129int selinux_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130#endif
131
Christoph Lametere18b8902006-12-06 20:33:20 -0800132static struct kmem_cache *sel_inode_cache;
James Morris7cae7e22006-03-22 00:09:22 -0800133
Paul Moored621d352008-01-29 08:43:36 -0500134/**
135 * selinux_secmark_enabled - Check to see if SECMARK is currently enabled
136 *
137 * Description:
138 * This function checks the SECMARK reference counter to see if any SECMARK
139 * targets are currently configured, if the reference counter is greater than
140 * zero SECMARK is considered to be enabled. Returns true (1) if SECMARK is
Chris PeBenito2be4d742013-05-03 09:05:39 -0400141 * enabled, false (0) if SECMARK is disabled. If the always_check_network
142 * policy capability is enabled, SECMARK is always considered enabled.
Paul Moored621d352008-01-29 08:43:36 -0500143 *
144 */
145static int selinux_secmark_enabled(void)
146{
Chris PeBenito2be4d742013-05-03 09:05:39 -0400147 return (selinux_policycap_alwaysnetwork || atomic_read(&selinux_secmark_refcount));
148}
149
150/**
151 * selinux_peerlbl_enabled - Check to see if peer labeling is currently enabled
152 *
153 * Description:
154 * This function checks if NetLabel or labeled IPSEC is enabled. Returns true
155 * (1) if any are enabled or false (0) if neither are enabled. If the
156 * always_check_network policy capability is enabled, peer labeling
157 * is always considered enabled.
158 *
159 */
160static int selinux_peerlbl_enabled(void)
161{
162 return (selinux_policycap_alwaysnetwork || netlbl_enabled() || selinux_xfrm_enabled());
Paul Moored621d352008-01-29 08:43:36 -0500163}
164
David Howellsd84f4f92008-11-14 10:39:23 +1100165/*
166 * initialise the security for the init task
167 */
168static void cred_init_security(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
David Howells3b11a1d2008-11-14 10:39:26 +1100170 struct cred *cred = (struct cred *) current->real_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 struct task_security_struct *tsec;
172
James Morris89d155e2005-10-30 14:59:21 -0800173 tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 if (!tsec)
David Howellsd84f4f92008-11-14 10:39:23 +1100175 panic("SELinux: Failed to initialize initial task.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
David Howellsd84f4f92008-11-14 10:39:23 +1100177 tsec->osid = tsec->sid = SECINITSID_KERNEL;
David Howellsf1752ee2008-11-14 10:39:17 +1100178 cred->security = tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
David Howells275bb412008-11-14 10:39:19 +1100181/*
David Howells88e67f32008-11-14 10:39:21 +1100182 * get the security ID of a set of credentials
183 */
184static inline u32 cred_sid(const struct cred *cred)
185{
186 const struct task_security_struct *tsec;
187
188 tsec = cred->security;
189 return tsec->sid;
190}
191
192/*
David Howells3b11a1d2008-11-14 10:39:26 +1100193 * get the objective security ID of a task
David Howells275bb412008-11-14 10:39:19 +1100194 */
195static inline u32 task_sid(const struct task_struct *task)
196{
David Howells275bb412008-11-14 10:39:19 +1100197 u32 sid;
198
199 rcu_read_lock();
David Howells88e67f32008-11-14 10:39:21 +1100200 sid = cred_sid(__task_cred(task));
David Howells275bb412008-11-14 10:39:19 +1100201 rcu_read_unlock();
202 return sid;
203}
204
205/*
David Howells3b11a1d2008-11-14 10:39:26 +1100206 * get the subjective security ID of the current task
David Howells275bb412008-11-14 10:39:19 +1100207 */
208static inline u32 current_sid(void)
209{
Paul Moore5fb49872010-04-22 14:46:19 -0400210 const struct task_security_struct *tsec = current_security();
David Howells275bb412008-11-14 10:39:19 +1100211
212 return tsec->sid;
213}
214
David Howells88e67f32008-11-14 10:39:21 +1100215/* Allocate and free functions for each kind of security blob. */
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217static int inode_alloc_security(struct inode *inode)
218{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 struct inode_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +1100220 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Josef Bacika02fe132008-04-04 09:35:05 +1100222 isec = kmem_cache_zalloc(sel_inode_cache, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 if (!isec)
224 return -ENOMEM;
225
Eric Paris23970742006-09-25 23:32:01 -0700226 mutex_init(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 INIT_LIST_HEAD(&isec->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 isec->inode = inode;
229 isec->sid = SECINITSID_UNLABELED;
230 isec->sclass = SECCLASS_FILE;
David Howells275bb412008-11-14 10:39:19 +1100231 isec->task_sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 inode->i_security = isec;
233
234 return 0;
235}
236
237static void inode_free_security(struct inode *inode)
238{
239 struct inode_security_struct *isec = inode->i_security;
240 struct superblock_security_struct *sbsec = inode->i_sb->s_security;
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 spin_lock(&sbsec->isec_lock);
243 if (!list_empty(&isec->list))
244 list_del_init(&isec->list);
245 spin_unlock(&sbsec->isec_lock);
246
247 inode->i_security = NULL;
James Morris7cae7e22006-03-22 00:09:22 -0800248 kmem_cache_free(sel_inode_cache, isec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
251static int file_alloc_security(struct file *file)
252{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 struct file_security_struct *fsec;
David Howells275bb412008-11-14 10:39:19 +1100254 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Stephen Smalley26d2a4b2006-02-01 03:05:55 -0800256 fsec = kzalloc(sizeof(struct file_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 if (!fsec)
258 return -ENOMEM;
259
David Howells275bb412008-11-14 10:39:19 +1100260 fsec->sid = sid;
261 fsec->fown_sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 file->f_security = fsec;
263
264 return 0;
265}
266
267static void file_free_security(struct file *file)
268{
269 struct file_security_struct *fsec = file->f_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 file->f_security = NULL;
271 kfree(fsec);
272}
273
274static int superblock_alloc_security(struct super_block *sb)
275{
276 struct superblock_security_struct *sbsec;
277
James Morris89d155e2005-10-30 14:59:21 -0800278 sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 if (!sbsec)
280 return -ENOMEM;
281
Eric Parisbc7e9822006-09-25 23:32:02 -0700282 mutex_init(&sbsec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 INIT_LIST_HEAD(&sbsec->isec_head);
284 spin_lock_init(&sbsec->isec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 sbsec->sb = sb;
286 sbsec->sid = SECINITSID_UNLABELED;
287 sbsec->def_sid = SECINITSID_FILE;
Eric Parisc312feb2006-07-10 04:43:53 -0700288 sbsec->mntpoint_sid = SECINITSID_UNLABELED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 sb->s_security = sbsec;
290
291 return 0;
292}
293
294static void superblock_free_security(struct super_block *sb)
295{
296 struct superblock_security_struct *sbsec = sb->s_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 sb->s_security = NULL;
298 kfree(sbsec);
299}
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301/* The file system's label must be initialized prior to use. */
302
David Quigleyeb9ae682013-05-22 12:50:37 -0400303static const char *labeling_behaviors[7] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 "uses xattr",
305 "uses transition SIDs",
306 "uses task SIDs",
307 "uses genfs_contexts",
308 "not configured for labeling",
309 "uses mountpoint labeling",
David Quigleyeb9ae682013-05-22 12:50:37 -0400310 "uses native labeling",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311};
312
313static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry);
314
315static inline int inode_doinit(struct inode *inode)
316{
317 return inode_doinit_with_dentry(inode, NULL);
318}
319
320enum {
Eric Paris31e87932007-09-19 17:19:12 -0400321 Opt_error = -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 Opt_context = 1,
323 Opt_fscontext = 2,
Eric Parisc9180a52007-11-30 13:00:35 -0500324 Opt_defcontext = 3,
325 Opt_rootcontext = 4,
David P. Quigley11689d42009-01-16 09:22:03 -0500326 Opt_labelsupport = 5,
Eric Parisd355987f2012-08-24 15:58:53 -0400327 Opt_nextmntopt = 6,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328};
329
Eric Parisd355987f2012-08-24 15:58:53 -0400330#define NUM_SEL_MNT_OPTS (Opt_nextmntopt - 1)
331
Steven Whitehousea447c092008-10-13 10:46:57 +0100332static const match_table_t tokens = {
Eric Paris832cbd92008-04-01 13:24:09 -0400333 {Opt_context, CONTEXT_STR "%s"},
334 {Opt_fscontext, FSCONTEXT_STR "%s"},
335 {Opt_defcontext, DEFCONTEXT_STR "%s"},
336 {Opt_rootcontext, ROOTCONTEXT_STR "%s"},
David P. Quigley11689d42009-01-16 09:22:03 -0500337 {Opt_labelsupport, LABELSUPP_STR},
Eric Paris31e87932007-09-19 17:19:12 -0400338 {Opt_error, NULL},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339};
340
341#define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n"
342
Eric Parisc312feb2006-07-10 04:43:53 -0700343static int may_context_mount_sb_relabel(u32 sid,
344 struct superblock_security_struct *sbsec,
David Howells275bb412008-11-14 10:39:19 +1100345 const struct cred *cred)
Eric Parisc312feb2006-07-10 04:43:53 -0700346{
David Howells275bb412008-11-14 10:39:19 +1100347 const struct task_security_struct *tsec = cred->security;
Eric Parisc312feb2006-07-10 04:43:53 -0700348 int rc;
349
350 rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
351 FILESYSTEM__RELABELFROM, NULL);
352 if (rc)
353 return rc;
354
355 rc = avc_has_perm(tsec->sid, sid, SECCLASS_FILESYSTEM,
356 FILESYSTEM__RELABELTO, NULL);
357 return rc;
358}
359
Eric Paris08089252006-07-10 04:43:55 -0700360static int may_context_mount_inode_relabel(u32 sid,
361 struct superblock_security_struct *sbsec,
David Howells275bb412008-11-14 10:39:19 +1100362 const struct cred *cred)
Eric Paris08089252006-07-10 04:43:55 -0700363{
David Howells275bb412008-11-14 10:39:19 +1100364 const struct task_security_struct *tsec = cred->security;
Eric Paris08089252006-07-10 04:43:55 -0700365 int rc;
366 rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
367 FILESYSTEM__RELABELFROM, NULL);
368 if (rc)
369 return rc;
370
371 rc = avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM,
372 FILESYSTEM__ASSOCIATE, NULL);
373 return rc;
374}
375
Eric Parisb43e7252012-10-10 14:27:35 -0400376static int selinux_is_sblabel_mnt(struct super_block *sb)
377{
378 struct superblock_security_struct *sbsec = sb->s_security;
379
380 if (sbsec->behavior == SECURITY_FS_USE_XATTR ||
381 sbsec->behavior == SECURITY_FS_USE_TRANS ||
382 sbsec->behavior == SECURITY_FS_USE_TASK)
383 return 1;
384
385 /* Special handling for sysfs. Is genfs but also has setxattr handler*/
386 if (strncmp(sb->s_type->name, "sysfs", sizeof("sysfs")) == 0)
387 return 1;
388
389 /*
390 * Special handling for rootfs. Is genfs but supports
391 * setting SELinux context on in-core inodes.
392 */
393 if (strncmp(sb->s_type->name, "rootfs", sizeof("rootfs")) == 0)
394 return 1;
395
396 return 0;
397}
398
Eric Parisc9180a52007-11-30 13:00:35 -0500399static int sb_finish_set_opts(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
401 struct superblock_security_struct *sbsec = sb->s_security;
402 struct dentry *root = sb->s_root;
Eric Parisc9180a52007-11-30 13:00:35 -0500403 struct inode *root_inode = root->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 int rc = 0;
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
407 /* Make sure that the xattr handler exists and that no
408 error other than -ENODATA is returned by getxattr on
409 the root directory. -ENODATA is ok, as this may be
410 the first boot of the SELinux kernel before we have
411 assigned xattr values to the filesystem. */
Eric Parisc9180a52007-11-30 13:00:35 -0500412 if (!root_inode->i_op->getxattr) {
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800413 printk(KERN_WARNING "SELinux: (dev %s, type %s) has no "
414 "xattr support\n", sb->s_id, sb->s_type->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 rc = -EOPNOTSUPP;
416 goto out;
417 }
Eric Parisc9180a52007-11-30 13:00:35 -0500418 rc = root_inode->i_op->getxattr(root, XATTR_NAME_SELINUX, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 if (rc < 0 && rc != -ENODATA) {
420 if (rc == -EOPNOTSUPP)
421 printk(KERN_WARNING "SELinux: (dev %s, type "
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800422 "%s) has no security xattr handler\n",
423 sb->s_id, sb->s_type->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 else
425 printk(KERN_WARNING "SELinux: (dev %s, type "
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800426 "%s) getxattr errno %d\n", sb->s_id,
427 sb->s_type->name, -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 goto out;
429 }
430 }
431
Eric Parisc9180a52007-11-30 13:00:35 -0500432 if (sbsec->behavior > ARRAY_SIZE(labeling_behaviors))
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800433 printk(KERN_ERR "SELinux: initialized (dev %s, type %s), unknown behavior\n",
434 sb->s_id, sb->s_type->name);
Eric Parisc9180a52007-11-30 13:00:35 -0500435 else
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800436 printk(KERN_DEBUG "SELinux: initialized (dev %s, type %s), %s\n",
437 sb->s_id, sb->s_type->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 labeling_behaviors[sbsec->behavior-1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Eric Pariseadcabc2012-08-24 15:59:14 -0400440 sbsec->flags |= SE_SBINITIALIZED;
Eric Parisb43e7252012-10-10 14:27:35 -0400441 if (selinux_is_sblabel_mnt(sb))
Eric Paris12f348b2012-10-09 10:56:25 -0400442 sbsec->flags |= SBLABEL_MNT;
David P. Quigleyddd29ec2009-09-09 14:25:37 -0400443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 /* Initialize the root inode. */
Eric Parisc9180a52007-11-30 13:00:35 -0500445 rc = inode_doinit_with_dentry(root_inode, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 /* Initialize any other inodes associated with the superblock, e.g.
448 inodes created prior to initial policy load or inodes created
449 during get_sb by a pseudo filesystem that directly
450 populates itself. */
451 spin_lock(&sbsec->isec_lock);
452next_inode:
453 if (!list_empty(&sbsec->isec_head)) {
454 struct inode_security_struct *isec =
455 list_entry(sbsec->isec_head.next,
Eric Parisc9180a52007-11-30 13:00:35 -0500456 struct inode_security_struct, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 struct inode *inode = isec->inode;
458 spin_unlock(&sbsec->isec_lock);
459 inode = igrab(inode);
460 if (inode) {
Eric Parisc9180a52007-11-30 13:00:35 -0500461 if (!IS_PRIVATE(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 inode_doinit(inode);
463 iput(inode);
464 }
465 spin_lock(&sbsec->isec_lock);
466 list_del_init(&isec->list);
467 goto next_inode;
468 }
469 spin_unlock(&sbsec->isec_lock);
470out:
Eric Parisc9180a52007-11-30 13:00:35 -0500471 return rc;
472}
473
474/*
475 * This function should allow an FS to ask what it's mount security
476 * options were so it can use those later for submounts, displaying
477 * mount options, or whatever.
478 */
479static int selinux_get_mnt_opts(const struct super_block *sb,
Eric Parise0007522008-03-05 10:31:54 -0500480 struct security_mnt_opts *opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500481{
482 int rc = 0, i;
483 struct superblock_security_struct *sbsec = sb->s_security;
484 char *context = NULL;
485 u32 len;
486 char tmp;
487
Eric Parise0007522008-03-05 10:31:54 -0500488 security_init_mnt_opts(opts);
Eric Parisc9180a52007-11-30 13:00:35 -0500489
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500490 if (!(sbsec->flags & SE_SBINITIALIZED))
Eric Parisc9180a52007-11-30 13:00:35 -0500491 return -EINVAL;
492
493 if (!ss_initialized)
494 return -EINVAL;
495
Eric Parisaf8e50c2012-08-24 15:59:00 -0400496 /* make sure we always check enough bits to cover the mask */
497 BUILD_BUG_ON(SE_MNTMASK >= (1 << NUM_SEL_MNT_OPTS));
498
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500499 tmp = sbsec->flags & SE_MNTMASK;
Eric Parisc9180a52007-11-30 13:00:35 -0500500 /* count the number of mount options for this sb */
Eric Parisaf8e50c2012-08-24 15:59:00 -0400501 for (i = 0; i < NUM_SEL_MNT_OPTS; i++) {
Eric Parisc9180a52007-11-30 13:00:35 -0500502 if (tmp & 0x01)
Eric Parise0007522008-03-05 10:31:54 -0500503 opts->num_mnt_opts++;
Eric Parisc9180a52007-11-30 13:00:35 -0500504 tmp >>= 1;
505 }
David P. Quigley11689d42009-01-16 09:22:03 -0500506 /* Check if the Label support flag is set */
Eric Paris0b4bdb32013-08-28 13:32:42 -0400507 if (sbsec->flags & SBLABEL_MNT)
David P. Quigley11689d42009-01-16 09:22:03 -0500508 opts->num_mnt_opts++;
Eric Parisc9180a52007-11-30 13:00:35 -0500509
Eric Parise0007522008-03-05 10:31:54 -0500510 opts->mnt_opts = kcalloc(opts->num_mnt_opts, sizeof(char *), GFP_ATOMIC);
511 if (!opts->mnt_opts) {
Eric Parisc9180a52007-11-30 13:00:35 -0500512 rc = -ENOMEM;
513 goto out_free;
514 }
515
Eric Parise0007522008-03-05 10:31:54 -0500516 opts->mnt_opts_flags = kcalloc(opts->num_mnt_opts, sizeof(int), GFP_ATOMIC);
517 if (!opts->mnt_opts_flags) {
Eric Parisc9180a52007-11-30 13:00:35 -0500518 rc = -ENOMEM;
519 goto out_free;
520 }
521
522 i = 0;
523 if (sbsec->flags & FSCONTEXT_MNT) {
524 rc = security_sid_to_context(sbsec->sid, &context, &len);
525 if (rc)
526 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500527 opts->mnt_opts[i] = context;
528 opts->mnt_opts_flags[i++] = FSCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500529 }
530 if (sbsec->flags & CONTEXT_MNT) {
531 rc = security_sid_to_context(sbsec->mntpoint_sid, &context, &len);
532 if (rc)
533 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500534 opts->mnt_opts[i] = context;
535 opts->mnt_opts_flags[i++] = CONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500536 }
537 if (sbsec->flags & DEFCONTEXT_MNT) {
538 rc = security_sid_to_context(sbsec->def_sid, &context, &len);
539 if (rc)
540 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500541 opts->mnt_opts[i] = context;
542 opts->mnt_opts_flags[i++] = DEFCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500543 }
544 if (sbsec->flags & ROOTCONTEXT_MNT) {
545 struct inode *root = sbsec->sb->s_root->d_inode;
546 struct inode_security_struct *isec = root->i_security;
547
548 rc = security_sid_to_context(isec->sid, &context, &len);
549 if (rc)
550 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500551 opts->mnt_opts[i] = context;
552 opts->mnt_opts_flags[i++] = ROOTCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500553 }
Eric Paris12f348b2012-10-09 10:56:25 -0400554 if (sbsec->flags & SBLABEL_MNT) {
David P. Quigley11689d42009-01-16 09:22:03 -0500555 opts->mnt_opts[i] = NULL;
Eric Paris12f348b2012-10-09 10:56:25 -0400556 opts->mnt_opts_flags[i++] = SBLABEL_MNT;
David P. Quigley11689d42009-01-16 09:22:03 -0500557 }
Eric Parisc9180a52007-11-30 13:00:35 -0500558
Eric Parise0007522008-03-05 10:31:54 -0500559 BUG_ON(i != opts->num_mnt_opts);
Eric Parisc9180a52007-11-30 13:00:35 -0500560
561 return 0;
562
563out_free:
Eric Parise0007522008-03-05 10:31:54 -0500564 security_free_mnt_opts(opts);
Eric Parisc9180a52007-11-30 13:00:35 -0500565 return rc;
566}
567
568static int bad_option(struct superblock_security_struct *sbsec, char flag,
569 u32 old_sid, u32 new_sid)
570{
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500571 char mnt_flags = sbsec->flags & SE_MNTMASK;
572
Eric Parisc9180a52007-11-30 13:00:35 -0500573 /* check if the old mount command had the same options */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500574 if (sbsec->flags & SE_SBINITIALIZED)
Eric Parisc9180a52007-11-30 13:00:35 -0500575 if (!(sbsec->flags & flag) ||
576 (old_sid != new_sid))
577 return 1;
578
579 /* check if we were passed the same options twice,
580 * aka someone passed context=a,context=b
581 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500582 if (!(sbsec->flags & SE_SBINITIALIZED))
583 if (mnt_flags & flag)
Eric Parisc9180a52007-11-30 13:00:35 -0500584 return 1;
585 return 0;
586}
Eric Parise0007522008-03-05 10:31:54 -0500587
Eric Parisc9180a52007-11-30 13:00:35 -0500588/*
589 * Allow filesystems with binary mount data to explicitly set mount point
590 * labeling information.
591 */
Eric Parise0007522008-03-05 10:31:54 -0500592static int selinux_set_mnt_opts(struct super_block *sb,
David Quigley649f6e72013-05-22 12:50:36 -0400593 struct security_mnt_opts *opts,
594 unsigned long kern_flags,
595 unsigned long *set_kern_flags)
Eric Parisc9180a52007-11-30 13:00:35 -0500596{
David Howells275bb412008-11-14 10:39:19 +1100597 const struct cred *cred = current_cred();
Eric Parisc9180a52007-11-30 13:00:35 -0500598 int rc = 0, i;
Eric Parisc9180a52007-11-30 13:00:35 -0500599 struct superblock_security_struct *sbsec = sb->s_security;
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800600 const char *name = sb->s_type->name;
James Morris089be432008-07-15 18:32:49 +1000601 struct inode *inode = sbsec->sb->s_root->d_inode;
602 struct inode_security_struct *root_isec = inode->i_security;
Eric Parisc9180a52007-11-30 13:00:35 -0500603 u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
604 u32 defcontext_sid = 0;
Eric Parise0007522008-03-05 10:31:54 -0500605 char **mount_options = opts->mnt_opts;
606 int *flags = opts->mnt_opts_flags;
607 int num_opts = opts->num_mnt_opts;
Eric Parisc9180a52007-11-30 13:00:35 -0500608
609 mutex_lock(&sbsec->lock);
610
611 if (!ss_initialized) {
612 if (!num_opts) {
613 /* Defer initialization until selinux_complete_init,
614 after the initial policy is loaded and the security
615 server is ready to handle calls. */
Eric Parisc9180a52007-11-30 13:00:35 -0500616 goto out;
617 }
618 rc = -EINVAL;
Eric Paris744ba352008-04-17 11:52:44 -0400619 printk(KERN_WARNING "SELinux: Unable to set superblock options "
620 "before the security server is initialized\n");
Eric Parisc9180a52007-11-30 13:00:35 -0500621 goto out;
622 }
David Quigley649f6e72013-05-22 12:50:36 -0400623 if (kern_flags && !set_kern_flags) {
624 /* Specifying internal flags without providing a place to
625 * place the results is not allowed */
626 rc = -EINVAL;
627 goto out;
628 }
Eric Parisc9180a52007-11-30 13:00:35 -0500629
630 /*
Eric Parise0007522008-03-05 10:31:54 -0500631 * Binary mount data FS will come through this function twice. Once
632 * from an explicit call and once from the generic calls from the vfs.
633 * Since the generic VFS calls will not contain any security mount data
634 * we need to skip the double mount verification.
635 *
636 * This does open a hole in which we will not notice if the first
637 * mount using this sb set explict options and a second mount using
638 * this sb does not set any security options. (The first options
639 * will be used for both mounts)
640 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500641 if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
Eric Parise0007522008-03-05 10:31:54 -0500642 && (num_opts == 0))
Eric Parisf5269712008-05-14 11:27:45 -0400643 goto out;
Eric Parise0007522008-03-05 10:31:54 -0500644
645 /*
Eric Parisc9180a52007-11-30 13:00:35 -0500646 * parse the mount options, check if they are valid sids.
647 * also check if someone is trying to mount the same sb more
648 * than once with different security options.
649 */
650 for (i = 0; i < num_opts; i++) {
651 u32 sid;
David P. Quigley11689d42009-01-16 09:22:03 -0500652
Eric Paris12f348b2012-10-09 10:56:25 -0400653 if (flags[i] == SBLABEL_MNT)
David P. Quigley11689d42009-01-16 09:22:03 -0500654 continue;
Eric Parisc9180a52007-11-30 13:00:35 -0500655 rc = security_context_to_sid(mount_options[i],
656 strlen(mount_options[i]), &sid);
657 if (rc) {
658 printk(KERN_WARNING "SELinux: security_context_to_sid"
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800659 "(%s) failed for (dev %s, type %s) errno=%d\n",
660 mount_options[i], sb->s_id, name, rc);
Eric Parisc9180a52007-11-30 13:00:35 -0500661 goto out;
662 }
663 switch (flags[i]) {
664 case FSCONTEXT_MNT:
665 fscontext_sid = sid;
666
667 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
668 fscontext_sid))
669 goto out_double_mount;
670
671 sbsec->flags |= FSCONTEXT_MNT;
672 break;
673 case CONTEXT_MNT:
674 context_sid = sid;
675
676 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
677 context_sid))
678 goto out_double_mount;
679
680 sbsec->flags |= CONTEXT_MNT;
681 break;
682 case ROOTCONTEXT_MNT:
683 rootcontext_sid = sid;
684
685 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
686 rootcontext_sid))
687 goto out_double_mount;
688
689 sbsec->flags |= ROOTCONTEXT_MNT;
690
691 break;
692 case DEFCONTEXT_MNT:
693 defcontext_sid = sid;
694
695 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
696 defcontext_sid))
697 goto out_double_mount;
698
699 sbsec->flags |= DEFCONTEXT_MNT;
700
701 break;
702 default:
703 rc = -EINVAL;
704 goto out;
705 }
706 }
707
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500708 if (sbsec->flags & SE_SBINITIALIZED) {
Eric Parisc9180a52007-11-30 13:00:35 -0500709 /* previously mounted with options, but not on this attempt? */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500710 if ((sbsec->flags & SE_MNTMASK) && !num_opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500711 goto out_double_mount;
712 rc = 0;
713 goto out;
714 }
715
James Morris089be432008-07-15 18:32:49 +1000716 if (strcmp(sb->s_type->name, "proc") == 0)
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500717 sbsec->flags |= SE_SBPROC;
Eric Parisc9180a52007-11-30 13:00:35 -0500718
David Quigleyeb9ae682013-05-22 12:50:37 -0400719 if (!sbsec->behavior) {
720 /*
721 * Determine the labeling behavior to use for this
722 * filesystem type.
723 */
Paul Moore98f700f2013-09-18 13:52:20 -0400724 rc = security_fs_use(sb);
David Quigleyeb9ae682013-05-22 12:50:37 -0400725 if (rc) {
726 printk(KERN_WARNING
727 "%s: security_fs_use(%s) returned %d\n",
728 __func__, sb->s_type->name, rc);
729 goto out;
730 }
Eric Parisc9180a52007-11-30 13:00:35 -0500731 }
Eric Parisc9180a52007-11-30 13:00:35 -0500732 /* sets the context of the superblock for the fs being mounted. */
733 if (fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100734 rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500735 if (rc)
736 goto out;
737
738 sbsec->sid = fscontext_sid;
739 }
740
741 /*
742 * Switch to using mount point labeling behavior.
743 * sets the label used on all file below the mountpoint, and will set
744 * the superblock context if not already set.
745 */
David Quigleyeb9ae682013-05-22 12:50:37 -0400746 if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !context_sid) {
747 sbsec->behavior = SECURITY_FS_USE_NATIVE;
748 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
749 }
750
Eric Parisc9180a52007-11-30 13:00:35 -0500751 if (context_sid) {
752 if (!fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100753 rc = may_context_mount_sb_relabel(context_sid, sbsec,
754 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500755 if (rc)
756 goto out;
757 sbsec->sid = context_sid;
758 } else {
David Howells275bb412008-11-14 10:39:19 +1100759 rc = may_context_mount_inode_relabel(context_sid, sbsec,
760 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500761 if (rc)
762 goto out;
763 }
764 if (!rootcontext_sid)
765 rootcontext_sid = context_sid;
766
767 sbsec->mntpoint_sid = context_sid;
768 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
769 }
770
771 if (rootcontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100772 rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec,
773 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500774 if (rc)
775 goto out;
776
777 root_isec->sid = rootcontext_sid;
778 root_isec->initialized = 1;
779 }
780
781 if (defcontext_sid) {
David Quigleyeb9ae682013-05-22 12:50:37 -0400782 if (sbsec->behavior != SECURITY_FS_USE_XATTR &&
783 sbsec->behavior != SECURITY_FS_USE_NATIVE) {
Eric Parisc9180a52007-11-30 13:00:35 -0500784 rc = -EINVAL;
785 printk(KERN_WARNING "SELinux: defcontext option is "
786 "invalid for this filesystem type\n");
787 goto out;
788 }
789
790 if (defcontext_sid != sbsec->def_sid) {
791 rc = may_context_mount_inode_relabel(defcontext_sid,
David Howells275bb412008-11-14 10:39:19 +1100792 sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500793 if (rc)
794 goto out;
795 }
796
797 sbsec->def_sid = defcontext_sid;
798 }
799
800 rc = sb_finish_set_opts(sb);
801out:
Eric Parisbc7e9822006-09-25 23:32:02 -0700802 mutex_unlock(&sbsec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 return rc;
Eric Parisc9180a52007-11-30 13:00:35 -0500804out_double_mount:
805 rc = -EINVAL;
806 printk(KERN_WARNING "SELinux: mount invalid. Same superblock, different "
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800807 "security settings for (dev %s, type %s)\n", sb->s_id, name);
Eric Parisc9180a52007-11-30 13:00:35 -0500808 goto out;
809}
810
Jeff Layton094f7b62013-04-01 08:14:24 -0400811static int selinux_cmp_sb_context(const struct super_block *oldsb,
812 const struct super_block *newsb)
813{
814 struct superblock_security_struct *old = oldsb->s_security;
815 struct superblock_security_struct *new = newsb->s_security;
816 char oldflags = old->flags & SE_MNTMASK;
817 char newflags = new->flags & SE_MNTMASK;
818
819 if (oldflags != newflags)
820 goto mismatch;
821 if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid)
822 goto mismatch;
823 if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid)
824 goto mismatch;
825 if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid)
826 goto mismatch;
827 if (oldflags & ROOTCONTEXT_MNT) {
828 struct inode_security_struct *oldroot = oldsb->s_root->d_inode->i_security;
829 struct inode_security_struct *newroot = newsb->s_root->d_inode->i_security;
830 if (oldroot->sid != newroot->sid)
831 goto mismatch;
832 }
833 return 0;
834mismatch:
835 printk(KERN_WARNING "SELinux: mount invalid. Same superblock, "
836 "different security settings for (dev %s, "
837 "type %s)\n", newsb->s_id, newsb->s_type->name);
838 return -EBUSY;
839}
840
841static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
Eric Parisc9180a52007-11-30 13:00:35 -0500842 struct super_block *newsb)
843{
844 const struct superblock_security_struct *oldsbsec = oldsb->s_security;
845 struct superblock_security_struct *newsbsec = newsb->s_security;
846
847 int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT);
848 int set_context = (oldsbsec->flags & CONTEXT_MNT);
849 int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT);
850
Eric Paris0f5e6422008-04-21 16:24:11 -0400851 /*
852 * if the parent was able to be mounted it clearly had no special lsm
Al Viroe8c26252010-03-23 06:36:54 -0400853 * mount options. thus we can safely deal with this superblock later
Eric Paris0f5e6422008-04-21 16:24:11 -0400854 */
Al Viroe8c26252010-03-23 06:36:54 -0400855 if (!ss_initialized)
Jeff Layton094f7b62013-04-01 08:14:24 -0400856 return 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500857
Eric Parisc9180a52007-11-30 13:00:35 -0500858 /* how can we clone if the old one wasn't set up?? */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500859 BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED));
Eric Parisc9180a52007-11-30 13:00:35 -0500860
Jeff Layton094f7b62013-04-01 08:14:24 -0400861 /* if fs is reusing a sb, make sure that the contexts match */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500862 if (newsbsec->flags & SE_SBINITIALIZED)
Jeff Layton094f7b62013-04-01 08:14:24 -0400863 return selinux_cmp_sb_context(oldsb, newsb);
Eric Paris5a552612008-04-09 14:08:35 -0400864
Eric Parisc9180a52007-11-30 13:00:35 -0500865 mutex_lock(&newsbsec->lock);
866
867 newsbsec->flags = oldsbsec->flags;
868
869 newsbsec->sid = oldsbsec->sid;
870 newsbsec->def_sid = oldsbsec->def_sid;
871 newsbsec->behavior = oldsbsec->behavior;
872
873 if (set_context) {
874 u32 sid = oldsbsec->mntpoint_sid;
875
876 if (!set_fscontext)
877 newsbsec->sid = sid;
878 if (!set_rootcontext) {
879 struct inode *newinode = newsb->s_root->d_inode;
880 struct inode_security_struct *newisec = newinode->i_security;
881 newisec->sid = sid;
882 }
883 newsbsec->mntpoint_sid = sid;
884 }
885 if (set_rootcontext) {
886 const struct inode *oldinode = oldsb->s_root->d_inode;
887 const struct inode_security_struct *oldisec = oldinode->i_security;
888 struct inode *newinode = newsb->s_root->d_inode;
889 struct inode_security_struct *newisec = newinode->i_security;
890
891 newisec->sid = oldisec->sid;
892 }
893
894 sb_finish_set_opts(newsb);
895 mutex_unlock(&newsbsec->lock);
Jeff Layton094f7b62013-04-01 08:14:24 -0400896 return 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500897}
898
Adrian Bunk2e1479d2008-03-17 22:29:23 +0200899static int selinux_parse_opts_str(char *options,
900 struct security_mnt_opts *opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500901{
Eric Parise0007522008-03-05 10:31:54 -0500902 char *p;
Eric Parisc9180a52007-11-30 13:00:35 -0500903 char *context = NULL, *defcontext = NULL;
904 char *fscontext = NULL, *rootcontext = NULL;
Eric Parise0007522008-03-05 10:31:54 -0500905 int rc, num_mnt_opts = 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500906
Eric Parise0007522008-03-05 10:31:54 -0500907 opts->num_mnt_opts = 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500908
909 /* Standard string-based options. */
910 while ((p = strsep(&options, "|")) != NULL) {
911 int token;
912 substring_t args[MAX_OPT_ARGS];
913
914 if (!*p)
915 continue;
916
917 token = match_token(p, tokens, args);
918
919 switch (token) {
920 case Opt_context:
921 if (context || defcontext) {
922 rc = -EINVAL;
923 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
924 goto out_err;
925 }
926 context = match_strdup(&args[0]);
927 if (!context) {
928 rc = -ENOMEM;
929 goto out_err;
930 }
931 break;
932
933 case Opt_fscontext:
934 if (fscontext) {
935 rc = -EINVAL;
936 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
937 goto out_err;
938 }
939 fscontext = match_strdup(&args[0]);
940 if (!fscontext) {
941 rc = -ENOMEM;
942 goto out_err;
943 }
944 break;
945
946 case Opt_rootcontext:
947 if (rootcontext) {
948 rc = -EINVAL;
949 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
950 goto out_err;
951 }
952 rootcontext = match_strdup(&args[0]);
953 if (!rootcontext) {
954 rc = -ENOMEM;
955 goto out_err;
956 }
957 break;
958
959 case Opt_defcontext:
960 if (context || defcontext) {
961 rc = -EINVAL;
962 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
963 goto out_err;
964 }
965 defcontext = match_strdup(&args[0]);
966 if (!defcontext) {
967 rc = -ENOMEM;
968 goto out_err;
969 }
970 break;
David P. Quigley11689d42009-01-16 09:22:03 -0500971 case Opt_labelsupport:
972 break;
Eric Parisc9180a52007-11-30 13:00:35 -0500973 default:
974 rc = -EINVAL;
975 printk(KERN_WARNING "SELinux: unknown mount option\n");
976 goto out_err;
977
978 }
979 }
980
Eric Parise0007522008-03-05 10:31:54 -0500981 rc = -ENOMEM;
982 opts->mnt_opts = kcalloc(NUM_SEL_MNT_OPTS, sizeof(char *), GFP_ATOMIC);
983 if (!opts->mnt_opts)
984 goto out_err;
985
986 opts->mnt_opts_flags = kcalloc(NUM_SEL_MNT_OPTS, sizeof(int), GFP_ATOMIC);
987 if (!opts->mnt_opts_flags) {
988 kfree(opts->mnt_opts);
989 goto out_err;
Eric Parisc9180a52007-11-30 13:00:35 -0500990 }
991
Eric Parise0007522008-03-05 10:31:54 -0500992 if (fscontext) {
993 opts->mnt_opts[num_mnt_opts] = fscontext;
994 opts->mnt_opts_flags[num_mnt_opts++] = FSCONTEXT_MNT;
995 }
996 if (context) {
997 opts->mnt_opts[num_mnt_opts] = context;
998 opts->mnt_opts_flags[num_mnt_opts++] = CONTEXT_MNT;
999 }
1000 if (rootcontext) {
1001 opts->mnt_opts[num_mnt_opts] = rootcontext;
1002 opts->mnt_opts_flags[num_mnt_opts++] = ROOTCONTEXT_MNT;
1003 }
1004 if (defcontext) {
1005 opts->mnt_opts[num_mnt_opts] = defcontext;
1006 opts->mnt_opts_flags[num_mnt_opts++] = DEFCONTEXT_MNT;
1007 }
1008
1009 opts->num_mnt_opts = num_mnt_opts;
1010 return 0;
1011
Eric Parisc9180a52007-11-30 13:00:35 -05001012out_err:
1013 kfree(context);
1014 kfree(defcontext);
1015 kfree(fscontext);
1016 kfree(rootcontext);
1017 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018}
Eric Parise0007522008-03-05 10:31:54 -05001019/*
1020 * string mount options parsing and call set the sbsec
1021 */
1022static int superblock_doinit(struct super_block *sb, void *data)
1023{
1024 int rc = 0;
1025 char *options = data;
1026 struct security_mnt_opts opts;
1027
1028 security_init_mnt_opts(&opts);
1029
1030 if (!data)
1031 goto out;
1032
1033 BUG_ON(sb->s_type->fs_flags & FS_BINARY_MOUNTDATA);
1034
1035 rc = selinux_parse_opts_str(options, &opts);
1036 if (rc)
1037 goto out_err;
1038
1039out:
David Quigley649f6e72013-05-22 12:50:36 -04001040 rc = selinux_set_mnt_opts(sb, &opts, 0, NULL);
Eric Parise0007522008-03-05 10:31:54 -05001041
1042out_err:
1043 security_free_mnt_opts(&opts);
1044 return rc;
1045}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Adrian Bunk3583a712008-07-22 20:21:23 +03001047static void selinux_write_opts(struct seq_file *m,
1048 struct security_mnt_opts *opts)
Eric Paris2069f452008-07-04 09:47:13 +10001049{
1050 int i;
1051 char *prefix;
1052
1053 for (i = 0; i < opts->num_mnt_opts; i++) {
David P. Quigley11689d42009-01-16 09:22:03 -05001054 char *has_comma;
1055
1056 if (opts->mnt_opts[i])
1057 has_comma = strchr(opts->mnt_opts[i], ',');
1058 else
1059 has_comma = NULL;
Eric Paris2069f452008-07-04 09:47:13 +10001060
1061 switch (opts->mnt_opts_flags[i]) {
1062 case CONTEXT_MNT:
1063 prefix = CONTEXT_STR;
1064 break;
1065 case FSCONTEXT_MNT:
1066 prefix = FSCONTEXT_STR;
1067 break;
1068 case ROOTCONTEXT_MNT:
1069 prefix = ROOTCONTEXT_STR;
1070 break;
1071 case DEFCONTEXT_MNT:
1072 prefix = DEFCONTEXT_STR;
1073 break;
Eric Paris12f348b2012-10-09 10:56:25 -04001074 case SBLABEL_MNT:
David P. Quigley11689d42009-01-16 09:22:03 -05001075 seq_putc(m, ',');
1076 seq_puts(m, LABELSUPP_STR);
1077 continue;
Eric Paris2069f452008-07-04 09:47:13 +10001078 default:
1079 BUG();
Eric Parisa35c6c82011-04-20 10:21:28 -04001080 return;
Eric Paris2069f452008-07-04 09:47:13 +10001081 };
1082 /* we need a comma before each option */
1083 seq_putc(m, ',');
1084 seq_puts(m, prefix);
1085 if (has_comma)
1086 seq_putc(m, '\"');
1087 seq_puts(m, opts->mnt_opts[i]);
1088 if (has_comma)
1089 seq_putc(m, '\"');
1090 }
1091}
1092
1093static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
1094{
1095 struct security_mnt_opts opts;
1096 int rc;
1097
1098 rc = selinux_get_mnt_opts(sb, &opts);
Eric Paris383795c2008-07-29 17:07:26 -04001099 if (rc) {
1100 /* before policy load we may get EINVAL, don't show anything */
1101 if (rc == -EINVAL)
1102 rc = 0;
Eric Paris2069f452008-07-04 09:47:13 +10001103 return rc;
Eric Paris383795c2008-07-29 17:07:26 -04001104 }
Eric Paris2069f452008-07-04 09:47:13 +10001105
1106 selinux_write_opts(m, &opts);
1107
1108 security_free_mnt_opts(&opts);
1109
1110 return rc;
1111}
1112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113static inline u16 inode_mode_to_security_class(umode_t mode)
1114{
1115 switch (mode & S_IFMT) {
1116 case S_IFSOCK:
1117 return SECCLASS_SOCK_FILE;
1118 case S_IFLNK:
1119 return SECCLASS_LNK_FILE;
1120 case S_IFREG:
1121 return SECCLASS_FILE;
1122 case S_IFBLK:
1123 return SECCLASS_BLK_FILE;
1124 case S_IFDIR:
1125 return SECCLASS_DIR;
1126 case S_IFCHR:
1127 return SECCLASS_CHR_FILE;
1128 case S_IFIFO:
1129 return SECCLASS_FIFO_FILE;
1130
1131 }
1132
1133 return SECCLASS_FILE;
1134}
1135
James Morris13402582005-09-30 14:24:34 -04001136static inline int default_protocol_stream(int protocol)
1137{
1138 return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP);
1139}
1140
1141static inline int default_protocol_dgram(int protocol)
1142{
1143 return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
1144}
1145
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146static inline u16 socket_type_to_security_class(int family, int type, int protocol)
1147{
1148 switch (family) {
1149 case PF_UNIX:
1150 switch (type) {
1151 case SOCK_STREAM:
1152 case SOCK_SEQPACKET:
1153 return SECCLASS_UNIX_STREAM_SOCKET;
1154 case SOCK_DGRAM:
1155 return SECCLASS_UNIX_DGRAM_SOCKET;
1156 }
1157 break;
1158 case PF_INET:
1159 case PF_INET6:
1160 switch (type) {
1161 case SOCK_STREAM:
James Morris13402582005-09-30 14:24:34 -04001162 if (default_protocol_stream(protocol))
1163 return SECCLASS_TCP_SOCKET;
1164 else
1165 return SECCLASS_RAWIP_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 case SOCK_DGRAM:
James Morris13402582005-09-30 14:24:34 -04001167 if (default_protocol_dgram(protocol))
1168 return SECCLASS_UDP_SOCKET;
1169 else
1170 return SECCLASS_RAWIP_SOCKET;
James Morris2ee92d42006-11-13 16:09:01 -08001171 case SOCK_DCCP:
1172 return SECCLASS_DCCP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001173 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 return SECCLASS_RAWIP_SOCKET;
1175 }
1176 break;
1177 case PF_NETLINK:
1178 switch (protocol) {
1179 case NETLINK_ROUTE:
1180 return SECCLASS_NETLINK_ROUTE_SOCKET;
1181 case NETLINK_FIREWALL:
1182 return SECCLASS_NETLINK_FIREWALL_SOCKET;
Pavel Emelyanov7f1fb602011-12-06 07:56:43 +00001183 case NETLINK_SOCK_DIAG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 return SECCLASS_NETLINK_TCPDIAG_SOCKET;
1185 case NETLINK_NFLOG:
1186 return SECCLASS_NETLINK_NFLOG_SOCKET;
1187 case NETLINK_XFRM:
1188 return SECCLASS_NETLINK_XFRM_SOCKET;
1189 case NETLINK_SELINUX:
1190 return SECCLASS_NETLINK_SELINUX_SOCKET;
1191 case NETLINK_AUDIT:
1192 return SECCLASS_NETLINK_AUDIT_SOCKET;
1193 case NETLINK_IP6_FW:
1194 return SECCLASS_NETLINK_IP6FW_SOCKET;
1195 case NETLINK_DNRTMSG:
1196 return SECCLASS_NETLINK_DNRT_SOCKET;
James Morris0c9b7942005-04-16 15:24:13 -07001197 case NETLINK_KOBJECT_UEVENT:
1198 return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 default:
1200 return SECCLASS_NETLINK_SOCKET;
1201 }
1202 case PF_PACKET:
1203 return SECCLASS_PACKET_SOCKET;
1204 case PF_KEY:
1205 return SECCLASS_KEY_SOCKET;
Christopher J. PeBenito3e3ff152006-06-09 00:25:03 -07001206 case PF_APPLETALK:
1207 return SECCLASS_APPLETALK_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 }
1209
1210 return SECCLASS_SOCKET;
1211}
1212
1213#ifdef CONFIG_PROC_FS
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001214static int selinux_proc_get_sid(struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 u16 tclass,
1216 u32 *sid)
1217{
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001218 int rc;
1219 char *buffer, *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Eric Paris828dfe12008-04-17 13:17:49 -04001221 buffer = (char *)__get_free_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 if (!buffer)
1223 return -ENOMEM;
1224
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001225 path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
1226 if (IS_ERR(path))
1227 rc = PTR_ERR(path);
1228 else {
1229 /* each process gets a /proc/PID/ entry. Strip off the
1230 * PID part to get a valid selinux labeling.
1231 * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
1232 while (path[1] >= '0' && path[1] <= '9') {
1233 path[1] = '/';
1234 path++;
1235 }
1236 rc = security_genfs_sid("proc", path, tclass, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 free_page((unsigned long)buffer);
1239 return rc;
1240}
1241#else
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001242static int selinux_proc_get_sid(struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 u16 tclass,
1244 u32 *sid)
1245{
1246 return -EINVAL;
1247}
1248#endif
1249
1250/* The inode's security attributes must be initialized before first use. */
1251static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
1252{
1253 struct superblock_security_struct *sbsec = NULL;
1254 struct inode_security_struct *isec = inode->i_security;
1255 u32 sid;
1256 struct dentry *dentry;
1257#define INITCONTEXTLEN 255
1258 char *context = NULL;
1259 unsigned len = 0;
1260 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
1262 if (isec->initialized)
1263 goto out;
1264
Eric Paris23970742006-09-25 23:32:01 -07001265 mutex_lock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 if (isec->initialized)
Eric Paris23970742006-09-25 23:32:01 -07001267 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
1269 sbsec = inode->i_sb->s_security;
David P. Quigley0d90a7e2009-01-16 09:22:02 -05001270 if (!(sbsec->flags & SE_SBINITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 /* Defer initialization until selinux_complete_init,
1272 after the initial policy is loaded and the security
1273 server is ready to handle calls. */
1274 spin_lock(&sbsec->isec_lock);
1275 if (list_empty(&isec->list))
1276 list_add(&isec->list, &sbsec->isec_head);
1277 spin_unlock(&sbsec->isec_lock);
Eric Paris23970742006-09-25 23:32:01 -07001278 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 }
1280
1281 switch (sbsec->behavior) {
David Quigleyeb9ae682013-05-22 12:50:37 -04001282 case SECURITY_FS_USE_NATIVE:
1283 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 case SECURITY_FS_USE_XATTR:
1285 if (!inode->i_op->getxattr) {
1286 isec->sid = sbsec->def_sid;
1287 break;
1288 }
1289
1290 /* Need a dentry, since the xattr API requires one.
1291 Life would be simpler if we could just pass the inode. */
1292 if (opt_dentry) {
1293 /* Called from d_instantiate or d_splice_alias. */
1294 dentry = dget(opt_dentry);
1295 } else {
1296 /* Called from selinux_complete_init, try to find a dentry. */
1297 dentry = d_find_alias(inode);
1298 }
1299 if (!dentry) {
Eric Parisdf7f54c2009-03-09 14:35:58 -04001300 /*
1301 * this is can be hit on boot when a file is accessed
1302 * before the policy is loaded. When we load policy we
1303 * may find inodes that have no dentry on the
1304 * sbsec->isec_head list. No reason to complain as these
1305 * will get fixed up the next time we go through
1306 * inode_doinit with a dentry, before these inodes could
1307 * be used again by userspace.
1308 */
Eric Paris23970742006-09-25 23:32:01 -07001309 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 }
1311
1312 len = INITCONTEXTLEN;
Eric Paris4cb912f2009-02-12 14:50:05 -05001313 context = kmalloc(len+1, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 if (!context) {
1315 rc = -ENOMEM;
1316 dput(dentry);
Eric Paris23970742006-09-25 23:32:01 -07001317 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 }
Eric Paris4cb912f2009-02-12 14:50:05 -05001319 context[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX,
1321 context, len);
1322 if (rc == -ERANGE) {
James Morris314dabb2009-08-10 22:00:13 +10001323 kfree(context);
1324
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 /* Need a larger buffer. Query for the right size. */
1326 rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX,
1327 NULL, 0);
1328 if (rc < 0) {
1329 dput(dentry);
Eric Paris23970742006-09-25 23:32:01 -07001330 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 len = rc;
Eric Paris4cb912f2009-02-12 14:50:05 -05001333 context = kmalloc(len+1, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 if (!context) {
1335 rc = -ENOMEM;
1336 dput(dentry);
Eric Paris23970742006-09-25 23:32:01 -07001337 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 }
Eric Paris4cb912f2009-02-12 14:50:05 -05001339 context[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 rc = inode->i_op->getxattr(dentry,
1341 XATTR_NAME_SELINUX,
1342 context, len);
1343 }
1344 dput(dentry);
1345 if (rc < 0) {
1346 if (rc != -ENODATA) {
Eric Paris744ba352008-04-17 11:52:44 -04001347 printk(KERN_WARNING "SELinux: %s: getxattr returned "
Harvey Harrisondd6f9532008-03-06 10:03:59 +11001348 "%d for dev=%s ino=%ld\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 -rc, inode->i_sb->s_id, inode->i_ino);
1350 kfree(context);
Eric Paris23970742006-09-25 23:32:01 -07001351 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 }
1353 /* Map ENODATA to the default file SID */
1354 sid = sbsec->def_sid;
1355 rc = 0;
1356 } else {
James Morrisf5c1d5b2005-07-28 01:07:37 -07001357 rc = security_context_to_sid_default(context, rc, &sid,
Stephen Smalley869ab512008-04-04 08:46:05 -04001358 sbsec->def_sid,
1359 GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 if (rc) {
Eric Paris4ba0a8a2009-02-12 15:01:10 -05001361 char *dev = inode->i_sb->s_id;
1362 unsigned long ino = inode->i_ino;
1363
1364 if (rc == -EINVAL) {
1365 if (printk_ratelimit())
1366 printk(KERN_NOTICE "SELinux: inode=%lu on dev=%s was found to have an invalid "
1367 "context=%s. This indicates you may need to relabel the inode or the "
1368 "filesystem in question.\n", ino, dev, context);
1369 } else {
1370 printk(KERN_WARNING "SELinux: %s: context_to_sid(%s) "
1371 "returned %d for dev=%s ino=%ld\n",
1372 __func__, context, -rc, dev, ino);
1373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 kfree(context);
1375 /* Leave with the unlabeled SID */
1376 rc = 0;
1377 break;
1378 }
1379 }
1380 kfree(context);
1381 isec->sid = sid;
1382 break;
1383 case SECURITY_FS_USE_TASK:
1384 isec->sid = isec->task_sid;
1385 break;
1386 case SECURITY_FS_USE_TRANS:
1387 /* Default to the fs SID. */
1388 isec->sid = sbsec->sid;
1389
1390 /* Try to obtain a transition SID. */
1391 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Eric Paris652bb9b2011-02-01 11:05:40 -05001392 rc = security_transition_sid(isec->task_sid, sbsec->sid,
1393 isec->sclass, NULL, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 if (rc)
Eric Paris23970742006-09-25 23:32:01 -07001395 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 isec->sid = sid;
1397 break;
Eric Parisc312feb2006-07-10 04:43:53 -07001398 case SECURITY_FS_USE_MNTPOINT:
1399 isec->sid = sbsec->mntpoint_sid;
1400 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 default:
Eric Parisc312feb2006-07-10 04:43:53 -07001402 /* Default to the fs superblock SID. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 isec->sid = sbsec->sid;
1404
David P. Quigley0d90a7e2009-01-16 09:22:02 -05001405 if ((sbsec->flags & SE_SBPROC) && !S_ISLNK(inode->i_mode)) {
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001406 if (opt_dentry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001408 rc = selinux_proc_get_sid(opt_dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 isec->sclass,
1410 &sid);
1411 if (rc)
Eric Paris23970742006-09-25 23:32:01 -07001412 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 isec->sid = sid;
1414 }
1415 }
1416 break;
1417 }
1418
1419 isec->initialized = 1;
1420
Eric Paris23970742006-09-25 23:32:01 -07001421out_unlock:
1422 mutex_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423out:
1424 if (isec->sclass == SECCLASS_FILE)
1425 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 return rc;
1427}
1428
1429/* Convert a Linux signal to an access vector. */
1430static inline u32 signal_to_av(int sig)
1431{
1432 u32 perm = 0;
1433
1434 switch (sig) {
1435 case SIGCHLD:
1436 /* Commonly granted from child to parent. */
1437 perm = PROCESS__SIGCHLD;
1438 break;
1439 case SIGKILL:
1440 /* Cannot be caught or ignored */
1441 perm = PROCESS__SIGKILL;
1442 break;
1443 case SIGSTOP:
1444 /* Cannot be caught or ignored */
1445 perm = PROCESS__SIGSTOP;
1446 break;
1447 default:
1448 /* All other signals. */
1449 perm = PROCESS__SIGNAL;
1450 break;
1451 }
1452
1453 return perm;
1454}
1455
David Howells275bb412008-11-14 10:39:19 +11001456/*
David Howellsd84f4f92008-11-14 10:39:23 +11001457 * Check permission between a pair of credentials
1458 * fork check, ptrace check, etc.
1459 */
1460static int cred_has_perm(const struct cred *actor,
1461 const struct cred *target,
1462 u32 perms)
1463{
1464 u32 asid = cred_sid(actor), tsid = cred_sid(target);
1465
1466 return avc_has_perm(asid, tsid, SECCLASS_PROCESS, perms, NULL);
1467}
1468
1469/*
David Howells88e67f32008-11-14 10:39:21 +11001470 * Check permission between a pair of tasks, e.g. signal checks,
David Howells275bb412008-11-14 10:39:19 +11001471 * fork check, ptrace check, etc.
1472 * tsk1 is the actor and tsk2 is the target
David Howells3b11a1d2008-11-14 10:39:26 +11001473 * - this uses the default subjective creds of tsk1
David Howells275bb412008-11-14 10:39:19 +11001474 */
1475static int task_has_perm(const struct task_struct *tsk1,
1476 const struct task_struct *tsk2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 u32 perms)
1478{
David Howells275bb412008-11-14 10:39:19 +11001479 const struct task_security_struct *__tsec1, *__tsec2;
1480 u32 sid1, sid2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
David Howells275bb412008-11-14 10:39:19 +11001482 rcu_read_lock();
1483 __tsec1 = __task_cred(tsk1)->security; sid1 = __tsec1->sid;
1484 __tsec2 = __task_cred(tsk2)->security; sid2 = __tsec2->sid;
1485 rcu_read_unlock();
1486 return avc_has_perm(sid1, sid2, SECCLASS_PROCESS, perms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487}
1488
David Howells3b11a1d2008-11-14 10:39:26 +11001489/*
1490 * Check permission between current and another task, e.g. signal checks,
1491 * fork check, ptrace check, etc.
1492 * current is the actor and tsk2 is the target
1493 * - this uses current's subjective creds
1494 */
1495static int current_has_perm(const struct task_struct *tsk,
1496 u32 perms)
1497{
1498 u32 sid, tsid;
1499
1500 sid = current_sid();
1501 tsid = task_sid(tsk);
1502 return avc_has_perm(sid, tsid, SECCLASS_PROCESS, perms, NULL);
1503}
1504
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001505#if CAP_LAST_CAP > 63
1506#error Fix SELinux to handle capabilities > 63.
1507#endif
1508
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509/* Check whether a task is allowed to use a capability. */
Eric Paris6a9de492012-01-03 12:25:14 -05001510static int cred_has_capability(const struct cred *cred,
Eric Paris06112162008-11-11 22:02:50 +11001511 int cap, int audit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512{
Thomas Liu2bf49692009-07-14 12:14:09 -04001513 struct common_audit_data ad;
Eric Paris06112162008-11-11 22:02:50 +11001514 struct av_decision avd;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001515 u16 sclass;
David Howells3699c532009-01-06 22:27:01 +00001516 u32 sid = cred_sid(cred);
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001517 u32 av = CAP_TO_MASK(cap);
Eric Paris06112162008-11-11 22:02:50 +11001518 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
Eric Paris50c205f2012-04-04 15:01:43 -04001520 ad.type = LSM_AUDIT_DATA_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 ad.u.cap = cap;
1522
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001523 switch (CAP_TO_INDEX(cap)) {
1524 case 0:
1525 sclass = SECCLASS_CAPABILITY;
1526 break;
1527 case 1:
1528 sclass = SECCLASS_CAPABILITY2;
1529 break;
1530 default:
1531 printk(KERN_ERR
1532 "SELinux: out of range capability %d\n", cap);
1533 BUG();
Eric Parisa35c6c82011-04-20 10:21:28 -04001534 return -EINVAL;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001535 }
Eric Paris06112162008-11-11 22:02:50 +11001536
David Howells275bb412008-11-14 10:39:19 +11001537 rc = avc_has_perm_noaudit(sid, sid, sclass, av, 0, &avd);
Eric Paris9ade0cf2011-04-25 16:26:29 -04001538 if (audit == SECURITY_CAP_AUDIT) {
Linus Torvaldsab354062013-10-04 14:05:38 -07001539 int rc2 = avc_audit(sid, sid, sclass, av, &avd, rc, &ad);
Eric Paris9ade0cf2011-04-25 16:26:29 -04001540 if (rc2)
1541 return rc2;
1542 }
Eric Paris06112162008-11-11 22:02:50 +11001543 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544}
1545
1546/* Check whether a task is allowed to use a system operation. */
1547static int task_has_system(struct task_struct *tsk,
1548 u32 perms)
1549{
David Howells275bb412008-11-14 10:39:19 +11001550 u32 sid = task_sid(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
David Howells275bb412008-11-14 10:39:19 +11001552 return avc_has_perm(sid, SECINITSID_KERNEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 SECCLASS_SYSTEM, perms, NULL);
1554}
1555
1556/* Check whether a task has a particular permission to an inode.
1557 The 'adp' parameter is optional and allows other audit
1558 data to be passed (e.g. the dentry). */
David Howells88e67f32008-11-14 10:39:21 +11001559static int inode_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 struct inode *inode,
1561 u32 perms,
Linus Torvalds19e49832013-10-04 12:54:11 -07001562 struct common_audit_data *adp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 struct inode_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +11001565 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
David Howellse0e81732009-09-02 09:13:40 +01001567 validate_creds(cred);
1568
Eric Paris828dfe12008-04-17 13:17:49 -04001569 if (unlikely(IS_PRIVATE(inode)))
Stephen Smalleybbaca6c2007-02-14 00:34:16 -08001570 return 0;
1571
David Howells88e67f32008-11-14 10:39:21 +11001572 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 isec = inode->i_security;
1574
Linus Torvalds19e49832013-10-04 12:54:11 -07001575 return avc_has_perm(sid, isec->sid, isec->sclass, perms, adp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576}
1577
1578/* Same as inode_has_perm, but pass explicit audit data containing
1579 the dentry to help the auditing code to more easily generate the
1580 pathname if needed. */
David Howells88e67f32008-11-14 10:39:21 +11001581static inline int dentry_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 struct dentry *dentry,
1583 u32 av)
1584{
1585 struct inode *inode = dentry->d_inode;
Thomas Liu2bf49692009-07-14 12:14:09 -04001586 struct common_audit_data ad;
David Howells88e67f32008-11-14 10:39:21 +11001587
Eric Paris50c205f2012-04-04 15:01:43 -04001588 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Paris2875fa02011-04-28 16:04:24 -04001589 ad.u.dentry = dentry;
Linus Torvalds19e49832013-10-04 12:54:11 -07001590 return inode_has_perm(cred, inode, av, &ad);
Eric Paris2875fa02011-04-28 16:04:24 -04001591}
1592
1593/* Same as inode_has_perm, but pass explicit audit data containing
1594 the path to help the auditing code to more easily generate the
1595 pathname if needed. */
1596static inline int path_has_perm(const struct cred *cred,
1597 struct path *path,
1598 u32 av)
1599{
1600 struct inode *inode = path->dentry->d_inode;
1601 struct common_audit_data ad;
1602
Eric Paris50c205f2012-04-04 15:01:43 -04001603 ad.type = LSM_AUDIT_DATA_PATH;
Eric Paris2875fa02011-04-28 16:04:24 -04001604 ad.u.path = *path;
Linus Torvalds19e49832013-10-04 12:54:11 -07001605 return inode_has_perm(cred, inode, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606}
1607
David Howells13f8e982013-06-13 23:37:55 +01001608/* Same as path_has_perm, but uses the inode from the file struct. */
1609static inline int file_path_has_perm(const struct cred *cred,
1610 struct file *file,
1611 u32 av)
1612{
1613 struct common_audit_data ad;
1614
1615 ad.type = LSM_AUDIT_DATA_PATH;
1616 ad.u.path = file->f_path;
Linus Torvalds19e49832013-10-04 12:54:11 -07001617 return inode_has_perm(cred, file_inode(file), av, &ad);
David Howells13f8e982013-06-13 23:37:55 +01001618}
1619
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620/* Check whether a task can use an open file descriptor to
1621 access an inode in a given way. Check access to the
1622 descriptor itself, and then use dentry_has_perm to
1623 check a particular permission to the file.
1624 Access to the descriptor is implicitly granted if it
1625 has the same SID as the process. If av is zero, then
1626 access to the file is not checked, e.g. for cases
1627 where only the descriptor is affected like seek. */
David Howells88e67f32008-11-14 10:39:21 +11001628static int file_has_perm(const struct cred *cred,
1629 struct file *file,
1630 u32 av)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 struct file_security_struct *fsec = file->f_security;
Al Viro496ad9a2013-01-23 17:07:38 -05001633 struct inode *inode = file_inode(file);
Thomas Liu2bf49692009-07-14 12:14:09 -04001634 struct common_audit_data ad;
David Howells88e67f32008-11-14 10:39:21 +11001635 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 int rc;
1637
Eric Paris50c205f2012-04-04 15:01:43 -04001638 ad.type = LSM_AUDIT_DATA_PATH;
Eric Parisf48b7392011-04-25 12:54:27 -04001639 ad.u.path = file->f_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
David Howells275bb412008-11-14 10:39:19 +11001641 if (sid != fsec->sid) {
1642 rc = avc_has_perm(sid, fsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 SECCLASS_FD,
1644 FD__USE,
1645 &ad);
1646 if (rc)
David Howells88e67f32008-11-14 10:39:21 +11001647 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 }
1649
1650 /* av is zero if only checking access to the descriptor. */
David Howells88e67f32008-11-14 10:39:21 +11001651 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 if (av)
Linus Torvalds19e49832013-10-04 12:54:11 -07001653 rc = inode_has_perm(cred, inode, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
David Howells88e67f32008-11-14 10:39:21 +11001655out:
1656 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657}
1658
1659/* Check whether a task can create a file. */
1660static int may_create(struct inode *dir,
1661 struct dentry *dentry,
1662 u16 tclass)
1663{
Paul Moore5fb49872010-04-22 14:46:19 -04001664 const struct task_security_struct *tsec = current_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 struct inode_security_struct *dsec;
1666 struct superblock_security_struct *sbsec;
David Howells275bb412008-11-14 10:39:19 +11001667 u32 sid, newsid;
Thomas Liu2bf49692009-07-14 12:14:09 -04001668 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 int rc;
1670
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 dsec = dir->i_security;
1672 sbsec = dir->i_sb->s_security;
1673
David Howells275bb412008-11-14 10:39:19 +11001674 sid = tsec->sid;
1675 newsid = tsec->create_sid;
1676
Eric Paris50c205f2012-04-04 15:01:43 -04001677 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04001678 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
David Howells275bb412008-11-14 10:39:19 +11001680 rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 DIR__ADD_NAME | DIR__SEARCH,
1682 &ad);
1683 if (rc)
1684 return rc;
1685
Eric Paris12f348b2012-10-09 10:56:25 -04001686 if (!newsid || !(sbsec->flags & SBLABEL_MNT)) {
Eric Pariscb1e9222011-04-28 15:11:21 -04001687 rc = security_transition_sid(sid, dsec->sid, tclass,
1688 &dentry->d_name, &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 if (rc)
1690 return rc;
1691 }
1692
David Howells275bb412008-11-14 10:39:19 +11001693 rc = avc_has_perm(sid, newsid, tclass, FILE__CREATE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 if (rc)
1695 return rc;
1696
1697 return avc_has_perm(newsid, sbsec->sid,
1698 SECCLASS_FILESYSTEM,
1699 FILESYSTEM__ASSOCIATE, &ad);
1700}
1701
Michael LeMay4eb582c2006-06-26 00:24:57 -07001702/* Check whether a task can create a key. */
1703static int may_create_key(u32 ksid,
1704 struct task_struct *ctx)
1705{
David Howells275bb412008-11-14 10:39:19 +11001706 u32 sid = task_sid(ctx);
Michael LeMay4eb582c2006-06-26 00:24:57 -07001707
David Howells275bb412008-11-14 10:39:19 +11001708 return avc_has_perm(sid, ksid, SECCLASS_KEY, KEY__CREATE, NULL);
Michael LeMay4eb582c2006-06-26 00:24:57 -07001709}
1710
Eric Paris828dfe12008-04-17 13:17:49 -04001711#define MAY_LINK 0
1712#define MAY_UNLINK 1
1713#define MAY_RMDIR 2
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
1715/* Check whether a task can link, unlink, or rmdir a file/directory. */
1716static int may_link(struct inode *dir,
1717 struct dentry *dentry,
1718 int kind)
1719
1720{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 struct inode_security_struct *dsec, *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001722 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11001723 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 u32 av;
1725 int rc;
1726
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 dsec = dir->i_security;
1728 isec = dentry->d_inode->i_security;
1729
Eric Paris50c205f2012-04-04 15:01:43 -04001730 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04001731 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
1733 av = DIR__SEARCH;
1734 av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
David Howells275bb412008-11-14 10:39:19 +11001735 rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 if (rc)
1737 return rc;
1738
1739 switch (kind) {
1740 case MAY_LINK:
1741 av = FILE__LINK;
1742 break;
1743 case MAY_UNLINK:
1744 av = FILE__UNLINK;
1745 break;
1746 case MAY_RMDIR:
1747 av = DIR__RMDIR;
1748 break;
1749 default:
Eric Paris744ba352008-04-17 11:52:44 -04001750 printk(KERN_WARNING "SELinux: %s: unrecognized kind %d\n",
1751 __func__, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 return 0;
1753 }
1754
David Howells275bb412008-11-14 10:39:19 +11001755 rc = avc_has_perm(sid, isec->sid, isec->sclass, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 return rc;
1757}
1758
1759static inline int may_rename(struct inode *old_dir,
1760 struct dentry *old_dentry,
1761 struct inode *new_dir,
1762 struct dentry *new_dentry)
1763{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001765 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11001766 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 u32 av;
1768 int old_is_dir, new_is_dir;
1769 int rc;
1770
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 old_dsec = old_dir->i_security;
1772 old_isec = old_dentry->d_inode->i_security;
1773 old_is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
1774 new_dsec = new_dir->i_security;
1775
Eric Paris50c205f2012-04-04 15:01:43 -04001776 ad.type = LSM_AUDIT_DATA_DENTRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
Eric Parisa2694342011-04-25 13:10:27 -04001778 ad.u.dentry = old_dentry;
David Howells275bb412008-11-14 10:39:19 +11001779 rc = avc_has_perm(sid, old_dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 DIR__REMOVE_NAME | DIR__SEARCH, &ad);
1781 if (rc)
1782 return rc;
David Howells275bb412008-11-14 10:39:19 +11001783 rc = avc_has_perm(sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 old_isec->sclass, FILE__RENAME, &ad);
1785 if (rc)
1786 return rc;
1787 if (old_is_dir && new_dir != old_dir) {
David Howells275bb412008-11-14 10:39:19 +11001788 rc = avc_has_perm(sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 old_isec->sclass, DIR__REPARENT, &ad);
1790 if (rc)
1791 return rc;
1792 }
1793
Eric Parisa2694342011-04-25 13:10:27 -04001794 ad.u.dentry = new_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 av = DIR__ADD_NAME | DIR__SEARCH;
1796 if (new_dentry->d_inode)
1797 av |= DIR__REMOVE_NAME;
David Howells275bb412008-11-14 10:39:19 +11001798 rc = avc_has_perm(sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 if (rc)
1800 return rc;
1801 if (new_dentry->d_inode) {
1802 new_isec = new_dentry->d_inode->i_security;
1803 new_is_dir = S_ISDIR(new_dentry->d_inode->i_mode);
David Howells275bb412008-11-14 10:39:19 +11001804 rc = avc_has_perm(sid, new_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 new_isec->sclass,
1806 (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
1807 if (rc)
1808 return rc;
1809 }
1810
1811 return 0;
1812}
1813
1814/* Check whether a task can perform a filesystem operation. */
David Howells88e67f32008-11-14 10:39:21 +11001815static int superblock_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 struct super_block *sb,
1817 u32 perms,
Thomas Liu2bf49692009-07-14 12:14:09 -04001818 struct common_audit_data *ad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 struct superblock_security_struct *sbsec;
David Howells88e67f32008-11-14 10:39:21 +11001821 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 sbsec = sb->s_security;
David Howells275bb412008-11-14 10:39:19 +11001824 return avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825}
1826
1827/* Convert a Linux mode and permission mask to an access vector. */
1828static inline u32 file_mask_to_av(int mode, int mask)
1829{
1830 u32 av = 0;
1831
Al Virodba19c62011-07-25 20:49:29 -04001832 if (!S_ISDIR(mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 if (mask & MAY_EXEC)
1834 av |= FILE__EXECUTE;
1835 if (mask & MAY_READ)
1836 av |= FILE__READ;
1837
1838 if (mask & MAY_APPEND)
1839 av |= FILE__APPEND;
1840 else if (mask & MAY_WRITE)
1841 av |= FILE__WRITE;
1842
1843 } else {
1844 if (mask & MAY_EXEC)
1845 av |= DIR__SEARCH;
1846 if (mask & MAY_WRITE)
1847 av |= DIR__WRITE;
1848 if (mask & MAY_READ)
1849 av |= DIR__READ;
1850 }
1851
1852 return av;
1853}
1854
1855/* Convert a Linux file to an access vector. */
1856static inline u32 file_to_av(struct file *file)
1857{
1858 u32 av = 0;
1859
1860 if (file->f_mode & FMODE_READ)
1861 av |= FILE__READ;
1862 if (file->f_mode & FMODE_WRITE) {
1863 if (file->f_flags & O_APPEND)
1864 av |= FILE__APPEND;
1865 else
1866 av |= FILE__WRITE;
1867 }
Stephen Smalley0794c662008-03-17 08:55:18 -04001868 if (!av) {
1869 /*
1870 * Special file opened with flags 3 for ioctl-only use.
1871 */
1872 av = FILE__IOCTL;
1873 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
1875 return av;
1876}
1877
Eric Paris8b6a5a32008-10-29 17:06:46 -04001878/*
1879 * Convert a file to an access vector and include the correct open
1880 * open permission.
1881 */
1882static inline u32 open_file_to_av(struct file *file)
1883{
1884 u32 av = file_to_av(file);
1885
Eric Paris49b7b8d2010-07-23 11:44:09 -04001886 if (selinux_policycap_openperm)
1887 av |= FILE__OPEN;
1888
Eric Paris8b6a5a32008-10-29 17:06:46 -04001889 return av;
1890}
1891
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892/* Hook functions begin here. */
1893
Ingo Molnar9e488582009-05-07 19:26:19 +10001894static int selinux_ptrace_access_check(struct task_struct *child,
David Howells5cd9c582008-08-14 11:37:28 +01001895 unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 int rc;
1898
Ingo Molnar9e488582009-05-07 19:26:19 +10001899 rc = cap_ptrace_access_check(child, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 if (rc)
1901 return rc;
1902
Eric Paris69f594a2012-01-03 12:25:15 -05001903 if (mode & PTRACE_MODE_READ) {
David Howells275bb412008-11-14 10:39:19 +11001904 u32 sid = current_sid();
1905 u32 csid = task_sid(child);
1906 return avc_has_perm(sid, csid, SECCLASS_FILE, FILE__READ, NULL);
Stephen Smalley006ebb42008-05-19 08:32:49 -04001907 }
1908
David Howells3b11a1d2008-11-14 10:39:26 +11001909 return current_has_perm(child, PROCESS__PTRACE);
David Howells5cd9c582008-08-14 11:37:28 +01001910}
1911
1912static int selinux_ptrace_traceme(struct task_struct *parent)
1913{
1914 int rc;
1915
Eric Paris200ac532009-02-12 15:01:04 -05001916 rc = cap_ptrace_traceme(parent);
David Howells5cd9c582008-08-14 11:37:28 +01001917 if (rc)
1918 return rc;
1919
1920 return task_has_perm(parent, current, PROCESS__PTRACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921}
1922
1923static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
Eric Paris828dfe12008-04-17 13:17:49 -04001924 kernel_cap_t *inheritable, kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925{
1926 int error;
1927
David Howells3b11a1d2008-11-14 10:39:26 +11001928 error = current_has_perm(target, PROCESS__GETCAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 if (error)
1930 return error;
1931
Eric Paris200ac532009-02-12 15:01:04 -05001932 return cap_capget(target, effective, inheritable, permitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933}
1934
David Howellsd84f4f92008-11-14 10:39:23 +11001935static int selinux_capset(struct cred *new, const struct cred *old,
1936 const kernel_cap_t *effective,
1937 const kernel_cap_t *inheritable,
1938 const kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939{
1940 int error;
1941
Eric Paris200ac532009-02-12 15:01:04 -05001942 error = cap_capset(new, old,
David Howellsd84f4f92008-11-14 10:39:23 +11001943 effective, inheritable, permitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 if (error)
1945 return error;
1946
David Howellsd84f4f92008-11-14 10:39:23 +11001947 return cred_has_perm(old, new, PROCESS__SETCAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948}
1949
James Morris5626d3e2009-01-30 10:05:06 +11001950/*
1951 * (This comment used to live with the selinux_task_setuid hook,
1952 * which was removed).
1953 *
1954 * Since setuid only affects the current process, and since the SELinux
1955 * controls are not based on the Linux identity attributes, SELinux does not
1956 * need to control this operation. However, SELinux does control the use of
1957 * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
1958 */
1959
Eric Paris6a9de492012-01-03 12:25:14 -05001960static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
1961 int cap, int audit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962{
1963 int rc;
1964
Eric Paris6a9de492012-01-03 12:25:14 -05001965 rc = cap_capable(cred, ns, cap, audit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 if (rc)
1967 return rc;
1968
Eric Paris6a9de492012-01-03 12:25:14 -05001969 return cred_has_capability(cred, cap, audit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970}
1971
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
1973{
David Howells88e67f32008-11-14 10:39:21 +11001974 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 int rc = 0;
1976
1977 if (!sb)
1978 return 0;
1979
1980 switch (cmds) {
Eric Paris828dfe12008-04-17 13:17:49 -04001981 case Q_SYNC:
1982 case Q_QUOTAON:
1983 case Q_QUOTAOFF:
1984 case Q_SETINFO:
1985 case Q_SETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11001986 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04001987 break;
1988 case Q_GETFMT:
1989 case Q_GETINFO:
1990 case Q_GETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11001991 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04001992 break;
1993 default:
1994 rc = 0; /* let the kernel handle invalid cmds */
1995 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 }
1997 return rc;
1998}
1999
2000static int selinux_quota_on(struct dentry *dentry)
2001{
David Howells88e67f32008-11-14 10:39:21 +11002002 const struct cred *cred = current_cred();
2003
Eric Paris2875fa02011-04-28 16:04:24 -04002004 return dentry_has_perm(cred, dentry, FILE__QUOTAON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005}
2006
Eric Paris12b30522010-11-15 18:36:29 -05002007static int selinux_syslog(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008{
2009 int rc;
2010
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 switch (type) {
Kees Cookd78ca3c2010-02-03 15:37:13 -08002012 case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */
2013 case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */
Eric Paris828dfe12008-04-17 13:17:49 -04002014 rc = task_has_system(current, SYSTEM__SYSLOG_READ);
2015 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08002016 case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */
2017 case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */
2018 /* Set level of messages printed to console */
2019 case SYSLOG_ACTION_CONSOLE_LEVEL:
Eric Paris828dfe12008-04-17 13:17:49 -04002020 rc = task_has_system(current, SYSTEM__SYSLOG_CONSOLE);
2021 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08002022 case SYSLOG_ACTION_CLOSE: /* Close log */
2023 case SYSLOG_ACTION_OPEN: /* Open log */
2024 case SYSLOG_ACTION_READ: /* Read from log */
2025 case SYSLOG_ACTION_READ_CLEAR: /* Read/clear last kernel messages */
2026 case SYSLOG_ACTION_CLEAR: /* Clear ring buffer */
Eric Paris828dfe12008-04-17 13:17:49 -04002027 default:
2028 rc = task_has_system(current, SYSTEM__SYSLOG_MOD);
2029 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 }
2031 return rc;
2032}
2033
2034/*
2035 * Check that a process has enough memory to allocate a new virtual
2036 * mapping. 0 means there is enough memory for the allocation to
2037 * succeed and -ENOMEM implies there is not.
2038 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 * Do not audit the selinux permission check, as this is applied to all
2040 * processes that allocate mappings.
2041 */
Alan Cox34b4e4a2007-08-22 14:01:28 -07002042static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043{
2044 int rc, cap_sys_admin = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
Eric Paris6a9de492012-01-03 12:25:14 -05002046 rc = selinux_capable(current_cred(), &init_user_ns, CAP_SYS_ADMIN,
David Howells3699c532009-01-06 22:27:01 +00002047 SECURITY_CAP_NOAUDIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 if (rc == 0)
2049 cap_sys_admin = 1;
2050
Alan Cox34b4e4a2007-08-22 14:01:28 -07002051 return __vm_enough_memory(mm, pages, cap_sys_admin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052}
2053
2054/* binprm security operations */
2055
David Howellsa6f76f22008-11-14 10:39:24 +11002056static int selinux_bprm_set_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057{
David Howellsa6f76f22008-11-14 10:39:24 +11002058 const struct task_security_struct *old_tsec;
2059 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 struct inode_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04002061 struct common_audit_data ad;
Al Viro496ad9a2013-01-23 17:07:38 -05002062 struct inode *inode = file_inode(bprm->file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 int rc;
2064
Eric Paris200ac532009-02-12 15:01:04 -05002065 rc = cap_bprm_set_creds(bprm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 if (rc)
2067 return rc;
2068
David Howellsa6f76f22008-11-14 10:39:24 +11002069 /* SELinux context only depends on initial program or script and not
2070 * the script interpreter */
2071 if (bprm->cred_prepared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 return 0;
2073
David Howellsa6f76f22008-11-14 10:39:24 +11002074 old_tsec = current_security();
2075 new_tsec = bprm->cred->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 isec = inode->i_security;
2077
2078 /* Default to the current task SID. */
David Howellsa6f76f22008-11-14 10:39:24 +11002079 new_tsec->sid = old_tsec->sid;
2080 new_tsec->osid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
Michael LeMay28eba5b2006-06-27 02:53:42 -07002082 /* Reset fs, key, and sock SIDs on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002083 new_tsec->create_sid = 0;
2084 new_tsec->keycreate_sid = 0;
2085 new_tsec->sockcreate_sid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
David Howellsa6f76f22008-11-14 10:39:24 +11002087 if (old_tsec->exec_sid) {
2088 new_tsec->sid = old_tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 /* Reset exec SID on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002090 new_tsec->exec_sid = 0;
Andy Lutomirski259e5e62012-04-12 16:47:50 -05002091
2092 /*
2093 * Minimize confusion: if no_new_privs and a transition is
2094 * explicitly requested, then fail the exec.
2095 */
2096 if (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS)
2097 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 } else {
2099 /* Check for a default transition on this program. */
David Howellsa6f76f22008-11-14 10:39:24 +11002100 rc = security_transition_sid(old_tsec->sid, isec->sid,
Eric Paris652bb9b2011-02-01 11:05:40 -05002101 SECCLASS_PROCESS, NULL,
2102 &new_tsec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 if (rc)
2104 return rc;
2105 }
2106
Eric Paris50c205f2012-04-04 15:01:43 -04002107 ad.type = LSM_AUDIT_DATA_PATH;
Eric Parisf48b7392011-04-25 12:54:27 -04002108 ad.u.path = bprm->file->f_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109
Andy Lutomirski259e5e62012-04-12 16:47:50 -05002110 if ((bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID) ||
2111 (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS))
David Howellsa6f76f22008-11-14 10:39:24 +11002112 new_tsec->sid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113
David Howellsa6f76f22008-11-14 10:39:24 +11002114 if (new_tsec->sid == old_tsec->sid) {
2115 rc = avc_has_perm(old_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
2117 if (rc)
2118 return rc;
2119 } else {
2120 /* Check permissions for the transition. */
David Howellsa6f76f22008-11-14 10:39:24 +11002121 rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
2123 if (rc)
2124 return rc;
2125
David Howellsa6f76f22008-11-14 10:39:24 +11002126 rc = avc_has_perm(new_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
2128 if (rc)
2129 return rc;
2130
David Howellsa6f76f22008-11-14 10:39:24 +11002131 /* Check for shared state */
2132 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
2133 rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
2134 SECCLASS_PROCESS, PROCESS__SHARE,
2135 NULL);
2136 if (rc)
2137 return -EPERM;
2138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
David Howellsa6f76f22008-11-14 10:39:24 +11002140 /* Make sure that anyone attempting to ptrace over a task that
2141 * changes its SID has the appropriate permit */
2142 if (bprm->unsafe &
2143 (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
2144 struct task_struct *tracer;
2145 struct task_security_struct *sec;
2146 u32 ptsid = 0;
2147
2148 rcu_read_lock();
Tejun Heo06d98472011-06-17 16:50:40 +02002149 tracer = ptrace_parent(current);
David Howellsa6f76f22008-11-14 10:39:24 +11002150 if (likely(tracer != NULL)) {
2151 sec = __task_cred(tracer)->security;
2152 ptsid = sec->sid;
2153 }
2154 rcu_read_unlock();
2155
2156 if (ptsid != 0) {
2157 rc = avc_has_perm(ptsid, new_tsec->sid,
2158 SECCLASS_PROCESS,
2159 PROCESS__PTRACE, NULL);
2160 if (rc)
2161 return -EPERM;
2162 }
2163 }
2164
2165 /* Clear any possibly unsafe personality bits on exec: */
2166 bprm->per_clear |= PER_CLEAR_ON_SETID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 }
2168
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 return 0;
2170}
2171
Eric Paris828dfe12008-04-17 13:17:49 -04002172static int selinux_bprm_secureexec(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173{
Paul Moore5fb49872010-04-22 14:46:19 -04002174 const struct task_security_struct *tsec = current_security();
David Howells275bb412008-11-14 10:39:19 +11002175 u32 sid, osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 int atsecure = 0;
2177
David Howells275bb412008-11-14 10:39:19 +11002178 sid = tsec->sid;
2179 osid = tsec->osid;
2180
2181 if (osid != sid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 /* Enable secure mode for SIDs transitions unless
2183 the noatsecure permission is granted between
2184 the two SIDs, i.e. ahp returns 0. */
David Howells275bb412008-11-14 10:39:19 +11002185 atsecure = avc_has_perm(osid, sid,
David Howellsa6f76f22008-11-14 10:39:24 +11002186 SECCLASS_PROCESS,
2187 PROCESS__NOATSECURE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 }
2189
Eric Paris200ac532009-02-12 15:01:04 -05002190 return (atsecure || cap_bprm_secureexec(bprm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191}
2192
Al Viroc3c073f2012-08-21 22:32:06 -04002193static int match_file(const void *p, struct file *file, unsigned fd)
2194{
2195 return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0;
2196}
2197
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198/* Derived from fs/exec.c:flush_old_files. */
David Howells745ca242008-11-14 10:39:22 +11002199static inline void flush_unauthorized_files(const struct cred *cred,
2200 struct files_struct *files)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 struct file *file, *devnull = NULL;
Stephen Smalleyb20c8122006-09-25 23:32:03 -07002203 struct tty_struct *tty;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002204 int drop_tty = 0;
Al Viroc3c073f2012-08-21 22:32:06 -04002205 unsigned n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002207 tty = get_current_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 if (tty) {
Nick Pigginee2ffa02010-08-18 04:37:35 +10002209 spin_lock(&tty_files_lock);
Eric Paris37dd0bd2008-10-31 17:40:00 -04002210 if (!list_empty(&tty->tty_files)) {
Nick Piggind996b622010-08-18 04:37:36 +10002211 struct tty_file_private *file_priv;
Eric Paris37dd0bd2008-10-31 17:40:00 -04002212
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 /* Revalidate access to controlling tty.
David Howells13f8e982013-06-13 23:37:55 +01002214 Use file_path_has_perm on the tty path directly
2215 rather than using file_has_perm, as this particular
2216 open file may belong to another process and we are
2217 only interested in the inode-based check here. */
Nick Piggind996b622010-08-18 04:37:36 +10002218 file_priv = list_first_entry(&tty->tty_files,
2219 struct tty_file_private, list);
2220 file = file_priv->file;
David Howells13f8e982013-06-13 23:37:55 +01002221 if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE))
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002222 drop_tty = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 }
Nick Pigginee2ffa02010-08-18 04:37:35 +10002224 spin_unlock(&tty_files_lock);
Alan Cox452a00d2008-10-13 10:39:13 +01002225 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 }
Eric W. Biederman98a27ba2007-05-08 00:26:56 -07002227 /* Reset controlling tty. */
2228 if (drop_tty)
2229 no_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230
2231 /* Revalidate access to inherited open files. */
Al Viroc3c073f2012-08-21 22:32:06 -04002232 n = iterate_fd(files, 0, match_file, cred);
2233 if (!n) /* none found? */
2234 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
Al Viroc3c073f2012-08-21 22:32:06 -04002236 devnull = dentry_open(&selinux_null, O_RDWR, cred);
Al Viro45525b22012-10-16 13:30:07 -04002237 if (IS_ERR(devnull))
2238 devnull = NULL;
2239 /* replace all the matching ones with this */
2240 do {
2241 replace_fd(n - 1, devnull, 0);
2242 } while ((n = iterate_fd(files, n, match_file, cred)) != 0);
2243 if (devnull)
Al Viroc3c073f2012-08-21 22:32:06 -04002244 fput(devnull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245}
2246
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247/*
David Howellsa6f76f22008-11-14 10:39:24 +11002248 * Prepare a process for imminent new credential changes due to exec
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 */
David Howellsa6f76f22008-11-14 10:39:24 +11002250static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251{
David Howellsa6f76f22008-11-14 10:39:24 +11002252 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 struct rlimit *rlim, *initrlim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 int rc, i;
2255
David Howellsa6f76f22008-11-14 10:39:24 +11002256 new_tsec = bprm->cred->security;
2257 if (new_tsec->sid == new_tsec->osid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 return;
2259
2260 /* Close files for which the new task SID is not authorized. */
David Howellsa6f76f22008-11-14 10:39:24 +11002261 flush_unauthorized_files(bprm->cred, current->files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
David Howellsa6f76f22008-11-14 10:39:24 +11002263 /* Always clear parent death signal on SID transitions. */
2264 current->pdeath_signal = 0;
2265
2266 /* Check whether the new SID can inherit resource limits from the old
2267 * SID. If not, reset all soft limits to the lower of the current
2268 * task's hard limit and the init task's soft limit.
2269 *
2270 * Note that the setting of hard limits (even to lower them) can be
2271 * controlled by the setrlimit check. The inclusion of the init task's
2272 * soft limit into the computation is to avoid resetting soft limits
2273 * higher than the default soft limit for cases where the default is
2274 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2275 */
2276 rc = avc_has_perm(new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
2277 PROCESS__RLIMITINH, NULL);
2278 if (rc) {
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002279 /* protect against do_prlimit() */
2280 task_lock(current);
David Howellsa6f76f22008-11-14 10:39:24 +11002281 for (i = 0; i < RLIM_NLIMITS; i++) {
2282 rlim = current->signal->rlim + i;
2283 initrlim = init_task.signal->rlim + i;
2284 rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
2285 }
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002286 task_unlock(current);
2287 update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
David Howellsa6f76f22008-11-14 10:39:24 +11002288 }
2289}
2290
2291/*
2292 * Clean up the process immediately after the installation of new credentials
2293 * due to exec
2294 */
2295static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
2296{
2297 const struct task_security_struct *tsec = current_security();
2298 struct itimerval itimer;
David Howellsa6f76f22008-11-14 10:39:24 +11002299 u32 osid, sid;
2300 int rc, i;
David Howellsa6f76f22008-11-14 10:39:24 +11002301
David Howellsa6f76f22008-11-14 10:39:24 +11002302 osid = tsec->osid;
2303 sid = tsec->sid;
2304
2305 if (sid == osid)
2306 return;
2307
2308 /* Check whether the new SID can inherit signal state from the old SID.
2309 * If not, clear itimers to avoid subsequent signal generation and
2310 * flush and unblock signals.
2311 *
2312 * This must occur _after_ the task SID has been updated so that any
2313 * kill done after the flush will be checked against the new SID.
2314 */
2315 rc = avc_has_perm(osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 if (rc) {
2317 memset(&itimer, 0, sizeof itimer);
2318 for (i = 0; i < 3; i++)
2319 do_setitimer(i, &itimer, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 spin_lock_irq(&current->sighand->siglock);
David Howells3bcac022009-04-29 13:45:05 +01002321 if (!(current->signal->flags & SIGNAL_GROUP_EXIT)) {
2322 __flush_signals(current);
2323 flush_signal_handlers(current, 1);
2324 sigemptyset(&current->blocked);
2325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 spin_unlock_irq(&current->sighand->siglock);
2327 }
2328
David Howellsa6f76f22008-11-14 10:39:24 +11002329 /* Wake up the parent if it is waiting so that it can recheck
2330 * wait permission to the new task SID. */
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002331 read_lock(&tasklist_lock);
Oleg Nesterov0b7570e2009-09-23 15:56:46 -07002332 __wake_up_parent(current, current->real_parent);
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002333 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334}
2335
2336/* superblock security operations */
2337
2338static int selinux_sb_alloc_security(struct super_block *sb)
2339{
2340 return superblock_alloc_security(sb);
2341}
2342
2343static void selinux_sb_free_security(struct super_block *sb)
2344{
2345 superblock_free_security(sb);
2346}
2347
2348static inline int match_prefix(char *prefix, int plen, char *option, int olen)
2349{
2350 if (plen > olen)
2351 return 0;
2352
2353 return !memcmp(prefix, option, plen);
2354}
2355
2356static inline int selinux_option(char *option, int len)
2357{
Eric Paris832cbd92008-04-01 13:24:09 -04002358 return (match_prefix(CONTEXT_STR, sizeof(CONTEXT_STR)-1, option, len) ||
2359 match_prefix(FSCONTEXT_STR, sizeof(FSCONTEXT_STR)-1, option, len) ||
2360 match_prefix(DEFCONTEXT_STR, sizeof(DEFCONTEXT_STR)-1, option, len) ||
David P. Quigley11689d42009-01-16 09:22:03 -05002361 match_prefix(ROOTCONTEXT_STR, sizeof(ROOTCONTEXT_STR)-1, option, len) ||
2362 match_prefix(LABELSUPP_STR, sizeof(LABELSUPP_STR)-1, option, len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363}
2364
2365static inline void take_option(char **to, char *from, int *first, int len)
2366{
2367 if (!*first) {
2368 **to = ',';
2369 *to += 1;
Cory Olmo3528a952006-09-29 01:58:44 -07002370 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 *first = 0;
2372 memcpy(*to, from, len);
2373 *to += len;
2374}
2375
Eric Paris828dfe12008-04-17 13:17:49 -04002376static inline void take_selinux_option(char **to, char *from, int *first,
2377 int len)
Cory Olmo3528a952006-09-29 01:58:44 -07002378{
2379 int current_size = 0;
2380
2381 if (!*first) {
2382 **to = '|';
2383 *to += 1;
Eric Paris828dfe12008-04-17 13:17:49 -04002384 } else
Cory Olmo3528a952006-09-29 01:58:44 -07002385 *first = 0;
2386
2387 while (current_size < len) {
2388 if (*from != '"') {
2389 **to = *from;
2390 *to += 1;
2391 }
2392 from += 1;
2393 current_size += 1;
2394 }
2395}
2396
Eric Parise0007522008-03-05 10:31:54 -05002397static int selinux_sb_copy_data(char *orig, char *copy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398{
2399 int fnosec, fsec, rc = 0;
2400 char *in_save, *in_curr, *in_end;
2401 char *sec_curr, *nosec_save, *nosec;
Cory Olmo3528a952006-09-29 01:58:44 -07002402 int open_quote = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
2404 in_curr = orig;
2405 sec_curr = copy;
2406
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 nosec = (char *)get_zeroed_page(GFP_KERNEL);
2408 if (!nosec) {
2409 rc = -ENOMEM;
2410 goto out;
2411 }
2412
2413 nosec_save = nosec;
2414 fnosec = fsec = 1;
2415 in_save = in_end = orig;
2416
2417 do {
Cory Olmo3528a952006-09-29 01:58:44 -07002418 if (*in_end == '"')
2419 open_quote = !open_quote;
2420 if ((*in_end == ',' && open_quote == 0) ||
2421 *in_end == '\0') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 int len = in_end - in_curr;
2423
2424 if (selinux_option(in_curr, len))
Cory Olmo3528a952006-09-29 01:58:44 -07002425 take_selinux_option(&sec_curr, in_curr, &fsec, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 else
2427 take_option(&nosec, in_curr, &fnosec, len);
2428
2429 in_curr = in_end + 1;
2430 }
2431 } while (*in_end++);
2432
Eric Paris6931dfc2005-06-30 02:58:51 -07002433 strcpy(in_save, nosec_save);
Gerald Schaeferda3caa22005-06-21 17:15:18 -07002434 free_page((unsigned long)nosec_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435out:
2436 return rc;
2437}
2438
Eric Paris026eb162011-03-03 16:09:14 -05002439static int selinux_sb_remount(struct super_block *sb, void *data)
2440{
2441 int rc, i, *flags;
2442 struct security_mnt_opts opts;
2443 char *secdata, **mount_options;
2444 struct superblock_security_struct *sbsec = sb->s_security;
2445
2446 if (!(sbsec->flags & SE_SBINITIALIZED))
2447 return 0;
2448
2449 if (!data)
2450 return 0;
2451
2452 if (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
2453 return 0;
2454
2455 security_init_mnt_opts(&opts);
2456 secdata = alloc_secdata();
2457 if (!secdata)
2458 return -ENOMEM;
2459 rc = selinux_sb_copy_data(data, secdata);
2460 if (rc)
2461 goto out_free_secdata;
2462
2463 rc = selinux_parse_opts_str(secdata, &opts);
2464 if (rc)
2465 goto out_free_secdata;
2466
2467 mount_options = opts.mnt_opts;
2468 flags = opts.mnt_opts_flags;
2469
2470 for (i = 0; i < opts.num_mnt_opts; i++) {
2471 u32 sid;
2472 size_t len;
2473
Eric Paris12f348b2012-10-09 10:56:25 -04002474 if (flags[i] == SBLABEL_MNT)
Eric Paris026eb162011-03-03 16:09:14 -05002475 continue;
2476 len = strlen(mount_options[i]);
2477 rc = security_context_to_sid(mount_options[i], len, &sid);
2478 if (rc) {
2479 printk(KERN_WARNING "SELinux: security_context_to_sid"
Linus Torvalds29b1deb2013-12-15 11:17:45 -08002480 "(%s) failed for (dev %s, type %s) errno=%d\n",
2481 mount_options[i], sb->s_id, sb->s_type->name, rc);
Eric Paris026eb162011-03-03 16:09:14 -05002482 goto out_free_opts;
2483 }
2484 rc = -EINVAL;
2485 switch (flags[i]) {
2486 case FSCONTEXT_MNT:
2487 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
2488 goto out_bad_option;
2489 break;
2490 case CONTEXT_MNT:
2491 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
2492 goto out_bad_option;
2493 break;
2494 case ROOTCONTEXT_MNT: {
2495 struct inode_security_struct *root_isec;
2496 root_isec = sb->s_root->d_inode->i_security;
2497
2498 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
2499 goto out_bad_option;
2500 break;
2501 }
2502 case DEFCONTEXT_MNT:
2503 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
2504 goto out_bad_option;
2505 break;
2506 default:
2507 goto out_free_opts;
2508 }
2509 }
2510
2511 rc = 0;
2512out_free_opts:
2513 security_free_mnt_opts(&opts);
2514out_free_secdata:
2515 free_secdata(secdata);
2516 return rc;
2517out_bad_option:
2518 printk(KERN_WARNING "SELinux: unable to change security options "
Linus Torvalds29b1deb2013-12-15 11:17:45 -08002519 "during remount (dev %s, type=%s)\n", sb->s_id,
2520 sb->s_type->name);
Eric Paris026eb162011-03-03 16:09:14 -05002521 goto out_free_opts;
2522}
2523
James Morris12204e22008-12-19 10:44:42 +11002524static int selinux_sb_kern_mount(struct super_block *sb, int flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525{
David Howells88e67f32008-11-14 10:39:21 +11002526 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002527 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 int rc;
2529
2530 rc = superblock_doinit(sb, data);
2531 if (rc)
2532 return rc;
2533
James Morris74192242008-12-19 11:41:10 +11002534 /* Allow all mounts performed by the kernel */
2535 if (flags & MS_KERNMOUNT)
2536 return 0;
2537
Eric Paris50c205f2012-04-04 15:01:43 -04002538 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002539 ad.u.dentry = sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002540 return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541}
2542
David Howells726c3342006-06-23 02:02:58 -07002543static int selinux_sb_statfs(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544{
David Howells88e67f32008-11-14 10:39:21 +11002545 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002546 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547
Eric Paris50c205f2012-04-04 15:01:43 -04002548 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002549 ad.u.dentry = dentry->d_sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002550 return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551}
2552
Al Viro808d4e32012-10-11 11:42:01 -04002553static int selinux_mount(const char *dev_name,
Al Virob5266eb2008-03-22 17:48:24 -04002554 struct path *path,
Al Viro808d4e32012-10-11 11:42:01 -04002555 const char *type,
Eric Paris828dfe12008-04-17 13:17:49 -04002556 unsigned long flags,
2557 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558{
David Howells88e67f32008-11-14 10:39:21 +11002559 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560
2561 if (flags & MS_REMOUNT)
Al Virod8c95842011-12-07 18:16:57 -05002562 return superblock_has_perm(cred, path->dentry->d_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002563 FILESYSTEM__REMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 else
Eric Paris2875fa02011-04-28 16:04:24 -04002565 return path_has_perm(cred, path, FILE__MOUNTON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566}
2567
2568static int selinux_umount(struct vfsmount *mnt, int flags)
2569{
David Howells88e67f32008-11-14 10:39:21 +11002570 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
David Howells88e67f32008-11-14 10:39:21 +11002572 return superblock_has_perm(cred, mnt->mnt_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002573 FILESYSTEM__UNMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574}
2575
2576/* inode security operations */
2577
2578static int selinux_inode_alloc_security(struct inode *inode)
2579{
2580 return inode_alloc_security(inode);
2581}
2582
2583static void selinux_inode_free_security(struct inode *inode)
2584{
2585 inode_free_security(inode);
2586}
2587
David Quigleyd47be3d2013-05-22 12:50:34 -04002588static int selinux_dentry_init_security(struct dentry *dentry, int mode,
2589 struct qstr *name, void **ctx,
2590 u32 *ctxlen)
2591{
2592 const struct cred *cred = current_cred();
2593 struct task_security_struct *tsec;
2594 struct inode_security_struct *dsec;
2595 struct superblock_security_struct *sbsec;
2596 struct inode *dir = dentry->d_parent->d_inode;
2597 u32 newsid;
2598 int rc;
2599
2600 tsec = cred->security;
2601 dsec = dir->i_security;
2602 sbsec = dir->i_sb->s_security;
2603
2604 if (tsec->create_sid && sbsec->behavior != SECURITY_FS_USE_MNTPOINT) {
2605 newsid = tsec->create_sid;
2606 } else {
2607 rc = security_transition_sid(tsec->sid, dsec->sid,
2608 inode_mode_to_security_class(mode),
2609 name,
2610 &newsid);
2611 if (rc) {
2612 printk(KERN_WARNING
2613 "%s: security_transition_sid failed, rc=%d\n",
2614 __func__, -rc);
2615 return rc;
2616 }
2617 }
2618
2619 return security_sid_to_context(newsid, (char **)ctx, ctxlen);
2620}
2621
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002622static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +09002623 const struct qstr *qstr,
2624 const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -05002625 void **value, size_t *len)
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002626{
Paul Moore5fb49872010-04-22 14:46:19 -04002627 const struct task_security_struct *tsec = current_security();
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002628 struct inode_security_struct *dsec;
2629 struct superblock_security_struct *sbsec;
David Howells275bb412008-11-14 10:39:19 +11002630 u32 sid, newsid, clen;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002631 int rc;
Tetsuo Handa95489062013-07-25 05:44:02 +09002632 char *context;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002633
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002634 dsec = dir->i_security;
2635 sbsec = dir->i_sb->s_security;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002636
David Howells275bb412008-11-14 10:39:19 +11002637 sid = tsec->sid;
2638 newsid = tsec->create_sid;
2639
Eric Paris415103f2010-12-02 16:13:40 -05002640 if ((sbsec->flags & SE_SBINITIALIZED) &&
2641 (sbsec->behavior == SECURITY_FS_USE_MNTPOINT))
2642 newsid = sbsec->mntpoint_sid;
Eric Paris12f348b2012-10-09 10:56:25 -04002643 else if (!newsid || !(sbsec->flags & SBLABEL_MNT)) {
David Howells275bb412008-11-14 10:39:19 +11002644 rc = security_transition_sid(sid, dsec->sid,
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002645 inode_mode_to_security_class(inode->i_mode),
Eric Paris652bb9b2011-02-01 11:05:40 -05002646 qstr, &newsid);
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002647 if (rc) {
2648 printk(KERN_WARNING "%s: "
2649 "security_transition_sid failed, rc=%d (dev=%s "
2650 "ino=%ld)\n",
Harvey Harrisondd6f9532008-03-06 10:03:59 +11002651 __func__,
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002652 -rc, inode->i_sb->s_id, inode->i_ino);
2653 return rc;
2654 }
2655 }
2656
Eric Paris296fddf2006-09-25 23:32:00 -07002657 /* Possibly defer initialization to selinux_complete_init. */
David P. Quigley0d90a7e2009-01-16 09:22:02 -05002658 if (sbsec->flags & SE_SBINITIALIZED) {
Eric Paris296fddf2006-09-25 23:32:00 -07002659 struct inode_security_struct *isec = inode->i_security;
2660 isec->sclass = inode_mode_to_security_class(inode->i_mode);
2661 isec->sid = newsid;
2662 isec->initialized = 1;
2663 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002664
Eric Paris12f348b2012-10-09 10:56:25 -04002665 if (!ss_initialized || !(sbsec->flags & SBLABEL_MNT))
Stephen Smalley25a74f32005-11-08 21:34:33 -08002666 return -EOPNOTSUPP;
2667
Tetsuo Handa95489062013-07-25 05:44:02 +09002668 if (name)
2669 *name = XATTR_SELINUX_SUFFIX;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002670
2671 if (value && len) {
Stephen Smalley12b29f32008-05-07 13:03:20 -04002672 rc = security_sid_to_context_force(newsid, &context, &clen);
Tetsuo Handa95489062013-07-25 05:44:02 +09002673 if (rc)
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002674 return rc;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002675 *value = context;
2676 *len = clen;
2677 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002678
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002679 return 0;
2680}
2681
Al Viro4acdaf22011-07-26 01:42:34 -04002682static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683{
2684 return may_create(dir, dentry, SECCLASS_FILE);
2685}
2686
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2688{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 return may_link(dir, old_dentry, MAY_LINK);
2690}
2691
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
2693{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 return may_link(dir, dentry, MAY_UNLINK);
2695}
2696
2697static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
2698{
2699 return may_create(dir, dentry, SECCLASS_LNK_FILE);
2700}
2701
Al Viro18bb1db2011-07-26 01:41:39 -04002702static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703{
2704 return may_create(dir, dentry, SECCLASS_DIR);
2705}
2706
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
2708{
2709 return may_link(dir, dentry, MAY_RMDIR);
2710}
2711
Al Viro1a67aaf2011-07-26 01:52:52 -04002712static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 return may_create(dir, dentry, inode_mode_to_security_class(mode));
2715}
2716
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
Eric Paris828dfe12008-04-17 13:17:49 -04002718 struct inode *new_inode, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719{
2720 return may_rename(old_inode, old_dentry, new_inode, new_dentry);
2721}
2722
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723static int selinux_inode_readlink(struct dentry *dentry)
2724{
David Howells88e67f32008-11-14 10:39:21 +11002725 const struct cred *cred = current_cred();
2726
Eric Paris2875fa02011-04-28 16:04:24 -04002727 return dentry_has_perm(cred, dentry, FILE__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728}
2729
2730static int selinux_inode_follow_link(struct dentry *dentry, struct nameidata *nameidata)
2731{
David Howells88e67f32008-11-14 10:39:21 +11002732 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733
Eric Paris2875fa02011-04-28 16:04:24 -04002734 return dentry_has_perm(cred, dentry, FILE__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735}
2736
Eric Parisd4cf970d2012-04-04 15:01:42 -04002737static noinline int audit_inode_permission(struct inode *inode,
2738 u32 perms, u32 audited, u32 denied,
2739 unsigned flags)
2740{
2741 struct common_audit_data ad;
Eric Parisd4cf970d2012-04-04 15:01:42 -04002742 struct inode_security_struct *isec = inode->i_security;
2743 int rc;
2744
Eric Paris50c205f2012-04-04 15:01:43 -04002745 ad.type = LSM_AUDIT_DATA_INODE;
Eric Parisd4cf970d2012-04-04 15:01:42 -04002746 ad.u.inode = inode;
2747
2748 rc = slow_avc_audit(current_sid(), isec->sid, isec->sclass, perms,
2749 audited, denied, &ad, flags);
2750 if (rc)
2751 return rc;
2752 return 0;
2753}
2754
Al Viroe74f71e2011-06-20 19:38:15 -04002755static int selinux_inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756{
David Howells88e67f32008-11-14 10:39:21 +11002757 const struct cred *cred = current_cred();
Eric Parisb782e0a2010-07-23 11:44:03 -04002758 u32 perms;
2759 bool from_access;
Al Virocf1dd1d2011-06-20 19:44:08 -04002760 unsigned flags = mask & MAY_NOT_BLOCK;
Eric Paris2e334052012-04-04 15:01:42 -04002761 struct inode_security_struct *isec;
2762 u32 sid;
2763 struct av_decision avd;
2764 int rc, rc2;
2765 u32 audited, denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766
Eric Parisb782e0a2010-07-23 11:44:03 -04002767 from_access = mask & MAY_ACCESS;
Eric Parisd09ca732010-07-23 11:43:57 -04002768 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
2769
Eric Parisb782e0a2010-07-23 11:44:03 -04002770 /* No permission to check. Existence test. */
2771 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773
Eric Paris2e334052012-04-04 15:01:42 -04002774 validate_creds(cred);
Eric Parisb782e0a2010-07-23 11:44:03 -04002775
Eric Paris2e334052012-04-04 15:01:42 -04002776 if (unlikely(IS_PRIVATE(inode)))
2777 return 0;
Eric Parisb782e0a2010-07-23 11:44:03 -04002778
2779 perms = file_mask_to_av(inode->i_mode, mask);
2780
Eric Paris2e334052012-04-04 15:01:42 -04002781 sid = cred_sid(cred);
2782 isec = inode->i_security;
2783
2784 rc = avc_has_perm_noaudit(sid, isec->sid, isec->sclass, perms, 0, &avd);
2785 audited = avc_audit_required(perms, &avd, rc,
2786 from_access ? FILE__AUDIT_ACCESS : 0,
2787 &denied);
2788 if (likely(!audited))
2789 return rc;
2790
Eric Parisd4cf970d2012-04-04 15:01:42 -04002791 rc2 = audit_inode_permission(inode, perms, audited, denied, flags);
Eric Paris2e334052012-04-04 15:01:42 -04002792 if (rc2)
2793 return rc2;
2794 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795}
2796
2797static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
2798{
David Howells88e67f32008-11-14 10:39:21 +11002799 const struct cred *cred = current_cred();
Amerigo Wangbc6a6002009-08-20 19:29:02 -07002800 unsigned int ia_valid = iattr->ia_valid;
Eric Paris95dbf732012-04-04 13:45:34 -04002801 __u32 av = FILE__WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802
Amerigo Wangbc6a6002009-08-20 19:29:02 -07002803 /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
2804 if (ia_valid & ATTR_FORCE) {
2805 ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
2806 ATTR_FORCE);
2807 if (!ia_valid)
2808 return 0;
2809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810
Amerigo Wangbc6a6002009-08-20 19:29:02 -07002811 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
2812 ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
Eric Paris2875fa02011-04-28 16:04:24 -04002813 return dentry_has_perm(cred, dentry, FILE__SETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814
Eric Paris3d2195c2012-07-06 14:13:30 -04002815 if (selinux_policycap_openperm && (ia_valid & ATTR_SIZE))
Eric Paris95dbf732012-04-04 13:45:34 -04002816 av |= FILE__OPEN;
2817
2818 return dentry_has_perm(cred, dentry, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819}
2820
2821static int selinux_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
2822{
David Howells88e67f32008-11-14 10:39:21 +11002823 const struct cred *cred = current_cred();
Eric Paris2875fa02011-04-28 16:04:24 -04002824 struct path path;
David Howells88e67f32008-11-14 10:39:21 +11002825
Eric Paris2875fa02011-04-28 16:04:24 -04002826 path.dentry = dentry;
2827 path.mnt = mnt;
2828
2829 return path_has_perm(cred, &path, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830}
2831
David Howells8f0cfa52008-04-29 00:59:41 -07002832static int selinux_inode_setotherxattr(struct dentry *dentry, const char *name)
Serge E. Hallynb5376772007-10-16 23:31:36 -07002833{
David Howells88e67f32008-11-14 10:39:21 +11002834 const struct cred *cred = current_cred();
2835
Serge E. Hallynb5376772007-10-16 23:31:36 -07002836 if (!strncmp(name, XATTR_SECURITY_PREFIX,
2837 sizeof XATTR_SECURITY_PREFIX - 1)) {
2838 if (!strcmp(name, XATTR_NAME_CAPS)) {
2839 if (!capable(CAP_SETFCAP))
2840 return -EPERM;
2841 } else if (!capable(CAP_SYS_ADMIN)) {
2842 /* A different attribute in the security namespace.
2843 Restrict to administrator. */
2844 return -EPERM;
2845 }
2846 }
2847
2848 /* Not an attribute we recognize, so just check the
2849 ordinary setattr permission. */
Eric Paris2875fa02011-04-28 16:04:24 -04002850 return dentry_has_perm(cred, dentry, FILE__SETATTR);
Serge E. Hallynb5376772007-10-16 23:31:36 -07002851}
2852
David Howells8f0cfa52008-04-29 00:59:41 -07002853static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
2854 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 struct inode *inode = dentry->d_inode;
2857 struct inode_security_struct *isec = inode->i_security;
2858 struct superblock_security_struct *sbsec;
Thomas Liu2bf49692009-07-14 12:14:09 -04002859 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11002860 u32 newsid, sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 int rc = 0;
2862
Serge E. Hallynb5376772007-10-16 23:31:36 -07002863 if (strcmp(name, XATTR_NAME_SELINUX))
2864 return selinux_inode_setotherxattr(dentry, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865
2866 sbsec = inode->i_sb->s_security;
Eric Paris12f348b2012-10-09 10:56:25 -04002867 if (!(sbsec->flags & SBLABEL_MNT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 return -EOPNOTSUPP;
2869
Serge E. Hallyn2e149672011-03-23 16:43:26 -07002870 if (!inode_owner_or_capable(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 return -EPERM;
2872
Eric Paris50c205f2012-04-04 15:01:43 -04002873 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002874 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875
David Howells275bb412008-11-14 10:39:19 +11002876 rc = avc_has_perm(sid, isec->sid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 FILE__RELABELFROM, &ad);
2878 if (rc)
2879 return rc;
2880
2881 rc = security_context_to_sid(value, size, &newsid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04002882 if (rc == -EINVAL) {
Eric Parisd6ea83e2012-04-04 13:45:49 -04002883 if (!capable(CAP_MAC_ADMIN)) {
2884 struct audit_buffer *ab;
2885 size_t audit_size;
2886 const char *str;
2887
2888 /* We strip a nul only if it is at the end, otherwise the
2889 * context contains a nul and we should audit that */
Al Viroe3fea3f2012-06-09 08:15:16 +01002890 if (value) {
2891 str = value;
2892 if (str[size - 1] == '\0')
2893 audit_size = size - 1;
2894 else
2895 audit_size = size;
2896 } else {
2897 str = "";
2898 audit_size = 0;
2899 }
Eric Parisd6ea83e2012-04-04 13:45:49 -04002900 ab = audit_log_start(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR);
2901 audit_log_format(ab, "op=setxattr invalid_context=");
2902 audit_log_n_untrustedstring(ab, value, audit_size);
2903 audit_log_end(ab);
2904
Stephen Smalley12b29f32008-05-07 13:03:20 -04002905 return rc;
Eric Parisd6ea83e2012-04-04 13:45:49 -04002906 }
Stephen Smalley12b29f32008-05-07 13:03:20 -04002907 rc = security_context_to_sid_force(value, size, &newsid);
2908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 if (rc)
2910 return rc;
2911
David Howells275bb412008-11-14 10:39:19 +11002912 rc = avc_has_perm(sid, newsid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 FILE__RELABELTO, &ad);
2914 if (rc)
2915 return rc;
2916
David Howells275bb412008-11-14 10:39:19 +11002917 rc = security_validate_transition(isec->sid, newsid, sid,
Eric Paris828dfe12008-04-17 13:17:49 -04002918 isec->sclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 if (rc)
2920 return rc;
2921
2922 return avc_has_perm(newsid,
2923 sbsec->sid,
2924 SECCLASS_FILESYSTEM,
2925 FILESYSTEM__ASSOCIATE,
2926 &ad);
2927}
2928
David Howells8f0cfa52008-04-29 00:59:41 -07002929static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
Eric Parisf5269712008-05-14 11:27:45 -04002930 const void *value, size_t size,
David Howells8f0cfa52008-04-29 00:59:41 -07002931 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932{
2933 struct inode *inode = dentry->d_inode;
2934 struct inode_security_struct *isec = inode->i_security;
2935 u32 newsid;
2936 int rc;
2937
2938 if (strcmp(name, XATTR_NAME_SELINUX)) {
2939 /* Not an attribute we recognize, so nothing to do. */
2940 return;
2941 }
2942
Stephen Smalley12b29f32008-05-07 13:03:20 -04002943 rc = security_context_to_sid_force(value, size, &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 if (rc) {
Stephen Smalley12b29f32008-05-07 13:03:20 -04002945 printk(KERN_ERR "SELinux: unable to map context to SID"
2946 "for (%s, %lu), rc=%d\n",
2947 inode->i_sb->s_id, inode->i_ino, -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 return;
2949 }
2950
David Quigleyaa9c2662013-05-22 12:50:44 -04002951 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 isec->sid = newsid;
David Quigleyaa9c2662013-05-22 12:50:44 -04002953 isec->initialized = 1;
2954
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 return;
2956}
2957
David Howells8f0cfa52008-04-29 00:59:41 -07002958static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959{
David Howells88e67f32008-11-14 10:39:21 +11002960 const struct cred *cred = current_cred();
2961
Eric Paris2875fa02011-04-28 16:04:24 -04002962 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963}
2964
Eric Paris828dfe12008-04-17 13:17:49 -04002965static int selinux_inode_listxattr(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966{
David Howells88e67f32008-11-14 10:39:21 +11002967 const struct cred *cred = current_cred();
2968
Eric Paris2875fa02011-04-28 16:04:24 -04002969 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970}
2971
David Howells8f0cfa52008-04-29 00:59:41 -07002972static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973{
Serge E. Hallynb5376772007-10-16 23:31:36 -07002974 if (strcmp(name, XATTR_NAME_SELINUX))
2975 return selinux_inode_setotherxattr(dentry, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976
2977 /* No one is allowed to remove a SELinux security label.
2978 You can change the label, but all data must be labeled. */
2979 return -EACCES;
2980}
2981
James Morrisd381d8a2005-10-30 14:59:22 -08002982/*
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04002983 * Copy the inode security context value to the user.
James Morrisd381d8a2005-10-30 14:59:22 -08002984 *
2985 * Permission check is handled by selinux_inode_getxattr hook.
2986 */
David P. Quigley42492592008-02-04 22:29:39 -08002987static int selinux_inode_getsecurity(const struct inode *inode, const char *name, void **buffer, bool alloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988{
David P. Quigley42492592008-02-04 22:29:39 -08002989 u32 size;
2990 int error;
2991 char *context = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 struct inode_security_struct *isec = inode->i_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00002994 if (strcmp(name, XATTR_SELINUX_SUFFIX))
2995 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04002997 /*
2998 * If the caller has CAP_MAC_ADMIN, then get the raw context
2999 * value even if it is not defined by current policy; otherwise,
3000 * use the in-core value under current policy.
3001 * Use the non-auditing forms of the permission checks since
3002 * getxattr may be called by unprivileged processes commonly
3003 * and lack of permission just means that we fall back to the
3004 * in-core context value, not a denial.
3005 */
Eric Paris6a9de492012-01-03 12:25:14 -05003006 error = selinux_capable(current_cred(), &init_user_ns, CAP_MAC_ADMIN,
David Howells3699c532009-01-06 22:27:01 +00003007 SECURITY_CAP_NOAUDIT);
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003008 if (!error)
3009 error = security_sid_to_context_force(isec->sid, &context,
3010 &size);
3011 else
3012 error = security_sid_to_context(isec->sid, &context, &size);
David P. Quigley42492592008-02-04 22:29:39 -08003013 if (error)
3014 return error;
3015 error = size;
3016 if (alloc) {
3017 *buffer = context;
3018 goto out_nofree;
3019 }
3020 kfree(context);
3021out_nofree:
3022 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023}
3024
3025static int selinux_inode_setsecurity(struct inode *inode, const char *name,
Eric Paris828dfe12008-04-17 13:17:49 -04003026 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027{
3028 struct inode_security_struct *isec = inode->i_security;
3029 u32 newsid;
3030 int rc;
3031
3032 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3033 return -EOPNOTSUPP;
3034
3035 if (!value || !size)
3036 return -EACCES;
3037
Eric Paris828dfe12008-04-17 13:17:49 -04003038 rc = security_context_to_sid((void *)value, size, &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 if (rc)
3040 return rc;
3041
David Quigleyaa9c2662013-05-22 12:50:44 -04003042 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 isec->sid = newsid;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04003044 isec->initialized = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 return 0;
3046}
3047
3048static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
3049{
3050 const int len = sizeof(XATTR_NAME_SELINUX);
3051 if (buffer && len <= buffer_size)
3052 memcpy(buffer, XATTR_NAME_SELINUX, len);
3053 return len;
3054}
3055
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02003056static void selinux_inode_getsecid(const struct inode *inode, u32 *secid)
3057{
3058 struct inode_security_struct *isec = inode->i_security;
3059 *secid = isec->sid;
3060}
3061
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062/* file security operations */
3063
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003064static int selinux_revalidate_file_permission(struct file *file, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065{
David Howells88e67f32008-11-14 10:39:21 +11003066 const struct cred *cred = current_cred();
Al Viro496ad9a2013-01-23 17:07:38 -05003067 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
3070 if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
3071 mask |= MAY_APPEND;
3072
Paul Moore389fb8002009-03-27 17:10:34 -04003073 return file_has_perm(cred, file,
3074 file_mask_to_av(inode->i_mode, mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075}
3076
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003077static int selinux_file_permission(struct file *file, int mask)
3078{
Al Viro496ad9a2013-01-23 17:07:38 -05003079 struct inode *inode = file_inode(file);
Stephen Smalley20dda182009-06-22 14:54:53 -04003080 struct file_security_struct *fsec = file->f_security;
3081 struct inode_security_struct *isec = inode->i_security;
3082 u32 sid = current_sid();
3083
Paul Moore389fb8002009-03-27 17:10:34 -04003084 if (!mask)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003085 /* No permission to check. Existence test. */
3086 return 0;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003087
Stephen Smalley20dda182009-06-22 14:54:53 -04003088 if (sid == fsec->sid && fsec->isid == isec->sid &&
3089 fsec->pseqno == avc_policy_seqno())
Eric Paris83d49852012-04-04 13:45:40 -04003090 /* No change since file_open check. */
Stephen Smalley20dda182009-06-22 14:54:53 -04003091 return 0;
3092
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003093 return selinux_revalidate_file_permission(file, mask);
3094}
3095
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096static int selinux_file_alloc_security(struct file *file)
3097{
3098 return file_alloc_security(file);
3099}
3100
3101static void selinux_file_free_security(struct file *file)
3102{
3103 file_free_security(file);
3104}
3105
3106static int selinux_file_ioctl(struct file *file, unsigned int cmd,
3107 unsigned long arg)
3108{
David Howells88e67f32008-11-14 10:39:21 +11003109 const struct cred *cred = current_cred();
Eric Paris0b24dcb2011-02-25 15:39:20 -05003110 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111
Eric Paris0b24dcb2011-02-25 15:39:20 -05003112 switch (cmd) {
3113 case FIONREAD:
3114 /* fall through */
3115 case FIBMAP:
3116 /* fall through */
3117 case FIGETBSZ:
3118 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003119 case FS_IOC_GETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003120 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003121 case FS_IOC_GETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003122 error = file_has_perm(cred, file, FILE__GETATTR);
3123 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124
Al Viro2f99c362012-03-23 16:04:05 -04003125 case FS_IOC_SETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003126 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003127 case FS_IOC_SETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003128 error = file_has_perm(cred, file, FILE__SETATTR);
3129 break;
3130
3131 /* sys_ioctl() checks */
3132 case FIONBIO:
3133 /* fall through */
3134 case FIOASYNC:
3135 error = file_has_perm(cred, file, 0);
3136 break;
3137
3138 case KDSKBENT:
3139 case KDSKBSENT:
Eric Paris6a9de492012-01-03 12:25:14 -05003140 error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
3141 SECURITY_CAP_AUDIT);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003142 break;
3143
3144 /* default case assumes that the command will go
3145 * to the file's ioctl() function.
3146 */
3147 default:
3148 error = file_has_perm(cred, file, FILE__IOCTL);
3149 }
3150 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151}
3152
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003153static int default_noexec;
3154
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
3156{
David Howells88e67f32008-11-14 10:39:21 +11003157 const struct cred *cred = current_cred();
David Howellsd84f4f92008-11-14 10:39:23 +11003158 int rc = 0;
David Howells88e67f32008-11-14 10:39:21 +11003159
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003160 if (default_noexec &&
3161 (prot & PROT_EXEC) && (!file || (!shared && (prot & PROT_WRITE)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 /*
3163 * We are making executable an anonymous mapping or a
3164 * private file mapping that will also be writable.
3165 * This has an additional check.
3166 */
David Howellsd84f4f92008-11-14 10:39:23 +11003167 rc = cred_has_perm(cred, cred, PROCESS__EXECMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 if (rc)
David Howellsd84f4f92008-11-14 10:39:23 +11003169 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171
3172 if (file) {
3173 /* read access is always possible with a mapping */
3174 u32 av = FILE__READ;
3175
3176 /* write access only matters if the mapping is shared */
3177 if (shared && (prot & PROT_WRITE))
3178 av |= FILE__WRITE;
3179
3180 if (prot & PROT_EXEC)
3181 av |= FILE__EXECUTE;
3182
David Howells88e67f32008-11-14 10:39:21 +11003183 return file_has_perm(cred, file, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 }
David Howellsd84f4f92008-11-14 10:39:23 +11003185
3186error:
3187 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188}
3189
Al Viroe5467852012-05-30 13:30:51 -04003190static int selinux_mmap_addr(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191{
Eric Parised032182007-06-28 15:55:21 -04003192 int rc = 0;
David Howells275bb412008-11-14 10:39:19 +11003193 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194
Eric Paris84336d1a2009-07-31 12:54:05 -04003195 /*
3196 * notice that we are intentionally putting the SELinux check before
3197 * the secondary cap_file_mmap check. This is such a likely attempt
3198 * at bad behaviour/exploit that we always want to get the AVC, even
3199 * if DAC would have also denied the operation.
3200 */
Eric Parisa2551df2009-07-31 12:54:11 -04003201 if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
Eric Parised032182007-06-28 15:55:21 -04003202 rc = avc_has_perm(sid, sid, SECCLASS_MEMPROTECT,
3203 MEMPROTECT__MMAP_ZERO, NULL);
Eric Paris84336d1a2009-07-31 12:54:05 -04003204 if (rc)
3205 return rc;
3206 }
3207
3208 /* do DAC check on address space usage */
Al Viroe5467852012-05-30 13:30:51 -04003209 return cap_mmap_addr(addr);
3210}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211
Al Viroe5467852012-05-30 13:30:51 -04003212static int selinux_mmap_file(struct file *file, unsigned long reqprot,
3213 unsigned long prot, unsigned long flags)
3214{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 if (selinux_checkreqprot)
3216 prot = reqprot;
3217
3218 return file_map_prot_check(file, prot,
3219 (flags & MAP_TYPE) == MAP_SHARED);
3220}
3221
3222static int selinux_file_mprotect(struct vm_area_struct *vma,
3223 unsigned long reqprot,
3224 unsigned long prot)
3225{
David Howells88e67f32008-11-14 10:39:21 +11003226 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227
3228 if (selinux_checkreqprot)
3229 prot = reqprot;
3230
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003231 if (default_noexec &&
3232 (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
James Morrisd541bbe2009-01-29 12:19:51 +11003233 int rc = 0;
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003234 if (vma->vm_start >= vma->vm_mm->start_brk &&
3235 vma->vm_end <= vma->vm_mm->brk) {
David Howellsd84f4f92008-11-14 10:39:23 +11003236 rc = cred_has_perm(cred, cred, PROCESS__EXECHEAP);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003237 } else if (!vma->vm_file &&
3238 vma->vm_start <= vma->vm_mm->start_stack &&
3239 vma->vm_end >= vma->vm_mm->start_stack) {
David Howells3b11a1d2008-11-14 10:39:26 +11003240 rc = current_has_perm(current, PROCESS__EXECSTACK);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003241 } else if (vma->vm_file && vma->anon_vma) {
3242 /*
3243 * We are making executable a file mapping that has
3244 * had some COW done. Since pages might have been
3245 * written, check ability to execute the possibly
3246 * modified content. This typically should only
3247 * occur for text relocations.
3248 */
David Howellsd84f4f92008-11-14 10:39:23 +11003249 rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003250 }
Lorenzo Hernandez García-Hierro6b992192005-06-25 14:54:34 -07003251 if (rc)
3252 return rc;
3253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254
3255 return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
3256}
3257
3258static int selinux_file_lock(struct file *file, unsigned int cmd)
3259{
David Howells88e67f32008-11-14 10:39:21 +11003260 const struct cred *cred = current_cred();
3261
3262 return file_has_perm(cred, file, FILE__LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263}
3264
3265static int selinux_file_fcntl(struct file *file, unsigned int cmd,
3266 unsigned long arg)
3267{
David Howells88e67f32008-11-14 10:39:21 +11003268 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 int err = 0;
3270
3271 switch (cmd) {
Eric Paris828dfe12008-04-17 13:17:49 -04003272 case F_SETFL:
Eric Paris828dfe12008-04-17 13:17:49 -04003273 if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
David Howells88e67f32008-11-14 10:39:21 +11003274 err = file_has_perm(cred, file, FILE__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003276 }
3277 /* fall through */
3278 case F_SETOWN:
3279 case F_SETSIG:
3280 case F_GETFL:
3281 case F_GETOWN:
3282 case F_GETSIG:
Cyrill Gorcunov1d151c32012-07-30 14:43:00 -07003283 case F_GETOWNER_UIDS:
Eric Paris828dfe12008-04-17 13:17:49 -04003284 /* Just check FD__USE permission */
David Howells88e67f32008-11-14 10:39:21 +11003285 err = file_has_perm(cred, file, 0);
Eric Paris828dfe12008-04-17 13:17:49 -04003286 break;
3287 case F_GETLK:
3288 case F_SETLK:
3289 case F_SETLKW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290#if BITS_PER_LONG == 32
Eric Paris828dfe12008-04-17 13:17:49 -04003291 case F_GETLK64:
3292 case F_SETLK64:
3293 case F_SETLKW64:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294#endif
David Howells88e67f32008-11-14 10:39:21 +11003295 err = file_has_perm(cred, file, FILE__LOCK);
Eric Paris828dfe12008-04-17 13:17:49 -04003296 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 }
3298
3299 return err;
3300}
3301
3302static int selinux_file_set_fowner(struct file *file)
3303{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 struct file_security_struct *fsec;
3305
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 fsec = file->f_security;
David Howells275bb412008-11-14 10:39:19 +11003307 fsec->fown_sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308
3309 return 0;
3310}
3311
3312static int selinux_file_send_sigiotask(struct task_struct *tsk,
3313 struct fown_struct *fown, int signum)
3314{
Eric Paris828dfe12008-04-17 13:17:49 -04003315 struct file *file;
Stephen Smalley65c90bc2009-05-04 15:43:18 -04003316 u32 sid = task_sid(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 struct file_security_struct *fsec;
3319
3320 /* struct fown_struct is never outside the context of a struct file */
Eric Paris828dfe12008-04-17 13:17:49 -04003321 file = container_of(fown, struct file, f_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 fsec = file->f_security;
3324
3325 if (!signum)
3326 perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
3327 else
3328 perm = signal_to_av(signum);
3329
David Howells275bb412008-11-14 10:39:19 +11003330 return avc_has_perm(fsec->fown_sid, sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 SECCLASS_PROCESS, perm, NULL);
3332}
3333
3334static int selinux_file_receive(struct file *file)
3335{
David Howells88e67f32008-11-14 10:39:21 +11003336 const struct cred *cred = current_cred();
3337
3338 return file_has_perm(cred, file, file_to_av(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339}
3340
Eric Paris83d49852012-04-04 13:45:40 -04003341static int selinux_file_open(struct file *file, const struct cred *cred)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003342{
3343 struct file_security_struct *fsec;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003344 struct inode_security_struct *isec;
David Howellsd84f4f92008-11-14 10:39:23 +11003345
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003346 fsec = file->f_security;
Al Viro496ad9a2013-01-23 17:07:38 -05003347 isec = file_inode(file)->i_security;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003348 /*
3349 * Save inode label and policy sequence number
3350 * at open-time so that selinux_file_permission
3351 * can determine whether revalidation is necessary.
3352 * Task label is already saved in the file security
3353 * struct as its SID.
3354 */
3355 fsec->isid = isec->sid;
3356 fsec->pseqno = avc_policy_seqno();
3357 /*
3358 * Since the inode label or policy seqno may have changed
3359 * between the selinux_inode_permission check and the saving
3360 * of state above, recheck that access is still permitted.
3361 * Otherwise, access might never be revalidated against the
3362 * new inode label or new policy.
3363 * This check is not redundant - do not remove.
3364 */
David Howells13f8e982013-06-13 23:37:55 +01003365 return file_path_has_perm(cred, file, open_file_to_av(file));
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003366}
3367
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368/* task security operations */
3369
3370static int selinux_task_create(unsigned long clone_flags)
3371{
David Howells3b11a1d2008-11-14 10:39:26 +11003372 return current_has_perm(current, PROCESS__FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373}
3374
David Howellsf1752ee2008-11-14 10:39:17 +11003375/*
David Howellsee18d642009-09-02 09:14:21 +01003376 * allocate the SELinux part of blank credentials
3377 */
3378static int selinux_cred_alloc_blank(struct cred *cred, gfp_t gfp)
3379{
3380 struct task_security_struct *tsec;
3381
3382 tsec = kzalloc(sizeof(struct task_security_struct), gfp);
3383 if (!tsec)
3384 return -ENOMEM;
3385
3386 cred->security = tsec;
3387 return 0;
3388}
3389
3390/*
David Howellsf1752ee2008-11-14 10:39:17 +11003391 * detach and free the LSM part of a set of credentials
3392 */
3393static void selinux_cred_free(struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394{
David Howellsf1752ee2008-11-14 10:39:17 +11003395 struct task_security_struct *tsec = cred->security;
David Howellse0e81732009-09-02 09:13:40 +01003396
Tetsuo Handa2edeaa32011-02-07 13:36:10 +00003397 /*
3398 * cred->security == NULL if security_cred_alloc_blank() or
3399 * security_prepare_creds() returned an error.
3400 */
3401 BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE);
David Howellse0e81732009-09-02 09:13:40 +01003402 cred->security = (void *) 0x7UL;
David Howellsf1752ee2008-11-14 10:39:17 +11003403 kfree(tsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404}
3405
David Howellsd84f4f92008-11-14 10:39:23 +11003406/*
3407 * prepare a new set of credentials for modification
3408 */
3409static int selinux_cred_prepare(struct cred *new, const struct cred *old,
3410 gfp_t gfp)
3411{
3412 const struct task_security_struct *old_tsec;
3413 struct task_security_struct *tsec;
3414
3415 old_tsec = old->security;
3416
3417 tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp);
3418 if (!tsec)
3419 return -ENOMEM;
3420
3421 new->security = tsec;
3422 return 0;
3423}
3424
3425/*
David Howellsee18d642009-09-02 09:14:21 +01003426 * transfer the SELinux data to a blank set of creds
3427 */
3428static void selinux_cred_transfer(struct cred *new, const struct cred *old)
3429{
3430 const struct task_security_struct *old_tsec = old->security;
3431 struct task_security_struct *tsec = new->security;
3432
3433 *tsec = *old_tsec;
3434}
3435
3436/*
David Howells3a3b7ce2008-11-14 10:39:28 +11003437 * set the security data for a kernel service
3438 * - all the creation contexts are set to unlabelled
3439 */
3440static int selinux_kernel_act_as(struct cred *new, u32 secid)
3441{
3442 struct task_security_struct *tsec = new->security;
3443 u32 sid = current_sid();
3444 int ret;
3445
3446 ret = avc_has_perm(sid, secid,
3447 SECCLASS_KERNEL_SERVICE,
3448 KERNEL_SERVICE__USE_AS_OVERRIDE,
3449 NULL);
3450 if (ret == 0) {
3451 tsec->sid = secid;
3452 tsec->create_sid = 0;
3453 tsec->keycreate_sid = 0;
3454 tsec->sockcreate_sid = 0;
3455 }
3456 return ret;
3457}
3458
3459/*
3460 * set the file creation context in a security record to the same as the
3461 * objective context of the specified inode
3462 */
3463static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
3464{
3465 struct inode_security_struct *isec = inode->i_security;
3466 struct task_security_struct *tsec = new->security;
3467 u32 sid = current_sid();
3468 int ret;
3469
3470 ret = avc_has_perm(sid, isec->sid,
3471 SECCLASS_KERNEL_SERVICE,
3472 KERNEL_SERVICE__CREATE_FILES_AS,
3473 NULL);
3474
3475 if (ret == 0)
3476 tsec->create_sid = isec->sid;
David Howellsef574712010-02-26 01:56:16 +00003477 return ret;
David Howells3a3b7ce2008-11-14 10:39:28 +11003478}
3479
Eric Parisdd8dbf22009-11-03 16:35:32 +11003480static int selinux_kernel_module_request(char *kmod_name)
Eric Paris25354c42009-08-13 09:45:03 -04003481{
Eric Parisdd8dbf22009-11-03 16:35:32 +11003482 u32 sid;
3483 struct common_audit_data ad;
3484
3485 sid = task_sid(current);
3486
Eric Paris50c205f2012-04-04 15:01:43 -04003487 ad.type = LSM_AUDIT_DATA_KMOD;
Eric Parisdd8dbf22009-11-03 16:35:32 +11003488 ad.u.kmod_name = kmod_name;
3489
3490 return avc_has_perm(sid, SECINITSID_KERNEL, SECCLASS_SYSTEM,
3491 SYSTEM__MODULE_REQUEST, &ad);
Eric Paris25354c42009-08-13 09:45:03 -04003492}
3493
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
3495{
David Howells3b11a1d2008-11-14 10:39:26 +11003496 return current_has_perm(p, PROCESS__SETPGID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497}
3498
3499static int selinux_task_getpgid(struct task_struct *p)
3500{
David Howells3b11a1d2008-11-14 10:39:26 +11003501 return current_has_perm(p, PROCESS__GETPGID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502}
3503
3504static int selinux_task_getsid(struct task_struct *p)
3505{
David Howells3b11a1d2008-11-14 10:39:26 +11003506 return current_has_perm(p, PROCESS__GETSESSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507}
3508
David Quigleyf9008e42006-06-30 01:55:46 -07003509static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
3510{
David Howells275bb412008-11-14 10:39:19 +11003511 *secid = task_sid(p);
David Quigleyf9008e42006-06-30 01:55:46 -07003512}
3513
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514static int selinux_task_setnice(struct task_struct *p, int nice)
3515{
3516 int rc;
3517
Eric Paris200ac532009-02-12 15:01:04 -05003518 rc = cap_task_setnice(p, nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 if (rc)
3520 return rc;
3521
David Howells3b11a1d2008-11-14 10:39:26 +11003522 return current_has_perm(p, PROCESS__SETSCHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523}
3524
James Morris03e68062006-06-23 02:03:58 -07003525static int selinux_task_setioprio(struct task_struct *p, int ioprio)
3526{
Serge E. Hallynb5376772007-10-16 23:31:36 -07003527 int rc;
3528
Eric Paris200ac532009-02-12 15:01:04 -05003529 rc = cap_task_setioprio(p, ioprio);
Serge E. Hallynb5376772007-10-16 23:31:36 -07003530 if (rc)
3531 return rc;
3532
David Howells3b11a1d2008-11-14 10:39:26 +11003533 return current_has_perm(p, PROCESS__SETSCHED);
James Morris03e68062006-06-23 02:03:58 -07003534}
3535
David Quigleya1836a42006-06-30 01:55:49 -07003536static int selinux_task_getioprio(struct task_struct *p)
3537{
David Howells3b11a1d2008-11-14 10:39:26 +11003538 return current_has_perm(p, PROCESS__GETSCHED);
David Quigleya1836a42006-06-30 01:55:49 -07003539}
3540
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003541static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
3542 struct rlimit *new_rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543{
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003544 struct rlimit *old_rlim = p->signal->rlim + resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545
3546 /* Control the ability to change the hard limit (whether
3547 lowering or raising it), so that the hard limit can
3548 later be used as a safe reset point for the soft limit
David Howellsd84f4f92008-11-14 10:39:23 +11003549 upon context transitions. See selinux_bprm_committing_creds. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 if (old_rlim->rlim_max != new_rlim->rlim_max)
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003551 return current_has_perm(p, PROCESS__SETRLIMIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552
3553 return 0;
3554}
3555
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09003556static int selinux_task_setscheduler(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557{
Serge E. Hallynb5376772007-10-16 23:31:36 -07003558 int rc;
3559
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09003560 rc = cap_task_setscheduler(p);
Serge E. Hallynb5376772007-10-16 23:31:36 -07003561 if (rc)
3562 return rc;
3563
David Howells3b11a1d2008-11-14 10:39:26 +11003564 return current_has_perm(p, PROCESS__SETSCHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565}
3566
3567static int selinux_task_getscheduler(struct task_struct *p)
3568{
David Howells3b11a1d2008-11-14 10:39:26 +11003569 return current_has_perm(p, PROCESS__GETSCHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570}
3571
David Quigley35601542006-06-23 02:04:01 -07003572static int selinux_task_movememory(struct task_struct *p)
3573{
David Howells3b11a1d2008-11-14 10:39:26 +11003574 return current_has_perm(p, PROCESS__SETSCHED);
David Quigley35601542006-06-23 02:04:01 -07003575}
3576
David Quigleyf9008e42006-06-30 01:55:46 -07003577static int selinux_task_kill(struct task_struct *p, struct siginfo *info,
3578 int sig, u32 secid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579{
3580 u32 perm;
3581 int rc;
3582
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 if (!sig)
3584 perm = PROCESS__SIGNULL; /* null signal; existence test */
3585 else
3586 perm = signal_to_av(sig);
David Quigleyf9008e42006-06-30 01:55:46 -07003587 if (secid)
David Howells275bb412008-11-14 10:39:19 +11003588 rc = avc_has_perm(secid, task_sid(p),
3589 SECCLASS_PROCESS, perm, NULL);
David Quigleyf9008e42006-06-30 01:55:46 -07003590 else
David Howells3b11a1d2008-11-14 10:39:26 +11003591 rc = current_has_perm(p, perm);
David Quigleyf9008e42006-06-30 01:55:46 -07003592 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593}
3594
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595static int selinux_task_wait(struct task_struct *p)
3596{
Eric Paris8a535142007-10-22 16:10:31 -04003597 return task_has_perm(p, current, PROCESS__SIGCHLD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598}
3599
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600static void selinux_task_to_inode(struct task_struct *p,
3601 struct inode *inode)
3602{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 struct inode_security_struct *isec = inode->i_security;
David Howells275bb412008-11-14 10:39:19 +11003604 u32 sid = task_sid(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605
David Howells275bb412008-11-14 10:39:19 +11003606 isec->sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 isec->initialized = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608}
3609
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003611static int selinux_parse_skb_ipv4(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04003612 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613{
3614 int offset, ihlen, ret = -EINVAL;
3615 struct iphdr _iph, *ih;
3616
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03003617 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
3619 if (ih == NULL)
3620 goto out;
3621
3622 ihlen = ih->ihl * 4;
3623 if (ihlen < sizeof(_iph))
3624 goto out;
3625
Eric Paris48c62af2012-04-02 13:15:44 -04003626 ad->u.net->v4info.saddr = ih->saddr;
3627 ad->u.net->v4info.daddr = ih->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 ret = 0;
3629
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003630 if (proto)
3631 *proto = ih->protocol;
3632
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 switch (ih->protocol) {
Eric Paris828dfe12008-04-17 13:17:49 -04003634 case IPPROTO_TCP: {
3635 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636
Eric Paris828dfe12008-04-17 13:17:49 -04003637 if (ntohs(ih->frag_off) & IP_OFFSET)
3638 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639
3640 offset += ihlen;
3641 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3642 if (th == NULL)
3643 break;
3644
Eric Paris48c62af2012-04-02 13:15:44 -04003645 ad->u.net->sport = th->source;
3646 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003648 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649
Eric Paris828dfe12008-04-17 13:17:49 -04003650 case IPPROTO_UDP: {
3651 struct udphdr _udph, *uh;
3652
3653 if (ntohs(ih->frag_off) & IP_OFFSET)
3654 break;
3655
3656 offset += ihlen;
3657 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3658 if (uh == NULL)
3659 break;
3660
Eric Paris48c62af2012-04-02 13:15:44 -04003661 ad->u.net->sport = uh->source;
3662 ad->u.net->dport = uh->dest;
Eric Paris828dfe12008-04-17 13:17:49 -04003663 break;
3664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665
James Morris2ee92d42006-11-13 16:09:01 -08003666 case IPPROTO_DCCP: {
3667 struct dccp_hdr _dccph, *dh;
3668
3669 if (ntohs(ih->frag_off) & IP_OFFSET)
3670 break;
3671
3672 offset += ihlen;
3673 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3674 if (dh == NULL)
3675 break;
3676
Eric Paris48c62af2012-04-02 13:15:44 -04003677 ad->u.net->sport = dh->dccph_sport;
3678 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08003679 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003680 }
James Morris2ee92d42006-11-13 16:09:01 -08003681
Eric Paris828dfe12008-04-17 13:17:49 -04003682 default:
3683 break;
3684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685out:
3686 return ret;
3687}
3688
3689#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3690
3691/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003692static int selinux_parse_skb_ipv6(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04003693 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694{
3695 u8 nexthdr;
3696 int ret = -EINVAL, offset;
3697 struct ipv6hdr _ipv6h, *ip6;
Jesse Gross75f28112011-11-30 17:05:51 -08003698 __be16 frag_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03003700 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3702 if (ip6 == NULL)
3703 goto out;
3704
Eric Paris48c62af2012-04-02 13:15:44 -04003705 ad->u.net->v6info.saddr = ip6->saddr;
3706 ad->u.net->v6info.daddr = ip6->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 ret = 0;
3708
3709 nexthdr = ip6->nexthdr;
3710 offset += sizeof(_ipv6h);
Jesse Gross75f28112011-11-30 17:05:51 -08003711 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 if (offset < 0)
3713 goto out;
3714
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003715 if (proto)
3716 *proto = nexthdr;
3717
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 switch (nexthdr) {
3719 case IPPROTO_TCP: {
Eric Paris828dfe12008-04-17 13:17:49 -04003720 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721
3722 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3723 if (th == NULL)
3724 break;
3725
Eric Paris48c62af2012-04-02 13:15:44 -04003726 ad->u.net->sport = th->source;
3727 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 break;
3729 }
3730
3731 case IPPROTO_UDP: {
3732 struct udphdr _udph, *uh;
3733
3734 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3735 if (uh == NULL)
3736 break;
3737
Eric Paris48c62af2012-04-02 13:15:44 -04003738 ad->u.net->sport = uh->source;
3739 ad->u.net->dport = uh->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 break;
3741 }
3742
James Morris2ee92d42006-11-13 16:09:01 -08003743 case IPPROTO_DCCP: {
3744 struct dccp_hdr _dccph, *dh;
3745
3746 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3747 if (dh == NULL)
3748 break;
3749
Eric Paris48c62af2012-04-02 13:15:44 -04003750 ad->u.net->sport = dh->dccph_sport;
3751 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08003752 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003753 }
James Morris2ee92d42006-11-13 16:09:01 -08003754
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 /* includes fragments */
3756 default:
3757 break;
3758 }
3759out:
3760 return ret;
3761}
3762
3763#endif /* IPV6 */
3764
Thomas Liu2bf49692009-07-14 12:14:09 -04003765static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
David Howellscf9481e2008-07-27 21:31:07 +10003766 char **_addrp, int src, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767{
David Howellscf9481e2008-07-27 21:31:07 +10003768 char *addrp;
3769 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770
Eric Paris48c62af2012-04-02 13:15:44 -04003771 switch (ad->u.net->family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 case PF_INET:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003773 ret = selinux_parse_skb_ipv4(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10003774 if (ret)
3775 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04003776 addrp = (char *)(src ? &ad->u.net->v4info.saddr :
3777 &ad->u.net->v4info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10003778 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779
3780#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3781 case PF_INET6:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003782 ret = selinux_parse_skb_ipv6(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10003783 if (ret)
3784 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04003785 addrp = (char *)(src ? &ad->u.net->v6info.saddr :
3786 &ad->u.net->v6info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10003787 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788#endif /* IPV6 */
3789 default:
David Howellscf9481e2008-07-27 21:31:07 +10003790 addrp = NULL;
3791 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 }
3793
David Howellscf9481e2008-07-27 21:31:07 +10003794parse_error:
3795 printk(KERN_WARNING
3796 "SELinux: failure in selinux_parse_skb(),"
3797 " unable to parse packet\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 return ret;
David Howellscf9481e2008-07-27 21:31:07 +10003799
3800okay:
3801 if (_addrp)
3802 *_addrp = addrp;
3803 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804}
3805
Paul Moore4f6a9932007-03-01 14:35:22 -05003806/**
Paul Moore220deb92008-01-29 08:38:23 -05003807 * selinux_skb_peerlbl_sid - Determine the peer label of a packet
Paul Moore4f6a9932007-03-01 14:35:22 -05003808 * @skb: the packet
Paul Moore75e22912008-01-29 08:38:04 -05003809 * @family: protocol family
Paul Moore220deb92008-01-29 08:38:23 -05003810 * @sid: the packet's peer label SID
Paul Moore4f6a9932007-03-01 14:35:22 -05003811 *
3812 * Description:
Paul Moore220deb92008-01-29 08:38:23 -05003813 * Check the various different forms of network peer labeling and determine
3814 * the peer label/SID for the packet; most of the magic actually occurs in
3815 * the security server function security_net_peersid_cmp(). The function
3816 * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
3817 * or -EACCES if @sid is invalid due to inconsistencies with the different
3818 * peer labels.
Paul Moore4f6a9932007-03-01 14:35:22 -05003819 *
3820 */
Paul Moore220deb92008-01-29 08:38:23 -05003821static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
Paul Moore4f6a9932007-03-01 14:35:22 -05003822{
Paul Moore71f1cb02008-01-29 08:51:16 -05003823 int err;
Paul Moore4f6a9932007-03-01 14:35:22 -05003824 u32 xfrm_sid;
3825 u32 nlbl_sid;
Paul Moore220deb92008-01-29 08:38:23 -05003826 u32 nlbl_type;
Paul Moore4f6a9932007-03-01 14:35:22 -05003827
Paul Moore817eff72013-12-10 14:57:54 -05003828 err = selinux_xfrm_skb_sid(skb, &xfrm_sid);
Paul Moorebed4d7e2013-07-23 17:38:40 -04003829 if (unlikely(err))
3830 return -EACCES;
3831 err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
3832 if (unlikely(err))
3833 return -EACCES;
Paul Moore220deb92008-01-29 08:38:23 -05003834
Paul Moore71f1cb02008-01-29 08:51:16 -05003835 err = security_net_peersid_resolve(nlbl_sid, nlbl_type, xfrm_sid, sid);
3836 if (unlikely(err)) {
3837 printk(KERN_WARNING
3838 "SELinux: failure in selinux_skb_peerlbl_sid(),"
3839 " unable to determine packet's peer label\n");
Paul Moore220deb92008-01-29 08:38:23 -05003840 return -EACCES;
Paul Moore71f1cb02008-01-29 08:51:16 -05003841 }
Paul Moore220deb92008-01-29 08:38:23 -05003842
3843 return 0;
Paul Moore4f6a9932007-03-01 14:35:22 -05003844}
3845
Paul Moore446b8022013-12-04 16:10:51 -05003846/**
3847 * selinux_conn_sid - Determine the child socket label for a connection
3848 * @sk_sid: the parent socket's SID
3849 * @skb_sid: the packet's SID
3850 * @conn_sid: the resulting connection SID
3851 *
3852 * If @skb_sid is valid then the user:role:type information from @sk_sid is
3853 * combined with the MLS information from @skb_sid in order to create
3854 * @conn_sid. If @skb_sid is not valid then then @conn_sid is simply a copy
3855 * of @sk_sid. Returns zero on success, negative values on failure.
3856 *
3857 */
3858static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid)
3859{
3860 int err = 0;
3861
3862 if (skb_sid != SECSID_NULL)
3863 err = security_sid_mls_copy(sk_sid, skb_sid, conn_sid);
3864 else
3865 *conn_sid = sk_sid;
3866
3867 return err;
3868}
3869
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870/* socket security operations */
Paul Moored4f2d972010-04-22 14:46:18 -04003871
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003872static int socket_sockcreate_sid(const struct task_security_struct *tsec,
3873 u16 secclass, u32 *socksid)
Paul Moored4f2d972010-04-22 14:46:18 -04003874{
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003875 if (tsec->sockcreate_sid > SECSID_NULL) {
3876 *socksid = tsec->sockcreate_sid;
3877 return 0;
3878 }
3879
3880 return security_transition_sid(tsec->sid, tsec->sid, secclass, NULL,
3881 socksid);
Paul Moored4f2d972010-04-22 14:46:18 -04003882}
3883
Paul Moore253bfae2010-04-22 14:46:19 -04003884static int sock_has_perm(struct task_struct *task, struct sock *sk, u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885{
Paul Moore253bfae2010-04-22 14:46:19 -04003886 struct sk_security_struct *sksec = sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04003887 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04003888 struct lsm_network_audit net = {0,};
Paul Moore253bfae2010-04-22 14:46:19 -04003889 u32 tsid = task_sid(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890
Paul Moore253bfae2010-04-22 14:46:19 -04003891 if (sksec->sid == SECINITSID_KERNEL)
3892 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893
Eric Paris50c205f2012-04-04 15:01:43 -04003894 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04003895 ad.u.net = &net;
3896 ad.u.net->sk = sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897
Paul Moore253bfae2010-04-22 14:46:19 -04003898 return avc_has_perm(tsid, sksec->sid, sksec->sclass, perms, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899}
3900
3901static int selinux_socket_create(int family, int type,
3902 int protocol, int kern)
3903{
Paul Moore5fb49872010-04-22 14:46:19 -04003904 const struct task_security_struct *tsec = current_security();
Paul Moored4f2d972010-04-22 14:46:18 -04003905 u32 newsid;
David Howells275bb412008-11-14 10:39:19 +11003906 u16 secclass;
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003907 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908
3909 if (kern)
Paul Moored4f2d972010-04-22 14:46:18 -04003910 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911
David Howells275bb412008-11-14 10:39:19 +11003912 secclass = socket_type_to_security_class(family, type, protocol);
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003913 rc = socket_sockcreate_sid(tsec, secclass, &newsid);
3914 if (rc)
3915 return rc;
3916
Paul Moored4f2d972010-04-22 14:46:18 -04003917 return avc_has_perm(tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918}
3919
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003920static int selinux_socket_post_create(struct socket *sock, int family,
3921 int type, int protocol, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922{
Paul Moore5fb49872010-04-22 14:46:19 -04003923 const struct task_security_struct *tsec = current_security();
Paul Moored4f2d972010-04-22 14:46:18 -04003924 struct inode_security_struct *isec = SOCK_INODE(sock)->i_security;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07003925 struct sk_security_struct *sksec;
David Howells275bb412008-11-14 10:39:19 +11003926 int err = 0;
3927
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003928 isec->sclass = socket_type_to_security_class(family, type, protocol);
3929
David Howells275bb412008-11-14 10:39:19 +11003930 if (kern)
3931 isec->sid = SECINITSID_KERNEL;
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003932 else {
3933 err = socket_sockcreate_sid(tsec, isec->sclass, &(isec->sid));
3934 if (err)
3935 return err;
3936 }
David Howells275bb412008-11-14 10:39:19 +11003937
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 isec->initialized = 1;
3939
Venkat Yekkirala892c1412006-08-04 23:08:56 -07003940 if (sock->sk) {
3941 sksec = sock->sk->sk_security;
3942 sksec->sid = isec->sid;
Paul Moore220deb92008-01-29 08:38:23 -05003943 sksec->sclass = isec->sclass;
Paul Moore389fb8002009-03-27 17:10:34 -04003944 err = selinux_netlbl_socket_post_create(sock->sk, family);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07003945 }
3946
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003947 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948}
3949
3950/* Range of port numbers used to automatically bind.
3951 Need to determine whether we should perform a name_bind
3952 permission check between the socket and the port number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953
3954static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
3955{
Paul Moore253bfae2010-04-22 14:46:19 -04003956 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 u16 family;
3958 int err;
3959
Paul Moore253bfae2010-04-22 14:46:19 -04003960 err = sock_has_perm(current, sk, SOCKET__BIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 if (err)
3962 goto out;
3963
3964 /*
3965 * If PF_INET or PF_INET6, check name_bind permission for the port.
James Morris13402582005-09-30 14:24:34 -04003966 * Multiple address binding for SCTP is not supported yet: we just
3967 * check the first address now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 */
Paul Moore253bfae2010-04-22 14:46:19 -04003969 family = sk->sk_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 if (family == PF_INET || family == PF_INET6) {
3971 char *addrp;
Paul Moore253bfae2010-04-22 14:46:19 -04003972 struct sk_security_struct *sksec = sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04003973 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04003974 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 struct sockaddr_in *addr4 = NULL;
3976 struct sockaddr_in6 *addr6 = NULL;
3977 unsigned short snum;
James Morrise399f982008-06-12 01:39:58 +10003978 u32 sid, node_perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 if (family == PF_INET) {
3981 addr4 = (struct sockaddr_in *)address;
3982 snum = ntohs(addr4->sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 addrp = (char *)&addr4->sin_addr.s_addr;
3984 } else {
3985 addr6 = (struct sockaddr_in6 *)address;
3986 snum = ntohs(addr6->sin6_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 addrp = (char *)&addr6->sin6_addr.s6_addr;
3988 }
3989
Stephen Hemminger227b60f2007-10-10 17:30:46 -07003990 if (snum) {
3991 int low, high;
3992
Eric W. Biederman0bbf87d2013-09-28 14:10:59 -07003993 inet_get_local_port_range(sock_net(sk), &low, &high);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07003994
3995 if (snum < max(PROT_SOCK, low) || snum > high) {
Paul Moore3e112172008-04-10 10:48:14 -04003996 err = sel_netport_sid(sk->sk_protocol,
3997 snum, &sid);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07003998 if (err)
3999 goto out;
Eric Paris50c205f2012-04-04 15:01:43 -04004000 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004001 ad.u.net = &net;
4002 ad.u.net->sport = htons(snum);
4003 ad.u.net->family = family;
Paul Moore253bfae2010-04-22 14:46:19 -04004004 err = avc_has_perm(sksec->sid, sid,
4005 sksec->sclass,
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004006 SOCKET__NAME_BIND, &ad);
4007 if (err)
4008 goto out;
4009 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 }
Eric Paris828dfe12008-04-17 13:17:49 -04004011
Paul Moore253bfae2010-04-22 14:46:19 -04004012 switch (sksec->sclass) {
James Morris13402582005-09-30 14:24:34 -04004013 case SECCLASS_TCP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 node_perm = TCP_SOCKET__NODE_BIND;
4015 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004016
James Morris13402582005-09-30 14:24:34 -04004017 case SECCLASS_UDP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 node_perm = UDP_SOCKET__NODE_BIND;
4019 break;
James Morris2ee92d42006-11-13 16:09:01 -08004020
4021 case SECCLASS_DCCP_SOCKET:
4022 node_perm = DCCP_SOCKET__NODE_BIND;
4023 break;
4024
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 default:
4026 node_perm = RAWIP_SOCKET__NODE_BIND;
4027 break;
4028 }
Eric Paris828dfe12008-04-17 13:17:49 -04004029
Paul Moore224dfbd2008-01-29 08:38:13 -05004030 err = sel_netnode_sid(addrp, family, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 if (err)
4032 goto out;
Eric Paris828dfe12008-04-17 13:17:49 -04004033
Eric Paris50c205f2012-04-04 15:01:43 -04004034 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004035 ad.u.net = &net;
4036 ad.u.net->sport = htons(snum);
4037 ad.u.net->family = family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038
4039 if (family == PF_INET)
Eric Paris48c62af2012-04-02 13:15:44 -04004040 ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041 else
Eric Paris48c62af2012-04-02 13:15:44 -04004042 ad.u.net->v6info.saddr = addr6->sin6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043
Paul Moore253bfae2010-04-22 14:46:19 -04004044 err = avc_has_perm(sksec->sid, sid,
4045 sksec->sclass, node_perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 if (err)
4047 goto out;
4048 }
4049out:
4050 return err;
4051}
4052
4053static int selinux_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen)
4054{
Paul Moore014ab192008-10-10 10:16:33 -04004055 struct sock *sk = sock->sk;
Paul Moore253bfae2010-04-22 14:46:19 -04004056 struct sk_security_struct *sksec = sk->sk_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 int err;
4058
Paul Moore253bfae2010-04-22 14:46:19 -04004059 err = sock_has_perm(current, sk, SOCKET__CONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 if (err)
4061 return err;
4062
4063 /*
James Morris2ee92d42006-11-13 16:09:01 -08004064 * If a TCP or DCCP socket, check name_connect permission for the port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 */
Paul Moore253bfae2010-04-22 14:46:19 -04004066 if (sksec->sclass == SECCLASS_TCP_SOCKET ||
4067 sksec->sclass == SECCLASS_DCCP_SOCKET) {
Thomas Liu2bf49692009-07-14 12:14:09 -04004068 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004069 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 struct sockaddr_in *addr4 = NULL;
4071 struct sockaddr_in6 *addr6 = NULL;
4072 unsigned short snum;
James Morris2ee92d42006-11-13 16:09:01 -08004073 u32 sid, perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074
4075 if (sk->sk_family == PF_INET) {
4076 addr4 = (struct sockaddr_in *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004077 if (addrlen < sizeof(struct sockaddr_in))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 return -EINVAL;
4079 snum = ntohs(addr4->sin_port);
4080 } else {
4081 addr6 = (struct sockaddr_in6 *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004082 if (addrlen < SIN6_LEN_RFC2133)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 return -EINVAL;
4084 snum = ntohs(addr6->sin6_port);
4085 }
4086
Paul Moore3e112172008-04-10 10:48:14 -04004087 err = sel_netport_sid(sk->sk_protocol, snum, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 if (err)
4089 goto out;
4090
Paul Moore253bfae2010-04-22 14:46:19 -04004091 perm = (sksec->sclass == SECCLASS_TCP_SOCKET) ?
James Morris2ee92d42006-11-13 16:09:01 -08004092 TCP_SOCKET__NAME_CONNECT : DCCP_SOCKET__NAME_CONNECT;
4093
Eric Paris50c205f2012-04-04 15:01:43 -04004094 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004095 ad.u.net = &net;
4096 ad.u.net->dport = htons(snum);
4097 ad.u.net->family = sk->sk_family;
Paul Moore253bfae2010-04-22 14:46:19 -04004098 err = avc_has_perm(sksec->sid, sid, sksec->sclass, perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 if (err)
4100 goto out;
4101 }
4102
Paul Moore014ab192008-10-10 10:16:33 -04004103 err = selinux_netlbl_socket_connect(sk, address);
4104
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105out:
4106 return err;
4107}
4108
4109static int selinux_socket_listen(struct socket *sock, int backlog)
4110{
Paul Moore253bfae2010-04-22 14:46:19 -04004111 return sock_has_perm(current, sock->sk, SOCKET__LISTEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112}
4113
4114static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
4115{
4116 int err;
4117 struct inode_security_struct *isec;
4118 struct inode_security_struct *newisec;
4119
Paul Moore253bfae2010-04-22 14:46:19 -04004120 err = sock_has_perm(current, sock->sk, SOCKET__ACCEPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 if (err)
4122 return err;
4123
4124 newisec = SOCK_INODE(newsock)->i_security;
4125
4126 isec = SOCK_INODE(sock)->i_security;
4127 newisec->sclass = isec->sclass;
4128 newisec->sid = isec->sid;
4129 newisec->initialized = 1;
4130
4131 return 0;
4132}
4133
4134static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
Eric Paris828dfe12008-04-17 13:17:49 -04004135 int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136{
Paul Moore253bfae2010-04-22 14:46:19 -04004137 return sock_has_perm(current, sock->sk, SOCKET__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138}
4139
4140static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
4141 int size, int flags)
4142{
Paul Moore253bfae2010-04-22 14:46:19 -04004143 return sock_has_perm(current, sock->sk, SOCKET__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144}
4145
4146static int selinux_socket_getsockname(struct socket *sock)
4147{
Paul Moore253bfae2010-04-22 14:46:19 -04004148 return sock_has_perm(current, sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149}
4150
4151static int selinux_socket_getpeername(struct socket *sock)
4152{
Paul Moore253bfae2010-04-22 14:46:19 -04004153 return sock_has_perm(current, sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154}
4155
Eric Paris828dfe12008-04-17 13:17:49 -04004156static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157{
Paul Mooref8687af2006-10-30 15:22:15 -08004158 int err;
4159
Paul Moore253bfae2010-04-22 14:46:19 -04004160 err = sock_has_perm(current, sock->sk, SOCKET__SETOPT);
Paul Mooref8687af2006-10-30 15:22:15 -08004161 if (err)
4162 return err;
4163
4164 return selinux_netlbl_socket_setsockopt(sock, level, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165}
4166
4167static int selinux_socket_getsockopt(struct socket *sock, int level,
4168 int optname)
4169{
Paul Moore253bfae2010-04-22 14:46:19 -04004170 return sock_has_perm(current, sock->sk, SOCKET__GETOPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171}
4172
4173static int selinux_socket_shutdown(struct socket *sock, int how)
4174{
Paul Moore253bfae2010-04-22 14:46:19 -04004175 return sock_has_perm(current, sock->sk, SOCKET__SHUTDOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176}
4177
David S. Miller3610cda2011-01-05 15:38:53 -08004178static int selinux_socket_unix_stream_connect(struct sock *sock,
4179 struct sock *other,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 struct sock *newsk)
4181{
David S. Miller3610cda2011-01-05 15:38:53 -08004182 struct sk_security_struct *sksec_sock = sock->sk_security;
4183 struct sk_security_struct *sksec_other = other->sk_security;
Paul Moore4d1e2452010-04-22 14:46:18 -04004184 struct sk_security_struct *sksec_new = newsk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004185 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004186 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 int err;
4188
Eric Paris50c205f2012-04-04 15:01:43 -04004189 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004190 ad.u.net = &net;
4191 ad.u.net->sk = other;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192
Paul Moore4d1e2452010-04-22 14:46:18 -04004193 err = avc_has_perm(sksec_sock->sid, sksec_other->sid,
4194 sksec_other->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 UNIX_STREAM_SOCKET__CONNECTTO, &ad);
4196 if (err)
4197 return err;
4198
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 /* server child socket */
Paul Moore4d1e2452010-04-22 14:46:18 -04004200 sksec_new->peer_sid = sksec_sock->sid;
4201 err = security_sid_mls_copy(sksec_other->sid, sksec_sock->sid,
4202 &sksec_new->sid);
4203 if (err)
4204 return err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004205
Paul Moore4d1e2452010-04-22 14:46:18 -04004206 /* connecting socket */
4207 sksec_sock->peer_sid = sksec_new->sid;
4208
4209 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210}
4211
4212static int selinux_socket_unix_may_send(struct socket *sock,
4213 struct socket *other)
4214{
Paul Moore253bfae2010-04-22 14:46:19 -04004215 struct sk_security_struct *ssec = sock->sk->sk_security;
4216 struct sk_security_struct *osec = other->sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004217 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004218 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219
Eric Paris50c205f2012-04-04 15:01:43 -04004220 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004221 ad.u.net = &net;
4222 ad.u.net->sk = other->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223
Paul Moore253bfae2010-04-22 14:46:19 -04004224 return avc_has_perm(ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
4225 &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226}
4227
Paul Mooreeffad8d2008-01-29 08:49:27 -05004228static int selinux_inet_sys_rcv_skb(int ifindex, char *addrp, u16 family,
4229 u32 peer_sid,
Thomas Liu2bf49692009-07-14 12:14:09 -04004230 struct common_audit_data *ad)
Paul Mooreeffad8d2008-01-29 08:49:27 -05004231{
4232 int err;
4233 u32 if_sid;
4234 u32 node_sid;
4235
4236 err = sel_netif_sid(ifindex, &if_sid);
4237 if (err)
4238 return err;
4239 err = avc_has_perm(peer_sid, if_sid,
4240 SECCLASS_NETIF, NETIF__INGRESS, ad);
4241 if (err)
4242 return err;
4243
4244 err = sel_netnode_sid(addrp, family, &node_sid);
4245 if (err)
4246 return err;
4247 return avc_has_perm(peer_sid, node_sid,
4248 SECCLASS_NODE, NODE__RECVFROM, ad);
4249}
4250
Paul Moore220deb92008-01-29 08:38:23 -05004251static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
Paul Moored8395c82008-10-10 10:16:30 -04004252 u16 family)
Paul Moore220deb92008-01-29 08:38:23 -05004253{
Paul Moore277d3422008-12-31 12:54:11 -05004254 int err = 0;
Paul Moore220deb92008-01-29 08:38:23 -05004255 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004256 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004257 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004258 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04004259 char *addrp;
4260
Eric Paris50c205f2012-04-04 15:01:43 -04004261 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004262 ad.u.net = &net;
4263 ad.u.net->netif = skb->skb_iif;
4264 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004265 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4266 if (err)
4267 return err;
Paul Moore220deb92008-01-29 08:38:23 -05004268
Paul Moore58bfbb52009-03-27 17:10:41 -04004269 if (selinux_secmark_enabled()) {
Paul Moore220deb92008-01-29 08:38:23 -05004270 err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Moored8395c82008-10-10 10:16:30 -04004271 PACKET__RECV, &ad);
Paul Moore58bfbb52009-03-27 17:10:41 -04004272 if (err)
4273 return err;
4274 }
Paul Moore220deb92008-01-29 08:38:23 -05004275
Steffen Klassertb9679a72011-02-23 12:55:21 +01004276 err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
4277 if (err)
4278 return err;
4279 err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004280
James Morris4e5ab4c2006-06-09 00:33:33 -07004281 return err;
4282}
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004283
James Morris4e5ab4c2006-06-09 00:33:33 -07004284static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
4285{
Paul Moore220deb92008-01-29 08:38:23 -05004286 int err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004287 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004288 u16 family = sk->sk_family;
4289 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004290 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004291 struct lsm_network_audit net = {0,};
Paul Moore220deb92008-01-29 08:38:23 -05004292 char *addrp;
Paul Moored8395c82008-10-10 10:16:30 -04004293 u8 secmark_active;
4294 u8 peerlbl_active;
James Morris4e5ab4c2006-06-09 00:33:33 -07004295
James Morris4e5ab4c2006-06-09 00:33:33 -07004296 if (family != PF_INET && family != PF_INET6)
Paul Moore220deb92008-01-29 08:38:23 -05004297 return 0;
James Morris4e5ab4c2006-06-09 00:33:33 -07004298
4299 /* Handle mapped IPv4 packets arriving via IPv6 sockets */
Al Viro87fcd702006-12-04 22:00:55 +00004300 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
James Morris4e5ab4c2006-06-09 00:33:33 -07004301 family = PF_INET;
4302
Paul Moored8395c82008-10-10 10:16:30 -04004303 /* If any sort of compatibility mode is enabled then handoff processing
4304 * to the selinux_sock_rcv_skb_compat() function to deal with the
4305 * special handling. We do this in an attempt to keep this function
4306 * as fast and as clean as possible. */
Paul Moore58bfbb52009-03-27 17:10:41 -04004307 if (!selinux_policycap_netpeer)
Paul Moored8395c82008-10-10 10:16:30 -04004308 return selinux_sock_rcv_skb_compat(sk, skb, family);
4309
4310 secmark_active = selinux_secmark_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04004311 peerlbl_active = selinux_peerlbl_enabled();
Paul Moored8395c82008-10-10 10:16:30 -04004312 if (!secmark_active && !peerlbl_active)
4313 return 0;
4314
Eric Paris50c205f2012-04-04 15:01:43 -04004315 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004316 ad.u.net = &net;
4317 ad.u.net->netif = skb->skb_iif;
4318 ad.u.net->family = family;
Paul Moore224dfbd2008-01-29 08:38:13 -05004319 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
James Morris4e5ab4c2006-06-09 00:33:33 -07004320 if (err)
Paul Moore220deb92008-01-29 08:38:23 -05004321 return err;
James Morris4e5ab4c2006-06-09 00:33:33 -07004322
Paul Moored8395c82008-10-10 10:16:30 -04004323 if (peerlbl_active) {
Paul Moored621d352008-01-29 08:43:36 -05004324 u32 peer_sid;
4325
4326 err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
4327 if (err)
4328 return err;
Eric Dumazet8964be42009-11-20 15:35:04 -08004329 err = selinux_inet_sys_rcv_skb(skb->skb_iif, addrp, family,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004330 peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04004331 if (err) {
4332 selinux_netlbl_err(skb, err, 0);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004333 return err;
Paul Mooredfaebe92008-10-10 10:16:31 -04004334 }
Paul Moored621d352008-01-29 08:43:36 -05004335 err = avc_has_perm(sk_sid, peer_sid, SECCLASS_PEER,
4336 PEER__RECV, &ad);
Chad Hanson46d01d62013-12-23 17:45:01 -05004337 if (err) {
Paul Mooredfaebe92008-10-10 10:16:31 -04004338 selinux_netlbl_err(skb, err, 0);
Chad Hanson46d01d62013-12-23 17:45:01 -05004339 return err;
4340 }
Paul Moored621d352008-01-29 08:43:36 -05004341 }
4342
Paul Moored8395c82008-10-10 10:16:30 -04004343 if (secmark_active) {
Paul Mooreeffad8d2008-01-29 08:49:27 -05004344 err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
4345 PACKET__RECV, &ad);
4346 if (err)
4347 return err;
4348 }
4349
Paul Moored621d352008-01-29 08:43:36 -05004350 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351}
4352
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004353static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
4354 int __user *optlen, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355{
4356 int err = 0;
4357 char *scontext;
4358 u32 scontext_len;
Paul Moore253bfae2010-04-22 14:46:19 -04004359 struct sk_security_struct *sksec = sock->sk->sk_security;
Paul Moore3de4bab2006-11-17 17:38:54 -05004360 u32 peer_sid = SECSID_NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361
Paul Moore253bfae2010-04-22 14:46:19 -04004362 if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
4363 sksec->sclass == SECCLASS_TCP_SOCKET)
Eric Parisdd3e7832010-04-07 15:08:46 -04004364 peer_sid = sksec->peer_sid;
Paul Moore253bfae2010-04-22 14:46:19 -04004365 if (peer_sid == SECSID_NULL)
4366 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004368 err = security_sid_to_context(peer_sid, &scontext, &scontext_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 if (err)
Paul Moore253bfae2010-04-22 14:46:19 -04004370 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371
4372 if (scontext_len > len) {
4373 err = -ERANGE;
4374 goto out_len;
4375 }
4376
4377 if (copy_to_user(optval, scontext, scontext_len))
4378 err = -EFAULT;
4379
4380out_len:
4381 if (put_user(scontext_len, optlen))
4382 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 kfree(scontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 return err;
4385}
4386
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004387static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004388{
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004389 u32 peer_secid = SECSID_NULL;
Paul Moore75e22912008-01-29 08:38:04 -05004390 u16 family;
Catherine Zhang877ce7c2006-06-29 12:27:47 -07004391
Paul Mooreaa862902008-10-10 10:16:29 -04004392 if (skb && skb->protocol == htons(ETH_P_IP))
4393 family = PF_INET;
4394 else if (skb && skb->protocol == htons(ETH_P_IPV6))
4395 family = PF_INET6;
4396 else if (sock)
Paul Moore75e22912008-01-29 08:38:04 -05004397 family = sock->sk->sk_family;
Paul Moore75e22912008-01-29 08:38:04 -05004398 else
4399 goto out;
4400
4401 if (sock && family == PF_UNIX)
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02004402 selinux_inode_getsecid(SOCK_INODE(sock), &peer_secid);
Paul Moore3de4bab2006-11-17 17:38:54 -05004403 else if (skb)
Paul Moore220deb92008-01-29 08:38:23 -05004404 selinux_skb_peerlbl_sid(skb, family, &peer_secid);
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004405
Paul Moore75e22912008-01-29 08:38:04 -05004406out:
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004407 *secid = peer_secid;
Paul Moore75e22912008-01-29 08:38:04 -05004408 if (peer_secid == SECSID_NULL)
4409 return -EINVAL;
4410 return 0;
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004411}
4412
Al Viro7d877f32005-10-21 03:20:43 -04004413static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414{
Paul Moore84914b72010-04-22 14:46:18 -04004415 struct sk_security_struct *sksec;
4416
4417 sksec = kzalloc(sizeof(*sksec), priority);
4418 if (!sksec)
4419 return -ENOMEM;
4420
4421 sksec->peer_sid = SECINITSID_UNLABELED;
4422 sksec->sid = SECINITSID_UNLABELED;
4423 selinux_netlbl_sk_security_reset(sksec);
4424 sk->sk_security = sksec;
4425
4426 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427}
4428
4429static void selinux_sk_free_security(struct sock *sk)
4430{
Paul Moore84914b72010-04-22 14:46:18 -04004431 struct sk_security_struct *sksec = sk->sk_security;
4432
4433 sk->sk_security = NULL;
4434 selinux_netlbl_sk_security_free(sksec);
4435 kfree(sksec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436}
4437
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004438static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
4439{
Eric Parisdd3e7832010-04-07 15:08:46 -04004440 struct sk_security_struct *sksec = sk->sk_security;
4441 struct sk_security_struct *newsksec = newsk->sk_security;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004442
Eric Parisdd3e7832010-04-07 15:08:46 -04004443 newsksec->sid = sksec->sid;
4444 newsksec->peer_sid = sksec->peer_sid;
4445 newsksec->sclass = sksec->sclass;
Paul Moore99f59ed2006-08-29 17:53:48 -07004446
Eric Parisdd3e7832010-04-07 15:08:46 -04004447 selinux_netlbl_sk_security_reset(newsksec);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004448}
4449
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07004450static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004451{
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004452 if (!sk)
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07004453 *secid = SECINITSID_ANY_SOCKET;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004454 else {
4455 struct sk_security_struct *sksec = sk->sk_security;
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004456
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07004457 *secid = sksec->sid;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004458 }
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004459}
4460
Eric Paris828dfe12008-04-17 13:17:49 -04004461static void selinux_sock_graft(struct sock *sk, struct socket *parent)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004462{
4463 struct inode_security_struct *isec = SOCK_INODE(parent)->i_security;
4464 struct sk_security_struct *sksec = sk->sk_security;
4465
David Woodhouse2148ccc2006-09-29 15:50:25 -07004466 if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
4467 sk->sk_family == PF_UNIX)
4468 isec->sid = sksec->sid;
Paul Moore220deb92008-01-29 08:38:23 -05004469 sksec->sclass = isec->sclass;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004470}
4471
Adrian Bunk9a673e52006-08-15 00:03:53 -07004472static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4473 struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004474{
4475 struct sk_security_struct *sksec = sk->sk_security;
4476 int err;
Paul Mooreaa862902008-10-10 10:16:29 -04004477 u16 family = sk->sk_family;
Paul Moore446b8022013-12-04 16:10:51 -05004478 u32 connsid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004479 u32 peersid;
4480
Paul Mooreaa862902008-10-10 10:16:29 -04004481 /* handle mapped IPv4 packets arriving via IPv6 sockets */
4482 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4483 family = PF_INET;
4484
4485 err = selinux_skb_peerlbl_sid(skb, family, &peersid);
Paul Moore220deb92008-01-29 08:38:23 -05004486 if (err)
4487 return err;
Paul Moore446b8022013-12-04 16:10:51 -05004488 err = selinux_conn_sid(sksec->sid, peersid, &connsid);
4489 if (err)
4490 return err;
4491 req->secid = connsid;
4492 req->peer_secid = peersid;
Venkat Yekkiralaa51c64f2006-07-27 22:01:34 -07004493
Paul Moore389fb8002009-03-27 17:10:34 -04004494 return selinux_netlbl_inet_conn_request(req, family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004495}
4496
Adrian Bunk9a673e52006-08-15 00:03:53 -07004497static void selinux_inet_csk_clone(struct sock *newsk,
4498 const struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004499{
4500 struct sk_security_struct *newsksec = newsk->sk_security;
4501
4502 newsksec->sid = req->secid;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004503 newsksec->peer_sid = req->peer_secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004504 /* NOTE: Ideally, we should also get the isec->sid for the
4505 new socket in sync, but we don't have the isec available yet.
4506 So we will wait until sock_graft to do it, by which
4507 time it will have been created and available. */
Paul Moore99f59ed2006-08-29 17:53:48 -07004508
Paul Moore9f2ad662006-11-17 17:38:53 -05004509 /* We don't need to take any sort of lock here as we are the only
4510 * thread with access to newsksec */
Paul Moore389fb8002009-03-27 17:10:34 -04004511 selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004512}
4513
Paul Moore014ab192008-10-10 10:16:33 -04004514static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004515{
Paul Mooreaa862902008-10-10 10:16:29 -04004516 u16 family = sk->sk_family;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004517 struct sk_security_struct *sksec = sk->sk_security;
4518
Paul Mooreaa862902008-10-10 10:16:29 -04004519 /* handle mapped IPv4 packets arriving via IPv6 sockets */
4520 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4521 family = PF_INET;
4522
4523 selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004524}
4525
Eric Dumazetca10b9e2013-04-08 17:58:11 +00004526static void selinux_skb_owned_by(struct sk_buff *skb, struct sock *sk)
4527{
4528 skb_set_owner_w(skb, sk);
4529}
4530
Eric Paris2606fd12010-10-13 16:24:41 -04004531static int selinux_secmark_relabel_packet(u32 sid)
4532{
4533 const struct task_security_struct *__tsec;
4534 u32 tsid;
4535
4536 __tsec = current_security();
4537 tsid = __tsec->sid;
4538
4539 return avc_has_perm(tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO, NULL);
4540}
4541
4542static void selinux_secmark_refcount_inc(void)
4543{
4544 atomic_inc(&selinux_secmark_refcount);
4545}
4546
4547static void selinux_secmark_refcount_dec(void)
4548{
4549 atomic_dec(&selinux_secmark_refcount);
4550}
4551
Adrian Bunk9a673e52006-08-15 00:03:53 -07004552static void selinux_req_classify_flow(const struct request_sock *req,
4553 struct flowi *fl)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004554{
David S. Miller1d28f422011-03-12 00:29:39 -05004555 fl->flowi_secid = req->secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004556}
4557
Paul Moore5dbbaf22013-01-14 07:12:19 +00004558static int selinux_tun_dev_alloc_security(void **security)
4559{
4560 struct tun_security_struct *tunsec;
4561
4562 tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL);
4563 if (!tunsec)
4564 return -ENOMEM;
4565 tunsec->sid = current_sid();
4566
4567 *security = tunsec;
4568 return 0;
4569}
4570
4571static void selinux_tun_dev_free_security(void *security)
4572{
4573 kfree(security);
4574}
4575
Paul Mooreed6d76e2009-08-28 18:12:49 -04004576static int selinux_tun_dev_create(void)
4577{
4578 u32 sid = current_sid();
4579
4580 /* we aren't taking into account the "sockcreate" SID since the socket
4581 * that is being created here is not a socket in the traditional sense,
4582 * instead it is a private sock, accessible only to the kernel, and
4583 * representing a wide range of network traffic spanning multiple
4584 * connections unlike traditional sockets - check the TUN driver to
4585 * get a better understanding of why this socket is special */
4586
4587 return avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
4588 NULL);
4589}
4590
Paul Moore5dbbaf22013-01-14 07:12:19 +00004591static int selinux_tun_dev_attach_queue(void *security)
Paul Mooreed6d76e2009-08-28 18:12:49 -04004592{
Paul Moore5dbbaf22013-01-14 07:12:19 +00004593 struct tun_security_struct *tunsec = security;
4594
4595 return avc_has_perm(current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET,
4596 TUN_SOCKET__ATTACH_QUEUE, NULL);
4597}
4598
4599static int selinux_tun_dev_attach(struct sock *sk, void *security)
4600{
4601 struct tun_security_struct *tunsec = security;
Paul Mooreed6d76e2009-08-28 18:12:49 -04004602 struct sk_security_struct *sksec = sk->sk_security;
4603
4604 /* we don't currently perform any NetLabel based labeling here and it
4605 * isn't clear that we would want to do so anyway; while we could apply
4606 * labeling without the support of the TUN user the resulting labeled
4607 * traffic from the other end of the connection would almost certainly
4608 * cause confusion to the TUN user that had no idea network labeling
4609 * protocols were being used */
4610
Paul Moore5dbbaf22013-01-14 07:12:19 +00004611 sksec->sid = tunsec->sid;
Paul Mooreed6d76e2009-08-28 18:12:49 -04004612 sksec->sclass = SECCLASS_TUN_SOCKET;
Paul Moore5dbbaf22013-01-14 07:12:19 +00004613
4614 return 0;
Paul Mooreed6d76e2009-08-28 18:12:49 -04004615}
4616
Paul Moore5dbbaf22013-01-14 07:12:19 +00004617static int selinux_tun_dev_open(void *security)
Paul Mooreed6d76e2009-08-28 18:12:49 -04004618{
Paul Moore5dbbaf22013-01-14 07:12:19 +00004619 struct tun_security_struct *tunsec = security;
Paul Mooreed6d76e2009-08-28 18:12:49 -04004620 u32 sid = current_sid();
4621 int err;
4622
Paul Moore5dbbaf22013-01-14 07:12:19 +00004623 err = avc_has_perm(sid, tunsec->sid, SECCLASS_TUN_SOCKET,
Paul Mooreed6d76e2009-08-28 18:12:49 -04004624 TUN_SOCKET__RELABELFROM, NULL);
4625 if (err)
4626 return err;
4627 err = avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET,
4628 TUN_SOCKET__RELABELTO, NULL);
4629 if (err)
4630 return err;
Paul Moore5dbbaf22013-01-14 07:12:19 +00004631 tunsec->sid = sid;
Paul Mooreed6d76e2009-08-28 18:12:49 -04004632
4633 return 0;
4634}
4635
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
4637{
4638 int err = 0;
4639 u32 perm;
4640 struct nlmsghdr *nlh;
Paul Moore253bfae2010-04-22 14:46:19 -04004641 struct sk_security_struct *sksec = sk->sk_security;
Eric Paris828dfe12008-04-17 13:17:49 -04004642
Hong zhi guo77954982013-03-27 06:49:35 +00004643 if (skb->len < NLMSG_HDRLEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 err = -EINVAL;
4645 goto out;
4646 }
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -07004647 nlh = nlmsg_hdr(skb);
Eric Paris828dfe12008-04-17 13:17:49 -04004648
Paul Moore253bfae2010-04-22 14:46:19 -04004649 err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 if (err) {
4651 if (err == -EINVAL) {
David Woodhouse9ad9ad32005-06-22 15:04:33 +01004652 audit_log(current->audit_context, GFP_KERNEL, AUDIT_SELINUX_ERR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 "SELinux: unrecognized netlink message"
4654 " type=%hu for sclass=%hu\n",
Paul Moore253bfae2010-04-22 14:46:19 -04004655 nlh->nlmsg_type, sksec->sclass);
Eric Paris39c9aed2008-11-05 09:34:42 -05004656 if (!selinux_enforcing || security_get_allow_unknown())
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 err = 0;
4658 }
4659
4660 /* Ignore */
4661 if (err == -ENOENT)
4662 err = 0;
4663 goto out;
4664 }
4665
Paul Moore253bfae2010-04-22 14:46:19 -04004666 err = sock_has_perm(current, sk, perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667out:
4668 return err;
4669}
4670
4671#ifdef CONFIG_NETFILTER
4672
Paul Mooreeffad8d2008-01-29 08:49:27 -05004673static unsigned int selinux_ip_forward(struct sk_buff *skb, int ifindex,
4674 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675{
Paul Mooredfaebe92008-10-10 10:16:31 -04004676 int err;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004677 char *addrp;
4678 u32 peer_sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004679 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004680 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05004681 u8 secmark_active;
Paul Moore948bf852008-10-10 10:16:32 -04004682 u8 netlbl_active;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004683 u8 peerlbl_active;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004684
Paul Mooreeffad8d2008-01-29 08:49:27 -05004685 if (!selinux_policycap_netpeer)
4686 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004687
Paul Mooreeffad8d2008-01-29 08:49:27 -05004688 secmark_active = selinux_secmark_enabled();
Paul Moore948bf852008-10-10 10:16:32 -04004689 netlbl_active = netlbl_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04004690 peerlbl_active = selinux_peerlbl_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05004691 if (!secmark_active && !peerlbl_active)
4692 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004693
Paul Moored8395c82008-10-10 10:16:30 -04004694 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
4695 return NF_DROP;
4696
Eric Paris50c205f2012-04-04 15:01:43 -04004697 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004698 ad.u.net = &net;
4699 ad.u.net->netif = ifindex;
4700 ad.u.net->family = family;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004701 if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
4702 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703
Paul Mooredfaebe92008-10-10 10:16:31 -04004704 if (peerlbl_active) {
4705 err = selinux_inet_sys_rcv_skb(ifindex, addrp, family,
4706 peer_sid, &ad);
4707 if (err) {
4708 selinux_netlbl_err(skb, err, 1);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004709 return NF_DROP;
Paul Mooredfaebe92008-10-10 10:16:31 -04004710 }
4711 }
Paul Mooreeffad8d2008-01-29 08:49:27 -05004712
4713 if (secmark_active)
4714 if (avc_has_perm(peer_sid, skb->secmark,
4715 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
4716 return NF_DROP;
4717
Paul Moore948bf852008-10-10 10:16:32 -04004718 if (netlbl_active)
4719 /* we do this in the FORWARD path and not the POST_ROUTING
4720 * path because we want to make sure we apply the necessary
4721 * labeling before IPsec is applied so we can leverage AH
4722 * protection */
4723 if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
4724 return NF_DROP;
4725
Paul Mooreeffad8d2008-01-29 08:49:27 -05004726 return NF_ACCEPT;
4727}
4728
Patrick McHardy795aa6e2013-10-10 09:21:55 +02004729static unsigned int selinux_ipv4_forward(const struct nf_hook_ops *ops,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004730 struct sk_buff *skb,
4731 const struct net_device *in,
4732 const struct net_device *out,
4733 int (*okfn)(struct sk_buff *))
4734{
4735 return selinux_ip_forward(skb, in->ifindex, PF_INET);
4736}
4737
4738#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Patrick McHardy795aa6e2013-10-10 09:21:55 +02004739static unsigned int selinux_ipv6_forward(const struct nf_hook_ops *ops,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004740 struct sk_buff *skb,
4741 const struct net_device *in,
4742 const struct net_device *out,
4743 int (*okfn)(struct sk_buff *))
4744{
4745 return selinux_ip_forward(skb, in->ifindex, PF_INET6);
4746}
4747#endif /* IPV6 */
4748
Paul Moore948bf852008-10-10 10:16:32 -04004749static unsigned int selinux_ip_output(struct sk_buff *skb,
4750 u16 family)
4751{
Paul Moore47180062013-12-04 16:10:45 -05004752 struct sock *sk;
Paul Moore948bf852008-10-10 10:16:32 -04004753 u32 sid;
4754
4755 if (!netlbl_enabled())
4756 return NF_ACCEPT;
4757
4758 /* we do this in the LOCAL_OUT path and not the POST_ROUTING path
4759 * because we want to make sure we apply the necessary labeling
4760 * before IPsec is applied so we can leverage AH protection */
Paul Moore47180062013-12-04 16:10:45 -05004761 sk = skb->sk;
4762 if (sk) {
4763 struct sk_security_struct *sksec;
4764
4765 if (sk->sk_state == TCP_LISTEN)
4766 /* if the socket is the listening state then this
4767 * packet is a SYN-ACK packet which means it needs to
4768 * be labeled based on the connection/request_sock and
4769 * not the parent socket. unfortunately, we can't
4770 * lookup the request_sock yet as it isn't queued on
4771 * the parent socket until after the SYN-ACK is sent.
4772 * the "solution" is to simply pass the packet as-is
4773 * as any IP option based labeling should be copied
4774 * from the initial connection request (in the IP
4775 * layer). it is far from ideal, but until we get a
4776 * security label in the packet itself this is the
4777 * best we can do. */
4778 return NF_ACCEPT;
4779
4780 /* standard practice, label using the parent socket */
4781 sksec = sk->sk_security;
Paul Moore948bf852008-10-10 10:16:32 -04004782 sid = sksec->sid;
4783 } else
4784 sid = SECINITSID_KERNEL;
4785 if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
4786 return NF_DROP;
4787
4788 return NF_ACCEPT;
4789}
4790
Patrick McHardy795aa6e2013-10-10 09:21:55 +02004791static unsigned int selinux_ipv4_output(const struct nf_hook_ops *ops,
Paul Moore948bf852008-10-10 10:16:32 -04004792 struct sk_buff *skb,
4793 const struct net_device *in,
4794 const struct net_device *out,
4795 int (*okfn)(struct sk_buff *))
4796{
4797 return selinux_ip_output(skb, PF_INET);
4798}
4799
Paul Mooreeffad8d2008-01-29 08:49:27 -05004800static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
4801 int ifindex,
Paul Moored8395c82008-10-10 10:16:30 -04004802 u16 family)
James Morris4e5ab4c2006-06-09 00:33:33 -07004803{
Paul Mooreeffad8d2008-01-29 08:49:27 -05004804 struct sock *sk = skb->sk;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004805 struct sk_security_struct *sksec;
Thomas Liu2bf49692009-07-14 12:14:09 -04004806 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004807 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04004808 char *addrp;
4809 u8 proto;
James Morris4e5ab4c2006-06-09 00:33:33 -07004810
Paul Mooreeffad8d2008-01-29 08:49:27 -05004811 if (sk == NULL)
4812 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004813 sksec = sk->sk_security;
James Morris4e5ab4c2006-06-09 00:33:33 -07004814
Eric Paris50c205f2012-04-04 15:01:43 -04004815 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004816 ad.u.net = &net;
4817 ad.u.net->netif = ifindex;
4818 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004819 if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
4820 return NF_DROP;
4821
Paul Moore58bfbb52009-03-27 17:10:41 -04004822 if (selinux_secmark_enabled())
Paul Mooreeffad8d2008-01-29 08:49:27 -05004823 if (avc_has_perm(sksec->sid, skb->secmark,
Paul Moored8395c82008-10-10 10:16:30 -04004824 SECCLASS_PACKET, PACKET__SEND, &ad))
Eric Paris2fe66ec2010-11-23 06:28:08 +00004825 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07004826
Steffen Klassertb9679a72011-02-23 12:55:21 +01004827 if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
4828 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07004829
Paul Mooreeffad8d2008-01-29 08:49:27 -05004830 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831}
4832
Paul Mooreeffad8d2008-01-29 08:49:27 -05004833static unsigned int selinux_ip_postroute(struct sk_buff *skb, int ifindex,
4834 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835{
Paul Mooreeffad8d2008-01-29 08:49:27 -05004836 u32 secmark_perm;
4837 u32 peer_sid;
4838 struct sock *sk;
Thomas Liu2bf49692009-07-14 12:14:09 -04004839 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004840 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05004841 char *addrp;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004842 u8 secmark_active;
4843 u8 peerlbl_active;
4844
Paul Mooreeffad8d2008-01-29 08:49:27 -05004845 /* If any sort of compatibility mode is enabled then handoff processing
4846 * to the selinux_ip_postroute_compat() function to deal with the
4847 * special handling. We do this in an attempt to keep this function
4848 * as fast and as clean as possible. */
Paul Moore58bfbb52009-03-27 17:10:41 -04004849 if (!selinux_policycap_netpeer)
Paul Moored8395c82008-10-10 10:16:30 -04004850 return selinux_ip_postroute_compat(skb, ifindex, family);
Paul Moorec0828e52013-12-10 14:58:01 -05004851
Paul Mooreeffad8d2008-01-29 08:49:27 -05004852 secmark_active = selinux_secmark_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04004853 peerlbl_active = selinux_peerlbl_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05004854 if (!secmark_active && !peerlbl_active)
4855 return NF_ACCEPT;
4856
Paul Mooreeffad8d2008-01-29 08:49:27 -05004857 sk = skb->sk;
Paul Moorec0828e52013-12-10 14:58:01 -05004858
Paul Mooreeffad8d2008-01-29 08:49:27 -05004859#ifdef CONFIG_XFRM
4860 /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
4861 * packet transformation so allow the packet to pass without any checks
4862 * since we'll have another chance to perform access control checks
4863 * when the packet is on it's final way out.
4864 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
Paul Moorec0828e52013-12-10 14:58:01 -05004865 * is NULL, in this case go ahead and apply access control.
4866 * NOTE: if this is a local socket (skb->sk != NULL) that is in the
4867 * TCP listening state we cannot wait until the XFRM processing
4868 * is done as we will miss out on the SA label if we do;
4869 * unfortunately, this means more work, but it is only once per
4870 * connection. */
4871 if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL &&
4872 !(sk != NULL && sk->sk_state == TCP_LISTEN))
Paul Mooreeffad8d2008-01-29 08:49:27 -05004873 return NF_ACCEPT;
4874#endif
Paul Mooreeffad8d2008-01-29 08:49:27 -05004875
Paul Moored8395c82008-10-10 10:16:30 -04004876 if (sk == NULL) {
Paul Moore446b8022013-12-04 16:10:51 -05004877 /* Without an associated socket the packet is either coming
4878 * from the kernel or it is being forwarded; check the packet
4879 * to determine which and if the packet is being forwarded
4880 * query the packet directly to determine the security label. */
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01004881 if (skb->skb_iif) {
4882 secmark_perm = PACKET__FORWARD_OUT;
Paul Moored8395c82008-10-10 10:16:30 -04004883 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00004884 return NF_DROP;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01004885 } else {
4886 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04004887 peer_sid = SECINITSID_KERNEL;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01004888 }
Paul Moore446b8022013-12-04 16:10:51 -05004889 } else if (sk->sk_state == TCP_LISTEN) {
4890 /* Locally generated packet but the associated socket is in the
4891 * listening state which means this is a SYN-ACK packet. In
4892 * this particular case the correct security label is assigned
4893 * to the connection/request_sock but unfortunately we can't
4894 * query the request_sock as it isn't queued on the parent
4895 * socket until after the SYN-ACK packet is sent; the only
4896 * viable choice is to regenerate the label like we do in
4897 * selinux_inet_conn_request(). See also selinux_ip_output()
4898 * for similar problems. */
4899 u32 skb_sid;
4900 struct sk_security_struct *sksec = sk->sk_security;
4901 if (selinux_skb_peerlbl_sid(skb, family, &skb_sid))
4902 return NF_DROP;
Paul Moorec0828e52013-12-10 14:58:01 -05004903 /* At this point, if the returned skb peerlbl is SECSID_NULL
4904 * and the packet has been through at least one XFRM
4905 * transformation then we must be dealing with the "final"
4906 * form of labeled IPsec packet; since we've already applied
4907 * all of our access controls on this packet we can safely
4908 * pass the packet. */
4909 if (skb_sid == SECSID_NULL) {
4910 switch (family) {
4911 case PF_INET:
4912 if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
4913 return NF_ACCEPT;
4914 break;
4915 case PF_INET6:
4916 if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED)
4917 return NF_ACCEPT;
4918 default:
4919 return NF_DROP_ERR(-ECONNREFUSED);
4920 }
4921 }
Paul Moore446b8022013-12-04 16:10:51 -05004922 if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid))
4923 return NF_DROP;
4924 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04004925 } else {
Paul Moore446b8022013-12-04 16:10:51 -05004926 /* Locally generated packet, fetch the security label from the
4927 * associated socket. */
Paul Mooreeffad8d2008-01-29 08:49:27 -05004928 struct sk_security_struct *sksec = sk->sk_security;
4929 peer_sid = sksec->sid;
4930 secmark_perm = PACKET__SEND;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004931 }
4932
Eric Paris50c205f2012-04-04 15:01:43 -04004933 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004934 ad.u.net = &net;
4935 ad.u.net->netif = ifindex;
4936 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004937 if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
Eric Paris04f6d702010-11-23 06:28:02 +00004938 return NF_DROP;
Paul Moored8395c82008-10-10 10:16:30 -04004939
Paul Mooreeffad8d2008-01-29 08:49:27 -05004940 if (secmark_active)
4941 if (avc_has_perm(peer_sid, skb->secmark,
4942 SECCLASS_PACKET, secmark_perm, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00004943 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004944
4945 if (peerlbl_active) {
4946 u32 if_sid;
4947 u32 node_sid;
4948
4949 if (sel_netif_sid(ifindex, &if_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00004950 return NF_DROP;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004951 if (avc_has_perm(peer_sid, if_sid,
4952 SECCLASS_NETIF, NETIF__EGRESS, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00004953 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004954
4955 if (sel_netnode_sid(addrp, family, &node_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00004956 return NF_DROP;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004957 if (avc_has_perm(peer_sid, node_sid,
4958 SECCLASS_NODE, NODE__SENDTO, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00004959 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004960 }
4961
4962 return NF_ACCEPT;
4963}
4964
Patrick McHardy795aa6e2013-10-10 09:21:55 +02004965static unsigned int selinux_ipv4_postroute(const struct nf_hook_ops *ops,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004966 struct sk_buff *skb,
4967 const struct net_device *in,
4968 const struct net_device *out,
4969 int (*okfn)(struct sk_buff *))
4970{
4971 return selinux_ip_postroute(skb, out->ifindex, PF_INET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972}
4973
4974#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Patrick McHardy795aa6e2013-10-10 09:21:55 +02004975static unsigned int selinux_ipv6_postroute(const struct nf_hook_ops *ops,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004976 struct sk_buff *skb,
4977 const struct net_device *in,
4978 const struct net_device *out,
4979 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980{
Paul Mooreeffad8d2008-01-29 08:49:27 -05004981 return selinux_ip_postroute(skb, out->ifindex, PF_INET6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983#endif /* IPV6 */
4984
4985#endif /* CONFIG_NETFILTER */
4986
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
4988{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989 int err;
4990
Eric Paris200ac532009-02-12 15:01:04 -05004991 err = cap_netlink_send(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992 if (err)
4993 return err;
4994
Stephen Smalley941fc5b2009-10-01 14:48:23 -04004995 return selinux_nlmsg_perm(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996}
4997
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998static int ipc_alloc_security(struct task_struct *task,
4999 struct kern_ipc_perm *perm,
5000 u16 sclass)
5001{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002 struct ipc_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +11005003 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004
James Morris89d155e2005-10-30 14:59:21 -08005005 isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006 if (!isec)
5007 return -ENOMEM;
5008
David Howells275bb412008-11-14 10:39:19 +11005009 sid = task_sid(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010 isec->sclass = sclass;
David Howells275bb412008-11-14 10:39:19 +11005011 isec->sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 perm->security = isec;
5013
5014 return 0;
5015}
5016
5017static void ipc_free_security(struct kern_ipc_perm *perm)
5018{
5019 struct ipc_security_struct *isec = perm->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020 perm->security = NULL;
5021 kfree(isec);
5022}
5023
5024static int msg_msg_alloc_security(struct msg_msg *msg)
5025{
5026 struct msg_security_struct *msec;
5027
James Morris89d155e2005-10-30 14:59:21 -08005028 msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029 if (!msec)
5030 return -ENOMEM;
5031
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 msec->sid = SECINITSID_UNLABELED;
5033 msg->security = msec;
5034
5035 return 0;
5036}
5037
5038static void msg_msg_free_security(struct msg_msg *msg)
5039{
5040 struct msg_security_struct *msec = msg->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041
5042 msg->security = NULL;
5043 kfree(msec);
5044}
5045
5046static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
Stephen Smalley6af963f2005-05-01 08:58:39 -07005047 u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005050 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005051 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053 isec = ipc_perms->security;
5054
Eric Paris50c205f2012-04-04 15:01:43 -04005055 ad.type = LSM_AUDIT_DATA_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056 ad.u.ipc_id = ipc_perms->key;
5057
David Howells275bb412008-11-14 10:39:19 +11005058 return avc_has_perm(sid, isec->sid, isec->sclass, perms, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059}
5060
5061static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
5062{
5063 return msg_msg_alloc_security(msg);
5064}
5065
5066static void selinux_msg_msg_free_security(struct msg_msg *msg)
5067{
5068 msg_msg_free_security(msg);
5069}
5070
5071/* message queue security operations */
5072static int selinux_msg_queue_alloc_security(struct msg_queue *msq)
5073{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005075 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005076 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077 int rc;
5078
5079 rc = ipc_alloc_security(current, &msq->q_perm, SECCLASS_MSGQ);
5080 if (rc)
5081 return rc;
5082
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083 isec = msq->q_perm.security;
5084
Eric Paris50c205f2012-04-04 15:01:43 -04005085 ad.type = LSM_AUDIT_DATA_IPC;
Eric Paris828dfe12008-04-17 13:17:49 -04005086 ad.u.ipc_id = msq->q_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087
David Howells275bb412008-11-14 10:39:19 +11005088 rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089 MSGQ__CREATE, &ad);
5090 if (rc) {
5091 ipc_free_security(&msq->q_perm);
5092 return rc;
5093 }
5094 return 0;
5095}
5096
5097static void selinux_msg_queue_free_security(struct msg_queue *msq)
5098{
5099 ipc_free_security(&msq->q_perm);
5100}
5101
5102static int selinux_msg_queue_associate(struct msg_queue *msq, int msqflg)
5103{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005105 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005106 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108 isec = msq->q_perm.security;
5109
Eric Paris50c205f2012-04-04 15:01:43 -04005110 ad.type = LSM_AUDIT_DATA_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111 ad.u.ipc_id = msq->q_perm.key;
5112
David Howells275bb412008-11-14 10:39:19 +11005113 return avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114 MSGQ__ASSOCIATE, &ad);
5115}
5116
5117static int selinux_msg_queue_msgctl(struct msg_queue *msq, int cmd)
5118{
5119 int err;
5120 int perms;
5121
Eric Paris828dfe12008-04-17 13:17:49 -04005122 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 case IPC_INFO:
5124 case MSG_INFO:
5125 /* No specific object, just general system-wide information. */
5126 return task_has_system(current, SYSTEM__IPC_INFO);
5127 case IPC_STAT:
5128 case MSG_STAT:
5129 perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
5130 break;
5131 case IPC_SET:
5132 perms = MSGQ__SETATTR;
5133 break;
5134 case IPC_RMID:
5135 perms = MSGQ__DESTROY;
5136 break;
5137 default:
5138 return 0;
5139 }
5140
Stephen Smalley6af963f2005-05-01 08:58:39 -07005141 err = ipc_has_perm(&msq->q_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142 return err;
5143}
5144
5145static int selinux_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg, int msqflg)
5146{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147 struct ipc_security_struct *isec;
5148 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005149 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005150 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151 int rc;
5152
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153 isec = msq->q_perm.security;
5154 msec = msg->security;
5155
5156 /*
5157 * First time through, need to assign label to the message
5158 */
5159 if (msec->sid == SECINITSID_UNLABELED) {
5160 /*
5161 * Compute new sid based on current process and
5162 * message queue this message will be stored in
5163 */
David Howells275bb412008-11-14 10:39:19 +11005164 rc = security_transition_sid(sid, isec->sid, SECCLASS_MSG,
Eric Paris652bb9b2011-02-01 11:05:40 -05005165 NULL, &msec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166 if (rc)
5167 return rc;
5168 }
5169
Eric Paris50c205f2012-04-04 15:01:43 -04005170 ad.type = LSM_AUDIT_DATA_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171 ad.u.ipc_id = msq->q_perm.key;
5172
5173 /* Can this process write to the queue? */
David Howells275bb412008-11-14 10:39:19 +11005174 rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175 MSGQ__WRITE, &ad);
5176 if (!rc)
5177 /* Can this process send the message */
David Howells275bb412008-11-14 10:39:19 +11005178 rc = avc_has_perm(sid, msec->sid, SECCLASS_MSG,
5179 MSG__SEND, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180 if (!rc)
5181 /* Can the message be put in the queue? */
David Howells275bb412008-11-14 10:39:19 +11005182 rc = avc_has_perm(msec->sid, isec->sid, SECCLASS_MSGQ,
5183 MSGQ__ENQUEUE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184
5185 return rc;
5186}
5187
5188static int selinux_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
5189 struct task_struct *target,
5190 long type, int mode)
5191{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192 struct ipc_security_struct *isec;
5193 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005194 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005195 u32 sid = task_sid(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196 int rc;
5197
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198 isec = msq->q_perm.security;
5199 msec = msg->security;
5200
Eric Paris50c205f2012-04-04 15:01:43 -04005201 ad.type = LSM_AUDIT_DATA_IPC;
Eric Paris828dfe12008-04-17 13:17:49 -04005202 ad.u.ipc_id = msq->q_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203
David Howells275bb412008-11-14 10:39:19 +11005204 rc = avc_has_perm(sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205 SECCLASS_MSGQ, MSGQ__READ, &ad);
5206 if (!rc)
David Howells275bb412008-11-14 10:39:19 +11005207 rc = avc_has_perm(sid, msec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208 SECCLASS_MSG, MSG__RECEIVE, &ad);
5209 return rc;
5210}
5211
5212/* Shared Memory security operations */
5213static int selinux_shm_alloc_security(struct shmid_kernel *shp)
5214{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005216 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005217 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218 int rc;
5219
5220 rc = ipc_alloc_security(current, &shp->shm_perm, SECCLASS_SHM);
5221 if (rc)
5222 return rc;
5223
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224 isec = shp->shm_perm.security;
5225
Eric Paris50c205f2012-04-04 15:01:43 -04005226 ad.type = LSM_AUDIT_DATA_IPC;
Eric Paris828dfe12008-04-17 13:17:49 -04005227 ad.u.ipc_id = shp->shm_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228
David Howells275bb412008-11-14 10:39:19 +11005229 rc = avc_has_perm(sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 SHM__CREATE, &ad);
5231 if (rc) {
5232 ipc_free_security(&shp->shm_perm);
5233 return rc;
5234 }
5235 return 0;
5236}
5237
5238static void selinux_shm_free_security(struct shmid_kernel *shp)
5239{
5240 ipc_free_security(&shp->shm_perm);
5241}
5242
5243static int selinux_shm_associate(struct shmid_kernel *shp, int shmflg)
5244{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005246 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005247 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249 isec = shp->shm_perm.security;
5250
Eric Paris50c205f2012-04-04 15:01:43 -04005251 ad.type = LSM_AUDIT_DATA_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252 ad.u.ipc_id = shp->shm_perm.key;
5253
David Howells275bb412008-11-14 10:39:19 +11005254 return avc_has_perm(sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255 SHM__ASSOCIATE, &ad);
5256}
5257
5258/* Note, at this point, shp is locked down */
5259static int selinux_shm_shmctl(struct shmid_kernel *shp, int cmd)
5260{
5261 int perms;
5262 int err;
5263
Eric Paris828dfe12008-04-17 13:17:49 -04005264 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265 case IPC_INFO:
5266 case SHM_INFO:
5267 /* No specific object, just general system-wide information. */
5268 return task_has_system(current, SYSTEM__IPC_INFO);
5269 case IPC_STAT:
5270 case SHM_STAT:
5271 perms = SHM__GETATTR | SHM__ASSOCIATE;
5272 break;
5273 case IPC_SET:
5274 perms = SHM__SETATTR;
5275 break;
5276 case SHM_LOCK:
5277 case SHM_UNLOCK:
5278 perms = SHM__LOCK;
5279 break;
5280 case IPC_RMID:
5281 perms = SHM__DESTROY;
5282 break;
5283 default:
5284 return 0;
5285 }
5286
Stephen Smalley6af963f2005-05-01 08:58:39 -07005287 err = ipc_has_perm(&shp->shm_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005288 return err;
5289}
5290
5291static int selinux_shm_shmat(struct shmid_kernel *shp,
5292 char __user *shmaddr, int shmflg)
5293{
5294 u32 perms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295
5296 if (shmflg & SHM_RDONLY)
5297 perms = SHM__READ;
5298 else
5299 perms = SHM__READ | SHM__WRITE;
5300
Stephen Smalley6af963f2005-05-01 08:58:39 -07005301 return ipc_has_perm(&shp->shm_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302}
5303
5304/* Semaphore security operations */
5305static int selinux_sem_alloc_security(struct sem_array *sma)
5306{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005308 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005309 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310 int rc;
5311
5312 rc = ipc_alloc_security(current, &sma->sem_perm, SECCLASS_SEM);
5313 if (rc)
5314 return rc;
5315
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 isec = sma->sem_perm.security;
5317
Eric Paris50c205f2012-04-04 15:01:43 -04005318 ad.type = LSM_AUDIT_DATA_IPC;
Eric Paris828dfe12008-04-17 13:17:49 -04005319 ad.u.ipc_id = sma->sem_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320
David Howells275bb412008-11-14 10:39:19 +11005321 rc = avc_has_perm(sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322 SEM__CREATE, &ad);
5323 if (rc) {
5324 ipc_free_security(&sma->sem_perm);
5325 return rc;
5326 }
5327 return 0;
5328}
5329
5330static void selinux_sem_free_security(struct sem_array *sma)
5331{
5332 ipc_free_security(&sma->sem_perm);
5333}
5334
5335static int selinux_sem_associate(struct sem_array *sma, int semflg)
5336{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005338 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005339 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341 isec = sma->sem_perm.security;
5342
Eric Paris50c205f2012-04-04 15:01:43 -04005343 ad.type = LSM_AUDIT_DATA_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344 ad.u.ipc_id = sma->sem_perm.key;
5345
David Howells275bb412008-11-14 10:39:19 +11005346 return avc_has_perm(sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347 SEM__ASSOCIATE, &ad);
5348}
5349
5350/* Note, at this point, sma is locked down */
5351static int selinux_sem_semctl(struct sem_array *sma, int cmd)
5352{
5353 int err;
5354 u32 perms;
5355
Eric Paris828dfe12008-04-17 13:17:49 -04005356 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 case IPC_INFO:
5358 case SEM_INFO:
5359 /* No specific object, just general system-wide information. */
5360 return task_has_system(current, SYSTEM__IPC_INFO);
5361 case GETPID:
5362 case GETNCNT:
5363 case GETZCNT:
5364 perms = SEM__GETATTR;
5365 break;
5366 case GETVAL:
5367 case GETALL:
5368 perms = SEM__READ;
5369 break;
5370 case SETVAL:
5371 case SETALL:
5372 perms = SEM__WRITE;
5373 break;
5374 case IPC_RMID:
5375 perms = SEM__DESTROY;
5376 break;
5377 case IPC_SET:
5378 perms = SEM__SETATTR;
5379 break;
5380 case IPC_STAT:
5381 case SEM_STAT:
5382 perms = SEM__GETATTR | SEM__ASSOCIATE;
5383 break;
5384 default:
5385 return 0;
5386 }
5387
Stephen Smalley6af963f2005-05-01 08:58:39 -07005388 err = ipc_has_perm(&sma->sem_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389 return err;
5390}
5391
5392static int selinux_sem_semop(struct sem_array *sma,
5393 struct sembuf *sops, unsigned nsops, int alter)
5394{
5395 u32 perms;
5396
5397 if (alter)
5398 perms = SEM__READ | SEM__WRITE;
5399 else
5400 perms = SEM__READ;
5401
Stephen Smalley6af963f2005-05-01 08:58:39 -07005402 return ipc_has_perm(&sma->sem_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403}
5404
5405static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
5406{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407 u32 av = 0;
5408
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409 av = 0;
5410 if (flag & S_IRUGO)
5411 av |= IPC__UNIX_READ;
5412 if (flag & S_IWUGO)
5413 av |= IPC__UNIX_WRITE;
5414
5415 if (av == 0)
5416 return 0;
5417
Stephen Smalley6af963f2005-05-01 08:58:39 -07005418 return ipc_has_perm(ipcp, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419}
5420
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02005421static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
5422{
5423 struct ipc_security_struct *isec = ipcp->security;
5424 *secid = isec->sid;
5425}
5426
Eric Paris828dfe12008-04-17 13:17:49 -04005427static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428{
5429 if (inode)
5430 inode_doinit_with_dentry(inode, dentry);
5431}
5432
5433static int selinux_getprocattr(struct task_struct *p,
Al Viro04ff9702007-03-12 16:17:58 +00005434 char *name, char **value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435{
David Howells275bb412008-11-14 10:39:19 +11005436 const struct task_security_struct *__tsec;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00005437 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438 int error;
Al Viro04ff9702007-03-12 16:17:58 +00005439 unsigned len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440
5441 if (current != p) {
David Howells3b11a1d2008-11-14 10:39:26 +11005442 error = current_has_perm(p, PROCESS__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443 if (error)
5444 return error;
5445 }
5446
David Howells275bb412008-11-14 10:39:19 +11005447 rcu_read_lock();
5448 __tsec = __task_cred(p)->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449
5450 if (!strcmp(name, "current"))
David Howells275bb412008-11-14 10:39:19 +11005451 sid = __tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452 else if (!strcmp(name, "prev"))
David Howells275bb412008-11-14 10:39:19 +11005453 sid = __tsec->osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454 else if (!strcmp(name, "exec"))
David Howells275bb412008-11-14 10:39:19 +11005455 sid = __tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 else if (!strcmp(name, "fscreate"))
David Howells275bb412008-11-14 10:39:19 +11005457 sid = __tsec->create_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07005458 else if (!strcmp(name, "keycreate"))
David Howells275bb412008-11-14 10:39:19 +11005459 sid = __tsec->keycreate_sid;
Eric Paris42c3e032006-06-26 00:26:03 -07005460 else if (!strcmp(name, "sockcreate"))
David Howells275bb412008-11-14 10:39:19 +11005461 sid = __tsec->sockcreate_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462 else
David Howells275bb412008-11-14 10:39:19 +11005463 goto invalid;
5464 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465
5466 if (!sid)
5467 return 0;
5468
Al Viro04ff9702007-03-12 16:17:58 +00005469 error = security_sid_to_context(sid, value, &len);
5470 if (error)
5471 return error;
5472 return len;
David Howells275bb412008-11-14 10:39:19 +11005473
5474invalid:
5475 rcu_read_unlock();
5476 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477}
5478
5479static int selinux_setprocattr(struct task_struct *p,
5480 char *name, void *value, size_t size)
5481{
5482 struct task_security_struct *tsec;
Roland McGrath03563572008-03-26 15:46:39 -07005483 struct task_struct *tracer;
David Howellsd84f4f92008-11-14 10:39:23 +11005484 struct cred *new;
5485 u32 sid = 0, ptsid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486 int error;
5487 char *str = value;
5488
5489 if (current != p) {
5490 /* SELinux only allows a process to change its own
5491 security attributes. */
5492 return -EACCES;
5493 }
5494
5495 /*
5496 * Basic control over ability to set these attributes at all.
5497 * current == p, but we'll pass them separately in case the
5498 * above restriction is ever removed.
5499 */
5500 if (!strcmp(name, "exec"))
David Howells3b11a1d2008-11-14 10:39:26 +11005501 error = current_has_perm(p, PROCESS__SETEXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 else if (!strcmp(name, "fscreate"))
David Howells3b11a1d2008-11-14 10:39:26 +11005503 error = current_has_perm(p, PROCESS__SETFSCREATE);
Michael LeMay4eb582c2006-06-26 00:24:57 -07005504 else if (!strcmp(name, "keycreate"))
David Howells3b11a1d2008-11-14 10:39:26 +11005505 error = current_has_perm(p, PROCESS__SETKEYCREATE);
Eric Paris42c3e032006-06-26 00:26:03 -07005506 else if (!strcmp(name, "sockcreate"))
David Howells3b11a1d2008-11-14 10:39:26 +11005507 error = current_has_perm(p, PROCESS__SETSOCKCREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508 else if (!strcmp(name, "current"))
David Howells3b11a1d2008-11-14 10:39:26 +11005509 error = current_has_perm(p, PROCESS__SETCURRENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510 else
5511 error = -EINVAL;
5512 if (error)
5513 return error;
5514
5515 /* Obtain a SID for the context, if one was specified. */
5516 if (size && str[1] && str[1] != '\n') {
5517 if (str[size-1] == '\n') {
5518 str[size-1] = 0;
5519 size--;
5520 }
5521 error = security_context_to_sid(value, size, &sid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04005522 if (error == -EINVAL && !strcmp(name, "fscreate")) {
Eric Parisd6ea83e2012-04-04 13:45:49 -04005523 if (!capable(CAP_MAC_ADMIN)) {
5524 struct audit_buffer *ab;
5525 size_t audit_size;
5526
5527 /* We strip a nul only if it is at the end, otherwise the
5528 * context contains a nul and we should audit that */
5529 if (str[size - 1] == '\0')
5530 audit_size = size - 1;
5531 else
5532 audit_size = size;
5533 ab = audit_log_start(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR);
5534 audit_log_format(ab, "op=fscreate invalid_context=");
5535 audit_log_n_untrustedstring(ab, value, audit_size);
5536 audit_log_end(ab);
5537
Stephen Smalley12b29f32008-05-07 13:03:20 -04005538 return error;
Eric Parisd6ea83e2012-04-04 13:45:49 -04005539 }
Stephen Smalley12b29f32008-05-07 13:03:20 -04005540 error = security_context_to_sid_force(value, size,
5541 &sid);
5542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543 if (error)
5544 return error;
5545 }
5546
David Howellsd84f4f92008-11-14 10:39:23 +11005547 new = prepare_creds();
5548 if (!new)
5549 return -ENOMEM;
5550
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551 /* Permission checking based on the specified context is
5552 performed during the actual operation (execve,
5553 open/mkdir/...), when we know the full context of the
David Howellsd84f4f92008-11-14 10:39:23 +11005554 operation. See selinux_bprm_set_creds for the execve
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555 checks and may_create for the file creation checks. The
5556 operation will then fail if the context is not permitted. */
David Howellsd84f4f92008-11-14 10:39:23 +11005557 tsec = new->security;
5558 if (!strcmp(name, "exec")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559 tsec->exec_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005560 } else if (!strcmp(name, "fscreate")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561 tsec->create_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005562 } else if (!strcmp(name, "keycreate")) {
Michael LeMay4eb582c2006-06-26 00:24:57 -07005563 error = may_create_key(sid, p);
5564 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11005565 goto abort_change;
Michael LeMay4eb582c2006-06-26 00:24:57 -07005566 tsec->keycreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005567 } else if (!strcmp(name, "sockcreate")) {
Eric Paris42c3e032006-06-26 00:26:03 -07005568 tsec->sockcreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005569 } else if (!strcmp(name, "current")) {
5570 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571 if (sid == 0)
David Howellsd84f4f92008-11-14 10:39:23 +11005572 goto abort_change;
KaiGai Koheid9250de2008-08-28 16:35:57 +09005573
David Howellsd84f4f92008-11-14 10:39:23 +11005574 /* Only allow single threaded processes to change context */
5575 error = -EPERM;
Oleg Nesterov5bb459b2009-07-10 03:48:23 +02005576 if (!current_is_single_threaded()) {
David Howellsd84f4f92008-11-14 10:39:23 +11005577 error = security_bounded_transition(tsec->sid, sid);
5578 if (error)
5579 goto abort_change;
Eric Paris828dfe12008-04-17 13:17:49 -04005580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581
5582 /* Check permissions for the transition. */
5583 error = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS,
Eric Paris828dfe12008-04-17 13:17:49 -04005584 PROCESS__DYNTRANSITION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11005586 goto abort_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587
5588 /* Check for ptracing, and update the task SID if ok.
5589 Otherwise, leave SID unchanged and fail. */
David Howellsd84f4f92008-11-14 10:39:23 +11005590 ptsid = 0;
Oleg Nesterovc0c14392013-12-23 17:45:01 -05005591 rcu_read_lock();
Tejun Heo06d98472011-06-17 16:50:40 +02005592 tracer = ptrace_parent(p);
David Howellsd84f4f92008-11-14 10:39:23 +11005593 if (tracer)
5594 ptsid = task_sid(tracer);
Oleg Nesterovc0c14392013-12-23 17:45:01 -05005595 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596
David Howellsd84f4f92008-11-14 10:39:23 +11005597 if (tracer) {
5598 error = avc_has_perm(ptsid, sid, SECCLASS_PROCESS,
5599 PROCESS__PTRACE, NULL);
5600 if (error)
5601 goto abort_change;
5602 }
5603
5604 tsec->sid = sid;
5605 } else {
5606 error = -EINVAL;
5607 goto abort_change;
5608 }
5609
5610 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611 return size;
David Howellsd84f4f92008-11-14 10:39:23 +11005612
5613abort_change:
5614 abort_creds(new);
5615 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616}
5617
David Quigley746df9b2013-05-22 12:50:35 -04005618static int selinux_ismaclabel(const char *name)
5619{
5620 return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0);
5621}
5622
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005623static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
5624{
5625 return security_sid_to_context(secid, secdata, seclen);
5626}
5627
David Howells7bf570d2008-04-29 20:52:51 +01005628static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
David Howells63cb3442008-01-15 23:47:35 +00005629{
5630 return security_context_to_sid(secdata, seclen, secid);
5631}
5632
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005633static void selinux_release_secctx(char *secdata, u32 seclen)
5634{
Paul Moore088999e2007-08-01 11:12:58 -04005635 kfree(secdata);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005636}
5637
David P. Quigley1ee65e32009-09-03 14:25:57 -04005638/*
5639 * called with inode->i_mutex locked
5640 */
5641static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
5642{
5643 return selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, ctx, ctxlen, 0);
5644}
5645
5646/*
5647 * called with inode->i_mutex locked
5648 */
5649static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
5650{
5651 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0);
5652}
5653
5654static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
5655{
5656 int len = 0;
5657 len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX,
5658 ctx, true);
5659 if (len < 0)
5660 return len;
5661 *ctxlen = len;
5662 return 0;
5663}
Michael LeMayd7200242006-06-22 14:47:17 -07005664#ifdef CONFIG_KEYS
5665
David Howellsd84f4f92008-11-14 10:39:23 +11005666static int selinux_key_alloc(struct key *k, const struct cred *cred,
David Howells7e047ef2006-06-26 00:24:50 -07005667 unsigned long flags)
Michael LeMayd7200242006-06-22 14:47:17 -07005668{
David Howellsd84f4f92008-11-14 10:39:23 +11005669 const struct task_security_struct *tsec;
Michael LeMayd7200242006-06-22 14:47:17 -07005670 struct key_security_struct *ksec;
5671
5672 ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
5673 if (!ksec)
5674 return -ENOMEM;
5675
David Howellsd84f4f92008-11-14 10:39:23 +11005676 tsec = cred->security;
5677 if (tsec->keycreate_sid)
5678 ksec->sid = tsec->keycreate_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07005679 else
David Howellsd84f4f92008-11-14 10:39:23 +11005680 ksec->sid = tsec->sid;
Michael LeMayd7200242006-06-22 14:47:17 -07005681
David Howells275bb412008-11-14 10:39:19 +11005682 k->security = ksec;
Michael LeMayd7200242006-06-22 14:47:17 -07005683 return 0;
5684}
5685
5686static void selinux_key_free(struct key *k)
5687{
5688 struct key_security_struct *ksec = k->security;
5689
5690 k->security = NULL;
5691 kfree(ksec);
5692}
5693
5694static int selinux_key_permission(key_ref_t key_ref,
David Howellsd84f4f92008-11-14 10:39:23 +11005695 const struct cred *cred,
5696 key_perm_t perm)
Michael LeMayd7200242006-06-22 14:47:17 -07005697{
5698 struct key *key;
Michael LeMayd7200242006-06-22 14:47:17 -07005699 struct key_security_struct *ksec;
David Howells275bb412008-11-14 10:39:19 +11005700 u32 sid;
Michael LeMayd7200242006-06-22 14:47:17 -07005701
5702 /* if no specific permissions are requested, we skip the
5703 permission check. No serious, additional covert channels
5704 appear to be created. */
5705 if (perm == 0)
5706 return 0;
5707
David Howellsd84f4f92008-11-14 10:39:23 +11005708 sid = cred_sid(cred);
David Howells275bb412008-11-14 10:39:19 +11005709
5710 key = key_ref_to_ptr(key_ref);
5711 ksec = key->security;
5712
5713 return avc_has_perm(sid, ksec->sid, SECCLASS_KEY, perm, NULL);
Michael LeMayd7200242006-06-22 14:47:17 -07005714}
5715
David Howells70a5bb72008-04-29 01:01:26 -07005716static int selinux_key_getsecurity(struct key *key, char **_buffer)
5717{
5718 struct key_security_struct *ksec = key->security;
5719 char *context = NULL;
5720 unsigned len;
5721 int rc;
5722
5723 rc = security_sid_to_context(ksec->sid, &context, &len);
5724 if (!rc)
5725 rc = len;
5726 *_buffer = context;
5727 return rc;
5728}
5729
Michael LeMayd7200242006-06-22 14:47:17 -07005730#endif
5731
Linus Torvalds1da177e2005-04-16 15:20:36 -07005732static struct security_operations selinux_ops = {
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02005733 .name = "selinux",
5734
Ingo Molnar9e488582009-05-07 19:26:19 +10005735 .ptrace_access_check = selinux_ptrace_access_check,
David Howells5cd9c582008-08-14 11:37:28 +01005736 .ptrace_traceme = selinux_ptrace_traceme,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737 .capget = selinux_capget,
David Howellsd84f4f92008-11-14 10:39:23 +11005738 .capset = selinux_capset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739 .capable = selinux_capable,
5740 .quotactl = selinux_quotactl,
5741 .quota_on = selinux_quota_on,
5742 .syslog = selinux_syslog,
5743 .vm_enough_memory = selinux_vm_enough_memory,
5744
5745 .netlink_send = selinux_netlink_send,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746
David Howellsa6f76f22008-11-14 10:39:24 +11005747 .bprm_set_creds = selinux_bprm_set_creds,
David Howellsa6f76f22008-11-14 10:39:24 +11005748 .bprm_committing_creds = selinux_bprm_committing_creds,
5749 .bprm_committed_creds = selinux_bprm_committed_creds,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750 .bprm_secureexec = selinux_bprm_secureexec,
5751
5752 .sb_alloc_security = selinux_sb_alloc_security,
5753 .sb_free_security = selinux_sb_free_security,
5754 .sb_copy_data = selinux_sb_copy_data,
Eric Paris026eb162011-03-03 16:09:14 -05005755 .sb_remount = selinux_sb_remount,
Eric Paris828dfe12008-04-17 13:17:49 -04005756 .sb_kern_mount = selinux_sb_kern_mount,
Eric Paris2069f452008-07-04 09:47:13 +10005757 .sb_show_options = selinux_sb_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758 .sb_statfs = selinux_sb_statfs,
5759 .sb_mount = selinux_mount,
5760 .sb_umount = selinux_umount,
Eric Parisc9180a52007-11-30 13:00:35 -05005761 .sb_set_mnt_opts = selinux_set_mnt_opts,
Eric Paris828dfe12008-04-17 13:17:49 -04005762 .sb_clone_mnt_opts = selinux_sb_clone_mnt_opts,
Eric Parise0007522008-03-05 10:31:54 -05005763 .sb_parse_opts_str = selinux_parse_opts_str,
5764
David Quigleyd47be3d2013-05-22 12:50:34 -04005765 .dentry_init_security = selinux_dentry_init_security,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005766
5767 .inode_alloc_security = selinux_inode_alloc_security,
5768 .inode_free_security = selinux_inode_free_security,
Stephen Smalley5e41ff92005-09-09 13:01:35 -07005769 .inode_init_security = selinux_inode_init_security,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770 .inode_create = selinux_inode_create,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771 .inode_link = selinux_inode_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772 .inode_unlink = selinux_inode_unlink,
5773 .inode_symlink = selinux_inode_symlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774 .inode_mkdir = selinux_inode_mkdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775 .inode_rmdir = selinux_inode_rmdir,
5776 .inode_mknod = selinux_inode_mknod,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777 .inode_rename = selinux_inode_rename,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778 .inode_readlink = selinux_inode_readlink,
5779 .inode_follow_link = selinux_inode_follow_link,
5780 .inode_permission = selinux_inode_permission,
5781 .inode_setattr = selinux_inode_setattr,
5782 .inode_getattr = selinux_inode_getattr,
5783 .inode_setxattr = selinux_inode_setxattr,
5784 .inode_post_setxattr = selinux_inode_post_setxattr,
5785 .inode_getxattr = selinux_inode_getxattr,
5786 .inode_listxattr = selinux_inode_listxattr,
5787 .inode_removexattr = selinux_inode_removexattr,
Eric Paris828dfe12008-04-17 13:17:49 -04005788 .inode_getsecurity = selinux_inode_getsecurity,
5789 .inode_setsecurity = selinux_inode_setsecurity,
5790 .inode_listsecurity = selinux_inode_listsecurity,
Eric Parisf5269712008-05-14 11:27:45 -04005791 .inode_getsecid = selinux_inode_getsecid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792
5793 .file_permission = selinux_file_permission,
5794 .file_alloc_security = selinux_file_alloc_security,
5795 .file_free_security = selinux_file_free_security,
5796 .file_ioctl = selinux_file_ioctl,
Al Viroe5467852012-05-30 13:30:51 -04005797 .mmap_file = selinux_mmap_file,
5798 .mmap_addr = selinux_mmap_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799 .file_mprotect = selinux_file_mprotect,
5800 .file_lock = selinux_file_lock,
5801 .file_fcntl = selinux_file_fcntl,
5802 .file_set_fowner = selinux_file_set_fowner,
5803 .file_send_sigiotask = selinux_file_send_sigiotask,
5804 .file_receive = selinux_file_receive,
5805
Eric Paris83d49852012-04-04 13:45:40 -04005806 .file_open = selinux_file_open,
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09005807
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808 .task_create = selinux_task_create,
David Howellsee18d642009-09-02 09:14:21 +01005809 .cred_alloc_blank = selinux_cred_alloc_blank,
David Howellsf1752ee2008-11-14 10:39:17 +11005810 .cred_free = selinux_cred_free,
David Howellsd84f4f92008-11-14 10:39:23 +11005811 .cred_prepare = selinux_cred_prepare,
David Howellsee18d642009-09-02 09:14:21 +01005812 .cred_transfer = selinux_cred_transfer,
David Howells3a3b7ce2008-11-14 10:39:28 +11005813 .kernel_act_as = selinux_kernel_act_as,
5814 .kernel_create_files_as = selinux_kernel_create_files_as,
Eric Paris25354c42009-08-13 09:45:03 -04005815 .kernel_module_request = selinux_kernel_module_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005816 .task_setpgid = selinux_task_setpgid,
5817 .task_getpgid = selinux_task_getpgid,
Eric Paris828dfe12008-04-17 13:17:49 -04005818 .task_getsid = selinux_task_getsid,
David Quigleyf9008e42006-06-30 01:55:46 -07005819 .task_getsecid = selinux_task_getsecid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820 .task_setnice = selinux_task_setnice,
James Morris03e68062006-06-23 02:03:58 -07005821 .task_setioprio = selinux_task_setioprio,
David Quigleya1836a42006-06-30 01:55:49 -07005822 .task_getioprio = selinux_task_getioprio,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005823 .task_setrlimit = selinux_task_setrlimit,
5824 .task_setscheduler = selinux_task_setscheduler,
5825 .task_getscheduler = selinux_task_getscheduler,
David Quigley35601542006-06-23 02:04:01 -07005826 .task_movememory = selinux_task_movememory,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827 .task_kill = selinux_task_kill,
5828 .task_wait = selinux_task_wait,
Eric Paris828dfe12008-04-17 13:17:49 -04005829 .task_to_inode = selinux_task_to_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005830
5831 .ipc_permission = selinux_ipc_permission,
Eric Parisf5269712008-05-14 11:27:45 -04005832 .ipc_getsecid = selinux_ipc_getsecid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833
5834 .msg_msg_alloc_security = selinux_msg_msg_alloc_security,
5835 .msg_msg_free_security = selinux_msg_msg_free_security,
5836
5837 .msg_queue_alloc_security = selinux_msg_queue_alloc_security,
5838 .msg_queue_free_security = selinux_msg_queue_free_security,
5839 .msg_queue_associate = selinux_msg_queue_associate,
5840 .msg_queue_msgctl = selinux_msg_queue_msgctl,
5841 .msg_queue_msgsnd = selinux_msg_queue_msgsnd,
5842 .msg_queue_msgrcv = selinux_msg_queue_msgrcv,
5843
5844 .shm_alloc_security = selinux_shm_alloc_security,
5845 .shm_free_security = selinux_shm_free_security,
5846 .shm_associate = selinux_shm_associate,
5847 .shm_shmctl = selinux_shm_shmctl,
5848 .shm_shmat = selinux_shm_shmat,
5849
Eric Paris828dfe12008-04-17 13:17:49 -04005850 .sem_alloc_security = selinux_sem_alloc_security,
5851 .sem_free_security = selinux_sem_free_security,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005852 .sem_associate = selinux_sem_associate,
5853 .sem_semctl = selinux_sem_semctl,
5854 .sem_semop = selinux_sem_semop,
5855
Eric Paris828dfe12008-04-17 13:17:49 -04005856 .d_instantiate = selinux_d_instantiate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857
Eric Paris828dfe12008-04-17 13:17:49 -04005858 .getprocattr = selinux_getprocattr,
5859 .setprocattr = selinux_setprocattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860
David Quigley746df9b2013-05-22 12:50:35 -04005861 .ismaclabel = selinux_ismaclabel,
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005862 .secid_to_secctx = selinux_secid_to_secctx,
David Howells63cb3442008-01-15 23:47:35 +00005863 .secctx_to_secid = selinux_secctx_to_secid,
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005864 .release_secctx = selinux_release_secctx,
David P. Quigley1ee65e32009-09-03 14:25:57 -04005865 .inode_notifysecctx = selinux_inode_notifysecctx,
5866 .inode_setsecctx = selinux_inode_setsecctx,
5867 .inode_getsecctx = selinux_inode_getsecctx,
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005868
Eric Paris828dfe12008-04-17 13:17:49 -04005869 .unix_stream_connect = selinux_socket_unix_stream_connect,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870 .unix_may_send = selinux_socket_unix_may_send,
5871
5872 .socket_create = selinux_socket_create,
5873 .socket_post_create = selinux_socket_post_create,
5874 .socket_bind = selinux_socket_bind,
5875 .socket_connect = selinux_socket_connect,
5876 .socket_listen = selinux_socket_listen,
5877 .socket_accept = selinux_socket_accept,
5878 .socket_sendmsg = selinux_socket_sendmsg,
5879 .socket_recvmsg = selinux_socket_recvmsg,
5880 .socket_getsockname = selinux_socket_getsockname,
5881 .socket_getpeername = selinux_socket_getpeername,
5882 .socket_getsockopt = selinux_socket_getsockopt,
5883 .socket_setsockopt = selinux_socket_setsockopt,
5884 .socket_shutdown = selinux_socket_shutdown,
5885 .socket_sock_rcv_skb = selinux_socket_sock_rcv_skb,
Catherine Zhang2c7946a2006-03-20 22:41:23 -08005886 .socket_getpeersec_stream = selinux_socket_getpeersec_stream,
5887 .socket_getpeersec_dgram = selinux_socket_getpeersec_dgram,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888 .sk_alloc_security = selinux_sk_alloc_security,
5889 .sk_free_security = selinux_sk_free_security,
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005890 .sk_clone_security = selinux_sk_clone_security,
Eric Paris828dfe12008-04-17 13:17:49 -04005891 .sk_getsecid = selinux_sk_getsecid,
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005892 .sock_graft = selinux_sock_graft,
5893 .inet_conn_request = selinux_inet_conn_request,
5894 .inet_csk_clone = selinux_inet_csk_clone,
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005895 .inet_conn_established = selinux_inet_conn_established,
Eric Paris2606fd12010-10-13 16:24:41 -04005896 .secmark_relabel_packet = selinux_secmark_relabel_packet,
5897 .secmark_refcount_inc = selinux_secmark_refcount_inc,
5898 .secmark_refcount_dec = selinux_secmark_refcount_dec,
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005899 .req_classify_flow = selinux_req_classify_flow,
Paul Moore5dbbaf22013-01-14 07:12:19 +00005900 .tun_dev_alloc_security = selinux_tun_dev_alloc_security,
5901 .tun_dev_free_security = selinux_tun_dev_free_security,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005902 .tun_dev_create = selinux_tun_dev_create,
Paul Moore5dbbaf22013-01-14 07:12:19 +00005903 .tun_dev_attach_queue = selinux_tun_dev_attach_queue,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005904 .tun_dev_attach = selinux_tun_dev_attach,
Paul Moore5dbbaf22013-01-14 07:12:19 +00005905 .tun_dev_open = selinux_tun_dev_open,
Eric Dumazetca10b9e2013-04-08 17:58:11 +00005906 .skb_owned_by = selinux_skb_owned_by,
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005907
5908#ifdef CONFIG_SECURITY_NETWORK_XFRM
5909 .xfrm_policy_alloc_security = selinux_xfrm_policy_alloc,
5910 .xfrm_policy_clone_security = selinux_xfrm_policy_clone,
5911 .xfrm_policy_free_security = selinux_xfrm_policy_free,
Catherine Zhangc8c05a82006-06-08 23:39:49 -07005912 .xfrm_policy_delete_security = selinux_xfrm_policy_delete,
Paul Moore2e5aa862013-07-23 17:38:38 -04005913 .xfrm_state_alloc = selinux_xfrm_state_alloc,
5914 .xfrm_state_alloc_acquire = selinux_xfrm_state_alloc_acquire,
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005915 .xfrm_state_free_security = selinux_xfrm_state_free,
Catherine Zhangc8c05a82006-06-08 23:39:49 -07005916 .xfrm_state_delete_security = selinux_xfrm_state_delete,
Eric Paris828dfe12008-04-17 13:17:49 -04005917 .xfrm_policy_lookup = selinux_xfrm_policy_lookup,
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07005918 .xfrm_state_pol_flow_match = selinux_xfrm_state_pol_flow_match,
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07005919 .xfrm_decode_session = selinux_xfrm_decode_session,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005920#endif
Michael LeMayd7200242006-06-22 14:47:17 -07005921
5922#ifdef CONFIG_KEYS
Eric Paris828dfe12008-04-17 13:17:49 -04005923 .key_alloc = selinux_key_alloc,
5924 .key_free = selinux_key_free,
5925 .key_permission = selinux_key_permission,
David Howells70a5bb72008-04-29 01:01:26 -07005926 .key_getsecurity = selinux_key_getsecurity,
Michael LeMayd7200242006-06-22 14:47:17 -07005927#endif
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02005928
5929#ifdef CONFIG_AUDIT
5930 .audit_rule_init = selinux_audit_rule_init,
5931 .audit_rule_known = selinux_audit_rule_known,
5932 .audit_rule_match = selinux_audit_rule_match,
5933 .audit_rule_free = selinux_audit_rule_free,
5934#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005935};
5936
5937static __init int selinux_init(void)
5938{
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02005939 if (!security_module_enable(&selinux_ops)) {
5940 selinux_enabled = 0;
5941 return 0;
5942 }
5943
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944 if (!selinux_enabled) {
5945 printk(KERN_INFO "SELinux: Disabled at boot.\n");
5946 return 0;
5947 }
5948
5949 printk(KERN_INFO "SELinux: Initializing.\n");
5950
5951 /* Set the security state for the initial task. */
David Howellsd84f4f92008-11-14 10:39:23 +11005952 cred_init_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005953
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04005954 default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
5955
James Morris7cae7e22006-03-22 00:09:22 -08005956 sel_inode_cache = kmem_cache_create("selinux_inode_security",
5957 sizeof(struct inode_security_struct),
Paul Mundt20c2df82007-07-20 10:11:58 +09005958 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005959 avc_init();
5960
Eric Paris828dfe12008-04-17 13:17:49 -04005961 if (register_security(&selinux_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005962 panic("SELinux: Unable to register with kernel.\n");
5963
Eric Paris828dfe12008-04-17 13:17:49 -04005964 if (selinux_enforcing)
Eric Parisfadcdb42007-02-22 18:11:31 -05005965 printk(KERN_DEBUG "SELinux: Starting in enforcing mode\n");
Eric Paris828dfe12008-04-17 13:17:49 -04005966 else
Eric Parisfadcdb42007-02-22 18:11:31 -05005967 printk(KERN_DEBUG "SELinux: Starting in permissive mode\n");
Michael LeMayd7200242006-06-22 14:47:17 -07005968
Linus Torvalds1da177e2005-04-16 15:20:36 -07005969 return 0;
5970}
5971
Al Viroe8c26252010-03-23 06:36:54 -04005972static void delayed_superblock_init(struct super_block *sb, void *unused)
5973{
5974 superblock_doinit(sb, NULL);
5975}
5976
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977void selinux_complete_init(void)
5978{
Eric Parisfadcdb42007-02-22 18:11:31 -05005979 printk(KERN_DEBUG "SELinux: Completing initialization.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005980
5981 /* Set up any superblocks initialized prior to the policy load. */
Eric Parisfadcdb42007-02-22 18:11:31 -05005982 printk(KERN_DEBUG "SELinux: Setting up existing superblocks.\n");
Al Viroe8c26252010-03-23 06:36:54 -04005983 iterate_supers(delayed_superblock_init, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984}
5985
5986/* SELinux requires early initialization in order to label
5987 all processes and objects when they are created. */
5988security_initcall(selinux_init);
5989
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08005990#if defined(CONFIG_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991
Paul Mooreeffad8d2008-01-29 08:49:27 -05005992static struct nf_hook_ops selinux_ipv4_ops[] = {
5993 {
5994 .hook = selinux_ipv4_postroute,
5995 .owner = THIS_MODULE,
Alban Crequy2597a832012-05-14 03:56:39 +00005996 .pf = NFPROTO_IPV4,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005997 .hooknum = NF_INET_POST_ROUTING,
5998 .priority = NF_IP_PRI_SELINUX_LAST,
5999 },
6000 {
6001 .hook = selinux_ipv4_forward,
6002 .owner = THIS_MODULE,
Alban Crequy2597a832012-05-14 03:56:39 +00006003 .pf = NFPROTO_IPV4,
Paul Mooreeffad8d2008-01-29 08:49:27 -05006004 .hooknum = NF_INET_FORWARD,
6005 .priority = NF_IP_PRI_SELINUX_FIRST,
Paul Moore948bf852008-10-10 10:16:32 -04006006 },
6007 {
6008 .hook = selinux_ipv4_output,
6009 .owner = THIS_MODULE,
Alban Crequy2597a832012-05-14 03:56:39 +00006010 .pf = NFPROTO_IPV4,
Paul Moore948bf852008-10-10 10:16:32 -04006011 .hooknum = NF_INET_LOCAL_OUT,
6012 .priority = NF_IP_PRI_SELINUX_FIRST,
Paul Mooreeffad8d2008-01-29 08:49:27 -05006013 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014};
6015
6016#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
6017
Paul Mooreeffad8d2008-01-29 08:49:27 -05006018static struct nf_hook_ops selinux_ipv6_ops[] = {
6019 {
6020 .hook = selinux_ipv6_postroute,
6021 .owner = THIS_MODULE,
Alban Crequy2597a832012-05-14 03:56:39 +00006022 .pf = NFPROTO_IPV6,
Paul Mooreeffad8d2008-01-29 08:49:27 -05006023 .hooknum = NF_INET_POST_ROUTING,
6024 .priority = NF_IP6_PRI_SELINUX_LAST,
6025 },
6026 {
6027 .hook = selinux_ipv6_forward,
6028 .owner = THIS_MODULE,
Alban Crequy2597a832012-05-14 03:56:39 +00006029 .pf = NFPROTO_IPV6,
Paul Mooreeffad8d2008-01-29 08:49:27 -05006030 .hooknum = NF_INET_FORWARD,
6031 .priority = NF_IP6_PRI_SELINUX_FIRST,
6032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033};
6034
6035#endif /* IPV6 */
6036
6037static int __init selinux_nf_ip_init(void)
6038{
6039 int err = 0;
6040
6041 if (!selinux_enabled)
6042 goto out;
Eric Parisfadcdb42007-02-22 18:11:31 -05006043
6044 printk(KERN_DEBUG "SELinux: Registering netfilter hooks\n");
6045
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07006046 err = nf_register_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops));
6047 if (err)
6048 panic("SELinux: nf_register_hooks for IPv4: error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049
6050#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07006051 err = nf_register_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops));
6052 if (err)
6053 panic("SELinux: nf_register_hooks for IPv6: error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006054#endif /* IPV6 */
Trent Jaegerd28d1e02005-12-13 23:12:40 -08006055
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056out:
6057 return err;
6058}
6059
6060__initcall(selinux_nf_ip_init);
6061
6062#ifdef CONFIG_SECURITY_SELINUX_DISABLE
6063static void selinux_nf_ip_exit(void)
6064{
Eric Parisfadcdb42007-02-22 18:11:31 -05006065 printk(KERN_DEBUG "SELinux: Unregistering netfilter hooks\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006066
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07006067 nf_unregister_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07006069 nf_unregister_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070#endif /* IPV6 */
6071}
6072#endif
6073
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08006074#else /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075
6076#ifdef CONFIG_SECURITY_SELINUX_DISABLE
6077#define selinux_nf_ip_exit()
6078#endif
6079
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08006080#endif /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081
6082#ifdef CONFIG_SECURITY_SELINUX_DISABLE
Eric Paris828dfe12008-04-17 13:17:49 -04006083static int selinux_disabled;
6084
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085int selinux_disable(void)
6086{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006087 if (ss_initialized) {
6088 /* Not permitted after initial policy load. */
6089 return -EINVAL;
6090 }
6091
6092 if (selinux_disabled) {
6093 /* Only do this once. */
6094 return -EINVAL;
6095 }
6096
6097 printk(KERN_INFO "SELinux: Disabled at runtime.\n");
6098
6099 selinux_disabled = 1;
Stephen Smalley30d55282006-05-03 10:52:36 -04006100 selinux_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101
wzt.wzt@gmail.com189b3b12010-02-23 23:15:28 +08006102 reset_security_ops();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006103
Eric Parisaf8ff042009-09-20 21:23:01 -04006104 /* Try to destroy the avc node cache */
6105 avc_disable();
6106
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107 /* Unregister netfilter hooks. */
6108 selinux_nf_ip_exit();
6109
6110 /* Unregister selinuxfs. */
6111 exit_sel_fs();
6112
6113 return 0;
6114}
6115#endif