blob: 6581b288e2af9d8c6bdf89d202e2dbed853aeaa6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * NSA Security-Enhanced Linux (SELinux) security module
3 *
4 * This file contains the SELinux hook function implementations.
5 *
6 * Authors: Stephen Smalley, <sds@epoch.ncsc.mil>
Eric Paris828dfe12008-04-17 13:17:49 -04007 * Chris Vance, <cvance@nai.com>
8 * Wayne Salamon, <wsalamon@nai.com>
9 * James Morris <jmorris@redhat.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * Copyright (C) 2001,2002 Networks Associates Technology, Inc.
Eric Paris2069f452008-07-04 09:47:13 +100012 * Copyright (C) 2003-2008 Red Hat, Inc., James Morris <jmorris@redhat.com>
13 * Eric Paris <eparis@redhat.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
Eric Paris828dfe12008-04-17 13:17:49 -040015 * <dgoeddel@trustedcs.com>
Paul Mooreed6d76e2009-08-28 18:12:49 -040016 * Copyright (C) 2006, 2007, 2009 Hewlett-Packard Development Company, L.P.
Paul Moore82c21bf2011-08-01 11:10:33 +000017 * Paul Moore <paul@paul-moore.com>
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +090018 * Copyright (C) 2007 Hitachi Software Engineering Co., Ltd.
Eric Paris828dfe12008-04-17 13:17:49 -040019 * Yuichi Nakamura <ynakam@hitachisoft.jp>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 *
21 * This program is free software; you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License version 2,
Eric Paris828dfe12008-04-17 13:17:49 -040023 * as published by the Free Software Foundation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 */
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/init.h>
Eric Paris0b24dcb2011-02-25 15:39:20 -050027#include <linux/kd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/kernel.h>
Roland McGrath0d094ef2008-07-25 19:45:49 -070029#include <linux/tracehook.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/errno.h>
31#include <linux/sched.h>
Casey Schaufler3c4ed7b2015-05-02 15:10:46 -070032#include <linux/lsm_hooks.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/xattr.h>
34#include <linux/capability.h>
35#include <linux/unistd.h>
36#include <linux/mm.h>
37#include <linux/mman.h>
38#include <linux/slab.h>
39#include <linux/pagemap.h>
Eric Paris0b24dcb2011-02-25 15:39:20 -050040#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/swap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/spinlock.h>
43#include <linux/syscalls.h>
Eric Paris2a7dba32011-02-01 11:05:39 -050044#include <linux/dcache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/file.h>
Al Viro9f3acc32008-04-24 07:44:08 -040046#include <linux/fdtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/namei.h>
48#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/netfilter_ipv4.h>
50#include <linux/netfilter_ipv6.h>
51#include <linux/tty.h>
52#include <net/icmp.h>
Stephen Hemminger227b60f2007-10-10 17:30:46 -070053#include <net/ip.h> /* for local_port_range[] */
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <net/tcp.h> /* struct or_callable used in sock_rcv_skb */
Paul Moore47180062013-12-04 16:10:45 -050055#include <net/inet_connection_sock.h>
Paul Moore220deb92008-01-29 08:38:23 -050056#include <net/net_namespace.h>
Paul Moored621d352008-01-29 08:43:36 -050057#include <net/netlabel.h>
Eric Parisf5269712008-05-14 11:27:45 -040058#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <asm/ioctls.h>
Arun Sharma600634972011-07-26 16:09:06 -070060#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/bitops.h>
62#include <linux/interrupt.h>
63#include <linux/netdevice.h> /* for network interface checks */
Hong zhi guo77954982013-03-27 06:49:35 +000064#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <linux/tcp.h>
66#include <linux/udp.h>
James Morris2ee92d42006-11-13 16:09:01 -080067#include <linux/dccp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/quota.h>
69#include <linux/un.h> /* for Unix socket types */
70#include <net/af_unix.h> /* for Unix socket types */
71#include <linux/parser.h>
72#include <linux/nfs_mount.h>
73#include <net/ipv6.h>
74#include <linux/hugetlb.h>
75#include <linux/personality.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include <linux/audit.h>
Eric Paris6931dfc2005-06-30 02:58:51 -070077#include <linux/string.h>
Catherine Zhang877ce7c2006-06-29 12:27:47 -070078#include <linux/selinux.h>
Eric Paris23970742006-09-25 23:32:01 -070079#include <linux/mutex.h>
Frank Mayharf06febc2008-09-12 09:54:39 -070080#include <linux/posix-timers.h>
Kees Cook00234592010-02-03 15:36:43 -080081#include <linux/syslog.h>
Serge E. Hallyn34867402011-03-23 16:43:17 -070082#include <linux/user_namespace.h>
Paul Gortmaker44fc7ea2011-05-26 20:52:10 -040083#include <linux/export.h>
Al Viro40401532012-02-13 03:58:52 +000084#include <linux/msg.h>
85#include <linux/shm.h>
Chenbo Feng9b629132017-10-18 13:00:25 -070086#include <linux/bpf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88#include "avc.h"
89#include "objsec.h"
90#include "netif.h"
Paul Moore224dfbd2008-01-29 08:38:13 -050091#include "netnode.h"
Paul Moore3e112172008-04-10 10:48:14 -040092#include "netport.h"
Trent Jaegerd28d1e02005-12-13 23:12:40 -080093#include "xfrm.h"
Paul Moorec60475b2007-02-28 15:14:23 -050094#include "netlabel.h"
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +020095#include "audit.h"
James Morris7b98a582011-08-30 12:52:32 +100096#include "avc_ss.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Paul Moored621d352008-01-29 08:43:36 -050098/* SECMARK reference count */
James Morris56a4ca92011-08-17 11:08:43 +100099static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0);
Paul Moored621d352008-01-29 08:43:36 -0500100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#ifdef CONFIG_SECURITY_SELINUX_DEVELOP
Zhen Kongc58c5612017-12-20 14:49:59 -0800102int selinux_enforcing __aligned(0x1000) __attribute__((section(".bss_rtic")));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104static int __init enforcing_setup(char *str)
105{
Eric Parisf5269712008-05-14 11:27:45 -0400106 unsigned long enforcing;
Jingoo Han29707b22014-02-05 15:13:14 +0900107 if (!kstrtoul(str, 0, &enforcing))
Eric Parisf5269712008-05-14 11:27:45 -0400108 selinux_enforcing = enforcing ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 return 1;
110}
111__setup("enforcing=", enforcing_setup);
112#endif
113
114#ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM
115int selinux_enabled = CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE;
116
117static int __init selinux_enabled_setup(char *str)
118{
Eric Parisf5269712008-05-14 11:27:45 -0400119 unsigned long enabled;
Jingoo Han29707b22014-02-05 15:13:14 +0900120 if (!kstrtoul(str, 0, &enabled))
Eric Parisf5269712008-05-14 11:27:45 -0400121 selinux_enabled = enabled ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 return 1;
123}
124__setup("selinux=", selinux_enabled_setup);
Stephen Smalley30d55282006-05-03 10:52:36 -0400125#else
126int selinux_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127#endif
128
Christoph Lametere18b8902006-12-06 20:33:20 -0800129static struct kmem_cache *sel_inode_cache;
Sangwoo63205652015-10-21 17:44:30 -0400130static struct kmem_cache *file_security_cache;
James Morris7cae7e22006-03-22 00:09:22 -0800131
Paul Moored621d352008-01-29 08:43:36 -0500132/**
133 * selinux_secmark_enabled - Check to see if SECMARK is currently enabled
134 *
135 * Description:
136 * This function checks the SECMARK reference counter to see if any SECMARK
137 * targets are currently configured, if the reference counter is greater than
138 * zero SECMARK is considered to be enabled. Returns true (1) if SECMARK is
Chris PeBenito2be4d742013-05-03 09:05:39 -0400139 * enabled, false (0) if SECMARK is disabled. If the always_check_network
140 * policy capability is enabled, SECMARK is always considered enabled.
Paul Moored621d352008-01-29 08:43:36 -0500141 *
142 */
143static int selinux_secmark_enabled(void)
144{
Chris PeBenito2be4d742013-05-03 09:05:39 -0400145 return (selinux_policycap_alwaysnetwork || atomic_read(&selinux_secmark_refcount));
146}
147
148/**
149 * selinux_peerlbl_enabled - Check to see if peer labeling is currently enabled
150 *
151 * Description:
152 * This function checks if NetLabel or labeled IPSEC is enabled. Returns true
153 * (1) if any are enabled or false (0) if neither are enabled. If the
154 * always_check_network policy capability is enabled, peer labeling
155 * is always considered enabled.
156 *
157 */
158static int selinux_peerlbl_enabled(void)
159{
160 return (selinux_policycap_alwaysnetwork || netlbl_enabled() || selinux_xfrm_enabled());
Paul Moored621d352008-01-29 08:43:36 -0500161}
162
Paul Moore615e51f2014-06-26 14:33:56 -0400163static int selinux_netcache_avc_callback(u32 event)
164{
165 if (event == AVC_CALLBACK_RESET) {
166 sel_netif_flush();
167 sel_netnode_flush();
168 sel_netport_flush();
169 synchronize_net();
170 }
171 return 0;
172}
173
David Howellsd84f4f92008-11-14 10:39:23 +1100174/*
175 * initialise the security for the init task
176 */
177static void cred_init_security(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
David Howells3b11a1d2008-11-14 10:39:26 +1100179 struct cred *cred = (struct cred *) current->real_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 struct task_security_struct *tsec;
181
James Morris89d155e2005-10-30 14:59:21 -0800182 tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 if (!tsec)
David Howellsd84f4f92008-11-14 10:39:23 +1100184 panic("SELinux: Failed to initialize initial task.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
David Howellsd84f4f92008-11-14 10:39:23 +1100186 tsec->osid = tsec->sid = SECINITSID_KERNEL;
David Howellsf1752ee2008-11-14 10:39:17 +1100187 cred->security = tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
David Howells275bb412008-11-14 10:39:19 +1100190/*
David Howells88e67f32008-11-14 10:39:21 +1100191 * get the security ID of a set of credentials
192 */
193static inline u32 cred_sid(const struct cred *cred)
194{
195 const struct task_security_struct *tsec;
196
197 tsec = cred->security;
198 return tsec->sid;
199}
200
201/*
David Howells3b11a1d2008-11-14 10:39:26 +1100202 * get the objective security ID of a task
David Howells275bb412008-11-14 10:39:19 +1100203 */
204static inline u32 task_sid(const struct task_struct *task)
205{
David Howells275bb412008-11-14 10:39:19 +1100206 u32 sid;
207
208 rcu_read_lock();
David Howells88e67f32008-11-14 10:39:21 +1100209 sid = cred_sid(__task_cred(task));
David Howells275bb412008-11-14 10:39:19 +1100210 rcu_read_unlock();
211 return sid;
212}
213
214/*
David Howells3b11a1d2008-11-14 10:39:26 +1100215 * get the subjective security ID of the current task
David Howells275bb412008-11-14 10:39:19 +1100216 */
217static inline u32 current_sid(void)
218{
Paul Moore5fb49872010-04-22 14:46:19 -0400219 const struct task_security_struct *tsec = current_security();
David Howells275bb412008-11-14 10:39:19 +1100220
221 return tsec->sid;
222}
223
David Howells88e67f32008-11-14 10:39:21 +1100224/* Allocate and free functions for each kind of security blob. */
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226static int inode_alloc_security(struct inode *inode)
227{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 struct inode_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +1100229 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Josef Bacika02fe132008-04-04 09:35:05 +1100231 isec = kmem_cache_zalloc(sel_inode_cache, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 if (!isec)
233 return -ENOMEM;
234
Eric Paris23970742006-09-25 23:32:01 -0700235 mutex_init(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 INIT_LIST_HEAD(&isec->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 isec->inode = inode;
238 isec->sid = SECINITSID_UNLABELED;
239 isec->sclass = SECCLASS_FILE;
David Howells275bb412008-11-14 10:39:19 +1100240 isec->task_sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 inode->i_security = isec;
242
243 return 0;
244}
245
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500246static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry);
247
248/*
249 * Try reloading inode security labels that have been marked as invalid. The
250 * @may_sleep parameter indicates when sleeping and thus reloading labels is
251 * allowed; when set to false, returns ERR_PTR(-ECHILD) when the label is
252 * invalid. The @opt_dentry parameter should be set to a dentry of the inode;
253 * when no dentry is available, set it to NULL instead.
254 */
255static int __inode_security_revalidate(struct inode *inode,
256 struct dentry *opt_dentry,
257 bool may_sleep)
258{
259 struct inode_security_struct *isec = inode->i_security;
260
261 might_sleep_if(may_sleep);
262
Paul Moore1ac424762016-04-18 16:41:38 -0400263 if (ss_initialized && isec->initialized != LABEL_INITIALIZED) {
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500264 if (!may_sleep)
265 return -ECHILD;
266
267 /*
268 * Try reloading the inode security label. This will fail if
269 * @opt_dentry is NULL and no dentry for this inode can be
270 * found; in that case, continue using the old label.
271 */
272 inode_doinit_with_dentry(inode, opt_dentry);
273 }
274 return 0;
275}
276
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500277static struct inode_security_struct *inode_security_novalidate(struct inode *inode)
278{
279 return inode->i_security;
280}
281
282static struct inode_security_struct *inode_security_rcu(struct inode *inode, bool rcu)
283{
284 int error;
285
286 error = __inode_security_revalidate(inode, NULL, !rcu);
287 if (error)
288 return ERR_PTR(error);
289 return inode->i_security;
290}
291
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500292/*
293 * Get the security label of an inode.
294 */
295static struct inode_security_struct *inode_security(struct inode *inode)
296{
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500297 __inode_security_revalidate(inode, NULL, true);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500298 return inode->i_security;
299}
300
Paul Moore2c971652016-04-19 16:36:28 -0400301static struct inode_security_struct *backing_inode_security_novalidate(struct dentry *dentry)
302{
303 struct inode *inode = d_backing_inode(dentry);
304
305 return inode->i_security;
306}
307
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500308/*
309 * Get the security label of a dentry's backing inode.
310 */
311static struct inode_security_struct *backing_inode_security(struct dentry *dentry)
312{
313 struct inode *inode = d_backing_inode(dentry);
314
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500315 __inode_security_revalidate(inode, dentry, true);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500316 return inode->i_security;
317}
318
Steven Rostedt3dc91d42014-01-09 21:46:34 -0500319static void inode_free_rcu(struct rcu_head *head)
320{
321 struct inode_security_struct *isec;
322
323 isec = container_of(head, struct inode_security_struct, rcu);
324 kmem_cache_free(sel_inode_cache, isec);
325}
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327static void inode_free_security(struct inode *inode)
328{
329 struct inode_security_struct *isec = inode->i_security;
330 struct superblock_security_struct *sbsec = inode->i_sb->s_security;
331
Waiman Long9629d042015-07-10 17:19:56 -0400332 /*
333 * As not all inode security structures are in a list, we check for
334 * empty list outside of the lock to make sure that we won't waste
335 * time taking a lock doing nothing.
336 *
337 * The list_del_init() function can be safely called more than once.
338 * It should not be possible for this function to be called with
339 * concurrent list_add(), but for better safety against future changes
340 * in the code, we use list_empty_careful() here.
341 */
342 if (!list_empty_careful(&isec->list)) {
343 spin_lock(&sbsec->isec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 list_del_init(&isec->list);
Waiman Long9629d042015-07-10 17:19:56 -0400345 spin_unlock(&sbsec->isec_lock);
346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Steven Rostedt3dc91d42014-01-09 21:46:34 -0500348 /*
349 * The inode may still be referenced in a path walk and
350 * a call to selinux_inode_permission() can be made
351 * after inode_free_security() is called. Ideally, the VFS
352 * wouldn't do this, but fixing that is a much harder
353 * job. For now, simply free the i_security via RCU, and
354 * leave the current inode->i_security pointer intact.
355 * The inode will be freed after the RCU grace period too.
356 */
357 call_rcu(&isec->rcu, inode_free_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
359
360static int file_alloc_security(struct file *file)
361{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 struct file_security_struct *fsec;
David Howells275bb412008-11-14 10:39:19 +1100363 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Sangwoo63205652015-10-21 17:44:30 -0400365 fsec = kmem_cache_zalloc(file_security_cache, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if (!fsec)
367 return -ENOMEM;
368
David Howells275bb412008-11-14 10:39:19 +1100369 fsec->sid = sid;
370 fsec->fown_sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 file->f_security = fsec;
372
373 return 0;
374}
375
376static void file_free_security(struct file *file)
377{
378 struct file_security_struct *fsec = file->f_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 file->f_security = NULL;
Sangwoo63205652015-10-21 17:44:30 -0400380 kmem_cache_free(file_security_cache, fsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381}
382
383static int superblock_alloc_security(struct super_block *sb)
384{
385 struct superblock_security_struct *sbsec;
386
James Morris89d155e2005-10-30 14:59:21 -0800387 sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 if (!sbsec)
389 return -ENOMEM;
390
Eric Parisbc7e9822006-09-25 23:32:02 -0700391 mutex_init(&sbsec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 INIT_LIST_HEAD(&sbsec->isec_head);
393 spin_lock_init(&sbsec->isec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 sbsec->sb = sb;
395 sbsec->sid = SECINITSID_UNLABELED;
396 sbsec->def_sid = SECINITSID_FILE;
Eric Parisc312feb2006-07-10 04:43:53 -0700397 sbsec->mntpoint_sid = SECINITSID_UNLABELED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 sb->s_security = sbsec;
399
400 return 0;
401}
402
403static void superblock_free_security(struct super_block *sb)
404{
405 struct superblock_security_struct *sbsec = sb->s_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 sb->s_security = NULL;
407 kfree(sbsec);
408}
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410static inline int inode_doinit(struct inode *inode)
411{
412 return inode_doinit_with_dentry(inode, NULL);
413}
414
415enum {
Eric Paris31e87932007-09-19 17:19:12 -0400416 Opt_error = -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 Opt_context = 1,
418 Opt_fscontext = 2,
Eric Parisc9180a52007-11-30 13:00:35 -0500419 Opt_defcontext = 3,
420 Opt_rootcontext = 4,
David P. Quigley11689d42009-01-16 09:22:03 -0500421 Opt_labelsupport = 5,
Eric Parisd355987f2012-08-24 15:58:53 -0400422 Opt_nextmntopt = 6,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423};
424
Eric Parisd355987f2012-08-24 15:58:53 -0400425#define NUM_SEL_MNT_OPTS (Opt_nextmntopt - 1)
426
Steven Whitehousea447c092008-10-13 10:46:57 +0100427static const match_table_t tokens = {
Eric Paris832cbd92008-04-01 13:24:09 -0400428 {Opt_context, CONTEXT_STR "%s"},
429 {Opt_fscontext, FSCONTEXT_STR "%s"},
430 {Opt_defcontext, DEFCONTEXT_STR "%s"},
431 {Opt_rootcontext, ROOTCONTEXT_STR "%s"},
David P. Quigley11689d42009-01-16 09:22:03 -0500432 {Opt_labelsupport, LABELSUPP_STR},
Eric Paris31e87932007-09-19 17:19:12 -0400433 {Opt_error, NULL},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434};
435
436#define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n"
437
Eric Parisc312feb2006-07-10 04:43:53 -0700438static int may_context_mount_sb_relabel(u32 sid,
439 struct superblock_security_struct *sbsec,
David Howells275bb412008-11-14 10:39:19 +1100440 const struct cred *cred)
Eric Parisc312feb2006-07-10 04:43:53 -0700441{
David Howells275bb412008-11-14 10:39:19 +1100442 const struct task_security_struct *tsec = cred->security;
Eric Parisc312feb2006-07-10 04:43:53 -0700443 int rc;
444
445 rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
446 FILESYSTEM__RELABELFROM, NULL);
447 if (rc)
448 return rc;
449
450 rc = avc_has_perm(tsec->sid, sid, SECCLASS_FILESYSTEM,
451 FILESYSTEM__RELABELTO, NULL);
452 return rc;
453}
454
Eric Paris08089252006-07-10 04:43:55 -0700455static int may_context_mount_inode_relabel(u32 sid,
456 struct superblock_security_struct *sbsec,
David Howells275bb412008-11-14 10:39:19 +1100457 const struct cred *cred)
Eric Paris08089252006-07-10 04:43:55 -0700458{
David Howells275bb412008-11-14 10:39:19 +1100459 const struct task_security_struct *tsec = cred->security;
Eric Paris08089252006-07-10 04:43:55 -0700460 int rc;
461 rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
462 FILESYSTEM__RELABELFROM, NULL);
463 if (rc)
464 return rc;
465
466 rc = avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM,
467 FILESYSTEM__ASSOCIATE, NULL);
468 return rc;
469}
470
Ondrej Mosnacek869d1e42018-12-21 21:18:52 +0100471static int selinux_is_genfs_special_handling(struct super_block *sb)
Eric Parisb43e7252012-10-10 14:27:35 -0400472{
Ondrej Mosnacek869d1e42018-12-21 21:18:52 +0100473 /* Special handling. Genfs but also in-core setxattr handler */
474 return !strcmp(sb->s_type->name, "sysfs") ||
Mark Salyzynd5f3a5f2015-02-04 11:34:30 -0500475 !strcmp(sb->s_type->name, "pstore") ||
476 !strcmp(sb->s_type->name, "debugfs") ||
Yongqin Liu93f1d0b2016-04-28 13:53:36 +0800477 !strcmp(sb->s_type->name, "tracefs") ||
Mark Salyzynd5f3a5f2015-02-04 11:34:30 -0500478 !strcmp(sb->s_type->name, "rootfs");
Eric Parisb43e7252012-10-10 14:27:35 -0400479}
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481static int selinux_is_sblabel_mnt(struct super_block *sb)
482{
483 struct superblock_security_struct *sbsec = sb->s_security;
484
Ondrej Mosnacek869d1e42018-12-21 21:18:52 +0100485 /*
486 * IMPORTANT: Double-check logic in this function when adding a new
487 * SECURITY_FS_USE_* definition!
488 */
489 BUILD_BUG_ON(SECURITY_FS_USE_MAX != 7);
490
491 switch (sbsec->behavior) {
492 case SECURITY_FS_USE_XATTR:
493 case SECURITY_FS_USE_TRANS:
494 case SECURITY_FS_USE_TASK:
495 case SECURITY_FS_USE_NATIVE:
496 return 1;
497
498 case SECURITY_FS_USE_GENFS:
499 return selinux_is_genfs_special_handling(sb);
500
501 /* Never allow relabeling on context mounts */
502 case SECURITY_FS_USE_MNTPOINT:
503 case SECURITY_FS_USE_NONE:
504 default:
505 return 0;
506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
508
Eric Parisc9180a52007-11-30 13:00:35 -0500509static int sb_finish_set_opts(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
511 struct superblock_security_struct *sbsec = sb->s_security;
512 struct dentry *root = sb->s_root;
David Howellsc6f493d2015-03-17 22:26:22 +0000513 struct inode *root_inode = d_backing_inode(root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 int rc = 0;
515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
517 /* Make sure that the xattr handler exists and that no
518 error other than -ENODATA is returned by getxattr on
519 the root directory. -ENODATA is ok, as this may be
520 the first boot of the SELinux kernel before we have
521 assigned xattr values to the filesystem. */
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200522 if (!(root_inode->i_opflags & IOP_XATTR)) {
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800523 printk(KERN_WARNING "SELinux: (dev %s, type %s) has no "
524 "xattr support\n", sb->s_id, sb->s_type->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 rc = -EOPNOTSUPP;
526 goto out;
527 }
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200528
529 rc = __vfs_getxattr(root, root_inode, XATTR_NAME_SELINUX, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 if (rc < 0 && rc != -ENODATA) {
531 if (rc == -EOPNOTSUPP)
532 printk(KERN_WARNING "SELinux: (dev %s, type "
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800533 "%s) has no security xattr handler\n",
534 sb->s_id, sb->s_type->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 else
536 printk(KERN_WARNING "SELinux: (dev %s, type "
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800537 "%s) getxattr errno %d\n", sb->s_id,
538 sb->s_type->name, -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 goto out;
540 }
541 }
542
Eric Pariseadcabc2012-08-24 15:59:14 -0400543 sbsec->flags |= SE_SBINITIALIZED;
Eric Parisb43e7252012-10-10 14:27:35 -0400544 if (selinux_is_sblabel_mnt(sb))
Eric Paris12f348b2012-10-09 10:56:25 -0400545 sbsec->flags |= SBLABEL_MNT;
David P. Quigleyddd29ec2009-09-09 14:25:37 -0400546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 /* Initialize the root inode. */
Eric Parisc9180a52007-11-30 13:00:35 -0500548 rc = inode_doinit_with_dentry(root_inode, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 /* Initialize any other inodes associated with the superblock, e.g.
551 inodes created prior to initial policy load or inodes created
552 during get_sb by a pseudo filesystem that directly
553 populates itself. */
554 spin_lock(&sbsec->isec_lock);
555next_inode:
556 if (!list_empty(&sbsec->isec_head)) {
557 struct inode_security_struct *isec =
558 list_entry(sbsec->isec_head.next,
Eric Parisc9180a52007-11-30 13:00:35 -0500559 struct inode_security_struct, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 struct inode *inode = isec->inode;
Stephen Smalley923190d2014-10-06 16:32:52 -0400561 list_del_init(&isec->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 spin_unlock(&sbsec->isec_lock);
563 inode = igrab(inode);
564 if (inode) {
Eric Parisc9180a52007-11-30 13:00:35 -0500565 if (!IS_PRIVATE(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 inode_doinit(inode);
567 iput(inode);
568 }
569 spin_lock(&sbsec->isec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 goto next_inode;
571 }
572 spin_unlock(&sbsec->isec_lock);
573out:
Eric Parisc9180a52007-11-30 13:00:35 -0500574 return rc;
575}
576
577/*
578 * This function should allow an FS to ask what it's mount security
579 * options were so it can use those later for submounts, displaying
580 * mount options, or whatever.
581 */
582static int selinux_get_mnt_opts(const struct super_block *sb,
Eric Parise0007522008-03-05 10:31:54 -0500583 struct security_mnt_opts *opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500584{
585 int rc = 0, i;
586 struct superblock_security_struct *sbsec = sb->s_security;
587 char *context = NULL;
588 u32 len;
589 char tmp;
590
Eric Parise0007522008-03-05 10:31:54 -0500591 security_init_mnt_opts(opts);
Eric Parisc9180a52007-11-30 13:00:35 -0500592
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500593 if (!(sbsec->flags & SE_SBINITIALIZED))
Eric Parisc9180a52007-11-30 13:00:35 -0500594 return -EINVAL;
595
596 if (!ss_initialized)
597 return -EINVAL;
598
Eric Parisaf8e50c2012-08-24 15:59:00 -0400599 /* make sure we always check enough bits to cover the mask */
600 BUILD_BUG_ON(SE_MNTMASK >= (1 << NUM_SEL_MNT_OPTS));
601
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500602 tmp = sbsec->flags & SE_MNTMASK;
Eric Parisc9180a52007-11-30 13:00:35 -0500603 /* count the number of mount options for this sb */
Eric Parisaf8e50c2012-08-24 15:59:00 -0400604 for (i = 0; i < NUM_SEL_MNT_OPTS; i++) {
Eric Parisc9180a52007-11-30 13:00:35 -0500605 if (tmp & 0x01)
Eric Parise0007522008-03-05 10:31:54 -0500606 opts->num_mnt_opts++;
Eric Parisc9180a52007-11-30 13:00:35 -0500607 tmp >>= 1;
608 }
David P. Quigley11689d42009-01-16 09:22:03 -0500609 /* Check if the Label support flag is set */
Eric Paris0b4bdb32013-08-28 13:32:42 -0400610 if (sbsec->flags & SBLABEL_MNT)
David P. Quigley11689d42009-01-16 09:22:03 -0500611 opts->num_mnt_opts++;
Eric Parisc9180a52007-11-30 13:00:35 -0500612
Eric Parise0007522008-03-05 10:31:54 -0500613 opts->mnt_opts = kcalloc(opts->num_mnt_opts, sizeof(char *), GFP_ATOMIC);
614 if (!opts->mnt_opts) {
Eric Parisc9180a52007-11-30 13:00:35 -0500615 rc = -ENOMEM;
616 goto out_free;
617 }
618
Eric Parise0007522008-03-05 10:31:54 -0500619 opts->mnt_opts_flags = kcalloc(opts->num_mnt_opts, sizeof(int), GFP_ATOMIC);
620 if (!opts->mnt_opts_flags) {
Eric Parisc9180a52007-11-30 13:00:35 -0500621 rc = -ENOMEM;
622 goto out_free;
623 }
624
625 i = 0;
626 if (sbsec->flags & FSCONTEXT_MNT) {
627 rc = security_sid_to_context(sbsec->sid, &context, &len);
628 if (rc)
629 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500630 opts->mnt_opts[i] = context;
631 opts->mnt_opts_flags[i++] = FSCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500632 }
633 if (sbsec->flags & CONTEXT_MNT) {
634 rc = security_sid_to_context(sbsec->mntpoint_sid, &context, &len);
635 if (rc)
636 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500637 opts->mnt_opts[i] = context;
638 opts->mnt_opts_flags[i++] = CONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500639 }
640 if (sbsec->flags & DEFCONTEXT_MNT) {
641 rc = security_sid_to_context(sbsec->def_sid, &context, &len);
642 if (rc)
643 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500644 opts->mnt_opts[i] = context;
645 opts->mnt_opts_flags[i++] = DEFCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500646 }
647 if (sbsec->flags & ROOTCONTEXT_MNT) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500648 struct dentry *root = sbsec->sb->s_root;
649 struct inode_security_struct *isec = backing_inode_security(root);
Eric Parisc9180a52007-11-30 13:00:35 -0500650
651 rc = security_sid_to_context(isec->sid, &context, &len);
652 if (rc)
653 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500654 opts->mnt_opts[i] = context;
655 opts->mnt_opts_flags[i++] = ROOTCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500656 }
Eric Paris12f348b2012-10-09 10:56:25 -0400657 if (sbsec->flags & SBLABEL_MNT) {
David P. Quigley11689d42009-01-16 09:22:03 -0500658 opts->mnt_opts[i] = NULL;
Eric Paris12f348b2012-10-09 10:56:25 -0400659 opts->mnt_opts_flags[i++] = SBLABEL_MNT;
David P. Quigley11689d42009-01-16 09:22:03 -0500660 }
Eric Parisc9180a52007-11-30 13:00:35 -0500661
Eric Parise0007522008-03-05 10:31:54 -0500662 BUG_ON(i != opts->num_mnt_opts);
Eric Parisc9180a52007-11-30 13:00:35 -0500663
664 return 0;
665
666out_free:
Eric Parise0007522008-03-05 10:31:54 -0500667 security_free_mnt_opts(opts);
Eric Parisc9180a52007-11-30 13:00:35 -0500668 return rc;
669}
670
671static int bad_option(struct superblock_security_struct *sbsec, char flag,
672 u32 old_sid, u32 new_sid)
673{
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500674 char mnt_flags = sbsec->flags & SE_MNTMASK;
675
Eric Parisc9180a52007-11-30 13:00:35 -0500676 /* check if the old mount command had the same options */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500677 if (sbsec->flags & SE_SBINITIALIZED)
Eric Parisc9180a52007-11-30 13:00:35 -0500678 if (!(sbsec->flags & flag) ||
679 (old_sid != new_sid))
680 return 1;
681
682 /* check if we were passed the same options twice,
683 * aka someone passed context=a,context=b
684 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500685 if (!(sbsec->flags & SE_SBINITIALIZED))
686 if (mnt_flags & flag)
Eric Parisc9180a52007-11-30 13:00:35 -0500687 return 1;
688 return 0;
689}
Eric Parise0007522008-03-05 10:31:54 -0500690
Eric Parisc9180a52007-11-30 13:00:35 -0500691/*
692 * Allow filesystems with binary mount data to explicitly set mount point
693 * labeling information.
694 */
Eric Parise0007522008-03-05 10:31:54 -0500695static int selinux_set_mnt_opts(struct super_block *sb,
David Quigley649f6e72013-05-22 12:50:36 -0400696 struct security_mnt_opts *opts,
697 unsigned long kern_flags,
698 unsigned long *set_kern_flags)
Eric Parisc9180a52007-11-30 13:00:35 -0500699{
David Howells275bb412008-11-14 10:39:19 +1100700 const struct cred *cred = current_cred();
Eric Parisc9180a52007-11-30 13:00:35 -0500701 int rc = 0, i;
Eric Parisc9180a52007-11-30 13:00:35 -0500702 struct superblock_security_struct *sbsec = sb->s_security;
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800703 const char *name = sb->s_type->name;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500704 struct dentry *root = sbsec->sb->s_root;
Paul Moore2c971652016-04-19 16:36:28 -0400705 struct inode_security_struct *root_isec;
Eric Parisc9180a52007-11-30 13:00:35 -0500706 u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
707 u32 defcontext_sid = 0;
Eric Parise0007522008-03-05 10:31:54 -0500708 char **mount_options = opts->mnt_opts;
709 int *flags = opts->mnt_opts_flags;
710 int num_opts = opts->num_mnt_opts;
Eric Parisc9180a52007-11-30 13:00:35 -0500711
712 mutex_lock(&sbsec->lock);
713
714 if (!ss_initialized) {
715 if (!num_opts) {
716 /* Defer initialization until selinux_complete_init,
717 after the initial policy is loaded and the security
718 server is ready to handle calls. */
Eric Parisc9180a52007-11-30 13:00:35 -0500719 goto out;
720 }
721 rc = -EINVAL;
Eric Paris744ba352008-04-17 11:52:44 -0400722 printk(KERN_WARNING "SELinux: Unable to set superblock options "
723 "before the security server is initialized\n");
Eric Parisc9180a52007-11-30 13:00:35 -0500724 goto out;
725 }
David Quigley649f6e72013-05-22 12:50:36 -0400726 if (kern_flags && !set_kern_flags) {
727 /* Specifying internal flags without providing a place to
728 * place the results is not allowed */
729 rc = -EINVAL;
730 goto out;
731 }
Eric Parisc9180a52007-11-30 13:00:35 -0500732
733 /*
Eric Parise0007522008-03-05 10:31:54 -0500734 * Binary mount data FS will come through this function twice. Once
735 * from an explicit call and once from the generic calls from the vfs.
736 * Since the generic VFS calls will not contain any security mount data
737 * we need to skip the double mount verification.
738 *
739 * This does open a hole in which we will not notice if the first
740 * mount using this sb set explict options and a second mount using
741 * this sb does not set any security options. (The first options
742 * will be used for both mounts)
743 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500744 if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
Eric Parise0007522008-03-05 10:31:54 -0500745 && (num_opts == 0))
Eric Parisf5269712008-05-14 11:27:45 -0400746 goto out;
Eric Parise0007522008-03-05 10:31:54 -0500747
Paul Moore2c971652016-04-19 16:36:28 -0400748 root_isec = backing_inode_security_novalidate(root);
749
Eric Parise0007522008-03-05 10:31:54 -0500750 /*
Eric Parisc9180a52007-11-30 13:00:35 -0500751 * parse the mount options, check if they are valid sids.
752 * also check if someone is trying to mount the same sb more
753 * than once with different security options.
754 */
755 for (i = 0; i < num_opts; i++) {
756 u32 sid;
David P. Quigley11689d42009-01-16 09:22:03 -0500757
Eric Paris12f348b2012-10-09 10:56:25 -0400758 if (flags[i] == SBLABEL_MNT)
David P. Quigley11689d42009-01-16 09:22:03 -0500759 continue;
Rasmus Villemoes44be2f62015-10-21 17:44:25 -0400760 rc = security_context_str_to_sid(mount_options[i], &sid, GFP_KERNEL);
Eric Parisc9180a52007-11-30 13:00:35 -0500761 if (rc) {
Rasmus Villemoes44be2f62015-10-21 17:44:25 -0400762 printk(KERN_WARNING "SELinux: security_context_str_to_sid"
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800763 "(%s) failed for (dev %s, type %s) errno=%d\n",
764 mount_options[i], sb->s_id, name, rc);
Eric Parisc9180a52007-11-30 13:00:35 -0500765 goto out;
766 }
767 switch (flags[i]) {
768 case FSCONTEXT_MNT:
769 fscontext_sid = sid;
770
771 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
772 fscontext_sid))
773 goto out_double_mount;
774
775 sbsec->flags |= FSCONTEXT_MNT;
776 break;
777 case CONTEXT_MNT:
778 context_sid = sid;
779
780 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
781 context_sid))
782 goto out_double_mount;
783
784 sbsec->flags |= CONTEXT_MNT;
785 break;
786 case ROOTCONTEXT_MNT:
787 rootcontext_sid = sid;
788
789 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
790 rootcontext_sid))
791 goto out_double_mount;
792
793 sbsec->flags |= ROOTCONTEXT_MNT;
794
795 break;
796 case DEFCONTEXT_MNT:
797 defcontext_sid = sid;
798
799 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
800 defcontext_sid))
801 goto out_double_mount;
802
803 sbsec->flags |= DEFCONTEXT_MNT;
804
805 break;
806 default:
807 rc = -EINVAL;
808 goto out;
809 }
810 }
811
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500812 if (sbsec->flags & SE_SBINITIALIZED) {
Eric Parisc9180a52007-11-30 13:00:35 -0500813 /* previously mounted with options, but not on this attempt? */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500814 if ((sbsec->flags & SE_MNTMASK) && !num_opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500815 goto out_double_mount;
816 rc = 0;
817 goto out;
818 }
819
James Morris089be432008-07-15 18:32:49 +1000820 if (strcmp(sb->s_type->name, "proc") == 0)
Stephen Smalley134509d2015-06-04 16:22:17 -0400821 sbsec->flags |= SE_SBPROC | SE_SBGENFS;
822
Stephen Smalley8e014722015-06-04 16:22:17 -0400823 if (!strcmp(sb->s_type->name, "debugfs") ||
Jeff Vander Stoep8bdee6782017-06-20 09:35:33 -0700824 !strcmp(sb->s_type->name, "tracefs") ||
Stephen Smalley8e014722015-06-04 16:22:17 -0400825 !strcmp(sb->s_type->name, "sysfs") ||
826 !strcmp(sb->s_type->name, "pstore"))
Stephen Smalley134509d2015-06-04 16:22:17 -0400827 sbsec->flags |= SE_SBGENFS;
Eric Parisc9180a52007-11-30 13:00:35 -0500828
David Quigleyeb9ae682013-05-22 12:50:37 -0400829 if (!sbsec->behavior) {
830 /*
831 * Determine the labeling behavior to use for this
832 * filesystem type.
833 */
Paul Moore98f700f2013-09-18 13:52:20 -0400834 rc = security_fs_use(sb);
David Quigleyeb9ae682013-05-22 12:50:37 -0400835 if (rc) {
836 printk(KERN_WARNING
837 "%s: security_fs_use(%s) returned %d\n",
838 __func__, sb->s_type->name, rc);
839 goto out;
840 }
Eric Parisc9180a52007-11-30 13:00:35 -0500841 }
Seth Forsheeaad82892016-04-26 14:36:20 -0500842
843 /*
844 * If this is a user namespace mount, no contexts are allowed
845 * on the command line and security labels must be ignored.
846 */
847 if (sb->s_user_ns != &init_user_ns) {
848 if (context_sid || fscontext_sid || rootcontext_sid ||
849 defcontext_sid) {
850 rc = -EACCES;
851 goto out;
852 }
853 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
854 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
855 rc = security_transition_sid(current_sid(), current_sid(),
856 SECCLASS_FILE, NULL,
857 &sbsec->mntpoint_sid);
858 if (rc)
859 goto out;
860 }
861 goto out_set_opts;
862 }
863
Eric Parisc9180a52007-11-30 13:00:35 -0500864 /* sets the context of the superblock for the fs being mounted. */
865 if (fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100866 rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500867 if (rc)
868 goto out;
869
870 sbsec->sid = fscontext_sid;
871 }
872
873 /*
874 * Switch to using mount point labeling behavior.
875 * sets the label used on all file below the mountpoint, and will set
876 * the superblock context if not already set.
877 */
David Quigleyeb9ae682013-05-22 12:50:37 -0400878 if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !context_sid) {
879 sbsec->behavior = SECURITY_FS_USE_NATIVE;
880 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
881 }
882
Eric Parisc9180a52007-11-30 13:00:35 -0500883 if (context_sid) {
884 if (!fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100885 rc = may_context_mount_sb_relabel(context_sid, sbsec,
886 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500887 if (rc)
888 goto out;
889 sbsec->sid = context_sid;
890 } else {
David Howells275bb412008-11-14 10:39:19 +1100891 rc = may_context_mount_inode_relabel(context_sid, sbsec,
892 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500893 if (rc)
894 goto out;
895 }
896 if (!rootcontext_sid)
897 rootcontext_sid = context_sid;
898
899 sbsec->mntpoint_sid = context_sid;
900 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
901 }
902
903 if (rootcontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100904 rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec,
905 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500906 if (rc)
907 goto out;
908
909 root_isec->sid = rootcontext_sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -0500910 root_isec->initialized = LABEL_INITIALIZED;
Eric Parisc9180a52007-11-30 13:00:35 -0500911 }
912
913 if (defcontext_sid) {
David Quigleyeb9ae682013-05-22 12:50:37 -0400914 if (sbsec->behavior != SECURITY_FS_USE_XATTR &&
915 sbsec->behavior != SECURITY_FS_USE_NATIVE) {
Eric Parisc9180a52007-11-30 13:00:35 -0500916 rc = -EINVAL;
917 printk(KERN_WARNING "SELinux: defcontext option is "
918 "invalid for this filesystem type\n");
919 goto out;
920 }
921
922 if (defcontext_sid != sbsec->def_sid) {
923 rc = may_context_mount_inode_relabel(defcontext_sid,
David Howells275bb412008-11-14 10:39:19 +1100924 sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500925 if (rc)
926 goto out;
927 }
928
929 sbsec->def_sid = defcontext_sid;
930 }
931
Seth Forsheeaad82892016-04-26 14:36:20 -0500932out_set_opts:
Eric Parisc9180a52007-11-30 13:00:35 -0500933 rc = sb_finish_set_opts(sb);
934out:
Eric Parisbc7e9822006-09-25 23:32:02 -0700935 mutex_unlock(&sbsec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 return rc;
Eric Parisc9180a52007-11-30 13:00:35 -0500937out_double_mount:
938 rc = -EINVAL;
939 printk(KERN_WARNING "SELinux: mount invalid. Same superblock, different "
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800940 "security settings for (dev %s, type %s)\n", sb->s_id, name);
Eric Parisc9180a52007-11-30 13:00:35 -0500941 goto out;
942}
943
Jeff Layton094f7b62013-04-01 08:14:24 -0400944static int selinux_cmp_sb_context(const struct super_block *oldsb,
945 const struct super_block *newsb)
946{
947 struct superblock_security_struct *old = oldsb->s_security;
948 struct superblock_security_struct *new = newsb->s_security;
949 char oldflags = old->flags & SE_MNTMASK;
950 char newflags = new->flags & SE_MNTMASK;
951
952 if (oldflags != newflags)
953 goto mismatch;
954 if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid)
955 goto mismatch;
956 if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid)
957 goto mismatch;
958 if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid)
959 goto mismatch;
960 if (oldflags & ROOTCONTEXT_MNT) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500961 struct inode_security_struct *oldroot = backing_inode_security(oldsb->s_root);
962 struct inode_security_struct *newroot = backing_inode_security(newsb->s_root);
Jeff Layton094f7b62013-04-01 08:14:24 -0400963 if (oldroot->sid != newroot->sid)
964 goto mismatch;
965 }
966 return 0;
967mismatch:
968 printk(KERN_WARNING "SELinux: mount invalid. Same superblock, "
969 "different security settings for (dev %s, "
970 "type %s)\n", newsb->s_id, newsb->s_type->name);
971 return -EBUSY;
972}
973
974static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
Eric Parisc9180a52007-11-30 13:00:35 -0500975 struct super_block *newsb)
976{
977 const struct superblock_security_struct *oldsbsec = oldsb->s_security;
978 struct superblock_security_struct *newsbsec = newsb->s_security;
979
980 int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT);
981 int set_context = (oldsbsec->flags & CONTEXT_MNT);
982 int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT);
983
Eric Paris0f5e6422008-04-21 16:24:11 -0400984 /*
985 * if the parent was able to be mounted it clearly had no special lsm
Al Viroe8c26252010-03-23 06:36:54 -0400986 * mount options. thus we can safely deal with this superblock later
Eric Paris0f5e6422008-04-21 16:24:11 -0400987 */
Al Viroe8c26252010-03-23 06:36:54 -0400988 if (!ss_initialized)
Jeff Layton094f7b62013-04-01 08:14:24 -0400989 return 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500990
Eric Parisc9180a52007-11-30 13:00:35 -0500991 /* how can we clone if the old one wasn't set up?? */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500992 BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED));
Eric Parisc9180a52007-11-30 13:00:35 -0500993
Jeff Layton094f7b62013-04-01 08:14:24 -0400994 /* if fs is reusing a sb, make sure that the contexts match */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500995 if (newsbsec->flags & SE_SBINITIALIZED)
Jeff Layton094f7b62013-04-01 08:14:24 -0400996 return selinux_cmp_sb_context(oldsb, newsb);
Eric Paris5a552612008-04-09 14:08:35 -0400997
Eric Parisc9180a52007-11-30 13:00:35 -0500998 mutex_lock(&newsbsec->lock);
999
1000 newsbsec->flags = oldsbsec->flags;
1001
1002 newsbsec->sid = oldsbsec->sid;
1003 newsbsec->def_sid = oldsbsec->def_sid;
1004 newsbsec->behavior = oldsbsec->behavior;
1005
1006 if (set_context) {
1007 u32 sid = oldsbsec->mntpoint_sid;
1008
1009 if (!set_fscontext)
1010 newsbsec->sid = sid;
1011 if (!set_rootcontext) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001012 struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
Eric Parisc9180a52007-11-30 13:00:35 -05001013 newisec->sid = sid;
1014 }
1015 newsbsec->mntpoint_sid = sid;
1016 }
1017 if (set_rootcontext) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001018 const struct inode_security_struct *oldisec = backing_inode_security(oldsb->s_root);
1019 struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
Eric Parisc9180a52007-11-30 13:00:35 -05001020
1021 newisec->sid = oldisec->sid;
1022 }
1023
1024 sb_finish_set_opts(newsb);
1025 mutex_unlock(&newsbsec->lock);
Jeff Layton094f7b62013-04-01 08:14:24 -04001026 return 0;
Eric Parisc9180a52007-11-30 13:00:35 -05001027}
1028
Adrian Bunk2e1479d2008-03-17 22:29:23 +02001029static int selinux_parse_opts_str(char *options,
1030 struct security_mnt_opts *opts)
Eric Parisc9180a52007-11-30 13:00:35 -05001031{
Eric Parise0007522008-03-05 10:31:54 -05001032 char *p;
Eric Parisc9180a52007-11-30 13:00:35 -05001033 char *context = NULL, *defcontext = NULL;
1034 char *fscontext = NULL, *rootcontext = NULL;
Eric Parise0007522008-03-05 10:31:54 -05001035 int rc, num_mnt_opts = 0;
Eric Parisc9180a52007-11-30 13:00:35 -05001036
Eric Parise0007522008-03-05 10:31:54 -05001037 opts->num_mnt_opts = 0;
Eric Parisc9180a52007-11-30 13:00:35 -05001038
1039 /* Standard string-based options. */
1040 while ((p = strsep(&options, "|")) != NULL) {
1041 int token;
1042 substring_t args[MAX_OPT_ARGS];
1043
1044 if (!*p)
1045 continue;
1046
1047 token = match_token(p, tokens, args);
1048
1049 switch (token) {
1050 case Opt_context:
1051 if (context || defcontext) {
1052 rc = -EINVAL;
1053 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
1054 goto out_err;
1055 }
1056 context = match_strdup(&args[0]);
1057 if (!context) {
1058 rc = -ENOMEM;
1059 goto out_err;
1060 }
1061 break;
1062
1063 case Opt_fscontext:
1064 if (fscontext) {
1065 rc = -EINVAL;
1066 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
1067 goto out_err;
1068 }
1069 fscontext = match_strdup(&args[0]);
1070 if (!fscontext) {
1071 rc = -ENOMEM;
1072 goto out_err;
1073 }
1074 break;
1075
1076 case Opt_rootcontext:
1077 if (rootcontext) {
1078 rc = -EINVAL;
1079 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
1080 goto out_err;
1081 }
1082 rootcontext = match_strdup(&args[0]);
1083 if (!rootcontext) {
1084 rc = -ENOMEM;
1085 goto out_err;
1086 }
1087 break;
1088
1089 case Opt_defcontext:
1090 if (context || defcontext) {
1091 rc = -EINVAL;
1092 printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
1093 goto out_err;
1094 }
1095 defcontext = match_strdup(&args[0]);
1096 if (!defcontext) {
1097 rc = -ENOMEM;
1098 goto out_err;
1099 }
1100 break;
David P. Quigley11689d42009-01-16 09:22:03 -05001101 case Opt_labelsupport:
1102 break;
Eric Parisc9180a52007-11-30 13:00:35 -05001103 default:
1104 rc = -EINVAL;
1105 printk(KERN_WARNING "SELinux: unknown mount option\n");
1106 goto out_err;
1107
1108 }
1109 }
1110
Eric Parise0007522008-03-05 10:31:54 -05001111 rc = -ENOMEM;
1112 opts->mnt_opts = kcalloc(NUM_SEL_MNT_OPTS, sizeof(char *), GFP_ATOMIC);
1113 if (!opts->mnt_opts)
1114 goto out_err;
1115
1116 opts->mnt_opts_flags = kcalloc(NUM_SEL_MNT_OPTS, sizeof(int), GFP_ATOMIC);
Paul Moore708f1812018-05-04 12:02:49 +05301117 if (!opts->mnt_opts_flags)
Eric Parise0007522008-03-05 10:31:54 -05001118 goto out_err;
Paul Moore708f1812018-05-04 12:02:49 +05301119
Eric Parisc9180a52007-11-30 13:00:35 -05001120
Eric Parise0007522008-03-05 10:31:54 -05001121 if (fscontext) {
1122 opts->mnt_opts[num_mnt_opts] = fscontext;
1123 opts->mnt_opts_flags[num_mnt_opts++] = FSCONTEXT_MNT;
1124 }
1125 if (context) {
1126 opts->mnt_opts[num_mnt_opts] = context;
1127 opts->mnt_opts_flags[num_mnt_opts++] = CONTEXT_MNT;
1128 }
1129 if (rootcontext) {
1130 opts->mnt_opts[num_mnt_opts] = rootcontext;
1131 opts->mnt_opts_flags[num_mnt_opts++] = ROOTCONTEXT_MNT;
1132 }
1133 if (defcontext) {
1134 opts->mnt_opts[num_mnt_opts] = defcontext;
1135 opts->mnt_opts_flags[num_mnt_opts++] = DEFCONTEXT_MNT;
1136 }
1137
1138 opts->num_mnt_opts = num_mnt_opts;
1139 return 0;
1140
Eric Parisc9180a52007-11-30 13:00:35 -05001141out_err:
Paul Moore708f1812018-05-04 12:02:49 +05301142 security_free_mnt_opts(opts);
Eric Parisc9180a52007-11-30 13:00:35 -05001143 kfree(context);
1144 kfree(defcontext);
1145 kfree(fscontext);
1146 kfree(rootcontext);
1147 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148}
Eric Parise0007522008-03-05 10:31:54 -05001149/*
1150 * string mount options parsing and call set the sbsec
1151 */
1152static int superblock_doinit(struct super_block *sb, void *data)
1153{
1154 int rc = 0;
1155 char *options = data;
1156 struct security_mnt_opts opts;
1157
1158 security_init_mnt_opts(&opts);
1159
1160 if (!data)
1161 goto out;
1162
1163 BUG_ON(sb->s_type->fs_flags & FS_BINARY_MOUNTDATA);
1164
1165 rc = selinux_parse_opts_str(options, &opts);
1166 if (rc)
1167 goto out_err;
1168
1169out:
David Quigley649f6e72013-05-22 12:50:36 -04001170 rc = selinux_set_mnt_opts(sb, &opts, 0, NULL);
Eric Parise0007522008-03-05 10:31:54 -05001171
1172out_err:
1173 security_free_mnt_opts(&opts);
1174 return rc;
1175}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Adrian Bunk3583a712008-07-22 20:21:23 +03001177static void selinux_write_opts(struct seq_file *m,
1178 struct security_mnt_opts *opts)
Eric Paris2069f452008-07-04 09:47:13 +10001179{
1180 int i;
1181 char *prefix;
1182
1183 for (i = 0; i < opts->num_mnt_opts; i++) {
David P. Quigley11689d42009-01-16 09:22:03 -05001184 char *has_comma;
1185
1186 if (opts->mnt_opts[i])
1187 has_comma = strchr(opts->mnt_opts[i], ',');
1188 else
1189 has_comma = NULL;
Eric Paris2069f452008-07-04 09:47:13 +10001190
1191 switch (opts->mnt_opts_flags[i]) {
1192 case CONTEXT_MNT:
1193 prefix = CONTEXT_STR;
1194 break;
1195 case FSCONTEXT_MNT:
1196 prefix = FSCONTEXT_STR;
1197 break;
1198 case ROOTCONTEXT_MNT:
1199 prefix = ROOTCONTEXT_STR;
1200 break;
1201 case DEFCONTEXT_MNT:
1202 prefix = DEFCONTEXT_STR;
1203 break;
Eric Paris12f348b2012-10-09 10:56:25 -04001204 case SBLABEL_MNT:
David P. Quigley11689d42009-01-16 09:22:03 -05001205 seq_putc(m, ',');
1206 seq_puts(m, LABELSUPP_STR);
1207 continue;
Eric Paris2069f452008-07-04 09:47:13 +10001208 default:
1209 BUG();
Eric Parisa35c6c82011-04-20 10:21:28 -04001210 return;
Eric Paris2069f452008-07-04 09:47:13 +10001211 };
1212 /* we need a comma before each option */
1213 seq_putc(m, ',');
1214 seq_puts(m, prefix);
1215 if (has_comma)
1216 seq_putc(m, '\"');
Kees Cooka068acf2015-09-04 15:44:57 -07001217 seq_escape(m, opts->mnt_opts[i], "\"\n\\");
Eric Paris2069f452008-07-04 09:47:13 +10001218 if (has_comma)
1219 seq_putc(m, '\"');
1220 }
1221}
1222
1223static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
1224{
1225 struct security_mnt_opts opts;
1226 int rc;
1227
1228 rc = selinux_get_mnt_opts(sb, &opts);
Eric Paris383795c2008-07-29 17:07:26 -04001229 if (rc) {
1230 /* before policy load we may get EINVAL, don't show anything */
1231 if (rc == -EINVAL)
1232 rc = 0;
Eric Paris2069f452008-07-04 09:47:13 +10001233 return rc;
Eric Paris383795c2008-07-29 17:07:26 -04001234 }
Eric Paris2069f452008-07-04 09:47:13 +10001235
1236 selinux_write_opts(m, &opts);
1237
1238 security_free_mnt_opts(&opts);
1239
1240 return rc;
1241}
1242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243static inline u16 inode_mode_to_security_class(umode_t mode)
1244{
1245 switch (mode & S_IFMT) {
1246 case S_IFSOCK:
1247 return SECCLASS_SOCK_FILE;
1248 case S_IFLNK:
1249 return SECCLASS_LNK_FILE;
1250 case S_IFREG:
1251 return SECCLASS_FILE;
1252 case S_IFBLK:
1253 return SECCLASS_BLK_FILE;
1254 case S_IFDIR:
1255 return SECCLASS_DIR;
1256 case S_IFCHR:
1257 return SECCLASS_CHR_FILE;
1258 case S_IFIFO:
1259 return SECCLASS_FIFO_FILE;
1260
1261 }
1262
1263 return SECCLASS_FILE;
1264}
1265
James Morris13402582005-09-30 14:24:34 -04001266static inline int default_protocol_stream(int protocol)
1267{
1268 return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP);
1269}
1270
1271static inline int default_protocol_dgram(int protocol)
1272{
1273 return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
1274}
1275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276static inline u16 socket_type_to_security_class(int family, int type, int protocol)
1277{
1278 switch (family) {
1279 case PF_UNIX:
1280 switch (type) {
1281 case SOCK_STREAM:
1282 case SOCK_SEQPACKET:
1283 return SECCLASS_UNIX_STREAM_SOCKET;
1284 case SOCK_DGRAM:
1285 return SECCLASS_UNIX_DGRAM_SOCKET;
1286 }
1287 break;
1288 case PF_INET:
1289 case PF_INET6:
1290 switch (type) {
1291 case SOCK_STREAM:
James Morris13402582005-09-30 14:24:34 -04001292 if (default_protocol_stream(protocol))
1293 return SECCLASS_TCP_SOCKET;
1294 else
1295 return SECCLASS_RAWIP_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 case SOCK_DGRAM:
James Morris13402582005-09-30 14:24:34 -04001297 if (default_protocol_dgram(protocol))
1298 return SECCLASS_UDP_SOCKET;
1299 else
1300 return SECCLASS_RAWIP_SOCKET;
James Morris2ee92d42006-11-13 16:09:01 -08001301 case SOCK_DCCP:
1302 return SECCLASS_DCCP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001303 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 return SECCLASS_RAWIP_SOCKET;
1305 }
1306 break;
1307 case PF_NETLINK:
1308 switch (protocol) {
1309 case NETLINK_ROUTE:
1310 return SECCLASS_NETLINK_ROUTE_SOCKET;
Pavel Emelyanov7f1fb602011-12-06 07:56:43 +00001311 case NETLINK_SOCK_DIAG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 return SECCLASS_NETLINK_TCPDIAG_SOCKET;
1313 case NETLINK_NFLOG:
1314 return SECCLASS_NETLINK_NFLOG_SOCKET;
1315 case NETLINK_XFRM:
1316 return SECCLASS_NETLINK_XFRM_SOCKET;
1317 case NETLINK_SELINUX:
1318 return SECCLASS_NETLINK_SELINUX_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001319 case NETLINK_ISCSI:
1320 return SECCLASS_NETLINK_ISCSI_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 case NETLINK_AUDIT:
1322 return SECCLASS_NETLINK_AUDIT_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001323 case NETLINK_FIB_LOOKUP:
1324 return SECCLASS_NETLINK_FIB_LOOKUP_SOCKET;
1325 case NETLINK_CONNECTOR:
1326 return SECCLASS_NETLINK_CONNECTOR_SOCKET;
1327 case NETLINK_NETFILTER:
1328 return SECCLASS_NETLINK_NETFILTER_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 case NETLINK_DNRTMSG:
1330 return SECCLASS_NETLINK_DNRT_SOCKET;
James Morris0c9b7942005-04-16 15:24:13 -07001331 case NETLINK_KOBJECT_UEVENT:
1332 return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001333 case NETLINK_GENERIC:
1334 return SECCLASS_NETLINK_GENERIC_SOCKET;
1335 case NETLINK_SCSITRANSPORT:
1336 return SECCLASS_NETLINK_SCSITRANSPORT_SOCKET;
1337 case NETLINK_RDMA:
1338 return SECCLASS_NETLINK_RDMA_SOCKET;
1339 case NETLINK_CRYPTO:
1340 return SECCLASS_NETLINK_CRYPTO_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 default:
1342 return SECCLASS_NETLINK_SOCKET;
1343 }
1344 case PF_PACKET:
1345 return SECCLASS_PACKET_SOCKET;
1346 case PF_KEY:
1347 return SECCLASS_KEY_SOCKET;
Christopher J. PeBenito3e3ff152006-06-09 00:25:03 -07001348 case PF_APPLETALK:
1349 return SECCLASS_APPLETALK_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 }
1351
1352 return SECCLASS_SOCKET;
1353}
1354
Stephen Smalley134509d2015-06-04 16:22:17 -04001355static int selinux_genfs_get_sid(struct dentry *dentry,
1356 u16 tclass,
1357 u16 flags,
1358 u32 *sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359{
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001360 int rc;
Al Virofc640052016-04-10 01:33:30 -04001361 struct super_block *sb = dentry->d_sb;
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001362 char *buffer, *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Eric Paris828dfe12008-04-17 13:17:49 -04001364 buffer = (char *)__get_free_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 if (!buffer)
1366 return -ENOMEM;
1367
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001368 path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
1369 if (IS_ERR(path))
1370 rc = PTR_ERR(path);
1371 else {
Stephen Smalley134509d2015-06-04 16:22:17 -04001372 if (flags & SE_SBPROC) {
1373 /* each process gets a /proc/PID/ entry. Strip off the
1374 * PID part to get a valid selinux labeling.
1375 * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
1376 while (path[1] >= '0' && path[1] <= '9') {
1377 path[1] = '/';
1378 path++;
1379 }
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001380 }
Stephen Smalley134509d2015-06-04 16:22:17 -04001381 rc = security_genfs_sid(sb->s_type->name, path, tclass, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 free_page((unsigned long)buffer);
1384 return rc;
1385}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
1387/* The inode's security attributes must be initialized before first use. */
1388static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
1389{
1390 struct superblock_security_struct *sbsec = NULL;
1391 struct inode_security_struct *isec = inode->i_security;
1392 u32 sid;
1393 struct dentry *dentry;
1394#define INITCONTEXTLEN 255
1395 char *context = NULL;
1396 unsigned len = 0;
1397 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05001399 if (isec->initialized == LABEL_INITIALIZED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 goto out;
1401
Eric Paris23970742006-09-25 23:32:01 -07001402 mutex_lock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05001403 if (isec->initialized == LABEL_INITIALIZED)
Eric Paris23970742006-09-25 23:32:01 -07001404 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
1406 sbsec = inode->i_sb->s_security;
David P. Quigley0d90a7e2009-01-16 09:22:02 -05001407 if (!(sbsec->flags & SE_SBINITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 /* Defer initialization until selinux_complete_init,
1409 after the initial policy is loaded and the security
1410 server is ready to handle calls. */
1411 spin_lock(&sbsec->isec_lock);
1412 if (list_empty(&isec->list))
1413 list_add(&isec->list, &sbsec->isec_head);
1414 spin_unlock(&sbsec->isec_lock);
Eric Paris23970742006-09-25 23:32:01 -07001415 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 }
1417
1418 switch (sbsec->behavior) {
David Quigleyeb9ae682013-05-22 12:50:37 -04001419 case SECURITY_FS_USE_NATIVE:
1420 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 case SECURITY_FS_USE_XATTR:
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001422 if (!(inode->i_opflags & IOP_XATTR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 isec->sid = sbsec->def_sid;
1424 break;
1425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 /* Need a dentry, since the xattr API requires one.
1427 Life would be simpler if we could just pass the inode. */
1428 if (opt_dentry) {
1429 /* Called from d_instantiate or d_splice_alias. */
1430 dentry = dget(opt_dentry);
1431 } else {
1432 /* Called from selinux_complete_init, try to find a dentry. */
1433 dentry = d_find_alias(inode);
1434 }
1435 if (!dentry) {
Eric Parisdf7f54c2009-03-09 14:35:58 -04001436 /*
1437 * this is can be hit on boot when a file is accessed
1438 * before the policy is loaded. When we load policy we
1439 * may find inodes that have no dentry on the
1440 * sbsec->isec_head list. No reason to complain as these
1441 * will get fixed up the next time we go through
1442 * inode_doinit with a dentry, before these inodes could
1443 * be used again by userspace.
1444 */
Eric Paris23970742006-09-25 23:32:01 -07001445 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 }
1447
1448 len = INITCONTEXTLEN;
Eric Paris4cb912f2009-02-12 14:50:05 -05001449 context = kmalloc(len+1, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 if (!context) {
1451 rc = -ENOMEM;
1452 dput(dentry);
Eric Paris23970742006-09-25 23:32:01 -07001453 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 }
Eric Paris4cb912f2009-02-12 14:50:05 -05001455 context[len] = '\0';
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001456 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 if (rc == -ERANGE) {
James Morris314dabb2009-08-10 22:00:13 +10001458 kfree(context);
1459
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 /* Need a larger buffer. Query for the right size. */
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001461 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 if (rc < 0) {
1463 dput(dentry);
Eric Paris23970742006-09-25 23:32:01 -07001464 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 len = rc;
Eric Paris4cb912f2009-02-12 14:50:05 -05001467 context = kmalloc(len+1, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 if (!context) {
1469 rc = -ENOMEM;
1470 dput(dentry);
Eric Paris23970742006-09-25 23:32:01 -07001471 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 }
Eric Paris4cb912f2009-02-12 14:50:05 -05001473 context[len] = '\0';
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001474 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 }
1476 dput(dentry);
1477 if (rc < 0) {
1478 if (rc != -ENODATA) {
Eric Paris744ba352008-04-17 11:52:44 -04001479 printk(KERN_WARNING "SELinux: %s: getxattr returned "
Harvey Harrisondd6f9532008-03-06 10:03:59 +11001480 "%d for dev=%s ino=%ld\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 -rc, inode->i_sb->s_id, inode->i_ino);
1482 kfree(context);
Eric Paris23970742006-09-25 23:32:01 -07001483 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 }
1485 /* Map ENODATA to the default file SID */
1486 sid = sbsec->def_sid;
1487 rc = 0;
1488 } else {
James Morrisf5c1d5b2005-07-28 01:07:37 -07001489 rc = security_context_to_sid_default(context, rc, &sid,
Stephen Smalley869ab512008-04-04 08:46:05 -04001490 sbsec->def_sid,
1491 GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 if (rc) {
Eric Paris4ba0a8a2009-02-12 15:01:10 -05001493 char *dev = inode->i_sb->s_id;
1494 unsigned long ino = inode->i_ino;
1495
1496 if (rc == -EINVAL) {
1497 if (printk_ratelimit())
1498 printk(KERN_NOTICE "SELinux: inode=%lu on dev=%s was found to have an invalid "
1499 "context=%s. This indicates you may need to relabel the inode or the "
1500 "filesystem in question.\n", ino, dev, context);
1501 } else {
1502 printk(KERN_WARNING "SELinux: %s: context_to_sid(%s) "
1503 "returned %d for dev=%s ino=%ld\n",
1504 __func__, context, -rc, dev, ino);
1505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 kfree(context);
1507 /* Leave with the unlabeled SID */
1508 rc = 0;
1509 break;
1510 }
1511 }
1512 kfree(context);
1513 isec->sid = sid;
1514 break;
1515 case SECURITY_FS_USE_TASK:
1516 isec->sid = isec->task_sid;
1517 break;
1518 case SECURITY_FS_USE_TRANS:
1519 /* Default to the fs SID. */
1520 isec->sid = sbsec->sid;
1521
1522 /* Try to obtain a transition SID. */
1523 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Eric Paris652bb9b2011-02-01 11:05:40 -05001524 rc = security_transition_sid(isec->task_sid, sbsec->sid,
1525 isec->sclass, NULL, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 if (rc)
Eric Paris23970742006-09-25 23:32:01 -07001527 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 isec->sid = sid;
1529 break;
Eric Parisc312feb2006-07-10 04:43:53 -07001530 case SECURITY_FS_USE_MNTPOINT:
1531 isec->sid = sbsec->mntpoint_sid;
1532 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 default:
Eric Parisc312feb2006-07-10 04:43:53 -07001534 /* Default to the fs superblock SID. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 isec->sid = sbsec->sid;
1536
Stephen Smalley134509d2015-06-04 16:22:17 -04001537 if ((sbsec->flags & SE_SBGENFS) && !S_ISLNK(inode->i_mode)) {
Paul Mooref64410e2014-03-19 16:46:18 -04001538 /* We must have a dentry to determine the label on
1539 * procfs inodes */
1540 if (opt_dentry)
1541 /* Called from d_instantiate or
1542 * d_splice_alias. */
1543 dentry = dget(opt_dentry);
1544 else
1545 /* Called from selinux_complete_init, try to
1546 * find a dentry. */
1547 dentry = d_find_alias(inode);
1548 /*
1549 * This can be hit on boot when a file is accessed
1550 * before the policy is loaded. When we load policy we
1551 * may find inodes that have no dentry on the
1552 * sbsec->isec_head list. No reason to complain as
1553 * these will get fixed up the next time we go through
1554 * inode_doinit() with a dentry, before these inodes
1555 * could be used again by userspace.
1556 */
1557 if (!dentry)
1558 goto out_unlock;
1559 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Stephen Smalley134509d2015-06-04 16:22:17 -04001560 rc = selinux_genfs_get_sid(dentry, isec->sclass,
1561 sbsec->flags, &sid);
Paul Mooref64410e2014-03-19 16:46:18 -04001562 dput(dentry);
1563 if (rc)
1564 goto out_unlock;
1565 isec->sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 }
1567 break;
1568 }
1569
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05001570 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
Eric Paris23970742006-09-25 23:32:01 -07001572out_unlock:
1573 mutex_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574out:
1575 if (isec->sclass == SECCLASS_FILE)
1576 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 return rc;
1578}
1579
1580/* Convert a Linux signal to an access vector. */
1581static inline u32 signal_to_av(int sig)
1582{
1583 u32 perm = 0;
1584
1585 switch (sig) {
1586 case SIGCHLD:
1587 /* Commonly granted from child to parent. */
1588 perm = PROCESS__SIGCHLD;
1589 break;
1590 case SIGKILL:
1591 /* Cannot be caught or ignored */
1592 perm = PROCESS__SIGKILL;
1593 break;
1594 case SIGSTOP:
1595 /* Cannot be caught or ignored */
1596 perm = PROCESS__SIGSTOP;
1597 break;
1598 default:
1599 /* All other signals. */
1600 perm = PROCESS__SIGNAL;
1601 break;
1602 }
1603
1604 return perm;
1605}
1606
David Howells275bb412008-11-14 10:39:19 +11001607/*
David Howellsd84f4f92008-11-14 10:39:23 +11001608 * Check permission between a pair of credentials
1609 * fork check, ptrace check, etc.
1610 */
1611static int cred_has_perm(const struct cred *actor,
1612 const struct cred *target,
1613 u32 perms)
1614{
1615 u32 asid = cred_sid(actor), tsid = cred_sid(target);
1616
1617 return avc_has_perm(asid, tsid, SECCLASS_PROCESS, perms, NULL);
1618}
1619
1620/*
David Howells88e67f32008-11-14 10:39:21 +11001621 * Check permission between a pair of tasks, e.g. signal checks,
David Howells275bb412008-11-14 10:39:19 +11001622 * fork check, ptrace check, etc.
1623 * tsk1 is the actor and tsk2 is the target
David Howells3b11a1d2008-11-14 10:39:26 +11001624 * - this uses the default subjective creds of tsk1
David Howells275bb412008-11-14 10:39:19 +11001625 */
1626static int task_has_perm(const struct task_struct *tsk1,
1627 const struct task_struct *tsk2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 u32 perms)
1629{
David Howells275bb412008-11-14 10:39:19 +11001630 const struct task_security_struct *__tsec1, *__tsec2;
1631 u32 sid1, sid2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
David Howells275bb412008-11-14 10:39:19 +11001633 rcu_read_lock();
1634 __tsec1 = __task_cred(tsk1)->security; sid1 = __tsec1->sid;
1635 __tsec2 = __task_cred(tsk2)->security; sid2 = __tsec2->sid;
1636 rcu_read_unlock();
1637 return avc_has_perm(sid1, sid2, SECCLASS_PROCESS, perms, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638}
1639
David Howells3b11a1d2008-11-14 10:39:26 +11001640/*
1641 * Check permission between current and another task, e.g. signal checks,
1642 * fork check, ptrace check, etc.
1643 * current is the actor and tsk2 is the target
1644 * - this uses current's subjective creds
1645 */
1646static int current_has_perm(const struct task_struct *tsk,
1647 u32 perms)
1648{
1649 u32 sid, tsid;
1650
1651 sid = current_sid();
1652 tsid = task_sid(tsk);
1653 return avc_has_perm(sid, tsid, SECCLASS_PROCESS, perms, NULL);
1654}
1655
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001656#if CAP_LAST_CAP > 63
1657#error Fix SELinux to handle capabilities > 63.
1658#endif
1659
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660/* Check whether a task is allowed to use a capability. */
Eric Paris6a9de492012-01-03 12:25:14 -05001661static int cred_has_capability(const struct cred *cred,
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04001662 int cap, int audit, bool initns)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663{
Thomas Liu2bf49692009-07-14 12:14:09 -04001664 struct common_audit_data ad;
Eric Paris06112162008-11-11 22:02:50 +11001665 struct av_decision avd;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001666 u16 sclass;
David Howells3699c532009-01-06 22:27:01 +00001667 u32 sid = cred_sid(cred);
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001668 u32 av = CAP_TO_MASK(cap);
Eric Paris06112162008-11-11 22:02:50 +11001669 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
Eric Paris50c205f2012-04-04 15:01:43 -04001671 ad.type = LSM_AUDIT_DATA_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 ad.u.cap = cap;
1673
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001674 switch (CAP_TO_INDEX(cap)) {
1675 case 0:
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04001676 sclass = initns ? SECCLASS_CAPABILITY : SECCLASS_CAP_USERNS;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001677 break;
1678 case 1:
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04001679 sclass = initns ? SECCLASS_CAPABILITY2 : SECCLASS_CAP2_USERNS;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001680 break;
1681 default:
1682 printk(KERN_ERR
1683 "SELinux: out of range capability %d\n", cap);
1684 BUG();
Eric Parisa35c6c82011-04-20 10:21:28 -04001685 return -EINVAL;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001686 }
Eric Paris06112162008-11-11 22:02:50 +11001687
David Howells275bb412008-11-14 10:39:19 +11001688 rc = avc_has_perm_noaudit(sid, sid, sclass, av, 0, &avd);
Eric Paris9ade0cf2011-04-25 16:26:29 -04001689 if (audit == SECURITY_CAP_AUDIT) {
NeilBrown7b20ea22015-03-23 13:37:39 +11001690 int rc2 = avc_audit(sid, sid, sclass, av, &avd, rc, &ad, 0);
Eric Paris9ade0cf2011-04-25 16:26:29 -04001691 if (rc2)
1692 return rc2;
1693 }
Eric Paris06112162008-11-11 22:02:50 +11001694 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695}
1696
1697/* Check whether a task is allowed to use a system operation. */
1698static int task_has_system(struct task_struct *tsk,
1699 u32 perms)
1700{
David Howells275bb412008-11-14 10:39:19 +11001701 u32 sid = task_sid(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
David Howells275bb412008-11-14 10:39:19 +11001703 return avc_has_perm(sid, SECINITSID_KERNEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 SECCLASS_SYSTEM, perms, NULL);
1705}
1706
1707/* Check whether a task has a particular permission to an inode.
1708 The 'adp' parameter is optional and allows other audit
1709 data to be passed (e.g. the dentry). */
David Howells88e67f32008-11-14 10:39:21 +11001710static int inode_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 struct inode *inode,
1712 u32 perms,
Linus Torvalds19e49832013-10-04 12:54:11 -07001713 struct common_audit_data *adp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 struct inode_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +11001716 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
David Howellse0e81732009-09-02 09:13:40 +01001718 validate_creds(cred);
1719
Eric Paris828dfe12008-04-17 13:17:49 -04001720 if (unlikely(IS_PRIVATE(inode)))
Stephen Smalleybbaca6c2007-02-14 00:34:16 -08001721 return 0;
1722
David Howells88e67f32008-11-14 10:39:21 +11001723 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 isec = inode->i_security;
1725
Linus Torvalds19e49832013-10-04 12:54:11 -07001726 return avc_has_perm(sid, isec->sid, isec->sclass, perms, adp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727}
1728
1729/* Same as inode_has_perm, but pass explicit audit data containing
1730 the dentry to help the auditing code to more easily generate the
1731 pathname if needed. */
David Howells88e67f32008-11-14 10:39:21 +11001732static inline int dentry_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 struct dentry *dentry,
1734 u32 av)
1735{
David Howellsc6f493d2015-03-17 22:26:22 +00001736 struct inode *inode = d_backing_inode(dentry);
Thomas Liu2bf49692009-07-14 12:14:09 -04001737 struct common_audit_data ad;
David Howells88e67f32008-11-14 10:39:21 +11001738
Eric Paris50c205f2012-04-04 15:01:43 -04001739 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Paris2875fa02011-04-28 16:04:24 -04001740 ad.u.dentry = dentry;
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05001741 __inode_security_revalidate(inode, dentry, true);
Linus Torvalds19e49832013-10-04 12:54:11 -07001742 return inode_has_perm(cred, inode, av, &ad);
Eric Paris2875fa02011-04-28 16:04:24 -04001743}
1744
1745/* Same as inode_has_perm, but pass explicit audit data containing
1746 the path to help the auditing code to more easily generate the
1747 pathname if needed. */
1748static inline int path_has_perm(const struct cred *cred,
Al Viro3f7036a2015-03-08 19:28:30 -04001749 const struct path *path,
Eric Paris2875fa02011-04-28 16:04:24 -04001750 u32 av)
1751{
David Howellsc6f493d2015-03-17 22:26:22 +00001752 struct inode *inode = d_backing_inode(path->dentry);
Eric Paris2875fa02011-04-28 16:04:24 -04001753 struct common_audit_data ad;
1754
Eric Paris50c205f2012-04-04 15:01:43 -04001755 ad.type = LSM_AUDIT_DATA_PATH;
Eric Paris2875fa02011-04-28 16:04:24 -04001756 ad.u.path = *path;
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05001757 __inode_security_revalidate(inode, path->dentry, true);
Linus Torvalds19e49832013-10-04 12:54:11 -07001758 return inode_has_perm(cred, inode, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759}
1760
David Howells13f8e982013-06-13 23:37:55 +01001761/* Same as path_has_perm, but uses the inode from the file struct. */
1762static inline int file_path_has_perm(const struct cred *cred,
1763 struct file *file,
1764 u32 av)
1765{
1766 struct common_audit_data ad;
1767
Vivek Goyal43af5de2016-09-09 11:37:49 -04001768 ad.type = LSM_AUDIT_DATA_FILE;
1769 ad.u.file = file;
Linus Torvalds19e49832013-10-04 12:54:11 -07001770 return inode_has_perm(cred, file_inode(file), av, &ad);
David Howells13f8e982013-06-13 23:37:55 +01001771}
1772
Chenbo Feng0521e0b2017-10-18 13:00:26 -07001773#ifdef CONFIG_BPF_SYSCALL
1774static int bpf_fd_pass(struct file *file, u32 sid);
1775#endif
1776
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777/* Check whether a task can use an open file descriptor to
1778 access an inode in a given way. Check access to the
1779 descriptor itself, and then use dentry_has_perm to
1780 check a particular permission to the file.
1781 Access to the descriptor is implicitly granted if it
1782 has the same SID as the process. If av is zero, then
1783 access to the file is not checked, e.g. for cases
1784 where only the descriptor is affected like seek. */
David Howells88e67f32008-11-14 10:39:21 +11001785static int file_has_perm(const struct cred *cred,
1786 struct file *file,
1787 u32 av)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 struct file_security_struct *fsec = file->f_security;
Al Viro496ad9a2013-01-23 17:07:38 -05001790 struct inode *inode = file_inode(file);
Thomas Liu2bf49692009-07-14 12:14:09 -04001791 struct common_audit_data ad;
David Howells88e67f32008-11-14 10:39:21 +11001792 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 int rc;
1794
Vivek Goyal43af5de2016-09-09 11:37:49 -04001795 ad.type = LSM_AUDIT_DATA_FILE;
1796 ad.u.file = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
David Howells275bb412008-11-14 10:39:19 +11001798 if (sid != fsec->sid) {
1799 rc = avc_has_perm(sid, fsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 SECCLASS_FD,
1801 FD__USE,
1802 &ad);
1803 if (rc)
David Howells88e67f32008-11-14 10:39:21 +11001804 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 }
1806
Chenbo Feng0521e0b2017-10-18 13:00:26 -07001807#ifdef CONFIG_BPF_SYSCALL
1808 rc = bpf_fd_pass(file, cred_sid(cred));
1809 if (rc)
1810 return rc;
1811#endif
1812
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 /* av is zero if only checking access to the descriptor. */
David Howells88e67f32008-11-14 10:39:21 +11001814 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 if (av)
Linus Torvalds19e49832013-10-04 12:54:11 -07001816 rc = inode_has_perm(cred, inode, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
David Howells88e67f32008-11-14 10:39:21 +11001818out:
1819 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820}
1821
David Howellsc3c188b2015-07-10 17:19:58 -04001822/*
1823 * Determine the label for an inode that might be unioned.
1824 */
Vivek Goyalc957f6d2016-07-13 10:44:51 -04001825static int
1826selinux_determine_inode_label(const struct task_security_struct *tsec,
1827 struct inode *dir,
1828 const struct qstr *name, u16 tclass,
1829 u32 *_new_isid)
David Howellsc3c188b2015-07-10 17:19:58 -04001830{
1831 const struct superblock_security_struct *sbsec = dir->i_sb->s_security;
David Howellsc3c188b2015-07-10 17:19:58 -04001832
1833 if ((sbsec->flags & SE_SBINITIALIZED) &&
1834 (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)) {
1835 *_new_isid = sbsec->mntpoint_sid;
1836 } else if ((sbsec->flags & SBLABEL_MNT) &&
1837 tsec->create_sid) {
1838 *_new_isid = tsec->create_sid;
1839 } else {
Paul Moore20cdef82016-04-04 14:14:42 -04001840 const struct inode_security_struct *dsec = inode_security(dir);
David Howellsc3c188b2015-07-10 17:19:58 -04001841 return security_transition_sid(tsec->sid, dsec->sid, tclass,
1842 name, _new_isid);
1843 }
1844
1845 return 0;
1846}
1847
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848/* Check whether a task can create a file. */
1849static int may_create(struct inode *dir,
1850 struct dentry *dentry,
1851 u16 tclass)
1852{
Paul Moore5fb49872010-04-22 14:46:19 -04001853 const struct task_security_struct *tsec = current_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 struct inode_security_struct *dsec;
1855 struct superblock_security_struct *sbsec;
David Howells275bb412008-11-14 10:39:19 +11001856 u32 sid, newsid;
Thomas Liu2bf49692009-07-14 12:14:09 -04001857 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 int rc;
1859
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001860 dsec = inode_security(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 sbsec = dir->i_sb->s_security;
1862
David Howells275bb412008-11-14 10:39:19 +11001863 sid = tsec->sid;
David Howells275bb412008-11-14 10:39:19 +11001864
Eric Paris50c205f2012-04-04 15:01:43 -04001865 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04001866 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
David Howells275bb412008-11-14 10:39:19 +11001868 rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 DIR__ADD_NAME | DIR__SEARCH,
1870 &ad);
1871 if (rc)
1872 return rc;
1873
Vivek Goyalc957f6d2016-07-13 10:44:51 -04001874 rc = selinux_determine_inode_label(current_security(), dir,
1875 &dentry->d_name, tclass, &newsid);
David Howellsc3c188b2015-07-10 17:19:58 -04001876 if (rc)
1877 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
David Howells275bb412008-11-14 10:39:19 +11001879 rc = avc_has_perm(sid, newsid, tclass, FILE__CREATE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 if (rc)
1881 return rc;
1882
1883 return avc_has_perm(newsid, sbsec->sid,
1884 SECCLASS_FILESYSTEM,
1885 FILESYSTEM__ASSOCIATE, &ad);
1886}
1887
Michael LeMay4eb582c2006-06-26 00:24:57 -07001888/* Check whether a task can create a key. */
1889static int may_create_key(u32 ksid,
1890 struct task_struct *ctx)
1891{
David Howells275bb412008-11-14 10:39:19 +11001892 u32 sid = task_sid(ctx);
Michael LeMay4eb582c2006-06-26 00:24:57 -07001893
David Howells275bb412008-11-14 10:39:19 +11001894 return avc_has_perm(sid, ksid, SECCLASS_KEY, KEY__CREATE, NULL);
Michael LeMay4eb582c2006-06-26 00:24:57 -07001895}
1896
Eric Paris828dfe12008-04-17 13:17:49 -04001897#define MAY_LINK 0
1898#define MAY_UNLINK 1
1899#define MAY_RMDIR 2
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
1901/* Check whether a task can link, unlink, or rmdir a file/directory. */
1902static int may_link(struct inode *dir,
1903 struct dentry *dentry,
1904 int kind)
1905
1906{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 struct inode_security_struct *dsec, *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001908 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11001909 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 u32 av;
1911 int rc;
1912
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001913 dsec = inode_security(dir);
1914 isec = backing_inode_security(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
Eric Paris50c205f2012-04-04 15:01:43 -04001916 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04001917 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
1919 av = DIR__SEARCH;
1920 av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
David Howells275bb412008-11-14 10:39:19 +11001921 rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 if (rc)
1923 return rc;
1924
1925 switch (kind) {
1926 case MAY_LINK:
1927 av = FILE__LINK;
1928 break;
1929 case MAY_UNLINK:
1930 av = FILE__UNLINK;
1931 break;
1932 case MAY_RMDIR:
1933 av = DIR__RMDIR;
1934 break;
1935 default:
Eric Paris744ba352008-04-17 11:52:44 -04001936 printk(KERN_WARNING "SELinux: %s: unrecognized kind %d\n",
1937 __func__, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 return 0;
1939 }
1940
David Howells275bb412008-11-14 10:39:19 +11001941 rc = avc_has_perm(sid, isec->sid, isec->sclass, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 return rc;
1943}
1944
1945static inline int may_rename(struct inode *old_dir,
1946 struct dentry *old_dentry,
1947 struct inode *new_dir,
1948 struct dentry *new_dentry)
1949{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001951 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11001952 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 u32 av;
1954 int old_is_dir, new_is_dir;
1955 int rc;
1956
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001957 old_dsec = inode_security(old_dir);
1958 old_isec = backing_inode_security(old_dentry);
David Howellse36cb0b2015-01-29 12:02:35 +00001959 old_is_dir = d_is_dir(old_dentry);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001960 new_dsec = inode_security(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
Eric Paris50c205f2012-04-04 15:01:43 -04001962 ad.type = LSM_AUDIT_DATA_DENTRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
Eric Parisa2694342011-04-25 13:10:27 -04001964 ad.u.dentry = old_dentry;
David Howells275bb412008-11-14 10:39:19 +11001965 rc = avc_has_perm(sid, old_dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 DIR__REMOVE_NAME | DIR__SEARCH, &ad);
1967 if (rc)
1968 return rc;
David Howells275bb412008-11-14 10:39:19 +11001969 rc = avc_has_perm(sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 old_isec->sclass, FILE__RENAME, &ad);
1971 if (rc)
1972 return rc;
1973 if (old_is_dir && new_dir != old_dir) {
David Howells275bb412008-11-14 10:39:19 +11001974 rc = avc_has_perm(sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 old_isec->sclass, DIR__REPARENT, &ad);
1976 if (rc)
1977 return rc;
1978 }
1979
Eric Parisa2694342011-04-25 13:10:27 -04001980 ad.u.dentry = new_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 av = DIR__ADD_NAME | DIR__SEARCH;
David Howells2c616d42015-01-29 12:02:33 +00001982 if (d_is_positive(new_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 av |= DIR__REMOVE_NAME;
David Howells275bb412008-11-14 10:39:19 +11001984 rc = avc_has_perm(sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 if (rc)
1986 return rc;
David Howells2c616d42015-01-29 12:02:33 +00001987 if (d_is_positive(new_dentry)) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001988 new_isec = backing_inode_security(new_dentry);
David Howellse36cb0b2015-01-29 12:02:35 +00001989 new_is_dir = d_is_dir(new_dentry);
David Howells275bb412008-11-14 10:39:19 +11001990 rc = avc_has_perm(sid, new_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 new_isec->sclass,
1992 (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
1993 if (rc)
1994 return rc;
1995 }
1996
1997 return 0;
1998}
1999
2000/* Check whether a task can perform a filesystem operation. */
David Howells88e67f32008-11-14 10:39:21 +11002001static int superblock_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 struct super_block *sb,
2003 u32 perms,
Thomas Liu2bf49692009-07-14 12:14:09 -04002004 struct common_audit_data *ad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 struct superblock_security_struct *sbsec;
David Howells88e67f32008-11-14 10:39:21 +11002007 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 sbsec = sb->s_security;
David Howells275bb412008-11-14 10:39:19 +11002010 return avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011}
2012
2013/* Convert a Linux mode and permission mask to an access vector. */
2014static inline u32 file_mask_to_av(int mode, int mask)
2015{
2016 u32 av = 0;
2017
Al Virodba19c62011-07-25 20:49:29 -04002018 if (!S_ISDIR(mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 if (mask & MAY_EXEC)
2020 av |= FILE__EXECUTE;
2021 if (mask & MAY_READ)
2022 av |= FILE__READ;
2023
2024 if (mask & MAY_APPEND)
2025 av |= FILE__APPEND;
2026 else if (mask & MAY_WRITE)
2027 av |= FILE__WRITE;
2028
2029 } else {
2030 if (mask & MAY_EXEC)
2031 av |= DIR__SEARCH;
2032 if (mask & MAY_WRITE)
2033 av |= DIR__WRITE;
2034 if (mask & MAY_READ)
2035 av |= DIR__READ;
2036 }
2037
2038 return av;
2039}
2040
2041/* Convert a Linux file to an access vector. */
2042static inline u32 file_to_av(struct file *file)
2043{
2044 u32 av = 0;
2045
2046 if (file->f_mode & FMODE_READ)
2047 av |= FILE__READ;
2048 if (file->f_mode & FMODE_WRITE) {
2049 if (file->f_flags & O_APPEND)
2050 av |= FILE__APPEND;
2051 else
2052 av |= FILE__WRITE;
2053 }
Stephen Smalley0794c662008-03-17 08:55:18 -04002054 if (!av) {
2055 /*
2056 * Special file opened with flags 3 for ioctl-only use.
2057 */
2058 av = FILE__IOCTL;
2059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
2061 return av;
2062}
2063
Eric Paris8b6a5a32008-10-29 17:06:46 -04002064/*
2065 * Convert a file to an access vector and include the correct open
2066 * open permission.
2067 */
2068static inline u32 open_file_to_av(struct file *file)
2069{
2070 u32 av = file_to_av(file);
Stephen Smalleyb983b2a2017-05-12 12:41:24 -04002071 struct inode *inode = file_inode(file);
Eric Paris8b6a5a32008-10-29 17:06:46 -04002072
Stephen Smalleyb983b2a2017-05-12 12:41:24 -04002073 if (selinux_policycap_openperm && inode->i_sb->s_magic != SOCKFS_MAGIC)
Eric Paris49b7b8d2010-07-23 11:44:09 -04002074 av |= FILE__OPEN;
2075
Eric Paris8b6a5a32008-10-29 17:06:46 -04002076 return av;
2077}
2078
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079/* Hook functions begin here. */
2080
Stephen Smalley79af7302015-01-21 10:54:10 -05002081static int selinux_binder_set_context_mgr(struct task_struct *mgr)
2082{
2083 u32 mysid = current_sid();
2084 u32 mgrsid = task_sid(mgr);
2085
2086 return avc_has_perm(mysid, mgrsid, SECCLASS_BINDER,
2087 BINDER__SET_CONTEXT_MGR, NULL);
2088}
2089
2090static int selinux_binder_transaction(struct task_struct *from,
2091 struct task_struct *to)
2092{
2093 u32 mysid = current_sid();
2094 u32 fromsid = task_sid(from);
2095 u32 tosid = task_sid(to);
2096 int rc;
2097
2098 if (mysid != fromsid) {
2099 rc = avc_has_perm(mysid, fromsid, SECCLASS_BINDER,
2100 BINDER__IMPERSONATE, NULL);
2101 if (rc)
2102 return rc;
2103 }
2104
2105 return avc_has_perm(fromsid, tosid, SECCLASS_BINDER, BINDER__CALL,
2106 NULL);
2107}
2108
2109static int selinux_binder_transfer_binder(struct task_struct *from,
2110 struct task_struct *to)
2111{
2112 u32 fromsid = task_sid(from);
2113 u32 tosid = task_sid(to);
2114
2115 return avc_has_perm(fromsid, tosid, SECCLASS_BINDER, BINDER__TRANSFER,
2116 NULL);
2117}
2118
2119static int selinux_binder_transfer_file(struct task_struct *from,
2120 struct task_struct *to,
2121 struct file *file)
2122{
2123 u32 sid = task_sid(to);
2124 struct file_security_struct *fsec = file->f_security;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002125 struct dentry *dentry = file->f_path.dentry;
Paul Moore20cdef82016-04-04 14:14:42 -04002126 struct inode_security_struct *isec;
Stephen Smalley79af7302015-01-21 10:54:10 -05002127 struct common_audit_data ad;
2128 int rc;
2129
2130 ad.type = LSM_AUDIT_DATA_PATH;
2131 ad.u.path = file->f_path;
2132
2133 if (sid != fsec->sid) {
2134 rc = avc_has_perm(sid, fsec->sid,
2135 SECCLASS_FD,
2136 FD__USE,
2137 &ad);
2138 if (rc)
2139 return rc;
2140 }
2141
Chenbo Feng0521e0b2017-10-18 13:00:26 -07002142#ifdef CONFIG_BPF_SYSCALL
2143 rc = bpf_fd_pass(file, sid);
2144 if (rc)
2145 return rc;
2146#endif
2147
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002148 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
Stephen Smalley79af7302015-01-21 10:54:10 -05002149 return 0;
2150
Paul Moore20cdef82016-04-04 14:14:42 -04002151 isec = backing_inode_security(dentry);
Stephen Smalley79af7302015-01-21 10:54:10 -05002152 return avc_has_perm(sid, isec->sid, isec->sclass, file_to_av(file),
2153 &ad);
2154}
2155
Ingo Molnar9e488582009-05-07 19:26:19 +10002156static int selinux_ptrace_access_check(struct task_struct *child,
David Howells5cd9c582008-08-14 11:37:28 +01002157 unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158{
Eric Paris69f594a2012-01-03 12:25:15 -05002159 if (mode & PTRACE_MODE_READ) {
David Howells275bb412008-11-14 10:39:19 +11002160 u32 sid = current_sid();
2161 u32 csid = task_sid(child);
2162 return avc_has_perm(sid, csid, SECCLASS_FILE, FILE__READ, NULL);
Stephen Smalley006ebb42008-05-19 08:32:49 -04002163 }
2164
David Howells3b11a1d2008-11-14 10:39:26 +11002165 return current_has_perm(child, PROCESS__PTRACE);
David Howells5cd9c582008-08-14 11:37:28 +01002166}
2167
2168static int selinux_ptrace_traceme(struct task_struct *parent)
2169{
David Howells5cd9c582008-08-14 11:37:28 +01002170 return task_has_perm(parent, current, PROCESS__PTRACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171}
2172
2173static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
Eric Paris828dfe12008-04-17 13:17:49 -04002174 kernel_cap_t *inheritable, kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002176 return current_has_perm(target, PROCESS__GETCAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177}
2178
David Howellsd84f4f92008-11-14 10:39:23 +11002179static int selinux_capset(struct cred *new, const struct cred *old,
2180 const kernel_cap_t *effective,
2181 const kernel_cap_t *inheritable,
2182 const kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183{
David Howellsd84f4f92008-11-14 10:39:23 +11002184 return cred_has_perm(old, new, PROCESS__SETCAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185}
2186
James Morris5626d3e2009-01-30 10:05:06 +11002187/*
2188 * (This comment used to live with the selinux_task_setuid hook,
2189 * which was removed).
2190 *
2191 * Since setuid only affects the current process, and since the SELinux
2192 * controls are not based on the Linux identity attributes, SELinux does not
2193 * need to control this operation. However, SELinux does control the use of
2194 * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
2195 */
2196
Eric Paris6a9de492012-01-03 12:25:14 -05002197static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
2198 int cap, int audit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199{
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04002200 return cred_has_capability(cred, cap, audit, ns == &init_user_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201}
2202
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
2204{
David Howells88e67f32008-11-14 10:39:21 +11002205 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 int rc = 0;
2207
2208 if (!sb)
2209 return 0;
2210
2211 switch (cmds) {
Eric Paris828dfe12008-04-17 13:17:49 -04002212 case Q_SYNC:
2213 case Q_QUOTAON:
2214 case Q_QUOTAOFF:
2215 case Q_SETINFO:
2216 case Q_SETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11002217 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04002218 break;
2219 case Q_GETFMT:
2220 case Q_GETINFO:
2221 case Q_GETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11002222 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04002223 break;
2224 default:
2225 rc = 0; /* let the kernel handle invalid cmds */
2226 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 }
2228 return rc;
2229}
2230
2231static int selinux_quota_on(struct dentry *dentry)
2232{
David Howells88e67f32008-11-14 10:39:21 +11002233 const struct cred *cred = current_cred();
2234
Eric Paris2875fa02011-04-28 16:04:24 -04002235 return dentry_has_perm(cred, dentry, FILE__QUOTAON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236}
2237
Eric Paris12b30522010-11-15 18:36:29 -05002238static int selinux_syslog(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239{
2240 int rc;
2241
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 switch (type) {
Kees Cookd78ca3c2010-02-03 15:37:13 -08002243 case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */
2244 case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */
Eric Paris828dfe12008-04-17 13:17:49 -04002245 rc = task_has_system(current, SYSTEM__SYSLOG_READ);
2246 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08002247 case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */
2248 case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */
2249 /* Set level of messages printed to console */
2250 case SYSLOG_ACTION_CONSOLE_LEVEL:
Eric Paris828dfe12008-04-17 13:17:49 -04002251 rc = task_has_system(current, SYSTEM__SYSLOG_CONSOLE);
2252 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08002253 case SYSLOG_ACTION_CLOSE: /* Close log */
2254 case SYSLOG_ACTION_OPEN: /* Open log */
2255 case SYSLOG_ACTION_READ: /* Read from log */
2256 case SYSLOG_ACTION_READ_CLEAR: /* Read/clear last kernel messages */
2257 case SYSLOG_ACTION_CLEAR: /* Clear ring buffer */
Eric Paris828dfe12008-04-17 13:17:49 -04002258 default:
2259 rc = task_has_system(current, SYSTEM__SYSLOG_MOD);
2260 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 }
2262 return rc;
2263}
2264
2265/*
2266 * Check that a process has enough memory to allocate a new virtual
2267 * mapping. 0 means there is enough memory for the allocation to
2268 * succeed and -ENOMEM implies there is not.
2269 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 * Do not audit the selinux permission check, as this is applied to all
2271 * processes that allocate mappings.
2272 */
Alan Cox34b4e4a2007-08-22 14:01:28 -07002273static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274{
2275 int rc, cap_sys_admin = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002277 rc = cred_has_capability(current_cred(), CAP_SYS_ADMIN,
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04002278 SECURITY_CAP_NOAUDIT, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 if (rc == 0)
2280 cap_sys_admin = 1;
2281
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002282 return cap_sys_admin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283}
2284
2285/* binprm security operations */
2286
Paul Moore0c6181c2016-03-30 21:41:21 -04002287static u32 ptrace_parent_sid(struct task_struct *task)
2288{
2289 u32 sid = 0;
2290 struct task_struct *tracer;
2291
2292 rcu_read_lock();
2293 tracer = ptrace_parent(task);
2294 if (tracer)
2295 sid = task_sid(tracer);
2296 rcu_read_unlock();
2297
2298 return sid;
2299}
2300
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002301static int check_nnp_nosuid(const struct linux_binprm *bprm,
2302 const struct task_security_struct *old_tsec,
2303 const struct task_security_struct *new_tsec)
2304{
2305 int nnp = (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS);
Andy Lutomirski380cf5b2016-06-23 16:41:05 -05002306 int nosuid = !mnt_may_suid(bprm->file->f_path.mnt);
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002307 int rc;
2308
2309 if (!nnp && !nosuid)
2310 return 0; /* neither NNP nor nosuid */
2311
2312 if (new_tsec->sid == old_tsec->sid)
2313 return 0; /* No change in credentials */
2314
2315 /*
2316 * The only transitions we permit under NNP or nosuid
2317 * are transitions to bounded SIDs, i.e. SIDs that are
2318 * guaranteed to only be allowed a subset of the permissions
2319 * of the current SID.
2320 */
2321 rc = security_bounded_transition(old_tsec->sid, new_tsec->sid);
2322 if (rc) {
2323 /*
2324 * On failure, preserve the errno values for NNP vs nosuid.
2325 * NNP: Operation not permitted for caller.
2326 * nosuid: Permission denied to file.
2327 */
2328 if (nnp)
2329 return -EPERM;
2330 else
2331 return -EACCES;
2332 }
2333 return 0;
2334}
2335
David Howellsa6f76f22008-11-14 10:39:24 +11002336static int selinux_bprm_set_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337{
David Howellsa6f76f22008-11-14 10:39:24 +11002338 const struct task_security_struct *old_tsec;
2339 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 struct inode_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04002341 struct common_audit_data ad;
Al Viro496ad9a2013-01-23 17:07:38 -05002342 struct inode *inode = file_inode(bprm->file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 int rc;
2344
David Howellsa6f76f22008-11-14 10:39:24 +11002345 /* SELinux context only depends on initial program or script and not
2346 * the script interpreter */
2347 if (bprm->cred_prepared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 return 0;
2349
David Howellsa6f76f22008-11-14 10:39:24 +11002350 old_tsec = current_security();
2351 new_tsec = bprm->cred->security;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002352 isec = inode_security(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353
2354 /* Default to the current task SID. */
David Howellsa6f76f22008-11-14 10:39:24 +11002355 new_tsec->sid = old_tsec->sid;
2356 new_tsec->osid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357
Michael LeMay28eba5b2006-06-27 02:53:42 -07002358 /* Reset fs, key, and sock SIDs on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002359 new_tsec->create_sid = 0;
2360 new_tsec->keycreate_sid = 0;
2361 new_tsec->sockcreate_sid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362
David Howellsa6f76f22008-11-14 10:39:24 +11002363 if (old_tsec->exec_sid) {
2364 new_tsec->sid = old_tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 /* Reset exec SID on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002366 new_tsec->exec_sid = 0;
Andy Lutomirski259e5e62012-04-12 16:47:50 -05002367
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002368 /* Fail on NNP or nosuid if not an allowed transition. */
2369 rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
2370 if (rc)
2371 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 } else {
2373 /* Check for a default transition on this program. */
David Howellsa6f76f22008-11-14 10:39:24 +11002374 rc = security_transition_sid(old_tsec->sid, isec->sid,
Eric Paris652bb9b2011-02-01 11:05:40 -05002375 SECCLASS_PROCESS, NULL,
2376 &new_tsec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 if (rc)
2378 return rc;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002379
2380 /*
2381 * Fallback to old SID on NNP or nosuid if not an allowed
2382 * transition.
2383 */
2384 rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
2385 if (rc)
2386 new_tsec->sid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 }
2388
Vivek Goyal43af5de2016-09-09 11:37:49 -04002389 ad.type = LSM_AUDIT_DATA_FILE;
2390 ad.u.file = bprm->file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
David Howellsa6f76f22008-11-14 10:39:24 +11002392 if (new_tsec->sid == old_tsec->sid) {
2393 rc = avc_has_perm(old_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
2395 if (rc)
2396 return rc;
2397 } else {
2398 /* Check permissions for the transition. */
David Howellsa6f76f22008-11-14 10:39:24 +11002399 rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
2401 if (rc)
2402 return rc;
2403
David Howellsa6f76f22008-11-14 10:39:24 +11002404 rc = avc_has_perm(new_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
2406 if (rc)
2407 return rc;
2408
David Howellsa6f76f22008-11-14 10:39:24 +11002409 /* Check for shared state */
2410 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
2411 rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
2412 SECCLASS_PROCESS, PROCESS__SHARE,
2413 NULL);
2414 if (rc)
2415 return -EPERM;
2416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417
David Howellsa6f76f22008-11-14 10:39:24 +11002418 /* Make sure that anyone attempting to ptrace over a task that
2419 * changes its SID has the appropriate permit */
2420 if (bprm->unsafe &
2421 (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
Paul Moore0c6181c2016-03-30 21:41:21 -04002422 u32 ptsid = ptrace_parent_sid(current);
David Howellsa6f76f22008-11-14 10:39:24 +11002423 if (ptsid != 0) {
2424 rc = avc_has_perm(ptsid, new_tsec->sid,
2425 SECCLASS_PROCESS,
2426 PROCESS__PTRACE, NULL);
2427 if (rc)
2428 return -EPERM;
2429 }
2430 }
2431
2432 /* Clear any possibly unsafe personality bits on exec: */
2433 bprm->per_clear |= PER_CLEAR_ON_SETID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 }
2435
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 return 0;
2437}
2438
Eric Paris828dfe12008-04-17 13:17:49 -04002439static int selinux_bprm_secureexec(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440{
Paul Moore5fb49872010-04-22 14:46:19 -04002441 const struct task_security_struct *tsec = current_security();
David Howells275bb412008-11-14 10:39:19 +11002442 u32 sid, osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 int atsecure = 0;
2444
David Howells275bb412008-11-14 10:39:19 +11002445 sid = tsec->sid;
2446 osid = tsec->osid;
2447
2448 if (osid != sid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 /* Enable secure mode for SIDs transitions unless
2450 the noatsecure permission is granted between
2451 the two SIDs, i.e. ahp returns 0. */
David Howells275bb412008-11-14 10:39:19 +11002452 atsecure = avc_has_perm(osid, sid,
David Howellsa6f76f22008-11-14 10:39:24 +11002453 SECCLASS_PROCESS,
2454 PROCESS__NOATSECURE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 }
2456
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002457 return !!atsecure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458}
2459
Al Viroc3c073f2012-08-21 22:32:06 -04002460static int match_file(const void *p, struct file *file, unsigned fd)
2461{
2462 return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0;
2463}
2464
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465/* Derived from fs/exec.c:flush_old_files. */
David Howells745ca242008-11-14 10:39:22 +11002466static inline void flush_unauthorized_files(const struct cred *cred,
2467 struct files_struct *files)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 struct file *file, *devnull = NULL;
Stephen Smalleyb20c8122006-09-25 23:32:03 -07002470 struct tty_struct *tty;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002471 int drop_tty = 0;
Al Viroc3c073f2012-08-21 22:32:06 -04002472 unsigned n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002474 tty = get_current_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 if (tty) {
Peter Hurley4a510962016-01-09 21:35:23 -08002476 spin_lock(&tty->files_lock);
Eric Paris37dd0bd2008-10-31 17:40:00 -04002477 if (!list_empty(&tty->tty_files)) {
Nick Piggind996b622010-08-18 04:37:36 +10002478 struct tty_file_private *file_priv;
Eric Paris37dd0bd2008-10-31 17:40:00 -04002479
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 /* Revalidate access to controlling tty.
David Howells13f8e982013-06-13 23:37:55 +01002481 Use file_path_has_perm on the tty path directly
2482 rather than using file_has_perm, as this particular
2483 open file may belong to another process and we are
2484 only interested in the inode-based check here. */
Nick Piggind996b622010-08-18 04:37:36 +10002485 file_priv = list_first_entry(&tty->tty_files,
2486 struct tty_file_private, list);
2487 file = file_priv->file;
David Howells13f8e982013-06-13 23:37:55 +01002488 if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE))
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002489 drop_tty = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 }
Peter Hurley4a510962016-01-09 21:35:23 -08002491 spin_unlock(&tty->files_lock);
Alan Cox452a00d2008-10-13 10:39:13 +01002492 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 }
Eric W. Biederman98a27ba2007-05-08 00:26:56 -07002494 /* Reset controlling tty. */
2495 if (drop_tty)
2496 no_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
2498 /* Revalidate access to inherited open files. */
Al Viroc3c073f2012-08-21 22:32:06 -04002499 n = iterate_fd(files, 0, match_file, cred);
2500 if (!n) /* none found? */
2501 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502
Al Viroc3c073f2012-08-21 22:32:06 -04002503 devnull = dentry_open(&selinux_null, O_RDWR, cred);
Al Viro45525b22012-10-16 13:30:07 -04002504 if (IS_ERR(devnull))
2505 devnull = NULL;
2506 /* replace all the matching ones with this */
2507 do {
2508 replace_fd(n - 1, devnull, 0);
2509 } while ((n = iterate_fd(files, n, match_file, cred)) != 0);
2510 if (devnull)
Al Viroc3c073f2012-08-21 22:32:06 -04002511 fput(devnull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512}
2513
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514/*
David Howellsa6f76f22008-11-14 10:39:24 +11002515 * Prepare a process for imminent new credential changes due to exec
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 */
David Howellsa6f76f22008-11-14 10:39:24 +11002517static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518{
David Howellsa6f76f22008-11-14 10:39:24 +11002519 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 struct rlimit *rlim, *initrlim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 int rc, i;
2522
David Howellsa6f76f22008-11-14 10:39:24 +11002523 new_tsec = bprm->cred->security;
2524 if (new_tsec->sid == new_tsec->osid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 return;
2526
2527 /* Close files for which the new task SID is not authorized. */
David Howellsa6f76f22008-11-14 10:39:24 +11002528 flush_unauthorized_files(bprm->cred, current->files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529
David Howellsa6f76f22008-11-14 10:39:24 +11002530 /* Always clear parent death signal on SID transitions. */
2531 current->pdeath_signal = 0;
2532
2533 /* Check whether the new SID can inherit resource limits from the old
2534 * SID. If not, reset all soft limits to the lower of the current
2535 * task's hard limit and the init task's soft limit.
2536 *
2537 * Note that the setting of hard limits (even to lower them) can be
2538 * controlled by the setrlimit check. The inclusion of the init task's
2539 * soft limit into the computation is to avoid resetting soft limits
2540 * higher than the default soft limit for cases where the default is
2541 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2542 */
2543 rc = avc_has_perm(new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
2544 PROCESS__RLIMITINH, NULL);
2545 if (rc) {
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002546 /* protect against do_prlimit() */
2547 task_lock(current);
David Howellsa6f76f22008-11-14 10:39:24 +11002548 for (i = 0; i < RLIM_NLIMITS; i++) {
2549 rlim = current->signal->rlim + i;
2550 initrlim = init_task.signal->rlim + i;
2551 rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
2552 }
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002553 task_unlock(current);
2554 update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
David Howellsa6f76f22008-11-14 10:39:24 +11002555 }
2556}
2557
2558/*
2559 * Clean up the process immediately after the installation of new credentials
2560 * due to exec
2561 */
2562static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
2563{
2564 const struct task_security_struct *tsec = current_security();
2565 struct itimerval itimer;
David Howellsa6f76f22008-11-14 10:39:24 +11002566 u32 osid, sid;
2567 int rc, i;
David Howellsa6f76f22008-11-14 10:39:24 +11002568
David Howellsa6f76f22008-11-14 10:39:24 +11002569 osid = tsec->osid;
2570 sid = tsec->sid;
2571
2572 if (sid == osid)
2573 return;
2574
2575 /* Check whether the new SID can inherit signal state from the old SID.
2576 * If not, clear itimers to avoid subsequent signal generation and
2577 * flush and unblock signals.
2578 *
2579 * This must occur _after_ the task SID has been updated so that any
2580 * kill done after the flush will be checked against the new SID.
2581 */
2582 rc = avc_has_perm(osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 if (rc) {
2584 memset(&itimer, 0, sizeof itimer);
2585 for (i = 0; i < 3; i++)
2586 do_setitimer(i, &itimer, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterov9e7c8f82015-06-04 16:22:16 -04002588 if (!fatal_signal_pending(current)) {
2589 flush_sigqueue(&current->pending);
2590 flush_sigqueue(&current->signal->shared_pending);
David Howells3bcac022009-04-29 13:45:05 +01002591 flush_signal_handlers(current, 1);
2592 sigemptyset(&current->blocked);
Oleg Nesterov9e7c8f82015-06-04 16:22:16 -04002593 recalc_sigpending();
David Howells3bcac022009-04-29 13:45:05 +01002594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 spin_unlock_irq(&current->sighand->siglock);
2596 }
2597
David Howellsa6f76f22008-11-14 10:39:24 +11002598 /* Wake up the parent if it is waiting so that it can recheck
2599 * wait permission to the new task SID. */
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002600 read_lock(&tasklist_lock);
Oleg Nesterov0b7570e2009-09-23 15:56:46 -07002601 __wake_up_parent(current, current->real_parent);
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002602 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603}
2604
2605/* superblock security operations */
2606
2607static int selinux_sb_alloc_security(struct super_block *sb)
2608{
2609 return superblock_alloc_security(sb);
2610}
2611
2612static void selinux_sb_free_security(struct super_block *sb)
2613{
2614 superblock_free_security(sb);
2615}
2616
2617static inline int match_prefix(char *prefix, int plen, char *option, int olen)
2618{
2619 if (plen > olen)
2620 return 0;
2621
2622 return !memcmp(prefix, option, plen);
2623}
2624
2625static inline int selinux_option(char *option, int len)
2626{
Eric Paris832cbd92008-04-01 13:24:09 -04002627 return (match_prefix(CONTEXT_STR, sizeof(CONTEXT_STR)-1, option, len) ||
2628 match_prefix(FSCONTEXT_STR, sizeof(FSCONTEXT_STR)-1, option, len) ||
2629 match_prefix(DEFCONTEXT_STR, sizeof(DEFCONTEXT_STR)-1, option, len) ||
David P. Quigley11689d42009-01-16 09:22:03 -05002630 match_prefix(ROOTCONTEXT_STR, sizeof(ROOTCONTEXT_STR)-1, option, len) ||
2631 match_prefix(LABELSUPP_STR, sizeof(LABELSUPP_STR)-1, option, len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632}
2633
2634static inline void take_option(char **to, char *from, int *first, int len)
2635{
2636 if (!*first) {
2637 **to = ',';
2638 *to += 1;
Cory Olmo3528a952006-09-29 01:58:44 -07002639 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 *first = 0;
2641 memcpy(*to, from, len);
2642 *to += len;
2643}
2644
Eric Paris828dfe12008-04-17 13:17:49 -04002645static inline void take_selinux_option(char **to, char *from, int *first,
2646 int len)
Cory Olmo3528a952006-09-29 01:58:44 -07002647{
2648 int current_size = 0;
2649
2650 if (!*first) {
2651 **to = '|';
2652 *to += 1;
Eric Paris828dfe12008-04-17 13:17:49 -04002653 } else
Cory Olmo3528a952006-09-29 01:58:44 -07002654 *first = 0;
2655
2656 while (current_size < len) {
2657 if (*from != '"') {
2658 **to = *from;
2659 *to += 1;
2660 }
2661 from += 1;
2662 current_size += 1;
2663 }
2664}
2665
Eric Parise0007522008-03-05 10:31:54 -05002666static int selinux_sb_copy_data(char *orig, char *copy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667{
2668 int fnosec, fsec, rc = 0;
2669 char *in_save, *in_curr, *in_end;
2670 char *sec_curr, *nosec_save, *nosec;
Cory Olmo3528a952006-09-29 01:58:44 -07002671 int open_quote = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672
2673 in_curr = orig;
2674 sec_curr = copy;
2675
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 nosec = (char *)get_zeroed_page(GFP_KERNEL);
2677 if (!nosec) {
2678 rc = -ENOMEM;
2679 goto out;
2680 }
2681
2682 nosec_save = nosec;
2683 fnosec = fsec = 1;
2684 in_save = in_end = orig;
2685
2686 do {
Cory Olmo3528a952006-09-29 01:58:44 -07002687 if (*in_end == '"')
2688 open_quote = !open_quote;
2689 if ((*in_end == ',' && open_quote == 0) ||
2690 *in_end == '\0') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 int len = in_end - in_curr;
2692
2693 if (selinux_option(in_curr, len))
Cory Olmo3528a952006-09-29 01:58:44 -07002694 take_selinux_option(&sec_curr, in_curr, &fsec, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 else
2696 take_option(&nosec, in_curr, &fnosec, len);
2697
2698 in_curr = in_end + 1;
2699 }
2700 } while (*in_end++);
2701
Eric Paris6931dfc2005-06-30 02:58:51 -07002702 strcpy(in_save, nosec_save);
Gerald Schaeferda3caa22005-06-21 17:15:18 -07002703 free_page((unsigned long)nosec_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704out:
2705 return rc;
2706}
2707
Eric Paris026eb162011-03-03 16:09:14 -05002708static int selinux_sb_remount(struct super_block *sb, void *data)
2709{
2710 int rc, i, *flags;
2711 struct security_mnt_opts opts;
2712 char *secdata, **mount_options;
2713 struct superblock_security_struct *sbsec = sb->s_security;
2714
2715 if (!(sbsec->flags & SE_SBINITIALIZED))
2716 return 0;
2717
2718 if (!data)
2719 return 0;
2720
2721 if (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
2722 return 0;
2723
2724 security_init_mnt_opts(&opts);
2725 secdata = alloc_secdata();
2726 if (!secdata)
2727 return -ENOMEM;
2728 rc = selinux_sb_copy_data(data, secdata);
2729 if (rc)
2730 goto out_free_secdata;
2731
2732 rc = selinux_parse_opts_str(secdata, &opts);
2733 if (rc)
2734 goto out_free_secdata;
2735
2736 mount_options = opts.mnt_opts;
2737 flags = opts.mnt_opts_flags;
2738
2739 for (i = 0; i < opts.num_mnt_opts; i++) {
2740 u32 sid;
Eric Paris026eb162011-03-03 16:09:14 -05002741
Eric Paris12f348b2012-10-09 10:56:25 -04002742 if (flags[i] == SBLABEL_MNT)
Eric Paris026eb162011-03-03 16:09:14 -05002743 continue;
Rasmus Villemoes44be2f62015-10-21 17:44:25 -04002744 rc = security_context_str_to_sid(mount_options[i], &sid, GFP_KERNEL);
Eric Paris026eb162011-03-03 16:09:14 -05002745 if (rc) {
Rasmus Villemoes44be2f62015-10-21 17:44:25 -04002746 printk(KERN_WARNING "SELinux: security_context_str_to_sid"
Linus Torvalds29b1deb2013-12-15 11:17:45 -08002747 "(%s) failed for (dev %s, type %s) errno=%d\n",
2748 mount_options[i], sb->s_id, sb->s_type->name, rc);
Eric Paris026eb162011-03-03 16:09:14 -05002749 goto out_free_opts;
2750 }
2751 rc = -EINVAL;
2752 switch (flags[i]) {
2753 case FSCONTEXT_MNT:
2754 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
2755 goto out_bad_option;
2756 break;
2757 case CONTEXT_MNT:
2758 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
2759 goto out_bad_option;
2760 break;
2761 case ROOTCONTEXT_MNT: {
2762 struct inode_security_struct *root_isec;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002763 root_isec = backing_inode_security(sb->s_root);
Eric Paris026eb162011-03-03 16:09:14 -05002764
2765 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
2766 goto out_bad_option;
2767 break;
2768 }
2769 case DEFCONTEXT_MNT:
2770 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
2771 goto out_bad_option;
2772 break;
2773 default:
2774 goto out_free_opts;
2775 }
2776 }
2777
2778 rc = 0;
2779out_free_opts:
2780 security_free_mnt_opts(&opts);
2781out_free_secdata:
2782 free_secdata(secdata);
2783 return rc;
2784out_bad_option:
2785 printk(KERN_WARNING "SELinux: unable to change security options "
Linus Torvalds29b1deb2013-12-15 11:17:45 -08002786 "during remount (dev %s, type=%s)\n", sb->s_id,
2787 sb->s_type->name);
Eric Paris026eb162011-03-03 16:09:14 -05002788 goto out_free_opts;
2789}
2790
James Morris12204e22008-12-19 10:44:42 +11002791static int selinux_sb_kern_mount(struct super_block *sb, int flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792{
David Howells88e67f32008-11-14 10:39:21 +11002793 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002794 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 int rc;
2796
2797 rc = superblock_doinit(sb, data);
2798 if (rc)
2799 return rc;
2800
James Morris74192242008-12-19 11:41:10 +11002801 /* Allow all mounts performed by the kernel */
Ondrej Mosnacek62044cb2018-11-16 14:12:02 +01002802 if (flags & (MS_KERNMOUNT | MS_SUBMOUNT))
James Morris74192242008-12-19 11:41:10 +11002803 return 0;
2804
Eric Paris50c205f2012-04-04 15:01:43 -04002805 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002806 ad.u.dentry = sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002807 return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808}
2809
David Howells726c3342006-06-23 02:02:58 -07002810static int selinux_sb_statfs(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811{
David Howells88e67f32008-11-14 10:39:21 +11002812 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002813 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814
Eric Paris50c205f2012-04-04 15:01:43 -04002815 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002816 ad.u.dentry = dentry->d_sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002817 return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818}
2819
Al Viro808d4e32012-10-11 11:42:01 -04002820static int selinux_mount(const char *dev_name,
Al Viro8a04c432016-03-25 14:52:53 -04002821 const struct path *path,
Al Viro808d4e32012-10-11 11:42:01 -04002822 const char *type,
Eric Paris828dfe12008-04-17 13:17:49 -04002823 unsigned long flags,
2824 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825{
David Howells88e67f32008-11-14 10:39:21 +11002826 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827
2828 if (flags & MS_REMOUNT)
Al Virod8c95842011-12-07 18:16:57 -05002829 return superblock_has_perm(cred, path->dentry->d_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002830 FILESYSTEM__REMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 else
Eric Paris2875fa02011-04-28 16:04:24 -04002832 return path_has_perm(cred, path, FILE__MOUNTON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833}
2834
2835static int selinux_umount(struct vfsmount *mnt, int flags)
2836{
David Howells88e67f32008-11-14 10:39:21 +11002837 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838
David Howells88e67f32008-11-14 10:39:21 +11002839 return superblock_has_perm(cred, mnt->mnt_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002840 FILESYSTEM__UNMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841}
2842
2843/* inode security operations */
2844
2845static int selinux_inode_alloc_security(struct inode *inode)
2846{
2847 return inode_alloc_security(inode);
2848}
2849
2850static void selinux_inode_free_security(struct inode *inode)
2851{
2852 inode_free_security(inode);
2853}
2854
David Quigleyd47be3d2013-05-22 12:50:34 -04002855static int selinux_dentry_init_security(struct dentry *dentry, int mode,
Al Viro4f3ccd72016-07-20 16:06:15 -04002856 const struct qstr *name, void **ctx,
David Quigleyd47be3d2013-05-22 12:50:34 -04002857 u32 *ctxlen)
2858{
David Quigleyd47be3d2013-05-22 12:50:34 -04002859 u32 newsid;
2860 int rc;
2861
Vivek Goyalc957f6d2016-07-13 10:44:51 -04002862 rc = selinux_determine_inode_label(current_security(),
2863 d_inode(dentry->d_parent), name,
David Howellsc3c188b2015-07-10 17:19:58 -04002864 inode_mode_to_security_class(mode),
2865 &newsid);
2866 if (rc)
2867 return rc;
David Quigleyd47be3d2013-05-22 12:50:34 -04002868
2869 return security_sid_to_context(newsid, (char **)ctx, ctxlen);
2870}
2871
Vivek Goyala518b0a2016-07-13 10:44:53 -04002872static int selinux_dentry_create_files_as(struct dentry *dentry, int mode,
2873 struct qstr *name,
2874 const struct cred *old,
2875 struct cred *new)
2876{
2877 u32 newsid;
2878 int rc;
2879 struct task_security_struct *tsec;
2880
2881 rc = selinux_determine_inode_label(old->security,
2882 d_inode(dentry->d_parent), name,
2883 inode_mode_to_security_class(mode),
2884 &newsid);
2885 if (rc)
2886 return rc;
2887
2888 tsec = new->security;
2889 tsec->create_sid = newsid;
2890 return 0;
2891}
2892
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002893static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +09002894 const struct qstr *qstr,
2895 const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -05002896 void **value, size_t *len)
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002897{
Paul Moore5fb49872010-04-22 14:46:19 -04002898 const struct task_security_struct *tsec = current_security();
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002899 struct superblock_security_struct *sbsec;
David Howells275bb412008-11-14 10:39:19 +11002900 u32 sid, newsid, clen;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002901 int rc;
Tetsuo Handa95489062013-07-25 05:44:02 +09002902 char *context;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002903
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002904 sbsec = dir->i_sb->s_security;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002905
David Howells275bb412008-11-14 10:39:19 +11002906 sid = tsec->sid;
2907 newsid = tsec->create_sid;
2908
Vivek Goyalc957f6d2016-07-13 10:44:51 -04002909 rc = selinux_determine_inode_label(current_security(),
David Howellsc3c188b2015-07-10 17:19:58 -04002910 dir, qstr,
2911 inode_mode_to_security_class(inode->i_mode),
2912 &newsid);
2913 if (rc)
2914 return rc;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002915
Eric Paris296fddf2006-09-25 23:32:00 -07002916 /* Possibly defer initialization to selinux_complete_init. */
David P. Quigley0d90a7e2009-01-16 09:22:02 -05002917 if (sbsec->flags & SE_SBINITIALIZED) {
Eric Paris296fddf2006-09-25 23:32:00 -07002918 struct inode_security_struct *isec = inode->i_security;
2919 isec->sclass = inode_mode_to_security_class(inode->i_mode);
2920 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05002921 isec->initialized = LABEL_INITIALIZED;
Eric Paris296fddf2006-09-25 23:32:00 -07002922 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002923
Eric Paris12f348b2012-10-09 10:56:25 -04002924 if (!ss_initialized || !(sbsec->flags & SBLABEL_MNT))
Stephen Smalley25a74f32005-11-08 21:34:33 -08002925 return -EOPNOTSUPP;
2926
Tetsuo Handa95489062013-07-25 05:44:02 +09002927 if (name)
2928 *name = XATTR_SELINUX_SUFFIX;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002929
2930 if (value && len) {
Stephen Smalley12b29f32008-05-07 13:03:20 -04002931 rc = security_sid_to_context_force(newsid, &context, &clen);
Tetsuo Handa95489062013-07-25 05:44:02 +09002932 if (rc)
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002933 return rc;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002934 *value = context;
2935 *len = clen;
2936 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002937
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002938 return 0;
2939}
2940
Al Viro4acdaf22011-07-26 01:42:34 -04002941static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942{
2943 return may_create(dir, dentry, SECCLASS_FILE);
2944}
2945
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2947{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 return may_link(dir, old_dentry, MAY_LINK);
2949}
2950
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
2952{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 return may_link(dir, dentry, MAY_UNLINK);
2954}
2955
2956static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
2957{
2958 return may_create(dir, dentry, SECCLASS_LNK_FILE);
2959}
2960
Al Viro18bb1db2011-07-26 01:41:39 -04002961static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962{
2963 return may_create(dir, dentry, SECCLASS_DIR);
2964}
2965
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
2967{
2968 return may_link(dir, dentry, MAY_RMDIR);
2969}
2970
Al Viro1a67aaf2011-07-26 01:52:52 -04002971static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 return may_create(dir, dentry, inode_mode_to_security_class(mode));
2974}
2975
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
Eric Paris828dfe12008-04-17 13:17:49 -04002977 struct inode *new_inode, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978{
2979 return may_rename(old_inode, old_dentry, new_inode, new_dentry);
2980}
2981
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982static int selinux_inode_readlink(struct dentry *dentry)
2983{
David Howells88e67f32008-11-14 10:39:21 +11002984 const struct cred *cred = current_cred();
2985
Eric Paris2875fa02011-04-28 16:04:24 -04002986 return dentry_has_perm(cred, dentry, FILE__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987}
2988
NeilBrownbda0be72015-03-23 13:37:39 +11002989static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode,
2990 bool rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991{
David Howells88e67f32008-11-14 10:39:21 +11002992 const struct cred *cred = current_cred();
NeilBrownbda0be72015-03-23 13:37:39 +11002993 struct common_audit_data ad;
2994 struct inode_security_struct *isec;
2995 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996
NeilBrownbda0be72015-03-23 13:37:39 +11002997 validate_creds(cred);
2998
2999 ad.type = LSM_AUDIT_DATA_DENTRY;
3000 ad.u.dentry = dentry;
3001 sid = cred_sid(cred);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05003002 isec = inode_security_rcu(inode, rcu);
3003 if (IS_ERR(isec))
3004 return PTR_ERR(isec);
NeilBrownbda0be72015-03-23 13:37:39 +11003005
3006 return avc_has_perm_flags(sid, isec->sid, isec->sclass, FILE__READ, &ad,
3007 rcu ? MAY_NOT_BLOCK : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008}
3009
Eric Parisd4cf970d2012-04-04 15:01:42 -04003010static noinline int audit_inode_permission(struct inode *inode,
3011 u32 perms, u32 audited, u32 denied,
Stephen Smalley626b9742014-04-29 11:29:04 -07003012 int result,
Eric Parisd4cf970d2012-04-04 15:01:42 -04003013 unsigned flags)
3014{
3015 struct common_audit_data ad;
Eric Parisd4cf970d2012-04-04 15:01:42 -04003016 struct inode_security_struct *isec = inode->i_security;
3017 int rc;
3018
Eric Paris50c205f2012-04-04 15:01:43 -04003019 ad.type = LSM_AUDIT_DATA_INODE;
Eric Parisd4cf970d2012-04-04 15:01:42 -04003020 ad.u.inode = inode;
3021
3022 rc = slow_avc_audit(current_sid(), isec->sid, isec->sclass, perms,
Stephen Smalley626b9742014-04-29 11:29:04 -07003023 audited, denied, result, &ad, flags);
Eric Parisd4cf970d2012-04-04 15:01:42 -04003024 if (rc)
3025 return rc;
3026 return 0;
3027}
3028
Al Viroe74f71e2011-06-20 19:38:15 -04003029static int selinux_inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030{
David Howells88e67f32008-11-14 10:39:21 +11003031 const struct cred *cred = current_cred();
Eric Parisb782e0a2010-07-23 11:44:03 -04003032 u32 perms;
3033 bool from_access;
Al Virocf1dd1d2011-06-20 19:44:08 -04003034 unsigned flags = mask & MAY_NOT_BLOCK;
Eric Paris2e334052012-04-04 15:01:42 -04003035 struct inode_security_struct *isec;
3036 u32 sid;
3037 struct av_decision avd;
3038 int rc, rc2;
3039 u32 audited, denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040
Eric Parisb782e0a2010-07-23 11:44:03 -04003041 from_access = mask & MAY_ACCESS;
Eric Parisd09ca732010-07-23 11:43:57 -04003042 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
3043
Eric Parisb782e0a2010-07-23 11:44:03 -04003044 /* No permission to check. Existence test. */
3045 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047
Eric Paris2e334052012-04-04 15:01:42 -04003048 validate_creds(cred);
Eric Parisb782e0a2010-07-23 11:44:03 -04003049
Eric Paris2e334052012-04-04 15:01:42 -04003050 if (unlikely(IS_PRIVATE(inode)))
3051 return 0;
Eric Parisb782e0a2010-07-23 11:44:03 -04003052
3053 perms = file_mask_to_av(inode->i_mode, mask);
3054
Eric Paris2e334052012-04-04 15:01:42 -04003055 sid = cred_sid(cred);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05003056 isec = inode_security_rcu(inode, flags & MAY_NOT_BLOCK);
3057 if (IS_ERR(isec))
3058 return PTR_ERR(isec);
Eric Paris2e334052012-04-04 15:01:42 -04003059
3060 rc = avc_has_perm_noaudit(sid, isec->sid, isec->sclass, perms, 0, &avd);
3061 audited = avc_audit_required(perms, &avd, rc,
3062 from_access ? FILE__AUDIT_ACCESS : 0,
3063 &denied);
3064 if (likely(!audited))
3065 return rc;
3066
Stephen Smalley626b9742014-04-29 11:29:04 -07003067 rc2 = audit_inode_permission(inode, perms, audited, denied, rc, flags);
Eric Paris2e334052012-04-04 15:01:42 -04003068 if (rc2)
3069 return rc2;
3070 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071}
3072
3073static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
3074{
David Howells88e67f32008-11-14 10:39:21 +11003075 const struct cred *cred = current_cred();
Stephen Smalleyb983b2a2017-05-12 12:41:24 -04003076 struct inode *inode = d_backing_inode(dentry);
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003077 unsigned int ia_valid = iattr->ia_valid;
Eric Paris95dbf732012-04-04 13:45:34 -04003078 __u32 av = FILE__WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003080 /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
3081 if (ia_valid & ATTR_FORCE) {
3082 ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
3083 ATTR_FORCE);
3084 if (!ia_valid)
3085 return 0;
3086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003088 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
3089 ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
Eric Paris2875fa02011-04-28 16:04:24 -04003090 return dentry_has_perm(cred, dentry, FILE__SETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091
Stephen Smalleyb983b2a2017-05-12 12:41:24 -04003092 if (selinux_policycap_openperm &&
3093 inode->i_sb->s_magic != SOCKFS_MAGIC &&
3094 (ia_valid & ATTR_SIZE) &&
3095 !(ia_valid & ATTR_FILE))
Eric Paris95dbf732012-04-04 13:45:34 -04003096 av |= FILE__OPEN;
3097
3098 return dentry_has_perm(cred, dentry, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099}
3100
Al Viro3f7036a2015-03-08 19:28:30 -04003101static int selinux_inode_getattr(const struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102{
Al Viro3f7036a2015-03-08 19:28:30 -04003103 return path_has_perm(current_cred(), path, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104}
3105
David Howells8f0cfa52008-04-29 00:59:41 -07003106static int selinux_inode_setotherxattr(struct dentry *dentry, const char *name)
Serge E. Hallynb5376772007-10-16 23:31:36 -07003107{
David Howells88e67f32008-11-14 10:39:21 +11003108 const struct cred *cred = current_cred();
3109
Serge E. Hallynb5376772007-10-16 23:31:36 -07003110 if (!strncmp(name, XATTR_SECURITY_PREFIX,
3111 sizeof XATTR_SECURITY_PREFIX - 1)) {
3112 if (!strcmp(name, XATTR_NAME_CAPS)) {
3113 if (!capable(CAP_SETFCAP))
3114 return -EPERM;
3115 } else if (!capable(CAP_SYS_ADMIN)) {
3116 /* A different attribute in the security namespace.
3117 Restrict to administrator. */
3118 return -EPERM;
3119 }
3120 }
3121
3122 /* Not an attribute we recognize, so just check the
3123 ordinary setattr permission. */
Eric Paris2875fa02011-04-28 16:04:24 -04003124 return dentry_has_perm(cred, dentry, FILE__SETATTR);
Serge E. Hallynb5376772007-10-16 23:31:36 -07003125}
3126
David Howells8f0cfa52008-04-29 00:59:41 -07003127static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
3128 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129{
David Howellsc6f493d2015-03-17 22:26:22 +00003130 struct inode *inode = d_backing_inode(dentry);
Paul Moore20cdef82016-04-04 14:14:42 -04003131 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 struct superblock_security_struct *sbsec;
Thomas Liu2bf49692009-07-14 12:14:09 -04003133 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11003134 u32 newsid, sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 int rc = 0;
3136
Serge E. Hallynb5376772007-10-16 23:31:36 -07003137 if (strcmp(name, XATTR_NAME_SELINUX))
3138 return selinux_inode_setotherxattr(dentry, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139
3140 sbsec = inode->i_sb->s_security;
Eric Paris12f348b2012-10-09 10:56:25 -04003141 if (!(sbsec->flags & SBLABEL_MNT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 return -EOPNOTSUPP;
3143
Serge E. Hallyn2e149672011-03-23 16:43:26 -07003144 if (!inode_owner_or_capable(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 return -EPERM;
3146
Eric Paris50c205f2012-04-04 15:01:43 -04003147 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04003148 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149
Paul Moore20cdef82016-04-04 14:14:42 -04003150 isec = backing_inode_security(dentry);
David Howells275bb412008-11-14 10:39:19 +11003151 rc = avc_has_perm(sid, isec->sid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 FILE__RELABELFROM, &ad);
3153 if (rc)
3154 return rc;
3155
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01003156 rc = security_context_to_sid(value, size, &newsid, GFP_KERNEL);
Stephen Smalley12b29f32008-05-07 13:03:20 -04003157 if (rc == -EINVAL) {
Eric Parisd6ea83e2012-04-04 13:45:49 -04003158 if (!capable(CAP_MAC_ADMIN)) {
3159 struct audit_buffer *ab;
3160 size_t audit_size;
3161 const char *str;
3162
3163 /* We strip a nul only if it is at the end, otherwise the
3164 * context contains a nul and we should audit that */
Al Viroe3fea3f2012-06-09 08:15:16 +01003165 if (value) {
3166 str = value;
3167 if (str[size - 1] == '\0')
3168 audit_size = size - 1;
3169 else
3170 audit_size = size;
3171 } else {
3172 str = "";
3173 audit_size = 0;
3174 }
Eric Parisd6ea83e2012-04-04 13:45:49 -04003175 ab = audit_log_start(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR);
3176 audit_log_format(ab, "op=setxattr invalid_context=");
3177 audit_log_n_untrustedstring(ab, value, audit_size);
3178 audit_log_end(ab);
3179
Stephen Smalley12b29f32008-05-07 13:03:20 -04003180 return rc;
Eric Parisd6ea83e2012-04-04 13:45:49 -04003181 }
Stephen Smalley12b29f32008-05-07 13:03:20 -04003182 rc = security_context_to_sid_force(value, size, &newsid);
3183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 if (rc)
3185 return rc;
3186
David Howells275bb412008-11-14 10:39:19 +11003187 rc = avc_has_perm(sid, newsid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 FILE__RELABELTO, &ad);
3189 if (rc)
3190 return rc;
3191
David Howells275bb412008-11-14 10:39:19 +11003192 rc = security_validate_transition(isec->sid, newsid, sid,
Eric Paris828dfe12008-04-17 13:17:49 -04003193 isec->sclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 if (rc)
3195 return rc;
3196
3197 return avc_has_perm(newsid,
3198 sbsec->sid,
3199 SECCLASS_FILESYSTEM,
3200 FILESYSTEM__ASSOCIATE,
3201 &ad);
3202}
3203
David Howells8f0cfa52008-04-29 00:59:41 -07003204static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
Eric Parisf5269712008-05-14 11:27:45 -04003205 const void *value, size_t size,
David Howells8f0cfa52008-04-29 00:59:41 -07003206 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207{
David Howellsc6f493d2015-03-17 22:26:22 +00003208 struct inode *inode = d_backing_inode(dentry);
Paul Moore20cdef82016-04-04 14:14:42 -04003209 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 u32 newsid;
3211 int rc;
3212
3213 if (strcmp(name, XATTR_NAME_SELINUX)) {
3214 /* Not an attribute we recognize, so nothing to do. */
3215 return;
3216 }
3217
Stephen Smalley12b29f32008-05-07 13:03:20 -04003218 rc = security_context_to_sid_force(value, size, &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 if (rc) {
Stephen Smalley12b29f32008-05-07 13:03:20 -04003220 printk(KERN_ERR "SELinux: unable to map context to SID"
3221 "for (%s, %lu), rc=%d\n",
3222 inode->i_sb->s_id, inode->i_ino, -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 return;
3224 }
3225
Paul Moore20cdef82016-04-04 14:14:42 -04003226 isec = backing_inode_security(dentry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003227 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05003229 isec->initialized = LABEL_INITIALIZED;
David Quigleyaa9c2662013-05-22 12:50:44 -04003230
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 return;
3232}
3233
David Howells8f0cfa52008-04-29 00:59:41 -07003234static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235{
David Howells88e67f32008-11-14 10:39:21 +11003236 const struct cred *cred = current_cred();
3237
Eric Paris2875fa02011-04-28 16:04:24 -04003238 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239}
3240
Eric Paris828dfe12008-04-17 13:17:49 -04003241static int selinux_inode_listxattr(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242{
David Howells88e67f32008-11-14 10:39:21 +11003243 const struct cred *cred = current_cred();
3244
Eric Paris2875fa02011-04-28 16:04:24 -04003245 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246}
3247
David Howells8f0cfa52008-04-29 00:59:41 -07003248static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249{
Serge E. Hallynb5376772007-10-16 23:31:36 -07003250 if (strcmp(name, XATTR_NAME_SELINUX))
3251 return selinux_inode_setotherxattr(dentry, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252
3253 /* No one is allowed to remove a SELinux security label.
3254 You can change the label, but all data must be labeled. */
3255 return -EACCES;
3256}
3257
James Morrisd381d8a2005-10-30 14:59:22 -08003258/*
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003259 * Copy the inode security context value to the user.
James Morrisd381d8a2005-10-30 14:59:22 -08003260 *
3261 * Permission check is handled by selinux_inode_getxattr hook.
3262 */
Andreas Gruenbacherea861df2015-12-24 11:09:39 -05003263static int selinux_inode_getsecurity(struct inode *inode, const char *name, void **buffer, bool alloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264{
David P. Quigley42492592008-02-04 22:29:39 -08003265 u32 size;
3266 int error;
3267 char *context = NULL;
Paul Moore20cdef82016-04-04 14:14:42 -04003268 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00003270 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3271 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003273 /*
3274 * If the caller has CAP_MAC_ADMIN, then get the raw context
3275 * value even if it is not defined by current policy; otherwise,
3276 * use the in-core value under current policy.
3277 * Use the non-auditing forms of the permission checks since
3278 * getxattr may be called by unprivileged processes commonly
3279 * and lack of permission just means that we fall back to the
3280 * in-core context value, not a denial.
3281 */
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07003282 error = cap_capable(current_cred(), &init_user_ns, CAP_MAC_ADMIN,
3283 SECURITY_CAP_NOAUDIT);
3284 if (!error)
3285 error = cred_has_capability(current_cred(), CAP_MAC_ADMIN,
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04003286 SECURITY_CAP_NOAUDIT, true);
Paul Moore20cdef82016-04-04 14:14:42 -04003287 isec = inode_security(inode);
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003288 if (!error)
3289 error = security_sid_to_context_force(isec->sid, &context,
3290 &size);
3291 else
3292 error = security_sid_to_context(isec->sid, &context, &size);
David P. Quigley42492592008-02-04 22:29:39 -08003293 if (error)
3294 return error;
3295 error = size;
3296 if (alloc) {
3297 *buffer = context;
3298 goto out_nofree;
3299 }
3300 kfree(context);
3301out_nofree:
3302 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303}
3304
3305static int selinux_inode_setsecurity(struct inode *inode, const char *name,
Eric Paris828dfe12008-04-17 13:17:49 -04003306 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307{
Paul Moore2c971652016-04-19 16:36:28 -04003308 struct inode_security_struct *isec = inode_security_novalidate(inode);
Ondrej Mosnacek992baf52018-12-21 21:18:53 +01003309 struct superblock_security_struct *sbsec = inode->i_sb->s_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 u32 newsid;
3311 int rc;
3312
3313 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3314 return -EOPNOTSUPP;
3315
Ondrej Mosnacek992baf52018-12-21 21:18:53 +01003316 if (!(sbsec->flags & SBLABEL_MNT))
3317 return -EOPNOTSUPP;
3318
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 if (!value || !size)
3320 return -EACCES;
3321
Rasmus Villemoes20ba96a2015-10-21 17:44:26 -04003322 rc = security_context_to_sid(value, size, &newsid, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 if (rc)
3324 return rc;
3325
David Quigleyaa9c2662013-05-22 12:50:44 -04003326 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05003328 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 return 0;
3330}
3331
3332static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
3333{
3334 const int len = sizeof(XATTR_NAME_SELINUX);
3335 if (buffer && len <= buffer_size)
3336 memcpy(buffer, XATTR_NAME_SELINUX, len);
3337 return len;
3338}
3339
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05003340static void selinux_inode_getsecid(struct inode *inode, u32 *secid)
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02003341{
Andreas Gruenbachere817c2f2016-02-18 12:04:08 +01003342 struct inode_security_struct *isec = inode_security_novalidate(inode);
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02003343 *secid = isec->sid;
3344}
3345
Vivek Goyal56909eb2016-07-13 10:44:48 -04003346static int selinux_inode_copy_up(struct dentry *src, struct cred **new)
3347{
3348 u32 sid;
3349 struct task_security_struct *tsec;
3350 struct cred *new_creds = *new;
3351
3352 if (new_creds == NULL) {
3353 new_creds = prepare_creds();
3354 if (!new_creds)
3355 return -ENOMEM;
3356 }
3357
3358 tsec = new_creds->security;
3359 /* Get label from overlay inode and set it in create_sid */
3360 selinux_inode_getsecid(d_inode(src), &sid);
3361 tsec->create_sid = sid;
3362 *new = new_creds;
3363 return 0;
3364}
3365
Vivek Goyal19472b62016-07-13 10:44:50 -04003366static int selinux_inode_copy_up_xattr(const char *name)
3367{
3368 /* The copy_up hook above sets the initial context on an inode, but we
3369 * don't then want to overwrite it by blindly copying all the lower
3370 * xattrs up. Instead, we have to filter out SELinux-related xattrs.
3371 */
3372 if (strcmp(name, XATTR_NAME_SELINUX) == 0)
3373 return 1; /* Discard */
3374 /*
3375 * Any other attribute apart from SELINUX is not claimed, supported
3376 * by selinux.
3377 */
3378 return -EOPNOTSUPP;
3379}
3380
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381/* file security operations */
3382
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003383static int selinux_revalidate_file_permission(struct file *file, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384{
David Howells88e67f32008-11-14 10:39:21 +11003385 const struct cred *cred = current_cred();
Al Viro496ad9a2013-01-23 17:07:38 -05003386 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
3389 if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
3390 mask |= MAY_APPEND;
3391
Paul Moore389fb8002009-03-27 17:10:34 -04003392 return file_has_perm(cred, file,
3393 file_mask_to_av(inode->i_mode, mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394}
3395
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003396static int selinux_file_permission(struct file *file, int mask)
3397{
Al Viro496ad9a2013-01-23 17:07:38 -05003398 struct inode *inode = file_inode(file);
Stephen Smalley20dda182009-06-22 14:54:53 -04003399 struct file_security_struct *fsec = file->f_security;
Andreas Gruenbacherb1973672016-01-05 23:12:33 +01003400 struct inode_security_struct *isec;
Stephen Smalley20dda182009-06-22 14:54:53 -04003401 u32 sid = current_sid();
3402
Paul Moore389fb8002009-03-27 17:10:34 -04003403 if (!mask)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003404 /* No permission to check. Existence test. */
3405 return 0;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003406
Andreas Gruenbacherb1973672016-01-05 23:12:33 +01003407 isec = inode_security(inode);
Stephen Smalley20dda182009-06-22 14:54:53 -04003408 if (sid == fsec->sid && fsec->isid == isec->sid &&
3409 fsec->pseqno == avc_policy_seqno())
Eric Paris83d49852012-04-04 13:45:40 -04003410 /* No change since file_open check. */
Stephen Smalley20dda182009-06-22 14:54:53 -04003411 return 0;
3412
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003413 return selinux_revalidate_file_permission(file, mask);
3414}
3415
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416static int selinux_file_alloc_security(struct file *file)
3417{
3418 return file_alloc_security(file);
3419}
3420
3421static void selinux_file_free_security(struct file *file)
3422{
3423 file_free_security(file);
3424}
3425
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003426/*
3427 * Check whether a task has the ioctl permission and cmd
3428 * operation to an inode.
3429 */
Geliang Tang1d2a1682015-10-21 17:44:27 -04003430static int ioctl_has_perm(const struct cred *cred, struct file *file,
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003431 u32 requested, u16 cmd)
3432{
3433 struct common_audit_data ad;
3434 struct file_security_struct *fsec = file->f_security;
3435 struct inode *inode = file_inode(file);
Paul Moore20cdef82016-04-04 14:14:42 -04003436 struct inode_security_struct *isec;
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003437 struct lsm_ioctlop_audit ioctl;
3438 u32 ssid = cred_sid(cred);
3439 int rc;
3440 u8 driver = cmd >> 8;
3441 u8 xperm = cmd & 0xff;
3442
3443 ad.type = LSM_AUDIT_DATA_IOCTL_OP;
3444 ad.u.op = &ioctl;
3445 ad.u.op->cmd = cmd;
3446 ad.u.op->path = file->f_path;
3447
3448 if (ssid != fsec->sid) {
3449 rc = avc_has_perm(ssid, fsec->sid,
3450 SECCLASS_FD,
3451 FD__USE,
3452 &ad);
3453 if (rc)
3454 goto out;
3455 }
3456
3457 if (unlikely(IS_PRIVATE(inode)))
3458 return 0;
3459
Paul Moore20cdef82016-04-04 14:14:42 -04003460 isec = inode_security(inode);
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003461 rc = avc_has_extended_perms(ssid, isec->sid, isec->sclass,
3462 requested, driver, xperm, &ad);
3463out:
3464 return rc;
3465}
3466
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467static int selinux_file_ioctl(struct file *file, unsigned int cmd,
3468 unsigned long arg)
3469{
David Howells88e67f32008-11-14 10:39:21 +11003470 const struct cred *cred = current_cred();
Eric Paris0b24dcb2011-02-25 15:39:20 -05003471 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472
Eric Paris0b24dcb2011-02-25 15:39:20 -05003473 switch (cmd) {
3474 case FIONREAD:
3475 /* fall through */
3476 case FIBMAP:
3477 /* fall through */
3478 case FIGETBSZ:
3479 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003480 case FS_IOC_GETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003481 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003482 case FS_IOC_GETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003483 error = file_has_perm(cred, file, FILE__GETATTR);
3484 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485
Al Viro2f99c362012-03-23 16:04:05 -04003486 case FS_IOC_SETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003487 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003488 case FS_IOC_SETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003489 error = file_has_perm(cred, file, FILE__SETATTR);
3490 break;
3491
3492 /* sys_ioctl() checks */
3493 case FIONBIO:
3494 /* fall through */
3495 case FIOASYNC:
3496 error = file_has_perm(cred, file, 0);
3497 break;
3498
3499 case KDSKBENT:
3500 case KDSKBSENT:
Eric Paris6a9de492012-01-03 12:25:14 -05003501 error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04003502 SECURITY_CAP_AUDIT, true);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003503 break;
3504
3505 /* default case assumes that the command will go
3506 * to the file's ioctl() function.
3507 */
3508 default:
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003509 error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003510 }
3511 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512}
3513
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003514static int default_noexec;
3515
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
3517{
David Howells88e67f32008-11-14 10:39:21 +11003518 const struct cred *cred = current_cred();
David Howellsd84f4f92008-11-14 10:39:23 +11003519 int rc = 0;
David Howells88e67f32008-11-14 10:39:21 +11003520
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003521 if (default_noexec &&
Stephen Smalley892e8ca2015-07-10 09:40:59 -04003522 (prot & PROT_EXEC) && (!file || IS_PRIVATE(file_inode(file)) ||
3523 (!shared && (prot & PROT_WRITE)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 /*
3525 * We are making executable an anonymous mapping or a
3526 * private file mapping that will also be writable.
3527 * This has an additional check.
3528 */
David Howellsd84f4f92008-11-14 10:39:23 +11003529 rc = cred_has_perm(cred, cred, PROCESS__EXECMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 if (rc)
David Howellsd84f4f92008-11-14 10:39:23 +11003531 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533
3534 if (file) {
3535 /* read access is always possible with a mapping */
3536 u32 av = FILE__READ;
3537
3538 /* write access only matters if the mapping is shared */
3539 if (shared && (prot & PROT_WRITE))
3540 av |= FILE__WRITE;
3541
3542 if (prot & PROT_EXEC)
3543 av |= FILE__EXECUTE;
3544
David Howells88e67f32008-11-14 10:39:21 +11003545 return file_has_perm(cred, file, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 }
David Howellsd84f4f92008-11-14 10:39:23 +11003547
3548error:
3549 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550}
3551
Al Viroe5467852012-05-30 13:30:51 -04003552static int selinux_mmap_addr(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07003554 int rc = 0;
Paul Moore98883bf2014-03-19 16:46:11 -04003555
3556 if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
3557 u32 sid = current_sid();
3558 rc = avc_has_perm(sid, sid, SECCLASS_MEMPROTECT,
3559 MEMPROTECT__MMAP_ZERO, NULL);
3560 }
3561
3562 return rc;
Al Viroe5467852012-05-30 13:30:51 -04003563}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564
Al Viroe5467852012-05-30 13:30:51 -04003565static int selinux_mmap_file(struct file *file, unsigned long reqprot,
3566 unsigned long prot, unsigned long flags)
3567{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 if (selinux_checkreqprot)
3569 prot = reqprot;
3570
3571 return file_map_prot_check(file, prot,
3572 (flags & MAP_TYPE) == MAP_SHARED);
3573}
3574
3575static int selinux_file_mprotect(struct vm_area_struct *vma,
3576 unsigned long reqprot,
3577 unsigned long prot)
3578{
David Howells88e67f32008-11-14 10:39:21 +11003579 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580
3581 if (selinux_checkreqprot)
3582 prot = reqprot;
3583
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003584 if (default_noexec &&
3585 (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
James Morrisd541bbe2009-01-29 12:19:51 +11003586 int rc = 0;
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003587 if (vma->vm_start >= vma->vm_mm->start_brk &&
3588 vma->vm_end <= vma->vm_mm->brk) {
David Howellsd84f4f92008-11-14 10:39:23 +11003589 rc = cred_has_perm(cred, cred, PROCESS__EXECHEAP);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003590 } else if (!vma->vm_file &&
Stephen Smalleyc2316dbf2016-04-08 13:55:03 -04003591 ((vma->vm_start <= vma->vm_mm->start_stack &&
3592 vma->vm_end >= vma->vm_mm->start_stack) ||
Andy Lutomirskid17af502016-09-30 10:58:58 -07003593 vma_is_stack_for_current(vma))) {
David Howells3b11a1d2008-11-14 10:39:26 +11003594 rc = current_has_perm(current, PROCESS__EXECSTACK);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003595 } else if (vma->vm_file && vma->anon_vma) {
3596 /*
3597 * We are making executable a file mapping that has
3598 * had some COW done. Since pages might have been
3599 * written, check ability to execute the possibly
3600 * modified content. This typically should only
3601 * occur for text relocations.
3602 */
David Howellsd84f4f92008-11-14 10:39:23 +11003603 rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003604 }
Lorenzo Hernandez García-Hierro6b992192005-06-25 14:54:34 -07003605 if (rc)
3606 return rc;
3607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608
3609 return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
3610}
3611
3612static int selinux_file_lock(struct file *file, unsigned int cmd)
3613{
David Howells88e67f32008-11-14 10:39:21 +11003614 const struct cred *cred = current_cred();
3615
3616 return file_has_perm(cred, file, FILE__LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617}
3618
3619static int selinux_file_fcntl(struct file *file, unsigned int cmd,
3620 unsigned long arg)
3621{
David Howells88e67f32008-11-14 10:39:21 +11003622 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 int err = 0;
3624
3625 switch (cmd) {
Eric Paris828dfe12008-04-17 13:17:49 -04003626 case F_SETFL:
Eric Paris828dfe12008-04-17 13:17:49 -04003627 if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
David Howells88e67f32008-11-14 10:39:21 +11003628 err = file_has_perm(cred, file, FILE__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003630 }
3631 /* fall through */
3632 case F_SETOWN:
3633 case F_SETSIG:
3634 case F_GETFL:
3635 case F_GETOWN:
3636 case F_GETSIG:
Cyrill Gorcunov1d151c32012-07-30 14:43:00 -07003637 case F_GETOWNER_UIDS:
Eric Paris828dfe12008-04-17 13:17:49 -04003638 /* Just check FD__USE permission */
David Howells88e67f32008-11-14 10:39:21 +11003639 err = file_has_perm(cred, file, 0);
Eric Paris828dfe12008-04-17 13:17:49 -04003640 break;
3641 case F_GETLK:
3642 case F_SETLK:
3643 case F_SETLKW:
Jeff Layton0d3f7a22014-04-22 08:23:58 -04003644 case F_OFD_GETLK:
3645 case F_OFD_SETLK:
3646 case F_OFD_SETLKW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647#if BITS_PER_LONG == 32
Eric Paris828dfe12008-04-17 13:17:49 -04003648 case F_GETLK64:
3649 case F_SETLK64:
3650 case F_SETLKW64:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651#endif
David Howells88e67f32008-11-14 10:39:21 +11003652 err = file_has_perm(cred, file, FILE__LOCK);
Eric Paris828dfe12008-04-17 13:17:49 -04003653 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 }
3655
3656 return err;
3657}
3658
Jeff Laytone0b93ed2014-08-22 11:27:32 -04003659static void selinux_file_set_fowner(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 struct file_security_struct *fsec;
3662
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 fsec = file->f_security;
David Howells275bb412008-11-14 10:39:19 +11003664 fsec->fown_sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665}
3666
3667static int selinux_file_send_sigiotask(struct task_struct *tsk,
3668 struct fown_struct *fown, int signum)
3669{
Eric Paris828dfe12008-04-17 13:17:49 -04003670 struct file *file;
Stephen Smalley65c90bc2009-05-04 15:43:18 -04003671 u32 sid = task_sid(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 struct file_security_struct *fsec;
3674
3675 /* struct fown_struct is never outside the context of a struct file */
Eric Paris828dfe12008-04-17 13:17:49 -04003676 file = container_of(fown, struct file, f_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 fsec = file->f_security;
3679
3680 if (!signum)
3681 perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
3682 else
3683 perm = signal_to_av(signum);
3684
David Howells275bb412008-11-14 10:39:19 +11003685 return avc_has_perm(fsec->fown_sid, sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 SECCLASS_PROCESS, perm, NULL);
3687}
3688
3689static int selinux_file_receive(struct file *file)
3690{
David Howells88e67f32008-11-14 10:39:21 +11003691 const struct cred *cred = current_cred();
3692
3693 return file_has_perm(cred, file, file_to_av(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694}
3695
Eric Paris83d49852012-04-04 13:45:40 -04003696static int selinux_file_open(struct file *file, const struct cred *cred)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003697{
3698 struct file_security_struct *fsec;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003699 struct inode_security_struct *isec;
David Howellsd84f4f92008-11-14 10:39:23 +11003700
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003701 fsec = file->f_security;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05003702 isec = inode_security(file_inode(file));
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003703 /*
3704 * Save inode label and policy sequence number
3705 * at open-time so that selinux_file_permission
3706 * can determine whether revalidation is necessary.
3707 * Task label is already saved in the file security
3708 * struct as its SID.
3709 */
3710 fsec->isid = isec->sid;
3711 fsec->pseqno = avc_policy_seqno();
3712 /*
3713 * Since the inode label or policy seqno may have changed
3714 * between the selinux_inode_permission check and the saving
3715 * of state above, recheck that access is still permitted.
3716 * Otherwise, access might never be revalidated against the
3717 * new inode label or new policy.
3718 * This check is not redundant - do not remove.
3719 */
David Howells13f8e982013-06-13 23:37:55 +01003720 return file_path_has_perm(cred, file, open_file_to_av(file));
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003721}
3722
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723/* task security operations */
3724
3725static int selinux_task_create(unsigned long clone_flags)
3726{
David Howells3b11a1d2008-11-14 10:39:26 +11003727 return current_has_perm(current, PROCESS__FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728}
3729
David Howellsf1752ee2008-11-14 10:39:17 +11003730/*
David Howellsee18d642009-09-02 09:14:21 +01003731 * allocate the SELinux part of blank credentials
3732 */
3733static int selinux_cred_alloc_blank(struct cred *cred, gfp_t gfp)
3734{
3735 struct task_security_struct *tsec;
3736
3737 tsec = kzalloc(sizeof(struct task_security_struct), gfp);
3738 if (!tsec)
3739 return -ENOMEM;
3740
3741 cred->security = tsec;
3742 return 0;
3743}
3744
3745/*
David Howellsf1752ee2008-11-14 10:39:17 +11003746 * detach and free the LSM part of a set of credentials
3747 */
3748static void selinux_cred_free(struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749{
David Howellsf1752ee2008-11-14 10:39:17 +11003750 struct task_security_struct *tsec = cred->security;
David Howellse0e81732009-09-02 09:13:40 +01003751
Tetsuo Handa2edeaa32011-02-07 13:36:10 +00003752 /*
3753 * cred->security == NULL if security_cred_alloc_blank() or
3754 * security_prepare_creds() returned an error.
3755 */
3756 BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE);
David Howellse0e81732009-09-02 09:13:40 +01003757 cred->security = (void *) 0x7UL;
David Howellsf1752ee2008-11-14 10:39:17 +11003758 kfree(tsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759}
3760
David Howellsd84f4f92008-11-14 10:39:23 +11003761/*
3762 * prepare a new set of credentials for modification
3763 */
3764static int selinux_cred_prepare(struct cred *new, const struct cred *old,
3765 gfp_t gfp)
3766{
3767 const struct task_security_struct *old_tsec;
3768 struct task_security_struct *tsec;
3769
3770 old_tsec = old->security;
3771
3772 tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp);
3773 if (!tsec)
3774 return -ENOMEM;
3775
3776 new->security = tsec;
3777 return 0;
3778}
3779
3780/*
David Howellsee18d642009-09-02 09:14:21 +01003781 * transfer the SELinux data to a blank set of creds
3782 */
3783static void selinux_cred_transfer(struct cred *new, const struct cred *old)
3784{
3785 const struct task_security_struct *old_tsec = old->security;
3786 struct task_security_struct *tsec = new->security;
3787
3788 *tsec = *old_tsec;
3789}
3790
3791/*
David Howells3a3b7ce2008-11-14 10:39:28 +11003792 * set the security data for a kernel service
3793 * - all the creation contexts are set to unlabelled
3794 */
3795static int selinux_kernel_act_as(struct cred *new, u32 secid)
3796{
3797 struct task_security_struct *tsec = new->security;
3798 u32 sid = current_sid();
3799 int ret;
3800
3801 ret = avc_has_perm(sid, secid,
3802 SECCLASS_KERNEL_SERVICE,
3803 KERNEL_SERVICE__USE_AS_OVERRIDE,
3804 NULL);
3805 if (ret == 0) {
3806 tsec->sid = secid;
3807 tsec->create_sid = 0;
3808 tsec->keycreate_sid = 0;
3809 tsec->sockcreate_sid = 0;
3810 }
3811 return ret;
3812}
3813
3814/*
3815 * set the file creation context in a security record to the same as the
3816 * objective context of the specified inode
3817 */
3818static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
3819{
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05003820 struct inode_security_struct *isec = inode_security(inode);
David Howells3a3b7ce2008-11-14 10:39:28 +11003821 struct task_security_struct *tsec = new->security;
3822 u32 sid = current_sid();
3823 int ret;
3824
3825 ret = avc_has_perm(sid, isec->sid,
3826 SECCLASS_KERNEL_SERVICE,
3827 KERNEL_SERVICE__CREATE_FILES_AS,
3828 NULL);
3829
3830 if (ret == 0)
3831 tsec->create_sid = isec->sid;
David Howellsef574712010-02-26 01:56:16 +00003832 return ret;
David Howells3a3b7ce2008-11-14 10:39:28 +11003833}
3834
Eric Parisdd8dbf22009-11-03 16:35:32 +11003835static int selinux_kernel_module_request(char *kmod_name)
Eric Paris25354c42009-08-13 09:45:03 -04003836{
Eric Parisdd8dbf22009-11-03 16:35:32 +11003837 u32 sid;
3838 struct common_audit_data ad;
3839
3840 sid = task_sid(current);
3841
Eric Paris50c205f2012-04-04 15:01:43 -04003842 ad.type = LSM_AUDIT_DATA_KMOD;
Eric Parisdd8dbf22009-11-03 16:35:32 +11003843 ad.u.kmod_name = kmod_name;
3844
3845 return avc_has_perm(sid, SECINITSID_KERNEL, SECCLASS_SYSTEM,
3846 SYSTEM__MODULE_REQUEST, &ad);
Eric Paris25354c42009-08-13 09:45:03 -04003847}
3848
Jeff Vander Stoep61d612e2016-04-05 13:06:27 -07003849static int selinux_kernel_module_from_file(struct file *file)
3850{
3851 struct common_audit_data ad;
3852 struct inode_security_struct *isec;
3853 struct file_security_struct *fsec;
3854 u32 sid = current_sid();
3855 int rc;
3856
3857 /* init_module */
3858 if (file == NULL)
3859 return avc_has_perm(sid, sid, SECCLASS_SYSTEM,
3860 SYSTEM__MODULE_LOAD, NULL);
3861
3862 /* finit_module */
Paul Moore20cdef82016-04-04 14:14:42 -04003863
Vivek Goyal43af5de2016-09-09 11:37:49 -04003864 ad.type = LSM_AUDIT_DATA_FILE;
3865 ad.u.file = file;
Jeff Vander Stoep61d612e2016-04-05 13:06:27 -07003866
Jeff Vander Stoep61d612e2016-04-05 13:06:27 -07003867 fsec = file->f_security;
Jeff Vander Stoep61d612e2016-04-05 13:06:27 -07003868 if (sid != fsec->sid) {
3869 rc = avc_has_perm(sid, fsec->sid, SECCLASS_FD, FD__USE, &ad);
3870 if (rc)
3871 return rc;
3872 }
3873
Paul Moore20cdef82016-04-04 14:14:42 -04003874 isec = inode_security(file_inode(file));
Jeff Vander Stoep61d612e2016-04-05 13:06:27 -07003875 return avc_has_perm(sid, isec->sid, SECCLASS_SYSTEM,
3876 SYSTEM__MODULE_LOAD, &ad);
3877}
3878
3879static int selinux_kernel_read_file(struct file *file,
3880 enum kernel_read_file_id id)
3881{
3882 int rc = 0;
3883
3884 switch (id) {
3885 case READING_MODULE:
3886 rc = selinux_kernel_module_from_file(file);
3887 break;
3888 default:
3889 break;
3890 }
3891
3892 return rc;
3893}
3894
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
3896{
David Howells3b11a1d2008-11-14 10:39:26 +11003897 return current_has_perm(p, PROCESS__SETPGID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898}
3899
3900static int selinux_task_getpgid(struct task_struct *p)
3901{
David Howells3b11a1d2008-11-14 10:39:26 +11003902 return current_has_perm(p, PROCESS__GETPGID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903}
3904
3905static int selinux_task_getsid(struct task_struct *p)
3906{
David Howells3b11a1d2008-11-14 10:39:26 +11003907 return current_has_perm(p, PROCESS__GETSESSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908}
3909
David Quigleyf9008e42006-06-30 01:55:46 -07003910static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
3911{
David Howells275bb412008-11-14 10:39:19 +11003912 *secid = task_sid(p);
David Quigleyf9008e42006-06-30 01:55:46 -07003913}
3914
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915static int selinux_task_setnice(struct task_struct *p, int nice)
3916{
David Howells3b11a1d2008-11-14 10:39:26 +11003917 return current_has_perm(p, PROCESS__SETSCHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918}
3919
James Morris03e68062006-06-23 02:03:58 -07003920static int selinux_task_setioprio(struct task_struct *p, int ioprio)
3921{
David Howells3b11a1d2008-11-14 10:39:26 +11003922 return current_has_perm(p, PROCESS__SETSCHED);
James Morris03e68062006-06-23 02:03:58 -07003923}
3924
David Quigleya1836a42006-06-30 01:55:49 -07003925static int selinux_task_getioprio(struct task_struct *p)
3926{
David Howells3b11a1d2008-11-14 10:39:26 +11003927 return current_has_perm(p, PROCESS__GETSCHED);
David Quigleya1836a42006-06-30 01:55:49 -07003928}
3929
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003930static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
3931 struct rlimit *new_rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932{
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003933 struct rlimit *old_rlim = p->signal->rlim + resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934
3935 /* Control the ability to change the hard limit (whether
3936 lowering or raising it), so that the hard limit can
3937 later be used as a safe reset point for the soft limit
David Howellsd84f4f92008-11-14 10:39:23 +11003938 upon context transitions. See selinux_bprm_committing_creds. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 if (old_rlim->rlim_max != new_rlim->rlim_max)
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003940 return current_has_perm(p, PROCESS__SETRLIMIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941
3942 return 0;
3943}
3944
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09003945static int selinux_task_setscheduler(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946{
David Howells3b11a1d2008-11-14 10:39:26 +11003947 return current_has_perm(p, PROCESS__SETSCHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948}
3949
3950static int selinux_task_getscheduler(struct task_struct *p)
3951{
David Howells3b11a1d2008-11-14 10:39:26 +11003952 return current_has_perm(p, PROCESS__GETSCHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953}
3954
David Quigley35601542006-06-23 02:04:01 -07003955static int selinux_task_movememory(struct task_struct *p)
3956{
David Howells3b11a1d2008-11-14 10:39:26 +11003957 return current_has_perm(p, PROCESS__SETSCHED);
David Quigley35601542006-06-23 02:04:01 -07003958}
3959
David Quigleyf9008e42006-06-30 01:55:46 -07003960static int selinux_task_kill(struct task_struct *p, struct siginfo *info,
3961 int sig, u32 secid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962{
3963 u32 perm;
3964 int rc;
3965
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 if (!sig)
3967 perm = PROCESS__SIGNULL; /* null signal; existence test */
3968 else
3969 perm = signal_to_av(sig);
David Quigleyf9008e42006-06-30 01:55:46 -07003970 if (secid)
David Howells275bb412008-11-14 10:39:19 +11003971 rc = avc_has_perm(secid, task_sid(p),
3972 SECCLASS_PROCESS, perm, NULL);
David Quigleyf9008e42006-06-30 01:55:46 -07003973 else
David Howells3b11a1d2008-11-14 10:39:26 +11003974 rc = current_has_perm(p, perm);
David Quigleyf9008e42006-06-30 01:55:46 -07003975 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976}
3977
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978static int selinux_task_wait(struct task_struct *p)
3979{
Eric Paris8a535142007-10-22 16:10:31 -04003980 return task_has_perm(p, current, PROCESS__SIGCHLD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981}
3982
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983static void selinux_task_to_inode(struct task_struct *p,
3984 struct inode *inode)
3985{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 struct inode_security_struct *isec = inode->i_security;
David Howells275bb412008-11-14 10:39:19 +11003987 u32 sid = task_sid(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988
David Howells275bb412008-11-14 10:39:19 +11003989 isec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05003990 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991}
3992
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003994static int selinux_parse_skb_ipv4(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04003995 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996{
3997 int offset, ihlen, ret = -EINVAL;
3998 struct iphdr _iph, *ih;
3999
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03004000 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
4002 if (ih == NULL)
4003 goto out;
4004
4005 ihlen = ih->ihl * 4;
4006 if (ihlen < sizeof(_iph))
4007 goto out;
4008
Eric Paris48c62af2012-04-02 13:15:44 -04004009 ad->u.net->v4info.saddr = ih->saddr;
4010 ad->u.net->v4info.daddr = ih->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 ret = 0;
4012
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004013 if (proto)
4014 *proto = ih->protocol;
4015
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 switch (ih->protocol) {
Eric Paris828dfe12008-04-17 13:17:49 -04004017 case IPPROTO_TCP: {
4018 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019
Eric Paris828dfe12008-04-17 13:17:49 -04004020 if (ntohs(ih->frag_off) & IP_OFFSET)
4021 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022
4023 offset += ihlen;
4024 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4025 if (th == NULL)
4026 break;
4027
Eric Paris48c62af2012-04-02 13:15:44 -04004028 ad->u.net->sport = th->source;
4029 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004031 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032
Eric Paris828dfe12008-04-17 13:17:49 -04004033 case IPPROTO_UDP: {
4034 struct udphdr _udph, *uh;
4035
4036 if (ntohs(ih->frag_off) & IP_OFFSET)
4037 break;
4038
4039 offset += ihlen;
4040 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4041 if (uh == NULL)
4042 break;
4043
Eric Paris48c62af2012-04-02 13:15:44 -04004044 ad->u.net->sport = uh->source;
4045 ad->u.net->dport = uh->dest;
Eric Paris828dfe12008-04-17 13:17:49 -04004046 break;
4047 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048
James Morris2ee92d42006-11-13 16:09:01 -08004049 case IPPROTO_DCCP: {
4050 struct dccp_hdr _dccph, *dh;
4051
4052 if (ntohs(ih->frag_off) & IP_OFFSET)
4053 break;
4054
4055 offset += ihlen;
4056 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4057 if (dh == NULL)
4058 break;
4059
Eric Paris48c62af2012-04-02 13:15:44 -04004060 ad->u.net->sport = dh->dccph_sport;
4061 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08004062 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004063 }
James Morris2ee92d42006-11-13 16:09:01 -08004064
Eric Paris828dfe12008-04-17 13:17:49 -04004065 default:
4066 break;
4067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068out:
4069 return ret;
4070}
4071
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04004072#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073
4074/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004075static int selinux_parse_skb_ipv6(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04004076 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077{
4078 u8 nexthdr;
4079 int ret = -EINVAL, offset;
4080 struct ipv6hdr _ipv6h, *ip6;
Jesse Gross75f28112011-11-30 17:05:51 -08004081 __be16 frag_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03004083 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
4085 if (ip6 == NULL)
4086 goto out;
4087
Eric Paris48c62af2012-04-02 13:15:44 -04004088 ad->u.net->v6info.saddr = ip6->saddr;
4089 ad->u.net->v6info.daddr = ip6->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090 ret = 0;
4091
4092 nexthdr = ip6->nexthdr;
4093 offset += sizeof(_ipv6h);
Jesse Gross75f28112011-11-30 17:05:51 -08004094 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 if (offset < 0)
4096 goto out;
4097
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004098 if (proto)
4099 *proto = nexthdr;
4100
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 switch (nexthdr) {
4102 case IPPROTO_TCP: {
Eric Paris828dfe12008-04-17 13:17:49 -04004103 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104
4105 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4106 if (th == NULL)
4107 break;
4108
Eric Paris48c62af2012-04-02 13:15:44 -04004109 ad->u.net->sport = th->source;
4110 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 break;
4112 }
4113
4114 case IPPROTO_UDP: {
4115 struct udphdr _udph, *uh;
4116
4117 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4118 if (uh == NULL)
4119 break;
4120
Eric Paris48c62af2012-04-02 13:15:44 -04004121 ad->u.net->sport = uh->source;
4122 ad->u.net->dport = uh->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 break;
4124 }
4125
James Morris2ee92d42006-11-13 16:09:01 -08004126 case IPPROTO_DCCP: {
4127 struct dccp_hdr _dccph, *dh;
4128
4129 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4130 if (dh == NULL)
4131 break;
4132
Eric Paris48c62af2012-04-02 13:15:44 -04004133 ad->u.net->sport = dh->dccph_sport;
4134 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08004135 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004136 }
James Morris2ee92d42006-11-13 16:09:01 -08004137
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138 /* includes fragments */
4139 default:
4140 break;
4141 }
4142out:
4143 return ret;
4144}
4145
4146#endif /* IPV6 */
4147
Thomas Liu2bf49692009-07-14 12:14:09 -04004148static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
David Howellscf9481e2008-07-27 21:31:07 +10004149 char **_addrp, int src, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150{
David Howellscf9481e2008-07-27 21:31:07 +10004151 char *addrp;
4152 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153
Eric Paris48c62af2012-04-02 13:15:44 -04004154 switch (ad->u.net->family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 case PF_INET:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004156 ret = selinux_parse_skb_ipv4(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10004157 if (ret)
4158 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04004159 addrp = (char *)(src ? &ad->u.net->v4info.saddr :
4160 &ad->u.net->v4info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10004161 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04004163#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 case PF_INET6:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004165 ret = selinux_parse_skb_ipv6(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10004166 if (ret)
4167 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04004168 addrp = (char *)(src ? &ad->u.net->v6info.saddr :
4169 &ad->u.net->v6info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10004170 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171#endif /* IPV6 */
4172 default:
David Howellscf9481e2008-07-27 21:31:07 +10004173 addrp = NULL;
4174 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 }
4176
David Howellscf9481e2008-07-27 21:31:07 +10004177parse_error:
4178 printk(KERN_WARNING
4179 "SELinux: failure in selinux_parse_skb(),"
4180 " unable to parse packet\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 return ret;
David Howellscf9481e2008-07-27 21:31:07 +10004182
4183okay:
4184 if (_addrp)
4185 *_addrp = addrp;
4186 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187}
4188
Paul Moore4f6a9932007-03-01 14:35:22 -05004189/**
Paul Moore220deb92008-01-29 08:38:23 -05004190 * selinux_skb_peerlbl_sid - Determine the peer label of a packet
Paul Moore4f6a9932007-03-01 14:35:22 -05004191 * @skb: the packet
Paul Moore75e22912008-01-29 08:38:04 -05004192 * @family: protocol family
Paul Moore220deb92008-01-29 08:38:23 -05004193 * @sid: the packet's peer label SID
Paul Moore4f6a9932007-03-01 14:35:22 -05004194 *
4195 * Description:
Paul Moore220deb92008-01-29 08:38:23 -05004196 * Check the various different forms of network peer labeling and determine
4197 * the peer label/SID for the packet; most of the magic actually occurs in
4198 * the security server function security_net_peersid_cmp(). The function
4199 * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
4200 * or -EACCES if @sid is invalid due to inconsistencies with the different
4201 * peer labels.
Paul Moore4f6a9932007-03-01 14:35:22 -05004202 *
4203 */
Paul Moore220deb92008-01-29 08:38:23 -05004204static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
Paul Moore4f6a9932007-03-01 14:35:22 -05004205{
Paul Moore71f1cb02008-01-29 08:51:16 -05004206 int err;
Paul Moore4f6a9932007-03-01 14:35:22 -05004207 u32 xfrm_sid;
4208 u32 nlbl_sid;
Paul Moore220deb92008-01-29 08:38:23 -05004209 u32 nlbl_type;
Paul Moore4f6a9932007-03-01 14:35:22 -05004210
Paul Moore817eff72013-12-10 14:57:54 -05004211 err = selinux_xfrm_skb_sid(skb, &xfrm_sid);
Paul Moorebed4d7e2013-07-23 17:38:40 -04004212 if (unlikely(err))
4213 return -EACCES;
4214 err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
4215 if (unlikely(err))
4216 return -EACCES;
Paul Moore220deb92008-01-29 08:38:23 -05004217
Paul Moore71f1cb02008-01-29 08:51:16 -05004218 err = security_net_peersid_resolve(nlbl_sid, nlbl_type, xfrm_sid, sid);
4219 if (unlikely(err)) {
4220 printk(KERN_WARNING
4221 "SELinux: failure in selinux_skb_peerlbl_sid(),"
4222 " unable to determine packet's peer label\n");
Paul Moore220deb92008-01-29 08:38:23 -05004223 return -EACCES;
Paul Moore71f1cb02008-01-29 08:51:16 -05004224 }
Paul Moore220deb92008-01-29 08:38:23 -05004225
4226 return 0;
Paul Moore4f6a9932007-03-01 14:35:22 -05004227}
4228
Paul Moore446b8022013-12-04 16:10:51 -05004229/**
4230 * selinux_conn_sid - Determine the child socket label for a connection
4231 * @sk_sid: the parent socket's SID
4232 * @skb_sid: the packet's SID
4233 * @conn_sid: the resulting connection SID
4234 *
4235 * If @skb_sid is valid then the user:role:type information from @sk_sid is
4236 * combined with the MLS information from @skb_sid in order to create
4237 * @conn_sid. If @skb_sid is not valid then then @conn_sid is simply a copy
4238 * of @sk_sid. Returns zero on success, negative values on failure.
4239 *
4240 */
4241static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid)
4242{
4243 int err = 0;
4244
4245 if (skb_sid != SECSID_NULL)
4246 err = security_sid_mls_copy(sk_sid, skb_sid, conn_sid);
4247 else
4248 *conn_sid = sk_sid;
4249
4250 return err;
4251}
4252
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253/* socket security operations */
Paul Moored4f2d972010-04-22 14:46:18 -04004254
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004255static int socket_sockcreate_sid(const struct task_security_struct *tsec,
4256 u16 secclass, u32 *socksid)
Paul Moored4f2d972010-04-22 14:46:18 -04004257{
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004258 if (tsec->sockcreate_sid > SECSID_NULL) {
4259 *socksid = tsec->sockcreate_sid;
4260 return 0;
4261 }
4262
4263 return security_transition_sid(tsec->sid, tsec->sid, secclass, NULL,
4264 socksid);
Paul Moored4f2d972010-04-22 14:46:18 -04004265}
4266
Paul Moore253bfae2010-04-22 14:46:19 -04004267static int sock_has_perm(struct task_struct *task, struct sock *sk, u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268{
Paul Moore253bfae2010-04-22 14:46:19 -04004269 struct sk_security_struct *sksec = sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004270 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004271 struct lsm_network_audit net = {0,};
Paul Moore253bfae2010-04-22 14:46:19 -04004272 u32 tsid = task_sid(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273
Paul Moore253bfae2010-04-22 14:46:19 -04004274 if (sksec->sid == SECINITSID_KERNEL)
4275 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276
Eric Paris50c205f2012-04-04 15:01:43 -04004277 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004278 ad.u.net = &net;
4279 ad.u.net->sk = sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280
Paul Moore253bfae2010-04-22 14:46:19 -04004281 return avc_has_perm(tsid, sksec->sid, sksec->sclass, perms, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282}
4283
4284static int selinux_socket_create(int family, int type,
4285 int protocol, int kern)
4286{
Paul Moore5fb49872010-04-22 14:46:19 -04004287 const struct task_security_struct *tsec = current_security();
Paul Moored4f2d972010-04-22 14:46:18 -04004288 u32 newsid;
David Howells275bb412008-11-14 10:39:19 +11004289 u16 secclass;
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004290 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291
4292 if (kern)
Paul Moored4f2d972010-04-22 14:46:18 -04004293 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294
David Howells275bb412008-11-14 10:39:19 +11004295 secclass = socket_type_to_security_class(family, type, protocol);
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004296 rc = socket_sockcreate_sid(tsec, secclass, &newsid);
4297 if (rc)
4298 return rc;
4299
Paul Moored4f2d972010-04-22 14:46:18 -04004300 return avc_has_perm(tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301}
4302
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07004303static int selinux_socket_post_create(struct socket *sock, int family,
4304 int type, int protocol, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305{
Paul Moore5fb49872010-04-22 14:46:19 -04004306 const struct task_security_struct *tsec = current_security();
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05004307 struct inode_security_struct *isec = inode_security_novalidate(SOCK_INODE(sock));
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004308 struct sk_security_struct *sksec;
David Howells275bb412008-11-14 10:39:19 +11004309 int err = 0;
4310
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004311 isec->sclass = socket_type_to_security_class(family, type, protocol);
4312
David Howells275bb412008-11-14 10:39:19 +11004313 if (kern)
4314 isec->sid = SECINITSID_KERNEL;
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004315 else {
4316 err = socket_sockcreate_sid(tsec, isec->sclass, &(isec->sid));
4317 if (err)
4318 return err;
4319 }
David Howells275bb412008-11-14 10:39:19 +11004320
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004321 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004323 if (sock->sk) {
4324 sksec = sock->sk->sk_security;
4325 sksec->sid = isec->sid;
Paul Moore220deb92008-01-29 08:38:23 -05004326 sksec->sclass = isec->sclass;
Paul Moore389fb8002009-03-27 17:10:34 -04004327 err = selinux_netlbl_socket_post_create(sock->sk, family);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004328 }
4329
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07004330 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331}
4332
4333/* Range of port numbers used to automatically bind.
4334 Need to determine whether we should perform a name_bind
4335 permission check between the socket and the port number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336
4337static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
4338{
Paul Moore253bfae2010-04-22 14:46:19 -04004339 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 u16 family;
4341 int err;
4342
Paul Moore253bfae2010-04-22 14:46:19 -04004343 err = sock_has_perm(current, sk, SOCKET__BIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 if (err)
4345 goto out;
4346
4347 /*
4348 * If PF_INET or PF_INET6, check name_bind permission for the port.
James Morris13402582005-09-30 14:24:34 -04004349 * Multiple address binding for SCTP is not supported yet: we just
4350 * check the first address now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 */
Paul Moore253bfae2010-04-22 14:46:19 -04004352 family = sk->sk_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 if (family == PF_INET || family == PF_INET6) {
4354 char *addrp;
Paul Moore253bfae2010-04-22 14:46:19 -04004355 struct sk_security_struct *sksec = sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004356 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004357 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358 struct sockaddr_in *addr4 = NULL;
4359 struct sockaddr_in6 *addr6 = NULL;
4360 unsigned short snum;
James Morrise399f982008-06-12 01:39:58 +10004361 u32 sid, node_perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 if (family == PF_INET) {
Alexander Potapenkob243aa82017-03-06 19:46:14 +01004364 if (addrlen < sizeof(struct sockaddr_in)) {
4365 err = -EINVAL;
4366 goto out;
4367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 addr4 = (struct sockaddr_in *)address;
4369 snum = ntohs(addr4->sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 addrp = (char *)&addr4->sin_addr.s_addr;
4371 } else {
Alexander Potapenkob243aa82017-03-06 19:46:14 +01004372 if (addrlen < SIN6_LEN_RFC2133) {
4373 err = -EINVAL;
4374 goto out;
4375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 addr6 = (struct sockaddr_in6 *)address;
4377 snum = ntohs(addr6->sin6_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 addrp = (char *)&addr6->sin6_addr.s6_addr;
4379 }
4380
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004381 if (snum) {
4382 int low, high;
4383
Eric W. Biederman0bbf87d2013-09-28 14:10:59 -07004384 inet_get_local_port_range(sock_net(sk), &low, &high);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004385
4386 if (snum < max(PROT_SOCK, low) || snum > high) {
Paul Moore3e112172008-04-10 10:48:14 -04004387 err = sel_netport_sid(sk->sk_protocol,
4388 snum, &sid);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004389 if (err)
4390 goto out;
Eric Paris50c205f2012-04-04 15:01:43 -04004391 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004392 ad.u.net = &net;
4393 ad.u.net->sport = htons(snum);
4394 ad.u.net->family = family;
Paul Moore253bfae2010-04-22 14:46:19 -04004395 err = avc_has_perm(sksec->sid, sid,
4396 sksec->sclass,
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004397 SOCKET__NAME_BIND, &ad);
4398 if (err)
4399 goto out;
4400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 }
Eric Paris828dfe12008-04-17 13:17:49 -04004402
Paul Moore253bfae2010-04-22 14:46:19 -04004403 switch (sksec->sclass) {
James Morris13402582005-09-30 14:24:34 -04004404 case SECCLASS_TCP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 node_perm = TCP_SOCKET__NODE_BIND;
4406 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004407
James Morris13402582005-09-30 14:24:34 -04004408 case SECCLASS_UDP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 node_perm = UDP_SOCKET__NODE_BIND;
4410 break;
James Morris2ee92d42006-11-13 16:09:01 -08004411
4412 case SECCLASS_DCCP_SOCKET:
4413 node_perm = DCCP_SOCKET__NODE_BIND;
4414 break;
4415
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 default:
4417 node_perm = RAWIP_SOCKET__NODE_BIND;
4418 break;
4419 }
Eric Paris828dfe12008-04-17 13:17:49 -04004420
Paul Moore224dfbd2008-01-29 08:38:13 -05004421 err = sel_netnode_sid(addrp, family, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 if (err)
4423 goto out;
Eric Paris828dfe12008-04-17 13:17:49 -04004424
Eric Paris50c205f2012-04-04 15:01:43 -04004425 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004426 ad.u.net = &net;
4427 ad.u.net->sport = htons(snum);
4428 ad.u.net->family = family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429
4430 if (family == PF_INET)
Eric Paris48c62af2012-04-02 13:15:44 -04004431 ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 else
Eric Paris48c62af2012-04-02 13:15:44 -04004433 ad.u.net->v6info.saddr = addr6->sin6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434
Paul Moore253bfae2010-04-22 14:46:19 -04004435 err = avc_has_perm(sksec->sid, sid,
4436 sksec->sclass, node_perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 if (err)
4438 goto out;
4439 }
4440out:
4441 return err;
4442}
4443
4444static int selinux_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen)
4445{
Paul Moore014ab192008-10-10 10:16:33 -04004446 struct sock *sk = sock->sk;
Paul Moore253bfae2010-04-22 14:46:19 -04004447 struct sk_security_struct *sksec = sk->sk_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 int err;
4449
Paul Moore253bfae2010-04-22 14:46:19 -04004450 err = sock_has_perm(current, sk, SOCKET__CONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 if (err)
4452 return err;
4453
4454 /*
James Morris2ee92d42006-11-13 16:09:01 -08004455 * If a TCP or DCCP socket, check name_connect permission for the port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456 */
Paul Moore253bfae2010-04-22 14:46:19 -04004457 if (sksec->sclass == SECCLASS_TCP_SOCKET ||
4458 sksec->sclass == SECCLASS_DCCP_SOCKET) {
Thomas Liu2bf49692009-07-14 12:14:09 -04004459 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004460 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 struct sockaddr_in *addr4 = NULL;
4462 struct sockaddr_in6 *addr6 = NULL;
4463 unsigned short snum;
James Morris2ee92d42006-11-13 16:09:01 -08004464 u32 sid, perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465
4466 if (sk->sk_family == PF_INET) {
4467 addr4 = (struct sockaddr_in *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004468 if (addrlen < sizeof(struct sockaddr_in))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 return -EINVAL;
4470 snum = ntohs(addr4->sin_port);
4471 } else {
4472 addr6 = (struct sockaddr_in6 *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004473 if (addrlen < SIN6_LEN_RFC2133)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 return -EINVAL;
4475 snum = ntohs(addr6->sin6_port);
4476 }
4477
Paul Moore3e112172008-04-10 10:48:14 -04004478 err = sel_netport_sid(sk->sk_protocol, snum, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 if (err)
4480 goto out;
4481
Paul Moore253bfae2010-04-22 14:46:19 -04004482 perm = (sksec->sclass == SECCLASS_TCP_SOCKET) ?
James Morris2ee92d42006-11-13 16:09:01 -08004483 TCP_SOCKET__NAME_CONNECT : DCCP_SOCKET__NAME_CONNECT;
4484
Eric Paris50c205f2012-04-04 15:01:43 -04004485 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004486 ad.u.net = &net;
4487 ad.u.net->dport = htons(snum);
4488 ad.u.net->family = sk->sk_family;
Paul Moore253bfae2010-04-22 14:46:19 -04004489 err = avc_has_perm(sksec->sid, sid, sksec->sclass, perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 if (err)
4491 goto out;
4492 }
4493
Paul Moore014ab192008-10-10 10:16:33 -04004494 err = selinux_netlbl_socket_connect(sk, address);
4495
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496out:
4497 return err;
4498}
4499
4500static int selinux_socket_listen(struct socket *sock, int backlog)
4501{
Paul Moore253bfae2010-04-22 14:46:19 -04004502 return sock_has_perm(current, sock->sk, SOCKET__LISTEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503}
4504
4505static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
4506{
4507 int err;
4508 struct inode_security_struct *isec;
4509 struct inode_security_struct *newisec;
4510
Paul Moore253bfae2010-04-22 14:46:19 -04004511 err = sock_has_perm(current, sock->sk, SOCKET__ACCEPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 if (err)
4513 return err;
4514
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05004515 newisec = inode_security_novalidate(SOCK_INODE(newsock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05004517 isec = inode_security_novalidate(SOCK_INODE(sock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 newisec->sclass = isec->sclass;
4519 newisec->sid = isec->sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004520 newisec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521
4522 return 0;
4523}
4524
4525static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
Eric Paris828dfe12008-04-17 13:17:49 -04004526 int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527{
Paul Moore253bfae2010-04-22 14:46:19 -04004528 return sock_has_perm(current, sock->sk, SOCKET__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529}
4530
4531static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
4532 int size, int flags)
4533{
Paul Moore253bfae2010-04-22 14:46:19 -04004534 return sock_has_perm(current, sock->sk, SOCKET__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535}
4536
4537static int selinux_socket_getsockname(struct socket *sock)
4538{
Paul Moore253bfae2010-04-22 14:46:19 -04004539 return sock_has_perm(current, sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540}
4541
4542static int selinux_socket_getpeername(struct socket *sock)
4543{
Paul Moore253bfae2010-04-22 14:46:19 -04004544 return sock_has_perm(current, sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545}
4546
Eric Paris828dfe12008-04-17 13:17:49 -04004547static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548{
Paul Mooref8687af2006-10-30 15:22:15 -08004549 int err;
4550
Paul Moore253bfae2010-04-22 14:46:19 -04004551 err = sock_has_perm(current, sock->sk, SOCKET__SETOPT);
Paul Mooref8687af2006-10-30 15:22:15 -08004552 if (err)
4553 return err;
4554
4555 return selinux_netlbl_socket_setsockopt(sock, level, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556}
4557
4558static int selinux_socket_getsockopt(struct socket *sock, int level,
4559 int optname)
4560{
Paul Moore253bfae2010-04-22 14:46:19 -04004561 return sock_has_perm(current, sock->sk, SOCKET__GETOPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562}
4563
4564static int selinux_socket_shutdown(struct socket *sock, int how)
4565{
Paul Moore253bfae2010-04-22 14:46:19 -04004566 return sock_has_perm(current, sock->sk, SOCKET__SHUTDOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567}
4568
David S. Miller3610cda2011-01-05 15:38:53 -08004569static int selinux_socket_unix_stream_connect(struct sock *sock,
4570 struct sock *other,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 struct sock *newsk)
4572{
David S. Miller3610cda2011-01-05 15:38:53 -08004573 struct sk_security_struct *sksec_sock = sock->sk_security;
4574 struct sk_security_struct *sksec_other = other->sk_security;
Paul Moore4d1e2452010-04-22 14:46:18 -04004575 struct sk_security_struct *sksec_new = newsk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004576 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004577 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 int err;
4579
Eric Paris50c205f2012-04-04 15:01:43 -04004580 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004581 ad.u.net = &net;
4582 ad.u.net->sk = other;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583
Paul Moore4d1e2452010-04-22 14:46:18 -04004584 err = avc_has_perm(sksec_sock->sid, sksec_other->sid,
4585 sksec_other->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 UNIX_STREAM_SOCKET__CONNECTTO, &ad);
4587 if (err)
4588 return err;
4589
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 /* server child socket */
Paul Moore4d1e2452010-04-22 14:46:18 -04004591 sksec_new->peer_sid = sksec_sock->sid;
4592 err = security_sid_mls_copy(sksec_other->sid, sksec_sock->sid,
4593 &sksec_new->sid);
4594 if (err)
4595 return err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004596
Paul Moore4d1e2452010-04-22 14:46:18 -04004597 /* connecting socket */
4598 sksec_sock->peer_sid = sksec_new->sid;
4599
4600 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601}
4602
4603static int selinux_socket_unix_may_send(struct socket *sock,
4604 struct socket *other)
4605{
Paul Moore253bfae2010-04-22 14:46:19 -04004606 struct sk_security_struct *ssec = sock->sk->sk_security;
4607 struct sk_security_struct *osec = other->sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004608 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004609 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610
Eric Paris50c205f2012-04-04 15:01:43 -04004611 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004612 ad.u.net = &net;
4613 ad.u.net->sk = other->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614
Paul Moore253bfae2010-04-22 14:46:19 -04004615 return avc_has_perm(ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
4616 &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617}
4618
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004619static int selinux_inet_sys_rcv_skb(struct net *ns, int ifindex,
4620 char *addrp, u16 family, u32 peer_sid,
Thomas Liu2bf49692009-07-14 12:14:09 -04004621 struct common_audit_data *ad)
Paul Mooreeffad8d2008-01-29 08:49:27 -05004622{
4623 int err;
4624 u32 if_sid;
4625 u32 node_sid;
4626
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004627 err = sel_netif_sid(ns, ifindex, &if_sid);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004628 if (err)
4629 return err;
4630 err = avc_has_perm(peer_sid, if_sid,
4631 SECCLASS_NETIF, NETIF__INGRESS, ad);
4632 if (err)
4633 return err;
4634
4635 err = sel_netnode_sid(addrp, family, &node_sid);
4636 if (err)
4637 return err;
4638 return avc_has_perm(peer_sid, node_sid,
4639 SECCLASS_NODE, NODE__RECVFROM, ad);
4640}
4641
Paul Moore220deb92008-01-29 08:38:23 -05004642static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
Paul Moored8395c82008-10-10 10:16:30 -04004643 u16 family)
Paul Moore220deb92008-01-29 08:38:23 -05004644{
Paul Moore277d3422008-12-31 12:54:11 -05004645 int err = 0;
Paul Moore220deb92008-01-29 08:38:23 -05004646 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004647 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004648 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004649 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04004650 char *addrp;
4651
Eric Paris50c205f2012-04-04 15:01:43 -04004652 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004653 ad.u.net = &net;
4654 ad.u.net->netif = skb->skb_iif;
4655 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004656 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4657 if (err)
4658 return err;
Paul Moore220deb92008-01-29 08:38:23 -05004659
Paul Moore58bfbb52009-03-27 17:10:41 -04004660 if (selinux_secmark_enabled()) {
Paul Moore220deb92008-01-29 08:38:23 -05004661 err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Moored8395c82008-10-10 10:16:30 -04004662 PACKET__RECV, &ad);
Paul Moore58bfbb52009-03-27 17:10:41 -04004663 if (err)
4664 return err;
4665 }
Paul Moore220deb92008-01-29 08:38:23 -05004666
Steffen Klassertb9679a72011-02-23 12:55:21 +01004667 err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
4668 if (err)
4669 return err;
4670 err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004671
James Morris4e5ab4c2006-06-09 00:33:33 -07004672 return err;
4673}
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004674
James Morris4e5ab4c2006-06-09 00:33:33 -07004675static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
4676{
Paul Moore220deb92008-01-29 08:38:23 -05004677 int err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004678 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004679 u16 family = sk->sk_family;
4680 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004681 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004682 struct lsm_network_audit net = {0,};
Paul Moore220deb92008-01-29 08:38:23 -05004683 char *addrp;
Paul Moored8395c82008-10-10 10:16:30 -04004684 u8 secmark_active;
4685 u8 peerlbl_active;
James Morris4e5ab4c2006-06-09 00:33:33 -07004686
James Morris4e5ab4c2006-06-09 00:33:33 -07004687 if (family != PF_INET && family != PF_INET6)
Paul Moore220deb92008-01-29 08:38:23 -05004688 return 0;
James Morris4e5ab4c2006-06-09 00:33:33 -07004689
4690 /* Handle mapped IPv4 packets arriving via IPv6 sockets */
Al Viro87fcd702006-12-04 22:00:55 +00004691 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
James Morris4e5ab4c2006-06-09 00:33:33 -07004692 family = PF_INET;
4693
Paul Moored8395c82008-10-10 10:16:30 -04004694 /* If any sort of compatibility mode is enabled then handoff processing
4695 * to the selinux_sock_rcv_skb_compat() function to deal with the
4696 * special handling. We do this in an attempt to keep this function
4697 * as fast and as clean as possible. */
Paul Moore58bfbb52009-03-27 17:10:41 -04004698 if (!selinux_policycap_netpeer)
Paul Moored8395c82008-10-10 10:16:30 -04004699 return selinux_sock_rcv_skb_compat(sk, skb, family);
4700
4701 secmark_active = selinux_secmark_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04004702 peerlbl_active = selinux_peerlbl_enabled();
Paul Moored8395c82008-10-10 10:16:30 -04004703 if (!secmark_active && !peerlbl_active)
4704 return 0;
4705
Eric Paris50c205f2012-04-04 15:01:43 -04004706 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004707 ad.u.net = &net;
4708 ad.u.net->netif = skb->skb_iif;
4709 ad.u.net->family = family;
Paul Moore224dfbd2008-01-29 08:38:13 -05004710 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
James Morris4e5ab4c2006-06-09 00:33:33 -07004711 if (err)
Paul Moore220deb92008-01-29 08:38:23 -05004712 return err;
James Morris4e5ab4c2006-06-09 00:33:33 -07004713
Paul Moored8395c82008-10-10 10:16:30 -04004714 if (peerlbl_active) {
Paul Moored621d352008-01-29 08:43:36 -05004715 u32 peer_sid;
4716
4717 err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
4718 if (err)
4719 return err;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004720 err = selinux_inet_sys_rcv_skb(sock_net(sk), skb->skb_iif,
4721 addrp, family, peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04004722 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04004723 selinux_netlbl_err(skb, family, err, 0);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004724 return err;
Paul Mooredfaebe92008-10-10 10:16:31 -04004725 }
Paul Moored621d352008-01-29 08:43:36 -05004726 err = avc_has_perm(sk_sid, peer_sid, SECCLASS_PEER,
4727 PEER__RECV, &ad);
Chad Hanson46d01d62013-12-23 17:45:01 -05004728 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04004729 selinux_netlbl_err(skb, family, err, 0);
Chad Hanson46d01d62013-12-23 17:45:01 -05004730 return err;
4731 }
Paul Moored621d352008-01-29 08:43:36 -05004732 }
4733
Paul Moored8395c82008-10-10 10:16:30 -04004734 if (secmark_active) {
Paul Mooreeffad8d2008-01-29 08:49:27 -05004735 err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
4736 PACKET__RECV, &ad);
4737 if (err)
4738 return err;
4739 }
4740
Paul Moored621d352008-01-29 08:43:36 -05004741 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742}
4743
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004744static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
4745 int __user *optlen, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746{
4747 int err = 0;
4748 char *scontext;
4749 u32 scontext_len;
Paul Moore253bfae2010-04-22 14:46:19 -04004750 struct sk_security_struct *sksec = sock->sk->sk_security;
Paul Moore3de4bab2006-11-17 17:38:54 -05004751 u32 peer_sid = SECSID_NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752
Paul Moore253bfae2010-04-22 14:46:19 -04004753 if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
4754 sksec->sclass == SECCLASS_TCP_SOCKET)
Eric Parisdd3e7832010-04-07 15:08:46 -04004755 peer_sid = sksec->peer_sid;
Paul Moore253bfae2010-04-22 14:46:19 -04004756 if (peer_sid == SECSID_NULL)
4757 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004759 err = security_sid_to_context(peer_sid, &scontext, &scontext_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 if (err)
Paul Moore253bfae2010-04-22 14:46:19 -04004761 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762
4763 if (scontext_len > len) {
4764 err = -ERANGE;
4765 goto out_len;
4766 }
4767
4768 if (copy_to_user(optval, scontext, scontext_len))
4769 err = -EFAULT;
4770
4771out_len:
4772 if (put_user(scontext_len, optlen))
4773 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774 kfree(scontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 return err;
4776}
4777
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004778static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004779{
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004780 u32 peer_secid = SECSID_NULL;
Paul Moore75e22912008-01-29 08:38:04 -05004781 u16 family;
Paul Moore899134f2016-03-28 15:19:10 -04004782 struct inode_security_struct *isec;
Catherine Zhang877ce7c2006-06-29 12:27:47 -07004783
Paul Mooreaa862902008-10-10 10:16:29 -04004784 if (skb && skb->protocol == htons(ETH_P_IP))
4785 family = PF_INET;
4786 else if (skb && skb->protocol == htons(ETH_P_IPV6))
4787 family = PF_INET6;
4788 else if (sock)
Paul Moore75e22912008-01-29 08:38:04 -05004789 family = sock->sk->sk_family;
Paul Moore75e22912008-01-29 08:38:04 -05004790 else
4791 goto out;
4792
Paul Moore899134f2016-03-28 15:19:10 -04004793 if (sock && family == PF_UNIX) {
4794 isec = inode_security_novalidate(SOCK_INODE(sock));
4795 peer_secid = isec->sid;
4796 } else if (skb)
Paul Moore220deb92008-01-29 08:38:23 -05004797 selinux_skb_peerlbl_sid(skb, family, &peer_secid);
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004798
Paul Moore75e22912008-01-29 08:38:04 -05004799out:
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004800 *secid = peer_secid;
Paul Moore75e22912008-01-29 08:38:04 -05004801 if (peer_secid == SECSID_NULL)
4802 return -EINVAL;
4803 return 0;
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004804}
4805
Al Viro7d877f32005-10-21 03:20:43 -04004806static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807{
Paul Moore84914b72010-04-22 14:46:18 -04004808 struct sk_security_struct *sksec;
4809
4810 sksec = kzalloc(sizeof(*sksec), priority);
4811 if (!sksec)
4812 return -ENOMEM;
4813
4814 sksec->peer_sid = SECINITSID_UNLABELED;
4815 sksec->sid = SECINITSID_UNLABELED;
Stephen Smalley5dee25d2015-07-10 17:19:57 -04004816 sksec->sclass = SECCLASS_SOCKET;
Paul Moore84914b72010-04-22 14:46:18 -04004817 selinux_netlbl_sk_security_reset(sksec);
4818 sk->sk_security = sksec;
4819
4820 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821}
4822
4823static void selinux_sk_free_security(struct sock *sk)
4824{
Paul Moore84914b72010-04-22 14:46:18 -04004825 struct sk_security_struct *sksec = sk->sk_security;
4826
4827 sk->sk_security = NULL;
4828 selinux_netlbl_sk_security_free(sksec);
4829 kfree(sksec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830}
4831
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004832static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
4833{
Eric Parisdd3e7832010-04-07 15:08:46 -04004834 struct sk_security_struct *sksec = sk->sk_security;
4835 struct sk_security_struct *newsksec = newsk->sk_security;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004836
Eric Parisdd3e7832010-04-07 15:08:46 -04004837 newsksec->sid = sksec->sid;
4838 newsksec->peer_sid = sksec->peer_sid;
4839 newsksec->sclass = sksec->sclass;
Paul Moore99f59ed2006-08-29 17:53:48 -07004840
Eric Parisdd3e7832010-04-07 15:08:46 -04004841 selinux_netlbl_sk_security_reset(newsksec);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004842}
4843
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07004844static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004845{
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004846 if (!sk)
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07004847 *secid = SECINITSID_ANY_SOCKET;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004848 else {
4849 struct sk_security_struct *sksec = sk->sk_security;
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004850
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07004851 *secid = sksec->sid;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004852 }
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004853}
4854
Eric Paris828dfe12008-04-17 13:17:49 -04004855static void selinux_sock_graft(struct sock *sk, struct socket *parent)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004856{
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05004857 struct inode_security_struct *isec =
4858 inode_security_novalidate(SOCK_INODE(parent));
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004859 struct sk_security_struct *sksec = sk->sk_security;
4860
Paul Moore2873ead2014-07-28 10:42:48 -04004861 if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
4862 sk->sk_family == PF_UNIX)
David Woodhouse2148ccc2006-09-29 15:50:25 -07004863 isec->sid = sksec->sid;
Paul Moore220deb92008-01-29 08:38:23 -05004864 sksec->sclass = isec->sclass;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004865}
4866
Adrian Bunk9a673e52006-08-15 00:03:53 -07004867static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4868 struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004869{
4870 struct sk_security_struct *sksec = sk->sk_security;
4871 int err;
Paul Moore0b1f24e2013-12-03 11:39:13 -05004872 u16 family = req->rsk_ops->family;
Paul Moore446b8022013-12-04 16:10:51 -05004873 u32 connsid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004874 u32 peersid;
4875
Paul Mooreaa862902008-10-10 10:16:29 -04004876 err = selinux_skb_peerlbl_sid(skb, family, &peersid);
Paul Moore220deb92008-01-29 08:38:23 -05004877 if (err)
4878 return err;
Paul Moore446b8022013-12-04 16:10:51 -05004879 err = selinux_conn_sid(sksec->sid, peersid, &connsid);
4880 if (err)
4881 return err;
4882 req->secid = connsid;
4883 req->peer_secid = peersid;
Venkat Yekkiralaa51c64f2006-07-27 22:01:34 -07004884
Paul Moore389fb8002009-03-27 17:10:34 -04004885 return selinux_netlbl_inet_conn_request(req, family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004886}
4887
Adrian Bunk9a673e52006-08-15 00:03:53 -07004888static void selinux_inet_csk_clone(struct sock *newsk,
4889 const struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004890{
4891 struct sk_security_struct *newsksec = newsk->sk_security;
4892
4893 newsksec->sid = req->secid;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004894 newsksec->peer_sid = req->peer_secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004895 /* NOTE: Ideally, we should also get the isec->sid for the
4896 new socket in sync, but we don't have the isec available yet.
4897 So we will wait until sock_graft to do it, by which
4898 time it will have been created and available. */
Paul Moore99f59ed2006-08-29 17:53:48 -07004899
Paul Moore9f2ad662006-11-17 17:38:53 -05004900 /* We don't need to take any sort of lock here as we are the only
4901 * thread with access to newsksec */
Paul Moore389fb8002009-03-27 17:10:34 -04004902 selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004903}
4904
Paul Moore014ab192008-10-10 10:16:33 -04004905static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004906{
Paul Mooreaa862902008-10-10 10:16:29 -04004907 u16 family = sk->sk_family;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004908 struct sk_security_struct *sksec = sk->sk_security;
4909
Paul Mooreaa862902008-10-10 10:16:29 -04004910 /* handle mapped IPv4 packets arriving via IPv6 sockets */
4911 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4912 family = PF_INET;
4913
4914 selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004915}
4916
Eric Paris2606fd12010-10-13 16:24:41 -04004917static int selinux_secmark_relabel_packet(u32 sid)
4918{
4919 const struct task_security_struct *__tsec;
4920 u32 tsid;
4921
4922 __tsec = current_security();
4923 tsid = __tsec->sid;
4924
4925 return avc_has_perm(tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO, NULL);
4926}
4927
4928static void selinux_secmark_refcount_inc(void)
4929{
4930 atomic_inc(&selinux_secmark_refcount);
4931}
4932
4933static void selinux_secmark_refcount_dec(void)
4934{
4935 atomic_dec(&selinux_secmark_refcount);
4936}
4937
Adrian Bunk9a673e52006-08-15 00:03:53 -07004938static void selinux_req_classify_flow(const struct request_sock *req,
4939 struct flowi *fl)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004940{
David S. Miller1d28f422011-03-12 00:29:39 -05004941 fl->flowi_secid = req->secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004942}
4943
Paul Moore5dbbaf22013-01-14 07:12:19 +00004944static int selinux_tun_dev_alloc_security(void **security)
4945{
4946 struct tun_security_struct *tunsec;
4947
4948 tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL);
4949 if (!tunsec)
4950 return -ENOMEM;
4951 tunsec->sid = current_sid();
4952
4953 *security = tunsec;
4954 return 0;
4955}
4956
4957static void selinux_tun_dev_free_security(void *security)
4958{
4959 kfree(security);
4960}
4961
Paul Mooreed6d76e2009-08-28 18:12:49 -04004962static int selinux_tun_dev_create(void)
4963{
4964 u32 sid = current_sid();
4965
4966 /* we aren't taking into account the "sockcreate" SID since the socket
4967 * that is being created here is not a socket in the traditional sense,
4968 * instead it is a private sock, accessible only to the kernel, and
4969 * representing a wide range of network traffic spanning multiple
4970 * connections unlike traditional sockets - check the TUN driver to
4971 * get a better understanding of why this socket is special */
4972
4973 return avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
4974 NULL);
4975}
4976
Paul Moore5dbbaf22013-01-14 07:12:19 +00004977static int selinux_tun_dev_attach_queue(void *security)
Paul Mooreed6d76e2009-08-28 18:12:49 -04004978{
Paul Moore5dbbaf22013-01-14 07:12:19 +00004979 struct tun_security_struct *tunsec = security;
4980
4981 return avc_has_perm(current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET,
4982 TUN_SOCKET__ATTACH_QUEUE, NULL);
4983}
4984
4985static int selinux_tun_dev_attach(struct sock *sk, void *security)
4986{
4987 struct tun_security_struct *tunsec = security;
Paul Mooreed6d76e2009-08-28 18:12:49 -04004988 struct sk_security_struct *sksec = sk->sk_security;
4989
4990 /* we don't currently perform any NetLabel based labeling here and it
4991 * isn't clear that we would want to do so anyway; while we could apply
4992 * labeling without the support of the TUN user the resulting labeled
4993 * traffic from the other end of the connection would almost certainly
4994 * cause confusion to the TUN user that had no idea network labeling
4995 * protocols were being used */
4996
Paul Moore5dbbaf22013-01-14 07:12:19 +00004997 sksec->sid = tunsec->sid;
Paul Mooreed6d76e2009-08-28 18:12:49 -04004998 sksec->sclass = SECCLASS_TUN_SOCKET;
Paul Moore5dbbaf22013-01-14 07:12:19 +00004999
5000 return 0;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005001}
5002
Paul Moore5dbbaf22013-01-14 07:12:19 +00005003static int selinux_tun_dev_open(void *security)
Paul Mooreed6d76e2009-08-28 18:12:49 -04005004{
Paul Moore5dbbaf22013-01-14 07:12:19 +00005005 struct tun_security_struct *tunsec = security;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005006 u32 sid = current_sid();
5007 int err;
5008
Paul Moore5dbbaf22013-01-14 07:12:19 +00005009 err = avc_has_perm(sid, tunsec->sid, SECCLASS_TUN_SOCKET,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005010 TUN_SOCKET__RELABELFROM, NULL);
5011 if (err)
5012 return err;
5013 err = avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET,
5014 TUN_SOCKET__RELABELTO, NULL);
5015 if (err)
5016 return err;
Paul Moore5dbbaf22013-01-14 07:12:19 +00005017 tunsec->sid = sid;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005018
5019 return 0;
5020}
5021
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
5023{
Paul Moorec2df86a2020-04-28 09:59:02 -04005024 int rc = 0;
5025 unsigned int msg_len;
5026 unsigned int data_len = skb->len;
5027 unsigned char *data = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028 struct nlmsghdr *nlh;
Paul Moore253bfae2010-04-22 14:46:19 -04005029 struct sk_security_struct *sksec = sk->sk_security;
Paul Moorec2df86a2020-04-28 09:59:02 -04005030 u16 sclass = sksec->sclass;
5031 u32 perm;
Eric Paris828dfe12008-04-17 13:17:49 -04005032
Paul Moorec2df86a2020-04-28 09:59:02 -04005033 while (data_len >= nlmsg_total_size(0)) {
5034 nlh = (struct nlmsghdr *)data;
Eric Paris828dfe12008-04-17 13:17:49 -04005035
Paul Moorec2df86a2020-04-28 09:59:02 -04005036 /* NOTE: the nlmsg_len field isn't reliably set by some netlink
5037 * users which means we can't reject skb's with bogus
5038 * length fields; our solution is to follow what
5039 * netlink_rcv_skb() does and simply skip processing at
5040 * messages with length fields that are clearly junk
5041 */
5042 if (nlh->nlmsg_len < NLMSG_HDRLEN || nlh->nlmsg_len > data_len)
5043 return 0;
5044
5045 rc = selinux_nlmsg_lookup(sclass, nlh->nlmsg_type, &perm);
5046 if (rc == 0) {
5047 rc = sock_has_perm(current, sk, perm);
5048 if (rc)
5049 return rc;
5050 } else if (rc == -EINVAL) {
5051 /* -EINVAL is a missing msg/perm mapping */
Vladis Dronov76319942015-12-24 11:09:41 -05005052 pr_warn_ratelimited("SELinux: unrecognized netlink"
Paul Moorec2df86a2020-04-28 09:59:02 -04005053 " message: protocol=%hu nlmsg_type=%hu sclass=%s"
5054 " pid=%d comm=%s\n",
5055 sk->sk_protocol, nlh->nlmsg_type,
5056 secclass_map[sclass - 1].name,
5057 task_pid_nr(current), current->comm);
5058 if (selinux_enforcing && !security_get_allow_unknown())
5059 return rc;
5060 rc = 0;
5061 } else if (rc == -ENOENT) {
5062 /* -ENOENT is a missing socket/class mapping, ignore */
5063 rc = 0;
5064 } else {
5065 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066 }
5067
Paul Moorec2df86a2020-04-28 09:59:02 -04005068 /* move to the next message after applying netlink padding */
5069 msg_len = NLMSG_ALIGN(nlh->nlmsg_len);
5070 if (msg_len >= data_len)
5071 return 0;
5072 data_len -= msg_len;
5073 data += msg_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 }
5075
Paul Moorec2df86a2020-04-28 09:59:02 -04005076 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077}
5078
5079#ifdef CONFIG_NETFILTER
5080
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005081static unsigned int selinux_ip_forward(struct sk_buff *skb,
5082 const struct net_device *indev,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005083 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084{
Paul Mooredfaebe92008-10-10 10:16:31 -04005085 int err;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005086 char *addrp;
5087 u32 peer_sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04005088 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005089 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05005090 u8 secmark_active;
Paul Moore948bf852008-10-10 10:16:32 -04005091 u8 netlbl_active;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005092 u8 peerlbl_active;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005093
Paul Mooreeffad8d2008-01-29 08:49:27 -05005094 if (!selinux_policycap_netpeer)
5095 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005096
Paul Mooreeffad8d2008-01-29 08:49:27 -05005097 secmark_active = selinux_secmark_enabled();
Paul Moore948bf852008-10-10 10:16:32 -04005098 netlbl_active = netlbl_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04005099 peerlbl_active = selinux_peerlbl_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05005100 if (!secmark_active && !peerlbl_active)
5101 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005102
Paul Moored8395c82008-10-10 10:16:30 -04005103 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
5104 return NF_DROP;
5105
Eric Paris50c205f2012-04-04 15:01:43 -04005106 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005107 ad.u.net = &net;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005108 ad.u.net->netif = indev->ifindex;
Eric Paris48c62af2012-04-02 13:15:44 -04005109 ad.u.net->family = family;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005110 if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
5111 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112
Paul Mooredfaebe92008-10-10 10:16:31 -04005113 if (peerlbl_active) {
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005114 err = selinux_inet_sys_rcv_skb(dev_net(indev), indev->ifindex,
5115 addrp, family, peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04005116 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04005117 selinux_netlbl_err(skb, family, err, 1);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005118 return NF_DROP;
Paul Mooredfaebe92008-10-10 10:16:31 -04005119 }
5120 }
Paul Mooreeffad8d2008-01-29 08:49:27 -05005121
5122 if (secmark_active)
5123 if (avc_has_perm(peer_sid, skb->secmark,
5124 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
5125 return NF_DROP;
5126
Paul Moore948bf852008-10-10 10:16:32 -04005127 if (netlbl_active)
5128 /* we do this in the FORWARD path and not the POST_ROUTING
5129 * path because we want to make sure we apply the necessary
5130 * labeling before IPsec is applied so we can leverage AH
5131 * protection */
5132 if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
5133 return NF_DROP;
5134
Paul Mooreeffad8d2008-01-29 08:49:27 -05005135 return NF_ACCEPT;
5136}
5137
Eric W. Biederman06198b32015-09-18 14:33:06 -05005138static unsigned int selinux_ipv4_forward(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005139 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005140 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005141{
David S. Miller238e54c2015-04-03 20:32:56 -04005142 return selinux_ip_forward(skb, state->in, PF_INET);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005143}
5144
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005145#if IS_ENABLED(CONFIG_IPV6)
Eric W. Biederman06198b32015-09-18 14:33:06 -05005146static unsigned int selinux_ipv6_forward(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005147 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005148 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005149{
David S. Miller238e54c2015-04-03 20:32:56 -04005150 return selinux_ip_forward(skb, state->in, PF_INET6);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005151}
5152#endif /* IPV6 */
5153
Paul Moore948bf852008-10-10 10:16:32 -04005154static unsigned int selinux_ip_output(struct sk_buff *skb,
5155 u16 family)
5156{
Paul Moore47180062013-12-04 16:10:45 -05005157 struct sock *sk;
Paul Moore948bf852008-10-10 10:16:32 -04005158 u32 sid;
5159
5160 if (!netlbl_enabled())
5161 return NF_ACCEPT;
5162
5163 /* we do this in the LOCAL_OUT path and not the POST_ROUTING path
5164 * because we want to make sure we apply the necessary labeling
5165 * before IPsec is applied so we can leverage AH protection */
Paul Moore47180062013-12-04 16:10:45 -05005166 sk = skb->sk;
5167 if (sk) {
5168 struct sk_security_struct *sksec;
5169
Eric Dumazete446f9d2015-10-08 05:01:55 -07005170 if (sk_listener(sk))
Paul Moore47180062013-12-04 16:10:45 -05005171 /* if the socket is the listening state then this
5172 * packet is a SYN-ACK packet which means it needs to
5173 * be labeled based on the connection/request_sock and
5174 * not the parent socket. unfortunately, we can't
5175 * lookup the request_sock yet as it isn't queued on
5176 * the parent socket until after the SYN-ACK is sent.
5177 * the "solution" is to simply pass the packet as-is
5178 * as any IP option based labeling should be copied
5179 * from the initial connection request (in the IP
5180 * layer). it is far from ideal, but until we get a
5181 * security label in the packet itself this is the
5182 * best we can do. */
5183 return NF_ACCEPT;
5184
5185 /* standard practice, label using the parent socket */
5186 sksec = sk->sk_security;
Paul Moore948bf852008-10-10 10:16:32 -04005187 sid = sksec->sid;
5188 } else
5189 sid = SECINITSID_KERNEL;
5190 if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
5191 return NF_DROP;
5192
5193 return NF_ACCEPT;
5194}
5195
Eric W. Biederman06198b32015-09-18 14:33:06 -05005196static unsigned int selinux_ipv4_output(void *priv,
Paul Moore948bf852008-10-10 10:16:32 -04005197 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005198 const struct nf_hook_state *state)
Paul Moore948bf852008-10-10 10:16:32 -04005199{
5200 return selinux_ip_output(skb, PF_INET);
5201}
5202
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005203#if IS_ENABLED(CONFIG_IPV6)
Huw Davies2917f572016-06-27 15:06:15 -04005204static unsigned int selinux_ipv6_output(void *priv,
5205 struct sk_buff *skb,
5206 const struct nf_hook_state *state)
5207{
5208 return selinux_ip_output(skb, PF_INET6);
5209}
5210#endif /* IPV6 */
5211
Paul Mooreeffad8d2008-01-29 08:49:27 -05005212static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
5213 int ifindex,
Paul Moored8395c82008-10-10 10:16:30 -04005214 u16 family)
James Morris4e5ab4c2006-06-09 00:33:33 -07005215{
Eric Dumazet54abc682015-11-08 10:54:07 -08005216 struct sock *sk = skb_to_full_sk(skb);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005217 struct sk_security_struct *sksec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005218 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005219 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04005220 char *addrp;
5221 u8 proto;
James Morris4e5ab4c2006-06-09 00:33:33 -07005222
Paul Mooreeffad8d2008-01-29 08:49:27 -05005223 if (sk == NULL)
5224 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005225 sksec = sk->sk_security;
James Morris4e5ab4c2006-06-09 00:33:33 -07005226
Eric Paris50c205f2012-04-04 15:01:43 -04005227 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005228 ad.u.net = &net;
5229 ad.u.net->netif = ifindex;
5230 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04005231 if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
5232 return NF_DROP;
5233
Paul Moore58bfbb52009-03-27 17:10:41 -04005234 if (selinux_secmark_enabled())
Paul Mooreeffad8d2008-01-29 08:49:27 -05005235 if (avc_has_perm(sksec->sid, skb->secmark,
Paul Moored8395c82008-10-10 10:16:30 -04005236 SECCLASS_PACKET, PACKET__SEND, &ad))
Eric Paris2fe66ec2010-11-23 06:28:08 +00005237 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07005238
Steffen Klassertb9679a72011-02-23 12:55:21 +01005239 if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
5240 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07005241
Paul Mooreeffad8d2008-01-29 08:49:27 -05005242 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243}
5244
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005245static unsigned int selinux_ip_postroute(struct sk_buff *skb,
5246 const struct net_device *outdev,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005247 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248{
Paul Mooreeffad8d2008-01-29 08:49:27 -05005249 u32 secmark_perm;
5250 u32 peer_sid;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005251 int ifindex = outdev->ifindex;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005252 struct sock *sk;
Thomas Liu2bf49692009-07-14 12:14:09 -04005253 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005254 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05005255 char *addrp;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005256 u8 secmark_active;
5257 u8 peerlbl_active;
5258
Paul Mooreeffad8d2008-01-29 08:49:27 -05005259 /* If any sort of compatibility mode is enabled then handoff processing
5260 * to the selinux_ip_postroute_compat() function to deal with the
5261 * special handling. We do this in an attempt to keep this function
5262 * as fast and as clean as possible. */
Paul Moore58bfbb52009-03-27 17:10:41 -04005263 if (!selinux_policycap_netpeer)
Paul Moored8395c82008-10-10 10:16:30 -04005264 return selinux_ip_postroute_compat(skb, ifindex, family);
Paul Moorec0828e52013-12-10 14:58:01 -05005265
Paul Mooreeffad8d2008-01-29 08:49:27 -05005266 secmark_active = selinux_secmark_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04005267 peerlbl_active = selinux_peerlbl_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05005268 if (!secmark_active && !peerlbl_active)
5269 return NF_ACCEPT;
5270
Eric Dumazet54abc682015-11-08 10:54:07 -08005271 sk = skb_to_full_sk(skb);
Paul Moorec0828e52013-12-10 14:58:01 -05005272
Paul Mooreeffad8d2008-01-29 08:49:27 -05005273#ifdef CONFIG_XFRM
5274 /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
5275 * packet transformation so allow the packet to pass without any checks
5276 * since we'll have another chance to perform access control checks
5277 * when the packet is on it's final way out.
5278 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
Paul Moorec0828e52013-12-10 14:58:01 -05005279 * is NULL, in this case go ahead and apply access control.
5280 * NOTE: if this is a local socket (skb->sk != NULL) that is in the
5281 * TCP listening state we cannot wait until the XFRM processing
5282 * is done as we will miss out on the SA label if we do;
5283 * unfortunately, this means more work, but it is only once per
5284 * connection. */
5285 if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL &&
Eric Dumazete446f9d2015-10-08 05:01:55 -07005286 !(sk && sk_listener(sk)))
Paul Mooreeffad8d2008-01-29 08:49:27 -05005287 return NF_ACCEPT;
5288#endif
Paul Mooreeffad8d2008-01-29 08:49:27 -05005289
Paul Moored8395c82008-10-10 10:16:30 -04005290 if (sk == NULL) {
Paul Moore446b8022013-12-04 16:10:51 -05005291 /* Without an associated socket the packet is either coming
5292 * from the kernel or it is being forwarded; check the packet
5293 * to determine which and if the packet is being forwarded
5294 * query the packet directly to determine the security label. */
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005295 if (skb->skb_iif) {
5296 secmark_perm = PACKET__FORWARD_OUT;
Paul Moored8395c82008-10-10 10:16:30 -04005297 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005298 return NF_DROP;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005299 } else {
5300 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04005301 peer_sid = SECINITSID_KERNEL;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005302 }
Eric Dumazete446f9d2015-10-08 05:01:55 -07005303 } else if (sk_listener(sk)) {
Paul Moore446b8022013-12-04 16:10:51 -05005304 /* Locally generated packet but the associated socket is in the
5305 * listening state which means this is a SYN-ACK packet. In
5306 * this particular case the correct security label is assigned
5307 * to the connection/request_sock but unfortunately we can't
5308 * query the request_sock as it isn't queued on the parent
5309 * socket until after the SYN-ACK packet is sent; the only
5310 * viable choice is to regenerate the label like we do in
5311 * selinux_inet_conn_request(). See also selinux_ip_output()
5312 * for similar problems. */
5313 u32 skb_sid;
Eric Dumazete446f9d2015-10-08 05:01:55 -07005314 struct sk_security_struct *sksec;
5315
Eric Dumazete446f9d2015-10-08 05:01:55 -07005316 sksec = sk->sk_security;
Paul Moore446b8022013-12-04 16:10:51 -05005317 if (selinux_skb_peerlbl_sid(skb, family, &skb_sid))
5318 return NF_DROP;
Paul Moorec0828e52013-12-10 14:58:01 -05005319 /* At this point, if the returned skb peerlbl is SECSID_NULL
5320 * and the packet has been through at least one XFRM
5321 * transformation then we must be dealing with the "final"
5322 * form of labeled IPsec packet; since we've already applied
5323 * all of our access controls on this packet we can safely
5324 * pass the packet. */
5325 if (skb_sid == SECSID_NULL) {
5326 switch (family) {
5327 case PF_INET:
5328 if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
5329 return NF_ACCEPT;
5330 break;
5331 case PF_INET6:
5332 if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED)
5333 return NF_ACCEPT;
Paul Moorea7a91a12014-09-03 10:51:59 -04005334 break;
Paul Moorec0828e52013-12-10 14:58:01 -05005335 default:
5336 return NF_DROP_ERR(-ECONNREFUSED);
5337 }
5338 }
Paul Moore446b8022013-12-04 16:10:51 -05005339 if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid))
5340 return NF_DROP;
5341 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04005342 } else {
Paul Moore446b8022013-12-04 16:10:51 -05005343 /* Locally generated packet, fetch the security label from the
5344 * associated socket. */
Paul Mooreeffad8d2008-01-29 08:49:27 -05005345 struct sk_security_struct *sksec = sk->sk_security;
5346 peer_sid = sksec->sid;
5347 secmark_perm = PACKET__SEND;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005348 }
5349
Eric Paris50c205f2012-04-04 15:01:43 -04005350 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005351 ad.u.net = &net;
5352 ad.u.net->netif = ifindex;
5353 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04005354 if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
Eric Paris04f6d702010-11-23 06:28:02 +00005355 return NF_DROP;
Paul Moored8395c82008-10-10 10:16:30 -04005356
Paul Mooreeffad8d2008-01-29 08:49:27 -05005357 if (secmark_active)
5358 if (avc_has_perm(peer_sid, skb->secmark,
5359 SECCLASS_PACKET, secmark_perm, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005360 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005361
5362 if (peerlbl_active) {
5363 u32 if_sid;
5364 u32 node_sid;
5365
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005366 if (sel_netif_sid(dev_net(outdev), ifindex, &if_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005367 return NF_DROP;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005368 if (avc_has_perm(peer_sid, if_sid,
5369 SECCLASS_NETIF, NETIF__EGRESS, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005370 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005371
5372 if (sel_netnode_sid(addrp, family, &node_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005373 return NF_DROP;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005374 if (avc_has_perm(peer_sid, node_sid,
5375 SECCLASS_NODE, NODE__SENDTO, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005376 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005377 }
5378
5379 return NF_ACCEPT;
5380}
5381
Eric W. Biederman06198b32015-09-18 14:33:06 -05005382static unsigned int selinux_ipv4_postroute(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005383 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005384 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005385{
David S. Miller238e54c2015-04-03 20:32:56 -04005386 return selinux_ip_postroute(skb, state->out, PF_INET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387}
5388
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005389#if IS_ENABLED(CONFIG_IPV6)
Eric W. Biederman06198b32015-09-18 14:33:06 -05005390static unsigned int selinux_ipv6_postroute(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005391 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005392 const struct nf_hook_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393{
David S. Miller238e54c2015-04-03 20:32:56 -04005394 return selinux_ip_postroute(skb, state->out, PF_INET6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396#endif /* IPV6 */
5397
5398#endif /* CONFIG_NETFILTER */
5399
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
5401{
Stephen Smalley941fc5b2009-10-01 14:48:23 -04005402 return selinux_nlmsg_perm(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403}
5404
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405static int ipc_alloc_security(struct task_struct *task,
5406 struct kern_ipc_perm *perm,
5407 u16 sclass)
5408{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409 struct ipc_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +11005410 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411
James Morris89d155e2005-10-30 14:59:21 -08005412 isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 if (!isec)
5414 return -ENOMEM;
5415
David Howells275bb412008-11-14 10:39:19 +11005416 sid = task_sid(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417 isec->sclass = sclass;
David Howells275bb412008-11-14 10:39:19 +11005418 isec->sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419 perm->security = isec;
5420
5421 return 0;
5422}
5423
5424static void ipc_free_security(struct kern_ipc_perm *perm)
5425{
5426 struct ipc_security_struct *isec = perm->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 perm->security = NULL;
5428 kfree(isec);
5429}
5430
5431static int msg_msg_alloc_security(struct msg_msg *msg)
5432{
5433 struct msg_security_struct *msec;
5434
James Morris89d155e2005-10-30 14:59:21 -08005435 msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 if (!msec)
5437 return -ENOMEM;
5438
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 msec->sid = SECINITSID_UNLABELED;
5440 msg->security = msec;
5441
5442 return 0;
5443}
5444
5445static void msg_msg_free_security(struct msg_msg *msg)
5446{
5447 struct msg_security_struct *msec = msg->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448
5449 msg->security = NULL;
5450 kfree(msec);
5451}
5452
5453static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
Stephen Smalley6af963f2005-05-01 08:58:39 -07005454 u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005457 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005458 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460 isec = ipc_perms->security;
5461
Eric Paris50c205f2012-04-04 15:01:43 -04005462 ad.type = LSM_AUDIT_DATA_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463 ad.u.ipc_id = ipc_perms->key;
5464
David Howells275bb412008-11-14 10:39:19 +11005465 return avc_has_perm(sid, isec->sid, isec->sclass, perms, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466}
5467
5468static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
5469{
5470 return msg_msg_alloc_security(msg);
5471}
5472
5473static void selinux_msg_msg_free_security(struct msg_msg *msg)
5474{
5475 msg_msg_free_security(msg);
5476}
5477
5478/* message queue security operations */
5479static int selinux_msg_queue_alloc_security(struct msg_queue *msq)
5480{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005482 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005483 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484 int rc;
5485
5486 rc = ipc_alloc_security(current, &msq->q_perm, SECCLASS_MSGQ);
5487 if (rc)
5488 return rc;
5489
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490 isec = msq->q_perm.security;
5491
Eric Paris50c205f2012-04-04 15:01:43 -04005492 ad.type = LSM_AUDIT_DATA_IPC;
Eric Paris828dfe12008-04-17 13:17:49 -04005493 ad.u.ipc_id = msq->q_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494
David Howells275bb412008-11-14 10:39:19 +11005495 rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496 MSGQ__CREATE, &ad);
5497 if (rc) {
5498 ipc_free_security(&msq->q_perm);
5499 return rc;
5500 }
5501 return 0;
5502}
5503
5504static void selinux_msg_queue_free_security(struct msg_queue *msq)
5505{
5506 ipc_free_security(&msq->q_perm);
5507}
5508
5509static int selinux_msg_queue_associate(struct msg_queue *msq, int msqflg)
5510{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005512 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005513 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515 isec = msq->q_perm.security;
5516
Eric Paris50c205f2012-04-04 15:01:43 -04005517 ad.type = LSM_AUDIT_DATA_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518 ad.u.ipc_id = msq->q_perm.key;
5519
David Howells275bb412008-11-14 10:39:19 +11005520 return avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521 MSGQ__ASSOCIATE, &ad);
5522}
5523
5524static int selinux_msg_queue_msgctl(struct msg_queue *msq, int cmd)
5525{
5526 int err;
5527 int perms;
5528
Eric Paris828dfe12008-04-17 13:17:49 -04005529 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530 case IPC_INFO:
5531 case MSG_INFO:
5532 /* No specific object, just general system-wide information. */
5533 return task_has_system(current, SYSTEM__IPC_INFO);
5534 case IPC_STAT:
5535 case MSG_STAT:
5536 perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
5537 break;
5538 case IPC_SET:
5539 perms = MSGQ__SETATTR;
5540 break;
5541 case IPC_RMID:
5542 perms = MSGQ__DESTROY;
5543 break;
5544 default:
5545 return 0;
5546 }
5547
Stephen Smalley6af963f2005-05-01 08:58:39 -07005548 err = ipc_has_perm(&msq->q_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549 return err;
5550}
5551
5552static int selinux_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg, int msqflg)
5553{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554 struct ipc_security_struct *isec;
5555 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005556 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005557 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558 int rc;
5559
Linus Torvalds1da177e2005-04-16 15:20:36 -07005560 isec = msq->q_perm.security;
5561 msec = msg->security;
5562
5563 /*
5564 * First time through, need to assign label to the message
5565 */
5566 if (msec->sid == SECINITSID_UNLABELED) {
5567 /*
5568 * Compute new sid based on current process and
5569 * message queue this message will be stored in
5570 */
David Howells275bb412008-11-14 10:39:19 +11005571 rc = security_transition_sid(sid, isec->sid, SECCLASS_MSG,
Eric Paris652bb9b2011-02-01 11:05:40 -05005572 NULL, &msec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 if (rc)
5574 return rc;
5575 }
5576
Eric Paris50c205f2012-04-04 15:01:43 -04005577 ad.type = LSM_AUDIT_DATA_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578 ad.u.ipc_id = msq->q_perm.key;
5579
5580 /* Can this process write to the queue? */
David Howells275bb412008-11-14 10:39:19 +11005581 rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582 MSGQ__WRITE, &ad);
5583 if (!rc)
5584 /* Can this process send the message */
David Howells275bb412008-11-14 10:39:19 +11005585 rc = avc_has_perm(sid, msec->sid, SECCLASS_MSG,
5586 MSG__SEND, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587 if (!rc)
5588 /* Can the message be put in the queue? */
David Howells275bb412008-11-14 10:39:19 +11005589 rc = avc_has_perm(msec->sid, isec->sid, SECCLASS_MSGQ,
5590 MSGQ__ENQUEUE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591
5592 return rc;
5593}
5594
5595static int selinux_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
5596 struct task_struct *target,
5597 long type, int mode)
5598{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599 struct ipc_security_struct *isec;
5600 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005601 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005602 u32 sid = task_sid(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603 int rc;
5604
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605 isec = msq->q_perm.security;
5606 msec = msg->security;
5607
Eric Paris50c205f2012-04-04 15:01:43 -04005608 ad.type = LSM_AUDIT_DATA_IPC;
Eric Paris828dfe12008-04-17 13:17:49 -04005609 ad.u.ipc_id = msq->q_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610
David Howells275bb412008-11-14 10:39:19 +11005611 rc = avc_has_perm(sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612 SECCLASS_MSGQ, MSGQ__READ, &ad);
5613 if (!rc)
David Howells275bb412008-11-14 10:39:19 +11005614 rc = avc_has_perm(sid, msec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615 SECCLASS_MSG, MSG__RECEIVE, &ad);
5616 return rc;
5617}
5618
5619/* Shared Memory security operations */
5620static int selinux_shm_alloc_security(struct shmid_kernel *shp)
5621{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005623 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005624 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625 int rc;
5626
5627 rc = ipc_alloc_security(current, &shp->shm_perm, SECCLASS_SHM);
5628 if (rc)
5629 return rc;
5630
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631 isec = shp->shm_perm.security;
5632
Eric Paris50c205f2012-04-04 15:01:43 -04005633 ad.type = LSM_AUDIT_DATA_IPC;
Eric Paris828dfe12008-04-17 13:17:49 -04005634 ad.u.ipc_id = shp->shm_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635
David Howells275bb412008-11-14 10:39:19 +11005636 rc = avc_has_perm(sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637 SHM__CREATE, &ad);
5638 if (rc) {
5639 ipc_free_security(&shp->shm_perm);
5640 return rc;
5641 }
5642 return 0;
5643}
5644
5645static void selinux_shm_free_security(struct shmid_kernel *shp)
5646{
5647 ipc_free_security(&shp->shm_perm);
5648}
5649
5650static int selinux_shm_associate(struct shmid_kernel *shp, int shmflg)
5651{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005653 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005654 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656 isec = shp->shm_perm.security;
5657
Eric Paris50c205f2012-04-04 15:01:43 -04005658 ad.type = LSM_AUDIT_DATA_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659 ad.u.ipc_id = shp->shm_perm.key;
5660
David Howells275bb412008-11-14 10:39:19 +11005661 return avc_has_perm(sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662 SHM__ASSOCIATE, &ad);
5663}
5664
5665/* Note, at this point, shp is locked down */
5666static int selinux_shm_shmctl(struct shmid_kernel *shp, int cmd)
5667{
5668 int perms;
5669 int err;
5670
Eric Paris828dfe12008-04-17 13:17:49 -04005671 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672 case IPC_INFO:
5673 case SHM_INFO:
5674 /* No specific object, just general system-wide information. */
5675 return task_has_system(current, SYSTEM__IPC_INFO);
5676 case IPC_STAT:
5677 case SHM_STAT:
5678 perms = SHM__GETATTR | SHM__ASSOCIATE;
5679 break;
5680 case IPC_SET:
5681 perms = SHM__SETATTR;
5682 break;
5683 case SHM_LOCK:
5684 case SHM_UNLOCK:
5685 perms = SHM__LOCK;
5686 break;
5687 case IPC_RMID:
5688 perms = SHM__DESTROY;
5689 break;
5690 default:
5691 return 0;
5692 }
5693
Stephen Smalley6af963f2005-05-01 08:58:39 -07005694 err = ipc_has_perm(&shp->shm_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695 return err;
5696}
5697
5698static int selinux_shm_shmat(struct shmid_kernel *shp,
5699 char __user *shmaddr, int shmflg)
5700{
5701 u32 perms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702
5703 if (shmflg & SHM_RDONLY)
5704 perms = SHM__READ;
5705 else
5706 perms = SHM__READ | SHM__WRITE;
5707
Stephen Smalley6af963f2005-05-01 08:58:39 -07005708 return ipc_has_perm(&shp->shm_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709}
5710
5711/* Semaphore security operations */
5712static int selinux_sem_alloc_security(struct sem_array *sma)
5713{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005715 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005716 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717 int rc;
5718
5719 rc = ipc_alloc_security(current, &sma->sem_perm, SECCLASS_SEM);
5720 if (rc)
5721 return rc;
5722
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723 isec = sma->sem_perm.security;
5724
Eric Paris50c205f2012-04-04 15:01:43 -04005725 ad.type = LSM_AUDIT_DATA_IPC;
Eric Paris828dfe12008-04-17 13:17:49 -04005726 ad.u.ipc_id = sma->sem_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727
David Howells275bb412008-11-14 10:39:19 +11005728 rc = avc_has_perm(sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729 SEM__CREATE, &ad);
5730 if (rc) {
5731 ipc_free_security(&sma->sem_perm);
5732 return rc;
5733 }
5734 return 0;
5735}
5736
5737static void selinux_sem_free_security(struct sem_array *sma)
5738{
5739 ipc_free_security(&sma->sem_perm);
5740}
5741
5742static int selinux_sem_associate(struct sem_array *sma, int semflg)
5743{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005745 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005746 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748 isec = sma->sem_perm.security;
5749
Eric Paris50c205f2012-04-04 15:01:43 -04005750 ad.type = LSM_AUDIT_DATA_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751 ad.u.ipc_id = sma->sem_perm.key;
5752
David Howells275bb412008-11-14 10:39:19 +11005753 return avc_has_perm(sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754 SEM__ASSOCIATE, &ad);
5755}
5756
5757/* Note, at this point, sma is locked down */
5758static int selinux_sem_semctl(struct sem_array *sma, int cmd)
5759{
5760 int err;
5761 u32 perms;
5762
Eric Paris828dfe12008-04-17 13:17:49 -04005763 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764 case IPC_INFO:
5765 case SEM_INFO:
5766 /* No specific object, just general system-wide information. */
5767 return task_has_system(current, SYSTEM__IPC_INFO);
5768 case GETPID:
5769 case GETNCNT:
5770 case GETZCNT:
5771 perms = SEM__GETATTR;
5772 break;
5773 case GETVAL:
5774 case GETALL:
5775 perms = SEM__READ;
5776 break;
5777 case SETVAL:
5778 case SETALL:
5779 perms = SEM__WRITE;
5780 break;
5781 case IPC_RMID:
5782 perms = SEM__DESTROY;
5783 break;
5784 case IPC_SET:
5785 perms = SEM__SETATTR;
5786 break;
5787 case IPC_STAT:
5788 case SEM_STAT:
5789 perms = SEM__GETATTR | SEM__ASSOCIATE;
5790 break;
5791 default:
5792 return 0;
5793 }
5794
Stephen Smalley6af963f2005-05-01 08:58:39 -07005795 err = ipc_has_perm(&sma->sem_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796 return err;
5797}
5798
5799static int selinux_sem_semop(struct sem_array *sma,
5800 struct sembuf *sops, unsigned nsops, int alter)
5801{
5802 u32 perms;
5803
5804 if (alter)
5805 perms = SEM__READ | SEM__WRITE;
5806 else
5807 perms = SEM__READ;
5808
Stephen Smalley6af963f2005-05-01 08:58:39 -07005809 return ipc_has_perm(&sma->sem_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005810}
5811
5812static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
5813{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814 u32 av = 0;
5815
Linus Torvalds1da177e2005-04-16 15:20:36 -07005816 av = 0;
5817 if (flag & S_IRUGO)
5818 av |= IPC__UNIX_READ;
5819 if (flag & S_IWUGO)
5820 av |= IPC__UNIX_WRITE;
5821
5822 if (av == 0)
5823 return 0;
5824
Stephen Smalley6af963f2005-05-01 08:58:39 -07005825 return ipc_has_perm(ipcp, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826}
5827
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02005828static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
5829{
5830 struct ipc_security_struct *isec = ipcp->security;
5831 *secid = isec->sid;
5832}
5833
Eric Paris828dfe12008-04-17 13:17:49 -04005834static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835{
5836 if (inode)
5837 inode_doinit_with_dentry(inode, dentry);
5838}
5839
5840static int selinux_getprocattr(struct task_struct *p,
Al Viro04ff9702007-03-12 16:17:58 +00005841 char *name, char **value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842{
David Howells275bb412008-11-14 10:39:19 +11005843 const struct task_security_struct *__tsec;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00005844 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005845 int error;
Al Viro04ff9702007-03-12 16:17:58 +00005846 unsigned len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005847
5848 if (current != p) {
David Howells3b11a1d2008-11-14 10:39:26 +11005849 error = current_has_perm(p, PROCESS__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005850 if (error)
5851 return error;
5852 }
5853
David Howells275bb412008-11-14 10:39:19 +11005854 rcu_read_lock();
5855 __tsec = __task_cred(p)->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856
5857 if (!strcmp(name, "current"))
David Howells275bb412008-11-14 10:39:19 +11005858 sid = __tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859 else if (!strcmp(name, "prev"))
David Howells275bb412008-11-14 10:39:19 +11005860 sid = __tsec->osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005861 else if (!strcmp(name, "exec"))
David Howells275bb412008-11-14 10:39:19 +11005862 sid = __tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863 else if (!strcmp(name, "fscreate"))
David Howells275bb412008-11-14 10:39:19 +11005864 sid = __tsec->create_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07005865 else if (!strcmp(name, "keycreate"))
David Howells275bb412008-11-14 10:39:19 +11005866 sid = __tsec->keycreate_sid;
Eric Paris42c3e032006-06-26 00:26:03 -07005867 else if (!strcmp(name, "sockcreate"))
David Howells275bb412008-11-14 10:39:19 +11005868 sid = __tsec->sockcreate_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869 else
David Howells275bb412008-11-14 10:39:19 +11005870 goto invalid;
5871 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005872
5873 if (!sid)
5874 return 0;
5875
Al Viro04ff9702007-03-12 16:17:58 +00005876 error = security_sid_to_context(sid, value, &len);
5877 if (error)
5878 return error;
5879 return len;
David Howells275bb412008-11-14 10:39:19 +11005880
5881invalid:
5882 rcu_read_unlock();
5883 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005884}
5885
5886static int selinux_setprocattr(struct task_struct *p,
5887 char *name, void *value, size_t size)
5888{
5889 struct task_security_struct *tsec;
David Howellsd84f4f92008-11-14 10:39:23 +11005890 struct cred *new;
5891 u32 sid = 0, ptsid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892 int error;
5893 char *str = value;
5894
5895 if (current != p) {
5896 /* SELinux only allows a process to change its own
5897 security attributes. */
5898 return -EACCES;
5899 }
5900
5901 /*
5902 * Basic control over ability to set these attributes at all.
5903 * current == p, but we'll pass them separately in case the
5904 * above restriction is ever removed.
5905 */
5906 if (!strcmp(name, "exec"))
David Howells3b11a1d2008-11-14 10:39:26 +11005907 error = current_has_perm(p, PROCESS__SETEXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908 else if (!strcmp(name, "fscreate"))
David Howells3b11a1d2008-11-14 10:39:26 +11005909 error = current_has_perm(p, PROCESS__SETFSCREATE);
Michael LeMay4eb582c2006-06-26 00:24:57 -07005910 else if (!strcmp(name, "keycreate"))
David Howells3b11a1d2008-11-14 10:39:26 +11005911 error = current_has_perm(p, PROCESS__SETKEYCREATE);
Eric Paris42c3e032006-06-26 00:26:03 -07005912 else if (!strcmp(name, "sockcreate"))
David Howells3b11a1d2008-11-14 10:39:26 +11005913 error = current_has_perm(p, PROCESS__SETSOCKCREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914 else if (!strcmp(name, "current"))
David Howells3b11a1d2008-11-14 10:39:26 +11005915 error = current_has_perm(p, PROCESS__SETCURRENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005916 else
5917 error = -EINVAL;
5918 if (error)
5919 return error;
5920
5921 /* Obtain a SID for the context, if one was specified. */
Stephen Smalley6cbaf7b2017-01-31 11:54:04 -05005922 if (size && str[0] && str[0] != '\n') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923 if (str[size-1] == '\n') {
5924 str[size-1] = 0;
5925 size--;
5926 }
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01005927 error = security_context_to_sid(value, size, &sid, GFP_KERNEL);
Stephen Smalley12b29f32008-05-07 13:03:20 -04005928 if (error == -EINVAL && !strcmp(name, "fscreate")) {
Eric Parisd6ea83e2012-04-04 13:45:49 -04005929 if (!capable(CAP_MAC_ADMIN)) {
5930 struct audit_buffer *ab;
5931 size_t audit_size;
5932
5933 /* We strip a nul only if it is at the end, otherwise the
5934 * context contains a nul and we should audit that */
5935 if (str[size - 1] == '\0')
5936 audit_size = size - 1;
5937 else
5938 audit_size = size;
5939 ab = audit_log_start(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR);
5940 audit_log_format(ab, "op=fscreate invalid_context=");
5941 audit_log_n_untrustedstring(ab, value, audit_size);
5942 audit_log_end(ab);
5943
Stephen Smalley12b29f32008-05-07 13:03:20 -04005944 return error;
Eric Parisd6ea83e2012-04-04 13:45:49 -04005945 }
Stephen Smalley12b29f32008-05-07 13:03:20 -04005946 error = security_context_to_sid_force(value, size,
5947 &sid);
5948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005949 if (error)
5950 return error;
5951 }
5952
David Howellsd84f4f92008-11-14 10:39:23 +11005953 new = prepare_creds();
5954 if (!new)
5955 return -ENOMEM;
5956
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957 /* Permission checking based on the specified context is
5958 performed during the actual operation (execve,
5959 open/mkdir/...), when we know the full context of the
David Howellsd84f4f92008-11-14 10:39:23 +11005960 operation. See selinux_bprm_set_creds for the execve
Linus Torvalds1da177e2005-04-16 15:20:36 -07005961 checks and may_create for the file creation checks. The
5962 operation will then fail if the context is not permitted. */
David Howellsd84f4f92008-11-14 10:39:23 +11005963 tsec = new->security;
5964 if (!strcmp(name, "exec")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005965 tsec->exec_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005966 } else if (!strcmp(name, "fscreate")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005967 tsec->create_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005968 } else if (!strcmp(name, "keycreate")) {
Michael LeMay4eb582c2006-06-26 00:24:57 -07005969 error = may_create_key(sid, p);
5970 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11005971 goto abort_change;
Michael LeMay4eb582c2006-06-26 00:24:57 -07005972 tsec->keycreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005973 } else if (!strcmp(name, "sockcreate")) {
Eric Paris42c3e032006-06-26 00:26:03 -07005974 tsec->sockcreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005975 } else if (!strcmp(name, "current")) {
5976 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977 if (sid == 0)
David Howellsd84f4f92008-11-14 10:39:23 +11005978 goto abort_change;
KaiGai Koheid9250de2008-08-28 16:35:57 +09005979
David Howellsd84f4f92008-11-14 10:39:23 +11005980 /* Only allow single threaded processes to change context */
5981 error = -EPERM;
Oleg Nesterov5bb459b2009-07-10 03:48:23 +02005982 if (!current_is_single_threaded()) {
David Howellsd84f4f92008-11-14 10:39:23 +11005983 error = security_bounded_transition(tsec->sid, sid);
5984 if (error)
5985 goto abort_change;
Eric Paris828dfe12008-04-17 13:17:49 -04005986 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987
5988 /* Check permissions for the transition. */
5989 error = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS,
Eric Paris828dfe12008-04-17 13:17:49 -04005990 PROCESS__DYNTRANSITION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11005992 goto abort_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993
5994 /* Check for ptracing, and update the task SID if ok.
5995 Otherwise, leave SID unchanged and fail. */
Paul Moore0c6181c2016-03-30 21:41:21 -04005996 ptsid = ptrace_parent_sid(p);
5997 if (ptsid != 0) {
David Howellsd84f4f92008-11-14 10:39:23 +11005998 error = avc_has_perm(ptsid, sid, SECCLASS_PROCESS,
5999 PROCESS__PTRACE, NULL);
6000 if (error)
6001 goto abort_change;
6002 }
6003
6004 tsec->sid = sid;
6005 } else {
6006 error = -EINVAL;
6007 goto abort_change;
6008 }
6009
6010 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011 return size;
David Howellsd84f4f92008-11-14 10:39:23 +11006012
6013abort_change:
6014 abort_creds(new);
6015 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006016}
6017
David Quigley746df9b2013-05-22 12:50:35 -04006018static int selinux_ismaclabel(const char *name)
6019{
6020 return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0);
6021}
6022
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006023static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
6024{
6025 return security_sid_to_context(secid, secdata, seclen);
6026}
6027
David Howells7bf570d2008-04-29 20:52:51 +01006028static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
David Howells63cb3442008-01-15 23:47:35 +00006029{
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01006030 return security_context_to_sid(secdata, seclen, secid, GFP_KERNEL);
David Howells63cb3442008-01-15 23:47:35 +00006031}
6032
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006033static void selinux_release_secctx(char *secdata, u32 seclen)
6034{
Paul Moore088999e2007-08-01 11:12:58 -04006035 kfree(secdata);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006036}
6037
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006038static void selinux_inode_invalidate_secctx(struct inode *inode)
6039{
6040 struct inode_security_struct *isec = inode->i_security;
6041
6042 mutex_lock(&isec->lock);
6043 isec->initialized = LABEL_INVALID;
6044 mutex_unlock(&isec->lock);
6045}
6046
David P. Quigley1ee65e32009-09-03 14:25:57 -04006047/*
6048 * called with inode->i_mutex locked
6049 */
6050static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
6051{
Ondrej Mosnacek992baf52018-12-21 21:18:53 +01006052 int rc = selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX,
6053 ctx, ctxlen, 0);
6054 /* Do not return error when suppressing label (SBLABEL_MNT not set). */
6055 return rc == -EOPNOTSUPP ? 0 : rc;
David P. Quigley1ee65e32009-09-03 14:25:57 -04006056}
6057
6058/*
6059 * called with inode->i_mutex locked
6060 */
6061static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
6062{
6063 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0);
6064}
6065
6066static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
6067{
6068 int len = 0;
6069 len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX,
6070 ctx, true);
6071 if (len < 0)
6072 return len;
6073 *ctxlen = len;
6074 return 0;
6075}
Michael LeMayd7200242006-06-22 14:47:17 -07006076#ifdef CONFIG_KEYS
6077
David Howellsd84f4f92008-11-14 10:39:23 +11006078static int selinux_key_alloc(struct key *k, const struct cred *cred,
David Howells7e047ef2006-06-26 00:24:50 -07006079 unsigned long flags)
Michael LeMayd7200242006-06-22 14:47:17 -07006080{
David Howellsd84f4f92008-11-14 10:39:23 +11006081 const struct task_security_struct *tsec;
Michael LeMayd7200242006-06-22 14:47:17 -07006082 struct key_security_struct *ksec;
6083
6084 ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
6085 if (!ksec)
6086 return -ENOMEM;
6087
David Howellsd84f4f92008-11-14 10:39:23 +11006088 tsec = cred->security;
6089 if (tsec->keycreate_sid)
6090 ksec->sid = tsec->keycreate_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006091 else
David Howellsd84f4f92008-11-14 10:39:23 +11006092 ksec->sid = tsec->sid;
Michael LeMayd7200242006-06-22 14:47:17 -07006093
David Howells275bb412008-11-14 10:39:19 +11006094 k->security = ksec;
Michael LeMayd7200242006-06-22 14:47:17 -07006095 return 0;
6096}
6097
6098static void selinux_key_free(struct key *k)
6099{
6100 struct key_security_struct *ksec = k->security;
6101
6102 k->security = NULL;
6103 kfree(ksec);
6104}
6105
6106static int selinux_key_permission(key_ref_t key_ref,
David Howellsd84f4f92008-11-14 10:39:23 +11006107 const struct cred *cred,
David Howellsf5895942014-03-14 17:44:49 +00006108 unsigned perm)
Michael LeMayd7200242006-06-22 14:47:17 -07006109{
6110 struct key *key;
Michael LeMayd7200242006-06-22 14:47:17 -07006111 struct key_security_struct *ksec;
David Howells275bb412008-11-14 10:39:19 +11006112 u32 sid;
Michael LeMayd7200242006-06-22 14:47:17 -07006113
6114 /* if no specific permissions are requested, we skip the
6115 permission check. No serious, additional covert channels
6116 appear to be created. */
6117 if (perm == 0)
6118 return 0;
6119
David Howellsd84f4f92008-11-14 10:39:23 +11006120 sid = cred_sid(cred);
David Howells275bb412008-11-14 10:39:19 +11006121
6122 key = key_ref_to_ptr(key_ref);
6123 ksec = key->security;
6124
6125 return avc_has_perm(sid, ksec->sid, SECCLASS_KEY, perm, NULL);
Michael LeMayd7200242006-06-22 14:47:17 -07006126}
6127
David Howells70a5bb72008-04-29 01:01:26 -07006128static int selinux_key_getsecurity(struct key *key, char **_buffer)
6129{
6130 struct key_security_struct *ksec = key->security;
6131 char *context = NULL;
6132 unsigned len;
6133 int rc;
6134
6135 rc = security_sid_to_context(ksec->sid, &context, &len);
6136 if (!rc)
6137 rc = len;
6138 *_buffer = context;
6139 return rc;
6140}
6141
Michael LeMayd7200242006-06-22 14:47:17 -07006142#endif
6143
Chenbo Feng9b629132017-10-18 13:00:25 -07006144#ifdef CONFIG_BPF_SYSCALL
6145static int selinux_bpf(int cmd, union bpf_attr *attr,
6146 unsigned int size)
6147{
6148 u32 sid = current_sid();
6149 int ret;
6150
6151 switch (cmd) {
6152 case BPF_MAP_CREATE:
6153 ret = avc_has_perm(sid, sid, SECCLASS_BPF, BPF__MAP_CREATE,
6154 NULL);
6155 break;
6156 case BPF_PROG_LOAD:
6157 ret = avc_has_perm(sid, sid, SECCLASS_BPF, BPF__PROG_LOAD,
6158 NULL);
6159 break;
6160 default:
6161 ret = 0;
6162 break;
6163 }
6164
6165 return ret;
6166}
6167
6168static u32 bpf_map_fmode_to_av(fmode_t fmode)
6169{
6170 u32 av = 0;
6171
6172 if (fmode & FMODE_READ)
6173 av |= BPF__MAP_READ;
6174 if (fmode & FMODE_WRITE)
6175 av |= BPF__MAP_WRITE;
6176 return av;
6177}
6178
Chenbo Feng0521e0b2017-10-18 13:00:26 -07006179/* This function will check the file pass through unix socket or binder to see
6180 * if it is a bpf related object. And apply correspinding checks on the bpf
6181 * object based on the type. The bpf maps and programs, not like other files and
6182 * socket, are using a shared anonymous inode inside the kernel as their inode.
6183 * So checking that inode cannot identify if the process have privilege to
6184 * access the bpf object and that's why we have to add this additional check in
6185 * selinux_file_receive and selinux_binder_transfer_files.
6186 */
6187static int bpf_fd_pass(struct file *file, u32 sid)
6188{
6189 struct bpf_security_struct *bpfsec;
6190 struct bpf_prog *prog;
6191 struct bpf_map *map;
6192 int ret;
6193
6194 if (file->f_op == &bpf_map_fops) {
6195 map = file->private_data;
6196 bpfsec = map->security;
6197 ret = avc_has_perm(sid, bpfsec->sid, SECCLASS_BPF,
6198 bpf_map_fmode_to_av(file->f_mode), NULL);
6199 if (ret)
6200 return ret;
6201 } else if (file->f_op == &bpf_prog_fops) {
6202 prog = file->private_data;
6203 bpfsec = prog->aux->security;
6204 ret = avc_has_perm(sid, bpfsec->sid, SECCLASS_BPF,
6205 BPF__PROG_RUN, NULL);
6206 if (ret)
6207 return ret;
6208 }
6209 return 0;
6210}
6211
Chenbo Feng9b629132017-10-18 13:00:25 -07006212static int selinux_bpf_map(struct bpf_map *map, fmode_t fmode)
6213{
6214 u32 sid = current_sid();
6215 struct bpf_security_struct *bpfsec;
6216
6217 bpfsec = map->security;
6218 return avc_has_perm(sid, bpfsec->sid, SECCLASS_BPF,
6219 bpf_map_fmode_to_av(fmode), NULL);
6220}
6221
6222static int selinux_bpf_prog(struct bpf_prog *prog)
6223{
6224 u32 sid = current_sid();
6225 struct bpf_security_struct *bpfsec;
6226
6227 bpfsec = prog->aux->security;
6228 return avc_has_perm(sid, bpfsec->sid, SECCLASS_BPF,
6229 BPF__PROG_RUN, NULL);
6230}
6231
6232static int selinux_bpf_map_alloc(struct bpf_map *map)
6233{
6234 struct bpf_security_struct *bpfsec;
6235
6236 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6237 if (!bpfsec)
6238 return -ENOMEM;
6239
6240 bpfsec->sid = current_sid();
6241 map->security = bpfsec;
6242
6243 return 0;
6244}
6245
6246static void selinux_bpf_map_free(struct bpf_map *map)
6247{
6248 struct bpf_security_struct *bpfsec = map->security;
6249
6250 map->security = NULL;
6251 kfree(bpfsec);
6252}
6253
6254static int selinux_bpf_prog_alloc(struct bpf_prog_aux *aux)
6255{
6256 struct bpf_security_struct *bpfsec;
6257
6258 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6259 if (!bpfsec)
6260 return -ENOMEM;
6261
6262 bpfsec->sid = current_sid();
6263 aux->security = bpfsec;
6264
6265 return 0;
6266}
6267
6268static void selinux_bpf_prog_free(struct bpf_prog_aux *aux)
6269{
6270 struct bpf_security_struct *bpfsec = aux->security;
6271
6272 aux->security = NULL;
6273 kfree(bpfsec);
6274}
6275#endif
6276
James Morriscaefc012017-02-15 00:18:51 +11006277static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {
Casey Schauflere20b0432015-05-02 15:11:36 -07006278 LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr),
6279 LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction),
6280 LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder),
6281 LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file),
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02006282
Casey Schauflere20b0432015-05-02 15:11:36 -07006283 LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check),
6284 LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme),
6285 LSM_HOOK_INIT(capget, selinux_capget),
6286 LSM_HOOK_INIT(capset, selinux_capset),
6287 LSM_HOOK_INIT(capable, selinux_capable),
6288 LSM_HOOK_INIT(quotactl, selinux_quotactl),
6289 LSM_HOOK_INIT(quota_on, selinux_quota_on),
6290 LSM_HOOK_INIT(syslog, selinux_syslog),
6291 LSM_HOOK_INIT(vm_enough_memory, selinux_vm_enough_memory),
Stephen Smalley79af7302015-01-21 10:54:10 -05006292
Casey Schauflere20b0432015-05-02 15:11:36 -07006293 LSM_HOOK_INIT(netlink_send, selinux_netlink_send),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006294
Casey Schauflere20b0432015-05-02 15:11:36 -07006295 LSM_HOOK_INIT(bprm_set_creds, selinux_bprm_set_creds),
6296 LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds),
6297 LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds),
6298 LSM_HOOK_INIT(bprm_secureexec, selinux_bprm_secureexec),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006299
Casey Schauflere20b0432015-05-02 15:11:36 -07006300 LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security),
6301 LSM_HOOK_INIT(sb_free_security, selinux_sb_free_security),
6302 LSM_HOOK_INIT(sb_copy_data, selinux_sb_copy_data),
6303 LSM_HOOK_INIT(sb_remount, selinux_sb_remount),
6304 LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount),
6305 LSM_HOOK_INIT(sb_show_options, selinux_sb_show_options),
6306 LSM_HOOK_INIT(sb_statfs, selinux_sb_statfs),
6307 LSM_HOOK_INIT(sb_mount, selinux_mount),
6308 LSM_HOOK_INIT(sb_umount, selinux_umount),
6309 LSM_HOOK_INIT(sb_set_mnt_opts, selinux_set_mnt_opts),
6310 LSM_HOOK_INIT(sb_clone_mnt_opts, selinux_sb_clone_mnt_opts),
6311 LSM_HOOK_INIT(sb_parse_opts_str, selinux_parse_opts_str),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006312
Casey Schauflere20b0432015-05-02 15:11:36 -07006313 LSM_HOOK_INIT(dentry_init_security, selinux_dentry_init_security),
Vivek Goyala518b0a2016-07-13 10:44:53 -04006314 LSM_HOOK_INIT(dentry_create_files_as, selinux_dentry_create_files_as),
Eric Parise0007522008-03-05 10:31:54 -05006315
Casey Schauflere20b0432015-05-02 15:11:36 -07006316 LSM_HOOK_INIT(inode_alloc_security, selinux_inode_alloc_security),
6317 LSM_HOOK_INIT(inode_free_security, selinux_inode_free_security),
6318 LSM_HOOK_INIT(inode_init_security, selinux_inode_init_security),
6319 LSM_HOOK_INIT(inode_create, selinux_inode_create),
6320 LSM_HOOK_INIT(inode_link, selinux_inode_link),
6321 LSM_HOOK_INIT(inode_unlink, selinux_inode_unlink),
6322 LSM_HOOK_INIT(inode_symlink, selinux_inode_symlink),
6323 LSM_HOOK_INIT(inode_mkdir, selinux_inode_mkdir),
6324 LSM_HOOK_INIT(inode_rmdir, selinux_inode_rmdir),
6325 LSM_HOOK_INIT(inode_mknod, selinux_inode_mknod),
6326 LSM_HOOK_INIT(inode_rename, selinux_inode_rename),
6327 LSM_HOOK_INIT(inode_readlink, selinux_inode_readlink),
6328 LSM_HOOK_INIT(inode_follow_link, selinux_inode_follow_link),
6329 LSM_HOOK_INIT(inode_permission, selinux_inode_permission),
6330 LSM_HOOK_INIT(inode_setattr, selinux_inode_setattr),
6331 LSM_HOOK_INIT(inode_getattr, selinux_inode_getattr),
6332 LSM_HOOK_INIT(inode_setxattr, selinux_inode_setxattr),
6333 LSM_HOOK_INIT(inode_post_setxattr, selinux_inode_post_setxattr),
6334 LSM_HOOK_INIT(inode_getxattr, selinux_inode_getxattr),
6335 LSM_HOOK_INIT(inode_listxattr, selinux_inode_listxattr),
6336 LSM_HOOK_INIT(inode_removexattr, selinux_inode_removexattr),
6337 LSM_HOOK_INIT(inode_getsecurity, selinux_inode_getsecurity),
6338 LSM_HOOK_INIT(inode_setsecurity, selinux_inode_setsecurity),
6339 LSM_HOOK_INIT(inode_listsecurity, selinux_inode_listsecurity),
6340 LSM_HOOK_INIT(inode_getsecid, selinux_inode_getsecid),
Vivek Goyal56909eb2016-07-13 10:44:48 -04006341 LSM_HOOK_INIT(inode_copy_up, selinux_inode_copy_up),
Vivek Goyal19472b62016-07-13 10:44:50 -04006342 LSM_HOOK_INIT(inode_copy_up_xattr, selinux_inode_copy_up_xattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006343
Casey Schauflere20b0432015-05-02 15:11:36 -07006344 LSM_HOOK_INIT(file_permission, selinux_file_permission),
6345 LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security),
6346 LSM_HOOK_INIT(file_free_security, selinux_file_free_security),
6347 LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl),
6348 LSM_HOOK_INIT(mmap_file, selinux_mmap_file),
6349 LSM_HOOK_INIT(mmap_addr, selinux_mmap_addr),
6350 LSM_HOOK_INIT(file_mprotect, selinux_file_mprotect),
6351 LSM_HOOK_INIT(file_lock, selinux_file_lock),
6352 LSM_HOOK_INIT(file_fcntl, selinux_file_fcntl),
6353 LSM_HOOK_INIT(file_set_fowner, selinux_file_set_fowner),
6354 LSM_HOOK_INIT(file_send_sigiotask, selinux_file_send_sigiotask),
6355 LSM_HOOK_INIT(file_receive, selinux_file_receive),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006356
Casey Schauflere20b0432015-05-02 15:11:36 -07006357 LSM_HOOK_INIT(file_open, selinux_file_open),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006358
Casey Schauflere20b0432015-05-02 15:11:36 -07006359 LSM_HOOK_INIT(task_create, selinux_task_create),
6360 LSM_HOOK_INIT(cred_alloc_blank, selinux_cred_alloc_blank),
6361 LSM_HOOK_INIT(cred_free, selinux_cred_free),
6362 LSM_HOOK_INIT(cred_prepare, selinux_cred_prepare),
6363 LSM_HOOK_INIT(cred_transfer, selinux_cred_transfer),
6364 LSM_HOOK_INIT(kernel_act_as, selinux_kernel_act_as),
6365 LSM_HOOK_INIT(kernel_create_files_as, selinux_kernel_create_files_as),
6366 LSM_HOOK_INIT(kernel_module_request, selinux_kernel_module_request),
Jeff Vander Stoep61d612e2016-04-05 13:06:27 -07006367 LSM_HOOK_INIT(kernel_read_file, selinux_kernel_read_file),
Casey Schauflere20b0432015-05-02 15:11:36 -07006368 LSM_HOOK_INIT(task_setpgid, selinux_task_setpgid),
6369 LSM_HOOK_INIT(task_getpgid, selinux_task_getpgid),
6370 LSM_HOOK_INIT(task_getsid, selinux_task_getsid),
6371 LSM_HOOK_INIT(task_getsecid, selinux_task_getsecid),
6372 LSM_HOOK_INIT(task_setnice, selinux_task_setnice),
6373 LSM_HOOK_INIT(task_setioprio, selinux_task_setioprio),
6374 LSM_HOOK_INIT(task_getioprio, selinux_task_getioprio),
6375 LSM_HOOK_INIT(task_setrlimit, selinux_task_setrlimit),
6376 LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler),
6377 LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler),
6378 LSM_HOOK_INIT(task_movememory, selinux_task_movememory),
6379 LSM_HOOK_INIT(task_kill, selinux_task_kill),
6380 LSM_HOOK_INIT(task_wait, selinux_task_wait),
6381 LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode),
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09006382
Casey Schauflere20b0432015-05-02 15:11:36 -07006383 LSM_HOOK_INIT(ipc_permission, selinux_ipc_permission),
6384 LSM_HOOK_INIT(ipc_getsecid, selinux_ipc_getsecid),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006385
Casey Schauflere20b0432015-05-02 15:11:36 -07006386 LSM_HOOK_INIT(msg_msg_alloc_security, selinux_msg_msg_alloc_security),
6387 LSM_HOOK_INIT(msg_msg_free_security, selinux_msg_msg_free_security),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006388
Casey Schauflere20b0432015-05-02 15:11:36 -07006389 LSM_HOOK_INIT(msg_queue_alloc_security,
6390 selinux_msg_queue_alloc_security),
6391 LSM_HOOK_INIT(msg_queue_free_security, selinux_msg_queue_free_security),
6392 LSM_HOOK_INIT(msg_queue_associate, selinux_msg_queue_associate),
6393 LSM_HOOK_INIT(msg_queue_msgctl, selinux_msg_queue_msgctl),
6394 LSM_HOOK_INIT(msg_queue_msgsnd, selinux_msg_queue_msgsnd),
6395 LSM_HOOK_INIT(msg_queue_msgrcv, selinux_msg_queue_msgrcv),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006396
Casey Schauflere20b0432015-05-02 15:11:36 -07006397 LSM_HOOK_INIT(shm_alloc_security, selinux_shm_alloc_security),
6398 LSM_HOOK_INIT(shm_free_security, selinux_shm_free_security),
6399 LSM_HOOK_INIT(shm_associate, selinux_shm_associate),
6400 LSM_HOOK_INIT(shm_shmctl, selinux_shm_shmctl),
6401 LSM_HOOK_INIT(shm_shmat, selinux_shm_shmat),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006402
Casey Schauflere20b0432015-05-02 15:11:36 -07006403 LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security),
6404 LSM_HOOK_INIT(sem_free_security, selinux_sem_free_security),
6405 LSM_HOOK_INIT(sem_associate, selinux_sem_associate),
6406 LSM_HOOK_INIT(sem_semctl, selinux_sem_semctl),
6407 LSM_HOOK_INIT(sem_semop, selinux_sem_semop),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006408
Casey Schauflere20b0432015-05-02 15:11:36 -07006409 LSM_HOOK_INIT(d_instantiate, selinux_d_instantiate),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006410
Casey Schauflere20b0432015-05-02 15:11:36 -07006411 LSM_HOOK_INIT(getprocattr, selinux_getprocattr),
6412 LSM_HOOK_INIT(setprocattr, selinux_setprocattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006413
Casey Schauflere20b0432015-05-02 15:11:36 -07006414 LSM_HOOK_INIT(ismaclabel, selinux_ismaclabel),
6415 LSM_HOOK_INIT(secid_to_secctx, selinux_secid_to_secctx),
6416 LSM_HOOK_INIT(secctx_to_secid, selinux_secctx_to_secid),
6417 LSM_HOOK_INIT(release_secctx, selinux_release_secctx),
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006418 LSM_HOOK_INIT(inode_invalidate_secctx, selinux_inode_invalidate_secctx),
Casey Schauflere20b0432015-05-02 15:11:36 -07006419 LSM_HOOK_INIT(inode_notifysecctx, selinux_inode_notifysecctx),
6420 LSM_HOOK_INIT(inode_setsecctx, selinux_inode_setsecctx),
6421 LSM_HOOK_INIT(inode_getsecctx, selinux_inode_getsecctx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006422
Casey Schauflere20b0432015-05-02 15:11:36 -07006423 LSM_HOOK_INIT(unix_stream_connect, selinux_socket_unix_stream_connect),
6424 LSM_HOOK_INIT(unix_may_send, selinux_socket_unix_may_send),
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006425
Casey Schauflere20b0432015-05-02 15:11:36 -07006426 LSM_HOOK_INIT(socket_create, selinux_socket_create),
6427 LSM_HOOK_INIT(socket_post_create, selinux_socket_post_create),
6428 LSM_HOOK_INIT(socket_bind, selinux_socket_bind),
6429 LSM_HOOK_INIT(socket_connect, selinux_socket_connect),
6430 LSM_HOOK_INIT(socket_listen, selinux_socket_listen),
6431 LSM_HOOK_INIT(socket_accept, selinux_socket_accept),
6432 LSM_HOOK_INIT(socket_sendmsg, selinux_socket_sendmsg),
6433 LSM_HOOK_INIT(socket_recvmsg, selinux_socket_recvmsg),
6434 LSM_HOOK_INIT(socket_getsockname, selinux_socket_getsockname),
6435 LSM_HOOK_INIT(socket_getpeername, selinux_socket_getpeername),
6436 LSM_HOOK_INIT(socket_getsockopt, selinux_socket_getsockopt),
6437 LSM_HOOK_INIT(socket_setsockopt, selinux_socket_setsockopt),
6438 LSM_HOOK_INIT(socket_shutdown, selinux_socket_shutdown),
6439 LSM_HOOK_INIT(socket_sock_rcv_skb, selinux_socket_sock_rcv_skb),
6440 LSM_HOOK_INIT(socket_getpeersec_stream,
6441 selinux_socket_getpeersec_stream),
6442 LSM_HOOK_INIT(socket_getpeersec_dgram, selinux_socket_getpeersec_dgram),
6443 LSM_HOOK_INIT(sk_alloc_security, selinux_sk_alloc_security),
6444 LSM_HOOK_INIT(sk_free_security, selinux_sk_free_security),
6445 LSM_HOOK_INIT(sk_clone_security, selinux_sk_clone_security),
6446 LSM_HOOK_INIT(sk_getsecid, selinux_sk_getsecid),
6447 LSM_HOOK_INIT(sock_graft, selinux_sock_graft),
6448 LSM_HOOK_INIT(inet_conn_request, selinux_inet_conn_request),
6449 LSM_HOOK_INIT(inet_csk_clone, selinux_inet_csk_clone),
6450 LSM_HOOK_INIT(inet_conn_established, selinux_inet_conn_established),
6451 LSM_HOOK_INIT(secmark_relabel_packet, selinux_secmark_relabel_packet),
6452 LSM_HOOK_INIT(secmark_refcount_inc, selinux_secmark_refcount_inc),
6453 LSM_HOOK_INIT(secmark_refcount_dec, selinux_secmark_refcount_dec),
6454 LSM_HOOK_INIT(req_classify_flow, selinux_req_classify_flow),
6455 LSM_HOOK_INIT(tun_dev_alloc_security, selinux_tun_dev_alloc_security),
6456 LSM_HOOK_INIT(tun_dev_free_security, selinux_tun_dev_free_security),
6457 LSM_HOOK_INIT(tun_dev_create, selinux_tun_dev_create),
6458 LSM_HOOK_INIT(tun_dev_attach_queue, selinux_tun_dev_attach_queue),
6459 LSM_HOOK_INIT(tun_dev_attach, selinux_tun_dev_attach),
6460 LSM_HOOK_INIT(tun_dev_open, selinux_tun_dev_open),
Trent Jaegerd28d1e02005-12-13 23:12:40 -08006461
6462#ifdef CONFIG_SECURITY_NETWORK_XFRM
Casey Schauflere20b0432015-05-02 15:11:36 -07006463 LSM_HOOK_INIT(xfrm_policy_alloc_security, selinux_xfrm_policy_alloc),
6464 LSM_HOOK_INIT(xfrm_policy_clone_security, selinux_xfrm_policy_clone),
6465 LSM_HOOK_INIT(xfrm_policy_free_security, selinux_xfrm_policy_free),
6466 LSM_HOOK_INIT(xfrm_policy_delete_security, selinux_xfrm_policy_delete),
6467 LSM_HOOK_INIT(xfrm_state_alloc, selinux_xfrm_state_alloc),
6468 LSM_HOOK_INIT(xfrm_state_alloc_acquire,
6469 selinux_xfrm_state_alloc_acquire),
6470 LSM_HOOK_INIT(xfrm_state_free_security, selinux_xfrm_state_free),
6471 LSM_HOOK_INIT(xfrm_state_delete_security, selinux_xfrm_state_delete),
6472 LSM_HOOK_INIT(xfrm_policy_lookup, selinux_xfrm_policy_lookup),
6473 LSM_HOOK_INIT(xfrm_state_pol_flow_match,
6474 selinux_xfrm_state_pol_flow_match),
6475 LSM_HOOK_INIT(xfrm_decode_session, selinux_xfrm_decode_session),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006476#endif
Michael LeMayd7200242006-06-22 14:47:17 -07006477
6478#ifdef CONFIG_KEYS
Casey Schauflere20b0432015-05-02 15:11:36 -07006479 LSM_HOOK_INIT(key_alloc, selinux_key_alloc),
6480 LSM_HOOK_INIT(key_free, selinux_key_free),
6481 LSM_HOOK_INIT(key_permission, selinux_key_permission),
6482 LSM_HOOK_INIT(key_getsecurity, selinux_key_getsecurity),
Michael LeMayd7200242006-06-22 14:47:17 -07006483#endif
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02006484
6485#ifdef CONFIG_AUDIT
Casey Schauflere20b0432015-05-02 15:11:36 -07006486 LSM_HOOK_INIT(audit_rule_init, selinux_audit_rule_init),
6487 LSM_HOOK_INIT(audit_rule_known, selinux_audit_rule_known),
6488 LSM_HOOK_INIT(audit_rule_match, selinux_audit_rule_match),
6489 LSM_HOOK_INIT(audit_rule_free, selinux_audit_rule_free),
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02006490#endif
Chenbo Feng9b629132017-10-18 13:00:25 -07006491
6492#ifdef CONFIG_BPF_SYSCALL
6493 LSM_HOOK_INIT(bpf, selinux_bpf),
6494 LSM_HOOK_INIT(bpf_map, selinux_bpf_map),
6495 LSM_HOOK_INIT(bpf_prog, selinux_bpf_prog),
6496 LSM_HOOK_INIT(bpf_map_alloc_security, selinux_bpf_map_alloc),
6497 LSM_HOOK_INIT(bpf_prog_alloc_security, selinux_bpf_prog_alloc),
6498 LSM_HOOK_INIT(bpf_map_free_security, selinux_bpf_map_free),
6499 LSM_HOOK_INIT(bpf_prog_free_security, selinux_bpf_prog_free),
6500#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006501};
6502
6503static __init int selinux_init(void)
6504{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07006505 if (!security_module_enable("selinux")) {
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02006506 selinux_enabled = 0;
6507 return 0;
6508 }
6509
Linus Torvalds1da177e2005-04-16 15:20:36 -07006510 if (!selinux_enabled) {
6511 printk(KERN_INFO "SELinux: Disabled at boot.\n");
6512 return 0;
6513 }
6514
6515 printk(KERN_INFO "SELinux: Initializing.\n");
6516
6517 /* Set the security state for the initial task. */
David Howellsd84f4f92008-11-14 10:39:23 +11006518 cred_init_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006519
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04006520 default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
6521
James Morris7cae7e22006-03-22 00:09:22 -08006522 sel_inode_cache = kmem_cache_create("selinux_inode_security",
6523 sizeof(struct inode_security_struct),
Paul Mundt20c2df82007-07-20 10:11:58 +09006524 0, SLAB_PANIC, NULL);
Sangwoo63205652015-10-21 17:44:30 -04006525 file_security_cache = kmem_cache_create("selinux_file_security",
6526 sizeof(struct file_security_struct),
6527 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006528 avc_init();
6529
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07006530 security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006531
Paul Moore615e51f2014-06-26 14:33:56 -04006532 if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET))
6533 panic("SELinux: Unable to register AVC netcache callback\n");
6534
Eric Paris828dfe12008-04-17 13:17:49 -04006535 if (selinux_enforcing)
Eric Parisfadcdb42007-02-22 18:11:31 -05006536 printk(KERN_DEBUG "SELinux: Starting in enforcing mode\n");
Eric Paris828dfe12008-04-17 13:17:49 -04006537 else
Eric Parisfadcdb42007-02-22 18:11:31 -05006538 printk(KERN_DEBUG "SELinux: Starting in permissive mode\n");
Michael LeMayd7200242006-06-22 14:47:17 -07006539
Linus Torvalds1da177e2005-04-16 15:20:36 -07006540 return 0;
6541}
6542
Al Viroe8c26252010-03-23 06:36:54 -04006543static void delayed_superblock_init(struct super_block *sb, void *unused)
6544{
6545 superblock_doinit(sb, NULL);
6546}
6547
Linus Torvalds1da177e2005-04-16 15:20:36 -07006548void selinux_complete_init(void)
6549{
Eric Parisfadcdb42007-02-22 18:11:31 -05006550 printk(KERN_DEBUG "SELinux: Completing initialization.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006551
6552 /* Set up any superblocks initialized prior to the policy load. */
Eric Parisfadcdb42007-02-22 18:11:31 -05006553 printk(KERN_DEBUG "SELinux: Setting up existing superblocks.\n");
Al Viroe8c26252010-03-23 06:36:54 -04006554 iterate_supers(delayed_superblock_init, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006555}
6556
6557/* SELinux requires early initialization in order to label
6558 all processes and objects when they are created. */
6559security_initcall(selinux_init);
6560
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08006561#if defined(CONFIG_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006562
Jiri Pirko25db6be2014-09-03 17:42:13 +02006563static struct nf_hook_ops selinux_nf_ops[] = {
Paul Mooreeffad8d2008-01-29 08:49:27 -05006564 {
6565 .hook = selinux_ipv4_postroute,
Alban Crequy2597a832012-05-14 03:56:39 +00006566 .pf = NFPROTO_IPV4,
Paul Mooreeffad8d2008-01-29 08:49:27 -05006567 .hooknum = NF_INET_POST_ROUTING,
6568 .priority = NF_IP_PRI_SELINUX_LAST,
6569 },
6570 {
6571 .hook = selinux_ipv4_forward,
Alban Crequy2597a832012-05-14 03:56:39 +00006572 .pf = NFPROTO_IPV4,
Paul Mooreeffad8d2008-01-29 08:49:27 -05006573 .hooknum = NF_INET_FORWARD,
6574 .priority = NF_IP_PRI_SELINUX_FIRST,
Paul Moore948bf852008-10-10 10:16:32 -04006575 },
6576 {
6577 .hook = selinux_ipv4_output,
Alban Crequy2597a832012-05-14 03:56:39 +00006578 .pf = NFPROTO_IPV4,
Paul Moore948bf852008-10-10 10:16:32 -04006579 .hooknum = NF_INET_LOCAL_OUT,
6580 .priority = NF_IP_PRI_SELINUX_FIRST,
Jiri Pirko25db6be2014-09-03 17:42:13 +02006581 },
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04006582#if IS_ENABLED(CONFIG_IPV6)
Paul Mooreeffad8d2008-01-29 08:49:27 -05006583 {
6584 .hook = selinux_ipv6_postroute,
Alban Crequy2597a832012-05-14 03:56:39 +00006585 .pf = NFPROTO_IPV6,
Paul Mooreeffad8d2008-01-29 08:49:27 -05006586 .hooknum = NF_INET_POST_ROUTING,
6587 .priority = NF_IP6_PRI_SELINUX_LAST,
6588 },
6589 {
6590 .hook = selinux_ipv6_forward,
Alban Crequy2597a832012-05-14 03:56:39 +00006591 .pf = NFPROTO_IPV6,
Paul Mooreeffad8d2008-01-29 08:49:27 -05006592 .hooknum = NF_INET_FORWARD,
6593 .priority = NF_IP6_PRI_SELINUX_FIRST,
Jiri Pirko25db6be2014-09-03 17:42:13 +02006594 },
Huw Davies2917f572016-06-27 15:06:15 -04006595 {
6596 .hook = selinux_ipv6_output,
6597 .pf = NFPROTO_IPV6,
6598 .hooknum = NF_INET_LOCAL_OUT,
6599 .priority = NF_IP6_PRI_SELINUX_FIRST,
6600 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07006601#endif /* IPV6 */
Jiri Pirko25db6be2014-09-03 17:42:13 +02006602};
Linus Torvalds1da177e2005-04-16 15:20:36 -07006603
6604static int __init selinux_nf_ip_init(void)
6605{
Jiri Pirko25db6be2014-09-03 17:42:13 +02006606 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006607
6608 if (!selinux_enabled)
Jiri Pirko25db6be2014-09-03 17:42:13 +02006609 return 0;
Eric Parisfadcdb42007-02-22 18:11:31 -05006610
6611 printk(KERN_DEBUG "SELinux: Registering netfilter hooks\n");
6612
Jiri Pirko25db6be2014-09-03 17:42:13 +02006613 err = nf_register_hooks(selinux_nf_ops, ARRAY_SIZE(selinux_nf_ops));
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07006614 if (err)
Jiri Pirko25db6be2014-09-03 17:42:13 +02006615 panic("SELinux: nf_register_hooks: error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006616
Jiri Pirko25db6be2014-09-03 17:42:13 +02006617 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006618}
6619
6620__initcall(selinux_nf_ip_init);
6621
6622#ifdef CONFIG_SECURITY_SELINUX_DISABLE
6623static void selinux_nf_ip_exit(void)
6624{
Eric Parisfadcdb42007-02-22 18:11:31 -05006625 printk(KERN_DEBUG "SELinux: Unregistering netfilter hooks\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006626
Jiri Pirko25db6be2014-09-03 17:42:13 +02006627 nf_unregister_hooks(selinux_nf_ops, ARRAY_SIZE(selinux_nf_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006628}
6629#endif
6630
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08006631#else /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006632
6633#ifdef CONFIG_SECURITY_SELINUX_DISABLE
6634#define selinux_nf_ip_exit()
6635#endif
6636
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08006637#endif /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006638
6639#ifdef CONFIG_SECURITY_SELINUX_DISABLE
Eric Paris828dfe12008-04-17 13:17:49 -04006640static int selinux_disabled;
6641
Linus Torvalds1da177e2005-04-16 15:20:36 -07006642int selinux_disable(void)
6643{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006644 if (ss_initialized) {
6645 /* Not permitted after initial policy load. */
6646 return -EINVAL;
6647 }
6648
6649 if (selinux_disabled) {
6650 /* Only do this once. */
6651 return -EINVAL;
6652 }
6653
6654 printk(KERN_INFO "SELinux: Disabled at runtime.\n");
6655
6656 selinux_disabled = 1;
Stephen Smalley30d55282006-05-03 10:52:36 -04006657 selinux_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006658
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07006659 security_delete_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006660
Eric Parisaf8ff042009-09-20 21:23:01 -04006661 /* Try to destroy the avc node cache */
6662 avc_disable();
6663
Linus Torvalds1da177e2005-04-16 15:20:36 -07006664 /* Unregister netfilter hooks. */
6665 selinux_nf_ip_exit();
6666
6667 /* Unregister selinuxfs. */
6668 exit_sel_fs();
6669
6670 return 0;
6671}
6672#endif