blob: 74dd46de01b6a4a7dbde1b8d697b9888cbe85f21 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * NSA Security-Enhanced Linux (SELinux) security module
4 *
5 * This file contains the SELinux hook function implementations.
6 *
Stephen Smalley7efbb602017-08-17 13:32:36 -04007 * Authors: Stephen Smalley, <sds@tycho.nsa.gov>
Eric Paris828dfe12008-04-17 13:17:49 -04008 * Chris Vance, <cvance@nai.com>
9 * Wayne Salamon, <wsalamon@nai.com>
10 * James Morris <jmorris@redhat.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
12 * Copyright (C) 2001,2002 Networks Associates Technology, Inc.
Eric Paris2069f452008-07-04 09:47:13 +100013 * Copyright (C) 2003-2008 Red Hat, Inc., James Morris <jmorris@redhat.com>
14 * Eric Paris <eparis@redhat.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
Eric Paris828dfe12008-04-17 13:17:49 -040016 * <dgoeddel@trustedcs.com>
Paul Mooreed6d76e2009-08-28 18:12:49 -040017 * Copyright (C) 2006, 2007, 2009 Hewlett-Packard Development Company, L.P.
Paul Moore82c21bf2011-08-01 11:10:33 +000018 * Paul Moore <paul@paul-moore.com>
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +090019 * Copyright (C) 2007 Hitachi Software Engineering Co., Ltd.
Eric Paris828dfe12008-04-17 13:17:49 -040020 * Yuichi Nakamura <ynakam@hitachisoft.jp>
Daniel Jurgens3a976fa2017-05-19 15:48:56 +030021 * Copyright (C) 2016 Mellanox Technologies
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 */
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/init.h>
Eric Paris0b24dcb2011-02-25 15:39:20 -050025#include <linux/kd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/kernel.h>
Roland McGrath0d094ef2008-07-25 19:45:49 -070027#include <linux/tracehook.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/errno.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010029#include <linux/sched/signal.h>
Ingo Molnar29930022017-02-08 18:51:36 +010030#include <linux/sched/task.h>
Casey Schaufler3c4ed7b2015-05-02 15:10:46 -070031#include <linux/lsm_hooks.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/xattr.h>
33#include <linux/capability.h>
34#include <linux/unistd.h>
35#include <linux/mm.h>
36#include <linux/mman.h>
37#include <linux/slab.h>
38#include <linux/pagemap.h>
Eric Paris0b24dcb2011-02-25 15:39:20 -050039#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/swap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/spinlock.h>
42#include <linux/syscalls.h>
Eric Paris2a7dba32011-02-01 11:05:39 -050043#include <linux/dcache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/file.h>
Al Viro9f3acc32008-04-24 07:44:08 -040045#include <linux/fdtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/namei.h>
47#include <linux/mount.h>
David Howells442155c2018-11-01 23:07:24 +000048#include <linux/fs_context.h>
49#include <linux/fs_parser.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/netfilter_ipv4.h>
51#include <linux/netfilter_ipv6.h>
52#include <linux/tty.h>
53#include <net/icmp.h>
Stephen Hemminger227b60f2007-10-10 17:30:46 -070054#include <net/ip.h> /* for local_port_range[] */
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <net/tcp.h> /* struct or_callable used in sock_rcv_skb */
Paul Moore47180062013-12-04 16:10:45 -050056#include <net/inet_connection_sock.h>
Paul Moore220deb92008-01-29 08:38:23 -050057#include <net/net_namespace.h>
Paul Moored621d352008-01-29 08:43:36 -050058#include <net/netlabel.h>
Eric Parisf5269712008-05-14 11:27:45 -040059#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <asm/ioctls.h>
Arun Sharma600634972011-07-26 16:09:06 -070061#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <linux/bitops.h>
63#include <linux/interrupt.h>
64#include <linux/netdevice.h> /* for network interface checks */
Hong zhi guo77954982013-03-27 06:49:35 +000065#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <linux/tcp.h>
67#include <linux/udp.h>
James Morris2ee92d42006-11-13 16:09:01 -080068#include <linux/dccp.h>
Richard Hainesd4529302018-02-13 20:57:18 +000069#include <linux/sctp.h>
70#include <net/sctp/structs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#include <linux/quota.h>
72#include <linux/un.h> /* for Unix socket types */
73#include <net/af_unix.h> /* for Unix socket types */
74#include <linux/parser.h>
75#include <linux/nfs_mount.h>
76#include <net/ipv6.h>
77#include <linux/hugetlb.h>
78#include <linux/personality.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#include <linux/audit.h>
Eric Paris6931dfc2005-06-30 02:58:51 -070080#include <linux/string.h>
Eric Paris23970742006-09-25 23:32:01 -070081#include <linux/mutex.h>
Frank Mayharf06febc2008-09-12 09:54:39 -070082#include <linux/posix-timers.h>
Kees Cook00234592010-02-03 15:36:43 -080083#include <linux/syslog.h>
Serge E. Hallyn34867402011-03-23 16:43:17 -070084#include <linux/user_namespace.h>
Paul Gortmaker44fc7ea2011-05-26 20:52:10 -040085#include <linux/export.h>
Al Viro40401532012-02-13 03:58:52 +000086#include <linux/msg.h>
87#include <linux/shm.h>
Chenbo Fengec27c352017-10-18 13:00:25 -070088#include <linux/bpf.h>
Ondrej Mosnacekec882da2019-02-22 15:57:17 +010089#include <linux/kernfs.h>
90#include <linux/stringhash.h> /* for hashlen_string() */
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
Kees Cookbe6ec882018-10-01 17:08:57 -0700124int selinux_enabled __lsm_ro_after_init = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125#ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126static int __init selinux_enabled_setup(char *str)
127{
Eric Parisf5269712008-05-14 11:27:45 -0400128 unsigned long enabled;
Jingoo Han29707b22014-02-05 15:13:14 +0900129 if (!kstrtoul(str, 0, &enabled))
Eric Parisf5269712008-05-14 11:27:45 -0400130 selinux_enabled = enabled ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 return 1;
132}
133__setup("selinux=", selinux_enabled_setup);
134#endif
135
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500136static unsigned int selinux_checkreqprot_boot =
137 CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE;
138
139static int __init checkreqprot_setup(char *str)
140{
141 unsigned long checkreqprot;
142
143 if (!kstrtoul(str, 0, &checkreqprot))
144 selinux_checkreqprot_boot = checkreqprot ? 1 : 0;
145 return 1;
146}
147__setup("checkreqprot=", checkreqprot_setup);
148
Paul Moored621d352008-01-29 08:43:36 -0500149/**
150 * selinux_secmark_enabled - Check to see if SECMARK is currently enabled
151 *
152 * Description:
153 * This function checks the SECMARK reference counter to see if any SECMARK
154 * targets are currently configured, if the reference counter is greater than
155 * zero SECMARK is considered to be enabled. Returns true (1) if SECMARK is
Chris PeBenito2be4d742013-05-03 09:05:39 -0400156 * enabled, false (0) if SECMARK is disabled. If the always_check_network
157 * policy capability is enabled, SECMARK is always considered enabled.
Paul Moored621d352008-01-29 08:43:36 -0500158 *
159 */
160static int selinux_secmark_enabled(void)
161{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500162 return (selinux_policycap_alwaysnetwork() ||
163 atomic_read(&selinux_secmark_refcount));
Chris PeBenito2be4d742013-05-03 09:05:39 -0400164}
165
166/**
167 * selinux_peerlbl_enabled - Check to see if peer labeling is currently enabled
168 *
169 * Description:
170 * This function checks if NetLabel or labeled IPSEC is enabled. Returns true
171 * (1) if any are enabled or false (0) if neither are enabled. If the
172 * always_check_network policy capability is enabled, peer labeling
173 * is always considered enabled.
174 *
175 */
176static int selinux_peerlbl_enabled(void)
177{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500178 return (selinux_policycap_alwaysnetwork() ||
179 netlbl_enabled() || selinux_xfrm_enabled());
Paul Moored621d352008-01-29 08:43:36 -0500180}
181
Paul Moore615e51f2014-06-26 14:33:56 -0400182static int selinux_netcache_avc_callback(u32 event)
183{
184 if (event == AVC_CALLBACK_RESET) {
185 sel_netif_flush();
186 sel_netnode_flush();
187 sel_netport_flush();
188 synchronize_net();
189 }
190 return 0;
191}
192
Daniel Jurgens8f408ab2017-05-19 15:48:53 +0300193static int selinux_lsm_notifier_avc_callback(u32 event)
194{
Daniel Jurgens409dcf32017-05-19 15:48:59 +0300195 if (event == AVC_CALLBACK_RESET) {
196 sel_ib_pkey_flush();
Janne Karhunen42df7442019-06-14 15:20:14 +0300197 call_blocking_lsm_notifier(LSM_POLICY_CHANGE, NULL);
Daniel Jurgens409dcf32017-05-19 15:48:59 +0300198 }
Daniel Jurgens8f408ab2017-05-19 15:48:53 +0300199
200 return 0;
201}
202
David Howellsd84f4f92008-11-14 10:39:23 +1100203/*
204 * initialise the security for the init task
205 */
206static void cred_init_security(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
David Howells3b11a1d2008-11-14 10:39:26 +1100208 struct cred *cred = (struct cred *) current->real_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 struct task_security_struct *tsec;
210
Casey Schauflerbbd36622018-11-12 09:30:56 -0800211 tsec = selinux_cred(cred);
David Howellsd84f4f92008-11-14 10:39:23 +1100212 tsec->osid = tsec->sid = SECINITSID_KERNEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213}
214
David Howells275bb412008-11-14 10:39:19 +1100215/*
David Howells88e67f32008-11-14 10:39:21 +1100216 * get the security ID of a set of credentials
217 */
218static inline u32 cred_sid(const struct cred *cred)
219{
220 const struct task_security_struct *tsec;
221
Casey Schaufler0c6cfa62018-09-21 17:17:16 -0700222 tsec = selinux_cred(cred);
David Howells88e67f32008-11-14 10:39:21 +1100223 return tsec->sid;
224}
225
226/*
David Howells3b11a1d2008-11-14 10:39:26 +1100227 * get the objective security ID of a task
David Howells275bb412008-11-14 10:39:19 +1100228 */
229static inline u32 task_sid(const struct task_struct *task)
230{
David Howells275bb412008-11-14 10:39:19 +1100231 u32 sid;
232
233 rcu_read_lock();
David Howells88e67f32008-11-14 10:39:21 +1100234 sid = cred_sid(__task_cred(task));
David Howells275bb412008-11-14 10:39:19 +1100235 rcu_read_unlock();
236 return sid;
237}
238
David Howells88e67f32008-11-14 10:39:21 +1100239/* Allocate and free functions for each kind of security blob. */
240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241static int inode_alloc_security(struct inode *inode)
242{
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700243 struct inode_security_struct *isec = selinux_inode(inode);
David Howells275bb412008-11-14 10:39:19 +1100244 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +0100246 spin_lock_init(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 INIT_LIST_HEAD(&isec->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 isec->inode = inode;
249 isec->sid = SECINITSID_UNLABELED;
250 isec->sclass = SECCLASS_FILE;
David Howells275bb412008-11-14 10:39:19 +1100251 isec->task_sid = sid;
Andreas Gruenbacher42059112016-11-10 22:18:27 +0100252 isec->initialized = LABEL_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 return 0;
255}
256
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500257static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry);
258
259/*
260 * Try reloading inode security labels that have been marked as invalid. The
261 * @may_sleep parameter indicates when sleeping and thus reloading labels is
Andreas Gruenbacher42059112016-11-10 22:18:27 +0100262 * allowed; when set to false, returns -ECHILD when the label is
Al Viroe9193282018-04-24 21:31:02 -0400263 * invalid. The @dentry parameter should be set to a dentry of the inode.
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500264 */
265static int __inode_security_revalidate(struct inode *inode,
Al Viroe9193282018-04-24 21:31:02 -0400266 struct dentry *dentry,
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500267 bool may_sleep)
268{
Casey Schaufler80788c22018-09-21 17:19:11 -0700269 struct inode_security_struct *isec = selinux_inode(inode);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500270
271 might_sleep_if(may_sleep);
272
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500273 if (selinux_state.initialized &&
274 isec->initialized != LABEL_INITIALIZED) {
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500275 if (!may_sleep)
276 return -ECHILD;
277
278 /*
279 * Try reloading the inode security label. This will fail if
280 * @opt_dentry is NULL and no dentry for this inode can be
281 * found; in that case, continue using the old label.
282 */
Al Viroe9193282018-04-24 21:31:02 -0400283 inode_doinit_with_dentry(inode, dentry);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500284 }
285 return 0;
286}
287
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500288static struct inode_security_struct *inode_security_novalidate(struct inode *inode)
289{
Casey Schaufler80788c22018-09-21 17:19:11 -0700290 return selinux_inode(inode);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500291}
292
293static struct inode_security_struct *inode_security_rcu(struct inode *inode, bool rcu)
294{
295 int error;
296
297 error = __inode_security_revalidate(inode, NULL, !rcu);
298 if (error)
299 return ERR_PTR(error);
Casey Schaufler80788c22018-09-21 17:19:11 -0700300 return selinux_inode(inode);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500301}
302
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500303/*
304 * Get the security label of an inode.
305 */
306static struct inode_security_struct *inode_security(struct inode *inode)
307{
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500308 __inode_security_revalidate(inode, NULL, true);
Casey Schaufler80788c22018-09-21 17:19:11 -0700309 return selinux_inode(inode);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500310}
311
Paul Moore2c971652016-04-19 16:36:28 -0400312static struct inode_security_struct *backing_inode_security_novalidate(struct dentry *dentry)
313{
314 struct inode *inode = d_backing_inode(dentry);
315
Casey Schaufler80788c22018-09-21 17:19:11 -0700316 return selinux_inode(inode);
Paul Moore2c971652016-04-19 16:36:28 -0400317}
318
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500319/*
320 * Get the security label of a dentry's backing inode.
321 */
322static struct inode_security_struct *backing_inode_security(struct dentry *dentry)
323{
324 struct inode *inode = d_backing_inode(dentry);
325
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500326 __inode_security_revalidate(inode, dentry, true);
Casey Schaufler80788c22018-09-21 17:19:11 -0700327 return selinux_inode(inode);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500328}
329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330static void inode_free_security(struct inode *inode)
331{
Casey Schaufler80788c22018-09-21 17:19:11 -0700332 struct inode_security_struct *isec = selinux_inode(inode);
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700333 struct superblock_security_struct *sbsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700335 if (!isec)
336 return;
337 sbsec = inode->i_sb->s_security;
Waiman Long9629d042015-07-10 17:19:56 -0400338 /*
339 * As not all inode security structures are in a list, we check for
340 * empty list outside of the lock to make sure that we won't waste
341 * time taking a lock doing nothing.
342 *
343 * The list_del_init() function can be safely called more than once.
344 * It should not be possible for this function to be called with
345 * concurrent list_add(), but for better safety against future changes
346 * in the code, we use list_empty_careful() here.
347 */
348 if (!list_empty_careful(&isec->list)) {
349 spin_lock(&sbsec->isec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 list_del_init(&isec->list);
Waiman Long9629d042015-07-10 17:19:56 -0400351 spin_unlock(&sbsec->isec_lock);
352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
355static int file_alloc_security(struct file *file)
356{
Casey Schaufler33bf60c2018-11-12 12:02:49 -0800357 struct file_security_struct *fsec = selinux_file(file);
David Howells275bb412008-11-14 10:39:19 +1100358 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
David Howells275bb412008-11-14 10:39:19 +1100360 fsec->sid = sid;
361 fsec->fown_sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 return 0;
364}
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366static int superblock_alloc_security(struct super_block *sb)
367{
368 struct superblock_security_struct *sbsec;
369
James Morris89d155e2005-10-30 14:59:21 -0800370 sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 if (!sbsec)
372 return -ENOMEM;
373
Eric Parisbc7e9822006-09-25 23:32:02 -0700374 mutex_init(&sbsec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 INIT_LIST_HEAD(&sbsec->isec_head);
376 spin_lock_init(&sbsec->isec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 sbsec->sb = sb;
378 sbsec->sid = SECINITSID_UNLABELED;
379 sbsec->def_sid = SECINITSID_FILE;
Eric Parisc312feb2006-07-10 04:43:53 -0700380 sbsec->mntpoint_sid = SECINITSID_UNLABELED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 sb->s_security = sbsec;
382
383 return 0;
384}
385
386static void superblock_free_security(struct super_block *sb)
387{
388 struct superblock_security_struct *sbsec = sb->s_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 sb->s_security = NULL;
390 kfree(sbsec);
391}
392
Al Virobd323652018-12-13 15:04:59 -0500393struct selinux_mnt_opts {
394 const char *fscontext, *context, *rootcontext, *defcontext;
395};
396
Al Viro204cc0c2018-12-13 13:41:47 -0500397static void selinux_free_mnt_opts(void *mnt_opts)
398{
Al Virobd323652018-12-13 15:04:59 -0500399 struct selinux_mnt_opts *opts = mnt_opts;
400 kfree(opts->fscontext);
401 kfree(opts->context);
402 kfree(opts->rootcontext);
403 kfree(opts->defcontext);
Al Viro204cc0c2018-12-13 13:41:47 -0500404 kfree(opts);
405}
406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407static inline int inode_doinit(struct inode *inode)
408{
409 return inode_doinit_with_dentry(inode, NULL);
410}
411
412enum {
Eric Paris31e87932007-09-19 17:19:12 -0400413 Opt_error = -1,
David Howells442155c2018-11-01 23:07:24 +0000414 Opt_context = 0,
415 Opt_defcontext = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 Opt_fscontext = 2,
David Howells442155c2018-11-01 23:07:24 +0000417 Opt_rootcontext = 3,
418 Opt_seclabel = 4,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419};
420
Al Viroda3d76a2018-12-17 10:14:16 -0500421#define A(s, has_arg) {#s, sizeof(#s) - 1, Opt_##s, has_arg}
Al Viro169d68efb2018-12-14 22:44:50 -0500422static struct {
423 const char *name;
424 int len;
425 int opt;
426 bool has_arg;
427} tokens[] = {
Al Viroda3d76a2018-12-17 10:14:16 -0500428 A(context, true),
429 A(fscontext, true),
430 A(defcontext, true),
431 A(rootcontext, true),
432 A(seclabel, false),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433};
Al Viro169d68efb2018-12-14 22:44:50 -0500434#undef A
435
436static int match_opt_prefix(char *s, int l, char **arg)
437{
438 int i;
439
440 for (i = 0; i < ARRAY_SIZE(tokens); i++) {
441 size_t len = tokens[i].len;
442 if (len > l || memcmp(s, tokens[i].name, len))
443 continue;
444 if (tokens[i].has_arg) {
445 if (len == l || s[len] != '=')
446 continue;
447 *arg = s + len + 1;
448 } else if (len != l)
449 continue;
450 return tokens[i].opt;
451 }
452 return Opt_error;
453}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455#define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n"
456
Eric Parisc312feb2006-07-10 04:43:53 -0700457static int may_context_mount_sb_relabel(u32 sid,
458 struct superblock_security_struct *sbsec,
David Howells275bb412008-11-14 10:39:19 +1100459 const struct cred *cred)
Eric Parisc312feb2006-07-10 04:43:53 -0700460{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -0700461 const struct task_security_struct *tsec = selinux_cred(cred);
Eric Parisc312feb2006-07-10 04:43:53 -0700462 int rc;
463
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500464 rc = avc_has_perm(&selinux_state,
465 tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
Eric Parisc312feb2006-07-10 04:43:53 -0700466 FILESYSTEM__RELABELFROM, NULL);
467 if (rc)
468 return rc;
469
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500470 rc = avc_has_perm(&selinux_state,
471 tsec->sid, sid, SECCLASS_FILESYSTEM,
Eric Parisc312feb2006-07-10 04:43:53 -0700472 FILESYSTEM__RELABELTO, NULL);
473 return rc;
474}
475
Eric Paris08089252006-07-10 04:43:55 -0700476static int may_context_mount_inode_relabel(u32 sid,
477 struct superblock_security_struct *sbsec,
David Howells275bb412008-11-14 10:39:19 +1100478 const struct cred *cred)
Eric Paris08089252006-07-10 04:43:55 -0700479{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -0700480 const struct task_security_struct *tsec = selinux_cred(cred);
Eric Paris08089252006-07-10 04:43:55 -0700481 int rc;
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500482 rc = avc_has_perm(&selinux_state,
483 tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
Eric Paris08089252006-07-10 04:43:55 -0700484 FILESYSTEM__RELABELFROM, NULL);
485 if (rc)
486 return rc;
487
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500488 rc = avc_has_perm(&selinux_state,
489 sid, sbsec->sid, SECCLASS_FILESYSTEM,
Eric Paris08089252006-07-10 04:43:55 -0700490 FILESYSTEM__ASSOCIATE, NULL);
491 return rc;
492}
493
Ondrej Mosnaceka83d6dd2018-12-21 21:18:52 +0100494static int selinux_is_genfs_special_handling(struct super_block *sb)
Eric Parisb43e7252012-10-10 14:27:35 -0400495{
Ondrej Mosnaceka83d6dd2018-12-21 21:18:52 +0100496 /* Special handling. Genfs but also in-core setxattr handler */
497 return !strcmp(sb->s_type->name, "sysfs") ||
Mark Salyzynd5f3a5f2015-02-04 11:34:30 -0500498 !strcmp(sb->s_type->name, "pstore") ||
499 !strcmp(sb->s_type->name, "debugfs") ||
Yongqin Liua2c7c6f2017-01-09 10:07:30 -0500500 !strcmp(sb->s_type->name, "tracefs") ||
Stephen Smalley2651225b2017-02-28 10:35:56 -0500501 !strcmp(sb->s_type->name, "rootfs") ||
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500502 (selinux_policycap_cgroupseclabel() &&
Stephen Smalley2651225b2017-02-28 10:35:56 -0500503 (!strcmp(sb->s_type->name, "cgroup") ||
504 !strcmp(sb->s_type->name, "cgroup2")));
Eric Parisb43e7252012-10-10 14:27:35 -0400505}
506
Ondrej Mosnaceka83d6dd2018-12-21 21:18:52 +0100507static int selinux_is_sblabel_mnt(struct super_block *sb)
508{
509 struct superblock_security_struct *sbsec = sb->s_security;
510
511 /*
512 * IMPORTANT: Double-check logic in this function when adding a new
513 * SECURITY_FS_USE_* definition!
514 */
515 BUILD_BUG_ON(SECURITY_FS_USE_MAX != 7);
516
517 switch (sbsec->behavior) {
518 case SECURITY_FS_USE_XATTR:
519 case SECURITY_FS_USE_TRANS:
520 case SECURITY_FS_USE_TASK:
521 case SECURITY_FS_USE_NATIVE:
522 return 1;
523
524 case SECURITY_FS_USE_GENFS:
525 return selinux_is_genfs_special_handling(sb);
526
527 /* Never allow relabeling on context mounts */
528 case SECURITY_FS_USE_MNTPOINT:
529 case SECURITY_FS_USE_NONE:
530 default:
531 return 0;
532 }
533}
534
Eric Parisc9180a52007-11-30 13:00:35 -0500535static int sb_finish_set_opts(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
537 struct superblock_security_struct *sbsec = sb->s_security;
538 struct dentry *root = sb->s_root;
David Howellsc6f493d2015-03-17 22:26:22 +0000539 struct inode *root_inode = d_backing_inode(root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 int rc = 0;
541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
543 /* Make sure that the xattr handler exists and that no
544 error other than -ENODATA is returned by getxattr on
545 the root directory. -ENODATA is ok, as this may be
546 the first boot of the SELinux kernel before we have
547 assigned xattr values to the filesystem. */
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200548 if (!(root_inode->i_opflags & IOP_XATTR)) {
peter enderborgc103a912018-06-12 10:09:03 +0200549 pr_warn("SELinux: (dev %s, type %s) has no "
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800550 "xattr support\n", sb->s_id, sb->s_type->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 rc = -EOPNOTSUPP;
552 goto out;
553 }
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200554
555 rc = __vfs_getxattr(root, root_inode, XATTR_NAME_SELINUX, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 if (rc < 0 && rc != -ENODATA) {
557 if (rc == -EOPNOTSUPP)
peter enderborgc103a912018-06-12 10:09:03 +0200558 pr_warn("SELinux: (dev %s, type "
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800559 "%s) has no security xattr handler\n",
560 sb->s_id, sb->s_type->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 else
peter enderborgc103a912018-06-12 10:09:03 +0200562 pr_warn("SELinux: (dev %s, type "
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800563 "%s) getxattr errno %d\n", sb->s_id,
564 sb->s_type->name, -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 goto out;
566 }
567 }
568
Eric Pariseadcabc2012-08-24 15:59:14 -0400569 sbsec->flags |= SE_SBINITIALIZED;
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400570
571 /*
572 * Explicitly set or clear SBLABEL_MNT. It's not sufficient to simply
573 * leave the flag untouched because sb_clone_mnt_opts might be handing
574 * us a superblock that needs the flag to be cleared.
575 */
Eric Parisb43e7252012-10-10 14:27:35 -0400576 if (selinux_is_sblabel_mnt(sb))
Eric Paris12f348b2012-10-09 10:56:25 -0400577 sbsec->flags |= SBLABEL_MNT;
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400578 else
579 sbsec->flags &= ~SBLABEL_MNT;
David P. Quigleyddd29ec2009-09-09 14:25:37 -0400580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 /* Initialize the root inode. */
Eric Parisc9180a52007-11-30 13:00:35 -0500582 rc = inode_doinit_with_dentry(root_inode, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 /* Initialize any other inodes associated with the superblock, e.g.
585 inodes created prior to initial policy load or inodes created
586 during get_sb by a pseudo filesystem that directly
587 populates itself. */
588 spin_lock(&sbsec->isec_lock);
Al Viro8d641242018-12-10 15:34:12 -0500589 while (!list_empty(&sbsec->isec_head)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 struct inode_security_struct *isec =
Al Viro8d641242018-12-10 15:34:12 -0500591 list_first_entry(&sbsec->isec_head,
Eric Parisc9180a52007-11-30 13:00:35 -0500592 struct inode_security_struct, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 struct inode *inode = isec->inode;
Stephen Smalley923190d2014-10-06 16:32:52 -0400594 list_del_init(&isec->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 spin_unlock(&sbsec->isec_lock);
596 inode = igrab(inode);
597 if (inode) {
Eric Parisc9180a52007-11-30 13:00:35 -0500598 if (!IS_PRIVATE(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 inode_doinit(inode);
600 iput(inode);
601 }
602 spin_lock(&sbsec->isec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 }
604 spin_unlock(&sbsec->isec_lock);
605out:
Eric Parisc9180a52007-11-30 13:00:35 -0500606 return rc;
607}
608
Eric Parisc9180a52007-11-30 13:00:35 -0500609static int bad_option(struct superblock_security_struct *sbsec, char flag,
610 u32 old_sid, u32 new_sid)
611{
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500612 char mnt_flags = sbsec->flags & SE_MNTMASK;
613
Eric Parisc9180a52007-11-30 13:00:35 -0500614 /* check if the old mount command had the same options */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500615 if (sbsec->flags & SE_SBINITIALIZED)
Eric Parisc9180a52007-11-30 13:00:35 -0500616 if (!(sbsec->flags & flag) ||
617 (old_sid != new_sid))
618 return 1;
619
620 /* check if we were passed the same options twice,
621 * aka someone passed context=a,context=b
622 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500623 if (!(sbsec->flags & SE_SBINITIALIZED))
624 if (mnt_flags & flag)
Eric Parisc9180a52007-11-30 13:00:35 -0500625 return 1;
626 return 0;
627}
Eric Parise0007522008-03-05 10:31:54 -0500628
Al Virobd323652018-12-13 15:04:59 -0500629static int parse_sid(struct super_block *sb, const char *s, u32 *sid)
630{
631 int rc = security_context_str_to_sid(&selinux_state, s,
632 sid, GFP_KERNEL);
633 if (rc)
634 pr_warn("SELinux: security_context_str_to_sid"
635 "(%s) failed for (dev %s, type %s) errno=%d\n",
636 s, sb->s_id, sb->s_type->name, rc);
637 return rc;
638}
639
Eric Parisc9180a52007-11-30 13:00:35 -0500640/*
641 * Allow filesystems with binary mount data to explicitly set mount point
642 * labeling information.
643 */
Eric Parise0007522008-03-05 10:31:54 -0500644static int selinux_set_mnt_opts(struct super_block *sb,
Al Viro204cc0c2018-12-13 13:41:47 -0500645 void *mnt_opts,
David Quigley649f6e72013-05-22 12:50:36 -0400646 unsigned long kern_flags,
647 unsigned long *set_kern_flags)
Eric Parisc9180a52007-11-30 13:00:35 -0500648{
David Howells275bb412008-11-14 10:39:19 +1100649 const struct cred *cred = current_cred();
Eric Parisc9180a52007-11-30 13:00:35 -0500650 struct superblock_security_struct *sbsec = sb->s_security;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500651 struct dentry *root = sbsec->sb->s_root;
Al Virobd323652018-12-13 15:04:59 -0500652 struct selinux_mnt_opts *opts = mnt_opts;
Paul Moore2c971652016-04-19 16:36:28 -0400653 struct inode_security_struct *root_isec;
Eric Parisc9180a52007-11-30 13:00:35 -0500654 u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
655 u32 defcontext_sid = 0;
Al Virobd323652018-12-13 15:04:59 -0500656 int rc = 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500657
658 mutex_lock(&sbsec->lock);
659
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500660 if (!selinux_state.initialized) {
Al Virobd323652018-12-13 15:04:59 -0500661 if (!opts) {
Eric Parisc9180a52007-11-30 13:00:35 -0500662 /* Defer initialization until selinux_complete_init,
663 after the initial policy is loaded and the security
664 server is ready to handle calls. */
Eric Parisc9180a52007-11-30 13:00:35 -0500665 goto out;
666 }
667 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +0200668 pr_warn("SELinux: Unable to set superblock options "
Eric Paris744ba352008-04-17 11:52:44 -0400669 "before the security server is initialized\n");
Eric Parisc9180a52007-11-30 13:00:35 -0500670 goto out;
671 }
David Quigley649f6e72013-05-22 12:50:36 -0400672 if (kern_flags && !set_kern_flags) {
673 /* Specifying internal flags without providing a place to
674 * place the results is not allowed */
675 rc = -EINVAL;
676 goto out;
677 }
Eric Parisc9180a52007-11-30 13:00:35 -0500678
679 /*
Eric Parise0007522008-03-05 10:31:54 -0500680 * Binary mount data FS will come through this function twice. Once
681 * from an explicit call and once from the generic calls from the vfs.
682 * Since the generic VFS calls will not contain any security mount data
683 * we need to skip the double mount verification.
684 *
685 * This does open a hole in which we will not notice if the first
686 * mount using this sb set explict options and a second mount using
687 * this sb does not set any security options. (The first options
688 * will be used for both mounts)
689 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500690 if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
Al Virobd323652018-12-13 15:04:59 -0500691 && !opts)
Eric Parisf5269712008-05-14 11:27:45 -0400692 goto out;
Eric Parise0007522008-03-05 10:31:54 -0500693
Paul Moore2c971652016-04-19 16:36:28 -0400694 root_isec = backing_inode_security_novalidate(root);
695
Eric Parise0007522008-03-05 10:31:54 -0500696 /*
Eric Parisc9180a52007-11-30 13:00:35 -0500697 * parse the mount options, check if they are valid sids.
698 * also check if someone is trying to mount the same sb more
699 * than once with different security options.
700 */
Al Virobd323652018-12-13 15:04:59 -0500701 if (opts) {
702 if (opts->fscontext) {
703 rc = parse_sid(sb, opts->fscontext, &fscontext_sid);
704 if (rc)
705 goto out;
Eric Parisc9180a52007-11-30 13:00:35 -0500706 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
707 fscontext_sid))
708 goto out_double_mount;
Eric Parisc9180a52007-11-30 13:00:35 -0500709 sbsec->flags |= FSCONTEXT_MNT;
Al Virobd323652018-12-13 15:04:59 -0500710 }
711 if (opts->context) {
712 rc = parse_sid(sb, opts->context, &context_sid);
713 if (rc)
714 goto out;
Eric Parisc9180a52007-11-30 13:00:35 -0500715 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
716 context_sid))
717 goto out_double_mount;
Eric Parisc9180a52007-11-30 13:00:35 -0500718 sbsec->flags |= CONTEXT_MNT;
Al Virobd323652018-12-13 15:04:59 -0500719 }
720 if (opts->rootcontext) {
721 rc = parse_sid(sb, opts->rootcontext, &rootcontext_sid);
722 if (rc)
723 goto out;
Eric Parisc9180a52007-11-30 13:00:35 -0500724 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
725 rootcontext_sid))
726 goto out_double_mount;
Eric Parisc9180a52007-11-30 13:00:35 -0500727 sbsec->flags |= ROOTCONTEXT_MNT;
Al Virobd323652018-12-13 15:04:59 -0500728 }
729 if (opts->defcontext) {
730 rc = parse_sid(sb, opts->defcontext, &defcontext_sid);
731 if (rc)
732 goto out;
Eric Parisc9180a52007-11-30 13:00:35 -0500733 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
734 defcontext_sid))
735 goto out_double_mount;
Eric Parisc9180a52007-11-30 13:00:35 -0500736 sbsec->flags |= DEFCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500737 }
738 }
739
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500740 if (sbsec->flags & SE_SBINITIALIZED) {
Eric Parisc9180a52007-11-30 13:00:35 -0500741 /* previously mounted with options, but not on this attempt? */
Al Virobd323652018-12-13 15:04:59 -0500742 if ((sbsec->flags & SE_MNTMASK) && !opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500743 goto out_double_mount;
744 rc = 0;
745 goto out;
746 }
747
James Morris089be432008-07-15 18:32:49 +1000748 if (strcmp(sb->s_type->name, "proc") == 0)
Stephen Smalley134509d2015-06-04 16:22:17 -0400749 sbsec->flags |= SE_SBPROC | SE_SBGENFS;
750
Stephen Smalley8e014722015-06-04 16:22:17 -0400751 if (!strcmp(sb->s_type->name, "debugfs") ||
Jeff Vander Stoep6a391182017-06-20 09:35:33 -0700752 !strcmp(sb->s_type->name, "tracefs") ||
Ondrej Mosnacekb7540262019-02-22 15:57:14 +0100753 !strcmp(sb->s_type->name, "pstore"))
754 sbsec->flags |= SE_SBGENFS;
755
756 if (!strcmp(sb->s_type->name, "sysfs") ||
Antonio Murdaca901ef842017-02-09 17:02:42 +0100757 !strcmp(sb->s_type->name, "cgroup") ||
758 !strcmp(sb->s_type->name, "cgroup2"))
Ondrej Mosnacekb7540262019-02-22 15:57:14 +0100759 sbsec->flags |= SE_SBGENFS | SE_SBGENFS_XATTR;
Eric Parisc9180a52007-11-30 13:00:35 -0500760
David Quigleyeb9ae682013-05-22 12:50:37 -0400761 if (!sbsec->behavior) {
762 /*
763 * Determine the labeling behavior to use for this
764 * filesystem type.
765 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500766 rc = security_fs_use(&selinux_state, sb);
David Quigleyeb9ae682013-05-22 12:50:37 -0400767 if (rc) {
peter enderborgc103a912018-06-12 10:09:03 +0200768 pr_warn("%s: security_fs_use(%s) returned %d\n",
David Quigleyeb9ae682013-05-22 12:50:37 -0400769 __func__, sb->s_type->name, rc);
770 goto out;
771 }
Eric Parisc9180a52007-11-30 13:00:35 -0500772 }
Seth Forsheeaad82892016-04-26 14:36:20 -0500773
774 /*
Stephen Smalley01593d32017-01-09 10:07:31 -0500775 * If this is a user namespace mount and the filesystem type is not
776 * explicitly whitelisted, then no contexts are allowed on the command
777 * line and security labels must be ignored.
Seth Forsheeaad82892016-04-26 14:36:20 -0500778 */
Stephen Smalley01593d32017-01-09 10:07:31 -0500779 if (sb->s_user_ns != &init_user_ns &&
780 strcmp(sb->s_type->name, "tmpfs") &&
781 strcmp(sb->s_type->name, "ramfs") &&
782 strcmp(sb->s_type->name, "devpts")) {
Seth Forsheeaad82892016-04-26 14:36:20 -0500783 if (context_sid || fscontext_sid || rootcontext_sid ||
784 defcontext_sid) {
785 rc = -EACCES;
786 goto out;
787 }
788 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
789 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500790 rc = security_transition_sid(&selinux_state,
791 current_sid(),
792 current_sid(),
Seth Forsheeaad82892016-04-26 14:36:20 -0500793 SECCLASS_FILE, NULL,
794 &sbsec->mntpoint_sid);
795 if (rc)
796 goto out;
797 }
798 goto out_set_opts;
799 }
800
Eric Parisc9180a52007-11-30 13:00:35 -0500801 /* sets the context of the superblock for the fs being mounted. */
802 if (fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100803 rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500804 if (rc)
805 goto out;
806
807 sbsec->sid = fscontext_sid;
808 }
809
810 /*
811 * Switch to using mount point labeling behavior.
812 * sets the label used on all file below the mountpoint, and will set
813 * the superblock context if not already set.
814 */
David Quigleyeb9ae682013-05-22 12:50:37 -0400815 if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !context_sid) {
816 sbsec->behavior = SECURITY_FS_USE_NATIVE;
817 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
818 }
819
Eric Parisc9180a52007-11-30 13:00:35 -0500820 if (context_sid) {
821 if (!fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100822 rc = may_context_mount_sb_relabel(context_sid, sbsec,
823 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500824 if (rc)
825 goto out;
826 sbsec->sid = context_sid;
827 } else {
David Howells275bb412008-11-14 10:39:19 +1100828 rc = may_context_mount_inode_relabel(context_sid, sbsec,
829 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500830 if (rc)
831 goto out;
832 }
833 if (!rootcontext_sid)
834 rootcontext_sid = context_sid;
835
836 sbsec->mntpoint_sid = context_sid;
837 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
838 }
839
840 if (rootcontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100841 rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec,
842 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500843 if (rc)
844 goto out;
845
846 root_isec->sid = rootcontext_sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -0500847 root_isec->initialized = LABEL_INITIALIZED;
Eric Parisc9180a52007-11-30 13:00:35 -0500848 }
849
850 if (defcontext_sid) {
David Quigleyeb9ae682013-05-22 12:50:37 -0400851 if (sbsec->behavior != SECURITY_FS_USE_XATTR &&
852 sbsec->behavior != SECURITY_FS_USE_NATIVE) {
Eric Parisc9180a52007-11-30 13:00:35 -0500853 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +0200854 pr_warn("SELinux: defcontext option is "
Eric Parisc9180a52007-11-30 13:00:35 -0500855 "invalid for this filesystem type\n");
856 goto out;
857 }
858
859 if (defcontext_sid != sbsec->def_sid) {
860 rc = may_context_mount_inode_relabel(defcontext_sid,
David Howells275bb412008-11-14 10:39:19 +1100861 sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500862 if (rc)
863 goto out;
864 }
865
866 sbsec->def_sid = defcontext_sid;
867 }
868
Seth Forsheeaad82892016-04-26 14:36:20 -0500869out_set_opts:
Eric Parisc9180a52007-11-30 13:00:35 -0500870 rc = sb_finish_set_opts(sb);
871out:
Eric Parisbc7e9822006-09-25 23:32:02 -0700872 mutex_unlock(&sbsec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 return rc;
Eric Parisc9180a52007-11-30 13:00:35 -0500874out_double_mount:
875 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +0200876 pr_warn("SELinux: mount invalid. Same superblock, different "
Al Virobd323652018-12-13 15:04:59 -0500877 "security settings for (dev %s, type %s)\n", sb->s_id,
878 sb->s_type->name);
Eric Parisc9180a52007-11-30 13:00:35 -0500879 goto out;
880}
881
Jeff Layton094f7b62013-04-01 08:14:24 -0400882static int selinux_cmp_sb_context(const struct super_block *oldsb,
883 const struct super_block *newsb)
884{
885 struct superblock_security_struct *old = oldsb->s_security;
886 struct superblock_security_struct *new = newsb->s_security;
887 char oldflags = old->flags & SE_MNTMASK;
888 char newflags = new->flags & SE_MNTMASK;
889
890 if (oldflags != newflags)
891 goto mismatch;
892 if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid)
893 goto mismatch;
894 if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid)
895 goto mismatch;
896 if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid)
897 goto mismatch;
898 if (oldflags & ROOTCONTEXT_MNT) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500899 struct inode_security_struct *oldroot = backing_inode_security(oldsb->s_root);
900 struct inode_security_struct *newroot = backing_inode_security(newsb->s_root);
Jeff Layton094f7b62013-04-01 08:14:24 -0400901 if (oldroot->sid != newroot->sid)
902 goto mismatch;
903 }
904 return 0;
905mismatch:
peter enderborgc103a912018-06-12 10:09:03 +0200906 pr_warn("SELinux: mount invalid. Same superblock, "
Jeff Layton094f7b62013-04-01 08:14:24 -0400907 "different security settings for (dev %s, "
908 "type %s)\n", newsb->s_id, newsb->s_type->name);
909 return -EBUSY;
910}
911
912static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400913 struct super_block *newsb,
914 unsigned long kern_flags,
915 unsigned long *set_kern_flags)
Eric Parisc9180a52007-11-30 13:00:35 -0500916{
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400917 int rc = 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500918 const struct superblock_security_struct *oldsbsec = oldsb->s_security;
919 struct superblock_security_struct *newsbsec = newsb->s_security;
920
921 int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT);
922 int set_context = (oldsbsec->flags & CONTEXT_MNT);
923 int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT);
924
Eric Paris0f5e6422008-04-21 16:24:11 -0400925 /*
926 * if the parent was able to be mounted it clearly had no special lsm
Al Viroe8c26252010-03-23 06:36:54 -0400927 * mount options. thus we can safely deal with this superblock later
Eric Paris0f5e6422008-04-21 16:24:11 -0400928 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500929 if (!selinux_state.initialized)
Jeff Layton094f7b62013-04-01 08:14:24 -0400930 return 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500931
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400932 /*
933 * Specifying internal flags without providing a place to
934 * place the results is not allowed.
935 */
936 if (kern_flags && !set_kern_flags)
937 return -EINVAL;
938
Eric Parisc9180a52007-11-30 13:00:35 -0500939 /* how can we clone if the old one wasn't set up?? */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500940 BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED));
Eric Parisc9180a52007-11-30 13:00:35 -0500941
Jeff Layton094f7b62013-04-01 08:14:24 -0400942 /* if fs is reusing a sb, make sure that the contexts match */
J. Bruce Fields3815a242019-03-05 16:17:58 -0500943 if (newsbsec->flags & SE_SBINITIALIZED) {
944 if ((kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context)
945 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
Jeff Layton094f7b62013-04-01 08:14:24 -0400946 return selinux_cmp_sb_context(oldsb, newsb);
J. Bruce Fields3815a242019-03-05 16:17:58 -0500947 }
Eric Paris5a552612008-04-09 14:08:35 -0400948
Eric Parisc9180a52007-11-30 13:00:35 -0500949 mutex_lock(&newsbsec->lock);
950
951 newsbsec->flags = oldsbsec->flags;
952
953 newsbsec->sid = oldsbsec->sid;
954 newsbsec->def_sid = oldsbsec->def_sid;
955 newsbsec->behavior = oldsbsec->behavior;
956
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400957 if (newsbsec->behavior == SECURITY_FS_USE_NATIVE &&
958 !(kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500959 rc = security_fs_use(&selinux_state, newsb);
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400960 if (rc)
961 goto out;
962 }
963
964 if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !set_context) {
965 newsbsec->behavior = SECURITY_FS_USE_NATIVE;
966 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
967 }
968
Eric Parisc9180a52007-11-30 13:00:35 -0500969 if (set_context) {
970 u32 sid = oldsbsec->mntpoint_sid;
971
972 if (!set_fscontext)
973 newsbsec->sid = sid;
974 if (!set_rootcontext) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500975 struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
Eric Parisc9180a52007-11-30 13:00:35 -0500976 newisec->sid = sid;
977 }
978 newsbsec->mntpoint_sid = sid;
979 }
980 if (set_rootcontext) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500981 const struct inode_security_struct *oldisec = backing_inode_security(oldsb->s_root);
982 struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
Eric Parisc9180a52007-11-30 13:00:35 -0500983
984 newisec->sid = oldisec->sid;
985 }
986
987 sb_finish_set_opts(newsb);
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400988out:
Eric Parisc9180a52007-11-30 13:00:35 -0500989 mutex_unlock(&newsbsec->lock);
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400990 return rc;
Eric Parisc9180a52007-11-30 13:00:35 -0500991}
992
Al Viroba641862018-12-14 20:28:15 -0500993static int selinux_add_opt(int token, const char *s, void **mnt_opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500994{
Al Viroba641862018-12-14 20:28:15 -0500995 struct selinux_mnt_opts *opts = *mnt_opts;
Eric Parisc9180a52007-11-30 13:00:35 -0500996
Al Viroda3d76a2018-12-17 10:14:16 -0500997 if (token == Opt_seclabel) /* eaten and completely ignored */
Al Viro169d68efb2018-12-14 22:44:50 -0500998 return 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500999
Al Viroba641862018-12-14 20:28:15 -05001000 if (!opts) {
1001 opts = kzalloc(sizeof(struct selinux_mnt_opts), GFP_KERNEL);
1002 if (!opts)
1003 return -ENOMEM;
1004 *mnt_opts = opts;
1005 }
1006 if (!s)
1007 return -ENOMEM;
1008 switch (token) {
1009 case Opt_context:
1010 if (opts->context || opts->defcontext)
1011 goto Einval;
1012 opts->context = s;
1013 break;
1014 case Opt_fscontext:
1015 if (opts->fscontext)
1016 goto Einval;
1017 opts->fscontext = s;
1018 break;
1019 case Opt_rootcontext:
1020 if (opts->rootcontext)
1021 goto Einval;
1022 opts->rootcontext = s;
1023 break;
1024 case Opt_defcontext:
1025 if (opts->context || opts->defcontext)
1026 goto Einval;
1027 opts->defcontext = s;
1028 break;
1029 }
1030 return 0;
1031Einval:
1032 pr_warn(SEL_MOUNT_FAIL_MSG);
Al Viroba641862018-12-14 20:28:15 -05001033 return -EINVAL;
1034}
Eric Parisc9180a52007-11-30 13:00:35 -05001035
Al Viro757cbe52018-12-14 23:42:21 -05001036static int selinux_add_mnt_opt(const char *option, const char *val, int len,
1037 void **mnt_opts)
Eric Parisc9180a52007-11-30 13:00:35 -05001038{
Al Viro757cbe52018-12-14 23:42:21 -05001039 int token = Opt_error;
1040 int rc, i;
Eric Parisc9180a52007-11-30 13:00:35 -05001041
Al Viro757cbe52018-12-14 23:42:21 -05001042 for (i = 0; i < ARRAY_SIZE(tokens); i++) {
1043 if (strcmp(option, tokens[i].name) == 0) {
1044 token = tokens[i].opt;
Eric Parisc9180a52007-11-30 13:00:35 -05001045 break;
Eric Parisc9180a52007-11-30 13:00:35 -05001046 }
1047 }
1048
Al Viro757cbe52018-12-14 23:42:21 -05001049 if (token == Opt_error)
1050 return -EINVAL;
Eric Parise0007522008-03-05 10:31:54 -05001051
Gen Zhange2e0e092019-06-12 21:28:21 +08001052 if (token != Opt_seclabel) {
Al Viro757cbe52018-12-14 23:42:21 -05001053 val = kmemdup_nul(val, len, GFP_KERNEL);
Gen Zhange2e0e092019-06-12 21:28:21 +08001054 if (!val) {
1055 rc = -ENOMEM;
1056 goto free_opt;
1057 }
1058 }
Al Viro757cbe52018-12-14 23:42:21 -05001059 rc = selinux_add_opt(token, val, mnt_opts);
1060 if (unlikely(rc)) {
1061 kfree(val);
Gen Zhange2e0e092019-06-12 21:28:21 +08001062 goto free_opt;
1063 }
1064 return rc;
1065
1066free_opt:
1067 if (*mnt_opts) {
1068 selinux_free_mnt_opts(*mnt_opts);
1069 *mnt_opts = NULL;
Al Viro757cbe52018-12-14 23:42:21 -05001070 }
1071 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Al Viroe3489f82018-12-13 00:24:36 -05001074static int show_sid(struct seq_file *m, u32 sid)
Eric Paris2069f452008-07-04 09:47:13 +10001075{
Al Viroe3489f82018-12-13 00:24:36 -05001076 char *context = NULL;
1077 u32 len;
1078 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Al Viroe3489f82018-12-13 00:24:36 -05001080 rc = security_sid_to_context(&selinux_state, sid,
1081 &context, &len);
1082 if (!rc) {
1083 bool has_comma = context && strchr(context, ',');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
David Howells442155c2018-11-01 23:07:24 +00001085 seq_putc(m, '=');
Eric Paris2069f452008-07-04 09:47:13 +10001086 if (has_comma)
1087 seq_putc(m, '\"');
Al Viroe3489f82018-12-13 00:24:36 -05001088 seq_escape(m, context, "\"\n\\");
Eric Paris2069f452008-07-04 09:47:13 +10001089 if (has_comma)
1090 seq_putc(m, '\"');
1091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 kfree(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 return rc;
1094}
Eric Paris2069f452008-07-04 09:47:13 +10001095
1096static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
1097{
Al Viroe3489f82018-12-13 00:24:36 -05001098 struct superblock_security_struct *sbsec = sb->s_security;
Eric Paris2069f452008-07-04 09:47:13 +10001099 int rc;
1100
Al Viroe3489f82018-12-13 00:24:36 -05001101 if (!(sbsec->flags & SE_SBINITIALIZED))
1102 return 0;
1103
1104 if (!selinux_state.initialized)
1105 return 0;
1106
1107 if (sbsec->flags & FSCONTEXT_MNT) {
1108 seq_putc(m, ',');
1109 seq_puts(m, FSCONTEXT_STR);
1110 rc = show_sid(m, sbsec->sid);
1111 if (rc)
1112 return rc;
Eric Paris383795c2008-07-29 17:07:26 -04001113 }
Al Viroe3489f82018-12-13 00:24:36 -05001114 if (sbsec->flags & CONTEXT_MNT) {
1115 seq_putc(m, ',');
1116 seq_puts(m, CONTEXT_STR);
1117 rc = show_sid(m, sbsec->mntpoint_sid);
1118 if (rc)
1119 return rc;
1120 }
1121 if (sbsec->flags & DEFCONTEXT_MNT) {
1122 seq_putc(m, ',');
1123 seq_puts(m, DEFCONTEXT_STR);
1124 rc = show_sid(m, sbsec->def_sid);
1125 if (rc)
1126 return rc;
1127 }
1128 if (sbsec->flags & ROOTCONTEXT_MNT) {
1129 struct dentry *root = sbsec->sb->s_root;
1130 struct inode_security_struct *isec = backing_inode_security(root);
1131 seq_putc(m, ',');
1132 seq_puts(m, ROOTCONTEXT_STR);
1133 rc = show_sid(m, isec->sid);
1134 if (rc)
1135 return rc;
1136 }
1137 if (sbsec->flags & SBLABEL_MNT) {
1138 seq_putc(m, ',');
David Howells442155c2018-11-01 23:07:24 +00001139 seq_puts(m, SECLABEL_STR);
Al Viroe3489f82018-12-13 00:24:36 -05001140 }
1141 return 0;
Eric Paris2069f452008-07-04 09:47:13 +10001142}
1143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144static inline u16 inode_mode_to_security_class(umode_t mode)
1145{
1146 switch (mode & S_IFMT) {
1147 case S_IFSOCK:
1148 return SECCLASS_SOCK_FILE;
1149 case S_IFLNK:
1150 return SECCLASS_LNK_FILE;
1151 case S_IFREG:
1152 return SECCLASS_FILE;
1153 case S_IFBLK:
1154 return SECCLASS_BLK_FILE;
1155 case S_IFDIR:
1156 return SECCLASS_DIR;
1157 case S_IFCHR:
1158 return SECCLASS_CHR_FILE;
1159 case S_IFIFO:
1160 return SECCLASS_FIFO_FILE;
1161
1162 }
1163
1164 return SECCLASS_FILE;
1165}
1166
James Morris13402582005-09-30 14:24:34 -04001167static inline int default_protocol_stream(int protocol)
1168{
1169 return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP);
1170}
1171
1172static inline int default_protocol_dgram(int protocol)
1173{
1174 return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
1175}
1176
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177static inline u16 socket_type_to_security_class(int family, int type, int protocol)
1178{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001179 int extsockclass = selinux_policycap_extsockclass();
Stephen Smalleyda69a532017-01-09 10:07:30 -05001180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 switch (family) {
1182 case PF_UNIX:
1183 switch (type) {
1184 case SOCK_STREAM:
1185 case SOCK_SEQPACKET:
1186 return SECCLASS_UNIX_STREAM_SOCKET;
1187 case SOCK_DGRAM:
Luis Ressel2a764b52017-07-25 15:13:41 -04001188 case SOCK_RAW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 return SECCLASS_UNIX_DGRAM_SOCKET;
1190 }
1191 break;
1192 case PF_INET:
1193 case PF_INET6:
1194 switch (type) {
1195 case SOCK_STREAM:
Stephen Smalleyda69a532017-01-09 10:07:30 -05001196 case SOCK_SEQPACKET:
James Morris13402582005-09-30 14:24:34 -04001197 if (default_protocol_stream(protocol))
1198 return SECCLASS_TCP_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001199 else if (extsockclass && protocol == IPPROTO_SCTP)
1200 return SECCLASS_SCTP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001201 else
1202 return SECCLASS_RAWIP_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 case SOCK_DGRAM:
James Morris13402582005-09-30 14:24:34 -04001204 if (default_protocol_dgram(protocol))
1205 return SECCLASS_UDP_SOCKET;
Stephen Smalleyef379792017-01-09 10:07:31 -05001206 else if (extsockclass && (protocol == IPPROTO_ICMP ||
1207 protocol == IPPROTO_ICMPV6))
Stephen Smalleyda69a532017-01-09 10:07:30 -05001208 return SECCLASS_ICMP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001209 else
1210 return SECCLASS_RAWIP_SOCKET;
James Morris2ee92d42006-11-13 16:09:01 -08001211 case SOCK_DCCP:
1212 return SECCLASS_DCCP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001213 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 return SECCLASS_RAWIP_SOCKET;
1215 }
1216 break;
1217 case PF_NETLINK:
1218 switch (protocol) {
1219 case NETLINK_ROUTE:
1220 return SECCLASS_NETLINK_ROUTE_SOCKET;
Pavel Emelyanov7f1fb602011-12-06 07:56:43 +00001221 case NETLINK_SOCK_DIAG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 return SECCLASS_NETLINK_TCPDIAG_SOCKET;
1223 case NETLINK_NFLOG:
1224 return SECCLASS_NETLINK_NFLOG_SOCKET;
1225 case NETLINK_XFRM:
1226 return SECCLASS_NETLINK_XFRM_SOCKET;
1227 case NETLINK_SELINUX:
1228 return SECCLASS_NETLINK_SELINUX_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001229 case NETLINK_ISCSI:
1230 return SECCLASS_NETLINK_ISCSI_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 case NETLINK_AUDIT:
1232 return SECCLASS_NETLINK_AUDIT_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001233 case NETLINK_FIB_LOOKUP:
1234 return SECCLASS_NETLINK_FIB_LOOKUP_SOCKET;
1235 case NETLINK_CONNECTOR:
1236 return SECCLASS_NETLINK_CONNECTOR_SOCKET;
1237 case NETLINK_NETFILTER:
1238 return SECCLASS_NETLINK_NETFILTER_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 case NETLINK_DNRTMSG:
1240 return SECCLASS_NETLINK_DNRT_SOCKET;
James Morris0c9b7942005-04-16 15:24:13 -07001241 case NETLINK_KOBJECT_UEVENT:
1242 return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001243 case NETLINK_GENERIC:
1244 return SECCLASS_NETLINK_GENERIC_SOCKET;
1245 case NETLINK_SCSITRANSPORT:
1246 return SECCLASS_NETLINK_SCSITRANSPORT_SOCKET;
1247 case NETLINK_RDMA:
1248 return SECCLASS_NETLINK_RDMA_SOCKET;
1249 case NETLINK_CRYPTO:
1250 return SECCLASS_NETLINK_CRYPTO_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 default:
1252 return SECCLASS_NETLINK_SOCKET;
1253 }
1254 case PF_PACKET:
1255 return SECCLASS_PACKET_SOCKET;
1256 case PF_KEY:
1257 return SECCLASS_KEY_SOCKET;
Christopher J. PeBenito3e3ff152006-06-09 00:25:03 -07001258 case PF_APPLETALK:
1259 return SECCLASS_APPLETALK_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 }
1261
Stephen Smalleyda69a532017-01-09 10:07:30 -05001262 if (extsockclass) {
1263 switch (family) {
1264 case PF_AX25:
1265 return SECCLASS_AX25_SOCKET;
1266 case PF_IPX:
1267 return SECCLASS_IPX_SOCKET;
1268 case PF_NETROM:
1269 return SECCLASS_NETROM_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001270 case PF_ATMPVC:
1271 return SECCLASS_ATMPVC_SOCKET;
1272 case PF_X25:
1273 return SECCLASS_X25_SOCKET;
1274 case PF_ROSE:
1275 return SECCLASS_ROSE_SOCKET;
1276 case PF_DECnet:
1277 return SECCLASS_DECNET_SOCKET;
1278 case PF_ATMSVC:
1279 return SECCLASS_ATMSVC_SOCKET;
1280 case PF_RDS:
1281 return SECCLASS_RDS_SOCKET;
1282 case PF_IRDA:
1283 return SECCLASS_IRDA_SOCKET;
1284 case PF_PPPOX:
1285 return SECCLASS_PPPOX_SOCKET;
1286 case PF_LLC:
1287 return SECCLASS_LLC_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001288 case PF_CAN:
1289 return SECCLASS_CAN_SOCKET;
1290 case PF_TIPC:
1291 return SECCLASS_TIPC_SOCKET;
1292 case PF_BLUETOOTH:
1293 return SECCLASS_BLUETOOTH_SOCKET;
1294 case PF_IUCV:
1295 return SECCLASS_IUCV_SOCKET;
1296 case PF_RXRPC:
1297 return SECCLASS_RXRPC_SOCKET;
1298 case PF_ISDN:
1299 return SECCLASS_ISDN_SOCKET;
1300 case PF_PHONET:
1301 return SECCLASS_PHONET_SOCKET;
1302 case PF_IEEE802154:
1303 return SECCLASS_IEEE802154_SOCKET;
1304 case PF_CAIF:
1305 return SECCLASS_CAIF_SOCKET;
1306 case PF_ALG:
1307 return SECCLASS_ALG_SOCKET;
1308 case PF_NFC:
1309 return SECCLASS_NFC_SOCKET;
1310 case PF_VSOCK:
1311 return SECCLASS_VSOCK_SOCKET;
1312 case PF_KCM:
1313 return SECCLASS_KCM_SOCKET;
1314 case PF_QIPCRTR:
1315 return SECCLASS_QIPCRTR_SOCKET;
Linus Torvalds3051bf32017-02-22 10:15:09 -08001316 case PF_SMC:
1317 return SECCLASS_SMC_SOCKET;
Björn Töpel68e8b842018-05-02 13:01:22 +02001318 case PF_XDP:
1319 return SECCLASS_XDP_SOCKET;
1320#if PF_MAX > 45
Stephen Smalleyda69a532017-01-09 10:07:30 -05001321#error New address family defined, please update this function.
1322#endif
1323 }
1324 }
1325
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 return SECCLASS_SOCKET;
1327}
1328
Stephen Smalley134509d2015-06-04 16:22:17 -04001329static int selinux_genfs_get_sid(struct dentry *dentry,
1330 u16 tclass,
1331 u16 flags,
1332 u32 *sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333{
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001334 int rc;
Al Virofc640052016-04-10 01:33:30 -04001335 struct super_block *sb = dentry->d_sb;
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001336 char *buffer, *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Eric Paris828dfe12008-04-17 13:17:49 -04001338 buffer = (char *)__get_free_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 if (!buffer)
1340 return -ENOMEM;
1341
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001342 path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
1343 if (IS_ERR(path))
1344 rc = PTR_ERR(path);
1345 else {
Stephen Smalley134509d2015-06-04 16:22:17 -04001346 if (flags & SE_SBPROC) {
1347 /* each process gets a /proc/PID/ entry. Strip off the
1348 * PID part to get a valid selinux labeling.
1349 * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
1350 while (path[1] >= '0' && path[1] <= '9') {
1351 path[1] = '/';
1352 path++;
1353 }
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001354 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001355 rc = security_genfs_sid(&selinux_state, sb->s_type->name,
1356 path, tclass, sid);
Stephen Smalley7bb185e2018-09-04 16:51:36 -04001357 if (rc == -ENOENT) {
1358 /* No match in policy, mark as unlabeled. */
1359 *sid = SECINITSID_UNLABELED;
1360 rc = 0;
1361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 free_page((unsigned long)buffer);
1364 return rc;
1365}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Ondrej Mosnacekb7540262019-02-22 15:57:14 +01001367static int inode_doinit_use_xattr(struct inode *inode, struct dentry *dentry,
1368 u32 def_sid, u32 *sid)
1369{
1370#define INITCONTEXTLEN 255
1371 char *context;
1372 unsigned int len;
1373 int rc;
1374
1375 len = INITCONTEXTLEN;
1376 context = kmalloc(len + 1, GFP_NOFS);
1377 if (!context)
1378 return -ENOMEM;
1379
1380 context[len] = '\0';
1381 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
1382 if (rc == -ERANGE) {
1383 kfree(context);
1384
1385 /* Need a larger buffer. Query for the right size. */
1386 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, NULL, 0);
1387 if (rc < 0)
1388 return rc;
1389
1390 len = rc;
1391 context = kmalloc(len + 1, GFP_NOFS);
1392 if (!context)
1393 return -ENOMEM;
1394
1395 context[len] = '\0';
1396 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX,
1397 context, len);
1398 }
1399 if (rc < 0) {
1400 kfree(context);
1401 if (rc != -ENODATA) {
1402 pr_warn("SELinux: %s: getxattr returned %d for dev=%s ino=%ld\n",
1403 __func__, -rc, inode->i_sb->s_id, inode->i_ino);
1404 return rc;
1405 }
1406 *sid = def_sid;
1407 return 0;
1408 }
1409
1410 rc = security_context_to_sid_default(&selinux_state, context, rc, sid,
1411 def_sid, GFP_NOFS);
1412 if (rc) {
1413 char *dev = inode->i_sb->s_id;
1414 unsigned long ino = inode->i_ino;
1415
1416 if (rc == -EINVAL) {
1417 pr_notice_ratelimited("SELinux: inode=%lu on dev=%s was found to have an invalid context=%s. This indicates you may need to relabel the inode or the filesystem in question.\n",
1418 ino, dev, context);
1419 } else {
1420 pr_warn("SELinux: %s: context_to_sid(%s) returned %d for dev=%s ino=%ld\n",
1421 __func__, context, -rc, dev, ino);
1422 }
1423 }
1424 kfree(context);
1425 return 0;
1426}
1427
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428/* The inode's security attributes must be initialized before first use. */
1429static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
1430{
1431 struct superblock_security_struct *sbsec = NULL;
Casey Schaufler80788c22018-09-21 17:19:11 -07001432 struct inode_security_struct *isec = selinux_inode(inode);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001433 u32 task_sid, sid = 0;
1434 u16 sclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05001438 if (isec->initialized == LABEL_INITIALIZED)
Andreas Gruenbacher13457d02016-11-10 22:18:29 +01001439 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001441 spin_lock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05001442 if (isec->initialized == LABEL_INITIALIZED)
Eric Paris23970742006-09-25 23:32:01 -07001443 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Andreas Gruenbacher13457d02016-11-10 22:18:29 +01001445 if (isec->sclass == SECCLASS_FILE)
1446 isec->sclass = inode_mode_to_security_class(inode->i_mode);
1447
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 sbsec = inode->i_sb->s_security;
David P. Quigley0d90a7e2009-01-16 09:22:02 -05001449 if (!(sbsec->flags & SE_SBINITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 /* Defer initialization until selinux_complete_init,
1451 after the initial policy is loaded and the security
1452 server is ready to handle calls. */
1453 spin_lock(&sbsec->isec_lock);
1454 if (list_empty(&isec->list))
1455 list_add(&isec->list, &sbsec->isec_head);
1456 spin_unlock(&sbsec->isec_lock);
Eric Paris23970742006-09-25 23:32:01 -07001457 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 }
1459
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001460 sclass = isec->sclass;
1461 task_sid = isec->task_sid;
1462 sid = isec->sid;
1463 isec->initialized = LABEL_PENDING;
1464 spin_unlock(&isec->lock);
1465
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 switch (sbsec->behavior) {
David Quigleyeb9ae682013-05-22 12:50:37 -04001467 case SECURITY_FS_USE_NATIVE:
1468 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 case SECURITY_FS_USE_XATTR:
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001470 if (!(inode->i_opflags & IOP_XATTR)) {
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001471 sid = sbsec->def_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 break;
1473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 /* Need a dentry, since the xattr API requires one.
1475 Life would be simpler if we could just pass the inode. */
1476 if (opt_dentry) {
1477 /* Called from d_instantiate or d_splice_alias. */
1478 dentry = dget(opt_dentry);
1479 } else {
Al Virob1271252018-04-25 10:28:38 -04001480 /*
1481 * Called from selinux_complete_init, try to find a dentry.
1482 * Some filesystems really want a connected one, so try
1483 * that first. We could split SECURITY_FS_USE_XATTR in
1484 * two, depending upon that...
1485 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 dentry = d_find_alias(inode);
Al Virob1271252018-04-25 10:28:38 -04001487 if (!dentry)
1488 dentry = d_find_any_alias(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 }
1490 if (!dentry) {
Eric Parisdf7f54c2009-03-09 14:35:58 -04001491 /*
1492 * this is can be hit on boot when a file is accessed
1493 * before the policy is loaded. When we load policy we
1494 * may find inodes that have no dentry on the
1495 * sbsec->isec_head list. No reason to complain as these
1496 * will get fixed up the next time we go through
1497 * inode_doinit with a dentry, before these inodes could
1498 * be used again by userspace.
1499 */
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001500 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 }
1502
Ondrej Mosnacekb7540262019-02-22 15:57:14 +01001503 rc = inode_doinit_use_xattr(inode, dentry, sbsec->def_sid,
1504 &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 dput(dentry);
Ondrej Mosnacekb7540262019-02-22 15:57:14 +01001506 if (rc)
1507 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 break;
1509 case SECURITY_FS_USE_TASK:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001510 sid = task_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 break;
1512 case SECURITY_FS_USE_TRANS:
1513 /* Default to the fs SID. */
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001514 sid = sbsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
1516 /* Try to obtain a transition SID. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001517 rc = security_transition_sid(&selinux_state, task_sid, sid,
1518 sclass, NULL, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 if (rc)
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001520 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 break;
Eric Parisc312feb2006-07-10 04:43:53 -07001522 case SECURITY_FS_USE_MNTPOINT:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001523 sid = sbsec->mntpoint_sid;
Eric Parisc312feb2006-07-10 04:43:53 -07001524 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 default:
Eric Parisc312feb2006-07-10 04:43:53 -07001526 /* Default to the fs superblock SID. */
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001527 sid = sbsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Stephen Smalley134509d2015-06-04 16:22:17 -04001529 if ((sbsec->flags & SE_SBGENFS) && !S_ISLNK(inode->i_mode)) {
Paul Mooref64410e2014-03-19 16:46:18 -04001530 /* We must have a dentry to determine the label on
1531 * procfs inodes */
Al Virob1271252018-04-25 10:28:38 -04001532 if (opt_dentry) {
Paul Mooref64410e2014-03-19 16:46:18 -04001533 /* Called from d_instantiate or
1534 * d_splice_alias. */
1535 dentry = dget(opt_dentry);
Al Virob1271252018-04-25 10:28:38 -04001536 } else {
Paul Mooref64410e2014-03-19 16:46:18 -04001537 /* Called from selinux_complete_init, try to
Al Virob1271252018-04-25 10:28:38 -04001538 * find a dentry. Some filesystems really want
1539 * a connected one, so try that first.
1540 */
Paul Mooref64410e2014-03-19 16:46:18 -04001541 dentry = d_find_alias(inode);
Al Virob1271252018-04-25 10:28:38 -04001542 if (!dentry)
1543 dentry = d_find_any_alias(inode);
1544 }
Paul Mooref64410e2014-03-19 16:46:18 -04001545 /*
1546 * This can be hit on boot when a file is accessed
1547 * before the policy is loaded. When we load policy we
1548 * may find inodes that have no dentry on the
1549 * sbsec->isec_head list. No reason to complain as
1550 * these will get fixed up the next time we go through
1551 * inode_doinit() with a dentry, before these inodes
1552 * could be used again by userspace.
1553 */
1554 if (!dentry)
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001555 goto out;
1556 rc = selinux_genfs_get_sid(dentry, sclass,
Stephen Smalley134509d2015-06-04 16:22:17 -04001557 sbsec->flags, &sid);
Ondrej Mosnacekb7540262019-02-22 15:57:14 +01001558 if (rc) {
1559 dput(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001560 goto out;
Ondrej Mosnacekb7540262019-02-22 15:57:14 +01001561 }
1562
1563 if ((sbsec->flags & SE_SBGENFS_XATTR) &&
1564 (inode->i_opflags & IOP_XATTR)) {
1565 rc = inode_doinit_use_xattr(inode, dentry,
1566 sid, &sid);
1567 if (rc) {
1568 dput(dentry);
1569 goto out;
1570 }
1571 }
1572 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 }
1574 break;
1575 }
1576
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001577out:
1578 spin_lock(&isec->lock);
1579 if (isec->initialized == LABEL_PENDING) {
1580 if (!sid || rc) {
1581 isec->initialized = LABEL_INVALID;
1582 goto out_unlock;
1583 }
1584
1585 isec->initialized = LABEL_INITIALIZED;
1586 isec->sid = sid;
1587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
Eric Paris23970742006-09-25 23:32:01 -07001589out_unlock:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001590 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 return rc;
1592}
1593
1594/* Convert a Linux signal to an access vector. */
1595static inline u32 signal_to_av(int sig)
1596{
1597 u32 perm = 0;
1598
1599 switch (sig) {
1600 case SIGCHLD:
1601 /* Commonly granted from child to parent. */
1602 perm = PROCESS__SIGCHLD;
1603 break;
1604 case SIGKILL:
1605 /* Cannot be caught or ignored */
1606 perm = PROCESS__SIGKILL;
1607 break;
1608 case SIGSTOP:
1609 /* Cannot be caught or ignored */
1610 perm = PROCESS__SIGSTOP;
1611 break;
1612 default:
1613 /* All other signals. */
1614 perm = PROCESS__SIGNAL;
1615 break;
1616 }
1617
1618 return perm;
1619}
1620
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001621#if CAP_LAST_CAP > 63
1622#error Fix SELinux to handle capabilities > 63.
1623#endif
1624
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625/* Check whether a task is allowed to use a capability. */
Eric Paris6a9de492012-01-03 12:25:14 -05001626static int cred_has_capability(const struct cred *cred,
Micah Mortonc1a85a02019-01-07 16:10:53 -08001627 int cap, unsigned int opts, bool initns)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628{
Thomas Liu2bf49692009-07-14 12:14:09 -04001629 struct common_audit_data ad;
Eric Paris06112162008-11-11 22:02:50 +11001630 struct av_decision avd;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001631 u16 sclass;
David Howells3699c532009-01-06 22:27:01 +00001632 u32 sid = cred_sid(cred);
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001633 u32 av = CAP_TO_MASK(cap);
Eric Paris06112162008-11-11 22:02:50 +11001634 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
Eric Paris50c205f2012-04-04 15:01:43 -04001636 ad.type = LSM_AUDIT_DATA_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 ad.u.cap = cap;
1638
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001639 switch (CAP_TO_INDEX(cap)) {
1640 case 0:
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04001641 sclass = initns ? SECCLASS_CAPABILITY : SECCLASS_CAP_USERNS;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001642 break;
1643 case 1:
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04001644 sclass = initns ? SECCLASS_CAPABILITY2 : SECCLASS_CAP2_USERNS;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001645 break;
1646 default:
peter enderborgc103a912018-06-12 10:09:03 +02001647 pr_err("SELinux: out of range capability %d\n", cap);
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001648 BUG();
Eric Parisa35c6c832011-04-20 10:21:28 -04001649 return -EINVAL;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001650 }
Eric Paris06112162008-11-11 22:02:50 +11001651
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001652 rc = avc_has_perm_noaudit(&selinux_state,
1653 sid, sid, sclass, av, 0, &avd);
Micah Mortonc1a85a02019-01-07 16:10:53 -08001654 if (!(opts & CAP_OPT_NOAUDIT)) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001655 int rc2 = avc_audit(&selinux_state,
1656 sid, sid, sclass, av, &avd, rc, &ad, 0);
Eric Paris9ade0cf2011-04-25 16:26:29 -04001657 if (rc2)
1658 return rc2;
1659 }
Eric Paris06112162008-11-11 22:02:50 +11001660 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661}
1662
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663/* Check whether a task has a particular permission to an inode.
1664 The 'adp' parameter is optional and allows other audit
1665 data to be passed (e.g. the dentry). */
David Howells88e67f32008-11-14 10:39:21 +11001666static int inode_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 struct inode *inode,
1668 u32 perms,
Linus Torvalds19e49832013-10-04 12:54:11 -07001669 struct common_audit_data *adp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 struct inode_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +11001672 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
David Howellse0e81732009-09-02 09:13:40 +01001674 validate_creds(cred);
1675
Eric Paris828dfe12008-04-17 13:17:49 -04001676 if (unlikely(IS_PRIVATE(inode)))
Stephen Smalleybbaca6c2007-02-14 00:34:16 -08001677 return 0;
1678
David Howells88e67f32008-11-14 10:39:21 +11001679 sid = cred_sid(cred);
Casey Schaufler80788c22018-09-21 17:19:11 -07001680 isec = selinux_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001682 return avc_has_perm(&selinux_state,
1683 sid, isec->sid, isec->sclass, perms, adp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684}
1685
1686/* Same as inode_has_perm, but pass explicit audit data containing
1687 the dentry to help the auditing code to more easily generate the
1688 pathname if needed. */
David Howells88e67f32008-11-14 10:39:21 +11001689static inline int dentry_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 struct dentry *dentry,
1691 u32 av)
1692{
David Howellsc6f493d2015-03-17 22:26:22 +00001693 struct inode *inode = d_backing_inode(dentry);
Thomas Liu2bf49692009-07-14 12:14:09 -04001694 struct common_audit_data ad;
David Howells88e67f32008-11-14 10:39:21 +11001695
Eric Paris50c205f2012-04-04 15:01:43 -04001696 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Paris2875fa02011-04-28 16:04:24 -04001697 ad.u.dentry = dentry;
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05001698 __inode_security_revalidate(inode, dentry, true);
Linus Torvalds19e49832013-10-04 12:54:11 -07001699 return inode_has_perm(cred, inode, av, &ad);
Eric Paris2875fa02011-04-28 16:04:24 -04001700}
1701
1702/* Same as inode_has_perm, but pass explicit audit data containing
1703 the path to help the auditing code to more easily generate the
1704 pathname if needed. */
1705static inline int path_has_perm(const struct cred *cred,
Al Viro3f7036a2015-03-08 19:28:30 -04001706 const struct path *path,
Eric Paris2875fa02011-04-28 16:04:24 -04001707 u32 av)
1708{
David Howellsc6f493d2015-03-17 22:26:22 +00001709 struct inode *inode = d_backing_inode(path->dentry);
Eric Paris2875fa02011-04-28 16:04:24 -04001710 struct common_audit_data ad;
1711
Eric Paris50c205f2012-04-04 15:01:43 -04001712 ad.type = LSM_AUDIT_DATA_PATH;
Eric Paris2875fa02011-04-28 16:04:24 -04001713 ad.u.path = *path;
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05001714 __inode_security_revalidate(inode, path->dentry, true);
Linus Torvalds19e49832013-10-04 12:54:11 -07001715 return inode_has_perm(cred, inode, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716}
1717
David Howells13f8e982013-06-13 23:37:55 +01001718/* Same as path_has_perm, but uses the inode from the file struct. */
1719static inline int file_path_has_perm(const struct cred *cred,
1720 struct file *file,
1721 u32 av)
1722{
1723 struct common_audit_data ad;
1724
Vivek Goyal43af5de2016-09-09 11:37:49 -04001725 ad.type = LSM_AUDIT_DATA_FILE;
1726 ad.u.file = file;
Linus Torvalds19e49832013-10-04 12:54:11 -07001727 return inode_has_perm(cred, file_inode(file), av, &ad);
David Howells13f8e982013-06-13 23:37:55 +01001728}
1729
Chenbo Fengf66e4482017-10-18 13:00:26 -07001730#ifdef CONFIG_BPF_SYSCALL
1731static int bpf_fd_pass(struct file *file, u32 sid);
1732#endif
1733
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734/* Check whether a task can use an open file descriptor to
1735 access an inode in a given way. Check access to the
1736 descriptor itself, and then use dentry_has_perm to
1737 check a particular permission to the file.
1738 Access to the descriptor is implicitly granted if it
1739 has the same SID as the process. If av is zero, then
1740 access to the file is not checked, e.g. for cases
1741 where only the descriptor is affected like seek. */
David Howells88e67f32008-11-14 10:39:21 +11001742static int file_has_perm(const struct cred *cred,
1743 struct file *file,
1744 u32 av)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745{
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07001746 struct file_security_struct *fsec = selinux_file(file);
Al Viro496ad9a2013-01-23 17:07:38 -05001747 struct inode *inode = file_inode(file);
Thomas Liu2bf49692009-07-14 12:14:09 -04001748 struct common_audit_data ad;
David Howells88e67f32008-11-14 10:39:21 +11001749 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 int rc;
1751
Vivek Goyal43af5de2016-09-09 11:37:49 -04001752 ad.type = LSM_AUDIT_DATA_FILE;
1753 ad.u.file = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
David Howells275bb412008-11-14 10:39:19 +11001755 if (sid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001756 rc = avc_has_perm(&selinux_state,
1757 sid, fsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 SECCLASS_FD,
1759 FD__USE,
1760 &ad);
1761 if (rc)
David Howells88e67f32008-11-14 10:39:21 +11001762 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 }
1764
Chenbo Fengf66e4482017-10-18 13:00:26 -07001765#ifdef CONFIG_BPF_SYSCALL
1766 rc = bpf_fd_pass(file, cred_sid(cred));
1767 if (rc)
1768 return rc;
1769#endif
1770
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 /* av is zero if only checking access to the descriptor. */
David Howells88e67f32008-11-14 10:39:21 +11001772 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 if (av)
Linus Torvalds19e49832013-10-04 12:54:11 -07001774 rc = inode_has_perm(cred, inode, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
David Howells88e67f32008-11-14 10:39:21 +11001776out:
1777 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778}
1779
David Howellsc3c188b2015-07-10 17:19:58 -04001780/*
1781 * Determine the label for an inode that might be unioned.
1782 */
Vivek Goyalc957f6d2016-07-13 10:44:51 -04001783static int
1784selinux_determine_inode_label(const struct task_security_struct *tsec,
1785 struct inode *dir,
1786 const struct qstr *name, u16 tclass,
1787 u32 *_new_isid)
David Howellsc3c188b2015-07-10 17:19:58 -04001788{
1789 const struct superblock_security_struct *sbsec = dir->i_sb->s_security;
David Howellsc3c188b2015-07-10 17:19:58 -04001790
1791 if ((sbsec->flags & SE_SBINITIALIZED) &&
1792 (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)) {
1793 *_new_isid = sbsec->mntpoint_sid;
1794 } else if ((sbsec->flags & SBLABEL_MNT) &&
1795 tsec->create_sid) {
1796 *_new_isid = tsec->create_sid;
1797 } else {
Paul Moore20cdef82016-04-04 14:14:42 -04001798 const struct inode_security_struct *dsec = inode_security(dir);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001799 return security_transition_sid(&selinux_state, tsec->sid,
1800 dsec->sid, tclass,
David Howellsc3c188b2015-07-10 17:19:58 -04001801 name, _new_isid);
1802 }
1803
1804 return 0;
1805}
1806
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807/* Check whether a task can create a file. */
1808static int may_create(struct inode *dir,
1809 struct dentry *dentry,
1810 u16 tclass)
1811{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07001812 const struct task_security_struct *tsec = selinux_cred(current_cred());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 struct inode_security_struct *dsec;
1814 struct superblock_security_struct *sbsec;
David Howells275bb412008-11-14 10:39:19 +11001815 u32 sid, newsid;
Thomas Liu2bf49692009-07-14 12:14:09 -04001816 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 int rc;
1818
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001819 dsec = inode_security(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 sbsec = dir->i_sb->s_security;
1821
David Howells275bb412008-11-14 10:39:19 +11001822 sid = tsec->sid;
David Howells275bb412008-11-14 10:39:19 +11001823
Eric Paris50c205f2012-04-04 15:01:43 -04001824 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04001825 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001827 rc = avc_has_perm(&selinux_state,
1828 sid, dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 DIR__ADD_NAME | DIR__SEARCH,
1830 &ad);
1831 if (rc)
1832 return rc;
1833
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07001834 rc = selinux_determine_inode_label(selinux_cred(current_cred()), dir,
Vivek Goyalc957f6d2016-07-13 10:44:51 -04001835 &dentry->d_name, tclass, &newsid);
David Howellsc3c188b2015-07-10 17:19:58 -04001836 if (rc)
1837 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001839 rc = avc_has_perm(&selinux_state,
1840 sid, newsid, tclass, FILE__CREATE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 if (rc)
1842 return rc;
1843
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001844 return avc_has_perm(&selinux_state,
1845 newsid, sbsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 SECCLASS_FILESYSTEM,
1847 FILESYSTEM__ASSOCIATE, &ad);
1848}
1849
Eric Paris828dfe12008-04-17 13:17:49 -04001850#define MAY_LINK 0
1851#define MAY_UNLINK 1
1852#define MAY_RMDIR 2
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
1854/* Check whether a task can link, unlink, or rmdir a file/directory. */
1855static int may_link(struct inode *dir,
1856 struct dentry *dentry,
1857 int kind)
1858
1859{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 struct inode_security_struct *dsec, *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001861 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11001862 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 u32 av;
1864 int rc;
1865
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001866 dsec = inode_security(dir);
1867 isec = backing_inode_security(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
Eric Paris50c205f2012-04-04 15:01:43 -04001869 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04001870 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
1872 av = DIR__SEARCH;
1873 av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001874 rc = avc_has_perm(&selinux_state,
1875 sid, dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 if (rc)
1877 return rc;
1878
1879 switch (kind) {
1880 case MAY_LINK:
1881 av = FILE__LINK;
1882 break;
1883 case MAY_UNLINK:
1884 av = FILE__UNLINK;
1885 break;
1886 case MAY_RMDIR:
1887 av = DIR__RMDIR;
1888 break;
1889 default:
peter enderborgc103a912018-06-12 10:09:03 +02001890 pr_warn("SELinux: %s: unrecognized kind %d\n",
Eric Paris744ba352008-04-17 11:52:44 -04001891 __func__, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 return 0;
1893 }
1894
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001895 rc = avc_has_perm(&selinux_state,
1896 sid, isec->sid, isec->sclass, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 return rc;
1898}
1899
1900static inline int may_rename(struct inode *old_dir,
1901 struct dentry *old_dentry,
1902 struct inode *new_dir,
1903 struct dentry *new_dentry)
1904{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001906 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11001907 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 u32 av;
1909 int old_is_dir, new_is_dir;
1910 int rc;
1911
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001912 old_dsec = inode_security(old_dir);
1913 old_isec = backing_inode_security(old_dentry);
David Howellse36cb0b2015-01-29 12:02:35 +00001914 old_is_dir = d_is_dir(old_dentry);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001915 new_dsec = inode_security(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
Eric Paris50c205f2012-04-04 15:01:43 -04001917 ad.type = LSM_AUDIT_DATA_DENTRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
Eric Parisa2694342011-04-25 13:10:27 -04001919 ad.u.dentry = old_dentry;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001920 rc = avc_has_perm(&selinux_state,
1921 sid, old_dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 DIR__REMOVE_NAME | DIR__SEARCH, &ad);
1923 if (rc)
1924 return rc;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001925 rc = avc_has_perm(&selinux_state,
1926 sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 old_isec->sclass, FILE__RENAME, &ad);
1928 if (rc)
1929 return rc;
1930 if (old_is_dir && new_dir != old_dir) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001931 rc = avc_has_perm(&selinux_state,
1932 sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 old_isec->sclass, DIR__REPARENT, &ad);
1934 if (rc)
1935 return rc;
1936 }
1937
Eric Parisa2694342011-04-25 13:10:27 -04001938 ad.u.dentry = new_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 av = DIR__ADD_NAME | DIR__SEARCH;
David Howells2c616d42015-01-29 12:02:33 +00001940 if (d_is_positive(new_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 av |= DIR__REMOVE_NAME;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001942 rc = avc_has_perm(&selinux_state,
1943 sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 if (rc)
1945 return rc;
David Howells2c616d42015-01-29 12:02:33 +00001946 if (d_is_positive(new_dentry)) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001947 new_isec = backing_inode_security(new_dentry);
David Howellse36cb0b2015-01-29 12:02:35 +00001948 new_is_dir = d_is_dir(new_dentry);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001949 rc = avc_has_perm(&selinux_state,
1950 sid, new_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 new_isec->sclass,
1952 (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
1953 if (rc)
1954 return rc;
1955 }
1956
1957 return 0;
1958}
1959
1960/* Check whether a task can perform a filesystem operation. */
David Howells88e67f32008-11-14 10:39:21 +11001961static int superblock_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 struct super_block *sb,
1963 u32 perms,
Thomas Liu2bf49692009-07-14 12:14:09 -04001964 struct common_audit_data *ad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 struct superblock_security_struct *sbsec;
David Howells88e67f32008-11-14 10:39:21 +11001967 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 sbsec = sb->s_security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001970 return avc_has_perm(&selinux_state,
1971 sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972}
1973
1974/* Convert a Linux mode and permission mask to an access vector. */
1975static inline u32 file_mask_to_av(int mode, int mask)
1976{
1977 u32 av = 0;
1978
Al Virodba19c62011-07-25 20:49:29 -04001979 if (!S_ISDIR(mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 if (mask & MAY_EXEC)
1981 av |= FILE__EXECUTE;
1982 if (mask & MAY_READ)
1983 av |= FILE__READ;
1984
1985 if (mask & MAY_APPEND)
1986 av |= FILE__APPEND;
1987 else if (mask & MAY_WRITE)
1988 av |= FILE__WRITE;
1989
1990 } else {
1991 if (mask & MAY_EXEC)
1992 av |= DIR__SEARCH;
1993 if (mask & MAY_WRITE)
1994 av |= DIR__WRITE;
1995 if (mask & MAY_READ)
1996 av |= DIR__READ;
1997 }
1998
1999 return av;
2000}
2001
2002/* Convert a Linux file to an access vector. */
2003static inline u32 file_to_av(struct file *file)
2004{
2005 u32 av = 0;
2006
2007 if (file->f_mode & FMODE_READ)
2008 av |= FILE__READ;
2009 if (file->f_mode & FMODE_WRITE) {
2010 if (file->f_flags & O_APPEND)
2011 av |= FILE__APPEND;
2012 else
2013 av |= FILE__WRITE;
2014 }
Stephen Smalley0794c662008-03-17 08:55:18 -04002015 if (!av) {
2016 /*
2017 * Special file opened with flags 3 for ioctl-only use.
2018 */
2019 av = FILE__IOCTL;
2020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
2022 return av;
2023}
2024
Eric Paris8b6a5a32008-10-29 17:06:46 -04002025/*
2026 * Convert a file to an access vector and include the correct open
2027 * open permission.
2028 */
2029static inline u32 open_file_to_av(struct file *file)
2030{
2031 u32 av = file_to_av(file);
Stephen Smalleyccb54472017-05-12 12:41:24 -04002032 struct inode *inode = file_inode(file);
Eric Paris8b6a5a32008-10-29 17:06:46 -04002033
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002034 if (selinux_policycap_openperm() &&
2035 inode->i_sb->s_magic != SOCKFS_MAGIC)
Eric Paris49b7b8d2010-07-23 11:44:09 -04002036 av |= FILE__OPEN;
2037
Eric Paris8b6a5a32008-10-29 17:06:46 -04002038 return av;
2039}
2040
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041/* Hook functions begin here. */
2042
Stephen Smalley79af7302015-01-21 10:54:10 -05002043static int selinux_binder_set_context_mgr(struct task_struct *mgr)
2044{
2045 u32 mysid = current_sid();
2046 u32 mgrsid = task_sid(mgr);
2047
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002048 return avc_has_perm(&selinux_state,
2049 mysid, mgrsid, SECCLASS_BINDER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002050 BINDER__SET_CONTEXT_MGR, NULL);
2051}
2052
2053static int selinux_binder_transaction(struct task_struct *from,
2054 struct task_struct *to)
2055{
2056 u32 mysid = current_sid();
2057 u32 fromsid = task_sid(from);
2058 u32 tosid = task_sid(to);
2059 int rc;
2060
2061 if (mysid != fromsid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002062 rc = avc_has_perm(&selinux_state,
2063 mysid, fromsid, SECCLASS_BINDER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002064 BINDER__IMPERSONATE, NULL);
2065 if (rc)
2066 return rc;
2067 }
2068
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002069 return avc_has_perm(&selinux_state,
2070 fromsid, tosid, SECCLASS_BINDER, BINDER__CALL,
Stephen Smalley79af7302015-01-21 10:54:10 -05002071 NULL);
2072}
2073
2074static int selinux_binder_transfer_binder(struct task_struct *from,
2075 struct task_struct *to)
2076{
2077 u32 fromsid = task_sid(from);
2078 u32 tosid = task_sid(to);
2079
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002080 return avc_has_perm(&selinux_state,
2081 fromsid, tosid, SECCLASS_BINDER, BINDER__TRANSFER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002082 NULL);
2083}
2084
2085static int selinux_binder_transfer_file(struct task_struct *from,
2086 struct task_struct *to,
2087 struct file *file)
2088{
2089 u32 sid = task_sid(to);
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07002090 struct file_security_struct *fsec = selinux_file(file);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002091 struct dentry *dentry = file->f_path.dentry;
Paul Moore20cdef82016-04-04 14:14:42 -04002092 struct inode_security_struct *isec;
Stephen Smalley79af7302015-01-21 10:54:10 -05002093 struct common_audit_data ad;
2094 int rc;
2095
2096 ad.type = LSM_AUDIT_DATA_PATH;
2097 ad.u.path = file->f_path;
2098
2099 if (sid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002100 rc = avc_has_perm(&selinux_state,
2101 sid, fsec->sid,
Stephen Smalley79af7302015-01-21 10:54:10 -05002102 SECCLASS_FD,
2103 FD__USE,
2104 &ad);
2105 if (rc)
2106 return rc;
2107 }
2108
Chenbo Fengf66e4482017-10-18 13:00:26 -07002109#ifdef CONFIG_BPF_SYSCALL
2110 rc = bpf_fd_pass(file, sid);
2111 if (rc)
2112 return rc;
2113#endif
2114
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002115 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
Stephen Smalley79af7302015-01-21 10:54:10 -05002116 return 0;
2117
Paul Moore20cdef82016-04-04 14:14:42 -04002118 isec = backing_inode_security(dentry);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002119 return avc_has_perm(&selinux_state,
2120 sid, isec->sid, isec->sclass, file_to_av(file),
Stephen Smalley79af7302015-01-21 10:54:10 -05002121 &ad);
2122}
2123
Ingo Molnar9e488582009-05-07 19:26:19 +10002124static int selinux_ptrace_access_check(struct task_struct *child,
David Howells5cd9c582008-08-14 11:37:28 +01002125 unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002127 u32 sid = current_sid();
2128 u32 csid = task_sid(child);
Stephen Smalley006ebb42008-05-19 08:32:49 -04002129
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002130 if (mode & PTRACE_MODE_READ)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002131 return avc_has_perm(&selinux_state,
2132 sid, csid, SECCLASS_FILE, FILE__READ, NULL);
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002133
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002134 return avc_has_perm(&selinux_state,
2135 sid, csid, SECCLASS_PROCESS, PROCESS__PTRACE, NULL);
David Howells5cd9c582008-08-14 11:37:28 +01002136}
2137
2138static int selinux_ptrace_traceme(struct task_struct *parent)
2139{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002140 return avc_has_perm(&selinux_state,
2141 task_sid(parent), current_sid(), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002142 PROCESS__PTRACE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143}
2144
2145static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
Eric Paris828dfe12008-04-17 13:17:49 -04002146 kernel_cap_t *inheritable, kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002148 return avc_has_perm(&selinux_state,
2149 current_sid(), task_sid(target), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002150 PROCESS__GETCAP, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151}
2152
David Howellsd84f4f92008-11-14 10:39:23 +11002153static int selinux_capset(struct cred *new, const struct cred *old,
2154 const kernel_cap_t *effective,
2155 const kernel_cap_t *inheritable,
2156 const kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002158 return avc_has_perm(&selinux_state,
2159 cred_sid(old), cred_sid(new), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002160 PROCESS__SETCAP, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161}
2162
James Morris5626d3e2009-01-30 10:05:06 +11002163/*
2164 * (This comment used to live with the selinux_task_setuid hook,
2165 * which was removed).
2166 *
2167 * Since setuid only affects the current process, and since the SELinux
2168 * controls are not based on the Linux identity attributes, SELinux does not
2169 * need to control this operation. However, SELinux does control the use of
2170 * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
2171 */
2172
Eric Paris6a9de492012-01-03 12:25:14 -05002173static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
Micah Mortonc1a85a02019-01-07 16:10:53 -08002174 int cap, unsigned int opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175{
Micah Mortonc1a85a02019-01-07 16:10:53 -08002176 return cred_has_capability(cred, cap, opts, ns == &init_user_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177}
2178
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
2180{
David Howells88e67f32008-11-14 10:39:21 +11002181 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 int rc = 0;
2183
2184 if (!sb)
2185 return 0;
2186
2187 switch (cmds) {
Eric Paris828dfe12008-04-17 13:17:49 -04002188 case Q_SYNC:
2189 case Q_QUOTAON:
2190 case Q_QUOTAOFF:
2191 case Q_SETINFO:
2192 case Q_SETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11002193 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04002194 break;
2195 case Q_GETFMT:
2196 case Q_GETINFO:
2197 case Q_GETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11002198 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04002199 break;
2200 default:
2201 rc = 0; /* let the kernel handle invalid cmds */
2202 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 }
2204 return rc;
2205}
2206
2207static int selinux_quota_on(struct dentry *dentry)
2208{
David Howells88e67f32008-11-14 10:39:21 +11002209 const struct cred *cred = current_cred();
2210
Eric Paris2875fa02011-04-28 16:04:24 -04002211 return dentry_has_perm(cred, dentry, FILE__QUOTAON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212}
2213
Eric Paris12b30522010-11-15 18:36:29 -05002214static int selinux_syslog(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 switch (type) {
Kees Cookd78ca3c2010-02-03 15:37:13 -08002217 case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */
2218 case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002219 return avc_has_perm(&selinux_state,
2220 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002221 SECCLASS_SYSTEM, SYSTEM__SYSLOG_READ, NULL);
Kees Cookd78ca3c2010-02-03 15:37:13 -08002222 case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */
2223 case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */
2224 /* Set level of messages printed to console */
2225 case SYSLOG_ACTION_CONSOLE_LEVEL:
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002226 return avc_has_perm(&selinux_state,
2227 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002228 SECCLASS_SYSTEM, SYSTEM__SYSLOG_CONSOLE,
2229 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 }
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002231 /* All other syslog types */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002232 return avc_has_perm(&selinux_state,
2233 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002234 SECCLASS_SYSTEM, SYSTEM__SYSLOG_MOD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235}
2236
2237/*
2238 * Check that a process has enough memory to allocate a new virtual
2239 * mapping. 0 means there is enough memory for the allocation to
2240 * succeed and -ENOMEM implies there is not.
2241 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 * Do not audit the selinux permission check, as this is applied to all
2243 * processes that allocate mappings.
2244 */
Alan Cox34b4e4a2007-08-22 14:01:28 -07002245static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246{
2247 int rc, cap_sys_admin = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002249 rc = cred_has_capability(current_cred(), CAP_SYS_ADMIN,
Micah Mortonc1a85a02019-01-07 16:10:53 -08002250 CAP_OPT_NOAUDIT, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 if (rc == 0)
2252 cap_sys_admin = 1;
2253
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002254 return cap_sys_admin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255}
2256
2257/* binprm security operations */
2258
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002259static u32 ptrace_parent_sid(void)
Paul Moore0c6181c2016-03-30 21:41:21 -04002260{
2261 u32 sid = 0;
2262 struct task_struct *tracer;
2263
2264 rcu_read_lock();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002265 tracer = ptrace_parent(current);
Paul Moore0c6181c2016-03-30 21:41:21 -04002266 if (tracer)
2267 sid = task_sid(tracer);
2268 rcu_read_unlock();
2269
2270 return sid;
2271}
2272
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002273static int check_nnp_nosuid(const struct linux_binprm *bprm,
2274 const struct task_security_struct *old_tsec,
2275 const struct task_security_struct *new_tsec)
2276{
2277 int nnp = (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS);
Andy Lutomirski380cf5b2016-06-23 16:41:05 -05002278 int nosuid = !mnt_may_suid(bprm->file->f_path.mnt);
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002279 int rc;
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002280 u32 av;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002281
2282 if (!nnp && !nosuid)
2283 return 0; /* neither NNP nor nosuid */
2284
2285 if (new_tsec->sid == old_tsec->sid)
2286 return 0; /* No change in credentials */
2287
2288 /*
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002289 * If the policy enables the nnp_nosuid_transition policy capability,
2290 * then we permit transitions under NNP or nosuid if the
2291 * policy allows the corresponding permission between
2292 * the old and new contexts.
2293 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002294 if (selinux_policycap_nnp_nosuid_transition()) {
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002295 av = 0;
2296 if (nnp)
2297 av |= PROCESS2__NNP_TRANSITION;
2298 if (nosuid)
2299 av |= PROCESS2__NOSUID_TRANSITION;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002300 rc = avc_has_perm(&selinux_state,
2301 old_tsec->sid, new_tsec->sid,
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002302 SECCLASS_PROCESS2, av, NULL);
2303 if (!rc)
2304 return 0;
2305 }
2306
2307 /*
2308 * We also permit NNP or nosuid transitions to bounded SIDs,
2309 * i.e. SIDs that are guaranteed to only be allowed a subset
2310 * of the permissions of the current SID.
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002311 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002312 rc = security_bounded_transition(&selinux_state, old_tsec->sid,
2313 new_tsec->sid);
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002314 if (!rc)
2315 return 0;
2316
2317 /*
2318 * On failure, preserve the errno values for NNP vs nosuid.
2319 * NNP: Operation not permitted for caller.
2320 * nosuid: Permission denied to file.
2321 */
2322 if (nnp)
2323 return -EPERM;
2324 return -EACCES;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002325}
2326
David Howellsa6f76f22008-11-14 10:39:24 +11002327static int selinux_bprm_set_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328{
David Howellsa6f76f22008-11-14 10:39:24 +11002329 const struct task_security_struct *old_tsec;
2330 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 struct inode_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04002332 struct common_audit_data ad;
Al Viro496ad9a2013-01-23 17:07:38 -05002333 struct inode *inode = file_inode(bprm->file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 int rc;
2335
David Howellsa6f76f22008-11-14 10:39:24 +11002336 /* SELinux context only depends on initial program or script and not
2337 * the script interpreter */
Kees Cookddb4a142017-07-18 15:25:23 -07002338 if (bprm->called_set_creds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 return 0;
2340
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002341 old_tsec = selinux_cred(current_cred());
2342 new_tsec = selinux_cred(bprm->cred);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002343 isec = inode_security(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
2345 /* Default to the current task SID. */
David Howellsa6f76f22008-11-14 10:39:24 +11002346 new_tsec->sid = old_tsec->sid;
2347 new_tsec->osid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
Michael LeMay28eba5b2006-06-27 02:53:42 -07002349 /* Reset fs, key, and sock SIDs on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002350 new_tsec->create_sid = 0;
2351 new_tsec->keycreate_sid = 0;
2352 new_tsec->sockcreate_sid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353
David Howellsa6f76f22008-11-14 10:39:24 +11002354 if (old_tsec->exec_sid) {
2355 new_tsec->sid = old_tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 /* Reset exec SID on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002357 new_tsec->exec_sid = 0;
Andy Lutomirski259e5e62012-04-12 16:47:50 -05002358
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002359 /* Fail on NNP or nosuid if not an allowed transition. */
2360 rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
2361 if (rc)
2362 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 } else {
2364 /* Check for a default transition on this program. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002365 rc = security_transition_sid(&selinux_state, old_tsec->sid,
2366 isec->sid, SECCLASS_PROCESS, NULL,
Eric Paris652bb9b2011-02-01 11:05:40 -05002367 &new_tsec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 if (rc)
2369 return rc;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002370
2371 /*
2372 * Fallback to old SID on NNP or nosuid if not an allowed
2373 * transition.
2374 */
2375 rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
2376 if (rc)
2377 new_tsec->sid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 }
2379
Vivek Goyal43af5de2016-09-09 11:37:49 -04002380 ad.type = LSM_AUDIT_DATA_FILE;
2381 ad.u.file = bprm->file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
David Howellsa6f76f22008-11-14 10:39:24 +11002383 if (new_tsec->sid == old_tsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002384 rc = avc_has_perm(&selinux_state,
2385 old_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
2387 if (rc)
2388 return rc;
2389 } else {
2390 /* Check permissions for the transition. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002391 rc = avc_has_perm(&selinux_state,
2392 old_tsec->sid, new_tsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
2394 if (rc)
2395 return rc;
2396
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002397 rc = avc_has_perm(&selinux_state,
2398 new_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
2400 if (rc)
2401 return rc;
2402
David Howellsa6f76f22008-11-14 10:39:24 +11002403 /* Check for shared state */
2404 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002405 rc = avc_has_perm(&selinux_state,
2406 old_tsec->sid, new_tsec->sid,
David Howellsa6f76f22008-11-14 10:39:24 +11002407 SECCLASS_PROCESS, PROCESS__SHARE,
2408 NULL);
2409 if (rc)
2410 return -EPERM;
2411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412
David Howellsa6f76f22008-11-14 10:39:24 +11002413 /* Make sure that anyone attempting to ptrace over a task that
2414 * changes its SID has the appropriate permit */
Eric W. Biederman9227dd22017-01-23 17:26:31 +13002415 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002416 u32 ptsid = ptrace_parent_sid();
David Howellsa6f76f22008-11-14 10:39:24 +11002417 if (ptsid != 0) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002418 rc = avc_has_perm(&selinux_state,
2419 ptsid, new_tsec->sid,
David Howellsa6f76f22008-11-14 10:39:24 +11002420 SECCLASS_PROCESS,
2421 PROCESS__PTRACE, NULL);
2422 if (rc)
2423 return -EPERM;
2424 }
2425 }
2426
2427 /* Clear any possibly unsafe personality bits on exec: */
2428 bprm->per_clear |= PER_CLEAR_ON_SETID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 /* Enable secure mode for SIDs transitions unless
2431 the noatsecure permission is granted between
2432 the two SIDs, i.e. ahp returns 0. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002433 rc = avc_has_perm(&selinux_state,
2434 old_tsec->sid, new_tsec->sid,
Kees Cook62874c32017-07-18 15:25:25 -07002435 SECCLASS_PROCESS, PROCESS__NOATSECURE,
2436 NULL);
2437 bprm->secureexec |= !!rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 }
2439
Kees Cook62874c32017-07-18 15:25:25 -07002440 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441}
2442
Al Viroc3c073f2012-08-21 22:32:06 -04002443static int match_file(const void *p, struct file *file, unsigned fd)
2444{
2445 return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0;
2446}
2447
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448/* Derived from fs/exec.c:flush_old_files. */
David Howells745ca242008-11-14 10:39:22 +11002449static inline void flush_unauthorized_files(const struct cred *cred,
2450 struct files_struct *files)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 struct file *file, *devnull = NULL;
Stephen Smalleyb20c8122006-09-25 23:32:03 -07002453 struct tty_struct *tty;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002454 int drop_tty = 0;
Al Viroc3c073f2012-08-21 22:32:06 -04002455 unsigned n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002457 tty = get_current_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 if (tty) {
Peter Hurley4a510962016-01-09 21:35:23 -08002459 spin_lock(&tty->files_lock);
Eric Paris37dd0bd2008-10-31 17:40:00 -04002460 if (!list_empty(&tty->tty_files)) {
Nick Piggind996b622010-08-18 04:37:36 +10002461 struct tty_file_private *file_priv;
Eric Paris37dd0bd2008-10-31 17:40:00 -04002462
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 /* Revalidate access to controlling tty.
David Howells13f8e982013-06-13 23:37:55 +01002464 Use file_path_has_perm on the tty path directly
2465 rather than using file_has_perm, as this particular
2466 open file may belong to another process and we are
2467 only interested in the inode-based check here. */
Nick Piggind996b622010-08-18 04:37:36 +10002468 file_priv = list_first_entry(&tty->tty_files,
2469 struct tty_file_private, list);
2470 file = file_priv->file;
David Howells13f8e982013-06-13 23:37:55 +01002471 if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE))
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002472 drop_tty = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 }
Peter Hurley4a510962016-01-09 21:35:23 -08002474 spin_unlock(&tty->files_lock);
Alan Cox452a00d2008-10-13 10:39:13 +01002475 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 }
Eric W. Biederman98a27ba2007-05-08 00:26:56 -07002477 /* Reset controlling tty. */
2478 if (drop_tty)
2479 no_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480
2481 /* Revalidate access to inherited open files. */
Al Viroc3c073f2012-08-21 22:32:06 -04002482 n = iterate_fd(files, 0, match_file, cred);
2483 if (!n) /* none found? */
2484 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
Al Viroc3c073f2012-08-21 22:32:06 -04002486 devnull = dentry_open(&selinux_null, O_RDWR, cred);
Al Viro45525b22012-10-16 13:30:07 -04002487 if (IS_ERR(devnull))
2488 devnull = NULL;
2489 /* replace all the matching ones with this */
2490 do {
2491 replace_fd(n - 1, devnull, 0);
2492 } while ((n = iterate_fd(files, n, match_file, cred)) != 0);
2493 if (devnull)
Al Viroc3c073f2012-08-21 22:32:06 -04002494 fput(devnull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495}
2496
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497/*
David Howellsa6f76f22008-11-14 10:39:24 +11002498 * Prepare a process for imminent new credential changes due to exec
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 */
David Howellsa6f76f22008-11-14 10:39:24 +11002500static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501{
David Howellsa6f76f22008-11-14 10:39:24 +11002502 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 struct rlimit *rlim, *initrlim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 int rc, i;
2505
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002506 new_tsec = selinux_cred(bprm->cred);
David Howellsa6f76f22008-11-14 10:39:24 +11002507 if (new_tsec->sid == new_tsec->osid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 return;
2509
2510 /* Close files for which the new task SID is not authorized. */
David Howellsa6f76f22008-11-14 10:39:24 +11002511 flush_unauthorized_files(bprm->cred, current->files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512
David Howellsa6f76f22008-11-14 10:39:24 +11002513 /* Always clear parent death signal on SID transitions. */
2514 current->pdeath_signal = 0;
2515
2516 /* Check whether the new SID can inherit resource limits from the old
2517 * SID. If not, reset all soft limits to the lower of the current
2518 * task's hard limit and the init task's soft limit.
2519 *
2520 * Note that the setting of hard limits (even to lower them) can be
2521 * controlled by the setrlimit check. The inclusion of the init task's
2522 * soft limit into the computation is to avoid resetting soft limits
2523 * higher than the default soft limit for cases where the default is
2524 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2525 */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002526 rc = avc_has_perm(&selinux_state,
2527 new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
David Howellsa6f76f22008-11-14 10:39:24 +11002528 PROCESS__RLIMITINH, NULL);
2529 if (rc) {
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002530 /* protect against do_prlimit() */
2531 task_lock(current);
David Howellsa6f76f22008-11-14 10:39:24 +11002532 for (i = 0; i < RLIM_NLIMITS; i++) {
2533 rlim = current->signal->rlim + i;
2534 initrlim = init_task.signal->rlim + i;
2535 rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
2536 }
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002537 task_unlock(current);
Nicolas Pitrebaa73d92016-11-11 00:10:10 -05002538 if (IS_ENABLED(CONFIG_POSIX_TIMERS))
2539 update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
David Howellsa6f76f22008-11-14 10:39:24 +11002540 }
2541}
2542
2543/*
2544 * Clean up the process immediately after the installation of new credentials
2545 * due to exec
2546 */
2547static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
2548{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002549 const struct task_security_struct *tsec = selinux_cred(current_cred());
David Howellsa6f76f22008-11-14 10:39:24 +11002550 struct itimerval itimer;
David Howellsa6f76f22008-11-14 10:39:24 +11002551 u32 osid, sid;
2552 int rc, i;
David Howellsa6f76f22008-11-14 10:39:24 +11002553
David Howellsa6f76f22008-11-14 10:39:24 +11002554 osid = tsec->osid;
2555 sid = tsec->sid;
2556
2557 if (sid == osid)
2558 return;
2559
2560 /* Check whether the new SID can inherit signal state from the old SID.
2561 * If not, clear itimers to avoid subsequent signal generation and
2562 * flush and unblock signals.
2563 *
2564 * This must occur _after_ the task SID has been updated so that any
2565 * kill done after the flush will be checked against the new SID.
2566 */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002567 rc = avc_has_perm(&selinux_state,
2568 osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 if (rc) {
Nicolas Pitrebaa73d92016-11-11 00:10:10 -05002570 if (IS_ENABLED(CONFIG_POSIX_TIMERS)) {
2571 memset(&itimer, 0, sizeof itimer);
2572 for (i = 0; i < 3; i++)
2573 do_setitimer(i, &itimer, NULL);
2574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterov9e7c8f82015-06-04 16:22:16 -04002576 if (!fatal_signal_pending(current)) {
2577 flush_sigqueue(&current->pending);
2578 flush_sigqueue(&current->signal->shared_pending);
David Howells3bcac022009-04-29 13:45:05 +01002579 flush_signal_handlers(current, 1);
2580 sigemptyset(&current->blocked);
Oleg Nesterov9e7c8f82015-06-04 16:22:16 -04002581 recalc_sigpending();
David Howells3bcac022009-04-29 13:45:05 +01002582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 spin_unlock_irq(&current->sighand->siglock);
2584 }
2585
David Howellsa6f76f22008-11-14 10:39:24 +11002586 /* Wake up the parent if it is waiting so that it can recheck
2587 * wait permission to the new task SID. */
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002588 read_lock(&tasklist_lock);
Oleg Nesterov0b7570e2009-09-23 15:56:46 -07002589 __wake_up_parent(current, current->real_parent);
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002590 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591}
2592
2593/* superblock security operations */
2594
2595static int selinux_sb_alloc_security(struct super_block *sb)
2596{
2597 return superblock_alloc_security(sb);
2598}
2599
2600static void selinux_sb_free_security(struct super_block *sb)
2601{
2602 superblock_free_security(sb);
2603}
2604
Al Viro99dbbb52018-12-14 21:56:23 -05002605static inline int opt_len(const char *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606{
Al Viro99dbbb52018-12-14 21:56:23 -05002607 bool open_quote = false;
2608 int len;
2609 char c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610
Al Viro99dbbb52018-12-14 21:56:23 -05002611 for (len = 0; (c = s[len]) != '\0'; len++) {
2612 if (c == '"')
Cory Olmo3528a952006-09-29 01:58:44 -07002613 open_quote = !open_quote;
Al Viro99dbbb52018-12-14 21:56:23 -05002614 if (c == ',' && !open_quote)
2615 break;
2616 }
2617 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618}
2619
Al Viro204cc0c2018-12-13 13:41:47 -05002620static int selinux_sb_eat_lsm_opts(char *options, void **mnt_opts)
Eric Paris026eb162011-03-03 16:09:14 -05002621{
Al Viro99dbbb52018-12-14 21:56:23 -05002622 char *from = options;
2623 char *to = options;
2624 bool first = true;
Gen Zhangfec63752019-06-12 21:55:38 +08002625 int rc;
Al Viro5b400232018-12-12 20:13:29 -05002626
Al Viro99dbbb52018-12-14 21:56:23 -05002627 while (1) {
2628 int len = opt_len(from);
Gen Zhangfec63752019-06-12 21:55:38 +08002629 int token;
Al Viro99dbbb52018-12-14 21:56:23 -05002630 char *arg = NULL;
2631
2632 token = match_opt_prefix(from, len, &arg);
2633
2634 if (token != Opt_error) {
2635 char *p, *q;
2636
2637 /* strip quotes */
2638 if (arg) {
2639 for (p = q = arg; p < from + len; p++) {
2640 char c = *p;
2641 if (c != '"')
2642 *q++ = c;
2643 }
2644 arg = kmemdup_nul(arg, q - arg, GFP_KERNEL);
Gen Zhangfec63752019-06-12 21:55:38 +08002645 if (!arg) {
2646 rc = -ENOMEM;
2647 goto free_opt;
2648 }
Al Viro99dbbb52018-12-14 21:56:23 -05002649 }
2650 rc = selinux_add_opt(token, arg, mnt_opts);
2651 if (unlikely(rc)) {
2652 kfree(arg);
Gen Zhangfec63752019-06-12 21:55:38 +08002653 goto free_opt;
Al Viro99dbbb52018-12-14 21:56:23 -05002654 }
2655 } else {
2656 if (!first) { // copy with preceding comma
2657 from--;
2658 len++;
2659 }
2660 if (to != from)
2661 memmove(to, from, len);
2662 to += len;
2663 first = false;
2664 }
2665 if (!from[len])
2666 break;
2667 from += len + 1;
2668 }
2669 *to = '\0';
2670 return 0;
Gen Zhangfec63752019-06-12 21:55:38 +08002671
2672free_opt:
2673 if (*mnt_opts) {
2674 selinux_free_mnt_opts(*mnt_opts);
2675 *mnt_opts = NULL;
2676 }
2677 return rc;
Al Viro5b400232018-12-12 20:13:29 -05002678}
2679
Al Viro204cc0c2018-12-13 13:41:47 -05002680static int selinux_sb_remount(struct super_block *sb, void *mnt_opts)
Eric Paris026eb162011-03-03 16:09:14 -05002681{
Al Virobd323652018-12-13 15:04:59 -05002682 struct selinux_mnt_opts *opts = mnt_opts;
Eric Paris026eb162011-03-03 16:09:14 -05002683 struct superblock_security_struct *sbsec = sb->s_security;
Al Virobd323652018-12-13 15:04:59 -05002684 u32 sid;
2685 int rc;
Eric Paris026eb162011-03-03 16:09:14 -05002686
2687 if (!(sbsec->flags & SE_SBINITIALIZED))
2688 return 0;
2689
Al Viro204cc0c2018-12-13 13:41:47 -05002690 if (!opts)
Eric Paris026eb162011-03-03 16:09:14 -05002691 return 0;
2692
Al Virobd323652018-12-13 15:04:59 -05002693 if (opts->fscontext) {
2694 rc = parse_sid(sb, opts->fscontext, &sid);
2695 if (rc)
Al Viroc039bc32018-12-01 23:06:57 -05002696 return rc;
Al Virobd323652018-12-13 15:04:59 -05002697 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
2698 goto out_bad_option;
Eric Paris026eb162011-03-03 16:09:14 -05002699 }
Al Virobd323652018-12-13 15:04:59 -05002700 if (opts->context) {
2701 rc = parse_sid(sb, opts->context, &sid);
2702 if (rc)
2703 return rc;
2704 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
2705 goto out_bad_option;
2706 }
2707 if (opts->rootcontext) {
2708 struct inode_security_struct *root_isec;
2709 root_isec = backing_inode_security(sb->s_root);
2710 rc = parse_sid(sb, opts->rootcontext, &sid);
2711 if (rc)
2712 return rc;
2713 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
2714 goto out_bad_option;
2715 }
2716 if (opts->defcontext) {
2717 rc = parse_sid(sb, opts->defcontext, &sid);
2718 if (rc)
2719 return rc;
2720 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
2721 goto out_bad_option;
Eric Paris026eb162011-03-03 16:09:14 -05002722 }
Al Viroc039bc32018-12-01 23:06:57 -05002723 return 0;
Eric Paris026eb162011-03-03 16:09:14 -05002724
Eric Paris026eb162011-03-03 16:09:14 -05002725out_bad_option:
peter enderborgc103a912018-06-12 10:09:03 +02002726 pr_warn("SELinux: unable to change security options "
Linus Torvalds29b1deb2013-12-15 11:17:45 -08002727 "during remount (dev %s, type=%s)\n", sb->s_id,
2728 sb->s_type->name);
Al Viroc039bc32018-12-01 23:06:57 -05002729 return -EINVAL;
Eric Paris026eb162011-03-03 16:09:14 -05002730}
2731
Al Viroa10d7c22018-12-05 11:58:35 -05002732static int selinux_sb_kern_mount(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733{
David Howells88e67f32008-11-14 10:39:21 +11002734 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002735 struct common_audit_data ad;
James Morris74192242008-12-19 11:41:10 +11002736
Eric Paris50c205f2012-04-04 15:01:43 -04002737 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002738 ad.u.dentry = sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002739 return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740}
2741
David Howells726c3342006-06-23 02:02:58 -07002742static int selinux_sb_statfs(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743{
David Howells88e67f32008-11-14 10:39:21 +11002744 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002745 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746
Eric Paris50c205f2012-04-04 15:01:43 -04002747 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002748 ad.u.dentry = dentry->d_sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002749 return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750}
2751
Al Viro808d4e32012-10-11 11:42:01 -04002752static int selinux_mount(const char *dev_name,
Al Viro8a04c432016-03-25 14:52:53 -04002753 const struct path *path,
Al Viro808d4e32012-10-11 11:42:01 -04002754 const char *type,
Eric Paris828dfe12008-04-17 13:17:49 -04002755 unsigned long flags,
2756 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757{
David Howells88e67f32008-11-14 10:39:21 +11002758 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759
2760 if (flags & MS_REMOUNT)
Al Virod8c95842011-12-07 18:16:57 -05002761 return superblock_has_perm(cred, path->dentry->d_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002762 FILESYSTEM__REMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 else
Eric Paris2875fa02011-04-28 16:04:24 -04002764 return path_has_perm(cred, path, FILE__MOUNTON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765}
2766
2767static int selinux_umount(struct vfsmount *mnt, int flags)
2768{
David Howells88e67f32008-11-14 10:39:21 +11002769 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770
David Howells88e67f32008-11-14 10:39:21 +11002771 return superblock_has_perm(cred, mnt->mnt_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002772 FILESYSTEM__UNMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773}
2774
Al Viro0b520752018-12-23 16:02:47 -05002775static int selinux_fs_context_dup(struct fs_context *fc,
2776 struct fs_context *src_fc)
2777{
2778 const struct selinux_mnt_opts *src = src_fc->security;
2779 struct selinux_mnt_opts *opts;
2780
2781 if (!src)
2782 return 0;
2783
2784 fc->security = kzalloc(sizeof(struct selinux_mnt_opts), GFP_KERNEL);
2785 if (!fc->security)
2786 return -ENOMEM;
2787
2788 opts = fc->security;
2789
2790 if (src->fscontext) {
2791 opts->fscontext = kstrdup(src->fscontext, GFP_KERNEL);
2792 if (!opts->fscontext)
2793 return -ENOMEM;
2794 }
2795 if (src->context) {
2796 opts->context = kstrdup(src->context, GFP_KERNEL);
2797 if (!opts->context)
2798 return -ENOMEM;
2799 }
2800 if (src->rootcontext) {
2801 opts->rootcontext = kstrdup(src->rootcontext, GFP_KERNEL);
2802 if (!opts->rootcontext)
2803 return -ENOMEM;
2804 }
2805 if (src->defcontext) {
2806 opts->defcontext = kstrdup(src->defcontext, GFP_KERNEL);
2807 if (!opts->defcontext)
2808 return -ENOMEM;
2809 }
2810 return 0;
2811}
2812
David Howells442155c2018-11-01 23:07:24 +00002813static const struct fs_parameter_spec selinux_param_specs[] = {
2814 fsparam_string(CONTEXT_STR, Opt_context),
2815 fsparam_string(DEFCONTEXT_STR, Opt_defcontext),
2816 fsparam_string(FSCONTEXT_STR, Opt_fscontext),
2817 fsparam_string(ROOTCONTEXT_STR, Opt_rootcontext),
2818 fsparam_flag (SECLABEL_STR, Opt_seclabel),
2819 {}
2820};
2821
2822static const struct fs_parameter_description selinux_fs_parameters = {
2823 .name = "SELinux",
2824 .specs = selinux_param_specs,
2825};
2826
2827static int selinux_fs_context_parse_param(struct fs_context *fc,
2828 struct fs_parameter *param)
2829{
2830 struct fs_parse_result result;
2831 int opt, rc;
2832
2833 opt = fs_parse(fc, &selinux_fs_parameters, param, &result);
2834 if (opt < 0)
2835 return opt;
2836
2837 rc = selinux_add_opt(opt, param->string, &fc->security);
2838 if (!rc) {
2839 param->string = NULL;
2840 rc = 1;
2841 }
2842 return rc;
2843}
2844
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845/* inode security operations */
2846
2847static int selinux_inode_alloc_security(struct inode *inode)
2848{
2849 return inode_alloc_security(inode);
2850}
2851
2852static void selinux_inode_free_security(struct inode *inode)
2853{
2854 inode_free_security(inode);
2855}
2856
David Quigleyd47be3d2013-05-22 12:50:34 -04002857static int selinux_dentry_init_security(struct dentry *dentry, int mode,
Al Viro4f3ccd72016-07-20 16:06:15 -04002858 const struct qstr *name, void **ctx,
David Quigleyd47be3d2013-05-22 12:50:34 -04002859 u32 *ctxlen)
2860{
David Quigleyd47be3d2013-05-22 12:50:34 -04002861 u32 newsid;
2862 int rc;
2863
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002864 rc = selinux_determine_inode_label(selinux_cred(current_cred()),
Vivek Goyalc957f6d2016-07-13 10:44:51 -04002865 d_inode(dentry->d_parent), name,
David Howellsc3c188b2015-07-10 17:19:58 -04002866 inode_mode_to_security_class(mode),
2867 &newsid);
2868 if (rc)
2869 return rc;
David Quigleyd47be3d2013-05-22 12:50:34 -04002870
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002871 return security_sid_to_context(&selinux_state, newsid, (char **)ctx,
2872 ctxlen);
David Quigleyd47be3d2013-05-22 12:50:34 -04002873}
2874
Vivek Goyala518b0a2016-07-13 10:44:53 -04002875static int selinux_dentry_create_files_as(struct dentry *dentry, int mode,
2876 struct qstr *name,
2877 const struct cred *old,
2878 struct cred *new)
2879{
2880 u32 newsid;
2881 int rc;
2882 struct task_security_struct *tsec;
2883
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002884 rc = selinux_determine_inode_label(selinux_cred(old),
Vivek Goyala518b0a2016-07-13 10:44:53 -04002885 d_inode(dentry->d_parent), name,
2886 inode_mode_to_security_class(mode),
2887 &newsid);
2888 if (rc)
2889 return rc;
2890
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002891 tsec = selinux_cred(new);
Vivek Goyala518b0a2016-07-13 10:44:53 -04002892 tsec->create_sid = newsid;
2893 return 0;
2894}
2895
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002896static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +09002897 const struct qstr *qstr,
2898 const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -05002899 void **value, size_t *len)
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002900{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002901 const struct task_security_struct *tsec = selinux_cred(current_cred());
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002902 struct superblock_security_struct *sbsec;
Corentin LABBEc0d4f462017-10-04 20:32:17 +02002903 u32 newsid, clen;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002904 int rc;
Tetsuo Handa95489062013-07-25 05:44:02 +09002905 char *context;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002906
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002907 sbsec = dir->i_sb->s_security;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002908
David Howells275bb412008-11-14 10:39:19 +11002909 newsid = tsec->create_sid;
2910
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002911 rc = selinux_determine_inode_label(selinux_cred(current_cred()),
David Howellsc3c188b2015-07-10 17:19:58 -04002912 dir, qstr,
2913 inode_mode_to_security_class(inode->i_mode),
2914 &newsid);
2915 if (rc)
2916 return rc;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002917
Eric Paris296fddf2006-09-25 23:32:00 -07002918 /* Possibly defer initialization to selinux_complete_init. */
David P. Quigley0d90a7e2009-01-16 09:22:02 -05002919 if (sbsec->flags & SE_SBINITIALIZED) {
Casey Schaufler80788c22018-09-21 17:19:11 -07002920 struct inode_security_struct *isec = selinux_inode(inode);
Eric Paris296fddf2006-09-25 23:32:00 -07002921 isec->sclass = inode_mode_to_security_class(inode->i_mode);
2922 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05002923 isec->initialized = LABEL_INITIALIZED;
Eric Paris296fddf2006-09-25 23:32:00 -07002924 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002925
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002926 if (!selinux_state.initialized || !(sbsec->flags & SBLABEL_MNT))
Stephen Smalley25a74f32005-11-08 21:34:33 -08002927 return -EOPNOTSUPP;
2928
Tetsuo Handa95489062013-07-25 05:44:02 +09002929 if (name)
2930 *name = XATTR_SELINUX_SUFFIX;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002931
2932 if (value && len) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002933 rc = security_sid_to_context_force(&selinux_state, newsid,
2934 &context, &clen);
Tetsuo Handa95489062013-07-25 05:44:02 +09002935 if (rc)
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002936 return rc;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002937 *value = context;
2938 *len = clen;
2939 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002940
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002941 return 0;
2942}
2943
Al Viro4acdaf22011-07-26 01:42:34 -04002944static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945{
2946 return may_create(dir, dentry, SECCLASS_FILE);
2947}
2948
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2950{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 return may_link(dir, old_dentry, MAY_LINK);
2952}
2953
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
2955{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 return may_link(dir, dentry, MAY_UNLINK);
2957}
2958
2959static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
2960{
2961 return may_create(dir, dentry, SECCLASS_LNK_FILE);
2962}
2963
Al Viro18bb1db2011-07-26 01:41:39 -04002964static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965{
2966 return may_create(dir, dentry, SECCLASS_DIR);
2967}
2968
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
2970{
2971 return may_link(dir, dentry, MAY_RMDIR);
2972}
2973
Al Viro1a67aaf2011-07-26 01:52:52 -04002974static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 return may_create(dir, dentry, inode_mode_to_security_class(mode));
2977}
2978
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
Eric Paris828dfe12008-04-17 13:17:49 -04002980 struct inode *new_inode, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981{
2982 return may_rename(old_inode, old_dentry, new_inode, new_dentry);
2983}
2984
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985static int selinux_inode_readlink(struct dentry *dentry)
2986{
David Howells88e67f32008-11-14 10:39:21 +11002987 const struct cred *cred = current_cred();
2988
Eric Paris2875fa02011-04-28 16:04:24 -04002989 return dentry_has_perm(cred, dentry, FILE__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990}
2991
NeilBrownbda0be72015-03-23 13:37:39 +11002992static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode,
2993 bool rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994{
David Howells88e67f32008-11-14 10:39:21 +11002995 const struct cred *cred = current_cred();
NeilBrownbda0be72015-03-23 13:37:39 +11002996 struct common_audit_data ad;
2997 struct inode_security_struct *isec;
2998 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999
NeilBrownbda0be72015-03-23 13:37:39 +11003000 validate_creds(cred);
3001
3002 ad.type = LSM_AUDIT_DATA_DENTRY;
3003 ad.u.dentry = dentry;
3004 sid = cred_sid(cred);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05003005 isec = inode_security_rcu(inode, rcu);
3006 if (IS_ERR(isec))
3007 return PTR_ERR(isec);
NeilBrownbda0be72015-03-23 13:37:39 +11003008
Stephen Smalleye46e01e2018-12-12 10:10:56 -05003009 return avc_has_perm(&selinux_state,
3010 sid, isec->sid, isec->sclass, FILE__READ, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011}
3012
Eric Parisd4cf970d2012-04-04 15:01:42 -04003013static noinline int audit_inode_permission(struct inode *inode,
3014 u32 perms, u32 audited, u32 denied,
Stephen Smalley626b9742014-04-29 11:29:04 -07003015 int result,
Eric Parisd4cf970d2012-04-04 15:01:42 -04003016 unsigned flags)
3017{
3018 struct common_audit_data ad;
Casey Schaufler80788c22018-09-21 17:19:11 -07003019 struct inode_security_struct *isec = selinux_inode(inode);
Eric Parisd4cf970d2012-04-04 15:01:42 -04003020 int rc;
3021
Eric Paris50c205f2012-04-04 15:01:43 -04003022 ad.type = LSM_AUDIT_DATA_INODE;
Eric Parisd4cf970d2012-04-04 15:01:42 -04003023 ad.u.inode = inode;
3024
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003025 rc = slow_avc_audit(&selinux_state,
3026 current_sid(), isec->sid, isec->sclass, perms,
Stephen Smalley626b9742014-04-29 11:29:04 -07003027 audited, denied, result, &ad, flags);
Eric Parisd4cf970d2012-04-04 15:01:42 -04003028 if (rc)
3029 return rc;
3030 return 0;
3031}
3032
Al Viroe74f71e2011-06-20 19:38:15 -04003033static int selinux_inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034{
David Howells88e67f32008-11-14 10:39:21 +11003035 const struct cred *cred = current_cred();
Eric Parisb782e0a2010-07-23 11:44:03 -04003036 u32 perms;
3037 bool from_access;
Al Virocf1dd1d2011-06-20 19:44:08 -04003038 unsigned flags = mask & MAY_NOT_BLOCK;
Eric Paris2e334052012-04-04 15:01:42 -04003039 struct inode_security_struct *isec;
3040 u32 sid;
3041 struct av_decision avd;
3042 int rc, rc2;
3043 u32 audited, denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044
Eric Parisb782e0a2010-07-23 11:44:03 -04003045 from_access = mask & MAY_ACCESS;
Eric Parisd09ca732010-07-23 11:43:57 -04003046 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
3047
Eric Parisb782e0a2010-07-23 11:44:03 -04003048 /* No permission to check. Existence test. */
3049 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051
Eric Paris2e334052012-04-04 15:01:42 -04003052 validate_creds(cred);
Eric Parisb782e0a2010-07-23 11:44:03 -04003053
Eric Paris2e334052012-04-04 15:01:42 -04003054 if (unlikely(IS_PRIVATE(inode)))
3055 return 0;
Eric Parisb782e0a2010-07-23 11:44:03 -04003056
3057 perms = file_mask_to_av(inode->i_mode, mask);
3058
Eric Paris2e334052012-04-04 15:01:42 -04003059 sid = cred_sid(cred);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05003060 isec = inode_security_rcu(inode, flags & MAY_NOT_BLOCK);
3061 if (IS_ERR(isec))
3062 return PTR_ERR(isec);
Eric Paris2e334052012-04-04 15:01:42 -04003063
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003064 rc = avc_has_perm_noaudit(&selinux_state,
Stephen Smalley3a28cff2018-12-12 10:10:55 -05003065 sid, isec->sid, isec->sclass, perms,
3066 (flags & MAY_NOT_BLOCK) ? AVC_NONBLOCKING : 0,
3067 &avd);
Eric Paris2e334052012-04-04 15:01:42 -04003068 audited = avc_audit_required(perms, &avd, rc,
3069 from_access ? FILE__AUDIT_ACCESS : 0,
3070 &denied);
3071 if (likely(!audited))
3072 return rc;
3073
Stephen Smalley626b9742014-04-29 11:29:04 -07003074 rc2 = audit_inode_permission(inode, perms, audited, denied, rc, flags);
Eric Paris2e334052012-04-04 15:01:42 -04003075 if (rc2)
3076 return rc2;
3077 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078}
3079
3080static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
3081{
David Howells88e67f32008-11-14 10:39:21 +11003082 const struct cred *cred = current_cred();
Stephen Smalleyccb54472017-05-12 12:41:24 -04003083 struct inode *inode = d_backing_inode(dentry);
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003084 unsigned int ia_valid = iattr->ia_valid;
Eric Paris95dbf732012-04-04 13:45:34 -04003085 __u32 av = FILE__WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003087 /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
3088 if (ia_valid & ATTR_FORCE) {
3089 ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
3090 ATTR_FORCE);
3091 if (!ia_valid)
3092 return 0;
3093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003095 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
3096 ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
Eric Paris2875fa02011-04-28 16:04:24 -04003097 return dentry_has_perm(cred, dentry, FILE__SETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003099 if (selinux_policycap_openperm() &&
Stephen Smalleyccb54472017-05-12 12:41:24 -04003100 inode->i_sb->s_magic != SOCKFS_MAGIC &&
3101 (ia_valid & ATTR_SIZE) &&
3102 !(ia_valid & ATTR_FILE))
Eric Paris95dbf732012-04-04 13:45:34 -04003103 av |= FILE__OPEN;
3104
3105 return dentry_has_perm(cred, dentry, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106}
3107
Al Viro3f7036a2015-03-08 19:28:30 -04003108static int selinux_inode_getattr(const struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109{
Al Viro3f7036a2015-03-08 19:28:30 -04003110 return path_has_perm(current_cred(), path, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111}
3112
Stephen Smalleydb590002017-04-20 11:31:30 -04003113static bool has_cap_mac_admin(bool audit)
3114{
3115 const struct cred *cred = current_cred();
Micah Mortonc1a85a02019-01-07 16:10:53 -08003116 unsigned int opts = audit ? CAP_OPT_NONE : CAP_OPT_NOAUDIT;
Stephen Smalleydb590002017-04-20 11:31:30 -04003117
Micah Mortonc1a85a02019-01-07 16:10:53 -08003118 if (cap_capable(cred, &init_user_ns, CAP_MAC_ADMIN, opts))
Stephen Smalleydb590002017-04-20 11:31:30 -04003119 return false;
Micah Mortonc1a85a02019-01-07 16:10:53 -08003120 if (cred_has_capability(cred, CAP_MAC_ADMIN, opts, true))
Stephen Smalleydb590002017-04-20 11:31:30 -04003121 return false;
3122 return true;
3123}
3124
David Howells8f0cfa52008-04-29 00:59:41 -07003125static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
3126 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127{
David Howellsc6f493d2015-03-17 22:26:22 +00003128 struct inode *inode = d_backing_inode(dentry);
Paul Moore20cdef82016-04-04 14:14:42 -04003129 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 struct superblock_security_struct *sbsec;
Thomas Liu2bf49692009-07-14 12:14:09 -04003131 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11003132 u32 newsid, sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 int rc = 0;
3134
Eric W. Biederman6b240302017-10-02 09:38:20 -05003135 if (strcmp(name, XATTR_NAME_SELINUX)) {
3136 rc = cap_inode_setxattr(dentry, name, value, size, flags);
3137 if (rc)
3138 return rc;
3139
3140 /* Not an attribute we recognize, so just check the
3141 ordinary setattr permission. */
3142 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144
3145 sbsec = inode->i_sb->s_security;
Eric Paris12f348b2012-10-09 10:56:25 -04003146 if (!(sbsec->flags & SBLABEL_MNT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 return -EOPNOTSUPP;
3148
Serge E. Hallyn2e149672011-03-23 16:43:26 -07003149 if (!inode_owner_or_capable(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 return -EPERM;
3151
Eric Paris50c205f2012-04-04 15:01:43 -04003152 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04003153 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154
Paul Moore20cdef82016-04-04 14:14:42 -04003155 isec = backing_inode_security(dentry);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003156 rc = avc_has_perm(&selinux_state,
3157 sid, isec->sid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 FILE__RELABELFROM, &ad);
3159 if (rc)
3160 return rc;
3161
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003162 rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3163 GFP_KERNEL);
Stephen Smalley12b29f32008-05-07 13:03:20 -04003164 if (rc == -EINVAL) {
Stephen Smalleydb590002017-04-20 11:31:30 -04003165 if (!has_cap_mac_admin(true)) {
Eric Parisd6ea83e2012-04-04 13:45:49 -04003166 struct audit_buffer *ab;
3167 size_t audit_size;
Eric Parisd6ea83e2012-04-04 13:45:49 -04003168
3169 /* We strip a nul only if it is at the end, otherwise the
3170 * context contains a nul and we should audit that */
Al Viroe3fea3f2012-06-09 08:15:16 +01003171 if (value) {
Colin Ian Kingadd24372017-10-14 13:46:55 +01003172 const char *str = value;
3173
Al Viroe3fea3f2012-06-09 08:15:16 +01003174 if (str[size - 1] == '\0')
3175 audit_size = size - 1;
3176 else
3177 audit_size = size;
3178 } else {
Al Viroe3fea3f2012-06-09 08:15:16 +01003179 audit_size = 0;
3180 }
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04003181 ab = audit_log_start(audit_context(),
3182 GFP_ATOMIC, AUDIT_SELINUX_ERR);
Eric Parisd6ea83e2012-04-04 13:45:49 -04003183 audit_log_format(ab, "op=setxattr invalid_context=");
3184 audit_log_n_untrustedstring(ab, value, audit_size);
3185 audit_log_end(ab);
3186
Stephen Smalley12b29f32008-05-07 13:03:20 -04003187 return rc;
Eric Parisd6ea83e2012-04-04 13:45:49 -04003188 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003189 rc = security_context_to_sid_force(&selinux_state, value,
3190 size, &newsid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04003191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 if (rc)
3193 return rc;
3194
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003195 rc = avc_has_perm(&selinux_state,
3196 sid, newsid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 FILE__RELABELTO, &ad);
3198 if (rc)
3199 return rc;
3200
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003201 rc = security_validate_transition(&selinux_state, isec->sid, newsid,
3202 sid, isec->sclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 if (rc)
3204 return rc;
3205
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003206 return avc_has_perm(&selinux_state,
3207 newsid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 sbsec->sid,
3209 SECCLASS_FILESYSTEM,
3210 FILESYSTEM__ASSOCIATE,
3211 &ad);
3212}
3213
David Howells8f0cfa52008-04-29 00:59:41 -07003214static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
Eric Parisf5269712008-05-14 11:27:45 -04003215 const void *value, size_t size,
David Howells8f0cfa52008-04-29 00:59:41 -07003216 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217{
David Howellsc6f493d2015-03-17 22:26:22 +00003218 struct inode *inode = d_backing_inode(dentry);
Paul Moore20cdef82016-04-04 14:14:42 -04003219 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 u32 newsid;
3221 int rc;
3222
3223 if (strcmp(name, XATTR_NAME_SELINUX)) {
3224 /* Not an attribute we recognize, so nothing to do. */
3225 return;
3226 }
3227
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003228 rc = security_context_to_sid_force(&selinux_state, value, size,
3229 &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 if (rc) {
peter enderborgc103a912018-06-12 10:09:03 +02003231 pr_err("SELinux: unable to map context to SID"
Stephen Smalley12b29f32008-05-07 13:03:20 -04003232 "for (%s, %lu), rc=%d\n",
3233 inode->i_sb->s_id, inode->i_ino, -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 return;
3235 }
3236
Paul Moore20cdef82016-04-04 14:14:42 -04003237 isec = backing_inode_security(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003238 spin_lock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003239 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05003241 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003242 spin_unlock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003243
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 return;
3245}
3246
David Howells8f0cfa52008-04-29 00:59:41 -07003247static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248{
David Howells88e67f32008-11-14 10:39:21 +11003249 const struct cred *cred = current_cred();
3250
Eric Paris2875fa02011-04-28 16:04:24 -04003251 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252}
3253
Eric Paris828dfe12008-04-17 13:17:49 -04003254static int selinux_inode_listxattr(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255{
David Howells88e67f32008-11-14 10:39:21 +11003256 const struct cred *cred = current_cred();
3257
Eric Paris2875fa02011-04-28 16:04:24 -04003258 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259}
3260
David Howells8f0cfa52008-04-29 00:59:41 -07003261static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262{
Eric W. Biederman6b240302017-10-02 09:38:20 -05003263 if (strcmp(name, XATTR_NAME_SELINUX)) {
3264 int rc = cap_inode_removexattr(dentry, name);
3265 if (rc)
3266 return rc;
3267
3268 /* Not an attribute we recognize, so just check the
3269 ordinary setattr permission. */
3270 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272
3273 /* No one is allowed to remove a SELinux security label.
3274 You can change the label, but all data must be labeled. */
3275 return -EACCES;
3276}
3277
James Morrisd381d8a2005-10-30 14:59:22 -08003278/*
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003279 * Copy the inode security context value to the user.
James Morrisd381d8a2005-10-30 14:59:22 -08003280 *
3281 * Permission check is handled by selinux_inode_getxattr hook.
3282 */
Andreas Gruenbacherea861df2015-12-24 11:09:39 -05003283static int selinux_inode_getsecurity(struct inode *inode, const char *name, void **buffer, bool alloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284{
David P. Quigley42492592008-02-04 22:29:39 -08003285 u32 size;
3286 int error;
3287 char *context = NULL;
Paul Moore20cdef82016-04-04 14:14:42 -04003288 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00003290 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3291 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003293 /*
3294 * If the caller has CAP_MAC_ADMIN, then get the raw context
3295 * value even if it is not defined by current policy; otherwise,
3296 * use the in-core value under current policy.
3297 * Use the non-auditing forms of the permission checks since
3298 * getxattr may be called by unprivileged processes commonly
3299 * and lack of permission just means that we fall back to the
3300 * in-core context value, not a denial.
3301 */
Paul Moore20cdef82016-04-04 14:14:42 -04003302 isec = inode_security(inode);
Stephen Smalleydb590002017-04-20 11:31:30 -04003303 if (has_cap_mac_admin(false))
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003304 error = security_sid_to_context_force(&selinux_state,
3305 isec->sid, &context,
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003306 &size);
3307 else
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003308 error = security_sid_to_context(&selinux_state, isec->sid,
3309 &context, &size);
David P. Quigley42492592008-02-04 22:29:39 -08003310 if (error)
3311 return error;
3312 error = size;
3313 if (alloc) {
3314 *buffer = context;
3315 goto out_nofree;
3316 }
3317 kfree(context);
3318out_nofree:
3319 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320}
3321
3322static int selinux_inode_setsecurity(struct inode *inode, const char *name,
Eric Paris828dfe12008-04-17 13:17:49 -04003323 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324{
Paul Moore2c971652016-04-19 16:36:28 -04003325 struct inode_security_struct *isec = inode_security_novalidate(inode);
Ondrej Mosnacek53e0c2a2018-12-21 21:18:53 +01003326 struct superblock_security_struct *sbsec = inode->i_sb->s_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 u32 newsid;
3328 int rc;
3329
3330 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3331 return -EOPNOTSUPP;
3332
Ondrej Mosnacek53e0c2a2018-12-21 21:18:53 +01003333 if (!(sbsec->flags & SBLABEL_MNT))
3334 return -EOPNOTSUPP;
3335
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 if (!value || !size)
3337 return -EACCES;
3338
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003339 rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3340 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 if (rc)
3342 return rc;
3343
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003344 spin_lock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003345 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05003347 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003348 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 return 0;
3350}
3351
3352static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
3353{
3354 const int len = sizeof(XATTR_NAME_SELINUX);
3355 if (buffer && len <= buffer_size)
3356 memcpy(buffer, XATTR_NAME_SELINUX, len);
3357 return len;
3358}
3359
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05003360static void selinux_inode_getsecid(struct inode *inode, u32 *secid)
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02003361{
Andreas Gruenbachere817c2f2016-02-18 12:04:08 +01003362 struct inode_security_struct *isec = inode_security_novalidate(inode);
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02003363 *secid = isec->sid;
3364}
3365
Vivek Goyal56909eb2016-07-13 10:44:48 -04003366static int selinux_inode_copy_up(struct dentry *src, struct cred **new)
3367{
3368 u32 sid;
3369 struct task_security_struct *tsec;
3370 struct cred *new_creds = *new;
3371
3372 if (new_creds == NULL) {
3373 new_creds = prepare_creds();
3374 if (!new_creds)
3375 return -ENOMEM;
3376 }
3377
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07003378 tsec = selinux_cred(new_creds);
Vivek Goyal56909eb2016-07-13 10:44:48 -04003379 /* Get label from overlay inode and set it in create_sid */
3380 selinux_inode_getsecid(d_inode(src), &sid);
3381 tsec->create_sid = sid;
3382 *new = new_creds;
3383 return 0;
3384}
3385
Vivek Goyal19472b62016-07-13 10:44:50 -04003386static int selinux_inode_copy_up_xattr(const char *name)
3387{
3388 /* The copy_up hook above sets the initial context on an inode, but we
3389 * don't then want to overwrite it by blindly copying all the lower
3390 * xattrs up. Instead, we have to filter out SELinux-related xattrs.
3391 */
3392 if (strcmp(name, XATTR_NAME_SELINUX) == 0)
3393 return 1; /* Discard */
3394 /*
3395 * Any other attribute apart from SELINUX is not claimed, supported
3396 * by selinux.
3397 */
3398 return -EOPNOTSUPP;
3399}
3400
Ondrej Mosnacekec882da2019-02-22 15:57:17 +01003401/* kernfs node operations */
3402
YueHaibingc72c4cd2019-03-22 22:04:00 +08003403static int selinux_kernfs_init_security(struct kernfs_node *kn_dir,
3404 struct kernfs_node *kn)
Ondrej Mosnacekec882da2019-02-22 15:57:17 +01003405{
3406 const struct task_security_struct *tsec = current_security();
3407 u32 parent_sid, newsid, clen;
3408 int rc;
3409 char *context;
3410
Ondrej Mosnacek1537ad12019-04-03 09:29:41 +02003411 rc = kernfs_xattr_get(kn_dir, XATTR_NAME_SELINUX, NULL, 0);
Ondrej Mosnacekec882da2019-02-22 15:57:17 +01003412 if (rc == -ENODATA)
3413 return 0;
3414 else if (rc < 0)
3415 return rc;
3416
3417 clen = (u32)rc;
3418 context = kmalloc(clen, GFP_KERNEL);
3419 if (!context)
3420 return -ENOMEM;
3421
Ondrej Mosnacek1537ad12019-04-03 09:29:41 +02003422 rc = kernfs_xattr_get(kn_dir, XATTR_NAME_SELINUX, context, clen);
Ondrej Mosnacekec882da2019-02-22 15:57:17 +01003423 if (rc < 0) {
3424 kfree(context);
3425 return rc;
3426 }
3427
3428 rc = security_context_to_sid(&selinux_state, context, clen, &parent_sid,
3429 GFP_KERNEL);
3430 kfree(context);
3431 if (rc)
3432 return rc;
3433
3434 if (tsec->create_sid) {
3435 newsid = tsec->create_sid;
3436 } else {
3437 u16 secclass = inode_mode_to_security_class(kn->mode);
3438 struct qstr q;
3439
3440 q.name = kn->name;
3441 q.hash_len = hashlen_string(kn_dir, kn->name);
3442
3443 rc = security_transition_sid(&selinux_state, tsec->sid,
3444 parent_sid, secclass, &q,
3445 &newsid);
3446 if (rc)
3447 return rc;
3448 }
3449
3450 rc = security_sid_to_context_force(&selinux_state, newsid,
3451 &context, &clen);
3452 if (rc)
3453 return rc;
3454
Ondrej Mosnacek1537ad12019-04-03 09:29:41 +02003455 rc = kernfs_xattr_set(kn, XATTR_NAME_SELINUX, context, clen,
3456 XATTR_CREATE);
Ondrej Mosnacekec882da2019-02-22 15:57:17 +01003457 kfree(context);
3458 return rc;
3459}
3460
3461
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462/* file security operations */
3463
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003464static int selinux_revalidate_file_permission(struct file *file, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465{
David Howells88e67f32008-11-14 10:39:21 +11003466 const struct cred *cred = current_cred();
Al Viro496ad9a2013-01-23 17:07:38 -05003467 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
3470 if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
3471 mask |= MAY_APPEND;
3472
Paul Moore389fb8002009-03-27 17:10:34 -04003473 return file_has_perm(cred, file,
3474 file_mask_to_av(inode->i_mode, mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475}
3476
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003477static int selinux_file_permission(struct file *file, int mask)
3478{
Al Viro496ad9a2013-01-23 17:07:38 -05003479 struct inode *inode = file_inode(file);
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07003480 struct file_security_struct *fsec = selinux_file(file);
Andreas Gruenbacherb1973672016-01-05 23:12:33 +01003481 struct inode_security_struct *isec;
Stephen Smalley20dda182009-06-22 14:54:53 -04003482 u32 sid = current_sid();
3483
Paul Moore389fb8002009-03-27 17:10:34 -04003484 if (!mask)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003485 /* No permission to check. Existence test. */
3486 return 0;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003487
Andreas Gruenbacherb1973672016-01-05 23:12:33 +01003488 isec = inode_security(inode);
Stephen Smalley20dda182009-06-22 14:54:53 -04003489 if (sid == fsec->sid && fsec->isid == isec->sid &&
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003490 fsec->pseqno == avc_policy_seqno(&selinux_state))
Eric Paris83d49852012-04-04 13:45:40 -04003491 /* No change since file_open check. */
Stephen Smalley20dda182009-06-22 14:54:53 -04003492 return 0;
3493
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003494 return selinux_revalidate_file_permission(file, mask);
3495}
3496
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497static int selinux_file_alloc_security(struct file *file)
3498{
3499 return file_alloc_security(file);
3500}
3501
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003502/*
3503 * Check whether a task has the ioctl permission and cmd
3504 * operation to an inode.
3505 */
Geliang Tang1d2a1682015-10-21 17:44:27 -04003506static int ioctl_has_perm(const struct cred *cred, struct file *file,
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003507 u32 requested, u16 cmd)
3508{
3509 struct common_audit_data ad;
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07003510 struct file_security_struct *fsec = selinux_file(file);
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003511 struct inode *inode = file_inode(file);
Paul Moore20cdef82016-04-04 14:14:42 -04003512 struct inode_security_struct *isec;
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003513 struct lsm_ioctlop_audit ioctl;
3514 u32 ssid = cred_sid(cred);
3515 int rc;
3516 u8 driver = cmd >> 8;
3517 u8 xperm = cmd & 0xff;
3518
3519 ad.type = LSM_AUDIT_DATA_IOCTL_OP;
3520 ad.u.op = &ioctl;
3521 ad.u.op->cmd = cmd;
3522 ad.u.op->path = file->f_path;
3523
3524 if (ssid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003525 rc = avc_has_perm(&selinux_state,
3526 ssid, fsec->sid,
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003527 SECCLASS_FD,
3528 FD__USE,
3529 &ad);
3530 if (rc)
3531 goto out;
3532 }
3533
3534 if (unlikely(IS_PRIVATE(inode)))
3535 return 0;
3536
Paul Moore20cdef82016-04-04 14:14:42 -04003537 isec = inode_security(inode);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003538 rc = avc_has_extended_perms(&selinux_state,
3539 ssid, isec->sid, isec->sclass,
3540 requested, driver, xperm, &ad);
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003541out:
3542 return rc;
3543}
3544
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545static int selinux_file_ioctl(struct file *file, unsigned int cmd,
3546 unsigned long arg)
3547{
David Howells88e67f32008-11-14 10:39:21 +11003548 const struct cred *cred = current_cred();
Eric Paris0b24dcb2011-02-25 15:39:20 -05003549 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550
Eric Paris0b24dcb2011-02-25 15:39:20 -05003551 switch (cmd) {
3552 case FIONREAD:
3553 /* fall through */
3554 case FIBMAP:
3555 /* fall through */
3556 case FIGETBSZ:
3557 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003558 case FS_IOC_GETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003559 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003560 case FS_IOC_GETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003561 error = file_has_perm(cred, file, FILE__GETATTR);
3562 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563
Al Viro2f99c362012-03-23 16:04:05 -04003564 case FS_IOC_SETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003565 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003566 case FS_IOC_SETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003567 error = file_has_perm(cred, file, FILE__SETATTR);
3568 break;
3569
3570 /* sys_ioctl() checks */
3571 case FIONBIO:
3572 /* fall through */
3573 case FIOASYNC:
3574 error = file_has_perm(cred, file, 0);
3575 break;
3576
3577 case KDSKBENT:
3578 case KDSKBSENT:
Eric Paris6a9de492012-01-03 12:25:14 -05003579 error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
Micah Mortonc1a85a02019-01-07 16:10:53 -08003580 CAP_OPT_NONE, true);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003581 break;
3582
3583 /* default case assumes that the command will go
3584 * to the file's ioctl() function.
3585 */
3586 default:
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003587 error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003588 }
3589 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590}
3591
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003592static int default_noexec;
3593
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
3595{
David Howells88e67f32008-11-14 10:39:21 +11003596 const struct cred *cred = current_cred();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003597 u32 sid = cred_sid(cred);
David Howellsd84f4f92008-11-14 10:39:23 +11003598 int rc = 0;
David Howells88e67f32008-11-14 10:39:21 +11003599
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003600 if (default_noexec &&
Stephen Smalley892e8ca2015-07-10 09:40:59 -04003601 (prot & PROT_EXEC) && (!file || IS_PRIVATE(file_inode(file)) ||
3602 (!shared && (prot & PROT_WRITE)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 /*
3604 * We are making executable an anonymous mapping or a
3605 * private file mapping that will also be writable.
3606 * This has an additional check.
3607 */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003608 rc = avc_has_perm(&selinux_state,
3609 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003610 PROCESS__EXECMEM, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 if (rc)
David Howellsd84f4f92008-11-14 10:39:23 +11003612 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614
3615 if (file) {
3616 /* read access is always possible with a mapping */
3617 u32 av = FILE__READ;
3618
3619 /* write access only matters if the mapping is shared */
3620 if (shared && (prot & PROT_WRITE))
3621 av |= FILE__WRITE;
3622
3623 if (prot & PROT_EXEC)
3624 av |= FILE__EXECUTE;
3625
David Howells88e67f32008-11-14 10:39:21 +11003626 return file_has_perm(cred, file, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 }
David Howellsd84f4f92008-11-14 10:39:23 +11003628
3629error:
3630 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631}
3632
Al Viroe5467852012-05-30 13:30:51 -04003633static int selinux_mmap_addr(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07003635 int rc = 0;
Paul Moore98883bf2014-03-19 16:46:11 -04003636
3637 if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
3638 u32 sid = current_sid();
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003639 rc = avc_has_perm(&selinux_state,
3640 sid, sid, SECCLASS_MEMPROTECT,
Paul Moore98883bf2014-03-19 16:46:11 -04003641 MEMPROTECT__MMAP_ZERO, NULL);
3642 }
3643
3644 return rc;
Al Viroe5467852012-05-30 13:30:51 -04003645}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646
Al Viroe5467852012-05-30 13:30:51 -04003647static int selinux_mmap_file(struct file *file, unsigned long reqprot,
3648 unsigned long prot, unsigned long flags)
3649{
Stephen Smalley3ba4bf52017-05-05 09:14:48 -04003650 struct common_audit_data ad;
3651 int rc;
3652
3653 if (file) {
3654 ad.type = LSM_AUDIT_DATA_FILE;
3655 ad.u.file = file;
3656 rc = inode_has_perm(current_cred(), file_inode(file),
3657 FILE__MAP, &ad);
3658 if (rc)
3659 return rc;
3660 }
3661
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003662 if (selinux_state.checkreqprot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 prot = reqprot;
3664
3665 return file_map_prot_check(file, prot,
3666 (flags & MAP_TYPE) == MAP_SHARED);
3667}
3668
3669static int selinux_file_mprotect(struct vm_area_struct *vma,
3670 unsigned long reqprot,
3671 unsigned long prot)
3672{
David Howells88e67f32008-11-14 10:39:21 +11003673 const struct cred *cred = current_cred();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003674 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003676 if (selinux_state.checkreqprot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 prot = reqprot;
3678
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003679 if (default_noexec &&
3680 (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
James Morrisd541bbe2009-01-29 12:19:51 +11003681 int rc = 0;
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003682 if (vma->vm_start >= vma->vm_mm->start_brk &&
3683 vma->vm_end <= vma->vm_mm->brk) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003684 rc = avc_has_perm(&selinux_state,
3685 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003686 PROCESS__EXECHEAP, NULL);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003687 } else if (!vma->vm_file &&
Stephen Smalleyc2316db2016-04-08 13:55:03 -04003688 ((vma->vm_start <= vma->vm_mm->start_stack &&
3689 vma->vm_end >= vma->vm_mm->start_stack) ||
Andy Lutomirskid17af502016-09-30 10:58:58 -07003690 vma_is_stack_for_current(vma))) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003691 rc = avc_has_perm(&selinux_state,
3692 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003693 PROCESS__EXECSTACK, NULL);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003694 } else if (vma->vm_file && vma->anon_vma) {
3695 /*
3696 * We are making executable a file mapping that has
3697 * had some COW done. Since pages might have been
3698 * written, check ability to execute the possibly
3699 * modified content. This typically should only
3700 * occur for text relocations.
3701 */
David Howellsd84f4f92008-11-14 10:39:23 +11003702 rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003703 }
Lorenzo Hernandez García-Hierro6b992192005-06-25 14:54:34 -07003704 if (rc)
3705 return rc;
3706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707
3708 return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
3709}
3710
3711static int selinux_file_lock(struct file *file, unsigned int cmd)
3712{
David Howells88e67f32008-11-14 10:39:21 +11003713 const struct cred *cred = current_cred();
3714
3715 return file_has_perm(cred, file, FILE__LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716}
3717
3718static int selinux_file_fcntl(struct file *file, unsigned int cmd,
3719 unsigned long arg)
3720{
David Howells88e67f32008-11-14 10:39:21 +11003721 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 int err = 0;
3723
3724 switch (cmd) {
Eric Paris828dfe12008-04-17 13:17:49 -04003725 case F_SETFL:
Eric Paris828dfe12008-04-17 13:17:49 -04003726 if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
David Howells88e67f32008-11-14 10:39:21 +11003727 err = file_has_perm(cred, file, FILE__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003729 }
3730 /* fall through */
3731 case F_SETOWN:
3732 case F_SETSIG:
3733 case F_GETFL:
3734 case F_GETOWN:
3735 case F_GETSIG:
Cyrill Gorcunov1d151c32012-07-30 14:43:00 -07003736 case F_GETOWNER_UIDS:
Eric Paris828dfe12008-04-17 13:17:49 -04003737 /* Just check FD__USE permission */
David Howells88e67f32008-11-14 10:39:21 +11003738 err = file_has_perm(cred, file, 0);
Eric Paris828dfe12008-04-17 13:17:49 -04003739 break;
3740 case F_GETLK:
3741 case F_SETLK:
3742 case F_SETLKW:
Jeff Layton0d3f7a22014-04-22 08:23:58 -04003743 case F_OFD_GETLK:
3744 case F_OFD_SETLK:
3745 case F_OFD_SETLKW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746#if BITS_PER_LONG == 32
Eric Paris828dfe12008-04-17 13:17:49 -04003747 case F_GETLK64:
3748 case F_SETLK64:
3749 case F_SETLKW64:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750#endif
David Howells88e67f32008-11-14 10:39:21 +11003751 err = file_has_perm(cred, file, FILE__LOCK);
Eric Paris828dfe12008-04-17 13:17:49 -04003752 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 }
3754
3755 return err;
3756}
3757
Jeff Laytone0b93ed2014-08-22 11:27:32 -04003758static void selinux_file_set_fowner(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 struct file_security_struct *fsec;
3761
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07003762 fsec = selinux_file(file);
David Howells275bb412008-11-14 10:39:19 +11003763 fsec->fown_sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764}
3765
3766static int selinux_file_send_sigiotask(struct task_struct *tsk,
3767 struct fown_struct *fown, int signum)
3768{
Eric Paris828dfe12008-04-17 13:17:49 -04003769 struct file *file;
Stephen Smalley65c90bc2009-05-04 15:43:18 -04003770 u32 sid = task_sid(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 struct file_security_struct *fsec;
3773
3774 /* struct fown_struct is never outside the context of a struct file */
Eric Paris828dfe12008-04-17 13:17:49 -04003775 file = container_of(fown, struct file, f_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07003777 fsec = selinux_file(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778
3779 if (!signum)
3780 perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
3781 else
3782 perm = signal_to_av(signum);
3783
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003784 return avc_has_perm(&selinux_state,
3785 fsec->fown_sid, sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 SECCLASS_PROCESS, perm, NULL);
3787}
3788
3789static int selinux_file_receive(struct file *file)
3790{
David Howells88e67f32008-11-14 10:39:21 +11003791 const struct cred *cred = current_cred();
3792
3793 return file_has_perm(cred, file, file_to_av(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794}
3795
Al Viro94817692018-07-10 14:13:18 -04003796static int selinux_file_open(struct file *file)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003797{
3798 struct file_security_struct *fsec;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003799 struct inode_security_struct *isec;
David Howellsd84f4f92008-11-14 10:39:23 +11003800
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07003801 fsec = selinux_file(file);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05003802 isec = inode_security(file_inode(file));
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003803 /*
3804 * Save inode label and policy sequence number
3805 * at open-time so that selinux_file_permission
3806 * can determine whether revalidation is necessary.
3807 * Task label is already saved in the file security
3808 * struct as its SID.
3809 */
3810 fsec->isid = isec->sid;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003811 fsec->pseqno = avc_policy_seqno(&selinux_state);
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003812 /*
3813 * Since the inode label or policy seqno may have changed
3814 * between the selinux_inode_permission check and the saving
3815 * of state above, recheck that access is still permitted.
3816 * Otherwise, access might never be revalidated against the
3817 * new inode label or new policy.
3818 * This check is not redundant - do not remove.
3819 */
Al Viro94817692018-07-10 14:13:18 -04003820 return file_path_has_perm(file->f_cred, file, open_file_to_av(file));
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003821}
3822
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823/* task security operations */
3824
Tetsuo Handaa79be232017-03-28 23:08:45 +09003825static int selinux_task_alloc(struct task_struct *task,
3826 unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003828 u32 sid = current_sid();
3829
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003830 return avc_has_perm(&selinux_state,
3831 sid, sid, SECCLASS_PROCESS, PROCESS__FORK, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832}
3833
David Howellsf1752ee2008-11-14 10:39:17 +11003834/*
David Howellsd84f4f92008-11-14 10:39:23 +11003835 * prepare a new set of credentials for modification
3836 */
3837static int selinux_cred_prepare(struct cred *new, const struct cred *old,
3838 gfp_t gfp)
3839{
Casey Schauflerbbd36622018-11-12 09:30:56 -08003840 const struct task_security_struct *old_tsec = selinux_cred(old);
3841 struct task_security_struct *tsec = selinux_cred(new);
David Howellsd84f4f92008-11-14 10:39:23 +11003842
Casey Schauflerbbd36622018-11-12 09:30:56 -08003843 *tsec = *old_tsec;
David Howellsd84f4f92008-11-14 10:39:23 +11003844 return 0;
3845}
3846
3847/*
David Howellsee18d642009-09-02 09:14:21 +01003848 * transfer the SELinux data to a blank set of creds
3849 */
3850static void selinux_cred_transfer(struct cred *new, const struct cred *old)
3851{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07003852 const struct task_security_struct *old_tsec = selinux_cred(old);
3853 struct task_security_struct *tsec = selinux_cred(new);
David Howellsee18d642009-09-02 09:14:21 +01003854
3855 *tsec = *old_tsec;
3856}
3857
Matthew Garrett3ec30112018-01-08 13:36:19 -08003858static void selinux_cred_getsecid(const struct cred *c, u32 *secid)
3859{
3860 *secid = cred_sid(c);
3861}
3862
David Howellsee18d642009-09-02 09:14:21 +01003863/*
David Howells3a3b7ce2008-11-14 10:39:28 +11003864 * set the security data for a kernel service
3865 * - all the creation contexts are set to unlabelled
3866 */
3867static int selinux_kernel_act_as(struct cred *new, u32 secid)
3868{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07003869 struct task_security_struct *tsec = selinux_cred(new);
David Howells3a3b7ce2008-11-14 10:39:28 +11003870 u32 sid = current_sid();
3871 int ret;
3872
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003873 ret = avc_has_perm(&selinux_state,
3874 sid, secid,
David Howells3a3b7ce2008-11-14 10:39:28 +11003875 SECCLASS_KERNEL_SERVICE,
3876 KERNEL_SERVICE__USE_AS_OVERRIDE,
3877 NULL);
3878 if (ret == 0) {
3879 tsec->sid = secid;
3880 tsec->create_sid = 0;
3881 tsec->keycreate_sid = 0;
3882 tsec->sockcreate_sid = 0;
3883 }
3884 return ret;
3885}
3886
3887/*
3888 * set the file creation context in a security record to the same as the
3889 * objective context of the specified inode
3890 */
3891static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
3892{
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05003893 struct inode_security_struct *isec = inode_security(inode);
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07003894 struct task_security_struct *tsec = selinux_cred(new);
David Howells3a3b7ce2008-11-14 10:39:28 +11003895 u32 sid = current_sid();
3896 int ret;
3897
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003898 ret = avc_has_perm(&selinux_state,
3899 sid, isec->sid,
David Howells3a3b7ce2008-11-14 10:39:28 +11003900 SECCLASS_KERNEL_SERVICE,
3901 KERNEL_SERVICE__CREATE_FILES_AS,
3902 NULL);
3903
3904 if (ret == 0)
3905 tsec->create_sid = isec->sid;
David Howellsef574712010-02-26 01:56:16 +00003906 return ret;
David Howells3a3b7ce2008-11-14 10:39:28 +11003907}
3908
Eric Parisdd8dbf22009-11-03 16:35:32 +11003909static int selinux_kernel_module_request(char *kmod_name)
Eric Paris25354c42009-08-13 09:45:03 -04003910{
Eric Parisdd8dbf22009-11-03 16:35:32 +11003911 struct common_audit_data ad;
3912
Eric Paris50c205f2012-04-04 15:01:43 -04003913 ad.type = LSM_AUDIT_DATA_KMOD;
Eric Parisdd8dbf22009-11-03 16:35:32 +11003914 ad.u.kmod_name = kmod_name;
3915
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003916 return avc_has_perm(&selinux_state,
3917 current_sid(), SECINITSID_KERNEL, SECCLASS_SYSTEM,
Eric Parisdd8dbf22009-11-03 16:35:32 +11003918 SYSTEM__MODULE_REQUEST, &ad);
Eric Paris25354c42009-08-13 09:45:03 -04003919}
3920
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003921static int selinux_kernel_module_from_file(struct file *file)
3922{
3923 struct common_audit_data ad;
3924 struct inode_security_struct *isec;
3925 struct file_security_struct *fsec;
3926 u32 sid = current_sid();
3927 int rc;
3928
3929 /* init_module */
3930 if (file == NULL)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003931 return avc_has_perm(&selinux_state,
3932 sid, sid, SECCLASS_SYSTEM,
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003933 SYSTEM__MODULE_LOAD, NULL);
3934
3935 /* finit_module */
Paul Moore20cdef82016-04-04 14:14:42 -04003936
Vivek Goyal43af5de2016-09-09 11:37:49 -04003937 ad.type = LSM_AUDIT_DATA_FILE;
3938 ad.u.file = file;
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003939
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07003940 fsec = selinux_file(file);
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003941 if (sid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003942 rc = avc_has_perm(&selinux_state,
3943 sid, fsec->sid, SECCLASS_FD, FD__USE, &ad);
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003944 if (rc)
3945 return rc;
3946 }
3947
Paul Moore20cdef82016-04-04 14:14:42 -04003948 isec = inode_security(file_inode(file));
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003949 return avc_has_perm(&selinux_state,
3950 sid, isec->sid, SECCLASS_SYSTEM,
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003951 SYSTEM__MODULE_LOAD, &ad);
3952}
3953
3954static int selinux_kernel_read_file(struct file *file,
3955 enum kernel_read_file_id id)
3956{
3957 int rc = 0;
3958
3959 switch (id) {
3960 case READING_MODULE:
3961 rc = selinux_kernel_module_from_file(file);
3962 break;
3963 default:
3964 break;
3965 }
3966
3967 return rc;
3968}
3969
Mimi Zoharc77b8cd2018-07-13 14:06:02 -04003970static int selinux_kernel_load_data(enum kernel_load_data_id id)
3971{
3972 int rc = 0;
3973
3974 switch (id) {
3975 case LOADING_MODULE:
3976 rc = selinux_kernel_module_from_file(NULL);
3977 default:
3978 break;
3979 }
3980
3981 return rc;
3982}
3983
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
3985{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003986 return avc_has_perm(&selinux_state,
3987 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003988 PROCESS__SETPGID, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989}
3990
3991static int selinux_task_getpgid(struct task_struct *p)
3992{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003993 return avc_has_perm(&selinux_state,
3994 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003995 PROCESS__GETPGID, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996}
3997
3998static int selinux_task_getsid(struct task_struct *p)
3999{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004000 return avc_has_perm(&selinux_state,
4001 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004002 PROCESS__GETSESSION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003}
4004
David Quigleyf9008e4c2006-06-30 01:55:46 -07004005static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
4006{
David Howells275bb412008-11-14 10:39:19 +11004007 *secid = task_sid(p);
David Quigleyf9008e4c2006-06-30 01:55:46 -07004008}
4009
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010static int selinux_task_setnice(struct task_struct *p, int nice)
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015}
4016
James Morris03e68062006-06-23 02:03:58 -07004017static int selinux_task_setioprio(struct task_struct *p, int ioprio)
4018{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004019 return avc_has_perm(&selinux_state,
4020 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004021 PROCESS__SETSCHED, NULL);
James Morris03e68062006-06-23 02:03:58 -07004022}
4023
David Quigleya1836a42006-06-30 01:55:49 -07004024static int selinux_task_getioprio(struct task_struct *p)
4025{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004026 return avc_has_perm(&selinux_state,
4027 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004028 PROCESS__GETSCHED, NULL);
David Quigleya1836a42006-06-30 01:55:49 -07004029}
4030
Corentin LABBE42985552017-10-04 20:32:18 +02004031static int selinux_task_prlimit(const struct cred *cred, const struct cred *tcred,
4032 unsigned int flags)
Stephen Smalley791ec492017-02-17 07:57:00 -05004033{
4034 u32 av = 0;
4035
Stephen Smalley84e68852017-02-28 09:35:08 -05004036 if (!flags)
4037 return 0;
Stephen Smalley791ec492017-02-17 07:57:00 -05004038 if (flags & LSM_PRLIMIT_WRITE)
4039 av |= PROCESS__SETRLIMIT;
4040 if (flags & LSM_PRLIMIT_READ)
4041 av |= PROCESS__GETRLIMIT;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004042 return avc_has_perm(&selinux_state,
4043 cred_sid(cred), cred_sid(tcred),
Stephen Smalley791ec492017-02-17 07:57:00 -05004044 SECCLASS_PROCESS, av, NULL);
4045}
4046
Jiri Slaby8fd00b42009-08-26 18:41:16 +02004047static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
4048 struct rlimit *new_rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049{
Jiri Slaby8fd00b42009-08-26 18:41:16 +02004050 struct rlimit *old_rlim = p->signal->rlim + resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051
4052 /* Control the ability to change the hard limit (whether
4053 lowering or raising it), so that the hard limit can
4054 later be used as a safe reset point for the soft limit
David Howellsd84f4f92008-11-14 10:39:23 +11004055 upon context transitions. See selinux_bprm_committing_creds. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 if (old_rlim->rlim_max != new_rlim->rlim_max)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004057 return avc_has_perm(&selinux_state,
4058 current_sid(), task_sid(p),
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004059 SECCLASS_PROCESS, PROCESS__SETRLIMIT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060
4061 return 0;
4062}
4063
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09004064static int selinux_task_setscheduler(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004066 return avc_has_perm(&selinux_state,
4067 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004068 PROCESS__SETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069}
4070
4071static int selinux_task_getscheduler(struct task_struct *p)
4072{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004073 return avc_has_perm(&selinux_state,
4074 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004075 PROCESS__GETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076}
4077
David Quigley35601542006-06-23 02:04:01 -07004078static int selinux_task_movememory(struct task_struct *p)
4079{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004080 return avc_has_perm(&selinux_state,
4081 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004082 PROCESS__SETSCHED, NULL);
David Quigley35601542006-06-23 02:04:01 -07004083}
4084
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02004085static int selinux_task_kill(struct task_struct *p, struct kernel_siginfo *info,
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004086 int sig, const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087{
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004088 u32 secid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 if (!sig)
4092 perm = PROCESS__SIGNULL; /* null signal; existence test */
4093 else
4094 perm = signal_to_av(sig);
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004095 if (!cred)
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004096 secid = current_sid();
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004097 else
4098 secid = cred_sid(cred);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004099 return avc_has_perm(&selinux_state,
4100 secid, task_sid(p), SECCLASS_PROCESS, perm, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101}
4102
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103static void selinux_task_to_inode(struct task_struct *p,
4104 struct inode *inode)
4105{
Casey Schaufler80788c22018-09-21 17:19:11 -07004106 struct inode_security_struct *isec = selinux_inode(inode);
David Howells275bb412008-11-14 10:39:19 +11004107 u32 sid = task_sid(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004109 spin_lock(&isec->lock);
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01004110 isec->sclass = inode_mode_to_security_class(inode->i_mode);
David Howells275bb412008-11-14 10:39:19 +11004111 isec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004112 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004113 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114}
4115
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004117static int selinux_parse_skb_ipv4(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04004118 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119{
4120 int offset, ihlen, ret = -EINVAL;
4121 struct iphdr _iph, *ih;
4122
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03004123 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
4125 if (ih == NULL)
4126 goto out;
4127
4128 ihlen = ih->ihl * 4;
4129 if (ihlen < sizeof(_iph))
4130 goto out;
4131
Eric Paris48c62af2012-04-02 13:15:44 -04004132 ad->u.net->v4info.saddr = ih->saddr;
4133 ad->u.net->v4info.daddr = ih->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 ret = 0;
4135
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004136 if (proto)
4137 *proto = ih->protocol;
4138
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 switch (ih->protocol) {
Eric Paris828dfe12008-04-17 13:17:49 -04004140 case IPPROTO_TCP: {
4141 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142
Eric Paris828dfe12008-04-17 13:17:49 -04004143 if (ntohs(ih->frag_off) & IP_OFFSET)
4144 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145
4146 offset += ihlen;
4147 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4148 if (th == NULL)
4149 break;
4150
Eric Paris48c62af2012-04-02 13:15:44 -04004151 ad->u.net->sport = th->source;
4152 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155
Eric Paris828dfe12008-04-17 13:17:49 -04004156 case IPPROTO_UDP: {
4157 struct udphdr _udph, *uh;
4158
4159 if (ntohs(ih->frag_off) & IP_OFFSET)
4160 break;
4161
4162 offset += ihlen;
4163 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4164 if (uh == NULL)
4165 break;
4166
Eric Paris48c62af2012-04-02 13:15:44 -04004167 ad->u.net->sport = uh->source;
4168 ad->u.net->dport = uh->dest;
Eric Paris828dfe12008-04-17 13:17:49 -04004169 break;
4170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171
James Morris2ee92d42006-11-13 16:09:01 -08004172 case IPPROTO_DCCP: {
4173 struct dccp_hdr _dccph, *dh;
4174
4175 if (ntohs(ih->frag_off) & IP_OFFSET)
4176 break;
4177
4178 offset += ihlen;
4179 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4180 if (dh == NULL)
4181 break;
4182
Eric Paris48c62af2012-04-02 13:15:44 -04004183 ad->u.net->sport = dh->dccph_sport;
4184 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08004185 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004186 }
James Morris2ee92d42006-11-13 16:09:01 -08004187
Richard Hainesd4529302018-02-13 20:57:18 +00004188#if IS_ENABLED(CONFIG_IP_SCTP)
4189 case IPPROTO_SCTP: {
4190 struct sctphdr _sctph, *sh;
4191
4192 if (ntohs(ih->frag_off) & IP_OFFSET)
4193 break;
4194
4195 offset += ihlen;
4196 sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4197 if (sh == NULL)
4198 break;
4199
4200 ad->u.net->sport = sh->source;
4201 ad->u.net->dport = sh->dest;
4202 break;
4203 }
4204#endif
Eric Paris828dfe12008-04-17 13:17:49 -04004205 default:
4206 break;
4207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208out:
4209 return ret;
4210}
4211
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04004212#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213
4214/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004215static int selinux_parse_skb_ipv6(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04004216 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217{
4218 u8 nexthdr;
4219 int ret = -EINVAL, offset;
4220 struct ipv6hdr _ipv6h, *ip6;
Jesse Gross75f28112011-11-30 17:05:51 -08004221 __be16 frag_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03004223 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
4225 if (ip6 == NULL)
4226 goto out;
4227
Eric Paris48c62af2012-04-02 13:15:44 -04004228 ad->u.net->v6info.saddr = ip6->saddr;
4229 ad->u.net->v6info.daddr = ip6->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 ret = 0;
4231
4232 nexthdr = ip6->nexthdr;
4233 offset += sizeof(_ipv6h);
Jesse Gross75f28112011-11-30 17:05:51 -08004234 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 if (offset < 0)
4236 goto out;
4237
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004238 if (proto)
4239 *proto = nexthdr;
4240
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 switch (nexthdr) {
4242 case IPPROTO_TCP: {
Eric Paris828dfe12008-04-17 13:17:49 -04004243 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244
4245 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4246 if (th == NULL)
4247 break;
4248
Eric Paris48c62af2012-04-02 13:15:44 -04004249 ad->u.net->sport = th->source;
4250 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 break;
4252 }
4253
4254 case IPPROTO_UDP: {
4255 struct udphdr _udph, *uh;
4256
4257 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4258 if (uh == NULL)
4259 break;
4260
Eric Paris48c62af2012-04-02 13:15:44 -04004261 ad->u.net->sport = uh->source;
4262 ad->u.net->dport = uh->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 break;
4264 }
4265
James Morris2ee92d42006-11-13 16:09:01 -08004266 case IPPROTO_DCCP: {
4267 struct dccp_hdr _dccph, *dh;
4268
4269 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4270 if (dh == NULL)
4271 break;
4272
Eric Paris48c62af2012-04-02 13:15:44 -04004273 ad->u.net->sport = dh->dccph_sport;
4274 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08004275 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004276 }
James Morris2ee92d42006-11-13 16:09:01 -08004277
Richard Hainesd4529302018-02-13 20:57:18 +00004278#if IS_ENABLED(CONFIG_IP_SCTP)
4279 case IPPROTO_SCTP: {
4280 struct sctphdr _sctph, *sh;
4281
4282 sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4283 if (sh == NULL)
4284 break;
4285
4286 ad->u.net->sport = sh->source;
4287 ad->u.net->dport = sh->dest;
4288 break;
4289 }
4290#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 /* includes fragments */
4292 default:
4293 break;
4294 }
4295out:
4296 return ret;
4297}
4298
4299#endif /* IPV6 */
4300
Thomas Liu2bf49692009-07-14 12:14:09 -04004301static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
David Howellscf9481e2008-07-27 21:31:07 +10004302 char **_addrp, int src, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303{
David Howellscf9481e2008-07-27 21:31:07 +10004304 char *addrp;
4305 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306
Eric Paris48c62af2012-04-02 13:15:44 -04004307 switch (ad->u.net->family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 case PF_INET:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004309 ret = selinux_parse_skb_ipv4(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10004310 if (ret)
4311 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04004312 addrp = (char *)(src ? &ad->u.net->v4info.saddr :
4313 &ad->u.net->v4info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10004314 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04004316#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 case PF_INET6:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004318 ret = selinux_parse_skb_ipv6(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10004319 if (ret)
4320 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04004321 addrp = (char *)(src ? &ad->u.net->v6info.saddr :
4322 &ad->u.net->v6info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10004323 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324#endif /* IPV6 */
4325 default:
David Howellscf9481e2008-07-27 21:31:07 +10004326 addrp = NULL;
4327 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 }
4329
David Howellscf9481e2008-07-27 21:31:07 +10004330parse_error:
peter enderborgc103a912018-06-12 10:09:03 +02004331 pr_warn(
David Howellscf9481e2008-07-27 21:31:07 +10004332 "SELinux: failure in selinux_parse_skb(),"
4333 " unable to parse packet\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 return ret;
David Howellscf9481e2008-07-27 21:31:07 +10004335
4336okay:
4337 if (_addrp)
4338 *_addrp = addrp;
4339 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340}
4341
Paul Moore4f6a9932007-03-01 14:35:22 -05004342/**
Paul Moore220deb92008-01-29 08:38:23 -05004343 * selinux_skb_peerlbl_sid - Determine the peer label of a packet
Paul Moore4f6a9932007-03-01 14:35:22 -05004344 * @skb: the packet
Paul Moore75e22912008-01-29 08:38:04 -05004345 * @family: protocol family
Paul Moore220deb92008-01-29 08:38:23 -05004346 * @sid: the packet's peer label SID
Paul Moore4f6a9932007-03-01 14:35:22 -05004347 *
4348 * Description:
Paul Moore220deb92008-01-29 08:38:23 -05004349 * Check the various different forms of network peer labeling and determine
4350 * the peer label/SID for the packet; most of the magic actually occurs in
4351 * the security server function security_net_peersid_cmp(). The function
4352 * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
4353 * or -EACCES if @sid is invalid due to inconsistencies with the different
4354 * peer labels.
Paul Moore4f6a9932007-03-01 14:35:22 -05004355 *
4356 */
Paul Moore220deb92008-01-29 08:38:23 -05004357static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
Paul Moore4f6a9932007-03-01 14:35:22 -05004358{
Paul Moore71f1cb02008-01-29 08:51:16 -05004359 int err;
Paul Moore4f6a9932007-03-01 14:35:22 -05004360 u32 xfrm_sid;
4361 u32 nlbl_sid;
Paul Moore220deb92008-01-29 08:38:23 -05004362 u32 nlbl_type;
Paul Moore4f6a9932007-03-01 14:35:22 -05004363
Paul Moore817eff72013-12-10 14:57:54 -05004364 err = selinux_xfrm_skb_sid(skb, &xfrm_sid);
Paul Moorebed4d7e2013-07-23 17:38:40 -04004365 if (unlikely(err))
4366 return -EACCES;
4367 err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
4368 if (unlikely(err))
4369 return -EACCES;
Paul Moore220deb92008-01-29 08:38:23 -05004370
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004371 err = security_net_peersid_resolve(&selinux_state, nlbl_sid,
4372 nlbl_type, xfrm_sid, sid);
Paul Moore71f1cb02008-01-29 08:51:16 -05004373 if (unlikely(err)) {
peter enderborgc103a912018-06-12 10:09:03 +02004374 pr_warn(
Paul Moore71f1cb02008-01-29 08:51:16 -05004375 "SELinux: failure in selinux_skb_peerlbl_sid(),"
4376 " unable to determine packet's peer label\n");
Paul Moore220deb92008-01-29 08:38:23 -05004377 return -EACCES;
Paul Moore71f1cb02008-01-29 08:51:16 -05004378 }
Paul Moore220deb92008-01-29 08:38:23 -05004379
4380 return 0;
Paul Moore4f6a9932007-03-01 14:35:22 -05004381}
4382
Paul Moore446b8022013-12-04 16:10:51 -05004383/**
4384 * selinux_conn_sid - Determine the child socket label for a connection
4385 * @sk_sid: the parent socket's SID
4386 * @skb_sid: the packet's SID
4387 * @conn_sid: the resulting connection SID
4388 *
4389 * If @skb_sid is valid then the user:role:type information from @sk_sid is
4390 * combined with the MLS information from @skb_sid in order to create
4391 * @conn_sid. If @skb_sid is not valid then then @conn_sid is simply a copy
4392 * of @sk_sid. Returns zero on success, negative values on failure.
4393 *
4394 */
4395static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid)
4396{
4397 int err = 0;
4398
4399 if (skb_sid != SECSID_NULL)
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004400 err = security_sid_mls_copy(&selinux_state, sk_sid, skb_sid,
4401 conn_sid);
Paul Moore446b8022013-12-04 16:10:51 -05004402 else
4403 *conn_sid = sk_sid;
4404
4405 return err;
4406}
4407
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408/* socket security operations */
Paul Moored4f2d972010-04-22 14:46:18 -04004409
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004410static int socket_sockcreate_sid(const struct task_security_struct *tsec,
4411 u16 secclass, u32 *socksid)
Paul Moored4f2d972010-04-22 14:46:18 -04004412{
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004413 if (tsec->sockcreate_sid > SECSID_NULL) {
4414 *socksid = tsec->sockcreate_sid;
4415 return 0;
4416 }
4417
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004418 return security_transition_sid(&selinux_state, tsec->sid, tsec->sid,
4419 secclass, NULL, socksid);
Paul Moored4f2d972010-04-22 14:46:18 -04004420}
4421
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004422static int sock_has_perm(struct sock *sk, u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423{
Paul Moore253bfae2010-04-22 14:46:19 -04004424 struct sk_security_struct *sksec = sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004425 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004426 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427
Paul Moore253bfae2010-04-22 14:46:19 -04004428 if (sksec->sid == SECINITSID_KERNEL)
4429 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430
Eric Paris50c205f2012-04-04 15:01:43 -04004431 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004432 ad.u.net = &net;
4433 ad.u.net->sk = sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004435 return avc_has_perm(&selinux_state,
4436 current_sid(), sksec->sid, sksec->sclass, perms,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004437 &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438}
4439
4440static int selinux_socket_create(int family, int type,
4441 int protocol, int kern)
4442{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07004443 const struct task_security_struct *tsec = selinux_cred(current_cred());
Paul Moored4f2d972010-04-22 14:46:18 -04004444 u32 newsid;
David Howells275bb412008-11-14 10:39:19 +11004445 u16 secclass;
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004446 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447
4448 if (kern)
Paul Moored4f2d972010-04-22 14:46:18 -04004449 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450
David Howells275bb412008-11-14 10:39:19 +11004451 secclass = socket_type_to_security_class(family, type, protocol);
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004452 rc = socket_sockcreate_sid(tsec, secclass, &newsid);
4453 if (rc)
4454 return rc;
4455
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004456 return avc_has_perm(&selinux_state,
4457 tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458}
4459
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07004460static int selinux_socket_post_create(struct socket *sock, int family,
4461 int type, int protocol, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07004463 const struct task_security_struct *tsec = selinux_cred(current_cred());
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05004464 struct inode_security_struct *isec = inode_security_novalidate(SOCK_INODE(sock));
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004465 struct sk_security_struct *sksec;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004466 u16 sclass = socket_type_to_security_class(family, type, protocol);
4467 u32 sid = SECINITSID_KERNEL;
David Howells275bb412008-11-14 10:39:19 +11004468 int err = 0;
4469
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004470 if (!kern) {
4471 err = socket_sockcreate_sid(tsec, sclass, &sid);
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004472 if (err)
4473 return err;
4474 }
David Howells275bb412008-11-14 10:39:19 +11004475
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004476 isec->sclass = sclass;
4477 isec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004478 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004480 if (sock->sk) {
4481 sksec = sock->sk->sk_security;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004482 sksec->sclass = sclass;
4483 sksec->sid = sid;
Richard Hainesd4529302018-02-13 20:57:18 +00004484 /* Allows detection of the first association on this socket */
4485 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4486 sksec->sctp_assoc_state = SCTP_ASSOC_UNSET;
4487
Paul Moore389fb8002009-03-27 17:10:34 -04004488 err = selinux_netlbl_socket_post_create(sock->sk, family);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004489 }
4490
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07004491 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492}
4493
David Herrmann0b811db2018-05-04 16:28:21 +02004494static int selinux_socket_socketpair(struct socket *socka,
4495 struct socket *sockb)
4496{
4497 struct sk_security_struct *sksec_a = socka->sk->sk_security;
4498 struct sk_security_struct *sksec_b = sockb->sk->sk_security;
4499
4500 sksec_a->peer_sid = sksec_b->sid;
4501 sksec_b->peer_sid = sksec_a->sid;
4502
4503 return 0;
4504}
4505
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506/* Range of port numbers used to automatically bind.
4507 Need to determine whether we should perform a name_bind
4508 permission check between the socket and the port number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509
4510static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
4511{
Paul Moore253bfae2010-04-22 14:46:19 -04004512 struct sock *sk = sock->sk;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004513 struct sk_security_struct *sksec = sk->sk_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 u16 family;
4515 int err;
4516
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004517 err = sock_has_perm(sk, SOCKET__BIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 if (err)
4519 goto out;
4520
Richard Hainesd4529302018-02-13 20:57:18 +00004521 /* If PF_INET or PF_INET6, check name_bind permission for the port. */
Paul Moore253bfae2010-04-22 14:46:19 -04004522 family = sk->sk_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523 if (family == PF_INET || family == PF_INET6) {
4524 char *addrp;
Thomas Liu2bf49692009-07-14 12:14:09 -04004525 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004526 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527 struct sockaddr_in *addr4 = NULL;
4528 struct sockaddr_in6 *addr6 = NULL;
Tetsuo Handac750e692019-04-12 19:59:34 +09004529 u16 family_sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 unsigned short snum;
James Morrise399f982008-06-12 01:39:58 +10004531 u32 sid, node_perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532
Richard Hainesd4529302018-02-13 20:57:18 +00004533 /*
4534 * sctp_bindx(3) calls via selinux_sctp_bind_connect()
4535 * that validates multiple binding addresses. Because of this
4536 * need to check address->sa_family as it is possible to have
4537 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4538 */
Tetsuo Handac750e692019-04-12 19:59:34 +09004539 if (addrlen < offsetofend(struct sockaddr, sa_family))
4540 return -EINVAL;
4541 family_sa = address->sa_family;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004542 switch (family_sa) {
4543 case AF_UNSPEC:
Richard Haines68741a8a2018-03-02 19:54:34 +00004544 case AF_INET:
4545 if (addrlen < sizeof(struct sockaddr_in))
4546 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 addr4 = (struct sockaddr_in *)address;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004548 if (family_sa == AF_UNSPEC) {
4549 /* see __inet_bind(), we only want to allow
4550 * AF_UNSPEC if the address is INADDR_ANY
4551 */
4552 if (addr4->sin_addr.s_addr != htonl(INADDR_ANY))
4553 goto err_af;
4554 family_sa = AF_INET;
4555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556 snum = ntohs(addr4->sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 addrp = (char *)&addr4->sin_addr.s_addr;
Richard Haines68741a8a2018-03-02 19:54:34 +00004558 break;
4559 case AF_INET6:
4560 if (addrlen < SIN6_LEN_RFC2133)
4561 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 addr6 = (struct sockaddr_in6 *)address;
4563 snum = ntohs(addr6->sin6_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 addrp = (char *)&addr6->sin6_addr.s6_addr;
Richard Haines68741a8a2018-03-02 19:54:34 +00004565 break;
4566 default:
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004567 goto err_af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568 }
4569
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004570 ad.type = LSM_AUDIT_DATA_NET;
4571 ad.u.net = &net;
4572 ad.u.net->sport = htons(snum);
4573 ad.u.net->family = family_sa;
4574
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004575 if (snum) {
4576 int low, high;
4577
Eric W. Biederman0bbf87d2013-09-28 14:10:59 -07004578 inet_get_local_port_range(sock_net(sk), &low, &high);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004579
Krister Johansen4548b682017-01-20 17:49:11 -08004580 if (snum < max(inet_prot_sock(sock_net(sk)), low) ||
4581 snum > high) {
Paul Moore3e1121722008-04-10 10:48:14 -04004582 err = sel_netport_sid(sk->sk_protocol,
4583 snum, &sid);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004584 if (err)
4585 goto out;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004586 err = avc_has_perm(&selinux_state,
4587 sksec->sid, sid,
Paul Moore253bfae2010-04-22 14:46:19 -04004588 sksec->sclass,
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004589 SOCKET__NAME_BIND, &ad);
4590 if (err)
4591 goto out;
4592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 }
Eric Paris828dfe12008-04-17 13:17:49 -04004594
Paul Moore253bfae2010-04-22 14:46:19 -04004595 switch (sksec->sclass) {
James Morris13402582005-09-30 14:24:34 -04004596 case SECCLASS_TCP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 node_perm = TCP_SOCKET__NODE_BIND;
4598 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004599
James Morris13402582005-09-30 14:24:34 -04004600 case SECCLASS_UDP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601 node_perm = UDP_SOCKET__NODE_BIND;
4602 break;
James Morris2ee92d42006-11-13 16:09:01 -08004603
4604 case SECCLASS_DCCP_SOCKET:
4605 node_perm = DCCP_SOCKET__NODE_BIND;
4606 break;
4607
Richard Hainesd4529302018-02-13 20:57:18 +00004608 case SECCLASS_SCTP_SOCKET:
4609 node_perm = SCTP_SOCKET__NODE_BIND;
4610 break;
4611
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 default:
4613 node_perm = RAWIP_SOCKET__NODE_BIND;
4614 break;
4615 }
Eric Paris828dfe12008-04-17 13:17:49 -04004616
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004617 err = sel_netnode_sid(addrp, family_sa, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 if (err)
4619 goto out;
Eric Paris828dfe12008-04-17 13:17:49 -04004620
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004621 if (family_sa == AF_INET)
Eric Paris48c62af2012-04-02 13:15:44 -04004622 ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623 else
Eric Paris48c62af2012-04-02 13:15:44 -04004624 ad.u.net->v6info.saddr = addr6->sin6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004626 err = avc_has_perm(&selinux_state,
4627 sksec->sid, sid,
Paul Moore253bfae2010-04-22 14:46:19 -04004628 sksec->sclass, node_perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629 if (err)
4630 goto out;
4631 }
4632out:
4633 return err;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004634err_af:
4635 /* Note that SCTP services expect -EINVAL, others -EAFNOSUPPORT. */
4636 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4637 return -EINVAL;
4638 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639}
4640
Richard Hainesd4529302018-02-13 20:57:18 +00004641/* This supports connect(2) and SCTP connect services such as sctp_connectx(3)
Kees Cookd61330c2019-02-17 14:08:36 -08004642 * and sctp_sendmsg(3) as described in Documentation/security/SCTP.rst
Richard Hainesd4529302018-02-13 20:57:18 +00004643 */
4644static int selinux_socket_connect_helper(struct socket *sock,
4645 struct sockaddr *address, int addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646{
Paul Moore014ab192008-10-10 10:16:33 -04004647 struct sock *sk = sock->sk;
Paul Moore253bfae2010-04-22 14:46:19 -04004648 struct sk_security_struct *sksec = sk->sk_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 int err;
4650
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004651 err = sock_has_perm(sk, SOCKET__CONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652 if (err)
4653 return err;
Paolo Abeni05174c92019-05-10 19:12:33 +02004654 if (addrlen < offsetofend(struct sockaddr, sa_family))
4655 return -EINVAL;
4656
4657 /* connect(AF_UNSPEC) has special handling, as it is a documented
4658 * way to disconnect the socket
4659 */
4660 if (address->sa_family == AF_UNSPEC)
4661 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662
4663 /*
Richard Hainesd4529302018-02-13 20:57:18 +00004664 * If a TCP, DCCP or SCTP socket, check name_connect permission
4665 * for the port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 */
Paul Moore253bfae2010-04-22 14:46:19 -04004667 if (sksec->sclass == SECCLASS_TCP_SOCKET ||
Richard Hainesd4529302018-02-13 20:57:18 +00004668 sksec->sclass == SECCLASS_DCCP_SOCKET ||
4669 sksec->sclass == SECCLASS_SCTP_SOCKET) {
Thomas Liu2bf49692009-07-14 12:14:09 -04004670 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004671 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672 struct sockaddr_in *addr4 = NULL;
4673 struct sockaddr_in6 *addr6 = NULL;
4674 unsigned short snum;
James Morris2ee92d42006-11-13 16:09:01 -08004675 u32 sid, perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676
Richard Hainesd4529302018-02-13 20:57:18 +00004677 /* sctp_connectx(3) calls via selinux_sctp_bind_connect()
4678 * that validates multiple connect addresses. Because of this
4679 * need to check address->sa_family as it is possible to have
4680 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4681 */
Richard Haines68741a8a2018-03-02 19:54:34 +00004682 switch (address->sa_family) {
4683 case AF_INET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 addr4 = (struct sockaddr_in *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004685 if (addrlen < sizeof(struct sockaddr_in))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686 return -EINVAL;
4687 snum = ntohs(addr4->sin_port);
Richard Haines68741a8a2018-03-02 19:54:34 +00004688 break;
4689 case AF_INET6:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690 addr6 = (struct sockaddr_in6 *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004691 if (addrlen < SIN6_LEN_RFC2133)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 return -EINVAL;
4693 snum = ntohs(addr6->sin6_port);
Richard Haines68741a8a2018-03-02 19:54:34 +00004694 break;
4695 default:
4696 /* Note that SCTP services expect -EINVAL, whereas
4697 * others expect -EAFNOSUPPORT.
4698 */
4699 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4700 return -EINVAL;
4701 else
4702 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 }
4704
Paul Moore3e1121722008-04-10 10:48:14 -04004705 err = sel_netport_sid(sk->sk_protocol, snum, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 if (err)
Richard Hainesd4529302018-02-13 20:57:18 +00004707 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708
Richard Hainesd4529302018-02-13 20:57:18 +00004709 switch (sksec->sclass) {
4710 case SECCLASS_TCP_SOCKET:
4711 perm = TCP_SOCKET__NAME_CONNECT;
4712 break;
4713 case SECCLASS_DCCP_SOCKET:
4714 perm = DCCP_SOCKET__NAME_CONNECT;
4715 break;
4716 case SECCLASS_SCTP_SOCKET:
4717 perm = SCTP_SOCKET__NAME_CONNECT;
4718 break;
4719 }
James Morris2ee92d42006-11-13 16:09:01 -08004720
Eric Paris50c205f2012-04-04 15:01:43 -04004721 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004722 ad.u.net = &net;
4723 ad.u.net->dport = htons(snum);
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004724 ad.u.net->family = address->sa_family;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004725 err = avc_has_perm(&selinux_state,
4726 sksec->sid, sid, sksec->sclass, perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727 if (err)
Richard Hainesd4529302018-02-13 20:57:18 +00004728 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 }
4730
Richard Hainesd4529302018-02-13 20:57:18 +00004731 return 0;
4732}
Paul Moore014ab192008-10-10 10:16:33 -04004733
Richard Hainesd4529302018-02-13 20:57:18 +00004734/* Supports connect(2), see comments in selinux_socket_connect_helper() */
4735static int selinux_socket_connect(struct socket *sock,
4736 struct sockaddr *address, int addrlen)
4737{
4738 int err;
4739 struct sock *sk = sock->sk;
4740
4741 err = selinux_socket_connect_helper(sock, address, addrlen);
4742 if (err)
4743 return err;
4744
4745 return selinux_netlbl_socket_connect(sk, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746}
4747
4748static int selinux_socket_listen(struct socket *sock, int backlog)
4749{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004750 return sock_has_perm(sock->sk, SOCKET__LISTEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751}
4752
4753static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
4754{
4755 int err;
4756 struct inode_security_struct *isec;
4757 struct inode_security_struct *newisec;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004758 u16 sclass;
4759 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004761 err = sock_has_perm(sock->sk, SOCKET__ACCEPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762 if (err)
4763 return err;
4764
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05004765 isec = inode_security_novalidate(SOCK_INODE(sock));
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004766 spin_lock(&isec->lock);
4767 sclass = isec->sclass;
4768 sid = isec->sid;
4769 spin_unlock(&isec->lock);
4770
4771 newisec = inode_security_novalidate(SOCK_INODE(newsock));
4772 newisec->sclass = sclass;
4773 newisec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004774 newisec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775
4776 return 0;
4777}
4778
4779static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
Eric Paris828dfe12008-04-17 13:17:49 -04004780 int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004782 return sock_has_perm(sock->sk, SOCKET__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783}
4784
4785static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
4786 int size, int flags)
4787{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004788 return sock_has_perm(sock->sk, SOCKET__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789}
4790
4791static int selinux_socket_getsockname(struct socket *sock)
4792{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004793 return sock_has_perm(sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794}
4795
4796static int selinux_socket_getpeername(struct socket *sock)
4797{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004798 return sock_has_perm(sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799}
4800
Eric Paris828dfe12008-04-17 13:17:49 -04004801static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802{
Paul Mooref8687af2006-10-30 15:22:15 -08004803 int err;
4804
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004805 err = sock_has_perm(sock->sk, SOCKET__SETOPT);
Paul Mooref8687af2006-10-30 15:22:15 -08004806 if (err)
4807 return err;
4808
4809 return selinux_netlbl_socket_setsockopt(sock, level, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810}
4811
4812static int selinux_socket_getsockopt(struct socket *sock, int level,
4813 int optname)
4814{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004815 return sock_has_perm(sock->sk, SOCKET__GETOPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816}
4817
4818static int selinux_socket_shutdown(struct socket *sock, int how)
4819{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004820 return sock_has_perm(sock->sk, SOCKET__SHUTDOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821}
4822
David S. Miller3610cda2011-01-05 15:38:53 -08004823static int selinux_socket_unix_stream_connect(struct sock *sock,
4824 struct sock *other,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825 struct sock *newsk)
4826{
David S. Miller3610cda2011-01-05 15:38:53 -08004827 struct sk_security_struct *sksec_sock = sock->sk_security;
4828 struct sk_security_struct *sksec_other = other->sk_security;
Paul Moore4d1e2452010-04-22 14:46:18 -04004829 struct sk_security_struct *sksec_new = newsk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004830 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004831 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832 int err;
4833
Eric Paris50c205f2012-04-04 15:01:43 -04004834 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004835 ad.u.net = &net;
4836 ad.u.net->sk = other;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004838 err = avc_has_perm(&selinux_state,
4839 sksec_sock->sid, sksec_other->sid,
Paul Moore4d1e2452010-04-22 14:46:18 -04004840 sksec_other->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841 UNIX_STREAM_SOCKET__CONNECTTO, &ad);
4842 if (err)
4843 return err;
4844
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845 /* server child socket */
Paul Moore4d1e2452010-04-22 14:46:18 -04004846 sksec_new->peer_sid = sksec_sock->sid;
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004847 err = security_sid_mls_copy(&selinux_state, sksec_other->sid,
4848 sksec_sock->sid, &sksec_new->sid);
Paul Moore4d1e2452010-04-22 14:46:18 -04004849 if (err)
4850 return err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004851
Paul Moore4d1e2452010-04-22 14:46:18 -04004852 /* connecting socket */
4853 sksec_sock->peer_sid = sksec_new->sid;
4854
4855 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856}
4857
4858static int selinux_socket_unix_may_send(struct socket *sock,
4859 struct socket *other)
4860{
Paul Moore253bfae2010-04-22 14:46:19 -04004861 struct sk_security_struct *ssec = sock->sk->sk_security;
4862 struct sk_security_struct *osec = other->sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004863 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004864 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865
Eric Paris50c205f2012-04-04 15:01:43 -04004866 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004867 ad.u.net = &net;
4868 ad.u.net->sk = other->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004870 return avc_has_perm(&selinux_state,
4871 ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
Paul Moore253bfae2010-04-22 14:46:19 -04004872 &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873}
4874
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004875static int selinux_inet_sys_rcv_skb(struct net *ns, int ifindex,
4876 char *addrp, u16 family, u32 peer_sid,
Thomas Liu2bf49692009-07-14 12:14:09 -04004877 struct common_audit_data *ad)
Paul Mooreeffad8d2008-01-29 08:49:27 -05004878{
4879 int err;
4880 u32 if_sid;
4881 u32 node_sid;
4882
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004883 err = sel_netif_sid(ns, ifindex, &if_sid);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004884 if (err)
4885 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004886 err = avc_has_perm(&selinux_state,
4887 peer_sid, if_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004888 SECCLASS_NETIF, NETIF__INGRESS, ad);
4889 if (err)
4890 return err;
4891
4892 err = sel_netnode_sid(addrp, family, &node_sid);
4893 if (err)
4894 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004895 return avc_has_perm(&selinux_state,
4896 peer_sid, node_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004897 SECCLASS_NODE, NODE__RECVFROM, ad);
4898}
4899
Paul Moore220deb92008-01-29 08:38:23 -05004900static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
Paul Moored8395c82008-10-10 10:16:30 -04004901 u16 family)
Paul Moore220deb92008-01-29 08:38:23 -05004902{
Paul Moore277d3422008-12-31 12:54:11 -05004903 int err = 0;
Paul Moore220deb92008-01-29 08:38:23 -05004904 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004905 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004906 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004907 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04004908 char *addrp;
4909
Eric Paris50c205f2012-04-04 15:01:43 -04004910 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004911 ad.u.net = &net;
4912 ad.u.net->netif = skb->skb_iif;
4913 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004914 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4915 if (err)
4916 return err;
Paul Moore220deb92008-01-29 08:38:23 -05004917
Paul Moore58bfbb52009-03-27 17:10:41 -04004918 if (selinux_secmark_enabled()) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004919 err = avc_has_perm(&selinux_state,
4920 sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Moored8395c82008-10-10 10:16:30 -04004921 PACKET__RECV, &ad);
Paul Moore58bfbb52009-03-27 17:10:41 -04004922 if (err)
4923 return err;
4924 }
Paul Moore220deb92008-01-29 08:38:23 -05004925
Steffen Klassertb9679a72011-02-23 12:55:21 +01004926 err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
4927 if (err)
4928 return err;
4929 err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004930
James Morris4e5ab4c2006-06-09 00:33:33 -07004931 return err;
4932}
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004933
James Morris4e5ab4c2006-06-09 00:33:33 -07004934static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
4935{
Paul Moore220deb92008-01-29 08:38:23 -05004936 int err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004937 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004938 u16 family = sk->sk_family;
4939 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004940 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004941 struct lsm_network_audit net = {0,};
Paul Moore220deb92008-01-29 08:38:23 -05004942 char *addrp;
Paul Moored8395c82008-10-10 10:16:30 -04004943 u8 secmark_active;
4944 u8 peerlbl_active;
James Morris4e5ab4c2006-06-09 00:33:33 -07004945
James Morris4e5ab4c2006-06-09 00:33:33 -07004946 if (family != PF_INET && family != PF_INET6)
Paul Moore220deb92008-01-29 08:38:23 -05004947 return 0;
James Morris4e5ab4c2006-06-09 00:33:33 -07004948
4949 /* Handle mapped IPv4 packets arriving via IPv6 sockets */
Al Viro87fcd702006-12-04 22:00:55 +00004950 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
James Morris4e5ab4c2006-06-09 00:33:33 -07004951 family = PF_INET;
4952
Paul Moored8395c82008-10-10 10:16:30 -04004953 /* If any sort of compatibility mode is enabled then handoff processing
4954 * to the selinux_sock_rcv_skb_compat() function to deal with the
4955 * special handling. We do this in an attempt to keep this function
4956 * as fast and as clean as possible. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004957 if (!selinux_policycap_netpeer())
Paul Moored8395c82008-10-10 10:16:30 -04004958 return selinux_sock_rcv_skb_compat(sk, skb, family);
4959
4960 secmark_active = selinux_secmark_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04004961 peerlbl_active = selinux_peerlbl_enabled();
Paul Moored8395c82008-10-10 10:16:30 -04004962 if (!secmark_active && !peerlbl_active)
4963 return 0;
4964
Eric Paris50c205f2012-04-04 15:01:43 -04004965 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004966 ad.u.net = &net;
4967 ad.u.net->netif = skb->skb_iif;
4968 ad.u.net->family = family;
Paul Moore224dfbd2008-01-29 08:38:13 -05004969 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
James Morris4e5ab4c2006-06-09 00:33:33 -07004970 if (err)
Paul Moore220deb92008-01-29 08:38:23 -05004971 return err;
James Morris4e5ab4c2006-06-09 00:33:33 -07004972
Paul Moored8395c82008-10-10 10:16:30 -04004973 if (peerlbl_active) {
Paul Moored621d352008-01-29 08:43:36 -05004974 u32 peer_sid;
4975
4976 err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
4977 if (err)
4978 return err;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004979 err = selinux_inet_sys_rcv_skb(sock_net(sk), skb->skb_iif,
4980 addrp, family, peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04004981 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04004982 selinux_netlbl_err(skb, family, err, 0);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004983 return err;
Paul Mooredfaebe92008-10-10 10:16:31 -04004984 }
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004985 err = avc_has_perm(&selinux_state,
4986 sk_sid, peer_sid, SECCLASS_PEER,
Paul Moored621d352008-01-29 08:43:36 -05004987 PEER__RECV, &ad);
Chad Hanson46d01d62013-12-23 17:45:01 -05004988 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04004989 selinux_netlbl_err(skb, family, err, 0);
Chad Hanson46d01d62013-12-23 17:45:01 -05004990 return err;
4991 }
Paul Moored621d352008-01-29 08:43:36 -05004992 }
4993
Paul Moored8395c82008-10-10 10:16:30 -04004994 if (secmark_active) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004995 err = avc_has_perm(&selinux_state,
4996 sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004997 PACKET__RECV, &ad);
4998 if (err)
4999 return err;
5000 }
5001
Paul Moored621d352008-01-29 08:43:36 -05005002 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003}
5004
Catherine Zhang2c7946a2006-03-20 22:41:23 -08005005static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
5006 int __user *optlen, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007{
5008 int err = 0;
5009 char *scontext;
5010 u32 scontext_len;
Paul Moore253bfae2010-04-22 14:46:19 -04005011 struct sk_security_struct *sksec = sock->sk->sk_security;
Paul Moore3de4bab2006-11-17 17:38:54 -05005012 u32 peer_sid = SECSID_NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013
Paul Moore253bfae2010-04-22 14:46:19 -04005014 if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
Richard Hainesd4529302018-02-13 20:57:18 +00005015 sksec->sclass == SECCLASS_TCP_SOCKET ||
5016 sksec->sclass == SECCLASS_SCTP_SOCKET)
Eric Parisdd3e7832010-04-07 15:08:46 -04005017 peer_sid = sksec->peer_sid;
Paul Moore253bfae2010-04-22 14:46:19 -04005018 if (peer_sid == SECSID_NULL)
5019 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005021 err = security_sid_to_context(&selinux_state, peer_sid, &scontext,
5022 &scontext_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023 if (err)
Paul Moore253bfae2010-04-22 14:46:19 -04005024 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025
5026 if (scontext_len > len) {
5027 err = -ERANGE;
5028 goto out_len;
5029 }
5030
5031 if (copy_to_user(optval, scontext, scontext_len))
5032 err = -EFAULT;
5033
5034out_len:
5035 if (put_user(scontext_len, optlen))
5036 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 kfree(scontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 return err;
5039}
5040
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005041static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
Catherine Zhang2c7946a2006-03-20 22:41:23 -08005042{
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005043 u32 peer_secid = SECSID_NULL;
Paul Moore75e22912008-01-29 08:38:04 -05005044 u16 family;
Paul Moore899134f2016-03-28 15:19:10 -04005045 struct inode_security_struct *isec;
Catherine Zhang877ce7c2006-06-29 12:27:47 -07005046
Paul Mooreaa862902008-10-10 10:16:29 -04005047 if (skb && skb->protocol == htons(ETH_P_IP))
5048 family = PF_INET;
5049 else if (skb && skb->protocol == htons(ETH_P_IPV6))
5050 family = PF_INET6;
5051 else if (sock)
Paul Moore75e22912008-01-29 08:38:04 -05005052 family = sock->sk->sk_family;
Paul Moore75e22912008-01-29 08:38:04 -05005053 else
5054 goto out;
5055
Paul Moore899134f2016-03-28 15:19:10 -04005056 if (sock && family == PF_UNIX) {
5057 isec = inode_security_novalidate(SOCK_INODE(sock));
5058 peer_secid = isec->sid;
5059 } else if (skb)
Paul Moore220deb92008-01-29 08:38:23 -05005060 selinux_skb_peerlbl_sid(skb, family, &peer_secid);
Catherine Zhang2c7946a2006-03-20 22:41:23 -08005061
Paul Moore75e22912008-01-29 08:38:04 -05005062out:
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005063 *secid = peer_secid;
Paul Moore75e22912008-01-29 08:38:04 -05005064 if (peer_secid == SECSID_NULL)
5065 return -EINVAL;
5066 return 0;
Catherine Zhang2c7946a2006-03-20 22:41:23 -08005067}
5068
Al Viro7d877f32005-10-21 03:20:43 -04005069static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070{
Paul Moore84914b72010-04-22 14:46:18 -04005071 struct sk_security_struct *sksec;
5072
5073 sksec = kzalloc(sizeof(*sksec), priority);
5074 if (!sksec)
5075 return -ENOMEM;
5076
5077 sksec->peer_sid = SECINITSID_UNLABELED;
5078 sksec->sid = SECINITSID_UNLABELED;
Stephen Smalley5dee25d2015-07-10 17:19:57 -04005079 sksec->sclass = SECCLASS_SOCKET;
Paul Moore84914b72010-04-22 14:46:18 -04005080 selinux_netlbl_sk_security_reset(sksec);
5081 sk->sk_security = sksec;
5082
5083 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084}
5085
5086static void selinux_sk_free_security(struct sock *sk)
5087{
Paul Moore84914b72010-04-22 14:46:18 -04005088 struct sk_security_struct *sksec = sk->sk_security;
5089
5090 sk->sk_security = NULL;
5091 selinux_netlbl_sk_security_free(sksec);
5092 kfree(sksec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093}
5094
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005095static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
5096{
Eric Parisdd3e7832010-04-07 15:08:46 -04005097 struct sk_security_struct *sksec = sk->sk_security;
5098 struct sk_security_struct *newsksec = newsk->sk_security;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005099
Eric Parisdd3e7832010-04-07 15:08:46 -04005100 newsksec->sid = sksec->sid;
5101 newsksec->peer_sid = sksec->peer_sid;
5102 newsksec->sclass = sksec->sclass;
Paul Moore99f59ed2006-08-29 17:53:48 -07005103
Eric Parisdd3e7832010-04-07 15:08:46 -04005104 selinux_netlbl_sk_security_reset(newsksec);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005105}
5106
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07005107static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005108{
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005109 if (!sk)
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07005110 *secid = SECINITSID_ANY_SOCKET;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005111 else {
5112 struct sk_security_struct *sksec = sk->sk_security;
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005113
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07005114 *secid = sksec->sid;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005115 }
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005116}
5117
Eric Paris828dfe12008-04-17 13:17:49 -04005118static void selinux_sock_graft(struct sock *sk, struct socket *parent)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005119{
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05005120 struct inode_security_struct *isec =
5121 inode_security_novalidate(SOCK_INODE(parent));
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005122 struct sk_security_struct *sksec = sk->sk_security;
5123
Paul Moore2873ead2014-07-28 10:42:48 -04005124 if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
5125 sk->sk_family == PF_UNIX)
David Woodhouse2148ccc2006-09-29 15:50:25 -07005126 isec->sid = sksec->sid;
Paul Moore220deb92008-01-29 08:38:23 -05005127 sksec->sclass = isec->sclass;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005128}
5129
Richard Hainesd4529302018-02-13 20:57:18 +00005130/* Called whenever SCTP receives an INIT chunk. This happens when an incoming
5131 * connect(2), sctp_connectx(3) or sctp_sendmsg(3) (with no association
5132 * already present).
5133 */
5134static int selinux_sctp_assoc_request(struct sctp_endpoint *ep,
5135 struct sk_buff *skb)
5136{
5137 struct sk_security_struct *sksec = ep->base.sk->sk_security;
5138 struct common_audit_data ad;
5139 struct lsm_network_audit net = {0,};
5140 u8 peerlbl_active;
5141 u32 peer_sid = SECINITSID_UNLABELED;
5142 u32 conn_sid;
5143 int err = 0;
5144
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005145 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005146 return 0;
5147
5148 peerlbl_active = selinux_peerlbl_enabled();
5149
5150 if (peerlbl_active) {
5151 /* This will return peer_sid = SECSID_NULL if there are
5152 * no peer labels, see security_net_peersid_resolve().
5153 */
5154 err = selinux_skb_peerlbl_sid(skb, ep->base.sk->sk_family,
5155 &peer_sid);
5156 if (err)
5157 return err;
5158
5159 if (peer_sid == SECSID_NULL)
5160 peer_sid = SECINITSID_UNLABELED;
5161 }
5162
5163 if (sksec->sctp_assoc_state == SCTP_ASSOC_UNSET) {
5164 sksec->sctp_assoc_state = SCTP_ASSOC_SET;
5165
5166 /* Here as first association on socket. As the peer SID
5167 * was allowed by peer recv (and the netif/node checks),
5168 * then it is approved by policy and used as the primary
5169 * peer SID for getpeercon(3).
5170 */
5171 sksec->peer_sid = peer_sid;
5172 } else if (sksec->peer_sid != peer_sid) {
5173 /* Other association peer SIDs are checked to enforce
5174 * consistency among the peer SIDs.
5175 */
5176 ad.type = LSM_AUDIT_DATA_NET;
5177 ad.u.net = &net;
5178 ad.u.net->sk = ep->base.sk;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005179 err = avc_has_perm(&selinux_state,
5180 sksec->peer_sid, peer_sid, sksec->sclass,
Richard Hainesd4529302018-02-13 20:57:18 +00005181 SCTP_SOCKET__ASSOCIATION, &ad);
5182 if (err)
5183 return err;
5184 }
5185
5186 /* Compute the MLS component for the connection and store
5187 * the information in ep. This will be used by SCTP TCP type
5188 * sockets and peeled off connections as they cause a new
5189 * socket to be generated. selinux_sctp_sk_clone() will then
5190 * plug this into the new socket.
5191 */
5192 err = selinux_conn_sid(sksec->sid, peer_sid, &conn_sid);
5193 if (err)
5194 return err;
5195
5196 ep->secid = conn_sid;
5197 ep->peer_secid = peer_sid;
5198
5199 /* Set any NetLabel labels including CIPSO/CALIPSO options. */
5200 return selinux_netlbl_sctp_assoc_request(ep, skb);
5201}
5202
5203/* Check if sctp IPv4/IPv6 addresses are valid for binding or connecting
5204 * based on their @optname.
5205 */
5206static int selinux_sctp_bind_connect(struct sock *sk, int optname,
5207 struct sockaddr *address,
5208 int addrlen)
5209{
5210 int len, err = 0, walk_size = 0;
5211 void *addr_buf;
5212 struct sockaddr *addr;
5213 struct socket *sock;
5214
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005215 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005216 return 0;
5217
5218 /* Process one or more addresses that may be IPv4 or IPv6 */
5219 sock = sk->sk_socket;
5220 addr_buf = address;
5221
5222 while (walk_size < addrlen) {
Ondrej Mosnacekc1383252018-11-13 16:16:08 +01005223 if (walk_size + sizeof(sa_family_t) > addrlen)
5224 return -EINVAL;
5225
Richard Hainesd4529302018-02-13 20:57:18 +00005226 addr = addr_buf;
5227 switch (addr->sa_family) {
Alexey Kodanev4152dc92018-05-11 20:15:13 +03005228 case AF_UNSPEC:
Richard Hainesd4529302018-02-13 20:57:18 +00005229 case AF_INET:
5230 len = sizeof(struct sockaddr_in);
5231 break;
5232 case AF_INET6:
5233 len = sizeof(struct sockaddr_in6);
5234 break;
5235 default:
Alexey Kodanev4152dc92018-05-11 20:15:13 +03005236 return -EINVAL;
Richard Hainesd4529302018-02-13 20:57:18 +00005237 }
5238
Xin Long292c9972019-03-09 00:07:34 +08005239 if (walk_size + len > addrlen)
5240 return -EINVAL;
5241
Richard Hainesd4529302018-02-13 20:57:18 +00005242 err = -EINVAL;
5243 switch (optname) {
5244 /* Bind checks */
5245 case SCTP_PRIMARY_ADDR:
5246 case SCTP_SET_PEER_PRIMARY_ADDR:
5247 case SCTP_SOCKOPT_BINDX_ADD:
5248 err = selinux_socket_bind(sock, addr, len);
5249 break;
5250 /* Connect checks */
5251 case SCTP_SOCKOPT_CONNECTX:
5252 case SCTP_PARAM_SET_PRIMARY:
5253 case SCTP_PARAM_ADD_IP:
5254 case SCTP_SENDMSG_CONNECT:
5255 err = selinux_socket_connect_helper(sock, addr, len);
5256 if (err)
5257 return err;
5258
5259 /* As selinux_sctp_bind_connect() is called by the
5260 * SCTP protocol layer, the socket is already locked,
5261 * therefore selinux_netlbl_socket_connect_locked() is
5262 * is called here. The situations handled are:
5263 * sctp_connectx(3), sctp_sendmsg(3), sendmsg(2),
5264 * whenever a new IP address is added or when a new
5265 * primary address is selected.
5266 * Note that an SCTP connect(2) call happens before
5267 * the SCTP protocol layer and is handled via
5268 * selinux_socket_connect().
5269 */
5270 err = selinux_netlbl_socket_connect_locked(sk, addr);
5271 break;
5272 }
5273
5274 if (err)
5275 return err;
5276
5277 addr_buf += len;
5278 walk_size += len;
5279 }
5280
5281 return 0;
5282}
5283
5284/* Called whenever a new socket is created by accept(2) or sctp_peeloff(3). */
5285static void selinux_sctp_sk_clone(struct sctp_endpoint *ep, struct sock *sk,
5286 struct sock *newsk)
5287{
5288 struct sk_security_struct *sksec = sk->sk_security;
5289 struct sk_security_struct *newsksec = newsk->sk_security;
5290
5291 /* If policy does not support SECCLASS_SCTP_SOCKET then call
5292 * the non-sctp clone version.
5293 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005294 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005295 return selinux_sk_clone_security(sk, newsk);
5296
5297 newsksec->sid = ep->secid;
5298 newsksec->peer_sid = ep->peer_secid;
5299 newsksec->sclass = sksec->sclass;
5300 selinux_netlbl_sctp_sk_clone(sk, newsk);
5301}
5302
Adrian Bunk9a673e52006-08-15 00:03:53 -07005303static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
5304 struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005305{
5306 struct sk_security_struct *sksec = sk->sk_security;
5307 int err;
Paul Moore0b1f24e2013-12-03 11:39:13 -05005308 u16 family = req->rsk_ops->family;
Paul Moore446b8022013-12-04 16:10:51 -05005309 u32 connsid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005310 u32 peersid;
5311
Paul Mooreaa862902008-10-10 10:16:29 -04005312 err = selinux_skb_peerlbl_sid(skb, family, &peersid);
Paul Moore220deb92008-01-29 08:38:23 -05005313 if (err)
5314 return err;
Paul Moore446b8022013-12-04 16:10:51 -05005315 err = selinux_conn_sid(sksec->sid, peersid, &connsid);
5316 if (err)
5317 return err;
5318 req->secid = connsid;
5319 req->peer_secid = peersid;
Venkat Yekkiralaa51c64f2006-07-27 22:01:34 -07005320
Paul Moore389fb8002009-03-27 17:10:34 -04005321 return selinux_netlbl_inet_conn_request(req, family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005322}
5323
Adrian Bunk9a673e52006-08-15 00:03:53 -07005324static void selinux_inet_csk_clone(struct sock *newsk,
5325 const struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005326{
5327 struct sk_security_struct *newsksec = newsk->sk_security;
5328
5329 newsksec->sid = req->secid;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005330 newsksec->peer_sid = req->peer_secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005331 /* NOTE: Ideally, we should also get the isec->sid for the
5332 new socket in sync, but we don't have the isec available yet.
5333 So we will wait until sock_graft to do it, by which
5334 time it will have been created and available. */
Paul Moore99f59ed2006-08-29 17:53:48 -07005335
Paul Moore9f2ad662006-11-17 17:38:53 -05005336 /* We don't need to take any sort of lock here as we are the only
5337 * thread with access to newsksec */
Paul Moore389fb8002009-03-27 17:10:34 -04005338 selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005339}
5340
Paul Moore014ab192008-10-10 10:16:33 -04005341static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005342{
Paul Mooreaa862902008-10-10 10:16:29 -04005343 u16 family = sk->sk_family;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005344 struct sk_security_struct *sksec = sk->sk_security;
5345
Paul Mooreaa862902008-10-10 10:16:29 -04005346 /* handle mapped IPv4 packets arriving via IPv6 sockets */
5347 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
5348 family = PF_INET;
5349
5350 selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005351}
5352
Eric Paris2606fd12010-10-13 16:24:41 -04005353static int selinux_secmark_relabel_packet(u32 sid)
5354{
5355 const struct task_security_struct *__tsec;
5356 u32 tsid;
5357
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07005358 __tsec = selinux_cred(current_cred());
Eric Paris2606fd12010-10-13 16:24:41 -04005359 tsid = __tsec->sid;
5360
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005361 return avc_has_perm(&selinux_state,
5362 tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO,
5363 NULL);
Eric Paris2606fd12010-10-13 16:24:41 -04005364}
5365
5366static void selinux_secmark_refcount_inc(void)
5367{
5368 atomic_inc(&selinux_secmark_refcount);
5369}
5370
5371static void selinux_secmark_refcount_dec(void)
5372{
5373 atomic_dec(&selinux_secmark_refcount);
5374}
5375
Adrian Bunk9a673e52006-08-15 00:03:53 -07005376static void selinux_req_classify_flow(const struct request_sock *req,
5377 struct flowi *fl)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005378{
David S. Miller1d28f422011-03-12 00:29:39 -05005379 fl->flowi_secid = req->secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005380}
5381
Paul Moore5dbbaf22013-01-14 07:12:19 +00005382static int selinux_tun_dev_alloc_security(void **security)
5383{
5384 struct tun_security_struct *tunsec;
5385
5386 tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL);
5387 if (!tunsec)
5388 return -ENOMEM;
5389 tunsec->sid = current_sid();
5390
5391 *security = tunsec;
5392 return 0;
5393}
5394
5395static void selinux_tun_dev_free_security(void *security)
5396{
5397 kfree(security);
5398}
5399
Paul Mooreed6d76e2009-08-28 18:12:49 -04005400static int selinux_tun_dev_create(void)
5401{
5402 u32 sid = current_sid();
5403
5404 /* we aren't taking into account the "sockcreate" SID since the socket
5405 * that is being created here is not a socket in the traditional sense,
5406 * instead it is a private sock, accessible only to the kernel, and
5407 * representing a wide range of network traffic spanning multiple
5408 * connections unlike traditional sockets - check the TUN driver to
5409 * get a better understanding of why this socket is special */
5410
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005411 return avc_has_perm(&selinux_state,
5412 sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005413 NULL);
5414}
5415
Paul Moore5dbbaf22013-01-14 07:12:19 +00005416static int selinux_tun_dev_attach_queue(void *security)
Paul Mooreed6d76e2009-08-28 18:12:49 -04005417{
Paul Moore5dbbaf22013-01-14 07:12:19 +00005418 struct tun_security_struct *tunsec = security;
5419
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005420 return avc_has_perm(&selinux_state,
5421 current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET,
Paul Moore5dbbaf22013-01-14 07:12:19 +00005422 TUN_SOCKET__ATTACH_QUEUE, NULL);
5423}
5424
5425static int selinux_tun_dev_attach(struct sock *sk, void *security)
5426{
5427 struct tun_security_struct *tunsec = security;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005428 struct sk_security_struct *sksec = sk->sk_security;
5429
5430 /* we don't currently perform any NetLabel based labeling here and it
5431 * isn't clear that we would want to do so anyway; while we could apply
5432 * labeling without the support of the TUN user the resulting labeled
5433 * traffic from the other end of the connection would almost certainly
5434 * cause confusion to the TUN user that had no idea network labeling
5435 * protocols were being used */
5436
Paul Moore5dbbaf22013-01-14 07:12:19 +00005437 sksec->sid = tunsec->sid;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005438 sksec->sclass = SECCLASS_TUN_SOCKET;
Paul Moore5dbbaf22013-01-14 07:12:19 +00005439
5440 return 0;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005441}
5442
Paul Moore5dbbaf22013-01-14 07:12:19 +00005443static int selinux_tun_dev_open(void *security)
Paul Mooreed6d76e2009-08-28 18:12:49 -04005444{
Paul Moore5dbbaf22013-01-14 07:12:19 +00005445 struct tun_security_struct *tunsec = security;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005446 u32 sid = current_sid();
5447 int err;
5448
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005449 err = avc_has_perm(&selinux_state,
5450 sid, tunsec->sid, SECCLASS_TUN_SOCKET,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005451 TUN_SOCKET__RELABELFROM, NULL);
5452 if (err)
5453 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005454 err = avc_has_perm(&selinux_state,
5455 sid, sid, SECCLASS_TUN_SOCKET,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005456 TUN_SOCKET__RELABELTO, NULL);
5457 if (err)
5458 return err;
Paul Moore5dbbaf22013-01-14 07:12:19 +00005459 tunsec->sid = sid;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005460
5461 return 0;
5462}
5463
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
5465{
5466 int err = 0;
5467 u32 perm;
5468 struct nlmsghdr *nlh;
Paul Moore253bfae2010-04-22 14:46:19 -04005469 struct sk_security_struct *sksec = sk->sk_security;
Eric Paris828dfe12008-04-17 13:17:49 -04005470
Hong zhi guo77954982013-03-27 06:49:35 +00005471 if (skb->len < NLMSG_HDRLEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 err = -EINVAL;
5473 goto out;
5474 }
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -07005475 nlh = nlmsg_hdr(skb);
Eric Paris828dfe12008-04-17 13:17:49 -04005476
Paul Moore253bfae2010-04-22 14:46:19 -04005477 err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 if (err) {
5479 if (err == -EINVAL) {
Vladis Dronov76319942015-12-24 11:09:41 -05005480 pr_warn_ratelimited("SELinux: unrecognized netlink"
5481 " message: protocol=%hu nlmsg_type=%hu sclass=%s"
5482 " pig=%d comm=%s\n",
Marek Milkoviccded3ff2015-06-04 16:22:16 -04005483 sk->sk_protocol, nlh->nlmsg_type,
Vladis Dronov76319942015-12-24 11:09:41 -05005484 secclass_map[sksec->sclass - 1].name,
5485 task_pid_nr(current), current->comm);
Paul Mooree5a5ca92018-03-01 17:38:30 -05005486 if (!enforcing_enabled(&selinux_state) ||
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005487 security_get_allow_unknown(&selinux_state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488 err = 0;
5489 }
5490
5491 /* Ignore */
5492 if (err == -ENOENT)
5493 err = 0;
5494 goto out;
5495 }
5496
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005497 err = sock_has_perm(sk, perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498out:
5499 return err;
5500}
5501
5502#ifdef CONFIG_NETFILTER
5503
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005504static unsigned int selinux_ip_forward(struct sk_buff *skb,
5505 const struct net_device *indev,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005506 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507{
Paul Mooredfaebe92008-10-10 10:16:31 -04005508 int err;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005509 char *addrp;
5510 u32 peer_sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04005511 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005512 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05005513 u8 secmark_active;
Paul Moore948bf852008-10-10 10:16:32 -04005514 u8 netlbl_active;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005515 u8 peerlbl_active;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005516
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005517 if (!selinux_policycap_netpeer())
Paul Mooreeffad8d2008-01-29 08:49:27 -05005518 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005519
Paul Mooreeffad8d2008-01-29 08:49:27 -05005520 secmark_active = selinux_secmark_enabled();
Paul Moore948bf852008-10-10 10:16:32 -04005521 netlbl_active = netlbl_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04005522 peerlbl_active = selinux_peerlbl_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05005523 if (!secmark_active && !peerlbl_active)
5524 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005525
Paul Moored8395c82008-10-10 10:16:30 -04005526 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
5527 return NF_DROP;
5528
Eric Paris50c205f2012-04-04 15:01:43 -04005529 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005530 ad.u.net = &net;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005531 ad.u.net->netif = indev->ifindex;
Eric Paris48c62af2012-04-02 13:15:44 -04005532 ad.u.net->family = family;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005533 if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
5534 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535
Paul Mooredfaebe92008-10-10 10:16:31 -04005536 if (peerlbl_active) {
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005537 err = selinux_inet_sys_rcv_skb(dev_net(indev), indev->ifindex,
5538 addrp, family, peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04005539 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04005540 selinux_netlbl_err(skb, family, err, 1);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005541 return NF_DROP;
Paul Mooredfaebe92008-10-10 10:16:31 -04005542 }
5543 }
Paul Mooreeffad8d2008-01-29 08:49:27 -05005544
5545 if (secmark_active)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005546 if (avc_has_perm(&selinux_state,
5547 peer_sid, skb->secmark,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005548 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
5549 return NF_DROP;
5550
Paul Moore948bf852008-10-10 10:16:32 -04005551 if (netlbl_active)
5552 /* we do this in the FORWARD path and not the POST_ROUTING
5553 * path because we want to make sure we apply the necessary
5554 * labeling before IPsec is applied so we can leverage AH
5555 * protection */
5556 if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
5557 return NF_DROP;
5558
Paul Mooreeffad8d2008-01-29 08:49:27 -05005559 return NF_ACCEPT;
5560}
5561
Eric W. Biederman06198b32015-09-18 14:33:06 -05005562static unsigned int selinux_ipv4_forward(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005563 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005564 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005565{
David S. Miller238e54c2015-04-03 20:32:56 -04005566 return selinux_ip_forward(skb, state->in, PF_INET);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005567}
5568
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005569#if IS_ENABLED(CONFIG_IPV6)
Eric W. Biederman06198b32015-09-18 14:33:06 -05005570static unsigned int selinux_ipv6_forward(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005571 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005572 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005573{
David S. Miller238e54c2015-04-03 20:32:56 -04005574 return selinux_ip_forward(skb, state->in, PF_INET6);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005575}
5576#endif /* IPV6 */
5577
Paul Moore948bf852008-10-10 10:16:32 -04005578static unsigned int selinux_ip_output(struct sk_buff *skb,
5579 u16 family)
5580{
Paul Moore47180062013-12-04 16:10:45 -05005581 struct sock *sk;
Paul Moore948bf852008-10-10 10:16:32 -04005582 u32 sid;
5583
5584 if (!netlbl_enabled())
5585 return NF_ACCEPT;
5586
5587 /* we do this in the LOCAL_OUT path and not the POST_ROUTING path
5588 * because we want to make sure we apply the necessary labeling
5589 * before IPsec is applied so we can leverage AH protection */
Paul Moore47180062013-12-04 16:10:45 -05005590 sk = skb->sk;
5591 if (sk) {
5592 struct sk_security_struct *sksec;
5593
Eric Dumazete446f9d2015-10-08 05:01:55 -07005594 if (sk_listener(sk))
Paul Moore47180062013-12-04 16:10:45 -05005595 /* if the socket is the listening state then this
5596 * packet is a SYN-ACK packet which means it needs to
5597 * be labeled based on the connection/request_sock and
5598 * not the parent socket. unfortunately, we can't
5599 * lookup the request_sock yet as it isn't queued on
5600 * the parent socket until after the SYN-ACK is sent.
5601 * the "solution" is to simply pass the packet as-is
5602 * as any IP option based labeling should be copied
5603 * from the initial connection request (in the IP
5604 * layer). it is far from ideal, but until we get a
5605 * security label in the packet itself this is the
5606 * best we can do. */
5607 return NF_ACCEPT;
5608
5609 /* standard practice, label using the parent socket */
5610 sksec = sk->sk_security;
Paul Moore948bf852008-10-10 10:16:32 -04005611 sid = sksec->sid;
5612 } else
5613 sid = SECINITSID_KERNEL;
5614 if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
5615 return NF_DROP;
5616
5617 return NF_ACCEPT;
5618}
5619
Eric W. Biederman06198b32015-09-18 14:33:06 -05005620static unsigned int selinux_ipv4_output(void *priv,
Paul Moore948bf852008-10-10 10:16:32 -04005621 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005622 const struct nf_hook_state *state)
Paul Moore948bf852008-10-10 10:16:32 -04005623{
5624 return selinux_ip_output(skb, PF_INET);
5625}
5626
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005627#if IS_ENABLED(CONFIG_IPV6)
Huw Davies2917f572016-06-27 15:06:15 -04005628static unsigned int selinux_ipv6_output(void *priv,
5629 struct sk_buff *skb,
5630 const struct nf_hook_state *state)
5631{
5632 return selinux_ip_output(skb, PF_INET6);
5633}
5634#endif /* IPV6 */
5635
Paul Mooreeffad8d2008-01-29 08:49:27 -05005636static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
5637 int ifindex,
Paul Moored8395c82008-10-10 10:16:30 -04005638 u16 family)
James Morris4e5ab4c2006-06-09 00:33:33 -07005639{
Eric Dumazet54abc682015-11-08 10:54:07 -08005640 struct sock *sk = skb_to_full_sk(skb);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005641 struct sk_security_struct *sksec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005642 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005643 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04005644 char *addrp;
5645 u8 proto;
James Morris4e5ab4c2006-06-09 00:33:33 -07005646
Paul Mooreeffad8d2008-01-29 08:49:27 -05005647 if (sk == NULL)
5648 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005649 sksec = sk->sk_security;
James Morris4e5ab4c2006-06-09 00:33:33 -07005650
Eric Paris50c205f2012-04-04 15:01:43 -04005651 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005652 ad.u.net = &net;
5653 ad.u.net->netif = ifindex;
5654 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04005655 if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
5656 return NF_DROP;
5657
Paul Moore58bfbb52009-03-27 17:10:41 -04005658 if (selinux_secmark_enabled())
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005659 if (avc_has_perm(&selinux_state,
5660 sksec->sid, skb->secmark,
Paul Moored8395c82008-10-10 10:16:30 -04005661 SECCLASS_PACKET, PACKET__SEND, &ad))
Eric Paris2fe66ec2010-11-23 06:28:08 +00005662 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07005663
Steffen Klassertb9679a72011-02-23 12:55:21 +01005664 if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
5665 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07005666
Paul Mooreeffad8d2008-01-29 08:49:27 -05005667 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668}
5669
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005670static unsigned int selinux_ip_postroute(struct sk_buff *skb,
5671 const struct net_device *outdev,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005672 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673{
Paul Mooreeffad8d2008-01-29 08:49:27 -05005674 u32 secmark_perm;
5675 u32 peer_sid;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005676 int ifindex = outdev->ifindex;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005677 struct sock *sk;
Thomas Liu2bf49692009-07-14 12:14:09 -04005678 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005679 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05005680 char *addrp;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005681 u8 secmark_active;
5682 u8 peerlbl_active;
5683
Paul Mooreeffad8d2008-01-29 08:49:27 -05005684 /* If any sort of compatibility mode is enabled then handoff processing
5685 * to the selinux_ip_postroute_compat() function to deal with the
5686 * special handling. We do this in an attempt to keep this function
5687 * as fast and as clean as possible. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005688 if (!selinux_policycap_netpeer())
Paul Moored8395c82008-10-10 10:16:30 -04005689 return selinux_ip_postroute_compat(skb, ifindex, family);
Paul Moorec0828e52013-12-10 14:58:01 -05005690
Paul Mooreeffad8d2008-01-29 08:49:27 -05005691 secmark_active = selinux_secmark_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04005692 peerlbl_active = selinux_peerlbl_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05005693 if (!secmark_active && !peerlbl_active)
5694 return NF_ACCEPT;
5695
Eric Dumazet54abc682015-11-08 10:54:07 -08005696 sk = skb_to_full_sk(skb);
Paul Moorec0828e52013-12-10 14:58:01 -05005697
Paul Mooreeffad8d2008-01-29 08:49:27 -05005698#ifdef CONFIG_XFRM
5699 /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
5700 * packet transformation so allow the packet to pass without any checks
5701 * since we'll have another chance to perform access control checks
5702 * when the packet is on it's final way out.
5703 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
Paul Moorec0828e52013-12-10 14:58:01 -05005704 * is NULL, in this case go ahead and apply access control.
5705 * NOTE: if this is a local socket (skb->sk != NULL) that is in the
5706 * TCP listening state we cannot wait until the XFRM processing
5707 * is done as we will miss out on the SA label if we do;
5708 * unfortunately, this means more work, but it is only once per
5709 * connection. */
5710 if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL &&
Eric Dumazete446f9d2015-10-08 05:01:55 -07005711 !(sk && sk_listener(sk)))
Paul Mooreeffad8d2008-01-29 08:49:27 -05005712 return NF_ACCEPT;
5713#endif
Paul Mooreeffad8d2008-01-29 08:49:27 -05005714
Paul Moored8395c82008-10-10 10:16:30 -04005715 if (sk == NULL) {
Paul Moore446b8022013-12-04 16:10:51 -05005716 /* Without an associated socket the packet is either coming
5717 * from the kernel or it is being forwarded; check the packet
5718 * to determine which and if the packet is being forwarded
5719 * query the packet directly to determine the security label. */
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005720 if (skb->skb_iif) {
5721 secmark_perm = PACKET__FORWARD_OUT;
Paul Moored8395c82008-10-10 10:16:30 -04005722 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005723 return NF_DROP;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005724 } else {
5725 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04005726 peer_sid = SECINITSID_KERNEL;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005727 }
Eric Dumazete446f9d2015-10-08 05:01:55 -07005728 } else if (sk_listener(sk)) {
Paul Moore446b8022013-12-04 16:10:51 -05005729 /* Locally generated packet but the associated socket is in the
5730 * listening state which means this is a SYN-ACK packet. In
5731 * this particular case the correct security label is assigned
5732 * to the connection/request_sock but unfortunately we can't
5733 * query the request_sock as it isn't queued on the parent
5734 * socket until after the SYN-ACK packet is sent; the only
5735 * viable choice is to regenerate the label like we do in
5736 * selinux_inet_conn_request(). See also selinux_ip_output()
5737 * for similar problems. */
5738 u32 skb_sid;
Eric Dumazete446f9d2015-10-08 05:01:55 -07005739 struct sk_security_struct *sksec;
5740
Eric Dumazete446f9d2015-10-08 05:01:55 -07005741 sksec = sk->sk_security;
Paul Moore446b8022013-12-04 16:10:51 -05005742 if (selinux_skb_peerlbl_sid(skb, family, &skb_sid))
5743 return NF_DROP;
Paul Moorec0828e52013-12-10 14:58:01 -05005744 /* At this point, if the returned skb peerlbl is SECSID_NULL
5745 * and the packet has been through at least one XFRM
5746 * transformation then we must be dealing with the "final"
5747 * form of labeled IPsec packet; since we've already applied
5748 * all of our access controls on this packet we can safely
5749 * pass the packet. */
5750 if (skb_sid == SECSID_NULL) {
5751 switch (family) {
5752 case PF_INET:
5753 if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
5754 return NF_ACCEPT;
5755 break;
5756 case PF_INET6:
5757 if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED)
5758 return NF_ACCEPT;
Paul Moorea7a91a12014-09-03 10:51:59 -04005759 break;
Paul Moorec0828e52013-12-10 14:58:01 -05005760 default:
5761 return NF_DROP_ERR(-ECONNREFUSED);
5762 }
5763 }
Paul Moore446b8022013-12-04 16:10:51 -05005764 if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid))
5765 return NF_DROP;
5766 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04005767 } else {
Paul Moore446b8022013-12-04 16:10:51 -05005768 /* Locally generated packet, fetch the security label from the
5769 * associated socket. */
Paul Mooreeffad8d2008-01-29 08:49:27 -05005770 struct sk_security_struct *sksec = sk->sk_security;
5771 peer_sid = sksec->sid;
5772 secmark_perm = PACKET__SEND;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005773 }
5774
Eric Paris50c205f2012-04-04 15:01:43 -04005775 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005776 ad.u.net = &net;
5777 ad.u.net->netif = ifindex;
5778 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04005779 if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
Eric Paris04f6d702010-11-23 06:28:02 +00005780 return NF_DROP;
Paul Moored8395c82008-10-10 10:16:30 -04005781
Paul Mooreeffad8d2008-01-29 08:49:27 -05005782 if (secmark_active)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005783 if (avc_has_perm(&selinux_state,
5784 peer_sid, skb->secmark,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005785 SECCLASS_PACKET, secmark_perm, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005786 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005787
5788 if (peerlbl_active) {
5789 u32 if_sid;
5790 u32 node_sid;
5791
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005792 if (sel_netif_sid(dev_net(outdev), ifindex, &if_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005793 return NF_DROP;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005794 if (avc_has_perm(&selinux_state,
5795 peer_sid, if_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005796 SECCLASS_NETIF, NETIF__EGRESS, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005797 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005798
5799 if (sel_netnode_sid(addrp, family, &node_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005800 return NF_DROP;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005801 if (avc_has_perm(&selinux_state,
5802 peer_sid, node_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005803 SECCLASS_NODE, NODE__SENDTO, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005804 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005805 }
5806
5807 return NF_ACCEPT;
5808}
5809
Eric W. Biederman06198b32015-09-18 14:33:06 -05005810static unsigned int selinux_ipv4_postroute(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005811 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005812 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005813{
David S. Miller238e54c2015-04-03 20:32:56 -04005814 return selinux_ip_postroute(skb, state->out, PF_INET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005815}
5816
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005817#if IS_ENABLED(CONFIG_IPV6)
Eric W. Biederman06198b32015-09-18 14:33:06 -05005818static unsigned int selinux_ipv6_postroute(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005819 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005820 const struct nf_hook_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821{
David S. Miller238e54c2015-04-03 20:32:56 -04005822 return selinux_ip_postroute(skb, state->out, PF_INET6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005823}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005824#endif /* IPV6 */
5825
5826#endif /* CONFIG_NETFILTER */
5827
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
5829{
Stephen Smalley941fc5b2009-10-01 14:48:23 -04005830 return selinux_nlmsg_perm(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831}
5832
Casey Schauflerecd5f822018-11-20 11:55:02 -08005833static void ipc_init_security(struct ipc_security_struct *isec, u16 sclass)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005834{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835 isec->sclass = sclass;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005836 isec->sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837}
5838
5839static int msg_msg_alloc_security(struct msg_msg *msg)
5840{
5841 struct msg_security_struct *msec;
5842
Casey Schauflerecd5f822018-11-20 11:55:02 -08005843 msec = selinux_msg_msg(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005844 msec->sid = SECINITSID_UNLABELED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005845
5846 return 0;
5847}
5848
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
Stephen Smalley6af963f2005-05-01 08:58:39 -07005850 u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005852 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005853 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005854 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855
Casey Schaufler7c653822018-09-21 17:19:45 -07005856 isec = selinux_ipc(ipc_perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857
Eric Paris50c205f2012-04-04 15:01:43 -04005858 ad.type = LSM_AUDIT_DATA_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859 ad.u.ipc_id = ipc_perms->key;
5860
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005861 return avc_has_perm(&selinux_state,
5862 sid, isec->sid, isec->sclass, perms, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863}
5864
5865static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
5866{
5867 return msg_msg_alloc_security(msg);
5868}
5869
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870/* message queue security operations */
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005871static int selinux_msg_queue_alloc_security(struct kern_ipc_perm *msq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005872{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005874 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005875 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876 int rc;
5877
Casey Schauflerecd5f822018-11-20 11:55:02 -08005878 isec = selinux_ipc(msq);
5879 ipc_init_security(isec, SECCLASS_MSGQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880
Eric Paris50c205f2012-04-04 15:01:43 -04005881 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005882 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005883
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005884 rc = avc_has_perm(&selinux_state,
5885 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886 MSGQ__CREATE, &ad);
Casey Schauflerecd5f822018-11-20 11:55:02 -08005887 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888}
5889
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005890static int selinux_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005893 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005894 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895
Casey Schaufler7c653822018-09-21 17:19:45 -07005896 isec = selinux_ipc(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005897
Eric Paris50c205f2012-04-04 15:01:43 -04005898 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005899 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005901 return avc_has_perm(&selinux_state,
5902 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903 MSGQ__ASSOCIATE, &ad);
5904}
5905
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005906static int selinux_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005907{
5908 int err;
5909 int perms;
5910
Eric Paris828dfe12008-04-17 13:17:49 -04005911 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912 case IPC_INFO:
5913 case MSG_INFO:
5914 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005915 return avc_has_perm(&selinux_state,
5916 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005917 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918 case IPC_STAT:
5919 case MSG_STAT:
Davidlohr Bueso23c8cec2018-04-10 16:35:30 -07005920 case MSG_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921 perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
5922 break;
5923 case IPC_SET:
5924 perms = MSGQ__SETATTR;
5925 break;
5926 case IPC_RMID:
5927 perms = MSGQ__DESTROY;
5928 break;
5929 default:
5930 return 0;
5931 }
5932
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005933 err = ipc_has_perm(msq, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934 return err;
5935}
5936
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005937static int selinux_msg_queue_msgsnd(struct kern_ipc_perm *msq, struct msg_msg *msg, int msqflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939 struct ipc_security_struct *isec;
5940 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005941 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005942 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005943 int rc;
5944
Casey Schaufler7c653822018-09-21 17:19:45 -07005945 isec = selinux_ipc(msq);
5946 msec = selinux_msg_msg(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005947
5948 /*
5949 * First time through, need to assign label to the message
5950 */
5951 if (msec->sid == SECINITSID_UNLABELED) {
5952 /*
5953 * Compute new sid based on current process and
5954 * message queue this message will be stored in
5955 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005956 rc = security_transition_sid(&selinux_state, sid, isec->sid,
5957 SECCLASS_MSG, NULL, &msec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005958 if (rc)
5959 return rc;
5960 }
5961
Eric Paris50c205f2012-04-04 15:01:43 -04005962 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005963 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005964
5965 /* Can this process write to the queue? */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005966 rc = avc_has_perm(&selinux_state,
5967 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005968 MSGQ__WRITE, &ad);
5969 if (!rc)
5970 /* Can this process send the message */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005971 rc = avc_has_perm(&selinux_state,
5972 sid, msec->sid, SECCLASS_MSG,
David Howells275bb412008-11-14 10:39:19 +11005973 MSG__SEND, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005974 if (!rc)
5975 /* Can the message be put in the queue? */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005976 rc = avc_has_perm(&selinux_state,
5977 msec->sid, isec->sid, SECCLASS_MSGQ,
David Howells275bb412008-11-14 10:39:19 +11005978 MSGQ__ENQUEUE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979
5980 return rc;
5981}
5982
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005983static int selinux_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984 struct task_struct *target,
5985 long type, int mode)
5986{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987 struct ipc_security_struct *isec;
5988 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005989 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005990 u32 sid = task_sid(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991 int rc;
5992
Casey Schaufler7c653822018-09-21 17:19:45 -07005993 isec = selinux_ipc(msq);
5994 msec = selinux_msg_msg(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005995
Eric Paris50c205f2012-04-04 15:01:43 -04005996 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005997 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005999 rc = avc_has_perm(&selinux_state,
6000 sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006001 SECCLASS_MSGQ, MSGQ__READ, &ad);
6002 if (!rc)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006003 rc = avc_has_perm(&selinux_state,
6004 sid, msec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005 SECCLASS_MSG, MSG__RECEIVE, &ad);
6006 return rc;
6007}
6008
6009/* Shared Memory security operations */
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006010static int selinux_shm_alloc_security(struct kern_ipc_perm *shp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006013 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006014 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015 int rc;
6016
Casey Schauflerecd5f822018-11-20 11:55:02 -08006017 isec = selinux_ipc(shp);
6018 ipc_init_security(isec, SECCLASS_SHM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019
Eric Paris50c205f2012-04-04 15:01:43 -04006020 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006021 ad.u.ipc_id = shp->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006023 rc = avc_has_perm(&selinux_state,
6024 sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006025 SHM__CREATE, &ad);
Casey Schauflerecd5f822018-11-20 11:55:02 -08006026 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027}
6028
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006029static int selinux_shm_associate(struct kern_ipc_perm *shp, int shmflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006031 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006032 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006033 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034
Casey Schaufler7c653822018-09-21 17:19:45 -07006035 isec = selinux_ipc(shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036
Eric Paris50c205f2012-04-04 15:01:43 -04006037 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006038 ad.u.ipc_id = shp->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006040 return avc_has_perm(&selinux_state,
6041 sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006042 SHM__ASSOCIATE, &ad);
6043}
6044
6045/* Note, at this point, shp is locked down */
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006046static int selinux_shm_shmctl(struct kern_ipc_perm *shp, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047{
6048 int perms;
6049 int err;
6050
Eric Paris828dfe12008-04-17 13:17:49 -04006051 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052 case IPC_INFO:
6053 case SHM_INFO:
6054 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006055 return avc_has_perm(&selinux_state,
6056 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006057 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058 case IPC_STAT:
6059 case SHM_STAT:
Davidlohr Buesoc21a6972018-04-10 16:35:23 -07006060 case SHM_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006061 perms = SHM__GETATTR | SHM__ASSOCIATE;
6062 break;
6063 case IPC_SET:
6064 perms = SHM__SETATTR;
6065 break;
6066 case SHM_LOCK:
6067 case SHM_UNLOCK:
6068 perms = SHM__LOCK;
6069 break;
6070 case IPC_RMID:
6071 perms = SHM__DESTROY;
6072 break;
6073 default:
6074 return 0;
6075 }
6076
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006077 err = ipc_has_perm(shp, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006078 return err;
6079}
6080
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006081static int selinux_shm_shmat(struct kern_ipc_perm *shp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082 char __user *shmaddr, int shmflg)
6083{
6084 u32 perms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085
6086 if (shmflg & SHM_RDONLY)
6087 perms = SHM__READ;
6088 else
6089 perms = SHM__READ | SHM__WRITE;
6090
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006091 return ipc_has_perm(shp, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006092}
6093
6094/* Semaphore security operations */
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006095static int selinux_sem_alloc_security(struct kern_ipc_perm *sma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006096{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006098 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006099 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006100 int rc;
6101
Casey Schauflerecd5f822018-11-20 11:55:02 -08006102 isec = selinux_ipc(sma);
6103 ipc_init_security(isec, SECCLASS_SEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104
Eric Paris50c205f2012-04-04 15:01:43 -04006105 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006106 ad.u.ipc_id = sma->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006108 rc = avc_has_perm(&selinux_state,
6109 sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006110 SEM__CREATE, &ad);
Casey Schauflerecd5f822018-11-20 11:55:02 -08006111 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006112}
6113
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006114static int selinux_sem_associate(struct kern_ipc_perm *sma, int semflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006115{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006116 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006117 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006118 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006119
Casey Schaufler7c653822018-09-21 17:19:45 -07006120 isec = selinux_ipc(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006121
Eric Paris50c205f2012-04-04 15:01:43 -04006122 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006123 ad.u.ipc_id = sma->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006124
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006125 return avc_has_perm(&selinux_state,
6126 sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127 SEM__ASSOCIATE, &ad);
6128}
6129
6130/* Note, at this point, sma is locked down */
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006131static int selinux_sem_semctl(struct kern_ipc_perm *sma, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006132{
6133 int err;
6134 u32 perms;
6135
Eric Paris828dfe12008-04-17 13:17:49 -04006136 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006137 case IPC_INFO:
6138 case SEM_INFO:
6139 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006140 return avc_has_perm(&selinux_state,
6141 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006142 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006143 case GETPID:
6144 case GETNCNT:
6145 case GETZCNT:
6146 perms = SEM__GETATTR;
6147 break;
6148 case GETVAL:
6149 case GETALL:
6150 perms = SEM__READ;
6151 break;
6152 case SETVAL:
6153 case SETALL:
6154 perms = SEM__WRITE;
6155 break;
6156 case IPC_RMID:
6157 perms = SEM__DESTROY;
6158 break;
6159 case IPC_SET:
6160 perms = SEM__SETATTR;
6161 break;
6162 case IPC_STAT:
6163 case SEM_STAT:
Davidlohr Buesoa280d6d2018-04-10 16:35:26 -07006164 case SEM_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006165 perms = SEM__GETATTR | SEM__ASSOCIATE;
6166 break;
6167 default:
6168 return 0;
6169 }
6170
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006171 err = ipc_has_perm(sma, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006172 return err;
6173}
6174
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006175static int selinux_sem_semop(struct kern_ipc_perm *sma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006176 struct sembuf *sops, unsigned nsops, int alter)
6177{
6178 u32 perms;
6179
6180 if (alter)
6181 perms = SEM__READ | SEM__WRITE;
6182 else
6183 perms = SEM__READ;
6184
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006185 return ipc_has_perm(sma, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006186}
6187
6188static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
6189{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006190 u32 av = 0;
6191
Linus Torvalds1da177e2005-04-16 15:20:36 -07006192 av = 0;
6193 if (flag & S_IRUGO)
6194 av |= IPC__UNIX_READ;
6195 if (flag & S_IWUGO)
6196 av |= IPC__UNIX_WRITE;
6197
6198 if (av == 0)
6199 return 0;
6200
Stephen Smalley6af963f2005-05-01 08:58:39 -07006201 return ipc_has_perm(ipcp, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006202}
6203
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02006204static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
6205{
Casey Schaufler7c653822018-09-21 17:19:45 -07006206 struct ipc_security_struct *isec = selinux_ipc(ipcp);
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02006207 *secid = isec->sid;
6208}
6209
Eric Paris828dfe12008-04-17 13:17:49 -04006210static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211{
6212 if (inode)
6213 inode_doinit_with_dentry(inode, dentry);
6214}
6215
6216static int selinux_getprocattr(struct task_struct *p,
Al Viro04ff9702007-03-12 16:17:58 +00006217 char *name, char **value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218{
David Howells275bb412008-11-14 10:39:19 +11006219 const struct task_security_struct *__tsec;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00006220 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006221 int error;
Al Viro04ff9702007-03-12 16:17:58 +00006222 unsigned len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006223
David Howells275bb412008-11-14 10:39:19 +11006224 rcu_read_lock();
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07006225 __tsec = selinux_cred(__task_cred(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006226
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006227 if (current != p) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006228 error = avc_has_perm(&selinux_state,
6229 current_sid(), __tsec->sid,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006230 SECCLASS_PROCESS, PROCESS__GETATTR, NULL);
6231 if (error)
6232 goto bad;
6233 }
6234
Linus Torvalds1da177e2005-04-16 15:20:36 -07006235 if (!strcmp(name, "current"))
David Howells275bb412008-11-14 10:39:19 +11006236 sid = __tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006237 else if (!strcmp(name, "prev"))
David Howells275bb412008-11-14 10:39:19 +11006238 sid = __tsec->osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006239 else if (!strcmp(name, "exec"))
David Howells275bb412008-11-14 10:39:19 +11006240 sid = __tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006241 else if (!strcmp(name, "fscreate"))
David Howells275bb412008-11-14 10:39:19 +11006242 sid = __tsec->create_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006243 else if (!strcmp(name, "keycreate"))
David Howells275bb412008-11-14 10:39:19 +11006244 sid = __tsec->keycreate_sid;
Eric Paris42c3e032006-06-26 00:26:03 -07006245 else if (!strcmp(name, "sockcreate"))
David Howells275bb412008-11-14 10:39:19 +11006246 sid = __tsec->sockcreate_sid;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006247 else {
6248 error = -EINVAL;
6249 goto bad;
6250 }
David Howells275bb412008-11-14 10:39:19 +11006251 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006252
6253 if (!sid)
6254 return 0;
6255
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006256 error = security_sid_to_context(&selinux_state, sid, value, &len);
Al Viro04ff9702007-03-12 16:17:58 +00006257 if (error)
6258 return error;
6259 return len;
David Howells275bb412008-11-14 10:39:19 +11006260
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006261bad:
David Howells275bb412008-11-14 10:39:19 +11006262 rcu_read_unlock();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006263 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006264}
6265
Stephen Smalleyb21507e2017-01-09 10:07:31 -05006266static int selinux_setprocattr(const char *name, void *value, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006267{
6268 struct task_security_struct *tsec;
David Howellsd84f4f92008-11-14 10:39:23 +11006269 struct cred *new;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006270 u32 mysid = current_sid(), sid = 0, ptsid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271 int error;
6272 char *str = value;
6273
Linus Torvalds1da177e2005-04-16 15:20:36 -07006274 /*
6275 * Basic control over ability to set these attributes at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006276 */
6277 if (!strcmp(name, "exec"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006278 error = avc_has_perm(&selinux_state,
6279 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006280 PROCESS__SETEXEC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006281 else if (!strcmp(name, "fscreate"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006282 error = avc_has_perm(&selinux_state,
6283 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006284 PROCESS__SETFSCREATE, NULL);
Michael LeMay4eb582c2006-06-26 00:24:57 -07006285 else if (!strcmp(name, "keycreate"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006286 error = avc_has_perm(&selinux_state,
6287 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006288 PROCESS__SETKEYCREATE, NULL);
Eric Paris42c3e032006-06-26 00:26:03 -07006289 else if (!strcmp(name, "sockcreate"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006290 error = avc_has_perm(&selinux_state,
6291 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006292 PROCESS__SETSOCKCREATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006293 else if (!strcmp(name, "current"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006294 error = avc_has_perm(&selinux_state,
6295 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006296 PROCESS__SETCURRENT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006297 else
6298 error = -EINVAL;
6299 if (error)
6300 return error;
6301
6302 /* Obtain a SID for the context, if one was specified. */
Stephen Smalleya050a572017-01-31 11:54:04 -05006303 if (size && str[0] && str[0] != '\n') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006304 if (str[size-1] == '\n') {
6305 str[size-1] = 0;
6306 size--;
6307 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006308 error = security_context_to_sid(&selinux_state, value, size,
6309 &sid, GFP_KERNEL);
Stephen Smalley12b29f32008-05-07 13:03:20 -04006310 if (error == -EINVAL && !strcmp(name, "fscreate")) {
Stephen Smalleydb590002017-04-20 11:31:30 -04006311 if (!has_cap_mac_admin(true)) {
Eric Parisd6ea83e2012-04-04 13:45:49 -04006312 struct audit_buffer *ab;
6313 size_t audit_size;
6314
6315 /* We strip a nul only if it is at the end, otherwise the
6316 * context contains a nul and we should audit that */
6317 if (str[size - 1] == '\0')
6318 audit_size = size - 1;
6319 else
6320 audit_size = size;
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04006321 ab = audit_log_start(audit_context(),
6322 GFP_ATOMIC,
6323 AUDIT_SELINUX_ERR);
Eric Parisd6ea83e2012-04-04 13:45:49 -04006324 audit_log_format(ab, "op=fscreate invalid_context=");
6325 audit_log_n_untrustedstring(ab, value, audit_size);
6326 audit_log_end(ab);
6327
Stephen Smalley12b29f32008-05-07 13:03:20 -04006328 return error;
Eric Parisd6ea83e2012-04-04 13:45:49 -04006329 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006330 error = security_context_to_sid_force(
6331 &selinux_state,
6332 value, size, &sid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04006333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006334 if (error)
6335 return error;
6336 }
6337
David Howellsd84f4f92008-11-14 10:39:23 +11006338 new = prepare_creds();
6339 if (!new)
6340 return -ENOMEM;
6341
Linus Torvalds1da177e2005-04-16 15:20:36 -07006342 /* Permission checking based on the specified context is
6343 performed during the actual operation (execve,
6344 open/mkdir/...), when we know the full context of the
David Howellsd84f4f92008-11-14 10:39:23 +11006345 operation. See selinux_bprm_set_creds for the execve
Linus Torvalds1da177e2005-04-16 15:20:36 -07006346 checks and may_create for the file creation checks. The
6347 operation will then fail if the context is not permitted. */
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07006348 tsec = selinux_cred(new);
David Howellsd84f4f92008-11-14 10:39:23 +11006349 if (!strcmp(name, "exec")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006350 tsec->exec_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006351 } else if (!strcmp(name, "fscreate")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006352 tsec->create_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006353 } else if (!strcmp(name, "keycreate")) {
Ondrej Mosnacek464c2582019-06-12 10:12:26 +02006354 if (sid) {
6355 error = avc_has_perm(&selinux_state, mysid, sid,
6356 SECCLASS_KEY, KEY__CREATE, NULL);
6357 if (error)
6358 goto abort_change;
6359 }
Michael LeMay4eb582c2006-06-26 00:24:57 -07006360 tsec->keycreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006361 } else if (!strcmp(name, "sockcreate")) {
Eric Paris42c3e032006-06-26 00:26:03 -07006362 tsec->sockcreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006363 } else if (!strcmp(name, "current")) {
6364 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006365 if (sid == 0)
David Howellsd84f4f92008-11-14 10:39:23 +11006366 goto abort_change;
KaiGai Koheid9250de2008-08-28 16:35:57 +09006367
David Howellsd84f4f92008-11-14 10:39:23 +11006368 /* Only allow single threaded processes to change context */
6369 error = -EPERM;
Oleg Nesterov5bb459b2009-07-10 03:48:23 +02006370 if (!current_is_single_threaded()) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006371 error = security_bounded_transition(&selinux_state,
6372 tsec->sid, sid);
David Howellsd84f4f92008-11-14 10:39:23 +11006373 if (error)
6374 goto abort_change;
Eric Paris828dfe12008-04-17 13:17:49 -04006375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006376
6377 /* Check permissions for the transition. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006378 error = avc_has_perm(&selinux_state,
6379 tsec->sid, sid, SECCLASS_PROCESS,
Eric Paris828dfe12008-04-17 13:17:49 -04006380 PROCESS__DYNTRANSITION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006381 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11006382 goto abort_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006383
6384 /* Check for ptracing, and update the task SID if ok.
6385 Otherwise, leave SID unchanged and fail. */
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006386 ptsid = ptrace_parent_sid();
Paul Moore0c6181c2016-03-30 21:41:21 -04006387 if (ptsid != 0) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006388 error = avc_has_perm(&selinux_state,
6389 ptsid, sid, SECCLASS_PROCESS,
David Howellsd84f4f92008-11-14 10:39:23 +11006390 PROCESS__PTRACE, NULL);
6391 if (error)
6392 goto abort_change;
6393 }
6394
6395 tsec->sid = sid;
6396 } else {
6397 error = -EINVAL;
6398 goto abort_change;
6399 }
6400
6401 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006402 return size;
David Howellsd84f4f92008-11-14 10:39:23 +11006403
6404abort_change:
6405 abort_creds(new);
6406 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006407}
6408
David Quigley746df9b2013-05-22 12:50:35 -04006409static int selinux_ismaclabel(const char *name)
6410{
6411 return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0);
6412}
6413
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006414static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
6415{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006416 return security_sid_to_context(&selinux_state, secid,
6417 secdata, seclen);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006418}
6419
David Howells7bf570d2008-04-29 20:52:51 +01006420static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
David Howells63cb3442008-01-15 23:47:35 +00006421{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006422 return security_context_to_sid(&selinux_state, secdata, seclen,
6423 secid, GFP_KERNEL);
David Howells63cb3442008-01-15 23:47:35 +00006424}
6425
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006426static void selinux_release_secctx(char *secdata, u32 seclen)
6427{
Paul Moore088999e2007-08-01 11:12:58 -04006428 kfree(secdata);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006429}
6430
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006431static void selinux_inode_invalidate_secctx(struct inode *inode)
6432{
Casey Schaufler80788c22018-09-21 17:19:11 -07006433 struct inode_security_struct *isec = selinux_inode(inode);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006434
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01006435 spin_lock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006436 isec->initialized = LABEL_INVALID;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01006437 spin_unlock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006438}
6439
David P. Quigley1ee65e32009-09-03 14:25:57 -04006440/*
6441 * called with inode->i_mutex locked
6442 */
6443static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
6444{
Ondrej Mosnacek53e0c2a2018-12-21 21:18:53 +01006445 int rc = selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX,
6446 ctx, ctxlen, 0);
6447 /* Do not return error when suppressing label (SBLABEL_MNT not set). */
6448 return rc == -EOPNOTSUPP ? 0 : rc;
David P. Quigley1ee65e32009-09-03 14:25:57 -04006449}
6450
6451/*
6452 * called with inode->i_mutex locked
6453 */
6454static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
6455{
6456 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0);
6457}
6458
6459static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
6460{
6461 int len = 0;
6462 len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX,
6463 ctx, true);
6464 if (len < 0)
6465 return len;
6466 *ctxlen = len;
6467 return 0;
6468}
Michael LeMayd7200242006-06-22 14:47:17 -07006469#ifdef CONFIG_KEYS
6470
David Howellsd84f4f92008-11-14 10:39:23 +11006471static int selinux_key_alloc(struct key *k, const struct cred *cred,
David Howells7e047ef2006-06-26 00:24:50 -07006472 unsigned long flags)
Michael LeMayd7200242006-06-22 14:47:17 -07006473{
David Howellsd84f4f92008-11-14 10:39:23 +11006474 const struct task_security_struct *tsec;
Michael LeMayd7200242006-06-22 14:47:17 -07006475 struct key_security_struct *ksec;
6476
6477 ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
6478 if (!ksec)
6479 return -ENOMEM;
6480
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07006481 tsec = selinux_cred(cred);
David Howellsd84f4f92008-11-14 10:39:23 +11006482 if (tsec->keycreate_sid)
6483 ksec->sid = tsec->keycreate_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006484 else
David Howellsd84f4f92008-11-14 10:39:23 +11006485 ksec->sid = tsec->sid;
Michael LeMayd7200242006-06-22 14:47:17 -07006486
David Howells275bb412008-11-14 10:39:19 +11006487 k->security = ksec;
Michael LeMayd7200242006-06-22 14:47:17 -07006488 return 0;
6489}
6490
6491static void selinux_key_free(struct key *k)
6492{
6493 struct key_security_struct *ksec = k->security;
6494
6495 k->security = NULL;
6496 kfree(ksec);
6497}
6498
6499static int selinux_key_permission(key_ref_t key_ref,
David Howellsd84f4f92008-11-14 10:39:23 +11006500 const struct cred *cred,
David Howellsf5895942014-03-14 17:44:49 +00006501 unsigned perm)
Michael LeMayd7200242006-06-22 14:47:17 -07006502{
6503 struct key *key;
Michael LeMayd7200242006-06-22 14:47:17 -07006504 struct key_security_struct *ksec;
David Howells275bb412008-11-14 10:39:19 +11006505 u32 sid;
Michael LeMayd7200242006-06-22 14:47:17 -07006506
6507 /* if no specific permissions are requested, we skip the
6508 permission check. No serious, additional covert channels
6509 appear to be created. */
6510 if (perm == 0)
6511 return 0;
6512
David Howellsd84f4f92008-11-14 10:39:23 +11006513 sid = cred_sid(cred);
David Howells275bb412008-11-14 10:39:19 +11006514
6515 key = key_ref_to_ptr(key_ref);
6516 ksec = key->security;
6517
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006518 return avc_has_perm(&selinux_state,
Linus Torvalds028db3e2019-07-10 18:43:43 -07006519 sid, ksec->sid, SECCLASS_KEY, perm, NULL);
Michael LeMayd7200242006-06-22 14:47:17 -07006520}
6521
David Howells70a5bb72008-04-29 01:01:26 -07006522static int selinux_key_getsecurity(struct key *key, char **_buffer)
6523{
6524 struct key_security_struct *ksec = key->security;
6525 char *context = NULL;
6526 unsigned len;
6527 int rc;
6528
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006529 rc = security_sid_to_context(&selinux_state, ksec->sid,
6530 &context, &len);
David Howells70a5bb72008-04-29 01:01:26 -07006531 if (!rc)
6532 rc = len;
6533 *_buffer = context;
6534 return rc;
6535}
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006536#endif
David Howells70a5bb72008-04-29 01:01:26 -07006537
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006538#ifdef CONFIG_SECURITY_INFINIBAND
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006539static int selinux_ib_pkey_access(void *ib_sec, u64 subnet_prefix, u16 pkey_val)
6540{
6541 struct common_audit_data ad;
6542 int err;
6543 u32 sid = 0;
6544 struct ib_security_struct *sec = ib_sec;
6545 struct lsm_ibpkey_audit ibpkey;
6546
Daniel Jurgens409dcf32017-05-19 15:48:59 +03006547 err = sel_ib_pkey_sid(subnet_prefix, pkey_val, &sid);
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006548 if (err)
6549 return err;
6550
6551 ad.type = LSM_AUDIT_DATA_IBPKEY;
6552 ibpkey.subnet_prefix = subnet_prefix;
6553 ibpkey.pkey = pkey_val;
6554 ad.u.ibpkey = &ibpkey;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006555 return avc_has_perm(&selinux_state,
6556 sec->sid, sid,
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006557 SECCLASS_INFINIBAND_PKEY,
6558 INFINIBAND_PKEY__ACCESS, &ad);
6559}
6560
Daniel Jurgensab861df2017-05-19 15:48:58 +03006561static int selinux_ib_endport_manage_subnet(void *ib_sec, const char *dev_name,
6562 u8 port_num)
6563{
6564 struct common_audit_data ad;
6565 int err;
6566 u32 sid = 0;
6567 struct ib_security_struct *sec = ib_sec;
6568 struct lsm_ibendport_audit ibendport;
6569
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006570 err = security_ib_endport_sid(&selinux_state, dev_name, port_num,
6571 &sid);
Daniel Jurgensab861df2017-05-19 15:48:58 +03006572
6573 if (err)
6574 return err;
6575
6576 ad.type = LSM_AUDIT_DATA_IBENDPORT;
6577 strncpy(ibendport.dev_name, dev_name, sizeof(ibendport.dev_name));
6578 ibendport.port = port_num;
6579 ad.u.ibendport = &ibendport;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006580 return avc_has_perm(&selinux_state,
6581 sec->sid, sid,
Daniel Jurgensab861df2017-05-19 15:48:58 +03006582 SECCLASS_INFINIBAND_ENDPORT,
6583 INFINIBAND_ENDPORT__MANAGE_SUBNET, &ad);
6584}
6585
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006586static int selinux_ib_alloc_security(void **ib_sec)
6587{
6588 struct ib_security_struct *sec;
6589
6590 sec = kzalloc(sizeof(*sec), GFP_KERNEL);
6591 if (!sec)
6592 return -ENOMEM;
6593 sec->sid = current_sid();
6594
6595 *ib_sec = sec;
6596 return 0;
6597}
6598
6599static void selinux_ib_free_security(void *ib_sec)
6600{
6601 kfree(ib_sec);
6602}
Michael LeMayd7200242006-06-22 14:47:17 -07006603#endif
6604
Chenbo Fengec27c352017-10-18 13:00:25 -07006605#ifdef CONFIG_BPF_SYSCALL
6606static int selinux_bpf(int cmd, union bpf_attr *attr,
6607 unsigned int size)
6608{
6609 u32 sid = current_sid();
6610 int ret;
6611
6612 switch (cmd) {
6613 case BPF_MAP_CREATE:
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006614 ret = avc_has_perm(&selinux_state,
6615 sid, sid, SECCLASS_BPF, BPF__MAP_CREATE,
Chenbo Fengec27c352017-10-18 13:00:25 -07006616 NULL);
6617 break;
6618 case BPF_PROG_LOAD:
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006619 ret = avc_has_perm(&selinux_state,
6620 sid, sid, SECCLASS_BPF, BPF__PROG_LOAD,
Chenbo Fengec27c352017-10-18 13:00:25 -07006621 NULL);
6622 break;
6623 default:
6624 ret = 0;
6625 break;
6626 }
6627
6628 return ret;
6629}
6630
6631static u32 bpf_map_fmode_to_av(fmode_t fmode)
6632{
6633 u32 av = 0;
6634
6635 if (fmode & FMODE_READ)
6636 av |= BPF__MAP_READ;
6637 if (fmode & FMODE_WRITE)
6638 av |= BPF__MAP_WRITE;
6639 return av;
6640}
6641
Chenbo Fengf66e4482017-10-18 13:00:26 -07006642/* This function will check the file pass through unix socket or binder to see
6643 * if it is a bpf related object. And apply correspinding checks on the bpf
6644 * object based on the type. The bpf maps and programs, not like other files and
6645 * socket, are using a shared anonymous inode inside the kernel as their inode.
6646 * So checking that inode cannot identify if the process have privilege to
6647 * access the bpf object and that's why we have to add this additional check in
6648 * selinux_file_receive and selinux_binder_transfer_files.
6649 */
6650static int bpf_fd_pass(struct file *file, u32 sid)
6651{
6652 struct bpf_security_struct *bpfsec;
6653 struct bpf_prog *prog;
6654 struct bpf_map *map;
6655 int ret;
6656
6657 if (file->f_op == &bpf_map_fops) {
6658 map = file->private_data;
6659 bpfsec = map->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006660 ret = avc_has_perm(&selinux_state,
6661 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengf66e4482017-10-18 13:00:26 -07006662 bpf_map_fmode_to_av(file->f_mode), NULL);
6663 if (ret)
6664 return ret;
6665 } else if (file->f_op == &bpf_prog_fops) {
6666 prog = file->private_data;
6667 bpfsec = prog->aux->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006668 ret = avc_has_perm(&selinux_state,
6669 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengf66e4482017-10-18 13:00:26 -07006670 BPF__PROG_RUN, NULL);
6671 if (ret)
6672 return ret;
6673 }
6674 return 0;
6675}
6676
Chenbo Fengec27c352017-10-18 13:00:25 -07006677static int selinux_bpf_map(struct bpf_map *map, fmode_t fmode)
6678{
6679 u32 sid = current_sid();
6680 struct bpf_security_struct *bpfsec;
6681
6682 bpfsec = map->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006683 return avc_has_perm(&selinux_state,
6684 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengec27c352017-10-18 13:00:25 -07006685 bpf_map_fmode_to_av(fmode), NULL);
6686}
6687
6688static int selinux_bpf_prog(struct bpf_prog *prog)
6689{
6690 u32 sid = current_sid();
6691 struct bpf_security_struct *bpfsec;
6692
6693 bpfsec = prog->aux->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006694 return avc_has_perm(&selinux_state,
6695 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengec27c352017-10-18 13:00:25 -07006696 BPF__PROG_RUN, NULL);
6697}
6698
6699static int selinux_bpf_map_alloc(struct bpf_map *map)
6700{
6701 struct bpf_security_struct *bpfsec;
6702
6703 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6704 if (!bpfsec)
6705 return -ENOMEM;
6706
6707 bpfsec->sid = current_sid();
6708 map->security = bpfsec;
6709
6710 return 0;
6711}
6712
6713static void selinux_bpf_map_free(struct bpf_map *map)
6714{
6715 struct bpf_security_struct *bpfsec = map->security;
6716
6717 map->security = NULL;
6718 kfree(bpfsec);
6719}
6720
6721static int selinux_bpf_prog_alloc(struct bpf_prog_aux *aux)
6722{
6723 struct bpf_security_struct *bpfsec;
6724
6725 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6726 if (!bpfsec)
6727 return -ENOMEM;
6728
6729 bpfsec->sid = current_sid();
6730 aux->security = bpfsec;
6731
6732 return 0;
6733}
6734
6735static void selinux_bpf_prog_free(struct bpf_prog_aux *aux)
6736{
6737 struct bpf_security_struct *bpfsec = aux->security;
6738
6739 aux->security = NULL;
6740 kfree(bpfsec);
6741}
6742#endif
6743
Casey Schauflerbbd36622018-11-12 09:30:56 -08006744struct lsm_blob_sizes selinux_blob_sizes __lsm_ro_after_init = {
6745 .lbs_cred = sizeof(struct task_security_struct),
Casey Schaufler33bf60c2018-11-12 12:02:49 -08006746 .lbs_file = sizeof(struct file_security_struct),
Casey Schauflerafb1cbe32018-09-21 17:19:29 -07006747 .lbs_inode = sizeof(struct inode_security_struct),
Casey Schauflerecd5f822018-11-20 11:55:02 -08006748 .lbs_ipc = sizeof(struct ipc_security_struct),
6749 .lbs_msg_msg = sizeof(struct msg_security_struct),
Casey Schauflerbbd36622018-11-12 09:30:56 -08006750};
6751
James Morrisca97d932017-02-15 00:18:51 +11006752static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {
Casey Schauflere20b0432015-05-02 15:11:36 -07006753 LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr),
6754 LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction),
6755 LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder),
6756 LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file),
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02006757
Casey Schauflere20b0432015-05-02 15:11:36 -07006758 LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check),
6759 LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme),
6760 LSM_HOOK_INIT(capget, selinux_capget),
6761 LSM_HOOK_INIT(capset, selinux_capset),
6762 LSM_HOOK_INIT(capable, selinux_capable),
6763 LSM_HOOK_INIT(quotactl, selinux_quotactl),
6764 LSM_HOOK_INIT(quota_on, selinux_quota_on),
6765 LSM_HOOK_INIT(syslog, selinux_syslog),
6766 LSM_HOOK_INIT(vm_enough_memory, selinux_vm_enough_memory),
Stephen Smalley79af7302015-01-21 10:54:10 -05006767
Casey Schauflere20b0432015-05-02 15:11:36 -07006768 LSM_HOOK_INIT(netlink_send, selinux_netlink_send),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006769
Casey Schauflere20b0432015-05-02 15:11:36 -07006770 LSM_HOOK_INIT(bprm_set_creds, selinux_bprm_set_creds),
6771 LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds),
6772 LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006773
Al Viro0b520752018-12-23 16:02:47 -05006774 LSM_HOOK_INIT(fs_context_dup, selinux_fs_context_dup),
David Howells442155c2018-11-01 23:07:24 +00006775 LSM_HOOK_INIT(fs_context_parse_param, selinux_fs_context_parse_param),
6776
Casey Schauflere20b0432015-05-02 15:11:36 -07006777 LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security),
6778 LSM_HOOK_INIT(sb_free_security, selinux_sb_free_security),
Al Viro5b400232018-12-12 20:13:29 -05006779 LSM_HOOK_INIT(sb_eat_lsm_opts, selinux_sb_eat_lsm_opts),
Al Viro204cc0c2018-12-13 13:41:47 -05006780 LSM_HOOK_INIT(sb_free_mnt_opts, selinux_free_mnt_opts),
Casey Schauflere20b0432015-05-02 15:11:36 -07006781 LSM_HOOK_INIT(sb_remount, selinux_sb_remount),
6782 LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount),
6783 LSM_HOOK_INIT(sb_show_options, selinux_sb_show_options),
6784 LSM_HOOK_INIT(sb_statfs, selinux_sb_statfs),
6785 LSM_HOOK_INIT(sb_mount, selinux_mount),
6786 LSM_HOOK_INIT(sb_umount, selinux_umount),
6787 LSM_HOOK_INIT(sb_set_mnt_opts, selinux_set_mnt_opts),
6788 LSM_HOOK_INIT(sb_clone_mnt_opts, selinux_sb_clone_mnt_opts),
Al Viro757cbe52018-12-14 23:42:21 -05006789 LSM_HOOK_INIT(sb_add_mnt_opt, selinux_add_mnt_opt),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006790
Casey Schauflere20b0432015-05-02 15:11:36 -07006791 LSM_HOOK_INIT(dentry_init_security, selinux_dentry_init_security),
Vivek Goyala518b0a2016-07-13 10:44:53 -04006792 LSM_HOOK_INIT(dentry_create_files_as, selinux_dentry_create_files_as),
Eric Parise0007522008-03-05 10:31:54 -05006793
Casey Schauflere20b0432015-05-02 15:11:36 -07006794 LSM_HOOK_INIT(inode_alloc_security, selinux_inode_alloc_security),
6795 LSM_HOOK_INIT(inode_free_security, selinux_inode_free_security),
6796 LSM_HOOK_INIT(inode_init_security, selinux_inode_init_security),
6797 LSM_HOOK_INIT(inode_create, selinux_inode_create),
6798 LSM_HOOK_INIT(inode_link, selinux_inode_link),
6799 LSM_HOOK_INIT(inode_unlink, selinux_inode_unlink),
6800 LSM_HOOK_INIT(inode_symlink, selinux_inode_symlink),
6801 LSM_HOOK_INIT(inode_mkdir, selinux_inode_mkdir),
6802 LSM_HOOK_INIT(inode_rmdir, selinux_inode_rmdir),
6803 LSM_HOOK_INIT(inode_mknod, selinux_inode_mknod),
6804 LSM_HOOK_INIT(inode_rename, selinux_inode_rename),
6805 LSM_HOOK_INIT(inode_readlink, selinux_inode_readlink),
6806 LSM_HOOK_INIT(inode_follow_link, selinux_inode_follow_link),
6807 LSM_HOOK_INIT(inode_permission, selinux_inode_permission),
6808 LSM_HOOK_INIT(inode_setattr, selinux_inode_setattr),
6809 LSM_HOOK_INIT(inode_getattr, selinux_inode_getattr),
6810 LSM_HOOK_INIT(inode_setxattr, selinux_inode_setxattr),
6811 LSM_HOOK_INIT(inode_post_setxattr, selinux_inode_post_setxattr),
6812 LSM_HOOK_INIT(inode_getxattr, selinux_inode_getxattr),
6813 LSM_HOOK_INIT(inode_listxattr, selinux_inode_listxattr),
6814 LSM_HOOK_INIT(inode_removexattr, selinux_inode_removexattr),
6815 LSM_HOOK_INIT(inode_getsecurity, selinux_inode_getsecurity),
6816 LSM_HOOK_INIT(inode_setsecurity, selinux_inode_setsecurity),
6817 LSM_HOOK_INIT(inode_listsecurity, selinux_inode_listsecurity),
6818 LSM_HOOK_INIT(inode_getsecid, selinux_inode_getsecid),
Vivek Goyal56909eb2016-07-13 10:44:48 -04006819 LSM_HOOK_INIT(inode_copy_up, selinux_inode_copy_up),
Vivek Goyal19472b62016-07-13 10:44:50 -04006820 LSM_HOOK_INIT(inode_copy_up_xattr, selinux_inode_copy_up_xattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006821
Ondrej Mosnacekec882da2019-02-22 15:57:17 +01006822 LSM_HOOK_INIT(kernfs_init_security, selinux_kernfs_init_security),
6823
Casey Schauflere20b0432015-05-02 15:11:36 -07006824 LSM_HOOK_INIT(file_permission, selinux_file_permission),
6825 LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07006826 LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl),
6827 LSM_HOOK_INIT(mmap_file, selinux_mmap_file),
6828 LSM_HOOK_INIT(mmap_addr, selinux_mmap_addr),
6829 LSM_HOOK_INIT(file_mprotect, selinux_file_mprotect),
6830 LSM_HOOK_INIT(file_lock, selinux_file_lock),
6831 LSM_HOOK_INIT(file_fcntl, selinux_file_fcntl),
6832 LSM_HOOK_INIT(file_set_fowner, selinux_file_set_fowner),
6833 LSM_HOOK_INIT(file_send_sigiotask, selinux_file_send_sigiotask),
6834 LSM_HOOK_INIT(file_receive, selinux_file_receive),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006835
Casey Schauflere20b0432015-05-02 15:11:36 -07006836 LSM_HOOK_INIT(file_open, selinux_file_open),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006837
Tetsuo Handaa79be232017-03-28 23:08:45 +09006838 LSM_HOOK_INIT(task_alloc, selinux_task_alloc),
Casey Schauflere20b0432015-05-02 15:11:36 -07006839 LSM_HOOK_INIT(cred_prepare, selinux_cred_prepare),
6840 LSM_HOOK_INIT(cred_transfer, selinux_cred_transfer),
Matthew Garrett3ec30112018-01-08 13:36:19 -08006841 LSM_HOOK_INIT(cred_getsecid, selinux_cred_getsecid),
Casey Schauflere20b0432015-05-02 15:11:36 -07006842 LSM_HOOK_INIT(kernel_act_as, selinux_kernel_act_as),
6843 LSM_HOOK_INIT(kernel_create_files_as, selinux_kernel_create_files_as),
6844 LSM_HOOK_INIT(kernel_module_request, selinux_kernel_module_request),
Mimi Zoharc77b8cd2018-07-13 14:06:02 -04006845 LSM_HOOK_INIT(kernel_load_data, selinux_kernel_load_data),
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07006846 LSM_HOOK_INIT(kernel_read_file, selinux_kernel_read_file),
Casey Schauflere20b0432015-05-02 15:11:36 -07006847 LSM_HOOK_INIT(task_setpgid, selinux_task_setpgid),
6848 LSM_HOOK_INIT(task_getpgid, selinux_task_getpgid),
6849 LSM_HOOK_INIT(task_getsid, selinux_task_getsid),
6850 LSM_HOOK_INIT(task_getsecid, selinux_task_getsecid),
6851 LSM_HOOK_INIT(task_setnice, selinux_task_setnice),
6852 LSM_HOOK_INIT(task_setioprio, selinux_task_setioprio),
6853 LSM_HOOK_INIT(task_getioprio, selinux_task_getioprio),
Stephen Smalley791ec492017-02-17 07:57:00 -05006854 LSM_HOOK_INIT(task_prlimit, selinux_task_prlimit),
Casey Schauflere20b0432015-05-02 15:11:36 -07006855 LSM_HOOK_INIT(task_setrlimit, selinux_task_setrlimit),
6856 LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler),
6857 LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler),
6858 LSM_HOOK_INIT(task_movememory, selinux_task_movememory),
6859 LSM_HOOK_INIT(task_kill, selinux_task_kill),
Casey Schauflere20b0432015-05-02 15:11:36 -07006860 LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode),
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09006861
Casey Schauflere20b0432015-05-02 15:11:36 -07006862 LSM_HOOK_INIT(ipc_permission, selinux_ipc_permission),
6863 LSM_HOOK_INIT(ipc_getsecid, selinux_ipc_getsecid),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006864
Casey Schauflere20b0432015-05-02 15:11:36 -07006865 LSM_HOOK_INIT(msg_msg_alloc_security, selinux_msg_msg_alloc_security),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006866
Casey Schauflere20b0432015-05-02 15:11:36 -07006867 LSM_HOOK_INIT(msg_queue_alloc_security,
6868 selinux_msg_queue_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07006869 LSM_HOOK_INIT(msg_queue_associate, selinux_msg_queue_associate),
6870 LSM_HOOK_INIT(msg_queue_msgctl, selinux_msg_queue_msgctl),
6871 LSM_HOOK_INIT(msg_queue_msgsnd, selinux_msg_queue_msgsnd),
6872 LSM_HOOK_INIT(msg_queue_msgrcv, selinux_msg_queue_msgrcv),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006873
Casey Schauflere20b0432015-05-02 15:11:36 -07006874 LSM_HOOK_INIT(shm_alloc_security, selinux_shm_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07006875 LSM_HOOK_INIT(shm_associate, selinux_shm_associate),
6876 LSM_HOOK_INIT(shm_shmctl, selinux_shm_shmctl),
6877 LSM_HOOK_INIT(shm_shmat, selinux_shm_shmat),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006878
Casey Schauflere20b0432015-05-02 15:11:36 -07006879 LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07006880 LSM_HOOK_INIT(sem_associate, selinux_sem_associate),
6881 LSM_HOOK_INIT(sem_semctl, selinux_sem_semctl),
6882 LSM_HOOK_INIT(sem_semop, selinux_sem_semop),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006883
Casey Schauflere20b0432015-05-02 15:11:36 -07006884 LSM_HOOK_INIT(d_instantiate, selinux_d_instantiate),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006885
Casey Schauflere20b0432015-05-02 15:11:36 -07006886 LSM_HOOK_INIT(getprocattr, selinux_getprocattr),
6887 LSM_HOOK_INIT(setprocattr, selinux_setprocattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006888
Casey Schauflere20b0432015-05-02 15:11:36 -07006889 LSM_HOOK_INIT(ismaclabel, selinux_ismaclabel),
6890 LSM_HOOK_INIT(secid_to_secctx, selinux_secid_to_secctx),
6891 LSM_HOOK_INIT(secctx_to_secid, selinux_secctx_to_secid),
6892 LSM_HOOK_INIT(release_secctx, selinux_release_secctx),
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006893 LSM_HOOK_INIT(inode_invalidate_secctx, selinux_inode_invalidate_secctx),
Casey Schauflere20b0432015-05-02 15:11:36 -07006894 LSM_HOOK_INIT(inode_notifysecctx, selinux_inode_notifysecctx),
6895 LSM_HOOK_INIT(inode_setsecctx, selinux_inode_setsecctx),
6896 LSM_HOOK_INIT(inode_getsecctx, selinux_inode_getsecctx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006897
Casey Schauflere20b0432015-05-02 15:11:36 -07006898 LSM_HOOK_INIT(unix_stream_connect, selinux_socket_unix_stream_connect),
6899 LSM_HOOK_INIT(unix_may_send, selinux_socket_unix_may_send),
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006900
Casey Schauflere20b0432015-05-02 15:11:36 -07006901 LSM_HOOK_INIT(socket_create, selinux_socket_create),
6902 LSM_HOOK_INIT(socket_post_create, selinux_socket_post_create),
David Herrmann0b811db2018-05-04 16:28:21 +02006903 LSM_HOOK_INIT(socket_socketpair, selinux_socket_socketpair),
Casey Schauflere20b0432015-05-02 15:11:36 -07006904 LSM_HOOK_INIT(socket_bind, selinux_socket_bind),
6905 LSM_HOOK_INIT(socket_connect, selinux_socket_connect),
6906 LSM_HOOK_INIT(socket_listen, selinux_socket_listen),
6907 LSM_HOOK_INIT(socket_accept, selinux_socket_accept),
6908 LSM_HOOK_INIT(socket_sendmsg, selinux_socket_sendmsg),
6909 LSM_HOOK_INIT(socket_recvmsg, selinux_socket_recvmsg),
6910 LSM_HOOK_INIT(socket_getsockname, selinux_socket_getsockname),
6911 LSM_HOOK_INIT(socket_getpeername, selinux_socket_getpeername),
6912 LSM_HOOK_INIT(socket_getsockopt, selinux_socket_getsockopt),
6913 LSM_HOOK_INIT(socket_setsockopt, selinux_socket_setsockopt),
6914 LSM_HOOK_INIT(socket_shutdown, selinux_socket_shutdown),
6915 LSM_HOOK_INIT(socket_sock_rcv_skb, selinux_socket_sock_rcv_skb),
6916 LSM_HOOK_INIT(socket_getpeersec_stream,
6917 selinux_socket_getpeersec_stream),
6918 LSM_HOOK_INIT(socket_getpeersec_dgram, selinux_socket_getpeersec_dgram),
6919 LSM_HOOK_INIT(sk_alloc_security, selinux_sk_alloc_security),
6920 LSM_HOOK_INIT(sk_free_security, selinux_sk_free_security),
6921 LSM_HOOK_INIT(sk_clone_security, selinux_sk_clone_security),
6922 LSM_HOOK_INIT(sk_getsecid, selinux_sk_getsecid),
6923 LSM_HOOK_INIT(sock_graft, selinux_sock_graft),
Richard Hainesd4529302018-02-13 20:57:18 +00006924 LSM_HOOK_INIT(sctp_assoc_request, selinux_sctp_assoc_request),
6925 LSM_HOOK_INIT(sctp_sk_clone, selinux_sctp_sk_clone),
6926 LSM_HOOK_INIT(sctp_bind_connect, selinux_sctp_bind_connect),
Casey Schauflere20b0432015-05-02 15:11:36 -07006927 LSM_HOOK_INIT(inet_conn_request, selinux_inet_conn_request),
6928 LSM_HOOK_INIT(inet_csk_clone, selinux_inet_csk_clone),
6929 LSM_HOOK_INIT(inet_conn_established, selinux_inet_conn_established),
6930 LSM_HOOK_INIT(secmark_relabel_packet, selinux_secmark_relabel_packet),
6931 LSM_HOOK_INIT(secmark_refcount_inc, selinux_secmark_refcount_inc),
6932 LSM_HOOK_INIT(secmark_refcount_dec, selinux_secmark_refcount_dec),
6933 LSM_HOOK_INIT(req_classify_flow, selinux_req_classify_flow),
6934 LSM_HOOK_INIT(tun_dev_alloc_security, selinux_tun_dev_alloc_security),
6935 LSM_HOOK_INIT(tun_dev_free_security, selinux_tun_dev_free_security),
6936 LSM_HOOK_INIT(tun_dev_create, selinux_tun_dev_create),
6937 LSM_HOOK_INIT(tun_dev_attach_queue, selinux_tun_dev_attach_queue),
6938 LSM_HOOK_INIT(tun_dev_attach, selinux_tun_dev_attach),
6939 LSM_HOOK_INIT(tun_dev_open, selinux_tun_dev_open),
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006940#ifdef CONFIG_SECURITY_INFINIBAND
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006941 LSM_HOOK_INIT(ib_pkey_access, selinux_ib_pkey_access),
Daniel Jurgensab861df2017-05-19 15:48:58 +03006942 LSM_HOOK_INIT(ib_endport_manage_subnet,
6943 selinux_ib_endport_manage_subnet),
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006944 LSM_HOOK_INIT(ib_alloc_security, selinux_ib_alloc_security),
6945 LSM_HOOK_INIT(ib_free_security, selinux_ib_free_security),
6946#endif
Trent Jaegerd28d1e02005-12-13 23:12:40 -08006947#ifdef CONFIG_SECURITY_NETWORK_XFRM
Casey Schauflere20b0432015-05-02 15:11:36 -07006948 LSM_HOOK_INIT(xfrm_policy_alloc_security, selinux_xfrm_policy_alloc),
6949 LSM_HOOK_INIT(xfrm_policy_clone_security, selinux_xfrm_policy_clone),
6950 LSM_HOOK_INIT(xfrm_policy_free_security, selinux_xfrm_policy_free),
6951 LSM_HOOK_INIT(xfrm_policy_delete_security, selinux_xfrm_policy_delete),
6952 LSM_HOOK_INIT(xfrm_state_alloc, selinux_xfrm_state_alloc),
6953 LSM_HOOK_INIT(xfrm_state_alloc_acquire,
6954 selinux_xfrm_state_alloc_acquire),
6955 LSM_HOOK_INIT(xfrm_state_free_security, selinux_xfrm_state_free),
6956 LSM_HOOK_INIT(xfrm_state_delete_security, selinux_xfrm_state_delete),
6957 LSM_HOOK_INIT(xfrm_policy_lookup, selinux_xfrm_policy_lookup),
6958 LSM_HOOK_INIT(xfrm_state_pol_flow_match,
6959 selinux_xfrm_state_pol_flow_match),
6960 LSM_HOOK_INIT(xfrm_decode_session, selinux_xfrm_decode_session),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006961#endif
Michael LeMayd7200242006-06-22 14:47:17 -07006962
6963#ifdef CONFIG_KEYS
Casey Schauflere20b0432015-05-02 15:11:36 -07006964 LSM_HOOK_INIT(key_alloc, selinux_key_alloc),
6965 LSM_HOOK_INIT(key_free, selinux_key_free),
6966 LSM_HOOK_INIT(key_permission, selinux_key_permission),
6967 LSM_HOOK_INIT(key_getsecurity, selinux_key_getsecurity),
Michael LeMayd7200242006-06-22 14:47:17 -07006968#endif
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02006969
6970#ifdef CONFIG_AUDIT
Casey Schauflere20b0432015-05-02 15:11:36 -07006971 LSM_HOOK_INIT(audit_rule_init, selinux_audit_rule_init),
6972 LSM_HOOK_INIT(audit_rule_known, selinux_audit_rule_known),
6973 LSM_HOOK_INIT(audit_rule_match, selinux_audit_rule_match),
6974 LSM_HOOK_INIT(audit_rule_free, selinux_audit_rule_free),
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02006975#endif
Chenbo Fengec27c352017-10-18 13:00:25 -07006976
6977#ifdef CONFIG_BPF_SYSCALL
6978 LSM_HOOK_INIT(bpf, selinux_bpf),
6979 LSM_HOOK_INIT(bpf_map, selinux_bpf_map),
6980 LSM_HOOK_INIT(bpf_prog, selinux_bpf_prog),
6981 LSM_HOOK_INIT(bpf_map_alloc_security, selinux_bpf_map_alloc),
6982 LSM_HOOK_INIT(bpf_prog_alloc_security, selinux_bpf_prog_alloc),
6983 LSM_HOOK_INIT(bpf_map_free_security, selinux_bpf_map_free),
6984 LSM_HOOK_INIT(bpf_prog_free_security, selinux_bpf_prog_free),
6985#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006986};
6987
6988static __init int selinux_init(void)
6989{
peter enderborgc103a912018-06-12 10:09:03 +02006990 pr_info("SELinux: Initializing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006991
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006992 memset(&selinux_state, 0, sizeof(selinux_state));
Paul Mooree5a5ca92018-03-01 17:38:30 -05006993 enforcing_set(&selinux_state, selinux_enforcing_boot);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006994 selinux_state.checkreqprot = selinux_checkreqprot_boot;
6995 selinux_ss_init(&selinux_state.ss);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006996 selinux_avc_init(&selinux_state.avc);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006997
Linus Torvalds1da177e2005-04-16 15:20:36 -07006998 /* Set the security state for the initial task. */
David Howellsd84f4f92008-11-14 10:39:23 +11006999 cred_init_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007000
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04007001 default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
7002
Linus Torvalds1da177e2005-04-16 15:20:36 -07007003 avc_init();
7004
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007005 avtab_cache_init();
7006
7007 ebitmap_cache_init();
7008
7009 hashtab_cache_init();
7010
Casey Schauflerd69dece52017-01-18 17:09:05 -08007011 security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks), "selinux");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007012
Paul Moore615e51f2014-06-26 14:33:56 -04007013 if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET))
7014 panic("SELinux: Unable to register AVC netcache callback\n");
7015
Daniel Jurgens8f408ab2017-05-19 15:48:53 +03007016 if (avc_add_callback(selinux_lsm_notifier_avc_callback, AVC_CALLBACK_RESET))
7017 panic("SELinux: Unable to register AVC LSM notifier callback\n");
7018
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007019 if (selinux_enforcing_boot)
peter enderborgc103a912018-06-12 10:09:03 +02007020 pr_debug("SELinux: Starting in enforcing mode\n");
Eric Paris828dfe12008-04-17 13:17:49 -04007021 else
peter enderborgc103a912018-06-12 10:09:03 +02007022 pr_debug("SELinux: Starting in permissive mode\n");
Michael LeMayd7200242006-06-22 14:47:17 -07007023
David Howells442155c2018-11-01 23:07:24 +00007024 fs_validate_description(&selinux_fs_parameters);
7025
Linus Torvalds1da177e2005-04-16 15:20:36 -07007026 return 0;
7027}
7028
Al Viroe8c26252010-03-23 06:36:54 -04007029static void delayed_superblock_init(struct super_block *sb, void *unused)
7030{
Al Viro204cc0c2018-12-13 13:41:47 -05007031 selinux_set_mnt_opts(sb, NULL, 0, NULL);
Al Viroe8c26252010-03-23 06:36:54 -04007032}
7033
Linus Torvalds1da177e2005-04-16 15:20:36 -07007034void selinux_complete_init(void)
7035{
peter enderborgc103a912018-06-12 10:09:03 +02007036 pr_debug("SELinux: Completing initialization.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007037
7038 /* Set up any superblocks initialized prior to the policy load. */
peter enderborgc103a912018-06-12 10:09:03 +02007039 pr_debug("SELinux: Setting up existing superblocks.\n");
Al Viroe8c26252010-03-23 06:36:54 -04007040 iterate_supers(delayed_superblock_init, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007041}
7042
7043/* SELinux requires early initialization in order to label
7044 all processes and objects when they are created. */
Kees Cook3d6e5f62018-10-10 17:18:23 -07007045DEFINE_LSM(selinux) = {
Kees Cook07aed2f2018-10-10 17:18:24 -07007046 .name = "selinux",
Kees Cook14bd99c2018-09-19 19:57:06 -07007047 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
Kees Cookc5459b82018-09-13 22:28:48 -07007048 .enabled = &selinux_enabled,
Casey Schauflerbbd36622018-11-12 09:30:56 -08007049 .blobs = &selinux_blob_sizes,
Kees Cook3d6e5f62018-10-10 17:18:23 -07007050 .init = selinux_init,
7051};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007052
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007053#if defined(CONFIG_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007054
Florian Westphal591bb272017-07-26 11:40:52 +02007055static const struct nf_hook_ops selinux_nf_ops[] = {
Paul Mooreeffad8d2008-01-29 08:49:27 -05007056 {
7057 .hook = selinux_ipv4_postroute,
Alban Crequy2597a832012-05-14 03:56:39 +00007058 .pf = NFPROTO_IPV4,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007059 .hooknum = NF_INET_POST_ROUTING,
7060 .priority = NF_IP_PRI_SELINUX_LAST,
7061 },
7062 {
7063 .hook = selinux_ipv4_forward,
Alban Crequy2597a832012-05-14 03:56:39 +00007064 .pf = NFPROTO_IPV4,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007065 .hooknum = NF_INET_FORWARD,
7066 .priority = NF_IP_PRI_SELINUX_FIRST,
Paul Moore948bf852008-10-10 10:16:32 -04007067 },
7068 {
7069 .hook = selinux_ipv4_output,
Alban Crequy2597a832012-05-14 03:56:39 +00007070 .pf = NFPROTO_IPV4,
Paul Moore948bf852008-10-10 10:16:32 -04007071 .hooknum = NF_INET_LOCAL_OUT,
7072 .priority = NF_IP_PRI_SELINUX_FIRST,
Jiri Pirko25db6be2014-09-03 17:42:13 +02007073 },
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04007074#if IS_ENABLED(CONFIG_IPV6)
Paul Mooreeffad8d2008-01-29 08:49:27 -05007075 {
7076 .hook = selinux_ipv6_postroute,
Alban Crequy2597a832012-05-14 03:56:39 +00007077 .pf = NFPROTO_IPV6,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007078 .hooknum = NF_INET_POST_ROUTING,
7079 .priority = NF_IP6_PRI_SELINUX_LAST,
7080 },
7081 {
7082 .hook = selinux_ipv6_forward,
Alban Crequy2597a832012-05-14 03:56:39 +00007083 .pf = NFPROTO_IPV6,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007084 .hooknum = NF_INET_FORWARD,
7085 .priority = NF_IP6_PRI_SELINUX_FIRST,
Jiri Pirko25db6be2014-09-03 17:42:13 +02007086 },
Huw Davies2917f572016-06-27 15:06:15 -04007087 {
7088 .hook = selinux_ipv6_output,
7089 .pf = NFPROTO_IPV6,
7090 .hooknum = NF_INET_LOCAL_OUT,
7091 .priority = NF_IP6_PRI_SELINUX_FIRST,
7092 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07007093#endif /* IPV6 */
Jiri Pirko25db6be2014-09-03 17:42:13 +02007094};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007095
Florian Westphal8e71bf72017-04-21 11:49:09 +02007096static int __net_init selinux_nf_register(struct net *net)
7097{
7098 return nf_register_net_hooks(net, selinux_nf_ops,
7099 ARRAY_SIZE(selinux_nf_ops));
7100}
7101
7102static void __net_exit selinux_nf_unregister(struct net *net)
7103{
7104 nf_unregister_net_hooks(net, selinux_nf_ops,
7105 ARRAY_SIZE(selinux_nf_ops));
7106}
7107
7108static struct pernet_operations selinux_net_ops = {
7109 .init = selinux_nf_register,
7110 .exit = selinux_nf_unregister,
7111};
7112
Linus Torvalds1da177e2005-04-16 15:20:36 -07007113static int __init selinux_nf_ip_init(void)
7114{
Jiri Pirko25db6be2014-09-03 17:42:13 +02007115 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007116
7117 if (!selinux_enabled)
Jiri Pirko25db6be2014-09-03 17:42:13 +02007118 return 0;
Eric Parisfadcdb42007-02-22 18:11:31 -05007119
peter enderborgc103a912018-06-12 10:09:03 +02007120 pr_debug("SELinux: Registering netfilter hooks\n");
Eric Parisfadcdb42007-02-22 18:11:31 -05007121
Florian Westphal8e71bf72017-04-21 11:49:09 +02007122 err = register_pernet_subsys(&selinux_net_ops);
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07007123 if (err)
Florian Westphal8e71bf72017-04-21 11:49:09 +02007124 panic("SELinux: register_pernet_subsys: error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007125
Jiri Pirko25db6be2014-09-03 17:42:13 +02007126 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007127}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007128__initcall(selinux_nf_ip_init);
7129
7130#ifdef CONFIG_SECURITY_SELINUX_DISABLE
7131static void selinux_nf_ip_exit(void)
7132{
peter enderborgc103a912018-06-12 10:09:03 +02007133 pr_debug("SELinux: Unregistering netfilter hooks\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007134
Florian Westphal8e71bf72017-04-21 11:49:09 +02007135 unregister_pernet_subsys(&selinux_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007136}
7137#endif
7138
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007139#else /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007140
7141#ifdef CONFIG_SECURITY_SELINUX_DISABLE
7142#define selinux_nf_ip_exit()
7143#endif
7144
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007145#endif /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007146
7147#ifdef CONFIG_SECURITY_SELINUX_DISABLE
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007148int selinux_disable(struct selinux_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007149{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007150 if (state->initialized) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007151 /* Not permitted after initial policy load. */
7152 return -EINVAL;
7153 }
7154
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007155 if (state->disabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007156 /* Only do this once. */
7157 return -EINVAL;
7158 }
7159
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007160 state->disabled = 1;
7161
peter enderborgc103a912018-06-12 10:09:03 +02007162 pr_info("SELinux: Disabled at runtime.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007163
Stephen Smalley30d55282006-05-03 10:52:36 -04007164 selinux_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007165
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07007166 security_delete_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007167
Eric Parisaf8ff042009-09-20 21:23:01 -04007168 /* Try to destroy the avc node cache */
7169 avc_disable();
7170
Linus Torvalds1da177e2005-04-16 15:20:36 -07007171 /* Unregister netfilter hooks. */
7172 selinux_nf_ip_exit();
7173
7174 /* Unregister selinuxfs. */
7175 exit_sel_fs();
7176
7177 return 0;
7178}
7179#endif