blob: ba3e2917bd24d358d8bd28e914b9cb3579bbc096 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * NSA Security-Enhanced Linux (SELinux) security module
3 *
4 * This file contains the SELinux hook function implementations.
5 *
Stephen Smalley7efbb602017-08-17 13:32:36 -04006 * Authors: Stephen Smalley, <sds@tycho.nsa.gov>
Eric Paris828dfe12008-04-17 13:17:49 -04007 * Chris Vance, <cvance@nai.com>
8 * Wayne Salamon, <wsalamon@nai.com>
9 * James Morris <jmorris@redhat.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * Copyright (C) 2001,2002 Networks Associates Technology, Inc.
Eric Paris2069f452008-07-04 09:47:13 +100012 * Copyright (C) 2003-2008 Red Hat, Inc., James Morris <jmorris@redhat.com>
13 * Eric Paris <eparis@redhat.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
Eric Paris828dfe12008-04-17 13:17:49 -040015 * <dgoeddel@trustedcs.com>
Paul Mooreed6d76e2009-08-28 18:12:49 -040016 * Copyright (C) 2006, 2007, 2009 Hewlett-Packard Development Company, L.P.
Paul Moore82c21bf2011-08-01 11:10:33 +000017 * Paul Moore <paul@paul-moore.com>
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +090018 * Copyright (C) 2007 Hitachi Software Engineering Co., Ltd.
Eric Paris828dfe12008-04-17 13:17:49 -040019 * Yuichi Nakamura <ynakam@hitachisoft.jp>
Daniel Jurgens3a976fa2017-05-19 15:48:56 +030020 * Copyright (C) 2016 Mellanox Technologies
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 *
22 * This program is free software; you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License version 2,
Eric Paris828dfe12008-04-17 13:17:49 -040024 * as published by the Free Software Foundation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 */
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/init.h>
Eric Paris0b24dcb2011-02-25 15:39:20 -050028#include <linux/kd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/kernel.h>
Roland McGrath0d094ef2008-07-25 19:45:49 -070030#include <linux/tracehook.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/errno.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010032#include <linux/sched/signal.h>
Ingo Molnar29930022017-02-08 18:51:36 +010033#include <linux/sched/task.h>
Casey Schaufler3c4ed7b2015-05-02 15:10:46 -070034#include <linux/lsm_hooks.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/xattr.h>
36#include <linux/capability.h>
37#include <linux/unistd.h>
38#include <linux/mm.h>
39#include <linux/mman.h>
40#include <linux/slab.h>
41#include <linux/pagemap.h>
Eric Paris0b24dcb2011-02-25 15:39:20 -050042#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/swap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/spinlock.h>
45#include <linux/syscalls.h>
Eric Paris2a7dba32011-02-01 11:05:39 -050046#include <linux/dcache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/file.h>
Al Viro9f3acc32008-04-24 07:44:08 -040048#include <linux/fdtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/namei.h>
50#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/netfilter_ipv4.h>
52#include <linux/netfilter_ipv6.h>
53#include <linux/tty.h>
54#include <net/icmp.h>
Stephen Hemminger227b60f2007-10-10 17:30:46 -070055#include <net/ip.h> /* for local_port_range[] */
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <net/tcp.h> /* struct or_callable used in sock_rcv_skb */
Paul Moore47180062013-12-04 16:10:45 -050057#include <net/inet_connection_sock.h>
Paul Moore220deb92008-01-29 08:38:23 -050058#include <net/net_namespace.h>
Paul Moored621d352008-01-29 08:43:36 -050059#include <net/netlabel.h>
Eric Parisf5269712008-05-14 11:27:45 -040060#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <asm/ioctls.h>
Arun Sharma600634972011-07-26 16:09:06 -070062#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <linux/bitops.h>
64#include <linux/interrupt.h>
65#include <linux/netdevice.h> /* for network interface checks */
Hong zhi guo77954982013-03-27 06:49:35 +000066#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <linux/tcp.h>
68#include <linux/udp.h>
James Morris2ee92d42006-11-13 16:09:01 -080069#include <linux/dccp.h>
Richard Hainesd4529302018-02-13 20:57:18 +000070#include <linux/sctp.h>
71#include <net/sctp/structs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <linux/quota.h>
73#include <linux/un.h> /* for Unix socket types */
74#include <net/af_unix.h> /* for Unix socket types */
75#include <linux/parser.h>
76#include <linux/nfs_mount.h>
77#include <net/ipv6.h>
78#include <linux/hugetlb.h>
79#include <linux/personality.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#include <linux/audit.h>
Eric Paris6931dfc2005-06-30 02:58:51 -070081#include <linux/string.h>
Catherine Zhang877ce7c2006-06-29 12:27:47 -070082#include <linux/selinux.h>
Eric Paris23970742006-09-25 23:32:01 -070083#include <linux/mutex.h>
Frank Mayharf06febc2008-09-12 09:54:39 -070084#include <linux/posix-timers.h>
Kees Cook00234592010-02-03 15:36:43 -080085#include <linux/syslog.h>
Serge E. Hallyn34867402011-03-23 16:43:17 -070086#include <linux/user_namespace.h>
Paul Gortmaker44fc7ea2011-05-26 20:52:10 -040087#include <linux/export.h>
Al Viro40401532012-02-13 03:58:52 +000088#include <linux/msg.h>
89#include <linux/shm.h>
Chenbo Fengec27c352017-10-18 13:00:25 -070090#include <linux/bpf.h>
David Howellse262e32d2018-11-01 23:07:23 +000091#include <uapi/linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93#include "avc.h"
94#include "objsec.h"
95#include "netif.h"
Paul Moore224dfbd2008-01-29 08:38:13 -050096#include "netnode.h"
Paul Moore3e1121722008-04-10 10:48:14 -040097#include "netport.h"
Daniel Jurgens409dcf32017-05-19 15:48:59 +030098#include "ibpkey.h"
Trent Jaegerd28d1e02005-12-13 23:12:40 -080099#include "xfrm.h"
Paul Moorec60475b2007-02-28 15:14:23 -0500100#include "netlabel.h"
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +0200101#include "audit.h"
James Morris7b98a582011-08-30 12:52:32 +1000102#include "avc_ss.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500104struct selinux_state selinux_state;
105
Paul Moored621d352008-01-29 08:43:36 -0500106/* SECMARK reference count */
James Morris56a4ca92011-08-17 11:08:43 +1000107static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0);
Paul Moored621d352008-01-29 08:43:36 -0500108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109#ifdef CONFIG_SECURITY_SELINUX_DEVELOP
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500110static int selinux_enforcing_boot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112static int __init enforcing_setup(char *str)
113{
Eric Parisf5269712008-05-14 11:27:45 -0400114 unsigned long enforcing;
Jingoo Han29707b22014-02-05 15:13:14 +0900115 if (!kstrtoul(str, 0, &enforcing))
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500116 selinux_enforcing_boot = enforcing ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return 1;
118}
119__setup("enforcing=", enforcing_setup);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500120#else
121#define selinux_enforcing_boot 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122#endif
123
124#ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM
125int selinux_enabled = CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE;
126
127static int __init selinux_enabled_setup(char *str)
128{
Eric Parisf5269712008-05-14 11:27:45 -0400129 unsigned long enabled;
Jingoo Han29707b22014-02-05 15:13:14 +0900130 if (!kstrtoul(str, 0, &enabled))
Eric Parisf5269712008-05-14 11:27:45 -0400131 selinux_enabled = enabled ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 return 1;
133}
134__setup("selinux=", selinux_enabled_setup);
Stephen Smalley30d55282006-05-03 10:52:36 -0400135#else
136int selinux_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137#endif
138
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500139static unsigned int selinux_checkreqprot_boot =
140 CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE;
141
142static int __init checkreqprot_setup(char *str)
143{
144 unsigned long checkreqprot;
145
146 if (!kstrtoul(str, 0, &checkreqprot))
147 selinux_checkreqprot_boot = checkreqprot ? 1 : 0;
148 return 1;
149}
150__setup("checkreqprot=", checkreqprot_setup);
151
Christoph Lametere18b8902006-12-06 20:33:20 -0800152static struct kmem_cache *sel_inode_cache;
Sangwoo63205652015-10-21 17:44:30 -0400153static struct kmem_cache *file_security_cache;
James Morris7cae7e22006-03-22 00:09:22 -0800154
Paul Moored621d352008-01-29 08:43:36 -0500155/**
156 * selinux_secmark_enabled - Check to see if SECMARK is currently enabled
157 *
158 * Description:
159 * This function checks the SECMARK reference counter to see if any SECMARK
160 * targets are currently configured, if the reference counter is greater than
161 * zero SECMARK is considered to be enabled. Returns true (1) if SECMARK is
Chris PeBenito2be4d742013-05-03 09:05:39 -0400162 * enabled, false (0) if SECMARK is disabled. If the always_check_network
163 * policy capability is enabled, SECMARK is always considered enabled.
Paul Moored621d352008-01-29 08:43:36 -0500164 *
165 */
166static int selinux_secmark_enabled(void)
167{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500168 return (selinux_policycap_alwaysnetwork() ||
169 atomic_read(&selinux_secmark_refcount));
Chris PeBenito2be4d742013-05-03 09:05:39 -0400170}
171
172/**
173 * selinux_peerlbl_enabled - Check to see if peer labeling is currently enabled
174 *
175 * Description:
176 * This function checks if NetLabel or labeled IPSEC is enabled. Returns true
177 * (1) if any are enabled or false (0) if neither are enabled. If the
178 * always_check_network policy capability is enabled, peer labeling
179 * is always considered enabled.
180 *
181 */
182static int selinux_peerlbl_enabled(void)
183{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500184 return (selinux_policycap_alwaysnetwork() ||
185 netlbl_enabled() || selinux_xfrm_enabled());
Paul Moored621d352008-01-29 08:43:36 -0500186}
187
Paul Moore615e51f2014-06-26 14:33:56 -0400188static int selinux_netcache_avc_callback(u32 event)
189{
190 if (event == AVC_CALLBACK_RESET) {
191 sel_netif_flush();
192 sel_netnode_flush();
193 sel_netport_flush();
194 synchronize_net();
195 }
196 return 0;
197}
198
Daniel Jurgens8f408ab2017-05-19 15:48:53 +0300199static int selinux_lsm_notifier_avc_callback(u32 event)
200{
Daniel Jurgens409dcf32017-05-19 15:48:59 +0300201 if (event == AVC_CALLBACK_RESET) {
202 sel_ib_pkey_flush();
Daniel Jurgens8f408ab2017-05-19 15:48:53 +0300203 call_lsm_notifier(LSM_POLICY_CHANGE, NULL);
Daniel Jurgens409dcf32017-05-19 15:48:59 +0300204 }
Daniel Jurgens8f408ab2017-05-19 15:48:53 +0300205
206 return 0;
207}
208
David Howellsd84f4f92008-11-14 10:39:23 +1100209/*
210 * initialise the security for the init task
211 */
212static void cred_init_security(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
David Howells3b11a1d2008-11-14 10:39:26 +1100214 struct cred *cred = (struct cred *) current->real_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 struct task_security_struct *tsec;
216
James Morris89d155e2005-10-30 14:59:21 -0800217 tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 if (!tsec)
David Howellsd84f4f92008-11-14 10:39:23 +1100219 panic("SELinux: Failed to initialize initial task.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
David Howellsd84f4f92008-11-14 10:39:23 +1100221 tsec->osid = tsec->sid = SECINITSID_KERNEL;
David Howellsf1752ee2008-11-14 10:39:17 +1100222 cred->security = tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
David Howells275bb412008-11-14 10:39:19 +1100225/*
David Howells88e67f32008-11-14 10:39:21 +1100226 * get the security ID of a set of credentials
227 */
228static inline u32 cred_sid(const struct cred *cred)
229{
230 const struct task_security_struct *tsec;
231
232 tsec = cred->security;
233 return tsec->sid;
234}
235
236/*
David Howells3b11a1d2008-11-14 10:39:26 +1100237 * get the objective security ID of a task
David Howells275bb412008-11-14 10:39:19 +1100238 */
239static inline u32 task_sid(const struct task_struct *task)
240{
David Howells275bb412008-11-14 10:39:19 +1100241 u32 sid;
242
243 rcu_read_lock();
David Howells88e67f32008-11-14 10:39:21 +1100244 sid = cred_sid(__task_cred(task));
David Howells275bb412008-11-14 10:39:19 +1100245 rcu_read_unlock();
246 return sid;
247}
248
David Howells88e67f32008-11-14 10:39:21 +1100249/* Allocate and free functions for each kind of security blob. */
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251static int inode_alloc_security(struct inode *inode)
252{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 struct inode_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +1100254 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Josef Bacika02fe132008-04-04 09:35:05 +1100256 isec = kmem_cache_zalloc(sel_inode_cache, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 if (!isec)
258 return -ENOMEM;
259
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +0100260 spin_lock_init(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 INIT_LIST_HEAD(&isec->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 isec->inode = inode;
263 isec->sid = SECINITSID_UNLABELED;
264 isec->sclass = SECCLASS_FILE;
David Howells275bb412008-11-14 10:39:19 +1100265 isec->task_sid = sid;
Andreas Gruenbacher42059112016-11-10 22:18:27 +0100266 isec->initialized = LABEL_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 inode->i_security = isec;
268
269 return 0;
270}
271
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500272static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry);
273
274/*
275 * Try reloading inode security labels that have been marked as invalid. The
276 * @may_sleep parameter indicates when sleeping and thus reloading labels is
Andreas Gruenbacher42059112016-11-10 22:18:27 +0100277 * allowed; when set to false, returns -ECHILD when the label is
Al Viroe9193282018-04-24 21:31:02 -0400278 * invalid. The @dentry parameter should be set to a dentry of the inode.
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500279 */
280static int __inode_security_revalidate(struct inode *inode,
Al Viroe9193282018-04-24 21:31:02 -0400281 struct dentry *dentry,
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500282 bool may_sleep)
283{
284 struct inode_security_struct *isec = inode->i_security;
285
286 might_sleep_if(may_sleep);
287
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500288 if (selinux_state.initialized &&
289 isec->initialized != LABEL_INITIALIZED) {
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500290 if (!may_sleep)
291 return -ECHILD;
292
293 /*
294 * Try reloading the inode security label. This will fail if
295 * @opt_dentry is NULL and no dentry for this inode can be
296 * found; in that case, continue using the old label.
297 */
Al Viroe9193282018-04-24 21:31:02 -0400298 inode_doinit_with_dentry(inode, dentry);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500299 }
300 return 0;
301}
302
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500303static struct inode_security_struct *inode_security_novalidate(struct inode *inode)
304{
305 return inode->i_security;
306}
307
308static struct inode_security_struct *inode_security_rcu(struct inode *inode, bool rcu)
309{
310 int error;
311
312 error = __inode_security_revalidate(inode, NULL, !rcu);
313 if (error)
314 return ERR_PTR(error);
315 return inode->i_security;
316}
317
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500318/*
319 * Get the security label of an inode.
320 */
321static struct inode_security_struct *inode_security(struct inode *inode)
322{
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500323 __inode_security_revalidate(inode, NULL, true);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500324 return inode->i_security;
325}
326
Paul Moore2c971652016-04-19 16:36:28 -0400327static struct inode_security_struct *backing_inode_security_novalidate(struct dentry *dentry)
328{
329 struct inode *inode = d_backing_inode(dentry);
330
331 return inode->i_security;
332}
333
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500334/*
335 * Get the security label of a dentry's backing inode.
336 */
337static struct inode_security_struct *backing_inode_security(struct dentry *dentry)
338{
339 struct inode *inode = d_backing_inode(dentry);
340
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500341 __inode_security_revalidate(inode, dentry, true);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500342 return inode->i_security;
343}
344
Steven Rostedt3dc91d42014-01-09 21:46:34 -0500345static void inode_free_rcu(struct rcu_head *head)
346{
347 struct inode_security_struct *isec;
348
349 isec = container_of(head, struct inode_security_struct, rcu);
350 kmem_cache_free(sel_inode_cache, isec);
351}
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353static void inode_free_security(struct inode *inode)
354{
355 struct inode_security_struct *isec = inode->i_security;
356 struct superblock_security_struct *sbsec = inode->i_sb->s_security;
357
Waiman Long9629d042015-07-10 17:19:56 -0400358 /*
359 * As not all inode security structures are in a list, we check for
360 * empty list outside of the lock to make sure that we won't waste
361 * time taking a lock doing nothing.
362 *
363 * The list_del_init() function can be safely called more than once.
364 * It should not be possible for this function to be called with
365 * concurrent list_add(), but for better safety against future changes
366 * in the code, we use list_empty_careful() here.
367 */
368 if (!list_empty_careful(&isec->list)) {
369 spin_lock(&sbsec->isec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 list_del_init(&isec->list);
Waiman Long9629d042015-07-10 17:19:56 -0400371 spin_unlock(&sbsec->isec_lock);
372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Steven Rostedt3dc91d42014-01-09 21:46:34 -0500374 /*
375 * The inode may still be referenced in a path walk and
376 * a call to selinux_inode_permission() can be made
377 * after inode_free_security() is called. Ideally, the VFS
378 * wouldn't do this, but fixing that is a much harder
379 * job. For now, simply free the i_security via RCU, and
380 * leave the current inode->i_security pointer intact.
381 * The inode will be freed after the RCU grace period too.
382 */
383 call_rcu(&isec->rcu, inode_free_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
385
386static int file_alloc_security(struct file *file)
387{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 struct file_security_struct *fsec;
David Howells275bb412008-11-14 10:39:19 +1100389 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Sangwoo63205652015-10-21 17:44:30 -0400391 fsec = kmem_cache_zalloc(file_security_cache, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 if (!fsec)
393 return -ENOMEM;
394
David Howells275bb412008-11-14 10:39:19 +1100395 fsec->sid = sid;
396 fsec->fown_sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 file->f_security = fsec;
398
399 return 0;
400}
401
402static void file_free_security(struct file *file)
403{
404 struct file_security_struct *fsec = file->f_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 file->f_security = NULL;
Sangwoo63205652015-10-21 17:44:30 -0400406 kmem_cache_free(file_security_cache, fsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
409static int superblock_alloc_security(struct super_block *sb)
410{
411 struct superblock_security_struct *sbsec;
412
James Morris89d155e2005-10-30 14:59:21 -0800413 sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (!sbsec)
415 return -ENOMEM;
416
Eric Parisbc7e9822006-09-25 23:32:02 -0700417 mutex_init(&sbsec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 INIT_LIST_HEAD(&sbsec->isec_head);
419 spin_lock_init(&sbsec->isec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 sbsec->sb = sb;
421 sbsec->sid = SECINITSID_UNLABELED;
422 sbsec->def_sid = SECINITSID_FILE;
Eric Parisc312feb2006-07-10 04:43:53 -0700423 sbsec->mntpoint_sid = SECINITSID_UNLABELED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 sb->s_security = sbsec;
425
426 return 0;
427}
428
429static void superblock_free_security(struct super_block *sb)
430{
431 struct superblock_security_struct *sbsec = sb->s_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 sb->s_security = NULL;
433 kfree(sbsec);
434}
435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436static inline int inode_doinit(struct inode *inode)
437{
438 return inode_doinit_with_dentry(inode, NULL);
439}
440
441enum {
Eric Paris31e87932007-09-19 17:19:12 -0400442 Opt_error = -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 Opt_context = 1,
444 Opt_fscontext = 2,
Eric Parisc9180a52007-11-30 13:00:35 -0500445 Opt_defcontext = 3,
446 Opt_rootcontext = 4,
David P. Quigley11689d42009-01-16 09:22:03 -0500447 Opt_labelsupport = 5,
Eric Parisd355987f2012-08-24 15:58:53 -0400448 Opt_nextmntopt = 6,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449};
450
Eric Parisd355987f2012-08-24 15:58:53 -0400451#define NUM_SEL_MNT_OPTS (Opt_nextmntopt - 1)
452
Steven Whitehousea447c092008-10-13 10:46:57 +0100453static const match_table_t tokens = {
Eric Paris832cbd92008-04-01 13:24:09 -0400454 {Opt_context, CONTEXT_STR "%s"},
455 {Opt_fscontext, FSCONTEXT_STR "%s"},
456 {Opt_defcontext, DEFCONTEXT_STR "%s"},
457 {Opt_rootcontext, ROOTCONTEXT_STR "%s"},
David P. Quigley11689d42009-01-16 09:22:03 -0500458 {Opt_labelsupport, LABELSUPP_STR},
Eric Paris31e87932007-09-19 17:19:12 -0400459 {Opt_error, NULL},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460};
461
462#define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n"
463
Eric Parisc312feb2006-07-10 04:43:53 -0700464static int may_context_mount_sb_relabel(u32 sid,
465 struct superblock_security_struct *sbsec,
David Howells275bb412008-11-14 10:39:19 +1100466 const struct cred *cred)
Eric Parisc312feb2006-07-10 04:43:53 -0700467{
David Howells275bb412008-11-14 10:39:19 +1100468 const struct task_security_struct *tsec = cred->security;
Eric Parisc312feb2006-07-10 04:43:53 -0700469 int rc;
470
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500471 rc = avc_has_perm(&selinux_state,
472 tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
Eric Parisc312feb2006-07-10 04:43:53 -0700473 FILESYSTEM__RELABELFROM, NULL);
474 if (rc)
475 return rc;
476
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500477 rc = avc_has_perm(&selinux_state,
478 tsec->sid, sid, SECCLASS_FILESYSTEM,
Eric Parisc312feb2006-07-10 04:43:53 -0700479 FILESYSTEM__RELABELTO, NULL);
480 return rc;
481}
482
Eric Paris08089252006-07-10 04:43:55 -0700483static int may_context_mount_inode_relabel(u32 sid,
484 struct superblock_security_struct *sbsec,
David Howells275bb412008-11-14 10:39:19 +1100485 const struct cred *cred)
Eric Paris08089252006-07-10 04:43:55 -0700486{
David Howells275bb412008-11-14 10:39:19 +1100487 const struct task_security_struct *tsec = cred->security;
Eric Paris08089252006-07-10 04:43:55 -0700488 int rc;
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500489 rc = avc_has_perm(&selinux_state,
490 tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
Eric Paris08089252006-07-10 04:43:55 -0700491 FILESYSTEM__RELABELFROM, NULL);
492 if (rc)
493 return rc;
494
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500495 rc = avc_has_perm(&selinux_state,
496 sid, sbsec->sid, SECCLASS_FILESYSTEM,
Eric Paris08089252006-07-10 04:43:55 -0700497 FILESYSTEM__ASSOCIATE, NULL);
498 return rc;
499}
500
Eric Parisb43e7252012-10-10 14:27:35 -0400501static int selinux_is_sblabel_mnt(struct super_block *sb)
502{
503 struct superblock_security_struct *sbsec = sb->s_security;
504
Mark Salyzynd5f3a5f2015-02-04 11:34:30 -0500505 return sbsec->behavior == SECURITY_FS_USE_XATTR ||
506 sbsec->behavior == SECURITY_FS_USE_TRANS ||
507 sbsec->behavior == SECURITY_FS_USE_TASK ||
J. Bruce Fields9fc2b4b2015-06-04 15:57:25 -0400508 sbsec->behavior == SECURITY_FS_USE_NATIVE ||
Mark Salyzynd5f3a5f2015-02-04 11:34:30 -0500509 /* Special handling. Genfs but also in-core setxattr handler */
510 !strcmp(sb->s_type->name, "sysfs") ||
511 !strcmp(sb->s_type->name, "pstore") ||
512 !strcmp(sb->s_type->name, "debugfs") ||
Yongqin Liua2c7c6f2017-01-09 10:07:30 -0500513 !strcmp(sb->s_type->name, "tracefs") ||
Stephen Smalley2651225b2017-02-28 10:35:56 -0500514 !strcmp(sb->s_type->name, "rootfs") ||
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500515 (selinux_policycap_cgroupseclabel() &&
Stephen Smalley2651225b2017-02-28 10:35:56 -0500516 (!strcmp(sb->s_type->name, "cgroup") ||
517 !strcmp(sb->s_type->name, "cgroup2")));
Eric Parisb43e7252012-10-10 14:27:35 -0400518}
519
Eric Parisc9180a52007-11-30 13:00:35 -0500520static int sb_finish_set_opts(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
522 struct superblock_security_struct *sbsec = sb->s_security;
523 struct dentry *root = sb->s_root;
David Howellsc6f493d2015-03-17 22:26:22 +0000524 struct inode *root_inode = d_backing_inode(root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 int rc = 0;
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
528 /* Make sure that the xattr handler exists and that no
529 error other than -ENODATA is returned by getxattr on
530 the root directory. -ENODATA is ok, as this may be
531 the first boot of the SELinux kernel before we have
532 assigned xattr values to the filesystem. */
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200533 if (!(root_inode->i_opflags & IOP_XATTR)) {
peter enderborgc103a912018-06-12 10:09:03 +0200534 pr_warn("SELinux: (dev %s, type %s) has no "
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800535 "xattr support\n", sb->s_id, sb->s_type->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 rc = -EOPNOTSUPP;
537 goto out;
538 }
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200539
540 rc = __vfs_getxattr(root, root_inode, XATTR_NAME_SELINUX, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (rc < 0 && rc != -ENODATA) {
542 if (rc == -EOPNOTSUPP)
peter enderborgc103a912018-06-12 10:09:03 +0200543 pr_warn("SELinux: (dev %s, type "
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800544 "%s) has no security xattr handler\n",
545 sb->s_id, sb->s_type->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 else
peter enderborgc103a912018-06-12 10:09:03 +0200547 pr_warn("SELinux: (dev %s, type "
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800548 "%s) getxattr errno %d\n", sb->s_id,
549 sb->s_type->name, -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 goto out;
551 }
552 }
553
Eric Pariseadcabc2012-08-24 15:59:14 -0400554 sbsec->flags |= SE_SBINITIALIZED;
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400555
556 /*
557 * Explicitly set or clear SBLABEL_MNT. It's not sufficient to simply
558 * leave the flag untouched because sb_clone_mnt_opts might be handing
559 * us a superblock that needs the flag to be cleared.
560 */
Eric Parisb43e7252012-10-10 14:27:35 -0400561 if (selinux_is_sblabel_mnt(sb))
Eric Paris12f348b2012-10-09 10:56:25 -0400562 sbsec->flags |= SBLABEL_MNT;
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400563 else
564 sbsec->flags &= ~SBLABEL_MNT;
David P. Quigleyddd29ec2009-09-09 14:25:37 -0400565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 /* Initialize the root inode. */
Eric Parisc9180a52007-11-30 13:00:35 -0500567 rc = inode_doinit_with_dentry(root_inode, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 /* Initialize any other inodes associated with the superblock, e.g.
570 inodes created prior to initial policy load or inodes created
571 during get_sb by a pseudo filesystem that directly
572 populates itself. */
573 spin_lock(&sbsec->isec_lock);
574next_inode:
575 if (!list_empty(&sbsec->isec_head)) {
576 struct inode_security_struct *isec =
577 list_entry(sbsec->isec_head.next,
Eric Parisc9180a52007-11-30 13:00:35 -0500578 struct inode_security_struct, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 struct inode *inode = isec->inode;
Stephen Smalley923190d2014-10-06 16:32:52 -0400580 list_del_init(&isec->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 spin_unlock(&sbsec->isec_lock);
582 inode = igrab(inode);
583 if (inode) {
Eric Parisc9180a52007-11-30 13:00:35 -0500584 if (!IS_PRIVATE(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 inode_doinit(inode);
586 iput(inode);
587 }
588 spin_lock(&sbsec->isec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 goto next_inode;
590 }
591 spin_unlock(&sbsec->isec_lock);
592out:
Eric Parisc9180a52007-11-30 13:00:35 -0500593 return rc;
594}
595
596/*
597 * This function should allow an FS to ask what it's mount security
598 * options were so it can use those later for submounts, displaying
599 * mount options, or whatever.
600 */
601static int selinux_get_mnt_opts(const struct super_block *sb,
Eric Parise0007522008-03-05 10:31:54 -0500602 struct security_mnt_opts *opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500603{
604 int rc = 0, i;
605 struct superblock_security_struct *sbsec = sb->s_security;
606 char *context = NULL;
607 u32 len;
608 char tmp;
609
Eric Parise0007522008-03-05 10:31:54 -0500610 security_init_mnt_opts(opts);
Eric Parisc9180a52007-11-30 13:00:35 -0500611
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500612 if (!(sbsec->flags & SE_SBINITIALIZED))
Eric Parisc9180a52007-11-30 13:00:35 -0500613 return -EINVAL;
614
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500615 if (!selinux_state.initialized)
Eric Parisc9180a52007-11-30 13:00:35 -0500616 return -EINVAL;
617
Eric Parisaf8e50c2012-08-24 15:59:00 -0400618 /* make sure we always check enough bits to cover the mask */
619 BUILD_BUG_ON(SE_MNTMASK >= (1 << NUM_SEL_MNT_OPTS));
620
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500621 tmp = sbsec->flags & SE_MNTMASK;
Eric Parisc9180a52007-11-30 13:00:35 -0500622 /* count the number of mount options for this sb */
Eric Parisaf8e50c2012-08-24 15:59:00 -0400623 for (i = 0; i < NUM_SEL_MNT_OPTS; i++) {
Eric Parisc9180a52007-11-30 13:00:35 -0500624 if (tmp & 0x01)
Eric Parise0007522008-03-05 10:31:54 -0500625 opts->num_mnt_opts++;
Eric Parisc9180a52007-11-30 13:00:35 -0500626 tmp >>= 1;
627 }
David P. Quigley11689d42009-01-16 09:22:03 -0500628 /* Check if the Label support flag is set */
Eric Paris0b4bdb32013-08-28 13:32:42 -0400629 if (sbsec->flags & SBLABEL_MNT)
David P. Quigley11689d42009-01-16 09:22:03 -0500630 opts->num_mnt_opts++;
Eric Parisc9180a52007-11-30 13:00:35 -0500631
Eric Parise0007522008-03-05 10:31:54 -0500632 opts->mnt_opts = kcalloc(opts->num_mnt_opts, sizeof(char *), GFP_ATOMIC);
633 if (!opts->mnt_opts) {
Eric Parisc9180a52007-11-30 13:00:35 -0500634 rc = -ENOMEM;
635 goto out_free;
636 }
637
Eric Parise0007522008-03-05 10:31:54 -0500638 opts->mnt_opts_flags = kcalloc(opts->num_mnt_opts, sizeof(int), GFP_ATOMIC);
639 if (!opts->mnt_opts_flags) {
Eric Parisc9180a52007-11-30 13:00:35 -0500640 rc = -ENOMEM;
641 goto out_free;
642 }
643
644 i = 0;
645 if (sbsec->flags & FSCONTEXT_MNT) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500646 rc = security_sid_to_context(&selinux_state, sbsec->sid,
647 &context, &len);
Eric Parisc9180a52007-11-30 13:00:35 -0500648 if (rc)
649 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500650 opts->mnt_opts[i] = context;
651 opts->mnt_opts_flags[i++] = FSCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500652 }
653 if (sbsec->flags & CONTEXT_MNT) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500654 rc = security_sid_to_context(&selinux_state,
655 sbsec->mntpoint_sid,
656 &context, &len);
Eric Parisc9180a52007-11-30 13:00:35 -0500657 if (rc)
658 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500659 opts->mnt_opts[i] = context;
660 opts->mnt_opts_flags[i++] = CONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500661 }
662 if (sbsec->flags & DEFCONTEXT_MNT) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500663 rc = security_sid_to_context(&selinux_state, sbsec->def_sid,
664 &context, &len);
Eric Parisc9180a52007-11-30 13:00:35 -0500665 if (rc)
666 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500667 opts->mnt_opts[i] = context;
668 opts->mnt_opts_flags[i++] = DEFCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500669 }
670 if (sbsec->flags & ROOTCONTEXT_MNT) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500671 struct dentry *root = sbsec->sb->s_root;
672 struct inode_security_struct *isec = backing_inode_security(root);
Eric Parisc9180a52007-11-30 13:00:35 -0500673
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500674 rc = security_sid_to_context(&selinux_state, isec->sid,
675 &context, &len);
Eric Parisc9180a52007-11-30 13:00:35 -0500676 if (rc)
677 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500678 opts->mnt_opts[i] = context;
679 opts->mnt_opts_flags[i++] = ROOTCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500680 }
Eric Paris12f348b2012-10-09 10:56:25 -0400681 if (sbsec->flags & SBLABEL_MNT) {
David P. Quigley11689d42009-01-16 09:22:03 -0500682 opts->mnt_opts[i] = NULL;
Eric Paris12f348b2012-10-09 10:56:25 -0400683 opts->mnt_opts_flags[i++] = SBLABEL_MNT;
David P. Quigley11689d42009-01-16 09:22:03 -0500684 }
Eric Parisc9180a52007-11-30 13:00:35 -0500685
Eric Parise0007522008-03-05 10:31:54 -0500686 BUG_ON(i != opts->num_mnt_opts);
Eric Parisc9180a52007-11-30 13:00:35 -0500687
688 return 0;
689
690out_free:
Eric Parise0007522008-03-05 10:31:54 -0500691 security_free_mnt_opts(opts);
Eric Parisc9180a52007-11-30 13:00:35 -0500692 return rc;
693}
694
695static int bad_option(struct superblock_security_struct *sbsec, char flag,
696 u32 old_sid, u32 new_sid)
697{
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500698 char mnt_flags = sbsec->flags & SE_MNTMASK;
699
Eric Parisc9180a52007-11-30 13:00:35 -0500700 /* check if the old mount command had the same options */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500701 if (sbsec->flags & SE_SBINITIALIZED)
Eric Parisc9180a52007-11-30 13:00:35 -0500702 if (!(sbsec->flags & flag) ||
703 (old_sid != new_sid))
704 return 1;
705
706 /* check if we were passed the same options twice,
707 * aka someone passed context=a,context=b
708 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500709 if (!(sbsec->flags & SE_SBINITIALIZED))
710 if (mnt_flags & flag)
Eric Parisc9180a52007-11-30 13:00:35 -0500711 return 1;
712 return 0;
713}
Eric Parise0007522008-03-05 10:31:54 -0500714
Eric Parisc9180a52007-11-30 13:00:35 -0500715/*
716 * Allow filesystems with binary mount data to explicitly set mount point
717 * labeling information.
718 */
Eric Parise0007522008-03-05 10:31:54 -0500719static int selinux_set_mnt_opts(struct super_block *sb,
David Quigley649f6e72013-05-22 12:50:36 -0400720 struct security_mnt_opts *opts,
721 unsigned long kern_flags,
722 unsigned long *set_kern_flags)
Eric Parisc9180a52007-11-30 13:00:35 -0500723{
David Howells275bb412008-11-14 10:39:19 +1100724 const struct cred *cred = current_cred();
Eric Parisc9180a52007-11-30 13:00:35 -0500725 int rc = 0, i;
Eric Parisc9180a52007-11-30 13:00:35 -0500726 struct superblock_security_struct *sbsec = sb->s_security;
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800727 const char *name = sb->s_type->name;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500728 struct dentry *root = sbsec->sb->s_root;
Paul Moore2c971652016-04-19 16:36:28 -0400729 struct inode_security_struct *root_isec;
Eric Parisc9180a52007-11-30 13:00:35 -0500730 u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
731 u32 defcontext_sid = 0;
Eric Parise0007522008-03-05 10:31:54 -0500732 char **mount_options = opts->mnt_opts;
733 int *flags = opts->mnt_opts_flags;
734 int num_opts = opts->num_mnt_opts;
Eric Parisc9180a52007-11-30 13:00:35 -0500735
736 mutex_lock(&sbsec->lock);
737
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500738 if (!selinux_state.initialized) {
Eric Parisc9180a52007-11-30 13:00:35 -0500739 if (!num_opts) {
740 /* Defer initialization until selinux_complete_init,
741 after the initial policy is loaded and the security
742 server is ready to handle calls. */
Eric Parisc9180a52007-11-30 13:00:35 -0500743 goto out;
744 }
745 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +0200746 pr_warn("SELinux: Unable to set superblock options "
Eric Paris744ba352008-04-17 11:52:44 -0400747 "before the security server is initialized\n");
Eric Parisc9180a52007-11-30 13:00:35 -0500748 goto out;
749 }
David Quigley649f6e72013-05-22 12:50:36 -0400750 if (kern_flags && !set_kern_flags) {
751 /* Specifying internal flags without providing a place to
752 * place the results is not allowed */
753 rc = -EINVAL;
754 goto out;
755 }
Eric Parisc9180a52007-11-30 13:00:35 -0500756
757 /*
Eric Parise0007522008-03-05 10:31:54 -0500758 * Binary mount data FS will come through this function twice. Once
759 * from an explicit call and once from the generic calls from the vfs.
760 * Since the generic VFS calls will not contain any security mount data
761 * we need to skip the double mount verification.
762 *
763 * This does open a hole in which we will not notice if the first
764 * mount using this sb set explict options and a second mount using
765 * this sb does not set any security options. (The first options
766 * will be used for both mounts)
767 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500768 if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
Eric Parise0007522008-03-05 10:31:54 -0500769 && (num_opts == 0))
Eric Parisf5269712008-05-14 11:27:45 -0400770 goto out;
Eric Parise0007522008-03-05 10:31:54 -0500771
Paul Moore2c971652016-04-19 16:36:28 -0400772 root_isec = backing_inode_security_novalidate(root);
773
Eric Parise0007522008-03-05 10:31:54 -0500774 /*
Eric Parisc9180a52007-11-30 13:00:35 -0500775 * parse the mount options, check if they are valid sids.
776 * also check if someone is trying to mount the same sb more
777 * than once with different security options.
778 */
779 for (i = 0; i < num_opts; i++) {
780 u32 sid;
David P. Quigley11689d42009-01-16 09:22:03 -0500781
Eric Paris12f348b2012-10-09 10:56:25 -0400782 if (flags[i] == SBLABEL_MNT)
David P. Quigley11689d42009-01-16 09:22:03 -0500783 continue;
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500784 rc = security_context_str_to_sid(&selinux_state,
785 mount_options[i], &sid,
786 GFP_KERNEL);
Eric Parisc9180a52007-11-30 13:00:35 -0500787 if (rc) {
peter enderborgc103a912018-06-12 10:09:03 +0200788 pr_warn("SELinux: security_context_str_to_sid"
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800789 "(%s) failed for (dev %s, type %s) errno=%d\n",
790 mount_options[i], sb->s_id, name, rc);
Eric Parisc9180a52007-11-30 13:00:35 -0500791 goto out;
792 }
793 switch (flags[i]) {
794 case FSCONTEXT_MNT:
795 fscontext_sid = sid;
796
797 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
798 fscontext_sid))
799 goto out_double_mount;
800
801 sbsec->flags |= FSCONTEXT_MNT;
802 break;
803 case CONTEXT_MNT:
804 context_sid = sid;
805
806 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
807 context_sid))
808 goto out_double_mount;
809
810 sbsec->flags |= CONTEXT_MNT;
811 break;
812 case ROOTCONTEXT_MNT:
813 rootcontext_sid = sid;
814
815 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
816 rootcontext_sid))
817 goto out_double_mount;
818
819 sbsec->flags |= ROOTCONTEXT_MNT;
820
821 break;
822 case DEFCONTEXT_MNT:
823 defcontext_sid = sid;
824
825 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
826 defcontext_sid))
827 goto out_double_mount;
828
829 sbsec->flags |= DEFCONTEXT_MNT;
830
831 break;
832 default:
833 rc = -EINVAL;
834 goto out;
835 }
836 }
837
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500838 if (sbsec->flags & SE_SBINITIALIZED) {
Eric Parisc9180a52007-11-30 13:00:35 -0500839 /* previously mounted with options, but not on this attempt? */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500840 if ((sbsec->flags & SE_MNTMASK) && !num_opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500841 goto out_double_mount;
842 rc = 0;
843 goto out;
844 }
845
James Morris089be432008-07-15 18:32:49 +1000846 if (strcmp(sb->s_type->name, "proc") == 0)
Stephen Smalley134509d2015-06-04 16:22:17 -0400847 sbsec->flags |= SE_SBPROC | SE_SBGENFS;
848
Stephen Smalley8e014722015-06-04 16:22:17 -0400849 if (!strcmp(sb->s_type->name, "debugfs") ||
Jeff Vander Stoep6a391182017-06-20 09:35:33 -0700850 !strcmp(sb->s_type->name, "tracefs") ||
Stephen Smalley8e014722015-06-04 16:22:17 -0400851 !strcmp(sb->s_type->name, "sysfs") ||
Antonio Murdaca901ef842017-02-09 17:02:42 +0100852 !strcmp(sb->s_type->name, "pstore") ||
853 !strcmp(sb->s_type->name, "cgroup") ||
854 !strcmp(sb->s_type->name, "cgroup2"))
Stephen Smalley134509d2015-06-04 16:22:17 -0400855 sbsec->flags |= SE_SBGENFS;
Eric Parisc9180a52007-11-30 13:00:35 -0500856
David Quigleyeb9ae682013-05-22 12:50:37 -0400857 if (!sbsec->behavior) {
858 /*
859 * Determine the labeling behavior to use for this
860 * filesystem type.
861 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500862 rc = security_fs_use(&selinux_state, sb);
David Quigleyeb9ae682013-05-22 12:50:37 -0400863 if (rc) {
peter enderborgc103a912018-06-12 10:09:03 +0200864 pr_warn("%s: security_fs_use(%s) returned %d\n",
David Quigleyeb9ae682013-05-22 12:50:37 -0400865 __func__, sb->s_type->name, rc);
866 goto out;
867 }
Eric Parisc9180a52007-11-30 13:00:35 -0500868 }
Seth Forsheeaad82892016-04-26 14:36:20 -0500869
870 /*
Stephen Smalley01593d32017-01-09 10:07:31 -0500871 * If this is a user namespace mount and the filesystem type is not
872 * explicitly whitelisted, then no contexts are allowed on the command
873 * line and security labels must be ignored.
Seth Forsheeaad82892016-04-26 14:36:20 -0500874 */
Stephen Smalley01593d32017-01-09 10:07:31 -0500875 if (sb->s_user_ns != &init_user_ns &&
876 strcmp(sb->s_type->name, "tmpfs") &&
877 strcmp(sb->s_type->name, "ramfs") &&
878 strcmp(sb->s_type->name, "devpts")) {
Seth Forsheeaad82892016-04-26 14:36:20 -0500879 if (context_sid || fscontext_sid || rootcontext_sid ||
880 defcontext_sid) {
881 rc = -EACCES;
882 goto out;
883 }
884 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
885 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500886 rc = security_transition_sid(&selinux_state,
887 current_sid(),
888 current_sid(),
Seth Forsheeaad82892016-04-26 14:36:20 -0500889 SECCLASS_FILE, NULL,
890 &sbsec->mntpoint_sid);
891 if (rc)
892 goto out;
893 }
894 goto out_set_opts;
895 }
896
Eric Parisc9180a52007-11-30 13:00:35 -0500897 /* sets the context of the superblock for the fs being mounted. */
898 if (fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100899 rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500900 if (rc)
901 goto out;
902
903 sbsec->sid = fscontext_sid;
904 }
905
906 /*
907 * Switch to using mount point labeling behavior.
908 * sets the label used on all file below the mountpoint, and will set
909 * the superblock context if not already set.
910 */
David Quigleyeb9ae682013-05-22 12:50:37 -0400911 if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !context_sid) {
912 sbsec->behavior = SECURITY_FS_USE_NATIVE;
913 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
914 }
915
Eric Parisc9180a52007-11-30 13:00:35 -0500916 if (context_sid) {
917 if (!fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100918 rc = may_context_mount_sb_relabel(context_sid, sbsec,
919 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500920 if (rc)
921 goto out;
922 sbsec->sid = context_sid;
923 } else {
David Howells275bb412008-11-14 10:39:19 +1100924 rc = may_context_mount_inode_relabel(context_sid, sbsec,
925 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500926 if (rc)
927 goto out;
928 }
929 if (!rootcontext_sid)
930 rootcontext_sid = context_sid;
931
932 sbsec->mntpoint_sid = context_sid;
933 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
934 }
935
936 if (rootcontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100937 rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec,
938 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500939 if (rc)
940 goto out;
941
942 root_isec->sid = rootcontext_sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -0500943 root_isec->initialized = LABEL_INITIALIZED;
Eric Parisc9180a52007-11-30 13:00:35 -0500944 }
945
946 if (defcontext_sid) {
David Quigleyeb9ae682013-05-22 12:50:37 -0400947 if (sbsec->behavior != SECURITY_FS_USE_XATTR &&
948 sbsec->behavior != SECURITY_FS_USE_NATIVE) {
Eric Parisc9180a52007-11-30 13:00:35 -0500949 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +0200950 pr_warn("SELinux: defcontext option is "
Eric Parisc9180a52007-11-30 13:00:35 -0500951 "invalid for this filesystem type\n");
952 goto out;
953 }
954
955 if (defcontext_sid != sbsec->def_sid) {
956 rc = may_context_mount_inode_relabel(defcontext_sid,
David Howells275bb412008-11-14 10:39:19 +1100957 sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500958 if (rc)
959 goto out;
960 }
961
962 sbsec->def_sid = defcontext_sid;
963 }
964
Seth Forsheeaad82892016-04-26 14:36:20 -0500965out_set_opts:
Eric Parisc9180a52007-11-30 13:00:35 -0500966 rc = sb_finish_set_opts(sb);
967out:
Eric Parisbc7e9822006-09-25 23:32:02 -0700968 mutex_unlock(&sbsec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 return rc;
Eric Parisc9180a52007-11-30 13:00:35 -0500970out_double_mount:
971 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +0200972 pr_warn("SELinux: mount invalid. Same superblock, different "
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800973 "security settings for (dev %s, type %s)\n", sb->s_id, name);
Eric Parisc9180a52007-11-30 13:00:35 -0500974 goto out;
975}
976
Jeff Layton094f7b62013-04-01 08:14:24 -0400977static int selinux_cmp_sb_context(const struct super_block *oldsb,
978 const struct super_block *newsb)
979{
980 struct superblock_security_struct *old = oldsb->s_security;
981 struct superblock_security_struct *new = newsb->s_security;
982 char oldflags = old->flags & SE_MNTMASK;
983 char newflags = new->flags & SE_MNTMASK;
984
985 if (oldflags != newflags)
986 goto mismatch;
987 if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid)
988 goto mismatch;
989 if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid)
990 goto mismatch;
991 if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid)
992 goto mismatch;
993 if (oldflags & ROOTCONTEXT_MNT) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500994 struct inode_security_struct *oldroot = backing_inode_security(oldsb->s_root);
995 struct inode_security_struct *newroot = backing_inode_security(newsb->s_root);
Jeff Layton094f7b62013-04-01 08:14:24 -0400996 if (oldroot->sid != newroot->sid)
997 goto mismatch;
998 }
999 return 0;
1000mismatch:
peter enderborgc103a912018-06-12 10:09:03 +02001001 pr_warn("SELinux: mount invalid. Same superblock, "
Jeff Layton094f7b62013-04-01 08:14:24 -04001002 "different security settings for (dev %s, "
1003 "type %s)\n", newsb->s_id, newsb->s_type->name);
1004 return -EBUSY;
1005}
1006
1007static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
Scott Mayhew0b4d3452017-06-05 11:45:04 -04001008 struct super_block *newsb,
1009 unsigned long kern_flags,
1010 unsigned long *set_kern_flags)
Eric Parisc9180a52007-11-30 13:00:35 -05001011{
Scott Mayhew0b4d3452017-06-05 11:45:04 -04001012 int rc = 0;
Eric Parisc9180a52007-11-30 13:00:35 -05001013 const struct superblock_security_struct *oldsbsec = oldsb->s_security;
1014 struct superblock_security_struct *newsbsec = newsb->s_security;
1015
1016 int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT);
1017 int set_context = (oldsbsec->flags & CONTEXT_MNT);
1018 int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT);
1019
Eric Paris0f5e6422008-04-21 16:24:11 -04001020 /*
1021 * if the parent was able to be mounted it clearly had no special lsm
Al Viroe8c26252010-03-23 06:36:54 -04001022 * mount options. thus we can safely deal with this superblock later
Eric Paris0f5e6422008-04-21 16:24:11 -04001023 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001024 if (!selinux_state.initialized)
Jeff Layton094f7b62013-04-01 08:14:24 -04001025 return 0;
Eric Parisc9180a52007-11-30 13:00:35 -05001026
Scott Mayhew0b4d3452017-06-05 11:45:04 -04001027 /*
1028 * Specifying internal flags without providing a place to
1029 * place the results is not allowed.
1030 */
1031 if (kern_flags && !set_kern_flags)
1032 return -EINVAL;
1033
Eric Parisc9180a52007-11-30 13:00:35 -05001034 /* how can we clone if the old one wasn't set up?? */
David P. Quigley0d90a7e2009-01-16 09:22:02 -05001035 BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED));
Eric Parisc9180a52007-11-30 13:00:35 -05001036
Jeff Layton094f7b62013-04-01 08:14:24 -04001037 /* if fs is reusing a sb, make sure that the contexts match */
David P. Quigley0d90a7e2009-01-16 09:22:02 -05001038 if (newsbsec->flags & SE_SBINITIALIZED)
Jeff Layton094f7b62013-04-01 08:14:24 -04001039 return selinux_cmp_sb_context(oldsb, newsb);
Eric Paris5a552612008-04-09 14:08:35 -04001040
Eric Parisc9180a52007-11-30 13:00:35 -05001041 mutex_lock(&newsbsec->lock);
1042
1043 newsbsec->flags = oldsbsec->flags;
1044
1045 newsbsec->sid = oldsbsec->sid;
1046 newsbsec->def_sid = oldsbsec->def_sid;
1047 newsbsec->behavior = oldsbsec->behavior;
1048
Scott Mayhew0b4d3452017-06-05 11:45:04 -04001049 if (newsbsec->behavior == SECURITY_FS_USE_NATIVE &&
1050 !(kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001051 rc = security_fs_use(&selinux_state, newsb);
Scott Mayhew0b4d3452017-06-05 11:45:04 -04001052 if (rc)
1053 goto out;
1054 }
1055
1056 if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !set_context) {
1057 newsbsec->behavior = SECURITY_FS_USE_NATIVE;
1058 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
1059 }
1060
Eric Parisc9180a52007-11-30 13:00:35 -05001061 if (set_context) {
1062 u32 sid = oldsbsec->mntpoint_sid;
1063
1064 if (!set_fscontext)
1065 newsbsec->sid = sid;
1066 if (!set_rootcontext) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001067 struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
Eric Parisc9180a52007-11-30 13:00:35 -05001068 newisec->sid = sid;
1069 }
1070 newsbsec->mntpoint_sid = sid;
1071 }
1072 if (set_rootcontext) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001073 const struct inode_security_struct *oldisec = backing_inode_security(oldsb->s_root);
1074 struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
Eric Parisc9180a52007-11-30 13:00:35 -05001075
1076 newisec->sid = oldisec->sid;
1077 }
1078
1079 sb_finish_set_opts(newsb);
Scott Mayhew0b4d3452017-06-05 11:45:04 -04001080out:
Eric Parisc9180a52007-11-30 13:00:35 -05001081 mutex_unlock(&newsbsec->lock);
Scott Mayhew0b4d3452017-06-05 11:45:04 -04001082 return rc;
Eric Parisc9180a52007-11-30 13:00:35 -05001083}
1084
Adrian Bunk2e1479d2008-03-17 22:29:23 +02001085static int selinux_parse_opts_str(char *options,
1086 struct security_mnt_opts *opts)
Eric Parisc9180a52007-11-30 13:00:35 -05001087{
Eric Parise0007522008-03-05 10:31:54 -05001088 char *p;
Eric Parisc9180a52007-11-30 13:00:35 -05001089 char *context = NULL, *defcontext = NULL;
1090 char *fscontext = NULL, *rootcontext = NULL;
Eric Parise0007522008-03-05 10:31:54 -05001091 int rc, num_mnt_opts = 0;
Eric Parisc9180a52007-11-30 13:00:35 -05001092
Eric Parise0007522008-03-05 10:31:54 -05001093 opts->num_mnt_opts = 0;
Eric Parisc9180a52007-11-30 13:00:35 -05001094
1095 /* Standard string-based options. */
1096 while ((p = strsep(&options, "|")) != NULL) {
1097 int token;
1098 substring_t args[MAX_OPT_ARGS];
1099
1100 if (!*p)
1101 continue;
1102
1103 token = match_token(p, tokens, args);
1104
1105 switch (token) {
1106 case Opt_context:
1107 if (context || defcontext) {
1108 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +02001109 pr_warn(SEL_MOUNT_FAIL_MSG);
Eric Parisc9180a52007-11-30 13:00:35 -05001110 goto out_err;
1111 }
1112 context = match_strdup(&args[0]);
1113 if (!context) {
1114 rc = -ENOMEM;
1115 goto out_err;
1116 }
1117 break;
1118
1119 case Opt_fscontext:
1120 if (fscontext) {
1121 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +02001122 pr_warn(SEL_MOUNT_FAIL_MSG);
Eric Parisc9180a52007-11-30 13:00:35 -05001123 goto out_err;
1124 }
1125 fscontext = match_strdup(&args[0]);
1126 if (!fscontext) {
1127 rc = -ENOMEM;
1128 goto out_err;
1129 }
1130 break;
1131
1132 case Opt_rootcontext:
1133 if (rootcontext) {
1134 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +02001135 pr_warn(SEL_MOUNT_FAIL_MSG);
Eric Parisc9180a52007-11-30 13:00:35 -05001136 goto out_err;
1137 }
1138 rootcontext = match_strdup(&args[0]);
1139 if (!rootcontext) {
1140 rc = -ENOMEM;
1141 goto out_err;
1142 }
1143 break;
1144
1145 case Opt_defcontext:
1146 if (context || defcontext) {
1147 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +02001148 pr_warn(SEL_MOUNT_FAIL_MSG);
Eric Parisc9180a52007-11-30 13:00:35 -05001149 goto out_err;
1150 }
1151 defcontext = match_strdup(&args[0]);
1152 if (!defcontext) {
1153 rc = -ENOMEM;
1154 goto out_err;
1155 }
1156 break;
David P. Quigley11689d42009-01-16 09:22:03 -05001157 case Opt_labelsupport:
1158 break;
Eric Parisc9180a52007-11-30 13:00:35 -05001159 default:
1160 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +02001161 pr_warn("SELinux: unknown mount option\n");
Eric Parisc9180a52007-11-30 13:00:35 -05001162 goto out_err;
1163
1164 }
1165 }
1166
Eric Parise0007522008-03-05 10:31:54 -05001167 rc = -ENOMEM;
Tetsuo Handa8931c3b2016-11-14 20:16:12 +09001168 opts->mnt_opts = kcalloc(NUM_SEL_MNT_OPTS, sizeof(char *), GFP_KERNEL);
Eric Parise0007522008-03-05 10:31:54 -05001169 if (!opts->mnt_opts)
1170 goto out_err;
1171
Tetsuo Handa8931c3b2016-11-14 20:16:12 +09001172 opts->mnt_opts_flags = kcalloc(NUM_SEL_MNT_OPTS, sizeof(int),
1173 GFP_KERNEL);
Paul Moore023f1082017-06-07 16:48:19 -04001174 if (!opts->mnt_opts_flags)
Eric Parise0007522008-03-05 10:31:54 -05001175 goto out_err;
Eric Parisc9180a52007-11-30 13:00:35 -05001176
Eric Parise0007522008-03-05 10:31:54 -05001177 if (fscontext) {
1178 opts->mnt_opts[num_mnt_opts] = fscontext;
1179 opts->mnt_opts_flags[num_mnt_opts++] = FSCONTEXT_MNT;
1180 }
1181 if (context) {
1182 opts->mnt_opts[num_mnt_opts] = context;
1183 opts->mnt_opts_flags[num_mnt_opts++] = CONTEXT_MNT;
1184 }
1185 if (rootcontext) {
1186 opts->mnt_opts[num_mnt_opts] = rootcontext;
1187 opts->mnt_opts_flags[num_mnt_opts++] = ROOTCONTEXT_MNT;
1188 }
1189 if (defcontext) {
1190 opts->mnt_opts[num_mnt_opts] = defcontext;
1191 opts->mnt_opts_flags[num_mnt_opts++] = DEFCONTEXT_MNT;
1192 }
1193
1194 opts->num_mnt_opts = num_mnt_opts;
1195 return 0;
1196
Eric Parisc9180a52007-11-30 13:00:35 -05001197out_err:
Paul Moore023f1082017-06-07 16:48:19 -04001198 security_free_mnt_opts(opts);
Eric Parisc9180a52007-11-30 13:00:35 -05001199 kfree(context);
1200 kfree(defcontext);
1201 kfree(fscontext);
1202 kfree(rootcontext);
1203 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204}
1205
Adrian Bunk3583a712008-07-22 20:21:23 +03001206static void selinux_write_opts(struct seq_file *m,
1207 struct security_mnt_opts *opts)
Eric Paris2069f452008-07-04 09:47:13 +10001208{
1209 int i;
1210 char *prefix;
1211
1212 for (i = 0; i < opts->num_mnt_opts; i++) {
David P. Quigley11689d42009-01-16 09:22:03 -05001213 char *has_comma;
1214
1215 if (opts->mnt_opts[i])
1216 has_comma = strchr(opts->mnt_opts[i], ',');
1217 else
1218 has_comma = NULL;
Eric Paris2069f452008-07-04 09:47:13 +10001219
1220 switch (opts->mnt_opts_flags[i]) {
1221 case CONTEXT_MNT:
1222 prefix = CONTEXT_STR;
1223 break;
1224 case FSCONTEXT_MNT:
1225 prefix = FSCONTEXT_STR;
1226 break;
1227 case ROOTCONTEXT_MNT:
1228 prefix = ROOTCONTEXT_STR;
1229 break;
1230 case DEFCONTEXT_MNT:
1231 prefix = DEFCONTEXT_STR;
1232 break;
Eric Paris12f348b2012-10-09 10:56:25 -04001233 case SBLABEL_MNT:
David P. Quigley11689d42009-01-16 09:22:03 -05001234 seq_putc(m, ',');
1235 seq_puts(m, LABELSUPP_STR);
1236 continue;
Eric Paris2069f452008-07-04 09:47:13 +10001237 default:
1238 BUG();
Eric Parisa35c6c832011-04-20 10:21:28 -04001239 return;
Eric Paris2069f452008-07-04 09:47:13 +10001240 };
1241 /* we need a comma before each option */
1242 seq_putc(m, ',');
1243 seq_puts(m, prefix);
1244 if (has_comma)
1245 seq_putc(m, '\"');
Kees Cooka068acf2015-09-04 15:44:57 -07001246 seq_escape(m, opts->mnt_opts[i], "\"\n\\");
Eric Paris2069f452008-07-04 09:47:13 +10001247 if (has_comma)
1248 seq_putc(m, '\"');
1249 }
1250}
1251
1252static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
1253{
1254 struct security_mnt_opts opts;
1255 int rc;
1256
1257 rc = selinux_get_mnt_opts(sb, &opts);
Eric Paris383795c2008-07-29 17:07:26 -04001258 if (rc) {
1259 /* before policy load we may get EINVAL, don't show anything */
1260 if (rc == -EINVAL)
1261 rc = 0;
Eric Paris2069f452008-07-04 09:47:13 +10001262 return rc;
Eric Paris383795c2008-07-29 17:07:26 -04001263 }
Eric Paris2069f452008-07-04 09:47:13 +10001264
1265 selinux_write_opts(m, &opts);
1266
1267 security_free_mnt_opts(&opts);
1268
1269 return rc;
1270}
1271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272static inline u16 inode_mode_to_security_class(umode_t mode)
1273{
1274 switch (mode & S_IFMT) {
1275 case S_IFSOCK:
1276 return SECCLASS_SOCK_FILE;
1277 case S_IFLNK:
1278 return SECCLASS_LNK_FILE;
1279 case S_IFREG:
1280 return SECCLASS_FILE;
1281 case S_IFBLK:
1282 return SECCLASS_BLK_FILE;
1283 case S_IFDIR:
1284 return SECCLASS_DIR;
1285 case S_IFCHR:
1286 return SECCLASS_CHR_FILE;
1287 case S_IFIFO:
1288 return SECCLASS_FIFO_FILE;
1289
1290 }
1291
1292 return SECCLASS_FILE;
1293}
1294
James Morris13402582005-09-30 14:24:34 -04001295static inline int default_protocol_stream(int protocol)
1296{
1297 return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP);
1298}
1299
1300static inline int default_protocol_dgram(int protocol)
1301{
1302 return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
1303}
1304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305static inline u16 socket_type_to_security_class(int family, int type, int protocol)
1306{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001307 int extsockclass = selinux_policycap_extsockclass();
Stephen Smalleyda69a532017-01-09 10:07:30 -05001308
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 switch (family) {
1310 case PF_UNIX:
1311 switch (type) {
1312 case SOCK_STREAM:
1313 case SOCK_SEQPACKET:
1314 return SECCLASS_UNIX_STREAM_SOCKET;
1315 case SOCK_DGRAM:
Luis Ressel2a764b52017-07-25 15:13:41 -04001316 case SOCK_RAW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 return SECCLASS_UNIX_DGRAM_SOCKET;
1318 }
1319 break;
1320 case PF_INET:
1321 case PF_INET6:
1322 switch (type) {
1323 case SOCK_STREAM:
Stephen Smalleyda69a532017-01-09 10:07:30 -05001324 case SOCK_SEQPACKET:
James Morris13402582005-09-30 14:24:34 -04001325 if (default_protocol_stream(protocol))
1326 return SECCLASS_TCP_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001327 else if (extsockclass && protocol == IPPROTO_SCTP)
1328 return SECCLASS_SCTP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001329 else
1330 return SECCLASS_RAWIP_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 case SOCK_DGRAM:
James Morris13402582005-09-30 14:24:34 -04001332 if (default_protocol_dgram(protocol))
1333 return SECCLASS_UDP_SOCKET;
Stephen Smalleyef379792017-01-09 10:07:31 -05001334 else if (extsockclass && (protocol == IPPROTO_ICMP ||
1335 protocol == IPPROTO_ICMPV6))
Stephen Smalleyda69a532017-01-09 10:07:30 -05001336 return SECCLASS_ICMP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001337 else
1338 return SECCLASS_RAWIP_SOCKET;
James Morris2ee92d42006-11-13 16:09:01 -08001339 case SOCK_DCCP:
1340 return SECCLASS_DCCP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001341 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 return SECCLASS_RAWIP_SOCKET;
1343 }
1344 break;
1345 case PF_NETLINK:
1346 switch (protocol) {
1347 case NETLINK_ROUTE:
1348 return SECCLASS_NETLINK_ROUTE_SOCKET;
Pavel Emelyanov7f1fb602011-12-06 07:56:43 +00001349 case NETLINK_SOCK_DIAG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 return SECCLASS_NETLINK_TCPDIAG_SOCKET;
1351 case NETLINK_NFLOG:
1352 return SECCLASS_NETLINK_NFLOG_SOCKET;
1353 case NETLINK_XFRM:
1354 return SECCLASS_NETLINK_XFRM_SOCKET;
1355 case NETLINK_SELINUX:
1356 return SECCLASS_NETLINK_SELINUX_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001357 case NETLINK_ISCSI:
1358 return SECCLASS_NETLINK_ISCSI_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 case NETLINK_AUDIT:
1360 return SECCLASS_NETLINK_AUDIT_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001361 case NETLINK_FIB_LOOKUP:
1362 return SECCLASS_NETLINK_FIB_LOOKUP_SOCKET;
1363 case NETLINK_CONNECTOR:
1364 return SECCLASS_NETLINK_CONNECTOR_SOCKET;
1365 case NETLINK_NETFILTER:
1366 return SECCLASS_NETLINK_NETFILTER_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 case NETLINK_DNRTMSG:
1368 return SECCLASS_NETLINK_DNRT_SOCKET;
James Morris0c9b7942005-04-16 15:24:13 -07001369 case NETLINK_KOBJECT_UEVENT:
1370 return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001371 case NETLINK_GENERIC:
1372 return SECCLASS_NETLINK_GENERIC_SOCKET;
1373 case NETLINK_SCSITRANSPORT:
1374 return SECCLASS_NETLINK_SCSITRANSPORT_SOCKET;
1375 case NETLINK_RDMA:
1376 return SECCLASS_NETLINK_RDMA_SOCKET;
1377 case NETLINK_CRYPTO:
1378 return SECCLASS_NETLINK_CRYPTO_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 default:
1380 return SECCLASS_NETLINK_SOCKET;
1381 }
1382 case PF_PACKET:
1383 return SECCLASS_PACKET_SOCKET;
1384 case PF_KEY:
1385 return SECCLASS_KEY_SOCKET;
Christopher J. PeBenito3e3ff152006-06-09 00:25:03 -07001386 case PF_APPLETALK:
1387 return SECCLASS_APPLETALK_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 }
1389
Stephen Smalleyda69a532017-01-09 10:07:30 -05001390 if (extsockclass) {
1391 switch (family) {
1392 case PF_AX25:
1393 return SECCLASS_AX25_SOCKET;
1394 case PF_IPX:
1395 return SECCLASS_IPX_SOCKET;
1396 case PF_NETROM:
1397 return SECCLASS_NETROM_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001398 case PF_ATMPVC:
1399 return SECCLASS_ATMPVC_SOCKET;
1400 case PF_X25:
1401 return SECCLASS_X25_SOCKET;
1402 case PF_ROSE:
1403 return SECCLASS_ROSE_SOCKET;
1404 case PF_DECnet:
1405 return SECCLASS_DECNET_SOCKET;
1406 case PF_ATMSVC:
1407 return SECCLASS_ATMSVC_SOCKET;
1408 case PF_RDS:
1409 return SECCLASS_RDS_SOCKET;
1410 case PF_IRDA:
1411 return SECCLASS_IRDA_SOCKET;
1412 case PF_PPPOX:
1413 return SECCLASS_PPPOX_SOCKET;
1414 case PF_LLC:
1415 return SECCLASS_LLC_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001416 case PF_CAN:
1417 return SECCLASS_CAN_SOCKET;
1418 case PF_TIPC:
1419 return SECCLASS_TIPC_SOCKET;
1420 case PF_BLUETOOTH:
1421 return SECCLASS_BLUETOOTH_SOCKET;
1422 case PF_IUCV:
1423 return SECCLASS_IUCV_SOCKET;
1424 case PF_RXRPC:
1425 return SECCLASS_RXRPC_SOCKET;
1426 case PF_ISDN:
1427 return SECCLASS_ISDN_SOCKET;
1428 case PF_PHONET:
1429 return SECCLASS_PHONET_SOCKET;
1430 case PF_IEEE802154:
1431 return SECCLASS_IEEE802154_SOCKET;
1432 case PF_CAIF:
1433 return SECCLASS_CAIF_SOCKET;
1434 case PF_ALG:
1435 return SECCLASS_ALG_SOCKET;
1436 case PF_NFC:
1437 return SECCLASS_NFC_SOCKET;
1438 case PF_VSOCK:
1439 return SECCLASS_VSOCK_SOCKET;
1440 case PF_KCM:
1441 return SECCLASS_KCM_SOCKET;
1442 case PF_QIPCRTR:
1443 return SECCLASS_QIPCRTR_SOCKET;
Linus Torvalds3051bf32017-02-22 10:15:09 -08001444 case PF_SMC:
1445 return SECCLASS_SMC_SOCKET;
Björn Töpel68e8b842018-05-02 13:01:22 +02001446 case PF_XDP:
1447 return SECCLASS_XDP_SOCKET;
1448#if PF_MAX > 45
Stephen Smalleyda69a532017-01-09 10:07:30 -05001449#error New address family defined, please update this function.
1450#endif
1451 }
1452 }
1453
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 return SECCLASS_SOCKET;
1455}
1456
Stephen Smalley134509d2015-06-04 16:22:17 -04001457static int selinux_genfs_get_sid(struct dentry *dentry,
1458 u16 tclass,
1459 u16 flags,
1460 u32 *sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461{
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001462 int rc;
Al Virofc640052016-04-10 01:33:30 -04001463 struct super_block *sb = dentry->d_sb;
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001464 char *buffer, *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
Eric Paris828dfe12008-04-17 13:17:49 -04001466 buffer = (char *)__get_free_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 if (!buffer)
1468 return -ENOMEM;
1469
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001470 path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
1471 if (IS_ERR(path))
1472 rc = PTR_ERR(path);
1473 else {
Stephen Smalley134509d2015-06-04 16:22:17 -04001474 if (flags & SE_SBPROC) {
1475 /* each process gets a /proc/PID/ entry. Strip off the
1476 * PID part to get a valid selinux labeling.
1477 * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
1478 while (path[1] >= '0' && path[1] <= '9') {
1479 path[1] = '/';
1480 path++;
1481 }
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001482 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001483 rc = security_genfs_sid(&selinux_state, sb->s_type->name,
1484 path, tclass, sid);
Stephen Smalley7bb185e2018-09-04 16:51:36 -04001485 if (rc == -ENOENT) {
1486 /* No match in policy, mark as unlabeled. */
1487 *sid = SECINITSID_UNLABELED;
1488 rc = 0;
1489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 free_page((unsigned long)buffer);
1492 return rc;
1493}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
1495/* The inode's security attributes must be initialized before first use. */
1496static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
1497{
1498 struct superblock_security_struct *sbsec = NULL;
1499 struct inode_security_struct *isec = inode->i_security;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001500 u32 task_sid, sid = 0;
1501 u16 sclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 struct dentry *dentry;
1503#define INITCONTEXTLEN 255
1504 char *context = NULL;
1505 unsigned len = 0;
1506 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05001508 if (isec->initialized == LABEL_INITIALIZED)
Andreas Gruenbacher13457d02016-11-10 22:18:29 +01001509 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001511 spin_lock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05001512 if (isec->initialized == LABEL_INITIALIZED)
Eric Paris23970742006-09-25 23:32:01 -07001513 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Andreas Gruenbacher13457d02016-11-10 22:18:29 +01001515 if (isec->sclass == SECCLASS_FILE)
1516 isec->sclass = inode_mode_to_security_class(inode->i_mode);
1517
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 sbsec = inode->i_sb->s_security;
David P. Quigley0d90a7e2009-01-16 09:22:02 -05001519 if (!(sbsec->flags & SE_SBINITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 /* Defer initialization until selinux_complete_init,
1521 after the initial policy is loaded and the security
1522 server is ready to handle calls. */
1523 spin_lock(&sbsec->isec_lock);
1524 if (list_empty(&isec->list))
1525 list_add(&isec->list, &sbsec->isec_head);
1526 spin_unlock(&sbsec->isec_lock);
Eric Paris23970742006-09-25 23:32:01 -07001527 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 }
1529
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001530 sclass = isec->sclass;
1531 task_sid = isec->task_sid;
1532 sid = isec->sid;
1533 isec->initialized = LABEL_PENDING;
1534 spin_unlock(&isec->lock);
1535
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 switch (sbsec->behavior) {
David Quigleyeb9ae682013-05-22 12:50:37 -04001537 case SECURITY_FS_USE_NATIVE:
1538 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 case SECURITY_FS_USE_XATTR:
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001540 if (!(inode->i_opflags & IOP_XATTR)) {
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001541 sid = sbsec->def_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 break;
1543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 /* Need a dentry, since the xattr API requires one.
1545 Life would be simpler if we could just pass the inode. */
1546 if (opt_dentry) {
1547 /* Called from d_instantiate or d_splice_alias. */
1548 dentry = dget(opt_dentry);
1549 } else {
Al Virob1271252018-04-25 10:28:38 -04001550 /*
1551 * Called from selinux_complete_init, try to find a dentry.
1552 * Some filesystems really want a connected one, so try
1553 * that first. We could split SECURITY_FS_USE_XATTR in
1554 * two, depending upon that...
1555 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 dentry = d_find_alias(inode);
Al Virob1271252018-04-25 10:28:38 -04001557 if (!dentry)
1558 dentry = d_find_any_alias(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 }
1560 if (!dentry) {
Eric Parisdf7f54c2009-03-09 14:35:58 -04001561 /*
1562 * this is can be hit on boot when a file is accessed
1563 * before the policy is loaded. When we load policy we
1564 * may find inodes that have no dentry on the
1565 * sbsec->isec_head list. No reason to complain as these
1566 * will get fixed up the next time we go through
1567 * inode_doinit with a dentry, before these inodes could
1568 * be used again by userspace.
1569 */
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001570 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 }
1572
1573 len = INITCONTEXTLEN;
Eric Paris4cb912f2009-02-12 14:50:05 -05001574 context = kmalloc(len+1, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 if (!context) {
1576 rc = -ENOMEM;
1577 dput(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001578 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 }
Eric Paris4cb912f2009-02-12 14:50:05 -05001580 context[len] = '\0';
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001581 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 if (rc == -ERANGE) {
James Morris314dabb2009-08-10 22:00:13 +10001583 kfree(context);
1584
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 /* Need a larger buffer. Query for the right size. */
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001586 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 if (rc < 0) {
1588 dput(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001589 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 len = rc;
Eric Paris4cb912f2009-02-12 14:50:05 -05001592 context = kmalloc(len+1, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 if (!context) {
1594 rc = -ENOMEM;
1595 dput(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001596 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 }
Eric Paris4cb912f2009-02-12 14:50:05 -05001598 context[len] = '\0';
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001599 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 }
1601 dput(dentry);
1602 if (rc < 0) {
1603 if (rc != -ENODATA) {
peter enderborgc103a912018-06-12 10:09:03 +02001604 pr_warn("SELinux: %s: getxattr returned "
Harvey Harrisondd6f9532008-03-06 10:03:59 +11001605 "%d for dev=%s ino=%ld\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 -rc, inode->i_sb->s_id, inode->i_ino);
1607 kfree(context);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001608 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 }
1610 /* Map ENODATA to the default file SID */
1611 sid = sbsec->def_sid;
1612 rc = 0;
1613 } else {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001614 rc = security_context_to_sid_default(&selinux_state,
1615 context, rc, &sid,
Stephen Smalley869ab512008-04-04 08:46:05 -04001616 sbsec->def_sid,
1617 GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 if (rc) {
Eric Paris4ba0a8a2009-02-12 15:01:10 -05001619 char *dev = inode->i_sb->s_id;
1620 unsigned long ino = inode->i_ino;
1621
1622 if (rc == -EINVAL) {
1623 if (printk_ratelimit())
peter enderborgc103a912018-06-12 10:09:03 +02001624 pr_notice("SELinux: inode=%lu on dev=%s was found to have an invalid "
Eric Paris4ba0a8a2009-02-12 15:01:10 -05001625 "context=%s. This indicates you may need to relabel the inode or the "
1626 "filesystem in question.\n", ino, dev, context);
1627 } else {
peter enderborgc103a912018-06-12 10:09:03 +02001628 pr_warn("SELinux: %s: context_to_sid(%s) "
Eric Paris4ba0a8a2009-02-12 15:01:10 -05001629 "returned %d for dev=%s ino=%ld\n",
1630 __func__, context, -rc, dev, ino);
1631 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 kfree(context);
1633 /* Leave with the unlabeled SID */
1634 rc = 0;
1635 break;
1636 }
1637 }
1638 kfree(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 break;
1640 case SECURITY_FS_USE_TASK:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001641 sid = task_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 break;
1643 case SECURITY_FS_USE_TRANS:
1644 /* Default to the fs SID. */
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001645 sid = sbsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
1647 /* Try to obtain a transition SID. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001648 rc = security_transition_sid(&selinux_state, task_sid, sid,
1649 sclass, NULL, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 if (rc)
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001651 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 break;
Eric Parisc312feb2006-07-10 04:43:53 -07001653 case SECURITY_FS_USE_MNTPOINT:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001654 sid = sbsec->mntpoint_sid;
Eric Parisc312feb2006-07-10 04:43:53 -07001655 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 default:
Eric Parisc312feb2006-07-10 04:43:53 -07001657 /* Default to the fs superblock SID. */
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001658 sid = sbsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
Stephen Smalley134509d2015-06-04 16:22:17 -04001660 if ((sbsec->flags & SE_SBGENFS) && !S_ISLNK(inode->i_mode)) {
Paul Mooref64410e2014-03-19 16:46:18 -04001661 /* We must have a dentry to determine the label on
1662 * procfs inodes */
Al Virob1271252018-04-25 10:28:38 -04001663 if (opt_dentry) {
Paul Mooref64410e2014-03-19 16:46:18 -04001664 /* Called from d_instantiate or
1665 * d_splice_alias. */
1666 dentry = dget(opt_dentry);
Al Virob1271252018-04-25 10:28:38 -04001667 } else {
Paul Mooref64410e2014-03-19 16:46:18 -04001668 /* Called from selinux_complete_init, try to
Al Virob1271252018-04-25 10:28:38 -04001669 * find a dentry. Some filesystems really want
1670 * a connected one, so try that first.
1671 */
Paul Mooref64410e2014-03-19 16:46:18 -04001672 dentry = d_find_alias(inode);
Al Virob1271252018-04-25 10:28:38 -04001673 if (!dentry)
1674 dentry = d_find_any_alias(inode);
1675 }
Paul Mooref64410e2014-03-19 16:46:18 -04001676 /*
1677 * This can be hit on boot when a file is accessed
1678 * before the policy is loaded. When we load policy we
1679 * may find inodes that have no dentry on the
1680 * sbsec->isec_head list. No reason to complain as
1681 * these will get fixed up the next time we go through
1682 * inode_doinit() with a dentry, before these inodes
1683 * could be used again by userspace.
1684 */
1685 if (!dentry)
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001686 goto out;
1687 rc = selinux_genfs_get_sid(dentry, sclass,
Stephen Smalley134509d2015-06-04 16:22:17 -04001688 sbsec->flags, &sid);
Paul Mooref64410e2014-03-19 16:46:18 -04001689 dput(dentry);
1690 if (rc)
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001691 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 }
1693 break;
1694 }
1695
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001696out:
1697 spin_lock(&isec->lock);
1698 if (isec->initialized == LABEL_PENDING) {
1699 if (!sid || rc) {
1700 isec->initialized = LABEL_INVALID;
1701 goto out_unlock;
1702 }
1703
1704 isec->initialized = LABEL_INITIALIZED;
1705 isec->sid = sid;
1706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
Eric Paris23970742006-09-25 23:32:01 -07001708out_unlock:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001709 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 return rc;
1711}
1712
1713/* Convert a Linux signal to an access vector. */
1714static inline u32 signal_to_av(int sig)
1715{
1716 u32 perm = 0;
1717
1718 switch (sig) {
1719 case SIGCHLD:
1720 /* Commonly granted from child to parent. */
1721 perm = PROCESS__SIGCHLD;
1722 break;
1723 case SIGKILL:
1724 /* Cannot be caught or ignored */
1725 perm = PROCESS__SIGKILL;
1726 break;
1727 case SIGSTOP:
1728 /* Cannot be caught or ignored */
1729 perm = PROCESS__SIGSTOP;
1730 break;
1731 default:
1732 /* All other signals. */
1733 perm = PROCESS__SIGNAL;
1734 break;
1735 }
1736
1737 return perm;
1738}
1739
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001740#if CAP_LAST_CAP > 63
1741#error Fix SELinux to handle capabilities > 63.
1742#endif
1743
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744/* Check whether a task is allowed to use a capability. */
Eric Paris6a9de492012-01-03 12:25:14 -05001745static int cred_has_capability(const struct cred *cred,
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04001746 int cap, int audit, bool initns)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747{
Thomas Liu2bf49692009-07-14 12:14:09 -04001748 struct common_audit_data ad;
Eric Paris06112162008-11-11 22:02:50 +11001749 struct av_decision avd;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001750 u16 sclass;
David Howells3699c532009-01-06 22:27:01 +00001751 u32 sid = cred_sid(cred);
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001752 u32 av = CAP_TO_MASK(cap);
Eric Paris06112162008-11-11 22:02:50 +11001753 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Eric Paris50c205f2012-04-04 15:01:43 -04001755 ad.type = LSM_AUDIT_DATA_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 ad.u.cap = cap;
1757
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001758 switch (CAP_TO_INDEX(cap)) {
1759 case 0:
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04001760 sclass = initns ? SECCLASS_CAPABILITY : SECCLASS_CAP_USERNS;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001761 break;
1762 case 1:
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04001763 sclass = initns ? SECCLASS_CAPABILITY2 : SECCLASS_CAP2_USERNS;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001764 break;
1765 default:
peter enderborgc103a912018-06-12 10:09:03 +02001766 pr_err("SELinux: out of range capability %d\n", cap);
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001767 BUG();
Eric Parisa35c6c832011-04-20 10:21:28 -04001768 return -EINVAL;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001769 }
Eric Paris06112162008-11-11 22:02:50 +11001770
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001771 rc = avc_has_perm_noaudit(&selinux_state,
1772 sid, sid, sclass, av, 0, &avd);
Eric Paris9ade0cf2011-04-25 16:26:29 -04001773 if (audit == SECURITY_CAP_AUDIT) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001774 int rc2 = avc_audit(&selinux_state,
1775 sid, sid, sclass, av, &avd, rc, &ad, 0);
Eric Paris9ade0cf2011-04-25 16:26:29 -04001776 if (rc2)
1777 return rc2;
1778 }
Eric Paris06112162008-11-11 22:02:50 +11001779 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780}
1781
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782/* Check whether a task has a particular permission to an inode.
1783 The 'adp' parameter is optional and allows other audit
1784 data to be passed (e.g. the dentry). */
David Howells88e67f32008-11-14 10:39:21 +11001785static int inode_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 struct inode *inode,
1787 u32 perms,
Linus Torvalds19e49832013-10-04 12:54:11 -07001788 struct common_audit_data *adp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 struct inode_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +11001791 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
David Howellse0e81732009-09-02 09:13:40 +01001793 validate_creds(cred);
1794
Eric Paris828dfe12008-04-17 13:17:49 -04001795 if (unlikely(IS_PRIVATE(inode)))
Stephen Smalleybbaca6c2007-02-14 00:34:16 -08001796 return 0;
1797
David Howells88e67f32008-11-14 10:39:21 +11001798 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 isec = inode->i_security;
1800
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001801 return avc_has_perm(&selinux_state,
1802 sid, isec->sid, isec->sclass, perms, adp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803}
1804
1805/* Same as inode_has_perm, but pass explicit audit data containing
1806 the dentry to help the auditing code to more easily generate the
1807 pathname if needed. */
David Howells88e67f32008-11-14 10:39:21 +11001808static inline int dentry_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 struct dentry *dentry,
1810 u32 av)
1811{
David Howellsc6f493d2015-03-17 22:26:22 +00001812 struct inode *inode = d_backing_inode(dentry);
Thomas Liu2bf49692009-07-14 12:14:09 -04001813 struct common_audit_data ad;
David Howells88e67f32008-11-14 10:39:21 +11001814
Eric Paris50c205f2012-04-04 15:01:43 -04001815 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Paris2875fa02011-04-28 16:04:24 -04001816 ad.u.dentry = dentry;
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05001817 __inode_security_revalidate(inode, dentry, true);
Linus Torvalds19e49832013-10-04 12:54:11 -07001818 return inode_has_perm(cred, inode, av, &ad);
Eric Paris2875fa02011-04-28 16:04:24 -04001819}
1820
1821/* Same as inode_has_perm, but pass explicit audit data containing
1822 the path to help the auditing code to more easily generate the
1823 pathname if needed. */
1824static inline int path_has_perm(const struct cred *cred,
Al Viro3f7036a2015-03-08 19:28:30 -04001825 const struct path *path,
Eric Paris2875fa02011-04-28 16:04:24 -04001826 u32 av)
1827{
David Howellsc6f493d2015-03-17 22:26:22 +00001828 struct inode *inode = d_backing_inode(path->dentry);
Eric Paris2875fa02011-04-28 16:04:24 -04001829 struct common_audit_data ad;
1830
Eric Paris50c205f2012-04-04 15:01:43 -04001831 ad.type = LSM_AUDIT_DATA_PATH;
Eric Paris2875fa02011-04-28 16:04:24 -04001832 ad.u.path = *path;
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05001833 __inode_security_revalidate(inode, path->dentry, true);
Linus Torvalds19e49832013-10-04 12:54:11 -07001834 return inode_has_perm(cred, inode, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835}
1836
David Howells13f8e982013-06-13 23:37:55 +01001837/* Same as path_has_perm, but uses the inode from the file struct. */
1838static inline int file_path_has_perm(const struct cred *cred,
1839 struct file *file,
1840 u32 av)
1841{
1842 struct common_audit_data ad;
1843
Vivek Goyal43af5de2016-09-09 11:37:49 -04001844 ad.type = LSM_AUDIT_DATA_FILE;
1845 ad.u.file = file;
Linus Torvalds19e49832013-10-04 12:54:11 -07001846 return inode_has_perm(cred, file_inode(file), av, &ad);
David Howells13f8e982013-06-13 23:37:55 +01001847}
1848
Chenbo Fengf66e4482017-10-18 13:00:26 -07001849#ifdef CONFIG_BPF_SYSCALL
1850static int bpf_fd_pass(struct file *file, u32 sid);
1851#endif
1852
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853/* Check whether a task can use an open file descriptor to
1854 access an inode in a given way. Check access to the
1855 descriptor itself, and then use dentry_has_perm to
1856 check a particular permission to the file.
1857 Access to the descriptor is implicitly granted if it
1858 has the same SID as the process. If av is zero, then
1859 access to the file is not checked, e.g. for cases
1860 where only the descriptor is affected like seek. */
David Howells88e67f32008-11-14 10:39:21 +11001861static int file_has_perm(const struct cred *cred,
1862 struct file *file,
1863 u32 av)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 struct file_security_struct *fsec = file->f_security;
Al Viro496ad9a2013-01-23 17:07:38 -05001866 struct inode *inode = file_inode(file);
Thomas Liu2bf49692009-07-14 12:14:09 -04001867 struct common_audit_data ad;
David Howells88e67f32008-11-14 10:39:21 +11001868 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 int rc;
1870
Vivek Goyal43af5de2016-09-09 11:37:49 -04001871 ad.type = LSM_AUDIT_DATA_FILE;
1872 ad.u.file = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
David Howells275bb412008-11-14 10:39:19 +11001874 if (sid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001875 rc = avc_has_perm(&selinux_state,
1876 sid, fsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 SECCLASS_FD,
1878 FD__USE,
1879 &ad);
1880 if (rc)
David Howells88e67f32008-11-14 10:39:21 +11001881 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 }
1883
Chenbo Fengf66e4482017-10-18 13:00:26 -07001884#ifdef CONFIG_BPF_SYSCALL
1885 rc = bpf_fd_pass(file, cred_sid(cred));
1886 if (rc)
1887 return rc;
1888#endif
1889
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 /* av is zero if only checking access to the descriptor. */
David Howells88e67f32008-11-14 10:39:21 +11001891 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 if (av)
Linus Torvalds19e49832013-10-04 12:54:11 -07001893 rc = inode_has_perm(cred, inode, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
David Howells88e67f32008-11-14 10:39:21 +11001895out:
1896 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897}
1898
David Howellsc3c188b2015-07-10 17:19:58 -04001899/*
1900 * Determine the label for an inode that might be unioned.
1901 */
Vivek Goyalc957f6d2016-07-13 10:44:51 -04001902static int
1903selinux_determine_inode_label(const struct task_security_struct *tsec,
1904 struct inode *dir,
1905 const struct qstr *name, u16 tclass,
1906 u32 *_new_isid)
David Howellsc3c188b2015-07-10 17:19:58 -04001907{
1908 const struct superblock_security_struct *sbsec = dir->i_sb->s_security;
David Howellsc3c188b2015-07-10 17:19:58 -04001909
1910 if ((sbsec->flags & SE_SBINITIALIZED) &&
1911 (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)) {
1912 *_new_isid = sbsec->mntpoint_sid;
1913 } else if ((sbsec->flags & SBLABEL_MNT) &&
1914 tsec->create_sid) {
1915 *_new_isid = tsec->create_sid;
1916 } else {
Paul Moore20cdef82016-04-04 14:14:42 -04001917 const struct inode_security_struct *dsec = inode_security(dir);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001918 return security_transition_sid(&selinux_state, tsec->sid,
1919 dsec->sid, tclass,
David Howellsc3c188b2015-07-10 17:19:58 -04001920 name, _new_isid);
1921 }
1922
1923 return 0;
1924}
1925
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926/* Check whether a task can create a file. */
1927static int may_create(struct inode *dir,
1928 struct dentry *dentry,
1929 u16 tclass)
1930{
Paul Moore5fb49872010-04-22 14:46:19 -04001931 const struct task_security_struct *tsec = current_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 struct inode_security_struct *dsec;
1933 struct superblock_security_struct *sbsec;
David Howells275bb412008-11-14 10:39:19 +11001934 u32 sid, newsid;
Thomas Liu2bf49692009-07-14 12:14:09 -04001935 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 int rc;
1937
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001938 dsec = inode_security(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 sbsec = dir->i_sb->s_security;
1940
David Howells275bb412008-11-14 10:39:19 +11001941 sid = tsec->sid;
David Howells275bb412008-11-14 10:39:19 +11001942
Eric Paris50c205f2012-04-04 15:01:43 -04001943 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04001944 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001946 rc = avc_has_perm(&selinux_state,
1947 sid, dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 DIR__ADD_NAME | DIR__SEARCH,
1949 &ad);
1950 if (rc)
1951 return rc;
1952
Vivek Goyalc957f6d2016-07-13 10:44:51 -04001953 rc = selinux_determine_inode_label(current_security(), dir,
1954 &dentry->d_name, tclass, &newsid);
David Howellsc3c188b2015-07-10 17:19:58 -04001955 if (rc)
1956 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001958 rc = avc_has_perm(&selinux_state,
1959 sid, newsid, tclass, FILE__CREATE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 if (rc)
1961 return rc;
1962
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001963 return avc_has_perm(&selinux_state,
1964 newsid, sbsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 SECCLASS_FILESYSTEM,
1966 FILESYSTEM__ASSOCIATE, &ad);
1967}
1968
Eric Paris828dfe12008-04-17 13:17:49 -04001969#define MAY_LINK 0
1970#define MAY_UNLINK 1
1971#define MAY_RMDIR 2
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
1973/* Check whether a task can link, unlink, or rmdir a file/directory. */
1974static int may_link(struct inode *dir,
1975 struct dentry *dentry,
1976 int kind)
1977
1978{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 struct inode_security_struct *dsec, *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001980 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11001981 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 u32 av;
1983 int rc;
1984
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001985 dsec = inode_security(dir);
1986 isec = backing_inode_security(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
Eric Paris50c205f2012-04-04 15:01:43 -04001988 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04001989 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
1991 av = DIR__SEARCH;
1992 av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001993 rc = avc_has_perm(&selinux_state,
1994 sid, dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 if (rc)
1996 return rc;
1997
1998 switch (kind) {
1999 case MAY_LINK:
2000 av = FILE__LINK;
2001 break;
2002 case MAY_UNLINK:
2003 av = FILE__UNLINK;
2004 break;
2005 case MAY_RMDIR:
2006 av = DIR__RMDIR;
2007 break;
2008 default:
peter enderborgc103a912018-06-12 10:09:03 +02002009 pr_warn("SELinux: %s: unrecognized kind %d\n",
Eric Paris744ba352008-04-17 11:52:44 -04002010 __func__, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 return 0;
2012 }
2013
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002014 rc = avc_has_perm(&selinux_state,
2015 sid, isec->sid, isec->sclass, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 return rc;
2017}
2018
2019static inline int may_rename(struct inode *old_dir,
2020 struct dentry *old_dentry,
2021 struct inode *new_dir,
2022 struct dentry *new_dentry)
2023{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04002025 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11002026 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 u32 av;
2028 int old_is_dir, new_is_dir;
2029 int rc;
2030
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002031 old_dsec = inode_security(old_dir);
2032 old_isec = backing_inode_security(old_dentry);
David Howellse36cb0b2015-01-29 12:02:35 +00002033 old_is_dir = d_is_dir(old_dentry);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002034 new_dsec = inode_security(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
Eric Paris50c205f2012-04-04 15:01:43 -04002036 ad.type = LSM_AUDIT_DATA_DENTRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
Eric Parisa2694342011-04-25 13:10:27 -04002038 ad.u.dentry = old_dentry;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002039 rc = avc_has_perm(&selinux_state,
2040 sid, old_dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 DIR__REMOVE_NAME | DIR__SEARCH, &ad);
2042 if (rc)
2043 return rc;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002044 rc = avc_has_perm(&selinux_state,
2045 sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 old_isec->sclass, FILE__RENAME, &ad);
2047 if (rc)
2048 return rc;
2049 if (old_is_dir && new_dir != old_dir) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002050 rc = avc_has_perm(&selinux_state,
2051 sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 old_isec->sclass, DIR__REPARENT, &ad);
2053 if (rc)
2054 return rc;
2055 }
2056
Eric Parisa2694342011-04-25 13:10:27 -04002057 ad.u.dentry = new_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 av = DIR__ADD_NAME | DIR__SEARCH;
David Howells2c616d42015-01-29 12:02:33 +00002059 if (d_is_positive(new_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 av |= DIR__REMOVE_NAME;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002061 rc = avc_has_perm(&selinux_state,
2062 sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 if (rc)
2064 return rc;
David Howells2c616d42015-01-29 12:02:33 +00002065 if (d_is_positive(new_dentry)) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002066 new_isec = backing_inode_security(new_dentry);
David Howellse36cb0b2015-01-29 12:02:35 +00002067 new_is_dir = d_is_dir(new_dentry);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002068 rc = avc_has_perm(&selinux_state,
2069 sid, new_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 new_isec->sclass,
2071 (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
2072 if (rc)
2073 return rc;
2074 }
2075
2076 return 0;
2077}
2078
2079/* Check whether a task can perform a filesystem operation. */
David Howells88e67f32008-11-14 10:39:21 +11002080static int superblock_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 struct super_block *sb,
2082 u32 perms,
Thomas Liu2bf49692009-07-14 12:14:09 -04002083 struct common_audit_data *ad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 struct superblock_security_struct *sbsec;
David Howells88e67f32008-11-14 10:39:21 +11002086 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 sbsec = sb->s_security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002089 return avc_has_perm(&selinux_state,
2090 sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091}
2092
2093/* Convert a Linux mode and permission mask to an access vector. */
2094static inline u32 file_mask_to_av(int mode, int mask)
2095{
2096 u32 av = 0;
2097
Al Virodba19c62011-07-25 20:49:29 -04002098 if (!S_ISDIR(mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 if (mask & MAY_EXEC)
2100 av |= FILE__EXECUTE;
2101 if (mask & MAY_READ)
2102 av |= FILE__READ;
2103
2104 if (mask & MAY_APPEND)
2105 av |= FILE__APPEND;
2106 else if (mask & MAY_WRITE)
2107 av |= FILE__WRITE;
2108
2109 } else {
2110 if (mask & MAY_EXEC)
2111 av |= DIR__SEARCH;
2112 if (mask & MAY_WRITE)
2113 av |= DIR__WRITE;
2114 if (mask & MAY_READ)
2115 av |= DIR__READ;
2116 }
2117
2118 return av;
2119}
2120
2121/* Convert a Linux file to an access vector. */
2122static inline u32 file_to_av(struct file *file)
2123{
2124 u32 av = 0;
2125
2126 if (file->f_mode & FMODE_READ)
2127 av |= FILE__READ;
2128 if (file->f_mode & FMODE_WRITE) {
2129 if (file->f_flags & O_APPEND)
2130 av |= FILE__APPEND;
2131 else
2132 av |= FILE__WRITE;
2133 }
Stephen Smalley0794c662008-03-17 08:55:18 -04002134 if (!av) {
2135 /*
2136 * Special file opened with flags 3 for ioctl-only use.
2137 */
2138 av = FILE__IOCTL;
2139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
2141 return av;
2142}
2143
Eric Paris8b6a5a32008-10-29 17:06:46 -04002144/*
2145 * Convert a file to an access vector and include the correct open
2146 * open permission.
2147 */
2148static inline u32 open_file_to_av(struct file *file)
2149{
2150 u32 av = file_to_av(file);
Stephen Smalleyccb54472017-05-12 12:41:24 -04002151 struct inode *inode = file_inode(file);
Eric Paris8b6a5a32008-10-29 17:06:46 -04002152
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002153 if (selinux_policycap_openperm() &&
2154 inode->i_sb->s_magic != SOCKFS_MAGIC)
Eric Paris49b7b8d2010-07-23 11:44:09 -04002155 av |= FILE__OPEN;
2156
Eric Paris8b6a5a32008-10-29 17:06:46 -04002157 return av;
2158}
2159
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160/* Hook functions begin here. */
2161
Stephen Smalley79af7302015-01-21 10:54:10 -05002162static int selinux_binder_set_context_mgr(struct task_struct *mgr)
2163{
2164 u32 mysid = current_sid();
2165 u32 mgrsid = task_sid(mgr);
2166
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002167 return avc_has_perm(&selinux_state,
2168 mysid, mgrsid, SECCLASS_BINDER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002169 BINDER__SET_CONTEXT_MGR, NULL);
2170}
2171
2172static int selinux_binder_transaction(struct task_struct *from,
2173 struct task_struct *to)
2174{
2175 u32 mysid = current_sid();
2176 u32 fromsid = task_sid(from);
2177 u32 tosid = task_sid(to);
2178 int rc;
2179
2180 if (mysid != fromsid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002181 rc = avc_has_perm(&selinux_state,
2182 mysid, fromsid, SECCLASS_BINDER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002183 BINDER__IMPERSONATE, NULL);
2184 if (rc)
2185 return rc;
2186 }
2187
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002188 return avc_has_perm(&selinux_state,
2189 fromsid, tosid, SECCLASS_BINDER, BINDER__CALL,
Stephen Smalley79af7302015-01-21 10:54:10 -05002190 NULL);
2191}
2192
2193static int selinux_binder_transfer_binder(struct task_struct *from,
2194 struct task_struct *to)
2195{
2196 u32 fromsid = task_sid(from);
2197 u32 tosid = task_sid(to);
2198
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002199 return avc_has_perm(&selinux_state,
2200 fromsid, tosid, SECCLASS_BINDER, BINDER__TRANSFER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002201 NULL);
2202}
2203
2204static int selinux_binder_transfer_file(struct task_struct *from,
2205 struct task_struct *to,
2206 struct file *file)
2207{
2208 u32 sid = task_sid(to);
2209 struct file_security_struct *fsec = file->f_security;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002210 struct dentry *dentry = file->f_path.dentry;
Paul Moore20cdef82016-04-04 14:14:42 -04002211 struct inode_security_struct *isec;
Stephen Smalley79af7302015-01-21 10:54:10 -05002212 struct common_audit_data ad;
2213 int rc;
2214
2215 ad.type = LSM_AUDIT_DATA_PATH;
2216 ad.u.path = file->f_path;
2217
2218 if (sid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002219 rc = avc_has_perm(&selinux_state,
2220 sid, fsec->sid,
Stephen Smalley79af7302015-01-21 10:54:10 -05002221 SECCLASS_FD,
2222 FD__USE,
2223 &ad);
2224 if (rc)
2225 return rc;
2226 }
2227
Chenbo Fengf66e4482017-10-18 13:00:26 -07002228#ifdef CONFIG_BPF_SYSCALL
2229 rc = bpf_fd_pass(file, sid);
2230 if (rc)
2231 return rc;
2232#endif
2233
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002234 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
Stephen Smalley79af7302015-01-21 10:54:10 -05002235 return 0;
2236
Paul Moore20cdef82016-04-04 14:14:42 -04002237 isec = backing_inode_security(dentry);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002238 return avc_has_perm(&selinux_state,
2239 sid, isec->sid, isec->sclass, file_to_av(file),
Stephen Smalley79af7302015-01-21 10:54:10 -05002240 &ad);
2241}
2242
Ingo Molnar9e488582009-05-07 19:26:19 +10002243static int selinux_ptrace_access_check(struct task_struct *child,
David Howells5cd9c582008-08-14 11:37:28 +01002244 unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002246 u32 sid = current_sid();
2247 u32 csid = task_sid(child);
Stephen Smalley006ebb42008-05-19 08:32:49 -04002248
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002249 if (mode & PTRACE_MODE_READ)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002250 return avc_has_perm(&selinux_state,
2251 sid, csid, SECCLASS_FILE, FILE__READ, NULL);
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002252
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002253 return avc_has_perm(&selinux_state,
2254 sid, csid, SECCLASS_PROCESS, PROCESS__PTRACE, NULL);
David Howells5cd9c582008-08-14 11:37:28 +01002255}
2256
2257static int selinux_ptrace_traceme(struct task_struct *parent)
2258{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002259 return avc_has_perm(&selinux_state,
2260 task_sid(parent), current_sid(), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002261 PROCESS__PTRACE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262}
2263
2264static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
Eric Paris828dfe12008-04-17 13:17:49 -04002265 kernel_cap_t *inheritable, kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002267 return avc_has_perm(&selinux_state,
2268 current_sid(), task_sid(target), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002269 PROCESS__GETCAP, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270}
2271
David Howellsd84f4f92008-11-14 10:39:23 +11002272static int selinux_capset(struct cred *new, const struct cred *old,
2273 const kernel_cap_t *effective,
2274 const kernel_cap_t *inheritable,
2275 const kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002277 return avc_has_perm(&selinux_state,
2278 cred_sid(old), cred_sid(new), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002279 PROCESS__SETCAP, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280}
2281
James Morris5626d3e2009-01-30 10:05:06 +11002282/*
2283 * (This comment used to live with the selinux_task_setuid hook,
2284 * which was removed).
2285 *
2286 * Since setuid only affects the current process, and since the SELinux
2287 * controls are not based on the Linux identity attributes, SELinux does not
2288 * need to control this operation. However, SELinux does control the use of
2289 * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
2290 */
2291
Eric Paris6a9de492012-01-03 12:25:14 -05002292static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
2293 int cap, int audit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294{
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04002295 return cred_has_capability(cred, cap, audit, ns == &init_user_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296}
2297
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
2299{
David Howells88e67f32008-11-14 10:39:21 +11002300 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 int rc = 0;
2302
2303 if (!sb)
2304 return 0;
2305
2306 switch (cmds) {
Eric Paris828dfe12008-04-17 13:17:49 -04002307 case Q_SYNC:
2308 case Q_QUOTAON:
2309 case Q_QUOTAOFF:
2310 case Q_SETINFO:
2311 case Q_SETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11002312 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04002313 break;
2314 case Q_GETFMT:
2315 case Q_GETINFO:
2316 case Q_GETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11002317 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04002318 break;
2319 default:
2320 rc = 0; /* let the kernel handle invalid cmds */
2321 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 }
2323 return rc;
2324}
2325
2326static int selinux_quota_on(struct dentry *dentry)
2327{
David Howells88e67f32008-11-14 10:39:21 +11002328 const struct cred *cred = current_cred();
2329
Eric Paris2875fa02011-04-28 16:04:24 -04002330 return dentry_has_perm(cred, dentry, FILE__QUOTAON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331}
2332
Eric Paris12b30522010-11-15 18:36:29 -05002333static int selinux_syslog(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 switch (type) {
Kees Cookd78ca3c2010-02-03 15:37:13 -08002336 case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */
2337 case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002338 return avc_has_perm(&selinux_state,
2339 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002340 SECCLASS_SYSTEM, SYSTEM__SYSLOG_READ, NULL);
Kees Cookd78ca3c2010-02-03 15:37:13 -08002341 case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */
2342 case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */
2343 /* Set level of messages printed to console */
2344 case SYSLOG_ACTION_CONSOLE_LEVEL:
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002345 return avc_has_perm(&selinux_state,
2346 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002347 SECCLASS_SYSTEM, SYSTEM__SYSLOG_CONSOLE,
2348 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 }
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002350 /* All other syslog types */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002351 return avc_has_perm(&selinux_state,
2352 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002353 SECCLASS_SYSTEM, SYSTEM__SYSLOG_MOD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354}
2355
2356/*
2357 * Check that a process has enough memory to allocate a new virtual
2358 * mapping. 0 means there is enough memory for the allocation to
2359 * succeed and -ENOMEM implies there is not.
2360 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 * Do not audit the selinux permission check, as this is applied to all
2362 * processes that allocate mappings.
2363 */
Alan Cox34b4e4a2007-08-22 14:01:28 -07002364static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365{
2366 int rc, cap_sys_admin = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002368 rc = cred_has_capability(current_cred(), CAP_SYS_ADMIN,
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04002369 SECURITY_CAP_NOAUDIT, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 if (rc == 0)
2371 cap_sys_admin = 1;
2372
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002373 return cap_sys_admin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374}
2375
2376/* binprm security operations */
2377
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002378static u32 ptrace_parent_sid(void)
Paul Moore0c6181c2016-03-30 21:41:21 -04002379{
2380 u32 sid = 0;
2381 struct task_struct *tracer;
2382
2383 rcu_read_lock();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002384 tracer = ptrace_parent(current);
Paul Moore0c6181c2016-03-30 21:41:21 -04002385 if (tracer)
2386 sid = task_sid(tracer);
2387 rcu_read_unlock();
2388
2389 return sid;
2390}
2391
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002392static int check_nnp_nosuid(const struct linux_binprm *bprm,
2393 const struct task_security_struct *old_tsec,
2394 const struct task_security_struct *new_tsec)
2395{
2396 int nnp = (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS);
Andy Lutomirski380cf5b2016-06-23 16:41:05 -05002397 int nosuid = !mnt_may_suid(bprm->file->f_path.mnt);
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002398 int rc;
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002399 u32 av;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002400
2401 if (!nnp && !nosuid)
2402 return 0; /* neither NNP nor nosuid */
2403
2404 if (new_tsec->sid == old_tsec->sid)
2405 return 0; /* No change in credentials */
2406
2407 /*
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002408 * If the policy enables the nnp_nosuid_transition policy capability,
2409 * then we permit transitions under NNP or nosuid if the
2410 * policy allows the corresponding permission between
2411 * the old and new contexts.
2412 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002413 if (selinux_policycap_nnp_nosuid_transition()) {
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002414 av = 0;
2415 if (nnp)
2416 av |= PROCESS2__NNP_TRANSITION;
2417 if (nosuid)
2418 av |= PROCESS2__NOSUID_TRANSITION;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002419 rc = avc_has_perm(&selinux_state,
2420 old_tsec->sid, new_tsec->sid,
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002421 SECCLASS_PROCESS2, av, NULL);
2422 if (!rc)
2423 return 0;
2424 }
2425
2426 /*
2427 * We also permit NNP or nosuid transitions to bounded SIDs,
2428 * i.e. SIDs that are guaranteed to only be allowed a subset
2429 * of the permissions of the current SID.
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002430 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002431 rc = security_bounded_transition(&selinux_state, old_tsec->sid,
2432 new_tsec->sid);
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002433 if (!rc)
2434 return 0;
2435
2436 /*
2437 * On failure, preserve the errno values for NNP vs nosuid.
2438 * NNP: Operation not permitted for caller.
2439 * nosuid: Permission denied to file.
2440 */
2441 if (nnp)
2442 return -EPERM;
2443 return -EACCES;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002444}
2445
David Howellsa6f76f22008-11-14 10:39:24 +11002446static int selinux_bprm_set_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447{
David Howellsa6f76f22008-11-14 10:39:24 +11002448 const struct task_security_struct *old_tsec;
2449 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 struct inode_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04002451 struct common_audit_data ad;
Al Viro496ad9a2013-01-23 17:07:38 -05002452 struct inode *inode = file_inode(bprm->file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 int rc;
2454
David Howellsa6f76f22008-11-14 10:39:24 +11002455 /* SELinux context only depends on initial program or script and not
2456 * the script interpreter */
Kees Cookddb4a142017-07-18 15:25:23 -07002457 if (bprm->called_set_creds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 return 0;
2459
David Howellsa6f76f22008-11-14 10:39:24 +11002460 old_tsec = current_security();
2461 new_tsec = bprm->cred->security;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002462 isec = inode_security(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463
2464 /* Default to the current task SID. */
David Howellsa6f76f22008-11-14 10:39:24 +11002465 new_tsec->sid = old_tsec->sid;
2466 new_tsec->osid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
Michael LeMay28eba5b2006-06-27 02:53:42 -07002468 /* Reset fs, key, and sock SIDs on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002469 new_tsec->create_sid = 0;
2470 new_tsec->keycreate_sid = 0;
2471 new_tsec->sockcreate_sid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
David Howellsa6f76f22008-11-14 10:39:24 +11002473 if (old_tsec->exec_sid) {
2474 new_tsec->sid = old_tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 /* Reset exec SID on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002476 new_tsec->exec_sid = 0;
Andy Lutomirski259e5e62012-04-12 16:47:50 -05002477
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002478 /* Fail on NNP or nosuid if not an allowed transition. */
2479 rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
2480 if (rc)
2481 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 } else {
2483 /* Check for a default transition on this program. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002484 rc = security_transition_sid(&selinux_state, old_tsec->sid,
2485 isec->sid, SECCLASS_PROCESS, NULL,
Eric Paris652bb9b2011-02-01 11:05:40 -05002486 &new_tsec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 if (rc)
2488 return rc;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002489
2490 /*
2491 * Fallback to old SID on NNP or nosuid if not an allowed
2492 * transition.
2493 */
2494 rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
2495 if (rc)
2496 new_tsec->sid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 }
2498
Vivek Goyal43af5de2016-09-09 11:37:49 -04002499 ad.type = LSM_AUDIT_DATA_FILE;
2500 ad.u.file = bprm->file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501
David Howellsa6f76f22008-11-14 10:39:24 +11002502 if (new_tsec->sid == old_tsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002503 rc = avc_has_perm(&selinux_state,
2504 old_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
2506 if (rc)
2507 return rc;
2508 } else {
2509 /* Check permissions for the transition. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002510 rc = avc_has_perm(&selinux_state,
2511 old_tsec->sid, new_tsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
2513 if (rc)
2514 return rc;
2515
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002516 rc = avc_has_perm(&selinux_state,
2517 new_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
2519 if (rc)
2520 return rc;
2521
David Howellsa6f76f22008-11-14 10:39:24 +11002522 /* Check for shared state */
2523 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002524 rc = avc_has_perm(&selinux_state,
2525 old_tsec->sid, new_tsec->sid,
David Howellsa6f76f22008-11-14 10:39:24 +11002526 SECCLASS_PROCESS, PROCESS__SHARE,
2527 NULL);
2528 if (rc)
2529 return -EPERM;
2530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531
David Howellsa6f76f22008-11-14 10:39:24 +11002532 /* Make sure that anyone attempting to ptrace over a task that
2533 * changes its SID has the appropriate permit */
Eric W. Biederman9227dd22017-01-23 17:26:31 +13002534 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002535 u32 ptsid = ptrace_parent_sid();
David Howellsa6f76f22008-11-14 10:39:24 +11002536 if (ptsid != 0) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002537 rc = avc_has_perm(&selinux_state,
2538 ptsid, new_tsec->sid,
David Howellsa6f76f22008-11-14 10:39:24 +11002539 SECCLASS_PROCESS,
2540 PROCESS__PTRACE, NULL);
2541 if (rc)
2542 return -EPERM;
2543 }
2544 }
2545
2546 /* Clear any possibly unsafe personality bits on exec: */
2547 bprm->per_clear |= PER_CLEAR_ON_SETID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 /* Enable secure mode for SIDs transitions unless
2550 the noatsecure permission is granted between
2551 the two SIDs, i.e. ahp returns 0. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002552 rc = avc_has_perm(&selinux_state,
2553 old_tsec->sid, new_tsec->sid,
Kees Cook62874c32017-07-18 15:25:25 -07002554 SECCLASS_PROCESS, PROCESS__NOATSECURE,
2555 NULL);
2556 bprm->secureexec |= !!rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 }
2558
Kees Cook62874c32017-07-18 15:25:25 -07002559 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560}
2561
Al Viroc3c073f2012-08-21 22:32:06 -04002562static int match_file(const void *p, struct file *file, unsigned fd)
2563{
2564 return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0;
2565}
2566
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567/* Derived from fs/exec.c:flush_old_files. */
David Howells745ca242008-11-14 10:39:22 +11002568static inline void flush_unauthorized_files(const struct cred *cred,
2569 struct files_struct *files)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 struct file *file, *devnull = NULL;
Stephen Smalleyb20c8122006-09-25 23:32:03 -07002572 struct tty_struct *tty;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002573 int drop_tty = 0;
Al Viroc3c073f2012-08-21 22:32:06 -04002574 unsigned n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002576 tty = get_current_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 if (tty) {
Peter Hurley4a510962016-01-09 21:35:23 -08002578 spin_lock(&tty->files_lock);
Eric Paris37dd0bd2008-10-31 17:40:00 -04002579 if (!list_empty(&tty->tty_files)) {
Nick Piggind996b622010-08-18 04:37:36 +10002580 struct tty_file_private *file_priv;
Eric Paris37dd0bd2008-10-31 17:40:00 -04002581
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 /* Revalidate access to controlling tty.
David Howells13f8e982013-06-13 23:37:55 +01002583 Use file_path_has_perm on the tty path directly
2584 rather than using file_has_perm, as this particular
2585 open file may belong to another process and we are
2586 only interested in the inode-based check here. */
Nick Piggind996b622010-08-18 04:37:36 +10002587 file_priv = list_first_entry(&tty->tty_files,
2588 struct tty_file_private, list);
2589 file = file_priv->file;
David Howells13f8e982013-06-13 23:37:55 +01002590 if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE))
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002591 drop_tty = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 }
Peter Hurley4a510962016-01-09 21:35:23 -08002593 spin_unlock(&tty->files_lock);
Alan Cox452a00d2008-10-13 10:39:13 +01002594 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 }
Eric W. Biederman98a27ba2007-05-08 00:26:56 -07002596 /* Reset controlling tty. */
2597 if (drop_tty)
2598 no_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599
2600 /* Revalidate access to inherited open files. */
Al Viroc3c073f2012-08-21 22:32:06 -04002601 n = iterate_fd(files, 0, match_file, cred);
2602 if (!n) /* none found? */
2603 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604
Al Viroc3c073f2012-08-21 22:32:06 -04002605 devnull = dentry_open(&selinux_null, O_RDWR, cred);
Al Viro45525b22012-10-16 13:30:07 -04002606 if (IS_ERR(devnull))
2607 devnull = NULL;
2608 /* replace all the matching ones with this */
2609 do {
2610 replace_fd(n - 1, devnull, 0);
2611 } while ((n = iterate_fd(files, n, match_file, cred)) != 0);
2612 if (devnull)
Al Viroc3c073f2012-08-21 22:32:06 -04002613 fput(devnull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614}
2615
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616/*
David Howellsa6f76f22008-11-14 10:39:24 +11002617 * Prepare a process for imminent new credential changes due to exec
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 */
David Howellsa6f76f22008-11-14 10:39:24 +11002619static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620{
David Howellsa6f76f22008-11-14 10:39:24 +11002621 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 struct rlimit *rlim, *initrlim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 int rc, i;
2624
David Howellsa6f76f22008-11-14 10:39:24 +11002625 new_tsec = bprm->cred->security;
2626 if (new_tsec->sid == new_tsec->osid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 return;
2628
2629 /* Close files for which the new task SID is not authorized. */
David Howellsa6f76f22008-11-14 10:39:24 +11002630 flush_unauthorized_files(bprm->cred, current->files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631
David Howellsa6f76f22008-11-14 10:39:24 +11002632 /* Always clear parent death signal on SID transitions. */
2633 current->pdeath_signal = 0;
2634
2635 /* Check whether the new SID can inherit resource limits from the old
2636 * SID. If not, reset all soft limits to the lower of the current
2637 * task's hard limit and the init task's soft limit.
2638 *
2639 * Note that the setting of hard limits (even to lower them) can be
2640 * controlled by the setrlimit check. The inclusion of the init task's
2641 * soft limit into the computation is to avoid resetting soft limits
2642 * higher than the default soft limit for cases where the default is
2643 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2644 */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002645 rc = avc_has_perm(&selinux_state,
2646 new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
David Howellsa6f76f22008-11-14 10:39:24 +11002647 PROCESS__RLIMITINH, NULL);
2648 if (rc) {
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002649 /* protect against do_prlimit() */
2650 task_lock(current);
David Howellsa6f76f22008-11-14 10:39:24 +11002651 for (i = 0; i < RLIM_NLIMITS; i++) {
2652 rlim = current->signal->rlim + i;
2653 initrlim = init_task.signal->rlim + i;
2654 rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
2655 }
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002656 task_unlock(current);
Nicolas Pitrebaa73d92016-11-11 00:10:10 -05002657 if (IS_ENABLED(CONFIG_POSIX_TIMERS))
2658 update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
David Howellsa6f76f22008-11-14 10:39:24 +11002659 }
2660}
2661
2662/*
2663 * Clean up the process immediately after the installation of new credentials
2664 * due to exec
2665 */
2666static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
2667{
2668 const struct task_security_struct *tsec = current_security();
2669 struct itimerval itimer;
David Howellsa6f76f22008-11-14 10:39:24 +11002670 u32 osid, sid;
2671 int rc, i;
David Howellsa6f76f22008-11-14 10:39:24 +11002672
David Howellsa6f76f22008-11-14 10:39:24 +11002673 osid = tsec->osid;
2674 sid = tsec->sid;
2675
2676 if (sid == osid)
2677 return;
2678
2679 /* Check whether the new SID can inherit signal state from the old SID.
2680 * If not, clear itimers to avoid subsequent signal generation and
2681 * flush and unblock signals.
2682 *
2683 * This must occur _after_ the task SID has been updated so that any
2684 * kill done after the flush will be checked against the new SID.
2685 */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002686 rc = avc_has_perm(&selinux_state,
2687 osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 if (rc) {
Nicolas Pitrebaa73d92016-11-11 00:10:10 -05002689 if (IS_ENABLED(CONFIG_POSIX_TIMERS)) {
2690 memset(&itimer, 0, sizeof itimer);
2691 for (i = 0; i < 3; i++)
2692 do_setitimer(i, &itimer, NULL);
2693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterov9e7c8f82015-06-04 16:22:16 -04002695 if (!fatal_signal_pending(current)) {
2696 flush_sigqueue(&current->pending);
2697 flush_sigqueue(&current->signal->shared_pending);
David Howells3bcac022009-04-29 13:45:05 +01002698 flush_signal_handlers(current, 1);
2699 sigemptyset(&current->blocked);
Oleg Nesterov9e7c8f82015-06-04 16:22:16 -04002700 recalc_sigpending();
David Howells3bcac022009-04-29 13:45:05 +01002701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 spin_unlock_irq(&current->sighand->siglock);
2703 }
2704
David Howellsa6f76f22008-11-14 10:39:24 +11002705 /* Wake up the parent if it is waiting so that it can recheck
2706 * wait permission to the new task SID. */
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002707 read_lock(&tasklist_lock);
Oleg Nesterov0b7570e2009-09-23 15:56:46 -07002708 __wake_up_parent(current, current->real_parent);
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002709 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710}
2711
2712/* superblock security operations */
2713
2714static int selinux_sb_alloc_security(struct super_block *sb)
2715{
2716 return superblock_alloc_security(sb);
2717}
2718
2719static void selinux_sb_free_security(struct super_block *sb)
2720{
2721 superblock_free_security(sb);
2722}
2723
2724static inline int match_prefix(char *prefix, int plen, char *option, int olen)
2725{
2726 if (plen > olen)
2727 return 0;
2728
2729 return !memcmp(prefix, option, plen);
2730}
2731
2732static inline int selinux_option(char *option, int len)
2733{
Eric Paris832cbd92008-04-01 13:24:09 -04002734 return (match_prefix(CONTEXT_STR, sizeof(CONTEXT_STR)-1, option, len) ||
2735 match_prefix(FSCONTEXT_STR, sizeof(FSCONTEXT_STR)-1, option, len) ||
2736 match_prefix(DEFCONTEXT_STR, sizeof(DEFCONTEXT_STR)-1, option, len) ||
David P. Quigley11689d42009-01-16 09:22:03 -05002737 match_prefix(ROOTCONTEXT_STR, sizeof(ROOTCONTEXT_STR)-1, option, len) ||
2738 match_prefix(LABELSUPP_STR, sizeof(LABELSUPP_STR)-1, option, len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739}
2740
2741static inline void take_option(char **to, char *from, int *first, int len)
2742{
2743 if (!*first) {
2744 **to = ',';
2745 *to += 1;
Cory Olmo3528a952006-09-29 01:58:44 -07002746 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 *first = 0;
2748 memcpy(*to, from, len);
2749 *to += len;
2750}
2751
Eric Paris828dfe12008-04-17 13:17:49 -04002752static inline void take_selinux_option(char **to, char *from, int *first,
2753 int len)
Cory Olmo3528a952006-09-29 01:58:44 -07002754{
2755 int current_size = 0;
2756
2757 if (!*first) {
2758 **to = '|';
2759 *to += 1;
Eric Paris828dfe12008-04-17 13:17:49 -04002760 } else
Cory Olmo3528a952006-09-29 01:58:44 -07002761 *first = 0;
2762
2763 while (current_size < len) {
2764 if (*from != '"') {
2765 **to = *from;
2766 *to += 1;
2767 }
2768 from += 1;
2769 current_size += 1;
2770 }
2771}
2772
Eric Parise0007522008-03-05 10:31:54 -05002773static int selinux_sb_copy_data(char *orig, char *copy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774{
2775 int fnosec, fsec, rc = 0;
2776 char *in_save, *in_curr, *in_end;
2777 char *sec_curr, *nosec_save, *nosec;
Cory Olmo3528a952006-09-29 01:58:44 -07002778 int open_quote = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779
2780 in_curr = orig;
2781 sec_curr = copy;
2782
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 nosec = (char *)get_zeroed_page(GFP_KERNEL);
2784 if (!nosec) {
2785 rc = -ENOMEM;
2786 goto out;
2787 }
2788
2789 nosec_save = nosec;
2790 fnosec = fsec = 1;
2791 in_save = in_end = orig;
2792
2793 do {
Cory Olmo3528a952006-09-29 01:58:44 -07002794 if (*in_end == '"')
2795 open_quote = !open_quote;
2796 if ((*in_end == ',' && open_quote == 0) ||
2797 *in_end == '\0') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 int len = in_end - in_curr;
2799
2800 if (selinux_option(in_curr, len))
Cory Olmo3528a952006-09-29 01:58:44 -07002801 take_selinux_option(&sec_curr, in_curr, &fsec, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 else
2803 take_option(&nosec, in_curr, &fnosec, len);
2804
2805 in_curr = in_end + 1;
2806 }
2807 } while (*in_end++);
2808
Eric Paris6931dfc2005-06-30 02:58:51 -07002809 strcpy(in_save, nosec_save);
Gerald Schaeferda3caa22005-06-21 17:15:18 -07002810 free_page((unsigned long)nosec_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811out:
2812 return rc;
2813}
2814
Al Viroc039bc32018-12-01 23:06:57 -05002815static int selinux_sb_remount(struct super_block *sb,
2816 struct security_mnt_opts *opts)
Eric Paris026eb162011-03-03 16:09:14 -05002817{
Al Viroc039bc32018-12-01 23:06:57 -05002818 int i, *flags;
2819 char **mount_options;
Eric Paris026eb162011-03-03 16:09:14 -05002820 struct superblock_security_struct *sbsec = sb->s_security;
2821
2822 if (!(sbsec->flags & SE_SBINITIALIZED))
2823 return 0;
2824
Al Viroc039bc32018-12-01 23:06:57 -05002825 mount_options = opts->mnt_opts;
2826 flags = opts->mnt_opts_flags;
Eric Paris026eb162011-03-03 16:09:14 -05002827
Al Viroc039bc32018-12-01 23:06:57 -05002828 for (i = 0; i < opts->num_mnt_opts; i++) {
Eric Paris026eb162011-03-03 16:09:14 -05002829 u32 sid;
Al Viroc039bc32018-12-01 23:06:57 -05002830 int rc;
Eric Paris026eb162011-03-03 16:09:14 -05002831
Eric Paris12f348b2012-10-09 10:56:25 -04002832 if (flags[i] == SBLABEL_MNT)
Eric Paris026eb162011-03-03 16:09:14 -05002833 continue;
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002834 rc = security_context_str_to_sid(&selinux_state,
2835 mount_options[i], &sid,
2836 GFP_KERNEL);
Eric Paris026eb162011-03-03 16:09:14 -05002837 if (rc) {
peter enderborgc103a912018-06-12 10:09:03 +02002838 pr_warn("SELinux: security_context_str_to_sid"
Linus Torvalds29b1deb2013-12-15 11:17:45 -08002839 "(%s) failed for (dev %s, type %s) errno=%d\n",
2840 mount_options[i], sb->s_id, sb->s_type->name, rc);
Al Viroc039bc32018-12-01 23:06:57 -05002841 return rc;
Eric Paris026eb162011-03-03 16:09:14 -05002842 }
Eric Paris026eb162011-03-03 16:09:14 -05002843 switch (flags[i]) {
2844 case FSCONTEXT_MNT:
2845 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
2846 goto out_bad_option;
2847 break;
2848 case CONTEXT_MNT:
2849 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
2850 goto out_bad_option;
2851 break;
2852 case ROOTCONTEXT_MNT: {
2853 struct inode_security_struct *root_isec;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002854 root_isec = backing_inode_security(sb->s_root);
Eric Paris026eb162011-03-03 16:09:14 -05002855
2856 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
2857 goto out_bad_option;
2858 break;
2859 }
2860 case DEFCONTEXT_MNT:
2861 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
2862 goto out_bad_option;
2863 break;
2864 default:
Al Viroc039bc32018-12-01 23:06:57 -05002865 return -EINVAL;
Eric Paris026eb162011-03-03 16:09:14 -05002866 }
2867 }
Al Viroc039bc32018-12-01 23:06:57 -05002868 return 0;
Eric Paris026eb162011-03-03 16:09:14 -05002869
Eric Paris026eb162011-03-03 16:09:14 -05002870out_bad_option:
peter enderborgc103a912018-06-12 10:09:03 +02002871 pr_warn("SELinux: unable to change security options "
Linus Torvalds29b1deb2013-12-15 11:17:45 -08002872 "during remount (dev %s, type=%s)\n", sb->s_id,
2873 sb->s_type->name);
Al Viroc039bc32018-12-01 23:06:57 -05002874 return -EINVAL;
Eric Paris026eb162011-03-03 16:09:14 -05002875}
2876
Al Viro6be87502018-12-01 22:42:44 -05002877static int selinux_sb_kern_mount(struct super_block *sb, int flags,
2878 struct security_mnt_opts *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879{
David Howells88e67f32008-11-14 10:39:21 +11002880 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002881 struct common_audit_data ad;
Al Viro6be87502018-12-01 22:42:44 -05002882 int rc = selinux_set_mnt_opts(sb, opts, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 if (rc)
2884 return rc;
2885
James Morris74192242008-12-19 11:41:10 +11002886 /* Allow all mounts performed by the kernel */
2887 if (flags & MS_KERNMOUNT)
2888 return 0;
2889
Eric Paris50c205f2012-04-04 15:01:43 -04002890 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002891 ad.u.dentry = sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002892 return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893}
2894
David Howells726c3342006-06-23 02:02:58 -07002895static int selinux_sb_statfs(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896{
David Howells88e67f32008-11-14 10:39:21 +11002897 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002898 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899
Eric Paris50c205f2012-04-04 15:01:43 -04002900 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002901 ad.u.dentry = dentry->d_sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002902 return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903}
2904
Al Viro808d4e32012-10-11 11:42:01 -04002905static int selinux_mount(const char *dev_name,
Al Viro8a04c432016-03-25 14:52:53 -04002906 const struct path *path,
Al Viro808d4e32012-10-11 11:42:01 -04002907 const char *type,
Eric Paris828dfe12008-04-17 13:17:49 -04002908 unsigned long flags,
2909 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910{
David Howells88e67f32008-11-14 10:39:21 +11002911 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912
2913 if (flags & MS_REMOUNT)
Al Virod8c95842011-12-07 18:16:57 -05002914 return superblock_has_perm(cred, path->dentry->d_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002915 FILESYSTEM__REMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 else
Eric Paris2875fa02011-04-28 16:04:24 -04002917 return path_has_perm(cred, path, FILE__MOUNTON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918}
2919
2920static int selinux_umount(struct vfsmount *mnt, int flags)
2921{
David Howells88e67f32008-11-14 10:39:21 +11002922 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923
David Howells88e67f32008-11-14 10:39:21 +11002924 return superblock_has_perm(cred, mnt->mnt_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002925 FILESYSTEM__UNMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926}
2927
2928/* inode security operations */
2929
2930static int selinux_inode_alloc_security(struct inode *inode)
2931{
2932 return inode_alloc_security(inode);
2933}
2934
2935static void selinux_inode_free_security(struct inode *inode)
2936{
2937 inode_free_security(inode);
2938}
2939
David Quigleyd47be3d2013-05-22 12:50:34 -04002940static int selinux_dentry_init_security(struct dentry *dentry, int mode,
Al Viro4f3ccd72016-07-20 16:06:15 -04002941 const struct qstr *name, void **ctx,
David Quigleyd47be3d2013-05-22 12:50:34 -04002942 u32 *ctxlen)
2943{
David Quigleyd47be3d2013-05-22 12:50:34 -04002944 u32 newsid;
2945 int rc;
2946
Vivek Goyalc957f6d2016-07-13 10:44:51 -04002947 rc = selinux_determine_inode_label(current_security(),
2948 d_inode(dentry->d_parent), name,
David Howellsc3c188b2015-07-10 17:19:58 -04002949 inode_mode_to_security_class(mode),
2950 &newsid);
2951 if (rc)
2952 return rc;
David Quigleyd47be3d2013-05-22 12:50:34 -04002953
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002954 return security_sid_to_context(&selinux_state, newsid, (char **)ctx,
2955 ctxlen);
David Quigleyd47be3d2013-05-22 12:50:34 -04002956}
2957
Vivek Goyala518b0a2016-07-13 10:44:53 -04002958static int selinux_dentry_create_files_as(struct dentry *dentry, int mode,
2959 struct qstr *name,
2960 const struct cred *old,
2961 struct cred *new)
2962{
2963 u32 newsid;
2964 int rc;
2965 struct task_security_struct *tsec;
2966
2967 rc = selinux_determine_inode_label(old->security,
2968 d_inode(dentry->d_parent), name,
2969 inode_mode_to_security_class(mode),
2970 &newsid);
2971 if (rc)
2972 return rc;
2973
2974 tsec = new->security;
2975 tsec->create_sid = newsid;
2976 return 0;
2977}
2978
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002979static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +09002980 const struct qstr *qstr,
2981 const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -05002982 void **value, size_t *len)
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002983{
Paul Moore5fb49872010-04-22 14:46:19 -04002984 const struct task_security_struct *tsec = current_security();
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002985 struct superblock_security_struct *sbsec;
Corentin LABBEc0d4f462017-10-04 20:32:17 +02002986 u32 newsid, clen;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002987 int rc;
Tetsuo Handa95489062013-07-25 05:44:02 +09002988 char *context;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002989
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002990 sbsec = dir->i_sb->s_security;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002991
David Howells275bb412008-11-14 10:39:19 +11002992 newsid = tsec->create_sid;
2993
Vivek Goyalc957f6d2016-07-13 10:44:51 -04002994 rc = selinux_determine_inode_label(current_security(),
David Howellsc3c188b2015-07-10 17:19:58 -04002995 dir, qstr,
2996 inode_mode_to_security_class(inode->i_mode),
2997 &newsid);
2998 if (rc)
2999 return rc;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07003000
Eric Paris296fddf2006-09-25 23:32:00 -07003001 /* Possibly defer initialization to selinux_complete_init. */
David P. Quigley0d90a7e2009-01-16 09:22:02 -05003002 if (sbsec->flags & SE_SBINITIALIZED) {
Eric Paris296fddf2006-09-25 23:32:00 -07003003 struct inode_security_struct *isec = inode->i_security;
3004 isec->sclass = inode_mode_to_security_class(inode->i_mode);
3005 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05003006 isec->initialized = LABEL_INITIALIZED;
Eric Paris296fddf2006-09-25 23:32:00 -07003007 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07003008
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003009 if (!selinux_state.initialized || !(sbsec->flags & SBLABEL_MNT))
Stephen Smalley25a74f32005-11-08 21:34:33 -08003010 return -EOPNOTSUPP;
3011
Tetsuo Handa95489062013-07-25 05:44:02 +09003012 if (name)
3013 *name = XATTR_SELINUX_SUFFIX;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07003014
3015 if (value && len) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003016 rc = security_sid_to_context_force(&selinux_state, newsid,
3017 &context, &clen);
Tetsuo Handa95489062013-07-25 05:44:02 +09003018 if (rc)
Stephen Smalley570bc1c2005-09-09 13:01:43 -07003019 return rc;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07003020 *value = context;
3021 *len = clen;
3022 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07003023
Stephen Smalley5e41ff92005-09-09 13:01:35 -07003024 return 0;
3025}
3026
Al Viro4acdaf22011-07-26 01:42:34 -04003027static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028{
3029 return may_create(dir, dentry, SECCLASS_FILE);
3030}
3031
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
3033{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 return may_link(dir, old_dentry, MAY_LINK);
3035}
3036
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
3038{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 return may_link(dir, dentry, MAY_UNLINK);
3040}
3041
3042static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
3043{
3044 return may_create(dir, dentry, SECCLASS_LNK_FILE);
3045}
3046
Al Viro18bb1db2011-07-26 01:41:39 -04003047static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048{
3049 return may_create(dir, dentry, SECCLASS_DIR);
3050}
3051
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
3053{
3054 return may_link(dir, dentry, MAY_RMDIR);
3055}
3056
Al Viro1a67aaf2011-07-26 01:52:52 -04003057static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 return may_create(dir, dentry, inode_mode_to_security_class(mode));
3060}
3061
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
Eric Paris828dfe12008-04-17 13:17:49 -04003063 struct inode *new_inode, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064{
3065 return may_rename(old_inode, old_dentry, new_inode, new_dentry);
3066}
3067
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068static int selinux_inode_readlink(struct dentry *dentry)
3069{
David Howells88e67f32008-11-14 10:39:21 +11003070 const struct cred *cred = current_cred();
3071
Eric Paris2875fa02011-04-28 16:04:24 -04003072 return dentry_has_perm(cred, dentry, FILE__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073}
3074
NeilBrownbda0be72015-03-23 13:37:39 +11003075static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode,
3076 bool rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077{
David Howells88e67f32008-11-14 10:39:21 +11003078 const struct cred *cred = current_cred();
NeilBrownbda0be72015-03-23 13:37:39 +11003079 struct common_audit_data ad;
3080 struct inode_security_struct *isec;
3081 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082
NeilBrownbda0be72015-03-23 13:37:39 +11003083 validate_creds(cred);
3084
3085 ad.type = LSM_AUDIT_DATA_DENTRY;
3086 ad.u.dentry = dentry;
3087 sid = cred_sid(cred);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05003088 isec = inode_security_rcu(inode, rcu);
3089 if (IS_ERR(isec))
3090 return PTR_ERR(isec);
NeilBrownbda0be72015-03-23 13:37:39 +11003091
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003092 return avc_has_perm_flags(&selinux_state,
3093 sid, isec->sid, isec->sclass, FILE__READ, &ad,
NeilBrownbda0be72015-03-23 13:37:39 +11003094 rcu ? MAY_NOT_BLOCK : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095}
3096
Eric Parisd4cf970d2012-04-04 15:01:42 -04003097static noinline int audit_inode_permission(struct inode *inode,
3098 u32 perms, u32 audited, u32 denied,
Stephen Smalley626b9742014-04-29 11:29:04 -07003099 int result,
Eric Parisd4cf970d2012-04-04 15:01:42 -04003100 unsigned flags)
3101{
3102 struct common_audit_data ad;
Eric Parisd4cf970d2012-04-04 15:01:42 -04003103 struct inode_security_struct *isec = inode->i_security;
3104 int rc;
3105
Eric Paris50c205f2012-04-04 15:01:43 -04003106 ad.type = LSM_AUDIT_DATA_INODE;
Eric Parisd4cf970d2012-04-04 15:01:42 -04003107 ad.u.inode = inode;
3108
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003109 rc = slow_avc_audit(&selinux_state,
3110 current_sid(), isec->sid, isec->sclass, perms,
Stephen Smalley626b9742014-04-29 11:29:04 -07003111 audited, denied, result, &ad, flags);
Eric Parisd4cf970d2012-04-04 15:01:42 -04003112 if (rc)
3113 return rc;
3114 return 0;
3115}
3116
Al Viroe74f71e2011-06-20 19:38:15 -04003117static int selinux_inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118{
David Howells88e67f32008-11-14 10:39:21 +11003119 const struct cred *cred = current_cred();
Eric Parisb782e0a2010-07-23 11:44:03 -04003120 u32 perms;
3121 bool from_access;
Al Virocf1dd1d2011-06-20 19:44:08 -04003122 unsigned flags = mask & MAY_NOT_BLOCK;
Eric Paris2e334052012-04-04 15:01:42 -04003123 struct inode_security_struct *isec;
3124 u32 sid;
3125 struct av_decision avd;
3126 int rc, rc2;
3127 u32 audited, denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128
Eric Parisb782e0a2010-07-23 11:44:03 -04003129 from_access = mask & MAY_ACCESS;
Eric Parisd09ca732010-07-23 11:43:57 -04003130 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
3131
Eric Parisb782e0a2010-07-23 11:44:03 -04003132 /* No permission to check. Existence test. */
3133 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135
Eric Paris2e334052012-04-04 15:01:42 -04003136 validate_creds(cred);
Eric Parisb782e0a2010-07-23 11:44:03 -04003137
Eric Paris2e334052012-04-04 15:01:42 -04003138 if (unlikely(IS_PRIVATE(inode)))
3139 return 0;
Eric Parisb782e0a2010-07-23 11:44:03 -04003140
3141 perms = file_mask_to_av(inode->i_mode, mask);
3142
Eric Paris2e334052012-04-04 15:01:42 -04003143 sid = cred_sid(cred);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05003144 isec = inode_security_rcu(inode, flags & MAY_NOT_BLOCK);
3145 if (IS_ERR(isec))
3146 return PTR_ERR(isec);
Eric Paris2e334052012-04-04 15:01:42 -04003147
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003148 rc = avc_has_perm_noaudit(&selinux_state,
3149 sid, isec->sid, isec->sclass, perms, 0, &avd);
Eric Paris2e334052012-04-04 15:01:42 -04003150 audited = avc_audit_required(perms, &avd, rc,
3151 from_access ? FILE__AUDIT_ACCESS : 0,
3152 &denied);
3153 if (likely(!audited))
3154 return rc;
3155
Stephen Smalley626b9742014-04-29 11:29:04 -07003156 rc2 = audit_inode_permission(inode, perms, audited, denied, rc, flags);
Eric Paris2e334052012-04-04 15:01:42 -04003157 if (rc2)
3158 return rc2;
3159 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160}
3161
3162static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
3163{
David Howells88e67f32008-11-14 10:39:21 +11003164 const struct cred *cred = current_cred();
Stephen Smalleyccb54472017-05-12 12:41:24 -04003165 struct inode *inode = d_backing_inode(dentry);
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003166 unsigned int ia_valid = iattr->ia_valid;
Eric Paris95dbf732012-04-04 13:45:34 -04003167 __u32 av = FILE__WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003169 /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
3170 if (ia_valid & ATTR_FORCE) {
3171 ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
3172 ATTR_FORCE);
3173 if (!ia_valid)
3174 return 0;
3175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003177 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
3178 ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
Eric Paris2875fa02011-04-28 16:04:24 -04003179 return dentry_has_perm(cred, dentry, FILE__SETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003181 if (selinux_policycap_openperm() &&
Stephen Smalleyccb54472017-05-12 12:41:24 -04003182 inode->i_sb->s_magic != SOCKFS_MAGIC &&
3183 (ia_valid & ATTR_SIZE) &&
3184 !(ia_valid & ATTR_FILE))
Eric Paris95dbf732012-04-04 13:45:34 -04003185 av |= FILE__OPEN;
3186
3187 return dentry_has_perm(cred, dentry, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188}
3189
Al Viro3f7036a2015-03-08 19:28:30 -04003190static int selinux_inode_getattr(const struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191{
Al Viro3f7036a2015-03-08 19:28:30 -04003192 return path_has_perm(current_cred(), path, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193}
3194
Stephen Smalleydb590002017-04-20 11:31:30 -04003195static bool has_cap_mac_admin(bool audit)
3196{
3197 const struct cred *cred = current_cred();
3198 int cap_audit = audit ? SECURITY_CAP_AUDIT : SECURITY_CAP_NOAUDIT;
3199
3200 if (cap_capable(cred, &init_user_ns, CAP_MAC_ADMIN, cap_audit))
3201 return false;
3202 if (cred_has_capability(cred, CAP_MAC_ADMIN, cap_audit, true))
3203 return false;
3204 return true;
3205}
3206
David Howells8f0cfa52008-04-29 00:59:41 -07003207static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
3208 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209{
David Howellsc6f493d2015-03-17 22:26:22 +00003210 struct inode *inode = d_backing_inode(dentry);
Paul Moore20cdef82016-04-04 14:14:42 -04003211 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 struct superblock_security_struct *sbsec;
Thomas Liu2bf49692009-07-14 12:14:09 -04003213 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11003214 u32 newsid, sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 int rc = 0;
3216
Eric W. Biederman6b240302017-10-02 09:38:20 -05003217 if (strcmp(name, XATTR_NAME_SELINUX)) {
3218 rc = cap_inode_setxattr(dentry, name, value, size, flags);
3219 if (rc)
3220 return rc;
3221
3222 /* Not an attribute we recognize, so just check the
3223 ordinary setattr permission. */
3224 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226
3227 sbsec = inode->i_sb->s_security;
Eric Paris12f348b2012-10-09 10:56:25 -04003228 if (!(sbsec->flags & SBLABEL_MNT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 return -EOPNOTSUPP;
3230
Serge E. Hallyn2e149672011-03-23 16:43:26 -07003231 if (!inode_owner_or_capable(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 return -EPERM;
3233
Eric Paris50c205f2012-04-04 15:01:43 -04003234 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04003235 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236
Paul Moore20cdef82016-04-04 14:14:42 -04003237 isec = backing_inode_security(dentry);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003238 rc = avc_has_perm(&selinux_state,
3239 sid, isec->sid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 FILE__RELABELFROM, &ad);
3241 if (rc)
3242 return rc;
3243
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003244 rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3245 GFP_KERNEL);
Stephen Smalley12b29f32008-05-07 13:03:20 -04003246 if (rc == -EINVAL) {
Stephen Smalleydb590002017-04-20 11:31:30 -04003247 if (!has_cap_mac_admin(true)) {
Eric Parisd6ea83e2012-04-04 13:45:49 -04003248 struct audit_buffer *ab;
3249 size_t audit_size;
Eric Parisd6ea83e2012-04-04 13:45:49 -04003250
3251 /* We strip a nul only if it is at the end, otherwise the
3252 * context contains a nul and we should audit that */
Al Viroe3fea3f2012-06-09 08:15:16 +01003253 if (value) {
Colin Ian Kingadd24372017-10-14 13:46:55 +01003254 const char *str = value;
3255
Al Viroe3fea3f2012-06-09 08:15:16 +01003256 if (str[size - 1] == '\0')
3257 audit_size = size - 1;
3258 else
3259 audit_size = size;
3260 } else {
Al Viroe3fea3f2012-06-09 08:15:16 +01003261 audit_size = 0;
3262 }
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04003263 ab = audit_log_start(audit_context(),
3264 GFP_ATOMIC, AUDIT_SELINUX_ERR);
Eric Parisd6ea83e2012-04-04 13:45:49 -04003265 audit_log_format(ab, "op=setxattr invalid_context=");
3266 audit_log_n_untrustedstring(ab, value, audit_size);
3267 audit_log_end(ab);
3268
Stephen Smalley12b29f32008-05-07 13:03:20 -04003269 return rc;
Eric Parisd6ea83e2012-04-04 13:45:49 -04003270 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003271 rc = security_context_to_sid_force(&selinux_state, value,
3272 size, &newsid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04003273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 if (rc)
3275 return rc;
3276
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003277 rc = avc_has_perm(&selinux_state,
3278 sid, newsid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 FILE__RELABELTO, &ad);
3280 if (rc)
3281 return rc;
3282
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003283 rc = security_validate_transition(&selinux_state, isec->sid, newsid,
3284 sid, isec->sclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 if (rc)
3286 return rc;
3287
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003288 return avc_has_perm(&selinux_state,
3289 newsid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 sbsec->sid,
3291 SECCLASS_FILESYSTEM,
3292 FILESYSTEM__ASSOCIATE,
3293 &ad);
3294}
3295
David Howells8f0cfa52008-04-29 00:59:41 -07003296static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
Eric Parisf5269712008-05-14 11:27:45 -04003297 const void *value, size_t size,
David Howells8f0cfa52008-04-29 00:59:41 -07003298 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299{
David Howellsc6f493d2015-03-17 22:26:22 +00003300 struct inode *inode = d_backing_inode(dentry);
Paul Moore20cdef82016-04-04 14:14:42 -04003301 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 u32 newsid;
3303 int rc;
3304
3305 if (strcmp(name, XATTR_NAME_SELINUX)) {
3306 /* Not an attribute we recognize, so nothing to do. */
3307 return;
3308 }
3309
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003310 rc = security_context_to_sid_force(&selinux_state, value, size,
3311 &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 if (rc) {
peter enderborgc103a912018-06-12 10:09:03 +02003313 pr_err("SELinux: unable to map context to SID"
Stephen Smalley12b29f32008-05-07 13:03:20 -04003314 "for (%s, %lu), rc=%d\n",
3315 inode->i_sb->s_id, inode->i_ino, -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 return;
3317 }
3318
Paul Moore20cdef82016-04-04 14:14:42 -04003319 isec = backing_inode_security(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003320 spin_lock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003321 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05003323 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003324 spin_unlock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003325
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 return;
3327}
3328
David Howells8f0cfa52008-04-29 00:59:41 -07003329static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330{
David Howells88e67f32008-11-14 10:39:21 +11003331 const struct cred *cred = current_cred();
3332
Eric Paris2875fa02011-04-28 16:04:24 -04003333 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334}
3335
Eric Paris828dfe12008-04-17 13:17:49 -04003336static int selinux_inode_listxattr(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337{
David Howells88e67f32008-11-14 10:39:21 +11003338 const struct cred *cred = current_cred();
3339
Eric Paris2875fa02011-04-28 16:04:24 -04003340 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341}
3342
David Howells8f0cfa52008-04-29 00:59:41 -07003343static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344{
Eric W. Biederman6b240302017-10-02 09:38:20 -05003345 if (strcmp(name, XATTR_NAME_SELINUX)) {
3346 int rc = cap_inode_removexattr(dentry, name);
3347 if (rc)
3348 return rc;
3349
3350 /* Not an attribute we recognize, so just check the
3351 ordinary setattr permission. */
3352 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354
3355 /* No one is allowed to remove a SELinux security label.
3356 You can change the label, but all data must be labeled. */
3357 return -EACCES;
3358}
3359
James Morrisd381d8a2005-10-30 14:59:22 -08003360/*
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003361 * Copy the inode security context value to the user.
James Morrisd381d8a2005-10-30 14:59:22 -08003362 *
3363 * Permission check is handled by selinux_inode_getxattr hook.
3364 */
Andreas Gruenbacherea861df2015-12-24 11:09:39 -05003365static int selinux_inode_getsecurity(struct inode *inode, const char *name, void **buffer, bool alloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366{
David P. Quigley42492592008-02-04 22:29:39 -08003367 u32 size;
3368 int error;
3369 char *context = NULL;
Paul Moore20cdef82016-04-04 14:14:42 -04003370 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00003372 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3373 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003375 /*
3376 * If the caller has CAP_MAC_ADMIN, then get the raw context
3377 * value even if it is not defined by current policy; otherwise,
3378 * use the in-core value under current policy.
3379 * Use the non-auditing forms of the permission checks since
3380 * getxattr may be called by unprivileged processes commonly
3381 * and lack of permission just means that we fall back to the
3382 * in-core context value, not a denial.
3383 */
Paul Moore20cdef82016-04-04 14:14:42 -04003384 isec = inode_security(inode);
Stephen Smalleydb590002017-04-20 11:31:30 -04003385 if (has_cap_mac_admin(false))
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003386 error = security_sid_to_context_force(&selinux_state,
3387 isec->sid, &context,
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003388 &size);
3389 else
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003390 error = security_sid_to_context(&selinux_state, isec->sid,
3391 &context, &size);
David P. Quigley42492592008-02-04 22:29:39 -08003392 if (error)
3393 return error;
3394 error = size;
3395 if (alloc) {
3396 *buffer = context;
3397 goto out_nofree;
3398 }
3399 kfree(context);
3400out_nofree:
3401 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402}
3403
3404static int selinux_inode_setsecurity(struct inode *inode, const char *name,
Eric Paris828dfe12008-04-17 13:17:49 -04003405 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406{
Paul Moore2c971652016-04-19 16:36:28 -04003407 struct inode_security_struct *isec = inode_security_novalidate(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 u32 newsid;
3409 int rc;
3410
3411 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3412 return -EOPNOTSUPP;
3413
3414 if (!value || !size)
3415 return -EACCES;
3416
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003417 rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3418 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 if (rc)
3420 return rc;
3421
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003422 spin_lock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003423 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05003425 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003426 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 return 0;
3428}
3429
3430static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
3431{
3432 const int len = sizeof(XATTR_NAME_SELINUX);
3433 if (buffer && len <= buffer_size)
3434 memcpy(buffer, XATTR_NAME_SELINUX, len);
3435 return len;
3436}
3437
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05003438static void selinux_inode_getsecid(struct inode *inode, u32 *secid)
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02003439{
Andreas Gruenbachere817c2f2016-02-18 12:04:08 +01003440 struct inode_security_struct *isec = inode_security_novalidate(inode);
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02003441 *secid = isec->sid;
3442}
3443
Vivek Goyal56909eb2016-07-13 10:44:48 -04003444static int selinux_inode_copy_up(struct dentry *src, struct cred **new)
3445{
3446 u32 sid;
3447 struct task_security_struct *tsec;
3448 struct cred *new_creds = *new;
3449
3450 if (new_creds == NULL) {
3451 new_creds = prepare_creds();
3452 if (!new_creds)
3453 return -ENOMEM;
3454 }
3455
3456 tsec = new_creds->security;
3457 /* Get label from overlay inode and set it in create_sid */
3458 selinux_inode_getsecid(d_inode(src), &sid);
3459 tsec->create_sid = sid;
3460 *new = new_creds;
3461 return 0;
3462}
3463
Vivek Goyal19472b62016-07-13 10:44:50 -04003464static int selinux_inode_copy_up_xattr(const char *name)
3465{
3466 /* The copy_up hook above sets the initial context on an inode, but we
3467 * don't then want to overwrite it by blindly copying all the lower
3468 * xattrs up. Instead, we have to filter out SELinux-related xattrs.
3469 */
3470 if (strcmp(name, XATTR_NAME_SELINUX) == 0)
3471 return 1; /* Discard */
3472 /*
3473 * Any other attribute apart from SELINUX is not claimed, supported
3474 * by selinux.
3475 */
3476 return -EOPNOTSUPP;
3477}
3478
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479/* file security operations */
3480
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003481static int selinux_revalidate_file_permission(struct file *file, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482{
David Howells88e67f32008-11-14 10:39:21 +11003483 const struct cred *cred = current_cred();
Al Viro496ad9a2013-01-23 17:07:38 -05003484 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
3487 if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
3488 mask |= MAY_APPEND;
3489
Paul Moore389fb8002009-03-27 17:10:34 -04003490 return file_has_perm(cred, file,
3491 file_mask_to_av(inode->i_mode, mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492}
3493
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003494static int selinux_file_permission(struct file *file, int mask)
3495{
Al Viro496ad9a2013-01-23 17:07:38 -05003496 struct inode *inode = file_inode(file);
Stephen Smalley20dda182009-06-22 14:54:53 -04003497 struct file_security_struct *fsec = file->f_security;
Andreas Gruenbacherb1973672016-01-05 23:12:33 +01003498 struct inode_security_struct *isec;
Stephen Smalley20dda182009-06-22 14:54:53 -04003499 u32 sid = current_sid();
3500
Paul Moore389fb8002009-03-27 17:10:34 -04003501 if (!mask)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003502 /* No permission to check. Existence test. */
3503 return 0;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003504
Andreas Gruenbacherb1973672016-01-05 23:12:33 +01003505 isec = inode_security(inode);
Stephen Smalley20dda182009-06-22 14:54:53 -04003506 if (sid == fsec->sid && fsec->isid == isec->sid &&
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003507 fsec->pseqno == avc_policy_seqno(&selinux_state))
Eric Paris83d49852012-04-04 13:45:40 -04003508 /* No change since file_open check. */
Stephen Smalley20dda182009-06-22 14:54:53 -04003509 return 0;
3510
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003511 return selinux_revalidate_file_permission(file, mask);
3512}
3513
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514static int selinux_file_alloc_security(struct file *file)
3515{
3516 return file_alloc_security(file);
3517}
3518
3519static void selinux_file_free_security(struct file *file)
3520{
3521 file_free_security(file);
3522}
3523
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003524/*
3525 * Check whether a task has the ioctl permission and cmd
3526 * operation to an inode.
3527 */
Geliang Tang1d2a1682015-10-21 17:44:27 -04003528static int ioctl_has_perm(const struct cred *cred, struct file *file,
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003529 u32 requested, u16 cmd)
3530{
3531 struct common_audit_data ad;
3532 struct file_security_struct *fsec = file->f_security;
3533 struct inode *inode = file_inode(file);
Paul Moore20cdef82016-04-04 14:14:42 -04003534 struct inode_security_struct *isec;
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003535 struct lsm_ioctlop_audit ioctl;
3536 u32 ssid = cred_sid(cred);
3537 int rc;
3538 u8 driver = cmd >> 8;
3539 u8 xperm = cmd & 0xff;
3540
3541 ad.type = LSM_AUDIT_DATA_IOCTL_OP;
3542 ad.u.op = &ioctl;
3543 ad.u.op->cmd = cmd;
3544 ad.u.op->path = file->f_path;
3545
3546 if (ssid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003547 rc = avc_has_perm(&selinux_state,
3548 ssid, fsec->sid,
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003549 SECCLASS_FD,
3550 FD__USE,
3551 &ad);
3552 if (rc)
3553 goto out;
3554 }
3555
3556 if (unlikely(IS_PRIVATE(inode)))
3557 return 0;
3558
Paul Moore20cdef82016-04-04 14:14:42 -04003559 isec = inode_security(inode);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003560 rc = avc_has_extended_perms(&selinux_state,
3561 ssid, isec->sid, isec->sclass,
3562 requested, driver, xperm, &ad);
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003563out:
3564 return rc;
3565}
3566
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567static int selinux_file_ioctl(struct file *file, unsigned int cmd,
3568 unsigned long arg)
3569{
David Howells88e67f32008-11-14 10:39:21 +11003570 const struct cred *cred = current_cred();
Eric Paris0b24dcb2011-02-25 15:39:20 -05003571 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572
Eric Paris0b24dcb2011-02-25 15:39:20 -05003573 switch (cmd) {
3574 case FIONREAD:
3575 /* fall through */
3576 case FIBMAP:
3577 /* fall through */
3578 case FIGETBSZ:
3579 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003580 case FS_IOC_GETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003581 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003582 case FS_IOC_GETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003583 error = file_has_perm(cred, file, FILE__GETATTR);
3584 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585
Al Viro2f99c362012-03-23 16:04:05 -04003586 case FS_IOC_SETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003587 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003588 case FS_IOC_SETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003589 error = file_has_perm(cred, file, FILE__SETATTR);
3590 break;
3591
3592 /* sys_ioctl() checks */
3593 case FIONBIO:
3594 /* fall through */
3595 case FIOASYNC:
3596 error = file_has_perm(cred, file, 0);
3597 break;
3598
3599 case KDSKBENT:
3600 case KDSKBSENT:
Eric Paris6a9de492012-01-03 12:25:14 -05003601 error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04003602 SECURITY_CAP_AUDIT, true);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003603 break;
3604
3605 /* default case assumes that the command will go
3606 * to the file's ioctl() function.
3607 */
3608 default:
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003609 error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003610 }
3611 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612}
3613
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003614static int default_noexec;
3615
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
3617{
David Howells88e67f32008-11-14 10:39:21 +11003618 const struct cred *cred = current_cred();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003619 u32 sid = cred_sid(cred);
David Howellsd84f4f92008-11-14 10:39:23 +11003620 int rc = 0;
David Howells88e67f32008-11-14 10:39:21 +11003621
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003622 if (default_noexec &&
Stephen Smalley892e8ca2015-07-10 09:40:59 -04003623 (prot & PROT_EXEC) && (!file || IS_PRIVATE(file_inode(file)) ||
3624 (!shared && (prot & PROT_WRITE)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 /*
3626 * We are making executable an anonymous mapping or a
3627 * private file mapping that will also be writable.
3628 * This has an additional check.
3629 */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003630 rc = avc_has_perm(&selinux_state,
3631 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003632 PROCESS__EXECMEM, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 if (rc)
David Howellsd84f4f92008-11-14 10:39:23 +11003634 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636
3637 if (file) {
3638 /* read access is always possible with a mapping */
3639 u32 av = FILE__READ;
3640
3641 /* write access only matters if the mapping is shared */
3642 if (shared && (prot & PROT_WRITE))
3643 av |= FILE__WRITE;
3644
3645 if (prot & PROT_EXEC)
3646 av |= FILE__EXECUTE;
3647
David Howells88e67f32008-11-14 10:39:21 +11003648 return file_has_perm(cred, file, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 }
David Howellsd84f4f92008-11-14 10:39:23 +11003650
3651error:
3652 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653}
3654
Al Viroe5467852012-05-30 13:30:51 -04003655static int selinux_mmap_addr(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07003657 int rc = 0;
Paul Moore98883bf2014-03-19 16:46:11 -04003658
3659 if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
3660 u32 sid = current_sid();
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003661 rc = avc_has_perm(&selinux_state,
3662 sid, sid, SECCLASS_MEMPROTECT,
Paul Moore98883bf2014-03-19 16:46:11 -04003663 MEMPROTECT__MMAP_ZERO, NULL);
3664 }
3665
3666 return rc;
Al Viroe5467852012-05-30 13:30:51 -04003667}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668
Al Viroe5467852012-05-30 13:30:51 -04003669static int selinux_mmap_file(struct file *file, unsigned long reqprot,
3670 unsigned long prot, unsigned long flags)
3671{
Stephen Smalley3ba4bf52017-05-05 09:14:48 -04003672 struct common_audit_data ad;
3673 int rc;
3674
3675 if (file) {
3676 ad.type = LSM_AUDIT_DATA_FILE;
3677 ad.u.file = file;
3678 rc = inode_has_perm(current_cred(), file_inode(file),
3679 FILE__MAP, &ad);
3680 if (rc)
3681 return rc;
3682 }
3683
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003684 if (selinux_state.checkreqprot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 prot = reqprot;
3686
3687 return file_map_prot_check(file, prot,
3688 (flags & MAP_TYPE) == MAP_SHARED);
3689}
3690
3691static int selinux_file_mprotect(struct vm_area_struct *vma,
3692 unsigned long reqprot,
3693 unsigned long prot)
3694{
David Howells88e67f32008-11-14 10:39:21 +11003695 const struct cred *cred = current_cred();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003696 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003698 if (selinux_state.checkreqprot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 prot = reqprot;
3700
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003701 if (default_noexec &&
3702 (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
James Morrisd541bbe2009-01-29 12:19:51 +11003703 int rc = 0;
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003704 if (vma->vm_start >= vma->vm_mm->start_brk &&
3705 vma->vm_end <= vma->vm_mm->brk) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003706 rc = avc_has_perm(&selinux_state,
3707 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003708 PROCESS__EXECHEAP, NULL);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003709 } else if (!vma->vm_file &&
Stephen Smalleyc2316db2016-04-08 13:55:03 -04003710 ((vma->vm_start <= vma->vm_mm->start_stack &&
3711 vma->vm_end >= vma->vm_mm->start_stack) ||
Andy Lutomirskid17af502016-09-30 10:58:58 -07003712 vma_is_stack_for_current(vma))) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003713 rc = avc_has_perm(&selinux_state,
3714 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003715 PROCESS__EXECSTACK, NULL);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003716 } else if (vma->vm_file && vma->anon_vma) {
3717 /*
3718 * We are making executable a file mapping that has
3719 * had some COW done. Since pages might have been
3720 * written, check ability to execute the possibly
3721 * modified content. This typically should only
3722 * occur for text relocations.
3723 */
David Howellsd84f4f92008-11-14 10:39:23 +11003724 rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003725 }
Lorenzo Hernandez García-Hierro6b992192005-06-25 14:54:34 -07003726 if (rc)
3727 return rc;
3728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729
3730 return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
3731}
3732
3733static int selinux_file_lock(struct file *file, unsigned int cmd)
3734{
David Howells88e67f32008-11-14 10:39:21 +11003735 const struct cred *cred = current_cred();
3736
3737 return file_has_perm(cred, file, FILE__LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738}
3739
3740static int selinux_file_fcntl(struct file *file, unsigned int cmd,
3741 unsigned long arg)
3742{
David Howells88e67f32008-11-14 10:39:21 +11003743 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 int err = 0;
3745
3746 switch (cmd) {
Eric Paris828dfe12008-04-17 13:17:49 -04003747 case F_SETFL:
Eric Paris828dfe12008-04-17 13:17:49 -04003748 if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
David Howells88e67f32008-11-14 10:39:21 +11003749 err = file_has_perm(cred, file, FILE__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003751 }
3752 /* fall through */
3753 case F_SETOWN:
3754 case F_SETSIG:
3755 case F_GETFL:
3756 case F_GETOWN:
3757 case F_GETSIG:
Cyrill Gorcunov1d151c32012-07-30 14:43:00 -07003758 case F_GETOWNER_UIDS:
Eric Paris828dfe12008-04-17 13:17:49 -04003759 /* Just check FD__USE permission */
David Howells88e67f32008-11-14 10:39:21 +11003760 err = file_has_perm(cred, file, 0);
Eric Paris828dfe12008-04-17 13:17:49 -04003761 break;
3762 case F_GETLK:
3763 case F_SETLK:
3764 case F_SETLKW:
Jeff Layton0d3f7a22014-04-22 08:23:58 -04003765 case F_OFD_GETLK:
3766 case F_OFD_SETLK:
3767 case F_OFD_SETLKW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768#if BITS_PER_LONG == 32
Eric Paris828dfe12008-04-17 13:17:49 -04003769 case F_GETLK64:
3770 case F_SETLK64:
3771 case F_SETLKW64:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772#endif
David Howells88e67f32008-11-14 10:39:21 +11003773 err = file_has_perm(cred, file, FILE__LOCK);
Eric Paris828dfe12008-04-17 13:17:49 -04003774 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 }
3776
3777 return err;
3778}
3779
Jeff Laytone0b93ed2014-08-22 11:27:32 -04003780static void selinux_file_set_fowner(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 struct file_security_struct *fsec;
3783
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 fsec = file->f_security;
David Howells275bb412008-11-14 10:39:19 +11003785 fsec->fown_sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786}
3787
3788static int selinux_file_send_sigiotask(struct task_struct *tsk,
3789 struct fown_struct *fown, int signum)
3790{
Eric Paris828dfe12008-04-17 13:17:49 -04003791 struct file *file;
Stephen Smalley65c90bc2009-05-04 15:43:18 -04003792 u32 sid = task_sid(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 struct file_security_struct *fsec;
3795
3796 /* struct fown_struct is never outside the context of a struct file */
Eric Paris828dfe12008-04-17 13:17:49 -04003797 file = container_of(fown, struct file, f_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 fsec = file->f_security;
3800
3801 if (!signum)
3802 perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
3803 else
3804 perm = signal_to_av(signum);
3805
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003806 return avc_has_perm(&selinux_state,
3807 fsec->fown_sid, sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 SECCLASS_PROCESS, perm, NULL);
3809}
3810
3811static int selinux_file_receive(struct file *file)
3812{
David Howells88e67f32008-11-14 10:39:21 +11003813 const struct cred *cred = current_cred();
3814
3815 return file_has_perm(cred, file, file_to_av(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816}
3817
Al Viro94817692018-07-10 14:13:18 -04003818static int selinux_file_open(struct file *file)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003819{
3820 struct file_security_struct *fsec;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003821 struct inode_security_struct *isec;
David Howellsd84f4f92008-11-14 10:39:23 +11003822
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003823 fsec = file->f_security;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05003824 isec = inode_security(file_inode(file));
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003825 /*
3826 * Save inode label and policy sequence number
3827 * at open-time so that selinux_file_permission
3828 * can determine whether revalidation is necessary.
3829 * Task label is already saved in the file security
3830 * struct as its SID.
3831 */
3832 fsec->isid = isec->sid;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003833 fsec->pseqno = avc_policy_seqno(&selinux_state);
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003834 /*
3835 * Since the inode label or policy seqno may have changed
3836 * between the selinux_inode_permission check and the saving
3837 * of state above, recheck that access is still permitted.
3838 * Otherwise, access might never be revalidated against the
3839 * new inode label or new policy.
3840 * This check is not redundant - do not remove.
3841 */
Al Viro94817692018-07-10 14:13:18 -04003842 return file_path_has_perm(file->f_cred, file, open_file_to_av(file));
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003843}
3844
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845/* task security operations */
3846
Tetsuo Handaa79be232017-03-28 23:08:45 +09003847static int selinux_task_alloc(struct task_struct *task,
3848 unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003850 u32 sid = current_sid();
3851
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003852 return avc_has_perm(&selinux_state,
3853 sid, sid, SECCLASS_PROCESS, PROCESS__FORK, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854}
3855
David Howellsf1752ee2008-11-14 10:39:17 +11003856/*
David Howellsee18d642009-09-02 09:14:21 +01003857 * allocate the SELinux part of blank credentials
3858 */
3859static int selinux_cred_alloc_blank(struct cred *cred, gfp_t gfp)
3860{
3861 struct task_security_struct *tsec;
3862
3863 tsec = kzalloc(sizeof(struct task_security_struct), gfp);
3864 if (!tsec)
3865 return -ENOMEM;
3866
3867 cred->security = tsec;
3868 return 0;
3869}
3870
3871/*
David Howellsf1752ee2008-11-14 10:39:17 +11003872 * detach and free the LSM part of a set of credentials
3873 */
3874static void selinux_cred_free(struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875{
David Howellsf1752ee2008-11-14 10:39:17 +11003876 struct task_security_struct *tsec = cred->security;
David Howellse0e81732009-09-02 09:13:40 +01003877
Tetsuo Handa2edeaa32011-02-07 13:36:10 +00003878 /*
3879 * cred->security == NULL if security_cred_alloc_blank() or
3880 * security_prepare_creds() returned an error.
3881 */
3882 BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE);
David Howellse0e81732009-09-02 09:13:40 +01003883 cred->security = (void *) 0x7UL;
David Howellsf1752ee2008-11-14 10:39:17 +11003884 kfree(tsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885}
3886
David Howellsd84f4f92008-11-14 10:39:23 +11003887/*
3888 * prepare a new set of credentials for modification
3889 */
3890static int selinux_cred_prepare(struct cred *new, const struct cred *old,
3891 gfp_t gfp)
3892{
3893 const struct task_security_struct *old_tsec;
3894 struct task_security_struct *tsec;
3895
3896 old_tsec = old->security;
3897
3898 tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp);
3899 if (!tsec)
3900 return -ENOMEM;
3901
3902 new->security = tsec;
3903 return 0;
3904}
3905
3906/*
David Howellsee18d642009-09-02 09:14:21 +01003907 * transfer the SELinux data to a blank set of creds
3908 */
3909static void selinux_cred_transfer(struct cred *new, const struct cred *old)
3910{
3911 const struct task_security_struct *old_tsec = old->security;
3912 struct task_security_struct *tsec = new->security;
3913
3914 *tsec = *old_tsec;
3915}
3916
Matthew Garrett3ec30112018-01-08 13:36:19 -08003917static void selinux_cred_getsecid(const struct cred *c, u32 *secid)
3918{
3919 *secid = cred_sid(c);
3920}
3921
David Howellsee18d642009-09-02 09:14:21 +01003922/*
David Howells3a3b7ce2008-11-14 10:39:28 +11003923 * set the security data for a kernel service
3924 * - all the creation contexts are set to unlabelled
3925 */
3926static int selinux_kernel_act_as(struct cred *new, u32 secid)
3927{
3928 struct task_security_struct *tsec = new->security;
3929 u32 sid = current_sid();
3930 int ret;
3931
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003932 ret = avc_has_perm(&selinux_state,
3933 sid, secid,
David Howells3a3b7ce2008-11-14 10:39:28 +11003934 SECCLASS_KERNEL_SERVICE,
3935 KERNEL_SERVICE__USE_AS_OVERRIDE,
3936 NULL);
3937 if (ret == 0) {
3938 tsec->sid = secid;
3939 tsec->create_sid = 0;
3940 tsec->keycreate_sid = 0;
3941 tsec->sockcreate_sid = 0;
3942 }
3943 return ret;
3944}
3945
3946/*
3947 * set the file creation context in a security record to the same as the
3948 * objective context of the specified inode
3949 */
3950static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
3951{
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05003952 struct inode_security_struct *isec = inode_security(inode);
David Howells3a3b7ce2008-11-14 10:39:28 +11003953 struct task_security_struct *tsec = new->security;
3954 u32 sid = current_sid();
3955 int ret;
3956
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003957 ret = avc_has_perm(&selinux_state,
3958 sid, isec->sid,
David Howells3a3b7ce2008-11-14 10:39:28 +11003959 SECCLASS_KERNEL_SERVICE,
3960 KERNEL_SERVICE__CREATE_FILES_AS,
3961 NULL);
3962
3963 if (ret == 0)
3964 tsec->create_sid = isec->sid;
David Howellsef574712010-02-26 01:56:16 +00003965 return ret;
David Howells3a3b7ce2008-11-14 10:39:28 +11003966}
3967
Eric Parisdd8dbf22009-11-03 16:35:32 +11003968static int selinux_kernel_module_request(char *kmod_name)
Eric Paris25354c42009-08-13 09:45:03 -04003969{
Eric Parisdd8dbf22009-11-03 16:35:32 +11003970 struct common_audit_data ad;
3971
Eric Paris50c205f2012-04-04 15:01:43 -04003972 ad.type = LSM_AUDIT_DATA_KMOD;
Eric Parisdd8dbf22009-11-03 16:35:32 +11003973 ad.u.kmod_name = kmod_name;
3974
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003975 return avc_has_perm(&selinux_state,
3976 current_sid(), SECINITSID_KERNEL, SECCLASS_SYSTEM,
Eric Parisdd8dbf22009-11-03 16:35:32 +11003977 SYSTEM__MODULE_REQUEST, &ad);
Eric Paris25354c42009-08-13 09:45:03 -04003978}
3979
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003980static int selinux_kernel_module_from_file(struct file *file)
3981{
3982 struct common_audit_data ad;
3983 struct inode_security_struct *isec;
3984 struct file_security_struct *fsec;
3985 u32 sid = current_sid();
3986 int rc;
3987
3988 /* init_module */
3989 if (file == NULL)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003990 return avc_has_perm(&selinux_state,
3991 sid, sid, SECCLASS_SYSTEM,
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003992 SYSTEM__MODULE_LOAD, NULL);
3993
3994 /* finit_module */
Paul Moore20cdef82016-04-04 14:14:42 -04003995
Vivek Goyal43af5de2016-09-09 11:37:49 -04003996 ad.type = LSM_AUDIT_DATA_FILE;
3997 ad.u.file = file;
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003998
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003999 fsec = file->f_security;
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07004000 if (sid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004001 rc = avc_has_perm(&selinux_state,
4002 sid, fsec->sid, SECCLASS_FD, FD__USE, &ad);
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07004003 if (rc)
4004 return rc;
4005 }
4006
Paul Moore20cdef82016-04-04 14:14:42 -04004007 isec = inode_security(file_inode(file));
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004008 return avc_has_perm(&selinux_state,
4009 sid, isec->sid, SECCLASS_SYSTEM,
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07004010 SYSTEM__MODULE_LOAD, &ad);
4011}
4012
4013static int selinux_kernel_read_file(struct file *file,
4014 enum kernel_read_file_id id)
4015{
4016 int rc = 0;
4017
4018 switch (id) {
4019 case READING_MODULE:
4020 rc = selinux_kernel_module_from_file(file);
4021 break;
4022 default:
4023 break;
4024 }
4025
4026 return rc;
4027}
4028
Mimi Zoharc77b8cd2018-07-13 14:06:02 -04004029static int selinux_kernel_load_data(enum kernel_load_data_id id)
4030{
4031 int rc = 0;
4032
4033 switch (id) {
4034 case LOADING_MODULE:
4035 rc = selinux_kernel_module_from_file(NULL);
4036 default:
4037 break;
4038 }
4039
4040 return rc;
4041}
4042
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
4044{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004045 return avc_has_perm(&selinux_state,
4046 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004047 PROCESS__SETPGID, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048}
4049
4050static int selinux_task_getpgid(struct task_struct *p)
4051{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004052 return avc_has_perm(&selinux_state,
4053 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004054 PROCESS__GETPGID, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055}
4056
4057static int selinux_task_getsid(struct task_struct *p)
4058{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004059 return avc_has_perm(&selinux_state,
4060 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004061 PROCESS__GETSESSION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062}
4063
David Quigleyf9008e4c2006-06-30 01:55:46 -07004064static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
4065{
David Howells275bb412008-11-14 10:39:19 +11004066 *secid = task_sid(p);
David Quigleyf9008e4c2006-06-30 01:55:46 -07004067}
4068
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069static int selinux_task_setnice(struct task_struct *p, int nice)
4070{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004071 return avc_has_perm(&selinux_state,
4072 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004073 PROCESS__SETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074}
4075
James Morris03e68062006-06-23 02:03:58 -07004076static int selinux_task_setioprio(struct task_struct *p, int ioprio)
4077{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004078 return avc_has_perm(&selinux_state,
4079 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004080 PROCESS__SETSCHED, NULL);
James Morris03e68062006-06-23 02:03:58 -07004081}
4082
David Quigleya1836a42006-06-30 01:55:49 -07004083static int selinux_task_getioprio(struct task_struct *p)
4084{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004085 return avc_has_perm(&selinux_state,
4086 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004087 PROCESS__GETSCHED, NULL);
David Quigleya1836a42006-06-30 01:55:49 -07004088}
4089
Corentin LABBE42985552017-10-04 20:32:18 +02004090static int selinux_task_prlimit(const struct cred *cred, const struct cred *tcred,
4091 unsigned int flags)
Stephen Smalley791ec492017-02-17 07:57:00 -05004092{
4093 u32 av = 0;
4094
Stephen Smalley84e68852017-02-28 09:35:08 -05004095 if (!flags)
4096 return 0;
Stephen Smalley791ec492017-02-17 07:57:00 -05004097 if (flags & LSM_PRLIMIT_WRITE)
4098 av |= PROCESS__SETRLIMIT;
4099 if (flags & LSM_PRLIMIT_READ)
4100 av |= PROCESS__GETRLIMIT;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004101 return avc_has_perm(&selinux_state,
4102 cred_sid(cred), cred_sid(tcred),
Stephen Smalley791ec492017-02-17 07:57:00 -05004103 SECCLASS_PROCESS, av, NULL);
4104}
4105
Jiri Slaby8fd00b42009-08-26 18:41:16 +02004106static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
4107 struct rlimit *new_rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108{
Jiri Slaby8fd00b42009-08-26 18:41:16 +02004109 struct rlimit *old_rlim = p->signal->rlim + resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110
4111 /* Control the ability to change the hard limit (whether
4112 lowering or raising it), so that the hard limit can
4113 later be used as a safe reset point for the soft limit
David Howellsd84f4f92008-11-14 10:39:23 +11004114 upon context transitions. See selinux_bprm_committing_creds. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 if (old_rlim->rlim_max != new_rlim->rlim_max)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004116 return avc_has_perm(&selinux_state,
4117 current_sid(), task_sid(p),
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004118 SECCLASS_PROCESS, PROCESS__SETRLIMIT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119
4120 return 0;
4121}
4122
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09004123static int selinux_task_setscheduler(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004125 return avc_has_perm(&selinux_state,
4126 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004127 PROCESS__SETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128}
4129
4130static int selinux_task_getscheduler(struct task_struct *p)
4131{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004132 return avc_has_perm(&selinux_state,
4133 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004134 PROCESS__GETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135}
4136
David Quigley35601542006-06-23 02:04:01 -07004137static int selinux_task_movememory(struct task_struct *p)
4138{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004139 return avc_has_perm(&selinux_state,
4140 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004141 PROCESS__SETSCHED, NULL);
David Quigley35601542006-06-23 02:04:01 -07004142}
4143
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02004144static int selinux_task_kill(struct task_struct *p, struct kernel_siginfo *info,
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004145 int sig, const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146{
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004147 u32 secid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 if (!sig)
4151 perm = PROCESS__SIGNULL; /* null signal; existence test */
4152 else
4153 perm = signal_to_av(sig);
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004154 if (!cred)
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004155 secid = current_sid();
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004156 else
4157 secid = cred_sid(cred);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004158 return avc_has_perm(&selinux_state,
4159 secid, task_sid(p), SECCLASS_PROCESS, perm, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160}
4161
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162static void selinux_task_to_inode(struct task_struct *p,
4163 struct inode *inode)
4164{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 struct inode_security_struct *isec = inode->i_security;
David Howells275bb412008-11-14 10:39:19 +11004166 u32 sid = task_sid(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004168 spin_lock(&isec->lock);
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01004169 isec->sclass = inode_mode_to_security_class(inode->i_mode);
David Howells275bb412008-11-14 10:39:19 +11004170 isec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004171 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004172 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173}
4174
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004176static int selinux_parse_skb_ipv4(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04004177 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178{
4179 int offset, ihlen, ret = -EINVAL;
4180 struct iphdr _iph, *ih;
4181
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03004182 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
4184 if (ih == NULL)
4185 goto out;
4186
4187 ihlen = ih->ihl * 4;
4188 if (ihlen < sizeof(_iph))
4189 goto out;
4190
Eric Paris48c62af2012-04-02 13:15:44 -04004191 ad->u.net->v4info.saddr = ih->saddr;
4192 ad->u.net->v4info.daddr = ih->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 ret = 0;
4194
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004195 if (proto)
4196 *proto = ih->protocol;
4197
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 switch (ih->protocol) {
Eric Paris828dfe12008-04-17 13:17:49 -04004199 case IPPROTO_TCP: {
4200 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201
Eric Paris828dfe12008-04-17 13:17:49 -04004202 if (ntohs(ih->frag_off) & IP_OFFSET)
4203 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204
4205 offset += ihlen;
4206 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4207 if (th == NULL)
4208 break;
4209
Eric Paris48c62af2012-04-02 13:15:44 -04004210 ad->u.net->sport = th->source;
4211 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214
Eric Paris828dfe12008-04-17 13:17:49 -04004215 case IPPROTO_UDP: {
4216 struct udphdr _udph, *uh;
4217
4218 if (ntohs(ih->frag_off) & IP_OFFSET)
4219 break;
4220
4221 offset += ihlen;
4222 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4223 if (uh == NULL)
4224 break;
4225
Eric Paris48c62af2012-04-02 13:15:44 -04004226 ad->u.net->sport = uh->source;
4227 ad->u.net->dport = uh->dest;
Eric Paris828dfe12008-04-17 13:17:49 -04004228 break;
4229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230
James Morris2ee92d42006-11-13 16:09:01 -08004231 case IPPROTO_DCCP: {
4232 struct dccp_hdr _dccph, *dh;
4233
4234 if (ntohs(ih->frag_off) & IP_OFFSET)
4235 break;
4236
4237 offset += ihlen;
4238 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4239 if (dh == NULL)
4240 break;
4241
Eric Paris48c62af2012-04-02 13:15:44 -04004242 ad->u.net->sport = dh->dccph_sport;
4243 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08004244 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004245 }
James Morris2ee92d42006-11-13 16:09:01 -08004246
Richard Hainesd4529302018-02-13 20:57:18 +00004247#if IS_ENABLED(CONFIG_IP_SCTP)
4248 case IPPROTO_SCTP: {
4249 struct sctphdr _sctph, *sh;
4250
4251 if (ntohs(ih->frag_off) & IP_OFFSET)
4252 break;
4253
4254 offset += ihlen;
4255 sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4256 if (sh == NULL)
4257 break;
4258
4259 ad->u.net->sport = sh->source;
4260 ad->u.net->dport = sh->dest;
4261 break;
4262 }
4263#endif
Eric Paris828dfe12008-04-17 13:17:49 -04004264 default:
4265 break;
4266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267out:
4268 return ret;
4269}
4270
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04004271#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272
4273/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004274static int selinux_parse_skb_ipv6(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04004275 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276{
4277 u8 nexthdr;
4278 int ret = -EINVAL, offset;
4279 struct ipv6hdr _ipv6h, *ip6;
Jesse Gross75f28112011-11-30 17:05:51 -08004280 __be16 frag_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03004282 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
4284 if (ip6 == NULL)
4285 goto out;
4286
Eric Paris48c62af2012-04-02 13:15:44 -04004287 ad->u.net->v6info.saddr = ip6->saddr;
4288 ad->u.net->v6info.daddr = ip6->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 ret = 0;
4290
4291 nexthdr = ip6->nexthdr;
4292 offset += sizeof(_ipv6h);
Jesse Gross75f28112011-11-30 17:05:51 -08004293 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 if (offset < 0)
4295 goto out;
4296
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004297 if (proto)
4298 *proto = nexthdr;
4299
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 switch (nexthdr) {
4301 case IPPROTO_TCP: {
Eric Paris828dfe12008-04-17 13:17:49 -04004302 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303
4304 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4305 if (th == NULL)
4306 break;
4307
Eric Paris48c62af2012-04-02 13:15:44 -04004308 ad->u.net->sport = th->source;
4309 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 break;
4311 }
4312
4313 case IPPROTO_UDP: {
4314 struct udphdr _udph, *uh;
4315
4316 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4317 if (uh == NULL)
4318 break;
4319
Eric Paris48c62af2012-04-02 13:15:44 -04004320 ad->u.net->sport = uh->source;
4321 ad->u.net->dport = uh->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 break;
4323 }
4324
James Morris2ee92d42006-11-13 16:09:01 -08004325 case IPPROTO_DCCP: {
4326 struct dccp_hdr _dccph, *dh;
4327
4328 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4329 if (dh == NULL)
4330 break;
4331
Eric Paris48c62af2012-04-02 13:15:44 -04004332 ad->u.net->sport = dh->dccph_sport;
4333 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08004334 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004335 }
James Morris2ee92d42006-11-13 16:09:01 -08004336
Richard Hainesd4529302018-02-13 20:57:18 +00004337#if IS_ENABLED(CONFIG_IP_SCTP)
4338 case IPPROTO_SCTP: {
4339 struct sctphdr _sctph, *sh;
4340
4341 sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4342 if (sh == NULL)
4343 break;
4344
4345 ad->u.net->sport = sh->source;
4346 ad->u.net->dport = sh->dest;
4347 break;
4348 }
4349#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 /* includes fragments */
4351 default:
4352 break;
4353 }
4354out:
4355 return ret;
4356}
4357
4358#endif /* IPV6 */
4359
Thomas Liu2bf49692009-07-14 12:14:09 -04004360static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
David Howellscf9481e2008-07-27 21:31:07 +10004361 char **_addrp, int src, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362{
David Howellscf9481e2008-07-27 21:31:07 +10004363 char *addrp;
4364 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365
Eric Paris48c62af2012-04-02 13:15:44 -04004366 switch (ad->u.net->family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367 case PF_INET:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004368 ret = selinux_parse_skb_ipv4(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10004369 if (ret)
4370 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04004371 addrp = (char *)(src ? &ad->u.net->v4info.saddr :
4372 &ad->u.net->v4info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10004373 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04004375#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 case PF_INET6:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004377 ret = selinux_parse_skb_ipv6(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10004378 if (ret)
4379 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04004380 addrp = (char *)(src ? &ad->u.net->v6info.saddr :
4381 &ad->u.net->v6info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10004382 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383#endif /* IPV6 */
4384 default:
David Howellscf9481e2008-07-27 21:31:07 +10004385 addrp = NULL;
4386 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 }
4388
David Howellscf9481e2008-07-27 21:31:07 +10004389parse_error:
peter enderborgc103a912018-06-12 10:09:03 +02004390 pr_warn(
David Howellscf9481e2008-07-27 21:31:07 +10004391 "SELinux: failure in selinux_parse_skb(),"
4392 " unable to parse packet\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 return ret;
David Howellscf9481e2008-07-27 21:31:07 +10004394
4395okay:
4396 if (_addrp)
4397 *_addrp = addrp;
4398 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399}
4400
Paul Moore4f6a9932007-03-01 14:35:22 -05004401/**
Paul Moore220deb92008-01-29 08:38:23 -05004402 * selinux_skb_peerlbl_sid - Determine the peer label of a packet
Paul Moore4f6a9932007-03-01 14:35:22 -05004403 * @skb: the packet
Paul Moore75e22912008-01-29 08:38:04 -05004404 * @family: protocol family
Paul Moore220deb92008-01-29 08:38:23 -05004405 * @sid: the packet's peer label SID
Paul Moore4f6a9932007-03-01 14:35:22 -05004406 *
4407 * Description:
Paul Moore220deb92008-01-29 08:38:23 -05004408 * Check the various different forms of network peer labeling and determine
4409 * the peer label/SID for the packet; most of the magic actually occurs in
4410 * the security server function security_net_peersid_cmp(). The function
4411 * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
4412 * or -EACCES if @sid is invalid due to inconsistencies with the different
4413 * peer labels.
Paul Moore4f6a9932007-03-01 14:35:22 -05004414 *
4415 */
Paul Moore220deb92008-01-29 08:38:23 -05004416static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
Paul Moore4f6a9932007-03-01 14:35:22 -05004417{
Paul Moore71f1cb02008-01-29 08:51:16 -05004418 int err;
Paul Moore4f6a9932007-03-01 14:35:22 -05004419 u32 xfrm_sid;
4420 u32 nlbl_sid;
Paul Moore220deb92008-01-29 08:38:23 -05004421 u32 nlbl_type;
Paul Moore4f6a9932007-03-01 14:35:22 -05004422
Paul Moore817eff72013-12-10 14:57:54 -05004423 err = selinux_xfrm_skb_sid(skb, &xfrm_sid);
Paul Moorebed4d7e2013-07-23 17:38:40 -04004424 if (unlikely(err))
4425 return -EACCES;
4426 err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
4427 if (unlikely(err))
4428 return -EACCES;
Paul Moore220deb92008-01-29 08:38:23 -05004429
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004430 err = security_net_peersid_resolve(&selinux_state, nlbl_sid,
4431 nlbl_type, xfrm_sid, sid);
Paul Moore71f1cb02008-01-29 08:51:16 -05004432 if (unlikely(err)) {
peter enderborgc103a912018-06-12 10:09:03 +02004433 pr_warn(
Paul Moore71f1cb02008-01-29 08:51:16 -05004434 "SELinux: failure in selinux_skb_peerlbl_sid(),"
4435 " unable to determine packet's peer label\n");
Paul Moore220deb92008-01-29 08:38:23 -05004436 return -EACCES;
Paul Moore71f1cb02008-01-29 08:51:16 -05004437 }
Paul Moore220deb92008-01-29 08:38:23 -05004438
4439 return 0;
Paul Moore4f6a9932007-03-01 14:35:22 -05004440}
4441
Paul Moore446b8022013-12-04 16:10:51 -05004442/**
4443 * selinux_conn_sid - Determine the child socket label for a connection
4444 * @sk_sid: the parent socket's SID
4445 * @skb_sid: the packet's SID
4446 * @conn_sid: the resulting connection SID
4447 *
4448 * If @skb_sid is valid then the user:role:type information from @sk_sid is
4449 * combined with the MLS information from @skb_sid in order to create
4450 * @conn_sid. If @skb_sid is not valid then then @conn_sid is simply a copy
4451 * of @sk_sid. Returns zero on success, negative values on failure.
4452 *
4453 */
4454static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid)
4455{
4456 int err = 0;
4457
4458 if (skb_sid != SECSID_NULL)
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004459 err = security_sid_mls_copy(&selinux_state, sk_sid, skb_sid,
4460 conn_sid);
Paul Moore446b8022013-12-04 16:10:51 -05004461 else
4462 *conn_sid = sk_sid;
4463
4464 return err;
4465}
4466
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467/* socket security operations */
Paul Moored4f2d972010-04-22 14:46:18 -04004468
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004469static int socket_sockcreate_sid(const struct task_security_struct *tsec,
4470 u16 secclass, u32 *socksid)
Paul Moored4f2d972010-04-22 14:46:18 -04004471{
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004472 if (tsec->sockcreate_sid > SECSID_NULL) {
4473 *socksid = tsec->sockcreate_sid;
4474 return 0;
4475 }
4476
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004477 return security_transition_sid(&selinux_state, tsec->sid, tsec->sid,
4478 secclass, NULL, socksid);
Paul Moored4f2d972010-04-22 14:46:18 -04004479}
4480
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004481static int sock_has_perm(struct sock *sk, u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482{
Paul Moore253bfae2010-04-22 14:46:19 -04004483 struct sk_security_struct *sksec = sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004484 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004485 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486
Paul Moore253bfae2010-04-22 14:46:19 -04004487 if (sksec->sid == SECINITSID_KERNEL)
4488 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489
Eric Paris50c205f2012-04-04 15:01:43 -04004490 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004491 ad.u.net = &net;
4492 ad.u.net->sk = sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004494 return avc_has_perm(&selinux_state,
4495 current_sid(), sksec->sid, sksec->sclass, perms,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004496 &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497}
4498
4499static int selinux_socket_create(int family, int type,
4500 int protocol, int kern)
4501{
Paul Moore5fb49872010-04-22 14:46:19 -04004502 const struct task_security_struct *tsec = current_security();
Paul Moored4f2d972010-04-22 14:46:18 -04004503 u32 newsid;
David Howells275bb412008-11-14 10:39:19 +11004504 u16 secclass;
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004505 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506
4507 if (kern)
Paul Moored4f2d972010-04-22 14:46:18 -04004508 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509
David Howells275bb412008-11-14 10:39:19 +11004510 secclass = socket_type_to_security_class(family, type, protocol);
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004511 rc = socket_sockcreate_sid(tsec, secclass, &newsid);
4512 if (rc)
4513 return rc;
4514
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004515 return avc_has_perm(&selinux_state,
4516 tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517}
4518
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07004519static int selinux_socket_post_create(struct socket *sock, int family,
4520 int type, int protocol, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521{
Paul Moore5fb49872010-04-22 14:46:19 -04004522 const struct task_security_struct *tsec = current_security();
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05004523 struct inode_security_struct *isec = inode_security_novalidate(SOCK_INODE(sock));
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004524 struct sk_security_struct *sksec;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004525 u16 sclass = socket_type_to_security_class(family, type, protocol);
4526 u32 sid = SECINITSID_KERNEL;
David Howells275bb412008-11-14 10:39:19 +11004527 int err = 0;
4528
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004529 if (!kern) {
4530 err = socket_sockcreate_sid(tsec, sclass, &sid);
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004531 if (err)
4532 return err;
4533 }
David Howells275bb412008-11-14 10:39:19 +11004534
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004535 isec->sclass = sclass;
4536 isec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004537 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004539 if (sock->sk) {
4540 sksec = sock->sk->sk_security;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004541 sksec->sclass = sclass;
4542 sksec->sid = sid;
Richard Hainesd4529302018-02-13 20:57:18 +00004543 /* Allows detection of the first association on this socket */
4544 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4545 sksec->sctp_assoc_state = SCTP_ASSOC_UNSET;
4546
Paul Moore389fb8002009-03-27 17:10:34 -04004547 err = selinux_netlbl_socket_post_create(sock->sk, family);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004548 }
4549
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07004550 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551}
4552
David Herrmann0b811db2018-05-04 16:28:21 +02004553static int selinux_socket_socketpair(struct socket *socka,
4554 struct socket *sockb)
4555{
4556 struct sk_security_struct *sksec_a = socka->sk->sk_security;
4557 struct sk_security_struct *sksec_b = sockb->sk->sk_security;
4558
4559 sksec_a->peer_sid = sksec_b->sid;
4560 sksec_b->peer_sid = sksec_a->sid;
4561
4562 return 0;
4563}
4564
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565/* Range of port numbers used to automatically bind.
4566 Need to determine whether we should perform a name_bind
4567 permission check between the socket and the port number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568
4569static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
4570{
Paul Moore253bfae2010-04-22 14:46:19 -04004571 struct sock *sk = sock->sk;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004572 struct sk_security_struct *sksec = sk->sk_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573 u16 family;
4574 int err;
4575
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004576 err = sock_has_perm(sk, SOCKET__BIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 if (err)
4578 goto out;
4579
Richard Hainesd4529302018-02-13 20:57:18 +00004580 /* If PF_INET or PF_INET6, check name_bind permission for the port. */
Paul Moore253bfae2010-04-22 14:46:19 -04004581 family = sk->sk_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 if (family == PF_INET || family == PF_INET6) {
4583 char *addrp;
Thomas Liu2bf49692009-07-14 12:14:09 -04004584 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004585 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 struct sockaddr_in *addr4 = NULL;
4587 struct sockaddr_in6 *addr6 = NULL;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004588 u16 family_sa = address->sa_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 unsigned short snum;
James Morrise399f982008-06-12 01:39:58 +10004590 u32 sid, node_perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591
Richard Hainesd4529302018-02-13 20:57:18 +00004592 /*
4593 * sctp_bindx(3) calls via selinux_sctp_bind_connect()
4594 * that validates multiple binding addresses. Because of this
4595 * need to check address->sa_family as it is possible to have
4596 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4597 */
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004598 switch (family_sa) {
4599 case AF_UNSPEC:
Richard Haines68741a8a2018-03-02 19:54:34 +00004600 case AF_INET:
4601 if (addrlen < sizeof(struct sockaddr_in))
4602 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 addr4 = (struct sockaddr_in *)address;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004604 if (family_sa == AF_UNSPEC) {
4605 /* see __inet_bind(), we only want to allow
4606 * AF_UNSPEC if the address is INADDR_ANY
4607 */
4608 if (addr4->sin_addr.s_addr != htonl(INADDR_ANY))
4609 goto err_af;
4610 family_sa = AF_INET;
4611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 snum = ntohs(addr4->sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 addrp = (char *)&addr4->sin_addr.s_addr;
Richard Haines68741a8a2018-03-02 19:54:34 +00004614 break;
4615 case AF_INET6:
4616 if (addrlen < SIN6_LEN_RFC2133)
4617 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 addr6 = (struct sockaddr_in6 *)address;
4619 snum = ntohs(addr6->sin6_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 addrp = (char *)&addr6->sin6_addr.s6_addr;
Richard Haines68741a8a2018-03-02 19:54:34 +00004621 break;
4622 default:
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004623 goto err_af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 }
4625
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004626 ad.type = LSM_AUDIT_DATA_NET;
4627 ad.u.net = &net;
4628 ad.u.net->sport = htons(snum);
4629 ad.u.net->family = family_sa;
4630
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004631 if (snum) {
4632 int low, high;
4633
Eric W. Biederman0bbf87d2013-09-28 14:10:59 -07004634 inet_get_local_port_range(sock_net(sk), &low, &high);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004635
Krister Johansen4548b682017-01-20 17:49:11 -08004636 if (snum < max(inet_prot_sock(sock_net(sk)), low) ||
4637 snum > high) {
Paul Moore3e1121722008-04-10 10:48:14 -04004638 err = sel_netport_sid(sk->sk_protocol,
4639 snum, &sid);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004640 if (err)
4641 goto out;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004642 err = avc_has_perm(&selinux_state,
4643 sksec->sid, sid,
Paul Moore253bfae2010-04-22 14:46:19 -04004644 sksec->sclass,
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004645 SOCKET__NAME_BIND, &ad);
4646 if (err)
4647 goto out;
4648 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 }
Eric Paris828dfe12008-04-17 13:17:49 -04004650
Paul Moore253bfae2010-04-22 14:46:19 -04004651 switch (sksec->sclass) {
James Morris13402582005-09-30 14:24:34 -04004652 case SECCLASS_TCP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 node_perm = TCP_SOCKET__NODE_BIND;
4654 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004655
James Morris13402582005-09-30 14:24:34 -04004656 case SECCLASS_UDP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 node_perm = UDP_SOCKET__NODE_BIND;
4658 break;
James Morris2ee92d42006-11-13 16:09:01 -08004659
4660 case SECCLASS_DCCP_SOCKET:
4661 node_perm = DCCP_SOCKET__NODE_BIND;
4662 break;
4663
Richard Hainesd4529302018-02-13 20:57:18 +00004664 case SECCLASS_SCTP_SOCKET:
4665 node_perm = SCTP_SOCKET__NODE_BIND;
4666 break;
4667
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668 default:
4669 node_perm = RAWIP_SOCKET__NODE_BIND;
4670 break;
4671 }
Eric Paris828dfe12008-04-17 13:17:49 -04004672
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004673 err = sel_netnode_sid(addrp, family_sa, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 if (err)
4675 goto out;
Eric Paris828dfe12008-04-17 13:17:49 -04004676
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004677 if (family_sa == AF_INET)
Eric Paris48c62af2012-04-02 13:15:44 -04004678 ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 else
Eric Paris48c62af2012-04-02 13:15:44 -04004680 ad.u.net->v6info.saddr = addr6->sin6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004682 err = avc_has_perm(&selinux_state,
4683 sksec->sid, sid,
Paul Moore253bfae2010-04-22 14:46:19 -04004684 sksec->sclass, node_perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685 if (err)
4686 goto out;
4687 }
4688out:
4689 return err;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004690err_af:
4691 /* Note that SCTP services expect -EINVAL, others -EAFNOSUPPORT. */
4692 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4693 return -EINVAL;
4694 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695}
4696
Richard Hainesd4529302018-02-13 20:57:18 +00004697/* This supports connect(2) and SCTP connect services such as sctp_connectx(3)
Mauro Carvalho Chehab5fb94e92018-05-08 15:14:57 -03004698 * and sctp_sendmsg(3) as described in Documentation/security/LSM-sctp.rst
Richard Hainesd4529302018-02-13 20:57:18 +00004699 */
4700static int selinux_socket_connect_helper(struct socket *sock,
4701 struct sockaddr *address, int addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702{
Paul Moore014ab192008-10-10 10:16:33 -04004703 struct sock *sk = sock->sk;
Paul Moore253bfae2010-04-22 14:46:19 -04004704 struct sk_security_struct *sksec = sk->sk_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705 int err;
4706
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004707 err = sock_has_perm(sk, SOCKET__CONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708 if (err)
4709 return err;
4710
4711 /*
Richard Hainesd4529302018-02-13 20:57:18 +00004712 * If a TCP, DCCP or SCTP socket, check name_connect permission
4713 * for the port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 */
Paul Moore253bfae2010-04-22 14:46:19 -04004715 if (sksec->sclass == SECCLASS_TCP_SOCKET ||
Richard Hainesd4529302018-02-13 20:57:18 +00004716 sksec->sclass == SECCLASS_DCCP_SOCKET ||
4717 sksec->sclass == SECCLASS_SCTP_SOCKET) {
Thomas Liu2bf49692009-07-14 12:14:09 -04004718 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004719 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720 struct sockaddr_in *addr4 = NULL;
4721 struct sockaddr_in6 *addr6 = NULL;
4722 unsigned short snum;
James Morris2ee92d42006-11-13 16:09:01 -08004723 u32 sid, perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724
Richard Hainesd4529302018-02-13 20:57:18 +00004725 /* sctp_connectx(3) calls via selinux_sctp_bind_connect()
4726 * that validates multiple connect addresses. Because of this
4727 * need to check address->sa_family as it is possible to have
4728 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4729 */
Richard Haines68741a8a2018-03-02 19:54:34 +00004730 switch (address->sa_family) {
4731 case AF_INET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732 addr4 = (struct sockaddr_in *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004733 if (addrlen < sizeof(struct sockaddr_in))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734 return -EINVAL;
4735 snum = ntohs(addr4->sin_port);
Richard Haines68741a8a2018-03-02 19:54:34 +00004736 break;
4737 case AF_INET6:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 addr6 = (struct sockaddr_in6 *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004739 if (addrlen < SIN6_LEN_RFC2133)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 return -EINVAL;
4741 snum = ntohs(addr6->sin6_port);
Richard Haines68741a8a2018-03-02 19:54:34 +00004742 break;
4743 default:
4744 /* Note that SCTP services expect -EINVAL, whereas
4745 * others expect -EAFNOSUPPORT.
4746 */
4747 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4748 return -EINVAL;
4749 else
4750 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751 }
4752
Paul Moore3e1121722008-04-10 10:48:14 -04004753 err = sel_netport_sid(sk->sk_protocol, snum, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754 if (err)
Richard Hainesd4529302018-02-13 20:57:18 +00004755 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756
Richard Hainesd4529302018-02-13 20:57:18 +00004757 switch (sksec->sclass) {
4758 case SECCLASS_TCP_SOCKET:
4759 perm = TCP_SOCKET__NAME_CONNECT;
4760 break;
4761 case SECCLASS_DCCP_SOCKET:
4762 perm = DCCP_SOCKET__NAME_CONNECT;
4763 break;
4764 case SECCLASS_SCTP_SOCKET:
4765 perm = SCTP_SOCKET__NAME_CONNECT;
4766 break;
4767 }
James Morris2ee92d42006-11-13 16:09:01 -08004768
Eric Paris50c205f2012-04-04 15:01:43 -04004769 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004770 ad.u.net = &net;
4771 ad.u.net->dport = htons(snum);
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004772 ad.u.net->family = address->sa_family;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004773 err = avc_has_perm(&selinux_state,
4774 sksec->sid, sid, sksec->sclass, perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 if (err)
Richard Hainesd4529302018-02-13 20:57:18 +00004776 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 }
4778
Richard Hainesd4529302018-02-13 20:57:18 +00004779 return 0;
4780}
Paul Moore014ab192008-10-10 10:16:33 -04004781
Richard Hainesd4529302018-02-13 20:57:18 +00004782/* Supports connect(2), see comments in selinux_socket_connect_helper() */
4783static int selinux_socket_connect(struct socket *sock,
4784 struct sockaddr *address, int addrlen)
4785{
4786 int err;
4787 struct sock *sk = sock->sk;
4788
4789 err = selinux_socket_connect_helper(sock, address, addrlen);
4790 if (err)
4791 return err;
4792
4793 return selinux_netlbl_socket_connect(sk, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794}
4795
4796static int selinux_socket_listen(struct socket *sock, int backlog)
4797{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004798 return sock_has_perm(sock->sk, SOCKET__LISTEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799}
4800
4801static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
4802{
4803 int err;
4804 struct inode_security_struct *isec;
4805 struct inode_security_struct *newisec;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004806 u16 sclass;
4807 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004809 err = sock_has_perm(sock->sk, SOCKET__ACCEPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810 if (err)
4811 return err;
4812
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05004813 isec = inode_security_novalidate(SOCK_INODE(sock));
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004814 spin_lock(&isec->lock);
4815 sclass = isec->sclass;
4816 sid = isec->sid;
4817 spin_unlock(&isec->lock);
4818
4819 newisec = inode_security_novalidate(SOCK_INODE(newsock));
4820 newisec->sclass = sclass;
4821 newisec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004822 newisec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823
4824 return 0;
4825}
4826
4827static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
Eric Paris828dfe12008-04-17 13:17:49 -04004828 int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004830 return sock_has_perm(sock->sk, SOCKET__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831}
4832
4833static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
4834 int size, int flags)
4835{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004836 return sock_has_perm(sock->sk, SOCKET__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837}
4838
4839static int selinux_socket_getsockname(struct socket *sock)
4840{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004841 return sock_has_perm(sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842}
4843
4844static int selinux_socket_getpeername(struct socket *sock)
4845{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004846 return sock_has_perm(sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847}
4848
Eric Paris828dfe12008-04-17 13:17:49 -04004849static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850{
Paul Mooref8687af2006-10-30 15:22:15 -08004851 int err;
4852
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004853 err = sock_has_perm(sock->sk, SOCKET__SETOPT);
Paul Mooref8687af2006-10-30 15:22:15 -08004854 if (err)
4855 return err;
4856
4857 return selinux_netlbl_socket_setsockopt(sock, level, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858}
4859
4860static int selinux_socket_getsockopt(struct socket *sock, int level,
4861 int optname)
4862{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004863 return sock_has_perm(sock->sk, SOCKET__GETOPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864}
4865
4866static int selinux_socket_shutdown(struct socket *sock, int how)
4867{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004868 return sock_has_perm(sock->sk, SOCKET__SHUTDOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869}
4870
David S. Miller3610cda2011-01-05 15:38:53 -08004871static int selinux_socket_unix_stream_connect(struct sock *sock,
4872 struct sock *other,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873 struct sock *newsk)
4874{
David S. Miller3610cda2011-01-05 15:38:53 -08004875 struct sk_security_struct *sksec_sock = sock->sk_security;
4876 struct sk_security_struct *sksec_other = other->sk_security;
Paul Moore4d1e2452010-04-22 14:46:18 -04004877 struct sk_security_struct *sksec_new = newsk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004878 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004879 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 int err;
4881
Eric Paris50c205f2012-04-04 15:01:43 -04004882 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004883 ad.u.net = &net;
4884 ad.u.net->sk = other;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004886 err = avc_has_perm(&selinux_state,
4887 sksec_sock->sid, sksec_other->sid,
Paul Moore4d1e2452010-04-22 14:46:18 -04004888 sksec_other->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 UNIX_STREAM_SOCKET__CONNECTTO, &ad);
4890 if (err)
4891 return err;
4892
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 /* server child socket */
Paul Moore4d1e2452010-04-22 14:46:18 -04004894 sksec_new->peer_sid = sksec_sock->sid;
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004895 err = security_sid_mls_copy(&selinux_state, sksec_other->sid,
4896 sksec_sock->sid, &sksec_new->sid);
Paul Moore4d1e2452010-04-22 14:46:18 -04004897 if (err)
4898 return err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004899
Paul Moore4d1e2452010-04-22 14:46:18 -04004900 /* connecting socket */
4901 sksec_sock->peer_sid = sksec_new->sid;
4902
4903 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904}
4905
4906static int selinux_socket_unix_may_send(struct socket *sock,
4907 struct socket *other)
4908{
Paul Moore253bfae2010-04-22 14:46:19 -04004909 struct sk_security_struct *ssec = sock->sk->sk_security;
4910 struct sk_security_struct *osec = other->sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004911 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004912 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913
Eric Paris50c205f2012-04-04 15:01:43 -04004914 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004915 ad.u.net = &net;
4916 ad.u.net->sk = other->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004918 return avc_has_perm(&selinux_state,
4919 ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
Paul Moore253bfae2010-04-22 14:46:19 -04004920 &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921}
4922
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004923static int selinux_inet_sys_rcv_skb(struct net *ns, int ifindex,
4924 char *addrp, u16 family, u32 peer_sid,
Thomas Liu2bf49692009-07-14 12:14:09 -04004925 struct common_audit_data *ad)
Paul Mooreeffad8d2008-01-29 08:49:27 -05004926{
4927 int err;
4928 u32 if_sid;
4929 u32 node_sid;
4930
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004931 err = sel_netif_sid(ns, ifindex, &if_sid);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004932 if (err)
4933 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004934 err = avc_has_perm(&selinux_state,
4935 peer_sid, if_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004936 SECCLASS_NETIF, NETIF__INGRESS, ad);
4937 if (err)
4938 return err;
4939
4940 err = sel_netnode_sid(addrp, family, &node_sid);
4941 if (err)
4942 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004943 return avc_has_perm(&selinux_state,
4944 peer_sid, node_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004945 SECCLASS_NODE, NODE__RECVFROM, ad);
4946}
4947
Paul Moore220deb92008-01-29 08:38:23 -05004948static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
Paul Moored8395c82008-10-10 10:16:30 -04004949 u16 family)
Paul Moore220deb92008-01-29 08:38:23 -05004950{
Paul Moore277d3422008-12-31 12:54:11 -05004951 int err = 0;
Paul Moore220deb92008-01-29 08:38:23 -05004952 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004953 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004954 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004955 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04004956 char *addrp;
4957
Eric Paris50c205f2012-04-04 15:01:43 -04004958 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004959 ad.u.net = &net;
4960 ad.u.net->netif = skb->skb_iif;
4961 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004962 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4963 if (err)
4964 return err;
Paul Moore220deb92008-01-29 08:38:23 -05004965
Paul Moore58bfbb52009-03-27 17:10:41 -04004966 if (selinux_secmark_enabled()) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004967 err = avc_has_perm(&selinux_state,
4968 sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Moored8395c82008-10-10 10:16:30 -04004969 PACKET__RECV, &ad);
Paul Moore58bfbb52009-03-27 17:10:41 -04004970 if (err)
4971 return err;
4972 }
Paul Moore220deb92008-01-29 08:38:23 -05004973
Steffen Klassertb9679a72011-02-23 12:55:21 +01004974 err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
4975 if (err)
4976 return err;
4977 err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004978
James Morris4e5ab4c2006-06-09 00:33:33 -07004979 return err;
4980}
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004981
James Morris4e5ab4c2006-06-09 00:33:33 -07004982static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
4983{
Paul Moore220deb92008-01-29 08:38:23 -05004984 int err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004985 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004986 u16 family = sk->sk_family;
4987 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004988 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004989 struct lsm_network_audit net = {0,};
Paul Moore220deb92008-01-29 08:38:23 -05004990 char *addrp;
Paul Moored8395c82008-10-10 10:16:30 -04004991 u8 secmark_active;
4992 u8 peerlbl_active;
James Morris4e5ab4c2006-06-09 00:33:33 -07004993
James Morris4e5ab4c2006-06-09 00:33:33 -07004994 if (family != PF_INET && family != PF_INET6)
Paul Moore220deb92008-01-29 08:38:23 -05004995 return 0;
James Morris4e5ab4c2006-06-09 00:33:33 -07004996
4997 /* Handle mapped IPv4 packets arriving via IPv6 sockets */
Al Viro87fcd702006-12-04 22:00:55 +00004998 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
James Morris4e5ab4c2006-06-09 00:33:33 -07004999 family = PF_INET;
5000
Paul Moored8395c82008-10-10 10:16:30 -04005001 /* If any sort of compatibility mode is enabled then handoff processing
5002 * to the selinux_sock_rcv_skb_compat() function to deal with the
5003 * special handling. We do this in an attempt to keep this function
5004 * as fast and as clean as possible. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005005 if (!selinux_policycap_netpeer())
Paul Moored8395c82008-10-10 10:16:30 -04005006 return selinux_sock_rcv_skb_compat(sk, skb, family);
5007
5008 secmark_active = selinux_secmark_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04005009 peerlbl_active = selinux_peerlbl_enabled();
Paul Moored8395c82008-10-10 10:16:30 -04005010 if (!secmark_active && !peerlbl_active)
5011 return 0;
5012
Eric Paris50c205f2012-04-04 15:01:43 -04005013 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005014 ad.u.net = &net;
5015 ad.u.net->netif = skb->skb_iif;
5016 ad.u.net->family = family;
Paul Moore224dfbd2008-01-29 08:38:13 -05005017 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
James Morris4e5ab4c2006-06-09 00:33:33 -07005018 if (err)
Paul Moore220deb92008-01-29 08:38:23 -05005019 return err;
James Morris4e5ab4c2006-06-09 00:33:33 -07005020
Paul Moored8395c82008-10-10 10:16:30 -04005021 if (peerlbl_active) {
Paul Moored621d352008-01-29 08:43:36 -05005022 u32 peer_sid;
5023
5024 err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
5025 if (err)
5026 return err;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005027 err = selinux_inet_sys_rcv_skb(sock_net(sk), skb->skb_iif,
5028 addrp, family, peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04005029 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04005030 selinux_netlbl_err(skb, family, err, 0);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005031 return err;
Paul Mooredfaebe92008-10-10 10:16:31 -04005032 }
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005033 err = avc_has_perm(&selinux_state,
5034 sk_sid, peer_sid, SECCLASS_PEER,
Paul Moored621d352008-01-29 08:43:36 -05005035 PEER__RECV, &ad);
Chad Hanson46d01d62013-12-23 17:45:01 -05005036 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04005037 selinux_netlbl_err(skb, family, err, 0);
Chad Hanson46d01d62013-12-23 17:45:01 -05005038 return err;
5039 }
Paul Moored621d352008-01-29 08:43:36 -05005040 }
5041
Paul Moored8395c82008-10-10 10:16:30 -04005042 if (secmark_active) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005043 err = avc_has_perm(&selinux_state,
5044 sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005045 PACKET__RECV, &ad);
5046 if (err)
5047 return err;
5048 }
5049
Paul Moored621d352008-01-29 08:43:36 -05005050 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051}
5052
Catherine Zhang2c7946a2006-03-20 22:41:23 -08005053static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
5054 int __user *optlen, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055{
5056 int err = 0;
5057 char *scontext;
5058 u32 scontext_len;
Paul Moore253bfae2010-04-22 14:46:19 -04005059 struct sk_security_struct *sksec = sock->sk->sk_security;
Paul Moore3de4bab2006-11-17 17:38:54 -05005060 u32 peer_sid = SECSID_NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061
Paul Moore253bfae2010-04-22 14:46:19 -04005062 if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
Richard Hainesd4529302018-02-13 20:57:18 +00005063 sksec->sclass == SECCLASS_TCP_SOCKET ||
5064 sksec->sclass == SECCLASS_SCTP_SOCKET)
Eric Parisdd3e7832010-04-07 15:08:46 -04005065 peer_sid = sksec->peer_sid;
Paul Moore253bfae2010-04-22 14:46:19 -04005066 if (peer_sid == SECSID_NULL)
5067 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005069 err = security_sid_to_context(&selinux_state, peer_sid, &scontext,
5070 &scontext_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071 if (err)
Paul Moore253bfae2010-04-22 14:46:19 -04005072 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073
5074 if (scontext_len > len) {
5075 err = -ERANGE;
5076 goto out_len;
5077 }
5078
5079 if (copy_to_user(optval, scontext, scontext_len))
5080 err = -EFAULT;
5081
5082out_len:
5083 if (put_user(scontext_len, optlen))
5084 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085 kfree(scontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086 return err;
5087}
5088
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005089static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
Catherine Zhang2c7946a2006-03-20 22:41:23 -08005090{
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005091 u32 peer_secid = SECSID_NULL;
Paul Moore75e22912008-01-29 08:38:04 -05005092 u16 family;
Paul Moore899134f2016-03-28 15:19:10 -04005093 struct inode_security_struct *isec;
Catherine Zhang877ce7c2006-06-29 12:27:47 -07005094
Paul Mooreaa862902008-10-10 10:16:29 -04005095 if (skb && skb->protocol == htons(ETH_P_IP))
5096 family = PF_INET;
5097 else if (skb && skb->protocol == htons(ETH_P_IPV6))
5098 family = PF_INET6;
5099 else if (sock)
Paul Moore75e22912008-01-29 08:38:04 -05005100 family = sock->sk->sk_family;
Paul Moore75e22912008-01-29 08:38:04 -05005101 else
5102 goto out;
5103
Paul Moore899134f2016-03-28 15:19:10 -04005104 if (sock && family == PF_UNIX) {
5105 isec = inode_security_novalidate(SOCK_INODE(sock));
5106 peer_secid = isec->sid;
5107 } else if (skb)
Paul Moore220deb92008-01-29 08:38:23 -05005108 selinux_skb_peerlbl_sid(skb, family, &peer_secid);
Catherine Zhang2c7946a2006-03-20 22:41:23 -08005109
Paul Moore75e22912008-01-29 08:38:04 -05005110out:
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005111 *secid = peer_secid;
Paul Moore75e22912008-01-29 08:38:04 -05005112 if (peer_secid == SECSID_NULL)
5113 return -EINVAL;
5114 return 0;
Catherine Zhang2c7946a2006-03-20 22:41:23 -08005115}
5116
Al Viro7d877f32005-10-21 03:20:43 -04005117static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118{
Paul Moore84914b72010-04-22 14:46:18 -04005119 struct sk_security_struct *sksec;
5120
5121 sksec = kzalloc(sizeof(*sksec), priority);
5122 if (!sksec)
5123 return -ENOMEM;
5124
5125 sksec->peer_sid = SECINITSID_UNLABELED;
5126 sksec->sid = SECINITSID_UNLABELED;
Stephen Smalley5dee25d2015-07-10 17:19:57 -04005127 sksec->sclass = SECCLASS_SOCKET;
Paul Moore84914b72010-04-22 14:46:18 -04005128 selinux_netlbl_sk_security_reset(sksec);
5129 sk->sk_security = sksec;
5130
5131 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132}
5133
5134static void selinux_sk_free_security(struct sock *sk)
5135{
Paul Moore84914b72010-04-22 14:46:18 -04005136 struct sk_security_struct *sksec = sk->sk_security;
5137
5138 sk->sk_security = NULL;
5139 selinux_netlbl_sk_security_free(sksec);
5140 kfree(sksec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141}
5142
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005143static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
5144{
Eric Parisdd3e7832010-04-07 15:08:46 -04005145 struct sk_security_struct *sksec = sk->sk_security;
5146 struct sk_security_struct *newsksec = newsk->sk_security;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005147
Eric Parisdd3e7832010-04-07 15:08:46 -04005148 newsksec->sid = sksec->sid;
5149 newsksec->peer_sid = sksec->peer_sid;
5150 newsksec->sclass = sksec->sclass;
Paul Moore99f59ed2006-08-29 17:53:48 -07005151
Eric Parisdd3e7832010-04-07 15:08:46 -04005152 selinux_netlbl_sk_security_reset(newsksec);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005153}
5154
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07005155static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005156{
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005157 if (!sk)
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07005158 *secid = SECINITSID_ANY_SOCKET;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005159 else {
5160 struct sk_security_struct *sksec = sk->sk_security;
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005161
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07005162 *secid = sksec->sid;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005163 }
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005164}
5165
Eric Paris828dfe12008-04-17 13:17:49 -04005166static void selinux_sock_graft(struct sock *sk, struct socket *parent)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005167{
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05005168 struct inode_security_struct *isec =
5169 inode_security_novalidate(SOCK_INODE(parent));
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005170 struct sk_security_struct *sksec = sk->sk_security;
5171
Paul Moore2873ead2014-07-28 10:42:48 -04005172 if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
5173 sk->sk_family == PF_UNIX)
David Woodhouse2148ccc2006-09-29 15:50:25 -07005174 isec->sid = sksec->sid;
Paul Moore220deb92008-01-29 08:38:23 -05005175 sksec->sclass = isec->sclass;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005176}
5177
Richard Hainesd4529302018-02-13 20:57:18 +00005178/* Called whenever SCTP receives an INIT chunk. This happens when an incoming
5179 * connect(2), sctp_connectx(3) or sctp_sendmsg(3) (with no association
5180 * already present).
5181 */
5182static int selinux_sctp_assoc_request(struct sctp_endpoint *ep,
5183 struct sk_buff *skb)
5184{
5185 struct sk_security_struct *sksec = ep->base.sk->sk_security;
5186 struct common_audit_data ad;
5187 struct lsm_network_audit net = {0,};
5188 u8 peerlbl_active;
5189 u32 peer_sid = SECINITSID_UNLABELED;
5190 u32 conn_sid;
5191 int err = 0;
5192
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005193 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005194 return 0;
5195
5196 peerlbl_active = selinux_peerlbl_enabled();
5197
5198 if (peerlbl_active) {
5199 /* This will return peer_sid = SECSID_NULL if there are
5200 * no peer labels, see security_net_peersid_resolve().
5201 */
5202 err = selinux_skb_peerlbl_sid(skb, ep->base.sk->sk_family,
5203 &peer_sid);
5204 if (err)
5205 return err;
5206
5207 if (peer_sid == SECSID_NULL)
5208 peer_sid = SECINITSID_UNLABELED;
5209 }
5210
5211 if (sksec->sctp_assoc_state == SCTP_ASSOC_UNSET) {
5212 sksec->sctp_assoc_state = SCTP_ASSOC_SET;
5213
5214 /* Here as first association on socket. As the peer SID
5215 * was allowed by peer recv (and the netif/node checks),
5216 * then it is approved by policy and used as the primary
5217 * peer SID for getpeercon(3).
5218 */
5219 sksec->peer_sid = peer_sid;
5220 } else if (sksec->peer_sid != peer_sid) {
5221 /* Other association peer SIDs are checked to enforce
5222 * consistency among the peer SIDs.
5223 */
5224 ad.type = LSM_AUDIT_DATA_NET;
5225 ad.u.net = &net;
5226 ad.u.net->sk = ep->base.sk;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005227 err = avc_has_perm(&selinux_state,
5228 sksec->peer_sid, peer_sid, sksec->sclass,
Richard Hainesd4529302018-02-13 20:57:18 +00005229 SCTP_SOCKET__ASSOCIATION, &ad);
5230 if (err)
5231 return err;
5232 }
5233
5234 /* Compute the MLS component for the connection and store
5235 * the information in ep. This will be used by SCTP TCP type
5236 * sockets and peeled off connections as they cause a new
5237 * socket to be generated. selinux_sctp_sk_clone() will then
5238 * plug this into the new socket.
5239 */
5240 err = selinux_conn_sid(sksec->sid, peer_sid, &conn_sid);
5241 if (err)
5242 return err;
5243
5244 ep->secid = conn_sid;
5245 ep->peer_secid = peer_sid;
5246
5247 /* Set any NetLabel labels including CIPSO/CALIPSO options. */
5248 return selinux_netlbl_sctp_assoc_request(ep, skb);
5249}
5250
5251/* Check if sctp IPv4/IPv6 addresses are valid for binding or connecting
5252 * based on their @optname.
5253 */
5254static int selinux_sctp_bind_connect(struct sock *sk, int optname,
5255 struct sockaddr *address,
5256 int addrlen)
5257{
5258 int len, err = 0, walk_size = 0;
5259 void *addr_buf;
5260 struct sockaddr *addr;
5261 struct socket *sock;
5262
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005263 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005264 return 0;
5265
5266 /* Process one or more addresses that may be IPv4 or IPv6 */
5267 sock = sk->sk_socket;
5268 addr_buf = address;
5269
5270 while (walk_size < addrlen) {
5271 addr = addr_buf;
5272 switch (addr->sa_family) {
Alexey Kodanev4152dc92018-05-11 20:15:13 +03005273 case AF_UNSPEC:
Richard Hainesd4529302018-02-13 20:57:18 +00005274 case AF_INET:
5275 len = sizeof(struct sockaddr_in);
5276 break;
5277 case AF_INET6:
5278 len = sizeof(struct sockaddr_in6);
5279 break;
5280 default:
Alexey Kodanev4152dc92018-05-11 20:15:13 +03005281 return -EINVAL;
Richard Hainesd4529302018-02-13 20:57:18 +00005282 }
5283
5284 err = -EINVAL;
5285 switch (optname) {
5286 /* Bind checks */
5287 case SCTP_PRIMARY_ADDR:
5288 case SCTP_SET_PEER_PRIMARY_ADDR:
5289 case SCTP_SOCKOPT_BINDX_ADD:
5290 err = selinux_socket_bind(sock, addr, len);
5291 break;
5292 /* Connect checks */
5293 case SCTP_SOCKOPT_CONNECTX:
5294 case SCTP_PARAM_SET_PRIMARY:
5295 case SCTP_PARAM_ADD_IP:
5296 case SCTP_SENDMSG_CONNECT:
5297 err = selinux_socket_connect_helper(sock, addr, len);
5298 if (err)
5299 return err;
5300
5301 /* As selinux_sctp_bind_connect() is called by the
5302 * SCTP protocol layer, the socket is already locked,
5303 * therefore selinux_netlbl_socket_connect_locked() is
5304 * is called here. The situations handled are:
5305 * sctp_connectx(3), sctp_sendmsg(3), sendmsg(2),
5306 * whenever a new IP address is added or when a new
5307 * primary address is selected.
5308 * Note that an SCTP connect(2) call happens before
5309 * the SCTP protocol layer and is handled via
5310 * selinux_socket_connect().
5311 */
5312 err = selinux_netlbl_socket_connect_locked(sk, addr);
5313 break;
5314 }
5315
5316 if (err)
5317 return err;
5318
5319 addr_buf += len;
5320 walk_size += len;
5321 }
5322
5323 return 0;
5324}
5325
5326/* Called whenever a new socket is created by accept(2) or sctp_peeloff(3). */
5327static void selinux_sctp_sk_clone(struct sctp_endpoint *ep, struct sock *sk,
5328 struct sock *newsk)
5329{
5330 struct sk_security_struct *sksec = sk->sk_security;
5331 struct sk_security_struct *newsksec = newsk->sk_security;
5332
5333 /* If policy does not support SECCLASS_SCTP_SOCKET then call
5334 * the non-sctp clone version.
5335 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005336 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005337 return selinux_sk_clone_security(sk, newsk);
5338
5339 newsksec->sid = ep->secid;
5340 newsksec->peer_sid = ep->peer_secid;
5341 newsksec->sclass = sksec->sclass;
5342 selinux_netlbl_sctp_sk_clone(sk, newsk);
5343}
5344
Adrian Bunk9a673e52006-08-15 00:03:53 -07005345static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
5346 struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005347{
5348 struct sk_security_struct *sksec = sk->sk_security;
5349 int err;
Paul Moore0b1f24e2013-12-03 11:39:13 -05005350 u16 family = req->rsk_ops->family;
Paul Moore446b8022013-12-04 16:10:51 -05005351 u32 connsid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005352 u32 peersid;
5353
Paul Mooreaa862902008-10-10 10:16:29 -04005354 err = selinux_skb_peerlbl_sid(skb, family, &peersid);
Paul Moore220deb92008-01-29 08:38:23 -05005355 if (err)
5356 return err;
Paul Moore446b8022013-12-04 16:10:51 -05005357 err = selinux_conn_sid(sksec->sid, peersid, &connsid);
5358 if (err)
5359 return err;
5360 req->secid = connsid;
5361 req->peer_secid = peersid;
Venkat Yekkiralaa51c64f2006-07-27 22:01:34 -07005362
Paul Moore389fb8002009-03-27 17:10:34 -04005363 return selinux_netlbl_inet_conn_request(req, family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005364}
5365
Adrian Bunk9a673e52006-08-15 00:03:53 -07005366static void selinux_inet_csk_clone(struct sock *newsk,
5367 const struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005368{
5369 struct sk_security_struct *newsksec = newsk->sk_security;
5370
5371 newsksec->sid = req->secid;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005372 newsksec->peer_sid = req->peer_secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005373 /* NOTE: Ideally, we should also get the isec->sid for the
5374 new socket in sync, but we don't have the isec available yet.
5375 So we will wait until sock_graft to do it, by which
5376 time it will have been created and available. */
Paul Moore99f59ed2006-08-29 17:53:48 -07005377
Paul Moore9f2ad662006-11-17 17:38:53 -05005378 /* We don't need to take any sort of lock here as we are the only
5379 * thread with access to newsksec */
Paul Moore389fb8002009-03-27 17:10:34 -04005380 selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005381}
5382
Paul Moore014ab192008-10-10 10:16:33 -04005383static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005384{
Paul Mooreaa862902008-10-10 10:16:29 -04005385 u16 family = sk->sk_family;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005386 struct sk_security_struct *sksec = sk->sk_security;
5387
Paul Mooreaa862902008-10-10 10:16:29 -04005388 /* handle mapped IPv4 packets arriving via IPv6 sockets */
5389 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
5390 family = PF_INET;
5391
5392 selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005393}
5394
Eric Paris2606fd12010-10-13 16:24:41 -04005395static int selinux_secmark_relabel_packet(u32 sid)
5396{
5397 const struct task_security_struct *__tsec;
5398 u32 tsid;
5399
5400 __tsec = current_security();
5401 tsid = __tsec->sid;
5402
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005403 return avc_has_perm(&selinux_state,
5404 tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO,
5405 NULL);
Eric Paris2606fd12010-10-13 16:24:41 -04005406}
5407
5408static void selinux_secmark_refcount_inc(void)
5409{
5410 atomic_inc(&selinux_secmark_refcount);
5411}
5412
5413static void selinux_secmark_refcount_dec(void)
5414{
5415 atomic_dec(&selinux_secmark_refcount);
5416}
5417
Adrian Bunk9a673e52006-08-15 00:03:53 -07005418static void selinux_req_classify_flow(const struct request_sock *req,
5419 struct flowi *fl)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005420{
David S. Miller1d28f422011-03-12 00:29:39 -05005421 fl->flowi_secid = req->secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005422}
5423
Paul Moore5dbbaf22013-01-14 07:12:19 +00005424static int selinux_tun_dev_alloc_security(void **security)
5425{
5426 struct tun_security_struct *tunsec;
5427
5428 tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL);
5429 if (!tunsec)
5430 return -ENOMEM;
5431 tunsec->sid = current_sid();
5432
5433 *security = tunsec;
5434 return 0;
5435}
5436
5437static void selinux_tun_dev_free_security(void *security)
5438{
5439 kfree(security);
5440}
5441
Paul Mooreed6d76e2009-08-28 18:12:49 -04005442static int selinux_tun_dev_create(void)
5443{
5444 u32 sid = current_sid();
5445
5446 /* we aren't taking into account the "sockcreate" SID since the socket
5447 * that is being created here is not a socket in the traditional sense,
5448 * instead it is a private sock, accessible only to the kernel, and
5449 * representing a wide range of network traffic spanning multiple
5450 * connections unlike traditional sockets - check the TUN driver to
5451 * get a better understanding of why this socket is special */
5452
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005453 return avc_has_perm(&selinux_state,
5454 sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005455 NULL);
5456}
5457
Paul Moore5dbbaf22013-01-14 07:12:19 +00005458static int selinux_tun_dev_attach_queue(void *security)
Paul Mooreed6d76e2009-08-28 18:12:49 -04005459{
Paul Moore5dbbaf22013-01-14 07:12:19 +00005460 struct tun_security_struct *tunsec = security;
5461
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005462 return avc_has_perm(&selinux_state,
5463 current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET,
Paul Moore5dbbaf22013-01-14 07:12:19 +00005464 TUN_SOCKET__ATTACH_QUEUE, NULL);
5465}
5466
5467static int selinux_tun_dev_attach(struct sock *sk, void *security)
5468{
5469 struct tun_security_struct *tunsec = security;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005470 struct sk_security_struct *sksec = sk->sk_security;
5471
5472 /* we don't currently perform any NetLabel based labeling here and it
5473 * isn't clear that we would want to do so anyway; while we could apply
5474 * labeling without the support of the TUN user the resulting labeled
5475 * traffic from the other end of the connection would almost certainly
5476 * cause confusion to the TUN user that had no idea network labeling
5477 * protocols were being used */
5478
Paul Moore5dbbaf22013-01-14 07:12:19 +00005479 sksec->sid = tunsec->sid;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005480 sksec->sclass = SECCLASS_TUN_SOCKET;
Paul Moore5dbbaf22013-01-14 07:12:19 +00005481
5482 return 0;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005483}
5484
Paul Moore5dbbaf22013-01-14 07:12:19 +00005485static int selinux_tun_dev_open(void *security)
Paul Mooreed6d76e2009-08-28 18:12:49 -04005486{
Paul Moore5dbbaf22013-01-14 07:12:19 +00005487 struct tun_security_struct *tunsec = security;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005488 u32 sid = current_sid();
5489 int err;
5490
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005491 err = avc_has_perm(&selinux_state,
5492 sid, tunsec->sid, SECCLASS_TUN_SOCKET,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005493 TUN_SOCKET__RELABELFROM, NULL);
5494 if (err)
5495 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005496 err = avc_has_perm(&selinux_state,
5497 sid, sid, SECCLASS_TUN_SOCKET,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005498 TUN_SOCKET__RELABELTO, NULL);
5499 if (err)
5500 return err;
Paul Moore5dbbaf22013-01-14 07:12:19 +00005501 tunsec->sid = sid;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005502
5503 return 0;
5504}
5505
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
5507{
5508 int err = 0;
5509 u32 perm;
5510 struct nlmsghdr *nlh;
Paul Moore253bfae2010-04-22 14:46:19 -04005511 struct sk_security_struct *sksec = sk->sk_security;
Eric Paris828dfe12008-04-17 13:17:49 -04005512
Hong zhi guo77954982013-03-27 06:49:35 +00005513 if (skb->len < NLMSG_HDRLEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 err = -EINVAL;
5515 goto out;
5516 }
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -07005517 nlh = nlmsg_hdr(skb);
Eric Paris828dfe12008-04-17 13:17:49 -04005518
Paul Moore253bfae2010-04-22 14:46:19 -04005519 err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520 if (err) {
5521 if (err == -EINVAL) {
Vladis Dronov76319942015-12-24 11:09:41 -05005522 pr_warn_ratelimited("SELinux: unrecognized netlink"
5523 " message: protocol=%hu nlmsg_type=%hu sclass=%s"
5524 " pig=%d comm=%s\n",
Marek Milkoviccded3ff2015-06-04 16:22:16 -04005525 sk->sk_protocol, nlh->nlmsg_type,
Vladis Dronov76319942015-12-24 11:09:41 -05005526 secclass_map[sksec->sclass - 1].name,
5527 task_pid_nr(current), current->comm);
Paul Mooree5a5ca92018-03-01 17:38:30 -05005528 if (!enforcing_enabled(&selinux_state) ||
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005529 security_get_allow_unknown(&selinux_state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530 err = 0;
5531 }
5532
5533 /* Ignore */
5534 if (err == -ENOENT)
5535 err = 0;
5536 goto out;
5537 }
5538
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005539 err = sock_has_perm(sk, perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540out:
5541 return err;
5542}
5543
5544#ifdef CONFIG_NETFILTER
5545
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005546static unsigned int selinux_ip_forward(struct sk_buff *skb,
5547 const struct net_device *indev,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005548 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549{
Paul Mooredfaebe92008-10-10 10:16:31 -04005550 int err;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005551 char *addrp;
5552 u32 peer_sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04005553 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005554 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05005555 u8 secmark_active;
Paul Moore948bf852008-10-10 10:16:32 -04005556 u8 netlbl_active;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005557 u8 peerlbl_active;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005558
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005559 if (!selinux_policycap_netpeer())
Paul Mooreeffad8d2008-01-29 08:49:27 -05005560 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005561
Paul Mooreeffad8d2008-01-29 08:49:27 -05005562 secmark_active = selinux_secmark_enabled();
Paul Moore948bf852008-10-10 10:16:32 -04005563 netlbl_active = netlbl_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04005564 peerlbl_active = selinux_peerlbl_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05005565 if (!secmark_active && !peerlbl_active)
5566 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005567
Paul Moored8395c82008-10-10 10:16:30 -04005568 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
5569 return NF_DROP;
5570
Eric Paris50c205f2012-04-04 15:01:43 -04005571 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005572 ad.u.net = &net;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005573 ad.u.net->netif = indev->ifindex;
Eric Paris48c62af2012-04-02 13:15:44 -04005574 ad.u.net->family = family;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005575 if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
5576 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577
Paul Mooredfaebe92008-10-10 10:16:31 -04005578 if (peerlbl_active) {
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005579 err = selinux_inet_sys_rcv_skb(dev_net(indev), indev->ifindex,
5580 addrp, family, peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04005581 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04005582 selinux_netlbl_err(skb, family, err, 1);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005583 return NF_DROP;
Paul Mooredfaebe92008-10-10 10:16:31 -04005584 }
5585 }
Paul Mooreeffad8d2008-01-29 08:49:27 -05005586
5587 if (secmark_active)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005588 if (avc_has_perm(&selinux_state,
5589 peer_sid, skb->secmark,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005590 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
5591 return NF_DROP;
5592
Paul Moore948bf852008-10-10 10:16:32 -04005593 if (netlbl_active)
5594 /* we do this in the FORWARD path and not the POST_ROUTING
5595 * path because we want to make sure we apply the necessary
5596 * labeling before IPsec is applied so we can leverage AH
5597 * protection */
5598 if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
5599 return NF_DROP;
5600
Paul Mooreeffad8d2008-01-29 08:49:27 -05005601 return NF_ACCEPT;
5602}
5603
Eric W. Biederman06198b32015-09-18 14:33:06 -05005604static unsigned int selinux_ipv4_forward(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005605 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005606 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005607{
David S. Miller238e54c2015-04-03 20:32:56 -04005608 return selinux_ip_forward(skb, state->in, PF_INET);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005609}
5610
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005611#if IS_ENABLED(CONFIG_IPV6)
Eric W. Biederman06198b32015-09-18 14:33:06 -05005612static unsigned int selinux_ipv6_forward(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005613 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005614 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005615{
David S. Miller238e54c2015-04-03 20:32:56 -04005616 return selinux_ip_forward(skb, state->in, PF_INET6);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005617}
5618#endif /* IPV6 */
5619
Paul Moore948bf852008-10-10 10:16:32 -04005620static unsigned int selinux_ip_output(struct sk_buff *skb,
5621 u16 family)
5622{
Paul Moore47180062013-12-04 16:10:45 -05005623 struct sock *sk;
Paul Moore948bf852008-10-10 10:16:32 -04005624 u32 sid;
5625
5626 if (!netlbl_enabled())
5627 return NF_ACCEPT;
5628
5629 /* we do this in the LOCAL_OUT path and not the POST_ROUTING path
5630 * because we want to make sure we apply the necessary labeling
5631 * before IPsec is applied so we can leverage AH protection */
Paul Moore47180062013-12-04 16:10:45 -05005632 sk = skb->sk;
5633 if (sk) {
5634 struct sk_security_struct *sksec;
5635
Eric Dumazete446f9d2015-10-08 05:01:55 -07005636 if (sk_listener(sk))
Paul Moore47180062013-12-04 16:10:45 -05005637 /* if the socket is the listening state then this
5638 * packet is a SYN-ACK packet which means it needs to
5639 * be labeled based on the connection/request_sock and
5640 * not the parent socket. unfortunately, we can't
5641 * lookup the request_sock yet as it isn't queued on
5642 * the parent socket until after the SYN-ACK is sent.
5643 * the "solution" is to simply pass the packet as-is
5644 * as any IP option based labeling should be copied
5645 * from the initial connection request (in the IP
5646 * layer). it is far from ideal, but until we get a
5647 * security label in the packet itself this is the
5648 * best we can do. */
5649 return NF_ACCEPT;
5650
5651 /* standard practice, label using the parent socket */
5652 sksec = sk->sk_security;
Paul Moore948bf852008-10-10 10:16:32 -04005653 sid = sksec->sid;
5654 } else
5655 sid = SECINITSID_KERNEL;
5656 if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
5657 return NF_DROP;
5658
5659 return NF_ACCEPT;
5660}
5661
Eric W. Biederman06198b32015-09-18 14:33:06 -05005662static unsigned int selinux_ipv4_output(void *priv,
Paul Moore948bf852008-10-10 10:16:32 -04005663 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005664 const struct nf_hook_state *state)
Paul Moore948bf852008-10-10 10:16:32 -04005665{
5666 return selinux_ip_output(skb, PF_INET);
5667}
5668
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005669#if IS_ENABLED(CONFIG_IPV6)
Huw Davies2917f572016-06-27 15:06:15 -04005670static unsigned int selinux_ipv6_output(void *priv,
5671 struct sk_buff *skb,
5672 const struct nf_hook_state *state)
5673{
5674 return selinux_ip_output(skb, PF_INET6);
5675}
5676#endif /* IPV6 */
5677
Paul Mooreeffad8d2008-01-29 08:49:27 -05005678static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
5679 int ifindex,
Paul Moored8395c82008-10-10 10:16:30 -04005680 u16 family)
James Morris4e5ab4c2006-06-09 00:33:33 -07005681{
Eric Dumazet54abc682015-11-08 10:54:07 -08005682 struct sock *sk = skb_to_full_sk(skb);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005683 struct sk_security_struct *sksec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005684 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005685 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04005686 char *addrp;
5687 u8 proto;
James Morris4e5ab4c2006-06-09 00:33:33 -07005688
Paul Mooreeffad8d2008-01-29 08:49:27 -05005689 if (sk == NULL)
5690 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005691 sksec = sk->sk_security;
James Morris4e5ab4c2006-06-09 00:33:33 -07005692
Eric Paris50c205f2012-04-04 15:01:43 -04005693 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005694 ad.u.net = &net;
5695 ad.u.net->netif = ifindex;
5696 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04005697 if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
5698 return NF_DROP;
5699
Paul Moore58bfbb52009-03-27 17:10:41 -04005700 if (selinux_secmark_enabled())
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005701 if (avc_has_perm(&selinux_state,
5702 sksec->sid, skb->secmark,
Paul Moored8395c82008-10-10 10:16:30 -04005703 SECCLASS_PACKET, PACKET__SEND, &ad))
Eric Paris2fe66ec2010-11-23 06:28:08 +00005704 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07005705
Steffen Klassertb9679a72011-02-23 12:55:21 +01005706 if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
5707 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07005708
Paul Mooreeffad8d2008-01-29 08:49:27 -05005709 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710}
5711
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005712static unsigned int selinux_ip_postroute(struct sk_buff *skb,
5713 const struct net_device *outdev,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005714 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715{
Paul Mooreeffad8d2008-01-29 08:49:27 -05005716 u32 secmark_perm;
5717 u32 peer_sid;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005718 int ifindex = outdev->ifindex;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005719 struct sock *sk;
Thomas Liu2bf49692009-07-14 12:14:09 -04005720 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005721 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05005722 char *addrp;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005723 u8 secmark_active;
5724 u8 peerlbl_active;
5725
Paul Mooreeffad8d2008-01-29 08:49:27 -05005726 /* If any sort of compatibility mode is enabled then handoff processing
5727 * to the selinux_ip_postroute_compat() function to deal with the
5728 * special handling. We do this in an attempt to keep this function
5729 * as fast and as clean as possible. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005730 if (!selinux_policycap_netpeer())
Paul Moored8395c82008-10-10 10:16:30 -04005731 return selinux_ip_postroute_compat(skb, ifindex, family);
Paul Moorec0828e52013-12-10 14:58:01 -05005732
Paul Mooreeffad8d2008-01-29 08:49:27 -05005733 secmark_active = selinux_secmark_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04005734 peerlbl_active = selinux_peerlbl_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05005735 if (!secmark_active && !peerlbl_active)
5736 return NF_ACCEPT;
5737
Eric Dumazet54abc682015-11-08 10:54:07 -08005738 sk = skb_to_full_sk(skb);
Paul Moorec0828e52013-12-10 14:58:01 -05005739
Paul Mooreeffad8d2008-01-29 08:49:27 -05005740#ifdef CONFIG_XFRM
5741 /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
5742 * packet transformation so allow the packet to pass without any checks
5743 * since we'll have another chance to perform access control checks
5744 * when the packet is on it's final way out.
5745 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
Paul Moorec0828e52013-12-10 14:58:01 -05005746 * is NULL, in this case go ahead and apply access control.
5747 * NOTE: if this is a local socket (skb->sk != NULL) that is in the
5748 * TCP listening state we cannot wait until the XFRM processing
5749 * is done as we will miss out on the SA label if we do;
5750 * unfortunately, this means more work, but it is only once per
5751 * connection. */
5752 if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL &&
Eric Dumazete446f9d2015-10-08 05:01:55 -07005753 !(sk && sk_listener(sk)))
Paul Mooreeffad8d2008-01-29 08:49:27 -05005754 return NF_ACCEPT;
5755#endif
Paul Mooreeffad8d2008-01-29 08:49:27 -05005756
Paul Moored8395c82008-10-10 10:16:30 -04005757 if (sk == NULL) {
Paul Moore446b8022013-12-04 16:10:51 -05005758 /* Without an associated socket the packet is either coming
5759 * from the kernel or it is being forwarded; check the packet
5760 * to determine which and if the packet is being forwarded
5761 * query the packet directly to determine the security label. */
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005762 if (skb->skb_iif) {
5763 secmark_perm = PACKET__FORWARD_OUT;
Paul Moored8395c82008-10-10 10:16:30 -04005764 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005765 return NF_DROP;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005766 } else {
5767 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04005768 peer_sid = SECINITSID_KERNEL;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005769 }
Eric Dumazete446f9d2015-10-08 05:01:55 -07005770 } else if (sk_listener(sk)) {
Paul Moore446b8022013-12-04 16:10:51 -05005771 /* Locally generated packet but the associated socket is in the
5772 * listening state which means this is a SYN-ACK packet. In
5773 * this particular case the correct security label is assigned
5774 * to the connection/request_sock but unfortunately we can't
5775 * query the request_sock as it isn't queued on the parent
5776 * socket until after the SYN-ACK packet is sent; the only
5777 * viable choice is to regenerate the label like we do in
5778 * selinux_inet_conn_request(). See also selinux_ip_output()
5779 * for similar problems. */
5780 u32 skb_sid;
Eric Dumazete446f9d2015-10-08 05:01:55 -07005781 struct sk_security_struct *sksec;
5782
Eric Dumazete446f9d2015-10-08 05:01:55 -07005783 sksec = sk->sk_security;
Paul Moore446b8022013-12-04 16:10:51 -05005784 if (selinux_skb_peerlbl_sid(skb, family, &skb_sid))
5785 return NF_DROP;
Paul Moorec0828e52013-12-10 14:58:01 -05005786 /* At this point, if the returned skb peerlbl is SECSID_NULL
5787 * and the packet has been through at least one XFRM
5788 * transformation then we must be dealing with the "final"
5789 * form of labeled IPsec packet; since we've already applied
5790 * all of our access controls on this packet we can safely
5791 * pass the packet. */
5792 if (skb_sid == SECSID_NULL) {
5793 switch (family) {
5794 case PF_INET:
5795 if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
5796 return NF_ACCEPT;
5797 break;
5798 case PF_INET6:
5799 if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED)
5800 return NF_ACCEPT;
Paul Moorea7a91a12014-09-03 10:51:59 -04005801 break;
Paul Moorec0828e52013-12-10 14:58:01 -05005802 default:
5803 return NF_DROP_ERR(-ECONNREFUSED);
5804 }
5805 }
Paul Moore446b8022013-12-04 16:10:51 -05005806 if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid))
5807 return NF_DROP;
5808 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04005809 } else {
Paul Moore446b8022013-12-04 16:10:51 -05005810 /* Locally generated packet, fetch the security label from the
5811 * associated socket. */
Paul Mooreeffad8d2008-01-29 08:49:27 -05005812 struct sk_security_struct *sksec = sk->sk_security;
5813 peer_sid = sksec->sid;
5814 secmark_perm = PACKET__SEND;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005815 }
5816
Eric Paris50c205f2012-04-04 15:01:43 -04005817 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005818 ad.u.net = &net;
5819 ad.u.net->netif = ifindex;
5820 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04005821 if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
Eric Paris04f6d702010-11-23 06:28:02 +00005822 return NF_DROP;
Paul Moored8395c82008-10-10 10:16:30 -04005823
Paul Mooreeffad8d2008-01-29 08:49:27 -05005824 if (secmark_active)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005825 if (avc_has_perm(&selinux_state,
5826 peer_sid, skb->secmark,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005827 SECCLASS_PACKET, secmark_perm, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005828 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005829
5830 if (peerlbl_active) {
5831 u32 if_sid;
5832 u32 node_sid;
5833
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005834 if (sel_netif_sid(dev_net(outdev), ifindex, &if_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005835 return NF_DROP;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005836 if (avc_has_perm(&selinux_state,
5837 peer_sid, if_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005838 SECCLASS_NETIF, NETIF__EGRESS, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005839 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005840
5841 if (sel_netnode_sid(addrp, family, &node_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005842 return NF_DROP;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005843 if (avc_has_perm(&selinux_state,
5844 peer_sid, node_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005845 SECCLASS_NODE, NODE__SENDTO, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005846 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005847 }
5848
5849 return NF_ACCEPT;
5850}
5851
Eric W. Biederman06198b32015-09-18 14:33:06 -05005852static unsigned int selinux_ipv4_postroute(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005853 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005854 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005855{
David S. Miller238e54c2015-04-03 20:32:56 -04005856 return selinux_ip_postroute(skb, state->out, PF_INET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857}
5858
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005859#if IS_ENABLED(CONFIG_IPV6)
Eric W. Biederman06198b32015-09-18 14:33:06 -05005860static unsigned int selinux_ipv6_postroute(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005861 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005862 const struct nf_hook_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863{
David S. Miller238e54c2015-04-03 20:32:56 -04005864 return selinux_ip_postroute(skb, state->out, PF_INET6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005865}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866#endif /* IPV6 */
5867
5868#endif /* CONFIG_NETFILTER */
5869
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
5871{
Stephen Smalley941fc5b2009-10-01 14:48:23 -04005872 return selinux_nlmsg_perm(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873}
5874
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005875static int ipc_alloc_security(struct kern_ipc_perm *perm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876 u16 sclass)
5877{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878 struct ipc_security_struct *isec;
5879
James Morris89d155e2005-10-30 14:59:21 -08005880 isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005881 if (!isec)
5882 return -ENOMEM;
5883
Linus Torvalds1da177e2005-04-16 15:20:36 -07005884 isec->sclass = sclass;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005885 isec->sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886 perm->security = isec;
5887
5888 return 0;
5889}
5890
5891static void ipc_free_security(struct kern_ipc_perm *perm)
5892{
5893 struct ipc_security_struct *isec = perm->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894 perm->security = NULL;
5895 kfree(isec);
5896}
5897
5898static int msg_msg_alloc_security(struct msg_msg *msg)
5899{
5900 struct msg_security_struct *msec;
5901
James Morris89d155e2005-10-30 14:59:21 -08005902 msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903 if (!msec)
5904 return -ENOMEM;
5905
Linus Torvalds1da177e2005-04-16 15:20:36 -07005906 msec->sid = SECINITSID_UNLABELED;
5907 msg->security = msec;
5908
5909 return 0;
5910}
5911
5912static void msg_msg_free_security(struct msg_msg *msg)
5913{
5914 struct msg_security_struct *msec = msg->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005915
5916 msg->security = NULL;
5917 kfree(msec);
5918}
5919
5920static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
Stephen Smalley6af963f2005-05-01 08:58:39 -07005921 u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005924 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005925 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005926
Linus Torvalds1da177e2005-04-16 15:20:36 -07005927 isec = ipc_perms->security;
5928
Eric Paris50c205f2012-04-04 15:01:43 -04005929 ad.type = LSM_AUDIT_DATA_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005930 ad.u.ipc_id = ipc_perms->key;
5931
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005932 return avc_has_perm(&selinux_state,
5933 sid, isec->sid, isec->sclass, perms, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934}
5935
5936static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
5937{
5938 return msg_msg_alloc_security(msg);
5939}
5940
5941static void selinux_msg_msg_free_security(struct msg_msg *msg)
5942{
5943 msg_msg_free_security(msg);
5944}
5945
5946/* message queue security operations */
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005947static int selinux_msg_queue_alloc_security(struct kern_ipc_perm *msq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005949 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005950 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005951 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952 int rc;
5953
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005954 rc = ipc_alloc_security(msq, SECCLASS_MSGQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955 if (rc)
5956 return rc;
5957
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005958 isec = msq->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005959
Eric Paris50c205f2012-04-04 15:01:43 -04005960 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005961 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005962
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005963 rc = avc_has_perm(&selinux_state,
5964 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005965 MSGQ__CREATE, &ad);
5966 if (rc) {
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005967 ipc_free_security(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005968 return rc;
5969 }
5970 return 0;
5971}
5972
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005973static void selinux_msg_queue_free_security(struct kern_ipc_perm *msq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005974{
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005975 ipc_free_security(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005976}
5977
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005978static int selinux_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005980 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005981 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005982 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005983
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005984 isec = msq->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005985
Eric Paris50c205f2012-04-04 15:01:43 -04005986 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005987 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005988
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005989 return avc_has_perm(&selinux_state,
5990 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991 MSGQ__ASSOCIATE, &ad);
5992}
5993
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005994static int selinux_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005995{
5996 int err;
5997 int perms;
5998
Eric Paris828dfe12008-04-17 13:17:49 -04005999 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006000 case IPC_INFO:
6001 case MSG_INFO:
6002 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006003 return avc_has_perm(&selinux_state,
6004 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006005 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006006 case IPC_STAT:
6007 case MSG_STAT:
Davidlohr Bueso23c8cec2018-04-10 16:35:30 -07006008 case MSG_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006009 perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
6010 break;
6011 case IPC_SET:
6012 perms = MSGQ__SETATTR;
6013 break;
6014 case IPC_RMID:
6015 perms = MSGQ__DESTROY;
6016 break;
6017 default:
6018 return 0;
6019 }
6020
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05006021 err = ipc_has_perm(msq, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022 return err;
6023}
6024
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05006025static int selinux_msg_queue_msgsnd(struct kern_ipc_perm *msq, struct msg_msg *msg, int msqflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027 struct ipc_security_struct *isec;
6028 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006029 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006030 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006031 int rc;
6032
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05006033 isec = msq->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034 msec = msg->security;
6035
6036 /*
6037 * First time through, need to assign label to the message
6038 */
6039 if (msec->sid == SECINITSID_UNLABELED) {
6040 /*
6041 * Compute new sid based on current process and
6042 * message queue this message will be stored in
6043 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006044 rc = security_transition_sid(&selinux_state, sid, isec->sid,
6045 SECCLASS_MSG, NULL, &msec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006046 if (rc)
6047 return rc;
6048 }
6049
Eric Paris50c205f2012-04-04 15:01:43 -04006050 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05006051 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052
6053 /* Can this process write to the queue? */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006054 rc = avc_has_perm(&selinux_state,
6055 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056 MSGQ__WRITE, &ad);
6057 if (!rc)
6058 /* Can this process send the message */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006059 rc = avc_has_perm(&selinux_state,
6060 sid, msec->sid, SECCLASS_MSG,
David Howells275bb412008-11-14 10:39:19 +11006061 MSG__SEND, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062 if (!rc)
6063 /* Can the message be put in the queue? */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006064 rc = avc_has_perm(&selinux_state,
6065 msec->sid, isec->sid, SECCLASS_MSGQ,
David Howells275bb412008-11-14 10:39:19 +11006066 MSGQ__ENQUEUE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067
6068 return rc;
6069}
6070
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05006071static int selinux_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072 struct task_struct *target,
6073 long type, int mode)
6074{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075 struct ipc_security_struct *isec;
6076 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006077 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006078 u32 sid = task_sid(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079 int rc;
6080
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05006081 isec = msq->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082 msec = msg->security;
6083
Eric Paris50c205f2012-04-04 15:01:43 -04006084 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05006085 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006087 rc = avc_has_perm(&selinux_state,
6088 sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006089 SECCLASS_MSGQ, MSGQ__READ, &ad);
6090 if (!rc)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006091 rc = avc_has_perm(&selinux_state,
6092 sid, msec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093 SECCLASS_MSG, MSG__RECEIVE, &ad);
6094 return rc;
6095}
6096
6097/* Shared Memory security operations */
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006098static int selinux_shm_alloc_security(struct kern_ipc_perm *shp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006100 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006101 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006102 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006103 int rc;
6104
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006105 rc = ipc_alloc_security(shp, SECCLASS_SHM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006106 if (rc)
6107 return rc;
6108
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006109 isec = shp->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006110
Eric Paris50c205f2012-04-04 15:01:43 -04006111 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006112 ad.u.ipc_id = shp->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006113
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006114 rc = avc_has_perm(&selinux_state,
6115 sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006116 SHM__CREATE, &ad);
6117 if (rc) {
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006118 ipc_free_security(shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006119 return rc;
6120 }
6121 return 0;
6122}
6123
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006124static void selinux_shm_free_security(struct kern_ipc_perm *shp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006125{
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006126 ipc_free_security(shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127}
6128
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006129static int selinux_shm_associate(struct kern_ipc_perm *shp, int shmflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006130{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006132 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006133 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006134
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006135 isec = shp->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006136
Eric Paris50c205f2012-04-04 15:01:43 -04006137 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006138 ad.u.ipc_id = shp->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006139
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006140 return avc_has_perm(&selinux_state,
6141 sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142 SHM__ASSOCIATE, &ad);
6143}
6144
6145/* Note, at this point, shp is locked down */
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006146static int selinux_shm_shmctl(struct kern_ipc_perm *shp, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147{
6148 int perms;
6149 int err;
6150
Eric Paris828dfe12008-04-17 13:17:49 -04006151 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006152 case IPC_INFO:
6153 case SHM_INFO:
6154 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006155 return avc_has_perm(&selinux_state,
6156 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006157 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006158 case IPC_STAT:
6159 case SHM_STAT:
Davidlohr Buesoc21a6972018-04-10 16:35:23 -07006160 case SHM_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006161 perms = SHM__GETATTR | SHM__ASSOCIATE;
6162 break;
6163 case IPC_SET:
6164 perms = SHM__SETATTR;
6165 break;
6166 case SHM_LOCK:
6167 case SHM_UNLOCK:
6168 perms = SHM__LOCK;
6169 break;
6170 case IPC_RMID:
6171 perms = SHM__DESTROY;
6172 break;
6173 default:
6174 return 0;
6175 }
6176
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006177 err = ipc_has_perm(shp, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006178 return err;
6179}
6180
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006181static int selinux_shm_shmat(struct kern_ipc_perm *shp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006182 char __user *shmaddr, int shmflg)
6183{
6184 u32 perms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006185
6186 if (shmflg & SHM_RDONLY)
6187 perms = SHM__READ;
6188 else
6189 perms = SHM__READ | SHM__WRITE;
6190
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006191 return ipc_has_perm(shp, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006192}
6193
6194/* Semaphore security operations */
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006195static int selinux_sem_alloc_security(struct kern_ipc_perm *sma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006196{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006197 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006198 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006199 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006200 int rc;
6201
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006202 rc = ipc_alloc_security(sma, SECCLASS_SEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006203 if (rc)
6204 return rc;
6205
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006206 isec = sma->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006207
Eric Paris50c205f2012-04-04 15:01:43 -04006208 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006209 ad.u.ipc_id = sma->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006211 rc = avc_has_perm(&selinux_state,
6212 sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006213 SEM__CREATE, &ad);
6214 if (rc) {
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006215 ipc_free_security(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006216 return rc;
6217 }
6218 return 0;
6219}
6220
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006221static void selinux_sem_free_security(struct kern_ipc_perm *sma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006222{
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006223 ipc_free_security(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006224}
6225
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006226static int selinux_sem_associate(struct kern_ipc_perm *sma, int semflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006227{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006228 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006229 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006230 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006231
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006232 isec = sma->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006233
Eric Paris50c205f2012-04-04 15:01:43 -04006234 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006235 ad.u.ipc_id = sma->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006236
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006237 return avc_has_perm(&selinux_state,
6238 sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006239 SEM__ASSOCIATE, &ad);
6240}
6241
6242/* Note, at this point, sma is locked down */
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006243static int selinux_sem_semctl(struct kern_ipc_perm *sma, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006244{
6245 int err;
6246 u32 perms;
6247
Eric Paris828dfe12008-04-17 13:17:49 -04006248 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249 case IPC_INFO:
6250 case SEM_INFO:
6251 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006252 return avc_has_perm(&selinux_state,
6253 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006254 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006255 case GETPID:
6256 case GETNCNT:
6257 case GETZCNT:
6258 perms = SEM__GETATTR;
6259 break;
6260 case GETVAL:
6261 case GETALL:
6262 perms = SEM__READ;
6263 break;
6264 case SETVAL:
6265 case SETALL:
6266 perms = SEM__WRITE;
6267 break;
6268 case IPC_RMID:
6269 perms = SEM__DESTROY;
6270 break;
6271 case IPC_SET:
6272 perms = SEM__SETATTR;
6273 break;
6274 case IPC_STAT:
6275 case SEM_STAT:
Davidlohr Buesoa280d6d2018-04-10 16:35:26 -07006276 case SEM_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006277 perms = SEM__GETATTR | SEM__ASSOCIATE;
6278 break;
6279 default:
6280 return 0;
6281 }
6282
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006283 err = ipc_has_perm(sma, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006284 return err;
6285}
6286
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006287static int selinux_sem_semop(struct kern_ipc_perm *sma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006288 struct sembuf *sops, unsigned nsops, int alter)
6289{
6290 u32 perms;
6291
6292 if (alter)
6293 perms = SEM__READ | SEM__WRITE;
6294 else
6295 perms = SEM__READ;
6296
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006297 return ipc_has_perm(sma, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006298}
6299
6300static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
6301{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006302 u32 av = 0;
6303
Linus Torvalds1da177e2005-04-16 15:20:36 -07006304 av = 0;
6305 if (flag & S_IRUGO)
6306 av |= IPC__UNIX_READ;
6307 if (flag & S_IWUGO)
6308 av |= IPC__UNIX_WRITE;
6309
6310 if (av == 0)
6311 return 0;
6312
Stephen Smalley6af963f2005-05-01 08:58:39 -07006313 return ipc_has_perm(ipcp, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006314}
6315
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02006316static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
6317{
6318 struct ipc_security_struct *isec = ipcp->security;
6319 *secid = isec->sid;
6320}
6321
Eric Paris828dfe12008-04-17 13:17:49 -04006322static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006323{
6324 if (inode)
6325 inode_doinit_with_dentry(inode, dentry);
6326}
6327
6328static int selinux_getprocattr(struct task_struct *p,
Al Viro04ff9702007-03-12 16:17:58 +00006329 char *name, char **value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330{
David Howells275bb412008-11-14 10:39:19 +11006331 const struct task_security_struct *__tsec;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00006332 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006333 int error;
Al Viro04ff9702007-03-12 16:17:58 +00006334 unsigned len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006335
David Howells275bb412008-11-14 10:39:19 +11006336 rcu_read_lock();
6337 __tsec = __task_cred(p)->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006338
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006339 if (current != p) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006340 error = avc_has_perm(&selinux_state,
6341 current_sid(), __tsec->sid,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006342 SECCLASS_PROCESS, PROCESS__GETATTR, NULL);
6343 if (error)
6344 goto bad;
6345 }
6346
Linus Torvalds1da177e2005-04-16 15:20:36 -07006347 if (!strcmp(name, "current"))
David Howells275bb412008-11-14 10:39:19 +11006348 sid = __tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006349 else if (!strcmp(name, "prev"))
David Howells275bb412008-11-14 10:39:19 +11006350 sid = __tsec->osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006351 else if (!strcmp(name, "exec"))
David Howells275bb412008-11-14 10:39:19 +11006352 sid = __tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006353 else if (!strcmp(name, "fscreate"))
David Howells275bb412008-11-14 10:39:19 +11006354 sid = __tsec->create_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006355 else if (!strcmp(name, "keycreate"))
David Howells275bb412008-11-14 10:39:19 +11006356 sid = __tsec->keycreate_sid;
Eric Paris42c3e032006-06-26 00:26:03 -07006357 else if (!strcmp(name, "sockcreate"))
David Howells275bb412008-11-14 10:39:19 +11006358 sid = __tsec->sockcreate_sid;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006359 else {
6360 error = -EINVAL;
6361 goto bad;
6362 }
David Howells275bb412008-11-14 10:39:19 +11006363 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006364
6365 if (!sid)
6366 return 0;
6367
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006368 error = security_sid_to_context(&selinux_state, sid, value, &len);
Al Viro04ff9702007-03-12 16:17:58 +00006369 if (error)
6370 return error;
6371 return len;
David Howells275bb412008-11-14 10:39:19 +11006372
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006373bad:
David Howells275bb412008-11-14 10:39:19 +11006374 rcu_read_unlock();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006375 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006376}
6377
Stephen Smalleyb21507e2017-01-09 10:07:31 -05006378static int selinux_setprocattr(const char *name, void *value, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006379{
6380 struct task_security_struct *tsec;
David Howellsd84f4f92008-11-14 10:39:23 +11006381 struct cred *new;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006382 u32 mysid = current_sid(), sid = 0, ptsid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006383 int error;
6384 char *str = value;
6385
Linus Torvalds1da177e2005-04-16 15:20:36 -07006386 /*
6387 * Basic control over ability to set these attributes at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006388 */
6389 if (!strcmp(name, "exec"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006390 error = avc_has_perm(&selinux_state,
6391 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006392 PROCESS__SETEXEC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006393 else if (!strcmp(name, "fscreate"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006394 error = avc_has_perm(&selinux_state,
6395 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006396 PROCESS__SETFSCREATE, NULL);
Michael LeMay4eb582c2006-06-26 00:24:57 -07006397 else if (!strcmp(name, "keycreate"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006398 error = avc_has_perm(&selinux_state,
6399 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006400 PROCESS__SETKEYCREATE, NULL);
Eric Paris42c3e032006-06-26 00:26:03 -07006401 else if (!strcmp(name, "sockcreate"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006402 error = avc_has_perm(&selinux_state,
6403 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006404 PROCESS__SETSOCKCREATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006405 else if (!strcmp(name, "current"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006406 error = avc_has_perm(&selinux_state,
6407 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006408 PROCESS__SETCURRENT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006409 else
6410 error = -EINVAL;
6411 if (error)
6412 return error;
6413
6414 /* Obtain a SID for the context, if one was specified. */
Stephen Smalleya050a572017-01-31 11:54:04 -05006415 if (size && str[0] && str[0] != '\n') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006416 if (str[size-1] == '\n') {
6417 str[size-1] = 0;
6418 size--;
6419 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006420 error = security_context_to_sid(&selinux_state, value, size,
6421 &sid, GFP_KERNEL);
Stephen Smalley12b29f32008-05-07 13:03:20 -04006422 if (error == -EINVAL && !strcmp(name, "fscreate")) {
Stephen Smalleydb590002017-04-20 11:31:30 -04006423 if (!has_cap_mac_admin(true)) {
Eric Parisd6ea83e2012-04-04 13:45:49 -04006424 struct audit_buffer *ab;
6425 size_t audit_size;
6426
6427 /* We strip a nul only if it is at the end, otherwise the
6428 * context contains a nul and we should audit that */
6429 if (str[size - 1] == '\0')
6430 audit_size = size - 1;
6431 else
6432 audit_size = size;
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04006433 ab = audit_log_start(audit_context(),
6434 GFP_ATOMIC,
6435 AUDIT_SELINUX_ERR);
Eric Parisd6ea83e2012-04-04 13:45:49 -04006436 audit_log_format(ab, "op=fscreate invalid_context=");
6437 audit_log_n_untrustedstring(ab, value, audit_size);
6438 audit_log_end(ab);
6439
Stephen Smalley12b29f32008-05-07 13:03:20 -04006440 return error;
Eric Parisd6ea83e2012-04-04 13:45:49 -04006441 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006442 error = security_context_to_sid_force(
6443 &selinux_state,
6444 value, size, &sid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04006445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006446 if (error)
6447 return error;
6448 }
6449
David Howellsd84f4f92008-11-14 10:39:23 +11006450 new = prepare_creds();
6451 if (!new)
6452 return -ENOMEM;
6453
Linus Torvalds1da177e2005-04-16 15:20:36 -07006454 /* Permission checking based on the specified context is
6455 performed during the actual operation (execve,
6456 open/mkdir/...), when we know the full context of the
David Howellsd84f4f92008-11-14 10:39:23 +11006457 operation. See selinux_bprm_set_creds for the execve
Linus Torvalds1da177e2005-04-16 15:20:36 -07006458 checks and may_create for the file creation checks. The
6459 operation will then fail if the context is not permitted. */
David Howellsd84f4f92008-11-14 10:39:23 +11006460 tsec = new->security;
6461 if (!strcmp(name, "exec")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006462 tsec->exec_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006463 } else if (!strcmp(name, "fscreate")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006464 tsec->create_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006465 } else if (!strcmp(name, "keycreate")) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006466 error = avc_has_perm(&selinux_state,
6467 mysid, sid, SECCLASS_KEY, KEY__CREATE,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006468 NULL);
Michael LeMay4eb582c2006-06-26 00:24:57 -07006469 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11006470 goto abort_change;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006471 tsec->keycreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006472 } else if (!strcmp(name, "sockcreate")) {
Eric Paris42c3e032006-06-26 00:26:03 -07006473 tsec->sockcreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006474 } else if (!strcmp(name, "current")) {
6475 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006476 if (sid == 0)
David Howellsd84f4f92008-11-14 10:39:23 +11006477 goto abort_change;
KaiGai Koheid9250de2008-08-28 16:35:57 +09006478
David Howellsd84f4f92008-11-14 10:39:23 +11006479 /* Only allow single threaded processes to change context */
6480 error = -EPERM;
Oleg Nesterov5bb459b2009-07-10 03:48:23 +02006481 if (!current_is_single_threaded()) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006482 error = security_bounded_transition(&selinux_state,
6483 tsec->sid, sid);
David Howellsd84f4f92008-11-14 10:39:23 +11006484 if (error)
6485 goto abort_change;
Eric Paris828dfe12008-04-17 13:17:49 -04006486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006487
6488 /* Check permissions for the transition. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006489 error = avc_has_perm(&selinux_state,
6490 tsec->sid, sid, SECCLASS_PROCESS,
Eric Paris828dfe12008-04-17 13:17:49 -04006491 PROCESS__DYNTRANSITION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006492 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11006493 goto abort_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006494
6495 /* Check for ptracing, and update the task SID if ok.
6496 Otherwise, leave SID unchanged and fail. */
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006497 ptsid = ptrace_parent_sid();
Paul Moore0c6181c2016-03-30 21:41:21 -04006498 if (ptsid != 0) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006499 error = avc_has_perm(&selinux_state,
6500 ptsid, sid, SECCLASS_PROCESS,
David Howellsd84f4f92008-11-14 10:39:23 +11006501 PROCESS__PTRACE, NULL);
6502 if (error)
6503 goto abort_change;
6504 }
6505
6506 tsec->sid = sid;
6507 } else {
6508 error = -EINVAL;
6509 goto abort_change;
6510 }
6511
6512 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006513 return size;
David Howellsd84f4f92008-11-14 10:39:23 +11006514
6515abort_change:
6516 abort_creds(new);
6517 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006518}
6519
David Quigley746df9b2013-05-22 12:50:35 -04006520static int selinux_ismaclabel(const char *name)
6521{
6522 return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0);
6523}
6524
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006525static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
6526{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006527 return security_sid_to_context(&selinux_state, secid,
6528 secdata, seclen);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006529}
6530
David Howells7bf570d2008-04-29 20:52:51 +01006531static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
David Howells63cb3442008-01-15 23:47:35 +00006532{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006533 return security_context_to_sid(&selinux_state, secdata, seclen,
6534 secid, GFP_KERNEL);
David Howells63cb3442008-01-15 23:47:35 +00006535}
6536
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006537static void selinux_release_secctx(char *secdata, u32 seclen)
6538{
Paul Moore088999e2007-08-01 11:12:58 -04006539 kfree(secdata);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006540}
6541
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006542static void selinux_inode_invalidate_secctx(struct inode *inode)
6543{
6544 struct inode_security_struct *isec = inode->i_security;
6545
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01006546 spin_lock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006547 isec->initialized = LABEL_INVALID;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01006548 spin_unlock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006549}
6550
David P. Quigley1ee65e32009-09-03 14:25:57 -04006551/*
6552 * called with inode->i_mutex locked
6553 */
6554static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
6555{
6556 return selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, ctx, ctxlen, 0);
6557}
6558
6559/*
6560 * called with inode->i_mutex locked
6561 */
6562static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
6563{
6564 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0);
6565}
6566
6567static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
6568{
6569 int len = 0;
6570 len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX,
6571 ctx, true);
6572 if (len < 0)
6573 return len;
6574 *ctxlen = len;
6575 return 0;
6576}
Michael LeMayd7200242006-06-22 14:47:17 -07006577#ifdef CONFIG_KEYS
6578
David Howellsd84f4f92008-11-14 10:39:23 +11006579static int selinux_key_alloc(struct key *k, const struct cred *cred,
David Howells7e047ef2006-06-26 00:24:50 -07006580 unsigned long flags)
Michael LeMayd7200242006-06-22 14:47:17 -07006581{
David Howellsd84f4f92008-11-14 10:39:23 +11006582 const struct task_security_struct *tsec;
Michael LeMayd7200242006-06-22 14:47:17 -07006583 struct key_security_struct *ksec;
6584
6585 ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
6586 if (!ksec)
6587 return -ENOMEM;
6588
David Howellsd84f4f92008-11-14 10:39:23 +11006589 tsec = cred->security;
6590 if (tsec->keycreate_sid)
6591 ksec->sid = tsec->keycreate_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006592 else
David Howellsd84f4f92008-11-14 10:39:23 +11006593 ksec->sid = tsec->sid;
Michael LeMayd7200242006-06-22 14:47:17 -07006594
David Howells275bb412008-11-14 10:39:19 +11006595 k->security = ksec;
Michael LeMayd7200242006-06-22 14:47:17 -07006596 return 0;
6597}
6598
6599static void selinux_key_free(struct key *k)
6600{
6601 struct key_security_struct *ksec = k->security;
6602
6603 k->security = NULL;
6604 kfree(ksec);
6605}
6606
6607static int selinux_key_permission(key_ref_t key_ref,
David Howellsd84f4f92008-11-14 10:39:23 +11006608 const struct cred *cred,
David Howellsf5895942014-03-14 17:44:49 +00006609 unsigned perm)
Michael LeMayd7200242006-06-22 14:47:17 -07006610{
6611 struct key *key;
Michael LeMayd7200242006-06-22 14:47:17 -07006612 struct key_security_struct *ksec;
David Howells275bb412008-11-14 10:39:19 +11006613 u32 sid;
Michael LeMayd7200242006-06-22 14:47:17 -07006614
6615 /* if no specific permissions are requested, we skip the
6616 permission check. No serious, additional covert channels
6617 appear to be created. */
6618 if (perm == 0)
6619 return 0;
6620
David Howellsd84f4f92008-11-14 10:39:23 +11006621 sid = cred_sid(cred);
David Howells275bb412008-11-14 10:39:19 +11006622
6623 key = key_ref_to_ptr(key_ref);
6624 ksec = key->security;
6625
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006626 return avc_has_perm(&selinux_state,
6627 sid, ksec->sid, SECCLASS_KEY, perm, NULL);
Michael LeMayd7200242006-06-22 14:47:17 -07006628}
6629
David Howells70a5bb72008-04-29 01:01:26 -07006630static int selinux_key_getsecurity(struct key *key, char **_buffer)
6631{
6632 struct key_security_struct *ksec = key->security;
6633 char *context = NULL;
6634 unsigned len;
6635 int rc;
6636
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006637 rc = security_sid_to_context(&selinux_state, ksec->sid,
6638 &context, &len);
David Howells70a5bb72008-04-29 01:01:26 -07006639 if (!rc)
6640 rc = len;
6641 *_buffer = context;
6642 return rc;
6643}
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006644#endif
David Howells70a5bb72008-04-29 01:01:26 -07006645
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006646#ifdef CONFIG_SECURITY_INFINIBAND
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006647static int selinux_ib_pkey_access(void *ib_sec, u64 subnet_prefix, u16 pkey_val)
6648{
6649 struct common_audit_data ad;
6650 int err;
6651 u32 sid = 0;
6652 struct ib_security_struct *sec = ib_sec;
6653 struct lsm_ibpkey_audit ibpkey;
6654
Daniel Jurgens409dcf32017-05-19 15:48:59 +03006655 err = sel_ib_pkey_sid(subnet_prefix, pkey_val, &sid);
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006656 if (err)
6657 return err;
6658
6659 ad.type = LSM_AUDIT_DATA_IBPKEY;
6660 ibpkey.subnet_prefix = subnet_prefix;
6661 ibpkey.pkey = pkey_val;
6662 ad.u.ibpkey = &ibpkey;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006663 return avc_has_perm(&selinux_state,
6664 sec->sid, sid,
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006665 SECCLASS_INFINIBAND_PKEY,
6666 INFINIBAND_PKEY__ACCESS, &ad);
6667}
6668
Daniel Jurgensab861df2017-05-19 15:48:58 +03006669static int selinux_ib_endport_manage_subnet(void *ib_sec, const char *dev_name,
6670 u8 port_num)
6671{
6672 struct common_audit_data ad;
6673 int err;
6674 u32 sid = 0;
6675 struct ib_security_struct *sec = ib_sec;
6676 struct lsm_ibendport_audit ibendport;
6677
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006678 err = security_ib_endport_sid(&selinux_state, dev_name, port_num,
6679 &sid);
Daniel Jurgensab861df2017-05-19 15:48:58 +03006680
6681 if (err)
6682 return err;
6683
6684 ad.type = LSM_AUDIT_DATA_IBENDPORT;
6685 strncpy(ibendport.dev_name, dev_name, sizeof(ibendport.dev_name));
6686 ibendport.port = port_num;
6687 ad.u.ibendport = &ibendport;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006688 return avc_has_perm(&selinux_state,
6689 sec->sid, sid,
Daniel Jurgensab861df2017-05-19 15:48:58 +03006690 SECCLASS_INFINIBAND_ENDPORT,
6691 INFINIBAND_ENDPORT__MANAGE_SUBNET, &ad);
6692}
6693
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006694static int selinux_ib_alloc_security(void **ib_sec)
6695{
6696 struct ib_security_struct *sec;
6697
6698 sec = kzalloc(sizeof(*sec), GFP_KERNEL);
6699 if (!sec)
6700 return -ENOMEM;
6701 sec->sid = current_sid();
6702
6703 *ib_sec = sec;
6704 return 0;
6705}
6706
6707static void selinux_ib_free_security(void *ib_sec)
6708{
6709 kfree(ib_sec);
6710}
Michael LeMayd7200242006-06-22 14:47:17 -07006711#endif
6712
Chenbo Fengec27c352017-10-18 13:00:25 -07006713#ifdef CONFIG_BPF_SYSCALL
6714static int selinux_bpf(int cmd, union bpf_attr *attr,
6715 unsigned int size)
6716{
6717 u32 sid = current_sid();
6718 int ret;
6719
6720 switch (cmd) {
6721 case BPF_MAP_CREATE:
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006722 ret = avc_has_perm(&selinux_state,
6723 sid, sid, SECCLASS_BPF, BPF__MAP_CREATE,
Chenbo Fengec27c352017-10-18 13:00:25 -07006724 NULL);
6725 break;
6726 case BPF_PROG_LOAD:
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006727 ret = avc_has_perm(&selinux_state,
6728 sid, sid, SECCLASS_BPF, BPF__PROG_LOAD,
Chenbo Fengec27c352017-10-18 13:00:25 -07006729 NULL);
6730 break;
6731 default:
6732 ret = 0;
6733 break;
6734 }
6735
6736 return ret;
6737}
6738
6739static u32 bpf_map_fmode_to_av(fmode_t fmode)
6740{
6741 u32 av = 0;
6742
6743 if (fmode & FMODE_READ)
6744 av |= BPF__MAP_READ;
6745 if (fmode & FMODE_WRITE)
6746 av |= BPF__MAP_WRITE;
6747 return av;
6748}
6749
Chenbo Fengf66e4482017-10-18 13:00:26 -07006750/* This function will check the file pass through unix socket or binder to see
6751 * if it is a bpf related object. And apply correspinding checks on the bpf
6752 * object based on the type. The bpf maps and programs, not like other files and
6753 * socket, are using a shared anonymous inode inside the kernel as their inode.
6754 * So checking that inode cannot identify if the process have privilege to
6755 * access the bpf object and that's why we have to add this additional check in
6756 * selinux_file_receive and selinux_binder_transfer_files.
6757 */
6758static int bpf_fd_pass(struct file *file, u32 sid)
6759{
6760 struct bpf_security_struct *bpfsec;
6761 struct bpf_prog *prog;
6762 struct bpf_map *map;
6763 int ret;
6764
6765 if (file->f_op == &bpf_map_fops) {
6766 map = file->private_data;
6767 bpfsec = map->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006768 ret = avc_has_perm(&selinux_state,
6769 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengf66e4482017-10-18 13:00:26 -07006770 bpf_map_fmode_to_av(file->f_mode), NULL);
6771 if (ret)
6772 return ret;
6773 } else if (file->f_op == &bpf_prog_fops) {
6774 prog = file->private_data;
6775 bpfsec = prog->aux->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006776 ret = avc_has_perm(&selinux_state,
6777 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengf66e4482017-10-18 13:00:26 -07006778 BPF__PROG_RUN, NULL);
6779 if (ret)
6780 return ret;
6781 }
6782 return 0;
6783}
6784
Chenbo Fengec27c352017-10-18 13:00:25 -07006785static int selinux_bpf_map(struct bpf_map *map, fmode_t fmode)
6786{
6787 u32 sid = current_sid();
6788 struct bpf_security_struct *bpfsec;
6789
6790 bpfsec = map->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006791 return avc_has_perm(&selinux_state,
6792 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengec27c352017-10-18 13:00:25 -07006793 bpf_map_fmode_to_av(fmode), NULL);
6794}
6795
6796static int selinux_bpf_prog(struct bpf_prog *prog)
6797{
6798 u32 sid = current_sid();
6799 struct bpf_security_struct *bpfsec;
6800
6801 bpfsec = prog->aux->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006802 return avc_has_perm(&selinux_state,
6803 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengec27c352017-10-18 13:00:25 -07006804 BPF__PROG_RUN, NULL);
6805}
6806
6807static int selinux_bpf_map_alloc(struct bpf_map *map)
6808{
6809 struct bpf_security_struct *bpfsec;
6810
6811 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6812 if (!bpfsec)
6813 return -ENOMEM;
6814
6815 bpfsec->sid = current_sid();
6816 map->security = bpfsec;
6817
6818 return 0;
6819}
6820
6821static void selinux_bpf_map_free(struct bpf_map *map)
6822{
6823 struct bpf_security_struct *bpfsec = map->security;
6824
6825 map->security = NULL;
6826 kfree(bpfsec);
6827}
6828
6829static int selinux_bpf_prog_alloc(struct bpf_prog_aux *aux)
6830{
6831 struct bpf_security_struct *bpfsec;
6832
6833 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6834 if (!bpfsec)
6835 return -ENOMEM;
6836
6837 bpfsec->sid = current_sid();
6838 aux->security = bpfsec;
6839
6840 return 0;
6841}
6842
6843static void selinux_bpf_prog_free(struct bpf_prog_aux *aux)
6844{
6845 struct bpf_security_struct *bpfsec = aux->security;
6846
6847 aux->security = NULL;
6848 kfree(bpfsec);
6849}
6850#endif
6851
James Morrisca97d932017-02-15 00:18:51 +11006852static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {
Casey Schauflere20b0432015-05-02 15:11:36 -07006853 LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr),
6854 LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction),
6855 LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder),
6856 LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file),
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02006857
Casey Schauflere20b0432015-05-02 15:11:36 -07006858 LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check),
6859 LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme),
6860 LSM_HOOK_INIT(capget, selinux_capget),
6861 LSM_HOOK_INIT(capset, selinux_capset),
6862 LSM_HOOK_INIT(capable, selinux_capable),
6863 LSM_HOOK_INIT(quotactl, selinux_quotactl),
6864 LSM_HOOK_INIT(quota_on, selinux_quota_on),
6865 LSM_HOOK_INIT(syslog, selinux_syslog),
6866 LSM_HOOK_INIT(vm_enough_memory, selinux_vm_enough_memory),
Stephen Smalley79af7302015-01-21 10:54:10 -05006867
Casey Schauflere20b0432015-05-02 15:11:36 -07006868 LSM_HOOK_INIT(netlink_send, selinux_netlink_send),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006869
Casey Schauflere20b0432015-05-02 15:11:36 -07006870 LSM_HOOK_INIT(bprm_set_creds, selinux_bprm_set_creds),
6871 LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds),
6872 LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006873
Casey Schauflere20b0432015-05-02 15:11:36 -07006874 LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security),
6875 LSM_HOOK_INIT(sb_free_security, selinux_sb_free_security),
6876 LSM_HOOK_INIT(sb_copy_data, selinux_sb_copy_data),
6877 LSM_HOOK_INIT(sb_remount, selinux_sb_remount),
6878 LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount),
6879 LSM_HOOK_INIT(sb_show_options, selinux_sb_show_options),
6880 LSM_HOOK_INIT(sb_statfs, selinux_sb_statfs),
6881 LSM_HOOK_INIT(sb_mount, selinux_mount),
6882 LSM_HOOK_INIT(sb_umount, selinux_umount),
6883 LSM_HOOK_INIT(sb_set_mnt_opts, selinux_set_mnt_opts),
6884 LSM_HOOK_INIT(sb_clone_mnt_opts, selinux_sb_clone_mnt_opts),
6885 LSM_HOOK_INIT(sb_parse_opts_str, selinux_parse_opts_str),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006886
Casey Schauflere20b0432015-05-02 15:11:36 -07006887 LSM_HOOK_INIT(dentry_init_security, selinux_dentry_init_security),
Vivek Goyala518b0a2016-07-13 10:44:53 -04006888 LSM_HOOK_INIT(dentry_create_files_as, selinux_dentry_create_files_as),
Eric Parise0007522008-03-05 10:31:54 -05006889
Casey Schauflere20b0432015-05-02 15:11:36 -07006890 LSM_HOOK_INIT(inode_alloc_security, selinux_inode_alloc_security),
6891 LSM_HOOK_INIT(inode_free_security, selinux_inode_free_security),
6892 LSM_HOOK_INIT(inode_init_security, selinux_inode_init_security),
6893 LSM_HOOK_INIT(inode_create, selinux_inode_create),
6894 LSM_HOOK_INIT(inode_link, selinux_inode_link),
6895 LSM_HOOK_INIT(inode_unlink, selinux_inode_unlink),
6896 LSM_HOOK_INIT(inode_symlink, selinux_inode_symlink),
6897 LSM_HOOK_INIT(inode_mkdir, selinux_inode_mkdir),
6898 LSM_HOOK_INIT(inode_rmdir, selinux_inode_rmdir),
6899 LSM_HOOK_INIT(inode_mknod, selinux_inode_mknod),
6900 LSM_HOOK_INIT(inode_rename, selinux_inode_rename),
6901 LSM_HOOK_INIT(inode_readlink, selinux_inode_readlink),
6902 LSM_HOOK_INIT(inode_follow_link, selinux_inode_follow_link),
6903 LSM_HOOK_INIT(inode_permission, selinux_inode_permission),
6904 LSM_HOOK_INIT(inode_setattr, selinux_inode_setattr),
6905 LSM_HOOK_INIT(inode_getattr, selinux_inode_getattr),
6906 LSM_HOOK_INIT(inode_setxattr, selinux_inode_setxattr),
6907 LSM_HOOK_INIT(inode_post_setxattr, selinux_inode_post_setxattr),
6908 LSM_HOOK_INIT(inode_getxattr, selinux_inode_getxattr),
6909 LSM_HOOK_INIT(inode_listxattr, selinux_inode_listxattr),
6910 LSM_HOOK_INIT(inode_removexattr, selinux_inode_removexattr),
6911 LSM_HOOK_INIT(inode_getsecurity, selinux_inode_getsecurity),
6912 LSM_HOOK_INIT(inode_setsecurity, selinux_inode_setsecurity),
6913 LSM_HOOK_INIT(inode_listsecurity, selinux_inode_listsecurity),
6914 LSM_HOOK_INIT(inode_getsecid, selinux_inode_getsecid),
Vivek Goyal56909eb2016-07-13 10:44:48 -04006915 LSM_HOOK_INIT(inode_copy_up, selinux_inode_copy_up),
Vivek Goyal19472b62016-07-13 10:44:50 -04006916 LSM_HOOK_INIT(inode_copy_up_xattr, selinux_inode_copy_up_xattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006917
Casey Schauflere20b0432015-05-02 15:11:36 -07006918 LSM_HOOK_INIT(file_permission, selinux_file_permission),
6919 LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security),
6920 LSM_HOOK_INIT(file_free_security, selinux_file_free_security),
6921 LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl),
6922 LSM_HOOK_INIT(mmap_file, selinux_mmap_file),
6923 LSM_HOOK_INIT(mmap_addr, selinux_mmap_addr),
6924 LSM_HOOK_INIT(file_mprotect, selinux_file_mprotect),
6925 LSM_HOOK_INIT(file_lock, selinux_file_lock),
6926 LSM_HOOK_INIT(file_fcntl, selinux_file_fcntl),
6927 LSM_HOOK_INIT(file_set_fowner, selinux_file_set_fowner),
6928 LSM_HOOK_INIT(file_send_sigiotask, selinux_file_send_sigiotask),
6929 LSM_HOOK_INIT(file_receive, selinux_file_receive),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006930
Casey Schauflere20b0432015-05-02 15:11:36 -07006931 LSM_HOOK_INIT(file_open, selinux_file_open),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006932
Tetsuo Handaa79be232017-03-28 23:08:45 +09006933 LSM_HOOK_INIT(task_alloc, selinux_task_alloc),
Casey Schauflere20b0432015-05-02 15:11:36 -07006934 LSM_HOOK_INIT(cred_alloc_blank, selinux_cred_alloc_blank),
6935 LSM_HOOK_INIT(cred_free, selinux_cred_free),
6936 LSM_HOOK_INIT(cred_prepare, selinux_cred_prepare),
6937 LSM_HOOK_INIT(cred_transfer, selinux_cred_transfer),
Matthew Garrett3ec30112018-01-08 13:36:19 -08006938 LSM_HOOK_INIT(cred_getsecid, selinux_cred_getsecid),
Casey Schauflere20b0432015-05-02 15:11:36 -07006939 LSM_HOOK_INIT(kernel_act_as, selinux_kernel_act_as),
6940 LSM_HOOK_INIT(kernel_create_files_as, selinux_kernel_create_files_as),
6941 LSM_HOOK_INIT(kernel_module_request, selinux_kernel_module_request),
Mimi Zoharc77b8cd2018-07-13 14:06:02 -04006942 LSM_HOOK_INIT(kernel_load_data, selinux_kernel_load_data),
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07006943 LSM_HOOK_INIT(kernel_read_file, selinux_kernel_read_file),
Casey Schauflere20b0432015-05-02 15:11:36 -07006944 LSM_HOOK_INIT(task_setpgid, selinux_task_setpgid),
6945 LSM_HOOK_INIT(task_getpgid, selinux_task_getpgid),
6946 LSM_HOOK_INIT(task_getsid, selinux_task_getsid),
6947 LSM_HOOK_INIT(task_getsecid, selinux_task_getsecid),
6948 LSM_HOOK_INIT(task_setnice, selinux_task_setnice),
6949 LSM_HOOK_INIT(task_setioprio, selinux_task_setioprio),
6950 LSM_HOOK_INIT(task_getioprio, selinux_task_getioprio),
Stephen Smalley791ec492017-02-17 07:57:00 -05006951 LSM_HOOK_INIT(task_prlimit, selinux_task_prlimit),
Casey Schauflere20b0432015-05-02 15:11:36 -07006952 LSM_HOOK_INIT(task_setrlimit, selinux_task_setrlimit),
6953 LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler),
6954 LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler),
6955 LSM_HOOK_INIT(task_movememory, selinux_task_movememory),
6956 LSM_HOOK_INIT(task_kill, selinux_task_kill),
Casey Schauflere20b0432015-05-02 15:11:36 -07006957 LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode),
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09006958
Casey Schauflere20b0432015-05-02 15:11:36 -07006959 LSM_HOOK_INIT(ipc_permission, selinux_ipc_permission),
6960 LSM_HOOK_INIT(ipc_getsecid, selinux_ipc_getsecid),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006961
Casey Schauflere20b0432015-05-02 15:11:36 -07006962 LSM_HOOK_INIT(msg_msg_alloc_security, selinux_msg_msg_alloc_security),
6963 LSM_HOOK_INIT(msg_msg_free_security, selinux_msg_msg_free_security),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006964
Casey Schauflere20b0432015-05-02 15:11:36 -07006965 LSM_HOOK_INIT(msg_queue_alloc_security,
6966 selinux_msg_queue_alloc_security),
6967 LSM_HOOK_INIT(msg_queue_free_security, selinux_msg_queue_free_security),
6968 LSM_HOOK_INIT(msg_queue_associate, selinux_msg_queue_associate),
6969 LSM_HOOK_INIT(msg_queue_msgctl, selinux_msg_queue_msgctl),
6970 LSM_HOOK_INIT(msg_queue_msgsnd, selinux_msg_queue_msgsnd),
6971 LSM_HOOK_INIT(msg_queue_msgrcv, selinux_msg_queue_msgrcv),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006972
Casey Schauflere20b0432015-05-02 15:11:36 -07006973 LSM_HOOK_INIT(shm_alloc_security, selinux_shm_alloc_security),
6974 LSM_HOOK_INIT(shm_free_security, selinux_shm_free_security),
6975 LSM_HOOK_INIT(shm_associate, selinux_shm_associate),
6976 LSM_HOOK_INIT(shm_shmctl, selinux_shm_shmctl),
6977 LSM_HOOK_INIT(shm_shmat, selinux_shm_shmat),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006978
Casey Schauflere20b0432015-05-02 15:11:36 -07006979 LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security),
6980 LSM_HOOK_INIT(sem_free_security, selinux_sem_free_security),
6981 LSM_HOOK_INIT(sem_associate, selinux_sem_associate),
6982 LSM_HOOK_INIT(sem_semctl, selinux_sem_semctl),
6983 LSM_HOOK_INIT(sem_semop, selinux_sem_semop),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006984
Casey Schauflere20b0432015-05-02 15:11:36 -07006985 LSM_HOOK_INIT(d_instantiate, selinux_d_instantiate),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006986
Casey Schauflere20b0432015-05-02 15:11:36 -07006987 LSM_HOOK_INIT(getprocattr, selinux_getprocattr),
6988 LSM_HOOK_INIT(setprocattr, selinux_setprocattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006989
Casey Schauflere20b0432015-05-02 15:11:36 -07006990 LSM_HOOK_INIT(ismaclabel, selinux_ismaclabel),
6991 LSM_HOOK_INIT(secid_to_secctx, selinux_secid_to_secctx),
6992 LSM_HOOK_INIT(secctx_to_secid, selinux_secctx_to_secid),
6993 LSM_HOOK_INIT(release_secctx, selinux_release_secctx),
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006994 LSM_HOOK_INIT(inode_invalidate_secctx, selinux_inode_invalidate_secctx),
Casey Schauflere20b0432015-05-02 15:11:36 -07006995 LSM_HOOK_INIT(inode_notifysecctx, selinux_inode_notifysecctx),
6996 LSM_HOOK_INIT(inode_setsecctx, selinux_inode_setsecctx),
6997 LSM_HOOK_INIT(inode_getsecctx, selinux_inode_getsecctx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006998
Casey Schauflere20b0432015-05-02 15:11:36 -07006999 LSM_HOOK_INIT(unix_stream_connect, selinux_socket_unix_stream_connect),
7000 LSM_HOOK_INIT(unix_may_send, selinux_socket_unix_may_send),
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07007001
Casey Schauflere20b0432015-05-02 15:11:36 -07007002 LSM_HOOK_INIT(socket_create, selinux_socket_create),
7003 LSM_HOOK_INIT(socket_post_create, selinux_socket_post_create),
David Herrmann0b811db2018-05-04 16:28:21 +02007004 LSM_HOOK_INIT(socket_socketpair, selinux_socket_socketpair),
Casey Schauflere20b0432015-05-02 15:11:36 -07007005 LSM_HOOK_INIT(socket_bind, selinux_socket_bind),
7006 LSM_HOOK_INIT(socket_connect, selinux_socket_connect),
7007 LSM_HOOK_INIT(socket_listen, selinux_socket_listen),
7008 LSM_HOOK_INIT(socket_accept, selinux_socket_accept),
7009 LSM_HOOK_INIT(socket_sendmsg, selinux_socket_sendmsg),
7010 LSM_HOOK_INIT(socket_recvmsg, selinux_socket_recvmsg),
7011 LSM_HOOK_INIT(socket_getsockname, selinux_socket_getsockname),
7012 LSM_HOOK_INIT(socket_getpeername, selinux_socket_getpeername),
7013 LSM_HOOK_INIT(socket_getsockopt, selinux_socket_getsockopt),
7014 LSM_HOOK_INIT(socket_setsockopt, selinux_socket_setsockopt),
7015 LSM_HOOK_INIT(socket_shutdown, selinux_socket_shutdown),
7016 LSM_HOOK_INIT(socket_sock_rcv_skb, selinux_socket_sock_rcv_skb),
7017 LSM_HOOK_INIT(socket_getpeersec_stream,
7018 selinux_socket_getpeersec_stream),
7019 LSM_HOOK_INIT(socket_getpeersec_dgram, selinux_socket_getpeersec_dgram),
7020 LSM_HOOK_INIT(sk_alloc_security, selinux_sk_alloc_security),
7021 LSM_HOOK_INIT(sk_free_security, selinux_sk_free_security),
7022 LSM_HOOK_INIT(sk_clone_security, selinux_sk_clone_security),
7023 LSM_HOOK_INIT(sk_getsecid, selinux_sk_getsecid),
7024 LSM_HOOK_INIT(sock_graft, selinux_sock_graft),
Richard Hainesd4529302018-02-13 20:57:18 +00007025 LSM_HOOK_INIT(sctp_assoc_request, selinux_sctp_assoc_request),
7026 LSM_HOOK_INIT(sctp_sk_clone, selinux_sctp_sk_clone),
7027 LSM_HOOK_INIT(sctp_bind_connect, selinux_sctp_bind_connect),
Casey Schauflere20b0432015-05-02 15:11:36 -07007028 LSM_HOOK_INIT(inet_conn_request, selinux_inet_conn_request),
7029 LSM_HOOK_INIT(inet_csk_clone, selinux_inet_csk_clone),
7030 LSM_HOOK_INIT(inet_conn_established, selinux_inet_conn_established),
7031 LSM_HOOK_INIT(secmark_relabel_packet, selinux_secmark_relabel_packet),
7032 LSM_HOOK_INIT(secmark_refcount_inc, selinux_secmark_refcount_inc),
7033 LSM_HOOK_INIT(secmark_refcount_dec, selinux_secmark_refcount_dec),
7034 LSM_HOOK_INIT(req_classify_flow, selinux_req_classify_flow),
7035 LSM_HOOK_INIT(tun_dev_alloc_security, selinux_tun_dev_alloc_security),
7036 LSM_HOOK_INIT(tun_dev_free_security, selinux_tun_dev_free_security),
7037 LSM_HOOK_INIT(tun_dev_create, selinux_tun_dev_create),
7038 LSM_HOOK_INIT(tun_dev_attach_queue, selinux_tun_dev_attach_queue),
7039 LSM_HOOK_INIT(tun_dev_attach, selinux_tun_dev_attach),
7040 LSM_HOOK_INIT(tun_dev_open, selinux_tun_dev_open),
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03007041#ifdef CONFIG_SECURITY_INFINIBAND
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03007042 LSM_HOOK_INIT(ib_pkey_access, selinux_ib_pkey_access),
Daniel Jurgensab861df2017-05-19 15:48:58 +03007043 LSM_HOOK_INIT(ib_endport_manage_subnet,
7044 selinux_ib_endport_manage_subnet),
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03007045 LSM_HOOK_INIT(ib_alloc_security, selinux_ib_alloc_security),
7046 LSM_HOOK_INIT(ib_free_security, selinux_ib_free_security),
7047#endif
Trent Jaegerd28d1e02005-12-13 23:12:40 -08007048#ifdef CONFIG_SECURITY_NETWORK_XFRM
Casey Schauflere20b0432015-05-02 15:11:36 -07007049 LSM_HOOK_INIT(xfrm_policy_alloc_security, selinux_xfrm_policy_alloc),
7050 LSM_HOOK_INIT(xfrm_policy_clone_security, selinux_xfrm_policy_clone),
7051 LSM_HOOK_INIT(xfrm_policy_free_security, selinux_xfrm_policy_free),
7052 LSM_HOOK_INIT(xfrm_policy_delete_security, selinux_xfrm_policy_delete),
7053 LSM_HOOK_INIT(xfrm_state_alloc, selinux_xfrm_state_alloc),
7054 LSM_HOOK_INIT(xfrm_state_alloc_acquire,
7055 selinux_xfrm_state_alloc_acquire),
7056 LSM_HOOK_INIT(xfrm_state_free_security, selinux_xfrm_state_free),
7057 LSM_HOOK_INIT(xfrm_state_delete_security, selinux_xfrm_state_delete),
7058 LSM_HOOK_INIT(xfrm_policy_lookup, selinux_xfrm_policy_lookup),
7059 LSM_HOOK_INIT(xfrm_state_pol_flow_match,
7060 selinux_xfrm_state_pol_flow_match),
7061 LSM_HOOK_INIT(xfrm_decode_session, selinux_xfrm_decode_session),
Linus Torvalds1da177e2005-04-16 15:20:36 -07007062#endif
Michael LeMayd7200242006-06-22 14:47:17 -07007063
7064#ifdef CONFIG_KEYS
Casey Schauflere20b0432015-05-02 15:11:36 -07007065 LSM_HOOK_INIT(key_alloc, selinux_key_alloc),
7066 LSM_HOOK_INIT(key_free, selinux_key_free),
7067 LSM_HOOK_INIT(key_permission, selinux_key_permission),
7068 LSM_HOOK_INIT(key_getsecurity, selinux_key_getsecurity),
Michael LeMayd7200242006-06-22 14:47:17 -07007069#endif
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02007070
7071#ifdef CONFIG_AUDIT
Casey Schauflere20b0432015-05-02 15:11:36 -07007072 LSM_HOOK_INIT(audit_rule_init, selinux_audit_rule_init),
7073 LSM_HOOK_INIT(audit_rule_known, selinux_audit_rule_known),
7074 LSM_HOOK_INIT(audit_rule_match, selinux_audit_rule_match),
7075 LSM_HOOK_INIT(audit_rule_free, selinux_audit_rule_free),
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02007076#endif
Chenbo Fengec27c352017-10-18 13:00:25 -07007077
7078#ifdef CONFIG_BPF_SYSCALL
7079 LSM_HOOK_INIT(bpf, selinux_bpf),
7080 LSM_HOOK_INIT(bpf_map, selinux_bpf_map),
7081 LSM_HOOK_INIT(bpf_prog, selinux_bpf_prog),
7082 LSM_HOOK_INIT(bpf_map_alloc_security, selinux_bpf_map_alloc),
7083 LSM_HOOK_INIT(bpf_prog_alloc_security, selinux_bpf_prog_alloc),
7084 LSM_HOOK_INIT(bpf_map_free_security, selinux_bpf_map_free),
7085 LSM_HOOK_INIT(bpf_prog_free_security, selinux_bpf_prog_free),
7086#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007087};
7088
7089static __init int selinux_init(void)
7090{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07007091 if (!security_module_enable("selinux")) {
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02007092 selinux_enabled = 0;
7093 return 0;
7094 }
7095
Linus Torvalds1da177e2005-04-16 15:20:36 -07007096 if (!selinux_enabled) {
peter enderborgc103a912018-06-12 10:09:03 +02007097 pr_info("SELinux: Disabled at boot.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007098 return 0;
7099 }
7100
peter enderborgc103a912018-06-12 10:09:03 +02007101 pr_info("SELinux: Initializing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007102
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007103 memset(&selinux_state, 0, sizeof(selinux_state));
Paul Mooree5a5ca92018-03-01 17:38:30 -05007104 enforcing_set(&selinux_state, selinux_enforcing_boot);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007105 selinux_state.checkreqprot = selinux_checkreqprot_boot;
7106 selinux_ss_init(&selinux_state.ss);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05007107 selinux_avc_init(&selinux_state.avc);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007108
Linus Torvalds1da177e2005-04-16 15:20:36 -07007109 /* Set the security state for the initial task. */
David Howellsd84f4f92008-11-14 10:39:23 +11007110 cred_init_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007111
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04007112 default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
7113
James Morris7cae7e22006-03-22 00:09:22 -08007114 sel_inode_cache = kmem_cache_create("selinux_inode_security",
7115 sizeof(struct inode_security_struct),
Paul Mundt20c2df82007-07-20 10:11:58 +09007116 0, SLAB_PANIC, NULL);
Sangwoo63205652015-10-21 17:44:30 -04007117 file_security_cache = kmem_cache_create("selinux_file_security",
7118 sizeof(struct file_security_struct),
7119 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007120 avc_init();
7121
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007122 avtab_cache_init();
7123
7124 ebitmap_cache_init();
7125
7126 hashtab_cache_init();
7127
Casey Schauflerd69dece52017-01-18 17:09:05 -08007128 security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks), "selinux");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007129
Paul Moore615e51f2014-06-26 14:33:56 -04007130 if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET))
7131 panic("SELinux: Unable to register AVC netcache callback\n");
7132
Daniel Jurgens8f408ab2017-05-19 15:48:53 +03007133 if (avc_add_callback(selinux_lsm_notifier_avc_callback, AVC_CALLBACK_RESET))
7134 panic("SELinux: Unable to register AVC LSM notifier callback\n");
7135
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007136 if (selinux_enforcing_boot)
peter enderborgc103a912018-06-12 10:09:03 +02007137 pr_debug("SELinux: Starting in enforcing mode\n");
Eric Paris828dfe12008-04-17 13:17:49 -04007138 else
peter enderborgc103a912018-06-12 10:09:03 +02007139 pr_debug("SELinux: Starting in permissive mode\n");
Michael LeMayd7200242006-06-22 14:47:17 -07007140
Linus Torvalds1da177e2005-04-16 15:20:36 -07007141 return 0;
7142}
7143
Al Viroe8c26252010-03-23 06:36:54 -04007144static void delayed_superblock_init(struct super_block *sb, void *unused)
7145{
Al Viro17f3b552018-11-10 23:46:07 -05007146 struct security_mnt_opts opts;
7147
7148 security_init_mnt_opts(&opts);
7149 selinux_set_mnt_opts(sb, &opts, 0, NULL);
7150 security_free_mnt_opts(&opts);
Al Viroe8c26252010-03-23 06:36:54 -04007151}
7152
Linus Torvalds1da177e2005-04-16 15:20:36 -07007153void selinux_complete_init(void)
7154{
peter enderborgc103a912018-06-12 10:09:03 +02007155 pr_debug("SELinux: Completing initialization.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007156
7157 /* Set up any superblocks initialized prior to the policy load. */
peter enderborgc103a912018-06-12 10:09:03 +02007158 pr_debug("SELinux: Setting up existing superblocks.\n");
Al Viroe8c26252010-03-23 06:36:54 -04007159 iterate_supers(delayed_superblock_init, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007160}
7161
7162/* SELinux requires early initialization in order to label
7163 all processes and objects when they are created. */
Kees Cook3d6e5f62018-10-10 17:18:23 -07007164DEFINE_LSM(selinux) = {
Kees Cook07aed2f2018-10-10 17:18:24 -07007165 .name = "selinux",
Kees Cook3d6e5f62018-10-10 17:18:23 -07007166 .init = selinux_init,
7167};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007168
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007169#if defined(CONFIG_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007170
Florian Westphal591bb272017-07-26 11:40:52 +02007171static const struct nf_hook_ops selinux_nf_ops[] = {
Paul Mooreeffad8d2008-01-29 08:49:27 -05007172 {
7173 .hook = selinux_ipv4_postroute,
Alban Crequy2597a832012-05-14 03:56:39 +00007174 .pf = NFPROTO_IPV4,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007175 .hooknum = NF_INET_POST_ROUTING,
7176 .priority = NF_IP_PRI_SELINUX_LAST,
7177 },
7178 {
7179 .hook = selinux_ipv4_forward,
Alban Crequy2597a832012-05-14 03:56:39 +00007180 .pf = NFPROTO_IPV4,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007181 .hooknum = NF_INET_FORWARD,
7182 .priority = NF_IP_PRI_SELINUX_FIRST,
Paul Moore948bf852008-10-10 10:16:32 -04007183 },
7184 {
7185 .hook = selinux_ipv4_output,
Alban Crequy2597a832012-05-14 03:56:39 +00007186 .pf = NFPROTO_IPV4,
Paul Moore948bf852008-10-10 10:16:32 -04007187 .hooknum = NF_INET_LOCAL_OUT,
7188 .priority = NF_IP_PRI_SELINUX_FIRST,
Jiri Pirko25db6be2014-09-03 17:42:13 +02007189 },
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04007190#if IS_ENABLED(CONFIG_IPV6)
Paul Mooreeffad8d2008-01-29 08:49:27 -05007191 {
7192 .hook = selinux_ipv6_postroute,
Alban Crequy2597a832012-05-14 03:56:39 +00007193 .pf = NFPROTO_IPV6,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007194 .hooknum = NF_INET_POST_ROUTING,
7195 .priority = NF_IP6_PRI_SELINUX_LAST,
7196 },
7197 {
7198 .hook = selinux_ipv6_forward,
Alban Crequy2597a832012-05-14 03:56:39 +00007199 .pf = NFPROTO_IPV6,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007200 .hooknum = NF_INET_FORWARD,
7201 .priority = NF_IP6_PRI_SELINUX_FIRST,
Jiri Pirko25db6be2014-09-03 17:42:13 +02007202 },
Huw Davies2917f572016-06-27 15:06:15 -04007203 {
7204 .hook = selinux_ipv6_output,
7205 .pf = NFPROTO_IPV6,
7206 .hooknum = NF_INET_LOCAL_OUT,
7207 .priority = NF_IP6_PRI_SELINUX_FIRST,
7208 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07007209#endif /* IPV6 */
Jiri Pirko25db6be2014-09-03 17:42:13 +02007210};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007211
Florian Westphal8e71bf72017-04-21 11:49:09 +02007212static int __net_init selinux_nf_register(struct net *net)
7213{
7214 return nf_register_net_hooks(net, selinux_nf_ops,
7215 ARRAY_SIZE(selinux_nf_ops));
7216}
7217
7218static void __net_exit selinux_nf_unregister(struct net *net)
7219{
7220 nf_unregister_net_hooks(net, selinux_nf_ops,
7221 ARRAY_SIZE(selinux_nf_ops));
7222}
7223
7224static struct pernet_operations selinux_net_ops = {
7225 .init = selinux_nf_register,
7226 .exit = selinux_nf_unregister,
7227};
7228
Linus Torvalds1da177e2005-04-16 15:20:36 -07007229static int __init selinux_nf_ip_init(void)
7230{
Jiri Pirko25db6be2014-09-03 17:42:13 +02007231 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007232
7233 if (!selinux_enabled)
Jiri Pirko25db6be2014-09-03 17:42:13 +02007234 return 0;
Eric Parisfadcdb42007-02-22 18:11:31 -05007235
peter enderborgc103a912018-06-12 10:09:03 +02007236 pr_debug("SELinux: Registering netfilter hooks\n");
Eric Parisfadcdb42007-02-22 18:11:31 -05007237
Florian Westphal8e71bf72017-04-21 11:49:09 +02007238 err = register_pernet_subsys(&selinux_net_ops);
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07007239 if (err)
Florian Westphal8e71bf72017-04-21 11:49:09 +02007240 panic("SELinux: register_pernet_subsys: error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007241
Jiri Pirko25db6be2014-09-03 17:42:13 +02007242 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007243}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007244__initcall(selinux_nf_ip_init);
7245
7246#ifdef CONFIG_SECURITY_SELINUX_DISABLE
7247static void selinux_nf_ip_exit(void)
7248{
peter enderborgc103a912018-06-12 10:09:03 +02007249 pr_debug("SELinux: Unregistering netfilter hooks\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007250
Florian Westphal8e71bf72017-04-21 11:49:09 +02007251 unregister_pernet_subsys(&selinux_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007252}
7253#endif
7254
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007255#else /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007256
7257#ifdef CONFIG_SECURITY_SELINUX_DISABLE
7258#define selinux_nf_ip_exit()
7259#endif
7260
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007261#endif /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007262
7263#ifdef CONFIG_SECURITY_SELINUX_DISABLE
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007264int selinux_disable(struct selinux_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007265{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007266 if (state->initialized) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007267 /* Not permitted after initial policy load. */
7268 return -EINVAL;
7269 }
7270
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007271 if (state->disabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007272 /* Only do this once. */
7273 return -EINVAL;
7274 }
7275
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007276 state->disabled = 1;
7277
peter enderborgc103a912018-06-12 10:09:03 +02007278 pr_info("SELinux: Disabled at runtime.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007279
Stephen Smalley30d55282006-05-03 10:52:36 -04007280 selinux_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007281
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07007282 security_delete_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007283
Eric Parisaf8ff042009-09-20 21:23:01 -04007284 /* Try to destroy the avc node cache */
7285 avc_disable();
7286
Linus Torvalds1da177e2005-04-16 15:20:36 -07007287 /* Unregister netfilter hooks. */
7288 selinux_nf_ip_exit();
7289
7290 /* Unregister selinuxfs. */
7291 exit_sel_fs();
7292
7293 return 0;
7294}
7295#endif