blob: 8f2285cb9029a88992c70d60281bf4cc5b00f288 [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 Moore3e112172008-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,
David P. Quigley11689d42009-01-16 09:22:03 -0500461 Opt_labelsupport = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462};
463
Al Viro169d68efb2018-12-14 22:44:50 -0500464#define A(s, opt, has_arg) {s, sizeof(s) - 1, opt, has_arg}
465static struct {
466 const char *name;
467 int len;
468 int opt;
469 bool has_arg;
470} tokens[] = {
471 A("context", Opt_context, true),
472 A("fscontext", Opt_fscontext, true),
473 A("defcontext", Opt_defcontext, true),
474 A("rootcontext", Opt_rootcontext, true),
475 A("seclabel", Opt_labelsupport, 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
Eric Parisb43e7252012-10-10 14:27:35 -0400537static int selinux_is_sblabel_mnt(struct super_block *sb)
538{
539 struct superblock_security_struct *sbsec = sb->s_security;
540
Mark Salyzynd5f3a5f2015-02-04 11:34:30 -0500541 return sbsec->behavior == SECURITY_FS_USE_XATTR ||
542 sbsec->behavior == SECURITY_FS_USE_TRANS ||
543 sbsec->behavior == SECURITY_FS_USE_TASK ||
J. Bruce Fields9fc2b4b2015-06-04 15:57:25 -0400544 sbsec->behavior == SECURITY_FS_USE_NATIVE ||
Mark Salyzynd5f3a5f2015-02-04 11:34:30 -0500545 /* Special handling. Genfs but also in-core setxattr handler */
546 !strcmp(sb->s_type->name, "sysfs") ||
547 !strcmp(sb->s_type->name, "pstore") ||
548 !strcmp(sb->s_type->name, "debugfs") ||
Yongqin Liua2c7c6f2017-01-09 10:07:30 -0500549 !strcmp(sb->s_type->name, "tracefs") ||
Stephen Smalley2651225b2017-02-28 10:35:56 -0500550 !strcmp(sb->s_type->name, "rootfs") ||
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500551 (selinux_policycap_cgroupseclabel() &&
Stephen Smalley2651225b2017-02-28 10:35:56 -0500552 (!strcmp(sb->s_type->name, "cgroup") ||
553 !strcmp(sb->s_type->name, "cgroup2")));
Eric Parisb43e7252012-10-10 14:27:35 -0400554}
555
Eric Parisc9180a52007-11-30 13:00:35 -0500556static int sb_finish_set_opts(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
558 struct superblock_security_struct *sbsec = sb->s_security;
559 struct dentry *root = sb->s_root;
David Howellsc6f493d2015-03-17 22:26:22 +0000560 struct inode *root_inode = d_backing_inode(root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 int rc = 0;
562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
564 /* Make sure that the xattr handler exists and that no
565 error other than -ENODATA is returned by getxattr on
566 the root directory. -ENODATA is ok, as this may be
567 the first boot of the SELinux kernel before we have
568 assigned xattr values to the filesystem. */
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200569 if (!(root_inode->i_opflags & IOP_XATTR)) {
peter enderborgc103a912018-06-12 10:09:03 +0200570 pr_warn("SELinux: (dev %s, type %s) has no "
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800571 "xattr support\n", sb->s_id, sb->s_type->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 rc = -EOPNOTSUPP;
573 goto out;
574 }
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200575
576 rc = __vfs_getxattr(root, root_inode, XATTR_NAME_SELINUX, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 if (rc < 0 && rc != -ENODATA) {
578 if (rc == -EOPNOTSUPP)
peter enderborgc103a912018-06-12 10:09:03 +0200579 pr_warn("SELinux: (dev %s, type "
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800580 "%s) has no security xattr handler\n",
581 sb->s_id, sb->s_type->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 else
peter enderborgc103a912018-06-12 10:09:03 +0200583 pr_warn("SELinux: (dev %s, type "
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800584 "%s) getxattr errno %d\n", sb->s_id,
585 sb->s_type->name, -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 goto out;
587 }
588 }
589
Eric Pariseadcabc2012-08-24 15:59:14 -0400590 sbsec->flags |= SE_SBINITIALIZED;
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400591
592 /*
593 * Explicitly set or clear SBLABEL_MNT. It's not sufficient to simply
594 * leave the flag untouched because sb_clone_mnt_opts might be handing
595 * us a superblock that needs the flag to be cleared.
596 */
Eric Parisb43e7252012-10-10 14:27:35 -0400597 if (selinux_is_sblabel_mnt(sb))
Eric Paris12f348b2012-10-09 10:56:25 -0400598 sbsec->flags |= SBLABEL_MNT;
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400599 else
600 sbsec->flags &= ~SBLABEL_MNT;
David P. Quigleyddd29ec2009-09-09 14:25:37 -0400601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 /* Initialize the root inode. */
Eric Parisc9180a52007-11-30 13:00:35 -0500603 rc = inode_doinit_with_dentry(root_inode, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 /* Initialize any other inodes associated with the superblock, e.g.
606 inodes created prior to initial policy load or inodes created
607 during get_sb by a pseudo filesystem that directly
608 populates itself. */
609 spin_lock(&sbsec->isec_lock);
Al Viro8d641242018-12-10 15:34:12 -0500610 while (!list_empty(&sbsec->isec_head)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 struct inode_security_struct *isec =
Al Viro8d641242018-12-10 15:34:12 -0500612 list_first_entry(&sbsec->isec_head,
Eric Parisc9180a52007-11-30 13:00:35 -0500613 struct inode_security_struct, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 struct inode *inode = isec->inode;
Stephen Smalley923190d2014-10-06 16:32:52 -0400615 list_del_init(&isec->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 spin_unlock(&sbsec->isec_lock);
617 inode = igrab(inode);
618 if (inode) {
Eric Parisc9180a52007-11-30 13:00:35 -0500619 if (!IS_PRIVATE(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 inode_doinit(inode);
621 iput(inode);
622 }
623 spin_lock(&sbsec->isec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 }
625 spin_unlock(&sbsec->isec_lock);
626out:
Eric Parisc9180a52007-11-30 13:00:35 -0500627 return rc;
628}
629
Eric Parisc9180a52007-11-30 13:00:35 -0500630static int bad_option(struct superblock_security_struct *sbsec, char flag,
631 u32 old_sid, u32 new_sid)
632{
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500633 char mnt_flags = sbsec->flags & SE_MNTMASK;
634
Eric Parisc9180a52007-11-30 13:00:35 -0500635 /* check if the old mount command had the same options */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500636 if (sbsec->flags & SE_SBINITIALIZED)
Eric Parisc9180a52007-11-30 13:00:35 -0500637 if (!(sbsec->flags & flag) ||
638 (old_sid != new_sid))
639 return 1;
640
641 /* check if we were passed the same options twice,
642 * aka someone passed context=a,context=b
643 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500644 if (!(sbsec->flags & SE_SBINITIALIZED))
645 if (mnt_flags & flag)
Eric Parisc9180a52007-11-30 13:00:35 -0500646 return 1;
647 return 0;
648}
Eric Parise0007522008-03-05 10:31:54 -0500649
Al Virobd323652018-12-13 15:04:59 -0500650static int parse_sid(struct super_block *sb, const char *s, u32 *sid)
651{
652 int rc = security_context_str_to_sid(&selinux_state, s,
653 sid, GFP_KERNEL);
654 if (rc)
655 pr_warn("SELinux: security_context_str_to_sid"
656 "(%s) failed for (dev %s, type %s) errno=%d\n",
657 s, sb->s_id, sb->s_type->name, rc);
658 return rc;
659}
660
Eric Parisc9180a52007-11-30 13:00:35 -0500661/*
662 * Allow filesystems with binary mount data to explicitly set mount point
663 * labeling information.
664 */
Eric Parise0007522008-03-05 10:31:54 -0500665static int selinux_set_mnt_opts(struct super_block *sb,
Al Viro204cc0c2018-12-13 13:41:47 -0500666 void *mnt_opts,
David Quigley649f6e72013-05-22 12:50:36 -0400667 unsigned long kern_flags,
668 unsigned long *set_kern_flags)
Eric Parisc9180a52007-11-30 13:00:35 -0500669{
David Howells275bb412008-11-14 10:39:19 +1100670 const struct cred *cred = current_cred();
Eric Parisc9180a52007-11-30 13:00:35 -0500671 struct superblock_security_struct *sbsec = sb->s_security;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500672 struct dentry *root = sbsec->sb->s_root;
Al Virobd323652018-12-13 15:04:59 -0500673 struct selinux_mnt_opts *opts = mnt_opts;
Paul Moore2c971652016-04-19 16:36:28 -0400674 struct inode_security_struct *root_isec;
Eric Parisc9180a52007-11-30 13:00:35 -0500675 u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
676 u32 defcontext_sid = 0;
Al Virobd323652018-12-13 15:04:59 -0500677 int rc = 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500678
679 mutex_lock(&sbsec->lock);
680
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500681 if (!selinux_state.initialized) {
Al Virobd323652018-12-13 15:04:59 -0500682 if (!opts) {
Eric Parisc9180a52007-11-30 13:00:35 -0500683 /* Defer initialization until selinux_complete_init,
684 after the initial policy is loaded and the security
685 server is ready to handle calls. */
Eric Parisc9180a52007-11-30 13:00:35 -0500686 goto out;
687 }
688 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +0200689 pr_warn("SELinux: Unable to set superblock options "
Eric Paris744ba352008-04-17 11:52:44 -0400690 "before the security server is initialized\n");
Eric Parisc9180a52007-11-30 13:00:35 -0500691 goto out;
692 }
David Quigley649f6e72013-05-22 12:50:36 -0400693 if (kern_flags && !set_kern_flags) {
694 /* Specifying internal flags without providing a place to
695 * place the results is not allowed */
696 rc = -EINVAL;
697 goto out;
698 }
Eric Parisc9180a52007-11-30 13:00:35 -0500699
700 /*
Eric Parise0007522008-03-05 10:31:54 -0500701 * Binary mount data FS will come through this function twice. Once
702 * from an explicit call and once from the generic calls from the vfs.
703 * Since the generic VFS calls will not contain any security mount data
704 * we need to skip the double mount verification.
705 *
706 * This does open a hole in which we will not notice if the first
707 * mount using this sb set explict options and a second mount using
708 * this sb does not set any security options. (The first options
709 * will be used for both mounts)
710 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500711 if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
Al Virobd323652018-12-13 15:04:59 -0500712 && !opts)
Eric Parisf5269712008-05-14 11:27:45 -0400713 goto out;
Eric Parise0007522008-03-05 10:31:54 -0500714
Paul Moore2c971652016-04-19 16:36:28 -0400715 root_isec = backing_inode_security_novalidate(root);
716
Eric Parise0007522008-03-05 10:31:54 -0500717 /*
Eric Parisc9180a52007-11-30 13:00:35 -0500718 * parse the mount options, check if they are valid sids.
719 * also check if someone is trying to mount the same sb more
720 * than once with different security options.
721 */
Al Virobd323652018-12-13 15:04:59 -0500722 if (opts) {
723 if (opts->fscontext) {
724 rc = parse_sid(sb, opts->fscontext, &fscontext_sid);
725 if (rc)
726 goto out;
Eric Parisc9180a52007-11-30 13:00:35 -0500727 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
728 fscontext_sid))
729 goto out_double_mount;
Eric Parisc9180a52007-11-30 13:00:35 -0500730 sbsec->flags |= FSCONTEXT_MNT;
Al Virobd323652018-12-13 15:04:59 -0500731 }
732 if (opts->context) {
733 rc = parse_sid(sb, opts->context, &context_sid);
734 if (rc)
735 goto out;
Eric Parisc9180a52007-11-30 13:00:35 -0500736 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
737 context_sid))
738 goto out_double_mount;
Eric Parisc9180a52007-11-30 13:00:35 -0500739 sbsec->flags |= CONTEXT_MNT;
Al Virobd323652018-12-13 15:04:59 -0500740 }
741 if (opts->rootcontext) {
742 rc = parse_sid(sb, opts->rootcontext, &rootcontext_sid);
743 if (rc)
744 goto out;
Eric Parisc9180a52007-11-30 13:00:35 -0500745 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
746 rootcontext_sid))
747 goto out_double_mount;
Eric Parisc9180a52007-11-30 13:00:35 -0500748 sbsec->flags |= ROOTCONTEXT_MNT;
Al Virobd323652018-12-13 15:04:59 -0500749 }
750 if (opts->defcontext) {
751 rc = parse_sid(sb, opts->defcontext, &defcontext_sid);
752 if (rc)
753 goto out;
Eric Parisc9180a52007-11-30 13:00:35 -0500754 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
755 defcontext_sid))
756 goto out_double_mount;
Eric Parisc9180a52007-11-30 13:00:35 -0500757 sbsec->flags |= DEFCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500758 }
759 }
760
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500761 if (sbsec->flags & SE_SBINITIALIZED) {
Eric Parisc9180a52007-11-30 13:00:35 -0500762 /* previously mounted with options, but not on this attempt? */
Al Virobd323652018-12-13 15:04:59 -0500763 if ((sbsec->flags & SE_MNTMASK) && !opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500764 goto out_double_mount;
765 rc = 0;
766 goto out;
767 }
768
James Morris089be432008-07-15 18:32:49 +1000769 if (strcmp(sb->s_type->name, "proc") == 0)
Stephen Smalley134509d2015-06-04 16:22:17 -0400770 sbsec->flags |= SE_SBPROC | SE_SBGENFS;
771
Stephen Smalley8e014722015-06-04 16:22:17 -0400772 if (!strcmp(sb->s_type->name, "debugfs") ||
Jeff Vander Stoep6a391182017-06-20 09:35:33 -0700773 !strcmp(sb->s_type->name, "tracefs") ||
Stephen Smalley8e014722015-06-04 16:22:17 -0400774 !strcmp(sb->s_type->name, "sysfs") ||
Antonio Murdaca901ef842017-02-09 17:02:42 +0100775 !strcmp(sb->s_type->name, "pstore") ||
776 !strcmp(sb->s_type->name, "cgroup") ||
777 !strcmp(sb->s_type->name, "cgroup2"))
Stephen Smalley134509d2015-06-04 16:22:17 -0400778 sbsec->flags |= SE_SBGENFS;
Eric Parisc9180a52007-11-30 13:00:35 -0500779
David Quigleyeb9ae682013-05-22 12:50:37 -0400780 if (!sbsec->behavior) {
781 /*
782 * Determine the labeling behavior to use for this
783 * filesystem type.
784 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500785 rc = security_fs_use(&selinux_state, sb);
David Quigleyeb9ae682013-05-22 12:50:37 -0400786 if (rc) {
peter enderborgc103a912018-06-12 10:09:03 +0200787 pr_warn("%s: security_fs_use(%s) returned %d\n",
David Quigleyeb9ae682013-05-22 12:50:37 -0400788 __func__, sb->s_type->name, rc);
789 goto out;
790 }
Eric Parisc9180a52007-11-30 13:00:35 -0500791 }
Seth Forsheeaad82892016-04-26 14:36:20 -0500792
793 /*
Stephen Smalley01593d32017-01-09 10:07:31 -0500794 * If this is a user namespace mount and the filesystem type is not
795 * explicitly whitelisted, then no contexts are allowed on the command
796 * line and security labels must be ignored.
Seth Forsheeaad82892016-04-26 14:36:20 -0500797 */
Stephen Smalley01593d32017-01-09 10:07:31 -0500798 if (sb->s_user_ns != &init_user_ns &&
799 strcmp(sb->s_type->name, "tmpfs") &&
800 strcmp(sb->s_type->name, "ramfs") &&
801 strcmp(sb->s_type->name, "devpts")) {
Seth Forsheeaad82892016-04-26 14:36:20 -0500802 if (context_sid || fscontext_sid || rootcontext_sid ||
803 defcontext_sid) {
804 rc = -EACCES;
805 goto out;
806 }
807 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
808 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500809 rc = security_transition_sid(&selinux_state,
810 current_sid(),
811 current_sid(),
Seth Forsheeaad82892016-04-26 14:36:20 -0500812 SECCLASS_FILE, NULL,
813 &sbsec->mntpoint_sid);
814 if (rc)
815 goto out;
816 }
817 goto out_set_opts;
818 }
819
Eric Parisc9180a52007-11-30 13:00:35 -0500820 /* sets the context of the superblock for the fs being mounted. */
821 if (fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100822 rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500823 if (rc)
824 goto out;
825
826 sbsec->sid = fscontext_sid;
827 }
828
829 /*
830 * Switch to using mount point labeling behavior.
831 * sets the label used on all file below the mountpoint, and will set
832 * the superblock context if not already set.
833 */
David Quigleyeb9ae682013-05-22 12:50:37 -0400834 if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !context_sid) {
835 sbsec->behavior = SECURITY_FS_USE_NATIVE;
836 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
837 }
838
Eric Parisc9180a52007-11-30 13:00:35 -0500839 if (context_sid) {
840 if (!fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100841 rc = may_context_mount_sb_relabel(context_sid, sbsec,
842 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500843 if (rc)
844 goto out;
845 sbsec->sid = context_sid;
846 } else {
David Howells275bb412008-11-14 10:39:19 +1100847 rc = may_context_mount_inode_relabel(context_sid, sbsec,
848 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500849 if (rc)
850 goto out;
851 }
852 if (!rootcontext_sid)
853 rootcontext_sid = context_sid;
854
855 sbsec->mntpoint_sid = context_sid;
856 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
857 }
858
859 if (rootcontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100860 rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec,
861 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500862 if (rc)
863 goto out;
864
865 root_isec->sid = rootcontext_sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -0500866 root_isec->initialized = LABEL_INITIALIZED;
Eric Parisc9180a52007-11-30 13:00:35 -0500867 }
868
869 if (defcontext_sid) {
David Quigleyeb9ae682013-05-22 12:50:37 -0400870 if (sbsec->behavior != SECURITY_FS_USE_XATTR &&
871 sbsec->behavior != SECURITY_FS_USE_NATIVE) {
Eric Parisc9180a52007-11-30 13:00:35 -0500872 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +0200873 pr_warn("SELinux: defcontext option is "
Eric Parisc9180a52007-11-30 13:00:35 -0500874 "invalid for this filesystem type\n");
875 goto out;
876 }
877
878 if (defcontext_sid != sbsec->def_sid) {
879 rc = may_context_mount_inode_relabel(defcontext_sid,
David Howells275bb412008-11-14 10:39:19 +1100880 sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500881 if (rc)
882 goto out;
883 }
884
885 sbsec->def_sid = defcontext_sid;
886 }
887
Seth Forsheeaad82892016-04-26 14:36:20 -0500888out_set_opts:
Eric Parisc9180a52007-11-30 13:00:35 -0500889 rc = sb_finish_set_opts(sb);
890out:
Eric Parisbc7e9822006-09-25 23:32:02 -0700891 mutex_unlock(&sbsec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 return rc;
Eric Parisc9180a52007-11-30 13:00:35 -0500893out_double_mount:
894 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +0200895 pr_warn("SELinux: mount invalid. Same superblock, different "
Al Virobd323652018-12-13 15:04:59 -0500896 "security settings for (dev %s, type %s)\n", sb->s_id,
897 sb->s_type->name);
Eric Parisc9180a52007-11-30 13:00:35 -0500898 goto out;
899}
900
Jeff Layton094f7b62013-04-01 08:14:24 -0400901static int selinux_cmp_sb_context(const struct super_block *oldsb,
902 const struct super_block *newsb)
903{
904 struct superblock_security_struct *old = oldsb->s_security;
905 struct superblock_security_struct *new = newsb->s_security;
906 char oldflags = old->flags & SE_MNTMASK;
907 char newflags = new->flags & SE_MNTMASK;
908
909 if (oldflags != newflags)
910 goto mismatch;
911 if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid)
912 goto mismatch;
913 if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid)
914 goto mismatch;
915 if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid)
916 goto mismatch;
917 if (oldflags & ROOTCONTEXT_MNT) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500918 struct inode_security_struct *oldroot = backing_inode_security(oldsb->s_root);
919 struct inode_security_struct *newroot = backing_inode_security(newsb->s_root);
Jeff Layton094f7b62013-04-01 08:14:24 -0400920 if (oldroot->sid != newroot->sid)
921 goto mismatch;
922 }
923 return 0;
924mismatch:
peter enderborgc103a912018-06-12 10:09:03 +0200925 pr_warn("SELinux: mount invalid. Same superblock, "
Jeff Layton094f7b62013-04-01 08:14:24 -0400926 "different security settings for (dev %s, "
927 "type %s)\n", newsb->s_id, newsb->s_type->name);
928 return -EBUSY;
929}
930
931static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400932 struct super_block *newsb,
933 unsigned long kern_flags,
934 unsigned long *set_kern_flags)
Eric Parisc9180a52007-11-30 13:00:35 -0500935{
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400936 int rc = 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500937 const struct superblock_security_struct *oldsbsec = oldsb->s_security;
938 struct superblock_security_struct *newsbsec = newsb->s_security;
939
940 int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT);
941 int set_context = (oldsbsec->flags & CONTEXT_MNT);
942 int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT);
943
Eric Paris0f5e6422008-04-21 16:24:11 -0400944 /*
945 * if the parent was able to be mounted it clearly had no special lsm
Al Viroe8c26252010-03-23 06:36:54 -0400946 * mount options. thus we can safely deal with this superblock later
Eric Paris0f5e6422008-04-21 16:24:11 -0400947 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500948 if (!selinux_state.initialized)
Jeff Layton094f7b62013-04-01 08:14:24 -0400949 return 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500950
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400951 /*
952 * Specifying internal flags without providing a place to
953 * place the results is not allowed.
954 */
955 if (kern_flags && !set_kern_flags)
956 return -EINVAL;
957
Eric Parisc9180a52007-11-30 13:00:35 -0500958 /* how can we clone if the old one wasn't set up?? */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500959 BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED));
Eric Parisc9180a52007-11-30 13:00:35 -0500960
Jeff Layton094f7b62013-04-01 08:14:24 -0400961 /* if fs is reusing a sb, make sure that the contexts match */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500962 if (newsbsec->flags & SE_SBINITIALIZED)
Jeff Layton094f7b62013-04-01 08:14:24 -0400963 return selinux_cmp_sb_context(oldsb, newsb);
Eric Paris5a552612008-04-09 14:08:35 -0400964
Eric Parisc9180a52007-11-30 13:00:35 -0500965 mutex_lock(&newsbsec->lock);
966
967 newsbsec->flags = oldsbsec->flags;
968
969 newsbsec->sid = oldsbsec->sid;
970 newsbsec->def_sid = oldsbsec->def_sid;
971 newsbsec->behavior = oldsbsec->behavior;
972
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400973 if (newsbsec->behavior == SECURITY_FS_USE_NATIVE &&
974 !(kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500975 rc = security_fs_use(&selinux_state, newsb);
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400976 if (rc)
977 goto out;
978 }
979
980 if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !set_context) {
981 newsbsec->behavior = SECURITY_FS_USE_NATIVE;
982 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
983 }
984
Eric Parisc9180a52007-11-30 13:00:35 -0500985 if (set_context) {
986 u32 sid = oldsbsec->mntpoint_sid;
987
988 if (!set_fscontext)
989 newsbsec->sid = sid;
990 if (!set_rootcontext) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500991 struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
Eric Parisc9180a52007-11-30 13:00:35 -0500992 newisec->sid = sid;
993 }
994 newsbsec->mntpoint_sid = sid;
995 }
996 if (set_rootcontext) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500997 const struct inode_security_struct *oldisec = backing_inode_security(oldsb->s_root);
998 struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
Eric Parisc9180a52007-11-30 13:00:35 -0500999
1000 newisec->sid = oldisec->sid;
1001 }
1002
1003 sb_finish_set_opts(newsb);
Scott Mayhew0b4d3452017-06-05 11:45:04 -04001004out:
Eric Parisc9180a52007-11-30 13:00:35 -05001005 mutex_unlock(&newsbsec->lock);
Scott Mayhew0b4d3452017-06-05 11:45:04 -04001006 return rc;
Eric Parisc9180a52007-11-30 13:00:35 -05001007}
1008
Al Viroba641862018-12-14 20:28:15 -05001009static int selinux_add_opt(int token, const char *s, void **mnt_opts)
1010{
1011 struct selinux_mnt_opts *opts = *mnt_opts;
1012
Al Viro169d68efb2018-12-14 22:44:50 -05001013 if (token == Opt_labelsupport) /* eaten and completely ignored */
1014 return 0;
1015
Al Viroba641862018-12-14 20:28:15 -05001016 if (!opts) {
1017 opts = kzalloc(sizeof(struct selinux_mnt_opts), GFP_KERNEL);
1018 if (!opts)
1019 return -ENOMEM;
1020 *mnt_opts = opts;
1021 }
1022 if (!s)
1023 return -ENOMEM;
1024 switch (token) {
1025 case Opt_context:
1026 if (opts->context || opts->defcontext)
1027 goto Einval;
1028 opts->context = s;
1029 break;
1030 case Opt_fscontext:
1031 if (opts->fscontext)
1032 goto Einval;
1033 opts->fscontext = s;
1034 break;
1035 case Opt_rootcontext:
1036 if (opts->rootcontext)
1037 goto Einval;
1038 opts->rootcontext = s;
1039 break;
1040 case Opt_defcontext:
1041 if (opts->context || opts->defcontext)
1042 goto Einval;
1043 opts->defcontext = s;
1044 break;
1045 }
1046 return 0;
1047Einval:
1048 pr_warn(SEL_MOUNT_FAIL_MSG);
Al Viroba641862018-12-14 20:28:15 -05001049 return -EINVAL;
1050}
1051
Adrian Bunk2e1479d2008-03-17 22:29:23 +02001052static int selinux_parse_opts_str(char *options,
Al Viro204cc0c2018-12-13 13:41:47 -05001053 void **mnt_opts)
Eric Parisc9180a52007-11-30 13:00:35 -05001054{
Al Viro169d68efb2018-12-14 22:44:50 -05001055 char *p = options, *next;
1056 int rc;
Eric Parisc9180a52007-11-30 13:00:35 -05001057
1058 /* Standard string-based options. */
Al Viro169d68efb2018-12-14 22:44:50 -05001059 for (p = options; *p; p = next) {
1060 int token, len;
1061 char *arg = NULL;
Eric Parisc9180a52007-11-30 13:00:35 -05001062
Al Viro169d68efb2018-12-14 22:44:50 -05001063 next = strchr(p, '|');
1064 if (next) {
1065 len = next++ - p;
1066 } else {
1067 len = strlen(p);
1068 next = p + len;
1069 }
1070
1071 if (!len)
Eric Parisc9180a52007-11-30 13:00:35 -05001072 continue;
1073
Al Viro169d68efb2018-12-14 22:44:50 -05001074 token = match_opt_prefix(p, len, &arg);
1075 if (arg)
1076 arg = kmemdup_nul(arg, p + len - arg, GFP_KERNEL);
Al Viroba641862018-12-14 20:28:15 -05001077 rc = selinux_add_opt(token, arg, mnt_opts);
Al Viro169d68efb2018-12-14 22:44:50 -05001078 if (rc) {
Al Viroba641862018-12-14 20:28:15 -05001079 kfree(arg);
Al Viro169d68efb2018-12-14 22:44:50 -05001080 selinux_free_mnt_opts(*mnt_opts);
1081 *mnt_opts = NULL;
Al Viroba641862018-12-14 20:28:15 -05001082 return rc;
Eric Parisc9180a52007-11-30 13:00:35 -05001083 }
1084 }
Eric Parise0007522008-03-05 10:31:54 -05001085 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086}
1087
Al Viroe3489f82018-12-13 00:24:36 -05001088static int show_sid(struct seq_file *m, u32 sid)
Eric Paris2069f452008-07-04 09:47:13 +10001089{
Al Viroe3489f82018-12-13 00:24:36 -05001090 char *context = NULL;
1091 u32 len;
1092 int rc;
Eric Paris2069f452008-07-04 09:47:13 +10001093
Al Viroe3489f82018-12-13 00:24:36 -05001094 rc = security_sid_to_context(&selinux_state, sid,
1095 &context, &len);
1096 if (!rc) {
1097 bool has_comma = context && strchr(context, ',');
David P. Quigley11689d42009-01-16 09:22:03 -05001098
Eric Paris2069f452008-07-04 09:47:13 +10001099 if (has_comma)
1100 seq_putc(m, '\"');
Al Viroe3489f82018-12-13 00:24:36 -05001101 seq_escape(m, context, "\"\n\\");
Eric Paris2069f452008-07-04 09:47:13 +10001102 if (has_comma)
1103 seq_putc(m, '\"');
1104 }
Al Viroe3489f82018-12-13 00:24:36 -05001105 kfree(context);
1106 return rc;
Eric Paris2069f452008-07-04 09:47:13 +10001107}
1108
1109static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
1110{
Al Viroe3489f82018-12-13 00:24:36 -05001111 struct superblock_security_struct *sbsec = sb->s_security;
Eric Paris2069f452008-07-04 09:47:13 +10001112 int rc;
1113
Al Viroe3489f82018-12-13 00:24:36 -05001114 if (!(sbsec->flags & SE_SBINITIALIZED))
1115 return 0;
1116
1117 if (!selinux_state.initialized)
1118 return 0;
1119
1120 if (sbsec->flags & FSCONTEXT_MNT) {
1121 seq_putc(m, ',');
1122 seq_puts(m, FSCONTEXT_STR);
1123 rc = show_sid(m, sbsec->sid);
1124 if (rc)
1125 return rc;
Eric Paris383795c2008-07-29 17:07:26 -04001126 }
Al Viroe3489f82018-12-13 00:24:36 -05001127 if (sbsec->flags & CONTEXT_MNT) {
1128 seq_putc(m, ',');
1129 seq_puts(m, CONTEXT_STR);
1130 rc = show_sid(m, sbsec->mntpoint_sid);
1131 if (rc)
1132 return rc;
1133 }
1134 if (sbsec->flags & DEFCONTEXT_MNT) {
1135 seq_putc(m, ',');
1136 seq_puts(m, DEFCONTEXT_STR);
1137 rc = show_sid(m, sbsec->def_sid);
1138 if (rc)
1139 return rc;
1140 }
1141 if (sbsec->flags & ROOTCONTEXT_MNT) {
1142 struct dentry *root = sbsec->sb->s_root;
1143 struct inode_security_struct *isec = backing_inode_security(root);
1144 seq_putc(m, ',');
1145 seq_puts(m, ROOTCONTEXT_STR);
1146 rc = show_sid(m, isec->sid);
1147 if (rc)
1148 return rc;
1149 }
1150 if (sbsec->flags & SBLABEL_MNT) {
1151 seq_putc(m, ',');
1152 seq_puts(m, LABELSUPP_STR);
1153 }
1154 return 0;
Eric Paris2069f452008-07-04 09:47:13 +10001155}
1156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157static inline u16 inode_mode_to_security_class(umode_t mode)
1158{
1159 switch (mode & S_IFMT) {
1160 case S_IFSOCK:
1161 return SECCLASS_SOCK_FILE;
1162 case S_IFLNK:
1163 return SECCLASS_LNK_FILE;
1164 case S_IFREG:
1165 return SECCLASS_FILE;
1166 case S_IFBLK:
1167 return SECCLASS_BLK_FILE;
1168 case S_IFDIR:
1169 return SECCLASS_DIR;
1170 case S_IFCHR:
1171 return SECCLASS_CHR_FILE;
1172 case S_IFIFO:
1173 return SECCLASS_FIFO_FILE;
1174
1175 }
1176
1177 return SECCLASS_FILE;
1178}
1179
James Morris13402582005-09-30 14:24:34 -04001180static inline int default_protocol_stream(int protocol)
1181{
1182 return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP);
1183}
1184
1185static inline int default_protocol_dgram(int protocol)
1186{
1187 return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
1188}
1189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190static inline u16 socket_type_to_security_class(int family, int type, int protocol)
1191{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001192 int extsockclass = selinux_policycap_extsockclass();
Stephen Smalleyda69a532017-01-09 10:07:30 -05001193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 switch (family) {
1195 case PF_UNIX:
1196 switch (type) {
1197 case SOCK_STREAM:
1198 case SOCK_SEQPACKET:
1199 return SECCLASS_UNIX_STREAM_SOCKET;
1200 case SOCK_DGRAM:
Luis Ressel2a764b52017-07-25 15:13:41 -04001201 case SOCK_RAW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 return SECCLASS_UNIX_DGRAM_SOCKET;
1203 }
1204 break;
1205 case PF_INET:
1206 case PF_INET6:
1207 switch (type) {
1208 case SOCK_STREAM:
Stephen Smalleyda69a532017-01-09 10:07:30 -05001209 case SOCK_SEQPACKET:
James Morris13402582005-09-30 14:24:34 -04001210 if (default_protocol_stream(protocol))
1211 return SECCLASS_TCP_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001212 else if (extsockclass && protocol == IPPROTO_SCTP)
1213 return SECCLASS_SCTP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001214 else
1215 return SECCLASS_RAWIP_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 case SOCK_DGRAM:
James Morris13402582005-09-30 14:24:34 -04001217 if (default_protocol_dgram(protocol))
1218 return SECCLASS_UDP_SOCKET;
Stephen Smalleyef379792017-01-09 10:07:31 -05001219 else if (extsockclass && (protocol == IPPROTO_ICMP ||
1220 protocol == IPPROTO_ICMPV6))
Stephen Smalleyda69a532017-01-09 10:07:30 -05001221 return SECCLASS_ICMP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001222 else
1223 return SECCLASS_RAWIP_SOCKET;
James Morris2ee92d42006-11-13 16:09:01 -08001224 case SOCK_DCCP:
1225 return SECCLASS_DCCP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001226 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 return SECCLASS_RAWIP_SOCKET;
1228 }
1229 break;
1230 case PF_NETLINK:
1231 switch (protocol) {
1232 case NETLINK_ROUTE:
1233 return SECCLASS_NETLINK_ROUTE_SOCKET;
Pavel Emelyanov7f1fb602011-12-06 07:56:43 +00001234 case NETLINK_SOCK_DIAG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 return SECCLASS_NETLINK_TCPDIAG_SOCKET;
1236 case NETLINK_NFLOG:
1237 return SECCLASS_NETLINK_NFLOG_SOCKET;
1238 case NETLINK_XFRM:
1239 return SECCLASS_NETLINK_XFRM_SOCKET;
1240 case NETLINK_SELINUX:
1241 return SECCLASS_NETLINK_SELINUX_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001242 case NETLINK_ISCSI:
1243 return SECCLASS_NETLINK_ISCSI_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 case NETLINK_AUDIT:
1245 return SECCLASS_NETLINK_AUDIT_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001246 case NETLINK_FIB_LOOKUP:
1247 return SECCLASS_NETLINK_FIB_LOOKUP_SOCKET;
1248 case NETLINK_CONNECTOR:
1249 return SECCLASS_NETLINK_CONNECTOR_SOCKET;
1250 case NETLINK_NETFILTER:
1251 return SECCLASS_NETLINK_NETFILTER_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 case NETLINK_DNRTMSG:
1253 return SECCLASS_NETLINK_DNRT_SOCKET;
James Morris0c9b7942005-04-16 15:24:13 -07001254 case NETLINK_KOBJECT_UEVENT:
1255 return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001256 case NETLINK_GENERIC:
1257 return SECCLASS_NETLINK_GENERIC_SOCKET;
1258 case NETLINK_SCSITRANSPORT:
1259 return SECCLASS_NETLINK_SCSITRANSPORT_SOCKET;
1260 case NETLINK_RDMA:
1261 return SECCLASS_NETLINK_RDMA_SOCKET;
1262 case NETLINK_CRYPTO:
1263 return SECCLASS_NETLINK_CRYPTO_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 default:
1265 return SECCLASS_NETLINK_SOCKET;
1266 }
1267 case PF_PACKET:
1268 return SECCLASS_PACKET_SOCKET;
1269 case PF_KEY:
1270 return SECCLASS_KEY_SOCKET;
Christopher J. PeBenito3e3ff152006-06-09 00:25:03 -07001271 case PF_APPLETALK:
1272 return SECCLASS_APPLETALK_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 }
1274
Stephen Smalleyda69a532017-01-09 10:07:30 -05001275 if (extsockclass) {
1276 switch (family) {
1277 case PF_AX25:
1278 return SECCLASS_AX25_SOCKET;
1279 case PF_IPX:
1280 return SECCLASS_IPX_SOCKET;
1281 case PF_NETROM:
1282 return SECCLASS_NETROM_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001283 case PF_ATMPVC:
1284 return SECCLASS_ATMPVC_SOCKET;
1285 case PF_X25:
1286 return SECCLASS_X25_SOCKET;
1287 case PF_ROSE:
1288 return SECCLASS_ROSE_SOCKET;
1289 case PF_DECnet:
1290 return SECCLASS_DECNET_SOCKET;
1291 case PF_ATMSVC:
1292 return SECCLASS_ATMSVC_SOCKET;
1293 case PF_RDS:
1294 return SECCLASS_RDS_SOCKET;
1295 case PF_IRDA:
1296 return SECCLASS_IRDA_SOCKET;
1297 case PF_PPPOX:
1298 return SECCLASS_PPPOX_SOCKET;
1299 case PF_LLC:
1300 return SECCLASS_LLC_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001301 case PF_CAN:
1302 return SECCLASS_CAN_SOCKET;
1303 case PF_TIPC:
1304 return SECCLASS_TIPC_SOCKET;
1305 case PF_BLUETOOTH:
1306 return SECCLASS_BLUETOOTH_SOCKET;
1307 case PF_IUCV:
1308 return SECCLASS_IUCV_SOCKET;
1309 case PF_RXRPC:
1310 return SECCLASS_RXRPC_SOCKET;
1311 case PF_ISDN:
1312 return SECCLASS_ISDN_SOCKET;
1313 case PF_PHONET:
1314 return SECCLASS_PHONET_SOCKET;
1315 case PF_IEEE802154:
1316 return SECCLASS_IEEE802154_SOCKET;
1317 case PF_CAIF:
1318 return SECCLASS_CAIF_SOCKET;
1319 case PF_ALG:
1320 return SECCLASS_ALG_SOCKET;
1321 case PF_NFC:
1322 return SECCLASS_NFC_SOCKET;
1323 case PF_VSOCK:
1324 return SECCLASS_VSOCK_SOCKET;
1325 case PF_KCM:
1326 return SECCLASS_KCM_SOCKET;
1327 case PF_QIPCRTR:
1328 return SECCLASS_QIPCRTR_SOCKET;
Linus Torvalds3051bf32017-02-22 10:15:09 -08001329 case PF_SMC:
1330 return SECCLASS_SMC_SOCKET;
Björn Töpel68e8b842018-05-02 13:01:22 +02001331 case PF_XDP:
1332 return SECCLASS_XDP_SOCKET;
1333#if PF_MAX > 45
Stephen Smalleyda69a532017-01-09 10:07:30 -05001334#error New address family defined, please update this function.
1335#endif
1336 }
1337 }
1338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 return SECCLASS_SOCKET;
1340}
1341
Stephen Smalley134509d2015-06-04 16:22:17 -04001342static int selinux_genfs_get_sid(struct dentry *dentry,
1343 u16 tclass,
1344 u16 flags,
1345 u32 *sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001347 int rc;
Al Virofc640052016-04-10 01:33:30 -04001348 struct super_block *sb = dentry->d_sb;
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001349 char *buffer, *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Eric Paris828dfe12008-04-17 13:17:49 -04001351 buffer = (char *)__get_free_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 if (!buffer)
1353 return -ENOMEM;
1354
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001355 path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
1356 if (IS_ERR(path))
1357 rc = PTR_ERR(path);
1358 else {
Stephen Smalley134509d2015-06-04 16:22:17 -04001359 if (flags & SE_SBPROC) {
1360 /* each process gets a /proc/PID/ entry. Strip off the
1361 * PID part to get a valid selinux labeling.
1362 * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
1363 while (path[1] >= '0' && path[1] <= '9') {
1364 path[1] = '/';
1365 path++;
1366 }
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001367 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001368 rc = security_genfs_sid(&selinux_state, sb->s_type->name,
1369 path, tclass, sid);
Stephen Smalley7bb185e2018-09-04 16:51:36 -04001370 if (rc == -ENOENT) {
1371 /* No match in policy, mark as unlabeled. */
1372 *sid = SECINITSID_UNLABELED;
1373 rc = 0;
1374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 free_page((unsigned long)buffer);
1377 return rc;
1378}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380/* The inode's security attributes must be initialized before first use. */
1381static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
1382{
1383 struct superblock_security_struct *sbsec = NULL;
1384 struct inode_security_struct *isec = inode->i_security;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001385 u32 task_sid, sid = 0;
1386 u16 sclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 struct dentry *dentry;
1388#define INITCONTEXTLEN 255
1389 char *context = NULL;
1390 unsigned len = 0;
1391 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05001393 if (isec->initialized == LABEL_INITIALIZED)
Andreas Gruenbacher13457d02016-11-10 22:18:29 +01001394 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001396 spin_lock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05001397 if (isec->initialized == LABEL_INITIALIZED)
Eric Paris23970742006-09-25 23:32:01 -07001398 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Andreas Gruenbacher13457d02016-11-10 22:18:29 +01001400 if (isec->sclass == SECCLASS_FILE)
1401 isec->sclass = inode_mode_to_security_class(inode->i_mode);
1402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 sbsec = inode->i_sb->s_security;
David P. Quigley0d90a7e2009-01-16 09:22:02 -05001404 if (!(sbsec->flags & SE_SBINITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 /* Defer initialization until selinux_complete_init,
1406 after the initial policy is loaded and the security
1407 server is ready to handle calls. */
1408 spin_lock(&sbsec->isec_lock);
1409 if (list_empty(&isec->list))
1410 list_add(&isec->list, &sbsec->isec_head);
1411 spin_unlock(&sbsec->isec_lock);
Eric Paris23970742006-09-25 23:32:01 -07001412 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 }
1414
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001415 sclass = isec->sclass;
1416 task_sid = isec->task_sid;
1417 sid = isec->sid;
1418 isec->initialized = LABEL_PENDING;
1419 spin_unlock(&isec->lock);
1420
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 switch (sbsec->behavior) {
David Quigleyeb9ae682013-05-22 12:50:37 -04001422 case SECURITY_FS_USE_NATIVE:
1423 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 case SECURITY_FS_USE_XATTR:
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001425 if (!(inode->i_opflags & IOP_XATTR)) {
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001426 sid = sbsec->def_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 break;
1428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 /* Need a dentry, since the xattr API requires one.
1430 Life would be simpler if we could just pass the inode. */
1431 if (opt_dentry) {
1432 /* Called from d_instantiate or d_splice_alias. */
1433 dentry = dget(opt_dentry);
1434 } else {
Al Virob1271252018-04-25 10:28:38 -04001435 /*
1436 * Called from selinux_complete_init, try to find a dentry.
1437 * Some filesystems really want a connected one, so try
1438 * that first. We could split SECURITY_FS_USE_XATTR in
1439 * two, depending upon that...
1440 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 dentry = d_find_alias(inode);
Al Virob1271252018-04-25 10:28:38 -04001442 if (!dentry)
1443 dentry = d_find_any_alias(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 }
1445 if (!dentry) {
Eric Parisdf7f54c2009-03-09 14:35:58 -04001446 /*
1447 * this is can be hit on boot when a file is accessed
1448 * before the policy is loaded. When we load policy we
1449 * may find inodes that have no dentry on the
1450 * sbsec->isec_head list. No reason to complain as these
1451 * will get fixed up the next time we go through
1452 * inode_doinit with a dentry, before these inodes could
1453 * be used again by userspace.
1454 */
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001455 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 }
1457
1458 len = INITCONTEXTLEN;
Eric Paris4cb912f2009-02-12 14:50:05 -05001459 context = kmalloc(len+1, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 if (!context) {
1461 rc = -ENOMEM;
1462 dput(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001463 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 }
Eric Paris4cb912f2009-02-12 14:50:05 -05001465 context[len] = '\0';
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001466 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 if (rc == -ERANGE) {
James Morris314dabb2009-08-10 22:00:13 +10001468 kfree(context);
1469
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 /* Need a larger buffer. Query for the right size. */
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001471 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 if (rc < 0) {
1473 dput(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001474 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 len = rc;
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 }
1486 dput(dentry);
1487 if (rc < 0) {
1488 if (rc != -ENODATA) {
peter enderborgc103a912018-06-12 10:09:03 +02001489 pr_warn("SELinux: %s: getxattr returned "
Harvey Harrisondd6f9532008-03-06 10:03:59 +11001490 "%d for dev=%s ino=%ld\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 -rc, inode->i_sb->s_id, inode->i_ino);
1492 kfree(context);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001493 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 }
1495 /* Map ENODATA to the default file SID */
1496 sid = sbsec->def_sid;
1497 rc = 0;
1498 } else {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001499 rc = security_context_to_sid_default(&selinux_state,
1500 context, rc, &sid,
Stephen Smalley869ab512008-04-04 08:46:05 -04001501 sbsec->def_sid,
1502 GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 if (rc) {
Eric Paris4ba0a8a2009-02-12 15:01:10 -05001504 char *dev = inode->i_sb->s_id;
1505 unsigned long ino = inode->i_ino;
1506
1507 if (rc == -EINVAL) {
1508 if (printk_ratelimit())
peter enderborgc103a912018-06-12 10:09:03 +02001509 pr_notice("SELinux: inode=%lu on dev=%s was found to have an invalid "
Eric Paris4ba0a8a2009-02-12 15:01:10 -05001510 "context=%s. This indicates you may need to relabel the inode or the "
1511 "filesystem in question.\n", ino, dev, context);
1512 } else {
peter enderborgc103a912018-06-12 10:09:03 +02001513 pr_warn("SELinux: %s: context_to_sid(%s) "
Eric Paris4ba0a8a2009-02-12 15:01:10 -05001514 "returned %d for dev=%s ino=%ld\n",
1515 __func__, context, -rc, dev, ino);
1516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 kfree(context);
1518 /* Leave with the unlabeled SID */
1519 rc = 0;
1520 break;
1521 }
1522 }
1523 kfree(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 break;
1525 case SECURITY_FS_USE_TASK:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001526 sid = task_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 break;
1528 case SECURITY_FS_USE_TRANS:
1529 /* Default to the fs SID. */
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001530 sid = sbsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
1532 /* Try to obtain a transition SID. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001533 rc = security_transition_sid(&selinux_state, task_sid, sid,
1534 sclass, NULL, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 if (rc)
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001536 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 break;
Eric Parisc312feb2006-07-10 04:43:53 -07001538 case SECURITY_FS_USE_MNTPOINT:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001539 sid = sbsec->mntpoint_sid;
Eric Parisc312feb2006-07-10 04:43:53 -07001540 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 default:
Eric Parisc312feb2006-07-10 04:43:53 -07001542 /* Default to the fs superblock SID. */
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001543 sid = sbsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
Stephen Smalley134509d2015-06-04 16:22:17 -04001545 if ((sbsec->flags & SE_SBGENFS) && !S_ISLNK(inode->i_mode)) {
Paul Mooref64410e2014-03-19 16:46:18 -04001546 /* We must have a dentry to determine the label on
1547 * procfs inodes */
Al Virob1271252018-04-25 10:28:38 -04001548 if (opt_dentry) {
Paul Mooref64410e2014-03-19 16:46:18 -04001549 /* Called from d_instantiate or
1550 * d_splice_alias. */
1551 dentry = dget(opt_dentry);
Al Virob1271252018-04-25 10:28:38 -04001552 } else {
Paul Mooref64410e2014-03-19 16:46:18 -04001553 /* Called from selinux_complete_init, try to
Al Virob1271252018-04-25 10:28:38 -04001554 * find a dentry. Some filesystems really want
1555 * a connected one, so try that first.
1556 */
Paul Mooref64410e2014-03-19 16:46:18 -04001557 dentry = d_find_alias(inode);
Al Virob1271252018-04-25 10:28:38 -04001558 if (!dentry)
1559 dentry = d_find_any_alias(inode);
1560 }
Paul Mooref64410e2014-03-19 16:46:18 -04001561 /*
1562 * This can be hit on boot when a file is accessed
1563 * before the policy is loaded. When we load policy we
1564 * may find inodes that have no dentry on the
1565 * sbsec->isec_head list. No reason to complain as
1566 * these will get fixed up the next time we go through
1567 * inode_doinit() with a dentry, before these inodes
1568 * could be used again by userspace.
1569 */
1570 if (!dentry)
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001571 goto out;
1572 rc = selinux_genfs_get_sid(dentry, sclass,
Stephen Smalley134509d2015-06-04 16:22:17 -04001573 sbsec->flags, &sid);
Paul Mooref64410e2014-03-19 16:46:18 -04001574 dput(dentry);
1575 if (rc)
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001576 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 }
1578 break;
1579 }
1580
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001581out:
1582 spin_lock(&isec->lock);
1583 if (isec->initialized == LABEL_PENDING) {
1584 if (!sid || rc) {
1585 isec->initialized = LABEL_INVALID;
1586 goto out_unlock;
1587 }
1588
1589 isec->initialized = LABEL_INITIALIZED;
1590 isec->sid = sid;
1591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
Eric Paris23970742006-09-25 23:32:01 -07001593out_unlock:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001594 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 return rc;
1596}
1597
1598/* Convert a Linux signal to an access vector. */
1599static inline u32 signal_to_av(int sig)
1600{
1601 u32 perm = 0;
1602
1603 switch (sig) {
1604 case SIGCHLD:
1605 /* Commonly granted from child to parent. */
1606 perm = PROCESS__SIGCHLD;
1607 break;
1608 case SIGKILL:
1609 /* Cannot be caught or ignored */
1610 perm = PROCESS__SIGKILL;
1611 break;
1612 case SIGSTOP:
1613 /* Cannot be caught or ignored */
1614 perm = PROCESS__SIGSTOP;
1615 break;
1616 default:
1617 /* All other signals. */
1618 perm = PROCESS__SIGNAL;
1619 break;
1620 }
1621
1622 return perm;
1623}
1624
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001625#if CAP_LAST_CAP > 63
1626#error Fix SELinux to handle capabilities > 63.
1627#endif
1628
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629/* Check whether a task is allowed to use a capability. */
Eric Paris6a9de492012-01-03 12:25:14 -05001630static int cred_has_capability(const struct cred *cred,
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04001631 int cap, int audit, bool initns)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632{
Thomas Liu2bf49692009-07-14 12:14:09 -04001633 struct common_audit_data ad;
Eric Paris06112162008-11-11 22:02:50 +11001634 struct av_decision avd;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001635 u16 sclass;
David Howells3699c532009-01-06 22:27:01 +00001636 u32 sid = cred_sid(cred);
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001637 u32 av = CAP_TO_MASK(cap);
Eric Paris06112162008-11-11 22:02:50 +11001638 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
Eric Paris50c205f2012-04-04 15:01:43 -04001640 ad.type = LSM_AUDIT_DATA_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 ad.u.cap = cap;
1642
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001643 switch (CAP_TO_INDEX(cap)) {
1644 case 0:
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04001645 sclass = initns ? SECCLASS_CAPABILITY : SECCLASS_CAP_USERNS;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001646 break;
1647 case 1:
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04001648 sclass = initns ? SECCLASS_CAPABILITY2 : SECCLASS_CAP2_USERNS;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001649 break;
1650 default:
peter enderborgc103a912018-06-12 10:09:03 +02001651 pr_err("SELinux: out of range capability %d\n", cap);
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001652 BUG();
Eric Parisa35c6c832011-04-20 10:21:28 -04001653 return -EINVAL;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001654 }
Eric Paris06112162008-11-11 22:02:50 +11001655
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001656 rc = avc_has_perm_noaudit(&selinux_state,
1657 sid, sid, sclass, av, 0, &avd);
Eric Paris9ade0cf2011-04-25 16:26:29 -04001658 if (audit == SECURITY_CAP_AUDIT) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001659 int rc2 = avc_audit(&selinux_state,
1660 sid, sid, sclass, av, &avd, rc, &ad, 0);
Eric Paris9ade0cf2011-04-25 16:26:29 -04001661 if (rc2)
1662 return rc2;
1663 }
Eric Paris06112162008-11-11 22:02:50 +11001664 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665}
1666
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667/* Check whether a task has a particular permission to an inode.
1668 The 'adp' parameter is optional and allows other audit
1669 data to be passed (e.g. the dentry). */
David Howells88e67f32008-11-14 10:39:21 +11001670static int inode_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 struct inode *inode,
1672 u32 perms,
Linus Torvalds19e49832013-10-04 12:54:11 -07001673 struct common_audit_data *adp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 struct inode_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +11001676 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
David Howellse0e81732009-09-02 09:13:40 +01001678 validate_creds(cred);
1679
Eric Paris828dfe12008-04-17 13:17:49 -04001680 if (unlikely(IS_PRIVATE(inode)))
Stephen Smalleybbaca6c2007-02-14 00:34:16 -08001681 return 0;
1682
David Howells88e67f32008-11-14 10:39:21 +11001683 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 isec = inode->i_security;
1685
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001686 return avc_has_perm(&selinux_state,
1687 sid, isec->sid, isec->sclass, perms, adp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688}
1689
1690/* Same as inode_has_perm, but pass explicit audit data containing
1691 the dentry to help the auditing code to more easily generate the
1692 pathname if needed. */
David Howells88e67f32008-11-14 10:39:21 +11001693static inline int dentry_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 struct dentry *dentry,
1695 u32 av)
1696{
David Howellsc6f493d2015-03-17 22:26:22 +00001697 struct inode *inode = d_backing_inode(dentry);
Thomas Liu2bf49692009-07-14 12:14:09 -04001698 struct common_audit_data ad;
David Howells88e67f32008-11-14 10:39:21 +11001699
Eric Paris50c205f2012-04-04 15:01:43 -04001700 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Paris2875fa02011-04-28 16:04:24 -04001701 ad.u.dentry = dentry;
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05001702 __inode_security_revalidate(inode, dentry, true);
Linus Torvalds19e49832013-10-04 12:54:11 -07001703 return inode_has_perm(cred, inode, av, &ad);
Eric Paris2875fa02011-04-28 16:04:24 -04001704}
1705
1706/* Same as inode_has_perm, but pass explicit audit data containing
1707 the path to help the auditing code to more easily generate the
1708 pathname if needed. */
1709static inline int path_has_perm(const struct cred *cred,
Al Viro3f7036a2015-03-08 19:28:30 -04001710 const struct path *path,
Eric Paris2875fa02011-04-28 16:04:24 -04001711 u32 av)
1712{
David Howellsc6f493d2015-03-17 22:26:22 +00001713 struct inode *inode = d_backing_inode(path->dentry);
Eric Paris2875fa02011-04-28 16:04:24 -04001714 struct common_audit_data ad;
1715
Eric Paris50c205f2012-04-04 15:01:43 -04001716 ad.type = LSM_AUDIT_DATA_PATH;
Eric Paris2875fa02011-04-28 16:04:24 -04001717 ad.u.path = *path;
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05001718 __inode_security_revalidate(inode, path->dentry, true);
Linus Torvalds19e49832013-10-04 12:54:11 -07001719 return inode_has_perm(cred, inode, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720}
1721
David Howells13f8e982013-06-13 23:37:55 +01001722/* Same as path_has_perm, but uses the inode from the file struct. */
1723static inline int file_path_has_perm(const struct cred *cred,
1724 struct file *file,
1725 u32 av)
1726{
1727 struct common_audit_data ad;
1728
Vivek Goyal43af5de2016-09-09 11:37:49 -04001729 ad.type = LSM_AUDIT_DATA_FILE;
1730 ad.u.file = file;
Linus Torvalds19e49832013-10-04 12:54:11 -07001731 return inode_has_perm(cred, file_inode(file), av, &ad);
David Howells13f8e982013-06-13 23:37:55 +01001732}
1733
Chenbo Fengf66e4482017-10-18 13:00:26 -07001734#ifdef CONFIG_BPF_SYSCALL
1735static int bpf_fd_pass(struct file *file, u32 sid);
1736#endif
1737
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738/* Check whether a task can use an open file descriptor to
1739 access an inode in a given way. Check access to the
1740 descriptor itself, and then use dentry_has_perm to
1741 check a particular permission to the file.
1742 Access to the descriptor is implicitly granted if it
1743 has the same SID as the process. If av is zero, then
1744 access to the file is not checked, e.g. for cases
1745 where only the descriptor is affected like seek. */
David Howells88e67f32008-11-14 10:39:21 +11001746static int file_has_perm(const struct cred *cred,
1747 struct file *file,
1748 u32 av)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 struct file_security_struct *fsec = file->f_security;
Al Viro496ad9a2013-01-23 17:07:38 -05001751 struct inode *inode = file_inode(file);
Thomas Liu2bf49692009-07-14 12:14:09 -04001752 struct common_audit_data ad;
David Howells88e67f32008-11-14 10:39:21 +11001753 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 int rc;
1755
Vivek Goyal43af5de2016-09-09 11:37:49 -04001756 ad.type = LSM_AUDIT_DATA_FILE;
1757 ad.u.file = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
David Howells275bb412008-11-14 10:39:19 +11001759 if (sid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001760 rc = avc_has_perm(&selinux_state,
1761 sid, fsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 SECCLASS_FD,
1763 FD__USE,
1764 &ad);
1765 if (rc)
David Howells88e67f32008-11-14 10:39:21 +11001766 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 }
1768
Chenbo Fengf66e4482017-10-18 13:00:26 -07001769#ifdef CONFIG_BPF_SYSCALL
1770 rc = bpf_fd_pass(file, cred_sid(cred));
1771 if (rc)
1772 return rc;
1773#endif
1774
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 /* av is zero if only checking access to the descriptor. */
David Howells88e67f32008-11-14 10:39:21 +11001776 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 if (av)
Linus Torvalds19e49832013-10-04 12:54:11 -07001778 rc = inode_has_perm(cred, inode, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
David Howells88e67f32008-11-14 10:39:21 +11001780out:
1781 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782}
1783
David Howellsc3c188b2015-07-10 17:19:58 -04001784/*
1785 * Determine the label for an inode that might be unioned.
1786 */
Vivek Goyalc957f6d2016-07-13 10:44:51 -04001787static int
1788selinux_determine_inode_label(const struct task_security_struct *tsec,
1789 struct inode *dir,
1790 const struct qstr *name, u16 tclass,
1791 u32 *_new_isid)
David Howellsc3c188b2015-07-10 17:19:58 -04001792{
1793 const struct superblock_security_struct *sbsec = dir->i_sb->s_security;
David Howellsc3c188b2015-07-10 17:19:58 -04001794
1795 if ((sbsec->flags & SE_SBINITIALIZED) &&
1796 (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)) {
1797 *_new_isid = sbsec->mntpoint_sid;
1798 } else if ((sbsec->flags & SBLABEL_MNT) &&
1799 tsec->create_sid) {
1800 *_new_isid = tsec->create_sid;
1801 } else {
Paul Moore20cdef82016-04-04 14:14:42 -04001802 const struct inode_security_struct *dsec = inode_security(dir);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001803 return security_transition_sid(&selinux_state, tsec->sid,
1804 dsec->sid, tclass,
David Howellsc3c188b2015-07-10 17:19:58 -04001805 name, _new_isid);
1806 }
1807
1808 return 0;
1809}
1810
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811/* Check whether a task can create a file. */
1812static int may_create(struct inode *dir,
1813 struct dentry *dentry,
1814 u16 tclass)
1815{
Paul Moore5fb49872010-04-22 14:46:19 -04001816 const struct task_security_struct *tsec = current_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 struct inode_security_struct *dsec;
1818 struct superblock_security_struct *sbsec;
David Howells275bb412008-11-14 10:39:19 +11001819 u32 sid, newsid;
Thomas Liu2bf49692009-07-14 12:14:09 -04001820 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 int rc;
1822
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001823 dsec = inode_security(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 sbsec = dir->i_sb->s_security;
1825
David Howells275bb412008-11-14 10:39:19 +11001826 sid = tsec->sid;
David Howells275bb412008-11-14 10:39:19 +11001827
Eric Paris50c205f2012-04-04 15:01:43 -04001828 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04001829 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001831 rc = avc_has_perm(&selinux_state,
1832 sid, dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 DIR__ADD_NAME | DIR__SEARCH,
1834 &ad);
1835 if (rc)
1836 return rc;
1837
Vivek Goyalc957f6d2016-07-13 10:44:51 -04001838 rc = selinux_determine_inode_label(current_security(), dir,
1839 &dentry->d_name, tclass, &newsid);
David Howellsc3c188b2015-07-10 17:19:58 -04001840 if (rc)
1841 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001843 rc = avc_has_perm(&selinux_state,
1844 sid, newsid, tclass, FILE__CREATE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 if (rc)
1846 return rc;
1847
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001848 return avc_has_perm(&selinux_state,
1849 newsid, sbsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 SECCLASS_FILESYSTEM,
1851 FILESYSTEM__ASSOCIATE, &ad);
1852}
1853
Eric Paris828dfe12008-04-17 13:17:49 -04001854#define MAY_LINK 0
1855#define MAY_UNLINK 1
1856#define MAY_RMDIR 2
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
1858/* Check whether a task can link, unlink, or rmdir a file/directory. */
1859static int may_link(struct inode *dir,
1860 struct dentry *dentry,
1861 int kind)
1862
1863{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 struct inode_security_struct *dsec, *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001865 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11001866 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 u32 av;
1868 int rc;
1869
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001870 dsec = inode_security(dir);
1871 isec = backing_inode_security(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
Eric Paris50c205f2012-04-04 15:01:43 -04001873 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04001874 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
1876 av = DIR__SEARCH;
1877 av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001878 rc = avc_has_perm(&selinux_state,
1879 sid, dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 if (rc)
1881 return rc;
1882
1883 switch (kind) {
1884 case MAY_LINK:
1885 av = FILE__LINK;
1886 break;
1887 case MAY_UNLINK:
1888 av = FILE__UNLINK;
1889 break;
1890 case MAY_RMDIR:
1891 av = DIR__RMDIR;
1892 break;
1893 default:
peter enderborgc103a912018-06-12 10:09:03 +02001894 pr_warn("SELinux: %s: unrecognized kind %d\n",
Eric Paris744ba352008-04-17 11:52:44 -04001895 __func__, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 return 0;
1897 }
1898
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001899 rc = avc_has_perm(&selinux_state,
1900 sid, isec->sid, isec->sclass, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 return rc;
1902}
1903
1904static inline int may_rename(struct inode *old_dir,
1905 struct dentry *old_dentry,
1906 struct inode *new_dir,
1907 struct dentry *new_dentry)
1908{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001910 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11001911 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 u32 av;
1913 int old_is_dir, new_is_dir;
1914 int rc;
1915
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001916 old_dsec = inode_security(old_dir);
1917 old_isec = backing_inode_security(old_dentry);
David Howellse36cb0b2015-01-29 12:02:35 +00001918 old_is_dir = d_is_dir(old_dentry);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001919 new_dsec = inode_security(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
Eric Paris50c205f2012-04-04 15:01:43 -04001921 ad.type = LSM_AUDIT_DATA_DENTRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Eric Parisa2694342011-04-25 13:10:27 -04001923 ad.u.dentry = old_dentry;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001924 rc = avc_has_perm(&selinux_state,
1925 sid, old_dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 DIR__REMOVE_NAME | DIR__SEARCH, &ad);
1927 if (rc)
1928 return rc;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001929 rc = avc_has_perm(&selinux_state,
1930 sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 old_isec->sclass, FILE__RENAME, &ad);
1932 if (rc)
1933 return rc;
1934 if (old_is_dir && new_dir != old_dir) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001935 rc = avc_has_perm(&selinux_state,
1936 sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 old_isec->sclass, DIR__REPARENT, &ad);
1938 if (rc)
1939 return rc;
1940 }
1941
Eric Parisa2694342011-04-25 13:10:27 -04001942 ad.u.dentry = new_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 av = DIR__ADD_NAME | DIR__SEARCH;
David Howells2c616d42015-01-29 12:02:33 +00001944 if (d_is_positive(new_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 av |= DIR__REMOVE_NAME;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001946 rc = avc_has_perm(&selinux_state,
1947 sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 if (rc)
1949 return rc;
David Howells2c616d42015-01-29 12:02:33 +00001950 if (d_is_positive(new_dentry)) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001951 new_isec = backing_inode_security(new_dentry);
David Howellse36cb0b2015-01-29 12:02:35 +00001952 new_is_dir = d_is_dir(new_dentry);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001953 rc = avc_has_perm(&selinux_state,
1954 sid, new_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 new_isec->sclass,
1956 (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
1957 if (rc)
1958 return rc;
1959 }
1960
1961 return 0;
1962}
1963
1964/* Check whether a task can perform a filesystem operation. */
David Howells88e67f32008-11-14 10:39:21 +11001965static int superblock_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 struct super_block *sb,
1967 u32 perms,
Thomas Liu2bf49692009-07-14 12:14:09 -04001968 struct common_audit_data *ad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 struct superblock_security_struct *sbsec;
David Howells88e67f32008-11-14 10:39:21 +11001971 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 sbsec = sb->s_security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001974 return avc_has_perm(&selinux_state,
1975 sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976}
1977
1978/* Convert a Linux mode and permission mask to an access vector. */
1979static inline u32 file_mask_to_av(int mode, int mask)
1980{
1981 u32 av = 0;
1982
Al Virodba19c62011-07-25 20:49:29 -04001983 if (!S_ISDIR(mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 if (mask & MAY_EXEC)
1985 av |= FILE__EXECUTE;
1986 if (mask & MAY_READ)
1987 av |= FILE__READ;
1988
1989 if (mask & MAY_APPEND)
1990 av |= FILE__APPEND;
1991 else if (mask & MAY_WRITE)
1992 av |= FILE__WRITE;
1993
1994 } else {
1995 if (mask & MAY_EXEC)
1996 av |= DIR__SEARCH;
1997 if (mask & MAY_WRITE)
1998 av |= DIR__WRITE;
1999 if (mask & MAY_READ)
2000 av |= DIR__READ;
2001 }
2002
2003 return av;
2004}
2005
2006/* Convert a Linux file to an access vector. */
2007static inline u32 file_to_av(struct file *file)
2008{
2009 u32 av = 0;
2010
2011 if (file->f_mode & FMODE_READ)
2012 av |= FILE__READ;
2013 if (file->f_mode & FMODE_WRITE) {
2014 if (file->f_flags & O_APPEND)
2015 av |= FILE__APPEND;
2016 else
2017 av |= FILE__WRITE;
2018 }
Stephen Smalley0794c662008-03-17 08:55:18 -04002019 if (!av) {
2020 /*
2021 * Special file opened with flags 3 for ioctl-only use.
2022 */
2023 av = FILE__IOCTL;
2024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
2026 return av;
2027}
2028
Eric Paris8b6a5a32008-10-29 17:06:46 -04002029/*
2030 * Convert a file to an access vector and include the correct open
2031 * open permission.
2032 */
2033static inline u32 open_file_to_av(struct file *file)
2034{
2035 u32 av = file_to_av(file);
Stephen Smalleyccb54472017-05-12 12:41:24 -04002036 struct inode *inode = file_inode(file);
Eric Paris8b6a5a32008-10-29 17:06:46 -04002037
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002038 if (selinux_policycap_openperm() &&
2039 inode->i_sb->s_magic != SOCKFS_MAGIC)
Eric Paris49b7b8d2010-07-23 11:44:09 -04002040 av |= FILE__OPEN;
2041
Eric Paris8b6a5a32008-10-29 17:06:46 -04002042 return av;
2043}
2044
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045/* Hook functions begin here. */
2046
Stephen Smalley79af7302015-01-21 10:54:10 -05002047static int selinux_binder_set_context_mgr(struct task_struct *mgr)
2048{
2049 u32 mysid = current_sid();
2050 u32 mgrsid = task_sid(mgr);
2051
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002052 return avc_has_perm(&selinux_state,
2053 mysid, mgrsid, SECCLASS_BINDER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002054 BINDER__SET_CONTEXT_MGR, NULL);
2055}
2056
2057static int selinux_binder_transaction(struct task_struct *from,
2058 struct task_struct *to)
2059{
2060 u32 mysid = current_sid();
2061 u32 fromsid = task_sid(from);
2062 u32 tosid = task_sid(to);
2063 int rc;
2064
2065 if (mysid != fromsid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002066 rc = avc_has_perm(&selinux_state,
2067 mysid, fromsid, SECCLASS_BINDER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002068 BINDER__IMPERSONATE, NULL);
2069 if (rc)
2070 return rc;
2071 }
2072
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002073 return avc_has_perm(&selinux_state,
2074 fromsid, tosid, SECCLASS_BINDER, BINDER__CALL,
Stephen Smalley79af7302015-01-21 10:54:10 -05002075 NULL);
2076}
2077
2078static int selinux_binder_transfer_binder(struct task_struct *from,
2079 struct task_struct *to)
2080{
2081 u32 fromsid = task_sid(from);
2082 u32 tosid = task_sid(to);
2083
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002084 return avc_has_perm(&selinux_state,
2085 fromsid, tosid, SECCLASS_BINDER, BINDER__TRANSFER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002086 NULL);
2087}
2088
2089static int selinux_binder_transfer_file(struct task_struct *from,
2090 struct task_struct *to,
2091 struct file *file)
2092{
2093 u32 sid = task_sid(to);
2094 struct file_security_struct *fsec = file->f_security;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002095 struct dentry *dentry = file->f_path.dentry;
Paul Moore20cdef82016-04-04 14:14:42 -04002096 struct inode_security_struct *isec;
Stephen Smalley79af7302015-01-21 10:54:10 -05002097 struct common_audit_data ad;
2098 int rc;
2099
2100 ad.type = LSM_AUDIT_DATA_PATH;
2101 ad.u.path = file->f_path;
2102
2103 if (sid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002104 rc = avc_has_perm(&selinux_state,
2105 sid, fsec->sid,
Stephen Smalley79af7302015-01-21 10:54:10 -05002106 SECCLASS_FD,
2107 FD__USE,
2108 &ad);
2109 if (rc)
2110 return rc;
2111 }
2112
Chenbo Fengf66e4482017-10-18 13:00:26 -07002113#ifdef CONFIG_BPF_SYSCALL
2114 rc = bpf_fd_pass(file, sid);
2115 if (rc)
2116 return rc;
2117#endif
2118
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002119 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
Stephen Smalley79af7302015-01-21 10:54:10 -05002120 return 0;
2121
Paul Moore20cdef82016-04-04 14:14:42 -04002122 isec = backing_inode_security(dentry);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002123 return avc_has_perm(&selinux_state,
2124 sid, isec->sid, isec->sclass, file_to_av(file),
Stephen Smalley79af7302015-01-21 10:54:10 -05002125 &ad);
2126}
2127
Ingo Molnar9e488582009-05-07 19:26:19 +10002128static int selinux_ptrace_access_check(struct task_struct *child,
David Howells5cd9c582008-08-14 11:37:28 +01002129 unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002131 u32 sid = current_sid();
2132 u32 csid = task_sid(child);
Stephen Smalley006ebb42008-05-19 08:32:49 -04002133
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002134 if (mode & PTRACE_MODE_READ)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002135 return avc_has_perm(&selinux_state,
2136 sid, csid, SECCLASS_FILE, FILE__READ, NULL);
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002137
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002138 return avc_has_perm(&selinux_state,
2139 sid, csid, SECCLASS_PROCESS, PROCESS__PTRACE, NULL);
David Howells5cd9c582008-08-14 11:37:28 +01002140}
2141
2142static int selinux_ptrace_traceme(struct task_struct *parent)
2143{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002144 return avc_has_perm(&selinux_state,
2145 task_sid(parent), current_sid(), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002146 PROCESS__PTRACE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147}
2148
2149static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
Eric Paris828dfe12008-04-17 13:17:49 -04002150 kernel_cap_t *inheritable, kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002152 return avc_has_perm(&selinux_state,
2153 current_sid(), task_sid(target), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002154 PROCESS__GETCAP, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155}
2156
David Howellsd84f4f92008-11-14 10:39:23 +11002157static int selinux_capset(struct cred *new, const struct cred *old,
2158 const kernel_cap_t *effective,
2159 const kernel_cap_t *inheritable,
2160 const kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002162 return avc_has_perm(&selinux_state,
2163 cred_sid(old), cred_sid(new), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002164 PROCESS__SETCAP, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165}
2166
James Morris5626d3e2009-01-30 10:05:06 +11002167/*
2168 * (This comment used to live with the selinux_task_setuid hook,
2169 * which was removed).
2170 *
2171 * Since setuid only affects the current process, and since the SELinux
2172 * controls are not based on the Linux identity attributes, SELinux does not
2173 * need to control this operation. However, SELinux does control the use of
2174 * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
2175 */
2176
Eric Paris6a9de492012-01-03 12:25:14 -05002177static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
2178 int cap, int audit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179{
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04002180 return cred_has_capability(cred, cap, audit, ns == &init_user_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181}
2182
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
2184{
David Howells88e67f32008-11-14 10:39:21 +11002185 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 int rc = 0;
2187
2188 if (!sb)
2189 return 0;
2190
2191 switch (cmds) {
Eric Paris828dfe12008-04-17 13:17:49 -04002192 case Q_SYNC:
2193 case Q_QUOTAON:
2194 case Q_QUOTAOFF:
2195 case Q_SETINFO:
2196 case Q_SETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11002197 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04002198 break;
2199 case Q_GETFMT:
2200 case Q_GETINFO:
2201 case Q_GETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11002202 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04002203 break;
2204 default:
2205 rc = 0; /* let the kernel handle invalid cmds */
2206 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 }
2208 return rc;
2209}
2210
2211static int selinux_quota_on(struct dentry *dentry)
2212{
David Howells88e67f32008-11-14 10:39:21 +11002213 const struct cred *cred = current_cred();
2214
Eric Paris2875fa02011-04-28 16:04:24 -04002215 return dentry_has_perm(cred, dentry, FILE__QUOTAON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216}
2217
Eric Paris12b30522010-11-15 18:36:29 -05002218static int selinux_syslog(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 switch (type) {
Kees Cookd78ca3c2010-02-03 15:37:13 -08002221 case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */
2222 case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002223 return avc_has_perm(&selinux_state,
2224 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002225 SECCLASS_SYSTEM, SYSTEM__SYSLOG_READ, NULL);
Kees Cookd78ca3c2010-02-03 15:37:13 -08002226 case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */
2227 case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */
2228 /* Set level of messages printed to console */
2229 case SYSLOG_ACTION_CONSOLE_LEVEL:
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002230 return avc_has_perm(&selinux_state,
2231 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002232 SECCLASS_SYSTEM, SYSTEM__SYSLOG_CONSOLE,
2233 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 }
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002235 /* All other syslog types */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002236 return avc_has_perm(&selinux_state,
2237 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002238 SECCLASS_SYSTEM, SYSTEM__SYSLOG_MOD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239}
2240
2241/*
2242 * Check that a process has enough memory to allocate a new virtual
2243 * mapping. 0 means there is enough memory for the allocation to
2244 * succeed and -ENOMEM implies there is not.
2245 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 * Do not audit the selinux permission check, as this is applied to all
2247 * processes that allocate mappings.
2248 */
Alan Cox34b4e4a2007-08-22 14:01:28 -07002249static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250{
2251 int rc, cap_sys_admin = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002253 rc = cred_has_capability(current_cred(), CAP_SYS_ADMIN,
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04002254 SECURITY_CAP_NOAUDIT, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 if (rc == 0)
2256 cap_sys_admin = 1;
2257
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002258 return cap_sys_admin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259}
2260
2261/* binprm security operations */
2262
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002263static u32 ptrace_parent_sid(void)
Paul Moore0c6181c2016-03-30 21:41:21 -04002264{
2265 u32 sid = 0;
2266 struct task_struct *tracer;
2267
2268 rcu_read_lock();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002269 tracer = ptrace_parent(current);
Paul Moore0c6181c2016-03-30 21:41:21 -04002270 if (tracer)
2271 sid = task_sid(tracer);
2272 rcu_read_unlock();
2273
2274 return sid;
2275}
2276
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002277static int check_nnp_nosuid(const struct linux_binprm *bprm,
2278 const struct task_security_struct *old_tsec,
2279 const struct task_security_struct *new_tsec)
2280{
2281 int nnp = (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS);
Andy Lutomirski380cf5b2016-06-23 16:41:05 -05002282 int nosuid = !mnt_may_suid(bprm->file->f_path.mnt);
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002283 int rc;
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002284 u32 av;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002285
2286 if (!nnp && !nosuid)
2287 return 0; /* neither NNP nor nosuid */
2288
2289 if (new_tsec->sid == old_tsec->sid)
2290 return 0; /* No change in credentials */
2291
2292 /*
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002293 * If the policy enables the nnp_nosuid_transition policy capability,
2294 * then we permit transitions under NNP or nosuid if the
2295 * policy allows the corresponding permission between
2296 * the old and new contexts.
2297 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002298 if (selinux_policycap_nnp_nosuid_transition()) {
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002299 av = 0;
2300 if (nnp)
2301 av |= PROCESS2__NNP_TRANSITION;
2302 if (nosuid)
2303 av |= PROCESS2__NOSUID_TRANSITION;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002304 rc = avc_has_perm(&selinux_state,
2305 old_tsec->sid, new_tsec->sid,
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002306 SECCLASS_PROCESS2, av, NULL);
2307 if (!rc)
2308 return 0;
2309 }
2310
2311 /*
2312 * We also permit NNP or nosuid transitions to bounded SIDs,
2313 * i.e. SIDs that are guaranteed to only be allowed a subset
2314 * of the permissions of the current SID.
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002315 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002316 rc = security_bounded_transition(&selinux_state, old_tsec->sid,
2317 new_tsec->sid);
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002318 if (!rc)
2319 return 0;
2320
2321 /*
2322 * On failure, preserve the errno values for NNP vs nosuid.
2323 * NNP: Operation not permitted for caller.
2324 * nosuid: Permission denied to file.
2325 */
2326 if (nnp)
2327 return -EPERM;
2328 return -EACCES;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002329}
2330
David Howellsa6f76f22008-11-14 10:39:24 +11002331static int selinux_bprm_set_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332{
David Howellsa6f76f22008-11-14 10:39:24 +11002333 const struct task_security_struct *old_tsec;
2334 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 struct inode_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04002336 struct common_audit_data ad;
Al Viro496ad9a2013-01-23 17:07:38 -05002337 struct inode *inode = file_inode(bprm->file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 int rc;
2339
David Howellsa6f76f22008-11-14 10:39:24 +11002340 /* SELinux context only depends on initial program or script and not
2341 * the script interpreter */
Kees Cookddb4a142017-07-18 15:25:23 -07002342 if (bprm->called_set_creds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 return 0;
2344
David Howellsa6f76f22008-11-14 10:39:24 +11002345 old_tsec = current_security();
2346 new_tsec = bprm->cred->security;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002347 isec = inode_security(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
2349 /* Default to the current task SID. */
David Howellsa6f76f22008-11-14 10:39:24 +11002350 new_tsec->sid = old_tsec->sid;
2351 new_tsec->osid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
Michael LeMay28eba5b2006-06-27 02:53:42 -07002353 /* Reset fs, key, and sock SIDs on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002354 new_tsec->create_sid = 0;
2355 new_tsec->keycreate_sid = 0;
2356 new_tsec->sockcreate_sid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357
David Howellsa6f76f22008-11-14 10:39:24 +11002358 if (old_tsec->exec_sid) {
2359 new_tsec->sid = old_tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 /* Reset exec SID on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002361 new_tsec->exec_sid = 0;
Andy Lutomirski259e5e62012-04-12 16:47:50 -05002362
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002363 /* Fail on NNP or nosuid if not an allowed transition. */
2364 rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
2365 if (rc)
2366 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 } else {
2368 /* Check for a default transition on this program. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002369 rc = security_transition_sid(&selinux_state, old_tsec->sid,
2370 isec->sid, SECCLASS_PROCESS, NULL,
Eric Paris652bb9b2011-02-01 11:05:40 -05002371 &new_tsec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 if (rc)
2373 return rc;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002374
2375 /*
2376 * Fallback to old SID on NNP or nosuid if not an allowed
2377 * transition.
2378 */
2379 rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
2380 if (rc)
2381 new_tsec->sid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 }
2383
Vivek Goyal43af5de2016-09-09 11:37:49 -04002384 ad.type = LSM_AUDIT_DATA_FILE;
2385 ad.u.file = bprm->file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
David Howellsa6f76f22008-11-14 10:39:24 +11002387 if (new_tsec->sid == old_tsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002388 rc = avc_has_perm(&selinux_state,
2389 old_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
2391 if (rc)
2392 return rc;
2393 } else {
2394 /* Check permissions for the transition. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002395 rc = avc_has_perm(&selinux_state,
2396 old_tsec->sid, new_tsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
2398 if (rc)
2399 return rc;
2400
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002401 rc = avc_has_perm(&selinux_state,
2402 new_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
2404 if (rc)
2405 return rc;
2406
David Howellsa6f76f22008-11-14 10:39:24 +11002407 /* Check for shared state */
2408 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002409 rc = avc_has_perm(&selinux_state,
2410 old_tsec->sid, new_tsec->sid,
David Howellsa6f76f22008-11-14 10:39:24 +11002411 SECCLASS_PROCESS, PROCESS__SHARE,
2412 NULL);
2413 if (rc)
2414 return -EPERM;
2415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416
David Howellsa6f76f22008-11-14 10:39:24 +11002417 /* Make sure that anyone attempting to ptrace over a task that
2418 * changes its SID has the appropriate permit */
Eric W. Biederman9227dd22017-01-23 17:26:31 +13002419 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002420 u32 ptsid = ptrace_parent_sid();
David Howellsa6f76f22008-11-14 10:39:24 +11002421 if (ptsid != 0) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002422 rc = avc_has_perm(&selinux_state,
2423 ptsid, new_tsec->sid,
David Howellsa6f76f22008-11-14 10:39:24 +11002424 SECCLASS_PROCESS,
2425 PROCESS__PTRACE, NULL);
2426 if (rc)
2427 return -EPERM;
2428 }
2429 }
2430
2431 /* Clear any possibly unsafe personality bits on exec: */
2432 bprm->per_clear |= PER_CLEAR_ON_SETID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 /* Enable secure mode for SIDs transitions unless
2435 the noatsecure permission is granted between
2436 the two SIDs, i.e. ahp returns 0. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002437 rc = avc_has_perm(&selinux_state,
2438 old_tsec->sid, new_tsec->sid,
Kees Cook62874c32017-07-18 15:25:25 -07002439 SECCLASS_PROCESS, PROCESS__NOATSECURE,
2440 NULL);
2441 bprm->secureexec |= !!rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 }
2443
Kees Cook62874c32017-07-18 15:25:25 -07002444 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445}
2446
Al Viroc3c073f2012-08-21 22:32:06 -04002447static int match_file(const void *p, struct file *file, unsigned fd)
2448{
2449 return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0;
2450}
2451
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452/* Derived from fs/exec.c:flush_old_files. */
David Howells745ca242008-11-14 10:39:22 +11002453static inline void flush_unauthorized_files(const struct cred *cred,
2454 struct files_struct *files)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 struct file *file, *devnull = NULL;
Stephen Smalleyb20c8122006-09-25 23:32:03 -07002457 struct tty_struct *tty;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002458 int drop_tty = 0;
Al Viroc3c073f2012-08-21 22:32:06 -04002459 unsigned n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002461 tty = get_current_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 if (tty) {
Peter Hurley4a510962016-01-09 21:35:23 -08002463 spin_lock(&tty->files_lock);
Eric Paris37dd0bd2008-10-31 17:40:00 -04002464 if (!list_empty(&tty->tty_files)) {
Nick Piggind996b622010-08-18 04:37:36 +10002465 struct tty_file_private *file_priv;
Eric Paris37dd0bd2008-10-31 17:40:00 -04002466
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 /* Revalidate access to controlling tty.
David Howells13f8e982013-06-13 23:37:55 +01002468 Use file_path_has_perm on the tty path directly
2469 rather than using file_has_perm, as this particular
2470 open file may belong to another process and we are
2471 only interested in the inode-based check here. */
Nick Piggind996b622010-08-18 04:37:36 +10002472 file_priv = list_first_entry(&tty->tty_files,
2473 struct tty_file_private, list);
2474 file = file_priv->file;
David Howells13f8e982013-06-13 23:37:55 +01002475 if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE))
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002476 drop_tty = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 }
Peter Hurley4a510962016-01-09 21:35:23 -08002478 spin_unlock(&tty->files_lock);
Alan Cox452a00d2008-10-13 10:39:13 +01002479 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 }
Eric W. Biederman98a27ba2007-05-08 00:26:56 -07002481 /* Reset controlling tty. */
2482 if (drop_tty)
2483 no_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484
2485 /* Revalidate access to inherited open files. */
Al Viroc3c073f2012-08-21 22:32:06 -04002486 n = iterate_fd(files, 0, match_file, cred);
2487 if (!n) /* none found? */
2488 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489
Al Viroc3c073f2012-08-21 22:32:06 -04002490 devnull = dentry_open(&selinux_null, O_RDWR, cred);
Al Viro45525b22012-10-16 13:30:07 -04002491 if (IS_ERR(devnull))
2492 devnull = NULL;
2493 /* replace all the matching ones with this */
2494 do {
2495 replace_fd(n - 1, devnull, 0);
2496 } while ((n = iterate_fd(files, n, match_file, cred)) != 0);
2497 if (devnull)
Al Viroc3c073f2012-08-21 22:32:06 -04002498 fput(devnull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499}
2500
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501/*
David Howellsa6f76f22008-11-14 10:39:24 +11002502 * Prepare a process for imminent new credential changes due to exec
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 */
David Howellsa6f76f22008-11-14 10:39:24 +11002504static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505{
David Howellsa6f76f22008-11-14 10:39:24 +11002506 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 struct rlimit *rlim, *initrlim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 int rc, i;
2509
David Howellsa6f76f22008-11-14 10:39:24 +11002510 new_tsec = bprm->cred->security;
2511 if (new_tsec->sid == new_tsec->osid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 return;
2513
2514 /* Close files for which the new task SID is not authorized. */
David Howellsa6f76f22008-11-14 10:39:24 +11002515 flush_unauthorized_files(bprm->cred, current->files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516
David Howellsa6f76f22008-11-14 10:39:24 +11002517 /* Always clear parent death signal on SID transitions. */
2518 current->pdeath_signal = 0;
2519
2520 /* Check whether the new SID can inherit resource limits from the old
2521 * SID. If not, reset all soft limits to the lower of the current
2522 * task's hard limit and the init task's soft limit.
2523 *
2524 * Note that the setting of hard limits (even to lower them) can be
2525 * controlled by the setrlimit check. The inclusion of the init task's
2526 * soft limit into the computation is to avoid resetting soft limits
2527 * higher than the default soft limit for cases where the default is
2528 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2529 */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002530 rc = avc_has_perm(&selinux_state,
2531 new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
David Howellsa6f76f22008-11-14 10:39:24 +11002532 PROCESS__RLIMITINH, NULL);
2533 if (rc) {
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002534 /* protect against do_prlimit() */
2535 task_lock(current);
David Howellsa6f76f22008-11-14 10:39:24 +11002536 for (i = 0; i < RLIM_NLIMITS; i++) {
2537 rlim = current->signal->rlim + i;
2538 initrlim = init_task.signal->rlim + i;
2539 rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
2540 }
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002541 task_unlock(current);
Nicolas Pitrebaa73d92016-11-11 00:10:10 -05002542 if (IS_ENABLED(CONFIG_POSIX_TIMERS))
2543 update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
David Howellsa6f76f22008-11-14 10:39:24 +11002544 }
2545}
2546
2547/*
2548 * Clean up the process immediately after the installation of new credentials
2549 * due to exec
2550 */
2551static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
2552{
2553 const struct task_security_struct *tsec = current_security();
2554 struct itimerval itimer;
David Howellsa6f76f22008-11-14 10:39:24 +11002555 u32 osid, sid;
2556 int rc, i;
David Howellsa6f76f22008-11-14 10:39:24 +11002557
David Howellsa6f76f22008-11-14 10:39:24 +11002558 osid = tsec->osid;
2559 sid = tsec->sid;
2560
2561 if (sid == osid)
2562 return;
2563
2564 /* Check whether the new SID can inherit signal state from the old SID.
2565 * If not, clear itimers to avoid subsequent signal generation and
2566 * flush and unblock signals.
2567 *
2568 * This must occur _after_ the task SID has been updated so that any
2569 * kill done after the flush will be checked against the new SID.
2570 */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002571 rc = avc_has_perm(&selinux_state,
2572 osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 if (rc) {
Nicolas Pitrebaa73d92016-11-11 00:10:10 -05002574 if (IS_ENABLED(CONFIG_POSIX_TIMERS)) {
2575 memset(&itimer, 0, sizeof itimer);
2576 for (i = 0; i < 3; i++)
2577 do_setitimer(i, &itimer, NULL);
2578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterov9e7c8f82015-06-04 16:22:16 -04002580 if (!fatal_signal_pending(current)) {
2581 flush_sigqueue(&current->pending);
2582 flush_sigqueue(&current->signal->shared_pending);
David Howells3bcac022009-04-29 13:45:05 +01002583 flush_signal_handlers(current, 1);
2584 sigemptyset(&current->blocked);
Oleg Nesterov9e7c8f82015-06-04 16:22:16 -04002585 recalc_sigpending();
David Howells3bcac022009-04-29 13:45:05 +01002586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 spin_unlock_irq(&current->sighand->siglock);
2588 }
2589
David Howellsa6f76f22008-11-14 10:39:24 +11002590 /* Wake up the parent if it is waiting so that it can recheck
2591 * wait permission to the new task SID. */
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002592 read_lock(&tasklist_lock);
Oleg Nesterov0b7570e2009-09-23 15:56:46 -07002593 __wake_up_parent(current, current->real_parent);
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002594 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595}
2596
2597/* superblock security operations */
2598
2599static int selinux_sb_alloc_security(struct super_block *sb)
2600{
2601 return superblock_alloc_security(sb);
2602}
2603
2604static void selinux_sb_free_security(struct super_block *sb)
2605{
2606 superblock_free_security(sb);
2607}
2608
2609static inline int match_prefix(char *prefix, int plen, char *option, int olen)
2610{
2611 if (plen > olen)
2612 return 0;
2613
2614 return !memcmp(prefix, option, plen);
2615}
2616
2617static inline int selinux_option(char *option, int len)
2618{
Eric Paris832cbd92008-04-01 13:24:09 -04002619 return (match_prefix(CONTEXT_STR, sizeof(CONTEXT_STR)-1, option, len) ||
2620 match_prefix(FSCONTEXT_STR, sizeof(FSCONTEXT_STR)-1, option, len) ||
2621 match_prefix(DEFCONTEXT_STR, sizeof(DEFCONTEXT_STR)-1, option, len) ||
David P. Quigley11689d42009-01-16 09:22:03 -05002622 match_prefix(ROOTCONTEXT_STR, sizeof(ROOTCONTEXT_STR)-1, option, len) ||
2623 match_prefix(LABELSUPP_STR, sizeof(LABELSUPP_STR)-1, option, len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624}
2625
2626static inline void take_option(char **to, char *from, int *first, int len)
2627{
2628 if (!*first) {
2629 **to = ',';
2630 *to += 1;
Cory Olmo3528a952006-09-29 01:58:44 -07002631 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 *first = 0;
2633 memcpy(*to, from, len);
2634 *to += len;
2635}
2636
Eric Paris828dfe12008-04-17 13:17:49 -04002637static inline void take_selinux_option(char **to, char *from, int *first,
2638 int len)
Cory Olmo3528a952006-09-29 01:58:44 -07002639{
2640 int current_size = 0;
2641
2642 if (!*first) {
2643 **to = '|';
2644 *to += 1;
Eric Paris828dfe12008-04-17 13:17:49 -04002645 } else
Cory Olmo3528a952006-09-29 01:58:44 -07002646 *first = 0;
2647
2648 while (current_size < len) {
2649 if (*from != '"') {
2650 **to = *from;
2651 *to += 1;
2652 }
2653 from += 1;
2654 current_size += 1;
2655 }
2656}
2657
Eric Parise0007522008-03-05 10:31:54 -05002658static int selinux_sb_copy_data(char *orig, char *copy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659{
2660 int fnosec, fsec, rc = 0;
2661 char *in_save, *in_curr, *in_end;
2662 char *sec_curr, *nosec_save, *nosec;
Cory Olmo3528a952006-09-29 01:58:44 -07002663 int open_quote = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664
2665 in_curr = orig;
2666 sec_curr = copy;
2667
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 nosec = (char *)get_zeroed_page(GFP_KERNEL);
2669 if (!nosec) {
2670 rc = -ENOMEM;
2671 goto out;
2672 }
2673
2674 nosec_save = nosec;
2675 fnosec = fsec = 1;
2676 in_save = in_end = orig;
2677
2678 do {
Cory Olmo3528a952006-09-29 01:58:44 -07002679 if (*in_end == '"')
2680 open_quote = !open_quote;
2681 if ((*in_end == ',' && open_quote == 0) ||
2682 *in_end == '\0') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 int len = in_end - in_curr;
2684
2685 if (selinux_option(in_curr, len))
Cory Olmo3528a952006-09-29 01:58:44 -07002686 take_selinux_option(&sec_curr, in_curr, &fsec, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 else
2688 take_option(&nosec, in_curr, &fnosec, len);
2689
2690 in_curr = in_end + 1;
2691 }
2692 } while (*in_end++);
2693
Eric Paris6931dfc2005-06-30 02:58:51 -07002694 strcpy(in_save, nosec_save);
Gerald Schaeferda3caa22005-06-21 17:15:18 -07002695 free_page((unsigned long)nosec_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696out:
2697 return rc;
2698}
2699
Al Viro204cc0c2018-12-13 13:41:47 -05002700static int selinux_sb_eat_lsm_opts(char *options, void **mnt_opts)
Al Viro5b400232018-12-12 20:13:29 -05002701{
2702 char *s = (char *)get_zeroed_page(GFP_KERNEL);
2703 int err;
2704
2705 if (!s)
2706 return -ENOMEM;
2707 err = selinux_sb_copy_data(options, s);
2708 if (!err)
Al Viro204cc0c2018-12-13 13:41:47 -05002709 err = selinux_parse_opts_str(s, mnt_opts);
Al Viro5b400232018-12-12 20:13:29 -05002710 free_page((unsigned long)s);
2711 return err;
2712}
2713
Al Viro204cc0c2018-12-13 13:41:47 -05002714static int selinux_sb_remount(struct super_block *sb, void *mnt_opts)
Eric Paris026eb162011-03-03 16:09:14 -05002715{
Al Virobd323652018-12-13 15:04:59 -05002716 struct selinux_mnt_opts *opts = mnt_opts;
Eric Paris026eb162011-03-03 16:09:14 -05002717 struct superblock_security_struct *sbsec = sb->s_security;
Al Virobd323652018-12-13 15:04:59 -05002718 u32 sid;
2719 int rc;
Eric Paris026eb162011-03-03 16:09:14 -05002720
2721 if (!(sbsec->flags & SE_SBINITIALIZED))
2722 return 0;
2723
Al Viro204cc0c2018-12-13 13:41:47 -05002724 if (!opts)
2725 return 0;
2726
Al Virobd323652018-12-13 15:04:59 -05002727 if (opts->fscontext) {
2728 rc = parse_sid(sb, opts->fscontext, &sid);
2729 if (rc)
Al Viroc039bc32018-12-01 23:06:57 -05002730 return rc;
Al Virobd323652018-12-13 15:04:59 -05002731 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
2732 goto out_bad_option;
2733 }
2734 if (opts->context) {
2735 rc = parse_sid(sb, opts->context, &sid);
2736 if (rc)
2737 return rc;
2738 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
2739 goto out_bad_option;
2740 }
2741 if (opts->rootcontext) {
2742 struct inode_security_struct *root_isec;
2743 root_isec = backing_inode_security(sb->s_root);
2744 rc = parse_sid(sb, opts->rootcontext, &sid);
2745 if (rc)
2746 return rc;
2747 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
2748 goto out_bad_option;
2749 }
2750 if (opts->defcontext) {
2751 rc = parse_sid(sb, opts->defcontext, &sid);
2752 if (rc)
2753 return rc;
2754 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
2755 goto out_bad_option;
Eric Paris026eb162011-03-03 16:09:14 -05002756 }
Al Viroc039bc32018-12-01 23:06:57 -05002757 return 0;
Eric Paris026eb162011-03-03 16:09:14 -05002758
Eric Paris026eb162011-03-03 16:09:14 -05002759out_bad_option:
peter enderborgc103a912018-06-12 10:09:03 +02002760 pr_warn("SELinux: unable to change security options "
Linus Torvalds29b1deb2013-12-15 11:17:45 -08002761 "during remount (dev %s, type=%s)\n", sb->s_id,
2762 sb->s_type->name);
Al Viroc039bc32018-12-01 23:06:57 -05002763 return -EINVAL;
Eric Paris026eb162011-03-03 16:09:14 -05002764}
2765
Al Viroa10d7c22018-12-05 11:58:35 -05002766static int selinux_sb_kern_mount(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767{
David Howells88e67f32008-11-14 10:39:21 +11002768 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002769 struct common_audit_data ad;
James Morris74192242008-12-19 11:41:10 +11002770
Eric Paris50c205f2012-04-04 15:01:43 -04002771 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002772 ad.u.dentry = sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002773 return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774}
2775
David Howells726c3342006-06-23 02:02:58 -07002776static int selinux_sb_statfs(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777{
David Howells88e67f32008-11-14 10:39:21 +11002778 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002779 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780
Eric Paris50c205f2012-04-04 15:01:43 -04002781 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002782 ad.u.dentry = dentry->d_sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002783 return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784}
2785
Al Viro808d4e32012-10-11 11:42:01 -04002786static int selinux_mount(const char *dev_name,
Al Viro8a04c432016-03-25 14:52:53 -04002787 const struct path *path,
Al Viro808d4e32012-10-11 11:42:01 -04002788 const char *type,
Eric Paris828dfe12008-04-17 13:17:49 -04002789 unsigned long flags,
2790 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791{
David Howells88e67f32008-11-14 10:39:21 +11002792 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793
2794 if (flags & MS_REMOUNT)
Al Virod8c95842011-12-07 18:16:57 -05002795 return superblock_has_perm(cred, path->dentry->d_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002796 FILESYSTEM__REMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 else
Eric Paris2875fa02011-04-28 16:04:24 -04002798 return path_has_perm(cred, path, FILE__MOUNTON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799}
2800
2801static int selinux_umount(struct vfsmount *mnt, int flags)
2802{
David Howells88e67f32008-11-14 10:39:21 +11002803 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804
David Howells88e67f32008-11-14 10:39:21 +11002805 return superblock_has_perm(cred, mnt->mnt_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002806 FILESYSTEM__UNMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807}
2808
2809/* inode security operations */
2810
2811static int selinux_inode_alloc_security(struct inode *inode)
2812{
2813 return inode_alloc_security(inode);
2814}
2815
2816static void selinux_inode_free_security(struct inode *inode)
2817{
2818 inode_free_security(inode);
2819}
2820
David Quigleyd47be3d2013-05-22 12:50:34 -04002821static int selinux_dentry_init_security(struct dentry *dentry, int mode,
Al Viro4f3ccd72016-07-20 16:06:15 -04002822 const struct qstr *name, void **ctx,
David Quigleyd47be3d2013-05-22 12:50:34 -04002823 u32 *ctxlen)
2824{
David Quigleyd47be3d2013-05-22 12:50:34 -04002825 u32 newsid;
2826 int rc;
2827
Vivek Goyalc957f6d2016-07-13 10:44:51 -04002828 rc = selinux_determine_inode_label(current_security(),
2829 d_inode(dentry->d_parent), name,
David Howellsc3c188b2015-07-10 17:19:58 -04002830 inode_mode_to_security_class(mode),
2831 &newsid);
2832 if (rc)
2833 return rc;
David Quigleyd47be3d2013-05-22 12:50:34 -04002834
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002835 return security_sid_to_context(&selinux_state, newsid, (char **)ctx,
2836 ctxlen);
David Quigleyd47be3d2013-05-22 12:50:34 -04002837}
2838
Vivek Goyala518b0a2016-07-13 10:44:53 -04002839static int selinux_dentry_create_files_as(struct dentry *dentry, int mode,
2840 struct qstr *name,
2841 const struct cred *old,
2842 struct cred *new)
2843{
2844 u32 newsid;
2845 int rc;
2846 struct task_security_struct *tsec;
2847
2848 rc = selinux_determine_inode_label(old->security,
2849 d_inode(dentry->d_parent), name,
2850 inode_mode_to_security_class(mode),
2851 &newsid);
2852 if (rc)
2853 return rc;
2854
2855 tsec = new->security;
2856 tsec->create_sid = newsid;
2857 return 0;
2858}
2859
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002860static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +09002861 const struct qstr *qstr,
2862 const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -05002863 void **value, size_t *len)
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002864{
Paul Moore5fb49872010-04-22 14:46:19 -04002865 const struct task_security_struct *tsec = current_security();
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002866 struct superblock_security_struct *sbsec;
Corentin LABBEc0d4f462017-10-04 20:32:17 +02002867 u32 newsid, clen;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002868 int rc;
Tetsuo Handa95489062013-07-25 05:44:02 +09002869 char *context;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002870
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002871 sbsec = dir->i_sb->s_security;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002872
David Howells275bb412008-11-14 10:39:19 +11002873 newsid = tsec->create_sid;
2874
Vivek Goyalc957f6d2016-07-13 10:44:51 -04002875 rc = selinux_determine_inode_label(current_security(),
David Howellsc3c188b2015-07-10 17:19:58 -04002876 dir, qstr,
2877 inode_mode_to_security_class(inode->i_mode),
2878 &newsid);
2879 if (rc)
2880 return rc;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002881
Eric Paris296fddf2006-09-25 23:32:00 -07002882 /* Possibly defer initialization to selinux_complete_init. */
David P. Quigley0d90a7e2009-01-16 09:22:02 -05002883 if (sbsec->flags & SE_SBINITIALIZED) {
Eric Paris296fddf2006-09-25 23:32:00 -07002884 struct inode_security_struct *isec = inode->i_security;
2885 isec->sclass = inode_mode_to_security_class(inode->i_mode);
2886 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05002887 isec->initialized = LABEL_INITIALIZED;
Eric Paris296fddf2006-09-25 23:32:00 -07002888 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002889
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002890 if (!selinux_state.initialized || !(sbsec->flags & SBLABEL_MNT))
Stephen Smalley25a74f32005-11-08 21:34:33 -08002891 return -EOPNOTSUPP;
2892
Tetsuo Handa95489062013-07-25 05:44:02 +09002893 if (name)
2894 *name = XATTR_SELINUX_SUFFIX;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002895
2896 if (value && len) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002897 rc = security_sid_to_context_force(&selinux_state, newsid,
2898 &context, &clen);
Tetsuo Handa95489062013-07-25 05:44:02 +09002899 if (rc)
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002900 return rc;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002901 *value = context;
2902 *len = clen;
2903 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002904
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002905 return 0;
2906}
2907
Al Viro4acdaf22011-07-26 01:42:34 -04002908static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909{
2910 return may_create(dir, dentry, SECCLASS_FILE);
2911}
2912
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2914{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 return may_link(dir, old_dentry, MAY_LINK);
2916}
2917
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
2919{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 return may_link(dir, dentry, MAY_UNLINK);
2921}
2922
2923static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
2924{
2925 return may_create(dir, dentry, SECCLASS_LNK_FILE);
2926}
2927
Al Viro18bb1db2011-07-26 01:41:39 -04002928static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929{
2930 return may_create(dir, dentry, SECCLASS_DIR);
2931}
2932
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
2934{
2935 return may_link(dir, dentry, MAY_RMDIR);
2936}
2937
Al Viro1a67aaf2011-07-26 01:52:52 -04002938static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 return may_create(dir, dentry, inode_mode_to_security_class(mode));
2941}
2942
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
Eric Paris828dfe12008-04-17 13:17:49 -04002944 struct inode *new_inode, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945{
2946 return may_rename(old_inode, old_dentry, new_inode, new_dentry);
2947}
2948
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949static int selinux_inode_readlink(struct dentry *dentry)
2950{
David Howells88e67f32008-11-14 10:39:21 +11002951 const struct cred *cred = current_cred();
2952
Eric Paris2875fa02011-04-28 16:04:24 -04002953 return dentry_has_perm(cred, dentry, FILE__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954}
2955
NeilBrownbda0be72015-03-23 13:37:39 +11002956static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode,
2957 bool rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958{
David Howells88e67f32008-11-14 10:39:21 +11002959 const struct cred *cred = current_cred();
NeilBrownbda0be72015-03-23 13:37:39 +11002960 struct common_audit_data ad;
2961 struct inode_security_struct *isec;
2962 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963
NeilBrownbda0be72015-03-23 13:37:39 +11002964 validate_creds(cred);
2965
2966 ad.type = LSM_AUDIT_DATA_DENTRY;
2967 ad.u.dentry = dentry;
2968 sid = cred_sid(cred);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05002969 isec = inode_security_rcu(inode, rcu);
2970 if (IS_ERR(isec))
2971 return PTR_ERR(isec);
NeilBrownbda0be72015-03-23 13:37:39 +11002972
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002973 return avc_has_perm_flags(&selinux_state,
2974 sid, isec->sid, isec->sclass, FILE__READ, &ad,
NeilBrownbda0be72015-03-23 13:37:39 +11002975 rcu ? MAY_NOT_BLOCK : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976}
2977
Eric Parisd4cf970d2012-04-04 15:01:42 -04002978static noinline int audit_inode_permission(struct inode *inode,
2979 u32 perms, u32 audited, u32 denied,
Stephen Smalley626b9742014-04-29 11:29:04 -07002980 int result,
Eric Parisd4cf970d2012-04-04 15:01:42 -04002981 unsigned flags)
2982{
2983 struct common_audit_data ad;
Eric Parisd4cf970d2012-04-04 15:01:42 -04002984 struct inode_security_struct *isec = inode->i_security;
2985 int rc;
2986
Eric Paris50c205f2012-04-04 15:01:43 -04002987 ad.type = LSM_AUDIT_DATA_INODE;
Eric Parisd4cf970d2012-04-04 15:01:42 -04002988 ad.u.inode = inode;
2989
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002990 rc = slow_avc_audit(&selinux_state,
2991 current_sid(), isec->sid, isec->sclass, perms,
Stephen Smalley626b9742014-04-29 11:29:04 -07002992 audited, denied, result, &ad, flags);
Eric Parisd4cf970d2012-04-04 15:01:42 -04002993 if (rc)
2994 return rc;
2995 return 0;
2996}
2997
Al Viroe74f71e2011-06-20 19:38:15 -04002998static int selinux_inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999{
David Howells88e67f32008-11-14 10:39:21 +11003000 const struct cred *cred = current_cred();
Eric Parisb782e0a2010-07-23 11:44:03 -04003001 u32 perms;
3002 bool from_access;
Al Virocf1dd1d2011-06-20 19:44:08 -04003003 unsigned flags = mask & MAY_NOT_BLOCK;
Eric Paris2e334052012-04-04 15:01:42 -04003004 struct inode_security_struct *isec;
3005 u32 sid;
3006 struct av_decision avd;
3007 int rc, rc2;
3008 u32 audited, denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009
Eric Parisb782e0a2010-07-23 11:44:03 -04003010 from_access = mask & MAY_ACCESS;
Eric Parisd09ca732010-07-23 11:43:57 -04003011 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
3012
Eric Parisb782e0a2010-07-23 11:44:03 -04003013 /* No permission to check. Existence test. */
3014 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016
Eric Paris2e334052012-04-04 15:01:42 -04003017 validate_creds(cred);
Eric Parisb782e0a2010-07-23 11:44:03 -04003018
Eric Paris2e334052012-04-04 15:01:42 -04003019 if (unlikely(IS_PRIVATE(inode)))
3020 return 0;
Eric Parisb782e0a2010-07-23 11:44:03 -04003021
3022 perms = file_mask_to_av(inode->i_mode, mask);
3023
Eric Paris2e334052012-04-04 15:01:42 -04003024 sid = cred_sid(cred);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05003025 isec = inode_security_rcu(inode, flags & MAY_NOT_BLOCK);
3026 if (IS_ERR(isec))
3027 return PTR_ERR(isec);
Eric Paris2e334052012-04-04 15:01:42 -04003028
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003029 rc = avc_has_perm_noaudit(&selinux_state,
3030 sid, isec->sid, isec->sclass, perms, 0, &avd);
Eric Paris2e334052012-04-04 15:01:42 -04003031 audited = avc_audit_required(perms, &avd, rc,
3032 from_access ? FILE__AUDIT_ACCESS : 0,
3033 &denied);
3034 if (likely(!audited))
3035 return rc;
3036
Stephen Smalley626b9742014-04-29 11:29:04 -07003037 rc2 = audit_inode_permission(inode, perms, audited, denied, rc, flags);
Eric Paris2e334052012-04-04 15:01:42 -04003038 if (rc2)
3039 return rc2;
3040 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041}
3042
3043static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
3044{
David Howells88e67f32008-11-14 10:39:21 +11003045 const struct cred *cred = current_cred();
Stephen Smalleyccb54472017-05-12 12:41:24 -04003046 struct inode *inode = d_backing_inode(dentry);
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003047 unsigned int ia_valid = iattr->ia_valid;
Eric Paris95dbf732012-04-04 13:45:34 -04003048 __u32 av = FILE__WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003050 /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
3051 if (ia_valid & ATTR_FORCE) {
3052 ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
3053 ATTR_FORCE);
3054 if (!ia_valid)
3055 return 0;
3056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003058 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
3059 ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
Eric Paris2875fa02011-04-28 16:04:24 -04003060 return dentry_has_perm(cred, dentry, FILE__SETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003062 if (selinux_policycap_openperm() &&
Stephen Smalleyccb54472017-05-12 12:41:24 -04003063 inode->i_sb->s_magic != SOCKFS_MAGIC &&
3064 (ia_valid & ATTR_SIZE) &&
3065 !(ia_valid & ATTR_FILE))
Eric Paris95dbf732012-04-04 13:45:34 -04003066 av |= FILE__OPEN;
3067
3068 return dentry_has_perm(cred, dentry, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069}
3070
Al Viro3f7036a2015-03-08 19:28:30 -04003071static int selinux_inode_getattr(const struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072{
Al Viro3f7036a2015-03-08 19:28:30 -04003073 return path_has_perm(current_cred(), path, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074}
3075
Stephen Smalleydb590002017-04-20 11:31:30 -04003076static bool has_cap_mac_admin(bool audit)
3077{
3078 const struct cred *cred = current_cred();
3079 int cap_audit = audit ? SECURITY_CAP_AUDIT : SECURITY_CAP_NOAUDIT;
3080
3081 if (cap_capable(cred, &init_user_ns, CAP_MAC_ADMIN, cap_audit))
3082 return false;
3083 if (cred_has_capability(cred, CAP_MAC_ADMIN, cap_audit, true))
3084 return false;
3085 return true;
3086}
3087
David Howells8f0cfa52008-04-29 00:59:41 -07003088static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
3089 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090{
David Howellsc6f493d2015-03-17 22:26:22 +00003091 struct inode *inode = d_backing_inode(dentry);
Paul Moore20cdef82016-04-04 14:14:42 -04003092 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 struct superblock_security_struct *sbsec;
Thomas Liu2bf49692009-07-14 12:14:09 -04003094 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11003095 u32 newsid, sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 int rc = 0;
3097
Eric W. Biederman6b240302017-10-02 09:38:20 -05003098 if (strcmp(name, XATTR_NAME_SELINUX)) {
3099 rc = cap_inode_setxattr(dentry, name, value, size, flags);
3100 if (rc)
3101 return rc;
3102
3103 /* Not an attribute we recognize, so just check the
3104 ordinary setattr permission. */
3105 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107
3108 sbsec = inode->i_sb->s_security;
Eric Paris12f348b2012-10-09 10:56:25 -04003109 if (!(sbsec->flags & SBLABEL_MNT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 return -EOPNOTSUPP;
3111
Serge E. Hallyn2e149672011-03-23 16:43:26 -07003112 if (!inode_owner_or_capable(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 return -EPERM;
3114
Eric Paris50c205f2012-04-04 15:01:43 -04003115 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04003116 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117
Paul Moore20cdef82016-04-04 14:14:42 -04003118 isec = backing_inode_security(dentry);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003119 rc = avc_has_perm(&selinux_state,
3120 sid, isec->sid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 FILE__RELABELFROM, &ad);
3122 if (rc)
3123 return rc;
3124
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003125 rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3126 GFP_KERNEL);
Stephen Smalley12b29f32008-05-07 13:03:20 -04003127 if (rc == -EINVAL) {
Stephen Smalleydb590002017-04-20 11:31:30 -04003128 if (!has_cap_mac_admin(true)) {
Eric Parisd6ea83e2012-04-04 13:45:49 -04003129 struct audit_buffer *ab;
3130 size_t audit_size;
Eric Parisd6ea83e2012-04-04 13:45:49 -04003131
3132 /* We strip a nul only if it is at the end, otherwise the
3133 * context contains a nul and we should audit that */
Al Viroe3fea3f2012-06-09 08:15:16 +01003134 if (value) {
Colin Ian Kingadd24372017-10-14 13:46:55 +01003135 const char *str = value;
3136
Al Viroe3fea3f2012-06-09 08:15:16 +01003137 if (str[size - 1] == '\0')
3138 audit_size = size - 1;
3139 else
3140 audit_size = size;
3141 } else {
Al Viroe3fea3f2012-06-09 08:15:16 +01003142 audit_size = 0;
3143 }
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04003144 ab = audit_log_start(audit_context(),
3145 GFP_ATOMIC, AUDIT_SELINUX_ERR);
Eric Parisd6ea83e2012-04-04 13:45:49 -04003146 audit_log_format(ab, "op=setxattr invalid_context=");
3147 audit_log_n_untrustedstring(ab, value, audit_size);
3148 audit_log_end(ab);
3149
Stephen Smalley12b29f32008-05-07 13:03:20 -04003150 return rc;
Eric Parisd6ea83e2012-04-04 13:45:49 -04003151 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003152 rc = security_context_to_sid_force(&selinux_state, value,
3153 size, &newsid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04003154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 if (rc)
3156 return rc;
3157
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003158 rc = avc_has_perm(&selinux_state,
3159 sid, newsid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 FILE__RELABELTO, &ad);
3161 if (rc)
3162 return rc;
3163
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003164 rc = security_validate_transition(&selinux_state, isec->sid, newsid,
3165 sid, isec->sclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 if (rc)
3167 return rc;
3168
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003169 return avc_has_perm(&selinux_state,
3170 newsid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 sbsec->sid,
3172 SECCLASS_FILESYSTEM,
3173 FILESYSTEM__ASSOCIATE,
3174 &ad);
3175}
3176
David Howells8f0cfa52008-04-29 00:59:41 -07003177static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
Eric Parisf5269712008-05-14 11:27:45 -04003178 const void *value, size_t size,
David Howells8f0cfa52008-04-29 00:59:41 -07003179 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180{
David Howellsc6f493d2015-03-17 22:26:22 +00003181 struct inode *inode = d_backing_inode(dentry);
Paul Moore20cdef82016-04-04 14:14:42 -04003182 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 u32 newsid;
3184 int rc;
3185
3186 if (strcmp(name, XATTR_NAME_SELINUX)) {
3187 /* Not an attribute we recognize, so nothing to do. */
3188 return;
3189 }
3190
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003191 rc = security_context_to_sid_force(&selinux_state, value, size,
3192 &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 if (rc) {
peter enderborgc103a912018-06-12 10:09:03 +02003194 pr_err("SELinux: unable to map context to SID"
Stephen Smalley12b29f32008-05-07 13:03:20 -04003195 "for (%s, %lu), rc=%d\n",
3196 inode->i_sb->s_id, inode->i_ino, -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 return;
3198 }
3199
Paul Moore20cdef82016-04-04 14:14:42 -04003200 isec = backing_inode_security(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003201 spin_lock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003202 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05003204 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003205 spin_unlock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003206
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 return;
3208}
3209
David Howells8f0cfa52008-04-29 00:59:41 -07003210static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211{
David Howells88e67f32008-11-14 10:39:21 +11003212 const struct cred *cred = current_cred();
3213
Eric Paris2875fa02011-04-28 16:04:24 -04003214 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215}
3216
Eric Paris828dfe12008-04-17 13:17:49 -04003217static int selinux_inode_listxattr(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218{
David Howells88e67f32008-11-14 10:39:21 +11003219 const struct cred *cred = current_cred();
3220
Eric Paris2875fa02011-04-28 16:04:24 -04003221 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222}
3223
David Howells8f0cfa52008-04-29 00:59:41 -07003224static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225{
Eric W. Biederman6b240302017-10-02 09:38:20 -05003226 if (strcmp(name, XATTR_NAME_SELINUX)) {
3227 int rc = cap_inode_removexattr(dentry, name);
3228 if (rc)
3229 return rc;
3230
3231 /* Not an attribute we recognize, so just check the
3232 ordinary setattr permission. */
3233 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235
3236 /* No one is allowed to remove a SELinux security label.
3237 You can change the label, but all data must be labeled. */
3238 return -EACCES;
3239}
3240
James Morrisd381d8a2005-10-30 14:59:22 -08003241/*
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003242 * Copy the inode security context value to the user.
James Morrisd381d8a2005-10-30 14:59:22 -08003243 *
3244 * Permission check is handled by selinux_inode_getxattr hook.
3245 */
Andreas Gruenbacherea861df2015-12-24 11:09:39 -05003246static int selinux_inode_getsecurity(struct inode *inode, const char *name, void **buffer, bool alloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247{
David P. Quigley42492592008-02-04 22:29:39 -08003248 u32 size;
3249 int error;
3250 char *context = NULL;
Paul Moore20cdef82016-04-04 14:14:42 -04003251 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00003253 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3254 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003256 /*
3257 * If the caller has CAP_MAC_ADMIN, then get the raw context
3258 * value even if it is not defined by current policy; otherwise,
3259 * use the in-core value under current policy.
3260 * Use the non-auditing forms of the permission checks since
3261 * getxattr may be called by unprivileged processes commonly
3262 * and lack of permission just means that we fall back to the
3263 * in-core context value, not a denial.
3264 */
Paul Moore20cdef82016-04-04 14:14:42 -04003265 isec = inode_security(inode);
Stephen Smalleydb590002017-04-20 11:31:30 -04003266 if (has_cap_mac_admin(false))
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003267 error = security_sid_to_context_force(&selinux_state,
3268 isec->sid, &context,
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003269 &size);
3270 else
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003271 error = security_sid_to_context(&selinux_state, isec->sid,
3272 &context, &size);
David P. Quigley42492592008-02-04 22:29:39 -08003273 if (error)
3274 return error;
3275 error = size;
3276 if (alloc) {
3277 *buffer = context;
3278 goto out_nofree;
3279 }
3280 kfree(context);
3281out_nofree:
3282 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283}
3284
3285static int selinux_inode_setsecurity(struct inode *inode, const char *name,
Eric Paris828dfe12008-04-17 13:17:49 -04003286 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287{
Paul Moore2c971652016-04-19 16:36:28 -04003288 struct inode_security_struct *isec = inode_security_novalidate(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 u32 newsid;
3290 int rc;
3291
3292 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3293 return -EOPNOTSUPP;
3294
3295 if (!value || !size)
3296 return -EACCES;
3297
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003298 rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3299 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 if (rc)
3301 return rc;
3302
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003303 spin_lock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003304 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05003306 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003307 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 return 0;
3309}
3310
3311static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
3312{
3313 const int len = sizeof(XATTR_NAME_SELINUX);
3314 if (buffer && len <= buffer_size)
3315 memcpy(buffer, XATTR_NAME_SELINUX, len);
3316 return len;
3317}
3318
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05003319static void selinux_inode_getsecid(struct inode *inode, u32 *secid)
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02003320{
Andreas Gruenbachere817c2f2016-02-18 12:04:08 +01003321 struct inode_security_struct *isec = inode_security_novalidate(inode);
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02003322 *secid = isec->sid;
3323}
3324
Vivek Goyal56909eb2016-07-13 10:44:48 -04003325static int selinux_inode_copy_up(struct dentry *src, struct cred **new)
3326{
3327 u32 sid;
3328 struct task_security_struct *tsec;
3329 struct cred *new_creds = *new;
3330
3331 if (new_creds == NULL) {
3332 new_creds = prepare_creds();
3333 if (!new_creds)
3334 return -ENOMEM;
3335 }
3336
3337 tsec = new_creds->security;
3338 /* Get label from overlay inode and set it in create_sid */
3339 selinux_inode_getsecid(d_inode(src), &sid);
3340 tsec->create_sid = sid;
3341 *new = new_creds;
3342 return 0;
3343}
3344
Vivek Goyal19472b62016-07-13 10:44:50 -04003345static int selinux_inode_copy_up_xattr(const char *name)
3346{
3347 /* The copy_up hook above sets the initial context on an inode, but we
3348 * don't then want to overwrite it by blindly copying all the lower
3349 * xattrs up. Instead, we have to filter out SELinux-related xattrs.
3350 */
3351 if (strcmp(name, XATTR_NAME_SELINUX) == 0)
3352 return 1; /* Discard */
3353 /*
3354 * Any other attribute apart from SELINUX is not claimed, supported
3355 * by selinux.
3356 */
3357 return -EOPNOTSUPP;
3358}
3359
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360/* file security operations */
3361
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003362static int selinux_revalidate_file_permission(struct file *file, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363{
David Howells88e67f32008-11-14 10:39:21 +11003364 const struct cred *cred = current_cred();
Al Viro496ad9a2013-01-23 17:07:38 -05003365 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
3368 if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
3369 mask |= MAY_APPEND;
3370
Paul Moore389fb8002009-03-27 17:10:34 -04003371 return file_has_perm(cred, file,
3372 file_mask_to_av(inode->i_mode, mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373}
3374
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003375static int selinux_file_permission(struct file *file, int mask)
3376{
Al Viro496ad9a2013-01-23 17:07:38 -05003377 struct inode *inode = file_inode(file);
Stephen Smalley20dda182009-06-22 14:54:53 -04003378 struct file_security_struct *fsec = file->f_security;
Andreas Gruenbacherb1973672016-01-05 23:12:33 +01003379 struct inode_security_struct *isec;
Stephen Smalley20dda182009-06-22 14:54:53 -04003380 u32 sid = current_sid();
3381
Paul Moore389fb8002009-03-27 17:10:34 -04003382 if (!mask)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003383 /* No permission to check. Existence test. */
3384 return 0;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003385
Andreas Gruenbacherb1973672016-01-05 23:12:33 +01003386 isec = inode_security(inode);
Stephen Smalley20dda182009-06-22 14:54:53 -04003387 if (sid == fsec->sid && fsec->isid == isec->sid &&
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003388 fsec->pseqno == avc_policy_seqno(&selinux_state))
Eric Paris83d49852012-04-04 13:45:40 -04003389 /* No change since file_open check. */
Stephen Smalley20dda182009-06-22 14:54:53 -04003390 return 0;
3391
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003392 return selinux_revalidate_file_permission(file, mask);
3393}
3394
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395static int selinux_file_alloc_security(struct file *file)
3396{
3397 return file_alloc_security(file);
3398}
3399
3400static void selinux_file_free_security(struct file *file)
3401{
3402 file_free_security(file);
3403}
3404
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003405/*
3406 * Check whether a task has the ioctl permission and cmd
3407 * operation to an inode.
3408 */
Geliang Tang1d2a1682015-10-21 17:44:27 -04003409static int ioctl_has_perm(const struct cred *cred, struct file *file,
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003410 u32 requested, u16 cmd)
3411{
3412 struct common_audit_data ad;
3413 struct file_security_struct *fsec = file->f_security;
3414 struct inode *inode = file_inode(file);
Paul Moore20cdef82016-04-04 14:14:42 -04003415 struct inode_security_struct *isec;
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003416 struct lsm_ioctlop_audit ioctl;
3417 u32 ssid = cred_sid(cred);
3418 int rc;
3419 u8 driver = cmd >> 8;
3420 u8 xperm = cmd & 0xff;
3421
3422 ad.type = LSM_AUDIT_DATA_IOCTL_OP;
3423 ad.u.op = &ioctl;
3424 ad.u.op->cmd = cmd;
3425 ad.u.op->path = file->f_path;
3426
3427 if (ssid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003428 rc = avc_has_perm(&selinux_state,
3429 ssid, fsec->sid,
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003430 SECCLASS_FD,
3431 FD__USE,
3432 &ad);
3433 if (rc)
3434 goto out;
3435 }
3436
3437 if (unlikely(IS_PRIVATE(inode)))
3438 return 0;
3439
Paul Moore20cdef82016-04-04 14:14:42 -04003440 isec = inode_security(inode);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003441 rc = avc_has_extended_perms(&selinux_state,
3442 ssid, isec->sid, isec->sclass,
3443 requested, driver, xperm, &ad);
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003444out:
3445 return rc;
3446}
3447
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448static int selinux_file_ioctl(struct file *file, unsigned int cmd,
3449 unsigned long arg)
3450{
David Howells88e67f32008-11-14 10:39:21 +11003451 const struct cred *cred = current_cred();
Eric Paris0b24dcb2011-02-25 15:39:20 -05003452 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453
Eric Paris0b24dcb2011-02-25 15:39:20 -05003454 switch (cmd) {
3455 case FIONREAD:
3456 /* fall through */
3457 case FIBMAP:
3458 /* fall through */
3459 case FIGETBSZ:
3460 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003461 case FS_IOC_GETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003462 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003463 case FS_IOC_GETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003464 error = file_has_perm(cred, file, FILE__GETATTR);
3465 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466
Al Viro2f99c362012-03-23 16:04:05 -04003467 case FS_IOC_SETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003468 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003469 case FS_IOC_SETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003470 error = file_has_perm(cred, file, FILE__SETATTR);
3471 break;
3472
3473 /* sys_ioctl() checks */
3474 case FIONBIO:
3475 /* fall through */
3476 case FIOASYNC:
3477 error = file_has_perm(cred, file, 0);
3478 break;
3479
3480 case KDSKBENT:
3481 case KDSKBSENT:
Eric Paris6a9de492012-01-03 12:25:14 -05003482 error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04003483 SECURITY_CAP_AUDIT, true);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003484 break;
3485
3486 /* default case assumes that the command will go
3487 * to the file's ioctl() function.
3488 */
3489 default:
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003490 error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003491 }
3492 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493}
3494
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003495static int default_noexec;
3496
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
3498{
David Howells88e67f32008-11-14 10:39:21 +11003499 const struct cred *cred = current_cred();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003500 u32 sid = cred_sid(cred);
David Howellsd84f4f92008-11-14 10:39:23 +11003501 int rc = 0;
David Howells88e67f32008-11-14 10:39:21 +11003502
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003503 if (default_noexec &&
Stephen Smalley892e8ca2015-07-10 09:40:59 -04003504 (prot & PROT_EXEC) && (!file || IS_PRIVATE(file_inode(file)) ||
3505 (!shared && (prot & PROT_WRITE)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 /*
3507 * We are making executable an anonymous mapping or a
3508 * private file mapping that will also be writable.
3509 * This has an additional check.
3510 */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003511 rc = avc_has_perm(&selinux_state,
3512 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003513 PROCESS__EXECMEM, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 if (rc)
David Howellsd84f4f92008-11-14 10:39:23 +11003515 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517
3518 if (file) {
3519 /* read access is always possible with a mapping */
3520 u32 av = FILE__READ;
3521
3522 /* write access only matters if the mapping is shared */
3523 if (shared && (prot & PROT_WRITE))
3524 av |= FILE__WRITE;
3525
3526 if (prot & PROT_EXEC)
3527 av |= FILE__EXECUTE;
3528
David Howells88e67f32008-11-14 10:39:21 +11003529 return file_has_perm(cred, file, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 }
David Howellsd84f4f92008-11-14 10:39:23 +11003531
3532error:
3533 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534}
3535
Al Viroe5467852012-05-30 13:30:51 -04003536static int selinux_mmap_addr(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07003538 int rc = 0;
Paul Moore98883bf2014-03-19 16:46:11 -04003539
3540 if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
3541 u32 sid = current_sid();
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003542 rc = avc_has_perm(&selinux_state,
3543 sid, sid, SECCLASS_MEMPROTECT,
Paul Moore98883bf2014-03-19 16:46:11 -04003544 MEMPROTECT__MMAP_ZERO, NULL);
3545 }
3546
3547 return rc;
Al Viroe5467852012-05-30 13:30:51 -04003548}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549
Al Viroe5467852012-05-30 13:30:51 -04003550static int selinux_mmap_file(struct file *file, unsigned long reqprot,
3551 unsigned long prot, unsigned long flags)
3552{
Stephen Smalley3ba4bf52017-05-05 09:14:48 -04003553 struct common_audit_data ad;
3554 int rc;
3555
3556 if (file) {
3557 ad.type = LSM_AUDIT_DATA_FILE;
3558 ad.u.file = file;
3559 rc = inode_has_perm(current_cred(), file_inode(file),
3560 FILE__MAP, &ad);
3561 if (rc)
3562 return rc;
3563 }
3564
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003565 if (selinux_state.checkreqprot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 prot = reqprot;
3567
3568 return file_map_prot_check(file, prot,
3569 (flags & MAP_TYPE) == MAP_SHARED);
3570}
3571
3572static int selinux_file_mprotect(struct vm_area_struct *vma,
3573 unsigned long reqprot,
3574 unsigned long prot)
3575{
David Howells88e67f32008-11-14 10:39:21 +11003576 const struct cred *cred = current_cred();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003577 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003579 if (selinux_state.checkreqprot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 prot = reqprot;
3581
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003582 if (default_noexec &&
3583 (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
James Morrisd541bbe2009-01-29 12:19:51 +11003584 int rc = 0;
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003585 if (vma->vm_start >= vma->vm_mm->start_brk &&
3586 vma->vm_end <= vma->vm_mm->brk) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003587 rc = avc_has_perm(&selinux_state,
3588 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003589 PROCESS__EXECHEAP, NULL);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003590 } else if (!vma->vm_file &&
Stephen Smalleyc2316db2016-04-08 13:55:03 -04003591 ((vma->vm_start <= vma->vm_mm->start_stack &&
3592 vma->vm_end >= vma->vm_mm->start_stack) ||
Andy Lutomirskid17af502016-09-30 10:58:58 -07003593 vma_is_stack_for_current(vma))) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003594 rc = avc_has_perm(&selinux_state,
3595 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003596 PROCESS__EXECSTACK, NULL);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003597 } else if (vma->vm_file && vma->anon_vma) {
3598 /*
3599 * We are making executable a file mapping that has
3600 * had some COW done. Since pages might have been
3601 * written, check ability to execute the possibly
3602 * modified content. This typically should only
3603 * occur for text relocations.
3604 */
David Howellsd84f4f92008-11-14 10:39:23 +11003605 rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003606 }
Lorenzo Hernandez García-Hierro6b992192005-06-25 14:54:34 -07003607 if (rc)
3608 return rc;
3609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610
3611 return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
3612}
3613
3614static int selinux_file_lock(struct file *file, unsigned int cmd)
3615{
David Howells88e67f32008-11-14 10:39:21 +11003616 const struct cred *cred = current_cred();
3617
3618 return file_has_perm(cred, file, FILE__LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619}
3620
3621static int selinux_file_fcntl(struct file *file, unsigned int cmd,
3622 unsigned long arg)
3623{
David Howells88e67f32008-11-14 10:39:21 +11003624 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 int err = 0;
3626
3627 switch (cmd) {
Eric Paris828dfe12008-04-17 13:17:49 -04003628 case F_SETFL:
Eric Paris828dfe12008-04-17 13:17:49 -04003629 if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
David Howells88e67f32008-11-14 10:39:21 +11003630 err = file_has_perm(cred, file, FILE__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003632 }
3633 /* fall through */
3634 case F_SETOWN:
3635 case F_SETSIG:
3636 case F_GETFL:
3637 case F_GETOWN:
3638 case F_GETSIG:
Cyrill Gorcunov1d151c32012-07-30 14:43:00 -07003639 case F_GETOWNER_UIDS:
Eric Paris828dfe12008-04-17 13:17:49 -04003640 /* Just check FD__USE permission */
David Howells88e67f32008-11-14 10:39:21 +11003641 err = file_has_perm(cred, file, 0);
Eric Paris828dfe12008-04-17 13:17:49 -04003642 break;
3643 case F_GETLK:
3644 case F_SETLK:
3645 case F_SETLKW:
Jeff Layton0d3f7a22014-04-22 08:23:58 -04003646 case F_OFD_GETLK:
3647 case F_OFD_SETLK:
3648 case F_OFD_SETLKW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649#if BITS_PER_LONG == 32
Eric Paris828dfe12008-04-17 13:17:49 -04003650 case F_GETLK64:
3651 case F_SETLK64:
3652 case F_SETLKW64:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653#endif
David Howells88e67f32008-11-14 10:39:21 +11003654 err = file_has_perm(cred, file, FILE__LOCK);
Eric Paris828dfe12008-04-17 13:17:49 -04003655 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 }
3657
3658 return err;
3659}
3660
Jeff Laytone0b93ed2014-08-22 11:27:32 -04003661static void selinux_file_set_fowner(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 struct file_security_struct *fsec;
3664
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 fsec = file->f_security;
David Howells275bb412008-11-14 10:39:19 +11003666 fsec->fown_sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667}
3668
3669static int selinux_file_send_sigiotask(struct task_struct *tsk,
3670 struct fown_struct *fown, int signum)
3671{
Eric Paris828dfe12008-04-17 13:17:49 -04003672 struct file *file;
Stephen Smalley65c90bc2009-05-04 15:43:18 -04003673 u32 sid = task_sid(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 struct file_security_struct *fsec;
3676
3677 /* struct fown_struct is never outside the context of a struct file */
Eric Paris828dfe12008-04-17 13:17:49 -04003678 file = container_of(fown, struct file, f_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 fsec = file->f_security;
3681
3682 if (!signum)
3683 perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
3684 else
3685 perm = signal_to_av(signum);
3686
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003687 return avc_has_perm(&selinux_state,
3688 fsec->fown_sid, sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 SECCLASS_PROCESS, perm, NULL);
3690}
3691
3692static int selinux_file_receive(struct file *file)
3693{
David Howells88e67f32008-11-14 10:39:21 +11003694 const struct cred *cred = current_cred();
3695
3696 return file_has_perm(cred, file, file_to_av(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697}
3698
Al Viro94817692018-07-10 14:13:18 -04003699static int selinux_file_open(struct file *file)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003700{
3701 struct file_security_struct *fsec;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003702 struct inode_security_struct *isec;
David Howellsd84f4f92008-11-14 10:39:23 +11003703
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003704 fsec = file->f_security;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05003705 isec = inode_security(file_inode(file));
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003706 /*
3707 * Save inode label and policy sequence number
3708 * at open-time so that selinux_file_permission
3709 * can determine whether revalidation is necessary.
3710 * Task label is already saved in the file security
3711 * struct as its SID.
3712 */
3713 fsec->isid = isec->sid;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003714 fsec->pseqno = avc_policy_seqno(&selinux_state);
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003715 /*
3716 * Since the inode label or policy seqno may have changed
3717 * between the selinux_inode_permission check and the saving
3718 * of state above, recheck that access is still permitted.
3719 * Otherwise, access might never be revalidated against the
3720 * new inode label or new policy.
3721 * This check is not redundant - do not remove.
3722 */
Al Viro94817692018-07-10 14:13:18 -04003723 return file_path_has_perm(file->f_cred, file, open_file_to_av(file));
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003724}
3725
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726/* task security operations */
3727
Tetsuo Handaa79be232017-03-28 23:08:45 +09003728static int selinux_task_alloc(struct task_struct *task,
3729 unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003731 u32 sid = current_sid();
3732
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003733 return avc_has_perm(&selinux_state,
3734 sid, sid, SECCLASS_PROCESS, PROCESS__FORK, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735}
3736
David Howellsf1752ee2008-11-14 10:39:17 +11003737/*
David Howellsee18d642009-09-02 09:14:21 +01003738 * allocate the SELinux part of blank credentials
3739 */
3740static int selinux_cred_alloc_blank(struct cred *cred, gfp_t gfp)
3741{
3742 struct task_security_struct *tsec;
3743
3744 tsec = kzalloc(sizeof(struct task_security_struct), gfp);
3745 if (!tsec)
3746 return -ENOMEM;
3747
3748 cred->security = tsec;
3749 return 0;
3750}
3751
3752/*
David Howellsf1752ee2008-11-14 10:39:17 +11003753 * detach and free the LSM part of a set of credentials
3754 */
3755static void selinux_cred_free(struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756{
David Howellsf1752ee2008-11-14 10:39:17 +11003757 struct task_security_struct *tsec = cred->security;
David Howellse0e81732009-09-02 09:13:40 +01003758
Tetsuo Handa2edeaa32011-02-07 13:36:10 +00003759 /*
3760 * cred->security == NULL if security_cred_alloc_blank() or
3761 * security_prepare_creds() returned an error.
3762 */
3763 BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE);
David Howellse0e81732009-09-02 09:13:40 +01003764 cred->security = (void *) 0x7UL;
David Howellsf1752ee2008-11-14 10:39:17 +11003765 kfree(tsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766}
3767
David Howellsd84f4f92008-11-14 10:39:23 +11003768/*
3769 * prepare a new set of credentials for modification
3770 */
3771static int selinux_cred_prepare(struct cred *new, const struct cred *old,
3772 gfp_t gfp)
3773{
3774 const struct task_security_struct *old_tsec;
3775 struct task_security_struct *tsec;
3776
3777 old_tsec = old->security;
3778
3779 tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp);
3780 if (!tsec)
3781 return -ENOMEM;
3782
3783 new->security = tsec;
3784 return 0;
3785}
3786
3787/*
David Howellsee18d642009-09-02 09:14:21 +01003788 * transfer the SELinux data to a blank set of creds
3789 */
3790static void selinux_cred_transfer(struct cred *new, const struct cred *old)
3791{
3792 const struct task_security_struct *old_tsec = old->security;
3793 struct task_security_struct *tsec = new->security;
3794
3795 *tsec = *old_tsec;
3796}
3797
Matthew Garrett3ec30112018-01-08 13:36:19 -08003798static void selinux_cred_getsecid(const struct cred *c, u32 *secid)
3799{
3800 *secid = cred_sid(c);
3801}
3802
David Howellsee18d642009-09-02 09:14:21 +01003803/*
David Howells3a3b7ce2008-11-14 10:39:28 +11003804 * set the security data for a kernel service
3805 * - all the creation contexts are set to unlabelled
3806 */
3807static int selinux_kernel_act_as(struct cred *new, u32 secid)
3808{
3809 struct task_security_struct *tsec = new->security;
3810 u32 sid = current_sid();
3811 int ret;
3812
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003813 ret = avc_has_perm(&selinux_state,
3814 sid, secid,
David Howells3a3b7ce2008-11-14 10:39:28 +11003815 SECCLASS_KERNEL_SERVICE,
3816 KERNEL_SERVICE__USE_AS_OVERRIDE,
3817 NULL);
3818 if (ret == 0) {
3819 tsec->sid = secid;
3820 tsec->create_sid = 0;
3821 tsec->keycreate_sid = 0;
3822 tsec->sockcreate_sid = 0;
3823 }
3824 return ret;
3825}
3826
3827/*
3828 * set the file creation context in a security record to the same as the
3829 * objective context of the specified inode
3830 */
3831static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
3832{
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05003833 struct inode_security_struct *isec = inode_security(inode);
David Howells3a3b7ce2008-11-14 10:39:28 +11003834 struct task_security_struct *tsec = new->security;
3835 u32 sid = current_sid();
3836 int ret;
3837
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003838 ret = avc_has_perm(&selinux_state,
3839 sid, isec->sid,
David Howells3a3b7ce2008-11-14 10:39:28 +11003840 SECCLASS_KERNEL_SERVICE,
3841 KERNEL_SERVICE__CREATE_FILES_AS,
3842 NULL);
3843
3844 if (ret == 0)
3845 tsec->create_sid = isec->sid;
David Howellsef574712010-02-26 01:56:16 +00003846 return ret;
David Howells3a3b7ce2008-11-14 10:39:28 +11003847}
3848
Eric Parisdd8dbf22009-11-03 16:35:32 +11003849static int selinux_kernel_module_request(char *kmod_name)
Eric Paris25354c42009-08-13 09:45:03 -04003850{
Eric Parisdd8dbf22009-11-03 16:35:32 +11003851 struct common_audit_data ad;
3852
Eric Paris50c205f2012-04-04 15:01:43 -04003853 ad.type = LSM_AUDIT_DATA_KMOD;
Eric Parisdd8dbf22009-11-03 16:35:32 +11003854 ad.u.kmod_name = kmod_name;
3855
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003856 return avc_has_perm(&selinux_state,
3857 current_sid(), SECINITSID_KERNEL, SECCLASS_SYSTEM,
Eric Parisdd8dbf22009-11-03 16:35:32 +11003858 SYSTEM__MODULE_REQUEST, &ad);
Eric Paris25354c42009-08-13 09:45:03 -04003859}
3860
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003861static int selinux_kernel_module_from_file(struct file *file)
3862{
3863 struct common_audit_data ad;
3864 struct inode_security_struct *isec;
3865 struct file_security_struct *fsec;
3866 u32 sid = current_sid();
3867 int rc;
3868
3869 /* init_module */
3870 if (file == NULL)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003871 return avc_has_perm(&selinux_state,
3872 sid, sid, SECCLASS_SYSTEM,
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003873 SYSTEM__MODULE_LOAD, NULL);
3874
3875 /* finit_module */
Paul Moore20cdef82016-04-04 14:14:42 -04003876
Vivek Goyal43af5de2016-09-09 11:37:49 -04003877 ad.type = LSM_AUDIT_DATA_FILE;
3878 ad.u.file = file;
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003879
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003880 fsec = file->f_security;
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003881 if (sid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003882 rc = avc_has_perm(&selinux_state,
3883 sid, fsec->sid, SECCLASS_FD, FD__USE, &ad);
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003884 if (rc)
3885 return rc;
3886 }
3887
Paul Moore20cdef82016-04-04 14:14:42 -04003888 isec = inode_security(file_inode(file));
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003889 return avc_has_perm(&selinux_state,
3890 sid, isec->sid, SECCLASS_SYSTEM,
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003891 SYSTEM__MODULE_LOAD, &ad);
3892}
3893
3894static int selinux_kernel_read_file(struct file *file,
3895 enum kernel_read_file_id id)
3896{
3897 int rc = 0;
3898
3899 switch (id) {
3900 case READING_MODULE:
3901 rc = selinux_kernel_module_from_file(file);
3902 break;
3903 default:
3904 break;
3905 }
3906
3907 return rc;
3908}
3909
Mimi Zoharc77b8cd2018-07-13 14:06:02 -04003910static int selinux_kernel_load_data(enum kernel_load_data_id id)
3911{
3912 int rc = 0;
3913
3914 switch (id) {
3915 case LOADING_MODULE:
3916 rc = selinux_kernel_module_from_file(NULL);
3917 default:
3918 break;
3919 }
3920
3921 return rc;
3922}
3923
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
3925{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003926 return avc_has_perm(&selinux_state,
3927 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003928 PROCESS__SETPGID, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929}
3930
3931static int selinux_task_getpgid(struct task_struct *p)
3932{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003933 return avc_has_perm(&selinux_state,
3934 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003935 PROCESS__GETPGID, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936}
3937
3938static int selinux_task_getsid(struct task_struct *p)
3939{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003940 return avc_has_perm(&selinux_state,
3941 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003942 PROCESS__GETSESSION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943}
3944
David Quigleyf9008e4c2006-06-30 01:55:46 -07003945static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
3946{
David Howells275bb412008-11-14 10:39:19 +11003947 *secid = task_sid(p);
David Quigleyf9008e4c2006-06-30 01:55:46 -07003948}
3949
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950static int selinux_task_setnice(struct task_struct *p, int nice)
3951{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003952 return avc_has_perm(&selinux_state,
3953 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003954 PROCESS__SETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955}
3956
James Morris03e68062006-06-23 02:03:58 -07003957static int selinux_task_setioprio(struct task_struct *p, int ioprio)
3958{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003959 return avc_has_perm(&selinux_state,
3960 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003961 PROCESS__SETSCHED, NULL);
James Morris03e68062006-06-23 02:03:58 -07003962}
3963
David Quigleya1836a42006-06-30 01:55:49 -07003964static int selinux_task_getioprio(struct task_struct *p)
3965{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003966 return avc_has_perm(&selinux_state,
3967 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003968 PROCESS__GETSCHED, NULL);
David Quigleya1836a42006-06-30 01:55:49 -07003969}
3970
Corentin LABBE42985552017-10-04 20:32:18 +02003971static int selinux_task_prlimit(const struct cred *cred, const struct cred *tcred,
3972 unsigned int flags)
Stephen Smalley791ec492017-02-17 07:57:00 -05003973{
3974 u32 av = 0;
3975
Stephen Smalley84e68852017-02-28 09:35:08 -05003976 if (!flags)
3977 return 0;
Stephen Smalley791ec492017-02-17 07:57:00 -05003978 if (flags & LSM_PRLIMIT_WRITE)
3979 av |= PROCESS__SETRLIMIT;
3980 if (flags & LSM_PRLIMIT_READ)
3981 av |= PROCESS__GETRLIMIT;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003982 return avc_has_perm(&selinux_state,
3983 cred_sid(cred), cred_sid(tcred),
Stephen Smalley791ec492017-02-17 07:57:00 -05003984 SECCLASS_PROCESS, av, NULL);
3985}
3986
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003987static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
3988 struct rlimit *new_rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989{
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003990 struct rlimit *old_rlim = p->signal->rlim + resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991
3992 /* Control the ability to change the hard limit (whether
3993 lowering or raising it), so that the hard limit can
3994 later be used as a safe reset point for the soft limit
David Howellsd84f4f92008-11-14 10:39:23 +11003995 upon context transitions. See selinux_bprm_committing_creds. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 if (old_rlim->rlim_max != new_rlim->rlim_max)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003997 return avc_has_perm(&selinux_state,
3998 current_sid(), task_sid(p),
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003999 SECCLASS_PROCESS, PROCESS__SETRLIMIT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000
4001 return 0;
4002}
4003
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09004004static int selinux_task_setscheduler(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004006 return avc_has_perm(&selinux_state,
4007 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004008 PROCESS__SETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009}
4010
4011static int selinux_task_getscheduler(struct task_struct *p)
4012{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004013 return avc_has_perm(&selinux_state,
4014 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004015 PROCESS__GETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016}
4017
David Quigley35601542006-06-23 02:04:01 -07004018static int selinux_task_movememory(struct task_struct *p)
4019{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004020 return avc_has_perm(&selinux_state,
4021 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004022 PROCESS__SETSCHED, NULL);
David Quigley35601542006-06-23 02:04:01 -07004023}
4024
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02004025static int selinux_task_kill(struct task_struct *p, struct kernel_siginfo *info,
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004026 int sig, const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027{
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004028 u32 secid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 if (!sig)
4032 perm = PROCESS__SIGNULL; /* null signal; existence test */
4033 else
4034 perm = signal_to_av(sig);
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004035 if (!cred)
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004036 secid = current_sid();
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004037 else
4038 secid = cred_sid(cred);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004039 return avc_has_perm(&selinux_state,
4040 secid, task_sid(p), SECCLASS_PROCESS, perm, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041}
4042
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043static void selinux_task_to_inode(struct task_struct *p,
4044 struct inode *inode)
4045{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 struct inode_security_struct *isec = inode->i_security;
David Howells275bb412008-11-14 10:39:19 +11004047 u32 sid = task_sid(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004049 spin_lock(&isec->lock);
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01004050 isec->sclass = inode_mode_to_security_class(inode->i_mode);
David Howells275bb412008-11-14 10:39:19 +11004051 isec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004052 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004053 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054}
4055
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004057static int selinux_parse_skb_ipv4(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04004058 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059{
4060 int offset, ihlen, ret = -EINVAL;
4061 struct iphdr _iph, *ih;
4062
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03004063 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
4065 if (ih == NULL)
4066 goto out;
4067
4068 ihlen = ih->ihl * 4;
4069 if (ihlen < sizeof(_iph))
4070 goto out;
4071
Eric Paris48c62af2012-04-02 13:15:44 -04004072 ad->u.net->v4info.saddr = ih->saddr;
4073 ad->u.net->v4info.daddr = ih->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 ret = 0;
4075
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004076 if (proto)
4077 *proto = ih->protocol;
4078
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 switch (ih->protocol) {
Eric Paris828dfe12008-04-17 13:17:49 -04004080 case IPPROTO_TCP: {
4081 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082
Eric Paris828dfe12008-04-17 13:17:49 -04004083 if (ntohs(ih->frag_off) & IP_OFFSET)
4084 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085
4086 offset += ihlen;
4087 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4088 if (th == NULL)
4089 break;
4090
Eric Paris48c62af2012-04-02 13:15:44 -04004091 ad->u.net->sport = th->source;
4092 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095
Eric Paris828dfe12008-04-17 13:17:49 -04004096 case IPPROTO_UDP: {
4097 struct udphdr _udph, *uh;
4098
4099 if (ntohs(ih->frag_off) & IP_OFFSET)
4100 break;
4101
4102 offset += ihlen;
4103 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4104 if (uh == NULL)
4105 break;
4106
Eric Paris48c62af2012-04-02 13:15:44 -04004107 ad->u.net->sport = uh->source;
4108 ad->u.net->dport = uh->dest;
Eric Paris828dfe12008-04-17 13:17:49 -04004109 break;
4110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111
James Morris2ee92d42006-11-13 16:09:01 -08004112 case IPPROTO_DCCP: {
4113 struct dccp_hdr _dccph, *dh;
4114
4115 if (ntohs(ih->frag_off) & IP_OFFSET)
4116 break;
4117
4118 offset += ihlen;
4119 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4120 if (dh == NULL)
4121 break;
4122
Eric Paris48c62af2012-04-02 13:15:44 -04004123 ad->u.net->sport = dh->dccph_sport;
4124 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08004125 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004126 }
James Morris2ee92d42006-11-13 16:09:01 -08004127
Richard Hainesd4529302018-02-13 20:57:18 +00004128#if IS_ENABLED(CONFIG_IP_SCTP)
4129 case IPPROTO_SCTP: {
4130 struct sctphdr _sctph, *sh;
4131
4132 if (ntohs(ih->frag_off) & IP_OFFSET)
4133 break;
4134
4135 offset += ihlen;
4136 sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4137 if (sh == NULL)
4138 break;
4139
4140 ad->u.net->sport = sh->source;
4141 ad->u.net->dport = sh->dest;
4142 break;
4143 }
4144#endif
Eric Paris828dfe12008-04-17 13:17:49 -04004145 default:
4146 break;
4147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148out:
4149 return ret;
4150}
4151
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04004152#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153
4154/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004155static int selinux_parse_skb_ipv6(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04004156 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157{
4158 u8 nexthdr;
4159 int ret = -EINVAL, offset;
4160 struct ipv6hdr _ipv6h, *ip6;
Jesse Gross75f28112011-11-30 17:05:51 -08004161 __be16 frag_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03004163 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
4165 if (ip6 == NULL)
4166 goto out;
4167
Eric Paris48c62af2012-04-02 13:15:44 -04004168 ad->u.net->v6info.saddr = ip6->saddr;
4169 ad->u.net->v6info.daddr = ip6->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 ret = 0;
4171
4172 nexthdr = ip6->nexthdr;
4173 offset += sizeof(_ipv6h);
Jesse Gross75f28112011-11-30 17:05:51 -08004174 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 if (offset < 0)
4176 goto out;
4177
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004178 if (proto)
4179 *proto = nexthdr;
4180
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 switch (nexthdr) {
4182 case IPPROTO_TCP: {
Eric Paris828dfe12008-04-17 13:17:49 -04004183 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184
4185 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4186 if (th == NULL)
4187 break;
4188
Eric Paris48c62af2012-04-02 13:15:44 -04004189 ad->u.net->sport = th->source;
4190 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 break;
4192 }
4193
4194 case IPPROTO_UDP: {
4195 struct udphdr _udph, *uh;
4196
4197 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4198 if (uh == NULL)
4199 break;
4200
Eric Paris48c62af2012-04-02 13:15:44 -04004201 ad->u.net->sport = uh->source;
4202 ad->u.net->dport = uh->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 break;
4204 }
4205
James Morris2ee92d42006-11-13 16:09:01 -08004206 case IPPROTO_DCCP: {
4207 struct dccp_hdr _dccph, *dh;
4208
4209 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4210 if (dh == NULL)
4211 break;
4212
Eric Paris48c62af2012-04-02 13:15:44 -04004213 ad->u.net->sport = dh->dccph_sport;
4214 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08004215 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004216 }
James Morris2ee92d42006-11-13 16:09:01 -08004217
Richard Hainesd4529302018-02-13 20:57:18 +00004218#if IS_ENABLED(CONFIG_IP_SCTP)
4219 case IPPROTO_SCTP: {
4220 struct sctphdr _sctph, *sh;
4221
4222 sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4223 if (sh == NULL)
4224 break;
4225
4226 ad->u.net->sport = sh->source;
4227 ad->u.net->dport = sh->dest;
4228 break;
4229 }
4230#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 /* includes fragments */
4232 default:
4233 break;
4234 }
4235out:
4236 return ret;
4237}
4238
4239#endif /* IPV6 */
4240
Thomas Liu2bf49692009-07-14 12:14:09 -04004241static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
David Howellscf9481e2008-07-27 21:31:07 +10004242 char **_addrp, int src, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243{
David Howellscf9481e2008-07-27 21:31:07 +10004244 char *addrp;
4245 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246
Eric Paris48c62af2012-04-02 13:15:44 -04004247 switch (ad->u.net->family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 case PF_INET:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004249 ret = selinux_parse_skb_ipv4(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10004250 if (ret)
4251 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04004252 addrp = (char *)(src ? &ad->u.net->v4info.saddr :
4253 &ad->u.net->v4info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10004254 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04004256#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 case PF_INET6:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004258 ret = selinux_parse_skb_ipv6(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10004259 if (ret)
4260 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04004261 addrp = (char *)(src ? &ad->u.net->v6info.saddr :
4262 &ad->u.net->v6info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10004263 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264#endif /* IPV6 */
4265 default:
David Howellscf9481e2008-07-27 21:31:07 +10004266 addrp = NULL;
4267 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 }
4269
David Howellscf9481e2008-07-27 21:31:07 +10004270parse_error:
peter enderborgc103a912018-06-12 10:09:03 +02004271 pr_warn(
David Howellscf9481e2008-07-27 21:31:07 +10004272 "SELinux: failure in selinux_parse_skb(),"
4273 " unable to parse packet\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 return ret;
David Howellscf9481e2008-07-27 21:31:07 +10004275
4276okay:
4277 if (_addrp)
4278 *_addrp = addrp;
4279 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280}
4281
Paul Moore4f6a9932007-03-01 14:35:22 -05004282/**
Paul Moore220deb92008-01-29 08:38:23 -05004283 * selinux_skb_peerlbl_sid - Determine the peer label of a packet
Paul Moore4f6a9932007-03-01 14:35:22 -05004284 * @skb: the packet
Paul Moore75e22912008-01-29 08:38:04 -05004285 * @family: protocol family
Paul Moore220deb92008-01-29 08:38:23 -05004286 * @sid: the packet's peer label SID
Paul Moore4f6a9932007-03-01 14:35:22 -05004287 *
4288 * Description:
Paul Moore220deb92008-01-29 08:38:23 -05004289 * Check the various different forms of network peer labeling and determine
4290 * the peer label/SID for the packet; most of the magic actually occurs in
4291 * the security server function security_net_peersid_cmp(). The function
4292 * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
4293 * or -EACCES if @sid is invalid due to inconsistencies with the different
4294 * peer labels.
Paul Moore4f6a9932007-03-01 14:35:22 -05004295 *
4296 */
Paul Moore220deb92008-01-29 08:38:23 -05004297static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
Paul Moore4f6a9932007-03-01 14:35:22 -05004298{
Paul Moore71f1cb02008-01-29 08:51:16 -05004299 int err;
Paul Moore4f6a9932007-03-01 14:35:22 -05004300 u32 xfrm_sid;
4301 u32 nlbl_sid;
Paul Moore220deb92008-01-29 08:38:23 -05004302 u32 nlbl_type;
Paul Moore4f6a9932007-03-01 14:35:22 -05004303
Paul Moore817eff72013-12-10 14:57:54 -05004304 err = selinux_xfrm_skb_sid(skb, &xfrm_sid);
Paul Moorebed4d7e2013-07-23 17:38:40 -04004305 if (unlikely(err))
4306 return -EACCES;
4307 err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
4308 if (unlikely(err))
4309 return -EACCES;
Paul Moore220deb92008-01-29 08:38:23 -05004310
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004311 err = security_net_peersid_resolve(&selinux_state, nlbl_sid,
4312 nlbl_type, xfrm_sid, sid);
Paul Moore71f1cb02008-01-29 08:51:16 -05004313 if (unlikely(err)) {
peter enderborgc103a912018-06-12 10:09:03 +02004314 pr_warn(
Paul Moore71f1cb02008-01-29 08:51:16 -05004315 "SELinux: failure in selinux_skb_peerlbl_sid(),"
4316 " unable to determine packet's peer label\n");
Paul Moore220deb92008-01-29 08:38:23 -05004317 return -EACCES;
Paul Moore71f1cb02008-01-29 08:51:16 -05004318 }
Paul Moore220deb92008-01-29 08:38:23 -05004319
4320 return 0;
Paul Moore4f6a9932007-03-01 14:35:22 -05004321}
4322
Paul Moore446b8022013-12-04 16:10:51 -05004323/**
4324 * selinux_conn_sid - Determine the child socket label for a connection
4325 * @sk_sid: the parent socket's SID
4326 * @skb_sid: the packet's SID
4327 * @conn_sid: the resulting connection SID
4328 *
4329 * If @skb_sid is valid then the user:role:type information from @sk_sid is
4330 * combined with the MLS information from @skb_sid in order to create
4331 * @conn_sid. If @skb_sid is not valid then then @conn_sid is simply a copy
4332 * of @sk_sid. Returns zero on success, negative values on failure.
4333 *
4334 */
4335static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid)
4336{
4337 int err = 0;
4338
4339 if (skb_sid != SECSID_NULL)
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004340 err = security_sid_mls_copy(&selinux_state, sk_sid, skb_sid,
4341 conn_sid);
Paul Moore446b8022013-12-04 16:10:51 -05004342 else
4343 *conn_sid = sk_sid;
4344
4345 return err;
4346}
4347
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348/* socket security operations */
Paul Moored4f2d972010-04-22 14:46:18 -04004349
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004350static int socket_sockcreate_sid(const struct task_security_struct *tsec,
4351 u16 secclass, u32 *socksid)
Paul Moored4f2d972010-04-22 14:46:18 -04004352{
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004353 if (tsec->sockcreate_sid > SECSID_NULL) {
4354 *socksid = tsec->sockcreate_sid;
4355 return 0;
4356 }
4357
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004358 return security_transition_sid(&selinux_state, tsec->sid, tsec->sid,
4359 secclass, NULL, socksid);
Paul Moored4f2d972010-04-22 14:46:18 -04004360}
4361
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004362static int sock_has_perm(struct sock *sk, u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363{
Paul Moore253bfae2010-04-22 14:46:19 -04004364 struct sk_security_struct *sksec = sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004365 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004366 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367
Paul Moore253bfae2010-04-22 14:46:19 -04004368 if (sksec->sid == SECINITSID_KERNEL)
4369 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370
Eric Paris50c205f2012-04-04 15:01:43 -04004371 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004372 ad.u.net = &net;
4373 ad.u.net->sk = sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004375 return avc_has_perm(&selinux_state,
4376 current_sid(), sksec->sid, sksec->sclass, perms,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004377 &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378}
4379
4380static int selinux_socket_create(int family, int type,
4381 int protocol, int kern)
4382{
Paul Moore5fb49872010-04-22 14:46:19 -04004383 const struct task_security_struct *tsec = current_security();
Paul Moored4f2d972010-04-22 14:46:18 -04004384 u32 newsid;
David Howells275bb412008-11-14 10:39:19 +11004385 u16 secclass;
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004386 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387
4388 if (kern)
Paul Moored4f2d972010-04-22 14:46:18 -04004389 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390
David Howells275bb412008-11-14 10:39:19 +11004391 secclass = socket_type_to_security_class(family, type, protocol);
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004392 rc = socket_sockcreate_sid(tsec, secclass, &newsid);
4393 if (rc)
4394 return rc;
4395
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004396 return avc_has_perm(&selinux_state,
4397 tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398}
4399
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07004400static int selinux_socket_post_create(struct socket *sock, int family,
4401 int type, int protocol, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402{
Paul Moore5fb49872010-04-22 14:46:19 -04004403 const struct task_security_struct *tsec = current_security();
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05004404 struct inode_security_struct *isec = inode_security_novalidate(SOCK_INODE(sock));
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004405 struct sk_security_struct *sksec;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004406 u16 sclass = socket_type_to_security_class(family, type, protocol);
4407 u32 sid = SECINITSID_KERNEL;
David Howells275bb412008-11-14 10:39:19 +11004408 int err = 0;
4409
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004410 if (!kern) {
4411 err = socket_sockcreate_sid(tsec, sclass, &sid);
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004412 if (err)
4413 return err;
4414 }
David Howells275bb412008-11-14 10:39:19 +11004415
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004416 isec->sclass = sclass;
4417 isec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004418 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004420 if (sock->sk) {
4421 sksec = sock->sk->sk_security;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004422 sksec->sclass = sclass;
4423 sksec->sid = sid;
Richard Hainesd4529302018-02-13 20:57:18 +00004424 /* Allows detection of the first association on this socket */
4425 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4426 sksec->sctp_assoc_state = SCTP_ASSOC_UNSET;
4427
Paul Moore389fb8002009-03-27 17:10:34 -04004428 err = selinux_netlbl_socket_post_create(sock->sk, family);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004429 }
4430
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07004431 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432}
4433
David Herrmann0b811db2018-05-04 16:28:21 +02004434static int selinux_socket_socketpair(struct socket *socka,
4435 struct socket *sockb)
4436{
4437 struct sk_security_struct *sksec_a = socka->sk->sk_security;
4438 struct sk_security_struct *sksec_b = sockb->sk->sk_security;
4439
4440 sksec_a->peer_sid = sksec_b->sid;
4441 sksec_b->peer_sid = sksec_a->sid;
4442
4443 return 0;
4444}
4445
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446/* Range of port numbers used to automatically bind.
4447 Need to determine whether we should perform a name_bind
4448 permission check between the socket and the port number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449
4450static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
4451{
Paul Moore253bfae2010-04-22 14:46:19 -04004452 struct sock *sk = sock->sk;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004453 struct sk_security_struct *sksec = sk->sk_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 u16 family;
4455 int err;
4456
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004457 err = sock_has_perm(sk, SOCKET__BIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 if (err)
4459 goto out;
4460
Richard Hainesd4529302018-02-13 20:57:18 +00004461 /* If PF_INET or PF_INET6, check name_bind permission for the port. */
Paul Moore253bfae2010-04-22 14:46:19 -04004462 family = sk->sk_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 if (family == PF_INET || family == PF_INET6) {
4464 char *addrp;
Thomas Liu2bf49692009-07-14 12:14:09 -04004465 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004466 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467 struct sockaddr_in *addr4 = NULL;
4468 struct sockaddr_in6 *addr6 = NULL;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004469 u16 family_sa = address->sa_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 unsigned short snum;
James Morrise399f982008-06-12 01:39:58 +10004471 u32 sid, node_perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472
Richard Hainesd4529302018-02-13 20:57:18 +00004473 /*
4474 * sctp_bindx(3) calls via selinux_sctp_bind_connect()
4475 * that validates multiple binding addresses. Because of this
4476 * need to check address->sa_family as it is possible to have
4477 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4478 */
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004479 switch (family_sa) {
4480 case AF_UNSPEC:
Richard Haines68741a8a2018-03-02 19:54:34 +00004481 case AF_INET:
4482 if (addrlen < sizeof(struct sockaddr_in))
4483 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 addr4 = (struct sockaddr_in *)address;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004485 if (family_sa == AF_UNSPEC) {
4486 /* see __inet_bind(), we only want to allow
4487 * AF_UNSPEC if the address is INADDR_ANY
4488 */
4489 if (addr4->sin_addr.s_addr != htonl(INADDR_ANY))
4490 goto err_af;
4491 family_sa = AF_INET;
4492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 snum = ntohs(addr4->sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 addrp = (char *)&addr4->sin_addr.s_addr;
Richard Haines68741a8a2018-03-02 19:54:34 +00004495 break;
4496 case AF_INET6:
4497 if (addrlen < SIN6_LEN_RFC2133)
4498 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499 addr6 = (struct sockaddr_in6 *)address;
4500 snum = ntohs(addr6->sin6_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 addrp = (char *)&addr6->sin6_addr.s6_addr;
Richard Haines68741a8a2018-03-02 19:54:34 +00004502 break;
4503 default:
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004504 goto err_af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 }
4506
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004507 ad.type = LSM_AUDIT_DATA_NET;
4508 ad.u.net = &net;
4509 ad.u.net->sport = htons(snum);
4510 ad.u.net->family = family_sa;
4511
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004512 if (snum) {
4513 int low, high;
4514
Eric W. Biederman0bbf87d2013-09-28 14:10:59 -07004515 inet_get_local_port_range(sock_net(sk), &low, &high);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004516
Krister Johansen4548b682017-01-20 17:49:11 -08004517 if (snum < max(inet_prot_sock(sock_net(sk)), low) ||
4518 snum > high) {
Paul Moore3e112172008-04-10 10:48:14 -04004519 err = sel_netport_sid(sk->sk_protocol,
4520 snum, &sid);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004521 if (err)
4522 goto out;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004523 err = avc_has_perm(&selinux_state,
4524 sksec->sid, sid,
Paul Moore253bfae2010-04-22 14:46:19 -04004525 sksec->sclass,
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004526 SOCKET__NAME_BIND, &ad);
4527 if (err)
4528 goto out;
4529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 }
Eric Paris828dfe12008-04-17 13:17:49 -04004531
Paul Moore253bfae2010-04-22 14:46:19 -04004532 switch (sksec->sclass) {
James Morris13402582005-09-30 14:24:34 -04004533 case SECCLASS_TCP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 node_perm = TCP_SOCKET__NODE_BIND;
4535 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004536
James Morris13402582005-09-30 14:24:34 -04004537 case SECCLASS_UDP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 node_perm = UDP_SOCKET__NODE_BIND;
4539 break;
James Morris2ee92d42006-11-13 16:09:01 -08004540
4541 case SECCLASS_DCCP_SOCKET:
4542 node_perm = DCCP_SOCKET__NODE_BIND;
4543 break;
4544
Richard Hainesd4529302018-02-13 20:57:18 +00004545 case SECCLASS_SCTP_SOCKET:
4546 node_perm = SCTP_SOCKET__NODE_BIND;
4547 break;
4548
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549 default:
4550 node_perm = RAWIP_SOCKET__NODE_BIND;
4551 break;
4552 }
Eric Paris828dfe12008-04-17 13:17:49 -04004553
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004554 err = sel_netnode_sid(addrp, family_sa, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 if (err)
4556 goto out;
Eric Paris828dfe12008-04-17 13:17:49 -04004557
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004558 if (family_sa == AF_INET)
Eric Paris48c62af2012-04-02 13:15:44 -04004559 ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 else
Eric Paris48c62af2012-04-02 13:15:44 -04004561 ad.u.net->v6info.saddr = addr6->sin6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004563 err = avc_has_perm(&selinux_state,
4564 sksec->sid, sid,
Paul Moore253bfae2010-04-22 14:46:19 -04004565 sksec->sclass, node_perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 if (err)
4567 goto out;
4568 }
4569out:
4570 return err;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004571err_af:
4572 /* Note that SCTP services expect -EINVAL, others -EAFNOSUPPORT. */
4573 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4574 return -EINVAL;
4575 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576}
4577
Richard Hainesd4529302018-02-13 20:57:18 +00004578/* This supports connect(2) and SCTP connect services such as sctp_connectx(3)
Mauro Carvalho Chehab5fb94e92018-05-08 15:14:57 -03004579 * and sctp_sendmsg(3) as described in Documentation/security/LSM-sctp.rst
Richard Hainesd4529302018-02-13 20:57:18 +00004580 */
4581static int selinux_socket_connect_helper(struct socket *sock,
4582 struct sockaddr *address, int addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583{
Paul Moore014ab192008-10-10 10:16:33 -04004584 struct sock *sk = sock->sk;
Paul Moore253bfae2010-04-22 14:46:19 -04004585 struct sk_security_struct *sksec = sk->sk_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 int err;
4587
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004588 err = sock_has_perm(sk, SOCKET__CONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 if (err)
4590 return err;
4591
4592 /*
Richard Hainesd4529302018-02-13 20:57:18 +00004593 * If a TCP, DCCP or SCTP socket, check name_connect permission
4594 * for the port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 */
Paul Moore253bfae2010-04-22 14:46:19 -04004596 if (sksec->sclass == SECCLASS_TCP_SOCKET ||
Richard Hainesd4529302018-02-13 20:57:18 +00004597 sksec->sclass == SECCLASS_DCCP_SOCKET ||
4598 sksec->sclass == SECCLASS_SCTP_SOCKET) {
Thomas Liu2bf49692009-07-14 12:14:09 -04004599 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004600 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601 struct sockaddr_in *addr4 = NULL;
4602 struct sockaddr_in6 *addr6 = NULL;
4603 unsigned short snum;
James Morris2ee92d42006-11-13 16:09:01 -08004604 u32 sid, perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605
Richard Hainesd4529302018-02-13 20:57:18 +00004606 /* sctp_connectx(3) calls via selinux_sctp_bind_connect()
4607 * that validates multiple connect addresses. Because of this
4608 * need to check address->sa_family as it is possible to have
4609 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4610 */
Richard Haines68741a8a2018-03-02 19:54:34 +00004611 switch (address->sa_family) {
4612 case AF_INET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 addr4 = (struct sockaddr_in *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004614 if (addrlen < sizeof(struct sockaddr_in))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 return -EINVAL;
4616 snum = ntohs(addr4->sin_port);
Richard Haines68741a8a2018-03-02 19:54:34 +00004617 break;
4618 case AF_INET6:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 addr6 = (struct sockaddr_in6 *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004620 if (addrlen < SIN6_LEN_RFC2133)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 return -EINVAL;
4622 snum = ntohs(addr6->sin6_port);
Richard Haines68741a8a2018-03-02 19:54:34 +00004623 break;
4624 default:
4625 /* Note that SCTP services expect -EINVAL, whereas
4626 * others expect -EAFNOSUPPORT.
4627 */
4628 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4629 return -EINVAL;
4630 else
4631 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632 }
4633
Paul Moore3e112172008-04-10 10:48:14 -04004634 err = sel_netport_sid(sk->sk_protocol, snum, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635 if (err)
Richard Hainesd4529302018-02-13 20:57:18 +00004636 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637
Richard Hainesd4529302018-02-13 20:57:18 +00004638 switch (sksec->sclass) {
4639 case SECCLASS_TCP_SOCKET:
4640 perm = TCP_SOCKET__NAME_CONNECT;
4641 break;
4642 case SECCLASS_DCCP_SOCKET:
4643 perm = DCCP_SOCKET__NAME_CONNECT;
4644 break;
4645 case SECCLASS_SCTP_SOCKET:
4646 perm = SCTP_SOCKET__NAME_CONNECT;
4647 break;
4648 }
James Morris2ee92d42006-11-13 16:09:01 -08004649
Eric Paris50c205f2012-04-04 15:01:43 -04004650 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004651 ad.u.net = &net;
4652 ad.u.net->dport = htons(snum);
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004653 ad.u.net->family = address->sa_family;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004654 err = avc_has_perm(&selinux_state,
4655 sksec->sid, sid, sksec->sclass, perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 if (err)
Richard Hainesd4529302018-02-13 20:57:18 +00004657 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658 }
4659
Richard Hainesd4529302018-02-13 20:57:18 +00004660 return 0;
4661}
Paul Moore014ab192008-10-10 10:16:33 -04004662
Richard Hainesd4529302018-02-13 20:57:18 +00004663/* Supports connect(2), see comments in selinux_socket_connect_helper() */
4664static int selinux_socket_connect(struct socket *sock,
4665 struct sockaddr *address, int addrlen)
4666{
4667 int err;
4668 struct sock *sk = sock->sk;
4669
4670 err = selinux_socket_connect_helper(sock, address, addrlen);
4671 if (err)
4672 return err;
4673
4674 return selinux_netlbl_socket_connect(sk, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675}
4676
4677static int selinux_socket_listen(struct socket *sock, int backlog)
4678{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004679 return sock_has_perm(sock->sk, SOCKET__LISTEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680}
4681
4682static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
4683{
4684 int err;
4685 struct inode_security_struct *isec;
4686 struct inode_security_struct *newisec;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004687 u16 sclass;
4688 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004690 err = sock_has_perm(sock->sk, SOCKET__ACCEPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 if (err)
4692 return err;
4693
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05004694 isec = inode_security_novalidate(SOCK_INODE(sock));
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004695 spin_lock(&isec->lock);
4696 sclass = isec->sclass;
4697 sid = isec->sid;
4698 spin_unlock(&isec->lock);
4699
4700 newisec = inode_security_novalidate(SOCK_INODE(newsock));
4701 newisec->sclass = sclass;
4702 newisec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004703 newisec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704
4705 return 0;
4706}
4707
4708static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
Eric Paris828dfe12008-04-17 13:17:49 -04004709 int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004711 return sock_has_perm(sock->sk, SOCKET__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712}
4713
4714static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
4715 int size, int flags)
4716{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004717 return sock_has_perm(sock->sk, SOCKET__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718}
4719
4720static int selinux_socket_getsockname(struct socket *sock)
4721{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004722 return sock_has_perm(sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723}
4724
4725static int selinux_socket_getpeername(struct socket *sock)
4726{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004727 return sock_has_perm(sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728}
4729
Eric Paris828dfe12008-04-17 13:17:49 -04004730static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731{
Paul Mooref8687af2006-10-30 15:22:15 -08004732 int err;
4733
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004734 err = sock_has_perm(sock->sk, SOCKET__SETOPT);
Paul Mooref8687af2006-10-30 15:22:15 -08004735 if (err)
4736 return err;
4737
4738 return selinux_netlbl_socket_setsockopt(sock, level, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739}
4740
4741static int selinux_socket_getsockopt(struct socket *sock, int level,
4742 int optname)
4743{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004744 return sock_has_perm(sock->sk, SOCKET__GETOPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745}
4746
4747static int selinux_socket_shutdown(struct socket *sock, int how)
4748{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004749 return sock_has_perm(sock->sk, SOCKET__SHUTDOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750}
4751
David S. Miller3610cda2011-01-05 15:38:53 -08004752static int selinux_socket_unix_stream_connect(struct sock *sock,
4753 struct sock *other,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754 struct sock *newsk)
4755{
David S. Miller3610cda2011-01-05 15:38:53 -08004756 struct sk_security_struct *sksec_sock = sock->sk_security;
4757 struct sk_security_struct *sksec_other = other->sk_security;
Paul Moore4d1e2452010-04-22 14:46:18 -04004758 struct sk_security_struct *sksec_new = newsk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004759 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004760 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761 int err;
4762
Eric Paris50c205f2012-04-04 15:01:43 -04004763 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004764 ad.u.net = &net;
4765 ad.u.net->sk = other;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004767 err = avc_has_perm(&selinux_state,
4768 sksec_sock->sid, sksec_other->sid,
Paul Moore4d1e2452010-04-22 14:46:18 -04004769 sksec_other->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770 UNIX_STREAM_SOCKET__CONNECTTO, &ad);
4771 if (err)
4772 return err;
4773
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774 /* server child socket */
Paul Moore4d1e2452010-04-22 14:46:18 -04004775 sksec_new->peer_sid = sksec_sock->sid;
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004776 err = security_sid_mls_copy(&selinux_state, sksec_other->sid,
4777 sksec_sock->sid, &sksec_new->sid);
Paul Moore4d1e2452010-04-22 14:46:18 -04004778 if (err)
4779 return err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004780
Paul Moore4d1e2452010-04-22 14:46:18 -04004781 /* connecting socket */
4782 sksec_sock->peer_sid = sksec_new->sid;
4783
4784 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785}
4786
4787static int selinux_socket_unix_may_send(struct socket *sock,
4788 struct socket *other)
4789{
Paul Moore253bfae2010-04-22 14:46:19 -04004790 struct sk_security_struct *ssec = sock->sk->sk_security;
4791 struct sk_security_struct *osec = other->sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004792 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004793 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794
Eric Paris50c205f2012-04-04 15:01:43 -04004795 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004796 ad.u.net = &net;
4797 ad.u.net->sk = other->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004799 return avc_has_perm(&selinux_state,
4800 ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
Paul Moore253bfae2010-04-22 14:46:19 -04004801 &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802}
4803
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004804static int selinux_inet_sys_rcv_skb(struct net *ns, int ifindex,
4805 char *addrp, u16 family, u32 peer_sid,
Thomas Liu2bf49692009-07-14 12:14:09 -04004806 struct common_audit_data *ad)
Paul Mooreeffad8d2008-01-29 08:49:27 -05004807{
4808 int err;
4809 u32 if_sid;
4810 u32 node_sid;
4811
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004812 err = sel_netif_sid(ns, ifindex, &if_sid);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004813 if (err)
4814 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004815 err = avc_has_perm(&selinux_state,
4816 peer_sid, if_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004817 SECCLASS_NETIF, NETIF__INGRESS, ad);
4818 if (err)
4819 return err;
4820
4821 err = sel_netnode_sid(addrp, family, &node_sid);
4822 if (err)
4823 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004824 return avc_has_perm(&selinux_state,
4825 peer_sid, node_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004826 SECCLASS_NODE, NODE__RECVFROM, ad);
4827}
4828
Paul Moore220deb92008-01-29 08:38:23 -05004829static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
Paul Moored8395c82008-10-10 10:16:30 -04004830 u16 family)
Paul Moore220deb92008-01-29 08:38:23 -05004831{
Paul Moore277d3422008-12-31 12:54:11 -05004832 int err = 0;
Paul Moore220deb92008-01-29 08:38:23 -05004833 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004834 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004835 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004836 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04004837 char *addrp;
4838
Eric Paris50c205f2012-04-04 15:01:43 -04004839 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004840 ad.u.net = &net;
4841 ad.u.net->netif = skb->skb_iif;
4842 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004843 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4844 if (err)
4845 return err;
Paul Moore220deb92008-01-29 08:38:23 -05004846
Paul Moore58bfbb52009-03-27 17:10:41 -04004847 if (selinux_secmark_enabled()) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004848 err = avc_has_perm(&selinux_state,
4849 sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Moored8395c82008-10-10 10:16:30 -04004850 PACKET__RECV, &ad);
Paul Moore58bfbb52009-03-27 17:10:41 -04004851 if (err)
4852 return err;
4853 }
Paul Moore220deb92008-01-29 08:38:23 -05004854
Steffen Klassertb9679a72011-02-23 12:55:21 +01004855 err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
4856 if (err)
4857 return err;
4858 err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004859
James Morris4e5ab4c2006-06-09 00:33:33 -07004860 return err;
4861}
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004862
James Morris4e5ab4c2006-06-09 00:33:33 -07004863static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
4864{
Paul Moore220deb92008-01-29 08:38:23 -05004865 int err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004866 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004867 u16 family = sk->sk_family;
4868 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004869 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004870 struct lsm_network_audit net = {0,};
Paul Moore220deb92008-01-29 08:38:23 -05004871 char *addrp;
Paul Moored8395c82008-10-10 10:16:30 -04004872 u8 secmark_active;
4873 u8 peerlbl_active;
James Morris4e5ab4c2006-06-09 00:33:33 -07004874
James Morris4e5ab4c2006-06-09 00:33:33 -07004875 if (family != PF_INET && family != PF_INET6)
Paul Moore220deb92008-01-29 08:38:23 -05004876 return 0;
James Morris4e5ab4c2006-06-09 00:33:33 -07004877
4878 /* Handle mapped IPv4 packets arriving via IPv6 sockets */
Al Viro87fcd702006-12-04 22:00:55 +00004879 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
James Morris4e5ab4c2006-06-09 00:33:33 -07004880 family = PF_INET;
4881
Paul Moored8395c82008-10-10 10:16:30 -04004882 /* If any sort of compatibility mode is enabled then handoff processing
4883 * to the selinux_sock_rcv_skb_compat() function to deal with the
4884 * special handling. We do this in an attempt to keep this function
4885 * as fast and as clean as possible. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004886 if (!selinux_policycap_netpeer())
Paul Moored8395c82008-10-10 10:16:30 -04004887 return selinux_sock_rcv_skb_compat(sk, skb, family);
4888
4889 secmark_active = selinux_secmark_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04004890 peerlbl_active = selinux_peerlbl_enabled();
Paul Moored8395c82008-10-10 10:16:30 -04004891 if (!secmark_active && !peerlbl_active)
4892 return 0;
4893
Eric Paris50c205f2012-04-04 15:01:43 -04004894 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004895 ad.u.net = &net;
4896 ad.u.net->netif = skb->skb_iif;
4897 ad.u.net->family = family;
Paul Moore224dfbd2008-01-29 08:38:13 -05004898 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
James Morris4e5ab4c2006-06-09 00:33:33 -07004899 if (err)
Paul Moore220deb92008-01-29 08:38:23 -05004900 return err;
James Morris4e5ab4c2006-06-09 00:33:33 -07004901
Paul Moored8395c82008-10-10 10:16:30 -04004902 if (peerlbl_active) {
Paul Moored621d352008-01-29 08:43:36 -05004903 u32 peer_sid;
4904
4905 err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
4906 if (err)
4907 return err;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004908 err = selinux_inet_sys_rcv_skb(sock_net(sk), skb->skb_iif,
4909 addrp, family, peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04004910 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04004911 selinux_netlbl_err(skb, family, err, 0);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004912 return err;
Paul Mooredfaebe92008-10-10 10:16:31 -04004913 }
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004914 err = avc_has_perm(&selinux_state,
4915 sk_sid, peer_sid, SECCLASS_PEER,
Paul Moored621d352008-01-29 08:43:36 -05004916 PEER__RECV, &ad);
Chad Hanson46d01d62013-12-23 17:45:01 -05004917 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04004918 selinux_netlbl_err(skb, family, err, 0);
Chad Hanson46d01d62013-12-23 17:45:01 -05004919 return err;
4920 }
Paul Moored621d352008-01-29 08:43:36 -05004921 }
4922
Paul Moored8395c82008-10-10 10:16:30 -04004923 if (secmark_active) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004924 err = avc_has_perm(&selinux_state,
4925 sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004926 PACKET__RECV, &ad);
4927 if (err)
4928 return err;
4929 }
4930
Paul Moored621d352008-01-29 08:43:36 -05004931 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932}
4933
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004934static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
4935 int __user *optlen, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936{
4937 int err = 0;
4938 char *scontext;
4939 u32 scontext_len;
Paul Moore253bfae2010-04-22 14:46:19 -04004940 struct sk_security_struct *sksec = sock->sk->sk_security;
Paul Moore3de4bab2006-11-17 17:38:54 -05004941 u32 peer_sid = SECSID_NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942
Paul Moore253bfae2010-04-22 14:46:19 -04004943 if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
Richard Hainesd4529302018-02-13 20:57:18 +00004944 sksec->sclass == SECCLASS_TCP_SOCKET ||
4945 sksec->sclass == SECCLASS_SCTP_SOCKET)
Eric Parisdd3e7832010-04-07 15:08:46 -04004946 peer_sid = sksec->peer_sid;
Paul Moore253bfae2010-04-22 14:46:19 -04004947 if (peer_sid == SECSID_NULL)
4948 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004950 err = security_sid_to_context(&selinux_state, peer_sid, &scontext,
4951 &scontext_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952 if (err)
Paul Moore253bfae2010-04-22 14:46:19 -04004953 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954
4955 if (scontext_len > len) {
4956 err = -ERANGE;
4957 goto out_len;
4958 }
4959
4960 if (copy_to_user(optval, scontext, scontext_len))
4961 err = -EFAULT;
4962
4963out_len:
4964 if (put_user(scontext_len, optlen))
4965 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966 kfree(scontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 return err;
4968}
4969
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004970static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004971{
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004972 u32 peer_secid = SECSID_NULL;
Paul Moore75e22912008-01-29 08:38:04 -05004973 u16 family;
Paul Moore899134f2016-03-28 15:19:10 -04004974 struct inode_security_struct *isec;
Catherine Zhang877ce7c2006-06-29 12:27:47 -07004975
Paul Mooreaa862902008-10-10 10:16:29 -04004976 if (skb && skb->protocol == htons(ETH_P_IP))
4977 family = PF_INET;
4978 else if (skb && skb->protocol == htons(ETH_P_IPV6))
4979 family = PF_INET6;
4980 else if (sock)
Paul Moore75e22912008-01-29 08:38:04 -05004981 family = sock->sk->sk_family;
Paul Moore75e22912008-01-29 08:38:04 -05004982 else
4983 goto out;
4984
Paul Moore899134f2016-03-28 15:19:10 -04004985 if (sock && family == PF_UNIX) {
4986 isec = inode_security_novalidate(SOCK_INODE(sock));
4987 peer_secid = isec->sid;
4988 } else if (skb)
Paul Moore220deb92008-01-29 08:38:23 -05004989 selinux_skb_peerlbl_sid(skb, family, &peer_secid);
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004990
Paul Moore75e22912008-01-29 08:38:04 -05004991out:
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004992 *secid = peer_secid;
Paul Moore75e22912008-01-29 08:38:04 -05004993 if (peer_secid == SECSID_NULL)
4994 return -EINVAL;
4995 return 0;
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004996}
4997
Al Viro7d877f32005-10-21 03:20:43 -04004998static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999{
Paul Moore84914b72010-04-22 14:46:18 -04005000 struct sk_security_struct *sksec;
5001
5002 sksec = kzalloc(sizeof(*sksec), priority);
5003 if (!sksec)
5004 return -ENOMEM;
5005
5006 sksec->peer_sid = SECINITSID_UNLABELED;
5007 sksec->sid = SECINITSID_UNLABELED;
Stephen Smalley5dee25d2015-07-10 17:19:57 -04005008 sksec->sclass = SECCLASS_SOCKET;
Paul Moore84914b72010-04-22 14:46:18 -04005009 selinux_netlbl_sk_security_reset(sksec);
5010 sk->sk_security = sksec;
5011
5012 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013}
5014
5015static void selinux_sk_free_security(struct sock *sk)
5016{
Paul Moore84914b72010-04-22 14:46:18 -04005017 struct sk_security_struct *sksec = sk->sk_security;
5018
5019 sk->sk_security = NULL;
5020 selinux_netlbl_sk_security_free(sksec);
5021 kfree(sksec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022}
5023
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005024static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
5025{
Eric Parisdd3e7832010-04-07 15:08:46 -04005026 struct sk_security_struct *sksec = sk->sk_security;
5027 struct sk_security_struct *newsksec = newsk->sk_security;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005028
Eric Parisdd3e7832010-04-07 15:08:46 -04005029 newsksec->sid = sksec->sid;
5030 newsksec->peer_sid = sksec->peer_sid;
5031 newsksec->sclass = sksec->sclass;
Paul Moore99f59ed2006-08-29 17:53:48 -07005032
Eric Parisdd3e7832010-04-07 15:08:46 -04005033 selinux_netlbl_sk_security_reset(newsksec);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005034}
5035
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07005036static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005037{
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005038 if (!sk)
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07005039 *secid = SECINITSID_ANY_SOCKET;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005040 else {
5041 struct sk_security_struct *sksec = sk->sk_security;
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005042
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07005043 *secid = sksec->sid;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005044 }
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005045}
5046
Eric Paris828dfe12008-04-17 13:17:49 -04005047static void selinux_sock_graft(struct sock *sk, struct socket *parent)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005048{
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05005049 struct inode_security_struct *isec =
5050 inode_security_novalidate(SOCK_INODE(parent));
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005051 struct sk_security_struct *sksec = sk->sk_security;
5052
Paul Moore2873ead2014-07-28 10:42:48 -04005053 if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
5054 sk->sk_family == PF_UNIX)
David Woodhouse2148ccc2006-09-29 15:50:25 -07005055 isec->sid = sksec->sid;
Paul Moore220deb92008-01-29 08:38:23 -05005056 sksec->sclass = isec->sclass;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005057}
5058
Richard Hainesd4529302018-02-13 20:57:18 +00005059/* Called whenever SCTP receives an INIT chunk. This happens when an incoming
5060 * connect(2), sctp_connectx(3) or sctp_sendmsg(3) (with no association
5061 * already present).
5062 */
5063static int selinux_sctp_assoc_request(struct sctp_endpoint *ep,
5064 struct sk_buff *skb)
5065{
5066 struct sk_security_struct *sksec = ep->base.sk->sk_security;
5067 struct common_audit_data ad;
5068 struct lsm_network_audit net = {0,};
5069 u8 peerlbl_active;
5070 u32 peer_sid = SECINITSID_UNLABELED;
5071 u32 conn_sid;
5072 int err = 0;
5073
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005074 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005075 return 0;
5076
5077 peerlbl_active = selinux_peerlbl_enabled();
5078
5079 if (peerlbl_active) {
5080 /* This will return peer_sid = SECSID_NULL if there are
5081 * no peer labels, see security_net_peersid_resolve().
5082 */
5083 err = selinux_skb_peerlbl_sid(skb, ep->base.sk->sk_family,
5084 &peer_sid);
5085 if (err)
5086 return err;
5087
5088 if (peer_sid == SECSID_NULL)
5089 peer_sid = SECINITSID_UNLABELED;
5090 }
5091
5092 if (sksec->sctp_assoc_state == SCTP_ASSOC_UNSET) {
5093 sksec->sctp_assoc_state = SCTP_ASSOC_SET;
5094
5095 /* Here as first association on socket. As the peer SID
5096 * was allowed by peer recv (and the netif/node checks),
5097 * then it is approved by policy and used as the primary
5098 * peer SID for getpeercon(3).
5099 */
5100 sksec->peer_sid = peer_sid;
5101 } else if (sksec->peer_sid != peer_sid) {
5102 /* Other association peer SIDs are checked to enforce
5103 * consistency among the peer SIDs.
5104 */
5105 ad.type = LSM_AUDIT_DATA_NET;
5106 ad.u.net = &net;
5107 ad.u.net->sk = ep->base.sk;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005108 err = avc_has_perm(&selinux_state,
5109 sksec->peer_sid, peer_sid, sksec->sclass,
Richard Hainesd4529302018-02-13 20:57:18 +00005110 SCTP_SOCKET__ASSOCIATION, &ad);
5111 if (err)
5112 return err;
5113 }
5114
5115 /* Compute the MLS component for the connection and store
5116 * the information in ep. This will be used by SCTP TCP type
5117 * sockets and peeled off connections as they cause a new
5118 * socket to be generated. selinux_sctp_sk_clone() will then
5119 * plug this into the new socket.
5120 */
5121 err = selinux_conn_sid(sksec->sid, peer_sid, &conn_sid);
5122 if (err)
5123 return err;
5124
5125 ep->secid = conn_sid;
5126 ep->peer_secid = peer_sid;
5127
5128 /* Set any NetLabel labels including CIPSO/CALIPSO options. */
5129 return selinux_netlbl_sctp_assoc_request(ep, skb);
5130}
5131
5132/* Check if sctp IPv4/IPv6 addresses are valid for binding or connecting
5133 * based on their @optname.
5134 */
5135static int selinux_sctp_bind_connect(struct sock *sk, int optname,
5136 struct sockaddr *address,
5137 int addrlen)
5138{
5139 int len, err = 0, walk_size = 0;
5140 void *addr_buf;
5141 struct sockaddr *addr;
5142 struct socket *sock;
5143
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005144 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005145 return 0;
5146
5147 /* Process one or more addresses that may be IPv4 or IPv6 */
5148 sock = sk->sk_socket;
5149 addr_buf = address;
5150
5151 while (walk_size < addrlen) {
5152 addr = addr_buf;
5153 switch (addr->sa_family) {
Alexey Kodanev4152dc92018-05-11 20:15:13 +03005154 case AF_UNSPEC:
Richard Hainesd4529302018-02-13 20:57:18 +00005155 case AF_INET:
5156 len = sizeof(struct sockaddr_in);
5157 break;
5158 case AF_INET6:
5159 len = sizeof(struct sockaddr_in6);
5160 break;
5161 default:
Alexey Kodanev4152dc92018-05-11 20:15:13 +03005162 return -EINVAL;
Richard Hainesd4529302018-02-13 20:57:18 +00005163 }
5164
5165 err = -EINVAL;
5166 switch (optname) {
5167 /* Bind checks */
5168 case SCTP_PRIMARY_ADDR:
5169 case SCTP_SET_PEER_PRIMARY_ADDR:
5170 case SCTP_SOCKOPT_BINDX_ADD:
5171 err = selinux_socket_bind(sock, addr, len);
5172 break;
5173 /* Connect checks */
5174 case SCTP_SOCKOPT_CONNECTX:
5175 case SCTP_PARAM_SET_PRIMARY:
5176 case SCTP_PARAM_ADD_IP:
5177 case SCTP_SENDMSG_CONNECT:
5178 err = selinux_socket_connect_helper(sock, addr, len);
5179 if (err)
5180 return err;
5181
5182 /* As selinux_sctp_bind_connect() is called by the
5183 * SCTP protocol layer, the socket is already locked,
5184 * therefore selinux_netlbl_socket_connect_locked() is
5185 * is called here. The situations handled are:
5186 * sctp_connectx(3), sctp_sendmsg(3), sendmsg(2),
5187 * whenever a new IP address is added or when a new
5188 * primary address is selected.
5189 * Note that an SCTP connect(2) call happens before
5190 * the SCTP protocol layer and is handled via
5191 * selinux_socket_connect().
5192 */
5193 err = selinux_netlbl_socket_connect_locked(sk, addr);
5194 break;
5195 }
5196
5197 if (err)
5198 return err;
5199
5200 addr_buf += len;
5201 walk_size += len;
5202 }
5203
5204 return 0;
5205}
5206
5207/* Called whenever a new socket is created by accept(2) or sctp_peeloff(3). */
5208static void selinux_sctp_sk_clone(struct sctp_endpoint *ep, struct sock *sk,
5209 struct sock *newsk)
5210{
5211 struct sk_security_struct *sksec = sk->sk_security;
5212 struct sk_security_struct *newsksec = newsk->sk_security;
5213
5214 /* If policy does not support SECCLASS_SCTP_SOCKET then call
5215 * the non-sctp clone version.
5216 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005217 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005218 return selinux_sk_clone_security(sk, newsk);
5219
5220 newsksec->sid = ep->secid;
5221 newsksec->peer_sid = ep->peer_secid;
5222 newsksec->sclass = sksec->sclass;
5223 selinux_netlbl_sctp_sk_clone(sk, newsk);
5224}
5225
Adrian Bunk9a673e52006-08-15 00:03:53 -07005226static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
5227 struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005228{
5229 struct sk_security_struct *sksec = sk->sk_security;
5230 int err;
Paul Moore0b1f24e2013-12-03 11:39:13 -05005231 u16 family = req->rsk_ops->family;
Paul Moore446b8022013-12-04 16:10:51 -05005232 u32 connsid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005233 u32 peersid;
5234
Paul Mooreaa862902008-10-10 10:16:29 -04005235 err = selinux_skb_peerlbl_sid(skb, family, &peersid);
Paul Moore220deb92008-01-29 08:38:23 -05005236 if (err)
5237 return err;
Paul Moore446b8022013-12-04 16:10:51 -05005238 err = selinux_conn_sid(sksec->sid, peersid, &connsid);
5239 if (err)
5240 return err;
5241 req->secid = connsid;
5242 req->peer_secid = peersid;
Venkat Yekkiralaa51c64f2006-07-27 22:01:34 -07005243
Paul Moore389fb8002009-03-27 17:10:34 -04005244 return selinux_netlbl_inet_conn_request(req, family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005245}
5246
Adrian Bunk9a673e52006-08-15 00:03:53 -07005247static void selinux_inet_csk_clone(struct sock *newsk,
5248 const struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005249{
5250 struct sk_security_struct *newsksec = newsk->sk_security;
5251
5252 newsksec->sid = req->secid;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005253 newsksec->peer_sid = req->peer_secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005254 /* NOTE: Ideally, we should also get the isec->sid for the
5255 new socket in sync, but we don't have the isec available yet.
5256 So we will wait until sock_graft to do it, by which
5257 time it will have been created and available. */
Paul Moore99f59ed2006-08-29 17:53:48 -07005258
Paul Moore9f2ad662006-11-17 17:38:53 -05005259 /* We don't need to take any sort of lock here as we are the only
5260 * thread with access to newsksec */
Paul Moore389fb8002009-03-27 17:10:34 -04005261 selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005262}
5263
Paul Moore014ab192008-10-10 10:16:33 -04005264static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005265{
Paul Mooreaa862902008-10-10 10:16:29 -04005266 u16 family = sk->sk_family;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005267 struct sk_security_struct *sksec = sk->sk_security;
5268
Paul Mooreaa862902008-10-10 10:16:29 -04005269 /* handle mapped IPv4 packets arriving via IPv6 sockets */
5270 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
5271 family = PF_INET;
5272
5273 selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005274}
5275
Eric Paris2606fd12010-10-13 16:24:41 -04005276static int selinux_secmark_relabel_packet(u32 sid)
5277{
5278 const struct task_security_struct *__tsec;
5279 u32 tsid;
5280
5281 __tsec = current_security();
5282 tsid = __tsec->sid;
5283
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005284 return avc_has_perm(&selinux_state,
5285 tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO,
5286 NULL);
Eric Paris2606fd12010-10-13 16:24:41 -04005287}
5288
5289static void selinux_secmark_refcount_inc(void)
5290{
5291 atomic_inc(&selinux_secmark_refcount);
5292}
5293
5294static void selinux_secmark_refcount_dec(void)
5295{
5296 atomic_dec(&selinux_secmark_refcount);
5297}
5298
Adrian Bunk9a673e52006-08-15 00:03:53 -07005299static void selinux_req_classify_flow(const struct request_sock *req,
5300 struct flowi *fl)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005301{
David S. Miller1d28f422011-03-12 00:29:39 -05005302 fl->flowi_secid = req->secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005303}
5304
Paul Moore5dbbaf22013-01-14 07:12:19 +00005305static int selinux_tun_dev_alloc_security(void **security)
5306{
5307 struct tun_security_struct *tunsec;
5308
5309 tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL);
5310 if (!tunsec)
5311 return -ENOMEM;
5312 tunsec->sid = current_sid();
5313
5314 *security = tunsec;
5315 return 0;
5316}
5317
5318static void selinux_tun_dev_free_security(void *security)
5319{
5320 kfree(security);
5321}
5322
Paul Mooreed6d76e2009-08-28 18:12:49 -04005323static int selinux_tun_dev_create(void)
5324{
5325 u32 sid = current_sid();
5326
5327 /* we aren't taking into account the "sockcreate" SID since the socket
5328 * that is being created here is not a socket in the traditional sense,
5329 * instead it is a private sock, accessible only to the kernel, and
5330 * representing a wide range of network traffic spanning multiple
5331 * connections unlike traditional sockets - check the TUN driver to
5332 * get a better understanding of why this socket is special */
5333
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005334 return avc_has_perm(&selinux_state,
5335 sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005336 NULL);
5337}
5338
Paul Moore5dbbaf22013-01-14 07:12:19 +00005339static int selinux_tun_dev_attach_queue(void *security)
Paul Mooreed6d76e2009-08-28 18:12:49 -04005340{
Paul Moore5dbbaf22013-01-14 07:12:19 +00005341 struct tun_security_struct *tunsec = security;
5342
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005343 return avc_has_perm(&selinux_state,
5344 current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET,
Paul Moore5dbbaf22013-01-14 07:12:19 +00005345 TUN_SOCKET__ATTACH_QUEUE, NULL);
5346}
5347
5348static int selinux_tun_dev_attach(struct sock *sk, void *security)
5349{
5350 struct tun_security_struct *tunsec = security;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005351 struct sk_security_struct *sksec = sk->sk_security;
5352
5353 /* we don't currently perform any NetLabel based labeling here and it
5354 * isn't clear that we would want to do so anyway; while we could apply
5355 * labeling without the support of the TUN user the resulting labeled
5356 * traffic from the other end of the connection would almost certainly
5357 * cause confusion to the TUN user that had no idea network labeling
5358 * protocols were being used */
5359
Paul Moore5dbbaf22013-01-14 07:12:19 +00005360 sksec->sid = tunsec->sid;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005361 sksec->sclass = SECCLASS_TUN_SOCKET;
Paul Moore5dbbaf22013-01-14 07:12:19 +00005362
5363 return 0;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005364}
5365
Paul Moore5dbbaf22013-01-14 07:12:19 +00005366static int selinux_tun_dev_open(void *security)
Paul Mooreed6d76e2009-08-28 18:12:49 -04005367{
Paul Moore5dbbaf22013-01-14 07:12:19 +00005368 struct tun_security_struct *tunsec = security;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005369 u32 sid = current_sid();
5370 int err;
5371
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005372 err = avc_has_perm(&selinux_state,
5373 sid, tunsec->sid, SECCLASS_TUN_SOCKET,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005374 TUN_SOCKET__RELABELFROM, NULL);
5375 if (err)
5376 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005377 err = avc_has_perm(&selinux_state,
5378 sid, sid, SECCLASS_TUN_SOCKET,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005379 TUN_SOCKET__RELABELTO, NULL);
5380 if (err)
5381 return err;
Paul Moore5dbbaf22013-01-14 07:12:19 +00005382 tunsec->sid = sid;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005383
5384 return 0;
5385}
5386
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
5388{
5389 int err = 0;
5390 u32 perm;
5391 struct nlmsghdr *nlh;
Paul Moore253bfae2010-04-22 14:46:19 -04005392 struct sk_security_struct *sksec = sk->sk_security;
Eric Paris828dfe12008-04-17 13:17:49 -04005393
Hong zhi guo77954982013-03-27 06:49:35 +00005394 if (skb->len < NLMSG_HDRLEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395 err = -EINVAL;
5396 goto out;
5397 }
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -07005398 nlh = nlmsg_hdr(skb);
Eric Paris828dfe12008-04-17 13:17:49 -04005399
Paul Moore253bfae2010-04-22 14:46:19 -04005400 err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401 if (err) {
5402 if (err == -EINVAL) {
Vladis Dronov76319942015-12-24 11:09:41 -05005403 pr_warn_ratelimited("SELinux: unrecognized netlink"
5404 " message: protocol=%hu nlmsg_type=%hu sclass=%s"
5405 " pig=%d comm=%s\n",
Marek Milkoviccded3ff2015-06-04 16:22:16 -04005406 sk->sk_protocol, nlh->nlmsg_type,
Vladis Dronov76319942015-12-24 11:09:41 -05005407 secclass_map[sksec->sclass - 1].name,
5408 task_pid_nr(current), current->comm);
Paul Mooree5a5ca92018-03-01 17:38:30 -05005409 if (!enforcing_enabled(&selinux_state) ||
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005410 security_get_allow_unknown(&selinux_state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411 err = 0;
5412 }
5413
5414 /* Ignore */
5415 if (err == -ENOENT)
5416 err = 0;
5417 goto out;
5418 }
5419
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005420 err = sock_has_perm(sk, perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421out:
5422 return err;
5423}
5424
5425#ifdef CONFIG_NETFILTER
5426
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005427static unsigned int selinux_ip_forward(struct sk_buff *skb,
5428 const struct net_device *indev,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005429 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430{
Paul Mooredfaebe92008-10-10 10:16:31 -04005431 int err;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005432 char *addrp;
5433 u32 peer_sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04005434 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005435 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05005436 u8 secmark_active;
Paul Moore948bf852008-10-10 10:16:32 -04005437 u8 netlbl_active;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005438 u8 peerlbl_active;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005439
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005440 if (!selinux_policycap_netpeer())
Paul Mooreeffad8d2008-01-29 08:49:27 -05005441 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005442
Paul Mooreeffad8d2008-01-29 08:49:27 -05005443 secmark_active = selinux_secmark_enabled();
Paul Moore948bf852008-10-10 10:16:32 -04005444 netlbl_active = netlbl_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04005445 peerlbl_active = selinux_peerlbl_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05005446 if (!secmark_active && !peerlbl_active)
5447 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005448
Paul Moored8395c82008-10-10 10:16:30 -04005449 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
5450 return NF_DROP;
5451
Eric Paris50c205f2012-04-04 15:01:43 -04005452 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005453 ad.u.net = &net;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005454 ad.u.net->netif = indev->ifindex;
Eric Paris48c62af2012-04-02 13:15:44 -04005455 ad.u.net->family = family;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005456 if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
5457 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458
Paul Mooredfaebe92008-10-10 10:16:31 -04005459 if (peerlbl_active) {
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005460 err = selinux_inet_sys_rcv_skb(dev_net(indev), indev->ifindex,
5461 addrp, family, peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04005462 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04005463 selinux_netlbl_err(skb, family, err, 1);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005464 return NF_DROP;
Paul Mooredfaebe92008-10-10 10:16:31 -04005465 }
5466 }
Paul Mooreeffad8d2008-01-29 08:49:27 -05005467
5468 if (secmark_active)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005469 if (avc_has_perm(&selinux_state,
5470 peer_sid, skb->secmark,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005471 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
5472 return NF_DROP;
5473
Paul Moore948bf852008-10-10 10:16:32 -04005474 if (netlbl_active)
5475 /* we do this in the FORWARD path and not the POST_ROUTING
5476 * path because we want to make sure we apply the necessary
5477 * labeling before IPsec is applied so we can leverage AH
5478 * protection */
5479 if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
5480 return NF_DROP;
5481
Paul Mooreeffad8d2008-01-29 08:49:27 -05005482 return NF_ACCEPT;
5483}
5484
Eric W. Biederman06198b32015-09-18 14:33:06 -05005485static unsigned int selinux_ipv4_forward(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005486 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005487 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005488{
David S. Miller238e54c2015-04-03 20:32:56 -04005489 return selinux_ip_forward(skb, state->in, PF_INET);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005490}
5491
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005492#if IS_ENABLED(CONFIG_IPV6)
Eric W. Biederman06198b32015-09-18 14:33:06 -05005493static unsigned int selinux_ipv6_forward(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005494 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005495 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005496{
David S. Miller238e54c2015-04-03 20:32:56 -04005497 return selinux_ip_forward(skb, state->in, PF_INET6);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005498}
5499#endif /* IPV6 */
5500
Paul Moore948bf852008-10-10 10:16:32 -04005501static unsigned int selinux_ip_output(struct sk_buff *skb,
5502 u16 family)
5503{
Paul Moore47180062013-12-04 16:10:45 -05005504 struct sock *sk;
Paul Moore948bf852008-10-10 10:16:32 -04005505 u32 sid;
5506
5507 if (!netlbl_enabled())
5508 return NF_ACCEPT;
5509
5510 /* we do this in the LOCAL_OUT path and not the POST_ROUTING path
5511 * because we want to make sure we apply the necessary labeling
5512 * before IPsec is applied so we can leverage AH protection */
Paul Moore47180062013-12-04 16:10:45 -05005513 sk = skb->sk;
5514 if (sk) {
5515 struct sk_security_struct *sksec;
5516
Eric Dumazete446f9d2015-10-08 05:01:55 -07005517 if (sk_listener(sk))
Paul Moore47180062013-12-04 16:10:45 -05005518 /* if the socket is the listening state then this
5519 * packet is a SYN-ACK packet which means it needs to
5520 * be labeled based on the connection/request_sock and
5521 * not the parent socket. unfortunately, we can't
5522 * lookup the request_sock yet as it isn't queued on
5523 * the parent socket until after the SYN-ACK is sent.
5524 * the "solution" is to simply pass the packet as-is
5525 * as any IP option based labeling should be copied
5526 * from the initial connection request (in the IP
5527 * layer). it is far from ideal, but until we get a
5528 * security label in the packet itself this is the
5529 * best we can do. */
5530 return NF_ACCEPT;
5531
5532 /* standard practice, label using the parent socket */
5533 sksec = sk->sk_security;
Paul Moore948bf852008-10-10 10:16:32 -04005534 sid = sksec->sid;
5535 } else
5536 sid = SECINITSID_KERNEL;
5537 if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
5538 return NF_DROP;
5539
5540 return NF_ACCEPT;
5541}
5542
Eric W. Biederman06198b32015-09-18 14:33:06 -05005543static unsigned int selinux_ipv4_output(void *priv,
Paul Moore948bf852008-10-10 10:16:32 -04005544 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005545 const struct nf_hook_state *state)
Paul Moore948bf852008-10-10 10:16:32 -04005546{
5547 return selinux_ip_output(skb, PF_INET);
5548}
5549
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005550#if IS_ENABLED(CONFIG_IPV6)
Huw Davies2917f572016-06-27 15:06:15 -04005551static unsigned int selinux_ipv6_output(void *priv,
5552 struct sk_buff *skb,
5553 const struct nf_hook_state *state)
5554{
5555 return selinux_ip_output(skb, PF_INET6);
5556}
5557#endif /* IPV6 */
5558
Paul Mooreeffad8d2008-01-29 08:49:27 -05005559static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
5560 int ifindex,
Paul Moored8395c82008-10-10 10:16:30 -04005561 u16 family)
James Morris4e5ab4c2006-06-09 00:33:33 -07005562{
Eric Dumazet54abc682015-11-08 10:54:07 -08005563 struct sock *sk = skb_to_full_sk(skb);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005564 struct sk_security_struct *sksec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005565 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005566 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04005567 char *addrp;
5568 u8 proto;
James Morris4e5ab4c2006-06-09 00:33:33 -07005569
Paul Mooreeffad8d2008-01-29 08:49:27 -05005570 if (sk == NULL)
5571 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005572 sksec = sk->sk_security;
James Morris4e5ab4c2006-06-09 00:33:33 -07005573
Eric Paris50c205f2012-04-04 15:01:43 -04005574 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005575 ad.u.net = &net;
5576 ad.u.net->netif = ifindex;
5577 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04005578 if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
5579 return NF_DROP;
5580
Paul Moore58bfbb52009-03-27 17:10:41 -04005581 if (selinux_secmark_enabled())
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005582 if (avc_has_perm(&selinux_state,
5583 sksec->sid, skb->secmark,
Paul Moored8395c82008-10-10 10:16:30 -04005584 SECCLASS_PACKET, PACKET__SEND, &ad))
Eric Paris2fe66ec2010-11-23 06:28:08 +00005585 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07005586
Steffen Klassertb9679a72011-02-23 12:55:21 +01005587 if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
5588 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07005589
Paul Mooreeffad8d2008-01-29 08:49:27 -05005590 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591}
5592
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005593static unsigned int selinux_ip_postroute(struct sk_buff *skb,
5594 const struct net_device *outdev,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005595 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596{
Paul Mooreeffad8d2008-01-29 08:49:27 -05005597 u32 secmark_perm;
5598 u32 peer_sid;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005599 int ifindex = outdev->ifindex;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005600 struct sock *sk;
Thomas Liu2bf49692009-07-14 12:14:09 -04005601 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005602 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05005603 char *addrp;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005604 u8 secmark_active;
5605 u8 peerlbl_active;
5606
Paul Mooreeffad8d2008-01-29 08:49:27 -05005607 /* If any sort of compatibility mode is enabled then handoff processing
5608 * to the selinux_ip_postroute_compat() function to deal with the
5609 * special handling. We do this in an attempt to keep this function
5610 * as fast and as clean as possible. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005611 if (!selinux_policycap_netpeer())
Paul Moored8395c82008-10-10 10:16:30 -04005612 return selinux_ip_postroute_compat(skb, ifindex, family);
Paul Moorec0828e52013-12-10 14:58:01 -05005613
Paul Mooreeffad8d2008-01-29 08:49:27 -05005614 secmark_active = selinux_secmark_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04005615 peerlbl_active = selinux_peerlbl_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05005616 if (!secmark_active && !peerlbl_active)
5617 return NF_ACCEPT;
5618
Eric Dumazet54abc682015-11-08 10:54:07 -08005619 sk = skb_to_full_sk(skb);
Paul Moorec0828e52013-12-10 14:58:01 -05005620
Paul Mooreeffad8d2008-01-29 08:49:27 -05005621#ifdef CONFIG_XFRM
5622 /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
5623 * packet transformation so allow the packet to pass without any checks
5624 * since we'll have another chance to perform access control checks
5625 * when the packet is on it's final way out.
5626 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
Paul Moorec0828e52013-12-10 14:58:01 -05005627 * is NULL, in this case go ahead and apply access control.
5628 * NOTE: if this is a local socket (skb->sk != NULL) that is in the
5629 * TCP listening state we cannot wait until the XFRM processing
5630 * is done as we will miss out on the SA label if we do;
5631 * unfortunately, this means more work, but it is only once per
5632 * connection. */
5633 if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL &&
Eric Dumazete446f9d2015-10-08 05:01:55 -07005634 !(sk && sk_listener(sk)))
Paul Mooreeffad8d2008-01-29 08:49:27 -05005635 return NF_ACCEPT;
5636#endif
Paul Mooreeffad8d2008-01-29 08:49:27 -05005637
Paul Moored8395c82008-10-10 10:16:30 -04005638 if (sk == NULL) {
Paul Moore446b8022013-12-04 16:10:51 -05005639 /* Without an associated socket the packet is either coming
5640 * from the kernel or it is being forwarded; check the packet
5641 * to determine which and if the packet is being forwarded
5642 * query the packet directly to determine the security label. */
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005643 if (skb->skb_iif) {
5644 secmark_perm = PACKET__FORWARD_OUT;
Paul Moored8395c82008-10-10 10:16:30 -04005645 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005646 return NF_DROP;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005647 } else {
5648 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04005649 peer_sid = SECINITSID_KERNEL;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005650 }
Eric Dumazete446f9d2015-10-08 05:01:55 -07005651 } else if (sk_listener(sk)) {
Paul Moore446b8022013-12-04 16:10:51 -05005652 /* Locally generated packet but the associated socket is in the
5653 * listening state which means this is a SYN-ACK packet. In
5654 * this particular case the correct security label is assigned
5655 * to the connection/request_sock but unfortunately we can't
5656 * query the request_sock as it isn't queued on the parent
5657 * socket until after the SYN-ACK packet is sent; the only
5658 * viable choice is to regenerate the label like we do in
5659 * selinux_inet_conn_request(). See also selinux_ip_output()
5660 * for similar problems. */
5661 u32 skb_sid;
Eric Dumazete446f9d2015-10-08 05:01:55 -07005662 struct sk_security_struct *sksec;
5663
Eric Dumazete446f9d2015-10-08 05:01:55 -07005664 sksec = sk->sk_security;
Paul Moore446b8022013-12-04 16:10:51 -05005665 if (selinux_skb_peerlbl_sid(skb, family, &skb_sid))
5666 return NF_DROP;
Paul Moorec0828e52013-12-10 14:58:01 -05005667 /* At this point, if the returned skb peerlbl is SECSID_NULL
5668 * and the packet has been through at least one XFRM
5669 * transformation then we must be dealing with the "final"
5670 * form of labeled IPsec packet; since we've already applied
5671 * all of our access controls on this packet we can safely
5672 * pass the packet. */
5673 if (skb_sid == SECSID_NULL) {
5674 switch (family) {
5675 case PF_INET:
5676 if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
5677 return NF_ACCEPT;
5678 break;
5679 case PF_INET6:
5680 if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED)
5681 return NF_ACCEPT;
Paul Moorea7a91a12014-09-03 10:51:59 -04005682 break;
Paul Moorec0828e52013-12-10 14:58:01 -05005683 default:
5684 return NF_DROP_ERR(-ECONNREFUSED);
5685 }
5686 }
Paul Moore446b8022013-12-04 16:10:51 -05005687 if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid))
5688 return NF_DROP;
5689 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04005690 } else {
Paul Moore446b8022013-12-04 16:10:51 -05005691 /* Locally generated packet, fetch the security label from the
5692 * associated socket. */
Paul Mooreeffad8d2008-01-29 08:49:27 -05005693 struct sk_security_struct *sksec = sk->sk_security;
5694 peer_sid = sksec->sid;
5695 secmark_perm = PACKET__SEND;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005696 }
5697
Eric Paris50c205f2012-04-04 15:01:43 -04005698 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005699 ad.u.net = &net;
5700 ad.u.net->netif = ifindex;
5701 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04005702 if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
Eric Paris04f6d702010-11-23 06:28:02 +00005703 return NF_DROP;
Paul Moored8395c82008-10-10 10:16:30 -04005704
Paul Mooreeffad8d2008-01-29 08:49:27 -05005705 if (secmark_active)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005706 if (avc_has_perm(&selinux_state,
5707 peer_sid, skb->secmark,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005708 SECCLASS_PACKET, secmark_perm, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005709 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005710
5711 if (peerlbl_active) {
5712 u32 if_sid;
5713 u32 node_sid;
5714
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005715 if (sel_netif_sid(dev_net(outdev), ifindex, &if_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005716 return NF_DROP;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005717 if (avc_has_perm(&selinux_state,
5718 peer_sid, if_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005719 SECCLASS_NETIF, NETIF__EGRESS, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005720 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005721
5722 if (sel_netnode_sid(addrp, family, &node_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005723 return NF_DROP;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005724 if (avc_has_perm(&selinux_state,
5725 peer_sid, node_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005726 SECCLASS_NODE, NODE__SENDTO, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005727 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005728 }
5729
5730 return NF_ACCEPT;
5731}
5732
Eric W. Biederman06198b32015-09-18 14:33:06 -05005733static unsigned int selinux_ipv4_postroute(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005734 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005735 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005736{
David S. Miller238e54c2015-04-03 20:32:56 -04005737 return selinux_ip_postroute(skb, state->out, PF_INET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738}
5739
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005740#if IS_ENABLED(CONFIG_IPV6)
Eric W. Biederman06198b32015-09-18 14:33:06 -05005741static unsigned int selinux_ipv6_postroute(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005742 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005743 const struct nf_hook_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744{
David S. Miller238e54c2015-04-03 20:32:56 -04005745 return selinux_ip_postroute(skb, state->out, PF_INET6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747#endif /* IPV6 */
5748
5749#endif /* CONFIG_NETFILTER */
5750
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
5752{
Stephen Smalley941fc5b2009-10-01 14:48:23 -04005753 return selinux_nlmsg_perm(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754}
5755
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005756static int ipc_alloc_security(struct kern_ipc_perm *perm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757 u16 sclass)
5758{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759 struct ipc_security_struct *isec;
5760
James Morris89d155e2005-10-30 14:59:21 -08005761 isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005762 if (!isec)
5763 return -ENOMEM;
5764
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765 isec->sclass = sclass;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005766 isec->sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767 perm->security = isec;
5768
5769 return 0;
5770}
5771
5772static void ipc_free_security(struct kern_ipc_perm *perm)
5773{
5774 struct ipc_security_struct *isec = perm->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775 perm->security = NULL;
5776 kfree(isec);
5777}
5778
5779static int msg_msg_alloc_security(struct msg_msg *msg)
5780{
5781 struct msg_security_struct *msec;
5782
James Morris89d155e2005-10-30 14:59:21 -08005783 msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784 if (!msec)
5785 return -ENOMEM;
5786
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787 msec->sid = SECINITSID_UNLABELED;
5788 msg->security = msec;
5789
5790 return 0;
5791}
5792
5793static void msg_msg_free_security(struct msg_msg *msg)
5794{
5795 struct msg_security_struct *msec = msg->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796
5797 msg->security = NULL;
5798 kfree(msec);
5799}
5800
5801static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
Stephen Smalley6af963f2005-05-01 08:58:39 -07005802 u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005805 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005806 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005807
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808 isec = ipc_perms->security;
5809
Eric Paris50c205f2012-04-04 15:01:43 -04005810 ad.type = LSM_AUDIT_DATA_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811 ad.u.ipc_id = ipc_perms->key;
5812
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005813 return avc_has_perm(&selinux_state,
5814 sid, isec->sid, isec->sclass, perms, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005815}
5816
5817static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
5818{
5819 return msg_msg_alloc_security(msg);
5820}
5821
5822static void selinux_msg_msg_free_security(struct msg_msg *msg)
5823{
5824 msg_msg_free_security(msg);
5825}
5826
5827/* message queue security operations */
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005828static int selinux_msg_queue_alloc_security(struct kern_ipc_perm *msq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005829{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005830 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005831 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005832 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833 int rc;
5834
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005835 rc = ipc_alloc_security(msq, SECCLASS_MSGQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836 if (rc)
5837 return rc;
5838
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005839 isec = msq->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840
Eric Paris50c205f2012-04-04 15:01:43 -04005841 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005842 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005844 rc = avc_has_perm(&selinux_state,
5845 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846 MSGQ__CREATE, &ad);
5847 if (rc) {
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005848 ipc_free_security(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849 return rc;
5850 }
5851 return 0;
5852}
5853
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005854static void selinux_msg_queue_free_security(struct kern_ipc_perm *msq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855{
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005856 ipc_free_security(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857}
5858
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005859static int selinux_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005861 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005862 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005863 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005865 isec = msq->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866
Eric Paris50c205f2012-04-04 15:01:43 -04005867 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005868 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005870 return avc_has_perm(&selinux_state,
5871 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005872 MSGQ__ASSOCIATE, &ad);
5873}
5874
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005875static int selinux_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876{
5877 int err;
5878 int perms;
5879
Eric Paris828dfe12008-04-17 13:17:49 -04005880 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005881 case IPC_INFO:
5882 case MSG_INFO:
5883 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005884 return avc_has_perm(&selinux_state,
5885 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005886 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005887 case IPC_STAT:
5888 case MSG_STAT:
Davidlohr Bueso23c8cec2018-04-10 16:35:30 -07005889 case MSG_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890 perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
5891 break;
5892 case IPC_SET:
5893 perms = MSGQ__SETATTR;
5894 break;
5895 case IPC_RMID:
5896 perms = MSGQ__DESTROY;
5897 break;
5898 default:
5899 return 0;
5900 }
5901
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005902 err = ipc_has_perm(msq, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903 return err;
5904}
5905
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005906static int selinux_msg_queue_msgsnd(struct kern_ipc_perm *msq, struct msg_msg *msg, int msqflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005907{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908 struct ipc_security_struct *isec;
5909 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005910 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005911 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912 int rc;
5913
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005914 isec = msq->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005915 msec = msg->security;
5916
5917 /*
5918 * First time through, need to assign label to the message
5919 */
5920 if (msec->sid == SECINITSID_UNLABELED) {
5921 /*
5922 * Compute new sid based on current process and
5923 * message queue this message will be stored in
5924 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005925 rc = security_transition_sid(&selinux_state, sid, isec->sid,
5926 SECCLASS_MSG, NULL, &msec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005927 if (rc)
5928 return rc;
5929 }
5930
Eric Paris50c205f2012-04-04 15:01:43 -04005931 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005932 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933
5934 /* Can this process write to the queue? */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005935 rc = avc_has_perm(&selinux_state,
5936 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005937 MSGQ__WRITE, &ad);
5938 if (!rc)
5939 /* Can this process send the message */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005940 rc = avc_has_perm(&selinux_state,
5941 sid, msec->sid, SECCLASS_MSG,
David Howells275bb412008-11-14 10:39:19 +11005942 MSG__SEND, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005943 if (!rc)
5944 /* Can the message be put in the queue? */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005945 rc = avc_has_perm(&selinux_state,
5946 msec->sid, isec->sid, SECCLASS_MSGQ,
David Howells275bb412008-11-14 10:39:19 +11005947 MSGQ__ENQUEUE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948
5949 return rc;
5950}
5951
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005952static int selinux_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005953 struct task_struct *target,
5954 long type, int mode)
5955{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005956 struct ipc_security_struct *isec;
5957 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005958 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005959 u32 sid = task_sid(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005960 int rc;
5961
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005962 isec = msq->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005963 msec = msg->security;
5964
Eric Paris50c205f2012-04-04 15:01:43 -04005965 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005966 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005967
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005968 rc = avc_has_perm(&selinux_state,
5969 sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005970 SECCLASS_MSGQ, MSGQ__READ, &ad);
5971 if (!rc)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005972 rc = avc_has_perm(&selinux_state,
5973 sid, msec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005974 SECCLASS_MSG, MSG__RECEIVE, &ad);
5975 return rc;
5976}
5977
5978/* Shared Memory security operations */
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005979static int selinux_shm_alloc_security(struct kern_ipc_perm *shp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005980{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005981 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005982 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005983 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984 int rc;
5985
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005986 rc = ipc_alloc_security(shp, SECCLASS_SHM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987 if (rc)
5988 return rc;
5989
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005990 isec = shp->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991
Eric Paris50c205f2012-04-04 15:01:43 -04005992 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005993 ad.u.ipc_id = shp->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005994
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005995 rc = avc_has_perm(&selinux_state,
5996 sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997 SHM__CREATE, &ad);
5998 if (rc) {
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005999 ipc_free_security(shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006000 return rc;
6001 }
6002 return 0;
6003}
6004
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006005static void selinux_shm_free_security(struct kern_ipc_perm *shp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006006{
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006007 ipc_free_security(shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006008}
6009
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006010static int selinux_shm_associate(struct kern_ipc_perm *shp, int shmflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006013 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006014 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006016 isec = shp->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006017
Eric Paris50c205f2012-04-04 15:01:43 -04006018 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006019 ad.u.ipc_id = shp->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006021 return avc_has_perm(&selinux_state,
6022 sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006023 SHM__ASSOCIATE, &ad);
6024}
6025
6026/* Note, at this point, shp is locked down */
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006027static int selinux_shm_shmctl(struct kern_ipc_perm *shp, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028{
6029 int perms;
6030 int err;
6031
Eric Paris828dfe12008-04-17 13:17:49 -04006032 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033 case IPC_INFO:
6034 case SHM_INFO:
6035 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006036 return avc_has_perm(&selinux_state,
6037 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006038 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039 case IPC_STAT:
6040 case SHM_STAT:
Davidlohr Buesoc21a6972018-04-10 16:35:23 -07006041 case SHM_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006042 perms = SHM__GETATTR | SHM__ASSOCIATE;
6043 break;
6044 case IPC_SET:
6045 perms = SHM__SETATTR;
6046 break;
6047 case SHM_LOCK:
6048 case SHM_UNLOCK:
6049 perms = SHM__LOCK;
6050 break;
6051 case IPC_RMID:
6052 perms = SHM__DESTROY;
6053 break;
6054 default:
6055 return 0;
6056 }
6057
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006058 err = ipc_has_perm(shp, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059 return err;
6060}
6061
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006062static int selinux_shm_shmat(struct kern_ipc_perm *shp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063 char __user *shmaddr, int shmflg)
6064{
6065 u32 perms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006066
6067 if (shmflg & SHM_RDONLY)
6068 perms = SHM__READ;
6069 else
6070 perms = SHM__READ | SHM__WRITE;
6071
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006072 return ipc_has_perm(shp, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006073}
6074
6075/* Semaphore security operations */
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006076static int selinux_sem_alloc_security(struct kern_ipc_perm *sma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006077{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006078 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006079 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006080 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081 int rc;
6082
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006083 rc = ipc_alloc_security(sma, SECCLASS_SEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084 if (rc)
6085 return rc;
6086
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006087 isec = sma->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088
Eric Paris50c205f2012-04-04 15:01:43 -04006089 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006090 ad.u.ipc_id = sma->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006091
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006092 rc = avc_has_perm(&selinux_state,
6093 sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006094 SEM__CREATE, &ad);
6095 if (rc) {
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006096 ipc_free_security(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097 return rc;
6098 }
6099 return 0;
6100}
6101
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006102static void selinux_sem_free_security(struct kern_ipc_perm *sma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006103{
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006104 ipc_free_security(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006105}
6106
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006107static int selinux_sem_associate(struct kern_ipc_perm *sma, int semflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006108{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006109 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006110 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006111 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006112
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006113 isec = sma->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006114
Eric Paris50c205f2012-04-04 15:01:43 -04006115 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006116 ad.u.ipc_id = sma->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006117
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006118 return avc_has_perm(&selinux_state,
6119 sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120 SEM__ASSOCIATE, &ad);
6121}
6122
6123/* Note, at this point, sma is locked down */
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006124static int selinux_sem_semctl(struct kern_ipc_perm *sma, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006125{
6126 int err;
6127 u32 perms;
6128
Eric Paris828dfe12008-04-17 13:17:49 -04006129 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006130 case IPC_INFO:
6131 case SEM_INFO:
6132 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006133 return avc_has_perm(&selinux_state,
6134 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006135 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006136 case GETPID:
6137 case GETNCNT:
6138 case GETZCNT:
6139 perms = SEM__GETATTR;
6140 break;
6141 case GETVAL:
6142 case GETALL:
6143 perms = SEM__READ;
6144 break;
6145 case SETVAL:
6146 case SETALL:
6147 perms = SEM__WRITE;
6148 break;
6149 case IPC_RMID:
6150 perms = SEM__DESTROY;
6151 break;
6152 case IPC_SET:
6153 perms = SEM__SETATTR;
6154 break;
6155 case IPC_STAT:
6156 case SEM_STAT:
Davidlohr Buesoa280d6d2018-04-10 16:35:26 -07006157 case SEM_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006158 perms = SEM__GETATTR | SEM__ASSOCIATE;
6159 break;
6160 default:
6161 return 0;
6162 }
6163
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006164 err = ipc_has_perm(sma, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006165 return err;
6166}
6167
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006168static int selinux_sem_semop(struct kern_ipc_perm *sma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006169 struct sembuf *sops, unsigned nsops, int alter)
6170{
6171 u32 perms;
6172
6173 if (alter)
6174 perms = SEM__READ | SEM__WRITE;
6175 else
6176 perms = SEM__READ;
6177
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006178 return ipc_has_perm(sma, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006179}
6180
6181static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
6182{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006183 u32 av = 0;
6184
Linus Torvalds1da177e2005-04-16 15:20:36 -07006185 av = 0;
6186 if (flag & S_IRUGO)
6187 av |= IPC__UNIX_READ;
6188 if (flag & S_IWUGO)
6189 av |= IPC__UNIX_WRITE;
6190
6191 if (av == 0)
6192 return 0;
6193
Stephen Smalley6af963f2005-05-01 08:58:39 -07006194 return ipc_has_perm(ipcp, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006195}
6196
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02006197static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
6198{
6199 struct ipc_security_struct *isec = ipcp->security;
6200 *secid = isec->sid;
6201}
6202
Eric Paris828dfe12008-04-17 13:17:49 -04006203static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204{
6205 if (inode)
6206 inode_doinit_with_dentry(inode, dentry);
6207}
6208
6209static int selinux_getprocattr(struct task_struct *p,
Al Viro04ff9702007-03-12 16:17:58 +00006210 char *name, char **value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211{
David Howells275bb412008-11-14 10:39:19 +11006212 const struct task_security_struct *__tsec;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00006213 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006214 int error;
Al Viro04ff9702007-03-12 16:17:58 +00006215 unsigned len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006216
David Howells275bb412008-11-14 10:39:19 +11006217 rcu_read_lock();
6218 __tsec = __task_cred(p)->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006220 if (current != p) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006221 error = avc_has_perm(&selinux_state,
6222 current_sid(), __tsec->sid,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006223 SECCLASS_PROCESS, PROCESS__GETATTR, NULL);
6224 if (error)
6225 goto bad;
6226 }
6227
Linus Torvalds1da177e2005-04-16 15:20:36 -07006228 if (!strcmp(name, "current"))
David Howells275bb412008-11-14 10:39:19 +11006229 sid = __tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006230 else if (!strcmp(name, "prev"))
David Howells275bb412008-11-14 10:39:19 +11006231 sid = __tsec->osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006232 else if (!strcmp(name, "exec"))
David Howells275bb412008-11-14 10:39:19 +11006233 sid = __tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006234 else if (!strcmp(name, "fscreate"))
David Howells275bb412008-11-14 10:39:19 +11006235 sid = __tsec->create_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006236 else if (!strcmp(name, "keycreate"))
David Howells275bb412008-11-14 10:39:19 +11006237 sid = __tsec->keycreate_sid;
Eric Paris42c3e032006-06-26 00:26:03 -07006238 else if (!strcmp(name, "sockcreate"))
David Howells275bb412008-11-14 10:39:19 +11006239 sid = __tsec->sockcreate_sid;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006240 else {
6241 error = -EINVAL;
6242 goto bad;
6243 }
David Howells275bb412008-11-14 10:39:19 +11006244 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006245
6246 if (!sid)
6247 return 0;
6248
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006249 error = security_sid_to_context(&selinux_state, sid, value, &len);
Al Viro04ff9702007-03-12 16:17:58 +00006250 if (error)
6251 return error;
6252 return len;
David Howells275bb412008-11-14 10:39:19 +11006253
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006254bad:
David Howells275bb412008-11-14 10:39:19 +11006255 rcu_read_unlock();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006256 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006257}
6258
Stephen Smalleyb21507e2017-01-09 10:07:31 -05006259static int selinux_setprocattr(const char *name, void *value, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006260{
6261 struct task_security_struct *tsec;
David Howellsd84f4f92008-11-14 10:39:23 +11006262 struct cred *new;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006263 u32 mysid = current_sid(), sid = 0, ptsid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006264 int error;
6265 char *str = value;
6266
Linus Torvalds1da177e2005-04-16 15:20:36 -07006267 /*
6268 * Basic control over ability to set these attributes at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006269 */
6270 if (!strcmp(name, "exec"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006271 error = avc_has_perm(&selinux_state,
6272 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006273 PROCESS__SETEXEC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006274 else if (!strcmp(name, "fscreate"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006275 error = avc_has_perm(&selinux_state,
6276 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006277 PROCESS__SETFSCREATE, NULL);
Michael LeMay4eb582c2006-06-26 00:24:57 -07006278 else if (!strcmp(name, "keycreate"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006279 error = avc_has_perm(&selinux_state,
6280 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006281 PROCESS__SETKEYCREATE, NULL);
Eric Paris42c3e032006-06-26 00:26:03 -07006282 else if (!strcmp(name, "sockcreate"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006283 error = avc_has_perm(&selinux_state,
6284 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006285 PROCESS__SETSOCKCREATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006286 else if (!strcmp(name, "current"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006287 error = avc_has_perm(&selinux_state,
6288 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006289 PROCESS__SETCURRENT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006290 else
6291 error = -EINVAL;
6292 if (error)
6293 return error;
6294
6295 /* Obtain a SID for the context, if one was specified. */
Stephen Smalleya050a572017-01-31 11:54:04 -05006296 if (size && str[0] && str[0] != '\n') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006297 if (str[size-1] == '\n') {
6298 str[size-1] = 0;
6299 size--;
6300 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006301 error = security_context_to_sid(&selinux_state, value, size,
6302 &sid, GFP_KERNEL);
Stephen Smalley12b29f32008-05-07 13:03:20 -04006303 if (error == -EINVAL && !strcmp(name, "fscreate")) {
Stephen Smalleydb590002017-04-20 11:31:30 -04006304 if (!has_cap_mac_admin(true)) {
Eric Parisd6ea83e2012-04-04 13:45:49 -04006305 struct audit_buffer *ab;
6306 size_t audit_size;
6307
6308 /* We strip a nul only if it is at the end, otherwise the
6309 * context contains a nul and we should audit that */
6310 if (str[size - 1] == '\0')
6311 audit_size = size - 1;
6312 else
6313 audit_size = size;
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04006314 ab = audit_log_start(audit_context(),
6315 GFP_ATOMIC,
6316 AUDIT_SELINUX_ERR);
Eric Parisd6ea83e2012-04-04 13:45:49 -04006317 audit_log_format(ab, "op=fscreate invalid_context=");
6318 audit_log_n_untrustedstring(ab, value, audit_size);
6319 audit_log_end(ab);
6320
Stephen Smalley12b29f32008-05-07 13:03:20 -04006321 return error;
Eric Parisd6ea83e2012-04-04 13:45:49 -04006322 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006323 error = security_context_to_sid_force(
6324 &selinux_state,
6325 value, size, &sid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04006326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006327 if (error)
6328 return error;
6329 }
6330
David Howellsd84f4f92008-11-14 10:39:23 +11006331 new = prepare_creds();
6332 if (!new)
6333 return -ENOMEM;
6334
Linus Torvalds1da177e2005-04-16 15:20:36 -07006335 /* Permission checking based on the specified context is
6336 performed during the actual operation (execve,
6337 open/mkdir/...), when we know the full context of the
David Howellsd84f4f92008-11-14 10:39:23 +11006338 operation. See selinux_bprm_set_creds for the execve
Linus Torvalds1da177e2005-04-16 15:20:36 -07006339 checks and may_create for the file creation checks. The
6340 operation will then fail if the context is not permitted. */
David Howellsd84f4f92008-11-14 10:39:23 +11006341 tsec = new->security;
6342 if (!strcmp(name, "exec")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006343 tsec->exec_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006344 } else if (!strcmp(name, "fscreate")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006345 tsec->create_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006346 } else if (!strcmp(name, "keycreate")) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006347 error = avc_has_perm(&selinux_state,
6348 mysid, sid, SECCLASS_KEY, KEY__CREATE,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006349 NULL);
Michael LeMay4eb582c2006-06-26 00:24:57 -07006350 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11006351 goto abort_change;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006352 tsec->keycreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006353 } else if (!strcmp(name, "sockcreate")) {
Eric Paris42c3e032006-06-26 00:26:03 -07006354 tsec->sockcreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006355 } else if (!strcmp(name, "current")) {
6356 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006357 if (sid == 0)
David Howellsd84f4f92008-11-14 10:39:23 +11006358 goto abort_change;
KaiGai Koheid9250de2008-08-28 16:35:57 +09006359
David Howellsd84f4f92008-11-14 10:39:23 +11006360 /* Only allow single threaded processes to change context */
6361 error = -EPERM;
Oleg Nesterov5bb459b2009-07-10 03:48:23 +02006362 if (!current_is_single_threaded()) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006363 error = security_bounded_transition(&selinux_state,
6364 tsec->sid, sid);
David Howellsd84f4f92008-11-14 10:39:23 +11006365 if (error)
6366 goto abort_change;
Eric Paris828dfe12008-04-17 13:17:49 -04006367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006368
6369 /* Check permissions for the transition. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006370 error = avc_has_perm(&selinux_state,
6371 tsec->sid, sid, SECCLASS_PROCESS,
Eric Paris828dfe12008-04-17 13:17:49 -04006372 PROCESS__DYNTRANSITION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006373 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11006374 goto abort_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375
6376 /* Check for ptracing, and update the task SID if ok.
6377 Otherwise, leave SID unchanged and fail. */
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006378 ptsid = ptrace_parent_sid();
Paul Moore0c6181c2016-03-30 21:41:21 -04006379 if (ptsid != 0) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006380 error = avc_has_perm(&selinux_state,
6381 ptsid, sid, SECCLASS_PROCESS,
David Howellsd84f4f92008-11-14 10:39:23 +11006382 PROCESS__PTRACE, NULL);
6383 if (error)
6384 goto abort_change;
6385 }
6386
6387 tsec->sid = sid;
6388 } else {
6389 error = -EINVAL;
6390 goto abort_change;
6391 }
6392
6393 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006394 return size;
David Howellsd84f4f92008-11-14 10:39:23 +11006395
6396abort_change:
6397 abort_creds(new);
6398 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006399}
6400
David Quigley746df9b2013-05-22 12:50:35 -04006401static int selinux_ismaclabel(const char *name)
6402{
6403 return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0);
6404}
6405
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006406static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
6407{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006408 return security_sid_to_context(&selinux_state, secid,
6409 secdata, seclen);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006410}
6411
David Howells7bf570d2008-04-29 20:52:51 +01006412static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
David Howells63cb3442008-01-15 23:47:35 +00006413{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006414 return security_context_to_sid(&selinux_state, secdata, seclen,
6415 secid, GFP_KERNEL);
David Howells63cb3442008-01-15 23:47:35 +00006416}
6417
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006418static void selinux_release_secctx(char *secdata, u32 seclen)
6419{
Paul Moore088999e2007-08-01 11:12:58 -04006420 kfree(secdata);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006421}
6422
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006423static void selinux_inode_invalidate_secctx(struct inode *inode)
6424{
6425 struct inode_security_struct *isec = inode->i_security;
6426
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01006427 spin_lock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006428 isec->initialized = LABEL_INVALID;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01006429 spin_unlock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006430}
6431
David P. Quigley1ee65e32009-09-03 14:25:57 -04006432/*
6433 * called with inode->i_mutex locked
6434 */
6435static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
6436{
6437 return selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, ctx, ctxlen, 0);
6438}
6439
6440/*
6441 * called with inode->i_mutex locked
6442 */
6443static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
6444{
6445 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0);
6446}
6447
6448static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
6449{
6450 int len = 0;
6451 len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX,
6452 ctx, true);
6453 if (len < 0)
6454 return len;
6455 *ctxlen = len;
6456 return 0;
6457}
Michael LeMayd7200242006-06-22 14:47:17 -07006458#ifdef CONFIG_KEYS
6459
David Howellsd84f4f92008-11-14 10:39:23 +11006460static int selinux_key_alloc(struct key *k, const struct cred *cred,
David Howells7e047ef2006-06-26 00:24:50 -07006461 unsigned long flags)
Michael LeMayd7200242006-06-22 14:47:17 -07006462{
David Howellsd84f4f92008-11-14 10:39:23 +11006463 const struct task_security_struct *tsec;
Michael LeMayd7200242006-06-22 14:47:17 -07006464 struct key_security_struct *ksec;
6465
6466 ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
6467 if (!ksec)
6468 return -ENOMEM;
6469
David Howellsd84f4f92008-11-14 10:39:23 +11006470 tsec = cred->security;
6471 if (tsec->keycreate_sid)
6472 ksec->sid = tsec->keycreate_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006473 else
David Howellsd84f4f92008-11-14 10:39:23 +11006474 ksec->sid = tsec->sid;
Michael LeMayd7200242006-06-22 14:47:17 -07006475
David Howells275bb412008-11-14 10:39:19 +11006476 k->security = ksec;
Michael LeMayd7200242006-06-22 14:47:17 -07006477 return 0;
6478}
6479
6480static void selinux_key_free(struct key *k)
6481{
6482 struct key_security_struct *ksec = k->security;
6483
6484 k->security = NULL;
6485 kfree(ksec);
6486}
6487
6488static int selinux_key_permission(key_ref_t key_ref,
David Howellsd84f4f92008-11-14 10:39:23 +11006489 const struct cred *cred,
David Howellsf5895942014-03-14 17:44:49 +00006490 unsigned perm)
Michael LeMayd7200242006-06-22 14:47:17 -07006491{
6492 struct key *key;
Michael LeMayd7200242006-06-22 14:47:17 -07006493 struct key_security_struct *ksec;
David Howells275bb412008-11-14 10:39:19 +11006494 u32 sid;
Michael LeMayd7200242006-06-22 14:47:17 -07006495
6496 /* if no specific permissions are requested, we skip the
6497 permission check. No serious, additional covert channels
6498 appear to be created. */
6499 if (perm == 0)
6500 return 0;
6501
David Howellsd84f4f92008-11-14 10:39:23 +11006502 sid = cred_sid(cred);
David Howells275bb412008-11-14 10:39:19 +11006503
6504 key = key_ref_to_ptr(key_ref);
6505 ksec = key->security;
6506
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006507 return avc_has_perm(&selinux_state,
6508 sid, ksec->sid, SECCLASS_KEY, perm, NULL);
Michael LeMayd7200242006-06-22 14:47:17 -07006509}
6510
David Howells70a5bb72008-04-29 01:01:26 -07006511static int selinux_key_getsecurity(struct key *key, char **_buffer)
6512{
6513 struct key_security_struct *ksec = key->security;
6514 char *context = NULL;
6515 unsigned len;
6516 int rc;
6517
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006518 rc = security_sid_to_context(&selinux_state, ksec->sid,
6519 &context, &len);
David Howells70a5bb72008-04-29 01:01:26 -07006520 if (!rc)
6521 rc = len;
6522 *_buffer = context;
6523 return rc;
6524}
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006525#endif
David Howells70a5bb72008-04-29 01:01:26 -07006526
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006527#ifdef CONFIG_SECURITY_INFINIBAND
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006528static int selinux_ib_pkey_access(void *ib_sec, u64 subnet_prefix, u16 pkey_val)
6529{
6530 struct common_audit_data ad;
6531 int err;
6532 u32 sid = 0;
6533 struct ib_security_struct *sec = ib_sec;
6534 struct lsm_ibpkey_audit ibpkey;
6535
Daniel Jurgens409dcf32017-05-19 15:48:59 +03006536 err = sel_ib_pkey_sid(subnet_prefix, pkey_val, &sid);
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006537 if (err)
6538 return err;
6539
6540 ad.type = LSM_AUDIT_DATA_IBPKEY;
6541 ibpkey.subnet_prefix = subnet_prefix;
6542 ibpkey.pkey = pkey_val;
6543 ad.u.ibpkey = &ibpkey;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006544 return avc_has_perm(&selinux_state,
6545 sec->sid, sid,
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006546 SECCLASS_INFINIBAND_PKEY,
6547 INFINIBAND_PKEY__ACCESS, &ad);
6548}
6549
Daniel Jurgensab861df2017-05-19 15:48:58 +03006550static int selinux_ib_endport_manage_subnet(void *ib_sec, const char *dev_name,
6551 u8 port_num)
6552{
6553 struct common_audit_data ad;
6554 int err;
6555 u32 sid = 0;
6556 struct ib_security_struct *sec = ib_sec;
6557 struct lsm_ibendport_audit ibendport;
6558
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006559 err = security_ib_endport_sid(&selinux_state, dev_name, port_num,
6560 &sid);
Daniel Jurgensab861df2017-05-19 15:48:58 +03006561
6562 if (err)
6563 return err;
6564
6565 ad.type = LSM_AUDIT_DATA_IBENDPORT;
6566 strncpy(ibendport.dev_name, dev_name, sizeof(ibendport.dev_name));
6567 ibendport.port = port_num;
6568 ad.u.ibendport = &ibendport;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006569 return avc_has_perm(&selinux_state,
6570 sec->sid, sid,
Daniel Jurgensab861df2017-05-19 15:48:58 +03006571 SECCLASS_INFINIBAND_ENDPORT,
6572 INFINIBAND_ENDPORT__MANAGE_SUBNET, &ad);
6573}
6574
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006575static int selinux_ib_alloc_security(void **ib_sec)
6576{
6577 struct ib_security_struct *sec;
6578
6579 sec = kzalloc(sizeof(*sec), GFP_KERNEL);
6580 if (!sec)
6581 return -ENOMEM;
6582 sec->sid = current_sid();
6583
6584 *ib_sec = sec;
6585 return 0;
6586}
6587
6588static void selinux_ib_free_security(void *ib_sec)
6589{
6590 kfree(ib_sec);
6591}
Michael LeMayd7200242006-06-22 14:47:17 -07006592#endif
6593
Chenbo Fengec27c352017-10-18 13:00:25 -07006594#ifdef CONFIG_BPF_SYSCALL
6595static int selinux_bpf(int cmd, union bpf_attr *attr,
6596 unsigned int size)
6597{
6598 u32 sid = current_sid();
6599 int ret;
6600
6601 switch (cmd) {
6602 case BPF_MAP_CREATE:
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006603 ret = avc_has_perm(&selinux_state,
6604 sid, sid, SECCLASS_BPF, BPF__MAP_CREATE,
Chenbo Fengec27c352017-10-18 13:00:25 -07006605 NULL);
6606 break;
6607 case BPF_PROG_LOAD:
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006608 ret = avc_has_perm(&selinux_state,
6609 sid, sid, SECCLASS_BPF, BPF__PROG_LOAD,
Chenbo Fengec27c352017-10-18 13:00:25 -07006610 NULL);
6611 break;
6612 default:
6613 ret = 0;
6614 break;
6615 }
6616
6617 return ret;
6618}
6619
6620static u32 bpf_map_fmode_to_av(fmode_t fmode)
6621{
6622 u32 av = 0;
6623
6624 if (fmode & FMODE_READ)
6625 av |= BPF__MAP_READ;
6626 if (fmode & FMODE_WRITE)
6627 av |= BPF__MAP_WRITE;
6628 return av;
6629}
6630
Chenbo Fengf66e4482017-10-18 13:00:26 -07006631/* This function will check the file pass through unix socket or binder to see
6632 * if it is a bpf related object. And apply correspinding checks on the bpf
6633 * object based on the type. The bpf maps and programs, not like other files and
6634 * socket, are using a shared anonymous inode inside the kernel as their inode.
6635 * So checking that inode cannot identify if the process have privilege to
6636 * access the bpf object and that's why we have to add this additional check in
6637 * selinux_file_receive and selinux_binder_transfer_files.
6638 */
6639static int bpf_fd_pass(struct file *file, u32 sid)
6640{
6641 struct bpf_security_struct *bpfsec;
6642 struct bpf_prog *prog;
6643 struct bpf_map *map;
6644 int ret;
6645
6646 if (file->f_op == &bpf_map_fops) {
6647 map = file->private_data;
6648 bpfsec = map->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006649 ret = avc_has_perm(&selinux_state,
6650 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengf66e4482017-10-18 13:00:26 -07006651 bpf_map_fmode_to_av(file->f_mode), NULL);
6652 if (ret)
6653 return ret;
6654 } else if (file->f_op == &bpf_prog_fops) {
6655 prog = file->private_data;
6656 bpfsec = prog->aux->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006657 ret = avc_has_perm(&selinux_state,
6658 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengf66e4482017-10-18 13:00:26 -07006659 BPF__PROG_RUN, NULL);
6660 if (ret)
6661 return ret;
6662 }
6663 return 0;
6664}
6665
Chenbo Fengec27c352017-10-18 13:00:25 -07006666static int selinux_bpf_map(struct bpf_map *map, fmode_t fmode)
6667{
6668 u32 sid = current_sid();
6669 struct bpf_security_struct *bpfsec;
6670
6671 bpfsec = map->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006672 return avc_has_perm(&selinux_state,
6673 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengec27c352017-10-18 13:00:25 -07006674 bpf_map_fmode_to_av(fmode), NULL);
6675}
6676
6677static int selinux_bpf_prog(struct bpf_prog *prog)
6678{
6679 u32 sid = current_sid();
6680 struct bpf_security_struct *bpfsec;
6681
6682 bpfsec = prog->aux->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006683 return avc_has_perm(&selinux_state,
6684 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengec27c352017-10-18 13:00:25 -07006685 BPF__PROG_RUN, NULL);
6686}
6687
6688static int selinux_bpf_map_alloc(struct bpf_map *map)
6689{
6690 struct bpf_security_struct *bpfsec;
6691
6692 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6693 if (!bpfsec)
6694 return -ENOMEM;
6695
6696 bpfsec->sid = current_sid();
6697 map->security = bpfsec;
6698
6699 return 0;
6700}
6701
6702static void selinux_bpf_map_free(struct bpf_map *map)
6703{
6704 struct bpf_security_struct *bpfsec = map->security;
6705
6706 map->security = NULL;
6707 kfree(bpfsec);
6708}
6709
6710static int selinux_bpf_prog_alloc(struct bpf_prog_aux *aux)
6711{
6712 struct bpf_security_struct *bpfsec;
6713
6714 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6715 if (!bpfsec)
6716 return -ENOMEM;
6717
6718 bpfsec->sid = current_sid();
6719 aux->security = bpfsec;
6720
6721 return 0;
6722}
6723
6724static void selinux_bpf_prog_free(struct bpf_prog_aux *aux)
6725{
6726 struct bpf_security_struct *bpfsec = aux->security;
6727
6728 aux->security = NULL;
6729 kfree(bpfsec);
6730}
6731#endif
6732
James Morrisca97d932017-02-15 00:18:51 +11006733static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {
Casey Schauflere20b0432015-05-02 15:11:36 -07006734 LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr),
6735 LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction),
6736 LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder),
6737 LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file),
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02006738
Casey Schauflere20b0432015-05-02 15:11:36 -07006739 LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check),
6740 LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme),
6741 LSM_HOOK_INIT(capget, selinux_capget),
6742 LSM_HOOK_INIT(capset, selinux_capset),
6743 LSM_HOOK_INIT(capable, selinux_capable),
6744 LSM_HOOK_INIT(quotactl, selinux_quotactl),
6745 LSM_HOOK_INIT(quota_on, selinux_quota_on),
6746 LSM_HOOK_INIT(syslog, selinux_syslog),
6747 LSM_HOOK_INIT(vm_enough_memory, selinux_vm_enough_memory),
Stephen Smalley79af7302015-01-21 10:54:10 -05006748
Casey Schauflere20b0432015-05-02 15:11:36 -07006749 LSM_HOOK_INIT(netlink_send, selinux_netlink_send),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006750
Casey Schauflere20b0432015-05-02 15:11:36 -07006751 LSM_HOOK_INIT(bprm_set_creds, selinux_bprm_set_creds),
6752 LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds),
6753 LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006754
Casey Schauflere20b0432015-05-02 15:11:36 -07006755 LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security),
6756 LSM_HOOK_INIT(sb_free_security, selinux_sb_free_security),
Al Viro5b400232018-12-12 20:13:29 -05006757 LSM_HOOK_INIT(sb_eat_lsm_opts, selinux_sb_eat_lsm_opts),
Al Viro204cc0c2018-12-13 13:41:47 -05006758 LSM_HOOK_INIT(sb_free_mnt_opts, selinux_free_mnt_opts),
Casey Schauflere20b0432015-05-02 15:11:36 -07006759 LSM_HOOK_INIT(sb_remount, selinux_sb_remount),
6760 LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount),
6761 LSM_HOOK_INIT(sb_show_options, selinux_sb_show_options),
6762 LSM_HOOK_INIT(sb_statfs, selinux_sb_statfs),
6763 LSM_HOOK_INIT(sb_mount, selinux_mount),
6764 LSM_HOOK_INIT(sb_umount, selinux_umount),
6765 LSM_HOOK_INIT(sb_set_mnt_opts, selinux_set_mnt_opts),
6766 LSM_HOOK_INIT(sb_clone_mnt_opts, selinux_sb_clone_mnt_opts),
6767 LSM_HOOK_INIT(sb_parse_opts_str, selinux_parse_opts_str),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006768
Casey Schauflere20b0432015-05-02 15:11:36 -07006769 LSM_HOOK_INIT(dentry_init_security, selinux_dentry_init_security),
Vivek Goyala518b0a2016-07-13 10:44:53 -04006770 LSM_HOOK_INIT(dentry_create_files_as, selinux_dentry_create_files_as),
Eric Parise0007522008-03-05 10:31:54 -05006771
Casey Schauflere20b0432015-05-02 15:11:36 -07006772 LSM_HOOK_INIT(inode_alloc_security, selinux_inode_alloc_security),
6773 LSM_HOOK_INIT(inode_free_security, selinux_inode_free_security),
6774 LSM_HOOK_INIT(inode_init_security, selinux_inode_init_security),
6775 LSM_HOOK_INIT(inode_create, selinux_inode_create),
6776 LSM_HOOK_INIT(inode_link, selinux_inode_link),
6777 LSM_HOOK_INIT(inode_unlink, selinux_inode_unlink),
6778 LSM_HOOK_INIT(inode_symlink, selinux_inode_symlink),
6779 LSM_HOOK_INIT(inode_mkdir, selinux_inode_mkdir),
6780 LSM_HOOK_INIT(inode_rmdir, selinux_inode_rmdir),
6781 LSM_HOOK_INIT(inode_mknod, selinux_inode_mknod),
6782 LSM_HOOK_INIT(inode_rename, selinux_inode_rename),
6783 LSM_HOOK_INIT(inode_readlink, selinux_inode_readlink),
6784 LSM_HOOK_INIT(inode_follow_link, selinux_inode_follow_link),
6785 LSM_HOOK_INIT(inode_permission, selinux_inode_permission),
6786 LSM_HOOK_INIT(inode_setattr, selinux_inode_setattr),
6787 LSM_HOOK_INIT(inode_getattr, selinux_inode_getattr),
6788 LSM_HOOK_INIT(inode_setxattr, selinux_inode_setxattr),
6789 LSM_HOOK_INIT(inode_post_setxattr, selinux_inode_post_setxattr),
6790 LSM_HOOK_INIT(inode_getxattr, selinux_inode_getxattr),
6791 LSM_HOOK_INIT(inode_listxattr, selinux_inode_listxattr),
6792 LSM_HOOK_INIT(inode_removexattr, selinux_inode_removexattr),
6793 LSM_HOOK_INIT(inode_getsecurity, selinux_inode_getsecurity),
6794 LSM_HOOK_INIT(inode_setsecurity, selinux_inode_setsecurity),
6795 LSM_HOOK_INIT(inode_listsecurity, selinux_inode_listsecurity),
6796 LSM_HOOK_INIT(inode_getsecid, selinux_inode_getsecid),
Vivek Goyal56909eb2016-07-13 10:44:48 -04006797 LSM_HOOK_INIT(inode_copy_up, selinux_inode_copy_up),
Vivek Goyal19472b62016-07-13 10:44:50 -04006798 LSM_HOOK_INIT(inode_copy_up_xattr, selinux_inode_copy_up_xattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006799
Casey Schauflere20b0432015-05-02 15:11:36 -07006800 LSM_HOOK_INIT(file_permission, selinux_file_permission),
6801 LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security),
6802 LSM_HOOK_INIT(file_free_security, selinux_file_free_security),
6803 LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl),
6804 LSM_HOOK_INIT(mmap_file, selinux_mmap_file),
6805 LSM_HOOK_INIT(mmap_addr, selinux_mmap_addr),
6806 LSM_HOOK_INIT(file_mprotect, selinux_file_mprotect),
6807 LSM_HOOK_INIT(file_lock, selinux_file_lock),
6808 LSM_HOOK_INIT(file_fcntl, selinux_file_fcntl),
6809 LSM_HOOK_INIT(file_set_fowner, selinux_file_set_fowner),
6810 LSM_HOOK_INIT(file_send_sigiotask, selinux_file_send_sigiotask),
6811 LSM_HOOK_INIT(file_receive, selinux_file_receive),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006812
Casey Schauflere20b0432015-05-02 15:11:36 -07006813 LSM_HOOK_INIT(file_open, selinux_file_open),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006814
Tetsuo Handaa79be232017-03-28 23:08:45 +09006815 LSM_HOOK_INIT(task_alloc, selinux_task_alloc),
Casey Schauflere20b0432015-05-02 15:11:36 -07006816 LSM_HOOK_INIT(cred_alloc_blank, selinux_cred_alloc_blank),
6817 LSM_HOOK_INIT(cred_free, selinux_cred_free),
6818 LSM_HOOK_INIT(cred_prepare, selinux_cred_prepare),
6819 LSM_HOOK_INIT(cred_transfer, selinux_cred_transfer),
Matthew Garrett3ec30112018-01-08 13:36:19 -08006820 LSM_HOOK_INIT(cred_getsecid, selinux_cred_getsecid),
Casey Schauflere20b0432015-05-02 15:11:36 -07006821 LSM_HOOK_INIT(kernel_act_as, selinux_kernel_act_as),
6822 LSM_HOOK_INIT(kernel_create_files_as, selinux_kernel_create_files_as),
6823 LSM_HOOK_INIT(kernel_module_request, selinux_kernel_module_request),
Mimi Zoharc77b8cd2018-07-13 14:06:02 -04006824 LSM_HOOK_INIT(kernel_load_data, selinux_kernel_load_data),
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07006825 LSM_HOOK_INIT(kernel_read_file, selinux_kernel_read_file),
Casey Schauflere20b0432015-05-02 15:11:36 -07006826 LSM_HOOK_INIT(task_setpgid, selinux_task_setpgid),
6827 LSM_HOOK_INIT(task_getpgid, selinux_task_getpgid),
6828 LSM_HOOK_INIT(task_getsid, selinux_task_getsid),
6829 LSM_HOOK_INIT(task_getsecid, selinux_task_getsecid),
6830 LSM_HOOK_INIT(task_setnice, selinux_task_setnice),
6831 LSM_HOOK_INIT(task_setioprio, selinux_task_setioprio),
6832 LSM_HOOK_INIT(task_getioprio, selinux_task_getioprio),
Stephen Smalley791ec492017-02-17 07:57:00 -05006833 LSM_HOOK_INIT(task_prlimit, selinux_task_prlimit),
Casey Schauflere20b0432015-05-02 15:11:36 -07006834 LSM_HOOK_INIT(task_setrlimit, selinux_task_setrlimit),
6835 LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler),
6836 LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler),
6837 LSM_HOOK_INIT(task_movememory, selinux_task_movememory),
6838 LSM_HOOK_INIT(task_kill, selinux_task_kill),
Casey Schauflere20b0432015-05-02 15:11:36 -07006839 LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode),
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09006840
Casey Schauflere20b0432015-05-02 15:11:36 -07006841 LSM_HOOK_INIT(ipc_permission, selinux_ipc_permission),
6842 LSM_HOOK_INIT(ipc_getsecid, selinux_ipc_getsecid),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006843
Casey Schauflere20b0432015-05-02 15:11:36 -07006844 LSM_HOOK_INIT(msg_msg_alloc_security, selinux_msg_msg_alloc_security),
6845 LSM_HOOK_INIT(msg_msg_free_security, selinux_msg_msg_free_security),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006846
Casey Schauflere20b0432015-05-02 15:11:36 -07006847 LSM_HOOK_INIT(msg_queue_alloc_security,
6848 selinux_msg_queue_alloc_security),
6849 LSM_HOOK_INIT(msg_queue_free_security, selinux_msg_queue_free_security),
6850 LSM_HOOK_INIT(msg_queue_associate, selinux_msg_queue_associate),
6851 LSM_HOOK_INIT(msg_queue_msgctl, selinux_msg_queue_msgctl),
6852 LSM_HOOK_INIT(msg_queue_msgsnd, selinux_msg_queue_msgsnd),
6853 LSM_HOOK_INIT(msg_queue_msgrcv, selinux_msg_queue_msgrcv),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006854
Casey Schauflere20b0432015-05-02 15:11:36 -07006855 LSM_HOOK_INIT(shm_alloc_security, selinux_shm_alloc_security),
6856 LSM_HOOK_INIT(shm_free_security, selinux_shm_free_security),
6857 LSM_HOOK_INIT(shm_associate, selinux_shm_associate),
6858 LSM_HOOK_INIT(shm_shmctl, selinux_shm_shmctl),
6859 LSM_HOOK_INIT(shm_shmat, selinux_shm_shmat),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006860
Casey Schauflere20b0432015-05-02 15:11:36 -07006861 LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security),
6862 LSM_HOOK_INIT(sem_free_security, selinux_sem_free_security),
6863 LSM_HOOK_INIT(sem_associate, selinux_sem_associate),
6864 LSM_HOOK_INIT(sem_semctl, selinux_sem_semctl),
6865 LSM_HOOK_INIT(sem_semop, selinux_sem_semop),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006866
Casey Schauflere20b0432015-05-02 15:11:36 -07006867 LSM_HOOK_INIT(d_instantiate, selinux_d_instantiate),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006868
Casey Schauflere20b0432015-05-02 15:11:36 -07006869 LSM_HOOK_INIT(getprocattr, selinux_getprocattr),
6870 LSM_HOOK_INIT(setprocattr, selinux_setprocattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006871
Casey Schauflere20b0432015-05-02 15:11:36 -07006872 LSM_HOOK_INIT(ismaclabel, selinux_ismaclabel),
6873 LSM_HOOK_INIT(secid_to_secctx, selinux_secid_to_secctx),
6874 LSM_HOOK_INIT(secctx_to_secid, selinux_secctx_to_secid),
6875 LSM_HOOK_INIT(release_secctx, selinux_release_secctx),
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006876 LSM_HOOK_INIT(inode_invalidate_secctx, selinux_inode_invalidate_secctx),
Casey Schauflere20b0432015-05-02 15:11:36 -07006877 LSM_HOOK_INIT(inode_notifysecctx, selinux_inode_notifysecctx),
6878 LSM_HOOK_INIT(inode_setsecctx, selinux_inode_setsecctx),
6879 LSM_HOOK_INIT(inode_getsecctx, selinux_inode_getsecctx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006880
Casey Schauflere20b0432015-05-02 15:11:36 -07006881 LSM_HOOK_INIT(unix_stream_connect, selinux_socket_unix_stream_connect),
6882 LSM_HOOK_INIT(unix_may_send, selinux_socket_unix_may_send),
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006883
Casey Schauflere20b0432015-05-02 15:11:36 -07006884 LSM_HOOK_INIT(socket_create, selinux_socket_create),
6885 LSM_HOOK_INIT(socket_post_create, selinux_socket_post_create),
David Herrmann0b811db2018-05-04 16:28:21 +02006886 LSM_HOOK_INIT(socket_socketpair, selinux_socket_socketpair),
Casey Schauflere20b0432015-05-02 15:11:36 -07006887 LSM_HOOK_INIT(socket_bind, selinux_socket_bind),
6888 LSM_HOOK_INIT(socket_connect, selinux_socket_connect),
6889 LSM_HOOK_INIT(socket_listen, selinux_socket_listen),
6890 LSM_HOOK_INIT(socket_accept, selinux_socket_accept),
6891 LSM_HOOK_INIT(socket_sendmsg, selinux_socket_sendmsg),
6892 LSM_HOOK_INIT(socket_recvmsg, selinux_socket_recvmsg),
6893 LSM_HOOK_INIT(socket_getsockname, selinux_socket_getsockname),
6894 LSM_HOOK_INIT(socket_getpeername, selinux_socket_getpeername),
6895 LSM_HOOK_INIT(socket_getsockopt, selinux_socket_getsockopt),
6896 LSM_HOOK_INIT(socket_setsockopt, selinux_socket_setsockopt),
6897 LSM_HOOK_INIT(socket_shutdown, selinux_socket_shutdown),
6898 LSM_HOOK_INIT(socket_sock_rcv_skb, selinux_socket_sock_rcv_skb),
6899 LSM_HOOK_INIT(socket_getpeersec_stream,
6900 selinux_socket_getpeersec_stream),
6901 LSM_HOOK_INIT(socket_getpeersec_dgram, selinux_socket_getpeersec_dgram),
6902 LSM_HOOK_INIT(sk_alloc_security, selinux_sk_alloc_security),
6903 LSM_HOOK_INIT(sk_free_security, selinux_sk_free_security),
6904 LSM_HOOK_INIT(sk_clone_security, selinux_sk_clone_security),
6905 LSM_HOOK_INIT(sk_getsecid, selinux_sk_getsecid),
6906 LSM_HOOK_INIT(sock_graft, selinux_sock_graft),
Richard Hainesd4529302018-02-13 20:57:18 +00006907 LSM_HOOK_INIT(sctp_assoc_request, selinux_sctp_assoc_request),
6908 LSM_HOOK_INIT(sctp_sk_clone, selinux_sctp_sk_clone),
6909 LSM_HOOK_INIT(sctp_bind_connect, selinux_sctp_bind_connect),
Casey Schauflere20b0432015-05-02 15:11:36 -07006910 LSM_HOOK_INIT(inet_conn_request, selinux_inet_conn_request),
6911 LSM_HOOK_INIT(inet_csk_clone, selinux_inet_csk_clone),
6912 LSM_HOOK_INIT(inet_conn_established, selinux_inet_conn_established),
6913 LSM_HOOK_INIT(secmark_relabel_packet, selinux_secmark_relabel_packet),
6914 LSM_HOOK_INIT(secmark_refcount_inc, selinux_secmark_refcount_inc),
6915 LSM_HOOK_INIT(secmark_refcount_dec, selinux_secmark_refcount_dec),
6916 LSM_HOOK_INIT(req_classify_flow, selinux_req_classify_flow),
6917 LSM_HOOK_INIT(tun_dev_alloc_security, selinux_tun_dev_alloc_security),
6918 LSM_HOOK_INIT(tun_dev_free_security, selinux_tun_dev_free_security),
6919 LSM_HOOK_INIT(tun_dev_create, selinux_tun_dev_create),
6920 LSM_HOOK_INIT(tun_dev_attach_queue, selinux_tun_dev_attach_queue),
6921 LSM_HOOK_INIT(tun_dev_attach, selinux_tun_dev_attach),
6922 LSM_HOOK_INIT(tun_dev_open, selinux_tun_dev_open),
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006923#ifdef CONFIG_SECURITY_INFINIBAND
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006924 LSM_HOOK_INIT(ib_pkey_access, selinux_ib_pkey_access),
Daniel Jurgensab861df2017-05-19 15:48:58 +03006925 LSM_HOOK_INIT(ib_endport_manage_subnet,
6926 selinux_ib_endport_manage_subnet),
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006927 LSM_HOOK_INIT(ib_alloc_security, selinux_ib_alloc_security),
6928 LSM_HOOK_INIT(ib_free_security, selinux_ib_free_security),
6929#endif
Trent Jaegerd28d1e02005-12-13 23:12:40 -08006930#ifdef CONFIG_SECURITY_NETWORK_XFRM
Casey Schauflere20b0432015-05-02 15:11:36 -07006931 LSM_HOOK_INIT(xfrm_policy_alloc_security, selinux_xfrm_policy_alloc),
6932 LSM_HOOK_INIT(xfrm_policy_clone_security, selinux_xfrm_policy_clone),
6933 LSM_HOOK_INIT(xfrm_policy_free_security, selinux_xfrm_policy_free),
6934 LSM_HOOK_INIT(xfrm_policy_delete_security, selinux_xfrm_policy_delete),
6935 LSM_HOOK_INIT(xfrm_state_alloc, selinux_xfrm_state_alloc),
6936 LSM_HOOK_INIT(xfrm_state_alloc_acquire,
6937 selinux_xfrm_state_alloc_acquire),
6938 LSM_HOOK_INIT(xfrm_state_free_security, selinux_xfrm_state_free),
6939 LSM_HOOK_INIT(xfrm_state_delete_security, selinux_xfrm_state_delete),
6940 LSM_HOOK_INIT(xfrm_policy_lookup, selinux_xfrm_policy_lookup),
6941 LSM_HOOK_INIT(xfrm_state_pol_flow_match,
6942 selinux_xfrm_state_pol_flow_match),
6943 LSM_HOOK_INIT(xfrm_decode_session, selinux_xfrm_decode_session),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006944#endif
Michael LeMayd7200242006-06-22 14:47:17 -07006945
6946#ifdef CONFIG_KEYS
Casey Schauflere20b0432015-05-02 15:11:36 -07006947 LSM_HOOK_INIT(key_alloc, selinux_key_alloc),
6948 LSM_HOOK_INIT(key_free, selinux_key_free),
6949 LSM_HOOK_INIT(key_permission, selinux_key_permission),
6950 LSM_HOOK_INIT(key_getsecurity, selinux_key_getsecurity),
Michael LeMayd7200242006-06-22 14:47:17 -07006951#endif
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02006952
6953#ifdef CONFIG_AUDIT
Casey Schauflere20b0432015-05-02 15:11:36 -07006954 LSM_HOOK_INIT(audit_rule_init, selinux_audit_rule_init),
6955 LSM_HOOK_INIT(audit_rule_known, selinux_audit_rule_known),
6956 LSM_HOOK_INIT(audit_rule_match, selinux_audit_rule_match),
6957 LSM_HOOK_INIT(audit_rule_free, selinux_audit_rule_free),
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02006958#endif
Chenbo Fengec27c352017-10-18 13:00:25 -07006959
6960#ifdef CONFIG_BPF_SYSCALL
6961 LSM_HOOK_INIT(bpf, selinux_bpf),
6962 LSM_HOOK_INIT(bpf_map, selinux_bpf_map),
6963 LSM_HOOK_INIT(bpf_prog, selinux_bpf_prog),
6964 LSM_HOOK_INIT(bpf_map_alloc_security, selinux_bpf_map_alloc),
6965 LSM_HOOK_INIT(bpf_prog_alloc_security, selinux_bpf_prog_alloc),
6966 LSM_HOOK_INIT(bpf_map_free_security, selinux_bpf_map_free),
6967 LSM_HOOK_INIT(bpf_prog_free_security, selinux_bpf_prog_free),
6968#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006969};
6970
6971static __init int selinux_init(void)
6972{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07006973 if (!security_module_enable("selinux")) {
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02006974 selinux_enabled = 0;
6975 return 0;
6976 }
6977
Linus Torvalds1da177e2005-04-16 15:20:36 -07006978 if (!selinux_enabled) {
peter enderborgc103a912018-06-12 10:09:03 +02006979 pr_info("SELinux: Disabled at boot.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006980 return 0;
6981 }
6982
peter enderborgc103a912018-06-12 10:09:03 +02006983 pr_info("SELinux: Initializing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006984
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006985 memset(&selinux_state, 0, sizeof(selinux_state));
Paul Mooree5a5ca92018-03-01 17:38:30 -05006986 enforcing_set(&selinux_state, selinux_enforcing_boot);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006987 selinux_state.checkreqprot = selinux_checkreqprot_boot;
6988 selinux_ss_init(&selinux_state.ss);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006989 selinux_avc_init(&selinux_state.avc);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006990
Linus Torvalds1da177e2005-04-16 15:20:36 -07006991 /* Set the security state for the initial task. */
David Howellsd84f4f92008-11-14 10:39:23 +11006992 cred_init_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006993
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04006994 default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
6995
James Morris7cae7e22006-03-22 00:09:22 -08006996 sel_inode_cache = kmem_cache_create("selinux_inode_security",
6997 sizeof(struct inode_security_struct),
Paul Mundt20c2df82007-07-20 10:11:58 +09006998 0, SLAB_PANIC, NULL);
Sangwoo63205652015-10-21 17:44:30 -04006999 file_security_cache = kmem_cache_create("selinux_file_security",
7000 sizeof(struct file_security_struct),
7001 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007002 avc_init();
7003
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007004 avtab_cache_init();
7005
7006 ebitmap_cache_init();
7007
7008 hashtab_cache_init();
7009
Casey Schauflerd69dece52017-01-18 17:09:05 -08007010 security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks), "selinux");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007011
Paul Moore615e51f2014-06-26 14:33:56 -04007012 if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET))
7013 panic("SELinux: Unable to register AVC netcache callback\n");
7014
Daniel Jurgens8f408ab2017-05-19 15:48:53 +03007015 if (avc_add_callback(selinux_lsm_notifier_avc_callback, AVC_CALLBACK_RESET))
7016 panic("SELinux: Unable to register AVC LSM notifier callback\n");
7017
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007018 if (selinux_enforcing_boot)
peter enderborgc103a912018-06-12 10:09:03 +02007019 pr_debug("SELinux: Starting in enforcing mode\n");
Eric Paris828dfe12008-04-17 13:17:49 -04007020 else
peter enderborgc103a912018-06-12 10:09:03 +02007021 pr_debug("SELinux: Starting in permissive mode\n");
Michael LeMayd7200242006-06-22 14:47:17 -07007022
Linus Torvalds1da177e2005-04-16 15:20:36 -07007023 return 0;
7024}
7025
Al Viroe8c26252010-03-23 06:36:54 -04007026static void delayed_superblock_init(struct super_block *sb, void *unused)
7027{
Al Viro204cc0c2018-12-13 13:41:47 -05007028 selinux_set_mnt_opts(sb, NULL, 0, NULL);
Al Viroe8c26252010-03-23 06:36:54 -04007029}
7030
Linus Torvalds1da177e2005-04-16 15:20:36 -07007031void selinux_complete_init(void)
7032{
peter enderborgc103a912018-06-12 10:09:03 +02007033 pr_debug("SELinux: Completing initialization.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007034
7035 /* Set up any superblocks initialized prior to the policy load. */
peter enderborgc103a912018-06-12 10:09:03 +02007036 pr_debug("SELinux: Setting up existing superblocks.\n");
Al Viroe8c26252010-03-23 06:36:54 -04007037 iterate_supers(delayed_superblock_init, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007038}
7039
7040/* SELinux requires early initialization in order to label
7041 all processes and objects when they are created. */
Kees Cook3d6e5f62018-10-10 17:18:23 -07007042DEFINE_LSM(selinux) = {
Kees Cook07aed2f2018-10-10 17:18:24 -07007043 .name = "selinux",
Kees Cook3d6e5f62018-10-10 17:18:23 -07007044 .init = selinux_init,
7045};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007046
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007047#if defined(CONFIG_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007048
Florian Westphal591bb272017-07-26 11:40:52 +02007049static const struct nf_hook_ops selinux_nf_ops[] = {
Paul Mooreeffad8d2008-01-29 08:49:27 -05007050 {
7051 .hook = selinux_ipv4_postroute,
Alban Crequy2597a832012-05-14 03:56:39 +00007052 .pf = NFPROTO_IPV4,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007053 .hooknum = NF_INET_POST_ROUTING,
7054 .priority = NF_IP_PRI_SELINUX_LAST,
7055 },
7056 {
7057 .hook = selinux_ipv4_forward,
Alban Crequy2597a832012-05-14 03:56:39 +00007058 .pf = NFPROTO_IPV4,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007059 .hooknum = NF_INET_FORWARD,
7060 .priority = NF_IP_PRI_SELINUX_FIRST,
Paul Moore948bf852008-10-10 10:16:32 -04007061 },
7062 {
7063 .hook = selinux_ipv4_output,
Alban Crequy2597a832012-05-14 03:56:39 +00007064 .pf = NFPROTO_IPV4,
Paul Moore948bf852008-10-10 10:16:32 -04007065 .hooknum = NF_INET_LOCAL_OUT,
7066 .priority = NF_IP_PRI_SELINUX_FIRST,
Jiri Pirko25db6be2014-09-03 17:42:13 +02007067 },
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04007068#if IS_ENABLED(CONFIG_IPV6)
Paul Mooreeffad8d2008-01-29 08:49:27 -05007069 {
7070 .hook = selinux_ipv6_postroute,
Alban Crequy2597a832012-05-14 03:56:39 +00007071 .pf = NFPROTO_IPV6,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007072 .hooknum = NF_INET_POST_ROUTING,
7073 .priority = NF_IP6_PRI_SELINUX_LAST,
7074 },
7075 {
7076 .hook = selinux_ipv6_forward,
Alban Crequy2597a832012-05-14 03:56:39 +00007077 .pf = NFPROTO_IPV6,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007078 .hooknum = NF_INET_FORWARD,
7079 .priority = NF_IP6_PRI_SELINUX_FIRST,
Jiri Pirko25db6be2014-09-03 17:42:13 +02007080 },
Huw Davies2917f572016-06-27 15:06:15 -04007081 {
7082 .hook = selinux_ipv6_output,
7083 .pf = NFPROTO_IPV6,
7084 .hooknum = NF_INET_LOCAL_OUT,
7085 .priority = NF_IP6_PRI_SELINUX_FIRST,
7086 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07007087#endif /* IPV6 */
Jiri Pirko25db6be2014-09-03 17:42:13 +02007088};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007089
Florian Westphal8e71bf72017-04-21 11:49:09 +02007090static int __net_init selinux_nf_register(struct net *net)
7091{
7092 return nf_register_net_hooks(net, selinux_nf_ops,
7093 ARRAY_SIZE(selinux_nf_ops));
7094}
7095
7096static void __net_exit selinux_nf_unregister(struct net *net)
7097{
7098 nf_unregister_net_hooks(net, selinux_nf_ops,
7099 ARRAY_SIZE(selinux_nf_ops));
7100}
7101
7102static struct pernet_operations selinux_net_ops = {
7103 .init = selinux_nf_register,
7104 .exit = selinux_nf_unregister,
7105};
7106
Linus Torvalds1da177e2005-04-16 15:20:36 -07007107static int __init selinux_nf_ip_init(void)
7108{
Jiri Pirko25db6be2014-09-03 17:42:13 +02007109 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007110
7111 if (!selinux_enabled)
Jiri Pirko25db6be2014-09-03 17:42:13 +02007112 return 0;
Eric Parisfadcdb42007-02-22 18:11:31 -05007113
peter enderborgc103a912018-06-12 10:09:03 +02007114 pr_debug("SELinux: Registering netfilter hooks\n");
Eric Parisfadcdb42007-02-22 18:11:31 -05007115
Florian Westphal8e71bf72017-04-21 11:49:09 +02007116 err = register_pernet_subsys(&selinux_net_ops);
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07007117 if (err)
Florian Westphal8e71bf72017-04-21 11:49:09 +02007118 panic("SELinux: register_pernet_subsys: error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007119
Jiri Pirko25db6be2014-09-03 17:42:13 +02007120 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007121}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007122__initcall(selinux_nf_ip_init);
7123
7124#ifdef CONFIG_SECURITY_SELINUX_DISABLE
7125static void selinux_nf_ip_exit(void)
7126{
peter enderborgc103a912018-06-12 10:09:03 +02007127 pr_debug("SELinux: Unregistering netfilter hooks\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007128
Florian Westphal8e71bf72017-04-21 11:49:09 +02007129 unregister_pernet_subsys(&selinux_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007130}
7131#endif
7132
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007133#else /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007134
7135#ifdef CONFIG_SECURITY_SELINUX_DISABLE
7136#define selinux_nf_ip_exit()
7137#endif
7138
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007139#endif /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007140
7141#ifdef CONFIG_SECURITY_SELINUX_DISABLE
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007142int selinux_disable(struct selinux_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007143{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007144 if (state->initialized) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007145 /* Not permitted after initial policy load. */
7146 return -EINVAL;
7147 }
7148
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007149 if (state->disabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007150 /* Only do this once. */
7151 return -EINVAL;
7152 }
7153
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007154 state->disabled = 1;
7155
peter enderborgc103a912018-06-12 10:09:03 +02007156 pr_info("SELinux: Disabled at runtime.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007157
Stephen Smalley30d55282006-05-03 10:52:36 -04007158 selinux_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007159
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07007160 security_delete_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007161
Eric Parisaf8ff042009-09-20 21:23:01 -04007162 /* Try to destroy the avc node cache */
7163 avc_disable();
7164
Linus Torvalds1da177e2005-04-16 15:20:36 -07007165 /* Unregister netfilter hooks. */
7166 selinux_nf_ip_exit();
7167
7168 /* Unregister selinuxfs. */
7169 exit_sel_fs();
7170
7171 return 0;
7172}
7173#endif