blob: 238907d69e8be4b3f6093c052b748e7309be384e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * NSA Security-Enhanced Linux (SELinux) security module
3 *
4 * This file contains the SELinux hook function implementations.
5 *
Stephen Smalley7efbb602017-08-17 13:32:36 -04006 * Authors: Stephen Smalley, <sds@tycho.nsa.gov>
Eric Paris828dfe12008-04-17 13:17:49 -04007 * Chris Vance, <cvance@nai.com>
8 * Wayne Salamon, <wsalamon@nai.com>
9 * James Morris <jmorris@redhat.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * Copyright (C) 2001,2002 Networks Associates Technology, Inc.
Eric Paris2069f452008-07-04 09:47:13 +100012 * Copyright (C) 2003-2008 Red Hat, Inc., James Morris <jmorris@redhat.com>
13 * Eric Paris <eparis@redhat.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
Eric Paris828dfe12008-04-17 13:17:49 -040015 * <dgoeddel@trustedcs.com>
Paul Mooreed6d76e2009-08-28 18:12:49 -040016 * Copyright (C) 2006, 2007, 2009 Hewlett-Packard Development Company, L.P.
Paul Moore82c21bf2011-08-01 11:10:33 +000017 * Paul Moore <paul@paul-moore.com>
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +090018 * Copyright (C) 2007 Hitachi Software Engineering Co., Ltd.
Eric Paris828dfe12008-04-17 13:17:49 -040019 * Yuichi Nakamura <ynakam@hitachisoft.jp>
Daniel Jurgens3a976fa2017-05-19 15:48:56 +030020 * Copyright (C) 2016 Mellanox Technologies
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 *
22 * This program is free software; you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License version 2,
Eric Paris828dfe12008-04-17 13:17:49 -040024 * as published by the Free Software Foundation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 */
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/init.h>
Eric Paris0b24dcb2011-02-25 15:39:20 -050028#include <linux/kd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/kernel.h>
Roland McGrath0d094ef2008-07-25 19:45:49 -070030#include <linux/tracehook.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/errno.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010032#include <linux/sched/signal.h>
Ingo Molnar29930022017-02-08 18:51:36 +010033#include <linux/sched/task.h>
Casey Schaufler3c4ed7b2015-05-02 15:10:46 -070034#include <linux/lsm_hooks.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/xattr.h>
36#include <linux/capability.h>
37#include <linux/unistd.h>
38#include <linux/mm.h>
39#include <linux/mman.h>
40#include <linux/slab.h>
41#include <linux/pagemap.h>
Eric Paris0b24dcb2011-02-25 15:39:20 -050042#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/swap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/spinlock.h>
45#include <linux/syscalls.h>
Eric Paris2a7dba32011-02-01 11:05:39 -050046#include <linux/dcache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/file.h>
Al Viro9f3acc32008-04-24 07:44:08 -040048#include <linux/fdtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/namei.h>
50#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/netfilter_ipv4.h>
52#include <linux/netfilter_ipv6.h>
53#include <linux/tty.h>
54#include <net/icmp.h>
Stephen Hemminger227b60f2007-10-10 17:30:46 -070055#include <net/ip.h> /* for local_port_range[] */
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <net/tcp.h> /* struct or_callable used in sock_rcv_skb */
Paul Moore47180062013-12-04 16:10:45 -050057#include <net/inet_connection_sock.h>
Paul Moore220deb92008-01-29 08:38:23 -050058#include <net/net_namespace.h>
Paul Moored621d352008-01-29 08:43:36 -050059#include <net/netlabel.h>
Eric Parisf5269712008-05-14 11:27:45 -040060#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <asm/ioctls.h>
Arun Sharma600634972011-07-26 16:09:06 -070062#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <linux/bitops.h>
64#include <linux/interrupt.h>
65#include <linux/netdevice.h> /* for network interface checks */
Hong zhi guo77954982013-03-27 06:49:35 +000066#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <linux/tcp.h>
68#include <linux/udp.h>
James Morris2ee92d42006-11-13 16:09:01 -080069#include <linux/dccp.h>
Richard Hainesd4529302018-02-13 20:57:18 +000070#include <linux/sctp.h>
71#include <net/sctp/structs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <linux/quota.h>
73#include <linux/un.h> /* for Unix socket types */
74#include <net/af_unix.h> /* for Unix socket types */
75#include <linux/parser.h>
76#include <linux/nfs_mount.h>
77#include <net/ipv6.h>
78#include <linux/hugetlb.h>
79#include <linux/personality.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#include <linux/audit.h>
Eric Paris6931dfc2005-06-30 02:58:51 -070081#include <linux/string.h>
Catherine Zhang877ce7c2006-06-29 12:27:47 -070082#include <linux/selinux.h>
Eric Paris23970742006-09-25 23:32:01 -070083#include <linux/mutex.h>
Frank Mayharf06febc2008-09-12 09:54:39 -070084#include <linux/posix-timers.h>
Kees Cook00234592010-02-03 15:36:43 -080085#include <linux/syslog.h>
Serge E. Hallyn34867402011-03-23 16:43:17 -070086#include <linux/user_namespace.h>
Paul Gortmaker44fc7ea2011-05-26 20:52:10 -040087#include <linux/export.h>
Al Viro40401532012-02-13 03:58:52 +000088#include <linux/msg.h>
89#include <linux/shm.h>
Chenbo Fengec27c352017-10-18 13:00:25 -070090#include <linux/bpf.h>
David Howellse262e32d2018-11-01 23:07:23 +000091#include <uapi/linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93#include "avc.h"
94#include "objsec.h"
95#include "netif.h"
Paul Moore224dfbd2008-01-29 08:38:13 -050096#include "netnode.h"
Paul Moore3e1121722008-04-10 10:48:14 -040097#include "netport.h"
Daniel Jurgens409dcf32017-05-19 15:48:59 +030098#include "ibpkey.h"
Trent Jaegerd28d1e02005-12-13 23:12:40 -080099#include "xfrm.h"
Paul Moorec60475b2007-02-28 15:14:23 -0500100#include "netlabel.h"
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +0200101#include "audit.h"
James Morris7b98a582011-08-30 12:52:32 +1000102#include "avc_ss.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500104struct selinux_state selinux_state;
105
Paul Moored621d352008-01-29 08:43:36 -0500106/* SECMARK reference count */
James Morris56a4ca92011-08-17 11:08:43 +1000107static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0);
Paul Moored621d352008-01-29 08:43:36 -0500108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109#ifdef CONFIG_SECURITY_SELINUX_DEVELOP
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500110static int selinux_enforcing_boot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112static int __init enforcing_setup(char *str)
113{
Eric Parisf5269712008-05-14 11:27:45 -0400114 unsigned long enforcing;
Jingoo Han29707b22014-02-05 15:13:14 +0900115 if (!kstrtoul(str, 0, &enforcing))
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500116 selinux_enforcing_boot = enforcing ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return 1;
118}
119__setup("enforcing=", enforcing_setup);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500120#else
121#define selinux_enforcing_boot 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122#endif
123
124#ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM
125int selinux_enabled = CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE;
126
127static int __init selinux_enabled_setup(char *str)
128{
Eric Parisf5269712008-05-14 11:27:45 -0400129 unsigned long enabled;
Jingoo Han29707b22014-02-05 15:13:14 +0900130 if (!kstrtoul(str, 0, &enabled))
Eric Parisf5269712008-05-14 11:27:45 -0400131 selinux_enabled = enabled ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 return 1;
133}
134__setup("selinux=", selinux_enabled_setup);
Stephen Smalley30d55282006-05-03 10:52:36 -0400135#else
136int selinux_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137#endif
138
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500139static unsigned int selinux_checkreqprot_boot =
140 CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE;
141
142static int __init checkreqprot_setup(char *str)
143{
144 unsigned long checkreqprot;
145
146 if (!kstrtoul(str, 0, &checkreqprot))
147 selinux_checkreqprot_boot = checkreqprot ? 1 : 0;
148 return 1;
149}
150__setup("checkreqprot=", checkreqprot_setup);
151
Christoph Lametere18b8902006-12-06 20:33:20 -0800152static struct kmem_cache *sel_inode_cache;
Sangwoo63205652015-10-21 17:44:30 -0400153static struct kmem_cache *file_security_cache;
James Morris7cae7e22006-03-22 00:09:22 -0800154
Paul Moored621d352008-01-29 08:43:36 -0500155/**
156 * selinux_secmark_enabled - Check to see if SECMARK is currently enabled
157 *
158 * Description:
159 * This function checks the SECMARK reference counter to see if any SECMARK
160 * targets are currently configured, if the reference counter is greater than
161 * zero SECMARK is considered to be enabled. Returns true (1) if SECMARK is
Chris PeBenito2be4d742013-05-03 09:05:39 -0400162 * enabled, false (0) if SECMARK is disabled. If the always_check_network
163 * policy capability is enabled, SECMARK is always considered enabled.
Paul Moored621d352008-01-29 08:43:36 -0500164 *
165 */
166static int selinux_secmark_enabled(void)
167{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500168 return (selinux_policycap_alwaysnetwork() ||
169 atomic_read(&selinux_secmark_refcount));
Chris PeBenito2be4d742013-05-03 09:05:39 -0400170}
171
172/**
173 * selinux_peerlbl_enabled - Check to see if peer labeling is currently enabled
174 *
175 * Description:
176 * This function checks if NetLabel or labeled IPSEC is enabled. Returns true
177 * (1) if any are enabled or false (0) if neither are enabled. If the
178 * always_check_network policy capability is enabled, peer labeling
179 * is always considered enabled.
180 *
181 */
182static int selinux_peerlbl_enabled(void)
183{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500184 return (selinux_policycap_alwaysnetwork() ||
185 netlbl_enabled() || selinux_xfrm_enabled());
Paul Moored621d352008-01-29 08:43:36 -0500186}
187
Paul Moore615e51f2014-06-26 14:33:56 -0400188static int selinux_netcache_avc_callback(u32 event)
189{
190 if (event == AVC_CALLBACK_RESET) {
191 sel_netif_flush();
192 sel_netnode_flush();
193 sel_netport_flush();
194 synchronize_net();
195 }
196 return 0;
197}
198
Daniel Jurgens8f408ab2017-05-19 15:48:53 +0300199static int selinux_lsm_notifier_avc_callback(u32 event)
200{
Daniel Jurgens409dcf32017-05-19 15:48:59 +0300201 if (event == AVC_CALLBACK_RESET) {
202 sel_ib_pkey_flush();
Daniel Jurgens8f408ab2017-05-19 15:48:53 +0300203 call_lsm_notifier(LSM_POLICY_CHANGE, NULL);
Daniel Jurgens409dcf32017-05-19 15:48:59 +0300204 }
Daniel Jurgens8f408ab2017-05-19 15:48:53 +0300205
206 return 0;
207}
208
David Howellsd84f4f92008-11-14 10:39:23 +1100209/*
210 * initialise the security for the init task
211 */
212static void cred_init_security(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
David Howells3b11a1d2008-11-14 10:39:26 +1100214 struct cred *cred = (struct cred *) current->real_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 struct task_security_struct *tsec;
216
James Morris89d155e2005-10-30 14:59:21 -0800217 tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 if (!tsec)
David Howellsd84f4f92008-11-14 10:39:23 +1100219 panic("SELinux: Failed to initialize initial task.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
David Howellsd84f4f92008-11-14 10:39:23 +1100221 tsec->osid = tsec->sid = SECINITSID_KERNEL;
David Howellsf1752ee2008-11-14 10:39:17 +1100222 cred->security = tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
David Howells275bb412008-11-14 10:39:19 +1100225/*
David Howells88e67f32008-11-14 10:39:21 +1100226 * get the security ID of a set of credentials
227 */
228static inline u32 cred_sid(const struct cred *cred)
229{
230 const struct task_security_struct *tsec;
231
232 tsec = cred->security;
233 return tsec->sid;
234}
235
236/*
David Howells3b11a1d2008-11-14 10:39:26 +1100237 * get the objective security ID of a task
David Howells275bb412008-11-14 10:39:19 +1100238 */
239static inline u32 task_sid(const struct task_struct *task)
240{
David Howells275bb412008-11-14 10:39:19 +1100241 u32 sid;
242
243 rcu_read_lock();
David Howells88e67f32008-11-14 10:39:21 +1100244 sid = cred_sid(__task_cred(task));
David Howells275bb412008-11-14 10:39:19 +1100245 rcu_read_unlock();
246 return sid;
247}
248
David Howells88e67f32008-11-14 10:39:21 +1100249/* Allocate and free functions for each kind of security blob. */
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251static int inode_alloc_security(struct inode *inode)
252{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 struct inode_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +1100254 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Josef Bacika02fe132008-04-04 09:35:05 +1100256 isec = kmem_cache_zalloc(sel_inode_cache, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 if (!isec)
258 return -ENOMEM;
259
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +0100260 spin_lock_init(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 INIT_LIST_HEAD(&isec->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 isec->inode = inode;
263 isec->sid = SECINITSID_UNLABELED;
264 isec->sclass = SECCLASS_FILE;
David Howells275bb412008-11-14 10:39:19 +1100265 isec->task_sid = sid;
Andreas Gruenbacher42059112016-11-10 22:18:27 +0100266 isec->initialized = LABEL_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 inode->i_security = isec;
268
269 return 0;
270}
271
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500272static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry);
273
274/*
275 * Try reloading inode security labels that have been marked as invalid. The
276 * @may_sleep parameter indicates when sleeping and thus reloading labels is
Andreas Gruenbacher42059112016-11-10 22:18:27 +0100277 * allowed; when set to false, returns -ECHILD when the label is
Al Viroe9193282018-04-24 21:31:02 -0400278 * invalid. The @dentry parameter should be set to a dentry of the inode.
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500279 */
280static int __inode_security_revalidate(struct inode *inode,
Al Viroe9193282018-04-24 21:31:02 -0400281 struct dentry *dentry,
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500282 bool may_sleep)
283{
284 struct inode_security_struct *isec = inode->i_security;
285
286 might_sleep_if(may_sleep);
287
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500288 if (selinux_state.initialized &&
289 isec->initialized != LABEL_INITIALIZED) {
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500290 if (!may_sleep)
291 return -ECHILD;
292
293 /*
294 * Try reloading the inode security label. This will fail if
295 * @opt_dentry is NULL and no dentry for this inode can be
296 * found; in that case, continue using the old label.
297 */
Al Viroe9193282018-04-24 21:31:02 -0400298 inode_doinit_with_dentry(inode, dentry);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500299 }
300 return 0;
301}
302
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500303static struct inode_security_struct *inode_security_novalidate(struct inode *inode)
304{
305 return inode->i_security;
306}
307
308static struct inode_security_struct *inode_security_rcu(struct inode *inode, bool rcu)
309{
310 int error;
311
312 error = __inode_security_revalidate(inode, NULL, !rcu);
313 if (error)
314 return ERR_PTR(error);
315 return inode->i_security;
316}
317
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500318/*
319 * Get the security label of an inode.
320 */
321static struct inode_security_struct *inode_security(struct inode *inode)
322{
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500323 __inode_security_revalidate(inode, NULL, true);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500324 return inode->i_security;
325}
326
Paul Moore2c971652016-04-19 16:36:28 -0400327static struct inode_security_struct *backing_inode_security_novalidate(struct dentry *dentry)
328{
329 struct inode *inode = d_backing_inode(dentry);
330
331 return inode->i_security;
332}
333
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500334/*
335 * Get the security label of a dentry's backing inode.
336 */
337static struct inode_security_struct *backing_inode_security(struct dentry *dentry)
338{
339 struct inode *inode = d_backing_inode(dentry);
340
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500341 __inode_security_revalidate(inode, dentry, true);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500342 return inode->i_security;
343}
344
Steven Rostedt3dc91d42014-01-09 21:46:34 -0500345static void inode_free_rcu(struct rcu_head *head)
346{
347 struct inode_security_struct *isec;
348
349 isec = container_of(head, struct inode_security_struct, rcu);
350 kmem_cache_free(sel_inode_cache, isec);
351}
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353static void inode_free_security(struct inode *inode)
354{
355 struct inode_security_struct *isec = inode->i_security;
356 struct superblock_security_struct *sbsec = inode->i_sb->s_security;
357
Waiman Long9629d042015-07-10 17:19:56 -0400358 /*
359 * As not all inode security structures are in a list, we check for
360 * empty list outside of the lock to make sure that we won't waste
361 * time taking a lock doing nothing.
362 *
363 * The list_del_init() function can be safely called more than once.
364 * It should not be possible for this function to be called with
365 * concurrent list_add(), but for better safety against future changes
366 * in the code, we use list_empty_careful() here.
367 */
368 if (!list_empty_careful(&isec->list)) {
369 spin_lock(&sbsec->isec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 list_del_init(&isec->list);
Waiman Long9629d042015-07-10 17:19:56 -0400371 spin_unlock(&sbsec->isec_lock);
372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Steven Rostedt3dc91d42014-01-09 21:46:34 -0500374 /*
375 * The inode may still be referenced in a path walk and
376 * a call to selinux_inode_permission() can be made
377 * after inode_free_security() is called. Ideally, the VFS
378 * wouldn't do this, but fixing that is a much harder
379 * job. For now, simply free the i_security via RCU, and
380 * leave the current inode->i_security pointer intact.
381 * The inode will be freed after the RCU grace period too.
382 */
383 call_rcu(&isec->rcu, inode_free_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
385
386static int file_alloc_security(struct file *file)
387{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 struct file_security_struct *fsec;
David Howells275bb412008-11-14 10:39:19 +1100389 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Sangwoo63205652015-10-21 17:44:30 -0400391 fsec = kmem_cache_zalloc(file_security_cache, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 if (!fsec)
393 return -ENOMEM;
394
David Howells275bb412008-11-14 10:39:19 +1100395 fsec->sid = sid;
396 fsec->fown_sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 file->f_security = fsec;
398
399 return 0;
400}
401
402static void file_free_security(struct file *file)
403{
404 struct file_security_struct *fsec = file->f_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 file->f_security = NULL;
Sangwoo63205652015-10-21 17:44:30 -0400406 kmem_cache_free(file_security_cache, fsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
409static int superblock_alloc_security(struct super_block *sb)
410{
411 struct superblock_security_struct *sbsec;
412
James Morris89d155e2005-10-30 14:59:21 -0800413 sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (!sbsec)
415 return -ENOMEM;
416
Eric Parisbc7e9822006-09-25 23:32:02 -0700417 mutex_init(&sbsec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 INIT_LIST_HEAD(&sbsec->isec_head);
419 spin_lock_init(&sbsec->isec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 sbsec->sb = sb;
421 sbsec->sid = SECINITSID_UNLABELED;
422 sbsec->def_sid = SECINITSID_FILE;
Eric Parisc312feb2006-07-10 04:43:53 -0700423 sbsec->mntpoint_sid = SECINITSID_UNLABELED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 sb->s_security = sbsec;
425
426 return 0;
427}
428
429static void superblock_free_security(struct super_block *sb)
430{
431 struct superblock_security_struct *sbsec = sb->s_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 sb->s_security = NULL;
433 kfree(sbsec);
434}
435
Al Virobd323652018-12-13 15:04:59 -0500436struct selinux_mnt_opts {
437 const char *fscontext, *context, *rootcontext, *defcontext;
438};
439
Al Viro204cc0c2018-12-13 13:41:47 -0500440static void selinux_free_mnt_opts(void *mnt_opts)
441{
Al Virobd323652018-12-13 15:04:59 -0500442 struct selinux_mnt_opts *opts = mnt_opts;
443 kfree(opts->fscontext);
444 kfree(opts->context);
445 kfree(opts->rootcontext);
446 kfree(opts->defcontext);
Al Viro204cc0c2018-12-13 13:41:47 -0500447 kfree(opts);
448}
449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450static inline int inode_doinit(struct inode *inode)
451{
452 return inode_doinit_with_dentry(inode, NULL);
453}
454
455enum {
Eric Paris31e87932007-09-19 17:19:12 -0400456 Opt_error = -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 Opt_context = 1,
458 Opt_fscontext = 2,
Eric Parisc9180a52007-11-30 13:00:35 -0500459 Opt_defcontext = 3,
460 Opt_rootcontext = 4,
David P. Quigley11689d42009-01-16 09:22:03 -0500461 Opt_labelsupport = 5,
Eric Parisd355987f2012-08-24 15:58:53 -0400462 Opt_nextmntopt = 6,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463};
464
Eric Parisd355987f2012-08-24 15:58:53 -0400465#define NUM_SEL_MNT_OPTS (Opt_nextmntopt - 1)
466
Steven Whitehousea447c092008-10-13 10:46:57 +0100467static const match_table_t tokens = {
Eric Paris832cbd92008-04-01 13:24:09 -0400468 {Opt_context, CONTEXT_STR "%s"},
469 {Opt_fscontext, FSCONTEXT_STR "%s"},
470 {Opt_defcontext, DEFCONTEXT_STR "%s"},
471 {Opt_rootcontext, ROOTCONTEXT_STR "%s"},
David P. Quigley11689d42009-01-16 09:22:03 -0500472 {Opt_labelsupport, LABELSUPP_STR},
Eric Paris31e87932007-09-19 17:19:12 -0400473 {Opt_error, NULL},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474};
475
476#define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n"
477
Eric Parisc312feb2006-07-10 04:43:53 -0700478static int may_context_mount_sb_relabel(u32 sid,
479 struct superblock_security_struct *sbsec,
David Howells275bb412008-11-14 10:39:19 +1100480 const struct cred *cred)
Eric Parisc312feb2006-07-10 04:43:53 -0700481{
David Howells275bb412008-11-14 10:39:19 +1100482 const struct task_security_struct *tsec = cred->security;
Eric Parisc312feb2006-07-10 04:43:53 -0700483 int rc;
484
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500485 rc = avc_has_perm(&selinux_state,
486 tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
Eric Parisc312feb2006-07-10 04:43:53 -0700487 FILESYSTEM__RELABELFROM, NULL);
488 if (rc)
489 return rc;
490
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500491 rc = avc_has_perm(&selinux_state,
492 tsec->sid, sid, SECCLASS_FILESYSTEM,
Eric Parisc312feb2006-07-10 04:43:53 -0700493 FILESYSTEM__RELABELTO, NULL);
494 return rc;
495}
496
Eric Paris08089252006-07-10 04:43:55 -0700497static int may_context_mount_inode_relabel(u32 sid,
498 struct superblock_security_struct *sbsec,
David Howells275bb412008-11-14 10:39:19 +1100499 const struct cred *cred)
Eric Paris08089252006-07-10 04:43:55 -0700500{
David Howells275bb412008-11-14 10:39:19 +1100501 const struct task_security_struct *tsec = cred->security;
Eric Paris08089252006-07-10 04:43:55 -0700502 int rc;
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500503 rc = avc_has_perm(&selinux_state,
504 tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
Eric Paris08089252006-07-10 04:43:55 -0700505 FILESYSTEM__RELABELFROM, NULL);
506 if (rc)
507 return rc;
508
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500509 rc = avc_has_perm(&selinux_state,
510 sid, sbsec->sid, SECCLASS_FILESYSTEM,
Eric Paris08089252006-07-10 04:43:55 -0700511 FILESYSTEM__ASSOCIATE, NULL);
512 return rc;
513}
514
Eric Parisb43e7252012-10-10 14:27:35 -0400515static int selinux_is_sblabel_mnt(struct super_block *sb)
516{
517 struct superblock_security_struct *sbsec = sb->s_security;
518
Mark Salyzynd5f3a5f2015-02-04 11:34:30 -0500519 return sbsec->behavior == SECURITY_FS_USE_XATTR ||
520 sbsec->behavior == SECURITY_FS_USE_TRANS ||
521 sbsec->behavior == SECURITY_FS_USE_TASK ||
J. Bruce Fields9fc2b4b2015-06-04 15:57:25 -0400522 sbsec->behavior == SECURITY_FS_USE_NATIVE ||
Mark Salyzynd5f3a5f2015-02-04 11:34:30 -0500523 /* Special handling. Genfs but also in-core setxattr handler */
524 !strcmp(sb->s_type->name, "sysfs") ||
525 !strcmp(sb->s_type->name, "pstore") ||
526 !strcmp(sb->s_type->name, "debugfs") ||
Yongqin Liua2c7c6f2017-01-09 10:07:30 -0500527 !strcmp(sb->s_type->name, "tracefs") ||
Stephen Smalley2651225b2017-02-28 10:35:56 -0500528 !strcmp(sb->s_type->name, "rootfs") ||
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500529 (selinux_policycap_cgroupseclabel() &&
Stephen Smalley2651225b2017-02-28 10:35:56 -0500530 (!strcmp(sb->s_type->name, "cgroup") ||
531 !strcmp(sb->s_type->name, "cgroup2")));
Eric Parisb43e7252012-10-10 14:27:35 -0400532}
533
Eric Parisc9180a52007-11-30 13:00:35 -0500534static int sb_finish_set_opts(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
536 struct superblock_security_struct *sbsec = sb->s_security;
537 struct dentry *root = sb->s_root;
David Howellsc6f493d2015-03-17 22:26:22 +0000538 struct inode *root_inode = d_backing_inode(root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 int rc = 0;
540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
542 /* Make sure that the xattr handler exists and that no
543 error other than -ENODATA is returned by getxattr on
544 the root directory. -ENODATA is ok, as this may be
545 the first boot of the SELinux kernel before we have
546 assigned xattr values to the filesystem. */
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200547 if (!(root_inode->i_opflags & IOP_XATTR)) {
peter enderborgc103a912018-06-12 10:09:03 +0200548 pr_warn("SELinux: (dev %s, type %s) has no "
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800549 "xattr support\n", sb->s_id, sb->s_type->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 rc = -EOPNOTSUPP;
551 goto out;
552 }
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200553
554 rc = __vfs_getxattr(root, root_inode, XATTR_NAME_SELINUX, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 if (rc < 0 && rc != -ENODATA) {
556 if (rc == -EOPNOTSUPP)
peter enderborgc103a912018-06-12 10:09:03 +0200557 pr_warn("SELinux: (dev %s, type "
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800558 "%s) has no security xattr handler\n",
559 sb->s_id, sb->s_type->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 else
peter enderborgc103a912018-06-12 10:09:03 +0200561 pr_warn("SELinux: (dev %s, type "
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800562 "%s) getxattr errno %d\n", sb->s_id,
563 sb->s_type->name, -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 goto out;
565 }
566 }
567
Eric Pariseadcabc2012-08-24 15:59:14 -0400568 sbsec->flags |= SE_SBINITIALIZED;
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400569
570 /*
571 * Explicitly set or clear SBLABEL_MNT. It's not sufficient to simply
572 * leave the flag untouched because sb_clone_mnt_opts might be handing
573 * us a superblock that needs the flag to be cleared.
574 */
Eric Parisb43e7252012-10-10 14:27:35 -0400575 if (selinux_is_sblabel_mnt(sb))
Eric Paris12f348b2012-10-09 10:56:25 -0400576 sbsec->flags |= SBLABEL_MNT;
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400577 else
578 sbsec->flags &= ~SBLABEL_MNT;
David P. Quigleyddd29ec2009-09-09 14:25:37 -0400579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 /* Initialize the root inode. */
Eric Parisc9180a52007-11-30 13:00:35 -0500581 rc = inode_doinit_with_dentry(root_inode, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 /* Initialize any other inodes associated with the superblock, e.g.
584 inodes created prior to initial policy load or inodes created
585 during get_sb by a pseudo filesystem that directly
586 populates itself. */
587 spin_lock(&sbsec->isec_lock);
Al Viro8d641242018-12-10 15:34:12 -0500588 while (!list_empty(&sbsec->isec_head)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 struct inode_security_struct *isec =
Al Viro8d641242018-12-10 15:34:12 -0500590 list_first_entry(&sbsec->isec_head,
Eric Parisc9180a52007-11-30 13:00:35 -0500591 struct inode_security_struct, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 struct inode *inode = isec->inode;
Stephen Smalley923190d2014-10-06 16:32:52 -0400593 list_del_init(&isec->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 spin_unlock(&sbsec->isec_lock);
595 inode = igrab(inode);
596 if (inode) {
Eric Parisc9180a52007-11-30 13:00:35 -0500597 if (!IS_PRIVATE(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 inode_doinit(inode);
599 iput(inode);
600 }
601 spin_lock(&sbsec->isec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
603 spin_unlock(&sbsec->isec_lock);
604out:
Eric Parisc9180a52007-11-30 13:00:35 -0500605 return rc;
606}
607
Eric Parisc9180a52007-11-30 13:00:35 -0500608static int bad_option(struct superblock_security_struct *sbsec, char flag,
609 u32 old_sid, u32 new_sid)
610{
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500611 char mnt_flags = sbsec->flags & SE_MNTMASK;
612
Eric Parisc9180a52007-11-30 13:00:35 -0500613 /* check if the old mount command had the same options */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500614 if (sbsec->flags & SE_SBINITIALIZED)
Eric Parisc9180a52007-11-30 13:00:35 -0500615 if (!(sbsec->flags & flag) ||
616 (old_sid != new_sid))
617 return 1;
618
619 /* check if we were passed the same options twice,
620 * aka someone passed context=a,context=b
621 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500622 if (!(sbsec->flags & SE_SBINITIALIZED))
623 if (mnt_flags & flag)
Eric Parisc9180a52007-11-30 13:00:35 -0500624 return 1;
625 return 0;
626}
Eric Parise0007522008-03-05 10:31:54 -0500627
Al Virobd323652018-12-13 15:04:59 -0500628static int parse_sid(struct super_block *sb, const char *s, u32 *sid)
629{
630 int rc = security_context_str_to_sid(&selinux_state, s,
631 sid, GFP_KERNEL);
632 if (rc)
633 pr_warn("SELinux: security_context_str_to_sid"
634 "(%s) failed for (dev %s, type %s) errno=%d\n",
635 s, sb->s_id, sb->s_type->name, rc);
636 return rc;
637}
638
Eric Parisc9180a52007-11-30 13:00:35 -0500639/*
640 * Allow filesystems with binary mount data to explicitly set mount point
641 * labeling information.
642 */
Eric Parise0007522008-03-05 10:31:54 -0500643static int selinux_set_mnt_opts(struct super_block *sb,
Al Viro204cc0c2018-12-13 13:41:47 -0500644 void *mnt_opts,
David Quigley649f6e72013-05-22 12:50:36 -0400645 unsigned long kern_flags,
646 unsigned long *set_kern_flags)
Eric Parisc9180a52007-11-30 13:00:35 -0500647{
David Howells275bb412008-11-14 10:39:19 +1100648 const struct cred *cred = current_cred();
Eric Parisc9180a52007-11-30 13:00:35 -0500649 struct superblock_security_struct *sbsec = sb->s_security;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500650 struct dentry *root = sbsec->sb->s_root;
Al Virobd323652018-12-13 15:04:59 -0500651 struct selinux_mnt_opts *opts = mnt_opts;
Paul Moore2c971652016-04-19 16:36:28 -0400652 struct inode_security_struct *root_isec;
Eric Parisc9180a52007-11-30 13:00:35 -0500653 u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
654 u32 defcontext_sid = 0;
Al Virobd323652018-12-13 15:04:59 -0500655 int rc = 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500656
657 mutex_lock(&sbsec->lock);
658
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500659 if (!selinux_state.initialized) {
Al Virobd323652018-12-13 15:04:59 -0500660 if (!opts) {
Eric Parisc9180a52007-11-30 13:00:35 -0500661 /* Defer initialization until selinux_complete_init,
662 after the initial policy is loaded and the security
663 server is ready to handle calls. */
Eric Parisc9180a52007-11-30 13:00:35 -0500664 goto out;
665 }
666 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +0200667 pr_warn("SELinux: Unable to set superblock options "
Eric Paris744ba352008-04-17 11:52:44 -0400668 "before the security server is initialized\n");
Eric Parisc9180a52007-11-30 13:00:35 -0500669 goto out;
670 }
David Quigley649f6e72013-05-22 12:50:36 -0400671 if (kern_flags && !set_kern_flags) {
672 /* Specifying internal flags without providing a place to
673 * place the results is not allowed */
674 rc = -EINVAL;
675 goto out;
676 }
Eric Parisc9180a52007-11-30 13:00:35 -0500677
678 /*
Eric Parise0007522008-03-05 10:31:54 -0500679 * Binary mount data FS will come through this function twice. Once
680 * from an explicit call and once from the generic calls from the vfs.
681 * Since the generic VFS calls will not contain any security mount data
682 * we need to skip the double mount verification.
683 *
684 * This does open a hole in which we will not notice if the first
685 * mount using this sb set explict options and a second mount using
686 * this sb does not set any security options. (The first options
687 * will be used for both mounts)
688 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500689 if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
Al Virobd323652018-12-13 15:04:59 -0500690 && !opts)
Eric Parisf5269712008-05-14 11:27:45 -0400691 goto out;
Eric Parise0007522008-03-05 10:31:54 -0500692
Paul Moore2c971652016-04-19 16:36:28 -0400693 root_isec = backing_inode_security_novalidate(root);
694
Eric Parise0007522008-03-05 10:31:54 -0500695 /*
Eric Parisc9180a52007-11-30 13:00:35 -0500696 * parse the mount options, check if they are valid sids.
697 * also check if someone is trying to mount the same sb more
698 * than once with different security options.
699 */
Al Virobd323652018-12-13 15:04:59 -0500700 if (opts) {
701 if (opts->fscontext) {
702 rc = parse_sid(sb, opts->fscontext, &fscontext_sid);
703 if (rc)
704 goto out;
Eric Parisc9180a52007-11-30 13:00:35 -0500705 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
706 fscontext_sid))
707 goto out_double_mount;
Eric Parisc9180a52007-11-30 13:00:35 -0500708 sbsec->flags |= FSCONTEXT_MNT;
Al Virobd323652018-12-13 15:04:59 -0500709 }
710 if (opts->context) {
711 rc = parse_sid(sb, opts->context, &context_sid);
712 if (rc)
713 goto out;
Eric Parisc9180a52007-11-30 13:00:35 -0500714 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
715 context_sid))
716 goto out_double_mount;
Eric Parisc9180a52007-11-30 13:00:35 -0500717 sbsec->flags |= CONTEXT_MNT;
Al Virobd323652018-12-13 15:04:59 -0500718 }
719 if (opts->rootcontext) {
720 rc = parse_sid(sb, opts->rootcontext, &rootcontext_sid);
721 if (rc)
722 goto out;
Eric Parisc9180a52007-11-30 13:00:35 -0500723 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
724 rootcontext_sid))
725 goto out_double_mount;
Eric Parisc9180a52007-11-30 13:00:35 -0500726 sbsec->flags |= ROOTCONTEXT_MNT;
Al Virobd323652018-12-13 15:04:59 -0500727 }
728 if (opts->defcontext) {
729 rc = parse_sid(sb, opts->defcontext, &defcontext_sid);
730 if (rc)
731 goto out;
Eric Parisc9180a52007-11-30 13:00:35 -0500732 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
733 defcontext_sid))
734 goto out_double_mount;
Eric Parisc9180a52007-11-30 13:00:35 -0500735 sbsec->flags |= DEFCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500736 }
737 }
738
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500739 if (sbsec->flags & SE_SBINITIALIZED) {
Eric Parisc9180a52007-11-30 13:00:35 -0500740 /* previously mounted with options, but not on this attempt? */
Al Virobd323652018-12-13 15:04:59 -0500741 if ((sbsec->flags & SE_MNTMASK) && !opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500742 goto out_double_mount;
743 rc = 0;
744 goto out;
745 }
746
James Morris089be432008-07-15 18:32:49 +1000747 if (strcmp(sb->s_type->name, "proc") == 0)
Stephen Smalley134509d2015-06-04 16:22:17 -0400748 sbsec->flags |= SE_SBPROC | SE_SBGENFS;
749
Stephen Smalley8e014722015-06-04 16:22:17 -0400750 if (!strcmp(sb->s_type->name, "debugfs") ||
Jeff Vander Stoep6a391182017-06-20 09:35:33 -0700751 !strcmp(sb->s_type->name, "tracefs") ||
Stephen Smalley8e014722015-06-04 16:22:17 -0400752 !strcmp(sb->s_type->name, "sysfs") ||
Antonio Murdaca901ef842017-02-09 17:02:42 +0100753 !strcmp(sb->s_type->name, "pstore") ||
754 !strcmp(sb->s_type->name, "cgroup") ||
755 !strcmp(sb->s_type->name, "cgroup2"))
Stephen Smalley134509d2015-06-04 16:22:17 -0400756 sbsec->flags |= SE_SBGENFS;
Eric Parisc9180a52007-11-30 13:00:35 -0500757
David Quigleyeb9ae682013-05-22 12:50:37 -0400758 if (!sbsec->behavior) {
759 /*
760 * Determine the labeling behavior to use for this
761 * filesystem type.
762 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500763 rc = security_fs_use(&selinux_state, sb);
David Quigleyeb9ae682013-05-22 12:50:37 -0400764 if (rc) {
peter enderborgc103a912018-06-12 10:09:03 +0200765 pr_warn("%s: security_fs_use(%s) returned %d\n",
David Quigleyeb9ae682013-05-22 12:50:37 -0400766 __func__, sb->s_type->name, rc);
767 goto out;
768 }
Eric Parisc9180a52007-11-30 13:00:35 -0500769 }
Seth Forsheeaad82892016-04-26 14:36:20 -0500770
771 /*
Stephen Smalley01593d32017-01-09 10:07:31 -0500772 * If this is a user namespace mount and the filesystem type is not
773 * explicitly whitelisted, then no contexts are allowed on the command
774 * line and security labels must be ignored.
Seth Forsheeaad82892016-04-26 14:36:20 -0500775 */
Stephen Smalley01593d32017-01-09 10:07:31 -0500776 if (sb->s_user_ns != &init_user_ns &&
777 strcmp(sb->s_type->name, "tmpfs") &&
778 strcmp(sb->s_type->name, "ramfs") &&
779 strcmp(sb->s_type->name, "devpts")) {
Seth Forsheeaad82892016-04-26 14:36:20 -0500780 if (context_sid || fscontext_sid || rootcontext_sid ||
781 defcontext_sid) {
782 rc = -EACCES;
783 goto out;
784 }
785 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
786 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500787 rc = security_transition_sid(&selinux_state,
788 current_sid(),
789 current_sid(),
Seth Forsheeaad82892016-04-26 14:36:20 -0500790 SECCLASS_FILE, NULL,
791 &sbsec->mntpoint_sid);
792 if (rc)
793 goto out;
794 }
795 goto out_set_opts;
796 }
797
Eric Parisc9180a52007-11-30 13:00:35 -0500798 /* sets the context of the superblock for the fs being mounted. */
799 if (fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100800 rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500801 if (rc)
802 goto out;
803
804 sbsec->sid = fscontext_sid;
805 }
806
807 /*
808 * Switch to using mount point labeling behavior.
809 * sets the label used on all file below the mountpoint, and will set
810 * the superblock context if not already set.
811 */
David Quigleyeb9ae682013-05-22 12:50:37 -0400812 if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !context_sid) {
813 sbsec->behavior = SECURITY_FS_USE_NATIVE;
814 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
815 }
816
Eric Parisc9180a52007-11-30 13:00:35 -0500817 if (context_sid) {
818 if (!fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100819 rc = may_context_mount_sb_relabel(context_sid, sbsec,
820 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500821 if (rc)
822 goto out;
823 sbsec->sid = context_sid;
824 } else {
David Howells275bb412008-11-14 10:39:19 +1100825 rc = may_context_mount_inode_relabel(context_sid, sbsec,
826 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500827 if (rc)
828 goto out;
829 }
830 if (!rootcontext_sid)
831 rootcontext_sid = context_sid;
832
833 sbsec->mntpoint_sid = context_sid;
834 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
835 }
836
837 if (rootcontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100838 rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec,
839 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500840 if (rc)
841 goto out;
842
843 root_isec->sid = rootcontext_sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -0500844 root_isec->initialized = LABEL_INITIALIZED;
Eric Parisc9180a52007-11-30 13:00:35 -0500845 }
846
847 if (defcontext_sid) {
David Quigleyeb9ae682013-05-22 12:50:37 -0400848 if (sbsec->behavior != SECURITY_FS_USE_XATTR &&
849 sbsec->behavior != SECURITY_FS_USE_NATIVE) {
Eric Parisc9180a52007-11-30 13:00:35 -0500850 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +0200851 pr_warn("SELinux: defcontext option is "
Eric Parisc9180a52007-11-30 13:00:35 -0500852 "invalid for this filesystem type\n");
853 goto out;
854 }
855
856 if (defcontext_sid != sbsec->def_sid) {
857 rc = may_context_mount_inode_relabel(defcontext_sid,
David Howells275bb412008-11-14 10:39:19 +1100858 sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500859 if (rc)
860 goto out;
861 }
862
863 sbsec->def_sid = defcontext_sid;
864 }
865
Seth Forsheeaad82892016-04-26 14:36:20 -0500866out_set_opts:
Eric Parisc9180a52007-11-30 13:00:35 -0500867 rc = sb_finish_set_opts(sb);
868out:
Eric Parisbc7e9822006-09-25 23:32:02 -0700869 mutex_unlock(&sbsec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 return rc;
Eric Parisc9180a52007-11-30 13:00:35 -0500871out_double_mount:
872 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +0200873 pr_warn("SELinux: mount invalid. Same superblock, different "
Al Virobd323652018-12-13 15:04:59 -0500874 "security settings for (dev %s, type %s)\n", sb->s_id,
875 sb->s_type->name);
Eric Parisc9180a52007-11-30 13:00:35 -0500876 goto out;
877}
878
Jeff Layton094f7b62013-04-01 08:14:24 -0400879static int selinux_cmp_sb_context(const struct super_block *oldsb,
880 const struct super_block *newsb)
881{
882 struct superblock_security_struct *old = oldsb->s_security;
883 struct superblock_security_struct *new = newsb->s_security;
884 char oldflags = old->flags & SE_MNTMASK;
885 char newflags = new->flags & SE_MNTMASK;
886
887 if (oldflags != newflags)
888 goto mismatch;
889 if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid)
890 goto mismatch;
891 if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid)
892 goto mismatch;
893 if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid)
894 goto mismatch;
895 if (oldflags & ROOTCONTEXT_MNT) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500896 struct inode_security_struct *oldroot = backing_inode_security(oldsb->s_root);
897 struct inode_security_struct *newroot = backing_inode_security(newsb->s_root);
Jeff Layton094f7b62013-04-01 08:14:24 -0400898 if (oldroot->sid != newroot->sid)
899 goto mismatch;
900 }
901 return 0;
902mismatch:
peter enderborgc103a912018-06-12 10:09:03 +0200903 pr_warn("SELinux: mount invalid. Same superblock, "
Jeff Layton094f7b62013-04-01 08:14:24 -0400904 "different security settings for (dev %s, "
905 "type %s)\n", newsb->s_id, newsb->s_type->name);
906 return -EBUSY;
907}
908
909static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400910 struct super_block *newsb,
911 unsigned long kern_flags,
912 unsigned long *set_kern_flags)
Eric Parisc9180a52007-11-30 13:00:35 -0500913{
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400914 int rc = 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500915 const struct superblock_security_struct *oldsbsec = oldsb->s_security;
916 struct superblock_security_struct *newsbsec = newsb->s_security;
917
918 int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT);
919 int set_context = (oldsbsec->flags & CONTEXT_MNT);
920 int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT);
921
Eric Paris0f5e6422008-04-21 16:24:11 -0400922 /*
923 * if the parent was able to be mounted it clearly had no special lsm
Al Viroe8c26252010-03-23 06:36:54 -0400924 * mount options. thus we can safely deal with this superblock later
Eric Paris0f5e6422008-04-21 16:24:11 -0400925 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500926 if (!selinux_state.initialized)
Jeff Layton094f7b62013-04-01 08:14:24 -0400927 return 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500928
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400929 /*
930 * Specifying internal flags without providing a place to
931 * place the results is not allowed.
932 */
933 if (kern_flags && !set_kern_flags)
934 return -EINVAL;
935
Eric Parisc9180a52007-11-30 13:00:35 -0500936 /* how can we clone if the old one wasn't set up?? */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500937 BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED));
Eric Parisc9180a52007-11-30 13:00:35 -0500938
Jeff Layton094f7b62013-04-01 08:14:24 -0400939 /* if fs is reusing a sb, make sure that the contexts match */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500940 if (newsbsec->flags & SE_SBINITIALIZED)
Jeff Layton094f7b62013-04-01 08:14:24 -0400941 return selinux_cmp_sb_context(oldsb, newsb);
Eric Paris5a552612008-04-09 14:08:35 -0400942
Eric Parisc9180a52007-11-30 13:00:35 -0500943 mutex_lock(&newsbsec->lock);
944
945 newsbsec->flags = oldsbsec->flags;
946
947 newsbsec->sid = oldsbsec->sid;
948 newsbsec->def_sid = oldsbsec->def_sid;
949 newsbsec->behavior = oldsbsec->behavior;
950
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400951 if (newsbsec->behavior == SECURITY_FS_USE_NATIVE &&
952 !(kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500953 rc = security_fs_use(&selinux_state, newsb);
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400954 if (rc)
955 goto out;
956 }
957
958 if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !set_context) {
959 newsbsec->behavior = SECURITY_FS_USE_NATIVE;
960 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
961 }
962
Eric Parisc9180a52007-11-30 13:00:35 -0500963 if (set_context) {
964 u32 sid = oldsbsec->mntpoint_sid;
965
966 if (!set_fscontext)
967 newsbsec->sid = sid;
968 if (!set_rootcontext) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500969 struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
Eric Parisc9180a52007-11-30 13:00:35 -0500970 newisec->sid = sid;
971 }
972 newsbsec->mntpoint_sid = sid;
973 }
974 if (set_rootcontext) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500975 const struct inode_security_struct *oldisec = backing_inode_security(oldsb->s_root);
976 struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
Eric Parisc9180a52007-11-30 13:00:35 -0500977
978 newisec->sid = oldisec->sid;
979 }
980
981 sb_finish_set_opts(newsb);
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400982out:
Eric Parisc9180a52007-11-30 13:00:35 -0500983 mutex_unlock(&newsbsec->lock);
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400984 return rc;
Eric Parisc9180a52007-11-30 13:00:35 -0500985}
986
Adrian Bunk2e1479d2008-03-17 22:29:23 +0200987static int selinux_parse_opts_str(char *options,
Al Viro204cc0c2018-12-13 13:41:47 -0500988 void **mnt_opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500989{
Al Virobd323652018-12-13 15:04:59 -0500990 struct selinux_mnt_opts *opts = *mnt_opts;
Eric Parise0007522008-03-05 10:31:54 -0500991 char *p;
Al Virobd323652018-12-13 15:04:59 -0500992 int rc;
Eric Parisc9180a52007-11-30 13:00:35 -0500993
994 /* Standard string-based options. */
995 while ((p = strsep(&options, "|")) != NULL) {
996 int token;
997 substring_t args[MAX_OPT_ARGS];
998
999 if (!*p)
1000 continue;
1001
1002 token = match_token(p, tokens, args);
1003
Al Virobd323652018-12-13 15:04:59 -05001004 if (!opts) {
1005 opts = kzalloc(sizeof(struct selinux_mnt_opts), GFP_KERNEL);
1006 if (!opts)
1007 return -ENOMEM;
1008 }
1009
Eric Parisc9180a52007-11-30 13:00:35 -05001010 switch (token) {
1011 case Opt_context:
Al Virobd323652018-12-13 15:04:59 -05001012 if (opts->context || opts->defcontext) {
Eric Parisc9180a52007-11-30 13:00:35 -05001013 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +02001014 pr_warn(SEL_MOUNT_FAIL_MSG);
Eric Parisc9180a52007-11-30 13:00:35 -05001015 goto out_err;
1016 }
Al Virobd323652018-12-13 15:04:59 -05001017 opts->context = match_strdup(&args[0]);
1018 if (!opts->context) {
Eric Parisc9180a52007-11-30 13:00:35 -05001019 rc = -ENOMEM;
1020 goto out_err;
1021 }
1022 break;
1023
1024 case Opt_fscontext:
Al Virobd323652018-12-13 15:04:59 -05001025 if (opts->fscontext) {
Eric Parisc9180a52007-11-30 13:00:35 -05001026 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +02001027 pr_warn(SEL_MOUNT_FAIL_MSG);
Eric Parisc9180a52007-11-30 13:00:35 -05001028 goto out_err;
1029 }
Al Virobd323652018-12-13 15:04:59 -05001030 opts->fscontext = match_strdup(&args[0]);
1031 if (!opts->fscontext) {
Eric Parisc9180a52007-11-30 13:00:35 -05001032 rc = -ENOMEM;
1033 goto out_err;
1034 }
1035 break;
1036
1037 case Opt_rootcontext:
Al Virobd323652018-12-13 15:04:59 -05001038 if (opts->rootcontext) {
Eric Parisc9180a52007-11-30 13:00:35 -05001039 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +02001040 pr_warn(SEL_MOUNT_FAIL_MSG);
Eric Parisc9180a52007-11-30 13:00:35 -05001041 goto out_err;
1042 }
Al Virobd323652018-12-13 15:04:59 -05001043 opts->rootcontext = match_strdup(&args[0]);
1044 if (!opts->rootcontext) {
Eric Parisc9180a52007-11-30 13:00:35 -05001045 rc = -ENOMEM;
1046 goto out_err;
1047 }
1048 break;
1049
1050 case Opt_defcontext:
Al Virobd323652018-12-13 15:04:59 -05001051 if (opts->context || opts->defcontext) {
Eric Parisc9180a52007-11-30 13:00:35 -05001052 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +02001053 pr_warn(SEL_MOUNT_FAIL_MSG);
Eric Parisc9180a52007-11-30 13:00:35 -05001054 goto out_err;
1055 }
Al Virobd323652018-12-13 15:04:59 -05001056 opts->defcontext = match_strdup(&args[0]);
1057 if (!opts->defcontext) {
Eric Parisc9180a52007-11-30 13:00:35 -05001058 rc = -ENOMEM;
1059 goto out_err;
1060 }
1061 break;
David P. Quigley11689d42009-01-16 09:22:03 -05001062 case Opt_labelsupport:
1063 break;
Eric Parisc9180a52007-11-30 13:00:35 -05001064 default:
1065 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +02001066 pr_warn("SELinux: unknown mount option\n");
Eric Parisc9180a52007-11-30 13:00:35 -05001067 goto out_err;
1068
1069 }
1070 }
Al Virobd323652018-12-13 15:04:59 -05001071 *mnt_opts = opts;
Eric Parise0007522008-03-05 10:31:54 -05001072 return 0;
1073
Eric Parisc9180a52007-11-30 13:00:35 -05001074out_err:
Al Virobd323652018-12-13 15:04:59 -05001075 if (opts)
1076 selinux_free_mnt_opts(opts);
Eric Parisc9180a52007-11-30 13:00:35 -05001077 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078}
1079
Al Viroe3489f82018-12-13 00:24:36 -05001080static int show_sid(struct seq_file *m, u32 sid)
Eric Paris2069f452008-07-04 09:47:13 +10001081{
Al Viroe3489f82018-12-13 00:24:36 -05001082 char *context = NULL;
1083 u32 len;
1084 int rc;
Eric Paris2069f452008-07-04 09:47:13 +10001085
Al Viroe3489f82018-12-13 00:24:36 -05001086 rc = security_sid_to_context(&selinux_state, sid,
1087 &context, &len);
1088 if (!rc) {
1089 bool has_comma = context && strchr(context, ',');
David P. Quigley11689d42009-01-16 09:22:03 -05001090
Eric Paris2069f452008-07-04 09:47:13 +10001091 if (has_comma)
1092 seq_putc(m, '\"');
Al Viroe3489f82018-12-13 00:24:36 -05001093 seq_escape(m, context, "\"\n\\");
Eric Paris2069f452008-07-04 09:47:13 +10001094 if (has_comma)
1095 seq_putc(m, '\"');
1096 }
Al Viroe3489f82018-12-13 00:24:36 -05001097 kfree(context);
1098 return rc;
Eric Paris2069f452008-07-04 09:47:13 +10001099}
1100
1101static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
1102{
Al Viroe3489f82018-12-13 00:24:36 -05001103 struct superblock_security_struct *sbsec = sb->s_security;
Eric Paris2069f452008-07-04 09:47:13 +10001104 int rc;
1105
Al Viroe3489f82018-12-13 00:24:36 -05001106 if (!(sbsec->flags & SE_SBINITIALIZED))
1107 return 0;
1108
1109 if (!selinux_state.initialized)
1110 return 0;
1111
1112 if (sbsec->flags & FSCONTEXT_MNT) {
1113 seq_putc(m, ',');
1114 seq_puts(m, FSCONTEXT_STR);
1115 rc = show_sid(m, sbsec->sid);
1116 if (rc)
1117 return rc;
Eric Paris383795c2008-07-29 17:07:26 -04001118 }
Al Viroe3489f82018-12-13 00:24:36 -05001119 if (sbsec->flags & CONTEXT_MNT) {
1120 seq_putc(m, ',');
1121 seq_puts(m, CONTEXT_STR);
1122 rc = show_sid(m, sbsec->mntpoint_sid);
1123 if (rc)
1124 return rc;
1125 }
1126 if (sbsec->flags & DEFCONTEXT_MNT) {
1127 seq_putc(m, ',');
1128 seq_puts(m, DEFCONTEXT_STR);
1129 rc = show_sid(m, sbsec->def_sid);
1130 if (rc)
1131 return rc;
1132 }
1133 if (sbsec->flags & ROOTCONTEXT_MNT) {
1134 struct dentry *root = sbsec->sb->s_root;
1135 struct inode_security_struct *isec = backing_inode_security(root);
1136 seq_putc(m, ',');
1137 seq_puts(m, ROOTCONTEXT_STR);
1138 rc = show_sid(m, isec->sid);
1139 if (rc)
1140 return rc;
1141 }
1142 if (sbsec->flags & SBLABEL_MNT) {
1143 seq_putc(m, ',');
1144 seq_puts(m, LABELSUPP_STR);
1145 }
1146 return 0;
Eric Paris2069f452008-07-04 09:47:13 +10001147}
1148
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149static inline u16 inode_mode_to_security_class(umode_t mode)
1150{
1151 switch (mode & S_IFMT) {
1152 case S_IFSOCK:
1153 return SECCLASS_SOCK_FILE;
1154 case S_IFLNK:
1155 return SECCLASS_LNK_FILE;
1156 case S_IFREG:
1157 return SECCLASS_FILE;
1158 case S_IFBLK:
1159 return SECCLASS_BLK_FILE;
1160 case S_IFDIR:
1161 return SECCLASS_DIR;
1162 case S_IFCHR:
1163 return SECCLASS_CHR_FILE;
1164 case S_IFIFO:
1165 return SECCLASS_FIFO_FILE;
1166
1167 }
1168
1169 return SECCLASS_FILE;
1170}
1171
James Morris13402582005-09-30 14:24:34 -04001172static inline int default_protocol_stream(int protocol)
1173{
1174 return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP);
1175}
1176
1177static inline int default_protocol_dgram(int protocol)
1178{
1179 return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
1180}
1181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182static inline u16 socket_type_to_security_class(int family, int type, int protocol)
1183{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001184 int extsockclass = selinux_policycap_extsockclass();
Stephen Smalleyda69a532017-01-09 10:07:30 -05001185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 switch (family) {
1187 case PF_UNIX:
1188 switch (type) {
1189 case SOCK_STREAM:
1190 case SOCK_SEQPACKET:
1191 return SECCLASS_UNIX_STREAM_SOCKET;
1192 case SOCK_DGRAM:
Luis Ressel2a764b52017-07-25 15:13:41 -04001193 case SOCK_RAW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 return SECCLASS_UNIX_DGRAM_SOCKET;
1195 }
1196 break;
1197 case PF_INET:
1198 case PF_INET6:
1199 switch (type) {
1200 case SOCK_STREAM:
Stephen Smalleyda69a532017-01-09 10:07:30 -05001201 case SOCK_SEQPACKET:
James Morris13402582005-09-30 14:24:34 -04001202 if (default_protocol_stream(protocol))
1203 return SECCLASS_TCP_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001204 else if (extsockclass && protocol == IPPROTO_SCTP)
1205 return SECCLASS_SCTP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001206 else
1207 return SECCLASS_RAWIP_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 case SOCK_DGRAM:
James Morris13402582005-09-30 14:24:34 -04001209 if (default_protocol_dgram(protocol))
1210 return SECCLASS_UDP_SOCKET;
Stephen Smalleyef379792017-01-09 10:07:31 -05001211 else if (extsockclass && (protocol == IPPROTO_ICMP ||
1212 protocol == IPPROTO_ICMPV6))
Stephen Smalleyda69a532017-01-09 10:07:30 -05001213 return SECCLASS_ICMP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001214 else
1215 return SECCLASS_RAWIP_SOCKET;
James Morris2ee92d42006-11-13 16:09:01 -08001216 case SOCK_DCCP:
1217 return SECCLASS_DCCP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001218 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 return SECCLASS_RAWIP_SOCKET;
1220 }
1221 break;
1222 case PF_NETLINK:
1223 switch (protocol) {
1224 case NETLINK_ROUTE:
1225 return SECCLASS_NETLINK_ROUTE_SOCKET;
Pavel Emelyanov7f1fb602011-12-06 07:56:43 +00001226 case NETLINK_SOCK_DIAG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 return SECCLASS_NETLINK_TCPDIAG_SOCKET;
1228 case NETLINK_NFLOG:
1229 return SECCLASS_NETLINK_NFLOG_SOCKET;
1230 case NETLINK_XFRM:
1231 return SECCLASS_NETLINK_XFRM_SOCKET;
1232 case NETLINK_SELINUX:
1233 return SECCLASS_NETLINK_SELINUX_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001234 case NETLINK_ISCSI:
1235 return SECCLASS_NETLINK_ISCSI_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 case NETLINK_AUDIT:
1237 return SECCLASS_NETLINK_AUDIT_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001238 case NETLINK_FIB_LOOKUP:
1239 return SECCLASS_NETLINK_FIB_LOOKUP_SOCKET;
1240 case NETLINK_CONNECTOR:
1241 return SECCLASS_NETLINK_CONNECTOR_SOCKET;
1242 case NETLINK_NETFILTER:
1243 return SECCLASS_NETLINK_NETFILTER_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 case NETLINK_DNRTMSG:
1245 return SECCLASS_NETLINK_DNRT_SOCKET;
James Morris0c9b7942005-04-16 15:24:13 -07001246 case NETLINK_KOBJECT_UEVENT:
1247 return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001248 case NETLINK_GENERIC:
1249 return SECCLASS_NETLINK_GENERIC_SOCKET;
1250 case NETLINK_SCSITRANSPORT:
1251 return SECCLASS_NETLINK_SCSITRANSPORT_SOCKET;
1252 case NETLINK_RDMA:
1253 return SECCLASS_NETLINK_RDMA_SOCKET;
1254 case NETLINK_CRYPTO:
1255 return SECCLASS_NETLINK_CRYPTO_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 default:
1257 return SECCLASS_NETLINK_SOCKET;
1258 }
1259 case PF_PACKET:
1260 return SECCLASS_PACKET_SOCKET;
1261 case PF_KEY:
1262 return SECCLASS_KEY_SOCKET;
Christopher J. PeBenito3e3ff152006-06-09 00:25:03 -07001263 case PF_APPLETALK:
1264 return SECCLASS_APPLETALK_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
1266
Stephen Smalleyda69a532017-01-09 10:07:30 -05001267 if (extsockclass) {
1268 switch (family) {
1269 case PF_AX25:
1270 return SECCLASS_AX25_SOCKET;
1271 case PF_IPX:
1272 return SECCLASS_IPX_SOCKET;
1273 case PF_NETROM:
1274 return SECCLASS_NETROM_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001275 case PF_ATMPVC:
1276 return SECCLASS_ATMPVC_SOCKET;
1277 case PF_X25:
1278 return SECCLASS_X25_SOCKET;
1279 case PF_ROSE:
1280 return SECCLASS_ROSE_SOCKET;
1281 case PF_DECnet:
1282 return SECCLASS_DECNET_SOCKET;
1283 case PF_ATMSVC:
1284 return SECCLASS_ATMSVC_SOCKET;
1285 case PF_RDS:
1286 return SECCLASS_RDS_SOCKET;
1287 case PF_IRDA:
1288 return SECCLASS_IRDA_SOCKET;
1289 case PF_PPPOX:
1290 return SECCLASS_PPPOX_SOCKET;
1291 case PF_LLC:
1292 return SECCLASS_LLC_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001293 case PF_CAN:
1294 return SECCLASS_CAN_SOCKET;
1295 case PF_TIPC:
1296 return SECCLASS_TIPC_SOCKET;
1297 case PF_BLUETOOTH:
1298 return SECCLASS_BLUETOOTH_SOCKET;
1299 case PF_IUCV:
1300 return SECCLASS_IUCV_SOCKET;
1301 case PF_RXRPC:
1302 return SECCLASS_RXRPC_SOCKET;
1303 case PF_ISDN:
1304 return SECCLASS_ISDN_SOCKET;
1305 case PF_PHONET:
1306 return SECCLASS_PHONET_SOCKET;
1307 case PF_IEEE802154:
1308 return SECCLASS_IEEE802154_SOCKET;
1309 case PF_CAIF:
1310 return SECCLASS_CAIF_SOCKET;
1311 case PF_ALG:
1312 return SECCLASS_ALG_SOCKET;
1313 case PF_NFC:
1314 return SECCLASS_NFC_SOCKET;
1315 case PF_VSOCK:
1316 return SECCLASS_VSOCK_SOCKET;
1317 case PF_KCM:
1318 return SECCLASS_KCM_SOCKET;
1319 case PF_QIPCRTR:
1320 return SECCLASS_QIPCRTR_SOCKET;
Linus Torvalds3051bf32017-02-22 10:15:09 -08001321 case PF_SMC:
1322 return SECCLASS_SMC_SOCKET;
Björn Töpel68e8b842018-05-02 13:01:22 +02001323 case PF_XDP:
1324 return SECCLASS_XDP_SOCKET;
1325#if PF_MAX > 45
Stephen Smalleyda69a532017-01-09 10:07:30 -05001326#error New address family defined, please update this function.
1327#endif
1328 }
1329 }
1330
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 return SECCLASS_SOCKET;
1332}
1333
Stephen Smalley134509d2015-06-04 16:22:17 -04001334static int selinux_genfs_get_sid(struct dentry *dentry,
1335 u16 tclass,
1336 u16 flags,
1337 u32 *sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001339 int rc;
Al Virofc640052016-04-10 01:33:30 -04001340 struct super_block *sb = dentry->d_sb;
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001341 char *buffer, *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Eric Paris828dfe12008-04-17 13:17:49 -04001343 buffer = (char *)__get_free_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 if (!buffer)
1345 return -ENOMEM;
1346
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001347 path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
1348 if (IS_ERR(path))
1349 rc = PTR_ERR(path);
1350 else {
Stephen Smalley134509d2015-06-04 16:22:17 -04001351 if (flags & SE_SBPROC) {
1352 /* each process gets a /proc/PID/ entry. Strip off the
1353 * PID part to get a valid selinux labeling.
1354 * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
1355 while (path[1] >= '0' && path[1] <= '9') {
1356 path[1] = '/';
1357 path++;
1358 }
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001359 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001360 rc = security_genfs_sid(&selinux_state, sb->s_type->name,
1361 path, tclass, sid);
Stephen Smalley7bb185e2018-09-04 16:51:36 -04001362 if (rc == -ENOENT) {
1363 /* No match in policy, mark as unlabeled. */
1364 *sid = SECINITSID_UNLABELED;
1365 rc = 0;
1366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 free_page((unsigned long)buffer);
1369 return rc;
1370}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
1372/* The inode's security attributes must be initialized before first use. */
1373static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
1374{
1375 struct superblock_security_struct *sbsec = NULL;
1376 struct inode_security_struct *isec = inode->i_security;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001377 u32 task_sid, sid = 0;
1378 u16 sclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 struct dentry *dentry;
1380#define INITCONTEXTLEN 255
1381 char *context = NULL;
1382 unsigned len = 0;
1383 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05001385 if (isec->initialized == LABEL_INITIALIZED)
Andreas Gruenbacher13457d02016-11-10 22:18:29 +01001386 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001388 spin_lock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05001389 if (isec->initialized == LABEL_INITIALIZED)
Eric Paris23970742006-09-25 23:32:01 -07001390 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
Andreas Gruenbacher13457d02016-11-10 22:18:29 +01001392 if (isec->sclass == SECCLASS_FILE)
1393 isec->sclass = inode_mode_to_security_class(inode->i_mode);
1394
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 sbsec = inode->i_sb->s_security;
David P. Quigley0d90a7e2009-01-16 09:22:02 -05001396 if (!(sbsec->flags & SE_SBINITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 /* Defer initialization until selinux_complete_init,
1398 after the initial policy is loaded and the security
1399 server is ready to handle calls. */
1400 spin_lock(&sbsec->isec_lock);
1401 if (list_empty(&isec->list))
1402 list_add(&isec->list, &sbsec->isec_head);
1403 spin_unlock(&sbsec->isec_lock);
Eric Paris23970742006-09-25 23:32:01 -07001404 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 }
1406
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001407 sclass = isec->sclass;
1408 task_sid = isec->task_sid;
1409 sid = isec->sid;
1410 isec->initialized = LABEL_PENDING;
1411 spin_unlock(&isec->lock);
1412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 switch (sbsec->behavior) {
David Quigleyeb9ae682013-05-22 12:50:37 -04001414 case SECURITY_FS_USE_NATIVE:
1415 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 case SECURITY_FS_USE_XATTR:
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001417 if (!(inode->i_opflags & IOP_XATTR)) {
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001418 sid = sbsec->def_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 break;
1420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 /* Need a dentry, since the xattr API requires one.
1422 Life would be simpler if we could just pass the inode. */
1423 if (opt_dentry) {
1424 /* Called from d_instantiate or d_splice_alias. */
1425 dentry = dget(opt_dentry);
1426 } else {
Al Virob1271252018-04-25 10:28:38 -04001427 /*
1428 * Called from selinux_complete_init, try to find a dentry.
1429 * Some filesystems really want a connected one, so try
1430 * that first. We could split SECURITY_FS_USE_XATTR in
1431 * two, depending upon that...
1432 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 dentry = d_find_alias(inode);
Al Virob1271252018-04-25 10:28:38 -04001434 if (!dentry)
1435 dentry = d_find_any_alias(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 }
1437 if (!dentry) {
Eric Parisdf7f54c2009-03-09 14:35:58 -04001438 /*
1439 * this is can be hit on boot when a file is accessed
1440 * before the policy is loaded. When we load policy we
1441 * may find inodes that have no dentry on the
1442 * sbsec->isec_head list. No reason to complain as these
1443 * will get fixed up the next time we go through
1444 * inode_doinit with a dentry, before these inodes could
1445 * be used again by userspace.
1446 */
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001447 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 }
1449
1450 len = INITCONTEXTLEN;
Eric Paris4cb912f2009-02-12 14:50:05 -05001451 context = kmalloc(len+1, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 if (!context) {
1453 rc = -ENOMEM;
1454 dput(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001455 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 }
Eric Paris4cb912f2009-02-12 14:50:05 -05001457 context[len] = '\0';
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001458 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 if (rc == -ERANGE) {
James Morris314dabb2009-08-10 22:00:13 +10001460 kfree(context);
1461
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 /* Need a larger buffer. Query for the right size. */
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001463 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 if (rc < 0) {
1465 dput(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001466 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 len = rc;
Eric Paris4cb912f2009-02-12 14:50:05 -05001469 context = kmalloc(len+1, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 if (!context) {
1471 rc = -ENOMEM;
1472 dput(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001473 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 }
Eric Paris4cb912f2009-02-12 14:50:05 -05001475 context[len] = '\0';
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001476 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 }
1478 dput(dentry);
1479 if (rc < 0) {
1480 if (rc != -ENODATA) {
peter enderborgc103a912018-06-12 10:09:03 +02001481 pr_warn("SELinux: %s: getxattr returned "
Harvey Harrisondd6f9532008-03-06 10:03:59 +11001482 "%d for dev=%s ino=%ld\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 -rc, inode->i_sb->s_id, inode->i_ino);
1484 kfree(context);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001485 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 }
1487 /* Map ENODATA to the default file SID */
1488 sid = sbsec->def_sid;
1489 rc = 0;
1490 } else {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001491 rc = security_context_to_sid_default(&selinux_state,
1492 context, rc, &sid,
Stephen Smalley869ab512008-04-04 08:46:05 -04001493 sbsec->def_sid,
1494 GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 if (rc) {
Eric Paris4ba0a8a2009-02-12 15:01:10 -05001496 char *dev = inode->i_sb->s_id;
1497 unsigned long ino = inode->i_ino;
1498
1499 if (rc == -EINVAL) {
1500 if (printk_ratelimit())
peter enderborgc103a912018-06-12 10:09:03 +02001501 pr_notice("SELinux: inode=%lu on dev=%s was found to have an invalid "
Eric Paris4ba0a8a2009-02-12 15:01:10 -05001502 "context=%s. This indicates you may need to relabel the inode or the "
1503 "filesystem in question.\n", ino, dev, context);
1504 } else {
peter enderborgc103a912018-06-12 10:09:03 +02001505 pr_warn("SELinux: %s: context_to_sid(%s) "
Eric Paris4ba0a8a2009-02-12 15:01:10 -05001506 "returned %d for dev=%s ino=%ld\n",
1507 __func__, context, -rc, dev, ino);
1508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 kfree(context);
1510 /* Leave with the unlabeled SID */
1511 rc = 0;
1512 break;
1513 }
1514 }
1515 kfree(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 break;
1517 case SECURITY_FS_USE_TASK:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001518 sid = task_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 break;
1520 case SECURITY_FS_USE_TRANS:
1521 /* Default to the fs SID. */
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001522 sid = sbsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
1524 /* Try to obtain a transition SID. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001525 rc = security_transition_sid(&selinux_state, task_sid, sid,
1526 sclass, NULL, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 if (rc)
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001528 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 break;
Eric Parisc312feb2006-07-10 04:43:53 -07001530 case SECURITY_FS_USE_MNTPOINT:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001531 sid = sbsec->mntpoint_sid;
Eric Parisc312feb2006-07-10 04:43:53 -07001532 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 default:
Eric Parisc312feb2006-07-10 04:43:53 -07001534 /* Default to the fs superblock SID. */
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001535 sid = sbsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Stephen Smalley134509d2015-06-04 16:22:17 -04001537 if ((sbsec->flags & SE_SBGENFS) && !S_ISLNK(inode->i_mode)) {
Paul Mooref64410e2014-03-19 16:46:18 -04001538 /* We must have a dentry to determine the label on
1539 * procfs inodes */
Al Virob1271252018-04-25 10:28:38 -04001540 if (opt_dentry) {
Paul Mooref64410e2014-03-19 16:46:18 -04001541 /* Called from d_instantiate or
1542 * d_splice_alias. */
1543 dentry = dget(opt_dentry);
Al Virob1271252018-04-25 10:28:38 -04001544 } else {
Paul Mooref64410e2014-03-19 16:46:18 -04001545 /* Called from selinux_complete_init, try to
Al Virob1271252018-04-25 10:28:38 -04001546 * find a dentry. Some filesystems really want
1547 * a connected one, so try that first.
1548 */
Paul Mooref64410e2014-03-19 16:46:18 -04001549 dentry = d_find_alias(inode);
Al Virob1271252018-04-25 10:28:38 -04001550 if (!dentry)
1551 dentry = d_find_any_alias(inode);
1552 }
Paul Mooref64410e2014-03-19 16:46:18 -04001553 /*
1554 * This can be hit on boot when a file is accessed
1555 * before the policy is loaded. When we load policy we
1556 * may find inodes that have no dentry on the
1557 * sbsec->isec_head list. No reason to complain as
1558 * these will get fixed up the next time we go through
1559 * inode_doinit() with a dentry, before these inodes
1560 * could be used again by userspace.
1561 */
1562 if (!dentry)
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001563 goto out;
1564 rc = selinux_genfs_get_sid(dentry, sclass,
Stephen Smalley134509d2015-06-04 16:22:17 -04001565 sbsec->flags, &sid);
Paul Mooref64410e2014-03-19 16:46:18 -04001566 dput(dentry);
1567 if (rc)
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001568 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 }
1570 break;
1571 }
1572
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001573out:
1574 spin_lock(&isec->lock);
1575 if (isec->initialized == LABEL_PENDING) {
1576 if (!sid || rc) {
1577 isec->initialized = LABEL_INVALID;
1578 goto out_unlock;
1579 }
1580
1581 isec->initialized = LABEL_INITIALIZED;
1582 isec->sid = sid;
1583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Eric Paris23970742006-09-25 23:32:01 -07001585out_unlock:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001586 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 return rc;
1588}
1589
1590/* Convert a Linux signal to an access vector. */
1591static inline u32 signal_to_av(int sig)
1592{
1593 u32 perm = 0;
1594
1595 switch (sig) {
1596 case SIGCHLD:
1597 /* Commonly granted from child to parent. */
1598 perm = PROCESS__SIGCHLD;
1599 break;
1600 case SIGKILL:
1601 /* Cannot be caught or ignored */
1602 perm = PROCESS__SIGKILL;
1603 break;
1604 case SIGSTOP:
1605 /* Cannot be caught or ignored */
1606 perm = PROCESS__SIGSTOP;
1607 break;
1608 default:
1609 /* All other signals. */
1610 perm = PROCESS__SIGNAL;
1611 break;
1612 }
1613
1614 return perm;
1615}
1616
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001617#if CAP_LAST_CAP > 63
1618#error Fix SELinux to handle capabilities > 63.
1619#endif
1620
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621/* Check whether a task is allowed to use a capability. */
Eric Paris6a9de492012-01-03 12:25:14 -05001622static int cred_has_capability(const struct cred *cred,
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04001623 int cap, int audit, bool initns)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624{
Thomas Liu2bf49692009-07-14 12:14:09 -04001625 struct common_audit_data ad;
Eric Paris06112162008-11-11 22:02:50 +11001626 struct av_decision avd;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001627 u16 sclass;
David Howells3699c532009-01-06 22:27:01 +00001628 u32 sid = cred_sid(cred);
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001629 u32 av = CAP_TO_MASK(cap);
Eric Paris06112162008-11-11 22:02:50 +11001630 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
Eric Paris50c205f2012-04-04 15:01:43 -04001632 ad.type = LSM_AUDIT_DATA_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 ad.u.cap = cap;
1634
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001635 switch (CAP_TO_INDEX(cap)) {
1636 case 0:
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04001637 sclass = initns ? SECCLASS_CAPABILITY : SECCLASS_CAP_USERNS;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001638 break;
1639 case 1:
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04001640 sclass = initns ? SECCLASS_CAPABILITY2 : SECCLASS_CAP2_USERNS;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001641 break;
1642 default:
peter enderborgc103a912018-06-12 10:09:03 +02001643 pr_err("SELinux: out of range capability %d\n", cap);
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001644 BUG();
Eric Parisa35c6c832011-04-20 10:21:28 -04001645 return -EINVAL;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001646 }
Eric Paris06112162008-11-11 22:02:50 +11001647
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001648 rc = avc_has_perm_noaudit(&selinux_state,
1649 sid, sid, sclass, av, 0, &avd);
Eric Paris9ade0cf2011-04-25 16:26:29 -04001650 if (audit == SECURITY_CAP_AUDIT) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001651 int rc2 = avc_audit(&selinux_state,
1652 sid, sid, sclass, av, &avd, rc, &ad, 0);
Eric Paris9ade0cf2011-04-25 16:26:29 -04001653 if (rc2)
1654 return rc2;
1655 }
Eric Paris06112162008-11-11 22:02:50 +11001656 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657}
1658
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659/* Check whether a task has a particular permission to an inode.
1660 The 'adp' parameter is optional and allows other audit
1661 data to be passed (e.g. the dentry). */
David Howells88e67f32008-11-14 10:39:21 +11001662static int inode_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 struct inode *inode,
1664 u32 perms,
Linus Torvalds19e49832013-10-04 12:54:11 -07001665 struct common_audit_data *adp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 struct inode_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +11001668 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
David Howellse0e81732009-09-02 09:13:40 +01001670 validate_creds(cred);
1671
Eric Paris828dfe12008-04-17 13:17:49 -04001672 if (unlikely(IS_PRIVATE(inode)))
Stephen Smalleybbaca6c2007-02-14 00:34:16 -08001673 return 0;
1674
David Howells88e67f32008-11-14 10:39:21 +11001675 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 isec = inode->i_security;
1677
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001678 return avc_has_perm(&selinux_state,
1679 sid, isec->sid, isec->sclass, perms, adp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680}
1681
1682/* Same as inode_has_perm, but pass explicit audit data containing
1683 the dentry to help the auditing code to more easily generate the
1684 pathname if needed. */
David Howells88e67f32008-11-14 10:39:21 +11001685static inline int dentry_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 struct dentry *dentry,
1687 u32 av)
1688{
David Howellsc6f493d2015-03-17 22:26:22 +00001689 struct inode *inode = d_backing_inode(dentry);
Thomas Liu2bf49692009-07-14 12:14:09 -04001690 struct common_audit_data ad;
David Howells88e67f32008-11-14 10:39:21 +11001691
Eric Paris50c205f2012-04-04 15:01:43 -04001692 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Paris2875fa02011-04-28 16:04:24 -04001693 ad.u.dentry = dentry;
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05001694 __inode_security_revalidate(inode, dentry, true);
Linus Torvalds19e49832013-10-04 12:54:11 -07001695 return inode_has_perm(cred, inode, av, &ad);
Eric Paris2875fa02011-04-28 16:04:24 -04001696}
1697
1698/* Same as inode_has_perm, but pass explicit audit data containing
1699 the path to help the auditing code to more easily generate the
1700 pathname if needed. */
1701static inline int path_has_perm(const struct cred *cred,
Al Viro3f7036a2015-03-08 19:28:30 -04001702 const struct path *path,
Eric Paris2875fa02011-04-28 16:04:24 -04001703 u32 av)
1704{
David Howellsc6f493d2015-03-17 22:26:22 +00001705 struct inode *inode = d_backing_inode(path->dentry);
Eric Paris2875fa02011-04-28 16:04:24 -04001706 struct common_audit_data ad;
1707
Eric Paris50c205f2012-04-04 15:01:43 -04001708 ad.type = LSM_AUDIT_DATA_PATH;
Eric Paris2875fa02011-04-28 16:04:24 -04001709 ad.u.path = *path;
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05001710 __inode_security_revalidate(inode, path->dentry, true);
Linus Torvalds19e49832013-10-04 12:54:11 -07001711 return inode_has_perm(cred, inode, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712}
1713
David Howells13f8e982013-06-13 23:37:55 +01001714/* Same as path_has_perm, but uses the inode from the file struct. */
1715static inline int file_path_has_perm(const struct cred *cred,
1716 struct file *file,
1717 u32 av)
1718{
1719 struct common_audit_data ad;
1720
Vivek Goyal43af5de2016-09-09 11:37:49 -04001721 ad.type = LSM_AUDIT_DATA_FILE;
1722 ad.u.file = file;
Linus Torvalds19e49832013-10-04 12:54:11 -07001723 return inode_has_perm(cred, file_inode(file), av, &ad);
David Howells13f8e982013-06-13 23:37:55 +01001724}
1725
Chenbo Fengf66e4482017-10-18 13:00:26 -07001726#ifdef CONFIG_BPF_SYSCALL
1727static int bpf_fd_pass(struct file *file, u32 sid);
1728#endif
1729
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730/* Check whether a task can use an open file descriptor to
1731 access an inode in a given way. Check access to the
1732 descriptor itself, and then use dentry_has_perm to
1733 check a particular permission to the file.
1734 Access to the descriptor is implicitly granted if it
1735 has the same SID as the process. If av is zero, then
1736 access to the file is not checked, e.g. for cases
1737 where only the descriptor is affected like seek. */
David Howells88e67f32008-11-14 10:39:21 +11001738static int file_has_perm(const struct cred *cred,
1739 struct file *file,
1740 u32 av)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 struct file_security_struct *fsec = file->f_security;
Al Viro496ad9a2013-01-23 17:07:38 -05001743 struct inode *inode = file_inode(file);
Thomas Liu2bf49692009-07-14 12:14:09 -04001744 struct common_audit_data ad;
David Howells88e67f32008-11-14 10:39:21 +11001745 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 int rc;
1747
Vivek Goyal43af5de2016-09-09 11:37:49 -04001748 ad.type = LSM_AUDIT_DATA_FILE;
1749 ad.u.file = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
David Howells275bb412008-11-14 10:39:19 +11001751 if (sid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001752 rc = avc_has_perm(&selinux_state,
1753 sid, fsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 SECCLASS_FD,
1755 FD__USE,
1756 &ad);
1757 if (rc)
David Howells88e67f32008-11-14 10:39:21 +11001758 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 }
1760
Chenbo Fengf66e4482017-10-18 13:00:26 -07001761#ifdef CONFIG_BPF_SYSCALL
1762 rc = bpf_fd_pass(file, cred_sid(cred));
1763 if (rc)
1764 return rc;
1765#endif
1766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 /* av is zero if only checking access to the descriptor. */
David Howells88e67f32008-11-14 10:39:21 +11001768 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 if (av)
Linus Torvalds19e49832013-10-04 12:54:11 -07001770 rc = inode_has_perm(cred, inode, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
David Howells88e67f32008-11-14 10:39:21 +11001772out:
1773 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774}
1775
David Howellsc3c188b2015-07-10 17:19:58 -04001776/*
1777 * Determine the label for an inode that might be unioned.
1778 */
Vivek Goyalc957f6d2016-07-13 10:44:51 -04001779static int
1780selinux_determine_inode_label(const struct task_security_struct *tsec,
1781 struct inode *dir,
1782 const struct qstr *name, u16 tclass,
1783 u32 *_new_isid)
David Howellsc3c188b2015-07-10 17:19:58 -04001784{
1785 const struct superblock_security_struct *sbsec = dir->i_sb->s_security;
David Howellsc3c188b2015-07-10 17:19:58 -04001786
1787 if ((sbsec->flags & SE_SBINITIALIZED) &&
1788 (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)) {
1789 *_new_isid = sbsec->mntpoint_sid;
1790 } else if ((sbsec->flags & SBLABEL_MNT) &&
1791 tsec->create_sid) {
1792 *_new_isid = tsec->create_sid;
1793 } else {
Paul Moore20cdef82016-04-04 14:14:42 -04001794 const struct inode_security_struct *dsec = inode_security(dir);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001795 return security_transition_sid(&selinux_state, tsec->sid,
1796 dsec->sid, tclass,
David Howellsc3c188b2015-07-10 17:19:58 -04001797 name, _new_isid);
1798 }
1799
1800 return 0;
1801}
1802
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803/* Check whether a task can create a file. */
1804static int may_create(struct inode *dir,
1805 struct dentry *dentry,
1806 u16 tclass)
1807{
Paul Moore5fb49872010-04-22 14:46:19 -04001808 const struct task_security_struct *tsec = current_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 struct inode_security_struct *dsec;
1810 struct superblock_security_struct *sbsec;
David Howells275bb412008-11-14 10:39:19 +11001811 u32 sid, newsid;
Thomas Liu2bf49692009-07-14 12:14:09 -04001812 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 int rc;
1814
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001815 dsec = inode_security(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 sbsec = dir->i_sb->s_security;
1817
David Howells275bb412008-11-14 10:39:19 +11001818 sid = tsec->sid;
David Howells275bb412008-11-14 10:39:19 +11001819
Eric Paris50c205f2012-04-04 15:01:43 -04001820 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04001821 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001823 rc = avc_has_perm(&selinux_state,
1824 sid, dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 DIR__ADD_NAME | DIR__SEARCH,
1826 &ad);
1827 if (rc)
1828 return rc;
1829
Vivek Goyalc957f6d2016-07-13 10:44:51 -04001830 rc = selinux_determine_inode_label(current_security(), dir,
1831 &dentry->d_name, tclass, &newsid);
David Howellsc3c188b2015-07-10 17:19:58 -04001832 if (rc)
1833 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001835 rc = avc_has_perm(&selinux_state,
1836 sid, newsid, tclass, FILE__CREATE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 if (rc)
1838 return rc;
1839
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001840 return avc_has_perm(&selinux_state,
1841 newsid, sbsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 SECCLASS_FILESYSTEM,
1843 FILESYSTEM__ASSOCIATE, &ad);
1844}
1845
Eric Paris828dfe12008-04-17 13:17:49 -04001846#define MAY_LINK 0
1847#define MAY_UNLINK 1
1848#define MAY_RMDIR 2
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
1850/* Check whether a task can link, unlink, or rmdir a file/directory. */
1851static int may_link(struct inode *dir,
1852 struct dentry *dentry,
1853 int kind)
1854
1855{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 struct inode_security_struct *dsec, *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001857 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11001858 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 u32 av;
1860 int rc;
1861
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001862 dsec = inode_security(dir);
1863 isec = backing_inode_security(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
Eric Paris50c205f2012-04-04 15:01:43 -04001865 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04001866 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
1868 av = DIR__SEARCH;
1869 av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001870 rc = avc_has_perm(&selinux_state,
1871 sid, dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 if (rc)
1873 return rc;
1874
1875 switch (kind) {
1876 case MAY_LINK:
1877 av = FILE__LINK;
1878 break;
1879 case MAY_UNLINK:
1880 av = FILE__UNLINK;
1881 break;
1882 case MAY_RMDIR:
1883 av = DIR__RMDIR;
1884 break;
1885 default:
peter enderborgc103a912018-06-12 10:09:03 +02001886 pr_warn("SELinux: %s: unrecognized kind %d\n",
Eric Paris744ba352008-04-17 11:52:44 -04001887 __func__, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 return 0;
1889 }
1890
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001891 rc = avc_has_perm(&selinux_state,
1892 sid, isec->sid, isec->sclass, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 return rc;
1894}
1895
1896static inline int may_rename(struct inode *old_dir,
1897 struct dentry *old_dentry,
1898 struct inode *new_dir,
1899 struct dentry *new_dentry)
1900{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001902 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11001903 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 u32 av;
1905 int old_is_dir, new_is_dir;
1906 int rc;
1907
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001908 old_dsec = inode_security(old_dir);
1909 old_isec = backing_inode_security(old_dentry);
David Howellse36cb0b2015-01-29 12:02:35 +00001910 old_is_dir = d_is_dir(old_dentry);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001911 new_dsec = inode_security(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
Eric Paris50c205f2012-04-04 15:01:43 -04001913 ad.type = LSM_AUDIT_DATA_DENTRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
Eric Parisa2694342011-04-25 13:10:27 -04001915 ad.u.dentry = old_dentry;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001916 rc = avc_has_perm(&selinux_state,
1917 sid, old_dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 DIR__REMOVE_NAME | DIR__SEARCH, &ad);
1919 if (rc)
1920 return rc;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001921 rc = avc_has_perm(&selinux_state,
1922 sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 old_isec->sclass, FILE__RENAME, &ad);
1924 if (rc)
1925 return rc;
1926 if (old_is_dir && new_dir != old_dir) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001927 rc = avc_has_perm(&selinux_state,
1928 sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 old_isec->sclass, DIR__REPARENT, &ad);
1930 if (rc)
1931 return rc;
1932 }
1933
Eric Parisa2694342011-04-25 13:10:27 -04001934 ad.u.dentry = new_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 av = DIR__ADD_NAME | DIR__SEARCH;
David Howells2c616d42015-01-29 12:02:33 +00001936 if (d_is_positive(new_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 av |= DIR__REMOVE_NAME;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001938 rc = avc_has_perm(&selinux_state,
1939 sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 if (rc)
1941 return rc;
David Howells2c616d42015-01-29 12:02:33 +00001942 if (d_is_positive(new_dentry)) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001943 new_isec = backing_inode_security(new_dentry);
David Howellse36cb0b2015-01-29 12:02:35 +00001944 new_is_dir = d_is_dir(new_dentry);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001945 rc = avc_has_perm(&selinux_state,
1946 sid, new_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 new_isec->sclass,
1948 (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
1949 if (rc)
1950 return rc;
1951 }
1952
1953 return 0;
1954}
1955
1956/* Check whether a task can perform a filesystem operation. */
David Howells88e67f32008-11-14 10:39:21 +11001957static int superblock_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 struct super_block *sb,
1959 u32 perms,
Thomas Liu2bf49692009-07-14 12:14:09 -04001960 struct common_audit_data *ad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 struct superblock_security_struct *sbsec;
David Howells88e67f32008-11-14 10:39:21 +11001963 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 sbsec = sb->s_security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001966 return avc_has_perm(&selinux_state,
1967 sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968}
1969
1970/* Convert a Linux mode and permission mask to an access vector. */
1971static inline u32 file_mask_to_av(int mode, int mask)
1972{
1973 u32 av = 0;
1974
Al Virodba19c62011-07-25 20:49:29 -04001975 if (!S_ISDIR(mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 if (mask & MAY_EXEC)
1977 av |= FILE__EXECUTE;
1978 if (mask & MAY_READ)
1979 av |= FILE__READ;
1980
1981 if (mask & MAY_APPEND)
1982 av |= FILE__APPEND;
1983 else if (mask & MAY_WRITE)
1984 av |= FILE__WRITE;
1985
1986 } else {
1987 if (mask & MAY_EXEC)
1988 av |= DIR__SEARCH;
1989 if (mask & MAY_WRITE)
1990 av |= DIR__WRITE;
1991 if (mask & MAY_READ)
1992 av |= DIR__READ;
1993 }
1994
1995 return av;
1996}
1997
1998/* Convert a Linux file to an access vector. */
1999static inline u32 file_to_av(struct file *file)
2000{
2001 u32 av = 0;
2002
2003 if (file->f_mode & FMODE_READ)
2004 av |= FILE__READ;
2005 if (file->f_mode & FMODE_WRITE) {
2006 if (file->f_flags & O_APPEND)
2007 av |= FILE__APPEND;
2008 else
2009 av |= FILE__WRITE;
2010 }
Stephen Smalley0794c662008-03-17 08:55:18 -04002011 if (!av) {
2012 /*
2013 * Special file opened with flags 3 for ioctl-only use.
2014 */
2015 av = FILE__IOCTL;
2016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
2018 return av;
2019}
2020
Eric Paris8b6a5a32008-10-29 17:06:46 -04002021/*
2022 * Convert a file to an access vector and include the correct open
2023 * open permission.
2024 */
2025static inline u32 open_file_to_av(struct file *file)
2026{
2027 u32 av = file_to_av(file);
Stephen Smalleyccb54472017-05-12 12:41:24 -04002028 struct inode *inode = file_inode(file);
Eric Paris8b6a5a32008-10-29 17:06:46 -04002029
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002030 if (selinux_policycap_openperm() &&
2031 inode->i_sb->s_magic != SOCKFS_MAGIC)
Eric Paris49b7b8d2010-07-23 11:44:09 -04002032 av |= FILE__OPEN;
2033
Eric Paris8b6a5a32008-10-29 17:06:46 -04002034 return av;
2035}
2036
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037/* Hook functions begin here. */
2038
Stephen Smalley79af7302015-01-21 10:54:10 -05002039static int selinux_binder_set_context_mgr(struct task_struct *mgr)
2040{
2041 u32 mysid = current_sid();
2042 u32 mgrsid = task_sid(mgr);
2043
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002044 return avc_has_perm(&selinux_state,
2045 mysid, mgrsid, SECCLASS_BINDER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002046 BINDER__SET_CONTEXT_MGR, NULL);
2047}
2048
2049static int selinux_binder_transaction(struct task_struct *from,
2050 struct task_struct *to)
2051{
2052 u32 mysid = current_sid();
2053 u32 fromsid = task_sid(from);
2054 u32 tosid = task_sid(to);
2055 int rc;
2056
2057 if (mysid != fromsid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002058 rc = avc_has_perm(&selinux_state,
2059 mysid, fromsid, SECCLASS_BINDER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002060 BINDER__IMPERSONATE, NULL);
2061 if (rc)
2062 return rc;
2063 }
2064
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002065 return avc_has_perm(&selinux_state,
2066 fromsid, tosid, SECCLASS_BINDER, BINDER__CALL,
Stephen Smalley79af7302015-01-21 10:54:10 -05002067 NULL);
2068}
2069
2070static int selinux_binder_transfer_binder(struct task_struct *from,
2071 struct task_struct *to)
2072{
2073 u32 fromsid = task_sid(from);
2074 u32 tosid = task_sid(to);
2075
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002076 return avc_has_perm(&selinux_state,
2077 fromsid, tosid, SECCLASS_BINDER, BINDER__TRANSFER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002078 NULL);
2079}
2080
2081static int selinux_binder_transfer_file(struct task_struct *from,
2082 struct task_struct *to,
2083 struct file *file)
2084{
2085 u32 sid = task_sid(to);
2086 struct file_security_struct *fsec = file->f_security;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002087 struct dentry *dentry = file->f_path.dentry;
Paul Moore20cdef82016-04-04 14:14:42 -04002088 struct inode_security_struct *isec;
Stephen Smalley79af7302015-01-21 10:54:10 -05002089 struct common_audit_data ad;
2090 int rc;
2091
2092 ad.type = LSM_AUDIT_DATA_PATH;
2093 ad.u.path = file->f_path;
2094
2095 if (sid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002096 rc = avc_has_perm(&selinux_state,
2097 sid, fsec->sid,
Stephen Smalley79af7302015-01-21 10:54:10 -05002098 SECCLASS_FD,
2099 FD__USE,
2100 &ad);
2101 if (rc)
2102 return rc;
2103 }
2104
Chenbo Fengf66e4482017-10-18 13:00:26 -07002105#ifdef CONFIG_BPF_SYSCALL
2106 rc = bpf_fd_pass(file, sid);
2107 if (rc)
2108 return rc;
2109#endif
2110
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002111 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
Stephen Smalley79af7302015-01-21 10:54:10 -05002112 return 0;
2113
Paul Moore20cdef82016-04-04 14:14:42 -04002114 isec = backing_inode_security(dentry);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002115 return avc_has_perm(&selinux_state,
2116 sid, isec->sid, isec->sclass, file_to_av(file),
Stephen Smalley79af7302015-01-21 10:54:10 -05002117 &ad);
2118}
2119
Ingo Molnar9e488582009-05-07 19:26:19 +10002120static int selinux_ptrace_access_check(struct task_struct *child,
David Howells5cd9c582008-08-14 11:37:28 +01002121 unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002123 u32 sid = current_sid();
2124 u32 csid = task_sid(child);
Stephen Smalley006ebb42008-05-19 08:32:49 -04002125
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002126 if (mode & PTRACE_MODE_READ)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002127 return avc_has_perm(&selinux_state,
2128 sid, csid, SECCLASS_FILE, FILE__READ, NULL);
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002129
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002130 return avc_has_perm(&selinux_state,
2131 sid, csid, SECCLASS_PROCESS, PROCESS__PTRACE, NULL);
David Howells5cd9c582008-08-14 11:37:28 +01002132}
2133
2134static int selinux_ptrace_traceme(struct task_struct *parent)
2135{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002136 return avc_has_perm(&selinux_state,
2137 task_sid(parent), current_sid(), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002138 PROCESS__PTRACE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139}
2140
2141static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
Eric Paris828dfe12008-04-17 13:17:49 -04002142 kernel_cap_t *inheritable, kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002144 return avc_has_perm(&selinux_state,
2145 current_sid(), task_sid(target), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002146 PROCESS__GETCAP, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147}
2148
David Howellsd84f4f92008-11-14 10:39:23 +11002149static int selinux_capset(struct cred *new, const struct cred *old,
2150 const kernel_cap_t *effective,
2151 const kernel_cap_t *inheritable,
2152 const kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002154 return avc_has_perm(&selinux_state,
2155 cred_sid(old), cred_sid(new), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002156 PROCESS__SETCAP, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157}
2158
James Morris5626d3e2009-01-30 10:05:06 +11002159/*
2160 * (This comment used to live with the selinux_task_setuid hook,
2161 * which was removed).
2162 *
2163 * Since setuid only affects the current process, and since the SELinux
2164 * controls are not based on the Linux identity attributes, SELinux does not
2165 * need to control this operation. However, SELinux does control the use of
2166 * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
2167 */
2168
Eric Paris6a9de492012-01-03 12:25:14 -05002169static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
2170 int cap, int audit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171{
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04002172 return cred_has_capability(cred, cap, audit, ns == &init_user_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173}
2174
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
2176{
David Howells88e67f32008-11-14 10:39:21 +11002177 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 int rc = 0;
2179
2180 if (!sb)
2181 return 0;
2182
2183 switch (cmds) {
Eric Paris828dfe12008-04-17 13:17:49 -04002184 case Q_SYNC:
2185 case Q_QUOTAON:
2186 case Q_QUOTAOFF:
2187 case Q_SETINFO:
2188 case Q_SETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11002189 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04002190 break;
2191 case Q_GETFMT:
2192 case Q_GETINFO:
2193 case Q_GETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11002194 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04002195 break;
2196 default:
2197 rc = 0; /* let the kernel handle invalid cmds */
2198 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 }
2200 return rc;
2201}
2202
2203static int selinux_quota_on(struct dentry *dentry)
2204{
David Howells88e67f32008-11-14 10:39:21 +11002205 const struct cred *cred = current_cred();
2206
Eric Paris2875fa02011-04-28 16:04:24 -04002207 return dentry_has_perm(cred, dentry, FILE__QUOTAON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208}
2209
Eric Paris12b30522010-11-15 18:36:29 -05002210static int selinux_syslog(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 switch (type) {
Kees Cookd78ca3c2010-02-03 15:37:13 -08002213 case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */
2214 case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002215 return avc_has_perm(&selinux_state,
2216 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002217 SECCLASS_SYSTEM, SYSTEM__SYSLOG_READ, NULL);
Kees Cookd78ca3c2010-02-03 15:37:13 -08002218 case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */
2219 case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */
2220 /* Set level of messages printed to console */
2221 case SYSLOG_ACTION_CONSOLE_LEVEL:
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002222 return avc_has_perm(&selinux_state,
2223 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002224 SECCLASS_SYSTEM, SYSTEM__SYSLOG_CONSOLE,
2225 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 }
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002227 /* All other syslog types */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002228 return avc_has_perm(&selinux_state,
2229 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002230 SECCLASS_SYSTEM, SYSTEM__SYSLOG_MOD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231}
2232
2233/*
2234 * Check that a process has enough memory to allocate a new virtual
2235 * mapping. 0 means there is enough memory for the allocation to
2236 * succeed and -ENOMEM implies there is not.
2237 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 * Do not audit the selinux permission check, as this is applied to all
2239 * processes that allocate mappings.
2240 */
Alan Cox34b4e4a2007-08-22 14:01:28 -07002241static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242{
2243 int rc, cap_sys_admin = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002245 rc = cred_has_capability(current_cred(), CAP_SYS_ADMIN,
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04002246 SECURITY_CAP_NOAUDIT, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 if (rc == 0)
2248 cap_sys_admin = 1;
2249
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002250 return cap_sys_admin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251}
2252
2253/* binprm security operations */
2254
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002255static u32 ptrace_parent_sid(void)
Paul Moore0c6181c2016-03-30 21:41:21 -04002256{
2257 u32 sid = 0;
2258 struct task_struct *tracer;
2259
2260 rcu_read_lock();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002261 tracer = ptrace_parent(current);
Paul Moore0c6181c2016-03-30 21:41:21 -04002262 if (tracer)
2263 sid = task_sid(tracer);
2264 rcu_read_unlock();
2265
2266 return sid;
2267}
2268
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002269static int check_nnp_nosuid(const struct linux_binprm *bprm,
2270 const struct task_security_struct *old_tsec,
2271 const struct task_security_struct *new_tsec)
2272{
2273 int nnp = (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS);
Andy Lutomirski380cf5b2016-06-23 16:41:05 -05002274 int nosuid = !mnt_may_suid(bprm->file->f_path.mnt);
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002275 int rc;
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002276 u32 av;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002277
2278 if (!nnp && !nosuid)
2279 return 0; /* neither NNP nor nosuid */
2280
2281 if (new_tsec->sid == old_tsec->sid)
2282 return 0; /* No change in credentials */
2283
2284 /*
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002285 * If the policy enables the nnp_nosuid_transition policy capability,
2286 * then we permit transitions under NNP or nosuid if the
2287 * policy allows the corresponding permission between
2288 * the old and new contexts.
2289 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002290 if (selinux_policycap_nnp_nosuid_transition()) {
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002291 av = 0;
2292 if (nnp)
2293 av |= PROCESS2__NNP_TRANSITION;
2294 if (nosuid)
2295 av |= PROCESS2__NOSUID_TRANSITION;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002296 rc = avc_has_perm(&selinux_state,
2297 old_tsec->sid, new_tsec->sid,
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002298 SECCLASS_PROCESS2, av, NULL);
2299 if (!rc)
2300 return 0;
2301 }
2302
2303 /*
2304 * We also permit NNP or nosuid transitions to bounded SIDs,
2305 * i.e. SIDs that are guaranteed to only be allowed a subset
2306 * of the permissions of the current SID.
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002307 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002308 rc = security_bounded_transition(&selinux_state, old_tsec->sid,
2309 new_tsec->sid);
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002310 if (!rc)
2311 return 0;
2312
2313 /*
2314 * On failure, preserve the errno values for NNP vs nosuid.
2315 * NNP: Operation not permitted for caller.
2316 * nosuid: Permission denied to file.
2317 */
2318 if (nnp)
2319 return -EPERM;
2320 return -EACCES;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002321}
2322
David Howellsa6f76f22008-11-14 10:39:24 +11002323static int selinux_bprm_set_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324{
David Howellsa6f76f22008-11-14 10:39:24 +11002325 const struct task_security_struct *old_tsec;
2326 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 struct inode_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04002328 struct common_audit_data ad;
Al Viro496ad9a2013-01-23 17:07:38 -05002329 struct inode *inode = file_inode(bprm->file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 int rc;
2331
David Howellsa6f76f22008-11-14 10:39:24 +11002332 /* SELinux context only depends on initial program or script and not
2333 * the script interpreter */
Kees Cookddb4a142017-07-18 15:25:23 -07002334 if (bprm->called_set_creds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 return 0;
2336
David Howellsa6f76f22008-11-14 10:39:24 +11002337 old_tsec = current_security();
2338 new_tsec = bprm->cred->security;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002339 isec = inode_security(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
2341 /* Default to the current task SID. */
David Howellsa6f76f22008-11-14 10:39:24 +11002342 new_tsec->sid = old_tsec->sid;
2343 new_tsec->osid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
Michael LeMay28eba5b2006-06-27 02:53:42 -07002345 /* Reset fs, key, and sock SIDs on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002346 new_tsec->create_sid = 0;
2347 new_tsec->keycreate_sid = 0;
2348 new_tsec->sockcreate_sid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
David Howellsa6f76f22008-11-14 10:39:24 +11002350 if (old_tsec->exec_sid) {
2351 new_tsec->sid = old_tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 /* Reset exec SID on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002353 new_tsec->exec_sid = 0;
Andy Lutomirski259e5e62012-04-12 16:47:50 -05002354
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002355 /* Fail on NNP or nosuid if not an allowed transition. */
2356 rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
2357 if (rc)
2358 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 } else {
2360 /* Check for a default transition on this program. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002361 rc = security_transition_sid(&selinux_state, old_tsec->sid,
2362 isec->sid, SECCLASS_PROCESS, NULL,
Eric Paris652bb9b2011-02-01 11:05:40 -05002363 &new_tsec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 if (rc)
2365 return rc;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002366
2367 /*
2368 * Fallback to old SID on NNP or nosuid if not an allowed
2369 * transition.
2370 */
2371 rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
2372 if (rc)
2373 new_tsec->sid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 }
2375
Vivek Goyal43af5de2016-09-09 11:37:49 -04002376 ad.type = LSM_AUDIT_DATA_FILE;
2377 ad.u.file = bprm->file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
David Howellsa6f76f22008-11-14 10:39:24 +11002379 if (new_tsec->sid == old_tsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002380 rc = avc_has_perm(&selinux_state,
2381 old_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
2383 if (rc)
2384 return rc;
2385 } else {
2386 /* Check permissions for the transition. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002387 rc = avc_has_perm(&selinux_state,
2388 old_tsec->sid, new_tsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
2390 if (rc)
2391 return rc;
2392
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002393 rc = avc_has_perm(&selinux_state,
2394 new_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
2396 if (rc)
2397 return rc;
2398
David Howellsa6f76f22008-11-14 10:39:24 +11002399 /* Check for shared state */
2400 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002401 rc = avc_has_perm(&selinux_state,
2402 old_tsec->sid, new_tsec->sid,
David Howellsa6f76f22008-11-14 10:39:24 +11002403 SECCLASS_PROCESS, PROCESS__SHARE,
2404 NULL);
2405 if (rc)
2406 return -EPERM;
2407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408
David Howellsa6f76f22008-11-14 10:39:24 +11002409 /* Make sure that anyone attempting to ptrace over a task that
2410 * changes its SID has the appropriate permit */
Eric W. Biederman9227dd22017-01-23 17:26:31 +13002411 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002412 u32 ptsid = ptrace_parent_sid();
David Howellsa6f76f22008-11-14 10:39:24 +11002413 if (ptsid != 0) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002414 rc = avc_has_perm(&selinux_state,
2415 ptsid, new_tsec->sid,
David Howellsa6f76f22008-11-14 10:39:24 +11002416 SECCLASS_PROCESS,
2417 PROCESS__PTRACE, NULL);
2418 if (rc)
2419 return -EPERM;
2420 }
2421 }
2422
2423 /* Clear any possibly unsafe personality bits on exec: */
2424 bprm->per_clear |= PER_CLEAR_ON_SETID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 /* Enable secure mode for SIDs transitions unless
2427 the noatsecure permission is granted between
2428 the two SIDs, i.e. ahp returns 0. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002429 rc = avc_has_perm(&selinux_state,
2430 old_tsec->sid, new_tsec->sid,
Kees Cook62874c32017-07-18 15:25:25 -07002431 SECCLASS_PROCESS, PROCESS__NOATSECURE,
2432 NULL);
2433 bprm->secureexec |= !!rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 }
2435
Kees Cook62874c32017-07-18 15:25:25 -07002436 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437}
2438
Al Viroc3c073f2012-08-21 22:32:06 -04002439static int match_file(const void *p, struct file *file, unsigned fd)
2440{
2441 return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0;
2442}
2443
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444/* Derived from fs/exec.c:flush_old_files. */
David Howells745ca242008-11-14 10:39:22 +11002445static inline void flush_unauthorized_files(const struct cred *cred,
2446 struct files_struct *files)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 struct file *file, *devnull = NULL;
Stephen Smalleyb20c8122006-09-25 23:32:03 -07002449 struct tty_struct *tty;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002450 int drop_tty = 0;
Al Viroc3c073f2012-08-21 22:32:06 -04002451 unsigned n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002453 tty = get_current_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 if (tty) {
Peter Hurley4a510962016-01-09 21:35:23 -08002455 spin_lock(&tty->files_lock);
Eric Paris37dd0bd2008-10-31 17:40:00 -04002456 if (!list_empty(&tty->tty_files)) {
Nick Piggind996b622010-08-18 04:37:36 +10002457 struct tty_file_private *file_priv;
Eric Paris37dd0bd2008-10-31 17:40:00 -04002458
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 /* Revalidate access to controlling tty.
David Howells13f8e982013-06-13 23:37:55 +01002460 Use file_path_has_perm on the tty path directly
2461 rather than using file_has_perm, as this particular
2462 open file may belong to another process and we are
2463 only interested in the inode-based check here. */
Nick Piggind996b622010-08-18 04:37:36 +10002464 file_priv = list_first_entry(&tty->tty_files,
2465 struct tty_file_private, list);
2466 file = file_priv->file;
David Howells13f8e982013-06-13 23:37:55 +01002467 if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE))
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002468 drop_tty = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 }
Peter Hurley4a510962016-01-09 21:35:23 -08002470 spin_unlock(&tty->files_lock);
Alan Cox452a00d2008-10-13 10:39:13 +01002471 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 }
Eric W. Biederman98a27ba2007-05-08 00:26:56 -07002473 /* Reset controlling tty. */
2474 if (drop_tty)
2475 no_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
2477 /* Revalidate access to inherited open files. */
Al Viroc3c073f2012-08-21 22:32:06 -04002478 n = iterate_fd(files, 0, match_file, cred);
2479 if (!n) /* none found? */
2480 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481
Al Viroc3c073f2012-08-21 22:32:06 -04002482 devnull = dentry_open(&selinux_null, O_RDWR, cred);
Al Viro45525b22012-10-16 13:30:07 -04002483 if (IS_ERR(devnull))
2484 devnull = NULL;
2485 /* replace all the matching ones with this */
2486 do {
2487 replace_fd(n - 1, devnull, 0);
2488 } while ((n = iterate_fd(files, n, match_file, cred)) != 0);
2489 if (devnull)
Al Viroc3c073f2012-08-21 22:32:06 -04002490 fput(devnull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491}
2492
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493/*
David Howellsa6f76f22008-11-14 10:39:24 +11002494 * Prepare a process for imminent new credential changes due to exec
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 */
David Howellsa6f76f22008-11-14 10:39:24 +11002496static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497{
David Howellsa6f76f22008-11-14 10:39:24 +11002498 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 struct rlimit *rlim, *initrlim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 int rc, i;
2501
David Howellsa6f76f22008-11-14 10:39:24 +11002502 new_tsec = bprm->cred->security;
2503 if (new_tsec->sid == new_tsec->osid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 return;
2505
2506 /* Close files for which the new task SID is not authorized. */
David Howellsa6f76f22008-11-14 10:39:24 +11002507 flush_unauthorized_files(bprm->cred, current->files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
David Howellsa6f76f22008-11-14 10:39:24 +11002509 /* Always clear parent death signal on SID transitions. */
2510 current->pdeath_signal = 0;
2511
2512 /* Check whether the new SID can inherit resource limits from the old
2513 * SID. If not, reset all soft limits to the lower of the current
2514 * task's hard limit and the init task's soft limit.
2515 *
2516 * Note that the setting of hard limits (even to lower them) can be
2517 * controlled by the setrlimit check. The inclusion of the init task's
2518 * soft limit into the computation is to avoid resetting soft limits
2519 * higher than the default soft limit for cases where the default is
2520 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2521 */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002522 rc = avc_has_perm(&selinux_state,
2523 new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
David Howellsa6f76f22008-11-14 10:39:24 +11002524 PROCESS__RLIMITINH, NULL);
2525 if (rc) {
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002526 /* protect against do_prlimit() */
2527 task_lock(current);
David Howellsa6f76f22008-11-14 10:39:24 +11002528 for (i = 0; i < RLIM_NLIMITS; i++) {
2529 rlim = current->signal->rlim + i;
2530 initrlim = init_task.signal->rlim + i;
2531 rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
2532 }
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002533 task_unlock(current);
Nicolas Pitrebaa73d92016-11-11 00:10:10 -05002534 if (IS_ENABLED(CONFIG_POSIX_TIMERS))
2535 update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
David Howellsa6f76f22008-11-14 10:39:24 +11002536 }
2537}
2538
2539/*
2540 * Clean up the process immediately after the installation of new credentials
2541 * due to exec
2542 */
2543static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
2544{
2545 const struct task_security_struct *tsec = current_security();
2546 struct itimerval itimer;
David Howellsa6f76f22008-11-14 10:39:24 +11002547 u32 osid, sid;
2548 int rc, i;
David Howellsa6f76f22008-11-14 10:39:24 +11002549
David Howellsa6f76f22008-11-14 10:39:24 +11002550 osid = tsec->osid;
2551 sid = tsec->sid;
2552
2553 if (sid == osid)
2554 return;
2555
2556 /* Check whether the new SID can inherit signal state from the old SID.
2557 * If not, clear itimers to avoid subsequent signal generation and
2558 * flush and unblock signals.
2559 *
2560 * This must occur _after_ the task SID has been updated so that any
2561 * kill done after the flush will be checked against the new SID.
2562 */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002563 rc = avc_has_perm(&selinux_state,
2564 osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 if (rc) {
Nicolas Pitrebaa73d92016-11-11 00:10:10 -05002566 if (IS_ENABLED(CONFIG_POSIX_TIMERS)) {
2567 memset(&itimer, 0, sizeof itimer);
2568 for (i = 0; i < 3; i++)
2569 do_setitimer(i, &itimer, NULL);
2570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterov9e7c8f82015-06-04 16:22:16 -04002572 if (!fatal_signal_pending(current)) {
2573 flush_sigqueue(&current->pending);
2574 flush_sigqueue(&current->signal->shared_pending);
David Howells3bcac022009-04-29 13:45:05 +01002575 flush_signal_handlers(current, 1);
2576 sigemptyset(&current->blocked);
Oleg Nesterov9e7c8f82015-06-04 16:22:16 -04002577 recalc_sigpending();
David Howells3bcac022009-04-29 13:45:05 +01002578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 spin_unlock_irq(&current->sighand->siglock);
2580 }
2581
David Howellsa6f76f22008-11-14 10:39:24 +11002582 /* Wake up the parent if it is waiting so that it can recheck
2583 * wait permission to the new task SID. */
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002584 read_lock(&tasklist_lock);
Oleg Nesterov0b7570e2009-09-23 15:56:46 -07002585 __wake_up_parent(current, current->real_parent);
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002586 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587}
2588
2589/* superblock security operations */
2590
2591static int selinux_sb_alloc_security(struct super_block *sb)
2592{
2593 return superblock_alloc_security(sb);
2594}
2595
2596static void selinux_sb_free_security(struct super_block *sb)
2597{
2598 superblock_free_security(sb);
2599}
2600
2601static inline int match_prefix(char *prefix, int plen, char *option, int olen)
2602{
2603 if (plen > olen)
2604 return 0;
2605
2606 return !memcmp(prefix, option, plen);
2607}
2608
2609static inline int selinux_option(char *option, int len)
2610{
Eric Paris832cbd92008-04-01 13:24:09 -04002611 return (match_prefix(CONTEXT_STR, sizeof(CONTEXT_STR)-1, option, len) ||
2612 match_prefix(FSCONTEXT_STR, sizeof(FSCONTEXT_STR)-1, option, len) ||
2613 match_prefix(DEFCONTEXT_STR, sizeof(DEFCONTEXT_STR)-1, option, len) ||
David P. Quigley11689d42009-01-16 09:22:03 -05002614 match_prefix(ROOTCONTEXT_STR, sizeof(ROOTCONTEXT_STR)-1, option, len) ||
2615 match_prefix(LABELSUPP_STR, sizeof(LABELSUPP_STR)-1, option, len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616}
2617
2618static inline void take_option(char **to, char *from, int *first, int len)
2619{
2620 if (!*first) {
2621 **to = ',';
2622 *to += 1;
Cory Olmo3528a952006-09-29 01:58:44 -07002623 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 *first = 0;
2625 memcpy(*to, from, len);
2626 *to += len;
2627}
2628
Eric Paris828dfe12008-04-17 13:17:49 -04002629static inline void take_selinux_option(char **to, char *from, int *first,
2630 int len)
Cory Olmo3528a952006-09-29 01:58:44 -07002631{
2632 int current_size = 0;
2633
2634 if (!*first) {
2635 **to = '|';
2636 *to += 1;
Eric Paris828dfe12008-04-17 13:17:49 -04002637 } else
Cory Olmo3528a952006-09-29 01:58:44 -07002638 *first = 0;
2639
2640 while (current_size < len) {
2641 if (*from != '"') {
2642 **to = *from;
2643 *to += 1;
2644 }
2645 from += 1;
2646 current_size += 1;
2647 }
2648}
2649
Eric Parise0007522008-03-05 10:31:54 -05002650static int selinux_sb_copy_data(char *orig, char *copy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651{
2652 int fnosec, fsec, rc = 0;
2653 char *in_save, *in_curr, *in_end;
2654 char *sec_curr, *nosec_save, *nosec;
Cory Olmo3528a952006-09-29 01:58:44 -07002655 int open_quote = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656
2657 in_curr = orig;
2658 sec_curr = copy;
2659
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 nosec = (char *)get_zeroed_page(GFP_KERNEL);
2661 if (!nosec) {
2662 rc = -ENOMEM;
2663 goto out;
2664 }
2665
2666 nosec_save = nosec;
2667 fnosec = fsec = 1;
2668 in_save = in_end = orig;
2669
2670 do {
Cory Olmo3528a952006-09-29 01:58:44 -07002671 if (*in_end == '"')
2672 open_quote = !open_quote;
2673 if ((*in_end == ',' && open_quote == 0) ||
2674 *in_end == '\0') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 int len = in_end - in_curr;
2676
2677 if (selinux_option(in_curr, len))
Cory Olmo3528a952006-09-29 01:58:44 -07002678 take_selinux_option(&sec_curr, in_curr, &fsec, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 else
2680 take_option(&nosec, in_curr, &fnosec, len);
2681
2682 in_curr = in_end + 1;
2683 }
2684 } while (*in_end++);
2685
Eric Paris6931dfc2005-06-30 02:58:51 -07002686 strcpy(in_save, nosec_save);
Gerald Schaeferda3caa22005-06-21 17:15:18 -07002687 free_page((unsigned long)nosec_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688out:
2689 return rc;
2690}
2691
Al Viro204cc0c2018-12-13 13:41:47 -05002692static int selinux_sb_eat_lsm_opts(char *options, void **mnt_opts)
Al Viro5b400232018-12-12 20:13:29 -05002693{
2694 char *s = (char *)get_zeroed_page(GFP_KERNEL);
2695 int err;
2696
2697 if (!s)
2698 return -ENOMEM;
2699 err = selinux_sb_copy_data(options, s);
2700 if (!err)
Al Viro204cc0c2018-12-13 13:41:47 -05002701 err = selinux_parse_opts_str(s, mnt_opts);
Al Viro5b400232018-12-12 20:13:29 -05002702 free_page((unsigned long)s);
2703 return err;
2704}
2705
Al Viro204cc0c2018-12-13 13:41:47 -05002706static int selinux_sb_remount(struct super_block *sb, void *mnt_opts)
Eric Paris026eb162011-03-03 16:09:14 -05002707{
Al Virobd323652018-12-13 15:04:59 -05002708 struct selinux_mnt_opts *opts = mnt_opts;
Eric Paris026eb162011-03-03 16:09:14 -05002709 struct superblock_security_struct *sbsec = sb->s_security;
Al Virobd323652018-12-13 15:04:59 -05002710 u32 sid;
2711 int rc;
Eric Paris026eb162011-03-03 16:09:14 -05002712
2713 if (!(sbsec->flags & SE_SBINITIALIZED))
2714 return 0;
2715
Al Viro204cc0c2018-12-13 13:41:47 -05002716 if (!opts)
2717 return 0;
2718
Al Virobd323652018-12-13 15:04:59 -05002719 if (opts->fscontext) {
2720 rc = parse_sid(sb, opts->fscontext, &sid);
2721 if (rc)
Al Viroc039bc32018-12-01 23:06:57 -05002722 return rc;
Al Virobd323652018-12-13 15:04:59 -05002723 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
2724 goto out_bad_option;
2725 }
2726 if (opts->context) {
2727 rc = parse_sid(sb, opts->context, &sid);
2728 if (rc)
2729 return rc;
2730 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
2731 goto out_bad_option;
2732 }
2733 if (opts->rootcontext) {
2734 struct inode_security_struct *root_isec;
2735 root_isec = backing_inode_security(sb->s_root);
2736 rc = parse_sid(sb, opts->rootcontext, &sid);
2737 if (rc)
2738 return rc;
2739 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
2740 goto out_bad_option;
2741 }
2742 if (opts->defcontext) {
2743 rc = parse_sid(sb, opts->defcontext, &sid);
2744 if (rc)
2745 return rc;
2746 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
2747 goto out_bad_option;
Eric Paris026eb162011-03-03 16:09:14 -05002748 }
Al Viroc039bc32018-12-01 23:06:57 -05002749 return 0;
Eric Paris026eb162011-03-03 16:09:14 -05002750
Eric Paris026eb162011-03-03 16:09:14 -05002751out_bad_option:
peter enderborgc103a912018-06-12 10:09:03 +02002752 pr_warn("SELinux: unable to change security options "
Linus Torvalds29b1deb2013-12-15 11:17:45 -08002753 "during remount (dev %s, type=%s)\n", sb->s_id,
2754 sb->s_type->name);
Al Viroc039bc32018-12-01 23:06:57 -05002755 return -EINVAL;
Eric Paris026eb162011-03-03 16:09:14 -05002756}
2757
Al Viroa10d7c22018-12-05 11:58:35 -05002758static int selinux_sb_kern_mount(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759{
David Howells88e67f32008-11-14 10:39:21 +11002760 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002761 struct common_audit_data ad;
James Morris74192242008-12-19 11:41:10 +11002762
Eric Paris50c205f2012-04-04 15:01:43 -04002763 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002764 ad.u.dentry = sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002765 return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766}
2767
David Howells726c3342006-06-23 02:02:58 -07002768static int selinux_sb_statfs(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769{
David Howells88e67f32008-11-14 10:39:21 +11002770 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002771 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772
Eric Paris50c205f2012-04-04 15:01:43 -04002773 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002774 ad.u.dentry = dentry->d_sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002775 return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776}
2777
Al Viro808d4e32012-10-11 11:42:01 -04002778static int selinux_mount(const char *dev_name,
Al Viro8a04c432016-03-25 14:52:53 -04002779 const struct path *path,
Al Viro808d4e32012-10-11 11:42:01 -04002780 const char *type,
Eric Paris828dfe12008-04-17 13:17:49 -04002781 unsigned long flags,
2782 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783{
David Howells88e67f32008-11-14 10:39:21 +11002784 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785
2786 if (flags & MS_REMOUNT)
Al Virod8c95842011-12-07 18:16:57 -05002787 return superblock_has_perm(cred, path->dentry->d_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002788 FILESYSTEM__REMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 else
Eric Paris2875fa02011-04-28 16:04:24 -04002790 return path_has_perm(cred, path, FILE__MOUNTON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791}
2792
2793static int selinux_umount(struct vfsmount *mnt, int flags)
2794{
David Howells88e67f32008-11-14 10:39:21 +11002795 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796
David Howells88e67f32008-11-14 10:39:21 +11002797 return superblock_has_perm(cred, mnt->mnt_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002798 FILESYSTEM__UNMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799}
2800
2801/* inode security operations */
2802
2803static int selinux_inode_alloc_security(struct inode *inode)
2804{
2805 return inode_alloc_security(inode);
2806}
2807
2808static void selinux_inode_free_security(struct inode *inode)
2809{
2810 inode_free_security(inode);
2811}
2812
David Quigleyd47be3d2013-05-22 12:50:34 -04002813static int selinux_dentry_init_security(struct dentry *dentry, int mode,
Al Viro4f3ccd72016-07-20 16:06:15 -04002814 const struct qstr *name, void **ctx,
David Quigleyd47be3d2013-05-22 12:50:34 -04002815 u32 *ctxlen)
2816{
David Quigleyd47be3d2013-05-22 12:50:34 -04002817 u32 newsid;
2818 int rc;
2819
Vivek Goyalc957f6d2016-07-13 10:44:51 -04002820 rc = selinux_determine_inode_label(current_security(),
2821 d_inode(dentry->d_parent), name,
David Howellsc3c188b2015-07-10 17:19:58 -04002822 inode_mode_to_security_class(mode),
2823 &newsid);
2824 if (rc)
2825 return rc;
David Quigleyd47be3d2013-05-22 12:50:34 -04002826
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002827 return security_sid_to_context(&selinux_state, newsid, (char **)ctx,
2828 ctxlen);
David Quigleyd47be3d2013-05-22 12:50:34 -04002829}
2830
Vivek Goyala518b0a2016-07-13 10:44:53 -04002831static int selinux_dentry_create_files_as(struct dentry *dentry, int mode,
2832 struct qstr *name,
2833 const struct cred *old,
2834 struct cred *new)
2835{
2836 u32 newsid;
2837 int rc;
2838 struct task_security_struct *tsec;
2839
2840 rc = selinux_determine_inode_label(old->security,
2841 d_inode(dentry->d_parent), name,
2842 inode_mode_to_security_class(mode),
2843 &newsid);
2844 if (rc)
2845 return rc;
2846
2847 tsec = new->security;
2848 tsec->create_sid = newsid;
2849 return 0;
2850}
2851
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002852static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +09002853 const struct qstr *qstr,
2854 const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -05002855 void **value, size_t *len)
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002856{
Paul Moore5fb49872010-04-22 14:46:19 -04002857 const struct task_security_struct *tsec = current_security();
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002858 struct superblock_security_struct *sbsec;
Corentin LABBEc0d4f462017-10-04 20:32:17 +02002859 u32 newsid, clen;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002860 int rc;
Tetsuo Handa95489062013-07-25 05:44:02 +09002861 char *context;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002862
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002863 sbsec = dir->i_sb->s_security;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002864
David Howells275bb412008-11-14 10:39:19 +11002865 newsid = tsec->create_sid;
2866
Vivek Goyalc957f6d2016-07-13 10:44:51 -04002867 rc = selinux_determine_inode_label(current_security(),
David Howellsc3c188b2015-07-10 17:19:58 -04002868 dir, qstr,
2869 inode_mode_to_security_class(inode->i_mode),
2870 &newsid);
2871 if (rc)
2872 return rc;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002873
Eric Paris296fddf2006-09-25 23:32:00 -07002874 /* Possibly defer initialization to selinux_complete_init. */
David P. Quigley0d90a7e2009-01-16 09:22:02 -05002875 if (sbsec->flags & SE_SBINITIALIZED) {
Eric Paris296fddf2006-09-25 23:32:00 -07002876 struct inode_security_struct *isec = inode->i_security;
2877 isec->sclass = inode_mode_to_security_class(inode->i_mode);
2878 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05002879 isec->initialized = LABEL_INITIALIZED;
Eric Paris296fddf2006-09-25 23:32:00 -07002880 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002881
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002882 if (!selinux_state.initialized || !(sbsec->flags & SBLABEL_MNT))
Stephen Smalley25a74f32005-11-08 21:34:33 -08002883 return -EOPNOTSUPP;
2884
Tetsuo Handa95489062013-07-25 05:44:02 +09002885 if (name)
2886 *name = XATTR_SELINUX_SUFFIX;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002887
2888 if (value && len) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002889 rc = security_sid_to_context_force(&selinux_state, newsid,
2890 &context, &clen);
Tetsuo Handa95489062013-07-25 05:44:02 +09002891 if (rc)
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002892 return rc;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002893 *value = context;
2894 *len = clen;
2895 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002896
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002897 return 0;
2898}
2899
Al Viro4acdaf22011-07-26 01:42:34 -04002900static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901{
2902 return may_create(dir, dentry, SECCLASS_FILE);
2903}
2904
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2906{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 return may_link(dir, old_dentry, MAY_LINK);
2908}
2909
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
2911{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 return may_link(dir, dentry, MAY_UNLINK);
2913}
2914
2915static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
2916{
2917 return may_create(dir, dentry, SECCLASS_LNK_FILE);
2918}
2919
Al Viro18bb1db2011-07-26 01:41:39 -04002920static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921{
2922 return may_create(dir, dentry, SECCLASS_DIR);
2923}
2924
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
2926{
2927 return may_link(dir, dentry, MAY_RMDIR);
2928}
2929
Al Viro1a67aaf2011-07-26 01:52:52 -04002930static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 return may_create(dir, dentry, inode_mode_to_security_class(mode));
2933}
2934
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
Eric Paris828dfe12008-04-17 13:17:49 -04002936 struct inode *new_inode, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937{
2938 return may_rename(old_inode, old_dentry, new_inode, new_dentry);
2939}
2940
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941static int selinux_inode_readlink(struct dentry *dentry)
2942{
David Howells88e67f32008-11-14 10:39:21 +11002943 const struct cred *cred = current_cred();
2944
Eric Paris2875fa02011-04-28 16:04:24 -04002945 return dentry_has_perm(cred, dentry, FILE__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946}
2947
NeilBrownbda0be72015-03-23 13:37:39 +11002948static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode,
2949 bool rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950{
David Howells88e67f32008-11-14 10:39:21 +11002951 const struct cred *cred = current_cred();
NeilBrownbda0be72015-03-23 13:37:39 +11002952 struct common_audit_data ad;
2953 struct inode_security_struct *isec;
2954 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955
NeilBrownbda0be72015-03-23 13:37:39 +11002956 validate_creds(cred);
2957
2958 ad.type = LSM_AUDIT_DATA_DENTRY;
2959 ad.u.dentry = dentry;
2960 sid = cred_sid(cred);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05002961 isec = inode_security_rcu(inode, rcu);
2962 if (IS_ERR(isec))
2963 return PTR_ERR(isec);
NeilBrownbda0be72015-03-23 13:37:39 +11002964
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002965 return avc_has_perm_flags(&selinux_state,
2966 sid, isec->sid, isec->sclass, FILE__READ, &ad,
NeilBrownbda0be72015-03-23 13:37:39 +11002967 rcu ? MAY_NOT_BLOCK : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968}
2969
Eric Parisd4cf970d2012-04-04 15:01:42 -04002970static noinline int audit_inode_permission(struct inode *inode,
2971 u32 perms, u32 audited, u32 denied,
Stephen Smalley626b9742014-04-29 11:29:04 -07002972 int result,
Eric Parisd4cf970d2012-04-04 15:01:42 -04002973 unsigned flags)
2974{
2975 struct common_audit_data ad;
Eric Parisd4cf970d2012-04-04 15:01:42 -04002976 struct inode_security_struct *isec = inode->i_security;
2977 int rc;
2978
Eric Paris50c205f2012-04-04 15:01:43 -04002979 ad.type = LSM_AUDIT_DATA_INODE;
Eric Parisd4cf970d2012-04-04 15:01:42 -04002980 ad.u.inode = inode;
2981
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002982 rc = slow_avc_audit(&selinux_state,
2983 current_sid(), isec->sid, isec->sclass, perms,
Stephen Smalley626b9742014-04-29 11:29:04 -07002984 audited, denied, result, &ad, flags);
Eric Parisd4cf970d2012-04-04 15:01:42 -04002985 if (rc)
2986 return rc;
2987 return 0;
2988}
2989
Al Viroe74f71e2011-06-20 19:38:15 -04002990static int selinux_inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991{
David Howells88e67f32008-11-14 10:39:21 +11002992 const struct cred *cred = current_cred();
Eric Parisb782e0a2010-07-23 11:44:03 -04002993 u32 perms;
2994 bool from_access;
Al Virocf1dd1d2011-06-20 19:44:08 -04002995 unsigned flags = mask & MAY_NOT_BLOCK;
Eric Paris2e334052012-04-04 15:01:42 -04002996 struct inode_security_struct *isec;
2997 u32 sid;
2998 struct av_decision avd;
2999 int rc, rc2;
3000 u32 audited, denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001
Eric Parisb782e0a2010-07-23 11:44:03 -04003002 from_access = mask & MAY_ACCESS;
Eric Parisd09ca732010-07-23 11:43:57 -04003003 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
3004
Eric Parisb782e0a2010-07-23 11:44:03 -04003005 /* No permission to check. Existence test. */
3006 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008
Eric Paris2e334052012-04-04 15:01:42 -04003009 validate_creds(cred);
Eric Parisb782e0a2010-07-23 11:44:03 -04003010
Eric Paris2e334052012-04-04 15:01:42 -04003011 if (unlikely(IS_PRIVATE(inode)))
3012 return 0;
Eric Parisb782e0a2010-07-23 11:44:03 -04003013
3014 perms = file_mask_to_av(inode->i_mode, mask);
3015
Eric Paris2e334052012-04-04 15:01:42 -04003016 sid = cred_sid(cred);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05003017 isec = inode_security_rcu(inode, flags & MAY_NOT_BLOCK);
3018 if (IS_ERR(isec))
3019 return PTR_ERR(isec);
Eric Paris2e334052012-04-04 15:01:42 -04003020
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003021 rc = avc_has_perm_noaudit(&selinux_state,
3022 sid, isec->sid, isec->sclass, perms, 0, &avd);
Eric Paris2e334052012-04-04 15:01:42 -04003023 audited = avc_audit_required(perms, &avd, rc,
3024 from_access ? FILE__AUDIT_ACCESS : 0,
3025 &denied);
3026 if (likely(!audited))
3027 return rc;
3028
Stephen Smalley626b9742014-04-29 11:29:04 -07003029 rc2 = audit_inode_permission(inode, perms, audited, denied, rc, flags);
Eric Paris2e334052012-04-04 15:01:42 -04003030 if (rc2)
3031 return rc2;
3032 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033}
3034
3035static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
3036{
David Howells88e67f32008-11-14 10:39:21 +11003037 const struct cred *cred = current_cred();
Stephen Smalleyccb54472017-05-12 12:41:24 -04003038 struct inode *inode = d_backing_inode(dentry);
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003039 unsigned int ia_valid = iattr->ia_valid;
Eric Paris95dbf732012-04-04 13:45:34 -04003040 __u32 av = FILE__WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003042 /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
3043 if (ia_valid & ATTR_FORCE) {
3044 ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
3045 ATTR_FORCE);
3046 if (!ia_valid)
3047 return 0;
3048 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003050 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
3051 ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
Eric Paris2875fa02011-04-28 16:04:24 -04003052 return dentry_has_perm(cred, dentry, FILE__SETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003054 if (selinux_policycap_openperm() &&
Stephen Smalleyccb54472017-05-12 12:41:24 -04003055 inode->i_sb->s_magic != SOCKFS_MAGIC &&
3056 (ia_valid & ATTR_SIZE) &&
3057 !(ia_valid & ATTR_FILE))
Eric Paris95dbf732012-04-04 13:45:34 -04003058 av |= FILE__OPEN;
3059
3060 return dentry_has_perm(cred, dentry, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061}
3062
Al Viro3f7036a2015-03-08 19:28:30 -04003063static int selinux_inode_getattr(const struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064{
Al Viro3f7036a2015-03-08 19:28:30 -04003065 return path_has_perm(current_cred(), path, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066}
3067
Stephen Smalleydb590002017-04-20 11:31:30 -04003068static bool has_cap_mac_admin(bool audit)
3069{
3070 const struct cred *cred = current_cred();
3071 int cap_audit = audit ? SECURITY_CAP_AUDIT : SECURITY_CAP_NOAUDIT;
3072
3073 if (cap_capable(cred, &init_user_ns, CAP_MAC_ADMIN, cap_audit))
3074 return false;
3075 if (cred_has_capability(cred, CAP_MAC_ADMIN, cap_audit, true))
3076 return false;
3077 return true;
3078}
3079
David Howells8f0cfa52008-04-29 00:59:41 -07003080static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
3081 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082{
David Howellsc6f493d2015-03-17 22:26:22 +00003083 struct inode *inode = d_backing_inode(dentry);
Paul Moore20cdef82016-04-04 14:14:42 -04003084 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 struct superblock_security_struct *sbsec;
Thomas Liu2bf49692009-07-14 12:14:09 -04003086 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11003087 u32 newsid, sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 int rc = 0;
3089
Eric W. Biederman6b240302017-10-02 09:38:20 -05003090 if (strcmp(name, XATTR_NAME_SELINUX)) {
3091 rc = cap_inode_setxattr(dentry, name, value, size, flags);
3092 if (rc)
3093 return rc;
3094
3095 /* Not an attribute we recognize, so just check the
3096 ordinary setattr permission. */
3097 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099
3100 sbsec = inode->i_sb->s_security;
Eric Paris12f348b2012-10-09 10:56:25 -04003101 if (!(sbsec->flags & SBLABEL_MNT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 return -EOPNOTSUPP;
3103
Serge E. Hallyn2e149672011-03-23 16:43:26 -07003104 if (!inode_owner_or_capable(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 return -EPERM;
3106
Eric Paris50c205f2012-04-04 15:01:43 -04003107 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04003108 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109
Paul Moore20cdef82016-04-04 14:14:42 -04003110 isec = backing_inode_security(dentry);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003111 rc = avc_has_perm(&selinux_state,
3112 sid, isec->sid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 FILE__RELABELFROM, &ad);
3114 if (rc)
3115 return rc;
3116
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003117 rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3118 GFP_KERNEL);
Stephen Smalley12b29f32008-05-07 13:03:20 -04003119 if (rc == -EINVAL) {
Stephen Smalleydb590002017-04-20 11:31:30 -04003120 if (!has_cap_mac_admin(true)) {
Eric Parisd6ea83e2012-04-04 13:45:49 -04003121 struct audit_buffer *ab;
3122 size_t audit_size;
Eric Parisd6ea83e2012-04-04 13:45:49 -04003123
3124 /* We strip a nul only if it is at the end, otherwise the
3125 * context contains a nul and we should audit that */
Al Viroe3fea3f2012-06-09 08:15:16 +01003126 if (value) {
Colin Ian Kingadd24372017-10-14 13:46:55 +01003127 const char *str = value;
3128
Al Viroe3fea3f2012-06-09 08:15:16 +01003129 if (str[size - 1] == '\0')
3130 audit_size = size - 1;
3131 else
3132 audit_size = size;
3133 } else {
Al Viroe3fea3f2012-06-09 08:15:16 +01003134 audit_size = 0;
3135 }
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04003136 ab = audit_log_start(audit_context(),
3137 GFP_ATOMIC, AUDIT_SELINUX_ERR);
Eric Parisd6ea83e2012-04-04 13:45:49 -04003138 audit_log_format(ab, "op=setxattr invalid_context=");
3139 audit_log_n_untrustedstring(ab, value, audit_size);
3140 audit_log_end(ab);
3141
Stephen Smalley12b29f32008-05-07 13:03:20 -04003142 return rc;
Eric Parisd6ea83e2012-04-04 13:45:49 -04003143 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003144 rc = security_context_to_sid_force(&selinux_state, value,
3145 size, &newsid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04003146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 if (rc)
3148 return rc;
3149
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003150 rc = avc_has_perm(&selinux_state,
3151 sid, newsid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 FILE__RELABELTO, &ad);
3153 if (rc)
3154 return rc;
3155
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003156 rc = security_validate_transition(&selinux_state, isec->sid, newsid,
3157 sid, isec->sclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 if (rc)
3159 return rc;
3160
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003161 return avc_has_perm(&selinux_state,
3162 newsid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 sbsec->sid,
3164 SECCLASS_FILESYSTEM,
3165 FILESYSTEM__ASSOCIATE,
3166 &ad);
3167}
3168
David Howells8f0cfa52008-04-29 00:59:41 -07003169static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
Eric Parisf5269712008-05-14 11:27:45 -04003170 const void *value, size_t size,
David Howells8f0cfa52008-04-29 00:59:41 -07003171 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172{
David Howellsc6f493d2015-03-17 22:26:22 +00003173 struct inode *inode = d_backing_inode(dentry);
Paul Moore20cdef82016-04-04 14:14:42 -04003174 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 u32 newsid;
3176 int rc;
3177
3178 if (strcmp(name, XATTR_NAME_SELINUX)) {
3179 /* Not an attribute we recognize, so nothing to do. */
3180 return;
3181 }
3182
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003183 rc = security_context_to_sid_force(&selinux_state, value, size,
3184 &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 if (rc) {
peter enderborgc103a912018-06-12 10:09:03 +02003186 pr_err("SELinux: unable to map context to SID"
Stephen Smalley12b29f32008-05-07 13:03:20 -04003187 "for (%s, %lu), rc=%d\n",
3188 inode->i_sb->s_id, inode->i_ino, -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 return;
3190 }
3191
Paul Moore20cdef82016-04-04 14:14:42 -04003192 isec = backing_inode_security(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003193 spin_lock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003194 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05003196 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003197 spin_unlock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003198
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 return;
3200}
3201
David Howells8f0cfa52008-04-29 00:59:41 -07003202static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203{
David Howells88e67f32008-11-14 10:39:21 +11003204 const struct cred *cred = current_cred();
3205
Eric Paris2875fa02011-04-28 16:04:24 -04003206 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207}
3208
Eric Paris828dfe12008-04-17 13:17:49 -04003209static int selinux_inode_listxattr(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210{
David Howells88e67f32008-11-14 10:39:21 +11003211 const struct cred *cred = current_cred();
3212
Eric Paris2875fa02011-04-28 16:04:24 -04003213 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214}
3215
David Howells8f0cfa52008-04-29 00:59:41 -07003216static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217{
Eric W. Biederman6b240302017-10-02 09:38:20 -05003218 if (strcmp(name, XATTR_NAME_SELINUX)) {
3219 int rc = cap_inode_removexattr(dentry, name);
3220 if (rc)
3221 return rc;
3222
3223 /* Not an attribute we recognize, so just check the
3224 ordinary setattr permission. */
3225 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227
3228 /* No one is allowed to remove a SELinux security label.
3229 You can change the label, but all data must be labeled. */
3230 return -EACCES;
3231}
3232
James Morrisd381d8a2005-10-30 14:59:22 -08003233/*
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003234 * Copy the inode security context value to the user.
James Morrisd381d8a2005-10-30 14:59:22 -08003235 *
3236 * Permission check is handled by selinux_inode_getxattr hook.
3237 */
Andreas Gruenbacherea861df2015-12-24 11:09:39 -05003238static int selinux_inode_getsecurity(struct inode *inode, const char *name, void **buffer, bool alloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239{
David P. Quigley42492592008-02-04 22:29:39 -08003240 u32 size;
3241 int error;
3242 char *context = NULL;
Paul Moore20cdef82016-04-04 14:14:42 -04003243 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00003245 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3246 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003248 /*
3249 * If the caller has CAP_MAC_ADMIN, then get the raw context
3250 * value even if it is not defined by current policy; otherwise,
3251 * use the in-core value under current policy.
3252 * Use the non-auditing forms of the permission checks since
3253 * getxattr may be called by unprivileged processes commonly
3254 * and lack of permission just means that we fall back to the
3255 * in-core context value, not a denial.
3256 */
Paul Moore20cdef82016-04-04 14:14:42 -04003257 isec = inode_security(inode);
Stephen Smalleydb590002017-04-20 11:31:30 -04003258 if (has_cap_mac_admin(false))
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003259 error = security_sid_to_context_force(&selinux_state,
3260 isec->sid, &context,
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003261 &size);
3262 else
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003263 error = security_sid_to_context(&selinux_state, isec->sid,
3264 &context, &size);
David P. Quigley42492592008-02-04 22:29:39 -08003265 if (error)
3266 return error;
3267 error = size;
3268 if (alloc) {
3269 *buffer = context;
3270 goto out_nofree;
3271 }
3272 kfree(context);
3273out_nofree:
3274 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275}
3276
3277static int selinux_inode_setsecurity(struct inode *inode, const char *name,
Eric Paris828dfe12008-04-17 13:17:49 -04003278 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279{
Paul Moore2c971652016-04-19 16:36:28 -04003280 struct inode_security_struct *isec = inode_security_novalidate(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 u32 newsid;
3282 int rc;
3283
3284 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3285 return -EOPNOTSUPP;
3286
3287 if (!value || !size)
3288 return -EACCES;
3289
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003290 rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3291 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 if (rc)
3293 return rc;
3294
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003295 spin_lock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003296 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05003298 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003299 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 return 0;
3301}
3302
3303static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
3304{
3305 const int len = sizeof(XATTR_NAME_SELINUX);
3306 if (buffer && len <= buffer_size)
3307 memcpy(buffer, XATTR_NAME_SELINUX, len);
3308 return len;
3309}
3310
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05003311static void selinux_inode_getsecid(struct inode *inode, u32 *secid)
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02003312{
Andreas Gruenbachere817c2f2016-02-18 12:04:08 +01003313 struct inode_security_struct *isec = inode_security_novalidate(inode);
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02003314 *secid = isec->sid;
3315}
3316
Vivek Goyal56909eb2016-07-13 10:44:48 -04003317static int selinux_inode_copy_up(struct dentry *src, struct cred **new)
3318{
3319 u32 sid;
3320 struct task_security_struct *tsec;
3321 struct cred *new_creds = *new;
3322
3323 if (new_creds == NULL) {
3324 new_creds = prepare_creds();
3325 if (!new_creds)
3326 return -ENOMEM;
3327 }
3328
3329 tsec = new_creds->security;
3330 /* Get label from overlay inode and set it in create_sid */
3331 selinux_inode_getsecid(d_inode(src), &sid);
3332 tsec->create_sid = sid;
3333 *new = new_creds;
3334 return 0;
3335}
3336
Vivek Goyal19472b62016-07-13 10:44:50 -04003337static int selinux_inode_copy_up_xattr(const char *name)
3338{
3339 /* The copy_up hook above sets the initial context on an inode, but we
3340 * don't then want to overwrite it by blindly copying all the lower
3341 * xattrs up. Instead, we have to filter out SELinux-related xattrs.
3342 */
3343 if (strcmp(name, XATTR_NAME_SELINUX) == 0)
3344 return 1; /* Discard */
3345 /*
3346 * Any other attribute apart from SELINUX is not claimed, supported
3347 * by selinux.
3348 */
3349 return -EOPNOTSUPP;
3350}
3351
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352/* file security operations */
3353
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003354static int selinux_revalidate_file_permission(struct file *file, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355{
David Howells88e67f32008-11-14 10:39:21 +11003356 const struct cred *cred = current_cred();
Al Viro496ad9a2013-01-23 17:07:38 -05003357 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
3360 if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
3361 mask |= MAY_APPEND;
3362
Paul Moore389fb8002009-03-27 17:10:34 -04003363 return file_has_perm(cred, file,
3364 file_mask_to_av(inode->i_mode, mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365}
3366
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003367static int selinux_file_permission(struct file *file, int mask)
3368{
Al Viro496ad9a2013-01-23 17:07:38 -05003369 struct inode *inode = file_inode(file);
Stephen Smalley20dda182009-06-22 14:54:53 -04003370 struct file_security_struct *fsec = file->f_security;
Andreas Gruenbacherb1973672016-01-05 23:12:33 +01003371 struct inode_security_struct *isec;
Stephen Smalley20dda182009-06-22 14:54:53 -04003372 u32 sid = current_sid();
3373
Paul Moore389fb8002009-03-27 17:10:34 -04003374 if (!mask)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003375 /* No permission to check. Existence test. */
3376 return 0;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003377
Andreas Gruenbacherb1973672016-01-05 23:12:33 +01003378 isec = inode_security(inode);
Stephen Smalley20dda182009-06-22 14:54:53 -04003379 if (sid == fsec->sid && fsec->isid == isec->sid &&
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003380 fsec->pseqno == avc_policy_seqno(&selinux_state))
Eric Paris83d49852012-04-04 13:45:40 -04003381 /* No change since file_open check. */
Stephen Smalley20dda182009-06-22 14:54:53 -04003382 return 0;
3383
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003384 return selinux_revalidate_file_permission(file, mask);
3385}
3386
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387static int selinux_file_alloc_security(struct file *file)
3388{
3389 return file_alloc_security(file);
3390}
3391
3392static void selinux_file_free_security(struct file *file)
3393{
3394 file_free_security(file);
3395}
3396
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003397/*
3398 * Check whether a task has the ioctl permission and cmd
3399 * operation to an inode.
3400 */
Geliang Tang1d2a1682015-10-21 17:44:27 -04003401static int ioctl_has_perm(const struct cred *cred, struct file *file,
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003402 u32 requested, u16 cmd)
3403{
3404 struct common_audit_data ad;
3405 struct file_security_struct *fsec = file->f_security;
3406 struct inode *inode = file_inode(file);
Paul Moore20cdef82016-04-04 14:14:42 -04003407 struct inode_security_struct *isec;
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003408 struct lsm_ioctlop_audit ioctl;
3409 u32 ssid = cred_sid(cred);
3410 int rc;
3411 u8 driver = cmd >> 8;
3412 u8 xperm = cmd & 0xff;
3413
3414 ad.type = LSM_AUDIT_DATA_IOCTL_OP;
3415 ad.u.op = &ioctl;
3416 ad.u.op->cmd = cmd;
3417 ad.u.op->path = file->f_path;
3418
3419 if (ssid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003420 rc = avc_has_perm(&selinux_state,
3421 ssid, fsec->sid,
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003422 SECCLASS_FD,
3423 FD__USE,
3424 &ad);
3425 if (rc)
3426 goto out;
3427 }
3428
3429 if (unlikely(IS_PRIVATE(inode)))
3430 return 0;
3431
Paul Moore20cdef82016-04-04 14:14:42 -04003432 isec = inode_security(inode);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003433 rc = avc_has_extended_perms(&selinux_state,
3434 ssid, isec->sid, isec->sclass,
3435 requested, driver, xperm, &ad);
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003436out:
3437 return rc;
3438}
3439
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440static int selinux_file_ioctl(struct file *file, unsigned int cmd,
3441 unsigned long arg)
3442{
David Howells88e67f32008-11-14 10:39:21 +11003443 const struct cred *cred = current_cred();
Eric Paris0b24dcb2011-02-25 15:39:20 -05003444 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445
Eric Paris0b24dcb2011-02-25 15:39:20 -05003446 switch (cmd) {
3447 case FIONREAD:
3448 /* fall through */
3449 case FIBMAP:
3450 /* fall through */
3451 case FIGETBSZ:
3452 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003453 case FS_IOC_GETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003454 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003455 case FS_IOC_GETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003456 error = file_has_perm(cred, file, FILE__GETATTR);
3457 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458
Al Viro2f99c362012-03-23 16:04:05 -04003459 case FS_IOC_SETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003460 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003461 case FS_IOC_SETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003462 error = file_has_perm(cred, file, FILE__SETATTR);
3463 break;
3464
3465 /* sys_ioctl() checks */
3466 case FIONBIO:
3467 /* fall through */
3468 case FIOASYNC:
3469 error = file_has_perm(cred, file, 0);
3470 break;
3471
3472 case KDSKBENT:
3473 case KDSKBSENT:
Eric Paris6a9de492012-01-03 12:25:14 -05003474 error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04003475 SECURITY_CAP_AUDIT, true);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003476 break;
3477
3478 /* default case assumes that the command will go
3479 * to the file's ioctl() function.
3480 */
3481 default:
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003482 error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003483 }
3484 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485}
3486
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003487static int default_noexec;
3488
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
3490{
David Howells88e67f32008-11-14 10:39:21 +11003491 const struct cred *cred = current_cred();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003492 u32 sid = cred_sid(cred);
David Howellsd84f4f92008-11-14 10:39:23 +11003493 int rc = 0;
David Howells88e67f32008-11-14 10:39:21 +11003494
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003495 if (default_noexec &&
Stephen Smalley892e8ca2015-07-10 09:40:59 -04003496 (prot & PROT_EXEC) && (!file || IS_PRIVATE(file_inode(file)) ||
3497 (!shared && (prot & PROT_WRITE)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 /*
3499 * We are making executable an anonymous mapping or a
3500 * private file mapping that will also be writable.
3501 * This has an additional check.
3502 */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003503 rc = avc_has_perm(&selinux_state,
3504 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003505 PROCESS__EXECMEM, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 if (rc)
David Howellsd84f4f92008-11-14 10:39:23 +11003507 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509
3510 if (file) {
3511 /* read access is always possible with a mapping */
3512 u32 av = FILE__READ;
3513
3514 /* write access only matters if the mapping is shared */
3515 if (shared && (prot & PROT_WRITE))
3516 av |= FILE__WRITE;
3517
3518 if (prot & PROT_EXEC)
3519 av |= FILE__EXECUTE;
3520
David Howells88e67f32008-11-14 10:39:21 +11003521 return file_has_perm(cred, file, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 }
David Howellsd84f4f92008-11-14 10:39:23 +11003523
3524error:
3525 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526}
3527
Al Viroe5467852012-05-30 13:30:51 -04003528static int selinux_mmap_addr(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07003530 int rc = 0;
Paul Moore98883bf2014-03-19 16:46:11 -04003531
3532 if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
3533 u32 sid = current_sid();
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003534 rc = avc_has_perm(&selinux_state,
3535 sid, sid, SECCLASS_MEMPROTECT,
Paul Moore98883bf2014-03-19 16:46:11 -04003536 MEMPROTECT__MMAP_ZERO, NULL);
3537 }
3538
3539 return rc;
Al Viroe5467852012-05-30 13:30:51 -04003540}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541
Al Viroe5467852012-05-30 13:30:51 -04003542static int selinux_mmap_file(struct file *file, unsigned long reqprot,
3543 unsigned long prot, unsigned long flags)
3544{
Stephen Smalley3ba4bf52017-05-05 09:14:48 -04003545 struct common_audit_data ad;
3546 int rc;
3547
3548 if (file) {
3549 ad.type = LSM_AUDIT_DATA_FILE;
3550 ad.u.file = file;
3551 rc = inode_has_perm(current_cred(), file_inode(file),
3552 FILE__MAP, &ad);
3553 if (rc)
3554 return rc;
3555 }
3556
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003557 if (selinux_state.checkreqprot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 prot = reqprot;
3559
3560 return file_map_prot_check(file, prot,
3561 (flags & MAP_TYPE) == MAP_SHARED);
3562}
3563
3564static int selinux_file_mprotect(struct vm_area_struct *vma,
3565 unsigned long reqprot,
3566 unsigned long prot)
3567{
David Howells88e67f32008-11-14 10:39:21 +11003568 const struct cred *cred = current_cred();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003569 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003571 if (selinux_state.checkreqprot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 prot = reqprot;
3573
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003574 if (default_noexec &&
3575 (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
James Morrisd541bbe2009-01-29 12:19:51 +11003576 int rc = 0;
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003577 if (vma->vm_start >= vma->vm_mm->start_brk &&
3578 vma->vm_end <= vma->vm_mm->brk) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003579 rc = avc_has_perm(&selinux_state,
3580 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003581 PROCESS__EXECHEAP, NULL);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003582 } else if (!vma->vm_file &&
Stephen Smalleyc2316db2016-04-08 13:55:03 -04003583 ((vma->vm_start <= vma->vm_mm->start_stack &&
3584 vma->vm_end >= vma->vm_mm->start_stack) ||
Andy Lutomirskid17af502016-09-30 10:58:58 -07003585 vma_is_stack_for_current(vma))) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003586 rc = avc_has_perm(&selinux_state,
3587 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003588 PROCESS__EXECSTACK, NULL);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003589 } else if (vma->vm_file && vma->anon_vma) {
3590 /*
3591 * We are making executable a file mapping that has
3592 * had some COW done. Since pages might have been
3593 * written, check ability to execute the possibly
3594 * modified content. This typically should only
3595 * occur for text relocations.
3596 */
David Howellsd84f4f92008-11-14 10:39:23 +11003597 rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003598 }
Lorenzo Hernandez García-Hierro6b992192005-06-25 14:54:34 -07003599 if (rc)
3600 return rc;
3601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602
3603 return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
3604}
3605
3606static int selinux_file_lock(struct file *file, unsigned int cmd)
3607{
David Howells88e67f32008-11-14 10:39:21 +11003608 const struct cred *cred = current_cred();
3609
3610 return file_has_perm(cred, file, FILE__LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611}
3612
3613static int selinux_file_fcntl(struct file *file, unsigned int cmd,
3614 unsigned long arg)
3615{
David Howells88e67f32008-11-14 10:39:21 +11003616 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 int err = 0;
3618
3619 switch (cmd) {
Eric Paris828dfe12008-04-17 13:17:49 -04003620 case F_SETFL:
Eric Paris828dfe12008-04-17 13:17:49 -04003621 if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
David Howells88e67f32008-11-14 10:39:21 +11003622 err = file_has_perm(cred, file, FILE__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003624 }
3625 /* fall through */
3626 case F_SETOWN:
3627 case F_SETSIG:
3628 case F_GETFL:
3629 case F_GETOWN:
3630 case F_GETSIG:
Cyrill Gorcunov1d151c32012-07-30 14:43:00 -07003631 case F_GETOWNER_UIDS:
Eric Paris828dfe12008-04-17 13:17:49 -04003632 /* Just check FD__USE permission */
David Howells88e67f32008-11-14 10:39:21 +11003633 err = file_has_perm(cred, file, 0);
Eric Paris828dfe12008-04-17 13:17:49 -04003634 break;
3635 case F_GETLK:
3636 case F_SETLK:
3637 case F_SETLKW:
Jeff Layton0d3f7a22014-04-22 08:23:58 -04003638 case F_OFD_GETLK:
3639 case F_OFD_SETLK:
3640 case F_OFD_SETLKW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641#if BITS_PER_LONG == 32
Eric Paris828dfe12008-04-17 13:17:49 -04003642 case F_GETLK64:
3643 case F_SETLK64:
3644 case F_SETLKW64:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645#endif
David Howells88e67f32008-11-14 10:39:21 +11003646 err = file_has_perm(cred, file, FILE__LOCK);
Eric Paris828dfe12008-04-17 13:17:49 -04003647 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 }
3649
3650 return err;
3651}
3652
Jeff Laytone0b93ed2014-08-22 11:27:32 -04003653static void selinux_file_set_fowner(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 struct file_security_struct *fsec;
3656
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 fsec = file->f_security;
David Howells275bb412008-11-14 10:39:19 +11003658 fsec->fown_sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659}
3660
3661static int selinux_file_send_sigiotask(struct task_struct *tsk,
3662 struct fown_struct *fown, int signum)
3663{
Eric Paris828dfe12008-04-17 13:17:49 -04003664 struct file *file;
Stephen Smalley65c90bc2009-05-04 15:43:18 -04003665 u32 sid = task_sid(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 struct file_security_struct *fsec;
3668
3669 /* struct fown_struct is never outside the context of a struct file */
Eric Paris828dfe12008-04-17 13:17:49 -04003670 file = container_of(fown, struct file, f_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 fsec = file->f_security;
3673
3674 if (!signum)
3675 perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
3676 else
3677 perm = signal_to_av(signum);
3678
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003679 return avc_has_perm(&selinux_state,
3680 fsec->fown_sid, sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 SECCLASS_PROCESS, perm, NULL);
3682}
3683
3684static int selinux_file_receive(struct file *file)
3685{
David Howells88e67f32008-11-14 10:39:21 +11003686 const struct cred *cred = current_cred();
3687
3688 return file_has_perm(cred, file, file_to_av(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689}
3690
Al Viro94817692018-07-10 14:13:18 -04003691static int selinux_file_open(struct file *file)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003692{
3693 struct file_security_struct *fsec;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003694 struct inode_security_struct *isec;
David Howellsd84f4f92008-11-14 10:39:23 +11003695
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003696 fsec = file->f_security;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05003697 isec = inode_security(file_inode(file));
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003698 /*
3699 * Save inode label and policy sequence number
3700 * at open-time so that selinux_file_permission
3701 * can determine whether revalidation is necessary.
3702 * Task label is already saved in the file security
3703 * struct as its SID.
3704 */
3705 fsec->isid = isec->sid;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003706 fsec->pseqno = avc_policy_seqno(&selinux_state);
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003707 /*
3708 * Since the inode label or policy seqno may have changed
3709 * between the selinux_inode_permission check and the saving
3710 * of state above, recheck that access is still permitted.
3711 * Otherwise, access might never be revalidated against the
3712 * new inode label or new policy.
3713 * This check is not redundant - do not remove.
3714 */
Al Viro94817692018-07-10 14:13:18 -04003715 return file_path_has_perm(file->f_cred, file, open_file_to_av(file));
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003716}
3717
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718/* task security operations */
3719
Tetsuo Handaa79be232017-03-28 23:08:45 +09003720static int selinux_task_alloc(struct task_struct *task,
3721 unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003723 u32 sid = current_sid();
3724
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003725 return avc_has_perm(&selinux_state,
3726 sid, sid, SECCLASS_PROCESS, PROCESS__FORK, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727}
3728
David Howellsf1752ee2008-11-14 10:39:17 +11003729/*
David Howellsee18d642009-09-02 09:14:21 +01003730 * allocate the SELinux part of blank credentials
3731 */
3732static int selinux_cred_alloc_blank(struct cred *cred, gfp_t gfp)
3733{
3734 struct task_security_struct *tsec;
3735
3736 tsec = kzalloc(sizeof(struct task_security_struct), gfp);
3737 if (!tsec)
3738 return -ENOMEM;
3739
3740 cred->security = tsec;
3741 return 0;
3742}
3743
3744/*
David Howellsf1752ee2008-11-14 10:39:17 +11003745 * detach and free the LSM part of a set of credentials
3746 */
3747static void selinux_cred_free(struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748{
David Howellsf1752ee2008-11-14 10:39:17 +11003749 struct task_security_struct *tsec = cred->security;
David Howellse0e81732009-09-02 09:13:40 +01003750
Tetsuo Handa2edeaa32011-02-07 13:36:10 +00003751 /*
3752 * cred->security == NULL if security_cred_alloc_blank() or
3753 * security_prepare_creds() returned an error.
3754 */
3755 BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE);
David Howellse0e81732009-09-02 09:13:40 +01003756 cred->security = (void *) 0x7UL;
David Howellsf1752ee2008-11-14 10:39:17 +11003757 kfree(tsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758}
3759
David Howellsd84f4f92008-11-14 10:39:23 +11003760/*
3761 * prepare a new set of credentials for modification
3762 */
3763static int selinux_cred_prepare(struct cred *new, const struct cred *old,
3764 gfp_t gfp)
3765{
3766 const struct task_security_struct *old_tsec;
3767 struct task_security_struct *tsec;
3768
3769 old_tsec = old->security;
3770
3771 tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp);
3772 if (!tsec)
3773 return -ENOMEM;
3774
3775 new->security = tsec;
3776 return 0;
3777}
3778
3779/*
David Howellsee18d642009-09-02 09:14:21 +01003780 * transfer the SELinux data to a blank set of creds
3781 */
3782static void selinux_cred_transfer(struct cred *new, const struct cred *old)
3783{
3784 const struct task_security_struct *old_tsec = old->security;
3785 struct task_security_struct *tsec = new->security;
3786
3787 *tsec = *old_tsec;
3788}
3789
Matthew Garrett3ec30112018-01-08 13:36:19 -08003790static void selinux_cred_getsecid(const struct cred *c, u32 *secid)
3791{
3792 *secid = cred_sid(c);
3793}
3794
David Howellsee18d642009-09-02 09:14:21 +01003795/*
David Howells3a3b7ce2008-11-14 10:39:28 +11003796 * set the security data for a kernel service
3797 * - all the creation contexts are set to unlabelled
3798 */
3799static int selinux_kernel_act_as(struct cred *new, u32 secid)
3800{
3801 struct task_security_struct *tsec = new->security;
3802 u32 sid = current_sid();
3803 int ret;
3804
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003805 ret = avc_has_perm(&selinux_state,
3806 sid, secid,
David Howells3a3b7ce2008-11-14 10:39:28 +11003807 SECCLASS_KERNEL_SERVICE,
3808 KERNEL_SERVICE__USE_AS_OVERRIDE,
3809 NULL);
3810 if (ret == 0) {
3811 tsec->sid = secid;
3812 tsec->create_sid = 0;
3813 tsec->keycreate_sid = 0;
3814 tsec->sockcreate_sid = 0;
3815 }
3816 return ret;
3817}
3818
3819/*
3820 * set the file creation context in a security record to the same as the
3821 * objective context of the specified inode
3822 */
3823static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
3824{
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05003825 struct inode_security_struct *isec = inode_security(inode);
David Howells3a3b7ce2008-11-14 10:39:28 +11003826 struct task_security_struct *tsec = new->security;
3827 u32 sid = current_sid();
3828 int ret;
3829
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003830 ret = avc_has_perm(&selinux_state,
3831 sid, isec->sid,
David Howells3a3b7ce2008-11-14 10:39:28 +11003832 SECCLASS_KERNEL_SERVICE,
3833 KERNEL_SERVICE__CREATE_FILES_AS,
3834 NULL);
3835
3836 if (ret == 0)
3837 tsec->create_sid = isec->sid;
David Howellsef574712010-02-26 01:56:16 +00003838 return ret;
David Howells3a3b7ce2008-11-14 10:39:28 +11003839}
3840
Eric Parisdd8dbf22009-11-03 16:35:32 +11003841static int selinux_kernel_module_request(char *kmod_name)
Eric Paris25354c42009-08-13 09:45:03 -04003842{
Eric Parisdd8dbf22009-11-03 16:35:32 +11003843 struct common_audit_data ad;
3844
Eric Paris50c205f2012-04-04 15:01:43 -04003845 ad.type = LSM_AUDIT_DATA_KMOD;
Eric Parisdd8dbf22009-11-03 16:35:32 +11003846 ad.u.kmod_name = kmod_name;
3847
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003848 return avc_has_perm(&selinux_state,
3849 current_sid(), SECINITSID_KERNEL, SECCLASS_SYSTEM,
Eric Parisdd8dbf22009-11-03 16:35:32 +11003850 SYSTEM__MODULE_REQUEST, &ad);
Eric Paris25354c42009-08-13 09:45:03 -04003851}
3852
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003853static int selinux_kernel_module_from_file(struct file *file)
3854{
3855 struct common_audit_data ad;
3856 struct inode_security_struct *isec;
3857 struct file_security_struct *fsec;
3858 u32 sid = current_sid();
3859 int rc;
3860
3861 /* init_module */
3862 if (file == NULL)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003863 return avc_has_perm(&selinux_state,
3864 sid, sid, SECCLASS_SYSTEM,
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003865 SYSTEM__MODULE_LOAD, NULL);
3866
3867 /* finit_module */
Paul Moore20cdef82016-04-04 14:14:42 -04003868
Vivek Goyal43af5de2016-09-09 11:37:49 -04003869 ad.type = LSM_AUDIT_DATA_FILE;
3870 ad.u.file = file;
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003871
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003872 fsec = file->f_security;
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003873 if (sid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003874 rc = avc_has_perm(&selinux_state,
3875 sid, fsec->sid, SECCLASS_FD, FD__USE, &ad);
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003876 if (rc)
3877 return rc;
3878 }
3879
Paul Moore20cdef82016-04-04 14:14:42 -04003880 isec = inode_security(file_inode(file));
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003881 return avc_has_perm(&selinux_state,
3882 sid, isec->sid, SECCLASS_SYSTEM,
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003883 SYSTEM__MODULE_LOAD, &ad);
3884}
3885
3886static int selinux_kernel_read_file(struct file *file,
3887 enum kernel_read_file_id id)
3888{
3889 int rc = 0;
3890
3891 switch (id) {
3892 case READING_MODULE:
3893 rc = selinux_kernel_module_from_file(file);
3894 break;
3895 default:
3896 break;
3897 }
3898
3899 return rc;
3900}
3901
Mimi Zoharc77b8cd2018-07-13 14:06:02 -04003902static int selinux_kernel_load_data(enum kernel_load_data_id id)
3903{
3904 int rc = 0;
3905
3906 switch (id) {
3907 case LOADING_MODULE:
3908 rc = selinux_kernel_module_from_file(NULL);
3909 default:
3910 break;
3911 }
3912
3913 return rc;
3914}
3915
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
3917{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003918 return avc_has_perm(&selinux_state,
3919 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003920 PROCESS__SETPGID, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921}
3922
3923static int selinux_task_getpgid(struct task_struct *p)
3924{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003925 return avc_has_perm(&selinux_state,
3926 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003927 PROCESS__GETPGID, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928}
3929
3930static int selinux_task_getsid(struct task_struct *p)
3931{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003932 return avc_has_perm(&selinux_state,
3933 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003934 PROCESS__GETSESSION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935}
3936
David Quigleyf9008e4c2006-06-30 01:55:46 -07003937static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
3938{
David Howells275bb412008-11-14 10:39:19 +11003939 *secid = task_sid(p);
David Quigleyf9008e4c2006-06-30 01:55:46 -07003940}
3941
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942static int selinux_task_setnice(struct task_struct *p, int nice)
3943{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003944 return avc_has_perm(&selinux_state,
3945 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003946 PROCESS__SETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947}
3948
James Morris03e68062006-06-23 02:03:58 -07003949static int selinux_task_setioprio(struct task_struct *p, int ioprio)
3950{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003951 return avc_has_perm(&selinux_state,
3952 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003953 PROCESS__SETSCHED, NULL);
James Morris03e68062006-06-23 02:03:58 -07003954}
3955
David Quigleya1836a42006-06-30 01:55:49 -07003956static int selinux_task_getioprio(struct task_struct *p)
3957{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003958 return avc_has_perm(&selinux_state,
3959 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003960 PROCESS__GETSCHED, NULL);
David Quigleya1836a42006-06-30 01:55:49 -07003961}
3962
Corentin LABBE42985552017-10-04 20:32:18 +02003963static int selinux_task_prlimit(const struct cred *cred, const struct cred *tcred,
3964 unsigned int flags)
Stephen Smalley791ec492017-02-17 07:57:00 -05003965{
3966 u32 av = 0;
3967
Stephen Smalley84e68852017-02-28 09:35:08 -05003968 if (!flags)
3969 return 0;
Stephen Smalley791ec492017-02-17 07:57:00 -05003970 if (flags & LSM_PRLIMIT_WRITE)
3971 av |= PROCESS__SETRLIMIT;
3972 if (flags & LSM_PRLIMIT_READ)
3973 av |= PROCESS__GETRLIMIT;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003974 return avc_has_perm(&selinux_state,
3975 cred_sid(cred), cred_sid(tcred),
Stephen Smalley791ec492017-02-17 07:57:00 -05003976 SECCLASS_PROCESS, av, NULL);
3977}
3978
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003979static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
3980 struct rlimit *new_rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981{
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003982 struct rlimit *old_rlim = p->signal->rlim + resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983
3984 /* Control the ability to change the hard limit (whether
3985 lowering or raising it), so that the hard limit can
3986 later be used as a safe reset point for the soft limit
David Howellsd84f4f92008-11-14 10:39:23 +11003987 upon context transitions. See selinux_bprm_committing_creds. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 if (old_rlim->rlim_max != new_rlim->rlim_max)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003989 return avc_has_perm(&selinux_state,
3990 current_sid(), task_sid(p),
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003991 SECCLASS_PROCESS, PROCESS__SETRLIMIT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992
3993 return 0;
3994}
3995
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09003996static int selinux_task_setscheduler(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003998 return avc_has_perm(&selinux_state,
3999 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004000 PROCESS__SETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001}
4002
4003static int selinux_task_getscheduler(struct task_struct *p)
4004{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004005 return avc_has_perm(&selinux_state,
4006 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004007 PROCESS__GETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008}
4009
David Quigley35601542006-06-23 02:04:01 -07004010static int selinux_task_movememory(struct task_struct *p)
4011{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004012 return avc_has_perm(&selinux_state,
4013 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004014 PROCESS__SETSCHED, NULL);
David Quigley35601542006-06-23 02:04:01 -07004015}
4016
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02004017static int selinux_task_kill(struct task_struct *p, struct kernel_siginfo *info,
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004018 int sig, const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019{
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004020 u32 secid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 if (!sig)
4024 perm = PROCESS__SIGNULL; /* null signal; existence test */
4025 else
4026 perm = signal_to_av(sig);
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004027 if (!cred)
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004028 secid = current_sid();
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004029 else
4030 secid = cred_sid(cred);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004031 return avc_has_perm(&selinux_state,
4032 secid, task_sid(p), SECCLASS_PROCESS, perm, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033}
4034
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035static void selinux_task_to_inode(struct task_struct *p,
4036 struct inode *inode)
4037{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 struct inode_security_struct *isec = inode->i_security;
David Howells275bb412008-11-14 10:39:19 +11004039 u32 sid = task_sid(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004041 spin_lock(&isec->lock);
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01004042 isec->sclass = inode_mode_to_security_class(inode->i_mode);
David Howells275bb412008-11-14 10:39:19 +11004043 isec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004044 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004045 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046}
4047
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004049static int selinux_parse_skb_ipv4(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04004050 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051{
4052 int offset, ihlen, ret = -EINVAL;
4053 struct iphdr _iph, *ih;
4054
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03004055 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
4057 if (ih == NULL)
4058 goto out;
4059
4060 ihlen = ih->ihl * 4;
4061 if (ihlen < sizeof(_iph))
4062 goto out;
4063
Eric Paris48c62af2012-04-02 13:15:44 -04004064 ad->u.net->v4info.saddr = ih->saddr;
4065 ad->u.net->v4info.daddr = ih->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 ret = 0;
4067
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004068 if (proto)
4069 *proto = ih->protocol;
4070
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 switch (ih->protocol) {
Eric Paris828dfe12008-04-17 13:17:49 -04004072 case IPPROTO_TCP: {
4073 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074
Eric Paris828dfe12008-04-17 13:17:49 -04004075 if (ntohs(ih->frag_off) & IP_OFFSET)
4076 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077
4078 offset += ihlen;
4079 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4080 if (th == NULL)
4081 break;
4082
Eric Paris48c62af2012-04-02 13:15:44 -04004083 ad->u.net->sport = th->source;
4084 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087
Eric Paris828dfe12008-04-17 13:17:49 -04004088 case IPPROTO_UDP: {
4089 struct udphdr _udph, *uh;
4090
4091 if (ntohs(ih->frag_off) & IP_OFFSET)
4092 break;
4093
4094 offset += ihlen;
4095 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4096 if (uh == NULL)
4097 break;
4098
Eric Paris48c62af2012-04-02 13:15:44 -04004099 ad->u.net->sport = uh->source;
4100 ad->u.net->dport = uh->dest;
Eric Paris828dfe12008-04-17 13:17:49 -04004101 break;
4102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103
James Morris2ee92d42006-11-13 16:09:01 -08004104 case IPPROTO_DCCP: {
4105 struct dccp_hdr _dccph, *dh;
4106
4107 if (ntohs(ih->frag_off) & IP_OFFSET)
4108 break;
4109
4110 offset += ihlen;
4111 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4112 if (dh == NULL)
4113 break;
4114
Eric Paris48c62af2012-04-02 13:15:44 -04004115 ad->u.net->sport = dh->dccph_sport;
4116 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08004117 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004118 }
James Morris2ee92d42006-11-13 16:09:01 -08004119
Richard Hainesd4529302018-02-13 20:57:18 +00004120#if IS_ENABLED(CONFIG_IP_SCTP)
4121 case IPPROTO_SCTP: {
4122 struct sctphdr _sctph, *sh;
4123
4124 if (ntohs(ih->frag_off) & IP_OFFSET)
4125 break;
4126
4127 offset += ihlen;
4128 sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4129 if (sh == NULL)
4130 break;
4131
4132 ad->u.net->sport = sh->source;
4133 ad->u.net->dport = sh->dest;
4134 break;
4135 }
4136#endif
Eric Paris828dfe12008-04-17 13:17:49 -04004137 default:
4138 break;
4139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140out:
4141 return ret;
4142}
4143
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04004144#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145
4146/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004147static int selinux_parse_skb_ipv6(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04004148 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149{
4150 u8 nexthdr;
4151 int ret = -EINVAL, offset;
4152 struct ipv6hdr _ipv6h, *ip6;
Jesse Gross75f28112011-11-30 17:05:51 -08004153 __be16 frag_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03004155 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
4157 if (ip6 == NULL)
4158 goto out;
4159
Eric Paris48c62af2012-04-02 13:15:44 -04004160 ad->u.net->v6info.saddr = ip6->saddr;
4161 ad->u.net->v6info.daddr = ip6->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 ret = 0;
4163
4164 nexthdr = ip6->nexthdr;
4165 offset += sizeof(_ipv6h);
Jesse Gross75f28112011-11-30 17:05:51 -08004166 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 if (offset < 0)
4168 goto out;
4169
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004170 if (proto)
4171 *proto = nexthdr;
4172
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 switch (nexthdr) {
4174 case IPPROTO_TCP: {
Eric Paris828dfe12008-04-17 13:17:49 -04004175 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176
4177 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4178 if (th == NULL)
4179 break;
4180
Eric Paris48c62af2012-04-02 13:15:44 -04004181 ad->u.net->sport = th->source;
4182 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 break;
4184 }
4185
4186 case IPPROTO_UDP: {
4187 struct udphdr _udph, *uh;
4188
4189 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4190 if (uh == NULL)
4191 break;
4192
Eric Paris48c62af2012-04-02 13:15:44 -04004193 ad->u.net->sport = uh->source;
4194 ad->u.net->dport = uh->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 break;
4196 }
4197
James Morris2ee92d42006-11-13 16:09:01 -08004198 case IPPROTO_DCCP: {
4199 struct dccp_hdr _dccph, *dh;
4200
4201 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4202 if (dh == NULL)
4203 break;
4204
Eric Paris48c62af2012-04-02 13:15:44 -04004205 ad->u.net->sport = dh->dccph_sport;
4206 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08004207 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004208 }
James Morris2ee92d42006-11-13 16:09:01 -08004209
Richard Hainesd4529302018-02-13 20:57:18 +00004210#if IS_ENABLED(CONFIG_IP_SCTP)
4211 case IPPROTO_SCTP: {
4212 struct sctphdr _sctph, *sh;
4213
4214 sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4215 if (sh == NULL)
4216 break;
4217
4218 ad->u.net->sport = sh->source;
4219 ad->u.net->dport = sh->dest;
4220 break;
4221 }
4222#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 /* includes fragments */
4224 default:
4225 break;
4226 }
4227out:
4228 return ret;
4229}
4230
4231#endif /* IPV6 */
4232
Thomas Liu2bf49692009-07-14 12:14:09 -04004233static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
David Howellscf9481e2008-07-27 21:31:07 +10004234 char **_addrp, int src, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235{
David Howellscf9481e2008-07-27 21:31:07 +10004236 char *addrp;
4237 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238
Eric Paris48c62af2012-04-02 13:15:44 -04004239 switch (ad->u.net->family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 case PF_INET:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004241 ret = selinux_parse_skb_ipv4(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10004242 if (ret)
4243 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04004244 addrp = (char *)(src ? &ad->u.net->v4info.saddr :
4245 &ad->u.net->v4info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10004246 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04004248#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249 case PF_INET6:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004250 ret = selinux_parse_skb_ipv6(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10004251 if (ret)
4252 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04004253 addrp = (char *)(src ? &ad->u.net->v6info.saddr :
4254 &ad->u.net->v6info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10004255 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256#endif /* IPV6 */
4257 default:
David Howellscf9481e2008-07-27 21:31:07 +10004258 addrp = NULL;
4259 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 }
4261
David Howellscf9481e2008-07-27 21:31:07 +10004262parse_error:
peter enderborgc103a912018-06-12 10:09:03 +02004263 pr_warn(
David Howellscf9481e2008-07-27 21:31:07 +10004264 "SELinux: failure in selinux_parse_skb(),"
4265 " unable to parse packet\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 return ret;
David Howellscf9481e2008-07-27 21:31:07 +10004267
4268okay:
4269 if (_addrp)
4270 *_addrp = addrp;
4271 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272}
4273
Paul Moore4f6a9932007-03-01 14:35:22 -05004274/**
Paul Moore220deb92008-01-29 08:38:23 -05004275 * selinux_skb_peerlbl_sid - Determine the peer label of a packet
Paul Moore4f6a9932007-03-01 14:35:22 -05004276 * @skb: the packet
Paul Moore75e22912008-01-29 08:38:04 -05004277 * @family: protocol family
Paul Moore220deb92008-01-29 08:38:23 -05004278 * @sid: the packet's peer label SID
Paul Moore4f6a9932007-03-01 14:35:22 -05004279 *
4280 * Description:
Paul Moore220deb92008-01-29 08:38:23 -05004281 * Check the various different forms of network peer labeling and determine
4282 * the peer label/SID for the packet; most of the magic actually occurs in
4283 * the security server function security_net_peersid_cmp(). The function
4284 * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
4285 * or -EACCES if @sid is invalid due to inconsistencies with the different
4286 * peer labels.
Paul Moore4f6a9932007-03-01 14:35:22 -05004287 *
4288 */
Paul Moore220deb92008-01-29 08:38:23 -05004289static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
Paul Moore4f6a9932007-03-01 14:35:22 -05004290{
Paul Moore71f1cb02008-01-29 08:51:16 -05004291 int err;
Paul Moore4f6a9932007-03-01 14:35:22 -05004292 u32 xfrm_sid;
4293 u32 nlbl_sid;
Paul Moore220deb92008-01-29 08:38:23 -05004294 u32 nlbl_type;
Paul Moore4f6a9932007-03-01 14:35:22 -05004295
Paul Moore817eff72013-12-10 14:57:54 -05004296 err = selinux_xfrm_skb_sid(skb, &xfrm_sid);
Paul Moorebed4d7e2013-07-23 17:38:40 -04004297 if (unlikely(err))
4298 return -EACCES;
4299 err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
4300 if (unlikely(err))
4301 return -EACCES;
Paul Moore220deb92008-01-29 08:38:23 -05004302
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004303 err = security_net_peersid_resolve(&selinux_state, nlbl_sid,
4304 nlbl_type, xfrm_sid, sid);
Paul Moore71f1cb02008-01-29 08:51:16 -05004305 if (unlikely(err)) {
peter enderborgc103a912018-06-12 10:09:03 +02004306 pr_warn(
Paul Moore71f1cb02008-01-29 08:51:16 -05004307 "SELinux: failure in selinux_skb_peerlbl_sid(),"
4308 " unable to determine packet's peer label\n");
Paul Moore220deb92008-01-29 08:38:23 -05004309 return -EACCES;
Paul Moore71f1cb02008-01-29 08:51:16 -05004310 }
Paul Moore220deb92008-01-29 08:38:23 -05004311
4312 return 0;
Paul Moore4f6a9932007-03-01 14:35:22 -05004313}
4314
Paul Moore446b8022013-12-04 16:10:51 -05004315/**
4316 * selinux_conn_sid - Determine the child socket label for a connection
4317 * @sk_sid: the parent socket's SID
4318 * @skb_sid: the packet's SID
4319 * @conn_sid: the resulting connection SID
4320 *
4321 * If @skb_sid is valid then the user:role:type information from @sk_sid is
4322 * combined with the MLS information from @skb_sid in order to create
4323 * @conn_sid. If @skb_sid is not valid then then @conn_sid is simply a copy
4324 * of @sk_sid. Returns zero on success, negative values on failure.
4325 *
4326 */
4327static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid)
4328{
4329 int err = 0;
4330
4331 if (skb_sid != SECSID_NULL)
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004332 err = security_sid_mls_copy(&selinux_state, sk_sid, skb_sid,
4333 conn_sid);
Paul Moore446b8022013-12-04 16:10:51 -05004334 else
4335 *conn_sid = sk_sid;
4336
4337 return err;
4338}
4339
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340/* socket security operations */
Paul Moored4f2d972010-04-22 14:46:18 -04004341
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004342static int socket_sockcreate_sid(const struct task_security_struct *tsec,
4343 u16 secclass, u32 *socksid)
Paul Moored4f2d972010-04-22 14:46:18 -04004344{
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004345 if (tsec->sockcreate_sid > SECSID_NULL) {
4346 *socksid = tsec->sockcreate_sid;
4347 return 0;
4348 }
4349
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004350 return security_transition_sid(&selinux_state, tsec->sid, tsec->sid,
4351 secclass, NULL, socksid);
Paul Moored4f2d972010-04-22 14:46:18 -04004352}
4353
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004354static int sock_has_perm(struct sock *sk, u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355{
Paul Moore253bfae2010-04-22 14:46:19 -04004356 struct sk_security_struct *sksec = sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004357 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004358 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359
Paul Moore253bfae2010-04-22 14:46:19 -04004360 if (sksec->sid == SECINITSID_KERNEL)
4361 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362
Eric Paris50c205f2012-04-04 15:01:43 -04004363 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004364 ad.u.net = &net;
4365 ad.u.net->sk = sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004367 return avc_has_perm(&selinux_state,
4368 current_sid(), sksec->sid, sksec->sclass, perms,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004369 &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370}
4371
4372static int selinux_socket_create(int family, int type,
4373 int protocol, int kern)
4374{
Paul Moore5fb49872010-04-22 14:46:19 -04004375 const struct task_security_struct *tsec = current_security();
Paul Moored4f2d972010-04-22 14:46:18 -04004376 u32 newsid;
David Howells275bb412008-11-14 10:39:19 +11004377 u16 secclass;
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004378 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379
4380 if (kern)
Paul Moored4f2d972010-04-22 14:46:18 -04004381 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382
David Howells275bb412008-11-14 10:39:19 +11004383 secclass = socket_type_to_security_class(family, type, protocol);
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004384 rc = socket_sockcreate_sid(tsec, secclass, &newsid);
4385 if (rc)
4386 return rc;
4387
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004388 return avc_has_perm(&selinux_state,
4389 tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390}
4391
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07004392static int selinux_socket_post_create(struct socket *sock, int family,
4393 int type, int protocol, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394{
Paul Moore5fb49872010-04-22 14:46:19 -04004395 const struct task_security_struct *tsec = current_security();
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05004396 struct inode_security_struct *isec = inode_security_novalidate(SOCK_INODE(sock));
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004397 struct sk_security_struct *sksec;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004398 u16 sclass = socket_type_to_security_class(family, type, protocol);
4399 u32 sid = SECINITSID_KERNEL;
David Howells275bb412008-11-14 10:39:19 +11004400 int err = 0;
4401
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004402 if (!kern) {
4403 err = socket_sockcreate_sid(tsec, sclass, &sid);
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004404 if (err)
4405 return err;
4406 }
David Howells275bb412008-11-14 10:39:19 +11004407
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004408 isec->sclass = sclass;
4409 isec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004410 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004412 if (sock->sk) {
4413 sksec = sock->sk->sk_security;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004414 sksec->sclass = sclass;
4415 sksec->sid = sid;
Richard Hainesd4529302018-02-13 20:57:18 +00004416 /* Allows detection of the first association on this socket */
4417 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4418 sksec->sctp_assoc_state = SCTP_ASSOC_UNSET;
4419
Paul Moore389fb8002009-03-27 17:10:34 -04004420 err = selinux_netlbl_socket_post_create(sock->sk, family);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004421 }
4422
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07004423 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424}
4425
David Herrmann0b811db2018-05-04 16:28:21 +02004426static int selinux_socket_socketpair(struct socket *socka,
4427 struct socket *sockb)
4428{
4429 struct sk_security_struct *sksec_a = socka->sk->sk_security;
4430 struct sk_security_struct *sksec_b = sockb->sk->sk_security;
4431
4432 sksec_a->peer_sid = sksec_b->sid;
4433 sksec_b->peer_sid = sksec_a->sid;
4434
4435 return 0;
4436}
4437
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438/* Range of port numbers used to automatically bind.
4439 Need to determine whether we should perform a name_bind
4440 permission check between the socket and the port number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441
4442static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
4443{
Paul Moore253bfae2010-04-22 14:46:19 -04004444 struct sock *sk = sock->sk;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004445 struct sk_security_struct *sksec = sk->sk_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446 u16 family;
4447 int err;
4448
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004449 err = sock_has_perm(sk, SOCKET__BIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 if (err)
4451 goto out;
4452
Richard Hainesd4529302018-02-13 20:57:18 +00004453 /* If PF_INET or PF_INET6, check name_bind permission for the port. */
Paul Moore253bfae2010-04-22 14:46:19 -04004454 family = sk->sk_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 if (family == PF_INET || family == PF_INET6) {
4456 char *addrp;
Thomas Liu2bf49692009-07-14 12:14:09 -04004457 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004458 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 struct sockaddr_in *addr4 = NULL;
4460 struct sockaddr_in6 *addr6 = NULL;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004461 u16 family_sa = address->sa_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462 unsigned short snum;
James Morrise399f982008-06-12 01:39:58 +10004463 u32 sid, node_perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464
Richard Hainesd4529302018-02-13 20:57:18 +00004465 /*
4466 * sctp_bindx(3) calls via selinux_sctp_bind_connect()
4467 * that validates multiple binding addresses. Because of this
4468 * need to check address->sa_family as it is possible to have
4469 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4470 */
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004471 switch (family_sa) {
4472 case AF_UNSPEC:
Richard Haines68741a8a2018-03-02 19:54:34 +00004473 case AF_INET:
4474 if (addrlen < sizeof(struct sockaddr_in))
4475 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 addr4 = (struct sockaddr_in *)address;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004477 if (family_sa == AF_UNSPEC) {
4478 /* see __inet_bind(), we only want to allow
4479 * AF_UNSPEC if the address is INADDR_ANY
4480 */
4481 if (addr4->sin_addr.s_addr != htonl(INADDR_ANY))
4482 goto err_af;
4483 family_sa = AF_INET;
4484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 snum = ntohs(addr4->sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 addrp = (char *)&addr4->sin_addr.s_addr;
Richard Haines68741a8a2018-03-02 19:54:34 +00004487 break;
4488 case AF_INET6:
4489 if (addrlen < SIN6_LEN_RFC2133)
4490 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 addr6 = (struct sockaddr_in6 *)address;
4492 snum = ntohs(addr6->sin6_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 addrp = (char *)&addr6->sin6_addr.s6_addr;
Richard Haines68741a8a2018-03-02 19:54:34 +00004494 break;
4495 default:
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004496 goto err_af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 }
4498
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004499 ad.type = LSM_AUDIT_DATA_NET;
4500 ad.u.net = &net;
4501 ad.u.net->sport = htons(snum);
4502 ad.u.net->family = family_sa;
4503
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004504 if (snum) {
4505 int low, high;
4506
Eric W. Biederman0bbf87d2013-09-28 14:10:59 -07004507 inet_get_local_port_range(sock_net(sk), &low, &high);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004508
Krister Johansen4548b682017-01-20 17:49:11 -08004509 if (snum < max(inet_prot_sock(sock_net(sk)), low) ||
4510 snum > high) {
Paul Moore3e1121722008-04-10 10:48:14 -04004511 err = sel_netport_sid(sk->sk_protocol,
4512 snum, &sid);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004513 if (err)
4514 goto out;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004515 err = avc_has_perm(&selinux_state,
4516 sksec->sid, sid,
Paul Moore253bfae2010-04-22 14:46:19 -04004517 sksec->sclass,
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004518 SOCKET__NAME_BIND, &ad);
4519 if (err)
4520 goto out;
4521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522 }
Eric Paris828dfe12008-04-17 13:17:49 -04004523
Paul Moore253bfae2010-04-22 14:46:19 -04004524 switch (sksec->sclass) {
James Morris13402582005-09-30 14:24:34 -04004525 case SECCLASS_TCP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 node_perm = TCP_SOCKET__NODE_BIND;
4527 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004528
James Morris13402582005-09-30 14:24:34 -04004529 case SECCLASS_UDP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 node_perm = UDP_SOCKET__NODE_BIND;
4531 break;
James Morris2ee92d42006-11-13 16:09:01 -08004532
4533 case SECCLASS_DCCP_SOCKET:
4534 node_perm = DCCP_SOCKET__NODE_BIND;
4535 break;
4536
Richard Hainesd4529302018-02-13 20:57:18 +00004537 case SECCLASS_SCTP_SOCKET:
4538 node_perm = SCTP_SOCKET__NODE_BIND;
4539 break;
4540
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541 default:
4542 node_perm = RAWIP_SOCKET__NODE_BIND;
4543 break;
4544 }
Eric Paris828dfe12008-04-17 13:17:49 -04004545
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004546 err = sel_netnode_sid(addrp, family_sa, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 if (err)
4548 goto out;
Eric Paris828dfe12008-04-17 13:17:49 -04004549
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004550 if (family_sa == AF_INET)
Eric Paris48c62af2012-04-02 13:15:44 -04004551 ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552 else
Eric Paris48c62af2012-04-02 13:15:44 -04004553 ad.u.net->v6info.saddr = addr6->sin6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004555 err = avc_has_perm(&selinux_state,
4556 sksec->sid, sid,
Paul Moore253bfae2010-04-22 14:46:19 -04004557 sksec->sclass, node_perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 if (err)
4559 goto out;
4560 }
4561out:
4562 return err;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004563err_af:
4564 /* Note that SCTP services expect -EINVAL, others -EAFNOSUPPORT. */
4565 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4566 return -EINVAL;
4567 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568}
4569
Richard Hainesd4529302018-02-13 20:57:18 +00004570/* This supports connect(2) and SCTP connect services such as sctp_connectx(3)
Mauro Carvalho Chehab5fb94e92018-05-08 15:14:57 -03004571 * and sctp_sendmsg(3) as described in Documentation/security/LSM-sctp.rst
Richard Hainesd4529302018-02-13 20:57:18 +00004572 */
4573static int selinux_socket_connect_helper(struct socket *sock,
4574 struct sockaddr *address, int addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575{
Paul Moore014ab192008-10-10 10:16:33 -04004576 struct sock *sk = sock->sk;
Paul Moore253bfae2010-04-22 14:46:19 -04004577 struct sk_security_struct *sksec = sk->sk_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 int err;
4579
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004580 err = sock_has_perm(sk, SOCKET__CONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 if (err)
4582 return err;
4583
4584 /*
Richard Hainesd4529302018-02-13 20:57:18 +00004585 * If a TCP, DCCP or SCTP socket, check name_connect permission
4586 * for the port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 */
Paul Moore253bfae2010-04-22 14:46:19 -04004588 if (sksec->sclass == SECCLASS_TCP_SOCKET ||
Richard Hainesd4529302018-02-13 20:57:18 +00004589 sksec->sclass == SECCLASS_DCCP_SOCKET ||
4590 sksec->sclass == SECCLASS_SCTP_SOCKET) {
Thomas Liu2bf49692009-07-14 12:14:09 -04004591 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004592 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 struct sockaddr_in *addr4 = NULL;
4594 struct sockaddr_in6 *addr6 = NULL;
4595 unsigned short snum;
James Morris2ee92d42006-11-13 16:09:01 -08004596 u32 sid, perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597
Richard Hainesd4529302018-02-13 20:57:18 +00004598 /* sctp_connectx(3) calls via selinux_sctp_bind_connect()
4599 * that validates multiple connect addresses. Because of this
4600 * need to check address->sa_family as it is possible to have
4601 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4602 */
Richard Haines68741a8a2018-03-02 19:54:34 +00004603 switch (address->sa_family) {
4604 case AF_INET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605 addr4 = (struct sockaddr_in *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004606 if (addrlen < sizeof(struct sockaddr_in))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 return -EINVAL;
4608 snum = ntohs(addr4->sin_port);
Richard Haines68741a8a2018-03-02 19:54:34 +00004609 break;
4610 case AF_INET6:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 addr6 = (struct sockaddr_in6 *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004612 if (addrlen < SIN6_LEN_RFC2133)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 return -EINVAL;
4614 snum = ntohs(addr6->sin6_port);
Richard Haines68741a8a2018-03-02 19:54:34 +00004615 break;
4616 default:
4617 /* Note that SCTP services expect -EINVAL, whereas
4618 * others expect -EAFNOSUPPORT.
4619 */
4620 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4621 return -EINVAL;
4622 else
4623 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 }
4625
Paul Moore3e1121722008-04-10 10:48:14 -04004626 err = sel_netport_sid(sk->sk_protocol, snum, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627 if (err)
Richard Hainesd4529302018-02-13 20:57:18 +00004628 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629
Richard Hainesd4529302018-02-13 20:57:18 +00004630 switch (sksec->sclass) {
4631 case SECCLASS_TCP_SOCKET:
4632 perm = TCP_SOCKET__NAME_CONNECT;
4633 break;
4634 case SECCLASS_DCCP_SOCKET:
4635 perm = DCCP_SOCKET__NAME_CONNECT;
4636 break;
4637 case SECCLASS_SCTP_SOCKET:
4638 perm = SCTP_SOCKET__NAME_CONNECT;
4639 break;
4640 }
James Morris2ee92d42006-11-13 16:09:01 -08004641
Eric Paris50c205f2012-04-04 15:01:43 -04004642 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004643 ad.u.net = &net;
4644 ad.u.net->dport = htons(snum);
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004645 ad.u.net->family = address->sa_family;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004646 err = avc_has_perm(&selinux_state,
4647 sksec->sid, sid, sksec->sclass, perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 if (err)
Richard Hainesd4529302018-02-13 20:57:18 +00004649 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 }
4651
Richard Hainesd4529302018-02-13 20:57:18 +00004652 return 0;
4653}
Paul Moore014ab192008-10-10 10:16:33 -04004654
Richard Hainesd4529302018-02-13 20:57:18 +00004655/* Supports connect(2), see comments in selinux_socket_connect_helper() */
4656static int selinux_socket_connect(struct socket *sock,
4657 struct sockaddr *address, int addrlen)
4658{
4659 int err;
4660 struct sock *sk = sock->sk;
4661
4662 err = selinux_socket_connect_helper(sock, address, addrlen);
4663 if (err)
4664 return err;
4665
4666 return selinux_netlbl_socket_connect(sk, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667}
4668
4669static int selinux_socket_listen(struct socket *sock, int backlog)
4670{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004671 return sock_has_perm(sock->sk, SOCKET__LISTEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672}
4673
4674static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
4675{
4676 int err;
4677 struct inode_security_struct *isec;
4678 struct inode_security_struct *newisec;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004679 u16 sclass;
4680 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004682 err = sock_has_perm(sock->sk, SOCKET__ACCEPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683 if (err)
4684 return err;
4685
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05004686 isec = inode_security_novalidate(SOCK_INODE(sock));
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004687 spin_lock(&isec->lock);
4688 sclass = isec->sclass;
4689 sid = isec->sid;
4690 spin_unlock(&isec->lock);
4691
4692 newisec = inode_security_novalidate(SOCK_INODE(newsock));
4693 newisec->sclass = sclass;
4694 newisec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004695 newisec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696
4697 return 0;
4698}
4699
4700static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
Eric Paris828dfe12008-04-17 13:17:49 -04004701 int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004703 return sock_has_perm(sock->sk, SOCKET__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704}
4705
4706static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
4707 int size, int flags)
4708{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004709 return sock_has_perm(sock->sk, SOCKET__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710}
4711
4712static int selinux_socket_getsockname(struct socket *sock)
4713{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004714 return sock_has_perm(sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715}
4716
4717static int selinux_socket_getpeername(struct socket *sock)
4718{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004719 return sock_has_perm(sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720}
4721
Eric Paris828dfe12008-04-17 13:17:49 -04004722static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723{
Paul Mooref8687af2006-10-30 15:22:15 -08004724 int err;
4725
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004726 err = sock_has_perm(sock->sk, SOCKET__SETOPT);
Paul Mooref8687af2006-10-30 15:22:15 -08004727 if (err)
4728 return err;
4729
4730 return selinux_netlbl_socket_setsockopt(sock, level, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731}
4732
4733static int selinux_socket_getsockopt(struct socket *sock, int level,
4734 int optname)
4735{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004736 return sock_has_perm(sock->sk, SOCKET__GETOPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737}
4738
4739static int selinux_socket_shutdown(struct socket *sock, int how)
4740{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004741 return sock_has_perm(sock->sk, SOCKET__SHUTDOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742}
4743
David S. Miller3610cda2011-01-05 15:38:53 -08004744static int selinux_socket_unix_stream_connect(struct sock *sock,
4745 struct sock *other,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 struct sock *newsk)
4747{
David S. Miller3610cda2011-01-05 15:38:53 -08004748 struct sk_security_struct *sksec_sock = sock->sk_security;
4749 struct sk_security_struct *sksec_other = other->sk_security;
Paul Moore4d1e2452010-04-22 14:46:18 -04004750 struct sk_security_struct *sksec_new = newsk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004751 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004752 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 int err;
4754
Eric Paris50c205f2012-04-04 15:01:43 -04004755 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004756 ad.u.net = &net;
4757 ad.u.net->sk = other;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004759 err = avc_has_perm(&selinux_state,
4760 sksec_sock->sid, sksec_other->sid,
Paul Moore4d1e2452010-04-22 14:46:18 -04004761 sksec_other->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762 UNIX_STREAM_SOCKET__CONNECTTO, &ad);
4763 if (err)
4764 return err;
4765
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 /* server child socket */
Paul Moore4d1e2452010-04-22 14:46:18 -04004767 sksec_new->peer_sid = sksec_sock->sid;
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004768 err = security_sid_mls_copy(&selinux_state, sksec_other->sid,
4769 sksec_sock->sid, &sksec_new->sid);
Paul Moore4d1e2452010-04-22 14:46:18 -04004770 if (err)
4771 return err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004772
Paul Moore4d1e2452010-04-22 14:46:18 -04004773 /* connecting socket */
4774 sksec_sock->peer_sid = sksec_new->sid;
4775
4776 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777}
4778
4779static int selinux_socket_unix_may_send(struct socket *sock,
4780 struct socket *other)
4781{
Paul Moore253bfae2010-04-22 14:46:19 -04004782 struct sk_security_struct *ssec = sock->sk->sk_security;
4783 struct sk_security_struct *osec = other->sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004784 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004785 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786
Eric Paris50c205f2012-04-04 15:01:43 -04004787 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004788 ad.u.net = &net;
4789 ad.u.net->sk = other->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004791 return avc_has_perm(&selinux_state,
4792 ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
Paul Moore253bfae2010-04-22 14:46:19 -04004793 &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794}
4795
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004796static int selinux_inet_sys_rcv_skb(struct net *ns, int ifindex,
4797 char *addrp, u16 family, u32 peer_sid,
Thomas Liu2bf49692009-07-14 12:14:09 -04004798 struct common_audit_data *ad)
Paul Mooreeffad8d2008-01-29 08:49:27 -05004799{
4800 int err;
4801 u32 if_sid;
4802 u32 node_sid;
4803
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004804 err = sel_netif_sid(ns, ifindex, &if_sid);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004805 if (err)
4806 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004807 err = avc_has_perm(&selinux_state,
4808 peer_sid, if_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004809 SECCLASS_NETIF, NETIF__INGRESS, ad);
4810 if (err)
4811 return err;
4812
4813 err = sel_netnode_sid(addrp, family, &node_sid);
4814 if (err)
4815 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004816 return avc_has_perm(&selinux_state,
4817 peer_sid, node_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004818 SECCLASS_NODE, NODE__RECVFROM, ad);
4819}
4820
Paul Moore220deb92008-01-29 08:38:23 -05004821static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
Paul Moored8395c82008-10-10 10:16:30 -04004822 u16 family)
Paul Moore220deb92008-01-29 08:38:23 -05004823{
Paul Moore277d3422008-12-31 12:54:11 -05004824 int err = 0;
Paul Moore220deb92008-01-29 08:38:23 -05004825 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004826 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004827 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004828 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04004829 char *addrp;
4830
Eric Paris50c205f2012-04-04 15:01:43 -04004831 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004832 ad.u.net = &net;
4833 ad.u.net->netif = skb->skb_iif;
4834 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004835 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4836 if (err)
4837 return err;
Paul Moore220deb92008-01-29 08:38:23 -05004838
Paul Moore58bfbb52009-03-27 17:10:41 -04004839 if (selinux_secmark_enabled()) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004840 err = avc_has_perm(&selinux_state,
4841 sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Moored8395c82008-10-10 10:16:30 -04004842 PACKET__RECV, &ad);
Paul Moore58bfbb52009-03-27 17:10:41 -04004843 if (err)
4844 return err;
4845 }
Paul Moore220deb92008-01-29 08:38:23 -05004846
Steffen Klassertb9679a72011-02-23 12:55:21 +01004847 err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
4848 if (err)
4849 return err;
4850 err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004851
James Morris4e5ab4c2006-06-09 00:33:33 -07004852 return err;
4853}
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004854
James Morris4e5ab4c2006-06-09 00:33:33 -07004855static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
4856{
Paul Moore220deb92008-01-29 08:38:23 -05004857 int err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004858 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004859 u16 family = sk->sk_family;
4860 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004861 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004862 struct lsm_network_audit net = {0,};
Paul Moore220deb92008-01-29 08:38:23 -05004863 char *addrp;
Paul Moored8395c82008-10-10 10:16:30 -04004864 u8 secmark_active;
4865 u8 peerlbl_active;
James Morris4e5ab4c2006-06-09 00:33:33 -07004866
James Morris4e5ab4c2006-06-09 00:33:33 -07004867 if (family != PF_INET && family != PF_INET6)
Paul Moore220deb92008-01-29 08:38:23 -05004868 return 0;
James Morris4e5ab4c2006-06-09 00:33:33 -07004869
4870 /* Handle mapped IPv4 packets arriving via IPv6 sockets */
Al Viro87fcd702006-12-04 22:00:55 +00004871 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
James Morris4e5ab4c2006-06-09 00:33:33 -07004872 family = PF_INET;
4873
Paul Moored8395c82008-10-10 10:16:30 -04004874 /* If any sort of compatibility mode is enabled then handoff processing
4875 * to the selinux_sock_rcv_skb_compat() function to deal with the
4876 * special handling. We do this in an attempt to keep this function
4877 * as fast and as clean as possible. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004878 if (!selinux_policycap_netpeer())
Paul Moored8395c82008-10-10 10:16:30 -04004879 return selinux_sock_rcv_skb_compat(sk, skb, family);
4880
4881 secmark_active = selinux_secmark_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04004882 peerlbl_active = selinux_peerlbl_enabled();
Paul Moored8395c82008-10-10 10:16:30 -04004883 if (!secmark_active && !peerlbl_active)
4884 return 0;
4885
Eric Paris50c205f2012-04-04 15:01:43 -04004886 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004887 ad.u.net = &net;
4888 ad.u.net->netif = skb->skb_iif;
4889 ad.u.net->family = family;
Paul Moore224dfbd2008-01-29 08:38:13 -05004890 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
James Morris4e5ab4c2006-06-09 00:33:33 -07004891 if (err)
Paul Moore220deb92008-01-29 08:38:23 -05004892 return err;
James Morris4e5ab4c2006-06-09 00:33:33 -07004893
Paul Moored8395c82008-10-10 10:16:30 -04004894 if (peerlbl_active) {
Paul Moored621d352008-01-29 08:43:36 -05004895 u32 peer_sid;
4896
4897 err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
4898 if (err)
4899 return err;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004900 err = selinux_inet_sys_rcv_skb(sock_net(sk), skb->skb_iif,
4901 addrp, family, peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04004902 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04004903 selinux_netlbl_err(skb, family, err, 0);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004904 return err;
Paul Mooredfaebe92008-10-10 10:16:31 -04004905 }
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004906 err = avc_has_perm(&selinux_state,
4907 sk_sid, peer_sid, SECCLASS_PEER,
Paul Moored621d352008-01-29 08:43:36 -05004908 PEER__RECV, &ad);
Chad Hanson46d01d62013-12-23 17:45:01 -05004909 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04004910 selinux_netlbl_err(skb, family, err, 0);
Chad Hanson46d01d62013-12-23 17:45:01 -05004911 return err;
4912 }
Paul Moored621d352008-01-29 08:43:36 -05004913 }
4914
Paul Moored8395c82008-10-10 10:16:30 -04004915 if (secmark_active) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004916 err = avc_has_perm(&selinux_state,
4917 sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004918 PACKET__RECV, &ad);
4919 if (err)
4920 return err;
4921 }
4922
Paul Moored621d352008-01-29 08:43:36 -05004923 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924}
4925
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004926static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
4927 int __user *optlen, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928{
4929 int err = 0;
4930 char *scontext;
4931 u32 scontext_len;
Paul Moore253bfae2010-04-22 14:46:19 -04004932 struct sk_security_struct *sksec = sock->sk->sk_security;
Paul Moore3de4bab2006-11-17 17:38:54 -05004933 u32 peer_sid = SECSID_NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934
Paul Moore253bfae2010-04-22 14:46:19 -04004935 if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
Richard Hainesd4529302018-02-13 20:57:18 +00004936 sksec->sclass == SECCLASS_TCP_SOCKET ||
4937 sksec->sclass == SECCLASS_SCTP_SOCKET)
Eric Parisdd3e7832010-04-07 15:08:46 -04004938 peer_sid = sksec->peer_sid;
Paul Moore253bfae2010-04-22 14:46:19 -04004939 if (peer_sid == SECSID_NULL)
4940 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004942 err = security_sid_to_context(&selinux_state, peer_sid, &scontext,
4943 &scontext_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 if (err)
Paul Moore253bfae2010-04-22 14:46:19 -04004945 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946
4947 if (scontext_len > len) {
4948 err = -ERANGE;
4949 goto out_len;
4950 }
4951
4952 if (copy_to_user(optval, scontext, scontext_len))
4953 err = -EFAULT;
4954
4955out_len:
4956 if (put_user(scontext_len, optlen))
4957 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958 kfree(scontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 return err;
4960}
4961
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004962static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004963{
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004964 u32 peer_secid = SECSID_NULL;
Paul Moore75e22912008-01-29 08:38:04 -05004965 u16 family;
Paul Moore899134f2016-03-28 15:19:10 -04004966 struct inode_security_struct *isec;
Catherine Zhang877ce7c2006-06-29 12:27:47 -07004967
Paul Mooreaa862902008-10-10 10:16:29 -04004968 if (skb && skb->protocol == htons(ETH_P_IP))
4969 family = PF_INET;
4970 else if (skb && skb->protocol == htons(ETH_P_IPV6))
4971 family = PF_INET6;
4972 else if (sock)
Paul Moore75e22912008-01-29 08:38:04 -05004973 family = sock->sk->sk_family;
Paul Moore75e22912008-01-29 08:38:04 -05004974 else
4975 goto out;
4976
Paul Moore899134f2016-03-28 15:19:10 -04004977 if (sock && family == PF_UNIX) {
4978 isec = inode_security_novalidate(SOCK_INODE(sock));
4979 peer_secid = isec->sid;
4980 } else if (skb)
Paul Moore220deb92008-01-29 08:38:23 -05004981 selinux_skb_peerlbl_sid(skb, family, &peer_secid);
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004982
Paul Moore75e22912008-01-29 08:38:04 -05004983out:
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004984 *secid = peer_secid;
Paul Moore75e22912008-01-29 08:38:04 -05004985 if (peer_secid == SECSID_NULL)
4986 return -EINVAL;
4987 return 0;
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004988}
4989
Al Viro7d877f32005-10-21 03:20:43 -04004990static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991{
Paul Moore84914b72010-04-22 14:46:18 -04004992 struct sk_security_struct *sksec;
4993
4994 sksec = kzalloc(sizeof(*sksec), priority);
4995 if (!sksec)
4996 return -ENOMEM;
4997
4998 sksec->peer_sid = SECINITSID_UNLABELED;
4999 sksec->sid = SECINITSID_UNLABELED;
Stephen Smalley5dee25d2015-07-10 17:19:57 -04005000 sksec->sclass = SECCLASS_SOCKET;
Paul Moore84914b72010-04-22 14:46:18 -04005001 selinux_netlbl_sk_security_reset(sksec);
5002 sk->sk_security = sksec;
5003
5004 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005}
5006
5007static void selinux_sk_free_security(struct sock *sk)
5008{
Paul Moore84914b72010-04-22 14:46:18 -04005009 struct sk_security_struct *sksec = sk->sk_security;
5010
5011 sk->sk_security = NULL;
5012 selinux_netlbl_sk_security_free(sksec);
5013 kfree(sksec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014}
5015
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005016static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
5017{
Eric Parisdd3e7832010-04-07 15:08:46 -04005018 struct sk_security_struct *sksec = sk->sk_security;
5019 struct sk_security_struct *newsksec = newsk->sk_security;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005020
Eric Parisdd3e7832010-04-07 15:08:46 -04005021 newsksec->sid = sksec->sid;
5022 newsksec->peer_sid = sksec->peer_sid;
5023 newsksec->sclass = sksec->sclass;
Paul Moore99f59ed2006-08-29 17:53:48 -07005024
Eric Parisdd3e7832010-04-07 15:08:46 -04005025 selinux_netlbl_sk_security_reset(newsksec);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005026}
5027
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07005028static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005029{
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005030 if (!sk)
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07005031 *secid = SECINITSID_ANY_SOCKET;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005032 else {
5033 struct sk_security_struct *sksec = sk->sk_security;
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005034
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07005035 *secid = sksec->sid;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005036 }
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005037}
5038
Eric Paris828dfe12008-04-17 13:17:49 -04005039static void selinux_sock_graft(struct sock *sk, struct socket *parent)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005040{
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05005041 struct inode_security_struct *isec =
5042 inode_security_novalidate(SOCK_INODE(parent));
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005043 struct sk_security_struct *sksec = sk->sk_security;
5044
Paul Moore2873ead2014-07-28 10:42:48 -04005045 if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
5046 sk->sk_family == PF_UNIX)
David Woodhouse2148ccc2006-09-29 15:50:25 -07005047 isec->sid = sksec->sid;
Paul Moore220deb92008-01-29 08:38:23 -05005048 sksec->sclass = isec->sclass;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005049}
5050
Richard Hainesd4529302018-02-13 20:57:18 +00005051/* Called whenever SCTP receives an INIT chunk. This happens when an incoming
5052 * connect(2), sctp_connectx(3) or sctp_sendmsg(3) (with no association
5053 * already present).
5054 */
5055static int selinux_sctp_assoc_request(struct sctp_endpoint *ep,
5056 struct sk_buff *skb)
5057{
5058 struct sk_security_struct *sksec = ep->base.sk->sk_security;
5059 struct common_audit_data ad;
5060 struct lsm_network_audit net = {0,};
5061 u8 peerlbl_active;
5062 u32 peer_sid = SECINITSID_UNLABELED;
5063 u32 conn_sid;
5064 int err = 0;
5065
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005066 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005067 return 0;
5068
5069 peerlbl_active = selinux_peerlbl_enabled();
5070
5071 if (peerlbl_active) {
5072 /* This will return peer_sid = SECSID_NULL if there are
5073 * no peer labels, see security_net_peersid_resolve().
5074 */
5075 err = selinux_skb_peerlbl_sid(skb, ep->base.sk->sk_family,
5076 &peer_sid);
5077 if (err)
5078 return err;
5079
5080 if (peer_sid == SECSID_NULL)
5081 peer_sid = SECINITSID_UNLABELED;
5082 }
5083
5084 if (sksec->sctp_assoc_state == SCTP_ASSOC_UNSET) {
5085 sksec->sctp_assoc_state = SCTP_ASSOC_SET;
5086
5087 /* Here as first association on socket. As the peer SID
5088 * was allowed by peer recv (and the netif/node checks),
5089 * then it is approved by policy and used as the primary
5090 * peer SID for getpeercon(3).
5091 */
5092 sksec->peer_sid = peer_sid;
5093 } else if (sksec->peer_sid != peer_sid) {
5094 /* Other association peer SIDs are checked to enforce
5095 * consistency among the peer SIDs.
5096 */
5097 ad.type = LSM_AUDIT_DATA_NET;
5098 ad.u.net = &net;
5099 ad.u.net->sk = ep->base.sk;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005100 err = avc_has_perm(&selinux_state,
5101 sksec->peer_sid, peer_sid, sksec->sclass,
Richard Hainesd4529302018-02-13 20:57:18 +00005102 SCTP_SOCKET__ASSOCIATION, &ad);
5103 if (err)
5104 return err;
5105 }
5106
5107 /* Compute the MLS component for the connection and store
5108 * the information in ep. This will be used by SCTP TCP type
5109 * sockets and peeled off connections as they cause a new
5110 * socket to be generated. selinux_sctp_sk_clone() will then
5111 * plug this into the new socket.
5112 */
5113 err = selinux_conn_sid(sksec->sid, peer_sid, &conn_sid);
5114 if (err)
5115 return err;
5116
5117 ep->secid = conn_sid;
5118 ep->peer_secid = peer_sid;
5119
5120 /* Set any NetLabel labels including CIPSO/CALIPSO options. */
5121 return selinux_netlbl_sctp_assoc_request(ep, skb);
5122}
5123
5124/* Check if sctp IPv4/IPv6 addresses are valid for binding or connecting
5125 * based on their @optname.
5126 */
5127static int selinux_sctp_bind_connect(struct sock *sk, int optname,
5128 struct sockaddr *address,
5129 int addrlen)
5130{
5131 int len, err = 0, walk_size = 0;
5132 void *addr_buf;
5133 struct sockaddr *addr;
5134 struct socket *sock;
5135
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005136 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005137 return 0;
5138
5139 /* Process one or more addresses that may be IPv4 or IPv6 */
5140 sock = sk->sk_socket;
5141 addr_buf = address;
5142
5143 while (walk_size < addrlen) {
5144 addr = addr_buf;
5145 switch (addr->sa_family) {
Alexey Kodanev4152dc92018-05-11 20:15:13 +03005146 case AF_UNSPEC:
Richard Hainesd4529302018-02-13 20:57:18 +00005147 case AF_INET:
5148 len = sizeof(struct sockaddr_in);
5149 break;
5150 case AF_INET6:
5151 len = sizeof(struct sockaddr_in6);
5152 break;
5153 default:
Alexey Kodanev4152dc92018-05-11 20:15:13 +03005154 return -EINVAL;
Richard Hainesd4529302018-02-13 20:57:18 +00005155 }
5156
5157 err = -EINVAL;
5158 switch (optname) {
5159 /* Bind checks */
5160 case SCTP_PRIMARY_ADDR:
5161 case SCTP_SET_PEER_PRIMARY_ADDR:
5162 case SCTP_SOCKOPT_BINDX_ADD:
5163 err = selinux_socket_bind(sock, addr, len);
5164 break;
5165 /* Connect checks */
5166 case SCTP_SOCKOPT_CONNECTX:
5167 case SCTP_PARAM_SET_PRIMARY:
5168 case SCTP_PARAM_ADD_IP:
5169 case SCTP_SENDMSG_CONNECT:
5170 err = selinux_socket_connect_helper(sock, addr, len);
5171 if (err)
5172 return err;
5173
5174 /* As selinux_sctp_bind_connect() is called by the
5175 * SCTP protocol layer, the socket is already locked,
5176 * therefore selinux_netlbl_socket_connect_locked() is
5177 * is called here. The situations handled are:
5178 * sctp_connectx(3), sctp_sendmsg(3), sendmsg(2),
5179 * whenever a new IP address is added or when a new
5180 * primary address is selected.
5181 * Note that an SCTP connect(2) call happens before
5182 * the SCTP protocol layer and is handled via
5183 * selinux_socket_connect().
5184 */
5185 err = selinux_netlbl_socket_connect_locked(sk, addr);
5186 break;
5187 }
5188
5189 if (err)
5190 return err;
5191
5192 addr_buf += len;
5193 walk_size += len;
5194 }
5195
5196 return 0;
5197}
5198
5199/* Called whenever a new socket is created by accept(2) or sctp_peeloff(3). */
5200static void selinux_sctp_sk_clone(struct sctp_endpoint *ep, struct sock *sk,
5201 struct sock *newsk)
5202{
5203 struct sk_security_struct *sksec = sk->sk_security;
5204 struct sk_security_struct *newsksec = newsk->sk_security;
5205
5206 /* If policy does not support SECCLASS_SCTP_SOCKET then call
5207 * the non-sctp clone version.
5208 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005209 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005210 return selinux_sk_clone_security(sk, newsk);
5211
5212 newsksec->sid = ep->secid;
5213 newsksec->peer_sid = ep->peer_secid;
5214 newsksec->sclass = sksec->sclass;
5215 selinux_netlbl_sctp_sk_clone(sk, newsk);
5216}
5217
Adrian Bunk9a673e52006-08-15 00:03:53 -07005218static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
5219 struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005220{
5221 struct sk_security_struct *sksec = sk->sk_security;
5222 int err;
Paul Moore0b1f24e2013-12-03 11:39:13 -05005223 u16 family = req->rsk_ops->family;
Paul Moore446b8022013-12-04 16:10:51 -05005224 u32 connsid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005225 u32 peersid;
5226
Paul Mooreaa862902008-10-10 10:16:29 -04005227 err = selinux_skb_peerlbl_sid(skb, family, &peersid);
Paul Moore220deb92008-01-29 08:38:23 -05005228 if (err)
5229 return err;
Paul Moore446b8022013-12-04 16:10:51 -05005230 err = selinux_conn_sid(sksec->sid, peersid, &connsid);
5231 if (err)
5232 return err;
5233 req->secid = connsid;
5234 req->peer_secid = peersid;
Venkat Yekkiralaa51c64f2006-07-27 22:01:34 -07005235
Paul Moore389fb8002009-03-27 17:10:34 -04005236 return selinux_netlbl_inet_conn_request(req, family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005237}
5238
Adrian Bunk9a673e52006-08-15 00:03:53 -07005239static void selinux_inet_csk_clone(struct sock *newsk,
5240 const struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005241{
5242 struct sk_security_struct *newsksec = newsk->sk_security;
5243
5244 newsksec->sid = req->secid;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005245 newsksec->peer_sid = req->peer_secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005246 /* NOTE: Ideally, we should also get the isec->sid for the
5247 new socket in sync, but we don't have the isec available yet.
5248 So we will wait until sock_graft to do it, by which
5249 time it will have been created and available. */
Paul Moore99f59ed2006-08-29 17:53:48 -07005250
Paul Moore9f2ad662006-11-17 17:38:53 -05005251 /* We don't need to take any sort of lock here as we are the only
5252 * thread with access to newsksec */
Paul Moore389fb8002009-03-27 17:10:34 -04005253 selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005254}
5255
Paul Moore014ab192008-10-10 10:16:33 -04005256static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005257{
Paul Mooreaa862902008-10-10 10:16:29 -04005258 u16 family = sk->sk_family;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005259 struct sk_security_struct *sksec = sk->sk_security;
5260
Paul Mooreaa862902008-10-10 10:16:29 -04005261 /* handle mapped IPv4 packets arriving via IPv6 sockets */
5262 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
5263 family = PF_INET;
5264
5265 selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005266}
5267
Eric Paris2606fd12010-10-13 16:24:41 -04005268static int selinux_secmark_relabel_packet(u32 sid)
5269{
5270 const struct task_security_struct *__tsec;
5271 u32 tsid;
5272
5273 __tsec = current_security();
5274 tsid = __tsec->sid;
5275
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005276 return avc_has_perm(&selinux_state,
5277 tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO,
5278 NULL);
Eric Paris2606fd12010-10-13 16:24:41 -04005279}
5280
5281static void selinux_secmark_refcount_inc(void)
5282{
5283 atomic_inc(&selinux_secmark_refcount);
5284}
5285
5286static void selinux_secmark_refcount_dec(void)
5287{
5288 atomic_dec(&selinux_secmark_refcount);
5289}
5290
Adrian Bunk9a673e52006-08-15 00:03:53 -07005291static void selinux_req_classify_flow(const struct request_sock *req,
5292 struct flowi *fl)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005293{
David S. Miller1d28f422011-03-12 00:29:39 -05005294 fl->flowi_secid = req->secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005295}
5296
Paul Moore5dbbaf22013-01-14 07:12:19 +00005297static int selinux_tun_dev_alloc_security(void **security)
5298{
5299 struct tun_security_struct *tunsec;
5300
5301 tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL);
5302 if (!tunsec)
5303 return -ENOMEM;
5304 tunsec->sid = current_sid();
5305
5306 *security = tunsec;
5307 return 0;
5308}
5309
5310static void selinux_tun_dev_free_security(void *security)
5311{
5312 kfree(security);
5313}
5314
Paul Mooreed6d76e2009-08-28 18:12:49 -04005315static int selinux_tun_dev_create(void)
5316{
5317 u32 sid = current_sid();
5318
5319 /* we aren't taking into account the "sockcreate" SID since the socket
5320 * that is being created here is not a socket in the traditional sense,
5321 * instead it is a private sock, accessible only to the kernel, and
5322 * representing a wide range of network traffic spanning multiple
5323 * connections unlike traditional sockets - check the TUN driver to
5324 * get a better understanding of why this socket is special */
5325
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005326 return avc_has_perm(&selinux_state,
5327 sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005328 NULL);
5329}
5330
Paul Moore5dbbaf22013-01-14 07:12:19 +00005331static int selinux_tun_dev_attach_queue(void *security)
Paul Mooreed6d76e2009-08-28 18:12:49 -04005332{
Paul Moore5dbbaf22013-01-14 07:12:19 +00005333 struct tun_security_struct *tunsec = security;
5334
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005335 return avc_has_perm(&selinux_state,
5336 current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET,
Paul Moore5dbbaf22013-01-14 07:12:19 +00005337 TUN_SOCKET__ATTACH_QUEUE, NULL);
5338}
5339
5340static int selinux_tun_dev_attach(struct sock *sk, void *security)
5341{
5342 struct tun_security_struct *tunsec = security;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005343 struct sk_security_struct *sksec = sk->sk_security;
5344
5345 /* we don't currently perform any NetLabel based labeling here and it
5346 * isn't clear that we would want to do so anyway; while we could apply
5347 * labeling without the support of the TUN user the resulting labeled
5348 * traffic from the other end of the connection would almost certainly
5349 * cause confusion to the TUN user that had no idea network labeling
5350 * protocols were being used */
5351
Paul Moore5dbbaf22013-01-14 07:12:19 +00005352 sksec->sid = tunsec->sid;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005353 sksec->sclass = SECCLASS_TUN_SOCKET;
Paul Moore5dbbaf22013-01-14 07:12:19 +00005354
5355 return 0;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005356}
5357
Paul Moore5dbbaf22013-01-14 07:12:19 +00005358static int selinux_tun_dev_open(void *security)
Paul Mooreed6d76e2009-08-28 18:12:49 -04005359{
Paul Moore5dbbaf22013-01-14 07:12:19 +00005360 struct tun_security_struct *tunsec = security;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005361 u32 sid = current_sid();
5362 int err;
5363
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005364 err = avc_has_perm(&selinux_state,
5365 sid, tunsec->sid, SECCLASS_TUN_SOCKET,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005366 TUN_SOCKET__RELABELFROM, NULL);
5367 if (err)
5368 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005369 err = avc_has_perm(&selinux_state,
5370 sid, sid, SECCLASS_TUN_SOCKET,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005371 TUN_SOCKET__RELABELTO, NULL);
5372 if (err)
5373 return err;
Paul Moore5dbbaf22013-01-14 07:12:19 +00005374 tunsec->sid = sid;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005375
5376 return 0;
5377}
5378
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
5380{
5381 int err = 0;
5382 u32 perm;
5383 struct nlmsghdr *nlh;
Paul Moore253bfae2010-04-22 14:46:19 -04005384 struct sk_security_struct *sksec = sk->sk_security;
Eric Paris828dfe12008-04-17 13:17:49 -04005385
Hong zhi guo77954982013-03-27 06:49:35 +00005386 if (skb->len < NLMSG_HDRLEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387 err = -EINVAL;
5388 goto out;
5389 }
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -07005390 nlh = nlmsg_hdr(skb);
Eric Paris828dfe12008-04-17 13:17:49 -04005391
Paul Moore253bfae2010-04-22 14:46:19 -04005392 err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393 if (err) {
5394 if (err == -EINVAL) {
Vladis Dronov76319942015-12-24 11:09:41 -05005395 pr_warn_ratelimited("SELinux: unrecognized netlink"
5396 " message: protocol=%hu nlmsg_type=%hu sclass=%s"
5397 " pig=%d comm=%s\n",
Marek Milkoviccded3ff2015-06-04 16:22:16 -04005398 sk->sk_protocol, nlh->nlmsg_type,
Vladis Dronov76319942015-12-24 11:09:41 -05005399 secclass_map[sksec->sclass - 1].name,
5400 task_pid_nr(current), current->comm);
Paul Mooree5a5ca92018-03-01 17:38:30 -05005401 if (!enforcing_enabled(&selinux_state) ||
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005402 security_get_allow_unknown(&selinux_state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403 err = 0;
5404 }
5405
5406 /* Ignore */
5407 if (err == -ENOENT)
5408 err = 0;
5409 goto out;
5410 }
5411
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005412 err = sock_has_perm(sk, perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413out:
5414 return err;
5415}
5416
5417#ifdef CONFIG_NETFILTER
5418
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005419static unsigned int selinux_ip_forward(struct sk_buff *skb,
5420 const struct net_device *indev,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005421 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422{
Paul Mooredfaebe92008-10-10 10:16:31 -04005423 int err;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005424 char *addrp;
5425 u32 peer_sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04005426 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005427 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05005428 u8 secmark_active;
Paul Moore948bf852008-10-10 10:16:32 -04005429 u8 netlbl_active;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005430 u8 peerlbl_active;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005431
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005432 if (!selinux_policycap_netpeer())
Paul Mooreeffad8d2008-01-29 08:49:27 -05005433 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005434
Paul Mooreeffad8d2008-01-29 08:49:27 -05005435 secmark_active = selinux_secmark_enabled();
Paul Moore948bf852008-10-10 10:16:32 -04005436 netlbl_active = netlbl_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04005437 peerlbl_active = selinux_peerlbl_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05005438 if (!secmark_active && !peerlbl_active)
5439 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005440
Paul Moored8395c82008-10-10 10:16:30 -04005441 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
5442 return NF_DROP;
5443
Eric Paris50c205f2012-04-04 15:01:43 -04005444 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005445 ad.u.net = &net;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005446 ad.u.net->netif = indev->ifindex;
Eric Paris48c62af2012-04-02 13:15:44 -04005447 ad.u.net->family = family;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005448 if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
5449 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450
Paul Mooredfaebe92008-10-10 10:16:31 -04005451 if (peerlbl_active) {
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005452 err = selinux_inet_sys_rcv_skb(dev_net(indev), indev->ifindex,
5453 addrp, family, peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04005454 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04005455 selinux_netlbl_err(skb, family, err, 1);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005456 return NF_DROP;
Paul Mooredfaebe92008-10-10 10:16:31 -04005457 }
5458 }
Paul Mooreeffad8d2008-01-29 08:49:27 -05005459
5460 if (secmark_active)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005461 if (avc_has_perm(&selinux_state,
5462 peer_sid, skb->secmark,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005463 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
5464 return NF_DROP;
5465
Paul Moore948bf852008-10-10 10:16:32 -04005466 if (netlbl_active)
5467 /* we do this in the FORWARD path and not the POST_ROUTING
5468 * path because we want to make sure we apply the necessary
5469 * labeling before IPsec is applied so we can leverage AH
5470 * protection */
5471 if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
5472 return NF_DROP;
5473
Paul Mooreeffad8d2008-01-29 08:49:27 -05005474 return NF_ACCEPT;
5475}
5476
Eric W. Biederman06198b32015-09-18 14:33:06 -05005477static unsigned int selinux_ipv4_forward(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005478 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005479 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005480{
David S. Miller238e54c2015-04-03 20:32:56 -04005481 return selinux_ip_forward(skb, state->in, PF_INET);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005482}
5483
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005484#if IS_ENABLED(CONFIG_IPV6)
Eric W. Biederman06198b32015-09-18 14:33:06 -05005485static unsigned int selinux_ipv6_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_INET6);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005490}
5491#endif /* IPV6 */
5492
Paul Moore948bf852008-10-10 10:16:32 -04005493static unsigned int selinux_ip_output(struct sk_buff *skb,
5494 u16 family)
5495{
Paul Moore47180062013-12-04 16:10:45 -05005496 struct sock *sk;
Paul Moore948bf852008-10-10 10:16:32 -04005497 u32 sid;
5498
5499 if (!netlbl_enabled())
5500 return NF_ACCEPT;
5501
5502 /* we do this in the LOCAL_OUT path and not the POST_ROUTING path
5503 * because we want to make sure we apply the necessary labeling
5504 * before IPsec is applied so we can leverage AH protection */
Paul Moore47180062013-12-04 16:10:45 -05005505 sk = skb->sk;
5506 if (sk) {
5507 struct sk_security_struct *sksec;
5508
Eric Dumazete446f9d2015-10-08 05:01:55 -07005509 if (sk_listener(sk))
Paul Moore47180062013-12-04 16:10:45 -05005510 /* if the socket is the listening state then this
5511 * packet is a SYN-ACK packet which means it needs to
5512 * be labeled based on the connection/request_sock and
5513 * not the parent socket. unfortunately, we can't
5514 * lookup the request_sock yet as it isn't queued on
5515 * the parent socket until after the SYN-ACK is sent.
5516 * the "solution" is to simply pass the packet as-is
5517 * as any IP option based labeling should be copied
5518 * from the initial connection request (in the IP
5519 * layer). it is far from ideal, but until we get a
5520 * security label in the packet itself this is the
5521 * best we can do. */
5522 return NF_ACCEPT;
5523
5524 /* standard practice, label using the parent socket */
5525 sksec = sk->sk_security;
Paul Moore948bf852008-10-10 10:16:32 -04005526 sid = sksec->sid;
5527 } else
5528 sid = SECINITSID_KERNEL;
5529 if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
5530 return NF_DROP;
5531
5532 return NF_ACCEPT;
5533}
5534
Eric W. Biederman06198b32015-09-18 14:33:06 -05005535static unsigned int selinux_ipv4_output(void *priv,
Paul Moore948bf852008-10-10 10:16:32 -04005536 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005537 const struct nf_hook_state *state)
Paul Moore948bf852008-10-10 10:16:32 -04005538{
5539 return selinux_ip_output(skb, PF_INET);
5540}
5541
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005542#if IS_ENABLED(CONFIG_IPV6)
Huw Davies2917f572016-06-27 15:06:15 -04005543static unsigned int selinux_ipv6_output(void *priv,
5544 struct sk_buff *skb,
5545 const struct nf_hook_state *state)
5546{
5547 return selinux_ip_output(skb, PF_INET6);
5548}
5549#endif /* IPV6 */
5550
Paul Mooreeffad8d2008-01-29 08:49:27 -05005551static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
5552 int ifindex,
Paul Moored8395c82008-10-10 10:16:30 -04005553 u16 family)
James Morris4e5ab4c2006-06-09 00:33:33 -07005554{
Eric Dumazet54abc682015-11-08 10:54:07 -08005555 struct sock *sk = skb_to_full_sk(skb);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005556 struct sk_security_struct *sksec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005557 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005558 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04005559 char *addrp;
5560 u8 proto;
James Morris4e5ab4c2006-06-09 00:33:33 -07005561
Paul Mooreeffad8d2008-01-29 08:49:27 -05005562 if (sk == NULL)
5563 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005564 sksec = sk->sk_security;
James Morris4e5ab4c2006-06-09 00:33:33 -07005565
Eric Paris50c205f2012-04-04 15:01:43 -04005566 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005567 ad.u.net = &net;
5568 ad.u.net->netif = ifindex;
5569 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04005570 if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
5571 return NF_DROP;
5572
Paul Moore58bfbb52009-03-27 17:10:41 -04005573 if (selinux_secmark_enabled())
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005574 if (avc_has_perm(&selinux_state,
5575 sksec->sid, skb->secmark,
Paul Moored8395c82008-10-10 10:16:30 -04005576 SECCLASS_PACKET, PACKET__SEND, &ad))
Eric Paris2fe66ec2010-11-23 06:28:08 +00005577 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07005578
Steffen Klassertb9679a72011-02-23 12:55:21 +01005579 if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
5580 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07005581
Paul Mooreeffad8d2008-01-29 08:49:27 -05005582 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583}
5584
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005585static unsigned int selinux_ip_postroute(struct sk_buff *skb,
5586 const struct net_device *outdev,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005587 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588{
Paul Mooreeffad8d2008-01-29 08:49:27 -05005589 u32 secmark_perm;
5590 u32 peer_sid;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005591 int ifindex = outdev->ifindex;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005592 struct sock *sk;
Thomas Liu2bf49692009-07-14 12:14:09 -04005593 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005594 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05005595 char *addrp;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005596 u8 secmark_active;
5597 u8 peerlbl_active;
5598
Paul Mooreeffad8d2008-01-29 08:49:27 -05005599 /* If any sort of compatibility mode is enabled then handoff processing
5600 * to the selinux_ip_postroute_compat() function to deal with the
5601 * special handling. We do this in an attempt to keep this function
5602 * as fast and as clean as possible. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005603 if (!selinux_policycap_netpeer())
Paul Moored8395c82008-10-10 10:16:30 -04005604 return selinux_ip_postroute_compat(skb, ifindex, family);
Paul Moorec0828e52013-12-10 14:58:01 -05005605
Paul Mooreeffad8d2008-01-29 08:49:27 -05005606 secmark_active = selinux_secmark_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04005607 peerlbl_active = selinux_peerlbl_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05005608 if (!secmark_active && !peerlbl_active)
5609 return NF_ACCEPT;
5610
Eric Dumazet54abc682015-11-08 10:54:07 -08005611 sk = skb_to_full_sk(skb);
Paul Moorec0828e52013-12-10 14:58:01 -05005612
Paul Mooreeffad8d2008-01-29 08:49:27 -05005613#ifdef CONFIG_XFRM
5614 /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
5615 * packet transformation so allow the packet to pass without any checks
5616 * since we'll have another chance to perform access control checks
5617 * when the packet is on it's final way out.
5618 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
Paul Moorec0828e52013-12-10 14:58:01 -05005619 * is NULL, in this case go ahead and apply access control.
5620 * NOTE: if this is a local socket (skb->sk != NULL) that is in the
5621 * TCP listening state we cannot wait until the XFRM processing
5622 * is done as we will miss out on the SA label if we do;
5623 * unfortunately, this means more work, but it is only once per
5624 * connection. */
5625 if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL &&
Eric Dumazete446f9d2015-10-08 05:01:55 -07005626 !(sk && sk_listener(sk)))
Paul Mooreeffad8d2008-01-29 08:49:27 -05005627 return NF_ACCEPT;
5628#endif
Paul Mooreeffad8d2008-01-29 08:49:27 -05005629
Paul Moored8395c82008-10-10 10:16:30 -04005630 if (sk == NULL) {
Paul Moore446b8022013-12-04 16:10:51 -05005631 /* Without an associated socket the packet is either coming
5632 * from the kernel or it is being forwarded; check the packet
5633 * to determine which and if the packet is being forwarded
5634 * query the packet directly to determine the security label. */
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005635 if (skb->skb_iif) {
5636 secmark_perm = PACKET__FORWARD_OUT;
Paul Moored8395c82008-10-10 10:16:30 -04005637 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005638 return NF_DROP;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005639 } else {
5640 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04005641 peer_sid = SECINITSID_KERNEL;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005642 }
Eric Dumazete446f9d2015-10-08 05:01:55 -07005643 } else if (sk_listener(sk)) {
Paul Moore446b8022013-12-04 16:10:51 -05005644 /* Locally generated packet but the associated socket is in the
5645 * listening state which means this is a SYN-ACK packet. In
5646 * this particular case the correct security label is assigned
5647 * to the connection/request_sock but unfortunately we can't
5648 * query the request_sock as it isn't queued on the parent
5649 * socket until after the SYN-ACK packet is sent; the only
5650 * viable choice is to regenerate the label like we do in
5651 * selinux_inet_conn_request(). See also selinux_ip_output()
5652 * for similar problems. */
5653 u32 skb_sid;
Eric Dumazete446f9d2015-10-08 05:01:55 -07005654 struct sk_security_struct *sksec;
5655
Eric Dumazete446f9d2015-10-08 05:01:55 -07005656 sksec = sk->sk_security;
Paul Moore446b8022013-12-04 16:10:51 -05005657 if (selinux_skb_peerlbl_sid(skb, family, &skb_sid))
5658 return NF_DROP;
Paul Moorec0828e52013-12-10 14:58:01 -05005659 /* At this point, if the returned skb peerlbl is SECSID_NULL
5660 * and the packet has been through at least one XFRM
5661 * transformation then we must be dealing with the "final"
5662 * form of labeled IPsec packet; since we've already applied
5663 * all of our access controls on this packet we can safely
5664 * pass the packet. */
5665 if (skb_sid == SECSID_NULL) {
5666 switch (family) {
5667 case PF_INET:
5668 if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
5669 return NF_ACCEPT;
5670 break;
5671 case PF_INET6:
5672 if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED)
5673 return NF_ACCEPT;
Paul Moorea7a91a12014-09-03 10:51:59 -04005674 break;
Paul Moorec0828e52013-12-10 14:58:01 -05005675 default:
5676 return NF_DROP_ERR(-ECONNREFUSED);
5677 }
5678 }
Paul Moore446b8022013-12-04 16:10:51 -05005679 if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid))
5680 return NF_DROP;
5681 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04005682 } else {
Paul Moore446b8022013-12-04 16:10:51 -05005683 /* Locally generated packet, fetch the security label from the
5684 * associated socket. */
Paul Mooreeffad8d2008-01-29 08:49:27 -05005685 struct sk_security_struct *sksec = sk->sk_security;
5686 peer_sid = sksec->sid;
5687 secmark_perm = PACKET__SEND;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005688 }
5689
Eric Paris50c205f2012-04-04 15:01:43 -04005690 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005691 ad.u.net = &net;
5692 ad.u.net->netif = ifindex;
5693 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04005694 if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
Eric Paris04f6d702010-11-23 06:28:02 +00005695 return NF_DROP;
Paul Moored8395c82008-10-10 10:16:30 -04005696
Paul Mooreeffad8d2008-01-29 08:49:27 -05005697 if (secmark_active)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005698 if (avc_has_perm(&selinux_state,
5699 peer_sid, skb->secmark,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005700 SECCLASS_PACKET, secmark_perm, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005701 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005702
5703 if (peerlbl_active) {
5704 u32 if_sid;
5705 u32 node_sid;
5706
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005707 if (sel_netif_sid(dev_net(outdev), ifindex, &if_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005708 return NF_DROP;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005709 if (avc_has_perm(&selinux_state,
5710 peer_sid, if_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005711 SECCLASS_NETIF, NETIF__EGRESS, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005712 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005713
5714 if (sel_netnode_sid(addrp, family, &node_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005715 return NF_DROP;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005716 if (avc_has_perm(&selinux_state,
5717 peer_sid, node_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005718 SECCLASS_NODE, NODE__SENDTO, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005719 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005720 }
5721
5722 return NF_ACCEPT;
5723}
5724
Eric W. Biederman06198b32015-09-18 14:33:06 -05005725static unsigned int selinux_ipv4_postroute(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005726 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005727 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005728{
David S. Miller238e54c2015-04-03 20:32:56 -04005729 return selinux_ip_postroute(skb, state->out, PF_INET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730}
5731
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005732#if IS_ENABLED(CONFIG_IPV6)
Eric W. Biederman06198b32015-09-18 14:33:06 -05005733static unsigned int selinux_ipv6_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)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736{
David S. Miller238e54c2015-04-03 20:32:56 -04005737 return selinux_ip_postroute(skb, state->out, PF_INET6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739#endif /* IPV6 */
5740
5741#endif /* CONFIG_NETFILTER */
5742
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
5744{
Stephen Smalley941fc5b2009-10-01 14:48:23 -04005745 return selinux_nlmsg_perm(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746}
5747
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005748static int ipc_alloc_security(struct kern_ipc_perm *perm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749 u16 sclass)
5750{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751 struct ipc_security_struct *isec;
5752
James Morris89d155e2005-10-30 14:59:21 -08005753 isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754 if (!isec)
5755 return -ENOMEM;
5756
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757 isec->sclass = sclass;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005758 isec->sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759 perm->security = isec;
5760
5761 return 0;
5762}
5763
5764static void ipc_free_security(struct kern_ipc_perm *perm)
5765{
5766 struct ipc_security_struct *isec = perm->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767 perm->security = NULL;
5768 kfree(isec);
5769}
5770
5771static int msg_msg_alloc_security(struct msg_msg *msg)
5772{
5773 struct msg_security_struct *msec;
5774
James Morris89d155e2005-10-30 14:59:21 -08005775 msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776 if (!msec)
5777 return -ENOMEM;
5778
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779 msec->sid = SECINITSID_UNLABELED;
5780 msg->security = msec;
5781
5782 return 0;
5783}
5784
5785static void msg_msg_free_security(struct msg_msg *msg)
5786{
5787 struct msg_security_struct *msec = msg->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788
5789 msg->security = NULL;
5790 kfree(msec);
5791}
5792
5793static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
Stephen Smalley6af963f2005-05-01 08:58:39 -07005794 u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005795{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005797 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005798 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800 isec = ipc_perms->security;
5801
Eric Paris50c205f2012-04-04 15:01:43 -04005802 ad.type = LSM_AUDIT_DATA_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803 ad.u.ipc_id = ipc_perms->key;
5804
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005805 return avc_has_perm(&selinux_state,
5806 sid, isec->sid, isec->sclass, perms, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005807}
5808
5809static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
5810{
5811 return msg_msg_alloc_security(msg);
5812}
5813
5814static void selinux_msg_msg_free_security(struct msg_msg *msg)
5815{
5816 msg_msg_free_security(msg);
5817}
5818
5819/* message queue security operations */
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005820static int selinux_msg_queue_alloc_security(struct kern_ipc_perm *msq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005823 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005824 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825 int rc;
5826
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005827 rc = ipc_alloc_security(msq, SECCLASS_MSGQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828 if (rc)
5829 return rc;
5830
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005831 isec = msq->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832
Eric Paris50c205f2012-04-04 15:01:43 -04005833 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005834 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005836 rc = avc_has_perm(&selinux_state,
5837 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838 MSGQ__CREATE, &ad);
5839 if (rc) {
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005840 ipc_free_security(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841 return rc;
5842 }
5843 return 0;
5844}
5845
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005846static void selinux_msg_queue_free_security(struct kern_ipc_perm *msq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005847{
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005848 ipc_free_security(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849}
5850
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005851static int selinux_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005852{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005854 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005855 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005857 isec = msq->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005858
Eric Paris50c205f2012-04-04 15:01:43 -04005859 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005860 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005861
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005862 return avc_has_perm(&selinux_state,
5863 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864 MSGQ__ASSOCIATE, &ad);
5865}
5866
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005867static int selinux_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005868{
5869 int err;
5870 int perms;
5871
Eric Paris828dfe12008-04-17 13:17:49 -04005872 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873 case IPC_INFO:
5874 case MSG_INFO:
5875 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005876 return avc_has_perm(&selinux_state,
5877 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005878 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005879 case IPC_STAT:
5880 case MSG_STAT:
Davidlohr Bueso23c8cec2018-04-10 16:35:30 -07005881 case MSG_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882 perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
5883 break;
5884 case IPC_SET:
5885 perms = MSGQ__SETATTR;
5886 break;
5887 case IPC_RMID:
5888 perms = MSGQ__DESTROY;
5889 break;
5890 default:
5891 return 0;
5892 }
5893
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005894 err = ipc_has_perm(msq, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895 return err;
5896}
5897
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005898static int selinux_msg_queue_msgsnd(struct kern_ipc_perm *msq, struct msg_msg *msg, int msqflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900 struct ipc_security_struct *isec;
5901 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005902 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005903 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904 int rc;
5905
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005906 isec = msq->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005907 msec = msg->security;
5908
5909 /*
5910 * First time through, need to assign label to the message
5911 */
5912 if (msec->sid == SECINITSID_UNLABELED) {
5913 /*
5914 * Compute new sid based on current process and
5915 * message queue this message will be stored in
5916 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005917 rc = security_transition_sid(&selinux_state, sid, isec->sid,
5918 SECCLASS_MSG, NULL, &msec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919 if (rc)
5920 return rc;
5921 }
5922
Eric Paris50c205f2012-04-04 15:01:43 -04005923 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005924 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005925
5926 /* Can this process write to the queue? */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005927 rc = avc_has_perm(&selinux_state,
5928 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005929 MSGQ__WRITE, &ad);
5930 if (!rc)
5931 /* Can this process send the message */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005932 rc = avc_has_perm(&selinux_state,
5933 sid, msec->sid, SECCLASS_MSG,
David Howells275bb412008-11-14 10:39:19 +11005934 MSG__SEND, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005935 if (!rc)
5936 /* Can the message be put in the queue? */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005937 rc = avc_has_perm(&selinux_state,
5938 msec->sid, isec->sid, SECCLASS_MSGQ,
David Howells275bb412008-11-14 10:39:19 +11005939 MSGQ__ENQUEUE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940
5941 return rc;
5942}
5943
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005944static int selinux_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005945 struct task_struct *target,
5946 long type, int mode)
5947{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948 struct ipc_security_struct *isec;
5949 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005950 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005951 u32 sid = task_sid(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952 int rc;
5953
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005954 isec = msq->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955 msec = msg->security;
5956
Eric Paris50c205f2012-04-04 15:01:43 -04005957 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005958 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005959
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005960 rc = avc_has_perm(&selinux_state,
5961 sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005962 SECCLASS_MSGQ, MSGQ__READ, &ad);
5963 if (!rc)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005964 rc = avc_has_perm(&selinux_state,
5965 sid, msec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966 SECCLASS_MSG, MSG__RECEIVE, &ad);
5967 return rc;
5968}
5969
5970/* Shared Memory security operations */
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005971static int selinux_shm_alloc_security(struct kern_ipc_perm *shp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005972{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005973 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005974 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005975 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005976 int rc;
5977
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005978 rc = ipc_alloc_security(shp, SECCLASS_SHM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979 if (rc)
5980 return rc;
5981
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005982 isec = shp->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005983
Eric Paris50c205f2012-04-04 15:01:43 -04005984 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005985 ad.u.ipc_id = shp->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005987 rc = avc_has_perm(&selinux_state,
5988 sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005989 SHM__CREATE, &ad);
5990 if (rc) {
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005991 ipc_free_security(shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005992 return rc;
5993 }
5994 return 0;
5995}
5996
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005997static void selinux_shm_free_security(struct kern_ipc_perm *shp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998{
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005999 ipc_free_security(shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006000}
6001
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006002static int selinux_shm_associate(struct kern_ipc_perm *shp, int shmflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006003{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006004 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006005 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006006 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006007
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006008 isec = shp->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006009
Eric Paris50c205f2012-04-04 15:01:43 -04006010 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006011 ad.u.ipc_id = shp->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006013 return avc_has_perm(&selinux_state,
6014 sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015 SHM__ASSOCIATE, &ad);
6016}
6017
6018/* Note, at this point, shp is locked down */
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006019static int selinux_shm_shmctl(struct kern_ipc_perm *shp, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020{
6021 int perms;
6022 int err;
6023
Eric Paris828dfe12008-04-17 13:17:49 -04006024 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006025 case IPC_INFO:
6026 case SHM_INFO:
6027 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006028 return avc_has_perm(&selinux_state,
6029 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006030 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006031 case IPC_STAT:
6032 case SHM_STAT:
Davidlohr Buesoc21a6972018-04-10 16:35:23 -07006033 case SHM_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034 perms = SHM__GETATTR | SHM__ASSOCIATE;
6035 break;
6036 case IPC_SET:
6037 perms = SHM__SETATTR;
6038 break;
6039 case SHM_LOCK:
6040 case SHM_UNLOCK:
6041 perms = SHM__LOCK;
6042 break;
6043 case IPC_RMID:
6044 perms = SHM__DESTROY;
6045 break;
6046 default:
6047 return 0;
6048 }
6049
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006050 err = ipc_has_perm(shp, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051 return err;
6052}
6053
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006054static int selinux_shm_shmat(struct kern_ipc_perm *shp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006055 char __user *shmaddr, int shmflg)
6056{
6057 u32 perms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058
6059 if (shmflg & SHM_RDONLY)
6060 perms = SHM__READ;
6061 else
6062 perms = SHM__READ | SHM__WRITE;
6063
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006064 return ipc_has_perm(shp, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006065}
6066
6067/* Semaphore security operations */
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006068static int selinux_sem_alloc_security(struct kern_ipc_perm *sma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006071 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006072 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006073 int rc;
6074
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006075 rc = ipc_alloc_security(sma, SECCLASS_SEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076 if (rc)
6077 return rc;
6078
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006079 isec = sma->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080
Eric Paris50c205f2012-04-04 15:01:43 -04006081 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006082 ad.u.ipc_id = sma->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006083
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006084 rc = avc_has_perm(&selinux_state,
6085 sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086 SEM__CREATE, &ad);
6087 if (rc) {
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006088 ipc_free_security(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006089 return rc;
6090 }
6091 return 0;
6092}
6093
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006094static void selinux_sem_free_security(struct kern_ipc_perm *sma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095{
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006096 ipc_free_security(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097}
6098
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006099static int selinux_sem_associate(struct kern_ipc_perm *sma, int semflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006100{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006102 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006103 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006105 isec = sma->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006106
Eric Paris50c205f2012-04-04 15:01:43 -04006107 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006108 ad.u.ipc_id = sma->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006109
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006110 return avc_has_perm(&selinux_state,
6111 sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006112 SEM__ASSOCIATE, &ad);
6113}
6114
6115/* Note, at this point, sma is locked down */
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006116static int selinux_sem_semctl(struct kern_ipc_perm *sma, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006117{
6118 int err;
6119 u32 perms;
6120
Eric Paris828dfe12008-04-17 13:17:49 -04006121 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006122 case IPC_INFO:
6123 case SEM_INFO:
6124 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006125 return avc_has_perm(&selinux_state,
6126 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006127 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006128 case GETPID:
6129 case GETNCNT:
6130 case GETZCNT:
6131 perms = SEM__GETATTR;
6132 break;
6133 case GETVAL:
6134 case GETALL:
6135 perms = SEM__READ;
6136 break;
6137 case SETVAL:
6138 case SETALL:
6139 perms = SEM__WRITE;
6140 break;
6141 case IPC_RMID:
6142 perms = SEM__DESTROY;
6143 break;
6144 case IPC_SET:
6145 perms = SEM__SETATTR;
6146 break;
6147 case IPC_STAT:
6148 case SEM_STAT:
Davidlohr Buesoa280d6d2018-04-10 16:35:26 -07006149 case SEM_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006150 perms = SEM__GETATTR | SEM__ASSOCIATE;
6151 break;
6152 default:
6153 return 0;
6154 }
6155
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006156 err = ipc_has_perm(sma, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006157 return err;
6158}
6159
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006160static int selinux_sem_semop(struct kern_ipc_perm *sma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006161 struct sembuf *sops, unsigned nsops, int alter)
6162{
6163 u32 perms;
6164
6165 if (alter)
6166 perms = SEM__READ | SEM__WRITE;
6167 else
6168 perms = SEM__READ;
6169
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006170 return ipc_has_perm(sma, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006171}
6172
6173static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
6174{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006175 u32 av = 0;
6176
Linus Torvalds1da177e2005-04-16 15:20:36 -07006177 av = 0;
6178 if (flag & S_IRUGO)
6179 av |= IPC__UNIX_READ;
6180 if (flag & S_IWUGO)
6181 av |= IPC__UNIX_WRITE;
6182
6183 if (av == 0)
6184 return 0;
6185
Stephen Smalley6af963f2005-05-01 08:58:39 -07006186 return ipc_has_perm(ipcp, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006187}
6188
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02006189static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
6190{
6191 struct ipc_security_struct *isec = ipcp->security;
6192 *secid = isec->sid;
6193}
6194
Eric Paris828dfe12008-04-17 13:17:49 -04006195static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006196{
6197 if (inode)
6198 inode_doinit_with_dentry(inode, dentry);
6199}
6200
6201static int selinux_getprocattr(struct task_struct *p,
Al Viro04ff9702007-03-12 16:17:58 +00006202 char *name, char **value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006203{
David Howells275bb412008-11-14 10:39:19 +11006204 const struct task_security_struct *__tsec;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00006205 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006206 int error;
Al Viro04ff9702007-03-12 16:17:58 +00006207 unsigned len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006208
David Howells275bb412008-11-14 10:39:19 +11006209 rcu_read_lock();
6210 __tsec = __task_cred(p)->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006212 if (current != p) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006213 error = avc_has_perm(&selinux_state,
6214 current_sid(), __tsec->sid,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006215 SECCLASS_PROCESS, PROCESS__GETATTR, NULL);
6216 if (error)
6217 goto bad;
6218 }
6219
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220 if (!strcmp(name, "current"))
David Howells275bb412008-11-14 10:39:19 +11006221 sid = __tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006222 else if (!strcmp(name, "prev"))
David Howells275bb412008-11-14 10:39:19 +11006223 sid = __tsec->osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006224 else if (!strcmp(name, "exec"))
David Howells275bb412008-11-14 10:39:19 +11006225 sid = __tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006226 else if (!strcmp(name, "fscreate"))
David Howells275bb412008-11-14 10:39:19 +11006227 sid = __tsec->create_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006228 else if (!strcmp(name, "keycreate"))
David Howells275bb412008-11-14 10:39:19 +11006229 sid = __tsec->keycreate_sid;
Eric Paris42c3e032006-06-26 00:26:03 -07006230 else if (!strcmp(name, "sockcreate"))
David Howells275bb412008-11-14 10:39:19 +11006231 sid = __tsec->sockcreate_sid;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006232 else {
6233 error = -EINVAL;
6234 goto bad;
6235 }
David Howells275bb412008-11-14 10:39:19 +11006236 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006237
6238 if (!sid)
6239 return 0;
6240
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006241 error = security_sid_to_context(&selinux_state, sid, value, &len);
Al Viro04ff9702007-03-12 16:17:58 +00006242 if (error)
6243 return error;
6244 return len;
David Howells275bb412008-11-14 10:39:19 +11006245
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006246bad:
David Howells275bb412008-11-14 10:39:19 +11006247 rcu_read_unlock();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006248 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249}
6250
Stephen Smalleyb21507e2017-01-09 10:07:31 -05006251static int selinux_setprocattr(const char *name, void *value, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006252{
6253 struct task_security_struct *tsec;
David Howellsd84f4f92008-11-14 10:39:23 +11006254 struct cred *new;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006255 u32 mysid = current_sid(), sid = 0, ptsid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006256 int error;
6257 char *str = value;
6258
Linus Torvalds1da177e2005-04-16 15:20:36 -07006259 /*
6260 * Basic control over ability to set these attributes at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006261 */
6262 if (!strcmp(name, "exec"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006263 error = avc_has_perm(&selinux_state,
6264 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006265 PROCESS__SETEXEC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006266 else if (!strcmp(name, "fscreate"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006267 error = avc_has_perm(&selinux_state,
6268 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006269 PROCESS__SETFSCREATE, NULL);
Michael LeMay4eb582c2006-06-26 00:24:57 -07006270 else if (!strcmp(name, "keycreate"))
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__SETKEYCREATE, NULL);
Eric Paris42c3e032006-06-26 00:26:03 -07006274 else if (!strcmp(name, "sockcreate"))
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__SETSOCKCREATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006278 else if (!strcmp(name, "current"))
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__SETCURRENT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006282 else
6283 error = -EINVAL;
6284 if (error)
6285 return error;
6286
6287 /* Obtain a SID for the context, if one was specified. */
Stephen Smalleya050a572017-01-31 11:54:04 -05006288 if (size && str[0] && str[0] != '\n') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006289 if (str[size-1] == '\n') {
6290 str[size-1] = 0;
6291 size--;
6292 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006293 error = security_context_to_sid(&selinux_state, value, size,
6294 &sid, GFP_KERNEL);
Stephen Smalley12b29f32008-05-07 13:03:20 -04006295 if (error == -EINVAL && !strcmp(name, "fscreate")) {
Stephen Smalleydb590002017-04-20 11:31:30 -04006296 if (!has_cap_mac_admin(true)) {
Eric Parisd6ea83e2012-04-04 13:45:49 -04006297 struct audit_buffer *ab;
6298 size_t audit_size;
6299
6300 /* We strip a nul only if it is at the end, otherwise the
6301 * context contains a nul and we should audit that */
6302 if (str[size - 1] == '\0')
6303 audit_size = size - 1;
6304 else
6305 audit_size = size;
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04006306 ab = audit_log_start(audit_context(),
6307 GFP_ATOMIC,
6308 AUDIT_SELINUX_ERR);
Eric Parisd6ea83e2012-04-04 13:45:49 -04006309 audit_log_format(ab, "op=fscreate invalid_context=");
6310 audit_log_n_untrustedstring(ab, value, audit_size);
6311 audit_log_end(ab);
6312
Stephen Smalley12b29f32008-05-07 13:03:20 -04006313 return error;
Eric Parisd6ea83e2012-04-04 13:45:49 -04006314 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006315 error = security_context_to_sid_force(
6316 &selinux_state,
6317 value, size, &sid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04006318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006319 if (error)
6320 return error;
6321 }
6322
David Howellsd84f4f92008-11-14 10:39:23 +11006323 new = prepare_creds();
6324 if (!new)
6325 return -ENOMEM;
6326
Linus Torvalds1da177e2005-04-16 15:20:36 -07006327 /* Permission checking based on the specified context is
6328 performed during the actual operation (execve,
6329 open/mkdir/...), when we know the full context of the
David Howellsd84f4f92008-11-14 10:39:23 +11006330 operation. See selinux_bprm_set_creds for the execve
Linus Torvalds1da177e2005-04-16 15:20:36 -07006331 checks and may_create for the file creation checks. The
6332 operation will then fail if the context is not permitted. */
David Howellsd84f4f92008-11-14 10:39:23 +11006333 tsec = new->security;
6334 if (!strcmp(name, "exec")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006335 tsec->exec_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006336 } else if (!strcmp(name, "fscreate")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006337 tsec->create_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006338 } else if (!strcmp(name, "keycreate")) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006339 error = avc_has_perm(&selinux_state,
6340 mysid, sid, SECCLASS_KEY, KEY__CREATE,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006341 NULL);
Michael LeMay4eb582c2006-06-26 00:24:57 -07006342 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11006343 goto abort_change;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006344 tsec->keycreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006345 } else if (!strcmp(name, "sockcreate")) {
Eric Paris42c3e032006-06-26 00:26:03 -07006346 tsec->sockcreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006347 } else if (!strcmp(name, "current")) {
6348 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006349 if (sid == 0)
David Howellsd84f4f92008-11-14 10:39:23 +11006350 goto abort_change;
KaiGai Koheid9250de2008-08-28 16:35:57 +09006351
David Howellsd84f4f92008-11-14 10:39:23 +11006352 /* Only allow single threaded processes to change context */
6353 error = -EPERM;
Oleg Nesterov5bb459b2009-07-10 03:48:23 +02006354 if (!current_is_single_threaded()) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006355 error = security_bounded_transition(&selinux_state,
6356 tsec->sid, sid);
David Howellsd84f4f92008-11-14 10:39:23 +11006357 if (error)
6358 goto abort_change;
Eric Paris828dfe12008-04-17 13:17:49 -04006359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006360
6361 /* Check permissions for the transition. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006362 error = avc_has_perm(&selinux_state,
6363 tsec->sid, sid, SECCLASS_PROCESS,
Eric Paris828dfe12008-04-17 13:17:49 -04006364 PROCESS__DYNTRANSITION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006365 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11006366 goto abort_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006367
6368 /* Check for ptracing, and update the task SID if ok.
6369 Otherwise, leave SID unchanged and fail. */
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006370 ptsid = ptrace_parent_sid();
Paul Moore0c6181c2016-03-30 21:41:21 -04006371 if (ptsid != 0) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006372 error = avc_has_perm(&selinux_state,
6373 ptsid, sid, SECCLASS_PROCESS,
David Howellsd84f4f92008-11-14 10:39:23 +11006374 PROCESS__PTRACE, NULL);
6375 if (error)
6376 goto abort_change;
6377 }
6378
6379 tsec->sid = sid;
6380 } else {
6381 error = -EINVAL;
6382 goto abort_change;
6383 }
6384
6385 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006386 return size;
David Howellsd84f4f92008-11-14 10:39:23 +11006387
6388abort_change:
6389 abort_creds(new);
6390 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006391}
6392
David Quigley746df9b2013-05-22 12:50:35 -04006393static int selinux_ismaclabel(const char *name)
6394{
6395 return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0);
6396}
6397
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006398static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
6399{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006400 return security_sid_to_context(&selinux_state, secid,
6401 secdata, seclen);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006402}
6403
David Howells7bf570d2008-04-29 20:52:51 +01006404static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
David Howells63cb3442008-01-15 23:47:35 +00006405{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006406 return security_context_to_sid(&selinux_state, secdata, seclen,
6407 secid, GFP_KERNEL);
David Howells63cb3442008-01-15 23:47:35 +00006408}
6409
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006410static void selinux_release_secctx(char *secdata, u32 seclen)
6411{
Paul Moore088999e2007-08-01 11:12:58 -04006412 kfree(secdata);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006413}
6414
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006415static void selinux_inode_invalidate_secctx(struct inode *inode)
6416{
6417 struct inode_security_struct *isec = inode->i_security;
6418
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01006419 spin_lock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006420 isec->initialized = LABEL_INVALID;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01006421 spin_unlock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006422}
6423
David P. Quigley1ee65e32009-09-03 14:25:57 -04006424/*
6425 * called with inode->i_mutex locked
6426 */
6427static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
6428{
6429 return selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, ctx, ctxlen, 0);
6430}
6431
6432/*
6433 * called with inode->i_mutex locked
6434 */
6435static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
6436{
6437 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0);
6438}
6439
6440static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
6441{
6442 int len = 0;
6443 len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX,
6444 ctx, true);
6445 if (len < 0)
6446 return len;
6447 *ctxlen = len;
6448 return 0;
6449}
Michael LeMayd7200242006-06-22 14:47:17 -07006450#ifdef CONFIG_KEYS
6451
David Howellsd84f4f92008-11-14 10:39:23 +11006452static int selinux_key_alloc(struct key *k, const struct cred *cred,
David Howells7e047ef2006-06-26 00:24:50 -07006453 unsigned long flags)
Michael LeMayd7200242006-06-22 14:47:17 -07006454{
David Howellsd84f4f92008-11-14 10:39:23 +11006455 const struct task_security_struct *tsec;
Michael LeMayd7200242006-06-22 14:47:17 -07006456 struct key_security_struct *ksec;
6457
6458 ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
6459 if (!ksec)
6460 return -ENOMEM;
6461
David Howellsd84f4f92008-11-14 10:39:23 +11006462 tsec = cred->security;
6463 if (tsec->keycreate_sid)
6464 ksec->sid = tsec->keycreate_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006465 else
David Howellsd84f4f92008-11-14 10:39:23 +11006466 ksec->sid = tsec->sid;
Michael LeMayd7200242006-06-22 14:47:17 -07006467
David Howells275bb412008-11-14 10:39:19 +11006468 k->security = ksec;
Michael LeMayd7200242006-06-22 14:47:17 -07006469 return 0;
6470}
6471
6472static void selinux_key_free(struct key *k)
6473{
6474 struct key_security_struct *ksec = k->security;
6475
6476 k->security = NULL;
6477 kfree(ksec);
6478}
6479
6480static int selinux_key_permission(key_ref_t key_ref,
David Howellsd84f4f92008-11-14 10:39:23 +11006481 const struct cred *cred,
David Howellsf5895942014-03-14 17:44:49 +00006482 unsigned perm)
Michael LeMayd7200242006-06-22 14:47:17 -07006483{
6484 struct key *key;
Michael LeMayd7200242006-06-22 14:47:17 -07006485 struct key_security_struct *ksec;
David Howells275bb412008-11-14 10:39:19 +11006486 u32 sid;
Michael LeMayd7200242006-06-22 14:47:17 -07006487
6488 /* if no specific permissions are requested, we skip the
6489 permission check. No serious, additional covert channels
6490 appear to be created. */
6491 if (perm == 0)
6492 return 0;
6493
David Howellsd84f4f92008-11-14 10:39:23 +11006494 sid = cred_sid(cred);
David Howells275bb412008-11-14 10:39:19 +11006495
6496 key = key_ref_to_ptr(key_ref);
6497 ksec = key->security;
6498
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006499 return avc_has_perm(&selinux_state,
6500 sid, ksec->sid, SECCLASS_KEY, perm, NULL);
Michael LeMayd7200242006-06-22 14:47:17 -07006501}
6502
David Howells70a5bb72008-04-29 01:01:26 -07006503static int selinux_key_getsecurity(struct key *key, char **_buffer)
6504{
6505 struct key_security_struct *ksec = key->security;
6506 char *context = NULL;
6507 unsigned len;
6508 int rc;
6509
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006510 rc = security_sid_to_context(&selinux_state, ksec->sid,
6511 &context, &len);
David Howells70a5bb72008-04-29 01:01:26 -07006512 if (!rc)
6513 rc = len;
6514 *_buffer = context;
6515 return rc;
6516}
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006517#endif
David Howells70a5bb72008-04-29 01:01:26 -07006518
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006519#ifdef CONFIG_SECURITY_INFINIBAND
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006520static int selinux_ib_pkey_access(void *ib_sec, u64 subnet_prefix, u16 pkey_val)
6521{
6522 struct common_audit_data ad;
6523 int err;
6524 u32 sid = 0;
6525 struct ib_security_struct *sec = ib_sec;
6526 struct lsm_ibpkey_audit ibpkey;
6527
Daniel Jurgens409dcf32017-05-19 15:48:59 +03006528 err = sel_ib_pkey_sid(subnet_prefix, pkey_val, &sid);
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006529 if (err)
6530 return err;
6531
6532 ad.type = LSM_AUDIT_DATA_IBPKEY;
6533 ibpkey.subnet_prefix = subnet_prefix;
6534 ibpkey.pkey = pkey_val;
6535 ad.u.ibpkey = &ibpkey;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006536 return avc_has_perm(&selinux_state,
6537 sec->sid, sid,
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006538 SECCLASS_INFINIBAND_PKEY,
6539 INFINIBAND_PKEY__ACCESS, &ad);
6540}
6541
Daniel Jurgensab861df2017-05-19 15:48:58 +03006542static int selinux_ib_endport_manage_subnet(void *ib_sec, const char *dev_name,
6543 u8 port_num)
6544{
6545 struct common_audit_data ad;
6546 int err;
6547 u32 sid = 0;
6548 struct ib_security_struct *sec = ib_sec;
6549 struct lsm_ibendport_audit ibendport;
6550
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006551 err = security_ib_endport_sid(&selinux_state, dev_name, port_num,
6552 &sid);
Daniel Jurgensab861df2017-05-19 15:48:58 +03006553
6554 if (err)
6555 return err;
6556
6557 ad.type = LSM_AUDIT_DATA_IBENDPORT;
6558 strncpy(ibendport.dev_name, dev_name, sizeof(ibendport.dev_name));
6559 ibendport.port = port_num;
6560 ad.u.ibendport = &ibendport;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006561 return avc_has_perm(&selinux_state,
6562 sec->sid, sid,
Daniel Jurgensab861df2017-05-19 15:48:58 +03006563 SECCLASS_INFINIBAND_ENDPORT,
6564 INFINIBAND_ENDPORT__MANAGE_SUBNET, &ad);
6565}
6566
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006567static int selinux_ib_alloc_security(void **ib_sec)
6568{
6569 struct ib_security_struct *sec;
6570
6571 sec = kzalloc(sizeof(*sec), GFP_KERNEL);
6572 if (!sec)
6573 return -ENOMEM;
6574 sec->sid = current_sid();
6575
6576 *ib_sec = sec;
6577 return 0;
6578}
6579
6580static void selinux_ib_free_security(void *ib_sec)
6581{
6582 kfree(ib_sec);
6583}
Michael LeMayd7200242006-06-22 14:47:17 -07006584#endif
6585
Chenbo Fengec27c352017-10-18 13:00:25 -07006586#ifdef CONFIG_BPF_SYSCALL
6587static int selinux_bpf(int cmd, union bpf_attr *attr,
6588 unsigned int size)
6589{
6590 u32 sid = current_sid();
6591 int ret;
6592
6593 switch (cmd) {
6594 case BPF_MAP_CREATE:
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006595 ret = avc_has_perm(&selinux_state,
6596 sid, sid, SECCLASS_BPF, BPF__MAP_CREATE,
Chenbo Fengec27c352017-10-18 13:00:25 -07006597 NULL);
6598 break;
6599 case BPF_PROG_LOAD:
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006600 ret = avc_has_perm(&selinux_state,
6601 sid, sid, SECCLASS_BPF, BPF__PROG_LOAD,
Chenbo Fengec27c352017-10-18 13:00:25 -07006602 NULL);
6603 break;
6604 default:
6605 ret = 0;
6606 break;
6607 }
6608
6609 return ret;
6610}
6611
6612static u32 bpf_map_fmode_to_av(fmode_t fmode)
6613{
6614 u32 av = 0;
6615
6616 if (fmode & FMODE_READ)
6617 av |= BPF__MAP_READ;
6618 if (fmode & FMODE_WRITE)
6619 av |= BPF__MAP_WRITE;
6620 return av;
6621}
6622
Chenbo Fengf66e4482017-10-18 13:00:26 -07006623/* This function will check the file pass through unix socket or binder to see
6624 * if it is a bpf related object. And apply correspinding checks on the bpf
6625 * object based on the type. The bpf maps and programs, not like other files and
6626 * socket, are using a shared anonymous inode inside the kernel as their inode.
6627 * So checking that inode cannot identify if the process have privilege to
6628 * access the bpf object and that's why we have to add this additional check in
6629 * selinux_file_receive and selinux_binder_transfer_files.
6630 */
6631static int bpf_fd_pass(struct file *file, u32 sid)
6632{
6633 struct bpf_security_struct *bpfsec;
6634 struct bpf_prog *prog;
6635 struct bpf_map *map;
6636 int ret;
6637
6638 if (file->f_op == &bpf_map_fops) {
6639 map = file->private_data;
6640 bpfsec = map->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006641 ret = avc_has_perm(&selinux_state,
6642 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengf66e4482017-10-18 13:00:26 -07006643 bpf_map_fmode_to_av(file->f_mode), NULL);
6644 if (ret)
6645 return ret;
6646 } else if (file->f_op == &bpf_prog_fops) {
6647 prog = file->private_data;
6648 bpfsec = prog->aux->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__PROG_RUN, NULL);
6652 if (ret)
6653 return ret;
6654 }
6655 return 0;
6656}
6657
Chenbo Fengec27c352017-10-18 13:00:25 -07006658static int selinux_bpf_map(struct bpf_map *map, fmode_t fmode)
6659{
6660 u32 sid = current_sid();
6661 struct bpf_security_struct *bpfsec;
6662
6663 bpfsec = map->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006664 return avc_has_perm(&selinux_state,
6665 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengec27c352017-10-18 13:00:25 -07006666 bpf_map_fmode_to_av(fmode), NULL);
6667}
6668
6669static int selinux_bpf_prog(struct bpf_prog *prog)
6670{
6671 u32 sid = current_sid();
6672 struct bpf_security_struct *bpfsec;
6673
6674 bpfsec = prog->aux->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006675 return avc_has_perm(&selinux_state,
6676 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengec27c352017-10-18 13:00:25 -07006677 BPF__PROG_RUN, NULL);
6678}
6679
6680static int selinux_bpf_map_alloc(struct bpf_map *map)
6681{
6682 struct bpf_security_struct *bpfsec;
6683
6684 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6685 if (!bpfsec)
6686 return -ENOMEM;
6687
6688 bpfsec->sid = current_sid();
6689 map->security = bpfsec;
6690
6691 return 0;
6692}
6693
6694static void selinux_bpf_map_free(struct bpf_map *map)
6695{
6696 struct bpf_security_struct *bpfsec = map->security;
6697
6698 map->security = NULL;
6699 kfree(bpfsec);
6700}
6701
6702static int selinux_bpf_prog_alloc(struct bpf_prog_aux *aux)
6703{
6704 struct bpf_security_struct *bpfsec;
6705
6706 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6707 if (!bpfsec)
6708 return -ENOMEM;
6709
6710 bpfsec->sid = current_sid();
6711 aux->security = bpfsec;
6712
6713 return 0;
6714}
6715
6716static void selinux_bpf_prog_free(struct bpf_prog_aux *aux)
6717{
6718 struct bpf_security_struct *bpfsec = aux->security;
6719
6720 aux->security = NULL;
6721 kfree(bpfsec);
6722}
6723#endif
6724
James Morrisca97d932017-02-15 00:18:51 +11006725static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {
Casey Schauflere20b0432015-05-02 15:11:36 -07006726 LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr),
6727 LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction),
6728 LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder),
6729 LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file),
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02006730
Casey Schauflere20b0432015-05-02 15:11:36 -07006731 LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check),
6732 LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme),
6733 LSM_HOOK_INIT(capget, selinux_capget),
6734 LSM_HOOK_INIT(capset, selinux_capset),
6735 LSM_HOOK_INIT(capable, selinux_capable),
6736 LSM_HOOK_INIT(quotactl, selinux_quotactl),
6737 LSM_HOOK_INIT(quota_on, selinux_quota_on),
6738 LSM_HOOK_INIT(syslog, selinux_syslog),
6739 LSM_HOOK_INIT(vm_enough_memory, selinux_vm_enough_memory),
Stephen Smalley79af7302015-01-21 10:54:10 -05006740
Casey Schauflere20b0432015-05-02 15:11:36 -07006741 LSM_HOOK_INIT(netlink_send, selinux_netlink_send),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006742
Casey Schauflere20b0432015-05-02 15:11:36 -07006743 LSM_HOOK_INIT(bprm_set_creds, selinux_bprm_set_creds),
6744 LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds),
6745 LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006746
Casey Schauflere20b0432015-05-02 15:11:36 -07006747 LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security),
6748 LSM_HOOK_INIT(sb_free_security, selinux_sb_free_security),
Al Viro5b400232018-12-12 20:13:29 -05006749 LSM_HOOK_INIT(sb_eat_lsm_opts, selinux_sb_eat_lsm_opts),
Al Viro204cc0c2018-12-13 13:41:47 -05006750 LSM_HOOK_INIT(sb_free_mnt_opts, selinux_free_mnt_opts),
Casey Schauflere20b0432015-05-02 15:11:36 -07006751 LSM_HOOK_INIT(sb_remount, selinux_sb_remount),
6752 LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount),
6753 LSM_HOOK_INIT(sb_show_options, selinux_sb_show_options),
6754 LSM_HOOK_INIT(sb_statfs, selinux_sb_statfs),
6755 LSM_HOOK_INIT(sb_mount, selinux_mount),
6756 LSM_HOOK_INIT(sb_umount, selinux_umount),
6757 LSM_HOOK_INIT(sb_set_mnt_opts, selinux_set_mnt_opts),
6758 LSM_HOOK_INIT(sb_clone_mnt_opts, selinux_sb_clone_mnt_opts),
6759 LSM_HOOK_INIT(sb_parse_opts_str, selinux_parse_opts_str),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006760
Casey Schauflere20b0432015-05-02 15:11:36 -07006761 LSM_HOOK_INIT(dentry_init_security, selinux_dentry_init_security),
Vivek Goyala518b0a2016-07-13 10:44:53 -04006762 LSM_HOOK_INIT(dentry_create_files_as, selinux_dentry_create_files_as),
Eric Parise0007522008-03-05 10:31:54 -05006763
Casey Schauflere20b0432015-05-02 15:11:36 -07006764 LSM_HOOK_INIT(inode_alloc_security, selinux_inode_alloc_security),
6765 LSM_HOOK_INIT(inode_free_security, selinux_inode_free_security),
6766 LSM_HOOK_INIT(inode_init_security, selinux_inode_init_security),
6767 LSM_HOOK_INIT(inode_create, selinux_inode_create),
6768 LSM_HOOK_INIT(inode_link, selinux_inode_link),
6769 LSM_HOOK_INIT(inode_unlink, selinux_inode_unlink),
6770 LSM_HOOK_INIT(inode_symlink, selinux_inode_symlink),
6771 LSM_HOOK_INIT(inode_mkdir, selinux_inode_mkdir),
6772 LSM_HOOK_INIT(inode_rmdir, selinux_inode_rmdir),
6773 LSM_HOOK_INIT(inode_mknod, selinux_inode_mknod),
6774 LSM_HOOK_INIT(inode_rename, selinux_inode_rename),
6775 LSM_HOOK_INIT(inode_readlink, selinux_inode_readlink),
6776 LSM_HOOK_INIT(inode_follow_link, selinux_inode_follow_link),
6777 LSM_HOOK_INIT(inode_permission, selinux_inode_permission),
6778 LSM_HOOK_INIT(inode_setattr, selinux_inode_setattr),
6779 LSM_HOOK_INIT(inode_getattr, selinux_inode_getattr),
6780 LSM_HOOK_INIT(inode_setxattr, selinux_inode_setxattr),
6781 LSM_HOOK_INIT(inode_post_setxattr, selinux_inode_post_setxattr),
6782 LSM_HOOK_INIT(inode_getxattr, selinux_inode_getxattr),
6783 LSM_HOOK_INIT(inode_listxattr, selinux_inode_listxattr),
6784 LSM_HOOK_INIT(inode_removexattr, selinux_inode_removexattr),
6785 LSM_HOOK_INIT(inode_getsecurity, selinux_inode_getsecurity),
6786 LSM_HOOK_INIT(inode_setsecurity, selinux_inode_setsecurity),
6787 LSM_HOOK_INIT(inode_listsecurity, selinux_inode_listsecurity),
6788 LSM_HOOK_INIT(inode_getsecid, selinux_inode_getsecid),
Vivek Goyal56909eb2016-07-13 10:44:48 -04006789 LSM_HOOK_INIT(inode_copy_up, selinux_inode_copy_up),
Vivek Goyal19472b62016-07-13 10:44:50 -04006790 LSM_HOOK_INIT(inode_copy_up_xattr, selinux_inode_copy_up_xattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006791
Casey Schauflere20b0432015-05-02 15:11:36 -07006792 LSM_HOOK_INIT(file_permission, selinux_file_permission),
6793 LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security),
6794 LSM_HOOK_INIT(file_free_security, selinux_file_free_security),
6795 LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl),
6796 LSM_HOOK_INIT(mmap_file, selinux_mmap_file),
6797 LSM_HOOK_INIT(mmap_addr, selinux_mmap_addr),
6798 LSM_HOOK_INIT(file_mprotect, selinux_file_mprotect),
6799 LSM_HOOK_INIT(file_lock, selinux_file_lock),
6800 LSM_HOOK_INIT(file_fcntl, selinux_file_fcntl),
6801 LSM_HOOK_INIT(file_set_fowner, selinux_file_set_fowner),
6802 LSM_HOOK_INIT(file_send_sigiotask, selinux_file_send_sigiotask),
6803 LSM_HOOK_INIT(file_receive, selinux_file_receive),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006804
Casey Schauflere20b0432015-05-02 15:11:36 -07006805 LSM_HOOK_INIT(file_open, selinux_file_open),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006806
Tetsuo Handaa79be232017-03-28 23:08:45 +09006807 LSM_HOOK_INIT(task_alloc, selinux_task_alloc),
Casey Schauflere20b0432015-05-02 15:11:36 -07006808 LSM_HOOK_INIT(cred_alloc_blank, selinux_cred_alloc_blank),
6809 LSM_HOOK_INIT(cred_free, selinux_cred_free),
6810 LSM_HOOK_INIT(cred_prepare, selinux_cred_prepare),
6811 LSM_HOOK_INIT(cred_transfer, selinux_cred_transfer),
Matthew Garrett3ec30112018-01-08 13:36:19 -08006812 LSM_HOOK_INIT(cred_getsecid, selinux_cred_getsecid),
Casey Schauflere20b0432015-05-02 15:11:36 -07006813 LSM_HOOK_INIT(kernel_act_as, selinux_kernel_act_as),
6814 LSM_HOOK_INIT(kernel_create_files_as, selinux_kernel_create_files_as),
6815 LSM_HOOK_INIT(kernel_module_request, selinux_kernel_module_request),
Mimi Zoharc77b8cd2018-07-13 14:06:02 -04006816 LSM_HOOK_INIT(kernel_load_data, selinux_kernel_load_data),
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07006817 LSM_HOOK_INIT(kernel_read_file, selinux_kernel_read_file),
Casey Schauflere20b0432015-05-02 15:11:36 -07006818 LSM_HOOK_INIT(task_setpgid, selinux_task_setpgid),
6819 LSM_HOOK_INIT(task_getpgid, selinux_task_getpgid),
6820 LSM_HOOK_INIT(task_getsid, selinux_task_getsid),
6821 LSM_HOOK_INIT(task_getsecid, selinux_task_getsecid),
6822 LSM_HOOK_INIT(task_setnice, selinux_task_setnice),
6823 LSM_HOOK_INIT(task_setioprio, selinux_task_setioprio),
6824 LSM_HOOK_INIT(task_getioprio, selinux_task_getioprio),
Stephen Smalley791ec492017-02-17 07:57:00 -05006825 LSM_HOOK_INIT(task_prlimit, selinux_task_prlimit),
Casey Schauflere20b0432015-05-02 15:11:36 -07006826 LSM_HOOK_INIT(task_setrlimit, selinux_task_setrlimit),
6827 LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler),
6828 LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler),
6829 LSM_HOOK_INIT(task_movememory, selinux_task_movememory),
6830 LSM_HOOK_INIT(task_kill, selinux_task_kill),
Casey Schauflere20b0432015-05-02 15:11:36 -07006831 LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode),
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09006832
Casey Schauflere20b0432015-05-02 15:11:36 -07006833 LSM_HOOK_INIT(ipc_permission, selinux_ipc_permission),
6834 LSM_HOOK_INIT(ipc_getsecid, selinux_ipc_getsecid),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006835
Casey Schauflere20b0432015-05-02 15:11:36 -07006836 LSM_HOOK_INIT(msg_msg_alloc_security, selinux_msg_msg_alloc_security),
6837 LSM_HOOK_INIT(msg_msg_free_security, selinux_msg_msg_free_security),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006838
Casey Schauflere20b0432015-05-02 15:11:36 -07006839 LSM_HOOK_INIT(msg_queue_alloc_security,
6840 selinux_msg_queue_alloc_security),
6841 LSM_HOOK_INIT(msg_queue_free_security, selinux_msg_queue_free_security),
6842 LSM_HOOK_INIT(msg_queue_associate, selinux_msg_queue_associate),
6843 LSM_HOOK_INIT(msg_queue_msgctl, selinux_msg_queue_msgctl),
6844 LSM_HOOK_INIT(msg_queue_msgsnd, selinux_msg_queue_msgsnd),
6845 LSM_HOOK_INIT(msg_queue_msgrcv, selinux_msg_queue_msgrcv),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006846
Casey Schauflere20b0432015-05-02 15:11:36 -07006847 LSM_HOOK_INIT(shm_alloc_security, selinux_shm_alloc_security),
6848 LSM_HOOK_INIT(shm_free_security, selinux_shm_free_security),
6849 LSM_HOOK_INIT(shm_associate, selinux_shm_associate),
6850 LSM_HOOK_INIT(shm_shmctl, selinux_shm_shmctl),
6851 LSM_HOOK_INIT(shm_shmat, selinux_shm_shmat),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006852
Casey Schauflere20b0432015-05-02 15:11:36 -07006853 LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security),
6854 LSM_HOOK_INIT(sem_free_security, selinux_sem_free_security),
6855 LSM_HOOK_INIT(sem_associate, selinux_sem_associate),
6856 LSM_HOOK_INIT(sem_semctl, selinux_sem_semctl),
6857 LSM_HOOK_INIT(sem_semop, selinux_sem_semop),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006858
Casey Schauflere20b0432015-05-02 15:11:36 -07006859 LSM_HOOK_INIT(d_instantiate, selinux_d_instantiate),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006860
Casey Schauflere20b0432015-05-02 15:11:36 -07006861 LSM_HOOK_INIT(getprocattr, selinux_getprocattr),
6862 LSM_HOOK_INIT(setprocattr, selinux_setprocattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006863
Casey Schauflere20b0432015-05-02 15:11:36 -07006864 LSM_HOOK_INIT(ismaclabel, selinux_ismaclabel),
6865 LSM_HOOK_INIT(secid_to_secctx, selinux_secid_to_secctx),
6866 LSM_HOOK_INIT(secctx_to_secid, selinux_secctx_to_secid),
6867 LSM_HOOK_INIT(release_secctx, selinux_release_secctx),
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006868 LSM_HOOK_INIT(inode_invalidate_secctx, selinux_inode_invalidate_secctx),
Casey Schauflere20b0432015-05-02 15:11:36 -07006869 LSM_HOOK_INIT(inode_notifysecctx, selinux_inode_notifysecctx),
6870 LSM_HOOK_INIT(inode_setsecctx, selinux_inode_setsecctx),
6871 LSM_HOOK_INIT(inode_getsecctx, selinux_inode_getsecctx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006872
Casey Schauflere20b0432015-05-02 15:11:36 -07006873 LSM_HOOK_INIT(unix_stream_connect, selinux_socket_unix_stream_connect),
6874 LSM_HOOK_INIT(unix_may_send, selinux_socket_unix_may_send),
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006875
Casey Schauflere20b0432015-05-02 15:11:36 -07006876 LSM_HOOK_INIT(socket_create, selinux_socket_create),
6877 LSM_HOOK_INIT(socket_post_create, selinux_socket_post_create),
David Herrmann0b811db2018-05-04 16:28:21 +02006878 LSM_HOOK_INIT(socket_socketpair, selinux_socket_socketpair),
Casey Schauflere20b0432015-05-02 15:11:36 -07006879 LSM_HOOK_INIT(socket_bind, selinux_socket_bind),
6880 LSM_HOOK_INIT(socket_connect, selinux_socket_connect),
6881 LSM_HOOK_INIT(socket_listen, selinux_socket_listen),
6882 LSM_HOOK_INIT(socket_accept, selinux_socket_accept),
6883 LSM_HOOK_INIT(socket_sendmsg, selinux_socket_sendmsg),
6884 LSM_HOOK_INIT(socket_recvmsg, selinux_socket_recvmsg),
6885 LSM_HOOK_INIT(socket_getsockname, selinux_socket_getsockname),
6886 LSM_HOOK_INIT(socket_getpeername, selinux_socket_getpeername),
6887 LSM_HOOK_INIT(socket_getsockopt, selinux_socket_getsockopt),
6888 LSM_HOOK_INIT(socket_setsockopt, selinux_socket_setsockopt),
6889 LSM_HOOK_INIT(socket_shutdown, selinux_socket_shutdown),
6890 LSM_HOOK_INIT(socket_sock_rcv_skb, selinux_socket_sock_rcv_skb),
6891 LSM_HOOK_INIT(socket_getpeersec_stream,
6892 selinux_socket_getpeersec_stream),
6893 LSM_HOOK_INIT(socket_getpeersec_dgram, selinux_socket_getpeersec_dgram),
6894 LSM_HOOK_INIT(sk_alloc_security, selinux_sk_alloc_security),
6895 LSM_HOOK_INIT(sk_free_security, selinux_sk_free_security),
6896 LSM_HOOK_INIT(sk_clone_security, selinux_sk_clone_security),
6897 LSM_HOOK_INIT(sk_getsecid, selinux_sk_getsecid),
6898 LSM_HOOK_INIT(sock_graft, selinux_sock_graft),
Richard Hainesd4529302018-02-13 20:57:18 +00006899 LSM_HOOK_INIT(sctp_assoc_request, selinux_sctp_assoc_request),
6900 LSM_HOOK_INIT(sctp_sk_clone, selinux_sctp_sk_clone),
6901 LSM_HOOK_INIT(sctp_bind_connect, selinux_sctp_bind_connect),
Casey Schauflere20b0432015-05-02 15:11:36 -07006902 LSM_HOOK_INIT(inet_conn_request, selinux_inet_conn_request),
6903 LSM_HOOK_INIT(inet_csk_clone, selinux_inet_csk_clone),
6904 LSM_HOOK_INIT(inet_conn_established, selinux_inet_conn_established),
6905 LSM_HOOK_INIT(secmark_relabel_packet, selinux_secmark_relabel_packet),
6906 LSM_HOOK_INIT(secmark_refcount_inc, selinux_secmark_refcount_inc),
6907 LSM_HOOK_INIT(secmark_refcount_dec, selinux_secmark_refcount_dec),
6908 LSM_HOOK_INIT(req_classify_flow, selinux_req_classify_flow),
6909 LSM_HOOK_INIT(tun_dev_alloc_security, selinux_tun_dev_alloc_security),
6910 LSM_HOOK_INIT(tun_dev_free_security, selinux_tun_dev_free_security),
6911 LSM_HOOK_INIT(tun_dev_create, selinux_tun_dev_create),
6912 LSM_HOOK_INIT(tun_dev_attach_queue, selinux_tun_dev_attach_queue),
6913 LSM_HOOK_INIT(tun_dev_attach, selinux_tun_dev_attach),
6914 LSM_HOOK_INIT(tun_dev_open, selinux_tun_dev_open),
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006915#ifdef CONFIG_SECURITY_INFINIBAND
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006916 LSM_HOOK_INIT(ib_pkey_access, selinux_ib_pkey_access),
Daniel Jurgensab861df2017-05-19 15:48:58 +03006917 LSM_HOOK_INIT(ib_endport_manage_subnet,
6918 selinux_ib_endport_manage_subnet),
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006919 LSM_HOOK_INIT(ib_alloc_security, selinux_ib_alloc_security),
6920 LSM_HOOK_INIT(ib_free_security, selinux_ib_free_security),
6921#endif
Trent Jaegerd28d1e02005-12-13 23:12:40 -08006922#ifdef CONFIG_SECURITY_NETWORK_XFRM
Casey Schauflere20b0432015-05-02 15:11:36 -07006923 LSM_HOOK_INIT(xfrm_policy_alloc_security, selinux_xfrm_policy_alloc),
6924 LSM_HOOK_INIT(xfrm_policy_clone_security, selinux_xfrm_policy_clone),
6925 LSM_HOOK_INIT(xfrm_policy_free_security, selinux_xfrm_policy_free),
6926 LSM_HOOK_INIT(xfrm_policy_delete_security, selinux_xfrm_policy_delete),
6927 LSM_HOOK_INIT(xfrm_state_alloc, selinux_xfrm_state_alloc),
6928 LSM_HOOK_INIT(xfrm_state_alloc_acquire,
6929 selinux_xfrm_state_alloc_acquire),
6930 LSM_HOOK_INIT(xfrm_state_free_security, selinux_xfrm_state_free),
6931 LSM_HOOK_INIT(xfrm_state_delete_security, selinux_xfrm_state_delete),
6932 LSM_HOOK_INIT(xfrm_policy_lookup, selinux_xfrm_policy_lookup),
6933 LSM_HOOK_INIT(xfrm_state_pol_flow_match,
6934 selinux_xfrm_state_pol_flow_match),
6935 LSM_HOOK_INIT(xfrm_decode_session, selinux_xfrm_decode_session),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006936#endif
Michael LeMayd7200242006-06-22 14:47:17 -07006937
6938#ifdef CONFIG_KEYS
Casey Schauflere20b0432015-05-02 15:11:36 -07006939 LSM_HOOK_INIT(key_alloc, selinux_key_alloc),
6940 LSM_HOOK_INIT(key_free, selinux_key_free),
6941 LSM_HOOK_INIT(key_permission, selinux_key_permission),
6942 LSM_HOOK_INIT(key_getsecurity, selinux_key_getsecurity),
Michael LeMayd7200242006-06-22 14:47:17 -07006943#endif
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02006944
6945#ifdef CONFIG_AUDIT
Casey Schauflere20b0432015-05-02 15:11:36 -07006946 LSM_HOOK_INIT(audit_rule_init, selinux_audit_rule_init),
6947 LSM_HOOK_INIT(audit_rule_known, selinux_audit_rule_known),
6948 LSM_HOOK_INIT(audit_rule_match, selinux_audit_rule_match),
6949 LSM_HOOK_INIT(audit_rule_free, selinux_audit_rule_free),
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02006950#endif
Chenbo Fengec27c352017-10-18 13:00:25 -07006951
6952#ifdef CONFIG_BPF_SYSCALL
6953 LSM_HOOK_INIT(bpf, selinux_bpf),
6954 LSM_HOOK_INIT(bpf_map, selinux_bpf_map),
6955 LSM_HOOK_INIT(bpf_prog, selinux_bpf_prog),
6956 LSM_HOOK_INIT(bpf_map_alloc_security, selinux_bpf_map_alloc),
6957 LSM_HOOK_INIT(bpf_prog_alloc_security, selinux_bpf_prog_alloc),
6958 LSM_HOOK_INIT(bpf_map_free_security, selinux_bpf_map_free),
6959 LSM_HOOK_INIT(bpf_prog_free_security, selinux_bpf_prog_free),
6960#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006961};
6962
6963static __init int selinux_init(void)
6964{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07006965 if (!security_module_enable("selinux")) {
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02006966 selinux_enabled = 0;
6967 return 0;
6968 }
6969
Linus Torvalds1da177e2005-04-16 15:20:36 -07006970 if (!selinux_enabled) {
peter enderborgc103a912018-06-12 10:09:03 +02006971 pr_info("SELinux: Disabled at boot.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006972 return 0;
6973 }
6974
peter enderborgc103a912018-06-12 10:09:03 +02006975 pr_info("SELinux: Initializing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006976
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006977 memset(&selinux_state, 0, sizeof(selinux_state));
Paul Mooree5a5ca92018-03-01 17:38:30 -05006978 enforcing_set(&selinux_state, selinux_enforcing_boot);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006979 selinux_state.checkreqprot = selinux_checkreqprot_boot;
6980 selinux_ss_init(&selinux_state.ss);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006981 selinux_avc_init(&selinux_state.avc);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006982
Linus Torvalds1da177e2005-04-16 15:20:36 -07006983 /* Set the security state for the initial task. */
David Howellsd84f4f92008-11-14 10:39:23 +11006984 cred_init_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006985
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04006986 default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
6987
James Morris7cae7e22006-03-22 00:09:22 -08006988 sel_inode_cache = kmem_cache_create("selinux_inode_security",
6989 sizeof(struct inode_security_struct),
Paul Mundt20c2df82007-07-20 10:11:58 +09006990 0, SLAB_PANIC, NULL);
Sangwoo63205652015-10-21 17:44:30 -04006991 file_security_cache = kmem_cache_create("selinux_file_security",
6992 sizeof(struct file_security_struct),
6993 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006994 avc_init();
6995
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006996 avtab_cache_init();
6997
6998 ebitmap_cache_init();
6999
7000 hashtab_cache_init();
7001
Casey Schauflerd69dece52017-01-18 17:09:05 -08007002 security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks), "selinux");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007003
Paul Moore615e51f2014-06-26 14:33:56 -04007004 if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET))
7005 panic("SELinux: Unable to register AVC netcache callback\n");
7006
Daniel Jurgens8f408ab2017-05-19 15:48:53 +03007007 if (avc_add_callback(selinux_lsm_notifier_avc_callback, AVC_CALLBACK_RESET))
7008 panic("SELinux: Unable to register AVC LSM notifier callback\n");
7009
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007010 if (selinux_enforcing_boot)
peter enderborgc103a912018-06-12 10:09:03 +02007011 pr_debug("SELinux: Starting in enforcing mode\n");
Eric Paris828dfe12008-04-17 13:17:49 -04007012 else
peter enderborgc103a912018-06-12 10:09:03 +02007013 pr_debug("SELinux: Starting in permissive mode\n");
Michael LeMayd7200242006-06-22 14:47:17 -07007014
Linus Torvalds1da177e2005-04-16 15:20:36 -07007015 return 0;
7016}
7017
Al Viroe8c26252010-03-23 06:36:54 -04007018static void delayed_superblock_init(struct super_block *sb, void *unused)
7019{
Al Viro204cc0c2018-12-13 13:41:47 -05007020 selinux_set_mnt_opts(sb, NULL, 0, NULL);
Al Viroe8c26252010-03-23 06:36:54 -04007021}
7022
Linus Torvalds1da177e2005-04-16 15:20:36 -07007023void selinux_complete_init(void)
7024{
peter enderborgc103a912018-06-12 10:09:03 +02007025 pr_debug("SELinux: Completing initialization.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007026
7027 /* Set up any superblocks initialized prior to the policy load. */
peter enderborgc103a912018-06-12 10:09:03 +02007028 pr_debug("SELinux: Setting up existing superblocks.\n");
Al Viroe8c26252010-03-23 06:36:54 -04007029 iterate_supers(delayed_superblock_init, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007030}
7031
7032/* SELinux requires early initialization in order to label
7033 all processes and objects when they are created. */
Kees Cook3d6e5f62018-10-10 17:18:23 -07007034DEFINE_LSM(selinux) = {
Kees Cook07aed2f2018-10-10 17:18:24 -07007035 .name = "selinux",
Kees Cook3d6e5f62018-10-10 17:18:23 -07007036 .init = selinux_init,
7037};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007038
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007039#if defined(CONFIG_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007040
Florian Westphal591bb272017-07-26 11:40:52 +02007041static const struct nf_hook_ops selinux_nf_ops[] = {
Paul Mooreeffad8d2008-01-29 08:49:27 -05007042 {
7043 .hook = selinux_ipv4_postroute,
Alban Crequy2597a832012-05-14 03:56:39 +00007044 .pf = NFPROTO_IPV4,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007045 .hooknum = NF_INET_POST_ROUTING,
7046 .priority = NF_IP_PRI_SELINUX_LAST,
7047 },
7048 {
7049 .hook = selinux_ipv4_forward,
Alban Crequy2597a832012-05-14 03:56:39 +00007050 .pf = NFPROTO_IPV4,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007051 .hooknum = NF_INET_FORWARD,
7052 .priority = NF_IP_PRI_SELINUX_FIRST,
Paul Moore948bf852008-10-10 10:16:32 -04007053 },
7054 {
7055 .hook = selinux_ipv4_output,
Alban Crequy2597a832012-05-14 03:56:39 +00007056 .pf = NFPROTO_IPV4,
Paul Moore948bf852008-10-10 10:16:32 -04007057 .hooknum = NF_INET_LOCAL_OUT,
7058 .priority = NF_IP_PRI_SELINUX_FIRST,
Jiri Pirko25db6be2014-09-03 17:42:13 +02007059 },
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04007060#if IS_ENABLED(CONFIG_IPV6)
Paul Mooreeffad8d2008-01-29 08:49:27 -05007061 {
7062 .hook = selinux_ipv6_postroute,
Alban Crequy2597a832012-05-14 03:56:39 +00007063 .pf = NFPROTO_IPV6,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007064 .hooknum = NF_INET_POST_ROUTING,
7065 .priority = NF_IP6_PRI_SELINUX_LAST,
7066 },
7067 {
7068 .hook = selinux_ipv6_forward,
Alban Crequy2597a832012-05-14 03:56:39 +00007069 .pf = NFPROTO_IPV6,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007070 .hooknum = NF_INET_FORWARD,
7071 .priority = NF_IP6_PRI_SELINUX_FIRST,
Jiri Pirko25db6be2014-09-03 17:42:13 +02007072 },
Huw Davies2917f572016-06-27 15:06:15 -04007073 {
7074 .hook = selinux_ipv6_output,
7075 .pf = NFPROTO_IPV6,
7076 .hooknum = NF_INET_LOCAL_OUT,
7077 .priority = NF_IP6_PRI_SELINUX_FIRST,
7078 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07007079#endif /* IPV6 */
Jiri Pirko25db6be2014-09-03 17:42:13 +02007080};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007081
Florian Westphal8e71bf72017-04-21 11:49:09 +02007082static int __net_init selinux_nf_register(struct net *net)
7083{
7084 return nf_register_net_hooks(net, selinux_nf_ops,
7085 ARRAY_SIZE(selinux_nf_ops));
7086}
7087
7088static void __net_exit selinux_nf_unregister(struct net *net)
7089{
7090 nf_unregister_net_hooks(net, selinux_nf_ops,
7091 ARRAY_SIZE(selinux_nf_ops));
7092}
7093
7094static struct pernet_operations selinux_net_ops = {
7095 .init = selinux_nf_register,
7096 .exit = selinux_nf_unregister,
7097};
7098
Linus Torvalds1da177e2005-04-16 15:20:36 -07007099static int __init selinux_nf_ip_init(void)
7100{
Jiri Pirko25db6be2014-09-03 17:42:13 +02007101 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007102
7103 if (!selinux_enabled)
Jiri Pirko25db6be2014-09-03 17:42:13 +02007104 return 0;
Eric Parisfadcdb42007-02-22 18:11:31 -05007105
peter enderborgc103a912018-06-12 10:09:03 +02007106 pr_debug("SELinux: Registering netfilter hooks\n");
Eric Parisfadcdb42007-02-22 18:11:31 -05007107
Florian Westphal8e71bf72017-04-21 11:49:09 +02007108 err = register_pernet_subsys(&selinux_net_ops);
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07007109 if (err)
Florian Westphal8e71bf72017-04-21 11:49:09 +02007110 panic("SELinux: register_pernet_subsys: error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007111
Jiri Pirko25db6be2014-09-03 17:42:13 +02007112 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007113}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007114__initcall(selinux_nf_ip_init);
7115
7116#ifdef CONFIG_SECURITY_SELINUX_DISABLE
7117static void selinux_nf_ip_exit(void)
7118{
peter enderborgc103a912018-06-12 10:09:03 +02007119 pr_debug("SELinux: Unregistering netfilter hooks\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007120
Florian Westphal8e71bf72017-04-21 11:49:09 +02007121 unregister_pernet_subsys(&selinux_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007122}
7123#endif
7124
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007125#else /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007126
7127#ifdef CONFIG_SECURITY_SELINUX_DISABLE
7128#define selinux_nf_ip_exit()
7129#endif
7130
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007131#endif /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007132
7133#ifdef CONFIG_SECURITY_SELINUX_DISABLE
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007134int selinux_disable(struct selinux_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007135{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007136 if (state->initialized) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007137 /* Not permitted after initial policy load. */
7138 return -EINVAL;
7139 }
7140
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007141 if (state->disabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007142 /* Only do this once. */
7143 return -EINVAL;
7144 }
7145
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007146 state->disabled = 1;
7147
peter enderborgc103a912018-06-12 10:09:03 +02007148 pr_info("SELinux: Disabled at runtime.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007149
Stephen Smalley30d55282006-05-03 10:52:36 -04007150 selinux_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007151
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07007152 security_delete_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007153
Eric Parisaf8ff042009-09-20 21:23:01 -04007154 /* Try to destroy the avc node cache */
7155 avc_disable();
7156
Linus Torvalds1da177e2005-04-16 15:20:36 -07007157 /* Unregister netfilter hooks. */
7158 selinux_nf_ip_exit();
7159
7160 /* Unregister selinuxfs. */
7161 exit_sel_fs();
7162
7163 return 0;
7164}
7165#endif