blob: 11cf2feb27b396a7da90f8e39c6fa69530aa6e9a [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
Eric Parisc9180a52007-11-30 13:00:35 -0500594static int bad_option(struct superblock_security_struct *sbsec, char flag,
595 u32 old_sid, u32 new_sid)
596{
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500597 char mnt_flags = sbsec->flags & SE_MNTMASK;
598
Eric Parisc9180a52007-11-30 13:00:35 -0500599 /* check if the old mount command had the same options */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500600 if (sbsec->flags & SE_SBINITIALIZED)
Eric Parisc9180a52007-11-30 13:00:35 -0500601 if (!(sbsec->flags & flag) ||
602 (old_sid != new_sid))
603 return 1;
604
605 /* check if we were passed the same options twice,
606 * aka someone passed context=a,context=b
607 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500608 if (!(sbsec->flags & SE_SBINITIALIZED))
609 if (mnt_flags & flag)
Eric Parisc9180a52007-11-30 13:00:35 -0500610 return 1;
611 return 0;
612}
Eric Parise0007522008-03-05 10:31:54 -0500613
Eric Parisc9180a52007-11-30 13:00:35 -0500614/*
615 * Allow filesystems with binary mount data to explicitly set mount point
616 * labeling information.
617 */
Eric Parise0007522008-03-05 10:31:54 -0500618static int selinux_set_mnt_opts(struct super_block *sb,
David Quigley649f6e72013-05-22 12:50:36 -0400619 struct security_mnt_opts *opts,
620 unsigned long kern_flags,
621 unsigned long *set_kern_flags)
Eric Parisc9180a52007-11-30 13:00:35 -0500622{
David Howells275bb412008-11-14 10:39:19 +1100623 const struct cred *cred = current_cred();
Eric Parisc9180a52007-11-30 13:00:35 -0500624 int rc = 0, i;
Eric Parisc9180a52007-11-30 13:00:35 -0500625 struct superblock_security_struct *sbsec = sb->s_security;
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800626 const char *name = sb->s_type->name;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500627 struct dentry *root = sbsec->sb->s_root;
Paul Moore2c971652016-04-19 16:36:28 -0400628 struct inode_security_struct *root_isec;
Eric Parisc9180a52007-11-30 13:00:35 -0500629 u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
630 u32 defcontext_sid = 0;
Eric Parise0007522008-03-05 10:31:54 -0500631 char **mount_options = opts->mnt_opts;
632 int *flags = opts->mnt_opts_flags;
633 int num_opts = opts->num_mnt_opts;
Eric Parisc9180a52007-11-30 13:00:35 -0500634
635 mutex_lock(&sbsec->lock);
636
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500637 if (!selinux_state.initialized) {
Eric Parisc9180a52007-11-30 13:00:35 -0500638 if (!num_opts) {
639 /* Defer initialization until selinux_complete_init,
640 after the initial policy is loaded and the security
641 server is ready to handle calls. */
Eric Parisc9180a52007-11-30 13:00:35 -0500642 goto out;
643 }
644 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +0200645 pr_warn("SELinux: Unable to set superblock options "
Eric Paris744ba352008-04-17 11:52:44 -0400646 "before the security server is initialized\n");
Eric Parisc9180a52007-11-30 13:00:35 -0500647 goto out;
648 }
David Quigley649f6e72013-05-22 12:50:36 -0400649 if (kern_flags && !set_kern_flags) {
650 /* Specifying internal flags without providing a place to
651 * place the results is not allowed */
652 rc = -EINVAL;
653 goto out;
654 }
Eric Parisc9180a52007-11-30 13:00:35 -0500655
656 /*
Eric Parise0007522008-03-05 10:31:54 -0500657 * Binary mount data FS will come through this function twice. Once
658 * from an explicit call and once from the generic calls from the vfs.
659 * Since the generic VFS calls will not contain any security mount data
660 * we need to skip the double mount verification.
661 *
662 * This does open a hole in which we will not notice if the first
663 * mount using this sb set explict options and a second mount using
664 * this sb does not set any security options. (The first options
665 * will be used for both mounts)
666 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500667 if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
Eric Parise0007522008-03-05 10:31:54 -0500668 && (num_opts == 0))
Eric Parisf5269712008-05-14 11:27:45 -0400669 goto out;
Eric Parise0007522008-03-05 10:31:54 -0500670
Paul Moore2c971652016-04-19 16:36:28 -0400671 root_isec = backing_inode_security_novalidate(root);
672
Eric Parise0007522008-03-05 10:31:54 -0500673 /*
Eric Parisc9180a52007-11-30 13:00:35 -0500674 * parse the mount options, check if they are valid sids.
675 * also check if someone is trying to mount the same sb more
676 * than once with different security options.
677 */
678 for (i = 0; i < num_opts; i++) {
679 u32 sid;
David P. Quigley11689d42009-01-16 09:22:03 -0500680
Eric Paris12f348b2012-10-09 10:56:25 -0400681 if (flags[i] == SBLABEL_MNT)
David P. Quigley11689d42009-01-16 09:22:03 -0500682 continue;
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500683 rc = security_context_str_to_sid(&selinux_state,
684 mount_options[i], &sid,
685 GFP_KERNEL);
Eric Parisc9180a52007-11-30 13:00:35 -0500686 if (rc) {
peter enderborgc103a912018-06-12 10:09:03 +0200687 pr_warn("SELinux: security_context_str_to_sid"
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800688 "(%s) failed for (dev %s, type %s) errno=%d\n",
689 mount_options[i], sb->s_id, name, rc);
Eric Parisc9180a52007-11-30 13:00:35 -0500690 goto out;
691 }
692 switch (flags[i]) {
693 case FSCONTEXT_MNT:
694 fscontext_sid = sid;
695
696 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
697 fscontext_sid))
698 goto out_double_mount;
699
700 sbsec->flags |= FSCONTEXT_MNT;
701 break;
702 case CONTEXT_MNT:
703 context_sid = sid;
704
705 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
706 context_sid))
707 goto out_double_mount;
708
709 sbsec->flags |= CONTEXT_MNT;
710 break;
711 case ROOTCONTEXT_MNT:
712 rootcontext_sid = sid;
713
714 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
715 rootcontext_sid))
716 goto out_double_mount;
717
718 sbsec->flags |= ROOTCONTEXT_MNT;
719
720 break;
721 case DEFCONTEXT_MNT:
722 defcontext_sid = sid;
723
724 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
725 defcontext_sid))
726 goto out_double_mount;
727
728 sbsec->flags |= DEFCONTEXT_MNT;
729
730 break;
731 default:
732 rc = -EINVAL;
733 goto out;
734 }
735 }
736
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500737 if (sbsec->flags & SE_SBINITIALIZED) {
Eric Parisc9180a52007-11-30 13:00:35 -0500738 /* previously mounted with options, but not on this attempt? */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500739 if ((sbsec->flags & SE_MNTMASK) && !num_opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500740 goto out_double_mount;
741 rc = 0;
742 goto out;
743 }
744
James Morris089be432008-07-15 18:32:49 +1000745 if (strcmp(sb->s_type->name, "proc") == 0)
Stephen Smalley134509d2015-06-04 16:22:17 -0400746 sbsec->flags |= SE_SBPROC | SE_SBGENFS;
747
Stephen Smalley8e014722015-06-04 16:22:17 -0400748 if (!strcmp(sb->s_type->name, "debugfs") ||
Jeff Vander Stoep6a391182017-06-20 09:35:33 -0700749 !strcmp(sb->s_type->name, "tracefs") ||
Stephen Smalley8e014722015-06-04 16:22:17 -0400750 !strcmp(sb->s_type->name, "sysfs") ||
Antonio Murdaca901ef842017-02-09 17:02:42 +0100751 !strcmp(sb->s_type->name, "pstore") ||
752 !strcmp(sb->s_type->name, "cgroup") ||
753 !strcmp(sb->s_type->name, "cgroup2"))
Stephen Smalley134509d2015-06-04 16:22:17 -0400754 sbsec->flags |= SE_SBGENFS;
Eric Parisc9180a52007-11-30 13:00:35 -0500755
David Quigleyeb9ae682013-05-22 12:50:37 -0400756 if (!sbsec->behavior) {
757 /*
758 * Determine the labeling behavior to use for this
759 * filesystem type.
760 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500761 rc = security_fs_use(&selinux_state, sb);
David Quigleyeb9ae682013-05-22 12:50:37 -0400762 if (rc) {
peter enderborgc103a912018-06-12 10:09:03 +0200763 pr_warn("%s: security_fs_use(%s) returned %d\n",
David Quigleyeb9ae682013-05-22 12:50:37 -0400764 __func__, sb->s_type->name, rc);
765 goto out;
766 }
Eric Parisc9180a52007-11-30 13:00:35 -0500767 }
Seth Forsheeaad82892016-04-26 14:36:20 -0500768
769 /*
Stephen Smalley01593d32017-01-09 10:07:31 -0500770 * If this is a user namespace mount and the filesystem type is not
771 * explicitly whitelisted, then no contexts are allowed on the command
772 * line and security labels must be ignored.
Seth Forsheeaad82892016-04-26 14:36:20 -0500773 */
Stephen Smalley01593d32017-01-09 10:07:31 -0500774 if (sb->s_user_ns != &init_user_ns &&
775 strcmp(sb->s_type->name, "tmpfs") &&
776 strcmp(sb->s_type->name, "ramfs") &&
777 strcmp(sb->s_type->name, "devpts")) {
Seth Forsheeaad82892016-04-26 14:36:20 -0500778 if (context_sid || fscontext_sid || rootcontext_sid ||
779 defcontext_sid) {
780 rc = -EACCES;
781 goto out;
782 }
783 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
784 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500785 rc = security_transition_sid(&selinux_state,
786 current_sid(),
787 current_sid(),
Seth Forsheeaad82892016-04-26 14:36:20 -0500788 SECCLASS_FILE, NULL,
789 &sbsec->mntpoint_sid);
790 if (rc)
791 goto out;
792 }
793 goto out_set_opts;
794 }
795
Eric Parisc9180a52007-11-30 13:00:35 -0500796 /* sets the context of the superblock for the fs being mounted. */
797 if (fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100798 rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500799 if (rc)
800 goto out;
801
802 sbsec->sid = fscontext_sid;
803 }
804
805 /*
806 * Switch to using mount point labeling behavior.
807 * sets the label used on all file below the mountpoint, and will set
808 * the superblock context if not already set.
809 */
David Quigleyeb9ae682013-05-22 12:50:37 -0400810 if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !context_sid) {
811 sbsec->behavior = SECURITY_FS_USE_NATIVE;
812 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
813 }
814
Eric Parisc9180a52007-11-30 13:00:35 -0500815 if (context_sid) {
816 if (!fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100817 rc = may_context_mount_sb_relabel(context_sid, sbsec,
818 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500819 if (rc)
820 goto out;
821 sbsec->sid = context_sid;
822 } else {
David Howells275bb412008-11-14 10:39:19 +1100823 rc = may_context_mount_inode_relabel(context_sid, sbsec,
824 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500825 if (rc)
826 goto out;
827 }
828 if (!rootcontext_sid)
829 rootcontext_sid = context_sid;
830
831 sbsec->mntpoint_sid = context_sid;
832 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
833 }
834
835 if (rootcontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100836 rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec,
837 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500838 if (rc)
839 goto out;
840
841 root_isec->sid = rootcontext_sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -0500842 root_isec->initialized = LABEL_INITIALIZED;
Eric Parisc9180a52007-11-30 13:00:35 -0500843 }
844
845 if (defcontext_sid) {
David Quigleyeb9ae682013-05-22 12:50:37 -0400846 if (sbsec->behavior != SECURITY_FS_USE_XATTR &&
847 sbsec->behavior != SECURITY_FS_USE_NATIVE) {
Eric Parisc9180a52007-11-30 13:00:35 -0500848 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +0200849 pr_warn("SELinux: defcontext option is "
Eric Parisc9180a52007-11-30 13:00:35 -0500850 "invalid for this filesystem type\n");
851 goto out;
852 }
853
854 if (defcontext_sid != sbsec->def_sid) {
855 rc = may_context_mount_inode_relabel(defcontext_sid,
David Howells275bb412008-11-14 10:39:19 +1100856 sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500857 if (rc)
858 goto out;
859 }
860
861 sbsec->def_sid = defcontext_sid;
862 }
863
Seth Forsheeaad82892016-04-26 14:36:20 -0500864out_set_opts:
Eric Parisc9180a52007-11-30 13:00:35 -0500865 rc = sb_finish_set_opts(sb);
866out:
Eric Parisbc7e9822006-09-25 23:32:02 -0700867 mutex_unlock(&sbsec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 return rc;
Eric Parisc9180a52007-11-30 13:00:35 -0500869out_double_mount:
870 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +0200871 pr_warn("SELinux: mount invalid. Same superblock, different "
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800872 "security settings for (dev %s, type %s)\n", sb->s_id, name);
Eric Parisc9180a52007-11-30 13:00:35 -0500873 goto out;
874}
875
Jeff Layton094f7b62013-04-01 08:14:24 -0400876static int selinux_cmp_sb_context(const struct super_block *oldsb,
877 const struct super_block *newsb)
878{
879 struct superblock_security_struct *old = oldsb->s_security;
880 struct superblock_security_struct *new = newsb->s_security;
881 char oldflags = old->flags & SE_MNTMASK;
882 char newflags = new->flags & SE_MNTMASK;
883
884 if (oldflags != newflags)
885 goto mismatch;
886 if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid)
887 goto mismatch;
888 if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid)
889 goto mismatch;
890 if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid)
891 goto mismatch;
892 if (oldflags & ROOTCONTEXT_MNT) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500893 struct inode_security_struct *oldroot = backing_inode_security(oldsb->s_root);
894 struct inode_security_struct *newroot = backing_inode_security(newsb->s_root);
Jeff Layton094f7b62013-04-01 08:14:24 -0400895 if (oldroot->sid != newroot->sid)
896 goto mismatch;
897 }
898 return 0;
899mismatch:
peter enderborgc103a912018-06-12 10:09:03 +0200900 pr_warn("SELinux: mount invalid. Same superblock, "
Jeff Layton094f7b62013-04-01 08:14:24 -0400901 "different security settings for (dev %s, "
902 "type %s)\n", newsb->s_id, newsb->s_type->name);
903 return -EBUSY;
904}
905
906static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400907 struct super_block *newsb,
908 unsigned long kern_flags,
909 unsigned long *set_kern_flags)
Eric Parisc9180a52007-11-30 13:00:35 -0500910{
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400911 int rc = 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500912 const struct superblock_security_struct *oldsbsec = oldsb->s_security;
913 struct superblock_security_struct *newsbsec = newsb->s_security;
914
915 int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT);
916 int set_context = (oldsbsec->flags & CONTEXT_MNT);
917 int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT);
918
Eric Paris0f5e6422008-04-21 16:24:11 -0400919 /*
920 * if the parent was able to be mounted it clearly had no special lsm
Al Viroe8c26252010-03-23 06:36:54 -0400921 * mount options. thus we can safely deal with this superblock later
Eric Paris0f5e6422008-04-21 16:24:11 -0400922 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500923 if (!selinux_state.initialized)
Jeff Layton094f7b62013-04-01 08:14:24 -0400924 return 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500925
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400926 /*
927 * Specifying internal flags without providing a place to
928 * place the results is not allowed.
929 */
930 if (kern_flags && !set_kern_flags)
931 return -EINVAL;
932
Eric Parisc9180a52007-11-30 13:00:35 -0500933 /* how can we clone if the old one wasn't set up?? */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500934 BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED));
Eric Parisc9180a52007-11-30 13:00:35 -0500935
Jeff Layton094f7b62013-04-01 08:14:24 -0400936 /* if fs is reusing a sb, make sure that the contexts match */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500937 if (newsbsec->flags & SE_SBINITIALIZED)
Jeff Layton094f7b62013-04-01 08:14:24 -0400938 return selinux_cmp_sb_context(oldsb, newsb);
Eric Paris5a552612008-04-09 14:08:35 -0400939
Eric Parisc9180a52007-11-30 13:00:35 -0500940 mutex_lock(&newsbsec->lock);
941
942 newsbsec->flags = oldsbsec->flags;
943
944 newsbsec->sid = oldsbsec->sid;
945 newsbsec->def_sid = oldsbsec->def_sid;
946 newsbsec->behavior = oldsbsec->behavior;
947
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400948 if (newsbsec->behavior == SECURITY_FS_USE_NATIVE &&
949 !(kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500950 rc = security_fs_use(&selinux_state, newsb);
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400951 if (rc)
952 goto out;
953 }
954
955 if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !set_context) {
956 newsbsec->behavior = SECURITY_FS_USE_NATIVE;
957 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
958 }
959
Eric Parisc9180a52007-11-30 13:00:35 -0500960 if (set_context) {
961 u32 sid = oldsbsec->mntpoint_sid;
962
963 if (!set_fscontext)
964 newsbsec->sid = sid;
965 if (!set_rootcontext) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500966 struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
Eric Parisc9180a52007-11-30 13:00:35 -0500967 newisec->sid = sid;
968 }
969 newsbsec->mntpoint_sid = sid;
970 }
971 if (set_rootcontext) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500972 const struct inode_security_struct *oldisec = backing_inode_security(oldsb->s_root);
973 struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
Eric Parisc9180a52007-11-30 13:00:35 -0500974
975 newisec->sid = oldisec->sid;
976 }
977
978 sb_finish_set_opts(newsb);
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400979out:
Eric Parisc9180a52007-11-30 13:00:35 -0500980 mutex_unlock(&newsbsec->lock);
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400981 return rc;
Eric Parisc9180a52007-11-30 13:00:35 -0500982}
983
Adrian Bunk2e1479d2008-03-17 22:29:23 +0200984static int selinux_parse_opts_str(char *options,
985 struct security_mnt_opts *opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500986{
Eric Parise0007522008-03-05 10:31:54 -0500987 char *p;
Eric Parisc9180a52007-11-30 13:00:35 -0500988 char *context = NULL, *defcontext = NULL;
989 char *fscontext = NULL, *rootcontext = NULL;
Eric Parise0007522008-03-05 10:31:54 -0500990 int rc, num_mnt_opts = 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500991
Eric Parise0007522008-03-05 10:31:54 -0500992 opts->num_mnt_opts = 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500993
994 /* Standard string-based options. */
995 while ((p = strsep(&options, "|")) != NULL) {
996 int token;
997 substring_t args[MAX_OPT_ARGS];
998
999 if (!*p)
1000 continue;
1001
1002 token = match_token(p, tokens, args);
1003
1004 switch (token) {
1005 case Opt_context:
1006 if (context || defcontext) {
1007 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +02001008 pr_warn(SEL_MOUNT_FAIL_MSG);
Eric Parisc9180a52007-11-30 13:00:35 -05001009 goto out_err;
1010 }
1011 context = match_strdup(&args[0]);
1012 if (!context) {
1013 rc = -ENOMEM;
1014 goto out_err;
1015 }
1016 break;
1017
1018 case Opt_fscontext:
1019 if (fscontext) {
1020 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +02001021 pr_warn(SEL_MOUNT_FAIL_MSG);
Eric Parisc9180a52007-11-30 13:00:35 -05001022 goto out_err;
1023 }
1024 fscontext = match_strdup(&args[0]);
1025 if (!fscontext) {
1026 rc = -ENOMEM;
1027 goto out_err;
1028 }
1029 break;
1030
1031 case Opt_rootcontext:
1032 if (rootcontext) {
1033 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +02001034 pr_warn(SEL_MOUNT_FAIL_MSG);
Eric Parisc9180a52007-11-30 13:00:35 -05001035 goto out_err;
1036 }
1037 rootcontext = match_strdup(&args[0]);
1038 if (!rootcontext) {
1039 rc = -ENOMEM;
1040 goto out_err;
1041 }
1042 break;
1043
1044 case Opt_defcontext:
1045 if (context || defcontext) {
1046 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +02001047 pr_warn(SEL_MOUNT_FAIL_MSG);
Eric Parisc9180a52007-11-30 13:00:35 -05001048 goto out_err;
1049 }
1050 defcontext = match_strdup(&args[0]);
1051 if (!defcontext) {
1052 rc = -ENOMEM;
1053 goto out_err;
1054 }
1055 break;
David P. Quigley11689d42009-01-16 09:22:03 -05001056 case Opt_labelsupport:
1057 break;
Eric Parisc9180a52007-11-30 13:00:35 -05001058 default:
1059 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +02001060 pr_warn("SELinux: unknown mount option\n");
Eric Parisc9180a52007-11-30 13:00:35 -05001061 goto out_err;
1062
1063 }
1064 }
1065
Eric Parise0007522008-03-05 10:31:54 -05001066 rc = -ENOMEM;
Tetsuo Handa8931c3b2016-11-14 20:16:12 +09001067 opts->mnt_opts = kcalloc(NUM_SEL_MNT_OPTS, sizeof(char *), GFP_KERNEL);
Eric Parise0007522008-03-05 10:31:54 -05001068 if (!opts->mnt_opts)
1069 goto out_err;
1070
Tetsuo Handa8931c3b2016-11-14 20:16:12 +09001071 opts->mnt_opts_flags = kcalloc(NUM_SEL_MNT_OPTS, sizeof(int),
1072 GFP_KERNEL);
Paul Moore023f1082017-06-07 16:48:19 -04001073 if (!opts->mnt_opts_flags)
Eric Parise0007522008-03-05 10:31:54 -05001074 goto out_err;
Eric Parisc9180a52007-11-30 13:00:35 -05001075
Eric Parise0007522008-03-05 10:31:54 -05001076 if (fscontext) {
1077 opts->mnt_opts[num_mnt_opts] = fscontext;
1078 opts->mnt_opts_flags[num_mnt_opts++] = FSCONTEXT_MNT;
1079 }
1080 if (context) {
1081 opts->mnt_opts[num_mnt_opts] = context;
1082 opts->mnt_opts_flags[num_mnt_opts++] = CONTEXT_MNT;
1083 }
1084 if (rootcontext) {
1085 opts->mnt_opts[num_mnt_opts] = rootcontext;
1086 opts->mnt_opts_flags[num_mnt_opts++] = ROOTCONTEXT_MNT;
1087 }
1088 if (defcontext) {
1089 opts->mnt_opts[num_mnt_opts] = defcontext;
1090 opts->mnt_opts_flags[num_mnt_opts++] = DEFCONTEXT_MNT;
1091 }
1092
1093 opts->num_mnt_opts = num_mnt_opts;
1094 return 0;
1095
Eric Parisc9180a52007-11-30 13:00:35 -05001096out_err:
Paul Moore023f1082017-06-07 16:48:19 -04001097 security_free_mnt_opts(opts);
Eric Parisc9180a52007-11-30 13:00:35 -05001098 kfree(context);
1099 kfree(defcontext);
1100 kfree(fscontext);
1101 kfree(rootcontext);
1102 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103}
1104
Al Viroe3489f82018-12-13 00:24:36 -05001105static int show_sid(struct seq_file *m, u32 sid)
Eric Paris2069f452008-07-04 09:47:13 +10001106{
Al Viroe3489f82018-12-13 00:24:36 -05001107 char *context = NULL;
1108 u32 len;
1109 int rc;
Eric Paris2069f452008-07-04 09:47:13 +10001110
Al Viroe3489f82018-12-13 00:24:36 -05001111 rc = security_sid_to_context(&selinux_state, sid,
1112 &context, &len);
1113 if (!rc) {
1114 bool has_comma = context && strchr(context, ',');
David P. Quigley11689d42009-01-16 09:22:03 -05001115
Eric Paris2069f452008-07-04 09:47:13 +10001116 if (has_comma)
1117 seq_putc(m, '\"');
Al Viroe3489f82018-12-13 00:24:36 -05001118 seq_escape(m, context, "\"\n\\");
Eric Paris2069f452008-07-04 09:47:13 +10001119 if (has_comma)
1120 seq_putc(m, '\"');
1121 }
Al Viroe3489f82018-12-13 00:24:36 -05001122 kfree(context);
1123 return rc;
Eric Paris2069f452008-07-04 09:47:13 +10001124}
1125
1126static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
1127{
Al Viroe3489f82018-12-13 00:24:36 -05001128 struct superblock_security_struct *sbsec = sb->s_security;
Eric Paris2069f452008-07-04 09:47:13 +10001129 int rc;
1130
Al Viroe3489f82018-12-13 00:24:36 -05001131 if (!(sbsec->flags & SE_SBINITIALIZED))
1132 return 0;
1133
1134 if (!selinux_state.initialized)
1135 return 0;
1136
1137 if (sbsec->flags & FSCONTEXT_MNT) {
1138 seq_putc(m, ',');
1139 seq_puts(m, FSCONTEXT_STR);
1140 rc = show_sid(m, sbsec->sid);
1141 if (rc)
1142 return rc;
Eric Paris383795c2008-07-29 17:07:26 -04001143 }
Al Viroe3489f82018-12-13 00:24:36 -05001144 if (sbsec->flags & CONTEXT_MNT) {
1145 seq_putc(m, ',');
1146 seq_puts(m, CONTEXT_STR);
1147 rc = show_sid(m, sbsec->mntpoint_sid);
1148 if (rc)
1149 return rc;
1150 }
1151 if (sbsec->flags & DEFCONTEXT_MNT) {
1152 seq_putc(m, ',');
1153 seq_puts(m, DEFCONTEXT_STR);
1154 rc = show_sid(m, sbsec->def_sid);
1155 if (rc)
1156 return rc;
1157 }
1158 if (sbsec->flags & ROOTCONTEXT_MNT) {
1159 struct dentry *root = sbsec->sb->s_root;
1160 struct inode_security_struct *isec = backing_inode_security(root);
1161 seq_putc(m, ',');
1162 seq_puts(m, ROOTCONTEXT_STR);
1163 rc = show_sid(m, isec->sid);
1164 if (rc)
1165 return rc;
1166 }
1167 if (sbsec->flags & SBLABEL_MNT) {
1168 seq_putc(m, ',');
1169 seq_puts(m, LABELSUPP_STR);
1170 }
1171 return 0;
Eric Paris2069f452008-07-04 09:47:13 +10001172}
1173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174static inline u16 inode_mode_to_security_class(umode_t mode)
1175{
1176 switch (mode & S_IFMT) {
1177 case S_IFSOCK:
1178 return SECCLASS_SOCK_FILE;
1179 case S_IFLNK:
1180 return SECCLASS_LNK_FILE;
1181 case S_IFREG:
1182 return SECCLASS_FILE;
1183 case S_IFBLK:
1184 return SECCLASS_BLK_FILE;
1185 case S_IFDIR:
1186 return SECCLASS_DIR;
1187 case S_IFCHR:
1188 return SECCLASS_CHR_FILE;
1189 case S_IFIFO:
1190 return SECCLASS_FIFO_FILE;
1191
1192 }
1193
1194 return SECCLASS_FILE;
1195}
1196
James Morris13402582005-09-30 14:24:34 -04001197static inline int default_protocol_stream(int protocol)
1198{
1199 return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP);
1200}
1201
1202static inline int default_protocol_dgram(int protocol)
1203{
1204 return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
1205}
1206
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207static inline u16 socket_type_to_security_class(int family, int type, int protocol)
1208{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001209 int extsockclass = selinux_policycap_extsockclass();
Stephen Smalleyda69a532017-01-09 10:07:30 -05001210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 switch (family) {
1212 case PF_UNIX:
1213 switch (type) {
1214 case SOCK_STREAM:
1215 case SOCK_SEQPACKET:
1216 return SECCLASS_UNIX_STREAM_SOCKET;
1217 case SOCK_DGRAM:
Luis Ressel2a764b52017-07-25 15:13:41 -04001218 case SOCK_RAW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 return SECCLASS_UNIX_DGRAM_SOCKET;
1220 }
1221 break;
1222 case PF_INET:
1223 case PF_INET6:
1224 switch (type) {
1225 case SOCK_STREAM:
Stephen Smalleyda69a532017-01-09 10:07:30 -05001226 case SOCK_SEQPACKET:
James Morris13402582005-09-30 14:24:34 -04001227 if (default_protocol_stream(protocol))
1228 return SECCLASS_TCP_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001229 else if (extsockclass && protocol == IPPROTO_SCTP)
1230 return SECCLASS_SCTP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001231 else
1232 return SECCLASS_RAWIP_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 case SOCK_DGRAM:
James Morris13402582005-09-30 14:24:34 -04001234 if (default_protocol_dgram(protocol))
1235 return SECCLASS_UDP_SOCKET;
Stephen Smalleyef379792017-01-09 10:07:31 -05001236 else if (extsockclass && (protocol == IPPROTO_ICMP ||
1237 protocol == IPPROTO_ICMPV6))
Stephen Smalleyda69a532017-01-09 10:07:30 -05001238 return SECCLASS_ICMP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001239 else
1240 return SECCLASS_RAWIP_SOCKET;
James Morris2ee92d42006-11-13 16:09:01 -08001241 case SOCK_DCCP:
1242 return SECCLASS_DCCP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001243 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 return SECCLASS_RAWIP_SOCKET;
1245 }
1246 break;
1247 case PF_NETLINK:
1248 switch (protocol) {
1249 case NETLINK_ROUTE:
1250 return SECCLASS_NETLINK_ROUTE_SOCKET;
Pavel Emelyanov7f1fb602011-12-06 07:56:43 +00001251 case NETLINK_SOCK_DIAG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 return SECCLASS_NETLINK_TCPDIAG_SOCKET;
1253 case NETLINK_NFLOG:
1254 return SECCLASS_NETLINK_NFLOG_SOCKET;
1255 case NETLINK_XFRM:
1256 return SECCLASS_NETLINK_XFRM_SOCKET;
1257 case NETLINK_SELINUX:
1258 return SECCLASS_NETLINK_SELINUX_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001259 case NETLINK_ISCSI:
1260 return SECCLASS_NETLINK_ISCSI_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 case NETLINK_AUDIT:
1262 return SECCLASS_NETLINK_AUDIT_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001263 case NETLINK_FIB_LOOKUP:
1264 return SECCLASS_NETLINK_FIB_LOOKUP_SOCKET;
1265 case NETLINK_CONNECTOR:
1266 return SECCLASS_NETLINK_CONNECTOR_SOCKET;
1267 case NETLINK_NETFILTER:
1268 return SECCLASS_NETLINK_NETFILTER_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 case NETLINK_DNRTMSG:
1270 return SECCLASS_NETLINK_DNRT_SOCKET;
James Morris0c9b7942005-04-16 15:24:13 -07001271 case NETLINK_KOBJECT_UEVENT:
1272 return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001273 case NETLINK_GENERIC:
1274 return SECCLASS_NETLINK_GENERIC_SOCKET;
1275 case NETLINK_SCSITRANSPORT:
1276 return SECCLASS_NETLINK_SCSITRANSPORT_SOCKET;
1277 case NETLINK_RDMA:
1278 return SECCLASS_NETLINK_RDMA_SOCKET;
1279 case NETLINK_CRYPTO:
1280 return SECCLASS_NETLINK_CRYPTO_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 default:
1282 return SECCLASS_NETLINK_SOCKET;
1283 }
1284 case PF_PACKET:
1285 return SECCLASS_PACKET_SOCKET;
1286 case PF_KEY:
1287 return SECCLASS_KEY_SOCKET;
Christopher J. PeBenito3e3ff152006-06-09 00:25:03 -07001288 case PF_APPLETALK:
1289 return SECCLASS_APPLETALK_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 }
1291
Stephen Smalleyda69a532017-01-09 10:07:30 -05001292 if (extsockclass) {
1293 switch (family) {
1294 case PF_AX25:
1295 return SECCLASS_AX25_SOCKET;
1296 case PF_IPX:
1297 return SECCLASS_IPX_SOCKET;
1298 case PF_NETROM:
1299 return SECCLASS_NETROM_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001300 case PF_ATMPVC:
1301 return SECCLASS_ATMPVC_SOCKET;
1302 case PF_X25:
1303 return SECCLASS_X25_SOCKET;
1304 case PF_ROSE:
1305 return SECCLASS_ROSE_SOCKET;
1306 case PF_DECnet:
1307 return SECCLASS_DECNET_SOCKET;
1308 case PF_ATMSVC:
1309 return SECCLASS_ATMSVC_SOCKET;
1310 case PF_RDS:
1311 return SECCLASS_RDS_SOCKET;
1312 case PF_IRDA:
1313 return SECCLASS_IRDA_SOCKET;
1314 case PF_PPPOX:
1315 return SECCLASS_PPPOX_SOCKET;
1316 case PF_LLC:
1317 return SECCLASS_LLC_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001318 case PF_CAN:
1319 return SECCLASS_CAN_SOCKET;
1320 case PF_TIPC:
1321 return SECCLASS_TIPC_SOCKET;
1322 case PF_BLUETOOTH:
1323 return SECCLASS_BLUETOOTH_SOCKET;
1324 case PF_IUCV:
1325 return SECCLASS_IUCV_SOCKET;
1326 case PF_RXRPC:
1327 return SECCLASS_RXRPC_SOCKET;
1328 case PF_ISDN:
1329 return SECCLASS_ISDN_SOCKET;
1330 case PF_PHONET:
1331 return SECCLASS_PHONET_SOCKET;
1332 case PF_IEEE802154:
1333 return SECCLASS_IEEE802154_SOCKET;
1334 case PF_CAIF:
1335 return SECCLASS_CAIF_SOCKET;
1336 case PF_ALG:
1337 return SECCLASS_ALG_SOCKET;
1338 case PF_NFC:
1339 return SECCLASS_NFC_SOCKET;
1340 case PF_VSOCK:
1341 return SECCLASS_VSOCK_SOCKET;
1342 case PF_KCM:
1343 return SECCLASS_KCM_SOCKET;
1344 case PF_QIPCRTR:
1345 return SECCLASS_QIPCRTR_SOCKET;
Linus Torvalds3051bf32017-02-22 10:15:09 -08001346 case PF_SMC:
1347 return SECCLASS_SMC_SOCKET;
Björn Töpel68e8b842018-05-02 13:01:22 +02001348 case PF_XDP:
1349 return SECCLASS_XDP_SOCKET;
1350#if PF_MAX > 45
Stephen Smalleyda69a532017-01-09 10:07:30 -05001351#error New address family defined, please update this function.
1352#endif
1353 }
1354 }
1355
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 return SECCLASS_SOCKET;
1357}
1358
Stephen Smalley134509d2015-06-04 16:22:17 -04001359static int selinux_genfs_get_sid(struct dentry *dentry,
1360 u16 tclass,
1361 u16 flags,
1362 u32 *sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363{
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001364 int rc;
Al Virofc640052016-04-10 01:33:30 -04001365 struct super_block *sb = dentry->d_sb;
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001366 char *buffer, *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
Eric Paris828dfe12008-04-17 13:17:49 -04001368 buffer = (char *)__get_free_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 if (!buffer)
1370 return -ENOMEM;
1371
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001372 path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
1373 if (IS_ERR(path))
1374 rc = PTR_ERR(path);
1375 else {
Stephen Smalley134509d2015-06-04 16:22:17 -04001376 if (flags & SE_SBPROC) {
1377 /* each process gets a /proc/PID/ entry. Strip off the
1378 * PID part to get a valid selinux labeling.
1379 * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
1380 while (path[1] >= '0' && path[1] <= '9') {
1381 path[1] = '/';
1382 path++;
1383 }
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001384 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001385 rc = security_genfs_sid(&selinux_state, sb->s_type->name,
1386 path, tclass, sid);
Stephen Smalley7bb185e2018-09-04 16:51:36 -04001387 if (rc == -ENOENT) {
1388 /* No match in policy, mark as unlabeled. */
1389 *sid = SECINITSID_UNLABELED;
1390 rc = 0;
1391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 free_page((unsigned long)buffer);
1394 return rc;
1395}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
1397/* The inode's security attributes must be initialized before first use. */
1398static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
1399{
1400 struct superblock_security_struct *sbsec = NULL;
1401 struct inode_security_struct *isec = inode->i_security;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001402 u32 task_sid, sid = 0;
1403 u16 sclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 struct dentry *dentry;
1405#define INITCONTEXTLEN 255
1406 char *context = NULL;
1407 unsigned len = 0;
1408 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05001410 if (isec->initialized == LABEL_INITIALIZED)
Andreas Gruenbacher13457d02016-11-10 22:18:29 +01001411 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001413 spin_lock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05001414 if (isec->initialized == LABEL_INITIALIZED)
Eric Paris23970742006-09-25 23:32:01 -07001415 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
Andreas Gruenbacher13457d02016-11-10 22:18:29 +01001417 if (isec->sclass == SECCLASS_FILE)
1418 isec->sclass = inode_mode_to_security_class(inode->i_mode);
1419
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 sbsec = inode->i_sb->s_security;
David P. Quigley0d90a7e2009-01-16 09:22:02 -05001421 if (!(sbsec->flags & SE_SBINITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 /* Defer initialization until selinux_complete_init,
1423 after the initial policy is loaded and the security
1424 server is ready to handle calls. */
1425 spin_lock(&sbsec->isec_lock);
1426 if (list_empty(&isec->list))
1427 list_add(&isec->list, &sbsec->isec_head);
1428 spin_unlock(&sbsec->isec_lock);
Eric Paris23970742006-09-25 23:32:01 -07001429 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 }
1431
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001432 sclass = isec->sclass;
1433 task_sid = isec->task_sid;
1434 sid = isec->sid;
1435 isec->initialized = LABEL_PENDING;
1436 spin_unlock(&isec->lock);
1437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 switch (sbsec->behavior) {
David Quigleyeb9ae682013-05-22 12:50:37 -04001439 case SECURITY_FS_USE_NATIVE:
1440 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 case SECURITY_FS_USE_XATTR:
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001442 if (!(inode->i_opflags & IOP_XATTR)) {
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001443 sid = sbsec->def_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 break;
1445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 /* Need a dentry, since the xattr API requires one.
1447 Life would be simpler if we could just pass the inode. */
1448 if (opt_dentry) {
1449 /* Called from d_instantiate or d_splice_alias. */
1450 dentry = dget(opt_dentry);
1451 } else {
Al Virob1271252018-04-25 10:28:38 -04001452 /*
1453 * Called from selinux_complete_init, try to find a dentry.
1454 * Some filesystems really want a connected one, so try
1455 * that first. We could split SECURITY_FS_USE_XATTR in
1456 * two, depending upon that...
1457 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 dentry = d_find_alias(inode);
Al Virob1271252018-04-25 10:28:38 -04001459 if (!dentry)
1460 dentry = d_find_any_alias(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 }
1462 if (!dentry) {
Eric Parisdf7f54c2009-03-09 14:35:58 -04001463 /*
1464 * this is can be hit on boot when a file is accessed
1465 * before the policy is loaded. When we load policy we
1466 * may find inodes that have no dentry on the
1467 * sbsec->isec_head list. No reason to complain as these
1468 * will get fixed up the next time we go through
1469 * inode_doinit with a dentry, before these inodes could
1470 * be used again by userspace.
1471 */
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001472 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 }
1474
1475 len = INITCONTEXTLEN;
Eric Paris4cb912f2009-02-12 14:50:05 -05001476 context = kmalloc(len+1, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 if (!context) {
1478 rc = -ENOMEM;
1479 dput(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001480 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 }
Eric Paris4cb912f2009-02-12 14:50:05 -05001482 context[len] = '\0';
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001483 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 if (rc == -ERANGE) {
James Morris314dabb2009-08-10 22:00:13 +10001485 kfree(context);
1486
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 /* Need a larger buffer. Query for the right size. */
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001488 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 if (rc < 0) {
1490 dput(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001491 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 len = rc;
Eric Paris4cb912f2009-02-12 14:50:05 -05001494 context = kmalloc(len+1, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 if (!context) {
1496 rc = -ENOMEM;
1497 dput(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001498 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 }
Eric Paris4cb912f2009-02-12 14:50:05 -05001500 context[len] = '\0';
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001501 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 }
1503 dput(dentry);
1504 if (rc < 0) {
1505 if (rc != -ENODATA) {
peter enderborgc103a912018-06-12 10:09:03 +02001506 pr_warn("SELinux: %s: getxattr returned "
Harvey Harrisondd6f9532008-03-06 10:03:59 +11001507 "%d for dev=%s ino=%ld\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 -rc, inode->i_sb->s_id, inode->i_ino);
1509 kfree(context);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001510 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 }
1512 /* Map ENODATA to the default file SID */
1513 sid = sbsec->def_sid;
1514 rc = 0;
1515 } else {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001516 rc = security_context_to_sid_default(&selinux_state,
1517 context, rc, &sid,
Stephen Smalley869ab512008-04-04 08:46:05 -04001518 sbsec->def_sid,
1519 GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 if (rc) {
Eric Paris4ba0a8a2009-02-12 15:01:10 -05001521 char *dev = inode->i_sb->s_id;
1522 unsigned long ino = inode->i_ino;
1523
1524 if (rc == -EINVAL) {
1525 if (printk_ratelimit())
peter enderborgc103a912018-06-12 10:09:03 +02001526 pr_notice("SELinux: inode=%lu on dev=%s was found to have an invalid "
Eric Paris4ba0a8a2009-02-12 15:01:10 -05001527 "context=%s. This indicates you may need to relabel the inode or the "
1528 "filesystem in question.\n", ino, dev, context);
1529 } else {
peter enderborgc103a912018-06-12 10:09:03 +02001530 pr_warn("SELinux: %s: context_to_sid(%s) "
Eric Paris4ba0a8a2009-02-12 15:01:10 -05001531 "returned %d for dev=%s ino=%ld\n",
1532 __func__, context, -rc, dev, ino);
1533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 kfree(context);
1535 /* Leave with the unlabeled SID */
1536 rc = 0;
1537 break;
1538 }
1539 }
1540 kfree(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 break;
1542 case SECURITY_FS_USE_TASK:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001543 sid = task_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 break;
1545 case SECURITY_FS_USE_TRANS:
1546 /* Default to the fs SID. */
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001547 sid = sbsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
1549 /* Try to obtain a transition SID. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001550 rc = security_transition_sid(&selinux_state, task_sid, sid,
1551 sclass, NULL, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 if (rc)
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001553 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 break;
Eric Parisc312feb2006-07-10 04:43:53 -07001555 case SECURITY_FS_USE_MNTPOINT:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001556 sid = sbsec->mntpoint_sid;
Eric Parisc312feb2006-07-10 04:43:53 -07001557 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 default:
Eric Parisc312feb2006-07-10 04:43:53 -07001559 /* Default to the fs superblock SID. */
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001560 sid = sbsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Stephen Smalley134509d2015-06-04 16:22:17 -04001562 if ((sbsec->flags & SE_SBGENFS) && !S_ISLNK(inode->i_mode)) {
Paul Mooref64410e2014-03-19 16:46:18 -04001563 /* We must have a dentry to determine the label on
1564 * procfs inodes */
Al Virob1271252018-04-25 10:28:38 -04001565 if (opt_dentry) {
Paul Mooref64410e2014-03-19 16:46:18 -04001566 /* Called from d_instantiate or
1567 * d_splice_alias. */
1568 dentry = dget(opt_dentry);
Al Virob1271252018-04-25 10:28:38 -04001569 } else {
Paul Mooref64410e2014-03-19 16:46:18 -04001570 /* Called from selinux_complete_init, try to
Al Virob1271252018-04-25 10:28:38 -04001571 * find a dentry. Some filesystems really want
1572 * a connected one, so try that first.
1573 */
Paul Mooref64410e2014-03-19 16:46:18 -04001574 dentry = d_find_alias(inode);
Al Virob1271252018-04-25 10:28:38 -04001575 if (!dentry)
1576 dentry = d_find_any_alias(inode);
1577 }
Paul Mooref64410e2014-03-19 16:46:18 -04001578 /*
1579 * This can be hit on boot when a file is accessed
1580 * before the policy is loaded. When we load policy we
1581 * may find inodes that have no dentry on the
1582 * sbsec->isec_head list. No reason to complain as
1583 * these will get fixed up the next time we go through
1584 * inode_doinit() with a dentry, before these inodes
1585 * could be used again by userspace.
1586 */
1587 if (!dentry)
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001588 goto out;
1589 rc = selinux_genfs_get_sid(dentry, sclass,
Stephen Smalley134509d2015-06-04 16:22:17 -04001590 sbsec->flags, &sid);
Paul Mooref64410e2014-03-19 16:46:18 -04001591 dput(dentry);
1592 if (rc)
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001593 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 }
1595 break;
1596 }
1597
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001598out:
1599 spin_lock(&isec->lock);
1600 if (isec->initialized == LABEL_PENDING) {
1601 if (!sid || rc) {
1602 isec->initialized = LABEL_INVALID;
1603 goto out_unlock;
1604 }
1605
1606 isec->initialized = LABEL_INITIALIZED;
1607 isec->sid = sid;
1608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Eric Paris23970742006-09-25 23:32:01 -07001610out_unlock:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001611 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 return rc;
1613}
1614
1615/* Convert a Linux signal to an access vector. */
1616static inline u32 signal_to_av(int sig)
1617{
1618 u32 perm = 0;
1619
1620 switch (sig) {
1621 case SIGCHLD:
1622 /* Commonly granted from child to parent. */
1623 perm = PROCESS__SIGCHLD;
1624 break;
1625 case SIGKILL:
1626 /* Cannot be caught or ignored */
1627 perm = PROCESS__SIGKILL;
1628 break;
1629 case SIGSTOP:
1630 /* Cannot be caught or ignored */
1631 perm = PROCESS__SIGSTOP;
1632 break;
1633 default:
1634 /* All other signals. */
1635 perm = PROCESS__SIGNAL;
1636 break;
1637 }
1638
1639 return perm;
1640}
1641
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001642#if CAP_LAST_CAP > 63
1643#error Fix SELinux to handle capabilities > 63.
1644#endif
1645
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646/* Check whether a task is allowed to use a capability. */
Eric Paris6a9de492012-01-03 12:25:14 -05001647static int cred_has_capability(const struct cred *cred,
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04001648 int cap, int audit, bool initns)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649{
Thomas Liu2bf49692009-07-14 12:14:09 -04001650 struct common_audit_data ad;
Eric Paris06112162008-11-11 22:02:50 +11001651 struct av_decision avd;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001652 u16 sclass;
David Howells3699c532009-01-06 22:27:01 +00001653 u32 sid = cred_sid(cred);
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001654 u32 av = CAP_TO_MASK(cap);
Eric Paris06112162008-11-11 22:02:50 +11001655 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
Eric Paris50c205f2012-04-04 15:01:43 -04001657 ad.type = LSM_AUDIT_DATA_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 ad.u.cap = cap;
1659
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001660 switch (CAP_TO_INDEX(cap)) {
1661 case 0:
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04001662 sclass = initns ? SECCLASS_CAPABILITY : SECCLASS_CAP_USERNS;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001663 break;
1664 case 1:
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04001665 sclass = initns ? SECCLASS_CAPABILITY2 : SECCLASS_CAP2_USERNS;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001666 break;
1667 default:
peter enderborgc103a912018-06-12 10:09:03 +02001668 pr_err("SELinux: out of range capability %d\n", cap);
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001669 BUG();
Eric Parisa35c6c832011-04-20 10:21:28 -04001670 return -EINVAL;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001671 }
Eric Paris06112162008-11-11 22:02:50 +11001672
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001673 rc = avc_has_perm_noaudit(&selinux_state,
1674 sid, sid, sclass, av, 0, &avd);
Eric Paris9ade0cf2011-04-25 16:26:29 -04001675 if (audit == SECURITY_CAP_AUDIT) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001676 int rc2 = avc_audit(&selinux_state,
1677 sid, sid, sclass, av, &avd, rc, &ad, 0);
Eric Paris9ade0cf2011-04-25 16:26:29 -04001678 if (rc2)
1679 return rc2;
1680 }
Eric Paris06112162008-11-11 22:02:50 +11001681 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682}
1683
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684/* Check whether a task has a particular permission to an inode.
1685 The 'adp' parameter is optional and allows other audit
1686 data to be passed (e.g. the dentry). */
David Howells88e67f32008-11-14 10:39:21 +11001687static int inode_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 struct inode *inode,
1689 u32 perms,
Linus Torvalds19e49832013-10-04 12:54:11 -07001690 struct common_audit_data *adp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 struct inode_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +11001693 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
David Howellse0e81732009-09-02 09:13:40 +01001695 validate_creds(cred);
1696
Eric Paris828dfe12008-04-17 13:17:49 -04001697 if (unlikely(IS_PRIVATE(inode)))
Stephen Smalleybbaca6c2007-02-14 00:34:16 -08001698 return 0;
1699
David Howells88e67f32008-11-14 10:39:21 +11001700 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 isec = inode->i_security;
1702
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001703 return avc_has_perm(&selinux_state,
1704 sid, isec->sid, isec->sclass, perms, adp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705}
1706
1707/* Same as inode_has_perm, but pass explicit audit data containing
1708 the dentry to help the auditing code to more easily generate the
1709 pathname if needed. */
David Howells88e67f32008-11-14 10:39:21 +11001710static inline int dentry_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 struct dentry *dentry,
1712 u32 av)
1713{
David Howellsc6f493d2015-03-17 22:26:22 +00001714 struct inode *inode = d_backing_inode(dentry);
Thomas Liu2bf49692009-07-14 12:14:09 -04001715 struct common_audit_data ad;
David Howells88e67f32008-11-14 10:39:21 +11001716
Eric Paris50c205f2012-04-04 15:01:43 -04001717 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Paris2875fa02011-04-28 16:04:24 -04001718 ad.u.dentry = dentry;
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05001719 __inode_security_revalidate(inode, dentry, true);
Linus Torvalds19e49832013-10-04 12:54:11 -07001720 return inode_has_perm(cred, inode, av, &ad);
Eric Paris2875fa02011-04-28 16:04:24 -04001721}
1722
1723/* Same as inode_has_perm, but pass explicit audit data containing
1724 the path to help the auditing code to more easily generate the
1725 pathname if needed. */
1726static inline int path_has_perm(const struct cred *cred,
Al Viro3f7036a2015-03-08 19:28:30 -04001727 const struct path *path,
Eric Paris2875fa02011-04-28 16:04:24 -04001728 u32 av)
1729{
David Howellsc6f493d2015-03-17 22:26:22 +00001730 struct inode *inode = d_backing_inode(path->dentry);
Eric Paris2875fa02011-04-28 16:04:24 -04001731 struct common_audit_data ad;
1732
Eric Paris50c205f2012-04-04 15:01:43 -04001733 ad.type = LSM_AUDIT_DATA_PATH;
Eric Paris2875fa02011-04-28 16:04:24 -04001734 ad.u.path = *path;
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05001735 __inode_security_revalidate(inode, path->dentry, true);
Linus Torvalds19e49832013-10-04 12:54:11 -07001736 return inode_has_perm(cred, inode, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737}
1738
David Howells13f8e982013-06-13 23:37:55 +01001739/* Same as path_has_perm, but uses the inode from the file struct. */
1740static inline int file_path_has_perm(const struct cred *cred,
1741 struct file *file,
1742 u32 av)
1743{
1744 struct common_audit_data ad;
1745
Vivek Goyal43af5de2016-09-09 11:37:49 -04001746 ad.type = LSM_AUDIT_DATA_FILE;
1747 ad.u.file = file;
Linus Torvalds19e49832013-10-04 12:54:11 -07001748 return inode_has_perm(cred, file_inode(file), av, &ad);
David Howells13f8e982013-06-13 23:37:55 +01001749}
1750
Chenbo Fengf66e4482017-10-18 13:00:26 -07001751#ifdef CONFIG_BPF_SYSCALL
1752static int bpf_fd_pass(struct file *file, u32 sid);
1753#endif
1754
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755/* Check whether a task can use an open file descriptor to
1756 access an inode in a given way. Check access to the
1757 descriptor itself, and then use dentry_has_perm to
1758 check a particular permission to the file.
1759 Access to the descriptor is implicitly granted if it
1760 has the same SID as the process. If av is zero, then
1761 access to the file is not checked, e.g. for cases
1762 where only the descriptor is affected like seek. */
David Howells88e67f32008-11-14 10:39:21 +11001763static int file_has_perm(const struct cred *cred,
1764 struct file *file,
1765 u32 av)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 struct file_security_struct *fsec = file->f_security;
Al Viro496ad9a2013-01-23 17:07:38 -05001768 struct inode *inode = file_inode(file);
Thomas Liu2bf49692009-07-14 12:14:09 -04001769 struct common_audit_data ad;
David Howells88e67f32008-11-14 10:39:21 +11001770 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 int rc;
1772
Vivek Goyal43af5de2016-09-09 11:37:49 -04001773 ad.type = LSM_AUDIT_DATA_FILE;
1774 ad.u.file = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
David Howells275bb412008-11-14 10:39:19 +11001776 if (sid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001777 rc = avc_has_perm(&selinux_state,
1778 sid, fsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 SECCLASS_FD,
1780 FD__USE,
1781 &ad);
1782 if (rc)
David Howells88e67f32008-11-14 10:39:21 +11001783 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 }
1785
Chenbo Fengf66e4482017-10-18 13:00:26 -07001786#ifdef CONFIG_BPF_SYSCALL
1787 rc = bpf_fd_pass(file, cred_sid(cred));
1788 if (rc)
1789 return rc;
1790#endif
1791
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 /* av is zero if only checking access to the descriptor. */
David Howells88e67f32008-11-14 10:39:21 +11001793 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 if (av)
Linus Torvalds19e49832013-10-04 12:54:11 -07001795 rc = inode_has_perm(cred, inode, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
David Howells88e67f32008-11-14 10:39:21 +11001797out:
1798 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799}
1800
David Howellsc3c188b2015-07-10 17:19:58 -04001801/*
1802 * Determine the label for an inode that might be unioned.
1803 */
Vivek Goyalc957f6d2016-07-13 10:44:51 -04001804static int
1805selinux_determine_inode_label(const struct task_security_struct *tsec,
1806 struct inode *dir,
1807 const struct qstr *name, u16 tclass,
1808 u32 *_new_isid)
David Howellsc3c188b2015-07-10 17:19:58 -04001809{
1810 const struct superblock_security_struct *sbsec = dir->i_sb->s_security;
David Howellsc3c188b2015-07-10 17:19:58 -04001811
1812 if ((sbsec->flags & SE_SBINITIALIZED) &&
1813 (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)) {
1814 *_new_isid = sbsec->mntpoint_sid;
1815 } else if ((sbsec->flags & SBLABEL_MNT) &&
1816 tsec->create_sid) {
1817 *_new_isid = tsec->create_sid;
1818 } else {
Paul Moore20cdef82016-04-04 14:14:42 -04001819 const struct inode_security_struct *dsec = inode_security(dir);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001820 return security_transition_sid(&selinux_state, tsec->sid,
1821 dsec->sid, tclass,
David Howellsc3c188b2015-07-10 17:19:58 -04001822 name, _new_isid);
1823 }
1824
1825 return 0;
1826}
1827
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828/* Check whether a task can create a file. */
1829static int may_create(struct inode *dir,
1830 struct dentry *dentry,
1831 u16 tclass)
1832{
Paul Moore5fb49872010-04-22 14:46:19 -04001833 const struct task_security_struct *tsec = current_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 struct inode_security_struct *dsec;
1835 struct superblock_security_struct *sbsec;
David Howells275bb412008-11-14 10:39:19 +11001836 u32 sid, newsid;
Thomas Liu2bf49692009-07-14 12:14:09 -04001837 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 int rc;
1839
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001840 dsec = inode_security(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 sbsec = dir->i_sb->s_security;
1842
David Howells275bb412008-11-14 10:39:19 +11001843 sid = tsec->sid;
David Howells275bb412008-11-14 10:39:19 +11001844
Eric Paris50c205f2012-04-04 15:01:43 -04001845 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04001846 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001848 rc = avc_has_perm(&selinux_state,
1849 sid, dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 DIR__ADD_NAME | DIR__SEARCH,
1851 &ad);
1852 if (rc)
1853 return rc;
1854
Vivek Goyalc957f6d2016-07-13 10:44:51 -04001855 rc = selinux_determine_inode_label(current_security(), dir,
1856 &dentry->d_name, tclass, &newsid);
David Howellsc3c188b2015-07-10 17:19:58 -04001857 if (rc)
1858 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001860 rc = avc_has_perm(&selinux_state,
1861 sid, newsid, tclass, FILE__CREATE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 if (rc)
1863 return rc;
1864
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001865 return avc_has_perm(&selinux_state,
1866 newsid, sbsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 SECCLASS_FILESYSTEM,
1868 FILESYSTEM__ASSOCIATE, &ad);
1869}
1870
Eric Paris828dfe12008-04-17 13:17:49 -04001871#define MAY_LINK 0
1872#define MAY_UNLINK 1
1873#define MAY_RMDIR 2
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
1875/* Check whether a task can link, unlink, or rmdir a file/directory. */
1876static int may_link(struct inode *dir,
1877 struct dentry *dentry,
1878 int kind)
1879
1880{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 struct inode_security_struct *dsec, *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001882 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11001883 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 u32 av;
1885 int rc;
1886
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001887 dsec = inode_security(dir);
1888 isec = backing_inode_security(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
Eric Paris50c205f2012-04-04 15:01:43 -04001890 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04001891 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
1893 av = DIR__SEARCH;
1894 av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001895 rc = avc_has_perm(&selinux_state,
1896 sid, dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 if (rc)
1898 return rc;
1899
1900 switch (kind) {
1901 case MAY_LINK:
1902 av = FILE__LINK;
1903 break;
1904 case MAY_UNLINK:
1905 av = FILE__UNLINK;
1906 break;
1907 case MAY_RMDIR:
1908 av = DIR__RMDIR;
1909 break;
1910 default:
peter enderborgc103a912018-06-12 10:09:03 +02001911 pr_warn("SELinux: %s: unrecognized kind %d\n",
Eric Paris744ba352008-04-17 11:52:44 -04001912 __func__, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 return 0;
1914 }
1915
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001916 rc = avc_has_perm(&selinux_state,
1917 sid, isec->sid, isec->sclass, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 return rc;
1919}
1920
1921static inline int may_rename(struct inode *old_dir,
1922 struct dentry *old_dentry,
1923 struct inode *new_dir,
1924 struct dentry *new_dentry)
1925{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001927 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11001928 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 u32 av;
1930 int old_is_dir, new_is_dir;
1931 int rc;
1932
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001933 old_dsec = inode_security(old_dir);
1934 old_isec = backing_inode_security(old_dentry);
David Howellse36cb0b2015-01-29 12:02:35 +00001935 old_is_dir = d_is_dir(old_dentry);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001936 new_dsec = inode_security(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
Eric Paris50c205f2012-04-04 15:01:43 -04001938 ad.type = LSM_AUDIT_DATA_DENTRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
Eric Parisa2694342011-04-25 13:10:27 -04001940 ad.u.dentry = old_dentry;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001941 rc = avc_has_perm(&selinux_state,
1942 sid, old_dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 DIR__REMOVE_NAME | DIR__SEARCH, &ad);
1944 if (rc)
1945 return rc;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001946 rc = avc_has_perm(&selinux_state,
1947 sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 old_isec->sclass, FILE__RENAME, &ad);
1949 if (rc)
1950 return rc;
1951 if (old_is_dir && new_dir != old_dir) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001952 rc = avc_has_perm(&selinux_state,
1953 sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 old_isec->sclass, DIR__REPARENT, &ad);
1955 if (rc)
1956 return rc;
1957 }
1958
Eric Parisa2694342011-04-25 13:10:27 -04001959 ad.u.dentry = new_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 av = DIR__ADD_NAME | DIR__SEARCH;
David Howells2c616d42015-01-29 12:02:33 +00001961 if (d_is_positive(new_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 av |= DIR__REMOVE_NAME;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001963 rc = avc_has_perm(&selinux_state,
1964 sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 if (rc)
1966 return rc;
David Howells2c616d42015-01-29 12:02:33 +00001967 if (d_is_positive(new_dentry)) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001968 new_isec = backing_inode_security(new_dentry);
David Howellse36cb0b2015-01-29 12:02:35 +00001969 new_is_dir = d_is_dir(new_dentry);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001970 rc = avc_has_perm(&selinux_state,
1971 sid, new_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 new_isec->sclass,
1973 (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
1974 if (rc)
1975 return rc;
1976 }
1977
1978 return 0;
1979}
1980
1981/* Check whether a task can perform a filesystem operation. */
David Howells88e67f32008-11-14 10:39:21 +11001982static int superblock_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 struct super_block *sb,
1984 u32 perms,
Thomas Liu2bf49692009-07-14 12:14:09 -04001985 struct common_audit_data *ad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 struct superblock_security_struct *sbsec;
David Howells88e67f32008-11-14 10:39:21 +11001988 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 sbsec = sb->s_security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001991 return avc_has_perm(&selinux_state,
1992 sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993}
1994
1995/* Convert a Linux mode and permission mask to an access vector. */
1996static inline u32 file_mask_to_av(int mode, int mask)
1997{
1998 u32 av = 0;
1999
Al Virodba19c62011-07-25 20:49:29 -04002000 if (!S_ISDIR(mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 if (mask & MAY_EXEC)
2002 av |= FILE__EXECUTE;
2003 if (mask & MAY_READ)
2004 av |= FILE__READ;
2005
2006 if (mask & MAY_APPEND)
2007 av |= FILE__APPEND;
2008 else if (mask & MAY_WRITE)
2009 av |= FILE__WRITE;
2010
2011 } else {
2012 if (mask & MAY_EXEC)
2013 av |= DIR__SEARCH;
2014 if (mask & MAY_WRITE)
2015 av |= DIR__WRITE;
2016 if (mask & MAY_READ)
2017 av |= DIR__READ;
2018 }
2019
2020 return av;
2021}
2022
2023/* Convert a Linux file to an access vector. */
2024static inline u32 file_to_av(struct file *file)
2025{
2026 u32 av = 0;
2027
2028 if (file->f_mode & FMODE_READ)
2029 av |= FILE__READ;
2030 if (file->f_mode & FMODE_WRITE) {
2031 if (file->f_flags & O_APPEND)
2032 av |= FILE__APPEND;
2033 else
2034 av |= FILE__WRITE;
2035 }
Stephen Smalley0794c662008-03-17 08:55:18 -04002036 if (!av) {
2037 /*
2038 * Special file opened with flags 3 for ioctl-only use.
2039 */
2040 av = FILE__IOCTL;
2041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
2043 return av;
2044}
2045
Eric Paris8b6a5a32008-10-29 17:06:46 -04002046/*
2047 * Convert a file to an access vector and include the correct open
2048 * open permission.
2049 */
2050static inline u32 open_file_to_av(struct file *file)
2051{
2052 u32 av = file_to_av(file);
Stephen Smalleyccb54472017-05-12 12:41:24 -04002053 struct inode *inode = file_inode(file);
Eric Paris8b6a5a32008-10-29 17:06:46 -04002054
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002055 if (selinux_policycap_openperm() &&
2056 inode->i_sb->s_magic != SOCKFS_MAGIC)
Eric Paris49b7b8d2010-07-23 11:44:09 -04002057 av |= FILE__OPEN;
2058
Eric Paris8b6a5a32008-10-29 17:06:46 -04002059 return av;
2060}
2061
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062/* Hook functions begin here. */
2063
Stephen Smalley79af7302015-01-21 10:54:10 -05002064static int selinux_binder_set_context_mgr(struct task_struct *mgr)
2065{
2066 u32 mysid = current_sid();
2067 u32 mgrsid = task_sid(mgr);
2068
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002069 return avc_has_perm(&selinux_state,
2070 mysid, mgrsid, SECCLASS_BINDER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002071 BINDER__SET_CONTEXT_MGR, NULL);
2072}
2073
2074static int selinux_binder_transaction(struct task_struct *from,
2075 struct task_struct *to)
2076{
2077 u32 mysid = current_sid();
2078 u32 fromsid = task_sid(from);
2079 u32 tosid = task_sid(to);
2080 int rc;
2081
2082 if (mysid != fromsid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002083 rc = avc_has_perm(&selinux_state,
2084 mysid, fromsid, SECCLASS_BINDER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002085 BINDER__IMPERSONATE, NULL);
2086 if (rc)
2087 return rc;
2088 }
2089
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002090 return avc_has_perm(&selinux_state,
2091 fromsid, tosid, SECCLASS_BINDER, BINDER__CALL,
Stephen Smalley79af7302015-01-21 10:54:10 -05002092 NULL);
2093}
2094
2095static int selinux_binder_transfer_binder(struct task_struct *from,
2096 struct task_struct *to)
2097{
2098 u32 fromsid = task_sid(from);
2099 u32 tosid = task_sid(to);
2100
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002101 return avc_has_perm(&selinux_state,
2102 fromsid, tosid, SECCLASS_BINDER, BINDER__TRANSFER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002103 NULL);
2104}
2105
2106static int selinux_binder_transfer_file(struct task_struct *from,
2107 struct task_struct *to,
2108 struct file *file)
2109{
2110 u32 sid = task_sid(to);
2111 struct file_security_struct *fsec = file->f_security;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002112 struct dentry *dentry = file->f_path.dentry;
Paul Moore20cdef82016-04-04 14:14:42 -04002113 struct inode_security_struct *isec;
Stephen Smalley79af7302015-01-21 10:54:10 -05002114 struct common_audit_data ad;
2115 int rc;
2116
2117 ad.type = LSM_AUDIT_DATA_PATH;
2118 ad.u.path = file->f_path;
2119
2120 if (sid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002121 rc = avc_has_perm(&selinux_state,
2122 sid, fsec->sid,
Stephen Smalley79af7302015-01-21 10:54:10 -05002123 SECCLASS_FD,
2124 FD__USE,
2125 &ad);
2126 if (rc)
2127 return rc;
2128 }
2129
Chenbo Fengf66e4482017-10-18 13:00:26 -07002130#ifdef CONFIG_BPF_SYSCALL
2131 rc = bpf_fd_pass(file, sid);
2132 if (rc)
2133 return rc;
2134#endif
2135
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002136 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
Stephen Smalley79af7302015-01-21 10:54:10 -05002137 return 0;
2138
Paul Moore20cdef82016-04-04 14:14:42 -04002139 isec = backing_inode_security(dentry);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002140 return avc_has_perm(&selinux_state,
2141 sid, isec->sid, isec->sclass, file_to_av(file),
Stephen Smalley79af7302015-01-21 10:54:10 -05002142 &ad);
2143}
2144
Ingo Molnar9e488582009-05-07 19:26:19 +10002145static int selinux_ptrace_access_check(struct task_struct *child,
David Howells5cd9c582008-08-14 11:37:28 +01002146 unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002148 u32 sid = current_sid();
2149 u32 csid = task_sid(child);
Stephen Smalley006ebb42008-05-19 08:32:49 -04002150
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002151 if (mode & PTRACE_MODE_READ)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002152 return avc_has_perm(&selinux_state,
2153 sid, csid, SECCLASS_FILE, FILE__READ, NULL);
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002154
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002155 return avc_has_perm(&selinux_state,
2156 sid, csid, SECCLASS_PROCESS, PROCESS__PTRACE, NULL);
David Howells5cd9c582008-08-14 11:37:28 +01002157}
2158
2159static int selinux_ptrace_traceme(struct task_struct *parent)
2160{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002161 return avc_has_perm(&selinux_state,
2162 task_sid(parent), current_sid(), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002163 PROCESS__PTRACE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164}
2165
2166static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
Eric Paris828dfe12008-04-17 13:17:49 -04002167 kernel_cap_t *inheritable, kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002169 return avc_has_perm(&selinux_state,
2170 current_sid(), task_sid(target), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002171 PROCESS__GETCAP, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172}
2173
David Howellsd84f4f92008-11-14 10:39:23 +11002174static int selinux_capset(struct cred *new, const struct cred *old,
2175 const kernel_cap_t *effective,
2176 const kernel_cap_t *inheritable,
2177 const kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002179 return avc_has_perm(&selinux_state,
2180 cred_sid(old), cred_sid(new), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002181 PROCESS__SETCAP, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182}
2183
James Morris5626d3e2009-01-30 10:05:06 +11002184/*
2185 * (This comment used to live with the selinux_task_setuid hook,
2186 * which was removed).
2187 *
2188 * Since setuid only affects the current process, and since the SELinux
2189 * controls are not based on the Linux identity attributes, SELinux does not
2190 * need to control this operation. However, SELinux does control the use of
2191 * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
2192 */
2193
Eric Paris6a9de492012-01-03 12:25:14 -05002194static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
2195 int cap, int audit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196{
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04002197 return cred_has_capability(cred, cap, audit, ns == &init_user_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198}
2199
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
2201{
David Howells88e67f32008-11-14 10:39:21 +11002202 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 int rc = 0;
2204
2205 if (!sb)
2206 return 0;
2207
2208 switch (cmds) {
Eric Paris828dfe12008-04-17 13:17:49 -04002209 case Q_SYNC:
2210 case Q_QUOTAON:
2211 case Q_QUOTAOFF:
2212 case Q_SETINFO:
2213 case Q_SETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11002214 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04002215 break;
2216 case Q_GETFMT:
2217 case Q_GETINFO:
2218 case Q_GETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11002219 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04002220 break;
2221 default:
2222 rc = 0; /* let the kernel handle invalid cmds */
2223 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 }
2225 return rc;
2226}
2227
2228static int selinux_quota_on(struct dentry *dentry)
2229{
David Howells88e67f32008-11-14 10:39:21 +11002230 const struct cred *cred = current_cred();
2231
Eric Paris2875fa02011-04-28 16:04:24 -04002232 return dentry_has_perm(cred, dentry, FILE__QUOTAON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233}
2234
Eric Paris12b30522010-11-15 18:36:29 -05002235static int selinux_syslog(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 switch (type) {
Kees Cookd78ca3c2010-02-03 15:37:13 -08002238 case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */
2239 case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002240 return avc_has_perm(&selinux_state,
2241 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002242 SECCLASS_SYSTEM, SYSTEM__SYSLOG_READ, NULL);
Kees Cookd78ca3c2010-02-03 15:37:13 -08002243 case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */
2244 case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */
2245 /* Set level of messages printed to console */
2246 case SYSLOG_ACTION_CONSOLE_LEVEL:
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002247 return avc_has_perm(&selinux_state,
2248 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002249 SECCLASS_SYSTEM, SYSTEM__SYSLOG_CONSOLE,
2250 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 }
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002252 /* All other syslog types */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002253 return avc_has_perm(&selinux_state,
2254 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002255 SECCLASS_SYSTEM, SYSTEM__SYSLOG_MOD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256}
2257
2258/*
2259 * Check that a process has enough memory to allocate a new virtual
2260 * mapping. 0 means there is enough memory for the allocation to
2261 * succeed and -ENOMEM implies there is not.
2262 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 * Do not audit the selinux permission check, as this is applied to all
2264 * processes that allocate mappings.
2265 */
Alan Cox34b4e4a2007-08-22 14:01:28 -07002266static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267{
2268 int rc, cap_sys_admin = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002270 rc = cred_has_capability(current_cred(), CAP_SYS_ADMIN,
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04002271 SECURITY_CAP_NOAUDIT, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 if (rc == 0)
2273 cap_sys_admin = 1;
2274
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002275 return cap_sys_admin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276}
2277
2278/* binprm security operations */
2279
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002280static u32 ptrace_parent_sid(void)
Paul Moore0c6181c2016-03-30 21:41:21 -04002281{
2282 u32 sid = 0;
2283 struct task_struct *tracer;
2284
2285 rcu_read_lock();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002286 tracer = ptrace_parent(current);
Paul Moore0c6181c2016-03-30 21:41:21 -04002287 if (tracer)
2288 sid = task_sid(tracer);
2289 rcu_read_unlock();
2290
2291 return sid;
2292}
2293
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002294static int check_nnp_nosuid(const struct linux_binprm *bprm,
2295 const struct task_security_struct *old_tsec,
2296 const struct task_security_struct *new_tsec)
2297{
2298 int nnp = (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS);
Andy Lutomirski380cf5b2016-06-23 16:41:05 -05002299 int nosuid = !mnt_may_suid(bprm->file->f_path.mnt);
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002300 int rc;
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002301 u32 av;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002302
2303 if (!nnp && !nosuid)
2304 return 0; /* neither NNP nor nosuid */
2305
2306 if (new_tsec->sid == old_tsec->sid)
2307 return 0; /* No change in credentials */
2308
2309 /*
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002310 * If the policy enables the nnp_nosuid_transition policy capability,
2311 * then we permit transitions under NNP or nosuid if the
2312 * policy allows the corresponding permission between
2313 * the old and new contexts.
2314 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002315 if (selinux_policycap_nnp_nosuid_transition()) {
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002316 av = 0;
2317 if (nnp)
2318 av |= PROCESS2__NNP_TRANSITION;
2319 if (nosuid)
2320 av |= PROCESS2__NOSUID_TRANSITION;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002321 rc = avc_has_perm(&selinux_state,
2322 old_tsec->sid, new_tsec->sid,
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002323 SECCLASS_PROCESS2, av, NULL);
2324 if (!rc)
2325 return 0;
2326 }
2327
2328 /*
2329 * We also permit NNP or nosuid transitions to bounded SIDs,
2330 * i.e. SIDs that are guaranteed to only be allowed a subset
2331 * of the permissions of the current SID.
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002332 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002333 rc = security_bounded_transition(&selinux_state, old_tsec->sid,
2334 new_tsec->sid);
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002335 if (!rc)
2336 return 0;
2337
2338 /*
2339 * On failure, preserve the errno values for NNP vs nosuid.
2340 * NNP: Operation not permitted for caller.
2341 * nosuid: Permission denied to file.
2342 */
2343 if (nnp)
2344 return -EPERM;
2345 return -EACCES;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002346}
2347
David Howellsa6f76f22008-11-14 10:39:24 +11002348static int selinux_bprm_set_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349{
David Howellsa6f76f22008-11-14 10:39:24 +11002350 const struct task_security_struct *old_tsec;
2351 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 struct inode_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04002353 struct common_audit_data ad;
Al Viro496ad9a2013-01-23 17:07:38 -05002354 struct inode *inode = file_inode(bprm->file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 int rc;
2356
David Howellsa6f76f22008-11-14 10:39:24 +11002357 /* SELinux context only depends on initial program or script and not
2358 * the script interpreter */
Kees Cookddb4a142017-07-18 15:25:23 -07002359 if (bprm->called_set_creds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 return 0;
2361
David Howellsa6f76f22008-11-14 10:39:24 +11002362 old_tsec = current_security();
2363 new_tsec = bprm->cred->security;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002364 isec = inode_security(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
2366 /* Default to the current task SID. */
David Howellsa6f76f22008-11-14 10:39:24 +11002367 new_tsec->sid = old_tsec->sid;
2368 new_tsec->osid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
Michael LeMay28eba5b2006-06-27 02:53:42 -07002370 /* Reset fs, key, and sock SIDs on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002371 new_tsec->create_sid = 0;
2372 new_tsec->keycreate_sid = 0;
2373 new_tsec->sockcreate_sid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
David Howellsa6f76f22008-11-14 10:39:24 +11002375 if (old_tsec->exec_sid) {
2376 new_tsec->sid = old_tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 /* Reset exec SID on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002378 new_tsec->exec_sid = 0;
Andy Lutomirski259e5e62012-04-12 16:47:50 -05002379
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002380 /* Fail on NNP or nosuid if not an allowed transition. */
2381 rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
2382 if (rc)
2383 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 } else {
2385 /* Check for a default transition on this program. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002386 rc = security_transition_sid(&selinux_state, old_tsec->sid,
2387 isec->sid, SECCLASS_PROCESS, NULL,
Eric Paris652bb9b2011-02-01 11:05:40 -05002388 &new_tsec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 if (rc)
2390 return rc;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002391
2392 /*
2393 * Fallback to old SID on NNP or nosuid if not an allowed
2394 * transition.
2395 */
2396 rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
2397 if (rc)
2398 new_tsec->sid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 }
2400
Vivek Goyal43af5de2016-09-09 11:37:49 -04002401 ad.type = LSM_AUDIT_DATA_FILE;
2402 ad.u.file = bprm->file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
David Howellsa6f76f22008-11-14 10:39:24 +11002404 if (new_tsec->sid == old_tsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002405 rc = avc_has_perm(&selinux_state,
2406 old_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
2408 if (rc)
2409 return rc;
2410 } else {
2411 /* Check permissions for the transition. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002412 rc = avc_has_perm(&selinux_state,
2413 old_tsec->sid, new_tsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
2415 if (rc)
2416 return rc;
2417
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002418 rc = avc_has_perm(&selinux_state,
2419 new_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
2421 if (rc)
2422 return rc;
2423
David Howellsa6f76f22008-11-14 10:39:24 +11002424 /* Check for shared state */
2425 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002426 rc = avc_has_perm(&selinux_state,
2427 old_tsec->sid, new_tsec->sid,
David Howellsa6f76f22008-11-14 10:39:24 +11002428 SECCLASS_PROCESS, PROCESS__SHARE,
2429 NULL);
2430 if (rc)
2431 return -EPERM;
2432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433
David Howellsa6f76f22008-11-14 10:39:24 +11002434 /* Make sure that anyone attempting to ptrace over a task that
2435 * changes its SID has the appropriate permit */
Eric W. Biederman9227dd22017-01-23 17:26:31 +13002436 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002437 u32 ptsid = ptrace_parent_sid();
David Howellsa6f76f22008-11-14 10:39:24 +11002438 if (ptsid != 0) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002439 rc = avc_has_perm(&selinux_state,
2440 ptsid, new_tsec->sid,
David Howellsa6f76f22008-11-14 10:39:24 +11002441 SECCLASS_PROCESS,
2442 PROCESS__PTRACE, NULL);
2443 if (rc)
2444 return -EPERM;
2445 }
2446 }
2447
2448 /* Clear any possibly unsafe personality bits on exec: */
2449 bprm->per_clear |= PER_CLEAR_ON_SETID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 /* Enable secure mode for SIDs transitions unless
2452 the noatsecure permission is granted between
2453 the two SIDs, i.e. ahp returns 0. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002454 rc = avc_has_perm(&selinux_state,
2455 old_tsec->sid, new_tsec->sid,
Kees Cook62874c32017-07-18 15:25:25 -07002456 SECCLASS_PROCESS, PROCESS__NOATSECURE,
2457 NULL);
2458 bprm->secureexec |= !!rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 }
2460
Kees Cook62874c32017-07-18 15:25:25 -07002461 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462}
2463
Al Viroc3c073f2012-08-21 22:32:06 -04002464static int match_file(const void *p, struct file *file, unsigned fd)
2465{
2466 return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0;
2467}
2468
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469/* Derived from fs/exec.c:flush_old_files. */
David Howells745ca242008-11-14 10:39:22 +11002470static inline void flush_unauthorized_files(const struct cred *cred,
2471 struct files_struct *files)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 struct file *file, *devnull = NULL;
Stephen Smalleyb20c8122006-09-25 23:32:03 -07002474 struct tty_struct *tty;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002475 int drop_tty = 0;
Al Viroc3c073f2012-08-21 22:32:06 -04002476 unsigned n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002478 tty = get_current_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 if (tty) {
Peter Hurley4a510962016-01-09 21:35:23 -08002480 spin_lock(&tty->files_lock);
Eric Paris37dd0bd2008-10-31 17:40:00 -04002481 if (!list_empty(&tty->tty_files)) {
Nick Piggind996b622010-08-18 04:37:36 +10002482 struct tty_file_private *file_priv;
Eric Paris37dd0bd2008-10-31 17:40:00 -04002483
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 /* Revalidate access to controlling tty.
David Howells13f8e982013-06-13 23:37:55 +01002485 Use file_path_has_perm on the tty path directly
2486 rather than using file_has_perm, as this particular
2487 open file may belong to another process and we are
2488 only interested in the inode-based check here. */
Nick Piggind996b622010-08-18 04:37:36 +10002489 file_priv = list_first_entry(&tty->tty_files,
2490 struct tty_file_private, list);
2491 file = file_priv->file;
David Howells13f8e982013-06-13 23:37:55 +01002492 if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE))
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002493 drop_tty = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 }
Peter Hurley4a510962016-01-09 21:35:23 -08002495 spin_unlock(&tty->files_lock);
Alan Cox452a00d2008-10-13 10:39:13 +01002496 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 }
Eric W. Biederman98a27ba2007-05-08 00:26:56 -07002498 /* Reset controlling tty. */
2499 if (drop_tty)
2500 no_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501
2502 /* Revalidate access to inherited open files. */
Al Viroc3c073f2012-08-21 22:32:06 -04002503 n = iterate_fd(files, 0, match_file, cred);
2504 if (!n) /* none found? */
2505 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506
Al Viroc3c073f2012-08-21 22:32:06 -04002507 devnull = dentry_open(&selinux_null, O_RDWR, cred);
Al Viro45525b22012-10-16 13:30:07 -04002508 if (IS_ERR(devnull))
2509 devnull = NULL;
2510 /* replace all the matching ones with this */
2511 do {
2512 replace_fd(n - 1, devnull, 0);
2513 } while ((n = iterate_fd(files, n, match_file, cred)) != 0);
2514 if (devnull)
Al Viroc3c073f2012-08-21 22:32:06 -04002515 fput(devnull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516}
2517
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518/*
David Howellsa6f76f22008-11-14 10:39:24 +11002519 * Prepare a process for imminent new credential changes due to exec
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 */
David Howellsa6f76f22008-11-14 10:39:24 +11002521static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522{
David Howellsa6f76f22008-11-14 10:39:24 +11002523 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 struct rlimit *rlim, *initrlim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 int rc, i;
2526
David Howellsa6f76f22008-11-14 10:39:24 +11002527 new_tsec = bprm->cred->security;
2528 if (new_tsec->sid == new_tsec->osid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 return;
2530
2531 /* Close files for which the new task SID is not authorized. */
David Howellsa6f76f22008-11-14 10:39:24 +11002532 flush_unauthorized_files(bprm->cred, current->files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
David Howellsa6f76f22008-11-14 10:39:24 +11002534 /* Always clear parent death signal on SID transitions. */
2535 current->pdeath_signal = 0;
2536
2537 /* Check whether the new SID can inherit resource limits from the old
2538 * SID. If not, reset all soft limits to the lower of the current
2539 * task's hard limit and the init task's soft limit.
2540 *
2541 * Note that the setting of hard limits (even to lower them) can be
2542 * controlled by the setrlimit check. The inclusion of the init task's
2543 * soft limit into the computation is to avoid resetting soft limits
2544 * higher than the default soft limit for cases where the default is
2545 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2546 */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002547 rc = avc_has_perm(&selinux_state,
2548 new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
David Howellsa6f76f22008-11-14 10:39:24 +11002549 PROCESS__RLIMITINH, NULL);
2550 if (rc) {
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002551 /* protect against do_prlimit() */
2552 task_lock(current);
David Howellsa6f76f22008-11-14 10:39:24 +11002553 for (i = 0; i < RLIM_NLIMITS; i++) {
2554 rlim = current->signal->rlim + i;
2555 initrlim = init_task.signal->rlim + i;
2556 rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
2557 }
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002558 task_unlock(current);
Nicolas Pitrebaa73d92016-11-11 00:10:10 -05002559 if (IS_ENABLED(CONFIG_POSIX_TIMERS))
2560 update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
David Howellsa6f76f22008-11-14 10:39:24 +11002561 }
2562}
2563
2564/*
2565 * Clean up the process immediately after the installation of new credentials
2566 * due to exec
2567 */
2568static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
2569{
2570 const struct task_security_struct *tsec = current_security();
2571 struct itimerval itimer;
David Howellsa6f76f22008-11-14 10:39:24 +11002572 u32 osid, sid;
2573 int rc, i;
David Howellsa6f76f22008-11-14 10:39:24 +11002574
David Howellsa6f76f22008-11-14 10:39:24 +11002575 osid = tsec->osid;
2576 sid = tsec->sid;
2577
2578 if (sid == osid)
2579 return;
2580
2581 /* Check whether the new SID can inherit signal state from the old SID.
2582 * If not, clear itimers to avoid subsequent signal generation and
2583 * flush and unblock signals.
2584 *
2585 * This must occur _after_ the task SID has been updated so that any
2586 * kill done after the flush will be checked against the new SID.
2587 */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002588 rc = avc_has_perm(&selinux_state,
2589 osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 if (rc) {
Nicolas Pitrebaa73d92016-11-11 00:10:10 -05002591 if (IS_ENABLED(CONFIG_POSIX_TIMERS)) {
2592 memset(&itimer, 0, sizeof itimer);
2593 for (i = 0; i < 3; i++)
2594 do_setitimer(i, &itimer, NULL);
2595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterov9e7c8f82015-06-04 16:22:16 -04002597 if (!fatal_signal_pending(current)) {
2598 flush_sigqueue(&current->pending);
2599 flush_sigqueue(&current->signal->shared_pending);
David Howells3bcac022009-04-29 13:45:05 +01002600 flush_signal_handlers(current, 1);
2601 sigemptyset(&current->blocked);
Oleg Nesterov9e7c8f82015-06-04 16:22:16 -04002602 recalc_sigpending();
David Howells3bcac022009-04-29 13:45:05 +01002603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 spin_unlock_irq(&current->sighand->siglock);
2605 }
2606
David Howellsa6f76f22008-11-14 10:39:24 +11002607 /* Wake up the parent if it is waiting so that it can recheck
2608 * wait permission to the new task SID. */
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002609 read_lock(&tasklist_lock);
Oleg Nesterov0b7570e2009-09-23 15:56:46 -07002610 __wake_up_parent(current, current->real_parent);
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002611 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612}
2613
2614/* superblock security operations */
2615
2616static int selinux_sb_alloc_security(struct super_block *sb)
2617{
2618 return superblock_alloc_security(sb);
2619}
2620
2621static void selinux_sb_free_security(struct super_block *sb)
2622{
2623 superblock_free_security(sb);
2624}
2625
2626static inline int match_prefix(char *prefix, int plen, char *option, int olen)
2627{
2628 if (plen > olen)
2629 return 0;
2630
2631 return !memcmp(prefix, option, plen);
2632}
2633
2634static inline int selinux_option(char *option, int len)
2635{
Eric Paris832cbd92008-04-01 13:24:09 -04002636 return (match_prefix(CONTEXT_STR, sizeof(CONTEXT_STR)-1, option, len) ||
2637 match_prefix(FSCONTEXT_STR, sizeof(FSCONTEXT_STR)-1, option, len) ||
2638 match_prefix(DEFCONTEXT_STR, sizeof(DEFCONTEXT_STR)-1, option, len) ||
David P. Quigley11689d42009-01-16 09:22:03 -05002639 match_prefix(ROOTCONTEXT_STR, sizeof(ROOTCONTEXT_STR)-1, option, len) ||
2640 match_prefix(LABELSUPP_STR, sizeof(LABELSUPP_STR)-1, option, len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641}
2642
2643static inline void take_option(char **to, char *from, int *first, int len)
2644{
2645 if (!*first) {
2646 **to = ',';
2647 *to += 1;
Cory Olmo3528a952006-09-29 01:58:44 -07002648 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 *first = 0;
2650 memcpy(*to, from, len);
2651 *to += len;
2652}
2653
Eric Paris828dfe12008-04-17 13:17:49 -04002654static inline void take_selinux_option(char **to, char *from, int *first,
2655 int len)
Cory Olmo3528a952006-09-29 01:58:44 -07002656{
2657 int current_size = 0;
2658
2659 if (!*first) {
2660 **to = '|';
2661 *to += 1;
Eric Paris828dfe12008-04-17 13:17:49 -04002662 } else
Cory Olmo3528a952006-09-29 01:58:44 -07002663 *first = 0;
2664
2665 while (current_size < len) {
2666 if (*from != '"') {
2667 **to = *from;
2668 *to += 1;
2669 }
2670 from += 1;
2671 current_size += 1;
2672 }
2673}
2674
Eric Parise0007522008-03-05 10:31:54 -05002675static int selinux_sb_copy_data(char *orig, char *copy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676{
2677 int fnosec, fsec, rc = 0;
2678 char *in_save, *in_curr, *in_end;
2679 char *sec_curr, *nosec_save, *nosec;
Cory Olmo3528a952006-09-29 01:58:44 -07002680 int open_quote = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681
2682 in_curr = orig;
2683 sec_curr = copy;
2684
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 nosec = (char *)get_zeroed_page(GFP_KERNEL);
2686 if (!nosec) {
2687 rc = -ENOMEM;
2688 goto out;
2689 }
2690
2691 nosec_save = nosec;
2692 fnosec = fsec = 1;
2693 in_save = in_end = orig;
2694
2695 do {
Cory Olmo3528a952006-09-29 01:58:44 -07002696 if (*in_end == '"')
2697 open_quote = !open_quote;
2698 if ((*in_end == ',' && open_quote == 0) ||
2699 *in_end == '\0') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 int len = in_end - in_curr;
2701
2702 if (selinux_option(in_curr, len))
Cory Olmo3528a952006-09-29 01:58:44 -07002703 take_selinux_option(&sec_curr, in_curr, &fsec, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 else
2705 take_option(&nosec, in_curr, &fnosec, len);
2706
2707 in_curr = in_end + 1;
2708 }
2709 } while (*in_end++);
2710
Eric Paris6931dfc2005-06-30 02:58:51 -07002711 strcpy(in_save, nosec_save);
Gerald Schaeferda3caa22005-06-21 17:15:18 -07002712 free_page((unsigned long)nosec_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713out:
2714 return rc;
2715}
2716
Al Viro5b400232018-12-12 20:13:29 -05002717static int selinux_sb_eat_lsm_opts(char *options, struct security_mnt_opts *opts)
2718{
2719 char *s = (char *)get_zeroed_page(GFP_KERNEL);
2720 int err;
2721
2722 if (!s)
2723 return -ENOMEM;
2724 err = selinux_sb_copy_data(options, s);
2725 if (!err)
2726 err = selinux_parse_opts_str(s, opts);
2727 free_page((unsigned long)s);
2728 return err;
2729}
2730
Al Viroc039bc32018-12-01 23:06:57 -05002731static int selinux_sb_remount(struct super_block *sb,
2732 struct security_mnt_opts *opts)
Eric Paris026eb162011-03-03 16:09:14 -05002733{
Al Viroc039bc32018-12-01 23:06:57 -05002734 int i, *flags;
2735 char **mount_options;
Eric Paris026eb162011-03-03 16:09:14 -05002736 struct superblock_security_struct *sbsec = sb->s_security;
2737
2738 if (!(sbsec->flags & SE_SBINITIALIZED))
2739 return 0;
2740
Al Viroc039bc32018-12-01 23:06:57 -05002741 mount_options = opts->mnt_opts;
2742 flags = opts->mnt_opts_flags;
Eric Paris026eb162011-03-03 16:09:14 -05002743
Al Viroc039bc32018-12-01 23:06:57 -05002744 for (i = 0; i < opts->num_mnt_opts; i++) {
Eric Paris026eb162011-03-03 16:09:14 -05002745 u32 sid;
Al Viroc039bc32018-12-01 23:06:57 -05002746 int rc;
Eric Paris026eb162011-03-03 16:09:14 -05002747
Eric Paris12f348b2012-10-09 10:56:25 -04002748 if (flags[i] == SBLABEL_MNT)
Eric Paris026eb162011-03-03 16:09:14 -05002749 continue;
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002750 rc = security_context_str_to_sid(&selinux_state,
2751 mount_options[i], &sid,
2752 GFP_KERNEL);
Eric Paris026eb162011-03-03 16:09:14 -05002753 if (rc) {
peter enderborgc103a912018-06-12 10:09:03 +02002754 pr_warn("SELinux: security_context_str_to_sid"
Linus Torvalds29b1deb2013-12-15 11:17:45 -08002755 "(%s) failed for (dev %s, type %s) errno=%d\n",
2756 mount_options[i], sb->s_id, sb->s_type->name, rc);
Al Viroc039bc32018-12-01 23:06:57 -05002757 return rc;
Eric Paris026eb162011-03-03 16:09:14 -05002758 }
Eric Paris026eb162011-03-03 16:09:14 -05002759 switch (flags[i]) {
2760 case FSCONTEXT_MNT:
2761 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
2762 goto out_bad_option;
2763 break;
2764 case CONTEXT_MNT:
2765 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
2766 goto out_bad_option;
2767 break;
2768 case ROOTCONTEXT_MNT: {
2769 struct inode_security_struct *root_isec;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002770 root_isec = backing_inode_security(sb->s_root);
Eric Paris026eb162011-03-03 16:09:14 -05002771
2772 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
2773 goto out_bad_option;
2774 break;
2775 }
2776 case DEFCONTEXT_MNT:
2777 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
2778 goto out_bad_option;
2779 break;
2780 default:
Al Viroc039bc32018-12-01 23:06:57 -05002781 return -EINVAL;
Eric Paris026eb162011-03-03 16:09:14 -05002782 }
2783 }
Al Viroc039bc32018-12-01 23:06:57 -05002784 return 0;
Eric Paris026eb162011-03-03 16:09:14 -05002785
Eric Paris026eb162011-03-03 16:09:14 -05002786out_bad_option:
peter enderborgc103a912018-06-12 10:09:03 +02002787 pr_warn("SELinux: unable to change security options "
Linus Torvalds29b1deb2013-12-15 11:17:45 -08002788 "during remount (dev %s, type=%s)\n", sb->s_id,
2789 sb->s_type->name);
Al Viroc039bc32018-12-01 23:06:57 -05002790 return -EINVAL;
Eric Paris026eb162011-03-03 16:09:14 -05002791}
2792
Al Viroa10d7c22018-12-05 11:58:35 -05002793static int selinux_sb_kern_mount(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794{
David Howells88e67f32008-11-14 10:39:21 +11002795 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002796 struct common_audit_data ad;
James Morris74192242008-12-19 11:41:10 +11002797
Eric Paris50c205f2012-04-04 15:01:43 -04002798 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002799 ad.u.dentry = sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002800 return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801}
2802
David Howells726c3342006-06-23 02:02:58 -07002803static int selinux_sb_statfs(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804{
David Howells88e67f32008-11-14 10:39:21 +11002805 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002806 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807
Eric Paris50c205f2012-04-04 15:01:43 -04002808 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002809 ad.u.dentry = dentry->d_sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002810 return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811}
2812
Al Viro808d4e32012-10-11 11:42:01 -04002813static int selinux_mount(const char *dev_name,
Al Viro8a04c432016-03-25 14:52:53 -04002814 const struct path *path,
Al Viro808d4e32012-10-11 11:42:01 -04002815 const char *type,
Eric Paris828dfe12008-04-17 13:17:49 -04002816 unsigned long flags,
2817 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818{
David Howells88e67f32008-11-14 10:39:21 +11002819 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820
2821 if (flags & MS_REMOUNT)
Al Virod8c95842011-12-07 18:16:57 -05002822 return superblock_has_perm(cred, path->dentry->d_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002823 FILESYSTEM__REMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 else
Eric Paris2875fa02011-04-28 16:04:24 -04002825 return path_has_perm(cred, path, FILE__MOUNTON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826}
2827
2828static int selinux_umount(struct vfsmount *mnt, int flags)
2829{
David Howells88e67f32008-11-14 10:39:21 +11002830 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831
David Howells88e67f32008-11-14 10:39:21 +11002832 return superblock_has_perm(cred, mnt->mnt_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002833 FILESYSTEM__UNMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834}
2835
2836/* inode security operations */
2837
2838static int selinux_inode_alloc_security(struct inode *inode)
2839{
2840 return inode_alloc_security(inode);
2841}
2842
2843static void selinux_inode_free_security(struct inode *inode)
2844{
2845 inode_free_security(inode);
2846}
2847
David Quigleyd47be3d2013-05-22 12:50:34 -04002848static int selinux_dentry_init_security(struct dentry *dentry, int mode,
Al Viro4f3ccd72016-07-20 16:06:15 -04002849 const struct qstr *name, void **ctx,
David Quigleyd47be3d2013-05-22 12:50:34 -04002850 u32 *ctxlen)
2851{
David Quigleyd47be3d2013-05-22 12:50:34 -04002852 u32 newsid;
2853 int rc;
2854
Vivek Goyalc957f6d2016-07-13 10:44:51 -04002855 rc = selinux_determine_inode_label(current_security(),
2856 d_inode(dentry->d_parent), name,
David Howellsc3c188b2015-07-10 17:19:58 -04002857 inode_mode_to_security_class(mode),
2858 &newsid);
2859 if (rc)
2860 return rc;
David Quigleyd47be3d2013-05-22 12:50:34 -04002861
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002862 return security_sid_to_context(&selinux_state, newsid, (char **)ctx,
2863 ctxlen);
David Quigleyd47be3d2013-05-22 12:50:34 -04002864}
2865
Vivek Goyala518b0a2016-07-13 10:44:53 -04002866static int selinux_dentry_create_files_as(struct dentry *dentry, int mode,
2867 struct qstr *name,
2868 const struct cred *old,
2869 struct cred *new)
2870{
2871 u32 newsid;
2872 int rc;
2873 struct task_security_struct *tsec;
2874
2875 rc = selinux_determine_inode_label(old->security,
2876 d_inode(dentry->d_parent), name,
2877 inode_mode_to_security_class(mode),
2878 &newsid);
2879 if (rc)
2880 return rc;
2881
2882 tsec = new->security;
2883 tsec->create_sid = newsid;
2884 return 0;
2885}
2886
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002887static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +09002888 const struct qstr *qstr,
2889 const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -05002890 void **value, size_t *len)
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002891{
Paul Moore5fb49872010-04-22 14:46:19 -04002892 const struct task_security_struct *tsec = current_security();
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002893 struct superblock_security_struct *sbsec;
Corentin LABBEc0d4f462017-10-04 20:32:17 +02002894 u32 newsid, clen;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002895 int rc;
Tetsuo Handa95489062013-07-25 05:44:02 +09002896 char *context;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002897
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002898 sbsec = dir->i_sb->s_security;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002899
David Howells275bb412008-11-14 10:39:19 +11002900 newsid = tsec->create_sid;
2901
Vivek Goyalc957f6d2016-07-13 10:44:51 -04002902 rc = selinux_determine_inode_label(current_security(),
David Howellsc3c188b2015-07-10 17:19:58 -04002903 dir, qstr,
2904 inode_mode_to_security_class(inode->i_mode),
2905 &newsid);
2906 if (rc)
2907 return rc;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002908
Eric Paris296fddf2006-09-25 23:32:00 -07002909 /* Possibly defer initialization to selinux_complete_init. */
David P. Quigley0d90a7e2009-01-16 09:22:02 -05002910 if (sbsec->flags & SE_SBINITIALIZED) {
Eric Paris296fddf2006-09-25 23:32:00 -07002911 struct inode_security_struct *isec = inode->i_security;
2912 isec->sclass = inode_mode_to_security_class(inode->i_mode);
2913 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05002914 isec->initialized = LABEL_INITIALIZED;
Eric Paris296fddf2006-09-25 23:32:00 -07002915 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002916
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002917 if (!selinux_state.initialized || !(sbsec->flags & SBLABEL_MNT))
Stephen Smalley25a74f32005-11-08 21:34:33 -08002918 return -EOPNOTSUPP;
2919
Tetsuo Handa95489062013-07-25 05:44:02 +09002920 if (name)
2921 *name = XATTR_SELINUX_SUFFIX;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002922
2923 if (value && len) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002924 rc = security_sid_to_context_force(&selinux_state, newsid,
2925 &context, &clen);
Tetsuo Handa95489062013-07-25 05:44:02 +09002926 if (rc)
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002927 return rc;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002928 *value = context;
2929 *len = clen;
2930 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002931
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002932 return 0;
2933}
2934
Al Viro4acdaf22011-07-26 01:42:34 -04002935static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936{
2937 return may_create(dir, dentry, SECCLASS_FILE);
2938}
2939
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2941{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 return may_link(dir, old_dentry, MAY_LINK);
2943}
2944
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
2946{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 return may_link(dir, dentry, MAY_UNLINK);
2948}
2949
2950static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
2951{
2952 return may_create(dir, dentry, SECCLASS_LNK_FILE);
2953}
2954
Al Viro18bb1db2011-07-26 01:41:39 -04002955static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956{
2957 return may_create(dir, dentry, SECCLASS_DIR);
2958}
2959
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
2961{
2962 return may_link(dir, dentry, MAY_RMDIR);
2963}
2964
Al Viro1a67aaf2011-07-26 01:52:52 -04002965static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 return may_create(dir, dentry, inode_mode_to_security_class(mode));
2968}
2969
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
Eric Paris828dfe12008-04-17 13:17:49 -04002971 struct inode *new_inode, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972{
2973 return may_rename(old_inode, old_dentry, new_inode, new_dentry);
2974}
2975
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976static int selinux_inode_readlink(struct dentry *dentry)
2977{
David Howells88e67f32008-11-14 10:39:21 +11002978 const struct cred *cred = current_cred();
2979
Eric Paris2875fa02011-04-28 16:04:24 -04002980 return dentry_has_perm(cred, dentry, FILE__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981}
2982
NeilBrownbda0be72015-03-23 13:37:39 +11002983static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode,
2984 bool rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985{
David Howells88e67f32008-11-14 10:39:21 +11002986 const struct cred *cred = current_cred();
NeilBrownbda0be72015-03-23 13:37:39 +11002987 struct common_audit_data ad;
2988 struct inode_security_struct *isec;
2989 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990
NeilBrownbda0be72015-03-23 13:37:39 +11002991 validate_creds(cred);
2992
2993 ad.type = LSM_AUDIT_DATA_DENTRY;
2994 ad.u.dentry = dentry;
2995 sid = cred_sid(cred);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05002996 isec = inode_security_rcu(inode, rcu);
2997 if (IS_ERR(isec))
2998 return PTR_ERR(isec);
NeilBrownbda0be72015-03-23 13:37:39 +11002999
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003000 return avc_has_perm_flags(&selinux_state,
3001 sid, isec->sid, isec->sclass, FILE__READ, &ad,
NeilBrownbda0be72015-03-23 13:37:39 +11003002 rcu ? MAY_NOT_BLOCK : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003}
3004
Eric Parisd4cf970d2012-04-04 15:01:42 -04003005static noinline int audit_inode_permission(struct inode *inode,
3006 u32 perms, u32 audited, u32 denied,
Stephen Smalley626b9742014-04-29 11:29:04 -07003007 int result,
Eric Parisd4cf970d2012-04-04 15:01:42 -04003008 unsigned flags)
3009{
3010 struct common_audit_data ad;
Eric Parisd4cf970d2012-04-04 15:01:42 -04003011 struct inode_security_struct *isec = inode->i_security;
3012 int rc;
3013
Eric Paris50c205f2012-04-04 15:01:43 -04003014 ad.type = LSM_AUDIT_DATA_INODE;
Eric Parisd4cf970d2012-04-04 15:01:42 -04003015 ad.u.inode = inode;
3016
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003017 rc = slow_avc_audit(&selinux_state,
3018 current_sid(), isec->sid, isec->sclass, perms,
Stephen Smalley626b9742014-04-29 11:29:04 -07003019 audited, denied, result, &ad, flags);
Eric Parisd4cf970d2012-04-04 15:01:42 -04003020 if (rc)
3021 return rc;
3022 return 0;
3023}
3024
Al Viroe74f71e2011-06-20 19:38:15 -04003025static int selinux_inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026{
David Howells88e67f32008-11-14 10:39:21 +11003027 const struct cred *cred = current_cred();
Eric Parisb782e0a2010-07-23 11:44:03 -04003028 u32 perms;
3029 bool from_access;
Al Virocf1dd1d2011-06-20 19:44:08 -04003030 unsigned flags = mask & MAY_NOT_BLOCK;
Eric Paris2e334052012-04-04 15:01:42 -04003031 struct inode_security_struct *isec;
3032 u32 sid;
3033 struct av_decision avd;
3034 int rc, rc2;
3035 u32 audited, denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036
Eric Parisb782e0a2010-07-23 11:44:03 -04003037 from_access = mask & MAY_ACCESS;
Eric Parisd09ca732010-07-23 11:43:57 -04003038 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
3039
Eric Parisb782e0a2010-07-23 11:44:03 -04003040 /* No permission to check. Existence test. */
3041 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043
Eric Paris2e334052012-04-04 15:01:42 -04003044 validate_creds(cred);
Eric Parisb782e0a2010-07-23 11:44:03 -04003045
Eric Paris2e334052012-04-04 15:01:42 -04003046 if (unlikely(IS_PRIVATE(inode)))
3047 return 0;
Eric Parisb782e0a2010-07-23 11:44:03 -04003048
3049 perms = file_mask_to_av(inode->i_mode, mask);
3050
Eric Paris2e334052012-04-04 15:01:42 -04003051 sid = cred_sid(cred);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05003052 isec = inode_security_rcu(inode, flags & MAY_NOT_BLOCK);
3053 if (IS_ERR(isec))
3054 return PTR_ERR(isec);
Eric Paris2e334052012-04-04 15:01:42 -04003055
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003056 rc = avc_has_perm_noaudit(&selinux_state,
3057 sid, isec->sid, isec->sclass, perms, 0, &avd);
Eric Paris2e334052012-04-04 15:01:42 -04003058 audited = avc_audit_required(perms, &avd, rc,
3059 from_access ? FILE__AUDIT_ACCESS : 0,
3060 &denied);
3061 if (likely(!audited))
3062 return rc;
3063
Stephen Smalley626b9742014-04-29 11:29:04 -07003064 rc2 = audit_inode_permission(inode, perms, audited, denied, rc, flags);
Eric Paris2e334052012-04-04 15:01:42 -04003065 if (rc2)
3066 return rc2;
3067 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068}
3069
3070static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
3071{
David Howells88e67f32008-11-14 10:39:21 +11003072 const struct cred *cred = current_cred();
Stephen Smalleyccb54472017-05-12 12:41:24 -04003073 struct inode *inode = d_backing_inode(dentry);
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003074 unsigned int ia_valid = iattr->ia_valid;
Eric Paris95dbf732012-04-04 13:45:34 -04003075 __u32 av = FILE__WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003077 /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
3078 if (ia_valid & ATTR_FORCE) {
3079 ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
3080 ATTR_FORCE);
3081 if (!ia_valid)
3082 return 0;
3083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003085 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
3086 ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
Eric Paris2875fa02011-04-28 16:04:24 -04003087 return dentry_has_perm(cred, dentry, FILE__SETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003089 if (selinux_policycap_openperm() &&
Stephen Smalleyccb54472017-05-12 12:41:24 -04003090 inode->i_sb->s_magic != SOCKFS_MAGIC &&
3091 (ia_valid & ATTR_SIZE) &&
3092 !(ia_valid & ATTR_FILE))
Eric Paris95dbf732012-04-04 13:45:34 -04003093 av |= FILE__OPEN;
3094
3095 return dentry_has_perm(cred, dentry, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096}
3097
Al Viro3f7036a2015-03-08 19:28:30 -04003098static int selinux_inode_getattr(const struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099{
Al Viro3f7036a2015-03-08 19:28:30 -04003100 return path_has_perm(current_cred(), path, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101}
3102
Stephen Smalleydb590002017-04-20 11:31:30 -04003103static bool has_cap_mac_admin(bool audit)
3104{
3105 const struct cred *cred = current_cred();
3106 int cap_audit = audit ? SECURITY_CAP_AUDIT : SECURITY_CAP_NOAUDIT;
3107
3108 if (cap_capable(cred, &init_user_ns, CAP_MAC_ADMIN, cap_audit))
3109 return false;
3110 if (cred_has_capability(cred, CAP_MAC_ADMIN, cap_audit, true))
3111 return false;
3112 return true;
3113}
3114
David Howells8f0cfa52008-04-29 00:59:41 -07003115static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
3116 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117{
David Howellsc6f493d2015-03-17 22:26:22 +00003118 struct inode *inode = d_backing_inode(dentry);
Paul Moore20cdef82016-04-04 14:14:42 -04003119 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 struct superblock_security_struct *sbsec;
Thomas Liu2bf49692009-07-14 12:14:09 -04003121 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11003122 u32 newsid, sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 int rc = 0;
3124
Eric W. Biederman6b240302017-10-02 09:38:20 -05003125 if (strcmp(name, XATTR_NAME_SELINUX)) {
3126 rc = cap_inode_setxattr(dentry, name, value, size, flags);
3127 if (rc)
3128 return rc;
3129
3130 /* Not an attribute we recognize, so just check the
3131 ordinary setattr permission. */
3132 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134
3135 sbsec = inode->i_sb->s_security;
Eric Paris12f348b2012-10-09 10:56:25 -04003136 if (!(sbsec->flags & SBLABEL_MNT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 return -EOPNOTSUPP;
3138
Serge E. Hallyn2e149672011-03-23 16:43:26 -07003139 if (!inode_owner_or_capable(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 return -EPERM;
3141
Eric Paris50c205f2012-04-04 15:01:43 -04003142 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04003143 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144
Paul Moore20cdef82016-04-04 14:14:42 -04003145 isec = backing_inode_security(dentry);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003146 rc = avc_has_perm(&selinux_state,
3147 sid, isec->sid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 FILE__RELABELFROM, &ad);
3149 if (rc)
3150 return rc;
3151
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003152 rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3153 GFP_KERNEL);
Stephen Smalley12b29f32008-05-07 13:03:20 -04003154 if (rc == -EINVAL) {
Stephen Smalleydb590002017-04-20 11:31:30 -04003155 if (!has_cap_mac_admin(true)) {
Eric Parisd6ea83e2012-04-04 13:45:49 -04003156 struct audit_buffer *ab;
3157 size_t audit_size;
Eric Parisd6ea83e2012-04-04 13:45:49 -04003158
3159 /* We strip a nul only if it is at the end, otherwise the
3160 * context contains a nul and we should audit that */
Al Viroe3fea3f2012-06-09 08:15:16 +01003161 if (value) {
Colin Ian Kingadd24372017-10-14 13:46:55 +01003162 const char *str = value;
3163
Al Viroe3fea3f2012-06-09 08:15:16 +01003164 if (str[size - 1] == '\0')
3165 audit_size = size - 1;
3166 else
3167 audit_size = size;
3168 } else {
Al Viroe3fea3f2012-06-09 08:15:16 +01003169 audit_size = 0;
3170 }
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04003171 ab = audit_log_start(audit_context(),
3172 GFP_ATOMIC, AUDIT_SELINUX_ERR);
Eric Parisd6ea83e2012-04-04 13:45:49 -04003173 audit_log_format(ab, "op=setxattr invalid_context=");
3174 audit_log_n_untrustedstring(ab, value, audit_size);
3175 audit_log_end(ab);
3176
Stephen Smalley12b29f32008-05-07 13:03:20 -04003177 return rc;
Eric Parisd6ea83e2012-04-04 13:45:49 -04003178 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003179 rc = security_context_to_sid_force(&selinux_state, value,
3180 size, &newsid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04003181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 if (rc)
3183 return rc;
3184
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003185 rc = avc_has_perm(&selinux_state,
3186 sid, newsid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 FILE__RELABELTO, &ad);
3188 if (rc)
3189 return rc;
3190
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003191 rc = security_validate_transition(&selinux_state, isec->sid, newsid,
3192 sid, isec->sclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 if (rc)
3194 return rc;
3195
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003196 return avc_has_perm(&selinux_state,
3197 newsid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 sbsec->sid,
3199 SECCLASS_FILESYSTEM,
3200 FILESYSTEM__ASSOCIATE,
3201 &ad);
3202}
3203
David Howells8f0cfa52008-04-29 00:59:41 -07003204static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
Eric Parisf5269712008-05-14 11:27:45 -04003205 const void *value, size_t size,
David Howells8f0cfa52008-04-29 00:59:41 -07003206 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207{
David Howellsc6f493d2015-03-17 22:26:22 +00003208 struct inode *inode = d_backing_inode(dentry);
Paul Moore20cdef82016-04-04 14:14:42 -04003209 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 u32 newsid;
3211 int rc;
3212
3213 if (strcmp(name, XATTR_NAME_SELINUX)) {
3214 /* Not an attribute we recognize, so nothing to do. */
3215 return;
3216 }
3217
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003218 rc = security_context_to_sid_force(&selinux_state, value, size,
3219 &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 if (rc) {
peter enderborgc103a912018-06-12 10:09:03 +02003221 pr_err("SELinux: unable to map context to SID"
Stephen Smalley12b29f32008-05-07 13:03:20 -04003222 "for (%s, %lu), rc=%d\n",
3223 inode->i_sb->s_id, inode->i_ino, -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 return;
3225 }
3226
Paul Moore20cdef82016-04-04 14:14:42 -04003227 isec = backing_inode_security(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003228 spin_lock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003229 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05003231 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003232 spin_unlock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003233
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 return;
3235}
3236
David Howells8f0cfa52008-04-29 00:59:41 -07003237static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238{
David Howells88e67f32008-11-14 10:39:21 +11003239 const struct cred *cred = current_cred();
3240
Eric Paris2875fa02011-04-28 16:04:24 -04003241 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242}
3243
Eric Paris828dfe12008-04-17 13:17:49 -04003244static int selinux_inode_listxattr(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245{
David Howells88e67f32008-11-14 10:39:21 +11003246 const struct cred *cred = current_cred();
3247
Eric Paris2875fa02011-04-28 16:04:24 -04003248 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249}
3250
David Howells8f0cfa52008-04-29 00:59:41 -07003251static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252{
Eric W. Biederman6b240302017-10-02 09:38:20 -05003253 if (strcmp(name, XATTR_NAME_SELINUX)) {
3254 int rc = cap_inode_removexattr(dentry, name);
3255 if (rc)
3256 return rc;
3257
3258 /* Not an attribute we recognize, so just check the
3259 ordinary setattr permission. */
3260 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262
3263 /* No one is allowed to remove a SELinux security label.
3264 You can change the label, but all data must be labeled. */
3265 return -EACCES;
3266}
3267
James Morrisd381d8a2005-10-30 14:59:22 -08003268/*
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003269 * Copy the inode security context value to the user.
James Morrisd381d8a2005-10-30 14:59:22 -08003270 *
3271 * Permission check is handled by selinux_inode_getxattr hook.
3272 */
Andreas Gruenbacherea861df2015-12-24 11:09:39 -05003273static int selinux_inode_getsecurity(struct inode *inode, const char *name, void **buffer, bool alloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274{
David P. Quigley42492592008-02-04 22:29:39 -08003275 u32 size;
3276 int error;
3277 char *context = NULL;
Paul Moore20cdef82016-04-04 14:14:42 -04003278 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00003280 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3281 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003283 /*
3284 * If the caller has CAP_MAC_ADMIN, then get the raw context
3285 * value even if it is not defined by current policy; otherwise,
3286 * use the in-core value under current policy.
3287 * Use the non-auditing forms of the permission checks since
3288 * getxattr may be called by unprivileged processes commonly
3289 * and lack of permission just means that we fall back to the
3290 * in-core context value, not a denial.
3291 */
Paul Moore20cdef82016-04-04 14:14:42 -04003292 isec = inode_security(inode);
Stephen Smalleydb590002017-04-20 11:31:30 -04003293 if (has_cap_mac_admin(false))
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003294 error = security_sid_to_context_force(&selinux_state,
3295 isec->sid, &context,
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003296 &size);
3297 else
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003298 error = security_sid_to_context(&selinux_state, isec->sid,
3299 &context, &size);
David P. Quigley42492592008-02-04 22:29:39 -08003300 if (error)
3301 return error;
3302 error = size;
3303 if (alloc) {
3304 *buffer = context;
3305 goto out_nofree;
3306 }
3307 kfree(context);
3308out_nofree:
3309 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310}
3311
3312static int selinux_inode_setsecurity(struct inode *inode, const char *name,
Eric Paris828dfe12008-04-17 13:17:49 -04003313 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314{
Paul Moore2c971652016-04-19 16:36:28 -04003315 struct inode_security_struct *isec = inode_security_novalidate(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 u32 newsid;
3317 int rc;
3318
3319 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3320 return -EOPNOTSUPP;
3321
3322 if (!value || !size)
3323 return -EACCES;
3324
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003325 rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3326 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 if (rc)
3328 return rc;
3329
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003330 spin_lock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003331 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05003333 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003334 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 return 0;
3336}
3337
3338static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
3339{
3340 const int len = sizeof(XATTR_NAME_SELINUX);
3341 if (buffer && len <= buffer_size)
3342 memcpy(buffer, XATTR_NAME_SELINUX, len);
3343 return len;
3344}
3345
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05003346static void selinux_inode_getsecid(struct inode *inode, u32 *secid)
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02003347{
Andreas Gruenbachere817c2f2016-02-18 12:04:08 +01003348 struct inode_security_struct *isec = inode_security_novalidate(inode);
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02003349 *secid = isec->sid;
3350}
3351
Vivek Goyal56909eb2016-07-13 10:44:48 -04003352static int selinux_inode_copy_up(struct dentry *src, struct cred **new)
3353{
3354 u32 sid;
3355 struct task_security_struct *tsec;
3356 struct cred *new_creds = *new;
3357
3358 if (new_creds == NULL) {
3359 new_creds = prepare_creds();
3360 if (!new_creds)
3361 return -ENOMEM;
3362 }
3363
3364 tsec = new_creds->security;
3365 /* Get label from overlay inode and set it in create_sid */
3366 selinux_inode_getsecid(d_inode(src), &sid);
3367 tsec->create_sid = sid;
3368 *new = new_creds;
3369 return 0;
3370}
3371
Vivek Goyal19472b62016-07-13 10:44:50 -04003372static int selinux_inode_copy_up_xattr(const char *name)
3373{
3374 /* The copy_up hook above sets the initial context on an inode, but we
3375 * don't then want to overwrite it by blindly copying all the lower
3376 * xattrs up. Instead, we have to filter out SELinux-related xattrs.
3377 */
3378 if (strcmp(name, XATTR_NAME_SELINUX) == 0)
3379 return 1; /* Discard */
3380 /*
3381 * Any other attribute apart from SELINUX is not claimed, supported
3382 * by selinux.
3383 */
3384 return -EOPNOTSUPP;
3385}
3386
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387/* file security operations */
3388
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003389static int selinux_revalidate_file_permission(struct file *file, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390{
David Howells88e67f32008-11-14 10:39:21 +11003391 const struct cred *cred = current_cred();
Al Viro496ad9a2013-01-23 17:07:38 -05003392 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
3395 if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
3396 mask |= MAY_APPEND;
3397
Paul Moore389fb8002009-03-27 17:10:34 -04003398 return file_has_perm(cred, file,
3399 file_mask_to_av(inode->i_mode, mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400}
3401
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003402static int selinux_file_permission(struct file *file, int mask)
3403{
Al Viro496ad9a2013-01-23 17:07:38 -05003404 struct inode *inode = file_inode(file);
Stephen Smalley20dda182009-06-22 14:54:53 -04003405 struct file_security_struct *fsec = file->f_security;
Andreas Gruenbacherb1973672016-01-05 23:12:33 +01003406 struct inode_security_struct *isec;
Stephen Smalley20dda182009-06-22 14:54:53 -04003407 u32 sid = current_sid();
3408
Paul Moore389fb8002009-03-27 17:10:34 -04003409 if (!mask)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003410 /* No permission to check. Existence test. */
3411 return 0;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003412
Andreas Gruenbacherb1973672016-01-05 23:12:33 +01003413 isec = inode_security(inode);
Stephen Smalley20dda182009-06-22 14:54:53 -04003414 if (sid == fsec->sid && fsec->isid == isec->sid &&
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003415 fsec->pseqno == avc_policy_seqno(&selinux_state))
Eric Paris83d49852012-04-04 13:45:40 -04003416 /* No change since file_open check. */
Stephen Smalley20dda182009-06-22 14:54:53 -04003417 return 0;
3418
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003419 return selinux_revalidate_file_permission(file, mask);
3420}
3421
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422static int selinux_file_alloc_security(struct file *file)
3423{
3424 return file_alloc_security(file);
3425}
3426
3427static void selinux_file_free_security(struct file *file)
3428{
3429 file_free_security(file);
3430}
3431
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003432/*
3433 * Check whether a task has the ioctl permission and cmd
3434 * operation to an inode.
3435 */
Geliang Tang1d2a1682015-10-21 17:44:27 -04003436static int ioctl_has_perm(const struct cred *cred, struct file *file,
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003437 u32 requested, u16 cmd)
3438{
3439 struct common_audit_data ad;
3440 struct file_security_struct *fsec = file->f_security;
3441 struct inode *inode = file_inode(file);
Paul Moore20cdef82016-04-04 14:14:42 -04003442 struct inode_security_struct *isec;
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003443 struct lsm_ioctlop_audit ioctl;
3444 u32 ssid = cred_sid(cred);
3445 int rc;
3446 u8 driver = cmd >> 8;
3447 u8 xperm = cmd & 0xff;
3448
3449 ad.type = LSM_AUDIT_DATA_IOCTL_OP;
3450 ad.u.op = &ioctl;
3451 ad.u.op->cmd = cmd;
3452 ad.u.op->path = file->f_path;
3453
3454 if (ssid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003455 rc = avc_has_perm(&selinux_state,
3456 ssid, fsec->sid,
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003457 SECCLASS_FD,
3458 FD__USE,
3459 &ad);
3460 if (rc)
3461 goto out;
3462 }
3463
3464 if (unlikely(IS_PRIVATE(inode)))
3465 return 0;
3466
Paul Moore20cdef82016-04-04 14:14:42 -04003467 isec = inode_security(inode);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003468 rc = avc_has_extended_perms(&selinux_state,
3469 ssid, isec->sid, isec->sclass,
3470 requested, driver, xperm, &ad);
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003471out:
3472 return rc;
3473}
3474
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475static int selinux_file_ioctl(struct file *file, unsigned int cmd,
3476 unsigned long arg)
3477{
David Howells88e67f32008-11-14 10:39:21 +11003478 const struct cred *cred = current_cred();
Eric Paris0b24dcb2011-02-25 15:39:20 -05003479 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480
Eric Paris0b24dcb2011-02-25 15:39:20 -05003481 switch (cmd) {
3482 case FIONREAD:
3483 /* fall through */
3484 case FIBMAP:
3485 /* fall through */
3486 case FIGETBSZ:
3487 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003488 case FS_IOC_GETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003489 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003490 case FS_IOC_GETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003491 error = file_has_perm(cred, file, FILE__GETATTR);
3492 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493
Al Viro2f99c362012-03-23 16:04:05 -04003494 case FS_IOC_SETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003495 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003496 case FS_IOC_SETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003497 error = file_has_perm(cred, file, FILE__SETATTR);
3498 break;
3499
3500 /* sys_ioctl() checks */
3501 case FIONBIO:
3502 /* fall through */
3503 case FIOASYNC:
3504 error = file_has_perm(cred, file, 0);
3505 break;
3506
3507 case KDSKBENT:
3508 case KDSKBSENT:
Eric Paris6a9de492012-01-03 12:25:14 -05003509 error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04003510 SECURITY_CAP_AUDIT, true);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003511 break;
3512
3513 /* default case assumes that the command will go
3514 * to the file's ioctl() function.
3515 */
3516 default:
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003517 error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003518 }
3519 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520}
3521
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003522static int default_noexec;
3523
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
3525{
David Howells88e67f32008-11-14 10:39:21 +11003526 const struct cred *cred = current_cred();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003527 u32 sid = cred_sid(cred);
David Howellsd84f4f92008-11-14 10:39:23 +11003528 int rc = 0;
David Howells88e67f32008-11-14 10:39:21 +11003529
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003530 if (default_noexec &&
Stephen Smalley892e8ca2015-07-10 09:40:59 -04003531 (prot & PROT_EXEC) && (!file || IS_PRIVATE(file_inode(file)) ||
3532 (!shared && (prot & PROT_WRITE)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 /*
3534 * We are making executable an anonymous mapping or a
3535 * private file mapping that will also be writable.
3536 * This has an additional check.
3537 */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003538 rc = avc_has_perm(&selinux_state,
3539 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003540 PROCESS__EXECMEM, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 if (rc)
David Howellsd84f4f92008-11-14 10:39:23 +11003542 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544
3545 if (file) {
3546 /* read access is always possible with a mapping */
3547 u32 av = FILE__READ;
3548
3549 /* write access only matters if the mapping is shared */
3550 if (shared && (prot & PROT_WRITE))
3551 av |= FILE__WRITE;
3552
3553 if (prot & PROT_EXEC)
3554 av |= FILE__EXECUTE;
3555
David Howells88e67f32008-11-14 10:39:21 +11003556 return file_has_perm(cred, file, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 }
David Howellsd84f4f92008-11-14 10:39:23 +11003558
3559error:
3560 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561}
3562
Al Viroe5467852012-05-30 13:30:51 -04003563static int selinux_mmap_addr(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07003565 int rc = 0;
Paul Moore98883bf2014-03-19 16:46:11 -04003566
3567 if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
3568 u32 sid = current_sid();
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003569 rc = avc_has_perm(&selinux_state,
3570 sid, sid, SECCLASS_MEMPROTECT,
Paul Moore98883bf2014-03-19 16:46:11 -04003571 MEMPROTECT__MMAP_ZERO, NULL);
3572 }
3573
3574 return rc;
Al Viroe5467852012-05-30 13:30:51 -04003575}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576
Al Viroe5467852012-05-30 13:30:51 -04003577static int selinux_mmap_file(struct file *file, unsigned long reqprot,
3578 unsigned long prot, unsigned long flags)
3579{
Stephen Smalley3ba4bf52017-05-05 09:14:48 -04003580 struct common_audit_data ad;
3581 int rc;
3582
3583 if (file) {
3584 ad.type = LSM_AUDIT_DATA_FILE;
3585 ad.u.file = file;
3586 rc = inode_has_perm(current_cred(), file_inode(file),
3587 FILE__MAP, &ad);
3588 if (rc)
3589 return rc;
3590 }
3591
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003592 if (selinux_state.checkreqprot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 prot = reqprot;
3594
3595 return file_map_prot_check(file, prot,
3596 (flags & MAP_TYPE) == MAP_SHARED);
3597}
3598
3599static int selinux_file_mprotect(struct vm_area_struct *vma,
3600 unsigned long reqprot,
3601 unsigned long prot)
3602{
David Howells88e67f32008-11-14 10:39:21 +11003603 const struct cred *cred = current_cred();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003604 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003606 if (selinux_state.checkreqprot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 prot = reqprot;
3608
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003609 if (default_noexec &&
3610 (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
James Morrisd541bbe2009-01-29 12:19:51 +11003611 int rc = 0;
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003612 if (vma->vm_start >= vma->vm_mm->start_brk &&
3613 vma->vm_end <= vma->vm_mm->brk) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003614 rc = avc_has_perm(&selinux_state,
3615 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003616 PROCESS__EXECHEAP, NULL);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003617 } else if (!vma->vm_file &&
Stephen Smalleyc2316db2016-04-08 13:55:03 -04003618 ((vma->vm_start <= vma->vm_mm->start_stack &&
3619 vma->vm_end >= vma->vm_mm->start_stack) ||
Andy Lutomirskid17af502016-09-30 10:58:58 -07003620 vma_is_stack_for_current(vma))) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003621 rc = avc_has_perm(&selinux_state,
3622 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003623 PROCESS__EXECSTACK, NULL);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003624 } else if (vma->vm_file && vma->anon_vma) {
3625 /*
3626 * We are making executable a file mapping that has
3627 * had some COW done. Since pages might have been
3628 * written, check ability to execute the possibly
3629 * modified content. This typically should only
3630 * occur for text relocations.
3631 */
David Howellsd84f4f92008-11-14 10:39:23 +11003632 rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003633 }
Lorenzo Hernandez García-Hierro6b992192005-06-25 14:54:34 -07003634 if (rc)
3635 return rc;
3636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637
3638 return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
3639}
3640
3641static int selinux_file_lock(struct file *file, unsigned int cmd)
3642{
David Howells88e67f32008-11-14 10:39:21 +11003643 const struct cred *cred = current_cred();
3644
3645 return file_has_perm(cred, file, FILE__LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646}
3647
3648static int selinux_file_fcntl(struct file *file, unsigned int cmd,
3649 unsigned long arg)
3650{
David Howells88e67f32008-11-14 10:39:21 +11003651 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 int err = 0;
3653
3654 switch (cmd) {
Eric Paris828dfe12008-04-17 13:17:49 -04003655 case F_SETFL:
Eric Paris828dfe12008-04-17 13:17:49 -04003656 if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
David Howells88e67f32008-11-14 10:39:21 +11003657 err = file_has_perm(cred, file, FILE__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003659 }
3660 /* fall through */
3661 case F_SETOWN:
3662 case F_SETSIG:
3663 case F_GETFL:
3664 case F_GETOWN:
3665 case F_GETSIG:
Cyrill Gorcunov1d151c32012-07-30 14:43:00 -07003666 case F_GETOWNER_UIDS:
Eric Paris828dfe12008-04-17 13:17:49 -04003667 /* Just check FD__USE permission */
David Howells88e67f32008-11-14 10:39:21 +11003668 err = file_has_perm(cred, file, 0);
Eric Paris828dfe12008-04-17 13:17:49 -04003669 break;
3670 case F_GETLK:
3671 case F_SETLK:
3672 case F_SETLKW:
Jeff Layton0d3f7a22014-04-22 08:23:58 -04003673 case F_OFD_GETLK:
3674 case F_OFD_SETLK:
3675 case F_OFD_SETLKW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676#if BITS_PER_LONG == 32
Eric Paris828dfe12008-04-17 13:17:49 -04003677 case F_GETLK64:
3678 case F_SETLK64:
3679 case F_SETLKW64:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680#endif
David Howells88e67f32008-11-14 10:39:21 +11003681 err = file_has_perm(cred, file, FILE__LOCK);
Eric Paris828dfe12008-04-17 13:17:49 -04003682 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 }
3684
3685 return err;
3686}
3687
Jeff Laytone0b93ed2014-08-22 11:27:32 -04003688static void selinux_file_set_fowner(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 struct file_security_struct *fsec;
3691
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 fsec = file->f_security;
David Howells275bb412008-11-14 10:39:19 +11003693 fsec->fown_sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694}
3695
3696static int selinux_file_send_sigiotask(struct task_struct *tsk,
3697 struct fown_struct *fown, int signum)
3698{
Eric Paris828dfe12008-04-17 13:17:49 -04003699 struct file *file;
Stephen Smalley65c90bc2009-05-04 15:43:18 -04003700 u32 sid = task_sid(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 struct file_security_struct *fsec;
3703
3704 /* struct fown_struct is never outside the context of a struct file */
Eric Paris828dfe12008-04-17 13:17:49 -04003705 file = container_of(fown, struct file, f_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 fsec = file->f_security;
3708
3709 if (!signum)
3710 perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
3711 else
3712 perm = signal_to_av(signum);
3713
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003714 return avc_has_perm(&selinux_state,
3715 fsec->fown_sid, sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 SECCLASS_PROCESS, perm, NULL);
3717}
3718
3719static int selinux_file_receive(struct file *file)
3720{
David Howells88e67f32008-11-14 10:39:21 +11003721 const struct cred *cred = current_cred();
3722
3723 return file_has_perm(cred, file, file_to_av(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724}
3725
Al Viro94817692018-07-10 14:13:18 -04003726static int selinux_file_open(struct file *file)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003727{
3728 struct file_security_struct *fsec;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003729 struct inode_security_struct *isec;
David Howellsd84f4f92008-11-14 10:39:23 +11003730
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003731 fsec = file->f_security;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05003732 isec = inode_security(file_inode(file));
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003733 /*
3734 * Save inode label and policy sequence number
3735 * at open-time so that selinux_file_permission
3736 * can determine whether revalidation is necessary.
3737 * Task label is already saved in the file security
3738 * struct as its SID.
3739 */
3740 fsec->isid = isec->sid;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003741 fsec->pseqno = avc_policy_seqno(&selinux_state);
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003742 /*
3743 * Since the inode label or policy seqno may have changed
3744 * between the selinux_inode_permission check and the saving
3745 * of state above, recheck that access is still permitted.
3746 * Otherwise, access might never be revalidated against the
3747 * new inode label or new policy.
3748 * This check is not redundant - do not remove.
3749 */
Al Viro94817692018-07-10 14:13:18 -04003750 return file_path_has_perm(file->f_cred, file, open_file_to_av(file));
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003751}
3752
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753/* task security operations */
3754
Tetsuo Handaa79be232017-03-28 23:08:45 +09003755static int selinux_task_alloc(struct task_struct *task,
3756 unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003758 u32 sid = current_sid();
3759
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003760 return avc_has_perm(&selinux_state,
3761 sid, sid, SECCLASS_PROCESS, PROCESS__FORK, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762}
3763
David Howellsf1752ee2008-11-14 10:39:17 +11003764/*
David Howellsee18d642009-09-02 09:14:21 +01003765 * allocate the SELinux part of blank credentials
3766 */
3767static int selinux_cred_alloc_blank(struct cred *cred, gfp_t gfp)
3768{
3769 struct task_security_struct *tsec;
3770
3771 tsec = kzalloc(sizeof(struct task_security_struct), gfp);
3772 if (!tsec)
3773 return -ENOMEM;
3774
3775 cred->security = tsec;
3776 return 0;
3777}
3778
3779/*
David Howellsf1752ee2008-11-14 10:39:17 +11003780 * detach and free the LSM part of a set of credentials
3781 */
3782static void selinux_cred_free(struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783{
David Howellsf1752ee2008-11-14 10:39:17 +11003784 struct task_security_struct *tsec = cred->security;
David Howellse0e81732009-09-02 09:13:40 +01003785
Tetsuo Handa2edeaa32011-02-07 13:36:10 +00003786 /*
3787 * cred->security == NULL if security_cred_alloc_blank() or
3788 * security_prepare_creds() returned an error.
3789 */
3790 BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE);
David Howellse0e81732009-09-02 09:13:40 +01003791 cred->security = (void *) 0x7UL;
David Howellsf1752ee2008-11-14 10:39:17 +11003792 kfree(tsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793}
3794
David Howellsd84f4f92008-11-14 10:39:23 +11003795/*
3796 * prepare a new set of credentials for modification
3797 */
3798static int selinux_cred_prepare(struct cred *new, const struct cred *old,
3799 gfp_t gfp)
3800{
3801 const struct task_security_struct *old_tsec;
3802 struct task_security_struct *tsec;
3803
3804 old_tsec = old->security;
3805
3806 tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp);
3807 if (!tsec)
3808 return -ENOMEM;
3809
3810 new->security = tsec;
3811 return 0;
3812}
3813
3814/*
David Howellsee18d642009-09-02 09:14:21 +01003815 * transfer the SELinux data to a blank set of creds
3816 */
3817static void selinux_cred_transfer(struct cred *new, const struct cred *old)
3818{
3819 const struct task_security_struct *old_tsec = old->security;
3820 struct task_security_struct *tsec = new->security;
3821
3822 *tsec = *old_tsec;
3823}
3824
Matthew Garrett3ec30112018-01-08 13:36:19 -08003825static void selinux_cred_getsecid(const struct cred *c, u32 *secid)
3826{
3827 *secid = cred_sid(c);
3828}
3829
David Howellsee18d642009-09-02 09:14:21 +01003830/*
David Howells3a3b7ce2008-11-14 10:39:28 +11003831 * set the security data for a kernel service
3832 * - all the creation contexts are set to unlabelled
3833 */
3834static int selinux_kernel_act_as(struct cred *new, u32 secid)
3835{
3836 struct task_security_struct *tsec = new->security;
3837 u32 sid = current_sid();
3838 int ret;
3839
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003840 ret = avc_has_perm(&selinux_state,
3841 sid, secid,
David Howells3a3b7ce2008-11-14 10:39:28 +11003842 SECCLASS_KERNEL_SERVICE,
3843 KERNEL_SERVICE__USE_AS_OVERRIDE,
3844 NULL);
3845 if (ret == 0) {
3846 tsec->sid = secid;
3847 tsec->create_sid = 0;
3848 tsec->keycreate_sid = 0;
3849 tsec->sockcreate_sid = 0;
3850 }
3851 return ret;
3852}
3853
3854/*
3855 * set the file creation context in a security record to the same as the
3856 * objective context of the specified inode
3857 */
3858static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
3859{
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05003860 struct inode_security_struct *isec = inode_security(inode);
David Howells3a3b7ce2008-11-14 10:39:28 +11003861 struct task_security_struct *tsec = new->security;
3862 u32 sid = current_sid();
3863 int ret;
3864
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003865 ret = avc_has_perm(&selinux_state,
3866 sid, isec->sid,
David Howells3a3b7ce2008-11-14 10:39:28 +11003867 SECCLASS_KERNEL_SERVICE,
3868 KERNEL_SERVICE__CREATE_FILES_AS,
3869 NULL);
3870
3871 if (ret == 0)
3872 tsec->create_sid = isec->sid;
David Howellsef574712010-02-26 01:56:16 +00003873 return ret;
David Howells3a3b7ce2008-11-14 10:39:28 +11003874}
3875
Eric Parisdd8dbf22009-11-03 16:35:32 +11003876static int selinux_kernel_module_request(char *kmod_name)
Eric Paris25354c42009-08-13 09:45:03 -04003877{
Eric Parisdd8dbf22009-11-03 16:35:32 +11003878 struct common_audit_data ad;
3879
Eric Paris50c205f2012-04-04 15:01:43 -04003880 ad.type = LSM_AUDIT_DATA_KMOD;
Eric Parisdd8dbf22009-11-03 16:35:32 +11003881 ad.u.kmod_name = kmod_name;
3882
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003883 return avc_has_perm(&selinux_state,
3884 current_sid(), SECINITSID_KERNEL, SECCLASS_SYSTEM,
Eric Parisdd8dbf22009-11-03 16:35:32 +11003885 SYSTEM__MODULE_REQUEST, &ad);
Eric Paris25354c42009-08-13 09:45:03 -04003886}
3887
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003888static int selinux_kernel_module_from_file(struct file *file)
3889{
3890 struct common_audit_data ad;
3891 struct inode_security_struct *isec;
3892 struct file_security_struct *fsec;
3893 u32 sid = current_sid();
3894 int rc;
3895
3896 /* init_module */
3897 if (file == NULL)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003898 return avc_has_perm(&selinux_state,
3899 sid, sid, SECCLASS_SYSTEM,
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003900 SYSTEM__MODULE_LOAD, NULL);
3901
3902 /* finit_module */
Paul Moore20cdef82016-04-04 14:14:42 -04003903
Vivek Goyal43af5de2016-09-09 11:37:49 -04003904 ad.type = LSM_AUDIT_DATA_FILE;
3905 ad.u.file = file;
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003906
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003907 fsec = file->f_security;
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003908 if (sid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003909 rc = avc_has_perm(&selinux_state,
3910 sid, fsec->sid, SECCLASS_FD, FD__USE, &ad);
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003911 if (rc)
3912 return rc;
3913 }
3914
Paul Moore20cdef82016-04-04 14:14:42 -04003915 isec = inode_security(file_inode(file));
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003916 return avc_has_perm(&selinux_state,
3917 sid, isec->sid, SECCLASS_SYSTEM,
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003918 SYSTEM__MODULE_LOAD, &ad);
3919}
3920
3921static int selinux_kernel_read_file(struct file *file,
3922 enum kernel_read_file_id id)
3923{
3924 int rc = 0;
3925
3926 switch (id) {
3927 case READING_MODULE:
3928 rc = selinux_kernel_module_from_file(file);
3929 break;
3930 default:
3931 break;
3932 }
3933
3934 return rc;
3935}
3936
Mimi Zoharc77b8cd2018-07-13 14:06:02 -04003937static int selinux_kernel_load_data(enum kernel_load_data_id id)
3938{
3939 int rc = 0;
3940
3941 switch (id) {
3942 case LOADING_MODULE:
3943 rc = selinux_kernel_module_from_file(NULL);
3944 default:
3945 break;
3946 }
3947
3948 return rc;
3949}
3950
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
3952{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003953 return avc_has_perm(&selinux_state,
3954 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003955 PROCESS__SETPGID, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956}
3957
3958static int selinux_task_getpgid(struct task_struct *p)
3959{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003960 return avc_has_perm(&selinux_state,
3961 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003962 PROCESS__GETPGID, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963}
3964
3965static int selinux_task_getsid(struct task_struct *p)
3966{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003967 return avc_has_perm(&selinux_state,
3968 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003969 PROCESS__GETSESSION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970}
3971
David Quigleyf9008e4c2006-06-30 01:55:46 -07003972static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
3973{
David Howells275bb412008-11-14 10:39:19 +11003974 *secid = task_sid(p);
David Quigleyf9008e4c2006-06-30 01:55:46 -07003975}
3976
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977static int selinux_task_setnice(struct task_struct *p, int nice)
3978{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003979 return avc_has_perm(&selinux_state,
3980 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003981 PROCESS__SETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982}
3983
James Morris03e68062006-06-23 02:03:58 -07003984static int selinux_task_setioprio(struct task_struct *p, int ioprio)
3985{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003986 return avc_has_perm(&selinux_state,
3987 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003988 PROCESS__SETSCHED, NULL);
James Morris03e68062006-06-23 02:03:58 -07003989}
3990
David Quigleya1836a42006-06-30 01:55:49 -07003991static int selinux_task_getioprio(struct task_struct *p)
3992{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003993 return avc_has_perm(&selinux_state,
3994 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003995 PROCESS__GETSCHED, NULL);
David Quigleya1836a42006-06-30 01:55:49 -07003996}
3997
Corentin LABBE42985552017-10-04 20:32:18 +02003998static int selinux_task_prlimit(const struct cred *cred, const struct cred *tcred,
3999 unsigned int flags)
Stephen Smalley791ec492017-02-17 07:57:00 -05004000{
4001 u32 av = 0;
4002
Stephen Smalley84e68852017-02-28 09:35:08 -05004003 if (!flags)
4004 return 0;
Stephen Smalley791ec492017-02-17 07:57:00 -05004005 if (flags & LSM_PRLIMIT_WRITE)
4006 av |= PROCESS__SETRLIMIT;
4007 if (flags & LSM_PRLIMIT_READ)
4008 av |= PROCESS__GETRLIMIT;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004009 return avc_has_perm(&selinux_state,
4010 cred_sid(cred), cred_sid(tcred),
Stephen Smalley791ec492017-02-17 07:57:00 -05004011 SECCLASS_PROCESS, av, NULL);
4012}
4013
Jiri Slaby8fd00b42009-08-26 18:41:16 +02004014static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
4015 struct rlimit *new_rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016{
Jiri Slaby8fd00b42009-08-26 18:41:16 +02004017 struct rlimit *old_rlim = p->signal->rlim + resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018
4019 /* Control the ability to change the hard limit (whether
4020 lowering or raising it), so that the hard limit can
4021 later be used as a safe reset point for the soft limit
David Howellsd84f4f92008-11-14 10:39:23 +11004022 upon context transitions. See selinux_bprm_committing_creds. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 if (old_rlim->rlim_max != new_rlim->rlim_max)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004024 return avc_has_perm(&selinux_state,
4025 current_sid(), task_sid(p),
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004026 SECCLASS_PROCESS, PROCESS__SETRLIMIT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027
4028 return 0;
4029}
4030
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09004031static int selinux_task_setscheduler(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004033 return avc_has_perm(&selinux_state,
4034 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004035 PROCESS__SETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036}
4037
4038static int selinux_task_getscheduler(struct task_struct *p)
4039{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004040 return avc_has_perm(&selinux_state,
4041 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004042 PROCESS__GETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043}
4044
David Quigley35601542006-06-23 02:04:01 -07004045static int selinux_task_movememory(struct task_struct *p)
4046{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004047 return avc_has_perm(&selinux_state,
4048 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004049 PROCESS__SETSCHED, NULL);
David Quigley35601542006-06-23 02:04:01 -07004050}
4051
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02004052static int selinux_task_kill(struct task_struct *p, struct kernel_siginfo *info,
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004053 int sig, const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054{
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004055 u32 secid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 if (!sig)
4059 perm = PROCESS__SIGNULL; /* null signal; existence test */
4060 else
4061 perm = signal_to_av(sig);
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004062 if (!cred)
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004063 secid = current_sid();
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004064 else
4065 secid = cred_sid(cred);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004066 return avc_has_perm(&selinux_state,
4067 secid, task_sid(p), SECCLASS_PROCESS, perm, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068}
4069
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070static void selinux_task_to_inode(struct task_struct *p,
4071 struct inode *inode)
4072{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 struct inode_security_struct *isec = inode->i_security;
David Howells275bb412008-11-14 10:39:19 +11004074 u32 sid = task_sid(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004076 spin_lock(&isec->lock);
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01004077 isec->sclass = inode_mode_to_security_class(inode->i_mode);
David Howells275bb412008-11-14 10:39:19 +11004078 isec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004079 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004080 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081}
4082
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004084static int selinux_parse_skb_ipv4(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04004085 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086{
4087 int offset, ihlen, ret = -EINVAL;
4088 struct iphdr _iph, *ih;
4089
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03004090 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
4092 if (ih == NULL)
4093 goto out;
4094
4095 ihlen = ih->ihl * 4;
4096 if (ihlen < sizeof(_iph))
4097 goto out;
4098
Eric Paris48c62af2012-04-02 13:15:44 -04004099 ad->u.net->v4info.saddr = ih->saddr;
4100 ad->u.net->v4info.daddr = ih->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 ret = 0;
4102
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004103 if (proto)
4104 *proto = ih->protocol;
4105
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 switch (ih->protocol) {
Eric Paris828dfe12008-04-17 13:17:49 -04004107 case IPPROTO_TCP: {
4108 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109
Eric Paris828dfe12008-04-17 13:17:49 -04004110 if (ntohs(ih->frag_off) & IP_OFFSET)
4111 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112
4113 offset += ihlen;
4114 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4115 if (th == NULL)
4116 break;
4117
Eric Paris48c62af2012-04-02 13:15:44 -04004118 ad->u.net->sport = th->source;
4119 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122
Eric Paris828dfe12008-04-17 13:17:49 -04004123 case IPPROTO_UDP: {
4124 struct udphdr _udph, *uh;
4125
4126 if (ntohs(ih->frag_off) & IP_OFFSET)
4127 break;
4128
4129 offset += ihlen;
4130 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4131 if (uh == NULL)
4132 break;
4133
Eric Paris48c62af2012-04-02 13:15:44 -04004134 ad->u.net->sport = uh->source;
4135 ad->u.net->dport = uh->dest;
Eric Paris828dfe12008-04-17 13:17:49 -04004136 break;
4137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138
James Morris2ee92d42006-11-13 16:09:01 -08004139 case IPPROTO_DCCP: {
4140 struct dccp_hdr _dccph, *dh;
4141
4142 if (ntohs(ih->frag_off) & IP_OFFSET)
4143 break;
4144
4145 offset += ihlen;
4146 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4147 if (dh == NULL)
4148 break;
4149
Eric Paris48c62af2012-04-02 13:15:44 -04004150 ad->u.net->sport = dh->dccph_sport;
4151 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08004152 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004153 }
James Morris2ee92d42006-11-13 16:09:01 -08004154
Richard Hainesd4529302018-02-13 20:57:18 +00004155#if IS_ENABLED(CONFIG_IP_SCTP)
4156 case IPPROTO_SCTP: {
4157 struct sctphdr _sctph, *sh;
4158
4159 if (ntohs(ih->frag_off) & IP_OFFSET)
4160 break;
4161
4162 offset += ihlen;
4163 sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4164 if (sh == NULL)
4165 break;
4166
4167 ad->u.net->sport = sh->source;
4168 ad->u.net->dport = sh->dest;
4169 break;
4170 }
4171#endif
Eric Paris828dfe12008-04-17 13:17:49 -04004172 default:
4173 break;
4174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175out:
4176 return ret;
4177}
4178
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04004179#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180
4181/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004182static int selinux_parse_skb_ipv6(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04004183 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184{
4185 u8 nexthdr;
4186 int ret = -EINVAL, offset;
4187 struct ipv6hdr _ipv6h, *ip6;
Jesse Gross75f28112011-11-30 17:05:51 -08004188 __be16 frag_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03004190 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
4192 if (ip6 == NULL)
4193 goto out;
4194
Eric Paris48c62af2012-04-02 13:15:44 -04004195 ad->u.net->v6info.saddr = ip6->saddr;
4196 ad->u.net->v6info.daddr = ip6->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 ret = 0;
4198
4199 nexthdr = ip6->nexthdr;
4200 offset += sizeof(_ipv6h);
Jesse Gross75f28112011-11-30 17:05:51 -08004201 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202 if (offset < 0)
4203 goto out;
4204
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004205 if (proto)
4206 *proto = nexthdr;
4207
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 switch (nexthdr) {
4209 case IPPROTO_TCP: {
Eric Paris828dfe12008-04-17 13:17:49 -04004210 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211
4212 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4213 if (th == NULL)
4214 break;
4215
Eric Paris48c62af2012-04-02 13:15:44 -04004216 ad->u.net->sport = th->source;
4217 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 break;
4219 }
4220
4221 case IPPROTO_UDP: {
4222 struct udphdr _udph, *uh;
4223
4224 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4225 if (uh == NULL)
4226 break;
4227
Eric Paris48c62af2012-04-02 13:15:44 -04004228 ad->u.net->sport = uh->source;
4229 ad->u.net->dport = uh->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 break;
4231 }
4232
James Morris2ee92d42006-11-13 16:09:01 -08004233 case IPPROTO_DCCP: {
4234 struct dccp_hdr _dccph, *dh;
4235
4236 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4237 if (dh == NULL)
4238 break;
4239
Eric Paris48c62af2012-04-02 13:15:44 -04004240 ad->u.net->sport = dh->dccph_sport;
4241 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08004242 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004243 }
James Morris2ee92d42006-11-13 16:09:01 -08004244
Richard Hainesd4529302018-02-13 20:57:18 +00004245#if IS_ENABLED(CONFIG_IP_SCTP)
4246 case IPPROTO_SCTP: {
4247 struct sctphdr _sctph, *sh;
4248
4249 sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4250 if (sh == NULL)
4251 break;
4252
4253 ad->u.net->sport = sh->source;
4254 ad->u.net->dport = sh->dest;
4255 break;
4256 }
4257#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 /* includes fragments */
4259 default:
4260 break;
4261 }
4262out:
4263 return ret;
4264}
4265
4266#endif /* IPV6 */
4267
Thomas Liu2bf49692009-07-14 12:14:09 -04004268static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
David Howellscf9481e2008-07-27 21:31:07 +10004269 char **_addrp, int src, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270{
David Howellscf9481e2008-07-27 21:31:07 +10004271 char *addrp;
4272 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273
Eric Paris48c62af2012-04-02 13:15:44 -04004274 switch (ad->u.net->family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 case PF_INET:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004276 ret = selinux_parse_skb_ipv4(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10004277 if (ret)
4278 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04004279 addrp = (char *)(src ? &ad->u.net->v4info.saddr :
4280 &ad->u.net->v4info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10004281 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04004283#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 case PF_INET6:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004285 ret = selinux_parse_skb_ipv6(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10004286 if (ret)
4287 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04004288 addrp = (char *)(src ? &ad->u.net->v6info.saddr :
4289 &ad->u.net->v6info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10004290 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291#endif /* IPV6 */
4292 default:
David Howellscf9481e2008-07-27 21:31:07 +10004293 addrp = NULL;
4294 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 }
4296
David Howellscf9481e2008-07-27 21:31:07 +10004297parse_error:
peter enderborgc103a912018-06-12 10:09:03 +02004298 pr_warn(
David Howellscf9481e2008-07-27 21:31:07 +10004299 "SELinux: failure in selinux_parse_skb(),"
4300 " unable to parse packet\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 return ret;
David Howellscf9481e2008-07-27 21:31:07 +10004302
4303okay:
4304 if (_addrp)
4305 *_addrp = addrp;
4306 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307}
4308
Paul Moore4f6a9932007-03-01 14:35:22 -05004309/**
Paul Moore220deb92008-01-29 08:38:23 -05004310 * selinux_skb_peerlbl_sid - Determine the peer label of a packet
Paul Moore4f6a9932007-03-01 14:35:22 -05004311 * @skb: the packet
Paul Moore75e22912008-01-29 08:38:04 -05004312 * @family: protocol family
Paul Moore220deb92008-01-29 08:38:23 -05004313 * @sid: the packet's peer label SID
Paul Moore4f6a9932007-03-01 14:35:22 -05004314 *
4315 * Description:
Paul Moore220deb92008-01-29 08:38:23 -05004316 * Check the various different forms of network peer labeling and determine
4317 * the peer label/SID for the packet; most of the magic actually occurs in
4318 * the security server function security_net_peersid_cmp(). The function
4319 * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
4320 * or -EACCES if @sid is invalid due to inconsistencies with the different
4321 * peer labels.
Paul Moore4f6a9932007-03-01 14:35:22 -05004322 *
4323 */
Paul Moore220deb92008-01-29 08:38:23 -05004324static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
Paul Moore4f6a9932007-03-01 14:35:22 -05004325{
Paul Moore71f1cb02008-01-29 08:51:16 -05004326 int err;
Paul Moore4f6a9932007-03-01 14:35:22 -05004327 u32 xfrm_sid;
4328 u32 nlbl_sid;
Paul Moore220deb92008-01-29 08:38:23 -05004329 u32 nlbl_type;
Paul Moore4f6a9932007-03-01 14:35:22 -05004330
Paul Moore817eff72013-12-10 14:57:54 -05004331 err = selinux_xfrm_skb_sid(skb, &xfrm_sid);
Paul Moorebed4d7e2013-07-23 17:38:40 -04004332 if (unlikely(err))
4333 return -EACCES;
4334 err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
4335 if (unlikely(err))
4336 return -EACCES;
Paul Moore220deb92008-01-29 08:38:23 -05004337
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004338 err = security_net_peersid_resolve(&selinux_state, nlbl_sid,
4339 nlbl_type, xfrm_sid, sid);
Paul Moore71f1cb02008-01-29 08:51:16 -05004340 if (unlikely(err)) {
peter enderborgc103a912018-06-12 10:09:03 +02004341 pr_warn(
Paul Moore71f1cb02008-01-29 08:51:16 -05004342 "SELinux: failure in selinux_skb_peerlbl_sid(),"
4343 " unable to determine packet's peer label\n");
Paul Moore220deb92008-01-29 08:38:23 -05004344 return -EACCES;
Paul Moore71f1cb02008-01-29 08:51:16 -05004345 }
Paul Moore220deb92008-01-29 08:38:23 -05004346
4347 return 0;
Paul Moore4f6a9932007-03-01 14:35:22 -05004348}
4349
Paul Moore446b8022013-12-04 16:10:51 -05004350/**
4351 * selinux_conn_sid - Determine the child socket label for a connection
4352 * @sk_sid: the parent socket's SID
4353 * @skb_sid: the packet's SID
4354 * @conn_sid: the resulting connection SID
4355 *
4356 * If @skb_sid is valid then the user:role:type information from @sk_sid is
4357 * combined with the MLS information from @skb_sid in order to create
4358 * @conn_sid. If @skb_sid is not valid then then @conn_sid is simply a copy
4359 * of @sk_sid. Returns zero on success, negative values on failure.
4360 *
4361 */
4362static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid)
4363{
4364 int err = 0;
4365
4366 if (skb_sid != SECSID_NULL)
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004367 err = security_sid_mls_copy(&selinux_state, sk_sid, skb_sid,
4368 conn_sid);
Paul Moore446b8022013-12-04 16:10:51 -05004369 else
4370 *conn_sid = sk_sid;
4371
4372 return err;
4373}
4374
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375/* socket security operations */
Paul Moored4f2d972010-04-22 14:46:18 -04004376
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004377static int socket_sockcreate_sid(const struct task_security_struct *tsec,
4378 u16 secclass, u32 *socksid)
Paul Moored4f2d972010-04-22 14:46:18 -04004379{
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004380 if (tsec->sockcreate_sid > SECSID_NULL) {
4381 *socksid = tsec->sockcreate_sid;
4382 return 0;
4383 }
4384
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004385 return security_transition_sid(&selinux_state, tsec->sid, tsec->sid,
4386 secclass, NULL, socksid);
Paul Moored4f2d972010-04-22 14:46:18 -04004387}
4388
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004389static int sock_has_perm(struct sock *sk, u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390{
Paul Moore253bfae2010-04-22 14:46:19 -04004391 struct sk_security_struct *sksec = sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004392 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004393 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394
Paul Moore253bfae2010-04-22 14:46:19 -04004395 if (sksec->sid == SECINITSID_KERNEL)
4396 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397
Eric Paris50c205f2012-04-04 15:01:43 -04004398 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004399 ad.u.net = &net;
4400 ad.u.net->sk = sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004402 return avc_has_perm(&selinux_state,
4403 current_sid(), sksec->sid, sksec->sclass, perms,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004404 &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405}
4406
4407static int selinux_socket_create(int family, int type,
4408 int protocol, int kern)
4409{
Paul Moore5fb49872010-04-22 14:46:19 -04004410 const struct task_security_struct *tsec = current_security();
Paul Moored4f2d972010-04-22 14:46:18 -04004411 u32 newsid;
David Howells275bb412008-11-14 10:39:19 +11004412 u16 secclass;
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004413 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414
4415 if (kern)
Paul Moored4f2d972010-04-22 14:46:18 -04004416 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417
David Howells275bb412008-11-14 10:39:19 +11004418 secclass = socket_type_to_security_class(family, type, protocol);
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004419 rc = socket_sockcreate_sid(tsec, secclass, &newsid);
4420 if (rc)
4421 return rc;
4422
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004423 return avc_has_perm(&selinux_state,
4424 tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425}
4426
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07004427static int selinux_socket_post_create(struct socket *sock, int family,
4428 int type, int protocol, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429{
Paul Moore5fb49872010-04-22 14:46:19 -04004430 const struct task_security_struct *tsec = current_security();
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05004431 struct inode_security_struct *isec = inode_security_novalidate(SOCK_INODE(sock));
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004432 struct sk_security_struct *sksec;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004433 u16 sclass = socket_type_to_security_class(family, type, protocol);
4434 u32 sid = SECINITSID_KERNEL;
David Howells275bb412008-11-14 10:39:19 +11004435 int err = 0;
4436
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004437 if (!kern) {
4438 err = socket_sockcreate_sid(tsec, sclass, &sid);
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004439 if (err)
4440 return err;
4441 }
David Howells275bb412008-11-14 10:39:19 +11004442
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004443 isec->sclass = sclass;
4444 isec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004445 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004447 if (sock->sk) {
4448 sksec = sock->sk->sk_security;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004449 sksec->sclass = sclass;
4450 sksec->sid = sid;
Richard Hainesd4529302018-02-13 20:57:18 +00004451 /* Allows detection of the first association on this socket */
4452 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4453 sksec->sctp_assoc_state = SCTP_ASSOC_UNSET;
4454
Paul Moore389fb8002009-03-27 17:10:34 -04004455 err = selinux_netlbl_socket_post_create(sock->sk, family);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004456 }
4457
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07004458 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459}
4460
David Herrmann0b811db2018-05-04 16:28:21 +02004461static int selinux_socket_socketpair(struct socket *socka,
4462 struct socket *sockb)
4463{
4464 struct sk_security_struct *sksec_a = socka->sk->sk_security;
4465 struct sk_security_struct *sksec_b = sockb->sk->sk_security;
4466
4467 sksec_a->peer_sid = sksec_b->sid;
4468 sksec_b->peer_sid = sksec_a->sid;
4469
4470 return 0;
4471}
4472
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473/* Range of port numbers used to automatically bind.
4474 Need to determine whether we should perform a name_bind
4475 permission check between the socket and the port number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476
4477static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
4478{
Paul Moore253bfae2010-04-22 14:46:19 -04004479 struct sock *sk = sock->sk;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004480 struct sk_security_struct *sksec = sk->sk_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 u16 family;
4482 int err;
4483
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004484 err = sock_has_perm(sk, SOCKET__BIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 if (err)
4486 goto out;
4487
Richard Hainesd4529302018-02-13 20:57:18 +00004488 /* If PF_INET or PF_INET6, check name_bind permission for the port. */
Paul Moore253bfae2010-04-22 14:46:19 -04004489 family = sk->sk_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 if (family == PF_INET || family == PF_INET6) {
4491 char *addrp;
Thomas Liu2bf49692009-07-14 12:14:09 -04004492 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004493 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 struct sockaddr_in *addr4 = NULL;
4495 struct sockaddr_in6 *addr6 = NULL;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004496 u16 family_sa = address->sa_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 unsigned short snum;
James Morrise399f982008-06-12 01:39:58 +10004498 u32 sid, node_perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499
Richard Hainesd4529302018-02-13 20:57:18 +00004500 /*
4501 * sctp_bindx(3) calls via selinux_sctp_bind_connect()
4502 * that validates multiple binding addresses. Because of this
4503 * need to check address->sa_family as it is possible to have
4504 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4505 */
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004506 switch (family_sa) {
4507 case AF_UNSPEC:
Richard Haines68741a8a2018-03-02 19:54:34 +00004508 case AF_INET:
4509 if (addrlen < sizeof(struct sockaddr_in))
4510 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 addr4 = (struct sockaddr_in *)address;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004512 if (family_sa == AF_UNSPEC) {
4513 /* see __inet_bind(), we only want to allow
4514 * AF_UNSPEC if the address is INADDR_ANY
4515 */
4516 if (addr4->sin_addr.s_addr != htonl(INADDR_ANY))
4517 goto err_af;
4518 family_sa = AF_INET;
4519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520 snum = ntohs(addr4->sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 addrp = (char *)&addr4->sin_addr.s_addr;
Richard Haines68741a8a2018-03-02 19:54:34 +00004522 break;
4523 case AF_INET6:
4524 if (addrlen < SIN6_LEN_RFC2133)
4525 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 addr6 = (struct sockaddr_in6 *)address;
4527 snum = ntohs(addr6->sin6_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 addrp = (char *)&addr6->sin6_addr.s6_addr;
Richard Haines68741a8a2018-03-02 19:54:34 +00004529 break;
4530 default:
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004531 goto err_af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 }
4533
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004534 ad.type = LSM_AUDIT_DATA_NET;
4535 ad.u.net = &net;
4536 ad.u.net->sport = htons(snum);
4537 ad.u.net->family = family_sa;
4538
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004539 if (snum) {
4540 int low, high;
4541
Eric W. Biederman0bbf87d2013-09-28 14:10:59 -07004542 inet_get_local_port_range(sock_net(sk), &low, &high);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004543
Krister Johansen4548b682017-01-20 17:49:11 -08004544 if (snum < max(inet_prot_sock(sock_net(sk)), low) ||
4545 snum > high) {
Paul Moore3e1121722008-04-10 10:48:14 -04004546 err = sel_netport_sid(sk->sk_protocol,
4547 snum, &sid);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004548 if (err)
4549 goto out;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004550 err = avc_has_perm(&selinux_state,
4551 sksec->sid, sid,
Paul Moore253bfae2010-04-22 14:46:19 -04004552 sksec->sclass,
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004553 SOCKET__NAME_BIND, &ad);
4554 if (err)
4555 goto out;
4556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 }
Eric Paris828dfe12008-04-17 13:17:49 -04004558
Paul Moore253bfae2010-04-22 14:46:19 -04004559 switch (sksec->sclass) {
James Morris13402582005-09-30 14:24:34 -04004560 case SECCLASS_TCP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561 node_perm = TCP_SOCKET__NODE_BIND;
4562 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004563
James Morris13402582005-09-30 14:24:34 -04004564 case SECCLASS_UDP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 node_perm = UDP_SOCKET__NODE_BIND;
4566 break;
James Morris2ee92d42006-11-13 16:09:01 -08004567
4568 case SECCLASS_DCCP_SOCKET:
4569 node_perm = DCCP_SOCKET__NODE_BIND;
4570 break;
4571
Richard Hainesd4529302018-02-13 20:57:18 +00004572 case SECCLASS_SCTP_SOCKET:
4573 node_perm = SCTP_SOCKET__NODE_BIND;
4574 break;
4575
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 default:
4577 node_perm = RAWIP_SOCKET__NODE_BIND;
4578 break;
4579 }
Eric Paris828dfe12008-04-17 13:17:49 -04004580
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004581 err = sel_netnode_sid(addrp, family_sa, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 if (err)
4583 goto out;
Eric Paris828dfe12008-04-17 13:17:49 -04004584
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004585 if (family_sa == AF_INET)
Eric Paris48c62af2012-04-02 13:15:44 -04004586 ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 else
Eric Paris48c62af2012-04-02 13:15:44 -04004588 ad.u.net->v6info.saddr = addr6->sin6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004590 err = avc_has_perm(&selinux_state,
4591 sksec->sid, sid,
Paul Moore253bfae2010-04-22 14:46:19 -04004592 sksec->sclass, node_perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 if (err)
4594 goto out;
4595 }
4596out:
4597 return err;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004598err_af:
4599 /* Note that SCTP services expect -EINVAL, others -EAFNOSUPPORT. */
4600 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4601 return -EINVAL;
4602 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603}
4604
Richard Hainesd4529302018-02-13 20:57:18 +00004605/* This supports connect(2) and SCTP connect services such as sctp_connectx(3)
Mauro Carvalho Chehab5fb94e92018-05-08 15:14:57 -03004606 * and sctp_sendmsg(3) as described in Documentation/security/LSM-sctp.rst
Richard Hainesd4529302018-02-13 20:57:18 +00004607 */
4608static int selinux_socket_connect_helper(struct socket *sock,
4609 struct sockaddr *address, int addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610{
Paul Moore014ab192008-10-10 10:16:33 -04004611 struct sock *sk = sock->sk;
Paul Moore253bfae2010-04-22 14:46:19 -04004612 struct sk_security_struct *sksec = sk->sk_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 int err;
4614
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004615 err = sock_has_perm(sk, SOCKET__CONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616 if (err)
4617 return err;
4618
4619 /*
Richard Hainesd4529302018-02-13 20:57:18 +00004620 * If a TCP, DCCP or SCTP socket, check name_connect permission
4621 * for the port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 */
Paul Moore253bfae2010-04-22 14:46:19 -04004623 if (sksec->sclass == SECCLASS_TCP_SOCKET ||
Richard Hainesd4529302018-02-13 20:57:18 +00004624 sksec->sclass == SECCLASS_DCCP_SOCKET ||
4625 sksec->sclass == SECCLASS_SCTP_SOCKET) {
Thomas Liu2bf49692009-07-14 12:14:09 -04004626 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004627 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628 struct sockaddr_in *addr4 = NULL;
4629 struct sockaddr_in6 *addr6 = NULL;
4630 unsigned short snum;
James Morris2ee92d42006-11-13 16:09:01 -08004631 u32 sid, perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632
Richard Hainesd4529302018-02-13 20:57:18 +00004633 /* sctp_connectx(3) calls via selinux_sctp_bind_connect()
4634 * that validates multiple connect addresses. Because of this
4635 * need to check address->sa_family as it is possible to have
4636 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4637 */
Richard Haines68741a8a2018-03-02 19:54:34 +00004638 switch (address->sa_family) {
4639 case AF_INET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 addr4 = (struct sockaddr_in *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004641 if (addrlen < sizeof(struct sockaddr_in))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 return -EINVAL;
4643 snum = ntohs(addr4->sin_port);
Richard Haines68741a8a2018-03-02 19:54:34 +00004644 break;
4645 case AF_INET6:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 addr6 = (struct sockaddr_in6 *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004647 if (addrlen < SIN6_LEN_RFC2133)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 return -EINVAL;
4649 snum = ntohs(addr6->sin6_port);
Richard Haines68741a8a2018-03-02 19:54:34 +00004650 break;
4651 default:
4652 /* Note that SCTP services expect -EINVAL, whereas
4653 * others expect -EAFNOSUPPORT.
4654 */
4655 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4656 return -EINVAL;
4657 else
4658 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659 }
4660
Paul Moore3e1121722008-04-10 10:48:14 -04004661 err = sel_netport_sid(sk->sk_protocol, snum, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 if (err)
Richard Hainesd4529302018-02-13 20:57:18 +00004663 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664
Richard Hainesd4529302018-02-13 20:57:18 +00004665 switch (sksec->sclass) {
4666 case SECCLASS_TCP_SOCKET:
4667 perm = TCP_SOCKET__NAME_CONNECT;
4668 break;
4669 case SECCLASS_DCCP_SOCKET:
4670 perm = DCCP_SOCKET__NAME_CONNECT;
4671 break;
4672 case SECCLASS_SCTP_SOCKET:
4673 perm = SCTP_SOCKET__NAME_CONNECT;
4674 break;
4675 }
James Morris2ee92d42006-11-13 16:09:01 -08004676
Eric Paris50c205f2012-04-04 15:01:43 -04004677 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004678 ad.u.net = &net;
4679 ad.u.net->dport = htons(snum);
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004680 ad.u.net->family = address->sa_family;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004681 err = avc_has_perm(&selinux_state,
4682 sksec->sid, sid, sksec->sclass, perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683 if (err)
Richard Hainesd4529302018-02-13 20:57:18 +00004684 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685 }
4686
Richard Hainesd4529302018-02-13 20:57:18 +00004687 return 0;
4688}
Paul Moore014ab192008-10-10 10:16:33 -04004689
Richard Hainesd4529302018-02-13 20:57:18 +00004690/* Supports connect(2), see comments in selinux_socket_connect_helper() */
4691static int selinux_socket_connect(struct socket *sock,
4692 struct sockaddr *address, int addrlen)
4693{
4694 int err;
4695 struct sock *sk = sock->sk;
4696
4697 err = selinux_socket_connect_helper(sock, address, addrlen);
4698 if (err)
4699 return err;
4700
4701 return selinux_netlbl_socket_connect(sk, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702}
4703
4704static int selinux_socket_listen(struct socket *sock, int backlog)
4705{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004706 return sock_has_perm(sock->sk, SOCKET__LISTEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707}
4708
4709static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
4710{
4711 int err;
4712 struct inode_security_struct *isec;
4713 struct inode_security_struct *newisec;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004714 u16 sclass;
4715 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004717 err = sock_has_perm(sock->sk, SOCKET__ACCEPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718 if (err)
4719 return err;
4720
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05004721 isec = inode_security_novalidate(SOCK_INODE(sock));
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004722 spin_lock(&isec->lock);
4723 sclass = isec->sclass;
4724 sid = isec->sid;
4725 spin_unlock(&isec->lock);
4726
4727 newisec = inode_security_novalidate(SOCK_INODE(newsock));
4728 newisec->sclass = sclass;
4729 newisec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004730 newisec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731
4732 return 0;
4733}
4734
4735static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
Eric Paris828dfe12008-04-17 13:17:49 -04004736 int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004738 return sock_has_perm(sock->sk, SOCKET__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739}
4740
4741static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
4742 int size, int flags)
4743{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004744 return sock_has_perm(sock->sk, SOCKET__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745}
4746
4747static int selinux_socket_getsockname(struct socket *sock)
4748{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004749 return sock_has_perm(sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750}
4751
4752static int selinux_socket_getpeername(struct socket *sock)
4753{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004754 return sock_has_perm(sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755}
4756
Eric Paris828dfe12008-04-17 13:17:49 -04004757static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758{
Paul Mooref8687af2006-10-30 15:22:15 -08004759 int err;
4760
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004761 err = sock_has_perm(sock->sk, SOCKET__SETOPT);
Paul Mooref8687af2006-10-30 15:22:15 -08004762 if (err)
4763 return err;
4764
4765 return selinux_netlbl_socket_setsockopt(sock, level, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766}
4767
4768static int selinux_socket_getsockopt(struct socket *sock, int level,
4769 int optname)
4770{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004771 return sock_has_perm(sock->sk, SOCKET__GETOPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772}
4773
4774static int selinux_socket_shutdown(struct socket *sock, int how)
4775{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004776 return sock_has_perm(sock->sk, SOCKET__SHUTDOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777}
4778
David S. Miller3610cda2011-01-05 15:38:53 -08004779static int selinux_socket_unix_stream_connect(struct sock *sock,
4780 struct sock *other,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 struct sock *newsk)
4782{
David S. Miller3610cda2011-01-05 15:38:53 -08004783 struct sk_security_struct *sksec_sock = sock->sk_security;
4784 struct sk_security_struct *sksec_other = other->sk_security;
Paul Moore4d1e2452010-04-22 14:46:18 -04004785 struct sk_security_struct *sksec_new = newsk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004786 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004787 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788 int err;
4789
Eric Paris50c205f2012-04-04 15:01:43 -04004790 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004791 ad.u.net = &net;
4792 ad.u.net->sk = other;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004794 err = avc_has_perm(&selinux_state,
4795 sksec_sock->sid, sksec_other->sid,
Paul Moore4d1e2452010-04-22 14:46:18 -04004796 sksec_other->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797 UNIX_STREAM_SOCKET__CONNECTTO, &ad);
4798 if (err)
4799 return err;
4800
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 /* server child socket */
Paul Moore4d1e2452010-04-22 14:46:18 -04004802 sksec_new->peer_sid = sksec_sock->sid;
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004803 err = security_sid_mls_copy(&selinux_state, sksec_other->sid,
4804 sksec_sock->sid, &sksec_new->sid);
Paul Moore4d1e2452010-04-22 14:46:18 -04004805 if (err)
4806 return err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004807
Paul Moore4d1e2452010-04-22 14:46:18 -04004808 /* connecting socket */
4809 sksec_sock->peer_sid = sksec_new->sid;
4810
4811 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812}
4813
4814static int selinux_socket_unix_may_send(struct socket *sock,
4815 struct socket *other)
4816{
Paul Moore253bfae2010-04-22 14:46:19 -04004817 struct sk_security_struct *ssec = sock->sk->sk_security;
4818 struct sk_security_struct *osec = other->sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004819 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004820 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821
Eric Paris50c205f2012-04-04 15:01:43 -04004822 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004823 ad.u.net = &net;
4824 ad.u.net->sk = other->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004826 return avc_has_perm(&selinux_state,
4827 ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
Paul Moore253bfae2010-04-22 14:46:19 -04004828 &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829}
4830
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004831static int selinux_inet_sys_rcv_skb(struct net *ns, int ifindex,
4832 char *addrp, u16 family, u32 peer_sid,
Thomas Liu2bf49692009-07-14 12:14:09 -04004833 struct common_audit_data *ad)
Paul Mooreeffad8d2008-01-29 08:49:27 -05004834{
4835 int err;
4836 u32 if_sid;
4837 u32 node_sid;
4838
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004839 err = sel_netif_sid(ns, ifindex, &if_sid);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004840 if (err)
4841 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004842 err = avc_has_perm(&selinux_state,
4843 peer_sid, if_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004844 SECCLASS_NETIF, NETIF__INGRESS, ad);
4845 if (err)
4846 return err;
4847
4848 err = sel_netnode_sid(addrp, family, &node_sid);
4849 if (err)
4850 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004851 return avc_has_perm(&selinux_state,
4852 peer_sid, node_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004853 SECCLASS_NODE, NODE__RECVFROM, ad);
4854}
4855
Paul Moore220deb92008-01-29 08:38:23 -05004856static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
Paul Moored8395c82008-10-10 10:16:30 -04004857 u16 family)
Paul Moore220deb92008-01-29 08:38:23 -05004858{
Paul Moore277d3422008-12-31 12:54:11 -05004859 int err = 0;
Paul Moore220deb92008-01-29 08:38:23 -05004860 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004861 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004862 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004863 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04004864 char *addrp;
4865
Eric Paris50c205f2012-04-04 15:01:43 -04004866 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004867 ad.u.net = &net;
4868 ad.u.net->netif = skb->skb_iif;
4869 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004870 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4871 if (err)
4872 return err;
Paul Moore220deb92008-01-29 08:38:23 -05004873
Paul Moore58bfbb52009-03-27 17:10:41 -04004874 if (selinux_secmark_enabled()) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004875 err = avc_has_perm(&selinux_state,
4876 sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Moored8395c82008-10-10 10:16:30 -04004877 PACKET__RECV, &ad);
Paul Moore58bfbb52009-03-27 17:10:41 -04004878 if (err)
4879 return err;
4880 }
Paul Moore220deb92008-01-29 08:38:23 -05004881
Steffen Klassertb9679a72011-02-23 12:55:21 +01004882 err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
4883 if (err)
4884 return err;
4885 err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004886
James Morris4e5ab4c2006-06-09 00:33:33 -07004887 return err;
4888}
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004889
James Morris4e5ab4c2006-06-09 00:33:33 -07004890static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
4891{
Paul Moore220deb92008-01-29 08:38:23 -05004892 int err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004893 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004894 u16 family = sk->sk_family;
4895 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004896 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004897 struct lsm_network_audit net = {0,};
Paul Moore220deb92008-01-29 08:38:23 -05004898 char *addrp;
Paul Moored8395c82008-10-10 10:16:30 -04004899 u8 secmark_active;
4900 u8 peerlbl_active;
James Morris4e5ab4c2006-06-09 00:33:33 -07004901
James Morris4e5ab4c2006-06-09 00:33:33 -07004902 if (family != PF_INET && family != PF_INET6)
Paul Moore220deb92008-01-29 08:38:23 -05004903 return 0;
James Morris4e5ab4c2006-06-09 00:33:33 -07004904
4905 /* Handle mapped IPv4 packets arriving via IPv6 sockets */
Al Viro87fcd702006-12-04 22:00:55 +00004906 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
James Morris4e5ab4c2006-06-09 00:33:33 -07004907 family = PF_INET;
4908
Paul Moored8395c82008-10-10 10:16:30 -04004909 /* If any sort of compatibility mode is enabled then handoff processing
4910 * to the selinux_sock_rcv_skb_compat() function to deal with the
4911 * special handling. We do this in an attempt to keep this function
4912 * as fast and as clean as possible. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004913 if (!selinux_policycap_netpeer())
Paul Moored8395c82008-10-10 10:16:30 -04004914 return selinux_sock_rcv_skb_compat(sk, skb, family);
4915
4916 secmark_active = selinux_secmark_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04004917 peerlbl_active = selinux_peerlbl_enabled();
Paul Moored8395c82008-10-10 10:16:30 -04004918 if (!secmark_active && !peerlbl_active)
4919 return 0;
4920
Eric Paris50c205f2012-04-04 15:01:43 -04004921 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004922 ad.u.net = &net;
4923 ad.u.net->netif = skb->skb_iif;
4924 ad.u.net->family = family;
Paul Moore224dfbd2008-01-29 08:38:13 -05004925 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
James Morris4e5ab4c2006-06-09 00:33:33 -07004926 if (err)
Paul Moore220deb92008-01-29 08:38:23 -05004927 return err;
James Morris4e5ab4c2006-06-09 00:33:33 -07004928
Paul Moored8395c82008-10-10 10:16:30 -04004929 if (peerlbl_active) {
Paul Moored621d352008-01-29 08:43:36 -05004930 u32 peer_sid;
4931
4932 err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
4933 if (err)
4934 return err;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004935 err = selinux_inet_sys_rcv_skb(sock_net(sk), skb->skb_iif,
4936 addrp, family, peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04004937 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04004938 selinux_netlbl_err(skb, family, err, 0);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004939 return err;
Paul Mooredfaebe92008-10-10 10:16:31 -04004940 }
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004941 err = avc_has_perm(&selinux_state,
4942 sk_sid, peer_sid, SECCLASS_PEER,
Paul Moored621d352008-01-29 08:43:36 -05004943 PEER__RECV, &ad);
Chad Hanson46d01d62013-12-23 17:45:01 -05004944 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04004945 selinux_netlbl_err(skb, family, err, 0);
Chad Hanson46d01d62013-12-23 17:45:01 -05004946 return err;
4947 }
Paul Moored621d352008-01-29 08:43:36 -05004948 }
4949
Paul Moored8395c82008-10-10 10:16:30 -04004950 if (secmark_active) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004951 err = avc_has_perm(&selinux_state,
4952 sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004953 PACKET__RECV, &ad);
4954 if (err)
4955 return err;
4956 }
4957
Paul Moored621d352008-01-29 08:43:36 -05004958 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959}
4960
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004961static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
4962 int __user *optlen, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963{
4964 int err = 0;
4965 char *scontext;
4966 u32 scontext_len;
Paul Moore253bfae2010-04-22 14:46:19 -04004967 struct sk_security_struct *sksec = sock->sk->sk_security;
Paul Moore3de4bab2006-11-17 17:38:54 -05004968 u32 peer_sid = SECSID_NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969
Paul Moore253bfae2010-04-22 14:46:19 -04004970 if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
Richard Hainesd4529302018-02-13 20:57:18 +00004971 sksec->sclass == SECCLASS_TCP_SOCKET ||
4972 sksec->sclass == SECCLASS_SCTP_SOCKET)
Eric Parisdd3e7832010-04-07 15:08:46 -04004973 peer_sid = sksec->peer_sid;
Paul Moore253bfae2010-04-22 14:46:19 -04004974 if (peer_sid == SECSID_NULL)
4975 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004977 err = security_sid_to_context(&selinux_state, peer_sid, &scontext,
4978 &scontext_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 if (err)
Paul Moore253bfae2010-04-22 14:46:19 -04004980 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981
4982 if (scontext_len > len) {
4983 err = -ERANGE;
4984 goto out_len;
4985 }
4986
4987 if (copy_to_user(optval, scontext, scontext_len))
4988 err = -EFAULT;
4989
4990out_len:
4991 if (put_user(scontext_len, optlen))
4992 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993 kfree(scontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994 return err;
4995}
4996
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004997static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004998{
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004999 u32 peer_secid = SECSID_NULL;
Paul Moore75e22912008-01-29 08:38:04 -05005000 u16 family;
Paul Moore899134f2016-03-28 15:19:10 -04005001 struct inode_security_struct *isec;
Catherine Zhang877ce7c2006-06-29 12:27:47 -07005002
Paul Mooreaa862902008-10-10 10:16:29 -04005003 if (skb && skb->protocol == htons(ETH_P_IP))
5004 family = PF_INET;
5005 else if (skb && skb->protocol == htons(ETH_P_IPV6))
5006 family = PF_INET6;
5007 else if (sock)
Paul Moore75e22912008-01-29 08:38:04 -05005008 family = sock->sk->sk_family;
Paul Moore75e22912008-01-29 08:38:04 -05005009 else
5010 goto out;
5011
Paul Moore899134f2016-03-28 15:19:10 -04005012 if (sock && family == PF_UNIX) {
5013 isec = inode_security_novalidate(SOCK_INODE(sock));
5014 peer_secid = isec->sid;
5015 } else if (skb)
Paul Moore220deb92008-01-29 08:38:23 -05005016 selinux_skb_peerlbl_sid(skb, family, &peer_secid);
Catherine Zhang2c7946a2006-03-20 22:41:23 -08005017
Paul Moore75e22912008-01-29 08:38:04 -05005018out:
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005019 *secid = peer_secid;
Paul Moore75e22912008-01-29 08:38:04 -05005020 if (peer_secid == SECSID_NULL)
5021 return -EINVAL;
5022 return 0;
Catherine Zhang2c7946a2006-03-20 22:41:23 -08005023}
5024
Al Viro7d877f32005-10-21 03:20:43 -04005025static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026{
Paul Moore84914b72010-04-22 14:46:18 -04005027 struct sk_security_struct *sksec;
5028
5029 sksec = kzalloc(sizeof(*sksec), priority);
5030 if (!sksec)
5031 return -ENOMEM;
5032
5033 sksec->peer_sid = SECINITSID_UNLABELED;
5034 sksec->sid = SECINITSID_UNLABELED;
Stephen Smalley5dee25d2015-07-10 17:19:57 -04005035 sksec->sclass = SECCLASS_SOCKET;
Paul Moore84914b72010-04-22 14:46:18 -04005036 selinux_netlbl_sk_security_reset(sksec);
5037 sk->sk_security = sksec;
5038
5039 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040}
5041
5042static void selinux_sk_free_security(struct sock *sk)
5043{
Paul Moore84914b72010-04-22 14:46:18 -04005044 struct sk_security_struct *sksec = sk->sk_security;
5045
5046 sk->sk_security = NULL;
5047 selinux_netlbl_sk_security_free(sksec);
5048 kfree(sksec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049}
5050
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005051static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
5052{
Eric Parisdd3e7832010-04-07 15:08:46 -04005053 struct sk_security_struct *sksec = sk->sk_security;
5054 struct sk_security_struct *newsksec = newsk->sk_security;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005055
Eric Parisdd3e7832010-04-07 15:08:46 -04005056 newsksec->sid = sksec->sid;
5057 newsksec->peer_sid = sksec->peer_sid;
5058 newsksec->sclass = sksec->sclass;
Paul Moore99f59ed2006-08-29 17:53:48 -07005059
Eric Parisdd3e7832010-04-07 15:08:46 -04005060 selinux_netlbl_sk_security_reset(newsksec);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005061}
5062
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07005063static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005064{
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005065 if (!sk)
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07005066 *secid = SECINITSID_ANY_SOCKET;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005067 else {
5068 struct sk_security_struct *sksec = sk->sk_security;
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005069
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07005070 *secid = sksec->sid;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005071 }
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005072}
5073
Eric Paris828dfe12008-04-17 13:17:49 -04005074static void selinux_sock_graft(struct sock *sk, struct socket *parent)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005075{
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05005076 struct inode_security_struct *isec =
5077 inode_security_novalidate(SOCK_INODE(parent));
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005078 struct sk_security_struct *sksec = sk->sk_security;
5079
Paul Moore2873ead2014-07-28 10:42:48 -04005080 if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
5081 sk->sk_family == PF_UNIX)
David Woodhouse2148ccc2006-09-29 15:50:25 -07005082 isec->sid = sksec->sid;
Paul Moore220deb92008-01-29 08:38:23 -05005083 sksec->sclass = isec->sclass;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005084}
5085
Richard Hainesd4529302018-02-13 20:57:18 +00005086/* Called whenever SCTP receives an INIT chunk. This happens when an incoming
5087 * connect(2), sctp_connectx(3) or sctp_sendmsg(3) (with no association
5088 * already present).
5089 */
5090static int selinux_sctp_assoc_request(struct sctp_endpoint *ep,
5091 struct sk_buff *skb)
5092{
5093 struct sk_security_struct *sksec = ep->base.sk->sk_security;
5094 struct common_audit_data ad;
5095 struct lsm_network_audit net = {0,};
5096 u8 peerlbl_active;
5097 u32 peer_sid = SECINITSID_UNLABELED;
5098 u32 conn_sid;
5099 int err = 0;
5100
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005101 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005102 return 0;
5103
5104 peerlbl_active = selinux_peerlbl_enabled();
5105
5106 if (peerlbl_active) {
5107 /* This will return peer_sid = SECSID_NULL if there are
5108 * no peer labels, see security_net_peersid_resolve().
5109 */
5110 err = selinux_skb_peerlbl_sid(skb, ep->base.sk->sk_family,
5111 &peer_sid);
5112 if (err)
5113 return err;
5114
5115 if (peer_sid == SECSID_NULL)
5116 peer_sid = SECINITSID_UNLABELED;
5117 }
5118
5119 if (sksec->sctp_assoc_state == SCTP_ASSOC_UNSET) {
5120 sksec->sctp_assoc_state = SCTP_ASSOC_SET;
5121
5122 /* Here as first association on socket. As the peer SID
5123 * was allowed by peer recv (and the netif/node checks),
5124 * then it is approved by policy and used as the primary
5125 * peer SID for getpeercon(3).
5126 */
5127 sksec->peer_sid = peer_sid;
5128 } else if (sksec->peer_sid != peer_sid) {
5129 /* Other association peer SIDs are checked to enforce
5130 * consistency among the peer SIDs.
5131 */
5132 ad.type = LSM_AUDIT_DATA_NET;
5133 ad.u.net = &net;
5134 ad.u.net->sk = ep->base.sk;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005135 err = avc_has_perm(&selinux_state,
5136 sksec->peer_sid, peer_sid, sksec->sclass,
Richard Hainesd4529302018-02-13 20:57:18 +00005137 SCTP_SOCKET__ASSOCIATION, &ad);
5138 if (err)
5139 return err;
5140 }
5141
5142 /* Compute the MLS component for the connection and store
5143 * the information in ep. This will be used by SCTP TCP type
5144 * sockets and peeled off connections as they cause a new
5145 * socket to be generated. selinux_sctp_sk_clone() will then
5146 * plug this into the new socket.
5147 */
5148 err = selinux_conn_sid(sksec->sid, peer_sid, &conn_sid);
5149 if (err)
5150 return err;
5151
5152 ep->secid = conn_sid;
5153 ep->peer_secid = peer_sid;
5154
5155 /* Set any NetLabel labels including CIPSO/CALIPSO options. */
5156 return selinux_netlbl_sctp_assoc_request(ep, skb);
5157}
5158
5159/* Check if sctp IPv4/IPv6 addresses are valid for binding or connecting
5160 * based on their @optname.
5161 */
5162static int selinux_sctp_bind_connect(struct sock *sk, int optname,
5163 struct sockaddr *address,
5164 int addrlen)
5165{
5166 int len, err = 0, walk_size = 0;
5167 void *addr_buf;
5168 struct sockaddr *addr;
5169 struct socket *sock;
5170
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005171 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005172 return 0;
5173
5174 /* Process one or more addresses that may be IPv4 or IPv6 */
5175 sock = sk->sk_socket;
5176 addr_buf = address;
5177
5178 while (walk_size < addrlen) {
5179 addr = addr_buf;
5180 switch (addr->sa_family) {
Alexey Kodanev4152dc92018-05-11 20:15:13 +03005181 case AF_UNSPEC:
Richard Hainesd4529302018-02-13 20:57:18 +00005182 case AF_INET:
5183 len = sizeof(struct sockaddr_in);
5184 break;
5185 case AF_INET6:
5186 len = sizeof(struct sockaddr_in6);
5187 break;
5188 default:
Alexey Kodanev4152dc92018-05-11 20:15:13 +03005189 return -EINVAL;
Richard Hainesd4529302018-02-13 20:57:18 +00005190 }
5191
5192 err = -EINVAL;
5193 switch (optname) {
5194 /* Bind checks */
5195 case SCTP_PRIMARY_ADDR:
5196 case SCTP_SET_PEER_PRIMARY_ADDR:
5197 case SCTP_SOCKOPT_BINDX_ADD:
5198 err = selinux_socket_bind(sock, addr, len);
5199 break;
5200 /* Connect checks */
5201 case SCTP_SOCKOPT_CONNECTX:
5202 case SCTP_PARAM_SET_PRIMARY:
5203 case SCTP_PARAM_ADD_IP:
5204 case SCTP_SENDMSG_CONNECT:
5205 err = selinux_socket_connect_helper(sock, addr, len);
5206 if (err)
5207 return err;
5208
5209 /* As selinux_sctp_bind_connect() is called by the
5210 * SCTP protocol layer, the socket is already locked,
5211 * therefore selinux_netlbl_socket_connect_locked() is
5212 * is called here. The situations handled are:
5213 * sctp_connectx(3), sctp_sendmsg(3), sendmsg(2),
5214 * whenever a new IP address is added or when a new
5215 * primary address is selected.
5216 * Note that an SCTP connect(2) call happens before
5217 * the SCTP protocol layer and is handled via
5218 * selinux_socket_connect().
5219 */
5220 err = selinux_netlbl_socket_connect_locked(sk, addr);
5221 break;
5222 }
5223
5224 if (err)
5225 return err;
5226
5227 addr_buf += len;
5228 walk_size += len;
5229 }
5230
5231 return 0;
5232}
5233
5234/* Called whenever a new socket is created by accept(2) or sctp_peeloff(3). */
5235static void selinux_sctp_sk_clone(struct sctp_endpoint *ep, struct sock *sk,
5236 struct sock *newsk)
5237{
5238 struct sk_security_struct *sksec = sk->sk_security;
5239 struct sk_security_struct *newsksec = newsk->sk_security;
5240
5241 /* If policy does not support SECCLASS_SCTP_SOCKET then call
5242 * the non-sctp clone version.
5243 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005244 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005245 return selinux_sk_clone_security(sk, newsk);
5246
5247 newsksec->sid = ep->secid;
5248 newsksec->peer_sid = ep->peer_secid;
5249 newsksec->sclass = sksec->sclass;
5250 selinux_netlbl_sctp_sk_clone(sk, newsk);
5251}
5252
Adrian Bunk9a673e52006-08-15 00:03:53 -07005253static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
5254 struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005255{
5256 struct sk_security_struct *sksec = sk->sk_security;
5257 int err;
Paul Moore0b1f24e2013-12-03 11:39:13 -05005258 u16 family = req->rsk_ops->family;
Paul Moore446b8022013-12-04 16:10:51 -05005259 u32 connsid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005260 u32 peersid;
5261
Paul Mooreaa862902008-10-10 10:16:29 -04005262 err = selinux_skb_peerlbl_sid(skb, family, &peersid);
Paul Moore220deb92008-01-29 08:38:23 -05005263 if (err)
5264 return err;
Paul Moore446b8022013-12-04 16:10:51 -05005265 err = selinux_conn_sid(sksec->sid, peersid, &connsid);
5266 if (err)
5267 return err;
5268 req->secid = connsid;
5269 req->peer_secid = peersid;
Venkat Yekkiralaa51c64f2006-07-27 22:01:34 -07005270
Paul Moore389fb8002009-03-27 17:10:34 -04005271 return selinux_netlbl_inet_conn_request(req, family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005272}
5273
Adrian Bunk9a673e52006-08-15 00:03:53 -07005274static void selinux_inet_csk_clone(struct sock *newsk,
5275 const struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005276{
5277 struct sk_security_struct *newsksec = newsk->sk_security;
5278
5279 newsksec->sid = req->secid;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005280 newsksec->peer_sid = req->peer_secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005281 /* NOTE: Ideally, we should also get the isec->sid for the
5282 new socket in sync, but we don't have the isec available yet.
5283 So we will wait until sock_graft to do it, by which
5284 time it will have been created and available. */
Paul Moore99f59ed2006-08-29 17:53:48 -07005285
Paul Moore9f2ad662006-11-17 17:38:53 -05005286 /* We don't need to take any sort of lock here as we are the only
5287 * thread with access to newsksec */
Paul Moore389fb8002009-03-27 17:10:34 -04005288 selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005289}
5290
Paul Moore014ab192008-10-10 10:16:33 -04005291static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005292{
Paul Mooreaa862902008-10-10 10:16:29 -04005293 u16 family = sk->sk_family;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005294 struct sk_security_struct *sksec = sk->sk_security;
5295
Paul Mooreaa862902008-10-10 10:16:29 -04005296 /* handle mapped IPv4 packets arriving via IPv6 sockets */
5297 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
5298 family = PF_INET;
5299
5300 selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005301}
5302
Eric Paris2606fd12010-10-13 16:24:41 -04005303static int selinux_secmark_relabel_packet(u32 sid)
5304{
5305 const struct task_security_struct *__tsec;
5306 u32 tsid;
5307
5308 __tsec = current_security();
5309 tsid = __tsec->sid;
5310
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005311 return avc_has_perm(&selinux_state,
5312 tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO,
5313 NULL);
Eric Paris2606fd12010-10-13 16:24:41 -04005314}
5315
5316static void selinux_secmark_refcount_inc(void)
5317{
5318 atomic_inc(&selinux_secmark_refcount);
5319}
5320
5321static void selinux_secmark_refcount_dec(void)
5322{
5323 atomic_dec(&selinux_secmark_refcount);
5324}
5325
Adrian Bunk9a673e52006-08-15 00:03:53 -07005326static void selinux_req_classify_flow(const struct request_sock *req,
5327 struct flowi *fl)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005328{
David S. Miller1d28f422011-03-12 00:29:39 -05005329 fl->flowi_secid = req->secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005330}
5331
Paul Moore5dbbaf22013-01-14 07:12:19 +00005332static int selinux_tun_dev_alloc_security(void **security)
5333{
5334 struct tun_security_struct *tunsec;
5335
5336 tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL);
5337 if (!tunsec)
5338 return -ENOMEM;
5339 tunsec->sid = current_sid();
5340
5341 *security = tunsec;
5342 return 0;
5343}
5344
5345static void selinux_tun_dev_free_security(void *security)
5346{
5347 kfree(security);
5348}
5349
Paul Mooreed6d76e2009-08-28 18:12:49 -04005350static int selinux_tun_dev_create(void)
5351{
5352 u32 sid = current_sid();
5353
5354 /* we aren't taking into account the "sockcreate" SID since the socket
5355 * that is being created here is not a socket in the traditional sense,
5356 * instead it is a private sock, accessible only to the kernel, and
5357 * representing a wide range of network traffic spanning multiple
5358 * connections unlike traditional sockets - check the TUN driver to
5359 * get a better understanding of why this socket is special */
5360
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005361 return avc_has_perm(&selinux_state,
5362 sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005363 NULL);
5364}
5365
Paul Moore5dbbaf22013-01-14 07:12:19 +00005366static int selinux_tun_dev_attach_queue(void *security)
Paul Mooreed6d76e2009-08-28 18:12:49 -04005367{
Paul Moore5dbbaf22013-01-14 07:12:19 +00005368 struct tun_security_struct *tunsec = security;
5369
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005370 return avc_has_perm(&selinux_state,
5371 current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET,
Paul Moore5dbbaf22013-01-14 07:12:19 +00005372 TUN_SOCKET__ATTACH_QUEUE, NULL);
5373}
5374
5375static int selinux_tun_dev_attach(struct sock *sk, void *security)
5376{
5377 struct tun_security_struct *tunsec = security;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005378 struct sk_security_struct *sksec = sk->sk_security;
5379
5380 /* we don't currently perform any NetLabel based labeling here and it
5381 * isn't clear that we would want to do so anyway; while we could apply
5382 * labeling without the support of the TUN user the resulting labeled
5383 * traffic from the other end of the connection would almost certainly
5384 * cause confusion to the TUN user that had no idea network labeling
5385 * protocols were being used */
5386
Paul Moore5dbbaf22013-01-14 07:12:19 +00005387 sksec->sid = tunsec->sid;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005388 sksec->sclass = SECCLASS_TUN_SOCKET;
Paul Moore5dbbaf22013-01-14 07:12:19 +00005389
5390 return 0;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005391}
5392
Paul Moore5dbbaf22013-01-14 07:12:19 +00005393static int selinux_tun_dev_open(void *security)
Paul Mooreed6d76e2009-08-28 18:12:49 -04005394{
Paul Moore5dbbaf22013-01-14 07:12:19 +00005395 struct tun_security_struct *tunsec = security;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005396 u32 sid = current_sid();
5397 int err;
5398
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005399 err = avc_has_perm(&selinux_state,
5400 sid, tunsec->sid, SECCLASS_TUN_SOCKET,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005401 TUN_SOCKET__RELABELFROM, NULL);
5402 if (err)
5403 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005404 err = avc_has_perm(&selinux_state,
5405 sid, sid, SECCLASS_TUN_SOCKET,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005406 TUN_SOCKET__RELABELTO, NULL);
5407 if (err)
5408 return err;
Paul Moore5dbbaf22013-01-14 07:12:19 +00005409 tunsec->sid = sid;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005410
5411 return 0;
5412}
5413
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
5415{
5416 int err = 0;
5417 u32 perm;
5418 struct nlmsghdr *nlh;
Paul Moore253bfae2010-04-22 14:46:19 -04005419 struct sk_security_struct *sksec = sk->sk_security;
Eric Paris828dfe12008-04-17 13:17:49 -04005420
Hong zhi guo77954982013-03-27 06:49:35 +00005421 if (skb->len < NLMSG_HDRLEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422 err = -EINVAL;
5423 goto out;
5424 }
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -07005425 nlh = nlmsg_hdr(skb);
Eric Paris828dfe12008-04-17 13:17:49 -04005426
Paul Moore253bfae2010-04-22 14:46:19 -04005427 err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428 if (err) {
5429 if (err == -EINVAL) {
Vladis Dronov76319942015-12-24 11:09:41 -05005430 pr_warn_ratelimited("SELinux: unrecognized netlink"
5431 " message: protocol=%hu nlmsg_type=%hu sclass=%s"
5432 " pig=%d comm=%s\n",
Marek Milkoviccded3ff2015-06-04 16:22:16 -04005433 sk->sk_protocol, nlh->nlmsg_type,
Vladis Dronov76319942015-12-24 11:09:41 -05005434 secclass_map[sksec->sclass - 1].name,
5435 task_pid_nr(current), current->comm);
Paul Mooree5a5ca92018-03-01 17:38:30 -05005436 if (!enforcing_enabled(&selinux_state) ||
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005437 security_get_allow_unknown(&selinux_state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438 err = 0;
5439 }
5440
5441 /* Ignore */
5442 if (err == -ENOENT)
5443 err = 0;
5444 goto out;
5445 }
5446
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005447 err = sock_has_perm(sk, perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448out:
5449 return err;
5450}
5451
5452#ifdef CONFIG_NETFILTER
5453
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005454static unsigned int selinux_ip_forward(struct sk_buff *skb,
5455 const struct net_device *indev,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005456 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457{
Paul Mooredfaebe92008-10-10 10:16:31 -04005458 int err;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005459 char *addrp;
5460 u32 peer_sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04005461 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005462 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05005463 u8 secmark_active;
Paul Moore948bf852008-10-10 10:16:32 -04005464 u8 netlbl_active;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005465 u8 peerlbl_active;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005466
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005467 if (!selinux_policycap_netpeer())
Paul Mooreeffad8d2008-01-29 08:49:27 -05005468 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005469
Paul Mooreeffad8d2008-01-29 08:49:27 -05005470 secmark_active = selinux_secmark_enabled();
Paul Moore948bf852008-10-10 10:16:32 -04005471 netlbl_active = netlbl_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04005472 peerlbl_active = selinux_peerlbl_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05005473 if (!secmark_active && !peerlbl_active)
5474 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005475
Paul Moored8395c82008-10-10 10:16:30 -04005476 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
5477 return NF_DROP;
5478
Eric Paris50c205f2012-04-04 15:01:43 -04005479 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005480 ad.u.net = &net;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005481 ad.u.net->netif = indev->ifindex;
Eric Paris48c62af2012-04-02 13:15:44 -04005482 ad.u.net->family = family;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005483 if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
5484 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485
Paul Mooredfaebe92008-10-10 10:16:31 -04005486 if (peerlbl_active) {
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005487 err = selinux_inet_sys_rcv_skb(dev_net(indev), indev->ifindex,
5488 addrp, family, peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04005489 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04005490 selinux_netlbl_err(skb, family, err, 1);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005491 return NF_DROP;
Paul Mooredfaebe92008-10-10 10:16:31 -04005492 }
5493 }
Paul Mooreeffad8d2008-01-29 08:49:27 -05005494
5495 if (secmark_active)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005496 if (avc_has_perm(&selinux_state,
5497 peer_sid, skb->secmark,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005498 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
5499 return NF_DROP;
5500
Paul Moore948bf852008-10-10 10:16:32 -04005501 if (netlbl_active)
5502 /* we do this in the FORWARD path and not the POST_ROUTING
5503 * path because we want to make sure we apply the necessary
5504 * labeling before IPsec is applied so we can leverage AH
5505 * protection */
5506 if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
5507 return NF_DROP;
5508
Paul Mooreeffad8d2008-01-29 08:49:27 -05005509 return NF_ACCEPT;
5510}
5511
Eric W. Biederman06198b32015-09-18 14:33:06 -05005512static unsigned int selinux_ipv4_forward(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005513 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005514 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005515{
David S. Miller238e54c2015-04-03 20:32:56 -04005516 return selinux_ip_forward(skb, state->in, PF_INET);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005517}
5518
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005519#if IS_ENABLED(CONFIG_IPV6)
Eric W. Biederman06198b32015-09-18 14:33:06 -05005520static unsigned int selinux_ipv6_forward(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005521 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005522 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005523{
David S. Miller238e54c2015-04-03 20:32:56 -04005524 return selinux_ip_forward(skb, state->in, PF_INET6);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005525}
5526#endif /* IPV6 */
5527
Paul Moore948bf852008-10-10 10:16:32 -04005528static unsigned int selinux_ip_output(struct sk_buff *skb,
5529 u16 family)
5530{
Paul Moore47180062013-12-04 16:10:45 -05005531 struct sock *sk;
Paul Moore948bf852008-10-10 10:16:32 -04005532 u32 sid;
5533
5534 if (!netlbl_enabled())
5535 return NF_ACCEPT;
5536
5537 /* we do this in the LOCAL_OUT path and not the POST_ROUTING path
5538 * because we want to make sure we apply the necessary labeling
5539 * before IPsec is applied so we can leverage AH protection */
Paul Moore47180062013-12-04 16:10:45 -05005540 sk = skb->sk;
5541 if (sk) {
5542 struct sk_security_struct *sksec;
5543
Eric Dumazete446f9d2015-10-08 05:01:55 -07005544 if (sk_listener(sk))
Paul Moore47180062013-12-04 16:10:45 -05005545 /* if the socket is the listening state then this
5546 * packet is a SYN-ACK packet which means it needs to
5547 * be labeled based on the connection/request_sock and
5548 * not the parent socket. unfortunately, we can't
5549 * lookup the request_sock yet as it isn't queued on
5550 * the parent socket until after the SYN-ACK is sent.
5551 * the "solution" is to simply pass the packet as-is
5552 * as any IP option based labeling should be copied
5553 * from the initial connection request (in the IP
5554 * layer). it is far from ideal, but until we get a
5555 * security label in the packet itself this is the
5556 * best we can do. */
5557 return NF_ACCEPT;
5558
5559 /* standard practice, label using the parent socket */
5560 sksec = sk->sk_security;
Paul Moore948bf852008-10-10 10:16:32 -04005561 sid = sksec->sid;
5562 } else
5563 sid = SECINITSID_KERNEL;
5564 if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
5565 return NF_DROP;
5566
5567 return NF_ACCEPT;
5568}
5569
Eric W. Biederman06198b32015-09-18 14:33:06 -05005570static unsigned int selinux_ipv4_output(void *priv,
Paul Moore948bf852008-10-10 10:16:32 -04005571 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005572 const struct nf_hook_state *state)
Paul Moore948bf852008-10-10 10:16:32 -04005573{
5574 return selinux_ip_output(skb, PF_INET);
5575}
5576
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005577#if IS_ENABLED(CONFIG_IPV6)
Huw Davies2917f572016-06-27 15:06:15 -04005578static unsigned int selinux_ipv6_output(void *priv,
5579 struct sk_buff *skb,
5580 const struct nf_hook_state *state)
5581{
5582 return selinux_ip_output(skb, PF_INET6);
5583}
5584#endif /* IPV6 */
5585
Paul Mooreeffad8d2008-01-29 08:49:27 -05005586static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
5587 int ifindex,
Paul Moored8395c82008-10-10 10:16:30 -04005588 u16 family)
James Morris4e5ab4c2006-06-09 00:33:33 -07005589{
Eric Dumazet54abc682015-11-08 10:54:07 -08005590 struct sock *sk = skb_to_full_sk(skb);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005591 struct sk_security_struct *sksec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005592 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005593 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04005594 char *addrp;
5595 u8 proto;
James Morris4e5ab4c2006-06-09 00:33:33 -07005596
Paul Mooreeffad8d2008-01-29 08:49:27 -05005597 if (sk == NULL)
5598 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005599 sksec = sk->sk_security;
James Morris4e5ab4c2006-06-09 00:33:33 -07005600
Eric Paris50c205f2012-04-04 15:01:43 -04005601 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005602 ad.u.net = &net;
5603 ad.u.net->netif = ifindex;
5604 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04005605 if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
5606 return NF_DROP;
5607
Paul Moore58bfbb52009-03-27 17:10:41 -04005608 if (selinux_secmark_enabled())
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005609 if (avc_has_perm(&selinux_state,
5610 sksec->sid, skb->secmark,
Paul Moored8395c82008-10-10 10:16:30 -04005611 SECCLASS_PACKET, PACKET__SEND, &ad))
Eric Paris2fe66ec2010-11-23 06:28:08 +00005612 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07005613
Steffen Klassertb9679a72011-02-23 12:55:21 +01005614 if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
5615 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07005616
Paul Mooreeffad8d2008-01-29 08:49:27 -05005617 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618}
5619
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005620static unsigned int selinux_ip_postroute(struct sk_buff *skb,
5621 const struct net_device *outdev,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005622 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623{
Paul Mooreeffad8d2008-01-29 08:49:27 -05005624 u32 secmark_perm;
5625 u32 peer_sid;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005626 int ifindex = outdev->ifindex;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005627 struct sock *sk;
Thomas Liu2bf49692009-07-14 12:14:09 -04005628 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005629 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05005630 char *addrp;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005631 u8 secmark_active;
5632 u8 peerlbl_active;
5633
Paul Mooreeffad8d2008-01-29 08:49:27 -05005634 /* If any sort of compatibility mode is enabled then handoff processing
5635 * to the selinux_ip_postroute_compat() function to deal with the
5636 * special handling. We do this in an attempt to keep this function
5637 * as fast and as clean as possible. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005638 if (!selinux_policycap_netpeer())
Paul Moored8395c82008-10-10 10:16:30 -04005639 return selinux_ip_postroute_compat(skb, ifindex, family);
Paul Moorec0828e52013-12-10 14:58:01 -05005640
Paul Mooreeffad8d2008-01-29 08:49:27 -05005641 secmark_active = selinux_secmark_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04005642 peerlbl_active = selinux_peerlbl_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05005643 if (!secmark_active && !peerlbl_active)
5644 return NF_ACCEPT;
5645
Eric Dumazet54abc682015-11-08 10:54:07 -08005646 sk = skb_to_full_sk(skb);
Paul Moorec0828e52013-12-10 14:58:01 -05005647
Paul Mooreeffad8d2008-01-29 08:49:27 -05005648#ifdef CONFIG_XFRM
5649 /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
5650 * packet transformation so allow the packet to pass without any checks
5651 * since we'll have another chance to perform access control checks
5652 * when the packet is on it's final way out.
5653 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
Paul Moorec0828e52013-12-10 14:58:01 -05005654 * is NULL, in this case go ahead and apply access control.
5655 * NOTE: if this is a local socket (skb->sk != NULL) that is in the
5656 * TCP listening state we cannot wait until the XFRM processing
5657 * is done as we will miss out on the SA label if we do;
5658 * unfortunately, this means more work, but it is only once per
5659 * connection. */
5660 if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL &&
Eric Dumazete446f9d2015-10-08 05:01:55 -07005661 !(sk && sk_listener(sk)))
Paul Mooreeffad8d2008-01-29 08:49:27 -05005662 return NF_ACCEPT;
5663#endif
Paul Mooreeffad8d2008-01-29 08:49:27 -05005664
Paul Moored8395c82008-10-10 10:16:30 -04005665 if (sk == NULL) {
Paul Moore446b8022013-12-04 16:10:51 -05005666 /* Without an associated socket the packet is either coming
5667 * from the kernel or it is being forwarded; check the packet
5668 * to determine which and if the packet is being forwarded
5669 * query the packet directly to determine the security label. */
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005670 if (skb->skb_iif) {
5671 secmark_perm = PACKET__FORWARD_OUT;
Paul Moored8395c82008-10-10 10:16:30 -04005672 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005673 return NF_DROP;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005674 } else {
5675 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04005676 peer_sid = SECINITSID_KERNEL;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005677 }
Eric Dumazete446f9d2015-10-08 05:01:55 -07005678 } else if (sk_listener(sk)) {
Paul Moore446b8022013-12-04 16:10:51 -05005679 /* Locally generated packet but the associated socket is in the
5680 * listening state which means this is a SYN-ACK packet. In
5681 * this particular case the correct security label is assigned
5682 * to the connection/request_sock but unfortunately we can't
5683 * query the request_sock as it isn't queued on the parent
5684 * socket until after the SYN-ACK packet is sent; the only
5685 * viable choice is to regenerate the label like we do in
5686 * selinux_inet_conn_request(). See also selinux_ip_output()
5687 * for similar problems. */
5688 u32 skb_sid;
Eric Dumazete446f9d2015-10-08 05:01:55 -07005689 struct sk_security_struct *sksec;
5690
Eric Dumazete446f9d2015-10-08 05:01:55 -07005691 sksec = sk->sk_security;
Paul Moore446b8022013-12-04 16:10:51 -05005692 if (selinux_skb_peerlbl_sid(skb, family, &skb_sid))
5693 return NF_DROP;
Paul Moorec0828e52013-12-10 14:58:01 -05005694 /* At this point, if the returned skb peerlbl is SECSID_NULL
5695 * and the packet has been through at least one XFRM
5696 * transformation then we must be dealing with the "final"
5697 * form of labeled IPsec packet; since we've already applied
5698 * all of our access controls on this packet we can safely
5699 * pass the packet. */
5700 if (skb_sid == SECSID_NULL) {
5701 switch (family) {
5702 case PF_INET:
5703 if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
5704 return NF_ACCEPT;
5705 break;
5706 case PF_INET6:
5707 if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED)
5708 return NF_ACCEPT;
Paul Moorea7a91a12014-09-03 10:51:59 -04005709 break;
Paul Moorec0828e52013-12-10 14:58:01 -05005710 default:
5711 return NF_DROP_ERR(-ECONNREFUSED);
5712 }
5713 }
Paul Moore446b8022013-12-04 16:10:51 -05005714 if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid))
5715 return NF_DROP;
5716 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04005717 } else {
Paul Moore446b8022013-12-04 16:10:51 -05005718 /* Locally generated packet, fetch the security label from the
5719 * associated socket. */
Paul Mooreeffad8d2008-01-29 08:49:27 -05005720 struct sk_security_struct *sksec = sk->sk_security;
5721 peer_sid = sksec->sid;
5722 secmark_perm = PACKET__SEND;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005723 }
5724
Eric Paris50c205f2012-04-04 15:01:43 -04005725 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005726 ad.u.net = &net;
5727 ad.u.net->netif = ifindex;
5728 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04005729 if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
Eric Paris04f6d702010-11-23 06:28:02 +00005730 return NF_DROP;
Paul Moored8395c82008-10-10 10:16:30 -04005731
Paul Mooreeffad8d2008-01-29 08:49:27 -05005732 if (secmark_active)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005733 if (avc_has_perm(&selinux_state,
5734 peer_sid, skb->secmark,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005735 SECCLASS_PACKET, secmark_perm, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005736 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005737
5738 if (peerlbl_active) {
5739 u32 if_sid;
5740 u32 node_sid;
5741
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005742 if (sel_netif_sid(dev_net(outdev), ifindex, &if_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005743 return NF_DROP;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005744 if (avc_has_perm(&selinux_state,
5745 peer_sid, if_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005746 SECCLASS_NETIF, NETIF__EGRESS, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005747 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005748
5749 if (sel_netnode_sid(addrp, family, &node_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005750 return NF_DROP;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005751 if (avc_has_perm(&selinux_state,
5752 peer_sid, node_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005753 SECCLASS_NODE, NODE__SENDTO, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005754 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005755 }
5756
5757 return NF_ACCEPT;
5758}
5759
Eric W. Biederman06198b32015-09-18 14:33:06 -05005760static unsigned int selinux_ipv4_postroute(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005761 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005762 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005763{
David S. Miller238e54c2015-04-03 20:32:56 -04005764 return selinux_ip_postroute(skb, state->out, PF_INET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765}
5766
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005767#if IS_ENABLED(CONFIG_IPV6)
Eric W. Biederman06198b32015-09-18 14:33:06 -05005768static unsigned int selinux_ipv6_postroute(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005769 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005770 const struct nf_hook_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771{
David S. Miller238e54c2015-04-03 20:32:56 -04005772 return selinux_ip_postroute(skb, state->out, PF_INET6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774#endif /* IPV6 */
5775
5776#endif /* CONFIG_NETFILTER */
5777
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
5779{
Stephen Smalley941fc5b2009-10-01 14:48:23 -04005780 return selinux_nlmsg_perm(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005781}
5782
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005783static int ipc_alloc_security(struct kern_ipc_perm *perm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784 u16 sclass)
5785{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005786 struct ipc_security_struct *isec;
5787
James Morris89d155e2005-10-30 14:59:21 -08005788 isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005789 if (!isec)
5790 return -ENOMEM;
5791
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792 isec->sclass = sclass;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005793 isec->sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005794 perm->security = isec;
5795
5796 return 0;
5797}
5798
5799static void ipc_free_security(struct kern_ipc_perm *perm)
5800{
5801 struct ipc_security_struct *isec = perm->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802 perm->security = NULL;
5803 kfree(isec);
5804}
5805
5806static int msg_msg_alloc_security(struct msg_msg *msg)
5807{
5808 struct msg_security_struct *msec;
5809
James Morris89d155e2005-10-30 14:59:21 -08005810 msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811 if (!msec)
5812 return -ENOMEM;
5813
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814 msec->sid = SECINITSID_UNLABELED;
5815 msg->security = msec;
5816
5817 return 0;
5818}
5819
5820static void msg_msg_free_security(struct msg_msg *msg)
5821{
5822 struct msg_security_struct *msec = msg->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005823
5824 msg->security = NULL;
5825 kfree(msec);
5826}
5827
5828static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
Stephen Smalley6af963f2005-05-01 08:58:39 -07005829 u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005830{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005832 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005833 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005834
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835 isec = ipc_perms->security;
5836
Eric Paris50c205f2012-04-04 15:01:43 -04005837 ad.type = LSM_AUDIT_DATA_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838 ad.u.ipc_id = ipc_perms->key;
5839
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005840 return avc_has_perm(&selinux_state,
5841 sid, isec->sid, isec->sclass, perms, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842}
5843
5844static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
5845{
5846 return msg_msg_alloc_security(msg);
5847}
5848
5849static void selinux_msg_msg_free_security(struct msg_msg *msg)
5850{
5851 msg_msg_free_security(msg);
5852}
5853
5854/* message queue security operations */
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005855static int selinux_msg_queue_alloc_security(struct kern_ipc_perm *msq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005858 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005859 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860 int rc;
5861
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005862 rc = ipc_alloc_security(msq, SECCLASS_MSGQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863 if (rc)
5864 return rc;
5865
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005866 isec = msq->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867
Eric Paris50c205f2012-04-04 15:01:43 -04005868 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005869 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005871 rc = avc_has_perm(&selinux_state,
5872 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873 MSGQ__CREATE, &ad);
5874 if (rc) {
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005875 ipc_free_security(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876 return rc;
5877 }
5878 return 0;
5879}
5880
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005881static void selinux_msg_queue_free_security(struct kern_ipc_perm *msq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882{
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005883 ipc_free_security(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005884}
5885
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005886static int selinux_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005887{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005889 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005890 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005892 isec = msq->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005893
Eric Paris50c205f2012-04-04 15:01:43 -04005894 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005895 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005896
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005897 return avc_has_perm(&selinux_state,
5898 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899 MSGQ__ASSOCIATE, &ad);
5900}
5901
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005902static int selinux_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903{
5904 int err;
5905 int perms;
5906
Eric Paris828dfe12008-04-17 13:17:49 -04005907 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908 case IPC_INFO:
5909 case MSG_INFO:
5910 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005911 return avc_has_perm(&selinux_state,
5912 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005913 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914 case IPC_STAT:
5915 case MSG_STAT:
Davidlohr Bueso23c8cec2018-04-10 16:35:30 -07005916 case MSG_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005917 perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
5918 break;
5919 case IPC_SET:
5920 perms = MSGQ__SETATTR;
5921 break;
5922 case IPC_RMID:
5923 perms = MSGQ__DESTROY;
5924 break;
5925 default:
5926 return 0;
5927 }
5928
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005929 err = ipc_has_perm(msq, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005930 return err;
5931}
5932
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005933static int selinux_msg_queue_msgsnd(struct kern_ipc_perm *msq, struct msg_msg *msg, int msqflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005935 struct ipc_security_struct *isec;
5936 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005937 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005938 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939 int rc;
5940
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005941 isec = msq->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005942 msec = msg->security;
5943
5944 /*
5945 * First time through, need to assign label to the message
5946 */
5947 if (msec->sid == SECINITSID_UNLABELED) {
5948 /*
5949 * Compute new sid based on current process and
5950 * message queue this message will be stored in
5951 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005952 rc = security_transition_sid(&selinux_state, sid, isec->sid,
5953 SECCLASS_MSG, NULL, &msec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954 if (rc)
5955 return rc;
5956 }
5957
Eric Paris50c205f2012-04-04 15:01:43 -04005958 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005959 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005960
5961 /* Can this process write to the queue? */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005962 rc = avc_has_perm(&selinux_state,
5963 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005964 MSGQ__WRITE, &ad);
5965 if (!rc)
5966 /* Can this process send the message */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005967 rc = avc_has_perm(&selinux_state,
5968 sid, msec->sid, SECCLASS_MSG,
David Howells275bb412008-11-14 10:39:19 +11005969 MSG__SEND, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005970 if (!rc)
5971 /* Can the message be put in the queue? */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005972 rc = avc_has_perm(&selinux_state,
5973 msec->sid, isec->sid, SECCLASS_MSGQ,
David Howells275bb412008-11-14 10:39:19 +11005974 MSGQ__ENQUEUE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005975
5976 return rc;
5977}
5978
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005979static int selinux_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005980 struct task_struct *target,
5981 long type, int mode)
5982{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005983 struct ipc_security_struct *isec;
5984 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005985 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005986 u32 sid = task_sid(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987 int rc;
5988
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005989 isec = msq->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005990 msec = msg->security;
5991
Eric Paris50c205f2012-04-04 15:01:43 -04005992 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005993 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005994
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005995 rc = avc_has_perm(&selinux_state,
5996 sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997 SECCLASS_MSGQ, MSGQ__READ, &ad);
5998 if (!rc)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005999 rc = avc_has_perm(&selinux_state,
6000 sid, msec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006001 SECCLASS_MSG, MSG__RECEIVE, &ad);
6002 return rc;
6003}
6004
6005/* Shared Memory security operations */
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006006static int selinux_shm_alloc_security(struct kern_ipc_perm *shp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006007{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006008 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006009 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006010 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011 int rc;
6012
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006013 rc = ipc_alloc_security(shp, SECCLASS_SHM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014 if (rc)
6015 return rc;
6016
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006017 isec = shp->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018
Eric Paris50c205f2012-04-04 15:01:43 -04006019 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006020 ad.u.ipc_id = shp->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006021
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006022 rc = avc_has_perm(&selinux_state,
6023 sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006024 SHM__CREATE, &ad);
6025 if (rc) {
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006026 ipc_free_security(shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027 return rc;
6028 }
6029 return 0;
6030}
6031
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006032static void selinux_shm_free_security(struct kern_ipc_perm *shp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033{
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006034 ipc_free_security(shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006035}
6036
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006037static int selinux_shm_associate(struct kern_ipc_perm *shp, int shmflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006040 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006041 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006042
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006043 isec = shp->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044
Eric Paris50c205f2012-04-04 15:01:43 -04006045 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006046 ad.u.ipc_id = shp->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006048 return avc_has_perm(&selinux_state,
6049 sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050 SHM__ASSOCIATE, &ad);
6051}
6052
6053/* Note, at this point, shp is locked down */
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006054static int selinux_shm_shmctl(struct kern_ipc_perm *shp, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006055{
6056 int perms;
6057 int err;
6058
Eric Paris828dfe12008-04-17 13:17:49 -04006059 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006060 case IPC_INFO:
6061 case SHM_INFO:
6062 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006063 return avc_has_perm(&selinux_state,
6064 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006065 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006066 case IPC_STAT:
6067 case SHM_STAT:
Davidlohr Buesoc21a6972018-04-10 16:35:23 -07006068 case SHM_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069 perms = SHM__GETATTR | SHM__ASSOCIATE;
6070 break;
6071 case IPC_SET:
6072 perms = SHM__SETATTR;
6073 break;
6074 case SHM_LOCK:
6075 case SHM_UNLOCK:
6076 perms = SHM__LOCK;
6077 break;
6078 case IPC_RMID:
6079 perms = SHM__DESTROY;
6080 break;
6081 default:
6082 return 0;
6083 }
6084
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006085 err = ipc_has_perm(shp, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086 return err;
6087}
6088
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006089static int selinux_shm_shmat(struct kern_ipc_perm *shp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006090 char __user *shmaddr, int shmflg)
6091{
6092 u32 perms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093
6094 if (shmflg & SHM_RDONLY)
6095 perms = SHM__READ;
6096 else
6097 perms = SHM__READ | SHM__WRITE;
6098
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006099 return ipc_has_perm(shp, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006100}
6101
6102/* Semaphore security operations */
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006103static int selinux_sem_alloc_security(struct kern_ipc_perm *sma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006105 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006106 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006107 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006108 int rc;
6109
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006110 rc = ipc_alloc_security(sma, SECCLASS_SEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006111 if (rc)
6112 return rc;
6113
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006114 isec = sma->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006115
Eric Paris50c205f2012-04-04 15:01:43 -04006116 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006117 ad.u.ipc_id = sma->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006118
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006119 rc = avc_has_perm(&selinux_state,
6120 sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006121 SEM__CREATE, &ad);
6122 if (rc) {
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006123 ipc_free_security(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006124 return rc;
6125 }
6126 return 0;
6127}
6128
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006129static void selinux_sem_free_security(struct kern_ipc_perm *sma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006130{
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006131 ipc_free_security(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006132}
6133
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006134static int selinux_sem_associate(struct kern_ipc_perm *sma, int semflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006135{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006136 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006137 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006138 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006139
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006140 isec = sma->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006141
Eric Paris50c205f2012-04-04 15:01:43 -04006142 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006143 ad.u.ipc_id = sma->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006144
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006145 return avc_has_perm(&selinux_state,
6146 sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147 SEM__ASSOCIATE, &ad);
6148}
6149
6150/* Note, at this point, sma is locked down */
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006151static int selinux_sem_semctl(struct kern_ipc_perm *sma, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006152{
6153 int err;
6154 u32 perms;
6155
Eric Paris828dfe12008-04-17 13:17:49 -04006156 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006157 case IPC_INFO:
6158 case SEM_INFO:
6159 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006160 return avc_has_perm(&selinux_state,
6161 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006162 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006163 case GETPID:
6164 case GETNCNT:
6165 case GETZCNT:
6166 perms = SEM__GETATTR;
6167 break;
6168 case GETVAL:
6169 case GETALL:
6170 perms = SEM__READ;
6171 break;
6172 case SETVAL:
6173 case SETALL:
6174 perms = SEM__WRITE;
6175 break;
6176 case IPC_RMID:
6177 perms = SEM__DESTROY;
6178 break;
6179 case IPC_SET:
6180 perms = SEM__SETATTR;
6181 break;
6182 case IPC_STAT:
6183 case SEM_STAT:
Davidlohr Buesoa280d6d2018-04-10 16:35:26 -07006184 case SEM_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006185 perms = SEM__GETATTR | SEM__ASSOCIATE;
6186 break;
6187 default:
6188 return 0;
6189 }
6190
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006191 err = ipc_has_perm(sma, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006192 return err;
6193}
6194
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006195static int selinux_sem_semop(struct kern_ipc_perm *sma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006196 struct sembuf *sops, unsigned nsops, int alter)
6197{
6198 u32 perms;
6199
6200 if (alter)
6201 perms = SEM__READ | SEM__WRITE;
6202 else
6203 perms = SEM__READ;
6204
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006205 return ipc_has_perm(sma, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006206}
6207
6208static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
6209{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210 u32 av = 0;
6211
Linus Torvalds1da177e2005-04-16 15:20:36 -07006212 av = 0;
6213 if (flag & S_IRUGO)
6214 av |= IPC__UNIX_READ;
6215 if (flag & S_IWUGO)
6216 av |= IPC__UNIX_WRITE;
6217
6218 if (av == 0)
6219 return 0;
6220
Stephen Smalley6af963f2005-05-01 08:58:39 -07006221 return ipc_has_perm(ipcp, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006222}
6223
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02006224static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
6225{
6226 struct ipc_security_struct *isec = ipcp->security;
6227 *secid = isec->sid;
6228}
6229
Eric Paris828dfe12008-04-17 13:17:49 -04006230static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006231{
6232 if (inode)
6233 inode_doinit_with_dentry(inode, dentry);
6234}
6235
6236static int selinux_getprocattr(struct task_struct *p,
Al Viro04ff9702007-03-12 16:17:58 +00006237 char *name, char **value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006238{
David Howells275bb412008-11-14 10:39:19 +11006239 const struct task_security_struct *__tsec;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00006240 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006241 int error;
Al Viro04ff9702007-03-12 16:17:58 +00006242 unsigned len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243
David Howells275bb412008-11-14 10:39:19 +11006244 rcu_read_lock();
6245 __tsec = __task_cred(p)->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006247 if (current != p) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006248 error = avc_has_perm(&selinux_state,
6249 current_sid(), __tsec->sid,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006250 SECCLASS_PROCESS, PROCESS__GETATTR, NULL);
6251 if (error)
6252 goto bad;
6253 }
6254
Linus Torvalds1da177e2005-04-16 15:20:36 -07006255 if (!strcmp(name, "current"))
David Howells275bb412008-11-14 10:39:19 +11006256 sid = __tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006257 else if (!strcmp(name, "prev"))
David Howells275bb412008-11-14 10:39:19 +11006258 sid = __tsec->osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006259 else if (!strcmp(name, "exec"))
David Howells275bb412008-11-14 10:39:19 +11006260 sid = __tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006261 else if (!strcmp(name, "fscreate"))
David Howells275bb412008-11-14 10:39:19 +11006262 sid = __tsec->create_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006263 else if (!strcmp(name, "keycreate"))
David Howells275bb412008-11-14 10:39:19 +11006264 sid = __tsec->keycreate_sid;
Eric Paris42c3e032006-06-26 00:26:03 -07006265 else if (!strcmp(name, "sockcreate"))
David Howells275bb412008-11-14 10:39:19 +11006266 sid = __tsec->sockcreate_sid;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006267 else {
6268 error = -EINVAL;
6269 goto bad;
6270 }
David Howells275bb412008-11-14 10:39:19 +11006271 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006272
6273 if (!sid)
6274 return 0;
6275
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006276 error = security_sid_to_context(&selinux_state, sid, value, &len);
Al Viro04ff9702007-03-12 16:17:58 +00006277 if (error)
6278 return error;
6279 return len;
David Howells275bb412008-11-14 10:39:19 +11006280
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006281bad:
David Howells275bb412008-11-14 10:39:19 +11006282 rcu_read_unlock();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006283 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006284}
6285
Stephen Smalleyb21507e2017-01-09 10:07:31 -05006286static int selinux_setprocattr(const char *name, void *value, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006287{
6288 struct task_security_struct *tsec;
David Howellsd84f4f92008-11-14 10:39:23 +11006289 struct cred *new;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006290 u32 mysid = current_sid(), sid = 0, ptsid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006291 int error;
6292 char *str = value;
6293
Linus Torvalds1da177e2005-04-16 15:20:36 -07006294 /*
6295 * Basic control over ability to set these attributes at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006296 */
6297 if (!strcmp(name, "exec"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006298 error = avc_has_perm(&selinux_state,
6299 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006300 PROCESS__SETEXEC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006301 else if (!strcmp(name, "fscreate"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006302 error = avc_has_perm(&selinux_state,
6303 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006304 PROCESS__SETFSCREATE, NULL);
Michael LeMay4eb582c2006-06-26 00:24:57 -07006305 else if (!strcmp(name, "keycreate"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006306 error = avc_has_perm(&selinux_state,
6307 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006308 PROCESS__SETKEYCREATE, NULL);
Eric Paris42c3e032006-06-26 00:26:03 -07006309 else if (!strcmp(name, "sockcreate"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006310 error = avc_has_perm(&selinux_state,
6311 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006312 PROCESS__SETSOCKCREATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006313 else if (!strcmp(name, "current"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006314 error = avc_has_perm(&selinux_state,
6315 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006316 PROCESS__SETCURRENT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006317 else
6318 error = -EINVAL;
6319 if (error)
6320 return error;
6321
6322 /* Obtain a SID for the context, if one was specified. */
Stephen Smalleya050a572017-01-31 11:54:04 -05006323 if (size && str[0] && str[0] != '\n') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006324 if (str[size-1] == '\n') {
6325 str[size-1] = 0;
6326 size--;
6327 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006328 error = security_context_to_sid(&selinux_state, value, size,
6329 &sid, GFP_KERNEL);
Stephen Smalley12b29f32008-05-07 13:03:20 -04006330 if (error == -EINVAL && !strcmp(name, "fscreate")) {
Stephen Smalleydb590002017-04-20 11:31:30 -04006331 if (!has_cap_mac_admin(true)) {
Eric Parisd6ea83e2012-04-04 13:45:49 -04006332 struct audit_buffer *ab;
6333 size_t audit_size;
6334
6335 /* We strip a nul only if it is at the end, otherwise the
6336 * context contains a nul and we should audit that */
6337 if (str[size - 1] == '\0')
6338 audit_size = size - 1;
6339 else
6340 audit_size = size;
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04006341 ab = audit_log_start(audit_context(),
6342 GFP_ATOMIC,
6343 AUDIT_SELINUX_ERR);
Eric Parisd6ea83e2012-04-04 13:45:49 -04006344 audit_log_format(ab, "op=fscreate invalid_context=");
6345 audit_log_n_untrustedstring(ab, value, audit_size);
6346 audit_log_end(ab);
6347
Stephen Smalley12b29f32008-05-07 13:03:20 -04006348 return error;
Eric Parisd6ea83e2012-04-04 13:45:49 -04006349 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006350 error = security_context_to_sid_force(
6351 &selinux_state,
6352 value, size, &sid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04006353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006354 if (error)
6355 return error;
6356 }
6357
David Howellsd84f4f92008-11-14 10:39:23 +11006358 new = prepare_creds();
6359 if (!new)
6360 return -ENOMEM;
6361
Linus Torvalds1da177e2005-04-16 15:20:36 -07006362 /* Permission checking based on the specified context is
6363 performed during the actual operation (execve,
6364 open/mkdir/...), when we know the full context of the
David Howellsd84f4f92008-11-14 10:39:23 +11006365 operation. See selinux_bprm_set_creds for the execve
Linus Torvalds1da177e2005-04-16 15:20:36 -07006366 checks and may_create for the file creation checks. The
6367 operation will then fail if the context is not permitted. */
David Howellsd84f4f92008-11-14 10:39:23 +11006368 tsec = new->security;
6369 if (!strcmp(name, "exec")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006370 tsec->exec_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006371 } else if (!strcmp(name, "fscreate")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006372 tsec->create_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006373 } else if (!strcmp(name, "keycreate")) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006374 error = avc_has_perm(&selinux_state,
6375 mysid, sid, SECCLASS_KEY, KEY__CREATE,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006376 NULL);
Michael LeMay4eb582c2006-06-26 00:24:57 -07006377 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11006378 goto abort_change;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006379 tsec->keycreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006380 } else if (!strcmp(name, "sockcreate")) {
Eric Paris42c3e032006-06-26 00:26:03 -07006381 tsec->sockcreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006382 } else if (!strcmp(name, "current")) {
6383 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006384 if (sid == 0)
David Howellsd84f4f92008-11-14 10:39:23 +11006385 goto abort_change;
KaiGai Koheid9250de2008-08-28 16:35:57 +09006386
David Howellsd84f4f92008-11-14 10:39:23 +11006387 /* Only allow single threaded processes to change context */
6388 error = -EPERM;
Oleg Nesterov5bb459b2009-07-10 03:48:23 +02006389 if (!current_is_single_threaded()) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006390 error = security_bounded_transition(&selinux_state,
6391 tsec->sid, sid);
David Howellsd84f4f92008-11-14 10:39:23 +11006392 if (error)
6393 goto abort_change;
Eric Paris828dfe12008-04-17 13:17:49 -04006394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006395
6396 /* Check permissions for the transition. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006397 error = avc_has_perm(&selinux_state,
6398 tsec->sid, sid, SECCLASS_PROCESS,
Eric Paris828dfe12008-04-17 13:17:49 -04006399 PROCESS__DYNTRANSITION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006400 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11006401 goto abort_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006402
6403 /* Check for ptracing, and update the task SID if ok.
6404 Otherwise, leave SID unchanged and fail. */
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006405 ptsid = ptrace_parent_sid();
Paul Moore0c6181c2016-03-30 21:41:21 -04006406 if (ptsid != 0) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006407 error = avc_has_perm(&selinux_state,
6408 ptsid, sid, SECCLASS_PROCESS,
David Howellsd84f4f92008-11-14 10:39:23 +11006409 PROCESS__PTRACE, NULL);
6410 if (error)
6411 goto abort_change;
6412 }
6413
6414 tsec->sid = sid;
6415 } else {
6416 error = -EINVAL;
6417 goto abort_change;
6418 }
6419
6420 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006421 return size;
David Howellsd84f4f92008-11-14 10:39:23 +11006422
6423abort_change:
6424 abort_creds(new);
6425 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006426}
6427
David Quigley746df9b2013-05-22 12:50:35 -04006428static int selinux_ismaclabel(const char *name)
6429{
6430 return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0);
6431}
6432
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006433static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
6434{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006435 return security_sid_to_context(&selinux_state, secid,
6436 secdata, seclen);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006437}
6438
David Howells7bf570d2008-04-29 20:52:51 +01006439static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
David Howells63cb3442008-01-15 23:47:35 +00006440{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006441 return security_context_to_sid(&selinux_state, secdata, seclen,
6442 secid, GFP_KERNEL);
David Howells63cb3442008-01-15 23:47:35 +00006443}
6444
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006445static void selinux_release_secctx(char *secdata, u32 seclen)
6446{
Paul Moore088999e2007-08-01 11:12:58 -04006447 kfree(secdata);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006448}
6449
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006450static void selinux_inode_invalidate_secctx(struct inode *inode)
6451{
6452 struct inode_security_struct *isec = inode->i_security;
6453
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01006454 spin_lock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006455 isec->initialized = LABEL_INVALID;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01006456 spin_unlock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006457}
6458
David P. Quigley1ee65e32009-09-03 14:25:57 -04006459/*
6460 * called with inode->i_mutex locked
6461 */
6462static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
6463{
6464 return selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, ctx, ctxlen, 0);
6465}
6466
6467/*
6468 * called with inode->i_mutex locked
6469 */
6470static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
6471{
6472 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0);
6473}
6474
6475static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
6476{
6477 int len = 0;
6478 len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX,
6479 ctx, true);
6480 if (len < 0)
6481 return len;
6482 *ctxlen = len;
6483 return 0;
6484}
Michael LeMayd7200242006-06-22 14:47:17 -07006485#ifdef CONFIG_KEYS
6486
David Howellsd84f4f92008-11-14 10:39:23 +11006487static int selinux_key_alloc(struct key *k, const struct cred *cred,
David Howells7e047ef2006-06-26 00:24:50 -07006488 unsigned long flags)
Michael LeMayd7200242006-06-22 14:47:17 -07006489{
David Howellsd84f4f92008-11-14 10:39:23 +11006490 const struct task_security_struct *tsec;
Michael LeMayd7200242006-06-22 14:47:17 -07006491 struct key_security_struct *ksec;
6492
6493 ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
6494 if (!ksec)
6495 return -ENOMEM;
6496
David Howellsd84f4f92008-11-14 10:39:23 +11006497 tsec = cred->security;
6498 if (tsec->keycreate_sid)
6499 ksec->sid = tsec->keycreate_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006500 else
David Howellsd84f4f92008-11-14 10:39:23 +11006501 ksec->sid = tsec->sid;
Michael LeMayd7200242006-06-22 14:47:17 -07006502
David Howells275bb412008-11-14 10:39:19 +11006503 k->security = ksec;
Michael LeMayd7200242006-06-22 14:47:17 -07006504 return 0;
6505}
6506
6507static void selinux_key_free(struct key *k)
6508{
6509 struct key_security_struct *ksec = k->security;
6510
6511 k->security = NULL;
6512 kfree(ksec);
6513}
6514
6515static int selinux_key_permission(key_ref_t key_ref,
David Howellsd84f4f92008-11-14 10:39:23 +11006516 const struct cred *cred,
David Howellsf5895942014-03-14 17:44:49 +00006517 unsigned perm)
Michael LeMayd7200242006-06-22 14:47:17 -07006518{
6519 struct key *key;
Michael LeMayd7200242006-06-22 14:47:17 -07006520 struct key_security_struct *ksec;
David Howells275bb412008-11-14 10:39:19 +11006521 u32 sid;
Michael LeMayd7200242006-06-22 14:47:17 -07006522
6523 /* if no specific permissions are requested, we skip the
6524 permission check. No serious, additional covert channels
6525 appear to be created. */
6526 if (perm == 0)
6527 return 0;
6528
David Howellsd84f4f92008-11-14 10:39:23 +11006529 sid = cred_sid(cred);
David Howells275bb412008-11-14 10:39:19 +11006530
6531 key = key_ref_to_ptr(key_ref);
6532 ksec = key->security;
6533
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006534 return avc_has_perm(&selinux_state,
6535 sid, ksec->sid, SECCLASS_KEY, perm, NULL);
Michael LeMayd7200242006-06-22 14:47:17 -07006536}
6537
David Howells70a5bb72008-04-29 01:01:26 -07006538static int selinux_key_getsecurity(struct key *key, char **_buffer)
6539{
6540 struct key_security_struct *ksec = key->security;
6541 char *context = NULL;
6542 unsigned len;
6543 int rc;
6544
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006545 rc = security_sid_to_context(&selinux_state, ksec->sid,
6546 &context, &len);
David Howells70a5bb72008-04-29 01:01:26 -07006547 if (!rc)
6548 rc = len;
6549 *_buffer = context;
6550 return rc;
6551}
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006552#endif
David Howells70a5bb72008-04-29 01:01:26 -07006553
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006554#ifdef CONFIG_SECURITY_INFINIBAND
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006555static int selinux_ib_pkey_access(void *ib_sec, u64 subnet_prefix, u16 pkey_val)
6556{
6557 struct common_audit_data ad;
6558 int err;
6559 u32 sid = 0;
6560 struct ib_security_struct *sec = ib_sec;
6561 struct lsm_ibpkey_audit ibpkey;
6562
Daniel Jurgens409dcf32017-05-19 15:48:59 +03006563 err = sel_ib_pkey_sid(subnet_prefix, pkey_val, &sid);
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006564 if (err)
6565 return err;
6566
6567 ad.type = LSM_AUDIT_DATA_IBPKEY;
6568 ibpkey.subnet_prefix = subnet_prefix;
6569 ibpkey.pkey = pkey_val;
6570 ad.u.ibpkey = &ibpkey;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006571 return avc_has_perm(&selinux_state,
6572 sec->sid, sid,
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006573 SECCLASS_INFINIBAND_PKEY,
6574 INFINIBAND_PKEY__ACCESS, &ad);
6575}
6576
Daniel Jurgensab861df2017-05-19 15:48:58 +03006577static int selinux_ib_endport_manage_subnet(void *ib_sec, const char *dev_name,
6578 u8 port_num)
6579{
6580 struct common_audit_data ad;
6581 int err;
6582 u32 sid = 0;
6583 struct ib_security_struct *sec = ib_sec;
6584 struct lsm_ibendport_audit ibendport;
6585
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006586 err = security_ib_endport_sid(&selinux_state, dev_name, port_num,
6587 &sid);
Daniel Jurgensab861df2017-05-19 15:48:58 +03006588
6589 if (err)
6590 return err;
6591
6592 ad.type = LSM_AUDIT_DATA_IBENDPORT;
6593 strncpy(ibendport.dev_name, dev_name, sizeof(ibendport.dev_name));
6594 ibendport.port = port_num;
6595 ad.u.ibendport = &ibendport;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006596 return avc_has_perm(&selinux_state,
6597 sec->sid, sid,
Daniel Jurgensab861df2017-05-19 15:48:58 +03006598 SECCLASS_INFINIBAND_ENDPORT,
6599 INFINIBAND_ENDPORT__MANAGE_SUBNET, &ad);
6600}
6601
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006602static int selinux_ib_alloc_security(void **ib_sec)
6603{
6604 struct ib_security_struct *sec;
6605
6606 sec = kzalloc(sizeof(*sec), GFP_KERNEL);
6607 if (!sec)
6608 return -ENOMEM;
6609 sec->sid = current_sid();
6610
6611 *ib_sec = sec;
6612 return 0;
6613}
6614
6615static void selinux_ib_free_security(void *ib_sec)
6616{
6617 kfree(ib_sec);
6618}
Michael LeMayd7200242006-06-22 14:47:17 -07006619#endif
6620
Chenbo Fengec27c352017-10-18 13:00:25 -07006621#ifdef CONFIG_BPF_SYSCALL
6622static int selinux_bpf(int cmd, union bpf_attr *attr,
6623 unsigned int size)
6624{
6625 u32 sid = current_sid();
6626 int ret;
6627
6628 switch (cmd) {
6629 case BPF_MAP_CREATE:
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006630 ret = avc_has_perm(&selinux_state,
6631 sid, sid, SECCLASS_BPF, BPF__MAP_CREATE,
Chenbo Fengec27c352017-10-18 13:00:25 -07006632 NULL);
6633 break;
6634 case BPF_PROG_LOAD:
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006635 ret = avc_has_perm(&selinux_state,
6636 sid, sid, SECCLASS_BPF, BPF__PROG_LOAD,
Chenbo Fengec27c352017-10-18 13:00:25 -07006637 NULL);
6638 break;
6639 default:
6640 ret = 0;
6641 break;
6642 }
6643
6644 return ret;
6645}
6646
6647static u32 bpf_map_fmode_to_av(fmode_t fmode)
6648{
6649 u32 av = 0;
6650
6651 if (fmode & FMODE_READ)
6652 av |= BPF__MAP_READ;
6653 if (fmode & FMODE_WRITE)
6654 av |= BPF__MAP_WRITE;
6655 return av;
6656}
6657
Chenbo Fengf66e4482017-10-18 13:00:26 -07006658/* This function will check the file pass through unix socket or binder to see
6659 * if it is a bpf related object. And apply correspinding checks on the bpf
6660 * object based on the type. The bpf maps and programs, not like other files and
6661 * socket, are using a shared anonymous inode inside the kernel as their inode.
6662 * So checking that inode cannot identify if the process have privilege to
6663 * access the bpf object and that's why we have to add this additional check in
6664 * selinux_file_receive and selinux_binder_transfer_files.
6665 */
6666static int bpf_fd_pass(struct file *file, u32 sid)
6667{
6668 struct bpf_security_struct *bpfsec;
6669 struct bpf_prog *prog;
6670 struct bpf_map *map;
6671 int ret;
6672
6673 if (file->f_op == &bpf_map_fops) {
6674 map = file->private_data;
6675 bpfsec = map->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006676 ret = avc_has_perm(&selinux_state,
6677 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengf66e4482017-10-18 13:00:26 -07006678 bpf_map_fmode_to_av(file->f_mode), NULL);
6679 if (ret)
6680 return ret;
6681 } else if (file->f_op == &bpf_prog_fops) {
6682 prog = file->private_data;
6683 bpfsec = prog->aux->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006684 ret = avc_has_perm(&selinux_state,
6685 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengf66e4482017-10-18 13:00:26 -07006686 BPF__PROG_RUN, NULL);
6687 if (ret)
6688 return ret;
6689 }
6690 return 0;
6691}
6692
Chenbo Fengec27c352017-10-18 13:00:25 -07006693static int selinux_bpf_map(struct bpf_map *map, fmode_t fmode)
6694{
6695 u32 sid = current_sid();
6696 struct bpf_security_struct *bpfsec;
6697
6698 bpfsec = map->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006699 return avc_has_perm(&selinux_state,
6700 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengec27c352017-10-18 13:00:25 -07006701 bpf_map_fmode_to_av(fmode), NULL);
6702}
6703
6704static int selinux_bpf_prog(struct bpf_prog *prog)
6705{
6706 u32 sid = current_sid();
6707 struct bpf_security_struct *bpfsec;
6708
6709 bpfsec = prog->aux->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006710 return avc_has_perm(&selinux_state,
6711 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengec27c352017-10-18 13:00:25 -07006712 BPF__PROG_RUN, NULL);
6713}
6714
6715static int selinux_bpf_map_alloc(struct bpf_map *map)
6716{
6717 struct bpf_security_struct *bpfsec;
6718
6719 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6720 if (!bpfsec)
6721 return -ENOMEM;
6722
6723 bpfsec->sid = current_sid();
6724 map->security = bpfsec;
6725
6726 return 0;
6727}
6728
6729static void selinux_bpf_map_free(struct bpf_map *map)
6730{
6731 struct bpf_security_struct *bpfsec = map->security;
6732
6733 map->security = NULL;
6734 kfree(bpfsec);
6735}
6736
6737static int selinux_bpf_prog_alloc(struct bpf_prog_aux *aux)
6738{
6739 struct bpf_security_struct *bpfsec;
6740
6741 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6742 if (!bpfsec)
6743 return -ENOMEM;
6744
6745 bpfsec->sid = current_sid();
6746 aux->security = bpfsec;
6747
6748 return 0;
6749}
6750
6751static void selinux_bpf_prog_free(struct bpf_prog_aux *aux)
6752{
6753 struct bpf_security_struct *bpfsec = aux->security;
6754
6755 aux->security = NULL;
6756 kfree(bpfsec);
6757}
6758#endif
6759
James Morrisca97d932017-02-15 00:18:51 +11006760static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {
Casey Schauflere20b0432015-05-02 15:11:36 -07006761 LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr),
6762 LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction),
6763 LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder),
6764 LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file),
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02006765
Casey Schauflere20b0432015-05-02 15:11:36 -07006766 LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check),
6767 LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme),
6768 LSM_HOOK_INIT(capget, selinux_capget),
6769 LSM_HOOK_INIT(capset, selinux_capset),
6770 LSM_HOOK_INIT(capable, selinux_capable),
6771 LSM_HOOK_INIT(quotactl, selinux_quotactl),
6772 LSM_HOOK_INIT(quota_on, selinux_quota_on),
6773 LSM_HOOK_INIT(syslog, selinux_syslog),
6774 LSM_HOOK_INIT(vm_enough_memory, selinux_vm_enough_memory),
Stephen Smalley79af7302015-01-21 10:54:10 -05006775
Casey Schauflere20b0432015-05-02 15:11:36 -07006776 LSM_HOOK_INIT(netlink_send, selinux_netlink_send),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006777
Casey Schauflere20b0432015-05-02 15:11:36 -07006778 LSM_HOOK_INIT(bprm_set_creds, selinux_bprm_set_creds),
6779 LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds),
6780 LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006781
Casey Schauflere20b0432015-05-02 15:11:36 -07006782 LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security),
6783 LSM_HOOK_INIT(sb_free_security, selinux_sb_free_security),
Al Viro5b400232018-12-12 20:13:29 -05006784 LSM_HOOK_INIT(sb_eat_lsm_opts, selinux_sb_eat_lsm_opts),
Casey Schauflere20b0432015-05-02 15:11:36 -07006785 LSM_HOOK_INIT(sb_remount, selinux_sb_remount),
6786 LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount),
6787 LSM_HOOK_INIT(sb_show_options, selinux_sb_show_options),
6788 LSM_HOOK_INIT(sb_statfs, selinux_sb_statfs),
6789 LSM_HOOK_INIT(sb_mount, selinux_mount),
6790 LSM_HOOK_INIT(sb_umount, selinux_umount),
6791 LSM_HOOK_INIT(sb_set_mnt_opts, selinux_set_mnt_opts),
6792 LSM_HOOK_INIT(sb_clone_mnt_opts, selinux_sb_clone_mnt_opts),
6793 LSM_HOOK_INIT(sb_parse_opts_str, selinux_parse_opts_str),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006794
Casey Schauflere20b0432015-05-02 15:11:36 -07006795 LSM_HOOK_INIT(dentry_init_security, selinux_dentry_init_security),
Vivek Goyala518b0a2016-07-13 10:44:53 -04006796 LSM_HOOK_INIT(dentry_create_files_as, selinux_dentry_create_files_as),
Eric Parise0007522008-03-05 10:31:54 -05006797
Casey Schauflere20b0432015-05-02 15:11:36 -07006798 LSM_HOOK_INIT(inode_alloc_security, selinux_inode_alloc_security),
6799 LSM_HOOK_INIT(inode_free_security, selinux_inode_free_security),
6800 LSM_HOOK_INIT(inode_init_security, selinux_inode_init_security),
6801 LSM_HOOK_INIT(inode_create, selinux_inode_create),
6802 LSM_HOOK_INIT(inode_link, selinux_inode_link),
6803 LSM_HOOK_INIT(inode_unlink, selinux_inode_unlink),
6804 LSM_HOOK_INIT(inode_symlink, selinux_inode_symlink),
6805 LSM_HOOK_INIT(inode_mkdir, selinux_inode_mkdir),
6806 LSM_HOOK_INIT(inode_rmdir, selinux_inode_rmdir),
6807 LSM_HOOK_INIT(inode_mknod, selinux_inode_mknod),
6808 LSM_HOOK_INIT(inode_rename, selinux_inode_rename),
6809 LSM_HOOK_INIT(inode_readlink, selinux_inode_readlink),
6810 LSM_HOOK_INIT(inode_follow_link, selinux_inode_follow_link),
6811 LSM_HOOK_INIT(inode_permission, selinux_inode_permission),
6812 LSM_HOOK_INIT(inode_setattr, selinux_inode_setattr),
6813 LSM_HOOK_INIT(inode_getattr, selinux_inode_getattr),
6814 LSM_HOOK_INIT(inode_setxattr, selinux_inode_setxattr),
6815 LSM_HOOK_INIT(inode_post_setxattr, selinux_inode_post_setxattr),
6816 LSM_HOOK_INIT(inode_getxattr, selinux_inode_getxattr),
6817 LSM_HOOK_INIT(inode_listxattr, selinux_inode_listxattr),
6818 LSM_HOOK_INIT(inode_removexattr, selinux_inode_removexattr),
6819 LSM_HOOK_INIT(inode_getsecurity, selinux_inode_getsecurity),
6820 LSM_HOOK_INIT(inode_setsecurity, selinux_inode_setsecurity),
6821 LSM_HOOK_INIT(inode_listsecurity, selinux_inode_listsecurity),
6822 LSM_HOOK_INIT(inode_getsecid, selinux_inode_getsecid),
Vivek Goyal56909eb2016-07-13 10:44:48 -04006823 LSM_HOOK_INIT(inode_copy_up, selinux_inode_copy_up),
Vivek Goyal19472b62016-07-13 10:44:50 -04006824 LSM_HOOK_INIT(inode_copy_up_xattr, selinux_inode_copy_up_xattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006825
Casey Schauflere20b0432015-05-02 15:11:36 -07006826 LSM_HOOK_INIT(file_permission, selinux_file_permission),
6827 LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security),
6828 LSM_HOOK_INIT(file_free_security, selinux_file_free_security),
6829 LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl),
6830 LSM_HOOK_INIT(mmap_file, selinux_mmap_file),
6831 LSM_HOOK_INIT(mmap_addr, selinux_mmap_addr),
6832 LSM_HOOK_INIT(file_mprotect, selinux_file_mprotect),
6833 LSM_HOOK_INIT(file_lock, selinux_file_lock),
6834 LSM_HOOK_INIT(file_fcntl, selinux_file_fcntl),
6835 LSM_HOOK_INIT(file_set_fowner, selinux_file_set_fowner),
6836 LSM_HOOK_INIT(file_send_sigiotask, selinux_file_send_sigiotask),
6837 LSM_HOOK_INIT(file_receive, selinux_file_receive),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006838
Casey Schauflere20b0432015-05-02 15:11:36 -07006839 LSM_HOOK_INIT(file_open, selinux_file_open),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006840
Tetsuo Handaa79be232017-03-28 23:08:45 +09006841 LSM_HOOK_INIT(task_alloc, selinux_task_alloc),
Casey Schauflere20b0432015-05-02 15:11:36 -07006842 LSM_HOOK_INIT(cred_alloc_blank, selinux_cred_alloc_blank),
6843 LSM_HOOK_INIT(cred_free, selinux_cred_free),
6844 LSM_HOOK_INIT(cred_prepare, selinux_cred_prepare),
6845 LSM_HOOK_INIT(cred_transfer, selinux_cred_transfer),
Matthew Garrett3ec30112018-01-08 13:36:19 -08006846 LSM_HOOK_INIT(cred_getsecid, selinux_cred_getsecid),
Casey Schauflere20b0432015-05-02 15:11:36 -07006847 LSM_HOOK_INIT(kernel_act_as, selinux_kernel_act_as),
6848 LSM_HOOK_INIT(kernel_create_files_as, selinux_kernel_create_files_as),
6849 LSM_HOOK_INIT(kernel_module_request, selinux_kernel_module_request),
Mimi Zoharc77b8cd2018-07-13 14:06:02 -04006850 LSM_HOOK_INIT(kernel_load_data, selinux_kernel_load_data),
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07006851 LSM_HOOK_INIT(kernel_read_file, selinux_kernel_read_file),
Casey Schauflere20b0432015-05-02 15:11:36 -07006852 LSM_HOOK_INIT(task_setpgid, selinux_task_setpgid),
6853 LSM_HOOK_INIT(task_getpgid, selinux_task_getpgid),
6854 LSM_HOOK_INIT(task_getsid, selinux_task_getsid),
6855 LSM_HOOK_INIT(task_getsecid, selinux_task_getsecid),
6856 LSM_HOOK_INIT(task_setnice, selinux_task_setnice),
6857 LSM_HOOK_INIT(task_setioprio, selinux_task_setioprio),
6858 LSM_HOOK_INIT(task_getioprio, selinux_task_getioprio),
Stephen Smalley791ec492017-02-17 07:57:00 -05006859 LSM_HOOK_INIT(task_prlimit, selinux_task_prlimit),
Casey Schauflere20b0432015-05-02 15:11:36 -07006860 LSM_HOOK_INIT(task_setrlimit, selinux_task_setrlimit),
6861 LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler),
6862 LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler),
6863 LSM_HOOK_INIT(task_movememory, selinux_task_movememory),
6864 LSM_HOOK_INIT(task_kill, selinux_task_kill),
Casey Schauflere20b0432015-05-02 15:11:36 -07006865 LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode),
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09006866
Casey Schauflere20b0432015-05-02 15:11:36 -07006867 LSM_HOOK_INIT(ipc_permission, selinux_ipc_permission),
6868 LSM_HOOK_INIT(ipc_getsecid, selinux_ipc_getsecid),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006869
Casey Schauflere20b0432015-05-02 15:11:36 -07006870 LSM_HOOK_INIT(msg_msg_alloc_security, selinux_msg_msg_alloc_security),
6871 LSM_HOOK_INIT(msg_msg_free_security, selinux_msg_msg_free_security),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006872
Casey Schauflere20b0432015-05-02 15:11:36 -07006873 LSM_HOOK_INIT(msg_queue_alloc_security,
6874 selinux_msg_queue_alloc_security),
6875 LSM_HOOK_INIT(msg_queue_free_security, selinux_msg_queue_free_security),
6876 LSM_HOOK_INIT(msg_queue_associate, selinux_msg_queue_associate),
6877 LSM_HOOK_INIT(msg_queue_msgctl, selinux_msg_queue_msgctl),
6878 LSM_HOOK_INIT(msg_queue_msgsnd, selinux_msg_queue_msgsnd),
6879 LSM_HOOK_INIT(msg_queue_msgrcv, selinux_msg_queue_msgrcv),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006880
Casey Schauflere20b0432015-05-02 15:11:36 -07006881 LSM_HOOK_INIT(shm_alloc_security, selinux_shm_alloc_security),
6882 LSM_HOOK_INIT(shm_free_security, selinux_shm_free_security),
6883 LSM_HOOK_INIT(shm_associate, selinux_shm_associate),
6884 LSM_HOOK_INIT(shm_shmctl, selinux_shm_shmctl),
6885 LSM_HOOK_INIT(shm_shmat, selinux_shm_shmat),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006886
Casey Schauflere20b0432015-05-02 15:11:36 -07006887 LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security),
6888 LSM_HOOK_INIT(sem_free_security, selinux_sem_free_security),
6889 LSM_HOOK_INIT(sem_associate, selinux_sem_associate),
6890 LSM_HOOK_INIT(sem_semctl, selinux_sem_semctl),
6891 LSM_HOOK_INIT(sem_semop, selinux_sem_semop),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006892
Casey Schauflere20b0432015-05-02 15:11:36 -07006893 LSM_HOOK_INIT(d_instantiate, selinux_d_instantiate),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006894
Casey Schauflere20b0432015-05-02 15:11:36 -07006895 LSM_HOOK_INIT(getprocattr, selinux_getprocattr),
6896 LSM_HOOK_INIT(setprocattr, selinux_setprocattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006897
Casey Schauflere20b0432015-05-02 15:11:36 -07006898 LSM_HOOK_INIT(ismaclabel, selinux_ismaclabel),
6899 LSM_HOOK_INIT(secid_to_secctx, selinux_secid_to_secctx),
6900 LSM_HOOK_INIT(secctx_to_secid, selinux_secctx_to_secid),
6901 LSM_HOOK_INIT(release_secctx, selinux_release_secctx),
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006902 LSM_HOOK_INIT(inode_invalidate_secctx, selinux_inode_invalidate_secctx),
Casey Schauflere20b0432015-05-02 15:11:36 -07006903 LSM_HOOK_INIT(inode_notifysecctx, selinux_inode_notifysecctx),
6904 LSM_HOOK_INIT(inode_setsecctx, selinux_inode_setsecctx),
6905 LSM_HOOK_INIT(inode_getsecctx, selinux_inode_getsecctx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006906
Casey Schauflere20b0432015-05-02 15:11:36 -07006907 LSM_HOOK_INIT(unix_stream_connect, selinux_socket_unix_stream_connect),
6908 LSM_HOOK_INIT(unix_may_send, selinux_socket_unix_may_send),
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006909
Casey Schauflere20b0432015-05-02 15:11:36 -07006910 LSM_HOOK_INIT(socket_create, selinux_socket_create),
6911 LSM_HOOK_INIT(socket_post_create, selinux_socket_post_create),
David Herrmann0b811db2018-05-04 16:28:21 +02006912 LSM_HOOK_INIT(socket_socketpair, selinux_socket_socketpair),
Casey Schauflere20b0432015-05-02 15:11:36 -07006913 LSM_HOOK_INIT(socket_bind, selinux_socket_bind),
6914 LSM_HOOK_INIT(socket_connect, selinux_socket_connect),
6915 LSM_HOOK_INIT(socket_listen, selinux_socket_listen),
6916 LSM_HOOK_INIT(socket_accept, selinux_socket_accept),
6917 LSM_HOOK_INIT(socket_sendmsg, selinux_socket_sendmsg),
6918 LSM_HOOK_INIT(socket_recvmsg, selinux_socket_recvmsg),
6919 LSM_HOOK_INIT(socket_getsockname, selinux_socket_getsockname),
6920 LSM_HOOK_INIT(socket_getpeername, selinux_socket_getpeername),
6921 LSM_HOOK_INIT(socket_getsockopt, selinux_socket_getsockopt),
6922 LSM_HOOK_INIT(socket_setsockopt, selinux_socket_setsockopt),
6923 LSM_HOOK_INIT(socket_shutdown, selinux_socket_shutdown),
6924 LSM_HOOK_INIT(socket_sock_rcv_skb, selinux_socket_sock_rcv_skb),
6925 LSM_HOOK_INIT(socket_getpeersec_stream,
6926 selinux_socket_getpeersec_stream),
6927 LSM_HOOK_INIT(socket_getpeersec_dgram, selinux_socket_getpeersec_dgram),
6928 LSM_HOOK_INIT(sk_alloc_security, selinux_sk_alloc_security),
6929 LSM_HOOK_INIT(sk_free_security, selinux_sk_free_security),
6930 LSM_HOOK_INIT(sk_clone_security, selinux_sk_clone_security),
6931 LSM_HOOK_INIT(sk_getsecid, selinux_sk_getsecid),
6932 LSM_HOOK_INIT(sock_graft, selinux_sock_graft),
Richard Hainesd4529302018-02-13 20:57:18 +00006933 LSM_HOOK_INIT(sctp_assoc_request, selinux_sctp_assoc_request),
6934 LSM_HOOK_INIT(sctp_sk_clone, selinux_sctp_sk_clone),
6935 LSM_HOOK_INIT(sctp_bind_connect, selinux_sctp_bind_connect),
Casey Schauflere20b0432015-05-02 15:11:36 -07006936 LSM_HOOK_INIT(inet_conn_request, selinux_inet_conn_request),
6937 LSM_HOOK_INIT(inet_csk_clone, selinux_inet_csk_clone),
6938 LSM_HOOK_INIT(inet_conn_established, selinux_inet_conn_established),
6939 LSM_HOOK_INIT(secmark_relabel_packet, selinux_secmark_relabel_packet),
6940 LSM_HOOK_INIT(secmark_refcount_inc, selinux_secmark_refcount_inc),
6941 LSM_HOOK_INIT(secmark_refcount_dec, selinux_secmark_refcount_dec),
6942 LSM_HOOK_INIT(req_classify_flow, selinux_req_classify_flow),
6943 LSM_HOOK_INIT(tun_dev_alloc_security, selinux_tun_dev_alloc_security),
6944 LSM_HOOK_INIT(tun_dev_free_security, selinux_tun_dev_free_security),
6945 LSM_HOOK_INIT(tun_dev_create, selinux_tun_dev_create),
6946 LSM_HOOK_INIT(tun_dev_attach_queue, selinux_tun_dev_attach_queue),
6947 LSM_HOOK_INIT(tun_dev_attach, selinux_tun_dev_attach),
6948 LSM_HOOK_INIT(tun_dev_open, selinux_tun_dev_open),
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006949#ifdef CONFIG_SECURITY_INFINIBAND
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006950 LSM_HOOK_INIT(ib_pkey_access, selinux_ib_pkey_access),
Daniel Jurgensab861df2017-05-19 15:48:58 +03006951 LSM_HOOK_INIT(ib_endport_manage_subnet,
6952 selinux_ib_endport_manage_subnet),
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006953 LSM_HOOK_INIT(ib_alloc_security, selinux_ib_alloc_security),
6954 LSM_HOOK_INIT(ib_free_security, selinux_ib_free_security),
6955#endif
Trent Jaegerd28d1e02005-12-13 23:12:40 -08006956#ifdef CONFIG_SECURITY_NETWORK_XFRM
Casey Schauflere20b0432015-05-02 15:11:36 -07006957 LSM_HOOK_INIT(xfrm_policy_alloc_security, selinux_xfrm_policy_alloc),
6958 LSM_HOOK_INIT(xfrm_policy_clone_security, selinux_xfrm_policy_clone),
6959 LSM_HOOK_INIT(xfrm_policy_free_security, selinux_xfrm_policy_free),
6960 LSM_HOOK_INIT(xfrm_policy_delete_security, selinux_xfrm_policy_delete),
6961 LSM_HOOK_INIT(xfrm_state_alloc, selinux_xfrm_state_alloc),
6962 LSM_HOOK_INIT(xfrm_state_alloc_acquire,
6963 selinux_xfrm_state_alloc_acquire),
6964 LSM_HOOK_INIT(xfrm_state_free_security, selinux_xfrm_state_free),
6965 LSM_HOOK_INIT(xfrm_state_delete_security, selinux_xfrm_state_delete),
6966 LSM_HOOK_INIT(xfrm_policy_lookup, selinux_xfrm_policy_lookup),
6967 LSM_HOOK_INIT(xfrm_state_pol_flow_match,
6968 selinux_xfrm_state_pol_flow_match),
6969 LSM_HOOK_INIT(xfrm_decode_session, selinux_xfrm_decode_session),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006970#endif
Michael LeMayd7200242006-06-22 14:47:17 -07006971
6972#ifdef CONFIG_KEYS
Casey Schauflere20b0432015-05-02 15:11:36 -07006973 LSM_HOOK_INIT(key_alloc, selinux_key_alloc),
6974 LSM_HOOK_INIT(key_free, selinux_key_free),
6975 LSM_HOOK_INIT(key_permission, selinux_key_permission),
6976 LSM_HOOK_INIT(key_getsecurity, selinux_key_getsecurity),
Michael LeMayd7200242006-06-22 14:47:17 -07006977#endif
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02006978
6979#ifdef CONFIG_AUDIT
Casey Schauflere20b0432015-05-02 15:11:36 -07006980 LSM_HOOK_INIT(audit_rule_init, selinux_audit_rule_init),
6981 LSM_HOOK_INIT(audit_rule_known, selinux_audit_rule_known),
6982 LSM_HOOK_INIT(audit_rule_match, selinux_audit_rule_match),
6983 LSM_HOOK_INIT(audit_rule_free, selinux_audit_rule_free),
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02006984#endif
Chenbo Fengec27c352017-10-18 13:00:25 -07006985
6986#ifdef CONFIG_BPF_SYSCALL
6987 LSM_HOOK_INIT(bpf, selinux_bpf),
6988 LSM_HOOK_INIT(bpf_map, selinux_bpf_map),
6989 LSM_HOOK_INIT(bpf_prog, selinux_bpf_prog),
6990 LSM_HOOK_INIT(bpf_map_alloc_security, selinux_bpf_map_alloc),
6991 LSM_HOOK_INIT(bpf_prog_alloc_security, selinux_bpf_prog_alloc),
6992 LSM_HOOK_INIT(bpf_map_free_security, selinux_bpf_map_free),
6993 LSM_HOOK_INIT(bpf_prog_free_security, selinux_bpf_prog_free),
6994#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006995};
6996
6997static __init int selinux_init(void)
6998{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07006999 if (!security_module_enable("selinux")) {
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02007000 selinux_enabled = 0;
7001 return 0;
7002 }
7003
Linus Torvalds1da177e2005-04-16 15:20:36 -07007004 if (!selinux_enabled) {
peter enderborgc103a912018-06-12 10:09:03 +02007005 pr_info("SELinux: Disabled at boot.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007006 return 0;
7007 }
7008
peter enderborgc103a912018-06-12 10:09:03 +02007009 pr_info("SELinux: Initializing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007010
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007011 memset(&selinux_state, 0, sizeof(selinux_state));
Paul Mooree5a5ca92018-03-01 17:38:30 -05007012 enforcing_set(&selinux_state, selinux_enforcing_boot);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007013 selinux_state.checkreqprot = selinux_checkreqprot_boot;
7014 selinux_ss_init(&selinux_state.ss);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05007015 selinux_avc_init(&selinux_state.avc);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007016
Linus Torvalds1da177e2005-04-16 15:20:36 -07007017 /* Set the security state for the initial task. */
David Howellsd84f4f92008-11-14 10:39:23 +11007018 cred_init_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007019
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04007020 default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
7021
James Morris7cae7e22006-03-22 00:09:22 -08007022 sel_inode_cache = kmem_cache_create("selinux_inode_security",
7023 sizeof(struct inode_security_struct),
Paul Mundt20c2df82007-07-20 10:11:58 +09007024 0, SLAB_PANIC, NULL);
Sangwoo63205652015-10-21 17:44:30 -04007025 file_security_cache = kmem_cache_create("selinux_file_security",
7026 sizeof(struct file_security_struct),
7027 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007028 avc_init();
7029
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007030 avtab_cache_init();
7031
7032 ebitmap_cache_init();
7033
7034 hashtab_cache_init();
7035
Casey Schauflerd69dece52017-01-18 17:09:05 -08007036 security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks), "selinux");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007037
Paul Moore615e51f2014-06-26 14:33:56 -04007038 if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET))
7039 panic("SELinux: Unable to register AVC netcache callback\n");
7040
Daniel Jurgens8f408ab2017-05-19 15:48:53 +03007041 if (avc_add_callback(selinux_lsm_notifier_avc_callback, AVC_CALLBACK_RESET))
7042 panic("SELinux: Unable to register AVC LSM notifier callback\n");
7043
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007044 if (selinux_enforcing_boot)
peter enderborgc103a912018-06-12 10:09:03 +02007045 pr_debug("SELinux: Starting in enforcing mode\n");
Eric Paris828dfe12008-04-17 13:17:49 -04007046 else
peter enderborgc103a912018-06-12 10:09:03 +02007047 pr_debug("SELinux: Starting in permissive mode\n");
Michael LeMayd7200242006-06-22 14:47:17 -07007048
Linus Torvalds1da177e2005-04-16 15:20:36 -07007049 return 0;
7050}
7051
Al Viroe8c26252010-03-23 06:36:54 -04007052static void delayed_superblock_init(struct super_block *sb, void *unused)
7053{
Al Viro17f3b552018-11-10 23:46:07 -05007054 struct security_mnt_opts opts;
7055
7056 security_init_mnt_opts(&opts);
7057 selinux_set_mnt_opts(sb, &opts, 0, NULL);
7058 security_free_mnt_opts(&opts);
Al Viroe8c26252010-03-23 06:36:54 -04007059}
7060
Linus Torvalds1da177e2005-04-16 15:20:36 -07007061void selinux_complete_init(void)
7062{
peter enderborgc103a912018-06-12 10:09:03 +02007063 pr_debug("SELinux: Completing initialization.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007064
7065 /* Set up any superblocks initialized prior to the policy load. */
peter enderborgc103a912018-06-12 10:09:03 +02007066 pr_debug("SELinux: Setting up existing superblocks.\n");
Al Viroe8c26252010-03-23 06:36:54 -04007067 iterate_supers(delayed_superblock_init, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007068}
7069
7070/* SELinux requires early initialization in order to label
7071 all processes and objects when they are created. */
Kees Cook3d6e5f62018-10-10 17:18:23 -07007072DEFINE_LSM(selinux) = {
Kees Cook07aed2f2018-10-10 17:18:24 -07007073 .name = "selinux",
Kees Cook3d6e5f62018-10-10 17:18:23 -07007074 .init = selinux_init,
7075};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007076
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007077#if defined(CONFIG_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007078
Florian Westphal591bb272017-07-26 11:40:52 +02007079static const struct nf_hook_ops selinux_nf_ops[] = {
Paul Mooreeffad8d2008-01-29 08:49:27 -05007080 {
7081 .hook = selinux_ipv4_postroute,
Alban Crequy2597a832012-05-14 03:56:39 +00007082 .pf = NFPROTO_IPV4,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007083 .hooknum = NF_INET_POST_ROUTING,
7084 .priority = NF_IP_PRI_SELINUX_LAST,
7085 },
7086 {
7087 .hook = selinux_ipv4_forward,
Alban Crequy2597a832012-05-14 03:56:39 +00007088 .pf = NFPROTO_IPV4,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007089 .hooknum = NF_INET_FORWARD,
7090 .priority = NF_IP_PRI_SELINUX_FIRST,
Paul Moore948bf852008-10-10 10:16:32 -04007091 },
7092 {
7093 .hook = selinux_ipv4_output,
Alban Crequy2597a832012-05-14 03:56:39 +00007094 .pf = NFPROTO_IPV4,
Paul Moore948bf852008-10-10 10:16:32 -04007095 .hooknum = NF_INET_LOCAL_OUT,
7096 .priority = NF_IP_PRI_SELINUX_FIRST,
Jiri Pirko25db6be2014-09-03 17:42:13 +02007097 },
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04007098#if IS_ENABLED(CONFIG_IPV6)
Paul Mooreeffad8d2008-01-29 08:49:27 -05007099 {
7100 .hook = selinux_ipv6_postroute,
Alban Crequy2597a832012-05-14 03:56:39 +00007101 .pf = NFPROTO_IPV6,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007102 .hooknum = NF_INET_POST_ROUTING,
7103 .priority = NF_IP6_PRI_SELINUX_LAST,
7104 },
7105 {
7106 .hook = selinux_ipv6_forward,
Alban Crequy2597a832012-05-14 03:56:39 +00007107 .pf = NFPROTO_IPV6,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007108 .hooknum = NF_INET_FORWARD,
7109 .priority = NF_IP6_PRI_SELINUX_FIRST,
Jiri Pirko25db6be2014-09-03 17:42:13 +02007110 },
Huw Davies2917f572016-06-27 15:06:15 -04007111 {
7112 .hook = selinux_ipv6_output,
7113 .pf = NFPROTO_IPV6,
7114 .hooknum = NF_INET_LOCAL_OUT,
7115 .priority = NF_IP6_PRI_SELINUX_FIRST,
7116 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07007117#endif /* IPV6 */
Jiri Pirko25db6be2014-09-03 17:42:13 +02007118};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007119
Florian Westphal8e71bf72017-04-21 11:49:09 +02007120static int __net_init selinux_nf_register(struct net *net)
7121{
7122 return nf_register_net_hooks(net, selinux_nf_ops,
7123 ARRAY_SIZE(selinux_nf_ops));
7124}
7125
7126static void __net_exit selinux_nf_unregister(struct net *net)
7127{
7128 nf_unregister_net_hooks(net, selinux_nf_ops,
7129 ARRAY_SIZE(selinux_nf_ops));
7130}
7131
7132static struct pernet_operations selinux_net_ops = {
7133 .init = selinux_nf_register,
7134 .exit = selinux_nf_unregister,
7135};
7136
Linus Torvalds1da177e2005-04-16 15:20:36 -07007137static int __init selinux_nf_ip_init(void)
7138{
Jiri Pirko25db6be2014-09-03 17:42:13 +02007139 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007140
7141 if (!selinux_enabled)
Jiri Pirko25db6be2014-09-03 17:42:13 +02007142 return 0;
Eric Parisfadcdb42007-02-22 18:11:31 -05007143
peter enderborgc103a912018-06-12 10:09:03 +02007144 pr_debug("SELinux: Registering netfilter hooks\n");
Eric Parisfadcdb42007-02-22 18:11:31 -05007145
Florian Westphal8e71bf72017-04-21 11:49:09 +02007146 err = register_pernet_subsys(&selinux_net_ops);
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07007147 if (err)
Florian Westphal8e71bf72017-04-21 11:49:09 +02007148 panic("SELinux: register_pernet_subsys: error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007149
Jiri Pirko25db6be2014-09-03 17:42:13 +02007150 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007151}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007152__initcall(selinux_nf_ip_init);
7153
7154#ifdef CONFIG_SECURITY_SELINUX_DISABLE
7155static void selinux_nf_ip_exit(void)
7156{
peter enderborgc103a912018-06-12 10:09:03 +02007157 pr_debug("SELinux: Unregistering netfilter hooks\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007158
Florian Westphal8e71bf72017-04-21 11:49:09 +02007159 unregister_pernet_subsys(&selinux_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007160}
7161#endif
7162
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007163#else /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007164
7165#ifdef CONFIG_SECURITY_SELINUX_DISABLE
7166#define selinux_nf_ip_exit()
7167#endif
7168
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007169#endif /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007170
7171#ifdef CONFIG_SECURITY_SELINUX_DISABLE
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007172int selinux_disable(struct selinux_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007173{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007174 if (state->initialized) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007175 /* Not permitted after initial policy load. */
7176 return -EINVAL;
7177 }
7178
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007179 if (state->disabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007180 /* Only do this once. */
7181 return -EINVAL;
7182 }
7183
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007184 state->disabled = 1;
7185
peter enderborgc103a912018-06-12 10:09:03 +02007186 pr_info("SELinux: Disabled at runtime.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007187
Stephen Smalley30d55282006-05-03 10:52:36 -04007188 selinux_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007189
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07007190 security_delete_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007191
Eric Parisaf8ff042009-09-20 21:23:01 -04007192 /* Try to destroy the avc node cache */
7193 avc_disable();
7194
Linus Torvalds1da177e2005-04-16 15:20:36 -07007195 /* Unregister netfilter hooks. */
7196 selinux_nf_ip_exit();
7197
7198 /* Unregister selinuxfs. */
7199 exit_sel_fs();
7200
7201 return 0;
7202}
7203#endif