blob: 90423e00a3572f787c16d73c5eaae8a7cc46811d [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
Todd Kjos25b5e562021-10-12 09:56:13 -07002081static int selinux_binder_set_context_mgr(const struct cred *mgr)
Stephen Smalley79af7302015-01-21 10:54:10 -05002082{
Todd Kjos25b5e562021-10-12 09:56:13 -07002083 return avc_has_perm(current_sid(), cred_sid(mgr), SECCLASS_BINDER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002084 BINDER__SET_CONTEXT_MGR, NULL);
2085}
2086
Todd Kjos25b5e562021-10-12 09:56:13 -07002087static int selinux_binder_transaction(const struct cred *from,
2088 const struct cred *to)
Stephen Smalley79af7302015-01-21 10:54:10 -05002089{
2090 u32 mysid = current_sid();
Todd Kjos25b5e562021-10-12 09:56:13 -07002091 u32 fromsid = cred_sid(from);
2092 u32 tosid = cred_sid(to);
Stephen Smalley79af7302015-01-21 10:54:10 -05002093 int rc;
2094
2095 if (mysid != fromsid) {
2096 rc = avc_has_perm(mysid, fromsid, SECCLASS_BINDER,
2097 BINDER__IMPERSONATE, NULL);
2098 if (rc)
2099 return rc;
2100 }
2101
2102 return avc_has_perm(fromsid, tosid, SECCLASS_BINDER, BINDER__CALL,
2103 NULL);
2104}
2105
Todd Kjos25b5e562021-10-12 09:56:13 -07002106static int selinux_binder_transfer_binder(const struct cred *from,
2107 const struct cred *to)
Stephen Smalley79af7302015-01-21 10:54:10 -05002108{
Todd Kjos25b5e562021-10-12 09:56:13 -07002109 return avc_has_perm(cred_sid(from), cred_sid(to),
2110 SECCLASS_BINDER, BINDER__TRANSFER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002111 NULL);
2112}
2113
Todd Kjos25b5e562021-10-12 09:56:13 -07002114static int selinux_binder_transfer_file(const struct cred *from,
2115 const struct cred *to,
Stephen Smalley79af7302015-01-21 10:54:10 -05002116 struct file *file)
2117{
Todd Kjos25b5e562021-10-12 09:56:13 -07002118 u32 sid = cred_sid(to);
Stephen Smalley79af7302015-01-21 10:54:10 -05002119 struct file_security_struct *fsec = file->f_security;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002120 struct dentry *dentry = file->f_path.dentry;
Paul Moore20cdef82016-04-04 14:14:42 -04002121 struct inode_security_struct *isec;
Stephen Smalley79af7302015-01-21 10:54:10 -05002122 struct common_audit_data ad;
2123 int rc;
2124
2125 ad.type = LSM_AUDIT_DATA_PATH;
2126 ad.u.path = file->f_path;
2127
2128 if (sid != fsec->sid) {
2129 rc = avc_has_perm(sid, fsec->sid,
2130 SECCLASS_FD,
2131 FD__USE,
2132 &ad);
2133 if (rc)
2134 return rc;
2135 }
2136
Chenbo Feng0521e0b2017-10-18 13:00:26 -07002137#ifdef CONFIG_BPF_SYSCALL
2138 rc = bpf_fd_pass(file, sid);
2139 if (rc)
2140 return rc;
2141#endif
2142
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002143 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
Stephen Smalley79af7302015-01-21 10:54:10 -05002144 return 0;
2145
Paul Moore20cdef82016-04-04 14:14:42 -04002146 isec = backing_inode_security(dentry);
Stephen Smalley79af7302015-01-21 10:54:10 -05002147 return avc_has_perm(sid, isec->sid, isec->sclass, file_to_av(file),
2148 &ad);
2149}
2150
Ingo Molnar9e488582009-05-07 19:26:19 +10002151static int selinux_ptrace_access_check(struct task_struct *child,
David Howells5cd9c582008-08-14 11:37:28 +01002152 unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153{
Eric Paris69f594a2012-01-03 12:25:15 -05002154 if (mode & PTRACE_MODE_READ) {
David Howells275bb412008-11-14 10:39:19 +11002155 u32 sid = current_sid();
2156 u32 csid = task_sid(child);
2157 return avc_has_perm(sid, csid, SECCLASS_FILE, FILE__READ, NULL);
Stephen Smalley006ebb42008-05-19 08:32:49 -04002158 }
2159
David Howells3b11a1d2008-11-14 10:39:26 +11002160 return current_has_perm(child, PROCESS__PTRACE);
David Howells5cd9c582008-08-14 11:37:28 +01002161}
2162
2163static int selinux_ptrace_traceme(struct task_struct *parent)
2164{
David Howells5cd9c582008-08-14 11:37:28 +01002165 return task_has_perm(parent, current, PROCESS__PTRACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166}
2167
2168static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
Eric Paris828dfe12008-04-17 13:17:49 -04002169 kernel_cap_t *inheritable, kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002171 return current_has_perm(target, PROCESS__GETCAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172}
2173
David Howellsd84f4f92008-11-14 10:39:23 +11002174static int selinux_capset(struct cred *new, const struct cred *old,
2175 const kernel_cap_t *effective,
2176 const kernel_cap_t *inheritable,
2177 const kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178{
David Howellsd84f4f92008-11-14 10:39:23 +11002179 return cred_has_perm(old, new, PROCESS__SETCAP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180}
2181
James Morris5626d3e2009-01-30 10:05:06 +11002182/*
2183 * (This comment used to live with the selinux_task_setuid hook,
2184 * which was removed).
2185 *
2186 * Since setuid only affects the current process, and since the SELinux
2187 * controls are not based on the Linux identity attributes, SELinux does not
2188 * need to control this operation. However, SELinux does control the use of
2189 * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
2190 */
2191
Eric Paris6a9de492012-01-03 12:25:14 -05002192static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
2193 int cap, int audit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194{
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04002195 return cred_has_capability(cred, cap, audit, ns == &init_user_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196}
2197
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
2199{
David Howells88e67f32008-11-14 10:39:21 +11002200 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 int rc = 0;
2202
2203 if (!sb)
2204 return 0;
2205
2206 switch (cmds) {
Eric Paris828dfe12008-04-17 13:17:49 -04002207 case Q_SYNC:
2208 case Q_QUOTAON:
2209 case Q_QUOTAOFF:
2210 case Q_SETINFO:
2211 case Q_SETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11002212 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04002213 break;
2214 case Q_GETFMT:
2215 case Q_GETINFO:
2216 case Q_GETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11002217 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04002218 break;
2219 default:
2220 rc = 0; /* let the kernel handle invalid cmds */
2221 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 }
2223 return rc;
2224}
2225
2226static int selinux_quota_on(struct dentry *dentry)
2227{
David Howells88e67f32008-11-14 10:39:21 +11002228 const struct cred *cred = current_cred();
2229
Eric Paris2875fa02011-04-28 16:04:24 -04002230 return dentry_has_perm(cred, dentry, FILE__QUOTAON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231}
2232
Eric Paris12b30522010-11-15 18:36:29 -05002233static int selinux_syslog(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234{
2235 int rc;
2236
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 switch (type) {
Kees Cookd78ca3c2010-02-03 15:37:13 -08002238 case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */
2239 case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */
Eric Paris828dfe12008-04-17 13:17:49 -04002240 rc = task_has_system(current, SYSTEM__SYSLOG_READ);
2241 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08002242 case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */
2243 case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */
2244 /* Set level of messages printed to console */
2245 case SYSLOG_ACTION_CONSOLE_LEVEL:
Eric Paris828dfe12008-04-17 13:17:49 -04002246 rc = task_has_system(current, SYSTEM__SYSLOG_CONSOLE);
2247 break;
Kees Cookd78ca3c2010-02-03 15:37:13 -08002248 case SYSLOG_ACTION_CLOSE: /* Close log */
2249 case SYSLOG_ACTION_OPEN: /* Open log */
2250 case SYSLOG_ACTION_READ: /* Read from log */
2251 case SYSLOG_ACTION_READ_CLEAR: /* Read/clear last kernel messages */
2252 case SYSLOG_ACTION_CLEAR: /* Clear ring buffer */
Eric Paris828dfe12008-04-17 13:17:49 -04002253 default:
2254 rc = task_has_system(current, SYSTEM__SYSLOG_MOD);
2255 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 }
2257 return rc;
2258}
2259
2260/*
2261 * Check that a process has enough memory to allocate a new virtual
2262 * mapping. 0 means there is enough memory for the allocation to
2263 * succeed and -ENOMEM implies there is not.
2264 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 * Do not audit the selinux permission check, as this is applied to all
2266 * processes that allocate mappings.
2267 */
Alan Cox34b4e4a2007-08-22 14:01:28 -07002268static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269{
2270 int rc, cap_sys_admin = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002272 rc = cred_has_capability(current_cred(), CAP_SYS_ADMIN,
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04002273 SECURITY_CAP_NOAUDIT, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 if (rc == 0)
2275 cap_sys_admin = 1;
2276
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002277 return cap_sys_admin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278}
2279
2280/* binprm security operations */
2281
Paul Moore0c6181c2016-03-30 21:41:21 -04002282static u32 ptrace_parent_sid(struct task_struct *task)
2283{
2284 u32 sid = 0;
2285 struct task_struct *tracer;
2286
2287 rcu_read_lock();
2288 tracer = ptrace_parent(task);
2289 if (tracer)
2290 sid = task_sid(tracer);
2291 rcu_read_unlock();
2292
2293 return sid;
2294}
2295
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002296static int check_nnp_nosuid(const struct linux_binprm *bprm,
2297 const struct task_security_struct *old_tsec,
2298 const struct task_security_struct *new_tsec)
2299{
2300 int nnp = (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS);
Andy Lutomirski380cf5b2016-06-23 16:41:05 -05002301 int nosuid = !mnt_may_suid(bprm->file->f_path.mnt);
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002302 int rc;
2303
2304 if (!nnp && !nosuid)
2305 return 0; /* neither NNP nor nosuid */
2306
2307 if (new_tsec->sid == old_tsec->sid)
2308 return 0; /* No change in credentials */
2309
2310 /*
2311 * The only transitions we permit under NNP or nosuid
2312 * are transitions to bounded SIDs, i.e. SIDs that are
2313 * guaranteed to only be allowed a subset of the permissions
2314 * of the current SID.
2315 */
2316 rc = security_bounded_transition(old_tsec->sid, new_tsec->sid);
2317 if (rc) {
2318 /*
2319 * On failure, preserve the errno values for NNP vs nosuid.
2320 * NNP: Operation not permitted for caller.
2321 * nosuid: Permission denied to file.
2322 */
2323 if (nnp)
2324 return -EPERM;
2325 else
2326 return -EACCES;
2327 }
2328 return 0;
2329}
2330
David Howellsa6f76f22008-11-14 10:39:24 +11002331static int selinux_bprm_set_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332{
David Howellsa6f76f22008-11-14 10:39:24 +11002333 const struct task_security_struct *old_tsec;
2334 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 struct inode_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04002336 struct common_audit_data ad;
Al Viro496ad9a2013-01-23 17:07:38 -05002337 struct inode *inode = file_inode(bprm->file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 int rc;
2339
David Howellsa6f76f22008-11-14 10:39:24 +11002340 /* SELinux context only depends on initial program or script and not
2341 * the script interpreter */
2342 if (bprm->cred_prepared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 return 0;
2344
David Howellsa6f76f22008-11-14 10:39:24 +11002345 old_tsec = current_security();
2346 new_tsec = bprm->cred->security;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002347 isec = inode_security(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
2349 /* Default to the current task SID. */
David Howellsa6f76f22008-11-14 10:39:24 +11002350 new_tsec->sid = old_tsec->sid;
2351 new_tsec->osid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
Michael LeMay28eba5b2006-06-27 02:53:42 -07002353 /* Reset fs, key, and sock SIDs on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002354 new_tsec->create_sid = 0;
2355 new_tsec->keycreate_sid = 0;
2356 new_tsec->sockcreate_sid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357
David Howellsa6f76f22008-11-14 10:39:24 +11002358 if (old_tsec->exec_sid) {
2359 new_tsec->sid = old_tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 /* Reset exec SID on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002361 new_tsec->exec_sid = 0;
Andy Lutomirski259e5e62012-04-12 16:47:50 -05002362
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002363 /* Fail on NNP or nosuid if not an allowed transition. */
2364 rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
2365 if (rc)
2366 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 } else {
2368 /* Check for a default transition on this program. */
David Howellsa6f76f22008-11-14 10:39:24 +11002369 rc = security_transition_sid(old_tsec->sid, isec->sid,
Eric Paris652bb9b2011-02-01 11:05:40 -05002370 SECCLASS_PROCESS, NULL,
2371 &new_tsec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 if (rc)
2373 return rc;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002374
2375 /*
2376 * Fallback to old SID on NNP or nosuid if not an allowed
2377 * transition.
2378 */
2379 rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
2380 if (rc)
2381 new_tsec->sid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 }
2383
Vivek Goyal43af5de2016-09-09 11:37:49 -04002384 ad.type = LSM_AUDIT_DATA_FILE;
2385 ad.u.file = bprm->file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
David Howellsa6f76f22008-11-14 10:39:24 +11002387 if (new_tsec->sid == old_tsec->sid) {
2388 rc = avc_has_perm(old_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
2390 if (rc)
2391 return rc;
2392 } else {
2393 /* Check permissions for the transition. */
David Howellsa6f76f22008-11-14 10:39:24 +11002394 rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
2396 if (rc)
2397 return rc;
2398
David Howellsa6f76f22008-11-14 10:39:24 +11002399 rc = avc_has_perm(new_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
2401 if (rc)
2402 return rc;
2403
David Howellsa6f76f22008-11-14 10:39:24 +11002404 /* Check for shared state */
2405 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
2406 rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
2407 SECCLASS_PROCESS, PROCESS__SHARE,
2408 NULL);
2409 if (rc)
2410 return -EPERM;
2411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412
David Howellsa6f76f22008-11-14 10:39:24 +11002413 /* Make sure that anyone attempting to ptrace over a task that
2414 * changes its SID has the appropriate permit */
2415 if (bprm->unsafe &
2416 (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
Paul Moore0c6181c2016-03-30 21:41:21 -04002417 u32 ptsid = ptrace_parent_sid(current);
David Howellsa6f76f22008-11-14 10:39:24 +11002418 if (ptsid != 0) {
2419 rc = avc_has_perm(ptsid, new_tsec->sid,
2420 SECCLASS_PROCESS,
2421 PROCESS__PTRACE, NULL);
2422 if (rc)
2423 return -EPERM;
2424 }
2425 }
2426
2427 /* Clear any possibly unsafe personality bits on exec: */
2428 bprm->per_clear |= PER_CLEAR_ON_SETID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 }
2430
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 return 0;
2432}
2433
Eric Paris828dfe12008-04-17 13:17:49 -04002434static int selinux_bprm_secureexec(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435{
Paul Moore5fb49872010-04-22 14:46:19 -04002436 const struct task_security_struct *tsec = current_security();
David Howells275bb412008-11-14 10:39:19 +11002437 u32 sid, osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 int atsecure = 0;
2439
David Howells275bb412008-11-14 10:39:19 +11002440 sid = tsec->sid;
2441 osid = tsec->osid;
2442
2443 if (osid != sid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 /* Enable secure mode for SIDs transitions unless
2445 the noatsecure permission is granted between
2446 the two SIDs, i.e. ahp returns 0. */
David Howells275bb412008-11-14 10:39:19 +11002447 atsecure = avc_has_perm(osid, sid,
David Howellsa6f76f22008-11-14 10:39:24 +11002448 SECCLASS_PROCESS,
2449 PROCESS__NOATSECURE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 }
2451
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002452 return !!atsecure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453}
2454
Al Viroc3c073f2012-08-21 22:32:06 -04002455static int match_file(const void *p, struct file *file, unsigned fd)
2456{
2457 return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0;
2458}
2459
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460/* Derived from fs/exec.c:flush_old_files. */
David Howells745ca242008-11-14 10:39:22 +11002461static inline void flush_unauthorized_files(const struct cred *cred,
2462 struct files_struct *files)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 struct file *file, *devnull = NULL;
Stephen Smalleyb20c8122006-09-25 23:32:03 -07002465 struct tty_struct *tty;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002466 int drop_tty = 0;
Al Viroc3c073f2012-08-21 22:32:06 -04002467 unsigned n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002469 tty = get_current_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 if (tty) {
Peter Hurley4a510962016-01-09 21:35:23 -08002471 spin_lock(&tty->files_lock);
Eric Paris37dd0bd2008-10-31 17:40:00 -04002472 if (!list_empty(&tty->tty_files)) {
Nick Piggind996b622010-08-18 04:37:36 +10002473 struct tty_file_private *file_priv;
Eric Paris37dd0bd2008-10-31 17:40:00 -04002474
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 /* Revalidate access to controlling tty.
David Howells13f8e982013-06-13 23:37:55 +01002476 Use file_path_has_perm on the tty path directly
2477 rather than using file_has_perm, as this particular
2478 open file may belong to another process and we are
2479 only interested in the inode-based check here. */
Nick Piggind996b622010-08-18 04:37:36 +10002480 file_priv = list_first_entry(&tty->tty_files,
2481 struct tty_file_private, list);
2482 file = file_priv->file;
David Howells13f8e982013-06-13 23:37:55 +01002483 if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE))
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002484 drop_tty = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 }
Peter Hurley4a510962016-01-09 21:35:23 -08002486 spin_unlock(&tty->files_lock);
Alan Cox452a00d2008-10-13 10:39:13 +01002487 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 }
Eric W. Biederman98a27ba2007-05-08 00:26:56 -07002489 /* Reset controlling tty. */
2490 if (drop_tty)
2491 no_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
2493 /* Revalidate access to inherited open files. */
Al Viroc3c073f2012-08-21 22:32:06 -04002494 n = iterate_fd(files, 0, match_file, cred);
2495 if (!n) /* none found? */
2496 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
Al Viroc3c073f2012-08-21 22:32:06 -04002498 devnull = dentry_open(&selinux_null, O_RDWR, cred);
Al Viro45525b22012-10-16 13:30:07 -04002499 if (IS_ERR(devnull))
2500 devnull = NULL;
2501 /* replace all the matching ones with this */
2502 do {
2503 replace_fd(n - 1, devnull, 0);
2504 } while ((n = iterate_fd(files, n, match_file, cred)) != 0);
2505 if (devnull)
Al Viroc3c073f2012-08-21 22:32:06 -04002506 fput(devnull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507}
2508
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509/*
David Howellsa6f76f22008-11-14 10:39:24 +11002510 * Prepare a process for imminent new credential changes due to exec
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 */
David Howellsa6f76f22008-11-14 10:39:24 +11002512static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513{
David Howellsa6f76f22008-11-14 10:39:24 +11002514 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 struct rlimit *rlim, *initrlim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 int rc, i;
2517
David Howellsa6f76f22008-11-14 10:39:24 +11002518 new_tsec = bprm->cred->security;
2519 if (new_tsec->sid == new_tsec->osid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 return;
2521
2522 /* Close files for which the new task SID is not authorized. */
David Howellsa6f76f22008-11-14 10:39:24 +11002523 flush_unauthorized_files(bprm->cred, current->files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
David Howellsa6f76f22008-11-14 10:39:24 +11002525 /* Always clear parent death signal on SID transitions. */
2526 current->pdeath_signal = 0;
2527
2528 /* Check whether the new SID can inherit resource limits from the old
2529 * SID. If not, reset all soft limits to the lower of the current
2530 * task's hard limit and the init task's soft limit.
2531 *
2532 * Note that the setting of hard limits (even to lower them) can be
2533 * controlled by the setrlimit check. The inclusion of the init task's
2534 * soft limit into the computation is to avoid resetting soft limits
2535 * higher than the default soft limit for cases where the default is
2536 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2537 */
2538 rc = avc_has_perm(new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
2539 PROCESS__RLIMITINH, NULL);
2540 if (rc) {
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002541 /* protect against do_prlimit() */
2542 task_lock(current);
David Howellsa6f76f22008-11-14 10:39:24 +11002543 for (i = 0; i < RLIM_NLIMITS; i++) {
2544 rlim = current->signal->rlim + i;
2545 initrlim = init_task.signal->rlim + i;
2546 rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
2547 }
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002548 task_unlock(current);
2549 update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
David Howellsa6f76f22008-11-14 10:39:24 +11002550 }
2551}
2552
2553/*
2554 * Clean up the process immediately after the installation of new credentials
2555 * due to exec
2556 */
2557static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
2558{
2559 const struct task_security_struct *tsec = current_security();
2560 struct itimerval itimer;
David Howellsa6f76f22008-11-14 10:39:24 +11002561 u32 osid, sid;
2562 int rc, i;
David Howellsa6f76f22008-11-14 10:39:24 +11002563
David Howellsa6f76f22008-11-14 10:39:24 +11002564 osid = tsec->osid;
2565 sid = tsec->sid;
2566
2567 if (sid == osid)
2568 return;
2569
2570 /* Check whether the new SID can inherit signal state from the old SID.
2571 * If not, clear itimers to avoid subsequent signal generation and
2572 * flush and unblock signals.
2573 *
2574 * This must occur _after_ the task SID has been updated so that any
2575 * kill done after the flush will be checked against the new SID.
2576 */
2577 rc = avc_has_perm(osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 if (rc) {
2579 memset(&itimer, 0, sizeof itimer);
2580 for (i = 0; i < 3; i++)
2581 do_setitimer(i, &itimer, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterov9e7c8f82015-06-04 16:22:16 -04002583 if (!fatal_signal_pending(current)) {
2584 flush_sigqueue(&current->pending);
2585 flush_sigqueue(&current->signal->shared_pending);
David Howells3bcac022009-04-29 13:45:05 +01002586 flush_signal_handlers(current, 1);
2587 sigemptyset(&current->blocked);
Oleg Nesterov9e7c8f82015-06-04 16:22:16 -04002588 recalc_sigpending();
David Howells3bcac022009-04-29 13:45:05 +01002589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 spin_unlock_irq(&current->sighand->siglock);
2591 }
2592
David Howellsa6f76f22008-11-14 10:39:24 +11002593 /* Wake up the parent if it is waiting so that it can recheck
2594 * wait permission to the new task SID. */
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002595 read_lock(&tasklist_lock);
Oleg Nesterov0b7570e2009-09-23 15:56:46 -07002596 __wake_up_parent(current, current->real_parent);
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002597 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598}
2599
2600/* superblock security operations */
2601
2602static int selinux_sb_alloc_security(struct super_block *sb)
2603{
2604 return superblock_alloc_security(sb);
2605}
2606
2607static void selinux_sb_free_security(struct super_block *sb)
2608{
2609 superblock_free_security(sb);
2610}
2611
2612static inline int match_prefix(char *prefix, int plen, char *option, int olen)
2613{
2614 if (plen > olen)
2615 return 0;
2616
2617 return !memcmp(prefix, option, plen);
2618}
2619
2620static inline int selinux_option(char *option, int len)
2621{
Eric Paris832cbd92008-04-01 13:24:09 -04002622 return (match_prefix(CONTEXT_STR, sizeof(CONTEXT_STR)-1, option, len) ||
2623 match_prefix(FSCONTEXT_STR, sizeof(FSCONTEXT_STR)-1, option, len) ||
2624 match_prefix(DEFCONTEXT_STR, sizeof(DEFCONTEXT_STR)-1, option, len) ||
David P. Quigley11689d42009-01-16 09:22:03 -05002625 match_prefix(ROOTCONTEXT_STR, sizeof(ROOTCONTEXT_STR)-1, option, len) ||
2626 match_prefix(LABELSUPP_STR, sizeof(LABELSUPP_STR)-1, option, len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627}
2628
2629static inline void take_option(char **to, char *from, int *first, int len)
2630{
2631 if (!*first) {
2632 **to = ',';
2633 *to += 1;
Cory Olmo3528a952006-09-29 01:58:44 -07002634 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 *first = 0;
2636 memcpy(*to, from, len);
2637 *to += len;
2638}
2639
Eric Paris828dfe12008-04-17 13:17:49 -04002640static inline void take_selinux_option(char **to, char *from, int *first,
2641 int len)
Cory Olmo3528a952006-09-29 01:58:44 -07002642{
2643 int current_size = 0;
2644
2645 if (!*first) {
2646 **to = '|';
2647 *to += 1;
Eric Paris828dfe12008-04-17 13:17:49 -04002648 } else
Cory Olmo3528a952006-09-29 01:58:44 -07002649 *first = 0;
2650
2651 while (current_size < len) {
2652 if (*from != '"') {
2653 **to = *from;
2654 *to += 1;
2655 }
2656 from += 1;
2657 current_size += 1;
2658 }
2659}
2660
Eric Parise0007522008-03-05 10:31:54 -05002661static int selinux_sb_copy_data(char *orig, char *copy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662{
2663 int fnosec, fsec, rc = 0;
2664 char *in_save, *in_curr, *in_end;
2665 char *sec_curr, *nosec_save, *nosec;
Cory Olmo3528a952006-09-29 01:58:44 -07002666 int open_quote = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667
2668 in_curr = orig;
2669 sec_curr = copy;
2670
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 nosec = (char *)get_zeroed_page(GFP_KERNEL);
2672 if (!nosec) {
2673 rc = -ENOMEM;
2674 goto out;
2675 }
2676
2677 nosec_save = nosec;
2678 fnosec = fsec = 1;
2679 in_save = in_end = orig;
2680
2681 do {
Cory Olmo3528a952006-09-29 01:58:44 -07002682 if (*in_end == '"')
2683 open_quote = !open_quote;
2684 if ((*in_end == ',' && open_quote == 0) ||
2685 *in_end == '\0') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 int len = in_end - in_curr;
2687
2688 if (selinux_option(in_curr, len))
Cory Olmo3528a952006-09-29 01:58:44 -07002689 take_selinux_option(&sec_curr, in_curr, &fsec, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 else
2691 take_option(&nosec, in_curr, &fnosec, len);
2692
2693 in_curr = in_end + 1;
2694 }
2695 } while (*in_end++);
2696
Eric Paris6931dfc2005-06-30 02:58:51 -07002697 strcpy(in_save, nosec_save);
Gerald Schaeferda3caa22005-06-21 17:15:18 -07002698 free_page((unsigned long)nosec_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699out:
2700 return rc;
2701}
2702
Eric Paris026eb162011-03-03 16:09:14 -05002703static int selinux_sb_remount(struct super_block *sb, void *data)
2704{
2705 int rc, i, *flags;
2706 struct security_mnt_opts opts;
2707 char *secdata, **mount_options;
2708 struct superblock_security_struct *sbsec = sb->s_security;
2709
2710 if (!(sbsec->flags & SE_SBINITIALIZED))
2711 return 0;
2712
2713 if (!data)
2714 return 0;
2715
2716 if (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
2717 return 0;
2718
2719 security_init_mnt_opts(&opts);
2720 secdata = alloc_secdata();
2721 if (!secdata)
2722 return -ENOMEM;
2723 rc = selinux_sb_copy_data(data, secdata);
2724 if (rc)
2725 goto out_free_secdata;
2726
2727 rc = selinux_parse_opts_str(secdata, &opts);
2728 if (rc)
2729 goto out_free_secdata;
2730
2731 mount_options = opts.mnt_opts;
2732 flags = opts.mnt_opts_flags;
2733
2734 for (i = 0; i < opts.num_mnt_opts; i++) {
2735 u32 sid;
Eric Paris026eb162011-03-03 16:09:14 -05002736
Eric Paris12f348b2012-10-09 10:56:25 -04002737 if (flags[i] == SBLABEL_MNT)
Eric Paris026eb162011-03-03 16:09:14 -05002738 continue;
Rasmus Villemoes44be2f62015-10-21 17:44:25 -04002739 rc = security_context_str_to_sid(mount_options[i], &sid, GFP_KERNEL);
Eric Paris026eb162011-03-03 16:09:14 -05002740 if (rc) {
Rasmus Villemoes44be2f62015-10-21 17:44:25 -04002741 printk(KERN_WARNING "SELinux: security_context_str_to_sid"
Linus Torvalds29b1deb2013-12-15 11:17:45 -08002742 "(%s) failed for (dev %s, type %s) errno=%d\n",
2743 mount_options[i], sb->s_id, sb->s_type->name, rc);
Eric Paris026eb162011-03-03 16:09:14 -05002744 goto out_free_opts;
2745 }
2746 rc = -EINVAL;
2747 switch (flags[i]) {
2748 case FSCONTEXT_MNT:
2749 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
2750 goto out_bad_option;
2751 break;
2752 case CONTEXT_MNT:
2753 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
2754 goto out_bad_option;
2755 break;
2756 case ROOTCONTEXT_MNT: {
2757 struct inode_security_struct *root_isec;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002758 root_isec = backing_inode_security(sb->s_root);
Eric Paris026eb162011-03-03 16:09:14 -05002759
2760 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
2761 goto out_bad_option;
2762 break;
2763 }
2764 case DEFCONTEXT_MNT:
2765 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
2766 goto out_bad_option;
2767 break;
2768 default:
2769 goto out_free_opts;
2770 }
2771 }
2772
2773 rc = 0;
2774out_free_opts:
2775 security_free_mnt_opts(&opts);
2776out_free_secdata:
2777 free_secdata(secdata);
2778 return rc;
2779out_bad_option:
2780 printk(KERN_WARNING "SELinux: unable to change security options "
Linus Torvalds29b1deb2013-12-15 11:17:45 -08002781 "during remount (dev %s, type=%s)\n", sb->s_id,
2782 sb->s_type->name);
Eric Paris026eb162011-03-03 16:09:14 -05002783 goto out_free_opts;
2784}
2785
James Morris12204e22008-12-19 10:44:42 +11002786static int selinux_sb_kern_mount(struct super_block *sb, int flags, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787{
David Howells88e67f32008-11-14 10:39:21 +11002788 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002789 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 int rc;
2791
2792 rc = superblock_doinit(sb, data);
2793 if (rc)
2794 return rc;
2795
James Morris74192242008-12-19 11:41:10 +11002796 /* Allow all mounts performed by the kernel */
Ondrej Mosnacek62044cb2018-11-16 14:12:02 +01002797 if (flags & (MS_KERNMOUNT | MS_SUBMOUNT))
James Morris74192242008-12-19 11:41:10 +11002798 return 0;
2799
Eric Paris50c205f2012-04-04 15:01:43 -04002800 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002801 ad.u.dentry = sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002802 return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803}
2804
David Howells726c3342006-06-23 02:02:58 -07002805static int selinux_sb_statfs(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806{
David Howells88e67f32008-11-14 10:39:21 +11002807 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002808 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809
Eric Paris50c205f2012-04-04 15:01:43 -04002810 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002811 ad.u.dentry = dentry->d_sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002812 return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813}
2814
Al Viro808d4e32012-10-11 11:42:01 -04002815static int selinux_mount(const char *dev_name,
Al Viro8a04c432016-03-25 14:52:53 -04002816 const struct path *path,
Al Viro808d4e32012-10-11 11:42:01 -04002817 const char *type,
Eric Paris828dfe12008-04-17 13:17:49 -04002818 unsigned long flags,
2819 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820{
David Howells88e67f32008-11-14 10:39:21 +11002821 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822
2823 if (flags & MS_REMOUNT)
Al Virod8c95842011-12-07 18:16:57 -05002824 return superblock_has_perm(cred, path->dentry->d_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002825 FILESYSTEM__REMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 else
Eric Paris2875fa02011-04-28 16:04:24 -04002827 return path_has_perm(cred, path, FILE__MOUNTON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828}
2829
2830static int selinux_umount(struct vfsmount *mnt, int flags)
2831{
David Howells88e67f32008-11-14 10:39:21 +11002832 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833
David Howells88e67f32008-11-14 10:39:21 +11002834 return superblock_has_perm(cred, mnt->mnt_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002835 FILESYSTEM__UNMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836}
2837
2838/* inode security operations */
2839
2840static int selinux_inode_alloc_security(struct inode *inode)
2841{
2842 return inode_alloc_security(inode);
2843}
2844
2845static void selinux_inode_free_security(struct inode *inode)
2846{
2847 inode_free_security(inode);
2848}
2849
David Quigleyd47be3d2013-05-22 12:50:34 -04002850static int selinux_dentry_init_security(struct dentry *dentry, int mode,
Al Viro4f3ccd72016-07-20 16:06:15 -04002851 const struct qstr *name, void **ctx,
David Quigleyd47be3d2013-05-22 12:50:34 -04002852 u32 *ctxlen)
2853{
David Quigleyd47be3d2013-05-22 12:50:34 -04002854 u32 newsid;
2855 int rc;
2856
Vivek Goyalc957f6d2016-07-13 10:44:51 -04002857 rc = selinux_determine_inode_label(current_security(),
2858 d_inode(dentry->d_parent), name,
David Howellsc3c188b2015-07-10 17:19:58 -04002859 inode_mode_to_security_class(mode),
2860 &newsid);
2861 if (rc)
2862 return rc;
David Quigleyd47be3d2013-05-22 12:50:34 -04002863
2864 return security_sid_to_context(newsid, (char **)ctx, ctxlen);
2865}
2866
Vivek Goyala518b0a2016-07-13 10:44:53 -04002867static int selinux_dentry_create_files_as(struct dentry *dentry, int mode,
2868 struct qstr *name,
2869 const struct cred *old,
2870 struct cred *new)
2871{
2872 u32 newsid;
2873 int rc;
2874 struct task_security_struct *tsec;
2875
2876 rc = selinux_determine_inode_label(old->security,
2877 d_inode(dentry->d_parent), name,
2878 inode_mode_to_security_class(mode),
2879 &newsid);
2880 if (rc)
2881 return rc;
2882
2883 tsec = new->security;
2884 tsec->create_sid = newsid;
2885 return 0;
2886}
2887
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002888static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +09002889 const struct qstr *qstr,
2890 const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -05002891 void **value, size_t *len)
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002892{
Paul Moore5fb49872010-04-22 14:46:19 -04002893 const struct task_security_struct *tsec = current_security();
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002894 struct superblock_security_struct *sbsec;
David Howells275bb412008-11-14 10:39:19 +11002895 u32 sid, newsid, clen;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002896 int rc;
Tetsuo Handa95489062013-07-25 05:44:02 +09002897 char *context;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002898
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002899 sbsec = dir->i_sb->s_security;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002900
David Howells275bb412008-11-14 10:39:19 +11002901 sid = tsec->sid;
2902 newsid = tsec->create_sid;
2903
Vivek Goyalc957f6d2016-07-13 10:44:51 -04002904 rc = selinux_determine_inode_label(current_security(),
David Howellsc3c188b2015-07-10 17:19:58 -04002905 dir, qstr,
2906 inode_mode_to_security_class(inode->i_mode),
2907 &newsid);
2908 if (rc)
2909 return rc;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002910
Eric Paris296fddf2006-09-25 23:32:00 -07002911 /* Possibly defer initialization to selinux_complete_init. */
David P. Quigley0d90a7e2009-01-16 09:22:02 -05002912 if (sbsec->flags & SE_SBINITIALIZED) {
Eric Paris296fddf2006-09-25 23:32:00 -07002913 struct inode_security_struct *isec = inode->i_security;
2914 isec->sclass = inode_mode_to_security_class(inode->i_mode);
2915 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05002916 isec->initialized = LABEL_INITIALIZED;
Eric Paris296fddf2006-09-25 23:32:00 -07002917 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002918
Eric Paris12f348b2012-10-09 10:56:25 -04002919 if (!ss_initialized || !(sbsec->flags & SBLABEL_MNT))
Stephen Smalley25a74f32005-11-08 21:34:33 -08002920 return -EOPNOTSUPP;
2921
Tetsuo Handa95489062013-07-25 05:44:02 +09002922 if (name)
2923 *name = XATTR_SELINUX_SUFFIX;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002924
2925 if (value && len) {
Stephen Smalley12b29f32008-05-07 13:03:20 -04002926 rc = security_sid_to_context_force(newsid, &context, &clen);
Tetsuo Handa95489062013-07-25 05:44:02 +09002927 if (rc)
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002928 return rc;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002929 *value = context;
2930 *len = clen;
2931 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002932
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002933 return 0;
2934}
2935
Al Viro4acdaf22011-07-26 01:42:34 -04002936static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937{
2938 return may_create(dir, dentry, SECCLASS_FILE);
2939}
2940
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2942{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 return may_link(dir, old_dentry, MAY_LINK);
2944}
2945
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
2947{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 return may_link(dir, dentry, MAY_UNLINK);
2949}
2950
2951static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
2952{
2953 return may_create(dir, dentry, SECCLASS_LNK_FILE);
2954}
2955
Al Viro18bb1db2011-07-26 01:41:39 -04002956static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957{
2958 return may_create(dir, dentry, SECCLASS_DIR);
2959}
2960
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
2962{
2963 return may_link(dir, dentry, MAY_RMDIR);
2964}
2965
Al Viro1a67aaf2011-07-26 01:52:52 -04002966static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 return may_create(dir, dentry, inode_mode_to_security_class(mode));
2969}
2970
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
Eric Paris828dfe12008-04-17 13:17:49 -04002972 struct inode *new_inode, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973{
2974 return may_rename(old_inode, old_dentry, new_inode, new_dentry);
2975}
2976
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977static int selinux_inode_readlink(struct dentry *dentry)
2978{
David Howells88e67f32008-11-14 10:39:21 +11002979 const struct cred *cred = current_cred();
2980
Eric Paris2875fa02011-04-28 16:04:24 -04002981 return dentry_has_perm(cred, dentry, FILE__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982}
2983
NeilBrownbda0be72015-03-23 13:37:39 +11002984static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode,
2985 bool rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986{
David Howells88e67f32008-11-14 10:39:21 +11002987 const struct cred *cred = current_cred();
NeilBrownbda0be72015-03-23 13:37:39 +11002988 struct common_audit_data ad;
2989 struct inode_security_struct *isec;
2990 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991
NeilBrownbda0be72015-03-23 13:37:39 +11002992 validate_creds(cred);
2993
2994 ad.type = LSM_AUDIT_DATA_DENTRY;
2995 ad.u.dentry = dentry;
2996 sid = cred_sid(cred);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05002997 isec = inode_security_rcu(inode, rcu);
2998 if (IS_ERR(isec))
2999 return PTR_ERR(isec);
NeilBrownbda0be72015-03-23 13:37:39 +11003000
3001 return avc_has_perm_flags(sid, isec->sid, isec->sclass, FILE__READ, &ad,
3002 rcu ? MAY_NOT_BLOCK : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003}
3004
Eric Parisd4cf970d2012-04-04 15:01:42 -04003005static noinline int audit_inode_permission(struct inode *inode,
3006 u32 perms, u32 audited, u32 denied,
Stephen Smalley626b9742014-04-29 11:29:04 -07003007 int result,
Eric Parisd4cf970d2012-04-04 15:01:42 -04003008 unsigned flags)
3009{
3010 struct common_audit_data ad;
Eric Parisd4cf970d2012-04-04 15:01:42 -04003011 struct inode_security_struct *isec = inode->i_security;
3012 int rc;
3013
Eric Paris50c205f2012-04-04 15:01:43 -04003014 ad.type = LSM_AUDIT_DATA_INODE;
Eric Parisd4cf970d2012-04-04 15:01:42 -04003015 ad.u.inode = inode;
3016
3017 rc = slow_avc_audit(current_sid(), isec->sid, isec->sclass, perms,
Stephen Smalley626b9742014-04-29 11:29:04 -07003018 audited, denied, result, &ad, flags);
Eric Parisd4cf970d2012-04-04 15:01:42 -04003019 if (rc)
3020 return rc;
3021 return 0;
3022}
3023
Al Viroe74f71e2011-06-20 19:38:15 -04003024static int selinux_inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025{
David Howells88e67f32008-11-14 10:39:21 +11003026 const struct cred *cred = current_cred();
Eric Parisb782e0a2010-07-23 11:44:03 -04003027 u32 perms;
3028 bool from_access;
Al Virocf1dd1d2011-06-20 19:44:08 -04003029 unsigned flags = mask & MAY_NOT_BLOCK;
Eric Paris2e334052012-04-04 15:01:42 -04003030 struct inode_security_struct *isec;
3031 u32 sid;
3032 struct av_decision avd;
3033 int rc, rc2;
3034 u32 audited, denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035
Eric Parisb782e0a2010-07-23 11:44:03 -04003036 from_access = mask & MAY_ACCESS;
Eric Parisd09ca732010-07-23 11:43:57 -04003037 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
3038
Eric Parisb782e0a2010-07-23 11:44:03 -04003039 /* No permission to check. Existence test. */
3040 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042
Eric Paris2e334052012-04-04 15:01:42 -04003043 validate_creds(cred);
Eric Parisb782e0a2010-07-23 11:44:03 -04003044
Eric Paris2e334052012-04-04 15:01:42 -04003045 if (unlikely(IS_PRIVATE(inode)))
3046 return 0;
Eric Parisb782e0a2010-07-23 11:44:03 -04003047
3048 perms = file_mask_to_av(inode->i_mode, mask);
3049
Eric Paris2e334052012-04-04 15:01:42 -04003050 sid = cred_sid(cred);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05003051 isec = inode_security_rcu(inode, flags & MAY_NOT_BLOCK);
3052 if (IS_ERR(isec))
3053 return PTR_ERR(isec);
Eric Paris2e334052012-04-04 15:01:42 -04003054
3055 rc = avc_has_perm_noaudit(sid, isec->sid, isec->sclass, perms, 0, &avd);
3056 audited = avc_audit_required(perms, &avd, rc,
3057 from_access ? FILE__AUDIT_ACCESS : 0,
3058 &denied);
3059 if (likely(!audited))
3060 return rc;
3061
Stephen Smalley626b9742014-04-29 11:29:04 -07003062 rc2 = audit_inode_permission(inode, perms, audited, denied, rc, flags);
Eric Paris2e334052012-04-04 15:01:42 -04003063 if (rc2)
3064 return rc2;
3065 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066}
3067
3068static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
3069{
David Howells88e67f32008-11-14 10:39:21 +11003070 const struct cred *cred = current_cred();
Stephen Smalleyb983b2a2017-05-12 12:41:24 -04003071 struct inode *inode = d_backing_inode(dentry);
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003072 unsigned int ia_valid = iattr->ia_valid;
Eric Paris95dbf732012-04-04 13:45:34 -04003073 __u32 av = FILE__WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003075 /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
3076 if (ia_valid & ATTR_FORCE) {
3077 ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
3078 ATTR_FORCE);
3079 if (!ia_valid)
3080 return 0;
3081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003083 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
3084 ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
Eric Paris2875fa02011-04-28 16:04:24 -04003085 return dentry_has_perm(cred, dentry, FILE__SETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086
Stephen Smalleyb983b2a2017-05-12 12:41:24 -04003087 if (selinux_policycap_openperm &&
3088 inode->i_sb->s_magic != SOCKFS_MAGIC &&
3089 (ia_valid & ATTR_SIZE) &&
3090 !(ia_valid & ATTR_FILE))
Eric Paris95dbf732012-04-04 13:45:34 -04003091 av |= FILE__OPEN;
3092
3093 return dentry_has_perm(cred, dentry, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094}
3095
Al Viro3f7036a2015-03-08 19:28:30 -04003096static int selinux_inode_getattr(const struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097{
Al Viro3f7036a2015-03-08 19:28:30 -04003098 return path_has_perm(current_cred(), path, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099}
3100
David Howells8f0cfa52008-04-29 00:59:41 -07003101static int selinux_inode_setotherxattr(struct dentry *dentry, const char *name)
Serge E. Hallynb5376772007-10-16 23:31:36 -07003102{
David Howells88e67f32008-11-14 10:39:21 +11003103 const struct cred *cred = current_cred();
3104
Serge E. Hallynb5376772007-10-16 23:31:36 -07003105 if (!strncmp(name, XATTR_SECURITY_PREFIX,
3106 sizeof XATTR_SECURITY_PREFIX - 1)) {
3107 if (!strcmp(name, XATTR_NAME_CAPS)) {
3108 if (!capable(CAP_SETFCAP))
3109 return -EPERM;
3110 } else if (!capable(CAP_SYS_ADMIN)) {
3111 /* A different attribute in the security namespace.
3112 Restrict to administrator. */
3113 return -EPERM;
3114 }
3115 }
3116
3117 /* Not an attribute we recognize, so just check the
3118 ordinary setattr permission. */
Eric Paris2875fa02011-04-28 16:04:24 -04003119 return dentry_has_perm(cred, dentry, FILE__SETATTR);
Serge E. Hallynb5376772007-10-16 23:31:36 -07003120}
3121
David Howells8f0cfa52008-04-29 00:59:41 -07003122static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
3123 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124{
David Howellsc6f493d2015-03-17 22:26:22 +00003125 struct inode *inode = d_backing_inode(dentry);
Paul Moore20cdef82016-04-04 14:14:42 -04003126 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 struct superblock_security_struct *sbsec;
Thomas Liu2bf49692009-07-14 12:14:09 -04003128 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11003129 u32 newsid, sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 int rc = 0;
3131
Serge E. Hallynb5376772007-10-16 23:31:36 -07003132 if (strcmp(name, XATTR_NAME_SELINUX))
3133 return selinux_inode_setotherxattr(dentry, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134
3135 sbsec = inode->i_sb->s_security;
Eric Paris12f348b2012-10-09 10:56:25 -04003136 if (!(sbsec->flags & SBLABEL_MNT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 return -EOPNOTSUPP;
3138
Serge E. Hallyn2e149672011-03-23 16:43:26 -07003139 if (!inode_owner_or_capable(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 return -EPERM;
3141
Eric Paris50c205f2012-04-04 15:01:43 -04003142 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04003143 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144
Paul Moore20cdef82016-04-04 14:14:42 -04003145 isec = backing_inode_security(dentry);
David Howells275bb412008-11-14 10:39:19 +11003146 rc = avc_has_perm(sid, isec->sid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 FILE__RELABELFROM, &ad);
3148 if (rc)
3149 return rc;
3150
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01003151 rc = security_context_to_sid(value, size, &newsid, GFP_KERNEL);
Stephen Smalley12b29f32008-05-07 13:03:20 -04003152 if (rc == -EINVAL) {
Eric Parisd6ea83e2012-04-04 13:45:49 -04003153 if (!capable(CAP_MAC_ADMIN)) {
3154 struct audit_buffer *ab;
3155 size_t audit_size;
3156 const char *str;
3157
3158 /* We strip a nul only if it is at the end, otherwise the
3159 * context contains a nul and we should audit that */
Al Viroe3fea3f2012-06-09 08:15:16 +01003160 if (value) {
3161 str = value;
3162 if (str[size - 1] == '\0')
3163 audit_size = size - 1;
3164 else
3165 audit_size = size;
3166 } else {
3167 str = "";
3168 audit_size = 0;
3169 }
Eric Parisd6ea83e2012-04-04 13:45:49 -04003170 ab = audit_log_start(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR);
3171 audit_log_format(ab, "op=setxattr invalid_context=");
3172 audit_log_n_untrustedstring(ab, value, audit_size);
3173 audit_log_end(ab);
3174
Stephen Smalley12b29f32008-05-07 13:03:20 -04003175 return rc;
Eric Parisd6ea83e2012-04-04 13:45:49 -04003176 }
Stephen Smalley12b29f32008-05-07 13:03:20 -04003177 rc = security_context_to_sid_force(value, size, &newsid);
3178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 if (rc)
3180 return rc;
3181
David Howells275bb412008-11-14 10:39:19 +11003182 rc = avc_has_perm(sid, newsid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 FILE__RELABELTO, &ad);
3184 if (rc)
3185 return rc;
3186
David Howells275bb412008-11-14 10:39:19 +11003187 rc = security_validate_transition(isec->sid, newsid, sid,
Eric Paris828dfe12008-04-17 13:17:49 -04003188 isec->sclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 if (rc)
3190 return rc;
3191
3192 return avc_has_perm(newsid,
3193 sbsec->sid,
3194 SECCLASS_FILESYSTEM,
3195 FILESYSTEM__ASSOCIATE,
3196 &ad);
3197}
3198
David Howells8f0cfa52008-04-29 00:59:41 -07003199static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
Eric Parisf5269712008-05-14 11:27:45 -04003200 const void *value, size_t size,
David Howells8f0cfa52008-04-29 00:59:41 -07003201 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202{
David Howellsc6f493d2015-03-17 22:26:22 +00003203 struct inode *inode = d_backing_inode(dentry);
Paul Moore20cdef82016-04-04 14:14:42 -04003204 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 u32 newsid;
3206 int rc;
3207
3208 if (strcmp(name, XATTR_NAME_SELINUX)) {
3209 /* Not an attribute we recognize, so nothing to do. */
3210 return;
3211 }
3212
Stephen Smalley12b29f32008-05-07 13:03:20 -04003213 rc = security_context_to_sid_force(value, size, &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 if (rc) {
Stephen Smalley12b29f32008-05-07 13:03:20 -04003215 printk(KERN_ERR "SELinux: unable to map context to SID"
3216 "for (%s, %lu), rc=%d\n",
3217 inode->i_sb->s_id, inode->i_ino, -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 return;
3219 }
3220
Paul Moore20cdef82016-04-04 14:14:42 -04003221 isec = backing_inode_security(dentry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003222 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05003224 isec->initialized = LABEL_INITIALIZED;
David Quigleyaa9c2662013-05-22 12:50:44 -04003225
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 return;
3227}
3228
David Howells8f0cfa52008-04-29 00:59:41 -07003229static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230{
David Howells88e67f32008-11-14 10:39:21 +11003231 const struct cred *cred = current_cred();
3232
Eric Paris2875fa02011-04-28 16:04:24 -04003233 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234}
3235
Eric Paris828dfe12008-04-17 13:17:49 -04003236static int selinux_inode_listxattr(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237{
David Howells88e67f32008-11-14 10:39:21 +11003238 const struct cred *cred = current_cred();
3239
Eric Paris2875fa02011-04-28 16:04:24 -04003240 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241}
3242
David Howells8f0cfa52008-04-29 00:59:41 -07003243static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244{
Serge E. Hallynb5376772007-10-16 23:31:36 -07003245 if (strcmp(name, XATTR_NAME_SELINUX))
3246 return selinux_inode_setotherxattr(dentry, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247
3248 /* No one is allowed to remove a SELinux security label.
3249 You can change the label, but all data must be labeled. */
3250 return -EACCES;
3251}
3252
James Morrisd381d8a2005-10-30 14:59:22 -08003253/*
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003254 * Copy the inode security context value to the user.
James Morrisd381d8a2005-10-30 14:59:22 -08003255 *
3256 * Permission check is handled by selinux_inode_getxattr hook.
3257 */
Andreas Gruenbacherea861df2015-12-24 11:09:39 -05003258static int selinux_inode_getsecurity(struct inode *inode, const char *name, void **buffer, bool alloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259{
David P. Quigley42492592008-02-04 22:29:39 -08003260 u32 size;
3261 int error;
3262 char *context = NULL;
Paul Moore20cdef82016-04-04 14:14:42 -04003263 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00003265 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3266 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003268 /*
3269 * If the caller has CAP_MAC_ADMIN, then get the raw context
3270 * value even if it is not defined by current policy; otherwise,
3271 * use the in-core value under current policy.
3272 * Use the non-auditing forms of the permission checks since
3273 * getxattr may be called by unprivileged processes commonly
3274 * and lack of permission just means that we fall back to the
3275 * in-core context value, not a denial.
3276 */
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07003277 error = cap_capable(current_cred(), &init_user_ns, CAP_MAC_ADMIN,
3278 SECURITY_CAP_NOAUDIT);
3279 if (!error)
3280 error = cred_has_capability(current_cred(), CAP_MAC_ADMIN,
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04003281 SECURITY_CAP_NOAUDIT, true);
Paul Moore20cdef82016-04-04 14:14:42 -04003282 isec = inode_security(inode);
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003283 if (!error)
3284 error = security_sid_to_context_force(isec->sid, &context,
3285 &size);
3286 else
3287 error = security_sid_to_context(isec->sid, &context, &size);
David P. Quigley42492592008-02-04 22:29:39 -08003288 if (error)
3289 return error;
3290 error = size;
3291 if (alloc) {
3292 *buffer = context;
3293 goto out_nofree;
3294 }
3295 kfree(context);
3296out_nofree:
3297 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298}
3299
3300static int selinux_inode_setsecurity(struct inode *inode, const char *name,
Eric Paris828dfe12008-04-17 13:17:49 -04003301 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302{
Paul Moore2c971652016-04-19 16:36:28 -04003303 struct inode_security_struct *isec = inode_security_novalidate(inode);
Ondrej Mosnacek992baf52018-12-21 21:18:53 +01003304 struct superblock_security_struct *sbsec = inode->i_sb->s_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 u32 newsid;
3306 int rc;
3307
3308 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3309 return -EOPNOTSUPP;
3310
Ondrej Mosnacek992baf52018-12-21 21:18:53 +01003311 if (!(sbsec->flags & SBLABEL_MNT))
3312 return -EOPNOTSUPP;
3313
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 if (!value || !size)
3315 return -EACCES;
3316
Rasmus Villemoes20ba96a2015-10-21 17:44:26 -04003317 rc = security_context_to_sid(value, size, &newsid, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 if (rc)
3319 return rc;
3320
David Quigleyaa9c2662013-05-22 12:50:44 -04003321 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05003323 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 return 0;
3325}
3326
3327static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
3328{
3329 const int len = sizeof(XATTR_NAME_SELINUX);
3330 if (buffer && len <= buffer_size)
3331 memcpy(buffer, XATTR_NAME_SELINUX, len);
3332 return len;
3333}
3334
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05003335static void selinux_inode_getsecid(struct inode *inode, u32 *secid)
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02003336{
Andreas Gruenbachere817c2f2016-02-18 12:04:08 +01003337 struct inode_security_struct *isec = inode_security_novalidate(inode);
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02003338 *secid = isec->sid;
3339}
3340
Vivek Goyal56909eb2016-07-13 10:44:48 -04003341static int selinux_inode_copy_up(struct dentry *src, struct cred **new)
3342{
3343 u32 sid;
3344 struct task_security_struct *tsec;
3345 struct cred *new_creds = *new;
3346
3347 if (new_creds == NULL) {
3348 new_creds = prepare_creds();
3349 if (!new_creds)
3350 return -ENOMEM;
3351 }
3352
3353 tsec = new_creds->security;
3354 /* Get label from overlay inode and set it in create_sid */
3355 selinux_inode_getsecid(d_inode(src), &sid);
3356 tsec->create_sid = sid;
3357 *new = new_creds;
3358 return 0;
3359}
3360
Vivek Goyal19472b62016-07-13 10:44:50 -04003361static int selinux_inode_copy_up_xattr(const char *name)
3362{
3363 /* The copy_up hook above sets the initial context on an inode, but we
3364 * don't then want to overwrite it by blindly copying all the lower
3365 * xattrs up. Instead, we have to filter out SELinux-related xattrs.
3366 */
3367 if (strcmp(name, XATTR_NAME_SELINUX) == 0)
3368 return 1; /* Discard */
3369 /*
3370 * Any other attribute apart from SELINUX is not claimed, supported
3371 * by selinux.
3372 */
3373 return -EOPNOTSUPP;
3374}
3375
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376/* file security operations */
3377
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003378static int selinux_revalidate_file_permission(struct file *file, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379{
David Howells88e67f32008-11-14 10:39:21 +11003380 const struct cred *cred = current_cred();
Al Viro496ad9a2013-01-23 17:07:38 -05003381 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
3384 if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
3385 mask |= MAY_APPEND;
3386
Paul Moore389fb8002009-03-27 17:10:34 -04003387 return file_has_perm(cred, file,
3388 file_mask_to_av(inode->i_mode, mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389}
3390
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003391static int selinux_file_permission(struct file *file, int mask)
3392{
Al Viro496ad9a2013-01-23 17:07:38 -05003393 struct inode *inode = file_inode(file);
Stephen Smalley20dda182009-06-22 14:54:53 -04003394 struct file_security_struct *fsec = file->f_security;
Andreas Gruenbacherb1973672016-01-05 23:12:33 +01003395 struct inode_security_struct *isec;
Stephen Smalley20dda182009-06-22 14:54:53 -04003396 u32 sid = current_sid();
3397
Paul Moore389fb8002009-03-27 17:10:34 -04003398 if (!mask)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003399 /* No permission to check. Existence test. */
3400 return 0;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003401
Andreas Gruenbacherb1973672016-01-05 23:12:33 +01003402 isec = inode_security(inode);
Stephen Smalley20dda182009-06-22 14:54:53 -04003403 if (sid == fsec->sid && fsec->isid == isec->sid &&
3404 fsec->pseqno == avc_policy_seqno())
Eric Paris83d49852012-04-04 13:45:40 -04003405 /* No change since file_open check. */
Stephen Smalley20dda182009-06-22 14:54:53 -04003406 return 0;
3407
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003408 return selinux_revalidate_file_permission(file, mask);
3409}
3410
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411static int selinux_file_alloc_security(struct file *file)
3412{
3413 return file_alloc_security(file);
3414}
3415
3416static void selinux_file_free_security(struct file *file)
3417{
3418 file_free_security(file);
3419}
3420
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003421/*
3422 * Check whether a task has the ioctl permission and cmd
3423 * operation to an inode.
3424 */
Geliang Tang1d2a1682015-10-21 17:44:27 -04003425static int ioctl_has_perm(const struct cred *cred, struct file *file,
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003426 u32 requested, u16 cmd)
3427{
3428 struct common_audit_data ad;
3429 struct file_security_struct *fsec = file->f_security;
3430 struct inode *inode = file_inode(file);
Paul Moore20cdef82016-04-04 14:14:42 -04003431 struct inode_security_struct *isec;
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003432 struct lsm_ioctlop_audit ioctl;
3433 u32 ssid = cred_sid(cred);
3434 int rc;
3435 u8 driver = cmd >> 8;
3436 u8 xperm = cmd & 0xff;
3437
3438 ad.type = LSM_AUDIT_DATA_IOCTL_OP;
3439 ad.u.op = &ioctl;
3440 ad.u.op->cmd = cmd;
3441 ad.u.op->path = file->f_path;
3442
3443 if (ssid != fsec->sid) {
3444 rc = avc_has_perm(ssid, fsec->sid,
3445 SECCLASS_FD,
3446 FD__USE,
3447 &ad);
3448 if (rc)
3449 goto out;
3450 }
3451
3452 if (unlikely(IS_PRIVATE(inode)))
3453 return 0;
3454
Paul Moore20cdef82016-04-04 14:14:42 -04003455 isec = inode_security(inode);
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003456 rc = avc_has_extended_perms(ssid, isec->sid, isec->sclass,
3457 requested, driver, xperm, &ad);
3458out:
3459 return rc;
3460}
3461
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462static int selinux_file_ioctl(struct file *file, unsigned int cmd,
3463 unsigned long arg)
3464{
David Howells88e67f32008-11-14 10:39:21 +11003465 const struct cred *cred = current_cred();
Eric Paris0b24dcb2011-02-25 15:39:20 -05003466 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467
Eric Paris0b24dcb2011-02-25 15:39:20 -05003468 switch (cmd) {
3469 case FIONREAD:
3470 /* fall through */
3471 case FIBMAP:
3472 /* fall through */
3473 case FIGETBSZ:
3474 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003475 case FS_IOC_GETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003476 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003477 case FS_IOC_GETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003478 error = file_has_perm(cred, file, FILE__GETATTR);
3479 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480
Al Viro2f99c362012-03-23 16:04:05 -04003481 case FS_IOC_SETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003482 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003483 case FS_IOC_SETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003484 error = file_has_perm(cred, file, FILE__SETATTR);
3485 break;
3486
3487 /* sys_ioctl() checks */
3488 case FIONBIO:
3489 /* fall through */
3490 case FIOASYNC:
3491 error = file_has_perm(cred, file, 0);
3492 break;
3493
3494 case KDSKBENT:
3495 case KDSKBSENT:
Eric Paris6a9de492012-01-03 12:25:14 -05003496 error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04003497 SECURITY_CAP_AUDIT, true);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003498 break;
3499
3500 /* default case assumes that the command will go
3501 * to the file's ioctl() function.
3502 */
3503 default:
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003504 error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003505 }
3506 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507}
3508
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003509static int default_noexec;
3510
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
3512{
David Howells88e67f32008-11-14 10:39:21 +11003513 const struct cred *cred = current_cred();
David Howellsd84f4f92008-11-14 10:39:23 +11003514 int rc = 0;
David Howells88e67f32008-11-14 10:39:21 +11003515
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003516 if (default_noexec &&
Stephen Smalley892e8ca2015-07-10 09:40:59 -04003517 (prot & PROT_EXEC) && (!file || IS_PRIVATE(file_inode(file)) ||
3518 (!shared && (prot & PROT_WRITE)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 /*
3520 * We are making executable an anonymous mapping or a
3521 * private file mapping that will also be writable.
3522 * This has an additional check.
3523 */
David Howellsd84f4f92008-11-14 10:39:23 +11003524 rc = cred_has_perm(cred, cred, PROCESS__EXECMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 if (rc)
David Howellsd84f4f92008-11-14 10:39:23 +11003526 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528
3529 if (file) {
3530 /* read access is always possible with a mapping */
3531 u32 av = FILE__READ;
3532
3533 /* write access only matters if the mapping is shared */
3534 if (shared && (prot & PROT_WRITE))
3535 av |= FILE__WRITE;
3536
3537 if (prot & PROT_EXEC)
3538 av |= FILE__EXECUTE;
3539
David Howells88e67f32008-11-14 10:39:21 +11003540 return file_has_perm(cred, file, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 }
David Howellsd84f4f92008-11-14 10:39:23 +11003542
3543error:
3544 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545}
3546
Al Viroe5467852012-05-30 13:30:51 -04003547static int selinux_mmap_addr(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07003549 int rc = 0;
Paul Moore98883bf2014-03-19 16:46:11 -04003550
3551 if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
3552 u32 sid = current_sid();
3553 rc = avc_has_perm(sid, sid, SECCLASS_MEMPROTECT,
3554 MEMPROTECT__MMAP_ZERO, NULL);
3555 }
3556
3557 return rc;
Al Viroe5467852012-05-30 13:30:51 -04003558}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559
Al Viroe5467852012-05-30 13:30:51 -04003560static int selinux_mmap_file(struct file *file, unsigned long reqprot,
3561 unsigned long prot, unsigned long flags)
3562{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 if (selinux_checkreqprot)
3564 prot = reqprot;
3565
3566 return file_map_prot_check(file, prot,
3567 (flags & MAP_TYPE) == MAP_SHARED);
3568}
3569
3570static int selinux_file_mprotect(struct vm_area_struct *vma,
3571 unsigned long reqprot,
3572 unsigned long prot)
3573{
David Howells88e67f32008-11-14 10:39:21 +11003574 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575
3576 if (selinux_checkreqprot)
3577 prot = reqprot;
3578
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003579 if (default_noexec &&
3580 (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
James Morrisd541bbe2009-01-29 12:19:51 +11003581 int rc = 0;
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003582 if (vma->vm_start >= vma->vm_mm->start_brk &&
3583 vma->vm_end <= vma->vm_mm->brk) {
David Howellsd84f4f92008-11-14 10:39:23 +11003584 rc = cred_has_perm(cred, cred, PROCESS__EXECHEAP);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003585 } else if (!vma->vm_file &&
Stephen Smalleyc2316dbf2016-04-08 13:55:03 -04003586 ((vma->vm_start <= vma->vm_mm->start_stack &&
3587 vma->vm_end >= vma->vm_mm->start_stack) ||
Andy Lutomirskid17af502016-09-30 10:58:58 -07003588 vma_is_stack_for_current(vma))) {
David Howells3b11a1d2008-11-14 10:39:26 +11003589 rc = current_has_perm(current, PROCESS__EXECSTACK);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003590 } else if (vma->vm_file && vma->anon_vma) {
3591 /*
3592 * We are making executable a file mapping that has
3593 * had some COW done. Since pages might have been
3594 * written, check ability to execute the possibly
3595 * modified content. This typically should only
3596 * occur for text relocations.
3597 */
David Howellsd84f4f92008-11-14 10:39:23 +11003598 rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003599 }
Lorenzo Hernandez García-Hierro6b992192005-06-25 14:54:34 -07003600 if (rc)
3601 return rc;
3602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603
3604 return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
3605}
3606
3607static int selinux_file_lock(struct file *file, unsigned int cmd)
3608{
David Howells88e67f32008-11-14 10:39:21 +11003609 const struct cred *cred = current_cred();
3610
3611 return file_has_perm(cred, file, FILE__LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612}
3613
3614static int selinux_file_fcntl(struct file *file, unsigned int cmd,
3615 unsigned long arg)
3616{
David Howells88e67f32008-11-14 10:39:21 +11003617 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 int err = 0;
3619
3620 switch (cmd) {
Eric Paris828dfe12008-04-17 13:17:49 -04003621 case F_SETFL:
Eric Paris828dfe12008-04-17 13:17:49 -04003622 if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
David Howells88e67f32008-11-14 10:39:21 +11003623 err = file_has_perm(cred, file, FILE__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003625 }
3626 /* fall through */
3627 case F_SETOWN:
3628 case F_SETSIG:
3629 case F_GETFL:
3630 case F_GETOWN:
3631 case F_GETSIG:
Cyrill Gorcunov1d151c32012-07-30 14:43:00 -07003632 case F_GETOWNER_UIDS:
Eric Paris828dfe12008-04-17 13:17:49 -04003633 /* Just check FD__USE permission */
David Howells88e67f32008-11-14 10:39:21 +11003634 err = file_has_perm(cred, file, 0);
Eric Paris828dfe12008-04-17 13:17:49 -04003635 break;
3636 case F_GETLK:
3637 case F_SETLK:
3638 case F_SETLKW:
Jeff Layton0d3f7a22014-04-22 08:23:58 -04003639 case F_OFD_GETLK:
3640 case F_OFD_SETLK:
3641 case F_OFD_SETLKW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642#if BITS_PER_LONG == 32
Eric Paris828dfe12008-04-17 13:17:49 -04003643 case F_GETLK64:
3644 case F_SETLK64:
3645 case F_SETLKW64:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646#endif
David Howells88e67f32008-11-14 10:39:21 +11003647 err = file_has_perm(cred, file, FILE__LOCK);
Eric Paris828dfe12008-04-17 13:17:49 -04003648 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 }
3650
3651 return err;
3652}
3653
Jeff Laytone0b93ed2014-08-22 11:27:32 -04003654static void selinux_file_set_fowner(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 struct file_security_struct *fsec;
3657
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 fsec = file->f_security;
David Howells275bb412008-11-14 10:39:19 +11003659 fsec->fown_sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660}
3661
3662static int selinux_file_send_sigiotask(struct task_struct *tsk,
3663 struct fown_struct *fown, int signum)
3664{
Eric Paris828dfe12008-04-17 13:17:49 -04003665 struct file *file;
Stephen Smalley65c90bc2009-05-04 15:43:18 -04003666 u32 sid = task_sid(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 struct file_security_struct *fsec;
3669
3670 /* struct fown_struct is never outside the context of a struct file */
Eric Paris828dfe12008-04-17 13:17:49 -04003671 file = container_of(fown, struct file, f_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 fsec = file->f_security;
3674
3675 if (!signum)
3676 perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
3677 else
3678 perm = signal_to_av(signum);
3679
David Howells275bb412008-11-14 10:39:19 +11003680 return avc_has_perm(fsec->fown_sid, sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 SECCLASS_PROCESS, perm, NULL);
3682}
3683
3684static int selinux_file_receive(struct file *file)
3685{
David Howells88e67f32008-11-14 10:39:21 +11003686 const struct cred *cred = current_cred();
3687
3688 return file_has_perm(cred, file, file_to_av(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689}
3690
Eric Paris83d49852012-04-04 13:45:40 -04003691static int selinux_file_open(struct file *file, const struct cred *cred)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003692{
3693 struct file_security_struct *fsec;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003694 struct inode_security_struct *isec;
David Howellsd84f4f92008-11-14 10:39:23 +11003695
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003696 fsec = file->f_security;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05003697 isec = inode_security(file_inode(file));
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003698 /*
3699 * Save inode label and policy sequence number
3700 * at open-time so that selinux_file_permission
3701 * can determine whether revalidation is necessary.
3702 * Task label is already saved in the file security
3703 * struct as its SID.
3704 */
3705 fsec->isid = isec->sid;
3706 fsec->pseqno = avc_policy_seqno();
3707 /*
3708 * Since the inode label or policy seqno may have changed
3709 * between the selinux_inode_permission check and the saving
3710 * of state above, recheck that access is still permitted.
3711 * Otherwise, access might never be revalidated against the
3712 * new inode label or new policy.
3713 * This check is not redundant - do not remove.
3714 */
David Howells13f8e982013-06-13 23:37:55 +01003715 return file_path_has_perm(cred, file, open_file_to_av(file));
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003716}
3717
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718/* task security operations */
3719
3720static int selinux_task_create(unsigned long clone_flags)
3721{
David Howells3b11a1d2008-11-14 10:39:26 +11003722 return current_has_perm(current, PROCESS__FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723}
3724
David Howellsf1752ee2008-11-14 10:39:17 +11003725/*
David Howellsee18d642009-09-02 09:14:21 +01003726 * allocate the SELinux part of blank credentials
3727 */
3728static int selinux_cred_alloc_blank(struct cred *cred, gfp_t gfp)
3729{
3730 struct task_security_struct *tsec;
3731
3732 tsec = kzalloc(sizeof(struct task_security_struct), gfp);
3733 if (!tsec)
3734 return -ENOMEM;
3735
3736 cred->security = tsec;
3737 return 0;
3738}
3739
3740/*
David Howellsf1752ee2008-11-14 10:39:17 +11003741 * detach and free the LSM part of a set of credentials
3742 */
3743static void selinux_cred_free(struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744{
David Howellsf1752ee2008-11-14 10:39:17 +11003745 struct task_security_struct *tsec = cred->security;
David Howellse0e81732009-09-02 09:13:40 +01003746
Tetsuo Handa2edeaa32011-02-07 13:36:10 +00003747 /*
3748 * cred->security == NULL if security_cred_alloc_blank() or
3749 * security_prepare_creds() returned an error.
3750 */
3751 BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE);
David Howellse0e81732009-09-02 09:13:40 +01003752 cred->security = (void *) 0x7UL;
David Howellsf1752ee2008-11-14 10:39:17 +11003753 kfree(tsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754}
3755
David Howellsd84f4f92008-11-14 10:39:23 +11003756/*
3757 * prepare a new set of credentials for modification
3758 */
3759static int selinux_cred_prepare(struct cred *new, const struct cred *old,
3760 gfp_t gfp)
3761{
3762 const struct task_security_struct *old_tsec;
3763 struct task_security_struct *tsec;
3764
3765 old_tsec = old->security;
3766
3767 tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp);
3768 if (!tsec)
3769 return -ENOMEM;
3770
3771 new->security = tsec;
3772 return 0;
3773}
3774
3775/*
David Howellsee18d642009-09-02 09:14:21 +01003776 * transfer the SELinux data to a blank set of creds
3777 */
3778static void selinux_cred_transfer(struct cred *new, const struct cred *old)
3779{
3780 const struct task_security_struct *old_tsec = old->security;
3781 struct task_security_struct *tsec = new->security;
3782
3783 *tsec = *old_tsec;
3784}
3785
3786/*
David Howells3a3b7ce2008-11-14 10:39:28 +11003787 * set the security data for a kernel service
3788 * - all the creation contexts are set to unlabelled
3789 */
3790static int selinux_kernel_act_as(struct cred *new, u32 secid)
3791{
3792 struct task_security_struct *tsec = new->security;
3793 u32 sid = current_sid();
3794 int ret;
3795
3796 ret = avc_has_perm(sid, secid,
3797 SECCLASS_KERNEL_SERVICE,
3798 KERNEL_SERVICE__USE_AS_OVERRIDE,
3799 NULL);
3800 if (ret == 0) {
3801 tsec->sid = secid;
3802 tsec->create_sid = 0;
3803 tsec->keycreate_sid = 0;
3804 tsec->sockcreate_sid = 0;
3805 }
3806 return ret;
3807}
3808
3809/*
3810 * set the file creation context in a security record to the same as the
3811 * objective context of the specified inode
3812 */
3813static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
3814{
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05003815 struct inode_security_struct *isec = inode_security(inode);
David Howells3a3b7ce2008-11-14 10:39:28 +11003816 struct task_security_struct *tsec = new->security;
3817 u32 sid = current_sid();
3818 int ret;
3819
3820 ret = avc_has_perm(sid, isec->sid,
3821 SECCLASS_KERNEL_SERVICE,
3822 KERNEL_SERVICE__CREATE_FILES_AS,
3823 NULL);
3824
3825 if (ret == 0)
3826 tsec->create_sid = isec->sid;
David Howellsef574712010-02-26 01:56:16 +00003827 return ret;
David Howells3a3b7ce2008-11-14 10:39:28 +11003828}
3829
Eric Parisdd8dbf22009-11-03 16:35:32 +11003830static int selinux_kernel_module_request(char *kmod_name)
Eric Paris25354c42009-08-13 09:45:03 -04003831{
Eric Parisdd8dbf22009-11-03 16:35:32 +11003832 u32 sid;
3833 struct common_audit_data ad;
3834
3835 sid = task_sid(current);
3836
Eric Paris50c205f2012-04-04 15:01:43 -04003837 ad.type = LSM_AUDIT_DATA_KMOD;
Eric Parisdd8dbf22009-11-03 16:35:32 +11003838 ad.u.kmod_name = kmod_name;
3839
3840 return avc_has_perm(sid, SECINITSID_KERNEL, SECCLASS_SYSTEM,
3841 SYSTEM__MODULE_REQUEST, &ad);
Eric Paris25354c42009-08-13 09:45:03 -04003842}
3843
Jeff Vander Stoep61d612e2016-04-05 13:06:27 -07003844static int selinux_kernel_module_from_file(struct file *file)
3845{
3846 struct common_audit_data ad;
3847 struct inode_security_struct *isec;
3848 struct file_security_struct *fsec;
3849 u32 sid = current_sid();
3850 int rc;
3851
3852 /* init_module */
3853 if (file == NULL)
3854 return avc_has_perm(sid, sid, SECCLASS_SYSTEM,
3855 SYSTEM__MODULE_LOAD, NULL);
3856
3857 /* finit_module */
Paul Moore20cdef82016-04-04 14:14:42 -04003858
Vivek Goyal43af5de2016-09-09 11:37:49 -04003859 ad.type = LSM_AUDIT_DATA_FILE;
3860 ad.u.file = file;
Jeff Vander Stoep61d612e2016-04-05 13:06:27 -07003861
Jeff Vander Stoep61d612e2016-04-05 13:06:27 -07003862 fsec = file->f_security;
Jeff Vander Stoep61d612e2016-04-05 13:06:27 -07003863 if (sid != fsec->sid) {
3864 rc = avc_has_perm(sid, fsec->sid, SECCLASS_FD, FD__USE, &ad);
3865 if (rc)
3866 return rc;
3867 }
3868
Paul Moore20cdef82016-04-04 14:14:42 -04003869 isec = inode_security(file_inode(file));
Jeff Vander Stoep61d612e2016-04-05 13:06:27 -07003870 return avc_has_perm(sid, isec->sid, SECCLASS_SYSTEM,
3871 SYSTEM__MODULE_LOAD, &ad);
3872}
3873
3874static int selinux_kernel_read_file(struct file *file,
3875 enum kernel_read_file_id id)
3876{
3877 int rc = 0;
3878
3879 switch (id) {
3880 case READING_MODULE:
3881 rc = selinux_kernel_module_from_file(file);
3882 break;
3883 default:
3884 break;
3885 }
3886
3887 return rc;
3888}
3889
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
3891{
David Howells3b11a1d2008-11-14 10:39:26 +11003892 return current_has_perm(p, PROCESS__SETPGID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893}
3894
3895static int selinux_task_getpgid(struct task_struct *p)
3896{
David Howells3b11a1d2008-11-14 10:39:26 +11003897 return current_has_perm(p, PROCESS__GETPGID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898}
3899
3900static int selinux_task_getsid(struct task_struct *p)
3901{
David Howells3b11a1d2008-11-14 10:39:26 +11003902 return current_has_perm(p, PROCESS__GETSESSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903}
3904
David Quigleyf9008e42006-06-30 01:55:46 -07003905static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
3906{
David Howells275bb412008-11-14 10:39:19 +11003907 *secid = task_sid(p);
David Quigleyf9008e42006-06-30 01:55:46 -07003908}
3909
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910static int selinux_task_setnice(struct task_struct *p, int nice)
3911{
David Howells3b11a1d2008-11-14 10:39:26 +11003912 return current_has_perm(p, PROCESS__SETSCHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913}
3914
James Morris03e68062006-06-23 02:03:58 -07003915static int selinux_task_setioprio(struct task_struct *p, int ioprio)
3916{
David Howells3b11a1d2008-11-14 10:39:26 +11003917 return current_has_perm(p, PROCESS__SETSCHED);
James Morris03e68062006-06-23 02:03:58 -07003918}
3919
David Quigleya1836a42006-06-30 01:55:49 -07003920static int selinux_task_getioprio(struct task_struct *p)
3921{
David Howells3b11a1d2008-11-14 10:39:26 +11003922 return current_has_perm(p, PROCESS__GETSCHED);
David Quigleya1836a42006-06-30 01:55:49 -07003923}
3924
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003925static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
3926 struct rlimit *new_rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927{
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003928 struct rlimit *old_rlim = p->signal->rlim + resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929
3930 /* Control the ability to change the hard limit (whether
3931 lowering or raising it), so that the hard limit can
3932 later be used as a safe reset point for the soft limit
David Howellsd84f4f92008-11-14 10:39:23 +11003933 upon context transitions. See selinux_bprm_committing_creds. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 if (old_rlim->rlim_max != new_rlim->rlim_max)
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003935 return current_has_perm(p, PROCESS__SETRLIMIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936
3937 return 0;
3938}
3939
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09003940static int selinux_task_setscheduler(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941{
David Howells3b11a1d2008-11-14 10:39:26 +11003942 return current_has_perm(p, PROCESS__SETSCHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943}
3944
3945static int selinux_task_getscheduler(struct task_struct *p)
3946{
David Howells3b11a1d2008-11-14 10:39:26 +11003947 return current_has_perm(p, PROCESS__GETSCHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948}
3949
David Quigley35601542006-06-23 02:04:01 -07003950static int selinux_task_movememory(struct task_struct *p)
3951{
David Howells3b11a1d2008-11-14 10:39:26 +11003952 return current_has_perm(p, PROCESS__SETSCHED);
David Quigley35601542006-06-23 02:04:01 -07003953}
3954
David Quigleyf9008e42006-06-30 01:55:46 -07003955static int selinux_task_kill(struct task_struct *p, struct siginfo *info,
3956 int sig, u32 secid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957{
3958 u32 perm;
3959 int rc;
3960
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 if (!sig)
3962 perm = PROCESS__SIGNULL; /* null signal; existence test */
3963 else
3964 perm = signal_to_av(sig);
David Quigleyf9008e42006-06-30 01:55:46 -07003965 if (secid)
David Howells275bb412008-11-14 10:39:19 +11003966 rc = avc_has_perm(secid, task_sid(p),
3967 SECCLASS_PROCESS, perm, NULL);
David Quigleyf9008e42006-06-30 01:55:46 -07003968 else
David Howells3b11a1d2008-11-14 10:39:26 +11003969 rc = current_has_perm(p, perm);
David Quigleyf9008e42006-06-30 01:55:46 -07003970 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971}
3972
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973static int selinux_task_wait(struct task_struct *p)
3974{
Eric Paris8a535142007-10-22 16:10:31 -04003975 return task_has_perm(p, current, PROCESS__SIGCHLD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976}
3977
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978static void selinux_task_to_inode(struct task_struct *p,
3979 struct inode *inode)
3980{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 struct inode_security_struct *isec = inode->i_security;
David Howells275bb412008-11-14 10:39:19 +11003982 u32 sid = task_sid(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983
David Howells275bb412008-11-14 10:39:19 +11003984 isec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05003985 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986}
3987
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003989static int selinux_parse_skb_ipv4(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04003990 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991{
3992 int offset, ihlen, ret = -EINVAL;
3993 struct iphdr _iph, *ih;
3994
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03003995 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
3997 if (ih == NULL)
3998 goto out;
3999
4000 ihlen = ih->ihl * 4;
4001 if (ihlen < sizeof(_iph))
4002 goto out;
4003
Eric Paris48c62af2012-04-02 13:15:44 -04004004 ad->u.net->v4info.saddr = ih->saddr;
4005 ad->u.net->v4info.daddr = ih->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 ret = 0;
4007
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004008 if (proto)
4009 *proto = ih->protocol;
4010
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 switch (ih->protocol) {
Eric Paris828dfe12008-04-17 13:17:49 -04004012 case IPPROTO_TCP: {
4013 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014
Eric Paris828dfe12008-04-17 13:17:49 -04004015 if (ntohs(ih->frag_off) & IP_OFFSET)
4016 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017
4018 offset += ihlen;
4019 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4020 if (th == NULL)
4021 break;
4022
Eric Paris48c62af2012-04-02 13:15:44 -04004023 ad->u.net->sport = th->source;
4024 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004026 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027
Eric Paris828dfe12008-04-17 13:17:49 -04004028 case IPPROTO_UDP: {
4029 struct udphdr _udph, *uh;
4030
4031 if (ntohs(ih->frag_off) & IP_OFFSET)
4032 break;
4033
4034 offset += ihlen;
4035 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4036 if (uh == NULL)
4037 break;
4038
Eric Paris48c62af2012-04-02 13:15:44 -04004039 ad->u.net->sport = uh->source;
4040 ad->u.net->dport = uh->dest;
Eric Paris828dfe12008-04-17 13:17:49 -04004041 break;
4042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043
James Morris2ee92d42006-11-13 16:09:01 -08004044 case IPPROTO_DCCP: {
4045 struct dccp_hdr _dccph, *dh;
4046
4047 if (ntohs(ih->frag_off) & IP_OFFSET)
4048 break;
4049
4050 offset += ihlen;
4051 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4052 if (dh == NULL)
4053 break;
4054
Eric Paris48c62af2012-04-02 13:15:44 -04004055 ad->u.net->sport = dh->dccph_sport;
4056 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08004057 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004058 }
James Morris2ee92d42006-11-13 16:09:01 -08004059
Eric Paris828dfe12008-04-17 13:17:49 -04004060 default:
4061 break;
4062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063out:
4064 return ret;
4065}
4066
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04004067#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068
4069/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004070static int selinux_parse_skb_ipv6(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04004071 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072{
4073 u8 nexthdr;
4074 int ret = -EINVAL, offset;
4075 struct ipv6hdr _ipv6h, *ip6;
Jesse Gross75f28112011-11-30 17:05:51 -08004076 __be16 frag_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03004078 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
4080 if (ip6 == NULL)
4081 goto out;
4082
Eric Paris48c62af2012-04-02 13:15:44 -04004083 ad->u.net->v6info.saddr = ip6->saddr;
4084 ad->u.net->v6info.daddr = ip6->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 ret = 0;
4086
4087 nexthdr = ip6->nexthdr;
4088 offset += sizeof(_ipv6h);
Jesse Gross75f28112011-11-30 17:05:51 -08004089 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090 if (offset < 0)
4091 goto out;
4092
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004093 if (proto)
4094 *proto = nexthdr;
4095
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 switch (nexthdr) {
4097 case IPPROTO_TCP: {
Eric Paris828dfe12008-04-17 13:17:49 -04004098 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099
4100 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4101 if (th == NULL)
4102 break;
4103
Eric Paris48c62af2012-04-02 13:15:44 -04004104 ad->u.net->sport = th->source;
4105 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 break;
4107 }
4108
4109 case IPPROTO_UDP: {
4110 struct udphdr _udph, *uh;
4111
4112 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4113 if (uh == NULL)
4114 break;
4115
Eric Paris48c62af2012-04-02 13:15:44 -04004116 ad->u.net->sport = uh->source;
4117 ad->u.net->dport = uh->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 break;
4119 }
4120
James Morris2ee92d42006-11-13 16:09:01 -08004121 case IPPROTO_DCCP: {
4122 struct dccp_hdr _dccph, *dh;
4123
4124 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4125 if (dh == NULL)
4126 break;
4127
Eric Paris48c62af2012-04-02 13:15:44 -04004128 ad->u.net->sport = dh->dccph_sport;
4129 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08004130 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004131 }
James Morris2ee92d42006-11-13 16:09:01 -08004132
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 /* includes fragments */
4134 default:
4135 break;
4136 }
4137out:
4138 return ret;
4139}
4140
4141#endif /* IPV6 */
4142
Thomas Liu2bf49692009-07-14 12:14:09 -04004143static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
David Howellscf9481e2008-07-27 21:31:07 +10004144 char **_addrp, int src, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145{
David Howellscf9481e2008-07-27 21:31:07 +10004146 char *addrp;
4147 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148
Eric Paris48c62af2012-04-02 13:15:44 -04004149 switch (ad->u.net->family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 case PF_INET:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004151 ret = selinux_parse_skb_ipv4(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10004152 if (ret)
4153 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04004154 addrp = (char *)(src ? &ad->u.net->v4info.saddr :
4155 &ad->u.net->v4info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10004156 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04004158#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 case PF_INET6:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004160 ret = selinux_parse_skb_ipv6(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10004161 if (ret)
4162 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04004163 addrp = (char *)(src ? &ad->u.net->v6info.saddr :
4164 &ad->u.net->v6info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10004165 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166#endif /* IPV6 */
4167 default:
David Howellscf9481e2008-07-27 21:31:07 +10004168 addrp = NULL;
4169 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 }
4171
David Howellscf9481e2008-07-27 21:31:07 +10004172parse_error:
4173 printk(KERN_WARNING
4174 "SELinux: failure in selinux_parse_skb(),"
4175 " unable to parse packet\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 return ret;
David Howellscf9481e2008-07-27 21:31:07 +10004177
4178okay:
4179 if (_addrp)
4180 *_addrp = addrp;
4181 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182}
4183
Paul Moore4f6a9932007-03-01 14:35:22 -05004184/**
Paul Moore220deb92008-01-29 08:38:23 -05004185 * selinux_skb_peerlbl_sid - Determine the peer label of a packet
Paul Moore4f6a9932007-03-01 14:35:22 -05004186 * @skb: the packet
Paul Moore75e22912008-01-29 08:38:04 -05004187 * @family: protocol family
Paul Moore220deb92008-01-29 08:38:23 -05004188 * @sid: the packet's peer label SID
Paul Moore4f6a9932007-03-01 14:35:22 -05004189 *
4190 * Description:
Paul Moore220deb92008-01-29 08:38:23 -05004191 * Check the various different forms of network peer labeling and determine
4192 * the peer label/SID for the packet; most of the magic actually occurs in
4193 * the security server function security_net_peersid_cmp(). The function
4194 * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
4195 * or -EACCES if @sid is invalid due to inconsistencies with the different
4196 * peer labels.
Paul Moore4f6a9932007-03-01 14:35:22 -05004197 *
4198 */
Paul Moore220deb92008-01-29 08:38:23 -05004199static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
Paul Moore4f6a9932007-03-01 14:35:22 -05004200{
Paul Moore71f1cb02008-01-29 08:51:16 -05004201 int err;
Paul Moore4f6a9932007-03-01 14:35:22 -05004202 u32 xfrm_sid;
4203 u32 nlbl_sid;
Paul Moore220deb92008-01-29 08:38:23 -05004204 u32 nlbl_type;
Paul Moore4f6a9932007-03-01 14:35:22 -05004205
Paul Moore817eff72013-12-10 14:57:54 -05004206 err = selinux_xfrm_skb_sid(skb, &xfrm_sid);
Paul Moorebed4d7e2013-07-23 17:38:40 -04004207 if (unlikely(err))
4208 return -EACCES;
4209 err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
4210 if (unlikely(err))
4211 return -EACCES;
Paul Moore220deb92008-01-29 08:38:23 -05004212
Paul Moore71f1cb02008-01-29 08:51:16 -05004213 err = security_net_peersid_resolve(nlbl_sid, nlbl_type, xfrm_sid, sid);
4214 if (unlikely(err)) {
4215 printk(KERN_WARNING
4216 "SELinux: failure in selinux_skb_peerlbl_sid(),"
4217 " unable to determine packet's peer label\n");
Paul Moore220deb92008-01-29 08:38:23 -05004218 return -EACCES;
Paul Moore71f1cb02008-01-29 08:51:16 -05004219 }
Paul Moore220deb92008-01-29 08:38:23 -05004220
4221 return 0;
Paul Moore4f6a9932007-03-01 14:35:22 -05004222}
4223
Paul Moore446b8022013-12-04 16:10:51 -05004224/**
4225 * selinux_conn_sid - Determine the child socket label for a connection
4226 * @sk_sid: the parent socket's SID
4227 * @skb_sid: the packet's SID
4228 * @conn_sid: the resulting connection SID
4229 *
4230 * If @skb_sid is valid then the user:role:type information from @sk_sid is
4231 * combined with the MLS information from @skb_sid in order to create
4232 * @conn_sid. If @skb_sid is not valid then then @conn_sid is simply a copy
4233 * of @sk_sid. Returns zero on success, negative values on failure.
4234 *
4235 */
4236static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid)
4237{
4238 int err = 0;
4239
4240 if (skb_sid != SECSID_NULL)
4241 err = security_sid_mls_copy(sk_sid, skb_sid, conn_sid);
4242 else
4243 *conn_sid = sk_sid;
4244
4245 return err;
4246}
4247
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248/* socket security operations */
Paul Moored4f2d972010-04-22 14:46:18 -04004249
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004250static int socket_sockcreate_sid(const struct task_security_struct *tsec,
4251 u16 secclass, u32 *socksid)
Paul Moored4f2d972010-04-22 14:46:18 -04004252{
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004253 if (tsec->sockcreate_sid > SECSID_NULL) {
4254 *socksid = tsec->sockcreate_sid;
4255 return 0;
4256 }
4257
4258 return security_transition_sid(tsec->sid, tsec->sid, secclass, NULL,
4259 socksid);
Paul Moored4f2d972010-04-22 14:46:18 -04004260}
4261
Paul Moore253bfae2010-04-22 14:46:19 -04004262static int sock_has_perm(struct task_struct *task, struct sock *sk, u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263{
Paul Moore253bfae2010-04-22 14:46:19 -04004264 struct sk_security_struct *sksec = sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004265 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004266 struct lsm_network_audit net = {0,};
Paul Moore253bfae2010-04-22 14:46:19 -04004267 u32 tsid = task_sid(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268
Paul Moore253bfae2010-04-22 14:46:19 -04004269 if (sksec->sid == SECINITSID_KERNEL)
4270 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271
Eric Paris50c205f2012-04-04 15:01:43 -04004272 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004273 ad.u.net = &net;
4274 ad.u.net->sk = sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275
Paul Moore253bfae2010-04-22 14:46:19 -04004276 return avc_has_perm(tsid, sksec->sid, sksec->sclass, perms, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277}
4278
4279static int selinux_socket_create(int family, int type,
4280 int protocol, int kern)
4281{
Paul Moore5fb49872010-04-22 14:46:19 -04004282 const struct task_security_struct *tsec = current_security();
Paul Moored4f2d972010-04-22 14:46:18 -04004283 u32 newsid;
David Howells275bb412008-11-14 10:39:19 +11004284 u16 secclass;
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004285 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286
4287 if (kern)
Paul Moored4f2d972010-04-22 14:46:18 -04004288 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289
David Howells275bb412008-11-14 10:39:19 +11004290 secclass = socket_type_to_security_class(family, type, protocol);
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004291 rc = socket_sockcreate_sid(tsec, secclass, &newsid);
4292 if (rc)
4293 return rc;
4294
Paul Moored4f2d972010-04-22 14:46:18 -04004295 return avc_has_perm(tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296}
4297
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07004298static int selinux_socket_post_create(struct socket *sock, int family,
4299 int type, int protocol, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300{
Paul Moore5fb49872010-04-22 14:46:19 -04004301 const struct task_security_struct *tsec = current_security();
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05004302 struct inode_security_struct *isec = inode_security_novalidate(SOCK_INODE(sock));
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004303 struct sk_security_struct *sksec;
David Howells275bb412008-11-14 10:39:19 +11004304 int err = 0;
4305
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004306 isec->sclass = socket_type_to_security_class(family, type, protocol);
4307
David Howells275bb412008-11-14 10:39:19 +11004308 if (kern)
4309 isec->sid = SECINITSID_KERNEL;
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004310 else {
4311 err = socket_sockcreate_sid(tsec, isec->sclass, &(isec->sid));
4312 if (err)
4313 return err;
4314 }
David Howells275bb412008-11-14 10:39:19 +11004315
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004316 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004318 if (sock->sk) {
4319 sksec = sock->sk->sk_security;
4320 sksec->sid = isec->sid;
Paul Moore220deb92008-01-29 08:38:23 -05004321 sksec->sclass = isec->sclass;
Paul Moore389fb8002009-03-27 17:10:34 -04004322 err = selinux_netlbl_socket_post_create(sock->sk, family);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004323 }
4324
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07004325 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326}
4327
4328/* Range of port numbers used to automatically bind.
4329 Need to determine whether we should perform a name_bind
4330 permission check between the socket and the port number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331
4332static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
4333{
Paul Moore253bfae2010-04-22 14:46:19 -04004334 struct sock *sk = sock->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 u16 family;
4336 int err;
4337
Paul Moore253bfae2010-04-22 14:46:19 -04004338 err = sock_has_perm(current, sk, SOCKET__BIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 if (err)
4340 goto out;
4341
4342 /*
4343 * If PF_INET or PF_INET6, check name_bind permission for the port.
James Morris13402582005-09-30 14:24:34 -04004344 * Multiple address binding for SCTP is not supported yet: we just
4345 * check the first address now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 */
Paul Moore253bfae2010-04-22 14:46:19 -04004347 family = sk->sk_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 if (family == PF_INET || family == PF_INET6) {
4349 char *addrp;
Paul Moore253bfae2010-04-22 14:46:19 -04004350 struct sk_security_struct *sksec = sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004351 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004352 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 struct sockaddr_in *addr4 = NULL;
4354 struct sockaddr_in6 *addr6 = NULL;
4355 unsigned short snum;
James Morrise399f982008-06-12 01:39:58 +10004356 u32 sid, node_perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358 if (family == PF_INET) {
Alexander Potapenkob243aa82017-03-06 19:46:14 +01004359 if (addrlen < sizeof(struct sockaddr_in)) {
4360 err = -EINVAL;
4361 goto out;
4362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 addr4 = (struct sockaddr_in *)address;
4364 snum = ntohs(addr4->sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 addrp = (char *)&addr4->sin_addr.s_addr;
4366 } else {
Alexander Potapenkob243aa82017-03-06 19:46:14 +01004367 if (addrlen < SIN6_LEN_RFC2133) {
4368 err = -EINVAL;
4369 goto out;
4370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 addr6 = (struct sockaddr_in6 *)address;
4372 snum = ntohs(addr6->sin6_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 addrp = (char *)&addr6->sin6_addr.s6_addr;
4374 }
4375
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004376 if (snum) {
4377 int low, high;
4378
Eric W. Biederman0bbf87d2013-09-28 14:10:59 -07004379 inet_get_local_port_range(sock_net(sk), &low, &high);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004380
4381 if (snum < max(PROT_SOCK, low) || snum > high) {
Paul Moore3e112172008-04-10 10:48:14 -04004382 err = sel_netport_sid(sk->sk_protocol,
4383 snum, &sid);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004384 if (err)
4385 goto out;
Eric Paris50c205f2012-04-04 15:01:43 -04004386 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004387 ad.u.net = &net;
4388 ad.u.net->sport = htons(snum);
4389 ad.u.net->family = family;
Paul Moore253bfae2010-04-22 14:46:19 -04004390 err = avc_has_perm(sksec->sid, sid,
4391 sksec->sclass,
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004392 SOCKET__NAME_BIND, &ad);
4393 if (err)
4394 goto out;
4395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 }
Eric Paris828dfe12008-04-17 13:17:49 -04004397
Paul Moore253bfae2010-04-22 14:46:19 -04004398 switch (sksec->sclass) {
James Morris13402582005-09-30 14:24:34 -04004399 case SECCLASS_TCP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400 node_perm = TCP_SOCKET__NODE_BIND;
4401 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004402
James Morris13402582005-09-30 14:24:34 -04004403 case SECCLASS_UDP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 node_perm = UDP_SOCKET__NODE_BIND;
4405 break;
James Morris2ee92d42006-11-13 16:09:01 -08004406
4407 case SECCLASS_DCCP_SOCKET:
4408 node_perm = DCCP_SOCKET__NODE_BIND;
4409 break;
4410
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 default:
4412 node_perm = RAWIP_SOCKET__NODE_BIND;
4413 break;
4414 }
Eric Paris828dfe12008-04-17 13:17:49 -04004415
Paul Moore224dfbd2008-01-29 08:38:13 -05004416 err = sel_netnode_sid(addrp, family, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 if (err)
4418 goto out;
Eric Paris828dfe12008-04-17 13:17:49 -04004419
Eric Paris50c205f2012-04-04 15:01:43 -04004420 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004421 ad.u.net = &net;
4422 ad.u.net->sport = htons(snum);
4423 ad.u.net->family = family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424
4425 if (family == PF_INET)
Eric Paris48c62af2012-04-02 13:15:44 -04004426 ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 else
Eric Paris48c62af2012-04-02 13:15:44 -04004428 ad.u.net->v6info.saddr = addr6->sin6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429
Paul Moore253bfae2010-04-22 14:46:19 -04004430 err = avc_has_perm(sksec->sid, sid,
4431 sksec->sclass, node_perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 if (err)
4433 goto out;
4434 }
4435out:
4436 return err;
4437}
4438
4439static int selinux_socket_connect(struct socket *sock, struct sockaddr *address, int addrlen)
4440{
Paul Moore014ab192008-10-10 10:16:33 -04004441 struct sock *sk = sock->sk;
Paul Moore253bfae2010-04-22 14:46:19 -04004442 struct sk_security_struct *sksec = sk->sk_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 int err;
4444
Paul Moore253bfae2010-04-22 14:46:19 -04004445 err = sock_has_perm(current, sk, SOCKET__CONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446 if (err)
4447 return err;
4448
4449 /*
James Morris2ee92d42006-11-13 16:09:01 -08004450 * If a TCP or DCCP socket, check name_connect permission for the port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 */
Paul Moore253bfae2010-04-22 14:46:19 -04004452 if (sksec->sclass == SECCLASS_TCP_SOCKET ||
4453 sksec->sclass == SECCLASS_DCCP_SOCKET) {
Thomas Liu2bf49692009-07-14 12:14:09 -04004454 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004455 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456 struct sockaddr_in *addr4 = NULL;
4457 struct sockaddr_in6 *addr6 = NULL;
4458 unsigned short snum;
James Morris2ee92d42006-11-13 16:09:01 -08004459 u32 sid, perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460
4461 if (sk->sk_family == PF_INET) {
4462 addr4 = (struct sockaddr_in *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004463 if (addrlen < sizeof(struct sockaddr_in))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464 return -EINVAL;
4465 snum = ntohs(addr4->sin_port);
4466 } else {
4467 addr6 = (struct sockaddr_in6 *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004468 if (addrlen < SIN6_LEN_RFC2133)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 return -EINVAL;
4470 snum = ntohs(addr6->sin6_port);
4471 }
4472
Paul Moore3e112172008-04-10 10:48:14 -04004473 err = sel_netport_sid(sk->sk_protocol, snum, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 if (err)
4475 goto out;
4476
Paul Moore253bfae2010-04-22 14:46:19 -04004477 perm = (sksec->sclass == SECCLASS_TCP_SOCKET) ?
James Morris2ee92d42006-11-13 16:09:01 -08004478 TCP_SOCKET__NAME_CONNECT : DCCP_SOCKET__NAME_CONNECT;
4479
Eric Paris50c205f2012-04-04 15:01:43 -04004480 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004481 ad.u.net = &net;
4482 ad.u.net->dport = htons(snum);
4483 ad.u.net->family = sk->sk_family;
Paul Moore253bfae2010-04-22 14:46:19 -04004484 err = avc_has_perm(sksec->sid, sid, sksec->sclass, perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 if (err)
4486 goto out;
4487 }
4488
Paul Moore014ab192008-10-10 10:16:33 -04004489 err = selinux_netlbl_socket_connect(sk, address);
4490
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491out:
4492 return err;
4493}
4494
4495static int selinux_socket_listen(struct socket *sock, int backlog)
4496{
Paul Moore253bfae2010-04-22 14:46:19 -04004497 return sock_has_perm(current, sock->sk, SOCKET__LISTEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498}
4499
4500static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
4501{
4502 int err;
4503 struct inode_security_struct *isec;
4504 struct inode_security_struct *newisec;
4505
Paul Moore253bfae2010-04-22 14:46:19 -04004506 err = sock_has_perm(current, sock->sk, SOCKET__ACCEPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 if (err)
4508 return err;
4509
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05004510 newisec = inode_security_novalidate(SOCK_INODE(newsock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05004512 isec = inode_security_novalidate(SOCK_INODE(sock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 newisec->sclass = isec->sclass;
4514 newisec->sid = isec->sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004515 newisec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516
4517 return 0;
4518}
4519
4520static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
Eric Paris828dfe12008-04-17 13:17:49 -04004521 int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522{
Paul Moore253bfae2010-04-22 14:46:19 -04004523 return sock_has_perm(current, sock->sk, SOCKET__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524}
4525
4526static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
4527 int size, int flags)
4528{
Paul Moore253bfae2010-04-22 14:46:19 -04004529 return sock_has_perm(current, sock->sk, SOCKET__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530}
4531
4532static int selinux_socket_getsockname(struct socket *sock)
4533{
Paul Moore253bfae2010-04-22 14:46:19 -04004534 return sock_has_perm(current, sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535}
4536
4537static int selinux_socket_getpeername(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
Eric Paris828dfe12008-04-17 13:17:49 -04004542static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543{
Paul Mooref8687af2006-10-30 15:22:15 -08004544 int err;
4545
Paul Moore253bfae2010-04-22 14:46:19 -04004546 err = sock_has_perm(current, sock->sk, SOCKET__SETOPT);
Paul Mooref8687af2006-10-30 15:22:15 -08004547 if (err)
4548 return err;
4549
4550 return selinux_netlbl_socket_setsockopt(sock, level, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551}
4552
4553static int selinux_socket_getsockopt(struct socket *sock, int level,
4554 int optname)
4555{
Paul Moore253bfae2010-04-22 14:46:19 -04004556 return sock_has_perm(current, sock->sk, SOCKET__GETOPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557}
4558
4559static int selinux_socket_shutdown(struct socket *sock, int how)
4560{
Paul Moore253bfae2010-04-22 14:46:19 -04004561 return sock_has_perm(current, sock->sk, SOCKET__SHUTDOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562}
4563
David S. Miller3610cda2011-01-05 15:38:53 -08004564static int selinux_socket_unix_stream_connect(struct sock *sock,
4565 struct sock *other,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 struct sock *newsk)
4567{
David S. Miller3610cda2011-01-05 15:38:53 -08004568 struct sk_security_struct *sksec_sock = sock->sk_security;
4569 struct sk_security_struct *sksec_other = other->sk_security;
Paul Moore4d1e2452010-04-22 14:46:18 -04004570 struct sk_security_struct *sksec_new = newsk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004571 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004572 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573 int err;
4574
Eric Paris50c205f2012-04-04 15:01:43 -04004575 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004576 ad.u.net = &net;
4577 ad.u.net->sk = other;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578
Paul Moore4d1e2452010-04-22 14:46:18 -04004579 err = avc_has_perm(sksec_sock->sid, sksec_other->sid,
4580 sksec_other->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 UNIX_STREAM_SOCKET__CONNECTTO, &ad);
4582 if (err)
4583 return err;
4584
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 /* server child socket */
Paul Moore4d1e2452010-04-22 14:46:18 -04004586 sksec_new->peer_sid = sksec_sock->sid;
4587 err = security_sid_mls_copy(sksec_other->sid, sksec_sock->sid,
4588 &sksec_new->sid);
4589 if (err)
4590 return err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004591
Paul Moore4d1e2452010-04-22 14:46:18 -04004592 /* connecting socket */
4593 sksec_sock->peer_sid = sksec_new->sid;
4594
4595 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596}
4597
4598static int selinux_socket_unix_may_send(struct socket *sock,
4599 struct socket *other)
4600{
Paul Moore253bfae2010-04-22 14:46:19 -04004601 struct sk_security_struct *ssec = sock->sk->sk_security;
4602 struct sk_security_struct *osec = other->sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004603 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004604 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605
Eric Paris50c205f2012-04-04 15:01:43 -04004606 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004607 ad.u.net = &net;
4608 ad.u.net->sk = other->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609
Paul Moore253bfae2010-04-22 14:46:19 -04004610 return avc_has_perm(ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
4611 &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612}
4613
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004614static int selinux_inet_sys_rcv_skb(struct net *ns, int ifindex,
4615 char *addrp, u16 family, u32 peer_sid,
Thomas Liu2bf49692009-07-14 12:14:09 -04004616 struct common_audit_data *ad)
Paul Mooreeffad8d2008-01-29 08:49:27 -05004617{
4618 int err;
4619 u32 if_sid;
4620 u32 node_sid;
4621
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004622 err = sel_netif_sid(ns, ifindex, &if_sid);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004623 if (err)
4624 return err;
4625 err = avc_has_perm(peer_sid, if_sid,
4626 SECCLASS_NETIF, NETIF__INGRESS, ad);
4627 if (err)
4628 return err;
4629
4630 err = sel_netnode_sid(addrp, family, &node_sid);
4631 if (err)
4632 return err;
4633 return avc_has_perm(peer_sid, node_sid,
4634 SECCLASS_NODE, NODE__RECVFROM, ad);
4635}
4636
Paul Moore220deb92008-01-29 08:38:23 -05004637static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
Paul Moored8395c82008-10-10 10:16:30 -04004638 u16 family)
Paul Moore220deb92008-01-29 08:38:23 -05004639{
Paul Moore277d3422008-12-31 12:54:11 -05004640 int err = 0;
Paul Moore220deb92008-01-29 08:38:23 -05004641 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004642 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004643 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004644 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04004645 char *addrp;
4646
Eric Paris50c205f2012-04-04 15:01:43 -04004647 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004648 ad.u.net = &net;
4649 ad.u.net->netif = skb->skb_iif;
4650 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004651 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4652 if (err)
4653 return err;
Paul Moore220deb92008-01-29 08:38:23 -05004654
Paul Moore58bfbb52009-03-27 17:10:41 -04004655 if (selinux_secmark_enabled()) {
Paul Moore220deb92008-01-29 08:38:23 -05004656 err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Moored8395c82008-10-10 10:16:30 -04004657 PACKET__RECV, &ad);
Paul Moore58bfbb52009-03-27 17:10:41 -04004658 if (err)
4659 return err;
4660 }
Paul Moore220deb92008-01-29 08:38:23 -05004661
Steffen Klassertb9679a72011-02-23 12:55:21 +01004662 err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
4663 if (err)
4664 return err;
4665 err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004666
James Morris4e5ab4c2006-06-09 00:33:33 -07004667 return err;
4668}
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004669
James Morris4e5ab4c2006-06-09 00:33:33 -07004670static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
4671{
Paul Moore220deb92008-01-29 08:38:23 -05004672 int err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004673 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004674 u16 family = sk->sk_family;
4675 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004676 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004677 struct lsm_network_audit net = {0,};
Paul Moore220deb92008-01-29 08:38:23 -05004678 char *addrp;
Paul Moored8395c82008-10-10 10:16:30 -04004679 u8 secmark_active;
4680 u8 peerlbl_active;
James Morris4e5ab4c2006-06-09 00:33:33 -07004681
James Morris4e5ab4c2006-06-09 00:33:33 -07004682 if (family != PF_INET && family != PF_INET6)
Paul Moore220deb92008-01-29 08:38:23 -05004683 return 0;
James Morris4e5ab4c2006-06-09 00:33:33 -07004684
4685 /* Handle mapped IPv4 packets arriving via IPv6 sockets */
Al Viro87fcd702006-12-04 22:00:55 +00004686 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
James Morris4e5ab4c2006-06-09 00:33:33 -07004687 family = PF_INET;
4688
Paul Moored8395c82008-10-10 10:16:30 -04004689 /* If any sort of compatibility mode is enabled then handoff processing
4690 * to the selinux_sock_rcv_skb_compat() function to deal with the
4691 * special handling. We do this in an attempt to keep this function
4692 * as fast and as clean as possible. */
Paul Moore58bfbb52009-03-27 17:10:41 -04004693 if (!selinux_policycap_netpeer)
Paul Moored8395c82008-10-10 10:16:30 -04004694 return selinux_sock_rcv_skb_compat(sk, skb, family);
4695
4696 secmark_active = selinux_secmark_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04004697 peerlbl_active = selinux_peerlbl_enabled();
Paul Moored8395c82008-10-10 10:16:30 -04004698 if (!secmark_active && !peerlbl_active)
4699 return 0;
4700
Eric Paris50c205f2012-04-04 15:01:43 -04004701 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004702 ad.u.net = &net;
4703 ad.u.net->netif = skb->skb_iif;
4704 ad.u.net->family = family;
Paul Moore224dfbd2008-01-29 08:38:13 -05004705 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
James Morris4e5ab4c2006-06-09 00:33:33 -07004706 if (err)
Paul Moore220deb92008-01-29 08:38:23 -05004707 return err;
James Morris4e5ab4c2006-06-09 00:33:33 -07004708
Paul Moored8395c82008-10-10 10:16:30 -04004709 if (peerlbl_active) {
Paul Moored621d352008-01-29 08:43:36 -05004710 u32 peer_sid;
4711
4712 err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
4713 if (err)
4714 return err;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004715 err = selinux_inet_sys_rcv_skb(sock_net(sk), skb->skb_iif,
4716 addrp, family, peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04004717 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04004718 selinux_netlbl_err(skb, family, err, 0);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004719 return err;
Paul Mooredfaebe92008-10-10 10:16:31 -04004720 }
Paul Moored621d352008-01-29 08:43:36 -05004721 err = avc_has_perm(sk_sid, peer_sid, SECCLASS_PEER,
4722 PEER__RECV, &ad);
Chad Hanson46d01d62013-12-23 17:45:01 -05004723 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04004724 selinux_netlbl_err(skb, family, err, 0);
Chad Hanson46d01d62013-12-23 17:45:01 -05004725 return err;
4726 }
Paul Moored621d352008-01-29 08:43:36 -05004727 }
4728
Paul Moored8395c82008-10-10 10:16:30 -04004729 if (secmark_active) {
Paul Mooreeffad8d2008-01-29 08:49:27 -05004730 err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
4731 PACKET__RECV, &ad);
4732 if (err)
4733 return err;
4734 }
4735
Paul Moored621d352008-01-29 08:43:36 -05004736 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737}
4738
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004739static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
4740 int __user *optlen, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741{
4742 int err = 0;
4743 char *scontext;
4744 u32 scontext_len;
Paul Moore253bfae2010-04-22 14:46:19 -04004745 struct sk_security_struct *sksec = sock->sk->sk_security;
Paul Moore3de4bab2006-11-17 17:38:54 -05004746 u32 peer_sid = SECSID_NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747
Paul Moore253bfae2010-04-22 14:46:19 -04004748 if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
4749 sksec->sclass == SECCLASS_TCP_SOCKET)
Eric Parisdd3e7832010-04-07 15:08:46 -04004750 peer_sid = sksec->peer_sid;
Paul Moore253bfae2010-04-22 14:46:19 -04004751 if (peer_sid == SECSID_NULL)
4752 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004754 err = security_sid_to_context(peer_sid, &scontext, &scontext_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 if (err)
Paul Moore253bfae2010-04-22 14:46:19 -04004756 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757
4758 if (scontext_len > len) {
4759 err = -ERANGE;
4760 goto out_len;
4761 }
4762
4763 if (copy_to_user(optval, scontext, scontext_len))
4764 err = -EFAULT;
4765
4766out_len:
4767 if (put_user(scontext_len, optlen))
4768 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 kfree(scontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770 return err;
4771}
4772
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004773static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004774{
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004775 u32 peer_secid = SECSID_NULL;
Paul Moore75e22912008-01-29 08:38:04 -05004776 u16 family;
Paul Moore899134f2016-03-28 15:19:10 -04004777 struct inode_security_struct *isec;
Catherine Zhang877ce7c2006-06-29 12:27:47 -07004778
Paul Mooreaa862902008-10-10 10:16:29 -04004779 if (skb && skb->protocol == htons(ETH_P_IP))
4780 family = PF_INET;
4781 else if (skb && skb->protocol == htons(ETH_P_IPV6))
4782 family = PF_INET6;
4783 else if (sock)
Paul Moore75e22912008-01-29 08:38:04 -05004784 family = sock->sk->sk_family;
Paul Moore75e22912008-01-29 08:38:04 -05004785 else
4786 goto out;
4787
Paul Moore899134f2016-03-28 15:19:10 -04004788 if (sock && family == PF_UNIX) {
4789 isec = inode_security_novalidate(SOCK_INODE(sock));
4790 peer_secid = isec->sid;
4791 } else if (skb)
Paul Moore220deb92008-01-29 08:38:23 -05004792 selinux_skb_peerlbl_sid(skb, family, &peer_secid);
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004793
Paul Moore75e22912008-01-29 08:38:04 -05004794out:
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004795 *secid = peer_secid;
Paul Moore75e22912008-01-29 08:38:04 -05004796 if (peer_secid == SECSID_NULL)
4797 return -EINVAL;
4798 return 0;
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004799}
4800
Al Viro7d877f32005-10-21 03:20:43 -04004801static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802{
Paul Moore84914b72010-04-22 14:46:18 -04004803 struct sk_security_struct *sksec;
4804
4805 sksec = kzalloc(sizeof(*sksec), priority);
4806 if (!sksec)
4807 return -ENOMEM;
4808
4809 sksec->peer_sid = SECINITSID_UNLABELED;
4810 sksec->sid = SECINITSID_UNLABELED;
Stephen Smalley5dee25d2015-07-10 17:19:57 -04004811 sksec->sclass = SECCLASS_SOCKET;
Paul Moore84914b72010-04-22 14:46:18 -04004812 selinux_netlbl_sk_security_reset(sksec);
4813 sk->sk_security = sksec;
4814
4815 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816}
4817
4818static void selinux_sk_free_security(struct sock *sk)
4819{
Paul Moore84914b72010-04-22 14:46:18 -04004820 struct sk_security_struct *sksec = sk->sk_security;
4821
4822 sk->sk_security = NULL;
4823 selinux_netlbl_sk_security_free(sksec);
4824 kfree(sksec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825}
4826
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004827static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
4828{
Eric Parisdd3e7832010-04-07 15:08:46 -04004829 struct sk_security_struct *sksec = sk->sk_security;
4830 struct sk_security_struct *newsksec = newsk->sk_security;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004831
Eric Parisdd3e7832010-04-07 15:08:46 -04004832 newsksec->sid = sksec->sid;
4833 newsksec->peer_sid = sksec->peer_sid;
4834 newsksec->sclass = sksec->sclass;
Paul Moore99f59ed2006-08-29 17:53:48 -07004835
Eric Parisdd3e7832010-04-07 15:08:46 -04004836 selinux_netlbl_sk_security_reset(newsksec);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004837}
4838
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07004839static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004840{
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004841 if (!sk)
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07004842 *secid = SECINITSID_ANY_SOCKET;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004843 else {
4844 struct sk_security_struct *sksec = sk->sk_security;
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004845
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07004846 *secid = sksec->sid;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004847 }
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004848}
4849
Eric Paris828dfe12008-04-17 13:17:49 -04004850static void selinux_sock_graft(struct sock *sk, struct socket *parent)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004851{
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05004852 struct inode_security_struct *isec =
4853 inode_security_novalidate(SOCK_INODE(parent));
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004854 struct sk_security_struct *sksec = sk->sk_security;
4855
Paul Moore2873ead2014-07-28 10:42:48 -04004856 if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
4857 sk->sk_family == PF_UNIX)
David Woodhouse2148ccc2006-09-29 15:50:25 -07004858 isec->sid = sksec->sid;
Paul Moore220deb92008-01-29 08:38:23 -05004859 sksec->sclass = isec->sclass;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004860}
4861
Adrian Bunk9a673e52006-08-15 00:03:53 -07004862static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
4863 struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004864{
4865 struct sk_security_struct *sksec = sk->sk_security;
4866 int err;
Paul Moore0b1f24e2013-12-03 11:39:13 -05004867 u16 family = req->rsk_ops->family;
Paul Moore446b8022013-12-04 16:10:51 -05004868 u32 connsid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004869 u32 peersid;
4870
Paul Mooreaa862902008-10-10 10:16:29 -04004871 err = selinux_skb_peerlbl_sid(skb, family, &peersid);
Paul Moore220deb92008-01-29 08:38:23 -05004872 if (err)
4873 return err;
Paul Moore446b8022013-12-04 16:10:51 -05004874 err = selinux_conn_sid(sksec->sid, peersid, &connsid);
4875 if (err)
4876 return err;
4877 req->secid = connsid;
4878 req->peer_secid = peersid;
Venkat Yekkiralaa51c64f2006-07-27 22:01:34 -07004879
Paul Moore389fb8002009-03-27 17:10:34 -04004880 return selinux_netlbl_inet_conn_request(req, family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004881}
4882
Adrian Bunk9a673e52006-08-15 00:03:53 -07004883static void selinux_inet_csk_clone(struct sock *newsk,
4884 const struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004885{
4886 struct sk_security_struct *newsksec = newsk->sk_security;
4887
4888 newsksec->sid = req->secid;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004889 newsksec->peer_sid = req->peer_secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004890 /* NOTE: Ideally, we should also get the isec->sid for the
4891 new socket in sync, but we don't have the isec available yet.
4892 So we will wait until sock_graft to do it, by which
4893 time it will have been created and available. */
Paul Moore99f59ed2006-08-29 17:53:48 -07004894
Paul Moore9f2ad662006-11-17 17:38:53 -05004895 /* We don't need to take any sort of lock here as we are the only
4896 * thread with access to newsksec */
Paul Moore389fb8002009-03-27 17:10:34 -04004897 selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004898}
4899
Paul Moore014ab192008-10-10 10:16:33 -04004900static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004901{
Paul Mooreaa862902008-10-10 10:16:29 -04004902 u16 family = sk->sk_family;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004903 struct sk_security_struct *sksec = sk->sk_security;
4904
Paul Mooreaa862902008-10-10 10:16:29 -04004905 /* handle mapped IPv4 packets arriving via IPv6 sockets */
4906 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
4907 family = PF_INET;
4908
4909 selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
Venkat Yekkirala6b877692006-11-08 17:04:09 -06004910}
4911
Eric Paris2606fd12010-10-13 16:24:41 -04004912static int selinux_secmark_relabel_packet(u32 sid)
4913{
4914 const struct task_security_struct *__tsec;
4915 u32 tsid;
4916
4917 __tsec = current_security();
4918 tsid = __tsec->sid;
4919
4920 return avc_has_perm(tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO, NULL);
4921}
4922
4923static void selinux_secmark_refcount_inc(void)
4924{
4925 atomic_inc(&selinux_secmark_refcount);
4926}
4927
4928static void selinux_secmark_refcount_dec(void)
4929{
4930 atomic_dec(&selinux_secmark_refcount);
4931}
4932
Adrian Bunk9a673e52006-08-15 00:03:53 -07004933static void selinux_req_classify_flow(const struct request_sock *req,
4934 struct flowi *fl)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004935{
David S. Miller1d28f422011-03-12 00:29:39 -05004936 fl->flowi_secid = req->secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004937}
4938
Paul Moore5dbbaf22013-01-14 07:12:19 +00004939static int selinux_tun_dev_alloc_security(void **security)
4940{
4941 struct tun_security_struct *tunsec;
4942
4943 tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL);
4944 if (!tunsec)
4945 return -ENOMEM;
4946 tunsec->sid = current_sid();
4947
4948 *security = tunsec;
4949 return 0;
4950}
4951
4952static void selinux_tun_dev_free_security(void *security)
4953{
4954 kfree(security);
4955}
4956
Paul Mooreed6d76e2009-08-28 18:12:49 -04004957static int selinux_tun_dev_create(void)
4958{
4959 u32 sid = current_sid();
4960
4961 /* we aren't taking into account the "sockcreate" SID since the socket
4962 * that is being created here is not a socket in the traditional sense,
4963 * instead it is a private sock, accessible only to the kernel, and
4964 * representing a wide range of network traffic spanning multiple
4965 * connections unlike traditional sockets - check the TUN driver to
4966 * get a better understanding of why this socket is special */
4967
4968 return avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
4969 NULL);
4970}
4971
Paul Moore5dbbaf22013-01-14 07:12:19 +00004972static int selinux_tun_dev_attach_queue(void *security)
Paul Mooreed6d76e2009-08-28 18:12:49 -04004973{
Paul Moore5dbbaf22013-01-14 07:12:19 +00004974 struct tun_security_struct *tunsec = security;
4975
4976 return avc_has_perm(current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET,
4977 TUN_SOCKET__ATTACH_QUEUE, NULL);
4978}
4979
4980static int selinux_tun_dev_attach(struct sock *sk, void *security)
4981{
4982 struct tun_security_struct *tunsec = security;
Paul Mooreed6d76e2009-08-28 18:12:49 -04004983 struct sk_security_struct *sksec = sk->sk_security;
4984
4985 /* we don't currently perform any NetLabel based labeling here and it
4986 * isn't clear that we would want to do so anyway; while we could apply
4987 * labeling without the support of the TUN user the resulting labeled
4988 * traffic from the other end of the connection would almost certainly
4989 * cause confusion to the TUN user that had no idea network labeling
4990 * protocols were being used */
4991
Paul Moore5dbbaf22013-01-14 07:12:19 +00004992 sksec->sid = tunsec->sid;
Paul Mooreed6d76e2009-08-28 18:12:49 -04004993 sksec->sclass = SECCLASS_TUN_SOCKET;
Paul Moore5dbbaf22013-01-14 07:12:19 +00004994
4995 return 0;
Paul Mooreed6d76e2009-08-28 18:12:49 -04004996}
4997
Paul Moore5dbbaf22013-01-14 07:12:19 +00004998static int selinux_tun_dev_open(void *security)
Paul Mooreed6d76e2009-08-28 18:12:49 -04004999{
Paul Moore5dbbaf22013-01-14 07:12:19 +00005000 struct tun_security_struct *tunsec = security;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005001 u32 sid = current_sid();
5002 int err;
5003
Paul Moore5dbbaf22013-01-14 07:12:19 +00005004 err = avc_has_perm(sid, tunsec->sid, SECCLASS_TUN_SOCKET,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005005 TUN_SOCKET__RELABELFROM, NULL);
5006 if (err)
5007 return err;
5008 err = avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET,
5009 TUN_SOCKET__RELABELTO, NULL);
5010 if (err)
5011 return err;
Paul Moore5dbbaf22013-01-14 07:12:19 +00005012 tunsec->sid = sid;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005013
5014 return 0;
5015}
5016
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
5018{
Paul Moore6affa872020-04-28 09:59:02 -04005019 int rc = 0;
5020 unsigned int msg_len;
5021 unsigned int data_len = skb->len;
5022 unsigned char *data = skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023 struct nlmsghdr *nlh;
Paul Moore253bfae2010-04-22 14:46:19 -04005024 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore6affa872020-04-28 09:59:02 -04005025 u16 sclass = sksec->sclass;
5026 u32 perm;
Eric Paris828dfe12008-04-17 13:17:49 -04005027
Paul Moore6affa872020-04-28 09:59:02 -04005028 while (data_len >= nlmsg_total_size(0)) {
5029 nlh = (struct nlmsghdr *)data;
Eric Paris828dfe12008-04-17 13:17:49 -04005030
Paul Moore6affa872020-04-28 09:59:02 -04005031 /* NOTE: the nlmsg_len field isn't reliably set by some netlink
5032 * users which means we can't reject skb's with bogus
5033 * length fields; our solution is to follow what
5034 * netlink_rcv_skb() does and simply skip processing at
5035 * messages with length fields that are clearly junk
5036 */
5037 if (nlh->nlmsg_len < NLMSG_HDRLEN || nlh->nlmsg_len > data_len)
5038 return 0;
5039
5040 rc = selinux_nlmsg_lookup(sclass, nlh->nlmsg_type, &perm);
5041 if (rc == 0) {
5042 rc = sock_has_perm(current, sk, perm);
5043 if (rc)
5044 return rc;
5045 } else if (rc == -EINVAL) {
5046 /* -EINVAL is a missing msg/perm mapping */
Vladis Dronov76319942015-12-24 11:09:41 -05005047 pr_warn_ratelimited("SELinux: unrecognized netlink"
Paul Moore6affa872020-04-28 09:59:02 -04005048 " message: protocol=%hu nlmsg_type=%hu sclass=%s"
5049 " pid=%d comm=%s\n",
5050 sk->sk_protocol, nlh->nlmsg_type,
5051 secclass_map[sclass - 1].name,
5052 task_pid_nr(current), current->comm);
5053 if (selinux_enforcing && !security_get_allow_unknown())
5054 return rc;
5055 rc = 0;
5056 } else if (rc == -ENOENT) {
5057 /* -ENOENT is a missing socket/class mapping, ignore */
5058 rc = 0;
5059 } else {
5060 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061 }
5062
Paul Moore6affa872020-04-28 09:59:02 -04005063 /* move to the next message after applying netlink padding */
5064 msg_len = NLMSG_ALIGN(nlh->nlmsg_len);
5065 if (msg_len >= data_len)
5066 return 0;
5067 data_len -= msg_len;
5068 data += msg_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 }
5070
Paul Moore6affa872020-04-28 09:59:02 -04005071 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072}
5073
5074#ifdef CONFIG_NETFILTER
5075
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005076static unsigned int selinux_ip_forward(struct sk_buff *skb,
5077 const struct net_device *indev,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005078 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079{
Paul Mooredfaebe92008-10-10 10:16:31 -04005080 int err;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005081 char *addrp;
5082 u32 peer_sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04005083 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005084 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05005085 u8 secmark_active;
Paul Moore948bf852008-10-10 10:16:32 -04005086 u8 netlbl_active;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005087 u8 peerlbl_active;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005088
Paul Mooreeffad8d2008-01-29 08:49:27 -05005089 if (!selinux_policycap_netpeer)
5090 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005091
Paul Mooreeffad8d2008-01-29 08:49:27 -05005092 secmark_active = selinux_secmark_enabled();
Paul Moore948bf852008-10-10 10:16:32 -04005093 netlbl_active = netlbl_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04005094 peerlbl_active = selinux_peerlbl_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05005095 if (!secmark_active && !peerlbl_active)
5096 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005097
Paul Moored8395c82008-10-10 10:16:30 -04005098 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
5099 return NF_DROP;
5100
Eric Paris50c205f2012-04-04 15:01:43 -04005101 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005102 ad.u.net = &net;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005103 ad.u.net->netif = indev->ifindex;
Eric Paris48c62af2012-04-02 13:15:44 -04005104 ad.u.net->family = family;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005105 if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
5106 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107
Paul Mooredfaebe92008-10-10 10:16:31 -04005108 if (peerlbl_active) {
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005109 err = selinux_inet_sys_rcv_skb(dev_net(indev), indev->ifindex,
5110 addrp, family, peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04005111 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04005112 selinux_netlbl_err(skb, family, err, 1);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005113 return NF_DROP;
Paul Mooredfaebe92008-10-10 10:16:31 -04005114 }
5115 }
Paul Mooreeffad8d2008-01-29 08:49:27 -05005116
5117 if (secmark_active)
5118 if (avc_has_perm(peer_sid, skb->secmark,
5119 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
5120 return NF_DROP;
5121
Paul Moore948bf852008-10-10 10:16:32 -04005122 if (netlbl_active)
5123 /* we do this in the FORWARD path and not the POST_ROUTING
5124 * path because we want to make sure we apply the necessary
5125 * labeling before IPsec is applied so we can leverage AH
5126 * protection */
5127 if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
5128 return NF_DROP;
5129
Paul Mooreeffad8d2008-01-29 08:49:27 -05005130 return NF_ACCEPT;
5131}
5132
Eric W. Biederman06198b32015-09-18 14:33:06 -05005133static unsigned int selinux_ipv4_forward(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005134 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005135 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005136{
David S. Miller238e54c2015-04-03 20:32:56 -04005137 return selinux_ip_forward(skb, state->in, PF_INET);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005138}
5139
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005140#if IS_ENABLED(CONFIG_IPV6)
Eric W. Biederman06198b32015-09-18 14:33:06 -05005141static unsigned int selinux_ipv6_forward(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005142 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005143 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005144{
David S. Miller238e54c2015-04-03 20:32:56 -04005145 return selinux_ip_forward(skb, state->in, PF_INET6);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005146}
5147#endif /* IPV6 */
5148
Paul Moore948bf852008-10-10 10:16:32 -04005149static unsigned int selinux_ip_output(struct sk_buff *skb,
5150 u16 family)
5151{
Paul Moore47180062013-12-04 16:10:45 -05005152 struct sock *sk;
Paul Moore948bf852008-10-10 10:16:32 -04005153 u32 sid;
5154
5155 if (!netlbl_enabled())
5156 return NF_ACCEPT;
5157
5158 /* we do this in the LOCAL_OUT path and not the POST_ROUTING path
5159 * because we want to make sure we apply the necessary labeling
5160 * before IPsec is applied so we can leverage AH protection */
Paul Moore47180062013-12-04 16:10:45 -05005161 sk = skb->sk;
5162 if (sk) {
5163 struct sk_security_struct *sksec;
5164
Eric Dumazete446f9d2015-10-08 05:01:55 -07005165 if (sk_listener(sk))
Paul Moore47180062013-12-04 16:10:45 -05005166 /* if the socket is the listening state then this
5167 * packet is a SYN-ACK packet which means it needs to
5168 * be labeled based on the connection/request_sock and
5169 * not the parent socket. unfortunately, we can't
5170 * lookup the request_sock yet as it isn't queued on
5171 * the parent socket until after the SYN-ACK is sent.
5172 * the "solution" is to simply pass the packet as-is
5173 * as any IP option based labeling should be copied
5174 * from the initial connection request (in the IP
5175 * layer). it is far from ideal, but until we get a
5176 * security label in the packet itself this is the
5177 * best we can do. */
5178 return NF_ACCEPT;
5179
5180 /* standard practice, label using the parent socket */
5181 sksec = sk->sk_security;
Paul Moore948bf852008-10-10 10:16:32 -04005182 sid = sksec->sid;
5183 } else
5184 sid = SECINITSID_KERNEL;
5185 if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
5186 return NF_DROP;
5187
5188 return NF_ACCEPT;
5189}
5190
Eric W. Biederman06198b32015-09-18 14:33:06 -05005191static unsigned int selinux_ipv4_output(void *priv,
Paul Moore948bf852008-10-10 10:16:32 -04005192 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005193 const struct nf_hook_state *state)
Paul Moore948bf852008-10-10 10:16:32 -04005194{
5195 return selinux_ip_output(skb, PF_INET);
5196}
5197
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005198#if IS_ENABLED(CONFIG_IPV6)
Huw Davies2917f572016-06-27 15:06:15 -04005199static unsigned int selinux_ipv6_output(void *priv,
5200 struct sk_buff *skb,
5201 const struct nf_hook_state *state)
5202{
5203 return selinux_ip_output(skb, PF_INET6);
5204}
5205#endif /* IPV6 */
5206
Paul Mooreeffad8d2008-01-29 08:49:27 -05005207static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
5208 int ifindex,
Paul Moored8395c82008-10-10 10:16:30 -04005209 u16 family)
James Morris4e5ab4c2006-06-09 00:33:33 -07005210{
Eric Dumazet54abc682015-11-08 10:54:07 -08005211 struct sock *sk = skb_to_full_sk(skb);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005212 struct sk_security_struct *sksec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005213 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005214 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04005215 char *addrp;
5216 u8 proto;
James Morris4e5ab4c2006-06-09 00:33:33 -07005217
Paul Mooreeffad8d2008-01-29 08:49:27 -05005218 if (sk == NULL)
5219 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005220 sksec = sk->sk_security;
James Morris4e5ab4c2006-06-09 00:33:33 -07005221
Eric Paris50c205f2012-04-04 15:01:43 -04005222 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005223 ad.u.net = &net;
5224 ad.u.net->netif = ifindex;
5225 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04005226 if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
5227 return NF_DROP;
5228
Paul Moore58bfbb52009-03-27 17:10:41 -04005229 if (selinux_secmark_enabled())
Paul Mooreeffad8d2008-01-29 08:49:27 -05005230 if (avc_has_perm(sksec->sid, skb->secmark,
Paul Moored8395c82008-10-10 10:16:30 -04005231 SECCLASS_PACKET, PACKET__SEND, &ad))
Eric Paris2fe66ec2010-11-23 06:28:08 +00005232 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07005233
Steffen Klassertb9679a72011-02-23 12:55:21 +01005234 if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
5235 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07005236
Paul Mooreeffad8d2008-01-29 08:49:27 -05005237 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238}
5239
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005240static unsigned int selinux_ip_postroute(struct sk_buff *skb,
5241 const struct net_device *outdev,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005242 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243{
Paul Mooreeffad8d2008-01-29 08:49:27 -05005244 u32 secmark_perm;
5245 u32 peer_sid;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005246 int ifindex = outdev->ifindex;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005247 struct sock *sk;
Thomas Liu2bf49692009-07-14 12:14:09 -04005248 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005249 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05005250 char *addrp;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005251 u8 secmark_active;
5252 u8 peerlbl_active;
5253
Paul Mooreeffad8d2008-01-29 08:49:27 -05005254 /* If any sort of compatibility mode is enabled then handoff processing
5255 * to the selinux_ip_postroute_compat() function to deal with the
5256 * special handling. We do this in an attempt to keep this function
5257 * as fast and as clean as possible. */
Paul Moore58bfbb52009-03-27 17:10:41 -04005258 if (!selinux_policycap_netpeer)
Paul Moored8395c82008-10-10 10:16:30 -04005259 return selinux_ip_postroute_compat(skb, ifindex, family);
Paul Moorec0828e52013-12-10 14:58:01 -05005260
Paul Mooreeffad8d2008-01-29 08:49:27 -05005261 secmark_active = selinux_secmark_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04005262 peerlbl_active = selinux_peerlbl_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05005263 if (!secmark_active && !peerlbl_active)
5264 return NF_ACCEPT;
5265
Eric Dumazet54abc682015-11-08 10:54:07 -08005266 sk = skb_to_full_sk(skb);
Paul Moorec0828e52013-12-10 14:58:01 -05005267
Paul Mooreeffad8d2008-01-29 08:49:27 -05005268#ifdef CONFIG_XFRM
5269 /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
5270 * packet transformation so allow the packet to pass without any checks
5271 * since we'll have another chance to perform access control checks
5272 * when the packet is on it's final way out.
5273 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
Paul Moorec0828e52013-12-10 14:58:01 -05005274 * is NULL, in this case go ahead and apply access control.
5275 * NOTE: if this is a local socket (skb->sk != NULL) that is in the
5276 * TCP listening state we cannot wait until the XFRM processing
5277 * is done as we will miss out on the SA label if we do;
5278 * unfortunately, this means more work, but it is only once per
5279 * connection. */
5280 if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL &&
Eric Dumazete446f9d2015-10-08 05:01:55 -07005281 !(sk && sk_listener(sk)))
Paul Mooreeffad8d2008-01-29 08:49:27 -05005282 return NF_ACCEPT;
5283#endif
Paul Mooreeffad8d2008-01-29 08:49:27 -05005284
Paul Moored8395c82008-10-10 10:16:30 -04005285 if (sk == NULL) {
Paul Moore446b8022013-12-04 16:10:51 -05005286 /* Without an associated socket the packet is either coming
5287 * from the kernel or it is being forwarded; check the packet
5288 * to determine which and if the packet is being forwarded
5289 * query the packet directly to determine the security label. */
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005290 if (skb->skb_iif) {
5291 secmark_perm = PACKET__FORWARD_OUT;
Paul Moored8395c82008-10-10 10:16:30 -04005292 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005293 return NF_DROP;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005294 } else {
5295 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04005296 peer_sid = SECINITSID_KERNEL;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005297 }
Eric Dumazete446f9d2015-10-08 05:01:55 -07005298 } else if (sk_listener(sk)) {
Paul Moore446b8022013-12-04 16:10:51 -05005299 /* Locally generated packet but the associated socket is in the
5300 * listening state which means this is a SYN-ACK packet. In
5301 * this particular case the correct security label is assigned
5302 * to the connection/request_sock but unfortunately we can't
5303 * query the request_sock as it isn't queued on the parent
5304 * socket until after the SYN-ACK packet is sent; the only
5305 * viable choice is to regenerate the label like we do in
5306 * selinux_inet_conn_request(). See also selinux_ip_output()
5307 * for similar problems. */
5308 u32 skb_sid;
Eric Dumazete446f9d2015-10-08 05:01:55 -07005309 struct sk_security_struct *sksec;
5310
Eric Dumazete446f9d2015-10-08 05:01:55 -07005311 sksec = sk->sk_security;
Paul Moore446b8022013-12-04 16:10:51 -05005312 if (selinux_skb_peerlbl_sid(skb, family, &skb_sid))
5313 return NF_DROP;
Paul Moorec0828e52013-12-10 14:58:01 -05005314 /* At this point, if the returned skb peerlbl is SECSID_NULL
5315 * and the packet has been through at least one XFRM
5316 * transformation then we must be dealing with the "final"
5317 * form of labeled IPsec packet; since we've already applied
5318 * all of our access controls on this packet we can safely
5319 * pass the packet. */
5320 if (skb_sid == SECSID_NULL) {
5321 switch (family) {
5322 case PF_INET:
5323 if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
5324 return NF_ACCEPT;
5325 break;
5326 case PF_INET6:
5327 if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED)
5328 return NF_ACCEPT;
Paul Moorea7a91a12014-09-03 10:51:59 -04005329 break;
Paul Moorec0828e52013-12-10 14:58:01 -05005330 default:
5331 return NF_DROP_ERR(-ECONNREFUSED);
5332 }
5333 }
Paul Moore446b8022013-12-04 16:10:51 -05005334 if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid))
5335 return NF_DROP;
5336 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04005337 } else {
Paul Moore446b8022013-12-04 16:10:51 -05005338 /* Locally generated packet, fetch the security label from the
5339 * associated socket. */
Paul Mooreeffad8d2008-01-29 08:49:27 -05005340 struct sk_security_struct *sksec = sk->sk_security;
5341 peer_sid = sksec->sid;
5342 secmark_perm = PACKET__SEND;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005343 }
5344
Eric Paris50c205f2012-04-04 15:01:43 -04005345 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005346 ad.u.net = &net;
5347 ad.u.net->netif = ifindex;
5348 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04005349 if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
Eric Paris04f6d702010-11-23 06:28:02 +00005350 return NF_DROP;
Paul Moored8395c82008-10-10 10:16:30 -04005351
Paul Mooreeffad8d2008-01-29 08:49:27 -05005352 if (secmark_active)
5353 if (avc_has_perm(peer_sid, skb->secmark,
5354 SECCLASS_PACKET, secmark_perm, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005355 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005356
5357 if (peerlbl_active) {
5358 u32 if_sid;
5359 u32 node_sid;
5360
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005361 if (sel_netif_sid(dev_net(outdev), ifindex, &if_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005362 return NF_DROP;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005363 if (avc_has_perm(peer_sid, if_sid,
5364 SECCLASS_NETIF, NETIF__EGRESS, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005365 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005366
5367 if (sel_netnode_sid(addrp, family, &node_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005368 return NF_DROP;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005369 if (avc_has_perm(peer_sid, node_sid,
5370 SECCLASS_NODE, NODE__SENDTO, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005371 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005372 }
5373
5374 return NF_ACCEPT;
5375}
5376
Eric W. Biederman06198b32015-09-18 14:33:06 -05005377static unsigned int selinux_ipv4_postroute(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005378 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005379 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005380{
David S. Miller238e54c2015-04-03 20:32:56 -04005381 return selinux_ip_postroute(skb, state->out, PF_INET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382}
5383
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005384#if IS_ENABLED(CONFIG_IPV6)
Eric W. Biederman06198b32015-09-18 14:33:06 -05005385static unsigned int selinux_ipv6_postroute(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005386 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005387 const struct nf_hook_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388{
David S. Miller238e54c2015-04-03 20:32:56 -04005389 return selinux_ip_postroute(skb, state->out, PF_INET6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391#endif /* IPV6 */
5392
5393#endif /* CONFIG_NETFILTER */
5394
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
5396{
Stephen Smalley941fc5b2009-10-01 14:48:23 -04005397 return selinux_nlmsg_perm(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398}
5399
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400static int ipc_alloc_security(struct task_struct *task,
5401 struct kern_ipc_perm *perm,
5402 u16 sclass)
5403{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404 struct ipc_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +11005405 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406
James Morris89d155e2005-10-30 14:59:21 -08005407 isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408 if (!isec)
5409 return -ENOMEM;
5410
David Howells275bb412008-11-14 10:39:19 +11005411 sid = task_sid(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412 isec->sclass = sclass;
David Howells275bb412008-11-14 10:39:19 +11005413 isec->sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414 perm->security = isec;
5415
5416 return 0;
5417}
5418
5419static void ipc_free_security(struct kern_ipc_perm *perm)
5420{
5421 struct ipc_security_struct *isec = perm->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422 perm->security = NULL;
5423 kfree(isec);
5424}
5425
5426static int msg_msg_alloc_security(struct msg_msg *msg)
5427{
5428 struct msg_security_struct *msec;
5429
James Morris89d155e2005-10-30 14:59:21 -08005430 msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 if (!msec)
5432 return -ENOMEM;
5433
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434 msec->sid = SECINITSID_UNLABELED;
5435 msg->security = msec;
5436
5437 return 0;
5438}
5439
5440static void msg_msg_free_security(struct msg_msg *msg)
5441{
5442 struct msg_security_struct *msec = msg->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443
5444 msg->security = NULL;
5445 kfree(msec);
5446}
5447
5448static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
Stephen Smalley6af963f2005-05-01 08:58:39 -07005449 u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005452 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005453 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455 isec = ipc_perms->security;
5456
Eric Paris50c205f2012-04-04 15:01:43 -04005457 ad.type = LSM_AUDIT_DATA_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458 ad.u.ipc_id = ipc_perms->key;
5459
David Howells275bb412008-11-14 10:39:19 +11005460 return avc_has_perm(sid, isec->sid, isec->sclass, perms, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461}
5462
5463static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
5464{
5465 return msg_msg_alloc_security(msg);
5466}
5467
5468static void selinux_msg_msg_free_security(struct msg_msg *msg)
5469{
5470 msg_msg_free_security(msg);
5471}
5472
5473/* message queue security operations */
5474static int selinux_msg_queue_alloc_security(struct msg_queue *msq)
5475{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005477 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005478 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479 int rc;
5480
5481 rc = ipc_alloc_security(current, &msq->q_perm, SECCLASS_MSGQ);
5482 if (rc)
5483 return rc;
5484
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485 isec = msq->q_perm.security;
5486
Eric Paris50c205f2012-04-04 15:01:43 -04005487 ad.type = LSM_AUDIT_DATA_IPC;
Eric Paris828dfe12008-04-17 13:17:49 -04005488 ad.u.ipc_id = msq->q_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489
David Howells275bb412008-11-14 10:39:19 +11005490 rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491 MSGQ__CREATE, &ad);
5492 if (rc) {
5493 ipc_free_security(&msq->q_perm);
5494 return rc;
5495 }
5496 return 0;
5497}
5498
5499static void selinux_msg_queue_free_security(struct msg_queue *msq)
5500{
5501 ipc_free_security(&msq->q_perm);
5502}
5503
5504static int selinux_msg_queue_associate(struct msg_queue *msq, int msqflg)
5505{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005507 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005508 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510 isec = msq->q_perm.security;
5511
Eric Paris50c205f2012-04-04 15:01:43 -04005512 ad.type = LSM_AUDIT_DATA_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513 ad.u.ipc_id = msq->q_perm.key;
5514
David Howells275bb412008-11-14 10:39:19 +11005515 return avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516 MSGQ__ASSOCIATE, &ad);
5517}
5518
5519static int selinux_msg_queue_msgctl(struct msg_queue *msq, int cmd)
5520{
5521 int err;
5522 int perms;
5523
Eric Paris828dfe12008-04-17 13:17:49 -04005524 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525 case IPC_INFO:
5526 case MSG_INFO:
5527 /* No specific object, just general system-wide information. */
5528 return task_has_system(current, SYSTEM__IPC_INFO);
5529 case IPC_STAT:
5530 case MSG_STAT:
5531 perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
5532 break;
5533 case IPC_SET:
5534 perms = MSGQ__SETATTR;
5535 break;
5536 case IPC_RMID:
5537 perms = MSGQ__DESTROY;
5538 break;
5539 default:
5540 return 0;
5541 }
5542
Stephen Smalley6af963f2005-05-01 08:58:39 -07005543 err = ipc_has_perm(&msq->q_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544 return err;
5545}
5546
5547static int selinux_msg_queue_msgsnd(struct msg_queue *msq, struct msg_msg *msg, int msqflg)
5548{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549 struct ipc_security_struct *isec;
5550 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005551 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005552 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553 int rc;
5554
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555 isec = msq->q_perm.security;
5556 msec = msg->security;
5557
5558 /*
5559 * First time through, need to assign label to the message
5560 */
5561 if (msec->sid == SECINITSID_UNLABELED) {
5562 /*
5563 * Compute new sid based on current process and
5564 * message queue this message will be stored in
5565 */
David Howells275bb412008-11-14 10:39:19 +11005566 rc = security_transition_sid(sid, isec->sid, SECCLASS_MSG,
Eric Paris652bb9b2011-02-01 11:05:40 -05005567 NULL, &msec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568 if (rc)
5569 return rc;
5570 }
5571
Eric Paris50c205f2012-04-04 15:01:43 -04005572 ad.type = LSM_AUDIT_DATA_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 ad.u.ipc_id = msq->q_perm.key;
5574
5575 /* Can this process write to the queue? */
David Howells275bb412008-11-14 10:39:19 +11005576 rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577 MSGQ__WRITE, &ad);
5578 if (!rc)
5579 /* Can this process send the message */
David Howells275bb412008-11-14 10:39:19 +11005580 rc = avc_has_perm(sid, msec->sid, SECCLASS_MSG,
5581 MSG__SEND, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582 if (!rc)
5583 /* Can the message be put in the queue? */
David Howells275bb412008-11-14 10:39:19 +11005584 rc = avc_has_perm(msec->sid, isec->sid, SECCLASS_MSGQ,
5585 MSGQ__ENQUEUE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586
5587 return rc;
5588}
5589
5590static int selinux_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
5591 struct task_struct *target,
5592 long type, int mode)
5593{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594 struct ipc_security_struct *isec;
5595 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005596 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005597 u32 sid = task_sid(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598 int rc;
5599
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600 isec = msq->q_perm.security;
5601 msec = msg->security;
5602
Eric Paris50c205f2012-04-04 15:01:43 -04005603 ad.type = LSM_AUDIT_DATA_IPC;
Eric Paris828dfe12008-04-17 13:17:49 -04005604 ad.u.ipc_id = msq->q_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605
David Howells275bb412008-11-14 10:39:19 +11005606 rc = avc_has_perm(sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607 SECCLASS_MSGQ, MSGQ__READ, &ad);
5608 if (!rc)
David Howells275bb412008-11-14 10:39:19 +11005609 rc = avc_has_perm(sid, msec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 SECCLASS_MSG, MSG__RECEIVE, &ad);
5611 return rc;
5612}
5613
5614/* Shared Memory security operations */
5615static int selinux_shm_alloc_security(struct shmid_kernel *shp)
5616{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005618 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005619 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620 int rc;
5621
5622 rc = ipc_alloc_security(current, &shp->shm_perm, SECCLASS_SHM);
5623 if (rc)
5624 return rc;
5625
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626 isec = shp->shm_perm.security;
5627
Eric Paris50c205f2012-04-04 15:01:43 -04005628 ad.type = LSM_AUDIT_DATA_IPC;
Eric Paris828dfe12008-04-17 13:17:49 -04005629 ad.u.ipc_id = shp->shm_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630
David Howells275bb412008-11-14 10:39:19 +11005631 rc = avc_has_perm(sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632 SHM__CREATE, &ad);
5633 if (rc) {
5634 ipc_free_security(&shp->shm_perm);
5635 return rc;
5636 }
5637 return 0;
5638}
5639
5640static void selinux_shm_free_security(struct shmid_kernel *shp)
5641{
5642 ipc_free_security(&shp->shm_perm);
5643}
5644
5645static int selinux_shm_associate(struct shmid_kernel *shp, int shmflg)
5646{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005648 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005649 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651 isec = shp->shm_perm.security;
5652
Eric Paris50c205f2012-04-04 15:01:43 -04005653 ad.type = LSM_AUDIT_DATA_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654 ad.u.ipc_id = shp->shm_perm.key;
5655
David Howells275bb412008-11-14 10:39:19 +11005656 return avc_has_perm(sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657 SHM__ASSOCIATE, &ad);
5658}
5659
5660/* Note, at this point, shp is locked down */
5661static int selinux_shm_shmctl(struct shmid_kernel *shp, int cmd)
5662{
5663 int perms;
5664 int err;
5665
Eric Paris828dfe12008-04-17 13:17:49 -04005666 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667 case IPC_INFO:
5668 case SHM_INFO:
5669 /* No specific object, just general system-wide information. */
5670 return task_has_system(current, SYSTEM__IPC_INFO);
5671 case IPC_STAT:
5672 case SHM_STAT:
5673 perms = SHM__GETATTR | SHM__ASSOCIATE;
5674 break;
5675 case IPC_SET:
5676 perms = SHM__SETATTR;
5677 break;
5678 case SHM_LOCK:
5679 case SHM_UNLOCK:
5680 perms = SHM__LOCK;
5681 break;
5682 case IPC_RMID:
5683 perms = SHM__DESTROY;
5684 break;
5685 default:
5686 return 0;
5687 }
5688
Stephen Smalley6af963f2005-05-01 08:58:39 -07005689 err = ipc_has_perm(&shp->shm_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690 return err;
5691}
5692
5693static int selinux_shm_shmat(struct shmid_kernel *shp,
5694 char __user *shmaddr, int shmflg)
5695{
5696 u32 perms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697
5698 if (shmflg & SHM_RDONLY)
5699 perms = SHM__READ;
5700 else
5701 perms = SHM__READ | SHM__WRITE;
5702
Stephen Smalley6af963f2005-05-01 08:58:39 -07005703 return ipc_has_perm(&shp->shm_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704}
5705
5706/* Semaphore security operations */
5707static int selinux_sem_alloc_security(struct sem_array *sma)
5708{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005710 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005711 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712 int rc;
5713
5714 rc = ipc_alloc_security(current, &sma->sem_perm, SECCLASS_SEM);
5715 if (rc)
5716 return rc;
5717
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718 isec = sma->sem_perm.security;
5719
Eric Paris50c205f2012-04-04 15:01:43 -04005720 ad.type = LSM_AUDIT_DATA_IPC;
Eric Paris828dfe12008-04-17 13:17:49 -04005721 ad.u.ipc_id = sma->sem_perm.key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722
David Howells275bb412008-11-14 10:39:19 +11005723 rc = avc_has_perm(sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724 SEM__CREATE, &ad);
5725 if (rc) {
5726 ipc_free_security(&sma->sem_perm);
5727 return rc;
5728 }
5729 return 0;
5730}
5731
5732static void selinux_sem_free_security(struct sem_array *sma)
5733{
5734 ipc_free_security(&sma->sem_perm);
5735}
5736
5737static int selinux_sem_associate(struct sem_array *sma, int semflg)
5738{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005740 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005741 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743 isec = sma->sem_perm.security;
5744
Eric Paris50c205f2012-04-04 15:01:43 -04005745 ad.type = LSM_AUDIT_DATA_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746 ad.u.ipc_id = sma->sem_perm.key;
5747
David Howells275bb412008-11-14 10:39:19 +11005748 return avc_has_perm(sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749 SEM__ASSOCIATE, &ad);
5750}
5751
5752/* Note, at this point, sma is locked down */
5753static int selinux_sem_semctl(struct sem_array *sma, int cmd)
5754{
5755 int err;
5756 u32 perms;
5757
Eric Paris828dfe12008-04-17 13:17:49 -04005758 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759 case IPC_INFO:
5760 case SEM_INFO:
5761 /* No specific object, just general system-wide information. */
5762 return task_has_system(current, SYSTEM__IPC_INFO);
5763 case GETPID:
5764 case GETNCNT:
5765 case GETZCNT:
5766 perms = SEM__GETATTR;
5767 break;
5768 case GETVAL:
5769 case GETALL:
5770 perms = SEM__READ;
5771 break;
5772 case SETVAL:
5773 case SETALL:
5774 perms = SEM__WRITE;
5775 break;
5776 case IPC_RMID:
5777 perms = SEM__DESTROY;
5778 break;
5779 case IPC_SET:
5780 perms = SEM__SETATTR;
5781 break;
5782 case IPC_STAT:
5783 case SEM_STAT:
5784 perms = SEM__GETATTR | SEM__ASSOCIATE;
5785 break;
5786 default:
5787 return 0;
5788 }
5789
Stephen Smalley6af963f2005-05-01 08:58:39 -07005790 err = ipc_has_perm(&sma->sem_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791 return err;
5792}
5793
5794static int selinux_sem_semop(struct sem_array *sma,
5795 struct sembuf *sops, unsigned nsops, int alter)
5796{
5797 u32 perms;
5798
5799 if (alter)
5800 perms = SEM__READ | SEM__WRITE;
5801 else
5802 perms = SEM__READ;
5803
Stephen Smalley6af963f2005-05-01 08:58:39 -07005804 return ipc_has_perm(&sma->sem_perm, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805}
5806
5807static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
5808{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809 u32 av = 0;
5810
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811 av = 0;
5812 if (flag & S_IRUGO)
5813 av |= IPC__UNIX_READ;
5814 if (flag & S_IWUGO)
5815 av |= IPC__UNIX_WRITE;
5816
5817 if (av == 0)
5818 return 0;
5819
Stephen Smalley6af963f2005-05-01 08:58:39 -07005820 return ipc_has_perm(ipcp, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821}
5822
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02005823static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
5824{
5825 struct ipc_security_struct *isec = ipcp->security;
5826 *secid = isec->sid;
5827}
5828
Eric Paris828dfe12008-04-17 13:17:49 -04005829static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005830{
5831 if (inode)
5832 inode_doinit_with_dentry(inode, dentry);
5833}
5834
5835static int selinux_getprocattr(struct task_struct *p,
Al Viro04ff9702007-03-12 16:17:58 +00005836 char *name, char **value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837{
David Howells275bb412008-11-14 10:39:19 +11005838 const struct task_security_struct *__tsec;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00005839 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840 int error;
Al Viro04ff9702007-03-12 16:17:58 +00005841 unsigned len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842
5843 if (current != p) {
David Howells3b11a1d2008-11-14 10:39:26 +11005844 error = current_has_perm(p, PROCESS__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005845 if (error)
5846 return error;
5847 }
5848
David Howells275bb412008-11-14 10:39:19 +11005849 rcu_read_lock();
5850 __tsec = __task_cred(p)->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851
5852 if (!strcmp(name, "current"))
David Howells275bb412008-11-14 10:39:19 +11005853 sid = __tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854 else if (!strcmp(name, "prev"))
David Howells275bb412008-11-14 10:39:19 +11005855 sid = __tsec->osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856 else if (!strcmp(name, "exec"))
David Howells275bb412008-11-14 10:39:19 +11005857 sid = __tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005858 else if (!strcmp(name, "fscreate"))
David Howells275bb412008-11-14 10:39:19 +11005859 sid = __tsec->create_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07005860 else if (!strcmp(name, "keycreate"))
David Howells275bb412008-11-14 10:39:19 +11005861 sid = __tsec->keycreate_sid;
Eric Paris42c3e032006-06-26 00:26:03 -07005862 else if (!strcmp(name, "sockcreate"))
David Howells275bb412008-11-14 10:39:19 +11005863 sid = __tsec->sockcreate_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864 else
David Howells275bb412008-11-14 10:39:19 +11005865 goto invalid;
5866 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867
5868 if (!sid)
5869 return 0;
5870
Al Viro04ff9702007-03-12 16:17:58 +00005871 error = security_sid_to_context(sid, value, &len);
5872 if (error)
5873 return error;
5874 return len;
David Howells275bb412008-11-14 10:39:19 +11005875
5876invalid:
5877 rcu_read_unlock();
5878 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005879}
5880
5881static int selinux_setprocattr(struct task_struct *p,
5882 char *name, void *value, size_t size)
5883{
5884 struct task_security_struct *tsec;
David Howellsd84f4f92008-11-14 10:39:23 +11005885 struct cred *new;
5886 u32 sid = 0, ptsid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005887 int error;
5888 char *str = value;
5889
5890 if (current != p) {
5891 /* SELinux only allows a process to change its own
5892 security attributes. */
5893 return -EACCES;
5894 }
5895
5896 /*
5897 * Basic control over ability to set these attributes at all.
5898 * current == p, but we'll pass them separately in case the
5899 * above restriction is ever removed.
5900 */
5901 if (!strcmp(name, "exec"))
David Howells3b11a1d2008-11-14 10:39:26 +11005902 error = current_has_perm(p, PROCESS__SETEXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903 else if (!strcmp(name, "fscreate"))
David Howells3b11a1d2008-11-14 10:39:26 +11005904 error = current_has_perm(p, PROCESS__SETFSCREATE);
Michael LeMay4eb582c2006-06-26 00:24:57 -07005905 else if (!strcmp(name, "keycreate"))
David Howells3b11a1d2008-11-14 10:39:26 +11005906 error = current_has_perm(p, PROCESS__SETKEYCREATE);
Eric Paris42c3e032006-06-26 00:26:03 -07005907 else if (!strcmp(name, "sockcreate"))
David Howells3b11a1d2008-11-14 10:39:26 +11005908 error = current_has_perm(p, PROCESS__SETSOCKCREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005909 else if (!strcmp(name, "current"))
David Howells3b11a1d2008-11-14 10:39:26 +11005910 error = current_has_perm(p, PROCESS__SETCURRENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005911 else
5912 error = -EINVAL;
5913 if (error)
5914 return error;
5915
5916 /* Obtain a SID for the context, if one was specified. */
Stephen Smalley6cbaf7b2017-01-31 11:54:04 -05005917 if (size && str[0] && str[0] != '\n') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918 if (str[size-1] == '\n') {
5919 str[size-1] = 0;
5920 size--;
5921 }
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01005922 error = security_context_to_sid(value, size, &sid, GFP_KERNEL);
Stephen Smalley12b29f32008-05-07 13:03:20 -04005923 if (error == -EINVAL && !strcmp(name, "fscreate")) {
Eric Parisd6ea83e2012-04-04 13:45:49 -04005924 if (!capable(CAP_MAC_ADMIN)) {
5925 struct audit_buffer *ab;
5926 size_t audit_size;
5927
5928 /* We strip a nul only if it is at the end, otherwise the
5929 * context contains a nul and we should audit that */
5930 if (str[size - 1] == '\0')
5931 audit_size = size - 1;
5932 else
5933 audit_size = size;
5934 ab = audit_log_start(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR);
5935 audit_log_format(ab, "op=fscreate invalid_context=");
5936 audit_log_n_untrustedstring(ab, value, audit_size);
5937 audit_log_end(ab);
5938
Stephen Smalley12b29f32008-05-07 13:03:20 -04005939 return error;
Eric Parisd6ea83e2012-04-04 13:45:49 -04005940 }
Stephen Smalley12b29f32008-05-07 13:03:20 -04005941 error = security_context_to_sid_force(value, size,
5942 &sid);
5943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944 if (error)
5945 return error;
5946 }
5947
David Howellsd84f4f92008-11-14 10:39:23 +11005948 new = prepare_creds();
5949 if (!new)
5950 return -ENOMEM;
5951
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952 /* Permission checking based on the specified context is
5953 performed during the actual operation (execve,
5954 open/mkdir/...), when we know the full context of the
David Howellsd84f4f92008-11-14 10:39:23 +11005955 operation. See selinux_bprm_set_creds for the execve
Linus Torvalds1da177e2005-04-16 15:20:36 -07005956 checks and may_create for the file creation checks. The
5957 operation will then fail if the context is not permitted. */
David Howellsd84f4f92008-11-14 10:39:23 +11005958 tsec = new->security;
5959 if (!strcmp(name, "exec")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005960 tsec->exec_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005961 } else if (!strcmp(name, "fscreate")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005962 tsec->create_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005963 } else if (!strcmp(name, "keycreate")) {
Michael LeMay4eb582c2006-06-26 00:24:57 -07005964 error = may_create_key(sid, p);
5965 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11005966 goto abort_change;
Michael LeMay4eb582c2006-06-26 00:24:57 -07005967 tsec->keycreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005968 } else if (!strcmp(name, "sockcreate")) {
Eric Paris42c3e032006-06-26 00:26:03 -07005969 tsec->sockcreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11005970 } else if (!strcmp(name, "current")) {
5971 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005972 if (sid == 0)
David Howellsd84f4f92008-11-14 10:39:23 +11005973 goto abort_change;
KaiGai Koheid9250de2008-08-28 16:35:57 +09005974
David Howellsd84f4f92008-11-14 10:39:23 +11005975 /* Only allow single threaded processes to change context */
5976 error = -EPERM;
Oleg Nesterov5bb459b2009-07-10 03:48:23 +02005977 if (!current_is_single_threaded()) {
David Howellsd84f4f92008-11-14 10:39:23 +11005978 error = security_bounded_transition(tsec->sid, sid);
5979 if (error)
5980 goto abort_change;
Eric Paris828dfe12008-04-17 13:17:49 -04005981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982
5983 /* Check permissions for the transition. */
5984 error = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS,
Eric Paris828dfe12008-04-17 13:17:49 -04005985 PROCESS__DYNTRANSITION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11005987 goto abort_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005988
5989 /* Check for ptracing, and update the task SID if ok.
5990 Otherwise, leave SID unchanged and fail. */
Paul Moore0c6181c2016-03-30 21:41:21 -04005991 ptsid = ptrace_parent_sid(p);
5992 if (ptsid != 0) {
David Howellsd84f4f92008-11-14 10:39:23 +11005993 error = avc_has_perm(ptsid, sid, SECCLASS_PROCESS,
5994 PROCESS__PTRACE, NULL);
5995 if (error)
5996 goto abort_change;
5997 }
5998
5999 tsec->sid = sid;
6000 } else {
6001 error = -EINVAL;
6002 goto abort_change;
6003 }
6004
6005 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006006 return size;
David Howellsd84f4f92008-11-14 10:39:23 +11006007
6008abort_change:
6009 abort_creds(new);
6010 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011}
6012
David Quigley746df9b2013-05-22 12:50:35 -04006013static int selinux_ismaclabel(const char *name)
6014{
6015 return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0);
6016}
6017
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006018static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
6019{
6020 return security_sid_to_context(secid, secdata, seclen);
6021}
6022
David Howells7bf570d2008-04-29 20:52:51 +01006023static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
David Howells63cb3442008-01-15 23:47:35 +00006024{
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01006025 return security_context_to_sid(secdata, seclen, secid, GFP_KERNEL);
David Howells63cb3442008-01-15 23:47:35 +00006026}
6027
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006028static void selinux_release_secctx(char *secdata, u32 seclen)
6029{
Paul Moore088999e2007-08-01 11:12:58 -04006030 kfree(secdata);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006031}
6032
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006033static void selinux_inode_invalidate_secctx(struct inode *inode)
6034{
6035 struct inode_security_struct *isec = inode->i_security;
6036
6037 mutex_lock(&isec->lock);
6038 isec->initialized = LABEL_INVALID;
6039 mutex_unlock(&isec->lock);
6040}
6041
David P. Quigley1ee65e32009-09-03 14:25:57 -04006042/*
6043 * called with inode->i_mutex locked
6044 */
6045static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
6046{
Ondrej Mosnacek992baf52018-12-21 21:18:53 +01006047 int rc = selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX,
6048 ctx, ctxlen, 0);
6049 /* Do not return error when suppressing label (SBLABEL_MNT not set). */
6050 return rc == -EOPNOTSUPP ? 0 : rc;
David P. Quigley1ee65e32009-09-03 14:25:57 -04006051}
6052
6053/*
6054 * called with inode->i_mutex locked
6055 */
6056static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
6057{
6058 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0);
6059}
6060
6061static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
6062{
6063 int len = 0;
6064 len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX,
6065 ctx, true);
6066 if (len < 0)
6067 return len;
6068 *ctxlen = len;
6069 return 0;
6070}
Michael LeMayd7200242006-06-22 14:47:17 -07006071#ifdef CONFIG_KEYS
6072
David Howellsd84f4f92008-11-14 10:39:23 +11006073static int selinux_key_alloc(struct key *k, const struct cred *cred,
David Howells7e047ef2006-06-26 00:24:50 -07006074 unsigned long flags)
Michael LeMayd7200242006-06-22 14:47:17 -07006075{
David Howellsd84f4f92008-11-14 10:39:23 +11006076 const struct task_security_struct *tsec;
Michael LeMayd7200242006-06-22 14:47:17 -07006077 struct key_security_struct *ksec;
6078
6079 ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
6080 if (!ksec)
6081 return -ENOMEM;
6082
David Howellsd84f4f92008-11-14 10:39:23 +11006083 tsec = cred->security;
6084 if (tsec->keycreate_sid)
6085 ksec->sid = tsec->keycreate_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006086 else
David Howellsd84f4f92008-11-14 10:39:23 +11006087 ksec->sid = tsec->sid;
Michael LeMayd7200242006-06-22 14:47:17 -07006088
David Howells275bb412008-11-14 10:39:19 +11006089 k->security = ksec;
Michael LeMayd7200242006-06-22 14:47:17 -07006090 return 0;
6091}
6092
6093static void selinux_key_free(struct key *k)
6094{
6095 struct key_security_struct *ksec = k->security;
6096
6097 k->security = NULL;
6098 kfree(ksec);
6099}
6100
6101static int selinux_key_permission(key_ref_t key_ref,
David Howellsd84f4f92008-11-14 10:39:23 +11006102 const struct cred *cred,
David Howellsf5895942014-03-14 17:44:49 +00006103 unsigned perm)
Michael LeMayd7200242006-06-22 14:47:17 -07006104{
6105 struct key *key;
Michael LeMayd7200242006-06-22 14:47:17 -07006106 struct key_security_struct *ksec;
David Howells275bb412008-11-14 10:39:19 +11006107 u32 sid;
Michael LeMayd7200242006-06-22 14:47:17 -07006108
6109 /* if no specific permissions are requested, we skip the
6110 permission check. No serious, additional covert channels
6111 appear to be created. */
6112 if (perm == 0)
6113 return 0;
6114
David Howellsd84f4f92008-11-14 10:39:23 +11006115 sid = cred_sid(cred);
David Howells275bb412008-11-14 10:39:19 +11006116
6117 key = key_ref_to_ptr(key_ref);
6118 ksec = key->security;
6119
6120 return avc_has_perm(sid, ksec->sid, SECCLASS_KEY, perm, NULL);
Michael LeMayd7200242006-06-22 14:47:17 -07006121}
6122
David Howells70a5bb72008-04-29 01:01:26 -07006123static int selinux_key_getsecurity(struct key *key, char **_buffer)
6124{
6125 struct key_security_struct *ksec = key->security;
6126 char *context = NULL;
6127 unsigned len;
6128 int rc;
6129
6130 rc = security_sid_to_context(ksec->sid, &context, &len);
6131 if (!rc)
6132 rc = len;
6133 *_buffer = context;
6134 return rc;
6135}
6136
Michael LeMayd7200242006-06-22 14:47:17 -07006137#endif
6138
Chenbo Feng9b629132017-10-18 13:00:25 -07006139#ifdef CONFIG_BPF_SYSCALL
6140static int selinux_bpf(int cmd, union bpf_attr *attr,
6141 unsigned int size)
6142{
6143 u32 sid = current_sid();
6144 int ret;
6145
6146 switch (cmd) {
6147 case BPF_MAP_CREATE:
6148 ret = avc_has_perm(sid, sid, SECCLASS_BPF, BPF__MAP_CREATE,
6149 NULL);
6150 break;
6151 case BPF_PROG_LOAD:
6152 ret = avc_has_perm(sid, sid, SECCLASS_BPF, BPF__PROG_LOAD,
6153 NULL);
6154 break;
6155 default:
6156 ret = 0;
6157 break;
6158 }
6159
6160 return ret;
6161}
6162
6163static u32 bpf_map_fmode_to_av(fmode_t fmode)
6164{
6165 u32 av = 0;
6166
6167 if (fmode & FMODE_READ)
6168 av |= BPF__MAP_READ;
6169 if (fmode & FMODE_WRITE)
6170 av |= BPF__MAP_WRITE;
6171 return av;
6172}
6173
Chenbo Feng0521e0b2017-10-18 13:00:26 -07006174/* This function will check the file pass through unix socket or binder to see
6175 * if it is a bpf related object. And apply correspinding checks on the bpf
6176 * object based on the type. The bpf maps and programs, not like other files and
6177 * socket, are using a shared anonymous inode inside the kernel as their inode.
6178 * So checking that inode cannot identify if the process have privilege to
6179 * access the bpf object and that's why we have to add this additional check in
6180 * selinux_file_receive and selinux_binder_transfer_files.
6181 */
6182static int bpf_fd_pass(struct file *file, u32 sid)
6183{
6184 struct bpf_security_struct *bpfsec;
6185 struct bpf_prog *prog;
6186 struct bpf_map *map;
6187 int ret;
6188
6189 if (file->f_op == &bpf_map_fops) {
6190 map = file->private_data;
6191 bpfsec = map->security;
6192 ret = avc_has_perm(sid, bpfsec->sid, SECCLASS_BPF,
6193 bpf_map_fmode_to_av(file->f_mode), NULL);
6194 if (ret)
6195 return ret;
6196 } else if (file->f_op == &bpf_prog_fops) {
6197 prog = file->private_data;
6198 bpfsec = prog->aux->security;
6199 ret = avc_has_perm(sid, bpfsec->sid, SECCLASS_BPF,
6200 BPF__PROG_RUN, NULL);
6201 if (ret)
6202 return ret;
6203 }
6204 return 0;
6205}
6206
Chenbo Feng9b629132017-10-18 13:00:25 -07006207static int selinux_bpf_map(struct bpf_map *map, fmode_t fmode)
6208{
6209 u32 sid = current_sid();
6210 struct bpf_security_struct *bpfsec;
6211
6212 bpfsec = map->security;
6213 return avc_has_perm(sid, bpfsec->sid, SECCLASS_BPF,
6214 bpf_map_fmode_to_av(fmode), NULL);
6215}
6216
6217static int selinux_bpf_prog(struct bpf_prog *prog)
6218{
6219 u32 sid = current_sid();
6220 struct bpf_security_struct *bpfsec;
6221
6222 bpfsec = prog->aux->security;
6223 return avc_has_perm(sid, bpfsec->sid, SECCLASS_BPF,
6224 BPF__PROG_RUN, NULL);
6225}
6226
6227static int selinux_bpf_map_alloc(struct bpf_map *map)
6228{
6229 struct bpf_security_struct *bpfsec;
6230
6231 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6232 if (!bpfsec)
6233 return -ENOMEM;
6234
6235 bpfsec->sid = current_sid();
6236 map->security = bpfsec;
6237
6238 return 0;
6239}
6240
6241static void selinux_bpf_map_free(struct bpf_map *map)
6242{
6243 struct bpf_security_struct *bpfsec = map->security;
6244
6245 map->security = NULL;
6246 kfree(bpfsec);
6247}
6248
6249static int selinux_bpf_prog_alloc(struct bpf_prog_aux *aux)
6250{
6251 struct bpf_security_struct *bpfsec;
6252
6253 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6254 if (!bpfsec)
6255 return -ENOMEM;
6256
6257 bpfsec->sid = current_sid();
6258 aux->security = bpfsec;
6259
6260 return 0;
6261}
6262
6263static void selinux_bpf_prog_free(struct bpf_prog_aux *aux)
6264{
6265 struct bpf_security_struct *bpfsec = aux->security;
6266
6267 aux->security = NULL;
6268 kfree(bpfsec);
6269}
6270#endif
6271
James Morriscaefc012017-02-15 00:18:51 +11006272static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {
Casey Schauflere20b0432015-05-02 15:11:36 -07006273 LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr),
6274 LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction),
6275 LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder),
6276 LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file),
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02006277
Casey Schauflere20b0432015-05-02 15:11:36 -07006278 LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check),
6279 LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme),
6280 LSM_HOOK_INIT(capget, selinux_capget),
6281 LSM_HOOK_INIT(capset, selinux_capset),
6282 LSM_HOOK_INIT(capable, selinux_capable),
6283 LSM_HOOK_INIT(quotactl, selinux_quotactl),
6284 LSM_HOOK_INIT(quota_on, selinux_quota_on),
6285 LSM_HOOK_INIT(syslog, selinux_syslog),
6286 LSM_HOOK_INIT(vm_enough_memory, selinux_vm_enough_memory),
Stephen Smalley79af7302015-01-21 10:54:10 -05006287
Casey Schauflere20b0432015-05-02 15:11:36 -07006288 LSM_HOOK_INIT(netlink_send, selinux_netlink_send),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006289
Casey Schauflere20b0432015-05-02 15:11:36 -07006290 LSM_HOOK_INIT(bprm_set_creds, selinux_bprm_set_creds),
6291 LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds),
6292 LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds),
6293 LSM_HOOK_INIT(bprm_secureexec, selinux_bprm_secureexec),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006294
Casey Schauflere20b0432015-05-02 15:11:36 -07006295 LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security),
6296 LSM_HOOK_INIT(sb_free_security, selinux_sb_free_security),
6297 LSM_HOOK_INIT(sb_copy_data, selinux_sb_copy_data),
6298 LSM_HOOK_INIT(sb_remount, selinux_sb_remount),
6299 LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount),
6300 LSM_HOOK_INIT(sb_show_options, selinux_sb_show_options),
6301 LSM_HOOK_INIT(sb_statfs, selinux_sb_statfs),
6302 LSM_HOOK_INIT(sb_mount, selinux_mount),
6303 LSM_HOOK_INIT(sb_umount, selinux_umount),
6304 LSM_HOOK_INIT(sb_set_mnt_opts, selinux_set_mnt_opts),
6305 LSM_HOOK_INIT(sb_clone_mnt_opts, selinux_sb_clone_mnt_opts),
6306 LSM_HOOK_INIT(sb_parse_opts_str, selinux_parse_opts_str),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006307
Casey Schauflere20b0432015-05-02 15:11:36 -07006308 LSM_HOOK_INIT(dentry_init_security, selinux_dentry_init_security),
Vivek Goyala518b0a2016-07-13 10:44:53 -04006309 LSM_HOOK_INIT(dentry_create_files_as, selinux_dentry_create_files_as),
Eric Parise0007522008-03-05 10:31:54 -05006310
Casey Schauflere20b0432015-05-02 15:11:36 -07006311 LSM_HOOK_INIT(inode_alloc_security, selinux_inode_alloc_security),
6312 LSM_HOOK_INIT(inode_free_security, selinux_inode_free_security),
6313 LSM_HOOK_INIT(inode_init_security, selinux_inode_init_security),
6314 LSM_HOOK_INIT(inode_create, selinux_inode_create),
6315 LSM_HOOK_INIT(inode_link, selinux_inode_link),
6316 LSM_HOOK_INIT(inode_unlink, selinux_inode_unlink),
6317 LSM_HOOK_INIT(inode_symlink, selinux_inode_symlink),
6318 LSM_HOOK_INIT(inode_mkdir, selinux_inode_mkdir),
6319 LSM_HOOK_INIT(inode_rmdir, selinux_inode_rmdir),
6320 LSM_HOOK_INIT(inode_mknod, selinux_inode_mknod),
6321 LSM_HOOK_INIT(inode_rename, selinux_inode_rename),
6322 LSM_HOOK_INIT(inode_readlink, selinux_inode_readlink),
6323 LSM_HOOK_INIT(inode_follow_link, selinux_inode_follow_link),
6324 LSM_HOOK_INIT(inode_permission, selinux_inode_permission),
6325 LSM_HOOK_INIT(inode_setattr, selinux_inode_setattr),
6326 LSM_HOOK_INIT(inode_getattr, selinux_inode_getattr),
6327 LSM_HOOK_INIT(inode_setxattr, selinux_inode_setxattr),
6328 LSM_HOOK_INIT(inode_post_setxattr, selinux_inode_post_setxattr),
6329 LSM_HOOK_INIT(inode_getxattr, selinux_inode_getxattr),
6330 LSM_HOOK_INIT(inode_listxattr, selinux_inode_listxattr),
6331 LSM_HOOK_INIT(inode_removexattr, selinux_inode_removexattr),
6332 LSM_HOOK_INIT(inode_getsecurity, selinux_inode_getsecurity),
6333 LSM_HOOK_INIT(inode_setsecurity, selinux_inode_setsecurity),
6334 LSM_HOOK_INIT(inode_listsecurity, selinux_inode_listsecurity),
6335 LSM_HOOK_INIT(inode_getsecid, selinux_inode_getsecid),
Vivek Goyal56909eb2016-07-13 10:44:48 -04006336 LSM_HOOK_INIT(inode_copy_up, selinux_inode_copy_up),
Vivek Goyal19472b62016-07-13 10:44:50 -04006337 LSM_HOOK_INIT(inode_copy_up_xattr, selinux_inode_copy_up_xattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006338
Casey Schauflere20b0432015-05-02 15:11:36 -07006339 LSM_HOOK_INIT(file_permission, selinux_file_permission),
6340 LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security),
6341 LSM_HOOK_INIT(file_free_security, selinux_file_free_security),
6342 LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl),
6343 LSM_HOOK_INIT(mmap_file, selinux_mmap_file),
6344 LSM_HOOK_INIT(mmap_addr, selinux_mmap_addr),
6345 LSM_HOOK_INIT(file_mprotect, selinux_file_mprotect),
6346 LSM_HOOK_INIT(file_lock, selinux_file_lock),
6347 LSM_HOOK_INIT(file_fcntl, selinux_file_fcntl),
6348 LSM_HOOK_INIT(file_set_fowner, selinux_file_set_fowner),
6349 LSM_HOOK_INIT(file_send_sigiotask, selinux_file_send_sigiotask),
6350 LSM_HOOK_INIT(file_receive, selinux_file_receive),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006351
Casey Schauflere20b0432015-05-02 15:11:36 -07006352 LSM_HOOK_INIT(file_open, selinux_file_open),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006353
Casey Schauflere20b0432015-05-02 15:11:36 -07006354 LSM_HOOK_INIT(task_create, selinux_task_create),
6355 LSM_HOOK_INIT(cred_alloc_blank, selinux_cred_alloc_blank),
6356 LSM_HOOK_INIT(cred_free, selinux_cred_free),
6357 LSM_HOOK_INIT(cred_prepare, selinux_cred_prepare),
6358 LSM_HOOK_INIT(cred_transfer, selinux_cred_transfer),
6359 LSM_HOOK_INIT(kernel_act_as, selinux_kernel_act_as),
6360 LSM_HOOK_INIT(kernel_create_files_as, selinux_kernel_create_files_as),
6361 LSM_HOOK_INIT(kernel_module_request, selinux_kernel_module_request),
Jeff Vander Stoep61d612e2016-04-05 13:06:27 -07006362 LSM_HOOK_INIT(kernel_read_file, selinux_kernel_read_file),
Casey Schauflere20b0432015-05-02 15:11:36 -07006363 LSM_HOOK_INIT(task_setpgid, selinux_task_setpgid),
6364 LSM_HOOK_INIT(task_getpgid, selinux_task_getpgid),
6365 LSM_HOOK_INIT(task_getsid, selinux_task_getsid),
6366 LSM_HOOK_INIT(task_getsecid, selinux_task_getsecid),
6367 LSM_HOOK_INIT(task_setnice, selinux_task_setnice),
6368 LSM_HOOK_INIT(task_setioprio, selinux_task_setioprio),
6369 LSM_HOOK_INIT(task_getioprio, selinux_task_getioprio),
6370 LSM_HOOK_INIT(task_setrlimit, selinux_task_setrlimit),
6371 LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler),
6372 LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler),
6373 LSM_HOOK_INIT(task_movememory, selinux_task_movememory),
6374 LSM_HOOK_INIT(task_kill, selinux_task_kill),
6375 LSM_HOOK_INIT(task_wait, selinux_task_wait),
6376 LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode),
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09006377
Casey Schauflere20b0432015-05-02 15:11:36 -07006378 LSM_HOOK_INIT(ipc_permission, selinux_ipc_permission),
6379 LSM_HOOK_INIT(ipc_getsecid, selinux_ipc_getsecid),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006380
Casey Schauflere20b0432015-05-02 15:11:36 -07006381 LSM_HOOK_INIT(msg_msg_alloc_security, selinux_msg_msg_alloc_security),
6382 LSM_HOOK_INIT(msg_msg_free_security, selinux_msg_msg_free_security),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006383
Casey Schauflere20b0432015-05-02 15:11:36 -07006384 LSM_HOOK_INIT(msg_queue_alloc_security,
6385 selinux_msg_queue_alloc_security),
6386 LSM_HOOK_INIT(msg_queue_free_security, selinux_msg_queue_free_security),
6387 LSM_HOOK_INIT(msg_queue_associate, selinux_msg_queue_associate),
6388 LSM_HOOK_INIT(msg_queue_msgctl, selinux_msg_queue_msgctl),
6389 LSM_HOOK_INIT(msg_queue_msgsnd, selinux_msg_queue_msgsnd),
6390 LSM_HOOK_INIT(msg_queue_msgrcv, selinux_msg_queue_msgrcv),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006391
Casey Schauflere20b0432015-05-02 15:11:36 -07006392 LSM_HOOK_INIT(shm_alloc_security, selinux_shm_alloc_security),
6393 LSM_HOOK_INIT(shm_free_security, selinux_shm_free_security),
6394 LSM_HOOK_INIT(shm_associate, selinux_shm_associate),
6395 LSM_HOOK_INIT(shm_shmctl, selinux_shm_shmctl),
6396 LSM_HOOK_INIT(shm_shmat, selinux_shm_shmat),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006397
Casey Schauflere20b0432015-05-02 15:11:36 -07006398 LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security),
6399 LSM_HOOK_INIT(sem_free_security, selinux_sem_free_security),
6400 LSM_HOOK_INIT(sem_associate, selinux_sem_associate),
6401 LSM_HOOK_INIT(sem_semctl, selinux_sem_semctl),
6402 LSM_HOOK_INIT(sem_semop, selinux_sem_semop),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006403
Casey Schauflere20b0432015-05-02 15:11:36 -07006404 LSM_HOOK_INIT(d_instantiate, selinux_d_instantiate),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006405
Casey Schauflere20b0432015-05-02 15:11:36 -07006406 LSM_HOOK_INIT(getprocattr, selinux_getprocattr),
6407 LSM_HOOK_INIT(setprocattr, selinux_setprocattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006408
Casey Schauflere20b0432015-05-02 15:11:36 -07006409 LSM_HOOK_INIT(ismaclabel, selinux_ismaclabel),
6410 LSM_HOOK_INIT(secid_to_secctx, selinux_secid_to_secctx),
6411 LSM_HOOK_INIT(secctx_to_secid, selinux_secctx_to_secid),
6412 LSM_HOOK_INIT(release_secctx, selinux_release_secctx),
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006413 LSM_HOOK_INIT(inode_invalidate_secctx, selinux_inode_invalidate_secctx),
Casey Schauflere20b0432015-05-02 15:11:36 -07006414 LSM_HOOK_INIT(inode_notifysecctx, selinux_inode_notifysecctx),
6415 LSM_HOOK_INIT(inode_setsecctx, selinux_inode_setsecctx),
6416 LSM_HOOK_INIT(inode_getsecctx, selinux_inode_getsecctx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006417
Casey Schauflere20b0432015-05-02 15:11:36 -07006418 LSM_HOOK_INIT(unix_stream_connect, selinux_socket_unix_stream_connect),
6419 LSM_HOOK_INIT(unix_may_send, selinux_socket_unix_may_send),
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006420
Casey Schauflere20b0432015-05-02 15:11:36 -07006421 LSM_HOOK_INIT(socket_create, selinux_socket_create),
6422 LSM_HOOK_INIT(socket_post_create, selinux_socket_post_create),
6423 LSM_HOOK_INIT(socket_bind, selinux_socket_bind),
6424 LSM_HOOK_INIT(socket_connect, selinux_socket_connect),
6425 LSM_HOOK_INIT(socket_listen, selinux_socket_listen),
6426 LSM_HOOK_INIT(socket_accept, selinux_socket_accept),
6427 LSM_HOOK_INIT(socket_sendmsg, selinux_socket_sendmsg),
6428 LSM_HOOK_INIT(socket_recvmsg, selinux_socket_recvmsg),
6429 LSM_HOOK_INIT(socket_getsockname, selinux_socket_getsockname),
6430 LSM_HOOK_INIT(socket_getpeername, selinux_socket_getpeername),
6431 LSM_HOOK_INIT(socket_getsockopt, selinux_socket_getsockopt),
6432 LSM_HOOK_INIT(socket_setsockopt, selinux_socket_setsockopt),
6433 LSM_HOOK_INIT(socket_shutdown, selinux_socket_shutdown),
6434 LSM_HOOK_INIT(socket_sock_rcv_skb, selinux_socket_sock_rcv_skb),
6435 LSM_HOOK_INIT(socket_getpeersec_stream,
6436 selinux_socket_getpeersec_stream),
6437 LSM_HOOK_INIT(socket_getpeersec_dgram, selinux_socket_getpeersec_dgram),
6438 LSM_HOOK_INIT(sk_alloc_security, selinux_sk_alloc_security),
6439 LSM_HOOK_INIT(sk_free_security, selinux_sk_free_security),
6440 LSM_HOOK_INIT(sk_clone_security, selinux_sk_clone_security),
6441 LSM_HOOK_INIT(sk_getsecid, selinux_sk_getsecid),
6442 LSM_HOOK_INIT(sock_graft, selinux_sock_graft),
6443 LSM_HOOK_INIT(inet_conn_request, selinux_inet_conn_request),
6444 LSM_HOOK_INIT(inet_csk_clone, selinux_inet_csk_clone),
6445 LSM_HOOK_INIT(inet_conn_established, selinux_inet_conn_established),
6446 LSM_HOOK_INIT(secmark_relabel_packet, selinux_secmark_relabel_packet),
6447 LSM_HOOK_INIT(secmark_refcount_inc, selinux_secmark_refcount_inc),
6448 LSM_HOOK_INIT(secmark_refcount_dec, selinux_secmark_refcount_dec),
6449 LSM_HOOK_INIT(req_classify_flow, selinux_req_classify_flow),
6450 LSM_HOOK_INIT(tun_dev_alloc_security, selinux_tun_dev_alloc_security),
6451 LSM_HOOK_INIT(tun_dev_free_security, selinux_tun_dev_free_security),
6452 LSM_HOOK_INIT(tun_dev_create, selinux_tun_dev_create),
6453 LSM_HOOK_INIT(tun_dev_attach_queue, selinux_tun_dev_attach_queue),
6454 LSM_HOOK_INIT(tun_dev_attach, selinux_tun_dev_attach),
6455 LSM_HOOK_INIT(tun_dev_open, selinux_tun_dev_open),
Trent Jaegerd28d1e02005-12-13 23:12:40 -08006456
6457#ifdef CONFIG_SECURITY_NETWORK_XFRM
Casey Schauflere20b0432015-05-02 15:11:36 -07006458 LSM_HOOK_INIT(xfrm_policy_alloc_security, selinux_xfrm_policy_alloc),
6459 LSM_HOOK_INIT(xfrm_policy_clone_security, selinux_xfrm_policy_clone),
6460 LSM_HOOK_INIT(xfrm_policy_free_security, selinux_xfrm_policy_free),
6461 LSM_HOOK_INIT(xfrm_policy_delete_security, selinux_xfrm_policy_delete),
6462 LSM_HOOK_INIT(xfrm_state_alloc, selinux_xfrm_state_alloc),
6463 LSM_HOOK_INIT(xfrm_state_alloc_acquire,
6464 selinux_xfrm_state_alloc_acquire),
6465 LSM_HOOK_INIT(xfrm_state_free_security, selinux_xfrm_state_free),
6466 LSM_HOOK_INIT(xfrm_state_delete_security, selinux_xfrm_state_delete),
6467 LSM_HOOK_INIT(xfrm_policy_lookup, selinux_xfrm_policy_lookup),
6468 LSM_HOOK_INIT(xfrm_state_pol_flow_match,
6469 selinux_xfrm_state_pol_flow_match),
6470 LSM_HOOK_INIT(xfrm_decode_session, selinux_xfrm_decode_session),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006471#endif
Michael LeMayd7200242006-06-22 14:47:17 -07006472
6473#ifdef CONFIG_KEYS
Casey Schauflere20b0432015-05-02 15:11:36 -07006474 LSM_HOOK_INIT(key_alloc, selinux_key_alloc),
6475 LSM_HOOK_INIT(key_free, selinux_key_free),
6476 LSM_HOOK_INIT(key_permission, selinux_key_permission),
6477 LSM_HOOK_INIT(key_getsecurity, selinux_key_getsecurity),
Michael LeMayd7200242006-06-22 14:47:17 -07006478#endif
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02006479
6480#ifdef CONFIG_AUDIT
Casey Schauflere20b0432015-05-02 15:11:36 -07006481 LSM_HOOK_INIT(audit_rule_init, selinux_audit_rule_init),
6482 LSM_HOOK_INIT(audit_rule_known, selinux_audit_rule_known),
6483 LSM_HOOK_INIT(audit_rule_match, selinux_audit_rule_match),
6484 LSM_HOOK_INIT(audit_rule_free, selinux_audit_rule_free),
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02006485#endif
Chenbo Feng9b629132017-10-18 13:00:25 -07006486
6487#ifdef CONFIG_BPF_SYSCALL
6488 LSM_HOOK_INIT(bpf, selinux_bpf),
6489 LSM_HOOK_INIT(bpf_map, selinux_bpf_map),
6490 LSM_HOOK_INIT(bpf_prog, selinux_bpf_prog),
6491 LSM_HOOK_INIT(bpf_map_alloc_security, selinux_bpf_map_alloc),
6492 LSM_HOOK_INIT(bpf_prog_alloc_security, selinux_bpf_prog_alloc),
6493 LSM_HOOK_INIT(bpf_map_free_security, selinux_bpf_map_free),
6494 LSM_HOOK_INIT(bpf_prog_free_security, selinux_bpf_prog_free),
6495#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006496};
6497
6498static __init int selinux_init(void)
6499{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07006500 if (!security_module_enable("selinux")) {
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02006501 selinux_enabled = 0;
6502 return 0;
6503 }
6504
Linus Torvalds1da177e2005-04-16 15:20:36 -07006505 if (!selinux_enabled) {
6506 printk(KERN_INFO "SELinux: Disabled at boot.\n");
6507 return 0;
6508 }
6509
6510 printk(KERN_INFO "SELinux: Initializing.\n");
6511
6512 /* Set the security state for the initial task. */
David Howellsd84f4f92008-11-14 10:39:23 +11006513 cred_init_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006514
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04006515 default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
6516
James Morris7cae7e22006-03-22 00:09:22 -08006517 sel_inode_cache = kmem_cache_create("selinux_inode_security",
6518 sizeof(struct inode_security_struct),
Paul Mundt20c2df82007-07-20 10:11:58 +09006519 0, SLAB_PANIC, NULL);
Sangwoo63205652015-10-21 17:44:30 -04006520 file_security_cache = kmem_cache_create("selinux_file_security",
6521 sizeof(struct file_security_struct),
6522 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006523 avc_init();
6524
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07006525 security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006526
Paul Moore615e51f2014-06-26 14:33:56 -04006527 if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET))
6528 panic("SELinux: Unable to register AVC netcache callback\n");
6529
Eric Paris828dfe12008-04-17 13:17:49 -04006530 if (selinux_enforcing)
Eric Parisfadcdb42007-02-22 18:11:31 -05006531 printk(KERN_DEBUG "SELinux: Starting in enforcing mode\n");
Eric Paris828dfe12008-04-17 13:17:49 -04006532 else
Eric Parisfadcdb42007-02-22 18:11:31 -05006533 printk(KERN_DEBUG "SELinux: Starting in permissive mode\n");
Michael LeMayd7200242006-06-22 14:47:17 -07006534
Linus Torvalds1da177e2005-04-16 15:20:36 -07006535 return 0;
6536}
6537
Al Viroe8c26252010-03-23 06:36:54 -04006538static void delayed_superblock_init(struct super_block *sb, void *unused)
6539{
6540 superblock_doinit(sb, NULL);
6541}
6542
Linus Torvalds1da177e2005-04-16 15:20:36 -07006543void selinux_complete_init(void)
6544{
Eric Parisfadcdb42007-02-22 18:11:31 -05006545 printk(KERN_DEBUG "SELinux: Completing initialization.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006546
6547 /* Set up any superblocks initialized prior to the policy load. */
Eric Parisfadcdb42007-02-22 18:11:31 -05006548 printk(KERN_DEBUG "SELinux: Setting up existing superblocks.\n");
Al Viroe8c26252010-03-23 06:36:54 -04006549 iterate_supers(delayed_superblock_init, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006550}
6551
6552/* SELinux requires early initialization in order to label
6553 all processes and objects when they are created. */
6554security_initcall(selinux_init);
6555
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08006556#if defined(CONFIG_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006557
Jiri Pirko25db6be2014-09-03 17:42:13 +02006558static struct nf_hook_ops selinux_nf_ops[] = {
Paul Mooreeffad8d2008-01-29 08:49:27 -05006559 {
6560 .hook = selinux_ipv4_postroute,
Alban Crequy2597a832012-05-14 03:56:39 +00006561 .pf = NFPROTO_IPV4,
Paul Mooreeffad8d2008-01-29 08:49:27 -05006562 .hooknum = NF_INET_POST_ROUTING,
6563 .priority = NF_IP_PRI_SELINUX_LAST,
6564 },
6565 {
6566 .hook = selinux_ipv4_forward,
Alban Crequy2597a832012-05-14 03:56:39 +00006567 .pf = NFPROTO_IPV4,
Paul Mooreeffad8d2008-01-29 08:49:27 -05006568 .hooknum = NF_INET_FORWARD,
6569 .priority = NF_IP_PRI_SELINUX_FIRST,
Paul Moore948bf852008-10-10 10:16:32 -04006570 },
6571 {
6572 .hook = selinux_ipv4_output,
Alban Crequy2597a832012-05-14 03:56:39 +00006573 .pf = NFPROTO_IPV4,
Paul Moore948bf852008-10-10 10:16:32 -04006574 .hooknum = NF_INET_LOCAL_OUT,
6575 .priority = NF_IP_PRI_SELINUX_FIRST,
Jiri Pirko25db6be2014-09-03 17:42:13 +02006576 },
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04006577#if IS_ENABLED(CONFIG_IPV6)
Paul Mooreeffad8d2008-01-29 08:49:27 -05006578 {
6579 .hook = selinux_ipv6_postroute,
Alban Crequy2597a832012-05-14 03:56:39 +00006580 .pf = NFPROTO_IPV6,
Paul Mooreeffad8d2008-01-29 08:49:27 -05006581 .hooknum = NF_INET_POST_ROUTING,
6582 .priority = NF_IP6_PRI_SELINUX_LAST,
6583 },
6584 {
6585 .hook = selinux_ipv6_forward,
Alban Crequy2597a832012-05-14 03:56:39 +00006586 .pf = NFPROTO_IPV6,
Paul Mooreeffad8d2008-01-29 08:49:27 -05006587 .hooknum = NF_INET_FORWARD,
6588 .priority = NF_IP6_PRI_SELINUX_FIRST,
Jiri Pirko25db6be2014-09-03 17:42:13 +02006589 },
Huw Davies2917f572016-06-27 15:06:15 -04006590 {
6591 .hook = selinux_ipv6_output,
6592 .pf = NFPROTO_IPV6,
6593 .hooknum = NF_INET_LOCAL_OUT,
6594 .priority = NF_IP6_PRI_SELINUX_FIRST,
6595 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07006596#endif /* IPV6 */
Jiri Pirko25db6be2014-09-03 17:42:13 +02006597};
Linus Torvalds1da177e2005-04-16 15:20:36 -07006598
6599static int __init selinux_nf_ip_init(void)
6600{
Jiri Pirko25db6be2014-09-03 17:42:13 +02006601 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006602
6603 if (!selinux_enabled)
Jiri Pirko25db6be2014-09-03 17:42:13 +02006604 return 0;
Eric Parisfadcdb42007-02-22 18:11:31 -05006605
6606 printk(KERN_DEBUG "SELinux: Registering netfilter hooks\n");
6607
Jiri Pirko25db6be2014-09-03 17:42:13 +02006608 err = nf_register_hooks(selinux_nf_ops, ARRAY_SIZE(selinux_nf_ops));
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07006609 if (err)
Jiri Pirko25db6be2014-09-03 17:42:13 +02006610 panic("SELinux: nf_register_hooks: error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006611
Jiri Pirko25db6be2014-09-03 17:42:13 +02006612 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006613}
6614
6615__initcall(selinux_nf_ip_init);
6616
6617#ifdef CONFIG_SECURITY_SELINUX_DISABLE
6618static void selinux_nf_ip_exit(void)
6619{
Eric Parisfadcdb42007-02-22 18:11:31 -05006620 printk(KERN_DEBUG "SELinux: Unregistering netfilter hooks\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006621
Jiri Pirko25db6be2014-09-03 17:42:13 +02006622 nf_unregister_hooks(selinux_nf_ops, ARRAY_SIZE(selinux_nf_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006623}
6624#endif
6625
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08006626#else /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006627
6628#ifdef CONFIG_SECURITY_SELINUX_DISABLE
6629#define selinux_nf_ip_exit()
6630#endif
6631
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08006632#endif /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006633
6634#ifdef CONFIG_SECURITY_SELINUX_DISABLE
Eric Paris828dfe12008-04-17 13:17:49 -04006635static int selinux_disabled;
6636
Linus Torvalds1da177e2005-04-16 15:20:36 -07006637int selinux_disable(void)
6638{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006639 if (ss_initialized) {
6640 /* Not permitted after initial policy load. */
6641 return -EINVAL;
6642 }
6643
6644 if (selinux_disabled) {
6645 /* Only do this once. */
6646 return -EINVAL;
6647 }
6648
6649 printk(KERN_INFO "SELinux: Disabled at runtime.\n");
6650
6651 selinux_disabled = 1;
Stephen Smalley30d55282006-05-03 10:52:36 -04006652 selinux_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006653
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07006654 security_delete_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006655
Eric Parisaf8ff042009-09-20 21:23:01 -04006656 /* Try to destroy the avc node cache */
6657 avc_disable();
6658
Linus Torvalds1da177e2005-04-16 15:20:36 -07006659 /* Unregister netfilter hooks. */
6660 selinux_nf_ip_exit();
6661
6662 /* Unregister selinuxfs. */
6663 exit_sel_fs();
6664
6665 return 0;
6666}
6667#endif