blob: ce0511f024e057790f303e74b52edb6234a43242 [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);
Al Viro8d641242018-12-10 15:34:12 -0500574 while (!list_empty(&sbsec->isec_head)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 struct inode_security_struct *isec =
Al Viro8d641242018-12-10 15:34:12 -0500576 list_first_entry(&sbsec->isec_head,
Eric Parisc9180a52007-11-30 13:00:35 -0500577 struct inode_security_struct, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 struct inode *inode = isec->inode;
Stephen Smalley923190d2014-10-06 16:32:52 -0400579 list_del_init(&isec->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 spin_unlock(&sbsec->isec_lock);
581 inode = igrab(inode);
582 if (inode) {
Eric Parisc9180a52007-11-30 13:00:35 -0500583 if (!IS_PRIVATE(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 inode_doinit(inode);
585 iput(inode);
586 }
587 spin_lock(&sbsec->isec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 }
589 spin_unlock(&sbsec->isec_lock);
590out:
Eric Parisc9180a52007-11-30 13:00:35 -0500591 return rc;
592}
593
594/*
595 * This function should allow an FS to ask what it's mount security
596 * options were so it can use those later for submounts, displaying
597 * mount options, or whatever.
598 */
599static int selinux_get_mnt_opts(const struct super_block *sb,
Eric Parise0007522008-03-05 10:31:54 -0500600 struct security_mnt_opts *opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500601{
602 int rc = 0, i;
603 struct superblock_security_struct *sbsec = sb->s_security;
604 char *context = NULL;
605 u32 len;
606 char tmp;
607
Eric Parise0007522008-03-05 10:31:54 -0500608 security_init_mnt_opts(opts);
Eric Parisc9180a52007-11-30 13:00:35 -0500609
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500610 if (!(sbsec->flags & SE_SBINITIALIZED))
Eric Parisc9180a52007-11-30 13:00:35 -0500611 return -EINVAL;
612
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500613 if (!selinux_state.initialized)
Eric Parisc9180a52007-11-30 13:00:35 -0500614 return -EINVAL;
615
Eric Parisaf8e50c2012-08-24 15:59:00 -0400616 /* make sure we always check enough bits to cover the mask */
617 BUILD_BUG_ON(SE_MNTMASK >= (1 << NUM_SEL_MNT_OPTS));
618
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500619 tmp = sbsec->flags & SE_MNTMASK;
Eric Parisc9180a52007-11-30 13:00:35 -0500620 /* count the number of mount options for this sb */
Eric Parisaf8e50c2012-08-24 15:59:00 -0400621 for (i = 0; i < NUM_SEL_MNT_OPTS; i++) {
Eric Parisc9180a52007-11-30 13:00:35 -0500622 if (tmp & 0x01)
Eric Parise0007522008-03-05 10:31:54 -0500623 opts->num_mnt_opts++;
Eric Parisc9180a52007-11-30 13:00:35 -0500624 tmp >>= 1;
625 }
David P. Quigley11689d42009-01-16 09:22:03 -0500626 /* Check if the Label support flag is set */
Eric Paris0b4bdb32013-08-28 13:32:42 -0400627 if (sbsec->flags & SBLABEL_MNT)
David P. Quigley11689d42009-01-16 09:22:03 -0500628 opts->num_mnt_opts++;
Eric Parisc9180a52007-11-30 13:00:35 -0500629
Eric Parise0007522008-03-05 10:31:54 -0500630 opts->mnt_opts = kcalloc(opts->num_mnt_opts, sizeof(char *), GFP_ATOMIC);
631 if (!opts->mnt_opts) {
Eric Parisc9180a52007-11-30 13:00:35 -0500632 rc = -ENOMEM;
633 goto out_free;
634 }
635
Eric Parise0007522008-03-05 10:31:54 -0500636 opts->mnt_opts_flags = kcalloc(opts->num_mnt_opts, sizeof(int), GFP_ATOMIC);
637 if (!opts->mnt_opts_flags) {
Eric Parisc9180a52007-11-30 13:00:35 -0500638 rc = -ENOMEM;
639 goto out_free;
640 }
641
642 i = 0;
643 if (sbsec->flags & FSCONTEXT_MNT) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500644 rc = security_sid_to_context(&selinux_state, sbsec->sid,
645 &context, &len);
Eric Parisc9180a52007-11-30 13:00:35 -0500646 if (rc)
647 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500648 opts->mnt_opts[i] = context;
649 opts->mnt_opts_flags[i++] = FSCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500650 }
651 if (sbsec->flags & CONTEXT_MNT) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500652 rc = security_sid_to_context(&selinux_state,
653 sbsec->mntpoint_sid,
654 &context, &len);
Eric Parisc9180a52007-11-30 13:00:35 -0500655 if (rc)
656 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500657 opts->mnt_opts[i] = context;
658 opts->mnt_opts_flags[i++] = CONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500659 }
660 if (sbsec->flags & DEFCONTEXT_MNT) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500661 rc = security_sid_to_context(&selinux_state, sbsec->def_sid,
662 &context, &len);
Eric Parisc9180a52007-11-30 13:00:35 -0500663 if (rc)
664 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500665 opts->mnt_opts[i] = context;
666 opts->mnt_opts_flags[i++] = DEFCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500667 }
668 if (sbsec->flags & ROOTCONTEXT_MNT) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500669 struct dentry *root = sbsec->sb->s_root;
670 struct inode_security_struct *isec = backing_inode_security(root);
Eric Parisc9180a52007-11-30 13:00:35 -0500671
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500672 rc = security_sid_to_context(&selinux_state, isec->sid,
673 &context, &len);
Eric Parisc9180a52007-11-30 13:00:35 -0500674 if (rc)
675 goto out_free;
Eric Parise0007522008-03-05 10:31:54 -0500676 opts->mnt_opts[i] = context;
677 opts->mnt_opts_flags[i++] = ROOTCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500678 }
Eric Paris12f348b2012-10-09 10:56:25 -0400679 if (sbsec->flags & SBLABEL_MNT) {
David P. Quigley11689d42009-01-16 09:22:03 -0500680 opts->mnt_opts[i] = NULL;
Eric Paris12f348b2012-10-09 10:56:25 -0400681 opts->mnt_opts_flags[i++] = SBLABEL_MNT;
David P. Quigley11689d42009-01-16 09:22:03 -0500682 }
Eric Parisc9180a52007-11-30 13:00:35 -0500683
Eric Parise0007522008-03-05 10:31:54 -0500684 BUG_ON(i != opts->num_mnt_opts);
Eric Parisc9180a52007-11-30 13:00:35 -0500685
686 return 0;
687
688out_free:
Eric Parise0007522008-03-05 10:31:54 -0500689 security_free_mnt_opts(opts);
Eric Parisc9180a52007-11-30 13:00:35 -0500690 return rc;
691}
692
693static int bad_option(struct superblock_security_struct *sbsec, char flag,
694 u32 old_sid, u32 new_sid)
695{
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500696 char mnt_flags = sbsec->flags & SE_MNTMASK;
697
Eric Parisc9180a52007-11-30 13:00:35 -0500698 /* check if the old mount command had the same options */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500699 if (sbsec->flags & SE_SBINITIALIZED)
Eric Parisc9180a52007-11-30 13:00:35 -0500700 if (!(sbsec->flags & flag) ||
701 (old_sid != new_sid))
702 return 1;
703
704 /* check if we were passed the same options twice,
705 * aka someone passed context=a,context=b
706 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500707 if (!(sbsec->flags & SE_SBINITIALIZED))
708 if (mnt_flags & flag)
Eric Parisc9180a52007-11-30 13:00:35 -0500709 return 1;
710 return 0;
711}
Eric Parise0007522008-03-05 10:31:54 -0500712
Eric Parisc9180a52007-11-30 13:00:35 -0500713/*
714 * Allow filesystems with binary mount data to explicitly set mount point
715 * labeling information.
716 */
Eric Parise0007522008-03-05 10:31:54 -0500717static int selinux_set_mnt_opts(struct super_block *sb,
David Quigley649f6e72013-05-22 12:50:36 -0400718 struct security_mnt_opts *opts,
719 unsigned long kern_flags,
720 unsigned long *set_kern_flags)
Eric Parisc9180a52007-11-30 13:00:35 -0500721{
David Howells275bb412008-11-14 10:39:19 +1100722 const struct cred *cred = current_cred();
Eric Parisc9180a52007-11-30 13:00:35 -0500723 int rc = 0, i;
Eric Parisc9180a52007-11-30 13:00:35 -0500724 struct superblock_security_struct *sbsec = sb->s_security;
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800725 const char *name = sb->s_type->name;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500726 struct dentry *root = sbsec->sb->s_root;
Paul Moore2c971652016-04-19 16:36:28 -0400727 struct inode_security_struct *root_isec;
Eric Parisc9180a52007-11-30 13:00:35 -0500728 u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
729 u32 defcontext_sid = 0;
Eric Parise0007522008-03-05 10:31:54 -0500730 char **mount_options = opts->mnt_opts;
731 int *flags = opts->mnt_opts_flags;
732 int num_opts = opts->num_mnt_opts;
Eric Parisc9180a52007-11-30 13:00:35 -0500733
734 mutex_lock(&sbsec->lock);
735
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500736 if (!selinux_state.initialized) {
Eric Parisc9180a52007-11-30 13:00:35 -0500737 if (!num_opts) {
738 /* Defer initialization until selinux_complete_init,
739 after the initial policy is loaded and the security
740 server is ready to handle calls. */
Eric Parisc9180a52007-11-30 13:00:35 -0500741 goto out;
742 }
743 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +0200744 pr_warn("SELinux: Unable to set superblock options "
Eric Paris744ba352008-04-17 11:52:44 -0400745 "before the security server is initialized\n");
Eric Parisc9180a52007-11-30 13:00:35 -0500746 goto out;
747 }
David Quigley649f6e72013-05-22 12:50:36 -0400748 if (kern_flags && !set_kern_flags) {
749 /* Specifying internal flags without providing a place to
750 * place the results is not allowed */
751 rc = -EINVAL;
752 goto out;
753 }
Eric Parisc9180a52007-11-30 13:00:35 -0500754
755 /*
Eric Parise0007522008-03-05 10:31:54 -0500756 * Binary mount data FS will come through this function twice. Once
757 * from an explicit call and once from the generic calls from the vfs.
758 * Since the generic VFS calls will not contain any security mount data
759 * we need to skip the double mount verification.
760 *
761 * This does open a hole in which we will not notice if the first
762 * mount using this sb set explict options and a second mount using
763 * this sb does not set any security options. (The first options
764 * will be used for both mounts)
765 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500766 if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
Eric Parise0007522008-03-05 10:31:54 -0500767 && (num_opts == 0))
Eric Parisf5269712008-05-14 11:27:45 -0400768 goto out;
Eric Parise0007522008-03-05 10:31:54 -0500769
Paul Moore2c971652016-04-19 16:36:28 -0400770 root_isec = backing_inode_security_novalidate(root);
771
Eric Parise0007522008-03-05 10:31:54 -0500772 /*
Eric Parisc9180a52007-11-30 13:00:35 -0500773 * parse the mount options, check if they are valid sids.
774 * also check if someone is trying to mount the same sb more
775 * than once with different security options.
776 */
777 for (i = 0; i < num_opts; i++) {
778 u32 sid;
David P. Quigley11689d42009-01-16 09:22:03 -0500779
Eric Paris12f348b2012-10-09 10:56:25 -0400780 if (flags[i] == SBLABEL_MNT)
David P. Quigley11689d42009-01-16 09:22:03 -0500781 continue;
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500782 rc = security_context_str_to_sid(&selinux_state,
783 mount_options[i], &sid,
784 GFP_KERNEL);
Eric Parisc9180a52007-11-30 13:00:35 -0500785 if (rc) {
peter enderborgc103a912018-06-12 10:09:03 +0200786 pr_warn("SELinux: security_context_str_to_sid"
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800787 "(%s) failed for (dev %s, type %s) errno=%d\n",
788 mount_options[i], sb->s_id, name, rc);
Eric Parisc9180a52007-11-30 13:00:35 -0500789 goto out;
790 }
791 switch (flags[i]) {
792 case FSCONTEXT_MNT:
793 fscontext_sid = sid;
794
795 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
796 fscontext_sid))
797 goto out_double_mount;
798
799 sbsec->flags |= FSCONTEXT_MNT;
800 break;
801 case CONTEXT_MNT:
802 context_sid = sid;
803
804 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
805 context_sid))
806 goto out_double_mount;
807
808 sbsec->flags |= CONTEXT_MNT;
809 break;
810 case ROOTCONTEXT_MNT:
811 rootcontext_sid = sid;
812
813 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
814 rootcontext_sid))
815 goto out_double_mount;
816
817 sbsec->flags |= ROOTCONTEXT_MNT;
818
819 break;
820 case DEFCONTEXT_MNT:
821 defcontext_sid = sid;
822
823 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
824 defcontext_sid))
825 goto out_double_mount;
826
827 sbsec->flags |= DEFCONTEXT_MNT;
828
829 break;
830 default:
831 rc = -EINVAL;
832 goto out;
833 }
834 }
835
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500836 if (sbsec->flags & SE_SBINITIALIZED) {
Eric Parisc9180a52007-11-30 13:00:35 -0500837 /* previously mounted with options, but not on this attempt? */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500838 if ((sbsec->flags & SE_MNTMASK) && !num_opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500839 goto out_double_mount;
840 rc = 0;
841 goto out;
842 }
843
James Morris089be432008-07-15 18:32:49 +1000844 if (strcmp(sb->s_type->name, "proc") == 0)
Stephen Smalley134509d2015-06-04 16:22:17 -0400845 sbsec->flags |= SE_SBPROC | SE_SBGENFS;
846
Stephen Smalley8e014722015-06-04 16:22:17 -0400847 if (!strcmp(sb->s_type->name, "debugfs") ||
Jeff Vander Stoep6a391182017-06-20 09:35:33 -0700848 !strcmp(sb->s_type->name, "tracefs") ||
Stephen Smalley8e014722015-06-04 16:22:17 -0400849 !strcmp(sb->s_type->name, "sysfs") ||
Antonio Murdaca901ef842017-02-09 17:02:42 +0100850 !strcmp(sb->s_type->name, "pstore") ||
851 !strcmp(sb->s_type->name, "cgroup") ||
852 !strcmp(sb->s_type->name, "cgroup2"))
Stephen Smalley134509d2015-06-04 16:22:17 -0400853 sbsec->flags |= SE_SBGENFS;
Eric Parisc9180a52007-11-30 13:00:35 -0500854
David Quigleyeb9ae682013-05-22 12:50:37 -0400855 if (!sbsec->behavior) {
856 /*
857 * Determine the labeling behavior to use for this
858 * filesystem type.
859 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500860 rc = security_fs_use(&selinux_state, sb);
David Quigleyeb9ae682013-05-22 12:50:37 -0400861 if (rc) {
peter enderborgc103a912018-06-12 10:09:03 +0200862 pr_warn("%s: security_fs_use(%s) returned %d\n",
David Quigleyeb9ae682013-05-22 12:50:37 -0400863 __func__, sb->s_type->name, rc);
864 goto out;
865 }
Eric Parisc9180a52007-11-30 13:00:35 -0500866 }
Seth Forsheeaad82892016-04-26 14:36:20 -0500867
868 /*
Stephen Smalley01593d32017-01-09 10:07:31 -0500869 * If this is a user namespace mount and the filesystem type is not
870 * explicitly whitelisted, then no contexts are allowed on the command
871 * line and security labels must be ignored.
Seth Forsheeaad82892016-04-26 14:36:20 -0500872 */
Stephen Smalley01593d32017-01-09 10:07:31 -0500873 if (sb->s_user_ns != &init_user_ns &&
874 strcmp(sb->s_type->name, "tmpfs") &&
875 strcmp(sb->s_type->name, "ramfs") &&
876 strcmp(sb->s_type->name, "devpts")) {
Seth Forsheeaad82892016-04-26 14:36:20 -0500877 if (context_sid || fscontext_sid || rootcontext_sid ||
878 defcontext_sid) {
879 rc = -EACCES;
880 goto out;
881 }
882 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
883 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500884 rc = security_transition_sid(&selinux_state,
885 current_sid(),
886 current_sid(),
Seth Forsheeaad82892016-04-26 14:36:20 -0500887 SECCLASS_FILE, NULL,
888 &sbsec->mntpoint_sid);
889 if (rc)
890 goto out;
891 }
892 goto out_set_opts;
893 }
894
Eric Parisc9180a52007-11-30 13:00:35 -0500895 /* sets the context of the superblock for the fs being mounted. */
896 if (fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100897 rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500898 if (rc)
899 goto out;
900
901 sbsec->sid = fscontext_sid;
902 }
903
904 /*
905 * Switch to using mount point labeling behavior.
906 * sets the label used on all file below the mountpoint, and will set
907 * the superblock context if not already set.
908 */
David Quigleyeb9ae682013-05-22 12:50:37 -0400909 if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !context_sid) {
910 sbsec->behavior = SECURITY_FS_USE_NATIVE;
911 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
912 }
913
Eric Parisc9180a52007-11-30 13:00:35 -0500914 if (context_sid) {
915 if (!fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100916 rc = may_context_mount_sb_relabel(context_sid, sbsec,
917 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500918 if (rc)
919 goto out;
920 sbsec->sid = context_sid;
921 } else {
David Howells275bb412008-11-14 10:39:19 +1100922 rc = may_context_mount_inode_relabel(context_sid, sbsec,
923 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500924 if (rc)
925 goto out;
926 }
927 if (!rootcontext_sid)
928 rootcontext_sid = context_sid;
929
930 sbsec->mntpoint_sid = context_sid;
931 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
932 }
933
934 if (rootcontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100935 rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec,
936 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500937 if (rc)
938 goto out;
939
940 root_isec->sid = rootcontext_sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -0500941 root_isec->initialized = LABEL_INITIALIZED;
Eric Parisc9180a52007-11-30 13:00:35 -0500942 }
943
944 if (defcontext_sid) {
David Quigleyeb9ae682013-05-22 12:50:37 -0400945 if (sbsec->behavior != SECURITY_FS_USE_XATTR &&
946 sbsec->behavior != SECURITY_FS_USE_NATIVE) {
Eric Parisc9180a52007-11-30 13:00:35 -0500947 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +0200948 pr_warn("SELinux: defcontext option is "
Eric Parisc9180a52007-11-30 13:00:35 -0500949 "invalid for this filesystem type\n");
950 goto out;
951 }
952
953 if (defcontext_sid != sbsec->def_sid) {
954 rc = may_context_mount_inode_relabel(defcontext_sid,
David Howells275bb412008-11-14 10:39:19 +1100955 sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500956 if (rc)
957 goto out;
958 }
959
960 sbsec->def_sid = defcontext_sid;
961 }
962
Seth Forsheeaad82892016-04-26 14:36:20 -0500963out_set_opts:
Eric Parisc9180a52007-11-30 13:00:35 -0500964 rc = sb_finish_set_opts(sb);
965out:
Eric Parisbc7e9822006-09-25 23:32:02 -0700966 mutex_unlock(&sbsec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 return rc;
Eric Parisc9180a52007-11-30 13:00:35 -0500968out_double_mount:
969 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +0200970 pr_warn("SELinux: mount invalid. Same superblock, different "
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800971 "security settings for (dev %s, type %s)\n", sb->s_id, name);
Eric Parisc9180a52007-11-30 13:00:35 -0500972 goto out;
973}
974
Jeff Layton094f7b62013-04-01 08:14:24 -0400975static int selinux_cmp_sb_context(const struct super_block *oldsb,
976 const struct super_block *newsb)
977{
978 struct superblock_security_struct *old = oldsb->s_security;
979 struct superblock_security_struct *new = newsb->s_security;
980 char oldflags = old->flags & SE_MNTMASK;
981 char newflags = new->flags & SE_MNTMASK;
982
983 if (oldflags != newflags)
984 goto mismatch;
985 if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid)
986 goto mismatch;
987 if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid)
988 goto mismatch;
989 if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid)
990 goto mismatch;
991 if (oldflags & ROOTCONTEXT_MNT) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500992 struct inode_security_struct *oldroot = backing_inode_security(oldsb->s_root);
993 struct inode_security_struct *newroot = backing_inode_security(newsb->s_root);
Jeff Layton094f7b62013-04-01 08:14:24 -0400994 if (oldroot->sid != newroot->sid)
995 goto mismatch;
996 }
997 return 0;
998mismatch:
peter enderborgc103a912018-06-12 10:09:03 +0200999 pr_warn("SELinux: mount invalid. Same superblock, "
Jeff Layton094f7b62013-04-01 08:14:24 -04001000 "different security settings for (dev %s, "
1001 "type %s)\n", newsb->s_id, newsb->s_type->name);
1002 return -EBUSY;
1003}
1004
1005static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
Scott Mayhew0b4d3452017-06-05 11:45:04 -04001006 struct super_block *newsb,
1007 unsigned long kern_flags,
1008 unsigned long *set_kern_flags)
Eric Parisc9180a52007-11-30 13:00:35 -05001009{
Scott Mayhew0b4d3452017-06-05 11:45:04 -04001010 int rc = 0;
Eric Parisc9180a52007-11-30 13:00:35 -05001011 const struct superblock_security_struct *oldsbsec = oldsb->s_security;
1012 struct superblock_security_struct *newsbsec = newsb->s_security;
1013
1014 int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT);
1015 int set_context = (oldsbsec->flags & CONTEXT_MNT);
1016 int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT);
1017
Eric Paris0f5e6422008-04-21 16:24:11 -04001018 /*
1019 * if the parent was able to be mounted it clearly had no special lsm
Al Viroe8c26252010-03-23 06:36:54 -04001020 * mount options. thus we can safely deal with this superblock later
Eric Paris0f5e6422008-04-21 16:24:11 -04001021 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001022 if (!selinux_state.initialized)
Jeff Layton094f7b62013-04-01 08:14:24 -04001023 return 0;
Eric Parisc9180a52007-11-30 13:00:35 -05001024
Scott Mayhew0b4d3452017-06-05 11:45:04 -04001025 /*
1026 * Specifying internal flags without providing a place to
1027 * place the results is not allowed.
1028 */
1029 if (kern_flags && !set_kern_flags)
1030 return -EINVAL;
1031
Eric Parisc9180a52007-11-30 13:00:35 -05001032 /* how can we clone if the old one wasn't set up?? */
David P. Quigley0d90a7e2009-01-16 09:22:02 -05001033 BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED));
Eric Parisc9180a52007-11-30 13:00:35 -05001034
Jeff Layton094f7b62013-04-01 08:14:24 -04001035 /* if fs is reusing a sb, make sure that the contexts match */
David P. Quigley0d90a7e2009-01-16 09:22:02 -05001036 if (newsbsec->flags & SE_SBINITIALIZED)
Jeff Layton094f7b62013-04-01 08:14:24 -04001037 return selinux_cmp_sb_context(oldsb, newsb);
Eric Paris5a552612008-04-09 14:08:35 -04001038
Eric Parisc9180a52007-11-30 13:00:35 -05001039 mutex_lock(&newsbsec->lock);
1040
1041 newsbsec->flags = oldsbsec->flags;
1042
1043 newsbsec->sid = oldsbsec->sid;
1044 newsbsec->def_sid = oldsbsec->def_sid;
1045 newsbsec->behavior = oldsbsec->behavior;
1046
Scott Mayhew0b4d3452017-06-05 11:45:04 -04001047 if (newsbsec->behavior == SECURITY_FS_USE_NATIVE &&
1048 !(kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001049 rc = security_fs_use(&selinux_state, newsb);
Scott Mayhew0b4d3452017-06-05 11:45:04 -04001050 if (rc)
1051 goto out;
1052 }
1053
1054 if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !set_context) {
1055 newsbsec->behavior = SECURITY_FS_USE_NATIVE;
1056 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
1057 }
1058
Eric Parisc9180a52007-11-30 13:00:35 -05001059 if (set_context) {
1060 u32 sid = oldsbsec->mntpoint_sid;
1061
1062 if (!set_fscontext)
1063 newsbsec->sid = sid;
1064 if (!set_rootcontext) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001065 struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
Eric Parisc9180a52007-11-30 13:00:35 -05001066 newisec->sid = sid;
1067 }
1068 newsbsec->mntpoint_sid = sid;
1069 }
1070 if (set_rootcontext) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001071 const struct inode_security_struct *oldisec = backing_inode_security(oldsb->s_root);
1072 struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
Eric Parisc9180a52007-11-30 13:00:35 -05001073
1074 newisec->sid = oldisec->sid;
1075 }
1076
1077 sb_finish_set_opts(newsb);
Scott Mayhew0b4d3452017-06-05 11:45:04 -04001078out:
Eric Parisc9180a52007-11-30 13:00:35 -05001079 mutex_unlock(&newsbsec->lock);
Scott Mayhew0b4d3452017-06-05 11:45:04 -04001080 return rc;
Eric Parisc9180a52007-11-30 13:00:35 -05001081}
1082
Adrian Bunk2e1479d2008-03-17 22:29:23 +02001083static int selinux_parse_opts_str(char *options,
1084 struct security_mnt_opts *opts)
Eric Parisc9180a52007-11-30 13:00:35 -05001085{
Eric Parise0007522008-03-05 10:31:54 -05001086 char *p;
Eric Parisc9180a52007-11-30 13:00:35 -05001087 char *context = NULL, *defcontext = NULL;
1088 char *fscontext = NULL, *rootcontext = NULL;
Eric Parise0007522008-03-05 10:31:54 -05001089 int rc, num_mnt_opts = 0;
Eric Parisc9180a52007-11-30 13:00:35 -05001090
Eric Parise0007522008-03-05 10:31:54 -05001091 opts->num_mnt_opts = 0;
Eric Parisc9180a52007-11-30 13:00:35 -05001092
1093 /* Standard string-based options. */
1094 while ((p = strsep(&options, "|")) != NULL) {
1095 int token;
1096 substring_t args[MAX_OPT_ARGS];
1097
1098 if (!*p)
1099 continue;
1100
1101 token = match_token(p, tokens, args);
1102
1103 switch (token) {
1104 case Opt_context:
1105 if (context || defcontext) {
1106 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +02001107 pr_warn(SEL_MOUNT_FAIL_MSG);
Eric Parisc9180a52007-11-30 13:00:35 -05001108 goto out_err;
1109 }
1110 context = match_strdup(&args[0]);
1111 if (!context) {
1112 rc = -ENOMEM;
1113 goto out_err;
1114 }
1115 break;
1116
1117 case Opt_fscontext:
1118 if (fscontext) {
1119 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +02001120 pr_warn(SEL_MOUNT_FAIL_MSG);
Eric Parisc9180a52007-11-30 13:00:35 -05001121 goto out_err;
1122 }
1123 fscontext = match_strdup(&args[0]);
1124 if (!fscontext) {
1125 rc = -ENOMEM;
1126 goto out_err;
1127 }
1128 break;
1129
1130 case Opt_rootcontext:
1131 if (rootcontext) {
1132 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +02001133 pr_warn(SEL_MOUNT_FAIL_MSG);
Eric Parisc9180a52007-11-30 13:00:35 -05001134 goto out_err;
1135 }
1136 rootcontext = match_strdup(&args[0]);
1137 if (!rootcontext) {
1138 rc = -ENOMEM;
1139 goto out_err;
1140 }
1141 break;
1142
1143 case Opt_defcontext:
1144 if (context || defcontext) {
1145 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +02001146 pr_warn(SEL_MOUNT_FAIL_MSG);
Eric Parisc9180a52007-11-30 13:00:35 -05001147 goto out_err;
1148 }
1149 defcontext = match_strdup(&args[0]);
1150 if (!defcontext) {
1151 rc = -ENOMEM;
1152 goto out_err;
1153 }
1154 break;
David P. Quigley11689d42009-01-16 09:22:03 -05001155 case Opt_labelsupport:
1156 break;
Eric Parisc9180a52007-11-30 13:00:35 -05001157 default:
1158 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +02001159 pr_warn("SELinux: unknown mount option\n");
Eric Parisc9180a52007-11-30 13:00:35 -05001160 goto out_err;
1161
1162 }
1163 }
1164
Eric Parise0007522008-03-05 10:31:54 -05001165 rc = -ENOMEM;
Tetsuo Handa8931c3b2016-11-14 20:16:12 +09001166 opts->mnt_opts = kcalloc(NUM_SEL_MNT_OPTS, sizeof(char *), GFP_KERNEL);
Eric Parise0007522008-03-05 10:31:54 -05001167 if (!opts->mnt_opts)
1168 goto out_err;
1169
Tetsuo Handa8931c3b2016-11-14 20:16:12 +09001170 opts->mnt_opts_flags = kcalloc(NUM_SEL_MNT_OPTS, sizeof(int),
1171 GFP_KERNEL);
Paul Moore023f1082017-06-07 16:48:19 -04001172 if (!opts->mnt_opts_flags)
Eric Parise0007522008-03-05 10:31:54 -05001173 goto out_err;
Eric Parisc9180a52007-11-30 13:00:35 -05001174
Eric Parise0007522008-03-05 10:31:54 -05001175 if (fscontext) {
1176 opts->mnt_opts[num_mnt_opts] = fscontext;
1177 opts->mnt_opts_flags[num_mnt_opts++] = FSCONTEXT_MNT;
1178 }
1179 if (context) {
1180 opts->mnt_opts[num_mnt_opts] = context;
1181 opts->mnt_opts_flags[num_mnt_opts++] = CONTEXT_MNT;
1182 }
1183 if (rootcontext) {
1184 opts->mnt_opts[num_mnt_opts] = rootcontext;
1185 opts->mnt_opts_flags[num_mnt_opts++] = ROOTCONTEXT_MNT;
1186 }
1187 if (defcontext) {
1188 opts->mnt_opts[num_mnt_opts] = defcontext;
1189 opts->mnt_opts_flags[num_mnt_opts++] = DEFCONTEXT_MNT;
1190 }
1191
1192 opts->num_mnt_opts = num_mnt_opts;
1193 return 0;
1194
Eric Parisc9180a52007-11-30 13:00:35 -05001195out_err:
Paul Moore023f1082017-06-07 16:48:19 -04001196 security_free_mnt_opts(opts);
Eric Parisc9180a52007-11-30 13:00:35 -05001197 kfree(context);
1198 kfree(defcontext);
1199 kfree(fscontext);
1200 kfree(rootcontext);
1201 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202}
1203
Adrian Bunk3583a712008-07-22 20:21:23 +03001204static void selinux_write_opts(struct seq_file *m,
1205 struct security_mnt_opts *opts)
Eric Paris2069f452008-07-04 09:47:13 +10001206{
1207 int i;
1208 char *prefix;
1209
1210 for (i = 0; i < opts->num_mnt_opts; i++) {
David P. Quigley11689d42009-01-16 09:22:03 -05001211 char *has_comma;
1212
1213 if (opts->mnt_opts[i])
1214 has_comma = strchr(opts->mnt_opts[i], ',');
1215 else
1216 has_comma = NULL;
Eric Paris2069f452008-07-04 09:47:13 +10001217
1218 switch (opts->mnt_opts_flags[i]) {
1219 case CONTEXT_MNT:
1220 prefix = CONTEXT_STR;
1221 break;
1222 case FSCONTEXT_MNT:
1223 prefix = FSCONTEXT_STR;
1224 break;
1225 case ROOTCONTEXT_MNT:
1226 prefix = ROOTCONTEXT_STR;
1227 break;
1228 case DEFCONTEXT_MNT:
1229 prefix = DEFCONTEXT_STR;
1230 break;
Eric Paris12f348b2012-10-09 10:56:25 -04001231 case SBLABEL_MNT:
David P. Quigley11689d42009-01-16 09:22:03 -05001232 seq_putc(m, ',');
1233 seq_puts(m, LABELSUPP_STR);
1234 continue;
Eric Paris2069f452008-07-04 09:47:13 +10001235 default:
1236 BUG();
Eric Parisa35c6c832011-04-20 10:21:28 -04001237 return;
Eric Paris2069f452008-07-04 09:47:13 +10001238 };
1239 /* we need a comma before each option */
1240 seq_putc(m, ',');
1241 seq_puts(m, prefix);
1242 if (has_comma)
1243 seq_putc(m, '\"');
Kees Cooka068acf2015-09-04 15:44:57 -07001244 seq_escape(m, opts->mnt_opts[i], "\"\n\\");
Eric Paris2069f452008-07-04 09:47:13 +10001245 if (has_comma)
1246 seq_putc(m, '\"');
1247 }
1248}
1249
1250static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
1251{
1252 struct security_mnt_opts opts;
1253 int rc;
1254
1255 rc = selinux_get_mnt_opts(sb, &opts);
Eric Paris383795c2008-07-29 17:07:26 -04001256 if (rc) {
1257 /* before policy load we may get EINVAL, don't show anything */
1258 if (rc == -EINVAL)
1259 rc = 0;
Eric Paris2069f452008-07-04 09:47:13 +10001260 return rc;
Eric Paris383795c2008-07-29 17:07:26 -04001261 }
Eric Paris2069f452008-07-04 09:47:13 +10001262
1263 selinux_write_opts(m, &opts);
1264
1265 security_free_mnt_opts(&opts);
1266
1267 return rc;
1268}
1269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270static inline u16 inode_mode_to_security_class(umode_t mode)
1271{
1272 switch (mode & S_IFMT) {
1273 case S_IFSOCK:
1274 return SECCLASS_SOCK_FILE;
1275 case S_IFLNK:
1276 return SECCLASS_LNK_FILE;
1277 case S_IFREG:
1278 return SECCLASS_FILE;
1279 case S_IFBLK:
1280 return SECCLASS_BLK_FILE;
1281 case S_IFDIR:
1282 return SECCLASS_DIR;
1283 case S_IFCHR:
1284 return SECCLASS_CHR_FILE;
1285 case S_IFIFO:
1286 return SECCLASS_FIFO_FILE;
1287
1288 }
1289
1290 return SECCLASS_FILE;
1291}
1292
James Morris13402582005-09-30 14:24:34 -04001293static inline int default_protocol_stream(int protocol)
1294{
1295 return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP);
1296}
1297
1298static inline int default_protocol_dgram(int protocol)
1299{
1300 return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
1301}
1302
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303static inline u16 socket_type_to_security_class(int family, int type, int protocol)
1304{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001305 int extsockclass = selinux_policycap_extsockclass();
Stephen Smalleyda69a532017-01-09 10:07:30 -05001306
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 switch (family) {
1308 case PF_UNIX:
1309 switch (type) {
1310 case SOCK_STREAM:
1311 case SOCK_SEQPACKET:
1312 return SECCLASS_UNIX_STREAM_SOCKET;
1313 case SOCK_DGRAM:
Luis Ressel2a764b52017-07-25 15:13:41 -04001314 case SOCK_RAW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 return SECCLASS_UNIX_DGRAM_SOCKET;
1316 }
1317 break;
1318 case PF_INET:
1319 case PF_INET6:
1320 switch (type) {
1321 case SOCK_STREAM:
Stephen Smalleyda69a532017-01-09 10:07:30 -05001322 case SOCK_SEQPACKET:
James Morris13402582005-09-30 14:24:34 -04001323 if (default_protocol_stream(protocol))
1324 return SECCLASS_TCP_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001325 else if (extsockclass && protocol == IPPROTO_SCTP)
1326 return SECCLASS_SCTP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001327 else
1328 return SECCLASS_RAWIP_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 case SOCK_DGRAM:
James Morris13402582005-09-30 14:24:34 -04001330 if (default_protocol_dgram(protocol))
1331 return SECCLASS_UDP_SOCKET;
Stephen Smalleyef379792017-01-09 10:07:31 -05001332 else if (extsockclass && (protocol == IPPROTO_ICMP ||
1333 protocol == IPPROTO_ICMPV6))
Stephen Smalleyda69a532017-01-09 10:07:30 -05001334 return SECCLASS_ICMP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001335 else
1336 return SECCLASS_RAWIP_SOCKET;
James Morris2ee92d42006-11-13 16:09:01 -08001337 case SOCK_DCCP:
1338 return SECCLASS_DCCP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001339 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 return SECCLASS_RAWIP_SOCKET;
1341 }
1342 break;
1343 case PF_NETLINK:
1344 switch (protocol) {
1345 case NETLINK_ROUTE:
1346 return SECCLASS_NETLINK_ROUTE_SOCKET;
Pavel Emelyanov7f1fb602011-12-06 07:56:43 +00001347 case NETLINK_SOCK_DIAG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 return SECCLASS_NETLINK_TCPDIAG_SOCKET;
1349 case NETLINK_NFLOG:
1350 return SECCLASS_NETLINK_NFLOG_SOCKET;
1351 case NETLINK_XFRM:
1352 return SECCLASS_NETLINK_XFRM_SOCKET;
1353 case NETLINK_SELINUX:
1354 return SECCLASS_NETLINK_SELINUX_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001355 case NETLINK_ISCSI:
1356 return SECCLASS_NETLINK_ISCSI_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 case NETLINK_AUDIT:
1358 return SECCLASS_NETLINK_AUDIT_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001359 case NETLINK_FIB_LOOKUP:
1360 return SECCLASS_NETLINK_FIB_LOOKUP_SOCKET;
1361 case NETLINK_CONNECTOR:
1362 return SECCLASS_NETLINK_CONNECTOR_SOCKET;
1363 case NETLINK_NETFILTER:
1364 return SECCLASS_NETLINK_NETFILTER_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 case NETLINK_DNRTMSG:
1366 return SECCLASS_NETLINK_DNRT_SOCKET;
James Morris0c9b7942005-04-16 15:24:13 -07001367 case NETLINK_KOBJECT_UEVENT:
1368 return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001369 case NETLINK_GENERIC:
1370 return SECCLASS_NETLINK_GENERIC_SOCKET;
1371 case NETLINK_SCSITRANSPORT:
1372 return SECCLASS_NETLINK_SCSITRANSPORT_SOCKET;
1373 case NETLINK_RDMA:
1374 return SECCLASS_NETLINK_RDMA_SOCKET;
1375 case NETLINK_CRYPTO:
1376 return SECCLASS_NETLINK_CRYPTO_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 default:
1378 return SECCLASS_NETLINK_SOCKET;
1379 }
1380 case PF_PACKET:
1381 return SECCLASS_PACKET_SOCKET;
1382 case PF_KEY:
1383 return SECCLASS_KEY_SOCKET;
Christopher J. PeBenito3e3ff152006-06-09 00:25:03 -07001384 case PF_APPLETALK:
1385 return SECCLASS_APPLETALK_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 }
1387
Stephen Smalleyda69a532017-01-09 10:07:30 -05001388 if (extsockclass) {
1389 switch (family) {
1390 case PF_AX25:
1391 return SECCLASS_AX25_SOCKET;
1392 case PF_IPX:
1393 return SECCLASS_IPX_SOCKET;
1394 case PF_NETROM:
1395 return SECCLASS_NETROM_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001396 case PF_ATMPVC:
1397 return SECCLASS_ATMPVC_SOCKET;
1398 case PF_X25:
1399 return SECCLASS_X25_SOCKET;
1400 case PF_ROSE:
1401 return SECCLASS_ROSE_SOCKET;
1402 case PF_DECnet:
1403 return SECCLASS_DECNET_SOCKET;
1404 case PF_ATMSVC:
1405 return SECCLASS_ATMSVC_SOCKET;
1406 case PF_RDS:
1407 return SECCLASS_RDS_SOCKET;
1408 case PF_IRDA:
1409 return SECCLASS_IRDA_SOCKET;
1410 case PF_PPPOX:
1411 return SECCLASS_PPPOX_SOCKET;
1412 case PF_LLC:
1413 return SECCLASS_LLC_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001414 case PF_CAN:
1415 return SECCLASS_CAN_SOCKET;
1416 case PF_TIPC:
1417 return SECCLASS_TIPC_SOCKET;
1418 case PF_BLUETOOTH:
1419 return SECCLASS_BLUETOOTH_SOCKET;
1420 case PF_IUCV:
1421 return SECCLASS_IUCV_SOCKET;
1422 case PF_RXRPC:
1423 return SECCLASS_RXRPC_SOCKET;
1424 case PF_ISDN:
1425 return SECCLASS_ISDN_SOCKET;
1426 case PF_PHONET:
1427 return SECCLASS_PHONET_SOCKET;
1428 case PF_IEEE802154:
1429 return SECCLASS_IEEE802154_SOCKET;
1430 case PF_CAIF:
1431 return SECCLASS_CAIF_SOCKET;
1432 case PF_ALG:
1433 return SECCLASS_ALG_SOCKET;
1434 case PF_NFC:
1435 return SECCLASS_NFC_SOCKET;
1436 case PF_VSOCK:
1437 return SECCLASS_VSOCK_SOCKET;
1438 case PF_KCM:
1439 return SECCLASS_KCM_SOCKET;
1440 case PF_QIPCRTR:
1441 return SECCLASS_QIPCRTR_SOCKET;
Linus Torvalds3051bf32017-02-22 10:15:09 -08001442 case PF_SMC:
1443 return SECCLASS_SMC_SOCKET;
Björn Töpel68e8b842018-05-02 13:01:22 +02001444 case PF_XDP:
1445 return SECCLASS_XDP_SOCKET;
1446#if PF_MAX > 45
Stephen Smalleyda69a532017-01-09 10:07:30 -05001447#error New address family defined, please update this function.
1448#endif
1449 }
1450 }
1451
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 return SECCLASS_SOCKET;
1453}
1454
Stephen Smalley134509d2015-06-04 16:22:17 -04001455static int selinux_genfs_get_sid(struct dentry *dentry,
1456 u16 tclass,
1457 u16 flags,
1458 u32 *sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459{
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001460 int rc;
Al Virofc640052016-04-10 01:33:30 -04001461 struct super_block *sb = dentry->d_sb;
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001462 char *buffer, *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
Eric Paris828dfe12008-04-17 13:17:49 -04001464 buffer = (char *)__get_free_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 if (!buffer)
1466 return -ENOMEM;
1467
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001468 path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
1469 if (IS_ERR(path))
1470 rc = PTR_ERR(path);
1471 else {
Stephen Smalley134509d2015-06-04 16:22:17 -04001472 if (flags & SE_SBPROC) {
1473 /* each process gets a /proc/PID/ entry. Strip off the
1474 * PID part to get a valid selinux labeling.
1475 * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
1476 while (path[1] >= '0' && path[1] <= '9') {
1477 path[1] = '/';
1478 path++;
1479 }
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001480 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001481 rc = security_genfs_sid(&selinux_state, sb->s_type->name,
1482 path, tclass, sid);
Stephen Smalley7bb185e2018-09-04 16:51:36 -04001483 if (rc == -ENOENT) {
1484 /* No match in policy, mark as unlabeled. */
1485 *sid = SECINITSID_UNLABELED;
1486 rc = 0;
1487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 free_page((unsigned long)buffer);
1490 return rc;
1491}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
1493/* The inode's security attributes must be initialized before first use. */
1494static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
1495{
1496 struct superblock_security_struct *sbsec = NULL;
1497 struct inode_security_struct *isec = inode->i_security;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001498 u32 task_sid, sid = 0;
1499 u16 sclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 struct dentry *dentry;
1501#define INITCONTEXTLEN 255
1502 char *context = NULL;
1503 unsigned len = 0;
1504 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05001506 if (isec->initialized == LABEL_INITIALIZED)
Andreas Gruenbacher13457d02016-11-10 22:18:29 +01001507 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001509 spin_lock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05001510 if (isec->initialized == LABEL_INITIALIZED)
Eric Paris23970742006-09-25 23:32:01 -07001511 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Andreas Gruenbacher13457d02016-11-10 22:18:29 +01001513 if (isec->sclass == SECCLASS_FILE)
1514 isec->sclass = inode_mode_to_security_class(inode->i_mode);
1515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 sbsec = inode->i_sb->s_security;
David P. Quigley0d90a7e2009-01-16 09:22:02 -05001517 if (!(sbsec->flags & SE_SBINITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 /* Defer initialization until selinux_complete_init,
1519 after the initial policy is loaded and the security
1520 server is ready to handle calls. */
1521 spin_lock(&sbsec->isec_lock);
1522 if (list_empty(&isec->list))
1523 list_add(&isec->list, &sbsec->isec_head);
1524 spin_unlock(&sbsec->isec_lock);
Eric Paris23970742006-09-25 23:32:01 -07001525 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 }
1527
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001528 sclass = isec->sclass;
1529 task_sid = isec->task_sid;
1530 sid = isec->sid;
1531 isec->initialized = LABEL_PENDING;
1532 spin_unlock(&isec->lock);
1533
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 switch (sbsec->behavior) {
David Quigleyeb9ae682013-05-22 12:50:37 -04001535 case SECURITY_FS_USE_NATIVE:
1536 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 case SECURITY_FS_USE_XATTR:
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001538 if (!(inode->i_opflags & IOP_XATTR)) {
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001539 sid = sbsec->def_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 break;
1541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 /* Need a dentry, since the xattr API requires one.
1543 Life would be simpler if we could just pass the inode. */
1544 if (opt_dentry) {
1545 /* Called from d_instantiate or d_splice_alias. */
1546 dentry = dget(opt_dentry);
1547 } else {
Al Virob1271252018-04-25 10:28:38 -04001548 /*
1549 * Called from selinux_complete_init, try to find a dentry.
1550 * Some filesystems really want a connected one, so try
1551 * that first. We could split SECURITY_FS_USE_XATTR in
1552 * two, depending upon that...
1553 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 dentry = d_find_alias(inode);
Al Virob1271252018-04-25 10:28:38 -04001555 if (!dentry)
1556 dentry = d_find_any_alias(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 }
1558 if (!dentry) {
Eric Parisdf7f54c2009-03-09 14:35:58 -04001559 /*
1560 * this is can be hit on boot when a file is accessed
1561 * before the policy is loaded. When we load policy we
1562 * may find inodes that have no dentry on the
1563 * sbsec->isec_head list. No reason to complain as these
1564 * will get fixed up the next time we go through
1565 * inode_doinit with a dentry, before these inodes could
1566 * be used again by userspace.
1567 */
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001568 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 }
1570
1571 len = INITCONTEXTLEN;
Eric Paris4cb912f2009-02-12 14:50:05 -05001572 context = kmalloc(len+1, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 if (!context) {
1574 rc = -ENOMEM;
1575 dput(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001576 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 }
Eric Paris4cb912f2009-02-12 14:50:05 -05001578 context[len] = '\0';
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001579 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 if (rc == -ERANGE) {
James Morris314dabb2009-08-10 22:00:13 +10001581 kfree(context);
1582
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 /* Need a larger buffer. Query for the right size. */
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001584 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 if (rc < 0) {
1586 dput(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001587 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 len = rc;
Eric Paris4cb912f2009-02-12 14:50:05 -05001590 context = kmalloc(len+1, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 if (!context) {
1592 rc = -ENOMEM;
1593 dput(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001594 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 }
Eric Paris4cb912f2009-02-12 14:50:05 -05001596 context[len] = '\0';
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001597 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 }
1599 dput(dentry);
1600 if (rc < 0) {
1601 if (rc != -ENODATA) {
peter enderborgc103a912018-06-12 10:09:03 +02001602 pr_warn("SELinux: %s: getxattr returned "
Harvey Harrisondd6f9532008-03-06 10:03:59 +11001603 "%d for dev=%s ino=%ld\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 -rc, inode->i_sb->s_id, inode->i_ino);
1605 kfree(context);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001606 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 }
1608 /* Map ENODATA to the default file SID */
1609 sid = sbsec->def_sid;
1610 rc = 0;
1611 } else {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001612 rc = security_context_to_sid_default(&selinux_state,
1613 context, rc, &sid,
Stephen Smalley869ab512008-04-04 08:46:05 -04001614 sbsec->def_sid,
1615 GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 if (rc) {
Eric Paris4ba0a8a2009-02-12 15:01:10 -05001617 char *dev = inode->i_sb->s_id;
1618 unsigned long ino = inode->i_ino;
1619
1620 if (rc == -EINVAL) {
1621 if (printk_ratelimit())
peter enderborgc103a912018-06-12 10:09:03 +02001622 pr_notice("SELinux: inode=%lu on dev=%s was found to have an invalid "
Eric Paris4ba0a8a2009-02-12 15:01:10 -05001623 "context=%s. This indicates you may need to relabel the inode or the "
1624 "filesystem in question.\n", ino, dev, context);
1625 } else {
peter enderborgc103a912018-06-12 10:09:03 +02001626 pr_warn("SELinux: %s: context_to_sid(%s) "
Eric Paris4ba0a8a2009-02-12 15:01:10 -05001627 "returned %d for dev=%s ino=%ld\n",
1628 __func__, context, -rc, dev, ino);
1629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 kfree(context);
1631 /* Leave with the unlabeled SID */
1632 rc = 0;
1633 break;
1634 }
1635 }
1636 kfree(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 break;
1638 case SECURITY_FS_USE_TASK:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001639 sid = task_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 break;
1641 case SECURITY_FS_USE_TRANS:
1642 /* Default to the fs SID. */
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001643 sid = sbsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
1645 /* Try to obtain a transition SID. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001646 rc = security_transition_sid(&selinux_state, task_sid, sid,
1647 sclass, NULL, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 if (rc)
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001649 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 break;
Eric Parisc312feb2006-07-10 04:43:53 -07001651 case SECURITY_FS_USE_MNTPOINT:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001652 sid = sbsec->mntpoint_sid;
Eric Parisc312feb2006-07-10 04:43:53 -07001653 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 default:
Eric Parisc312feb2006-07-10 04:43:53 -07001655 /* Default to the fs superblock SID. */
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001656 sid = sbsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
Stephen Smalley134509d2015-06-04 16:22:17 -04001658 if ((sbsec->flags & SE_SBGENFS) && !S_ISLNK(inode->i_mode)) {
Paul Mooref64410e2014-03-19 16:46:18 -04001659 /* We must have a dentry to determine the label on
1660 * procfs inodes */
Al Virob1271252018-04-25 10:28:38 -04001661 if (opt_dentry) {
Paul Mooref64410e2014-03-19 16:46:18 -04001662 /* Called from d_instantiate or
1663 * d_splice_alias. */
1664 dentry = dget(opt_dentry);
Al Virob1271252018-04-25 10:28:38 -04001665 } else {
Paul Mooref64410e2014-03-19 16:46:18 -04001666 /* Called from selinux_complete_init, try to
Al Virob1271252018-04-25 10:28:38 -04001667 * find a dentry. Some filesystems really want
1668 * a connected one, so try that first.
1669 */
Paul Mooref64410e2014-03-19 16:46:18 -04001670 dentry = d_find_alias(inode);
Al Virob1271252018-04-25 10:28:38 -04001671 if (!dentry)
1672 dentry = d_find_any_alias(inode);
1673 }
Paul Mooref64410e2014-03-19 16:46:18 -04001674 /*
1675 * This can be hit on boot when a file is accessed
1676 * before the policy is loaded. When we load policy we
1677 * may find inodes that have no dentry on the
1678 * sbsec->isec_head list. No reason to complain as
1679 * these will get fixed up the next time we go through
1680 * inode_doinit() with a dentry, before these inodes
1681 * could be used again by userspace.
1682 */
1683 if (!dentry)
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001684 goto out;
1685 rc = selinux_genfs_get_sid(dentry, sclass,
Stephen Smalley134509d2015-06-04 16:22:17 -04001686 sbsec->flags, &sid);
Paul Mooref64410e2014-03-19 16:46:18 -04001687 dput(dentry);
1688 if (rc)
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001689 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 }
1691 break;
1692 }
1693
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001694out:
1695 spin_lock(&isec->lock);
1696 if (isec->initialized == LABEL_PENDING) {
1697 if (!sid || rc) {
1698 isec->initialized = LABEL_INVALID;
1699 goto out_unlock;
1700 }
1701
1702 isec->initialized = LABEL_INITIALIZED;
1703 isec->sid = sid;
1704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
Eric Paris23970742006-09-25 23:32:01 -07001706out_unlock:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001707 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 return rc;
1709}
1710
1711/* Convert a Linux signal to an access vector. */
1712static inline u32 signal_to_av(int sig)
1713{
1714 u32 perm = 0;
1715
1716 switch (sig) {
1717 case SIGCHLD:
1718 /* Commonly granted from child to parent. */
1719 perm = PROCESS__SIGCHLD;
1720 break;
1721 case SIGKILL:
1722 /* Cannot be caught or ignored */
1723 perm = PROCESS__SIGKILL;
1724 break;
1725 case SIGSTOP:
1726 /* Cannot be caught or ignored */
1727 perm = PROCESS__SIGSTOP;
1728 break;
1729 default:
1730 /* All other signals. */
1731 perm = PROCESS__SIGNAL;
1732 break;
1733 }
1734
1735 return perm;
1736}
1737
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001738#if CAP_LAST_CAP > 63
1739#error Fix SELinux to handle capabilities > 63.
1740#endif
1741
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742/* Check whether a task is allowed to use a capability. */
Eric Paris6a9de492012-01-03 12:25:14 -05001743static int cred_has_capability(const struct cred *cred,
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04001744 int cap, int audit, bool initns)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745{
Thomas Liu2bf49692009-07-14 12:14:09 -04001746 struct common_audit_data ad;
Eric Paris06112162008-11-11 22:02:50 +11001747 struct av_decision avd;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001748 u16 sclass;
David Howells3699c532009-01-06 22:27:01 +00001749 u32 sid = cred_sid(cred);
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001750 u32 av = CAP_TO_MASK(cap);
Eric Paris06112162008-11-11 22:02:50 +11001751 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Eric Paris50c205f2012-04-04 15:01:43 -04001753 ad.type = LSM_AUDIT_DATA_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 ad.u.cap = cap;
1755
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001756 switch (CAP_TO_INDEX(cap)) {
1757 case 0:
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04001758 sclass = initns ? SECCLASS_CAPABILITY : SECCLASS_CAP_USERNS;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001759 break;
1760 case 1:
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04001761 sclass = initns ? SECCLASS_CAPABILITY2 : SECCLASS_CAP2_USERNS;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001762 break;
1763 default:
peter enderborgc103a912018-06-12 10:09:03 +02001764 pr_err("SELinux: out of range capability %d\n", cap);
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001765 BUG();
Eric Parisa35c6c832011-04-20 10:21:28 -04001766 return -EINVAL;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001767 }
Eric Paris06112162008-11-11 22:02:50 +11001768
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001769 rc = avc_has_perm_noaudit(&selinux_state,
1770 sid, sid, sclass, av, 0, &avd);
Eric Paris9ade0cf2011-04-25 16:26:29 -04001771 if (audit == SECURITY_CAP_AUDIT) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001772 int rc2 = avc_audit(&selinux_state,
1773 sid, sid, sclass, av, &avd, rc, &ad, 0);
Eric Paris9ade0cf2011-04-25 16:26:29 -04001774 if (rc2)
1775 return rc2;
1776 }
Eric Paris06112162008-11-11 22:02:50 +11001777 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778}
1779
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780/* Check whether a task has a particular permission to an inode.
1781 The 'adp' parameter is optional and allows other audit
1782 data to be passed (e.g. the dentry). */
David Howells88e67f32008-11-14 10:39:21 +11001783static int inode_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 struct inode *inode,
1785 u32 perms,
Linus Torvalds19e49832013-10-04 12:54:11 -07001786 struct common_audit_data *adp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 struct inode_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +11001789 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
David Howellse0e81732009-09-02 09:13:40 +01001791 validate_creds(cred);
1792
Eric Paris828dfe12008-04-17 13:17:49 -04001793 if (unlikely(IS_PRIVATE(inode)))
Stephen Smalleybbaca6c2007-02-14 00:34:16 -08001794 return 0;
1795
David Howells88e67f32008-11-14 10:39:21 +11001796 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 isec = inode->i_security;
1798
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001799 return avc_has_perm(&selinux_state,
1800 sid, isec->sid, isec->sclass, perms, adp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801}
1802
1803/* Same as inode_has_perm, but pass explicit audit data containing
1804 the dentry to help the auditing code to more easily generate the
1805 pathname if needed. */
David Howells88e67f32008-11-14 10:39:21 +11001806static inline int dentry_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 struct dentry *dentry,
1808 u32 av)
1809{
David Howellsc6f493d2015-03-17 22:26:22 +00001810 struct inode *inode = d_backing_inode(dentry);
Thomas Liu2bf49692009-07-14 12:14:09 -04001811 struct common_audit_data ad;
David Howells88e67f32008-11-14 10:39:21 +11001812
Eric Paris50c205f2012-04-04 15:01:43 -04001813 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Paris2875fa02011-04-28 16:04:24 -04001814 ad.u.dentry = dentry;
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05001815 __inode_security_revalidate(inode, dentry, true);
Linus Torvalds19e49832013-10-04 12:54:11 -07001816 return inode_has_perm(cred, inode, av, &ad);
Eric Paris2875fa02011-04-28 16:04:24 -04001817}
1818
1819/* Same as inode_has_perm, but pass explicit audit data containing
1820 the path to help the auditing code to more easily generate the
1821 pathname if needed. */
1822static inline int path_has_perm(const struct cred *cred,
Al Viro3f7036a2015-03-08 19:28:30 -04001823 const struct path *path,
Eric Paris2875fa02011-04-28 16:04:24 -04001824 u32 av)
1825{
David Howellsc6f493d2015-03-17 22:26:22 +00001826 struct inode *inode = d_backing_inode(path->dentry);
Eric Paris2875fa02011-04-28 16:04:24 -04001827 struct common_audit_data ad;
1828
Eric Paris50c205f2012-04-04 15:01:43 -04001829 ad.type = LSM_AUDIT_DATA_PATH;
Eric Paris2875fa02011-04-28 16:04:24 -04001830 ad.u.path = *path;
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05001831 __inode_security_revalidate(inode, path->dentry, true);
Linus Torvalds19e49832013-10-04 12:54:11 -07001832 return inode_has_perm(cred, inode, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833}
1834
David Howells13f8e982013-06-13 23:37:55 +01001835/* Same as path_has_perm, but uses the inode from the file struct. */
1836static inline int file_path_has_perm(const struct cred *cred,
1837 struct file *file,
1838 u32 av)
1839{
1840 struct common_audit_data ad;
1841
Vivek Goyal43af5de2016-09-09 11:37:49 -04001842 ad.type = LSM_AUDIT_DATA_FILE;
1843 ad.u.file = file;
Linus Torvalds19e49832013-10-04 12:54:11 -07001844 return inode_has_perm(cred, file_inode(file), av, &ad);
David Howells13f8e982013-06-13 23:37:55 +01001845}
1846
Chenbo Fengf66e4482017-10-18 13:00:26 -07001847#ifdef CONFIG_BPF_SYSCALL
1848static int bpf_fd_pass(struct file *file, u32 sid);
1849#endif
1850
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851/* Check whether a task can use an open file descriptor to
1852 access an inode in a given way. Check access to the
1853 descriptor itself, and then use dentry_has_perm to
1854 check a particular permission to the file.
1855 Access to the descriptor is implicitly granted if it
1856 has the same SID as the process. If av is zero, then
1857 access to the file is not checked, e.g. for cases
1858 where only the descriptor is affected like seek. */
David Howells88e67f32008-11-14 10:39:21 +11001859static int file_has_perm(const struct cred *cred,
1860 struct file *file,
1861 u32 av)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 struct file_security_struct *fsec = file->f_security;
Al Viro496ad9a2013-01-23 17:07:38 -05001864 struct inode *inode = file_inode(file);
Thomas Liu2bf49692009-07-14 12:14:09 -04001865 struct common_audit_data ad;
David Howells88e67f32008-11-14 10:39:21 +11001866 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 int rc;
1868
Vivek Goyal43af5de2016-09-09 11:37:49 -04001869 ad.type = LSM_AUDIT_DATA_FILE;
1870 ad.u.file = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
David Howells275bb412008-11-14 10:39:19 +11001872 if (sid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001873 rc = avc_has_perm(&selinux_state,
1874 sid, fsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 SECCLASS_FD,
1876 FD__USE,
1877 &ad);
1878 if (rc)
David Howells88e67f32008-11-14 10:39:21 +11001879 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 }
1881
Chenbo Fengf66e4482017-10-18 13:00:26 -07001882#ifdef CONFIG_BPF_SYSCALL
1883 rc = bpf_fd_pass(file, cred_sid(cred));
1884 if (rc)
1885 return rc;
1886#endif
1887
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 /* av is zero if only checking access to the descriptor. */
David Howells88e67f32008-11-14 10:39:21 +11001889 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 if (av)
Linus Torvalds19e49832013-10-04 12:54:11 -07001891 rc = inode_has_perm(cred, inode, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
David Howells88e67f32008-11-14 10:39:21 +11001893out:
1894 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895}
1896
David Howellsc3c188b2015-07-10 17:19:58 -04001897/*
1898 * Determine the label for an inode that might be unioned.
1899 */
Vivek Goyalc957f6d2016-07-13 10:44:51 -04001900static int
1901selinux_determine_inode_label(const struct task_security_struct *tsec,
1902 struct inode *dir,
1903 const struct qstr *name, u16 tclass,
1904 u32 *_new_isid)
David Howellsc3c188b2015-07-10 17:19:58 -04001905{
1906 const struct superblock_security_struct *sbsec = dir->i_sb->s_security;
David Howellsc3c188b2015-07-10 17:19:58 -04001907
1908 if ((sbsec->flags & SE_SBINITIALIZED) &&
1909 (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)) {
1910 *_new_isid = sbsec->mntpoint_sid;
1911 } else if ((sbsec->flags & SBLABEL_MNT) &&
1912 tsec->create_sid) {
1913 *_new_isid = tsec->create_sid;
1914 } else {
Paul Moore20cdef82016-04-04 14:14:42 -04001915 const struct inode_security_struct *dsec = inode_security(dir);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001916 return security_transition_sid(&selinux_state, tsec->sid,
1917 dsec->sid, tclass,
David Howellsc3c188b2015-07-10 17:19:58 -04001918 name, _new_isid);
1919 }
1920
1921 return 0;
1922}
1923
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924/* Check whether a task can create a file. */
1925static int may_create(struct inode *dir,
1926 struct dentry *dentry,
1927 u16 tclass)
1928{
Paul Moore5fb49872010-04-22 14:46:19 -04001929 const struct task_security_struct *tsec = current_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 struct inode_security_struct *dsec;
1931 struct superblock_security_struct *sbsec;
David Howells275bb412008-11-14 10:39:19 +11001932 u32 sid, newsid;
Thomas Liu2bf49692009-07-14 12:14:09 -04001933 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 int rc;
1935
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001936 dsec = inode_security(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 sbsec = dir->i_sb->s_security;
1938
David Howells275bb412008-11-14 10:39:19 +11001939 sid = tsec->sid;
David Howells275bb412008-11-14 10:39:19 +11001940
Eric Paris50c205f2012-04-04 15:01:43 -04001941 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04001942 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001944 rc = avc_has_perm(&selinux_state,
1945 sid, dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 DIR__ADD_NAME | DIR__SEARCH,
1947 &ad);
1948 if (rc)
1949 return rc;
1950
Vivek Goyalc957f6d2016-07-13 10:44:51 -04001951 rc = selinux_determine_inode_label(current_security(), dir,
1952 &dentry->d_name, tclass, &newsid);
David Howellsc3c188b2015-07-10 17:19:58 -04001953 if (rc)
1954 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001956 rc = avc_has_perm(&selinux_state,
1957 sid, newsid, tclass, FILE__CREATE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 if (rc)
1959 return rc;
1960
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001961 return avc_has_perm(&selinux_state,
1962 newsid, sbsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 SECCLASS_FILESYSTEM,
1964 FILESYSTEM__ASSOCIATE, &ad);
1965}
1966
Eric Paris828dfe12008-04-17 13:17:49 -04001967#define MAY_LINK 0
1968#define MAY_UNLINK 1
1969#define MAY_RMDIR 2
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
1971/* Check whether a task can link, unlink, or rmdir a file/directory. */
1972static int may_link(struct inode *dir,
1973 struct dentry *dentry,
1974 int kind)
1975
1976{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 struct inode_security_struct *dsec, *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001978 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11001979 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 u32 av;
1981 int rc;
1982
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001983 dsec = inode_security(dir);
1984 isec = backing_inode_security(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
Eric Paris50c205f2012-04-04 15:01:43 -04001986 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04001987 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
1989 av = DIR__SEARCH;
1990 av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001991 rc = avc_has_perm(&selinux_state,
1992 sid, dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 if (rc)
1994 return rc;
1995
1996 switch (kind) {
1997 case MAY_LINK:
1998 av = FILE__LINK;
1999 break;
2000 case MAY_UNLINK:
2001 av = FILE__UNLINK;
2002 break;
2003 case MAY_RMDIR:
2004 av = DIR__RMDIR;
2005 break;
2006 default:
peter enderborgc103a912018-06-12 10:09:03 +02002007 pr_warn("SELinux: %s: unrecognized kind %d\n",
Eric Paris744ba352008-04-17 11:52:44 -04002008 __func__, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 return 0;
2010 }
2011
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002012 rc = avc_has_perm(&selinux_state,
2013 sid, isec->sid, isec->sclass, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 return rc;
2015}
2016
2017static inline int may_rename(struct inode *old_dir,
2018 struct dentry *old_dentry,
2019 struct inode *new_dir,
2020 struct dentry *new_dentry)
2021{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04002023 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11002024 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 u32 av;
2026 int old_is_dir, new_is_dir;
2027 int rc;
2028
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002029 old_dsec = inode_security(old_dir);
2030 old_isec = backing_inode_security(old_dentry);
David Howellse36cb0b2015-01-29 12:02:35 +00002031 old_is_dir = d_is_dir(old_dentry);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002032 new_dsec = inode_security(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
Eric Paris50c205f2012-04-04 15:01:43 -04002034 ad.type = LSM_AUDIT_DATA_DENTRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
Eric Parisa2694342011-04-25 13:10:27 -04002036 ad.u.dentry = old_dentry;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002037 rc = avc_has_perm(&selinux_state,
2038 sid, old_dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 DIR__REMOVE_NAME | DIR__SEARCH, &ad);
2040 if (rc)
2041 return rc;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002042 rc = avc_has_perm(&selinux_state,
2043 sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 old_isec->sclass, FILE__RENAME, &ad);
2045 if (rc)
2046 return rc;
2047 if (old_is_dir && new_dir != old_dir) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002048 rc = avc_has_perm(&selinux_state,
2049 sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 old_isec->sclass, DIR__REPARENT, &ad);
2051 if (rc)
2052 return rc;
2053 }
2054
Eric Parisa2694342011-04-25 13:10:27 -04002055 ad.u.dentry = new_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 av = DIR__ADD_NAME | DIR__SEARCH;
David Howells2c616d42015-01-29 12:02:33 +00002057 if (d_is_positive(new_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 av |= DIR__REMOVE_NAME;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002059 rc = avc_has_perm(&selinux_state,
2060 sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 if (rc)
2062 return rc;
David Howells2c616d42015-01-29 12:02:33 +00002063 if (d_is_positive(new_dentry)) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002064 new_isec = backing_inode_security(new_dentry);
David Howellse36cb0b2015-01-29 12:02:35 +00002065 new_is_dir = d_is_dir(new_dentry);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002066 rc = avc_has_perm(&selinux_state,
2067 sid, new_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 new_isec->sclass,
2069 (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
2070 if (rc)
2071 return rc;
2072 }
2073
2074 return 0;
2075}
2076
2077/* Check whether a task can perform a filesystem operation. */
David Howells88e67f32008-11-14 10:39:21 +11002078static int superblock_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 struct super_block *sb,
2080 u32 perms,
Thomas Liu2bf49692009-07-14 12:14:09 -04002081 struct common_audit_data *ad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 struct superblock_security_struct *sbsec;
David Howells88e67f32008-11-14 10:39:21 +11002084 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 sbsec = sb->s_security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002087 return avc_has_perm(&selinux_state,
2088 sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089}
2090
2091/* Convert a Linux mode and permission mask to an access vector. */
2092static inline u32 file_mask_to_av(int mode, int mask)
2093{
2094 u32 av = 0;
2095
Al Virodba19c62011-07-25 20:49:29 -04002096 if (!S_ISDIR(mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 if (mask & MAY_EXEC)
2098 av |= FILE__EXECUTE;
2099 if (mask & MAY_READ)
2100 av |= FILE__READ;
2101
2102 if (mask & MAY_APPEND)
2103 av |= FILE__APPEND;
2104 else if (mask & MAY_WRITE)
2105 av |= FILE__WRITE;
2106
2107 } else {
2108 if (mask & MAY_EXEC)
2109 av |= DIR__SEARCH;
2110 if (mask & MAY_WRITE)
2111 av |= DIR__WRITE;
2112 if (mask & MAY_READ)
2113 av |= DIR__READ;
2114 }
2115
2116 return av;
2117}
2118
2119/* Convert a Linux file to an access vector. */
2120static inline u32 file_to_av(struct file *file)
2121{
2122 u32 av = 0;
2123
2124 if (file->f_mode & FMODE_READ)
2125 av |= FILE__READ;
2126 if (file->f_mode & FMODE_WRITE) {
2127 if (file->f_flags & O_APPEND)
2128 av |= FILE__APPEND;
2129 else
2130 av |= FILE__WRITE;
2131 }
Stephen Smalley0794c662008-03-17 08:55:18 -04002132 if (!av) {
2133 /*
2134 * Special file opened with flags 3 for ioctl-only use.
2135 */
2136 av = FILE__IOCTL;
2137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138
2139 return av;
2140}
2141
Eric Paris8b6a5a32008-10-29 17:06:46 -04002142/*
2143 * Convert a file to an access vector and include the correct open
2144 * open permission.
2145 */
2146static inline u32 open_file_to_av(struct file *file)
2147{
2148 u32 av = file_to_av(file);
Stephen Smalleyccb54472017-05-12 12:41:24 -04002149 struct inode *inode = file_inode(file);
Eric Paris8b6a5a32008-10-29 17:06:46 -04002150
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002151 if (selinux_policycap_openperm() &&
2152 inode->i_sb->s_magic != SOCKFS_MAGIC)
Eric Paris49b7b8d2010-07-23 11:44:09 -04002153 av |= FILE__OPEN;
2154
Eric Paris8b6a5a32008-10-29 17:06:46 -04002155 return av;
2156}
2157
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158/* Hook functions begin here. */
2159
Stephen Smalley79af7302015-01-21 10:54:10 -05002160static int selinux_binder_set_context_mgr(struct task_struct *mgr)
2161{
2162 u32 mysid = current_sid();
2163 u32 mgrsid = task_sid(mgr);
2164
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002165 return avc_has_perm(&selinux_state,
2166 mysid, mgrsid, SECCLASS_BINDER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002167 BINDER__SET_CONTEXT_MGR, NULL);
2168}
2169
2170static int selinux_binder_transaction(struct task_struct *from,
2171 struct task_struct *to)
2172{
2173 u32 mysid = current_sid();
2174 u32 fromsid = task_sid(from);
2175 u32 tosid = task_sid(to);
2176 int rc;
2177
2178 if (mysid != fromsid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002179 rc = avc_has_perm(&selinux_state,
2180 mysid, fromsid, SECCLASS_BINDER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002181 BINDER__IMPERSONATE, NULL);
2182 if (rc)
2183 return rc;
2184 }
2185
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002186 return avc_has_perm(&selinux_state,
2187 fromsid, tosid, SECCLASS_BINDER, BINDER__CALL,
Stephen Smalley79af7302015-01-21 10:54:10 -05002188 NULL);
2189}
2190
2191static int selinux_binder_transfer_binder(struct task_struct *from,
2192 struct task_struct *to)
2193{
2194 u32 fromsid = task_sid(from);
2195 u32 tosid = task_sid(to);
2196
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002197 return avc_has_perm(&selinux_state,
2198 fromsid, tosid, SECCLASS_BINDER, BINDER__TRANSFER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002199 NULL);
2200}
2201
2202static int selinux_binder_transfer_file(struct task_struct *from,
2203 struct task_struct *to,
2204 struct file *file)
2205{
2206 u32 sid = task_sid(to);
2207 struct file_security_struct *fsec = file->f_security;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002208 struct dentry *dentry = file->f_path.dentry;
Paul Moore20cdef82016-04-04 14:14:42 -04002209 struct inode_security_struct *isec;
Stephen Smalley79af7302015-01-21 10:54:10 -05002210 struct common_audit_data ad;
2211 int rc;
2212
2213 ad.type = LSM_AUDIT_DATA_PATH;
2214 ad.u.path = file->f_path;
2215
2216 if (sid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002217 rc = avc_has_perm(&selinux_state,
2218 sid, fsec->sid,
Stephen Smalley79af7302015-01-21 10:54:10 -05002219 SECCLASS_FD,
2220 FD__USE,
2221 &ad);
2222 if (rc)
2223 return rc;
2224 }
2225
Chenbo Fengf66e4482017-10-18 13:00:26 -07002226#ifdef CONFIG_BPF_SYSCALL
2227 rc = bpf_fd_pass(file, sid);
2228 if (rc)
2229 return rc;
2230#endif
2231
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002232 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
Stephen Smalley79af7302015-01-21 10:54:10 -05002233 return 0;
2234
Paul Moore20cdef82016-04-04 14:14:42 -04002235 isec = backing_inode_security(dentry);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002236 return avc_has_perm(&selinux_state,
2237 sid, isec->sid, isec->sclass, file_to_av(file),
Stephen Smalley79af7302015-01-21 10:54:10 -05002238 &ad);
2239}
2240
Ingo Molnar9e488582009-05-07 19:26:19 +10002241static int selinux_ptrace_access_check(struct task_struct *child,
David Howells5cd9c582008-08-14 11:37:28 +01002242 unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002244 u32 sid = current_sid();
2245 u32 csid = task_sid(child);
Stephen Smalley006ebb42008-05-19 08:32:49 -04002246
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002247 if (mode & PTRACE_MODE_READ)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002248 return avc_has_perm(&selinux_state,
2249 sid, csid, SECCLASS_FILE, FILE__READ, NULL);
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002250
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002251 return avc_has_perm(&selinux_state,
2252 sid, csid, SECCLASS_PROCESS, PROCESS__PTRACE, NULL);
David Howells5cd9c582008-08-14 11:37:28 +01002253}
2254
2255static int selinux_ptrace_traceme(struct task_struct *parent)
2256{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002257 return avc_has_perm(&selinux_state,
2258 task_sid(parent), current_sid(), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002259 PROCESS__PTRACE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260}
2261
2262static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
Eric Paris828dfe12008-04-17 13:17:49 -04002263 kernel_cap_t *inheritable, kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002265 return avc_has_perm(&selinux_state,
2266 current_sid(), task_sid(target), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002267 PROCESS__GETCAP, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268}
2269
David Howellsd84f4f92008-11-14 10:39:23 +11002270static int selinux_capset(struct cred *new, const struct cred *old,
2271 const kernel_cap_t *effective,
2272 const kernel_cap_t *inheritable,
2273 const kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002275 return avc_has_perm(&selinux_state,
2276 cred_sid(old), cred_sid(new), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002277 PROCESS__SETCAP, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278}
2279
James Morris5626d3e2009-01-30 10:05:06 +11002280/*
2281 * (This comment used to live with the selinux_task_setuid hook,
2282 * which was removed).
2283 *
2284 * Since setuid only affects the current process, and since the SELinux
2285 * controls are not based on the Linux identity attributes, SELinux does not
2286 * need to control this operation. However, SELinux does control the use of
2287 * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
2288 */
2289
Eric Paris6a9de492012-01-03 12:25:14 -05002290static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
2291 int cap, int audit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292{
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04002293 return cred_has_capability(cred, cap, audit, ns == &init_user_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294}
2295
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
2297{
David Howells88e67f32008-11-14 10:39:21 +11002298 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 int rc = 0;
2300
2301 if (!sb)
2302 return 0;
2303
2304 switch (cmds) {
Eric Paris828dfe12008-04-17 13:17:49 -04002305 case Q_SYNC:
2306 case Q_QUOTAON:
2307 case Q_QUOTAOFF:
2308 case Q_SETINFO:
2309 case Q_SETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11002310 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04002311 break;
2312 case Q_GETFMT:
2313 case Q_GETINFO:
2314 case Q_GETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11002315 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04002316 break;
2317 default:
2318 rc = 0; /* let the kernel handle invalid cmds */
2319 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 }
2321 return rc;
2322}
2323
2324static int selinux_quota_on(struct dentry *dentry)
2325{
David Howells88e67f32008-11-14 10:39:21 +11002326 const struct cred *cred = current_cred();
2327
Eric Paris2875fa02011-04-28 16:04:24 -04002328 return dentry_has_perm(cred, dentry, FILE__QUOTAON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329}
2330
Eric Paris12b30522010-11-15 18:36:29 -05002331static int selinux_syslog(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 switch (type) {
Kees Cookd78ca3c2010-02-03 15:37:13 -08002334 case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */
2335 case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002336 return avc_has_perm(&selinux_state,
2337 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002338 SECCLASS_SYSTEM, SYSTEM__SYSLOG_READ, NULL);
Kees Cookd78ca3c2010-02-03 15:37:13 -08002339 case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */
2340 case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */
2341 /* Set level of messages printed to console */
2342 case SYSLOG_ACTION_CONSOLE_LEVEL:
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002343 return avc_has_perm(&selinux_state,
2344 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002345 SECCLASS_SYSTEM, SYSTEM__SYSLOG_CONSOLE,
2346 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 }
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002348 /* All other syslog types */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002349 return avc_has_perm(&selinux_state,
2350 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002351 SECCLASS_SYSTEM, SYSTEM__SYSLOG_MOD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352}
2353
2354/*
2355 * Check that a process has enough memory to allocate a new virtual
2356 * mapping. 0 means there is enough memory for the allocation to
2357 * succeed and -ENOMEM implies there is not.
2358 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 * Do not audit the selinux permission check, as this is applied to all
2360 * processes that allocate mappings.
2361 */
Alan Cox34b4e4a2007-08-22 14:01:28 -07002362static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363{
2364 int rc, cap_sys_admin = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002366 rc = cred_has_capability(current_cred(), CAP_SYS_ADMIN,
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04002367 SECURITY_CAP_NOAUDIT, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 if (rc == 0)
2369 cap_sys_admin = 1;
2370
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002371 return cap_sys_admin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372}
2373
2374/* binprm security operations */
2375
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002376static u32 ptrace_parent_sid(void)
Paul Moore0c6181c2016-03-30 21:41:21 -04002377{
2378 u32 sid = 0;
2379 struct task_struct *tracer;
2380
2381 rcu_read_lock();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002382 tracer = ptrace_parent(current);
Paul Moore0c6181c2016-03-30 21:41:21 -04002383 if (tracer)
2384 sid = task_sid(tracer);
2385 rcu_read_unlock();
2386
2387 return sid;
2388}
2389
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002390static int check_nnp_nosuid(const struct linux_binprm *bprm,
2391 const struct task_security_struct *old_tsec,
2392 const struct task_security_struct *new_tsec)
2393{
2394 int nnp = (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS);
Andy Lutomirski380cf5b2016-06-23 16:41:05 -05002395 int nosuid = !mnt_may_suid(bprm->file->f_path.mnt);
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002396 int rc;
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002397 u32 av;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002398
2399 if (!nnp && !nosuid)
2400 return 0; /* neither NNP nor nosuid */
2401
2402 if (new_tsec->sid == old_tsec->sid)
2403 return 0; /* No change in credentials */
2404
2405 /*
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002406 * If the policy enables the nnp_nosuid_transition policy capability,
2407 * then we permit transitions under NNP or nosuid if the
2408 * policy allows the corresponding permission between
2409 * the old and new contexts.
2410 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002411 if (selinux_policycap_nnp_nosuid_transition()) {
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002412 av = 0;
2413 if (nnp)
2414 av |= PROCESS2__NNP_TRANSITION;
2415 if (nosuid)
2416 av |= PROCESS2__NOSUID_TRANSITION;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002417 rc = avc_has_perm(&selinux_state,
2418 old_tsec->sid, new_tsec->sid,
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002419 SECCLASS_PROCESS2, av, NULL);
2420 if (!rc)
2421 return 0;
2422 }
2423
2424 /*
2425 * We also permit NNP or nosuid transitions to bounded SIDs,
2426 * i.e. SIDs that are guaranteed to only be allowed a subset
2427 * of the permissions of the current SID.
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002428 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002429 rc = security_bounded_transition(&selinux_state, old_tsec->sid,
2430 new_tsec->sid);
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002431 if (!rc)
2432 return 0;
2433
2434 /*
2435 * On failure, preserve the errno values for NNP vs nosuid.
2436 * NNP: Operation not permitted for caller.
2437 * nosuid: Permission denied to file.
2438 */
2439 if (nnp)
2440 return -EPERM;
2441 return -EACCES;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002442}
2443
David Howellsa6f76f22008-11-14 10:39:24 +11002444static int selinux_bprm_set_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445{
David Howellsa6f76f22008-11-14 10:39:24 +11002446 const struct task_security_struct *old_tsec;
2447 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 struct inode_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04002449 struct common_audit_data ad;
Al Viro496ad9a2013-01-23 17:07:38 -05002450 struct inode *inode = file_inode(bprm->file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 int rc;
2452
David Howellsa6f76f22008-11-14 10:39:24 +11002453 /* SELinux context only depends on initial program or script and not
2454 * the script interpreter */
Kees Cookddb4a142017-07-18 15:25:23 -07002455 if (bprm->called_set_creds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 return 0;
2457
David Howellsa6f76f22008-11-14 10:39:24 +11002458 old_tsec = current_security();
2459 new_tsec = bprm->cred->security;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002460 isec = inode_security(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461
2462 /* Default to the current task SID. */
David Howellsa6f76f22008-11-14 10:39:24 +11002463 new_tsec->sid = old_tsec->sid;
2464 new_tsec->osid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465
Michael LeMay28eba5b2006-06-27 02:53:42 -07002466 /* Reset fs, key, and sock SIDs on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002467 new_tsec->create_sid = 0;
2468 new_tsec->keycreate_sid = 0;
2469 new_tsec->sockcreate_sid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
David Howellsa6f76f22008-11-14 10:39:24 +11002471 if (old_tsec->exec_sid) {
2472 new_tsec->sid = old_tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 /* Reset exec SID on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002474 new_tsec->exec_sid = 0;
Andy Lutomirski259e5e62012-04-12 16:47:50 -05002475
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002476 /* Fail on NNP or nosuid if not an allowed transition. */
2477 rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
2478 if (rc)
2479 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 } else {
2481 /* Check for a default transition on this program. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002482 rc = security_transition_sid(&selinux_state, old_tsec->sid,
2483 isec->sid, SECCLASS_PROCESS, NULL,
Eric Paris652bb9b2011-02-01 11:05:40 -05002484 &new_tsec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 if (rc)
2486 return rc;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002487
2488 /*
2489 * Fallback to old SID on NNP or nosuid if not an allowed
2490 * transition.
2491 */
2492 rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
2493 if (rc)
2494 new_tsec->sid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 }
2496
Vivek Goyal43af5de2016-09-09 11:37:49 -04002497 ad.type = LSM_AUDIT_DATA_FILE;
2498 ad.u.file = bprm->file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499
David Howellsa6f76f22008-11-14 10:39:24 +11002500 if (new_tsec->sid == old_tsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002501 rc = avc_has_perm(&selinux_state,
2502 old_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
2504 if (rc)
2505 return rc;
2506 } else {
2507 /* Check permissions for the transition. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002508 rc = avc_has_perm(&selinux_state,
2509 old_tsec->sid, new_tsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
2511 if (rc)
2512 return rc;
2513
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002514 rc = avc_has_perm(&selinux_state,
2515 new_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
2517 if (rc)
2518 return rc;
2519
David Howellsa6f76f22008-11-14 10:39:24 +11002520 /* Check for shared state */
2521 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002522 rc = avc_has_perm(&selinux_state,
2523 old_tsec->sid, new_tsec->sid,
David Howellsa6f76f22008-11-14 10:39:24 +11002524 SECCLASS_PROCESS, PROCESS__SHARE,
2525 NULL);
2526 if (rc)
2527 return -EPERM;
2528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529
David Howellsa6f76f22008-11-14 10:39:24 +11002530 /* Make sure that anyone attempting to ptrace over a task that
2531 * changes its SID has the appropriate permit */
Eric W. Biederman9227dd22017-01-23 17:26:31 +13002532 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002533 u32 ptsid = ptrace_parent_sid();
David Howellsa6f76f22008-11-14 10:39:24 +11002534 if (ptsid != 0) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002535 rc = avc_has_perm(&selinux_state,
2536 ptsid, new_tsec->sid,
David Howellsa6f76f22008-11-14 10:39:24 +11002537 SECCLASS_PROCESS,
2538 PROCESS__PTRACE, NULL);
2539 if (rc)
2540 return -EPERM;
2541 }
2542 }
2543
2544 /* Clear any possibly unsafe personality bits on exec: */
2545 bprm->per_clear |= PER_CLEAR_ON_SETID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 /* Enable secure mode for SIDs transitions unless
2548 the noatsecure permission is granted between
2549 the two SIDs, i.e. ahp returns 0. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002550 rc = avc_has_perm(&selinux_state,
2551 old_tsec->sid, new_tsec->sid,
Kees Cook62874c32017-07-18 15:25:25 -07002552 SECCLASS_PROCESS, PROCESS__NOATSECURE,
2553 NULL);
2554 bprm->secureexec |= !!rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 }
2556
Kees Cook62874c32017-07-18 15:25:25 -07002557 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558}
2559
Al Viroc3c073f2012-08-21 22:32:06 -04002560static int match_file(const void *p, struct file *file, unsigned fd)
2561{
2562 return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0;
2563}
2564
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565/* Derived from fs/exec.c:flush_old_files. */
David Howells745ca242008-11-14 10:39:22 +11002566static inline void flush_unauthorized_files(const struct cred *cred,
2567 struct files_struct *files)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 struct file *file, *devnull = NULL;
Stephen Smalleyb20c8122006-09-25 23:32:03 -07002570 struct tty_struct *tty;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002571 int drop_tty = 0;
Al Viroc3c073f2012-08-21 22:32:06 -04002572 unsigned n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002574 tty = get_current_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 if (tty) {
Peter Hurley4a510962016-01-09 21:35:23 -08002576 spin_lock(&tty->files_lock);
Eric Paris37dd0bd2008-10-31 17:40:00 -04002577 if (!list_empty(&tty->tty_files)) {
Nick Piggind996b622010-08-18 04:37:36 +10002578 struct tty_file_private *file_priv;
Eric Paris37dd0bd2008-10-31 17:40:00 -04002579
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 /* Revalidate access to controlling tty.
David Howells13f8e982013-06-13 23:37:55 +01002581 Use file_path_has_perm on the tty path directly
2582 rather than using file_has_perm, as this particular
2583 open file may belong to another process and we are
2584 only interested in the inode-based check here. */
Nick Piggind996b622010-08-18 04:37:36 +10002585 file_priv = list_first_entry(&tty->tty_files,
2586 struct tty_file_private, list);
2587 file = file_priv->file;
David Howells13f8e982013-06-13 23:37:55 +01002588 if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE))
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002589 drop_tty = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 }
Peter Hurley4a510962016-01-09 21:35:23 -08002591 spin_unlock(&tty->files_lock);
Alan Cox452a00d2008-10-13 10:39:13 +01002592 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 }
Eric W. Biederman98a27ba2007-05-08 00:26:56 -07002594 /* Reset controlling tty. */
2595 if (drop_tty)
2596 no_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597
2598 /* Revalidate access to inherited open files. */
Al Viroc3c073f2012-08-21 22:32:06 -04002599 n = iterate_fd(files, 0, match_file, cred);
2600 if (!n) /* none found? */
2601 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602
Al Viroc3c073f2012-08-21 22:32:06 -04002603 devnull = dentry_open(&selinux_null, O_RDWR, cred);
Al Viro45525b22012-10-16 13:30:07 -04002604 if (IS_ERR(devnull))
2605 devnull = NULL;
2606 /* replace all the matching ones with this */
2607 do {
2608 replace_fd(n - 1, devnull, 0);
2609 } while ((n = iterate_fd(files, n, match_file, cred)) != 0);
2610 if (devnull)
Al Viroc3c073f2012-08-21 22:32:06 -04002611 fput(devnull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612}
2613
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614/*
David Howellsa6f76f22008-11-14 10:39:24 +11002615 * Prepare a process for imminent new credential changes due to exec
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 */
David Howellsa6f76f22008-11-14 10:39:24 +11002617static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618{
David Howellsa6f76f22008-11-14 10:39:24 +11002619 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 struct rlimit *rlim, *initrlim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 int rc, i;
2622
David Howellsa6f76f22008-11-14 10:39:24 +11002623 new_tsec = bprm->cred->security;
2624 if (new_tsec->sid == new_tsec->osid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 return;
2626
2627 /* Close files for which the new task SID is not authorized. */
David Howellsa6f76f22008-11-14 10:39:24 +11002628 flush_unauthorized_files(bprm->cred, current->files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629
David Howellsa6f76f22008-11-14 10:39:24 +11002630 /* Always clear parent death signal on SID transitions. */
2631 current->pdeath_signal = 0;
2632
2633 /* Check whether the new SID can inherit resource limits from the old
2634 * SID. If not, reset all soft limits to the lower of the current
2635 * task's hard limit and the init task's soft limit.
2636 *
2637 * Note that the setting of hard limits (even to lower them) can be
2638 * controlled by the setrlimit check. The inclusion of the init task's
2639 * soft limit into the computation is to avoid resetting soft limits
2640 * higher than the default soft limit for cases where the default is
2641 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2642 */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002643 rc = avc_has_perm(&selinux_state,
2644 new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
David Howellsa6f76f22008-11-14 10:39:24 +11002645 PROCESS__RLIMITINH, NULL);
2646 if (rc) {
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002647 /* protect against do_prlimit() */
2648 task_lock(current);
David Howellsa6f76f22008-11-14 10:39:24 +11002649 for (i = 0; i < RLIM_NLIMITS; i++) {
2650 rlim = current->signal->rlim + i;
2651 initrlim = init_task.signal->rlim + i;
2652 rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
2653 }
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002654 task_unlock(current);
Nicolas Pitrebaa73d92016-11-11 00:10:10 -05002655 if (IS_ENABLED(CONFIG_POSIX_TIMERS))
2656 update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
David Howellsa6f76f22008-11-14 10:39:24 +11002657 }
2658}
2659
2660/*
2661 * Clean up the process immediately after the installation of new credentials
2662 * due to exec
2663 */
2664static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
2665{
2666 const struct task_security_struct *tsec = current_security();
2667 struct itimerval itimer;
David Howellsa6f76f22008-11-14 10:39:24 +11002668 u32 osid, sid;
2669 int rc, i;
David Howellsa6f76f22008-11-14 10:39:24 +11002670
David Howellsa6f76f22008-11-14 10:39:24 +11002671 osid = tsec->osid;
2672 sid = tsec->sid;
2673
2674 if (sid == osid)
2675 return;
2676
2677 /* Check whether the new SID can inherit signal state from the old SID.
2678 * If not, clear itimers to avoid subsequent signal generation and
2679 * flush and unblock signals.
2680 *
2681 * This must occur _after_ the task SID has been updated so that any
2682 * kill done after the flush will be checked against the new SID.
2683 */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002684 rc = avc_has_perm(&selinux_state,
2685 osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 if (rc) {
Nicolas Pitrebaa73d92016-11-11 00:10:10 -05002687 if (IS_ENABLED(CONFIG_POSIX_TIMERS)) {
2688 memset(&itimer, 0, sizeof itimer);
2689 for (i = 0; i < 3; i++)
2690 do_setitimer(i, &itimer, NULL);
2691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterov9e7c8f82015-06-04 16:22:16 -04002693 if (!fatal_signal_pending(current)) {
2694 flush_sigqueue(&current->pending);
2695 flush_sigqueue(&current->signal->shared_pending);
David Howells3bcac022009-04-29 13:45:05 +01002696 flush_signal_handlers(current, 1);
2697 sigemptyset(&current->blocked);
Oleg Nesterov9e7c8f82015-06-04 16:22:16 -04002698 recalc_sigpending();
David Howells3bcac022009-04-29 13:45:05 +01002699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 spin_unlock_irq(&current->sighand->siglock);
2701 }
2702
David Howellsa6f76f22008-11-14 10:39:24 +11002703 /* Wake up the parent if it is waiting so that it can recheck
2704 * wait permission to the new task SID. */
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002705 read_lock(&tasklist_lock);
Oleg Nesterov0b7570e2009-09-23 15:56:46 -07002706 __wake_up_parent(current, current->real_parent);
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002707 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708}
2709
2710/* superblock security operations */
2711
2712static int selinux_sb_alloc_security(struct super_block *sb)
2713{
2714 return superblock_alloc_security(sb);
2715}
2716
2717static void selinux_sb_free_security(struct super_block *sb)
2718{
2719 superblock_free_security(sb);
2720}
2721
2722static inline int match_prefix(char *prefix, int plen, char *option, int olen)
2723{
2724 if (plen > olen)
2725 return 0;
2726
2727 return !memcmp(prefix, option, plen);
2728}
2729
2730static inline int selinux_option(char *option, int len)
2731{
Eric Paris832cbd92008-04-01 13:24:09 -04002732 return (match_prefix(CONTEXT_STR, sizeof(CONTEXT_STR)-1, option, len) ||
2733 match_prefix(FSCONTEXT_STR, sizeof(FSCONTEXT_STR)-1, option, len) ||
2734 match_prefix(DEFCONTEXT_STR, sizeof(DEFCONTEXT_STR)-1, option, len) ||
David P. Quigley11689d42009-01-16 09:22:03 -05002735 match_prefix(ROOTCONTEXT_STR, sizeof(ROOTCONTEXT_STR)-1, option, len) ||
2736 match_prefix(LABELSUPP_STR, sizeof(LABELSUPP_STR)-1, option, len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737}
2738
2739static inline void take_option(char **to, char *from, int *first, int len)
2740{
2741 if (!*first) {
2742 **to = ',';
2743 *to += 1;
Cory Olmo3528a952006-09-29 01:58:44 -07002744 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 *first = 0;
2746 memcpy(*to, from, len);
2747 *to += len;
2748}
2749
Eric Paris828dfe12008-04-17 13:17:49 -04002750static inline void take_selinux_option(char **to, char *from, int *first,
2751 int len)
Cory Olmo3528a952006-09-29 01:58:44 -07002752{
2753 int current_size = 0;
2754
2755 if (!*first) {
2756 **to = '|';
2757 *to += 1;
Eric Paris828dfe12008-04-17 13:17:49 -04002758 } else
Cory Olmo3528a952006-09-29 01:58:44 -07002759 *first = 0;
2760
2761 while (current_size < len) {
2762 if (*from != '"') {
2763 **to = *from;
2764 *to += 1;
2765 }
2766 from += 1;
2767 current_size += 1;
2768 }
2769}
2770
Eric Parise0007522008-03-05 10:31:54 -05002771static int selinux_sb_copy_data(char *orig, char *copy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772{
2773 int fnosec, fsec, rc = 0;
2774 char *in_save, *in_curr, *in_end;
2775 char *sec_curr, *nosec_save, *nosec;
Cory Olmo3528a952006-09-29 01:58:44 -07002776 int open_quote = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777
2778 in_curr = orig;
2779 sec_curr = copy;
2780
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 nosec = (char *)get_zeroed_page(GFP_KERNEL);
2782 if (!nosec) {
2783 rc = -ENOMEM;
2784 goto out;
2785 }
2786
2787 nosec_save = nosec;
2788 fnosec = fsec = 1;
2789 in_save = in_end = orig;
2790
2791 do {
Cory Olmo3528a952006-09-29 01:58:44 -07002792 if (*in_end == '"')
2793 open_quote = !open_quote;
2794 if ((*in_end == ',' && open_quote == 0) ||
2795 *in_end == '\0') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 int len = in_end - in_curr;
2797
2798 if (selinux_option(in_curr, len))
Cory Olmo3528a952006-09-29 01:58:44 -07002799 take_selinux_option(&sec_curr, in_curr, &fsec, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 else
2801 take_option(&nosec, in_curr, &fnosec, len);
2802
2803 in_curr = in_end + 1;
2804 }
2805 } while (*in_end++);
2806
Eric Paris6931dfc2005-06-30 02:58:51 -07002807 strcpy(in_save, nosec_save);
Gerald Schaeferda3caa22005-06-21 17:15:18 -07002808 free_page((unsigned long)nosec_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809out:
2810 return rc;
2811}
2812
Al Viro5b400232018-12-12 20:13:29 -05002813static int selinux_sb_eat_lsm_opts(char *options, struct security_mnt_opts *opts)
2814{
2815 char *s = (char *)get_zeroed_page(GFP_KERNEL);
2816 int err;
2817
2818 if (!s)
2819 return -ENOMEM;
2820 err = selinux_sb_copy_data(options, s);
2821 if (!err)
2822 err = selinux_parse_opts_str(s, opts);
2823 free_page((unsigned long)s);
2824 return err;
2825}
2826
Al Viroc039bc32018-12-01 23:06:57 -05002827static int selinux_sb_remount(struct super_block *sb,
2828 struct security_mnt_opts *opts)
Eric Paris026eb162011-03-03 16:09:14 -05002829{
Al Viroc039bc32018-12-01 23:06:57 -05002830 int i, *flags;
2831 char **mount_options;
Eric Paris026eb162011-03-03 16:09:14 -05002832 struct superblock_security_struct *sbsec = sb->s_security;
2833
2834 if (!(sbsec->flags & SE_SBINITIALIZED))
2835 return 0;
2836
Al Viroc039bc32018-12-01 23:06:57 -05002837 mount_options = opts->mnt_opts;
2838 flags = opts->mnt_opts_flags;
Eric Paris026eb162011-03-03 16:09:14 -05002839
Al Viroc039bc32018-12-01 23:06:57 -05002840 for (i = 0; i < opts->num_mnt_opts; i++) {
Eric Paris026eb162011-03-03 16:09:14 -05002841 u32 sid;
Al Viroc039bc32018-12-01 23:06:57 -05002842 int rc;
Eric Paris026eb162011-03-03 16:09:14 -05002843
Eric Paris12f348b2012-10-09 10:56:25 -04002844 if (flags[i] == SBLABEL_MNT)
Eric Paris026eb162011-03-03 16:09:14 -05002845 continue;
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002846 rc = security_context_str_to_sid(&selinux_state,
2847 mount_options[i], &sid,
2848 GFP_KERNEL);
Eric Paris026eb162011-03-03 16:09:14 -05002849 if (rc) {
peter enderborgc103a912018-06-12 10:09:03 +02002850 pr_warn("SELinux: security_context_str_to_sid"
Linus Torvalds29b1deb2013-12-15 11:17:45 -08002851 "(%s) failed for (dev %s, type %s) errno=%d\n",
2852 mount_options[i], sb->s_id, sb->s_type->name, rc);
Al Viroc039bc32018-12-01 23:06:57 -05002853 return rc;
Eric Paris026eb162011-03-03 16:09:14 -05002854 }
Eric Paris026eb162011-03-03 16:09:14 -05002855 switch (flags[i]) {
2856 case FSCONTEXT_MNT:
2857 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
2858 goto out_bad_option;
2859 break;
2860 case CONTEXT_MNT:
2861 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
2862 goto out_bad_option;
2863 break;
2864 case ROOTCONTEXT_MNT: {
2865 struct inode_security_struct *root_isec;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002866 root_isec = backing_inode_security(sb->s_root);
Eric Paris026eb162011-03-03 16:09:14 -05002867
2868 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
2869 goto out_bad_option;
2870 break;
2871 }
2872 case DEFCONTEXT_MNT:
2873 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
2874 goto out_bad_option;
2875 break;
2876 default:
Al Viroc039bc32018-12-01 23:06:57 -05002877 return -EINVAL;
Eric Paris026eb162011-03-03 16:09:14 -05002878 }
2879 }
Al Viroc039bc32018-12-01 23:06:57 -05002880 return 0;
Eric Paris026eb162011-03-03 16:09:14 -05002881
Eric Paris026eb162011-03-03 16:09:14 -05002882out_bad_option:
peter enderborgc103a912018-06-12 10:09:03 +02002883 pr_warn("SELinux: unable to change security options "
Linus Torvalds29b1deb2013-12-15 11:17:45 -08002884 "during remount (dev %s, type=%s)\n", sb->s_id,
2885 sb->s_type->name);
Al Viroc039bc32018-12-01 23:06:57 -05002886 return -EINVAL;
Eric Paris026eb162011-03-03 16:09:14 -05002887}
2888
Al Viroa10d7c22018-12-05 11:58:35 -05002889static int selinux_sb_kern_mount(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890{
David Howells88e67f32008-11-14 10:39:21 +11002891 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002892 struct common_audit_data ad;
James Morris74192242008-12-19 11:41:10 +11002893
Eric Paris50c205f2012-04-04 15:01:43 -04002894 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002895 ad.u.dentry = sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002896 return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897}
2898
David Howells726c3342006-06-23 02:02:58 -07002899static int selinux_sb_statfs(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900{
David Howells88e67f32008-11-14 10:39:21 +11002901 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002902 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903
Eric Paris50c205f2012-04-04 15:01:43 -04002904 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002905 ad.u.dentry = dentry->d_sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002906 return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907}
2908
Al Viro808d4e32012-10-11 11:42:01 -04002909static int selinux_mount(const char *dev_name,
Al Viro8a04c432016-03-25 14:52:53 -04002910 const struct path *path,
Al Viro808d4e32012-10-11 11:42:01 -04002911 const char *type,
Eric Paris828dfe12008-04-17 13:17:49 -04002912 unsigned long flags,
2913 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914{
David Howells88e67f32008-11-14 10:39:21 +11002915 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916
2917 if (flags & MS_REMOUNT)
Al Virod8c95842011-12-07 18:16:57 -05002918 return superblock_has_perm(cred, path->dentry->d_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002919 FILESYSTEM__REMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 else
Eric Paris2875fa02011-04-28 16:04:24 -04002921 return path_has_perm(cred, path, FILE__MOUNTON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922}
2923
2924static int selinux_umount(struct vfsmount *mnt, int flags)
2925{
David Howells88e67f32008-11-14 10:39:21 +11002926 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927
David Howells88e67f32008-11-14 10:39:21 +11002928 return superblock_has_perm(cred, mnt->mnt_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002929 FILESYSTEM__UNMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930}
2931
2932/* inode security operations */
2933
2934static int selinux_inode_alloc_security(struct inode *inode)
2935{
2936 return inode_alloc_security(inode);
2937}
2938
2939static void selinux_inode_free_security(struct inode *inode)
2940{
2941 inode_free_security(inode);
2942}
2943
David Quigleyd47be3d2013-05-22 12:50:34 -04002944static int selinux_dentry_init_security(struct dentry *dentry, int mode,
Al Viro4f3ccd72016-07-20 16:06:15 -04002945 const struct qstr *name, void **ctx,
David Quigleyd47be3d2013-05-22 12:50:34 -04002946 u32 *ctxlen)
2947{
David Quigleyd47be3d2013-05-22 12:50:34 -04002948 u32 newsid;
2949 int rc;
2950
Vivek Goyalc957f6d2016-07-13 10:44:51 -04002951 rc = selinux_determine_inode_label(current_security(),
2952 d_inode(dentry->d_parent), name,
David Howellsc3c188b2015-07-10 17:19:58 -04002953 inode_mode_to_security_class(mode),
2954 &newsid);
2955 if (rc)
2956 return rc;
David Quigleyd47be3d2013-05-22 12:50:34 -04002957
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002958 return security_sid_to_context(&selinux_state, newsid, (char **)ctx,
2959 ctxlen);
David Quigleyd47be3d2013-05-22 12:50:34 -04002960}
2961
Vivek Goyala518b0a2016-07-13 10:44:53 -04002962static int selinux_dentry_create_files_as(struct dentry *dentry, int mode,
2963 struct qstr *name,
2964 const struct cred *old,
2965 struct cred *new)
2966{
2967 u32 newsid;
2968 int rc;
2969 struct task_security_struct *tsec;
2970
2971 rc = selinux_determine_inode_label(old->security,
2972 d_inode(dentry->d_parent), name,
2973 inode_mode_to_security_class(mode),
2974 &newsid);
2975 if (rc)
2976 return rc;
2977
2978 tsec = new->security;
2979 tsec->create_sid = newsid;
2980 return 0;
2981}
2982
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002983static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +09002984 const struct qstr *qstr,
2985 const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -05002986 void **value, size_t *len)
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002987{
Paul Moore5fb49872010-04-22 14:46:19 -04002988 const struct task_security_struct *tsec = current_security();
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002989 struct superblock_security_struct *sbsec;
Corentin LABBEc0d4f462017-10-04 20:32:17 +02002990 u32 newsid, clen;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002991 int rc;
Tetsuo Handa95489062013-07-25 05:44:02 +09002992 char *context;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002993
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002994 sbsec = dir->i_sb->s_security;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002995
David Howells275bb412008-11-14 10:39:19 +11002996 newsid = tsec->create_sid;
2997
Vivek Goyalc957f6d2016-07-13 10:44:51 -04002998 rc = selinux_determine_inode_label(current_security(),
David Howellsc3c188b2015-07-10 17:19:58 -04002999 dir, qstr,
3000 inode_mode_to_security_class(inode->i_mode),
3001 &newsid);
3002 if (rc)
3003 return rc;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07003004
Eric Paris296fddf2006-09-25 23:32:00 -07003005 /* Possibly defer initialization to selinux_complete_init. */
David P. Quigley0d90a7e2009-01-16 09:22:02 -05003006 if (sbsec->flags & SE_SBINITIALIZED) {
Eric Paris296fddf2006-09-25 23:32:00 -07003007 struct inode_security_struct *isec = inode->i_security;
3008 isec->sclass = inode_mode_to_security_class(inode->i_mode);
3009 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05003010 isec->initialized = LABEL_INITIALIZED;
Eric Paris296fddf2006-09-25 23:32:00 -07003011 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07003012
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003013 if (!selinux_state.initialized || !(sbsec->flags & SBLABEL_MNT))
Stephen Smalley25a74f32005-11-08 21:34:33 -08003014 return -EOPNOTSUPP;
3015
Tetsuo Handa95489062013-07-25 05:44:02 +09003016 if (name)
3017 *name = XATTR_SELINUX_SUFFIX;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07003018
3019 if (value && len) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003020 rc = security_sid_to_context_force(&selinux_state, newsid,
3021 &context, &clen);
Tetsuo Handa95489062013-07-25 05:44:02 +09003022 if (rc)
Stephen Smalley570bc1c2005-09-09 13:01:43 -07003023 return rc;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07003024 *value = context;
3025 *len = clen;
3026 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07003027
Stephen Smalley5e41ff92005-09-09 13:01:35 -07003028 return 0;
3029}
3030
Al Viro4acdaf22011-07-26 01:42:34 -04003031static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032{
3033 return may_create(dir, dentry, SECCLASS_FILE);
3034}
3035
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
3037{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 return may_link(dir, old_dentry, MAY_LINK);
3039}
3040
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
3042{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 return may_link(dir, dentry, MAY_UNLINK);
3044}
3045
3046static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
3047{
3048 return may_create(dir, dentry, SECCLASS_LNK_FILE);
3049}
3050
Al Viro18bb1db2011-07-26 01:41:39 -04003051static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052{
3053 return may_create(dir, dentry, SECCLASS_DIR);
3054}
3055
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
3057{
3058 return may_link(dir, dentry, MAY_RMDIR);
3059}
3060
Al Viro1a67aaf2011-07-26 01:52:52 -04003061static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 return may_create(dir, dentry, inode_mode_to_security_class(mode));
3064}
3065
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
Eric Paris828dfe12008-04-17 13:17:49 -04003067 struct inode *new_inode, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068{
3069 return may_rename(old_inode, old_dentry, new_inode, new_dentry);
3070}
3071
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072static int selinux_inode_readlink(struct dentry *dentry)
3073{
David Howells88e67f32008-11-14 10:39:21 +11003074 const struct cred *cred = current_cred();
3075
Eric Paris2875fa02011-04-28 16:04:24 -04003076 return dentry_has_perm(cred, dentry, FILE__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077}
3078
NeilBrownbda0be72015-03-23 13:37:39 +11003079static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode,
3080 bool rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081{
David Howells88e67f32008-11-14 10:39:21 +11003082 const struct cred *cred = current_cred();
NeilBrownbda0be72015-03-23 13:37:39 +11003083 struct common_audit_data ad;
3084 struct inode_security_struct *isec;
3085 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086
NeilBrownbda0be72015-03-23 13:37:39 +11003087 validate_creds(cred);
3088
3089 ad.type = LSM_AUDIT_DATA_DENTRY;
3090 ad.u.dentry = dentry;
3091 sid = cred_sid(cred);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05003092 isec = inode_security_rcu(inode, rcu);
3093 if (IS_ERR(isec))
3094 return PTR_ERR(isec);
NeilBrownbda0be72015-03-23 13:37:39 +11003095
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003096 return avc_has_perm_flags(&selinux_state,
3097 sid, isec->sid, isec->sclass, FILE__READ, &ad,
NeilBrownbda0be72015-03-23 13:37:39 +11003098 rcu ? MAY_NOT_BLOCK : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099}
3100
Eric Parisd4cf970d2012-04-04 15:01:42 -04003101static noinline int audit_inode_permission(struct inode *inode,
3102 u32 perms, u32 audited, u32 denied,
Stephen Smalley626b9742014-04-29 11:29:04 -07003103 int result,
Eric Parisd4cf970d2012-04-04 15:01:42 -04003104 unsigned flags)
3105{
3106 struct common_audit_data ad;
Eric Parisd4cf970d2012-04-04 15:01:42 -04003107 struct inode_security_struct *isec = inode->i_security;
3108 int rc;
3109
Eric Paris50c205f2012-04-04 15:01:43 -04003110 ad.type = LSM_AUDIT_DATA_INODE;
Eric Parisd4cf970d2012-04-04 15:01:42 -04003111 ad.u.inode = inode;
3112
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003113 rc = slow_avc_audit(&selinux_state,
3114 current_sid(), isec->sid, isec->sclass, perms,
Stephen Smalley626b9742014-04-29 11:29:04 -07003115 audited, denied, result, &ad, flags);
Eric Parisd4cf970d2012-04-04 15:01:42 -04003116 if (rc)
3117 return rc;
3118 return 0;
3119}
3120
Al Viroe74f71e2011-06-20 19:38:15 -04003121static int selinux_inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122{
David Howells88e67f32008-11-14 10:39:21 +11003123 const struct cred *cred = current_cred();
Eric Parisb782e0a2010-07-23 11:44:03 -04003124 u32 perms;
3125 bool from_access;
Al Virocf1dd1d2011-06-20 19:44:08 -04003126 unsigned flags = mask & MAY_NOT_BLOCK;
Eric Paris2e334052012-04-04 15:01:42 -04003127 struct inode_security_struct *isec;
3128 u32 sid;
3129 struct av_decision avd;
3130 int rc, rc2;
3131 u32 audited, denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132
Eric Parisb782e0a2010-07-23 11:44:03 -04003133 from_access = mask & MAY_ACCESS;
Eric Parisd09ca732010-07-23 11:43:57 -04003134 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
3135
Eric Parisb782e0a2010-07-23 11:44:03 -04003136 /* No permission to check. Existence test. */
3137 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139
Eric Paris2e334052012-04-04 15:01:42 -04003140 validate_creds(cred);
Eric Parisb782e0a2010-07-23 11:44:03 -04003141
Eric Paris2e334052012-04-04 15:01:42 -04003142 if (unlikely(IS_PRIVATE(inode)))
3143 return 0;
Eric Parisb782e0a2010-07-23 11:44:03 -04003144
3145 perms = file_mask_to_av(inode->i_mode, mask);
3146
Eric Paris2e334052012-04-04 15:01:42 -04003147 sid = cred_sid(cred);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05003148 isec = inode_security_rcu(inode, flags & MAY_NOT_BLOCK);
3149 if (IS_ERR(isec))
3150 return PTR_ERR(isec);
Eric Paris2e334052012-04-04 15:01:42 -04003151
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003152 rc = avc_has_perm_noaudit(&selinux_state,
3153 sid, isec->sid, isec->sclass, perms, 0, &avd);
Eric Paris2e334052012-04-04 15:01:42 -04003154 audited = avc_audit_required(perms, &avd, rc,
3155 from_access ? FILE__AUDIT_ACCESS : 0,
3156 &denied);
3157 if (likely(!audited))
3158 return rc;
3159
Stephen Smalley626b9742014-04-29 11:29:04 -07003160 rc2 = audit_inode_permission(inode, perms, audited, denied, rc, flags);
Eric Paris2e334052012-04-04 15:01:42 -04003161 if (rc2)
3162 return rc2;
3163 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164}
3165
3166static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
3167{
David Howells88e67f32008-11-14 10:39:21 +11003168 const struct cred *cred = current_cred();
Stephen Smalleyccb54472017-05-12 12:41:24 -04003169 struct inode *inode = d_backing_inode(dentry);
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003170 unsigned int ia_valid = iattr->ia_valid;
Eric Paris95dbf732012-04-04 13:45:34 -04003171 __u32 av = FILE__WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003173 /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
3174 if (ia_valid & ATTR_FORCE) {
3175 ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
3176 ATTR_FORCE);
3177 if (!ia_valid)
3178 return 0;
3179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003181 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
3182 ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
Eric Paris2875fa02011-04-28 16:04:24 -04003183 return dentry_has_perm(cred, dentry, FILE__SETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003185 if (selinux_policycap_openperm() &&
Stephen Smalleyccb54472017-05-12 12:41:24 -04003186 inode->i_sb->s_magic != SOCKFS_MAGIC &&
3187 (ia_valid & ATTR_SIZE) &&
3188 !(ia_valid & ATTR_FILE))
Eric Paris95dbf732012-04-04 13:45:34 -04003189 av |= FILE__OPEN;
3190
3191 return dentry_has_perm(cred, dentry, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192}
3193
Al Viro3f7036a2015-03-08 19:28:30 -04003194static int selinux_inode_getattr(const struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195{
Al Viro3f7036a2015-03-08 19:28:30 -04003196 return path_has_perm(current_cred(), path, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197}
3198
Stephen Smalleydb590002017-04-20 11:31:30 -04003199static bool has_cap_mac_admin(bool audit)
3200{
3201 const struct cred *cred = current_cred();
3202 int cap_audit = audit ? SECURITY_CAP_AUDIT : SECURITY_CAP_NOAUDIT;
3203
3204 if (cap_capable(cred, &init_user_ns, CAP_MAC_ADMIN, cap_audit))
3205 return false;
3206 if (cred_has_capability(cred, CAP_MAC_ADMIN, cap_audit, true))
3207 return false;
3208 return true;
3209}
3210
David Howells8f0cfa52008-04-29 00:59:41 -07003211static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
3212 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213{
David Howellsc6f493d2015-03-17 22:26:22 +00003214 struct inode *inode = d_backing_inode(dentry);
Paul Moore20cdef82016-04-04 14:14:42 -04003215 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 struct superblock_security_struct *sbsec;
Thomas Liu2bf49692009-07-14 12:14:09 -04003217 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11003218 u32 newsid, sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 int rc = 0;
3220
Eric W. Biederman6b240302017-10-02 09:38:20 -05003221 if (strcmp(name, XATTR_NAME_SELINUX)) {
3222 rc = cap_inode_setxattr(dentry, name, value, size, flags);
3223 if (rc)
3224 return rc;
3225
3226 /* Not an attribute we recognize, so just check the
3227 ordinary setattr permission. */
3228 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230
3231 sbsec = inode->i_sb->s_security;
Eric Paris12f348b2012-10-09 10:56:25 -04003232 if (!(sbsec->flags & SBLABEL_MNT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 return -EOPNOTSUPP;
3234
Serge E. Hallyn2e149672011-03-23 16:43:26 -07003235 if (!inode_owner_or_capable(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 return -EPERM;
3237
Eric Paris50c205f2012-04-04 15:01:43 -04003238 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04003239 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240
Paul Moore20cdef82016-04-04 14:14:42 -04003241 isec = backing_inode_security(dentry);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003242 rc = avc_has_perm(&selinux_state,
3243 sid, isec->sid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 FILE__RELABELFROM, &ad);
3245 if (rc)
3246 return rc;
3247
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003248 rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3249 GFP_KERNEL);
Stephen Smalley12b29f32008-05-07 13:03:20 -04003250 if (rc == -EINVAL) {
Stephen Smalleydb590002017-04-20 11:31:30 -04003251 if (!has_cap_mac_admin(true)) {
Eric Parisd6ea83e2012-04-04 13:45:49 -04003252 struct audit_buffer *ab;
3253 size_t audit_size;
Eric Parisd6ea83e2012-04-04 13:45:49 -04003254
3255 /* We strip a nul only if it is at the end, otherwise the
3256 * context contains a nul and we should audit that */
Al Viroe3fea3f2012-06-09 08:15:16 +01003257 if (value) {
Colin Ian Kingadd24372017-10-14 13:46:55 +01003258 const char *str = value;
3259
Al Viroe3fea3f2012-06-09 08:15:16 +01003260 if (str[size - 1] == '\0')
3261 audit_size = size - 1;
3262 else
3263 audit_size = size;
3264 } else {
Al Viroe3fea3f2012-06-09 08:15:16 +01003265 audit_size = 0;
3266 }
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04003267 ab = audit_log_start(audit_context(),
3268 GFP_ATOMIC, AUDIT_SELINUX_ERR);
Eric Parisd6ea83e2012-04-04 13:45:49 -04003269 audit_log_format(ab, "op=setxattr invalid_context=");
3270 audit_log_n_untrustedstring(ab, value, audit_size);
3271 audit_log_end(ab);
3272
Stephen Smalley12b29f32008-05-07 13:03:20 -04003273 return rc;
Eric Parisd6ea83e2012-04-04 13:45:49 -04003274 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003275 rc = security_context_to_sid_force(&selinux_state, value,
3276 size, &newsid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04003277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 if (rc)
3279 return rc;
3280
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003281 rc = avc_has_perm(&selinux_state,
3282 sid, newsid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 FILE__RELABELTO, &ad);
3284 if (rc)
3285 return rc;
3286
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003287 rc = security_validate_transition(&selinux_state, isec->sid, newsid,
3288 sid, isec->sclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 if (rc)
3290 return rc;
3291
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003292 return avc_has_perm(&selinux_state,
3293 newsid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 sbsec->sid,
3295 SECCLASS_FILESYSTEM,
3296 FILESYSTEM__ASSOCIATE,
3297 &ad);
3298}
3299
David Howells8f0cfa52008-04-29 00:59:41 -07003300static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
Eric Parisf5269712008-05-14 11:27:45 -04003301 const void *value, size_t size,
David Howells8f0cfa52008-04-29 00:59:41 -07003302 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303{
David Howellsc6f493d2015-03-17 22:26:22 +00003304 struct inode *inode = d_backing_inode(dentry);
Paul Moore20cdef82016-04-04 14:14:42 -04003305 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 u32 newsid;
3307 int rc;
3308
3309 if (strcmp(name, XATTR_NAME_SELINUX)) {
3310 /* Not an attribute we recognize, so nothing to do. */
3311 return;
3312 }
3313
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003314 rc = security_context_to_sid_force(&selinux_state, value, size,
3315 &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 if (rc) {
peter enderborgc103a912018-06-12 10:09:03 +02003317 pr_err("SELinux: unable to map context to SID"
Stephen Smalley12b29f32008-05-07 13:03:20 -04003318 "for (%s, %lu), rc=%d\n",
3319 inode->i_sb->s_id, inode->i_ino, -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 return;
3321 }
3322
Paul Moore20cdef82016-04-04 14:14:42 -04003323 isec = backing_inode_security(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003324 spin_lock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003325 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05003327 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003328 spin_unlock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003329
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 return;
3331}
3332
David Howells8f0cfa52008-04-29 00:59:41 -07003333static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334{
David Howells88e67f32008-11-14 10:39:21 +11003335 const struct cred *cred = current_cred();
3336
Eric Paris2875fa02011-04-28 16:04:24 -04003337 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338}
3339
Eric Paris828dfe12008-04-17 13:17:49 -04003340static int selinux_inode_listxattr(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341{
David Howells88e67f32008-11-14 10:39:21 +11003342 const struct cred *cred = current_cred();
3343
Eric Paris2875fa02011-04-28 16:04:24 -04003344 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345}
3346
David Howells8f0cfa52008-04-29 00:59:41 -07003347static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348{
Eric W. Biederman6b240302017-10-02 09:38:20 -05003349 if (strcmp(name, XATTR_NAME_SELINUX)) {
3350 int rc = cap_inode_removexattr(dentry, name);
3351 if (rc)
3352 return rc;
3353
3354 /* Not an attribute we recognize, so just check the
3355 ordinary setattr permission. */
3356 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358
3359 /* No one is allowed to remove a SELinux security label.
3360 You can change the label, but all data must be labeled. */
3361 return -EACCES;
3362}
3363
James Morrisd381d8a2005-10-30 14:59:22 -08003364/*
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003365 * Copy the inode security context value to the user.
James Morrisd381d8a2005-10-30 14:59:22 -08003366 *
3367 * Permission check is handled by selinux_inode_getxattr hook.
3368 */
Andreas Gruenbacherea861df2015-12-24 11:09:39 -05003369static int selinux_inode_getsecurity(struct inode *inode, const char *name, void **buffer, bool alloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370{
David P. Quigley42492592008-02-04 22:29:39 -08003371 u32 size;
3372 int error;
3373 char *context = NULL;
Paul Moore20cdef82016-04-04 14:14:42 -04003374 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00003376 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3377 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003379 /*
3380 * If the caller has CAP_MAC_ADMIN, then get the raw context
3381 * value even if it is not defined by current policy; otherwise,
3382 * use the in-core value under current policy.
3383 * Use the non-auditing forms of the permission checks since
3384 * getxattr may be called by unprivileged processes commonly
3385 * and lack of permission just means that we fall back to the
3386 * in-core context value, not a denial.
3387 */
Paul Moore20cdef82016-04-04 14:14:42 -04003388 isec = inode_security(inode);
Stephen Smalleydb590002017-04-20 11:31:30 -04003389 if (has_cap_mac_admin(false))
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003390 error = security_sid_to_context_force(&selinux_state,
3391 isec->sid, &context,
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003392 &size);
3393 else
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003394 error = security_sid_to_context(&selinux_state, isec->sid,
3395 &context, &size);
David P. Quigley42492592008-02-04 22:29:39 -08003396 if (error)
3397 return error;
3398 error = size;
3399 if (alloc) {
3400 *buffer = context;
3401 goto out_nofree;
3402 }
3403 kfree(context);
3404out_nofree:
3405 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406}
3407
3408static int selinux_inode_setsecurity(struct inode *inode, const char *name,
Eric Paris828dfe12008-04-17 13:17:49 -04003409 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410{
Paul Moore2c971652016-04-19 16:36:28 -04003411 struct inode_security_struct *isec = inode_security_novalidate(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 u32 newsid;
3413 int rc;
3414
3415 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3416 return -EOPNOTSUPP;
3417
3418 if (!value || !size)
3419 return -EACCES;
3420
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003421 rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3422 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 if (rc)
3424 return rc;
3425
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003426 spin_lock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003427 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05003429 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003430 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 return 0;
3432}
3433
3434static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
3435{
3436 const int len = sizeof(XATTR_NAME_SELINUX);
3437 if (buffer && len <= buffer_size)
3438 memcpy(buffer, XATTR_NAME_SELINUX, len);
3439 return len;
3440}
3441
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05003442static void selinux_inode_getsecid(struct inode *inode, u32 *secid)
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02003443{
Andreas Gruenbachere817c2f2016-02-18 12:04:08 +01003444 struct inode_security_struct *isec = inode_security_novalidate(inode);
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02003445 *secid = isec->sid;
3446}
3447
Vivek Goyal56909eb2016-07-13 10:44:48 -04003448static int selinux_inode_copy_up(struct dentry *src, struct cred **new)
3449{
3450 u32 sid;
3451 struct task_security_struct *tsec;
3452 struct cred *new_creds = *new;
3453
3454 if (new_creds == NULL) {
3455 new_creds = prepare_creds();
3456 if (!new_creds)
3457 return -ENOMEM;
3458 }
3459
3460 tsec = new_creds->security;
3461 /* Get label from overlay inode and set it in create_sid */
3462 selinux_inode_getsecid(d_inode(src), &sid);
3463 tsec->create_sid = sid;
3464 *new = new_creds;
3465 return 0;
3466}
3467
Vivek Goyal19472b62016-07-13 10:44:50 -04003468static int selinux_inode_copy_up_xattr(const char *name)
3469{
3470 /* The copy_up hook above sets the initial context on an inode, but we
3471 * don't then want to overwrite it by blindly copying all the lower
3472 * xattrs up. Instead, we have to filter out SELinux-related xattrs.
3473 */
3474 if (strcmp(name, XATTR_NAME_SELINUX) == 0)
3475 return 1; /* Discard */
3476 /*
3477 * Any other attribute apart from SELINUX is not claimed, supported
3478 * by selinux.
3479 */
3480 return -EOPNOTSUPP;
3481}
3482
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483/* file security operations */
3484
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003485static int selinux_revalidate_file_permission(struct file *file, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486{
David Howells88e67f32008-11-14 10:39:21 +11003487 const struct cred *cred = current_cred();
Al Viro496ad9a2013-01-23 17:07:38 -05003488 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
3491 if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
3492 mask |= MAY_APPEND;
3493
Paul Moore389fb8002009-03-27 17:10:34 -04003494 return file_has_perm(cred, file,
3495 file_mask_to_av(inode->i_mode, mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496}
3497
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003498static int selinux_file_permission(struct file *file, int mask)
3499{
Al Viro496ad9a2013-01-23 17:07:38 -05003500 struct inode *inode = file_inode(file);
Stephen Smalley20dda182009-06-22 14:54:53 -04003501 struct file_security_struct *fsec = file->f_security;
Andreas Gruenbacherb1973672016-01-05 23:12:33 +01003502 struct inode_security_struct *isec;
Stephen Smalley20dda182009-06-22 14:54:53 -04003503 u32 sid = current_sid();
3504
Paul Moore389fb8002009-03-27 17:10:34 -04003505 if (!mask)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003506 /* No permission to check. Existence test. */
3507 return 0;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003508
Andreas Gruenbacherb1973672016-01-05 23:12:33 +01003509 isec = inode_security(inode);
Stephen Smalley20dda182009-06-22 14:54:53 -04003510 if (sid == fsec->sid && fsec->isid == isec->sid &&
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003511 fsec->pseqno == avc_policy_seqno(&selinux_state))
Eric Paris83d49852012-04-04 13:45:40 -04003512 /* No change since file_open check. */
Stephen Smalley20dda182009-06-22 14:54:53 -04003513 return 0;
3514
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003515 return selinux_revalidate_file_permission(file, mask);
3516}
3517
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518static int selinux_file_alloc_security(struct file *file)
3519{
3520 return file_alloc_security(file);
3521}
3522
3523static void selinux_file_free_security(struct file *file)
3524{
3525 file_free_security(file);
3526}
3527
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003528/*
3529 * Check whether a task has the ioctl permission and cmd
3530 * operation to an inode.
3531 */
Geliang Tang1d2a1682015-10-21 17:44:27 -04003532static int ioctl_has_perm(const struct cred *cred, struct file *file,
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003533 u32 requested, u16 cmd)
3534{
3535 struct common_audit_data ad;
3536 struct file_security_struct *fsec = file->f_security;
3537 struct inode *inode = file_inode(file);
Paul Moore20cdef82016-04-04 14:14:42 -04003538 struct inode_security_struct *isec;
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003539 struct lsm_ioctlop_audit ioctl;
3540 u32 ssid = cred_sid(cred);
3541 int rc;
3542 u8 driver = cmd >> 8;
3543 u8 xperm = cmd & 0xff;
3544
3545 ad.type = LSM_AUDIT_DATA_IOCTL_OP;
3546 ad.u.op = &ioctl;
3547 ad.u.op->cmd = cmd;
3548 ad.u.op->path = file->f_path;
3549
3550 if (ssid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003551 rc = avc_has_perm(&selinux_state,
3552 ssid, fsec->sid,
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003553 SECCLASS_FD,
3554 FD__USE,
3555 &ad);
3556 if (rc)
3557 goto out;
3558 }
3559
3560 if (unlikely(IS_PRIVATE(inode)))
3561 return 0;
3562
Paul Moore20cdef82016-04-04 14:14:42 -04003563 isec = inode_security(inode);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003564 rc = avc_has_extended_perms(&selinux_state,
3565 ssid, isec->sid, isec->sclass,
3566 requested, driver, xperm, &ad);
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003567out:
3568 return rc;
3569}
3570
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571static int selinux_file_ioctl(struct file *file, unsigned int cmd,
3572 unsigned long arg)
3573{
David Howells88e67f32008-11-14 10:39:21 +11003574 const struct cred *cred = current_cred();
Eric Paris0b24dcb2011-02-25 15:39:20 -05003575 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576
Eric Paris0b24dcb2011-02-25 15:39:20 -05003577 switch (cmd) {
3578 case FIONREAD:
3579 /* fall through */
3580 case FIBMAP:
3581 /* fall through */
3582 case FIGETBSZ:
3583 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003584 case FS_IOC_GETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003585 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003586 case FS_IOC_GETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003587 error = file_has_perm(cred, file, FILE__GETATTR);
3588 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589
Al Viro2f99c362012-03-23 16:04:05 -04003590 case FS_IOC_SETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003591 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003592 case FS_IOC_SETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003593 error = file_has_perm(cred, file, FILE__SETATTR);
3594 break;
3595
3596 /* sys_ioctl() checks */
3597 case FIONBIO:
3598 /* fall through */
3599 case FIOASYNC:
3600 error = file_has_perm(cred, file, 0);
3601 break;
3602
3603 case KDSKBENT:
3604 case KDSKBSENT:
Eric Paris6a9de492012-01-03 12:25:14 -05003605 error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04003606 SECURITY_CAP_AUDIT, true);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003607 break;
3608
3609 /* default case assumes that the command will go
3610 * to the file's ioctl() function.
3611 */
3612 default:
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003613 error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003614 }
3615 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616}
3617
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003618static int default_noexec;
3619
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
3621{
David Howells88e67f32008-11-14 10:39:21 +11003622 const struct cred *cred = current_cred();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003623 u32 sid = cred_sid(cred);
David Howellsd84f4f92008-11-14 10:39:23 +11003624 int rc = 0;
David Howells88e67f32008-11-14 10:39:21 +11003625
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003626 if (default_noexec &&
Stephen Smalley892e8ca2015-07-10 09:40:59 -04003627 (prot & PROT_EXEC) && (!file || IS_PRIVATE(file_inode(file)) ||
3628 (!shared && (prot & PROT_WRITE)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 /*
3630 * We are making executable an anonymous mapping or a
3631 * private file mapping that will also be writable.
3632 * This has an additional check.
3633 */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003634 rc = avc_has_perm(&selinux_state,
3635 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003636 PROCESS__EXECMEM, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 if (rc)
David Howellsd84f4f92008-11-14 10:39:23 +11003638 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640
3641 if (file) {
3642 /* read access is always possible with a mapping */
3643 u32 av = FILE__READ;
3644
3645 /* write access only matters if the mapping is shared */
3646 if (shared && (prot & PROT_WRITE))
3647 av |= FILE__WRITE;
3648
3649 if (prot & PROT_EXEC)
3650 av |= FILE__EXECUTE;
3651
David Howells88e67f32008-11-14 10:39:21 +11003652 return file_has_perm(cred, file, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 }
David Howellsd84f4f92008-11-14 10:39:23 +11003654
3655error:
3656 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657}
3658
Al Viroe5467852012-05-30 13:30:51 -04003659static int selinux_mmap_addr(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07003661 int rc = 0;
Paul Moore98883bf2014-03-19 16:46:11 -04003662
3663 if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
3664 u32 sid = current_sid();
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003665 rc = avc_has_perm(&selinux_state,
3666 sid, sid, SECCLASS_MEMPROTECT,
Paul Moore98883bf2014-03-19 16:46:11 -04003667 MEMPROTECT__MMAP_ZERO, NULL);
3668 }
3669
3670 return rc;
Al Viroe5467852012-05-30 13:30:51 -04003671}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672
Al Viroe5467852012-05-30 13:30:51 -04003673static int selinux_mmap_file(struct file *file, unsigned long reqprot,
3674 unsigned long prot, unsigned long flags)
3675{
Stephen Smalley3ba4bf52017-05-05 09:14:48 -04003676 struct common_audit_data ad;
3677 int rc;
3678
3679 if (file) {
3680 ad.type = LSM_AUDIT_DATA_FILE;
3681 ad.u.file = file;
3682 rc = inode_has_perm(current_cred(), file_inode(file),
3683 FILE__MAP, &ad);
3684 if (rc)
3685 return rc;
3686 }
3687
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003688 if (selinux_state.checkreqprot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 prot = reqprot;
3690
3691 return file_map_prot_check(file, prot,
3692 (flags & MAP_TYPE) == MAP_SHARED);
3693}
3694
3695static int selinux_file_mprotect(struct vm_area_struct *vma,
3696 unsigned long reqprot,
3697 unsigned long prot)
3698{
David Howells88e67f32008-11-14 10:39:21 +11003699 const struct cred *cred = current_cred();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003700 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003702 if (selinux_state.checkreqprot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 prot = reqprot;
3704
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003705 if (default_noexec &&
3706 (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
James Morrisd541bbe2009-01-29 12:19:51 +11003707 int rc = 0;
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003708 if (vma->vm_start >= vma->vm_mm->start_brk &&
3709 vma->vm_end <= vma->vm_mm->brk) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003710 rc = avc_has_perm(&selinux_state,
3711 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003712 PROCESS__EXECHEAP, NULL);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003713 } else if (!vma->vm_file &&
Stephen Smalleyc2316db2016-04-08 13:55:03 -04003714 ((vma->vm_start <= vma->vm_mm->start_stack &&
3715 vma->vm_end >= vma->vm_mm->start_stack) ||
Andy Lutomirskid17af502016-09-30 10:58:58 -07003716 vma_is_stack_for_current(vma))) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003717 rc = avc_has_perm(&selinux_state,
3718 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003719 PROCESS__EXECSTACK, NULL);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003720 } else if (vma->vm_file && vma->anon_vma) {
3721 /*
3722 * We are making executable a file mapping that has
3723 * had some COW done. Since pages might have been
3724 * written, check ability to execute the possibly
3725 * modified content. This typically should only
3726 * occur for text relocations.
3727 */
David Howellsd84f4f92008-11-14 10:39:23 +11003728 rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003729 }
Lorenzo Hernandez García-Hierro6b992192005-06-25 14:54:34 -07003730 if (rc)
3731 return rc;
3732 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733
3734 return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
3735}
3736
3737static int selinux_file_lock(struct file *file, unsigned int cmd)
3738{
David Howells88e67f32008-11-14 10:39:21 +11003739 const struct cred *cred = current_cred();
3740
3741 return file_has_perm(cred, file, FILE__LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742}
3743
3744static int selinux_file_fcntl(struct file *file, unsigned int cmd,
3745 unsigned long arg)
3746{
David Howells88e67f32008-11-14 10:39:21 +11003747 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 int err = 0;
3749
3750 switch (cmd) {
Eric Paris828dfe12008-04-17 13:17:49 -04003751 case F_SETFL:
Eric Paris828dfe12008-04-17 13:17:49 -04003752 if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
David Howells88e67f32008-11-14 10:39:21 +11003753 err = file_has_perm(cred, file, FILE__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003755 }
3756 /* fall through */
3757 case F_SETOWN:
3758 case F_SETSIG:
3759 case F_GETFL:
3760 case F_GETOWN:
3761 case F_GETSIG:
Cyrill Gorcunov1d151c32012-07-30 14:43:00 -07003762 case F_GETOWNER_UIDS:
Eric Paris828dfe12008-04-17 13:17:49 -04003763 /* Just check FD__USE permission */
David Howells88e67f32008-11-14 10:39:21 +11003764 err = file_has_perm(cred, file, 0);
Eric Paris828dfe12008-04-17 13:17:49 -04003765 break;
3766 case F_GETLK:
3767 case F_SETLK:
3768 case F_SETLKW:
Jeff Layton0d3f7a22014-04-22 08:23:58 -04003769 case F_OFD_GETLK:
3770 case F_OFD_SETLK:
3771 case F_OFD_SETLKW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772#if BITS_PER_LONG == 32
Eric Paris828dfe12008-04-17 13:17:49 -04003773 case F_GETLK64:
3774 case F_SETLK64:
3775 case F_SETLKW64:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776#endif
David Howells88e67f32008-11-14 10:39:21 +11003777 err = file_has_perm(cred, file, FILE__LOCK);
Eric Paris828dfe12008-04-17 13:17:49 -04003778 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 }
3780
3781 return err;
3782}
3783
Jeff Laytone0b93ed2014-08-22 11:27:32 -04003784static void selinux_file_set_fowner(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 struct file_security_struct *fsec;
3787
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 fsec = file->f_security;
David Howells275bb412008-11-14 10:39:19 +11003789 fsec->fown_sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790}
3791
3792static int selinux_file_send_sigiotask(struct task_struct *tsk,
3793 struct fown_struct *fown, int signum)
3794{
Eric Paris828dfe12008-04-17 13:17:49 -04003795 struct file *file;
Stephen Smalley65c90bc2009-05-04 15:43:18 -04003796 u32 sid = task_sid(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 struct file_security_struct *fsec;
3799
3800 /* struct fown_struct is never outside the context of a struct file */
Eric Paris828dfe12008-04-17 13:17:49 -04003801 file = container_of(fown, struct file, f_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 fsec = file->f_security;
3804
3805 if (!signum)
3806 perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
3807 else
3808 perm = signal_to_av(signum);
3809
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003810 return avc_has_perm(&selinux_state,
3811 fsec->fown_sid, sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 SECCLASS_PROCESS, perm, NULL);
3813}
3814
3815static int selinux_file_receive(struct file *file)
3816{
David Howells88e67f32008-11-14 10:39:21 +11003817 const struct cred *cred = current_cred();
3818
3819 return file_has_perm(cred, file, file_to_av(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820}
3821
Al Viro94817692018-07-10 14:13:18 -04003822static int selinux_file_open(struct file *file)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003823{
3824 struct file_security_struct *fsec;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003825 struct inode_security_struct *isec;
David Howellsd84f4f92008-11-14 10:39:23 +11003826
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003827 fsec = file->f_security;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05003828 isec = inode_security(file_inode(file));
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003829 /*
3830 * Save inode label and policy sequence number
3831 * at open-time so that selinux_file_permission
3832 * can determine whether revalidation is necessary.
3833 * Task label is already saved in the file security
3834 * struct as its SID.
3835 */
3836 fsec->isid = isec->sid;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003837 fsec->pseqno = avc_policy_seqno(&selinux_state);
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003838 /*
3839 * Since the inode label or policy seqno may have changed
3840 * between the selinux_inode_permission check and the saving
3841 * of state above, recheck that access is still permitted.
3842 * Otherwise, access might never be revalidated against the
3843 * new inode label or new policy.
3844 * This check is not redundant - do not remove.
3845 */
Al Viro94817692018-07-10 14:13:18 -04003846 return file_path_has_perm(file->f_cred, file, open_file_to_av(file));
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003847}
3848
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849/* task security operations */
3850
Tetsuo Handaa79be232017-03-28 23:08:45 +09003851static int selinux_task_alloc(struct task_struct *task,
3852 unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003854 u32 sid = current_sid();
3855
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003856 return avc_has_perm(&selinux_state,
3857 sid, sid, SECCLASS_PROCESS, PROCESS__FORK, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858}
3859
David Howellsf1752ee2008-11-14 10:39:17 +11003860/*
David Howellsee18d642009-09-02 09:14:21 +01003861 * allocate the SELinux part of blank credentials
3862 */
3863static int selinux_cred_alloc_blank(struct cred *cred, gfp_t gfp)
3864{
3865 struct task_security_struct *tsec;
3866
3867 tsec = kzalloc(sizeof(struct task_security_struct), gfp);
3868 if (!tsec)
3869 return -ENOMEM;
3870
3871 cred->security = tsec;
3872 return 0;
3873}
3874
3875/*
David Howellsf1752ee2008-11-14 10:39:17 +11003876 * detach and free the LSM part of a set of credentials
3877 */
3878static void selinux_cred_free(struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879{
David Howellsf1752ee2008-11-14 10:39:17 +11003880 struct task_security_struct *tsec = cred->security;
David Howellse0e81732009-09-02 09:13:40 +01003881
Tetsuo Handa2edeaa32011-02-07 13:36:10 +00003882 /*
3883 * cred->security == NULL if security_cred_alloc_blank() or
3884 * security_prepare_creds() returned an error.
3885 */
3886 BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE);
David Howellse0e81732009-09-02 09:13:40 +01003887 cred->security = (void *) 0x7UL;
David Howellsf1752ee2008-11-14 10:39:17 +11003888 kfree(tsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889}
3890
David Howellsd84f4f92008-11-14 10:39:23 +11003891/*
3892 * prepare a new set of credentials for modification
3893 */
3894static int selinux_cred_prepare(struct cred *new, const struct cred *old,
3895 gfp_t gfp)
3896{
3897 const struct task_security_struct *old_tsec;
3898 struct task_security_struct *tsec;
3899
3900 old_tsec = old->security;
3901
3902 tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp);
3903 if (!tsec)
3904 return -ENOMEM;
3905
3906 new->security = tsec;
3907 return 0;
3908}
3909
3910/*
David Howellsee18d642009-09-02 09:14:21 +01003911 * transfer the SELinux data to a blank set of creds
3912 */
3913static void selinux_cred_transfer(struct cred *new, const struct cred *old)
3914{
3915 const struct task_security_struct *old_tsec = old->security;
3916 struct task_security_struct *tsec = new->security;
3917
3918 *tsec = *old_tsec;
3919}
3920
Matthew Garrett3ec30112018-01-08 13:36:19 -08003921static void selinux_cred_getsecid(const struct cred *c, u32 *secid)
3922{
3923 *secid = cred_sid(c);
3924}
3925
David Howellsee18d642009-09-02 09:14:21 +01003926/*
David Howells3a3b7ce2008-11-14 10:39:28 +11003927 * set the security data for a kernel service
3928 * - all the creation contexts are set to unlabelled
3929 */
3930static int selinux_kernel_act_as(struct cred *new, u32 secid)
3931{
3932 struct task_security_struct *tsec = new->security;
3933 u32 sid = current_sid();
3934 int ret;
3935
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003936 ret = avc_has_perm(&selinux_state,
3937 sid, secid,
David Howells3a3b7ce2008-11-14 10:39:28 +11003938 SECCLASS_KERNEL_SERVICE,
3939 KERNEL_SERVICE__USE_AS_OVERRIDE,
3940 NULL);
3941 if (ret == 0) {
3942 tsec->sid = secid;
3943 tsec->create_sid = 0;
3944 tsec->keycreate_sid = 0;
3945 tsec->sockcreate_sid = 0;
3946 }
3947 return ret;
3948}
3949
3950/*
3951 * set the file creation context in a security record to the same as the
3952 * objective context of the specified inode
3953 */
3954static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
3955{
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05003956 struct inode_security_struct *isec = inode_security(inode);
David Howells3a3b7ce2008-11-14 10:39:28 +11003957 struct task_security_struct *tsec = new->security;
3958 u32 sid = current_sid();
3959 int ret;
3960
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003961 ret = avc_has_perm(&selinux_state,
3962 sid, isec->sid,
David Howells3a3b7ce2008-11-14 10:39:28 +11003963 SECCLASS_KERNEL_SERVICE,
3964 KERNEL_SERVICE__CREATE_FILES_AS,
3965 NULL);
3966
3967 if (ret == 0)
3968 tsec->create_sid = isec->sid;
David Howellsef574712010-02-26 01:56:16 +00003969 return ret;
David Howells3a3b7ce2008-11-14 10:39:28 +11003970}
3971
Eric Parisdd8dbf22009-11-03 16:35:32 +11003972static int selinux_kernel_module_request(char *kmod_name)
Eric Paris25354c42009-08-13 09:45:03 -04003973{
Eric Parisdd8dbf22009-11-03 16:35:32 +11003974 struct common_audit_data ad;
3975
Eric Paris50c205f2012-04-04 15:01:43 -04003976 ad.type = LSM_AUDIT_DATA_KMOD;
Eric Parisdd8dbf22009-11-03 16:35:32 +11003977 ad.u.kmod_name = kmod_name;
3978
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003979 return avc_has_perm(&selinux_state,
3980 current_sid(), SECINITSID_KERNEL, SECCLASS_SYSTEM,
Eric Parisdd8dbf22009-11-03 16:35:32 +11003981 SYSTEM__MODULE_REQUEST, &ad);
Eric Paris25354c42009-08-13 09:45:03 -04003982}
3983
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003984static int selinux_kernel_module_from_file(struct file *file)
3985{
3986 struct common_audit_data ad;
3987 struct inode_security_struct *isec;
3988 struct file_security_struct *fsec;
3989 u32 sid = current_sid();
3990 int rc;
3991
3992 /* init_module */
3993 if (file == NULL)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003994 return avc_has_perm(&selinux_state,
3995 sid, sid, SECCLASS_SYSTEM,
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003996 SYSTEM__MODULE_LOAD, NULL);
3997
3998 /* finit_module */
Paul Moore20cdef82016-04-04 14:14:42 -04003999
Vivek Goyal43af5de2016-09-09 11:37:49 -04004000 ad.type = LSM_AUDIT_DATA_FILE;
4001 ad.u.file = file;
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07004002
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07004003 fsec = file->f_security;
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07004004 if (sid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004005 rc = avc_has_perm(&selinux_state,
4006 sid, fsec->sid, SECCLASS_FD, FD__USE, &ad);
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07004007 if (rc)
4008 return rc;
4009 }
4010
Paul Moore20cdef82016-04-04 14:14:42 -04004011 isec = inode_security(file_inode(file));
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004012 return avc_has_perm(&selinux_state,
4013 sid, isec->sid, SECCLASS_SYSTEM,
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07004014 SYSTEM__MODULE_LOAD, &ad);
4015}
4016
4017static int selinux_kernel_read_file(struct file *file,
4018 enum kernel_read_file_id id)
4019{
4020 int rc = 0;
4021
4022 switch (id) {
4023 case READING_MODULE:
4024 rc = selinux_kernel_module_from_file(file);
4025 break;
4026 default:
4027 break;
4028 }
4029
4030 return rc;
4031}
4032
Mimi Zoharc77b8cd2018-07-13 14:06:02 -04004033static int selinux_kernel_load_data(enum kernel_load_data_id id)
4034{
4035 int rc = 0;
4036
4037 switch (id) {
4038 case LOADING_MODULE:
4039 rc = selinux_kernel_module_from_file(NULL);
4040 default:
4041 break;
4042 }
4043
4044 return rc;
4045}
4046
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
4048{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004049 return avc_has_perm(&selinux_state,
4050 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004051 PROCESS__SETPGID, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052}
4053
4054static int selinux_task_getpgid(struct task_struct *p)
4055{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004056 return avc_has_perm(&selinux_state,
4057 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004058 PROCESS__GETPGID, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059}
4060
4061static int selinux_task_getsid(struct task_struct *p)
4062{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004063 return avc_has_perm(&selinux_state,
4064 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004065 PROCESS__GETSESSION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066}
4067
David Quigleyf9008e4c2006-06-30 01:55:46 -07004068static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
4069{
David Howells275bb412008-11-14 10:39:19 +11004070 *secid = task_sid(p);
David Quigleyf9008e4c2006-06-30 01:55:46 -07004071}
4072
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073static int selinux_task_setnice(struct task_struct *p, int nice)
4074{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004075 return avc_has_perm(&selinux_state,
4076 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004077 PROCESS__SETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078}
4079
James Morris03e68062006-06-23 02:03:58 -07004080static int selinux_task_setioprio(struct task_struct *p, int ioprio)
4081{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004082 return avc_has_perm(&selinux_state,
4083 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004084 PROCESS__SETSCHED, NULL);
James Morris03e68062006-06-23 02:03:58 -07004085}
4086
David Quigleya1836a42006-06-30 01:55:49 -07004087static int selinux_task_getioprio(struct task_struct *p)
4088{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004089 return avc_has_perm(&selinux_state,
4090 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004091 PROCESS__GETSCHED, NULL);
David Quigleya1836a42006-06-30 01:55:49 -07004092}
4093
Corentin LABBE42985552017-10-04 20:32:18 +02004094static int selinux_task_prlimit(const struct cred *cred, const struct cred *tcred,
4095 unsigned int flags)
Stephen Smalley791ec492017-02-17 07:57:00 -05004096{
4097 u32 av = 0;
4098
Stephen Smalley84e68852017-02-28 09:35:08 -05004099 if (!flags)
4100 return 0;
Stephen Smalley791ec492017-02-17 07:57:00 -05004101 if (flags & LSM_PRLIMIT_WRITE)
4102 av |= PROCESS__SETRLIMIT;
4103 if (flags & LSM_PRLIMIT_READ)
4104 av |= PROCESS__GETRLIMIT;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004105 return avc_has_perm(&selinux_state,
4106 cred_sid(cred), cred_sid(tcred),
Stephen Smalley791ec492017-02-17 07:57:00 -05004107 SECCLASS_PROCESS, av, NULL);
4108}
4109
Jiri Slaby8fd00b42009-08-26 18:41:16 +02004110static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
4111 struct rlimit *new_rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112{
Jiri Slaby8fd00b42009-08-26 18:41:16 +02004113 struct rlimit *old_rlim = p->signal->rlim + resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114
4115 /* Control the ability to change the hard limit (whether
4116 lowering or raising it), so that the hard limit can
4117 later be used as a safe reset point for the soft limit
David Howellsd84f4f92008-11-14 10:39:23 +11004118 upon context transitions. See selinux_bprm_committing_creds. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 if (old_rlim->rlim_max != new_rlim->rlim_max)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004120 return avc_has_perm(&selinux_state,
4121 current_sid(), task_sid(p),
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004122 SECCLASS_PROCESS, PROCESS__SETRLIMIT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123
4124 return 0;
4125}
4126
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09004127static int selinux_task_setscheduler(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004129 return avc_has_perm(&selinux_state,
4130 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004131 PROCESS__SETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132}
4133
4134static int selinux_task_getscheduler(struct task_struct *p)
4135{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004136 return avc_has_perm(&selinux_state,
4137 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004138 PROCESS__GETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139}
4140
David Quigley35601542006-06-23 02:04:01 -07004141static int selinux_task_movememory(struct task_struct *p)
4142{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004143 return avc_has_perm(&selinux_state,
4144 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004145 PROCESS__SETSCHED, NULL);
David Quigley35601542006-06-23 02:04:01 -07004146}
4147
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02004148static int selinux_task_kill(struct task_struct *p, struct kernel_siginfo *info,
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004149 int sig, const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150{
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004151 u32 secid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 if (!sig)
4155 perm = PROCESS__SIGNULL; /* null signal; existence test */
4156 else
4157 perm = signal_to_av(sig);
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004158 if (!cred)
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004159 secid = current_sid();
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004160 else
4161 secid = cred_sid(cred);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004162 return avc_has_perm(&selinux_state,
4163 secid, task_sid(p), SECCLASS_PROCESS, perm, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164}
4165
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166static void selinux_task_to_inode(struct task_struct *p,
4167 struct inode *inode)
4168{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 struct inode_security_struct *isec = inode->i_security;
David Howells275bb412008-11-14 10:39:19 +11004170 u32 sid = task_sid(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004172 spin_lock(&isec->lock);
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01004173 isec->sclass = inode_mode_to_security_class(inode->i_mode);
David Howells275bb412008-11-14 10:39:19 +11004174 isec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004175 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004176 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177}
4178
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004180static int selinux_parse_skb_ipv4(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04004181 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182{
4183 int offset, ihlen, ret = -EINVAL;
4184 struct iphdr _iph, *ih;
4185
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03004186 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
4188 if (ih == NULL)
4189 goto out;
4190
4191 ihlen = ih->ihl * 4;
4192 if (ihlen < sizeof(_iph))
4193 goto out;
4194
Eric Paris48c62af2012-04-02 13:15:44 -04004195 ad->u.net->v4info.saddr = ih->saddr;
4196 ad->u.net->v4info.daddr = ih->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 ret = 0;
4198
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004199 if (proto)
4200 *proto = ih->protocol;
4201
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202 switch (ih->protocol) {
Eric Paris828dfe12008-04-17 13:17:49 -04004203 case IPPROTO_TCP: {
4204 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205
Eric Paris828dfe12008-04-17 13:17:49 -04004206 if (ntohs(ih->frag_off) & IP_OFFSET)
4207 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208
4209 offset += ihlen;
4210 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4211 if (th == NULL)
4212 break;
4213
Eric Paris48c62af2012-04-02 13:15:44 -04004214 ad->u.net->sport = th->source;
4215 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218
Eric Paris828dfe12008-04-17 13:17:49 -04004219 case IPPROTO_UDP: {
4220 struct udphdr _udph, *uh;
4221
4222 if (ntohs(ih->frag_off) & IP_OFFSET)
4223 break;
4224
4225 offset += ihlen;
4226 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4227 if (uh == NULL)
4228 break;
4229
Eric Paris48c62af2012-04-02 13:15:44 -04004230 ad->u.net->sport = uh->source;
4231 ad->u.net->dport = uh->dest;
Eric Paris828dfe12008-04-17 13:17:49 -04004232 break;
4233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234
James Morris2ee92d42006-11-13 16:09:01 -08004235 case IPPROTO_DCCP: {
4236 struct dccp_hdr _dccph, *dh;
4237
4238 if (ntohs(ih->frag_off) & IP_OFFSET)
4239 break;
4240
4241 offset += ihlen;
4242 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4243 if (dh == NULL)
4244 break;
4245
Eric Paris48c62af2012-04-02 13:15:44 -04004246 ad->u.net->sport = dh->dccph_sport;
4247 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08004248 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004249 }
James Morris2ee92d42006-11-13 16:09:01 -08004250
Richard Hainesd4529302018-02-13 20:57:18 +00004251#if IS_ENABLED(CONFIG_IP_SCTP)
4252 case IPPROTO_SCTP: {
4253 struct sctphdr _sctph, *sh;
4254
4255 if (ntohs(ih->frag_off) & IP_OFFSET)
4256 break;
4257
4258 offset += ihlen;
4259 sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4260 if (sh == NULL)
4261 break;
4262
4263 ad->u.net->sport = sh->source;
4264 ad->u.net->dport = sh->dest;
4265 break;
4266 }
4267#endif
Eric Paris828dfe12008-04-17 13:17:49 -04004268 default:
4269 break;
4270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271out:
4272 return ret;
4273}
4274
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04004275#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276
4277/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004278static int selinux_parse_skb_ipv6(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04004279 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280{
4281 u8 nexthdr;
4282 int ret = -EINVAL, offset;
4283 struct ipv6hdr _ipv6h, *ip6;
Jesse Gross75f28112011-11-30 17:05:51 -08004284 __be16 frag_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03004286 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
4288 if (ip6 == NULL)
4289 goto out;
4290
Eric Paris48c62af2012-04-02 13:15:44 -04004291 ad->u.net->v6info.saddr = ip6->saddr;
4292 ad->u.net->v6info.daddr = ip6->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 ret = 0;
4294
4295 nexthdr = ip6->nexthdr;
4296 offset += sizeof(_ipv6h);
Jesse Gross75f28112011-11-30 17:05:51 -08004297 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 if (offset < 0)
4299 goto out;
4300
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004301 if (proto)
4302 *proto = nexthdr;
4303
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 switch (nexthdr) {
4305 case IPPROTO_TCP: {
Eric Paris828dfe12008-04-17 13:17:49 -04004306 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307
4308 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4309 if (th == NULL)
4310 break;
4311
Eric Paris48c62af2012-04-02 13:15:44 -04004312 ad->u.net->sport = th->source;
4313 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 break;
4315 }
4316
4317 case IPPROTO_UDP: {
4318 struct udphdr _udph, *uh;
4319
4320 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4321 if (uh == NULL)
4322 break;
4323
Eric Paris48c62af2012-04-02 13:15:44 -04004324 ad->u.net->sport = uh->source;
4325 ad->u.net->dport = uh->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 break;
4327 }
4328
James Morris2ee92d42006-11-13 16:09:01 -08004329 case IPPROTO_DCCP: {
4330 struct dccp_hdr _dccph, *dh;
4331
4332 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4333 if (dh == NULL)
4334 break;
4335
Eric Paris48c62af2012-04-02 13:15:44 -04004336 ad->u.net->sport = dh->dccph_sport;
4337 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08004338 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004339 }
James Morris2ee92d42006-11-13 16:09:01 -08004340
Richard Hainesd4529302018-02-13 20:57:18 +00004341#if IS_ENABLED(CONFIG_IP_SCTP)
4342 case IPPROTO_SCTP: {
4343 struct sctphdr _sctph, *sh;
4344
4345 sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4346 if (sh == NULL)
4347 break;
4348
4349 ad->u.net->sport = sh->source;
4350 ad->u.net->dport = sh->dest;
4351 break;
4352 }
4353#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 /* includes fragments */
4355 default:
4356 break;
4357 }
4358out:
4359 return ret;
4360}
4361
4362#endif /* IPV6 */
4363
Thomas Liu2bf49692009-07-14 12:14:09 -04004364static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
David Howellscf9481e2008-07-27 21:31:07 +10004365 char **_addrp, int src, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366{
David Howellscf9481e2008-07-27 21:31:07 +10004367 char *addrp;
4368 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369
Eric Paris48c62af2012-04-02 13:15:44 -04004370 switch (ad->u.net->family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 case PF_INET:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004372 ret = selinux_parse_skb_ipv4(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10004373 if (ret)
4374 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04004375 addrp = (char *)(src ? &ad->u.net->v4info.saddr :
4376 &ad->u.net->v4info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10004377 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04004379#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380 case PF_INET6:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004381 ret = selinux_parse_skb_ipv6(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10004382 if (ret)
4383 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04004384 addrp = (char *)(src ? &ad->u.net->v6info.saddr :
4385 &ad->u.net->v6info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10004386 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387#endif /* IPV6 */
4388 default:
David Howellscf9481e2008-07-27 21:31:07 +10004389 addrp = NULL;
4390 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 }
4392
David Howellscf9481e2008-07-27 21:31:07 +10004393parse_error:
peter enderborgc103a912018-06-12 10:09:03 +02004394 pr_warn(
David Howellscf9481e2008-07-27 21:31:07 +10004395 "SELinux: failure in selinux_parse_skb(),"
4396 " unable to parse packet\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 return ret;
David Howellscf9481e2008-07-27 21:31:07 +10004398
4399okay:
4400 if (_addrp)
4401 *_addrp = addrp;
4402 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403}
4404
Paul Moore4f6a9932007-03-01 14:35:22 -05004405/**
Paul Moore220deb92008-01-29 08:38:23 -05004406 * selinux_skb_peerlbl_sid - Determine the peer label of a packet
Paul Moore4f6a9932007-03-01 14:35:22 -05004407 * @skb: the packet
Paul Moore75e22912008-01-29 08:38:04 -05004408 * @family: protocol family
Paul Moore220deb92008-01-29 08:38:23 -05004409 * @sid: the packet's peer label SID
Paul Moore4f6a9932007-03-01 14:35:22 -05004410 *
4411 * Description:
Paul Moore220deb92008-01-29 08:38:23 -05004412 * Check the various different forms of network peer labeling and determine
4413 * the peer label/SID for the packet; most of the magic actually occurs in
4414 * the security server function security_net_peersid_cmp(). The function
4415 * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
4416 * or -EACCES if @sid is invalid due to inconsistencies with the different
4417 * peer labels.
Paul Moore4f6a9932007-03-01 14:35:22 -05004418 *
4419 */
Paul Moore220deb92008-01-29 08:38:23 -05004420static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
Paul Moore4f6a9932007-03-01 14:35:22 -05004421{
Paul Moore71f1cb02008-01-29 08:51:16 -05004422 int err;
Paul Moore4f6a9932007-03-01 14:35:22 -05004423 u32 xfrm_sid;
4424 u32 nlbl_sid;
Paul Moore220deb92008-01-29 08:38:23 -05004425 u32 nlbl_type;
Paul Moore4f6a9932007-03-01 14:35:22 -05004426
Paul Moore817eff72013-12-10 14:57:54 -05004427 err = selinux_xfrm_skb_sid(skb, &xfrm_sid);
Paul Moorebed4d7e2013-07-23 17:38:40 -04004428 if (unlikely(err))
4429 return -EACCES;
4430 err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
4431 if (unlikely(err))
4432 return -EACCES;
Paul Moore220deb92008-01-29 08:38:23 -05004433
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004434 err = security_net_peersid_resolve(&selinux_state, nlbl_sid,
4435 nlbl_type, xfrm_sid, sid);
Paul Moore71f1cb02008-01-29 08:51:16 -05004436 if (unlikely(err)) {
peter enderborgc103a912018-06-12 10:09:03 +02004437 pr_warn(
Paul Moore71f1cb02008-01-29 08:51:16 -05004438 "SELinux: failure in selinux_skb_peerlbl_sid(),"
4439 " unable to determine packet's peer label\n");
Paul Moore220deb92008-01-29 08:38:23 -05004440 return -EACCES;
Paul Moore71f1cb02008-01-29 08:51:16 -05004441 }
Paul Moore220deb92008-01-29 08:38:23 -05004442
4443 return 0;
Paul Moore4f6a9932007-03-01 14:35:22 -05004444}
4445
Paul Moore446b8022013-12-04 16:10:51 -05004446/**
4447 * selinux_conn_sid - Determine the child socket label for a connection
4448 * @sk_sid: the parent socket's SID
4449 * @skb_sid: the packet's SID
4450 * @conn_sid: the resulting connection SID
4451 *
4452 * If @skb_sid is valid then the user:role:type information from @sk_sid is
4453 * combined with the MLS information from @skb_sid in order to create
4454 * @conn_sid. If @skb_sid is not valid then then @conn_sid is simply a copy
4455 * of @sk_sid. Returns zero on success, negative values on failure.
4456 *
4457 */
4458static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid)
4459{
4460 int err = 0;
4461
4462 if (skb_sid != SECSID_NULL)
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004463 err = security_sid_mls_copy(&selinux_state, sk_sid, skb_sid,
4464 conn_sid);
Paul Moore446b8022013-12-04 16:10:51 -05004465 else
4466 *conn_sid = sk_sid;
4467
4468 return err;
4469}
4470
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471/* socket security operations */
Paul Moored4f2d972010-04-22 14:46:18 -04004472
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004473static int socket_sockcreate_sid(const struct task_security_struct *tsec,
4474 u16 secclass, u32 *socksid)
Paul Moored4f2d972010-04-22 14:46:18 -04004475{
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004476 if (tsec->sockcreate_sid > SECSID_NULL) {
4477 *socksid = tsec->sockcreate_sid;
4478 return 0;
4479 }
4480
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004481 return security_transition_sid(&selinux_state, tsec->sid, tsec->sid,
4482 secclass, NULL, socksid);
Paul Moored4f2d972010-04-22 14:46:18 -04004483}
4484
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004485static int sock_has_perm(struct sock *sk, u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486{
Paul Moore253bfae2010-04-22 14:46:19 -04004487 struct sk_security_struct *sksec = sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004488 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004489 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490
Paul Moore253bfae2010-04-22 14:46:19 -04004491 if (sksec->sid == SECINITSID_KERNEL)
4492 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493
Eric Paris50c205f2012-04-04 15:01:43 -04004494 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004495 ad.u.net = &net;
4496 ad.u.net->sk = sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004498 return avc_has_perm(&selinux_state,
4499 current_sid(), sksec->sid, sksec->sclass, perms,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004500 &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501}
4502
4503static int selinux_socket_create(int family, int type,
4504 int protocol, int kern)
4505{
Paul Moore5fb49872010-04-22 14:46:19 -04004506 const struct task_security_struct *tsec = current_security();
Paul Moored4f2d972010-04-22 14:46:18 -04004507 u32 newsid;
David Howells275bb412008-11-14 10:39:19 +11004508 u16 secclass;
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004509 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510
4511 if (kern)
Paul Moored4f2d972010-04-22 14:46:18 -04004512 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513
David Howells275bb412008-11-14 10:39:19 +11004514 secclass = socket_type_to_security_class(family, type, protocol);
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004515 rc = socket_sockcreate_sid(tsec, secclass, &newsid);
4516 if (rc)
4517 return rc;
4518
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004519 return avc_has_perm(&selinux_state,
4520 tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521}
4522
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07004523static int selinux_socket_post_create(struct socket *sock, int family,
4524 int type, int protocol, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525{
Paul Moore5fb49872010-04-22 14:46:19 -04004526 const struct task_security_struct *tsec = current_security();
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05004527 struct inode_security_struct *isec = inode_security_novalidate(SOCK_INODE(sock));
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004528 struct sk_security_struct *sksec;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004529 u16 sclass = socket_type_to_security_class(family, type, protocol);
4530 u32 sid = SECINITSID_KERNEL;
David Howells275bb412008-11-14 10:39:19 +11004531 int err = 0;
4532
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004533 if (!kern) {
4534 err = socket_sockcreate_sid(tsec, sclass, &sid);
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004535 if (err)
4536 return err;
4537 }
David Howells275bb412008-11-14 10:39:19 +11004538
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004539 isec->sclass = sclass;
4540 isec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004541 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004543 if (sock->sk) {
4544 sksec = sock->sk->sk_security;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004545 sksec->sclass = sclass;
4546 sksec->sid = sid;
Richard Hainesd4529302018-02-13 20:57:18 +00004547 /* Allows detection of the first association on this socket */
4548 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4549 sksec->sctp_assoc_state = SCTP_ASSOC_UNSET;
4550
Paul Moore389fb8002009-03-27 17:10:34 -04004551 err = selinux_netlbl_socket_post_create(sock->sk, family);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004552 }
4553
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07004554 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555}
4556
David Herrmann0b811db2018-05-04 16:28:21 +02004557static int selinux_socket_socketpair(struct socket *socka,
4558 struct socket *sockb)
4559{
4560 struct sk_security_struct *sksec_a = socka->sk->sk_security;
4561 struct sk_security_struct *sksec_b = sockb->sk->sk_security;
4562
4563 sksec_a->peer_sid = sksec_b->sid;
4564 sksec_b->peer_sid = sksec_a->sid;
4565
4566 return 0;
4567}
4568
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569/* Range of port numbers used to automatically bind.
4570 Need to determine whether we should perform a name_bind
4571 permission check between the socket and the port number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572
4573static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
4574{
Paul Moore253bfae2010-04-22 14:46:19 -04004575 struct sock *sk = sock->sk;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004576 struct sk_security_struct *sksec = sk->sk_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 u16 family;
4578 int err;
4579
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004580 err = sock_has_perm(sk, SOCKET__BIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 if (err)
4582 goto out;
4583
Richard Hainesd4529302018-02-13 20:57:18 +00004584 /* If PF_INET or PF_INET6, check name_bind permission for the port. */
Paul Moore253bfae2010-04-22 14:46:19 -04004585 family = sk->sk_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 if (family == PF_INET || family == PF_INET6) {
4587 char *addrp;
Thomas Liu2bf49692009-07-14 12:14:09 -04004588 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004589 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 struct sockaddr_in *addr4 = NULL;
4591 struct sockaddr_in6 *addr6 = NULL;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004592 u16 family_sa = address->sa_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 unsigned short snum;
James Morrise399f982008-06-12 01:39:58 +10004594 u32 sid, node_perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595
Richard Hainesd4529302018-02-13 20:57:18 +00004596 /*
4597 * sctp_bindx(3) calls via selinux_sctp_bind_connect()
4598 * that validates multiple binding addresses. Because of this
4599 * need to check address->sa_family as it is possible to have
4600 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4601 */
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004602 switch (family_sa) {
4603 case AF_UNSPEC:
Richard Haines68741a8a2018-03-02 19:54:34 +00004604 case AF_INET:
4605 if (addrlen < sizeof(struct sockaddr_in))
4606 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 addr4 = (struct sockaddr_in *)address;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004608 if (family_sa == AF_UNSPEC) {
4609 /* see __inet_bind(), we only want to allow
4610 * AF_UNSPEC if the address is INADDR_ANY
4611 */
4612 if (addr4->sin_addr.s_addr != htonl(INADDR_ANY))
4613 goto err_af;
4614 family_sa = AF_INET;
4615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616 snum = ntohs(addr4->sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 addrp = (char *)&addr4->sin_addr.s_addr;
Richard Haines68741a8a2018-03-02 19:54:34 +00004618 break;
4619 case AF_INET6:
4620 if (addrlen < SIN6_LEN_RFC2133)
4621 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 addr6 = (struct sockaddr_in6 *)address;
4623 snum = ntohs(addr6->sin6_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 addrp = (char *)&addr6->sin6_addr.s6_addr;
Richard Haines68741a8a2018-03-02 19:54:34 +00004625 break;
4626 default:
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004627 goto err_af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628 }
4629
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004630 ad.type = LSM_AUDIT_DATA_NET;
4631 ad.u.net = &net;
4632 ad.u.net->sport = htons(snum);
4633 ad.u.net->family = family_sa;
4634
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004635 if (snum) {
4636 int low, high;
4637
Eric W. Biederman0bbf87d2013-09-28 14:10:59 -07004638 inet_get_local_port_range(sock_net(sk), &low, &high);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004639
Krister Johansen4548b682017-01-20 17:49:11 -08004640 if (snum < max(inet_prot_sock(sock_net(sk)), low) ||
4641 snum > high) {
Paul Moore3e1121722008-04-10 10:48:14 -04004642 err = sel_netport_sid(sk->sk_protocol,
4643 snum, &sid);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004644 if (err)
4645 goto out;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004646 err = avc_has_perm(&selinux_state,
4647 sksec->sid, sid,
Paul Moore253bfae2010-04-22 14:46:19 -04004648 sksec->sclass,
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004649 SOCKET__NAME_BIND, &ad);
4650 if (err)
4651 goto out;
4652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 }
Eric Paris828dfe12008-04-17 13:17:49 -04004654
Paul Moore253bfae2010-04-22 14:46:19 -04004655 switch (sksec->sclass) {
James Morris13402582005-09-30 14:24:34 -04004656 case SECCLASS_TCP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 node_perm = TCP_SOCKET__NODE_BIND;
4658 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004659
James Morris13402582005-09-30 14:24:34 -04004660 case SECCLASS_UDP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 node_perm = UDP_SOCKET__NODE_BIND;
4662 break;
James Morris2ee92d42006-11-13 16:09:01 -08004663
4664 case SECCLASS_DCCP_SOCKET:
4665 node_perm = DCCP_SOCKET__NODE_BIND;
4666 break;
4667
Richard Hainesd4529302018-02-13 20:57:18 +00004668 case SECCLASS_SCTP_SOCKET:
4669 node_perm = SCTP_SOCKET__NODE_BIND;
4670 break;
4671
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672 default:
4673 node_perm = RAWIP_SOCKET__NODE_BIND;
4674 break;
4675 }
Eric Paris828dfe12008-04-17 13:17:49 -04004676
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004677 err = sel_netnode_sid(addrp, family_sa, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 if (err)
4679 goto out;
Eric Paris828dfe12008-04-17 13:17:49 -04004680
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004681 if (family_sa == AF_INET)
Eric Paris48c62af2012-04-02 13:15:44 -04004682 ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683 else
Eric Paris48c62af2012-04-02 13:15:44 -04004684 ad.u.net->v6info.saddr = addr6->sin6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004686 err = avc_has_perm(&selinux_state,
4687 sksec->sid, sid,
Paul Moore253bfae2010-04-22 14:46:19 -04004688 sksec->sclass, node_perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 if (err)
4690 goto out;
4691 }
4692out:
4693 return err;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004694err_af:
4695 /* Note that SCTP services expect -EINVAL, others -EAFNOSUPPORT. */
4696 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4697 return -EINVAL;
4698 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699}
4700
Richard Hainesd4529302018-02-13 20:57:18 +00004701/* This supports connect(2) and SCTP connect services such as sctp_connectx(3)
Mauro Carvalho Chehab5fb94e92018-05-08 15:14:57 -03004702 * and sctp_sendmsg(3) as described in Documentation/security/LSM-sctp.rst
Richard Hainesd4529302018-02-13 20:57:18 +00004703 */
4704static int selinux_socket_connect_helper(struct socket *sock,
4705 struct sockaddr *address, int addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706{
Paul Moore014ab192008-10-10 10:16:33 -04004707 struct sock *sk = sock->sk;
Paul Moore253bfae2010-04-22 14:46:19 -04004708 struct sk_security_struct *sksec = sk->sk_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 int err;
4710
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004711 err = sock_has_perm(sk, SOCKET__CONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 if (err)
4713 return err;
4714
4715 /*
Richard Hainesd4529302018-02-13 20:57:18 +00004716 * If a TCP, DCCP or SCTP socket, check name_connect permission
4717 * for the port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718 */
Paul Moore253bfae2010-04-22 14:46:19 -04004719 if (sksec->sclass == SECCLASS_TCP_SOCKET ||
Richard Hainesd4529302018-02-13 20:57:18 +00004720 sksec->sclass == SECCLASS_DCCP_SOCKET ||
4721 sksec->sclass == SECCLASS_SCTP_SOCKET) {
Thomas Liu2bf49692009-07-14 12:14:09 -04004722 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004723 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 struct sockaddr_in *addr4 = NULL;
4725 struct sockaddr_in6 *addr6 = NULL;
4726 unsigned short snum;
James Morris2ee92d42006-11-13 16:09:01 -08004727 u32 sid, perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728
Richard Hainesd4529302018-02-13 20:57:18 +00004729 /* sctp_connectx(3) calls via selinux_sctp_bind_connect()
4730 * that validates multiple connect addresses. Because of this
4731 * need to check address->sa_family as it is possible to have
4732 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4733 */
Richard Haines68741a8a2018-03-02 19:54:34 +00004734 switch (address->sa_family) {
4735 case AF_INET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 addr4 = (struct sockaddr_in *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004737 if (addrlen < sizeof(struct sockaddr_in))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 return -EINVAL;
4739 snum = ntohs(addr4->sin_port);
Richard Haines68741a8a2018-03-02 19:54:34 +00004740 break;
4741 case AF_INET6:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742 addr6 = (struct sockaddr_in6 *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004743 if (addrlen < SIN6_LEN_RFC2133)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 return -EINVAL;
4745 snum = ntohs(addr6->sin6_port);
Richard Haines68741a8a2018-03-02 19:54:34 +00004746 break;
4747 default:
4748 /* Note that SCTP services expect -EINVAL, whereas
4749 * others expect -EAFNOSUPPORT.
4750 */
4751 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4752 return -EINVAL;
4753 else
4754 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 }
4756
Paul Moore3e1121722008-04-10 10:48:14 -04004757 err = sel_netport_sid(sk->sk_protocol, snum, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758 if (err)
Richard Hainesd4529302018-02-13 20:57:18 +00004759 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760
Richard Hainesd4529302018-02-13 20:57:18 +00004761 switch (sksec->sclass) {
4762 case SECCLASS_TCP_SOCKET:
4763 perm = TCP_SOCKET__NAME_CONNECT;
4764 break;
4765 case SECCLASS_DCCP_SOCKET:
4766 perm = DCCP_SOCKET__NAME_CONNECT;
4767 break;
4768 case SECCLASS_SCTP_SOCKET:
4769 perm = SCTP_SOCKET__NAME_CONNECT;
4770 break;
4771 }
James Morris2ee92d42006-11-13 16:09:01 -08004772
Eric Paris50c205f2012-04-04 15:01:43 -04004773 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004774 ad.u.net = &net;
4775 ad.u.net->dport = htons(snum);
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004776 ad.u.net->family = address->sa_family;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004777 err = avc_has_perm(&selinux_state,
4778 sksec->sid, sid, sksec->sclass, perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 if (err)
Richard Hainesd4529302018-02-13 20:57:18 +00004780 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 }
4782
Richard Hainesd4529302018-02-13 20:57:18 +00004783 return 0;
4784}
Paul Moore014ab192008-10-10 10:16:33 -04004785
Richard Hainesd4529302018-02-13 20:57:18 +00004786/* Supports connect(2), see comments in selinux_socket_connect_helper() */
4787static int selinux_socket_connect(struct socket *sock,
4788 struct sockaddr *address, int addrlen)
4789{
4790 int err;
4791 struct sock *sk = sock->sk;
4792
4793 err = selinux_socket_connect_helper(sock, address, addrlen);
4794 if (err)
4795 return err;
4796
4797 return selinux_netlbl_socket_connect(sk, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798}
4799
4800static int selinux_socket_listen(struct socket *sock, int backlog)
4801{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004802 return sock_has_perm(sock->sk, SOCKET__LISTEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803}
4804
4805static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
4806{
4807 int err;
4808 struct inode_security_struct *isec;
4809 struct inode_security_struct *newisec;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004810 u16 sclass;
4811 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004813 err = sock_has_perm(sock->sk, SOCKET__ACCEPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814 if (err)
4815 return err;
4816
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05004817 isec = inode_security_novalidate(SOCK_INODE(sock));
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004818 spin_lock(&isec->lock);
4819 sclass = isec->sclass;
4820 sid = isec->sid;
4821 spin_unlock(&isec->lock);
4822
4823 newisec = inode_security_novalidate(SOCK_INODE(newsock));
4824 newisec->sclass = sclass;
4825 newisec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004826 newisec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827
4828 return 0;
4829}
4830
4831static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
Eric Paris828dfe12008-04-17 13:17:49 -04004832 int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004834 return sock_has_perm(sock->sk, SOCKET__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835}
4836
4837static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
4838 int size, int flags)
4839{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004840 return sock_has_perm(sock->sk, SOCKET__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841}
4842
4843static int selinux_socket_getsockname(struct socket *sock)
4844{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004845 return sock_has_perm(sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846}
4847
4848static int selinux_socket_getpeername(struct socket *sock)
4849{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004850 return sock_has_perm(sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851}
4852
Eric Paris828dfe12008-04-17 13:17:49 -04004853static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854{
Paul Mooref8687af2006-10-30 15:22:15 -08004855 int err;
4856
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004857 err = sock_has_perm(sock->sk, SOCKET__SETOPT);
Paul Mooref8687af2006-10-30 15:22:15 -08004858 if (err)
4859 return err;
4860
4861 return selinux_netlbl_socket_setsockopt(sock, level, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862}
4863
4864static int selinux_socket_getsockopt(struct socket *sock, int level,
4865 int optname)
4866{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004867 return sock_has_perm(sock->sk, SOCKET__GETOPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868}
4869
4870static int selinux_socket_shutdown(struct socket *sock, int how)
4871{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004872 return sock_has_perm(sock->sk, SOCKET__SHUTDOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873}
4874
David S. Miller3610cda2011-01-05 15:38:53 -08004875static int selinux_socket_unix_stream_connect(struct sock *sock,
4876 struct sock *other,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877 struct sock *newsk)
4878{
David S. Miller3610cda2011-01-05 15:38:53 -08004879 struct sk_security_struct *sksec_sock = sock->sk_security;
4880 struct sk_security_struct *sksec_other = other->sk_security;
Paul Moore4d1e2452010-04-22 14:46:18 -04004881 struct sk_security_struct *sksec_new = newsk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004882 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004883 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884 int err;
4885
Eric Paris50c205f2012-04-04 15:01:43 -04004886 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004887 ad.u.net = &net;
4888 ad.u.net->sk = other;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004890 err = avc_has_perm(&selinux_state,
4891 sksec_sock->sid, sksec_other->sid,
Paul Moore4d1e2452010-04-22 14:46:18 -04004892 sksec_other->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 UNIX_STREAM_SOCKET__CONNECTTO, &ad);
4894 if (err)
4895 return err;
4896
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 /* server child socket */
Paul Moore4d1e2452010-04-22 14:46:18 -04004898 sksec_new->peer_sid = sksec_sock->sid;
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004899 err = security_sid_mls_copy(&selinux_state, sksec_other->sid,
4900 sksec_sock->sid, &sksec_new->sid);
Paul Moore4d1e2452010-04-22 14:46:18 -04004901 if (err)
4902 return err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004903
Paul Moore4d1e2452010-04-22 14:46:18 -04004904 /* connecting socket */
4905 sksec_sock->peer_sid = sksec_new->sid;
4906
4907 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908}
4909
4910static int selinux_socket_unix_may_send(struct socket *sock,
4911 struct socket *other)
4912{
Paul Moore253bfae2010-04-22 14:46:19 -04004913 struct sk_security_struct *ssec = sock->sk->sk_security;
4914 struct sk_security_struct *osec = other->sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004915 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004916 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917
Eric Paris50c205f2012-04-04 15:01:43 -04004918 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004919 ad.u.net = &net;
4920 ad.u.net->sk = other->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004922 return avc_has_perm(&selinux_state,
4923 ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
Paul Moore253bfae2010-04-22 14:46:19 -04004924 &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925}
4926
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004927static int selinux_inet_sys_rcv_skb(struct net *ns, int ifindex,
4928 char *addrp, u16 family, u32 peer_sid,
Thomas Liu2bf49692009-07-14 12:14:09 -04004929 struct common_audit_data *ad)
Paul Mooreeffad8d2008-01-29 08:49:27 -05004930{
4931 int err;
4932 u32 if_sid;
4933 u32 node_sid;
4934
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004935 err = sel_netif_sid(ns, ifindex, &if_sid);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004936 if (err)
4937 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004938 err = avc_has_perm(&selinux_state,
4939 peer_sid, if_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004940 SECCLASS_NETIF, NETIF__INGRESS, ad);
4941 if (err)
4942 return err;
4943
4944 err = sel_netnode_sid(addrp, family, &node_sid);
4945 if (err)
4946 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004947 return avc_has_perm(&selinux_state,
4948 peer_sid, node_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004949 SECCLASS_NODE, NODE__RECVFROM, ad);
4950}
4951
Paul Moore220deb92008-01-29 08:38:23 -05004952static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
Paul Moored8395c82008-10-10 10:16:30 -04004953 u16 family)
Paul Moore220deb92008-01-29 08:38:23 -05004954{
Paul Moore277d3422008-12-31 12:54:11 -05004955 int err = 0;
Paul Moore220deb92008-01-29 08:38:23 -05004956 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004957 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004958 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004959 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04004960 char *addrp;
4961
Eric Paris50c205f2012-04-04 15:01:43 -04004962 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004963 ad.u.net = &net;
4964 ad.u.net->netif = skb->skb_iif;
4965 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004966 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4967 if (err)
4968 return err;
Paul Moore220deb92008-01-29 08:38:23 -05004969
Paul Moore58bfbb52009-03-27 17:10:41 -04004970 if (selinux_secmark_enabled()) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004971 err = avc_has_perm(&selinux_state,
4972 sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Moored8395c82008-10-10 10:16:30 -04004973 PACKET__RECV, &ad);
Paul Moore58bfbb52009-03-27 17:10:41 -04004974 if (err)
4975 return err;
4976 }
Paul Moore220deb92008-01-29 08:38:23 -05004977
Steffen Klassertb9679a72011-02-23 12:55:21 +01004978 err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
4979 if (err)
4980 return err;
4981 err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004982
James Morris4e5ab4c2006-06-09 00:33:33 -07004983 return err;
4984}
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004985
James Morris4e5ab4c2006-06-09 00:33:33 -07004986static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
4987{
Paul Moore220deb92008-01-29 08:38:23 -05004988 int err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004989 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004990 u16 family = sk->sk_family;
4991 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004992 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004993 struct lsm_network_audit net = {0,};
Paul Moore220deb92008-01-29 08:38:23 -05004994 char *addrp;
Paul Moored8395c82008-10-10 10:16:30 -04004995 u8 secmark_active;
4996 u8 peerlbl_active;
James Morris4e5ab4c2006-06-09 00:33:33 -07004997
James Morris4e5ab4c2006-06-09 00:33:33 -07004998 if (family != PF_INET && family != PF_INET6)
Paul Moore220deb92008-01-29 08:38:23 -05004999 return 0;
James Morris4e5ab4c2006-06-09 00:33:33 -07005000
5001 /* Handle mapped IPv4 packets arriving via IPv6 sockets */
Al Viro87fcd702006-12-04 22:00:55 +00005002 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
James Morris4e5ab4c2006-06-09 00:33:33 -07005003 family = PF_INET;
5004
Paul Moored8395c82008-10-10 10:16:30 -04005005 /* If any sort of compatibility mode is enabled then handoff processing
5006 * to the selinux_sock_rcv_skb_compat() function to deal with the
5007 * special handling. We do this in an attempt to keep this function
5008 * as fast and as clean as possible. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005009 if (!selinux_policycap_netpeer())
Paul Moored8395c82008-10-10 10:16:30 -04005010 return selinux_sock_rcv_skb_compat(sk, skb, family);
5011
5012 secmark_active = selinux_secmark_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04005013 peerlbl_active = selinux_peerlbl_enabled();
Paul Moored8395c82008-10-10 10:16:30 -04005014 if (!secmark_active && !peerlbl_active)
5015 return 0;
5016
Eric Paris50c205f2012-04-04 15:01:43 -04005017 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005018 ad.u.net = &net;
5019 ad.u.net->netif = skb->skb_iif;
5020 ad.u.net->family = family;
Paul Moore224dfbd2008-01-29 08:38:13 -05005021 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
James Morris4e5ab4c2006-06-09 00:33:33 -07005022 if (err)
Paul Moore220deb92008-01-29 08:38:23 -05005023 return err;
James Morris4e5ab4c2006-06-09 00:33:33 -07005024
Paul Moored8395c82008-10-10 10:16:30 -04005025 if (peerlbl_active) {
Paul Moored621d352008-01-29 08:43:36 -05005026 u32 peer_sid;
5027
5028 err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
5029 if (err)
5030 return err;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005031 err = selinux_inet_sys_rcv_skb(sock_net(sk), skb->skb_iif,
5032 addrp, family, peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04005033 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04005034 selinux_netlbl_err(skb, family, err, 0);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005035 return err;
Paul Mooredfaebe92008-10-10 10:16:31 -04005036 }
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005037 err = avc_has_perm(&selinux_state,
5038 sk_sid, peer_sid, SECCLASS_PEER,
Paul Moored621d352008-01-29 08:43:36 -05005039 PEER__RECV, &ad);
Chad Hanson46d01d62013-12-23 17:45:01 -05005040 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04005041 selinux_netlbl_err(skb, family, err, 0);
Chad Hanson46d01d62013-12-23 17:45:01 -05005042 return err;
5043 }
Paul Moored621d352008-01-29 08:43:36 -05005044 }
5045
Paul Moored8395c82008-10-10 10:16:30 -04005046 if (secmark_active) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005047 err = avc_has_perm(&selinux_state,
5048 sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005049 PACKET__RECV, &ad);
5050 if (err)
5051 return err;
5052 }
5053
Paul Moored621d352008-01-29 08:43:36 -05005054 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055}
5056
Catherine Zhang2c7946a2006-03-20 22:41:23 -08005057static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
5058 int __user *optlen, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059{
5060 int err = 0;
5061 char *scontext;
5062 u32 scontext_len;
Paul Moore253bfae2010-04-22 14:46:19 -04005063 struct sk_security_struct *sksec = sock->sk->sk_security;
Paul Moore3de4bab2006-11-17 17:38:54 -05005064 u32 peer_sid = SECSID_NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065
Paul Moore253bfae2010-04-22 14:46:19 -04005066 if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
Richard Hainesd4529302018-02-13 20:57:18 +00005067 sksec->sclass == SECCLASS_TCP_SOCKET ||
5068 sksec->sclass == SECCLASS_SCTP_SOCKET)
Eric Parisdd3e7832010-04-07 15:08:46 -04005069 peer_sid = sksec->peer_sid;
Paul Moore253bfae2010-04-22 14:46:19 -04005070 if (peer_sid == SECSID_NULL)
5071 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005073 err = security_sid_to_context(&selinux_state, peer_sid, &scontext,
5074 &scontext_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075 if (err)
Paul Moore253bfae2010-04-22 14:46:19 -04005076 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077
5078 if (scontext_len > len) {
5079 err = -ERANGE;
5080 goto out_len;
5081 }
5082
5083 if (copy_to_user(optval, scontext, scontext_len))
5084 err = -EFAULT;
5085
5086out_len:
5087 if (put_user(scontext_len, optlen))
5088 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089 kfree(scontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090 return err;
5091}
5092
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005093static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
Catherine Zhang2c7946a2006-03-20 22:41:23 -08005094{
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005095 u32 peer_secid = SECSID_NULL;
Paul Moore75e22912008-01-29 08:38:04 -05005096 u16 family;
Paul Moore899134f2016-03-28 15:19:10 -04005097 struct inode_security_struct *isec;
Catherine Zhang877ce7c2006-06-29 12:27:47 -07005098
Paul Mooreaa862902008-10-10 10:16:29 -04005099 if (skb && skb->protocol == htons(ETH_P_IP))
5100 family = PF_INET;
5101 else if (skb && skb->protocol == htons(ETH_P_IPV6))
5102 family = PF_INET6;
5103 else if (sock)
Paul Moore75e22912008-01-29 08:38:04 -05005104 family = sock->sk->sk_family;
Paul Moore75e22912008-01-29 08:38:04 -05005105 else
5106 goto out;
5107
Paul Moore899134f2016-03-28 15:19:10 -04005108 if (sock && family == PF_UNIX) {
5109 isec = inode_security_novalidate(SOCK_INODE(sock));
5110 peer_secid = isec->sid;
5111 } else if (skb)
Paul Moore220deb92008-01-29 08:38:23 -05005112 selinux_skb_peerlbl_sid(skb, family, &peer_secid);
Catherine Zhang2c7946a2006-03-20 22:41:23 -08005113
Paul Moore75e22912008-01-29 08:38:04 -05005114out:
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005115 *secid = peer_secid;
Paul Moore75e22912008-01-29 08:38:04 -05005116 if (peer_secid == SECSID_NULL)
5117 return -EINVAL;
5118 return 0;
Catherine Zhang2c7946a2006-03-20 22:41:23 -08005119}
5120
Al Viro7d877f32005-10-21 03:20:43 -04005121static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122{
Paul Moore84914b72010-04-22 14:46:18 -04005123 struct sk_security_struct *sksec;
5124
5125 sksec = kzalloc(sizeof(*sksec), priority);
5126 if (!sksec)
5127 return -ENOMEM;
5128
5129 sksec->peer_sid = SECINITSID_UNLABELED;
5130 sksec->sid = SECINITSID_UNLABELED;
Stephen Smalley5dee25d2015-07-10 17:19:57 -04005131 sksec->sclass = SECCLASS_SOCKET;
Paul Moore84914b72010-04-22 14:46:18 -04005132 selinux_netlbl_sk_security_reset(sksec);
5133 sk->sk_security = sksec;
5134
5135 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136}
5137
5138static void selinux_sk_free_security(struct sock *sk)
5139{
Paul Moore84914b72010-04-22 14:46:18 -04005140 struct sk_security_struct *sksec = sk->sk_security;
5141
5142 sk->sk_security = NULL;
5143 selinux_netlbl_sk_security_free(sksec);
5144 kfree(sksec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145}
5146
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005147static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
5148{
Eric Parisdd3e7832010-04-07 15:08:46 -04005149 struct sk_security_struct *sksec = sk->sk_security;
5150 struct sk_security_struct *newsksec = newsk->sk_security;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005151
Eric Parisdd3e7832010-04-07 15:08:46 -04005152 newsksec->sid = sksec->sid;
5153 newsksec->peer_sid = sksec->peer_sid;
5154 newsksec->sclass = sksec->sclass;
Paul Moore99f59ed2006-08-29 17:53:48 -07005155
Eric Parisdd3e7832010-04-07 15:08:46 -04005156 selinux_netlbl_sk_security_reset(newsksec);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005157}
5158
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07005159static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005160{
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005161 if (!sk)
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07005162 *secid = SECINITSID_ANY_SOCKET;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005163 else {
5164 struct sk_security_struct *sksec = sk->sk_security;
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005165
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07005166 *secid = sksec->sid;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005167 }
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005168}
5169
Eric Paris828dfe12008-04-17 13:17:49 -04005170static void selinux_sock_graft(struct sock *sk, struct socket *parent)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005171{
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05005172 struct inode_security_struct *isec =
5173 inode_security_novalidate(SOCK_INODE(parent));
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005174 struct sk_security_struct *sksec = sk->sk_security;
5175
Paul Moore2873ead2014-07-28 10:42:48 -04005176 if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
5177 sk->sk_family == PF_UNIX)
David Woodhouse2148ccc2006-09-29 15:50:25 -07005178 isec->sid = sksec->sid;
Paul Moore220deb92008-01-29 08:38:23 -05005179 sksec->sclass = isec->sclass;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005180}
5181
Richard Hainesd4529302018-02-13 20:57:18 +00005182/* Called whenever SCTP receives an INIT chunk. This happens when an incoming
5183 * connect(2), sctp_connectx(3) or sctp_sendmsg(3) (with no association
5184 * already present).
5185 */
5186static int selinux_sctp_assoc_request(struct sctp_endpoint *ep,
5187 struct sk_buff *skb)
5188{
5189 struct sk_security_struct *sksec = ep->base.sk->sk_security;
5190 struct common_audit_data ad;
5191 struct lsm_network_audit net = {0,};
5192 u8 peerlbl_active;
5193 u32 peer_sid = SECINITSID_UNLABELED;
5194 u32 conn_sid;
5195 int err = 0;
5196
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005197 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005198 return 0;
5199
5200 peerlbl_active = selinux_peerlbl_enabled();
5201
5202 if (peerlbl_active) {
5203 /* This will return peer_sid = SECSID_NULL if there are
5204 * no peer labels, see security_net_peersid_resolve().
5205 */
5206 err = selinux_skb_peerlbl_sid(skb, ep->base.sk->sk_family,
5207 &peer_sid);
5208 if (err)
5209 return err;
5210
5211 if (peer_sid == SECSID_NULL)
5212 peer_sid = SECINITSID_UNLABELED;
5213 }
5214
5215 if (sksec->sctp_assoc_state == SCTP_ASSOC_UNSET) {
5216 sksec->sctp_assoc_state = SCTP_ASSOC_SET;
5217
5218 /* Here as first association on socket. As the peer SID
5219 * was allowed by peer recv (and the netif/node checks),
5220 * then it is approved by policy and used as the primary
5221 * peer SID for getpeercon(3).
5222 */
5223 sksec->peer_sid = peer_sid;
5224 } else if (sksec->peer_sid != peer_sid) {
5225 /* Other association peer SIDs are checked to enforce
5226 * consistency among the peer SIDs.
5227 */
5228 ad.type = LSM_AUDIT_DATA_NET;
5229 ad.u.net = &net;
5230 ad.u.net->sk = ep->base.sk;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005231 err = avc_has_perm(&selinux_state,
5232 sksec->peer_sid, peer_sid, sksec->sclass,
Richard Hainesd4529302018-02-13 20:57:18 +00005233 SCTP_SOCKET__ASSOCIATION, &ad);
5234 if (err)
5235 return err;
5236 }
5237
5238 /* Compute the MLS component for the connection and store
5239 * the information in ep. This will be used by SCTP TCP type
5240 * sockets and peeled off connections as they cause a new
5241 * socket to be generated. selinux_sctp_sk_clone() will then
5242 * plug this into the new socket.
5243 */
5244 err = selinux_conn_sid(sksec->sid, peer_sid, &conn_sid);
5245 if (err)
5246 return err;
5247
5248 ep->secid = conn_sid;
5249 ep->peer_secid = peer_sid;
5250
5251 /* Set any NetLabel labels including CIPSO/CALIPSO options. */
5252 return selinux_netlbl_sctp_assoc_request(ep, skb);
5253}
5254
5255/* Check if sctp IPv4/IPv6 addresses are valid for binding or connecting
5256 * based on their @optname.
5257 */
5258static int selinux_sctp_bind_connect(struct sock *sk, int optname,
5259 struct sockaddr *address,
5260 int addrlen)
5261{
5262 int len, err = 0, walk_size = 0;
5263 void *addr_buf;
5264 struct sockaddr *addr;
5265 struct socket *sock;
5266
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005267 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005268 return 0;
5269
5270 /* Process one or more addresses that may be IPv4 or IPv6 */
5271 sock = sk->sk_socket;
5272 addr_buf = address;
5273
5274 while (walk_size < addrlen) {
5275 addr = addr_buf;
5276 switch (addr->sa_family) {
Alexey Kodanev4152dc92018-05-11 20:15:13 +03005277 case AF_UNSPEC:
Richard Hainesd4529302018-02-13 20:57:18 +00005278 case AF_INET:
5279 len = sizeof(struct sockaddr_in);
5280 break;
5281 case AF_INET6:
5282 len = sizeof(struct sockaddr_in6);
5283 break;
5284 default:
Alexey Kodanev4152dc92018-05-11 20:15:13 +03005285 return -EINVAL;
Richard Hainesd4529302018-02-13 20:57:18 +00005286 }
5287
5288 err = -EINVAL;
5289 switch (optname) {
5290 /* Bind checks */
5291 case SCTP_PRIMARY_ADDR:
5292 case SCTP_SET_PEER_PRIMARY_ADDR:
5293 case SCTP_SOCKOPT_BINDX_ADD:
5294 err = selinux_socket_bind(sock, addr, len);
5295 break;
5296 /* Connect checks */
5297 case SCTP_SOCKOPT_CONNECTX:
5298 case SCTP_PARAM_SET_PRIMARY:
5299 case SCTP_PARAM_ADD_IP:
5300 case SCTP_SENDMSG_CONNECT:
5301 err = selinux_socket_connect_helper(sock, addr, len);
5302 if (err)
5303 return err;
5304
5305 /* As selinux_sctp_bind_connect() is called by the
5306 * SCTP protocol layer, the socket is already locked,
5307 * therefore selinux_netlbl_socket_connect_locked() is
5308 * is called here. The situations handled are:
5309 * sctp_connectx(3), sctp_sendmsg(3), sendmsg(2),
5310 * whenever a new IP address is added or when a new
5311 * primary address is selected.
5312 * Note that an SCTP connect(2) call happens before
5313 * the SCTP protocol layer and is handled via
5314 * selinux_socket_connect().
5315 */
5316 err = selinux_netlbl_socket_connect_locked(sk, addr);
5317 break;
5318 }
5319
5320 if (err)
5321 return err;
5322
5323 addr_buf += len;
5324 walk_size += len;
5325 }
5326
5327 return 0;
5328}
5329
5330/* Called whenever a new socket is created by accept(2) or sctp_peeloff(3). */
5331static void selinux_sctp_sk_clone(struct sctp_endpoint *ep, struct sock *sk,
5332 struct sock *newsk)
5333{
5334 struct sk_security_struct *sksec = sk->sk_security;
5335 struct sk_security_struct *newsksec = newsk->sk_security;
5336
5337 /* If policy does not support SECCLASS_SCTP_SOCKET then call
5338 * the non-sctp clone version.
5339 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005340 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005341 return selinux_sk_clone_security(sk, newsk);
5342
5343 newsksec->sid = ep->secid;
5344 newsksec->peer_sid = ep->peer_secid;
5345 newsksec->sclass = sksec->sclass;
5346 selinux_netlbl_sctp_sk_clone(sk, newsk);
5347}
5348
Adrian Bunk9a673e52006-08-15 00:03:53 -07005349static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
5350 struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005351{
5352 struct sk_security_struct *sksec = sk->sk_security;
5353 int err;
Paul Moore0b1f24e2013-12-03 11:39:13 -05005354 u16 family = req->rsk_ops->family;
Paul Moore446b8022013-12-04 16:10:51 -05005355 u32 connsid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005356 u32 peersid;
5357
Paul Mooreaa862902008-10-10 10:16:29 -04005358 err = selinux_skb_peerlbl_sid(skb, family, &peersid);
Paul Moore220deb92008-01-29 08:38:23 -05005359 if (err)
5360 return err;
Paul Moore446b8022013-12-04 16:10:51 -05005361 err = selinux_conn_sid(sksec->sid, peersid, &connsid);
5362 if (err)
5363 return err;
5364 req->secid = connsid;
5365 req->peer_secid = peersid;
Venkat Yekkiralaa51c64f2006-07-27 22:01:34 -07005366
Paul Moore389fb8002009-03-27 17:10:34 -04005367 return selinux_netlbl_inet_conn_request(req, family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005368}
5369
Adrian Bunk9a673e52006-08-15 00:03:53 -07005370static void selinux_inet_csk_clone(struct sock *newsk,
5371 const struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005372{
5373 struct sk_security_struct *newsksec = newsk->sk_security;
5374
5375 newsksec->sid = req->secid;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005376 newsksec->peer_sid = req->peer_secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005377 /* NOTE: Ideally, we should also get the isec->sid for the
5378 new socket in sync, but we don't have the isec available yet.
5379 So we will wait until sock_graft to do it, by which
5380 time it will have been created and available. */
Paul Moore99f59ed2006-08-29 17:53:48 -07005381
Paul Moore9f2ad662006-11-17 17:38:53 -05005382 /* We don't need to take any sort of lock here as we are the only
5383 * thread with access to newsksec */
Paul Moore389fb8002009-03-27 17:10:34 -04005384 selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005385}
5386
Paul Moore014ab192008-10-10 10:16:33 -04005387static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005388{
Paul Mooreaa862902008-10-10 10:16:29 -04005389 u16 family = sk->sk_family;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005390 struct sk_security_struct *sksec = sk->sk_security;
5391
Paul Mooreaa862902008-10-10 10:16:29 -04005392 /* handle mapped IPv4 packets arriving via IPv6 sockets */
5393 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
5394 family = PF_INET;
5395
5396 selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005397}
5398
Eric Paris2606fd12010-10-13 16:24:41 -04005399static int selinux_secmark_relabel_packet(u32 sid)
5400{
5401 const struct task_security_struct *__tsec;
5402 u32 tsid;
5403
5404 __tsec = current_security();
5405 tsid = __tsec->sid;
5406
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005407 return avc_has_perm(&selinux_state,
5408 tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO,
5409 NULL);
Eric Paris2606fd12010-10-13 16:24:41 -04005410}
5411
5412static void selinux_secmark_refcount_inc(void)
5413{
5414 atomic_inc(&selinux_secmark_refcount);
5415}
5416
5417static void selinux_secmark_refcount_dec(void)
5418{
5419 atomic_dec(&selinux_secmark_refcount);
5420}
5421
Adrian Bunk9a673e52006-08-15 00:03:53 -07005422static void selinux_req_classify_flow(const struct request_sock *req,
5423 struct flowi *fl)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005424{
David S. Miller1d28f422011-03-12 00:29:39 -05005425 fl->flowi_secid = req->secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005426}
5427
Paul Moore5dbbaf22013-01-14 07:12:19 +00005428static int selinux_tun_dev_alloc_security(void **security)
5429{
5430 struct tun_security_struct *tunsec;
5431
5432 tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL);
5433 if (!tunsec)
5434 return -ENOMEM;
5435 tunsec->sid = current_sid();
5436
5437 *security = tunsec;
5438 return 0;
5439}
5440
5441static void selinux_tun_dev_free_security(void *security)
5442{
5443 kfree(security);
5444}
5445
Paul Mooreed6d76e2009-08-28 18:12:49 -04005446static int selinux_tun_dev_create(void)
5447{
5448 u32 sid = current_sid();
5449
5450 /* we aren't taking into account the "sockcreate" SID since the socket
5451 * that is being created here is not a socket in the traditional sense,
5452 * instead it is a private sock, accessible only to the kernel, and
5453 * representing a wide range of network traffic spanning multiple
5454 * connections unlike traditional sockets - check the TUN driver to
5455 * get a better understanding of why this socket is special */
5456
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005457 return avc_has_perm(&selinux_state,
5458 sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005459 NULL);
5460}
5461
Paul Moore5dbbaf22013-01-14 07:12:19 +00005462static int selinux_tun_dev_attach_queue(void *security)
Paul Mooreed6d76e2009-08-28 18:12:49 -04005463{
Paul Moore5dbbaf22013-01-14 07:12:19 +00005464 struct tun_security_struct *tunsec = security;
5465
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005466 return avc_has_perm(&selinux_state,
5467 current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET,
Paul Moore5dbbaf22013-01-14 07:12:19 +00005468 TUN_SOCKET__ATTACH_QUEUE, NULL);
5469}
5470
5471static int selinux_tun_dev_attach(struct sock *sk, void *security)
5472{
5473 struct tun_security_struct *tunsec = security;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005474 struct sk_security_struct *sksec = sk->sk_security;
5475
5476 /* we don't currently perform any NetLabel based labeling here and it
5477 * isn't clear that we would want to do so anyway; while we could apply
5478 * labeling without the support of the TUN user the resulting labeled
5479 * traffic from the other end of the connection would almost certainly
5480 * cause confusion to the TUN user that had no idea network labeling
5481 * protocols were being used */
5482
Paul Moore5dbbaf22013-01-14 07:12:19 +00005483 sksec->sid = tunsec->sid;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005484 sksec->sclass = SECCLASS_TUN_SOCKET;
Paul Moore5dbbaf22013-01-14 07:12:19 +00005485
5486 return 0;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005487}
5488
Paul Moore5dbbaf22013-01-14 07:12:19 +00005489static int selinux_tun_dev_open(void *security)
Paul Mooreed6d76e2009-08-28 18:12:49 -04005490{
Paul Moore5dbbaf22013-01-14 07:12:19 +00005491 struct tun_security_struct *tunsec = security;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005492 u32 sid = current_sid();
5493 int err;
5494
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005495 err = avc_has_perm(&selinux_state,
5496 sid, tunsec->sid, SECCLASS_TUN_SOCKET,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005497 TUN_SOCKET__RELABELFROM, NULL);
5498 if (err)
5499 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005500 err = avc_has_perm(&selinux_state,
5501 sid, sid, SECCLASS_TUN_SOCKET,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005502 TUN_SOCKET__RELABELTO, NULL);
5503 if (err)
5504 return err;
Paul Moore5dbbaf22013-01-14 07:12:19 +00005505 tunsec->sid = sid;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005506
5507 return 0;
5508}
5509
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
5511{
5512 int err = 0;
5513 u32 perm;
5514 struct nlmsghdr *nlh;
Paul Moore253bfae2010-04-22 14:46:19 -04005515 struct sk_security_struct *sksec = sk->sk_security;
Eric Paris828dfe12008-04-17 13:17:49 -04005516
Hong zhi guo77954982013-03-27 06:49:35 +00005517 if (skb->len < NLMSG_HDRLEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518 err = -EINVAL;
5519 goto out;
5520 }
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -07005521 nlh = nlmsg_hdr(skb);
Eric Paris828dfe12008-04-17 13:17:49 -04005522
Paul Moore253bfae2010-04-22 14:46:19 -04005523 err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524 if (err) {
5525 if (err == -EINVAL) {
Vladis Dronov76319942015-12-24 11:09:41 -05005526 pr_warn_ratelimited("SELinux: unrecognized netlink"
5527 " message: protocol=%hu nlmsg_type=%hu sclass=%s"
5528 " pig=%d comm=%s\n",
Marek Milkoviccded3ff2015-06-04 16:22:16 -04005529 sk->sk_protocol, nlh->nlmsg_type,
Vladis Dronov76319942015-12-24 11:09:41 -05005530 secclass_map[sksec->sclass - 1].name,
5531 task_pid_nr(current), current->comm);
Paul Mooree5a5ca92018-03-01 17:38:30 -05005532 if (!enforcing_enabled(&selinux_state) ||
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005533 security_get_allow_unknown(&selinux_state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534 err = 0;
5535 }
5536
5537 /* Ignore */
5538 if (err == -ENOENT)
5539 err = 0;
5540 goto out;
5541 }
5542
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005543 err = sock_has_perm(sk, perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544out:
5545 return err;
5546}
5547
5548#ifdef CONFIG_NETFILTER
5549
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005550static unsigned int selinux_ip_forward(struct sk_buff *skb,
5551 const struct net_device *indev,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005552 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553{
Paul Mooredfaebe92008-10-10 10:16:31 -04005554 int err;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005555 char *addrp;
5556 u32 peer_sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04005557 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005558 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05005559 u8 secmark_active;
Paul Moore948bf852008-10-10 10:16:32 -04005560 u8 netlbl_active;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005561 u8 peerlbl_active;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005562
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005563 if (!selinux_policycap_netpeer())
Paul Mooreeffad8d2008-01-29 08:49:27 -05005564 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005565
Paul Mooreeffad8d2008-01-29 08:49:27 -05005566 secmark_active = selinux_secmark_enabled();
Paul Moore948bf852008-10-10 10:16:32 -04005567 netlbl_active = netlbl_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04005568 peerlbl_active = selinux_peerlbl_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05005569 if (!secmark_active && !peerlbl_active)
5570 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005571
Paul Moored8395c82008-10-10 10:16:30 -04005572 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
5573 return NF_DROP;
5574
Eric Paris50c205f2012-04-04 15:01:43 -04005575 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005576 ad.u.net = &net;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005577 ad.u.net->netif = indev->ifindex;
Eric Paris48c62af2012-04-02 13:15:44 -04005578 ad.u.net->family = family;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005579 if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
5580 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581
Paul Mooredfaebe92008-10-10 10:16:31 -04005582 if (peerlbl_active) {
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005583 err = selinux_inet_sys_rcv_skb(dev_net(indev), indev->ifindex,
5584 addrp, family, peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04005585 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04005586 selinux_netlbl_err(skb, family, err, 1);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005587 return NF_DROP;
Paul Mooredfaebe92008-10-10 10:16:31 -04005588 }
5589 }
Paul Mooreeffad8d2008-01-29 08:49:27 -05005590
5591 if (secmark_active)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005592 if (avc_has_perm(&selinux_state,
5593 peer_sid, skb->secmark,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005594 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
5595 return NF_DROP;
5596
Paul Moore948bf852008-10-10 10:16:32 -04005597 if (netlbl_active)
5598 /* we do this in the FORWARD path and not the POST_ROUTING
5599 * path because we want to make sure we apply the necessary
5600 * labeling before IPsec is applied so we can leverage AH
5601 * protection */
5602 if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
5603 return NF_DROP;
5604
Paul Mooreeffad8d2008-01-29 08:49:27 -05005605 return NF_ACCEPT;
5606}
5607
Eric W. Biederman06198b32015-09-18 14:33:06 -05005608static unsigned int selinux_ipv4_forward(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005609 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005610 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005611{
David S. Miller238e54c2015-04-03 20:32:56 -04005612 return selinux_ip_forward(skb, state->in, PF_INET);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005613}
5614
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005615#if IS_ENABLED(CONFIG_IPV6)
Eric W. Biederman06198b32015-09-18 14:33:06 -05005616static unsigned int selinux_ipv6_forward(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005617 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005618 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005619{
David S. Miller238e54c2015-04-03 20:32:56 -04005620 return selinux_ip_forward(skb, state->in, PF_INET6);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005621}
5622#endif /* IPV6 */
5623
Paul Moore948bf852008-10-10 10:16:32 -04005624static unsigned int selinux_ip_output(struct sk_buff *skb,
5625 u16 family)
5626{
Paul Moore47180062013-12-04 16:10:45 -05005627 struct sock *sk;
Paul Moore948bf852008-10-10 10:16:32 -04005628 u32 sid;
5629
5630 if (!netlbl_enabled())
5631 return NF_ACCEPT;
5632
5633 /* we do this in the LOCAL_OUT path and not the POST_ROUTING path
5634 * because we want to make sure we apply the necessary labeling
5635 * before IPsec is applied so we can leverage AH protection */
Paul Moore47180062013-12-04 16:10:45 -05005636 sk = skb->sk;
5637 if (sk) {
5638 struct sk_security_struct *sksec;
5639
Eric Dumazete446f9d2015-10-08 05:01:55 -07005640 if (sk_listener(sk))
Paul Moore47180062013-12-04 16:10:45 -05005641 /* if the socket is the listening state then this
5642 * packet is a SYN-ACK packet which means it needs to
5643 * be labeled based on the connection/request_sock and
5644 * not the parent socket. unfortunately, we can't
5645 * lookup the request_sock yet as it isn't queued on
5646 * the parent socket until after the SYN-ACK is sent.
5647 * the "solution" is to simply pass the packet as-is
5648 * as any IP option based labeling should be copied
5649 * from the initial connection request (in the IP
5650 * layer). it is far from ideal, but until we get a
5651 * security label in the packet itself this is the
5652 * best we can do. */
5653 return NF_ACCEPT;
5654
5655 /* standard practice, label using the parent socket */
5656 sksec = sk->sk_security;
Paul Moore948bf852008-10-10 10:16:32 -04005657 sid = sksec->sid;
5658 } else
5659 sid = SECINITSID_KERNEL;
5660 if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
5661 return NF_DROP;
5662
5663 return NF_ACCEPT;
5664}
5665
Eric W. Biederman06198b32015-09-18 14:33:06 -05005666static unsigned int selinux_ipv4_output(void *priv,
Paul Moore948bf852008-10-10 10:16:32 -04005667 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005668 const struct nf_hook_state *state)
Paul Moore948bf852008-10-10 10:16:32 -04005669{
5670 return selinux_ip_output(skb, PF_INET);
5671}
5672
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005673#if IS_ENABLED(CONFIG_IPV6)
Huw Davies2917f572016-06-27 15:06:15 -04005674static unsigned int selinux_ipv6_output(void *priv,
5675 struct sk_buff *skb,
5676 const struct nf_hook_state *state)
5677{
5678 return selinux_ip_output(skb, PF_INET6);
5679}
5680#endif /* IPV6 */
5681
Paul Mooreeffad8d2008-01-29 08:49:27 -05005682static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
5683 int ifindex,
Paul Moored8395c82008-10-10 10:16:30 -04005684 u16 family)
James Morris4e5ab4c2006-06-09 00:33:33 -07005685{
Eric Dumazet54abc682015-11-08 10:54:07 -08005686 struct sock *sk = skb_to_full_sk(skb);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005687 struct sk_security_struct *sksec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005688 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005689 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04005690 char *addrp;
5691 u8 proto;
James Morris4e5ab4c2006-06-09 00:33:33 -07005692
Paul Mooreeffad8d2008-01-29 08:49:27 -05005693 if (sk == NULL)
5694 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005695 sksec = sk->sk_security;
James Morris4e5ab4c2006-06-09 00:33:33 -07005696
Eric Paris50c205f2012-04-04 15:01:43 -04005697 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005698 ad.u.net = &net;
5699 ad.u.net->netif = ifindex;
5700 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04005701 if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
5702 return NF_DROP;
5703
Paul Moore58bfbb52009-03-27 17:10:41 -04005704 if (selinux_secmark_enabled())
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005705 if (avc_has_perm(&selinux_state,
5706 sksec->sid, skb->secmark,
Paul Moored8395c82008-10-10 10:16:30 -04005707 SECCLASS_PACKET, PACKET__SEND, &ad))
Eric Paris2fe66ec2010-11-23 06:28:08 +00005708 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07005709
Steffen Klassertb9679a72011-02-23 12:55:21 +01005710 if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
5711 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07005712
Paul Mooreeffad8d2008-01-29 08:49:27 -05005713 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714}
5715
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005716static unsigned int selinux_ip_postroute(struct sk_buff *skb,
5717 const struct net_device *outdev,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005718 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719{
Paul Mooreeffad8d2008-01-29 08:49:27 -05005720 u32 secmark_perm;
5721 u32 peer_sid;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005722 int ifindex = outdev->ifindex;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005723 struct sock *sk;
Thomas Liu2bf49692009-07-14 12:14:09 -04005724 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005725 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05005726 char *addrp;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005727 u8 secmark_active;
5728 u8 peerlbl_active;
5729
Paul Mooreeffad8d2008-01-29 08:49:27 -05005730 /* If any sort of compatibility mode is enabled then handoff processing
5731 * to the selinux_ip_postroute_compat() function to deal with the
5732 * special handling. We do this in an attempt to keep this function
5733 * as fast and as clean as possible. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005734 if (!selinux_policycap_netpeer())
Paul Moored8395c82008-10-10 10:16:30 -04005735 return selinux_ip_postroute_compat(skb, ifindex, family);
Paul Moorec0828e52013-12-10 14:58:01 -05005736
Paul Mooreeffad8d2008-01-29 08:49:27 -05005737 secmark_active = selinux_secmark_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04005738 peerlbl_active = selinux_peerlbl_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05005739 if (!secmark_active && !peerlbl_active)
5740 return NF_ACCEPT;
5741
Eric Dumazet54abc682015-11-08 10:54:07 -08005742 sk = skb_to_full_sk(skb);
Paul Moorec0828e52013-12-10 14:58:01 -05005743
Paul Mooreeffad8d2008-01-29 08:49:27 -05005744#ifdef CONFIG_XFRM
5745 /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
5746 * packet transformation so allow the packet to pass without any checks
5747 * since we'll have another chance to perform access control checks
5748 * when the packet is on it's final way out.
5749 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
Paul Moorec0828e52013-12-10 14:58:01 -05005750 * is NULL, in this case go ahead and apply access control.
5751 * NOTE: if this is a local socket (skb->sk != NULL) that is in the
5752 * TCP listening state we cannot wait until the XFRM processing
5753 * is done as we will miss out on the SA label if we do;
5754 * unfortunately, this means more work, but it is only once per
5755 * connection. */
5756 if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL &&
Eric Dumazete446f9d2015-10-08 05:01:55 -07005757 !(sk && sk_listener(sk)))
Paul Mooreeffad8d2008-01-29 08:49:27 -05005758 return NF_ACCEPT;
5759#endif
Paul Mooreeffad8d2008-01-29 08:49:27 -05005760
Paul Moored8395c82008-10-10 10:16:30 -04005761 if (sk == NULL) {
Paul Moore446b8022013-12-04 16:10:51 -05005762 /* Without an associated socket the packet is either coming
5763 * from the kernel or it is being forwarded; check the packet
5764 * to determine which and if the packet is being forwarded
5765 * query the packet directly to determine the security label. */
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005766 if (skb->skb_iif) {
5767 secmark_perm = PACKET__FORWARD_OUT;
Paul Moored8395c82008-10-10 10:16:30 -04005768 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005769 return NF_DROP;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005770 } else {
5771 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04005772 peer_sid = SECINITSID_KERNEL;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005773 }
Eric Dumazete446f9d2015-10-08 05:01:55 -07005774 } else if (sk_listener(sk)) {
Paul Moore446b8022013-12-04 16:10:51 -05005775 /* Locally generated packet but the associated socket is in the
5776 * listening state which means this is a SYN-ACK packet. In
5777 * this particular case the correct security label is assigned
5778 * to the connection/request_sock but unfortunately we can't
5779 * query the request_sock as it isn't queued on the parent
5780 * socket until after the SYN-ACK packet is sent; the only
5781 * viable choice is to regenerate the label like we do in
5782 * selinux_inet_conn_request(). See also selinux_ip_output()
5783 * for similar problems. */
5784 u32 skb_sid;
Eric Dumazete446f9d2015-10-08 05:01:55 -07005785 struct sk_security_struct *sksec;
5786
Eric Dumazete446f9d2015-10-08 05:01:55 -07005787 sksec = sk->sk_security;
Paul Moore446b8022013-12-04 16:10:51 -05005788 if (selinux_skb_peerlbl_sid(skb, family, &skb_sid))
5789 return NF_DROP;
Paul Moorec0828e52013-12-10 14:58:01 -05005790 /* At this point, if the returned skb peerlbl is SECSID_NULL
5791 * and the packet has been through at least one XFRM
5792 * transformation then we must be dealing with the "final"
5793 * form of labeled IPsec packet; since we've already applied
5794 * all of our access controls on this packet we can safely
5795 * pass the packet. */
5796 if (skb_sid == SECSID_NULL) {
5797 switch (family) {
5798 case PF_INET:
5799 if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
5800 return NF_ACCEPT;
5801 break;
5802 case PF_INET6:
5803 if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED)
5804 return NF_ACCEPT;
Paul Moorea7a91a12014-09-03 10:51:59 -04005805 break;
Paul Moorec0828e52013-12-10 14:58:01 -05005806 default:
5807 return NF_DROP_ERR(-ECONNREFUSED);
5808 }
5809 }
Paul Moore446b8022013-12-04 16:10:51 -05005810 if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid))
5811 return NF_DROP;
5812 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04005813 } else {
Paul Moore446b8022013-12-04 16:10:51 -05005814 /* Locally generated packet, fetch the security label from the
5815 * associated socket. */
Paul Mooreeffad8d2008-01-29 08:49:27 -05005816 struct sk_security_struct *sksec = sk->sk_security;
5817 peer_sid = sksec->sid;
5818 secmark_perm = PACKET__SEND;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005819 }
5820
Eric Paris50c205f2012-04-04 15:01:43 -04005821 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005822 ad.u.net = &net;
5823 ad.u.net->netif = ifindex;
5824 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04005825 if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
Eric Paris04f6d702010-11-23 06:28:02 +00005826 return NF_DROP;
Paul Moored8395c82008-10-10 10:16:30 -04005827
Paul Mooreeffad8d2008-01-29 08:49:27 -05005828 if (secmark_active)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005829 if (avc_has_perm(&selinux_state,
5830 peer_sid, skb->secmark,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005831 SECCLASS_PACKET, secmark_perm, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005832 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005833
5834 if (peerlbl_active) {
5835 u32 if_sid;
5836 u32 node_sid;
5837
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005838 if (sel_netif_sid(dev_net(outdev), ifindex, &if_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005839 return NF_DROP;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005840 if (avc_has_perm(&selinux_state,
5841 peer_sid, if_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005842 SECCLASS_NETIF, NETIF__EGRESS, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005843 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005844
5845 if (sel_netnode_sid(addrp, family, &node_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005846 return NF_DROP;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005847 if (avc_has_perm(&selinux_state,
5848 peer_sid, node_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005849 SECCLASS_NODE, NODE__SENDTO, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005850 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005851 }
5852
5853 return NF_ACCEPT;
5854}
5855
Eric W. Biederman06198b32015-09-18 14:33:06 -05005856static unsigned int selinux_ipv4_postroute(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005857 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005858 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005859{
David S. Miller238e54c2015-04-03 20:32:56 -04005860 return selinux_ip_postroute(skb, state->out, PF_INET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005861}
5862
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005863#if IS_ENABLED(CONFIG_IPV6)
Eric W. Biederman06198b32015-09-18 14:33:06 -05005864static unsigned int selinux_ipv6_postroute(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005865 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005866 const struct nf_hook_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867{
David S. Miller238e54c2015-04-03 20:32:56 -04005868 return selinux_ip_postroute(skb, state->out, PF_INET6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870#endif /* IPV6 */
5871
5872#endif /* CONFIG_NETFILTER */
5873
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
5875{
Stephen Smalley941fc5b2009-10-01 14:48:23 -04005876 return selinux_nlmsg_perm(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877}
5878
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005879static int ipc_alloc_security(struct kern_ipc_perm *perm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880 u16 sclass)
5881{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882 struct ipc_security_struct *isec;
5883
James Morris89d155e2005-10-30 14:59:21 -08005884 isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885 if (!isec)
5886 return -ENOMEM;
5887
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888 isec->sclass = sclass;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005889 isec->sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890 perm->security = isec;
5891
5892 return 0;
5893}
5894
5895static void ipc_free_security(struct kern_ipc_perm *perm)
5896{
5897 struct ipc_security_struct *isec = perm->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898 perm->security = NULL;
5899 kfree(isec);
5900}
5901
5902static int msg_msg_alloc_security(struct msg_msg *msg)
5903{
5904 struct msg_security_struct *msec;
5905
James Morris89d155e2005-10-30 14:59:21 -08005906 msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005907 if (!msec)
5908 return -ENOMEM;
5909
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910 msec->sid = SECINITSID_UNLABELED;
5911 msg->security = msec;
5912
5913 return 0;
5914}
5915
5916static void msg_msg_free_security(struct msg_msg *msg)
5917{
5918 struct msg_security_struct *msec = msg->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919
5920 msg->security = NULL;
5921 kfree(msec);
5922}
5923
5924static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
Stephen Smalley6af963f2005-05-01 08:58:39 -07005925 u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005926{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005927 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005928 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005929 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005930
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931 isec = ipc_perms->security;
5932
Eric Paris50c205f2012-04-04 15:01:43 -04005933 ad.type = LSM_AUDIT_DATA_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934 ad.u.ipc_id = ipc_perms->key;
5935
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005936 return avc_has_perm(&selinux_state,
5937 sid, isec->sid, isec->sclass, perms, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938}
5939
5940static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
5941{
5942 return msg_msg_alloc_security(msg);
5943}
5944
5945static void selinux_msg_msg_free_security(struct msg_msg *msg)
5946{
5947 msg_msg_free_security(msg);
5948}
5949
5950/* message queue security operations */
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005951static int selinux_msg_queue_alloc_security(struct kern_ipc_perm *msq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005953 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005954 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005955 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005956 int rc;
5957
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005958 rc = ipc_alloc_security(msq, SECCLASS_MSGQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005959 if (rc)
5960 return rc;
5961
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005962 isec = msq->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005963
Eric Paris50c205f2012-04-04 15:01:43 -04005964 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005965 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005967 rc = avc_has_perm(&selinux_state,
5968 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005969 MSGQ__CREATE, &ad);
5970 if (rc) {
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005971 ipc_free_security(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005972 return rc;
5973 }
5974 return 0;
5975}
5976
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005977static void selinux_msg_queue_free_security(struct kern_ipc_perm *msq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005978{
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005979 ipc_free_security(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005980}
5981
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005982static int selinux_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005983{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005985 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005986 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005988 isec = msq->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005989
Eric Paris50c205f2012-04-04 15:01:43 -04005990 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005991 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005992
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005993 return avc_has_perm(&selinux_state,
5994 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005995 MSGQ__ASSOCIATE, &ad);
5996}
5997
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005998static int selinux_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999{
6000 int err;
6001 int perms;
6002
Eric Paris828dfe12008-04-17 13:17:49 -04006003 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006004 case IPC_INFO:
6005 case MSG_INFO:
6006 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006007 return avc_has_perm(&selinux_state,
6008 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006009 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010 case IPC_STAT:
6011 case MSG_STAT:
Davidlohr Bueso23c8cec2018-04-10 16:35:30 -07006012 case MSG_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013 perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
6014 break;
6015 case IPC_SET:
6016 perms = MSGQ__SETATTR;
6017 break;
6018 case IPC_RMID:
6019 perms = MSGQ__DESTROY;
6020 break;
6021 default:
6022 return 0;
6023 }
6024
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05006025 err = ipc_has_perm(msq, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026 return err;
6027}
6028
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05006029static int selinux_msg_queue_msgsnd(struct kern_ipc_perm *msq, struct msg_msg *msg, int msqflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006031 struct ipc_security_struct *isec;
6032 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006033 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006034 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006035 int rc;
6036
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05006037 isec = msq->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038 msec = msg->security;
6039
6040 /*
6041 * First time through, need to assign label to the message
6042 */
6043 if (msec->sid == SECINITSID_UNLABELED) {
6044 /*
6045 * Compute new sid based on current process and
6046 * message queue this message will be stored in
6047 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006048 rc = security_transition_sid(&selinux_state, sid, isec->sid,
6049 SECCLASS_MSG, NULL, &msec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050 if (rc)
6051 return rc;
6052 }
6053
Eric Paris50c205f2012-04-04 15:01:43 -04006054 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05006055 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056
6057 /* Can this process write to the queue? */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006058 rc = avc_has_perm(&selinux_state,
6059 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006060 MSGQ__WRITE, &ad);
6061 if (!rc)
6062 /* Can this process send the message */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006063 rc = avc_has_perm(&selinux_state,
6064 sid, msec->sid, SECCLASS_MSG,
David Howells275bb412008-11-14 10:39:19 +11006065 MSG__SEND, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006066 if (!rc)
6067 /* Can the message be put in the queue? */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006068 rc = avc_has_perm(&selinux_state,
6069 msec->sid, isec->sid, SECCLASS_MSGQ,
David Howells275bb412008-11-14 10:39:19 +11006070 MSGQ__ENQUEUE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071
6072 return rc;
6073}
6074
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05006075static int selinux_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076 struct task_struct *target,
6077 long type, int mode)
6078{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079 struct ipc_security_struct *isec;
6080 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006081 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006082 u32 sid = task_sid(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006083 int rc;
6084
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05006085 isec = msq->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086 msec = msg->security;
6087
Eric Paris50c205f2012-04-04 15:01:43 -04006088 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05006089 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006090
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006091 rc = avc_has_perm(&selinux_state,
6092 sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093 SECCLASS_MSGQ, MSGQ__READ, &ad);
6094 if (!rc)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006095 rc = avc_has_perm(&selinux_state,
6096 sid, msec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097 SECCLASS_MSG, MSG__RECEIVE, &ad);
6098 return rc;
6099}
6100
6101/* Shared Memory security operations */
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006102static int selinux_shm_alloc_security(struct kern_ipc_perm *shp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006103{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006105 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006106 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107 int rc;
6108
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006109 rc = ipc_alloc_security(shp, SECCLASS_SHM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006110 if (rc)
6111 return rc;
6112
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006113 isec = shp->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006114
Eric Paris50c205f2012-04-04 15:01:43 -04006115 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006116 ad.u.ipc_id = shp->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006117
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006118 rc = avc_has_perm(&selinux_state,
6119 sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120 SHM__CREATE, &ad);
6121 if (rc) {
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006122 ipc_free_security(shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006123 return rc;
6124 }
6125 return 0;
6126}
6127
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006128static void selinux_shm_free_security(struct kern_ipc_perm *shp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006129{
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006130 ipc_free_security(shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131}
6132
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006133static int selinux_shm_associate(struct kern_ipc_perm *shp, int shmflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006134{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006135 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006136 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006137 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006138
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006139 isec = shp->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006140
Eric Paris50c205f2012-04-04 15:01:43 -04006141 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006142 ad.u.ipc_id = shp->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006143
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006144 return avc_has_perm(&selinux_state,
6145 sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006146 SHM__ASSOCIATE, &ad);
6147}
6148
6149/* Note, at this point, shp is locked down */
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006150static int selinux_shm_shmctl(struct kern_ipc_perm *shp, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006151{
6152 int perms;
6153 int err;
6154
Eric Paris828dfe12008-04-17 13:17:49 -04006155 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006156 case IPC_INFO:
6157 case SHM_INFO:
6158 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006159 return avc_has_perm(&selinux_state,
6160 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006161 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006162 case IPC_STAT:
6163 case SHM_STAT:
Davidlohr Buesoc21a6972018-04-10 16:35:23 -07006164 case SHM_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006165 perms = SHM__GETATTR | SHM__ASSOCIATE;
6166 break;
6167 case IPC_SET:
6168 perms = SHM__SETATTR;
6169 break;
6170 case SHM_LOCK:
6171 case SHM_UNLOCK:
6172 perms = SHM__LOCK;
6173 break;
6174 case IPC_RMID:
6175 perms = SHM__DESTROY;
6176 break;
6177 default:
6178 return 0;
6179 }
6180
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006181 err = ipc_has_perm(shp, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006182 return err;
6183}
6184
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006185static int selinux_shm_shmat(struct kern_ipc_perm *shp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006186 char __user *shmaddr, int shmflg)
6187{
6188 u32 perms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006189
6190 if (shmflg & SHM_RDONLY)
6191 perms = SHM__READ;
6192 else
6193 perms = SHM__READ | SHM__WRITE;
6194
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006195 return ipc_has_perm(shp, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006196}
6197
6198/* Semaphore security operations */
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006199static int selinux_sem_alloc_security(struct kern_ipc_perm *sma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006200{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006201 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006202 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006203 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204 int rc;
6205
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006206 rc = ipc_alloc_security(sma, SECCLASS_SEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006207 if (rc)
6208 return rc;
6209
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006210 isec = sma->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211
Eric Paris50c205f2012-04-04 15:01:43 -04006212 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006213 ad.u.ipc_id = sma->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006214
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006215 rc = avc_has_perm(&selinux_state,
6216 sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217 SEM__CREATE, &ad);
6218 if (rc) {
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006219 ipc_free_security(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220 return rc;
6221 }
6222 return 0;
6223}
6224
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006225static void selinux_sem_free_security(struct kern_ipc_perm *sma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006226{
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006227 ipc_free_security(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006228}
6229
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006230static int selinux_sem_associate(struct kern_ipc_perm *sma, int semflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006231{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006232 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006233 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006234 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006235
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006236 isec = sma->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006237
Eric Paris50c205f2012-04-04 15:01:43 -04006238 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006239 ad.u.ipc_id = sma->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006240
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006241 return avc_has_perm(&selinux_state,
6242 sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243 SEM__ASSOCIATE, &ad);
6244}
6245
6246/* Note, at this point, sma is locked down */
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006247static int selinux_sem_semctl(struct kern_ipc_perm *sma, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006248{
6249 int err;
6250 u32 perms;
6251
Eric Paris828dfe12008-04-17 13:17:49 -04006252 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006253 case IPC_INFO:
6254 case SEM_INFO:
6255 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006256 return avc_has_perm(&selinux_state,
6257 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006258 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006259 case GETPID:
6260 case GETNCNT:
6261 case GETZCNT:
6262 perms = SEM__GETATTR;
6263 break;
6264 case GETVAL:
6265 case GETALL:
6266 perms = SEM__READ;
6267 break;
6268 case SETVAL:
6269 case SETALL:
6270 perms = SEM__WRITE;
6271 break;
6272 case IPC_RMID:
6273 perms = SEM__DESTROY;
6274 break;
6275 case IPC_SET:
6276 perms = SEM__SETATTR;
6277 break;
6278 case IPC_STAT:
6279 case SEM_STAT:
Davidlohr Buesoa280d6d2018-04-10 16:35:26 -07006280 case SEM_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006281 perms = SEM__GETATTR | SEM__ASSOCIATE;
6282 break;
6283 default:
6284 return 0;
6285 }
6286
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006287 err = ipc_has_perm(sma, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006288 return err;
6289}
6290
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006291static int selinux_sem_semop(struct kern_ipc_perm *sma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006292 struct sembuf *sops, unsigned nsops, int alter)
6293{
6294 u32 perms;
6295
6296 if (alter)
6297 perms = SEM__READ | SEM__WRITE;
6298 else
6299 perms = SEM__READ;
6300
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006301 return ipc_has_perm(sma, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006302}
6303
6304static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
6305{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006306 u32 av = 0;
6307
Linus Torvalds1da177e2005-04-16 15:20:36 -07006308 av = 0;
6309 if (flag & S_IRUGO)
6310 av |= IPC__UNIX_READ;
6311 if (flag & S_IWUGO)
6312 av |= IPC__UNIX_WRITE;
6313
6314 if (av == 0)
6315 return 0;
6316
Stephen Smalley6af963f2005-05-01 08:58:39 -07006317 return ipc_has_perm(ipcp, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006318}
6319
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02006320static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
6321{
6322 struct ipc_security_struct *isec = ipcp->security;
6323 *secid = isec->sid;
6324}
6325
Eric Paris828dfe12008-04-17 13:17:49 -04006326static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006327{
6328 if (inode)
6329 inode_doinit_with_dentry(inode, dentry);
6330}
6331
6332static int selinux_getprocattr(struct task_struct *p,
Al Viro04ff9702007-03-12 16:17:58 +00006333 char *name, char **value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006334{
David Howells275bb412008-11-14 10:39:19 +11006335 const struct task_security_struct *__tsec;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00006336 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006337 int error;
Al Viro04ff9702007-03-12 16:17:58 +00006338 unsigned len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006339
David Howells275bb412008-11-14 10:39:19 +11006340 rcu_read_lock();
6341 __tsec = __task_cred(p)->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006342
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006343 if (current != p) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006344 error = avc_has_perm(&selinux_state,
6345 current_sid(), __tsec->sid,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006346 SECCLASS_PROCESS, PROCESS__GETATTR, NULL);
6347 if (error)
6348 goto bad;
6349 }
6350
Linus Torvalds1da177e2005-04-16 15:20:36 -07006351 if (!strcmp(name, "current"))
David Howells275bb412008-11-14 10:39:19 +11006352 sid = __tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006353 else if (!strcmp(name, "prev"))
David Howells275bb412008-11-14 10:39:19 +11006354 sid = __tsec->osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006355 else if (!strcmp(name, "exec"))
David Howells275bb412008-11-14 10:39:19 +11006356 sid = __tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006357 else if (!strcmp(name, "fscreate"))
David Howells275bb412008-11-14 10:39:19 +11006358 sid = __tsec->create_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006359 else if (!strcmp(name, "keycreate"))
David Howells275bb412008-11-14 10:39:19 +11006360 sid = __tsec->keycreate_sid;
Eric Paris42c3e032006-06-26 00:26:03 -07006361 else if (!strcmp(name, "sockcreate"))
David Howells275bb412008-11-14 10:39:19 +11006362 sid = __tsec->sockcreate_sid;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006363 else {
6364 error = -EINVAL;
6365 goto bad;
6366 }
David Howells275bb412008-11-14 10:39:19 +11006367 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006368
6369 if (!sid)
6370 return 0;
6371
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006372 error = security_sid_to_context(&selinux_state, sid, value, &len);
Al Viro04ff9702007-03-12 16:17:58 +00006373 if (error)
6374 return error;
6375 return len;
David Howells275bb412008-11-14 10:39:19 +11006376
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006377bad:
David Howells275bb412008-11-14 10:39:19 +11006378 rcu_read_unlock();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006379 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006380}
6381
Stephen Smalleyb21507e2017-01-09 10:07:31 -05006382static int selinux_setprocattr(const char *name, void *value, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006383{
6384 struct task_security_struct *tsec;
David Howellsd84f4f92008-11-14 10:39:23 +11006385 struct cred *new;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006386 u32 mysid = current_sid(), sid = 0, ptsid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006387 int error;
6388 char *str = value;
6389
Linus Torvalds1da177e2005-04-16 15:20:36 -07006390 /*
6391 * Basic control over ability to set these attributes at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006392 */
6393 if (!strcmp(name, "exec"))
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__SETEXEC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006397 else if (!strcmp(name, "fscreate"))
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__SETFSCREATE, NULL);
Michael LeMay4eb582c2006-06-26 00:24:57 -07006401 else if (!strcmp(name, "keycreate"))
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__SETKEYCREATE, NULL);
Eric Paris42c3e032006-06-26 00:26:03 -07006405 else if (!strcmp(name, "sockcreate"))
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__SETSOCKCREATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006409 else if (!strcmp(name, "current"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006410 error = avc_has_perm(&selinux_state,
6411 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006412 PROCESS__SETCURRENT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006413 else
6414 error = -EINVAL;
6415 if (error)
6416 return error;
6417
6418 /* Obtain a SID for the context, if one was specified. */
Stephen Smalleya050a572017-01-31 11:54:04 -05006419 if (size && str[0] && str[0] != '\n') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006420 if (str[size-1] == '\n') {
6421 str[size-1] = 0;
6422 size--;
6423 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006424 error = security_context_to_sid(&selinux_state, value, size,
6425 &sid, GFP_KERNEL);
Stephen Smalley12b29f32008-05-07 13:03:20 -04006426 if (error == -EINVAL && !strcmp(name, "fscreate")) {
Stephen Smalleydb590002017-04-20 11:31:30 -04006427 if (!has_cap_mac_admin(true)) {
Eric Parisd6ea83e2012-04-04 13:45:49 -04006428 struct audit_buffer *ab;
6429 size_t audit_size;
6430
6431 /* We strip a nul only if it is at the end, otherwise the
6432 * context contains a nul and we should audit that */
6433 if (str[size - 1] == '\0')
6434 audit_size = size - 1;
6435 else
6436 audit_size = size;
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04006437 ab = audit_log_start(audit_context(),
6438 GFP_ATOMIC,
6439 AUDIT_SELINUX_ERR);
Eric Parisd6ea83e2012-04-04 13:45:49 -04006440 audit_log_format(ab, "op=fscreate invalid_context=");
6441 audit_log_n_untrustedstring(ab, value, audit_size);
6442 audit_log_end(ab);
6443
Stephen Smalley12b29f32008-05-07 13:03:20 -04006444 return error;
Eric Parisd6ea83e2012-04-04 13:45:49 -04006445 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006446 error = security_context_to_sid_force(
6447 &selinux_state,
6448 value, size, &sid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04006449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006450 if (error)
6451 return error;
6452 }
6453
David Howellsd84f4f92008-11-14 10:39:23 +11006454 new = prepare_creds();
6455 if (!new)
6456 return -ENOMEM;
6457
Linus Torvalds1da177e2005-04-16 15:20:36 -07006458 /* Permission checking based on the specified context is
6459 performed during the actual operation (execve,
6460 open/mkdir/...), when we know the full context of the
David Howellsd84f4f92008-11-14 10:39:23 +11006461 operation. See selinux_bprm_set_creds for the execve
Linus Torvalds1da177e2005-04-16 15:20:36 -07006462 checks and may_create for the file creation checks. The
6463 operation will then fail if the context is not permitted. */
David Howellsd84f4f92008-11-14 10:39:23 +11006464 tsec = new->security;
6465 if (!strcmp(name, "exec")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006466 tsec->exec_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006467 } else if (!strcmp(name, "fscreate")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006468 tsec->create_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006469 } else if (!strcmp(name, "keycreate")) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006470 error = avc_has_perm(&selinux_state,
6471 mysid, sid, SECCLASS_KEY, KEY__CREATE,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006472 NULL);
Michael LeMay4eb582c2006-06-26 00:24:57 -07006473 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11006474 goto abort_change;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006475 tsec->keycreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006476 } else if (!strcmp(name, "sockcreate")) {
Eric Paris42c3e032006-06-26 00:26:03 -07006477 tsec->sockcreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006478 } else if (!strcmp(name, "current")) {
6479 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006480 if (sid == 0)
David Howellsd84f4f92008-11-14 10:39:23 +11006481 goto abort_change;
KaiGai Koheid9250de2008-08-28 16:35:57 +09006482
David Howellsd84f4f92008-11-14 10:39:23 +11006483 /* Only allow single threaded processes to change context */
6484 error = -EPERM;
Oleg Nesterov5bb459b2009-07-10 03:48:23 +02006485 if (!current_is_single_threaded()) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006486 error = security_bounded_transition(&selinux_state,
6487 tsec->sid, sid);
David Howellsd84f4f92008-11-14 10:39:23 +11006488 if (error)
6489 goto abort_change;
Eric Paris828dfe12008-04-17 13:17:49 -04006490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006491
6492 /* Check permissions for the transition. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006493 error = avc_has_perm(&selinux_state,
6494 tsec->sid, sid, SECCLASS_PROCESS,
Eric Paris828dfe12008-04-17 13:17:49 -04006495 PROCESS__DYNTRANSITION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006496 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11006497 goto abort_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006498
6499 /* Check for ptracing, and update the task SID if ok.
6500 Otherwise, leave SID unchanged and fail. */
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006501 ptsid = ptrace_parent_sid();
Paul Moore0c6181c2016-03-30 21:41:21 -04006502 if (ptsid != 0) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006503 error = avc_has_perm(&selinux_state,
6504 ptsid, sid, SECCLASS_PROCESS,
David Howellsd84f4f92008-11-14 10:39:23 +11006505 PROCESS__PTRACE, NULL);
6506 if (error)
6507 goto abort_change;
6508 }
6509
6510 tsec->sid = sid;
6511 } else {
6512 error = -EINVAL;
6513 goto abort_change;
6514 }
6515
6516 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006517 return size;
David Howellsd84f4f92008-11-14 10:39:23 +11006518
6519abort_change:
6520 abort_creds(new);
6521 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006522}
6523
David Quigley746df9b2013-05-22 12:50:35 -04006524static int selinux_ismaclabel(const char *name)
6525{
6526 return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0);
6527}
6528
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006529static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
6530{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006531 return security_sid_to_context(&selinux_state, secid,
6532 secdata, seclen);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006533}
6534
David Howells7bf570d2008-04-29 20:52:51 +01006535static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
David Howells63cb3442008-01-15 23:47:35 +00006536{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006537 return security_context_to_sid(&selinux_state, secdata, seclen,
6538 secid, GFP_KERNEL);
David Howells63cb3442008-01-15 23:47:35 +00006539}
6540
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006541static void selinux_release_secctx(char *secdata, u32 seclen)
6542{
Paul Moore088999e2007-08-01 11:12:58 -04006543 kfree(secdata);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006544}
6545
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006546static void selinux_inode_invalidate_secctx(struct inode *inode)
6547{
6548 struct inode_security_struct *isec = inode->i_security;
6549
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01006550 spin_lock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006551 isec->initialized = LABEL_INVALID;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01006552 spin_unlock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006553}
6554
David P. Quigley1ee65e32009-09-03 14:25:57 -04006555/*
6556 * called with inode->i_mutex locked
6557 */
6558static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
6559{
6560 return selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, ctx, ctxlen, 0);
6561}
6562
6563/*
6564 * called with inode->i_mutex locked
6565 */
6566static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
6567{
6568 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0);
6569}
6570
6571static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
6572{
6573 int len = 0;
6574 len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX,
6575 ctx, true);
6576 if (len < 0)
6577 return len;
6578 *ctxlen = len;
6579 return 0;
6580}
Michael LeMayd7200242006-06-22 14:47:17 -07006581#ifdef CONFIG_KEYS
6582
David Howellsd84f4f92008-11-14 10:39:23 +11006583static int selinux_key_alloc(struct key *k, const struct cred *cred,
David Howells7e047ef2006-06-26 00:24:50 -07006584 unsigned long flags)
Michael LeMayd7200242006-06-22 14:47:17 -07006585{
David Howellsd84f4f92008-11-14 10:39:23 +11006586 const struct task_security_struct *tsec;
Michael LeMayd7200242006-06-22 14:47:17 -07006587 struct key_security_struct *ksec;
6588
6589 ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
6590 if (!ksec)
6591 return -ENOMEM;
6592
David Howellsd84f4f92008-11-14 10:39:23 +11006593 tsec = cred->security;
6594 if (tsec->keycreate_sid)
6595 ksec->sid = tsec->keycreate_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006596 else
David Howellsd84f4f92008-11-14 10:39:23 +11006597 ksec->sid = tsec->sid;
Michael LeMayd7200242006-06-22 14:47:17 -07006598
David Howells275bb412008-11-14 10:39:19 +11006599 k->security = ksec;
Michael LeMayd7200242006-06-22 14:47:17 -07006600 return 0;
6601}
6602
6603static void selinux_key_free(struct key *k)
6604{
6605 struct key_security_struct *ksec = k->security;
6606
6607 k->security = NULL;
6608 kfree(ksec);
6609}
6610
6611static int selinux_key_permission(key_ref_t key_ref,
David Howellsd84f4f92008-11-14 10:39:23 +11006612 const struct cred *cred,
David Howellsf5895942014-03-14 17:44:49 +00006613 unsigned perm)
Michael LeMayd7200242006-06-22 14:47:17 -07006614{
6615 struct key *key;
Michael LeMayd7200242006-06-22 14:47:17 -07006616 struct key_security_struct *ksec;
David Howells275bb412008-11-14 10:39:19 +11006617 u32 sid;
Michael LeMayd7200242006-06-22 14:47:17 -07006618
6619 /* if no specific permissions are requested, we skip the
6620 permission check. No serious, additional covert channels
6621 appear to be created. */
6622 if (perm == 0)
6623 return 0;
6624
David Howellsd84f4f92008-11-14 10:39:23 +11006625 sid = cred_sid(cred);
David Howells275bb412008-11-14 10:39:19 +11006626
6627 key = key_ref_to_ptr(key_ref);
6628 ksec = key->security;
6629
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006630 return avc_has_perm(&selinux_state,
6631 sid, ksec->sid, SECCLASS_KEY, perm, NULL);
Michael LeMayd7200242006-06-22 14:47:17 -07006632}
6633
David Howells70a5bb72008-04-29 01:01:26 -07006634static int selinux_key_getsecurity(struct key *key, char **_buffer)
6635{
6636 struct key_security_struct *ksec = key->security;
6637 char *context = NULL;
6638 unsigned len;
6639 int rc;
6640
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006641 rc = security_sid_to_context(&selinux_state, ksec->sid,
6642 &context, &len);
David Howells70a5bb72008-04-29 01:01:26 -07006643 if (!rc)
6644 rc = len;
6645 *_buffer = context;
6646 return rc;
6647}
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006648#endif
David Howells70a5bb72008-04-29 01:01:26 -07006649
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006650#ifdef CONFIG_SECURITY_INFINIBAND
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006651static int selinux_ib_pkey_access(void *ib_sec, u64 subnet_prefix, u16 pkey_val)
6652{
6653 struct common_audit_data ad;
6654 int err;
6655 u32 sid = 0;
6656 struct ib_security_struct *sec = ib_sec;
6657 struct lsm_ibpkey_audit ibpkey;
6658
Daniel Jurgens409dcf32017-05-19 15:48:59 +03006659 err = sel_ib_pkey_sid(subnet_prefix, pkey_val, &sid);
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006660 if (err)
6661 return err;
6662
6663 ad.type = LSM_AUDIT_DATA_IBPKEY;
6664 ibpkey.subnet_prefix = subnet_prefix;
6665 ibpkey.pkey = pkey_val;
6666 ad.u.ibpkey = &ibpkey;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006667 return avc_has_perm(&selinux_state,
6668 sec->sid, sid,
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006669 SECCLASS_INFINIBAND_PKEY,
6670 INFINIBAND_PKEY__ACCESS, &ad);
6671}
6672
Daniel Jurgensab861df2017-05-19 15:48:58 +03006673static int selinux_ib_endport_manage_subnet(void *ib_sec, const char *dev_name,
6674 u8 port_num)
6675{
6676 struct common_audit_data ad;
6677 int err;
6678 u32 sid = 0;
6679 struct ib_security_struct *sec = ib_sec;
6680 struct lsm_ibendport_audit ibendport;
6681
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006682 err = security_ib_endport_sid(&selinux_state, dev_name, port_num,
6683 &sid);
Daniel Jurgensab861df2017-05-19 15:48:58 +03006684
6685 if (err)
6686 return err;
6687
6688 ad.type = LSM_AUDIT_DATA_IBENDPORT;
6689 strncpy(ibendport.dev_name, dev_name, sizeof(ibendport.dev_name));
6690 ibendport.port = port_num;
6691 ad.u.ibendport = &ibendport;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006692 return avc_has_perm(&selinux_state,
6693 sec->sid, sid,
Daniel Jurgensab861df2017-05-19 15:48:58 +03006694 SECCLASS_INFINIBAND_ENDPORT,
6695 INFINIBAND_ENDPORT__MANAGE_SUBNET, &ad);
6696}
6697
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006698static int selinux_ib_alloc_security(void **ib_sec)
6699{
6700 struct ib_security_struct *sec;
6701
6702 sec = kzalloc(sizeof(*sec), GFP_KERNEL);
6703 if (!sec)
6704 return -ENOMEM;
6705 sec->sid = current_sid();
6706
6707 *ib_sec = sec;
6708 return 0;
6709}
6710
6711static void selinux_ib_free_security(void *ib_sec)
6712{
6713 kfree(ib_sec);
6714}
Michael LeMayd7200242006-06-22 14:47:17 -07006715#endif
6716
Chenbo Fengec27c352017-10-18 13:00:25 -07006717#ifdef CONFIG_BPF_SYSCALL
6718static int selinux_bpf(int cmd, union bpf_attr *attr,
6719 unsigned int size)
6720{
6721 u32 sid = current_sid();
6722 int ret;
6723
6724 switch (cmd) {
6725 case BPF_MAP_CREATE:
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006726 ret = avc_has_perm(&selinux_state,
6727 sid, sid, SECCLASS_BPF, BPF__MAP_CREATE,
Chenbo Fengec27c352017-10-18 13:00:25 -07006728 NULL);
6729 break;
6730 case BPF_PROG_LOAD:
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006731 ret = avc_has_perm(&selinux_state,
6732 sid, sid, SECCLASS_BPF, BPF__PROG_LOAD,
Chenbo Fengec27c352017-10-18 13:00:25 -07006733 NULL);
6734 break;
6735 default:
6736 ret = 0;
6737 break;
6738 }
6739
6740 return ret;
6741}
6742
6743static u32 bpf_map_fmode_to_av(fmode_t fmode)
6744{
6745 u32 av = 0;
6746
6747 if (fmode & FMODE_READ)
6748 av |= BPF__MAP_READ;
6749 if (fmode & FMODE_WRITE)
6750 av |= BPF__MAP_WRITE;
6751 return av;
6752}
6753
Chenbo Fengf66e4482017-10-18 13:00:26 -07006754/* This function will check the file pass through unix socket or binder to see
6755 * if it is a bpf related object. And apply correspinding checks on the bpf
6756 * object based on the type. The bpf maps and programs, not like other files and
6757 * socket, are using a shared anonymous inode inside the kernel as their inode.
6758 * So checking that inode cannot identify if the process have privilege to
6759 * access the bpf object and that's why we have to add this additional check in
6760 * selinux_file_receive and selinux_binder_transfer_files.
6761 */
6762static int bpf_fd_pass(struct file *file, u32 sid)
6763{
6764 struct bpf_security_struct *bpfsec;
6765 struct bpf_prog *prog;
6766 struct bpf_map *map;
6767 int ret;
6768
6769 if (file->f_op == &bpf_map_fops) {
6770 map = file->private_data;
6771 bpfsec = map->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006772 ret = avc_has_perm(&selinux_state,
6773 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengf66e4482017-10-18 13:00:26 -07006774 bpf_map_fmode_to_av(file->f_mode), NULL);
6775 if (ret)
6776 return ret;
6777 } else if (file->f_op == &bpf_prog_fops) {
6778 prog = file->private_data;
6779 bpfsec = prog->aux->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006780 ret = avc_has_perm(&selinux_state,
6781 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengf66e4482017-10-18 13:00:26 -07006782 BPF__PROG_RUN, NULL);
6783 if (ret)
6784 return ret;
6785 }
6786 return 0;
6787}
6788
Chenbo Fengec27c352017-10-18 13:00:25 -07006789static int selinux_bpf_map(struct bpf_map *map, fmode_t fmode)
6790{
6791 u32 sid = current_sid();
6792 struct bpf_security_struct *bpfsec;
6793
6794 bpfsec = map->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006795 return avc_has_perm(&selinux_state,
6796 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengec27c352017-10-18 13:00:25 -07006797 bpf_map_fmode_to_av(fmode), NULL);
6798}
6799
6800static int selinux_bpf_prog(struct bpf_prog *prog)
6801{
6802 u32 sid = current_sid();
6803 struct bpf_security_struct *bpfsec;
6804
6805 bpfsec = prog->aux->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006806 return avc_has_perm(&selinux_state,
6807 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengec27c352017-10-18 13:00:25 -07006808 BPF__PROG_RUN, NULL);
6809}
6810
6811static int selinux_bpf_map_alloc(struct bpf_map *map)
6812{
6813 struct bpf_security_struct *bpfsec;
6814
6815 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6816 if (!bpfsec)
6817 return -ENOMEM;
6818
6819 bpfsec->sid = current_sid();
6820 map->security = bpfsec;
6821
6822 return 0;
6823}
6824
6825static void selinux_bpf_map_free(struct bpf_map *map)
6826{
6827 struct bpf_security_struct *bpfsec = map->security;
6828
6829 map->security = NULL;
6830 kfree(bpfsec);
6831}
6832
6833static int selinux_bpf_prog_alloc(struct bpf_prog_aux *aux)
6834{
6835 struct bpf_security_struct *bpfsec;
6836
6837 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6838 if (!bpfsec)
6839 return -ENOMEM;
6840
6841 bpfsec->sid = current_sid();
6842 aux->security = bpfsec;
6843
6844 return 0;
6845}
6846
6847static void selinux_bpf_prog_free(struct bpf_prog_aux *aux)
6848{
6849 struct bpf_security_struct *bpfsec = aux->security;
6850
6851 aux->security = NULL;
6852 kfree(bpfsec);
6853}
6854#endif
6855
James Morrisca97d932017-02-15 00:18:51 +11006856static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {
Casey Schauflere20b0432015-05-02 15:11:36 -07006857 LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr),
6858 LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction),
6859 LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder),
6860 LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file),
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02006861
Casey Schauflere20b0432015-05-02 15:11:36 -07006862 LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check),
6863 LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme),
6864 LSM_HOOK_INIT(capget, selinux_capget),
6865 LSM_HOOK_INIT(capset, selinux_capset),
6866 LSM_HOOK_INIT(capable, selinux_capable),
6867 LSM_HOOK_INIT(quotactl, selinux_quotactl),
6868 LSM_HOOK_INIT(quota_on, selinux_quota_on),
6869 LSM_HOOK_INIT(syslog, selinux_syslog),
6870 LSM_HOOK_INIT(vm_enough_memory, selinux_vm_enough_memory),
Stephen Smalley79af7302015-01-21 10:54:10 -05006871
Casey Schauflere20b0432015-05-02 15:11:36 -07006872 LSM_HOOK_INIT(netlink_send, selinux_netlink_send),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006873
Casey Schauflere20b0432015-05-02 15:11:36 -07006874 LSM_HOOK_INIT(bprm_set_creds, selinux_bprm_set_creds),
6875 LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds),
6876 LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006877
Casey Schauflere20b0432015-05-02 15:11:36 -07006878 LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security),
6879 LSM_HOOK_INIT(sb_free_security, selinux_sb_free_security),
Al Viro5b400232018-12-12 20:13:29 -05006880 LSM_HOOK_INIT(sb_eat_lsm_opts, selinux_sb_eat_lsm_opts),
Casey Schauflere20b0432015-05-02 15:11:36 -07006881 LSM_HOOK_INIT(sb_remount, selinux_sb_remount),
6882 LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount),
6883 LSM_HOOK_INIT(sb_show_options, selinux_sb_show_options),
6884 LSM_HOOK_INIT(sb_statfs, selinux_sb_statfs),
6885 LSM_HOOK_INIT(sb_mount, selinux_mount),
6886 LSM_HOOK_INIT(sb_umount, selinux_umount),
6887 LSM_HOOK_INIT(sb_set_mnt_opts, selinux_set_mnt_opts),
6888 LSM_HOOK_INIT(sb_clone_mnt_opts, selinux_sb_clone_mnt_opts),
6889 LSM_HOOK_INIT(sb_parse_opts_str, selinux_parse_opts_str),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006890
Casey Schauflere20b0432015-05-02 15:11:36 -07006891 LSM_HOOK_INIT(dentry_init_security, selinux_dentry_init_security),
Vivek Goyala518b0a2016-07-13 10:44:53 -04006892 LSM_HOOK_INIT(dentry_create_files_as, selinux_dentry_create_files_as),
Eric Parise0007522008-03-05 10:31:54 -05006893
Casey Schauflere20b0432015-05-02 15:11:36 -07006894 LSM_HOOK_INIT(inode_alloc_security, selinux_inode_alloc_security),
6895 LSM_HOOK_INIT(inode_free_security, selinux_inode_free_security),
6896 LSM_HOOK_INIT(inode_init_security, selinux_inode_init_security),
6897 LSM_HOOK_INIT(inode_create, selinux_inode_create),
6898 LSM_HOOK_INIT(inode_link, selinux_inode_link),
6899 LSM_HOOK_INIT(inode_unlink, selinux_inode_unlink),
6900 LSM_HOOK_INIT(inode_symlink, selinux_inode_symlink),
6901 LSM_HOOK_INIT(inode_mkdir, selinux_inode_mkdir),
6902 LSM_HOOK_INIT(inode_rmdir, selinux_inode_rmdir),
6903 LSM_HOOK_INIT(inode_mknod, selinux_inode_mknod),
6904 LSM_HOOK_INIT(inode_rename, selinux_inode_rename),
6905 LSM_HOOK_INIT(inode_readlink, selinux_inode_readlink),
6906 LSM_HOOK_INIT(inode_follow_link, selinux_inode_follow_link),
6907 LSM_HOOK_INIT(inode_permission, selinux_inode_permission),
6908 LSM_HOOK_INIT(inode_setattr, selinux_inode_setattr),
6909 LSM_HOOK_INIT(inode_getattr, selinux_inode_getattr),
6910 LSM_HOOK_INIT(inode_setxattr, selinux_inode_setxattr),
6911 LSM_HOOK_INIT(inode_post_setxattr, selinux_inode_post_setxattr),
6912 LSM_HOOK_INIT(inode_getxattr, selinux_inode_getxattr),
6913 LSM_HOOK_INIT(inode_listxattr, selinux_inode_listxattr),
6914 LSM_HOOK_INIT(inode_removexattr, selinux_inode_removexattr),
6915 LSM_HOOK_INIT(inode_getsecurity, selinux_inode_getsecurity),
6916 LSM_HOOK_INIT(inode_setsecurity, selinux_inode_setsecurity),
6917 LSM_HOOK_INIT(inode_listsecurity, selinux_inode_listsecurity),
6918 LSM_HOOK_INIT(inode_getsecid, selinux_inode_getsecid),
Vivek Goyal56909eb2016-07-13 10:44:48 -04006919 LSM_HOOK_INIT(inode_copy_up, selinux_inode_copy_up),
Vivek Goyal19472b62016-07-13 10:44:50 -04006920 LSM_HOOK_INIT(inode_copy_up_xattr, selinux_inode_copy_up_xattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006921
Casey Schauflere20b0432015-05-02 15:11:36 -07006922 LSM_HOOK_INIT(file_permission, selinux_file_permission),
6923 LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security),
6924 LSM_HOOK_INIT(file_free_security, selinux_file_free_security),
6925 LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl),
6926 LSM_HOOK_INIT(mmap_file, selinux_mmap_file),
6927 LSM_HOOK_INIT(mmap_addr, selinux_mmap_addr),
6928 LSM_HOOK_INIT(file_mprotect, selinux_file_mprotect),
6929 LSM_HOOK_INIT(file_lock, selinux_file_lock),
6930 LSM_HOOK_INIT(file_fcntl, selinux_file_fcntl),
6931 LSM_HOOK_INIT(file_set_fowner, selinux_file_set_fowner),
6932 LSM_HOOK_INIT(file_send_sigiotask, selinux_file_send_sigiotask),
6933 LSM_HOOK_INIT(file_receive, selinux_file_receive),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006934
Casey Schauflere20b0432015-05-02 15:11:36 -07006935 LSM_HOOK_INIT(file_open, selinux_file_open),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006936
Tetsuo Handaa79be232017-03-28 23:08:45 +09006937 LSM_HOOK_INIT(task_alloc, selinux_task_alloc),
Casey Schauflere20b0432015-05-02 15:11:36 -07006938 LSM_HOOK_INIT(cred_alloc_blank, selinux_cred_alloc_blank),
6939 LSM_HOOK_INIT(cred_free, selinux_cred_free),
6940 LSM_HOOK_INIT(cred_prepare, selinux_cred_prepare),
6941 LSM_HOOK_INIT(cred_transfer, selinux_cred_transfer),
Matthew Garrett3ec30112018-01-08 13:36:19 -08006942 LSM_HOOK_INIT(cred_getsecid, selinux_cred_getsecid),
Casey Schauflere20b0432015-05-02 15:11:36 -07006943 LSM_HOOK_INIT(kernel_act_as, selinux_kernel_act_as),
6944 LSM_HOOK_INIT(kernel_create_files_as, selinux_kernel_create_files_as),
6945 LSM_HOOK_INIT(kernel_module_request, selinux_kernel_module_request),
Mimi Zoharc77b8cd2018-07-13 14:06:02 -04006946 LSM_HOOK_INIT(kernel_load_data, selinux_kernel_load_data),
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07006947 LSM_HOOK_INIT(kernel_read_file, selinux_kernel_read_file),
Casey Schauflere20b0432015-05-02 15:11:36 -07006948 LSM_HOOK_INIT(task_setpgid, selinux_task_setpgid),
6949 LSM_HOOK_INIT(task_getpgid, selinux_task_getpgid),
6950 LSM_HOOK_INIT(task_getsid, selinux_task_getsid),
6951 LSM_HOOK_INIT(task_getsecid, selinux_task_getsecid),
6952 LSM_HOOK_INIT(task_setnice, selinux_task_setnice),
6953 LSM_HOOK_INIT(task_setioprio, selinux_task_setioprio),
6954 LSM_HOOK_INIT(task_getioprio, selinux_task_getioprio),
Stephen Smalley791ec492017-02-17 07:57:00 -05006955 LSM_HOOK_INIT(task_prlimit, selinux_task_prlimit),
Casey Schauflere20b0432015-05-02 15:11:36 -07006956 LSM_HOOK_INIT(task_setrlimit, selinux_task_setrlimit),
6957 LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler),
6958 LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler),
6959 LSM_HOOK_INIT(task_movememory, selinux_task_movememory),
6960 LSM_HOOK_INIT(task_kill, selinux_task_kill),
Casey Schauflere20b0432015-05-02 15:11:36 -07006961 LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode),
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09006962
Casey Schauflere20b0432015-05-02 15:11:36 -07006963 LSM_HOOK_INIT(ipc_permission, selinux_ipc_permission),
6964 LSM_HOOK_INIT(ipc_getsecid, selinux_ipc_getsecid),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006965
Casey Schauflere20b0432015-05-02 15:11:36 -07006966 LSM_HOOK_INIT(msg_msg_alloc_security, selinux_msg_msg_alloc_security),
6967 LSM_HOOK_INIT(msg_msg_free_security, selinux_msg_msg_free_security),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006968
Casey Schauflere20b0432015-05-02 15:11:36 -07006969 LSM_HOOK_INIT(msg_queue_alloc_security,
6970 selinux_msg_queue_alloc_security),
6971 LSM_HOOK_INIT(msg_queue_free_security, selinux_msg_queue_free_security),
6972 LSM_HOOK_INIT(msg_queue_associate, selinux_msg_queue_associate),
6973 LSM_HOOK_INIT(msg_queue_msgctl, selinux_msg_queue_msgctl),
6974 LSM_HOOK_INIT(msg_queue_msgsnd, selinux_msg_queue_msgsnd),
6975 LSM_HOOK_INIT(msg_queue_msgrcv, selinux_msg_queue_msgrcv),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006976
Casey Schauflere20b0432015-05-02 15:11:36 -07006977 LSM_HOOK_INIT(shm_alloc_security, selinux_shm_alloc_security),
6978 LSM_HOOK_INIT(shm_free_security, selinux_shm_free_security),
6979 LSM_HOOK_INIT(shm_associate, selinux_shm_associate),
6980 LSM_HOOK_INIT(shm_shmctl, selinux_shm_shmctl),
6981 LSM_HOOK_INIT(shm_shmat, selinux_shm_shmat),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006982
Casey Schauflere20b0432015-05-02 15:11:36 -07006983 LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security),
6984 LSM_HOOK_INIT(sem_free_security, selinux_sem_free_security),
6985 LSM_HOOK_INIT(sem_associate, selinux_sem_associate),
6986 LSM_HOOK_INIT(sem_semctl, selinux_sem_semctl),
6987 LSM_HOOK_INIT(sem_semop, selinux_sem_semop),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006988
Casey Schauflere20b0432015-05-02 15:11:36 -07006989 LSM_HOOK_INIT(d_instantiate, selinux_d_instantiate),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006990
Casey Schauflere20b0432015-05-02 15:11:36 -07006991 LSM_HOOK_INIT(getprocattr, selinux_getprocattr),
6992 LSM_HOOK_INIT(setprocattr, selinux_setprocattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006993
Casey Schauflere20b0432015-05-02 15:11:36 -07006994 LSM_HOOK_INIT(ismaclabel, selinux_ismaclabel),
6995 LSM_HOOK_INIT(secid_to_secctx, selinux_secid_to_secctx),
6996 LSM_HOOK_INIT(secctx_to_secid, selinux_secctx_to_secid),
6997 LSM_HOOK_INIT(release_secctx, selinux_release_secctx),
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006998 LSM_HOOK_INIT(inode_invalidate_secctx, selinux_inode_invalidate_secctx),
Casey Schauflere20b0432015-05-02 15:11:36 -07006999 LSM_HOOK_INIT(inode_notifysecctx, selinux_inode_notifysecctx),
7000 LSM_HOOK_INIT(inode_setsecctx, selinux_inode_setsecctx),
7001 LSM_HOOK_INIT(inode_getsecctx, selinux_inode_getsecctx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07007002
Casey Schauflere20b0432015-05-02 15:11:36 -07007003 LSM_HOOK_INIT(unix_stream_connect, selinux_socket_unix_stream_connect),
7004 LSM_HOOK_INIT(unix_may_send, selinux_socket_unix_may_send),
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07007005
Casey Schauflere20b0432015-05-02 15:11:36 -07007006 LSM_HOOK_INIT(socket_create, selinux_socket_create),
7007 LSM_HOOK_INIT(socket_post_create, selinux_socket_post_create),
David Herrmann0b811db2018-05-04 16:28:21 +02007008 LSM_HOOK_INIT(socket_socketpair, selinux_socket_socketpair),
Casey Schauflere20b0432015-05-02 15:11:36 -07007009 LSM_HOOK_INIT(socket_bind, selinux_socket_bind),
7010 LSM_HOOK_INIT(socket_connect, selinux_socket_connect),
7011 LSM_HOOK_INIT(socket_listen, selinux_socket_listen),
7012 LSM_HOOK_INIT(socket_accept, selinux_socket_accept),
7013 LSM_HOOK_INIT(socket_sendmsg, selinux_socket_sendmsg),
7014 LSM_HOOK_INIT(socket_recvmsg, selinux_socket_recvmsg),
7015 LSM_HOOK_INIT(socket_getsockname, selinux_socket_getsockname),
7016 LSM_HOOK_INIT(socket_getpeername, selinux_socket_getpeername),
7017 LSM_HOOK_INIT(socket_getsockopt, selinux_socket_getsockopt),
7018 LSM_HOOK_INIT(socket_setsockopt, selinux_socket_setsockopt),
7019 LSM_HOOK_INIT(socket_shutdown, selinux_socket_shutdown),
7020 LSM_HOOK_INIT(socket_sock_rcv_skb, selinux_socket_sock_rcv_skb),
7021 LSM_HOOK_INIT(socket_getpeersec_stream,
7022 selinux_socket_getpeersec_stream),
7023 LSM_HOOK_INIT(socket_getpeersec_dgram, selinux_socket_getpeersec_dgram),
7024 LSM_HOOK_INIT(sk_alloc_security, selinux_sk_alloc_security),
7025 LSM_HOOK_INIT(sk_free_security, selinux_sk_free_security),
7026 LSM_HOOK_INIT(sk_clone_security, selinux_sk_clone_security),
7027 LSM_HOOK_INIT(sk_getsecid, selinux_sk_getsecid),
7028 LSM_HOOK_INIT(sock_graft, selinux_sock_graft),
Richard Hainesd4529302018-02-13 20:57:18 +00007029 LSM_HOOK_INIT(sctp_assoc_request, selinux_sctp_assoc_request),
7030 LSM_HOOK_INIT(sctp_sk_clone, selinux_sctp_sk_clone),
7031 LSM_HOOK_INIT(sctp_bind_connect, selinux_sctp_bind_connect),
Casey Schauflere20b0432015-05-02 15:11:36 -07007032 LSM_HOOK_INIT(inet_conn_request, selinux_inet_conn_request),
7033 LSM_HOOK_INIT(inet_csk_clone, selinux_inet_csk_clone),
7034 LSM_HOOK_INIT(inet_conn_established, selinux_inet_conn_established),
7035 LSM_HOOK_INIT(secmark_relabel_packet, selinux_secmark_relabel_packet),
7036 LSM_HOOK_INIT(secmark_refcount_inc, selinux_secmark_refcount_inc),
7037 LSM_HOOK_INIT(secmark_refcount_dec, selinux_secmark_refcount_dec),
7038 LSM_HOOK_INIT(req_classify_flow, selinux_req_classify_flow),
7039 LSM_HOOK_INIT(tun_dev_alloc_security, selinux_tun_dev_alloc_security),
7040 LSM_HOOK_INIT(tun_dev_free_security, selinux_tun_dev_free_security),
7041 LSM_HOOK_INIT(tun_dev_create, selinux_tun_dev_create),
7042 LSM_HOOK_INIT(tun_dev_attach_queue, selinux_tun_dev_attach_queue),
7043 LSM_HOOK_INIT(tun_dev_attach, selinux_tun_dev_attach),
7044 LSM_HOOK_INIT(tun_dev_open, selinux_tun_dev_open),
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03007045#ifdef CONFIG_SECURITY_INFINIBAND
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03007046 LSM_HOOK_INIT(ib_pkey_access, selinux_ib_pkey_access),
Daniel Jurgensab861df2017-05-19 15:48:58 +03007047 LSM_HOOK_INIT(ib_endport_manage_subnet,
7048 selinux_ib_endport_manage_subnet),
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03007049 LSM_HOOK_INIT(ib_alloc_security, selinux_ib_alloc_security),
7050 LSM_HOOK_INIT(ib_free_security, selinux_ib_free_security),
7051#endif
Trent Jaegerd28d1e02005-12-13 23:12:40 -08007052#ifdef CONFIG_SECURITY_NETWORK_XFRM
Casey Schauflere20b0432015-05-02 15:11:36 -07007053 LSM_HOOK_INIT(xfrm_policy_alloc_security, selinux_xfrm_policy_alloc),
7054 LSM_HOOK_INIT(xfrm_policy_clone_security, selinux_xfrm_policy_clone),
7055 LSM_HOOK_INIT(xfrm_policy_free_security, selinux_xfrm_policy_free),
7056 LSM_HOOK_INIT(xfrm_policy_delete_security, selinux_xfrm_policy_delete),
7057 LSM_HOOK_INIT(xfrm_state_alloc, selinux_xfrm_state_alloc),
7058 LSM_HOOK_INIT(xfrm_state_alloc_acquire,
7059 selinux_xfrm_state_alloc_acquire),
7060 LSM_HOOK_INIT(xfrm_state_free_security, selinux_xfrm_state_free),
7061 LSM_HOOK_INIT(xfrm_state_delete_security, selinux_xfrm_state_delete),
7062 LSM_HOOK_INIT(xfrm_policy_lookup, selinux_xfrm_policy_lookup),
7063 LSM_HOOK_INIT(xfrm_state_pol_flow_match,
7064 selinux_xfrm_state_pol_flow_match),
7065 LSM_HOOK_INIT(xfrm_decode_session, selinux_xfrm_decode_session),
Linus Torvalds1da177e2005-04-16 15:20:36 -07007066#endif
Michael LeMayd7200242006-06-22 14:47:17 -07007067
7068#ifdef CONFIG_KEYS
Casey Schauflere20b0432015-05-02 15:11:36 -07007069 LSM_HOOK_INIT(key_alloc, selinux_key_alloc),
7070 LSM_HOOK_INIT(key_free, selinux_key_free),
7071 LSM_HOOK_INIT(key_permission, selinux_key_permission),
7072 LSM_HOOK_INIT(key_getsecurity, selinux_key_getsecurity),
Michael LeMayd7200242006-06-22 14:47:17 -07007073#endif
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02007074
7075#ifdef CONFIG_AUDIT
Casey Schauflere20b0432015-05-02 15:11:36 -07007076 LSM_HOOK_INIT(audit_rule_init, selinux_audit_rule_init),
7077 LSM_HOOK_INIT(audit_rule_known, selinux_audit_rule_known),
7078 LSM_HOOK_INIT(audit_rule_match, selinux_audit_rule_match),
7079 LSM_HOOK_INIT(audit_rule_free, selinux_audit_rule_free),
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02007080#endif
Chenbo Fengec27c352017-10-18 13:00:25 -07007081
7082#ifdef CONFIG_BPF_SYSCALL
7083 LSM_HOOK_INIT(bpf, selinux_bpf),
7084 LSM_HOOK_INIT(bpf_map, selinux_bpf_map),
7085 LSM_HOOK_INIT(bpf_prog, selinux_bpf_prog),
7086 LSM_HOOK_INIT(bpf_map_alloc_security, selinux_bpf_map_alloc),
7087 LSM_HOOK_INIT(bpf_prog_alloc_security, selinux_bpf_prog_alloc),
7088 LSM_HOOK_INIT(bpf_map_free_security, selinux_bpf_map_free),
7089 LSM_HOOK_INIT(bpf_prog_free_security, selinux_bpf_prog_free),
7090#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007091};
7092
7093static __init int selinux_init(void)
7094{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07007095 if (!security_module_enable("selinux")) {
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02007096 selinux_enabled = 0;
7097 return 0;
7098 }
7099
Linus Torvalds1da177e2005-04-16 15:20:36 -07007100 if (!selinux_enabled) {
peter enderborgc103a912018-06-12 10:09:03 +02007101 pr_info("SELinux: Disabled at boot.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007102 return 0;
7103 }
7104
peter enderborgc103a912018-06-12 10:09:03 +02007105 pr_info("SELinux: Initializing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007106
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007107 memset(&selinux_state, 0, sizeof(selinux_state));
Paul Mooree5a5ca92018-03-01 17:38:30 -05007108 enforcing_set(&selinux_state, selinux_enforcing_boot);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007109 selinux_state.checkreqprot = selinux_checkreqprot_boot;
7110 selinux_ss_init(&selinux_state.ss);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05007111 selinux_avc_init(&selinux_state.avc);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007112
Linus Torvalds1da177e2005-04-16 15:20:36 -07007113 /* Set the security state for the initial task. */
David Howellsd84f4f92008-11-14 10:39:23 +11007114 cred_init_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007115
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04007116 default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
7117
James Morris7cae7e22006-03-22 00:09:22 -08007118 sel_inode_cache = kmem_cache_create("selinux_inode_security",
7119 sizeof(struct inode_security_struct),
Paul Mundt20c2df82007-07-20 10:11:58 +09007120 0, SLAB_PANIC, NULL);
Sangwoo63205652015-10-21 17:44:30 -04007121 file_security_cache = kmem_cache_create("selinux_file_security",
7122 sizeof(struct file_security_struct),
7123 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007124 avc_init();
7125
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007126 avtab_cache_init();
7127
7128 ebitmap_cache_init();
7129
7130 hashtab_cache_init();
7131
Casey Schauflerd69dece52017-01-18 17:09:05 -08007132 security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks), "selinux");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007133
Paul Moore615e51f2014-06-26 14:33:56 -04007134 if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET))
7135 panic("SELinux: Unable to register AVC netcache callback\n");
7136
Daniel Jurgens8f408ab2017-05-19 15:48:53 +03007137 if (avc_add_callback(selinux_lsm_notifier_avc_callback, AVC_CALLBACK_RESET))
7138 panic("SELinux: Unable to register AVC LSM notifier callback\n");
7139
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007140 if (selinux_enforcing_boot)
peter enderborgc103a912018-06-12 10:09:03 +02007141 pr_debug("SELinux: Starting in enforcing mode\n");
Eric Paris828dfe12008-04-17 13:17:49 -04007142 else
peter enderborgc103a912018-06-12 10:09:03 +02007143 pr_debug("SELinux: Starting in permissive mode\n");
Michael LeMayd7200242006-06-22 14:47:17 -07007144
Linus Torvalds1da177e2005-04-16 15:20:36 -07007145 return 0;
7146}
7147
Al Viroe8c26252010-03-23 06:36:54 -04007148static void delayed_superblock_init(struct super_block *sb, void *unused)
7149{
Al Viro17f3b552018-11-10 23:46:07 -05007150 struct security_mnt_opts opts;
7151
7152 security_init_mnt_opts(&opts);
7153 selinux_set_mnt_opts(sb, &opts, 0, NULL);
7154 security_free_mnt_opts(&opts);
Al Viroe8c26252010-03-23 06:36:54 -04007155}
7156
Linus Torvalds1da177e2005-04-16 15:20:36 -07007157void selinux_complete_init(void)
7158{
peter enderborgc103a912018-06-12 10:09:03 +02007159 pr_debug("SELinux: Completing initialization.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007160
7161 /* Set up any superblocks initialized prior to the policy load. */
peter enderborgc103a912018-06-12 10:09:03 +02007162 pr_debug("SELinux: Setting up existing superblocks.\n");
Al Viroe8c26252010-03-23 06:36:54 -04007163 iterate_supers(delayed_superblock_init, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007164}
7165
7166/* SELinux requires early initialization in order to label
7167 all processes and objects when they are created. */
Kees Cook3d6e5f62018-10-10 17:18:23 -07007168DEFINE_LSM(selinux) = {
Kees Cook07aed2f2018-10-10 17:18:24 -07007169 .name = "selinux",
Kees Cook3d6e5f62018-10-10 17:18:23 -07007170 .init = selinux_init,
7171};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007172
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007173#if defined(CONFIG_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007174
Florian Westphal591bb272017-07-26 11:40:52 +02007175static const struct nf_hook_ops selinux_nf_ops[] = {
Paul Mooreeffad8d2008-01-29 08:49:27 -05007176 {
7177 .hook = selinux_ipv4_postroute,
Alban Crequy2597a832012-05-14 03:56:39 +00007178 .pf = NFPROTO_IPV4,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007179 .hooknum = NF_INET_POST_ROUTING,
7180 .priority = NF_IP_PRI_SELINUX_LAST,
7181 },
7182 {
7183 .hook = selinux_ipv4_forward,
Alban Crequy2597a832012-05-14 03:56:39 +00007184 .pf = NFPROTO_IPV4,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007185 .hooknum = NF_INET_FORWARD,
7186 .priority = NF_IP_PRI_SELINUX_FIRST,
Paul Moore948bf852008-10-10 10:16:32 -04007187 },
7188 {
7189 .hook = selinux_ipv4_output,
Alban Crequy2597a832012-05-14 03:56:39 +00007190 .pf = NFPROTO_IPV4,
Paul Moore948bf852008-10-10 10:16:32 -04007191 .hooknum = NF_INET_LOCAL_OUT,
7192 .priority = NF_IP_PRI_SELINUX_FIRST,
Jiri Pirko25db6be2014-09-03 17:42:13 +02007193 },
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04007194#if IS_ENABLED(CONFIG_IPV6)
Paul Mooreeffad8d2008-01-29 08:49:27 -05007195 {
7196 .hook = selinux_ipv6_postroute,
Alban Crequy2597a832012-05-14 03:56:39 +00007197 .pf = NFPROTO_IPV6,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007198 .hooknum = NF_INET_POST_ROUTING,
7199 .priority = NF_IP6_PRI_SELINUX_LAST,
7200 },
7201 {
7202 .hook = selinux_ipv6_forward,
Alban Crequy2597a832012-05-14 03:56:39 +00007203 .pf = NFPROTO_IPV6,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007204 .hooknum = NF_INET_FORWARD,
7205 .priority = NF_IP6_PRI_SELINUX_FIRST,
Jiri Pirko25db6be2014-09-03 17:42:13 +02007206 },
Huw Davies2917f572016-06-27 15:06:15 -04007207 {
7208 .hook = selinux_ipv6_output,
7209 .pf = NFPROTO_IPV6,
7210 .hooknum = NF_INET_LOCAL_OUT,
7211 .priority = NF_IP6_PRI_SELINUX_FIRST,
7212 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07007213#endif /* IPV6 */
Jiri Pirko25db6be2014-09-03 17:42:13 +02007214};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007215
Florian Westphal8e71bf72017-04-21 11:49:09 +02007216static int __net_init selinux_nf_register(struct net *net)
7217{
7218 return nf_register_net_hooks(net, selinux_nf_ops,
7219 ARRAY_SIZE(selinux_nf_ops));
7220}
7221
7222static void __net_exit selinux_nf_unregister(struct net *net)
7223{
7224 nf_unregister_net_hooks(net, selinux_nf_ops,
7225 ARRAY_SIZE(selinux_nf_ops));
7226}
7227
7228static struct pernet_operations selinux_net_ops = {
7229 .init = selinux_nf_register,
7230 .exit = selinux_nf_unregister,
7231};
7232
Linus Torvalds1da177e2005-04-16 15:20:36 -07007233static int __init selinux_nf_ip_init(void)
7234{
Jiri Pirko25db6be2014-09-03 17:42:13 +02007235 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007236
7237 if (!selinux_enabled)
Jiri Pirko25db6be2014-09-03 17:42:13 +02007238 return 0;
Eric Parisfadcdb42007-02-22 18:11:31 -05007239
peter enderborgc103a912018-06-12 10:09:03 +02007240 pr_debug("SELinux: Registering netfilter hooks\n");
Eric Parisfadcdb42007-02-22 18:11:31 -05007241
Florian Westphal8e71bf72017-04-21 11:49:09 +02007242 err = register_pernet_subsys(&selinux_net_ops);
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07007243 if (err)
Florian Westphal8e71bf72017-04-21 11:49:09 +02007244 panic("SELinux: register_pernet_subsys: error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007245
Jiri Pirko25db6be2014-09-03 17:42:13 +02007246 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007247}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007248__initcall(selinux_nf_ip_init);
7249
7250#ifdef CONFIG_SECURITY_SELINUX_DISABLE
7251static void selinux_nf_ip_exit(void)
7252{
peter enderborgc103a912018-06-12 10:09:03 +02007253 pr_debug("SELinux: Unregistering netfilter hooks\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007254
Florian Westphal8e71bf72017-04-21 11:49:09 +02007255 unregister_pernet_subsys(&selinux_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007256}
7257#endif
7258
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007259#else /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007260
7261#ifdef CONFIG_SECURITY_SELINUX_DISABLE
7262#define selinux_nf_ip_exit()
7263#endif
7264
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007265#endif /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007266
7267#ifdef CONFIG_SECURITY_SELINUX_DISABLE
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007268int selinux_disable(struct selinux_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007269{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007270 if (state->initialized) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007271 /* Not permitted after initial policy load. */
7272 return -EINVAL;
7273 }
7274
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007275 if (state->disabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007276 /* Only do this once. */
7277 return -EINVAL;
7278 }
7279
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007280 state->disabled = 1;
7281
peter enderborgc103a912018-06-12 10:09:03 +02007282 pr_info("SELinux: Disabled at runtime.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007283
Stephen Smalley30d55282006-05-03 10:52:36 -04007284 selinux_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007285
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07007286 security_delete_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007287
Eric Parisaf8ff042009-09-20 21:23:01 -04007288 /* Try to destroy the avc node cache */
7289 avc_disable();
7290
Linus Torvalds1da177e2005-04-16 15:20:36 -07007291 /* Unregister netfilter hooks. */
7292 selinux_nf_ip_exit();
7293
7294 /* Unregister selinuxfs. */
7295 exit_sel_fs();
7296
7297 return 0;
7298}
7299#endif