blob: 4fbf2c5f26ce5f3abccf283b108d62f6bdc778a8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * NSA Security-Enhanced Linux (SELinux) security module
3 *
4 * This file contains the SELinux hook function implementations.
5 *
6 * Authors: Stephen Smalley, <sds@epoch.ncsc.mil>
Eric Paris828dfe12008-04-17 13:17:49 -04007 * Chris Vance, <cvance@nai.com>
8 * Wayne Salamon, <wsalamon@nai.com>
9 * James Morris <jmorris@redhat.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * Copyright (C) 2001,2002 Networks Associates Technology, Inc.
Eric Paris2069f452008-07-04 09:47:13 +100012 * Copyright (C) 2003-2008 Red Hat, Inc., James Morris <jmorris@redhat.com>
13 * Eric Paris <eparis@redhat.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
Eric Paris828dfe12008-04-17 13:17:49 -040015 * <dgoeddel@trustedcs.com>
Paul Mooreed6d76e2009-08-28 18:12:49 -040016 * Copyright (C) 2006, 2007, 2009 Hewlett-Packard Development Company, L.P.
Paul Moore82c21bf2011-08-01 11:10:33 +000017 * Paul Moore <paul@paul-moore.com>
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +090018 * Copyright (C) 2007 Hitachi Software Engineering Co., Ltd.
Eric Paris828dfe12008-04-17 13:17:49 -040019 * Yuichi Nakamura <ynakam@hitachisoft.jp>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 *
21 * This program is free software; you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License version 2,
Eric Paris828dfe12008-04-17 13:17:49 -040023 * as published by the Free Software Foundation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 */
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/init.h>
Eric Paris0b24dcb2011-02-25 15:39:20 -050027#include <linux/kd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/kernel.h>
Roland McGrath0d094ef2008-07-25 19:45:49 -070029#include <linux/tracehook.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/errno.h>
31#include <linux/sched.h>
32#include <linux/security.h>
33#include <linux/xattr.h>
34#include <linux/capability.h>
35#include <linux/unistd.h>
36#include <linux/mm.h>
37#include <linux/mman.h>
38#include <linux/slab.h>
39#include <linux/pagemap.h>
Eric Paris0b24dcb2011-02-25 15:39:20 -050040#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/swap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/spinlock.h>
43#include <linux/syscalls.h>
Eric Paris2a7dba32011-02-01 11:05:39 -050044#include <linux/dcache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/file.h>
Al Viro9f3acc32008-04-24 07:44:08 -040046#include <linux/fdtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/namei.h>
48#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/netfilter_ipv4.h>
50#include <linux/netfilter_ipv6.h>
51#include <linux/tty.h>
52#include <net/icmp.h>
Stephen Hemminger227b60f2007-10-10 17:30:46 -070053#include <net/ip.h> /* for local_port_range[] */
Eric Dumazetca10b9e2013-04-08 17:58:11 +000054#include <net/sock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <net/tcp.h> /* struct or_callable used in sock_rcv_skb */
Paul Moore220deb92008-01-29 08:38:23 -050056#include <net/net_namespace.h>
Paul Moored621d352008-01-29 08:43:36 -050057#include <net/netlabel.h>
Eric Parisf5269712008-05-14 11:27:45 -040058#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <asm/ioctls.h>
Arun Sharma600634972011-07-26 16:09:06 -070060#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/bitops.h>
62#include <linux/interrupt.h>
63#include <linux/netdevice.h> /* for network interface checks */
Hong zhi guo77954982013-03-27 06:49:35 +000064#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <linux/tcp.h>
66#include <linux/udp.h>
James Morris2ee92d42006-11-13 16:09:01 -080067#include <linux/dccp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/quota.h>
69#include <linux/un.h> /* for Unix socket types */
70#include <net/af_unix.h> /* for Unix socket types */
71#include <linux/parser.h>
72#include <linux/nfs_mount.h>
73#include <net/ipv6.h>
74#include <linux/hugetlb.h>
75#include <linux/personality.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include <linux/audit.h>
Eric Paris6931dfc2005-06-30 02:58:51 -070077#include <linux/string.h>
Catherine Zhang877ce7c2006-06-29 12:27:47 -070078#include <linux/selinux.h>
Eric Paris23970742006-09-25 23:32:01 -070079#include <linux/mutex.h>
Frank Mayharf06febc2008-09-12 09:54:39 -070080#include <linux/posix-timers.h>
Kees Cook00234592010-02-03 15:36:43 -080081#include <linux/syslog.h>
Serge E. Hallyn34867402011-03-23 16:43:17 -070082#include <linux/user_namespace.h>
Paul Gortmaker44fc7ea2011-05-26 20:52:10 -040083#include <linux/export.h>
Al Viro40401532012-02-13 03:58:52 +000084#include <linux/msg.h>
85#include <linux/shm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87#include "avc.h"
88#include "objsec.h"
89#include "netif.h"
Paul Moore224dfbd2008-01-29 08:38:13 -050090#include "netnode.h"
Paul Moore3e1121722008-04-10 10:48:14 -040091#include "netport.h"
Trent Jaegerd28d1e02005-12-13 23:12:40 -080092#include "xfrm.h"
Paul Moorec60475b2007-02-28 15:14:23 -050093#include "netlabel.h"
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +020094#include "audit.h"
James Morris7b98a582011-08-30 12:52:32 +100095#include "avc_ss.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
James Morris20510f22007-10-16 23:31:32 -070097extern struct security_operations *security_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Paul Moored621d352008-01-29 08:43:36 -050099/* SECMARK reference count */
James Morris56a4ca92011-08-17 11:08:43 +1000100static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0);
Paul Moored621d352008-01-29 08:43:36 -0500101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#ifdef CONFIG_SECURITY_SELINUX_DEVELOP
Eric Paris828dfe12008-04-17 13:17:49 -0400103int selinux_enforcing;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105static int __init enforcing_setup(char *str)
106{
Eric Parisf5269712008-05-14 11:27:45 -0400107 unsigned long enforcing;
108 if (!strict_strtoul(str, 0, &enforcing))
109 selinux_enforcing = enforcing ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 return 1;
111}
112__setup("enforcing=", enforcing_setup);
113#endif
114
115#ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM
116int selinux_enabled = CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE;
117
118static int __init selinux_enabled_setup(char *str)
119{
Eric Parisf5269712008-05-14 11:27:45 -0400120 unsigned long enabled;
121 if (!strict_strtoul(str, 0, &enabled))
122 selinux_enabled = enabled ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 return 1;
124}
125__setup("selinux=", selinux_enabled_setup);
Stephen Smalley30d55282006-05-03 10:52:36 -0400126#else
127int selinux_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128#endif
129
Christoph Lametere18b8902006-12-06 20:33:20 -0800130static struct kmem_cache *sel_inode_cache;
James Morris7cae7e22006-03-22 00:09:22 -0800131
Paul Moored621d352008-01-29 08:43:36 -0500132/**
133 * selinux_secmark_enabled - Check to see if SECMARK is currently enabled
134 *
135 * Description:
136 * This function checks the SECMARK reference counter to see if any SECMARK
137 * targets are currently configured, if the reference counter is greater than
138 * zero SECMARK is considered to be enabled. Returns true (1) if SECMARK is
Chris PeBenito2be4d742013-05-03 09:05:39 -0400139 * enabled, false (0) if SECMARK is disabled. If the always_check_network
140 * policy capability is enabled, SECMARK is always considered enabled.
Paul Moored621d352008-01-29 08:43:36 -0500141 *
142 */
143static int selinux_secmark_enabled(void)
144{
Chris PeBenito2be4d742013-05-03 09:05:39 -0400145 return (selinux_policycap_alwaysnetwork || atomic_read(&selinux_secmark_refcount));
146}
147
148/**
149 * selinux_peerlbl_enabled - Check to see if peer labeling is currently enabled
150 *
151 * Description:
152 * This function checks if NetLabel or labeled IPSEC is enabled. Returns true
153 * (1) if any are enabled or false (0) if neither are enabled. If the
154 * always_check_network policy capability is enabled, peer labeling
155 * is always considered enabled.
156 *
157 */
158static int selinux_peerlbl_enabled(void)
159{
160 return (selinux_policycap_alwaysnetwork || netlbl_enabled() || selinux_xfrm_enabled());
Paul Moored621d352008-01-29 08:43:36 -0500161}
162
David Howellsd84f4f92008-11-14 10:39:23 +1100163/*
164 * initialise the security for the init task
165 */
166static void cred_init_security(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
David Howells3b11a1d2008-11-14 10:39:26 +1100168 struct cred *cred = (struct cred *) current->real_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 struct task_security_struct *tsec;
170
James Morris89d155e2005-10-30 14:59:21 -0800171 tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 if (!tsec)
David Howellsd84f4f92008-11-14 10:39:23 +1100173 panic("SELinux: Failed to initialize initial task.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
David Howellsd84f4f92008-11-14 10:39:23 +1100175 tsec->osid = tsec->sid = SECINITSID_KERNEL;
David Howellsf1752ee2008-11-14 10:39:17 +1100176 cred->security = tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
David Howells275bb412008-11-14 10:39:19 +1100179/*
David Howells88e67f32008-11-14 10:39:21 +1100180 * get the security ID of a set of credentials
181 */
182static inline u32 cred_sid(const struct cred *cred)
183{
184 const struct task_security_struct *tsec;
185
186 tsec = cred->security;
187 return tsec->sid;
188}
189
190/*
David Howells3b11a1d2008-11-14 10:39:26 +1100191 * get the objective security ID of a task
David Howells275bb412008-11-14 10:39:19 +1100192 */
193static inline u32 task_sid(const struct task_struct *task)
194{
David Howells275bb412008-11-14 10:39:19 +1100195 u32 sid;
196
197 rcu_read_lock();
David Howells88e67f32008-11-14 10:39:21 +1100198 sid = cred_sid(__task_cred(task));
David Howells275bb412008-11-14 10:39:19 +1100199 rcu_read_unlock();
200 return sid;
201}
202
203/*
David Howells3b11a1d2008-11-14 10:39:26 +1100204 * get the subjective security ID of the current task
David Howells275bb412008-11-14 10:39:19 +1100205 */
206static inline u32 current_sid(void)
207{
Paul Moore5fb49872010-04-22 14:46:19 -0400208 const struct task_security_struct *tsec = current_security();
David Howells275bb412008-11-14 10:39:19 +1100209
210 return tsec->sid;
211}
212
David Howells88e67f32008-11-14 10:39:21 +1100213/* Allocate and free functions for each kind of security blob. */
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215static int inode_alloc_security(struct inode *inode)
216{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 struct inode_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +1100218 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Josef Bacika02fe132008-04-04 09:35:05 +1100220 isec = kmem_cache_zalloc(sel_inode_cache, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 if (!isec)
222 return -ENOMEM;
223
Eric Paris23970742006-09-25 23:32:01 -0700224 mutex_init(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 INIT_LIST_HEAD(&isec->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 isec->inode = inode;
227 isec->sid = SECINITSID_UNLABELED;
228 isec->sclass = SECCLASS_FILE;
David Howells275bb412008-11-14 10:39:19 +1100229 isec->task_sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 inode->i_security = isec;
231
232 return 0;
233}
234
235static void inode_free_security(struct inode *inode)
236{
237 struct inode_security_struct *isec = inode->i_security;
238 struct superblock_security_struct *sbsec = inode->i_sb->s_security;
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 spin_lock(&sbsec->isec_lock);
241 if (!list_empty(&isec->list))
242 list_del_init(&isec->list);
243 spin_unlock(&sbsec->isec_lock);
244
245 inode->i_security = NULL;
James Morris7cae7e22006-03-22 00:09:22 -0800246 kmem_cache_free(sel_inode_cache, isec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
248
249static int file_alloc_security(struct file *file)
250{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 struct file_security_struct *fsec;
David Howells275bb412008-11-14 10:39:19 +1100252 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Stephen Smalley26d2a4b2006-02-01 03:05:55 -0800254 fsec = kzalloc(sizeof(struct file_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if (!fsec)
256 return -ENOMEM;
257
David Howells275bb412008-11-14 10:39:19 +1100258 fsec->sid = sid;
259 fsec->fown_sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 file->f_security = fsec;
261
262 return 0;
263}
264
265static void file_free_security(struct file *file)
266{
267 struct file_security_struct *fsec = file->f_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 file->f_security = NULL;
269 kfree(fsec);
270}
271
272static int superblock_alloc_security(struct super_block *sb)
273{
274 struct superblock_security_struct *sbsec;
275
James Morris89d155e2005-10-30 14:59:21 -0800276 sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 if (!sbsec)
278 return -ENOMEM;
279
Eric Parisbc7e9822006-09-25 23:32:02 -0700280 mutex_init(&sbsec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 INIT_LIST_HEAD(&sbsec->isec_head);
282 spin_lock_init(&sbsec->isec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 sbsec->sb = sb;
284 sbsec->sid = SECINITSID_UNLABELED;
285 sbsec->def_sid = SECINITSID_FILE;
Eric Parisc312feb2006-07-10 04:43:53 -0700286 sbsec->mntpoint_sid = SECINITSID_UNLABELED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 sb->s_security = sbsec;
288
289 return 0;
290}
291
292static void superblock_free_security(struct super_block *sb)
293{
294 struct superblock_security_struct *sbsec = sb->s_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 sb->s_security = NULL;
296 kfree(sbsec);
297}
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299/* The file system's label must be initialized prior to use. */
300
Stephen Hemminger634a5392010-03-04 21:59:03 -0800301static const char *labeling_behaviors[6] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 "uses xattr",
303 "uses transition SIDs",
304 "uses task SIDs",
305 "uses genfs_contexts",
306 "not configured for labeling",
307 "uses mountpoint labeling",
308};
309
310static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry);
311
312static inline int inode_doinit(struct inode *inode)
313{
314 return inode_doinit_with_dentry(inode, NULL);
315}
316
317enum {
Eric Paris31e87932007-09-19 17:19:12 -0400318 Opt_error = -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 Opt_context = 1,
320 Opt_fscontext = 2,
Eric Parisc9180a52007-11-30 13:00:35 -0500321 Opt_defcontext = 3,
322 Opt_rootcontext = 4,
David P. Quigley11689d42009-01-16 09:22:03 -0500323 Opt_labelsupport = 5,
Eric Parisd355987f2012-08-24 15:58:53 -0400324 Opt_nextmntopt = 6,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325};
326
Eric Parisd355987f2012-08-24 15:58:53 -0400327#define NUM_SEL_MNT_OPTS (Opt_nextmntopt - 1)
328
Steven Whitehousea447c092008-10-13 10:46:57 +0100329static const match_table_t tokens = {
Eric Paris832cbd92008-04-01 13:24:09 -0400330 {Opt_context, CONTEXT_STR "%s"},
331 {Opt_fscontext, FSCONTEXT_STR "%s"},
332 {Opt_defcontext, DEFCONTEXT_STR "%s"},
333 {Opt_rootcontext, ROOTCONTEXT_STR "%s"},
David P. Quigley11689d42009-01-16 09:22:03 -0500334 {Opt_labelsupport, LABELSUPP_STR},
Eric Paris31e87932007-09-19 17:19:12 -0400335 {Opt_error, NULL},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336};
337
338#define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n"
339
Eric Parisc312feb2006-07-10 04:43:53 -0700340static int may_context_mount_sb_relabel(u32 sid,
341 struct superblock_security_struct *sbsec,
David Howells275bb412008-11-14 10:39:19 +1100342 const struct cred *cred)
Eric Parisc312feb2006-07-10 04:43:53 -0700343{
David Howells275bb412008-11-14 10:39:19 +1100344 const struct task_security_struct *tsec = cred->security;
Eric Parisc312feb2006-07-10 04:43:53 -0700345 int rc;
346
347 rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
348 FILESYSTEM__RELABELFROM, NULL);
349 if (rc)
350 return rc;
351
352 rc = avc_has_perm(tsec->sid, sid, SECCLASS_FILESYSTEM,
353 FILESYSTEM__RELABELTO, NULL);
354 return rc;
355}
356
Eric Paris08089252006-07-10 04:43:55 -0700357static int may_context_mount_inode_relabel(u32 sid,
358 struct superblock_security_struct *sbsec,
David Howells275bb412008-11-14 10:39:19 +1100359 const struct cred *cred)
Eric Paris08089252006-07-10 04:43:55 -0700360{
David Howells275bb412008-11-14 10:39:19 +1100361 const struct task_security_struct *tsec = cred->security;
Eric Paris08089252006-07-10 04:43:55 -0700362 int rc;
363 rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
364 FILESYSTEM__RELABELFROM, NULL);
365 if (rc)
366 return rc;
367
368 rc = avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM,
369 FILESYSTEM__ASSOCIATE, NULL);
370 return rc;
371}
372
Eric Parisb43e7252012-10-10 14:27:35 -0400373static int selinux_is_sblabel_mnt(struct super_block *sb)
374{
375 struct superblock_security_struct *sbsec = sb->s_security;
376
377 if (sbsec->behavior == SECURITY_FS_USE_XATTR ||
378 sbsec->behavior == SECURITY_FS_USE_TRANS ||
379 sbsec->behavior == SECURITY_FS_USE_TASK)
380 return 1;
381
382 /* Special handling for sysfs. Is genfs but also has setxattr handler*/
383 if (strncmp(sb->s_type->name, "sysfs", sizeof("sysfs")) == 0)
384 return 1;
385
386 /*
387 * Special handling for rootfs. Is genfs but supports
388 * setting SELinux context on in-core inodes.
389 */
390 if (strncmp(sb->s_type->name, "rootfs", sizeof("rootfs")) == 0)
391 return 1;
392
393 return 0;
394}
395
Eric Parisc9180a52007-11-30 13:00:35 -0500396static int sb_finish_set_opts(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
398 struct superblock_security_struct *sbsec = sb->s_security;
399 struct dentry *root = sb->s_root;
Eric Parisc9180a52007-11-30 13:00:35 -0500400 struct inode *root_inode = root->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 int rc = 0;
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
404 /* Make sure that the xattr handler exists and that no
405 error other than -ENODATA is returned by getxattr on
406 the root directory. -ENODATA is ok, as this may be
407 the first boot of the SELinux kernel before we have
408 assigned xattr values to the filesystem. */
Eric Parisc9180a52007-11-30 13:00:35 -0500409 if (!root_inode->i_op->getxattr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 printk(KERN_WARNING "SELinux: (dev %s, type %s) has no "
411 "xattr support\n", sb->s_id, sb->s_type->name);
412 rc = -EOPNOTSUPP;
413 goto out;
414 }
Eric Parisc9180a52007-11-30 13:00:35 -0500415 rc = root_inode->i_op->getxattr(root, XATTR_NAME_SELINUX, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 if (rc < 0 && rc != -ENODATA) {
417 if (rc == -EOPNOTSUPP)
418 printk(KERN_WARNING "SELinux: (dev %s, type "
419 "%s) has no security xattr handler\n",
420 sb->s_id, sb->s_type->name);
421 else
422 printk(KERN_WARNING "SELinux: (dev %s, type "
423 "%s) getxattr errno %d\n", sb->s_id,
424 sb->s_type->name, -rc);
425 goto out;
426 }
427 }
428
Eric Parisc9180a52007-11-30 13:00:35 -0500429 if (sbsec->behavior > ARRAY_SIZE(labeling_behaviors))
Eric Parisfadcdb42007-02-22 18:11:31 -0500430 printk(KERN_ERR "SELinux: initialized (dev %s, type %s), unknown behavior\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 sb->s_id, sb->s_type->name);
Eric Parisc9180a52007-11-30 13:00:35 -0500432 else
Eric Parisfadcdb42007-02-22 18:11:31 -0500433 printk(KERN_DEBUG "SELinux: initialized (dev %s, type %s), %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 sb->s_id, sb->s_type->name,
435 labeling_behaviors[sbsec->behavior-1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Eric Pariseadcabc2012-08-24 15:59:14 -0400437 sbsec->flags |= SE_SBINITIALIZED;
Eric Parisb43e7252012-10-10 14:27:35 -0400438 if (selinux_is_sblabel_mnt(sb))
Eric Paris12f348b2012-10-09 10:56:25 -0400439 sbsec->flags |= SBLABEL_MNT;
Stephen Smalley5c73fce2013-07-23 17:38:41 -0400440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 /* Initialize the root inode. */
Eric Parisc9180a52007-11-30 13:00:35 -0500442 rc = inode_doinit_with_dentry(root_inode, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 /* Initialize any other inodes associated with the superblock, e.g.
445 inodes created prior to initial policy load or inodes created
446 during get_sb by a pseudo filesystem that directly
447 populates itself. */
448 spin_lock(&sbsec->isec_lock);
449next_inode:
450 if (!list_empty(&sbsec->isec_head)) {
451 struct inode_security_struct *isec =
452 list_entry(sbsec->isec_head.next,
Eric Parisc9180a52007-11-30 13:00:35 -0500453 struct inode_security_struct, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 struct inode *inode = isec->inode;
455 spin_unlock(&sbsec->isec_lock);
456 inode = igrab(inode);
457 if (inode) {
Eric Parisc9180a52007-11-30 13:00:35 -0500458 if (!IS_PRIVATE(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 inode_doinit(inode);
460 iput(inode);
461 }
462 spin_lock(&sbsec->isec_lock);
463 list_del_init(&isec->list);
464 goto next_inode;
465 }
466 spin_unlock(&sbsec->isec_lock);
467out:
Eric Parisc9180a52007-11-30 13:00:35 -0500468 return rc;
469}
470
471/*
472 * This function should allow an FS to ask what it's mount security
473 * options were so it can use those later for submounts, displaying
474 * mount options, or whatever.
475 */
476static int selinux_get_mnt_opts(const struct super_block *sb,
Eric Parise0007522008-03-05 10:31:54 -0500477 struct security_mnt_opts *opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500478{
479 int rc = 0, i;
480 struct superblock_security_struct *sbsec = sb->s_security;
481 char *context = NULL;
482 u32 len;
483 char tmp;
484
Eric Parise0007522008-03-05 10:31:54 -0500485 security_init_mnt_opts(opts);
Eric Parisc9180a52007-11-30 13:00:35 -0500486
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500487 if (!(sbsec->flags & SE_SBINITIALIZED))
Eric Parisc9180a52007-11-30 13:00:35 -0500488 return -EINVAL;
489
490 if (!ss_initialized)
491 return -EINVAL;
492
Eric Parisaf8e50c2012-08-24 15:59:00 -0400493 /* make sure we always check enough bits to cover the mask */
494 BUILD_BUG_ON(SE_MNTMASK >= (1 << NUM_SEL_MNT_OPTS));
495
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500496 tmp = sbsec->flags & SE_MNTMASK;
Eric Parisc9180a52007-11-30 13:00:35 -0500497 /* count the number of mount options for this sb */
Eric Parisaf8e50c2012-08-24 15:59:00 -0400498 for (i = 0; i < NUM_SEL_MNT_OPTS; i++) {
Eric Parisc9180a52007-11-30 13:00:35 -0500499 if (tmp & 0x01)
Eric Parise0007522008-03-05 10:31:54 -0500500 opts->num_mnt_opts++;
Eric Parisc9180a52007-11-30 13:00:35 -0500501 tmp >>= 1;
502 }
503
Eric Parise0007522008-03-05 10:31:54 -0500504 opts->mnt_opts = kcalloc(opts->num_mnt_opts, sizeof(char *), GFP_ATOMIC);
505 if (!opts->mnt_opts) {
Eric Parisc9180a52007-11-30 13:00:35 -0500506 rc = -ENOMEM;
507 goto out_free;
508 }
509
Eric Parise0007522008-03-05 10:31:54 -0500510 opts->mnt_opts_flags = kcalloc(opts->num_mnt_opts, sizeof(int), GFP_ATOMIC);
511 if (!opts->mnt_opts_flags) {
Eric Parisc9180a52007-11-30 13:00:35 -0500512 rc = -ENOMEM;
513 goto out_free;
514 }
515
516 i = 0;
517 if (sbsec->flags & FSCONTEXT_MNT) {
518 rc = security_sid_to_context(sbsec->sid, &context, &len);
519 if (rc)
520 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500521 opts->mnt_opts[i] = context;
522 opts->mnt_opts_flags[i++] = FSCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500523 }
524 if (sbsec->flags & CONTEXT_MNT) {
525 rc = security_sid_to_context(sbsec->mntpoint_sid, &context, &len);
526 if (rc)
527 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500528 opts->mnt_opts[i] = context;
529 opts->mnt_opts_flags[i++] = CONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500530 }
531 if (sbsec->flags & DEFCONTEXT_MNT) {
532 rc = security_sid_to_context(sbsec->def_sid, &context, &len);
533 if (rc)
534 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500535 opts->mnt_opts[i] = context;
536 opts->mnt_opts_flags[i++] = DEFCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500537 }
538 if (sbsec->flags & ROOTCONTEXT_MNT) {
539 struct inode *root = sbsec->sb->s_root->d_inode;
540 struct inode_security_struct *isec = root->i_security;
541
542 rc = security_sid_to_context(isec->sid, &context, &len);
543 if (rc)
544 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500545 opts->mnt_opts[i] = context;
546 opts->mnt_opts_flags[i++] = ROOTCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500547 }
Eric Paris12f348b2012-10-09 10:56:25 -0400548 if (sbsec->flags & SBLABEL_MNT) {
David P. Quigley11689d42009-01-16 09:22:03 -0500549 opts->mnt_opts[i] = NULL;
Eric Paris12f348b2012-10-09 10:56:25 -0400550 opts->mnt_opts_flags[i++] = SBLABEL_MNT;
David P. Quigley11689d42009-01-16 09:22:03 -0500551 }
Eric Parisc9180a52007-11-30 13:00:35 -0500552
Eric Parise0007522008-03-05 10:31:54 -0500553 BUG_ON(i != opts->num_mnt_opts);
Eric Parisc9180a52007-11-30 13:00:35 -0500554
555 return 0;
556
557out_free:
Eric Parise0007522008-03-05 10:31:54 -0500558 security_free_mnt_opts(opts);
Eric Parisc9180a52007-11-30 13:00:35 -0500559 return rc;
560}
561
562static int bad_option(struct superblock_security_struct *sbsec, char flag,
563 u32 old_sid, u32 new_sid)
564{
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500565 char mnt_flags = sbsec->flags & SE_MNTMASK;
566
Eric Parisc9180a52007-11-30 13:00:35 -0500567 /* check if the old mount command had the same options */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500568 if (sbsec->flags & SE_SBINITIALIZED)
Eric Parisc9180a52007-11-30 13:00:35 -0500569 if (!(sbsec->flags & flag) ||
570 (old_sid != new_sid))
571 return 1;
572
573 /* check if we were passed the same options twice,
574 * aka someone passed context=a,context=b
575 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500576 if (!(sbsec->flags & SE_SBINITIALIZED))
577 if (mnt_flags & flag)
Eric Parisc9180a52007-11-30 13:00:35 -0500578 return 1;
579 return 0;
580}
Eric Parise0007522008-03-05 10:31:54 -0500581
Eric Parisc9180a52007-11-30 13:00:35 -0500582/*
583 * Allow filesystems with binary mount data to explicitly set mount point
584 * labeling information.
585 */
Eric Parise0007522008-03-05 10:31:54 -0500586static int selinux_set_mnt_opts(struct super_block *sb,
587 struct security_mnt_opts *opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500588{
David Howells275bb412008-11-14 10:39:19 +1100589 const struct cred *cred = current_cred();
Eric Parisc9180a52007-11-30 13:00:35 -0500590 int rc = 0, i;
Eric Parisc9180a52007-11-30 13:00:35 -0500591 struct superblock_security_struct *sbsec = sb->s_security;
592 const char *name = sb->s_type->name;
James Morris089be432008-07-15 18:32:49 +1000593 struct inode *inode = sbsec->sb->s_root->d_inode;
594 struct inode_security_struct *root_isec = inode->i_security;
Eric Parisc9180a52007-11-30 13:00:35 -0500595 u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
596 u32 defcontext_sid = 0;
Eric Parise0007522008-03-05 10:31:54 -0500597 char **mount_options = opts->mnt_opts;
598 int *flags = opts->mnt_opts_flags;
599 int num_opts = opts->num_mnt_opts;
Eric Parisc9180a52007-11-30 13:00:35 -0500600
601 mutex_lock(&sbsec->lock);
602
603 if (!ss_initialized) {
604 if (!num_opts) {
605 /* Defer initialization until selinux_complete_init,
606 after the initial policy is loaded and the security
607 server is ready to handle calls. */
Eric Parisc9180a52007-11-30 13:00:35 -0500608 goto out;
609 }
610 rc = -EINVAL;
Eric Paris744ba352008-04-17 11:52:44 -0400611 printk(KERN_WARNING "SELinux: Unable to set superblock options "
612 "before the security server is initialized\n");
Eric Parisc9180a52007-11-30 13:00:35 -0500613 goto out;
614 }
615
616 /*
Eric Parise0007522008-03-05 10:31:54 -0500617 * Binary mount data FS will come through this function twice. Once
618 * from an explicit call and once from the generic calls from the vfs.
619 * Since the generic VFS calls will not contain any security mount data
620 * we need to skip the double mount verification.
621 *
622 * This does open a hole in which we will not notice if the first
623 * mount using this sb set explict options and a second mount using
624 * this sb does not set any security options. (The first options
625 * will be used for both mounts)
626 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500627 if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
Eric Parise0007522008-03-05 10:31:54 -0500628 && (num_opts == 0))
Eric Parisf5269712008-05-14 11:27:45 -0400629 goto out;
Eric Parise0007522008-03-05 10:31:54 -0500630
631 /*
Eric Parisc9180a52007-11-30 13:00:35 -0500632 * parse the mount options, check if they are valid sids.
633 * also check if someone is trying to mount the same sb more
634 * than once with different security options.
635 */
636 for (i = 0; i < num_opts; i++) {
637 u32 sid;
David P. Quigley11689d42009-01-16 09:22:03 -0500638
Eric Paris12f348b2012-10-09 10:56:25 -0400639 if (flags[i] == SBLABEL_MNT)
David P. Quigley11689d42009-01-16 09:22:03 -0500640 continue;
Eric Parisc9180a52007-11-30 13:00:35 -0500641 rc = security_context_to_sid(mount_options[i],
642 strlen(mount_options[i]), &sid);
643 if (rc) {
644 printk(KERN_WARNING "SELinux: security_context_to_sid"
645 "(%s) failed for (dev %s, type %s) errno=%d\n",
646 mount_options[i], sb->s_id, name, rc);
647 goto out;
648 }
649 switch (flags[i]) {
650 case FSCONTEXT_MNT:
651 fscontext_sid = sid;
652
653 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
654 fscontext_sid))
655 goto out_double_mount;
656
657 sbsec->flags |= FSCONTEXT_MNT;
658 break;
659 case CONTEXT_MNT:
660 context_sid = sid;
661
662 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
663 context_sid))
664 goto out_double_mount;
665
666 sbsec->flags |= CONTEXT_MNT;
667 break;
668 case ROOTCONTEXT_MNT:
669 rootcontext_sid = sid;
670
671 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
672 rootcontext_sid))
673 goto out_double_mount;
674
675 sbsec->flags |= ROOTCONTEXT_MNT;
676
677 break;
678 case DEFCONTEXT_MNT:
679 defcontext_sid = sid;
680
681 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
682 defcontext_sid))
683 goto out_double_mount;
684
685 sbsec->flags |= DEFCONTEXT_MNT;
686
687 break;
688 default:
689 rc = -EINVAL;
690 goto out;
691 }
692 }
693
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500694 if (sbsec->flags & SE_SBINITIALIZED) {
Eric Parisc9180a52007-11-30 13:00:35 -0500695 /* previously mounted with options, but not on this attempt? */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500696 if ((sbsec->flags & SE_MNTMASK) && !num_opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500697 goto out_double_mount;
698 rc = 0;
699 goto out;
700 }
701
James Morris089be432008-07-15 18:32:49 +1000702 if (strcmp(sb->s_type->name, "proc") == 0)
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500703 sbsec->flags |= SE_SBPROC;
Eric Parisc9180a52007-11-30 13:00:35 -0500704
705 /* Determine the labeling behavior to use for this filesystem type. */
Eric Parisa64c54c2012-08-24 15:59:07 -0400706 rc = security_fs_use(sb);
Eric Parisc9180a52007-11-30 13:00:35 -0500707 if (rc) {
708 printk(KERN_WARNING "%s: security_fs_use(%s) returned %d\n",
James Morris089be432008-07-15 18:32:49 +1000709 __func__, sb->s_type->name, rc);
Eric Parisc9180a52007-11-30 13:00:35 -0500710 goto out;
711 }
712
713 /* sets the context of the superblock for the fs being mounted. */
714 if (fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100715 rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500716 if (rc)
717 goto out;
718
719 sbsec->sid = fscontext_sid;
720 }
721
722 /*
723 * Switch to using mount point labeling behavior.
724 * sets the label used on all file below the mountpoint, and will set
725 * the superblock context if not already set.
726 */
727 if (context_sid) {
728 if (!fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100729 rc = may_context_mount_sb_relabel(context_sid, sbsec,
730 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500731 if (rc)
732 goto out;
733 sbsec->sid = context_sid;
734 } else {
David Howells275bb412008-11-14 10:39:19 +1100735 rc = may_context_mount_inode_relabel(context_sid, sbsec,
736 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500737 if (rc)
738 goto out;
739 }
740 if (!rootcontext_sid)
741 rootcontext_sid = context_sid;
742
743 sbsec->mntpoint_sid = context_sid;
744 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
745 }
746
747 if (rootcontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100748 rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec,
749 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500750 if (rc)
751 goto out;
752
753 root_isec->sid = rootcontext_sid;
754 root_isec->initialized = 1;
755 }
756
757 if (defcontext_sid) {
758 if (sbsec->behavior != SECURITY_FS_USE_XATTR) {
759 rc = -EINVAL;
760 printk(KERN_WARNING "SELinux: defcontext option is "
761 "invalid for this filesystem type\n");
762 goto out;
763 }
764
765 if (defcontext_sid != sbsec->def_sid) {
766 rc = may_context_mount_inode_relabel(defcontext_sid,
David Howells275bb412008-11-14 10:39:19 +1100767 sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500768 if (rc)
769 goto out;
770 }
771
772 sbsec->def_sid = defcontext_sid;
773 }
774
775 rc = sb_finish_set_opts(sb);
776out:
Eric Parisbc7e9822006-09-25 23:32:02 -0700777 mutex_unlock(&sbsec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 return rc;
Eric Parisc9180a52007-11-30 13:00:35 -0500779out_double_mount:
780 rc = -EINVAL;
781 printk(KERN_WARNING "SELinux: mount invalid. Same superblock, different "
782 "security settings for (dev %s, type %s)\n", sb->s_id, name);
783 goto out;
784}
785
Jeff Layton094f7b62013-04-01 08:14:24 -0400786static int selinux_cmp_sb_context(const struct super_block *oldsb,
787 const struct super_block *newsb)
788{
789 struct superblock_security_struct *old = oldsb->s_security;
790 struct superblock_security_struct *new = newsb->s_security;
791 char oldflags = old->flags & SE_MNTMASK;
792 char newflags = new->flags & SE_MNTMASK;
793
794 if (oldflags != newflags)
795 goto mismatch;
796 if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid)
797 goto mismatch;
798 if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid)
799 goto mismatch;
800 if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid)
801 goto mismatch;
802 if (oldflags & ROOTCONTEXT_MNT) {
803 struct inode_security_struct *oldroot = oldsb->s_root->d_inode->i_security;
804 struct inode_security_struct *newroot = newsb->s_root->d_inode->i_security;
805 if (oldroot->sid != newroot->sid)
806 goto mismatch;
807 }
808 return 0;
809mismatch:
810 printk(KERN_WARNING "SELinux: mount invalid. Same superblock, "
811 "different security settings for (dev %s, "
812 "type %s)\n", newsb->s_id, newsb->s_type->name);
813 return -EBUSY;
814}
815
816static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
Eric Parisc9180a52007-11-30 13:00:35 -0500817 struct super_block *newsb)
818{
819 const struct superblock_security_struct *oldsbsec = oldsb->s_security;
820 struct superblock_security_struct *newsbsec = newsb->s_security;
821
822 int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT);
823 int set_context = (oldsbsec->flags & CONTEXT_MNT);
824 int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT);
825
Eric Paris0f5e6422008-04-21 16:24:11 -0400826 /*
827 * if the parent was able to be mounted it clearly had no special lsm
Al Viroe8c26252010-03-23 06:36:54 -0400828 * mount options. thus we can safely deal with this superblock later
Eric Paris0f5e6422008-04-21 16:24:11 -0400829 */
Al Viroe8c26252010-03-23 06:36:54 -0400830 if (!ss_initialized)
Jeff Layton094f7b62013-04-01 08:14:24 -0400831 return 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500832
Eric Parisc9180a52007-11-30 13:00:35 -0500833 /* how can we clone if the old one wasn't set up?? */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500834 BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED));
Eric Parisc9180a52007-11-30 13:00:35 -0500835
Jeff Layton094f7b62013-04-01 08:14:24 -0400836 /* if fs is reusing a sb, make sure that the contexts match */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500837 if (newsbsec->flags & SE_SBINITIALIZED)
Jeff Layton094f7b62013-04-01 08:14:24 -0400838 return selinux_cmp_sb_context(oldsb, newsb);
Eric Paris5a552612008-04-09 14:08:35 -0400839
Eric Parisc9180a52007-11-30 13:00:35 -0500840 mutex_lock(&newsbsec->lock);
841
842 newsbsec->flags = oldsbsec->flags;
843
844 newsbsec->sid = oldsbsec->sid;
845 newsbsec->def_sid = oldsbsec->def_sid;
846 newsbsec->behavior = oldsbsec->behavior;
847
848 if (set_context) {
849 u32 sid = oldsbsec->mntpoint_sid;
850
851 if (!set_fscontext)
852 newsbsec->sid = sid;
853 if (!set_rootcontext) {
854 struct inode *newinode = newsb->s_root->d_inode;
855 struct inode_security_struct *newisec = newinode->i_security;
856 newisec->sid = sid;
857 }
858 newsbsec->mntpoint_sid = sid;
859 }
860 if (set_rootcontext) {
861 const struct inode *oldinode = oldsb->s_root->d_inode;
862 const struct inode_security_struct *oldisec = oldinode->i_security;
863 struct inode *newinode = newsb->s_root->d_inode;
864 struct inode_security_struct *newisec = newinode->i_security;
865
866 newisec->sid = oldisec->sid;
867 }
868
869 sb_finish_set_opts(newsb);
870 mutex_unlock(&newsbsec->lock);
Jeff Layton094f7b62013-04-01 08:14:24 -0400871 return 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500872}
873
Adrian Bunk2e1479d2008-03-17 22:29:23 +0200874static int selinux_parse_opts_str(char *options,
875 struct security_mnt_opts *opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500876{
Eric Parise0007522008-03-05 10:31:54 -0500877 char *p;
Eric Parisc9180a52007-11-30 13:00:35 -0500878 char *context = NULL, *defcontext = NULL;
879 char *fscontext = NULL, *rootcontext = NULL;
Eric Parise0007522008-03-05 10:31:54 -0500880 int rc, num_mnt_opts = 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500881
Eric Parise0007522008-03-05 10:31:54 -0500882 opts->num_mnt_opts = 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500883
884 /* Standard string-based options. */
885 while ((p = strsep(&options, "|")) != NULL) {
886 int token;
887 substring_t args[MAX_OPT_ARGS];
888
889 if (!*p)
890 continue;
891
892 token = match_token(p, tokens, args);
893
894 switch (token) {
895 case Opt_context:
896 if (context || defcontext) {
897 rc = -EINVAL;
898 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
899 goto out_err;
900 }
901 context = match_strdup(&args[0]);
902 if (!context) {
903 rc = -ENOMEM;
904 goto out_err;
905 }
906 break;
907
908 case Opt_fscontext:
909 if (fscontext) {
910 rc = -EINVAL;
911 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
912 goto out_err;
913 }
914 fscontext = match_strdup(&args[0]);
915 if (!fscontext) {
916 rc = -ENOMEM;
917 goto out_err;
918 }
919 break;
920
921 case Opt_rootcontext:
922 if (rootcontext) {
923 rc = -EINVAL;
924 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
925 goto out_err;
926 }
927 rootcontext = match_strdup(&args[0]);
928 if (!rootcontext) {
929 rc = -ENOMEM;
930 goto out_err;
931 }
932 break;
933
934 case Opt_defcontext:
935 if (context || defcontext) {
936 rc = -EINVAL;
937 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
938 goto out_err;
939 }
940 defcontext = match_strdup(&args[0]);
941 if (!defcontext) {
942 rc = -ENOMEM;
943 goto out_err;
944 }
945 break;
David P. Quigley11689d42009-01-16 09:22:03 -0500946 case Opt_labelsupport:
947 break;
Eric Parisc9180a52007-11-30 13:00:35 -0500948 default:
949 rc = -EINVAL;
950 printk(KERN_WARNING "SELinux: unknown mount option\n");
951 goto out_err;
952
953 }
954 }
955
Eric Parise0007522008-03-05 10:31:54 -0500956 rc = -ENOMEM;
957 opts->mnt_opts = kcalloc(NUM_SEL_MNT_OPTS, sizeof(char *), GFP_ATOMIC);
958 if (!opts->mnt_opts)
959 goto out_err;
960
961 opts->mnt_opts_flags = kcalloc(NUM_SEL_MNT_OPTS, sizeof(int), GFP_ATOMIC);
962 if (!opts->mnt_opts_flags) {
963 kfree(opts->mnt_opts);
964 goto out_err;
Eric Parisc9180a52007-11-30 13:00:35 -0500965 }
966
Eric Parise0007522008-03-05 10:31:54 -0500967 if (fscontext) {
968 opts->mnt_opts[num_mnt_opts] = fscontext;
969 opts->mnt_opts_flags[num_mnt_opts++] = FSCONTEXT_MNT;
970 }
971 if (context) {
972 opts->mnt_opts[num_mnt_opts] = context;
973 opts->mnt_opts_flags[num_mnt_opts++] = CONTEXT_MNT;
974 }
975 if (rootcontext) {
976 opts->mnt_opts[num_mnt_opts] = rootcontext;
977 opts->mnt_opts_flags[num_mnt_opts++] = ROOTCONTEXT_MNT;
978 }
979 if (defcontext) {
980 opts->mnt_opts[num_mnt_opts] = defcontext;
981 opts->mnt_opts_flags[num_mnt_opts++] = DEFCONTEXT_MNT;
982 }
983
984 opts->num_mnt_opts = num_mnt_opts;
985 return 0;
986
Eric Parisc9180a52007-11-30 13:00:35 -0500987out_err:
988 kfree(context);
989 kfree(defcontext);
990 kfree(fscontext);
991 kfree(rootcontext);
992 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993}
Eric Parise0007522008-03-05 10:31:54 -0500994/*
995 * string mount options parsing and call set the sbsec
996 */
997static int superblock_doinit(struct super_block *sb, void *data)
998{
999 int rc = 0;
1000 char *options = data;
1001 struct security_mnt_opts opts;
1002
1003 security_init_mnt_opts(&opts);
1004
1005 if (!data)
1006 goto out;
1007
1008 BUG_ON(sb->s_type->fs_flags & FS_BINARY_MOUNTDATA);
1009
1010 rc = selinux_parse_opts_str(options, &opts);
1011 if (rc)
1012 goto out_err;
1013
1014out:
1015 rc = selinux_set_mnt_opts(sb, &opts);
1016
1017out_err:
1018 security_free_mnt_opts(&opts);
1019 return rc;
1020}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Adrian Bunk3583a712008-07-22 20:21:23 +03001022static void selinux_write_opts(struct seq_file *m,
1023 struct security_mnt_opts *opts)
Eric Paris2069f452008-07-04 09:47:13 +10001024{
1025 int i;
1026 char *prefix;
1027
1028 for (i = 0; i < opts->num_mnt_opts; i++) {
David P. Quigley11689d42009-01-16 09:22:03 -05001029 char *has_comma;
1030
1031 if (opts->mnt_opts[i])
1032 has_comma = strchr(opts->mnt_opts[i], ',');
1033 else
1034 has_comma = NULL;
Eric Paris2069f452008-07-04 09:47:13 +10001035
1036 switch (opts->mnt_opts_flags[i]) {
1037 case CONTEXT_MNT:
1038 prefix = CONTEXT_STR;
1039 break;
1040 case FSCONTEXT_MNT:
1041 prefix = FSCONTEXT_STR;
1042 break;
1043 case ROOTCONTEXT_MNT:
1044 prefix = ROOTCONTEXT_STR;
1045 break;
1046 case DEFCONTEXT_MNT:
1047 prefix = DEFCONTEXT_STR;
1048 break;
Eric Paris12f348b2012-10-09 10:56:25 -04001049 case SBLABEL_MNT:
David P. Quigley11689d42009-01-16 09:22:03 -05001050 seq_putc(m, ',');
1051 seq_puts(m, LABELSUPP_STR);
1052 continue;
Eric Paris2069f452008-07-04 09:47:13 +10001053 default:
1054 BUG();
Eric Parisa35c6c832011-04-20 10:21:28 -04001055 return;
Eric Paris2069f452008-07-04 09:47:13 +10001056 };
1057 /* we need a comma before each option */
1058 seq_putc(m, ',');
1059 seq_puts(m, prefix);
1060 if (has_comma)
1061 seq_putc(m, '\"');
1062 seq_puts(m, opts->mnt_opts[i]);
1063 if (has_comma)
1064 seq_putc(m, '\"');
1065 }
1066}
1067
1068static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
1069{
1070 struct security_mnt_opts opts;
1071 int rc;
1072
1073 rc = selinux_get_mnt_opts(sb, &opts);
Eric Paris383795c2008-07-29 17:07:26 -04001074 if (rc) {
1075 /* before policy load we may get EINVAL, don't show anything */
1076 if (rc == -EINVAL)
1077 rc = 0;
Eric Paris2069f452008-07-04 09:47:13 +10001078 return rc;
Eric Paris383795c2008-07-29 17:07:26 -04001079 }
Eric Paris2069f452008-07-04 09:47:13 +10001080
1081 selinux_write_opts(m, &opts);
1082
1083 security_free_mnt_opts(&opts);
1084
1085 return rc;
1086}
1087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088static inline u16 inode_mode_to_security_class(umode_t mode)
1089{
1090 switch (mode & S_IFMT) {
1091 case S_IFSOCK:
1092 return SECCLASS_SOCK_FILE;
1093 case S_IFLNK:
1094 return SECCLASS_LNK_FILE;
1095 case S_IFREG:
1096 return SECCLASS_FILE;
1097 case S_IFBLK:
1098 return SECCLASS_BLK_FILE;
1099 case S_IFDIR:
1100 return SECCLASS_DIR;
1101 case S_IFCHR:
1102 return SECCLASS_CHR_FILE;
1103 case S_IFIFO:
1104 return SECCLASS_FIFO_FILE;
1105
1106 }
1107
1108 return SECCLASS_FILE;
1109}
1110
James Morris13402582005-09-30 14:24:34 -04001111static inline int default_protocol_stream(int protocol)
1112{
1113 return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP);
1114}
1115
1116static inline int default_protocol_dgram(int protocol)
1117{
1118 return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
1119}
1120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121static inline u16 socket_type_to_security_class(int family, int type, int protocol)
1122{
1123 switch (family) {
1124 case PF_UNIX:
1125 switch (type) {
1126 case SOCK_STREAM:
1127 case SOCK_SEQPACKET:
1128 return SECCLASS_UNIX_STREAM_SOCKET;
1129 case SOCK_DGRAM:
1130 return SECCLASS_UNIX_DGRAM_SOCKET;
1131 }
1132 break;
1133 case PF_INET:
1134 case PF_INET6:
1135 switch (type) {
1136 case SOCK_STREAM:
James Morris13402582005-09-30 14:24:34 -04001137 if (default_protocol_stream(protocol))
1138 return SECCLASS_TCP_SOCKET;
1139 else
1140 return SECCLASS_RAWIP_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 case SOCK_DGRAM:
James Morris13402582005-09-30 14:24:34 -04001142 if (default_protocol_dgram(protocol))
1143 return SECCLASS_UDP_SOCKET;
1144 else
1145 return SECCLASS_RAWIP_SOCKET;
James Morris2ee92d42006-11-13 16:09:01 -08001146 case SOCK_DCCP:
1147 return SECCLASS_DCCP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001148 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 return SECCLASS_RAWIP_SOCKET;
1150 }
1151 break;
1152 case PF_NETLINK:
1153 switch (protocol) {
1154 case NETLINK_ROUTE:
1155 return SECCLASS_NETLINK_ROUTE_SOCKET;
1156 case NETLINK_FIREWALL:
1157 return SECCLASS_NETLINK_FIREWALL_SOCKET;
Pavel Emelyanov7f1fb602011-12-06 07:56:43 +00001158 case NETLINK_SOCK_DIAG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 return SECCLASS_NETLINK_TCPDIAG_SOCKET;
1160 case NETLINK_NFLOG:
1161 return SECCLASS_NETLINK_NFLOG_SOCKET;
1162 case NETLINK_XFRM:
1163 return SECCLASS_NETLINK_XFRM_SOCKET;
1164 case NETLINK_SELINUX:
1165 return SECCLASS_NETLINK_SELINUX_SOCKET;
1166 case NETLINK_AUDIT:
1167 return SECCLASS_NETLINK_AUDIT_SOCKET;
1168 case NETLINK_IP6_FW:
1169 return SECCLASS_NETLINK_IP6FW_SOCKET;
1170 case NETLINK_DNRTMSG:
1171 return SECCLASS_NETLINK_DNRT_SOCKET;
James Morris0c9b7942005-04-16 15:24:13 -07001172 case NETLINK_KOBJECT_UEVENT:
1173 return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 default:
1175 return SECCLASS_NETLINK_SOCKET;
1176 }
1177 case PF_PACKET:
1178 return SECCLASS_PACKET_SOCKET;
1179 case PF_KEY:
1180 return SECCLASS_KEY_SOCKET;
Christopher J. PeBenito3e3ff152006-06-09 00:25:03 -07001181 case PF_APPLETALK:
1182 return SECCLASS_APPLETALK_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 }
1184
1185 return SECCLASS_SOCKET;
1186}
1187
1188#ifdef CONFIG_PROC_FS
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001189static int selinux_proc_get_sid(struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 u16 tclass,
1191 u32 *sid)
1192{
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001193 int rc;
1194 char *buffer, *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Eric Paris828dfe12008-04-17 13:17:49 -04001196 buffer = (char *)__get_free_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 if (!buffer)
1198 return -ENOMEM;
1199
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001200 path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
1201 if (IS_ERR(path))
1202 rc = PTR_ERR(path);
1203 else {
1204 /* each process gets a /proc/PID/ entry. Strip off the
1205 * PID part to get a valid selinux labeling.
1206 * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
1207 while (path[1] >= '0' && path[1] <= '9') {
1208 path[1] = '/';
1209 path++;
1210 }
1211 rc = security_genfs_sid("proc", path, tclass, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 free_page((unsigned long)buffer);
1214 return rc;
1215}
1216#else
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001217static int selinux_proc_get_sid(struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 u16 tclass,
1219 u32 *sid)
1220{
1221 return -EINVAL;
1222}
1223#endif
1224
1225/* The inode's security attributes must be initialized before first use. */
1226static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
1227{
1228 struct superblock_security_struct *sbsec = NULL;
1229 struct inode_security_struct *isec = inode->i_security;
1230 u32 sid;
1231 struct dentry *dentry;
1232#define INITCONTEXTLEN 255
1233 char *context = NULL;
1234 unsigned len = 0;
1235 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
1237 if (isec->initialized)
1238 goto out;
1239
Eric Paris23970742006-09-25 23:32:01 -07001240 mutex_lock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 if (isec->initialized)
Eric Paris23970742006-09-25 23:32:01 -07001242 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
1244 sbsec = inode->i_sb->s_security;
David P. Quigley0d90a7e2009-01-16 09:22:02 -05001245 if (!(sbsec->flags & SE_SBINITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 /* Defer initialization until selinux_complete_init,
1247 after the initial policy is loaded and the security
1248 server is ready to handle calls. */
1249 spin_lock(&sbsec->isec_lock);
1250 if (list_empty(&isec->list))
1251 list_add(&isec->list, &sbsec->isec_head);
1252 spin_unlock(&sbsec->isec_lock);
Eric Paris23970742006-09-25 23:32:01 -07001253 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 }
1255
1256 switch (sbsec->behavior) {
1257 case SECURITY_FS_USE_XATTR:
1258 if (!inode->i_op->getxattr) {
1259 isec->sid = sbsec->def_sid;
1260 break;
1261 }
1262
1263 /* Need a dentry, since the xattr API requires one.
1264 Life would be simpler if we could just pass the inode. */
1265 if (opt_dentry) {
1266 /* Called from d_instantiate or d_splice_alias. */
1267 dentry = dget(opt_dentry);
1268 } else {
1269 /* Called from selinux_complete_init, try to find a dentry. */
1270 dentry = d_find_alias(inode);
1271 }
1272 if (!dentry) {
Eric Parisdf7f54c2009-03-09 14:35:58 -04001273 /*
1274 * this is can be hit on boot when a file is accessed
1275 * before the policy is loaded. When we load policy we
1276 * may find inodes that have no dentry on the
1277 * sbsec->isec_head list. No reason to complain as these
1278 * will get fixed up the next time we go through
1279 * inode_doinit with a dentry, before these inodes could
1280 * be used again by userspace.
1281 */
Eric Paris23970742006-09-25 23:32:01 -07001282 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 }
1284
1285 len = INITCONTEXTLEN;
Eric Paris4cb912f2009-02-12 14:50:05 -05001286 context = kmalloc(len+1, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 if (!context) {
1288 rc = -ENOMEM;
1289 dput(dentry);
Eric Paris23970742006-09-25 23:32:01 -07001290 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 }
Eric Paris4cb912f2009-02-12 14:50:05 -05001292 context[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX,
1294 context, len);
1295 if (rc == -ERANGE) {
James Morris314dabb2009-08-10 22:00:13 +10001296 kfree(context);
1297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 /* Need a larger buffer. Query for the right size. */
1299 rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX,
1300 NULL, 0);
1301 if (rc < 0) {
1302 dput(dentry);
Eric Paris23970742006-09-25 23:32:01 -07001303 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 len = rc;
Eric Paris4cb912f2009-02-12 14:50:05 -05001306 context = kmalloc(len+1, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 if (!context) {
1308 rc = -ENOMEM;
1309 dput(dentry);
Eric Paris23970742006-09-25 23:32:01 -07001310 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 }
Eric Paris4cb912f2009-02-12 14:50:05 -05001312 context[len] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 rc = inode->i_op->getxattr(dentry,
1314 XATTR_NAME_SELINUX,
1315 context, len);
1316 }
1317 dput(dentry);
1318 if (rc < 0) {
1319 if (rc != -ENODATA) {
Eric Paris744ba352008-04-17 11:52:44 -04001320 printk(KERN_WARNING "SELinux: %s: getxattr returned "
Harvey Harrisondd6f9532008-03-06 10:03:59 +11001321 "%d for dev=%s ino=%ld\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 -rc, inode->i_sb->s_id, inode->i_ino);
1323 kfree(context);
Eric Paris23970742006-09-25 23:32:01 -07001324 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 }
1326 /* Map ENODATA to the default file SID */
1327 sid = sbsec->def_sid;
1328 rc = 0;
1329 } else {
James Morrisf5c1d5b2005-07-28 01:07:37 -07001330 rc = security_context_to_sid_default(context, rc, &sid,
Stephen Smalley869ab512008-04-04 08:46:05 -04001331 sbsec->def_sid,
1332 GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 if (rc) {
Eric Paris4ba0a8a2009-02-12 15:01:10 -05001334 char *dev = inode->i_sb->s_id;
1335 unsigned long ino = inode->i_ino;
1336
1337 if (rc == -EINVAL) {
1338 if (printk_ratelimit())
1339 printk(KERN_NOTICE "SELinux: inode=%lu on dev=%s was found to have an invalid "
1340 "context=%s. This indicates you may need to relabel the inode or the "
1341 "filesystem in question.\n", ino, dev, context);
1342 } else {
1343 printk(KERN_WARNING "SELinux: %s: context_to_sid(%s) "
1344 "returned %d for dev=%s ino=%ld\n",
1345 __func__, context, -rc, dev, ino);
1346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 kfree(context);
1348 /* Leave with the unlabeled SID */
1349 rc = 0;
1350 break;
1351 }
1352 }
1353 kfree(context);
1354 isec->sid = sid;
1355 break;
1356 case SECURITY_FS_USE_TASK:
1357 isec->sid = isec->task_sid;
1358 break;
1359 case SECURITY_FS_USE_TRANS:
1360 /* Default to the fs SID. */
1361 isec->sid = sbsec->sid;
1362
1363 /* Try to obtain a transition SID. */
1364 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Eric Paris652bb9b2011-02-01 11:05:40 -05001365 rc = security_transition_sid(isec->task_sid, sbsec->sid,
1366 isec->sclass, NULL, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 if (rc)
Eric Paris23970742006-09-25 23:32:01 -07001368 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 isec->sid = sid;
1370 break;
Eric Parisc312feb2006-07-10 04:43:53 -07001371 case SECURITY_FS_USE_MNTPOINT:
1372 isec->sid = sbsec->mntpoint_sid;
1373 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 default:
Eric Parisc312feb2006-07-10 04:43:53 -07001375 /* Default to the fs superblock SID. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 isec->sid = sbsec->sid;
1377
David P. Quigley0d90a7e2009-01-16 09:22:02 -05001378 if ((sbsec->flags & SE_SBPROC) && !S_ISLNK(inode->i_mode)) {
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001379 if (opt_dentry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001381 rc = selinux_proc_get_sid(opt_dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 isec->sclass,
1383 &sid);
1384 if (rc)
Eric Paris23970742006-09-25 23:32:01 -07001385 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 isec->sid = sid;
1387 }
1388 }
1389 break;
1390 }
1391
1392 isec->initialized = 1;
1393
Eric Paris23970742006-09-25 23:32:01 -07001394out_unlock:
1395 mutex_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396out:
1397 if (isec->sclass == SECCLASS_FILE)
1398 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 return rc;
1400}
1401
1402/* Convert a Linux signal to an access vector. */
1403static inline u32 signal_to_av(int sig)
1404{
1405 u32 perm = 0;
1406
1407 switch (sig) {
1408 case SIGCHLD:
1409 /* Commonly granted from child to parent. */
1410 perm = PROCESS__SIGCHLD;
1411 break;
1412 case SIGKILL:
1413 /* Cannot be caught or ignored */
1414 perm = PROCESS__SIGKILL;
1415 break;
1416 case SIGSTOP:
1417 /* Cannot be caught or ignored */
1418 perm = PROCESS__SIGSTOP;
1419 break;
1420 default:
1421 /* All other signals. */
1422 perm = PROCESS__SIGNAL;
1423 break;
1424 }
1425
1426 return perm;
1427}
1428
David Howells275bb412008-11-14 10:39:19 +11001429/*
David Howellsd84f4f92008-11-14 10:39:23 +11001430 * Check permission between a pair of credentials
1431 * fork check, ptrace check, etc.
1432 */
1433static int cred_has_perm(const struct cred *actor,
1434 const struct cred *target,
1435 u32 perms)
1436{
1437 u32 asid = cred_sid(actor), tsid = cred_sid(target);
1438
1439 return avc_has_perm(asid, tsid, SECCLASS_PROCESS, perms, NULL);
1440}
1441
1442/*
David Howells88e67f32008-11-14 10:39:21 +11001443 * Check permission between a pair of tasks, e.g. signal checks,
David Howells275bb412008-11-14 10:39:19 +11001444 * fork check, ptrace check, etc.
1445 * tsk1 is the actor and tsk2 is the target
David Howells3b11a1d2008-11-14 10:39:26 +11001446 * - this uses the default subjective creds of tsk1
David Howells275bb412008-11-14 10:39:19 +11001447 */
1448static int task_has_perm(const struct task_struct *tsk1,
1449 const struct task_struct *tsk2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 u32 perms)
1451{
David Howells275bb412008-11-14 10:39:19 +11001452 const struct task_security_struct *__tsec1, *__tsec2;
1453 u32 sid1, sid2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
David Howells275bb412008-11-14 10:39:19 +11001455 rcu_read_lock();
1456 __tsec1 = __task_cred(tsk1)->security; sid1 = __tsec1->sid;
1457 __tsec2 = __task_cred(tsk2)->security; sid2 = __tsec2->sid;
1458 rcu_read_unlock();
1459 return avc_has_perm(sid1, sid2, SECCLASS_PROCESS, perms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460}
1461
David Howells3b11a1d2008-11-14 10:39:26 +11001462/*
1463 * Check permission between current and another task, e.g. signal checks,
1464 * fork check, ptrace check, etc.
1465 * current is the actor and tsk2 is the target
1466 * - this uses current's subjective creds
1467 */
1468static int current_has_perm(const struct task_struct *tsk,
1469 u32 perms)
1470{
1471 u32 sid, tsid;
1472
1473 sid = current_sid();
1474 tsid = task_sid(tsk);
1475 return avc_has_perm(sid, tsid, SECCLASS_PROCESS, perms, NULL);
1476}
1477
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001478#if CAP_LAST_CAP > 63
1479#error Fix SELinux to handle capabilities > 63.
1480#endif
1481
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482/* Check whether a task is allowed to use a capability. */
Eric Paris6a9de492012-01-03 12:25:14 -05001483static int cred_has_capability(const struct cred *cred,
Eric Paris06112162008-11-11 22:02:50 +11001484 int cap, int audit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485{
Thomas Liu2bf49692009-07-14 12:14:09 -04001486 struct common_audit_data ad;
Eric Paris06112162008-11-11 22:02:50 +11001487 struct av_decision avd;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001488 u16 sclass;
David Howells3699c532009-01-06 22:27:01 +00001489 u32 sid = cred_sid(cred);
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001490 u32 av = CAP_TO_MASK(cap);
Eric Paris06112162008-11-11 22:02:50 +11001491 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Eric Paris50c205f2012-04-04 15:01:43 -04001493 ad.type = LSM_AUDIT_DATA_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 ad.u.cap = cap;
1495
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001496 switch (CAP_TO_INDEX(cap)) {
1497 case 0:
1498 sclass = SECCLASS_CAPABILITY;
1499 break;
1500 case 1:
1501 sclass = SECCLASS_CAPABILITY2;
1502 break;
1503 default:
1504 printk(KERN_ERR
1505 "SELinux: out of range capability %d\n", cap);
1506 BUG();
Eric Parisa35c6c832011-04-20 10:21:28 -04001507 return -EINVAL;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001508 }
Eric Paris06112162008-11-11 22:02:50 +11001509
David Howells275bb412008-11-14 10:39:19 +11001510 rc = avc_has_perm_noaudit(sid, sid, sclass, av, 0, &avd);
Eric Paris9ade0cf2011-04-25 16:26:29 -04001511 if (audit == SECURITY_CAP_AUDIT) {
1512 int rc2 = avc_audit(sid, sid, sclass, av, &avd, rc, &ad, 0);
1513 if (rc2)
1514 return rc2;
1515 }
Eric Paris06112162008-11-11 22:02:50 +11001516 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517}
1518
1519/* Check whether a task is allowed to use a system operation. */
1520static int task_has_system(struct task_struct *tsk,
1521 u32 perms)
1522{
David Howells275bb412008-11-14 10:39:19 +11001523 u32 sid = task_sid(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
David Howells275bb412008-11-14 10:39:19 +11001525 return avc_has_perm(sid, SECINITSID_KERNEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 SECCLASS_SYSTEM, perms, NULL);
1527}
1528
1529/* Check whether a task has a particular permission to an inode.
1530 The 'adp' parameter is optional and allows other audit
1531 data to be passed (e.g. the dentry). */
David Howells88e67f32008-11-14 10:39:21 +11001532static int inode_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 struct inode *inode,
1534 u32 perms,
Eric Paris9ade0cf2011-04-25 16:26:29 -04001535 struct common_audit_data *adp,
1536 unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 struct inode_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +11001539 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
David Howellse0e81732009-09-02 09:13:40 +01001541 validate_creds(cred);
1542
Eric Paris828dfe12008-04-17 13:17:49 -04001543 if (unlikely(IS_PRIVATE(inode)))
Stephen Smalleybbaca6c2007-02-14 00:34:16 -08001544 return 0;
1545
David Howells88e67f32008-11-14 10:39:21 +11001546 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 isec = inode->i_security;
1548
Eric Paris9ade0cf2011-04-25 16:26:29 -04001549 return avc_has_perm_flags(sid, isec->sid, isec->sclass, perms, adp, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550}
1551
1552/* Same as inode_has_perm, but pass explicit audit data containing
1553 the dentry to help the auditing code to more easily generate the
1554 pathname if needed. */
David Howells88e67f32008-11-14 10:39:21 +11001555static inline int dentry_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 struct dentry *dentry,
1557 u32 av)
1558{
1559 struct inode *inode = dentry->d_inode;
Thomas Liu2bf49692009-07-14 12:14:09 -04001560 struct common_audit_data ad;
David Howells88e67f32008-11-14 10:39:21 +11001561
Eric Paris50c205f2012-04-04 15:01:43 -04001562 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Paris2875fa02011-04-28 16:04:24 -04001563 ad.u.dentry = dentry;
1564 return inode_has_perm(cred, inode, av, &ad, 0);
1565}
1566
1567/* Same as inode_has_perm, but pass explicit audit data containing
1568 the path to help the auditing code to more easily generate the
1569 pathname if needed. */
1570static inline int path_has_perm(const struct cred *cred,
1571 struct path *path,
1572 u32 av)
1573{
1574 struct inode *inode = path->dentry->d_inode;
1575 struct common_audit_data ad;
1576
Eric Paris50c205f2012-04-04 15:01:43 -04001577 ad.type = LSM_AUDIT_DATA_PATH;
Eric Paris2875fa02011-04-28 16:04:24 -04001578 ad.u.path = *path;
Eric Paris9ade0cf2011-04-25 16:26:29 -04001579 return inode_has_perm(cred, inode, av, &ad, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580}
1581
1582/* Check whether a task can use an open file descriptor to
1583 access an inode in a given way. Check access to the
1584 descriptor itself, and then use dentry_has_perm to
1585 check a particular permission to the file.
1586 Access to the descriptor is implicitly granted if it
1587 has the same SID as the process. If av is zero, then
1588 access to the file is not checked, e.g. for cases
1589 where only the descriptor is affected like seek. */
David Howells88e67f32008-11-14 10:39:21 +11001590static int file_has_perm(const struct cred *cred,
1591 struct file *file,
1592 u32 av)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 struct file_security_struct *fsec = file->f_security;
Al Viro496ad9a2013-01-23 17:07:38 -05001595 struct inode *inode = file_inode(file);
Thomas Liu2bf49692009-07-14 12:14:09 -04001596 struct common_audit_data ad;
David Howells88e67f32008-11-14 10:39:21 +11001597 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 int rc;
1599
Eric Paris50c205f2012-04-04 15:01:43 -04001600 ad.type = LSM_AUDIT_DATA_PATH;
Eric Parisf48b7392011-04-25 12:54:27 -04001601 ad.u.path = file->f_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
David Howells275bb412008-11-14 10:39:19 +11001603 if (sid != fsec->sid) {
1604 rc = avc_has_perm(sid, fsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 SECCLASS_FD,
1606 FD__USE,
1607 &ad);
1608 if (rc)
David Howells88e67f32008-11-14 10:39:21 +11001609 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 }
1611
1612 /* av is zero if only checking access to the descriptor. */
David Howells88e67f32008-11-14 10:39:21 +11001613 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 if (av)
Eric Paris9ade0cf2011-04-25 16:26:29 -04001615 rc = inode_has_perm(cred, inode, av, &ad, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
David Howells88e67f32008-11-14 10:39:21 +11001617out:
1618 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619}
1620
1621/* Check whether a task can create a file. */
1622static int may_create(struct inode *dir,
1623 struct dentry *dentry,
1624 u16 tclass)
1625{
Paul Moore5fb49872010-04-22 14:46:19 -04001626 const struct task_security_struct *tsec = current_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 struct inode_security_struct *dsec;
1628 struct superblock_security_struct *sbsec;
David Howells275bb412008-11-14 10:39:19 +11001629 u32 sid, newsid;
Thomas Liu2bf49692009-07-14 12:14:09 -04001630 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 int rc;
1632
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 dsec = dir->i_security;
1634 sbsec = dir->i_sb->s_security;
1635
David Howells275bb412008-11-14 10:39:19 +11001636 sid = tsec->sid;
1637 newsid = tsec->create_sid;
1638
Eric Paris50c205f2012-04-04 15:01:43 -04001639 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04001640 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
David Howells275bb412008-11-14 10:39:19 +11001642 rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 DIR__ADD_NAME | DIR__SEARCH,
1644 &ad);
1645 if (rc)
1646 return rc;
1647
Eric Paris12f348b2012-10-09 10:56:25 -04001648 if (!newsid || !(sbsec->flags & SBLABEL_MNT)) {
Eric Pariscb1e9222011-04-28 15:11:21 -04001649 rc = security_transition_sid(sid, dsec->sid, tclass,
1650 &dentry->d_name, &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 if (rc)
1652 return rc;
1653 }
1654
David Howells275bb412008-11-14 10:39:19 +11001655 rc = avc_has_perm(sid, newsid, tclass, FILE__CREATE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 if (rc)
1657 return rc;
1658
1659 return avc_has_perm(newsid, sbsec->sid,
1660 SECCLASS_FILESYSTEM,
1661 FILESYSTEM__ASSOCIATE, &ad);
1662}
1663
Michael LeMay4eb582c2006-06-26 00:24:57 -07001664/* Check whether a task can create a key. */
1665static int may_create_key(u32 ksid,
1666 struct task_struct *ctx)
1667{
David Howells275bb412008-11-14 10:39:19 +11001668 u32 sid = task_sid(ctx);
Michael LeMay4eb582c2006-06-26 00:24:57 -07001669
David Howells275bb412008-11-14 10:39:19 +11001670 return avc_has_perm(sid, ksid, SECCLASS_KEY, KEY__CREATE, NULL);
Michael LeMay4eb582c2006-06-26 00:24:57 -07001671}
1672
Eric Paris828dfe12008-04-17 13:17:49 -04001673#define MAY_LINK 0
1674#define MAY_UNLINK 1
1675#define MAY_RMDIR 2
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
1677/* Check whether a task can link, unlink, or rmdir a file/directory. */
1678static int may_link(struct inode *dir,
1679 struct dentry *dentry,
1680 int kind)
1681
1682{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 struct inode_security_struct *dsec, *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001684 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11001685 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 u32 av;
1687 int rc;
1688
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 dsec = dir->i_security;
1690 isec = dentry->d_inode->i_security;
1691
Eric Paris50c205f2012-04-04 15:01:43 -04001692 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04001693 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
1695 av = DIR__SEARCH;
1696 av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
David Howells275bb412008-11-14 10:39:19 +11001697 rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 if (rc)
1699 return rc;
1700
1701 switch (kind) {
1702 case MAY_LINK:
1703 av = FILE__LINK;
1704 break;
1705 case MAY_UNLINK:
1706 av = FILE__UNLINK;
1707 break;
1708 case MAY_RMDIR:
1709 av = DIR__RMDIR;
1710 break;
1711 default:
Eric Paris744ba352008-04-17 11:52:44 -04001712 printk(KERN_WARNING "SELinux: %s: unrecognized kind %d\n",
1713 __func__, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 return 0;
1715 }
1716
David Howells275bb412008-11-14 10:39:19 +11001717 rc = avc_has_perm(sid, isec->sid, isec->sclass, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 return rc;
1719}
1720
1721static inline int may_rename(struct inode *old_dir,
1722 struct dentry *old_dentry,
1723 struct inode *new_dir,
1724 struct dentry *new_dentry)
1725{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001727 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11001728 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 u32 av;
1730 int old_is_dir, new_is_dir;
1731 int rc;
1732
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 old_dsec = old_dir->i_security;
1734 old_isec = old_dentry->d_inode->i_security;
1735 old_is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
1736 new_dsec = new_dir->i_security;
1737
Eric Paris50c205f2012-04-04 15:01:43 -04001738 ad.type = LSM_AUDIT_DATA_DENTRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
Eric Parisa2694342011-04-25 13:10:27 -04001740 ad.u.dentry = old_dentry;
David Howells275bb412008-11-14 10:39:19 +11001741 rc = avc_has_perm(sid, old_dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 DIR__REMOVE_NAME | DIR__SEARCH, &ad);
1743 if (rc)
1744 return rc;
David Howells275bb412008-11-14 10:39:19 +11001745 rc = avc_has_perm(sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 old_isec->sclass, FILE__RENAME, &ad);
1747 if (rc)
1748 return rc;
1749 if (old_is_dir && new_dir != old_dir) {
David Howells275bb412008-11-14 10:39:19 +11001750 rc = avc_has_perm(sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 old_isec->sclass, DIR__REPARENT, &ad);
1752 if (rc)
1753 return rc;
1754 }
1755
Eric Parisa2694342011-04-25 13:10:27 -04001756 ad.u.dentry = new_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 av = DIR__ADD_NAME | DIR__SEARCH;
1758 if (new_dentry->d_inode)
1759 av |= DIR__REMOVE_NAME;
David Howells275bb412008-11-14 10:39:19 +11001760 rc = avc_has_perm(sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 if (rc)
1762 return rc;
1763 if (new_dentry->d_inode) {
1764 new_isec = new_dentry->d_inode->i_security;
1765 new_is_dir = S_ISDIR(new_dentry->d_inode->i_mode);
David Howells275bb412008-11-14 10:39:19 +11001766 rc = avc_has_perm(sid, new_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 new_isec->sclass,
1768 (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
1769 if (rc)
1770 return rc;
1771 }
1772
1773 return 0;
1774}
1775
1776/* Check whether a task can perform a filesystem operation. */
David Howells88e67f32008-11-14 10:39:21 +11001777static int superblock_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 struct super_block *sb,
1779 u32 perms,
Thomas Liu2bf49692009-07-14 12:14:09 -04001780 struct common_audit_data *ad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 struct superblock_security_struct *sbsec;
David Howells88e67f32008-11-14 10:39:21 +11001783 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 sbsec = sb->s_security;
David Howells275bb412008-11-14 10:39:19 +11001786 return avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787}
1788
1789/* Convert a Linux mode and permission mask to an access vector. */
1790static inline u32 file_mask_to_av(int mode, int mask)
1791{
1792 u32 av = 0;
1793
Al Virodba19c62011-07-25 20:49:29 -04001794 if (!S_ISDIR(mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 if (mask & MAY_EXEC)
1796 av |= FILE__EXECUTE;
1797 if (mask & MAY_READ)
1798 av |= FILE__READ;
1799
1800 if (mask & MAY_APPEND)
1801 av |= FILE__APPEND;
1802 else if (mask & MAY_WRITE)
1803 av |= FILE__WRITE;
1804
1805 } else {
1806 if (mask & MAY_EXEC)
1807 av |= DIR__SEARCH;
1808 if (mask & MAY_WRITE)
1809 av |= DIR__WRITE;
1810 if (mask & MAY_READ)
1811 av |= DIR__READ;
1812 }
1813
1814 return av;
1815}
1816
1817/* Convert a Linux file to an access vector. */
1818static inline u32 file_to_av(struct file *file)
1819{
1820 u32 av = 0;
1821
1822 if (file->f_mode & FMODE_READ)
1823 av |= FILE__READ;
1824 if (file->f_mode & FMODE_WRITE) {
1825 if (file->f_flags & O_APPEND)
1826 av |= FILE__APPEND;
1827 else
1828 av |= FILE__WRITE;
1829 }
Stephen Smalley0794c662008-03-17 08:55:18 -04001830 if (!av) {
1831 /*
1832 * Special file opened with flags 3 for ioctl-only use.
1833 */
1834 av = FILE__IOCTL;
1835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
1837 return av;
1838}
1839
Eric Paris8b6a5a32008-10-29 17:06:46 -04001840/*
1841 * Convert a file to an access vector and include the correct open
1842 * open permission.
1843 */
1844static inline u32 open_file_to_av(struct file *file)
1845{
1846 u32 av = file_to_av(file);
1847
Eric Paris49b7b8d2010-07-23 11:44:09 -04001848 if (selinux_policycap_openperm)
1849 av |= FILE__OPEN;
1850
Eric Paris8b6a5a32008-10-29 17:06:46 -04001851 return av;
1852}
1853
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854/* Hook functions begin here. */
1855
Ingo Molnar9e488582009-05-07 19:26:19 +10001856static int selinux_ptrace_access_check(struct task_struct *child,
David Howells5cd9c582008-08-14 11:37:28 +01001857 unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 int rc;
1860
Ingo Molnar9e488582009-05-07 19:26:19 +10001861 rc = cap_ptrace_access_check(child, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 if (rc)
1863 return rc;
1864
Eric Paris69f594a2012-01-03 12:25:15 -05001865 if (mode & PTRACE_MODE_READ) {
David Howells275bb412008-11-14 10:39:19 +11001866 u32 sid = current_sid();
1867 u32 csid = task_sid(child);
1868 return avc_has_perm(sid, csid, SECCLASS_FILE, FILE__READ, NULL);
Stephen Smalley006ebb42008-05-19 08:32:49 -04001869 }
1870
David Howells3b11a1d2008-11-14 10:39:26 +11001871 return current_has_perm(child, PROCESS__PTRACE);
David Howells5cd9c582008-08-14 11:37:28 +01001872}
1873
1874static int selinux_ptrace_traceme(struct task_struct *parent)
1875{
1876 int rc;
1877
Eric Paris200ac532009-02-12 15:01:04 -05001878 rc = cap_ptrace_traceme(parent);
David Howells5cd9c582008-08-14 11:37:28 +01001879 if (rc)
1880 return rc;
1881
1882 return task_has_perm(parent, current, PROCESS__PTRACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883}
1884
1885static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
Eric Paris828dfe12008-04-17 13:17:49 -04001886 kernel_cap_t *inheritable, kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887{
1888 int error;
1889
David Howells3b11a1d2008-11-14 10:39:26 +11001890 error = current_has_perm(target, PROCESS__GETCAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 if (error)
1892 return error;
1893
Eric Paris200ac532009-02-12 15:01:04 -05001894 return cap_capget(target, effective, inheritable, permitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895}
1896
David Howellsd84f4f92008-11-14 10:39:23 +11001897static int selinux_capset(struct cred *new, const struct cred *old,
1898 const kernel_cap_t *effective,
1899 const kernel_cap_t *inheritable,
1900 const kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901{
1902 int error;
1903
Eric Paris200ac532009-02-12 15:01:04 -05001904 error = cap_capset(new, old,
David Howellsd84f4f92008-11-14 10:39:23 +11001905 effective, inheritable, permitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 if (error)
1907 return error;
1908
David Howellsd84f4f92008-11-14 10:39:23 +11001909 return cred_has_perm(old, new, PROCESS__SETCAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910}
1911
James Morris5626d3e2009-01-30 10:05:06 +11001912/*
1913 * (This comment used to live with the selinux_task_setuid hook,
1914 * which was removed).
1915 *
1916 * Since setuid only affects the current process, and since the SELinux
1917 * controls are not based on the Linux identity attributes, SELinux does not
1918 * need to control this operation. However, SELinux does control the use of
1919 * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
1920 */
1921
Eric Paris6a9de492012-01-03 12:25:14 -05001922static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
1923 int cap, int audit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924{
1925 int rc;
1926
Eric Paris6a9de492012-01-03 12:25:14 -05001927 rc = cap_capable(cred, ns, cap, audit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 if (rc)
1929 return rc;
1930
Eric Paris6a9de492012-01-03 12:25:14 -05001931 return cred_has_capability(cred, cap, audit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932}
1933
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
1935{
David Howells88e67f32008-11-14 10:39:21 +11001936 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 int rc = 0;
1938
1939 if (!sb)
1940 return 0;
1941
1942 switch (cmds) {
Eric Paris828dfe12008-04-17 13:17:49 -04001943 case Q_SYNC:
1944 case Q_QUOTAON:
1945 case Q_QUOTAOFF:
1946 case Q_SETINFO:
1947 case Q_SETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11001948 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04001949 break;
1950 case Q_GETFMT:
1951 case Q_GETINFO:
1952 case Q_GETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11001953 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04001954 break;
1955 default:
1956 rc = 0; /* let the kernel handle invalid cmds */
1957 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 }
1959 return rc;
1960}
1961
1962static int selinux_quota_on(struct dentry *dentry)
1963{
David Howells88e67f32008-11-14 10:39:21 +11001964 const struct cred *cred = current_cred();
1965
Eric Paris2875fa02011-04-28 16:04:24 -04001966 return dentry_has_perm(cred, dentry, FILE__QUOTAON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967}
1968
Eric Paris12b30522010-11-15 18:36:29 -05001969static int selinux_syslog(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970{
1971 int rc;
1972
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 switch (type) {
Kees Cookd78ca3c2010-02-03 15:37:13 -08001974 case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */
1975 case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */
Eric Paris828dfe12008-04-17 13:17:49 -04001976 rc = task_has_system(current, SYSTEM__SYSLOG_READ);
1977 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001978 case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */
1979 case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */
1980 /* Set level of messages printed to console */
1981 case SYSLOG_ACTION_CONSOLE_LEVEL:
Eric Paris828dfe12008-04-17 13:17:49 -04001982 rc = task_has_system(current, SYSTEM__SYSLOG_CONSOLE);
1983 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08001984 case SYSLOG_ACTION_CLOSE: /* Close log */
1985 case SYSLOG_ACTION_OPEN: /* Open log */
1986 case SYSLOG_ACTION_READ: /* Read from log */
1987 case SYSLOG_ACTION_READ_CLEAR: /* Read/clear last kernel messages */
1988 case SYSLOG_ACTION_CLEAR: /* Clear ring buffer */
Eric Paris828dfe12008-04-17 13:17:49 -04001989 default:
1990 rc = task_has_system(current, SYSTEM__SYSLOG_MOD);
1991 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 }
1993 return rc;
1994}
1995
1996/*
1997 * Check that a process has enough memory to allocate a new virtual
1998 * mapping. 0 means there is enough memory for the allocation to
1999 * succeed and -ENOMEM implies there is not.
2000 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 * Do not audit the selinux permission check, as this is applied to all
2002 * processes that allocate mappings.
2003 */
Alan Cox34b4e4a2007-08-22 14:01:28 -07002004static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005{
2006 int rc, cap_sys_admin = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
Eric Paris6a9de492012-01-03 12:25:14 -05002008 rc = selinux_capable(current_cred(), &init_user_ns, CAP_SYS_ADMIN,
David Howells3699c532009-01-06 22:27:01 +00002009 SECURITY_CAP_NOAUDIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 if (rc == 0)
2011 cap_sys_admin = 1;
2012
Alan Cox34b4e4a2007-08-22 14:01:28 -07002013 return __vm_enough_memory(mm, pages, cap_sys_admin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014}
2015
2016/* binprm security operations */
2017
David Howellsa6f76f22008-11-14 10:39:24 +11002018static int selinux_bprm_set_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019{
David Howellsa6f76f22008-11-14 10:39:24 +11002020 const struct task_security_struct *old_tsec;
2021 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 struct inode_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04002023 struct common_audit_data ad;
Al Viro496ad9a2013-01-23 17:07:38 -05002024 struct inode *inode = file_inode(bprm->file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 int rc;
2026
Eric Paris200ac532009-02-12 15:01:04 -05002027 rc = cap_bprm_set_creds(bprm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 if (rc)
2029 return rc;
2030
David Howellsa6f76f22008-11-14 10:39:24 +11002031 /* SELinux context only depends on initial program or script and not
2032 * the script interpreter */
2033 if (bprm->cred_prepared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 return 0;
2035
David Howellsa6f76f22008-11-14 10:39:24 +11002036 old_tsec = current_security();
2037 new_tsec = bprm->cred->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 isec = inode->i_security;
2039
2040 /* Default to the current task SID. */
David Howellsa6f76f22008-11-14 10:39:24 +11002041 new_tsec->sid = old_tsec->sid;
2042 new_tsec->osid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
Michael LeMay28eba5b2006-06-27 02:53:42 -07002044 /* Reset fs, key, and sock SIDs on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002045 new_tsec->create_sid = 0;
2046 new_tsec->keycreate_sid = 0;
2047 new_tsec->sockcreate_sid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
David Howellsa6f76f22008-11-14 10:39:24 +11002049 if (old_tsec->exec_sid) {
2050 new_tsec->sid = old_tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 /* Reset exec SID on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002052 new_tsec->exec_sid = 0;
Andy Lutomirski259e5e62012-04-12 16:47:50 -05002053
2054 /*
2055 * Minimize confusion: if no_new_privs and a transition is
2056 * explicitly requested, then fail the exec.
2057 */
2058 if (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS)
2059 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 } else {
2061 /* Check for a default transition on this program. */
David Howellsa6f76f22008-11-14 10:39:24 +11002062 rc = security_transition_sid(old_tsec->sid, isec->sid,
Eric Paris652bb9b2011-02-01 11:05:40 -05002063 SECCLASS_PROCESS, NULL,
2064 &new_tsec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 if (rc)
2066 return rc;
2067 }
2068
Eric Paris50c205f2012-04-04 15:01:43 -04002069 ad.type = LSM_AUDIT_DATA_PATH;
Eric Parisf48b7392011-04-25 12:54:27 -04002070 ad.u.path = bprm->file->f_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
Andy Lutomirski259e5e62012-04-12 16:47:50 -05002072 if ((bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID) ||
2073 (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS))
David Howellsa6f76f22008-11-14 10:39:24 +11002074 new_tsec->sid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
David Howellsa6f76f22008-11-14 10:39:24 +11002076 if (new_tsec->sid == old_tsec->sid) {
2077 rc = avc_has_perm(old_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
2079 if (rc)
2080 return rc;
2081 } else {
2082 /* Check permissions for the transition. */
David Howellsa6f76f22008-11-14 10:39:24 +11002083 rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
2085 if (rc)
2086 return rc;
2087
David Howellsa6f76f22008-11-14 10:39:24 +11002088 rc = avc_has_perm(new_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
2090 if (rc)
2091 return rc;
2092
David Howellsa6f76f22008-11-14 10:39:24 +11002093 /* Check for shared state */
2094 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
2095 rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
2096 SECCLASS_PROCESS, PROCESS__SHARE,
2097 NULL);
2098 if (rc)
2099 return -EPERM;
2100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
David Howellsa6f76f22008-11-14 10:39:24 +11002102 /* Make sure that anyone attempting to ptrace over a task that
2103 * changes its SID has the appropriate permit */
2104 if (bprm->unsafe &
2105 (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
2106 struct task_struct *tracer;
2107 struct task_security_struct *sec;
2108 u32 ptsid = 0;
2109
2110 rcu_read_lock();
Tejun Heo06d98472011-06-17 16:50:40 +02002111 tracer = ptrace_parent(current);
David Howellsa6f76f22008-11-14 10:39:24 +11002112 if (likely(tracer != NULL)) {
2113 sec = __task_cred(tracer)->security;
2114 ptsid = sec->sid;
2115 }
2116 rcu_read_unlock();
2117
2118 if (ptsid != 0) {
2119 rc = avc_has_perm(ptsid, new_tsec->sid,
2120 SECCLASS_PROCESS,
2121 PROCESS__PTRACE, NULL);
2122 if (rc)
2123 return -EPERM;
2124 }
2125 }
2126
2127 /* Clear any possibly unsafe personality bits on exec: */
2128 bprm->per_clear |= PER_CLEAR_ON_SETID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 }
2130
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 return 0;
2132}
2133
Eric Paris828dfe12008-04-17 13:17:49 -04002134static int selinux_bprm_secureexec(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135{
Paul Moore5fb49872010-04-22 14:46:19 -04002136 const struct task_security_struct *tsec = current_security();
David Howells275bb412008-11-14 10:39:19 +11002137 u32 sid, osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 int atsecure = 0;
2139
David Howells275bb412008-11-14 10:39:19 +11002140 sid = tsec->sid;
2141 osid = tsec->osid;
2142
2143 if (osid != sid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 /* Enable secure mode for SIDs transitions unless
2145 the noatsecure permission is granted between
2146 the two SIDs, i.e. ahp returns 0. */
David Howells275bb412008-11-14 10:39:19 +11002147 atsecure = avc_has_perm(osid, sid,
David Howellsa6f76f22008-11-14 10:39:24 +11002148 SECCLASS_PROCESS,
2149 PROCESS__NOATSECURE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 }
2151
Eric Paris200ac532009-02-12 15:01:04 -05002152 return (atsecure || cap_bprm_secureexec(bprm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153}
2154
Al Viroc3c073f2012-08-21 22:32:06 -04002155static int match_file(const void *p, struct file *file, unsigned fd)
2156{
2157 return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0;
2158}
2159
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160/* Derived from fs/exec.c:flush_old_files. */
David Howells745ca242008-11-14 10:39:22 +11002161static inline void flush_unauthorized_files(const struct cred *cred,
2162 struct files_struct *files)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 struct file *file, *devnull = NULL;
Stephen Smalleyb20c8122006-09-25 23:32:03 -07002165 struct tty_struct *tty;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002166 int drop_tty = 0;
Al Viroc3c073f2012-08-21 22:32:06 -04002167 unsigned n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002169 tty = get_current_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 if (tty) {
Nick Pigginee2ffa02010-08-18 04:37:35 +10002171 spin_lock(&tty_files_lock);
Eric Paris37dd0bd2008-10-31 17:40:00 -04002172 if (!list_empty(&tty->tty_files)) {
Nick Piggind996b622010-08-18 04:37:36 +10002173 struct tty_file_private *file_priv;
Eric Paris37dd0bd2008-10-31 17:40:00 -04002174
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 /* Revalidate access to controlling tty.
Eric Paris602a8dd2012-04-04 15:01:42 -04002176 Use path_has_perm on the tty path directly rather
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 than using file_has_perm, as this particular open
2178 file may belong to another process and we are only
2179 interested in the inode-based check here. */
Nick Piggind996b622010-08-18 04:37:36 +10002180 file_priv = list_first_entry(&tty->tty_files,
2181 struct tty_file_private, list);
2182 file = file_priv->file;
Eric Paris602a8dd2012-04-04 15:01:42 -04002183 if (path_has_perm(cred, &file->f_path, FILE__READ | FILE__WRITE))
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002184 drop_tty = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 }
Nick Pigginee2ffa02010-08-18 04:37:35 +10002186 spin_unlock(&tty_files_lock);
Alan Cox452a00d2008-10-13 10:39:13 +01002187 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 }
Eric W. Biederman98a27ba2007-05-08 00:26:56 -07002189 /* Reset controlling tty. */
2190 if (drop_tty)
2191 no_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192
2193 /* Revalidate access to inherited open files. */
Al Viroc3c073f2012-08-21 22:32:06 -04002194 n = iterate_fd(files, 0, match_file, cred);
2195 if (!n) /* none found? */
2196 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
Al Viroc3c073f2012-08-21 22:32:06 -04002198 devnull = dentry_open(&selinux_null, O_RDWR, cred);
Al Viro45525b22012-10-16 13:30:07 -04002199 if (IS_ERR(devnull))
2200 devnull = NULL;
2201 /* replace all the matching ones with this */
2202 do {
2203 replace_fd(n - 1, devnull, 0);
2204 } while ((n = iterate_fd(files, n, match_file, cred)) != 0);
2205 if (devnull)
Al Viroc3c073f2012-08-21 22:32:06 -04002206 fput(devnull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207}
2208
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209/*
David Howellsa6f76f22008-11-14 10:39:24 +11002210 * Prepare a process for imminent new credential changes due to exec
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 */
David Howellsa6f76f22008-11-14 10:39:24 +11002212static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213{
David Howellsa6f76f22008-11-14 10:39:24 +11002214 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 struct rlimit *rlim, *initrlim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 int rc, i;
2217
David Howellsa6f76f22008-11-14 10:39:24 +11002218 new_tsec = bprm->cred->security;
2219 if (new_tsec->sid == new_tsec->osid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 return;
2221
2222 /* Close files for which the new task SID is not authorized. */
David Howellsa6f76f22008-11-14 10:39:24 +11002223 flush_unauthorized_files(bprm->cred, current->files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224
David Howellsa6f76f22008-11-14 10:39:24 +11002225 /* Always clear parent death signal on SID transitions. */
2226 current->pdeath_signal = 0;
2227
2228 /* Check whether the new SID can inherit resource limits from the old
2229 * SID. If not, reset all soft limits to the lower of the current
2230 * task's hard limit and the init task's soft limit.
2231 *
2232 * Note that the setting of hard limits (even to lower them) can be
2233 * controlled by the setrlimit check. The inclusion of the init task's
2234 * soft limit into the computation is to avoid resetting soft limits
2235 * higher than the default soft limit for cases where the default is
2236 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2237 */
2238 rc = avc_has_perm(new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
2239 PROCESS__RLIMITINH, NULL);
2240 if (rc) {
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002241 /* protect against do_prlimit() */
2242 task_lock(current);
David Howellsa6f76f22008-11-14 10:39:24 +11002243 for (i = 0; i < RLIM_NLIMITS; i++) {
2244 rlim = current->signal->rlim + i;
2245 initrlim = init_task.signal->rlim + i;
2246 rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
2247 }
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002248 task_unlock(current);
2249 update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
David Howellsa6f76f22008-11-14 10:39:24 +11002250 }
2251}
2252
2253/*
2254 * Clean up the process immediately after the installation of new credentials
2255 * due to exec
2256 */
2257static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
2258{
2259 const struct task_security_struct *tsec = current_security();
2260 struct itimerval itimer;
David Howellsa6f76f22008-11-14 10:39:24 +11002261 u32 osid, sid;
2262 int rc, i;
David Howellsa6f76f22008-11-14 10:39:24 +11002263
David Howellsa6f76f22008-11-14 10:39:24 +11002264 osid = tsec->osid;
2265 sid = tsec->sid;
2266
2267 if (sid == osid)
2268 return;
2269
2270 /* Check whether the new SID can inherit signal state from the old SID.
2271 * If not, clear itimers to avoid subsequent signal generation and
2272 * flush and unblock signals.
2273 *
2274 * This must occur _after_ the task SID has been updated so that any
2275 * kill done after the flush will be checked against the new SID.
2276 */
2277 rc = avc_has_perm(osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 if (rc) {
2279 memset(&itimer, 0, sizeof itimer);
2280 for (i = 0; i < 3; i++)
2281 do_setitimer(i, &itimer, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 spin_lock_irq(&current->sighand->siglock);
David Howells3bcac022009-04-29 13:45:05 +01002283 if (!(current->signal->flags & SIGNAL_GROUP_EXIT)) {
2284 __flush_signals(current);
2285 flush_signal_handlers(current, 1);
2286 sigemptyset(&current->blocked);
2287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 spin_unlock_irq(&current->sighand->siglock);
2289 }
2290
David Howellsa6f76f22008-11-14 10:39:24 +11002291 /* Wake up the parent if it is waiting so that it can recheck
2292 * wait permission to the new task SID. */
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002293 read_lock(&tasklist_lock);
Oleg Nesterov0b7570e2009-09-23 15:56:46 -07002294 __wake_up_parent(current, current->real_parent);
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002295 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296}
2297
2298/* superblock security operations */
2299
2300static int selinux_sb_alloc_security(struct super_block *sb)
2301{
2302 return superblock_alloc_security(sb);
2303}
2304
2305static void selinux_sb_free_security(struct super_block *sb)
2306{
2307 superblock_free_security(sb);
2308}
2309
2310static inline int match_prefix(char *prefix, int plen, char *option, int olen)
2311{
2312 if (plen > olen)
2313 return 0;
2314
2315 return !memcmp(prefix, option, plen);
2316}
2317
2318static inline int selinux_option(char *option, int len)
2319{
Eric Paris832cbd92008-04-01 13:24:09 -04002320 return (match_prefix(CONTEXT_STR, sizeof(CONTEXT_STR)-1, option, len) ||
2321 match_prefix(FSCONTEXT_STR, sizeof(FSCONTEXT_STR)-1, option, len) ||
2322 match_prefix(DEFCONTEXT_STR, sizeof(DEFCONTEXT_STR)-1, option, len) ||
David P. Quigley11689d42009-01-16 09:22:03 -05002323 match_prefix(ROOTCONTEXT_STR, sizeof(ROOTCONTEXT_STR)-1, option, len) ||
2324 match_prefix(LABELSUPP_STR, sizeof(LABELSUPP_STR)-1, option, len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325}
2326
2327static inline void take_option(char **to, char *from, int *first, int len)
2328{
2329 if (!*first) {
2330 **to = ',';
2331 *to += 1;
Cory Olmo3528a952006-09-29 01:58:44 -07002332 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 *first = 0;
2334 memcpy(*to, from, len);
2335 *to += len;
2336}
2337
Eric Paris828dfe12008-04-17 13:17:49 -04002338static inline void take_selinux_option(char **to, char *from, int *first,
2339 int len)
Cory Olmo3528a952006-09-29 01:58:44 -07002340{
2341 int current_size = 0;
2342
2343 if (!*first) {
2344 **to = '|';
2345 *to += 1;
Eric Paris828dfe12008-04-17 13:17:49 -04002346 } else
Cory Olmo3528a952006-09-29 01:58:44 -07002347 *first = 0;
2348
2349 while (current_size < len) {
2350 if (*from != '"') {
2351 **to = *from;
2352 *to += 1;
2353 }
2354 from += 1;
2355 current_size += 1;
2356 }
2357}
2358
Eric Parise0007522008-03-05 10:31:54 -05002359static int selinux_sb_copy_data(char *orig, char *copy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360{
2361 int fnosec, fsec, rc = 0;
2362 char *in_save, *in_curr, *in_end;
2363 char *sec_curr, *nosec_save, *nosec;
Cory Olmo3528a952006-09-29 01:58:44 -07002364 int open_quote = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
2366 in_curr = orig;
2367 sec_curr = copy;
2368
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 nosec = (char *)get_zeroed_page(GFP_KERNEL);
2370 if (!nosec) {
2371 rc = -ENOMEM;
2372 goto out;
2373 }
2374
2375 nosec_save = nosec;
2376 fnosec = fsec = 1;
2377 in_save = in_end = orig;
2378
2379 do {
Cory Olmo3528a952006-09-29 01:58:44 -07002380 if (*in_end == '"')
2381 open_quote = !open_quote;
2382 if ((*in_end == ',' && open_quote == 0) ||
2383 *in_end == '\0') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 int len = in_end - in_curr;
2385
2386 if (selinux_option(in_curr, len))
Cory Olmo3528a952006-09-29 01:58:44 -07002387 take_selinux_option(&sec_curr, in_curr, &fsec, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 else
2389 take_option(&nosec, in_curr, &fnosec, len);
2390
2391 in_curr = in_end + 1;
2392 }
2393 } while (*in_end++);
2394
Eric Paris6931dfc2005-06-30 02:58:51 -07002395 strcpy(in_save, nosec_save);
Gerald Schaeferda3caa22005-06-21 17:15:18 -07002396 free_page((unsigned long)nosec_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397out:
2398 return rc;
2399}
2400
Eric Paris026eb162011-03-03 16:09:14 -05002401static int selinux_sb_remount(struct super_block *sb, void *data)
2402{
2403 int rc, i, *flags;
2404 struct security_mnt_opts opts;
2405 char *secdata, **mount_options;
2406 struct superblock_security_struct *sbsec = sb->s_security;
2407
2408 if (!(sbsec->flags & SE_SBINITIALIZED))
2409 return 0;
2410
2411 if (!data)
2412 return 0;
2413
2414 if (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
2415 return 0;
2416
2417 security_init_mnt_opts(&opts);
2418 secdata = alloc_secdata();
2419 if (!secdata)
2420 return -ENOMEM;
2421 rc = selinux_sb_copy_data(data, secdata);
2422 if (rc)
2423 goto out_free_secdata;
2424
2425 rc = selinux_parse_opts_str(secdata, &opts);
2426 if (rc)
2427 goto out_free_secdata;
2428
2429 mount_options = opts.mnt_opts;
2430 flags = opts.mnt_opts_flags;
2431
2432 for (i = 0; i < opts.num_mnt_opts; i++) {
2433 u32 sid;
2434 size_t len;
2435
Eric Paris12f348b2012-10-09 10:56:25 -04002436 if (flags[i] == SBLABEL_MNT)
Eric Paris026eb162011-03-03 16:09:14 -05002437 continue;
2438 len = strlen(mount_options[i]);
2439 rc = security_context_to_sid(mount_options[i], len, &sid);
2440 if (rc) {
2441 printk(KERN_WARNING "SELinux: security_context_to_sid"
2442 "(%s) failed for (dev %s, type %s) errno=%d\n",
2443 mount_options[i], sb->s_id, sb->s_type->name, rc);
2444 goto out_free_opts;
2445 }
2446 rc = -EINVAL;
2447 switch (flags[i]) {
2448 case FSCONTEXT_MNT:
2449 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
2450 goto out_bad_option;
2451 break;
2452 case CONTEXT_MNT:
2453 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
2454 goto out_bad_option;
2455 break;
2456 case ROOTCONTEXT_MNT: {
2457 struct inode_security_struct *root_isec;
2458 root_isec = sb->s_root->d_inode->i_security;
2459
2460 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
2461 goto out_bad_option;
2462 break;
2463 }
2464 case DEFCONTEXT_MNT:
2465 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
2466 goto out_bad_option;
2467 break;
2468 default:
2469 goto out_free_opts;
2470 }
2471 }
2472
2473 rc = 0;
2474out_free_opts:
2475 security_free_mnt_opts(&opts);
2476out_free_secdata:
2477 free_secdata(secdata);
2478 return rc;
2479out_bad_option:
2480 printk(KERN_WARNING "SELinux: unable to change security options "
2481 "during remount (dev %s, type=%s)\n", sb->s_id,
2482 sb->s_type->name);
2483 goto out_free_opts;
2484}
2485
James Morris12204e22008-12-19 10:44:42 +11002486static int selinux_sb_kern_mount(struct super_block *sb, int flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487{
David Howells88e67f32008-11-14 10:39:21 +11002488 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002489 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 int rc;
2491
2492 rc = superblock_doinit(sb, data);
2493 if (rc)
2494 return rc;
2495
James Morris74192242008-12-19 11:41:10 +11002496 /* Allow all mounts performed by the kernel */
2497 if (flags & MS_KERNMOUNT)
2498 return 0;
2499
Eric Paris50c205f2012-04-04 15:01:43 -04002500 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002501 ad.u.dentry = sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002502 return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503}
2504
David Howells726c3342006-06-23 02:02:58 -07002505static int selinux_sb_statfs(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506{
David Howells88e67f32008-11-14 10:39:21 +11002507 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002508 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509
Eric Paris50c205f2012-04-04 15:01:43 -04002510 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002511 ad.u.dentry = dentry->d_sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002512 return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513}
2514
Al Viro808d4e32012-10-11 11:42:01 -04002515static int selinux_mount(const char *dev_name,
Al Virob5266eb2008-03-22 17:48:24 -04002516 struct path *path,
Al Viro808d4e32012-10-11 11:42:01 -04002517 const char *type,
Eric Paris828dfe12008-04-17 13:17:49 -04002518 unsigned long flags,
2519 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520{
David Howells88e67f32008-11-14 10:39:21 +11002521 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522
2523 if (flags & MS_REMOUNT)
Al Virod8c95842011-12-07 18:16:57 -05002524 return superblock_has_perm(cred, path->dentry->d_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002525 FILESYSTEM__REMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 else
Eric Paris2875fa02011-04-28 16:04:24 -04002527 return path_has_perm(cred, path, FILE__MOUNTON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528}
2529
2530static int selinux_umount(struct vfsmount *mnt, int flags)
2531{
David Howells88e67f32008-11-14 10:39:21 +11002532 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
David Howells88e67f32008-11-14 10:39:21 +11002534 return superblock_has_perm(cred, mnt->mnt_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002535 FILESYSTEM__UNMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536}
2537
2538/* inode security operations */
2539
2540static int selinux_inode_alloc_security(struct inode *inode)
2541{
2542 return inode_alloc_security(inode);
2543}
2544
2545static void selinux_inode_free_security(struct inode *inode)
2546{
2547 inode_free_security(inode);
2548}
2549
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002550static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
Eric Paris2a7dba32011-02-01 11:05:39 -05002551 const struct qstr *qstr, char **name,
2552 void **value, size_t *len)
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002553{
Paul Moore5fb49872010-04-22 14:46:19 -04002554 const struct task_security_struct *tsec = current_security();
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002555 struct inode_security_struct *dsec;
2556 struct superblock_security_struct *sbsec;
David Howells275bb412008-11-14 10:39:19 +11002557 u32 sid, newsid, clen;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002558 int rc;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002559 char *namep = NULL, *context;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002560
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002561 dsec = dir->i_security;
2562 sbsec = dir->i_sb->s_security;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002563
David Howells275bb412008-11-14 10:39:19 +11002564 sid = tsec->sid;
2565 newsid = tsec->create_sid;
2566
Eric Paris415103f2010-12-02 16:13:40 -05002567 if ((sbsec->flags & SE_SBINITIALIZED) &&
2568 (sbsec->behavior == SECURITY_FS_USE_MNTPOINT))
2569 newsid = sbsec->mntpoint_sid;
Eric Paris12f348b2012-10-09 10:56:25 -04002570 else if (!newsid || !(sbsec->flags & SBLABEL_MNT)) {
David Howells275bb412008-11-14 10:39:19 +11002571 rc = security_transition_sid(sid, dsec->sid,
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002572 inode_mode_to_security_class(inode->i_mode),
Eric Paris652bb9b2011-02-01 11:05:40 -05002573 qstr, &newsid);
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002574 if (rc) {
2575 printk(KERN_WARNING "%s: "
2576 "security_transition_sid failed, rc=%d (dev=%s "
2577 "ino=%ld)\n",
Harvey Harrisondd6f9532008-03-06 10:03:59 +11002578 __func__,
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002579 -rc, inode->i_sb->s_id, inode->i_ino);
2580 return rc;
2581 }
2582 }
2583
Eric Paris296fddf2006-09-25 23:32:00 -07002584 /* Possibly defer initialization to selinux_complete_init. */
David P. Quigley0d90a7e2009-01-16 09:22:02 -05002585 if (sbsec->flags & SE_SBINITIALIZED) {
Eric Paris296fddf2006-09-25 23:32:00 -07002586 struct inode_security_struct *isec = inode->i_security;
2587 isec->sclass = inode_mode_to_security_class(inode->i_mode);
2588 isec->sid = newsid;
2589 isec->initialized = 1;
2590 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002591
Eric Paris12f348b2012-10-09 10:56:25 -04002592 if (!ss_initialized || !(sbsec->flags & SBLABEL_MNT))
Stephen Smalley25a74f32005-11-08 21:34:33 -08002593 return -EOPNOTSUPP;
2594
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002595 if (name) {
Josef Bacika02fe132008-04-04 09:35:05 +11002596 namep = kstrdup(XATTR_SELINUX_SUFFIX, GFP_NOFS);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002597 if (!namep)
2598 return -ENOMEM;
2599 *name = namep;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002600 }
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002601
2602 if (value && len) {
Stephen Smalley12b29f32008-05-07 13:03:20 -04002603 rc = security_sid_to_context_force(newsid, &context, &clen);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002604 if (rc) {
2605 kfree(namep);
2606 return rc;
2607 }
2608 *value = context;
2609 *len = clen;
2610 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002611
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002612 return 0;
2613}
2614
Al Viro4acdaf22011-07-26 01:42:34 -04002615static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616{
2617 return may_create(dir, dentry, SECCLASS_FILE);
2618}
2619
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2621{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 return may_link(dir, old_dentry, MAY_LINK);
2623}
2624
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
2626{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 return may_link(dir, dentry, MAY_UNLINK);
2628}
2629
2630static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
2631{
2632 return may_create(dir, dentry, SECCLASS_LNK_FILE);
2633}
2634
Al Viro18bb1db2011-07-26 01:41:39 -04002635static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636{
2637 return may_create(dir, dentry, SECCLASS_DIR);
2638}
2639
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
2641{
2642 return may_link(dir, dentry, MAY_RMDIR);
2643}
2644
Al Viro1a67aaf2011-07-26 01:52:52 -04002645static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 return may_create(dir, dentry, inode_mode_to_security_class(mode));
2648}
2649
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
Eric Paris828dfe12008-04-17 13:17:49 -04002651 struct inode *new_inode, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652{
2653 return may_rename(old_inode, old_dentry, new_inode, new_dentry);
2654}
2655
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656static int selinux_inode_readlink(struct dentry *dentry)
2657{
David Howells88e67f32008-11-14 10:39:21 +11002658 const struct cred *cred = current_cred();
2659
Eric Paris2875fa02011-04-28 16:04:24 -04002660 return dentry_has_perm(cred, dentry, FILE__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661}
2662
2663static int selinux_inode_follow_link(struct dentry *dentry, struct nameidata *nameidata)
2664{
David Howells88e67f32008-11-14 10:39:21 +11002665 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666
Eric Paris2875fa02011-04-28 16:04:24 -04002667 return dentry_has_perm(cred, dentry, FILE__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668}
2669
Eric Parisd4cf970d2012-04-04 15:01:42 -04002670static noinline int audit_inode_permission(struct inode *inode,
2671 u32 perms, u32 audited, u32 denied,
2672 unsigned flags)
2673{
2674 struct common_audit_data ad;
Eric Parisd4cf970d2012-04-04 15:01:42 -04002675 struct inode_security_struct *isec = inode->i_security;
2676 int rc;
2677
Eric Paris50c205f2012-04-04 15:01:43 -04002678 ad.type = LSM_AUDIT_DATA_INODE;
Eric Parisd4cf970d2012-04-04 15:01:42 -04002679 ad.u.inode = inode;
2680
2681 rc = slow_avc_audit(current_sid(), isec->sid, isec->sclass, perms,
2682 audited, denied, &ad, flags);
2683 if (rc)
2684 return rc;
2685 return 0;
2686}
2687
Al Viroe74f71e2011-06-20 19:38:15 -04002688static int selinux_inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689{
David Howells88e67f32008-11-14 10:39:21 +11002690 const struct cred *cred = current_cred();
Eric Parisb782e0a2010-07-23 11:44:03 -04002691 u32 perms;
2692 bool from_access;
Al Virocf1dd1d2011-06-20 19:44:08 -04002693 unsigned flags = mask & MAY_NOT_BLOCK;
Eric Paris2e334052012-04-04 15:01:42 -04002694 struct inode_security_struct *isec;
2695 u32 sid;
2696 struct av_decision avd;
2697 int rc, rc2;
2698 u32 audited, denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699
Eric Parisb782e0a2010-07-23 11:44:03 -04002700 from_access = mask & MAY_ACCESS;
Eric Parisd09ca732010-07-23 11:43:57 -04002701 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
2702
Eric Parisb782e0a2010-07-23 11:44:03 -04002703 /* No permission to check. Existence test. */
2704 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706
Eric Paris2e334052012-04-04 15:01:42 -04002707 validate_creds(cred);
Eric Parisb782e0a2010-07-23 11:44:03 -04002708
Eric Paris2e334052012-04-04 15:01:42 -04002709 if (unlikely(IS_PRIVATE(inode)))
2710 return 0;
Eric Parisb782e0a2010-07-23 11:44:03 -04002711
2712 perms = file_mask_to_av(inode->i_mode, mask);
2713
Eric Paris2e334052012-04-04 15:01:42 -04002714 sid = cred_sid(cred);
2715 isec = inode->i_security;
2716
2717 rc = avc_has_perm_noaudit(sid, isec->sid, isec->sclass, perms, 0, &avd);
2718 audited = avc_audit_required(perms, &avd, rc,
2719 from_access ? FILE__AUDIT_ACCESS : 0,
2720 &denied);
2721 if (likely(!audited))
2722 return rc;
2723
Eric Parisd4cf970d2012-04-04 15:01:42 -04002724 rc2 = audit_inode_permission(inode, perms, audited, denied, flags);
Eric Paris2e334052012-04-04 15:01:42 -04002725 if (rc2)
2726 return rc2;
2727 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728}
2729
2730static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
2731{
David Howells88e67f32008-11-14 10:39:21 +11002732 const struct cred *cred = current_cred();
Amerigo Wangbc6a6002009-08-20 19:29:02 -07002733 unsigned int ia_valid = iattr->ia_valid;
Eric Paris95dbf732012-04-04 13:45:34 -04002734 __u32 av = FILE__WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735
Amerigo Wangbc6a6002009-08-20 19:29:02 -07002736 /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
2737 if (ia_valid & ATTR_FORCE) {
2738 ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
2739 ATTR_FORCE);
2740 if (!ia_valid)
2741 return 0;
2742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743
Amerigo Wangbc6a6002009-08-20 19:29:02 -07002744 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
2745 ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
Eric Paris2875fa02011-04-28 16:04:24 -04002746 return dentry_has_perm(cred, dentry, FILE__SETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747
Eric Paris3d2195c2012-07-06 14:13:30 -04002748 if (selinux_policycap_openperm && (ia_valid & ATTR_SIZE))
Eric Paris95dbf732012-04-04 13:45:34 -04002749 av |= FILE__OPEN;
2750
2751 return dentry_has_perm(cred, dentry, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752}
2753
2754static int selinux_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
2755{
David Howells88e67f32008-11-14 10:39:21 +11002756 const struct cred *cred = current_cred();
Eric Paris2875fa02011-04-28 16:04:24 -04002757 struct path path;
David Howells88e67f32008-11-14 10:39:21 +11002758
Eric Paris2875fa02011-04-28 16:04:24 -04002759 path.dentry = dentry;
2760 path.mnt = mnt;
2761
2762 return path_has_perm(cred, &path, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763}
2764
David Howells8f0cfa52008-04-29 00:59:41 -07002765static int selinux_inode_setotherxattr(struct dentry *dentry, const char *name)
Serge E. Hallynb5376772007-10-16 23:31:36 -07002766{
David Howells88e67f32008-11-14 10:39:21 +11002767 const struct cred *cred = current_cred();
2768
Serge E. Hallynb5376772007-10-16 23:31:36 -07002769 if (!strncmp(name, XATTR_SECURITY_PREFIX,
2770 sizeof XATTR_SECURITY_PREFIX - 1)) {
2771 if (!strcmp(name, XATTR_NAME_CAPS)) {
2772 if (!capable(CAP_SETFCAP))
2773 return -EPERM;
2774 } else if (!capable(CAP_SYS_ADMIN)) {
2775 /* A different attribute in the security namespace.
2776 Restrict to administrator. */
2777 return -EPERM;
2778 }
2779 }
2780
2781 /* Not an attribute we recognize, so just check the
2782 ordinary setattr permission. */
Eric Paris2875fa02011-04-28 16:04:24 -04002783 return dentry_has_perm(cred, dentry, FILE__SETATTR);
Serge E. Hallynb5376772007-10-16 23:31:36 -07002784}
2785
David Howells8f0cfa52008-04-29 00:59:41 -07002786static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
2787 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 struct inode *inode = dentry->d_inode;
2790 struct inode_security_struct *isec = inode->i_security;
2791 struct superblock_security_struct *sbsec;
Thomas Liu2bf49692009-07-14 12:14:09 -04002792 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11002793 u32 newsid, sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 int rc = 0;
2795
Serge E. Hallynb5376772007-10-16 23:31:36 -07002796 if (strcmp(name, XATTR_NAME_SELINUX))
2797 return selinux_inode_setotherxattr(dentry, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798
2799 sbsec = inode->i_sb->s_security;
Eric Paris12f348b2012-10-09 10:56:25 -04002800 if (!(sbsec->flags & SBLABEL_MNT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 return -EOPNOTSUPP;
2802
Serge E. Hallyn2e149672011-03-23 16:43:26 -07002803 if (!inode_owner_or_capable(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 return -EPERM;
2805
Eric Paris50c205f2012-04-04 15:01:43 -04002806 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002807 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808
David Howells275bb412008-11-14 10:39:19 +11002809 rc = avc_has_perm(sid, isec->sid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 FILE__RELABELFROM, &ad);
2811 if (rc)
2812 return rc;
2813
2814 rc = security_context_to_sid(value, size, &newsid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04002815 if (rc == -EINVAL) {
Eric Parisd6ea83e2012-04-04 13:45:49 -04002816 if (!capable(CAP_MAC_ADMIN)) {
2817 struct audit_buffer *ab;
2818 size_t audit_size;
2819 const char *str;
2820
2821 /* We strip a nul only if it is at the end, otherwise the
2822 * context contains a nul and we should audit that */
Al Viroe3fea3f2012-06-09 08:15:16 +01002823 if (value) {
2824 str = value;
2825 if (str[size - 1] == '\0')
2826 audit_size = size - 1;
2827 else
2828 audit_size = size;
2829 } else {
2830 str = "";
2831 audit_size = 0;
2832 }
Eric Parisd6ea83e2012-04-04 13:45:49 -04002833 ab = audit_log_start(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR);
2834 audit_log_format(ab, "op=setxattr invalid_context=");
2835 audit_log_n_untrustedstring(ab, value, audit_size);
2836 audit_log_end(ab);
2837
Stephen Smalley12b29f32008-05-07 13:03:20 -04002838 return rc;
Eric Parisd6ea83e2012-04-04 13:45:49 -04002839 }
Stephen Smalley12b29f32008-05-07 13:03:20 -04002840 rc = security_context_to_sid_force(value, size, &newsid);
2841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 if (rc)
2843 return rc;
2844
David Howells275bb412008-11-14 10:39:19 +11002845 rc = avc_has_perm(sid, newsid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 FILE__RELABELTO, &ad);
2847 if (rc)
2848 return rc;
2849
David Howells275bb412008-11-14 10:39:19 +11002850 rc = security_validate_transition(isec->sid, newsid, sid,
Eric Paris828dfe12008-04-17 13:17:49 -04002851 isec->sclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 if (rc)
2853 return rc;
2854
2855 return avc_has_perm(newsid,
2856 sbsec->sid,
2857 SECCLASS_FILESYSTEM,
2858 FILESYSTEM__ASSOCIATE,
2859 &ad);
2860}
2861
David Howells8f0cfa52008-04-29 00:59:41 -07002862static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
Eric Parisf5269712008-05-14 11:27:45 -04002863 const void *value, size_t size,
David Howells8f0cfa52008-04-29 00:59:41 -07002864 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865{
2866 struct inode *inode = dentry->d_inode;
2867 struct inode_security_struct *isec = inode->i_security;
2868 u32 newsid;
2869 int rc;
2870
2871 if (strcmp(name, XATTR_NAME_SELINUX)) {
2872 /* Not an attribute we recognize, so nothing to do. */
2873 return;
2874 }
2875
Stephen Smalley12b29f32008-05-07 13:03:20 -04002876 rc = security_context_to_sid_force(value, size, &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 if (rc) {
Stephen Smalley12b29f32008-05-07 13:03:20 -04002878 printk(KERN_ERR "SELinux: unable to map context to SID"
2879 "for (%s, %lu), rc=%d\n",
2880 inode->i_sb->s_id, inode->i_ino, -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 return;
2882 }
2883
2884 isec->sid = newsid;
2885 return;
2886}
2887
David Howells8f0cfa52008-04-29 00:59:41 -07002888static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889{
David Howells88e67f32008-11-14 10:39:21 +11002890 const struct cred *cred = current_cred();
2891
Eric Paris2875fa02011-04-28 16:04:24 -04002892 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893}
2894
Eric Paris828dfe12008-04-17 13:17:49 -04002895static int selinux_inode_listxattr(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896{
David Howells88e67f32008-11-14 10:39:21 +11002897 const struct cred *cred = current_cred();
2898
Eric Paris2875fa02011-04-28 16:04:24 -04002899 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900}
2901
David Howells8f0cfa52008-04-29 00:59:41 -07002902static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903{
Serge E. Hallynb5376772007-10-16 23:31:36 -07002904 if (strcmp(name, XATTR_NAME_SELINUX))
2905 return selinux_inode_setotherxattr(dentry, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906
2907 /* No one is allowed to remove a SELinux security label.
2908 You can change the label, but all data must be labeled. */
2909 return -EACCES;
2910}
2911
James Morrisd381d8a2005-10-30 14:59:22 -08002912/*
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04002913 * Copy the inode security context value to the user.
James Morrisd381d8a2005-10-30 14:59:22 -08002914 *
2915 * Permission check is handled by selinux_inode_getxattr hook.
2916 */
David P. Quigley42492592008-02-04 22:29:39 -08002917static int selinux_inode_getsecurity(const struct inode *inode, const char *name, void **buffer, bool alloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918{
David P. Quigley42492592008-02-04 22:29:39 -08002919 u32 size;
2920 int error;
2921 char *context = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 struct inode_security_struct *isec = inode->i_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00002924 if (strcmp(name, XATTR_SELINUX_SUFFIX))
2925 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04002927 /*
2928 * If the caller has CAP_MAC_ADMIN, then get the raw context
2929 * value even if it is not defined by current policy; otherwise,
2930 * use the in-core value under current policy.
2931 * Use the non-auditing forms of the permission checks since
2932 * getxattr may be called by unprivileged processes commonly
2933 * and lack of permission just means that we fall back to the
2934 * in-core context value, not a denial.
2935 */
Eric Paris6a9de492012-01-03 12:25:14 -05002936 error = selinux_capable(current_cred(), &init_user_ns, CAP_MAC_ADMIN,
David Howells3699c532009-01-06 22:27:01 +00002937 SECURITY_CAP_NOAUDIT);
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04002938 if (!error)
2939 error = security_sid_to_context_force(isec->sid, &context,
2940 &size);
2941 else
2942 error = security_sid_to_context(isec->sid, &context, &size);
David P. Quigley42492592008-02-04 22:29:39 -08002943 if (error)
2944 return error;
2945 error = size;
2946 if (alloc) {
2947 *buffer = context;
2948 goto out_nofree;
2949 }
2950 kfree(context);
2951out_nofree:
2952 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953}
2954
2955static int selinux_inode_setsecurity(struct inode *inode, const char *name,
Eric Paris828dfe12008-04-17 13:17:49 -04002956 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957{
2958 struct inode_security_struct *isec = inode->i_security;
2959 u32 newsid;
2960 int rc;
2961
2962 if (strcmp(name, XATTR_SELINUX_SUFFIX))
2963 return -EOPNOTSUPP;
2964
2965 if (!value || !size)
2966 return -EACCES;
2967
Eric Paris828dfe12008-04-17 13:17:49 -04002968 rc = security_context_to_sid((void *)value, size, &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 if (rc)
2970 return rc;
2971
2972 isec->sid = newsid;
David P. Quigleyddd29ec2009-09-09 14:25:37 -04002973 isec->initialized = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 return 0;
2975}
2976
2977static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
2978{
2979 const int len = sizeof(XATTR_NAME_SELINUX);
2980 if (buffer && len <= buffer_size)
2981 memcpy(buffer, XATTR_NAME_SELINUX, len);
2982 return len;
2983}
2984
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02002985static void selinux_inode_getsecid(const struct inode *inode, u32 *secid)
2986{
2987 struct inode_security_struct *isec = inode->i_security;
2988 *secid = isec->sid;
2989}
2990
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991/* file security operations */
2992
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09002993static int selinux_revalidate_file_permission(struct file *file, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994{
David Howells88e67f32008-11-14 10:39:21 +11002995 const struct cred *cred = current_cred();
Al Viro496ad9a2013-01-23 17:07:38 -05002996 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
2999 if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
3000 mask |= MAY_APPEND;
3001
Paul Moore389fb8002009-03-27 17:10:34 -04003002 return file_has_perm(cred, file,
3003 file_mask_to_av(inode->i_mode, mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004}
3005
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003006static int selinux_file_permission(struct file *file, int mask)
3007{
Al Viro496ad9a2013-01-23 17:07:38 -05003008 struct inode *inode = file_inode(file);
Stephen Smalley20dda182009-06-22 14:54:53 -04003009 struct file_security_struct *fsec = file->f_security;
3010 struct inode_security_struct *isec = inode->i_security;
3011 u32 sid = current_sid();
3012
Paul Moore389fb8002009-03-27 17:10:34 -04003013 if (!mask)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003014 /* No permission to check. Existence test. */
3015 return 0;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003016
Stephen Smalley20dda182009-06-22 14:54:53 -04003017 if (sid == fsec->sid && fsec->isid == isec->sid &&
3018 fsec->pseqno == avc_policy_seqno())
Eric Paris83d49852012-04-04 13:45:40 -04003019 /* No change since file_open check. */
Stephen Smalley20dda182009-06-22 14:54:53 -04003020 return 0;
3021
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003022 return selinux_revalidate_file_permission(file, mask);
3023}
3024
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025static int selinux_file_alloc_security(struct file *file)
3026{
3027 return file_alloc_security(file);
3028}
3029
3030static void selinux_file_free_security(struct file *file)
3031{
3032 file_free_security(file);
3033}
3034
3035static int selinux_file_ioctl(struct file *file, unsigned int cmd,
3036 unsigned long arg)
3037{
David Howells88e67f32008-11-14 10:39:21 +11003038 const struct cred *cred = current_cred();
Eric Paris0b24dcb2011-02-25 15:39:20 -05003039 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040
Eric Paris0b24dcb2011-02-25 15:39:20 -05003041 switch (cmd) {
3042 case FIONREAD:
3043 /* fall through */
3044 case FIBMAP:
3045 /* fall through */
3046 case FIGETBSZ:
3047 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003048 case FS_IOC_GETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003049 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003050 case FS_IOC_GETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003051 error = file_has_perm(cred, file, FILE__GETATTR);
3052 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053
Al Viro2f99c362012-03-23 16:04:05 -04003054 case FS_IOC_SETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003055 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003056 case FS_IOC_SETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003057 error = file_has_perm(cred, file, FILE__SETATTR);
3058 break;
3059
3060 /* sys_ioctl() checks */
3061 case FIONBIO:
3062 /* fall through */
3063 case FIOASYNC:
3064 error = file_has_perm(cred, file, 0);
3065 break;
3066
3067 case KDSKBENT:
3068 case KDSKBSENT:
Eric Paris6a9de492012-01-03 12:25:14 -05003069 error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
3070 SECURITY_CAP_AUDIT);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003071 break;
3072
3073 /* default case assumes that the command will go
3074 * to the file's ioctl() function.
3075 */
3076 default:
3077 error = file_has_perm(cred, file, FILE__IOCTL);
3078 }
3079 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080}
3081
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003082static int default_noexec;
3083
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
3085{
David Howells88e67f32008-11-14 10:39:21 +11003086 const struct cred *cred = current_cred();
David Howellsd84f4f92008-11-14 10:39:23 +11003087 int rc = 0;
David Howells88e67f32008-11-14 10:39:21 +11003088
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003089 if (default_noexec &&
3090 (prot & PROT_EXEC) && (!file || (!shared && (prot & PROT_WRITE)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 /*
3092 * We are making executable an anonymous mapping or a
3093 * private file mapping that will also be writable.
3094 * This has an additional check.
3095 */
David Howellsd84f4f92008-11-14 10:39:23 +11003096 rc = cred_has_perm(cred, cred, PROCESS__EXECMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 if (rc)
David Howellsd84f4f92008-11-14 10:39:23 +11003098 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100
3101 if (file) {
3102 /* read access is always possible with a mapping */
3103 u32 av = FILE__READ;
3104
3105 /* write access only matters if the mapping is shared */
3106 if (shared && (prot & PROT_WRITE))
3107 av |= FILE__WRITE;
3108
3109 if (prot & PROT_EXEC)
3110 av |= FILE__EXECUTE;
3111
David Howells88e67f32008-11-14 10:39:21 +11003112 return file_has_perm(cred, file, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 }
David Howellsd84f4f92008-11-14 10:39:23 +11003114
3115error:
3116 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117}
3118
Al Viroe5467852012-05-30 13:30:51 -04003119static int selinux_mmap_addr(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120{
Eric Parised032182007-06-28 15:55:21 -04003121 int rc = 0;
David Howells275bb412008-11-14 10:39:19 +11003122 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123
Eric Paris84336d1a2009-07-31 12:54:05 -04003124 /*
3125 * notice that we are intentionally putting the SELinux check before
3126 * the secondary cap_file_mmap check. This is such a likely attempt
3127 * at bad behaviour/exploit that we always want to get the AVC, even
3128 * if DAC would have also denied the operation.
3129 */
Eric Parisa2551df72009-07-31 12:54:11 -04003130 if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
Eric Parised032182007-06-28 15:55:21 -04003131 rc = avc_has_perm(sid, sid, SECCLASS_MEMPROTECT,
3132 MEMPROTECT__MMAP_ZERO, NULL);
Eric Paris84336d1a2009-07-31 12:54:05 -04003133 if (rc)
3134 return rc;
3135 }
3136
3137 /* do DAC check on address space usage */
Al Viroe5467852012-05-30 13:30:51 -04003138 return cap_mmap_addr(addr);
3139}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140
Al Viroe5467852012-05-30 13:30:51 -04003141static int selinux_mmap_file(struct file *file, unsigned long reqprot,
3142 unsigned long prot, unsigned long flags)
3143{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 if (selinux_checkreqprot)
3145 prot = reqprot;
3146
3147 return file_map_prot_check(file, prot,
3148 (flags & MAP_TYPE) == MAP_SHARED);
3149}
3150
3151static int selinux_file_mprotect(struct vm_area_struct *vma,
3152 unsigned long reqprot,
3153 unsigned long prot)
3154{
David Howells88e67f32008-11-14 10:39:21 +11003155 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156
3157 if (selinux_checkreqprot)
3158 prot = reqprot;
3159
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003160 if (default_noexec &&
3161 (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
James Morrisd541bbe2009-01-29 12:19:51 +11003162 int rc = 0;
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003163 if (vma->vm_start >= vma->vm_mm->start_brk &&
3164 vma->vm_end <= vma->vm_mm->brk) {
David Howellsd84f4f92008-11-14 10:39:23 +11003165 rc = cred_has_perm(cred, cred, PROCESS__EXECHEAP);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003166 } else if (!vma->vm_file &&
3167 vma->vm_start <= vma->vm_mm->start_stack &&
3168 vma->vm_end >= vma->vm_mm->start_stack) {
David Howells3b11a1d2008-11-14 10:39:26 +11003169 rc = current_has_perm(current, PROCESS__EXECSTACK);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003170 } else if (vma->vm_file && vma->anon_vma) {
3171 /*
3172 * We are making executable a file mapping that has
3173 * had some COW done. Since pages might have been
3174 * written, check ability to execute the possibly
3175 * modified content. This typically should only
3176 * occur for text relocations.
3177 */
David Howellsd84f4f92008-11-14 10:39:23 +11003178 rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003179 }
Lorenzo Hernandez García-Hierro6b992192005-06-25 14:54:34 -07003180 if (rc)
3181 return rc;
3182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183
3184 return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
3185}
3186
3187static int selinux_file_lock(struct file *file, unsigned int cmd)
3188{
David Howells88e67f32008-11-14 10:39:21 +11003189 const struct cred *cred = current_cred();
3190
3191 return file_has_perm(cred, file, FILE__LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192}
3193
3194static int selinux_file_fcntl(struct file *file, unsigned int cmd,
3195 unsigned long arg)
3196{
David Howells88e67f32008-11-14 10:39:21 +11003197 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 int err = 0;
3199
3200 switch (cmd) {
Eric Paris828dfe12008-04-17 13:17:49 -04003201 case F_SETFL:
Eric Paris828dfe12008-04-17 13:17:49 -04003202 if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
David Howells88e67f32008-11-14 10:39:21 +11003203 err = file_has_perm(cred, file, FILE__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003205 }
3206 /* fall through */
3207 case F_SETOWN:
3208 case F_SETSIG:
3209 case F_GETFL:
3210 case F_GETOWN:
3211 case F_GETSIG:
Cyrill Gorcunov1d151c32012-07-30 14:43:00 -07003212 case F_GETOWNER_UIDS:
Eric Paris828dfe12008-04-17 13:17:49 -04003213 /* Just check FD__USE permission */
David Howells88e67f32008-11-14 10:39:21 +11003214 err = file_has_perm(cred, file, 0);
Eric Paris828dfe12008-04-17 13:17:49 -04003215 break;
3216 case F_GETLK:
3217 case F_SETLK:
3218 case F_SETLKW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219#if BITS_PER_LONG == 32
Eric Paris828dfe12008-04-17 13:17:49 -04003220 case F_GETLK64:
3221 case F_SETLK64:
3222 case F_SETLKW64:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223#endif
David Howells88e67f32008-11-14 10:39:21 +11003224 err = file_has_perm(cred, file, FILE__LOCK);
Eric Paris828dfe12008-04-17 13:17:49 -04003225 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 }
3227
3228 return err;
3229}
3230
3231static int selinux_file_set_fowner(struct file *file)
3232{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 struct file_security_struct *fsec;
3234
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 fsec = file->f_security;
David Howells275bb412008-11-14 10:39:19 +11003236 fsec->fown_sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237
3238 return 0;
3239}
3240
3241static int selinux_file_send_sigiotask(struct task_struct *tsk,
3242 struct fown_struct *fown, int signum)
3243{
Eric Paris828dfe12008-04-17 13:17:49 -04003244 struct file *file;
Stephen Smalley65c90bc2009-05-04 15:43:18 -04003245 u32 sid = task_sid(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 struct file_security_struct *fsec;
3248
3249 /* struct fown_struct is never outside the context of a struct file */
Eric Paris828dfe12008-04-17 13:17:49 -04003250 file = container_of(fown, struct file, f_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 fsec = file->f_security;
3253
3254 if (!signum)
3255 perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
3256 else
3257 perm = signal_to_av(signum);
3258
David Howells275bb412008-11-14 10:39:19 +11003259 return avc_has_perm(fsec->fown_sid, sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 SECCLASS_PROCESS, perm, NULL);
3261}
3262
3263static int selinux_file_receive(struct file *file)
3264{
David Howells88e67f32008-11-14 10:39:21 +11003265 const struct cred *cred = current_cred();
3266
3267 return file_has_perm(cred, file, file_to_av(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268}
3269
Eric Paris83d49852012-04-04 13:45:40 -04003270static int selinux_file_open(struct file *file, const struct cred *cred)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003271{
3272 struct file_security_struct *fsec;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003273 struct inode_security_struct *isec;
David Howellsd84f4f92008-11-14 10:39:23 +11003274
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003275 fsec = file->f_security;
Al Viro496ad9a2013-01-23 17:07:38 -05003276 isec = file_inode(file)->i_security;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003277 /*
3278 * Save inode label and policy sequence number
3279 * at open-time so that selinux_file_permission
3280 * can determine whether revalidation is necessary.
3281 * Task label is already saved in the file security
3282 * struct as its SID.
3283 */
3284 fsec->isid = isec->sid;
3285 fsec->pseqno = avc_policy_seqno();
3286 /*
3287 * Since the inode label or policy seqno may have changed
3288 * between the selinux_inode_permission check and the saving
3289 * of state above, recheck that access is still permitted.
3290 * Otherwise, access might never be revalidated against the
3291 * new inode label or new policy.
3292 * This check is not redundant - do not remove.
3293 */
Eric Paris602a8dd2012-04-04 15:01:42 -04003294 return path_has_perm(cred, &file->f_path, open_file_to_av(file));
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003295}
3296
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297/* task security operations */
3298
3299static int selinux_task_create(unsigned long clone_flags)
3300{
David Howells3b11a1d2008-11-14 10:39:26 +11003301 return current_has_perm(current, PROCESS__FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302}
3303
David Howellsf1752ee2008-11-14 10:39:17 +11003304/*
David Howellsee18d642009-09-02 09:14:21 +01003305 * allocate the SELinux part of blank credentials
3306 */
3307static int selinux_cred_alloc_blank(struct cred *cred, gfp_t gfp)
3308{
3309 struct task_security_struct *tsec;
3310
3311 tsec = kzalloc(sizeof(struct task_security_struct), gfp);
3312 if (!tsec)
3313 return -ENOMEM;
3314
3315 cred->security = tsec;
3316 return 0;
3317}
3318
3319/*
David Howellsf1752ee2008-11-14 10:39:17 +11003320 * detach and free the LSM part of a set of credentials
3321 */
3322static void selinux_cred_free(struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323{
David Howellsf1752ee2008-11-14 10:39:17 +11003324 struct task_security_struct *tsec = cred->security;
David Howellse0e81732009-09-02 09:13:40 +01003325
Tetsuo Handa2edeaa32011-02-07 13:36:10 +00003326 /*
3327 * cred->security == NULL if security_cred_alloc_blank() or
3328 * security_prepare_creds() returned an error.
3329 */
3330 BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE);
David Howellse0e81732009-09-02 09:13:40 +01003331 cred->security = (void *) 0x7UL;
David Howellsf1752ee2008-11-14 10:39:17 +11003332 kfree(tsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333}
3334
David Howellsd84f4f92008-11-14 10:39:23 +11003335/*
3336 * prepare a new set of credentials for modification
3337 */
3338static int selinux_cred_prepare(struct cred *new, const struct cred *old,
3339 gfp_t gfp)
3340{
3341 const struct task_security_struct *old_tsec;
3342 struct task_security_struct *tsec;
3343
3344 old_tsec = old->security;
3345
3346 tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp);
3347 if (!tsec)
3348 return -ENOMEM;
3349
3350 new->security = tsec;
3351 return 0;
3352}
3353
3354/*
David Howellsee18d642009-09-02 09:14:21 +01003355 * transfer the SELinux data to a blank set of creds
3356 */
3357static void selinux_cred_transfer(struct cred *new, const struct cred *old)
3358{
3359 const struct task_security_struct *old_tsec = old->security;
3360 struct task_security_struct *tsec = new->security;
3361
3362 *tsec = *old_tsec;
3363}
3364
3365/*
David Howells3a3b7ce2008-11-14 10:39:28 +11003366 * set the security data for a kernel service
3367 * - all the creation contexts are set to unlabelled
3368 */
3369static int selinux_kernel_act_as(struct cred *new, u32 secid)
3370{
3371 struct task_security_struct *tsec = new->security;
3372 u32 sid = current_sid();
3373 int ret;
3374
3375 ret = avc_has_perm(sid, secid,
3376 SECCLASS_KERNEL_SERVICE,
3377 KERNEL_SERVICE__USE_AS_OVERRIDE,
3378 NULL);
3379 if (ret == 0) {
3380 tsec->sid = secid;
3381 tsec->create_sid = 0;
3382 tsec->keycreate_sid = 0;
3383 tsec->sockcreate_sid = 0;
3384 }
3385 return ret;
3386}
3387
3388/*
3389 * set the file creation context in a security record to the same as the
3390 * objective context of the specified inode
3391 */
3392static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
3393{
3394 struct inode_security_struct *isec = inode->i_security;
3395 struct task_security_struct *tsec = new->security;
3396 u32 sid = current_sid();
3397 int ret;
3398
3399 ret = avc_has_perm(sid, isec->sid,
3400 SECCLASS_KERNEL_SERVICE,
3401 KERNEL_SERVICE__CREATE_FILES_AS,
3402 NULL);
3403
3404 if (ret == 0)
3405 tsec->create_sid = isec->sid;
David Howellsef574712010-02-26 01:56:16 +00003406 return ret;
David Howells3a3b7ce2008-11-14 10:39:28 +11003407}
3408
Eric Parisdd8dbf22009-11-03 16:35:32 +11003409static int selinux_kernel_module_request(char *kmod_name)
Eric Paris25354c42009-08-13 09:45:03 -04003410{
Eric Parisdd8dbf22009-11-03 16:35:32 +11003411 u32 sid;
3412 struct common_audit_data ad;
3413
3414 sid = task_sid(current);
3415
Eric Paris50c205f2012-04-04 15:01:43 -04003416 ad.type = LSM_AUDIT_DATA_KMOD;
Eric Parisdd8dbf22009-11-03 16:35:32 +11003417 ad.u.kmod_name = kmod_name;
3418
3419 return avc_has_perm(sid, SECINITSID_KERNEL, SECCLASS_SYSTEM,
3420 SYSTEM__MODULE_REQUEST, &ad);
Eric Paris25354c42009-08-13 09:45:03 -04003421}
3422
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
3424{
David Howells3b11a1d2008-11-14 10:39:26 +11003425 return current_has_perm(p, PROCESS__SETPGID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426}
3427
3428static int selinux_task_getpgid(struct task_struct *p)
3429{
David Howells3b11a1d2008-11-14 10:39:26 +11003430 return current_has_perm(p, PROCESS__GETPGID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431}
3432
3433static int selinux_task_getsid(struct task_struct *p)
3434{
David Howells3b11a1d2008-11-14 10:39:26 +11003435 return current_has_perm(p, PROCESS__GETSESSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436}
3437
David Quigleyf9008e4c2006-06-30 01:55:46 -07003438static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
3439{
David Howells275bb412008-11-14 10:39:19 +11003440 *secid = task_sid(p);
David Quigleyf9008e4c2006-06-30 01:55:46 -07003441}
3442
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443static int selinux_task_setnice(struct task_struct *p, int nice)
3444{
3445 int rc;
3446
Eric Paris200ac532009-02-12 15:01:04 -05003447 rc = cap_task_setnice(p, nice);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 if (rc)
3449 return rc;
3450
David Howells3b11a1d2008-11-14 10:39:26 +11003451 return current_has_perm(p, PROCESS__SETSCHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452}
3453
James Morris03e68062006-06-23 02:03:58 -07003454static int selinux_task_setioprio(struct task_struct *p, int ioprio)
3455{
Serge E. Hallynb5376772007-10-16 23:31:36 -07003456 int rc;
3457
Eric Paris200ac532009-02-12 15:01:04 -05003458 rc = cap_task_setioprio(p, ioprio);
Serge E. Hallynb5376772007-10-16 23:31:36 -07003459 if (rc)
3460 return rc;
3461
David Howells3b11a1d2008-11-14 10:39:26 +11003462 return current_has_perm(p, PROCESS__SETSCHED);
James Morris03e68062006-06-23 02:03:58 -07003463}
3464
David Quigleya1836a42006-06-30 01:55:49 -07003465static int selinux_task_getioprio(struct task_struct *p)
3466{
David Howells3b11a1d2008-11-14 10:39:26 +11003467 return current_has_perm(p, PROCESS__GETSCHED);
David Quigleya1836a42006-06-30 01:55:49 -07003468}
3469
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003470static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
3471 struct rlimit *new_rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472{
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003473 struct rlimit *old_rlim = p->signal->rlim + resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474
3475 /* Control the ability to change the hard limit (whether
3476 lowering or raising it), so that the hard limit can
3477 later be used as a safe reset point for the soft limit
David Howellsd84f4f92008-11-14 10:39:23 +11003478 upon context transitions. See selinux_bprm_committing_creds. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 if (old_rlim->rlim_max != new_rlim->rlim_max)
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003480 return current_has_perm(p, PROCESS__SETRLIMIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481
3482 return 0;
3483}
3484
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09003485static int selinux_task_setscheduler(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486{
Serge E. Hallynb5376772007-10-16 23:31:36 -07003487 int rc;
3488
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09003489 rc = cap_task_setscheduler(p);
Serge E. Hallynb5376772007-10-16 23:31:36 -07003490 if (rc)
3491 return rc;
3492
David Howells3b11a1d2008-11-14 10:39:26 +11003493 return current_has_perm(p, PROCESS__SETSCHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494}
3495
3496static int selinux_task_getscheduler(struct task_struct *p)
3497{
David Howells3b11a1d2008-11-14 10:39:26 +11003498 return current_has_perm(p, PROCESS__GETSCHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499}
3500
David Quigley35601542006-06-23 02:04:01 -07003501static int selinux_task_movememory(struct task_struct *p)
3502{
David Howells3b11a1d2008-11-14 10:39:26 +11003503 return current_has_perm(p, PROCESS__SETSCHED);
David Quigley35601542006-06-23 02:04:01 -07003504}
3505
David Quigleyf9008e4c2006-06-30 01:55:46 -07003506static int selinux_task_kill(struct task_struct *p, struct siginfo *info,
3507 int sig, u32 secid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508{
3509 u32 perm;
3510 int rc;
3511
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 if (!sig)
3513 perm = PROCESS__SIGNULL; /* null signal; existence test */
3514 else
3515 perm = signal_to_av(sig);
David Quigleyf9008e4c2006-06-30 01:55:46 -07003516 if (secid)
David Howells275bb412008-11-14 10:39:19 +11003517 rc = avc_has_perm(secid, task_sid(p),
3518 SECCLASS_PROCESS, perm, NULL);
David Quigleyf9008e4c2006-06-30 01:55:46 -07003519 else
David Howells3b11a1d2008-11-14 10:39:26 +11003520 rc = current_has_perm(p, perm);
David Quigleyf9008e4c2006-06-30 01:55:46 -07003521 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522}
3523
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524static int selinux_task_wait(struct task_struct *p)
3525{
Eric Paris8a535142007-10-22 16:10:31 -04003526 return task_has_perm(p, current, PROCESS__SIGCHLD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527}
3528
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529static void selinux_task_to_inode(struct task_struct *p,
3530 struct inode *inode)
3531{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 struct inode_security_struct *isec = inode->i_security;
David Howells275bb412008-11-14 10:39:19 +11003533 u32 sid = task_sid(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534
David Howells275bb412008-11-14 10:39:19 +11003535 isec->sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 isec->initialized = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537}
3538
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003540static int selinux_parse_skb_ipv4(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04003541 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542{
3543 int offset, ihlen, ret = -EINVAL;
3544 struct iphdr _iph, *ih;
3545
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03003546 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
3548 if (ih == NULL)
3549 goto out;
3550
3551 ihlen = ih->ihl * 4;
3552 if (ihlen < sizeof(_iph))
3553 goto out;
3554
Eric Paris48c62af2012-04-02 13:15:44 -04003555 ad->u.net->v4info.saddr = ih->saddr;
3556 ad->u.net->v4info.daddr = ih->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 ret = 0;
3558
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003559 if (proto)
3560 *proto = ih->protocol;
3561
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 switch (ih->protocol) {
Eric Paris828dfe12008-04-17 13:17:49 -04003563 case IPPROTO_TCP: {
3564 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565
Eric Paris828dfe12008-04-17 13:17:49 -04003566 if (ntohs(ih->frag_off) & IP_OFFSET)
3567 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568
3569 offset += ihlen;
3570 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3571 if (th == NULL)
3572 break;
3573
Eric Paris48c62af2012-04-02 13:15:44 -04003574 ad->u.net->sport = th->source;
3575 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578
Eric Paris828dfe12008-04-17 13:17:49 -04003579 case IPPROTO_UDP: {
3580 struct udphdr _udph, *uh;
3581
3582 if (ntohs(ih->frag_off) & IP_OFFSET)
3583 break;
3584
3585 offset += ihlen;
3586 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3587 if (uh == NULL)
3588 break;
3589
Eric Paris48c62af2012-04-02 13:15:44 -04003590 ad->u.net->sport = uh->source;
3591 ad->u.net->dport = uh->dest;
Eric Paris828dfe12008-04-17 13:17:49 -04003592 break;
3593 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594
James Morris2ee92d42006-11-13 16:09:01 -08003595 case IPPROTO_DCCP: {
3596 struct dccp_hdr _dccph, *dh;
3597
3598 if (ntohs(ih->frag_off) & IP_OFFSET)
3599 break;
3600
3601 offset += ihlen;
3602 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3603 if (dh == NULL)
3604 break;
3605
Eric Paris48c62af2012-04-02 13:15:44 -04003606 ad->u.net->sport = dh->dccph_sport;
3607 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08003608 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003609 }
James Morris2ee92d42006-11-13 16:09:01 -08003610
Eric Paris828dfe12008-04-17 13:17:49 -04003611 default:
3612 break;
3613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614out:
3615 return ret;
3616}
3617
3618#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3619
3620/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003621static int selinux_parse_skb_ipv6(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04003622 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623{
3624 u8 nexthdr;
3625 int ret = -EINVAL, offset;
3626 struct ipv6hdr _ipv6h, *ip6;
Jesse Gross75f28112011-11-30 17:05:51 -08003627 __be16 frag_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03003629 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3631 if (ip6 == NULL)
3632 goto out;
3633
Eric Paris48c62af2012-04-02 13:15:44 -04003634 ad->u.net->v6info.saddr = ip6->saddr;
3635 ad->u.net->v6info.daddr = ip6->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 ret = 0;
3637
3638 nexthdr = ip6->nexthdr;
3639 offset += sizeof(_ipv6h);
Jesse Gross75f28112011-11-30 17:05:51 -08003640 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 if (offset < 0)
3642 goto out;
3643
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003644 if (proto)
3645 *proto = nexthdr;
3646
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 switch (nexthdr) {
3648 case IPPROTO_TCP: {
Eric Paris828dfe12008-04-17 13:17:49 -04003649 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650
3651 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3652 if (th == NULL)
3653 break;
3654
Eric Paris48c62af2012-04-02 13:15:44 -04003655 ad->u.net->sport = th->source;
3656 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 break;
3658 }
3659
3660 case IPPROTO_UDP: {
3661 struct udphdr _udph, *uh;
3662
3663 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3664 if (uh == NULL)
3665 break;
3666
Eric Paris48c62af2012-04-02 13:15:44 -04003667 ad->u.net->sport = uh->source;
3668 ad->u.net->dport = uh->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 break;
3670 }
3671
James Morris2ee92d42006-11-13 16:09:01 -08003672 case IPPROTO_DCCP: {
3673 struct dccp_hdr _dccph, *dh;
3674
3675 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3676 if (dh == NULL)
3677 break;
3678
Eric Paris48c62af2012-04-02 13:15:44 -04003679 ad->u.net->sport = dh->dccph_sport;
3680 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08003681 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003682 }
James Morris2ee92d42006-11-13 16:09:01 -08003683
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 /* includes fragments */
3685 default:
3686 break;
3687 }
3688out:
3689 return ret;
3690}
3691
3692#endif /* IPV6 */
3693
Thomas Liu2bf49692009-07-14 12:14:09 -04003694static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
David Howellscf9481e2008-07-27 21:31:07 +10003695 char **_addrp, int src, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696{
David Howellscf9481e2008-07-27 21:31:07 +10003697 char *addrp;
3698 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699
Eric Paris48c62af2012-04-02 13:15:44 -04003700 switch (ad->u.net->family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 case PF_INET:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003702 ret = selinux_parse_skb_ipv4(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10003703 if (ret)
3704 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04003705 addrp = (char *)(src ? &ad->u.net->v4info.saddr :
3706 &ad->u.net->v4info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10003707 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708
3709#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3710 case PF_INET6:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003711 ret = selinux_parse_skb_ipv6(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10003712 if (ret)
3713 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04003714 addrp = (char *)(src ? &ad->u.net->v6info.saddr :
3715 &ad->u.net->v6info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10003716 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717#endif /* IPV6 */
3718 default:
David Howellscf9481e2008-07-27 21:31:07 +10003719 addrp = NULL;
3720 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 }
3722
David Howellscf9481e2008-07-27 21:31:07 +10003723parse_error:
3724 printk(KERN_WARNING
3725 "SELinux: failure in selinux_parse_skb(),"
3726 " unable to parse packet\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 return ret;
David Howellscf9481e2008-07-27 21:31:07 +10003728
3729okay:
3730 if (_addrp)
3731 *_addrp = addrp;
3732 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733}
3734
Paul Moore4f6a9932007-03-01 14:35:22 -05003735/**
Paul Moore220deb92008-01-29 08:38:23 -05003736 * selinux_skb_peerlbl_sid - Determine the peer label of a packet
Paul Moore4f6a9932007-03-01 14:35:22 -05003737 * @skb: the packet
Paul Moore75e22912008-01-29 08:38:04 -05003738 * @family: protocol family
Paul Moore220deb92008-01-29 08:38:23 -05003739 * @sid: the packet's peer label SID
Paul Moore4f6a9932007-03-01 14:35:22 -05003740 *
3741 * Description:
Paul Moore220deb92008-01-29 08:38:23 -05003742 * Check the various different forms of network peer labeling and determine
3743 * the peer label/SID for the packet; most of the magic actually occurs in
3744 * the security server function security_net_peersid_cmp(). The function
3745 * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
3746 * or -EACCES if @sid is invalid due to inconsistencies with the different
3747 * peer labels.
Paul Moore4f6a9932007-03-01 14:35:22 -05003748 *
3749 */
Paul Moore220deb92008-01-29 08:38:23 -05003750static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
Paul Moore4f6a9932007-03-01 14:35:22 -05003751{
Paul Moore71f1cb02008-01-29 08:51:16 -05003752 int err;
Paul Moore4f6a9932007-03-01 14:35:22 -05003753 u32 xfrm_sid;
3754 u32 nlbl_sid;
Paul Moore220deb92008-01-29 08:38:23 -05003755 u32 nlbl_type;
Paul Moore4f6a9932007-03-01 14:35:22 -05003756
Paul Moorebed4d7e2013-07-23 17:38:40 -04003757 err = selinux_skb_xfrm_sid(skb, &xfrm_sid);
3758 if (unlikely(err))
3759 return -EACCES;
3760 err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
3761 if (unlikely(err))
3762 return -EACCES;
Paul Moore220deb92008-01-29 08:38:23 -05003763
Paul Moore71f1cb02008-01-29 08:51:16 -05003764 err = security_net_peersid_resolve(nlbl_sid, nlbl_type, xfrm_sid, sid);
3765 if (unlikely(err)) {
3766 printk(KERN_WARNING
3767 "SELinux: failure in selinux_skb_peerlbl_sid(),"
3768 " unable to determine packet's peer label\n");
Paul Moore220deb92008-01-29 08:38:23 -05003769 return -EACCES;
Paul Moore71f1cb02008-01-29 08:51:16 -05003770 }
Paul Moore220deb92008-01-29 08:38:23 -05003771
3772 return 0;
Paul Moore4f6a9932007-03-01 14:35:22 -05003773}
3774
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775/* socket security operations */
Paul Moored4f2d972010-04-22 14:46:18 -04003776
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003777static int socket_sockcreate_sid(const struct task_security_struct *tsec,
3778 u16 secclass, u32 *socksid)
Paul Moored4f2d972010-04-22 14:46:18 -04003779{
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003780 if (tsec->sockcreate_sid > SECSID_NULL) {
3781 *socksid = tsec->sockcreate_sid;
3782 return 0;
3783 }
3784
3785 return security_transition_sid(tsec->sid, tsec->sid, secclass, NULL,
3786 socksid);
Paul Moored4f2d972010-04-22 14:46:18 -04003787}
3788
Paul Moore253bfae2010-04-22 14:46:19 -04003789static int sock_has_perm(struct task_struct *task, struct sock *sk, u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790{
Paul Moore253bfae2010-04-22 14:46:19 -04003791 struct sk_security_struct *sksec = sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04003792 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04003793 struct lsm_network_audit net = {0,};
Paul Moore253bfae2010-04-22 14:46:19 -04003794 u32 tsid = task_sid(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795
Paul Moore253bfae2010-04-22 14:46:19 -04003796 if (sksec->sid == SECINITSID_KERNEL)
3797 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798
Eric Paris50c205f2012-04-04 15:01:43 -04003799 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04003800 ad.u.net = &net;
3801 ad.u.net->sk = sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802
Paul Moore253bfae2010-04-22 14:46:19 -04003803 return avc_has_perm(tsid, sksec->sid, sksec->sclass, perms, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804}
3805
3806static int selinux_socket_create(int family, int type,
3807 int protocol, int kern)
3808{
Paul Moore5fb49872010-04-22 14:46:19 -04003809 const struct task_security_struct *tsec = current_security();
Paul Moored4f2d972010-04-22 14:46:18 -04003810 u32 newsid;
David Howells275bb412008-11-14 10:39:19 +11003811 u16 secclass;
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003812 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813
3814 if (kern)
Paul Moored4f2d972010-04-22 14:46:18 -04003815 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816
David Howells275bb412008-11-14 10:39:19 +11003817 secclass = socket_type_to_security_class(family, type, protocol);
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003818 rc = socket_sockcreate_sid(tsec, secclass, &newsid);
3819 if (rc)
3820 return rc;
3821
Paul Moored4f2d972010-04-22 14:46:18 -04003822 return avc_has_perm(tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823}
3824
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003825static int selinux_socket_post_create(struct socket *sock, int family,
3826 int type, int protocol, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827{
Paul Moore5fb49872010-04-22 14:46:19 -04003828 const struct task_security_struct *tsec = current_security();
Paul Moored4f2d972010-04-22 14:46:18 -04003829 struct inode_security_struct *isec = SOCK_INODE(sock)->i_security;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07003830 struct sk_security_struct *sksec;
David Howells275bb412008-11-14 10:39:19 +11003831 int err = 0;
3832
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003833 isec->sclass = socket_type_to_security_class(family, type, protocol);
3834
David Howells275bb412008-11-14 10:39:19 +11003835 if (kern)
3836 isec->sid = SECINITSID_KERNEL;
Harry Ciao2ad18bd2011-03-02 13:32:34 +08003837 else {
3838 err = socket_sockcreate_sid(tsec, isec->sclass, &(isec->sid));
3839 if (err)
3840 return err;
3841 }
David Howells275bb412008-11-14 10:39:19 +11003842
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 isec->initialized = 1;
3844
Venkat Yekkirala892c1412006-08-04 23:08:56 -07003845 if (sock->sk) {
3846 sksec = sock->sk->sk_security;
3847 sksec->sid = isec->sid;
Paul Moore220deb92008-01-29 08:38:23 -05003848 sksec->sclass = isec->sclass;
Paul Moore389fb8002009-03-27 17:10:34 -04003849 err = selinux_netlbl_socket_post_create(sock->sk, family);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07003850 }
3851
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003852 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853}
3854
3855/* Range of port numbers used to automatically bind.
3856 Need to determine whether we should perform a name_bind
3857 permission check between the socket and the port number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858
3859static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
3860{
Paul Moore253bfae2010-04-22 14:46:19 -04003861 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 u16 family;
3863 int err;
3864
Paul Moore253bfae2010-04-22 14:46:19 -04003865 err = sock_has_perm(current, sk, SOCKET__BIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 if (err)
3867 goto out;
3868
3869 /*
3870 * If PF_INET or PF_INET6, check name_bind permission for the port.
James Morris13402582005-09-30 14:24:34 -04003871 * Multiple address binding for SCTP is not supported yet: we just
3872 * check the first address now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 */
Paul Moore253bfae2010-04-22 14:46:19 -04003874 family = sk->sk_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 if (family == PF_INET || family == PF_INET6) {
3876 char *addrp;
Paul Moore253bfae2010-04-22 14:46:19 -04003877 struct sk_security_struct *sksec = sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04003878 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04003879 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 struct sockaddr_in *addr4 = NULL;
3881 struct sockaddr_in6 *addr6 = NULL;
3882 unsigned short snum;
James Morrise399f982008-06-12 01:39:58 +10003883 u32 sid, node_perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 if (family == PF_INET) {
3886 addr4 = (struct sockaddr_in *)address;
3887 snum = ntohs(addr4->sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 addrp = (char *)&addr4->sin_addr.s_addr;
3889 } else {
3890 addr6 = (struct sockaddr_in6 *)address;
3891 snum = ntohs(addr6->sin6_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 addrp = (char *)&addr6->sin6_addr.s6_addr;
3893 }
3894
Stephen Hemminger227b60f2007-10-10 17:30:46 -07003895 if (snum) {
3896 int low, high;
3897
3898 inet_get_local_port_range(&low, &high);
3899
3900 if (snum < max(PROT_SOCK, low) || snum > high) {
Paul Moore3e1121722008-04-10 10:48:14 -04003901 err = sel_netport_sid(sk->sk_protocol,
3902 snum, &sid);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07003903 if (err)
3904 goto out;
Eric Paris50c205f2012-04-04 15:01:43 -04003905 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04003906 ad.u.net = &net;
3907 ad.u.net->sport = htons(snum);
3908 ad.u.net->family = family;
Paul Moore253bfae2010-04-22 14:46:19 -04003909 err = avc_has_perm(sksec->sid, sid,
3910 sksec->sclass,
Stephen Hemminger227b60f2007-10-10 17:30:46 -07003911 SOCKET__NAME_BIND, &ad);
3912 if (err)
3913 goto out;
3914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 }
Eric Paris828dfe12008-04-17 13:17:49 -04003916
Paul Moore253bfae2010-04-22 14:46:19 -04003917 switch (sksec->sclass) {
James Morris13402582005-09-30 14:24:34 -04003918 case SECCLASS_TCP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 node_perm = TCP_SOCKET__NODE_BIND;
3920 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003921
James Morris13402582005-09-30 14:24:34 -04003922 case SECCLASS_UDP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 node_perm = UDP_SOCKET__NODE_BIND;
3924 break;
James Morris2ee92d42006-11-13 16:09:01 -08003925
3926 case SECCLASS_DCCP_SOCKET:
3927 node_perm = DCCP_SOCKET__NODE_BIND;
3928 break;
3929
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 default:
3931 node_perm = RAWIP_SOCKET__NODE_BIND;
3932 break;
3933 }
Eric Paris828dfe12008-04-17 13:17:49 -04003934
Paul Moore224dfbd2008-01-29 08:38:13 -05003935 err = sel_netnode_sid(addrp, family, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 if (err)
3937 goto out;
Eric Paris828dfe12008-04-17 13:17:49 -04003938
Eric Paris50c205f2012-04-04 15:01:43 -04003939 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04003940 ad.u.net = &net;
3941 ad.u.net->sport = htons(snum);
3942 ad.u.net->family = family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943
3944 if (family == PF_INET)
Eric Paris48c62af2012-04-02 13:15:44 -04003945 ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 else
Eric Paris48c62af2012-04-02 13:15:44 -04003947 ad.u.net->v6info.saddr = addr6->sin6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948
Paul Moore253bfae2010-04-22 14:46:19 -04003949 err = avc_has_perm(sksec->sid, sid,
3950 sksec->sclass, node_perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 if (err)
3952 goto out;
3953 }
3954out:
3955 return err;
3956}
3957
3958static int selinux_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen)
3959{
Paul Moore014ab192008-10-10 10:16:33 -04003960 struct sock *sk = sock->sk;
Paul Moore253bfae2010-04-22 14:46:19 -04003961 struct sk_security_struct *sksec = sk->sk_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 int err;
3963
Paul Moore253bfae2010-04-22 14:46:19 -04003964 err = sock_has_perm(current, sk, SOCKET__CONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 if (err)
3966 return err;
3967
3968 /*
James Morris2ee92d42006-11-13 16:09:01 -08003969 * If a TCP or DCCP socket, check name_connect permission for the port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 */
Paul Moore253bfae2010-04-22 14:46:19 -04003971 if (sksec->sclass == SECCLASS_TCP_SOCKET ||
3972 sksec->sclass == SECCLASS_DCCP_SOCKET) {
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 Morris2ee92d42006-11-13 16:09:01 -08003978 u32 sid, perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979
3980 if (sk->sk_family == PF_INET) {
3981 addr4 = (struct sockaddr_in *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07003982 if (addrlen < sizeof(struct sockaddr_in))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 return -EINVAL;
3984 snum = ntohs(addr4->sin_port);
3985 } else {
3986 addr6 = (struct sockaddr_in6 *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07003987 if (addrlen < SIN6_LEN_RFC2133)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 return -EINVAL;
3989 snum = ntohs(addr6->sin6_port);
3990 }
3991
Paul Moore3e1121722008-04-10 10:48:14 -04003992 err = sel_netport_sid(sk->sk_protocol, snum, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 if (err)
3994 goto out;
3995
Paul Moore253bfae2010-04-22 14:46:19 -04003996 perm = (sksec->sclass == SECCLASS_TCP_SOCKET) ?
James Morris2ee92d42006-11-13 16:09:01 -08003997 TCP_SOCKET__NAME_CONNECT : DCCP_SOCKET__NAME_CONNECT;
3998
Eric Paris50c205f2012-04-04 15:01:43 -04003999 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004000 ad.u.net = &net;
4001 ad.u.net->dport = htons(snum);
4002 ad.u.net->family = sk->sk_family;
Paul Moore253bfae2010-04-22 14:46:19 -04004003 err = avc_has_perm(sksec->sid, sid, sksec->sclass, perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 if (err)
4005 goto out;
4006 }
4007
Paul Moore014ab192008-10-10 10:16:33 -04004008 err = selinux_netlbl_socket_connect(sk, address);
4009
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010out:
4011 return err;
4012}
4013
4014static int selinux_socket_listen(struct socket *sock, int backlog)
4015{
Paul Moore253bfae2010-04-22 14:46:19 -04004016 return sock_has_perm(current, sock->sk, SOCKET__LISTEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017}
4018
4019static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
4020{
4021 int err;
4022 struct inode_security_struct *isec;
4023 struct inode_security_struct *newisec;
4024
Paul Moore253bfae2010-04-22 14:46:19 -04004025 err = sock_has_perm(current, sock->sk, SOCKET__ACCEPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 if (err)
4027 return err;
4028
4029 newisec = SOCK_INODE(newsock)->i_security;
4030
4031 isec = SOCK_INODE(sock)->i_security;
4032 newisec->sclass = isec->sclass;
4033 newisec->sid = isec->sid;
4034 newisec->initialized = 1;
4035
4036 return 0;
4037}
4038
4039static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
Eric Paris828dfe12008-04-17 13:17:49 -04004040 int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041{
Paul Moore253bfae2010-04-22 14:46:19 -04004042 return sock_has_perm(current, sock->sk, SOCKET__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043}
4044
4045static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
4046 int size, int flags)
4047{
Paul Moore253bfae2010-04-22 14:46:19 -04004048 return sock_has_perm(current, sock->sk, SOCKET__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049}
4050
4051static int selinux_socket_getsockname(struct socket *sock)
4052{
Paul Moore253bfae2010-04-22 14:46:19 -04004053 return sock_has_perm(current, sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054}
4055
4056static int selinux_socket_getpeername(struct socket *sock)
4057{
Paul Moore253bfae2010-04-22 14:46:19 -04004058 return sock_has_perm(current, sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059}
4060
Eric Paris828dfe12008-04-17 13:17:49 -04004061static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062{
Paul Mooref8687af2006-10-30 15:22:15 -08004063 int err;
4064
Paul Moore253bfae2010-04-22 14:46:19 -04004065 err = sock_has_perm(current, sock->sk, SOCKET__SETOPT);
Paul Mooref8687af2006-10-30 15:22:15 -08004066 if (err)
4067 return err;
4068
4069 return selinux_netlbl_socket_setsockopt(sock, level, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070}
4071
4072static int selinux_socket_getsockopt(struct socket *sock, int level,
4073 int optname)
4074{
Paul Moore253bfae2010-04-22 14:46:19 -04004075 return sock_has_perm(current, sock->sk, SOCKET__GETOPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076}
4077
4078static int selinux_socket_shutdown(struct socket *sock, int how)
4079{
Paul Moore253bfae2010-04-22 14:46:19 -04004080 return sock_has_perm(current, sock->sk, SOCKET__SHUTDOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081}
4082
David S. Miller3610cda2011-01-05 15:38:53 -08004083static int selinux_socket_unix_stream_connect(struct sock *sock,
4084 struct sock *other,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 struct sock *newsk)
4086{
David S. Miller3610cda2011-01-05 15:38:53 -08004087 struct sk_security_struct *sksec_sock = sock->sk_security;
4088 struct sk_security_struct *sksec_other = other->sk_security;
Paul Moore4d1e2452010-04-22 14:46:18 -04004089 struct sk_security_struct *sksec_new = newsk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004090 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004091 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 int err;
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->sk = other;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097
Paul Moore4d1e2452010-04-22 14:46:18 -04004098 err = avc_has_perm(sksec_sock->sid, sksec_other->sid,
4099 sksec_other->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 UNIX_STREAM_SOCKET__CONNECTTO, &ad);
4101 if (err)
4102 return err;
4103
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 /* server child socket */
Paul Moore4d1e2452010-04-22 14:46:18 -04004105 sksec_new->peer_sid = sksec_sock->sid;
4106 err = security_sid_mls_copy(sksec_other->sid, sksec_sock->sid,
4107 &sksec_new->sid);
4108 if (err)
4109 return err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004110
Paul Moore4d1e2452010-04-22 14:46:18 -04004111 /* connecting socket */
4112 sksec_sock->peer_sid = sksec_new->sid;
4113
4114 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115}
4116
4117static int selinux_socket_unix_may_send(struct socket *sock,
4118 struct socket *other)
4119{
Paul Moore253bfae2010-04-22 14:46:19 -04004120 struct sk_security_struct *ssec = sock->sk->sk_security;
4121 struct sk_security_struct *osec = other->sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004122 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004123 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124
Eric Paris50c205f2012-04-04 15:01:43 -04004125 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004126 ad.u.net = &net;
4127 ad.u.net->sk = other->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128
Paul Moore253bfae2010-04-22 14:46:19 -04004129 return avc_has_perm(ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
4130 &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131}
4132
Paul Mooreeffad8d2008-01-29 08:49:27 -05004133static int selinux_inet_sys_rcv_skb(int ifindex, char *addrp, u16 family,
4134 u32 peer_sid,
Thomas Liu2bf49692009-07-14 12:14:09 -04004135 struct common_audit_data *ad)
Paul Mooreeffad8d2008-01-29 08:49:27 -05004136{
4137 int err;
4138 u32 if_sid;
4139 u32 node_sid;
4140
4141 err = sel_netif_sid(ifindex, &if_sid);
4142 if (err)
4143 return err;
4144 err = avc_has_perm(peer_sid, if_sid,
4145 SECCLASS_NETIF, NETIF__INGRESS, ad);
4146 if (err)
4147 return err;
4148
4149 err = sel_netnode_sid(addrp, family, &node_sid);
4150 if (err)
4151 return err;
4152 return avc_has_perm(peer_sid, node_sid,
4153 SECCLASS_NODE, NODE__RECVFROM, ad);
4154}
4155
Paul Moore220deb92008-01-29 08:38:23 -05004156static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
Paul Moored8395c82008-10-10 10:16:30 -04004157 u16 family)
Paul Moore220deb92008-01-29 08:38:23 -05004158{
Paul Moore277d3422008-12-31 12:54:11 -05004159 int err = 0;
Paul Moore220deb92008-01-29 08:38:23 -05004160 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004161 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004162 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004163 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04004164 char *addrp;
4165
Eric Paris50c205f2012-04-04 15:01:43 -04004166 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004167 ad.u.net = &net;
4168 ad.u.net->netif = skb->skb_iif;
4169 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004170 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4171 if (err)
4172 return err;
Paul Moore220deb92008-01-29 08:38:23 -05004173
Paul Moore58bfbb52009-03-27 17:10:41 -04004174 if (selinux_secmark_enabled()) {
Paul Moore220deb92008-01-29 08:38:23 -05004175 err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Moored8395c82008-10-10 10:16:30 -04004176 PACKET__RECV, &ad);
Paul Moore58bfbb52009-03-27 17:10:41 -04004177 if (err)
4178 return err;
4179 }
Paul Moore220deb92008-01-29 08:38:23 -05004180
Steffen Klassertb9679a72011-02-23 12:55:21 +01004181 err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
4182 if (err)
4183 return err;
4184 err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004185
James Morris4e5ab4c2006-06-09 00:33:33 -07004186 return err;
4187}
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004188
James Morris4e5ab4c2006-06-09 00:33:33 -07004189static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
4190{
Paul Moore220deb92008-01-29 08:38:23 -05004191 int err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004192 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004193 u16 family = sk->sk_family;
4194 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004195 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004196 struct lsm_network_audit net = {0,};
Paul Moore220deb92008-01-29 08:38:23 -05004197 char *addrp;
Paul Moored8395c82008-10-10 10:16:30 -04004198 u8 secmark_active;
4199 u8 peerlbl_active;
James Morris4e5ab4c2006-06-09 00:33:33 -07004200
James Morris4e5ab4c2006-06-09 00:33:33 -07004201 if (family != PF_INET && family != PF_INET6)
Paul Moore220deb92008-01-29 08:38:23 -05004202 return 0;
James Morris4e5ab4c2006-06-09 00:33:33 -07004203
4204 /* Handle mapped IPv4 packets arriving via IPv6 sockets */
Al Viro87fcd702006-12-04 22:00:55 +00004205 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
James Morris4e5ab4c2006-06-09 00:33:33 -07004206 family = PF_INET;
4207
Paul Moored8395c82008-10-10 10:16:30 -04004208 /* If any sort of compatibility mode is enabled then handoff processing
4209 * to the selinux_sock_rcv_skb_compat() function to deal with the
4210 * special handling. We do this in an attempt to keep this function
4211 * as fast and as clean as possible. */
Paul Moore58bfbb52009-03-27 17:10:41 -04004212 if (!selinux_policycap_netpeer)
Paul Moored8395c82008-10-10 10:16:30 -04004213 return selinux_sock_rcv_skb_compat(sk, skb, family);
4214
4215 secmark_active = selinux_secmark_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04004216 peerlbl_active = selinux_peerlbl_enabled();
Paul Moored8395c82008-10-10 10:16:30 -04004217 if (!secmark_active && !peerlbl_active)
4218 return 0;
4219
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->netif = skb->skb_iif;
4223 ad.u.net->family = family;
Paul Moore224dfbd2008-01-29 08:38:13 -05004224 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
James Morris4e5ab4c2006-06-09 00:33:33 -07004225 if (err)
Paul Moore220deb92008-01-29 08:38:23 -05004226 return err;
James Morris4e5ab4c2006-06-09 00:33:33 -07004227
Paul Moored8395c82008-10-10 10:16:30 -04004228 if (peerlbl_active) {
Paul Moored621d352008-01-29 08:43:36 -05004229 u32 peer_sid;
4230
4231 err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
4232 if (err)
4233 return err;
Eric Dumazet8964be42009-11-20 15:35:04 -08004234 err = selinux_inet_sys_rcv_skb(skb->skb_iif, addrp, family,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004235 peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04004236 if (err) {
4237 selinux_netlbl_err(skb, err, 0);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004238 return err;
Paul Mooredfaebe92008-10-10 10:16:31 -04004239 }
Paul Moored621d352008-01-29 08:43:36 -05004240 err = avc_has_perm(sk_sid, peer_sid, SECCLASS_PEER,
4241 PEER__RECV, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04004242 if (err)
4243 selinux_netlbl_err(skb, err, 0);
Paul Moored621d352008-01-29 08:43:36 -05004244 }
4245
Paul Moored8395c82008-10-10 10:16:30 -04004246 if (secmark_active) {
Paul Mooreeffad8d2008-01-29 08:49:27 -05004247 err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
4248 PACKET__RECV, &ad);
4249 if (err)
4250 return err;
4251 }
4252
Paul Moored621d352008-01-29 08:43:36 -05004253 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254}
4255
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004256static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
4257 int __user *optlen, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258{
4259 int err = 0;
4260 char *scontext;
4261 u32 scontext_len;
Paul Moore253bfae2010-04-22 14:46:19 -04004262 struct sk_security_struct *sksec = sock->sk->sk_security;
Paul Moore3de4bab2006-11-17 17:38:54 -05004263 u32 peer_sid = SECSID_NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264
Paul Moore253bfae2010-04-22 14:46:19 -04004265 if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
4266 sksec->sclass == SECCLASS_TCP_SOCKET)
Eric Parisdd3e7832010-04-07 15:08:46 -04004267 peer_sid = sksec->peer_sid;
Paul Moore253bfae2010-04-22 14:46:19 -04004268 if (peer_sid == SECSID_NULL)
4269 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004271 err = security_sid_to_context(peer_sid, &scontext, &scontext_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 if (err)
Paul Moore253bfae2010-04-22 14:46:19 -04004273 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274
4275 if (scontext_len > len) {
4276 err = -ERANGE;
4277 goto out_len;
4278 }
4279
4280 if (copy_to_user(optval, scontext, scontext_len))
4281 err = -EFAULT;
4282
4283out_len:
4284 if (put_user(scontext_len, optlen))
4285 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 kfree(scontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 return err;
4288}
4289
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004290static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004291{
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004292 u32 peer_secid = SECSID_NULL;
Paul Moore75e22912008-01-29 08:38:04 -05004293 u16 family;
Catherine Zhang877ce7c2006-06-29 12:27:47 -07004294
Paul Mooreaa862902008-10-10 10:16:29 -04004295 if (skb && skb->protocol == htons(ETH_P_IP))
4296 family = PF_INET;
4297 else if (skb && skb->protocol == htons(ETH_P_IPV6))
4298 family = PF_INET6;
4299 else if (sock)
Paul Moore75e22912008-01-29 08:38:04 -05004300 family = sock->sk->sk_family;
Paul Moore75e22912008-01-29 08:38:04 -05004301 else
4302 goto out;
4303
4304 if (sock && family == PF_UNIX)
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02004305 selinux_inode_getsecid(SOCK_INODE(sock), &peer_secid);
Paul Moore3de4bab2006-11-17 17:38:54 -05004306 else if (skb)
Paul Moore220deb92008-01-29 08:38:23 -05004307 selinux_skb_peerlbl_sid(skb, family, &peer_secid);
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004308
Paul Moore75e22912008-01-29 08:38:04 -05004309out:
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004310 *secid = peer_secid;
Paul Moore75e22912008-01-29 08:38:04 -05004311 if (peer_secid == SECSID_NULL)
4312 return -EINVAL;
4313 return 0;
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004314}
4315
Al Viro7d877f32005-10-21 03:20:43 -04004316static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317{
Paul Moore84914b72010-04-22 14:46:18 -04004318 struct sk_security_struct *sksec;
4319
4320 sksec = kzalloc(sizeof(*sksec), priority);
4321 if (!sksec)
4322 return -ENOMEM;
4323
4324 sksec->peer_sid = SECINITSID_UNLABELED;
4325 sksec->sid = SECINITSID_UNLABELED;
4326 selinux_netlbl_sk_security_reset(sksec);
4327 sk->sk_security = sksec;
4328
4329 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330}
4331
4332static void selinux_sk_free_security(struct sock *sk)
4333{
Paul Moore84914b72010-04-22 14:46:18 -04004334 struct sk_security_struct *sksec = sk->sk_security;
4335
4336 sk->sk_security = NULL;
4337 selinux_netlbl_sk_security_free(sksec);
4338 kfree(sksec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339}
4340
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004341static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
4342{
Eric Parisdd3e7832010-04-07 15:08:46 -04004343 struct sk_security_struct *sksec = sk->sk_security;
4344 struct sk_security_struct *newsksec = newsk->sk_security;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004345
Eric Parisdd3e7832010-04-07 15:08:46 -04004346 newsksec->sid = sksec->sid;
4347 newsksec->peer_sid = sksec->peer_sid;
4348 newsksec->sclass = sksec->sclass;
Paul Moore99f59ed2006-08-29 17:53:48 -07004349
Eric Parisdd3e7832010-04-07 15:08:46 -04004350 selinux_netlbl_sk_security_reset(newsksec);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004351}
4352
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07004353static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004354{
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004355 if (!sk)
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07004356 *secid = SECINITSID_ANY_SOCKET;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004357 else {
4358 struct sk_security_struct *sksec = sk->sk_security;
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004359
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07004360 *secid = sksec->sid;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004361 }
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004362}
4363
Eric Paris828dfe12008-04-17 13:17:49 -04004364static void selinux_sock_graft(struct sock *sk, struct socket *parent)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004365{
4366 struct inode_security_struct *isec = SOCK_INODE(parent)->i_security;
4367 struct sk_security_struct *sksec = sk->sk_security;
4368
David Woodhouse2148ccc2006-09-29 15:50:25 -07004369 if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
4370 sk->sk_family == PF_UNIX)
4371 isec->sid = sksec->sid;
Paul Moore220deb92008-01-29 08:38:23 -05004372 sksec->sclass = isec->sclass;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004373}
4374
Adrian Bunk9a673e52006-08-15 00:03:53 -07004375static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4376 struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004377{
4378 struct sk_security_struct *sksec = sk->sk_security;
4379 int err;
Paul Mooreaa862902008-10-10 10:16:29 -04004380 u16 family = sk->sk_family;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07004381 u32 newsid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004382 u32 peersid;
4383
Paul Mooreaa862902008-10-10 10:16:29 -04004384 /* handle mapped IPv4 packets arriving via IPv6 sockets */
4385 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4386 family = PF_INET;
4387
4388 err = selinux_skb_peerlbl_sid(skb, family, &peersid);
Paul Moore220deb92008-01-29 08:38:23 -05004389 if (err)
4390 return err;
Venkat Yekkiralaa51c64f2006-07-27 22:01:34 -07004391 if (peersid == SECSID_NULL) {
4392 req->secid = sksec->sid;
Paul Moore3de4bab2006-11-17 17:38:54 -05004393 req->peer_secid = SECSID_NULL;
Paul Moore389fb8002009-03-27 17:10:34 -04004394 } else {
4395 err = security_sid_mls_copy(sksec->sid, peersid, &newsid);
4396 if (err)
4397 return err;
4398 req->secid = newsid;
4399 req->peer_secid = peersid;
Venkat Yekkiralaa51c64f2006-07-27 22:01:34 -07004400 }
4401
Paul Moore389fb8002009-03-27 17:10:34 -04004402 return selinux_netlbl_inet_conn_request(req, family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004403}
4404
Adrian Bunk9a673e52006-08-15 00:03:53 -07004405static void selinux_inet_csk_clone(struct sock *newsk,
4406 const struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004407{
4408 struct sk_security_struct *newsksec = newsk->sk_security;
4409
4410 newsksec->sid = req->secid;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004411 newsksec->peer_sid = req->peer_secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004412 /* NOTE: Ideally, we should also get the isec->sid for the
4413 new socket in sync, but we don't have the isec available yet.
4414 So we will wait until sock_graft to do it, by which
4415 time it will have been created and available. */
Paul Moore99f59ed2006-08-29 17:53:48 -07004416
Paul Moore9f2ad662006-11-17 17:38:53 -05004417 /* We don't need to take any sort of lock here as we are the only
4418 * thread with access to newsksec */
Paul Moore389fb8002009-03-27 17:10:34 -04004419 selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004420}
4421
Paul Moore014ab192008-10-10 10:16:33 -04004422static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004423{
Paul Mooreaa862902008-10-10 10:16:29 -04004424 u16 family = sk->sk_family;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004425 struct sk_security_struct *sksec = sk->sk_security;
4426
Paul Mooreaa862902008-10-10 10:16:29 -04004427 /* handle mapped IPv4 packets arriving via IPv6 sockets */
4428 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4429 family = PF_INET;
4430
4431 selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004432}
4433
Eric Dumazetca10b9e2013-04-08 17:58:11 +00004434static void selinux_skb_owned_by(struct sk_buff *skb, struct sock *sk)
4435{
4436 skb_set_owner_w(skb, sk);
4437}
4438
Eric Paris2606fd12010-10-13 16:24:41 -04004439static int selinux_secmark_relabel_packet(u32 sid)
4440{
4441 const struct task_security_struct *__tsec;
4442 u32 tsid;
4443
4444 __tsec = current_security();
4445 tsid = __tsec->sid;
4446
4447 return avc_has_perm(tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO, NULL);
4448}
4449
4450static void selinux_secmark_refcount_inc(void)
4451{
4452 atomic_inc(&selinux_secmark_refcount);
4453}
4454
4455static void selinux_secmark_refcount_dec(void)
4456{
4457 atomic_dec(&selinux_secmark_refcount);
4458}
4459
Adrian Bunk9a673e52006-08-15 00:03:53 -07004460static void selinux_req_classify_flow(const struct request_sock *req,
4461 struct flowi *fl)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004462{
David S. Miller1d28f422011-03-12 00:29:39 -05004463 fl->flowi_secid = req->secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004464}
4465
Paul Moore5dbbaf22013-01-14 07:12:19 +00004466static int selinux_tun_dev_alloc_security(void **security)
4467{
4468 struct tun_security_struct *tunsec;
4469
4470 tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL);
4471 if (!tunsec)
4472 return -ENOMEM;
4473 tunsec->sid = current_sid();
4474
4475 *security = tunsec;
4476 return 0;
4477}
4478
4479static void selinux_tun_dev_free_security(void *security)
4480{
4481 kfree(security);
4482}
4483
Paul Mooreed6d76e2009-08-28 18:12:49 -04004484static int selinux_tun_dev_create(void)
4485{
4486 u32 sid = current_sid();
4487
4488 /* we aren't taking into account the "sockcreate" SID since the socket
4489 * that is being created here is not a socket in the traditional sense,
4490 * instead it is a private sock, accessible only to the kernel, and
4491 * representing a wide range of network traffic spanning multiple
4492 * connections unlike traditional sockets - check the TUN driver to
4493 * get a better understanding of why this socket is special */
4494
4495 return avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
4496 NULL);
4497}
4498
Paul Moore5dbbaf22013-01-14 07:12:19 +00004499static int selinux_tun_dev_attach_queue(void *security)
Paul Mooreed6d76e2009-08-28 18:12:49 -04004500{
Paul Moore5dbbaf22013-01-14 07:12:19 +00004501 struct tun_security_struct *tunsec = security;
4502
4503 return avc_has_perm(current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET,
4504 TUN_SOCKET__ATTACH_QUEUE, NULL);
4505}
4506
4507static int selinux_tun_dev_attach(struct sock *sk, void *security)
4508{
4509 struct tun_security_struct *tunsec = security;
Paul Mooreed6d76e2009-08-28 18:12:49 -04004510 struct sk_security_struct *sksec = sk->sk_security;
4511
4512 /* we don't currently perform any NetLabel based labeling here and it
4513 * isn't clear that we would want to do so anyway; while we could apply
4514 * labeling without the support of the TUN user the resulting labeled
4515 * traffic from the other end of the connection would almost certainly
4516 * cause confusion to the TUN user that had no idea network labeling
4517 * protocols were being used */
4518
Paul Moore5dbbaf22013-01-14 07:12:19 +00004519 sksec->sid = tunsec->sid;
Paul Mooreed6d76e2009-08-28 18:12:49 -04004520 sksec->sclass = SECCLASS_TUN_SOCKET;
Paul Moore5dbbaf22013-01-14 07:12:19 +00004521
4522 return 0;
Paul Mooreed6d76e2009-08-28 18:12:49 -04004523}
4524
Paul Moore5dbbaf22013-01-14 07:12:19 +00004525static int selinux_tun_dev_open(void *security)
Paul Mooreed6d76e2009-08-28 18:12:49 -04004526{
Paul Moore5dbbaf22013-01-14 07:12:19 +00004527 struct tun_security_struct *tunsec = security;
Paul Mooreed6d76e2009-08-28 18:12:49 -04004528 u32 sid = current_sid();
4529 int err;
4530
Paul Moore5dbbaf22013-01-14 07:12:19 +00004531 err = avc_has_perm(sid, tunsec->sid, SECCLASS_TUN_SOCKET,
Paul Mooreed6d76e2009-08-28 18:12:49 -04004532 TUN_SOCKET__RELABELFROM, NULL);
4533 if (err)
4534 return err;
4535 err = avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET,
4536 TUN_SOCKET__RELABELTO, NULL);
4537 if (err)
4538 return err;
Paul Moore5dbbaf22013-01-14 07:12:19 +00004539 tunsec->sid = sid;
Paul Mooreed6d76e2009-08-28 18:12:49 -04004540
4541 return 0;
4542}
4543
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
4545{
4546 int err = 0;
4547 u32 perm;
4548 struct nlmsghdr *nlh;
Paul Moore253bfae2010-04-22 14:46:19 -04004549 struct sk_security_struct *sksec = sk->sk_security;
Eric Paris828dfe12008-04-17 13:17:49 -04004550
Hong zhi guo77954982013-03-27 06:49:35 +00004551 if (skb->len < NLMSG_HDRLEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552 err = -EINVAL;
4553 goto out;
4554 }
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -07004555 nlh = nlmsg_hdr(skb);
Eric Paris828dfe12008-04-17 13:17:49 -04004556
Paul Moore253bfae2010-04-22 14:46:19 -04004557 err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 if (err) {
4559 if (err == -EINVAL) {
David Woodhouse9ad9ad32005-06-22 15:04:33 +01004560 audit_log(current->audit_context, GFP_KERNEL, AUDIT_SELINUX_ERR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561 "SELinux: unrecognized netlink message"
4562 " type=%hu for sclass=%hu\n",
Paul Moore253bfae2010-04-22 14:46:19 -04004563 nlh->nlmsg_type, sksec->sclass);
Eric Paris39c9aed2008-11-05 09:34:42 -05004564 if (!selinux_enforcing || security_get_allow_unknown())
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 err = 0;
4566 }
4567
4568 /* Ignore */
4569 if (err == -ENOENT)
4570 err = 0;
4571 goto out;
4572 }
4573
Paul Moore253bfae2010-04-22 14:46:19 -04004574 err = sock_has_perm(current, sk, perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575out:
4576 return err;
4577}
4578
4579#ifdef CONFIG_NETFILTER
4580
Paul Mooreeffad8d2008-01-29 08:49:27 -05004581static unsigned int selinux_ip_forward(struct sk_buff *skb, int ifindex,
4582 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583{
Paul Mooredfaebe92008-10-10 10:16:31 -04004584 int err;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004585 char *addrp;
4586 u32 peer_sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004587 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004588 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05004589 u8 secmark_active;
Paul Moore948bf852008-10-10 10:16:32 -04004590 u8 netlbl_active;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004591 u8 peerlbl_active;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004592
Paul Mooreeffad8d2008-01-29 08:49:27 -05004593 if (!selinux_policycap_netpeer)
4594 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004595
Paul Mooreeffad8d2008-01-29 08:49:27 -05004596 secmark_active = selinux_secmark_enabled();
Paul Moore948bf852008-10-10 10:16:32 -04004597 netlbl_active = netlbl_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04004598 peerlbl_active = selinux_peerlbl_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05004599 if (!secmark_active && !peerlbl_active)
4600 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004601
Paul Moored8395c82008-10-10 10:16:30 -04004602 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
4603 return NF_DROP;
4604
Eric Paris50c205f2012-04-04 15:01:43 -04004605 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004606 ad.u.net = &net;
4607 ad.u.net->netif = ifindex;
4608 ad.u.net->family = family;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004609 if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
4610 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611
Paul Mooredfaebe92008-10-10 10:16:31 -04004612 if (peerlbl_active) {
4613 err = selinux_inet_sys_rcv_skb(ifindex, addrp, family,
4614 peer_sid, &ad);
4615 if (err) {
4616 selinux_netlbl_err(skb, err, 1);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004617 return NF_DROP;
Paul Mooredfaebe92008-10-10 10:16:31 -04004618 }
4619 }
Paul Mooreeffad8d2008-01-29 08:49:27 -05004620
4621 if (secmark_active)
4622 if (avc_has_perm(peer_sid, skb->secmark,
4623 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
4624 return NF_DROP;
4625
Paul Moore948bf852008-10-10 10:16:32 -04004626 if (netlbl_active)
4627 /* we do this in the FORWARD path and not the POST_ROUTING
4628 * path because we want to make sure we apply the necessary
4629 * labeling before IPsec is applied so we can leverage AH
4630 * protection */
4631 if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
4632 return NF_DROP;
4633
Paul Mooreeffad8d2008-01-29 08:49:27 -05004634 return NF_ACCEPT;
4635}
4636
4637static unsigned int selinux_ipv4_forward(unsigned int hooknum,
4638 struct sk_buff *skb,
4639 const struct net_device *in,
4640 const struct net_device *out,
4641 int (*okfn)(struct sk_buff *))
4642{
4643 return selinux_ip_forward(skb, in->ifindex, PF_INET);
4644}
4645
4646#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
4647static unsigned int selinux_ipv6_forward(unsigned int hooknum,
4648 struct sk_buff *skb,
4649 const struct net_device *in,
4650 const struct net_device *out,
4651 int (*okfn)(struct sk_buff *))
4652{
4653 return selinux_ip_forward(skb, in->ifindex, PF_INET6);
4654}
4655#endif /* IPV6 */
4656
Paul Moore948bf852008-10-10 10:16:32 -04004657static unsigned int selinux_ip_output(struct sk_buff *skb,
4658 u16 family)
4659{
4660 u32 sid;
4661
4662 if (!netlbl_enabled())
4663 return NF_ACCEPT;
4664
4665 /* we do this in the LOCAL_OUT path and not the POST_ROUTING path
4666 * because we want to make sure we apply the necessary labeling
4667 * before IPsec is applied so we can leverage AH protection */
4668 if (skb->sk) {
4669 struct sk_security_struct *sksec = skb->sk->sk_security;
4670 sid = sksec->sid;
4671 } else
4672 sid = SECINITSID_KERNEL;
4673 if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
4674 return NF_DROP;
4675
4676 return NF_ACCEPT;
4677}
4678
4679static unsigned int selinux_ipv4_output(unsigned int hooknum,
4680 struct sk_buff *skb,
4681 const struct net_device *in,
4682 const struct net_device *out,
4683 int (*okfn)(struct sk_buff *))
4684{
4685 return selinux_ip_output(skb, PF_INET);
4686}
4687
Paul Mooreeffad8d2008-01-29 08:49:27 -05004688static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
4689 int ifindex,
Paul Moored8395c82008-10-10 10:16:30 -04004690 u16 family)
James Morris4e5ab4c2006-06-09 00:33:33 -07004691{
Paul Mooreeffad8d2008-01-29 08:49:27 -05004692 struct sock *sk = skb->sk;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004693 struct sk_security_struct *sksec;
Thomas Liu2bf49692009-07-14 12:14:09 -04004694 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004695 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04004696 char *addrp;
4697 u8 proto;
James Morris4e5ab4c2006-06-09 00:33:33 -07004698
Paul Mooreeffad8d2008-01-29 08:49:27 -05004699 if (sk == NULL)
4700 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004701 sksec = sk->sk_security;
James Morris4e5ab4c2006-06-09 00:33:33 -07004702
Eric Paris50c205f2012-04-04 15:01:43 -04004703 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004704 ad.u.net = &net;
4705 ad.u.net->netif = ifindex;
4706 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004707 if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
4708 return NF_DROP;
4709
Paul Moore58bfbb52009-03-27 17:10:41 -04004710 if (selinux_secmark_enabled())
Paul Mooreeffad8d2008-01-29 08:49:27 -05004711 if (avc_has_perm(sksec->sid, skb->secmark,
Paul Moored8395c82008-10-10 10:16:30 -04004712 SECCLASS_PACKET, PACKET__SEND, &ad))
Eric Paris2fe66ec2010-11-23 06:28:08 +00004713 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07004714
Steffen Klassertb9679a72011-02-23 12:55:21 +01004715 if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
4716 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07004717
Paul Mooreeffad8d2008-01-29 08:49:27 -05004718 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719}
4720
Paul Mooreeffad8d2008-01-29 08:49:27 -05004721static unsigned int selinux_ip_postroute(struct sk_buff *skb, int ifindex,
4722 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723{
Paul Mooreeffad8d2008-01-29 08:49:27 -05004724 u32 secmark_perm;
4725 u32 peer_sid;
4726 struct sock *sk;
Thomas Liu2bf49692009-07-14 12:14:09 -04004727 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004728 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05004729 char *addrp;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004730 u8 secmark_active;
4731 u8 peerlbl_active;
4732
Paul Mooreeffad8d2008-01-29 08:49:27 -05004733 /* If any sort of compatibility mode is enabled then handoff processing
4734 * to the selinux_ip_postroute_compat() function to deal with the
4735 * special handling. We do this in an attempt to keep this function
4736 * as fast and as clean as possible. */
Paul Moore58bfbb52009-03-27 17:10:41 -04004737 if (!selinux_policycap_netpeer)
Paul Moored8395c82008-10-10 10:16:30 -04004738 return selinux_ip_postroute_compat(skb, ifindex, family);
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -07004739#ifdef CONFIG_XFRM
Paul Mooreeffad8d2008-01-29 08:49:27 -05004740 /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
4741 * packet transformation so allow the packet to pass without any checks
4742 * since we'll have another chance to perform access control checks
4743 * when the packet is on it's final way out.
4744 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
4745 * is NULL, in this case go ahead and apply access control. */
Eric Dumazetadf30902009-06-02 05:19:30 +00004746 if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL)
Paul Mooreeffad8d2008-01-29 08:49:27 -05004747 return NF_ACCEPT;
Alexey Dobriyandef8b4f2008-10-28 13:24:06 -07004748#endif
Paul Mooreeffad8d2008-01-29 08:49:27 -05004749 secmark_active = selinux_secmark_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04004750 peerlbl_active = selinux_peerlbl_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05004751 if (!secmark_active && !peerlbl_active)
4752 return NF_ACCEPT;
4753
Paul Moored8395c82008-10-10 10:16:30 -04004754 /* if the packet is being forwarded then get the peer label from the
4755 * packet itself; otherwise check to see if it is from a local
4756 * application or the kernel, if from an application get the peer label
4757 * from the sending socket, otherwise use the kernel's sid */
Paul Mooreeffad8d2008-01-29 08:49:27 -05004758 sk = skb->sk;
Paul Moored8395c82008-10-10 10:16:30 -04004759 if (sk == NULL) {
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01004760 if (skb->skb_iif) {
4761 secmark_perm = PACKET__FORWARD_OUT;
Paul Moored8395c82008-10-10 10:16:30 -04004762 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00004763 return NF_DROP;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01004764 } else {
4765 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04004766 peer_sid = SECINITSID_KERNEL;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01004767 }
Paul Moored8395c82008-10-10 10:16:30 -04004768 } else {
Paul Mooreeffad8d2008-01-29 08:49:27 -05004769 struct sk_security_struct *sksec = sk->sk_security;
4770 peer_sid = sksec->sid;
4771 secmark_perm = PACKET__SEND;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004772 }
4773
Eric Paris50c205f2012-04-04 15:01:43 -04004774 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004775 ad.u.net = &net;
4776 ad.u.net->netif = ifindex;
4777 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004778 if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
Eric Paris04f6d702010-11-23 06:28:02 +00004779 return NF_DROP;
Paul Moored8395c82008-10-10 10:16:30 -04004780
Paul Mooreeffad8d2008-01-29 08:49:27 -05004781 if (secmark_active)
4782 if (avc_has_perm(peer_sid, skb->secmark,
4783 SECCLASS_PACKET, secmark_perm, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00004784 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004785
4786 if (peerlbl_active) {
4787 u32 if_sid;
4788 u32 node_sid;
4789
4790 if (sel_netif_sid(ifindex, &if_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00004791 return NF_DROP;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004792 if (avc_has_perm(peer_sid, if_sid,
4793 SECCLASS_NETIF, NETIF__EGRESS, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00004794 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004795
4796 if (sel_netnode_sid(addrp, family, &node_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00004797 return NF_DROP;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004798 if (avc_has_perm(peer_sid, node_sid,
4799 SECCLASS_NODE, NODE__SENDTO, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00004800 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004801 }
4802
4803 return NF_ACCEPT;
4804}
4805
4806static unsigned int selinux_ipv4_postroute(unsigned int hooknum,
4807 struct sk_buff *skb,
4808 const struct net_device *in,
4809 const struct net_device *out,
4810 int (*okfn)(struct sk_buff *))
4811{
4812 return selinux_ip_postroute(skb, out->ifindex, PF_INET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813}
4814
4815#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Paul Mooreeffad8d2008-01-29 08:49:27 -05004816static unsigned int selinux_ipv6_postroute(unsigned int hooknum,
4817 struct sk_buff *skb,
4818 const struct net_device *in,
4819 const struct net_device *out,
4820 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821{
Paul Mooreeffad8d2008-01-29 08:49:27 -05004822 return selinux_ip_postroute(skb, out->ifindex, PF_INET6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824#endif /* IPV6 */
4825
4826#endif /* CONFIG_NETFILTER */
4827
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
4829{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830 int err;
4831
Eric Paris200ac532009-02-12 15:01:04 -05004832 err = cap_netlink_send(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833 if (err)
4834 return err;
4835
Stephen Smalley941fc5b2009-10-01 14:48:23 -04004836 return selinux_nlmsg_perm(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837}
4838
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839static int ipc_alloc_security(struct task_struct *task,
4840 struct kern_ipc_perm *perm,
4841 u16 sclass)
4842{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 struct ipc_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +11004844 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845
James Morris89d155e2005-10-30 14:59:21 -08004846 isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 if (!isec)
4848 return -ENOMEM;
4849
David Howells275bb412008-11-14 10:39:19 +11004850 sid = task_sid(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851 isec->sclass = sclass;
David Howells275bb412008-11-14 10:39:19 +11004852 isec->sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853 perm->security = isec;
4854
4855 return 0;
4856}
4857
4858static void ipc_free_security(struct kern_ipc_perm *perm)
4859{
4860 struct ipc_security_struct *isec = perm->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861 perm->security = NULL;
4862 kfree(isec);
4863}
4864
4865static int msg_msg_alloc_security(struct msg_msg *msg)
4866{
4867 struct msg_security_struct *msec;
4868
James Morris89d155e2005-10-30 14:59:21 -08004869 msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870 if (!msec)
4871 return -ENOMEM;
4872
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873 msec->sid = SECINITSID_UNLABELED;
4874 msg->security = msec;
4875
4876 return 0;
4877}
4878
4879static void msg_msg_free_security(struct msg_msg *msg)
4880{
4881 struct msg_security_struct *msec = msg->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882
4883 msg->security = NULL;
4884 kfree(msec);
4885}
4886
4887static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
Stephen Smalley6af963f2005-05-01 08:58:39 -07004888 u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04004891 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11004892 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894 isec = ipc_perms->security;
4895
Eric Paris50c205f2012-04-04 15:01:43 -04004896 ad.type = LSM_AUDIT_DATA_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 ad.u.ipc_id = ipc_perms->key;
4898
David Howells275bb412008-11-14 10:39:19 +11004899 return avc_has_perm(sid, isec->sid, isec->sclass, perms, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900}
4901
4902static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
4903{
4904 return msg_msg_alloc_security(msg);
4905}
4906
4907static void selinux_msg_msg_free_security(struct msg_msg *msg)
4908{
4909 msg_msg_free_security(msg);
4910}
4911
4912/* message queue security operations */
4913static int selinux_msg_queue_alloc_security(struct msg_queue *msq)
4914{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04004916 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11004917 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918 int rc;
4919
4920 rc = ipc_alloc_security(current, &msq->q_perm, SECCLASS_MSGQ);
4921 if (rc)
4922 return rc;
4923
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 isec = msq->q_perm.security;
4925
Eric Paris50c205f2012-04-04 15:01:43 -04004926 ad.type = LSM_AUDIT_DATA_IPC;
Eric Paris828dfe12008-04-17 13:17:49 -04004927 ad.u.ipc_id = msq->q_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928
David Howells275bb412008-11-14 10:39:19 +11004929 rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930 MSGQ__CREATE, &ad);
4931 if (rc) {
4932 ipc_free_security(&msq->q_perm);
4933 return rc;
4934 }
4935 return 0;
4936}
4937
4938static void selinux_msg_queue_free_security(struct msg_queue *msq)
4939{
4940 ipc_free_security(&msq->q_perm);
4941}
4942
4943static int selinux_msg_queue_associate(struct msg_queue *msq, int msqflg)
4944{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04004946 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11004947 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 isec = msq->q_perm.security;
4950
Eric Paris50c205f2012-04-04 15:01:43 -04004951 ad.type = LSM_AUDIT_DATA_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952 ad.u.ipc_id = msq->q_perm.key;
4953
David Howells275bb412008-11-14 10:39:19 +11004954 return avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 MSGQ__ASSOCIATE, &ad);
4956}
4957
4958static int selinux_msg_queue_msgctl(struct msg_queue *msq, int cmd)
4959{
4960 int err;
4961 int perms;
4962
Eric Paris828dfe12008-04-17 13:17:49 -04004963 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964 case IPC_INFO:
4965 case MSG_INFO:
4966 /* No specific object, just general system-wide information. */
4967 return task_has_system(current, SYSTEM__IPC_INFO);
4968 case IPC_STAT:
4969 case MSG_STAT:
4970 perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
4971 break;
4972 case IPC_SET:
4973 perms = MSGQ__SETATTR;
4974 break;
4975 case IPC_RMID:
4976 perms = MSGQ__DESTROY;
4977 break;
4978 default:
4979 return 0;
4980 }
4981
Stephen Smalley6af963f2005-05-01 08:58:39 -07004982 err = ipc_has_perm(&msq->q_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 return err;
4984}
4985
4986static int selinux_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg, int msqflg)
4987{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988 struct ipc_security_struct *isec;
4989 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04004990 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11004991 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992 int rc;
4993
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994 isec = msq->q_perm.security;
4995 msec = msg->security;
4996
4997 /*
4998 * First time through, need to assign label to the message
4999 */
5000 if (msec->sid == SECINITSID_UNLABELED) {
5001 /*
5002 * Compute new sid based on current process and
5003 * message queue this message will be stored in
5004 */
David Howells275bb412008-11-14 10:39:19 +11005005 rc = security_transition_sid(sid, isec->sid, SECCLASS_MSG,
Eric Paris652bb9b2011-02-01 11:05:40 -05005006 NULL, &msec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007 if (rc)
5008 return rc;
5009 }
5010
Eric Paris50c205f2012-04-04 15:01:43 -04005011 ad.type = LSM_AUDIT_DATA_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 ad.u.ipc_id = msq->q_perm.key;
5013
5014 /* Can this process write to the queue? */
David Howells275bb412008-11-14 10:39:19 +11005015 rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016 MSGQ__WRITE, &ad);
5017 if (!rc)
5018 /* Can this process send the message */
David Howells275bb412008-11-14 10:39:19 +11005019 rc = avc_has_perm(sid, msec->sid, SECCLASS_MSG,
5020 MSG__SEND, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021 if (!rc)
5022 /* Can the message be put in the queue? */
David Howells275bb412008-11-14 10:39:19 +11005023 rc = avc_has_perm(msec->sid, isec->sid, SECCLASS_MSGQ,
5024 MSGQ__ENQUEUE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025
5026 return rc;
5027}
5028
5029static int selinux_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
5030 struct task_struct *target,
5031 long type, int mode)
5032{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 struct ipc_security_struct *isec;
5034 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005035 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005036 u32 sid = task_sid(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 int rc;
5038
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039 isec = msq->q_perm.security;
5040 msec = msg->security;
5041
Eric Paris50c205f2012-04-04 15:01:43 -04005042 ad.type = LSM_AUDIT_DATA_IPC;
Eric Paris828dfe12008-04-17 13:17:49 -04005043 ad.u.ipc_id = msq->q_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044
David Howells275bb412008-11-14 10:39:19 +11005045 rc = avc_has_perm(sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 SECCLASS_MSGQ, MSGQ__READ, &ad);
5047 if (!rc)
David Howells275bb412008-11-14 10:39:19 +11005048 rc = avc_has_perm(sid, msec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049 SECCLASS_MSG, MSG__RECEIVE, &ad);
5050 return rc;
5051}
5052
5053/* Shared Memory security operations */
5054static int selinux_shm_alloc_security(struct shmid_kernel *shp)
5055{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005057 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005058 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059 int rc;
5060
5061 rc = ipc_alloc_security(current, &shp->shm_perm, SECCLASS_SHM);
5062 if (rc)
5063 return rc;
5064
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065 isec = shp->shm_perm.security;
5066
Eric Paris50c205f2012-04-04 15:01:43 -04005067 ad.type = LSM_AUDIT_DATA_IPC;
Eric Paris828dfe12008-04-17 13:17:49 -04005068 ad.u.ipc_id = shp->shm_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069
David Howells275bb412008-11-14 10:39:19 +11005070 rc = avc_has_perm(sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071 SHM__CREATE, &ad);
5072 if (rc) {
5073 ipc_free_security(&shp->shm_perm);
5074 return rc;
5075 }
5076 return 0;
5077}
5078
5079static void selinux_shm_free_security(struct shmid_kernel *shp)
5080{
5081 ipc_free_security(&shp->shm_perm);
5082}
5083
5084static int selinux_shm_associate(struct shmid_kernel *shp, int shmflg)
5085{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005087 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005088 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090 isec = shp->shm_perm.security;
5091
Eric Paris50c205f2012-04-04 15:01:43 -04005092 ad.type = LSM_AUDIT_DATA_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093 ad.u.ipc_id = shp->shm_perm.key;
5094
David Howells275bb412008-11-14 10:39:19 +11005095 return avc_has_perm(sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 SHM__ASSOCIATE, &ad);
5097}
5098
5099/* Note, at this point, shp is locked down */
5100static int selinux_shm_shmctl(struct shmid_kernel *shp, int cmd)
5101{
5102 int perms;
5103 int err;
5104
Eric Paris828dfe12008-04-17 13:17:49 -04005105 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106 case IPC_INFO:
5107 case SHM_INFO:
5108 /* No specific object, just general system-wide information. */
5109 return task_has_system(current, SYSTEM__IPC_INFO);
5110 case IPC_STAT:
5111 case SHM_STAT:
5112 perms = SHM__GETATTR | SHM__ASSOCIATE;
5113 break;
5114 case IPC_SET:
5115 perms = SHM__SETATTR;
5116 break;
5117 case SHM_LOCK:
5118 case SHM_UNLOCK:
5119 perms = SHM__LOCK;
5120 break;
5121 case IPC_RMID:
5122 perms = SHM__DESTROY;
5123 break;
5124 default:
5125 return 0;
5126 }
5127
Stephen Smalley6af963f2005-05-01 08:58:39 -07005128 err = ipc_has_perm(&shp->shm_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129 return err;
5130}
5131
5132static int selinux_shm_shmat(struct shmid_kernel *shp,
5133 char __user *shmaddr, int shmflg)
5134{
5135 u32 perms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136
5137 if (shmflg & SHM_RDONLY)
5138 perms = SHM__READ;
5139 else
5140 perms = SHM__READ | SHM__WRITE;
5141
Stephen Smalley6af963f2005-05-01 08:58:39 -07005142 return ipc_has_perm(&shp->shm_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143}
5144
5145/* Semaphore security operations */
5146static int selinux_sem_alloc_security(struct sem_array *sma)
5147{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148 struct ipc_security_struct *isec;
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
5153 rc = ipc_alloc_security(current, &sma->sem_perm, SECCLASS_SEM);
5154 if (rc)
5155 return rc;
5156
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157 isec = sma->sem_perm.security;
5158
Eric Paris50c205f2012-04-04 15:01:43 -04005159 ad.type = LSM_AUDIT_DATA_IPC;
Eric Paris828dfe12008-04-17 13:17:49 -04005160 ad.u.ipc_id = sma->sem_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161
David Howells275bb412008-11-14 10:39:19 +11005162 rc = avc_has_perm(sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163 SEM__CREATE, &ad);
5164 if (rc) {
5165 ipc_free_security(&sma->sem_perm);
5166 return rc;
5167 }
5168 return 0;
5169}
5170
5171static void selinux_sem_free_security(struct sem_array *sma)
5172{
5173 ipc_free_security(&sma->sem_perm);
5174}
5175
5176static int selinux_sem_associate(struct sem_array *sma, int semflg)
5177{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005179 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005180 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182 isec = sma->sem_perm.security;
5183
Eric Paris50c205f2012-04-04 15:01:43 -04005184 ad.type = LSM_AUDIT_DATA_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185 ad.u.ipc_id = sma->sem_perm.key;
5186
David Howells275bb412008-11-14 10:39:19 +11005187 return avc_has_perm(sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188 SEM__ASSOCIATE, &ad);
5189}
5190
5191/* Note, at this point, sma is locked down */
5192static int selinux_sem_semctl(struct sem_array *sma, int cmd)
5193{
5194 int err;
5195 u32 perms;
5196
Eric Paris828dfe12008-04-17 13:17:49 -04005197 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198 case IPC_INFO:
5199 case SEM_INFO:
5200 /* No specific object, just general system-wide information. */
5201 return task_has_system(current, SYSTEM__IPC_INFO);
5202 case GETPID:
5203 case GETNCNT:
5204 case GETZCNT:
5205 perms = SEM__GETATTR;
5206 break;
5207 case GETVAL:
5208 case GETALL:
5209 perms = SEM__READ;
5210 break;
5211 case SETVAL:
5212 case SETALL:
5213 perms = SEM__WRITE;
5214 break;
5215 case IPC_RMID:
5216 perms = SEM__DESTROY;
5217 break;
5218 case IPC_SET:
5219 perms = SEM__SETATTR;
5220 break;
5221 case IPC_STAT:
5222 case SEM_STAT:
5223 perms = SEM__GETATTR | SEM__ASSOCIATE;
5224 break;
5225 default:
5226 return 0;
5227 }
5228
Stephen Smalley6af963f2005-05-01 08:58:39 -07005229 err = ipc_has_perm(&sma->sem_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 return err;
5231}
5232
5233static int selinux_sem_semop(struct sem_array *sma,
5234 struct sembuf *sops, unsigned nsops, int alter)
5235{
5236 u32 perms;
5237
5238 if (alter)
5239 perms = SEM__READ | SEM__WRITE;
5240 else
5241 perms = SEM__READ;
5242
Stephen Smalley6af963f2005-05-01 08:58:39 -07005243 return ipc_has_perm(&sma->sem_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244}
5245
5246static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
5247{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248 u32 av = 0;
5249
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250 av = 0;
5251 if (flag & S_IRUGO)
5252 av |= IPC__UNIX_READ;
5253 if (flag & S_IWUGO)
5254 av |= IPC__UNIX_WRITE;
5255
5256 if (av == 0)
5257 return 0;
5258
Stephen Smalley6af963f2005-05-01 08:58:39 -07005259 return ipc_has_perm(ipcp, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260}
5261
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02005262static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
5263{
5264 struct ipc_security_struct *isec = ipcp->security;
5265 *secid = isec->sid;
5266}
5267
Eric Paris828dfe12008-04-17 13:17:49 -04005268static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269{
5270 if (inode)
5271 inode_doinit_with_dentry(inode, dentry);
5272}
5273
5274static int selinux_getprocattr(struct task_struct *p,
Al Viro04ff9702007-03-12 16:17:58 +00005275 char *name, char **value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276{
David Howells275bb412008-11-14 10:39:19 +11005277 const struct task_security_struct *__tsec;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00005278 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279 int error;
Al Viro04ff9702007-03-12 16:17:58 +00005280 unsigned len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281
5282 if (current != p) {
David Howells3b11a1d2008-11-14 10:39:26 +11005283 error = current_has_perm(p, PROCESS__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284 if (error)
5285 return error;
5286 }
5287
David Howells275bb412008-11-14 10:39:19 +11005288 rcu_read_lock();
5289 __tsec = __task_cred(p)->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290
5291 if (!strcmp(name, "current"))
David Howells275bb412008-11-14 10:39:19 +11005292 sid = __tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293 else if (!strcmp(name, "prev"))
David Howells275bb412008-11-14 10:39:19 +11005294 sid = __tsec->osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295 else if (!strcmp(name, "exec"))
David Howells275bb412008-11-14 10:39:19 +11005296 sid = __tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297 else if (!strcmp(name, "fscreate"))
David Howells275bb412008-11-14 10:39:19 +11005298 sid = __tsec->create_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07005299 else if (!strcmp(name, "keycreate"))
David Howells275bb412008-11-14 10:39:19 +11005300 sid = __tsec->keycreate_sid;
Eric Paris42c3e032006-06-26 00:26:03 -07005301 else if (!strcmp(name, "sockcreate"))
David Howells275bb412008-11-14 10:39:19 +11005302 sid = __tsec->sockcreate_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303 else
David Howells275bb412008-11-14 10:39:19 +11005304 goto invalid;
5305 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306
5307 if (!sid)
5308 return 0;
5309
Al Viro04ff9702007-03-12 16:17:58 +00005310 error = security_sid_to_context(sid, value, &len);
5311 if (error)
5312 return error;
5313 return len;
David Howells275bb412008-11-14 10:39:19 +11005314
5315invalid:
5316 rcu_read_unlock();
5317 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318}
5319
5320static int selinux_setprocattr(struct task_struct *p,
5321 char *name, void *value, size_t size)
5322{
5323 struct task_security_struct *tsec;
Roland McGrath03563572008-03-26 15:46:39 -07005324 struct task_struct *tracer;
David Howellsd84f4f92008-11-14 10:39:23 +11005325 struct cred *new;
5326 u32 sid = 0, ptsid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327 int error;
5328 char *str = value;
5329
5330 if (current != p) {
5331 /* SELinux only allows a process to change its own
5332 security attributes. */
5333 return -EACCES;
5334 }
5335
5336 /*
5337 * Basic control over ability to set these attributes at all.
5338 * current == p, but we'll pass them separately in case the
5339 * above restriction is ever removed.
5340 */
5341 if (!strcmp(name, "exec"))
David Howells3b11a1d2008-11-14 10:39:26 +11005342 error = current_has_perm(p, PROCESS__SETEXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343 else if (!strcmp(name, "fscreate"))
David Howells3b11a1d2008-11-14 10:39:26 +11005344 error = current_has_perm(p, PROCESS__SETFSCREATE);
Michael LeMay4eb582c2006-06-26 00:24:57 -07005345 else if (!strcmp(name, "keycreate"))
David Howells3b11a1d2008-11-14 10:39:26 +11005346 error = current_has_perm(p, PROCESS__SETKEYCREATE);
Eric Paris42c3e032006-06-26 00:26:03 -07005347 else if (!strcmp(name, "sockcreate"))
David Howells3b11a1d2008-11-14 10:39:26 +11005348 error = current_has_perm(p, PROCESS__SETSOCKCREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349 else if (!strcmp(name, "current"))
David Howells3b11a1d2008-11-14 10:39:26 +11005350 error = current_has_perm(p, PROCESS__SETCURRENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351 else
5352 error = -EINVAL;
5353 if (error)
5354 return error;
5355
5356 /* Obtain a SID for the context, if one was specified. */
5357 if (size && str[1] && str[1] != '\n') {
5358 if (str[size-1] == '\n') {
5359 str[size-1] = 0;
5360 size--;
5361 }
5362 error = security_context_to_sid(value, size, &sid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04005363 if (error == -EINVAL && !strcmp(name, "fscreate")) {
Eric Parisd6ea83e2012-04-04 13:45:49 -04005364 if (!capable(CAP_MAC_ADMIN)) {
5365 struct audit_buffer *ab;
5366 size_t audit_size;
5367
5368 /* We strip a nul only if it is at the end, otherwise the
5369 * context contains a nul and we should audit that */
5370 if (str[size - 1] == '\0')
5371 audit_size = size - 1;
5372 else
5373 audit_size = size;
5374 ab = audit_log_start(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR);
5375 audit_log_format(ab, "op=fscreate invalid_context=");
5376 audit_log_n_untrustedstring(ab, value, audit_size);
5377 audit_log_end(ab);
5378
Stephen Smalley12b29f32008-05-07 13:03:20 -04005379 return error;
Eric Parisd6ea83e2012-04-04 13:45:49 -04005380 }
Stephen Smalley12b29f32008-05-07 13:03:20 -04005381 error = security_context_to_sid_force(value, size,
5382 &sid);
5383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384 if (error)
5385 return error;
5386 }
5387
David Howellsd84f4f92008-11-14 10:39:23 +11005388 new = prepare_creds();
5389 if (!new)
5390 return -ENOMEM;
5391
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392 /* Permission checking based on the specified context is
5393 performed during the actual operation (execve,
5394 open/mkdir/...), when we know the full context of the
David Howellsd84f4f92008-11-14 10:39:23 +11005395 operation. See selinux_bprm_set_creds for the execve
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396 checks and may_create for the file creation checks. The
5397 operation will then fail if the context is not permitted. */
David Howellsd84f4f92008-11-14 10:39:23 +11005398 tsec = new->security;
5399 if (!strcmp(name, "exec")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400 tsec->exec_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005401 } else if (!strcmp(name, "fscreate")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402 tsec->create_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005403 } else if (!strcmp(name, "keycreate")) {
Michael LeMay4eb582c2006-06-26 00:24:57 -07005404 error = may_create_key(sid, p);
5405 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11005406 goto abort_change;
Michael LeMay4eb582c2006-06-26 00:24:57 -07005407 tsec->keycreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005408 } else if (!strcmp(name, "sockcreate")) {
Eric Paris42c3e032006-06-26 00:26:03 -07005409 tsec->sockcreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005410 } else if (!strcmp(name, "current")) {
5411 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412 if (sid == 0)
David Howellsd84f4f92008-11-14 10:39:23 +11005413 goto abort_change;
KaiGai Koheid9250de2008-08-28 16:35:57 +09005414
David Howellsd84f4f92008-11-14 10:39:23 +11005415 /* Only allow single threaded processes to change context */
5416 error = -EPERM;
Oleg Nesterov5bb459b2009-07-10 03:48:23 +02005417 if (!current_is_single_threaded()) {
David Howellsd84f4f92008-11-14 10:39:23 +11005418 error = security_bounded_transition(tsec->sid, sid);
5419 if (error)
5420 goto abort_change;
Eric Paris828dfe12008-04-17 13:17:49 -04005421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422
5423 /* Check permissions for the transition. */
5424 error = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS,
Eric Paris828dfe12008-04-17 13:17:49 -04005425 PROCESS__DYNTRANSITION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11005427 goto abort_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428
5429 /* Check for ptracing, and update the task SID if ok.
5430 Otherwise, leave SID unchanged and fail. */
David Howellsd84f4f92008-11-14 10:39:23 +11005431 ptsid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432 task_lock(p);
Tejun Heo06d98472011-06-17 16:50:40 +02005433 tracer = ptrace_parent(p);
David Howellsd84f4f92008-11-14 10:39:23 +11005434 if (tracer)
5435 ptsid = task_sid(tracer);
5436 task_unlock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437
David Howellsd84f4f92008-11-14 10:39:23 +11005438 if (tracer) {
5439 error = avc_has_perm(ptsid, sid, SECCLASS_PROCESS,
5440 PROCESS__PTRACE, NULL);
5441 if (error)
5442 goto abort_change;
5443 }
5444
5445 tsec->sid = sid;
5446 } else {
5447 error = -EINVAL;
5448 goto abort_change;
5449 }
5450
5451 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452 return size;
David Howellsd84f4f92008-11-14 10:39:23 +11005453
5454abort_change:
5455 abort_creds(new);
5456 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457}
5458
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005459static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
5460{
5461 return security_sid_to_context(secid, secdata, seclen);
5462}
5463
David Howells7bf570d2008-04-29 20:52:51 +01005464static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
David Howells63cb3442008-01-15 23:47:35 +00005465{
5466 return security_context_to_sid(secdata, seclen, secid);
5467}
5468
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005469static void selinux_release_secctx(char *secdata, u32 seclen)
5470{
Paul Moore088999e2007-08-01 11:12:58 -04005471 kfree(secdata);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005472}
5473
David P. Quigley1ee65e32009-09-03 14:25:57 -04005474/*
5475 * called with inode->i_mutex locked
5476 */
5477static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
5478{
5479 return selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, ctx, ctxlen, 0);
5480}
5481
5482/*
5483 * called with inode->i_mutex locked
5484 */
5485static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
5486{
5487 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0);
5488}
5489
5490static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
5491{
5492 int len = 0;
5493 len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX,
5494 ctx, true);
5495 if (len < 0)
5496 return len;
5497 *ctxlen = len;
5498 return 0;
5499}
Michael LeMayd7200242006-06-22 14:47:17 -07005500#ifdef CONFIG_KEYS
5501
David Howellsd84f4f92008-11-14 10:39:23 +11005502static int selinux_key_alloc(struct key *k, const struct cred *cred,
David Howells7e047ef2006-06-26 00:24:50 -07005503 unsigned long flags)
Michael LeMayd7200242006-06-22 14:47:17 -07005504{
David Howellsd84f4f92008-11-14 10:39:23 +11005505 const struct task_security_struct *tsec;
Michael LeMayd7200242006-06-22 14:47:17 -07005506 struct key_security_struct *ksec;
5507
5508 ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
5509 if (!ksec)
5510 return -ENOMEM;
5511
David Howellsd84f4f92008-11-14 10:39:23 +11005512 tsec = cred->security;
5513 if (tsec->keycreate_sid)
5514 ksec->sid = tsec->keycreate_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07005515 else
David Howellsd84f4f92008-11-14 10:39:23 +11005516 ksec->sid = tsec->sid;
Michael LeMayd7200242006-06-22 14:47:17 -07005517
David Howells275bb412008-11-14 10:39:19 +11005518 k->security = ksec;
Michael LeMayd7200242006-06-22 14:47:17 -07005519 return 0;
5520}
5521
5522static void selinux_key_free(struct key *k)
5523{
5524 struct key_security_struct *ksec = k->security;
5525
5526 k->security = NULL;
5527 kfree(ksec);
5528}
5529
5530static int selinux_key_permission(key_ref_t key_ref,
David Howellsd84f4f92008-11-14 10:39:23 +11005531 const struct cred *cred,
5532 key_perm_t perm)
Michael LeMayd7200242006-06-22 14:47:17 -07005533{
5534 struct key *key;
Michael LeMayd7200242006-06-22 14:47:17 -07005535 struct key_security_struct *ksec;
David Howells275bb412008-11-14 10:39:19 +11005536 u32 sid;
Michael LeMayd7200242006-06-22 14:47:17 -07005537
5538 /* if no specific permissions are requested, we skip the
5539 permission check. No serious, additional covert channels
5540 appear to be created. */
5541 if (perm == 0)
5542 return 0;
5543
David Howellsd84f4f92008-11-14 10:39:23 +11005544 sid = cred_sid(cred);
David Howells275bb412008-11-14 10:39:19 +11005545
5546 key = key_ref_to_ptr(key_ref);
5547 ksec = key->security;
5548
5549 return avc_has_perm(sid, ksec->sid, SECCLASS_KEY, perm, NULL);
Michael LeMayd7200242006-06-22 14:47:17 -07005550}
5551
David Howells70a5bb72008-04-29 01:01:26 -07005552static int selinux_key_getsecurity(struct key *key, char **_buffer)
5553{
5554 struct key_security_struct *ksec = key->security;
5555 char *context = NULL;
5556 unsigned len;
5557 int rc;
5558
5559 rc = security_sid_to_context(ksec->sid, &context, &len);
5560 if (!rc)
5561 rc = len;
5562 *_buffer = context;
5563 return rc;
5564}
5565
Michael LeMayd7200242006-06-22 14:47:17 -07005566#endif
5567
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568static struct security_operations selinux_ops = {
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02005569 .name = "selinux",
5570
Ingo Molnar9e488582009-05-07 19:26:19 +10005571 .ptrace_access_check = selinux_ptrace_access_check,
David Howells5cd9c582008-08-14 11:37:28 +01005572 .ptrace_traceme = selinux_ptrace_traceme,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 .capget = selinux_capget,
David Howellsd84f4f92008-11-14 10:39:23 +11005574 .capset = selinux_capset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575 .capable = selinux_capable,
5576 .quotactl = selinux_quotactl,
5577 .quota_on = selinux_quota_on,
5578 .syslog = selinux_syslog,
5579 .vm_enough_memory = selinux_vm_enough_memory,
5580
5581 .netlink_send = selinux_netlink_send,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582
David Howellsa6f76f22008-11-14 10:39:24 +11005583 .bprm_set_creds = selinux_bprm_set_creds,
David Howellsa6f76f22008-11-14 10:39:24 +11005584 .bprm_committing_creds = selinux_bprm_committing_creds,
5585 .bprm_committed_creds = selinux_bprm_committed_creds,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586 .bprm_secureexec = selinux_bprm_secureexec,
5587
5588 .sb_alloc_security = selinux_sb_alloc_security,
5589 .sb_free_security = selinux_sb_free_security,
5590 .sb_copy_data = selinux_sb_copy_data,
Eric Paris026eb162011-03-03 16:09:14 -05005591 .sb_remount = selinux_sb_remount,
Eric Paris828dfe12008-04-17 13:17:49 -04005592 .sb_kern_mount = selinux_sb_kern_mount,
Eric Paris2069f452008-07-04 09:47:13 +10005593 .sb_show_options = selinux_sb_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594 .sb_statfs = selinux_sb_statfs,
5595 .sb_mount = selinux_mount,
5596 .sb_umount = selinux_umount,
Eric Parisc9180a52007-11-30 13:00:35 -05005597 .sb_set_mnt_opts = selinux_set_mnt_opts,
Eric Paris828dfe12008-04-17 13:17:49 -04005598 .sb_clone_mnt_opts = selinux_sb_clone_mnt_opts,
Eric Parise0007522008-03-05 10:31:54 -05005599 .sb_parse_opts_str = selinux_parse_opts_str,
5600
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601
5602 .inode_alloc_security = selinux_inode_alloc_security,
5603 .inode_free_security = selinux_inode_free_security,
Stephen Smalley5e41ff92005-09-09 13:01:35 -07005604 .inode_init_security = selinux_inode_init_security,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605 .inode_create = selinux_inode_create,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606 .inode_link = selinux_inode_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607 .inode_unlink = selinux_inode_unlink,
5608 .inode_symlink = selinux_inode_symlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609 .inode_mkdir = selinux_inode_mkdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 .inode_rmdir = selinux_inode_rmdir,
5611 .inode_mknod = selinux_inode_mknod,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612 .inode_rename = selinux_inode_rename,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613 .inode_readlink = selinux_inode_readlink,
5614 .inode_follow_link = selinux_inode_follow_link,
5615 .inode_permission = selinux_inode_permission,
5616 .inode_setattr = selinux_inode_setattr,
5617 .inode_getattr = selinux_inode_getattr,
5618 .inode_setxattr = selinux_inode_setxattr,
5619 .inode_post_setxattr = selinux_inode_post_setxattr,
5620 .inode_getxattr = selinux_inode_getxattr,
5621 .inode_listxattr = selinux_inode_listxattr,
5622 .inode_removexattr = selinux_inode_removexattr,
Eric Paris828dfe12008-04-17 13:17:49 -04005623 .inode_getsecurity = selinux_inode_getsecurity,
5624 .inode_setsecurity = selinux_inode_setsecurity,
5625 .inode_listsecurity = selinux_inode_listsecurity,
Eric Parisf5269712008-05-14 11:27:45 -04005626 .inode_getsecid = selinux_inode_getsecid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627
5628 .file_permission = selinux_file_permission,
5629 .file_alloc_security = selinux_file_alloc_security,
5630 .file_free_security = selinux_file_free_security,
5631 .file_ioctl = selinux_file_ioctl,
Al Viroe5467852012-05-30 13:30:51 -04005632 .mmap_file = selinux_mmap_file,
5633 .mmap_addr = selinux_mmap_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634 .file_mprotect = selinux_file_mprotect,
5635 .file_lock = selinux_file_lock,
5636 .file_fcntl = selinux_file_fcntl,
5637 .file_set_fowner = selinux_file_set_fowner,
5638 .file_send_sigiotask = selinux_file_send_sigiotask,
5639 .file_receive = selinux_file_receive,
5640
Eric Paris83d49852012-04-04 13:45:40 -04005641 .file_open = selinux_file_open,
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09005642
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643 .task_create = selinux_task_create,
David Howellsee18d642009-09-02 09:14:21 +01005644 .cred_alloc_blank = selinux_cred_alloc_blank,
David Howellsf1752ee2008-11-14 10:39:17 +11005645 .cred_free = selinux_cred_free,
David Howellsd84f4f92008-11-14 10:39:23 +11005646 .cred_prepare = selinux_cred_prepare,
David Howellsee18d642009-09-02 09:14:21 +01005647 .cred_transfer = selinux_cred_transfer,
David Howells3a3b7ce2008-11-14 10:39:28 +11005648 .kernel_act_as = selinux_kernel_act_as,
5649 .kernel_create_files_as = selinux_kernel_create_files_as,
Eric Paris25354c42009-08-13 09:45:03 -04005650 .kernel_module_request = selinux_kernel_module_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651 .task_setpgid = selinux_task_setpgid,
5652 .task_getpgid = selinux_task_getpgid,
Eric Paris828dfe12008-04-17 13:17:49 -04005653 .task_getsid = selinux_task_getsid,
David Quigleyf9008e4c2006-06-30 01:55:46 -07005654 .task_getsecid = selinux_task_getsecid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655 .task_setnice = selinux_task_setnice,
James Morris03e68062006-06-23 02:03:58 -07005656 .task_setioprio = selinux_task_setioprio,
David Quigleya1836a42006-06-30 01:55:49 -07005657 .task_getioprio = selinux_task_getioprio,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658 .task_setrlimit = selinux_task_setrlimit,
5659 .task_setscheduler = selinux_task_setscheduler,
5660 .task_getscheduler = selinux_task_getscheduler,
David Quigley35601542006-06-23 02:04:01 -07005661 .task_movememory = selinux_task_movememory,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662 .task_kill = selinux_task_kill,
5663 .task_wait = selinux_task_wait,
Eric Paris828dfe12008-04-17 13:17:49 -04005664 .task_to_inode = selinux_task_to_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665
5666 .ipc_permission = selinux_ipc_permission,
Eric Parisf5269712008-05-14 11:27:45 -04005667 .ipc_getsecid = selinux_ipc_getsecid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668
5669 .msg_msg_alloc_security = selinux_msg_msg_alloc_security,
5670 .msg_msg_free_security = selinux_msg_msg_free_security,
5671
5672 .msg_queue_alloc_security = selinux_msg_queue_alloc_security,
5673 .msg_queue_free_security = selinux_msg_queue_free_security,
5674 .msg_queue_associate = selinux_msg_queue_associate,
5675 .msg_queue_msgctl = selinux_msg_queue_msgctl,
5676 .msg_queue_msgsnd = selinux_msg_queue_msgsnd,
5677 .msg_queue_msgrcv = selinux_msg_queue_msgrcv,
5678
5679 .shm_alloc_security = selinux_shm_alloc_security,
5680 .shm_free_security = selinux_shm_free_security,
5681 .shm_associate = selinux_shm_associate,
5682 .shm_shmctl = selinux_shm_shmctl,
5683 .shm_shmat = selinux_shm_shmat,
5684
Eric Paris828dfe12008-04-17 13:17:49 -04005685 .sem_alloc_security = selinux_sem_alloc_security,
5686 .sem_free_security = selinux_sem_free_security,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687 .sem_associate = selinux_sem_associate,
5688 .sem_semctl = selinux_sem_semctl,
5689 .sem_semop = selinux_sem_semop,
5690
Eric Paris828dfe12008-04-17 13:17:49 -04005691 .d_instantiate = selinux_d_instantiate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692
Eric Paris828dfe12008-04-17 13:17:49 -04005693 .getprocattr = selinux_getprocattr,
5694 .setprocattr = selinux_setprocattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005696 .secid_to_secctx = selinux_secid_to_secctx,
David Howells63cb3442008-01-15 23:47:35 +00005697 .secctx_to_secid = selinux_secctx_to_secid,
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005698 .release_secctx = selinux_release_secctx,
David P. Quigley1ee65e32009-09-03 14:25:57 -04005699 .inode_notifysecctx = selinux_inode_notifysecctx,
5700 .inode_setsecctx = selinux_inode_setsecctx,
5701 .inode_getsecctx = selinux_inode_getsecctx,
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005702
Eric Paris828dfe12008-04-17 13:17:49 -04005703 .unix_stream_connect = selinux_socket_unix_stream_connect,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704 .unix_may_send = selinux_socket_unix_may_send,
5705
5706 .socket_create = selinux_socket_create,
5707 .socket_post_create = selinux_socket_post_create,
5708 .socket_bind = selinux_socket_bind,
5709 .socket_connect = selinux_socket_connect,
5710 .socket_listen = selinux_socket_listen,
5711 .socket_accept = selinux_socket_accept,
5712 .socket_sendmsg = selinux_socket_sendmsg,
5713 .socket_recvmsg = selinux_socket_recvmsg,
5714 .socket_getsockname = selinux_socket_getsockname,
5715 .socket_getpeername = selinux_socket_getpeername,
5716 .socket_getsockopt = selinux_socket_getsockopt,
5717 .socket_setsockopt = selinux_socket_setsockopt,
5718 .socket_shutdown = selinux_socket_shutdown,
5719 .socket_sock_rcv_skb = selinux_socket_sock_rcv_skb,
Catherine Zhang2c7946a2006-03-20 22:41:23 -08005720 .socket_getpeersec_stream = selinux_socket_getpeersec_stream,
5721 .socket_getpeersec_dgram = selinux_socket_getpeersec_dgram,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722 .sk_alloc_security = selinux_sk_alloc_security,
5723 .sk_free_security = selinux_sk_free_security,
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005724 .sk_clone_security = selinux_sk_clone_security,
Eric Paris828dfe12008-04-17 13:17:49 -04005725 .sk_getsecid = selinux_sk_getsecid,
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005726 .sock_graft = selinux_sock_graft,
5727 .inet_conn_request = selinux_inet_conn_request,
5728 .inet_csk_clone = selinux_inet_csk_clone,
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005729 .inet_conn_established = selinux_inet_conn_established,
Eric Paris2606fd12010-10-13 16:24:41 -04005730 .secmark_relabel_packet = selinux_secmark_relabel_packet,
5731 .secmark_refcount_inc = selinux_secmark_refcount_inc,
5732 .secmark_refcount_dec = selinux_secmark_refcount_dec,
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005733 .req_classify_flow = selinux_req_classify_flow,
Paul Moore5dbbaf22013-01-14 07:12:19 +00005734 .tun_dev_alloc_security = selinux_tun_dev_alloc_security,
5735 .tun_dev_free_security = selinux_tun_dev_free_security,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005736 .tun_dev_create = selinux_tun_dev_create,
Paul Moore5dbbaf22013-01-14 07:12:19 +00005737 .tun_dev_attach_queue = selinux_tun_dev_attach_queue,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005738 .tun_dev_attach = selinux_tun_dev_attach,
Paul Moore5dbbaf22013-01-14 07:12:19 +00005739 .tun_dev_open = selinux_tun_dev_open,
Eric Dumazetca10b9e2013-04-08 17:58:11 +00005740 .skb_owned_by = selinux_skb_owned_by,
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005741
5742#ifdef CONFIG_SECURITY_NETWORK_XFRM
5743 .xfrm_policy_alloc_security = selinux_xfrm_policy_alloc,
5744 .xfrm_policy_clone_security = selinux_xfrm_policy_clone,
5745 .xfrm_policy_free_security = selinux_xfrm_policy_free,
Catherine Zhangc8c05a82006-06-08 23:39:49 -07005746 .xfrm_policy_delete_security = selinux_xfrm_policy_delete,
Paul Moore2e5aa862013-07-23 17:38:38 -04005747 .xfrm_state_alloc = selinux_xfrm_state_alloc,
5748 .xfrm_state_alloc_acquire = selinux_xfrm_state_alloc_acquire,
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005749 .xfrm_state_free_security = selinux_xfrm_state_free,
Catherine Zhangc8c05a82006-06-08 23:39:49 -07005750 .xfrm_state_delete_security = selinux_xfrm_state_delete,
Eric Paris828dfe12008-04-17 13:17:49 -04005751 .xfrm_policy_lookup = selinux_xfrm_policy_lookup,
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07005752 .xfrm_state_pol_flow_match = selinux_xfrm_state_pol_flow_match,
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07005753 .xfrm_decode_session = selinux_xfrm_decode_session,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754#endif
Michael LeMayd7200242006-06-22 14:47:17 -07005755
5756#ifdef CONFIG_KEYS
Eric Paris828dfe12008-04-17 13:17:49 -04005757 .key_alloc = selinux_key_alloc,
5758 .key_free = selinux_key_free,
5759 .key_permission = selinux_key_permission,
David Howells70a5bb72008-04-29 01:01:26 -07005760 .key_getsecurity = selinux_key_getsecurity,
Michael LeMayd7200242006-06-22 14:47:17 -07005761#endif
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02005762
5763#ifdef CONFIG_AUDIT
5764 .audit_rule_init = selinux_audit_rule_init,
5765 .audit_rule_known = selinux_audit_rule_known,
5766 .audit_rule_match = selinux_audit_rule_match,
5767 .audit_rule_free = selinux_audit_rule_free,
5768#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769};
5770
5771static __init int selinux_init(void)
5772{
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02005773 if (!security_module_enable(&selinux_ops)) {
5774 selinux_enabled = 0;
5775 return 0;
5776 }
5777
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778 if (!selinux_enabled) {
5779 printk(KERN_INFO "SELinux: Disabled at boot.\n");
5780 return 0;
5781 }
5782
5783 printk(KERN_INFO "SELinux: Initializing.\n");
5784
5785 /* Set the security state for the initial task. */
David Howellsd84f4f92008-11-14 10:39:23 +11005786 cred_init_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04005788 default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
5789
James Morris7cae7e22006-03-22 00:09:22 -08005790 sel_inode_cache = kmem_cache_create("selinux_inode_security",
5791 sizeof(struct inode_security_struct),
Paul Mundt20c2df82007-07-20 10:11:58 +09005792 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793 avc_init();
5794
Eric Paris828dfe12008-04-17 13:17:49 -04005795 if (register_security(&selinux_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796 panic("SELinux: Unable to register with kernel.\n");
5797
Eric Paris828dfe12008-04-17 13:17:49 -04005798 if (selinux_enforcing)
Eric Parisfadcdb42007-02-22 18:11:31 -05005799 printk(KERN_DEBUG "SELinux: Starting in enforcing mode\n");
Eric Paris828dfe12008-04-17 13:17:49 -04005800 else
Eric Parisfadcdb42007-02-22 18:11:31 -05005801 printk(KERN_DEBUG "SELinux: Starting in permissive mode\n");
Michael LeMayd7200242006-06-22 14:47:17 -07005802
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803 return 0;
5804}
5805
Al Viroe8c26252010-03-23 06:36:54 -04005806static void delayed_superblock_init(struct super_block *sb, void *unused)
5807{
5808 superblock_doinit(sb, NULL);
5809}
5810
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811void selinux_complete_init(void)
5812{
Eric Parisfadcdb42007-02-22 18:11:31 -05005813 printk(KERN_DEBUG "SELinux: Completing initialization.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814
5815 /* Set up any superblocks initialized prior to the policy load. */
Eric Parisfadcdb42007-02-22 18:11:31 -05005816 printk(KERN_DEBUG "SELinux: Setting up existing superblocks.\n");
Al Viroe8c26252010-03-23 06:36:54 -04005817 iterate_supers(delayed_superblock_init, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005818}
5819
5820/* SELinux requires early initialization in order to label
5821 all processes and objects when they are created. */
5822security_initcall(selinux_init);
5823
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08005824#if defined(CONFIG_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825
Paul Mooreeffad8d2008-01-29 08:49:27 -05005826static struct nf_hook_ops selinux_ipv4_ops[] = {
5827 {
5828 .hook = selinux_ipv4_postroute,
5829 .owner = THIS_MODULE,
Alban Crequy2597a832012-05-14 03:56:39 +00005830 .pf = NFPROTO_IPV4,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005831 .hooknum = NF_INET_POST_ROUTING,
5832 .priority = NF_IP_PRI_SELINUX_LAST,
5833 },
5834 {
5835 .hook = selinux_ipv4_forward,
5836 .owner = THIS_MODULE,
Alban Crequy2597a832012-05-14 03:56:39 +00005837 .pf = NFPROTO_IPV4,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005838 .hooknum = NF_INET_FORWARD,
5839 .priority = NF_IP_PRI_SELINUX_FIRST,
Paul Moore948bf852008-10-10 10:16:32 -04005840 },
5841 {
5842 .hook = selinux_ipv4_output,
5843 .owner = THIS_MODULE,
Alban Crequy2597a832012-05-14 03:56:39 +00005844 .pf = NFPROTO_IPV4,
Paul Moore948bf852008-10-10 10:16:32 -04005845 .hooknum = NF_INET_LOCAL_OUT,
5846 .priority = NF_IP_PRI_SELINUX_FIRST,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848};
5849
5850#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
5851
Paul Mooreeffad8d2008-01-29 08:49:27 -05005852static struct nf_hook_ops selinux_ipv6_ops[] = {
5853 {
5854 .hook = selinux_ipv6_postroute,
5855 .owner = THIS_MODULE,
Alban Crequy2597a832012-05-14 03:56:39 +00005856 .pf = NFPROTO_IPV6,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005857 .hooknum = NF_INET_POST_ROUTING,
5858 .priority = NF_IP6_PRI_SELINUX_LAST,
5859 },
5860 {
5861 .hook = selinux_ipv6_forward,
5862 .owner = THIS_MODULE,
Alban Crequy2597a832012-05-14 03:56:39 +00005863 .pf = NFPROTO_IPV6,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005864 .hooknum = NF_INET_FORWARD,
5865 .priority = NF_IP6_PRI_SELINUX_FIRST,
5866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867};
5868
5869#endif /* IPV6 */
5870
5871static int __init selinux_nf_ip_init(void)
5872{
5873 int err = 0;
5874
5875 if (!selinux_enabled)
5876 goto out;
Eric Parisfadcdb42007-02-22 18:11:31 -05005877
5878 printk(KERN_DEBUG "SELinux: Registering netfilter hooks\n");
5879
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07005880 err = nf_register_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops));
5881 if (err)
5882 panic("SELinux: nf_register_hooks for IPv4: error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005883
5884#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07005885 err = nf_register_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops));
5886 if (err)
5887 panic("SELinux: nf_register_hooks for IPv6: error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888#endif /* IPV6 */
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005889
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890out:
5891 return err;
5892}
5893
5894__initcall(selinux_nf_ip_init);
5895
5896#ifdef CONFIG_SECURITY_SELINUX_DISABLE
5897static void selinux_nf_ip_exit(void)
5898{
Eric Parisfadcdb42007-02-22 18:11:31 -05005899 printk(KERN_DEBUG "SELinux: Unregistering netfilter hooks\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07005901 nf_unregister_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005902#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07005903 nf_unregister_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904#endif /* IPV6 */
5905}
5906#endif
5907
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08005908#else /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005909
5910#ifdef CONFIG_SECURITY_SELINUX_DISABLE
5911#define selinux_nf_ip_exit()
5912#endif
5913
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08005914#endif /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005915
5916#ifdef CONFIG_SECURITY_SELINUX_DISABLE
Eric Paris828dfe12008-04-17 13:17:49 -04005917static int selinux_disabled;
5918
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919int selinux_disable(void)
5920{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921 if (ss_initialized) {
5922 /* Not permitted after initial policy load. */
5923 return -EINVAL;
5924 }
5925
5926 if (selinux_disabled) {
5927 /* Only do this once. */
5928 return -EINVAL;
5929 }
5930
5931 printk(KERN_INFO "SELinux: Disabled at runtime.\n");
5932
5933 selinux_disabled = 1;
Stephen Smalley30d55282006-05-03 10:52:36 -04005934 selinux_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005935
wzt.wzt@gmail.com189b3b12010-02-23 23:15:28 +08005936 reset_security_ops();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005937
Eric Parisaf8ff042009-09-20 21:23:01 -04005938 /* Try to destroy the avc node cache */
5939 avc_disable();
5940
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941 /* Unregister netfilter hooks. */
5942 selinux_nf_ip_exit();
5943
5944 /* Unregister selinuxfs. */
5945 exit_sel_fs();
5946
5947 return 0;
5948}
5949#endif