blob: 21a59218463321278a03d7d75b7f50617546a989 [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 Mooreeffad8d2008-01-29 08:49:27 -050016 * Copyright (C) 2006, 2007 Hewlett-Packard Development Company, L.P.
Eric Paris828dfe12008-04-17 13:17:49 -040017 * Paul Moore <paul.moore@hp.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>
27#include <linux/kernel.h>
Roland McGrath0d094ef2008-07-25 19:45:49 -070028#include <linux/tracehook.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/errno.h>
30#include <linux/sched.h>
31#include <linux/security.h>
32#include <linux/xattr.h>
33#include <linux/capability.h>
34#include <linux/unistd.h>
35#include <linux/mm.h>
36#include <linux/mman.h>
37#include <linux/slab.h>
38#include <linux/pagemap.h>
39#include <linux/swap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/spinlock.h>
41#include <linux/syscalls.h>
42#include <linux/file.h>
Al Viro9f3acc32008-04-24 07:44:08 -040043#include <linux/fdtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/namei.h>
45#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/netfilter_ipv4.h>
48#include <linux/netfilter_ipv6.h>
49#include <linux/tty.h>
50#include <net/icmp.h>
Stephen Hemminger227b60f2007-10-10 17:30:46 -070051#include <net/ip.h> /* for local_port_range[] */
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <net/tcp.h> /* struct or_callable used in sock_rcv_skb */
Paul Moore220deb92008-01-29 08:38:23 -050053#include <net/net_namespace.h>
Paul Moored621d352008-01-29 08:43:36 -050054#include <net/netlabel.h>
Eric Parisf5269712008-05-14 11:27:45 -040055#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <asm/ioctls.h>
Paul Moored621d352008-01-29 08:43:36 -050057#include <asm/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <linux/bitops.h>
59#include <linux/interrupt.h>
60#include <linux/netdevice.h> /* for network interface checks */
61#include <linux/netlink.h>
62#include <linux/tcp.h>
63#include <linux/udp.h>
James Morris2ee92d42006-11-13 16:09:01 -080064#include <linux/dccp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <linux/quota.h>
66#include <linux/un.h> /* for Unix socket types */
67#include <net/af_unix.h> /* for Unix socket types */
68#include <linux/parser.h>
69#include <linux/nfs_mount.h>
70#include <net/ipv6.h>
71#include <linux/hugetlb.h>
72#include <linux/personality.h>
73#include <linux/sysctl.h>
74#include <linux/audit.h>
Eric Paris6931dfc2005-06-30 02:58:51 -070075#include <linux/string.h>
Catherine Zhang877ce7c2006-06-29 12:27:47 -070076#include <linux/selinux.h>
Eric Paris23970742006-09-25 23:32:01 -070077#include <linux/mutex.h>
Frank Mayharf06febc2008-09-12 09:54:39 -070078#include <linux/posix-timers.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80#include "avc.h"
81#include "objsec.h"
82#include "netif.h"
Paul Moore224dfbd2008-01-29 08:38:13 -050083#include "netnode.h"
Paul Moore3e112172008-04-10 10:48:14 -040084#include "netport.h"
Trent Jaegerd28d1e02005-12-13 23:12:40 -080085#include "xfrm.h"
Paul Moorec60475b2007-02-28 15:14:23 -050086#include "netlabel.h"
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +020087#include "audit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89#define XATTR_SELINUX_SUFFIX "selinux"
90#define XATTR_NAME_SELINUX XATTR_SECURITY_PREFIX XATTR_SELINUX_SUFFIX
91
Eric Parisc9180a52007-11-30 13:00:35 -050092#define NUM_SEL_MNT_OPTS 4
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094extern unsigned int policydb_loaded_version;
95extern int selinux_nlmsg_lookup(u16 sclass, u16 nlmsg_type, u32 *perm);
James Morris4e5ab4c2006-06-09 00:33:33 -070096extern int selinux_compat_net;
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 */
100atomic_t selinux_secmark_refcount = ATOMIC_INIT(0);
101
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
James Morris6f0f0fd2008-07-10 17:02:07 +0900131/*
132 * Minimal support for a secondary security module,
133 * just to allow the use of the capability module.
134 */
Eric Paris828dfe12008-04-17 13:17:49 -0400135static struct security_operations *secondary_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137/* Lists of inode and superblock security structures initialized
138 before the policy was loaded. */
139static LIST_HEAD(superblock_security_head);
140static DEFINE_SPINLOCK(sb_security_lock);
141
Christoph Lametere18b8902006-12-06 20:33:20 -0800142static struct kmem_cache *sel_inode_cache;
James Morris7cae7e22006-03-22 00:09:22 -0800143
Paul Moored621d352008-01-29 08:43:36 -0500144/**
145 * selinux_secmark_enabled - Check to see if SECMARK is currently enabled
146 *
147 * Description:
148 * This function checks the SECMARK reference counter to see if any SECMARK
149 * targets are currently configured, if the reference counter is greater than
150 * zero SECMARK is considered to be enabled. Returns true (1) if SECMARK is
151 * enabled, false (0) if SECMARK is disabled.
152 *
153 */
154static int selinux_secmark_enabled(void)
155{
156 return (atomic_read(&selinux_secmark_refcount) > 0);
157}
158
David Howellsd84f4f92008-11-14 10:39:23 +1100159/*
160 * initialise the security for the init task
161 */
162static void cred_init_security(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
David Howellsd84f4f92008-11-14 10:39:23 +1100164 struct cred *cred = (struct cred *) current->cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 struct task_security_struct *tsec;
166
James Morris89d155e2005-10-30 14:59:21 -0800167 tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 if (!tsec)
David Howellsd84f4f92008-11-14 10:39:23 +1100169 panic("SELinux: Failed to initialize initial task.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
David Howellsd84f4f92008-11-14 10:39:23 +1100171 tsec->osid = tsec->sid = SECINITSID_KERNEL;
David Howellsf1752ee2008-11-14 10:39:17 +1100172 cred->security = tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
David Howells275bb412008-11-14 10:39:19 +1100175/*
David Howells88e67f32008-11-14 10:39:21 +1100176 * get the security ID of a set of credentials
177 */
178static inline u32 cred_sid(const struct cred *cred)
179{
180 const struct task_security_struct *tsec;
181
182 tsec = cred->security;
183 return tsec->sid;
184}
185
186/*
David Howells275bb412008-11-14 10:39:19 +1100187 * get the security ID of a task
188 */
189static inline u32 task_sid(const struct task_struct *task)
190{
David Howells275bb412008-11-14 10:39:19 +1100191 u32 sid;
192
193 rcu_read_lock();
David Howells88e67f32008-11-14 10:39:21 +1100194 sid = cred_sid(__task_cred(task));
David Howells275bb412008-11-14 10:39:19 +1100195 rcu_read_unlock();
196 return sid;
197}
198
199/*
200 * get the security ID of the current task
201 */
202static inline u32 current_sid(void)
203{
204 const struct task_security_struct *tsec = current_cred()->security;
205
206 return tsec->sid;
207}
208
David Howells88e67f32008-11-14 10:39:21 +1100209/* Allocate and free functions for each kind of security blob. */
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211static int inode_alloc_security(struct inode *inode)
212{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 struct inode_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +1100214 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Josef Bacika02fe132008-04-04 09:35:05 +1100216 isec = kmem_cache_zalloc(sel_inode_cache, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if (!isec)
218 return -ENOMEM;
219
Eric Paris23970742006-09-25 23:32:01 -0700220 mutex_init(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 INIT_LIST_HEAD(&isec->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 isec->inode = inode;
223 isec->sid = SECINITSID_UNLABELED;
224 isec->sclass = SECCLASS_FILE;
David Howells275bb412008-11-14 10:39:19 +1100225 isec->task_sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 inode->i_security = isec;
227
228 return 0;
229}
230
231static void inode_free_security(struct inode *inode)
232{
233 struct inode_security_struct *isec = inode->i_security;
234 struct superblock_security_struct *sbsec = inode->i_sb->s_security;
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 spin_lock(&sbsec->isec_lock);
237 if (!list_empty(&isec->list))
238 list_del_init(&isec->list);
239 spin_unlock(&sbsec->isec_lock);
240
241 inode->i_security = NULL;
James Morris7cae7e22006-03-22 00:09:22 -0800242 kmem_cache_free(sel_inode_cache, isec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
245static int file_alloc_security(struct file *file)
246{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 struct file_security_struct *fsec;
David Howells275bb412008-11-14 10:39:19 +1100248 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Stephen Smalley26d2a4b2006-02-01 03:05:55 -0800250 fsec = kzalloc(sizeof(struct file_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 if (!fsec)
252 return -ENOMEM;
253
David Howells275bb412008-11-14 10:39:19 +1100254 fsec->sid = sid;
255 fsec->fown_sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 file->f_security = fsec;
257
258 return 0;
259}
260
261static void file_free_security(struct file *file)
262{
263 struct file_security_struct *fsec = file->f_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 file->f_security = NULL;
265 kfree(fsec);
266}
267
268static int superblock_alloc_security(struct super_block *sb)
269{
270 struct superblock_security_struct *sbsec;
271
James Morris89d155e2005-10-30 14:59:21 -0800272 sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 if (!sbsec)
274 return -ENOMEM;
275
Eric Parisbc7e9822006-09-25 23:32:02 -0700276 mutex_init(&sbsec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 INIT_LIST_HEAD(&sbsec->list);
278 INIT_LIST_HEAD(&sbsec->isec_head);
279 spin_lock_init(&sbsec->isec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 sbsec->sb = sb;
281 sbsec->sid = SECINITSID_UNLABELED;
282 sbsec->def_sid = SECINITSID_FILE;
Eric Parisc312feb2006-07-10 04:43:53 -0700283 sbsec->mntpoint_sid = SECINITSID_UNLABELED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 sb->s_security = sbsec;
285
286 return 0;
287}
288
289static void superblock_free_security(struct super_block *sb)
290{
291 struct superblock_security_struct *sbsec = sb->s_security;
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 spin_lock(&sb_security_lock);
294 if (!list_empty(&sbsec->list))
295 list_del_init(&sbsec->list);
296 spin_unlock(&sb_security_lock);
297
298 sb->s_security = NULL;
299 kfree(sbsec);
300}
301
Al Viro7d877f32005-10-21 03:20:43 -0400302static int sk_alloc_security(struct sock *sk, int family, gfp_t priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
304 struct sk_security_struct *ssec;
305
James Morris89d155e2005-10-30 14:59:21 -0800306 ssec = kzalloc(sizeof(*ssec), priority);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 if (!ssec)
308 return -ENOMEM;
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 ssec->peer_sid = SECINITSID_UNLABELED;
Venkat Yekkirala892c1412006-08-04 23:08:56 -0700311 ssec->sid = SECINITSID_UNLABELED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 sk->sk_security = ssec;
313
Paul Mooref74af6e2008-02-25 11:40:33 -0500314 selinux_netlbl_sk_security_reset(ssec, family);
Paul Moore99f59ed2006-08-29 17:53:48 -0700315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return 0;
317}
318
319static void sk_free_security(struct sock *sk)
320{
321 struct sk_security_struct *ssec = sk->sk_security;
322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 sk->sk_security = NULL;
Paul Moore6c5b3fc2008-10-10 10:16:33 -0400324 selinux_netlbl_sk_security_free(ssec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 kfree(ssec);
326}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328/* The security server must be initialized before
329 any labeling or access decisions can be provided. */
330extern int ss_initialized;
331
332/* The file system's label must be initialized prior to use. */
333
334static char *labeling_behaviors[6] = {
335 "uses xattr",
336 "uses transition SIDs",
337 "uses task SIDs",
338 "uses genfs_contexts",
339 "not configured for labeling",
340 "uses mountpoint labeling",
341};
342
343static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry);
344
345static inline int inode_doinit(struct inode *inode)
346{
347 return inode_doinit_with_dentry(inode, NULL);
348}
349
350enum {
Eric Paris31e87932007-09-19 17:19:12 -0400351 Opt_error = -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 Opt_context = 1,
353 Opt_fscontext = 2,
Eric Parisc9180a52007-11-30 13:00:35 -0500354 Opt_defcontext = 3,
355 Opt_rootcontext = 4,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356};
357
Steven Whitehousea447c092008-10-13 10:46:57 +0100358static const match_table_t tokens = {
Eric Paris832cbd92008-04-01 13:24:09 -0400359 {Opt_context, CONTEXT_STR "%s"},
360 {Opt_fscontext, FSCONTEXT_STR "%s"},
361 {Opt_defcontext, DEFCONTEXT_STR "%s"},
362 {Opt_rootcontext, ROOTCONTEXT_STR "%s"},
Eric Paris31e87932007-09-19 17:19:12 -0400363 {Opt_error, NULL},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364};
365
366#define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n"
367
Eric Parisc312feb2006-07-10 04:43:53 -0700368static int may_context_mount_sb_relabel(u32 sid,
369 struct superblock_security_struct *sbsec,
David Howells275bb412008-11-14 10:39:19 +1100370 const struct cred *cred)
Eric Parisc312feb2006-07-10 04:43:53 -0700371{
David Howells275bb412008-11-14 10:39:19 +1100372 const struct task_security_struct *tsec = cred->security;
Eric Parisc312feb2006-07-10 04:43:53 -0700373 int rc;
374
375 rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
376 FILESYSTEM__RELABELFROM, NULL);
377 if (rc)
378 return rc;
379
380 rc = avc_has_perm(tsec->sid, sid, SECCLASS_FILESYSTEM,
381 FILESYSTEM__RELABELTO, NULL);
382 return rc;
383}
384
Eric Paris08089252006-07-10 04:43:55 -0700385static int may_context_mount_inode_relabel(u32 sid,
386 struct superblock_security_struct *sbsec,
David Howells275bb412008-11-14 10:39:19 +1100387 const struct cred *cred)
Eric Paris08089252006-07-10 04:43:55 -0700388{
David Howells275bb412008-11-14 10:39:19 +1100389 const struct task_security_struct *tsec = cred->security;
Eric Paris08089252006-07-10 04:43:55 -0700390 int rc;
391 rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
392 FILESYSTEM__RELABELFROM, NULL);
393 if (rc)
394 return rc;
395
396 rc = avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM,
397 FILESYSTEM__ASSOCIATE, NULL);
398 return rc;
399}
400
Eric Parisc9180a52007-11-30 13:00:35 -0500401static int sb_finish_set_opts(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
403 struct superblock_security_struct *sbsec = sb->s_security;
404 struct dentry *root = sb->s_root;
Eric Parisc9180a52007-11-30 13:00:35 -0500405 struct inode *root_inode = root->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 int rc = 0;
407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
409 /* Make sure that the xattr handler exists and that no
410 error other than -ENODATA is returned by getxattr on
411 the root directory. -ENODATA is ok, as this may be
412 the first boot of the SELinux kernel before we have
413 assigned xattr values to the filesystem. */
Eric Parisc9180a52007-11-30 13:00:35 -0500414 if (!root_inode->i_op->getxattr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 printk(KERN_WARNING "SELinux: (dev %s, type %s) has no "
416 "xattr support\n", sb->s_id, sb->s_type->name);
417 rc = -EOPNOTSUPP;
418 goto out;
419 }
Eric Parisc9180a52007-11-30 13:00:35 -0500420 rc = root_inode->i_op->getxattr(root, XATTR_NAME_SELINUX, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 if (rc < 0 && rc != -ENODATA) {
422 if (rc == -EOPNOTSUPP)
423 printk(KERN_WARNING "SELinux: (dev %s, type "
424 "%s) has no security xattr handler\n",
425 sb->s_id, sb->s_type->name);
426 else
427 printk(KERN_WARNING "SELinux: (dev %s, type "
428 "%s) getxattr errno %d\n", sb->s_id,
429 sb->s_type->name, -rc);
430 goto out;
431 }
432 }
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 sbsec->initialized = 1;
435
Eric Parisc9180a52007-11-30 13:00:35 -0500436 if (sbsec->behavior > ARRAY_SIZE(labeling_behaviors))
Eric Parisfadcdb42007-02-22 18:11:31 -0500437 printk(KERN_ERR "SELinux: initialized (dev %s, type %s), unknown behavior\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 sb->s_id, sb->s_type->name);
Eric Parisc9180a52007-11-30 13:00:35 -0500439 else
Eric Parisfadcdb42007-02-22 18:11:31 -0500440 printk(KERN_DEBUG "SELinux: initialized (dev %s, type %s), %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 sb->s_id, sb->s_type->name,
442 labeling_behaviors[sbsec->behavior-1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 /* Initialize the root inode. */
Eric Parisc9180a52007-11-30 13:00:35 -0500445 rc = inode_doinit_with_dentry(root_inode, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 /* Initialize any other inodes associated with the superblock, e.g.
448 inodes created prior to initial policy load or inodes created
449 during get_sb by a pseudo filesystem that directly
450 populates itself. */
451 spin_lock(&sbsec->isec_lock);
452next_inode:
453 if (!list_empty(&sbsec->isec_head)) {
454 struct inode_security_struct *isec =
455 list_entry(sbsec->isec_head.next,
Eric Parisc9180a52007-11-30 13:00:35 -0500456 struct inode_security_struct, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 struct inode *inode = isec->inode;
458 spin_unlock(&sbsec->isec_lock);
459 inode = igrab(inode);
460 if (inode) {
Eric Parisc9180a52007-11-30 13:00:35 -0500461 if (!IS_PRIVATE(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 inode_doinit(inode);
463 iput(inode);
464 }
465 spin_lock(&sbsec->isec_lock);
466 list_del_init(&isec->list);
467 goto next_inode;
468 }
469 spin_unlock(&sbsec->isec_lock);
470out:
Eric Parisc9180a52007-11-30 13:00:35 -0500471 return rc;
472}
473
474/*
475 * This function should allow an FS to ask what it's mount security
476 * options were so it can use those later for submounts, displaying
477 * mount options, or whatever.
478 */
479static int selinux_get_mnt_opts(const struct super_block *sb,
Eric Parise0007522008-03-05 10:31:54 -0500480 struct security_mnt_opts *opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500481{
482 int rc = 0, i;
483 struct superblock_security_struct *sbsec = sb->s_security;
484 char *context = NULL;
485 u32 len;
486 char tmp;
487
Eric Parise0007522008-03-05 10:31:54 -0500488 security_init_mnt_opts(opts);
Eric Parisc9180a52007-11-30 13:00:35 -0500489
490 if (!sbsec->initialized)
491 return -EINVAL;
492
493 if (!ss_initialized)
494 return -EINVAL;
495
496 /*
497 * if we ever use sbsec flags for anything other than tracking mount
498 * settings this is going to need a mask
499 */
500 tmp = sbsec->flags;
501 /* count the number of mount options for this sb */
502 for (i = 0; i < 8; i++) {
503 if (tmp & 0x01)
Eric Parise0007522008-03-05 10:31:54 -0500504 opts->num_mnt_opts++;
Eric Parisc9180a52007-11-30 13:00:35 -0500505 tmp >>= 1;
506 }
507
Eric Parise0007522008-03-05 10:31:54 -0500508 opts->mnt_opts = kcalloc(opts->num_mnt_opts, sizeof(char *), GFP_ATOMIC);
509 if (!opts->mnt_opts) {
Eric Parisc9180a52007-11-30 13:00:35 -0500510 rc = -ENOMEM;
511 goto out_free;
512 }
513
Eric Parise0007522008-03-05 10:31:54 -0500514 opts->mnt_opts_flags = kcalloc(opts->num_mnt_opts, sizeof(int), GFP_ATOMIC);
515 if (!opts->mnt_opts_flags) {
Eric Parisc9180a52007-11-30 13:00:35 -0500516 rc = -ENOMEM;
517 goto out_free;
518 }
519
520 i = 0;
521 if (sbsec->flags & FSCONTEXT_MNT) {
522 rc = security_sid_to_context(sbsec->sid, &context, &len);
523 if (rc)
524 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500525 opts->mnt_opts[i] = context;
526 opts->mnt_opts_flags[i++] = FSCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500527 }
528 if (sbsec->flags & CONTEXT_MNT) {
529 rc = security_sid_to_context(sbsec->mntpoint_sid, &context, &len);
530 if (rc)
531 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500532 opts->mnt_opts[i] = context;
533 opts->mnt_opts_flags[i++] = CONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500534 }
535 if (sbsec->flags & DEFCONTEXT_MNT) {
536 rc = security_sid_to_context(sbsec->def_sid, &context, &len);
537 if (rc)
538 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500539 opts->mnt_opts[i] = context;
540 opts->mnt_opts_flags[i++] = DEFCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500541 }
542 if (sbsec->flags & ROOTCONTEXT_MNT) {
543 struct inode *root = sbsec->sb->s_root->d_inode;
544 struct inode_security_struct *isec = root->i_security;
545
546 rc = security_sid_to_context(isec->sid, &context, &len);
547 if (rc)
548 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500549 opts->mnt_opts[i] = context;
550 opts->mnt_opts_flags[i++] = ROOTCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500551 }
552
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{
565 /* check if the old mount command had the same options */
566 if (sbsec->initialized)
567 if (!(sbsec->flags & flag) ||
568 (old_sid != new_sid))
569 return 1;
570
571 /* check if we were passed the same options twice,
572 * aka someone passed context=a,context=b
573 */
574 if (!sbsec->initialized)
575 if (sbsec->flags & flag)
576 return 1;
577 return 0;
578}
Eric Parise0007522008-03-05 10:31:54 -0500579
Eric Parisc9180a52007-11-30 13:00:35 -0500580/*
581 * Allow filesystems with binary mount data to explicitly set mount point
582 * labeling information.
583 */
Eric Parise0007522008-03-05 10:31:54 -0500584static int selinux_set_mnt_opts(struct super_block *sb,
585 struct security_mnt_opts *opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500586{
David Howells275bb412008-11-14 10:39:19 +1100587 const struct cred *cred = current_cred();
Eric Parisc9180a52007-11-30 13:00:35 -0500588 int rc = 0, i;
Eric Parisc9180a52007-11-30 13:00:35 -0500589 struct superblock_security_struct *sbsec = sb->s_security;
590 const char *name = sb->s_type->name;
James Morris089be432008-07-15 18:32:49 +1000591 struct inode *inode = sbsec->sb->s_root->d_inode;
592 struct inode_security_struct *root_isec = inode->i_security;
Eric Parisc9180a52007-11-30 13:00:35 -0500593 u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
594 u32 defcontext_sid = 0;
Eric Parise0007522008-03-05 10:31:54 -0500595 char **mount_options = opts->mnt_opts;
596 int *flags = opts->mnt_opts_flags;
597 int num_opts = opts->num_mnt_opts;
Eric Parisc9180a52007-11-30 13:00:35 -0500598
599 mutex_lock(&sbsec->lock);
600
601 if (!ss_initialized) {
602 if (!num_opts) {
603 /* Defer initialization until selinux_complete_init,
604 after the initial policy is loaded and the security
605 server is ready to handle calls. */
606 spin_lock(&sb_security_lock);
607 if (list_empty(&sbsec->list))
608 list_add(&sbsec->list, &superblock_security_head);
609 spin_unlock(&sb_security_lock);
610 goto out;
611 }
612 rc = -EINVAL;
Eric Paris744ba352008-04-17 11:52:44 -0400613 printk(KERN_WARNING "SELinux: Unable to set superblock options "
614 "before the security server is initialized\n");
Eric Parisc9180a52007-11-30 13:00:35 -0500615 goto out;
616 }
617
618 /*
Eric Parise0007522008-03-05 10:31:54 -0500619 * Binary mount data FS will come through this function twice. Once
620 * from an explicit call and once from the generic calls from the vfs.
621 * Since the generic VFS calls will not contain any security mount data
622 * we need to skip the double mount verification.
623 *
624 * This does open a hole in which we will not notice if the first
625 * mount using this sb set explict options and a second mount using
626 * this sb does not set any security options. (The first options
627 * will be used for both mounts)
628 */
629 if (sbsec->initialized && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
630 && (num_opts == 0))
Eric Parisf5269712008-05-14 11:27:45 -0400631 goto out;
Eric Parise0007522008-03-05 10:31:54 -0500632
633 /*
Eric Parisc9180a52007-11-30 13:00:35 -0500634 * parse the mount options, check if they are valid sids.
635 * also check if someone is trying to mount the same sb more
636 * than once with different security options.
637 */
638 for (i = 0; i < num_opts; i++) {
639 u32 sid;
640 rc = security_context_to_sid(mount_options[i],
641 strlen(mount_options[i]), &sid);
642 if (rc) {
643 printk(KERN_WARNING "SELinux: security_context_to_sid"
644 "(%s) failed for (dev %s, type %s) errno=%d\n",
645 mount_options[i], sb->s_id, name, rc);
646 goto out;
647 }
648 switch (flags[i]) {
649 case FSCONTEXT_MNT:
650 fscontext_sid = sid;
651
652 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
653 fscontext_sid))
654 goto out_double_mount;
655
656 sbsec->flags |= FSCONTEXT_MNT;
657 break;
658 case CONTEXT_MNT:
659 context_sid = sid;
660
661 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
662 context_sid))
663 goto out_double_mount;
664
665 sbsec->flags |= CONTEXT_MNT;
666 break;
667 case ROOTCONTEXT_MNT:
668 rootcontext_sid = sid;
669
670 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
671 rootcontext_sid))
672 goto out_double_mount;
673
674 sbsec->flags |= ROOTCONTEXT_MNT;
675
676 break;
677 case DEFCONTEXT_MNT:
678 defcontext_sid = sid;
679
680 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
681 defcontext_sid))
682 goto out_double_mount;
683
684 sbsec->flags |= DEFCONTEXT_MNT;
685
686 break;
687 default:
688 rc = -EINVAL;
689 goto out;
690 }
691 }
692
693 if (sbsec->initialized) {
694 /* previously mounted with options, but not on this attempt? */
695 if (sbsec->flags && !num_opts)
696 goto out_double_mount;
697 rc = 0;
698 goto out;
699 }
700
James Morris089be432008-07-15 18:32:49 +1000701 if (strcmp(sb->s_type->name, "proc") == 0)
Eric Parisc9180a52007-11-30 13:00:35 -0500702 sbsec->proc = 1;
703
704 /* Determine the labeling behavior to use for this filesystem type. */
James Morris089be432008-07-15 18:32:49 +1000705 rc = security_fs_use(sb->s_type->name, &sbsec->behavior, &sbsec->sid);
Eric Parisc9180a52007-11-30 13:00:35 -0500706 if (rc) {
707 printk(KERN_WARNING "%s: security_fs_use(%s) returned %d\n",
James Morris089be432008-07-15 18:32:49 +1000708 __func__, sb->s_type->name, rc);
Eric Parisc9180a52007-11-30 13:00:35 -0500709 goto out;
710 }
711
712 /* sets the context of the superblock for the fs being mounted. */
713 if (fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100714 rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500715 if (rc)
716 goto out;
717
718 sbsec->sid = fscontext_sid;
719 }
720
721 /*
722 * Switch to using mount point labeling behavior.
723 * sets the label used on all file below the mountpoint, and will set
724 * the superblock context if not already set.
725 */
726 if (context_sid) {
727 if (!fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100728 rc = may_context_mount_sb_relabel(context_sid, sbsec,
729 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500730 if (rc)
731 goto out;
732 sbsec->sid = context_sid;
733 } else {
David Howells275bb412008-11-14 10:39:19 +1100734 rc = may_context_mount_inode_relabel(context_sid, sbsec,
735 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500736 if (rc)
737 goto out;
738 }
739 if (!rootcontext_sid)
740 rootcontext_sid = context_sid;
741
742 sbsec->mntpoint_sid = context_sid;
743 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
744 }
745
746 if (rootcontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100747 rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec,
748 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500749 if (rc)
750 goto out;
751
752 root_isec->sid = rootcontext_sid;
753 root_isec->initialized = 1;
754 }
755
756 if (defcontext_sid) {
757 if (sbsec->behavior != SECURITY_FS_USE_XATTR) {
758 rc = -EINVAL;
759 printk(KERN_WARNING "SELinux: defcontext option is "
760 "invalid for this filesystem type\n");
761 goto out;
762 }
763
764 if (defcontext_sid != sbsec->def_sid) {
765 rc = may_context_mount_inode_relabel(defcontext_sid,
David Howells275bb412008-11-14 10:39:19 +1100766 sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500767 if (rc)
768 goto out;
769 }
770
771 sbsec->def_sid = defcontext_sid;
772 }
773
774 rc = sb_finish_set_opts(sb);
775out:
Eric Parisbc7e9822006-09-25 23:32:02 -0700776 mutex_unlock(&sbsec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 return rc;
Eric Parisc9180a52007-11-30 13:00:35 -0500778out_double_mount:
779 rc = -EINVAL;
780 printk(KERN_WARNING "SELinux: mount invalid. Same superblock, different "
781 "security settings for (dev %s, type %s)\n", sb->s_id, name);
782 goto out;
783}
784
785static void selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
786 struct super_block *newsb)
787{
788 const struct superblock_security_struct *oldsbsec = oldsb->s_security;
789 struct superblock_security_struct *newsbsec = newsb->s_security;
790
791 int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT);
792 int set_context = (oldsbsec->flags & CONTEXT_MNT);
793 int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT);
794
Eric Paris0f5e6422008-04-21 16:24:11 -0400795 /*
796 * if the parent was able to be mounted it clearly had no special lsm
797 * mount options. thus we can safely put this sb on the list and deal
798 * with it later
799 */
800 if (!ss_initialized) {
801 spin_lock(&sb_security_lock);
802 if (list_empty(&newsbsec->list))
803 list_add(&newsbsec->list, &superblock_security_head);
804 spin_unlock(&sb_security_lock);
805 return;
806 }
Eric Parisc9180a52007-11-30 13:00:35 -0500807
Eric Parisc9180a52007-11-30 13:00:35 -0500808 /* how can we clone if the old one wasn't set up?? */
809 BUG_ON(!oldsbsec->initialized);
810
Eric Paris5a552612008-04-09 14:08:35 -0400811 /* if fs is reusing a sb, just let its options stand... */
812 if (newsbsec->initialized)
813 return;
814
Eric Parisc9180a52007-11-30 13:00:35 -0500815 mutex_lock(&newsbsec->lock);
816
817 newsbsec->flags = oldsbsec->flags;
818
819 newsbsec->sid = oldsbsec->sid;
820 newsbsec->def_sid = oldsbsec->def_sid;
821 newsbsec->behavior = oldsbsec->behavior;
822
823 if (set_context) {
824 u32 sid = oldsbsec->mntpoint_sid;
825
826 if (!set_fscontext)
827 newsbsec->sid = sid;
828 if (!set_rootcontext) {
829 struct inode *newinode = newsb->s_root->d_inode;
830 struct inode_security_struct *newisec = newinode->i_security;
831 newisec->sid = sid;
832 }
833 newsbsec->mntpoint_sid = sid;
834 }
835 if (set_rootcontext) {
836 const struct inode *oldinode = oldsb->s_root->d_inode;
837 const struct inode_security_struct *oldisec = oldinode->i_security;
838 struct inode *newinode = newsb->s_root->d_inode;
839 struct inode_security_struct *newisec = newinode->i_security;
840
841 newisec->sid = oldisec->sid;
842 }
843
844 sb_finish_set_opts(newsb);
845 mutex_unlock(&newsbsec->lock);
846}
847
Adrian Bunk2e1479d2008-03-17 22:29:23 +0200848static int selinux_parse_opts_str(char *options,
849 struct security_mnt_opts *opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500850{
Eric Parise0007522008-03-05 10:31:54 -0500851 char *p;
Eric Parisc9180a52007-11-30 13:00:35 -0500852 char *context = NULL, *defcontext = NULL;
853 char *fscontext = NULL, *rootcontext = NULL;
Eric Parise0007522008-03-05 10:31:54 -0500854 int rc, num_mnt_opts = 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500855
Eric Parise0007522008-03-05 10:31:54 -0500856 opts->num_mnt_opts = 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500857
858 /* Standard string-based options. */
859 while ((p = strsep(&options, "|")) != NULL) {
860 int token;
861 substring_t args[MAX_OPT_ARGS];
862
863 if (!*p)
864 continue;
865
866 token = match_token(p, tokens, args);
867
868 switch (token) {
869 case Opt_context:
870 if (context || defcontext) {
871 rc = -EINVAL;
872 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
873 goto out_err;
874 }
875 context = match_strdup(&args[0]);
876 if (!context) {
877 rc = -ENOMEM;
878 goto out_err;
879 }
880 break;
881
882 case Opt_fscontext:
883 if (fscontext) {
884 rc = -EINVAL;
885 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
886 goto out_err;
887 }
888 fscontext = match_strdup(&args[0]);
889 if (!fscontext) {
890 rc = -ENOMEM;
891 goto out_err;
892 }
893 break;
894
895 case Opt_rootcontext:
896 if (rootcontext) {
897 rc = -EINVAL;
898 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
899 goto out_err;
900 }
901 rootcontext = match_strdup(&args[0]);
902 if (!rootcontext) {
903 rc = -ENOMEM;
904 goto out_err;
905 }
906 break;
907
908 case Opt_defcontext:
909 if (context || defcontext) {
910 rc = -EINVAL;
911 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
912 goto out_err;
913 }
914 defcontext = match_strdup(&args[0]);
915 if (!defcontext) {
916 rc = -ENOMEM;
917 goto out_err;
918 }
919 break;
920
921 default:
922 rc = -EINVAL;
923 printk(KERN_WARNING "SELinux: unknown mount option\n");
924 goto out_err;
925
926 }
927 }
928
Eric Parise0007522008-03-05 10:31:54 -0500929 rc = -ENOMEM;
930 opts->mnt_opts = kcalloc(NUM_SEL_MNT_OPTS, sizeof(char *), GFP_ATOMIC);
931 if (!opts->mnt_opts)
932 goto out_err;
933
934 opts->mnt_opts_flags = kcalloc(NUM_SEL_MNT_OPTS, sizeof(int), GFP_ATOMIC);
935 if (!opts->mnt_opts_flags) {
936 kfree(opts->mnt_opts);
937 goto out_err;
Eric Parisc9180a52007-11-30 13:00:35 -0500938 }
939
Eric Parise0007522008-03-05 10:31:54 -0500940 if (fscontext) {
941 opts->mnt_opts[num_mnt_opts] = fscontext;
942 opts->mnt_opts_flags[num_mnt_opts++] = FSCONTEXT_MNT;
943 }
944 if (context) {
945 opts->mnt_opts[num_mnt_opts] = context;
946 opts->mnt_opts_flags[num_mnt_opts++] = CONTEXT_MNT;
947 }
948 if (rootcontext) {
949 opts->mnt_opts[num_mnt_opts] = rootcontext;
950 opts->mnt_opts_flags[num_mnt_opts++] = ROOTCONTEXT_MNT;
951 }
952 if (defcontext) {
953 opts->mnt_opts[num_mnt_opts] = defcontext;
954 opts->mnt_opts_flags[num_mnt_opts++] = DEFCONTEXT_MNT;
955 }
956
957 opts->num_mnt_opts = num_mnt_opts;
958 return 0;
959
Eric Parisc9180a52007-11-30 13:00:35 -0500960out_err:
961 kfree(context);
962 kfree(defcontext);
963 kfree(fscontext);
964 kfree(rootcontext);
965 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966}
Eric Parise0007522008-03-05 10:31:54 -0500967/*
968 * string mount options parsing and call set the sbsec
969 */
970static int superblock_doinit(struct super_block *sb, void *data)
971{
972 int rc = 0;
973 char *options = data;
974 struct security_mnt_opts opts;
975
976 security_init_mnt_opts(&opts);
977
978 if (!data)
979 goto out;
980
981 BUG_ON(sb->s_type->fs_flags & FS_BINARY_MOUNTDATA);
982
983 rc = selinux_parse_opts_str(options, &opts);
984 if (rc)
985 goto out_err;
986
987out:
988 rc = selinux_set_mnt_opts(sb, &opts);
989
990out_err:
991 security_free_mnt_opts(&opts);
992 return rc;
993}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Adrian Bunk3583a712008-07-22 20:21:23 +0300995static void selinux_write_opts(struct seq_file *m,
996 struct security_mnt_opts *opts)
Eric Paris2069f452008-07-04 09:47:13 +1000997{
998 int i;
999 char *prefix;
1000
1001 for (i = 0; i < opts->num_mnt_opts; i++) {
1002 char *has_comma = strchr(opts->mnt_opts[i], ',');
1003
1004 switch (opts->mnt_opts_flags[i]) {
1005 case CONTEXT_MNT:
1006 prefix = CONTEXT_STR;
1007 break;
1008 case FSCONTEXT_MNT:
1009 prefix = FSCONTEXT_STR;
1010 break;
1011 case ROOTCONTEXT_MNT:
1012 prefix = ROOTCONTEXT_STR;
1013 break;
1014 case DEFCONTEXT_MNT:
1015 prefix = DEFCONTEXT_STR;
1016 break;
1017 default:
1018 BUG();
1019 };
1020 /* we need a comma before each option */
1021 seq_putc(m, ',');
1022 seq_puts(m, prefix);
1023 if (has_comma)
1024 seq_putc(m, '\"');
1025 seq_puts(m, opts->mnt_opts[i]);
1026 if (has_comma)
1027 seq_putc(m, '\"');
1028 }
1029}
1030
1031static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
1032{
1033 struct security_mnt_opts opts;
1034 int rc;
1035
1036 rc = selinux_get_mnt_opts(sb, &opts);
Eric Paris383795c2008-07-29 17:07:26 -04001037 if (rc) {
1038 /* before policy load we may get EINVAL, don't show anything */
1039 if (rc == -EINVAL)
1040 rc = 0;
Eric Paris2069f452008-07-04 09:47:13 +10001041 return rc;
Eric Paris383795c2008-07-29 17:07:26 -04001042 }
Eric Paris2069f452008-07-04 09:47:13 +10001043
1044 selinux_write_opts(m, &opts);
1045
1046 security_free_mnt_opts(&opts);
1047
1048 return rc;
1049}
1050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051static inline u16 inode_mode_to_security_class(umode_t mode)
1052{
1053 switch (mode & S_IFMT) {
1054 case S_IFSOCK:
1055 return SECCLASS_SOCK_FILE;
1056 case S_IFLNK:
1057 return SECCLASS_LNK_FILE;
1058 case S_IFREG:
1059 return SECCLASS_FILE;
1060 case S_IFBLK:
1061 return SECCLASS_BLK_FILE;
1062 case S_IFDIR:
1063 return SECCLASS_DIR;
1064 case S_IFCHR:
1065 return SECCLASS_CHR_FILE;
1066 case S_IFIFO:
1067 return SECCLASS_FIFO_FILE;
1068
1069 }
1070
1071 return SECCLASS_FILE;
1072}
1073
James Morris13402582005-09-30 14:24:34 -04001074static inline int default_protocol_stream(int protocol)
1075{
1076 return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP);
1077}
1078
1079static inline int default_protocol_dgram(int protocol)
1080{
1081 return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
1082}
1083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084static inline u16 socket_type_to_security_class(int family, int type, int protocol)
1085{
1086 switch (family) {
1087 case PF_UNIX:
1088 switch (type) {
1089 case SOCK_STREAM:
1090 case SOCK_SEQPACKET:
1091 return SECCLASS_UNIX_STREAM_SOCKET;
1092 case SOCK_DGRAM:
1093 return SECCLASS_UNIX_DGRAM_SOCKET;
1094 }
1095 break;
1096 case PF_INET:
1097 case PF_INET6:
1098 switch (type) {
1099 case SOCK_STREAM:
James Morris13402582005-09-30 14:24:34 -04001100 if (default_protocol_stream(protocol))
1101 return SECCLASS_TCP_SOCKET;
1102 else
1103 return SECCLASS_RAWIP_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 case SOCK_DGRAM:
James Morris13402582005-09-30 14:24:34 -04001105 if (default_protocol_dgram(protocol))
1106 return SECCLASS_UDP_SOCKET;
1107 else
1108 return SECCLASS_RAWIP_SOCKET;
James Morris2ee92d42006-11-13 16:09:01 -08001109 case SOCK_DCCP:
1110 return SECCLASS_DCCP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001111 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 return SECCLASS_RAWIP_SOCKET;
1113 }
1114 break;
1115 case PF_NETLINK:
1116 switch (protocol) {
1117 case NETLINK_ROUTE:
1118 return SECCLASS_NETLINK_ROUTE_SOCKET;
1119 case NETLINK_FIREWALL:
1120 return SECCLASS_NETLINK_FIREWALL_SOCKET;
James Morris216efaa2005-08-15 20:34:48 -07001121 case NETLINK_INET_DIAG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 return SECCLASS_NETLINK_TCPDIAG_SOCKET;
1123 case NETLINK_NFLOG:
1124 return SECCLASS_NETLINK_NFLOG_SOCKET;
1125 case NETLINK_XFRM:
1126 return SECCLASS_NETLINK_XFRM_SOCKET;
1127 case NETLINK_SELINUX:
1128 return SECCLASS_NETLINK_SELINUX_SOCKET;
1129 case NETLINK_AUDIT:
1130 return SECCLASS_NETLINK_AUDIT_SOCKET;
1131 case NETLINK_IP6_FW:
1132 return SECCLASS_NETLINK_IP6FW_SOCKET;
1133 case NETLINK_DNRTMSG:
1134 return SECCLASS_NETLINK_DNRT_SOCKET;
James Morris0c9b7942005-04-16 15:24:13 -07001135 case NETLINK_KOBJECT_UEVENT:
1136 return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 default:
1138 return SECCLASS_NETLINK_SOCKET;
1139 }
1140 case PF_PACKET:
1141 return SECCLASS_PACKET_SOCKET;
1142 case PF_KEY:
1143 return SECCLASS_KEY_SOCKET;
Christopher J. PeBenito3e3ff152006-06-09 00:25:03 -07001144 case PF_APPLETALK:
1145 return SECCLASS_APPLETALK_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 }
1147
1148 return SECCLASS_SOCKET;
1149}
1150
1151#ifdef CONFIG_PROC_FS
1152static int selinux_proc_get_sid(struct proc_dir_entry *de,
1153 u16 tclass,
1154 u32 *sid)
1155{
1156 int buflen, rc;
1157 char *buffer, *path, *end;
1158
Eric Paris828dfe12008-04-17 13:17:49 -04001159 buffer = (char *)__get_free_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 if (!buffer)
1161 return -ENOMEM;
1162
1163 buflen = PAGE_SIZE;
1164 end = buffer+buflen;
1165 *--end = '\0';
1166 buflen--;
1167 path = end-1;
1168 *path = '/';
1169 while (de && de != de->parent) {
1170 buflen -= de->namelen + 1;
1171 if (buflen < 0)
1172 break;
1173 end -= de->namelen;
1174 memcpy(end, de->name, de->namelen);
1175 *--end = '/';
1176 path = end;
1177 de = de->parent;
1178 }
1179 rc = security_genfs_sid("proc", path, tclass, sid);
1180 free_page((unsigned long)buffer);
1181 return rc;
1182}
1183#else
1184static int selinux_proc_get_sid(struct proc_dir_entry *de,
1185 u16 tclass,
1186 u32 *sid)
1187{
1188 return -EINVAL;
1189}
1190#endif
1191
1192/* The inode's security attributes must be initialized before first use. */
1193static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
1194{
1195 struct superblock_security_struct *sbsec = NULL;
1196 struct inode_security_struct *isec = inode->i_security;
1197 u32 sid;
1198 struct dentry *dentry;
1199#define INITCONTEXTLEN 255
1200 char *context = NULL;
1201 unsigned len = 0;
1202 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
1204 if (isec->initialized)
1205 goto out;
1206
Eric Paris23970742006-09-25 23:32:01 -07001207 mutex_lock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 if (isec->initialized)
Eric Paris23970742006-09-25 23:32:01 -07001209 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
1211 sbsec = inode->i_sb->s_security;
1212 if (!sbsec->initialized) {
1213 /* Defer initialization until selinux_complete_init,
1214 after the initial policy is loaded and the security
1215 server is ready to handle calls. */
1216 spin_lock(&sbsec->isec_lock);
1217 if (list_empty(&isec->list))
1218 list_add(&isec->list, &sbsec->isec_head);
1219 spin_unlock(&sbsec->isec_lock);
Eric Paris23970742006-09-25 23:32:01 -07001220 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 }
1222
1223 switch (sbsec->behavior) {
1224 case SECURITY_FS_USE_XATTR:
1225 if (!inode->i_op->getxattr) {
1226 isec->sid = sbsec->def_sid;
1227 break;
1228 }
1229
1230 /* Need a dentry, since the xattr API requires one.
1231 Life would be simpler if we could just pass the inode. */
1232 if (opt_dentry) {
1233 /* Called from d_instantiate or d_splice_alias. */
1234 dentry = dget(opt_dentry);
1235 } else {
1236 /* Called from selinux_complete_init, try to find a dentry. */
1237 dentry = d_find_alias(inode);
1238 }
1239 if (!dentry) {
Eric Paris744ba352008-04-17 11:52:44 -04001240 printk(KERN_WARNING "SELinux: %s: no dentry for dev=%s "
Harvey Harrisondd6f9532008-03-06 10:03:59 +11001241 "ino=%ld\n", __func__, inode->i_sb->s_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 inode->i_ino);
Eric Paris23970742006-09-25 23:32:01 -07001243 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 }
1245
1246 len = INITCONTEXTLEN;
Stephen Smalley869ab512008-04-04 08:46:05 -04001247 context = kmalloc(len, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 if (!context) {
1249 rc = -ENOMEM;
1250 dput(dentry);
Eric Paris23970742006-09-25 23:32:01 -07001251 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 }
1253 rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX,
1254 context, len);
1255 if (rc == -ERANGE) {
1256 /* Need a larger buffer. Query for the right size. */
1257 rc = inode->i_op->getxattr(dentry, XATTR_NAME_SELINUX,
1258 NULL, 0);
1259 if (rc < 0) {
1260 dput(dentry);
Eric Paris23970742006-09-25 23:32:01 -07001261 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 }
1263 kfree(context);
1264 len = rc;
Stephen Smalley869ab512008-04-04 08:46:05 -04001265 context = kmalloc(len, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 if (!context) {
1267 rc = -ENOMEM;
1268 dput(dentry);
Eric Paris23970742006-09-25 23:32:01 -07001269 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 }
1271 rc = inode->i_op->getxattr(dentry,
1272 XATTR_NAME_SELINUX,
1273 context, len);
1274 }
1275 dput(dentry);
1276 if (rc < 0) {
1277 if (rc != -ENODATA) {
Eric Paris744ba352008-04-17 11:52:44 -04001278 printk(KERN_WARNING "SELinux: %s: getxattr returned "
Harvey Harrisondd6f9532008-03-06 10:03:59 +11001279 "%d for dev=%s ino=%ld\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 -rc, inode->i_sb->s_id, inode->i_ino);
1281 kfree(context);
Eric Paris23970742006-09-25 23:32:01 -07001282 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 }
1284 /* Map ENODATA to the default file SID */
1285 sid = sbsec->def_sid;
1286 rc = 0;
1287 } else {
James Morrisf5c1d5b2005-07-28 01:07:37 -07001288 rc = security_context_to_sid_default(context, rc, &sid,
Stephen Smalley869ab512008-04-04 08:46:05 -04001289 sbsec->def_sid,
1290 GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 if (rc) {
Eric Paris744ba352008-04-17 11:52:44 -04001292 printk(KERN_WARNING "SELinux: %s: context_to_sid(%s) "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 "returned %d for dev=%s ino=%ld\n",
Harvey Harrisondd6f9532008-03-06 10:03:59 +11001294 __func__, context, -rc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 inode->i_sb->s_id, inode->i_ino);
1296 kfree(context);
1297 /* Leave with the unlabeled SID */
1298 rc = 0;
1299 break;
1300 }
1301 }
1302 kfree(context);
1303 isec->sid = sid;
1304 break;
1305 case SECURITY_FS_USE_TASK:
1306 isec->sid = isec->task_sid;
1307 break;
1308 case SECURITY_FS_USE_TRANS:
1309 /* Default to the fs SID. */
1310 isec->sid = sbsec->sid;
1311
1312 /* Try to obtain a transition SID. */
1313 isec->sclass = inode_mode_to_security_class(inode->i_mode);
1314 rc = security_transition_sid(isec->task_sid,
1315 sbsec->sid,
1316 isec->sclass,
1317 &sid);
1318 if (rc)
Eric Paris23970742006-09-25 23:32:01 -07001319 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 isec->sid = sid;
1321 break;
Eric Parisc312feb2006-07-10 04:43:53 -07001322 case SECURITY_FS_USE_MNTPOINT:
1323 isec->sid = sbsec->mntpoint_sid;
1324 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 default:
Eric Parisc312feb2006-07-10 04:43:53 -07001326 /* Default to the fs superblock SID. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 isec->sid = sbsec->sid;
1328
Stephen Smalleyea6b1842008-09-22 15:41:19 -04001329 if (sbsec->proc && !S_ISLNK(inode->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 struct proc_inode *proci = PROC_I(inode);
1331 if (proci->pde) {
1332 isec->sclass = inode_mode_to_security_class(inode->i_mode);
1333 rc = selinux_proc_get_sid(proci->pde,
1334 isec->sclass,
1335 &sid);
1336 if (rc)
Eric Paris23970742006-09-25 23:32:01 -07001337 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 isec->sid = sid;
1339 }
1340 }
1341 break;
1342 }
1343
1344 isec->initialized = 1;
1345
Eric Paris23970742006-09-25 23:32:01 -07001346out_unlock:
1347 mutex_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348out:
1349 if (isec->sclass == SECCLASS_FILE)
1350 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 return rc;
1352}
1353
1354/* Convert a Linux signal to an access vector. */
1355static inline u32 signal_to_av(int sig)
1356{
1357 u32 perm = 0;
1358
1359 switch (sig) {
1360 case SIGCHLD:
1361 /* Commonly granted from child to parent. */
1362 perm = PROCESS__SIGCHLD;
1363 break;
1364 case SIGKILL:
1365 /* Cannot be caught or ignored */
1366 perm = PROCESS__SIGKILL;
1367 break;
1368 case SIGSTOP:
1369 /* Cannot be caught or ignored */
1370 perm = PROCESS__SIGSTOP;
1371 break;
1372 default:
1373 /* All other signals. */
1374 perm = PROCESS__SIGNAL;
1375 break;
1376 }
1377
1378 return perm;
1379}
1380
David Howells275bb412008-11-14 10:39:19 +11001381/*
David Howellsd84f4f92008-11-14 10:39:23 +11001382 * Check permission between a pair of credentials
1383 * fork check, ptrace check, etc.
1384 */
1385static int cred_has_perm(const struct cred *actor,
1386 const struct cred *target,
1387 u32 perms)
1388{
1389 u32 asid = cred_sid(actor), tsid = cred_sid(target);
1390
1391 return avc_has_perm(asid, tsid, SECCLASS_PROCESS, perms, NULL);
1392}
1393
1394/*
David Howells88e67f32008-11-14 10:39:21 +11001395 * Check permission between a pair of tasks, e.g. signal checks,
David Howells275bb412008-11-14 10:39:19 +11001396 * fork check, ptrace check, etc.
1397 * tsk1 is the actor and tsk2 is the target
1398 */
1399static int task_has_perm(const struct task_struct *tsk1,
1400 const struct task_struct *tsk2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 u32 perms)
1402{
David Howells275bb412008-11-14 10:39:19 +11001403 const struct task_security_struct *__tsec1, *__tsec2;
1404 u32 sid1, sid2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
David Howells275bb412008-11-14 10:39:19 +11001406 rcu_read_lock();
1407 __tsec1 = __task_cred(tsk1)->security; sid1 = __tsec1->sid;
1408 __tsec2 = __task_cred(tsk2)->security; sid2 = __tsec2->sid;
1409 rcu_read_unlock();
1410 return avc_has_perm(sid1, sid2, SECCLASS_PROCESS, perms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411}
1412
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001413#if CAP_LAST_CAP > 63
1414#error Fix SELinux to handle capabilities > 63.
1415#endif
1416
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417/* Check whether a task is allowed to use a capability. */
1418static int task_has_capability(struct task_struct *tsk,
Eric Paris06112162008-11-11 22:02:50 +11001419 int cap, int audit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 struct avc_audit_data ad;
Eric Paris06112162008-11-11 22:02:50 +11001422 struct av_decision avd;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001423 u16 sclass;
David Howells275bb412008-11-14 10:39:19 +11001424 u32 sid = task_sid(tsk);
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001425 u32 av = CAP_TO_MASK(cap);
Eric Paris06112162008-11-11 22:02:50 +11001426 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Eric Paris828dfe12008-04-17 13:17:49 -04001428 AVC_AUDIT_DATA_INIT(&ad, CAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 ad.tsk = tsk;
1430 ad.u.cap = cap;
1431
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001432 switch (CAP_TO_INDEX(cap)) {
1433 case 0:
1434 sclass = SECCLASS_CAPABILITY;
1435 break;
1436 case 1:
1437 sclass = SECCLASS_CAPABILITY2;
1438 break;
1439 default:
1440 printk(KERN_ERR
1441 "SELinux: out of range capability %d\n", cap);
1442 BUG();
1443 }
Eric Paris06112162008-11-11 22:02:50 +11001444
David Howells275bb412008-11-14 10:39:19 +11001445 rc = avc_has_perm_noaudit(sid, sid, sclass, av, 0, &avd);
Eric Paris06112162008-11-11 22:02:50 +11001446 if (audit == SECURITY_CAP_AUDIT)
David Howells275bb412008-11-14 10:39:19 +11001447 avc_audit(sid, sid, sclass, av, &avd, rc, &ad);
Eric Paris06112162008-11-11 22:02:50 +11001448 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449}
1450
1451/* Check whether a task is allowed to use a system operation. */
1452static int task_has_system(struct task_struct *tsk,
1453 u32 perms)
1454{
David Howells275bb412008-11-14 10:39:19 +11001455 u32 sid = task_sid(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
David Howells275bb412008-11-14 10:39:19 +11001457 return avc_has_perm(sid, SECINITSID_KERNEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 SECCLASS_SYSTEM, perms, NULL);
1459}
1460
1461/* Check whether a task has a particular permission to an inode.
1462 The 'adp' parameter is optional and allows other audit
1463 data to be passed (e.g. the dentry). */
David Howells88e67f32008-11-14 10:39:21 +11001464static int inode_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 struct inode *inode,
1466 u32 perms,
1467 struct avc_audit_data *adp)
1468{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 struct inode_security_struct *isec;
1470 struct avc_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11001471 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
Eric Paris828dfe12008-04-17 13:17:49 -04001473 if (unlikely(IS_PRIVATE(inode)))
Stephen Smalleybbaca6c2007-02-14 00:34:16 -08001474 return 0;
1475
David Howells88e67f32008-11-14 10:39:21 +11001476 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 isec = inode->i_security;
1478
1479 if (!adp) {
1480 adp = &ad;
1481 AVC_AUDIT_DATA_INIT(&ad, FS);
1482 ad.u.fs.inode = inode;
1483 }
1484
David Howells275bb412008-11-14 10:39:19 +11001485 return avc_has_perm(sid, isec->sid, isec->sclass, perms, adp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486}
1487
1488/* Same as inode_has_perm, but pass explicit audit data containing
1489 the dentry to help the auditing code to more easily generate the
1490 pathname if needed. */
David Howells88e67f32008-11-14 10:39:21 +11001491static inline int dentry_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 struct vfsmount *mnt,
1493 struct dentry *dentry,
1494 u32 av)
1495{
1496 struct inode *inode = dentry->d_inode;
1497 struct avc_audit_data ad;
David Howells88e67f32008-11-14 10:39:21 +11001498
Eric Paris828dfe12008-04-17 13:17:49 -04001499 AVC_AUDIT_DATA_INIT(&ad, FS);
Jan Blunck44707fd2008-02-14 19:38:33 -08001500 ad.u.fs.path.mnt = mnt;
1501 ad.u.fs.path.dentry = dentry;
David Howells88e67f32008-11-14 10:39:21 +11001502 return inode_has_perm(cred, inode, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503}
1504
1505/* Check whether a task can use an open file descriptor to
1506 access an inode in a given way. Check access to the
1507 descriptor itself, and then use dentry_has_perm to
1508 check a particular permission to the file.
1509 Access to the descriptor is implicitly granted if it
1510 has the same SID as the process. If av is zero, then
1511 access to the file is not checked, e.g. for cases
1512 where only the descriptor is affected like seek. */
David Howells88e67f32008-11-14 10:39:21 +11001513static int file_has_perm(const struct cred *cred,
1514 struct file *file,
1515 u32 av)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 struct file_security_struct *fsec = file->f_security;
Jan Blunck44707fd2008-02-14 19:38:33 -08001518 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 struct avc_audit_data ad;
David Howells88e67f32008-11-14 10:39:21 +11001520 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 int rc;
1522
1523 AVC_AUDIT_DATA_INIT(&ad, FS);
Jan Blunck44707fd2008-02-14 19:38:33 -08001524 ad.u.fs.path = file->f_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
David Howells275bb412008-11-14 10:39:19 +11001526 if (sid != fsec->sid) {
1527 rc = avc_has_perm(sid, fsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 SECCLASS_FD,
1529 FD__USE,
1530 &ad);
1531 if (rc)
David Howells88e67f32008-11-14 10:39:21 +11001532 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 }
1534
1535 /* av is zero if only checking access to the descriptor. */
David Howells88e67f32008-11-14 10:39:21 +11001536 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 if (av)
David Howells88e67f32008-11-14 10:39:21 +11001538 rc = inode_has_perm(cred, inode, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
David Howells88e67f32008-11-14 10:39:21 +11001540out:
1541 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542}
1543
1544/* Check whether a task can create a file. */
1545static int may_create(struct inode *dir,
1546 struct dentry *dentry,
1547 u16 tclass)
1548{
David Howells275bb412008-11-14 10:39:19 +11001549 const struct cred *cred = current_cred();
1550 const struct task_security_struct *tsec = cred->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 struct inode_security_struct *dsec;
1552 struct superblock_security_struct *sbsec;
David Howells275bb412008-11-14 10:39:19 +11001553 u32 sid, newsid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 struct avc_audit_data ad;
1555 int rc;
1556
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 dsec = dir->i_security;
1558 sbsec = dir->i_sb->s_security;
1559
David Howells275bb412008-11-14 10:39:19 +11001560 sid = tsec->sid;
1561 newsid = tsec->create_sid;
1562
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 AVC_AUDIT_DATA_INIT(&ad, FS);
Jan Blunck44707fd2008-02-14 19:38:33 -08001564 ad.u.fs.path.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
David Howells275bb412008-11-14 10:39:19 +11001566 rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 DIR__ADD_NAME | DIR__SEARCH,
1568 &ad);
1569 if (rc)
1570 return rc;
1571
David Howells275bb412008-11-14 10:39:19 +11001572 if (!newsid || sbsec->behavior == SECURITY_FS_USE_MNTPOINT) {
1573 rc = security_transition_sid(sid, dsec->sid, tclass, &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 if (rc)
1575 return rc;
1576 }
1577
David Howells275bb412008-11-14 10:39:19 +11001578 rc = avc_has_perm(sid, newsid, tclass, FILE__CREATE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 if (rc)
1580 return rc;
1581
1582 return avc_has_perm(newsid, sbsec->sid,
1583 SECCLASS_FILESYSTEM,
1584 FILESYSTEM__ASSOCIATE, &ad);
1585}
1586
Michael LeMay4eb582c2006-06-26 00:24:57 -07001587/* Check whether a task can create a key. */
1588static int may_create_key(u32 ksid,
1589 struct task_struct *ctx)
1590{
David Howells275bb412008-11-14 10:39:19 +11001591 u32 sid = task_sid(ctx);
Michael LeMay4eb582c2006-06-26 00:24:57 -07001592
David Howells275bb412008-11-14 10:39:19 +11001593 return avc_has_perm(sid, ksid, SECCLASS_KEY, KEY__CREATE, NULL);
Michael LeMay4eb582c2006-06-26 00:24:57 -07001594}
1595
Eric Paris828dfe12008-04-17 13:17:49 -04001596#define MAY_LINK 0
1597#define MAY_UNLINK 1
1598#define MAY_RMDIR 2
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
1600/* Check whether a task can link, unlink, or rmdir a file/directory. */
1601static int may_link(struct inode *dir,
1602 struct dentry *dentry,
1603 int kind)
1604
1605{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 struct inode_security_struct *dsec, *isec;
1607 struct avc_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11001608 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 u32 av;
1610 int rc;
1611
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 dsec = dir->i_security;
1613 isec = dentry->d_inode->i_security;
1614
1615 AVC_AUDIT_DATA_INIT(&ad, FS);
Jan Blunck44707fd2008-02-14 19:38:33 -08001616 ad.u.fs.path.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
1618 av = DIR__SEARCH;
1619 av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
David Howells275bb412008-11-14 10:39:19 +11001620 rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 if (rc)
1622 return rc;
1623
1624 switch (kind) {
1625 case MAY_LINK:
1626 av = FILE__LINK;
1627 break;
1628 case MAY_UNLINK:
1629 av = FILE__UNLINK;
1630 break;
1631 case MAY_RMDIR:
1632 av = DIR__RMDIR;
1633 break;
1634 default:
Eric Paris744ba352008-04-17 11:52:44 -04001635 printk(KERN_WARNING "SELinux: %s: unrecognized kind %d\n",
1636 __func__, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 return 0;
1638 }
1639
David Howells275bb412008-11-14 10:39:19 +11001640 rc = avc_has_perm(sid, isec->sid, isec->sclass, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 return rc;
1642}
1643
1644static inline int may_rename(struct inode *old_dir,
1645 struct dentry *old_dentry,
1646 struct inode *new_dir,
1647 struct dentry *new_dentry)
1648{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
1650 struct avc_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11001651 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 u32 av;
1653 int old_is_dir, new_is_dir;
1654 int rc;
1655
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 old_dsec = old_dir->i_security;
1657 old_isec = old_dentry->d_inode->i_security;
1658 old_is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
1659 new_dsec = new_dir->i_security;
1660
1661 AVC_AUDIT_DATA_INIT(&ad, FS);
1662
Jan Blunck44707fd2008-02-14 19:38:33 -08001663 ad.u.fs.path.dentry = old_dentry;
David Howells275bb412008-11-14 10:39:19 +11001664 rc = avc_has_perm(sid, old_dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 DIR__REMOVE_NAME | DIR__SEARCH, &ad);
1666 if (rc)
1667 return rc;
David Howells275bb412008-11-14 10:39:19 +11001668 rc = avc_has_perm(sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 old_isec->sclass, FILE__RENAME, &ad);
1670 if (rc)
1671 return rc;
1672 if (old_is_dir && new_dir != old_dir) {
David Howells275bb412008-11-14 10:39:19 +11001673 rc = avc_has_perm(sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 old_isec->sclass, DIR__REPARENT, &ad);
1675 if (rc)
1676 return rc;
1677 }
1678
Jan Blunck44707fd2008-02-14 19:38:33 -08001679 ad.u.fs.path.dentry = new_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 av = DIR__ADD_NAME | DIR__SEARCH;
1681 if (new_dentry->d_inode)
1682 av |= DIR__REMOVE_NAME;
David Howells275bb412008-11-14 10:39:19 +11001683 rc = avc_has_perm(sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 if (rc)
1685 return rc;
1686 if (new_dentry->d_inode) {
1687 new_isec = new_dentry->d_inode->i_security;
1688 new_is_dir = S_ISDIR(new_dentry->d_inode->i_mode);
David Howells275bb412008-11-14 10:39:19 +11001689 rc = avc_has_perm(sid, new_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 new_isec->sclass,
1691 (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
1692 if (rc)
1693 return rc;
1694 }
1695
1696 return 0;
1697}
1698
1699/* Check whether a task can perform a filesystem operation. */
David Howells88e67f32008-11-14 10:39:21 +11001700static int superblock_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 struct super_block *sb,
1702 u32 perms,
1703 struct avc_audit_data *ad)
1704{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 struct superblock_security_struct *sbsec;
David Howells88e67f32008-11-14 10:39:21 +11001706 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 sbsec = sb->s_security;
David Howells275bb412008-11-14 10:39:19 +11001709 return avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710}
1711
1712/* Convert a Linux mode and permission mask to an access vector. */
1713static inline u32 file_mask_to_av(int mode, int mask)
1714{
1715 u32 av = 0;
1716
1717 if ((mode & S_IFMT) != S_IFDIR) {
1718 if (mask & MAY_EXEC)
1719 av |= FILE__EXECUTE;
1720 if (mask & MAY_READ)
1721 av |= FILE__READ;
1722
1723 if (mask & MAY_APPEND)
1724 av |= FILE__APPEND;
1725 else if (mask & MAY_WRITE)
1726 av |= FILE__WRITE;
1727
1728 } else {
1729 if (mask & MAY_EXEC)
1730 av |= DIR__SEARCH;
1731 if (mask & MAY_WRITE)
1732 av |= DIR__WRITE;
1733 if (mask & MAY_READ)
1734 av |= DIR__READ;
1735 }
1736
1737 return av;
1738}
1739
1740/* Convert a Linux file to an access vector. */
1741static inline u32 file_to_av(struct file *file)
1742{
1743 u32 av = 0;
1744
1745 if (file->f_mode & FMODE_READ)
1746 av |= FILE__READ;
1747 if (file->f_mode & FMODE_WRITE) {
1748 if (file->f_flags & O_APPEND)
1749 av |= FILE__APPEND;
1750 else
1751 av |= FILE__WRITE;
1752 }
Stephen Smalley0794c662008-03-17 08:55:18 -04001753 if (!av) {
1754 /*
1755 * Special file opened with flags 3 for ioctl-only use.
1756 */
1757 av = FILE__IOCTL;
1758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
1760 return av;
1761}
1762
Eric Paris8b6a5a32008-10-29 17:06:46 -04001763/*
1764 * Convert a file to an access vector and include the correct open
1765 * open permission.
1766 */
1767static inline u32 open_file_to_av(struct file *file)
1768{
1769 u32 av = file_to_av(file);
1770
1771 if (selinux_policycap_openperm) {
1772 mode_t mode = file->f_path.dentry->d_inode->i_mode;
1773 /*
1774 * lnk files and socks do not really have an 'open'
1775 */
1776 if (S_ISREG(mode))
1777 av |= FILE__OPEN;
1778 else if (S_ISCHR(mode))
1779 av |= CHR_FILE__OPEN;
1780 else if (S_ISBLK(mode))
1781 av |= BLK_FILE__OPEN;
1782 else if (S_ISFIFO(mode))
1783 av |= FIFO_FILE__OPEN;
1784 else if (S_ISDIR(mode))
1785 av |= DIR__OPEN;
1786 else
1787 printk(KERN_ERR "SELinux: WARNING: inside %s with "
1788 "unknown mode:%o\n", __func__, mode);
1789 }
1790 return av;
1791}
1792
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793/* Hook functions begin here. */
1794
David Howells5cd9c582008-08-14 11:37:28 +01001795static int selinux_ptrace_may_access(struct task_struct *child,
1796 unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 int rc;
1799
David Howells5cd9c582008-08-14 11:37:28 +01001800 rc = secondary_ops->ptrace_may_access(child, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 if (rc)
1802 return rc;
1803
Stephen Smalley006ebb42008-05-19 08:32:49 -04001804 if (mode == PTRACE_MODE_READ) {
David Howells275bb412008-11-14 10:39:19 +11001805 u32 sid = current_sid();
1806 u32 csid = task_sid(child);
1807 return avc_has_perm(sid, csid, SECCLASS_FILE, FILE__READ, NULL);
Stephen Smalley006ebb42008-05-19 08:32:49 -04001808 }
1809
David Howells5cd9c582008-08-14 11:37:28 +01001810 return task_has_perm(current, child, PROCESS__PTRACE);
1811}
1812
1813static int selinux_ptrace_traceme(struct task_struct *parent)
1814{
1815 int rc;
1816
1817 rc = secondary_ops->ptrace_traceme(parent);
1818 if (rc)
1819 return rc;
1820
1821 return task_has_perm(parent, current, PROCESS__PTRACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822}
1823
1824static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
Eric Paris828dfe12008-04-17 13:17:49 -04001825 kernel_cap_t *inheritable, kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826{
1827 int error;
1828
1829 error = task_has_perm(current, target, PROCESS__GETCAP);
1830 if (error)
1831 return error;
1832
1833 return secondary_ops->capget(target, effective, inheritable, permitted);
1834}
1835
David Howellsd84f4f92008-11-14 10:39:23 +11001836static int selinux_capset(struct cred *new, const struct cred *old,
1837 const kernel_cap_t *effective,
1838 const kernel_cap_t *inheritable,
1839 const kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840{
1841 int error;
1842
David Howellsd84f4f92008-11-14 10:39:23 +11001843 error = secondary_ops->capset(new, old,
1844 effective, inheritable, permitted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 if (error)
1846 return error;
1847
David Howellsd84f4f92008-11-14 10:39:23 +11001848 return cred_has_perm(old, new, PROCESS__SETCAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849}
1850
Eric Paris06112162008-11-11 22:02:50 +11001851static int selinux_capable(struct task_struct *tsk, int cap, int audit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852{
1853 int rc;
1854
Eric Paris06112162008-11-11 22:02:50 +11001855 rc = secondary_ops->capable(tsk, cap, audit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 if (rc)
1857 return rc;
1858
Eric Paris06112162008-11-11 22:02:50 +11001859 return task_has_capability(tsk, cap, audit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860}
1861
Eric W. Biederman3fbfa982007-02-14 00:34:14 -08001862static int selinux_sysctl_get_sid(ctl_table *table, u16 tclass, u32 *sid)
1863{
1864 int buflen, rc;
1865 char *buffer, *path, *end;
1866
1867 rc = -ENOMEM;
Eric Paris828dfe12008-04-17 13:17:49 -04001868 buffer = (char *)__get_free_page(GFP_KERNEL);
Eric W. Biederman3fbfa982007-02-14 00:34:14 -08001869 if (!buffer)
1870 goto out;
1871
1872 buflen = PAGE_SIZE;
1873 end = buffer+buflen;
1874 *--end = '\0';
1875 buflen--;
1876 path = end-1;
1877 *path = '/';
1878 while (table) {
1879 const char *name = table->procname;
1880 size_t namelen = strlen(name);
1881 buflen -= namelen + 1;
1882 if (buflen < 0)
1883 goto out_free;
1884 end -= namelen;
1885 memcpy(end, name, namelen);
1886 *--end = '/';
1887 path = end;
1888 table = table->parent;
1889 }
Eric W. Biedermanb599fdf2007-02-14 00:34:15 -08001890 buflen -= 4;
1891 if (buflen < 0)
1892 goto out_free;
1893 end -= 4;
1894 memcpy(end, "/sys", 4);
1895 path = end;
Eric W. Biederman3fbfa982007-02-14 00:34:14 -08001896 rc = security_genfs_sid("proc", path, tclass, sid);
1897out_free:
1898 free_page((unsigned long)buffer);
1899out:
1900 return rc;
1901}
1902
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903static int selinux_sysctl(ctl_table *table, int op)
1904{
1905 int error = 0;
1906 u32 av;
David Howells275bb412008-11-14 10:39:19 +11001907 u32 tsid, sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 int rc;
1909
1910 rc = secondary_ops->sysctl(table, op);
1911 if (rc)
1912 return rc;
1913
David Howells275bb412008-11-14 10:39:19 +11001914 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
Eric W. Biederman3fbfa982007-02-14 00:34:14 -08001916 rc = selinux_sysctl_get_sid(table, (op == 0001) ?
1917 SECCLASS_DIR : SECCLASS_FILE, &tsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 if (rc) {
1919 /* Default to the well-defined sysctl SID. */
1920 tsid = SECINITSID_SYSCTL;
1921 }
1922
1923 /* The op values are "defined" in sysctl.c, thereby creating
1924 * a bad coupling between this module and sysctl.c */
Eric Paris828dfe12008-04-17 13:17:49 -04001925 if (op == 001) {
David Howells275bb412008-11-14 10:39:19 +11001926 error = avc_has_perm(sid, tsid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 SECCLASS_DIR, DIR__SEARCH, NULL);
1928 } else {
1929 av = 0;
1930 if (op & 004)
1931 av |= FILE__READ;
1932 if (op & 002)
1933 av |= FILE__WRITE;
1934 if (av)
David Howells275bb412008-11-14 10:39:19 +11001935 error = avc_has_perm(sid, tsid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 SECCLASS_FILE, av, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04001937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
1939 return error;
1940}
1941
1942static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
1943{
David Howells88e67f32008-11-14 10:39:21 +11001944 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 int rc = 0;
1946
1947 if (!sb)
1948 return 0;
1949
1950 switch (cmds) {
Eric Paris828dfe12008-04-17 13:17:49 -04001951 case Q_SYNC:
1952 case Q_QUOTAON:
1953 case Q_QUOTAOFF:
1954 case Q_SETINFO:
1955 case Q_SETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11001956 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04001957 break;
1958 case Q_GETFMT:
1959 case Q_GETINFO:
1960 case Q_GETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11001961 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04001962 break;
1963 default:
1964 rc = 0; /* let the kernel handle invalid cmds */
1965 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 }
1967 return rc;
1968}
1969
1970static int selinux_quota_on(struct dentry *dentry)
1971{
David Howells88e67f32008-11-14 10:39:21 +11001972 const struct cred *cred = current_cred();
1973
1974 return dentry_has_perm(cred, NULL, dentry, FILE__QUOTAON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975}
1976
1977static int selinux_syslog(int type)
1978{
1979 int rc;
1980
1981 rc = secondary_ops->syslog(type);
1982 if (rc)
1983 return rc;
1984
1985 switch (type) {
Eric Paris828dfe12008-04-17 13:17:49 -04001986 case 3: /* Read last kernel messages */
1987 case 10: /* Return size of the log buffer */
1988 rc = task_has_system(current, SYSTEM__SYSLOG_READ);
1989 break;
1990 case 6: /* Disable logging to console */
1991 case 7: /* Enable logging to console */
1992 case 8: /* Set level of messages printed to console */
1993 rc = task_has_system(current, SYSTEM__SYSLOG_CONSOLE);
1994 break;
1995 case 0: /* Close log */
1996 case 1: /* Open log */
1997 case 2: /* Read from log */
1998 case 4: /* Read/clear last kernel messages */
1999 case 5: /* Clear ring buffer */
2000 default:
2001 rc = task_has_system(current, SYSTEM__SYSLOG_MOD);
2002 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 }
2004 return rc;
2005}
2006
2007/*
2008 * Check that a process has enough memory to allocate a new virtual
2009 * mapping. 0 means there is enough memory for the allocation to
2010 * succeed and -ENOMEM implies there is not.
2011 *
2012 * Note that secondary_ops->capable and task_has_perm_noaudit return 0
2013 * if the capability is granted, but __vm_enough_memory requires 1 if
2014 * the capability is granted.
2015 *
2016 * Do not audit the selinux permission check, as this is applied to all
2017 * processes that allocate mappings.
2018 */
Alan Cox34b4e4a2007-08-22 14:01:28 -07002019static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020{
2021 int rc, cap_sys_admin = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
Eric Paris06674672008-11-11 22:02:57 +11002023 rc = selinux_capable(current, CAP_SYS_ADMIN, SECURITY_CAP_NOAUDIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 if (rc == 0)
2025 cap_sys_admin = 1;
2026
Alan Cox34b4e4a2007-08-22 14:01:28 -07002027 return __vm_enough_memory(mm, pages, cap_sys_admin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028}
2029
2030/* binprm security operations */
2031
David Howellsa6f76f22008-11-14 10:39:24 +11002032static int selinux_bprm_set_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033{
David Howellsa6f76f22008-11-14 10:39:24 +11002034 const struct task_security_struct *old_tsec;
2035 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 struct avc_audit_data ad;
David Howellsa6f76f22008-11-14 10:39:24 +11002038 struct inode *inode = bprm->file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 int rc;
2040
David Howellsa6f76f22008-11-14 10:39:24 +11002041 rc = secondary_ops->bprm_set_creds(bprm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 if (rc)
2043 return rc;
2044
David Howellsa6f76f22008-11-14 10:39:24 +11002045 /* SELinux context only depends on initial program or script and not
2046 * the script interpreter */
2047 if (bprm->cred_prepared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 return 0;
2049
David Howellsa6f76f22008-11-14 10:39:24 +11002050 old_tsec = current_security();
2051 new_tsec = bprm->cred->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 isec = inode->i_security;
2053
2054 /* Default to the current task SID. */
David Howellsa6f76f22008-11-14 10:39:24 +11002055 new_tsec->sid = old_tsec->sid;
2056 new_tsec->osid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
Michael LeMay28eba5b2006-06-27 02:53:42 -07002058 /* Reset fs, key, and sock SIDs on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002059 new_tsec->create_sid = 0;
2060 new_tsec->keycreate_sid = 0;
2061 new_tsec->sockcreate_sid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
David Howellsa6f76f22008-11-14 10:39:24 +11002063 if (old_tsec->exec_sid) {
2064 new_tsec->sid = old_tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 /* Reset exec SID on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002066 new_tsec->exec_sid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 } else {
2068 /* Check for a default transition on this program. */
David Howellsa6f76f22008-11-14 10:39:24 +11002069 rc = security_transition_sid(old_tsec->sid, isec->sid,
2070 SECCLASS_PROCESS, &new_tsec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 if (rc)
2072 return rc;
2073 }
2074
2075 AVC_AUDIT_DATA_INIT(&ad, FS);
Jan Blunck44707fd2008-02-14 19:38:33 -08002076 ad.u.fs.path = bprm->file->f_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
Josef Sipek3d5ff522006-12-08 02:37:38 -08002078 if (bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID)
David Howellsa6f76f22008-11-14 10:39:24 +11002079 new_tsec->sid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
David Howellsa6f76f22008-11-14 10:39:24 +11002081 if (new_tsec->sid == old_tsec->sid) {
2082 rc = avc_has_perm(old_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
2084 if (rc)
2085 return rc;
2086 } else {
2087 /* Check permissions for the transition. */
David Howellsa6f76f22008-11-14 10:39:24 +11002088 rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
2090 if (rc)
2091 return rc;
2092
David Howellsa6f76f22008-11-14 10:39:24 +11002093 rc = avc_has_perm(new_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
2095 if (rc)
2096 return rc;
2097
David Howellsa6f76f22008-11-14 10:39:24 +11002098 /* Check for shared state */
2099 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
2100 rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
2101 SECCLASS_PROCESS, PROCESS__SHARE,
2102 NULL);
2103 if (rc)
2104 return -EPERM;
2105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
David Howellsa6f76f22008-11-14 10:39:24 +11002107 /* Make sure that anyone attempting to ptrace over a task that
2108 * changes its SID has the appropriate permit */
2109 if (bprm->unsafe &
2110 (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
2111 struct task_struct *tracer;
2112 struct task_security_struct *sec;
2113 u32 ptsid = 0;
2114
2115 rcu_read_lock();
2116 tracer = tracehook_tracer_task(current);
2117 if (likely(tracer != NULL)) {
2118 sec = __task_cred(tracer)->security;
2119 ptsid = sec->sid;
2120 }
2121 rcu_read_unlock();
2122
2123 if (ptsid != 0) {
2124 rc = avc_has_perm(ptsid, new_tsec->sid,
2125 SECCLASS_PROCESS,
2126 PROCESS__PTRACE, NULL);
2127 if (rc)
2128 return -EPERM;
2129 }
2130 }
2131
2132 /* Clear any possibly unsafe personality bits on exec: */
2133 bprm->per_clear |= PER_CLEAR_ON_SETID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 }
2135
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 return 0;
2137}
2138
Eric Paris828dfe12008-04-17 13:17:49 -04002139static int selinux_bprm_check_security(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140{
2141 return secondary_ops->bprm_check_security(bprm);
2142}
2143
Eric Paris828dfe12008-04-17 13:17:49 -04002144static int selinux_bprm_secureexec(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145{
David Howells275bb412008-11-14 10:39:19 +11002146 const struct cred *cred = current_cred();
2147 const struct task_security_struct *tsec = cred->security;
2148 u32 sid, osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 int atsecure = 0;
2150
David Howells275bb412008-11-14 10:39:19 +11002151 sid = tsec->sid;
2152 osid = tsec->osid;
2153
2154 if (osid != sid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 /* Enable secure mode for SIDs transitions unless
2156 the noatsecure permission is granted between
2157 the two SIDs, i.e. ahp returns 0. */
David Howells275bb412008-11-14 10:39:19 +11002158 atsecure = avc_has_perm(osid, sid,
David Howellsa6f76f22008-11-14 10:39:24 +11002159 SECCLASS_PROCESS,
2160 PROCESS__NOATSECURE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 }
2162
2163 return (atsecure || secondary_ops->bprm_secureexec(bprm));
2164}
2165
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166extern struct vfsmount *selinuxfs_mount;
2167extern struct dentry *selinux_null;
2168
2169/* Derived from fs/exec.c:flush_old_files. */
David Howells745ca242008-11-14 10:39:22 +11002170static inline void flush_unauthorized_files(const struct cred *cred,
2171 struct files_struct *files)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172{
2173 struct avc_audit_data ad;
2174 struct file *file, *devnull = NULL;
Stephen Smalleyb20c8122006-09-25 23:32:03 -07002175 struct tty_struct *tty;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07002176 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 long j = -1;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002178 int drop_tty = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002180 tty = get_current_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 if (tty) {
2182 file_list_lock();
Eric Paris37dd0bd2008-10-31 17:40:00 -04002183 if (!list_empty(&tty->tty_files)) {
2184 struct inode *inode;
2185
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 /* Revalidate access to controlling tty.
2187 Use inode_has_perm on the tty inode directly rather
2188 than using file_has_perm, as this particular open
2189 file may belong to another process and we are only
2190 interested in the inode-based check here. */
Eric Paris37dd0bd2008-10-31 17:40:00 -04002191 file = list_first_entry(&tty->tty_files, struct file, f_u.fu_list);
2192 inode = file->f_path.dentry->d_inode;
David Howells88e67f32008-11-14 10:39:21 +11002193 if (inode_has_perm(cred, inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 FILE__READ | FILE__WRITE, NULL)) {
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002195 drop_tty = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 }
2197 }
2198 file_list_unlock();
Alan Cox452a00d2008-10-13 10:39:13 +01002199 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 }
Eric W. Biederman98a27ba2007-05-08 00:26:56 -07002201 /* Reset controlling tty. */
2202 if (drop_tty)
2203 no_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
2205 /* Revalidate access to inherited open files. */
2206
Eric Paris828dfe12008-04-17 13:17:49 -04002207 AVC_AUDIT_DATA_INIT(&ad, FS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
2209 spin_lock(&files->file_lock);
2210 for (;;) {
2211 unsigned long set, i;
2212 int fd;
2213
2214 j++;
2215 i = j * __NFDBITS;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07002216 fdt = files_fdtable(files);
Vadim Lobanovbbea9f62006-12-10 02:21:12 -08002217 if (i >= fdt->max_fds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 break;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07002219 set = fdt->open_fds->fds_bits[j];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 if (!set)
2221 continue;
2222 spin_unlock(&files->file_lock);
Eric Paris828dfe12008-04-17 13:17:49 -04002223 for ( ; set ; i++, set >>= 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 if (set & 1) {
2225 file = fget(i);
2226 if (!file)
2227 continue;
David Howells88e67f32008-11-14 10:39:21 +11002228 if (file_has_perm(cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 file,
2230 file_to_av(file))) {
2231 sys_close(i);
2232 fd = get_unused_fd();
2233 if (fd != i) {
2234 if (fd >= 0)
2235 put_unused_fd(fd);
2236 fput(file);
2237 continue;
2238 }
2239 if (devnull) {
Nick Piggin095975d2006-01-08 01:02:19 -08002240 get_file(devnull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 } else {
David Howells745ca242008-11-14 10:39:22 +11002242 devnull = dentry_open(
2243 dget(selinux_null),
2244 mntget(selinuxfs_mount),
2245 O_RDWR, cred);
Akinobu Mitafc5d81e2006-11-27 15:16:48 +09002246 if (IS_ERR(devnull)) {
2247 devnull = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 put_unused_fd(fd);
2249 fput(file);
2250 continue;
2251 }
2252 }
2253 fd_install(fd, devnull);
2254 }
2255 fput(file);
2256 }
2257 }
2258 spin_lock(&files->file_lock);
2259
2260 }
2261 spin_unlock(&files->file_lock);
2262}
2263
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264/*
David Howellsa6f76f22008-11-14 10:39:24 +11002265 * Prepare a process for imminent new credential changes due to exec
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 */
David Howellsa6f76f22008-11-14 10:39:24 +11002267static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268{
David Howellsa6f76f22008-11-14 10:39:24 +11002269 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 struct rlimit *rlim, *initrlim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 int rc, i;
2272
David Howellsa6f76f22008-11-14 10:39:24 +11002273 secondary_ops->bprm_committing_creds(bprm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
David Howellsa6f76f22008-11-14 10:39:24 +11002275 new_tsec = bprm->cred->security;
2276 if (new_tsec->sid == new_tsec->osid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 return;
2278
2279 /* Close files for which the new task SID is not authorized. */
David Howellsa6f76f22008-11-14 10:39:24 +11002280 flush_unauthorized_files(bprm->cred, current->files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281
David Howellsa6f76f22008-11-14 10:39:24 +11002282 /* Always clear parent death signal on SID transitions. */
2283 current->pdeath_signal = 0;
2284
2285 /* Check whether the new SID can inherit resource limits from the old
2286 * SID. If not, reset all soft limits to the lower of the current
2287 * task's hard limit and the init task's soft limit.
2288 *
2289 * Note that the setting of hard limits (even to lower them) can be
2290 * controlled by the setrlimit check. The inclusion of the init task's
2291 * soft limit into the computation is to avoid resetting soft limits
2292 * higher than the default soft limit for cases where the default is
2293 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2294 */
2295 rc = avc_has_perm(new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
2296 PROCESS__RLIMITINH, NULL);
2297 if (rc) {
2298 for (i = 0; i < RLIM_NLIMITS; i++) {
2299 rlim = current->signal->rlim + i;
2300 initrlim = init_task.signal->rlim + i;
2301 rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
2302 }
2303 update_rlimit_cpu(rlim->rlim_cur);
2304 }
2305}
2306
2307/*
2308 * Clean up the process immediately after the installation of new credentials
2309 * due to exec
2310 */
2311static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
2312{
2313 const struct task_security_struct *tsec = current_security();
2314 struct itimerval itimer;
2315 struct sighand_struct *psig;
2316 u32 osid, sid;
2317 int rc, i;
2318 unsigned long flags;
2319
2320 secondary_ops->bprm_committed_creds(bprm);
2321
2322 osid = tsec->osid;
2323 sid = tsec->sid;
2324
2325 if (sid == osid)
2326 return;
2327
2328 /* Check whether the new SID can inherit signal state from the old SID.
2329 * If not, clear itimers to avoid subsequent signal generation and
2330 * flush and unblock signals.
2331 *
2332 * This must occur _after_ the task SID has been updated so that any
2333 * kill done after the flush will be checked against the new SID.
2334 */
2335 rc = avc_has_perm(osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 if (rc) {
2337 memset(&itimer, 0, sizeof itimer);
2338 for (i = 0; i < 3; i++)
2339 do_setitimer(i, &itimer, NULL);
2340 flush_signals(current);
2341 spin_lock_irq(&current->sighand->siglock);
2342 flush_signal_handlers(current, 1);
2343 sigemptyset(&current->blocked);
2344 recalc_sigpending();
2345 spin_unlock_irq(&current->sighand->siglock);
2346 }
2347
David Howellsa6f76f22008-11-14 10:39:24 +11002348 /* Wake up the parent if it is waiting so that it can recheck
2349 * wait permission to the new task SID. */
Eric Paris41d9f9c2008-11-04 15:18:26 -05002350 read_lock_irq(&tasklist_lock);
2351 psig = current->parent->sighand;
2352 spin_lock_irqsave(&psig->siglock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 wake_up_interruptible(&current->parent->signal->wait_chldexit);
Eric Paris41d9f9c2008-11-04 15:18:26 -05002354 spin_unlock_irqrestore(&psig->siglock, flags);
2355 read_unlock_irq(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356}
2357
2358/* superblock security operations */
2359
2360static int selinux_sb_alloc_security(struct super_block *sb)
2361{
2362 return superblock_alloc_security(sb);
2363}
2364
2365static void selinux_sb_free_security(struct super_block *sb)
2366{
2367 superblock_free_security(sb);
2368}
2369
2370static inline int match_prefix(char *prefix, int plen, char *option, int olen)
2371{
2372 if (plen > olen)
2373 return 0;
2374
2375 return !memcmp(prefix, option, plen);
2376}
2377
2378static inline int selinux_option(char *option, int len)
2379{
Eric Paris832cbd92008-04-01 13:24:09 -04002380 return (match_prefix(CONTEXT_STR, sizeof(CONTEXT_STR)-1, option, len) ||
2381 match_prefix(FSCONTEXT_STR, sizeof(FSCONTEXT_STR)-1, option, len) ||
2382 match_prefix(DEFCONTEXT_STR, sizeof(DEFCONTEXT_STR)-1, option, len) ||
2383 match_prefix(ROOTCONTEXT_STR, sizeof(ROOTCONTEXT_STR)-1, option, len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384}
2385
2386static inline void take_option(char **to, char *from, int *first, int len)
2387{
2388 if (!*first) {
2389 **to = ',';
2390 *to += 1;
Cory Olmo3528a952006-09-29 01:58:44 -07002391 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 *first = 0;
2393 memcpy(*to, from, len);
2394 *to += len;
2395}
2396
Eric Paris828dfe12008-04-17 13:17:49 -04002397static inline void take_selinux_option(char **to, char *from, int *first,
2398 int len)
Cory Olmo3528a952006-09-29 01:58:44 -07002399{
2400 int current_size = 0;
2401
2402 if (!*first) {
2403 **to = '|';
2404 *to += 1;
Eric Paris828dfe12008-04-17 13:17:49 -04002405 } else
Cory Olmo3528a952006-09-29 01:58:44 -07002406 *first = 0;
2407
2408 while (current_size < len) {
2409 if (*from != '"') {
2410 **to = *from;
2411 *to += 1;
2412 }
2413 from += 1;
2414 current_size += 1;
2415 }
2416}
2417
Eric Parise0007522008-03-05 10:31:54 -05002418static int selinux_sb_copy_data(char *orig, char *copy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419{
2420 int fnosec, fsec, rc = 0;
2421 char *in_save, *in_curr, *in_end;
2422 char *sec_curr, *nosec_save, *nosec;
Cory Olmo3528a952006-09-29 01:58:44 -07002423 int open_quote = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424
2425 in_curr = orig;
2426 sec_curr = copy;
2427
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 nosec = (char *)get_zeroed_page(GFP_KERNEL);
2429 if (!nosec) {
2430 rc = -ENOMEM;
2431 goto out;
2432 }
2433
2434 nosec_save = nosec;
2435 fnosec = fsec = 1;
2436 in_save = in_end = orig;
2437
2438 do {
Cory Olmo3528a952006-09-29 01:58:44 -07002439 if (*in_end == '"')
2440 open_quote = !open_quote;
2441 if ((*in_end == ',' && open_quote == 0) ||
2442 *in_end == '\0') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 int len = in_end - in_curr;
2444
2445 if (selinux_option(in_curr, len))
Cory Olmo3528a952006-09-29 01:58:44 -07002446 take_selinux_option(&sec_curr, in_curr, &fsec, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 else
2448 take_option(&nosec, in_curr, &fnosec, len);
2449
2450 in_curr = in_end + 1;
2451 }
2452 } while (*in_end++);
2453
Eric Paris6931dfc2005-06-30 02:58:51 -07002454 strcpy(in_save, nosec_save);
Gerald Schaeferda3caa22005-06-21 17:15:18 -07002455 free_page((unsigned long)nosec_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456out:
2457 return rc;
2458}
2459
2460static int selinux_sb_kern_mount(struct super_block *sb, void *data)
2461{
David Howells88e67f32008-11-14 10:39:21 +11002462 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 struct avc_audit_data ad;
2464 int rc;
2465
2466 rc = superblock_doinit(sb, data);
2467 if (rc)
2468 return rc;
2469
Eric Paris828dfe12008-04-17 13:17:49 -04002470 AVC_AUDIT_DATA_INIT(&ad, FS);
Jan Blunck44707fd2008-02-14 19:38:33 -08002471 ad.u.fs.path.dentry = sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002472 return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473}
2474
David Howells726c3342006-06-23 02:02:58 -07002475static int selinux_sb_statfs(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476{
David Howells88e67f32008-11-14 10:39:21 +11002477 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 struct avc_audit_data ad;
2479
Eric Paris828dfe12008-04-17 13:17:49 -04002480 AVC_AUDIT_DATA_INIT(&ad, FS);
Jan Blunck44707fd2008-02-14 19:38:33 -08002481 ad.u.fs.path.dentry = dentry->d_sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002482 return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483}
2484
Eric Paris828dfe12008-04-17 13:17:49 -04002485static int selinux_mount(char *dev_name,
Al Virob5266eb2008-03-22 17:48:24 -04002486 struct path *path,
Eric Paris828dfe12008-04-17 13:17:49 -04002487 char *type,
2488 unsigned long flags,
2489 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490{
David Howells88e67f32008-11-14 10:39:21 +11002491 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 int rc;
2493
Al Virob5266eb2008-03-22 17:48:24 -04002494 rc = secondary_ops->sb_mount(dev_name, path, type, flags, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 if (rc)
2496 return rc;
2497
2498 if (flags & MS_REMOUNT)
David Howells88e67f32008-11-14 10:39:21 +11002499 return superblock_has_perm(cred, path->mnt->mnt_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002500 FILESYSTEM__REMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 else
David Howells88e67f32008-11-14 10:39:21 +11002502 return dentry_has_perm(cred, path->mnt, path->dentry,
Eric Paris828dfe12008-04-17 13:17:49 -04002503 FILE__MOUNTON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504}
2505
2506static int selinux_umount(struct vfsmount *mnt, int flags)
2507{
David Howells88e67f32008-11-14 10:39:21 +11002508 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 int rc;
2510
2511 rc = secondary_ops->sb_umount(mnt, flags);
2512 if (rc)
2513 return rc;
2514
David Howells88e67f32008-11-14 10:39:21 +11002515 return superblock_has_perm(cred, mnt->mnt_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002516 FILESYSTEM__UNMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517}
2518
2519/* inode security operations */
2520
2521static int selinux_inode_alloc_security(struct inode *inode)
2522{
2523 return inode_alloc_security(inode);
2524}
2525
2526static void selinux_inode_free_security(struct inode *inode)
2527{
2528 inode_free_security(inode);
2529}
2530
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002531static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
2532 char **name, void **value,
2533 size_t *len)
2534{
David Howells275bb412008-11-14 10:39:19 +11002535 const struct cred *cred = current_cred();
2536 const struct task_security_struct *tsec = cred->security;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002537 struct inode_security_struct *dsec;
2538 struct superblock_security_struct *sbsec;
David Howells275bb412008-11-14 10:39:19 +11002539 u32 sid, newsid, clen;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002540 int rc;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002541 char *namep = NULL, *context;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002542
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002543 dsec = dir->i_security;
2544 sbsec = dir->i_sb->s_security;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002545
David Howells275bb412008-11-14 10:39:19 +11002546 sid = tsec->sid;
2547 newsid = tsec->create_sid;
2548
2549 if (!newsid || sbsec->behavior == SECURITY_FS_USE_MNTPOINT) {
2550 rc = security_transition_sid(sid, dsec->sid,
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002551 inode_mode_to_security_class(inode->i_mode),
2552 &newsid);
2553 if (rc) {
2554 printk(KERN_WARNING "%s: "
2555 "security_transition_sid failed, rc=%d (dev=%s "
2556 "ino=%ld)\n",
Harvey Harrisondd6f9532008-03-06 10:03:59 +11002557 __func__,
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002558 -rc, inode->i_sb->s_id, inode->i_ino);
2559 return rc;
2560 }
2561 }
2562
Eric Paris296fddf2006-09-25 23:32:00 -07002563 /* Possibly defer initialization to selinux_complete_init. */
2564 if (sbsec->initialized) {
2565 struct inode_security_struct *isec = inode->i_security;
2566 isec->sclass = inode_mode_to_security_class(inode->i_mode);
2567 isec->sid = newsid;
2568 isec->initialized = 1;
2569 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002570
Stephen Smalley8aad3872006-03-22 00:09:13 -08002571 if (!ss_initialized || sbsec->behavior == SECURITY_FS_USE_MNTPOINT)
Stephen Smalley25a74f32005-11-08 21:34:33 -08002572 return -EOPNOTSUPP;
2573
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002574 if (name) {
Josef Bacika02fe132008-04-04 09:35:05 +11002575 namep = kstrdup(XATTR_SELINUX_SUFFIX, GFP_NOFS);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002576 if (!namep)
2577 return -ENOMEM;
2578 *name = namep;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002579 }
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002580
2581 if (value && len) {
Stephen Smalley12b29f32008-05-07 13:03:20 -04002582 rc = security_sid_to_context_force(newsid, &context, &clen);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002583 if (rc) {
2584 kfree(namep);
2585 return rc;
2586 }
2587 *value = context;
2588 *len = clen;
2589 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002590
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002591 return 0;
2592}
2593
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594static int selinux_inode_create(struct inode *dir, struct dentry *dentry, int mask)
2595{
2596 return may_create(dir, dentry, SECCLASS_FILE);
2597}
2598
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2600{
2601 int rc;
2602
Eric Paris828dfe12008-04-17 13:17:49 -04002603 rc = secondary_ops->inode_link(old_dentry, dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 if (rc)
2605 return rc;
2606 return may_link(dir, old_dentry, MAY_LINK);
2607}
2608
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
2610{
2611 int rc;
2612
2613 rc = secondary_ops->inode_unlink(dir, dentry);
2614 if (rc)
2615 return rc;
2616 return may_link(dir, dentry, MAY_UNLINK);
2617}
2618
2619static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
2620{
2621 return may_create(dir, dentry, SECCLASS_LNK_FILE);
2622}
2623
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, int mask)
2625{
2626 return may_create(dir, dentry, SECCLASS_DIR);
2627}
2628
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
2630{
2631 return may_link(dir, dentry, MAY_RMDIR);
2632}
2633
2634static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
2635{
2636 int rc;
2637
2638 rc = secondary_ops->inode_mknod(dir, dentry, mode, dev);
2639 if (rc)
2640 return rc;
2641
2642 return may_create(dir, dentry, inode_mode_to_security_class(mode));
2643}
2644
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
Eric Paris828dfe12008-04-17 13:17:49 -04002646 struct inode *new_inode, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647{
2648 return may_rename(old_inode, old_dentry, new_inode, new_dentry);
2649}
2650
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651static int selinux_inode_readlink(struct dentry *dentry)
2652{
David Howells88e67f32008-11-14 10:39:21 +11002653 const struct cred *cred = current_cred();
2654
2655 return dentry_has_perm(cred, NULL, dentry, FILE__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656}
2657
2658static int selinux_inode_follow_link(struct dentry *dentry, struct nameidata *nameidata)
2659{
David Howells88e67f32008-11-14 10:39:21 +11002660 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 int rc;
2662
Eric Paris828dfe12008-04-17 13:17:49 -04002663 rc = secondary_ops->inode_follow_link(dentry, nameidata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 if (rc)
2665 return rc;
David Howells88e67f32008-11-14 10:39:21 +11002666 return dentry_has_perm(cred, NULL, dentry, FILE__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667}
2668
Al Virob77b0642008-07-17 09:37:02 -04002669static int selinux_inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670{
David Howells88e67f32008-11-14 10:39:21 +11002671 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 int rc;
2673
Al Virob77b0642008-07-17 09:37:02 -04002674 rc = secondary_ops->inode_permission(inode, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 if (rc)
2676 return rc;
2677
2678 if (!mask) {
2679 /* No permission to check. Existence test. */
2680 return 0;
2681 }
2682
David Howells88e67f32008-11-14 10:39:21 +11002683 return inode_has_perm(cred, inode,
Eric Paris8b6a5a32008-10-29 17:06:46 -04002684 file_mask_to_av(inode->i_mode, mask), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685}
2686
2687static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
2688{
David Howells88e67f32008-11-14 10:39:21 +11002689 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 int rc;
2691
2692 rc = secondary_ops->inode_setattr(dentry, iattr);
2693 if (rc)
2694 return rc;
2695
2696 if (iattr->ia_valid & ATTR_FORCE)
2697 return 0;
2698
2699 if (iattr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
2700 ATTR_ATIME_SET | ATTR_MTIME_SET))
David Howells88e67f32008-11-14 10:39:21 +11002701 return dentry_has_perm(cred, NULL, dentry, FILE__SETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702
David Howells88e67f32008-11-14 10:39:21 +11002703 return dentry_has_perm(cred, NULL, dentry, FILE__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704}
2705
2706static int selinux_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
2707{
David Howells88e67f32008-11-14 10:39:21 +11002708 const struct cred *cred = current_cred();
2709
2710 return dentry_has_perm(cred, mnt, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711}
2712
David Howells8f0cfa52008-04-29 00:59:41 -07002713static int selinux_inode_setotherxattr(struct dentry *dentry, const char *name)
Serge E. Hallynb5376772007-10-16 23:31:36 -07002714{
David Howells88e67f32008-11-14 10:39:21 +11002715 const struct cred *cred = current_cred();
2716
Serge E. Hallynb5376772007-10-16 23:31:36 -07002717 if (!strncmp(name, XATTR_SECURITY_PREFIX,
2718 sizeof XATTR_SECURITY_PREFIX - 1)) {
2719 if (!strcmp(name, XATTR_NAME_CAPS)) {
2720 if (!capable(CAP_SETFCAP))
2721 return -EPERM;
2722 } else if (!capable(CAP_SYS_ADMIN)) {
2723 /* A different attribute in the security namespace.
2724 Restrict to administrator. */
2725 return -EPERM;
2726 }
2727 }
2728
2729 /* Not an attribute we recognize, so just check the
2730 ordinary setattr permission. */
David Howells88e67f32008-11-14 10:39:21 +11002731 return dentry_has_perm(cred, NULL, dentry, FILE__SETATTR);
Serge E. Hallynb5376772007-10-16 23:31:36 -07002732}
2733
David Howells8f0cfa52008-04-29 00:59:41 -07002734static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
2735 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 struct inode *inode = dentry->d_inode;
2738 struct inode_security_struct *isec = inode->i_security;
2739 struct superblock_security_struct *sbsec;
2740 struct avc_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11002741 u32 newsid, sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 int rc = 0;
2743
Serge E. Hallynb5376772007-10-16 23:31:36 -07002744 if (strcmp(name, XATTR_NAME_SELINUX))
2745 return selinux_inode_setotherxattr(dentry, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746
2747 sbsec = inode->i_sb->s_security;
2748 if (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)
2749 return -EOPNOTSUPP;
2750
Satyam Sharma3bd858a2007-07-17 15:00:08 +05302751 if (!is_owner_or_cap(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 return -EPERM;
2753
Eric Paris828dfe12008-04-17 13:17:49 -04002754 AVC_AUDIT_DATA_INIT(&ad, FS);
Jan Blunck44707fd2008-02-14 19:38:33 -08002755 ad.u.fs.path.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756
David Howells275bb412008-11-14 10:39:19 +11002757 rc = avc_has_perm(sid, isec->sid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 FILE__RELABELFROM, &ad);
2759 if (rc)
2760 return rc;
2761
2762 rc = security_context_to_sid(value, size, &newsid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04002763 if (rc == -EINVAL) {
2764 if (!capable(CAP_MAC_ADMIN))
2765 return rc;
2766 rc = security_context_to_sid_force(value, size, &newsid);
2767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 if (rc)
2769 return rc;
2770
David Howells275bb412008-11-14 10:39:19 +11002771 rc = avc_has_perm(sid, newsid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 FILE__RELABELTO, &ad);
2773 if (rc)
2774 return rc;
2775
David Howells275bb412008-11-14 10:39:19 +11002776 rc = security_validate_transition(isec->sid, newsid, sid,
Eric Paris828dfe12008-04-17 13:17:49 -04002777 isec->sclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 if (rc)
2779 return rc;
2780
2781 return avc_has_perm(newsid,
2782 sbsec->sid,
2783 SECCLASS_FILESYSTEM,
2784 FILESYSTEM__ASSOCIATE,
2785 &ad);
2786}
2787
David Howells8f0cfa52008-04-29 00:59:41 -07002788static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
Eric Parisf5269712008-05-14 11:27:45 -04002789 const void *value, size_t size,
David Howells8f0cfa52008-04-29 00:59:41 -07002790 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791{
2792 struct inode *inode = dentry->d_inode;
2793 struct inode_security_struct *isec = inode->i_security;
2794 u32 newsid;
2795 int rc;
2796
2797 if (strcmp(name, XATTR_NAME_SELINUX)) {
2798 /* Not an attribute we recognize, so nothing to do. */
2799 return;
2800 }
2801
Stephen Smalley12b29f32008-05-07 13:03:20 -04002802 rc = security_context_to_sid_force(value, size, &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 if (rc) {
Stephen Smalley12b29f32008-05-07 13:03:20 -04002804 printk(KERN_ERR "SELinux: unable to map context to SID"
2805 "for (%s, %lu), rc=%d\n",
2806 inode->i_sb->s_id, inode->i_ino, -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 return;
2808 }
2809
2810 isec->sid = newsid;
2811 return;
2812}
2813
David Howells8f0cfa52008-04-29 00:59:41 -07002814static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815{
David Howells88e67f32008-11-14 10:39:21 +11002816 const struct cred *cred = current_cred();
2817
2818 return dentry_has_perm(cred, NULL, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819}
2820
Eric Paris828dfe12008-04-17 13:17:49 -04002821static int selinux_inode_listxattr(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822{
David Howells88e67f32008-11-14 10:39:21 +11002823 const struct cred *cred = current_cred();
2824
2825 return dentry_has_perm(cred, NULL, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826}
2827
David Howells8f0cfa52008-04-29 00:59:41 -07002828static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829{
Serge E. Hallynb5376772007-10-16 23:31:36 -07002830 if (strcmp(name, XATTR_NAME_SELINUX))
2831 return selinux_inode_setotherxattr(dentry, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832
2833 /* No one is allowed to remove a SELinux security label.
2834 You can change the label, but all data must be labeled. */
2835 return -EACCES;
2836}
2837
James Morrisd381d8a2005-10-30 14:59:22 -08002838/*
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04002839 * Copy the inode security context value to the user.
James Morrisd381d8a2005-10-30 14:59:22 -08002840 *
2841 * Permission check is handled by selinux_inode_getxattr hook.
2842 */
David P. Quigley42492592008-02-04 22:29:39 -08002843static int selinux_inode_getsecurity(const struct inode *inode, const char *name, void **buffer, bool alloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844{
David P. Quigley42492592008-02-04 22:29:39 -08002845 u32 size;
2846 int error;
2847 char *context = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 struct inode_security_struct *isec = inode->i_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00002850 if (strcmp(name, XATTR_SELINUX_SUFFIX))
2851 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04002853 /*
2854 * If the caller has CAP_MAC_ADMIN, then get the raw context
2855 * value even if it is not defined by current policy; otherwise,
2856 * use the in-core value under current policy.
2857 * Use the non-auditing forms of the permission checks since
2858 * getxattr may be called by unprivileged processes commonly
2859 * and lack of permission just means that we fall back to the
2860 * in-core context value, not a denial.
2861 */
Eric Paris06674672008-11-11 22:02:57 +11002862 error = selinux_capable(current, CAP_MAC_ADMIN, SECURITY_CAP_NOAUDIT);
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04002863 if (!error)
2864 error = security_sid_to_context_force(isec->sid, &context,
2865 &size);
2866 else
2867 error = security_sid_to_context(isec->sid, &context, &size);
David P. Quigley42492592008-02-04 22:29:39 -08002868 if (error)
2869 return error;
2870 error = size;
2871 if (alloc) {
2872 *buffer = context;
2873 goto out_nofree;
2874 }
2875 kfree(context);
2876out_nofree:
2877 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878}
2879
2880static int selinux_inode_setsecurity(struct inode *inode, const char *name,
Eric Paris828dfe12008-04-17 13:17:49 -04002881 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882{
2883 struct inode_security_struct *isec = inode->i_security;
2884 u32 newsid;
2885 int rc;
2886
2887 if (strcmp(name, XATTR_SELINUX_SUFFIX))
2888 return -EOPNOTSUPP;
2889
2890 if (!value || !size)
2891 return -EACCES;
2892
Eric Paris828dfe12008-04-17 13:17:49 -04002893 rc = security_context_to_sid((void *)value, size, &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 if (rc)
2895 return rc;
2896
2897 isec->sid = newsid;
2898 return 0;
2899}
2900
2901static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
2902{
2903 const int len = sizeof(XATTR_NAME_SELINUX);
2904 if (buffer && len <= buffer_size)
2905 memcpy(buffer, XATTR_NAME_SELINUX, len);
2906 return len;
2907}
2908
Serge E. Hallynb5376772007-10-16 23:31:36 -07002909static int selinux_inode_need_killpriv(struct dentry *dentry)
2910{
2911 return secondary_ops->inode_need_killpriv(dentry);
2912}
2913
2914static int selinux_inode_killpriv(struct dentry *dentry)
2915{
2916 return secondary_ops->inode_killpriv(dentry);
2917}
2918
Ahmed S. Darwish713a04a2008-03-01 21:52:30 +02002919static void selinux_inode_getsecid(const struct inode *inode, u32 *secid)
2920{
2921 struct inode_security_struct *isec = inode->i_security;
2922 *secid = isec->sid;
2923}
2924
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925/* file security operations */
2926
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09002927static int selinux_revalidate_file_permission(struct file *file, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928{
David Howells88e67f32008-11-14 10:39:21 +11002929 const struct cred *cred = current_cred();
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002930 int rc;
Josef Sipek3d5ff522006-12-08 02:37:38 -08002931 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932
2933 if (!mask) {
2934 /* No permission to check. Existence test. */
2935 return 0;
2936 }
2937
2938 /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
2939 if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
2940 mask |= MAY_APPEND;
2941
David Howells88e67f32008-11-14 10:39:21 +11002942 rc = file_has_perm(cred, file,
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002943 file_mask_to_av(inode->i_mode, mask));
2944 if (rc)
2945 return rc;
2946
2947 return selinux_netlbl_inode_permission(inode, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948}
2949
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09002950static int selinux_file_permission(struct file *file, int mask)
2951{
2952 struct inode *inode = file->f_path.dentry->d_inode;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09002953 struct file_security_struct *fsec = file->f_security;
2954 struct inode_security_struct *isec = inode->i_security;
David Howells275bb412008-11-14 10:39:19 +11002955 u32 sid = current_sid();
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09002956
2957 if (!mask) {
2958 /* No permission to check. Existence test. */
2959 return 0;
2960 }
2961
David Howells275bb412008-11-14 10:39:19 +11002962 if (sid == fsec->sid && fsec->isid == isec->sid
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09002963 && fsec->pseqno == avc_policy_seqno())
2964 return selinux_netlbl_inode_permission(inode, mask);
2965
2966 return selinux_revalidate_file_permission(file, mask);
2967}
2968
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969static int selinux_file_alloc_security(struct file *file)
2970{
2971 return file_alloc_security(file);
2972}
2973
2974static void selinux_file_free_security(struct file *file)
2975{
2976 file_free_security(file);
2977}
2978
2979static int selinux_file_ioctl(struct file *file, unsigned int cmd,
2980 unsigned long arg)
2981{
David Howells88e67f32008-11-14 10:39:21 +11002982 const struct cred *cred = current_cred();
Stephen Smalley242631c2008-06-05 09:21:28 -04002983 u32 av = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984
Stephen Smalley242631c2008-06-05 09:21:28 -04002985 if (_IOC_DIR(cmd) & _IOC_WRITE)
2986 av |= FILE__WRITE;
2987 if (_IOC_DIR(cmd) & _IOC_READ)
2988 av |= FILE__READ;
2989 if (!av)
2990 av = FILE__IOCTL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991
David Howells88e67f32008-11-14 10:39:21 +11002992 return file_has_perm(cred, file, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993}
2994
2995static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
2996{
David Howells88e67f32008-11-14 10:39:21 +11002997 const struct cred *cred = current_cred();
David Howellsd84f4f92008-11-14 10:39:23 +11002998 int rc = 0;
David Howells88e67f32008-11-14 10:39:21 +11002999
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000#ifndef CONFIG_PPC32
3001 if ((prot & PROT_EXEC) && (!file || (!shared && (prot & PROT_WRITE)))) {
3002 /*
3003 * We are making executable an anonymous mapping or a
3004 * private file mapping that will also be writable.
3005 * This has an additional check.
3006 */
David Howellsd84f4f92008-11-14 10:39:23 +11003007 rc = cred_has_perm(cred, cred, PROCESS__EXECMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 if (rc)
David Howellsd84f4f92008-11-14 10:39:23 +11003009 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 }
3011#endif
3012
3013 if (file) {
3014 /* read access is always possible with a mapping */
3015 u32 av = FILE__READ;
3016
3017 /* write access only matters if the mapping is shared */
3018 if (shared && (prot & PROT_WRITE))
3019 av |= FILE__WRITE;
3020
3021 if (prot & PROT_EXEC)
3022 av |= FILE__EXECUTE;
3023
David Howells88e67f32008-11-14 10:39:21 +11003024 return file_has_perm(cred, file, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 }
David Howellsd84f4f92008-11-14 10:39:23 +11003026
3027error:
3028 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029}
3030
3031static int selinux_file_mmap(struct file *file, unsigned long reqprot,
Eric Parised032182007-06-28 15:55:21 -04003032 unsigned long prot, unsigned long flags,
3033 unsigned long addr, unsigned long addr_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034{
Eric Parised032182007-06-28 15:55:21 -04003035 int rc = 0;
David Howells275bb412008-11-14 10:39:19 +11003036 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037
Eric Parised032182007-06-28 15:55:21 -04003038 if (addr < mmap_min_addr)
3039 rc = avc_has_perm(sid, sid, SECCLASS_MEMPROTECT,
3040 MEMPROTECT__MMAP_ZERO, NULL);
3041 if (rc || addr_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 return rc;
3043
3044 if (selinux_checkreqprot)
3045 prot = reqprot;
3046
3047 return file_map_prot_check(file, prot,
3048 (flags & MAP_TYPE) == MAP_SHARED);
3049}
3050
3051static int selinux_file_mprotect(struct vm_area_struct *vma,
3052 unsigned long reqprot,
3053 unsigned long prot)
3054{
David Howells88e67f32008-11-14 10:39:21 +11003055 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 int rc;
3057
3058 rc = secondary_ops->file_mprotect(vma, reqprot, prot);
3059 if (rc)
3060 return rc;
3061
3062 if (selinux_checkreqprot)
3063 prot = reqprot;
3064
3065#ifndef CONFIG_PPC32
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003066 if ((prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
3067 rc = 0;
3068 if (vma->vm_start >= vma->vm_mm->start_brk &&
3069 vma->vm_end <= vma->vm_mm->brk) {
David Howellsd84f4f92008-11-14 10:39:23 +11003070 rc = cred_has_perm(cred, cred, PROCESS__EXECHEAP);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003071 } else if (!vma->vm_file &&
3072 vma->vm_start <= vma->vm_mm->start_stack &&
3073 vma->vm_end >= vma->vm_mm->start_stack) {
3074 rc = task_has_perm(current, current, PROCESS__EXECSTACK);
3075 } else if (vma->vm_file && vma->anon_vma) {
3076 /*
3077 * We are making executable a file mapping that has
3078 * had some COW done. Since pages might have been
3079 * written, check ability to execute the possibly
3080 * modified content. This typically should only
3081 * occur for text relocations.
3082 */
David Howellsd84f4f92008-11-14 10:39:23 +11003083 rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003084 }
Lorenzo Hernandez García-Hierro6b992192005-06-25 14:54:34 -07003085 if (rc)
3086 return rc;
3087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088#endif
3089
3090 return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
3091}
3092
3093static int selinux_file_lock(struct file *file, unsigned int cmd)
3094{
David Howells88e67f32008-11-14 10:39:21 +11003095 const struct cred *cred = current_cred();
3096
3097 return file_has_perm(cred, file, FILE__LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098}
3099
3100static int selinux_file_fcntl(struct file *file, unsigned int cmd,
3101 unsigned long arg)
3102{
David Howells88e67f32008-11-14 10:39:21 +11003103 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 int err = 0;
3105
3106 switch (cmd) {
Eric Paris828dfe12008-04-17 13:17:49 -04003107 case F_SETFL:
3108 if (!file->f_path.dentry || !file->f_path.dentry->d_inode) {
3109 err = -EINVAL;
3110 break;
3111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112
Eric Paris828dfe12008-04-17 13:17:49 -04003113 if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
David Howells88e67f32008-11-14 10:39:21 +11003114 err = file_has_perm(cred, file, FILE__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003116 }
3117 /* fall through */
3118 case F_SETOWN:
3119 case F_SETSIG:
3120 case F_GETFL:
3121 case F_GETOWN:
3122 case F_GETSIG:
3123 /* Just check FD__USE permission */
David Howells88e67f32008-11-14 10:39:21 +11003124 err = file_has_perm(cred, file, 0);
Eric Paris828dfe12008-04-17 13:17:49 -04003125 break;
3126 case F_GETLK:
3127 case F_SETLK:
3128 case F_SETLKW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129#if BITS_PER_LONG == 32
Eric Paris828dfe12008-04-17 13:17:49 -04003130 case F_GETLK64:
3131 case F_SETLK64:
3132 case F_SETLKW64:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133#endif
Eric Paris828dfe12008-04-17 13:17:49 -04003134 if (!file->f_path.dentry || !file->f_path.dentry->d_inode) {
3135 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003137 }
David Howells88e67f32008-11-14 10:39:21 +11003138 err = file_has_perm(cred, file, FILE__LOCK);
Eric Paris828dfe12008-04-17 13:17:49 -04003139 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 }
3141
3142 return err;
3143}
3144
3145static int selinux_file_set_fowner(struct file *file)
3146{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 struct file_security_struct *fsec;
3148
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 fsec = file->f_security;
David Howells275bb412008-11-14 10:39:19 +11003150 fsec->fown_sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151
3152 return 0;
3153}
3154
3155static int selinux_file_send_sigiotask(struct task_struct *tsk,
3156 struct fown_struct *fown, int signum)
3157{
Eric Paris828dfe12008-04-17 13:17:49 -04003158 struct file *file;
David Howells275bb412008-11-14 10:39:19 +11003159 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 struct file_security_struct *fsec;
3162
3163 /* struct fown_struct is never outside the context of a struct file */
Eric Paris828dfe12008-04-17 13:17:49 -04003164 file = container_of(fown, struct file, f_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 fsec = file->f_security;
3167
3168 if (!signum)
3169 perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
3170 else
3171 perm = signal_to_av(signum);
3172
David Howells275bb412008-11-14 10:39:19 +11003173 return avc_has_perm(fsec->fown_sid, sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 SECCLASS_PROCESS, perm, NULL);
3175}
3176
3177static int selinux_file_receive(struct file *file)
3178{
David Howells88e67f32008-11-14 10:39:21 +11003179 const struct cred *cred = current_cred();
3180
3181 return file_has_perm(cred, file, file_to_av(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182}
3183
David Howells745ca242008-11-14 10:39:22 +11003184static int selinux_dentry_open(struct file *file, const struct cred *cred)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003185{
3186 struct file_security_struct *fsec;
3187 struct inode *inode;
3188 struct inode_security_struct *isec;
David Howellsd84f4f92008-11-14 10:39:23 +11003189
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003190 inode = file->f_path.dentry->d_inode;
3191 fsec = file->f_security;
3192 isec = inode->i_security;
3193 /*
3194 * Save inode label and policy sequence number
3195 * at open-time so that selinux_file_permission
3196 * can determine whether revalidation is necessary.
3197 * Task label is already saved in the file security
3198 * struct as its SID.
3199 */
3200 fsec->isid = isec->sid;
3201 fsec->pseqno = avc_policy_seqno();
3202 /*
3203 * Since the inode label or policy seqno may have changed
3204 * between the selinux_inode_permission check and the saving
3205 * of state above, recheck that access is still permitted.
3206 * Otherwise, access might never be revalidated against the
3207 * new inode label or new policy.
3208 * This check is not redundant - do not remove.
3209 */
David Howells88e67f32008-11-14 10:39:21 +11003210 return inode_has_perm(cred, inode, open_file_to_av(file), NULL);
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003211}
3212
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213/* task security operations */
3214
3215static int selinux_task_create(unsigned long clone_flags)
3216{
3217 int rc;
3218
3219 rc = secondary_ops->task_create(clone_flags);
3220 if (rc)
3221 return rc;
3222
3223 return task_has_perm(current, current, PROCESS__FORK);
3224}
3225
David Howellsf1752ee2008-11-14 10:39:17 +11003226/*
3227 * detach and free the LSM part of a set of credentials
3228 */
3229static void selinux_cred_free(struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230{
David Howellsf1752ee2008-11-14 10:39:17 +11003231 struct task_security_struct *tsec = cred->security;
3232 cred->security = NULL;
3233 kfree(tsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234}
3235
David Howellsd84f4f92008-11-14 10:39:23 +11003236/*
3237 * prepare a new set of credentials for modification
3238 */
3239static int selinux_cred_prepare(struct cred *new, const struct cred *old,
3240 gfp_t gfp)
3241{
3242 const struct task_security_struct *old_tsec;
3243 struct task_security_struct *tsec;
3244
3245 old_tsec = old->security;
3246
3247 tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp);
3248 if (!tsec)
3249 return -ENOMEM;
3250
3251 new->security = tsec;
3252 return 0;
3253}
3254
3255/*
3256 * commit new credentials
3257 */
3258static void selinux_cred_commit(struct cred *new, const struct cred *old)
3259{
3260 secondary_ops->cred_commit(new, old);
3261}
3262
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263static int selinux_task_setuid(uid_t id0, uid_t id1, uid_t id2, int flags)
3264{
3265 /* Since setuid only affects the current process, and
3266 since the SELinux controls are not based on the Linux
3267 identity attributes, SELinux does not need to control
3268 this operation. However, SELinux does control the use
3269 of the CAP_SETUID and CAP_SETGID capabilities using the
3270 capable hook. */
3271 return 0;
3272}
3273
David Howellsd84f4f92008-11-14 10:39:23 +11003274static int selinux_task_fix_setuid(struct cred *new, const struct cred *old,
3275 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276{
David Howellsd84f4f92008-11-14 10:39:23 +11003277 return secondary_ops->task_fix_setuid(new, old, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278}
3279
3280static int selinux_task_setgid(gid_t id0, gid_t id1, gid_t id2, int flags)
3281{
3282 /* See the comment for setuid above. */
3283 return 0;
3284}
3285
3286static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
3287{
3288 return task_has_perm(current, p, PROCESS__SETPGID);
3289}
3290
3291static int selinux_task_getpgid(struct task_struct *p)
3292{
3293 return task_has_perm(current, p, PROCESS__GETPGID);
3294}
3295
3296static int selinux_task_getsid(struct task_struct *p)
3297{
3298 return task_has_perm(current, p, PROCESS__GETSESSION);
3299}
3300
David Quigleyf9008e42006-06-30 01:55:46 -07003301static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
3302{
David Howells275bb412008-11-14 10:39:19 +11003303 *secid = task_sid(p);
David Quigleyf9008e42006-06-30 01:55:46 -07003304}
3305
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306static int selinux_task_setgroups(struct group_info *group_info)
3307{
3308 /* See the comment for setuid above. */
3309 return 0;
3310}
3311
3312static int selinux_task_setnice(struct task_struct *p, int nice)
3313{
3314 int rc;
3315
3316 rc = secondary_ops->task_setnice(p, nice);
3317 if (rc)
3318 return rc;
3319
Eric Paris828dfe12008-04-17 13:17:49 -04003320 return task_has_perm(current, p, PROCESS__SETSCHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321}
3322
James Morris03e68062006-06-23 02:03:58 -07003323static int selinux_task_setioprio(struct task_struct *p, int ioprio)
3324{
Serge E. Hallynb5376772007-10-16 23:31:36 -07003325 int rc;
3326
3327 rc = secondary_ops->task_setioprio(p, ioprio);
3328 if (rc)
3329 return rc;
3330
James Morris03e68062006-06-23 02:03:58 -07003331 return task_has_perm(current, p, PROCESS__SETSCHED);
3332}
3333
David Quigleya1836a42006-06-30 01:55:49 -07003334static int selinux_task_getioprio(struct task_struct *p)
3335{
3336 return task_has_perm(current, p, PROCESS__GETSCHED);
3337}
3338
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339static int selinux_task_setrlimit(unsigned int resource, struct rlimit *new_rlim)
3340{
3341 struct rlimit *old_rlim = current->signal->rlim + resource;
3342 int rc;
3343
3344 rc = secondary_ops->task_setrlimit(resource, new_rlim);
3345 if (rc)
3346 return rc;
3347
3348 /* Control the ability to change the hard limit (whether
3349 lowering or raising it), so that the hard limit can
3350 later be used as a safe reset point for the soft limit
David Howellsd84f4f92008-11-14 10:39:23 +11003351 upon context transitions. See selinux_bprm_committing_creds. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 if (old_rlim->rlim_max != new_rlim->rlim_max)
3353 return task_has_perm(current, current, PROCESS__SETRLIMIT);
3354
3355 return 0;
3356}
3357
3358static int selinux_task_setscheduler(struct task_struct *p, int policy, struct sched_param *lp)
3359{
Serge E. Hallynb5376772007-10-16 23:31:36 -07003360 int rc;
3361
3362 rc = secondary_ops->task_setscheduler(p, policy, lp);
3363 if (rc)
3364 return rc;
3365
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 return task_has_perm(current, p, PROCESS__SETSCHED);
3367}
3368
3369static int selinux_task_getscheduler(struct task_struct *p)
3370{
3371 return task_has_perm(current, p, PROCESS__GETSCHED);
3372}
3373
David Quigley35601542006-06-23 02:04:01 -07003374static int selinux_task_movememory(struct task_struct *p)
3375{
3376 return task_has_perm(current, p, PROCESS__SETSCHED);
3377}
3378
David Quigleyf9008e42006-06-30 01:55:46 -07003379static int selinux_task_kill(struct task_struct *p, struct siginfo *info,
3380 int sig, u32 secid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381{
3382 u32 perm;
3383 int rc;
3384
David Quigleyf9008e42006-06-30 01:55:46 -07003385 rc = secondary_ops->task_kill(p, info, sig, secid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 if (rc)
3387 return rc;
3388
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 if (!sig)
3390 perm = PROCESS__SIGNULL; /* null signal; existence test */
3391 else
3392 perm = signal_to_av(sig);
David Quigleyf9008e42006-06-30 01:55:46 -07003393 if (secid)
David Howells275bb412008-11-14 10:39:19 +11003394 rc = avc_has_perm(secid, task_sid(p),
3395 SECCLASS_PROCESS, perm, NULL);
David Quigleyf9008e42006-06-30 01:55:46 -07003396 else
3397 rc = task_has_perm(current, p, perm);
3398 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399}
3400
3401static int selinux_task_prctl(int option,
3402 unsigned long arg2,
3403 unsigned long arg3,
3404 unsigned long arg4,
David Howellsd84f4f92008-11-14 10:39:23 +11003405 unsigned long arg5)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406{
3407 /* The current prctl operations do not appear to require
3408 any SELinux controls since they merely observe or modify
3409 the state of the current process. */
David Howellsd84f4f92008-11-14 10:39:23 +11003410 return secondary_ops->task_prctl(option, arg2, arg3, arg4, arg5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411}
3412
3413static int selinux_task_wait(struct task_struct *p)
3414{
Eric Paris8a535142007-10-22 16:10:31 -04003415 return task_has_perm(p, current, PROCESS__SIGCHLD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416}
3417
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418static void selinux_task_to_inode(struct task_struct *p,
3419 struct inode *inode)
3420{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 struct inode_security_struct *isec = inode->i_security;
David Howells275bb412008-11-14 10:39:19 +11003422 u32 sid = task_sid(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423
David Howells275bb412008-11-14 10:39:19 +11003424 isec->sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 isec->initialized = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426}
3427
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003429static int selinux_parse_skb_ipv4(struct sk_buff *skb,
3430 struct avc_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431{
3432 int offset, ihlen, ret = -EINVAL;
3433 struct iphdr _iph, *ih;
3434
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03003435 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
3437 if (ih == NULL)
3438 goto out;
3439
3440 ihlen = ih->ihl * 4;
3441 if (ihlen < sizeof(_iph))
3442 goto out;
3443
3444 ad->u.net.v4info.saddr = ih->saddr;
3445 ad->u.net.v4info.daddr = ih->daddr;
3446 ret = 0;
3447
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003448 if (proto)
3449 *proto = ih->protocol;
3450
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 switch (ih->protocol) {
Eric Paris828dfe12008-04-17 13:17:49 -04003452 case IPPROTO_TCP: {
3453 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454
Eric Paris828dfe12008-04-17 13:17:49 -04003455 if (ntohs(ih->frag_off) & IP_OFFSET)
3456 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457
3458 offset += ihlen;
3459 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3460 if (th == NULL)
3461 break;
3462
3463 ad->u.net.sport = th->source;
3464 ad->u.net.dport = th->dest;
3465 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467
Eric Paris828dfe12008-04-17 13:17:49 -04003468 case IPPROTO_UDP: {
3469 struct udphdr _udph, *uh;
3470
3471 if (ntohs(ih->frag_off) & IP_OFFSET)
3472 break;
3473
3474 offset += ihlen;
3475 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3476 if (uh == NULL)
3477 break;
3478
3479 ad->u.net.sport = uh->source;
3480 ad->u.net.dport = uh->dest;
3481 break;
3482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483
James Morris2ee92d42006-11-13 16:09:01 -08003484 case IPPROTO_DCCP: {
3485 struct dccp_hdr _dccph, *dh;
3486
3487 if (ntohs(ih->frag_off) & IP_OFFSET)
3488 break;
3489
3490 offset += ihlen;
3491 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3492 if (dh == NULL)
3493 break;
3494
3495 ad->u.net.sport = dh->dccph_sport;
3496 ad->u.net.dport = dh->dccph_dport;
3497 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003498 }
James Morris2ee92d42006-11-13 16:09:01 -08003499
Eric Paris828dfe12008-04-17 13:17:49 -04003500 default:
3501 break;
3502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503out:
3504 return ret;
3505}
3506
3507#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3508
3509/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003510static int selinux_parse_skb_ipv6(struct sk_buff *skb,
3511 struct avc_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512{
3513 u8 nexthdr;
3514 int ret = -EINVAL, offset;
3515 struct ipv6hdr _ipv6h, *ip6;
3516
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03003517 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
3519 if (ip6 == NULL)
3520 goto out;
3521
3522 ipv6_addr_copy(&ad->u.net.v6info.saddr, &ip6->saddr);
3523 ipv6_addr_copy(&ad->u.net.v6info.daddr, &ip6->daddr);
3524 ret = 0;
3525
3526 nexthdr = ip6->nexthdr;
3527 offset += sizeof(_ipv6h);
Herbert Xu0d3d0772005-04-24 20:16:19 -07003528 offset = ipv6_skip_exthdr(skb, offset, &nexthdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 if (offset < 0)
3530 goto out;
3531
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003532 if (proto)
3533 *proto = nexthdr;
3534
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 switch (nexthdr) {
3536 case IPPROTO_TCP: {
Eric Paris828dfe12008-04-17 13:17:49 -04003537 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538
3539 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3540 if (th == NULL)
3541 break;
3542
3543 ad->u.net.sport = th->source;
3544 ad->u.net.dport = th->dest;
3545 break;
3546 }
3547
3548 case IPPROTO_UDP: {
3549 struct udphdr _udph, *uh;
3550
3551 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
3552 if (uh == NULL)
3553 break;
3554
3555 ad->u.net.sport = uh->source;
3556 ad->u.net.dport = uh->dest;
3557 break;
3558 }
3559
James Morris2ee92d42006-11-13 16:09:01 -08003560 case IPPROTO_DCCP: {
3561 struct dccp_hdr _dccph, *dh;
3562
3563 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
3564 if (dh == NULL)
3565 break;
3566
3567 ad->u.net.sport = dh->dccph_sport;
3568 ad->u.net.dport = dh->dccph_dport;
3569 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003570 }
James Morris2ee92d42006-11-13 16:09:01 -08003571
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 /* includes fragments */
3573 default:
3574 break;
3575 }
3576out:
3577 return ret;
3578}
3579
3580#endif /* IPV6 */
3581
3582static int selinux_parse_skb(struct sk_buff *skb, struct avc_audit_data *ad,
David Howellscf9481e2008-07-27 21:31:07 +10003583 char **_addrp, int src, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584{
David Howellscf9481e2008-07-27 21:31:07 +10003585 char *addrp;
3586 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587
3588 switch (ad->u.net.family) {
3589 case PF_INET:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003590 ret = selinux_parse_skb_ipv4(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10003591 if (ret)
3592 goto parse_error;
3593 addrp = (char *)(src ? &ad->u.net.v4info.saddr :
3594 &ad->u.net.v4info.daddr);
3595 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596
3597#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3598 case PF_INET6:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003599 ret = selinux_parse_skb_ipv6(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10003600 if (ret)
3601 goto parse_error;
3602 addrp = (char *)(src ? &ad->u.net.v6info.saddr :
3603 &ad->u.net.v6info.daddr);
3604 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605#endif /* IPV6 */
3606 default:
David Howellscf9481e2008-07-27 21:31:07 +10003607 addrp = NULL;
3608 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 }
3610
David Howellscf9481e2008-07-27 21:31:07 +10003611parse_error:
3612 printk(KERN_WARNING
3613 "SELinux: failure in selinux_parse_skb(),"
3614 " unable to parse packet\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 return ret;
David Howellscf9481e2008-07-27 21:31:07 +10003616
3617okay:
3618 if (_addrp)
3619 *_addrp = addrp;
3620 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621}
3622
Paul Moore4f6a9932007-03-01 14:35:22 -05003623/**
Paul Moore220deb92008-01-29 08:38:23 -05003624 * selinux_skb_peerlbl_sid - Determine the peer label of a packet
Paul Moore4f6a9932007-03-01 14:35:22 -05003625 * @skb: the packet
Paul Moore75e22912008-01-29 08:38:04 -05003626 * @family: protocol family
Paul Moore220deb92008-01-29 08:38:23 -05003627 * @sid: the packet's peer label SID
Paul Moore4f6a9932007-03-01 14:35:22 -05003628 *
3629 * Description:
Paul Moore220deb92008-01-29 08:38:23 -05003630 * Check the various different forms of network peer labeling and determine
3631 * the peer label/SID for the packet; most of the magic actually occurs in
3632 * the security server function security_net_peersid_cmp(). The function
3633 * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
3634 * or -EACCES if @sid is invalid due to inconsistencies with the different
3635 * peer labels.
Paul Moore4f6a9932007-03-01 14:35:22 -05003636 *
3637 */
Paul Moore220deb92008-01-29 08:38:23 -05003638static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
Paul Moore4f6a9932007-03-01 14:35:22 -05003639{
Paul Moore71f1cb02008-01-29 08:51:16 -05003640 int err;
Paul Moore4f6a9932007-03-01 14:35:22 -05003641 u32 xfrm_sid;
3642 u32 nlbl_sid;
Paul Moore220deb92008-01-29 08:38:23 -05003643 u32 nlbl_type;
Paul Moore4f6a9932007-03-01 14:35:22 -05003644
3645 selinux_skb_xfrm_sid(skb, &xfrm_sid);
Paul Moore5dbe1eb2008-01-29 08:44:18 -05003646 selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
Paul Moore220deb92008-01-29 08:38:23 -05003647
Paul Moore71f1cb02008-01-29 08:51:16 -05003648 err = security_net_peersid_resolve(nlbl_sid, nlbl_type, xfrm_sid, sid);
3649 if (unlikely(err)) {
3650 printk(KERN_WARNING
3651 "SELinux: failure in selinux_skb_peerlbl_sid(),"
3652 " unable to determine packet's peer label\n");
Paul Moore220deb92008-01-29 08:38:23 -05003653 return -EACCES;
Paul Moore71f1cb02008-01-29 08:51:16 -05003654 }
Paul Moore220deb92008-01-29 08:38:23 -05003655
3656 return 0;
Paul Moore4f6a9932007-03-01 14:35:22 -05003657}
3658
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659/* socket security operations */
3660static int socket_has_perm(struct task_struct *task, struct socket *sock,
3661 u32 perms)
3662{
3663 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 struct avc_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11003665 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 int err = 0;
3667
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 isec = SOCK_INODE(sock)->i_security;
3669
3670 if (isec->sid == SECINITSID_KERNEL)
3671 goto out;
David Howells275bb412008-11-14 10:39:19 +11003672 sid = task_sid(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673
Eric Paris828dfe12008-04-17 13:17:49 -04003674 AVC_AUDIT_DATA_INIT(&ad, NET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 ad.u.net.sk = sock->sk;
David Howells275bb412008-11-14 10:39:19 +11003676 err = avc_has_perm(sid, isec->sid, isec->sclass, perms, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677
3678out:
3679 return err;
3680}
3681
3682static int selinux_socket_create(int family, int type,
3683 int protocol, int kern)
3684{
David Howells275bb412008-11-14 10:39:19 +11003685 const struct cred *cred = current_cred();
3686 const struct task_security_struct *tsec = cred->security;
3687 u32 sid, newsid;
3688 u16 secclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690
3691 if (kern)
3692 goto out;
3693
David Howells275bb412008-11-14 10:39:19 +11003694 sid = tsec->sid;
3695 newsid = tsec->sockcreate_sid ?: sid;
3696
3697 secclass = socket_type_to_security_class(family, type, protocol);
3698 err = avc_has_perm(sid, newsid, secclass, SOCKET__CREATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699
3700out:
3701 return err;
3702}
3703
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003704static int selinux_socket_post_create(struct socket *sock, int family,
3705 int type, int protocol, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706{
David Howells275bb412008-11-14 10:39:19 +11003707 const struct cred *cred = current_cred();
3708 const struct task_security_struct *tsec = cred->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 struct inode_security_struct *isec;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07003710 struct sk_security_struct *sksec;
David Howells275bb412008-11-14 10:39:19 +11003711 u32 sid, newsid;
3712 int err = 0;
3713
3714 sid = tsec->sid;
3715 newsid = tsec->sockcreate_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716
3717 isec = SOCK_INODE(sock)->i_security;
3718
David Howells275bb412008-11-14 10:39:19 +11003719 if (kern)
3720 isec->sid = SECINITSID_KERNEL;
3721 else if (newsid)
3722 isec->sid = newsid;
3723 else
3724 isec->sid = sid;
3725
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 isec->sclass = socket_type_to_security_class(family, type, protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 isec->initialized = 1;
3728
Venkat Yekkirala892c1412006-08-04 23:08:56 -07003729 if (sock->sk) {
3730 sksec = sock->sk->sk_security;
3731 sksec->sid = isec->sid;
Paul Moore220deb92008-01-29 08:38:23 -05003732 sksec->sclass = isec->sclass;
Paul Moore9f2ad662006-11-17 17:38:53 -05003733 err = selinux_netlbl_socket_post_create(sock);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07003734 }
3735
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003736 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737}
3738
3739/* Range of port numbers used to automatically bind.
3740 Need to determine whether we should perform a name_bind
3741 permission check between the socket and the port number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742
3743static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
3744{
3745 u16 family;
3746 int err;
3747
3748 err = socket_has_perm(current, sock, SOCKET__BIND);
3749 if (err)
3750 goto out;
3751
3752 /*
3753 * If PF_INET or PF_INET6, check name_bind permission for the port.
James Morris13402582005-09-30 14:24:34 -04003754 * Multiple address binding for SCTP is not supported yet: we just
3755 * check the first address now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 */
3757 family = sock->sk->sk_family;
3758 if (family == PF_INET || family == PF_INET6) {
3759 char *addrp;
3760 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 struct avc_audit_data ad;
3762 struct sockaddr_in *addr4 = NULL;
3763 struct sockaddr_in6 *addr6 = NULL;
3764 unsigned short snum;
3765 struct sock *sk = sock->sk;
James Morrise399f982008-06-12 01:39:58 +10003766 u32 sid, node_perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 isec = SOCK_INODE(sock)->i_security;
3769
3770 if (family == PF_INET) {
3771 addr4 = (struct sockaddr_in *)address;
3772 snum = ntohs(addr4->sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 addrp = (char *)&addr4->sin_addr.s_addr;
3774 } else {
3775 addr6 = (struct sockaddr_in6 *)address;
3776 snum = ntohs(addr6->sin6_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 addrp = (char *)&addr6->sin6_addr.s6_addr;
3778 }
3779
Stephen Hemminger227b60f2007-10-10 17:30:46 -07003780 if (snum) {
3781 int low, high;
3782
3783 inet_get_local_port_range(&low, &high);
3784
3785 if (snum < max(PROT_SOCK, low) || snum > high) {
Paul Moore3e112172008-04-10 10:48:14 -04003786 err = sel_netport_sid(sk->sk_protocol,
3787 snum, &sid);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07003788 if (err)
3789 goto out;
Eric Paris828dfe12008-04-17 13:17:49 -04003790 AVC_AUDIT_DATA_INIT(&ad, NET);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07003791 ad.u.net.sport = htons(snum);
3792 ad.u.net.family = family;
3793 err = avc_has_perm(isec->sid, sid,
3794 isec->sclass,
3795 SOCKET__NAME_BIND, &ad);
3796 if (err)
3797 goto out;
3798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 }
Eric Paris828dfe12008-04-17 13:17:49 -04003800
3801 switch (isec->sclass) {
James Morris13402582005-09-30 14:24:34 -04003802 case SECCLASS_TCP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 node_perm = TCP_SOCKET__NODE_BIND;
3804 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003805
James Morris13402582005-09-30 14:24:34 -04003806 case SECCLASS_UDP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 node_perm = UDP_SOCKET__NODE_BIND;
3808 break;
James Morris2ee92d42006-11-13 16:09:01 -08003809
3810 case SECCLASS_DCCP_SOCKET:
3811 node_perm = DCCP_SOCKET__NODE_BIND;
3812 break;
3813
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 default:
3815 node_perm = RAWIP_SOCKET__NODE_BIND;
3816 break;
3817 }
Eric Paris828dfe12008-04-17 13:17:49 -04003818
Paul Moore224dfbd2008-01-29 08:38:13 -05003819 err = sel_netnode_sid(addrp, family, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 if (err)
3821 goto out;
Eric Paris828dfe12008-04-17 13:17:49 -04003822
3823 AVC_AUDIT_DATA_INIT(&ad, NET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 ad.u.net.sport = htons(snum);
3825 ad.u.net.family = family;
3826
3827 if (family == PF_INET)
3828 ad.u.net.v4info.saddr = addr4->sin_addr.s_addr;
3829 else
3830 ipv6_addr_copy(&ad.u.net.v6info.saddr, &addr6->sin6_addr);
3831
3832 err = avc_has_perm(isec->sid, sid,
Eric Paris828dfe12008-04-17 13:17:49 -04003833 isec->sclass, node_perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 if (err)
3835 goto out;
3836 }
3837out:
3838 return err;
3839}
3840
3841static int selinux_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen)
3842{
Paul Moore014ab192008-10-10 10:16:33 -04003843 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 struct inode_security_struct *isec;
3845 int err;
3846
3847 err = socket_has_perm(current, sock, SOCKET__CONNECT);
3848 if (err)
3849 return err;
3850
3851 /*
James Morris2ee92d42006-11-13 16:09:01 -08003852 * If a TCP or DCCP socket, check name_connect permission for the port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 */
3854 isec = SOCK_INODE(sock)->i_security;
James Morris2ee92d42006-11-13 16:09:01 -08003855 if (isec->sclass == SECCLASS_TCP_SOCKET ||
3856 isec->sclass == SECCLASS_DCCP_SOCKET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 struct avc_audit_data ad;
3858 struct sockaddr_in *addr4 = NULL;
3859 struct sockaddr_in6 *addr6 = NULL;
3860 unsigned short snum;
James Morris2ee92d42006-11-13 16:09:01 -08003861 u32 sid, perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862
3863 if (sk->sk_family == PF_INET) {
3864 addr4 = (struct sockaddr_in *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07003865 if (addrlen < sizeof(struct sockaddr_in))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 return -EINVAL;
3867 snum = ntohs(addr4->sin_port);
3868 } else {
3869 addr6 = (struct sockaddr_in6 *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07003870 if (addrlen < SIN6_LEN_RFC2133)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 return -EINVAL;
3872 snum = ntohs(addr6->sin6_port);
3873 }
3874
Paul Moore3e112172008-04-10 10:48:14 -04003875 err = sel_netport_sid(sk->sk_protocol, snum, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 if (err)
3877 goto out;
3878
James Morris2ee92d42006-11-13 16:09:01 -08003879 perm = (isec->sclass == SECCLASS_TCP_SOCKET) ?
3880 TCP_SOCKET__NAME_CONNECT : DCCP_SOCKET__NAME_CONNECT;
3881
Eric Paris828dfe12008-04-17 13:17:49 -04003882 AVC_AUDIT_DATA_INIT(&ad, NET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 ad.u.net.dport = htons(snum);
3884 ad.u.net.family = sk->sk_family;
James Morris2ee92d42006-11-13 16:09:01 -08003885 err = avc_has_perm(isec->sid, sid, isec->sclass, perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 if (err)
3887 goto out;
3888 }
3889
Paul Moore014ab192008-10-10 10:16:33 -04003890 err = selinux_netlbl_socket_connect(sk, address);
3891
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892out:
3893 return err;
3894}
3895
3896static int selinux_socket_listen(struct socket *sock, int backlog)
3897{
3898 return socket_has_perm(current, sock, SOCKET__LISTEN);
3899}
3900
3901static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
3902{
3903 int err;
3904 struct inode_security_struct *isec;
3905 struct inode_security_struct *newisec;
3906
3907 err = socket_has_perm(current, sock, SOCKET__ACCEPT);
3908 if (err)
3909 return err;
3910
3911 newisec = SOCK_INODE(newsock)->i_security;
3912
3913 isec = SOCK_INODE(sock)->i_security;
3914 newisec->sclass = isec->sclass;
3915 newisec->sid = isec->sid;
3916 newisec->initialized = 1;
3917
3918 return 0;
3919}
3920
3921static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
Eric Paris828dfe12008-04-17 13:17:49 -04003922 int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923{
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003924 int rc;
3925
3926 rc = socket_has_perm(current, sock, SOCKET__WRITE);
3927 if (rc)
3928 return rc;
3929
3930 return selinux_netlbl_inode_permission(SOCK_INODE(sock), MAY_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931}
3932
3933static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
3934 int size, int flags)
3935{
3936 return socket_has_perm(current, sock, SOCKET__READ);
3937}
3938
3939static int selinux_socket_getsockname(struct socket *sock)
3940{
3941 return socket_has_perm(current, sock, SOCKET__GETATTR);
3942}
3943
3944static int selinux_socket_getpeername(struct socket *sock)
3945{
3946 return socket_has_perm(current, sock, SOCKET__GETATTR);
3947}
3948
Eric Paris828dfe12008-04-17 13:17:49 -04003949static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950{
Paul Mooref8687af2006-10-30 15:22:15 -08003951 int err;
3952
3953 err = socket_has_perm(current, sock, SOCKET__SETOPT);
3954 if (err)
3955 return err;
3956
3957 return selinux_netlbl_socket_setsockopt(sock, level, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958}
3959
3960static int selinux_socket_getsockopt(struct socket *sock, int level,
3961 int optname)
3962{
3963 return socket_has_perm(current, sock, SOCKET__GETOPT);
3964}
3965
3966static int selinux_socket_shutdown(struct socket *sock, int how)
3967{
3968 return socket_has_perm(current, sock, SOCKET__SHUTDOWN);
3969}
3970
3971static int selinux_socket_unix_stream_connect(struct socket *sock,
3972 struct socket *other,
3973 struct sock *newsk)
3974{
3975 struct sk_security_struct *ssec;
3976 struct inode_security_struct *isec;
3977 struct inode_security_struct *other_isec;
3978 struct avc_audit_data ad;
3979 int err;
3980
3981 err = secondary_ops->unix_stream_connect(sock, other, newsk);
3982 if (err)
3983 return err;
3984
3985 isec = SOCK_INODE(sock)->i_security;
3986 other_isec = SOCK_INODE(other)->i_security;
3987
Eric Paris828dfe12008-04-17 13:17:49 -04003988 AVC_AUDIT_DATA_INIT(&ad, NET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 ad.u.net.sk = other->sk;
3990
3991 err = avc_has_perm(isec->sid, other_isec->sid,
3992 isec->sclass,
3993 UNIX_STREAM_SOCKET__CONNECTTO, &ad);
3994 if (err)
3995 return err;
3996
3997 /* connecting socket */
3998 ssec = sock->sk->sk_security;
3999 ssec->peer_sid = other_isec->sid;
Eric Paris828dfe12008-04-17 13:17:49 -04004000
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 /* server child socket */
4002 ssec = newsk->sk_security;
4003 ssec->peer_sid = isec->sid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004004 err = security_sid_mls_copy(other_isec->sid, ssec->peer_sid, &ssec->sid);
4005
4006 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007}
4008
4009static int selinux_socket_unix_may_send(struct socket *sock,
4010 struct socket *other)
4011{
4012 struct inode_security_struct *isec;
4013 struct inode_security_struct *other_isec;
4014 struct avc_audit_data ad;
4015 int err;
4016
4017 isec = SOCK_INODE(sock)->i_security;
4018 other_isec = SOCK_INODE(other)->i_security;
4019
Eric Paris828dfe12008-04-17 13:17:49 -04004020 AVC_AUDIT_DATA_INIT(&ad, NET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 ad.u.net.sk = other->sk;
4022
4023 err = avc_has_perm(isec->sid, other_isec->sid,
4024 isec->sclass, SOCKET__SENDTO, &ad);
4025 if (err)
4026 return err;
4027
4028 return 0;
4029}
4030
Paul Mooreeffad8d2008-01-29 08:49:27 -05004031static int selinux_inet_sys_rcv_skb(int ifindex, char *addrp, u16 family,
4032 u32 peer_sid,
4033 struct avc_audit_data *ad)
4034{
4035 int err;
4036 u32 if_sid;
4037 u32 node_sid;
4038
4039 err = sel_netif_sid(ifindex, &if_sid);
4040 if (err)
4041 return err;
4042 err = avc_has_perm(peer_sid, if_sid,
4043 SECCLASS_NETIF, NETIF__INGRESS, ad);
4044 if (err)
4045 return err;
4046
4047 err = sel_netnode_sid(addrp, family, &node_sid);
4048 if (err)
4049 return err;
4050 return avc_has_perm(peer_sid, node_sid,
4051 SECCLASS_NODE, NODE__RECVFROM, ad);
4052}
4053
Paul Moore220deb92008-01-29 08:38:23 -05004054static int selinux_sock_rcv_skb_iptables_compat(struct sock *sk,
4055 struct sk_buff *skb,
4056 struct avc_audit_data *ad,
4057 u16 family,
4058 char *addrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059{
Paul Moore220deb92008-01-29 08:38:23 -05004060 int err;
4061 struct sk_security_struct *sksec = sk->sk_security;
4062 u16 sk_class;
4063 u32 netif_perm, node_perm, recv_perm;
4064 u32 port_sid, node_sid, if_sid, sk_sid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004065
Paul Moore220deb92008-01-29 08:38:23 -05004066 sk_sid = sksec->sid;
4067 sk_class = sksec->sclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068
Paul Moore220deb92008-01-29 08:38:23 -05004069 switch (sk_class) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 case SECCLASS_UDP_SOCKET:
4071 netif_perm = NETIF__UDP_RECV;
4072 node_perm = NODE__UDP_RECV;
4073 recv_perm = UDP_SOCKET__RECV_MSG;
4074 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 case SECCLASS_TCP_SOCKET:
4076 netif_perm = NETIF__TCP_RECV;
4077 node_perm = NODE__TCP_RECV;
4078 recv_perm = TCP_SOCKET__RECV_MSG;
4079 break;
James Morris2ee92d42006-11-13 16:09:01 -08004080 case SECCLASS_DCCP_SOCKET:
4081 netif_perm = NETIF__DCCP_RECV;
4082 node_perm = NODE__DCCP_RECV;
4083 recv_perm = DCCP_SOCKET__RECV_MSG;
4084 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 default:
4086 netif_perm = NETIF__RAWIP_RECV;
4087 node_perm = NODE__RAWIP_RECV;
Paul Moore220deb92008-01-29 08:38:23 -05004088 recv_perm = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 break;
4090 }
4091
Paul Moore220deb92008-01-29 08:38:23 -05004092 err = sel_netif_sid(skb->iif, &if_sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 if (err)
Paul Moore220deb92008-01-29 08:38:23 -05004094 return err;
4095 err = avc_has_perm(sk_sid, if_sid, SECCLASS_NETIF, netif_perm, ad);
4096 if (err)
4097 return err;
Eric Paris828dfe12008-04-17 13:17:49 -04004098
Paul Moore224dfbd2008-01-29 08:38:13 -05004099 err = sel_netnode_sid(addrp, family, &node_sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 if (err)
Paul Moore220deb92008-01-29 08:38:23 -05004101 return err;
4102 err = avc_has_perm(sk_sid, node_sid, SECCLASS_NODE, node_perm, ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103 if (err)
Paul Moore220deb92008-01-29 08:38:23 -05004104 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105
Paul Moore220deb92008-01-29 08:38:23 -05004106 if (!recv_perm)
4107 return 0;
Paul Moore3e112172008-04-10 10:48:14 -04004108 err = sel_netport_sid(sk->sk_protocol,
4109 ntohs(ad->u.net.sport), &port_sid);
Paul Moore71f1cb02008-01-29 08:51:16 -05004110 if (unlikely(err)) {
4111 printk(KERN_WARNING
4112 "SELinux: failure in"
4113 " selinux_sock_rcv_skb_iptables_compat(),"
4114 " network port label not found\n");
Paul Moore220deb92008-01-29 08:38:23 -05004115 return err;
Paul Moore71f1cb02008-01-29 08:51:16 -05004116 }
Paul Moore220deb92008-01-29 08:38:23 -05004117 return avc_has_perm(sk_sid, port_sid, sk_class, recv_perm, ad);
4118}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119
Paul Moore220deb92008-01-29 08:38:23 -05004120static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
Paul Moored8395c82008-10-10 10:16:30 -04004121 u16 family)
Paul Moore220deb92008-01-29 08:38:23 -05004122{
4123 int err;
4124 struct sk_security_struct *sksec = sk->sk_security;
4125 u32 peer_sid;
4126 u32 sk_sid = sksec->sid;
Paul Moored8395c82008-10-10 10:16:30 -04004127 struct avc_audit_data ad;
4128 char *addrp;
4129
4130 AVC_AUDIT_DATA_INIT(&ad, NET);
4131 ad.u.net.netif = skb->iif;
4132 ad.u.net.family = family;
4133 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4134 if (err)
4135 return err;
Paul Moore220deb92008-01-29 08:38:23 -05004136
4137 if (selinux_compat_net)
Paul Moored8395c82008-10-10 10:16:30 -04004138 err = selinux_sock_rcv_skb_iptables_compat(sk, skb, &ad,
Paul Moore220deb92008-01-29 08:38:23 -05004139 family, addrp);
4140 else
4141 err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Moored8395c82008-10-10 10:16:30 -04004142 PACKET__RECV, &ad);
Paul Moore220deb92008-01-29 08:38:23 -05004143 if (err)
4144 return err;
4145
4146 if (selinux_policycap_netpeer) {
4147 err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 if (err)
Paul Moore220deb92008-01-29 08:38:23 -05004149 return err;
4150 err = avc_has_perm(sk_sid, peer_sid,
Paul Moored8395c82008-10-10 10:16:30 -04004151 SECCLASS_PEER, PEER__RECV, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04004152 if (err)
4153 selinux_netlbl_err(skb, err, 0);
Paul Moore220deb92008-01-29 08:38:23 -05004154 } else {
Paul Moored8395c82008-10-10 10:16:30 -04004155 err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
Paul Moore220deb92008-01-29 08:38:23 -05004156 if (err)
4157 return err;
Paul Moored8395c82008-10-10 10:16:30 -04004158 err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 }
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004160
James Morris4e5ab4c2006-06-09 00:33:33 -07004161 return err;
4162}
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004163
James Morris4e5ab4c2006-06-09 00:33:33 -07004164static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
4165{
Paul Moore220deb92008-01-29 08:38:23 -05004166 int err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004167 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004168 u16 family = sk->sk_family;
4169 u32 sk_sid = sksec->sid;
Paul Moore220deb92008-01-29 08:38:23 -05004170 struct avc_audit_data ad;
4171 char *addrp;
Paul Moored8395c82008-10-10 10:16:30 -04004172 u8 secmark_active;
4173 u8 peerlbl_active;
James Morris4e5ab4c2006-06-09 00:33:33 -07004174
James Morris4e5ab4c2006-06-09 00:33:33 -07004175 if (family != PF_INET && family != PF_INET6)
Paul Moore220deb92008-01-29 08:38:23 -05004176 return 0;
James Morris4e5ab4c2006-06-09 00:33:33 -07004177
4178 /* Handle mapped IPv4 packets arriving via IPv6 sockets */
Al Viro87fcd702006-12-04 22:00:55 +00004179 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
James Morris4e5ab4c2006-06-09 00:33:33 -07004180 family = PF_INET;
4181
Paul Moored8395c82008-10-10 10:16:30 -04004182 /* If any sort of compatibility mode is enabled then handoff processing
4183 * to the selinux_sock_rcv_skb_compat() function to deal with the
4184 * special handling. We do this in an attempt to keep this function
4185 * as fast and as clean as possible. */
4186 if (selinux_compat_net || !selinux_policycap_netpeer)
4187 return selinux_sock_rcv_skb_compat(sk, skb, family);
4188
4189 secmark_active = selinux_secmark_enabled();
4190 peerlbl_active = netlbl_enabled() || selinux_xfrm_enabled();
4191 if (!secmark_active && !peerlbl_active)
4192 return 0;
4193
James Morris4e5ab4c2006-06-09 00:33:33 -07004194 AVC_AUDIT_DATA_INIT(&ad, NET);
Paul Mooreda5645a2008-01-29 08:38:10 -05004195 ad.u.net.netif = skb->iif;
James Morris4e5ab4c2006-06-09 00:33:33 -07004196 ad.u.net.family = family;
Paul Moore224dfbd2008-01-29 08:38:13 -05004197 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
James Morris4e5ab4c2006-06-09 00:33:33 -07004198 if (err)
Paul Moore220deb92008-01-29 08:38:23 -05004199 return err;
James Morris4e5ab4c2006-06-09 00:33:33 -07004200
Paul Moored8395c82008-10-10 10:16:30 -04004201 if (peerlbl_active) {
Paul Moored621d352008-01-29 08:43:36 -05004202 u32 peer_sid;
4203
4204 err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
4205 if (err)
4206 return err;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004207 err = selinux_inet_sys_rcv_skb(skb->iif, addrp, family,
4208 peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04004209 if (err) {
4210 selinux_netlbl_err(skb, err, 0);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004211 return err;
Paul Mooredfaebe92008-10-10 10:16:31 -04004212 }
Paul Moored621d352008-01-29 08:43:36 -05004213 err = avc_has_perm(sk_sid, peer_sid, SECCLASS_PEER,
4214 PEER__RECV, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04004215 if (err)
4216 selinux_netlbl_err(skb, err, 0);
Paul Moored621d352008-01-29 08:43:36 -05004217 }
4218
Paul Moored8395c82008-10-10 10:16:30 -04004219 if (secmark_active) {
Paul Mooreeffad8d2008-01-29 08:49:27 -05004220 err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
4221 PACKET__RECV, &ad);
4222 if (err)
4223 return err;
4224 }
4225
Paul Moored621d352008-01-29 08:43:36 -05004226 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227}
4228
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004229static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
4230 int __user *optlen, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231{
4232 int err = 0;
4233 char *scontext;
4234 u32 scontext_len;
4235 struct sk_security_struct *ssec;
4236 struct inode_security_struct *isec;
Paul Moore3de4bab2006-11-17 17:38:54 -05004237 u32 peer_sid = SECSID_NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238
4239 isec = SOCK_INODE(sock)->i_security;
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004240
Paul Moore3de4bab2006-11-17 17:38:54 -05004241 if (isec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
4242 isec->sclass == SECCLASS_TCP_SOCKET) {
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004243 ssec = sock->sk->sk_security;
4244 peer_sid = ssec->peer_sid;
4245 }
Paul Moore3de4bab2006-11-17 17:38:54 -05004246 if (peer_sid == SECSID_NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 err = -ENOPROTOOPT;
4248 goto out;
4249 }
4250
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004251 err = security_sid_to_context(peer_sid, &scontext, &scontext_len);
4252
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 if (err)
4254 goto out;
4255
4256 if (scontext_len > len) {
4257 err = -ERANGE;
4258 goto out_len;
4259 }
4260
4261 if (copy_to_user(optval, scontext, scontext_len))
4262 err = -EFAULT;
4263
4264out_len:
4265 if (put_user(scontext_len, optlen))
4266 err = -EFAULT;
4267
4268 kfree(scontext);
Eric Paris828dfe12008-04-17 13:17:49 -04004269out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 return err;
4271}
4272
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004273static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004274{
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004275 u32 peer_secid = SECSID_NULL;
Paul Moore75e22912008-01-29 08:38:04 -05004276 u16 family;
Catherine Zhang877ce7c2006-06-29 12:27:47 -07004277
Paul Mooreaa862902008-10-10 10:16:29 -04004278 if (skb && skb->protocol == htons(ETH_P_IP))
4279 family = PF_INET;
4280 else if (skb && skb->protocol == htons(ETH_P_IPV6))
4281 family = PF_INET6;
4282 else if (sock)
Paul Moore75e22912008-01-29 08:38:04 -05004283 family = sock->sk->sk_family;
Paul Moore75e22912008-01-29 08:38:04 -05004284 else
4285 goto out;
4286
4287 if (sock && family == PF_UNIX)
Ahmed S. Darwish713a04a2008-03-01 21:52:30 +02004288 selinux_inode_getsecid(SOCK_INODE(sock), &peer_secid);
Paul Moore3de4bab2006-11-17 17:38:54 -05004289 else if (skb)
Paul Moore220deb92008-01-29 08:38:23 -05004290 selinux_skb_peerlbl_sid(skb, family, &peer_secid);
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004291
Paul Moore75e22912008-01-29 08:38:04 -05004292out:
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004293 *secid = peer_secid;
Paul Moore75e22912008-01-29 08:38:04 -05004294 if (peer_secid == SECSID_NULL)
4295 return -EINVAL;
4296 return 0;
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004297}
4298
Al Viro7d877f32005-10-21 03:20:43 -04004299static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300{
4301 return sk_alloc_security(sk, family, priority);
4302}
4303
4304static void selinux_sk_free_security(struct sock *sk)
4305{
4306 sk_free_security(sk);
4307}
4308
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004309static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
4310{
4311 struct sk_security_struct *ssec = sk->sk_security;
4312 struct sk_security_struct *newssec = newsk->sk_security;
4313
4314 newssec->sid = ssec->sid;
4315 newssec->peer_sid = ssec->peer_sid;
Paul Moore220deb92008-01-29 08:38:23 -05004316 newssec->sclass = ssec->sclass;
Paul Moore99f59ed2006-08-29 17:53:48 -07004317
Paul Mooref74af6e2008-02-25 11:40:33 -05004318 selinux_netlbl_sk_security_reset(newssec, newsk->sk_family);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004319}
4320
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07004321static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004322{
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004323 if (!sk)
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07004324 *secid = SECINITSID_ANY_SOCKET;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004325 else {
4326 struct sk_security_struct *sksec = sk->sk_security;
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004327
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07004328 *secid = sksec->sid;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004329 }
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004330}
4331
Eric Paris828dfe12008-04-17 13:17:49 -04004332static void selinux_sock_graft(struct sock *sk, struct socket *parent)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004333{
4334 struct inode_security_struct *isec = SOCK_INODE(parent)->i_security;
4335 struct sk_security_struct *sksec = sk->sk_security;
4336
David Woodhouse2148ccc2006-09-29 15:50:25 -07004337 if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
4338 sk->sk_family == PF_UNIX)
4339 isec->sid = sksec->sid;
Paul Moore220deb92008-01-29 08:38:23 -05004340 sksec->sclass = isec->sclass;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004341}
4342
Adrian Bunk9a673e52006-08-15 00:03:53 -07004343static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4344 struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004345{
4346 struct sk_security_struct *sksec = sk->sk_security;
4347 int err;
Paul Mooreaa862902008-10-10 10:16:29 -04004348 u16 family = sk->sk_family;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07004349 u32 newsid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004350 u32 peersid;
4351
Paul Mooreaa862902008-10-10 10:16:29 -04004352 /* handle mapped IPv4 packets arriving via IPv6 sockets */
4353 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4354 family = PF_INET;
4355
4356 err = selinux_skb_peerlbl_sid(skb, family, &peersid);
Paul Moore220deb92008-01-29 08:38:23 -05004357 if (err)
4358 return err;
Venkat Yekkiralaa51c64f2006-07-27 22:01:34 -07004359 if (peersid == SECSID_NULL) {
4360 req->secid = sksec->sid;
Paul Moore3de4bab2006-11-17 17:38:54 -05004361 req->peer_secid = SECSID_NULL;
Venkat Yekkiralaa51c64f2006-07-27 22:01:34 -07004362 return 0;
4363 }
4364
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004365 err = security_sid_mls_copy(sksec->sid, peersid, &newsid);
4366 if (err)
4367 return err;
4368
4369 req->secid = newsid;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004370 req->peer_secid = peersid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004371 return 0;
4372}
4373
Adrian Bunk9a673e52006-08-15 00:03:53 -07004374static void selinux_inet_csk_clone(struct sock *newsk,
4375 const struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004376{
4377 struct sk_security_struct *newsksec = newsk->sk_security;
4378
4379 newsksec->sid = req->secid;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004380 newsksec->peer_sid = req->peer_secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004381 /* NOTE: Ideally, we should also get the isec->sid for the
4382 new socket in sync, but we don't have the isec available yet.
4383 So we will wait until sock_graft to do it, by which
4384 time it will have been created and available. */
Paul Moore99f59ed2006-08-29 17:53:48 -07004385
Paul Moore9f2ad662006-11-17 17:38:53 -05004386 /* We don't need to take any sort of lock here as we are the only
4387 * thread with access to newsksec */
4388 selinux_netlbl_sk_security_reset(newsksec, req->rsk_ops->family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004389}
4390
Paul Moore014ab192008-10-10 10:16:33 -04004391static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004392{
Paul Mooreaa862902008-10-10 10:16:29 -04004393 u16 family = sk->sk_family;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004394 struct sk_security_struct *sksec = sk->sk_security;
4395
Paul Mooreaa862902008-10-10 10:16:29 -04004396 /* handle mapped IPv4 packets arriving via IPv6 sockets */
4397 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4398 family = PF_INET;
4399
4400 selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
Paul Moore014ab192008-10-10 10:16:33 -04004401
4402 selinux_netlbl_inet_conn_established(sk, family);
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004403}
4404
Adrian Bunk9a673e52006-08-15 00:03:53 -07004405static void selinux_req_classify_flow(const struct request_sock *req,
4406 struct flowi *fl)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004407{
4408 fl->secid = req->secid;
4409}
4410
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
4412{
4413 int err = 0;
4414 u32 perm;
4415 struct nlmsghdr *nlh;
4416 struct socket *sock = sk->sk_socket;
4417 struct inode_security_struct *isec = SOCK_INODE(sock)->i_security;
Eric Paris828dfe12008-04-17 13:17:49 -04004418
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419 if (skb->len < NLMSG_SPACE(0)) {
4420 err = -EINVAL;
4421 goto out;
4422 }
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -07004423 nlh = nlmsg_hdr(skb);
Eric Paris828dfe12008-04-17 13:17:49 -04004424
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 err = selinux_nlmsg_lookup(isec->sclass, nlh->nlmsg_type, &perm);
4426 if (err) {
4427 if (err == -EINVAL) {
David Woodhouse9ad9ad32005-06-22 15:04:33 +01004428 audit_log(current->audit_context, GFP_KERNEL, AUDIT_SELINUX_ERR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 "SELinux: unrecognized netlink message"
4430 " type=%hu for sclass=%hu\n",
4431 nlh->nlmsg_type, isec->sclass);
Eric Paris39c9aed2008-11-05 09:34:42 -05004432 if (!selinux_enforcing || security_get_allow_unknown())
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 err = 0;
4434 }
4435
4436 /* Ignore */
4437 if (err == -ENOENT)
4438 err = 0;
4439 goto out;
4440 }
4441
4442 err = socket_has_perm(current, sock, perm);
4443out:
4444 return err;
4445}
4446
4447#ifdef CONFIG_NETFILTER
4448
Paul Mooreeffad8d2008-01-29 08:49:27 -05004449static unsigned int selinux_ip_forward(struct sk_buff *skb, int ifindex,
4450 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451{
Paul Mooredfaebe92008-10-10 10:16:31 -04004452 int err;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004453 char *addrp;
4454 u32 peer_sid;
4455 struct avc_audit_data ad;
4456 u8 secmark_active;
Paul Moore948bf852008-10-10 10:16:32 -04004457 u8 netlbl_active;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004458 u8 peerlbl_active;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004459
Paul Mooreeffad8d2008-01-29 08:49:27 -05004460 if (!selinux_policycap_netpeer)
4461 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004462
Paul Mooreeffad8d2008-01-29 08:49:27 -05004463 secmark_active = selinux_secmark_enabled();
Paul Moore948bf852008-10-10 10:16:32 -04004464 netlbl_active = netlbl_enabled();
4465 peerlbl_active = netlbl_active || selinux_xfrm_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05004466 if (!secmark_active && !peerlbl_active)
4467 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004468
Paul Moored8395c82008-10-10 10:16:30 -04004469 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
4470 return NF_DROP;
4471
Paul Mooreeffad8d2008-01-29 08:49:27 -05004472 AVC_AUDIT_DATA_INIT(&ad, NET);
4473 ad.u.net.netif = ifindex;
4474 ad.u.net.family = family;
4475 if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
4476 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477
Paul Mooredfaebe92008-10-10 10:16:31 -04004478 if (peerlbl_active) {
4479 err = selinux_inet_sys_rcv_skb(ifindex, addrp, family,
4480 peer_sid, &ad);
4481 if (err) {
4482 selinux_netlbl_err(skb, err, 1);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004483 return NF_DROP;
Paul Mooredfaebe92008-10-10 10:16:31 -04004484 }
4485 }
Paul Mooreeffad8d2008-01-29 08:49:27 -05004486
4487 if (secmark_active)
4488 if (avc_has_perm(peer_sid, skb->secmark,
4489 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
4490 return NF_DROP;
4491
Paul Moore948bf852008-10-10 10:16:32 -04004492 if (netlbl_active)
4493 /* we do this in the FORWARD path and not the POST_ROUTING
4494 * path because we want to make sure we apply the necessary
4495 * labeling before IPsec is applied so we can leverage AH
4496 * protection */
4497 if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
4498 return NF_DROP;
4499
Paul Mooreeffad8d2008-01-29 08:49:27 -05004500 return NF_ACCEPT;
4501}
4502
4503static unsigned int selinux_ipv4_forward(unsigned int hooknum,
4504 struct sk_buff *skb,
4505 const struct net_device *in,
4506 const struct net_device *out,
4507 int (*okfn)(struct sk_buff *))
4508{
4509 return selinux_ip_forward(skb, in->ifindex, PF_INET);
4510}
4511
4512#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
4513static unsigned int selinux_ipv6_forward(unsigned int hooknum,
4514 struct sk_buff *skb,
4515 const struct net_device *in,
4516 const struct net_device *out,
4517 int (*okfn)(struct sk_buff *))
4518{
4519 return selinux_ip_forward(skb, in->ifindex, PF_INET6);
4520}
4521#endif /* IPV6 */
4522
Paul Moore948bf852008-10-10 10:16:32 -04004523static unsigned int selinux_ip_output(struct sk_buff *skb,
4524 u16 family)
4525{
4526 u32 sid;
4527
4528 if (!netlbl_enabled())
4529 return NF_ACCEPT;
4530
4531 /* we do this in the LOCAL_OUT path and not the POST_ROUTING path
4532 * because we want to make sure we apply the necessary labeling
4533 * before IPsec is applied so we can leverage AH protection */
4534 if (skb->sk) {
4535 struct sk_security_struct *sksec = skb->sk->sk_security;
4536 sid = sksec->sid;
4537 } else
4538 sid = SECINITSID_KERNEL;
4539 if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
4540 return NF_DROP;
4541
4542 return NF_ACCEPT;
4543}
4544
4545static unsigned int selinux_ipv4_output(unsigned int hooknum,
4546 struct sk_buff *skb,
4547 const struct net_device *in,
4548 const struct net_device *out,
4549 int (*okfn)(struct sk_buff *))
4550{
4551 return selinux_ip_output(skb, PF_INET);
4552}
4553
Paul Mooreeffad8d2008-01-29 08:49:27 -05004554static int selinux_ip_postroute_iptables_compat(struct sock *sk,
4555 int ifindex,
4556 struct avc_audit_data *ad,
4557 u16 family, char *addrp)
4558{
4559 int err;
4560 struct sk_security_struct *sksec = sk->sk_security;
4561 u16 sk_class;
4562 u32 netif_perm, node_perm, send_perm;
4563 u32 port_sid, node_sid, if_sid, sk_sid;
4564
4565 sk_sid = sksec->sid;
4566 sk_class = sksec->sclass;
4567
4568 switch (sk_class) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569 case SECCLASS_UDP_SOCKET:
4570 netif_perm = NETIF__UDP_SEND;
4571 node_perm = NODE__UDP_SEND;
4572 send_perm = UDP_SOCKET__SEND_MSG;
4573 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 case SECCLASS_TCP_SOCKET:
4575 netif_perm = NETIF__TCP_SEND;
4576 node_perm = NODE__TCP_SEND;
4577 send_perm = TCP_SOCKET__SEND_MSG;
4578 break;
James Morris2ee92d42006-11-13 16:09:01 -08004579 case SECCLASS_DCCP_SOCKET:
4580 netif_perm = NETIF__DCCP_SEND;
4581 node_perm = NODE__DCCP_SEND;
4582 send_perm = DCCP_SOCKET__SEND_MSG;
4583 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 default:
4585 netif_perm = NETIF__RAWIP_SEND;
4586 node_perm = NODE__RAWIP_SEND;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004587 send_perm = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 break;
4589 }
4590
Paul Mooreeffad8d2008-01-29 08:49:27 -05004591 err = sel_netif_sid(ifindex, &if_sid);
James Morris4e5ab4c2006-06-09 00:33:33 -07004592 if (err)
Paul Mooreeffad8d2008-01-29 08:49:27 -05004593 return err;
4594 err = avc_has_perm(sk_sid, if_sid, SECCLASS_NETIF, netif_perm, ad);
4595 return err;
Eric Paris828dfe12008-04-17 13:17:49 -04004596
Paul Moore224dfbd2008-01-29 08:38:13 -05004597 err = sel_netnode_sid(addrp, family, &node_sid);
James Morris4e5ab4c2006-06-09 00:33:33 -07004598 if (err)
Paul Mooreeffad8d2008-01-29 08:49:27 -05004599 return err;
4600 err = avc_has_perm(sk_sid, node_sid, SECCLASS_NODE, node_perm, ad);
James Morris4e5ab4c2006-06-09 00:33:33 -07004601 if (err)
Paul Mooreeffad8d2008-01-29 08:49:27 -05004602 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603
Paul Mooreeffad8d2008-01-29 08:49:27 -05004604 if (send_perm != 0)
4605 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606
Paul Moore3e112172008-04-10 10:48:14 -04004607 err = sel_netport_sid(sk->sk_protocol,
4608 ntohs(ad->u.net.dport), &port_sid);
Paul Moore71f1cb02008-01-29 08:51:16 -05004609 if (unlikely(err)) {
4610 printk(KERN_WARNING
4611 "SELinux: failure in"
4612 " selinux_ip_postroute_iptables_compat(),"
4613 " network port label not found\n");
Paul Mooreeffad8d2008-01-29 08:49:27 -05004614 return err;
Paul Moore71f1cb02008-01-29 08:51:16 -05004615 }
Paul Mooreeffad8d2008-01-29 08:49:27 -05004616 return avc_has_perm(sk_sid, port_sid, sk_class, send_perm, ad);
James Morris4e5ab4c2006-06-09 00:33:33 -07004617}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618
Paul Mooreeffad8d2008-01-29 08:49:27 -05004619static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
4620 int ifindex,
Paul Moored8395c82008-10-10 10:16:30 -04004621 u16 family)
James Morris4e5ab4c2006-06-09 00:33:33 -07004622{
Paul Mooreeffad8d2008-01-29 08:49:27 -05004623 struct sock *sk = skb->sk;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004624 struct sk_security_struct *sksec;
Paul Moored8395c82008-10-10 10:16:30 -04004625 struct avc_audit_data ad;
4626 char *addrp;
4627 u8 proto;
James Morris4e5ab4c2006-06-09 00:33:33 -07004628
Paul Mooreeffad8d2008-01-29 08:49:27 -05004629 if (sk == NULL)
4630 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004631 sksec = sk->sk_security;
James Morris4e5ab4c2006-06-09 00:33:33 -07004632
Paul Moored8395c82008-10-10 10:16:30 -04004633 AVC_AUDIT_DATA_INIT(&ad, NET);
4634 ad.u.net.netif = ifindex;
4635 ad.u.net.family = family;
4636 if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
4637 return NF_DROP;
4638
Paul Mooreeffad8d2008-01-29 08:49:27 -05004639 if (selinux_compat_net) {
4640 if (selinux_ip_postroute_iptables_compat(skb->sk, ifindex,
Paul Moored8395c82008-10-10 10:16:30 -04004641 &ad, family, addrp))
Paul Mooreeffad8d2008-01-29 08:49:27 -05004642 return NF_DROP;
4643 } else {
4644 if (avc_has_perm(sksec->sid, skb->secmark,
Paul Moored8395c82008-10-10 10:16:30 -04004645 SECCLASS_PACKET, PACKET__SEND, &ad))
Paul Mooreeffad8d2008-01-29 08:49:27 -05004646 return NF_DROP;
4647 }
James Morris4e5ab4c2006-06-09 00:33:33 -07004648
Paul Mooreeffad8d2008-01-29 08:49:27 -05004649 if (selinux_policycap_netpeer)
Paul Moored8395c82008-10-10 10:16:30 -04004650 if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
Paul Mooreeffad8d2008-01-29 08:49:27 -05004651 return NF_DROP;
James Morris4e5ab4c2006-06-09 00:33:33 -07004652
Paul Mooreeffad8d2008-01-29 08:49:27 -05004653 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654}
4655
Paul Mooreeffad8d2008-01-29 08:49:27 -05004656static unsigned int selinux_ip_postroute(struct sk_buff *skb, int ifindex,
4657 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658{
Paul Mooreeffad8d2008-01-29 08:49:27 -05004659 u32 secmark_perm;
4660 u32 peer_sid;
4661 struct sock *sk;
4662 struct avc_audit_data ad;
4663 char *addrp;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004664 u8 secmark_active;
4665 u8 peerlbl_active;
4666
Paul Mooreeffad8d2008-01-29 08:49:27 -05004667 /* If any sort of compatibility mode is enabled then handoff processing
4668 * to the selinux_ip_postroute_compat() function to deal with the
4669 * special handling. We do this in an attempt to keep this function
4670 * as fast and as clean as possible. */
4671 if (selinux_compat_net || !selinux_policycap_netpeer)
Paul Moored8395c82008-10-10 10:16:30 -04004672 return selinux_ip_postroute_compat(skb, ifindex, family);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004673
4674 /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
4675 * packet transformation so allow the packet to pass without any checks
4676 * since we'll have another chance to perform access control checks
4677 * when the packet is on it's final way out.
4678 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
4679 * is NULL, in this case go ahead and apply access control. */
4680 if (skb->dst != NULL && skb->dst->xfrm != NULL)
4681 return NF_ACCEPT;
4682
4683 secmark_active = selinux_secmark_enabled();
4684 peerlbl_active = netlbl_enabled() || selinux_xfrm_enabled();
4685 if (!secmark_active && !peerlbl_active)
4686 return NF_ACCEPT;
4687
Paul Moored8395c82008-10-10 10:16:30 -04004688 /* if the packet is being forwarded then get the peer label from the
4689 * packet itself; otherwise check to see if it is from a local
4690 * application or the kernel, if from an application get the peer label
4691 * from the sending socket, otherwise use the kernel's sid */
Paul Mooreeffad8d2008-01-29 08:49:27 -05004692 sk = skb->sk;
Paul Moored8395c82008-10-10 10:16:30 -04004693 if (sk == NULL) {
4694 switch (family) {
4695 case PF_INET:
4696 if (IPCB(skb)->flags & IPSKB_FORWARDED)
4697 secmark_perm = PACKET__FORWARD_OUT;
4698 else
4699 secmark_perm = PACKET__SEND;
4700 break;
4701 case PF_INET6:
4702 if (IP6CB(skb)->flags & IP6SKB_FORWARDED)
4703 secmark_perm = PACKET__FORWARD_OUT;
4704 else
4705 secmark_perm = PACKET__SEND;
4706 break;
4707 default:
4708 return NF_DROP;
4709 }
4710 if (secmark_perm == PACKET__FORWARD_OUT) {
4711 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
4712 return NF_DROP;
4713 } else
4714 peer_sid = SECINITSID_KERNEL;
4715 } else {
Paul Mooreeffad8d2008-01-29 08:49:27 -05004716 struct sk_security_struct *sksec = sk->sk_security;
4717 peer_sid = sksec->sid;
4718 secmark_perm = PACKET__SEND;
Paul Mooreeffad8d2008-01-29 08:49:27 -05004719 }
4720
Paul Moored8395c82008-10-10 10:16:30 -04004721 AVC_AUDIT_DATA_INIT(&ad, NET);
4722 ad.u.net.netif = ifindex;
4723 ad.u.net.family = family;
4724 if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
4725 return NF_DROP;
4726
Paul Mooreeffad8d2008-01-29 08:49:27 -05004727 if (secmark_active)
4728 if (avc_has_perm(peer_sid, skb->secmark,
4729 SECCLASS_PACKET, secmark_perm, &ad))
4730 return NF_DROP;
4731
4732 if (peerlbl_active) {
4733 u32 if_sid;
4734 u32 node_sid;
4735
4736 if (sel_netif_sid(ifindex, &if_sid))
4737 return NF_DROP;
4738 if (avc_has_perm(peer_sid, if_sid,
4739 SECCLASS_NETIF, NETIF__EGRESS, &ad))
4740 return NF_DROP;
4741
4742 if (sel_netnode_sid(addrp, family, &node_sid))
4743 return NF_DROP;
4744 if (avc_has_perm(peer_sid, node_sid,
4745 SECCLASS_NODE, NODE__SENDTO, &ad))
4746 return NF_DROP;
4747 }
4748
4749 return NF_ACCEPT;
4750}
4751
4752static unsigned int selinux_ipv4_postroute(unsigned int hooknum,
4753 struct sk_buff *skb,
4754 const struct net_device *in,
4755 const struct net_device *out,
4756 int (*okfn)(struct sk_buff *))
4757{
4758 return selinux_ip_postroute(skb, out->ifindex, PF_INET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759}
4760
4761#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Paul Mooreeffad8d2008-01-29 08:49:27 -05004762static unsigned int selinux_ipv6_postroute(unsigned int hooknum,
4763 struct sk_buff *skb,
4764 const struct net_device *in,
4765 const struct net_device *out,
4766 int (*okfn)(struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767{
Paul Mooreeffad8d2008-01-29 08:49:27 -05004768 return selinux_ip_postroute(skb, out->ifindex, PF_INET6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770#endif /* IPV6 */
4771
4772#endif /* CONFIG_NETFILTER */
4773
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
4775{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776 int err;
4777
4778 err = secondary_ops->netlink_send(sk, skb);
4779 if (err)
4780 return err;
4781
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 if (policydb_loaded_version >= POLICYDB_VERSION_NLCLASS)
4783 err = selinux_nlmsg_perm(sk, skb);
4784
4785 return err;
4786}
4787
Darrel Goeddelc7bdb542006-06-27 13:26:11 -07004788static int selinux_netlink_recv(struct sk_buff *skb, int capability)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789{
Darrel Goeddelc7bdb542006-06-27 13:26:11 -07004790 int err;
4791 struct avc_audit_data ad;
4792
4793 err = secondary_ops->netlink_recv(skb, capability);
4794 if (err)
4795 return err;
4796
4797 AVC_AUDIT_DATA_INIT(&ad, CAP);
4798 ad.u.cap = capability;
4799
4800 return avc_has_perm(NETLINK_CB(skb).sid, NETLINK_CB(skb).sid,
Eric Paris828dfe12008-04-17 13:17:49 -04004801 SECCLASS_CAPABILITY, CAP_TO_MASK(capability), &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802}
4803
4804static int ipc_alloc_security(struct task_struct *task,
4805 struct kern_ipc_perm *perm,
4806 u16 sclass)
4807{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808 struct ipc_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +11004809 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810
James Morris89d155e2005-10-30 14:59:21 -08004811 isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812 if (!isec)
4813 return -ENOMEM;
4814
David Howells275bb412008-11-14 10:39:19 +11004815 sid = task_sid(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816 isec->sclass = sclass;
David Howells275bb412008-11-14 10:39:19 +11004817 isec->sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818 perm->security = isec;
4819
4820 return 0;
4821}
4822
4823static void ipc_free_security(struct kern_ipc_perm *perm)
4824{
4825 struct ipc_security_struct *isec = perm->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826 perm->security = NULL;
4827 kfree(isec);
4828}
4829
4830static int msg_msg_alloc_security(struct msg_msg *msg)
4831{
4832 struct msg_security_struct *msec;
4833
James Morris89d155e2005-10-30 14:59:21 -08004834 msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835 if (!msec)
4836 return -ENOMEM;
4837
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838 msec->sid = SECINITSID_UNLABELED;
4839 msg->security = msec;
4840
4841 return 0;
4842}
4843
4844static void msg_msg_free_security(struct msg_msg *msg)
4845{
4846 struct msg_security_struct *msec = msg->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847
4848 msg->security = NULL;
4849 kfree(msec);
4850}
4851
4852static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
Stephen Smalley6af963f2005-05-01 08:58:39 -07004853 u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 struct ipc_security_struct *isec;
4856 struct avc_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11004857 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859 isec = ipc_perms->security;
4860
4861 AVC_AUDIT_DATA_INIT(&ad, IPC);
4862 ad.u.ipc_id = ipc_perms->key;
4863
David Howells275bb412008-11-14 10:39:19 +11004864 return avc_has_perm(sid, isec->sid, isec->sclass, perms, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865}
4866
4867static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
4868{
4869 return msg_msg_alloc_security(msg);
4870}
4871
4872static void selinux_msg_msg_free_security(struct msg_msg *msg)
4873{
4874 msg_msg_free_security(msg);
4875}
4876
4877/* message queue security operations */
4878static int selinux_msg_queue_alloc_security(struct msg_queue *msq)
4879{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 struct ipc_security_struct *isec;
4881 struct avc_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11004882 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883 int rc;
4884
4885 rc = ipc_alloc_security(current, &msq->q_perm, SECCLASS_MSGQ);
4886 if (rc)
4887 return rc;
4888
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 isec = msq->q_perm.security;
4890
4891 AVC_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris828dfe12008-04-17 13:17:49 -04004892 ad.u.ipc_id = msq->q_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893
David Howells275bb412008-11-14 10:39:19 +11004894 rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 MSGQ__CREATE, &ad);
4896 if (rc) {
4897 ipc_free_security(&msq->q_perm);
4898 return rc;
4899 }
4900 return 0;
4901}
4902
4903static void selinux_msg_queue_free_security(struct msg_queue *msq)
4904{
4905 ipc_free_security(&msq->q_perm);
4906}
4907
4908static int selinux_msg_queue_associate(struct msg_queue *msq, int msqflg)
4909{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910 struct ipc_security_struct *isec;
4911 struct avc_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11004912 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 isec = msq->q_perm.security;
4915
4916 AVC_AUDIT_DATA_INIT(&ad, IPC);
4917 ad.u.ipc_id = msq->q_perm.key;
4918
David Howells275bb412008-11-14 10:39:19 +11004919 return avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920 MSGQ__ASSOCIATE, &ad);
4921}
4922
4923static int selinux_msg_queue_msgctl(struct msg_queue *msq, int cmd)
4924{
4925 int err;
4926 int perms;
4927
Eric Paris828dfe12008-04-17 13:17:49 -04004928 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929 case IPC_INFO:
4930 case MSG_INFO:
4931 /* No specific object, just general system-wide information. */
4932 return task_has_system(current, SYSTEM__IPC_INFO);
4933 case IPC_STAT:
4934 case MSG_STAT:
4935 perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
4936 break;
4937 case IPC_SET:
4938 perms = MSGQ__SETATTR;
4939 break;
4940 case IPC_RMID:
4941 perms = MSGQ__DESTROY;
4942 break;
4943 default:
4944 return 0;
4945 }
4946
Stephen Smalley6af963f2005-05-01 08:58:39 -07004947 err = ipc_has_perm(&msq->q_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948 return err;
4949}
4950
4951static int selinux_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg, int msqflg)
4952{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953 struct ipc_security_struct *isec;
4954 struct msg_security_struct *msec;
4955 struct avc_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11004956 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957 int rc;
4958
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 isec = msq->q_perm.security;
4960 msec = msg->security;
4961
4962 /*
4963 * First time through, need to assign label to the message
4964 */
4965 if (msec->sid == SECINITSID_UNLABELED) {
4966 /*
4967 * Compute new sid based on current process and
4968 * message queue this message will be stored in
4969 */
David Howells275bb412008-11-14 10:39:19 +11004970 rc = security_transition_sid(sid, isec->sid, SECCLASS_MSG,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971 &msec->sid);
4972 if (rc)
4973 return rc;
4974 }
4975
4976 AVC_AUDIT_DATA_INIT(&ad, IPC);
4977 ad.u.ipc_id = msq->q_perm.key;
4978
4979 /* Can this process write to the queue? */
David Howells275bb412008-11-14 10:39:19 +11004980 rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 MSGQ__WRITE, &ad);
4982 if (!rc)
4983 /* Can this process send the message */
David Howells275bb412008-11-14 10:39:19 +11004984 rc = avc_has_perm(sid, msec->sid, SECCLASS_MSG,
4985 MSG__SEND, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 if (!rc)
4987 /* Can the message be put in the queue? */
David Howells275bb412008-11-14 10:39:19 +11004988 rc = avc_has_perm(msec->sid, isec->sid, SECCLASS_MSGQ,
4989 MSGQ__ENQUEUE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990
4991 return rc;
4992}
4993
4994static int selinux_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
4995 struct task_struct *target,
4996 long type, int mode)
4997{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998 struct ipc_security_struct *isec;
4999 struct msg_security_struct *msec;
5000 struct avc_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005001 u32 sid = task_sid(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002 int rc;
5003
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004 isec = msq->q_perm.security;
5005 msec = msg->security;
5006
5007 AVC_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris828dfe12008-04-17 13:17:49 -04005008 ad.u.ipc_id = msq->q_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009
David Howells275bb412008-11-14 10:39:19 +11005010 rc = avc_has_perm(sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011 SECCLASS_MSGQ, MSGQ__READ, &ad);
5012 if (!rc)
David Howells275bb412008-11-14 10:39:19 +11005013 rc = avc_has_perm(sid, msec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 SECCLASS_MSG, MSG__RECEIVE, &ad);
5015 return rc;
5016}
5017
5018/* Shared Memory security operations */
5019static int selinux_shm_alloc_security(struct shmid_kernel *shp)
5020{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021 struct ipc_security_struct *isec;
5022 struct avc_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005023 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024 int rc;
5025
5026 rc = ipc_alloc_security(current, &shp->shm_perm, SECCLASS_SHM);
5027 if (rc)
5028 return rc;
5029
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030 isec = shp->shm_perm.security;
5031
5032 AVC_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris828dfe12008-04-17 13:17:49 -04005033 ad.u.ipc_id = shp->shm_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034
David Howells275bb412008-11-14 10:39:19 +11005035 rc = avc_has_perm(sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036 SHM__CREATE, &ad);
5037 if (rc) {
5038 ipc_free_security(&shp->shm_perm);
5039 return rc;
5040 }
5041 return 0;
5042}
5043
5044static void selinux_shm_free_security(struct shmid_kernel *shp)
5045{
5046 ipc_free_security(&shp->shm_perm);
5047}
5048
5049static int selinux_shm_associate(struct shmid_kernel *shp, int shmflg)
5050{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051 struct ipc_security_struct *isec;
5052 struct avc_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005053 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055 isec = shp->shm_perm.security;
5056
5057 AVC_AUDIT_DATA_INIT(&ad, IPC);
5058 ad.u.ipc_id = shp->shm_perm.key;
5059
David Howells275bb412008-11-14 10:39:19 +11005060 return avc_has_perm(sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061 SHM__ASSOCIATE, &ad);
5062}
5063
5064/* Note, at this point, shp is locked down */
5065static int selinux_shm_shmctl(struct shmid_kernel *shp, int cmd)
5066{
5067 int perms;
5068 int err;
5069
Eric Paris828dfe12008-04-17 13:17:49 -04005070 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071 case IPC_INFO:
5072 case SHM_INFO:
5073 /* No specific object, just general system-wide information. */
5074 return task_has_system(current, SYSTEM__IPC_INFO);
5075 case IPC_STAT:
5076 case SHM_STAT:
5077 perms = SHM__GETATTR | SHM__ASSOCIATE;
5078 break;
5079 case IPC_SET:
5080 perms = SHM__SETATTR;
5081 break;
5082 case SHM_LOCK:
5083 case SHM_UNLOCK:
5084 perms = SHM__LOCK;
5085 break;
5086 case IPC_RMID:
5087 perms = SHM__DESTROY;
5088 break;
5089 default:
5090 return 0;
5091 }
5092
Stephen Smalley6af963f2005-05-01 08:58:39 -07005093 err = ipc_has_perm(&shp->shm_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094 return err;
5095}
5096
5097static int selinux_shm_shmat(struct shmid_kernel *shp,
5098 char __user *shmaddr, int shmflg)
5099{
5100 u32 perms;
5101 int rc;
5102
5103 rc = secondary_ops->shm_shmat(shp, shmaddr, shmflg);
5104 if (rc)
5105 return rc;
5106
5107 if (shmflg & SHM_RDONLY)
5108 perms = SHM__READ;
5109 else
5110 perms = SHM__READ | SHM__WRITE;
5111
Stephen Smalley6af963f2005-05-01 08:58:39 -07005112 return ipc_has_perm(&shp->shm_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113}
5114
5115/* Semaphore security operations */
5116static int selinux_sem_alloc_security(struct sem_array *sma)
5117{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 struct ipc_security_struct *isec;
5119 struct avc_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005120 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121 int rc;
5122
5123 rc = ipc_alloc_security(current, &sma->sem_perm, SECCLASS_SEM);
5124 if (rc)
5125 return rc;
5126
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127 isec = sma->sem_perm.security;
5128
5129 AVC_AUDIT_DATA_INIT(&ad, IPC);
Eric Paris828dfe12008-04-17 13:17:49 -04005130 ad.u.ipc_id = sma->sem_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131
David Howells275bb412008-11-14 10:39:19 +11005132 rc = avc_has_perm(sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133 SEM__CREATE, &ad);
5134 if (rc) {
5135 ipc_free_security(&sma->sem_perm);
5136 return rc;
5137 }
5138 return 0;
5139}
5140
5141static void selinux_sem_free_security(struct sem_array *sma)
5142{
5143 ipc_free_security(&sma->sem_perm);
5144}
5145
5146static int selinux_sem_associate(struct sem_array *sma, int semflg)
5147{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148 struct ipc_security_struct *isec;
5149 struct avc_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005150 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152 isec = sma->sem_perm.security;
5153
5154 AVC_AUDIT_DATA_INIT(&ad, IPC);
5155 ad.u.ipc_id = sma->sem_perm.key;
5156
David Howells275bb412008-11-14 10:39:19 +11005157 return avc_has_perm(sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 SEM__ASSOCIATE, &ad);
5159}
5160
5161/* Note, at this point, sma is locked down */
5162static int selinux_sem_semctl(struct sem_array *sma, int cmd)
5163{
5164 int err;
5165 u32 perms;
5166
Eric Paris828dfe12008-04-17 13:17:49 -04005167 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168 case IPC_INFO:
5169 case SEM_INFO:
5170 /* No specific object, just general system-wide information. */
5171 return task_has_system(current, SYSTEM__IPC_INFO);
5172 case GETPID:
5173 case GETNCNT:
5174 case GETZCNT:
5175 perms = SEM__GETATTR;
5176 break;
5177 case GETVAL:
5178 case GETALL:
5179 perms = SEM__READ;
5180 break;
5181 case SETVAL:
5182 case SETALL:
5183 perms = SEM__WRITE;
5184 break;
5185 case IPC_RMID:
5186 perms = SEM__DESTROY;
5187 break;
5188 case IPC_SET:
5189 perms = SEM__SETATTR;
5190 break;
5191 case IPC_STAT:
5192 case SEM_STAT:
5193 perms = SEM__GETATTR | SEM__ASSOCIATE;
5194 break;
5195 default:
5196 return 0;
5197 }
5198
Stephen Smalley6af963f2005-05-01 08:58:39 -07005199 err = ipc_has_perm(&sma->sem_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200 return err;
5201}
5202
5203static int selinux_sem_semop(struct sem_array *sma,
5204 struct sembuf *sops, unsigned nsops, int alter)
5205{
5206 u32 perms;
5207
5208 if (alter)
5209 perms = SEM__READ | SEM__WRITE;
5210 else
5211 perms = SEM__READ;
5212
Stephen Smalley6af963f2005-05-01 08:58:39 -07005213 return ipc_has_perm(&sma->sem_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214}
5215
5216static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
5217{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218 u32 av = 0;
5219
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220 av = 0;
5221 if (flag & S_IRUGO)
5222 av |= IPC__UNIX_READ;
5223 if (flag & S_IWUGO)
5224 av |= IPC__UNIX_WRITE;
5225
5226 if (av == 0)
5227 return 0;
5228
Stephen Smalley6af963f2005-05-01 08:58:39 -07005229 return ipc_has_perm(ipcp, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230}
5231
Ahmed S. Darwish713a04a2008-03-01 21:52:30 +02005232static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
5233{
5234 struct ipc_security_struct *isec = ipcp->security;
5235 *secid = isec->sid;
5236}
5237
Eric Paris828dfe12008-04-17 13:17:49 -04005238static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005239{
5240 if (inode)
5241 inode_doinit_with_dentry(inode, dentry);
5242}
5243
5244static int selinux_getprocattr(struct task_struct *p,
Al Viro04ff9702007-03-12 16:17:58 +00005245 char *name, char **value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246{
David Howells275bb412008-11-14 10:39:19 +11005247 const struct task_security_struct *__tsec;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00005248 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249 int error;
Al Viro04ff9702007-03-12 16:17:58 +00005250 unsigned len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251
5252 if (current != p) {
5253 error = task_has_perm(current, p, PROCESS__GETATTR);
5254 if (error)
5255 return error;
5256 }
5257
David Howells275bb412008-11-14 10:39:19 +11005258 rcu_read_lock();
5259 __tsec = __task_cred(p)->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260
5261 if (!strcmp(name, "current"))
David Howells275bb412008-11-14 10:39:19 +11005262 sid = __tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 else if (!strcmp(name, "prev"))
David Howells275bb412008-11-14 10:39:19 +11005264 sid = __tsec->osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265 else if (!strcmp(name, "exec"))
David Howells275bb412008-11-14 10:39:19 +11005266 sid = __tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267 else if (!strcmp(name, "fscreate"))
David Howells275bb412008-11-14 10:39:19 +11005268 sid = __tsec->create_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07005269 else if (!strcmp(name, "keycreate"))
David Howells275bb412008-11-14 10:39:19 +11005270 sid = __tsec->keycreate_sid;
Eric Paris42c3e032006-06-26 00:26:03 -07005271 else if (!strcmp(name, "sockcreate"))
David Howells275bb412008-11-14 10:39:19 +11005272 sid = __tsec->sockcreate_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273 else
David Howells275bb412008-11-14 10:39:19 +11005274 goto invalid;
5275 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276
5277 if (!sid)
5278 return 0;
5279
Al Viro04ff9702007-03-12 16:17:58 +00005280 error = security_sid_to_context(sid, value, &len);
5281 if (error)
5282 return error;
5283 return len;
David Howells275bb412008-11-14 10:39:19 +11005284
5285invalid:
5286 rcu_read_unlock();
5287 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005288}
5289
5290static int selinux_setprocattr(struct task_struct *p,
5291 char *name, void *value, size_t size)
5292{
5293 struct task_security_struct *tsec;
Roland McGrath03563572008-03-26 15:46:39 -07005294 struct task_struct *tracer;
David Howellsd84f4f92008-11-14 10:39:23 +11005295 struct cred *new;
5296 u32 sid = 0, ptsid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297 int error;
5298 char *str = value;
5299
5300 if (current != p) {
5301 /* SELinux only allows a process to change its own
5302 security attributes. */
5303 return -EACCES;
5304 }
5305
5306 /*
5307 * Basic control over ability to set these attributes at all.
5308 * current == p, but we'll pass them separately in case the
5309 * above restriction is ever removed.
5310 */
5311 if (!strcmp(name, "exec"))
5312 error = task_has_perm(current, p, PROCESS__SETEXEC);
5313 else if (!strcmp(name, "fscreate"))
5314 error = task_has_perm(current, p, PROCESS__SETFSCREATE);
Michael LeMay4eb582c2006-06-26 00:24:57 -07005315 else if (!strcmp(name, "keycreate"))
5316 error = task_has_perm(current, p, PROCESS__SETKEYCREATE);
Eric Paris42c3e032006-06-26 00:26:03 -07005317 else if (!strcmp(name, "sockcreate"))
5318 error = task_has_perm(current, p, PROCESS__SETSOCKCREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319 else if (!strcmp(name, "current"))
5320 error = task_has_perm(current, p, PROCESS__SETCURRENT);
5321 else
5322 error = -EINVAL;
5323 if (error)
5324 return error;
5325
5326 /* Obtain a SID for the context, if one was specified. */
5327 if (size && str[1] && str[1] != '\n') {
5328 if (str[size-1] == '\n') {
5329 str[size-1] = 0;
5330 size--;
5331 }
5332 error = security_context_to_sid(value, size, &sid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04005333 if (error == -EINVAL && !strcmp(name, "fscreate")) {
5334 if (!capable(CAP_MAC_ADMIN))
5335 return error;
5336 error = security_context_to_sid_force(value, size,
5337 &sid);
5338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339 if (error)
5340 return error;
5341 }
5342
David Howellsd84f4f92008-11-14 10:39:23 +11005343 new = prepare_creds();
5344 if (!new)
5345 return -ENOMEM;
5346
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347 /* Permission checking based on the specified context is
5348 performed during the actual operation (execve,
5349 open/mkdir/...), when we know the full context of the
David Howellsd84f4f92008-11-14 10:39:23 +11005350 operation. See selinux_bprm_set_creds for the execve
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351 checks and may_create for the file creation checks. The
5352 operation will then fail if the context is not permitted. */
David Howellsd84f4f92008-11-14 10:39:23 +11005353 tsec = new->security;
5354 if (!strcmp(name, "exec")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355 tsec->exec_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005356 } else if (!strcmp(name, "fscreate")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 tsec->create_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005358 } else if (!strcmp(name, "keycreate")) {
Michael LeMay4eb582c2006-06-26 00:24:57 -07005359 error = may_create_key(sid, p);
5360 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11005361 goto abort_change;
Michael LeMay4eb582c2006-06-26 00:24:57 -07005362 tsec->keycreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005363 } else if (!strcmp(name, "sockcreate")) {
Eric Paris42c3e032006-06-26 00:26:03 -07005364 tsec->sockcreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005365 } else if (!strcmp(name, "current")) {
5366 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367 if (sid == 0)
David Howellsd84f4f92008-11-14 10:39:23 +11005368 goto abort_change;
KaiGai Koheid9250de2008-08-28 16:35:57 +09005369
David Howellsd84f4f92008-11-14 10:39:23 +11005370 /* Only allow single threaded processes to change context */
5371 error = -EPERM;
5372 if (!is_single_threaded(p)) {
5373 error = security_bounded_transition(tsec->sid, sid);
5374 if (error)
5375 goto abort_change;
Eric Paris828dfe12008-04-17 13:17:49 -04005376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377
5378 /* Check permissions for the transition. */
5379 error = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS,
Eric Paris828dfe12008-04-17 13:17:49 -04005380 PROCESS__DYNTRANSITION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11005382 goto abort_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383
5384 /* Check for ptracing, and update the task SID if ok.
5385 Otherwise, leave SID unchanged and fail. */
David Howellsd84f4f92008-11-14 10:39:23 +11005386 ptsid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387 task_lock(p);
Roland McGrath0d094ef2008-07-25 19:45:49 -07005388 tracer = tracehook_tracer_task(p);
David Howellsd84f4f92008-11-14 10:39:23 +11005389 if (tracer)
5390 ptsid = task_sid(tracer);
5391 task_unlock(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392
David Howellsd84f4f92008-11-14 10:39:23 +11005393 if (tracer) {
5394 error = avc_has_perm(ptsid, sid, SECCLASS_PROCESS,
5395 PROCESS__PTRACE, NULL);
5396 if (error)
5397 goto abort_change;
5398 }
5399
5400 tsec->sid = sid;
5401 } else {
5402 error = -EINVAL;
5403 goto abort_change;
5404 }
5405
5406 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407 return size;
David Howellsd84f4f92008-11-14 10:39:23 +11005408
5409abort_change:
5410 abort_creds(new);
5411 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412}
5413
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005414static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
5415{
5416 return security_sid_to_context(secid, secdata, seclen);
5417}
5418
David Howells7bf570d2008-04-29 20:52:51 +01005419static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
David Howells63cb3442008-01-15 23:47:35 +00005420{
5421 return security_context_to_sid(secdata, seclen, secid);
5422}
5423
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005424static void selinux_release_secctx(char *secdata, u32 seclen)
5425{
Paul Moore088999e2007-08-01 11:12:58 -04005426 kfree(secdata);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005427}
5428
Michael LeMayd7200242006-06-22 14:47:17 -07005429#ifdef CONFIG_KEYS
5430
David Howellsd84f4f92008-11-14 10:39:23 +11005431static int selinux_key_alloc(struct key *k, const struct cred *cred,
David Howells7e047ef2006-06-26 00:24:50 -07005432 unsigned long flags)
Michael LeMayd7200242006-06-22 14:47:17 -07005433{
David Howellsd84f4f92008-11-14 10:39:23 +11005434 const struct task_security_struct *tsec;
Michael LeMayd7200242006-06-22 14:47:17 -07005435 struct key_security_struct *ksec;
5436
5437 ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
5438 if (!ksec)
5439 return -ENOMEM;
5440
David Howellsd84f4f92008-11-14 10:39:23 +11005441 tsec = cred->security;
5442 if (tsec->keycreate_sid)
5443 ksec->sid = tsec->keycreate_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07005444 else
David Howellsd84f4f92008-11-14 10:39:23 +11005445 ksec->sid = tsec->sid;
Michael LeMayd7200242006-06-22 14:47:17 -07005446
David Howells275bb412008-11-14 10:39:19 +11005447 k->security = ksec;
Michael LeMayd7200242006-06-22 14:47:17 -07005448 return 0;
5449}
5450
5451static void selinux_key_free(struct key *k)
5452{
5453 struct key_security_struct *ksec = k->security;
5454
5455 k->security = NULL;
5456 kfree(ksec);
5457}
5458
5459static int selinux_key_permission(key_ref_t key_ref,
David Howellsd84f4f92008-11-14 10:39:23 +11005460 const struct cred *cred,
5461 key_perm_t perm)
Michael LeMayd7200242006-06-22 14:47:17 -07005462{
5463 struct key *key;
Michael LeMayd7200242006-06-22 14:47:17 -07005464 struct key_security_struct *ksec;
David Howells275bb412008-11-14 10:39:19 +11005465 u32 sid;
Michael LeMayd7200242006-06-22 14:47:17 -07005466
5467 /* if no specific permissions are requested, we skip the
5468 permission check. No serious, additional covert channels
5469 appear to be created. */
5470 if (perm == 0)
5471 return 0;
5472
David Howellsd84f4f92008-11-14 10:39:23 +11005473 sid = cred_sid(cred);
David Howells275bb412008-11-14 10:39:19 +11005474
5475 key = key_ref_to_ptr(key_ref);
5476 ksec = key->security;
5477
5478 return avc_has_perm(sid, ksec->sid, SECCLASS_KEY, perm, NULL);
Michael LeMayd7200242006-06-22 14:47:17 -07005479}
5480
David Howells70a5bb72008-04-29 01:01:26 -07005481static int selinux_key_getsecurity(struct key *key, char **_buffer)
5482{
5483 struct key_security_struct *ksec = key->security;
5484 char *context = NULL;
5485 unsigned len;
5486 int rc;
5487
5488 rc = security_sid_to_context(ksec->sid, &context, &len);
5489 if (!rc)
5490 rc = len;
5491 *_buffer = context;
5492 return rc;
5493}
5494
Michael LeMayd7200242006-06-22 14:47:17 -07005495#endif
5496
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497static struct security_operations selinux_ops = {
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02005498 .name = "selinux",
5499
David Howells5cd9c582008-08-14 11:37:28 +01005500 .ptrace_may_access = selinux_ptrace_may_access,
5501 .ptrace_traceme = selinux_ptrace_traceme,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 .capget = selinux_capget,
David Howellsd84f4f92008-11-14 10:39:23 +11005503 .capset = selinux_capset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504 .sysctl = selinux_sysctl,
5505 .capable = selinux_capable,
5506 .quotactl = selinux_quotactl,
5507 .quota_on = selinux_quota_on,
5508 .syslog = selinux_syslog,
5509 .vm_enough_memory = selinux_vm_enough_memory,
5510
5511 .netlink_send = selinux_netlink_send,
Eric Paris828dfe12008-04-17 13:17:49 -04005512 .netlink_recv = selinux_netlink_recv,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513
David Howellsa6f76f22008-11-14 10:39:24 +11005514 .bprm_set_creds = selinux_bprm_set_creds,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515 .bprm_check_security = selinux_bprm_check_security,
David Howellsa6f76f22008-11-14 10:39:24 +11005516 .bprm_committing_creds = selinux_bprm_committing_creds,
5517 .bprm_committed_creds = selinux_bprm_committed_creds,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518 .bprm_secureexec = selinux_bprm_secureexec,
5519
5520 .sb_alloc_security = selinux_sb_alloc_security,
5521 .sb_free_security = selinux_sb_free_security,
5522 .sb_copy_data = selinux_sb_copy_data,
Eric Paris828dfe12008-04-17 13:17:49 -04005523 .sb_kern_mount = selinux_sb_kern_mount,
Eric Paris2069f452008-07-04 09:47:13 +10005524 .sb_show_options = selinux_sb_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525 .sb_statfs = selinux_sb_statfs,
5526 .sb_mount = selinux_mount,
5527 .sb_umount = selinux_umount,
Eric Parisc9180a52007-11-30 13:00:35 -05005528 .sb_set_mnt_opts = selinux_set_mnt_opts,
Eric Paris828dfe12008-04-17 13:17:49 -04005529 .sb_clone_mnt_opts = selinux_sb_clone_mnt_opts,
Eric Parise0007522008-03-05 10:31:54 -05005530 .sb_parse_opts_str = selinux_parse_opts_str,
5531
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532
5533 .inode_alloc_security = selinux_inode_alloc_security,
5534 .inode_free_security = selinux_inode_free_security,
Stephen Smalley5e41ff92005-09-09 13:01:35 -07005535 .inode_init_security = selinux_inode_init_security,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536 .inode_create = selinux_inode_create,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537 .inode_link = selinux_inode_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538 .inode_unlink = selinux_inode_unlink,
5539 .inode_symlink = selinux_inode_symlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540 .inode_mkdir = selinux_inode_mkdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541 .inode_rmdir = selinux_inode_rmdir,
5542 .inode_mknod = selinux_inode_mknod,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543 .inode_rename = selinux_inode_rename,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544 .inode_readlink = selinux_inode_readlink,
5545 .inode_follow_link = selinux_inode_follow_link,
5546 .inode_permission = selinux_inode_permission,
5547 .inode_setattr = selinux_inode_setattr,
5548 .inode_getattr = selinux_inode_getattr,
5549 .inode_setxattr = selinux_inode_setxattr,
5550 .inode_post_setxattr = selinux_inode_post_setxattr,
5551 .inode_getxattr = selinux_inode_getxattr,
5552 .inode_listxattr = selinux_inode_listxattr,
5553 .inode_removexattr = selinux_inode_removexattr,
Eric Paris828dfe12008-04-17 13:17:49 -04005554 .inode_getsecurity = selinux_inode_getsecurity,
5555 .inode_setsecurity = selinux_inode_setsecurity,
5556 .inode_listsecurity = selinux_inode_listsecurity,
Serge E. Hallynb5376772007-10-16 23:31:36 -07005557 .inode_need_killpriv = selinux_inode_need_killpriv,
5558 .inode_killpriv = selinux_inode_killpriv,
Eric Parisf5269712008-05-14 11:27:45 -04005559 .inode_getsecid = selinux_inode_getsecid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005560
5561 .file_permission = selinux_file_permission,
5562 .file_alloc_security = selinux_file_alloc_security,
5563 .file_free_security = selinux_file_free_security,
5564 .file_ioctl = selinux_file_ioctl,
5565 .file_mmap = selinux_file_mmap,
5566 .file_mprotect = selinux_file_mprotect,
5567 .file_lock = selinux_file_lock,
5568 .file_fcntl = selinux_file_fcntl,
5569 .file_set_fowner = selinux_file_set_fowner,
5570 .file_send_sigiotask = selinux_file_send_sigiotask,
5571 .file_receive = selinux_file_receive,
5572
Eric Paris828dfe12008-04-17 13:17:49 -04005573 .dentry_open = selinux_dentry_open,
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09005574
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575 .task_create = selinux_task_create,
David Howellsf1752ee2008-11-14 10:39:17 +11005576 .cred_free = selinux_cred_free,
David Howellsd84f4f92008-11-14 10:39:23 +11005577 .cred_prepare = selinux_cred_prepare,
5578 .cred_commit = selinux_cred_commit,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579 .task_setuid = selinux_task_setuid,
David Howellsd84f4f92008-11-14 10:39:23 +11005580 .task_fix_setuid = selinux_task_fix_setuid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581 .task_setgid = selinux_task_setgid,
5582 .task_setpgid = selinux_task_setpgid,
5583 .task_getpgid = selinux_task_getpgid,
Eric Paris828dfe12008-04-17 13:17:49 -04005584 .task_getsid = selinux_task_getsid,
David Quigleyf9008e42006-06-30 01:55:46 -07005585 .task_getsecid = selinux_task_getsecid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586 .task_setgroups = selinux_task_setgroups,
5587 .task_setnice = selinux_task_setnice,
James Morris03e68062006-06-23 02:03:58 -07005588 .task_setioprio = selinux_task_setioprio,
David Quigleya1836a42006-06-30 01:55:49 -07005589 .task_getioprio = selinux_task_getioprio,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590 .task_setrlimit = selinux_task_setrlimit,
5591 .task_setscheduler = selinux_task_setscheduler,
5592 .task_getscheduler = selinux_task_getscheduler,
David Quigley35601542006-06-23 02:04:01 -07005593 .task_movememory = selinux_task_movememory,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594 .task_kill = selinux_task_kill,
5595 .task_wait = selinux_task_wait,
5596 .task_prctl = selinux_task_prctl,
Eric Paris828dfe12008-04-17 13:17:49 -04005597 .task_to_inode = selinux_task_to_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598
5599 .ipc_permission = selinux_ipc_permission,
Eric Parisf5269712008-05-14 11:27:45 -04005600 .ipc_getsecid = selinux_ipc_getsecid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601
5602 .msg_msg_alloc_security = selinux_msg_msg_alloc_security,
5603 .msg_msg_free_security = selinux_msg_msg_free_security,
5604
5605 .msg_queue_alloc_security = selinux_msg_queue_alloc_security,
5606 .msg_queue_free_security = selinux_msg_queue_free_security,
5607 .msg_queue_associate = selinux_msg_queue_associate,
5608 .msg_queue_msgctl = selinux_msg_queue_msgctl,
5609 .msg_queue_msgsnd = selinux_msg_queue_msgsnd,
5610 .msg_queue_msgrcv = selinux_msg_queue_msgrcv,
5611
5612 .shm_alloc_security = selinux_shm_alloc_security,
5613 .shm_free_security = selinux_shm_free_security,
5614 .shm_associate = selinux_shm_associate,
5615 .shm_shmctl = selinux_shm_shmctl,
5616 .shm_shmat = selinux_shm_shmat,
5617
Eric Paris828dfe12008-04-17 13:17:49 -04005618 .sem_alloc_security = selinux_sem_alloc_security,
5619 .sem_free_security = selinux_sem_free_security,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620 .sem_associate = selinux_sem_associate,
5621 .sem_semctl = selinux_sem_semctl,
5622 .sem_semop = selinux_sem_semop,
5623
Eric Paris828dfe12008-04-17 13:17:49 -04005624 .d_instantiate = selinux_d_instantiate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625
Eric Paris828dfe12008-04-17 13:17:49 -04005626 .getprocattr = selinux_getprocattr,
5627 .setprocattr = selinux_setprocattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005629 .secid_to_secctx = selinux_secid_to_secctx,
David Howells63cb3442008-01-15 23:47:35 +00005630 .secctx_to_secid = selinux_secctx_to_secid,
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005631 .release_secctx = selinux_release_secctx,
5632
Eric Paris828dfe12008-04-17 13:17:49 -04005633 .unix_stream_connect = selinux_socket_unix_stream_connect,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634 .unix_may_send = selinux_socket_unix_may_send,
5635
5636 .socket_create = selinux_socket_create,
5637 .socket_post_create = selinux_socket_post_create,
5638 .socket_bind = selinux_socket_bind,
5639 .socket_connect = selinux_socket_connect,
5640 .socket_listen = selinux_socket_listen,
5641 .socket_accept = selinux_socket_accept,
5642 .socket_sendmsg = selinux_socket_sendmsg,
5643 .socket_recvmsg = selinux_socket_recvmsg,
5644 .socket_getsockname = selinux_socket_getsockname,
5645 .socket_getpeername = selinux_socket_getpeername,
5646 .socket_getsockopt = selinux_socket_getsockopt,
5647 .socket_setsockopt = selinux_socket_setsockopt,
5648 .socket_shutdown = selinux_socket_shutdown,
5649 .socket_sock_rcv_skb = selinux_socket_sock_rcv_skb,
Catherine Zhang2c7946a2006-03-20 22:41:23 -08005650 .socket_getpeersec_stream = selinux_socket_getpeersec_stream,
5651 .socket_getpeersec_dgram = selinux_socket_getpeersec_dgram,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652 .sk_alloc_security = selinux_sk_alloc_security,
5653 .sk_free_security = selinux_sk_free_security,
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005654 .sk_clone_security = selinux_sk_clone_security,
Eric Paris828dfe12008-04-17 13:17:49 -04005655 .sk_getsecid = selinux_sk_getsecid,
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005656 .sock_graft = selinux_sock_graft,
5657 .inet_conn_request = selinux_inet_conn_request,
5658 .inet_csk_clone = selinux_inet_csk_clone,
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005659 .inet_conn_established = selinux_inet_conn_established,
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005660 .req_classify_flow = selinux_req_classify_flow,
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005661
5662#ifdef CONFIG_SECURITY_NETWORK_XFRM
5663 .xfrm_policy_alloc_security = selinux_xfrm_policy_alloc,
5664 .xfrm_policy_clone_security = selinux_xfrm_policy_clone,
5665 .xfrm_policy_free_security = selinux_xfrm_policy_free,
Catherine Zhangc8c05a82006-06-08 23:39:49 -07005666 .xfrm_policy_delete_security = selinux_xfrm_policy_delete,
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005667 .xfrm_state_alloc_security = selinux_xfrm_state_alloc,
5668 .xfrm_state_free_security = selinux_xfrm_state_free,
Catherine Zhangc8c05a82006-06-08 23:39:49 -07005669 .xfrm_state_delete_security = selinux_xfrm_state_delete,
Eric Paris828dfe12008-04-17 13:17:49 -04005670 .xfrm_policy_lookup = selinux_xfrm_policy_lookup,
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07005671 .xfrm_state_pol_flow_match = selinux_xfrm_state_pol_flow_match,
Venkat Yekkiralae0d1caa2006-07-24 23:29:07 -07005672 .xfrm_decode_session = selinux_xfrm_decode_session,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673#endif
Michael LeMayd7200242006-06-22 14:47:17 -07005674
5675#ifdef CONFIG_KEYS
Eric Paris828dfe12008-04-17 13:17:49 -04005676 .key_alloc = selinux_key_alloc,
5677 .key_free = selinux_key_free,
5678 .key_permission = selinux_key_permission,
David Howells70a5bb72008-04-29 01:01:26 -07005679 .key_getsecurity = selinux_key_getsecurity,
Michael LeMayd7200242006-06-22 14:47:17 -07005680#endif
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02005681
5682#ifdef CONFIG_AUDIT
5683 .audit_rule_init = selinux_audit_rule_init,
5684 .audit_rule_known = selinux_audit_rule_known,
5685 .audit_rule_match = selinux_audit_rule_match,
5686 .audit_rule_free = selinux_audit_rule_free,
5687#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688};
5689
5690static __init int selinux_init(void)
5691{
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02005692 if (!security_module_enable(&selinux_ops)) {
5693 selinux_enabled = 0;
5694 return 0;
5695 }
5696
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697 if (!selinux_enabled) {
5698 printk(KERN_INFO "SELinux: Disabled at boot.\n");
5699 return 0;
5700 }
5701
5702 printk(KERN_INFO "SELinux: Initializing.\n");
5703
5704 /* Set the security state for the initial task. */
David Howellsd84f4f92008-11-14 10:39:23 +11005705 cred_init_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706
James Morris7cae7e22006-03-22 00:09:22 -08005707 sel_inode_cache = kmem_cache_create("selinux_inode_security",
5708 sizeof(struct inode_security_struct),
Paul Mundt20c2df82007-07-20 10:11:58 +09005709 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710 avc_init();
5711
James Morris6f0f0fd2008-07-10 17:02:07 +09005712 secondary_ops = security_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713 if (!secondary_ops)
Eric Paris828dfe12008-04-17 13:17:49 -04005714 panic("SELinux: No initial security operations\n");
5715 if (register_security(&selinux_ops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716 panic("SELinux: Unable to register with kernel.\n");
5717
Eric Paris828dfe12008-04-17 13:17:49 -04005718 if (selinux_enforcing)
Eric Parisfadcdb42007-02-22 18:11:31 -05005719 printk(KERN_DEBUG "SELinux: Starting in enforcing mode\n");
Eric Paris828dfe12008-04-17 13:17:49 -04005720 else
Eric Parisfadcdb42007-02-22 18:11:31 -05005721 printk(KERN_DEBUG "SELinux: Starting in permissive mode\n");
Michael LeMayd7200242006-06-22 14:47:17 -07005722
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723 return 0;
5724}
5725
5726void selinux_complete_init(void)
5727{
Eric Parisfadcdb42007-02-22 18:11:31 -05005728 printk(KERN_DEBUG "SELinux: Completing initialization.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729
5730 /* Set up any superblocks initialized prior to the policy load. */
Eric Parisfadcdb42007-02-22 18:11:31 -05005731 printk(KERN_DEBUG "SELinux: Setting up existing superblocks.\n");
Stephen Smalleyba0c19e2006-06-04 02:51:30 -07005732 spin_lock(&sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005733 spin_lock(&sb_security_lock);
5734next_sb:
5735 if (!list_empty(&superblock_security_head)) {
5736 struct superblock_security_struct *sbsec =
5737 list_entry(superblock_security_head.next,
Eric Paris828dfe12008-04-17 13:17:49 -04005738 struct superblock_security_struct,
5739 list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740 struct super_block *sb = sbsec->sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741 sb->s_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742 spin_unlock(&sb_security_lock);
Stephen Smalleyba0c19e2006-06-04 02:51:30 -07005743 spin_unlock(&sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744 down_read(&sb->s_umount);
5745 if (sb->s_root)
5746 superblock_doinit(sb, NULL);
5747 drop_super(sb);
Stephen Smalleyba0c19e2006-06-04 02:51:30 -07005748 spin_lock(&sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749 spin_lock(&sb_security_lock);
5750 list_del_init(&sbsec->list);
5751 goto next_sb;
5752 }
5753 spin_unlock(&sb_security_lock);
Stephen Smalleyba0c19e2006-06-04 02:51:30 -07005754 spin_unlock(&sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755}
5756
5757/* SELinux requires early initialization in order to label
5758 all processes and objects when they are created. */
5759security_initcall(selinux_init);
5760
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08005761#if defined(CONFIG_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005762
Paul Mooreeffad8d2008-01-29 08:49:27 -05005763static struct nf_hook_ops selinux_ipv4_ops[] = {
5764 {
5765 .hook = selinux_ipv4_postroute,
5766 .owner = THIS_MODULE,
5767 .pf = PF_INET,
5768 .hooknum = NF_INET_POST_ROUTING,
5769 .priority = NF_IP_PRI_SELINUX_LAST,
5770 },
5771 {
5772 .hook = selinux_ipv4_forward,
5773 .owner = THIS_MODULE,
5774 .pf = PF_INET,
5775 .hooknum = NF_INET_FORWARD,
5776 .priority = NF_IP_PRI_SELINUX_FIRST,
Paul Moore948bf852008-10-10 10:16:32 -04005777 },
5778 {
5779 .hook = selinux_ipv4_output,
5780 .owner = THIS_MODULE,
5781 .pf = PF_INET,
5782 .hooknum = NF_INET_LOCAL_OUT,
5783 .priority = NF_IP_PRI_SELINUX_FIRST,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005785};
5786
5787#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
5788
Paul Mooreeffad8d2008-01-29 08:49:27 -05005789static struct nf_hook_ops selinux_ipv6_ops[] = {
5790 {
5791 .hook = selinux_ipv6_postroute,
5792 .owner = THIS_MODULE,
5793 .pf = PF_INET6,
5794 .hooknum = NF_INET_POST_ROUTING,
5795 .priority = NF_IP6_PRI_SELINUX_LAST,
5796 },
5797 {
5798 .hook = selinux_ipv6_forward,
5799 .owner = THIS_MODULE,
5800 .pf = PF_INET6,
5801 .hooknum = NF_INET_FORWARD,
5802 .priority = NF_IP6_PRI_SELINUX_FIRST,
5803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804};
5805
5806#endif /* IPV6 */
5807
5808static int __init selinux_nf_ip_init(void)
5809{
5810 int err = 0;
5811
5812 if (!selinux_enabled)
5813 goto out;
Eric Parisfadcdb42007-02-22 18:11:31 -05005814
5815 printk(KERN_DEBUG "SELinux: Registering netfilter hooks\n");
5816
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07005817 err = nf_register_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops));
5818 if (err)
5819 panic("SELinux: nf_register_hooks for IPv4: error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820
5821#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07005822 err = nf_register_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops));
5823 if (err)
5824 panic("SELinux: nf_register_hooks for IPv6: error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825#endif /* IPV6 */
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005826
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827out:
5828 return err;
5829}
5830
5831__initcall(selinux_nf_ip_init);
5832
5833#ifdef CONFIG_SECURITY_SELINUX_DISABLE
5834static void selinux_nf_ip_exit(void)
5835{
Eric Parisfadcdb42007-02-22 18:11:31 -05005836 printk(KERN_DEBUG "SELinux: Unregistering netfilter hooks\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07005838 nf_unregister_hooks(selinux_ipv4_ops, ARRAY_SIZE(selinux_ipv4_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07005840 nf_unregister_hooks(selinux_ipv6_ops, ARRAY_SIZE(selinux_ipv6_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841#endif /* IPV6 */
5842}
5843#endif
5844
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08005845#else /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846
5847#ifdef CONFIG_SECURITY_SELINUX_DISABLE
5848#define selinux_nf_ip_exit()
5849#endif
5850
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08005851#endif /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005852
5853#ifdef CONFIG_SECURITY_SELINUX_DISABLE
Eric Paris828dfe12008-04-17 13:17:49 -04005854static int selinux_disabled;
5855
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856int selinux_disable(void)
5857{
5858 extern void exit_sel_fs(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859
5860 if (ss_initialized) {
5861 /* Not permitted after initial policy load. */
5862 return -EINVAL;
5863 }
5864
5865 if (selinux_disabled) {
5866 /* Only do this once. */
5867 return -EINVAL;
5868 }
5869
5870 printk(KERN_INFO "SELinux: Disabled at runtime.\n");
5871
5872 selinux_disabled = 1;
Stephen Smalley30d55282006-05-03 10:52:36 -04005873 selinux_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874
5875 /* Reset security_ops to the secondary module, dummy or capability. */
5876 security_ops = secondary_ops;
5877
5878 /* Unregister netfilter hooks. */
5879 selinux_nf_ip_exit();
5880
5881 /* Unregister selinuxfs. */
5882 exit_sel_fs();
5883
5884 return 0;
5885}
5886#endif