blob: 81e012c66d95d756ab929479a093392b5be51700 [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 *
Stephen Smalley7efbb602017-08-17 13:32:36 -04006 * Authors: Stephen Smalley, <sds@tycho.nsa.gov>
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>
Daniel Jurgens3a976fa2017-05-19 15:48:56 +030020 * Copyright (C) 2016 Mellanox Technologies
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 *
22 * This program is free software; you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License version 2,
Eric Paris828dfe12008-04-17 13:17:49 -040024 * as published by the Free Software Foundation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 */
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/init.h>
Eric Paris0b24dcb2011-02-25 15:39:20 -050028#include <linux/kd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/kernel.h>
Roland McGrath0d094ef2008-07-25 19:45:49 -070030#include <linux/tracehook.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/errno.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010032#include <linux/sched/signal.h>
Ingo Molnar29930022017-02-08 18:51:36 +010033#include <linux/sched/task.h>
Casey Schaufler3c4ed7b2015-05-02 15:10:46 -070034#include <linux/lsm_hooks.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/xattr.h>
36#include <linux/capability.h>
37#include <linux/unistd.h>
38#include <linux/mm.h>
39#include <linux/mman.h>
40#include <linux/slab.h>
41#include <linux/pagemap.h>
Eric Paris0b24dcb2011-02-25 15:39:20 -050042#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/swap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/spinlock.h>
45#include <linux/syscalls.h>
Eric Paris2a7dba32011-02-01 11:05:39 -050046#include <linux/dcache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/file.h>
Al Viro9f3acc32008-04-24 07:44:08 -040048#include <linux/fdtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/namei.h>
50#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/netfilter_ipv4.h>
52#include <linux/netfilter_ipv6.h>
53#include <linux/tty.h>
54#include <net/icmp.h>
Stephen Hemminger227b60f2007-10-10 17:30:46 -070055#include <net/ip.h> /* for local_port_range[] */
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <net/tcp.h> /* struct or_callable used in sock_rcv_skb */
Paul Moore47180062013-12-04 16:10:45 -050057#include <net/inet_connection_sock.h>
Paul Moore220deb92008-01-29 08:38:23 -050058#include <net/net_namespace.h>
Paul Moored621d352008-01-29 08:43:36 -050059#include <net/netlabel.h>
Eric Parisf5269712008-05-14 11:27:45 -040060#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <asm/ioctls.h>
Arun Sharma600634972011-07-26 16:09:06 -070062#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <linux/bitops.h>
64#include <linux/interrupt.h>
65#include <linux/netdevice.h> /* for network interface checks */
Hong zhi guo77954982013-03-27 06:49:35 +000066#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <linux/tcp.h>
68#include <linux/udp.h>
James Morris2ee92d42006-11-13 16:09:01 -080069#include <linux/dccp.h>
Richard Hainesd4529302018-02-13 20:57:18 +000070#include <linux/sctp.h>
71#include <net/sctp/structs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <linux/quota.h>
73#include <linux/un.h> /* for Unix socket types */
74#include <net/af_unix.h> /* for Unix socket types */
75#include <linux/parser.h>
76#include <linux/nfs_mount.h>
77#include <net/ipv6.h>
78#include <linux/hugetlb.h>
79#include <linux/personality.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#include <linux/audit.h>
Eric Paris6931dfc2005-06-30 02:58:51 -070081#include <linux/string.h>
Catherine Zhang877ce7c2006-06-29 12:27:47 -070082#include <linux/selinux.h>
Eric Paris23970742006-09-25 23:32:01 -070083#include <linux/mutex.h>
Frank Mayharf06febc2008-09-12 09:54:39 -070084#include <linux/posix-timers.h>
Kees Cook00234592010-02-03 15:36:43 -080085#include <linux/syslog.h>
Serge E. Hallyn34867402011-03-23 16:43:17 -070086#include <linux/user_namespace.h>
Paul Gortmaker44fc7ea2011-05-26 20:52:10 -040087#include <linux/export.h>
Al Viro40401532012-02-13 03:58:52 +000088#include <linux/msg.h>
89#include <linux/shm.h>
Chenbo Fengec27c352017-10-18 13:00:25 -070090#include <linux/bpf.h>
David Howellse262e32d2018-11-01 23:07:23 +000091#include <uapi/linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93#include "avc.h"
94#include "objsec.h"
95#include "netif.h"
Paul Moore224dfbd2008-01-29 08:38:13 -050096#include "netnode.h"
Paul Moore3e1121722008-04-10 10:48:14 -040097#include "netport.h"
Daniel Jurgens409dcf32017-05-19 15:48:59 +030098#include "ibpkey.h"
Trent Jaegerd28d1e02005-12-13 23:12:40 -080099#include "xfrm.h"
Paul Moorec60475b2007-02-28 15:14:23 -0500100#include "netlabel.h"
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +0200101#include "audit.h"
James Morris7b98a582011-08-30 12:52:32 +1000102#include "avc_ss.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500104struct selinux_state selinux_state;
105
Paul Moored621d352008-01-29 08:43:36 -0500106/* SECMARK reference count */
James Morris56a4ca92011-08-17 11:08:43 +1000107static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0);
Paul Moored621d352008-01-29 08:43:36 -0500108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109#ifdef CONFIG_SECURITY_SELINUX_DEVELOP
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500110static int selinux_enforcing_boot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112static int __init enforcing_setup(char *str)
113{
Eric Parisf5269712008-05-14 11:27:45 -0400114 unsigned long enforcing;
Jingoo Han29707b22014-02-05 15:13:14 +0900115 if (!kstrtoul(str, 0, &enforcing))
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500116 selinux_enforcing_boot = enforcing ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return 1;
118}
119__setup("enforcing=", enforcing_setup);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500120#else
121#define selinux_enforcing_boot 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122#endif
123
124#ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM
125int selinux_enabled = CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE;
126
127static int __init selinux_enabled_setup(char *str)
128{
Eric Parisf5269712008-05-14 11:27:45 -0400129 unsigned long enabled;
Jingoo Han29707b22014-02-05 15:13:14 +0900130 if (!kstrtoul(str, 0, &enabled))
Eric Parisf5269712008-05-14 11:27:45 -0400131 selinux_enabled = enabled ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 return 1;
133}
134__setup("selinux=", selinux_enabled_setup);
Stephen Smalley30d55282006-05-03 10:52:36 -0400135#else
136int selinux_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137#endif
138
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500139static unsigned int selinux_checkreqprot_boot =
140 CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE;
141
142static int __init checkreqprot_setup(char *str)
143{
144 unsigned long checkreqprot;
145
146 if (!kstrtoul(str, 0, &checkreqprot))
147 selinux_checkreqprot_boot = checkreqprot ? 1 : 0;
148 return 1;
149}
150__setup("checkreqprot=", checkreqprot_setup);
151
Christoph Lametere18b8902006-12-06 20:33:20 -0800152static struct kmem_cache *sel_inode_cache;
Sangwoo63205652015-10-21 17:44:30 -0400153static struct kmem_cache *file_security_cache;
James Morris7cae7e22006-03-22 00:09:22 -0800154
Paul Moored621d352008-01-29 08:43:36 -0500155/**
156 * selinux_secmark_enabled - Check to see if SECMARK is currently enabled
157 *
158 * Description:
159 * This function checks the SECMARK reference counter to see if any SECMARK
160 * targets are currently configured, if the reference counter is greater than
161 * zero SECMARK is considered to be enabled. Returns true (1) if SECMARK is
Chris PeBenito2be4d742013-05-03 09:05:39 -0400162 * enabled, false (0) if SECMARK is disabled. If the always_check_network
163 * policy capability is enabled, SECMARK is always considered enabled.
Paul Moored621d352008-01-29 08:43:36 -0500164 *
165 */
166static int selinux_secmark_enabled(void)
167{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500168 return (selinux_policycap_alwaysnetwork() ||
169 atomic_read(&selinux_secmark_refcount));
Chris PeBenito2be4d742013-05-03 09:05:39 -0400170}
171
172/**
173 * selinux_peerlbl_enabled - Check to see if peer labeling is currently enabled
174 *
175 * Description:
176 * This function checks if NetLabel or labeled IPSEC is enabled. Returns true
177 * (1) if any are enabled or false (0) if neither are enabled. If the
178 * always_check_network policy capability is enabled, peer labeling
179 * is always considered enabled.
180 *
181 */
182static int selinux_peerlbl_enabled(void)
183{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500184 return (selinux_policycap_alwaysnetwork() ||
185 netlbl_enabled() || selinux_xfrm_enabled());
Paul Moored621d352008-01-29 08:43:36 -0500186}
187
Paul Moore615e51f2014-06-26 14:33:56 -0400188static int selinux_netcache_avc_callback(u32 event)
189{
190 if (event == AVC_CALLBACK_RESET) {
191 sel_netif_flush();
192 sel_netnode_flush();
193 sel_netport_flush();
194 synchronize_net();
195 }
196 return 0;
197}
198
Daniel Jurgens8f408ab2017-05-19 15:48:53 +0300199static int selinux_lsm_notifier_avc_callback(u32 event)
200{
Daniel Jurgens409dcf32017-05-19 15:48:59 +0300201 if (event == AVC_CALLBACK_RESET) {
202 sel_ib_pkey_flush();
Daniel Jurgens8f408ab2017-05-19 15:48:53 +0300203 call_lsm_notifier(LSM_POLICY_CHANGE, NULL);
Daniel Jurgens409dcf32017-05-19 15:48:59 +0300204 }
Daniel Jurgens8f408ab2017-05-19 15:48:53 +0300205
206 return 0;
207}
208
David Howellsd84f4f92008-11-14 10:39:23 +1100209/*
210 * initialise the security for the init task
211 */
212static void cred_init_security(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
David Howells3b11a1d2008-11-14 10:39:26 +1100214 struct cred *cred = (struct cred *) current->real_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 struct task_security_struct *tsec;
216
James Morris89d155e2005-10-30 14:59:21 -0800217 tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 if (!tsec)
David Howellsd84f4f92008-11-14 10:39:23 +1100219 panic("SELinux: Failed to initialize initial task.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
David Howellsd84f4f92008-11-14 10:39:23 +1100221 tsec->osid = tsec->sid = SECINITSID_KERNEL;
David Howellsf1752ee2008-11-14 10:39:17 +1100222 cred->security = tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
David Howells275bb412008-11-14 10:39:19 +1100225/*
David Howells88e67f32008-11-14 10:39:21 +1100226 * get the security ID of a set of credentials
227 */
228static inline u32 cred_sid(const struct cred *cred)
229{
230 const struct task_security_struct *tsec;
231
232 tsec = cred->security;
233 return tsec->sid;
234}
235
236/*
David Howells3b11a1d2008-11-14 10:39:26 +1100237 * get the objective security ID of a task
David Howells275bb412008-11-14 10:39:19 +1100238 */
239static inline u32 task_sid(const struct task_struct *task)
240{
David Howells275bb412008-11-14 10:39:19 +1100241 u32 sid;
242
243 rcu_read_lock();
David Howells88e67f32008-11-14 10:39:21 +1100244 sid = cred_sid(__task_cred(task));
David Howells275bb412008-11-14 10:39:19 +1100245 rcu_read_unlock();
246 return sid;
247}
248
David Howells88e67f32008-11-14 10:39:21 +1100249/* Allocate and free functions for each kind of security blob. */
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251static int inode_alloc_security(struct inode *inode)
252{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 struct inode_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +1100254 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Josef Bacika02fe132008-04-04 09:35:05 +1100256 isec = kmem_cache_zalloc(sel_inode_cache, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 if (!isec)
258 return -ENOMEM;
259
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +0100260 spin_lock_init(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 INIT_LIST_HEAD(&isec->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 isec->inode = inode;
263 isec->sid = SECINITSID_UNLABELED;
264 isec->sclass = SECCLASS_FILE;
David Howells275bb412008-11-14 10:39:19 +1100265 isec->task_sid = sid;
Andreas Gruenbacher42059112016-11-10 22:18:27 +0100266 isec->initialized = LABEL_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 inode->i_security = isec;
268
269 return 0;
270}
271
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500272static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry);
273
274/*
275 * Try reloading inode security labels that have been marked as invalid. The
276 * @may_sleep parameter indicates when sleeping and thus reloading labels is
Andreas Gruenbacher42059112016-11-10 22:18:27 +0100277 * allowed; when set to false, returns -ECHILD when the label is
Al Viroe9193282018-04-24 21:31:02 -0400278 * invalid. The @dentry parameter should be set to a dentry of the inode.
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500279 */
280static int __inode_security_revalidate(struct inode *inode,
Al Viroe9193282018-04-24 21:31:02 -0400281 struct dentry *dentry,
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500282 bool may_sleep)
283{
284 struct inode_security_struct *isec = inode->i_security;
285
286 might_sleep_if(may_sleep);
287
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500288 if (selinux_state.initialized &&
289 isec->initialized != LABEL_INITIALIZED) {
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500290 if (!may_sleep)
291 return -ECHILD;
292
293 /*
294 * Try reloading the inode security label. This will fail if
295 * @opt_dentry is NULL and no dentry for this inode can be
296 * found; in that case, continue using the old label.
297 */
Al Viroe9193282018-04-24 21:31:02 -0400298 inode_doinit_with_dentry(inode, dentry);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500299 }
300 return 0;
301}
302
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500303static struct inode_security_struct *inode_security_novalidate(struct inode *inode)
304{
305 return inode->i_security;
306}
307
308static struct inode_security_struct *inode_security_rcu(struct inode *inode, bool rcu)
309{
310 int error;
311
312 error = __inode_security_revalidate(inode, NULL, !rcu);
313 if (error)
314 return ERR_PTR(error);
315 return inode->i_security;
316}
317
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500318/*
319 * Get the security label of an inode.
320 */
321static struct inode_security_struct *inode_security(struct inode *inode)
322{
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500323 __inode_security_revalidate(inode, NULL, true);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500324 return inode->i_security;
325}
326
Paul Moore2c971652016-04-19 16:36:28 -0400327static struct inode_security_struct *backing_inode_security_novalidate(struct dentry *dentry)
328{
329 struct inode *inode = d_backing_inode(dentry);
330
331 return inode->i_security;
332}
333
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500334/*
335 * Get the security label of a dentry's backing inode.
336 */
337static struct inode_security_struct *backing_inode_security(struct dentry *dentry)
338{
339 struct inode *inode = d_backing_inode(dentry);
340
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500341 __inode_security_revalidate(inode, dentry, true);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500342 return inode->i_security;
343}
344
Steven Rostedt3dc91d42014-01-09 21:46:34 -0500345static void inode_free_rcu(struct rcu_head *head)
346{
347 struct inode_security_struct *isec;
348
349 isec = container_of(head, struct inode_security_struct, rcu);
350 kmem_cache_free(sel_inode_cache, isec);
351}
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353static void inode_free_security(struct inode *inode)
354{
355 struct inode_security_struct *isec = inode->i_security;
356 struct superblock_security_struct *sbsec = inode->i_sb->s_security;
357
Waiman Long9629d042015-07-10 17:19:56 -0400358 /*
359 * As not all inode security structures are in a list, we check for
360 * empty list outside of the lock to make sure that we won't waste
361 * time taking a lock doing nothing.
362 *
363 * The list_del_init() function can be safely called more than once.
364 * It should not be possible for this function to be called with
365 * concurrent list_add(), but for better safety against future changes
366 * in the code, we use list_empty_careful() here.
367 */
368 if (!list_empty_careful(&isec->list)) {
369 spin_lock(&sbsec->isec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 list_del_init(&isec->list);
Waiman Long9629d042015-07-10 17:19:56 -0400371 spin_unlock(&sbsec->isec_lock);
372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Steven Rostedt3dc91d42014-01-09 21:46:34 -0500374 /*
375 * The inode may still be referenced in a path walk and
376 * a call to selinux_inode_permission() can be made
377 * after inode_free_security() is called. Ideally, the VFS
378 * wouldn't do this, but fixing that is a much harder
379 * job. For now, simply free the i_security via RCU, and
380 * leave the current inode->i_security pointer intact.
381 * The inode will be freed after the RCU grace period too.
382 */
383 call_rcu(&isec->rcu, inode_free_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
385
386static int file_alloc_security(struct file *file)
387{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 struct file_security_struct *fsec;
David Howells275bb412008-11-14 10:39:19 +1100389 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Sangwoo63205652015-10-21 17:44:30 -0400391 fsec = kmem_cache_zalloc(file_security_cache, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 if (!fsec)
393 return -ENOMEM;
394
David Howells275bb412008-11-14 10:39:19 +1100395 fsec->sid = sid;
396 fsec->fown_sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 file->f_security = fsec;
398
399 return 0;
400}
401
402static void file_free_security(struct file *file)
403{
404 struct file_security_struct *fsec = file->f_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 file->f_security = NULL;
Sangwoo63205652015-10-21 17:44:30 -0400406 kmem_cache_free(file_security_cache, fsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
409static int superblock_alloc_security(struct super_block *sb)
410{
411 struct superblock_security_struct *sbsec;
412
James Morris89d155e2005-10-30 14:59:21 -0800413 sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (!sbsec)
415 return -ENOMEM;
416
Eric Parisbc7e9822006-09-25 23:32:02 -0700417 mutex_init(&sbsec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 INIT_LIST_HEAD(&sbsec->isec_head);
419 spin_lock_init(&sbsec->isec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 sbsec->sb = sb;
421 sbsec->sid = SECINITSID_UNLABELED;
422 sbsec->def_sid = SECINITSID_FILE;
Eric Parisc312feb2006-07-10 04:43:53 -0700423 sbsec->mntpoint_sid = SECINITSID_UNLABELED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 sb->s_security = sbsec;
425
426 return 0;
427}
428
429static void superblock_free_security(struct super_block *sb)
430{
431 struct superblock_security_struct *sbsec = sb->s_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 sb->s_security = NULL;
433 kfree(sbsec);
434}
435
Al Virobd323652018-12-13 15:04:59 -0500436struct selinux_mnt_opts {
437 const char *fscontext, *context, *rootcontext, *defcontext;
438};
439
Al Viro204cc0c2018-12-13 13:41:47 -0500440static void selinux_free_mnt_opts(void *mnt_opts)
441{
Al Virobd323652018-12-13 15:04:59 -0500442 struct selinux_mnt_opts *opts = mnt_opts;
443 kfree(opts->fscontext);
444 kfree(opts->context);
445 kfree(opts->rootcontext);
446 kfree(opts->defcontext);
Al Viro204cc0c2018-12-13 13:41:47 -0500447 kfree(opts);
448}
449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450static inline int inode_doinit(struct inode *inode)
451{
452 return inode_doinit_with_dentry(inode, NULL);
453}
454
455enum {
Eric Paris31e87932007-09-19 17:19:12 -0400456 Opt_error = -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 Opt_context = 1,
458 Opt_fscontext = 2,
Eric Parisc9180a52007-11-30 13:00:35 -0500459 Opt_defcontext = 3,
460 Opt_rootcontext = 4,
Al Viroda3d76a2018-12-17 10:14:16 -0500461 Opt_seclabel = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462};
463
Al Viroda3d76a2018-12-17 10:14:16 -0500464#define A(s, has_arg) {#s, sizeof(#s) - 1, Opt_##s, has_arg}
Al Viro169d68efb2018-12-14 22:44:50 -0500465static struct {
466 const char *name;
467 int len;
468 int opt;
469 bool has_arg;
470} tokens[] = {
Al Viroda3d76a2018-12-17 10:14:16 -0500471 A(context, true),
472 A(fscontext, true),
473 A(defcontext, true),
474 A(rootcontext, true),
475 A(seclabel, false),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476};
Al Viro169d68efb2018-12-14 22:44:50 -0500477#undef A
478
479static int match_opt_prefix(char *s, int l, char **arg)
480{
481 int i;
482
483 for (i = 0; i < ARRAY_SIZE(tokens); i++) {
484 size_t len = tokens[i].len;
485 if (len > l || memcmp(s, tokens[i].name, len))
486 continue;
487 if (tokens[i].has_arg) {
488 if (len == l || s[len] != '=')
489 continue;
490 *arg = s + len + 1;
491 } else if (len != l)
492 continue;
493 return tokens[i].opt;
494 }
495 return Opt_error;
496}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498#define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n"
499
Eric Parisc312feb2006-07-10 04:43:53 -0700500static int may_context_mount_sb_relabel(u32 sid,
501 struct superblock_security_struct *sbsec,
David Howells275bb412008-11-14 10:39:19 +1100502 const struct cred *cred)
Eric Parisc312feb2006-07-10 04:43:53 -0700503{
David Howells275bb412008-11-14 10:39:19 +1100504 const struct task_security_struct *tsec = cred->security;
Eric Parisc312feb2006-07-10 04:43:53 -0700505 int rc;
506
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500507 rc = avc_has_perm(&selinux_state,
508 tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
Eric Parisc312feb2006-07-10 04:43:53 -0700509 FILESYSTEM__RELABELFROM, NULL);
510 if (rc)
511 return rc;
512
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500513 rc = avc_has_perm(&selinux_state,
514 tsec->sid, sid, SECCLASS_FILESYSTEM,
Eric Parisc312feb2006-07-10 04:43:53 -0700515 FILESYSTEM__RELABELTO, NULL);
516 return rc;
517}
518
Eric Paris08089252006-07-10 04:43:55 -0700519static int may_context_mount_inode_relabel(u32 sid,
520 struct superblock_security_struct *sbsec,
David Howells275bb412008-11-14 10:39:19 +1100521 const struct cred *cred)
Eric Paris08089252006-07-10 04:43:55 -0700522{
David Howells275bb412008-11-14 10:39:19 +1100523 const struct task_security_struct *tsec = cred->security;
Eric Paris08089252006-07-10 04:43:55 -0700524 int rc;
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500525 rc = avc_has_perm(&selinux_state,
526 tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
Eric Paris08089252006-07-10 04:43:55 -0700527 FILESYSTEM__RELABELFROM, NULL);
528 if (rc)
529 return rc;
530
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500531 rc = avc_has_perm(&selinux_state,
532 sid, sbsec->sid, SECCLASS_FILESYSTEM,
Eric Paris08089252006-07-10 04:43:55 -0700533 FILESYSTEM__ASSOCIATE, NULL);
534 return rc;
535}
536
Ondrej Mosnaceka83d6dd2018-12-21 21:18:52 +0100537static int selinux_is_genfs_special_handling(struct super_block *sb)
Eric Parisb43e7252012-10-10 14:27:35 -0400538{
Ondrej Mosnaceka83d6dd2018-12-21 21:18:52 +0100539 /* Special handling. Genfs but also in-core setxattr handler */
540 return !strcmp(sb->s_type->name, "sysfs") ||
Mark Salyzynd5f3a5f2015-02-04 11:34:30 -0500541 !strcmp(sb->s_type->name, "pstore") ||
542 !strcmp(sb->s_type->name, "debugfs") ||
Yongqin Liua2c7c6f2017-01-09 10:07:30 -0500543 !strcmp(sb->s_type->name, "tracefs") ||
Stephen Smalley2651225b2017-02-28 10:35:56 -0500544 !strcmp(sb->s_type->name, "rootfs") ||
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500545 (selinux_policycap_cgroupseclabel() &&
Stephen Smalley2651225b2017-02-28 10:35:56 -0500546 (!strcmp(sb->s_type->name, "cgroup") ||
547 !strcmp(sb->s_type->name, "cgroup2")));
Eric Parisb43e7252012-10-10 14:27:35 -0400548}
549
Ondrej Mosnaceka83d6dd2018-12-21 21:18:52 +0100550static int selinux_is_sblabel_mnt(struct super_block *sb)
551{
552 struct superblock_security_struct *sbsec = sb->s_security;
553
554 /*
555 * IMPORTANT: Double-check logic in this function when adding a new
556 * SECURITY_FS_USE_* definition!
557 */
558 BUILD_BUG_ON(SECURITY_FS_USE_MAX != 7);
559
560 switch (sbsec->behavior) {
561 case SECURITY_FS_USE_XATTR:
562 case SECURITY_FS_USE_TRANS:
563 case SECURITY_FS_USE_TASK:
564 case SECURITY_FS_USE_NATIVE:
565 return 1;
566
567 case SECURITY_FS_USE_GENFS:
568 return selinux_is_genfs_special_handling(sb);
569
570 /* Never allow relabeling on context mounts */
571 case SECURITY_FS_USE_MNTPOINT:
572 case SECURITY_FS_USE_NONE:
573 default:
574 return 0;
575 }
576}
577
Eric Parisc9180a52007-11-30 13:00:35 -0500578static int sb_finish_set_opts(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
580 struct superblock_security_struct *sbsec = sb->s_security;
581 struct dentry *root = sb->s_root;
David Howellsc6f493d2015-03-17 22:26:22 +0000582 struct inode *root_inode = d_backing_inode(root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 int rc = 0;
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
586 /* Make sure that the xattr handler exists and that no
587 error other than -ENODATA is returned by getxattr on
588 the root directory. -ENODATA is ok, as this may be
589 the first boot of the SELinux kernel before we have
590 assigned xattr values to the filesystem. */
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200591 if (!(root_inode->i_opflags & IOP_XATTR)) {
peter enderborgc103a912018-06-12 10:09:03 +0200592 pr_warn("SELinux: (dev %s, type %s) has no "
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800593 "xattr support\n", sb->s_id, sb->s_type->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 rc = -EOPNOTSUPP;
595 goto out;
596 }
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200597
598 rc = __vfs_getxattr(root, root_inode, XATTR_NAME_SELINUX, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 if (rc < 0 && rc != -ENODATA) {
600 if (rc == -EOPNOTSUPP)
peter enderborgc103a912018-06-12 10:09:03 +0200601 pr_warn("SELinux: (dev %s, type "
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800602 "%s) has no security xattr handler\n",
603 sb->s_id, sb->s_type->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 else
peter enderborgc103a912018-06-12 10:09:03 +0200605 pr_warn("SELinux: (dev %s, type "
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800606 "%s) getxattr errno %d\n", sb->s_id,
607 sb->s_type->name, -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 goto out;
609 }
610 }
611
Eric Pariseadcabc2012-08-24 15:59:14 -0400612 sbsec->flags |= SE_SBINITIALIZED;
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400613
614 /*
615 * Explicitly set or clear SBLABEL_MNT. It's not sufficient to simply
616 * leave the flag untouched because sb_clone_mnt_opts might be handing
617 * us a superblock that needs the flag to be cleared.
618 */
Eric Parisb43e7252012-10-10 14:27:35 -0400619 if (selinux_is_sblabel_mnt(sb))
Eric Paris12f348b2012-10-09 10:56:25 -0400620 sbsec->flags |= SBLABEL_MNT;
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400621 else
622 sbsec->flags &= ~SBLABEL_MNT;
David P. Quigleyddd29ec2009-09-09 14:25:37 -0400623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 /* Initialize the root inode. */
Eric Parisc9180a52007-11-30 13:00:35 -0500625 rc = inode_doinit_with_dentry(root_inode, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627 /* Initialize any other inodes associated with the superblock, e.g.
628 inodes created prior to initial policy load or inodes created
629 during get_sb by a pseudo filesystem that directly
630 populates itself. */
631 spin_lock(&sbsec->isec_lock);
Al Viro8d641242018-12-10 15:34:12 -0500632 while (!list_empty(&sbsec->isec_head)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 struct inode_security_struct *isec =
Al Viro8d641242018-12-10 15:34:12 -0500634 list_first_entry(&sbsec->isec_head,
Eric Parisc9180a52007-11-30 13:00:35 -0500635 struct inode_security_struct, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 struct inode *inode = isec->inode;
Stephen Smalley923190d2014-10-06 16:32:52 -0400637 list_del_init(&isec->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 spin_unlock(&sbsec->isec_lock);
639 inode = igrab(inode);
640 if (inode) {
Eric Parisc9180a52007-11-30 13:00:35 -0500641 if (!IS_PRIVATE(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 inode_doinit(inode);
643 iput(inode);
644 }
645 spin_lock(&sbsec->isec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 }
647 spin_unlock(&sbsec->isec_lock);
648out:
Eric Parisc9180a52007-11-30 13:00:35 -0500649 return rc;
650}
651
Eric Parisc9180a52007-11-30 13:00:35 -0500652static int bad_option(struct superblock_security_struct *sbsec, char flag,
653 u32 old_sid, u32 new_sid)
654{
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500655 char mnt_flags = sbsec->flags & SE_MNTMASK;
656
Eric Parisc9180a52007-11-30 13:00:35 -0500657 /* check if the old mount command had the same options */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500658 if (sbsec->flags & SE_SBINITIALIZED)
Eric Parisc9180a52007-11-30 13:00:35 -0500659 if (!(sbsec->flags & flag) ||
660 (old_sid != new_sid))
661 return 1;
662
663 /* check if we were passed the same options twice,
664 * aka someone passed context=a,context=b
665 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500666 if (!(sbsec->flags & SE_SBINITIALIZED))
667 if (mnt_flags & flag)
Eric Parisc9180a52007-11-30 13:00:35 -0500668 return 1;
669 return 0;
670}
Eric Parise0007522008-03-05 10:31:54 -0500671
Al Virobd323652018-12-13 15:04:59 -0500672static int parse_sid(struct super_block *sb, const char *s, u32 *sid)
673{
674 int rc = security_context_str_to_sid(&selinux_state, s,
675 sid, GFP_KERNEL);
676 if (rc)
677 pr_warn("SELinux: security_context_str_to_sid"
678 "(%s) failed for (dev %s, type %s) errno=%d\n",
679 s, sb->s_id, sb->s_type->name, rc);
680 return rc;
681}
682
Eric Parisc9180a52007-11-30 13:00:35 -0500683/*
684 * Allow filesystems with binary mount data to explicitly set mount point
685 * labeling information.
686 */
Eric Parise0007522008-03-05 10:31:54 -0500687static int selinux_set_mnt_opts(struct super_block *sb,
Al Viro204cc0c2018-12-13 13:41:47 -0500688 void *mnt_opts,
David Quigley649f6e72013-05-22 12:50:36 -0400689 unsigned long kern_flags,
690 unsigned long *set_kern_flags)
Eric Parisc9180a52007-11-30 13:00:35 -0500691{
David Howells275bb412008-11-14 10:39:19 +1100692 const struct cred *cred = current_cred();
Eric Parisc9180a52007-11-30 13:00:35 -0500693 struct superblock_security_struct *sbsec = sb->s_security;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500694 struct dentry *root = sbsec->sb->s_root;
Al Virobd323652018-12-13 15:04:59 -0500695 struct selinux_mnt_opts *opts = mnt_opts;
Paul Moore2c971652016-04-19 16:36:28 -0400696 struct inode_security_struct *root_isec;
Eric Parisc9180a52007-11-30 13:00:35 -0500697 u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
698 u32 defcontext_sid = 0;
Al Virobd323652018-12-13 15:04:59 -0500699 int rc = 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500700
701 mutex_lock(&sbsec->lock);
702
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500703 if (!selinux_state.initialized) {
Al Virobd323652018-12-13 15:04:59 -0500704 if (!opts) {
Eric Parisc9180a52007-11-30 13:00:35 -0500705 /* Defer initialization until selinux_complete_init,
706 after the initial policy is loaded and the security
707 server is ready to handle calls. */
Eric Parisc9180a52007-11-30 13:00:35 -0500708 goto out;
709 }
710 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +0200711 pr_warn("SELinux: Unable to set superblock options "
Eric Paris744ba352008-04-17 11:52:44 -0400712 "before the security server is initialized\n");
Eric Parisc9180a52007-11-30 13:00:35 -0500713 goto out;
714 }
David Quigley649f6e72013-05-22 12:50:36 -0400715 if (kern_flags && !set_kern_flags) {
716 /* Specifying internal flags without providing a place to
717 * place the results is not allowed */
718 rc = -EINVAL;
719 goto out;
720 }
Eric Parisc9180a52007-11-30 13:00:35 -0500721
722 /*
Eric Parise0007522008-03-05 10:31:54 -0500723 * Binary mount data FS will come through this function twice. Once
724 * from an explicit call and once from the generic calls from the vfs.
725 * Since the generic VFS calls will not contain any security mount data
726 * we need to skip the double mount verification.
727 *
728 * This does open a hole in which we will not notice if the first
729 * mount using this sb set explict options and a second mount using
730 * this sb does not set any security options. (The first options
731 * will be used for both mounts)
732 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500733 if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
Al Virobd323652018-12-13 15:04:59 -0500734 && !opts)
Eric Parisf5269712008-05-14 11:27:45 -0400735 goto out;
Eric Parise0007522008-03-05 10:31:54 -0500736
Paul Moore2c971652016-04-19 16:36:28 -0400737 root_isec = backing_inode_security_novalidate(root);
738
Eric Parise0007522008-03-05 10:31:54 -0500739 /*
Eric Parisc9180a52007-11-30 13:00:35 -0500740 * parse the mount options, check if they are valid sids.
741 * also check if someone is trying to mount the same sb more
742 * than once with different security options.
743 */
Al Virobd323652018-12-13 15:04:59 -0500744 if (opts) {
745 if (opts->fscontext) {
746 rc = parse_sid(sb, opts->fscontext, &fscontext_sid);
747 if (rc)
748 goto out;
Eric Parisc9180a52007-11-30 13:00:35 -0500749 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
750 fscontext_sid))
751 goto out_double_mount;
Eric Parisc9180a52007-11-30 13:00:35 -0500752 sbsec->flags |= FSCONTEXT_MNT;
Al Virobd323652018-12-13 15:04:59 -0500753 }
754 if (opts->context) {
755 rc = parse_sid(sb, opts->context, &context_sid);
756 if (rc)
757 goto out;
Eric Parisc9180a52007-11-30 13:00:35 -0500758 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
759 context_sid))
760 goto out_double_mount;
Eric Parisc9180a52007-11-30 13:00:35 -0500761 sbsec->flags |= CONTEXT_MNT;
Al Virobd323652018-12-13 15:04:59 -0500762 }
763 if (opts->rootcontext) {
764 rc = parse_sid(sb, opts->rootcontext, &rootcontext_sid);
765 if (rc)
766 goto out;
Eric Parisc9180a52007-11-30 13:00:35 -0500767 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
768 rootcontext_sid))
769 goto out_double_mount;
Eric Parisc9180a52007-11-30 13:00:35 -0500770 sbsec->flags |= ROOTCONTEXT_MNT;
Al Virobd323652018-12-13 15:04:59 -0500771 }
772 if (opts->defcontext) {
773 rc = parse_sid(sb, opts->defcontext, &defcontext_sid);
774 if (rc)
775 goto out;
Eric Parisc9180a52007-11-30 13:00:35 -0500776 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
777 defcontext_sid))
778 goto out_double_mount;
Eric Parisc9180a52007-11-30 13:00:35 -0500779 sbsec->flags |= DEFCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500780 }
781 }
782
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500783 if (sbsec->flags & SE_SBINITIALIZED) {
Eric Parisc9180a52007-11-30 13:00:35 -0500784 /* previously mounted with options, but not on this attempt? */
Al Virobd323652018-12-13 15:04:59 -0500785 if ((sbsec->flags & SE_MNTMASK) && !opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500786 goto out_double_mount;
787 rc = 0;
788 goto out;
789 }
790
James Morris089be432008-07-15 18:32:49 +1000791 if (strcmp(sb->s_type->name, "proc") == 0)
Stephen Smalley134509d2015-06-04 16:22:17 -0400792 sbsec->flags |= SE_SBPROC | SE_SBGENFS;
793
Stephen Smalley8e014722015-06-04 16:22:17 -0400794 if (!strcmp(sb->s_type->name, "debugfs") ||
Jeff Vander Stoep6a391182017-06-20 09:35:33 -0700795 !strcmp(sb->s_type->name, "tracefs") ||
Stephen Smalley8e014722015-06-04 16:22:17 -0400796 !strcmp(sb->s_type->name, "sysfs") ||
Antonio Murdaca901ef842017-02-09 17:02:42 +0100797 !strcmp(sb->s_type->name, "pstore") ||
798 !strcmp(sb->s_type->name, "cgroup") ||
799 !strcmp(sb->s_type->name, "cgroup2"))
Stephen Smalley134509d2015-06-04 16:22:17 -0400800 sbsec->flags |= SE_SBGENFS;
Eric Parisc9180a52007-11-30 13:00:35 -0500801
David Quigleyeb9ae682013-05-22 12:50:37 -0400802 if (!sbsec->behavior) {
803 /*
804 * Determine the labeling behavior to use for this
805 * filesystem type.
806 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500807 rc = security_fs_use(&selinux_state, sb);
David Quigleyeb9ae682013-05-22 12:50:37 -0400808 if (rc) {
peter enderborgc103a912018-06-12 10:09:03 +0200809 pr_warn("%s: security_fs_use(%s) returned %d\n",
David Quigleyeb9ae682013-05-22 12:50:37 -0400810 __func__, sb->s_type->name, rc);
811 goto out;
812 }
Eric Parisc9180a52007-11-30 13:00:35 -0500813 }
Seth Forsheeaad82892016-04-26 14:36:20 -0500814
815 /*
Stephen Smalley01593d32017-01-09 10:07:31 -0500816 * If this is a user namespace mount and the filesystem type is not
817 * explicitly whitelisted, then no contexts are allowed on the command
818 * line and security labels must be ignored.
Seth Forsheeaad82892016-04-26 14:36:20 -0500819 */
Stephen Smalley01593d32017-01-09 10:07:31 -0500820 if (sb->s_user_ns != &init_user_ns &&
821 strcmp(sb->s_type->name, "tmpfs") &&
822 strcmp(sb->s_type->name, "ramfs") &&
823 strcmp(sb->s_type->name, "devpts")) {
Seth Forsheeaad82892016-04-26 14:36:20 -0500824 if (context_sid || fscontext_sid || rootcontext_sid ||
825 defcontext_sid) {
826 rc = -EACCES;
827 goto out;
828 }
829 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
830 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500831 rc = security_transition_sid(&selinux_state,
832 current_sid(),
833 current_sid(),
Seth Forsheeaad82892016-04-26 14:36:20 -0500834 SECCLASS_FILE, NULL,
835 &sbsec->mntpoint_sid);
836 if (rc)
837 goto out;
838 }
839 goto out_set_opts;
840 }
841
Eric Parisc9180a52007-11-30 13:00:35 -0500842 /* sets the context of the superblock for the fs being mounted. */
843 if (fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100844 rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500845 if (rc)
846 goto out;
847
848 sbsec->sid = fscontext_sid;
849 }
850
851 /*
852 * Switch to using mount point labeling behavior.
853 * sets the label used on all file below the mountpoint, and will set
854 * the superblock context if not already set.
855 */
David Quigleyeb9ae682013-05-22 12:50:37 -0400856 if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !context_sid) {
857 sbsec->behavior = SECURITY_FS_USE_NATIVE;
858 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
859 }
860
Eric Parisc9180a52007-11-30 13:00:35 -0500861 if (context_sid) {
862 if (!fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100863 rc = may_context_mount_sb_relabel(context_sid, sbsec,
864 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500865 if (rc)
866 goto out;
867 sbsec->sid = context_sid;
868 } else {
David Howells275bb412008-11-14 10:39:19 +1100869 rc = may_context_mount_inode_relabel(context_sid, sbsec,
870 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500871 if (rc)
872 goto out;
873 }
874 if (!rootcontext_sid)
875 rootcontext_sid = context_sid;
876
877 sbsec->mntpoint_sid = context_sid;
878 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
879 }
880
881 if (rootcontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100882 rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec,
883 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500884 if (rc)
885 goto out;
886
887 root_isec->sid = rootcontext_sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -0500888 root_isec->initialized = LABEL_INITIALIZED;
Eric Parisc9180a52007-11-30 13:00:35 -0500889 }
890
891 if (defcontext_sid) {
David Quigleyeb9ae682013-05-22 12:50:37 -0400892 if (sbsec->behavior != SECURITY_FS_USE_XATTR &&
893 sbsec->behavior != SECURITY_FS_USE_NATIVE) {
Eric Parisc9180a52007-11-30 13:00:35 -0500894 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +0200895 pr_warn("SELinux: defcontext option is "
Eric Parisc9180a52007-11-30 13:00:35 -0500896 "invalid for this filesystem type\n");
897 goto out;
898 }
899
900 if (defcontext_sid != sbsec->def_sid) {
901 rc = may_context_mount_inode_relabel(defcontext_sid,
David Howells275bb412008-11-14 10:39:19 +1100902 sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500903 if (rc)
904 goto out;
905 }
906
907 sbsec->def_sid = defcontext_sid;
908 }
909
Seth Forsheeaad82892016-04-26 14:36:20 -0500910out_set_opts:
Eric Parisc9180a52007-11-30 13:00:35 -0500911 rc = sb_finish_set_opts(sb);
912out:
Eric Parisbc7e9822006-09-25 23:32:02 -0700913 mutex_unlock(&sbsec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 return rc;
Eric Parisc9180a52007-11-30 13:00:35 -0500915out_double_mount:
916 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +0200917 pr_warn("SELinux: mount invalid. Same superblock, different "
Al Virobd323652018-12-13 15:04:59 -0500918 "security settings for (dev %s, type %s)\n", sb->s_id,
919 sb->s_type->name);
Eric Parisc9180a52007-11-30 13:00:35 -0500920 goto out;
921}
922
Jeff Layton094f7b62013-04-01 08:14:24 -0400923static int selinux_cmp_sb_context(const struct super_block *oldsb,
924 const struct super_block *newsb)
925{
926 struct superblock_security_struct *old = oldsb->s_security;
927 struct superblock_security_struct *new = newsb->s_security;
928 char oldflags = old->flags & SE_MNTMASK;
929 char newflags = new->flags & SE_MNTMASK;
930
931 if (oldflags != newflags)
932 goto mismatch;
933 if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid)
934 goto mismatch;
935 if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid)
936 goto mismatch;
937 if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid)
938 goto mismatch;
939 if (oldflags & ROOTCONTEXT_MNT) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500940 struct inode_security_struct *oldroot = backing_inode_security(oldsb->s_root);
941 struct inode_security_struct *newroot = backing_inode_security(newsb->s_root);
Jeff Layton094f7b62013-04-01 08:14:24 -0400942 if (oldroot->sid != newroot->sid)
943 goto mismatch;
944 }
945 return 0;
946mismatch:
peter enderborgc103a912018-06-12 10:09:03 +0200947 pr_warn("SELinux: mount invalid. Same superblock, "
Jeff Layton094f7b62013-04-01 08:14:24 -0400948 "different security settings for (dev %s, "
949 "type %s)\n", newsb->s_id, newsb->s_type->name);
950 return -EBUSY;
951}
952
953static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400954 struct super_block *newsb,
955 unsigned long kern_flags,
956 unsigned long *set_kern_flags)
Eric Parisc9180a52007-11-30 13:00:35 -0500957{
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400958 int rc = 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500959 const struct superblock_security_struct *oldsbsec = oldsb->s_security;
960 struct superblock_security_struct *newsbsec = newsb->s_security;
961
962 int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT);
963 int set_context = (oldsbsec->flags & CONTEXT_MNT);
964 int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT);
965
Eric Paris0f5e6422008-04-21 16:24:11 -0400966 /*
967 * if the parent was able to be mounted it clearly had no special lsm
Al Viroe8c26252010-03-23 06:36:54 -0400968 * mount options. thus we can safely deal with this superblock later
Eric Paris0f5e6422008-04-21 16:24:11 -0400969 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500970 if (!selinux_state.initialized)
Jeff Layton094f7b62013-04-01 08:14:24 -0400971 return 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500972
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400973 /*
974 * Specifying internal flags without providing a place to
975 * place the results is not allowed.
976 */
977 if (kern_flags && !set_kern_flags)
978 return -EINVAL;
979
Eric Parisc9180a52007-11-30 13:00:35 -0500980 /* how can we clone if the old one wasn't set up?? */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500981 BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED));
Eric Parisc9180a52007-11-30 13:00:35 -0500982
Jeff Layton094f7b62013-04-01 08:14:24 -0400983 /* if fs is reusing a sb, make sure that the contexts match */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500984 if (newsbsec->flags & SE_SBINITIALIZED)
Jeff Layton094f7b62013-04-01 08:14:24 -0400985 return selinux_cmp_sb_context(oldsb, newsb);
Eric Paris5a552612008-04-09 14:08:35 -0400986
Eric Parisc9180a52007-11-30 13:00:35 -0500987 mutex_lock(&newsbsec->lock);
988
989 newsbsec->flags = oldsbsec->flags;
990
991 newsbsec->sid = oldsbsec->sid;
992 newsbsec->def_sid = oldsbsec->def_sid;
993 newsbsec->behavior = oldsbsec->behavior;
994
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400995 if (newsbsec->behavior == SECURITY_FS_USE_NATIVE &&
996 !(kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500997 rc = security_fs_use(&selinux_state, newsb);
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400998 if (rc)
999 goto out;
1000 }
1001
1002 if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !set_context) {
1003 newsbsec->behavior = SECURITY_FS_USE_NATIVE;
1004 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
1005 }
1006
Eric Parisc9180a52007-11-30 13:00:35 -05001007 if (set_context) {
1008 u32 sid = oldsbsec->mntpoint_sid;
1009
1010 if (!set_fscontext)
1011 newsbsec->sid = sid;
1012 if (!set_rootcontext) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001013 struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
Eric Parisc9180a52007-11-30 13:00:35 -05001014 newisec->sid = sid;
1015 }
1016 newsbsec->mntpoint_sid = sid;
1017 }
1018 if (set_rootcontext) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001019 const struct inode_security_struct *oldisec = backing_inode_security(oldsb->s_root);
1020 struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
Eric Parisc9180a52007-11-30 13:00:35 -05001021
1022 newisec->sid = oldisec->sid;
1023 }
1024
1025 sb_finish_set_opts(newsb);
Scott Mayhew0b4d3452017-06-05 11:45:04 -04001026out:
Eric Parisc9180a52007-11-30 13:00:35 -05001027 mutex_unlock(&newsbsec->lock);
Scott Mayhew0b4d3452017-06-05 11:45:04 -04001028 return rc;
Eric Parisc9180a52007-11-30 13:00:35 -05001029}
1030
Al Viroba641862018-12-14 20:28:15 -05001031static int selinux_add_opt(int token, const char *s, void **mnt_opts)
Eric Parisc9180a52007-11-30 13:00:35 -05001032{
Al Viroba641862018-12-14 20:28:15 -05001033 struct selinux_mnt_opts *opts = *mnt_opts;
Eric Parisc9180a52007-11-30 13:00:35 -05001034
Al Viroda3d76a2018-12-17 10:14:16 -05001035 if (token == Opt_seclabel) /* eaten and completely ignored */
Al Viro169d68efb2018-12-14 22:44:50 -05001036 return 0;
Eric Parisc9180a52007-11-30 13:00:35 -05001037
Al Viroba641862018-12-14 20:28:15 -05001038 if (!opts) {
1039 opts = kzalloc(sizeof(struct selinux_mnt_opts), GFP_KERNEL);
1040 if (!opts)
1041 return -ENOMEM;
1042 *mnt_opts = opts;
1043 }
1044 if (!s)
1045 return -ENOMEM;
1046 switch (token) {
1047 case Opt_context:
1048 if (opts->context || opts->defcontext)
1049 goto Einval;
1050 opts->context = s;
1051 break;
1052 case Opt_fscontext:
1053 if (opts->fscontext)
1054 goto Einval;
1055 opts->fscontext = s;
1056 break;
1057 case Opt_rootcontext:
1058 if (opts->rootcontext)
1059 goto Einval;
1060 opts->rootcontext = s;
1061 break;
1062 case Opt_defcontext:
1063 if (opts->context || opts->defcontext)
1064 goto Einval;
1065 opts->defcontext = s;
1066 break;
1067 }
1068 return 0;
1069Einval:
1070 pr_warn(SEL_MOUNT_FAIL_MSG);
Al Viroba641862018-12-14 20:28:15 -05001071 return -EINVAL;
1072}
Eric Parisc9180a52007-11-30 13:00:35 -05001073
Al Viro757cbe52018-12-14 23:42:21 -05001074static int selinux_add_mnt_opt(const char *option, const char *val, int len,
1075 void **mnt_opts)
Eric Parisc9180a52007-11-30 13:00:35 -05001076{
Al Viro757cbe52018-12-14 23:42:21 -05001077 int token = Opt_error;
1078 int rc, i;
Eric Parisc9180a52007-11-30 13:00:35 -05001079
Al Viro757cbe52018-12-14 23:42:21 -05001080 for (i = 0; i < ARRAY_SIZE(tokens); i++) {
1081 if (strcmp(option, tokens[i].name) == 0) {
1082 token = tokens[i].opt;
Eric Parisc9180a52007-11-30 13:00:35 -05001083 break;
Eric Parisc9180a52007-11-30 13:00:35 -05001084 }
1085 }
1086
Al Viro757cbe52018-12-14 23:42:21 -05001087 if (token == Opt_error)
1088 return -EINVAL;
Eric Parise0007522008-03-05 10:31:54 -05001089
Al Viro757cbe52018-12-14 23:42:21 -05001090 if (token != Opt_seclabel)
1091 val = kmemdup_nul(val, len, GFP_KERNEL);
1092 rc = selinux_add_opt(token, val, mnt_opts);
1093 if (unlikely(rc)) {
1094 kfree(val);
1095 if (*mnt_opts) {
1096 selinux_free_mnt_opts(*mnt_opts);
1097 *mnt_opts = NULL;
1098 }
1099 }
1100 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Al Viroe3489f82018-12-13 00:24:36 -05001103static int show_sid(struct seq_file *m, u32 sid)
Eric Paris2069f452008-07-04 09:47:13 +10001104{
Al Viroe3489f82018-12-13 00:24:36 -05001105 char *context = NULL;
1106 u32 len;
1107 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Al Viroe3489f82018-12-13 00:24:36 -05001109 rc = security_sid_to_context(&selinux_state, sid,
1110 &context, &len);
1111 if (!rc) {
1112 bool has_comma = context && strchr(context, ',');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Eric Paris2069f452008-07-04 09:47:13 +10001114 if (has_comma)
1115 seq_putc(m, '\"');
Al Viroe3489f82018-12-13 00:24:36 -05001116 seq_escape(m, context, "\"\n\\");
Eric Paris2069f452008-07-04 09:47:13 +10001117 if (has_comma)
1118 seq_putc(m, '\"');
1119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 kfree(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 return rc;
1122}
Eric Paris2069f452008-07-04 09:47:13 +10001123
1124static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
1125{
Al Viroe3489f82018-12-13 00:24:36 -05001126 struct superblock_security_struct *sbsec = sb->s_security;
Eric Paris2069f452008-07-04 09:47:13 +10001127 int rc;
1128
Al Viroe3489f82018-12-13 00:24:36 -05001129 if (!(sbsec->flags & SE_SBINITIALIZED))
1130 return 0;
1131
1132 if (!selinux_state.initialized)
1133 return 0;
1134
1135 if (sbsec->flags & FSCONTEXT_MNT) {
1136 seq_putc(m, ',');
1137 seq_puts(m, FSCONTEXT_STR);
1138 rc = show_sid(m, sbsec->sid);
1139 if (rc)
1140 return rc;
Eric Paris383795c2008-07-29 17:07:26 -04001141 }
Al Viroe3489f82018-12-13 00:24:36 -05001142 if (sbsec->flags & CONTEXT_MNT) {
1143 seq_putc(m, ',');
1144 seq_puts(m, CONTEXT_STR);
1145 rc = show_sid(m, sbsec->mntpoint_sid);
1146 if (rc)
1147 return rc;
1148 }
1149 if (sbsec->flags & DEFCONTEXT_MNT) {
1150 seq_putc(m, ',');
1151 seq_puts(m, DEFCONTEXT_STR);
1152 rc = show_sid(m, sbsec->def_sid);
1153 if (rc)
1154 return rc;
1155 }
1156 if (sbsec->flags & ROOTCONTEXT_MNT) {
1157 struct dentry *root = sbsec->sb->s_root;
1158 struct inode_security_struct *isec = backing_inode_security(root);
1159 seq_putc(m, ',');
1160 seq_puts(m, ROOTCONTEXT_STR);
1161 rc = show_sid(m, isec->sid);
1162 if (rc)
1163 return rc;
1164 }
1165 if (sbsec->flags & SBLABEL_MNT) {
1166 seq_putc(m, ',');
1167 seq_puts(m, LABELSUPP_STR);
1168 }
1169 return 0;
Eric Paris2069f452008-07-04 09:47:13 +10001170}
1171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172static inline u16 inode_mode_to_security_class(umode_t mode)
1173{
1174 switch (mode & S_IFMT) {
1175 case S_IFSOCK:
1176 return SECCLASS_SOCK_FILE;
1177 case S_IFLNK:
1178 return SECCLASS_LNK_FILE;
1179 case S_IFREG:
1180 return SECCLASS_FILE;
1181 case S_IFBLK:
1182 return SECCLASS_BLK_FILE;
1183 case S_IFDIR:
1184 return SECCLASS_DIR;
1185 case S_IFCHR:
1186 return SECCLASS_CHR_FILE;
1187 case S_IFIFO:
1188 return SECCLASS_FIFO_FILE;
1189
1190 }
1191
1192 return SECCLASS_FILE;
1193}
1194
James Morris13402582005-09-30 14:24:34 -04001195static inline int default_protocol_stream(int protocol)
1196{
1197 return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP);
1198}
1199
1200static inline int default_protocol_dgram(int protocol)
1201{
1202 return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
1203}
1204
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205static inline u16 socket_type_to_security_class(int family, int type, int protocol)
1206{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001207 int extsockclass = selinux_policycap_extsockclass();
Stephen Smalleyda69a532017-01-09 10:07:30 -05001208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 switch (family) {
1210 case PF_UNIX:
1211 switch (type) {
1212 case SOCK_STREAM:
1213 case SOCK_SEQPACKET:
1214 return SECCLASS_UNIX_STREAM_SOCKET;
1215 case SOCK_DGRAM:
Luis Ressel2a764b52017-07-25 15:13:41 -04001216 case SOCK_RAW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 return SECCLASS_UNIX_DGRAM_SOCKET;
1218 }
1219 break;
1220 case PF_INET:
1221 case PF_INET6:
1222 switch (type) {
1223 case SOCK_STREAM:
Stephen Smalleyda69a532017-01-09 10:07:30 -05001224 case SOCK_SEQPACKET:
James Morris13402582005-09-30 14:24:34 -04001225 if (default_protocol_stream(protocol))
1226 return SECCLASS_TCP_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001227 else if (extsockclass && protocol == IPPROTO_SCTP)
1228 return SECCLASS_SCTP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001229 else
1230 return SECCLASS_RAWIP_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 case SOCK_DGRAM:
James Morris13402582005-09-30 14:24:34 -04001232 if (default_protocol_dgram(protocol))
1233 return SECCLASS_UDP_SOCKET;
Stephen Smalleyef379792017-01-09 10:07:31 -05001234 else if (extsockclass && (protocol == IPPROTO_ICMP ||
1235 protocol == IPPROTO_ICMPV6))
Stephen Smalleyda69a532017-01-09 10:07:30 -05001236 return SECCLASS_ICMP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001237 else
1238 return SECCLASS_RAWIP_SOCKET;
James Morris2ee92d42006-11-13 16:09:01 -08001239 case SOCK_DCCP:
1240 return SECCLASS_DCCP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001241 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 return SECCLASS_RAWIP_SOCKET;
1243 }
1244 break;
1245 case PF_NETLINK:
1246 switch (protocol) {
1247 case NETLINK_ROUTE:
1248 return SECCLASS_NETLINK_ROUTE_SOCKET;
Pavel Emelyanov7f1fb602011-12-06 07:56:43 +00001249 case NETLINK_SOCK_DIAG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 return SECCLASS_NETLINK_TCPDIAG_SOCKET;
1251 case NETLINK_NFLOG:
1252 return SECCLASS_NETLINK_NFLOG_SOCKET;
1253 case NETLINK_XFRM:
1254 return SECCLASS_NETLINK_XFRM_SOCKET;
1255 case NETLINK_SELINUX:
1256 return SECCLASS_NETLINK_SELINUX_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001257 case NETLINK_ISCSI:
1258 return SECCLASS_NETLINK_ISCSI_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 case NETLINK_AUDIT:
1260 return SECCLASS_NETLINK_AUDIT_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001261 case NETLINK_FIB_LOOKUP:
1262 return SECCLASS_NETLINK_FIB_LOOKUP_SOCKET;
1263 case NETLINK_CONNECTOR:
1264 return SECCLASS_NETLINK_CONNECTOR_SOCKET;
1265 case NETLINK_NETFILTER:
1266 return SECCLASS_NETLINK_NETFILTER_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 case NETLINK_DNRTMSG:
1268 return SECCLASS_NETLINK_DNRT_SOCKET;
James Morris0c9b7942005-04-16 15:24:13 -07001269 case NETLINK_KOBJECT_UEVENT:
1270 return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001271 case NETLINK_GENERIC:
1272 return SECCLASS_NETLINK_GENERIC_SOCKET;
1273 case NETLINK_SCSITRANSPORT:
1274 return SECCLASS_NETLINK_SCSITRANSPORT_SOCKET;
1275 case NETLINK_RDMA:
1276 return SECCLASS_NETLINK_RDMA_SOCKET;
1277 case NETLINK_CRYPTO:
1278 return SECCLASS_NETLINK_CRYPTO_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 default:
1280 return SECCLASS_NETLINK_SOCKET;
1281 }
1282 case PF_PACKET:
1283 return SECCLASS_PACKET_SOCKET;
1284 case PF_KEY:
1285 return SECCLASS_KEY_SOCKET;
Christopher J. PeBenito3e3ff152006-06-09 00:25:03 -07001286 case PF_APPLETALK:
1287 return SECCLASS_APPLETALK_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 }
1289
Stephen Smalleyda69a532017-01-09 10:07:30 -05001290 if (extsockclass) {
1291 switch (family) {
1292 case PF_AX25:
1293 return SECCLASS_AX25_SOCKET;
1294 case PF_IPX:
1295 return SECCLASS_IPX_SOCKET;
1296 case PF_NETROM:
1297 return SECCLASS_NETROM_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001298 case PF_ATMPVC:
1299 return SECCLASS_ATMPVC_SOCKET;
1300 case PF_X25:
1301 return SECCLASS_X25_SOCKET;
1302 case PF_ROSE:
1303 return SECCLASS_ROSE_SOCKET;
1304 case PF_DECnet:
1305 return SECCLASS_DECNET_SOCKET;
1306 case PF_ATMSVC:
1307 return SECCLASS_ATMSVC_SOCKET;
1308 case PF_RDS:
1309 return SECCLASS_RDS_SOCKET;
1310 case PF_IRDA:
1311 return SECCLASS_IRDA_SOCKET;
1312 case PF_PPPOX:
1313 return SECCLASS_PPPOX_SOCKET;
1314 case PF_LLC:
1315 return SECCLASS_LLC_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001316 case PF_CAN:
1317 return SECCLASS_CAN_SOCKET;
1318 case PF_TIPC:
1319 return SECCLASS_TIPC_SOCKET;
1320 case PF_BLUETOOTH:
1321 return SECCLASS_BLUETOOTH_SOCKET;
1322 case PF_IUCV:
1323 return SECCLASS_IUCV_SOCKET;
1324 case PF_RXRPC:
1325 return SECCLASS_RXRPC_SOCKET;
1326 case PF_ISDN:
1327 return SECCLASS_ISDN_SOCKET;
1328 case PF_PHONET:
1329 return SECCLASS_PHONET_SOCKET;
1330 case PF_IEEE802154:
1331 return SECCLASS_IEEE802154_SOCKET;
1332 case PF_CAIF:
1333 return SECCLASS_CAIF_SOCKET;
1334 case PF_ALG:
1335 return SECCLASS_ALG_SOCKET;
1336 case PF_NFC:
1337 return SECCLASS_NFC_SOCKET;
1338 case PF_VSOCK:
1339 return SECCLASS_VSOCK_SOCKET;
1340 case PF_KCM:
1341 return SECCLASS_KCM_SOCKET;
1342 case PF_QIPCRTR:
1343 return SECCLASS_QIPCRTR_SOCKET;
Linus Torvalds3051bf32017-02-22 10:15:09 -08001344 case PF_SMC:
1345 return SECCLASS_SMC_SOCKET;
Björn Töpel68e8b842018-05-02 13:01:22 +02001346 case PF_XDP:
1347 return SECCLASS_XDP_SOCKET;
1348#if PF_MAX > 45
Stephen Smalleyda69a532017-01-09 10:07:30 -05001349#error New address family defined, please update this function.
1350#endif
1351 }
1352 }
1353
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 return SECCLASS_SOCKET;
1355}
1356
Stephen Smalley134509d2015-06-04 16:22:17 -04001357static int selinux_genfs_get_sid(struct dentry *dentry,
1358 u16 tclass,
1359 u16 flags,
1360 u32 *sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361{
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001362 int rc;
Al Virofc640052016-04-10 01:33:30 -04001363 struct super_block *sb = dentry->d_sb;
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001364 char *buffer, *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Eric Paris828dfe12008-04-17 13:17:49 -04001366 buffer = (char *)__get_free_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 if (!buffer)
1368 return -ENOMEM;
1369
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001370 path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
1371 if (IS_ERR(path))
1372 rc = PTR_ERR(path);
1373 else {
Stephen Smalley134509d2015-06-04 16:22:17 -04001374 if (flags & SE_SBPROC) {
1375 /* each process gets a /proc/PID/ entry. Strip off the
1376 * PID part to get a valid selinux labeling.
1377 * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
1378 while (path[1] >= '0' && path[1] <= '9') {
1379 path[1] = '/';
1380 path++;
1381 }
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001382 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001383 rc = security_genfs_sid(&selinux_state, sb->s_type->name,
1384 path, tclass, sid);
Stephen Smalley7bb185e2018-09-04 16:51:36 -04001385 if (rc == -ENOENT) {
1386 /* No match in policy, mark as unlabeled. */
1387 *sid = SECINITSID_UNLABELED;
1388 rc = 0;
1389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 free_page((unsigned long)buffer);
1392 return rc;
1393}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395/* The inode's security attributes must be initialized before first use. */
1396static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
1397{
1398 struct superblock_security_struct *sbsec = NULL;
1399 struct inode_security_struct *isec = inode->i_security;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001400 u32 task_sid, sid = 0;
1401 u16 sclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 struct dentry *dentry;
1403#define INITCONTEXTLEN 255
1404 char *context = NULL;
1405 unsigned len = 0;
1406 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05001408 if (isec->initialized == LABEL_INITIALIZED)
Andreas Gruenbacher13457d02016-11-10 22:18:29 +01001409 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001411 spin_lock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05001412 if (isec->initialized == LABEL_INITIALIZED)
Eric Paris23970742006-09-25 23:32:01 -07001413 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
Andreas Gruenbacher13457d02016-11-10 22:18:29 +01001415 if (isec->sclass == SECCLASS_FILE)
1416 isec->sclass = inode_mode_to_security_class(inode->i_mode);
1417
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 sbsec = inode->i_sb->s_security;
David P. Quigley0d90a7e2009-01-16 09:22:02 -05001419 if (!(sbsec->flags & SE_SBINITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 /* Defer initialization until selinux_complete_init,
1421 after the initial policy is loaded and the security
1422 server is ready to handle calls. */
1423 spin_lock(&sbsec->isec_lock);
1424 if (list_empty(&isec->list))
1425 list_add(&isec->list, &sbsec->isec_head);
1426 spin_unlock(&sbsec->isec_lock);
Eric Paris23970742006-09-25 23:32:01 -07001427 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 }
1429
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001430 sclass = isec->sclass;
1431 task_sid = isec->task_sid;
1432 sid = isec->sid;
1433 isec->initialized = LABEL_PENDING;
1434 spin_unlock(&isec->lock);
1435
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 switch (sbsec->behavior) {
David Quigleyeb9ae682013-05-22 12:50:37 -04001437 case SECURITY_FS_USE_NATIVE:
1438 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 case SECURITY_FS_USE_XATTR:
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001440 if (!(inode->i_opflags & IOP_XATTR)) {
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001441 sid = sbsec->def_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 break;
1443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 /* Need a dentry, since the xattr API requires one.
1445 Life would be simpler if we could just pass the inode. */
1446 if (opt_dentry) {
1447 /* Called from d_instantiate or d_splice_alias. */
1448 dentry = dget(opt_dentry);
1449 } else {
Al Virob1271252018-04-25 10:28:38 -04001450 /*
1451 * Called from selinux_complete_init, try to find a dentry.
1452 * Some filesystems really want a connected one, so try
1453 * that first. We could split SECURITY_FS_USE_XATTR in
1454 * two, depending upon that...
1455 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 dentry = d_find_alias(inode);
Al Virob1271252018-04-25 10:28:38 -04001457 if (!dentry)
1458 dentry = d_find_any_alias(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 }
1460 if (!dentry) {
Eric Parisdf7f54c2009-03-09 14:35:58 -04001461 /*
1462 * this is can be hit on boot when a file is accessed
1463 * before the policy is loaded. When we load policy we
1464 * may find inodes that have no dentry on the
1465 * sbsec->isec_head list. No reason to complain as these
1466 * will get fixed up the next time we go through
1467 * inode_doinit with a dentry, before these inodes could
1468 * be used again by userspace.
1469 */
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001470 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 }
1472
1473 len = INITCONTEXTLEN;
Eric Paris4cb912f2009-02-12 14:50:05 -05001474 context = kmalloc(len+1, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 if (!context) {
1476 rc = -ENOMEM;
1477 dput(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001478 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 }
Eric Paris4cb912f2009-02-12 14:50:05 -05001480 context[len] = '\0';
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001481 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 if (rc == -ERANGE) {
James Morris314dabb2009-08-10 22:00:13 +10001483 kfree(context);
1484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 /* Need a larger buffer. Query for the right size. */
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001486 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 if (rc < 0) {
1488 dput(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001489 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 len = rc;
Eric Paris4cb912f2009-02-12 14:50:05 -05001492 context = kmalloc(len+1, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 if (!context) {
1494 rc = -ENOMEM;
1495 dput(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001496 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 }
Eric Paris4cb912f2009-02-12 14:50:05 -05001498 context[len] = '\0';
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001499 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 }
1501 dput(dentry);
1502 if (rc < 0) {
1503 if (rc != -ENODATA) {
peter enderborgc103a912018-06-12 10:09:03 +02001504 pr_warn("SELinux: %s: getxattr returned "
Harvey Harrisondd6f9532008-03-06 10:03:59 +11001505 "%d for dev=%s ino=%ld\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 -rc, inode->i_sb->s_id, inode->i_ino);
1507 kfree(context);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001508 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 }
1510 /* Map ENODATA to the default file SID */
1511 sid = sbsec->def_sid;
1512 rc = 0;
1513 } else {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001514 rc = security_context_to_sid_default(&selinux_state,
1515 context, rc, &sid,
Stephen Smalley869ab512008-04-04 08:46:05 -04001516 sbsec->def_sid,
1517 GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 if (rc) {
Eric Paris4ba0a8a2009-02-12 15:01:10 -05001519 char *dev = inode->i_sb->s_id;
1520 unsigned long ino = inode->i_ino;
1521
1522 if (rc == -EINVAL) {
1523 if (printk_ratelimit())
peter enderborgc103a912018-06-12 10:09:03 +02001524 pr_notice("SELinux: inode=%lu on dev=%s was found to have an invalid "
Eric Paris4ba0a8a2009-02-12 15:01:10 -05001525 "context=%s. This indicates you may need to relabel the inode or the "
1526 "filesystem in question.\n", ino, dev, context);
1527 } else {
peter enderborgc103a912018-06-12 10:09:03 +02001528 pr_warn("SELinux: %s: context_to_sid(%s) "
Eric Paris4ba0a8a2009-02-12 15:01:10 -05001529 "returned %d for dev=%s ino=%ld\n",
1530 __func__, context, -rc, dev, ino);
1531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 kfree(context);
1533 /* Leave with the unlabeled SID */
1534 rc = 0;
1535 break;
1536 }
1537 }
1538 kfree(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 break;
1540 case SECURITY_FS_USE_TASK:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001541 sid = task_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 break;
1543 case SECURITY_FS_USE_TRANS:
1544 /* Default to the fs SID. */
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001545 sid = sbsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
1547 /* Try to obtain a transition SID. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001548 rc = security_transition_sid(&selinux_state, task_sid, sid,
1549 sclass, NULL, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 if (rc)
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001551 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 break;
Eric Parisc312feb2006-07-10 04:43:53 -07001553 case SECURITY_FS_USE_MNTPOINT:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001554 sid = sbsec->mntpoint_sid;
Eric Parisc312feb2006-07-10 04:43:53 -07001555 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 default:
Eric Parisc312feb2006-07-10 04:43:53 -07001557 /* Default to the fs superblock SID. */
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001558 sid = sbsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Stephen Smalley134509d2015-06-04 16:22:17 -04001560 if ((sbsec->flags & SE_SBGENFS) && !S_ISLNK(inode->i_mode)) {
Paul Mooref64410e2014-03-19 16:46:18 -04001561 /* We must have a dentry to determine the label on
1562 * procfs inodes */
Al Virob1271252018-04-25 10:28:38 -04001563 if (opt_dentry) {
Paul Mooref64410e2014-03-19 16:46:18 -04001564 /* Called from d_instantiate or
1565 * d_splice_alias. */
1566 dentry = dget(opt_dentry);
Al Virob1271252018-04-25 10:28:38 -04001567 } else {
Paul Mooref64410e2014-03-19 16:46:18 -04001568 /* Called from selinux_complete_init, try to
Al Virob1271252018-04-25 10:28:38 -04001569 * find a dentry. Some filesystems really want
1570 * a connected one, so try that first.
1571 */
Paul Mooref64410e2014-03-19 16:46:18 -04001572 dentry = d_find_alias(inode);
Al Virob1271252018-04-25 10:28:38 -04001573 if (!dentry)
1574 dentry = d_find_any_alias(inode);
1575 }
Paul Mooref64410e2014-03-19 16:46:18 -04001576 /*
1577 * This can be hit on boot when a file is accessed
1578 * before the policy is loaded. When we load policy we
1579 * may find inodes that have no dentry on the
1580 * sbsec->isec_head list. No reason to complain as
1581 * these will get fixed up the next time we go through
1582 * inode_doinit() with a dentry, before these inodes
1583 * could be used again by userspace.
1584 */
1585 if (!dentry)
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001586 goto out;
1587 rc = selinux_genfs_get_sid(dentry, sclass,
Stephen Smalley134509d2015-06-04 16:22:17 -04001588 sbsec->flags, &sid);
Paul Mooref64410e2014-03-19 16:46:18 -04001589 dput(dentry);
1590 if (rc)
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001591 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 }
1593 break;
1594 }
1595
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001596out:
1597 spin_lock(&isec->lock);
1598 if (isec->initialized == LABEL_PENDING) {
1599 if (!sid || rc) {
1600 isec->initialized = LABEL_INVALID;
1601 goto out_unlock;
1602 }
1603
1604 isec->initialized = LABEL_INITIALIZED;
1605 isec->sid = sid;
1606 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
Eric Paris23970742006-09-25 23:32:01 -07001608out_unlock:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001609 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 return rc;
1611}
1612
1613/* Convert a Linux signal to an access vector. */
1614static inline u32 signal_to_av(int sig)
1615{
1616 u32 perm = 0;
1617
1618 switch (sig) {
1619 case SIGCHLD:
1620 /* Commonly granted from child to parent. */
1621 perm = PROCESS__SIGCHLD;
1622 break;
1623 case SIGKILL:
1624 /* Cannot be caught or ignored */
1625 perm = PROCESS__SIGKILL;
1626 break;
1627 case SIGSTOP:
1628 /* Cannot be caught or ignored */
1629 perm = PROCESS__SIGSTOP;
1630 break;
1631 default:
1632 /* All other signals. */
1633 perm = PROCESS__SIGNAL;
1634 break;
1635 }
1636
1637 return perm;
1638}
1639
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001640#if CAP_LAST_CAP > 63
1641#error Fix SELinux to handle capabilities > 63.
1642#endif
1643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644/* Check whether a task is allowed to use a capability. */
Eric Paris6a9de492012-01-03 12:25:14 -05001645static int cred_has_capability(const struct cred *cred,
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04001646 int cap, int audit, bool initns)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647{
Thomas Liu2bf49692009-07-14 12:14:09 -04001648 struct common_audit_data ad;
Eric Paris06112162008-11-11 22:02:50 +11001649 struct av_decision avd;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001650 u16 sclass;
David Howells3699c532009-01-06 22:27:01 +00001651 u32 sid = cred_sid(cred);
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001652 u32 av = CAP_TO_MASK(cap);
Eric Paris06112162008-11-11 22:02:50 +11001653 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
Eric Paris50c205f2012-04-04 15:01:43 -04001655 ad.type = LSM_AUDIT_DATA_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 ad.u.cap = cap;
1657
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001658 switch (CAP_TO_INDEX(cap)) {
1659 case 0:
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04001660 sclass = initns ? SECCLASS_CAPABILITY : SECCLASS_CAP_USERNS;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001661 break;
1662 case 1:
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04001663 sclass = initns ? SECCLASS_CAPABILITY2 : SECCLASS_CAP2_USERNS;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001664 break;
1665 default:
peter enderborgc103a912018-06-12 10:09:03 +02001666 pr_err("SELinux: out of range capability %d\n", cap);
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001667 BUG();
Eric Parisa35c6c832011-04-20 10:21:28 -04001668 return -EINVAL;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001669 }
Eric Paris06112162008-11-11 22:02:50 +11001670
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001671 rc = avc_has_perm_noaudit(&selinux_state,
1672 sid, sid, sclass, av, 0, &avd);
Eric Paris9ade0cf2011-04-25 16:26:29 -04001673 if (audit == SECURITY_CAP_AUDIT) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001674 int rc2 = avc_audit(&selinux_state,
1675 sid, sid, sclass, av, &avd, rc, &ad, 0);
Eric Paris9ade0cf2011-04-25 16:26:29 -04001676 if (rc2)
1677 return rc2;
1678 }
Eric Paris06112162008-11-11 22:02:50 +11001679 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680}
1681
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682/* Check whether a task has a particular permission to an inode.
1683 The 'adp' parameter is optional and allows other audit
1684 data to be passed (e.g. the dentry). */
David Howells88e67f32008-11-14 10:39:21 +11001685static int inode_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 struct inode *inode,
1687 u32 perms,
Linus Torvalds19e49832013-10-04 12:54:11 -07001688 struct common_audit_data *adp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 struct inode_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +11001691 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
David Howellse0e81732009-09-02 09:13:40 +01001693 validate_creds(cred);
1694
Eric Paris828dfe12008-04-17 13:17:49 -04001695 if (unlikely(IS_PRIVATE(inode)))
Stephen Smalleybbaca6c2007-02-14 00:34:16 -08001696 return 0;
1697
David Howells88e67f32008-11-14 10:39:21 +11001698 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 isec = inode->i_security;
1700
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001701 return avc_has_perm(&selinux_state,
1702 sid, isec->sid, isec->sclass, perms, adp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703}
1704
1705/* Same as inode_has_perm, but pass explicit audit data containing
1706 the dentry to help the auditing code to more easily generate the
1707 pathname if needed. */
David Howells88e67f32008-11-14 10:39:21 +11001708static inline int dentry_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 struct dentry *dentry,
1710 u32 av)
1711{
David Howellsc6f493d2015-03-17 22:26:22 +00001712 struct inode *inode = d_backing_inode(dentry);
Thomas Liu2bf49692009-07-14 12:14:09 -04001713 struct common_audit_data ad;
David Howells88e67f32008-11-14 10:39:21 +11001714
Eric Paris50c205f2012-04-04 15:01:43 -04001715 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Paris2875fa02011-04-28 16:04:24 -04001716 ad.u.dentry = dentry;
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05001717 __inode_security_revalidate(inode, dentry, true);
Linus Torvalds19e49832013-10-04 12:54:11 -07001718 return inode_has_perm(cred, inode, av, &ad);
Eric Paris2875fa02011-04-28 16:04:24 -04001719}
1720
1721/* Same as inode_has_perm, but pass explicit audit data containing
1722 the path to help the auditing code to more easily generate the
1723 pathname if needed. */
1724static inline int path_has_perm(const struct cred *cred,
Al Viro3f7036a2015-03-08 19:28:30 -04001725 const struct path *path,
Eric Paris2875fa02011-04-28 16:04:24 -04001726 u32 av)
1727{
David Howellsc6f493d2015-03-17 22:26:22 +00001728 struct inode *inode = d_backing_inode(path->dentry);
Eric Paris2875fa02011-04-28 16:04:24 -04001729 struct common_audit_data ad;
1730
Eric Paris50c205f2012-04-04 15:01:43 -04001731 ad.type = LSM_AUDIT_DATA_PATH;
Eric Paris2875fa02011-04-28 16:04:24 -04001732 ad.u.path = *path;
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05001733 __inode_security_revalidate(inode, path->dentry, true);
Linus Torvalds19e49832013-10-04 12:54:11 -07001734 return inode_has_perm(cred, inode, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735}
1736
David Howells13f8e982013-06-13 23:37:55 +01001737/* Same as path_has_perm, but uses the inode from the file struct. */
1738static inline int file_path_has_perm(const struct cred *cred,
1739 struct file *file,
1740 u32 av)
1741{
1742 struct common_audit_data ad;
1743
Vivek Goyal43af5de2016-09-09 11:37:49 -04001744 ad.type = LSM_AUDIT_DATA_FILE;
1745 ad.u.file = file;
Linus Torvalds19e49832013-10-04 12:54:11 -07001746 return inode_has_perm(cred, file_inode(file), av, &ad);
David Howells13f8e982013-06-13 23:37:55 +01001747}
1748
Chenbo Fengf66e4482017-10-18 13:00:26 -07001749#ifdef CONFIG_BPF_SYSCALL
1750static int bpf_fd_pass(struct file *file, u32 sid);
1751#endif
1752
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753/* Check whether a task can use an open file descriptor to
1754 access an inode in a given way. Check access to the
1755 descriptor itself, and then use dentry_has_perm to
1756 check a particular permission to the file.
1757 Access to the descriptor is implicitly granted if it
1758 has the same SID as the process. If av is zero, then
1759 access to the file is not checked, e.g. for cases
1760 where only the descriptor is affected like seek. */
David Howells88e67f32008-11-14 10:39:21 +11001761static int file_has_perm(const struct cred *cred,
1762 struct file *file,
1763 u32 av)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 struct file_security_struct *fsec = file->f_security;
Al Viro496ad9a2013-01-23 17:07:38 -05001766 struct inode *inode = file_inode(file);
Thomas Liu2bf49692009-07-14 12:14:09 -04001767 struct common_audit_data ad;
David Howells88e67f32008-11-14 10:39:21 +11001768 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 int rc;
1770
Vivek Goyal43af5de2016-09-09 11:37:49 -04001771 ad.type = LSM_AUDIT_DATA_FILE;
1772 ad.u.file = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
David Howells275bb412008-11-14 10:39:19 +11001774 if (sid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001775 rc = avc_has_perm(&selinux_state,
1776 sid, fsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 SECCLASS_FD,
1778 FD__USE,
1779 &ad);
1780 if (rc)
David Howells88e67f32008-11-14 10:39:21 +11001781 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 }
1783
Chenbo Fengf66e4482017-10-18 13:00:26 -07001784#ifdef CONFIG_BPF_SYSCALL
1785 rc = bpf_fd_pass(file, cred_sid(cred));
1786 if (rc)
1787 return rc;
1788#endif
1789
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 /* av is zero if only checking access to the descriptor. */
David Howells88e67f32008-11-14 10:39:21 +11001791 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 if (av)
Linus Torvalds19e49832013-10-04 12:54:11 -07001793 rc = inode_has_perm(cred, inode, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
David Howells88e67f32008-11-14 10:39:21 +11001795out:
1796 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797}
1798
David Howellsc3c188b2015-07-10 17:19:58 -04001799/*
1800 * Determine the label for an inode that might be unioned.
1801 */
Vivek Goyalc957f6d2016-07-13 10:44:51 -04001802static int
1803selinux_determine_inode_label(const struct task_security_struct *tsec,
1804 struct inode *dir,
1805 const struct qstr *name, u16 tclass,
1806 u32 *_new_isid)
David Howellsc3c188b2015-07-10 17:19:58 -04001807{
1808 const struct superblock_security_struct *sbsec = dir->i_sb->s_security;
David Howellsc3c188b2015-07-10 17:19:58 -04001809
1810 if ((sbsec->flags & SE_SBINITIALIZED) &&
1811 (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)) {
1812 *_new_isid = sbsec->mntpoint_sid;
1813 } else if ((sbsec->flags & SBLABEL_MNT) &&
1814 tsec->create_sid) {
1815 *_new_isid = tsec->create_sid;
1816 } else {
Paul Moore20cdef82016-04-04 14:14:42 -04001817 const struct inode_security_struct *dsec = inode_security(dir);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001818 return security_transition_sid(&selinux_state, tsec->sid,
1819 dsec->sid, tclass,
David Howellsc3c188b2015-07-10 17:19:58 -04001820 name, _new_isid);
1821 }
1822
1823 return 0;
1824}
1825
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826/* Check whether a task can create a file. */
1827static int may_create(struct inode *dir,
1828 struct dentry *dentry,
1829 u16 tclass)
1830{
Paul Moore5fb49872010-04-22 14:46:19 -04001831 const struct task_security_struct *tsec = current_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 struct inode_security_struct *dsec;
1833 struct superblock_security_struct *sbsec;
David Howells275bb412008-11-14 10:39:19 +11001834 u32 sid, newsid;
Thomas Liu2bf49692009-07-14 12:14:09 -04001835 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 int rc;
1837
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001838 dsec = inode_security(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 sbsec = dir->i_sb->s_security;
1840
David Howells275bb412008-11-14 10:39:19 +11001841 sid = tsec->sid;
David Howells275bb412008-11-14 10:39:19 +11001842
Eric Paris50c205f2012-04-04 15:01:43 -04001843 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04001844 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001846 rc = avc_has_perm(&selinux_state,
1847 sid, dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 DIR__ADD_NAME | DIR__SEARCH,
1849 &ad);
1850 if (rc)
1851 return rc;
1852
Vivek Goyalc957f6d2016-07-13 10:44:51 -04001853 rc = selinux_determine_inode_label(current_security(), dir,
1854 &dentry->d_name, tclass, &newsid);
David Howellsc3c188b2015-07-10 17:19:58 -04001855 if (rc)
1856 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001858 rc = avc_has_perm(&selinux_state,
1859 sid, newsid, tclass, FILE__CREATE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 if (rc)
1861 return rc;
1862
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001863 return avc_has_perm(&selinux_state,
1864 newsid, sbsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 SECCLASS_FILESYSTEM,
1866 FILESYSTEM__ASSOCIATE, &ad);
1867}
1868
Eric Paris828dfe12008-04-17 13:17:49 -04001869#define MAY_LINK 0
1870#define MAY_UNLINK 1
1871#define MAY_RMDIR 2
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
1873/* Check whether a task can link, unlink, or rmdir a file/directory. */
1874static int may_link(struct inode *dir,
1875 struct dentry *dentry,
1876 int kind)
1877
1878{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 struct inode_security_struct *dsec, *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001880 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11001881 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 u32 av;
1883 int rc;
1884
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001885 dsec = inode_security(dir);
1886 isec = backing_inode_security(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
Eric Paris50c205f2012-04-04 15:01:43 -04001888 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04001889 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
1891 av = DIR__SEARCH;
1892 av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001893 rc = avc_has_perm(&selinux_state,
1894 sid, dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 if (rc)
1896 return rc;
1897
1898 switch (kind) {
1899 case MAY_LINK:
1900 av = FILE__LINK;
1901 break;
1902 case MAY_UNLINK:
1903 av = FILE__UNLINK;
1904 break;
1905 case MAY_RMDIR:
1906 av = DIR__RMDIR;
1907 break;
1908 default:
peter enderborgc103a912018-06-12 10:09:03 +02001909 pr_warn("SELinux: %s: unrecognized kind %d\n",
Eric Paris744ba352008-04-17 11:52:44 -04001910 __func__, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 return 0;
1912 }
1913
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001914 rc = avc_has_perm(&selinux_state,
1915 sid, isec->sid, isec->sclass, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 return rc;
1917}
1918
1919static inline int may_rename(struct inode *old_dir,
1920 struct dentry *old_dentry,
1921 struct inode *new_dir,
1922 struct dentry *new_dentry)
1923{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001925 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11001926 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 u32 av;
1928 int old_is_dir, new_is_dir;
1929 int rc;
1930
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001931 old_dsec = inode_security(old_dir);
1932 old_isec = backing_inode_security(old_dentry);
David Howellse36cb0b2015-01-29 12:02:35 +00001933 old_is_dir = d_is_dir(old_dentry);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001934 new_dsec = inode_security(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
Eric Paris50c205f2012-04-04 15:01:43 -04001936 ad.type = LSM_AUDIT_DATA_DENTRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
Eric Parisa2694342011-04-25 13:10:27 -04001938 ad.u.dentry = old_dentry;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001939 rc = avc_has_perm(&selinux_state,
1940 sid, old_dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 DIR__REMOVE_NAME | DIR__SEARCH, &ad);
1942 if (rc)
1943 return rc;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001944 rc = avc_has_perm(&selinux_state,
1945 sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 old_isec->sclass, FILE__RENAME, &ad);
1947 if (rc)
1948 return rc;
1949 if (old_is_dir && new_dir != old_dir) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001950 rc = avc_has_perm(&selinux_state,
1951 sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 old_isec->sclass, DIR__REPARENT, &ad);
1953 if (rc)
1954 return rc;
1955 }
1956
Eric Parisa2694342011-04-25 13:10:27 -04001957 ad.u.dentry = new_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 av = DIR__ADD_NAME | DIR__SEARCH;
David Howells2c616d42015-01-29 12:02:33 +00001959 if (d_is_positive(new_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 av |= DIR__REMOVE_NAME;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001961 rc = avc_has_perm(&selinux_state,
1962 sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 if (rc)
1964 return rc;
David Howells2c616d42015-01-29 12:02:33 +00001965 if (d_is_positive(new_dentry)) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001966 new_isec = backing_inode_security(new_dentry);
David Howellse36cb0b2015-01-29 12:02:35 +00001967 new_is_dir = d_is_dir(new_dentry);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001968 rc = avc_has_perm(&selinux_state,
1969 sid, new_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 new_isec->sclass,
1971 (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
1972 if (rc)
1973 return rc;
1974 }
1975
1976 return 0;
1977}
1978
1979/* Check whether a task can perform a filesystem operation. */
David Howells88e67f32008-11-14 10:39:21 +11001980static int superblock_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 struct super_block *sb,
1982 u32 perms,
Thomas Liu2bf49692009-07-14 12:14:09 -04001983 struct common_audit_data *ad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 struct superblock_security_struct *sbsec;
David Howells88e67f32008-11-14 10:39:21 +11001986 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 sbsec = sb->s_security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001989 return avc_has_perm(&selinux_state,
1990 sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991}
1992
1993/* Convert a Linux mode and permission mask to an access vector. */
1994static inline u32 file_mask_to_av(int mode, int mask)
1995{
1996 u32 av = 0;
1997
Al Virodba19c62011-07-25 20:49:29 -04001998 if (!S_ISDIR(mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 if (mask & MAY_EXEC)
2000 av |= FILE__EXECUTE;
2001 if (mask & MAY_READ)
2002 av |= FILE__READ;
2003
2004 if (mask & MAY_APPEND)
2005 av |= FILE__APPEND;
2006 else if (mask & MAY_WRITE)
2007 av |= FILE__WRITE;
2008
2009 } else {
2010 if (mask & MAY_EXEC)
2011 av |= DIR__SEARCH;
2012 if (mask & MAY_WRITE)
2013 av |= DIR__WRITE;
2014 if (mask & MAY_READ)
2015 av |= DIR__READ;
2016 }
2017
2018 return av;
2019}
2020
2021/* Convert a Linux file to an access vector. */
2022static inline u32 file_to_av(struct file *file)
2023{
2024 u32 av = 0;
2025
2026 if (file->f_mode & FMODE_READ)
2027 av |= FILE__READ;
2028 if (file->f_mode & FMODE_WRITE) {
2029 if (file->f_flags & O_APPEND)
2030 av |= FILE__APPEND;
2031 else
2032 av |= FILE__WRITE;
2033 }
Stephen Smalley0794c662008-03-17 08:55:18 -04002034 if (!av) {
2035 /*
2036 * Special file opened with flags 3 for ioctl-only use.
2037 */
2038 av = FILE__IOCTL;
2039 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
2041 return av;
2042}
2043
Eric Paris8b6a5a32008-10-29 17:06:46 -04002044/*
2045 * Convert a file to an access vector and include the correct open
2046 * open permission.
2047 */
2048static inline u32 open_file_to_av(struct file *file)
2049{
2050 u32 av = file_to_av(file);
Stephen Smalleyccb54472017-05-12 12:41:24 -04002051 struct inode *inode = file_inode(file);
Eric Paris8b6a5a32008-10-29 17:06:46 -04002052
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002053 if (selinux_policycap_openperm() &&
2054 inode->i_sb->s_magic != SOCKFS_MAGIC)
Eric Paris49b7b8d2010-07-23 11:44:09 -04002055 av |= FILE__OPEN;
2056
Eric Paris8b6a5a32008-10-29 17:06:46 -04002057 return av;
2058}
2059
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060/* Hook functions begin here. */
2061
Stephen Smalley79af7302015-01-21 10:54:10 -05002062static int selinux_binder_set_context_mgr(struct task_struct *mgr)
2063{
2064 u32 mysid = current_sid();
2065 u32 mgrsid = task_sid(mgr);
2066
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002067 return avc_has_perm(&selinux_state,
2068 mysid, mgrsid, SECCLASS_BINDER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002069 BINDER__SET_CONTEXT_MGR, NULL);
2070}
2071
2072static int selinux_binder_transaction(struct task_struct *from,
2073 struct task_struct *to)
2074{
2075 u32 mysid = current_sid();
2076 u32 fromsid = task_sid(from);
2077 u32 tosid = task_sid(to);
2078 int rc;
2079
2080 if (mysid != fromsid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002081 rc = avc_has_perm(&selinux_state,
2082 mysid, fromsid, SECCLASS_BINDER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002083 BINDER__IMPERSONATE, NULL);
2084 if (rc)
2085 return rc;
2086 }
2087
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002088 return avc_has_perm(&selinux_state,
2089 fromsid, tosid, SECCLASS_BINDER, BINDER__CALL,
Stephen Smalley79af7302015-01-21 10:54:10 -05002090 NULL);
2091}
2092
2093static int selinux_binder_transfer_binder(struct task_struct *from,
2094 struct task_struct *to)
2095{
2096 u32 fromsid = task_sid(from);
2097 u32 tosid = task_sid(to);
2098
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002099 return avc_has_perm(&selinux_state,
2100 fromsid, tosid, SECCLASS_BINDER, BINDER__TRANSFER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002101 NULL);
2102}
2103
2104static int selinux_binder_transfer_file(struct task_struct *from,
2105 struct task_struct *to,
2106 struct file *file)
2107{
2108 u32 sid = task_sid(to);
2109 struct file_security_struct *fsec = file->f_security;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002110 struct dentry *dentry = file->f_path.dentry;
Paul Moore20cdef82016-04-04 14:14:42 -04002111 struct inode_security_struct *isec;
Stephen Smalley79af7302015-01-21 10:54:10 -05002112 struct common_audit_data ad;
2113 int rc;
2114
2115 ad.type = LSM_AUDIT_DATA_PATH;
2116 ad.u.path = file->f_path;
2117
2118 if (sid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002119 rc = avc_has_perm(&selinux_state,
2120 sid, fsec->sid,
Stephen Smalley79af7302015-01-21 10:54:10 -05002121 SECCLASS_FD,
2122 FD__USE,
2123 &ad);
2124 if (rc)
2125 return rc;
2126 }
2127
Chenbo Fengf66e4482017-10-18 13:00:26 -07002128#ifdef CONFIG_BPF_SYSCALL
2129 rc = bpf_fd_pass(file, sid);
2130 if (rc)
2131 return rc;
2132#endif
2133
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002134 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
Stephen Smalley79af7302015-01-21 10:54:10 -05002135 return 0;
2136
Paul Moore20cdef82016-04-04 14:14:42 -04002137 isec = backing_inode_security(dentry);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002138 return avc_has_perm(&selinux_state,
2139 sid, isec->sid, isec->sclass, file_to_av(file),
Stephen Smalley79af7302015-01-21 10:54:10 -05002140 &ad);
2141}
2142
Ingo Molnar9e488582009-05-07 19:26:19 +10002143static int selinux_ptrace_access_check(struct task_struct *child,
David Howells5cd9c582008-08-14 11:37:28 +01002144 unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002146 u32 sid = current_sid();
2147 u32 csid = task_sid(child);
Stephen Smalley006ebb42008-05-19 08:32:49 -04002148
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002149 if (mode & PTRACE_MODE_READ)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002150 return avc_has_perm(&selinux_state,
2151 sid, csid, SECCLASS_FILE, FILE__READ, NULL);
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002152
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002153 return avc_has_perm(&selinux_state,
2154 sid, csid, SECCLASS_PROCESS, PROCESS__PTRACE, NULL);
David Howells5cd9c582008-08-14 11:37:28 +01002155}
2156
2157static int selinux_ptrace_traceme(struct task_struct *parent)
2158{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002159 return avc_has_perm(&selinux_state,
2160 task_sid(parent), current_sid(), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002161 PROCESS__PTRACE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162}
2163
2164static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
Eric Paris828dfe12008-04-17 13:17:49 -04002165 kernel_cap_t *inheritable, kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002167 return avc_has_perm(&selinux_state,
2168 current_sid(), task_sid(target), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002169 PROCESS__GETCAP, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170}
2171
David Howellsd84f4f92008-11-14 10:39:23 +11002172static int selinux_capset(struct cred *new, const struct cred *old,
2173 const kernel_cap_t *effective,
2174 const kernel_cap_t *inheritable,
2175 const kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002177 return avc_has_perm(&selinux_state,
2178 cred_sid(old), cred_sid(new), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002179 PROCESS__SETCAP, NULL);
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{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 switch (type) {
Kees Cookd78ca3c2010-02-03 15:37:13 -08002236 case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */
2237 case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002238 return avc_has_perm(&selinux_state,
2239 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002240 SECCLASS_SYSTEM, SYSTEM__SYSLOG_READ, NULL);
Kees Cookd78ca3c2010-02-03 15:37:13 -08002241 case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */
2242 case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */
2243 /* Set level of messages printed to console */
2244 case SYSLOG_ACTION_CONSOLE_LEVEL:
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002245 return avc_has_perm(&selinux_state,
2246 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002247 SECCLASS_SYSTEM, SYSTEM__SYSLOG_CONSOLE,
2248 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 }
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002250 /* All other syslog types */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002251 return avc_has_perm(&selinux_state,
2252 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002253 SECCLASS_SYSTEM, SYSTEM__SYSLOG_MOD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254}
2255
2256/*
2257 * Check that a process has enough memory to allocate a new virtual
2258 * mapping. 0 means there is enough memory for the allocation to
2259 * succeed and -ENOMEM implies there is not.
2260 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 * Do not audit the selinux permission check, as this is applied to all
2262 * processes that allocate mappings.
2263 */
Alan Cox34b4e4a2007-08-22 14:01:28 -07002264static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265{
2266 int rc, cap_sys_admin = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002268 rc = cred_has_capability(current_cred(), CAP_SYS_ADMIN,
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04002269 SECURITY_CAP_NOAUDIT, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 if (rc == 0)
2271 cap_sys_admin = 1;
2272
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002273 return cap_sys_admin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274}
2275
2276/* binprm security operations */
2277
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002278static u32 ptrace_parent_sid(void)
Paul Moore0c6181c2016-03-30 21:41:21 -04002279{
2280 u32 sid = 0;
2281 struct task_struct *tracer;
2282
2283 rcu_read_lock();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002284 tracer = ptrace_parent(current);
Paul Moore0c6181c2016-03-30 21:41:21 -04002285 if (tracer)
2286 sid = task_sid(tracer);
2287 rcu_read_unlock();
2288
2289 return sid;
2290}
2291
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002292static int check_nnp_nosuid(const struct linux_binprm *bprm,
2293 const struct task_security_struct *old_tsec,
2294 const struct task_security_struct *new_tsec)
2295{
2296 int nnp = (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS);
Andy Lutomirski380cf5b2016-06-23 16:41:05 -05002297 int nosuid = !mnt_may_suid(bprm->file->f_path.mnt);
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002298 int rc;
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002299 u32 av;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002300
2301 if (!nnp && !nosuid)
2302 return 0; /* neither NNP nor nosuid */
2303
2304 if (new_tsec->sid == old_tsec->sid)
2305 return 0; /* No change in credentials */
2306
2307 /*
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002308 * If the policy enables the nnp_nosuid_transition policy capability,
2309 * then we permit transitions under NNP or nosuid if the
2310 * policy allows the corresponding permission between
2311 * the old and new contexts.
2312 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002313 if (selinux_policycap_nnp_nosuid_transition()) {
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002314 av = 0;
2315 if (nnp)
2316 av |= PROCESS2__NNP_TRANSITION;
2317 if (nosuid)
2318 av |= PROCESS2__NOSUID_TRANSITION;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002319 rc = avc_has_perm(&selinux_state,
2320 old_tsec->sid, new_tsec->sid,
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002321 SECCLASS_PROCESS2, av, NULL);
2322 if (!rc)
2323 return 0;
2324 }
2325
2326 /*
2327 * We also permit NNP or nosuid transitions to bounded SIDs,
2328 * i.e. SIDs that are guaranteed to only be allowed a subset
2329 * of the permissions of the current SID.
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002330 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002331 rc = security_bounded_transition(&selinux_state, old_tsec->sid,
2332 new_tsec->sid);
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002333 if (!rc)
2334 return 0;
2335
2336 /*
2337 * On failure, preserve the errno values for NNP vs nosuid.
2338 * NNP: Operation not permitted for caller.
2339 * nosuid: Permission denied to file.
2340 */
2341 if (nnp)
2342 return -EPERM;
2343 return -EACCES;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002344}
2345
David Howellsa6f76f22008-11-14 10:39:24 +11002346static int selinux_bprm_set_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347{
David Howellsa6f76f22008-11-14 10:39:24 +11002348 const struct task_security_struct *old_tsec;
2349 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 struct inode_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04002351 struct common_audit_data ad;
Al Viro496ad9a2013-01-23 17:07:38 -05002352 struct inode *inode = file_inode(bprm->file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 int rc;
2354
David Howellsa6f76f22008-11-14 10:39:24 +11002355 /* SELinux context only depends on initial program or script and not
2356 * the script interpreter */
Kees Cookddb4a142017-07-18 15:25:23 -07002357 if (bprm->called_set_creds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 return 0;
2359
David Howellsa6f76f22008-11-14 10:39:24 +11002360 old_tsec = current_security();
2361 new_tsec = bprm->cred->security;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002362 isec = inode_security(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
2364 /* Default to the current task SID. */
David Howellsa6f76f22008-11-14 10:39:24 +11002365 new_tsec->sid = old_tsec->sid;
2366 new_tsec->osid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
Michael LeMay28eba5b2006-06-27 02:53:42 -07002368 /* Reset fs, key, and sock SIDs on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002369 new_tsec->create_sid = 0;
2370 new_tsec->keycreate_sid = 0;
2371 new_tsec->sockcreate_sid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
David Howellsa6f76f22008-11-14 10:39:24 +11002373 if (old_tsec->exec_sid) {
2374 new_tsec->sid = old_tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 /* Reset exec SID on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002376 new_tsec->exec_sid = 0;
Andy Lutomirski259e5e62012-04-12 16:47:50 -05002377
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002378 /* Fail on NNP or nosuid if not an allowed transition. */
2379 rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
2380 if (rc)
2381 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 } else {
2383 /* Check for a default transition on this program. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002384 rc = security_transition_sid(&selinux_state, old_tsec->sid,
2385 isec->sid, SECCLASS_PROCESS, NULL,
Eric Paris652bb9b2011-02-01 11:05:40 -05002386 &new_tsec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 if (rc)
2388 return rc;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002389
2390 /*
2391 * Fallback to old SID on NNP or nosuid if not an allowed
2392 * transition.
2393 */
2394 rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
2395 if (rc)
2396 new_tsec->sid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 }
2398
Vivek Goyal43af5de2016-09-09 11:37:49 -04002399 ad.type = LSM_AUDIT_DATA_FILE;
2400 ad.u.file = bprm->file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
David Howellsa6f76f22008-11-14 10:39:24 +11002402 if (new_tsec->sid == old_tsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002403 rc = avc_has_perm(&selinux_state,
2404 old_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
2406 if (rc)
2407 return rc;
2408 } else {
2409 /* Check permissions for the transition. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002410 rc = avc_has_perm(&selinux_state,
2411 old_tsec->sid, new_tsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
2413 if (rc)
2414 return rc;
2415
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002416 rc = avc_has_perm(&selinux_state,
2417 new_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
2419 if (rc)
2420 return rc;
2421
David Howellsa6f76f22008-11-14 10:39:24 +11002422 /* Check for shared state */
2423 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002424 rc = avc_has_perm(&selinux_state,
2425 old_tsec->sid, new_tsec->sid,
David Howellsa6f76f22008-11-14 10:39:24 +11002426 SECCLASS_PROCESS, PROCESS__SHARE,
2427 NULL);
2428 if (rc)
2429 return -EPERM;
2430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431
David Howellsa6f76f22008-11-14 10:39:24 +11002432 /* Make sure that anyone attempting to ptrace over a task that
2433 * changes its SID has the appropriate permit */
Eric W. Biederman9227dd22017-01-23 17:26:31 +13002434 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002435 u32 ptsid = ptrace_parent_sid();
David Howellsa6f76f22008-11-14 10:39:24 +11002436 if (ptsid != 0) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002437 rc = avc_has_perm(&selinux_state,
2438 ptsid, new_tsec->sid,
David Howellsa6f76f22008-11-14 10:39:24 +11002439 SECCLASS_PROCESS,
2440 PROCESS__PTRACE, NULL);
2441 if (rc)
2442 return -EPERM;
2443 }
2444 }
2445
2446 /* Clear any possibly unsafe personality bits on exec: */
2447 bprm->per_clear |= PER_CLEAR_ON_SETID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 /* Enable secure mode for SIDs transitions unless
2450 the noatsecure permission is granted between
2451 the two SIDs, i.e. ahp returns 0. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002452 rc = avc_has_perm(&selinux_state,
2453 old_tsec->sid, new_tsec->sid,
Kees Cook62874c32017-07-18 15:25:25 -07002454 SECCLASS_PROCESS, PROCESS__NOATSECURE,
2455 NULL);
2456 bprm->secureexec |= !!rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 }
2458
Kees Cook62874c32017-07-18 15:25:25 -07002459 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460}
2461
Al Viroc3c073f2012-08-21 22:32:06 -04002462static int match_file(const void *p, struct file *file, unsigned fd)
2463{
2464 return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0;
2465}
2466
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467/* Derived from fs/exec.c:flush_old_files. */
David Howells745ca242008-11-14 10:39:22 +11002468static inline void flush_unauthorized_files(const struct cred *cred,
2469 struct files_struct *files)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 struct file *file, *devnull = NULL;
Stephen Smalleyb20c8122006-09-25 23:32:03 -07002472 struct tty_struct *tty;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002473 int drop_tty = 0;
Al Viroc3c073f2012-08-21 22:32:06 -04002474 unsigned n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002476 tty = get_current_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 if (tty) {
Peter Hurley4a510962016-01-09 21:35:23 -08002478 spin_lock(&tty->files_lock);
Eric Paris37dd0bd2008-10-31 17:40:00 -04002479 if (!list_empty(&tty->tty_files)) {
Nick Piggind996b622010-08-18 04:37:36 +10002480 struct tty_file_private *file_priv;
Eric Paris37dd0bd2008-10-31 17:40:00 -04002481
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 /* Revalidate access to controlling tty.
David Howells13f8e982013-06-13 23:37:55 +01002483 Use file_path_has_perm on the tty path directly
2484 rather than using file_has_perm, as this particular
2485 open file may belong to another process and we are
2486 only interested in the inode-based check here. */
Nick Piggind996b622010-08-18 04:37:36 +10002487 file_priv = list_first_entry(&tty->tty_files,
2488 struct tty_file_private, list);
2489 file = file_priv->file;
David Howells13f8e982013-06-13 23:37:55 +01002490 if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE))
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002491 drop_tty = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 }
Peter Hurley4a510962016-01-09 21:35:23 -08002493 spin_unlock(&tty->files_lock);
Alan Cox452a00d2008-10-13 10:39:13 +01002494 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 }
Eric W. Biederman98a27ba2007-05-08 00:26:56 -07002496 /* Reset controlling tty. */
2497 if (drop_tty)
2498 no_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499
2500 /* Revalidate access to inherited open files. */
Al Viroc3c073f2012-08-21 22:32:06 -04002501 n = iterate_fd(files, 0, match_file, cred);
2502 if (!n) /* none found? */
2503 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504
Al Viroc3c073f2012-08-21 22:32:06 -04002505 devnull = dentry_open(&selinux_null, O_RDWR, cred);
Al Viro45525b22012-10-16 13:30:07 -04002506 if (IS_ERR(devnull))
2507 devnull = NULL;
2508 /* replace all the matching ones with this */
2509 do {
2510 replace_fd(n - 1, devnull, 0);
2511 } while ((n = iterate_fd(files, n, match_file, cred)) != 0);
2512 if (devnull)
Al Viroc3c073f2012-08-21 22:32:06 -04002513 fput(devnull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514}
2515
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516/*
David Howellsa6f76f22008-11-14 10:39:24 +11002517 * Prepare a process for imminent new credential changes due to exec
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 */
David Howellsa6f76f22008-11-14 10:39:24 +11002519static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520{
David Howellsa6f76f22008-11-14 10:39:24 +11002521 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 struct rlimit *rlim, *initrlim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 int rc, i;
2524
David Howellsa6f76f22008-11-14 10:39:24 +11002525 new_tsec = bprm->cred->security;
2526 if (new_tsec->sid == new_tsec->osid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 return;
2528
2529 /* Close files for which the new task SID is not authorized. */
David Howellsa6f76f22008-11-14 10:39:24 +11002530 flush_unauthorized_files(bprm->cred, current->files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531
David Howellsa6f76f22008-11-14 10:39:24 +11002532 /* Always clear parent death signal on SID transitions. */
2533 current->pdeath_signal = 0;
2534
2535 /* Check whether the new SID can inherit resource limits from the old
2536 * SID. If not, reset all soft limits to the lower of the current
2537 * task's hard limit and the init task's soft limit.
2538 *
2539 * Note that the setting of hard limits (even to lower them) can be
2540 * controlled by the setrlimit check. The inclusion of the init task's
2541 * soft limit into the computation is to avoid resetting soft limits
2542 * higher than the default soft limit for cases where the default is
2543 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2544 */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002545 rc = avc_has_perm(&selinux_state,
2546 new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
David Howellsa6f76f22008-11-14 10:39:24 +11002547 PROCESS__RLIMITINH, NULL);
2548 if (rc) {
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002549 /* protect against do_prlimit() */
2550 task_lock(current);
David Howellsa6f76f22008-11-14 10:39:24 +11002551 for (i = 0; i < RLIM_NLIMITS; i++) {
2552 rlim = current->signal->rlim + i;
2553 initrlim = init_task.signal->rlim + i;
2554 rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
2555 }
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002556 task_unlock(current);
Nicolas Pitrebaa73d92016-11-11 00:10:10 -05002557 if (IS_ENABLED(CONFIG_POSIX_TIMERS))
2558 update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
David Howellsa6f76f22008-11-14 10:39:24 +11002559 }
2560}
2561
2562/*
2563 * Clean up the process immediately after the installation of new credentials
2564 * due to exec
2565 */
2566static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
2567{
2568 const struct task_security_struct *tsec = current_security();
2569 struct itimerval itimer;
David Howellsa6f76f22008-11-14 10:39:24 +11002570 u32 osid, sid;
2571 int rc, i;
David Howellsa6f76f22008-11-14 10:39:24 +11002572
David Howellsa6f76f22008-11-14 10:39:24 +11002573 osid = tsec->osid;
2574 sid = tsec->sid;
2575
2576 if (sid == osid)
2577 return;
2578
2579 /* Check whether the new SID can inherit signal state from the old SID.
2580 * If not, clear itimers to avoid subsequent signal generation and
2581 * flush and unblock signals.
2582 *
2583 * This must occur _after_ the task SID has been updated so that any
2584 * kill done after the flush will be checked against the new SID.
2585 */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002586 rc = avc_has_perm(&selinux_state,
2587 osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 if (rc) {
Nicolas Pitrebaa73d92016-11-11 00:10:10 -05002589 if (IS_ENABLED(CONFIG_POSIX_TIMERS)) {
2590 memset(&itimer, 0, sizeof itimer);
2591 for (i = 0; i < 3; i++)
2592 do_setitimer(i, &itimer, NULL);
2593 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterov9e7c8f82015-06-04 16:22:16 -04002595 if (!fatal_signal_pending(current)) {
2596 flush_sigqueue(&current->pending);
2597 flush_sigqueue(&current->signal->shared_pending);
David Howells3bcac022009-04-29 13:45:05 +01002598 flush_signal_handlers(current, 1);
2599 sigemptyset(&current->blocked);
Oleg Nesterov9e7c8f82015-06-04 16:22:16 -04002600 recalc_sigpending();
David Howells3bcac022009-04-29 13:45:05 +01002601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 spin_unlock_irq(&current->sighand->siglock);
2603 }
2604
David Howellsa6f76f22008-11-14 10:39:24 +11002605 /* Wake up the parent if it is waiting so that it can recheck
2606 * wait permission to the new task SID. */
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002607 read_lock(&tasklist_lock);
Oleg Nesterov0b7570e2009-09-23 15:56:46 -07002608 __wake_up_parent(current, current->real_parent);
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002609 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610}
2611
2612/* superblock security operations */
2613
2614static int selinux_sb_alloc_security(struct super_block *sb)
2615{
2616 return superblock_alloc_security(sb);
2617}
2618
2619static void selinux_sb_free_security(struct super_block *sb)
2620{
2621 superblock_free_security(sb);
2622}
2623
Al Viro99dbbb52018-12-14 21:56:23 -05002624static inline int opt_len(const char *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625{
Al Viro99dbbb52018-12-14 21:56:23 -05002626 bool open_quote = false;
2627 int len;
2628 char c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629
Al Viro99dbbb52018-12-14 21:56:23 -05002630 for (len = 0; (c = s[len]) != '\0'; len++) {
2631 if (c == '"')
Cory Olmo3528a952006-09-29 01:58:44 -07002632 open_quote = !open_quote;
Al Viro99dbbb52018-12-14 21:56:23 -05002633 if (c == ',' && !open_quote)
2634 break;
2635 }
2636 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637}
2638
Al Viro204cc0c2018-12-13 13:41:47 -05002639static int selinux_sb_eat_lsm_opts(char *options, void **mnt_opts)
Eric Paris026eb162011-03-03 16:09:14 -05002640{
Al Viro99dbbb52018-12-14 21:56:23 -05002641 char *from = options;
2642 char *to = options;
2643 bool first = true;
Al Viro5b400232018-12-12 20:13:29 -05002644
Al Viro99dbbb52018-12-14 21:56:23 -05002645 while (1) {
2646 int len = opt_len(from);
2647 int token, rc;
2648 char *arg = NULL;
2649
2650 token = match_opt_prefix(from, len, &arg);
2651
2652 if (token != Opt_error) {
2653 char *p, *q;
2654
2655 /* strip quotes */
2656 if (arg) {
2657 for (p = q = arg; p < from + len; p++) {
2658 char c = *p;
2659 if (c != '"')
2660 *q++ = c;
2661 }
2662 arg = kmemdup_nul(arg, q - arg, GFP_KERNEL);
2663 }
2664 rc = selinux_add_opt(token, arg, mnt_opts);
2665 if (unlikely(rc)) {
2666 kfree(arg);
2667 if (*mnt_opts) {
2668 selinux_free_mnt_opts(*mnt_opts);
2669 *mnt_opts = NULL;
2670 }
2671 return rc;
2672 }
2673 } else {
2674 if (!first) { // copy with preceding comma
2675 from--;
2676 len++;
2677 }
2678 if (to != from)
2679 memmove(to, from, len);
2680 to += len;
2681 first = false;
2682 }
2683 if (!from[len])
2684 break;
2685 from += len + 1;
2686 }
2687 *to = '\0';
2688 return 0;
Al Viro5b400232018-12-12 20:13:29 -05002689}
2690
Al Viro204cc0c2018-12-13 13:41:47 -05002691static int selinux_sb_remount(struct super_block *sb, void *mnt_opts)
Eric Paris026eb162011-03-03 16:09:14 -05002692{
Al Virobd323652018-12-13 15:04:59 -05002693 struct selinux_mnt_opts *opts = mnt_opts;
Eric Paris026eb162011-03-03 16:09:14 -05002694 struct superblock_security_struct *sbsec = sb->s_security;
Al Virobd323652018-12-13 15:04:59 -05002695 u32 sid;
2696 int rc;
Eric Paris026eb162011-03-03 16:09:14 -05002697
2698 if (!(sbsec->flags & SE_SBINITIALIZED))
2699 return 0;
2700
Al Viro204cc0c2018-12-13 13:41:47 -05002701 if (!opts)
Eric Paris026eb162011-03-03 16:09:14 -05002702 return 0;
2703
Al Virobd323652018-12-13 15:04:59 -05002704 if (opts->fscontext) {
2705 rc = parse_sid(sb, opts->fscontext, &sid);
2706 if (rc)
Al Viroc039bc32018-12-01 23:06:57 -05002707 return rc;
Al Virobd323652018-12-13 15:04:59 -05002708 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
2709 goto out_bad_option;
Eric Paris026eb162011-03-03 16:09:14 -05002710 }
Al Virobd323652018-12-13 15:04:59 -05002711 if (opts->context) {
2712 rc = parse_sid(sb, opts->context, &sid);
2713 if (rc)
2714 return rc;
2715 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
2716 goto out_bad_option;
2717 }
2718 if (opts->rootcontext) {
2719 struct inode_security_struct *root_isec;
2720 root_isec = backing_inode_security(sb->s_root);
2721 rc = parse_sid(sb, opts->rootcontext, &sid);
2722 if (rc)
2723 return rc;
2724 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
2725 goto out_bad_option;
2726 }
2727 if (opts->defcontext) {
2728 rc = parse_sid(sb, opts->defcontext, &sid);
2729 if (rc)
2730 return rc;
2731 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
2732 goto out_bad_option;
Eric Paris026eb162011-03-03 16:09:14 -05002733 }
Al Viroc039bc32018-12-01 23:06:57 -05002734 return 0;
Eric Paris026eb162011-03-03 16:09:14 -05002735
Eric Paris026eb162011-03-03 16:09:14 -05002736out_bad_option:
peter enderborgc103a912018-06-12 10:09:03 +02002737 pr_warn("SELinux: unable to change security options "
Linus Torvalds29b1deb2013-12-15 11:17:45 -08002738 "during remount (dev %s, type=%s)\n", sb->s_id,
2739 sb->s_type->name);
Al Viroc039bc32018-12-01 23:06:57 -05002740 return -EINVAL;
Eric Paris026eb162011-03-03 16:09:14 -05002741}
2742
Al Viroa10d7c22018-12-05 11:58:35 -05002743static int selinux_sb_kern_mount(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744{
David Howells88e67f32008-11-14 10:39:21 +11002745 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002746 struct common_audit_data ad;
James Morris74192242008-12-19 11:41:10 +11002747
Eric Paris50c205f2012-04-04 15:01:43 -04002748 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002749 ad.u.dentry = sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002750 return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751}
2752
David Howells726c3342006-06-23 02:02:58 -07002753static int selinux_sb_statfs(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754{
David Howells88e67f32008-11-14 10:39:21 +11002755 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002756 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757
Eric Paris50c205f2012-04-04 15:01:43 -04002758 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002759 ad.u.dentry = dentry->d_sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002760 return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761}
2762
Al Viro808d4e32012-10-11 11:42:01 -04002763static int selinux_mount(const char *dev_name,
Al Viro8a04c432016-03-25 14:52:53 -04002764 const struct path *path,
Al Viro808d4e32012-10-11 11:42:01 -04002765 const char *type,
Eric Paris828dfe12008-04-17 13:17:49 -04002766 unsigned long flags,
2767 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768{
David Howells88e67f32008-11-14 10:39:21 +11002769 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770
2771 if (flags & MS_REMOUNT)
Al Virod8c95842011-12-07 18:16:57 -05002772 return superblock_has_perm(cred, path->dentry->d_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002773 FILESYSTEM__REMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 else
Eric Paris2875fa02011-04-28 16:04:24 -04002775 return path_has_perm(cred, path, FILE__MOUNTON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776}
2777
2778static int selinux_umount(struct vfsmount *mnt, int flags)
2779{
David Howells88e67f32008-11-14 10:39:21 +11002780 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781
David Howells88e67f32008-11-14 10:39:21 +11002782 return superblock_has_perm(cred, mnt->mnt_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002783 FILESYSTEM__UNMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784}
2785
2786/* inode security operations */
2787
2788static int selinux_inode_alloc_security(struct inode *inode)
2789{
2790 return inode_alloc_security(inode);
2791}
2792
2793static void selinux_inode_free_security(struct inode *inode)
2794{
2795 inode_free_security(inode);
2796}
2797
David Quigleyd47be3d2013-05-22 12:50:34 -04002798static int selinux_dentry_init_security(struct dentry *dentry, int mode,
Al Viro4f3ccd72016-07-20 16:06:15 -04002799 const struct qstr *name, void **ctx,
David Quigleyd47be3d2013-05-22 12:50:34 -04002800 u32 *ctxlen)
2801{
David Quigleyd47be3d2013-05-22 12:50:34 -04002802 u32 newsid;
2803 int rc;
2804
Vivek Goyalc957f6d2016-07-13 10:44:51 -04002805 rc = selinux_determine_inode_label(current_security(),
2806 d_inode(dentry->d_parent), name,
David Howellsc3c188b2015-07-10 17:19:58 -04002807 inode_mode_to_security_class(mode),
2808 &newsid);
2809 if (rc)
2810 return rc;
David Quigleyd47be3d2013-05-22 12:50:34 -04002811
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002812 return security_sid_to_context(&selinux_state, newsid, (char **)ctx,
2813 ctxlen);
David Quigleyd47be3d2013-05-22 12:50:34 -04002814}
2815
Vivek Goyala518b0a2016-07-13 10:44:53 -04002816static int selinux_dentry_create_files_as(struct dentry *dentry, int mode,
2817 struct qstr *name,
2818 const struct cred *old,
2819 struct cred *new)
2820{
2821 u32 newsid;
2822 int rc;
2823 struct task_security_struct *tsec;
2824
2825 rc = selinux_determine_inode_label(old->security,
2826 d_inode(dentry->d_parent), name,
2827 inode_mode_to_security_class(mode),
2828 &newsid);
2829 if (rc)
2830 return rc;
2831
2832 tsec = new->security;
2833 tsec->create_sid = newsid;
2834 return 0;
2835}
2836
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002837static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +09002838 const struct qstr *qstr,
2839 const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -05002840 void **value, size_t *len)
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002841{
Paul Moore5fb49872010-04-22 14:46:19 -04002842 const struct task_security_struct *tsec = current_security();
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002843 struct superblock_security_struct *sbsec;
Corentin LABBEc0d4f462017-10-04 20:32:17 +02002844 u32 newsid, clen;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002845 int rc;
Tetsuo Handa95489062013-07-25 05:44:02 +09002846 char *context;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002847
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002848 sbsec = dir->i_sb->s_security;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002849
David Howells275bb412008-11-14 10:39:19 +11002850 newsid = tsec->create_sid;
2851
Vivek Goyalc957f6d2016-07-13 10:44:51 -04002852 rc = selinux_determine_inode_label(current_security(),
David Howellsc3c188b2015-07-10 17:19:58 -04002853 dir, qstr,
2854 inode_mode_to_security_class(inode->i_mode),
2855 &newsid);
2856 if (rc)
2857 return rc;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002858
Eric Paris296fddf2006-09-25 23:32:00 -07002859 /* Possibly defer initialization to selinux_complete_init. */
David P. Quigley0d90a7e2009-01-16 09:22:02 -05002860 if (sbsec->flags & SE_SBINITIALIZED) {
Eric Paris296fddf2006-09-25 23:32:00 -07002861 struct inode_security_struct *isec = inode->i_security;
2862 isec->sclass = inode_mode_to_security_class(inode->i_mode);
2863 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05002864 isec->initialized = LABEL_INITIALIZED;
Eric Paris296fddf2006-09-25 23:32:00 -07002865 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002866
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002867 if (!selinux_state.initialized || !(sbsec->flags & SBLABEL_MNT))
Stephen Smalley25a74f32005-11-08 21:34:33 -08002868 return -EOPNOTSUPP;
2869
Tetsuo Handa95489062013-07-25 05:44:02 +09002870 if (name)
2871 *name = XATTR_SELINUX_SUFFIX;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002872
2873 if (value && len) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002874 rc = security_sid_to_context_force(&selinux_state, newsid,
2875 &context, &clen);
Tetsuo Handa95489062013-07-25 05:44:02 +09002876 if (rc)
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002877 return rc;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002878 *value = context;
2879 *len = clen;
2880 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002881
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002882 return 0;
2883}
2884
Al Viro4acdaf22011-07-26 01:42:34 -04002885static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886{
2887 return may_create(dir, dentry, SECCLASS_FILE);
2888}
2889
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2891{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 return may_link(dir, old_dentry, MAY_LINK);
2893}
2894
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
2896{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 return may_link(dir, dentry, MAY_UNLINK);
2898}
2899
2900static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
2901{
2902 return may_create(dir, dentry, SECCLASS_LNK_FILE);
2903}
2904
Al Viro18bb1db2011-07-26 01:41:39 -04002905static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906{
2907 return may_create(dir, dentry, SECCLASS_DIR);
2908}
2909
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
2911{
2912 return may_link(dir, dentry, MAY_RMDIR);
2913}
2914
Al Viro1a67aaf2011-07-26 01:52:52 -04002915static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 return may_create(dir, dentry, inode_mode_to_security_class(mode));
2918}
2919
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
Eric Paris828dfe12008-04-17 13:17:49 -04002921 struct inode *new_inode, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922{
2923 return may_rename(old_inode, old_dentry, new_inode, new_dentry);
2924}
2925
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926static int selinux_inode_readlink(struct dentry *dentry)
2927{
David Howells88e67f32008-11-14 10:39:21 +11002928 const struct cred *cred = current_cred();
2929
Eric Paris2875fa02011-04-28 16:04:24 -04002930 return dentry_has_perm(cred, dentry, FILE__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931}
2932
NeilBrownbda0be72015-03-23 13:37:39 +11002933static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode,
2934 bool rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935{
David Howells88e67f32008-11-14 10:39:21 +11002936 const struct cred *cred = current_cred();
NeilBrownbda0be72015-03-23 13:37:39 +11002937 struct common_audit_data ad;
2938 struct inode_security_struct *isec;
2939 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940
NeilBrownbda0be72015-03-23 13:37:39 +11002941 validate_creds(cred);
2942
2943 ad.type = LSM_AUDIT_DATA_DENTRY;
2944 ad.u.dentry = dentry;
2945 sid = cred_sid(cred);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05002946 isec = inode_security_rcu(inode, rcu);
2947 if (IS_ERR(isec))
2948 return PTR_ERR(isec);
NeilBrownbda0be72015-03-23 13:37:39 +11002949
Stephen Smalleye46e01e2018-12-12 10:10:56 -05002950 return avc_has_perm(&selinux_state,
2951 sid, isec->sid, isec->sclass, FILE__READ, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952}
2953
Eric Parisd4cf970d2012-04-04 15:01:42 -04002954static noinline int audit_inode_permission(struct inode *inode,
2955 u32 perms, u32 audited, u32 denied,
Stephen Smalley626b9742014-04-29 11:29:04 -07002956 int result,
Eric Parisd4cf970d2012-04-04 15:01:42 -04002957 unsigned flags)
2958{
2959 struct common_audit_data ad;
Eric Parisd4cf970d2012-04-04 15:01:42 -04002960 struct inode_security_struct *isec = inode->i_security;
2961 int rc;
2962
Eric Paris50c205f2012-04-04 15:01:43 -04002963 ad.type = LSM_AUDIT_DATA_INODE;
Eric Parisd4cf970d2012-04-04 15:01:42 -04002964 ad.u.inode = inode;
2965
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002966 rc = slow_avc_audit(&selinux_state,
2967 current_sid(), isec->sid, isec->sclass, perms,
Stephen Smalley626b9742014-04-29 11:29:04 -07002968 audited, denied, result, &ad, flags);
Eric Parisd4cf970d2012-04-04 15:01:42 -04002969 if (rc)
2970 return rc;
2971 return 0;
2972}
2973
Al Viroe74f71e2011-06-20 19:38:15 -04002974static int selinux_inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975{
David Howells88e67f32008-11-14 10:39:21 +11002976 const struct cred *cred = current_cred();
Eric Parisb782e0a2010-07-23 11:44:03 -04002977 u32 perms;
2978 bool from_access;
Al Virocf1dd1d2011-06-20 19:44:08 -04002979 unsigned flags = mask & MAY_NOT_BLOCK;
Eric Paris2e334052012-04-04 15:01:42 -04002980 struct inode_security_struct *isec;
2981 u32 sid;
2982 struct av_decision avd;
2983 int rc, rc2;
2984 u32 audited, denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985
Eric Parisb782e0a2010-07-23 11:44:03 -04002986 from_access = mask & MAY_ACCESS;
Eric Parisd09ca732010-07-23 11:43:57 -04002987 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
2988
Eric Parisb782e0a2010-07-23 11:44:03 -04002989 /* No permission to check. Existence test. */
2990 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992
Eric Paris2e334052012-04-04 15:01:42 -04002993 validate_creds(cred);
Eric Parisb782e0a2010-07-23 11:44:03 -04002994
Eric Paris2e334052012-04-04 15:01:42 -04002995 if (unlikely(IS_PRIVATE(inode)))
2996 return 0;
Eric Parisb782e0a2010-07-23 11:44:03 -04002997
2998 perms = file_mask_to_av(inode->i_mode, mask);
2999
Eric Paris2e334052012-04-04 15:01:42 -04003000 sid = cred_sid(cred);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05003001 isec = inode_security_rcu(inode, flags & MAY_NOT_BLOCK);
3002 if (IS_ERR(isec))
3003 return PTR_ERR(isec);
Eric Paris2e334052012-04-04 15:01:42 -04003004
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003005 rc = avc_has_perm_noaudit(&selinux_state,
Stephen Smalley3a28cff2018-12-12 10:10:55 -05003006 sid, isec->sid, isec->sclass, perms,
3007 (flags & MAY_NOT_BLOCK) ? AVC_NONBLOCKING : 0,
3008 &avd);
Eric Paris2e334052012-04-04 15:01:42 -04003009 audited = avc_audit_required(perms, &avd, rc,
3010 from_access ? FILE__AUDIT_ACCESS : 0,
3011 &denied);
3012 if (likely(!audited))
3013 return rc;
3014
Stephen Smalley626b9742014-04-29 11:29:04 -07003015 rc2 = audit_inode_permission(inode, perms, audited, denied, rc, flags);
Eric Paris2e334052012-04-04 15:01:42 -04003016 if (rc2)
3017 return rc2;
3018 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019}
3020
3021static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
3022{
David Howells88e67f32008-11-14 10:39:21 +11003023 const struct cred *cred = current_cred();
Stephen Smalleyccb54472017-05-12 12:41:24 -04003024 struct inode *inode = d_backing_inode(dentry);
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003025 unsigned int ia_valid = iattr->ia_valid;
Eric Paris95dbf732012-04-04 13:45:34 -04003026 __u32 av = FILE__WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003028 /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
3029 if (ia_valid & ATTR_FORCE) {
3030 ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
3031 ATTR_FORCE);
3032 if (!ia_valid)
3033 return 0;
3034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003036 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
3037 ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
Eric Paris2875fa02011-04-28 16:04:24 -04003038 return dentry_has_perm(cred, dentry, FILE__SETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003040 if (selinux_policycap_openperm() &&
Stephen Smalleyccb54472017-05-12 12:41:24 -04003041 inode->i_sb->s_magic != SOCKFS_MAGIC &&
3042 (ia_valid & ATTR_SIZE) &&
3043 !(ia_valid & ATTR_FILE))
Eric Paris95dbf732012-04-04 13:45:34 -04003044 av |= FILE__OPEN;
3045
3046 return dentry_has_perm(cred, dentry, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047}
3048
Al Viro3f7036a2015-03-08 19:28:30 -04003049static int selinux_inode_getattr(const struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050{
Al Viro3f7036a2015-03-08 19:28:30 -04003051 return path_has_perm(current_cred(), path, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052}
3053
Stephen Smalleydb590002017-04-20 11:31:30 -04003054static bool has_cap_mac_admin(bool audit)
3055{
3056 const struct cred *cred = current_cred();
3057 int cap_audit = audit ? SECURITY_CAP_AUDIT : SECURITY_CAP_NOAUDIT;
3058
3059 if (cap_capable(cred, &init_user_ns, CAP_MAC_ADMIN, cap_audit))
3060 return false;
3061 if (cred_has_capability(cred, CAP_MAC_ADMIN, cap_audit, true))
3062 return false;
3063 return true;
3064}
3065
David Howells8f0cfa52008-04-29 00:59:41 -07003066static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
3067 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068{
David Howellsc6f493d2015-03-17 22:26:22 +00003069 struct inode *inode = d_backing_inode(dentry);
Paul Moore20cdef82016-04-04 14:14:42 -04003070 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 struct superblock_security_struct *sbsec;
Thomas Liu2bf49692009-07-14 12:14:09 -04003072 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11003073 u32 newsid, sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 int rc = 0;
3075
Eric W. Biederman6b240302017-10-02 09:38:20 -05003076 if (strcmp(name, XATTR_NAME_SELINUX)) {
3077 rc = cap_inode_setxattr(dentry, name, value, size, flags);
3078 if (rc)
3079 return rc;
3080
3081 /* Not an attribute we recognize, so just check the
3082 ordinary setattr permission. */
3083 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085
3086 sbsec = inode->i_sb->s_security;
Eric Paris12f348b2012-10-09 10:56:25 -04003087 if (!(sbsec->flags & SBLABEL_MNT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 return -EOPNOTSUPP;
3089
Serge E. Hallyn2e149672011-03-23 16:43:26 -07003090 if (!inode_owner_or_capable(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 return -EPERM;
3092
Eric Paris50c205f2012-04-04 15:01:43 -04003093 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04003094 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095
Paul Moore20cdef82016-04-04 14:14:42 -04003096 isec = backing_inode_security(dentry);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003097 rc = avc_has_perm(&selinux_state,
3098 sid, isec->sid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 FILE__RELABELFROM, &ad);
3100 if (rc)
3101 return rc;
3102
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003103 rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3104 GFP_KERNEL);
Stephen Smalley12b29f32008-05-07 13:03:20 -04003105 if (rc == -EINVAL) {
Stephen Smalleydb590002017-04-20 11:31:30 -04003106 if (!has_cap_mac_admin(true)) {
Eric Parisd6ea83e2012-04-04 13:45:49 -04003107 struct audit_buffer *ab;
3108 size_t audit_size;
Eric Parisd6ea83e2012-04-04 13:45:49 -04003109
3110 /* We strip a nul only if it is at the end, otherwise the
3111 * context contains a nul and we should audit that */
Al Viroe3fea3f2012-06-09 08:15:16 +01003112 if (value) {
Colin Ian Kingadd24372017-10-14 13:46:55 +01003113 const char *str = value;
3114
Al Viroe3fea3f2012-06-09 08:15:16 +01003115 if (str[size - 1] == '\0')
3116 audit_size = size - 1;
3117 else
3118 audit_size = size;
3119 } else {
Al Viroe3fea3f2012-06-09 08:15:16 +01003120 audit_size = 0;
3121 }
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04003122 ab = audit_log_start(audit_context(),
3123 GFP_ATOMIC, AUDIT_SELINUX_ERR);
Eric Parisd6ea83e2012-04-04 13:45:49 -04003124 audit_log_format(ab, "op=setxattr invalid_context=");
3125 audit_log_n_untrustedstring(ab, value, audit_size);
3126 audit_log_end(ab);
3127
Stephen Smalley12b29f32008-05-07 13:03:20 -04003128 return rc;
Eric Parisd6ea83e2012-04-04 13:45:49 -04003129 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003130 rc = security_context_to_sid_force(&selinux_state, value,
3131 size, &newsid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04003132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 if (rc)
3134 return rc;
3135
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003136 rc = avc_has_perm(&selinux_state,
3137 sid, newsid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 FILE__RELABELTO, &ad);
3139 if (rc)
3140 return rc;
3141
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003142 rc = security_validate_transition(&selinux_state, isec->sid, newsid,
3143 sid, isec->sclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 if (rc)
3145 return rc;
3146
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003147 return avc_has_perm(&selinux_state,
3148 newsid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 sbsec->sid,
3150 SECCLASS_FILESYSTEM,
3151 FILESYSTEM__ASSOCIATE,
3152 &ad);
3153}
3154
David Howells8f0cfa52008-04-29 00:59:41 -07003155static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
Eric Parisf5269712008-05-14 11:27:45 -04003156 const void *value, size_t size,
David Howells8f0cfa52008-04-29 00:59:41 -07003157 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158{
David Howellsc6f493d2015-03-17 22:26:22 +00003159 struct inode *inode = d_backing_inode(dentry);
Paul Moore20cdef82016-04-04 14:14:42 -04003160 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 u32 newsid;
3162 int rc;
3163
3164 if (strcmp(name, XATTR_NAME_SELINUX)) {
3165 /* Not an attribute we recognize, so nothing to do. */
3166 return;
3167 }
3168
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003169 rc = security_context_to_sid_force(&selinux_state, value, size,
3170 &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 if (rc) {
peter enderborgc103a912018-06-12 10:09:03 +02003172 pr_err("SELinux: unable to map context to SID"
Stephen Smalley12b29f32008-05-07 13:03:20 -04003173 "for (%s, %lu), rc=%d\n",
3174 inode->i_sb->s_id, inode->i_ino, -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 return;
3176 }
3177
Paul Moore20cdef82016-04-04 14:14:42 -04003178 isec = backing_inode_security(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003179 spin_lock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003180 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05003182 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003183 spin_unlock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003184
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 return;
3186}
3187
David Howells8f0cfa52008-04-29 00:59:41 -07003188static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189{
David Howells88e67f32008-11-14 10:39:21 +11003190 const struct cred *cred = current_cred();
3191
Eric Paris2875fa02011-04-28 16:04:24 -04003192 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193}
3194
Eric Paris828dfe12008-04-17 13:17:49 -04003195static int selinux_inode_listxattr(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196{
David Howells88e67f32008-11-14 10:39:21 +11003197 const struct cred *cred = current_cred();
3198
Eric Paris2875fa02011-04-28 16:04:24 -04003199 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200}
3201
David Howells8f0cfa52008-04-29 00:59:41 -07003202static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203{
Eric W. Biederman6b240302017-10-02 09:38:20 -05003204 if (strcmp(name, XATTR_NAME_SELINUX)) {
3205 int rc = cap_inode_removexattr(dentry, name);
3206 if (rc)
3207 return rc;
3208
3209 /* Not an attribute we recognize, so just check the
3210 ordinary setattr permission. */
3211 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213
3214 /* No one is allowed to remove a SELinux security label.
3215 You can change the label, but all data must be labeled. */
3216 return -EACCES;
3217}
3218
James Morrisd381d8a2005-10-30 14:59:22 -08003219/*
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003220 * Copy the inode security context value to the user.
James Morrisd381d8a2005-10-30 14:59:22 -08003221 *
3222 * Permission check is handled by selinux_inode_getxattr hook.
3223 */
Andreas Gruenbacherea861df2015-12-24 11:09:39 -05003224static int selinux_inode_getsecurity(struct inode *inode, const char *name, void **buffer, bool alloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225{
David P. Quigley42492592008-02-04 22:29:39 -08003226 u32 size;
3227 int error;
3228 char *context = NULL;
Paul Moore20cdef82016-04-04 14:14:42 -04003229 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00003231 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3232 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003234 /*
3235 * If the caller has CAP_MAC_ADMIN, then get the raw context
3236 * value even if it is not defined by current policy; otherwise,
3237 * use the in-core value under current policy.
3238 * Use the non-auditing forms of the permission checks since
3239 * getxattr may be called by unprivileged processes commonly
3240 * and lack of permission just means that we fall back to the
3241 * in-core context value, not a denial.
3242 */
Paul Moore20cdef82016-04-04 14:14:42 -04003243 isec = inode_security(inode);
Stephen Smalleydb590002017-04-20 11:31:30 -04003244 if (has_cap_mac_admin(false))
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003245 error = security_sid_to_context_force(&selinux_state,
3246 isec->sid, &context,
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003247 &size);
3248 else
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003249 error = security_sid_to_context(&selinux_state, isec->sid,
3250 &context, &size);
David P. Quigley42492592008-02-04 22:29:39 -08003251 if (error)
3252 return error;
3253 error = size;
3254 if (alloc) {
3255 *buffer = context;
3256 goto out_nofree;
3257 }
3258 kfree(context);
3259out_nofree:
3260 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261}
3262
3263static int selinux_inode_setsecurity(struct inode *inode, const char *name,
Eric Paris828dfe12008-04-17 13:17:49 -04003264 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265{
Paul Moore2c971652016-04-19 16:36:28 -04003266 struct inode_security_struct *isec = inode_security_novalidate(inode);
Ondrej Mosnacek53e0c2a2018-12-21 21:18:53 +01003267 struct superblock_security_struct *sbsec = inode->i_sb->s_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 u32 newsid;
3269 int rc;
3270
3271 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3272 return -EOPNOTSUPP;
3273
Ondrej Mosnacek53e0c2a2018-12-21 21:18:53 +01003274 if (!(sbsec->flags & SBLABEL_MNT))
3275 return -EOPNOTSUPP;
3276
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 if (!value || !size)
3278 return -EACCES;
3279
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003280 rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3281 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 if (rc)
3283 return rc;
3284
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003285 spin_lock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003286 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05003288 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003289 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 return 0;
3291}
3292
3293static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
3294{
3295 const int len = sizeof(XATTR_NAME_SELINUX);
3296 if (buffer && len <= buffer_size)
3297 memcpy(buffer, XATTR_NAME_SELINUX, len);
3298 return len;
3299}
3300
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05003301static void selinux_inode_getsecid(struct inode *inode, u32 *secid)
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02003302{
Andreas Gruenbachere817c2f2016-02-18 12:04:08 +01003303 struct inode_security_struct *isec = inode_security_novalidate(inode);
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02003304 *secid = isec->sid;
3305}
3306
Vivek Goyal56909eb2016-07-13 10:44:48 -04003307static int selinux_inode_copy_up(struct dentry *src, struct cred **new)
3308{
3309 u32 sid;
3310 struct task_security_struct *tsec;
3311 struct cred *new_creds = *new;
3312
3313 if (new_creds == NULL) {
3314 new_creds = prepare_creds();
3315 if (!new_creds)
3316 return -ENOMEM;
3317 }
3318
3319 tsec = new_creds->security;
3320 /* Get label from overlay inode and set it in create_sid */
3321 selinux_inode_getsecid(d_inode(src), &sid);
3322 tsec->create_sid = sid;
3323 *new = new_creds;
3324 return 0;
3325}
3326
Vivek Goyal19472b62016-07-13 10:44:50 -04003327static int selinux_inode_copy_up_xattr(const char *name)
3328{
3329 /* The copy_up hook above sets the initial context on an inode, but we
3330 * don't then want to overwrite it by blindly copying all the lower
3331 * xattrs up. Instead, we have to filter out SELinux-related xattrs.
3332 */
3333 if (strcmp(name, XATTR_NAME_SELINUX) == 0)
3334 return 1; /* Discard */
3335 /*
3336 * Any other attribute apart from SELINUX is not claimed, supported
3337 * by selinux.
3338 */
3339 return -EOPNOTSUPP;
3340}
3341
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342/* file security operations */
3343
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003344static int selinux_revalidate_file_permission(struct file *file, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345{
David Howells88e67f32008-11-14 10:39:21 +11003346 const struct cred *cred = current_cred();
Al Viro496ad9a2013-01-23 17:07:38 -05003347 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
3350 if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
3351 mask |= MAY_APPEND;
3352
Paul Moore389fb8002009-03-27 17:10:34 -04003353 return file_has_perm(cred, file,
3354 file_mask_to_av(inode->i_mode, mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355}
3356
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003357static int selinux_file_permission(struct file *file, int mask)
3358{
Al Viro496ad9a2013-01-23 17:07:38 -05003359 struct inode *inode = file_inode(file);
Stephen Smalley20dda182009-06-22 14:54:53 -04003360 struct file_security_struct *fsec = file->f_security;
Andreas Gruenbacherb1973672016-01-05 23:12:33 +01003361 struct inode_security_struct *isec;
Stephen Smalley20dda182009-06-22 14:54:53 -04003362 u32 sid = current_sid();
3363
Paul Moore389fb8002009-03-27 17:10:34 -04003364 if (!mask)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003365 /* No permission to check. Existence test. */
3366 return 0;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003367
Andreas Gruenbacherb1973672016-01-05 23:12:33 +01003368 isec = inode_security(inode);
Stephen Smalley20dda182009-06-22 14:54:53 -04003369 if (sid == fsec->sid && fsec->isid == isec->sid &&
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003370 fsec->pseqno == avc_policy_seqno(&selinux_state))
Eric Paris83d49852012-04-04 13:45:40 -04003371 /* No change since file_open check. */
Stephen Smalley20dda182009-06-22 14:54:53 -04003372 return 0;
3373
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003374 return selinux_revalidate_file_permission(file, mask);
3375}
3376
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377static int selinux_file_alloc_security(struct file *file)
3378{
3379 return file_alloc_security(file);
3380}
3381
3382static void selinux_file_free_security(struct file *file)
3383{
3384 file_free_security(file);
3385}
3386
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003387/*
3388 * Check whether a task has the ioctl permission and cmd
3389 * operation to an inode.
3390 */
Geliang Tang1d2a1682015-10-21 17:44:27 -04003391static int ioctl_has_perm(const struct cred *cred, struct file *file,
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003392 u32 requested, u16 cmd)
3393{
3394 struct common_audit_data ad;
3395 struct file_security_struct *fsec = file->f_security;
3396 struct inode *inode = file_inode(file);
Paul Moore20cdef82016-04-04 14:14:42 -04003397 struct inode_security_struct *isec;
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003398 struct lsm_ioctlop_audit ioctl;
3399 u32 ssid = cred_sid(cred);
3400 int rc;
3401 u8 driver = cmd >> 8;
3402 u8 xperm = cmd & 0xff;
3403
3404 ad.type = LSM_AUDIT_DATA_IOCTL_OP;
3405 ad.u.op = &ioctl;
3406 ad.u.op->cmd = cmd;
3407 ad.u.op->path = file->f_path;
3408
3409 if (ssid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003410 rc = avc_has_perm(&selinux_state,
3411 ssid, fsec->sid,
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003412 SECCLASS_FD,
3413 FD__USE,
3414 &ad);
3415 if (rc)
3416 goto out;
3417 }
3418
3419 if (unlikely(IS_PRIVATE(inode)))
3420 return 0;
3421
Paul Moore20cdef82016-04-04 14:14:42 -04003422 isec = inode_security(inode);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003423 rc = avc_has_extended_perms(&selinux_state,
3424 ssid, isec->sid, isec->sclass,
3425 requested, driver, xperm, &ad);
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003426out:
3427 return rc;
3428}
3429
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430static int selinux_file_ioctl(struct file *file, unsigned int cmd,
3431 unsigned long arg)
3432{
David Howells88e67f32008-11-14 10:39:21 +11003433 const struct cred *cred = current_cred();
Eric Paris0b24dcb2011-02-25 15:39:20 -05003434 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435
Eric Paris0b24dcb2011-02-25 15:39:20 -05003436 switch (cmd) {
3437 case FIONREAD:
3438 /* fall through */
3439 case FIBMAP:
3440 /* fall through */
3441 case FIGETBSZ:
3442 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003443 case FS_IOC_GETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003444 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003445 case FS_IOC_GETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003446 error = file_has_perm(cred, file, FILE__GETATTR);
3447 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448
Al Viro2f99c362012-03-23 16:04:05 -04003449 case FS_IOC_SETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003450 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003451 case FS_IOC_SETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003452 error = file_has_perm(cred, file, FILE__SETATTR);
3453 break;
3454
3455 /* sys_ioctl() checks */
3456 case FIONBIO:
3457 /* fall through */
3458 case FIOASYNC:
3459 error = file_has_perm(cred, file, 0);
3460 break;
3461
3462 case KDSKBENT:
3463 case KDSKBSENT:
Eric Paris6a9de492012-01-03 12:25:14 -05003464 error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04003465 SECURITY_CAP_AUDIT, true);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003466 break;
3467
3468 /* default case assumes that the command will go
3469 * to the file's ioctl() function.
3470 */
3471 default:
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003472 error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003473 }
3474 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475}
3476
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003477static int default_noexec;
3478
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
3480{
David Howells88e67f32008-11-14 10:39:21 +11003481 const struct cred *cred = current_cred();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003482 u32 sid = cred_sid(cred);
David Howellsd84f4f92008-11-14 10:39:23 +11003483 int rc = 0;
David Howells88e67f32008-11-14 10:39:21 +11003484
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003485 if (default_noexec &&
Stephen Smalley892e8ca2015-07-10 09:40:59 -04003486 (prot & PROT_EXEC) && (!file || IS_PRIVATE(file_inode(file)) ||
3487 (!shared && (prot & PROT_WRITE)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 /*
3489 * We are making executable an anonymous mapping or a
3490 * private file mapping that will also be writable.
3491 * This has an additional check.
3492 */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003493 rc = avc_has_perm(&selinux_state,
3494 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003495 PROCESS__EXECMEM, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 if (rc)
David Howellsd84f4f92008-11-14 10:39:23 +11003497 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499
3500 if (file) {
3501 /* read access is always possible with a mapping */
3502 u32 av = FILE__READ;
3503
3504 /* write access only matters if the mapping is shared */
3505 if (shared && (prot & PROT_WRITE))
3506 av |= FILE__WRITE;
3507
3508 if (prot & PROT_EXEC)
3509 av |= FILE__EXECUTE;
3510
David Howells88e67f32008-11-14 10:39:21 +11003511 return file_has_perm(cred, file, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 }
David Howellsd84f4f92008-11-14 10:39:23 +11003513
3514error:
3515 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516}
3517
Al Viroe5467852012-05-30 13:30:51 -04003518static int selinux_mmap_addr(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07003520 int rc = 0;
Paul Moore98883bf2014-03-19 16:46:11 -04003521
3522 if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
3523 u32 sid = current_sid();
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003524 rc = avc_has_perm(&selinux_state,
3525 sid, sid, SECCLASS_MEMPROTECT,
Paul Moore98883bf2014-03-19 16:46:11 -04003526 MEMPROTECT__MMAP_ZERO, NULL);
3527 }
3528
3529 return rc;
Al Viroe5467852012-05-30 13:30:51 -04003530}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531
Al Viroe5467852012-05-30 13:30:51 -04003532static int selinux_mmap_file(struct file *file, unsigned long reqprot,
3533 unsigned long prot, unsigned long flags)
3534{
Stephen Smalley3ba4bf52017-05-05 09:14:48 -04003535 struct common_audit_data ad;
3536 int rc;
3537
3538 if (file) {
3539 ad.type = LSM_AUDIT_DATA_FILE;
3540 ad.u.file = file;
3541 rc = inode_has_perm(current_cred(), file_inode(file),
3542 FILE__MAP, &ad);
3543 if (rc)
3544 return rc;
3545 }
3546
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003547 if (selinux_state.checkreqprot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 prot = reqprot;
3549
3550 return file_map_prot_check(file, prot,
3551 (flags & MAP_TYPE) == MAP_SHARED);
3552}
3553
3554static int selinux_file_mprotect(struct vm_area_struct *vma,
3555 unsigned long reqprot,
3556 unsigned long prot)
3557{
David Howells88e67f32008-11-14 10:39:21 +11003558 const struct cred *cred = current_cred();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003559 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003561 if (selinux_state.checkreqprot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 prot = reqprot;
3563
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003564 if (default_noexec &&
3565 (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
James Morrisd541bbe2009-01-29 12:19:51 +11003566 int rc = 0;
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003567 if (vma->vm_start >= vma->vm_mm->start_brk &&
3568 vma->vm_end <= vma->vm_mm->brk) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003569 rc = avc_has_perm(&selinux_state,
3570 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003571 PROCESS__EXECHEAP, NULL);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003572 } else if (!vma->vm_file &&
Stephen Smalleyc2316db2016-04-08 13:55:03 -04003573 ((vma->vm_start <= vma->vm_mm->start_stack &&
3574 vma->vm_end >= vma->vm_mm->start_stack) ||
Andy Lutomirskid17af502016-09-30 10:58:58 -07003575 vma_is_stack_for_current(vma))) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003576 rc = avc_has_perm(&selinux_state,
3577 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003578 PROCESS__EXECSTACK, NULL);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003579 } else if (vma->vm_file && vma->anon_vma) {
3580 /*
3581 * We are making executable a file mapping that has
3582 * had some COW done. Since pages might have been
3583 * written, check ability to execute the possibly
3584 * modified content. This typically should only
3585 * occur for text relocations.
3586 */
David Howellsd84f4f92008-11-14 10:39:23 +11003587 rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003588 }
Lorenzo Hernandez García-Hierro6b992192005-06-25 14:54:34 -07003589 if (rc)
3590 return rc;
3591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592
3593 return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
3594}
3595
3596static int selinux_file_lock(struct file *file, unsigned int cmd)
3597{
David Howells88e67f32008-11-14 10:39:21 +11003598 const struct cred *cred = current_cred();
3599
3600 return file_has_perm(cred, file, FILE__LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601}
3602
3603static int selinux_file_fcntl(struct file *file, unsigned int cmd,
3604 unsigned long arg)
3605{
David Howells88e67f32008-11-14 10:39:21 +11003606 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 int err = 0;
3608
3609 switch (cmd) {
Eric Paris828dfe12008-04-17 13:17:49 -04003610 case F_SETFL:
Eric Paris828dfe12008-04-17 13:17:49 -04003611 if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
David Howells88e67f32008-11-14 10:39:21 +11003612 err = file_has_perm(cred, file, FILE__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003614 }
3615 /* fall through */
3616 case F_SETOWN:
3617 case F_SETSIG:
3618 case F_GETFL:
3619 case F_GETOWN:
3620 case F_GETSIG:
Cyrill Gorcunov1d151c32012-07-30 14:43:00 -07003621 case F_GETOWNER_UIDS:
Eric Paris828dfe12008-04-17 13:17:49 -04003622 /* Just check FD__USE permission */
David Howells88e67f32008-11-14 10:39:21 +11003623 err = file_has_perm(cred, file, 0);
Eric Paris828dfe12008-04-17 13:17:49 -04003624 break;
3625 case F_GETLK:
3626 case F_SETLK:
3627 case F_SETLKW:
Jeff Layton0d3f7a22014-04-22 08:23:58 -04003628 case F_OFD_GETLK:
3629 case F_OFD_SETLK:
3630 case F_OFD_SETLKW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631#if BITS_PER_LONG == 32
Eric Paris828dfe12008-04-17 13:17:49 -04003632 case F_GETLK64:
3633 case F_SETLK64:
3634 case F_SETLKW64:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635#endif
David Howells88e67f32008-11-14 10:39:21 +11003636 err = file_has_perm(cred, file, FILE__LOCK);
Eric Paris828dfe12008-04-17 13:17:49 -04003637 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 }
3639
3640 return err;
3641}
3642
Jeff Laytone0b93ed2014-08-22 11:27:32 -04003643static void selinux_file_set_fowner(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 struct file_security_struct *fsec;
3646
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 fsec = file->f_security;
David Howells275bb412008-11-14 10:39:19 +11003648 fsec->fown_sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649}
3650
3651static int selinux_file_send_sigiotask(struct task_struct *tsk,
3652 struct fown_struct *fown, int signum)
3653{
Eric Paris828dfe12008-04-17 13:17:49 -04003654 struct file *file;
Stephen Smalley65c90bc2009-05-04 15:43:18 -04003655 u32 sid = task_sid(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 struct file_security_struct *fsec;
3658
3659 /* struct fown_struct is never outside the context of a struct file */
Eric Paris828dfe12008-04-17 13:17:49 -04003660 file = container_of(fown, struct file, f_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 fsec = file->f_security;
3663
3664 if (!signum)
3665 perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
3666 else
3667 perm = signal_to_av(signum);
3668
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003669 return avc_has_perm(&selinux_state,
3670 fsec->fown_sid, sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 SECCLASS_PROCESS, perm, NULL);
3672}
3673
3674static int selinux_file_receive(struct file *file)
3675{
David Howells88e67f32008-11-14 10:39:21 +11003676 const struct cred *cred = current_cred();
3677
3678 return file_has_perm(cred, file, file_to_av(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679}
3680
Al Viro94817692018-07-10 14:13:18 -04003681static int selinux_file_open(struct file *file)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003682{
3683 struct file_security_struct *fsec;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003684 struct inode_security_struct *isec;
David Howellsd84f4f92008-11-14 10:39:23 +11003685
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003686 fsec = file->f_security;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05003687 isec = inode_security(file_inode(file));
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003688 /*
3689 * Save inode label and policy sequence number
3690 * at open-time so that selinux_file_permission
3691 * can determine whether revalidation is necessary.
3692 * Task label is already saved in the file security
3693 * struct as its SID.
3694 */
3695 fsec->isid = isec->sid;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003696 fsec->pseqno = avc_policy_seqno(&selinux_state);
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003697 /*
3698 * Since the inode label or policy seqno may have changed
3699 * between the selinux_inode_permission check and the saving
3700 * of state above, recheck that access is still permitted.
3701 * Otherwise, access might never be revalidated against the
3702 * new inode label or new policy.
3703 * This check is not redundant - do not remove.
3704 */
Al Viro94817692018-07-10 14:13:18 -04003705 return file_path_has_perm(file->f_cred, file, open_file_to_av(file));
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003706}
3707
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708/* task security operations */
3709
Tetsuo Handaa79be232017-03-28 23:08:45 +09003710static int selinux_task_alloc(struct task_struct *task,
3711 unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003713 u32 sid = current_sid();
3714
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003715 return avc_has_perm(&selinux_state,
3716 sid, sid, SECCLASS_PROCESS, PROCESS__FORK, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717}
3718
David Howellsf1752ee2008-11-14 10:39:17 +11003719/*
David Howellsee18d642009-09-02 09:14:21 +01003720 * allocate the SELinux part of blank credentials
3721 */
3722static int selinux_cred_alloc_blank(struct cred *cred, gfp_t gfp)
3723{
3724 struct task_security_struct *tsec;
3725
3726 tsec = kzalloc(sizeof(struct task_security_struct), gfp);
3727 if (!tsec)
3728 return -ENOMEM;
3729
3730 cred->security = tsec;
3731 return 0;
3732}
3733
3734/*
David Howellsf1752ee2008-11-14 10:39:17 +11003735 * detach and free the LSM part of a set of credentials
3736 */
3737static void selinux_cred_free(struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738{
David Howellsf1752ee2008-11-14 10:39:17 +11003739 struct task_security_struct *tsec = cred->security;
David Howellse0e81732009-09-02 09:13:40 +01003740
Tetsuo Handa2edeaa32011-02-07 13:36:10 +00003741 /*
3742 * cred->security == NULL if security_cred_alloc_blank() or
3743 * security_prepare_creds() returned an error.
3744 */
3745 BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE);
David Howellse0e81732009-09-02 09:13:40 +01003746 cred->security = (void *) 0x7UL;
David Howellsf1752ee2008-11-14 10:39:17 +11003747 kfree(tsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748}
3749
David Howellsd84f4f92008-11-14 10:39:23 +11003750/*
3751 * prepare a new set of credentials for modification
3752 */
3753static int selinux_cred_prepare(struct cred *new, const struct cred *old,
3754 gfp_t gfp)
3755{
3756 const struct task_security_struct *old_tsec;
3757 struct task_security_struct *tsec;
3758
3759 old_tsec = old->security;
3760
3761 tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp);
3762 if (!tsec)
3763 return -ENOMEM;
3764
3765 new->security = tsec;
3766 return 0;
3767}
3768
3769/*
David Howellsee18d642009-09-02 09:14:21 +01003770 * transfer the SELinux data to a blank set of creds
3771 */
3772static void selinux_cred_transfer(struct cred *new, const struct cred *old)
3773{
3774 const struct task_security_struct *old_tsec = old->security;
3775 struct task_security_struct *tsec = new->security;
3776
3777 *tsec = *old_tsec;
3778}
3779
Matthew Garrett3ec30112018-01-08 13:36:19 -08003780static void selinux_cred_getsecid(const struct cred *c, u32 *secid)
3781{
3782 *secid = cred_sid(c);
3783}
3784
David Howellsee18d642009-09-02 09:14:21 +01003785/*
David Howells3a3b7ce2008-11-14 10:39:28 +11003786 * set the security data for a kernel service
3787 * - all the creation contexts are set to unlabelled
3788 */
3789static int selinux_kernel_act_as(struct cred *new, u32 secid)
3790{
3791 struct task_security_struct *tsec = new->security;
3792 u32 sid = current_sid();
3793 int ret;
3794
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003795 ret = avc_has_perm(&selinux_state,
3796 sid, secid,
David Howells3a3b7ce2008-11-14 10:39:28 +11003797 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
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003820 ret = avc_has_perm(&selinux_state,
3821 sid, isec->sid,
David Howells3a3b7ce2008-11-14 10:39:28 +11003822 SECCLASS_KERNEL_SERVICE,
3823 KERNEL_SERVICE__CREATE_FILES_AS,
3824 NULL);
3825
3826 if (ret == 0)
3827 tsec->create_sid = isec->sid;
David Howellsef574712010-02-26 01:56:16 +00003828 return ret;
David Howells3a3b7ce2008-11-14 10:39:28 +11003829}
3830
Eric Parisdd8dbf22009-11-03 16:35:32 +11003831static int selinux_kernel_module_request(char *kmod_name)
Eric Paris25354c42009-08-13 09:45:03 -04003832{
Eric Parisdd8dbf22009-11-03 16:35:32 +11003833 struct common_audit_data ad;
3834
Eric Paris50c205f2012-04-04 15:01:43 -04003835 ad.type = LSM_AUDIT_DATA_KMOD;
Eric Parisdd8dbf22009-11-03 16:35:32 +11003836 ad.u.kmod_name = kmod_name;
3837
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003838 return avc_has_perm(&selinux_state,
3839 current_sid(), SECINITSID_KERNEL, SECCLASS_SYSTEM,
Eric Parisdd8dbf22009-11-03 16:35:32 +11003840 SYSTEM__MODULE_REQUEST, &ad);
Eric Paris25354c42009-08-13 09:45:03 -04003841}
3842
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003843static int selinux_kernel_module_from_file(struct file *file)
3844{
3845 struct common_audit_data ad;
3846 struct inode_security_struct *isec;
3847 struct file_security_struct *fsec;
3848 u32 sid = current_sid();
3849 int rc;
3850
3851 /* init_module */
3852 if (file == NULL)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003853 return avc_has_perm(&selinux_state,
3854 sid, sid, SECCLASS_SYSTEM,
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003855 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 Stoep61d612ea2016-04-05 13:06:27 -07003861
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003862 fsec = file->f_security;
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003863 if (sid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003864 rc = avc_has_perm(&selinux_state,
3865 sid, fsec->sid, SECCLASS_FD, FD__USE, &ad);
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003866 if (rc)
3867 return rc;
3868 }
3869
Paul Moore20cdef82016-04-04 14:14:42 -04003870 isec = inode_security(file_inode(file));
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003871 return avc_has_perm(&selinux_state,
3872 sid, isec->sid, SECCLASS_SYSTEM,
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003873 SYSTEM__MODULE_LOAD, &ad);
3874}
3875
3876static int selinux_kernel_read_file(struct file *file,
3877 enum kernel_read_file_id id)
3878{
3879 int rc = 0;
3880
3881 switch (id) {
3882 case READING_MODULE:
3883 rc = selinux_kernel_module_from_file(file);
3884 break;
3885 default:
3886 break;
3887 }
3888
3889 return rc;
3890}
3891
Mimi Zoharc77b8cd2018-07-13 14:06:02 -04003892static int selinux_kernel_load_data(enum kernel_load_data_id id)
3893{
3894 int rc = 0;
3895
3896 switch (id) {
3897 case LOADING_MODULE:
3898 rc = selinux_kernel_module_from_file(NULL);
3899 default:
3900 break;
3901 }
3902
3903 return rc;
3904}
3905
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
3907{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003908 return avc_has_perm(&selinux_state,
3909 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003910 PROCESS__SETPGID, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911}
3912
3913static int selinux_task_getpgid(struct task_struct *p)
3914{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003915 return avc_has_perm(&selinux_state,
3916 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003917 PROCESS__GETPGID, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918}
3919
3920static int selinux_task_getsid(struct task_struct *p)
3921{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003922 return avc_has_perm(&selinux_state,
3923 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003924 PROCESS__GETSESSION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925}
3926
David Quigleyf9008e4c2006-06-30 01:55:46 -07003927static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
3928{
David Howells275bb412008-11-14 10:39:19 +11003929 *secid = task_sid(p);
David Quigleyf9008e4c2006-06-30 01:55:46 -07003930}
3931
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932static int selinux_task_setnice(struct task_struct *p, int nice)
3933{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003934 return avc_has_perm(&selinux_state,
3935 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003936 PROCESS__SETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937}
3938
James Morris03e68062006-06-23 02:03:58 -07003939static int selinux_task_setioprio(struct task_struct *p, int ioprio)
3940{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003941 return avc_has_perm(&selinux_state,
3942 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003943 PROCESS__SETSCHED, NULL);
James Morris03e68062006-06-23 02:03:58 -07003944}
3945
David Quigleya1836a42006-06-30 01:55:49 -07003946static int selinux_task_getioprio(struct task_struct *p)
3947{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003948 return avc_has_perm(&selinux_state,
3949 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003950 PROCESS__GETSCHED, NULL);
David Quigleya1836a42006-06-30 01:55:49 -07003951}
3952
Corentin LABBE42985552017-10-04 20:32:18 +02003953static int selinux_task_prlimit(const struct cred *cred, const struct cred *tcred,
3954 unsigned int flags)
Stephen Smalley791ec492017-02-17 07:57:00 -05003955{
3956 u32 av = 0;
3957
Stephen Smalley84e68852017-02-28 09:35:08 -05003958 if (!flags)
3959 return 0;
Stephen Smalley791ec492017-02-17 07:57:00 -05003960 if (flags & LSM_PRLIMIT_WRITE)
3961 av |= PROCESS__SETRLIMIT;
3962 if (flags & LSM_PRLIMIT_READ)
3963 av |= PROCESS__GETRLIMIT;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003964 return avc_has_perm(&selinux_state,
3965 cred_sid(cred), cred_sid(tcred),
Stephen Smalley791ec492017-02-17 07:57:00 -05003966 SECCLASS_PROCESS, av, NULL);
3967}
3968
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003969static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
3970 struct rlimit *new_rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971{
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003972 struct rlimit *old_rlim = p->signal->rlim + resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973
3974 /* Control the ability to change the hard limit (whether
3975 lowering or raising it), so that the hard limit can
3976 later be used as a safe reset point for the soft limit
David Howellsd84f4f92008-11-14 10:39:23 +11003977 upon context transitions. See selinux_bprm_committing_creds. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 if (old_rlim->rlim_max != new_rlim->rlim_max)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003979 return avc_has_perm(&selinux_state,
3980 current_sid(), task_sid(p),
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003981 SECCLASS_PROCESS, PROCESS__SETRLIMIT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982
3983 return 0;
3984}
3985
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09003986static int selinux_task_setscheduler(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003988 return avc_has_perm(&selinux_state,
3989 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003990 PROCESS__SETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991}
3992
3993static int selinux_task_getscheduler(struct task_struct *p)
3994{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003995 return avc_has_perm(&selinux_state,
3996 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003997 PROCESS__GETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998}
3999
David Quigley35601542006-06-23 02:04:01 -07004000static int selinux_task_movememory(struct task_struct *p)
4001{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004002 return avc_has_perm(&selinux_state,
4003 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004004 PROCESS__SETSCHED, NULL);
David Quigley35601542006-06-23 02:04:01 -07004005}
4006
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02004007static int selinux_task_kill(struct task_struct *p, struct kernel_siginfo *info,
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004008 int sig, const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009{
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004010 u32 secid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 if (!sig)
4014 perm = PROCESS__SIGNULL; /* null signal; existence test */
4015 else
4016 perm = signal_to_av(sig);
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004017 if (!cred)
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004018 secid = current_sid();
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004019 else
4020 secid = cred_sid(cred);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004021 return avc_has_perm(&selinux_state,
4022 secid, task_sid(p), SECCLASS_PROCESS, perm, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023}
4024
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025static void selinux_task_to_inode(struct task_struct *p,
4026 struct inode *inode)
4027{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 struct inode_security_struct *isec = inode->i_security;
David Howells275bb412008-11-14 10:39:19 +11004029 u32 sid = task_sid(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004031 spin_lock(&isec->lock);
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01004032 isec->sclass = inode_mode_to_security_class(inode->i_mode);
David Howells275bb412008-11-14 10:39:19 +11004033 isec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004034 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004035 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036}
4037
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004039static int selinux_parse_skb_ipv4(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04004040 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041{
4042 int offset, ihlen, ret = -EINVAL;
4043 struct iphdr _iph, *ih;
4044
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03004045 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
4047 if (ih == NULL)
4048 goto out;
4049
4050 ihlen = ih->ihl * 4;
4051 if (ihlen < sizeof(_iph))
4052 goto out;
4053
Eric Paris48c62af2012-04-02 13:15:44 -04004054 ad->u.net->v4info.saddr = ih->saddr;
4055 ad->u.net->v4info.daddr = ih->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 ret = 0;
4057
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004058 if (proto)
4059 *proto = ih->protocol;
4060
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 switch (ih->protocol) {
Eric Paris828dfe12008-04-17 13:17:49 -04004062 case IPPROTO_TCP: {
4063 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064
Eric Paris828dfe12008-04-17 13:17:49 -04004065 if (ntohs(ih->frag_off) & IP_OFFSET)
4066 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067
4068 offset += ihlen;
4069 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4070 if (th == NULL)
4071 break;
4072
Eric Paris48c62af2012-04-02 13:15:44 -04004073 ad->u.net->sport = th->source;
4074 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077
Eric Paris828dfe12008-04-17 13:17:49 -04004078 case IPPROTO_UDP: {
4079 struct udphdr _udph, *uh;
4080
4081 if (ntohs(ih->frag_off) & IP_OFFSET)
4082 break;
4083
4084 offset += ihlen;
4085 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4086 if (uh == NULL)
4087 break;
4088
Eric Paris48c62af2012-04-02 13:15:44 -04004089 ad->u.net->sport = uh->source;
4090 ad->u.net->dport = uh->dest;
Eric Paris828dfe12008-04-17 13:17:49 -04004091 break;
4092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093
James Morris2ee92d42006-11-13 16:09:01 -08004094 case IPPROTO_DCCP: {
4095 struct dccp_hdr _dccph, *dh;
4096
4097 if (ntohs(ih->frag_off) & IP_OFFSET)
4098 break;
4099
4100 offset += ihlen;
4101 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4102 if (dh == NULL)
4103 break;
4104
Eric Paris48c62af2012-04-02 13:15:44 -04004105 ad->u.net->sport = dh->dccph_sport;
4106 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08004107 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004108 }
James Morris2ee92d42006-11-13 16:09:01 -08004109
Richard Hainesd4529302018-02-13 20:57:18 +00004110#if IS_ENABLED(CONFIG_IP_SCTP)
4111 case IPPROTO_SCTP: {
4112 struct sctphdr _sctph, *sh;
4113
4114 if (ntohs(ih->frag_off) & IP_OFFSET)
4115 break;
4116
4117 offset += ihlen;
4118 sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4119 if (sh == NULL)
4120 break;
4121
4122 ad->u.net->sport = sh->source;
4123 ad->u.net->dport = sh->dest;
4124 break;
4125 }
4126#endif
Eric Paris828dfe12008-04-17 13:17:49 -04004127 default:
4128 break;
4129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130out:
4131 return ret;
4132}
4133
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04004134#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135
4136/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004137static int selinux_parse_skb_ipv6(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04004138 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139{
4140 u8 nexthdr;
4141 int ret = -EINVAL, offset;
4142 struct ipv6hdr _ipv6h, *ip6;
Jesse Gross75f28112011-11-30 17:05:51 -08004143 __be16 frag_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03004145 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
4147 if (ip6 == NULL)
4148 goto out;
4149
Eric Paris48c62af2012-04-02 13:15:44 -04004150 ad->u.net->v6info.saddr = ip6->saddr;
4151 ad->u.net->v6info.daddr = ip6->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 ret = 0;
4153
4154 nexthdr = ip6->nexthdr;
4155 offset += sizeof(_ipv6h);
Jesse Gross75f28112011-11-30 17:05:51 -08004156 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 if (offset < 0)
4158 goto out;
4159
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004160 if (proto)
4161 *proto = nexthdr;
4162
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 switch (nexthdr) {
4164 case IPPROTO_TCP: {
Eric Paris828dfe12008-04-17 13:17:49 -04004165 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166
4167 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4168 if (th == NULL)
4169 break;
4170
Eric Paris48c62af2012-04-02 13:15:44 -04004171 ad->u.net->sport = th->source;
4172 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 break;
4174 }
4175
4176 case IPPROTO_UDP: {
4177 struct udphdr _udph, *uh;
4178
4179 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4180 if (uh == NULL)
4181 break;
4182
Eric Paris48c62af2012-04-02 13:15:44 -04004183 ad->u.net->sport = uh->source;
4184 ad->u.net->dport = uh->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 break;
4186 }
4187
James Morris2ee92d42006-11-13 16:09:01 -08004188 case IPPROTO_DCCP: {
4189 struct dccp_hdr _dccph, *dh;
4190
4191 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4192 if (dh == NULL)
4193 break;
4194
Eric Paris48c62af2012-04-02 13:15:44 -04004195 ad->u.net->sport = dh->dccph_sport;
4196 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08004197 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004198 }
James Morris2ee92d42006-11-13 16:09:01 -08004199
Richard Hainesd4529302018-02-13 20:57:18 +00004200#if IS_ENABLED(CONFIG_IP_SCTP)
4201 case IPPROTO_SCTP: {
4202 struct sctphdr _sctph, *sh;
4203
4204 sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4205 if (sh == NULL)
4206 break;
4207
4208 ad->u.net->sport = sh->source;
4209 ad->u.net->dport = sh->dest;
4210 break;
4211 }
4212#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 /* includes fragments */
4214 default:
4215 break;
4216 }
4217out:
4218 return ret;
4219}
4220
4221#endif /* IPV6 */
4222
Thomas Liu2bf49692009-07-14 12:14:09 -04004223static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
David Howellscf9481e2008-07-27 21:31:07 +10004224 char **_addrp, int src, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225{
David Howellscf9481e2008-07-27 21:31:07 +10004226 char *addrp;
4227 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228
Eric Paris48c62af2012-04-02 13:15:44 -04004229 switch (ad->u.net->family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 case PF_INET:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004231 ret = selinux_parse_skb_ipv4(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10004232 if (ret)
4233 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04004234 addrp = (char *)(src ? &ad->u.net->v4info.saddr :
4235 &ad->u.net->v4info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10004236 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04004238#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 case PF_INET6:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004240 ret = selinux_parse_skb_ipv6(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10004241 if (ret)
4242 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04004243 addrp = (char *)(src ? &ad->u.net->v6info.saddr :
4244 &ad->u.net->v6info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10004245 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246#endif /* IPV6 */
4247 default:
David Howellscf9481e2008-07-27 21:31:07 +10004248 addrp = NULL;
4249 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 }
4251
David Howellscf9481e2008-07-27 21:31:07 +10004252parse_error:
peter enderborgc103a912018-06-12 10:09:03 +02004253 pr_warn(
David Howellscf9481e2008-07-27 21:31:07 +10004254 "SELinux: failure in selinux_parse_skb(),"
4255 " unable to parse packet\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 return ret;
David Howellscf9481e2008-07-27 21:31:07 +10004257
4258okay:
4259 if (_addrp)
4260 *_addrp = addrp;
4261 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262}
4263
Paul Moore4f6a9932007-03-01 14:35:22 -05004264/**
Paul Moore220deb92008-01-29 08:38:23 -05004265 * selinux_skb_peerlbl_sid - Determine the peer label of a packet
Paul Moore4f6a9932007-03-01 14:35:22 -05004266 * @skb: the packet
Paul Moore75e22912008-01-29 08:38:04 -05004267 * @family: protocol family
Paul Moore220deb92008-01-29 08:38:23 -05004268 * @sid: the packet's peer label SID
Paul Moore4f6a9932007-03-01 14:35:22 -05004269 *
4270 * Description:
Paul Moore220deb92008-01-29 08:38:23 -05004271 * Check the various different forms of network peer labeling and determine
4272 * the peer label/SID for the packet; most of the magic actually occurs in
4273 * the security server function security_net_peersid_cmp(). The function
4274 * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
4275 * or -EACCES if @sid is invalid due to inconsistencies with the different
4276 * peer labels.
Paul Moore4f6a9932007-03-01 14:35:22 -05004277 *
4278 */
Paul Moore220deb92008-01-29 08:38:23 -05004279static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
Paul Moore4f6a9932007-03-01 14:35:22 -05004280{
Paul Moore71f1cb02008-01-29 08:51:16 -05004281 int err;
Paul Moore4f6a9932007-03-01 14:35:22 -05004282 u32 xfrm_sid;
4283 u32 nlbl_sid;
Paul Moore220deb92008-01-29 08:38:23 -05004284 u32 nlbl_type;
Paul Moore4f6a9932007-03-01 14:35:22 -05004285
Paul Moore817eff72013-12-10 14:57:54 -05004286 err = selinux_xfrm_skb_sid(skb, &xfrm_sid);
Paul Moorebed4d7e2013-07-23 17:38:40 -04004287 if (unlikely(err))
4288 return -EACCES;
4289 err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
4290 if (unlikely(err))
4291 return -EACCES;
Paul Moore220deb92008-01-29 08:38:23 -05004292
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004293 err = security_net_peersid_resolve(&selinux_state, nlbl_sid,
4294 nlbl_type, xfrm_sid, sid);
Paul Moore71f1cb02008-01-29 08:51:16 -05004295 if (unlikely(err)) {
peter enderborgc103a912018-06-12 10:09:03 +02004296 pr_warn(
Paul Moore71f1cb02008-01-29 08:51:16 -05004297 "SELinux: failure in selinux_skb_peerlbl_sid(),"
4298 " unable to determine packet's peer label\n");
Paul Moore220deb92008-01-29 08:38:23 -05004299 return -EACCES;
Paul Moore71f1cb02008-01-29 08:51:16 -05004300 }
Paul Moore220deb92008-01-29 08:38:23 -05004301
4302 return 0;
Paul Moore4f6a9932007-03-01 14:35:22 -05004303}
4304
Paul Moore446b8022013-12-04 16:10:51 -05004305/**
4306 * selinux_conn_sid - Determine the child socket label for a connection
4307 * @sk_sid: the parent socket's SID
4308 * @skb_sid: the packet's SID
4309 * @conn_sid: the resulting connection SID
4310 *
4311 * If @skb_sid is valid then the user:role:type information from @sk_sid is
4312 * combined with the MLS information from @skb_sid in order to create
4313 * @conn_sid. If @skb_sid is not valid then then @conn_sid is simply a copy
4314 * of @sk_sid. Returns zero on success, negative values on failure.
4315 *
4316 */
4317static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid)
4318{
4319 int err = 0;
4320
4321 if (skb_sid != SECSID_NULL)
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004322 err = security_sid_mls_copy(&selinux_state, sk_sid, skb_sid,
4323 conn_sid);
Paul Moore446b8022013-12-04 16:10:51 -05004324 else
4325 *conn_sid = sk_sid;
4326
4327 return err;
4328}
4329
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330/* socket security operations */
Paul Moored4f2d972010-04-22 14:46:18 -04004331
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004332static int socket_sockcreate_sid(const struct task_security_struct *tsec,
4333 u16 secclass, u32 *socksid)
Paul Moored4f2d972010-04-22 14:46:18 -04004334{
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004335 if (tsec->sockcreate_sid > SECSID_NULL) {
4336 *socksid = tsec->sockcreate_sid;
4337 return 0;
4338 }
4339
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004340 return security_transition_sid(&selinux_state, tsec->sid, tsec->sid,
4341 secclass, NULL, socksid);
Paul Moored4f2d972010-04-22 14:46:18 -04004342}
4343
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004344static int sock_has_perm(struct sock *sk, u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345{
Paul Moore253bfae2010-04-22 14:46:19 -04004346 struct sk_security_struct *sksec = sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004347 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004348 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349
Paul Moore253bfae2010-04-22 14:46:19 -04004350 if (sksec->sid == SECINITSID_KERNEL)
4351 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352
Eric Paris50c205f2012-04-04 15:01:43 -04004353 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004354 ad.u.net = &net;
4355 ad.u.net->sk = sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004357 return avc_has_perm(&selinux_state,
4358 current_sid(), sksec->sid, sksec->sclass, perms,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004359 &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360}
4361
4362static int selinux_socket_create(int family, int type,
4363 int protocol, int kern)
4364{
Paul Moore5fb49872010-04-22 14:46:19 -04004365 const struct task_security_struct *tsec = current_security();
Paul Moored4f2d972010-04-22 14:46:18 -04004366 u32 newsid;
David Howells275bb412008-11-14 10:39:19 +11004367 u16 secclass;
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004368 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369
4370 if (kern)
Paul Moored4f2d972010-04-22 14:46:18 -04004371 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372
David Howells275bb412008-11-14 10:39:19 +11004373 secclass = socket_type_to_security_class(family, type, protocol);
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004374 rc = socket_sockcreate_sid(tsec, secclass, &newsid);
4375 if (rc)
4376 return rc;
4377
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004378 return avc_has_perm(&selinux_state,
4379 tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380}
4381
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07004382static int selinux_socket_post_create(struct socket *sock, int family,
4383 int type, int protocol, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384{
Paul Moore5fb49872010-04-22 14:46:19 -04004385 const struct task_security_struct *tsec = current_security();
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05004386 struct inode_security_struct *isec = inode_security_novalidate(SOCK_INODE(sock));
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004387 struct sk_security_struct *sksec;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004388 u16 sclass = socket_type_to_security_class(family, type, protocol);
4389 u32 sid = SECINITSID_KERNEL;
David Howells275bb412008-11-14 10:39:19 +11004390 int err = 0;
4391
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004392 if (!kern) {
4393 err = socket_sockcreate_sid(tsec, sclass, &sid);
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004394 if (err)
4395 return err;
4396 }
David Howells275bb412008-11-14 10:39:19 +11004397
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004398 isec->sclass = sclass;
4399 isec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004400 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004402 if (sock->sk) {
4403 sksec = sock->sk->sk_security;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004404 sksec->sclass = sclass;
4405 sksec->sid = sid;
Richard Hainesd4529302018-02-13 20:57:18 +00004406 /* Allows detection of the first association on this socket */
4407 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4408 sksec->sctp_assoc_state = SCTP_ASSOC_UNSET;
4409
Paul Moore389fb8002009-03-27 17:10:34 -04004410 err = selinux_netlbl_socket_post_create(sock->sk, family);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004411 }
4412
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07004413 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414}
4415
David Herrmann0b811db2018-05-04 16:28:21 +02004416static int selinux_socket_socketpair(struct socket *socka,
4417 struct socket *sockb)
4418{
4419 struct sk_security_struct *sksec_a = socka->sk->sk_security;
4420 struct sk_security_struct *sksec_b = sockb->sk->sk_security;
4421
4422 sksec_a->peer_sid = sksec_b->sid;
4423 sksec_b->peer_sid = sksec_a->sid;
4424
4425 return 0;
4426}
4427
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428/* Range of port numbers used to automatically bind.
4429 Need to determine whether we should perform a name_bind
4430 permission check between the socket and the port number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431
4432static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
4433{
Paul Moore253bfae2010-04-22 14:46:19 -04004434 struct sock *sk = sock->sk;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004435 struct sk_security_struct *sksec = sk->sk_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 u16 family;
4437 int err;
4438
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004439 err = sock_has_perm(sk, SOCKET__BIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 if (err)
4441 goto out;
4442
Richard Hainesd4529302018-02-13 20:57:18 +00004443 /* If PF_INET or PF_INET6, check name_bind permission for the port. */
Paul Moore253bfae2010-04-22 14:46:19 -04004444 family = sk->sk_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 if (family == PF_INET || family == PF_INET6) {
4446 char *addrp;
Thomas Liu2bf49692009-07-14 12:14:09 -04004447 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004448 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 struct sockaddr_in *addr4 = NULL;
4450 struct sockaddr_in6 *addr6 = NULL;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004451 u16 family_sa = address->sa_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 unsigned short snum;
James Morrise399f982008-06-12 01:39:58 +10004453 u32 sid, node_perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454
Richard Hainesd4529302018-02-13 20:57:18 +00004455 /*
4456 * sctp_bindx(3) calls via selinux_sctp_bind_connect()
4457 * that validates multiple binding addresses. Because of this
4458 * need to check address->sa_family as it is possible to have
4459 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4460 */
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004461 switch (family_sa) {
4462 case AF_UNSPEC:
Richard Haines68741a8a2018-03-02 19:54:34 +00004463 case AF_INET:
4464 if (addrlen < sizeof(struct sockaddr_in))
4465 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 addr4 = (struct sockaddr_in *)address;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004467 if (family_sa == AF_UNSPEC) {
4468 /* see __inet_bind(), we only want to allow
4469 * AF_UNSPEC if the address is INADDR_ANY
4470 */
4471 if (addr4->sin_addr.s_addr != htonl(INADDR_ANY))
4472 goto err_af;
4473 family_sa = AF_INET;
4474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 snum = ntohs(addr4->sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 addrp = (char *)&addr4->sin_addr.s_addr;
Richard Haines68741a8a2018-03-02 19:54:34 +00004477 break;
4478 case AF_INET6:
4479 if (addrlen < SIN6_LEN_RFC2133)
4480 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 addr6 = (struct sockaddr_in6 *)address;
4482 snum = ntohs(addr6->sin6_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 addrp = (char *)&addr6->sin6_addr.s6_addr;
Richard Haines68741a8a2018-03-02 19:54:34 +00004484 break;
4485 default:
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004486 goto err_af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 }
4488
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004489 ad.type = LSM_AUDIT_DATA_NET;
4490 ad.u.net = &net;
4491 ad.u.net->sport = htons(snum);
4492 ad.u.net->family = family_sa;
4493
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004494 if (snum) {
4495 int low, high;
4496
Eric W. Biederman0bbf87d2013-09-28 14:10:59 -07004497 inet_get_local_port_range(sock_net(sk), &low, &high);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004498
Krister Johansen4548b682017-01-20 17:49:11 -08004499 if (snum < max(inet_prot_sock(sock_net(sk)), low) ||
4500 snum > high) {
Paul Moore3e1121722008-04-10 10:48:14 -04004501 err = sel_netport_sid(sk->sk_protocol,
4502 snum, &sid);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004503 if (err)
4504 goto out;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004505 err = avc_has_perm(&selinux_state,
4506 sksec->sid, sid,
Paul Moore253bfae2010-04-22 14:46:19 -04004507 sksec->sclass,
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004508 SOCKET__NAME_BIND, &ad);
4509 if (err)
4510 goto out;
4511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 }
Eric Paris828dfe12008-04-17 13:17:49 -04004513
Paul Moore253bfae2010-04-22 14:46:19 -04004514 switch (sksec->sclass) {
James Morris13402582005-09-30 14:24:34 -04004515 case SECCLASS_TCP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 node_perm = TCP_SOCKET__NODE_BIND;
4517 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004518
James Morris13402582005-09-30 14:24:34 -04004519 case SECCLASS_UDP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520 node_perm = UDP_SOCKET__NODE_BIND;
4521 break;
James Morris2ee92d42006-11-13 16:09:01 -08004522
4523 case SECCLASS_DCCP_SOCKET:
4524 node_perm = DCCP_SOCKET__NODE_BIND;
4525 break;
4526
Richard Hainesd4529302018-02-13 20:57:18 +00004527 case SECCLASS_SCTP_SOCKET:
4528 node_perm = SCTP_SOCKET__NODE_BIND;
4529 break;
4530
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 default:
4532 node_perm = RAWIP_SOCKET__NODE_BIND;
4533 break;
4534 }
Eric Paris828dfe12008-04-17 13:17:49 -04004535
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004536 err = sel_netnode_sid(addrp, family_sa, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 if (err)
4538 goto out;
Eric Paris828dfe12008-04-17 13:17:49 -04004539
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004540 if (family_sa == AF_INET)
Eric Paris48c62af2012-04-02 13:15:44 -04004541 ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 else
Eric Paris48c62af2012-04-02 13:15:44 -04004543 ad.u.net->v6info.saddr = addr6->sin6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004545 err = avc_has_perm(&selinux_state,
4546 sksec->sid, sid,
Paul Moore253bfae2010-04-22 14:46:19 -04004547 sksec->sclass, node_perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548 if (err)
4549 goto out;
4550 }
4551out:
4552 return err;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004553err_af:
4554 /* Note that SCTP services expect -EINVAL, others -EAFNOSUPPORT. */
4555 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4556 return -EINVAL;
4557 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558}
4559
Richard Hainesd4529302018-02-13 20:57:18 +00004560/* This supports connect(2) and SCTP connect services such as sctp_connectx(3)
Mauro Carvalho Chehab5fb94e92018-05-08 15:14:57 -03004561 * and sctp_sendmsg(3) as described in Documentation/security/LSM-sctp.rst
Richard Hainesd4529302018-02-13 20:57:18 +00004562 */
4563static int selinux_socket_connect_helper(struct socket *sock,
4564 struct sockaddr *address, int addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565{
Paul Moore014ab192008-10-10 10:16:33 -04004566 struct sock *sk = sock->sk;
Paul Moore253bfae2010-04-22 14:46:19 -04004567 struct sk_security_struct *sksec = sk->sk_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568 int err;
4569
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004570 err = sock_has_perm(sk, SOCKET__CONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 if (err)
4572 return err;
4573
4574 /*
Richard Hainesd4529302018-02-13 20:57:18 +00004575 * If a TCP, DCCP or SCTP socket, check name_connect permission
4576 * for the port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 */
Paul Moore253bfae2010-04-22 14:46:19 -04004578 if (sksec->sclass == SECCLASS_TCP_SOCKET ||
Richard Hainesd4529302018-02-13 20:57:18 +00004579 sksec->sclass == SECCLASS_DCCP_SOCKET ||
4580 sksec->sclass == SECCLASS_SCTP_SOCKET) {
Thomas Liu2bf49692009-07-14 12:14:09 -04004581 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004582 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 struct sockaddr_in *addr4 = NULL;
4584 struct sockaddr_in6 *addr6 = NULL;
4585 unsigned short snum;
James Morris2ee92d42006-11-13 16:09:01 -08004586 u32 sid, perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587
Richard Hainesd4529302018-02-13 20:57:18 +00004588 /* sctp_connectx(3) calls via selinux_sctp_bind_connect()
4589 * that validates multiple connect addresses. Because of this
4590 * need to check address->sa_family as it is possible to have
4591 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4592 */
Richard Haines68741a8a2018-03-02 19:54:34 +00004593 switch (address->sa_family) {
4594 case AF_INET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 addr4 = (struct sockaddr_in *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004596 if (addrlen < sizeof(struct sockaddr_in))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 return -EINVAL;
4598 snum = ntohs(addr4->sin_port);
Richard Haines68741a8a2018-03-02 19:54:34 +00004599 break;
4600 case AF_INET6:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601 addr6 = (struct sockaddr_in6 *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004602 if (addrlen < SIN6_LEN_RFC2133)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 return -EINVAL;
4604 snum = ntohs(addr6->sin6_port);
Richard Haines68741a8a2018-03-02 19:54:34 +00004605 break;
4606 default:
4607 /* Note that SCTP services expect -EINVAL, whereas
4608 * others expect -EAFNOSUPPORT.
4609 */
4610 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4611 return -EINVAL;
4612 else
4613 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614 }
4615
Paul Moore3e1121722008-04-10 10:48:14 -04004616 err = sel_netport_sid(sk->sk_protocol, snum, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 if (err)
Richard Hainesd4529302018-02-13 20:57:18 +00004618 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619
Richard Hainesd4529302018-02-13 20:57:18 +00004620 switch (sksec->sclass) {
4621 case SECCLASS_TCP_SOCKET:
4622 perm = TCP_SOCKET__NAME_CONNECT;
4623 break;
4624 case SECCLASS_DCCP_SOCKET:
4625 perm = DCCP_SOCKET__NAME_CONNECT;
4626 break;
4627 case SECCLASS_SCTP_SOCKET:
4628 perm = SCTP_SOCKET__NAME_CONNECT;
4629 break;
4630 }
James Morris2ee92d42006-11-13 16:09:01 -08004631
Eric Paris50c205f2012-04-04 15:01:43 -04004632 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004633 ad.u.net = &net;
4634 ad.u.net->dport = htons(snum);
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004635 ad.u.net->family = address->sa_family;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004636 err = avc_has_perm(&selinux_state,
4637 sksec->sid, sid, sksec->sclass, perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 if (err)
Richard Hainesd4529302018-02-13 20:57:18 +00004639 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 }
4641
Richard Hainesd4529302018-02-13 20:57:18 +00004642 return 0;
4643}
Paul Moore014ab192008-10-10 10:16:33 -04004644
Richard Hainesd4529302018-02-13 20:57:18 +00004645/* Supports connect(2), see comments in selinux_socket_connect_helper() */
4646static int selinux_socket_connect(struct socket *sock,
4647 struct sockaddr *address, int addrlen)
4648{
4649 int err;
4650 struct sock *sk = sock->sk;
4651
4652 err = selinux_socket_connect_helper(sock, address, addrlen);
4653 if (err)
4654 return err;
4655
4656 return selinux_netlbl_socket_connect(sk, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657}
4658
4659static int selinux_socket_listen(struct socket *sock, int backlog)
4660{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004661 return sock_has_perm(sock->sk, SOCKET__LISTEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662}
4663
4664static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
4665{
4666 int err;
4667 struct inode_security_struct *isec;
4668 struct inode_security_struct *newisec;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004669 u16 sclass;
4670 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004672 err = sock_has_perm(sock->sk, SOCKET__ACCEPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 if (err)
4674 return err;
4675
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05004676 isec = inode_security_novalidate(SOCK_INODE(sock));
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004677 spin_lock(&isec->lock);
4678 sclass = isec->sclass;
4679 sid = isec->sid;
4680 spin_unlock(&isec->lock);
4681
4682 newisec = inode_security_novalidate(SOCK_INODE(newsock));
4683 newisec->sclass = sclass;
4684 newisec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004685 newisec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686
4687 return 0;
4688}
4689
4690static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
Eric Paris828dfe12008-04-17 13:17:49 -04004691 int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004693 return sock_has_perm(sock->sk, SOCKET__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694}
4695
4696static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
4697 int size, int flags)
4698{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004699 return sock_has_perm(sock->sk, SOCKET__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700}
4701
4702static int selinux_socket_getsockname(struct socket *sock)
4703{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004704 return sock_has_perm(sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705}
4706
4707static int selinux_socket_getpeername(struct socket *sock)
4708{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004709 return sock_has_perm(sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710}
4711
Eric Paris828dfe12008-04-17 13:17:49 -04004712static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713{
Paul Mooref8687af2006-10-30 15:22:15 -08004714 int err;
4715
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004716 err = sock_has_perm(sock->sk, SOCKET__SETOPT);
Paul Mooref8687af2006-10-30 15:22:15 -08004717 if (err)
4718 return err;
4719
4720 return selinux_netlbl_socket_setsockopt(sock, level, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721}
4722
4723static int selinux_socket_getsockopt(struct socket *sock, int level,
4724 int optname)
4725{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004726 return sock_has_perm(sock->sk, SOCKET__GETOPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727}
4728
4729static int selinux_socket_shutdown(struct socket *sock, int how)
4730{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004731 return sock_has_perm(sock->sk, SOCKET__SHUTDOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732}
4733
David S. Miller3610cda2011-01-05 15:38:53 -08004734static int selinux_socket_unix_stream_connect(struct sock *sock,
4735 struct sock *other,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 struct sock *newsk)
4737{
David S. Miller3610cda2011-01-05 15:38:53 -08004738 struct sk_security_struct *sksec_sock = sock->sk_security;
4739 struct sk_security_struct *sksec_other = other->sk_security;
Paul Moore4d1e2452010-04-22 14:46:18 -04004740 struct sk_security_struct *sksec_new = newsk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004741 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004742 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743 int err;
4744
Eric Paris50c205f2012-04-04 15:01:43 -04004745 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004746 ad.u.net = &net;
4747 ad.u.net->sk = other;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004749 err = avc_has_perm(&selinux_state,
4750 sksec_sock->sid, sksec_other->sid,
Paul Moore4d1e2452010-04-22 14:46:18 -04004751 sksec_other->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752 UNIX_STREAM_SOCKET__CONNECTTO, &ad);
4753 if (err)
4754 return err;
4755
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756 /* server child socket */
Paul Moore4d1e2452010-04-22 14:46:18 -04004757 sksec_new->peer_sid = sksec_sock->sid;
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004758 err = security_sid_mls_copy(&selinux_state, sksec_other->sid,
4759 sksec_sock->sid, &sksec_new->sid);
Paul Moore4d1e2452010-04-22 14:46:18 -04004760 if (err)
4761 return err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004762
Paul Moore4d1e2452010-04-22 14:46:18 -04004763 /* connecting socket */
4764 sksec_sock->peer_sid = sksec_new->sid;
4765
4766 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767}
4768
4769static int selinux_socket_unix_may_send(struct socket *sock,
4770 struct socket *other)
4771{
Paul Moore253bfae2010-04-22 14:46:19 -04004772 struct sk_security_struct *ssec = sock->sk->sk_security;
4773 struct sk_security_struct *osec = other->sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004774 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004775 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776
Eric Paris50c205f2012-04-04 15:01:43 -04004777 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004778 ad.u.net = &net;
4779 ad.u.net->sk = other->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004781 return avc_has_perm(&selinux_state,
4782 ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
Paul Moore253bfae2010-04-22 14:46:19 -04004783 &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784}
4785
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004786static int selinux_inet_sys_rcv_skb(struct net *ns, int ifindex,
4787 char *addrp, u16 family, u32 peer_sid,
Thomas Liu2bf49692009-07-14 12:14:09 -04004788 struct common_audit_data *ad)
Paul Mooreeffad8d2008-01-29 08:49:27 -05004789{
4790 int err;
4791 u32 if_sid;
4792 u32 node_sid;
4793
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004794 err = sel_netif_sid(ns, ifindex, &if_sid);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004795 if (err)
4796 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004797 err = avc_has_perm(&selinux_state,
4798 peer_sid, if_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004799 SECCLASS_NETIF, NETIF__INGRESS, ad);
4800 if (err)
4801 return err;
4802
4803 err = sel_netnode_sid(addrp, family, &node_sid);
4804 if (err)
4805 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004806 return avc_has_perm(&selinux_state,
4807 peer_sid, node_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004808 SECCLASS_NODE, NODE__RECVFROM, ad);
4809}
4810
Paul Moore220deb92008-01-29 08:38:23 -05004811static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
Paul Moored8395c82008-10-10 10:16:30 -04004812 u16 family)
Paul Moore220deb92008-01-29 08:38:23 -05004813{
Paul Moore277d3422008-12-31 12:54:11 -05004814 int err = 0;
Paul Moore220deb92008-01-29 08:38:23 -05004815 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004816 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004817 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004818 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04004819 char *addrp;
4820
Eric Paris50c205f2012-04-04 15:01:43 -04004821 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004822 ad.u.net = &net;
4823 ad.u.net->netif = skb->skb_iif;
4824 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004825 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4826 if (err)
4827 return err;
Paul Moore220deb92008-01-29 08:38:23 -05004828
Paul Moore58bfbb52009-03-27 17:10:41 -04004829 if (selinux_secmark_enabled()) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004830 err = avc_has_perm(&selinux_state,
4831 sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Moored8395c82008-10-10 10:16:30 -04004832 PACKET__RECV, &ad);
Paul Moore58bfbb52009-03-27 17:10:41 -04004833 if (err)
4834 return err;
4835 }
Paul Moore220deb92008-01-29 08:38:23 -05004836
Steffen Klassertb9679a72011-02-23 12:55:21 +01004837 err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
4838 if (err)
4839 return err;
4840 err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004841
James Morris4e5ab4c2006-06-09 00:33:33 -07004842 return err;
4843}
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004844
James Morris4e5ab4c2006-06-09 00:33:33 -07004845static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
4846{
Paul Moore220deb92008-01-29 08:38:23 -05004847 int err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004848 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004849 u16 family = sk->sk_family;
4850 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004851 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004852 struct lsm_network_audit net = {0,};
Paul Moore220deb92008-01-29 08:38:23 -05004853 char *addrp;
Paul Moored8395c82008-10-10 10:16:30 -04004854 u8 secmark_active;
4855 u8 peerlbl_active;
James Morris4e5ab4c2006-06-09 00:33:33 -07004856
James Morris4e5ab4c2006-06-09 00:33:33 -07004857 if (family != PF_INET && family != PF_INET6)
Paul Moore220deb92008-01-29 08:38:23 -05004858 return 0;
James Morris4e5ab4c2006-06-09 00:33:33 -07004859
4860 /* Handle mapped IPv4 packets arriving via IPv6 sockets */
Al Viro87fcd702006-12-04 22:00:55 +00004861 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
James Morris4e5ab4c2006-06-09 00:33:33 -07004862 family = PF_INET;
4863
Paul Moored8395c82008-10-10 10:16:30 -04004864 /* If any sort of compatibility mode is enabled then handoff processing
4865 * to the selinux_sock_rcv_skb_compat() function to deal with the
4866 * special handling. We do this in an attempt to keep this function
4867 * as fast and as clean as possible. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004868 if (!selinux_policycap_netpeer())
Paul Moored8395c82008-10-10 10:16:30 -04004869 return selinux_sock_rcv_skb_compat(sk, skb, family);
4870
4871 secmark_active = selinux_secmark_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04004872 peerlbl_active = selinux_peerlbl_enabled();
Paul Moored8395c82008-10-10 10:16:30 -04004873 if (!secmark_active && !peerlbl_active)
4874 return 0;
4875
Eric Paris50c205f2012-04-04 15:01:43 -04004876 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004877 ad.u.net = &net;
4878 ad.u.net->netif = skb->skb_iif;
4879 ad.u.net->family = family;
Paul Moore224dfbd2008-01-29 08:38:13 -05004880 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
James Morris4e5ab4c2006-06-09 00:33:33 -07004881 if (err)
Paul Moore220deb92008-01-29 08:38:23 -05004882 return err;
James Morris4e5ab4c2006-06-09 00:33:33 -07004883
Paul Moored8395c82008-10-10 10:16:30 -04004884 if (peerlbl_active) {
Paul Moored621d352008-01-29 08:43:36 -05004885 u32 peer_sid;
4886
4887 err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
4888 if (err)
4889 return err;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004890 err = selinux_inet_sys_rcv_skb(sock_net(sk), skb->skb_iif,
4891 addrp, family, peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04004892 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04004893 selinux_netlbl_err(skb, family, err, 0);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004894 return err;
Paul Mooredfaebe92008-10-10 10:16:31 -04004895 }
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004896 err = avc_has_perm(&selinux_state,
4897 sk_sid, peer_sid, SECCLASS_PEER,
Paul Moored621d352008-01-29 08:43:36 -05004898 PEER__RECV, &ad);
Chad Hanson46d01d62013-12-23 17:45:01 -05004899 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04004900 selinux_netlbl_err(skb, family, err, 0);
Chad Hanson46d01d62013-12-23 17:45:01 -05004901 return err;
4902 }
Paul Moored621d352008-01-29 08:43:36 -05004903 }
4904
Paul Moored8395c82008-10-10 10:16:30 -04004905 if (secmark_active) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004906 err = avc_has_perm(&selinux_state,
4907 sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004908 PACKET__RECV, &ad);
4909 if (err)
4910 return err;
4911 }
4912
Paul Moored621d352008-01-29 08:43:36 -05004913 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914}
4915
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004916static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
4917 int __user *optlen, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918{
4919 int err = 0;
4920 char *scontext;
4921 u32 scontext_len;
Paul Moore253bfae2010-04-22 14:46:19 -04004922 struct sk_security_struct *sksec = sock->sk->sk_security;
Paul Moore3de4bab2006-11-17 17:38:54 -05004923 u32 peer_sid = SECSID_NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924
Paul Moore253bfae2010-04-22 14:46:19 -04004925 if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
Richard Hainesd4529302018-02-13 20:57:18 +00004926 sksec->sclass == SECCLASS_TCP_SOCKET ||
4927 sksec->sclass == SECCLASS_SCTP_SOCKET)
Eric Parisdd3e7832010-04-07 15:08:46 -04004928 peer_sid = sksec->peer_sid;
Paul Moore253bfae2010-04-22 14:46:19 -04004929 if (peer_sid == SECSID_NULL)
4930 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004932 err = security_sid_to_context(&selinux_state, peer_sid, &scontext,
4933 &scontext_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934 if (err)
Paul Moore253bfae2010-04-22 14:46:19 -04004935 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936
4937 if (scontext_len > len) {
4938 err = -ERANGE;
4939 goto out_len;
4940 }
4941
4942 if (copy_to_user(optval, scontext, scontext_len))
4943 err = -EFAULT;
4944
4945out_len:
4946 if (put_user(scontext_len, optlen))
4947 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948 kfree(scontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 return err;
4950}
4951
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004952static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004953{
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004954 u32 peer_secid = SECSID_NULL;
Paul Moore75e22912008-01-29 08:38:04 -05004955 u16 family;
Paul Moore899134f2016-03-28 15:19:10 -04004956 struct inode_security_struct *isec;
Catherine Zhang877ce7c2006-06-29 12:27:47 -07004957
Paul Mooreaa862902008-10-10 10:16:29 -04004958 if (skb && skb->protocol == htons(ETH_P_IP))
4959 family = PF_INET;
4960 else if (skb && skb->protocol == htons(ETH_P_IPV6))
4961 family = PF_INET6;
4962 else if (sock)
Paul Moore75e22912008-01-29 08:38:04 -05004963 family = sock->sk->sk_family;
Paul Moore75e22912008-01-29 08:38:04 -05004964 else
4965 goto out;
4966
Paul Moore899134f2016-03-28 15:19:10 -04004967 if (sock && family == PF_UNIX) {
4968 isec = inode_security_novalidate(SOCK_INODE(sock));
4969 peer_secid = isec->sid;
4970 } else if (skb)
Paul Moore220deb92008-01-29 08:38:23 -05004971 selinux_skb_peerlbl_sid(skb, family, &peer_secid);
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004972
Paul Moore75e22912008-01-29 08:38:04 -05004973out:
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004974 *secid = peer_secid;
Paul Moore75e22912008-01-29 08:38:04 -05004975 if (peer_secid == SECSID_NULL)
4976 return -EINVAL;
4977 return 0;
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004978}
4979
Al Viro7d877f32005-10-21 03:20:43 -04004980static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981{
Paul Moore84914b72010-04-22 14:46:18 -04004982 struct sk_security_struct *sksec;
4983
4984 sksec = kzalloc(sizeof(*sksec), priority);
4985 if (!sksec)
4986 return -ENOMEM;
4987
4988 sksec->peer_sid = SECINITSID_UNLABELED;
4989 sksec->sid = SECINITSID_UNLABELED;
Stephen Smalley5dee25d2015-07-10 17:19:57 -04004990 sksec->sclass = SECCLASS_SOCKET;
Paul Moore84914b72010-04-22 14:46:18 -04004991 selinux_netlbl_sk_security_reset(sksec);
4992 sk->sk_security = sksec;
4993
4994 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995}
4996
4997static void selinux_sk_free_security(struct sock *sk)
4998{
Paul Moore84914b72010-04-22 14:46:18 -04004999 struct sk_security_struct *sksec = sk->sk_security;
5000
5001 sk->sk_security = NULL;
5002 selinux_netlbl_sk_security_free(sksec);
5003 kfree(sksec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004}
5005
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005006static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
5007{
Eric Parisdd3e7832010-04-07 15:08:46 -04005008 struct sk_security_struct *sksec = sk->sk_security;
5009 struct sk_security_struct *newsksec = newsk->sk_security;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005010
Eric Parisdd3e7832010-04-07 15:08:46 -04005011 newsksec->sid = sksec->sid;
5012 newsksec->peer_sid = sksec->peer_sid;
5013 newsksec->sclass = sksec->sclass;
Paul Moore99f59ed2006-08-29 17:53:48 -07005014
Eric Parisdd3e7832010-04-07 15:08:46 -04005015 selinux_netlbl_sk_security_reset(newsksec);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005016}
5017
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07005018static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005019{
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005020 if (!sk)
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07005021 *secid = SECINITSID_ANY_SOCKET;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005022 else {
5023 struct sk_security_struct *sksec = sk->sk_security;
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005024
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07005025 *secid = sksec->sid;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005026 }
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005027}
5028
Eric Paris828dfe12008-04-17 13:17:49 -04005029static void selinux_sock_graft(struct sock *sk, struct socket *parent)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005030{
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05005031 struct inode_security_struct *isec =
5032 inode_security_novalidate(SOCK_INODE(parent));
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005033 struct sk_security_struct *sksec = sk->sk_security;
5034
Paul Moore2873ead2014-07-28 10:42:48 -04005035 if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
5036 sk->sk_family == PF_UNIX)
David Woodhouse2148ccc2006-09-29 15:50:25 -07005037 isec->sid = sksec->sid;
Paul Moore220deb92008-01-29 08:38:23 -05005038 sksec->sclass = isec->sclass;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005039}
5040
Richard Hainesd4529302018-02-13 20:57:18 +00005041/* Called whenever SCTP receives an INIT chunk. This happens when an incoming
5042 * connect(2), sctp_connectx(3) or sctp_sendmsg(3) (with no association
5043 * already present).
5044 */
5045static int selinux_sctp_assoc_request(struct sctp_endpoint *ep,
5046 struct sk_buff *skb)
5047{
5048 struct sk_security_struct *sksec = ep->base.sk->sk_security;
5049 struct common_audit_data ad;
5050 struct lsm_network_audit net = {0,};
5051 u8 peerlbl_active;
5052 u32 peer_sid = SECINITSID_UNLABELED;
5053 u32 conn_sid;
5054 int err = 0;
5055
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005056 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005057 return 0;
5058
5059 peerlbl_active = selinux_peerlbl_enabled();
5060
5061 if (peerlbl_active) {
5062 /* This will return peer_sid = SECSID_NULL if there are
5063 * no peer labels, see security_net_peersid_resolve().
5064 */
5065 err = selinux_skb_peerlbl_sid(skb, ep->base.sk->sk_family,
5066 &peer_sid);
5067 if (err)
5068 return err;
5069
5070 if (peer_sid == SECSID_NULL)
5071 peer_sid = SECINITSID_UNLABELED;
5072 }
5073
5074 if (sksec->sctp_assoc_state == SCTP_ASSOC_UNSET) {
5075 sksec->sctp_assoc_state = SCTP_ASSOC_SET;
5076
5077 /* Here as first association on socket. As the peer SID
5078 * was allowed by peer recv (and the netif/node checks),
5079 * then it is approved by policy and used as the primary
5080 * peer SID for getpeercon(3).
5081 */
5082 sksec->peer_sid = peer_sid;
5083 } else if (sksec->peer_sid != peer_sid) {
5084 /* Other association peer SIDs are checked to enforce
5085 * consistency among the peer SIDs.
5086 */
5087 ad.type = LSM_AUDIT_DATA_NET;
5088 ad.u.net = &net;
5089 ad.u.net->sk = ep->base.sk;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005090 err = avc_has_perm(&selinux_state,
5091 sksec->peer_sid, peer_sid, sksec->sclass,
Richard Hainesd4529302018-02-13 20:57:18 +00005092 SCTP_SOCKET__ASSOCIATION, &ad);
5093 if (err)
5094 return err;
5095 }
5096
5097 /* Compute the MLS component for the connection and store
5098 * the information in ep. This will be used by SCTP TCP type
5099 * sockets and peeled off connections as they cause a new
5100 * socket to be generated. selinux_sctp_sk_clone() will then
5101 * plug this into the new socket.
5102 */
5103 err = selinux_conn_sid(sksec->sid, peer_sid, &conn_sid);
5104 if (err)
5105 return err;
5106
5107 ep->secid = conn_sid;
5108 ep->peer_secid = peer_sid;
5109
5110 /* Set any NetLabel labels including CIPSO/CALIPSO options. */
5111 return selinux_netlbl_sctp_assoc_request(ep, skb);
5112}
5113
5114/* Check if sctp IPv4/IPv6 addresses are valid for binding or connecting
5115 * based on their @optname.
5116 */
5117static int selinux_sctp_bind_connect(struct sock *sk, int optname,
5118 struct sockaddr *address,
5119 int addrlen)
5120{
5121 int len, err = 0, walk_size = 0;
5122 void *addr_buf;
5123 struct sockaddr *addr;
5124 struct socket *sock;
5125
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005126 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005127 return 0;
5128
5129 /* Process one or more addresses that may be IPv4 or IPv6 */
5130 sock = sk->sk_socket;
5131 addr_buf = address;
5132
5133 while (walk_size < addrlen) {
Ondrej Mosnacekc1383252018-11-13 16:16:08 +01005134 if (walk_size + sizeof(sa_family_t) > addrlen)
5135 return -EINVAL;
5136
Richard Hainesd4529302018-02-13 20:57:18 +00005137 addr = addr_buf;
5138 switch (addr->sa_family) {
Alexey Kodanev4152dc92018-05-11 20:15:13 +03005139 case AF_UNSPEC:
Richard Hainesd4529302018-02-13 20:57:18 +00005140 case AF_INET:
5141 len = sizeof(struct sockaddr_in);
5142 break;
5143 case AF_INET6:
5144 len = sizeof(struct sockaddr_in6);
5145 break;
5146 default:
Alexey Kodanev4152dc92018-05-11 20:15:13 +03005147 return -EINVAL;
Richard Hainesd4529302018-02-13 20:57:18 +00005148 }
5149
5150 err = -EINVAL;
5151 switch (optname) {
5152 /* Bind checks */
5153 case SCTP_PRIMARY_ADDR:
5154 case SCTP_SET_PEER_PRIMARY_ADDR:
5155 case SCTP_SOCKOPT_BINDX_ADD:
5156 err = selinux_socket_bind(sock, addr, len);
5157 break;
5158 /* Connect checks */
5159 case SCTP_SOCKOPT_CONNECTX:
5160 case SCTP_PARAM_SET_PRIMARY:
5161 case SCTP_PARAM_ADD_IP:
5162 case SCTP_SENDMSG_CONNECT:
5163 err = selinux_socket_connect_helper(sock, addr, len);
5164 if (err)
5165 return err;
5166
5167 /* As selinux_sctp_bind_connect() is called by the
5168 * SCTP protocol layer, the socket is already locked,
5169 * therefore selinux_netlbl_socket_connect_locked() is
5170 * is called here. The situations handled are:
5171 * sctp_connectx(3), sctp_sendmsg(3), sendmsg(2),
5172 * whenever a new IP address is added or when a new
5173 * primary address is selected.
5174 * Note that an SCTP connect(2) call happens before
5175 * the SCTP protocol layer and is handled via
5176 * selinux_socket_connect().
5177 */
5178 err = selinux_netlbl_socket_connect_locked(sk, addr);
5179 break;
5180 }
5181
5182 if (err)
5183 return err;
5184
5185 addr_buf += len;
5186 walk_size += len;
5187 }
5188
5189 return 0;
5190}
5191
5192/* Called whenever a new socket is created by accept(2) or sctp_peeloff(3). */
5193static void selinux_sctp_sk_clone(struct sctp_endpoint *ep, struct sock *sk,
5194 struct sock *newsk)
5195{
5196 struct sk_security_struct *sksec = sk->sk_security;
5197 struct sk_security_struct *newsksec = newsk->sk_security;
5198
5199 /* If policy does not support SECCLASS_SCTP_SOCKET then call
5200 * the non-sctp clone version.
5201 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005202 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005203 return selinux_sk_clone_security(sk, newsk);
5204
5205 newsksec->sid = ep->secid;
5206 newsksec->peer_sid = ep->peer_secid;
5207 newsksec->sclass = sksec->sclass;
5208 selinux_netlbl_sctp_sk_clone(sk, newsk);
5209}
5210
Adrian Bunk9a673e52006-08-15 00:03:53 -07005211static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
5212 struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005213{
5214 struct sk_security_struct *sksec = sk->sk_security;
5215 int err;
Paul Moore0b1f24e2013-12-03 11:39:13 -05005216 u16 family = req->rsk_ops->family;
Paul Moore446b8022013-12-04 16:10:51 -05005217 u32 connsid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005218 u32 peersid;
5219
Paul Mooreaa862902008-10-10 10:16:29 -04005220 err = selinux_skb_peerlbl_sid(skb, family, &peersid);
Paul Moore220deb92008-01-29 08:38:23 -05005221 if (err)
5222 return err;
Paul Moore446b8022013-12-04 16:10:51 -05005223 err = selinux_conn_sid(sksec->sid, peersid, &connsid);
5224 if (err)
5225 return err;
5226 req->secid = connsid;
5227 req->peer_secid = peersid;
Venkat Yekkiralaa51c64f2006-07-27 22:01:34 -07005228
Paul Moore389fb8002009-03-27 17:10:34 -04005229 return selinux_netlbl_inet_conn_request(req, family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005230}
5231
Adrian Bunk9a673e52006-08-15 00:03:53 -07005232static void selinux_inet_csk_clone(struct sock *newsk,
5233 const struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005234{
5235 struct sk_security_struct *newsksec = newsk->sk_security;
5236
5237 newsksec->sid = req->secid;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005238 newsksec->peer_sid = req->peer_secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005239 /* NOTE: Ideally, we should also get the isec->sid for the
5240 new socket in sync, but we don't have the isec available yet.
5241 So we will wait until sock_graft to do it, by which
5242 time it will have been created and available. */
Paul Moore99f59ed2006-08-29 17:53:48 -07005243
Paul Moore9f2ad662006-11-17 17:38:53 -05005244 /* We don't need to take any sort of lock here as we are the only
5245 * thread with access to newsksec */
Paul Moore389fb8002009-03-27 17:10:34 -04005246 selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005247}
5248
Paul Moore014ab192008-10-10 10:16:33 -04005249static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005250{
Paul Mooreaa862902008-10-10 10:16:29 -04005251 u16 family = sk->sk_family;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005252 struct sk_security_struct *sksec = sk->sk_security;
5253
Paul Mooreaa862902008-10-10 10:16:29 -04005254 /* handle mapped IPv4 packets arriving via IPv6 sockets */
5255 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
5256 family = PF_INET;
5257
5258 selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005259}
5260
Eric Paris2606fd12010-10-13 16:24:41 -04005261static int selinux_secmark_relabel_packet(u32 sid)
5262{
5263 const struct task_security_struct *__tsec;
5264 u32 tsid;
5265
5266 __tsec = current_security();
5267 tsid = __tsec->sid;
5268
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005269 return avc_has_perm(&selinux_state,
5270 tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO,
5271 NULL);
Eric Paris2606fd12010-10-13 16:24:41 -04005272}
5273
5274static void selinux_secmark_refcount_inc(void)
5275{
5276 atomic_inc(&selinux_secmark_refcount);
5277}
5278
5279static void selinux_secmark_refcount_dec(void)
5280{
5281 atomic_dec(&selinux_secmark_refcount);
5282}
5283
Adrian Bunk9a673e52006-08-15 00:03:53 -07005284static void selinux_req_classify_flow(const struct request_sock *req,
5285 struct flowi *fl)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005286{
David S. Miller1d28f422011-03-12 00:29:39 -05005287 fl->flowi_secid = req->secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005288}
5289
Paul Moore5dbbaf22013-01-14 07:12:19 +00005290static int selinux_tun_dev_alloc_security(void **security)
5291{
5292 struct tun_security_struct *tunsec;
5293
5294 tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL);
5295 if (!tunsec)
5296 return -ENOMEM;
5297 tunsec->sid = current_sid();
5298
5299 *security = tunsec;
5300 return 0;
5301}
5302
5303static void selinux_tun_dev_free_security(void *security)
5304{
5305 kfree(security);
5306}
5307
Paul Mooreed6d76e2009-08-28 18:12:49 -04005308static int selinux_tun_dev_create(void)
5309{
5310 u32 sid = current_sid();
5311
5312 /* we aren't taking into account the "sockcreate" SID since the socket
5313 * that is being created here is not a socket in the traditional sense,
5314 * instead it is a private sock, accessible only to the kernel, and
5315 * representing a wide range of network traffic spanning multiple
5316 * connections unlike traditional sockets - check the TUN driver to
5317 * get a better understanding of why this socket is special */
5318
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005319 return avc_has_perm(&selinux_state,
5320 sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005321 NULL);
5322}
5323
Paul Moore5dbbaf22013-01-14 07:12:19 +00005324static int selinux_tun_dev_attach_queue(void *security)
Paul Mooreed6d76e2009-08-28 18:12:49 -04005325{
Paul Moore5dbbaf22013-01-14 07:12:19 +00005326 struct tun_security_struct *tunsec = security;
5327
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005328 return avc_has_perm(&selinux_state,
5329 current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET,
Paul Moore5dbbaf22013-01-14 07:12:19 +00005330 TUN_SOCKET__ATTACH_QUEUE, NULL);
5331}
5332
5333static int selinux_tun_dev_attach(struct sock *sk, void *security)
5334{
5335 struct tun_security_struct *tunsec = security;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005336 struct sk_security_struct *sksec = sk->sk_security;
5337
5338 /* we don't currently perform any NetLabel based labeling here and it
5339 * isn't clear that we would want to do so anyway; while we could apply
5340 * labeling without the support of the TUN user the resulting labeled
5341 * traffic from the other end of the connection would almost certainly
5342 * cause confusion to the TUN user that had no idea network labeling
5343 * protocols were being used */
5344
Paul Moore5dbbaf22013-01-14 07:12:19 +00005345 sksec->sid = tunsec->sid;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005346 sksec->sclass = SECCLASS_TUN_SOCKET;
Paul Moore5dbbaf22013-01-14 07:12:19 +00005347
5348 return 0;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005349}
5350
Paul Moore5dbbaf22013-01-14 07:12:19 +00005351static int selinux_tun_dev_open(void *security)
Paul Mooreed6d76e2009-08-28 18:12:49 -04005352{
Paul Moore5dbbaf22013-01-14 07:12:19 +00005353 struct tun_security_struct *tunsec = security;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005354 u32 sid = current_sid();
5355 int err;
5356
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005357 err = avc_has_perm(&selinux_state,
5358 sid, tunsec->sid, SECCLASS_TUN_SOCKET,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005359 TUN_SOCKET__RELABELFROM, NULL);
5360 if (err)
5361 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005362 err = avc_has_perm(&selinux_state,
5363 sid, sid, SECCLASS_TUN_SOCKET,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005364 TUN_SOCKET__RELABELTO, NULL);
5365 if (err)
5366 return err;
Paul Moore5dbbaf22013-01-14 07:12:19 +00005367 tunsec->sid = sid;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005368
5369 return 0;
5370}
5371
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
5373{
5374 int err = 0;
5375 u32 perm;
5376 struct nlmsghdr *nlh;
Paul Moore253bfae2010-04-22 14:46:19 -04005377 struct sk_security_struct *sksec = sk->sk_security;
Eric Paris828dfe12008-04-17 13:17:49 -04005378
Hong zhi guo77954982013-03-27 06:49:35 +00005379 if (skb->len < NLMSG_HDRLEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380 err = -EINVAL;
5381 goto out;
5382 }
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -07005383 nlh = nlmsg_hdr(skb);
Eric Paris828dfe12008-04-17 13:17:49 -04005384
Paul Moore253bfae2010-04-22 14:46:19 -04005385 err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386 if (err) {
5387 if (err == -EINVAL) {
Vladis Dronov76319942015-12-24 11:09:41 -05005388 pr_warn_ratelimited("SELinux: unrecognized netlink"
5389 " message: protocol=%hu nlmsg_type=%hu sclass=%s"
5390 " pig=%d comm=%s\n",
Marek Milkoviccded3ff2015-06-04 16:22:16 -04005391 sk->sk_protocol, nlh->nlmsg_type,
Vladis Dronov76319942015-12-24 11:09:41 -05005392 secclass_map[sksec->sclass - 1].name,
5393 task_pid_nr(current), current->comm);
Paul Mooree5a5ca92018-03-01 17:38:30 -05005394 if (!enforcing_enabled(&selinux_state) ||
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005395 security_get_allow_unknown(&selinux_state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396 err = 0;
5397 }
5398
5399 /* Ignore */
5400 if (err == -ENOENT)
5401 err = 0;
5402 goto out;
5403 }
5404
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005405 err = sock_has_perm(sk, perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406out:
5407 return err;
5408}
5409
5410#ifdef CONFIG_NETFILTER
5411
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005412static unsigned int selinux_ip_forward(struct sk_buff *skb,
5413 const struct net_device *indev,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005414 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415{
Paul Mooredfaebe92008-10-10 10:16:31 -04005416 int err;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005417 char *addrp;
5418 u32 peer_sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04005419 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005420 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05005421 u8 secmark_active;
Paul Moore948bf852008-10-10 10:16:32 -04005422 u8 netlbl_active;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005423 u8 peerlbl_active;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005424
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005425 if (!selinux_policycap_netpeer())
Paul Mooreeffad8d2008-01-29 08:49:27 -05005426 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005427
Paul Mooreeffad8d2008-01-29 08:49:27 -05005428 secmark_active = selinux_secmark_enabled();
Paul Moore948bf852008-10-10 10:16:32 -04005429 netlbl_active = netlbl_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04005430 peerlbl_active = selinux_peerlbl_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05005431 if (!secmark_active && !peerlbl_active)
5432 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005433
Paul Moored8395c82008-10-10 10:16:30 -04005434 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
5435 return NF_DROP;
5436
Eric Paris50c205f2012-04-04 15:01:43 -04005437 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005438 ad.u.net = &net;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005439 ad.u.net->netif = indev->ifindex;
Eric Paris48c62af2012-04-02 13:15:44 -04005440 ad.u.net->family = family;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005441 if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
5442 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443
Paul Mooredfaebe92008-10-10 10:16:31 -04005444 if (peerlbl_active) {
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005445 err = selinux_inet_sys_rcv_skb(dev_net(indev), indev->ifindex,
5446 addrp, family, peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04005447 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04005448 selinux_netlbl_err(skb, family, err, 1);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005449 return NF_DROP;
Paul Mooredfaebe92008-10-10 10:16:31 -04005450 }
5451 }
Paul Mooreeffad8d2008-01-29 08:49:27 -05005452
5453 if (secmark_active)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005454 if (avc_has_perm(&selinux_state,
5455 peer_sid, skb->secmark,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005456 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
5457 return NF_DROP;
5458
Paul Moore948bf852008-10-10 10:16:32 -04005459 if (netlbl_active)
5460 /* we do this in the FORWARD path and not the POST_ROUTING
5461 * path because we want to make sure we apply the necessary
5462 * labeling before IPsec is applied so we can leverage AH
5463 * protection */
5464 if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
5465 return NF_DROP;
5466
Paul Mooreeffad8d2008-01-29 08:49:27 -05005467 return NF_ACCEPT;
5468}
5469
Eric W. Biederman06198b32015-09-18 14:33:06 -05005470static unsigned int selinux_ipv4_forward(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005471 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005472 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005473{
David S. Miller238e54c2015-04-03 20:32:56 -04005474 return selinux_ip_forward(skb, state->in, PF_INET);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005475}
5476
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005477#if IS_ENABLED(CONFIG_IPV6)
Eric W. Biederman06198b32015-09-18 14:33:06 -05005478static unsigned int selinux_ipv6_forward(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005479 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005480 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005481{
David S. Miller238e54c2015-04-03 20:32:56 -04005482 return selinux_ip_forward(skb, state->in, PF_INET6);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005483}
5484#endif /* IPV6 */
5485
Paul Moore948bf852008-10-10 10:16:32 -04005486static unsigned int selinux_ip_output(struct sk_buff *skb,
5487 u16 family)
5488{
Paul Moore47180062013-12-04 16:10:45 -05005489 struct sock *sk;
Paul Moore948bf852008-10-10 10:16:32 -04005490 u32 sid;
5491
5492 if (!netlbl_enabled())
5493 return NF_ACCEPT;
5494
5495 /* we do this in the LOCAL_OUT path and not the POST_ROUTING path
5496 * because we want to make sure we apply the necessary labeling
5497 * before IPsec is applied so we can leverage AH protection */
Paul Moore47180062013-12-04 16:10:45 -05005498 sk = skb->sk;
5499 if (sk) {
5500 struct sk_security_struct *sksec;
5501
Eric Dumazete446f9d2015-10-08 05:01:55 -07005502 if (sk_listener(sk))
Paul Moore47180062013-12-04 16:10:45 -05005503 /* if the socket is the listening state then this
5504 * packet is a SYN-ACK packet which means it needs to
5505 * be labeled based on the connection/request_sock and
5506 * not the parent socket. unfortunately, we can't
5507 * lookup the request_sock yet as it isn't queued on
5508 * the parent socket until after the SYN-ACK is sent.
5509 * the "solution" is to simply pass the packet as-is
5510 * as any IP option based labeling should be copied
5511 * from the initial connection request (in the IP
5512 * layer). it is far from ideal, but until we get a
5513 * security label in the packet itself this is the
5514 * best we can do. */
5515 return NF_ACCEPT;
5516
5517 /* standard practice, label using the parent socket */
5518 sksec = sk->sk_security;
Paul Moore948bf852008-10-10 10:16:32 -04005519 sid = sksec->sid;
5520 } else
5521 sid = SECINITSID_KERNEL;
5522 if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
5523 return NF_DROP;
5524
5525 return NF_ACCEPT;
5526}
5527
Eric W. Biederman06198b32015-09-18 14:33:06 -05005528static unsigned int selinux_ipv4_output(void *priv,
Paul Moore948bf852008-10-10 10:16:32 -04005529 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005530 const struct nf_hook_state *state)
Paul Moore948bf852008-10-10 10:16:32 -04005531{
5532 return selinux_ip_output(skb, PF_INET);
5533}
5534
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005535#if IS_ENABLED(CONFIG_IPV6)
Huw Davies2917f572016-06-27 15:06:15 -04005536static unsigned int selinux_ipv6_output(void *priv,
5537 struct sk_buff *skb,
5538 const struct nf_hook_state *state)
5539{
5540 return selinux_ip_output(skb, PF_INET6);
5541}
5542#endif /* IPV6 */
5543
Paul Mooreeffad8d2008-01-29 08:49:27 -05005544static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
5545 int ifindex,
Paul Moored8395c82008-10-10 10:16:30 -04005546 u16 family)
James Morris4e5ab4c2006-06-09 00:33:33 -07005547{
Eric Dumazet54abc682015-11-08 10:54:07 -08005548 struct sock *sk = skb_to_full_sk(skb);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005549 struct sk_security_struct *sksec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005550 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005551 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04005552 char *addrp;
5553 u8 proto;
James Morris4e5ab4c2006-06-09 00:33:33 -07005554
Paul Mooreeffad8d2008-01-29 08:49:27 -05005555 if (sk == NULL)
5556 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005557 sksec = sk->sk_security;
James Morris4e5ab4c2006-06-09 00:33:33 -07005558
Eric Paris50c205f2012-04-04 15:01:43 -04005559 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005560 ad.u.net = &net;
5561 ad.u.net->netif = ifindex;
5562 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04005563 if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
5564 return NF_DROP;
5565
Paul Moore58bfbb52009-03-27 17:10:41 -04005566 if (selinux_secmark_enabled())
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005567 if (avc_has_perm(&selinux_state,
5568 sksec->sid, skb->secmark,
Paul Moored8395c82008-10-10 10:16:30 -04005569 SECCLASS_PACKET, PACKET__SEND, &ad))
Eric Paris2fe66ec2010-11-23 06:28:08 +00005570 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07005571
Steffen Klassertb9679a72011-02-23 12:55:21 +01005572 if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
5573 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07005574
Paul Mooreeffad8d2008-01-29 08:49:27 -05005575 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576}
5577
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005578static unsigned int selinux_ip_postroute(struct sk_buff *skb,
5579 const struct net_device *outdev,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005580 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581{
Paul Mooreeffad8d2008-01-29 08:49:27 -05005582 u32 secmark_perm;
5583 u32 peer_sid;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005584 int ifindex = outdev->ifindex;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005585 struct sock *sk;
Thomas Liu2bf49692009-07-14 12:14:09 -04005586 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005587 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05005588 char *addrp;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005589 u8 secmark_active;
5590 u8 peerlbl_active;
5591
Paul Mooreeffad8d2008-01-29 08:49:27 -05005592 /* If any sort of compatibility mode is enabled then handoff processing
5593 * to the selinux_ip_postroute_compat() function to deal with the
5594 * special handling. We do this in an attempt to keep this function
5595 * as fast and as clean as possible. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005596 if (!selinux_policycap_netpeer())
Paul Moored8395c82008-10-10 10:16:30 -04005597 return selinux_ip_postroute_compat(skb, ifindex, family);
Paul Moorec0828e52013-12-10 14:58:01 -05005598
Paul Mooreeffad8d2008-01-29 08:49:27 -05005599 secmark_active = selinux_secmark_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04005600 peerlbl_active = selinux_peerlbl_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05005601 if (!secmark_active && !peerlbl_active)
5602 return NF_ACCEPT;
5603
Eric Dumazet54abc682015-11-08 10:54:07 -08005604 sk = skb_to_full_sk(skb);
Paul Moorec0828e52013-12-10 14:58:01 -05005605
Paul Mooreeffad8d2008-01-29 08:49:27 -05005606#ifdef CONFIG_XFRM
5607 /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
5608 * packet transformation so allow the packet to pass without any checks
5609 * since we'll have another chance to perform access control checks
5610 * when the packet is on it's final way out.
5611 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
Paul Moorec0828e52013-12-10 14:58:01 -05005612 * is NULL, in this case go ahead and apply access control.
5613 * NOTE: if this is a local socket (skb->sk != NULL) that is in the
5614 * TCP listening state we cannot wait until the XFRM processing
5615 * is done as we will miss out on the SA label if we do;
5616 * unfortunately, this means more work, but it is only once per
5617 * connection. */
5618 if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL &&
Eric Dumazete446f9d2015-10-08 05:01:55 -07005619 !(sk && sk_listener(sk)))
Paul Mooreeffad8d2008-01-29 08:49:27 -05005620 return NF_ACCEPT;
5621#endif
Paul Mooreeffad8d2008-01-29 08:49:27 -05005622
Paul Moored8395c82008-10-10 10:16:30 -04005623 if (sk == NULL) {
Paul Moore446b8022013-12-04 16:10:51 -05005624 /* Without an associated socket the packet is either coming
5625 * from the kernel or it is being forwarded; check the packet
5626 * to determine which and if the packet is being forwarded
5627 * query the packet directly to determine the security label. */
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005628 if (skb->skb_iif) {
5629 secmark_perm = PACKET__FORWARD_OUT;
Paul Moored8395c82008-10-10 10:16:30 -04005630 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005631 return NF_DROP;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005632 } else {
5633 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04005634 peer_sid = SECINITSID_KERNEL;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005635 }
Eric Dumazete446f9d2015-10-08 05:01:55 -07005636 } else if (sk_listener(sk)) {
Paul Moore446b8022013-12-04 16:10:51 -05005637 /* Locally generated packet but the associated socket is in the
5638 * listening state which means this is a SYN-ACK packet. In
5639 * this particular case the correct security label is assigned
5640 * to the connection/request_sock but unfortunately we can't
5641 * query the request_sock as it isn't queued on the parent
5642 * socket until after the SYN-ACK packet is sent; the only
5643 * viable choice is to regenerate the label like we do in
5644 * selinux_inet_conn_request(). See also selinux_ip_output()
5645 * for similar problems. */
5646 u32 skb_sid;
Eric Dumazete446f9d2015-10-08 05:01:55 -07005647 struct sk_security_struct *sksec;
5648
Eric Dumazete446f9d2015-10-08 05:01:55 -07005649 sksec = sk->sk_security;
Paul Moore446b8022013-12-04 16:10:51 -05005650 if (selinux_skb_peerlbl_sid(skb, family, &skb_sid))
5651 return NF_DROP;
Paul Moorec0828e52013-12-10 14:58:01 -05005652 /* At this point, if the returned skb peerlbl is SECSID_NULL
5653 * and the packet has been through at least one XFRM
5654 * transformation then we must be dealing with the "final"
5655 * form of labeled IPsec packet; since we've already applied
5656 * all of our access controls on this packet we can safely
5657 * pass the packet. */
5658 if (skb_sid == SECSID_NULL) {
5659 switch (family) {
5660 case PF_INET:
5661 if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
5662 return NF_ACCEPT;
5663 break;
5664 case PF_INET6:
5665 if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED)
5666 return NF_ACCEPT;
Paul Moorea7a91a12014-09-03 10:51:59 -04005667 break;
Paul Moorec0828e52013-12-10 14:58:01 -05005668 default:
5669 return NF_DROP_ERR(-ECONNREFUSED);
5670 }
5671 }
Paul Moore446b8022013-12-04 16:10:51 -05005672 if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid))
5673 return NF_DROP;
5674 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04005675 } else {
Paul Moore446b8022013-12-04 16:10:51 -05005676 /* Locally generated packet, fetch the security label from the
5677 * associated socket. */
Paul Mooreeffad8d2008-01-29 08:49:27 -05005678 struct sk_security_struct *sksec = sk->sk_security;
5679 peer_sid = sksec->sid;
5680 secmark_perm = PACKET__SEND;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005681 }
5682
Eric Paris50c205f2012-04-04 15:01:43 -04005683 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005684 ad.u.net = &net;
5685 ad.u.net->netif = ifindex;
5686 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04005687 if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
Eric Paris04f6d702010-11-23 06:28:02 +00005688 return NF_DROP;
Paul Moored8395c82008-10-10 10:16:30 -04005689
Paul Mooreeffad8d2008-01-29 08:49:27 -05005690 if (secmark_active)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005691 if (avc_has_perm(&selinux_state,
5692 peer_sid, skb->secmark,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005693 SECCLASS_PACKET, secmark_perm, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005694 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005695
5696 if (peerlbl_active) {
5697 u32 if_sid;
5698 u32 node_sid;
5699
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005700 if (sel_netif_sid(dev_net(outdev), ifindex, &if_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005701 return NF_DROP;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005702 if (avc_has_perm(&selinux_state,
5703 peer_sid, if_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005704 SECCLASS_NETIF, NETIF__EGRESS, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005705 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005706
5707 if (sel_netnode_sid(addrp, family, &node_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005708 return NF_DROP;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005709 if (avc_has_perm(&selinux_state,
5710 peer_sid, node_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005711 SECCLASS_NODE, NODE__SENDTO, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005712 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005713 }
5714
5715 return NF_ACCEPT;
5716}
5717
Eric W. Biederman06198b32015-09-18 14:33:06 -05005718static unsigned int selinux_ipv4_postroute(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005719 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005720 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005721{
David S. Miller238e54c2015-04-03 20:32:56 -04005722 return selinux_ip_postroute(skb, state->out, PF_INET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723}
5724
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005725#if IS_ENABLED(CONFIG_IPV6)
Eric W. Biederman06198b32015-09-18 14:33:06 -05005726static unsigned int selinux_ipv6_postroute(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005727 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005728 const struct nf_hook_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729{
David S. Miller238e54c2015-04-03 20:32:56 -04005730 return selinux_ip_postroute(skb, state->out, PF_INET6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005732#endif /* IPV6 */
5733
5734#endif /* CONFIG_NETFILTER */
5735
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
5737{
Stephen Smalley941fc5b2009-10-01 14:48:23 -04005738 return selinux_nlmsg_perm(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739}
5740
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005741static int ipc_alloc_security(struct kern_ipc_perm *perm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742 u16 sclass)
5743{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744 struct ipc_security_struct *isec;
5745
James Morris89d155e2005-10-30 14:59:21 -08005746 isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747 if (!isec)
5748 return -ENOMEM;
5749
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750 isec->sclass = sclass;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005751 isec->sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752 perm->security = isec;
5753
5754 return 0;
5755}
5756
5757static void ipc_free_security(struct kern_ipc_perm *perm)
5758{
5759 struct ipc_security_struct *isec = perm->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005760 perm->security = NULL;
5761 kfree(isec);
5762}
5763
5764static int msg_msg_alloc_security(struct msg_msg *msg)
5765{
5766 struct msg_security_struct *msec;
5767
James Morris89d155e2005-10-30 14:59:21 -08005768 msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769 if (!msec)
5770 return -ENOMEM;
5771
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772 msec->sid = SECINITSID_UNLABELED;
5773 msg->security = msec;
5774
5775 return 0;
5776}
5777
5778static void msg_msg_free_security(struct msg_msg *msg)
5779{
5780 struct msg_security_struct *msec = msg->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005781
5782 msg->security = NULL;
5783 kfree(msec);
5784}
5785
5786static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
Stephen Smalley6af963f2005-05-01 08:58:39 -07005787 u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005789 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005790 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005791 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793 isec = ipc_perms->security;
5794
Eric Paris50c205f2012-04-04 15:01:43 -04005795 ad.type = LSM_AUDIT_DATA_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796 ad.u.ipc_id = ipc_perms->key;
5797
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005798 return avc_has_perm(&selinux_state,
5799 sid, isec->sid, isec->sclass, perms, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800}
5801
5802static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
5803{
5804 return msg_msg_alloc_security(msg);
5805}
5806
5807static void selinux_msg_msg_free_security(struct msg_msg *msg)
5808{
5809 msg_msg_free_security(msg);
5810}
5811
5812/* message queue security operations */
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005813static int selinux_msg_queue_alloc_security(struct kern_ipc_perm *msq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005815 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005816 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005817 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005818 int rc;
5819
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005820 rc = ipc_alloc_security(msq, SECCLASS_MSGQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821 if (rc)
5822 return rc;
5823
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005824 isec = msq->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825
Eric Paris50c205f2012-04-04 15:01:43 -04005826 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005827 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005829 rc = avc_has_perm(&selinux_state,
5830 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831 MSGQ__CREATE, &ad);
5832 if (rc) {
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005833 ipc_free_security(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005834 return rc;
5835 }
5836 return 0;
5837}
5838
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005839static void selinux_msg_queue_free_security(struct kern_ipc_perm *msq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840{
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005841 ipc_free_security(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842}
5843
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005844static int selinux_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005845{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005847 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005848 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005850 isec = msq->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851
Eric Paris50c205f2012-04-04 15:01:43 -04005852 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005853 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005855 return avc_has_perm(&selinux_state,
5856 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857 MSGQ__ASSOCIATE, &ad);
5858}
5859
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005860static int selinux_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005861{
5862 int err;
5863 int perms;
5864
Eric Paris828dfe12008-04-17 13:17:49 -04005865 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866 case IPC_INFO:
5867 case MSG_INFO:
5868 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005869 return avc_has_perm(&selinux_state,
5870 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005871 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005872 case IPC_STAT:
5873 case MSG_STAT:
Davidlohr Bueso23c8cec2018-04-10 16:35:30 -07005874 case MSG_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005875 perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
5876 break;
5877 case IPC_SET:
5878 perms = MSGQ__SETATTR;
5879 break;
5880 case IPC_RMID:
5881 perms = MSGQ__DESTROY;
5882 break;
5883 default:
5884 return 0;
5885 }
5886
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005887 err = ipc_has_perm(msq, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888 return err;
5889}
5890
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005891static int selinux_msg_queue_msgsnd(struct kern_ipc_perm *msq, struct msg_msg *msg, int msqflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005893 struct ipc_security_struct *isec;
5894 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005895 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005896 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005897 int rc;
5898
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005899 isec = msq->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900 msec = msg->security;
5901
5902 /*
5903 * First time through, need to assign label to the message
5904 */
5905 if (msec->sid == SECINITSID_UNLABELED) {
5906 /*
5907 * Compute new sid based on current process and
5908 * message queue this message will be stored in
5909 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005910 rc = security_transition_sid(&selinux_state, sid, isec->sid,
5911 SECCLASS_MSG, NULL, &msec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912 if (rc)
5913 return rc;
5914 }
5915
Eric Paris50c205f2012-04-04 15:01:43 -04005916 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005917 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918
5919 /* Can this process write to the queue? */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005920 rc = avc_has_perm(&selinux_state,
5921 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922 MSGQ__WRITE, &ad);
5923 if (!rc)
5924 /* Can this process send the message */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005925 rc = avc_has_perm(&selinux_state,
5926 sid, msec->sid, SECCLASS_MSG,
David Howells275bb412008-11-14 10:39:19 +11005927 MSG__SEND, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928 if (!rc)
5929 /* Can the message be put in the queue? */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005930 rc = avc_has_perm(&selinux_state,
5931 msec->sid, isec->sid, SECCLASS_MSGQ,
David Howells275bb412008-11-14 10:39:19 +11005932 MSGQ__ENQUEUE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933
5934 return rc;
5935}
5936
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005937static int selinux_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938 struct task_struct *target,
5939 long type, int mode)
5940{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941 struct ipc_security_struct *isec;
5942 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005943 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005944 u32 sid = task_sid(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005945 int rc;
5946
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005947 isec = msq->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948 msec = msg->security;
5949
Eric Paris50c205f2012-04-04 15:01:43 -04005950 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005951 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005953 rc = avc_has_perm(&selinux_state,
5954 sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955 SECCLASS_MSGQ, MSGQ__READ, &ad);
5956 if (!rc)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005957 rc = avc_has_perm(&selinux_state,
5958 sid, msec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005959 SECCLASS_MSG, MSG__RECEIVE, &ad);
5960 return rc;
5961}
5962
5963/* Shared Memory security operations */
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005964static int selinux_shm_alloc_security(struct kern_ipc_perm *shp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005965{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005967 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005968 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005969 int rc;
5970
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005971 rc = ipc_alloc_security(shp, SECCLASS_SHM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005972 if (rc)
5973 return rc;
5974
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005975 isec = shp->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005976
Eric Paris50c205f2012-04-04 15:01:43 -04005977 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005978 ad.u.ipc_id = shp->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005980 rc = avc_has_perm(&selinux_state,
5981 sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982 SHM__CREATE, &ad);
5983 if (rc) {
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005984 ipc_free_security(shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005985 return rc;
5986 }
5987 return 0;
5988}
5989
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005990static void selinux_shm_free_security(struct kern_ipc_perm *shp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991{
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005992 ipc_free_security(shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993}
5994
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005995static int selinux_shm_associate(struct kern_ipc_perm *shp, int shmflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005998 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005999 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006000
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006001 isec = shp->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002
Eric Paris50c205f2012-04-04 15:01:43 -04006003 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006004 ad.u.ipc_id = shp->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006006 return avc_has_perm(&selinux_state,
6007 sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006008 SHM__ASSOCIATE, &ad);
6009}
6010
6011/* Note, at this point, shp is locked down */
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006012static int selinux_shm_shmctl(struct kern_ipc_perm *shp, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013{
6014 int perms;
6015 int err;
6016
Eric Paris828dfe12008-04-17 13:17:49 -04006017 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018 case IPC_INFO:
6019 case SHM_INFO:
6020 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006021 return avc_has_perm(&selinux_state,
6022 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006023 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006024 case IPC_STAT:
6025 case SHM_STAT:
Davidlohr Buesoc21a6972018-04-10 16:35:23 -07006026 case SHM_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027 perms = SHM__GETATTR | SHM__ASSOCIATE;
6028 break;
6029 case IPC_SET:
6030 perms = SHM__SETATTR;
6031 break;
6032 case SHM_LOCK:
6033 case SHM_UNLOCK:
6034 perms = SHM__LOCK;
6035 break;
6036 case IPC_RMID:
6037 perms = SHM__DESTROY;
6038 break;
6039 default:
6040 return 0;
6041 }
6042
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006043 err = ipc_has_perm(shp, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044 return err;
6045}
6046
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006047static int selinux_shm_shmat(struct kern_ipc_perm *shp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048 char __user *shmaddr, int shmflg)
6049{
6050 u32 perms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051
6052 if (shmflg & SHM_RDONLY)
6053 perms = SHM__READ;
6054 else
6055 perms = SHM__READ | SHM__WRITE;
6056
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006057 return ipc_has_perm(shp, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058}
6059
6060/* Semaphore security operations */
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006061static int selinux_sem_alloc_security(struct kern_ipc_perm *sma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006064 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006065 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006066 int rc;
6067
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006068 rc = ipc_alloc_security(sma, SECCLASS_SEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069 if (rc)
6070 return rc;
6071
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006072 isec = sma->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006073
Eric Paris50c205f2012-04-04 15:01:43 -04006074 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006075 ad.u.ipc_id = sma->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006077 rc = avc_has_perm(&selinux_state,
6078 sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079 SEM__CREATE, &ad);
6080 if (rc) {
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006081 ipc_free_security(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082 return rc;
6083 }
6084 return 0;
6085}
6086
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006087static void selinux_sem_free_security(struct kern_ipc_perm *sma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088{
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006089 ipc_free_security(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006090}
6091
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006092static int selinux_sem_associate(struct kern_ipc_perm *sma, int semflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006094 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006095 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006096 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006098 isec = sma->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099
Eric Paris50c205f2012-04-04 15:01:43 -04006100 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006101 ad.u.ipc_id = sma->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006102
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006103 return avc_has_perm(&selinux_state,
6104 sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006105 SEM__ASSOCIATE, &ad);
6106}
6107
6108/* Note, at this point, sma is locked down */
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006109static int selinux_sem_semctl(struct kern_ipc_perm *sma, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006110{
6111 int err;
6112 u32 perms;
6113
Eric Paris828dfe12008-04-17 13:17:49 -04006114 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006115 case IPC_INFO:
6116 case SEM_INFO:
6117 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006118 return avc_has_perm(&selinux_state,
6119 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006120 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006121 case GETPID:
6122 case GETNCNT:
6123 case GETZCNT:
6124 perms = SEM__GETATTR;
6125 break;
6126 case GETVAL:
6127 case GETALL:
6128 perms = SEM__READ;
6129 break;
6130 case SETVAL:
6131 case SETALL:
6132 perms = SEM__WRITE;
6133 break;
6134 case IPC_RMID:
6135 perms = SEM__DESTROY;
6136 break;
6137 case IPC_SET:
6138 perms = SEM__SETATTR;
6139 break;
6140 case IPC_STAT:
6141 case SEM_STAT:
Davidlohr Buesoa280d6d2018-04-10 16:35:26 -07006142 case SEM_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006143 perms = SEM__GETATTR | SEM__ASSOCIATE;
6144 break;
6145 default:
6146 return 0;
6147 }
6148
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006149 err = ipc_has_perm(sma, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006150 return err;
6151}
6152
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006153static int selinux_sem_semop(struct kern_ipc_perm *sma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006154 struct sembuf *sops, unsigned nsops, int alter)
6155{
6156 u32 perms;
6157
6158 if (alter)
6159 perms = SEM__READ | SEM__WRITE;
6160 else
6161 perms = SEM__READ;
6162
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006163 return ipc_has_perm(sma, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006164}
6165
6166static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
6167{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006168 u32 av = 0;
6169
Linus Torvalds1da177e2005-04-16 15:20:36 -07006170 av = 0;
6171 if (flag & S_IRUGO)
6172 av |= IPC__UNIX_READ;
6173 if (flag & S_IWUGO)
6174 av |= IPC__UNIX_WRITE;
6175
6176 if (av == 0)
6177 return 0;
6178
Stephen Smalley6af963f2005-05-01 08:58:39 -07006179 return ipc_has_perm(ipcp, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006180}
6181
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02006182static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
6183{
6184 struct ipc_security_struct *isec = ipcp->security;
6185 *secid = isec->sid;
6186}
6187
Eric Paris828dfe12008-04-17 13:17:49 -04006188static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006189{
6190 if (inode)
6191 inode_doinit_with_dentry(inode, dentry);
6192}
6193
6194static int selinux_getprocattr(struct task_struct *p,
Al Viro04ff9702007-03-12 16:17:58 +00006195 char *name, char **value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006196{
David Howells275bb412008-11-14 10:39:19 +11006197 const struct task_security_struct *__tsec;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00006198 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006199 int error;
Al Viro04ff9702007-03-12 16:17:58 +00006200 unsigned len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006201
David Howells275bb412008-11-14 10:39:19 +11006202 rcu_read_lock();
6203 __tsec = __task_cred(p)->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006205 if (current != p) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006206 error = avc_has_perm(&selinux_state,
6207 current_sid(), __tsec->sid,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006208 SECCLASS_PROCESS, PROCESS__GETATTR, NULL);
6209 if (error)
6210 goto bad;
6211 }
6212
Linus Torvalds1da177e2005-04-16 15:20:36 -07006213 if (!strcmp(name, "current"))
David Howells275bb412008-11-14 10:39:19 +11006214 sid = __tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006215 else if (!strcmp(name, "prev"))
David Howells275bb412008-11-14 10:39:19 +11006216 sid = __tsec->osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217 else if (!strcmp(name, "exec"))
David Howells275bb412008-11-14 10:39:19 +11006218 sid = __tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219 else if (!strcmp(name, "fscreate"))
David Howells275bb412008-11-14 10:39:19 +11006220 sid = __tsec->create_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006221 else if (!strcmp(name, "keycreate"))
David Howells275bb412008-11-14 10:39:19 +11006222 sid = __tsec->keycreate_sid;
Eric Paris42c3e032006-06-26 00:26:03 -07006223 else if (!strcmp(name, "sockcreate"))
David Howells275bb412008-11-14 10:39:19 +11006224 sid = __tsec->sockcreate_sid;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006225 else {
6226 error = -EINVAL;
6227 goto bad;
6228 }
David Howells275bb412008-11-14 10:39:19 +11006229 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006230
6231 if (!sid)
6232 return 0;
6233
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006234 error = security_sid_to_context(&selinux_state, sid, value, &len);
Al Viro04ff9702007-03-12 16:17:58 +00006235 if (error)
6236 return error;
6237 return len;
David Howells275bb412008-11-14 10:39:19 +11006238
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006239bad:
David Howells275bb412008-11-14 10:39:19 +11006240 rcu_read_unlock();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006241 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006242}
6243
Stephen Smalleyb21507e2017-01-09 10:07:31 -05006244static int selinux_setprocattr(const char *name, void *value, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006245{
6246 struct task_security_struct *tsec;
David Howellsd84f4f92008-11-14 10:39:23 +11006247 struct cred *new;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006248 u32 mysid = current_sid(), sid = 0, ptsid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249 int error;
6250 char *str = value;
6251
Linus Torvalds1da177e2005-04-16 15:20:36 -07006252 /*
6253 * Basic control over ability to set these attributes at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006254 */
6255 if (!strcmp(name, "exec"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006256 error = avc_has_perm(&selinux_state,
6257 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006258 PROCESS__SETEXEC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006259 else if (!strcmp(name, "fscreate"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006260 error = avc_has_perm(&selinux_state,
6261 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006262 PROCESS__SETFSCREATE, NULL);
Michael LeMay4eb582c2006-06-26 00:24:57 -07006263 else if (!strcmp(name, "keycreate"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006264 error = avc_has_perm(&selinux_state,
6265 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006266 PROCESS__SETKEYCREATE, NULL);
Eric Paris42c3e032006-06-26 00:26:03 -07006267 else if (!strcmp(name, "sockcreate"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006268 error = avc_has_perm(&selinux_state,
6269 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006270 PROCESS__SETSOCKCREATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271 else if (!strcmp(name, "current"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006272 error = avc_has_perm(&selinux_state,
6273 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006274 PROCESS__SETCURRENT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006275 else
6276 error = -EINVAL;
6277 if (error)
6278 return error;
6279
6280 /* Obtain a SID for the context, if one was specified. */
Stephen Smalleya050a572017-01-31 11:54:04 -05006281 if (size && str[0] && str[0] != '\n') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006282 if (str[size-1] == '\n') {
6283 str[size-1] = 0;
6284 size--;
6285 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006286 error = security_context_to_sid(&selinux_state, value, size,
6287 &sid, GFP_KERNEL);
Stephen Smalley12b29f32008-05-07 13:03:20 -04006288 if (error == -EINVAL && !strcmp(name, "fscreate")) {
Stephen Smalleydb590002017-04-20 11:31:30 -04006289 if (!has_cap_mac_admin(true)) {
Eric Parisd6ea83e2012-04-04 13:45:49 -04006290 struct audit_buffer *ab;
6291 size_t audit_size;
6292
6293 /* We strip a nul only if it is at the end, otherwise the
6294 * context contains a nul and we should audit that */
6295 if (str[size - 1] == '\0')
6296 audit_size = size - 1;
6297 else
6298 audit_size = size;
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04006299 ab = audit_log_start(audit_context(),
6300 GFP_ATOMIC,
6301 AUDIT_SELINUX_ERR);
Eric Parisd6ea83e2012-04-04 13:45:49 -04006302 audit_log_format(ab, "op=fscreate invalid_context=");
6303 audit_log_n_untrustedstring(ab, value, audit_size);
6304 audit_log_end(ab);
6305
Stephen Smalley12b29f32008-05-07 13:03:20 -04006306 return error;
Eric Parisd6ea83e2012-04-04 13:45:49 -04006307 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006308 error = security_context_to_sid_force(
6309 &selinux_state,
6310 value, size, &sid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04006311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006312 if (error)
6313 return error;
6314 }
6315
David Howellsd84f4f92008-11-14 10:39:23 +11006316 new = prepare_creds();
6317 if (!new)
6318 return -ENOMEM;
6319
Linus Torvalds1da177e2005-04-16 15:20:36 -07006320 /* Permission checking based on the specified context is
6321 performed during the actual operation (execve,
6322 open/mkdir/...), when we know the full context of the
David Howellsd84f4f92008-11-14 10:39:23 +11006323 operation. See selinux_bprm_set_creds for the execve
Linus Torvalds1da177e2005-04-16 15:20:36 -07006324 checks and may_create for the file creation checks. The
6325 operation will then fail if the context is not permitted. */
David Howellsd84f4f92008-11-14 10:39:23 +11006326 tsec = new->security;
6327 if (!strcmp(name, "exec")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006328 tsec->exec_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006329 } else if (!strcmp(name, "fscreate")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330 tsec->create_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006331 } else if (!strcmp(name, "keycreate")) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006332 error = avc_has_perm(&selinux_state,
6333 mysid, sid, SECCLASS_KEY, KEY__CREATE,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006334 NULL);
Michael LeMay4eb582c2006-06-26 00:24:57 -07006335 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11006336 goto abort_change;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006337 tsec->keycreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006338 } else if (!strcmp(name, "sockcreate")) {
Eric Paris42c3e032006-06-26 00:26:03 -07006339 tsec->sockcreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006340 } else if (!strcmp(name, "current")) {
6341 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006342 if (sid == 0)
David Howellsd84f4f92008-11-14 10:39:23 +11006343 goto abort_change;
KaiGai Koheid9250de2008-08-28 16:35:57 +09006344
David Howellsd84f4f92008-11-14 10:39:23 +11006345 /* Only allow single threaded processes to change context */
6346 error = -EPERM;
Oleg Nesterov5bb459b2009-07-10 03:48:23 +02006347 if (!current_is_single_threaded()) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006348 error = security_bounded_transition(&selinux_state,
6349 tsec->sid, sid);
David Howellsd84f4f92008-11-14 10:39:23 +11006350 if (error)
6351 goto abort_change;
Eric Paris828dfe12008-04-17 13:17:49 -04006352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006353
6354 /* Check permissions for the transition. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006355 error = avc_has_perm(&selinux_state,
6356 tsec->sid, sid, SECCLASS_PROCESS,
Eric Paris828dfe12008-04-17 13:17:49 -04006357 PROCESS__DYNTRANSITION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006358 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11006359 goto abort_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006360
6361 /* Check for ptracing, and update the task SID if ok.
6362 Otherwise, leave SID unchanged and fail. */
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006363 ptsid = ptrace_parent_sid();
Paul Moore0c6181c2016-03-30 21:41:21 -04006364 if (ptsid != 0) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006365 error = avc_has_perm(&selinux_state,
6366 ptsid, sid, SECCLASS_PROCESS,
David Howellsd84f4f92008-11-14 10:39:23 +11006367 PROCESS__PTRACE, NULL);
6368 if (error)
6369 goto abort_change;
6370 }
6371
6372 tsec->sid = sid;
6373 } else {
6374 error = -EINVAL;
6375 goto abort_change;
6376 }
6377
6378 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006379 return size;
David Howellsd84f4f92008-11-14 10:39:23 +11006380
6381abort_change:
6382 abort_creds(new);
6383 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006384}
6385
David Quigley746df9b2013-05-22 12:50:35 -04006386static int selinux_ismaclabel(const char *name)
6387{
6388 return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0);
6389}
6390
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006391static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
6392{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006393 return security_sid_to_context(&selinux_state, secid,
6394 secdata, seclen);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006395}
6396
David Howells7bf570d2008-04-29 20:52:51 +01006397static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
David Howells63cb3442008-01-15 23:47:35 +00006398{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006399 return security_context_to_sid(&selinux_state, secdata, seclen,
6400 secid, GFP_KERNEL);
David Howells63cb3442008-01-15 23:47:35 +00006401}
6402
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006403static void selinux_release_secctx(char *secdata, u32 seclen)
6404{
Paul Moore088999e2007-08-01 11:12:58 -04006405 kfree(secdata);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006406}
6407
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006408static void selinux_inode_invalidate_secctx(struct inode *inode)
6409{
6410 struct inode_security_struct *isec = inode->i_security;
6411
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01006412 spin_lock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006413 isec->initialized = LABEL_INVALID;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01006414 spin_unlock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006415}
6416
David P. Quigley1ee65e32009-09-03 14:25:57 -04006417/*
6418 * called with inode->i_mutex locked
6419 */
6420static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
6421{
Ondrej Mosnacek53e0c2a2018-12-21 21:18:53 +01006422 int rc = selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX,
6423 ctx, ctxlen, 0);
6424 /* Do not return error when suppressing label (SBLABEL_MNT not set). */
6425 return rc == -EOPNOTSUPP ? 0 : rc;
David P. Quigley1ee65e32009-09-03 14:25:57 -04006426}
6427
6428/*
6429 * called with inode->i_mutex locked
6430 */
6431static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
6432{
6433 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0);
6434}
6435
6436static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
6437{
6438 int len = 0;
6439 len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX,
6440 ctx, true);
6441 if (len < 0)
6442 return len;
6443 *ctxlen = len;
6444 return 0;
6445}
Michael LeMayd7200242006-06-22 14:47:17 -07006446#ifdef CONFIG_KEYS
6447
David Howellsd84f4f92008-11-14 10:39:23 +11006448static int selinux_key_alloc(struct key *k, const struct cred *cred,
David Howells7e047ef2006-06-26 00:24:50 -07006449 unsigned long flags)
Michael LeMayd7200242006-06-22 14:47:17 -07006450{
David Howellsd84f4f92008-11-14 10:39:23 +11006451 const struct task_security_struct *tsec;
Michael LeMayd7200242006-06-22 14:47:17 -07006452 struct key_security_struct *ksec;
6453
6454 ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
6455 if (!ksec)
6456 return -ENOMEM;
6457
David Howellsd84f4f92008-11-14 10:39:23 +11006458 tsec = cred->security;
6459 if (tsec->keycreate_sid)
6460 ksec->sid = tsec->keycreate_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006461 else
David Howellsd84f4f92008-11-14 10:39:23 +11006462 ksec->sid = tsec->sid;
Michael LeMayd7200242006-06-22 14:47:17 -07006463
David Howells275bb412008-11-14 10:39:19 +11006464 k->security = ksec;
Michael LeMayd7200242006-06-22 14:47:17 -07006465 return 0;
6466}
6467
6468static void selinux_key_free(struct key *k)
6469{
6470 struct key_security_struct *ksec = k->security;
6471
6472 k->security = NULL;
6473 kfree(ksec);
6474}
6475
6476static int selinux_key_permission(key_ref_t key_ref,
David Howellsd84f4f92008-11-14 10:39:23 +11006477 const struct cred *cred,
David Howellsf5895942014-03-14 17:44:49 +00006478 unsigned perm)
Michael LeMayd7200242006-06-22 14:47:17 -07006479{
6480 struct key *key;
Michael LeMayd7200242006-06-22 14:47:17 -07006481 struct key_security_struct *ksec;
David Howells275bb412008-11-14 10:39:19 +11006482 u32 sid;
Michael LeMayd7200242006-06-22 14:47:17 -07006483
6484 /* if no specific permissions are requested, we skip the
6485 permission check. No serious, additional covert channels
6486 appear to be created. */
6487 if (perm == 0)
6488 return 0;
6489
David Howellsd84f4f92008-11-14 10:39:23 +11006490 sid = cred_sid(cred);
David Howells275bb412008-11-14 10:39:19 +11006491
6492 key = key_ref_to_ptr(key_ref);
6493 ksec = key->security;
6494
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006495 return avc_has_perm(&selinux_state,
6496 sid, ksec->sid, SECCLASS_KEY, perm, NULL);
Michael LeMayd7200242006-06-22 14:47:17 -07006497}
6498
David Howells70a5bb72008-04-29 01:01:26 -07006499static int selinux_key_getsecurity(struct key *key, char **_buffer)
6500{
6501 struct key_security_struct *ksec = key->security;
6502 char *context = NULL;
6503 unsigned len;
6504 int rc;
6505
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006506 rc = security_sid_to_context(&selinux_state, ksec->sid,
6507 &context, &len);
David Howells70a5bb72008-04-29 01:01:26 -07006508 if (!rc)
6509 rc = len;
6510 *_buffer = context;
6511 return rc;
6512}
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006513#endif
David Howells70a5bb72008-04-29 01:01:26 -07006514
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006515#ifdef CONFIG_SECURITY_INFINIBAND
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006516static int selinux_ib_pkey_access(void *ib_sec, u64 subnet_prefix, u16 pkey_val)
6517{
6518 struct common_audit_data ad;
6519 int err;
6520 u32 sid = 0;
6521 struct ib_security_struct *sec = ib_sec;
6522 struct lsm_ibpkey_audit ibpkey;
6523
Daniel Jurgens409dcf32017-05-19 15:48:59 +03006524 err = sel_ib_pkey_sid(subnet_prefix, pkey_val, &sid);
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006525 if (err)
6526 return err;
6527
6528 ad.type = LSM_AUDIT_DATA_IBPKEY;
6529 ibpkey.subnet_prefix = subnet_prefix;
6530 ibpkey.pkey = pkey_val;
6531 ad.u.ibpkey = &ibpkey;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006532 return avc_has_perm(&selinux_state,
6533 sec->sid, sid,
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006534 SECCLASS_INFINIBAND_PKEY,
6535 INFINIBAND_PKEY__ACCESS, &ad);
6536}
6537
Daniel Jurgensab861df2017-05-19 15:48:58 +03006538static int selinux_ib_endport_manage_subnet(void *ib_sec, const char *dev_name,
6539 u8 port_num)
6540{
6541 struct common_audit_data ad;
6542 int err;
6543 u32 sid = 0;
6544 struct ib_security_struct *sec = ib_sec;
6545 struct lsm_ibendport_audit ibendport;
6546
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006547 err = security_ib_endport_sid(&selinux_state, dev_name, port_num,
6548 &sid);
Daniel Jurgensab861df2017-05-19 15:48:58 +03006549
6550 if (err)
6551 return err;
6552
6553 ad.type = LSM_AUDIT_DATA_IBENDPORT;
6554 strncpy(ibendport.dev_name, dev_name, sizeof(ibendport.dev_name));
6555 ibendport.port = port_num;
6556 ad.u.ibendport = &ibendport;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006557 return avc_has_perm(&selinux_state,
6558 sec->sid, sid,
Daniel Jurgensab861df2017-05-19 15:48:58 +03006559 SECCLASS_INFINIBAND_ENDPORT,
6560 INFINIBAND_ENDPORT__MANAGE_SUBNET, &ad);
6561}
6562
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006563static int selinux_ib_alloc_security(void **ib_sec)
6564{
6565 struct ib_security_struct *sec;
6566
6567 sec = kzalloc(sizeof(*sec), GFP_KERNEL);
6568 if (!sec)
6569 return -ENOMEM;
6570 sec->sid = current_sid();
6571
6572 *ib_sec = sec;
6573 return 0;
6574}
6575
6576static void selinux_ib_free_security(void *ib_sec)
6577{
6578 kfree(ib_sec);
6579}
Michael LeMayd7200242006-06-22 14:47:17 -07006580#endif
6581
Chenbo Fengec27c352017-10-18 13:00:25 -07006582#ifdef CONFIG_BPF_SYSCALL
6583static int selinux_bpf(int cmd, union bpf_attr *attr,
6584 unsigned int size)
6585{
6586 u32 sid = current_sid();
6587 int ret;
6588
6589 switch (cmd) {
6590 case BPF_MAP_CREATE:
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006591 ret = avc_has_perm(&selinux_state,
6592 sid, sid, SECCLASS_BPF, BPF__MAP_CREATE,
Chenbo Fengec27c352017-10-18 13:00:25 -07006593 NULL);
6594 break;
6595 case BPF_PROG_LOAD:
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006596 ret = avc_has_perm(&selinux_state,
6597 sid, sid, SECCLASS_BPF, BPF__PROG_LOAD,
Chenbo Fengec27c352017-10-18 13:00:25 -07006598 NULL);
6599 break;
6600 default:
6601 ret = 0;
6602 break;
6603 }
6604
6605 return ret;
6606}
6607
6608static u32 bpf_map_fmode_to_av(fmode_t fmode)
6609{
6610 u32 av = 0;
6611
6612 if (fmode & FMODE_READ)
6613 av |= BPF__MAP_READ;
6614 if (fmode & FMODE_WRITE)
6615 av |= BPF__MAP_WRITE;
6616 return av;
6617}
6618
Chenbo Fengf66e4482017-10-18 13:00:26 -07006619/* This function will check the file pass through unix socket or binder to see
6620 * if it is a bpf related object. And apply correspinding checks on the bpf
6621 * object based on the type. The bpf maps and programs, not like other files and
6622 * socket, are using a shared anonymous inode inside the kernel as their inode.
6623 * So checking that inode cannot identify if the process have privilege to
6624 * access the bpf object and that's why we have to add this additional check in
6625 * selinux_file_receive and selinux_binder_transfer_files.
6626 */
6627static int bpf_fd_pass(struct file *file, u32 sid)
6628{
6629 struct bpf_security_struct *bpfsec;
6630 struct bpf_prog *prog;
6631 struct bpf_map *map;
6632 int ret;
6633
6634 if (file->f_op == &bpf_map_fops) {
6635 map = file->private_data;
6636 bpfsec = map->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006637 ret = avc_has_perm(&selinux_state,
6638 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengf66e4482017-10-18 13:00:26 -07006639 bpf_map_fmode_to_av(file->f_mode), NULL);
6640 if (ret)
6641 return ret;
6642 } else if (file->f_op == &bpf_prog_fops) {
6643 prog = file->private_data;
6644 bpfsec = prog->aux->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006645 ret = avc_has_perm(&selinux_state,
6646 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengf66e4482017-10-18 13:00:26 -07006647 BPF__PROG_RUN, NULL);
6648 if (ret)
6649 return ret;
6650 }
6651 return 0;
6652}
6653
Chenbo Fengec27c352017-10-18 13:00:25 -07006654static int selinux_bpf_map(struct bpf_map *map, fmode_t fmode)
6655{
6656 u32 sid = current_sid();
6657 struct bpf_security_struct *bpfsec;
6658
6659 bpfsec = map->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006660 return avc_has_perm(&selinux_state,
6661 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengec27c352017-10-18 13:00:25 -07006662 bpf_map_fmode_to_av(fmode), NULL);
6663}
6664
6665static int selinux_bpf_prog(struct bpf_prog *prog)
6666{
6667 u32 sid = current_sid();
6668 struct bpf_security_struct *bpfsec;
6669
6670 bpfsec = prog->aux->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006671 return avc_has_perm(&selinux_state,
6672 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengec27c352017-10-18 13:00:25 -07006673 BPF__PROG_RUN, NULL);
6674}
6675
6676static int selinux_bpf_map_alloc(struct bpf_map *map)
6677{
6678 struct bpf_security_struct *bpfsec;
6679
6680 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6681 if (!bpfsec)
6682 return -ENOMEM;
6683
6684 bpfsec->sid = current_sid();
6685 map->security = bpfsec;
6686
6687 return 0;
6688}
6689
6690static void selinux_bpf_map_free(struct bpf_map *map)
6691{
6692 struct bpf_security_struct *bpfsec = map->security;
6693
6694 map->security = NULL;
6695 kfree(bpfsec);
6696}
6697
6698static int selinux_bpf_prog_alloc(struct bpf_prog_aux *aux)
6699{
6700 struct bpf_security_struct *bpfsec;
6701
6702 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6703 if (!bpfsec)
6704 return -ENOMEM;
6705
6706 bpfsec->sid = current_sid();
6707 aux->security = bpfsec;
6708
6709 return 0;
6710}
6711
6712static void selinux_bpf_prog_free(struct bpf_prog_aux *aux)
6713{
6714 struct bpf_security_struct *bpfsec = aux->security;
6715
6716 aux->security = NULL;
6717 kfree(bpfsec);
6718}
6719#endif
6720
James Morrisca97d932017-02-15 00:18:51 +11006721static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {
Casey Schauflere20b0432015-05-02 15:11:36 -07006722 LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr),
6723 LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction),
6724 LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder),
6725 LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file),
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02006726
Casey Schauflere20b0432015-05-02 15:11:36 -07006727 LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check),
6728 LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme),
6729 LSM_HOOK_INIT(capget, selinux_capget),
6730 LSM_HOOK_INIT(capset, selinux_capset),
6731 LSM_HOOK_INIT(capable, selinux_capable),
6732 LSM_HOOK_INIT(quotactl, selinux_quotactl),
6733 LSM_HOOK_INIT(quota_on, selinux_quota_on),
6734 LSM_HOOK_INIT(syslog, selinux_syslog),
6735 LSM_HOOK_INIT(vm_enough_memory, selinux_vm_enough_memory),
Stephen Smalley79af7302015-01-21 10:54:10 -05006736
Casey Schauflere20b0432015-05-02 15:11:36 -07006737 LSM_HOOK_INIT(netlink_send, selinux_netlink_send),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006738
Casey Schauflere20b0432015-05-02 15:11:36 -07006739 LSM_HOOK_INIT(bprm_set_creds, selinux_bprm_set_creds),
6740 LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds),
6741 LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006742
Casey Schauflere20b0432015-05-02 15:11:36 -07006743 LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security),
6744 LSM_HOOK_INIT(sb_free_security, selinux_sb_free_security),
Al Viro5b400232018-12-12 20:13:29 -05006745 LSM_HOOK_INIT(sb_eat_lsm_opts, selinux_sb_eat_lsm_opts),
Al Viro204cc0c2018-12-13 13:41:47 -05006746 LSM_HOOK_INIT(sb_free_mnt_opts, selinux_free_mnt_opts),
Casey Schauflere20b0432015-05-02 15:11:36 -07006747 LSM_HOOK_INIT(sb_remount, selinux_sb_remount),
6748 LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount),
6749 LSM_HOOK_INIT(sb_show_options, selinux_sb_show_options),
6750 LSM_HOOK_INIT(sb_statfs, selinux_sb_statfs),
6751 LSM_HOOK_INIT(sb_mount, selinux_mount),
6752 LSM_HOOK_INIT(sb_umount, selinux_umount),
6753 LSM_HOOK_INIT(sb_set_mnt_opts, selinux_set_mnt_opts),
6754 LSM_HOOK_INIT(sb_clone_mnt_opts, selinux_sb_clone_mnt_opts),
Al Viro757cbe52018-12-14 23:42:21 -05006755 LSM_HOOK_INIT(sb_add_mnt_opt, selinux_add_mnt_opt),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006756
Casey Schauflere20b0432015-05-02 15:11:36 -07006757 LSM_HOOK_INIT(dentry_init_security, selinux_dentry_init_security),
Vivek Goyala518b0a2016-07-13 10:44:53 -04006758 LSM_HOOK_INIT(dentry_create_files_as, selinux_dentry_create_files_as),
Eric Parise0007522008-03-05 10:31:54 -05006759
Casey Schauflere20b0432015-05-02 15:11:36 -07006760 LSM_HOOK_INIT(inode_alloc_security, selinux_inode_alloc_security),
6761 LSM_HOOK_INIT(inode_free_security, selinux_inode_free_security),
6762 LSM_HOOK_INIT(inode_init_security, selinux_inode_init_security),
6763 LSM_HOOK_INIT(inode_create, selinux_inode_create),
6764 LSM_HOOK_INIT(inode_link, selinux_inode_link),
6765 LSM_HOOK_INIT(inode_unlink, selinux_inode_unlink),
6766 LSM_HOOK_INIT(inode_symlink, selinux_inode_symlink),
6767 LSM_HOOK_INIT(inode_mkdir, selinux_inode_mkdir),
6768 LSM_HOOK_INIT(inode_rmdir, selinux_inode_rmdir),
6769 LSM_HOOK_INIT(inode_mknod, selinux_inode_mknod),
6770 LSM_HOOK_INIT(inode_rename, selinux_inode_rename),
6771 LSM_HOOK_INIT(inode_readlink, selinux_inode_readlink),
6772 LSM_HOOK_INIT(inode_follow_link, selinux_inode_follow_link),
6773 LSM_HOOK_INIT(inode_permission, selinux_inode_permission),
6774 LSM_HOOK_INIT(inode_setattr, selinux_inode_setattr),
6775 LSM_HOOK_INIT(inode_getattr, selinux_inode_getattr),
6776 LSM_HOOK_INIT(inode_setxattr, selinux_inode_setxattr),
6777 LSM_HOOK_INIT(inode_post_setxattr, selinux_inode_post_setxattr),
6778 LSM_HOOK_INIT(inode_getxattr, selinux_inode_getxattr),
6779 LSM_HOOK_INIT(inode_listxattr, selinux_inode_listxattr),
6780 LSM_HOOK_INIT(inode_removexattr, selinux_inode_removexattr),
6781 LSM_HOOK_INIT(inode_getsecurity, selinux_inode_getsecurity),
6782 LSM_HOOK_INIT(inode_setsecurity, selinux_inode_setsecurity),
6783 LSM_HOOK_INIT(inode_listsecurity, selinux_inode_listsecurity),
6784 LSM_HOOK_INIT(inode_getsecid, selinux_inode_getsecid),
Vivek Goyal56909eb2016-07-13 10:44:48 -04006785 LSM_HOOK_INIT(inode_copy_up, selinux_inode_copy_up),
Vivek Goyal19472b62016-07-13 10:44:50 -04006786 LSM_HOOK_INIT(inode_copy_up_xattr, selinux_inode_copy_up_xattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006787
Casey Schauflere20b0432015-05-02 15:11:36 -07006788 LSM_HOOK_INIT(file_permission, selinux_file_permission),
6789 LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security),
6790 LSM_HOOK_INIT(file_free_security, selinux_file_free_security),
6791 LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl),
6792 LSM_HOOK_INIT(mmap_file, selinux_mmap_file),
6793 LSM_HOOK_INIT(mmap_addr, selinux_mmap_addr),
6794 LSM_HOOK_INIT(file_mprotect, selinux_file_mprotect),
6795 LSM_HOOK_INIT(file_lock, selinux_file_lock),
6796 LSM_HOOK_INIT(file_fcntl, selinux_file_fcntl),
6797 LSM_HOOK_INIT(file_set_fowner, selinux_file_set_fowner),
6798 LSM_HOOK_INIT(file_send_sigiotask, selinux_file_send_sigiotask),
6799 LSM_HOOK_INIT(file_receive, selinux_file_receive),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006800
Casey Schauflere20b0432015-05-02 15:11:36 -07006801 LSM_HOOK_INIT(file_open, selinux_file_open),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006802
Tetsuo Handaa79be232017-03-28 23:08:45 +09006803 LSM_HOOK_INIT(task_alloc, selinux_task_alloc),
Casey Schauflere20b0432015-05-02 15:11:36 -07006804 LSM_HOOK_INIT(cred_alloc_blank, selinux_cred_alloc_blank),
6805 LSM_HOOK_INIT(cred_free, selinux_cred_free),
6806 LSM_HOOK_INIT(cred_prepare, selinux_cred_prepare),
6807 LSM_HOOK_INIT(cred_transfer, selinux_cred_transfer),
Matthew Garrett3ec30112018-01-08 13:36:19 -08006808 LSM_HOOK_INIT(cred_getsecid, selinux_cred_getsecid),
Casey Schauflere20b0432015-05-02 15:11:36 -07006809 LSM_HOOK_INIT(kernel_act_as, selinux_kernel_act_as),
6810 LSM_HOOK_INIT(kernel_create_files_as, selinux_kernel_create_files_as),
6811 LSM_HOOK_INIT(kernel_module_request, selinux_kernel_module_request),
Mimi Zoharc77b8cd2018-07-13 14:06:02 -04006812 LSM_HOOK_INIT(kernel_load_data, selinux_kernel_load_data),
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07006813 LSM_HOOK_INIT(kernel_read_file, selinux_kernel_read_file),
Casey Schauflere20b0432015-05-02 15:11:36 -07006814 LSM_HOOK_INIT(task_setpgid, selinux_task_setpgid),
6815 LSM_HOOK_INIT(task_getpgid, selinux_task_getpgid),
6816 LSM_HOOK_INIT(task_getsid, selinux_task_getsid),
6817 LSM_HOOK_INIT(task_getsecid, selinux_task_getsecid),
6818 LSM_HOOK_INIT(task_setnice, selinux_task_setnice),
6819 LSM_HOOK_INIT(task_setioprio, selinux_task_setioprio),
6820 LSM_HOOK_INIT(task_getioprio, selinux_task_getioprio),
Stephen Smalley791ec492017-02-17 07:57:00 -05006821 LSM_HOOK_INIT(task_prlimit, selinux_task_prlimit),
Casey Schauflere20b0432015-05-02 15:11:36 -07006822 LSM_HOOK_INIT(task_setrlimit, selinux_task_setrlimit),
6823 LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler),
6824 LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler),
6825 LSM_HOOK_INIT(task_movememory, selinux_task_movememory),
6826 LSM_HOOK_INIT(task_kill, selinux_task_kill),
Casey Schauflere20b0432015-05-02 15:11:36 -07006827 LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode),
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09006828
Casey Schauflere20b0432015-05-02 15:11:36 -07006829 LSM_HOOK_INIT(ipc_permission, selinux_ipc_permission),
6830 LSM_HOOK_INIT(ipc_getsecid, selinux_ipc_getsecid),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006831
Casey Schauflere20b0432015-05-02 15:11:36 -07006832 LSM_HOOK_INIT(msg_msg_alloc_security, selinux_msg_msg_alloc_security),
6833 LSM_HOOK_INIT(msg_msg_free_security, selinux_msg_msg_free_security),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006834
Casey Schauflere20b0432015-05-02 15:11:36 -07006835 LSM_HOOK_INIT(msg_queue_alloc_security,
6836 selinux_msg_queue_alloc_security),
6837 LSM_HOOK_INIT(msg_queue_free_security, selinux_msg_queue_free_security),
6838 LSM_HOOK_INIT(msg_queue_associate, selinux_msg_queue_associate),
6839 LSM_HOOK_INIT(msg_queue_msgctl, selinux_msg_queue_msgctl),
6840 LSM_HOOK_INIT(msg_queue_msgsnd, selinux_msg_queue_msgsnd),
6841 LSM_HOOK_INIT(msg_queue_msgrcv, selinux_msg_queue_msgrcv),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006842
Casey Schauflere20b0432015-05-02 15:11:36 -07006843 LSM_HOOK_INIT(shm_alloc_security, selinux_shm_alloc_security),
6844 LSM_HOOK_INIT(shm_free_security, selinux_shm_free_security),
6845 LSM_HOOK_INIT(shm_associate, selinux_shm_associate),
6846 LSM_HOOK_INIT(shm_shmctl, selinux_shm_shmctl),
6847 LSM_HOOK_INIT(shm_shmat, selinux_shm_shmat),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006848
Casey Schauflere20b0432015-05-02 15:11:36 -07006849 LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security),
6850 LSM_HOOK_INIT(sem_free_security, selinux_sem_free_security),
6851 LSM_HOOK_INIT(sem_associate, selinux_sem_associate),
6852 LSM_HOOK_INIT(sem_semctl, selinux_sem_semctl),
6853 LSM_HOOK_INIT(sem_semop, selinux_sem_semop),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006854
Casey Schauflere20b0432015-05-02 15:11:36 -07006855 LSM_HOOK_INIT(d_instantiate, selinux_d_instantiate),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006856
Casey Schauflere20b0432015-05-02 15:11:36 -07006857 LSM_HOOK_INIT(getprocattr, selinux_getprocattr),
6858 LSM_HOOK_INIT(setprocattr, selinux_setprocattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006859
Casey Schauflere20b0432015-05-02 15:11:36 -07006860 LSM_HOOK_INIT(ismaclabel, selinux_ismaclabel),
6861 LSM_HOOK_INIT(secid_to_secctx, selinux_secid_to_secctx),
6862 LSM_HOOK_INIT(secctx_to_secid, selinux_secctx_to_secid),
6863 LSM_HOOK_INIT(release_secctx, selinux_release_secctx),
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006864 LSM_HOOK_INIT(inode_invalidate_secctx, selinux_inode_invalidate_secctx),
Casey Schauflere20b0432015-05-02 15:11:36 -07006865 LSM_HOOK_INIT(inode_notifysecctx, selinux_inode_notifysecctx),
6866 LSM_HOOK_INIT(inode_setsecctx, selinux_inode_setsecctx),
6867 LSM_HOOK_INIT(inode_getsecctx, selinux_inode_getsecctx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006868
Casey Schauflere20b0432015-05-02 15:11:36 -07006869 LSM_HOOK_INIT(unix_stream_connect, selinux_socket_unix_stream_connect),
6870 LSM_HOOK_INIT(unix_may_send, selinux_socket_unix_may_send),
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006871
Casey Schauflere20b0432015-05-02 15:11:36 -07006872 LSM_HOOK_INIT(socket_create, selinux_socket_create),
6873 LSM_HOOK_INIT(socket_post_create, selinux_socket_post_create),
David Herrmann0b811db2018-05-04 16:28:21 +02006874 LSM_HOOK_INIT(socket_socketpair, selinux_socket_socketpair),
Casey Schauflere20b0432015-05-02 15:11:36 -07006875 LSM_HOOK_INIT(socket_bind, selinux_socket_bind),
6876 LSM_HOOK_INIT(socket_connect, selinux_socket_connect),
6877 LSM_HOOK_INIT(socket_listen, selinux_socket_listen),
6878 LSM_HOOK_INIT(socket_accept, selinux_socket_accept),
6879 LSM_HOOK_INIT(socket_sendmsg, selinux_socket_sendmsg),
6880 LSM_HOOK_INIT(socket_recvmsg, selinux_socket_recvmsg),
6881 LSM_HOOK_INIT(socket_getsockname, selinux_socket_getsockname),
6882 LSM_HOOK_INIT(socket_getpeername, selinux_socket_getpeername),
6883 LSM_HOOK_INIT(socket_getsockopt, selinux_socket_getsockopt),
6884 LSM_HOOK_INIT(socket_setsockopt, selinux_socket_setsockopt),
6885 LSM_HOOK_INIT(socket_shutdown, selinux_socket_shutdown),
6886 LSM_HOOK_INIT(socket_sock_rcv_skb, selinux_socket_sock_rcv_skb),
6887 LSM_HOOK_INIT(socket_getpeersec_stream,
6888 selinux_socket_getpeersec_stream),
6889 LSM_HOOK_INIT(socket_getpeersec_dgram, selinux_socket_getpeersec_dgram),
6890 LSM_HOOK_INIT(sk_alloc_security, selinux_sk_alloc_security),
6891 LSM_HOOK_INIT(sk_free_security, selinux_sk_free_security),
6892 LSM_HOOK_INIT(sk_clone_security, selinux_sk_clone_security),
6893 LSM_HOOK_INIT(sk_getsecid, selinux_sk_getsecid),
6894 LSM_HOOK_INIT(sock_graft, selinux_sock_graft),
Richard Hainesd4529302018-02-13 20:57:18 +00006895 LSM_HOOK_INIT(sctp_assoc_request, selinux_sctp_assoc_request),
6896 LSM_HOOK_INIT(sctp_sk_clone, selinux_sctp_sk_clone),
6897 LSM_HOOK_INIT(sctp_bind_connect, selinux_sctp_bind_connect),
Casey Schauflere20b0432015-05-02 15:11:36 -07006898 LSM_HOOK_INIT(inet_conn_request, selinux_inet_conn_request),
6899 LSM_HOOK_INIT(inet_csk_clone, selinux_inet_csk_clone),
6900 LSM_HOOK_INIT(inet_conn_established, selinux_inet_conn_established),
6901 LSM_HOOK_INIT(secmark_relabel_packet, selinux_secmark_relabel_packet),
6902 LSM_HOOK_INIT(secmark_refcount_inc, selinux_secmark_refcount_inc),
6903 LSM_HOOK_INIT(secmark_refcount_dec, selinux_secmark_refcount_dec),
6904 LSM_HOOK_INIT(req_classify_flow, selinux_req_classify_flow),
6905 LSM_HOOK_INIT(tun_dev_alloc_security, selinux_tun_dev_alloc_security),
6906 LSM_HOOK_INIT(tun_dev_free_security, selinux_tun_dev_free_security),
6907 LSM_HOOK_INIT(tun_dev_create, selinux_tun_dev_create),
6908 LSM_HOOK_INIT(tun_dev_attach_queue, selinux_tun_dev_attach_queue),
6909 LSM_HOOK_INIT(tun_dev_attach, selinux_tun_dev_attach),
6910 LSM_HOOK_INIT(tun_dev_open, selinux_tun_dev_open),
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006911#ifdef CONFIG_SECURITY_INFINIBAND
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006912 LSM_HOOK_INIT(ib_pkey_access, selinux_ib_pkey_access),
Daniel Jurgensab861df2017-05-19 15:48:58 +03006913 LSM_HOOK_INIT(ib_endport_manage_subnet,
6914 selinux_ib_endport_manage_subnet),
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006915 LSM_HOOK_INIT(ib_alloc_security, selinux_ib_alloc_security),
6916 LSM_HOOK_INIT(ib_free_security, selinux_ib_free_security),
6917#endif
Trent Jaegerd28d1e02005-12-13 23:12:40 -08006918#ifdef CONFIG_SECURITY_NETWORK_XFRM
Casey Schauflere20b0432015-05-02 15:11:36 -07006919 LSM_HOOK_INIT(xfrm_policy_alloc_security, selinux_xfrm_policy_alloc),
6920 LSM_HOOK_INIT(xfrm_policy_clone_security, selinux_xfrm_policy_clone),
6921 LSM_HOOK_INIT(xfrm_policy_free_security, selinux_xfrm_policy_free),
6922 LSM_HOOK_INIT(xfrm_policy_delete_security, selinux_xfrm_policy_delete),
6923 LSM_HOOK_INIT(xfrm_state_alloc, selinux_xfrm_state_alloc),
6924 LSM_HOOK_INIT(xfrm_state_alloc_acquire,
6925 selinux_xfrm_state_alloc_acquire),
6926 LSM_HOOK_INIT(xfrm_state_free_security, selinux_xfrm_state_free),
6927 LSM_HOOK_INIT(xfrm_state_delete_security, selinux_xfrm_state_delete),
6928 LSM_HOOK_INIT(xfrm_policy_lookup, selinux_xfrm_policy_lookup),
6929 LSM_HOOK_INIT(xfrm_state_pol_flow_match,
6930 selinux_xfrm_state_pol_flow_match),
6931 LSM_HOOK_INIT(xfrm_decode_session, selinux_xfrm_decode_session),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006932#endif
Michael LeMayd7200242006-06-22 14:47:17 -07006933
6934#ifdef CONFIG_KEYS
Casey Schauflere20b0432015-05-02 15:11:36 -07006935 LSM_HOOK_INIT(key_alloc, selinux_key_alloc),
6936 LSM_HOOK_INIT(key_free, selinux_key_free),
6937 LSM_HOOK_INIT(key_permission, selinux_key_permission),
6938 LSM_HOOK_INIT(key_getsecurity, selinux_key_getsecurity),
Michael LeMayd7200242006-06-22 14:47:17 -07006939#endif
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02006940
6941#ifdef CONFIG_AUDIT
Casey Schauflere20b0432015-05-02 15:11:36 -07006942 LSM_HOOK_INIT(audit_rule_init, selinux_audit_rule_init),
6943 LSM_HOOK_INIT(audit_rule_known, selinux_audit_rule_known),
6944 LSM_HOOK_INIT(audit_rule_match, selinux_audit_rule_match),
6945 LSM_HOOK_INIT(audit_rule_free, selinux_audit_rule_free),
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02006946#endif
Chenbo Fengec27c352017-10-18 13:00:25 -07006947
6948#ifdef CONFIG_BPF_SYSCALL
6949 LSM_HOOK_INIT(bpf, selinux_bpf),
6950 LSM_HOOK_INIT(bpf_map, selinux_bpf_map),
6951 LSM_HOOK_INIT(bpf_prog, selinux_bpf_prog),
6952 LSM_HOOK_INIT(bpf_map_alloc_security, selinux_bpf_map_alloc),
6953 LSM_HOOK_INIT(bpf_prog_alloc_security, selinux_bpf_prog_alloc),
6954 LSM_HOOK_INIT(bpf_map_free_security, selinux_bpf_map_free),
6955 LSM_HOOK_INIT(bpf_prog_free_security, selinux_bpf_prog_free),
6956#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006957};
6958
6959static __init int selinux_init(void)
6960{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07006961 if (!security_module_enable("selinux")) {
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02006962 selinux_enabled = 0;
6963 return 0;
6964 }
6965
Linus Torvalds1da177e2005-04-16 15:20:36 -07006966 if (!selinux_enabled) {
peter enderborgc103a912018-06-12 10:09:03 +02006967 pr_info("SELinux: Disabled at boot.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006968 return 0;
6969 }
6970
peter enderborgc103a912018-06-12 10:09:03 +02006971 pr_info("SELinux: Initializing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006972
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006973 memset(&selinux_state, 0, sizeof(selinux_state));
Paul Mooree5a5ca92018-03-01 17:38:30 -05006974 enforcing_set(&selinux_state, selinux_enforcing_boot);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006975 selinux_state.checkreqprot = selinux_checkreqprot_boot;
6976 selinux_ss_init(&selinux_state.ss);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006977 selinux_avc_init(&selinux_state.avc);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006978
Linus Torvalds1da177e2005-04-16 15:20:36 -07006979 /* Set the security state for the initial task. */
David Howellsd84f4f92008-11-14 10:39:23 +11006980 cred_init_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006981
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04006982 default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
6983
James Morris7cae7e22006-03-22 00:09:22 -08006984 sel_inode_cache = kmem_cache_create("selinux_inode_security",
6985 sizeof(struct inode_security_struct),
Paul Mundt20c2df82007-07-20 10:11:58 +09006986 0, SLAB_PANIC, NULL);
Sangwoo63205652015-10-21 17:44:30 -04006987 file_security_cache = kmem_cache_create("selinux_file_security",
6988 sizeof(struct file_security_struct),
6989 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006990 avc_init();
6991
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006992 avtab_cache_init();
6993
6994 ebitmap_cache_init();
6995
6996 hashtab_cache_init();
6997
Casey Schauflerd69dece52017-01-18 17:09:05 -08006998 security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks), "selinux");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006999
Paul Moore615e51f2014-06-26 14:33:56 -04007000 if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET))
7001 panic("SELinux: Unable to register AVC netcache callback\n");
7002
Daniel Jurgens8f408ab2017-05-19 15:48:53 +03007003 if (avc_add_callback(selinux_lsm_notifier_avc_callback, AVC_CALLBACK_RESET))
7004 panic("SELinux: Unable to register AVC LSM notifier callback\n");
7005
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007006 if (selinux_enforcing_boot)
peter enderborgc103a912018-06-12 10:09:03 +02007007 pr_debug("SELinux: Starting in enforcing mode\n");
Eric Paris828dfe12008-04-17 13:17:49 -04007008 else
peter enderborgc103a912018-06-12 10:09:03 +02007009 pr_debug("SELinux: Starting in permissive mode\n");
Michael LeMayd7200242006-06-22 14:47:17 -07007010
Linus Torvalds1da177e2005-04-16 15:20:36 -07007011 return 0;
7012}
7013
Al Viroe8c26252010-03-23 06:36:54 -04007014static void delayed_superblock_init(struct super_block *sb, void *unused)
7015{
Al Viro204cc0c2018-12-13 13:41:47 -05007016 selinux_set_mnt_opts(sb, NULL, 0, NULL);
Al Viroe8c26252010-03-23 06:36:54 -04007017}
7018
Linus Torvalds1da177e2005-04-16 15:20:36 -07007019void selinux_complete_init(void)
7020{
peter enderborgc103a912018-06-12 10:09:03 +02007021 pr_debug("SELinux: Completing initialization.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007022
7023 /* Set up any superblocks initialized prior to the policy load. */
peter enderborgc103a912018-06-12 10:09:03 +02007024 pr_debug("SELinux: Setting up existing superblocks.\n");
Al Viroe8c26252010-03-23 06:36:54 -04007025 iterate_supers(delayed_superblock_init, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007026}
7027
7028/* SELinux requires early initialization in order to label
7029 all processes and objects when they are created. */
Kees Cook3d6e5f62018-10-10 17:18:23 -07007030DEFINE_LSM(selinux) = {
Kees Cook07aed2f2018-10-10 17:18:24 -07007031 .name = "selinux",
Kees Cook3d6e5f62018-10-10 17:18:23 -07007032 .init = selinux_init,
7033};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007034
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007035#if defined(CONFIG_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007036
Florian Westphal591bb272017-07-26 11:40:52 +02007037static const struct nf_hook_ops selinux_nf_ops[] = {
Paul Mooreeffad8d2008-01-29 08:49:27 -05007038 {
7039 .hook = selinux_ipv4_postroute,
Alban Crequy2597a832012-05-14 03:56:39 +00007040 .pf = NFPROTO_IPV4,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007041 .hooknum = NF_INET_POST_ROUTING,
7042 .priority = NF_IP_PRI_SELINUX_LAST,
7043 },
7044 {
7045 .hook = selinux_ipv4_forward,
Alban Crequy2597a832012-05-14 03:56:39 +00007046 .pf = NFPROTO_IPV4,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007047 .hooknum = NF_INET_FORWARD,
7048 .priority = NF_IP_PRI_SELINUX_FIRST,
Paul Moore948bf852008-10-10 10:16:32 -04007049 },
7050 {
7051 .hook = selinux_ipv4_output,
Alban Crequy2597a832012-05-14 03:56:39 +00007052 .pf = NFPROTO_IPV4,
Paul Moore948bf852008-10-10 10:16:32 -04007053 .hooknum = NF_INET_LOCAL_OUT,
7054 .priority = NF_IP_PRI_SELINUX_FIRST,
Jiri Pirko25db6be2014-09-03 17:42:13 +02007055 },
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04007056#if IS_ENABLED(CONFIG_IPV6)
Paul Mooreeffad8d2008-01-29 08:49:27 -05007057 {
7058 .hook = selinux_ipv6_postroute,
Alban Crequy2597a832012-05-14 03:56:39 +00007059 .pf = NFPROTO_IPV6,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007060 .hooknum = NF_INET_POST_ROUTING,
7061 .priority = NF_IP6_PRI_SELINUX_LAST,
7062 },
7063 {
7064 .hook = selinux_ipv6_forward,
Alban Crequy2597a832012-05-14 03:56:39 +00007065 .pf = NFPROTO_IPV6,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007066 .hooknum = NF_INET_FORWARD,
7067 .priority = NF_IP6_PRI_SELINUX_FIRST,
Jiri Pirko25db6be2014-09-03 17:42:13 +02007068 },
Huw Davies2917f572016-06-27 15:06:15 -04007069 {
7070 .hook = selinux_ipv6_output,
7071 .pf = NFPROTO_IPV6,
7072 .hooknum = NF_INET_LOCAL_OUT,
7073 .priority = NF_IP6_PRI_SELINUX_FIRST,
7074 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07007075#endif /* IPV6 */
Jiri Pirko25db6be2014-09-03 17:42:13 +02007076};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007077
Florian Westphal8e71bf72017-04-21 11:49:09 +02007078static int __net_init selinux_nf_register(struct net *net)
7079{
7080 return nf_register_net_hooks(net, selinux_nf_ops,
7081 ARRAY_SIZE(selinux_nf_ops));
7082}
7083
7084static void __net_exit selinux_nf_unregister(struct net *net)
7085{
7086 nf_unregister_net_hooks(net, selinux_nf_ops,
7087 ARRAY_SIZE(selinux_nf_ops));
7088}
7089
7090static struct pernet_operations selinux_net_ops = {
7091 .init = selinux_nf_register,
7092 .exit = selinux_nf_unregister,
7093};
7094
Linus Torvalds1da177e2005-04-16 15:20:36 -07007095static int __init selinux_nf_ip_init(void)
7096{
Jiri Pirko25db6be2014-09-03 17:42:13 +02007097 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007098
7099 if (!selinux_enabled)
Jiri Pirko25db6be2014-09-03 17:42:13 +02007100 return 0;
Eric Parisfadcdb42007-02-22 18:11:31 -05007101
peter enderborgc103a912018-06-12 10:09:03 +02007102 pr_debug("SELinux: Registering netfilter hooks\n");
Eric Parisfadcdb42007-02-22 18:11:31 -05007103
Florian Westphal8e71bf72017-04-21 11:49:09 +02007104 err = register_pernet_subsys(&selinux_net_ops);
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07007105 if (err)
Florian Westphal8e71bf72017-04-21 11:49:09 +02007106 panic("SELinux: register_pernet_subsys: error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007107
Jiri Pirko25db6be2014-09-03 17:42:13 +02007108 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007109}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007110__initcall(selinux_nf_ip_init);
7111
7112#ifdef CONFIG_SECURITY_SELINUX_DISABLE
7113static void selinux_nf_ip_exit(void)
7114{
peter enderborgc103a912018-06-12 10:09:03 +02007115 pr_debug("SELinux: Unregistering netfilter hooks\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007116
Florian Westphal8e71bf72017-04-21 11:49:09 +02007117 unregister_pernet_subsys(&selinux_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007118}
7119#endif
7120
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007121#else /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007122
7123#ifdef CONFIG_SECURITY_SELINUX_DISABLE
7124#define selinux_nf_ip_exit()
7125#endif
7126
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007127#endif /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007128
7129#ifdef CONFIG_SECURITY_SELINUX_DISABLE
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007130int selinux_disable(struct selinux_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007131{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007132 if (state->initialized) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007133 /* Not permitted after initial policy load. */
7134 return -EINVAL;
7135 }
7136
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007137 if (state->disabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007138 /* Only do this once. */
7139 return -EINVAL;
7140 }
7141
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007142 state->disabled = 1;
7143
peter enderborgc103a912018-06-12 10:09:03 +02007144 pr_info("SELinux: Disabled at runtime.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007145
Stephen Smalley30d55282006-05-03 10:52:36 -04007146 selinux_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007147
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07007148 security_delete_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007149
Eric Parisaf8ff042009-09-20 21:23:01 -04007150 /* Try to destroy the avc node cache */
7151 avc_disable();
7152
Linus Torvalds1da177e2005-04-16 15:20:36 -07007153 /* Unregister netfilter hooks. */
7154 selinux_nf_ip_exit();
7155
7156 /* Unregister selinuxfs. */
7157 exit_sel_fs();
7158
7159 return 0;
7160}
7161#endif