blob: 0fe5ed8c33a0ade2df5e1adb25b2b7bafa792bd0 [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 */
J. Bruce Fields3815a242019-03-05 16:17:58 -0500984 if (newsbsec->flags & SE_SBINITIALIZED) {
985 if ((kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context)
986 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
Jeff Layton094f7b62013-04-01 08:14:24 -0400987 return selinux_cmp_sb_context(oldsb, newsb);
J. Bruce Fields3815a242019-03-05 16:17:58 -0500988 }
Eric Paris5a552612008-04-09 14:08:35 -0400989
Eric Parisc9180a52007-11-30 13:00:35 -0500990 mutex_lock(&newsbsec->lock);
991
992 newsbsec->flags = oldsbsec->flags;
993
994 newsbsec->sid = oldsbsec->sid;
995 newsbsec->def_sid = oldsbsec->def_sid;
996 newsbsec->behavior = oldsbsec->behavior;
997
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400998 if (newsbsec->behavior == SECURITY_FS_USE_NATIVE &&
999 !(kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001000 rc = security_fs_use(&selinux_state, newsb);
Scott Mayhew0b4d3452017-06-05 11:45:04 -04001001 if (rc)
1002 goto out;
1003 }
1004
1005 if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !set_context) {
1006 newsbsec->behavior = SECURITY_FS_USE_NATIVE;
1007 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
1008 }
1009
Eric Parisc9180a52007-11-30 13:00:35 -05001010 if (set_context) {
1011 u32 sid = oldsbsec->mntpoint_sid;
1012
1013 if (!set_fscontext)
1014 newsbsec->sid = sid;
1015 if (!set_rootcontext) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001016 struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
Eric Parisc9180a52007-11-30 13:00:35 -05001017 newisec->sid = sid;
1018 }
1019 newsbsec->mntpoint_sid = sid;
1020 }
1021 if (set_rootcontext) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001022 const struct inode_security_struct *oldisec = backing_inode_security(oldsb->s_root);
1023 struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
Eric Parisc9180a52007-11-30 13:00:35 -05001024
1025 newisec->sid = oldisec->sid;
1026 }
1027
1028 sb_finish_set_opts(newsb);
Scott Mayhew0b4d3452017-06-05 11:45:04 -04001029out:
Eric Parisc9180a52007-11-30 13:00:35 -05001030 mutex_unlock(&newsbsec->lock);
Scott Mayhew0b4d3452017-06-05 11:45:04 -04001031 return rc;
Eric Parisc9180a52007-11-30 13:00:35 -05001032}
1033
Al Viroba641862018-12-14 20:28:15 -05001034static int selinux_add_opt(int token, const char *s, void **mnt_opts)
Eric Parisc9180a52007-11-30 13:00:35 -05001035{
Al Viroba641862018-12-14 20:28:15 -05001036 struct selinux_mnt_opts *opts = *mnt_opts;
Eric Parisc9180a52007-11-30 13:00:35 -05001037
Al Viroda3d76a2018-12-17 10:14:16 -05001038 if (token == Opt_seclabel) /* eaten and completely ignored */
Al Viro169d68efb2018-12-14 22:44:50 -05001039 return 0;
Eric Parisc9180a52007-11-30 13:00:35 -05001040
Al Viroba641862018-12-14 20:28:15 -05001041 if (!opts) {
1042 opts = kzalloc(sizeof(struct selinux_mnt_opts), GFP_KERNEL);
1043 if (!opts)
1044 return -ENOMEM;
1045 *mnt_opts = opts;
1046 }
1047 if (!s)
1048 return -ENOMEM;
1049 switch (token) {
1050 case Opt_context:
1051 if (opts->context || opts->defcontext)
1052 goto Einval;
1053 opts->context = s;
1054 break;
1055 case Opt_fscontext:
1056 if (opts->fscontext)
1057 goto Einval;
1058 opts->fscontext = s;
1059 break;
1060 case Opt_rootcontext:
1061 if (opts->rootcontext)
1062 goto Einval;
1063 opts->rootcontext = s;
1064 break;
1065 case Opt_defcontext:
1066 if (opts->context || opts->defcontext)
1067 goto Einval;
1068 opts->defcontext = s;
1069 break;
1070 }
1071 return 0;
1072Einval:
1073 pr_warn(SEL_MOUNT_FAIL_MSG);
Al Viroba641862018-12-14 20:28:15 -05001074 return -EINVAL;
1075}
Eric Parisc9180a52007-11-30 13:00:35 -05001076
Al Viro757cbe52018-12-14 23:42:21 -05001077static int selinux_add_mnt_opt(const char *option, const char *val, int len,
1078 void **mnt_opts)
Eric Parisc9180a52007-11-30 13:00:35 -05001079{
Al Viro757cbe52018-12-14 23:42:21 -05001080 int token = Opt_error;
1081 int rc, i;
Eric Parisc9180a52007-11-30 13:00:35 -05001082
Al Viro757cbe52018-12-14 23:42:21 -05001083 for (i = 0; i < ARRAY_SIZE(tokens); i++) {
1084 if (strcmp(option, tokens[i].name) == 0) {
1085 token = tokens[i].opt;
Eric Parisc9180a52007-11-30 13:00:35 -05001086 break;
Eric Parisc9180a52007-11-30 13:00:35 -05001087 }
1088 }
1089
Al Viro757cbe52018-12-14 23:42:21 -05001090 if (token == Opt_error)
1091 return -EINVAL;
Eric Parise0007522008-03-05 10:31:54 -05001092
Al Viro757cbe52018-12-14 23:42:21 -05001093 if (token != Opt_seclabel)
1094 val = kmemdup_nul(val, len, GFP_KERNEL);
1095 rc = selinux_add_opt(token, val, mnt_opts);
1096 if (unlikely(rc)) {
1097 kfree(val);
1098 if (*mnt_opts) {
1099 selinux_free_mnt_opts(*mnt_opts);
1100 *mnt_opts = NULL;
1101 }
1102 }
1103 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Al Viroe3489f82018-12-13 00:24:36 -05001106static int show_sid(struct seq_file *m, u32 sid)
Eric Paris2069f452008-07-04 09:47:13 +10001107{
Al Viroe3489f82018-12-13 00:24:36 -05001108 char *context = NULL;
1109 u32 len;
1110 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Al Viroe3489f82018-12-13 00:24:36 -05001112 rc = security_sid_to_context(&selinux_state, sid,
1113 &context, &len);
1114 if (!rc) {
1115 bool has_comma = context && strchr(context, ',');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Eric Paris2069f452008-07-04 09:47:13 +10001117 if (has_comma)
1118 seq_putc(m, '\"');
Al Viroe3489f82018-12-13 00:24:36 -05001119 seq_escape(m, context, "\"\n\\");
Eric Paris2069f452008-07-04 09:47:13 +10001120 if (has_comma)
1121 seq_putc(m, '\"');
1122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 kfree(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 return rc;
1125}
Eric Paris2069f452008-07-04 09:47:13 +10001126
1127static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
1128{
Al Viroe3489f82018-12-13 00:24:36 -05001129 struct superblock_security_struct *sbsec = sb->s_security;
Eric Paris2069f452008-07-04 09:47:13 +10001130 int rc;
1131
Al Viroe3489f82018-12-13 00:24:36 -05001132 if (!(sbsec->flags & SE_SBINITIALIZED))
1133 return 0;
1134
1135 if (!selinux_state.initialized)
1136 return 0;
1137
1138 if (sbsec->flags & FSCONTEXT_MNT) {
1139 seq_putc(m, ',');
1140 seq_puts(m, FSCONTEXT_STR);
1141 rc = show_sid(m, sbsec->sid);
1142 if (rc)
1143 return rc;
Eric Paris383795c2008-07-29 17:07:26 -04001144 }
Al Viroe3489f82018-12-13 00:24:36 -05001145 if (sbsec->flags & CONTEXT_MNT) {
1146 seq_putc(m, ',');
1147 seq_puts(m, CONTEXT_STR);
1148 rc = show_sid(m, sbsec->mntpoint_sid);
1149 if (rc)
1150 return rc;
1151 }
1152 if (sbsec->flags & DEFCONTEXT_MNT) {
1153 seq_putc(m, ',');
1154 seq_puts(m, DEFCONTEXT_STR);
1155 rc = show_sid(m, sbsec->def_sid);
1156 if (rc)
1157 return rc;
1158 }
1159 if (sbsec->flags & ROOTCONTEXT_MNT) {
1160 struct dentry *root = sbsec->sb->s_root;
1161 struct inode_security_struct *isec = backing_inode_security(root);
1162 seq_putc(m, ',');
1163 seq_puts(m, ROOTCONTEXT_STR);
1164 rc = show_sid(m, isec->sid);
1165 if (rc)
1166 return rc;
1167 }
1168 if (sbsec->flags & SBLABEL_MNT) {
1169 seq_putc(m, ',');
1170 seq_puts(m, LABELSUPP_STR);
1171 }
1172 return 0;
Eric Paris2069f452008-07-04 09:47:13 +10001173}
1174
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175static inline u16 inode_mode_to_security_class(umode_t mode)
1176{
1177 switch (mode & S_IFMT) {
1178 case S_IFSOCK:
1179 return SECCLASS_SOCK_FILE;
1180 case S_IFLNK:
1181 return SECCLASS_LNK_FILE;
1182 case S_IFREG:
1183 return SECCLASS_FILE;
1184 case S_IFBLK:
1185 return SECCLASS_BLK_FILE;
1186 case S_IFDIR:
1187 return SECCLASS_DIR;
1188 case S_IFCHR:
1189 return SECCLASS_CHR_FILE;
1190 case S_IFIFO:
1191 return SECCLASS_FIFO_FILE;
1192
1193 }
1194
1195 return SECCLASS_FILE;
1196}
1197
James Morris13402582005-09-30 14:24:34 -04001198static inline int default_protocol_stream(int protocol)
1199{
1200 return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP);
1201}
1202
1203static inline int default_protocol_dgram(int protocol)
1204{
1205 return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
1206}
1207
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208static inline u16 socket_type_to_security_class(int family, int type, int protocol)
1209{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001210 int extsockclass = selinux_policycap_extsockclass();
Stephen Smalleyda69a532017-01-09 10:07:30 -05001211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 switch (family) {
1213 case PF_UNIX:
1214 switch (type) {
1215 case SOCK_STREAM:
1216 case SOCK_SEQPACKET:
1217 return SECCLASS_UNIX_STREAM_SOCKET;
1218 case SOCK_DGRAM:
Luis Ressel2a764b52017-07-25 15:13:41 -04001219 case SOCK_RAW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 return SECCLASS_UNIX_DGRAM_SOCKET;
1221 }
1222 break;
1223 case PF_INET:
1224 case PF_INET6:
1225 switch (type) {
1226 case SOCK_STREAM:
Stephen Smalleyda69a532017-01-09 10:07:30 -05001227 case SOCK_SEQPACKET:
James Morris13402582005-09-30 14:24:34 -04001228 if (default_protocol_stream(protocol))
1229 return SECCLASS_TCP_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001230 else if (extsockclass && protocol == IPPROTO_SCTP)
1231 return SECCLASS_SCTP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001232 else
1233 return SECCLASS_RAWIP_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 case SOCK_DGRAM:
James Morris13402582005-09-30 14:24:34 -04001235 if (default_protocol_dgram(protocol))
1236 return SECCLASS_UDP_SOCKET;
Stephen Smalleyef379792017-01-09 10:07:31 -05001237 else if (extsockclass && (protocol == IPPROTO_ICMP ||
1238 protocol == IPPROTO_ICMPV6))
Stephen Smalleyda69a532017-01-09 10:07:30 -05001239 return SECCLASS_ICMP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001240 else
1241 return SECCLASS_RAWIP_SOCKET;
James Morris2ee92d42006-11-13 16:09:01 -08001242 case SOCK_DCCP:
1243 return SECCLASS_DCCP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001244 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 return SECCLASS_RAWIP_SOCKET;
1246 }
1247 break;
1248 case PF_NETLINK:
1249 switch (protocol) {
1250 case NETLINK_ROUTE:
1251 return SECCLASS_NETLINK_ROUTE_SOCKET;
Pavel Emelyanov7f1fb602011-12-06 07:56:43 +00001252 case NETLINK_SOCK_DIAG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 return SECCLASS_NETLINK_TCPDIAG_SOCKET;
1254 case NETLINK_NFLOG:
1255 return SECCLASS_NETLINK_NFLOG_SOCKET;
1256 case NETLINK_XFRM:
1257 return SECCLASS_NETLINK_XFRM_SOCKET;
1258 case NETLINK_SELINUX:
1259 return SECCLASS_NETLINK_SELINUX_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001260 case NETLINK_ISCSI:
1261 return SECCLASS_NETLINK_ISCSI_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 case NETLINK_AUDIT:
1263 return SECCLASS_NETLINK_AUDIT_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001264 case NETLINK_FIB_LOOKUP:
1265 return SECCLASS_NETLINK_FIB_LOOKUP_SOCKET;
1266 case NETLINK_CONNECTOR:
1267 return SECCLASS_NETLINK_CONNECTOR_SOCKET;
1268 case NETLINK_NETFILTER:
1269 return SECCLASS_NETLINK_NETFILTER_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 case NETLINK_DNRTMSG:
1271 return SECCLASS_NETLINK_DNRT_SOCKET;
James Morris0c9b7942005-04-16 15:24:13 -07001272 case NETLINK_KOBJECT_UEVENT:
1273 return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001274 case NETLINK_GENERIC:
1275 return SECCLASS_NETLINK_GENERIC_SOCKET;
1276 case NETLINK_SCSITRANSPORT:
1277 return SECCLASS_NETLINK_SCSITRANSPORT_SOCKET;
1278 case NETLINK_RDMA:
1279 return SECCLASS_NETLINK_RDMA_SOCKET;
1280 case NETLINK_CRYPTO:
1281 return SECCLASS_NETLINK_CRYPTO_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 default:
1283 return SECCLASS_NETLINK_SOCKET;
1284 }
1285 case PF_PACKET:
1286 return SECCLASS_PACKET_SOCKET;
1287 case PF_KEY:
1288 return SECCLASS_KEY_SOCKET;
Christopher J. PeBenito3e3ff152006-06-09 00:25:03 -07001289 case PF_APPLETALK:
1290 return SECCLASS_APPLETALK_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 }
1292
Stephen Smalleyda69a532017-01-09 10:07:30 -05001293 if (extsockclass) {
1294 switch (family) {
1295 case PF_AX25:
1296 return SECCLASS_AX25_SOCKET;
1297 case PF_IPX:
1298 return SECCLASS_IPX_SOCKET;
1299 case PF_NETROM:
1300 return SECCLASS_NETROM_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001301 case PF_ATMPVC:
1302 return SECCLASS_ATMPVC_SOCKET;
1303 case PF_X25:
1304 return SECCLASS_X25_SOCKET;
1305 case PF_ROSE:
1306 return SECCLASS_ROSE_SOCKET;
1307 case PF_DECnet:
1308 return SECCLASS_DECNET_SOCKET;
1309 case PF_ATMSVC:
1310 return SECCLASS_ATMSVC_SOCKET;
1311 case PF_RDS:
1312 return SECCLASS_RDS_SOCKET;
1313 case PF_IRDA:
1314 return SECCLASS_IRDA_SOCKET;
1315 case PF_PPPOX:
1316 return SECCLASS_PPPOX_SOCKET;
1317 case PF_LLC:
1318 return SECCLASS_LLC_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001319 case PF_CAN:
1320 return SECCLASS_CAN_SOCKET;
1321 case PF_TIPC:
1322 return SECCLASS_TIPC_SOCKET;
1323 case PF_BLUETOOTH:
1324 return SECCLASS_BLUETOOTH_SOCKET;
1325 case PF_IUCV:
1326 return SECCLASS_IUCV_SOCKET;
1327 case PF_RXRPC:
1328 return SECCLASS_RXRPC_SOCKET;
1329 case PF_ISDN:
1330 return SECCLASS_ISDN_SOCKET;
1331 case PF_PHONET:
1332 return SECCLASS_PHONET_SOCKET;
1333 case PF_IEEE802154:
1334 return SECCLASS_IEEE802154_SOCKET;
1335 case PF_CAIF:
1336 return SECCLASS_CAIF_SOCKET;
1337 case PF_ALG:
1338 return SECCLASS_ALG_SOCKET;
1339 case PF_NFC:
1340 return SECCLASS_NFC_SOCKET;
1341 case PF_VSOCK:
1342 return SECCLASS_VSOCK_SOCKET;
1343 case PF_KCM:
1344 return SECCLASS_KCM_SOCKET;
1345 case PF_QIPCRTR:
1346 return SECCLASS_QIPCRTR_SOCKET;
Linus Torvalds3051bf32017-02-22 10:15:09 -08001347 case PF_SMC:
1348 return SECCLASS_SMC_SOCKET;
Björn Töpel68e8b842018-05-02 13:01:22 +02001349 case PF_XDP:
1350 return SECCLASS_XDP_SOCKET;
1351#if PF_MAX > 45
Stephen Smalleyda69a532017-01-09 10:07:30 -05001352#error New address family defined, please update this function.
1353#endif
1354 }
1355 }
1356
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 return SECCLASS_SOCKET;
1358}
1359
Stephen Smalley134509d2015-06-04 16:22:17 -04001360static int selinux_genfs_get_sid(struct dentry *dentry,
1361 u16 tclass,
1362 u16 flags,
1363 u32 *sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001365 int rc;
Al Virofc640052016-04-10 01:33:30 -04001366 struct super_block *sb = dentry->d_sb;
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001367 char *buffer, *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Eric Paris828dfe12008-04-17 13:17:49 -04001369 buffer = (char *)__get_free_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 if (!buffer)
1371 return -ENOMEM;
1372
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001373 path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
1374 if (IS_ERR(path))
1375 rc = PTR_ERR(path);
1376 else {
Stephen Smalley134509d2015-06-04 16:22:17 -04001377 if (flags & SE_SBPROC) {
1378 /* each process gets a /proc/PID/ entry. Strip off the
1379 * PID part to get a valid selinux labeling.
1380 * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
1381 while (path[1] >= '0' && path[1] <= '9') {
1382 path[1] = '/';
1383 path++;
1384 }
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001385 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001386 rc = security_genfs_sid(&selinux_state, sb->s_type->name,
1387 path, tclass, sid);
Stephen Smalley7bb185e2018-09-04 16:51:36 -04001388 if (rc == -ENOENT) {
1389 /* No match in policy, mark as unlabeled. */
1390 *sid = SECINITSID_UNLABELED;
1391 rc = 0;
1392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 free_page((unsigned long)buffer);
1395 return rc;
1396}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
1398/* The inode's security attributes must be initialized before first use. */
1399static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
1400{
1401 struct superblock_security_struct *sbsec = NULL;
1402 struct inode_security_struct *isec = inode->i_security;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001403 u32 task_sid, sid = 0;
1404 u16 sclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 struct dentry *dentry;
1406#define INITCONTEXTLEN 255
1407 char *context = NULL;
1408 unsigned len = 0;
1409 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05001411 if (isec->initialized == LABEL_INITIALIZED)
Andreas Gruenbacher13457d02016-11-10 22:18:29 +01001412 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001414 spin_lock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05001415 if (isec->initialized == LABEL_INITIALIZED)
Eric Paris23970742006-09-25 23:32:01 -07001416 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
Andreas Gruenbacher13457d02016-11-10 22:18:29 +01001418 if (isec->sclass == SECCLASS_FILE)
1419 isec->sclass = inode_mode_to_security_class(inode->i_mode);
1420
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 sbsec = inode->i_sb->s_security;
David P. Quigley0d90a7e2009-01-16 09:22:02 -05001422 if (!(sbsec->flags & SE_SBINITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 /* Defer initialization until selinux_complete_init,
1424 after the initial policy is loaded and the security
1425 server is ready to handle calls. */
1426 spin_lock(&sbsec->isec_lock);
1427 if (list_empty(&isec->list))
1428 list_add(&isec->list, &sbsec->isec_head);
1429 spin_unlock(&sbsec->isec_lock);
Eric Paris23970742006-09-25 23:32:01 -07001430 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 }
1432
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001433 sclass = isec->sclass;
1434 task_sid = isec->task_sid;
1435 sid = isec->sid;
1436 isec->initialized = LABEL_PENDING;
1437 spin_unlock(&isec->lock);
1438
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 switch (sbsec->behavior) {
David Quigleyeb9ae682013-05-22 12:50:37 -04001440 case SECURITY_FS_USE_NATIVE:
1441 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 case SECURITY_FS_USE_XATTR:
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001443 if (!(inode->i_opflags & IOP_XATTR)) {
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001444 sid = sbsec->def_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 break;
1446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 /* Need a dentry, since the xattr API requires one.
1448 Life would be simpler if we could just pass the inode. */
1449 if (opt_dentry) {
1450 /* Called from d_instantiate or d_splice_alias. */
1451 dentry = dget(opt_dentry);
1452 } else {
Al Virob1271252018-04-25 10:28:38 -04001453 /*
1454 * Called from selinux_complete_init, try to find a dentry.
1455 * Some filesystems really want a connected one, so try
1456 * that first. We could split SECURITY_FS_USE_XATTR in
1457 * two, depending upon that...
1458 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 dentry = d_find_alias(inode);
Al Virob1271252018-04-25 10:28:38 -04001460 if (!dentry)
1461 dentry = d_find_any_alias(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 }
1463 if (!dentry) {
Eric Parisdf7f54c2009-03-09 14:35:58 -04001464 /*
1465 * this is can be hit on boot when a file is accessed
1466 * before the policy is loaded. When we load policy we
1467 * may find inodes that have no dentry on the
1468 * sbsec->isec_head list. No reason to complain as these
1469 * will get fixed up the next time we go through
1470 * inode_doinit with a dentry, before these inodes could
1471 * be used again by userspace.
1472 */
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001473 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 }
1475
1476 len = INITCONTEXTLEN;
Eric Paris4cb912f2009-02-12 14:50:05 -05001477 context = kmalloc(len+1, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 if (!context) {
1479 rc = -ENOMEM;
1480 dput(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001481 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 }
Eric Paris4cb912f2009-02-12 14:50:05 -05001483 context[len] = '\0';
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001484 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 if (rc == -ERANGE) {
James Morris314dabb2009-08-10 22:00:13 +10001486 kfree(context);
1487
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 /* Need a larger buffer. Query for the right size. */
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001489 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 if (rc < 0) {
1491 dput(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001492 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 len = rc;
Eric Paris4cb912f2009-02-12 14:50:05 -05001495 context = kmalloc(len+1, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 if (!context) {
1497 rc = -ENOMEM;
1498 dput(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001499 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 }
Eric Paris4cb912f2009-02-12 14:50:05 -05001501 context[len] = '\0';
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001502 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 }
1504 dput(dentry);
1505 if (rc < 0) {
1506 if (rc != -ENODATA) {
peter enderborgc103a912018-06-12 10:09:03 +02001507 pr_warn("SELinux: %s: getxattr returned "
Harvey Harrisondd6f9532008-03-06 10:03:59 +11001508 "%d for dev=%s ino=%ld\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 -rc, inode->i_sb->s_id, inode->i_ino);
1510 kfree(context);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001511 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 }
1513 /* Map ENODATA to the default file SID */
1514 sid = sbsec->def_sid;
1515 rc = 0;
1516 } else {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001517 rc = security_context_to_sid_default(&selinux_state,
1518 context, rc, &sid,
Stephen Smalley869ab512008-04-04 08:46:05 -04001519 sbsec->def_sid,
1520 GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 if (rc) {
Eric Paris4ba0a8a2009-02-12 15:01:10 -05001522 char *dev = inode->i_sb->s_id;
1523 unsigned long ino = inode->i_ino;
1524
1525 if (rc == -EINVAL) {
1526 if (printk_ratelimit())
peter enderborgc103a912018-06-12 10:09:03 +02001527 pr_notice("SELinux: inode=%lu on dev=%s was found to have an invalid "
Eric Paris4ba0a8a2009-02-12 15:01:10 -05001528 "context=%s. This indicates you may need to relabel the inode or the "
1529 "filesystem in question.\n", ino, dev, context);
1530 } else {
peter enderborgc103a912018-06-12 10:09:03 +02001531 pr_warn("SELinux: %s: context_to_sid(%s) "
Eric Paris4ba0a8a2009-02-12 15:01:10 -05001532 "returned %d for dev=%s ino=%ld\n",
1533 __func__, context, -rc, dev, ino);
1534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 kfree(context);
1536 /* Leave with the unlabeled SID */
1537 rc = 0;
1538 break;
1539 }
1540 }
1541 kfree(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 break;
1543 case SECURITY_FS_USE_TASK:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001544 sid = task_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 break;
1546 case SECURITY_FS_USE_TRANS:
1547 /* Default to the fs SID. */
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001548 sid = sbsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
1550 /* Try to obtain a transition SID. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001551 rc = security_transition_sid(&selinux_state, task_sid, sid,
1552 sclass, NULL, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 if (rc)
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001554 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 break;
Eric Parisc312feb2006-07-10 04:43:53 -07001556 case SECURITY_FS_USE_MNTPOINT:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001557 sid = sbsec->mntpoint_sid;
Eric Parisc312feb2006-07-10 04:43:53 -07001558 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 default:
Eric Parisc312feb2006-07-10 04:43:53 -07001560 /* Default to the fs superblock SID. */
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001561 sid = sbsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
Stephen Smalley134509d2015-06-04 16:22:17 -04001563 if ((sbsec->flags & SE_SBGENFS) && !S_ISLNK(inode->i_mode)) {
Paul Mooref64410e2014-03-19 16:46:18 -04001564 /* We must have a dentry to determine the label on
1565 * procfs inodes */
Al Virob1271252018-04-25 10:28:38 -04001566 if (opt_dentry) {
Paul Mooref64410e2014-03-19 16:46:18 -04001567 /* Called from d_instantiate or
1568 * d_splice_alias. */
1569 dentry = dget(opt_dentry);
Al Virob1271252018-04-25 10:28:38 -04001570 } else {
Paul Mooref64410e2014-03-19 16:46:18 -04001571 /* Called from selinux_complete_init, try to
Al Virob1271252018-04-25 10:28:38 -04001572 * find a dentry. Some filesystems really want
1573 * a connected one, so try that first.
1574 */
Paul Mooref64410e2014-03-19 16:46:18 -04001575 dentry = d_find_alias(inode);
Al Virob1271252018-04-25 10:28:38 -04001576 if (!dentry)
1577 dentry = d_find_any_alias(inode);
1578 }
Paul Mooref64410e2014-03-19 16:46:18 -04001579 /*
1580 * This can be hit on boot when a file is accessed
1581 * before the policy is loaded. When we load policy we
1582 * may find inodes that have no dentry on the
1583 * sbsec->isec_head list. No reason to complain as
1584 * these will get fixed up the next time we go through
1585 * inode_doinit() with a dentry, before these inodes
1586 * could be used again by userspace.
1587 */
1588 if (!dentry)
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001589 goto out;
1590 rc = selinux_genfs_get_sid(dentry, sclass,
Stephen Smalley134509d2015-06-04 16:22:17 -04001591 sbsec->flags, &sid);
Paul Mooref64410e2014-03-19 16:46:18 -04001592 dput(dentry);
1593 if (rc)
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001594 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 }
1596 break;
1597 }
1598
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001599out:
1600 spin_lock(&isec->lock);
1601 if (isec->initialized == LABEL_PENDING) {
1602 if (!sid || rc) {
1603 isec->initialized = LABEL_INVALID;
1604 goto out_unlock;
1605 }
1606
1607 isec->initialized = LABEL_INITIALIZED;
1608 isec->sid = sid;
1609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
Eric Paris23970742006-09-25 23:32:01 -07001611out_unlock:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001612 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 return rc;
1614}
1615
1616/* Convert a Linux signal to an access vector. */
1617static inline u32 signal_to_av(int sig)
1618{
1619 u32 perm = 0;
1620
1621 switch (sig) {
1622 case SIGCHLD:
1623 /* Commonly granted from child to parent. */
1624 perm = PROCESS__SIGCHLD;
1625 break;
1626 case SIGKILL:
1627 /* Cannot be caught or ignored */
1628 perm = PROCESS__SIGKILL;
1629 break;
1630 case SIGSTOP:
1631 /* Cannot be caught or ignored */
1632 perm = PROCESS__SIGSTOP;
1633 break;
1634 default:
1635 /* All other signals. */
1636 perm = PROCESS__SIGNAL;
1637 break;
1638 }
1639
1640 return perm;
1641}
1642
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001643#if CAP_LAST_CAP > 63
1644#error Fix SELinux to handle capabilities > 63.
1645#endif
1646
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647/* Check whether a task is allowed to use a capability. */
Eric Paris6a9de492012-01-03 12:25:14 -05001648static int cred_has_capability(const struct cred *cred,
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04001649 int cap, int audit, bool initns)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650{
Thomas Liu2bf49692009-07-14 12:14:09 -04001651 struct common_audit_data ad;
Eric Paris06112162008-11-11 22:02:50 +11001652 struct av_decision avd;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001653 u16 sclass;
David Howells3699c532009-01-06 22:27:01 +00001654 u32 sid = cred_sid(cred);
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001655 u32 av = CAP_TO_MASK(cap);
Eric Paris06112162008-11-11 22:02:50 +11001656 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
Eric Paris50c205f2012-04-04 15:01:43 -04001658 ad.type = LSM_AUDIT_DATA_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 ad.u.cap = cap;
1660
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001661 switch (CAP_TO_INDEX(cap)) {
1662 case 0:
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04001663 sclass = initns ? SECCLASS_CAPABILITY : SECCLASS_CAP_USERNS;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001664 break;
1665 case 1:
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04001666 sclass = initns ? SECCLASS_CAPABILITY2 : SECCLASS_CAP2_USERNS;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001667 break;
1668 default:
peter enderborgc103a912018-06-12 10:09:03 +02001669 pr_err("SELinux: out of range capability %d\n", cap);
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001670 BUG();
Eric Parisa35c6c832011-04-20 10:21:28 -04001671 return -EINVAL;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001672 }
Eric Paris06112162008-11-11 22:02:50 +11001673
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001674 rc = avc_has_perm_noaudit(&selinux_state,
1675 sid, sid, sclass, av, 0, &avd);
Eric Paris9ade0cf2011-04-25 16:26:29 -04001676 if (audit == SECURITY_CAP_AUDIT) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001677 int rc2 = avc_audit(&selinux_state,
1678 sid, sid, sclass, av, &avd, rc, &ad, 0);
Eric Paris9ade0cf2011-04-25 16:26:29 -04001679 if (rc2)
1680 return rc2;
1681 }
Eric Paris06112162008-11-11 22:02:50 +11001682 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683}
1684
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685/* Check whether a task has a particular permission to an inode.
1686 The 'adp' parameter is optional and allows other audit
1687 data to be passed (e.g. the dentry). */
David Howells88e67f32008-11-14 10:39:21 +11001688static int inode_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 struct inode *inode,
1690 u32 perms,
Linus Torvalds19e49832013-10-04 12:54:11 -07001691 struct common_audit_data *adp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 struct inode_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +11001694 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
David Howellse0e81732009-09-02 09:13:40 +01001696 validate_creds(cred);
1697
Eric Paris828dfe12008-04-17 13:17:49 -04001698 if (unlikely(IS_PRIVATE(inode)))
Stephen Smalleybbaca6c2007-02-14 00:34:16 -08001699 return 0;
1700
David Howells88e67f32008-11-14 10:39:21 +11001701 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 isec = inode->i_security;
1703
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001704 return avc_has_perm(&selinux_state,
1705 sid, isec->sid, isec->sclass, perms, adp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706}
1707
1708/* Same as inode_has_perm, but pass explicit audit data containing
1709 the dentry to help the auditing code to more easily generate the
1710 pathname if needed. */
David Howells88e67f32008-11-14 10:39:21 +11001711static inline int dentry_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 struct dentry *dentry,
1713 u32 av)
1714{
David Howellsc6f493d2015-03-17 22:26:22 +00001715 struct inode *inode = d_backing_inode(dentry);
Thomas Liu2bf49692009-07-14 12:14:09 -04001716 struct common_audit_data ad;
David Howells88e67f32008-11-14 10:39:21 +11001717
Eric Paris50c205f2012-04-04 15:01:43 -04001718 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Paris2875fa02011-04-28 16:04:24 -04001719 ad.u.dentry = dentry;
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05001720 __inode_security_revalidate(inode, dentry, true);
Linus Torvalds19e49832013-10-04 12:54:11 -07001721 return inode_has_perm(cred, inode, av, &ad);
Eric Paris2875fa02011-04-28 16:04:24 -04001722}
1723
1724/* Same as inode_has_perm, but pass explicit audit data containing
1725 the path to help the auditing code to more easily generate the
1726 pathname if needed. */
1727static inline int path_has_perm(const struct cred *cred,
Al Viro3f7036a2015-03-08 19:28:30 -04001728 const struct path *path,
Eric Paris2875fa02011-04-28 16:04:24 -04001729 u32 av)
1730{
David Howellsc6f493d2015-03-17 22:26:22 +00001731 struct inode *inode = d_backing_inode(path->dentry);
Eric Paris2875fa02011-04-28 16:04:24 -04001732 struct common_audit_data ad;
1733
Eric Paris50c205f2012-04-04 15:01:43 -04001734 ad.type = LSM_AUDIT_DATA_PATH;
Eric Paris2875fa02011-04-28 16:04:24 -04001735 ad.u.path = *path;
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05001736 __inode_security_revalidate(inode, path->dentry, true);
Linus Torvalds19e49832013-10-04 12:54:11 -07001737 return inode_has_perm(cred, inode, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738}
1739
David Howells13f8e982013-06-13 23:37:55 +01001740/* Same as path_has_perm, but uses the inode from the file struct. */
1741static inline int file_path_has_perm(const struct cred *cred,
1742 struct file *file,
1743 u32 av)
1744{
1745 struct common_audit_data ad;
1746
Vivek Goyal43af5de2016-09-09 11:37:49 -04001747 ad.type = LSM_AUDIT_DATA_FILE;
1748 ad.u.file = file;
Linus Torvalds19e49832013-10-04 12:54:11 -07001749 return inode_has_perm(cred, file_inode(file), av, &ad);
David Howells13f8e982013-06-13 23:37:55 +01001750}
1751
Chenbo Fengf66e4482017-10-18 13:00:26 -07001752#ifdef CONFIG_BPF_SYSCALL
1753static int bpf_fd_pass(struct file *file, u32 sid);
1754#endif
1755
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756/* Check whether a task can use an open file descriptor to
1757 access an inode in a given way. Check access to the
1758 descriptor itself, and then use dentry_has_perm to
1759 check a particular permission to the file.
1760 Access to the descriptor is implicitly granted if it
1761 has the same SID as the process. If av is zero, then
1762 access to the file is not checked, e.g. for cases
1763 where only the descriptor is affected like seek. */
David Howells88e67f32008-11-14 10:39:21 +11001764static int file_has_perm(const struct cred *cred,
1765 struct file *file,
1766 u32 av)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 struct file_security_struct *fsec = file->f_security;
Al Viro496ad9a2013-01-23 17:07:38 -05001769 struct inode *inode = file_inode(file);
Thomas Liu2bf49692009-07-14 12:14:09 -04001770 struct common_audit_data ad;
David Howells88e67f32008-11-14 10:39:21 +11001771 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 int rc;
1773
Vivek Goyal43af5de2016-09-09 11:37:49 -04001774 ad.type = LSM_AUDIT_DATA_FILE;
1775 ad.u.file = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
David Howells275bb412008-11-14 10:39:19 +11001777 if (sid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001778 rc = avc_has_perm(&selinux_state,
1779 sid, fsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 SECCLASS_FD,
1781 FD__USE,
1782 &ad);
1783 if (rc)
David Howells88e67f32008-11-14 10:39:21 +11001784 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 }
1786
Chenbo Fengf66e4482017-10-18 13:00:26 -07001787#ifdef CONFIG_BPF_SYSCALL
1788 rc = bpf_fd_pass(file, cred_sid(cred));
1789 if (rc)
1790 return rc;
1791#endif
1792
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 /* av is zero if only checking access to the descriptor. */
David Howells88e67f32008-11-14 10:39:21 +11001794 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 if (av)
Linus Torvalds19e49832013-10-04 12:54:11 -07001796 rc = inode_has_perm(cred, inode, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
David Howells88e67f32008-11-14 10:39:21 +11001798out:
1799 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800}
1801
David Howellsc3c188b2015-07-10 17:19:58 -04001802/*
1803 * Determine the label for an inode that might be unioned.
1804 */
Vivek Goyalc957f6d2016-07-13 10:44:51 -04001805static int
1806selinux_determine_inode_label(const struct task_security_struct *tsec,
1807 struct inode *dir,
1808 const struct qstr *name, u16 tclass,
1809 u32 *_new_isid)
David Howellsc3c188b2015-07-10 17:19:58 -04001810{
1811 const struct superblock_security_struct *sbsec = dir->i_sb->s_security;
David Howellsc3c188b2015-07-10 17:19:58 -04001812
1813 if ((sbsec->flags & SE_SBINITIALIZED) &&
1814 (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)) {
1815 *_new_isid = sbsec->mntpoint_sid;
1816 } else if ((sbsec->flags & SBLABEL_MNT) &&
1817 tsec->create_sid) {
1818 *_new_isid = tsec->create_sid;
1819 } else {
Paul Moore20cdef82016-04-04 14:14:42 -04001820 const struct inode_security_struct *dsec = inode_security(dir);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001821 return security_transition_sid(&selinux_state, tsec->sid,
1822 dsec->sid, tclass,
David Howellsc3c188b2015-07-10 17:19:58 -04001823 name, _new_isid);
1824 }
1825
1826 return 0;
1827}
1828
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829/* Check whether a task can create a file. */
1830static int may_create(struct inode *dir,
1831 struct dentry *dentry,
1832 u16 tclass)
1833{
Paul Moore5fb49872010-04-22 14:46:19 -04001834 const struct task_security_struct *tsec = current_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 struct inode_security_struct *dsec;
1836 struct superblock_security_struct *sbsec;
David Howells275bb412008-11-14 10:39:19 +11001837 u32 sid, newsid;
Thomas Liu2bf49692009-07-14 12:14:09 -04001838 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 int rc;
1840
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001841 dsec = inode_security(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 sbsec = dir->i_sb->s_security;
1843
David Howells275bb412008-11-14 10:39:19 +11001844 sid = tsec->sid;
David Howells275bb412008-11-14 10:39:19 +11001845
Eric Paris50c205f2012-04-04 15:01:43 -04001846 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04001847 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001849 rc = avc_has_perm(&selinux_state,
1850 sid, dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 DIR__ADD_NAME | DIR__SEARCH,
1852 &ad);
1853 if (rc)
1854 return rc;
1855
Vivek Goyalc957f6d2016-07-13 10:44:51 -04001856 rc = selinux_determine_inode_label(current_security(), dir,
1857 &dentry->d_name, tclass, &newsid);
David Howellsc3c188b2015-07-10 17:19:58 -04001858 if (rc)
1859 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001861 rc = avc_has_perm(&selinux_state,
1862 sid, newsid, tclass, FILE__CREATE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 if (rc)
1864 return rc;
1865
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001866 return avc_has_perm(&selinux_state,
1867 newsid, sbsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 SECCLASS_FILESYSTEM,
1869 FILESYSTEM__ASSOCIATE, &ad);
1870}
1871
Eric Paris828dfe12008-04-17 13:17:49 -04001872#define MAY_LINK 0
1873#define MAY_UNLINK 1
1874#define MAY_RMDIR 2
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
1876/* Check whether a task can link, unlink, or rmdir a file/directory. */
1877static int may_link(struct inode *dir,
1878 struct dentry *dentry,
1879 int kind)
1880
1881{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 struct inode_security_struct *dsec, *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001883 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11001884 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 u32 av;
1886 int rc;
1887
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001888 dsec = inode_security(dir);
1889 isec = backing_inode_security(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
Eric Paris50c205f2012-04-04 15:01:43 -04001891 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04001892 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
1894 av = DIR__SEARCH;
1895 av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001896 rc = avc_has_perm(&selinux_state,
1897 sid, dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 if (rc)
1899 return rc;
1900
1901 switch (kind) {
1902 case MAY_LINK:
1903 av = FILE__LINK;
1904 break;
1905 case MAY_UNLINK:
1906 av = FILE__UNLINK;
1907 break;
1908 case MAY_RMDIR:
1909 av = DIR__RMDIR;
1910 break;
1911 default:
peter enderborgc103a912018-06-12 10:09:03 +02001912 pr_warn("SELinux: %s: unrecognized kind %d\n",
Eric Paris744ba352008-04-17 11:52:44 -04001913 __func__, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 return 0;
1915 }
1916
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001917 rc = avc_has_perm(&selinux_state,
1918 sid, isec->sid, isec->sclass, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 return rc;
1920}
1921
1922static inline int may_rename(struct inode *old_dir,
1923 struct dentry *old_dentry,
1924 struct inode *new_dir,
1925 struct dentry *new_dentry)
1926{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001928 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11001929 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 u32 av;
1931 int old_is_dir, new_is_dir;
1932 int rc;
1933
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001934 old_dsec = inode_security(old_dir);
1935 old_isec = backing_inode_security(old_dentry);
David Howellse36cb0b2015-01-29 12:02:35 +00001936 old_is_dir = d_is_dir(old_dentry);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001937 new_dsec = inode_security(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
Eric Paris50c205f2012-04-04 15:01:43 -04001939 ad.type = LSM_AUDIT_DATA_DENTRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
Eric Parisa2694342011-04-25 13:10:27 -04001941 ad.u.dentry = old_dentry;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001942 rc = avc_has_perm(&selinux_state,
1943 sid, old_dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 DIR__REMOVE_NAME | DIR__SEARCH, &ad);
1945 if (rc)
1946 return rc;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001947 rc = avc_has_perm(&selinux_state,
1948 sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 old_isec->sclass, FILE__RENAME, &ad);
1950 if (rc)
1951 return rc;
1952 if (old_is_dir && new_dir != old_dir) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001953 rc = avc_has_perm(&selinux_state,
1954 sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 old_isec->sclass, DIR__REPARENT, &ad);
1956 if (rc)
1957 return rc;
1958 }
1959
Eric Parisa2694342011-04-25 13:10:27 -04001960 ad.u.dentry = new_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 av = DIR__ADD_NAME | DIR__SEARCH;
David Howells2c616d42015-01-29 12:02:33 +00001962 if (d_is_positive(new_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 av |= DIR__REMOVE_NAME;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001964 rc = avc_has_perm(&selinux_state,
1965 sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 if (rc)
1967 return rc;
David Howells2c616d42015-01-29 12:02:33 +00001968 if (d_is_positive(new_dentry)) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001969 new_isec = backing_inode_security(new_dentry);
David Howellse36cb0b2015-01-29 12:02:35 +00001970 new_is_dir = d_is_dir(new_dentry);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001971 rc = avc_has_perm(&selinux_state,
1972 sid, new_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 new_isec->sclass,
1974 (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
1975 if (rc)
1976 return rc;
1977 }
1978
1979 return 0;
1980}
1981
1982/* Check whether a task can perform a filesystem operation. */
David Howells88e67f32008-11-14 10:39:21 +11001983static int superblock_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 struct super_block *sb,
1985 u32 perms,
Thomas Liu2bf49692009-07-14 12:14:09 -04001986 struct common_audit_data *ad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 struct superblock_security_struct *sbsec;
David Howells88e67f32008-11-14 10:39:21 +11001989 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 sbsec = sb->s_security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001992 return avc_has_perm(&selinux_state,
1993 sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994}
1995
1996/* Convert a Linux mode and permission mask to an access vector. */
1997static inline u32 file_mask_to_av(int mode, int mask)
1998{
1999 u32 av = 0;
2000
Al Virodba19c62011-07-25 20:49:29 -04002001 if (!S_ISDIR(mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 if (mask & MAY_EXEC)
2003 av |= FILE__EXECUTE;
2004 if (mask & MAY_READ)
2005 av |= FILE__READ;
2006
2007 if (mask & MAY_APPEND)
2008 av |= FILE__APPEND;
2009 else if (mask & MAY_WRITE)
2010 av |= FILE__WRITE;
2011
2012 } else {
2013 if (mask & MAY_EXEC)
2014 av |= DIR__SEARCH;
2015 if (mask & MAY_WRITE)
2016 av |= DIR__WRITE;
2017 if (mask & MAY_READ)
2018 av |= DIR__READ;
2019 }
2020
2021 return av;
2022}
2023
2024/* Convert a Linux file to an access vector. */
2025static inline u32 file_to_av(struct file *file)
2026{
2027 u32 av = 0;
2028
2029 if (file->f_mode & FMODE_READ)
2030 av |= FILE__READ;
2031 if (file->f_mode & FMODE_WRITE) {
2032 if (file->f_flags & O_APPEND)
2033 av |= FILE__APPEND;
2034 else
2035 av |= FILE__WRITE;
2036 }
Stephen Smalley0794c662008-03-17 08:55:18 -04002037 if (!av) {
2038 /*
2039 * Special file opened with flags 3 for ioctl-only use.
2040 */
2041 av = FILE__IOCTL;
2042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
2044 return av;
2045}
2046
Eric Paris8b6a5a32008-10-29 17:06:46 -04002047/*
2048 * Convert a file to an access vector and include the correct open
2049 * open permission.
2050 */
2051static inline u32 open_file_to_av(struct file *file)
2052{
2053 u32 av = file_to_av(file);
Stephen Smalleyccb54472017-05-12 12:41:24 -04002054 struct inode *inode = file_inode(file);
Eric Paris8b6a5a32008-10-29 17:06:46 -04002055
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002056 if (selinux_policycap_openperm() &&
2057 inode->i_sb->s_magic != SOCKFS_MAGIC)
Eric Paris49b7b8d2010-07-23 11:44:09 -04002058 av |= FILE__OPEN;
2059
Eric Paris8b6a5a32008-10-29 17:06:46 -04002060 return av;
2061}
2062
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063/* Hook functions begin here. */
2064
Stephen Smalley79af7302015-01-21 10:54:10 -05002065static int selinux_binder_set_context_mgr(struct task_struct *mgr)
2066{
2067 u32 mysid = current_sid();
2068 u32 mgrsid = task_sid(mgr);
2069
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002070 return avc_has_perm(&selinux_state,
2071 mysid, mgrsid, SECCLASS_BINDER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002072 BINDER__SET_CONTEXT_MGR, NULL);
2073}
2074
2075static int selinux_binder_transaction(struct task_struct *from,
2076 struct task_struct *to)
2077{
2078 u32 mysid = current_sid();
2079 u32 fromsid = task_sid(from);
2080 u32 tosid = task_sid(to);
2081 int rc;
2082
2083 if (mysid != fromsid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002084 rc = avc_has_perm(&selinux_state,
2085 mysid, fromsid, SECCLASS_BINDER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002086 BINDER__IMPERSONATE, NULL);
2087 if (rc)
2088 return rc;
2089 }
2090
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002091 return avc_has_perm(&selinux_state,
2092 fromsid, tosid, SECCLASS_BINDER, BINDER__CALL,
Stephen Smalley79af7302015-01-21 10:54:10 -05002093 NULL);
2094}
2095
2096static int selinux_binder_transfer_binder(struct task_struct *from,
2097 struct task_struct *to)
2098{
2099 u32 fromsid = task_sid(from);
2100 u32 tosid = task_sid(to);
2101
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002102 return avc_has_perm(&selinux_state,
2103 fromsid, tosid, SECCLASS_BINDER, BINDER__TRANSFER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002104 NULL);
2105}
2106
2107static int selinux_binder_transfer_file(struct task_struct *from,
2108 struct task_struct *to,
2109 struct file *file)
2110{
2111 u32 sid = task_sid(to);
2112 struct file_security_struct *fsec = file->f_security;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002113 struct dentry *dentry = file->f_path.dentry;
Paul Moore20cdef82016-04-04 14:14:42 -04002114 struct inode_security_struct *isec;
Stephen Smalley79af7302015-01-21 10:54:10 -05002115 struct common_audit_data ad;
2116 int rc;
2117
2118 ad.type = LSM_AUDIT_DATA_PATH;
2119 ad.u.path = file->f_path;
2120
2121 if (sid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002122 rc = avc_has_perm(&selinux_state,
2123 sid, fsec->sid,
Stephen Smalley79af7302015-01-21 10:54:10 -05002124 SECCLASS_FD,
2125 FD__USE,
2126 &ad);
2127 if (rc)
2128 return rc;
2129 }
2130
Chenbo Fengf66e4482017-10-18 13:00:26 -07002131#ifdef CONFIG_BPF_SYSCALL
2132 rc = bpf_fd_pass(file, sid);
2133 if (rc)
2134 return rc;
2135#endif
2136
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002137 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
Stephen Smalley79af7302015-01-21 10:54:10 -05002138 return 0;
2139
Paul Moore20cdef82016-04-04 14:14:42 -04002140 isec = backing_inode_security(dentry);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002141 return avc_has_perm(&selinux_state,
2142 sid, isec->sid, isec->sclass, file_to_av(file),
Stephen Smalley79af7302015-01-21 10:54:10 -05002143 &ad);
2144}
2145
Ingo Molnar9e488582009-05-07 19:26:19 +10002146static int selinux_ptrace_access_check(struct task_struct *child,
David Howells5cd9c582008-08-14 11:37:28 +01002147 unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002149 u32 sid = current_sid();
2150 u32 csid = task_sid(child);
Stephen Smalley006ebb42008-05-19 08:32:49 -04002151
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002152 if (mode & PTRACE_MODE_READ)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002153 return avc_has_perm(&selinux_state,
2154 sid, csid, SECCLASS_FILE, FILE__READ, NULL);
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002155
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002156 return avc_has_perm(&selinux_state,
2157 sid, csid, SECCLASS_PROCESS, PROCESS__PTRACE, NULL);
David Howells5cd9c582008-08-14 11:37:28 +01002158}
2159
2160static int selinux_ptrace_traceme(struct task_struct *parent)
2161{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002162 return avc_has_perm(&selinux_state,
2163 task_sid(parent), current_sid(), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002164 PROCESS__PTRACE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165}
2166
2167static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
Eric Paris828dfe12008-04-17 13:17:49 -04002168 kernel_cap_t *inheritable, kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002170 return avc_has_perm(&selinux_state,
2171 current_sid(), task_sid(target), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002172 PROCESS__GETCAP, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173}
2174
David Howellsd84f4f92008-11-14 10:39:23 +11002175static int selinux_capset(struct cred *new, const struct cred *old,
2176 const kernel_cap_t *effective,
2177 const kernel_cap_t *inheritable,
2178 const kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002180 return avc_has_perm(&selinux_state,
2181 cred_sid(old), cred_sid(new), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002182 PROCESS__SETCAP, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183}
2184
James Morris5626d3e2009-01-30 10:05:06 +11002185/*
2186 * (This comment used to live with the selinux_task_setuid hook,
2187 * which was removed).
2188 *
2189 * Since setuid only affects the current process, and since the SELinux
2190 * controls are not based on the Linux identity attributes, SELinux does not
2191 * need to control this operation. However, SELinux does control the use of
2192 * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
2193 */
2194
Eric Paris6a9de492012-01-03 12:25:14 -05002195static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
2196 int cap, int audit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197{
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04002198 return cred_has_capability(cred, cap, audit, ns == &init_user_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199}
2200
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
2202{
David Howells88e67f32008-11-14 10:39:21 +11002203 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 int rc = 0;
2205
2206 if (!sb)
2207 return 0;
2208
2209 switch (cmds) {
Eric Paris828dfe12008-04-17 13:17:49 -04002210 case Q_SYNC:
2211 case Q_QUOTAON:
2212 case Q_QUOTAOFF:
2213 case Q_SETINFO:
2214 case Q_SETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11002215 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04002216 break;
2217 case Q_GETFMT:
2218 case Q_GETINFO:
2219 case Q_GETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11002220 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04002221 break;
2222 default:
2223 rc = 0; /* let the kernel handle invalid cmds */
2224 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 }
2226 return rc;
2227}
2228
2229static int selinux_quota_on(struct dentry *dentry)
2230{
David Howells88e67f32008-11-14 10:39:21 +11002231 const struct cred *cred = current_cred();
2232
Eric Paris2875fa02011-04-28 16:04:24 -04002233 return dentry_has_perm(cred, dentry, FILE__QUOTAON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234}
2235
Eric Paris12b30522010-11-15 18:36:29 -05002236static int selinux_syslog(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 switch (type) {
Kees Cookd78ca3c2010-02-03 15:37:13 -08002239 case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */
2240 case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002241 return avc_has_perm(&selinux_state,
2242 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002243 SECCLASS_SYSTEM, SYSTEM__SYSLOG_READ, NULL);
Kees Cookd78ca3c2010-02-03 15:37:13 -08002244 case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */
2245 case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */
2246 /* Set level of messages printed to console */
2247 case SYSLOG_ACTION_CONSOLE_LEVEL:
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002248 return avc_has_perm(&selinux_state,
2249 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002250 SECCLASS_SYSTEM, SYSTEM__SYSLOG_CONSOLE,
2251 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 }
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002253 /* All other syslog types */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002254 return avc_has_perm(&selinux_state,
2255 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002256 SECCLASS_SYSTEM, SYSTEM__SYSLOG_MOD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257}
2258
2259/*
2260 * Check that a process has enough memory to allocate a new virtual
2261 * mapping. 0 means there is enough memory for the allocation to
2262 * succeed and -ENOMEM implies there is not.
2263 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 * Do not audit the selinux permission check, as this is applied to all
2265 * processes that allocate mappings.
2266 */
Alan Cox34b4e4a2007-08-22 14:01:28 -07002267static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268{
2269 int rc, cap_sys_admin = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002271 rc = cred_has_capability(current_cred(), CAP_SYS_ADMIN,
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04002272 SECURITY_CAP_NOAUDIT, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 if (rc == 0)
2274 cap_sys_admin = 1;
2275
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002276 return cap_sys_admin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277}
2278
2279/* binprm security operations */
2280
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002281static u32 ptrace_parent_sid(void)
Paul Moore0c6181c2016-03-30 21:41:21 -04002282{
2283 u32 sid = 0;
2284 struct task_struct *tracer;
2285
2286 rcu_read_lock();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002287 tracer = ptrace_parent(current);
Paul Moore0c6181c2016-03-30 21:41:21 -04002288 if (tracer)
2289 sid = task_sid(tracer);
2290 rcu_read_unlock();
2291
2292 return sid;
2293}
2294
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002295static int check_nnp_nosuid(const struct linux_binprm *bprm,
2296 const struct task_security_struct *old_tsec,
2297 const struct task_security_struct *new_tsec)
2298{
2299 int nnp = (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS);
Andy Lutomirski380cf5b2016-06-23 16:41:05 -05002300 int nosuid = !mnt_may_suid(bprm->file->f_path.mnt);
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002301 int rc;
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002302 u32 av;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002303
2304 if (!nnp && !nosuid)
2305 return 0; /* neither NNP nor nosuid */
2306
2307 if (new_tsec->sid == old_tsec->sid)
2308 return 0; /* No change in credentials */
2309
2310 /*
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002311 * If the policy enables the nnp_nosuid_transition policy capability,
2312 * then we permit transitions under NNP or nosuid if the
2313 * policy allows the corresponding permission between
2314 * the old and new contexts.
2315 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002316 if (selinux_policycap_nnp_nosuid_transition()) {
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002317 av = 0;
2318 if (nnp)
2319 av |= PROCESS2__NNP_TRANSITION;
2320 if (nosuid)
2321 av |= PROCESS2__NOSUID_TRANSITION;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002322 rc = avc_has_perm(&selinux_state,
2323 old_tsec->sid, new_tsec->sid,
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002324 SECCLASS_PROCESS2, av, NULL);
2325 if (!rc)
2326 return 0;
2327 }
2328
2329 /*
2330 * We also permit NNP or nosuid transitions to bounded SIDs,
2331 * i.e. SIDs that are guaranteed to only be allowed a subset
2332 * of the permissions of the current SID.
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002333 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002334 rc = security_bounded_transition(&selinux_state, old_tsec->sid,
2335 new_tsec->sid);
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002336 if (!rc)
2337 return 0;
2338
2339 /*
2340 * On failure, preserve the errno values for NNP vs nosuid.
2341 * NNP: Operation not permitted for caller.
2342 * nosuid: Permission denied to file.
2343 */
2344 if (nnp)
2345 return -EPERM;
2346 return -EACCES;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002347}
2348
David Howellsa6f76f22008-11-14 10:39:24 +11002349static int selinux_bprm_set_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350{
David Howellsa6f76f22008-11-14 10:39:24 +11002351 const struct task_security_struct *old_tsec;
2352 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 struct inode_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04002354 struct common_audit_data ad;
Al Viro496ad9a2013-01-23 17:07:38 -05002355 struct inode *inode = file_inode(bprm->file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 int rc;
2357
David Howellsa6f76f22008-11-14 10:39:24 +11002358 /* SELinux context only depends on initial program or script and not
2359 * the script interpreter */
Kees Cookddb4a142017-07-18 15:25:23 -07002360 if (bprm->called_set_creds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 return 0;
2362
David Howellsa6f76f22008-11-14 10:39:24 +11002363 old_tsec = current_security();
2364 new_tsec = bprm->cred->security;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002365 isec = inode_security(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
2367 /* Default to the current task SID. */
David Howellsa6f76f22008-11-14 10:39:24 +11002368 new_tsec->sid = old_tsec->sid;
2369 new_tsec->osid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
Michael LeMay28eba5b2006-06-27 02:53:42 -07002371 /* Reset fs, key, and sock SIDs on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002372 new_tsec->create_sid = 0;
2373 new_tsec->keycreate_sid = 0;
2374 new_tsec->sockcreate_sid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
David Howellsa6f76f22008-11-14 10:39:24 +11002376 if (old_tsec->exec_sid) {
2377 new_tsec->sid = old_tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 /* Reset exec SID on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002379 new_tsec->exec_sid = 0;
Andy Lutomirski259e5e62012-04-12 16:47:50 -05002380
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002381 /* Fail on NNP or nosuid if not an allowed transition. */
2382 rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
2383 if (rc)
2384 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 } else {
2386 /* Check for a default transition on this program. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002387 rc = security_transition_sid(&selinux_state, old_tsec->sid,
2388 isec->sid, SECCLASS_PROCESS, NULL,
Eric Paris652bb9b2011-02-01 11:05:40 -05002389 &new_tsec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 if (rc)
2391 return rc;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002392
2393 /*
2394 * Fallback to old SID on NNP or nosuid if not an allowed
2395 * transition.
2396 */
2397 rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
2398 if (rc)
2399 new_tsec->sid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 }
2401
Vivek Goyal43af5de2016-09-09 11:37:49 -04002402 ad.type = LSM_AUDIT_DATA_FILE;
2403 ad.u.file = bprm->file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
David Howellsa6f76f22008-11-14 10:39:24 +11002405 if (new_tsec->sid == old_tsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002406 rc = avc_has_perm(&selinux_state,
2407 old_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
2409 if (rc)
2410 return rc;
2411 } else {
2412 /* Check permissions for the transition. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002413 rc = avc_has_perm(&selinux_state,
2414 old_tsec->sid, new_tsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
2416 if (rc)
2417 return rc;
2418
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002419 rc = avc_has_perm(&selinux_state,
2420 new_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
2422 if (rc)
2423 return rc;
2424
David Howellsa6f76f22008-11-14 10:39:24 +11002425 /* Check for shared state */
2426 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002427 rc = avc_has_perm(&selinux_state,
2428 old_tsec->sid, new_tsec->sid,
David Howellsa6f76f22008-11-14 10:39:24 +11002429 SECCLASS_PROCESS, PROCESS__SHARE,
2430 NULL);
2431 if (rc)
2432 return -EPERM;
2433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434
David Howellsa6f76f22008-11-14 10:39:24 +11002435 /* Make sure that anyone attempting to ptrace over a task that
2436 * changes its SID has the appropriate permit */
Eric W. Biederman9227dd22017-01-23 17:26:31 +13002437 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002438 u32 ptsid = ptrace_parent_sid();
David Howellsa6f76f22008-11-14 10:39:24 +11002439 if (ptsid != 0) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002440 rc = avc_has_perm(&selinux_state,
2441 ptsid, new_tsec->sid,
David Howellsa6f76f22008-11-14 10:39:24 +11002442 SECCLASS_PROCESS,
2443 PROCESS__PTRACE, NULL);
2444 if (rc)
2445 return -EPERM;
2446 }
2447 }
2448
2449 /* Clear any possibly unsafe personality bits on exec: */
2450 bprm->per_clear |= PER_CLEAR_ON_SETID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 /* Enable secure mode for SIDs transitions unless
2453 the noatsecure permission is granted between
2454 the two SIDs, i.e. ahp returns 0. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002455 rc = avc_has_perm(&selinux_state,
2456 old_tsec->sid, new_tsec->sid,
Kees Cook62874c32017-07-18 15:25:25 -07002457 SECCLASS_PROCESS, PROCESS__NOATSECURE,
2458 NULL);
2459 bprm->secureexec |= !!rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 }
2461
Kees Cook62874c32017-07-18 15:25:25 -07002462 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463}
2464
Al Viroc3c073f2012-08-21 22:32:06 -04002465static int match_file(const void *p, struct file *file, unsigned fd)
2466{
2467 return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0;
2468}
2469
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470/* Derived from fs/exec.c:flush_old_files. */
David Howells745ca242008-11-14 10:39:22 +11002471static inline void flush_unauthorized_files(const struct cred *cred,
2472 struct files_struct *files)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 struct file *file, *devnull = NULL;
Stephen Smalleyb20c8122006-09-25 23:32:03 -07002475 struct tty_struct *tty;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002476 int drop_tty = 0;
Al Viroc3c073f2012-08-21 22:32:06 -04002477 unsigned n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002479 tty = get_current_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 if (tty) {
Peter Hurley4a510962016-01-09 21:35:23 -08002481 spin_lock(&tty->files_lock);
Eric Paris37dd0bd2008-10-31 17:40:00 -04002482 if (!list_empty(&tty->tty_files)) {
Nick Piggind996b622010-08-18 04:37:36 +10002483 struct tty_file_private *file_priv;
Eric Paris37dd0bd2008-10-31 17:40:00 -04002484
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 /* Revalidate access to controlling tty.
David Howells13f8e982013-06-13 23:37:55 +01002486 Use file_path_has_perm on the tty path directly
2487 rather than using file_has_perm, as this particular
2488 open file may belong to another process and we are
2489 only interested in the inode-based check here. */
Nick Piggind996b622010-08-18 04:37:36 +10002490 file_priv = list_first_entry(&tty->tty_files,
2491 struct tty_file_private, list);
2492 file = file_priv->file;
David Howells13f8e982013-06-13 23:37:55 +01002493 if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE))
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002494 drop_tty = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 }
Peter Hurley4a510962016-01-09 21:35:23 -08002496 spin_unlock(&tty->files_lock);
Alan Cox452a00d2008-10-13 10:39:13 +01002497 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 }
Eric W. Biederman98a27ba2007-05-08 00:26:56 -07002499 /* Reset controlling tty. */
2500 if (drop_tty)
2501 no_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502
2503 /* Revalidate access to inherited open files. */
Al Viroc3c073f2012-08-21 22:32:06 -04002504 n = iterate_fd(files, 0, match_file, cred);
2505 if (!n) /* none found? */
2506 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507
Al Viroc3c073f2012-08-21 22:32:06 -04002508 devnull = dentry_open(&selinux_null, O_RDWR, cred);
Al Viro45525b22012-10-16 13:30:07 -04002509 if (IS_ERR(devnull))
2510 devnull = NULL;
2511 /* replace all the matching ones with this */
2512 do {
2513 replace_fd(n - 1, devnull, 0);
2514 } while ((n = iterate_fd(files, n, match_file, cred)) != 0);
2515 if (devnull)
Al Viroc3c073f2012-08-21 22:32:06 -04002516 fput(devnull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517}
2518
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519/*
David Howellsa6f76f22008-11-14 10:39:24 +11002520 * Prepare a process for imminent new credential changes due to exec
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 */
David Howellsa6f76f22008-11-14 10:39:24 +11002522static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523{
David Howellsa6f76f22008-11-14 10:39:24 +11002524 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 struct rlimit *rlim, *initrlim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 int rc, i;
2527
David Howellsa6f76f22008-11-14 10:39:24 +11002528 new_tsec = bprm->cred->security;
2529 if (new_tsec->sid == new_tsec->osid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 return;
2531
2532 /* Close files for which the new task SID is not authorized. */
David Howellsa6f76f22008-11-14 10:39:24 +11002533 flush_unauthorized_files(bprm->cred, current->files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534
David Howellsa6f76f22008-11-14 10:39:24 +11002535 /* Always clear parent death signal on SID transitions. */
2536 current->pdeath_signal = 0;
2537
2538 /* Check whether the new SID can inherit resource limits from the old
2539 * SID. If not, reset all soft limits to the lower of the current
2540 * task's hard limit and the init task's soft limit.
2541 *
2542 * Note that the setting of hard limits (even to lower them) can be
2543 * controlled by the setrlimit check. The inclusion of the init task's
2544 * soft limit into the computation is to avoid resetting soft limits
2545 * higher than the default soft limit for cases where the default is
2546 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2547 */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002548 rc = avc_has_perm(&selinux_state,
2549 new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
David Howellsa6f76f22008-11-14 10:39:24 +11002550 PROCESS__RLIMITINH, NULL);
2551 if (rc) {
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002552 /* protect against do_prlimit() */
2553 task_lock(current);
David Howellsa6f76f22008-11-14 10:39:24 +11002554 for (i = 0; i < RLIM_NLIMITS; i++) {
2555 rlim = current->signal->rlim + i;
2556 initrlim = init_task.signal->rlim + i;
2557 rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
2558 }
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002559 task_unlock(current);
Nicolas Pitrebaa73d92016-11-11 00:10:10 -05002560 if (IS_ENABLED(CONFIG_POSIX_TIMERS))
2561 update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
David Howellsa6f76f22008-11-14 10:39:24 +11002562 }
2563}
2564
2565/*
2566 * Clean up the process immediately after the installation of new credentials
2567 * due to exec
2568 */
2569static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
2570{
2571 const struct task_security_struct *tsec = current_security();
2572 struct itimerval itimer;
David Howellsa6f76f22008-11-14 10:39:24 +11002573 u32 osid, sid;
2574 int rc, i;
David Howellsa6f76f22008-11-14 10:39:24 +11002575
David Howellsa6f76f22008-11-14 10:39:24 +11002576 osid = tsec->osid;
2577 sid = tsec->sid;
2578
2579 if (sid == osid)
2580 return;
2581
2582 /* Check whether the new SID can inherit signal state from the old SID.
2583 * If not, clear itimers to avoid subsequent signal generation and
2584 * flush and unblock signals.
2585 *
2586 * This must occur _after_ the task SID has been updated so that any
2587 * kill done after the flush will be checked against the new SID.
2588 */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002589 rc = avc_has_perm(&selinux_state,
2590 osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 if (rc) {
Nicolas Pitrebaa73d92016-11-11 00:10:10 -05002592 if (IS_ENABLED(CONFIG_POSIX_TIMERS)) {
2593 memset(&itimer, 0, sizeof itimer);
2594 for (i = 0; i < 3; i++)
2595 do_setitimer(i, &itimer, NULL);
2596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterov9e7c8f82015-06-04 16:22:16 -04002598 if (!fatal_signal_pending(current)) {
2599 flush_sigqueue(&current->pending);
2600 flush_sigqueue(&current->signal->shared_pending);
David Howells3bcac022009-04-29 13:45:05 +01002601 flush_signal_handlers(current, 1);
2602 sigemptyset(&current->blocked);
Oleg Nesterov9e7c8f82015-06-04 16:22:16 -04002603 recalc_sigpending();
David Howells3bcac022009-04-29 13:45:05 +01002604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 spin_unlock_irq(&current->sighand->siglock);
2606 }
2607
David Howellsa6f76f22008-11-14 10:39:24 +11002608 /* Wake up the parent if it is waiting so that it can recheck
2609 * wait permission to the new task SID. */
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002610 read_lock(&tasklist_lock);
Oleg Nesterov0b7570e2009-09-23 15:56:46 -07002611 __wake_up_parent(current, current->real_parent);
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002612 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613}
2614
2615/* superblock security operations */
2616
2617static int selinux_sb_alloc_security(struct super_block *sb)
2618{
2619 return superblock_alloc_security(sb);
2620}
2621
2622static void selinux_sb_free_security(struct super_block *sb)
2623{
2624 superblock_free_security(sb);
2625}
2626
Al Viro99dbbb52018-12-14 21:56:23 -05002627static inline int opt_len(const char *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628{
Al Viro99dbbb52018-12-14 21:56:23 -05002629 bool open_quote = false;
2630 int len;
2631 char c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632
Al Viro99dbbb52018-12-14 21:56:23 -05002633 for (len = 0; (c = s[len]) != '\0'; len++) {
2634 if (c == '"')
Cory Olmo3528a952006-09-29 01:58:44 -07002635 open_quote = !open_quote;
Al Viro99dbbb52018-12-14 21:56:23 -05002636 if (c == ',' && !open_quote)
2637 break;
2638 }
2639 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640}
2641
Al Viro204cc0c2018-12-13 13:41:47 -05002642static int selinux_sb_eat_lsm_opts(char *options, void **mnt_opts)
Eric Paris026eb162011-03-03 16:09:14 -05002643{
Al Viro99dbbb52018-12-14 21:56:23 -05002644 char *from = options;
2645 char *to = options;
2646 bool first = true;
Al Viro5b400232018-12-12 20:13:29 -05002647
Al Viro99dbbb52018-12-14 21:56:23 -05002648 while (1) {
2649 int len = opt_len(from);
2650 int token, rc;
2651 char *arg = NULL;
2652
2653 token = match_opt_prefix(from, len, &arg);
2654
2655 if (token != Opt_error) {
2656 char *p, *q;
2657
2658 /* strip quotes */
2659 if (arg) {
2660 for (p = q = arg; p < from + len; p++) {
2661 char c = *p;
2662 if (c != '"')
2663 *q++ = c;
2664 }
2665 arg = kmemdup_nul(arg, q - arg, GFP_KERNEL);
2666 }
2667 rc = selinux_add_opt(token, arg, mnt_opts);
2668 if (unlikely(rc)) {
2669 kfree(arg);
2670 if (*mnt_opts) {
2671 selinux_free_mnt_opts(*mnt_opts);
2672 *mnt_opts = NULL;
2673 }
2674 return rc;
2675 }
2676 } else {
2677 if (!first) { // copy with preceding comma
2678 from--;
2679 len++;
2680 }
2681 if (to != from)
2682 memmove(to, from, len);
2683 to += len;
2684 first = false;
2685 }
2686 if (!from[len])
2687 break;
2688 from += len + 1;
2689 }
2690 *to = '\0';
2691 return 0;
Al Viro5b400232018-12-12 20:13:29 -05002692}
2693
Al Viro204cc0c2018-12-13 13:41:47 -05002694static int selinux_sb_remount(struct super_block *sb, void *mnt_opts)
Eric Paris026eb162011-03-03 16:09:14 -05002695{
Al Virobd323652018-12-13 15:04:59 -05002696 struct selinux_mnt_opts *opts = mnt_opts;
Eric Paris026eb162011-03-03 16:09:14 -05002697 struct superblock_security_struct *sbsec = sb->s_security;
Al Virobd323652018-12-13 15:04:59 -05002698 u32 sid;
2699 int rc;
Eric Paris026eb162011-03-03 16:09:14 -05002700
2701 if (!(sbsec->flags & SE_SBINITIALIZED))
2702 return 0;
2703
Al Viro204cc0c2018-12-13 13:41:47 -05002704 if (!opts)
Eric Paris026eb162011-03-03 16:09:14 -05002705 return 0;
2706
Al Virobd323652018-12-13 15:04:59 -05002707 if (opts->fscontext) {
2708 rc = parse_sid(sb, opts->fscontext, &sid);
2709 if (rc)
Al Viroc039bc32018-12-01 23:06:57 -05002710 return rc;
Al Virobd323652018-12-13 15:04:59 -05002711 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
2712 goto out_bad_option;
Eric Paris026eb162011-03-03 16:09:14 -05002713 }
Al Virobd323652018-12-13 15:04:59 -05002714 if (opts->context) {
2715 rc = parse_sid(sb, opts->context, &sid);
2716 if (rc)
2717 return rc;
2718 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
2719 goto out_bad_option;
2720 }
2721 if (opts->rootcontext) {
2722 struct inode_security_struct *root_isec;
2723 root_isec = backing_inode_security(sb->s_root);
2724 rc = parse_sid(sb, opts->rootcontext, &sid);
2725 if (rc)
2726 return rc;
2727 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
2728 goto out_bad_option;
2729 }
2730 if (opts->defcontext) {
2731 rc = parse_sid(sb, opts->defcontext, &sid);
2732 if (rc)
2733 return rc;
2734 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
2735 goto out_bad_option;
Eric Paris026eb162011-03-03 16:09:14 -05002736 }
Al Viroc039bc32018-12-01 23:06:57 -05002737 return 0;
Eric Paris026eb162011-03-03 16:09:14 -05002738
Eric Paris026eb162011-03-03 16:09:14 -05002739out_bad_option:
peter enderborgc103a912018-06-12 10:09:03 +02002740 pr_warn("SELinux: unable to change security options "
Linus Torvalds29b1deb2013-12-15 11:17:45 -08002741 "during remount (dev %s, type=%s)\n", sb->s_id,
2742 sb->s_type->name);
Al Viroc039bc32018-12-01 23:06:57 -05002743 return -EINVAL;
Eric Paris026eb162011-03-03 16:09:14 -05002744}
2745
Al Viroa10d7c22018-12-05 11:58:35 -05002746static int selinux_sb_kern_mount(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747{
David Howells88e67f32008-11-14 10:39:21 +11002748 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002749 struct common_audit_data ad;
James Morris74192242008-12-19 11:41:10 +11002750
Eric Paris50c205f2012-04-04 15:01:43 -04002751 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002752 ad.u.dentry = sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002753 return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754}
2755
David Howells726c3342006-06-23 02:02:58 -07002756static int selinux_sb_statfs(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757{
David Howells88e67f32008-11-14 10:39:21 +11002758 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002759 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760
Eric Paris50c205f2012-04-04 15:01:43 -04002761 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002762 ad.u.dentry = dentry->d_sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002763 return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764}
2765
Al Viro808d4e32012-10-11 11:42:01 -04002766static int selinux_mount(const char *dev_name,
Al Viro8a04c432016-03-25 14:52:53 -04002767 const struct path *path,
Al Viro808d4e32012-10-11 11:42:01 -04002768 const char *type,
Eric Paris828dfe12008-04-17 13:17:49 -04002769 unsigned long flags,
2770 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771{
David Howells88e67f32008-11-14 10:39:21 +11002772 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773
2774 if (flags & MS_REMOUNT)
Al Virod8c95842011-12-07 18:16:57 -05002775 return superblock_has_perm(cred, path->dentry->d_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002776 FILESYSTEM__REMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 else
Eric Paris2875fa02011-04-28 16:04:24 -04002778 return path_has_perm(cred, path, FILE__MOUNTON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779}
2780
2781static int selinux_umount(struct vfsmount *mnt, int flags)
2782{
David Howells88e67f32008-11-14 10:39:21 +11002783 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784
David Howells88e67f32008-11-14 10:39:21 +11002785 return superblock_has_perm(cred, mnt->mnt_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002786 FILESYSTEM__UNMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787}
2788
2789/* inode security operations */
2790
2791static int selinux_inode_alloc_security(struct inode *inode)
2792{
2793 return inode_alloc_security(inode);
2794}
2795
2796static void selinux_inode_free_security(struct inode *inode)
2797{
2798 inode_free_security(inode);
2799}
2800
David Quigleyd47be3d2013-05-22 12:50:34 -04002801static int selinux_dentry_init_security(struct dentry *dentry, int mode,
Al Viro4f3ccd72016-07-20 16:06:15 -04002802 const struct qstr *name, void **ctx,
David Quigleyd47be3d2013-05-22 12:50:34 -04002803 u32 *ctxlen)
2804{
David Quigleyd47be3d2013-05-22 12:50:34 -04002805 u32 newsid;
2806 int rc;
2807
Vivek Goyalc957f6d2016-07-13 10:44:51 -04002808 rc = selinux_determine_inode_label(current_security(),
2809 d_inode(dentry->d_parent), name,
David Howellsc3c188b2015-07-10 17:19:58 -04002810 inode_mode_to_security_class(mode),
2811 &newsid);
2812 if (rc)
2813 return rc;
David Quigleyd47be3d2013-05-22 12:50:34 -04002814
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002815 return security_sid_to_context(&selinux_state, newsid, (char **)ctx,
2816 ctxlen);
David Quigleyd47be3d2013-05-22 12:50:34 -04002817}
2818
Vivek Goyala518b0a2016-07-13 10:44:53 -04002819static int selinux_dentry_create_files_as(struct dentry *dentry, int mode,
2820 struct qstr *name,
2821 const struct cred *old,
2822 struct cred *new)
2823{
2824 u32 newsid;
2825 int rc;
2826 struct task_security_struct *tsec;
2827
2828 rc = selinux_determine_inode_label(old->security,
2829 d_inode(dentry->d_parent), name,
2830 inode_mode_to_security_class(mode),
2831 &newsid);
2832 if (rc)
2833 return rc;
2834
2835 tsec = new->security;
2836 tsec->create_sid = newsid;
2837 return 0;
2838}
2839
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002840static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +09002841 const struct qstr *qstr,
2842 const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -05002843 void **value, size_t *len)
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002844{
Paul Moore5fb49872010-04-22 14:46:19 -04002845 const struct task_security_struct *tsec = current_security();
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002846 struct superblock_security_struct *sbsec;
Corentin LABBEc0d4f462017-10-04 20:32:17 +02002847 u32 newsid, clen;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002848 int rc;
Tetsuo Handa95489062013-07-25 05:44:02 +09002849 char *context;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002850
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002851 sbsec = dir->i_sb->s_security;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002852
David Howells275bb412008-11-14 10:39:19 +11002853 newsid = tsec->create_sid;
2854
Vivek Goyalc957f6d2016-07-13 10:44:51 -04002855 rc = selinux_determine_inode_label(current_security(),
David Howellsc3c188b2015-07-10 17:19:58 -04002856 dir, qstr,
2857 inode_mode_to_security_class(inode->i_mode),
2858 &newsid);
2859 if (rc)
2860 return rc;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002861
Eric Paris296fddf2006-09-25 23:32:00 -07002862 /* Possibly defer initialization to selinux_complete_init. */
David P. Quigley0d90a7e2009-01-16 09:22:02 -05002863 if (sbsec->flags & SE_SBINITIALIZED) {
Eric Paris296fddf2006-09-25 23:32:00 -07002864 struct inode_security_struct *isec = inode->i_security;
2865 isec->sclass = inode_mode_to_security_class(inode->i_mode);
2866 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05002867 isec->initialized = LABEL_INITIALIZED;
Eric Paris296fddf2006-09-25 23:32:00 -07002868 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002869
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002870 if (!selinux_state.initialized || !(sbsec->flags & SBLABEL_MNT))
Stephen Smalley25a74f32005-11-08 21:34:33 -08002871 return -EOPNOTSUPP;
2872
Tetsuo Handa95489062013-07-25 05:44:02 +09002873 if (name)
2874 *name = XATTR_SELINUX_SUFFIX;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002875
2876 if (value && len) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002877 rc = security_sid_to_context_force(&selinux_state, newsid,
2878 &context, &clen);
Tetsuo Handa95489062013-07-25 05:44:02 +09002879 if (rc)
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002880 return rc;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002881 *value = context;
2882 *len = clen;
2883 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002884
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002885 return 0;
2886}
2887
Al Viro4acdaf22011-07-26 01:42:34 -04002888static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889{
2890 return may_create(dir, dentry, SECCLASS_FILE);
2891}
2892
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2894{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 return may_link(dir, old_dentry, MAY_LINK);
2896}
2897
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
2899{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 return may_link(dir, dentry, MAY_UNLINK);
2901}
2902
2903static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
2904{
2905 return may_create(dir, dentry, SECCLASS_LNK_FILE);
2906}
2907
Al Viro18bb1db2011-07-26 01:41:39 -04002908static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909{
2910 return may_create(dir, dentry, SECCLASS_DIR);
2911}
2912
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
2914{
2915 return may_link(dir, dentry, MAY_RMDIR);
2916}
2917
Al Viro1a67aaf2011-07-26 01:52:52 -04002918static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 return may_create(dir, dentry, inode_mode_to_security_class(mode));
2921}
2922
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
Eric Paris828dfe12008-04-17 13:17:49 -04002924 struct inode *new_inode, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925{
2926 return may_rename(old_inode, old_dentry, new_inode, new_dentry);
2927}
2928
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929static int selinux_inode_readlink(struct dentry *dentry)
2930{
David Howells88e67f32008-11-14 10:39:21 +11002931 const struct cred *cred = current_cred();
2932
Eric Paris2875fa02011-04-28 16:04:24 -04002933 return dentry_has_perm(cred, dentry, FILE__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934}
2935
NeilBrownbda0be72015-03-23 13:37:39 +11002936static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode,
2937 bool rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938{
David Howells88e67f32008-11-14 10:39:21 +11002939 const struct cred *cred = current_cred();
NeilBrownbda0be72015-03-23 13:37:39 +11002940 struct common_audit_data ad;
2941 struct inode_security_struct *isec;
2942 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943
NeilBrownbda0be72015-03-23 13:37:39 +11002944 validate_creds(cred);
2945
2946 ad.type = LSM_AUDIT_DATA_DENTRY;
2947 ad.u.dentry = dentry;
2948 sid = cred_sid(cred);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05002949 isec = inode_security_rcu(inode, rcu);
2950 if (IS_ERR(isec))
2951 return PTR_ERR(isec);
NeilBrownbda0be72015-03-23 13:37:39 +11002952
Stephen Smalleye46e01e2018-12-12 10:10:56 -05002953 return avc_has_perm(&selinux_state,
2954 sid, isec->sid, isec->sclass, FILE__READ, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955}
2956
Eric Parisd4cf970d2012-04-04 15:01:42 -04002957static noinline int audit_inode_permission(struct inode *inode,
2958 u32 perms, u32 audited, u32 denied,
Stephen Smalley626b9742014-04-29 11:29:04 -07002959 int result,
Eric Parisd4cf970d2012-04-04 15:01:42 -04002960 unsigned flags)
2961{
2962 struct common_audit_data ad;
Eric Parisd4cf970d2012-04-04 15:01:42 -04002963 struct inode_security_struct *isec = inode->i_security;
2964 int rc;
2965
Eric Paris50c205f2012-04-04 15:01:43 -04002966 ad.type = LSM_AUDIT_DATA_INODE;
Eric Parisd4cf970d2012-04-04 15:01:42 -04002967 ad.u.inode = inode;
2968
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002969 rc = slow_avc_audit(&selinux_state,
2970 current_sid(), isec->sid, isec->sclass, perms,
Stephen Smalley626b9742014-04-29 11:29:04 -07002971 audited, denied, result, &ad, flags);
Eric Parisd4cf970d2012-04-04 15:01:42 -04002972 if (rc)
2973 return rc;
2974 return 0;
2975}
2976
Al Viroe74f71e2011-06-20 19:38:15 -04002977static int selinux_inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978{
David Howells88e67f32008-11-14 10:39:21 +11002979 const struct cred *cred = current_cred();
Eric Parisb782e0a2010-07-23 11:44:03 -04002980 u32 perms;
2981 bool from_access;
Al Virocf1dd1d2011-06-20 19:44:08 -04002982 unsigned flags = mask & MAY_NOT_BLOCK;
Eric Paris2e334052012-04-04 15:01:42 -04002983 struct inode_security_struct *isec;
2984 u32 sid;
2985 struct av_decision avd;
2986 int rc, rc2;
2987 u32 audited, denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988
Eric Parisb782e0a2010-07-23 11:44:03 -04002989 from_access = mask & MAY_ACCESS;
Eric Parisd09ca732010-07-23 11:43:57 -04002990 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
2991
Eric Parisb782e0a2010-07-23 11:44:03 -04002992 /* No permission to check. Existence test. */
2993 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995
Eric Paris2e334052012-04-04 15:01:42 -04002996 validate_creds(cred);
Eric Parisb782e0a2010-07-23 11:44:03 -04002997
Eric Paris2e334052012-04-04 15:01:42 -04002998 if (unlikely(IS_PRIVATE(inode)))
2999 return 0;
Eric Parisb782e0a2010-07-23 11:44:03 -04003000
3001 perms = file_mask_to_av(inode->i_mode, mask);
3002
Eric Paris2e334052012-04-04 15:01:42 -04003003 sid = cred_sid(cred);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05003004 isec = inode_security_rcu(inode, flags & MAY_NOT_BLOCK);
3005 if (IS_ERR(isec))
3006 return PTR_ERR(isec);
Eric Paris2e334052012-04-04 15:01:42 -04003007
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003008 rc = avc_has_perm_noaudit(&selinux_state,
Stephen Smalley3a28cff2018-12-12 10:10:55 -05003009 sid, isec->sid, isec->sclass, perms,
3010 (flags & MAY_NOT_BLOCK) ? AVC_NONBLOCKING : 0,
3011 &avd);
Eric Paris2e334052012-04-04 15:01:42 -04003012 audited = avc_audit_required(perms, &avd, rc,
3013 from_access ? FILE__AUDIT_ACCESS : 0,
3014 &denied);
3015 if (likely(!audited))
3016 return rc;
3017
Stephen Smalley626b9742014-04-29 11:29:04 -07003018 rc2 = audit_inode_permission(inode, perms, audited, denied, rc, flags);
Eric Paris2e334052012-04-04 15:01:42 -04003019 if (rc2)
3020 return rc2;
3021 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022}
3023
3024static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
3025{
David Howells88e67f32008-11-14 10:39:21 +11003026 const struct cred *cred = current_cred();
Stephen Smalleyccb54472017-05-12 12:41:24 -04003027 struct inode *inode = d_backing_inode(dentry);
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003028 unsigned int ia_valid = iattr->ia_valid;
Eric Paris95dbf732012-04-04 13:45:34 -04003029 __u32 av = FILE__WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003031 /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
3032 if (ia_valid & ATTR_FORCE) {
3033 ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
3034 ATTR_FORCE);
3035 if (!ia_valid)
3036 return 0;
3037 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003039 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
3040 ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
Eric Paris2875fa02011-04-28 16:04:24 -04003041 return dentry_has_perm(cred, dentry, FILE__SETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003043 if (selinux_policycap_openperm() &&
Stephen Smalleyccb54472017-05-12 12:41:24 -04003044 inode->i_sb->s_magic != SOCKFS_MAGIC &&
3045 (ia_valid & ATTR_SIZE) &&
3046 !(ia_valid & ATTR_FILE))
Eric Paris95dbf732012-04-04 13:45:34 -04003047 av |= FILE__OPEN;
3048
3049 return dentry_has_perm(cred, dentry, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050}
3051
Al Viro3f7036a2015-03-08 19:28:30 -04003052static int selinux_inode_getattr(const struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053{
Al Viro3f7036a2015-03-08 19:28:30 -04003054 return path_has_perm(current_cred(), path, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055}
3056
Stephen Smalleydb590002017-04-20 11:31:30 -04003057static bool has_cap_mac_admin(bool audit)
3058{
3059 const struct cred *cred = current_cred();
3060 int cap_audit = audit ? SECURITY_CAP_AUDIT : SECURITY_CAP_NOAUDIT;
3061
3062 if (cap_capable(cred, &init_user_ns, CAP_MAC_ADMIN, cap_audit))
3063 return false;
3064 if (cred_has_capability(cred, CAP_MAC_ADMIN, cap_audit, true))
3065 return false;
3066 return true;
3067}
3068
David Howells8f0cfa52008-04-29 00:59:41 -07003069static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
3070 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071{
David Howellsc6f493d2015-03-17 22:26:22 +00003072 struct inode *inode = d_backing_inode(dentry);
Paul Moore20cdef82016-04-04 14:14:42 -04003073 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 struct superblock_security_struct *sbsec;
Thomas Liu2bf49692009-07-14 12:14:09 -04003075 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11003076 u32 newsid, sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 int rc = 0;
3078
Eric W. Biederman6b240302017-10-02 09:38:20 -05003079 if (strcmp(name, XATTR_NAME_SELINUX)) {
3080 rc = cap_inode_setxattr(dentry, name, value, size, flags);
3081 if (rc)
3082 return rc;
3083
3084 /* Not an attribute we recognize, so just check the
3085 ordinary setattr permission. */
3086 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088
3089 sbsec = inode->i_sb->s_security;
Eric Paris12f348b2012-10-09 10:56:25 -04003090 if (!(sbsec->flags & SBLABEL_MNT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 return -EOPNOTSUPP;
3092
Serge E. Hallyn2e149672011-03-23 16:43:26 -07003093 if (!inode_owner_or_capable(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 return -EPERM;
3095
Eric Paris50c205f2012-04-04 15:01:43 -04003096 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04003097 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098
Paul Moore20cdef82016-04-04 14:14:42 -04003099 isec = backing_inode_security(dentry);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003100 rc = avc_has_perm(&selinux_state,
3101 sid, isec->sid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 FILE__RELABELFROM, &ad);
3103 if (rc)
3104 return rc;
3105
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003106 rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3107 GFP_KERNEL);
Stephen Smalley12b29f32008-05-07 13:03:20 -04003108 if (rc == -EINVAL) {
Stephen Smalleydb590002017-04-20 11:31:30 -04003109 if (!has_cap_mac_admin(true)) {
Eric Parisd6ea83e2012-04-04 13:45:49 -04003110 struct audit_buffer *ab;
3111 size_t audit_size;
Eric Parisd6ea83e2012-04-04 13:45:49 -04003112
3113 /* We strip a nul only if it is at the end, otherwise the
3114 * context contains a nul and we should audit that */
Al Viroe3fea3f2012-06-09 08:15:16 +01003115 if (value) {
Colin Ian Kingadd24372017-10-14 13:46:55 +01003116 const char *str = value;
3117
Al Viroe3fea3f2012-06-09 08:15:16 +01003118 if (str[size - 1] == '\0')
3119 audit_size = size - 1;
3120 else
3121 audit_size = size;
3122 } else {
Al Viroe3fea3f2012-06-09 08:15:16 +01003123 audit_size = 0;
3124 }
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04003125 ab = audit_log_start(audit_context(),
3126 GFP_ATOMIC, AUDIT_SELINUX_ERR);
Eric Parisd6ea83e2012-04-04 13:45:49 -04003127 audit_log_format(ab, "op=setxattr invalid_context=");
3128 audit_log_n_untrustedstring(ab, value, audit_size);
3129 audit_log_end(ab);
3130
Stephen Smalley12b29f32008-05-07 13:03:20 -04003131 return rc;
Eric Parisd6ea83e2012-04-04 13:45:49 -04003132 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003133 rc = security_context_to_sid_force(&selinux_state, value,
3134 size, &newsid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04003135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 if (rc)
3137 return rc;
3138
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003139 rc = avc_has_perm(&selinux_state,
3140 sid, newsid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 FILE__RELABELTO, &ad);
3142 if (rc)
3143 return rc;
3144
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003145 rc = security_validate_transition(&selinux_state, isec->sid, newsid,
3146 sid, isec->sclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 if (rc)
3148 return rc;
3149
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003150 return avc_has_perm(&selinux_state,
3151 newsid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 sbsec->sid,
3153 SECCLASS_FILESYSTEM,
3154 FILESYSTEM__ASSOCIATE,
3155 &ad);
3156}
3157
David Howells8f0cfa52008-04-29 00:59:41 -07003158static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
Eric Parisf5269712008-05-14 11:27:45 -04003159 const void *value, size_t size,
David Howells8f0cfa52008-04-29 00:59:41 -07003160 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161{
David Howellsc6f493d2015-03-17 22:26:22 +00003162 struct inode *inode = d_backing_inode(dentry);
Paul Moore20cdef82016-04-04 14:14:42 -04003163 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 u32 newsid;
3165 int rc;
3166
3167 if (strcmp(name, XATTR_NAME_SELINUX)) {
3168 /* Not an attribute we recognize, so nothing to do. */
3169 return;
3170 }
3171
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003172 rc = security_context_to_sid_force(&selinux_state, value, size,
3173 &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 if (rc) {
peter enderborgc103a912018-06-12 10:09:03 +02003175 pr_err("SELinux: unable to map context to SID"
Stephen Smalley12b29f32008-05-07 13:03:20 -04003176 "for (%s, %lu), rc=%d\n",
3177 inode->i_sb->s_id, inode->i_ino, -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 return;
3179 }
3180
Paul Moore20cdef82016-04-04 14:14:42 -04003181 isec = backing_inode_security(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003182 spin_lock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003183 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05003185 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003186 spin_unlock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003187
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 return;
3189}
3190
David Howells8f0cfa52008-04-29 00:59:41 -07003191static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192{
David Howells88e67f32008-11-14 10:39:21 +11003193 const struct cred *cred = current_cred();
3194
Eric Paris2875fa02011-04-28 16:04:24 -04003195 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196}
3197
Eric Paris828dfe12008-04-17 13:17:49 -04003198static int selinux_inode_listxattr(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199{
David Howells88e67f32008-11-14 10:39:21 +11003200 const struct cred *cred = current_cred();
3201
Eric Paris2875fa02011-04-28 16:04:24 -04003202 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203}
3204
David Howells8f0cfa52008-04-29 00:59:41 -07003205static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206{
Eric W. Biederman6b240302017-10-02 09:38:20 -05003207 if (strcmp(name, XATTR_NAME_SELINUX)) {
3208 int rc = cap_inode_removexattr(dentry, name);
3209 if (rc)
3210 return rc;
3211
3212 /* Not an attribute we recognize, so just check the
3213 ordinary setattr permission. */
3214 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216
3217 /* No one is allowed to remove a SELinux security label.
3218 You can change the label, but all data must be labeled. */
3219 return -EACCES;
3220}
3221
James Morrisd381d8a2005-10-30 14:59:22 -08003222/*
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003223 * Copy the inode security context value to the user.
James Morrisd381d8a2005-10-30 14:59:22 -08003224 *
3225 * Permission check is handled by selinux_inode_getxattr hook.
3226 */
Andreas Gruenbacherea861df2015-12-24 11:09:39 -05003227static int selinux_inode_getsecurity(struct inode *inode, const char *name, void **buffer, bool alloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228{
David P. Quigley42492592008-02-04 22:29:39 -08003229 u32 size;
3230 int error;
3231 char *context = NULL;
Paul Moore20cdef82016-04-04 14:14:42 -04003232 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00003234 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3235 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003237 /*
3238 * If the caller has CAP_MAC_ADMIN, then get the raw context
3239 * value even if it is not defined by current policy; otherwise,
3240 * use the in-core value under current policy.
3241 * Use the non-auditing forms of the permission checks since
3242 * getxattr may be called by unprivileged processes commonly
3243 * and lack of permission just means that we fall back to the
3244 * in-core context value, not a denial.
3245 */
Paul Moore20cdef82016-04-04 14:14:42 -04003246 isec = inode_security(inode);
Stephen Smalleydb590002017-04-20 11:31:30 -04003247 if (has_cap_mac_admin(false))
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003248 error = security_sid_to_context_force(&selinux_state,
3249 isec->sid, &context,
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003250 &size);
3251 else
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003252 error = security_sid_to_context(&selinux_state, isec->sid,
3253 &context, &size);
David P. Quigley42492592008-02-04 22:29:39 -08003254 if (error)
3255 return error;
3256 error = size;
3257 if (alloc) {
3258 *buffer = context;
3259 goto out_nofree;
3260 }
3261 kfree(context);
3262out_nofree:
3263 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264}
3265
3266static int selinux_inode_setsecurity(struct inode *inode, const char *name,
Eric Paris828dfe12008-04-17 13:17:49 -04003267 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268{
Paul Moore2c971652016-04-19 16:36:28 -04003269 struct inode_security_struct *isec = inode_security_novalidate(inode);
Ondrej Mosnacek53e0c2a2018-12-21 21:18:53 +01003270 struct superblock_security_struct *sbsec = inode->i_sb->s_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 u32 newsid;
3272 int rc;
3273
3274 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3275 return -EOPNOTSUPP;
3276
Ondrej Mosnacek53e0c2a2018-12-21 21:18:53 +01003277 if (!(sbsec->flags & SBLABEL_MNT))
3278 return -EOPNOTSUPP;
3279
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 if (!value || !size)
3281 return -EACCES;
3282
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003283 rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3284 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 if (rc)
3286 return rc;
3287
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003288 spin_lock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003289 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05003291 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003292 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 return 0;
3294}
3295
3296static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
3297{
3298 const int len = sizeof(XATTR_NAME_SELINUX);
3299 if (buffer && len <= buffer_size)
3300 memcpy(buffer, XATTR_NAME_SELINUX, len);
3301 return len;
3302}
3303
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05003304static void selinux_inode_getsecid(struct inode *inode, u32 *secid)
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02003305{
Andreas Gruenbachere817c2f2016-02-18 12:04:08 +01003306 struct inode_security_struct *isec = inode_security_novalidate(inode);
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02003307 *secid = isec->sid;
3308}
3309
Vivek Goyal56909eb2016-07-13 10:44:48 -04003310static int selinux_inode_copy_up(struct dentry *src, struct cred **new)
3311{
3312 u32 sid;
3313 struct task_security_struct *tsec;
3314 struct cred *new_creds = *new;
3315
3316 if (new_creds == NULL) {
3317 new_creds = prepare_creds();
3318 if (!new_creds)
3319 return -ENOMEM;
3320 }
3321
3322 tsec = new_creds->security;
3323 /* Get label from overlay inode and set it in create_sid */
3324 selinux_inode_getsecid(d_inode(src), &sid);
3325 tsec->create_sid = sid;
3326 *new = new_creds;
3327 return 0;
3328}
3329
Vivek Goyal19472b62016-07-13 10:44:50 -04003330static int selinux_inode_copy_up_xattr(const char *name)
3331{
3332 /* The copy_up hook above sets the initial context on an inode, but we
3333 * don't then want to overwrite it by blindly copying all the lower
3334 * xattrs up. Instead, we have to filter out SELinux-related xattrs.
3335 */
3336 if (strcmp(name, XATTR_NAME_SELINUX) == 0)
3337 return 1; /* Discard */
3338 /*
3339 * Any other attribute apart from SELINUX is not claimed, supported
3340 * by selinux.
3341 */
3342 return -EOPNOTSUPP;
3343}
3344
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345/* file security operations */
3346
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003347static int selinux_revalidate_file_permission(struct file *file, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348{
David Howells88e67f32008-11-14 10:39:21 +11003349 const struct cred *cred = current_cred();
Al Viro496ad9a2013-01-23 17:07:38 -05003350 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
3353 if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
3354 mask |= MAY_APPEND;
3355
Paul Moore389fb8002009-03-27 17:10:34 -04003356 return file_has_perm(cred, file,
3357 file_mask_to_av(inode->i_mode, mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358}
3359
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003360static int selinux_file_permission(struct file *file, int mask)
3361{
Al Viro496ad9a2013-01-23 17:07:38 -05003362 struct inode *inode = file_inode(file);
Stephen Smalley20dda182009-06-22 14:54:53 -04003363 struct file_security_struct *fsec = file->f_security;
Andreas Gruenbacherb1973672016-01-05 23:12:33 +01003364 struct inode_security_struct *isec;
Stephen Smalley20dda182009-06-22 14:54:53 -04003365 u32 sid = current_sid();
3366
Paul Moore389fb8002009-03-27 17:10:34 -04003367 if (!mask)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003368 /* No permission to check. Existence test. */
3369 return 0;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003370
Andreas Gruenbacherb1973672016-01-05 23:12:33 +01003371 isec = inode_security(inode);
Stephen Smalley20dda182009-06-22 14:54:53 -04003372 if (sid == fsec->sid && fsec->isid == isec->sid &&
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003373 fsec->pseqno == avc_policy_seqno(&selinux_state))
Eric Paris83d49852012-04-04 13:45:40 -04003374 /* No change since file_open check. */
Stephen Smalley20dda182009-06-22 14:54:53 -04003375 return 0;
3376
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003377 return selinux_revalidate_file_permission(file, mask);
3378}
3379
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380static int selinux_file_alloc_security(struct file *file)
3381{
3382 return file_alloc_security(file);
3383}
3384
3385static void selinux_file_free_security(struct file *file)
3386{
3387 file_free_security(file);
3388}
3389
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003390/*
3391 * Check whether a task has the ioctl permission and cmd
3392 * operation to an inode.
3393 */
Geliang Tang1d2a1682015-10-21 17:44:27 -04003394static int ioctl_has_perm(const struct cred *cred, struct file *file,
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003395 u32 requested, u16 cmd)
3396{
3397 struct common_audit_data ad;
3398 struct file_security_struct *fsec = file->f_security;
3399 struct inode *inode = file_inode(file);
Paul Moore20cdef82016-04-04 14:14:42 -04003400 struct inode_security_struct *isec;
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003401 struct lsm_ioctlop_audit ioctl;
3402 u32 ssid = cred_sid(cred);
3403 int rc;
3404 u8 driver = cmd >> 8;
3405 u8 xperm = cmd & 0xff;
3406
3407 ad.type = LSM_AUDIT_DATA_IOCTL_OP;
3408 ad.u.op = &ioctl;
3409 ad.u.op->cmd = cmd;
3410 ad.u.op->path = file->f_path;
3411
3412 if (ssid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003413 rc = avc_has_perm(&selinux_state,
3414 ssid, fsec->sid,
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003415 SECCLASS_FD,
3416 FD__USE,
3417 &ad);
3418 if (rc)
3419 goto out;
3420 }
3421
3422 if (unlikely(IS_PRIVATE(inode)))
3423 return 0;
3424
Paul Moore20cdef82016-04-04 14:14:42 -04003425 isec = inode_security(inode);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003426 rc = avc_has_extended_perms(&selinux_state,
3427 ssid, isec->sid, isec->sclass,
3428 requested, driver, xperm, &ad);
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003429out:
3430 return rc;
3431}
3432
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433static int selinux_file_ioctl(struct file *file, unsigned int cmd,
3434 unsigned long arg)
3435{
David Howells88e67f32008-11-14 10:39:21 +11003436 const struct cred *cred = current_cred();
Eric Paris0b24dcb2011-02-25 15:39:20 -05003437 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438
Eric Paris0b24dcb2011-02-25 15:39:20 -05003439 switch (cmd) {
3440 case FIONREAD:
3441 /* fall through */
3442 case FIBMAP:
3443 /* fall through */
3444 case FIGETBSZ:
3445 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003446 case FS_IOC_GETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003447 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003448 case FS_IOC_GETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003449 error = file_has_perm(cred, file, FILE__GETATTR);
3450 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451
Al Viro2f99c362012-03-23 16:04:05 -04003452 case FS_IOC_SETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003453 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003454 case FS_IOC_SETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003455 error = file_has_perm(cred, file, FILE__SETATTR);
3456 break;
3457
3458 /* sys_ioctl() checks */
3459 case FIONBIO:
3460 /* fall through */
3461 case FIOASYNC:
3462 error = file_has_perm(cred, file, 0);
3463 break;
3464
3465 case KDSKBENT:
3466 case KDSKBSENT:
Eric Paris6a9de492012-01-03 12:25:14 -05003467 error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04003468 SECURITY_CAP_AUDIT, true);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003469 break;
3470
3471 /* default case assumes that the command will go
3472 * to the file's ioctl() function.
3473 */
3474 default:
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003475 error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003476 }
3477 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478}
3479
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003480static int default_noexec;
3481
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
3483{
David Howells88e67f32008-11-14 10:39:21 +11003484 const struct cred *cred = current_cred();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003485 u32 sid = cred_sid(cred);
David Howellsd84f4f92008-11-14 10:39:23 +11003486 int rc = 0;
David Howells88e67f32008-11-14 10:39:21 +11003487
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003488 if (default_noexec &&
Stephen Smalley892e8ca2015-07-10 09:40:59 -04003489 (prot & PROT_EXEC) && (!file || IS_PRIVATE(file_inode(file)) ||
3490 (!shared && (prot & PROT_WRITE)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 /*
3492 * We are making executable an anonymous mapping or a
3493 * private file mapping that will also be writable.
3494 * This has an additional check.
3495 */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003496 rc = avc_has_perm(&selinux_state,
3497 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003498 PROCESS__EXECMEM, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 if (rc)
David Howellsd84f4f92008-11-14 10:39:23 +11003500 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502
3503 if (file) {
3504 /* read access is always possible with a mapping */
3505 u32 av = FILE__READ;
3506
3507 /* write access only matters if the mapping is shared */
3508 if (shared && (prot & PROT_WRITE))
3509 av |= FILE__WRITE;
3510
3511 if (prot & PROT_EXEC)
3512 av |= FILE__EXECUTE;
3513
David Howells88e67f32008-11-14 10:39:21 +11003514 return file_has_perm(cred, file, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 }
David Howellsd84f4f92008-11-14 10:39:23 +11003516
3517error:
3518 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519}
3520
Al Viroe5467852012-05-30 13:30:51 -04003521static int selinux_mmap_addr(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07003523 int rc = 0;
Paul Moore98883bf2014-03-19 16:46:11 -04003524
3525 if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
3526 u32 sid = current_sid();
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003527 rc = avc_has_perm(&selinux_state,
3528 sid, sid, SECCLASS_MEMPROTECT,
Paul Moore98883bf2014-03-19 16:46:11 -04003529 MEMPROTECT__MMAP_ZERO, NULL);
3530 }
3531
3532 return rc;
Al Viroe5467852012-05-30 13:30:51 -04003533}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534
Al Viroe5467852012-05-30 13:30:51 -04003535static int selinux_mmap_file(struct file *file, unsigned long reqprot,
3536 unsigned long prot, unsigned long flags)
3537{
Stephen Smalley3ba4bf52017-05-05 09:14:48 -04003538 struct common_audit_data ad;
3539 int rc;
3540
3541 if (file) {
3542 ad.type = LSM_AUDIT_DATA_FILE;
3543 ad.u.file = file;
3544 rc = inode_has_perm(current_cred(), file_inode(file),
3545 FILE__MAP, &ad);
3546 if (rc)
3547 return rc;
3548 }
3549
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003550 if (selinux_state.checkreqprot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 prot = reqprot;
3552
3553 return file_map_prot_check(file, prot,
3554 (flags & MAP_TYPE) == MAP_SHARED);
3555}
3556
3557static int selinux_file_mprotect(struct vm_area_struct *vma,
3558 unsigned long reqprot,
3559 unsigned long prot)
3560{
David Howells88e67f32008-11-14 10:39:21 +11003561 const struct cred *cred = current_cred();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003562 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003564 if (selinux_state.checkreqprot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 prot = reqprot;
3566
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003567 if (default_noexec &&
3568 (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
James Morrisd541bbe2009-01-29 12:19:51 +11003569 int rc = 0;
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003570 if (vma->vm_start >= vma->vm_mm->start_brk &&
3571 vma->vm_end <= vma->vm_mm->brk) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003572 rc = avc_has_perm(&selinux_state,
3573 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003574 PROCESS__EXECHEAP, NULL);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003575 } else if (!vma->vm_file &&
Stephen Smalleyc2316db2016-04-08 13:55:03 -04003576 ((vma->vm_start <= vma->vm_mm->start_stack &&
3577 vma->vm_end >= vma->vm_mm->start_stack) ||
Andy Lutomirskid17af502016-09-30 10:58:58 -07003578 vma_is_stack_for_current(vma))) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003579 rc = avc_has_perm(&selinux_state,
3580 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003581 PROCESS__EXECSTACK, NULL);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003582 } else if (vma->vm_file && vma->anon_vma) {
3583 /*
3584 * We are making executable a file mapping that has
3585 * had some COW done. Since pages might have been
3586 * written, check ability to execute the possibly
3587 * modified content. This typically should only
3588 * occur for text relocations.
3589 */
David Howellsd84f4f92008-11-14 10:39:23 +11003590 rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003591 }
Lorenzo Hernandez García-Hierro6b992192005-06-25 14:54:34 -07003592 if (rc)
3593 return rc;
3594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595
3596 return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
3597}
3598
3599static int selinux_file_lock(struct file *file, unsigned int cmd)
3600{
David Howells88e67f32008-11-14 10:39:21 +11003601 const struct cred *cred = current_cred();
3602
3603 return file_has_perm(cred, file, FILE__LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604}
3605
3606static int selinux_file_fcntl(struct file *file, unsigned int cmd,
3607 unsigned long arg)
3608{
David Howells88e67f32008-11-14 10:39:21 +11003609 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 int err = 0;
3611
3612 switch (cmd) {
Eric Paris828dfe12008-04-17 13:17:49 -04003613 case F_SETFL:
Eric Paris828dfe12008-04-17 13:17:49 -04003614 if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
David Howells88e67f32008-11-14 10:39:21 +11003615 err = file_has_perm(cred, file, FILE__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003617 }
3618 /* fall through */
3619 case F_SETOWN:
3620 case F_SETSIG:
3621 case F_GETFL:
3622 case F_GETOWN:
3623 case F_GETSIG:
Cyrill Gorcunov1d151c32012-07-30 14:43:00 -07003624 case F_GETOWNER_UIDS:
Eric Paris828dfe12008-04-17 13:17:49 -04003625 /* Just check FD__USE permission */
David Howells88e67f32008-11-14 10:39:21 +11003626 err = file_has_perm(cred, file, 0);
Eric Paris828dfe12008-04-17 13:17:49 -04003627 break;
3628 case F_GETLK:
3629 case F_SETLK:
3630 case F_SETLKW:
Jeff Layton0d3f7a22014-04-22 08:23:58 -04003631 case F_OFD_GETLK:
3632 case F_OFD_SETLK:
3633 case F_OFD_SETLKW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634#if BITS_PER_LONG == 32
Eric Paris828dfe12008-04-17 13:17:49 -04003635 case F_GETLK64:
3636 case F_SETLK64:
3637 case F_SETLKW64:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638#endif
David Howells88e67f32008-11-14 10:39:21 +11003639 err = file_has_perm(cred, file, FILE__LOCK);
Eric Paris828dfe12008-04-17 13:17:49 -04003640 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 }
3642
3643 return err;
3644}
3645
Jeff Laytone0b93ed2014-08-22 11:27:32 -04003646static void selinux_file_set_fowner(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 struct file_security_struct *fsec;
3649
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 fsec = file->f_security;
David Howells275bb412008-11-14 10:39:19 +11003651 fsec->fown_sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652}
3653
3654static int selinux_file_send_sigiotask(struct task_struct *tsk,
3655 struct fown_struct *fown, int signum)
3656{
Eric Paris828dfe12008-04-17 13:17:49 -04003657 struct file *file;
Stephen Smalley65c90bc2009-05-04 15:43:18 -04003658 u32 sid = task_sid(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 struct file_security_struct *fsec;
3661
3662 /* struct fown_struct is never outside the context of a struct file */
Eric Paris828dfe12008-04-17 13:17:49 -04003663 file = container_of(fown, struct file, f_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 fsec = file->f_security;
3666
3667 if (!signum)
3668 perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
3669 else
3670 perm = signal_to_av(signum);
3671
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003672 return avc_has_perm(&selinux_state,
3673 fsec->fown_sid, sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 SECCLASS_PROCESS, perm, NULL);
3675}
3676
3677static int selinux_file_receive(struct file *file)
3678{
David Howells88e67f32008-11-14 10:39:21 +11003679 const struct cred *cred = current_cred();
3680
3681 return file_has_perm(cred, file, file_to_av(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682}
3683
Al Viro94817692018-07-10 14:13:18 -04003684static int selinux_file_open(struct file *file)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003685{
3686 struct file_security_struct *fsec;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003687 struct inode_security_struct *isec;
David Howellsd84f4f92008-11-14 10:39:23 +11003688
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003689 fsec = file->f_security;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05003690 isec = inode_security(file_inode(file));
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003691 /*
3692 * Save inode label and policy sequence number
3693 * at open-time so that selinux_file_permission
3694 * can determine whether revalidation is necessary.
3695 * Task label is already saved in the file security
3696 * struct as its SID.
3697 */
3698 fsec->isid = isec->sid;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003699 fsec->pseqno = avc_policy_seqno(&selinux_state);
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003700 /*
3701 * Since the inode label or policy seqno may have changed
3702 * between the selinux_inode_permission check and the saving
3703 * of state above, recheck that access is still permitted.
3704 * Otherwise, access might never be revalidated against the
3705 * new inode label or new policy.
3706 * This check is not redundant - do not remove.
3707 */
Al Viro94817692018-07-10 14:13:18 -04003708 return file_path_has_perm(file->f_cred, file, open_file_to_av(file));
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003709}
3710
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711/* task security operations */
3712
Tetsuo Handaa79be232017-03-28 23:08:45 +09003713static int selinux_task_alloc(struct task_struct *task,
3714 unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003716 u32 sid = current_sid();
3717
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003718 return avc_has_perm(&selinux_state,
3719 sid, sid, SECCLASS_PROCESS, PROCESS__FORK, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720}
3721
David Howellsf1752ee2008-11-14 10:39:17 +11003722/*
David Howellsee18d642009-09-02 09:14:21 +01003723 * allocate the SELinux part of blank credentials
3724 */
3725static int selinux_cred_alloc_blank(struct cred *cred, gfp_t gfp)
3726{
3727 struct task_security_struct *tsec;
3728
3729 tsec = kzalloc(sizeof(struct task_security_struct), gfp);
3730 if (!tsec)
3731 return -ENOMEM;
3732
3733 cred->security = tsec;
3734 return 0;
3735}
3736
3737/*
David Howellsf1752ee2008-11-14 10:39:17 +11003738 * detach and free the LSM part of a set of credentials
3739 */
3740static void selinux_cred_free(struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741{
David Howellsf1752ee2008-11-14 10:39:17 +11003742 struct task_security_struct *tsec = cred->security;
David Howellse0e81732009-09-02 09:13:40 +01003743
Tetsuo Handa2edeaa32011-02-07 13:36:10 +00003744 /*
3745 * cred->security == NULL if security_cred_alloc_blank() or
3746 * security_prepare_creds() returned an error.
3747 */
3748 BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE);
David Howellse0e81732009-09-02 09:13:40 +01003749 cred->security = (void *) 0x7UL;
David Howellsf1752ee2008-11-14 10:39:17 +11003750 kfree(tsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751}
3752
David Howellsd84f4f92008-11-14 10:39:23 +11003753/*
3754 * prepare a new set of credentials for modification
3755 */
3756static int selinux_cred_prepare(struct cred *new, const struct cred *old,
3757 gfp_t gfp)
3758{
3759 const struct task_security_struct *old_tsec;
3760 struct task_security_struct *tsec;
3761
3762 old_tsec = old->security;
3763
3764 tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp);
3765 if (!tsec)
3766 return -ENOMEM;
3767
3768 new->security = tsec;
3769 return 0;
3770}
3771
3772/*
David Howellsee18d642009-09-02 09:14:21 +01003773 * transfer the SELinux data to a blank set of creds
3774 */
3775static void selinux_cred_transfer(struct cred *new, const struct cred *old)
3776{
3777 const struct task_security_struct *old_tsec = old->security;
3778 struct task_security_struct *tsec = new->security;
3779
3780 *tsec = *old_tsec;
3781}
3782
Matthew Garrett3ec30112018-01-08 13:36:19 -08003783static void selinux_cred_getsecid(const struct cred *c, u32 *secid)
3784{
3785 *secid = cred_sid(c);
3786}
3787
David Howellsee18d642009-09-02 09:14:21 +01003788/*
David Howells3a3b7ce2008-11-14 10:39:28 +11003789 * set the security data for a kernel service
3790 * - all the creation contexts are set to unlabelled
3791 */
3792static int selinux_kernel_act_as(struct cred *new, u32 secid)
3793{
3794 struct task_security_struct *tsec = new->security;
3795 u32 sid = current_sid();
3796 int ret;
3797
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003798 ret = avc_has_perm(&selinux_state,
3799 sid, secid,
David Howells3a3b7ce2008-11-14 10:39:28 +11003800 SECCLASS_KERNEL_SERVICE,
3801 KERNEL_SERVICE__USE_AS_OVERRIDE,
3802 NULL);
3803 if (ret == 0) {
3804 tsec->sid = secid;
3805 tsec->create_sid = 0;
3806 tsec->keycreate_sid = 0;
3807 tsec->sockcreate_sid = 0;
3808 }
3809 return ret;
3810}
3811
3812/*
3813 * set the file creation context in a security record to the same as the
3814 * objective context of the specified inode
3815 */
3816static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
3817{
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05003818 struct inode_security_struct *isec = inode_security(inode);
David Howells3a3b7ce2008-11-14 10:39:28 +11003819 struct task_security_struct *tsec = new->security;
3820 u32 sid = current_sid();
3821 int ret;
3822
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003823 ret = avc_has_perm(&selinux_state,
3824 sid, isec->sid,
David Howells3a3b7ce2008-11-14 10:39:28 +11003825 SECCLASS_KERNEL_SERVICE,
3826 KERNEL_SERVICE__CREATE_FILES_AS,
3827 NULL);
3828
3829 if (ret == 0)
3830 tsec->create_sid = isec->sid;
David Howellsef574712010-02-26 01:56:16 +00003831 return ret;
David Howells3a3b7ce2008-11-14 10:39:28 +11003832}
3833
Eric Parisdd8dbf22009-11-03 16:35:32 +11003834static int selinux_kernel_module_request(char *kmod_name)
Eric Paris25354c42009-08-13 09:45:03 -04003835{
Eric Parisdd8dbf22009-11-03 16:35:32 +11003836 struct common_audit_data ad;
3837
Eric Paris50c205f2012-04-04 15:01:43 -04003838 ad.type = LSM_AUDIT_DATA_KMOD;
Eric Parisdd8dbf22009-11-03 16:35:32 +11003839 ad.u.kmod_name = kmod_name;
3840
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003841 return avc_has_perm(&selinux_state,
3842 current_sid(), SECINITSID_KERNEL, SECCLASS_SYSTEM,
Eric Parisdd8dbf22009-11-03 16:35:32 +11003843 SYSTEM__MODULE_REQUEST, &ad);
Eric Paris25354c42009-08-13 09:45:03 -04003844}
3845
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003846static int selinux_kernel_module_from_file(struct file *file)
3847{
3848 struct common_audit_data ad;
3849 struct inode_security_struct *isec;
3850 struct file_security_struct *fsec;
3851 u32 sid = current_sid();
3852 int rc;
3853
3854 /* init_module */
3855 if (file == NULL)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003856 return avc_has_perm(&selinux_state,
3857 sid, sid, SECCLASS_SYSTEM,
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003858 SYSTEM__MODULE_LOAD, NULL);
3859
3860 /* finit_module */
Paul Moore20cdef82016-04-04 14:14:42 -04003861
Vivek Goyal43af5de2016-09-09 11:37:49 -04003862 ad.type = LSM_AUDIT_DATA_FILE;
3863 ad.u.file = file;
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003864
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003865 fsec = file->f_security;
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003866 if (sid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003867 rc = avc_has_perm(&selinux_state,
3868 sid, fsec->sid, SECCLASS_FD, FD__USE, &ad);
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003869 if (rc)
3870 return rc;
3871 }
3872
Paul Moore20cdef82016-04-04 14:14:42 -04003873 isec = inode_security(file_inode(file));
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003874 return avc_has_perm(&selinux_state,
3875 sid, isec->sid, SECCLASS_SYSTEM,
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003876 SYSTEM__MODULE_LOAD, &ad);
3877}
3878
3879static int selinux_kernel_read_file(struct file *file,
3880 enum kernel_read_file_id id)
3881{
3882 int rc = 0;
3883
3884 switch (id) {
3885 case READING_MODULE:
3886 rc = selinux_kernel_module_from_file(file);
3887 break;
3888 default:
3889 break;
3890 }
3891
3892 return rc;
3893}
3894
Mimi Zoharc77b8cd2018-07-13 14:06:02 -04003895static int selinux_kernel_load_data(enum kernel_load_data_id id)
3896{
3897 int rc = 0;
3898
3899 switch (id) {
3900 case LOADING_MODULE:
3901 rc = selinux_kernel_module_from_file(NULL);
3902 default:
3903 break;
3904 }
3905
3906 return rc;
3907}
3908
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
3910{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003911 return avc_has_perm(&selinux_state,
3912 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003913 PROCESS__SETPGID, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914}
3915
3916static int selinux_task_getpgid(struct task_struct *p)
3917{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003918 return avc_has_perm(&selinux_state,
3919 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003920 PROCESS__GETPGID, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921}
3922
3923static int selinux_task_getsid(struct task_struct *p)
3924{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003925 return avc_has_perm(&selinux_state,
3926 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003927 PROCESS__GETSESSION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928}
3929
David Quigleyf9008e4c2006-06-30 01:55:46 -07003930static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
3931{
David Howells275bb412008-11-14 10:39:19 +11003932 *secid = task_sid(p);
David Quigleyf9008e4c2006-06-30 01:55:46 -07003933}
3934
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935static int selinux_task_setnice(struct task_struct *p, int nice)
3936{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003937 return avc_has_perm(&selinux_state,
3938 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003939 PROCESS__SETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940}
3941
James Morris03e68062006-06-23 02:03:58 -07003942static int selinux_task_setioprio(struct task_struct *p, int ioprio)
3943{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003944 return avc_has_perm(&selinux_state,
3945 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003946 PROCESS__SETSCHED, NULL);
James Morris03e68062006-06-23 02:03:58 -07003947}
3948
David Quigleya1836a42006-06-30 01:55:49 -07003949static int selinux_task_getioprio(struct task_struct *p)
3950{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003951 return avc_has_perm(&selinux_state,
3952 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003953 PROCESS__GETSCHED, NULL);
David Quigleya1836a42006-06-30 01:55:49 -07003954}
3955
Corentin LABBE42985552017-10-04 20:32:18 +02003956static int selinux_task_prlimit(const struct cred *cred, const struct cred *tcred,
3957 unsigned int flags)
Stephen Smalley791ec492017-02-17 07:57:00 -05003958{
3959 u32 av = 0;
3960
Stephen Smalley84e68852017-02-28 09:35:08 -05003961 if (!flags)
3962 return 0;
Stephen Smalley791ec492017-02-17 07:57:00 -05003963 if (flags & LSM_PRLIMIT_WRITE)
3964 av |= PROCESS__SETRLIMIT;
3965 if (flags & LSM_PRLIMIT_READ)
3966 av |= PROCESS__GETRLIMIT;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003967 return avc_has_perm(&selinux_state,
3968 cred_sid(cred), cred_sid(tcred),
Stephen Smalley791ec492017-02-17 07:57:00 -05003969 SECCLASS_PROCESS, av, NULL);
3970}
3971
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003972static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
3973 struct rlimit *new_rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974{
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003975 struct rlimit *old_rlim = p->signal->rlim + resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976
3977 /* Control the ability to change the hard limit (whether
3978 lowering or raising it), so that the hard limit can
3979 later be used as a safe reset point for the soft limit
David Howellsd84f4f92008-11-14 10:39:23 +11003980 upon context transitions. See selinux_bprm_committing_creds. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 if (old_rlim->rlim_max != new_rlim->rlim_max)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003982 return avc_has_perm(&selinux_state,
3983 current_sid(), task_sid(p),
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003984 SECCLASS_PROCESS, PROCESS__SETRLIMIT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985
3986 return 0;
3987}
3988
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09003989static int selinux_task_setscheduler(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003991 return avc_has_perm(&selinux_state,
3992 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003993 PROCESS__SETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994}
3995
3996static int selinux_task_getscheduler(struct task_struct *p)
3997{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003998 return avc_has_perm(&selinux_state,
3999 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004000 PROCESS__GETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001}
4002
David Quigley35601542006-06-23 02:04:01 -07004003static int selinux_task_movememory(struct task_struct *p)
4004{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004005 return avc_has_perm(&selinux_state,
4006 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004007 PROCESS__SETSCHED, NULL);
David Quigley35601542006-06-23 02:04:01 -07004008}
4009
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02004010static int selinux_task_kill(struct task_struct *p, struct kernel_siginfo *info,
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004011 int sig, const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012{
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004013 u32 secid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 if (!sig)
4017 perm = PROCESS__SIGNULL; /* null signal; existence test */
4018 else
4019 perm = signal_to_av(sig);
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004020 if (!cred)
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004021 secid = current_sid();
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004022 else
4023 secid = cred_sid(cred);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004024 return avc_has_perm(&selinux_state,
4025 secid, task_sid(p), SECCLASS_PROCESS, perm, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026}
4027
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028static void selinux_task_to_inode(struct task_struct *p,
4029 struct inode *inode)
4030{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 struct inode_security_struct *isec = inode->i_security;
David Howells275bb412008-11-14 10:39:19 +11004032 u32 sid = task_sid(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004034 spin_lock(&isec->lock);
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01004035 isec->sclass = inode_mode_to_security_class(inode->i_mode);
David Howells275bb412008-11-14 10:39:19 +11004036 isec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004037 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004038 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039}
4040
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004042static int selinux_parse_skb_ipv4(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04004043 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044{
4045 int offset, ihlen, ret = -EINVAL;
4046 struct iphdr _iph, *ih;
4047
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03004048 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
4050 if (ih == NULL)
4051 goto out;
4052
4053 ihlen = ih->ihl * 4;
4054 if (ihlen < sizeof(_iph))
4055 goto out;
4056
Eric Paris48c62af2012-04-02 13:15:44 -04004057 ad->u.net->v4info.saddr = ih->saddr;
4058 ad->u.net->v4info.daddr = ih->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 ret = 0;
4060
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004061 if (proto)
4062 *proto = ih->protocol;
4063
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 switch (ih->protocol) {
Eric Paris828dfe12008-04-17 13:17:49 -04004065 case IPPROTO_TCP: {
4066 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067
Eric Paris828dfe12008-04-17 13:17:49 -04004068 if (ntohs(ih->frag_off) & IP_OFFSET)
4069 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070
4071 offset += ihlen;
4072 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4073 if (th == NULL)
4074 break;
4075
Eric Paris48c62af2012-04-02 13:15:44 -04004076 ad->u.net->sport = th->source;
4077 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080
Eric Paris828dfe12008-04-17 13:17:49 -04004081 case IPPROTO_UDP: {
4082 struct udphdr _udph, *uh;
4083
4084 if (ntohs(ih->frag_off) & IP_OFFSET)
4085 break;
4086
4087 offset += ihlen;
4088 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4089 if (uh == NULL)
4090 break;
4091
Eric Paris48c62af2012-04-02 13:15:44 -04004092 ad->u.net->sport = uh->source;
4093 ad->u.net->dport = uh->dest;
Eric Paris828dfe12008-04-17 13:17:49 -04004094 break;
4095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096
James Morris2ee92d42006-11-13 16:09:01 -08004097 case IPPROTO_DCCP: {
4098 struct dccp_hdr _dccph, *dh;
4099
4100 if (ntohs(ih->frag_off) & IP_OFFSET)
4101 break;
4102
4103 offset += ihlen;
4104 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4105 if (dh == NULL)
4106 break;
4107
Eric Paris48c62af2012-04-02 13:15:44 -04004108 ad->u.net->sport = dh->dccph_sport;
4109 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08004110 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004111 }
James Morris2ee92d42006-11-13 16:09:01 -08004112
Richard Hainesd4529302018-02-13 20:57:18 +00004113#if IS_ENABLED(CONFIG_IP_SCTP)
4114 case IPPROTO_SCTP: {
4115 struct sctphdr _sctph, *sh;
4116
4117 if (ntohs(ih->frag_off) & IP_OFFSET)
4118 break;
4119
4120 offset += ihlen;
4121 sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4122 if (sh == NULL)
4123 break;
4124
4125 ad->u.net->sport = sh->source;
4126 ad->u.net->dport = sh->dest;
4127 break;
4128 }
4129#endif
Eric Paris828dfe12008-04-17 13:17:49 -04004130 default:
4131 break;
4132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133out:
4134 return ret;
4135}
4136
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04004137#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138
4139/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004140static int selinux_parse_skb_ipv6(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04004141 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142{
4143 u8 nexthdr;
4144 int ret = -EINVAL, offset;
4145 struct ipv6hdr _ipv6h, *ip6;
Jesse Gross75f28112011-11-30 17:05:51 -08004146 __be16 frag_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03004148 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
4150 if (ip6 == NULL)
4151 goto out;
4152
Eric Paris48c62af2012-04-02 13:15:44 -04004153 ad->u.net->v6info.saddr = ip6->saddr;
4154 ad->u.net->v6info.daddr = ip6->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 ret = 0;
4156
4157 nexthdr = ip6->nexthdr;
4158 offset += sizeof(_ipv6h);
Jesse Gross75f28112011-11-30 17:05:51 -08004159 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 if (offset < 0)
4161 goto out;
4162
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004163 if (proto)
4164 *proto = nexthdr;
4165
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 switch (nexthdr) {
4167 case IPPROTO_TCP: {
Eric Paris828dfe12008-04-17 13:17:49 -04004168 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169
4170 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4171 if (th == NULL)
4172 break;
4173
Eric Paris48c62af2012-04-02 13:15:44 -04004174 ad->u.net->sport = th->source;
4175 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 break;
4177 }
4178
4179 case IPPROTO_UDP: {
4180 struct udphdr _udph, *uh;
4181
4182 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4183 if (uh == NULL)
4184 break;
4185
Eric Paris48c62af2012-04-02 13:15:44 -04004186 ad->u.net->sport = uh->source;
4187 ad->u.net->dport = uh->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 break;
4189 }
4190
James Morris2ee92d42006-11-13 16:09:01 -08004191 case IPPROTO_DCCP: {
4192 struct dccp_hdr _dccph, *dh;
4193
4194 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4195 if (dh == NULL)
4196 break;
4197
Eric Paris48c62af2012-04-02 13:15:44 -04004198 ad->u.net->sport = dh->dccph_sport;
4199 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08004200 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004201 }
James Morris2ee92d42006-11-13 16:09:01 -08004202
Richard Hainesd4529302018-02-13 20:57:18 +00004203#if IS_ENABLED(CONFIG_IP_SCTP)
4204 case IPPROTO_SCTP: {
4205 struct sctphdr _sctph, *sh;
4206
4207 sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4208 if (sh == NULL)
4209 break;
4210
4211 ad->u.net->sport = sh->source;
4212 ad->u.net->dport = sh->dest;
4213 break;
4214 }
4215#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 /* includes fragments */
4217 default:
4218 break;
4219 }
4220out:
4221 return ret;
4222}
4223
4224#endif /* IPV6 */
4225
Thomas Liu2bf49692009-07-14 12:14:09 -04004226static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
David Howellscf9481e2008-07-27 21:31:07 +10004227 char **_addrp, int src, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228{
David Howellscf9481e2008-07-27 21:31:07 +10004229 char *addrp;
4230 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231
Eric Paris48c62af2012-04-02 13:15:44 -04004232 switch (ad->u.net->family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 case PF_INET:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004234 ret = selinux_parse_skb_ipv4(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10004235 if (ret)
4236 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04004237 addrp = (char *)(src ? &ad->u.net->v4info.saddr :
4238 &ad->u.net->v4info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10004239 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04004241#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 case PF_INET6:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004243 ret = selinux_parse_skb_ipv6(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10004244 if (ret)
4245 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04004246 addrp = (char *)(src ? &ad->u.net->v6info.saddr :
4247 &ad->u.net->v6info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10004248 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249#endif /* IPV6 */
4250 default:
David Howellscf9481e2008-07-27 21:31:07 +10004251 addrp = NULL;
4252 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 }
4254
David Howellscf9481e2008-07-27 21:31:07 +10004255parse_error:
peter enderborgc103a912018-06-12 10:09:03 +02004256 pr_warn(
David Howellscf9481e2008-07-27 21:31:07 +10004257 "SELinux: failure in selinux_parse_skb(),"
4258 " unable to parse packet\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 return ret;
David Howellscf9481e2008-07-27 21:31:07 +10004260
4261okay:
4262 if (_addrp)
4263 *_addrp = addrp;
4264 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265}
4266
Paul Moore4f6a9932007-03-01 14:35:22 -05004267/**
Paul Moore220deb92008-01-29 08:38:23 -05004268 * selinux_skb_peerlbl_sid - Determine the peer label of a packet
Paul Moore4f6a9932007-03-01 14:35:22 -05004269 * @skb: the packet
Paul Moore75e22912008-01-29 08:38:04 -05004270 * @family: protocol family
Paul Moore220deb92008-01-29 08:38:23 -05004271 * @sid: the packet's peer label SID
Paul Moore4f6a9932007-03-01 14:35:22 -05004272 *
4273 * Description:
Paul Moore220deb92008-01-29 08:38:23 -05004274 * Check the various different forms of network peer labeling and determine
4275 * the peer label/SID for the packet; most of the magic actually occurs in
4276 * the security server function security_net_peersid_cmp(). The function
4277 * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
4278 * or -EACCES if @sid is invalid due to inconsistencies with the different
4279 * peer labels.
Paul Moore4f6a9932007-03-01 14:35:22 -05004280 *
4281 */
Paul Moore220deb92008-01-29 08:38:23 -05004282static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
Paul Moore4f6a9932007-03-01 14:35:22 -05004283{
Paul Moore71f1cb02008-01-29 08:51:16 -05004284 int err;
Paul Moore4f6a9932007-03-01 14:35:22 -05004285 u32 xfrm_sid;
4286 u32 nlbl_sid;
Paul Moore220deb92008-01-29 08:38:23 -05004287 u32 nlbl_type;
Paul Moore4f6a9932007-03-01 14:35:22 -05004288
Paul Moore817eff72013-12-10 14:57:54 -05004289 err = selinux_xfrm_skb_sid(skb, &xfrm_sid);
Paul Moorebed4d7e2013-07-23 17:38:40 -04004290 if (unlikely(err))
4291 return -EACCES;
4292 err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
4293 if (unlikely(err))
4294 return -EACCES;
Paul Moore220deb92008-01-29 08:38:23 -05004295
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004296 err = security_net_peersid_resolve(&selinux_state, nlbl_sid,
4297 nlbl_type, xfrm_sid, sid);
Paul Moore71f1cb02008-01-29 08:51:16 -05004298 if (unlikely(err)) {
peter enderborgc103a912018-06-12 10:09:03 +02004299 pr_warn(
Paul Moore71f1cb02008-01-29 08:51:16 -05004300 "SELinux: failure in selinux_skb_peerlbl_sid(),"
4301 " unable to determine packet's peer label\n");
Paul Moore220deb92008-01-29 08:38:23 -05004302 return -EACCES;
Paul Moore71f1cb02008-01-29 08:51:16 -05004303 }
Paul Moore220deb92008-01-29 08:38:23 -05004304
4305 return 0;
Paul Moore4f6a9932007-03-01 14:35:22 -05004306}
4307
Paul Moore446b8022013-12-04 16:10:51 -05004308/**
4309 * selinux_conn_sid - Determine the child socket label for a connection
4310 * @sk_sid: the parent socket's SID
4311 * @skb_sid: the packet's SID
4312 * @conn_sid: the resulting connection SID
4313 *
4314 * If @skb_sid is valid then the user:role:type information from @sk_sid is
4315 * combined with the MLS information from @skb_sid in order to create
4316 * @conn_sid. If @skb_sid is not valid then then @conn_sid is simply a copy
4317 * of @sk_sid. Returns zero on success, negative values on failure.
4318 *
4319 */
4320static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid)
4321{
4322 int err = 0;
4323
4324 if (skb_sid != SECSID_NULL)
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004325 err = security_sid_mls_copy(&selinux_state, sk_sid, skb_sid,
4326 conn_sid);
Paul Moore446b8022013-12-04 16:10:51 -05004327 else
4328 *conn_sid = sk_sid;
4329
4330 return err;
4331}
4332
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333/* socket security operations */
Paul Moored4f2d972010-04-22 14:46:18 -04004334
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004335static int socket_sockcreate_sid(const struct task_security_struct *tsec,
4336 u16 secclass, u32 *socksid)
Paul Moored4f2d972010-04-22 14:46:18 -04004337{
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004338 if (tsec->sockcreate_sid > SECSID_NULL) {
4339 *socksid = tsec->sockcreate_sid;
4340 return 0;
4341 }
4342
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004343 return security_transition_sid(&selinux_state, tsec->sid, tsec->sid,
4344 secclass, NULL, socksid);
Paul Moored4f2d972010-04-22 14:46:18 -04004345}
4346
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004347static int sock_has_perm(struct sock *sk, u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348{
Paul Moore253bfae2010-04-22 14:46:19 -04004349 struct sk_security_struct *sksec = sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004350 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004351 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352
Paul Moore253bfae2010-04-22 14:46:19 -04004353 if (sksec->sid == SECINITSID_KERNEL)
4354 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355
Eric Paris50c205f2012-04-04 15:01:43 -04004356 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004357 ad.u.net = &net;
4358 ad.u.net->sk = sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004360 return avc_has_perm(&selinux_state,
4361 current_sid(), sksec->sid, sksec->sclass, perms,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004362 &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363}
4364
4365static int selinux_socket_create(int family, int type,
4366 int protocol, int kern)
4367{
Paul Moore5fb49872010-04-22 14:46:19 -04004368 const struct task_security_struct *tsec = current_security();
Paul Moored4f2d972010-04-22 14:46:18 -04004369 u32 newsid;
David Howells275bb412008-11-14 10:39:19 +11004370 u16 secclass;
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004371 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372
4373 if (kern)
Paul Moored4f2d972010-04-22 14:46:18 -04004374 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375
David Howells275bb412008-11-14 10:39:19 +11004376 secclass = socket_type_to_security_class(family, type, protocol);
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004377 rc = socket_sockcreate_sid(tsec, secclass, &newsid);
4378 if (rc)
4379 return rc;
4380
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004381 return avc_has_perm(&selinux_state,
4382 tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383}
4384
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07004385static int selinux_socket_post_create(struct socket *sock, int family,
4386 int type, int protocol, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387{
Paul Moore5fb49872010-04-22 14:46:19 -04004388 const struct task_security_struct *tsec = current_security();
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05004389 struct inode_security_struct *isec = inode_security_novalidate(SOCK_INODE(sock));
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004390 struct sk_security_struct *sksec;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004391 u16 sclass = socket_type_to_security_class(family, type, protocol);
4392 u32 sid = SECINITSID_KERNEL;
David Howells275bb412008-11-14 10:39:19 +11004393 int err = 0;
4394
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004395 if (!kern) {
4396 err = socket_sockcreate_sid(tsec, sclass, &sid);
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004397 if (err)
4398 return err;
4399 }
David Howells275bb412008-11-14 10:39:19 +11004400
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004401 isec->sclass = sclass;
4402 isec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004403 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004405 if (sock->sk) {
4406 sksec = sock->sk->sk_security;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004407 sksec->sclass = sclass;
4408 sksec->sid = sid;
Richard Hainesd4529302018-02-13 20:57:18 +00004409 /* Allows detection of the first association on this socket */
4410 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4411 sksec->sctp_assoc_state = SCTP_ASSOC_UNSET;
4412
Paul Moore389fb8002009-03-27 17:10:34 -04004413 err = selinux_netlbl_socket_post_create(sock->sk, family);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004414 }
4415
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07004416 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417}
4418
David Herrmann0b811db2018-05-04 16:28:21 +02004419static int selinux_socket_socketpair(struct socket *socka,
4420 struct socket *sockb)
4421{
4422 struct sk_security_struct *sksec_a = socka->sk->sk_security;
4423 struct sk_security_struct *sksec_b = sockb->sk->sk_security;
4424
4425 sksec_a->peer_sid = sksec_b->sid;
4426 sksec_b->peer_sid = sksec_a->sid;
4427
4428 return 0;
4429}
4430
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431/* Range of port numbers used to automatically bind.
4432 Need to determine whether we should perform a name_bind
4433 permission check between the socket and the port number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434
4435static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
4436{
Paul Moore253bfae2010-04-22 14:46:19 -04004437 struct sock *sk = sock->sk;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004438 struct sk_security_struct *sksec = sk->sk_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 u16 family;
4440 int err;
4441
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004442 err = sock_has_perm(sk, SOCKET__BIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 if (err)
4444 goto out;
4445
Richard Hainesd4529302018-02-13 20:57:18 +00004446 /* If PF_INET or PF_INET6, check name_bind permission for the port. */
Paul Moore253bfae2010-04-22 14:46:19 -04004447 family = sk->sk_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 if (family == PF_INET || family == PF_INET6) {
4449 char *addrp;
Thomas Liu2bf49692009-07-14 12:14:09 -04004450 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004451 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 struct sockaddr_in *addr4 = NULL;
4453 struct sockaddr_in6 *addr6 = NULL;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004454 u16 family_sa = address->sa_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 unsigned short snum;
James Morrise399f982008-06-12 01:39:58 +10004456 u32 sid, node_perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457
Richard Hainesd4529302018-02-13 20:57:18 +00004458 /*
4459 * sctp_bindx(3) calls via selinux_sctp_bind_connect()
4460 * that validates multiple binding addresses. Because of this
4461 * need to check address->sa_family as it is possible to have
4462 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4463 */
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004464 switch (family_sa) {
4465 case AF_UNSPEC:
Richard Haines68741a8a2018-03-02 19:54:34 +00004466 case AF_INET:
4467 if (addrlen < sizeof(struct sockaddr_in))
4468 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 addr4 = (struct sockaddr_in *)address;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004470 if (family_sa == AF_UNSPEC) {
4471 /* see __inet_bind(), we only want to allow
4472 * AF_UNSPEC if the address is INADDR_ANY
4473 */
4474 if (addr4->sin_addr.s_addr != htonl(INADDR_ANY))
4475 goto err_af;
4476 family_sa = AF_INET;
4477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 snum = ntohs(addr4->sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 addrp = (char *)&addr4->sin_addr.s_addr;
Richard Haines68741a8a2018-03-02 19:54:34 +00004480 break;
4481 case AF_INET6:
4482 if (addrlen < SIN6_LEN_RFC2133)
4483 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 addr6 = (struct sockaddr_in6 *)address;
4485 snum = ntohs(addr6->sin6_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 addrp = (char *)&addr6->sin6_addr.s6_addr;
Richard Haines68741a8a2018-03-02 19:54:34 +00004487 break;
4488 default:
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004489 goto err_af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 }
4491
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004492 ad.type = LSM_AUDIT_DATA_NET;
4493 ad.u.net = &net;
4494 ad.u.net->sport = htons(snum);
4495 ad.u.net->family = family_sa;
4496
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004497 if (snum) {
4498 int low, high;
4499
Eric W. Biederman0bbf87d2013-09-28 14:10:59 -07004500 inet_get_local_port_range(sock_net(sk), &low, &high);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004501
Krister Johansen4548b682017-01-20 17:49:11 -08004502 if (snum < max(inet_prot_sock(sock_net(sk)), low) ||
4503 snum > high) {
Paul Moore3e1121722008-04-10 10:48:14 -04004504 err = sel_netport_sid(sk->sk_protocol,
4505 snum, &sid);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004506 if (err)
4507 goto out;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004508 err = avc_has_perm(&selinux_state,
4509 sksec->sid, sid,
Paul Moore253bfae2010-04-22 14:46:19 -04004510 sksec->sclass,
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004511 SOCKET__NAME_BIND, &ad);
4512 if (err)
4513 goto out;
4514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 }
Eric Paris828dfe12008-04-17 13:17:49 -04004516
Paul Moore253bfae2010-04-22 14:46:19 -04004517 switch (sksec->sclass) {
James Morris13402582005-09-30 14:24:34 -04004518 case SECCLASS_TCP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 node_perm = TCP_SOCKET__NODE_BIND;
4520 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004521
James Morris13402582005-09-30 14:24:34 -04004522 case SECCLASS_UDP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523 node_perm = UDP_SOCKET__NODE_BIND;
4524 break;
James Morris2ee92d42006-11-13 16:09:01 -08004525
4526 case SECCLASS_DCCP_SOCKET:
4527 node_perm = DCCP_SOCKET__NODE_BIND;
4528 break;
4529
Richard Hainesd4529302018-02-13 20:57:18 +00004530 case SECCLASS_SCTP_SOCKET:
4531 node_perm = SCTP_SOCKET__NODE_BIND;
4532 break;
4533
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 default:
4535 node_perm = RAWIP_SOCKET__NODE_BIND;
4536 break;
4537 }
Eric Paris828dfe12008-04-17 13:17:49 -04004538
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004539 err = sel_netnode_sid(addrp, family_sa, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540 if (err)
4541 goto out;
Eric Paris828dfe12008-04-17 13:17:49 -04004542
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004543 if (family_sa == AF_INET)
Eric Paris48c62af2012-04-02 13:15:44 -04004544 ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545 else
Eric Paris48c62af2012-04-02 13:15:44 -04004546 ad.u.net->v6info.saddr = addr6->sin6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004548 err = avc_has_perm(&selinux_state,
4549 sksec->sid, sid,
Paul Moore253bfae2010-04-22 14:46:19 -04004550 sksec->sclass, node_perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 if (err)
4552 goto out;
4553 }
4554out:
4555 return err;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004556err_af:
4557 /* Note that SCTP services expect -EINVAL, others -EAFNOSUPPORT. */
4558 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4559 return -EINVAL;
4560 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561}
4562
Richard Hainesd4529302018-02-13 20:57:18 +00004563/* This supports connect(2) and SCTP connect services such as sctp_connectx(3)
Mauro Carvalho Chehab5fb94e92018-05-08 15:14:57 -03004564 * and sctp_sendmsg(3) as described in Documentation/security/LSM-sctp.rst
Richard Hainesd4529302018-02-13 20:57:18 +00004565 */
4566static int selinux_socket_connect_helper(struct socket *sock,
4567 struct sockaddr *address, int addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568{
Paul Moore014ab192008-10-10 10:16:33 -04004569 struct sock *sk = sock->sk;
Paul Moore253bfae2010-04-22 14:46:19 -04004570 struct sk_security_struct *sksec = sk->sk_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 int err;
4572
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004573 err = sock_has_perm(sk, SOCKET__CONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 if (err)
4575 return err;
4576
4577 /*
Richard Hainesd4529302018-02-13 20:57:18 +00004578 * If a TCP, DCCP or SCTP socket, check name_connect permission
4579 * for the port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580 */
Paul Moore253bfae2010-04-22 14:46:19 -04004581 if (sksec->sclass == SECCLASS_TCP_SOCKET ||
Richard Hainesd4529302018-02-13 20:57:18 +00004582 sksec->sclass == SECCLASS_DCCP_SOCKET ||
4583 sksec->sclass == SECCLASS_SCTP_SOCKET) {
Thomas Liu2bf49692009-07-14 12:14:09 -04004584 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004585 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 struct sockaddr_in *addr4 = NULL;
4587 struct sockaddr_in6 *addr6 = NULL;
4588 unsigned short snum;
James Morris2ee92d42006-11-13 16:09:01 -08004589 u32 sid, perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590
Richard Hainesd4529302018-02-13 20:57:18 +00004591 /* sctp_connectx(3) calls via selinux_sctp_bind_connect()
4592 * that validates multiple connect addresses. Because of this
4593 * need to check address->sa_family as it is possible to have
4594 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4595 */
Richard Haines68741a8a2018-03-02 19:54:34 +00004596 switch (address->sa_family) {
4597 case AF_INET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 addr4 = (struct sockaddr_in *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004599 if (addrlen < sizeof(struct sockaddr_in))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600 return -EINVAL;
4601 snum = ntohs(addr4->sin_port);
Richard Haines68741a8a2018-03-02 19:54:34 +00004602 break;
4603 case AF_INET6:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 addr6 = (struct sockaddr_in6 *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004605 if (addrlen < SIN6_LEN_RFC2133)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 return -EINVAL;
4607 snum = ntohs(addr6->sin6_port);
Richard Haines68741a8a2018-03-02 19:54:34 +00004608 break;
4609 default:
4610 /* Note that SCTP services expect -EINVAL, whereas
4611 * others expect -EAFNOSUPPORT.
4612 */
4613 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4614 return -EINVAL;
4615 else
4616 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 }
4618
Paul Moore3e1121722008-04-10 10:48:14 -04004619 err = sel_netport_sid(sk->sk_protocol, snum, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 if (err)
Richard Hainesd4529302018-02-13 20:57:18 +00004621 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622
Richard Hainesd4529302018-02-13 20:57:18 +00004623 switch (sksec->sclass) {
4624 case SECCLASS_TCP_SOCKET:
4625 perm = TCP_SOCKET__NAME_CONNECT;
4626 break;
4627 case SECCLASS_DCCP_SOCKET:
4628 perm = DCCP_SOCKET__NAME_CONNECT;
4629 break;
4630 case SECCLASS_SCTP_SOCKET:
4631 perm = SCTP_SOCKET__NAME_CONNECT;
4632 break;
4633 }
James Morris2ee92d42006-11-13 16:09:01 -08004634
Eric Paris50c205f2012-04-04 15:01:43 -04004635 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004636 ad.u.net = &net;
4637 ad.u.net->dport = htons(snum);
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004638 ad.u.net->family = address->sa_family;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004639 err = avc_has_perm(&selinux_state,
4640 sksec->sid, sid, sksec->sclass, perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 if (err)
Richard Hainesd4529302018-02-13 20:57:18 +00004642 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 }
4644
Richard Hainesd4529302018-02-13 20:57:18 +00004645 return 0;
4646}
Paul Moore014ab192008-10-10 10:16:33 -04004647
Richard Hainesd4529302018-02-13 20:57:18 +00004648/* Supports connect(2), see comments in selinux_socket_connect_helper() */
4649static int selinux_socket_connect(struct socket *sock,
4650 struct sockaddr *address, int addrlen)
4651{
4652 int err;
4653 struct sock *sk = sock->sk;
4654
4655 err = selinux_socket_connect_helper(sock, address, addrlen);
4656 if (err)
4657 return err;
4658
4659 return selinux_netlbl_socket_connect(sk, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660}
4661
4662static int selinux_socket_listen(struct socket *sock, int backlog)
4663{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004664 return sock_has_perm(sock->sk, SOCKET__LISTEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665}
4666
4667static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
4668{
4669 int err;
4670 struct inode_security_struct *isec;
4671 struct inode_security_struct *newisec;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004672 u16 sclass;
4673 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004675 err = sock_has_perm(sock->sk, SOCKET__ACCEPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676 if (err)
4677 return err;
4678
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05004679 isec = inode_security_novalidate(SOCK_INODE(sock));
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004680 spin_lock(&isec->lock);
4681 sclass = isec->sclass;
4682 sid = isec->sid;
4683 spin_unlock(&isec->lock);
4684
4685 newisec = inode_security_novalidate(SOCK_INODE(newsock));
4686 newisec->sclass = sclass;
4687 newisec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004688 newisec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689
4690 return 0;
4691}
4692
4693static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
Eric Paris828dfe12008-04-17 13:17:49 -04004694 int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004696 return sock_has_perm(sock->sk, SOCKET__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697}
4698
4699static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
4700 int size, int flags)
4701{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004702 return sock_has_perm(sock->sk, SOCKET__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703}
4704
4705static int selinux_socket_getsockname(struct socket *sock)
4706{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004707 return sock_has_perm(sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708}
4709
4710static int selinux_socket_getpeername(struct socket *sock)
4711{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004712 return sock_has_perm(sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713}
4714
Eric Paris828dfe12008-04-17 13:17:49 -04004715static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716{
Paul Mooref8687af2006-10-30 15:22:15 -08004717 int err;
4718
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004719 err = sock_has_perm(sock->sk, SOCKET__SETOPT);
Paul Mooref8687af2006-10-30 15:22:15 -08004720 if (err)
4721 return err;
4722
4723 return selinux_netlbl_socket_setsockopt(sock, level, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724}
4725
4726static int selinux_socket_getsockopt(struct socket *sock, int level,
4727 int optname)
4728{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004729 return sock_has_perm(sock->sk, SOCKET__GETOPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730}
4731
4732static int selinux_socket_shutdown(struct socket *sock, int how)
4733{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004734 return sock_has_perm(sock->sk, SOCKET__SHUTDOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735}
4736
David S. Miller3610cda2011-01-05 15:38:53 -08004737static int selinux_socket_unix_stream_connect(struct sock *sock,
4738 struct sock *other,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 struct sock *newsk)
4740{
David S. Miller3610cda2011-01-05 15:38:53 -08004741 struct sk_security_struct *sksec_sock = sock->sk_security;
4742 struct sk_security_struct *sksec_other = other->sk_security;
Paul Moore4d1e2452010-04-22 14:46:18 -04004743 struct sk_security_struct *sksec_new = newsk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004744 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004745 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 int err;
4747
Eric Paris50c205f2012-04-04 15:01:43 -04004748 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004749 ad.u.net = &net;
4750 ad.u.net->sk = other;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004752 err = avc_has_perm(&selinux_state,
4753 sksec_sock->sid, sksec_other->sid,
Paul Moore4d1e2452010-04-22 14:46:18 -04004754 sksec_other->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 UNIX_STREAM_SOCKET__CONNECTTO, &ad);
4756 if (err)
4757 return err;
4758
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 /* server child socket */
Paul Moore4d1e2452010-04-22 14:46:18 -04004760 sksec_new->peer_sid = sksec_sock->sid;
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004761 err = security_sid_mls_copy(&selinux_state, sksec_other->sid,
4762 sksec_sock->sid, &sksec_new->sid);
Paul Moore4d1e2452010-04-22 14:46:18 -04004763 if (err)
4764 return err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004765
Paul Moore4d1e2452010-04-22 14:46:18 -04004766 /* connecting socket */
4767 sksec_sock->peer_sid = sksec_new->sid;
4768
4769 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770}
4771
4772static int selinux_socket_unix_may_send(struct socket *sock,
4773 struct socket *other)
4774{
Paul Moore253bfae2010-04-22 14:46:19 -04004775 struct sk_security_struct *ssec = sock->sk->sk_security;
4776 struct sk_security_struct *osec = other->sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004777 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004778 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779
Eric Paris50c205f2012-04-04 15:01:43 -04004780 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004781 ad.u.net = &net;
4782 ad.u.net->sk = other->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004784 return avc_has_perm(&selinux_state,
4785 ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
Paul Moore253bfae2010-04-22 14:46:19 -04004786 &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787}
4788
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004789static int selinux_inet_sys_rcv_skb(struct net *ns, int ifindex,
4790 char *addrp, u16 family, u32 peer_sid,
Thomas Liu2bf49692009-07-14 12:14:09 -04004791 struct common_audit_data *ad)
Paul Mooreeffad8d2008-01-29 08:49:27 -05004792{
4793 int err;
4794 u32 if_sid;
4795 u32 node_sid;
4796
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004797 err = sel_netif_sid(ns, ifindex, &if_sid);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004798 if (err)
4799 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004800 err = avc_has_perm(&selinux_state,
4801 peer_sid, if_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004802 SECCLASS_NETIF, NETIF__INGRESS, ad);
4803 if (err)
4804 return err;
4805
4806 err = sel_netnode_sid(addrp, family, &node_sid);
4807 if (err)
4808 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004809 return avc_has_perm(&selinux_state,
4810 peer_sid, node_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004811 SECCLASS_NODE, NODE__RECVFROM, ad);
4812}
4813
Paul Moore220deb92008-01-29 08:38:23 -05004814static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
Paul Moored8395c82008-10-10 10:16:30 -04004815 u16 family)
Paul Moore220deb92008-01-29 08:38:23 -05004816{
Paul Moore277d3422008-12-31 12:54:11 -05004817 int err = 0;
Paul Moore220deb92008-01-29 08:38:23 -05004818 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004819 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004820 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004821 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04004822 char *addrp;
4823
Eric Paris50c205f2012-04-04 15:01:43 -04004824 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004825 ad.u.net = &net;
4826 ad.u.net->netif = skb->skb_iif;
4827 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004828 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4829 if (err)
4830 return err;
Paul Moore220deb92008-01-29 08:38:23 -05004831
Paul Moore58bfbb52009-03-27 17:10:41 -04004832 if (selinux_secmark_enabled()) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004833 err = avc_has_perm(&selinux_state,
4834 sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Moored8395c82008-10-10 10:16:30 -04004835 PACKET__RECV, &ad);
Paul Moore58bfbb52009-03-27 17:10:41 -04004836 if (err)
4837 return err;
4838 }
Paul Moore220deb92008-01-29 08:38:23 -05004839
Steffen Klassertb9679a72011-02-23 12:55:21 +01004840 err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
4841 if (err)
4842 return err;
4843 err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004844
James Morris4e5ab4c2006-06-09 00:33:33 -07004845 return err;
4846}
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004847
James Morris4e5ab4c2006-06-09 00:33:33 -07004848static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
4849{
Paul Moore220deb92008-01-29 08:38:23 -05004850 int err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004851 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004852 u16 family = sk->sk_family;
4853 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004854 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004855 struct lsm_network_audit net = {0,};
Paul Moore220deb92008-01-29 08:38:23 -05004856 char *addrp;
Paul Moored8395c82008-10-10 10:16:30 -04004857 u8 secmark_active;
4858 u8 peerlbl_active;
James Morris4e5ab4c2006-06-09 00:33:33 -07004859
James Morris4e5ab4c2006-06-09 00:33:33 -07004860 if (family != PF_INET && family != PF_INET6)
Paul Moore220deb92008-01-29 08:38:23 -05004861 return 0;
James Morris4e5ab4c2006-06-09 00:33:33 -07004862
4863 /* Handle mapped IPv4 packets arriving via IPv6 sockets */
Al Viro87fcd702006-12-04 22:00:55 +00004864 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
James Morris4e5ab4c2006-06-09 00:33:33 -07004865 family = PF_INET;
4866
Paul Moored8395c82008-10-10 10:16:30 -04004867 /* If any sort of compatibility mode is enabled then handoff processing
4868 * to the selinux_sock_rcv_skb_compat() function to deal with the
4869 * special handling. We do this in an attempt to keep this function
4870 * as fast and as clean as possible. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004871 if (!selinux_policycap_netpeer())
Paul Moored8395c82008-10-10 10:16:30 -04004872 return selinux_sock_rcv_skb_compat(sk, skb, family);
4873
4874 secmark_active = selinux_secmark_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04004875 peerlbl_active = selinux_peerlbl_enabled();
Paul Moored8395c82008-10-10 10:16:30 -04004876 if (!secmark_active && !peerlbl_active)
4877 return 0;
4878
Eric Paris50c205f2012-04-04 15:01:43 -04004879 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004880 ad.u.net = &net;
4881 ad.u.net->netif = skb->skb_iif;
4882 ad.u.net->family = family;
Paul Moore224dfbd2008-01-29 08:38:13 -05004883 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
James Morris4e5ab4c2006-06-09 00:33:33 -07004884 if (err)
Paul Moore220deb92008-01-29 08:38:23 -05004885 return err;
James Morris4e5ab4c2006-06-09 00:33:33 -07004886
Paul Moored8395c82008-10-10 10:16:30 -04004887 if (peerlbl_active) {
Paul Moored621d352008-01-29 08:43:36 -05004888 u32 peer_sid;
4889
4890 err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
4891 if (err)
4892 return err;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004893 err = selinux_inet_sys_rcv_skb(sock_net(sk), skb->skb_iif,
4894 addrp, family, peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04004895 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04004896 selinux_netlbl_err(skb, family, err, 0);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004897 return err;
Paul Mooredfaebe92008-10-10 10:16:31 -04004898 }
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004899 err = avc_has_perm(&selinux_state,
4900 sk_sid, peer_sid, SECCLASS_PEER,
Paul Moored621d352008-01-29 08:43:36 -05004901 PEER__RECV, &ad);
Chad Hanson46d01d62013-12-23 17:45:01 -05004902 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04004903 selinux_netlbl_err(skb, family, err, 0);
Chad Hanson46d01d62013-12-23 17:45:01 -05004904 return err;
4905 }
Paul Moored621d352008-01-29 08:43:36 -05004906 }
4907
Paul Moored8395c82008-10-10 10:16:30 -04004908 if (secmark_active) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004909 err = avc_has_perm(&selinux_state,
4910 sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004911 PACKET__RECV, &ad);
4912 if (err)
4913 return err;
4914 }
4915
Paul Moored621d352008-01-29 08:43:36 -05004916 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917}
4918
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004919static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
4920 int __user *optlen, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921{
4922 int err = 0;
4923 char *scontext;
4924 u32 scontext_len;
Paul Moore253bfae2010-04-22 14:46:19 -04004925 struct sk_security_struct *sksec = sock->sk->sk_security;
Paul Moore3de4bab2006-11-17 17:38:54 -05004926 u32 peer_sid = SECSID_NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927
Paul Moore253bfae2010-04-22 14:46:19 -04004928 if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
Richard Hainesd4529302018-02-13 20:57:18 +00004929 sksec->sclass == SECCLASS_TCP_SOCKET ||
4930 sksec->sclass == SECCLASS_SCTP_SOCKET)
Eric Parisdd3e7832010-04-07 15:08:46 -04004931 peer_sid = sksec->peer_sid;
Paul Moore253bfae2010-04-22 14:46:19 -04004932 if (peer_sid == SECSID_NULL)
4933 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004935 err = security_sid_to_context(&selinux_state, peer_sid, &scontext,
4936 &scontext_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 if (err)
Paul Moore253bfae2010-04-22 14:46:19 -04004938 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939
4940 if (scontext_len > len) {
4941 err = -ERANGE;
4942 goto out_len;
4943 }
4944
4945 if (copy_to_user(optval, scontext, scontext_len))
4946 err = -EFAULT;
4947
4948out_len:
4949 if (put_user(scontext_len, optlen))
4950 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 kfree(scontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952 return err;
4953}
4954
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004955static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004956{
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004957 u32 peer_secid = SECSID_NULL;
Paul Moore75e22912008-01-29 08:38:04 -05004958 u16 family;
Paul Moore899134f2016-03-28 15:19:10 -04004959 struct inode_security_struct *isec;
Catherine Zhang877ce7c2006-06-29 12:27:47 -07004960
Paul Mooreaa862902008-10-10 10:16:29 -04004961 if (skb && skb->protocol == htons(ETH_P_IP))
4962 family = PF_INET;
4963 else if (skb && skb->protocol == htons(ETH_P_IPV6))
4964 family = PF_INET6;
4965 else if (sock)
Paul Moore75e22912008-01-29 08:38:04 -05004966 family = sock->sk->sk_family;
Paul Moore75e22912008-01-29 08:38:04 -05004967 else
4968 goto out;
4969
Paul Moore899134f2016-03-28 15:19:10 -04004970 if (sock && family == PF_UNIX) {
4971 isec = inode_security_novalidate(SOCK_INODE(sock));
4972 peer_secid = isec->sid;
4973 } else if (skb)
Paul Moore220deb92008-01-29 08:38:23 -05004974 selinux_skb_peerlbl_sid(skb, family, &peer_secid);
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004975
Paul Moore75e22912008-01-29 08:38:04 -05004976out:
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004977 *secid = peer_secid;
Paul Moore75e22912008-01-29 08:38:04 -05004978 if (peer_secid == SECSID_NULL)
4979 return -EINVAL;
4980 return 0;
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004981}
4982
Al Viro7d877f32005-10-21 03:20:43 -04004983static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984{
Paul Moore84914b72010-04-22 14:46:18 -04004985 struct sk_security_struct *sksec;
4986
4987 sksec = kzalloc(sizeof(*sksec), priority);
4988 if (!sksec)
4989 return -ENOMEM;
4990
4991 sksec->peer_sid = SECINITSID_UNLABELED;
4992 sksec->sid = SECINITSID_UNLABELED;
Stephen Smalley5dee25d2015-07-10 17:19:57 -04004993 sksec->sclass = SECCLASS_SOCKET;
Paul Moore84914b72010-04-22 14:46:18 -04004994 selinux_netlbl_sk_security_reset(sksec);
4995 sk->sk_security = sksec;
4996
4997 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998}
4999
5000static void selinux_sk_free_security(struct sock *sk)
5001{
Paul Moore84914b72010-04-22 14:46:18 -04005002 struct sk_security_struct *sksec = sk->sk_security;
5003
5004 sk->sk_security = NULL;
5005 selinux_netlbl_sk_security_free(sksec);
5006 kfree(sksec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007}
5008
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005009static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
5010{
Eric Parisdd3e7832010-04-07 15:08:46 -04005011 struct sk_security_struct *sksec = sk->sk_security;
5012 struct sk_security_struct *newsksec = newsk->sk_security;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005013
Eric Parisdd3e7832010-04-07 15:08:46 -04005014 newsksec->sid = sksec->sid;
5015 newsksec->peer_sid = sksec->peer_sid;
5016 newsksec->sclass = sksec->sclass;
Paul Moore99f59ed2006-08-29 17:53:48 -07005017
Eric Parisdd3e7832010-04-07 15:08:46 -04005018 selinux_netlbl_sk_security_reset(newsksec);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005019}
5020
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07005021static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005022{
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005023 if (!sk)
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07005024 *secid = SECINITSID_ANY_SOCKET;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005025 else {
5026 struct sk_security_struct *sksec = sk->sk_security;
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005027
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07005028 *secid = sksec->sid;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005029 }
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005030}
5031
Eric Paris828dfe12008-04-17 13:17:49 -04005032static void selinux_sock_graft(struct sock *sk, struct socket *parent)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005033{
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05005034 struct inode_security_struct *isec =
5035 inode_security_novalidate(SOCK_INODE(parent));
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005036 struct sk_security_struct *sksec = sk->sk_security;
5037
Paul Moore2873ead2014-07-28 10:42:48 -04005038 if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
5039 sk->sk_family == PF_UNIX)
David Woodhouse2148ccc2006-09-29 15:50:25 -07005040 isec->sid = sksec->sid;
Paul Moore220deb92008-01-29 08:38:23 -05005041 sksec->sclass = isec->sclass;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005042}
5043
Richard Hainesd4529302018-02-13 20:57:18 +00005044/* Called whenever SCTP receives an INIT chunk. This happens when an incoming
5045 * connect(2), sctp_connectx(3) or sctp_sendmsg(3) (with no association
5046 * already present).
5047 */
5048static int selinux_sctp_assoc_request(struct sctp_endpoint *ep,
5049 struct sk_buff *skb)
5050{
5051 struct sk_security_struct *sksec = ep->base.sk->sk_security;
5052 struct common_audit_data ad;
5053 struct lsm_network_audit net = {0,};
5054 u8 peerlbl_active;
5055 u32 peer_sid = SECINITSID_UNLABELED;
5056 u32 conn_sid;
5057 int err = 0;
5058
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005059 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005060 return 0;
5061
5062 peerlbl_active = selinux_peerlbl_enabled();
5063
5064 if (peerlbl_active) {
5065 /* This will return peer_sid = SECSID_NULL if there are
5066 * no peer labels, see security_net_peersid_resolve().
5067 */
5068 err = selinux_skb_peerlbl_sid(skb, ep->base.sk->sk_family,
5069 &peer_sid);
5070 if (err)
5071 return err;
5072
5073 if (peer_sid == SECSID_NULL)
5074 peer_sid = SECINITSID_UNLABELED;
5075 }
5076
5077 if (sksec->sctp_assoc_state == SCTP_ASSOC_UNSET) {
5078 sksec->sctp_assoc_state = SCTP_ASSOC_SET;
5079
5080 /* Here as first association on socket. As the peer SID
5081 * was allowed by peer recv (and the netif/node checks),
5082 * then it is approved by policy and used as the primary
5083 * peer SID for getpeercon(3).
5084 */
5085 sksec->peer_sid = peer_sid;
5086 } else if (sksec->peer_sid != peer_sid) {
5087 /* Other association peer SIDs are checked to enforce
5088 * consistency among the peer SIDs.
5089 */
5090 ad.type = LSM_AUDIT_DATA_NET;
5091 ad.u.net = &net;
5092 ad.u.net->sk = ep->base.sk;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005093 err = avc_has_perm(&selinux_state,
5094 sksec->peer_sid, peer_sid, sksec->sclass,
Richard Hainesd4529302018-02-13 20:57:18 +00005095 SCTP_SOCKET__ASSOCIATION, &ad);
5096 if (err)
5097 return err;
5098 }
5099
5100 /* Compute the MLS component for the connection and store
5101 * the information in ep. This will be used by SCTP TCP type
5102 * sockets and peeled off connections as they cause a new
5103 * socket to be generated. selinux_sctp_sk_clone() will then
5104 * plug this into the new socket.
5105 */
5106 err = selinux_conn_sid(sksec->sid, peer_sid, &conn_sid);
5107 if (err)
5108 return err;
5109
5110 ep->secid = conn_sid;
5111 ep->peer_secid = peer_sid;
5112
5113 /* Set any NetLabel labels including CIPSO/CALIPSO options. */
5114 return selinux_netlbl_sctp_assoc_request(ep, skb);
5115}
5116
5117/* Check if sctp IPv4/IPv6 addresses are valid for binding or connecting
5118 * based on their @optname.
5119 */
5120static int selinux_sctp_bind_connect(struct sock *sk, int optname,
5121 struct sockaddr *address,
5122 int addrlen)
5123{
5124 int len, err = 0, walk_size = 0;
5125 void *addr_buf;
5126 struct sockaddr *addr;
5127 struct socket *sock;
5128
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005129 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005130 return 0;
5131
5132 /* Process one or more addresses that may be IPv4 or IPv6 */
5133 sock = sk->sk_socket;
5134 addr_buf = address;
5135
5136 while (walk_size < addrlen) {
Ondrej Mosnacekc1383252018-11-13 16:16:08 +01005137 if (walk_size + sizeof(sa_family_t) > addrlen)
5138 return -EINVAL;
5139
Richard Hainesd4529302018-02-13 20:57:18 +00005140 addr = addr_buf;
5141 switch (addr->sa_family) {
Alexey Kodanev4152dc92018-05-11 20:15:13 +03005142 case AF_UNSPEC:
Richard Hainesd4529302018-02-13 20:57:18 +00005143 case AF_INET:
5144 len = sizeof(struct sockaddr_in);
5145 break;
5146 case AF_INET6:
5147 len = sizeof(struct sockaddr_in6);
5148 break;
5149 default:
Alexey Kodanev4152dc92018-05-11 20:15:13 +03005150 return -EINVAL;
Richard Hainesd4529302018-02-13 20:57:18 +00005151 }
5152
Xin Long292c9972019-03-09 00:07:34 +08005153 if (walk_size + len > addrlen)
5154 return -EINVAL;
5155
Richard Hainesd4529302018-02-13 20:57:18 +00005156 err = -EINVAL;
5157 switch (optname) {
5158 /* Bind checks */
5159 case SCTP_PRIMARY_ADDR:
5160 case SCTP_SET_PEER_PRIMARY_ADDR:
5161 case SCTP_SOCKOPT_BINDX_ADD:
5162 err = selinux_socket_bind(sock, addr, len);
5163 break;
5164 /* Connect checks */
5165 case SCTP_SOCKOPT_CONNECTX:
5166 case SCTP_PARAM_SET_PRIMARY:
5167 case SCTP_PARAM_ADD_IP:
5168 case SCTP_SENDMSG_CONNECT:
5169 err = selinux_socket_connect_helper(sock, addr, len);
5170 if (err)
5171 return err;
5172
5173 /* As selinux_sctp_bind_connect() is called by the
5174 * SCTP protocol layer, the socket is already locked,
5175 * therefore selinux_netlbl_socket_connect_locked() is
5176 * is called here. The situations handled are:
5177 * sctp_connectx(3), sctp_sendmsg(3), sendmsg(2),
5178 * whenever a new IP address is added or when a new
5179 * primary address is selected.
5180 * Note that an SCTP connect(2) call happens before
5181 * the SCTP protocol layer and is handled via
5182 * selinux_socket_connect().
5183 */
5184 err = selinux_netlbl_socket_connect_locked(sk, addr);
5185 break;
5186 }
5187
5188 if (err)
5189 return err;
5190
5191 addr_buf += len;
5192 walk_size += len;
5193 }
5194
5195 return 0;
5196}
5197
5198/* Called whenever a new socket is created by accept(2) or sctp_peeloff(3). */
5199static void selinux_sctp_sk_clone(struct sctp_endpoint *ep, struct sock *sk,
5200 struct sock *newsk)
5201{
5202 struct sk_security_struct *sksec = sk->sk_security;
5203 struct sk_security_struct *newsksec = newsk->sk_security;
5204
5205 /* If policy does not support SECCLASS_SCTP_SOCKET then call
5206 * the non-sctp clone version.
5207 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005208 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005209 return selinux_sk_clone_security(sk, newsk);
5210
5211 newsksec->sid = ep->secid;
5212 newsksec->peer_sid = ep->peer_secid;
5213 newsksec->sclass = sksec->sclass;
5214 selinux_netlbl_sctp_sk_clone(sk, newsk);
5215}
5216
Adrian Bunk9a673e52006-08-15 00:03:53 -07005217static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
5218 struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005219{
5220 struct sk_security_struct *sksec = sk->sk_security;
5221 int err;
Paul Moore0b1f24e2013-12-03 11:39:13 -05005222 u16 family = req->rsk_ops->family;
Paul Moore446b8022013-12-04 16:10:51 -05005223 u32 connsid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005224 u32 peersid;
5225
Paul Mooreaa862902008-10-10 10:16:29 -04005226 err = selinux_skb_peerlbl_sid(skb, family, &peersid);
Paul Moore220deb92008-01-29 08:38:23 -05005227 if (err)
5228 return err;
Paul Moore446b8022013-12-04 16:10:51 -05005229 err = selinux_conn_sid(sksec->sid, peersid, &connsid);
5230 if (err)
5231 return err;
5232 req->secid = connsid;
5233 req->peer_secid = peersid;
Venkat Yekkiralaa51c64f2006-07-27 22:01:34 -07005234
Paul Moore389fb8002009-03-27 17:10:34 -04005235 return selinux_netlbl_inet_conn_request(req, family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005236}
5237
Adrian Bunk9a673e52006-08-15 00:03:53 -07005238static void selinux_inet_csk_clone(struct sock *newsk,
5239 const struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005240{
5241 struct sk_security_struct *newsksec = newsk->sk_security;
5242
5243 newsksec->sid = req->secid;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005244 newsksec->peer_sid = req->peer_secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005245 /* NOTE: Ideally, we should also get the isec->sid for the
5246 new socket in sync, but we don't have the isec available yet.
5247 So we will wait until sock_graft to do it, by which
5248 time it will have been created and available. */
Paul Moore99f59ed2006-08-29 17:53:48 -07005249
Paul Moore9f2ad662006-11-17 17:38:53 -05005250 /* We don't need to take any sort of lock here as we are the only
5251 * thread with access to newsksec */
Paul Moore389fb8002009-03-27 17:10:34 -04005252 selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005253}
5254
Paul Moore014ab192008-10-10 10:16:33 -04005255static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005256{
Paul Mooreaa862902008-10-10 10:16:29 -04005257 u16 family = sk->sk_family;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005258 struct sk_security_struct *sksec = sk->sk_security;
5259
Paul Mooreaa862902008-10-10 10:16:29 -04005260 /* handle mapped IPv4 packets arriving via IPv6 sockets */
5261 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
5262 family = PF_INET;
5263
5264 selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005265}
5266
Eric Paris2606fd12010-10-13 16:24:41 -04005267static int selinux_secmark_relabel_packet(u32 sid)
5268{
5269 const struct task_security_struct *__tsec;
5270 u32 tsid;
5271
5272 __tsec = current_security();
5273 tsid = __tsec->sid;
5274
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005275 return avc_has_perm(&selinux_state,
5276 tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO,
5277 NULL);
Eric Paris2606fd12010-10-13 16:24:41 -04005278}
5279
5280static void selinux_secmark_refcount_inc(void)
5281{
5282 atomic_inc(&selinux_secmark_refcount);
5283}
5284
5285static void selinux_secmark_refcount_dec(void)
5286{
5287 atomic_dec(&selinux_secmark_refcount);
5288}
5289
Adrian Bunk9a673e52006-08-15 00:03:53 -07005290static void selinux_req_classify_flow(const struct request_sock *req,
5291 struct flowi *fl)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005292{
David S. Miller1d28f422011-03-12 00:29:39 -05005293 fl->flowi_secid = req->secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005294}
5295
Paul Moore5dbbaf22013-01-14 07:12:19 +00005296static int selinux_tun_dev_alloc_security(void **security)
5297{
5298 struct tun_security_struct *tunsec;
5299
5300 tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL);
5301 if (!tunsec)
5302 return -ENOMEM;
5303 tunsec->sid = current_sid();
5304
5305 *security = tunsec;
5306 return 0;
5307}
5308
5309static void selinux_tun_dev_free_security(void *security)
5310{
5311 kfree(security);
5312}
5313
Paul Mooreed6d76e2009-08-28 18:12:49 -04005314static int selinux_tun_dev_create(void)
5315{
5316 u32 sid = current_sid();
5317
5318 /* we aren't taking into account the "sockcreate" SID since the socket
5319 * that is being created here is not a socket in the traditional sense,
5320 * instead it is a private sock, accessible only to the kernel, and
5321 * representing a wide range of network traffic spanning multiple
5322 * connections unlike traditional sockets - check the TUN driver to
5323 * get a better understanding of why this socket is special */
5324
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005325 return avc_has_perm(&selinux_state,
5326 sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005327 NULL);
5328}
5329
Paul Moore5dbbaf22013-01-14 07:12:19 +00005330static int selinux_tun_dev_attach_queue(void *security)
Paul Mooreed6d76e2009-08-28 18:12:49 -04005331{
Paul Moore5dbbaf22013-01-14 07:12:19 +00005332 struct tun_security_struct *tunsec = security;
5333
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005334 return avc_has_perm(&selinux_state,
5335 current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET,
Paul Moore5dbbaf22013-01-14 07:12:19 +00005336 TUN_SOCKET__ATTACH_QUEUE, NULL);
5337}
5338
5339static int selinux_tun_dev_attach(struct sock *sk, void *security)
5340{
5341 struct tun_security_struct *tunsec = security;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005342 struct sk_security_struct *sksec = sk->sk_security;
5343
5344 /* we don't currently perform any NetLabel based labeling here and it
5345 * isn't clear that we would want to do so anyway; while we could apply
5346 * labeling without the support of the TUN user the resulting labeled
5347 * traffic from the other end of the connection would almost certainly
5348 * cause confusion to the TUN user that had no idea network labeling
5349 * protocols were being used */
5350
Paul Moore5dbbaf22013-01-14 07:12:19 +00005351 sksec->sid = tunsec->sid;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005352 sksec->sclass = SECCLASS_TUN_SOCKET;
Paul Moore5dbbaf22013-01-14 07:12:19 +00005353
5354 return 0;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005355}
5356
Paul Moore5dbbaf22013-01-14 07:12:19 +00005357static int selinux_tun_dev_open(void *security)
Paul Mooreed6d76e2009-08-28 18:12:49 -04005358{
Paul Moore5dbbaf22013-01-14 07:12:19 +00005359 struct tun_security_struct *tunsec = security;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005360 u32 sid = current_sid();
5361 int err;
5362
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005363 err = avc_has_perm(&selinux_state,
5364 sid, tunsec->sid, SECCLASS_TUN_SOCKET,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005365 TUN_SOCKET__RELABELFROM, NULL);
5366 if (err)
5367 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005368 err = avc_has_perm(&selinux_state,
5369 sid, sid, SECCLASS_TUN_SOCKET,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005370 TUN_SOCKET__RELABELTO, NULL);
5371 if (err)
5372 return err;
Paul Moore5dbbaf22013-01-14 07:12:19 +00005373 tunsec->sid = sid;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005374
5375 return 0;
5376}
5377
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
5379{
5380 int err = 0;
5381 u32 perm;
5382 struct nlmsghdr *nlh;
Paul Moore253bfae2010-04-22 14:46:19 -04005383 struct sk_security_struct *sksec = sk->sk_security;
Eric Paris828dfe12008-04-17 13:17:49 -04005384
Hong zhi guo77954982013-03-27 06:49:35 +00005385 if (skb->len < NLMSG_HDRLEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386 err = -EINVAL;
5387 goto out;
5388 }
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -07005389 nlh = nlmsg_hdr(skb);
Eric Paris828dfe12008-04-17 13:17:49 -04005390
Paul Moore253bfae2010-04-22 14:46:19 -04005391 err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392 if (err) {
5393 if (err == -EINVAL) {
Vladis Dronov76319942015-12-24 11:09:41 -05005394 pr_warn_ratelimited("SELinux: unrecognized netlink"
5395 " message: protocol=%hu nlmsg_type=%hu sclass=%s"
5396 " pig=%d comm=%s\n",
Marek Milkoviccded3ff2015-06-04 16:22:16 -04005397 sk->sk_protocol, nlh->nlmsg_type,
Vladis Dronov76319942015-12-24 11:09:41 -05005398 secclass_map[sksec->sclass - 1].name,
5399 task_pid_nr(current), current->comm);
Paul Mooree5a5ca92018-03-01 17:38:30 -05005400 if (!enforcing_enabled(&selinux_state) ||
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005401 security_get_allow_unknown(&selinux_state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402 err = 0;
5403 }
5404
5405 /* Ignore */
5406 if (err == -ENOENT)
5407 err = 0;
5408 goto out;
5409 }
5410
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005411 err = sock_has_perm(sk, perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412out:
5413 return err;
5414}
5415
5416#ifdef CONFIG_NETFILTER
5417
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005418static unsigned int selinux_ip_forward(struct sk_buff *skb,
5419 const struct net_device *indev,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005420 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421{
Paul Mooredfaebe92008-10-10 10:16:31 -04005422 int err;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005423 char *addrp;
5424 u32 peer_sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04005425 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005426 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05005427 u8 secmark_active;
Paul Moore948bf852008-10-10 10:16:32 -04005428 u8 netlbl_active;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005429 u8 peerlbl_active;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005430
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005431 if (!selinux_policycap_netpeer())
Paul Mooreeffad8d2008-01-29 08:49:27 -05005432 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005433
Paul Mooreeffad8d2008-01-29 08:49:27 -05005434 secmark_active = selinux_secmark_enabled();
Paul Moore948bf852008-10-10 10:16:32 -04005435 netlbl_active = netlbl_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04005436 peerlbl_active = selinux_peerlbl_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05005437 if (!secmark_active && !peerlbl_active)
5438 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005439
Paul Moored8395c82008-10-10 10:16:30 -04005440 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
5441 return NF_DROP;
5442
Eric Paris50c205f2012-04-04 15:01:43 -04005443 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005444 ad.u.net = &net;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005445 ad.u.net->netif = indev->ifindex;
Eric Paris48c62af2012-04-02 13:15:44 -04005446 ad.u.net->family = family;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005447 if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
5448 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449
Paul Mooredfaebe92008-10-10 10:16:31 -04005450 if (peerlbl_active) {
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005451 err = selinux_inet_sys_rcv_skb(dev_net(indev), indev->ifindex,
5452 addrp, family, peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04005453 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04005454 selinux_netlbl_err(skb, family, err, 1);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005455 return NF_DROP;
Paul Mooredfaebe92008-10-10 10:16:31 -04005456 }
5457 }
Paul Mooreeffad8d2008-01-29 08:49:27 -05005458
5459 if (secmark_active)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005460 if (avc_has_perm(&selinux_state,
5461 peer_sid, skb->secmark,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005462 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
5463 return NF_DROP;
5464
Paul Moore948bf852008-10-10 10:16:32 -04005465 if (netlbl_active)
5466 /* we do this in the FORWARD path and not the POST_ROUTING
5467 * path because we want to make sure we apply the necessary
5468 * labeling before IPsec is applied so we can leverage AH
5469 * protection */
5470 if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
5471 return NF_DROP;
5472
Paul Mooreeffad8d2008-01-29 08:49:27 -05005473 return NF_ACCEPT;
5474}
5475
Eric W. Biederman06198b32015-09-18 14:33:06 -05005476static unsigned int selinux_ipv4_forward(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005477 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005478 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005479{
David S. Miller238e54c2015-04-03 20:32:56 -04005480 return selinux_ip_forward(skb, state->in, PF_INET);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005481}
5482
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005483#if IS_ENABLED(CONFIG_IPV6)
Eric W. Biederman06198b32015-09-18 14:33:06 -05005484static unsigned int selinux_ipv6_forward(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005485 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005486 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005487{
David S. Miller238e54c2015-04-03 20:32:56 -04005488 return selinux_ip_forward(skb, state->in, PF_INET6);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005489}
5490#endif /* IPV6 */
5491
Paul Moore948bf852008-10-10 10:16:32 -04005492static unsigned int selinux_ip_output(struct sk_buff *skb,
5493 u16 family)
5494{
Paul Moore47180062013-12-04 16:10:45 -05005495 struct sock *sk;
Paul Moore948bf852008-10-10 10:16:32 -04005496 u32 sid;
5497
5498 if (!netlbl_enabled())
5499 return NF_ACCEPT;
5500
5501 /* we do this in the LOCAL_OUT path and not the POST_ROUTING path
5502 * because we want to make sure we apply the necessary labeling
5503 * before IPsec is applied so we can leverage AH protection */
Paul Moore47180062013-12-04 16:10:45 -05005504 sk = skb->sk;
5505 if (sk) {
5506 struct sk_security_struct *sksec;
5507
Eric Dumazete446f9d2015-10-08 05:01:55 -07005508 if (sk_listener(sk))
Paul Moore47180062013-12-04 16:10:45 -05005509 /* if the socket is the listening state then this
5510 * packet is a SYN-ACK packet which means it needs to
5511 * be labeled based on the connection/request_sock and
5512 * not the parent socket. unfortunately, we can't
5513 * lookup the request_sock yet as it isn't queued on
5514 * the parent socket until after the SYN-ACK is sent.
5515 * the "solution" is to simply pass the packet as-is
5516 * as any IP option based labeling should be copied
5517 * from the initial connection request (in the IP
5518 * layer). it is far from ideal, but until we get a
5519 * security label in the packet itself this is the
5520 * best we can do. */
5521 return NF_ACCEPT;
5522
5523 /* standard practice, label using the parent socket */
5524 sksec = sk->sk_security;
Paul Moore948bf852008-10-10 10:16:32 -04005525 sid = sksec->sid;
5526 } else
5527 sid = SECINITSID_KERNEL;
5528 if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
5529 return NF_DROP;
5530
5531 return NF_ACCEPT;
5532}
5533
Eric W. Biederman06198b32015-09-18 14:33:06 -05005534static unsigned int selinux_ipv4_output(void *priv,
Paul Moore948bf852008-10-10 10:16:32 -04005535 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005536 const struct nf_hook_state *state)
Paul Moore948bf852008-10-10 10:16:32 -04005537{
5538 return selinux_ip_output(skb, PF_INET);
5539}
5540
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005541#if IS_ENABLED(CONFIG_IPV6)
Huw Davies2917f572016-06-27 15:06:15 -04005542static unsigned int selinux_ipv6_output(void *priv,
5543 struct sk_buff *skb,
5544 const struct nf_hook_state *state)
5545{
5546 return selinux_ip_output(skb, PF_INET6);
5547}
5548#endif /* IPV6 */
5549
Paul Mooreeffad8d2008-01-29 08:49:27 -05005550static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
5551 int ifindex,
Paul Moored8395c82008-10-10 10:16:30 -04005552 u16 family)
James Morris4e5ab4c2006-06-09 00:33:33 -07005553{
Eric Dumazet54abc682015-11-08 10:54:07 -08005554 struct sock *sk = skb_to_full_sk(skb);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005555 struct sk_security_struct *sksec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005556 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005557 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04005558 char *addrp;
5559 u8 proto;
James Morris4e5ab4c2006-06-09 00:33:33 -07005560
Paul Mooreeffad8d2008-01-29 08:49:27 -05005561 if (sk == NULL)
5562 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005563 sksec = sk->sk_security;
James Morris4e5ab4c2006-06-09 00:33:33 -07005564
Eric Paris50c205f2012-04-04 15:01:43 -04005565 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005566 ad.u.net = &net;
5567 ad.u.net->netif = ifindex;
5568 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04005569 if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
5570 return NF_DROP;
5571
Paul Moore58bfbb52009-03-27 17:10:41 -04005572 if (selinux_secmark_enabled())
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005573 if (avc_has_perm(&selinux_state,
5574 sksec->sid, skb->secmark,
Paul Moored8395c82008-10-10 10:16:30 -04005575 SECCLASS_PACKET, PACKET__SEND, &ad))
Eric Paris2fe66ec2010-11-23 06:28:08 +00005576 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07005577
Steffen Klassertb9679a72011-02-23 12:55:21 +01005578 if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
5579 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07005580
Paul Mooreeffad8d2008-01-29 08:49:27 -05005581 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582}
5583
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005584static unsigned int selinux_ip_postroute(struct sk_buff *skb,
5585 const struct net_device *outdev,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005586 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587{
Paul Mooreeffad8d2008-01-29 08:49:27 -05005588 u32 secmark_perm;
5589 u32 peer_sid;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005590 int ifindex = outdev->ifindex;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005591 struct sock *sk;
Thomas Liu2bf49692009-07-14 12:14:09 -04005592 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005593 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05005594 char *addrp;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005595 u8 secmark_active;
5596 u8 peerlbl_active;
5597
Paul Mooreeffad8d2008-01-29 08:49:27 -05005598 /* If any sort of compatibility mode is enabled then handoff processing
5599 * to the selinux_ip_postroute_compat() function to deal with the
5600 * special handling. We do this in an attempt to keep this function
5601 * as fast and as clean as possible. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005602 if (!selinux_policycap_netpeer())
Paul Moored8395c82008-10-10 10:16:30 -04005603 return selinux_ip_postroute_compat(skb, ifindex, family);
Paul Moorec0828e52013-12-10 14:58:01 -05005604
Paul Mooreeffad8d2008-01-29 08:49:27 -05005605 secmark_active = selinux_secmark_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04005606 peerlbl_active = selinux_peerlbl_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05005607 if (!secmark_active && !peerlbl_active)
5608 return NF_ACCEPT;
5609
Eric Dumazet54abc682015-11-08 10:54:07 -08005610 sk = skb_to_full_sk(skb);
Paul Moorec0828e52013-12-10 14:58:01 -05005611
Paul Mooreeffad8d2008-01-29 08:49:27 -05005612#ifdef CONFIG_XFRM
5613 /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
5614 * packet transformation so allow the packet to pass without any checks
5615 * since we'll have another chance to perform access control checks
5616 * when the packet is on it's final way out.
5617 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
Paul Moorec0828e52013-12-10 14:58:01 -05005618 * is NULL, in this case go ahead and apply access control.
5619 * NOTE: if this is a local socket (skb->sk != NULL) that is in the
5620 * TCP listening state we cannot wait until the XFRM processing
5621 * is done as we will miss out on the SA label if we do;
5622 * unfortunately, this means more work, but it is only once per
5623 * connection. */
5624 if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL &&
Eric Dumazete446f9d2015-10-08 05:01:55 -07005625 !(sk && sk_listener(sk)))
Paul Mooreeffad8d2008-01-29 08:49:27 -05005626 return NF_ACCEPT;
5627#endif
Paul Mooreeffad8d2008-01-29 08:49:27 -05005628
Paul Moored8395c82008-10-10 10:16:30 -04005629 if (sk == NULL) {
Paul Moore446b8022013-12-04 16:10:51 -05005630 /* Without an associated socket the packet is either coming
5631 * from the kernel or it is being forwarded; check the packet
5632 * to determine which and if the packet is being forwarded
5633 * query the packet directly to determine the security label. */
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005634 if (skb->skb_iif) {
5635 secmark_perm = PACKET__FORWARD_OUT;
Paul Moored8395c82008-10-10 10:16:30 -04005636 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005637 return NF_DROP;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005638 } else {
5639 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04005640 peer_sid = SECINITSID_KERNEL;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005641 }
Eric Dumazete446f9d2015-10-08 05:01:55 -07005642 } else if (sk_listener(sk)) {
Paul Moore446b8022013-12-04 16:10:51 -05005643 /* Locally generated packet but the associated socket is in the
5644 * listening state which means this is a SYN-ACK packet. In
5645 * this particular case the correct security label is assigned
5646 * to the connection/request_sock but unfortunately we can't
5647 * query the request_sock as it isn't queued on the parent
5648 * socket until after the SYN-ACK packet is sent; the only
5649 * viable choice is to regenerate the label like we do in
5650 * selinux_inet_conn_request(). See also selinux_ip_output()
5651 * for similar problems. */
5652 u32 skb_sid;
Eric Dumazete446f9d2015-10-08 05:01:55 -07005653 struct sk_security_struct *sksec;
5654
Eric Dumazete446f9d2015-10-08 05:01:55 -07005655 sksec = sk->sk_security;
Paul Moore446b8022013-12-04 16:10:51 -05005656 if (selinux_skb_peerlbl_sid(skb, family, &skb_sid))
5657 return NF_DROP;
Paul Moorec0828e52013-12-10 14:58:01 -05005658 /* At this point, if the returned skb peerlbl is SECSID_NULL
5659 * and the packet has been through at least one XFRM
5660 * transformation then we must be dealing with the "final"
5661 * form of labeled IPsec packet; since we've already applied
5662 * all of our access controls on this packet we can safely
5663 * pass the packet. */
5664 if (skb_sid == SECSID_NULL) {
5665 switch (family) {
5666 case PF_INET:
5667 if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
5668 return NF_ACCEPT;
5669 break;
5670 case PF_INET6:
5671 if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED)
5672 return NF_ACCEPT;
Paul Moorea7a91a12014-09-03 10:51:59 -04005673 break;
Paul Moorec0828e52013-12-10 14:58:01 -05005674 default:
5675 return NF_DROP_ERR(-ECONNREFUSED);
5676 }
5677 }
Paul Moore446b8022013-12-04 16:10:51 -05005678 if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid))
5679 return NF_DROP;
5680 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04005681 } else {
Paul Moore446b8022013-12-04 16:10:51 -05005682 /* Locally generated packet, fetch the security label from the
5683 * associated socket. */
Paul Mooreeffad8d2008-01-29 08:49:27 -05005684 struct sk_security_struct *sksec = sk->sk_security;
5685 peer_sid = sksec->sid;
5686 secmark_perm = PACKET__SEND;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005687 }
5688
Eric Paris50c205f2012-04-04 15:01:43 -04005689 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005690 ad.u.net = &net;
5691 ad.u.net->netif = ifindex;
5692 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04005693 if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
Eric Paris04f6d702010-11-23 06:28:02 +00005694 return NF_DROP;
Paul Moored8395c82008-10-10 10:16:30 -04005695
Paul Mooreeffad8d2008-01-29 08:49:27 -05005696 if (secmark_active)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005697 if (avc_has_perm(&selinux_state,
5698 peer_sid, skb->secmark,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005699 SECCLASS_PACKET, secmark_perm, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005700 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005701
5702 if (peerlbl_active) {
5703 u32 if_sid;
5704 u32 node_sid;
5705
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005706 if (sel_netif_sid(dev_net(outdev), ifindex, &if_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005707 return NF_DROP;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005708 if (avc_has_perm(&selinux_state,
5709 peer_sid, if_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005710 SECCLASS_NETIF, NETIF__EGRESS, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005711 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005712
5713 if (sel_netnode_sid(addrp, family, &node_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005714 return NF_DROP;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005715 if (avc_has_perm(&selinux_state,
5716 peer_sid, node_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005717 SECCLASS_NODE, NODE__SENDTO, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005718 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005719 }
5720
5721 return NF_ACCEPT;
5722}
5723
Eric W. Biederman06198b32015-09-18 14:33:06 -05005724static unsigned int selinux_ipv4_postroute(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005725 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005726 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005727{
David S. Miller238e54c2015-04-03 20:32:56 -04005728 return selinux_ip_postroute(skb, state->out, PF_INET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729}
5730
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005731#if IS_ENABLED(CONFIG_IPV6)
Eric W. Biederman06198b32015-09-18 14:33:06 -05005732static unsigned int selinux_ipv6_postroute(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005733 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005734 const struct nf_hook_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735{
David S. Miller238e54c2015-04-03 20:32:56 -04005736 return selinux_ip_postroute(skb, state->out, PF_INET6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738#endif /* IPV6 */
5739
5740#endif /* CONFIG_NETFILTER */
5741
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
5743{
Stephen Smalley941fc5b2009-10-01 14:48:23 -04005744 return selinux_nlmsg_perm(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745}
5746
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005747static int ipc_alloc_security(struct kern_ipc_perm *perm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748 u16 sclass)
5749{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750 struct ipc_security_struct *isec;
5751
James Morris89d155e2005-10-30 14:59:21 -08005752 isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753 if (!isec)
5754 return -ENOMEM;
5755
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756 isec->sclass = sclass;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005757 isec->sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758 perm->security = isec;
5759
5760 return 0;
5761}
5762
5763static void ipc_free_security(struct kern_ipc_perm *perm)
5764{
5765 struct ipc_security_struct *isec = perm->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005766 perm->security = NULL;
5767 kfree(isec);
5768}
5769
5770static int msg_msg_alloc_security(struct msg_msg *msg)
5771{
5772 struct msg_security_struct *msec;
5773
James Morris89d155e2005-10-30 14:59:21 -08005774 msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775 if (!msec)
5776 return -ENOMEM;
5777
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778 msec->sid = SECINITSID_UNLABELED;
5779 msg->security = msec;
5780
5781 return 0;
5782}
5783
5784static void msg_msg_free_security(struct msg_msg *msg)
5785{
5786 struct msg_security_struct *msec = msg->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787
5788 msg->security = NULL;
5789 kfree(msec);
5790}
5791
5792static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
Stephen Smalley6af963f2005-05-01 08:58:39 -07005793 u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005794{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005795 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005796 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005797 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799 isec = ipc_perms->security;
5800
Eric Paris50c205f2012-04-04 15:01:43 -04005801 ad.type = LSM_AUDIT_DATA_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802 ad.u.ipc_id = ipc_perms->key;
5803
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005804 return avc_has_perm(&selinux_state,
5805 sid, isec->sid, isec->sclass, perms, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806}
5807
5808static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
5809{
5810 return msg_msg_alloc_security(msg);
5811}
5812
5813static void selinux_msg_msg_free_security(struct msg_msg *msg)
5814{
5815 msg_msg_free_security(msg);
5816}
5817
5818/* message queue security operations */
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005819static int selinux_msg_queue_alloc_security(struct kern_ipc_perm *msq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005822 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005823 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005824 int rc;
5825
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005826 rc = ipc_alloc_security(msq, SECCLASS_MSGQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827 if (rc)
5828 return rc;
5829
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005830 isec = msq->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831
Eric Paris50c205f2012-04-04 15:01:43 -04005832 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005833 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005834
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005835 rc = avc_has_perm(&selinux_state,
5836 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837 MSGQ__CREATE, &ad);
5838 if (rc) {
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005839 ipc_free_security(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840 return rc;
5841 }
5842 return 0;
5843}
5844
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005845static void selinux_msg_queue_free_security(struct kern_ipc_perm *msq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846{
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005847 ipc_free_security(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848}
5849
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005850static int selinux_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005852 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005853 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005854 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005856 isec = msq->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857
Eric Paris50c205f2012-04-04 15:01:43 -04005858 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005859 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005861 return avc_has_perm(&selinux_state,
5862 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863 MSGQ__ASSOCIATE, &ad);
5864}
5865
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005866static int selinux_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867{
5868 int err;
5869 int perms;
5870
Eric Paris828dfe12008-04-17 13:17:49 -04005871 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005872 case IPC_INFO:
5873 case MSG_INFO:
5874 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005875 return avc_has_perm(&selinux_state,
5876 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005877 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878 case IPC_STAT:
5879 case MSG_STAT:
Davidlohr Bueso23c8cec2018-04-10 16:35:30 -07005880 case MSG_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005881 perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
5882 break;
5883 case IPC_SET:
5884 perms = MSGQ__SETATTR;
5885 break;
5886 case IPC_RMID:
5887 perms = MSGQ__DESTROY;
5888 break;
5889 default:
5890 return 0;
5891 }
5892
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005893 err = ipc_has_perm(msq, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894 return err;
5895}
5896
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005897static int selinux_msg_queue_msgsnd(struct kern_ipc_perm *msq, struct msg_msg *msg, int msqflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899 struct ipc_security_struct *isec;
5900 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005901 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005902 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903 int rc;
5904
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005905 isec = msq->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005906 msec = msg->security;
5907
5908 /*
5909 * First time through, need to assign label to the message
5910 */
5911 if (msec->sid == SECINITSID_UNLABELED) {
5912 /*
5913 * Compute new sid based on current process and
5914 * message queue this message will be stored in
5915 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005916 rc = security_transition_sid(&selinux_state, sid, isec->sid,
5917 SECCLASS_MSG, NULL, &msec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918 if (rc)
5919 return rc;
5920 }
5921
Eric Paris50c205f2012-04-04 15:01:43 -04005922 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005923 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924
5925 /* Can this process write to the queue? */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005926 rc = avc_has_perm(&selinux_state,
5927 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928 MSGQ__WRITE, &ad);
5929 if (!rc)
5930 /* Can this process send the message */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005931 rc = avc_has_perm(&selinux_state,
5932 sid, msec->sid, SECCLASS_MSG,
David Howells275bb412008-11-14 10:39:19 +11005933 MSG__SEND, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934 if (!rc)
5935 /* Can the message be put in the queue? */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005936 rc = avc_has_perm(&selinux_state,
5937 msec->sid, isec->sid, SECCLASS_MSGQ,
David Howells275bb412008-11-14 10:39:19 +11005938 MSGQ__ENQUEUE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939
5940 return rc;
5941}
5942
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005943static int selinux_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944 struct task_struct *target,
5945 long type, int mode)
5946{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005947 struct ipc_security_struct *isec;
5948 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005949 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005950 u32 sid = task_sid(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951 int rc;
5952
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005953 isec = msq->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954 msec = msg->security;
5955
Eric Paris50c205f2012-04-04 15:01:43 -04005956 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005957 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005958
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005959 rc = avc_has_perm(&selinux_state,
5960 sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005961 SECCLASS_MSGQ, MSGQ__READ, &ad);
5962 if (!rc)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005963 rc = avc_has_perm(&selinux_state,
5964 sid, msec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005965 SECCLASS_MSG, MSG__RECEIVE, &ad);
5966 return rc;
5967}
5968
5969/* Shared Memory security operations */
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005970static int selinux_shm_alloc_security(struct kern_ipc_perm *shp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005971{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005972 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005973 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005974 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005975 int rc;
5976
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005977 rc = ipc_alloc_security(shp, SECCLASS_SHM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005978 if (rc)
5979 return rc;
5980
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005981 isec = shp->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982
Eric Paris50c205f2012-04-04 15:01:43 -04005983 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005984 ad.u.ipc_id = shp->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005985
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005986 rc = avc_has_perm(&selinux_state,
5987 sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005988 SHM__CREATE, &ad);
5989 if (rc) {
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005990 ipc_free_security(shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991 return rc;
5992 }
5993 return 0;
5994}
5995
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005996static void selinux_shm_free_security(struct kern_ipc_perm *shp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997{
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005998 ipc_free_security(shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999}
6000
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006001static int selinux_shm_associate(struct kern_ipc_perm *shp, int shmflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006003 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006004 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006005 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006006
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006007 isec = shp->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006008
Eric Paris50c205f2012-04-04 15:01:43 -04006009 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006010 ad.u.ipc_id = shp->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006012 return avc_has_perm(&selinux_state,
6013 sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014 SHM__ASSOCIATE, &ad);
6015}
6016
6017/* Note, at this point, shp is locked down */
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006018static int selinux_shm_shmctl(struct kern_ipc_perm *shp, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019{
6020 int perms;
6021 int err;
6022
Eric Paris828dfe12008-04-17 13:17:49 -04006023 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006024 case IPC_INFO:
6025 case SHM_INFO:
6026 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006027 return avc_has_perm(&selinux_state,
6028 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006029 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030 case IPC_STAT:
6031 case SHM_STAT:
Davidlohr Buesoc21a6972018-04-10 16:35:23 -07006032 case SHM_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033 perms = SHM__GETATTR | SHM__ASSOCIATE;
6034 break;
6035 case IPC_SET:
6036 perms = SHM__SETATTR;
6037 break;
6038 case SHM_LOCK:
6039 case SHM_UNLOCK:
6040 perms = SHM__LOCK;
6041 break;
6042 case IPC_RMID:
6043 perms = SHM__DESTROY;
6044 break;
6045 default:
6046 return 0;
6047 }
6048
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006049 err = ipc_has_perm(shp, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050 return err;
6051}
6052
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006053static int selinux_shm_shmat(struct kern_ipc_perm *shp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006054 char __user *shmaddr, int shmflg)
6055{
6056 u32 perms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006057
6058 if (shmflg & SHM_RDONLY)
6059 perms = SHM__READ;
6060 else
6061 perms = SHM__READ | SHM__WRITE;
6062
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006063 return ipc_has_perm(shp, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006064}
6065
6066/* Semaphore security operations */
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006067static int selinux_sem_alloc_security(struct kern_ipc_perm *sma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006070 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006071 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072 int rc;
6073
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006074 rc = ipc_alloc_security(sma, SECCLASS_SEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075 if (rc)
6076 return rc;
6077
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006078 isec = sma->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079
Eric Paris50c205f2012-04-04 15:01:43 -04006080 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006081 ad.u.ipc_id = sma->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006083 rc = avc_has_perm(&selinux_state,
6084 sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085 SEM__CREATE, &ad);
6086 if (rc) {
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006087 ipc_free_security(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088 return rc;
6089 }
6090 return 0;
6091}
6092
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006093static void selinux_sem_free_security(struct kern_ipc_perm *sma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006094{
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006095 ipc_free_security(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006096}
6097
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006098static int selinux_sem_associate(struct kern_ipc_perm *sma, int semflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006100 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006101 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006102 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006103
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006104 isec = sma->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006105
Eric Paris50c205f2012-04-04 15:01:43 -04006106 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006107 ad.u.ipc_id = sma->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006108
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006109 return avc_has_perm(&selinux_state,
6110 sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006111 SEM__ASSOCIATE, &ad);
6112}
6113
6114/* Note, at this point, sma is locked down */
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006115static int selinux_sem_semctl(struct kern_ipc_perm *sma, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006116{
6117 int err;
6118 u32 perms;
6119
Eric Paris828dfe12008-04-17 13:17:49 -04006120 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006121 case IPC_INFO:
6122 case SEM_INFO:
6123 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006124 return avc_has_perm(&selinux_state,
6125 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006126 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127 case GETPID:
6128 case GETNCNT:
6129 case GETZCNT:
6130 perms = SEM__GETATTR;
6131 break;
6132 case GETVAL:
6133 case GETALL:
6134 perms = SEM__READ;
6135 break;
6136 case SETVAL:
6137 case SETALL:
6138 perms = SEM__WRITE;
6139 break;
6140 case IPC_RMID:
6141 perms = SEM__DESTROY;
6142 break;
6143 case IPC_SET:
6144 perms = SEM__SETATTR;
6145 break;
6146 case IPC_STAT:
6147 case SEM_STAT:
Davidlohr Buesoa280d6d2018-04-10 16:35:26 -07006148 case SEM_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006149 perms = SEM__GETATTR | SEM__ASSOCIATE;
6150 break;
6151 default:
6152 return 0;
6153 }
6154
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006155 err = ipc_has_perm(sma, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006156 return err;
6157}
6158
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006159static int selinux_sem_semop(struct kern_ipc_perm *sma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006160 struct sembuf *sops, unsigned nsops, int alter)
6161{
6162 u32 perms;
6163
6164 if (alter)
6165 perms = SEM__READ | SEM__WRITE;
6166 else
6167 perms = SEM__READ;
6168
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006169 return ipc_has_perm(sma, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006170}
6171
6172static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
6173{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006174 u32 av = 0;
6175
Linus Torvalds1da177e2005-04-16 15:20:36 -07006176 av = 0;
6177 if (flag & S_IRUGO)
6178 av |= IPC__UNIX_READ;
6179 if (flag & S_IWUGO)
6180 av |= IPC__UNIX_WRITE;
6181
6182 if (av == 0)
6183 return 0;
6184
Stephen Smalley6af963f2005-05-01 08:58:39 -07006185 return ipc_has_perm(ipcp, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006186}
6187
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02006188static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
6189{
6190 struct ipc_security_struct *isec = ipcp->security;
6191 *secid = isec->sid;
6192}
6193
Eric Paris828dfe12008-04-17 13:17:49 -04006194static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006195{
6196 if (inode)
6197 inode_doinit_with_dentry(inode, dentry);
6198}
6199
6200static int selinux_getprocattr(struct task_struct *p,
Al Viro04ff9702007-03-12 16:17:58 +00006201 char *name, char **value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006202{
David Howells275bb412008-11-14 10:39:19 +11006203 const struct task_security_struct *__tsec;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00006204 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205 int error;
Al Viro04ff9702007-03-12 16:17:58 +00006206 unsigned len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006207
David Howells275bb412008-11-14 10:39:19 +11006208 rcu_read_lock();
6209 __tsec = __task_cred(p)->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006211 if (current != p) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006212 error = avc_has_perm(&selinux_state,
6213 current_sid(), __tsec->sid,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006214 SECCLASS_PROCESS, PROCESS__GETATTR, NULL);
6215 if (error)
6216 goto bad;
6217 }
6218
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219 if (!strcmp(name, "current"))
David Howells275bb412008-11-14 10:39:19 +11006220 sid = __tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006221 else if (!strcmp(name, "prev"))
David Howells275bb412008-11-14 10:39:19 +11006222 sid = __tsec->osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006223 else if (!strcmp(name, "exec"))
David Howells275bb412008-11-14 10:39:19 +11006224 sid = __tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006225 else if (!strcmp(name, "fscreate"))
David Howells275bb412008-11-14 10:39:19 +11006226 sid = __tsec->create_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006227 else if (!strcmp(name, "keycreate"))
David Howells275bb412008-11-14 10:39:19 +11006228 sid = __tsec->keycreate_sid;
Eric Paris42c3e032006-06-26 00:26:03 -07006229 else if (!strcmp(name, "sockcreate"))
David Howells275bb412008-11-14 10:39:19 +11006230 sid = __tsec->sockcreate_sid;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006231 else {
6232 error = -EINVAL;
6233 goto bad;
6234 }
David Howells275bb412008-11-14 10:39:19 +11006235 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006236
6237 if (!sid)
6238 return 0;
6239
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006240 error = security_sid_to_context(&selinux_state, sid, value, &len);
Al Viro04ff9702007-03-12 16:17:58 +00006241 if (error)
6242 return error;
6243 return len;
David Howells275bb412008-11-14 10:39:19 +11006244
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006245bad:
David Howells275bb412008-11-14 10:39:19 +11006246 rcu_read_unlock();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006247 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006248}
6249
Stephen Smalleyb21507e2017-01-09 10:07:31 -05006250static int selinux_setprocattr(const char *name, void *value, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006251{
6252 struct task_security_struct *tsec;
David Howellsd84f4f92008-11-14 10:39:23 +11006253 struct cred *new;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006254 u32 mysid = current_sid(), sid = 0, ptsid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006255 int error;
6256 char *str = value;
6257
Linus Torvalds1da177e2005-04-16 15:20:36 -07006258 /*
6259 * Basic control over ability to set these attributes at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006260 */
6261 if (!strcmp(name, "exec"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006262 error = avc_has_perm(&selinux_state,
6263 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006264 PROCESS__SETEXEC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006265 else if (!strcmp(name, "fscreate"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006266 error = avc_has_perm(&selinux_state,
6267 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006268 PROCESS__SETFSCREATE, NULL);
Michael LeMay4eb582c2006-06-26 00:24:57 -07006269 else if (!strcmp(name, "keycreate"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006270 error = avc_has_perm(&selinux_state,
6271 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006272 PROCESS__SETKEYCREATE, NULL);
Eric Paris42c3e032006-06-26 00:26:03 -07006273 else if (!strcmp(name, "sockcreate"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006274 error = avc_has_perm(&selinux_state,
6275 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006276 PROCESS__SETSOCKCREATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006277 else if (!strcmp(name, "current"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006278 error = avc_has_perm(&selinux_state,
6279 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006280 PROCESS__SETCURRENT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006281 else
6282 error = -EINVAL;
6283 if (error)
6284 return error;
6285
6286 /* Obtain a SID for the context, if one was specified. */
Stephen Smalleya050a572017-01-31 11:54:04 -05006287 if (size && str[0] && str[0] != '\n') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006288 if (str[size-1] == '\n') {
6289 str[size-1] = 0;
6290 size--;
6291 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006292 error = security_context_to_sid(&selinux_state, value, size,
6293 &sid, GFP_KERNEL);
Stephen Smalley12b29f32008-05-07 13:03:20 -04006294 if (error == -EINVAL && !strcmp(name, "fscreate")) {
Stephen Smalleydb590002017-04-20 11:31:30 -04006295 if (!has_cap_mac_admin(true)) {
Eric Parisd6ea83e2012-04-04 13:45:49 -04006296 struct audit_buffer *ab;
6297 size_t audit_size;
6298
6299 /* We strip a nul only if it is at the end, otherwise the
6300 * context contains a nul and we should audit that */
6301 if (str[size - 1] == '\0')
6302 audit_size = size - 1;
6303 else
6304 audit_size = size;
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04006305 ab = audit_log_start(audit_context(),
6306 GFP_ATOMIC,
6307 AUDIT_SELINUX_ERR);
Eric Parisd6ea83e2012-04-04 13:45:49 -04006308 audit_log_format(ab, "op=fscreate invalid_context=");
6309 audit_log_n_untrustedstring(ab, value, audit_size);
6310 audit_log_end(ab);
6311
Stephen Smalley12b29f32008-05-07 13:03:20 -04006312 return error;
Eric Parisd6ea83e2012-04-04 13:45:49 -04006313 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006314 error = security_context_to_sid_force(
6315 &selinux_state,
6316 value, size, &sid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04006317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006318 if (error)
6319 return error;
6320 }
6321
David Howellsd84f4f92008-11-14 10:39:23 +11006322 new = prepare_creds();
6323 if (!new)
6324 return -ENOMEM;
6325
Linus Torvalds1da177e2005-04-16 15:20:36 -07006326 /* Permission checking based on the specified context is
6327 performed during the actual operation (execve,
6328 open/mkdir/...), when we know the full context of the
David Howellsd84f4f92008-11-14 10:39:23 +11006329 operation. See selinux_bprm_set_creds for the execve
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330 checks and may_create for the file creation checks. The
6331 operation will then fail if the context is not permitted. */
David Howellsd84f4f92008-11-14 10:39:23 +11006332 tsec = new->security;
6333 if (!strcmp(name, "exec")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006334 tsec->exec_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006335 } else if (!strcmp(name, "fscreate")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006336 tsec->create_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006337 } else if (!strcmp(name, "keycreate")) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006338 error = avc_has_perm(&selinux_state,
6339 mysid, sid, SECCLASS_KEY, KEY__CREATE,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006340 NULL);
Michael LeMay4eb582c2006-06-26 00:24:57 -07006341 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11006342 goto abort_change;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006343 tsec->keycreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006344 } else if (!strcmp(name, "sockcreate")) {
Eric Paris42c3e032006-06-26 00:26:03 -07006345 tsec->sockcreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006346 } else if (!strcmp(name, "current")) {
6347 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006348 if (sid == 0)
David Howellsd84f4f92008-11-14 10:39:23 +11006349 goto abort_change;
KaiGai Koheid9250de2008-08-28 16:35:57 +09006350
David Howellsd84f4f92008-11-14 10:39:23 +11006351 /* Only allow single threaded processes to change context */
6352 error = -EPERM;
Oleg Nesterov5bb459b2009-07-10 03:48:23 +02006353 if (!current_is_single_threaded()) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006354 error = security_bounded_transition(&selinux_state,
6355 tsec->sid, sid);
David Howellsd84f4f92008-11-14 10:39:23 +11006356 if (error)
6357 goto abort_change;
Eric Paris828dfe12008-04-17 13:17:49 -04006358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006359
6360 /* Check permissions for the transition. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006361 error = avc_has_perm(&selinux_state,
6362 tsec->sid, sid, SECCLASS_PROCESS,
Eric Paris828dfe12008-04-17 13:17:49 -04006363 PROCESS__DYNTRANSITION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006364 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11006365 goto abort_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006366
6367 /* Check for ptracing, and update the task SID if ok.
6368 Otherwise, leave SID unchanged and fail. */
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006369 ptsid = ptrace_parent_sid();
Paul Moore0c6181c2016-03-30 21:41:21 -04006370 if (ptsid != 0) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006371 error = avc_has_perm(&selinux_state,
6372 ptsid, sid, SECCLASS_PROCESS,
David Howellsd84f4f92008-11-14 10:39:23 +11006373 PROCESS__PTRACE, NULL);
6374 if (error)
6375 goto abort_change;
6376 }
6377
6378 tsec->sid = sid;
6379 } else {
6380 error = -EINVAL;
6381 goto abort_change;
6382 }
6383
6384 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006385 return size;
David Howellsd84f4f92008-11-14 10:39:23 +11006386
6387abort_change:
6388 abort_creds(new);
6389 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006390}
6391
David Quigley746df9b2013-05-22 12:50:35 -04006392static int selinux_ismaclabel(const char *name)
6393{
6394 return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0);
6395}
6396
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006397static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
6398{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006399 return security_sid_to_context(&selinux_state, secid,
6400 secdata, seclen);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006401}
6402
David Howells7bf570d2008-04-29 20:52:51 +01006403static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
David Howells63cb3442008-01-15 23:47:35 +00006404{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006405 return security_context_to_sid(&selinux_state, secdata, seclen,
6406 secid, GFP_KERNEL);
David Howells63cb3442008-01-15 23:47:35 +00006407}
6408
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006409static void selinux_release_secctx(char *secdata, u32 seclen)
6410{
Paul Moore088999e2007-08-01 11:12:58 -04006411 kfree(secdata);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006412}
6413
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006414static void selinux_inode_invalidate_secctx(struct inode *inode)
6415{
6416 struct inode_security_struct *isec = inode->i_security;
6417
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01006418 spin_lock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006419 isec->initialized = LABEL_INVALID;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01006420 spin_unlock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006421}
6422
David P. Quigley1ee65e32009-09-03 14:25:57 -04006423/*
6424 * called with inode->i_mutex locked
6425 */
6426static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
6427{
Ondrej Mosnacek53e0c2a2018-12-21 21:18:53 +01006428 int rc = selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX,
6429 ctx, ctxlen, 0);
6430 /* Do not return error when suppressing label (SBLABEL_MNT not set). */
6431 return rc == -EOPNOTSUPP ? 0 : rc;
David P. Quigley1ee65e32009-09-03 14:25:57 -04006432}
6433
6434/*
6435 * called with inode->i_mutex locked
6436 */
6437static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
6438{
6439 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0);
6440}
6441
6442static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
6443{
6444 int len = 0;
6445 len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX,
6446 ctx, true);
6447 if (len < 0)
6448 return len;
6449 *ctxlen = len;
6450 return 0;
6451}
Michael LeMayd7200242006-06-22 14:47:17 -07006452#ifdef CONFIG_KEYS
6453
David Howellsd84f4f92008-11-14 10:39:23 +11006454static int selinux_key_alloc(struct key *k, const struct cred *cred,
David Howells7e047ef2006-06-26 00:24:50 -07006455 unsigned long flags)
Michael LeMayd7200242006-06-22 14:47:17 -07006456{
David Howellsd84f4f92008-11-14 10:39:23 +11006457 const struct task_security_struct *tsec;
Michael LeMayd7200242006-06-22 14:47:17 -07006458 struct key_security_struct *ksec;
6459
6460 ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
6461 if (!ksec)
6462 return -ENOMEM;
6463
David Howellsd84f4f92008-11-14 10:39:23 +11006464 tsec = cred->security;
6465 if (tsec->keycreate_sid)
6466 ksec->sid = tsec->keycreate_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006467 else
David Howellsd84f4f92008-11-14 10:39:23 +11006468 ksec->sid = tsec->sid;
Michael LeMayd7200242006-06-22 14:47:17 -07006469
David Howells275bb412008-11-14 10:39:19 +11006470 k->security = ksec;
Michael LeMayd7200242006-06-22 14:47:17 -07006471 return 0;
6472}
6473
6474static void selinux_key_free(struct key *k)
6475{
6476 struct key_security_struct *ksec = k->security;
6477
6478 k->security = NULL;
6479 kfree(ksec);
6480}
6481
6482static int selinux_key_permission(key_ref_t key_ref,
David Howellsd84f4f92008-11-14 10:39:23 +11006483 const struct cred *cred,
David Howellsf5895942014-03-14 17:44:49 +00006484 unsigned perm)
Michael LeMayd7200242006-06-22 14:47:17 -07006485{
6486 struct key *key;
Michael LeMayd7200242006-06-22 14:47:17 -07006487 struct key_security_struct *ksec;
David Howells275bb412008-11-14 10:39:19 +11006488 u32 sid;
Michael LeMayd7200242006-06-22 14:47:17 -07006489
6490 /* if no specific permissions are requested, we skip the
6491 permission check. No serious, additional covert channels
6492 appear to be created. */
6493 if (perm == 0)
6494 return 0;
6495
David Howellsd84f4f92008-11-14 10:39:23 +11006496 sid = cred_sid(cred);
David Howells275bb412008-11-14 10:39:19 +11006497
6498 key = key_ref_to_ptr(key_ref);
6499 ksec = key->security;
6500
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006501 return avc_has_perm(&selinux_state,
6502 sid, ksec->sid, SECCLASS_KEY, perm, NULL);
Michael LeMayd7200242006-06-22 14:47:17 -07006503}
6504
David Howells70a5bb72008-04-29 01:01:26 -07006505static int selinux_key_getsecurity(struct key *key, char **_buffer)
6506{
6507 struct key_security_struct *ksec = key->security;
6508 char *context = NULL;
6509 unsigned len;
6510 int rc;
6511
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006512 rc = security_sid_to_context(&selinux_state, ksec->sid,
6513 &context, &len);
David Howells70a5bb72008-04-29 01:01:26 -07006514 if (!rc)
6515 rc = len;
6516 *_buffer = context;
6517 return rc;
6518}
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006519#endif
David Howells70a5bb72008-04-29 01:01:26 -07006520
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006521#ifdef CONFIG_SECURITY_INFINIBAND
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006522static int selinux_ib_pkey_access(void *ib_sec, u64 subnet_prefix, u16 pkey_val)
6523{
6524 struct common_audit_data ad;
6525 int err;
6526 u32 sid = 0;
6527 struct ib_security_struct *sec = ib_sec;
6528 struct lsm_ibpkey_audit ibpkey;
6529
Daniel Jurgens409dcf32017-05-19 15:48:59 +03006530 err = sel_ib_pkey_sid(subnet_prefix, pkey_val, &sid);
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006531 if (err)
6532 return err;
6533
6534 ad.type = LSM_AUDIT_DATA_IBPKEY;
6535 ibpkey.subnet_prefix = subnet_prefix;
6536 ibpkey.pkey = pkey_val;
6537 ad.u.ibpkey = &ibpkey;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006538 return avc_has_perm(&selinux_state,
6539 sec->sid, sid,
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006540 SECCLASS_INFINIBAND_PKEY,
6541 INFINIBAND_PKEY__ACCESS, &ad);
6542}
6543
Daniel Jurgensab861df2017-05-19 15:48:58 +03006544static int selinux_ib_endport_manage_subnet(void *ib_sec, const char *dev_name,
6545 u8 port_num)
6546{
6547 struct common_audit_data ad;
6548 int err;
6549 u32 sid = 0;
6550 struct ib_security_struct *sec = ib_sec;
6551 struct lsm_ibendport_audit ibendport;
6552
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006553 err = security_ib_endport_sid(&selinux_state, dev_name, port_num,
6554 &sid);
Daniel Jurgensab861df2017-05-19 15:48:58 +03006555
6556 if (err)
6557 return err;
6558
6559 ad.type = LSM_AUDIT_DATA_IBENDPORT;
6560 strncpy(ibendport.dev_name, dev_name, sizeof(ibendport.dev_name));
6561 ibendport.port = port_num;
6562 ad.u.ibendport = &ibendport;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006563 return avc_has_perm(&selinux_state,
6564 sec->sid, sid,
Daniel Jurgensab861df2017-05-19 15:48:58 +03006565 SECCLASS_INFINIBAND_ENDPORT,
6566 INFINIBAND_ENDPORT__MANAGE_SUBNET, &ad);
6567}
6568
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006569static int selinux_ib_alloc_security(void **ib_sec)
6570{
6571 struct ib_security_struct *sec;
6572
6573 sec = kzalloc(sizeof(*sec), GFP_KERNEL);
6574 if (!sec)
6575 return -ENOMEM;
6576 sec->sid = current_sid();
6577
6578 *ib_sec = sec;
6579 return 0;
6580}
6581
6582static void selinux_ib_free_security(void *ib_sec)
6583{
6584 kfree(ib_sec);
6585}
Michael LeMayd7200242006-06-22 14:47:17 -07006586#endif
6587
Chenbo Fengec27c352017-10-18 13:00:25 -07006588#ifdef CONFIG_BPF_SYSCALL
6589static int selinux_bpf(int cmd, union bpf_attr *attr,
6590 unsigned int size)
6591{
6592 u32 sid = current_sid();
6593 int ret;
6594
6595 switch (cmd) {
6596 case BPF_MAP_CREATE:
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006597 ret = avc_has_perm(&selinux_state,
6598 sid, sid, SECCLASS_BPF, BPF__MAP_CREATE,
Chenbo Fengec27c352017-10-18 13:00:25 -07006599 NULL);
6600 break;
6601 case BPF_PROG_LOAD:
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006602 ret = avc_has_perm(&selinux_state,
6603 sid, sid, SECCLASS_BPF, BPF__PROG_LOAD,
Chenbo Fengec27c352017-10-18 13:00:25 -07006604 NULL);
6605 break;
6606 default:
6607 ret = 0;
6608 break;
6609 }
6610
6611 return ret;
6612}
6613
6614static u32 bpf_map_fmode_to_av(fmode_t fmode)
6615{
6616 u32 av = 0;
6617
6618 if (fmode & FMODE_READ)
6619 av |= BPF__MAP_READ;
6620 if (fmode & FMODE_WRITE)
6621 av |= BPF__MAP_WRITE;
6622 return av;
6623}
6624
Chenbo Fengf66e4482017-10-18 13:00:26 -07006625/* This function will check the file pass through unix socket or binder to see
6626 * if it is a bpf related object. And apply correspinding checks on the bpf
6627 * object based on the type. The bpf maps and programs, not like other files and
6628 * socket, are using a shared anonymous inode inside the kernel as their inode.
6629 * So checking that inode cannot identify if the process have privilege to
6630 * access the bpf object and that's why we have to add this additional check in
6631 * selinux_file_receive and selinux_binder_transfer_files.
6632 */
6633static int bpf_fd_pass(struct file *file, u32 sid)
6634{
6635 struct bpf_security_struct *bpfsec;
6636 struct bpf_prog *prog;
6637 struct bpf_map *map;
6638 int ret;
6639
6640 if (file->f_op == &bpf_map_fops) {
6641 map = file->private_data;
6642 bpfsec = map->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006643 ret = avc_has_perm(&selinux_state,
6644 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengf66e4482017-10-18 13:00:26 -07006645 bpf_map_fmode_to_av(file->f_mode), NULL);
6646 if (ret)
6647 return ret;
6648 } else if (file->f_op == &bpf_prog_fops) {
6649 prog = file->private_data;
6650 bpfsec = prog->aux->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006651 ret = avc_has_perm(&selinux_state,
6652 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengf66e4482017-10-18 13:00:26 -07006653 BPF__PROG_RUN, NULL);
6654 if (ret)
6655 return ret;
6656 }
6657 return 0;
6658}
6659
Chenbo Fengec27c352017-10-18 13:00:25 -07006660static int selinux_bpf_map(struct bpf_map *map, fmode_t fmode)
6661{
6662 u32 sid = current_sid();
6663 struct bpf_security_struct *bpfsec;
6664
6665 bpfsec = map->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006666 return avc_has_perm(&selinux_state,
6667 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengec27c352017-10-18 13:00:25 -07006668 bpf_map_fmode_to_av(fmode), NULL);
6669}
6670
6671static int selinux_bpf_prog(struct bpf_prog *prog)
6672{
6673 u32 sid = current_sid();
6674 struct bpf_security_struct *bpfsec;
6675
6676 bpfsec = prog->aux->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006677 return avc_has_perm(&selinux_state,
6678 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengec27c352017-10-18 13:00:25 -07006679 BPF__PROG_RUN, NULL);
6680}
6681
6682static int selinux_bpf_map_alloc(struct bpf_map *map)
6683{
6684 struct bpf_security_struct *bpfsec;
6685
6686 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6687 if (!bpfsec)
6688 return -ENOMEM;
6689
6690 bpfsec->sid = current_sid();
6691 map->security = bpfsec;
6692
6693 return 0;
6694}
6695
6696static void selinux_bpf_map_free(struct bpf_map *map)
6697{
6698 struct bpf_security_struct *bpfsec = map->security;
6699
6700 map->security = NULL;
6701 kfree(bpfsec);
6702}
6703
6704static int selinux_bpf_prog_alloc(struct bpf_prog_aux *aux)
6705{
6706 struct bpf_security_struct *bpfsec;
6707
6708 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6709 if (!bpfsec)
6710 return -ENOMEM;
6711
6712 bpfsec->sid = current_sid();
6713 aux->security = bpfsec;
6714
6715 return 0;
6716}
6717
6718static void selinux_bpf_prog_free(struct bpf_prog_aux *aux)
6719{
6720 struct bpf_security_struct *bpfsec = aux->security;
6721
6722 aux->security = NULL;
6723 kfree(bpfsec);
6724}
6725#endif
6726
James Morrisca97d932017-02-15 00:18:51 +11006727static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {
Casey Schauflere20b0432015-05-02 15:11:36 -07006728 LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr),
6729 LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction),
6730 LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder),
6731 LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file),
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02006732
Casey Schauflere20b0432015-05-02 15:11:36 -07006733 LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check),
6734 LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme),
6735 LSM_HOOK_INIT(capget, selinux_capget),
6736 LSM_HOOK_INIT(capset, selinux_capset),
6737 LSM_HOOK_INIT(capable, selinux_capable),
6738 LSM_HOOK_INIT(quotactl, selinux_quotactl),
6739 LSM_HOOK_INIT(quota_on, selinux_quota_on),
6740 LSM_HOOK_INIT(syslog, selinux_syslog),
6741 LSM_HOOK_INIT(vm_enough_memory, selinux_vm_enough_memory),
Stephen Smalley79af7302015-01-21 10:54:10 -05006742
Casey Schauflere20b0432015-05-02 15:11:36 -07006743 LSM_HOOK_INIT(netlink_send, selinux_netlink_send),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006744
Casey Schauflere20b0432015-05-02 15:11:36 -07006745 LSM_HOOK_INIT(bprm_set_creds, selinux_bprm_set_creds),
6746 LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds),
6747 LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006748
Casey Schauflere20b0432015-05-02 15:11:36 -07006749 LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security),
6750 LSM_HOOK_INIT(sb_free_security, selinux_sb_free_security),
Al Viro5b400232018-12-12 20:13:29 -05006751 LSM_HOOK_INIT(sb_eat_lsm_opts, selinux_sb_eat_lsm_opts),
Al Viro204cc0c2018-12-13 13:41:47 -05006752 LSM_HOOK_INIT(sb_free_mnt_opts, selinux_free_mnt_opts),
Casey Schauflere20b0432015-05-02 15:11:36 -07006753 LSM_HOOK_INIT(sb_remount, selinux_sb_remount),
6754 LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount),
6755 LSM_HOOK_INIT(sb_show_options, selinux_sb_show_options),
6756 LSM_HOOK_INIT(sb_statfs, selinux_sb_statfs),
6757 LSM_HOOK_INIT(sb_mount, selinux_mount),
6758 LSM_HOOK_INIT(sb_umount, selinux_umount),
6759 LSM_HOOK_INIT(sb_set_mnt_opts, selinux_set_mnt_opts),
6760 LSM_HOOK_INIT(sb_clone_mnt_opts, selinux_sb_clone_mnt_opts),
Al Viro757cbe52018-12-14 23:42:21 -05006761 LSM_HOOK_INIT(sb_add_mnt_opt, selinux_add_mnt_opt),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006762
Casey Schauflere20b0432015-05-02 15:11:36 -07006763 LSM_HOOK_INIT(dentry_init_security, selinux_dentry_init_security),
Vivek Goyala518b0a2016-07-13 10:44:53 -04006764 LSM_HOOK_INIT(dentry_create_files_as, selinux_dentry_create_files_as),
Eric Parise0007522008-03-05 10:31:54 -05006765
Casey Schauflere20b0432015-05-02 15:11:36 -07006766 LSM_HOOK_INIT(inode_alloc_security, selinux_inode_alloc_security),
6767 LSM_HOOK_INIT(inode_free_security, selinux_inode_free_security),
6768 LSM_HOOK_INIT(inode_init_security, selinux_inode_init_security),
6769 LSM_HOOK_INIT(inode_create, selinux_inode_create),
6770 LSM_HOOK_INIT(inode_link, selinux_inode_link),
6771 LSM_HOOK_INIT(inode_unlink, selinux_inode_unlink),
6772 LSM_HOOK_INIT(inode_symlink, selinux_inode_symlink),
6773 LSM_HOOK_INIT(inode_mkdir, selinux_inode_mkdir),
6774 LSM_HOOK_INIT(inode_rmdir, selinux_inode_rmdir),
6775 LSM_HOOK_INIT(inode_mknod, selinux_inode_mknod),
6776 LSM_HOOK_INIT(inode_rename, selinux_inode_rename),
6777 LSM_HOOK_INIT(inode_readlink, selinux_inode_readlink),
6778 LSM_HOOK_INIT(inode_follow_link, selinux_inode_follow_link),
6779 LSM_HOOK_INIT(inode_permission, selinux_inode_permission),
6780 LSM_HOOK_INIT(inode_setattr, selinux_inode_setattr),
6781 LSM_HOOK_INIT(inode_getattr, selinux_inode_getattr),
6782 LSM_HOOK_INIT(inode_setxattr, selinux_inode_setxattr),
6783 LSM_HOOK_INIT(inode_post_setxattr, selinux_inode_post_setxattr),
6784 LSM_HOOK_INIT(inode_getxattr, selinux_inode_getxattr),
6785 LSM_HOOK_INIT(inode_listxattr, selinux_inode_listxattr),
6786 LSM_HOOK_INIT(inode_removexattr, selinux_inode_removexattr),
6787 LSM_HOOK_INIT(inode_getsecurity, selinux_inode_getsecurity),
6788 LSM_HOOK_INIT(inode_setsecurity, selinux_inode_setsecurity),
6789 LSM_HOOK_INIT(inode_listsecurity, selinux_inode_listsecurity),
6790 LSM_HOOK_INIT(inode_getsecid, selinux_inode_getsecid),
Vivek Goyal56909eb2016-07-13 10:44:48 -04006791 LSM_HOOK_INIT(inode_copy_up, selinux_inode_copy_up),
Vivek Goyal19472b62016-07-13 10:44:50 -04006792 LSM_HOOK_INIT(inode_copy_up_xattr, selinux_inode_copy_up_xattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006793
Casey Schauflere20b0432015-05-02 15:11:36 -07006794 LSM_HOOK_INIT(file_permission, selinux_file_permission),
6795 LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security),
6796 LSM_HOOK_INIT(file_free_security, selinux_file_free_security),
6797 LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl),
6798 LSM_HOOK_INIT(mmap_file, selinux_mmap_file),
6799 LSM_HOOK_INIT(mmap_addr, selinux_mmap_addr),
6800 LSM_HOOK_INIT(file_mprotect, selinux_file_mprotect),
6801 LSM_HOOK_INIT(file_lock, selinux_file_lock),
6802 LSM_HOOK_INIT(file_fcntl, selinux_file_fcntl),
6803 LSM_HOOK_INIT(file_set_fowner, selinux_file_set_fowner),
6804 LSM_HOOK_INIT(file_send_sigiotask, selinux_file_send_sigiotask),
6805 LSM_HOOK_INIT(file_receive, selinux_file_receive),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006806
Casey Schauflere20b0432015-05-02 15:11:36 -07006807 LSM_HOOK_INIT(file_open, selinux_file_open),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006808
Tetsuo Handaa79be232017-03-28 23:08:45 +09006809 LSM_HOOK_INIT(task_alloc, selinux_task_alloc),
Casey Schauflere20b0432015-05-02 15:11:36 -07006810 LSM_HOOK_INIT(cred_alloc_blank, selinux_cred_alloc_blank),
6811 LSM_HOOK_INIT(cred_free, selinux_cred_free),
6812 LSM_HOOK_INIT(cred_prepare, selinux_cred_prepare),
6813 LSM_HOOK_INIT(cred_transfer, selinux_cred_transfer),
Matthew Garrett3ec30112018-01-08 13:36:19 -08006814 LSM_HOOK_INIT(cred_getsecid, selinux_cred_getsecid),
Casey Schauflere20b0432015-05-02 15:11:36 -07006815 LSM_HOOK_INIT(kernel_act_as, selinux_kernel_act_as),
6816 LSM_HOOK_INIT(kernel_create_files_as, selinux_kernel_create_files_as),
6817 LSM_HOOK_INIT(kernel_module_request, selinux_kernel_module_request),
Mimi Zoharc77b8cd2018-07-13 14:06:02 -04006818 LSM_HOOK_INIT(kernel_load_data, selinux_kernel_load_data),
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07006819 LSM_HOOK_INIT(kernel_read_file, selinux_kernel_read_file),
Casey Schauflere20b0432015-05-02 15:11:36 -07006820 LSM_HOOK_INIT(task_setpgid, selinux_task_setpgid),
6821 LSM_HOOK_INIT(task_getpgid, selinux_task_getpgid),
6822 LSM_HOOK_INIT(task_getsid, selinux_task_getsid),
6823 LSM_HOOK_INIT(task_getsecid, selinux_task_getsecid),
6824 LSM_HOOK_INIT(task_setnice, selinux_task_setnice),
6825 LSM_HOOK_INIT(task_setioprio, selinux_task_setioprio),
6826 LSM_HOOK_INIT(task_getioprio, selinux_task_getioprio),
Stephen Smalley791ec492017-02-17 07:57:00 -05006827 LSM_HOOK_INIT(task_prlimit, selinux_task_prlimit),
Casey Schauflere20b0432015-05-02 15:11:36 -07006828 LSM_HOOK_INIT(task_setrlimit, selinux_task_setrlimit),
6829 LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler),
6830 LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler),
6831 LSM_HOOK_INIT(task_movememory, selinux_task_movememory),
6832 LSM_HOOK_INIT(task_kill, selinux_task_kill),
Casey Schauflere20b0432015-05-02 15:11:36 -07006833 LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode),
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09006834
Casey Schauflere20b0432015-05-02 15:11:36 -07006835 LSM_HOOK_INIT(ipc_permission, selinux_ipc_permission),
6836 LSM_HOOK_INIT(ipc_getsecid, selinux_ipc_getsecid),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006837
Casey Schauflere20b0432015-05-02 15:11:36 -07006838 LSM_HOOK_INIT(msg_msg_alloc_security, selinux_msg_msg_alloc_security),
6839 LSM_HOOK_INIT(msg_msg_free_security, selinux_msg_msg_free_security),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006840
Casey Schauflere20b0432015-05-02 15:11:36 -07006841 LSM_HOOK_INIT(msg_queue_alloc_security,
6842 selinux_msg_queue_alloc_security),
6843 LSM_HOOK_INIT(msg_queue_free_security, selinux_msg_queue_free_security),
6844 LSM_HOOK_INIT(msg_queue_associate, selinux_msg_queue_associate),
6845 LSM_HOOK_INIT(msg_queue_msgctl, selinux_msg_queue_msgctl),
6846 LSM_HOOK_INIT(msg_queue_msgsnd, selinux_msg_queue_msgsnd),
6847 LSM_HOOK_INIT(msg_queue_msgrcv, selinux_msg_queue_msgrcv),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006848
Casey Schauflere20b0432015-05-02 15:11:36 -07006849 LSM_HOOK_INIT(shm_alloc_security, selinux_shm_alloc_security),
6850 LSM_HOOK_INIT(shm_free_security, selinux_shm_free_security),
6851 LSM_HOOK_INIT(shm_associate, selinux_shm_associate),
6852 LSM_HOOK_INIT(shm_shmctl, selinux_shm_shmctl),
6853 LSM_HOOK_INIT(shm_shmat, selinux_shm_shmat),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006854
Casey Schauflere20b0432015-05-02 15:11:36 -07006855 LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security),
6856 LSM_HOOK_INIT(sem_free_security, selinux_sem_free_security),
6857 LSM_HOOK_INIT(sem_associate, selinux_sem_associate),
6858 LSM_HOOK_INIT(sem_semctl, selinux_sem_semctl),
6859 LSM_HOOK_INIT(sem_semop, selinux_sem_semop),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006860
Casey Schauflere20b0432015-05-02 15:11:36 -07006861 LSM_HOOK_INIT(d_instantiate, selinux_d_instantiate),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006862
Casey Schauflere20b0432015-05-02 15:11:36 -07006863 LSM_HOOK_INIT(getprocattr, selinux_getprocattr),
6864 LSM_HOOK_INIT(setprocattr, selinux_setprocattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006865
Casey Schauflere20b0432015-05-02 15:11:36 -07006866 LSM_HOOK_INIT(ismaclabel, selinux_ismaclabel),
6867 LSM_HOOK_INIT(secid_to_secctx, selinux_secid_to_secctx),
6868 LSM_HOOK_INIT(secctx_to_secid, selinux_secctx_to_secid),
6869 LSM_HOOK_INIT(release_secctx, selinux_release_secctx),
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006870 LSM_HOOK_INIT(inode_invalidate_secctx, selinux_inode_invalidate_secctx),
Casey Schauflere20b0432015-05-02 15:11:36 -07006871 LSM_HOOK_INIT(inode_notifysecctx, selinux_inode_notifysecctx),
6872 LSM_HOOK_INIT(inode_setsecctx, selinux_inode_setsecctx),
6873 LSM_HOOK_INIT(inode_getsecctx, selinux_inode_getsecctx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006874
Casey Schauflere20b0432015-05-02 15:11:36 -07006875 LSM_HOOK_INIT(unix_stream_connect, selinux_socket_unix_stream_connect),
6876 LSM_HOOK_INIT(unix_may_send, selinux_socket_unix_may_send),
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006877
Casey Schauflere20b0432015-05-02 15:11:36 -07006878 LSM_HOOK_INIT(socket_create, selinux_socket_create),
6879 LSM_HOOK_INIT(socket_post_create, selinux_socket_post_create),
David Herrmann0b811db2018-05-04 16:28:21 +02006880 LSM_HOOK_INIT(socket_socketpair, selinux_socket_socketpair),
Casey Schauflere20b0432015-05-02 15:11:36 -07006881 LSM_HOOK_INIT(socket_bind, selinux_socket_bind),
6882 LSM_HOOK_INIT(socket_connect, selinux_socket_connect),
6883 LSM_HOOK_INIT(socket_listen, selinux_socket_listen),
6884 LSM_HOOK_INIT(socket_accept, selinux_socket_accept),
6885 LSM_HOOK_INIT(socket_sendmsg, selinux_socket_sendmsg),
6886 LSM_HOOK_INIT(socket_recvmsg, selinux_socket_recvmsg),
6887 LSM_HOOK_INIT(socket_getsockname, selinux_socket_getsockname),
6888 LSM_HOOK_INIT(socket_getpeername, selinux_socket_getpeername),
6889 LSM_HOOK_INIT(socket_getsockopt, selinux_socket_getsockopt),
6890 LSM_HOOK_INIT(socket_setsockopt, selinux_socket_setsockopt),
6891 LSM_HOOK_INIT(socket_shutdown, selinux_socket_shutdown),
6892 LSM_HOOK_INIT(socket_sock_rcv_skb, selinux_socket_sock_rcv_skb),
6893 LSM_HOOK_INIT(socket_getpeersec_stream,
6894 selinux_socket_getpeersec_stream),
6895 LSM_HOOK_INIT(socket_getpeersec_dgram, selinux_socket_getpeersec_dgram),
6896 LSM_HOOK_INIT(sk_alloc_security, selinux_sk_alloc_security),
6897 LSM_HOOK_INIT(sk_free_security, selinux_sk_free_security),
6898 LSM_HOOK_INIT(sk_clone_security, selinux_sk_clone_security),
6899 LSM_HOOK_INIT(sk_getsecid, selinux_sk_getsecid),
6900 LSM_HOOK_INIT(sock_graft, selinux_sock_graft),
Richard Hainesd4529302018-02-13 20:57:18 +00006901 LSM_HOOK_INIT(sctp_assoc_request, selinux_sctp_assoc_request),
6902 LSM_HOOK_INIT(sctp_sk_clone, selinux_sctp_sk_clone),
6903 LSM_HOOK_INIT(sctp_bind_connect, selinux_sctp_bind_connect),
Casey Schauflere20b0432015-05-02 15:11:36 -07006904 LSM_HOOK_INIT(inet_conn_request, selinux_inet_conn_request),
6905 LSM_HOOK_INIT(inet_csk_clone, selinux_inet_csk_clone),
6906 LSM_HOOK_INIT(inet_conn_established, selinux_inet_conn_established),
6907 LSM_HOOK_INIT(secmark_relabel_packet, selinux_secmark_relabel_packet),
6908 LSM_HOOK_INIT(secmark_refcount_inc, selinux_secmark_refcount_inc),
6909 LSM_HOOK_INIT(secmark_refcount_dec, selinux_secmark_refcount_dec),
6910 LSM_HOOK_INIT(req_classify_flow, selinux_req_classify_flow),
6911 LSM_HOOK_INIT(tun_dev_alloc_security, selinux_tun_dev_alloc_security),
6912 LSM_HOOK_INIT(tun_dev_free_security, selinux_tun_dev_free_security),
6913 LSM_HOOK_INIT(tun_dev_create, selinux_tun_dev_create),
6914 LSM_HOOK_INIT(tun_dev_attach_queue, selinux_tun_dev_attach_queue),
6915 LSM_HOOK_INIT(tun_dev_attach, selinux_tun_dev_attach),
6916 LSM_HOOK_INIT(tun_dev_open, selinux_tun_dev_open),
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006917#ifdef CONFIG_SECURITY_INFINIBAND
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006918 LSM_HOOK_INIT(ib_pkey_access, selinux_ib_pkey_access),
Daniel Jurgensab861df2017-05-19 15:48:58 +03006919 LSM_HOOK_INIT(ib_endport_manage_subnet,
6920 selinux_ib_endport_manage_subnet),
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006921 LSM_HOOK_INIT(ib_alloc_security, selinux_ib_alloc_security),
6922 LSM_HOOK_INIT(ib_free_security, selinux_ib_free_security),
6923#endif
Trent Jaegerd28d1e02005-12-13 23:12:40 -08006924#ifdef CONFIG_SECURITY_NETWORK_XFRM
Casey Schauflere20b0432015-05-02 15:11:36 -07006925 LSM_HOOK_INIT(xfrm_policy_alloc_security, selinux_xfrm_policy_alloc),
6926 LSM_HOOK_INIT(xfrm_policy_clone_security, selinux_xfrm_policy_clone),
6927 LSM_HOOK_INIT(xfrm_policy_free_security, selinux_xfrm_policy_free),
6928 LSM_HOOK_INIT(xfrm_policy_delete_security, selinux_xfrm_policy_delete),
6929 LSM_HOOK_INIT(xfrm_state_alloc, selinux_xfrm_state_alloc),
6930 LSM_HOOK_INIT(xfrm_state_alloc_acquire,
6931 selinux_xfrm_state_alloc_acquire),
6932 LSM_HOOK_INIT(xfrm_state_free_security, selinux_xfrm_state_free),
6933 LSM_HOOK_INIT(xfrm_state_delete_security, selinux_xfrm_state_delete),
6934 LSM_HOOK_INIT(xfrm_policy_lookup, selinux_xfrm_policy_lookup),
6935 LSM_HOOK_INIT(xfrm_state_pol_flow_match,
6936 selinux_xfrm_state_pol_flow_match),
6937 LSM_HOOK_INIT(xfrm_decode_session, selinux_xfrm_decode_session),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006938#endif
Michael LeMayd7200242006-06-22 14:47:17 -07006939
6940#ifdef CONFIG_KEYS
Casey Schauflere20b0432015-05-02 15:11:36 -07006941 LSM_HOOK_INIT(key_alloc, selinux_key_alloc),
6942 LSM_HOOK_INIT(key_free, selinux_key_free),
6943 LSM_HOOK_INIT(key_permission, selinux_key_permission),
6944 LSM_HOOK_INIT(key_getsecurity, selinux_key_getsecurity),
Michael LeMayd7200242006-06-22 14:47:17 -07006945#endif
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02006946
6947#ifdef CONFIG_AUDIT
Casey Schauflere20b0432015-05-02 15:11:36 -07006948 LSM_HOOK_INIT(audit_rule_init, selinux_audit_rule_init),
6949 LSM_HOOK_INIT(audit_rule_known, selinux_audit_rule_known),
6950 LSM_HOOK_INIT(audit_rule_match, selinux_audit_rule_match),
6951 LSM_HOOK_INIT(audit_rule_free, selinux_audit_rule_free),
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02006952#endif
Chenbo Fengec27c352017-10-18 13:00:25 -07006953
6954#ifdef CONFIG_BPF_SYSCALL
6955 LSM_HOOK_INIT(bpf, selinux_bpf),
6956 LSM_HOOK_INIT(bpf_map, selinux_bpf_map),
6957 LSM_HOOK_INIT(bpf_prog, selinux_bpf_prog),
6958 LSM_HOOK_INIT(bpf_map_alloc_security, selinux_bpf_map_alloc),
6959 LSM_HOOK_INIT(bpf_prog_alloc_security, selinux_bpf_prog_alloc),
6960 LSM_HOOK_INIT(bpf_map_free_security, selinux_bpf_map_free),
6961 LSM_HOOK_INIT(bpf_prog_free_security, selinux_bpf_prog_free),
6962#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006963};
6964
6965static __init int selinux_init(void)
6966{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07006967 if (!security_module_enable("selinux")) {
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02006968 selinux_enabled = 0;
6969 return 0;
6970 }
6971
Linus Torvalds1da177e2005-04-16 15:20:36 -07006972 if (!selinux_enabled) {
peter enderborgc103a912018-06-12 10:09:03 +02006973 pr_info("SELinux: Disabled at boot.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006974 return 0;
6975 }
6976
peter enderborgc103a912018-06-12 10:09:03 +02006977 pr_info("SELinux: Initializing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006978
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006979 memset(&selinux_state, 0, sizeof(selinux_state));
Paul Mooree5a5ca92018-03-01 17:38:30 -05006980 enforcing_set(&selinux_state, selinux_enforcing_boot);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006981 selinux_state.checkreqprot = selinux_checkreqprot_boot;
6982 selinux_ss_init(&selinux_state.ss);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006983 selinux_avc_init(&selinux_state.avc);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006984
Linus Torvalds1da177e2005-04-16 15:20:36 -07006985 /* Set the security state for the initial task. */
David Howellsd84f4f92008-11-14 10:39:23 +11006986 cred_init_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006987
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04006988 default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
6989
James Morris7cae7e22006-03-22 00:09:22 -08006990 sel_inode_cache = kmem_cache_create("selinux_inode_security",
6991 sizeof(struct inode_security_struct),
Paul Mundt20c2df82007-07-20 10:11:58 +09006992 0, SLAB_PANIC, NULL);
Sangwoo63205652015-10-21 17:44:30 -04006993 file_security_cache = kmem_cache_create("selinux_file_security",
6994 sizeof(struct file_security_struct),
6995 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006996 avc_init();
6997
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006998 avtab_cache_init();
6999
7000 ebitmap_cache_init();
7001
7002 hashtab_cache_init();
7003
Casey Schauflerd69dece52017-01-18 17:09:05 -08007004 security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks), "selinux");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007005
Paul Moore615e51f2014-06-26 14:33:56 -04007006 if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET))
7007 panic("SELinux: Unable to register AVC netcache callback\n");
7008
Daniel Jurgens8f408ab2017-05-19 15:48:53 +03007009 if (avc_add_callback(selinux_lsm_notifier_avc_callback, AVC_CALLBACK_RESET))
7010 panic("SELinux: Unable to register AVC LSM notifier callback\n");
7011
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007012 if (selinux_enforcing_boot)
peter enderborgc103a912018-06-12 10:09:03 +02007013 pr_debug("SELinux: Starting in enforcing mode\n");
Eric Paris828dfe12008-04-17 13:17:49 -04007014 else
peter enderborgc103a912018-06-12 10:09:03 +02007015 pr_debug("SELinux: Starting in permissive mode\n");
Michael LeMayd7200242006-06-22 14:47:17 -07007016
Linus Torvalds1da177e2005-04-16 15:20:36 -07007017 return 0;
7018}
7019
Al Viroe8c26252010-03-23 06:36:54 -04007020static void delayed_superblock_init(struct super_block *sb, void *unused)
7021{
Al Viro204cc0c2018-12-13 13:41:47 -05007022 selinux_set_mnt_opts(sb, NULL, 0, NULL);
Al Viroe8c26252010-03-23 06:36:54 -04007023}
7024
Linus Torvalds1da177e2005-04-16 15:20:36 -07007025void selinux_complete_init(void)
7026{
peter enderborgc103a912018-06-12 10:09:03 +02007027 pr_debug("SELinux: Completing initialization.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007028
7029 /* Set up any superblocks initialized prior to the policy load. */
peter enderborgc103a912018-06-12 10:09:03 +02007030 pr_debug("SELinux: Setting up existing superblocks.\n");
Al Viroe8c26252010-03-23 06:36:54 -04007031 iterate_supers(delayed_superblock_init, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007032}
7033
7034/* SELinux requires early initialization in order to label
7035 all processes and objects when they are created. */
Kees Cook3d6e5f62018-10-10 17:18:23 -07007036DEFINE_LSM(selinux) = {
Kees Cook07aed2f2018-10-10 17:18:24 -07007037 .name = "selinux",
Kees Cook3d6e5f62018-10-10 17:18:23 -07007038 .init = selinux_init,
7039};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007040
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007041#if defined(CONFIG_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007042
Florian Westphal591bb2782017-07-26 11:40:52 +02007043static const struct nf_hook_ops selinux_nf_ops[] = {
Paul Mooreeffad8d2008-01-29 08:49:27 -05007044 {
7045 .hook = selinux_ipv4_postroute,
Alban Crequy2597a832012-05-14 03:56:39 +00007046 .pf = NFPROTO_IPV4,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007047 .hooknum = NF_INET_POST_ROUTING,
7048 .priority = NF_IP_PRI_SELINUX_LAST,
7049 },
7050 {
7051 .hook = selinux_ipv4_forward,
Alban Crequy2597a832012-05-14 03:56:39 +00007052 .pf = NFPROTO_IPV4,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007053 .hooknum = NF_INET_FORWARD,
7054 .priority = NF_IP_PRI_SELINUX_FIRST,
Paul Moore948bf852008-10-10 10:16:32 -04007055 },
7056 {
7057 .hook = selinux_ipv4_output,
Alban Crequy2597a832012-05-14 03:56:39 +00007058 .pf = NFPROTO_IPV4,
Paul Moore948bf852008-10-10 10:16:32 -04007059 .hooknum = NF_INET_LOCAL_OUT,
7060 .priority = NF_IP_PRI_SELINUX_FIRST,
Jiri Pirko25db6be2014-09-03 17:42:13 +02007061 },
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04007062#if IS_ENABLED(CONFIG_IPV6)
Paul Mooreeffad8d2008-01-29 08:49:27 -05007063 {
7064 .hook = selinux_ipv6_postroute,
Alban Crequy2597a832012-05-14 03:56:39 +00007065 .pf = NFPROTO_IPV6,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007066 .hooknum = NF_INET_POST_ROUTING,
7067 .priority = NF_IP6_PRI_SELINUX_LAST,
7068 },
7069 {
7070 .hook = selinux_ipv6_forward,
Alban Crequy2597a832012-05-14 03:56:39 +00007071 .pf = NFPROTO_IPV6,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007072 .hooknum = NF_INET_FORWARD,
7073 .priority = NF_IP6_PRI_SELINUX_FIRST,
Jiri Pirko25db6be2014-09-03 17:42:13 +02007074 },
Huw Davies2917f572016-06-27 15:06:15 -04007075 {
7076 .hook = selinux_ipv6_output,
7077 .pf = NFPROTO_IPV6,
7078 .hooknum = NF_INET_LOCAL_OUT,
7079 .priority = NF_IP6_PRI_SELINUX_FIRST,
7080 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07007081#endif /* IPV6 */
Jiri Pirko25db6be2014-09-03 17:42:13 +02007082};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007083
Florian Westphal8e71bf72017-04-21 11:49:09 +02007084static int __net_init selinux_nf_register(struct net *net)
7085{
7086 return nf_register_net_hooks(net, selinux_nf_ops,
7087 ARRAY_SIZE(selinux_nf_ops));
7088}
7089
7090static void __net_exit selinux_nf_unregister(struct net *net)
7091{
7092 nf_unregister_net_hooks(net, selinux_nf_ops,
7093 ARRAY_SIZE(selinux_nf_ops));
7094}
7095
7096static struct pernet_operations selinux_net_ops = {
7097 .init = selinux_nf_register,
7098 .exit = selinux_nf_unregister,
7099};
7100
Linus Torvalds1da177e2005-04-16 15:20:36 -07007101static int __init selinux_nf_ip_init(void)
7102{
Jiri Pirko25db6be2014-09-03 17:42:13 +02007103 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007104
7105 if (!selinux_enabled)
Jiri Pirko25db6be2014-09-03 17:42:13 +02007106 return 0;
Eric Parisfadcdb42007-02-22 18:11:31 -05007107
peter enderborgc103a912018-06-12 10:09:03 +02007108 pr_debug("SELinux: Registering netfilter hooks\n");
Eric Parisfadcdb42007-02-22 18:11:31 -05007109
Florian Westphal8e71bf72017-04-21 11:49:09 +02007110 err = register_pernet_subsys(&selinux_net_ops);
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07007111 if (err)
Florian Westphal8e71bf72017-04-21 11:49:09 +02007112 panic("SELinux: register_pernet_subsys: error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007113
Jiri Pirko25db6be2014-09-03 17:42:13 +02007114 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007115}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007116__initcall(selinux_nf_ip_init);
7117
7118#ifdef CONFIG_SECURITY_SELINUX_DISABLE
7119static void selinux_nf_ip_exit(void)
7120{
peter enderborgc103a912018-06-12 10:09:03 +02007121 pr_debug("SELinux: Unregistering netfilter hooks\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007122
Florian Westphal8e71bf72017-04-21 11:49:09 +02007123 unregister_pernet_subsys(&selinux_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007124}
7125#endif
7126
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007127#else /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007128
7129#ifdef CONFIG_SECURITY_SELINUX_DISABLE
7130#define selinux_nf_ip_exit()
7131#endif
7132
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007133#endif /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007134
7135#ifdef CONFIG_SECURITY_SELINUX_DISABLE
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007136int selinux_disable(struct selinux_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007137{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007138 if (state->initialized) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007139 /* Not permitted after initial policy load. */
7140 return -EINVAL;
7141 }
7142
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007143 if (state->disabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007144 /* Only do this once. */
7145 return -EINVAL;
7146 }
7147
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007148 state->disabled = 1;
7149
peter enderborgc103a912018-06-12 10:09:03 +02007150 pr_info("SELinux: Disabled at runtime.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007151
Stephen Smalley30d55282006-05-03 10:52:36 -04007152 selinux_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007153
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07007154 security_delete_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007155
Eric Parisaf8ff042009-09-20 21:23:01 -04007156 /* Try to destroy the avc node cache */
7157 avc_disable();
7158
Linus Torvalds1da177e2005-04-16 15:20:36 -07007159 /* Unregister netfilter hooks. */
7160 selinux_nf_ip_exit();
7161
7162 /* Unregister selinuxfs. */
7163 exit_sel_fs();
7164
7165 return 0;
7166}
7167#endif