blob: caf7ca7abfc16fb04b86060e738f5d060e48aacd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * NSA Security-Enhanced Linux (SELinux) security module
3 *
4 * This file contains the SELinux hook function implementations.
5 *
Stephen Smalley7efbb602017-08-17 13:32:36 -04006 * Authors: Stephen Smalley, <sds@tycho.nsa.gov>
Eric Paris828dfe12008-04-17 13:17:49 -04007 * Chris Vance, <cvance@nai.com>
8 * Wayne Salamon, <wsalamon@nai.com>
9 * James Morris <jmorris@redhat.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * Copyright (C) 2001,2002 Networks Associates Technology, Inc.
Eric Paris2069f452008-07-04 09:47:13 +100012 * Copyright (C) 2003-2008 Red Hat, Inc., James Morris <jmorris@redhat.com>
13 * Eric Paris <eparis@redhat.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
Eric Paris828dfe12008-04-17 13:17:49 -040015 * <dgoeddel@trustedcs.com>
Paul Mooreed6d76e2009-08-28 18:12:49 -040016 * Copyright (C) 2006, 2007, 2009 Hewlett-Packard Development Company, L.P.
Paul Moore82c21bf2011-08-01 11:10:33 +000017 * Paul Moore <paul@paul-moore.com>
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +090018 * Copyright (C) 2007 Hitachi Software Engineering Co., Ltd.
Eric Paris828dfe12008-04-17 13:17:49 -040019 * Yuichi Nakamura <ynakam@hitachisoft.jp>
Daniel Jurgens3a976fa2017-05-19 15:48:56 +030020 * Copyright (C) 2016 Mellanox Technologies
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 *
22 * This program is free software; you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License version 2,
Eric Paris828dfe12008-04-17 13:17:49 -040024 * as published by the Free Software Foundation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 */
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/init.h>
Eric Paris0b24dcb2011-02-25 15:39:20 -050028#include <linux/kd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/kernel.h>
Roland McGrath0d094ef2008-07-25 19:45:49 -070030#include <linux/tracehook.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/errno.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010032#include <linux/sched/signal.h>
Ingo Molnar29930022017-02-08 18:51:36 +010033#include <linux/sched/task.h>
Casey Schaufler3c4ed7b2015-05-02 15:10:46 -070034#include <linux/lsm_hooks.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/xattr.h>
36#include <linux/capability.h>
37#include <linux/unistd.h>
38#include <linux/mm.h>
39#include <linux/mman.h>
40#include <linux/slab.h>
41#include <linux/pagemap.h>
Eric Paris0b24dcb2011-02-25 15:39:20 -050042#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/swap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/spinlock.h>
45#include <linux/syscalls.h>
Eric Paris2a7dba32011-02-01 11:05:39 -050046#include <linux/dcache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/file.h>
Al Viro9f3acc32008-04-24 07:44:08 -040048#include <linux/fdtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/namei.h>
50#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/netfilter_ipv4.h>
52#include <linux/netfilter_ipv6.h>
53#include <linux/tty.h>
54#include <net/icmp.h>
Stephen Hemminger227b60f2007-10-10 17:30:46 -070055#include <net/ip.h> /* for local_port_range[] */
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <net/tcp.h> /* struct or_callable used in sock_rcv_skb */
Paul Moore47180062013-12-04 16:10:45 -050057#include <net/inet_connection_sock.h>
Paul Moore220deb92008-01-29 08:38:23 -050058#include <net/net_namespace.h>
Paul Moored621d352008-01-29 08:43:36 -050059#include <net/netlabel.h>
Eric Parisf5269712008-05-14 11:27:45 -040060#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <asm/ioctls.h>
Arun Sharma600634972011-07-26 16:09:06 -070062#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <linux/bitops.h>
64#include <linux/interrupt.h>
65#include <linux/netdevice.h> /* for network interface checks */
Hong zhi guo77954982013-03-27 06:49:35 +000066#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <linux/tcp.h>
68#include <linux/udp.h>
James Morris2ee92d42006-11-13 16:09:01 -080069#include <linux/dccp.h>
Richard Hainesd4529302018-02-13 20:57:18 +000070#include <linux/sctp.h>
71#include <net/sctp/structs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <linux/quota.h>
73#include <linux/un.h> /* for Unix socket types */
74#include <net/af_unix.h> /* for Unix socket types */
75#include <linux/parser.h>
76#include <linux/nfs_mount.h>
77#include <net/ipv6.h>
78#include <linux/hugetlb.h>
79#include <linux/personality.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#include <linux/audit.h>
Eric Paris6931dfc2005-06-30 02:58:51 -070081#include <linux/string.h>
Catherine Zhang877ce7c2006-06-29 12:27:47 -070082#include <linux/selinux.h>
Eric Paris23970742006-09-25 23:32:01 -070083#include <linux/mutex.h>
Frank Mayharf06febc2008-09-12 09:54:39 -070084#include <linux/posix-timers.h>
Kees Cook00234592010-02-03 15:36:43 -080085#include <linux/syslog.h>
Serge E. Hallyn34867402011-03-23 16:43:17 -070086#include <linux/user_namespace.h>
Paul Gortmaker44fc7ea2011-05-26 20:52:10 -040087#include <linux/export.h>
Al Viro40401532012-02-13 03:58:52 +000088#include <linux/msg.h>
89#include <linux/shm.h>
Chenbo Fengec27c352017-10-18 13:00:25 -070090#include <linux/bpf.h>
David Howellse262e32d2018-11-01 23:07:23 +000091#include <uapi/linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93#include "avc.h"
94#include "objsec.h"
95#include "netif.h"
Paul Moore224dfbd2008-01-29 08:38:13 -050096#include "netnode.h"
Paul Moore3e1121722008-04-10 10:48:14 -040097#include "netport.h"
Daniel Jurgens409dcf32017-05-19 15:48:59 +030098#include "ibpkey.h"
Trent Jaegerd28d1e02005-12-13 23:12:40 -080099#include "xfrm.h"
Paul Moorec60475b2007-02-28 15:14:23 -0500100#include "netlabel.h"
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +0200101#include "audit.h"
James Morris7b98a582011-08-30 12:52:32 +1000102#include "avc_ss.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500104struct selinux_state selinux_state;
105
Paul Moored621d352008-01-29 08:43:36 -0500106/* SECMARK reference count */
James Morris56a4ca92011-08-17 11:08:43 +1000107static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0);
Paul Moored621d352008-01-29 08:43:36 -0500108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109#ifdef CONFIG_SECURITY_SELINUX_DEVELOP
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500110static int selinux_enforcing_boot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112static int __init enforcing_setup(char *str)
113{
Eric Parisf5269712008-05-14 11:27:45 -0400114 unsigned long enforcing;
Jingoo Han29707b22014-02-05 15:13:14 +0900115 if (!kstrtoul(str, 0, &enforcing))
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500116 selinux_enforcing_boot = enforcing ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return 1;
118}
119__setup("enforcing=", enforcing_setup);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500120#else
121#define selinux_enforcing_boot 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122#endif
123
124#ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM
125int selinux_enabled = CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE;
126
127static int __init selinux_enabled_setup(char *str)
128{
Eric Parisf5269712008-05-14 11:27:45 -0400129 unsigned long enabled;
Jingoo Han29707b22014-02-05 15:13:14 +0900130 if (!kstrtoul(str, 0, &enabled))
Eric Parisf5269712008-05-14 11:27:45 -0400131 selinux_enabled = enabled ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 return 1;
133}
134__setup("selinux=", selinux_enabled_setup);
Stephen Smalley30d55282006-05-03 10:52:36 -0400135#else
136int selinux_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137#endif
138
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500139static unsigned int selinux_checkreqprot_boot =
140 CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE;
141
142static int __init checkreqprot_setup(char *str)
143{
144 unsigned long checkreqprot;
145
146 if (!kstrtoul(str, 0, &checkreqprot))
147 selinux_checkreqprot_boot = checkreqprot ? 1 : 0;
148 return 1;
149}
150__setup("checkreqprot=", checkreqprot_setup);
151
Christoph Lametere18b8902006-12-06 20:33:20 -0800152static struct kmem_cache *sel_inode_cache;
Sangwoo63205652015-10-21 17:44:30 -0400153static struct kmem_cache *file_security_cache;
James Morris7cae7e22006-03-22 00:09:22 -0800154
Paul Moored621d352008-01-29 08:43:36 -0500155/**
156 * selinux_secmark_enabled - Check to see if SECMARK is currently enabled
157 *
158 * Description:
159 * This function checks the SECMARK reference counter to see if any SECMARK
160 * targets are currently configured, if the reference counter is greater than
161 * zero SECMARK is considered to be enabled. Returns true (1) if SECMARK is
Chris PeBenito2be4d742013-05-03 09:05:39 -0400162 * enabled, false (0) if SECMARK is disabled. If the always_check_network
163 * policy capability is enabled, SECMARK is always considered enabled.
Paul Moored621d352008-01-29 08:43:36 -0500164 *
165 */
166static int selinux_secmark_enabled(void)
167{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500168 return (selinux_policycap_alwaysnetwork() ||
169 atomic_read(&selinux_secmark_refcount));
Chris PeBenito2be4d742013-05-03 09:05:39 -0400170}
171
172/**
173 * selinux_peerlbl_enabled - Check to see if peer labeling is currently enabled
174 *
175 * Description:
176 * This function checks if NetLabel or labeled IPSEC is enabled. Returns true
177 * (1) if any are enabled or false (0) if neither are enabled. If the
178 * always_check_network policy capability is enabled, peer labeling
179 * is always considered enabled.
180 *
181 */
182static int selinux_peerlbl_enabled(void)
183{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500184 return (selinux_policycap_alwaysnetwork() ||
185 netlbl_enabled() || selinux_xfrm_enabled());
Paul Moored621d352008-01-29 08:43:36 -0500186}
187
Paul Moore615e51f2014-06-26 14:33:56 -0400188static int selinux_netcache_avc_callback(u32 event)
189{
190 if (event == AVC_CALLBACK_RESET) {
191 sel_netif_flush();
192 sel_netnode_flush();
193 sel_netport_flush();
194 synchronize_net();
195 }
196 return 0;
197}
198
Daniel Jurgens8f408ab2017-05-19 15:48:53 +0300199static int selinux_lsm_notifier_avc_callback(u32 event)
200{
Daniel Jurgens409dcf32017-05-19 15:48:59 +0300201 if (event == AVC_CALLBACK_RESET) {
202 sel_ib_pkey_flush();
Daniel Jurgens8f408ab2017-05-19 15:48:53 +0300203 call_lsm_notifier(LSM_POLICY_CHANGE, NULL);
Daniel Jurgens409dcf32017-05-19 15:48:59 +0300204 }
Daniel Jurgens8f408ab2017-05-19 15:48:53 +0300205
206 return 0;
207}
208
David Howellsd84f4f92008-11-14 10:39:23 +1100209/*
210 * initialise the security for the init task
211 */
212static void cred_init_security(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
David Howells3b11a1d2008-11-14 10:39:26 +1100214 struct cred *cred = (struct cred *) current->real_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 struct task_security_struct *tsec;
216
James Morris89d155e2005-10-30 14:59:21 -0800217 tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 if (!tsec)
David Howellsd84f4f92008-11-14 10:39:23 +1100219 panic("SELinux: Failed to initialize initial task.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
David Howellsd84f4f92008-11-14 10:39:23 +1100221 tsec->osid = tsec->sid = SECINITSID_KERNEL;
David Howellsf1752ee2008-11-14 10:39:17 +1100222 cred->security = tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
David Howells275bb412008-11-14 10:39:19 +1100225/*
David Howells88e67f32008-11-14 10:39:21 +1100226 * get the security ID of a set of credentials
227 */
228static inline u32 cred_sid(const struct cred *cred)
229{
230 const struct task_security_struct *tsec;
231
232 tsec = cred->security;
233 return tsec->sid;
234}
235
236/*
David Howells3b11a1d2008-11-14 10:39:26 +1100237 * get the objective security ID of a task
David Howells275bb412008-11-14 10:39:19 +1100238 */
239static inline u32 task_sid(const struct task_struct *task)
240{
David Howells275bb412008-11-14 10:39:19 +1100241 u32 sid;
242
243 rcu_read_lock();
David Howells88e67f32008-11-14 10:39:21 +1100244 sid = cred_sid(__task_cred(task));
David Howells275bb412008-11-14 10:39:19 +1100245 rcu_read_unlock();
246 return sid;
247}
248
David Howells88e67f32008-11-14 10:39:21 +1100249/* Allocate and free functions for each kind of security blob. */
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251static int inode_alloc_security(struct inode *inode)
252{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 struct inode_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +1100254 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Josef Bacika02fe132008-04-04 09:35:05 +1100256 isec = kmem_cache_zalloc(sel_inode_cache, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 if (!isec)
258 return -ENOMEM;
259
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +0100260 spin_lock_init(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 INIT_LIST_HEAD(&isec->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 isec->inode = inode;
263 isec->sid = SECINITSID_UNLABELED;
264 isec->sclass = SECCLASS_FILE;
David Howells275bb412008-11-14 10:39:19 +1100265 isec->task_sid = sid;
Andreas Gruenbacher42059112016-11-10 22:18:27 +0100266 isec->initialized = LABEL_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 inode->i_security = isec;
268
269 return 0;
270}
271
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500272static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry);
273
274/*
275 * Try reloading inode security labels that have been marked as invalid. The
276 * @may_sleep parameter indicates when sleeping and thus reloading labels is
Andreas Gruenbacher42059112016-11-10 22:18:27 +0100277 * allowed; when set to false, returns -ECHILD when the label is
Al Viroe9193282018-04-24 21:31:02 -0400278 * invalid. The @dentry parameter should be set to a dentry of the inode.
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500279 */
280static int __inode_security_revalidate(struct inode *inode,
Al Viroe9193282018-04-24 21:31:02 -0400281 struct dentry *dentry,
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500282 bool may_sleep)
283{
284 struct inode_security_struct *isec = inode->i_security;
285
286 might_sleep_if(may_sleep);
287
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500288 if (selinux_state.initialized &&
289 isec->initialized != LABEL_INITIALIZED) {
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500290 if (!may_sleep)
291 return -ECHILD;
292
293 /*
294 * Try reloading the inode security label. This will fail if
295 * @opt_dentry is NULL and no dentry for this inode can be
296 * found; in that case, continue using the old label.
297 */
Al Viroe9193282018-04-24 21:31:02 -0400298 inode_doinit_with_dentry(inode, dentry);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500299 }
300 return 0;
301}
302
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500303static struct inode_security_struct *inode_security_novalidate(struct inode *inode)
304{
305 return inode->i_security;
306}
307
308static struct inode_security_struct *inode_security_rcu(struct inode *inode, bool rcu)
309{
310 int error;
311
312 error = __inode_security_revalidate(inode, NULL, !rcu);
313 if (error)
314 return ERR_PTR(error);
315 return inode->i_security;
316}
317
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500318/*
319 * Get the security label of an inode.
320 */
321static struct inode_security_struct *inode_security(struct inode *inode)
322{
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500323 __inode_security_revalidate(inode, NULL, true);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500324 return inode->i_security;
325}
326
Paul Moore2c971652016-04-19 16:36:28 -0400327static struct inode_security_struct *backing_inode_security_novalidate(struct dentry *dentry)
328{
329 struct inode *inode = d_backing_inode(dentry);
330
331 return inode->i_security;
332}
333
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500334/*
335 * Get the security label of a dentry's backing inode.
336 */
337static struct inode_security_struct *backing_inode_security(struct dentry *dentry)
338{
339 struct inode *inode = d_backing_inode(dentry);
340
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500341 __inode_security_revalidate(inode, dentry, true);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500342 return inode->i_security;
343}
344
Steven Rostedt3dc91d42014-01-09 21:46:34 -0500345static void inode_free_rcu(struct rcu_head *head)
346{
347 struct inode_security_struct *isec;
348
349 isec = container_of(head, struct inode_security_struct, rcu);
350 kmem_cache_free(sel_inode_cache, isec);
351}
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353static void inode_free_security(struct inode *inode)
354{
355 struct inode_security_struct *isec = inode->i_security;
356 struct superblock_security_struct *sbsec = inode->i_sb->s_security;
357
Waiman Long9629d042015-07-10 17:19:56 -0400358 /*
359 * As not all inode security structures are in a list, we check for
360 * empty list outside of the lock to make sure that we won't waste
361 * time taking a lock doing nothing.
362 *
363 * The list_del_init() function can be safely called more than once.
364 * It should not be possible for this function to be called with
365 * concurrent list_add(), but for better safety against future changes
366 * in the code, we use list_empty_careful() here.
367 */
368 if (!list_empty_careful(&isec->list)) {
369 spin_lock(&sbsec->isec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 list_del_init(&isec->list);
Waiman Long9629d042015-07-10 17:19:56 -0400371 spin_unlock(&sbsec->isec_lock);
372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Steven Rostedt3dc91d42014-01-09 21:46:34 -0500374 /*
375 * The inode may still be referenced in a path walk and
376 * a call to selinux_inode_permission() can be made
377 * after inode_free_security() is called. Ideally, the VFS
378 * wouldn't do this, but fixing that is a much harder
379 * job. For now, simply free the i_security via RCU, and
380 * leave the current inode->i_security pointer intact.
381 * The inode will be freed after the RCU grace period too.
382 */
383 call_rcu(&isec->rcu, inode_free_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
385
386static int file_alloc_security(struct file *file)
387{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 struct file_security_struct *fsec;
David Howells275bb412008-11-14 10:39:19 +1100389 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Sangwoo63205652015-10-21 17:44:30 -0400391 fsec = kmem_cache_zalloc(file_security_cache, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 if (!fsec)
393 return -ENOMEM;
394
David Howells275bb412008-11-14 10:39:19 +1100395 fsec->sid = sid;
396 fsec->fown_sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 file->f_security = fsec;
398
399 return 0;
400}
401
402static void file_free_security(struct file *file)
403{
404 struct file_security_struct *fsec = file->f_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 file->f_security = NULL;
Sangwoo63205652015-10-21 17:44:30 -0400406 kmem_cache_free(file_security_cache, fsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
409static int superblock_alloc_security(struct super_block *sb)
410{
411 struct superblock_security_struct *sbsec;
412
James Morris89d155e2005-10-30 14:59:21 -0800413 sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (!sbsec)
415 return -ENOMEM;
416
Eric Parisbc7e9822006-09-25 23:32:02 -0700417 mutex_init(&sbsec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 INIT_LIST_HEAD(&sbsec->isec_head);
419 spin_lock_init(&sbsec->isec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 sbsec->sb = sb;
421 sbsec->sid = SECINITSID_UNLABELED;
422 sbsec->def_sid = SECINITSID_FILE;
Eric Parisc312feb2006-07-10 04:43:53 -0700423 sbsec->mntpoint_sid = SECINITSID_UNLABELED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 sb->s_security = sbsec;
425
426 return 0;
427}
428
429static void superblock_free_security(struct super_block *sb)
430{
431 struct superblock_security_struct *sbsec = sb->s_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 sb->s_security = NULL;
433 kfree(sbsec);
434}
435
Al Viro204cc0c2018-12-13 13:41:47 -0500436static void selinux_free_mnt_opts(void *mnt_opts)
437{
438 struct security_mnt_opts *opts = mnt_opts;
439 int i;
440
441 if (opts->mnt_opts)
442 for (i = 0; i < opts->num_mnt_opts; i++)
443 kfree(opts->mnt_opts[i]);
444 kfree(opts->mnt_opts);
445 kfree(opts->mnt_opts_flags);
446 kfree(opts);
447}
448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449static inline int inode_doinit(struct inode *inode)
450{
451 return inode_doinit_with_dentry(inode, NULL);
452}
453
454enum {
Eric Paris31e87932007-09-19 17:19:12 -0400455 Opt_error = -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 Opt_context = 1,
457 Opt_fscontext = 2,
Eric Parisc9180a52007-11-30 13:00:35 -0500458 Opt_defcontext = 3,
459 Opt_rootcontext = 4,
David P. Quigley11689d42009-01-16 09:22:03 -0500460 Opt_labelsupport = 5,
Eric Parisd355987f2012-08-24 15:58:53 -0400461 Opt_nextmntopt = 6,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462};
463
Eric Parisd355987f2012-08-24 15:58:53 -0400464#define NUM_SEL_MNT_OPTS (Opt_nextmntopt - 1)
465
Steven Whitehousea447c092008-10-13 10:46:57 +0100466static const match_table_t tokens = {
Eric Paris832cbd92008-04-01 13:24:09 -0400467 {Opt_context, CONTEXT_STR "%s"},
468 {Opt_fscontext, FSCONTEXT_STR "%s"},
469 {Opt_defcontext, DEFCONTEXT_STR "%s"},
470 {Opt_rootcontext, ROOTCONTEXT_STR "%s"},
David P. Quigley11689d42009-01-16 09:22:03 -0500471 {Opt_labelsupport, LABELSUPP_STR},
Eric Paris31e87932007-09-19 17:19:12 -0400472 {Opt_error, NULL},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473};
474
475#define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n"
476
Eric Parisc312feb2006-07-10 04:43:53 -0700477static int may_context_mount_sb_relabel(u32 sid,
478 struct superblock_security_struct *sbsec,
David Howells275bb412008-11-14 10:39:19 +1100479 const struct cred *cred)
Eric Parisc312feb2006-07-10 04:43:53 -0700480{
David Howells275bb412008-11-14 10:39:19 +1100481 const struct task_security_struct *tsec = cred->security;
Eric Parisc312feb2006-07-10 04:43:53 -0700482 int rc;
483
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500484 rc = avc_has_perm(&selinux_state,
485 tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
Eric Parisc312feb2006-07-10 04:43:53 -0700486 FILESYSTEM__RELABELFROM, NULL);
487 if (rc)
488 return rc;
489
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500490 rc = avc_has_perm(&selinux_state,
491 tsec->sid, sid, SECCLASS_FILESYSTEM,
Eric Parisc312feb2006-07-10 04:43:53 -0700492 FILESYSTEM__RELABELTO, NULL);
493 return rc;
494}
495
Eric Paris08089252006-07-10 04:43:55 -0700496static int may_context_mount_inode_relabel(u32 sid,
497 struct superblock_security_struct *sbsec,
David Howells275bb412008-11-14 10:39:19 +1100498 const struct cred *cred)
Eric Paris08089252006-07-10 04:43:55 -0700499{
David Howells275bb412008-11-14 10:39:19 +1100500 const struct task_security_struct *tsec = cred->security;
Eric Paris08089252006-07-10 04:43:55 -0700501 int rc;
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500502 rc = avc_has_perm(&selinux_state,
503 tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
Eric Paris08089252006-07-10 04:43:55 -0700504 FILESYSTEM__RELABELFROM, NULL);
505 if (rc)
506 return rc;
507
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500508 rc = avc_has_perm(&selinux_state,
509 sid, sbsec->sid, SECCLASS_FILESYSTEM,
Eric Paris08089252006-07-10 04:43:55 -0700510 FILESYSTEM__ASSOCIATE, NULL);
511 return rc;
512}
513
Eric Parisb43e7252012-10-10 14:27:35 -0400514static int selinux_is_sblabel_mnt(struct super_block *sb)
515{
516 struct superblock_security_struct *sbsec = sb->s_security;
517
Mark Salyzynd5f3a5f2015-02-04 11:34:30 -0500518 return sbsec->behavior == SECURITY_FS_USE_XATTR ||
519 sbsec->behavior == SECURITY_FS_USE_TRANS ||
520 sbsec->behavior == SECURITY_FS_USE_TASK ||
J. Bruce Fields9fc2b4b2015-06-04 15:57:25 -0400521 sbsec->behavior == SECURITY_FS_USE_NATIVE ||
Mark Salyzynd5f3a5f2015-02-04 11:34:30 -0500522 /* Special handling. Genfs but also in-core setxattr handler */
523 !strcmp(sb->s_type->name, "sysfs") ||
524 !strcmp(sb->s_type->name, "pstore") ||
525 !strcmp(sb->s_type->name, "debugfs") ||
Yongqin Liua2c7c6f2017-01-09 10:07:30 -0500526 !strcmp(sb->s_type->name, "tracefs") ||
Stephen Smalley2651225b2017-02-28 10:35:56 -0500527 !strcmp(sb->s_type->name, "rootfs") ||
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500528 (selinux_policycap_cgroupseclabel() &&
Stephen Smalley2651225b2017-02-28 10:35:56 -0500529 (!strcmp(sb->s_type->name, "cgroup") ||
530 !strcmp(sb->s_type->name, "cgroup2")));
Eric Parisb43e7252012-10-10 14:27:35 -0400531}
532
Eric Parisc9180a52007-11-30 13:00:35 -0500533static int sb_finish_set_opts(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
535 struct superblock_security_struct *sbsec = sb->s_security;
536 struct dentry *root = sb->s_root;
David Howellsc6f493d2015-03-17 22:26:22 +0000537 struct inode *root_inode = d_backing_inode(root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 int rc = 0;
539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
541 /* Make sure that the xattr handler exists and that no
542 error other than -ENODATA is returned by getxattr on
543 the root directory. -ENODATA is ok, as this may be
544 the first boot of the SELinux kernel before we have
545 assigned xattr values to the filesystem. */
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200546 if (!(root_inode->i_opflags & IOP_XATTR)) {
peter enderborgc103a912018-06-12 10:09:03 +0200547 pr_warn("SELinux: (dev %s, type %s) has no "
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800548 "xattr support\n", sb->s_id, sb->s_type->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 rc = -EOPNOTSUPP;
550 goto out;
551 }
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200552
553 rc = __vfs_getxattr(root, root_inode, XATTR_NAME_SELINUX, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 if (rc < 0 && rc != -ENODATA) {
555 if (rc == -EOPNOTSUPP)
peter enderborgc103a912018-06-12 10:09:03 +0200556 pr_warn("SELinux: (dev %s, type "
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800557 "%s) has no security xattr handler\n",
558 sb->s_id, sb->s_type->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 else
peter enderborgc103a912018-06-12 10:09:03 +0200560 pr_warn("SELinux: (dev %s, type "
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800561 "%s) getxattr errno %d\n", sb->s_id,
562 sb->s_type->name, -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 goto out;
564 }
565 }
566
Eric Pariseadcabc2012-08-24 15:59:14 -0400567 sbsec->flags |= SE_SBINITIALIZED;
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400568
569 /*
570 * Explicitly set or clear SBLABEL_MNT. It's not sufficient to simply
571 * leave the flag untouched because sb_clone_mnt_opts might be handing
572 * us a superblock that needs the flag to be cleared.
573 */
Eric Parisb43e7252012-10-10 14:27:35 -0400574 if (selinux_is_sblabel_mnt(sb))
Eric Paris12f348b2012-10-09 10:56:25 -0400575 sbsec->flags |= SBLABEL_MNT;
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400576 else
577 sbsec->flags &= ~SBLABEL_MNT;
David P. Quigleyddd29ec2009-09-09 14:25:37 -0400578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 /* Initialize the root inode. */
Eric Parisc9180a52007-11-30 13:00:35 -0500580 rc = inode_doinit_with_dentry(root_inode, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 /* Initialize any other inodes associated with the superblock, e.g.
583 inodes created prior to initial policy load or inodes created
584 during get_sb by a pseudo filesystem that directly
585 populates itself. */
586 spin_lock(&sbsec->isec_lock);
Al Viro8d641242018-12-10 15:34:12 -0500587 while (!list_empty(&sbsec->isec_head)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 struct inode_security_struct *isec =
Al Viro8d641242018-12-10 15:34:12 -0500589 list_first_entry(&sbsec->isec_head,
Eric Parisc9180a52007-11-30 13:00:35 -0500590 struct inode_security_struct, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 struct inode *inode = isec->inode;
Stephen Smalley923190d2014-10-06 16:32:52 -0400592 list_del_init(&isec->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 spin_unlock(&sbsec->isec_lock);
594 inode = igrab(inode);
595 if (inode) {
Eric Parisc9180a52007-11-30 13:00:35 -0500596 if (!IS_PRIVATE(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 inode_doinit(inode);
598 iput(inode);
599 }
600 spin_lock(&sbsec->isec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 }
602 spin_unlock(&sbsec->isec_lock);
603out:
Eric Parisc9180a52007-11-30 13:00:35 -0500604 return rc;
605}
606
Eric Parisc9180a52007-11-30 13:00:35 -0500607static int bad_option(struct superblock_security_struct *sbsec, char flag,
608 u32 old_sid, u32 new_sid)
609{
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500610 char mnt_flags = sbsec->flags & SE_MNTMASK;
611
Eric Parisc9180a52007-11-30 13:00:35 -0500612 /* check if the old mount command had the same options */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500613 if (sbsec->flags & SE_SBINITIALIZED)
Eric Parisc9180a52007-11-30 13:00:35 -0500614 if (!(sbsec->flags & flag) ||
615 (old_sid != new_sid))
616 return 1;
617
618 /* check if we were passed the same options twice,
619 * aka someone passed context=a,context=b
620 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500621 if (!(sbsec->flags & SE_SBINITIALIZED))
622 if (mnt_flags & flag)
Eric Parisc9180a52007-11-30 13:00:35 -0500623 return 1;
624 return 0;
625}
Eric Parise0007522008-03-05 10:31:54 -0500626
Eric Parisc9180a52007-11-30 13:00:35 -0500627/*
628 * Allow filesystems with binary mount data to explicitly set mount point
629 * labeling information.
630 */
Eric Parise0007522008-03-05 10:31:54 -0500631static int selinux_set_mnt_opts(struct super_block *sb,
Al Viro204cc0c2018-12-13 13:41:47 -0500632 void *mnt_opts,
David Quigley649f6e72013-05-22 12:50:36 -0400633 unsigned long kern_flags,
634 unsigned long *set_kern_flags)
Eric Parisc9180a52007-11-30 13:00:35 -0500635{
David Howells275bb412008-11-14 10:39:19 +1100636 const struct cred *cred = current_cred();
Eric Parisc9180a52007-11-30 13:00:35 -0500637 int rc = 0, i;
Eric Parisc9180a52007-11-30 13:00:35 -0500638 struct superblock_security_struct *sbsec = sb->s_security;
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800639 const char *name = sb->s_type->name;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500640 struct dentry *root = sbsec->sb->s_root;
Paul Moore2c971652016-04-19 16:36:28 -0400641 struct inode_security_struct *root_isec;
Eric Parisc9180a52007-11-30 13:00:35 -0500642 u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
643 u32 defcontext_sid = 0;
Al Viro204cc0c2018-12-13 13:41:47 -0500644 struct security_mnt_opts *opts = mnt_opts;
645 char **mount_options = opts ? opts->mnt_opts : NULL;
646 int *flags = opts ? opts->mnt_opts_flags : NULL;
647 int num_opts = opts ? opts->num_mnt_opts : 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500648
649 mutex_lock(&sbsec->lock);
650
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500651 if (!selinux_state.initialized) {
Eric Parisc9180a52007-11-30 13:00:35 -0500652 if (!num_opts) {
653 /* Defer initialization until selinux_complete_init,
654 after the initial policy is loaded and the security
655 server is ready to handle calls. */
Eric Parisc9180a52007-11-30 13:00:35 -0500656 goto out;
657 }
658 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +0200659 pr_warn("SELinux: Unable to set superblock options "
Eric Paris744ba352008-04-17 11:52:44 -0400660 "before the security server is initialized\n");
Eric Parisc9180a52007-11-30 13:00:35 -0500661 goto out;
662 }
David Quigley649f6e72013-05-22 12:50:36 -0400663 if (kern_flags && !set_kern_flags) {
664 /* Specifying internal flags without providing a place to
665 * place the results is not allowed */
666 rc = -EINVAL;
667 goto out;
668 }
Eric Parisc9180a52007-11-30 13:00:35 -0500669
670 /*
Eric Parise0007522008-03-05 10:31:54 -0500671 * Binary mount data FS will come through this function twice. Once
672 * from an explicit call and once from the generic calls from the vfs.
673 * Since the generic VFS calls will not contain any security mount data
674 * we need to skip the double mount verification.
675 *
676 * This does open a hole in which we will not notice if the first
677 * mount using this sb set explict options and a second mount using
678 * this sb does not set any security options. (The first options
679 * will be used for both mounts)
680 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500681 if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
Eric Parise0007522008-03-05 10:31:54 -0500682 && (num_opts == 0))
Eric Parisf5269712008-05-14 11:27:45 -0400683 goto out;
Eric Parise0007522008-03-05 10:31:54 -0500684
Paul Moore2c971652016-04-19 16:36:28 -0400685 root_isec = backing_inode_security_novalidate(root);
686
Eric Parise0007522008-03-05 10:31:54 -0500687 /*
Eric Parisc9180a52007-11-30 13:00:35 -0500688 * parse the mount options, check if they are valid sids.
689 * also check if someone is trying to mount the same sb more
690 * than once with different security options.
691 */
692 for (i = 0; i < num_opts; i++) {
693 u32 sid;
David P. Quigley11689d42009-01-16 09:22:03 -0500694
Eric Paris12f348b2012-10-09 10:56:25 -0400695 if (flags[i] == SBLABEL_MNT)
David P. Quigley11689d42009-01-16 09:22:03 -0500696 continue;
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500697 rc = security_context_str_to_sid(&selinux_state,
698 mount_options[i], &sid,
699 GFP_KERNEL);
Eric Parisc9180a52007-11-30 13:00:35 -0500700 if (rc) {
peter enderborgc103a912018-06-12 10:09:03 +0200701 pr_warn("SELinux: security_context_str_to_sid"
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800702 "(%s) failed for (dev %s, type %s) errno=%d\n",
703 mount_options[i], sb->s_id, name, rc);
Eric Parisc9180a52007-11-30 13:00:35 -0500704 goto out;
705 }
706 switch (flags[i]) {
707 case FSCONTEXT_MNT:
708 fscontext_sid = sid;
709
710 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
711 fscontext_sid))
712 goto out_double_mount;
713
714 sbsec->flags |= FSCONTEXT_MNT;
715 break;
716 case CONTEXT_MNT:
717 context_sid = sid;
718
719 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
720 context_sid))
721 goto out_double_mount;
722
723 sbsec->flags |= CONTEXT_MNT;
724 break;
725 case ROOTCONTEXT_MNT:
726 rootcontext_sid = sid;
727
728 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
729 rootcontext_sid))
730 goto out_double_mount;
731
732 sbsec->flags |= ROOTCONTEXT_MNT;
733
734 break;
735 case DEFCONTEXT_MNT:
736 defcontext_sid = sid;
737
738 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
739 defcontext_sid))
740 goto out_double_mount;
741
742 sbsec->flags |= DEFCONTEXT_MNT;
743
744 break;
745 default:
746 rc = -EINVAL;
747 goto out;
748 }
749 }
750
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500751 if (sbsec->flags & SE_SBINITIALIZED) {
Eric Parisc9180a52007-11-30 13:00:35 -0500752 /* previously mounted with options, but not on this attempt? */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500753 if ((sbsec->flags & SE_MNTMASK) && !num_opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500754 goto out_double_mount;
755 rc = 0;
756 goto out;
757 }
758
James Morris089be432008-07-15 18:32:49 +1000759 if (strcmp(sb->s_type->name, "proc") == 0)
Stephen Smalley134509d2015-06-04 16:22:17 -0400760 sbsec->flags |= SE_SBPROC | SE_SBGENFS;
761
Stephen Smalley8e014722015-06-04 16:22:17 -0400762 if (!strcmp(sb->s_type->name, "debugfs") ||
Jeff Vander Stoep6a391182017-06-20 09:35:33 -0700763 !strcmp(sb->s_type->name, "tracefs") ||
Stephen Smalley8e014722015-06-04 16:22:17 -0400764 !strcmp(sb->s_type->name, "sysfs") ||
Antonio Murdaca901ef842017-02-09 17:02:42 +0100765 !strcmp(sb->s_type->name, "pstore") ||
766 !strcmp(sb->s_type->name, "cgroup") ||
767 !strcmp(sb->s_type->name, "cgroup2"))
Stephen Smalley134509d2015-06-04 16:22:17 -0400768 sbsec->flags |= SE_SBGENFS;
Eric Parisc9180a52007-11-30 13:00:35 -0500769
David Quigleyeb9ae682013-05-22 12:50:37 -0400770 if (!sbsec->behavior) {
771 /*
772 * Determine the labeling behavior to use for this
773 * filesystem type.
774 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500775 rc = security_fs_use(&selinux_state, sb);
David Quigleyeb9ae682013-05-22 12:50:37 -0400776 if (rc) {
peter enderborgc103a912018-06-12 10:09:03 +0200777 pr_warn("%s: security_fs_use(%s) returned %d\n",
David Quigleyeb9ae682013-05-22 12:50:37 -0400778 __func__, sb->s_type->name, rc);
779 goto out;
780 }
Eric Parisc9180a52007-11-30 13:00:35 -0500781 }
Seth Forsheeaad82892016-04-26 14:36:20 -0500782
783 /*
Stephen Smalley01593d32017-01-09 10:07:31 -0500784 * If this is a user namespace mount and the filesystem type is not
785 * explicitly whitelisted, then no contexts are allowed on the command
786 * line and security labels must be ignored.
Seth Forsheeaad82892016-04-26 14:36:20 -0500787 */
Stephen Smalley01593d32017-01-09 10:07:31 -0500788 if (sb->s_user_ns != &init_user_ns &&
789 strcmp(sb->s_type->name, "tmpfs") &&
790 strcmp(sb->s_type->name, "ramfs") &&
791 strcmp(sb->s_type->name, "devpts")) {
Seth Forsheeaad82892016-04-26 14:36:20 -0500792 if (context_sid || fscontext_sid || rootcontext_sid ||
793 defcontext_sid) {
794 rc = -EACCES;
795 goto out;
796 }
797 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
798 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500799 rc = security_transition_sid(&selinux_state,
800 current_sid(),
801 current_sid(),
Seth Forsheeaad82892016-04-26 14:36:20 -0500802 SECCLASS_FILE, NULL,
803 &sbsec->mntpoint_sid);
804 if (rc)
805 goto out;
806 }
807 goto out_set_opts;
808 }
809
Eric Parisc9180a52007-11-30 13:00:35 -0500810 /* sets the context of the superblock for the fs being mounted. */
811 if (fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100812 rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500813 if (rc)
814 goto out;
815
816 sbsec->sid = fscontext_sid;
817 }
818
819 /*
820 * Switch to using mount point labeling behavior.
821 * sets the label used on all file below the mountpoint, and will set
822 * the superblock context if not already set.
823 */
David Quigleyeb9ae682013-05-22 12:50:37 -0400824 if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !context_sid) {
825 sbsec->behavior = SECURITY_FS_USE_NATIVE;
826 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
827 }
828
Eric Parisc9180a52007-11-30 13:00:35 -0500829 if (context_sid) {
830 if (!fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100831 rc = may_context_mount_sb_relabel(context_sid, sbsec,
832 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500833 if (rc)
834 goto out;
835 sbsec->sid = context_sid;
836 } else {
David Howells275bb412008-11-14 10:39:19 +1100837 rc = may_context_mount_inode_relabel(context_sid, sbsec,
838 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500839 if (rc)
840 goto out;
841 }
842 if (!rootcontext_sid)
843 rootcontext_sid = context_sid;
844
845 sbsec->mntpoint_sid = context_sid;
846 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
847 }
848
849 if (rootcontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100850 rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec,
851 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500852 if (rc)
853 goto out;
854
855 root_isec->sid = rootcontext_sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -0500856 root_isec->initialized = LABEL_INITIALIZED;
Eric Parisc9180a52007-11-30 13:00:35 -0500857 }
858
859 if (defcontext_sid) {
David Quigleyeb9ae682013-05-22 12:50:37 -0400860 if (sbsec->behavior != SECURITY_FS_USE_XATTR &&
861 sbsec->behavior != SECURITY_FS_USE_NATIVE) {
Eric Parisc9180a52007-11-30 13:00:35 -0500862 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +0200863 pr_warn("SELinux: defcontext option is "
Eric Parisc9180a52007-11-30 13:00:35 -0500864 "invalid for this filesystem type\n");
865 goto out;
866 }
867
868 if (defcontext_sid != sbsec->def_sid) {
869 rc = may_context_mount_inode_relabel(defcontext_sid,
David Howells275bb412008-11-14 10:39:19 +1100870 sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500871 if (rc)
872 goto out;
873 }
874
875 sbsec->def_sid = defcontext_sid;
876 }
877
Seth Forsheeaad82892016-04-26 14:36:20 -0500878out_set_opts:
Eric Parisc9180a52007-11-30 13:00:35 -0500879 rc = sb_finish_set_opts(sb);
880out:
Eric Parisbc7e9822006-09-25 23:32:02 -0700881 mutex_unlock(&sbsec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 return rc;
Eric Parisc9180a52007-11-30 13:00:35 -0500883out_double_mount:
884 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +0200885 pr_warn("SELinux: mount invalid. Same superblock, different "
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800886 "security settings for (dev %s, type %s)\n", sb->s_id, name);
Eric Parisc9180a52007-11-30 13:00:35 -0500887 goto out;
888}
889
Jeff Layton094f7b62013-04-01 08:14:24 -0400890static int selinux_cmp_sb_context(const struct super_block *oldsb,
891 const struct super_block *newsb)
892{
893 struct superblock_security_struct *old = oldsb->s_security;
894 struct superblock_security_struct *new = newsb->s_security;
895 char oldflags = old->flags & SE_MNTMASK;
896 char newflags = new->flags & SE_MNTMASK;
897
898 if (oldflags != newflags)
899 goto mismatch;
900 if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid)
901 goto mismatch;
902 if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid)
903 goto mismatch;
904 if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid)
905 goto mismatch;
906 if (oldflags & ROOTCONTEXT_MNT) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500907 struct inode_security_struct *oldroot = backing_inode_security(oldsb->s_root);
908 struct inode_security_struct *newroot = backing_inode_security(newsb->s_root);
Jeff Layton094f7b62013-04-01 08:14:24 -0400909 if (oldroot->sid != newroot->sid)
910 goto mismatch;
911 }
912 return 0;
913mismatch:
peter enderborgc103a912018-06-12 10:09:03 +0200914 pr_warn("SELinux: mount invalid. Same superblock, "
Jeff Layton094f7b62013-04-01 08:14:24 -0400915 "different security settings for (dev %s, "
916 "type %s)\n", newsb->s_id, newsb->s_type->name);
917 return -EBUSY;
918}
919
920static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400921 struct super_block *newsb,
922 unsigned long kern_flags,
923 unsigned long *set_kern_flags)
Eric Parisc9180a52007-11-30 13:00:35 -0500924{
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400925 int rc = 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500926 const struct superblock_security_struct *oldsbsec = oldsb->s_security;
927 struct superblock_security_struct *newsbsec = newsb->s_security;
928
929 int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT);
930 int set_context = (oldsbsec->flags & CONTEXT_MNT);
931 int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT);
932
Eric Paris0f5e6422008-04-21 16:24:11 -0400933 /*
934 * if the parent was able to be mounted it clearly had no special lsm
Al Viroe8c26252010-03-23 06:36:54 -0400935 * mount options. thus we can safely deal with this superblock later
Eric Paris0f5e6422008-04-21 16:24:11 -0400936 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500937 if (!selinux_state.initialized)
Jeff Layton094f7b62013-04-01 08:14:24 -0400938 return 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500939
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400940 /*
941 * Specifying internal flags without providing a place to
942 * place the results is not allowed.
943 */
944 if (kern_flags && !set_kern_flags)
945 return -EINVAL;
946
Eric Parisc9180a52007-11-30 13:00:35 -0500947 /* how can we clone if the old one wasn't set up?? */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500948 BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED));
Eric Parisc9180a52007-11-30 13:00:35 -0500949
Jeff Layton094f7b62013-04-01 08:14:24 -0400950 /* if fs is reusing a sb, make sure that the contexts match */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500951 if (newsbsec->flags & SE_SBINITIALIZED)
Jeff Layton094f7b62013-04-01 08:14:24 -0400952 return selinux_cmp_sb_context(oldsb, newsb);
Eric Paris5a552612008-04-09 14:08:35 -0400953
Eric Parisc9180a52007-11-30 13:00:35 -0500954 mutex_lock(&newsbsec->lock);
955
956 newsbsec->flags = oldsbsec->flags;
957
958 newsbsec->sid = oldsbsec->sid;
959 newsbsec->def_sid = oldsbsec->def_sid;
960 newsbsec->behavior = oldsbsec->behavior;
961
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400962 if (newsbsec->behavior == SECURITY_FS_USE_NATIVE &&
963 !(kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500964 rc = security_fs_use(&selinux_state, newsb);
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400965 if (rc)
966 goto out;
967 }
968
969 if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !set_context) {
970 newsbsec->behavior = SECURITY_FS_USE_NATIVE;
971 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
972 }
973
Eric Parisc9180a52007-11-30 13:00:35 -0500974 if (set_context) {
975 u32 sid = oldsbsec->mntpoint_sid;
976
977 if (!set_fscontext)
978 newsbsec->sid = sid;
979 if (!set_rootcontext) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500980 struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
Eric Parisc9180a52007-11-30 13:00:35 -0500981 newisec->sid = sid;
982 }
983 newsbsec->mntpoint_sid = sid;
984 }
985 if (set_rootcontext) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500986 const struct inode_security_struct *oldisec = backing_inode_security(oldsb->s_root);
987 struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
Eric Parisc9180a52007-11-30 13:00:35 -0500988
989 newisec->sid = oldisec->sid;
990 }
991
992 sb_finish_set_opts(newsb);
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400993out:
Eric Parisc9180a52007-11-30 13:00:35 -0500994 mutex_unlock(&newsbsec->lock);
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400995 return rc;
Eric Parisc9180a52007-11-30 13:00:35 -0500996}
997
Adrian Bunk2e1479d2008-03-17 22:29:23 +0200998static int selinux_parse_opts_str(char *options,
Al Viro204cc0c2018-12-13 13:41:47 -0500999 void **mnt_opts)
Eric Parisc9180a52007-11-30 13:00:35 -05001000{
Eric Parise0007522008-03-05 10:31:54 -05001001 char *p;
Eric Parisc9180a52007-11-30 13:00:35 -05001002 char *context = NULL, *defcontext = NULL;
1003 char *fscontext = NULL, *rootcontext = NULL;
Eric Parise0007522008-03-05 10:31:54 -05001004 int rc, num_mnt_opts = 0;
Al Viro204cc0c2018-12-13 13:41:47 -05001005 struct security_mnt_opts *opts = *mnt_opts;
1006
1007 if (!opts) {
1008 opts = kzalloc(sizeof(struct security_mnt_opts), GFP_KERNEL);
1009 *mnt_opts = opts;
1010 if (!opts)
1011 return -ENOMEM;
1012 }
Eric Parisc9180a52007-11-30 13:00:35 -05001013
Eric Parise0007522008-03-05 10:31:54 -05001014 opts->num_mnt_opts = 0;
Eric Parisc9180a52007-11-30 13:00:35 -05001015
1016 /* Standard string-based options. */
1017 while ((p = strsep(&options, "|")) != NULL) {
1018 int token;
1019 substring_t args[MAX_OPT_ARGS];
1020
1021 if (!*p)
1022 continue;
1023
1024 token = match_token(p, tokens, args);
1025
1026 switch (token) {
1027 case Opt_context:
1028 if (context || defcontext) {
1029 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +02001030 pr_warn(SEL_MOUNT_FAIL_MSG);
Eric Parisc9180a52007-11-30 13:00:35 -05001031 goto out_err;
1032 }
1033 context = match_strdup(&args[0]);
1034 if (!context) {
1035 rc = -ENOMEM;
1036 goto out_err;
1037 }
1038 break;
1039
1040 case Opt_fscontext:
1041 if (fscontext) {
1042 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +02001043 pr_warn(SEL_MOUNT_FAIL_MSG);
Eric Parisc9180a52007-11-30 13:00:35 -05001044 goto out_err;
1045 }
1046 fscontext = match_strdup(&args[0]);
1047 if (!fscontext) {
1048 rc = -ENOMEM;
1049 goto out_err;
1050 }
1051 break;
1052
1053 case Opt_rootcontext:
1054 if (rootcontext) {
1055 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +02001056 pr_warn(SEL_MOUNT_FAIL_MSG);
Eric Parisc9180a52007-11-30 13:00:35 -05001057 goto out_err;
1058 }
1059 rootcontext = match_strdup(&args[0]);
1060 if (!rootcontext) {
1061 rc = -ENOMEM;
1062 goto out_err;
1063 }
1064 break;
1065
1066 case Opt_defcontext:
1067 if (context || defcontext) {
1068 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +02001069 pr_warn(SEL_MOUNT_FAIL_MSG);
Eric Parisc9180a52007-11-30 13:00:35 -05001070 goto out_err;
1071 }
1072 defcontext = match_strdup(&args[0]);
1073 if (!defcontext) {
1074 rc = -ENOMEM;
1075 goto out_err;
1076 }
1077 break;
David P. Quigley11689d42009-01-16 09:22:03 -05001078 case Opt_labelsupport:
1079 break;
Eric Parisc9180a52007-11-30 13:00:35 -05001080 default:
1081 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +02001082 pr_warn("SELinux: unknown mount option\n");
Eric Parisc9180a52007-11-30 13:00:35 -05001083 goto out_err;
1084
1085 }
1086 }
1087
Eric Parise0007522008-03-05 10:31:54 -05001088 rc = -ENOMEM;
Tetsuo Handa8931c3b2016-11-14 20:16:12 +09001089 opts->mnt_opts = kcalloc(NUM_SEL_MNT_OPTS, sizeof(char *), GFP_KERNEL);
Eric Parise0007522008-03-05 10:31:54 -05001090 if (!opts->mnt_opts)
1091 goto out_err;
1092
Tetsuo Handa8931c3b2016-11-14 20:16:12 +09001093 opts->mnt_opts_flags = kcalloc(NUM_SEL_MNT_OPTS, sizeof(int),
1094 GFP_KERNEL);
Paul Moore023f1082017-06-07 16:48:19 -04001095 if (!opts->mnt_opts_flags)
Eric Parise0007522008-03-05 10:31:54 -05001096 goto out_err;
Eric Parisc9180a52007-11-30 13:00:35 -05001097
Eric Parise0007522008-03-05 10:31:54 -05001098 if (fscontext) {
1099 opts->mnt_opts[num_mnt_opts] = fscontext;
1100 opts->mnt_opts_flags[num_mnt_opts++] = FSCONTEXT_MNT;
1101 }
1102 if (context) {
1103 opts->mnt_opts[num_mnt_opts] = context;
1104 opts->mnt_opts_flags[num_mnt_opts++] = CONTEXT_MNT;
1105 }
1106 if (rootcontext) {
1107 opts->mnt_opts[num_mnt_opts] = rootcontext;
1108 opts->mnt_opts_flags[num_mnt_opts++] = ROOTCONTEXT_MNT;
1109 }
1110 if (defcontext) {
1111 opts->mnt_opts[num_mnt_opts] = defcontext;
1112 opts->mnt_opts_flags[num_mnt_opts++] = DEFCONTEXT_MNT;
1113 }
1114
1115 opts->num_mnt_opts = num_mnt_opts;
1116 return 0;
1117
Eric Parisc9180a52007-11-30 13:00:35 -05001118out_err:
Al Viro204cc0c2018-12-13 13:41:47 -05001119 security_free_mnt_opts(mnt_opts);
Eric Parisc9180a52007-11-30 13:00:35 -05001120 kfree(context);
1121 kfree(defcontext);
1122 kfree(fscontext);
1123 kfree(rootcontext);
1124 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125}
1126
Al Viroe3489f82018-12-13 00:24:36 -05001127static int show_sid(struct seq_file *m, u32 sid)
Eric Paris2069f452008-07-04 09:47:13 +10001128{
Al Viroe3489f82018-12-13 00:24:36 -05001129 char *context = NULL;
1130 u32 len;
1131 int rc;
Eric Paris2069f452008-07-04 09:47:13 +10001132
Al Viroe3489f82018-12-13 00:24:36 -05001133 rc = security_sid_to_context(&selinux_state, sid,
1134 &context, &len);
1135 if (!rc) {
1136 bool has_comma = context && strchr(context, ',');
David P. Quigley11689d42009-01-16 09:22:03 -05001137
Eric Paris2069f452008-07-04 09:47:13 +10001138 if (has_comma)
1139 seq_putc(m, '\"');
Al Viroe3489f82018-12-13 00:24:36 -05001140 seq_escape(m, context, "\"\n\\");
Eric Paris2069f452008-07-04 09:47:13 +10001141 if (has_comma)
1142 seq_putc(m, '\"');
1143 }
Al Viroe3489f82018-12-13 00:24:36 -05001144 kfree(context);
1145 return rc;
Eric Paris2069f452008-07-04 09:47:13 +10001146}
1147
1148static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
1149{
Al Viroe3489f82018-12-13 00:24:36 -05001150 struct superblock_security_struct *sbsec = sb->s_security;
Eric Paris2069f452008-07-04 09:47:13 +10001151 int rc;
1152
Al Viroe3489f82018-12-13 00:24:36 -05001153 if (!(sbsec->flags & SE_SBINITIALIZED))
1154 return 0;
1155
1156 if (!selinux_state.initialized)
1157 return 0;
1158
1159 if (sbsec->flags & FSCONTEXT_MNT) {
1160 seq_putc(m, ',');
1161 seq_puts(m, FSCONTEXT_STR);
1162 rc = show_sid(m, sbsec->sid);
1163 if (rc)
1164 return rc;
Eric Paris383795c2008-07-29 17:07:26 -04001165 }
Al Viroe3489f82018-12-13 00:24:36 -05001166 if (sbsec->flags & CONTEXT_MNT) {
1167 seq_putc(m, ',');
1168 seq_puts(m, CONTEXT_STR);
1169 rc = show_sid(m, sbsec->mntpoint_sid);
1170 if (rc)
1171 return rc;
1172 }
1173 if (sbsec->flags & DEFCONTEXT_MNT) {
1174 seq_putc(m, ',');
1175 seq_puts(m, DEFCONTEXT_STR);
1176 rc = show_sid(m, sbsec->def_sid);
1177 if (rc)
1178 return rc;
1179 }
1180 if (sbsec->flags & ROOTCONTEXT_MNT) {
1181 struct dentry *root = sbsec->sb->s_root;
1182 struct inode_security_struct *isec = backing_inode_security(root);
1183 seq_putc(m, ',');
1184 seq_puts(m, ROOTCONTEXT_STR);
1185 rc = show_sid(m, isec->sid);
1186 if (rc)
1187 return rc;
1188 }
1189 if (sbsec->flags & SBLABEL_MNT) {
1190 seq_putc(m, ',');
1191 seq_puts(m, LABELSUPP_STR);
1192 }
1193 return 0;
Eric Paris2069f452008-07-04 09:47:13 +10001194}
1195
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196static inline u16 inode_mode_to_security_class(umode_t mode)
1197{
1198 switch (mode & S_IFMT) {
1199 case S_IFSOCK:
1200 return SECCLASS_SOCK_FILE;
1201 case S_IFLNK:
1202 return SECCLASS_LNK_FILE;
1203 case S_IFREG:
1204 return SECCLASS_FILE;
1205 case S_IFBLK:
1206 return SECCLASS_BLK_FILE;
1207 case S_IFDIR:
1208 return SECCLASS_DIR;
1209 case S_IFCHR:
1210 return SECCLASS_CHR_FILE;
1211 case S_IFIFO:
1212 return SECCLASS_FIFO_FILE;
1213
1214 }
1215
1216 return SECCLASS_FILE;
1217}
1218
James Morris13402582005-09-30 14:24:34 -04001219static inline int default_protocol_stream(int protocol)
1220{
1221 return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP);
1222}
1223
1224static inline int default_protocol_dgram(int protocol)
1225{
1226 return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
1227}
1228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229static inline u16 socket_type_to_security_class(int family, int type, int protocol)
1230{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001231 int extsockclass = selinux_policycap_extsockclass();
Stephen Smalleyda69a532017-01-09 10:07:30 -05001232
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 switch (family) {
1234 case PF_UNIX:
1235 switch (type) {
1236 case SOCK_STREAM:
1237 case SOCK_SEQPACKET:
1238 return SECCLASS_UNIX_STREAM_SOCKET;
1239 case SOCK_DGRAM:
Luis Ressel2a764b52017-07-25 15:13:41 -04001240 case SOCK_RAW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 return SECCLASS_UNIX_DGRAM_SOCKET;
1242 }
1243 break;
1244 case PF_INET:
1245 case PF_INET6:
1246 switch (type) {
1247 case SOCK_STREAM:
Stephen Smalleyda69a532017-01-09 10:07:30 -05001248 case SOCK_SEQPACKET:
James Morris13402582005-09-30 14:24:34 -04001249 if (default_protocol_stream(protocol))
1250 return SECCLASS_TCP_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001251 else if (extsockclass && protocol == IPPROTO_SCTP)
1252 return SECCLASS_SCTP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001253 else
1254 return SECCLASS_RAWIP_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 case SOCK_DGRAM:
James Morris13402582005-09-30 14:24:34 -04001256 if (default_protocol_dgram(protocol))
1257 return SECCLASS_UDP_SOCKET;
Stephen Smalleyef379792017-01-09 10:07:31 -05001258 else if (extsockclass && (protocol == IPPROTO_ICMP ||
1259 protocol == IPPROTO_ICMPV6))
Stephen Smalleyda69a532017-01-09 10:07:30 -05001260 return SECCLASS_ICMP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001261 else
1262 return SECCLASS_RAWIP_SOCKET;
James Morris2ee92d42006-11-13 16:09:01 -08001263 case SOCK_DCCP:
1264 return SECCLASS_DCCP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001265 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 return SECCLASS_RAWIP_SOCKET;
1267 }
1268 break;
1269 case PF_NETLINK:
1270 switch (protocol) {
1271 case NETLINK_ROUTE:
1272 return SECCLASS_NETLINK_ROUTE_SOCKET;
Pavel Emelyanov7f1fb602011-12-06 07:56:43 +00001273 case NETLINK_SOCK_DIAG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 return SECCLASS_NETLINK_TCPDIAG_SOCKET;
1275 case NETLINK_NFLOG:
1276 return SECCLASS_NETLINK_NFLOG_SOCKET;
1277 case NETLINK_XFRM:
1278 return SECCLASS_NETLINK_XFRM_SOCKET;
1279 case NETLINK_SELINUX:
1280 return SECCLASS_NETLINK_SELINUX_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001281 case NETLINK_ISCSI:
1282 return SECCLASS_NETLINK_ISCSI_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 case NETLINK_AUDIT:
1284 return SECCLASS_NETLINK_AUDIT_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001285 case NETLINK_FIB_LOOKUP:
1286 return SECCLASS_NETLINK_FIB_LOOKUP_SOCKET;
1287 case NETLINK_CONNECTOR:
1288 return SECCLASS_NETLINK_CONNECTOR_SOCKET;
1289 case NETLINK_NETFILTER:
1290 return SECCLASS_NETLINK_NETFILTER_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 case NETLINK_DNRTMSG:
1292 return SECCLASS_NETLINK_DNRT_SOCKET;
James Morris0c9b7942005-04-16 15:24:13 -07001293 case NETLINK_KOBJECT_UEVENT:
1294 return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001295 case NETLINK_GENERIC:
1296 return SECCLASS_NETLINK_GENERIC_SOCKET;
1297 case NETLINK_SCSITRANSPORT:
1298 return SECCLASS_NETLINK_SCSITRANSPORT_SOCKET;
1299 case NETLINK_RDMA:
1300 return SECCLASS_NETLINK_RDMA_SOCKET;
1301 case NETLINK_CRYPTO:
1302 return SECCLASS_NETLINK_CRYPTO_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 default:
1304 return SECCLASS_NETLINK_SOCKET;
1305 }
1306 case PF_PACKET:
1307 return SECCLASS_PACKET_SOCKET;
1308 case PF_KEY:
1309 return SECCLASS_KEY_SOCKET;
Christopher J. PeBenito3e3ff152006-06-09 00:25:03 -07001310 case PF_APPLETALK:
1311 return SECCLASS_APPLETALK_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 }
1313
Stephen Smalleyda69a532017-01-09 10:07:30 -05001314 if (extsockclass) {
1315 switch (family) {
1316 case PF_AX25:
1317 return SECCLASS_AX25_SOCKET;
1318 case PF_IPX:
1319 return SECCLASS_IPX_SOCKET;
1320 case PF_NETROM:
1321 return SECCLASS_NETROM_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001322 case PF_ATMPVC:
1323 return SECCLASS_ATMPVC_SOCKET;
1324 case PF_X25:
1325 return SECCLASS_X25_SOCKET;
1326 case PF_ROSE:
1327 return SECCLASS_ROSE_SOCKET;
1328 case PF_DECnet:
1329 return SECCLASS_DECNET_SOCKET;
1330 case PF_ATMSVC:
1331 return SECCLASS_ATMSVC_SOCKET;
1332 case PF_RDS:
1333 return SECCLASS_RDS_SOCKET;
1334 case PF_IRDA:
1335 return SECCLASS_IRDA_SOCKET;
1336 case PF_PPPOX:
1337 return SECCLASS_PPPOX_SOCKET;
1338 case PF_LLC:
1339 return SECCLASS_LLC_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001340 case PF_CAN:
1341 return SECCLASS_CAN_SOCKET;
1342 case PF_TIPC:
1343 return SECCLASS_TIPC_SOCKET;
1344 case PF_BLUETOOTH:
1345 return SECCLASS_BLUETOOTH_SOCKET;
1346 case PF_IUCV:
1347 return SECCLASS_IUCV_SOCKET;
1348 case PF_RXRPC:
1349 return SECCLASS_RXRPC_SOCKET;
1350 case PF_ISDN:
1351 return SECCLASS_ISDN_SOCKET;
1352 case PF_PHONET:
1353 return SECCLASS_PHONET_SOCKET;
1354 case PF_IEEE802154:
1355 return SECCLASS_IEEE802154_SOCKET;
1356 case PF_CAIF:
1357 return SECCLASS_CAIF_SOCKET;
1358 case PF_ALG:
1359 return SECCLASS_ALG_SOCKET;
1360 case PF_NFC:
1361 return SECCLASS_NFC_SOCKET;
1362 case PF_VSOCK:
1363 return SECCLASS_VSOCK_SOCKET;
1364 case PF_KCM:
1365 return SECCLASS_KCM_SOCKET;
1366 case PF_QIPCRTR:
1367 return SECCLASS_QIPCRTR_SOCKET;
Linus Torvalds3051bf32017-02-22 10:15:09 -08001368 case PF_SMC:
1369 return SECCLASS_SMC_SOCKET;
Björn Töpel68e8b842018-05-02 13:01:22 +02001370 case PF_XDP:
1371 return SECCLASS_XDP_SOCKET;
1372#if PF_MAX > 45
Stephen Smalleyda69a532017-01-09 10:07:30 -05001373#error New address family defined, please update this function.
1374#endif
1375 }
1376 }
1377
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 return SECCLASS_SOCKET;
1379}
1380
Stephen Smalley134509d2015-06-04 16:22:17 -04001381static int selinux_genfs_get_sid(struct dentry *dentry,
1382 u16 tclass,
1383 u16 flags,
1384 u32 *sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385{
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001386 int rc;
Al Virofc640052016-04-10 01:33:30 -04001387 struct super_block *sb = dentry->d_sb;
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001388 char *buffer, *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
Eric Paris828dfe12008-04-17 13:17:49 -04001390 buffer = (char *)__get_free_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 if (!buffer)
1392 return -ENOMEM;
1393
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001394 path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
1395 if (IS_ERR(path))
1396 rc = PTR_ERR(path);
1397 else {
Stephen Smalley134509d2015-06-04 16:22:17 -04001398 if (flags & SE_SBPROC) {
1399 /* each process gets a /proc/PID/ entry. Strip off the
1400 * PID part to get a valid selinux labeling.
1401 * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
1402 while (path[1] >= '0' && path[1] <= '9') {
1403 path[1] = '/';
1404 path++;
1405 }
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001406 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001407 rc = security_genfs_sid(&selinux_state, sb->s_type->name,
1408 path, tclass, sid);
Stephen Smalley7bb185e2018-09-04 16:51:36 -04001409 if (rc == -ENOENT) {
1410 /* No match in policy, mark as unlabeled. */
1411 *sid = SECINITSID_UNLABELED;
1412 rc = 0;
1413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 free_page((unsigned long)buffer);
1416 return rc;
1417}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
1419/* The inode's security attributes must be initialized before first use. */
1420static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
1421{
1422 struct superblock_security_struct *sbsec = NULL;
1423 struct inode_security_struct *isec = inode->i_security;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001424 u32 task_sid, sid = 0;
1425 u16 sclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 struct dentry *dentry;
1427#define INITCONTEXTLEN 255
1428 char *context = NULL;
1429 unsigned len = 0;
1430 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05001432 if (isec->initialized == LABEL_INITIALIZED)
Andreas Gruenbacher13457d02016-11-10 22:18:29 +01001433 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001435 spin_lock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05001436 if (isec->initialized == LABEL_INITIALIZED)
Eric Paris23970742006-09-25 23:32:01 -07001437 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Andreas Gruenbacher13457d02016-11-10 22:18:29 +01001439 if (isec->sclass == SECCLASS_FILE)
1440 isec->sclass = inode_mode_to_security_class(inode->i_mode);
1441
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 sbsec = inode->i_sb->s_security;
David P. Quigley0d90a7e2009-01-16 09:22:02 -05001443 if (!(sbsec->flags & SE_SBINITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 /* Defer initialization until selinux_complete_init,
1445 after the initial policy is loaded and the security
1446 server is ready to handle calls. */
1447 spin_lock(&sbsec->isec_lock);
1448 if (list_empty(&isec->list))
1449 list_add(&isec->list, &sbsec->isec_head);
1450 spin_unlock(&sbsec->isec_lock);
Eric Paris23970742006-09-25 23:32:01 -07001451 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 }
1453
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001454 sclass = isec->sclass;
1455 task_sid = isec->task_sid;
1456 sid = isec->sid;
1457 isec->initialized = LABEL_PENDING;
1458 spin_unlock(&isec->lock);
1459
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 switch (sbsec->behavior) {
David Quigleyeb9ae682013-05-22 12:50:37 -04001461 case SECURITY_FS_USE_NATIVE:
1462 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 case SECURITY_FS_USE_XATTR:
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001464 if (!(inode->i_opflags & IOP_XATTR)) {
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001465 sid = sbsec->def_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 break;
1467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 /* Need a dentry, since the xattr API requires one.
1469 Life would be simpler if we could just pass the inode. */
1470 if (opt_dentry) {
1471 /* Called from d_instantiate or d_splice_alias. */
1472 dentry = dget(opt_dentry);
1473 } else {
Al Virob1271252018-04-25 10:28:38 -04001474 /*
1475 * Called from selinux_complete_init, try to find a dentry.
1476 * Some filesystems really want a connected one, so try
1477 * that first. We could split SECURITY_FS_USE_XATTR in
1478 * two, depending upon that...
1479 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 dentry = d_find_alias(inode);
Al Virob1271252018-04-25 10:28:38 -04001481 if (!dentry)
1482 dentry = d_find_any_alias(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 }
1484 if (!dentry) {
Eric Parisdf7f54c2009-03-09 14:35:58 -04001485 /*
1486 * this is can be hit on boot when a file is accessed
1487 * before the policy is loaded. When we load policy we
1488 * may find inodes that have no dentry on the
1489 * sbsec->isec_head list. No reason to complain as these
1490 * will get fixed up the next time we go through
1491 * inode_doinit with a dentry, before these inodes could
1492 * be used again by userspace.
1493 */
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001494 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 }
1496
1497 len = INITCONTEXTLEN;
Eric Paris4cb912f2009-02-12 14:50:05 -05001498 context = kmalloc(len+1, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 if (!context) {
1500 rc = -ENOMEM;
1501 dput(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001502 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 }
Eric Paris4cb912f2009-02-12 14:50:05 -05001504 context[len] = '\0';
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001505 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 if (rc == -ERANGE) {
James Morris314dabb2009-08-10 22:00:13 +10001507 kfree(context);
1508
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 /* Need a larger buffer. Query for the right size. */
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001510 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 if (rc < 0) {
1512 dput(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001513 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 len = rc;
Eric Paris4cb912f2009-02-12 14:50:05 -05001516 context = kmalloc(len+1, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 if (!context) {
1518 rc = -ENOMEM;
1519 dput(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001520 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 }
Eric Paris4cb912f2009-02-12 14:50:05 -05001522 context[len] = '\0';
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001523 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 }
1525 dput(dentry);
1526 if (rc < 0) {
1527 if (rc != -ENODATA) {
peter enderborgc103a912018-06-12 10:09:03 +02001528 pr_warn("SELinux: %s: getxattr returned "
Harvey Harrisondd6f9532008-03-06 10:03:59 +11001529 "%d for dev=%s ino=%ld\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 -rc, inode->i_sb->s_id, inode->i_ino);
1531 kfree(context);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001532 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 }
1534 /* Map ENODATA to the default file SID */
1535 sid = sbsec->def_sid;
1536 rc = 0;
1537 } else {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001538 rc = security_context_to_sid_default(&selinux_state,
1539 context, rc, &sid,
Stephen Smalley869ab512008-04-04 08:46:05 -04001540 sbsec->def_sid,
1541 GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 if (rc) {
Eric Paris4ba0a8a2009-02-12 15:01:10 -05001543 char *dev = inode->i_sb->s_id;
1544 unsigned long ino = inode->i_ino;
1545
1546 if (rc == -EINVAL) {
1547 if (printk_ratelimit())
peter enderborgc103a912018-06-12 10:09:03 +02001548 pr_notice("SELinux: inode=%lu on dev=%s was found to have an invalid "
Eric Paris4ba0a8a2009-02-12 15:01:10 -05001549 "context=%s. This indicates you may need to relabel the inode or the "
1550 "filesystem in question.\n", ino, dev, context);
1551 } else {
peter enderborgc103a912018-06-12 10:09:03 +02001552 pr_warn("SELinux: %s: context_to_sid(%s) "
Eric Paris4ba0a8a2009-02-12 15:01:10 -05001553 "returned %d for dev=%s ino=%ld\n",
1554 __func__, context, -rc, dev, ino);
1555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 kfree(context);
1557 /* Leave with the unlabeled SID */
1558 rc = 0;
1559 break;
1560 }
1561 }
1562 kfree(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 break;
1564 case SECURITY_FS_USE_TASK:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001565 sid = task_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 break;
1567 case SECURITY_FS_USE_TRANS:
1568 /* Default to the fs SID. */
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001569 sid = sbsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
1571 /* Try to obtain a transition SID. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001572 rc = security_transition_sid(&selinux_state, task_sid, sid,
1573 sclass, NULL, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 if (rc)
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001575 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 break;
Eric Parisc312feb2006-07-10 04:43:53 -07001577 case SECURITY_FS_USE_MNTPOINT:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001578 sid = sbsec->mntpoint_sid;
Eric Parisc312feb2006-07-10 04:43:53 -07001579 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 default:
Eric Parisc312feb2006-07-10 04:43:53 -07001581 /* Default to the fs superblock SID. */
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001582 sid = sbsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
Stephen Smalley134509d2015-06-04 16:22:17 -04001584 if ((sbsec->flags & SE_SBGENFS) && !S_ISLNK(inode->i_mode)) {
Paul Mooref64410e2014-03-19 16:46:18 -04001585 /* We must have a dentry to determine the label on
1586 * procfs inodes */
Al Virob1271252018-04-25 10:28:38 -04001587 if (opt_dentry) {
Paul Mooref64410e2014-03-19 16:46:18 -04001588 /* Called from d_instantiate or
1589 * d_splice_alias. */
1590 dentry = dget(opt_dentry);
Al Virob1271252018-04-25 10:28:38 -04001591 } else {
Paul Mooref64410e2014-03-19 16:46:18 -04001592 /* Called from selinux_complete_init, try to
Al Virob1271252018-04-25 10:28:38 -04001593 * find a dentry. Some filesystems really want
1594 * a connected one, so try that first.
1595 */
Paul Mooref64410e2014-03-19 16:46:18 -04001596 dentry = d_find_alias(inode);
Al Virob1271252018-04-25 10:28:38 -04001597 if (!dentry)
1598 dentry = d_find_any_alias(inode);
1599 }
Paul Mooref64410e2014-03-19 16:46:18 -04001600 /*
1601 * This can be hit on boot when a file is accessed
1602 * before the policy is loaded. When we load policy we
1603 * may find inodes that have no dentry on the
1604 * sbsec->isec_head list. No reason to complain as
1605 * these will get fixed up the next time we go through
1606 * inode_doinit() with a dentry, before these inodes
1607 * could be used again by userspace.
1608 */
1609 if (!dentry)
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001610 goto out;
1611 rc = selinux_genfs_get_sid(dentry, sclass,
Stephen Smalley134509d2015-06-04 16:22:17 -04001612 sbsec->flags, &sid);
Paul Mooref64410e2014-03-19 16:46:18 -04001613 dput(dentry);
1614 if (rc)
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001615 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 }
1617 break;
1618 }
1619
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001620out:
1621 spin_lock(&isec->lock);
1622 if (isec->initialized == LABEL_PENDING) {
1623 if (!sid || rc) {
1624 isec->initialized = LABEL_INVALID;
1625 goto out_unlock;
1626 }
1627
1628 isec->initialized = LABEL_INITIALIZED;
1629 isec->sid = sid;
1630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
Eric Paris23970742006-09-25 23:32:01 -07001632out_unlock:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001633 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 return rc;
1635}
1636
1637/* Convert a Linux signal to an access vector. */
1638static inline u32 signal_to_av(int sig)
1639{
1640 u32 perm = 0;
1641
1642 switch (sig) {
1643 case SIGCHLD:
1644 /* Commonly granted from child to parent. */
1645 perm = PROCESS__SIGCHLD;
1646 break;
1647 case SIGKILL:
1648 /* Cannot be caught or ignored */
1649 perm = PROCESS__SIGKILL;
1650 break;
1651 case SIGSTOP:
1652 /* Cannot be caught or ignored */
1653 perm = PROCESS__SIGSTOP;
1654 break;
1655 default:
1656 /* All other signals. */
1657 perm = PROCESS__SIGNAL;
1658 break;
1659 }
1660
1661 return perm;
1662}
1663
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001664#if CAP_LAST_CAP > 63
1665#error Fix SELinux to handle capabilities > 63.
1666#endif
1667
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668/* Check whether a task is allowed to use a capability. */
Eric Paris6a9de492012-01-03 12:25:14 -05001669static int cred_has_capability(const struct cred *cred,
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04001670 int cap, int audit, bool initns)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671{
Thomas Liu2bf49692009-07-14 12:14:09 -04001672 struct common_audit_data ad;
Eric Paris06112162008-11-11 22:02:50 +11001673 struct av_decision avd;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001674 u16 sclass;
David Howells3699c532009-01-06 22:27:01 +00001675 u32 sid = cred_sid(cred);
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001676 u32 av = CAP_TO_MASK(cap);
Eric Paris06112162008-11-11 22:02:50 +11001677 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
Eric Paris50c205f2012-04-04 15:01:43 -04001679 ad.type = LSM_AUDIT_DATA_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 ad.u.cap = cap;
1681
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001682 switch (CAP_TO_INDEX(cap)) {
1683 case 0:
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04001684 sclass = initns ? SECCLASS_CAPABILITY : SECCLASS_CAP_USERNS;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001685 break;
1686 case 1:
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04001687 sclass = initns ? SECCLASS_CAPABILITY2 : SECCLASS_CAP2_USERNS;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001688 break;
1689 default:
peter enderborgc103a912018-06-12 10:09:03 +02001690 pr_err("SELinux: out of range capability %d\n", cap);
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001691 BUG();
Eric Parisa35c6c832011-04-20 10:21:28 -04001692 return -EINVAL;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001693 }
Eric Paris06112162008-11-11 22:02:50 +11001694
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001695 rc = avc_has_perm_noaudit(&selinux_state,
1696 sid, sid, sclass, av, 0, &avd);
Eric Paris9ade0cf2011-04-25 16:26:29 -04001697 if (audit == SECURITY_CAP_AUDIT) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001698 int rc2 = avc_audit(&selinux_state,
1699 sid, sid, sclass, av, &avd, rc, &ad, 0);
Eric Paris9ade0cf2011-04-25 16:26:29 -04001700 if (rc2)
1701 return rc2;
1702 }
Eric Paris06112162008-11-11 22:02:50 +11001703 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704}
1705
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706/* Check whether a task has a particular permission to an inode.
1707 The 'adp' parameter is optional and allows other audit
1708 data to be passed (e.g. the dentry). */
David Howells88e67f32008-11-14 10:39:21 +11001709static int inode_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 struct inode *inode,
1711 u32 perms,
Linus Torvalds19e49832013-10-04 12:54:11 -07001712 struct common_audit_data *adp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 struct inode_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +11001715 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
David Howellse0e81732009-09-02 09:13:40 +01001717 validate_creds(cred);
1718
Eric Paris828dfe12008-04-17 13:17:49 -04001719 if (unlikely(IS_PRIVATE(inode)))
Stephen Smalleybbaca6c2007-02-14 00:34:16 -08001720 return 0;
1721
David Howells88e67f32008-11-14 10:39:21 +11001722 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 isec = inode->i_security;
1724
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001725 return avc_has_perm(&selinux_state,
1726 sid, isec->sid, isec->sclass, perms, adp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727}
1728
1729/* Same as inode_has_perm, but pass explicit audit data containing
1730 the dentry to help the auditing code to more easily generate the
1731 pathname if needed. */
David Howells88e67f32008-11-14 10:39:21 +11001732static inline int dentry_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 struct dentry *dentry,
1734 u32 av)
1735{
David Howellsc6f493d2015-03-17 22:26:22 +00001736 struct inode *inode = d_backing_inode(dentry);
Thomas Liu2bf49692009-07-14 12:14:09 -04001737 struct common_audit_data ad;
David Howells88e67f32008-11-14 10:39:21 +11001738
Eric Paris50c205f2012-04-04 15:01:43 -04001739 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Paris2875fa02011-04-28 16:04:24 -04001740 ad.u.dentry = dentry;
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05001741 __inode_security_revalidate(inode, dentry, true);
Linus Torvalds19e49832013-10-04 12:54:11 -07001742 return inode_has_perm(cred, inode, av, &ad);
Eric Paris2875fa02011-04-28 16:04:24 -04001743}
1744
1745/* Same as inode_has_perm, but pass explicit audit data containing
1746 the path to help the auditing code to more easily generate the
1747 pathname if needed. */
1748static inline int path_has_perm(const struct cred *cred,
Al Viro3f7036a2015-03-08 19:28:30 -04001749 const struct path *path,
Eric Paris2875fa02011-04-28 16:04:24 -04001750 u32 av)
1751{
David Howellsc6f493d2015-03-17 22:26:22 +00001752 struct inode *inode = d_backing_inode(path->dentry);
Eric Paris2875fa02011-04-28 16:04:24 -04001753 struct common_audit_data ad;
1754
Eric Paris50c205f2012-04-04 15:01:43 -04001755 ad.type = LSM_AUDIT_DATA_PATH;
Eric Paris2875fa02011-04-28 16:04:24 -04001756 ad.u.path = *path;
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05001757 __inode_security_revalidate(inode, path->dentry, true);
Linus Torvalds19e49832013-10-04 12:54:11 -07001758 return inode_has_perm(cred, inode, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759}
1760
David Howells13f8e982013-06-13 23:37:55 +01001761/* Same as path_has_perm, but uses the inode from the file struct. */
1762static inline int file_path_has_perm(const struct cred *cred,
1763 struct file *file,
1764 u32 av)
1765{
1766 struct common_audit_data ad;
1767
Vivek Goyal43af5de2016-09-09 11:37:49 -04001768 ad.type = LSM_AUDIT_DATA_FILE;
1769 ad.u.file = file;
Linus Torvalds19e49832013-10-04 12:54:11 -07001770 return inode_has_perm(cred, file_inode(file), av, &ad);
David Howells13f8e982013-06-13 23:37:55 +01001771}
1772
Chenbo Fengf66e4482017-10-18 13:00:26 -07001773#ifdef CONFIG_BPF_SYSCALL
1774static int bpf_fd_pass(struct file *file, u32 sid);
1775#endif
1776
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777/* Check whether a task can use an open file descriptor to
1778 access an inode in a given way. Check access to the
1779 descriptor itself, and then use dentry_has_perm to
1780 check a particular permission to the file.
1781 Access to the descriptor is implicitly granted if it
1782 has the same SID as the process. If av is zero, then
1783 access to the file is not checked, e.g. for cases
1784 where only the descriptor is affected like seek. */
David Howells88e67f32008-11-14 10:39:21 +11001785static int file_has_perm(const struct cred *cred,
1786 struct file *file,
1787 u32 av)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 struct file_security_struct *fsec = file->f_security;
Al Viro496ad9a2013-01-23 17:07:38 -05001790 struct inode *inode = file_inode(file);
Thomas Liu2bf49692009-07-14 12:14:09 -04001791 struct common_audit_data ad;
David Howells88e67f32008-11-14 10:39:21 +11001792 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 int rc;
1794
Vivek Goyal43af5de2016-09-09 11:37:49 -04001795 ad.type = LSM_AUDIT_DATA_FILE;
1796 ad.u.file = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
David Howells275bb412008-11-14 10:39:19 +11001798 if (sid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001799 rc = avc_has_perm(&selinux_state,
1800 sid, fsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 SECCLASS_FD,
1802 FD__USE,
1803 &ad);
1804 if (rc)
David Howells88e67f32008-11-14 10:39:21 +11001805 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 }
1807
Chenbo Fengf66e4482017-10-18 13:00:26 -07001808#ifdef CONFIG_BPF_SYSCALL
1809 rc = bpf_fd_pass(file, cred_sid(cred));
1810 if (rc)
1811 return rc;
1812#endif
1813
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 /* av is zero if only checking access to the descriptor. */
David Howells88e67f32008-11-14 10:39:21 +11001815 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 if (av)
Linus Torvalds19e49832013-10-04 12:54:11 -07001817 rc = inode_has_perm(cred, inode, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
David Howells88e67f32008-11-14 10:39:21 +11001819out:
1820 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821}
1822
David Howellsc3c188b2015-07-10 17:19:58 -04001823/*
1824 * Determine the label for an inode that might be unioned.
1825 */
Vivek Goyalc957f6d2016-07-13 10:44:51 -04001826static int
1827selinux_determine_inode_label(const struct task_security_struct *tsec,
1828 struct inode *dir,
1829 const struct qstr *name, u16 tclass,
1830 u32 *_new_isid)
David Howellsc3c188b2015-07-10 17:19:58 -04001831{
1832 const struct superblock_security_struct *sbsec = dir->i_sb->s_security;
David Howellsc3c188b2015-07-10 17:19:58 -04001833
1834 if ((sbsec->flags & SE_SBINITIALIZED) &&
1835 (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)) {
1836 *_new_isid = sbsec->mntpoint_sid;
1837 } else if ((sbsec->flags & SBLABEL_MNT) &&
1838 tsec->create_sid) {
1839 *_new_isid = tsec->create_sid;
1840 } else {
Paul Moore20cdef82016-04-04 14:14:42 -04001841 const struct inode_security_struct *dsec = inode_security(dir);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001842 return security_transition_sid(&selinux_state, tsec->sid,
1843 dsec->sid, tclass,
David Howellsc3c188b2015-07-10 17:19:58 -04001844 name, _new_isid);
1845 }
1846
1847 return 0;
1848}
1849
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850/* Check whether a task can create a file. */
1851static int may_create(struct inode *dir,
1852 struct dentry *dentry,
1853 u16 tclass)
1854{
Paul Moore5fb49872010-04-22 14:46:19 -04001855 const struct task_security_struct *tsec = current_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 struct inode_security_struct *dsec;
1857 struct superblock_security_struct *sbsec;
David Howells275bb412008-11-14 10:39:19 +11001858 u32 sid, newsid;
Thomas Liu2bf49692009-07-14 12:14:09 -04001859 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 int rc;
1861
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001862 dsec = inode_security(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 sbsec = dir->i_sb->s_security;
1864
David Howells275bb412008-11-14 10:39:19 +11001865 sid = tsec->sid;
David Howells275bb412008-11-14 10:39:19 +11001866
Eric Paris50c205f2012-04-04 15:01:43 -04001867 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04001868 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001870 rc = avc_has_perm(&selinux_state,
1871 sid, dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 DIR__ADD_NAME | DIR__SEARCH,
1873 &ad);
1874 if (rc)
1875 return rc;
1876
Vivek Goyalc957f6d2016-07-13 10:44:51 -04001877 rc = selinux_determine_inode_label(current_security(), dir,
1878 &dentry->d_name, tclass, &newsid);
David Howellsc3c188b2015-07-10 17:19:58 -04001879 if (rc)
1880 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001882 rc = avc_has_perm(&selinux_state,
1883 sid, newsid, tclass, FILE__CREATE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 if (rc)
1885 return rc;
1886
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001887 return avc_has_perm(&selinux_state,
1888 newsid, sbsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 SECCLASS_FILESYSTEM,
1890 FILESYSTEM__ASSOCIATE, &ad);
1891}
1892
Eric Paris828dfe12008-04-17 13:17:49 -04001893#define MAY_LINK 0
1894#define MAY_UNLINK 1
1895#define MAY_RMDIR 2
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
1897/* Check whether a task can link, unlink, or rmdir a file/directory. */
1898static int may_link(struct inode *dir,
1899 struct dentry *dentry,
1900 int kind)
1901
1902{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 struct inode_security_struct *dsec, *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001904 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11001905 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 u32 av;
1907 int rc;
1908
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001909 dsec = inode_security(dir);
1910 isec = backing_inode_security(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
Eric Paris50c205f2012-04-04 15:01:43 -04001912 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04001913 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
1915 av = DIR__SEARCH;
1916 av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001917 rc = avc_has_perm(&selinux_state,
1918 sid, dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 if (rc)
1920 return rc;
1921
1922 switch (kind) {
1923 case MAY_LINK:
1924 av = FILE__LINK;
1925 break;
1926 case MAY_UNLINK:
1927 av = FILE__UNLINK;
1928 break;
1929 case MAY_RMDIR:
1930 av = DIR__RMDIR;
1931 break;
1932 default:
peter enderborgc103a912018-06-12 10:09:03 +02001933 pr_warn("SELinux: %s: unrecognized kind %d\n",
Eric Paris744ba352008-04-17 11:52:44 -04001934 __func__, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 return 0;
1936 }
1937
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001938 rc = avc_has_perm(&selinux_state,
1939 sid, isec->sid, isec->sclass, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 return rc;
1941}
1942
1943static inline int may_rename(struct inode *old_dir,
1944 struct dentry *old_dentry,
1945 struct inode *new_dir,
1946 struct dentry *new_dentry)
1947{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001949 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11001950 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 u32 av;
1952 int old_is_dir, new_is_dir;
1953 int rc;
1954
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001955 old_dsec = inode_security(old_dir);
1956 old_isec = backing_inode_security(old_dentry);
David Howellse36cb0b2015-01-29 12:02:35 +00001957 old_is_dir = d_is_dir(old_dentry);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001958 new_dsec = inode_security(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
Eric Paris50c205f2012-04-04 15:01:43 -04001960 ad.type = LSM_AUDIT_DATA_DENTRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
Eric Parisa2694342011-04-25 13:10:27 -04001962 ad.u.dentry = old_dentry;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001963 rc = avc_has_perm(&selinux_state,
1964 sid, old_dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 DIR__REMOVE_NAME | DIR__SEARCH, &ad);
1966 if (rc)
1967 return rc;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001968 rc = avc_has_perm(&selinux_state,
1969 sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 old_isec->sclass, FILE__RENAME, &ad);
1971 if (rc)
1972 return rc;
1973 if (old_is_dir && new_dir != old_dir) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001974 rc = avc_has_perm(&selinux_state,
1975 sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 old_isec->sclass, DIR__REPARENT, &ad);
1977 if (rc)
1978 return rc;
1979 }
1980
Eric Parisa2694342011-04-25 13:10:27 -04001981 ad.u.dentry = new_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 av = DIR__ADD_NAME | DIR__SEARCH;
David Howells2c616d42015-01-29 12:02:33 +00001983 if (d_is_positive(new_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 av |= DIR__REMOVE_NAME;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001985 rc = avc_has_perm(&selinux_state,
1986 sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 if (rc)
1988 return rc;
David Howells2c616d42015-01-29 12:02:33 +00001989 if (d_is_positive(new_dentry)) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001990 new_isec = backing_inode_security(new_dentry);
David Howellse36cb0b2015-01-29 12:02:35 +00001991 new_is_dir = d_is_dir(new_dentry);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001992 rc = avc_has_perm(&selinux_state,
1993 sid, new_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 new_isec->sclass,
1995 (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
1996 if (rc)
1997 return rc;
1998 }
1999
2000 return 0;
2001}
2002
2003/* Check whether a task can perform a filesystem operation. */
David Howells88e67f32008-11-14 10:39:21 +11002004static int superblock_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 struct super_block *sb,
2006 u32 perms,
Thomas Liu2bf49692009-07-14 12:14:09 -04002007 struct common_audit_data *ad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 struct superblock_security_struct *sbsec;
David Howells88e67f32008-11-14 10:39:21 +11002010 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 sbsec = sb->s_security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002013 return avc_has_perm(&selinux_state,
2014 sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015}
2016
2017/* Convert a Linux mode and permission mask to an access vector. */
2018static inline u32 file_mask_to_av(int mode, int mask)
2019{
2020 u32 av = 0;
2021
Al Virodba19c62011-07-25 20:49:29 -04002022 if (!S_ISDIR(mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 if (mask & MAY_EXEC)
2024 av |= FILE__EXECUTE;
2025 if (mask & MAY_READ)
2026 av |= FILE__READ;
2027
2028 if (mask & MAY_APPEND)
2029 av |= FILE__APPEND;
2030 else if (mask & MAY_WRITE)
2031 av |= FILE__WRITE;
2032
2033 } else {
2034 if (mask & MAY_EXEC)
2035 av |= DIR__SEARCH;
2036 if (mask & MAY_WRITE)
2037 av |= DIR__WRITE;
2038 if (mask & MAY_READ)
2039 av |= DIR__READ;
2040 }
2041
2042 return av;
2043}
2044
2045/* Convert a Linux file to an access vector. */
2046static inline u32 file_to_av(struct file *file)
2047{
2048 u32 av = 0;
2049
2050 if (file->f_mode & FMODE_READ)
2051 av |= FILE__READ;
2052 if (file->f_mode & FMODE_WRITE) {
2053 if (file->f_flags & O_APPEND)
2054 av |= FILE__APPEND;
2055 else
2056 av |= FILE__WRITE;
2057 }
Stephen Smalley0794c662008-03-17 08:55:18 -04002058 if (!av) {
2059 /*
2060 * Special file opened with flags 3 for ioctl-only use.
2061 */
2062 av = FILE__IOCTL;
2063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
2065 return av;
2066}
2067
Eric Paris8b6a5a32008-10-29 17:06:46 -04002068/*
2069 * Convert a file to an access vector and include the correct open
2070 * open permission.
2071 */
2072static inline u32 open_file_to_av(struct file *file)
2073{
2074 u32 av = file_to_av(file);
Stephen Smalleyccb54472017-05-12 12:41:24 -04002075 struct inode *inode = file_inode(file);
Eric Paris8b6a5a32008-10-29 17:06:46 -04002076
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002077 if (selinux_policycap_openperm() &&
2078 inode->i_sb->s_magic != SOCKFS_MAGIC)
Eric Paris49b7b8d2010-07-23 11:44:09 -04002079 av |= FILE__OPEN;
2080
Eric Paris8b6a5a32008-10-29 17:06:46 -04002081 return av;
2082}
2083
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084/* Hook functions begin here. */
2085
Stephen Smalley79af7302015-01-21 10:54:10 -05002086static int selinux_binder_set_context_mgr(struct task_struct *mgr)
2087{
2088 u32 mysid = current_sid();
2089 u32 mgrsid = task_sid(mgr);
2090
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002091 return avc_has_perm(&selinux_state,
2092 mysid, mgrsid, SECCLASS_BINDER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002093 BINDER__SET_CONTEXT_MGR, NULL);
2094}
2095
2096static int selinux_binder_transaction(struct task_struct *from,
2097 struct task_struct *to)
2098{
2099 u32 mysid = current_sid();
2100 u32 fromsid = task_sid(from);
2101 u32 tosid = task_sid(to);
2102 int rc;
2103
2104 if (mysid != fromsid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002105 rc = avc_has_perm(&selinux_state,
2106 mysid, fromsid, SECCLASS_BINDER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002107 BINDER__IMPERSONATE, NULL);
2108 if (rc)
2109 return rc;
2110 }
2111
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002112 return avc_has_perm(&selinux_state,
2113 fromsid, tosid, SECCLASS_BINDER, BINDER__CALL,
Stephen Smalley79af7302015-01-21 10:54:10 -05002114 NULL);
2115}
2116
2117static int selinux_binder_transfer_binder(struct task_struct *from,
2118 struct task_struct *to)
2119{
2120 u32 fromsid = task_sid(from);
2121 u32 tosid = task_sid(to);
2122
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002123 return avc_has_perm(&selinux_state,
2124 fromsid, tosid, SECCLASS_BINDER, BINDER__TRANSFER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002125 NULL);
2126}
2127
2128static int selinux_binder_transfer_file(struct task_struct *from,
2129 struct task_struct *to,
2130 struct file *file)
2131{
2132 u32 sid = task_sid(to);
2133 struct file_security_struct *fsec = file->f_security;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002134 struct dentry *dentry = file->f_path.dentry;
Paul Moore20cdef82016-04-04 14:14:42 -04002135 struct inode_security_struct *isec;
Stephen Smalley79af7302015-01-21 10:54:10 -05002136 struct common_audit_data ad;
2137 int rc;
2138
2139 ad.type = LSM_AUDIT_DATA_PATH;
2140 ad.u.path = file->f_path;
2141
2142 if (sid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002143 rc = avc_has_perm(&selinux_state,
2144 sid, fsec->sid,
Stephen Smalley79af7302015-01-21 10:54:10 -05002145 SECCLASS_FD,
2146 FD__USE,
2147 &ad);
2148 if (rc)
2149 return rc;
2150 }
2151
Chenbo Fengf66e4482017-10-18 13:00:26 -07002152#ifdef CONFIG_BPF_SYSCALL
2153 rc = bpf_fd_pass(file, sid);
2154 if (rc)
2155 return rc;
2156#endif
2157
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002158 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
Stephen Smalley79af7302015-01-21 10:54:10 -05002159 return 0;
2160
Paul Moore20cdef82016-04-04 14:14:42 -04002161 isec = backing_inode_security(dentry);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002162 return avc_has_perm(&selinux_state,
2163 sid, isec->sid, isec->sclass, file_to_av(file),
Stephen Smalley79af7302015-01-21 10:54:10 -05002164 &ad);
2165}
2166
Ingo Molnar9e488582009-05-07 19:26:19 +10002167static int selinux_ptrace_access_check(struct task_struct *child,
David Howells5cd9c582008-08-14 11:37:28 +01002168 unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002170 u32 sid = current_sid();
2171 u32 csid = task_sid(child);
Stephen Smalley006ebb42008-05-19 08:32:49 -04002172
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002173 if (mode & PTRACE_MODE_READ)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002174 return avc_has_perm(&selinux_state,
2175 sid, csid, SECCLASS_FILE, FILE__READ, NULL);
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002176
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002177 return avc_has_perm(&selinux_state,
2178 sid, csid, SECCLASS_PROCESS, PROCESS__PTRACE, NULL);
David Howells5cd9c582008-08-14 11:37:28 +01002179}
2180
2181static int selinux_ptrace_traceme(struct task_struct *parent)
2182{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002183 return avc_has_perm(&selinux_state,
2184 task_sid(parent), current_sid(), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002185 PROCESS__PTRACE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186}
2187
2188static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
Eric Paris828dfe12008-04-17 13:17:49 -04002189 kernel_cap_t *inheritable, kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002191 return avc_has_perm(&selinux_state,
2192 current_sid(), task_sid(target), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002193 PROCESS__GETCAP, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194}
2195
David Howellsd84f4f92008-11-14 10:39:23 +11002196static int selinux_capset(struct cred *new, const struct cred *old,
2197 const kernel_cap_t *effective,
2198 const kernel_cap_t *inheritable,
2199 const kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002201 return avc_has_perm(&selinux_state,
2202 cred_sid(old), cred_sid(new), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002203 PROCESS__SETCAP, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204}
2205
James Morris5626d3e2009-01-30 10:05:06 +11002206/*
2207 * (This comment used to live with the selinux_task_setuid hook,
2208 * which was removed).
2209 *
2210 * Since setuid only affects the current process, and since the SELinux
2211 * controls are not based on the Linux identity attributes, SELinux does not
2212 * need to control this operation. However, SELinux does control the use of
2213 * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
2214 */
2215
Eric Paris6a9de492012-01-03 12:25:14 -05002216static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
2217 int cap, int audit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218{
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04002219 return cred_has_capability(cred, cap, audit, ns == &init_user_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220}
2221
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
2223{
David Howells88e67f32008-11-14 10:39:21 +11002224 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 int rc = 0;
2226
2227 if (!sb)
2228 return 0;
2229
2230 switch (cmds) {
Eric Paris828dfe12008-04-17 13:17:49 -04002231 case Q_SYNC:
2232 case Q_QUOTAON:
2233 case Q_QUOTAOFF:
2234 case Q_SETINFO:
2235 case Q_SETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11002236 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04002237 break;
2238 case Q_GETFMT:
2239 case Q_GETINFO:
2240 case Q_GETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11002241 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04002242 break;
2243 default:
2244 rc = 0; /* let the kernel handle invalid cmds */
2245 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 }
2247 return rc;
2248}
2249
2250static int selinux_quota_on(struct dentry *dentry)
2251{
David Howells88e67f32008-11-14 10:39:21 +11002252 const struct cred *cred = current_cred();
2253
Eric Paris2875fa02011-04-28 16:04:24 -04002254 return dentry_has_perm(cred, dentry, FILE__QUOTAON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255}
2256
Eric Paris12b30522010-11-15 18:36:29 -05002257static int selinux_syslog(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 switch (type) {
Kees Cookd78ca3c2010-02-03 15:37:13 -08002260 case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */
2261 case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002262 return avc_has_perm(&selinux_state,
2263 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002264 SECCLASS_SYSTEM, SYSTEM__SYSLOG_READ, NULL);
Kees Cookd78ca3c2010-02-03 15:37:13 -08002265 case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */
2266 case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */
2267 /* Set level of messages printed to console */
2268 case SYSLOG_ACTION_CONSOLE_LEVEL:
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002269 return avc_has_perm(&selinux_state,
2270 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002271 SECCLASS_SYSTEM, SYSTEM__SYSLOG_CONSOLE,
2272 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 }
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002274 /* All other syslog types */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002275 return avc_has_perm(&selinux_state,
2276 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002277 SECCLASS_SYSTEM, SYSTEM__SYSLOG_MOD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278}
2279
2280/*
2281 * Check that a process has enough memory to allocate a new virtual
2282 * mapping. 0 means there is enough memory for the allocation to
2283 * succeed and -ENOMEM implies there is not.
2284 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 * Do not audit the selinux permission check, as this is applied to all
2286 * processes that allocate mappings.
2287 */
Alan Cox34b4e4a2007-08-22 14:01:28 -07002288static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289{
2290 int rc, cap_sys_admin = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002292 rc = cred_has_capability(current_cred(), CAP_SYS_ADMIN,
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04002293 SECURITY_CAP_NOAUDIT, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 if (rc == 0)
2295 cap_sys_admin = 1;
2296
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002297 return cap_sys_admin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298}
2299
2300/* binprm security operations */
2301
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002302static u32 ptrace_parent_sid(void)
Paul Moore0c6181c2016-03-30 21:41:21 -04002303{
2304 u32 sid = 0;
2305 struct task_struct *tracer;
2306
2307 rcu_read_lock();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002308 tracer = ptrace_parent(current);
Paul Moore0c6181c2016-03-30 21:41:21 -04002309 if (tracer)
2310 sid = task_sid(tracer);
2311 rcu_read_unlock();
2312
2313 return sid;
2314}
2315
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002316static int check_nnp_nosuid(const struct linux_binprm *bprm,
2317 const struct task_security_struct *old_tsec,
2318 const struct task_security_struct *new_tsec)
2319{
2320 int nnp = (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS);
Andy Lutomirski380cf5b2016-06-23 16:41:05 -05002321 int nosuid = !mnt_may_suid(bprm->file->f_path.mnt);
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002322 int rc;
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002323 u32 av;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002324
2325 if (!nnp && !nosuid)
2326 return 0; /* neither NNP nor nosuid */
2327
2328 if (new_tsec->sid == old_tsec->sid)
2329 return 0; /* No change in credentials */
2330
2331 /*
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002332 * If the policy enables the nnp_nosuid_transition policy capability,
2333 * then we permit transitions under NNP or nosuid if the
2334 * policy allows the corresponding permission between
2335 * the old and new contexts.
2336 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002337 if (selinux_policycap_nnp_nosuid_transition()) {
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002338 av = 0;
2339 if (nnp)
2340 av |= PROCESS2__NNP_TRANSITION;
2341 if (nosuid)
2342 av |= PROCESS2__NOSUID_TRANSITION;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002343 rc = avc_has_perm(&selinux_state,
2344 old_tsec->sid, new_tsec->sid,
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002345 SECCLASS_PROCESS2, av, NULL);
2346 if (!rc)
2347 return 0;
2348 }
2349
2350 /*
2351 * We also permit NNP or nosuid transitions to bounded SIDs,
2352 * i.e. SIDs that are guaranteed to only be allowed a subset
2353 * of the permissions of the current SID.
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002354 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002355 rc = security_bounded_transition(&selinux_state, old_tsec->sid,
2356 new_tsec->sid);
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002357 if (!rc)
2358 return 0;
2359
2360 /*
2361 * On failure, preserve the errno values for NNP vs nosuid.
2362 * NNP: Operation not permitted for caller.
2363 * nosuid: Permission denied to file.
2364 */
2365 if (nnp)
2366 return -EPERM;
2367 return -EACCES;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002368}
2369
David Howellsa6f76f22008-11-14 10:39:24 +11002370static int selinux_bprm_set_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371{
David Howellsa6f76f22008-11-14 10:39:24 +11002372 const struct task_security_struct *old_tsec;
2373 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 struct inode_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04002375 struct common_audit_data ad;
Al Viro496ad9a2013-01-23 17:07:38 -05002376 struct inode *inode = file_inode(bprm->file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 int rc;
2378
David Howellsa6f76f22008-11-14 10:39:24 +11002379 /* SELinux context only depends on initial program or script and not
2380 * the script interpreter */
Kees Cookddb4a142017-07-18 15:25:23 -07002381 if (bprm->called_set_creds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 return 0;
2383
David Howellsa6f76f22008-11-14 10:39:24 +11002384 old_tsec = current_security();
2385 new_tsec = bprm->cred->security;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002386 isec = inode_security(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
2388 /* Default to the current task SID. */
David Howellsa6f76f22008-11-14 10:39:24 +11002389 new_tsec->sid = old_tsec->sid;
2390 new_tsec->osid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
Michael LeMay28eba5b2006-06-27 02:53:42 -07002392 /* Reset fs, key, and sock SIDs on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002393 new_tsec->create_sid = 0;
2394 new_tsec->keycreate_sid = 0;
2395 new_tsec->sockcreate_sid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
David Howellsa6f76f22008-11-14 10:39:24 +11002397 if (old_tsec->exec_sid) {
2398 new_tsec->sid = old_tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 /* Reset exec SID on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002400 new_tsec->exec_sid = 0;
Andy Lutomirski259e5e62012-04-12 16:47:50 -05002401
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002402 /* Fail on NNP or nosuid if not an allowed transition. */
2403 rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
2404 if (rc)
2405 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 } else {
2407 /* Check for a default transition on this program. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002408 rc = security_transition_sid(&selinux_state, old_tsec->sid,
2409 isec->sid, SECCLASS_PROCESS, NULL,
Eric Paris652bb9b2011-02-01 11:05:40 -05002410 &new_tsec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 if (rc)
2412 return rc;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002413
2414 /*
2415 * Fallback to old SID on NNP or nosuid if not an allowed
2416 * transition.
2417 */
2418 rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
2419 if (rc)
2420 new_tsec->sid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 }
2422
Vivek Goyal43af5de2016-09-09 11:37:49 -04002423 ad.type = LSM_AUDIT_DATA_FILE;
2424 ad.u.file = bprm->file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425
David Howellsa6f76f22008-11-14 10:39:24 +11002426 if (new_tsec->sid == old_tsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002427 rc = avc_has_perm(&selinux_state,
2428 old_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
2430 if (rc)
2431 return rc;
2432 } else {
2433 /* Check permissions for the transition. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002434 rc = avc_has_perm(&selinux_state,
2435 old_tsec->sid, new_tsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
2437 if (rc)
2438 return rc;
2439
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002440 rc = avc_has_perm(&selinux_state,
2441 new_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
2443 if (rc)
2444 return rc;
2445
David Howellsa6f76f22008-11-14 10:39:24 +11002446 /* Check for shared state */
2447 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002448 rc = avc_has_perm(&selinux_state,
2449 old_tsec->sid, new_tsec->sid,
David Howellsa6f76f22008-11-14 10:39:24 +11002450 SECCLASS_PROCESS, PROCESS__SHARE,
2451 NULL);
2452 if (rc)
2453 return -EPERM;
2454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455
David Howellsa6f76f22008-11-14 10:39:24 +11002456 /* Make sure that anyone attempting to ptrace over a task that
2457 * changes its SID has the appropriate permit */
Eric W. Biederman9227dd22017-01-23 17:26:31 +13002458 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002459 u32 ptsid = ptrace_parent_sid();
David Howellsa6f76f22008-11-14 10:39:24 +11002460 if (ptsid != 0) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002461 rc = avc_has_perm(&selinux_state,
2462 ptsid, new_tsec->sid,
David Howellsa6f76f22008-11-14 10:39:24 +11002463 SECCLASS_PROCESS,
2464 PROCESS__PTRACE, NULL);
2465 if (rc)
2466 return -EPERM;
2467 }
2468 }
2469
2470 /* Clear any possibly unsafe personality bits on exec: */
2471 bprm->per_clear |= PER_CLEAR_ON_SETID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 /* Enable secure mode for SIDs transitions unless
2474 the noatsecure permission is granted between
2475 the two SIDs, i.e. ahp returns 0. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002476 rc = avc_has_perm(&selinux_state,
2477 old_tsec->sid, new_tsec->sid,
Kees Cook62874c32017-07-18 15:25:25 -07002478 SECCLASS_PROCESS, PROCESS__NOATSECURE,
2479 NULL);
2480 bprm->secureexec |= !!rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 }
2482
Kees Cook62874c32017-07-18 15:25:25 -07002483 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484}
2485
Al Viroc3c073f2012-08-21 22:32:06 -04002486static int match_file(const void *p, struct file *file, unsigned fd)
2487{
2488 return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0;
2489}
2490
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491/* Derived from fs/exec.c:flush_old_files. */
David Howells745ca242008-11-14 10:39:22 +11002492static inline void flush_unauthorized_files(const struct cred *cred,
2493 struct files_struct *files)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 struct file *file, *devnull = NULL;
Stephen Smalleyb20c8122006-09-25 23:32:03 -07002496 struct tty_struct *tty;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002497 int drop_tty = 0;
Al Viroc3c073f2012-08-21 22:32:06 -04002498 unsigned n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002500 tty = get_current_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 if (tty) {
Peter Hurley4a510962016-01-09 21:35:23 -08002502 spin_lock(&tty->files_lock);
Eric Paris37dd0bd2008-10-31 17:40:00 -04002503 if (!list_empty(&tty->tty_files)) {
Nick Piggind996b622010-08-18 04:37:36 +10002504 struct tty_file_private *file_priv;
Eric Paris37dd0bd2008-10-31 17:40:00 -04002505
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 /* Revalidate access to controlling tty.
David Howells13f8e982013-06-13 23:37:55 +01002507 Use file_path_has_perm on the tty path directly
2508 rather than using file_has_perm, as this particular
2509 open file may belong to another process and we are
2510 only interested in the inode-based check here. */
Nick Piggind996b622010-08-18 04:37:36 +10002511 file_priv = list_first_entry(&tty->tty_files,
2512 struct tty_file_private, list);
2513 file = file_priv->file;
David Howells13f8e982013-06-13 23:37:55 +01002514 if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE))
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002515 drop_tty = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 }
Peter Hurley4a510962016-01-09 21:35:23 -08002517 spin_unlock(&tty->files_lock);
Alan Cox452a00d2008-10-13 10:39:13 +01002518 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 }
Eric W. Biederman98a27ba2007-05-08 00:26:56 -07002520 /* Reset controlling tty. */
2521 if (drop_tty)
2522 no_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
2524 /* Revalidate access to inherited open files. */
Al Viroc3c073f2012-08-21 22:32:06 -04002525 n = iterate_fd(files, 0, match_file, cred);
2526 if (!n) /* none found? */
2527 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528
Al Viroc3c073f2012-08-21 22:32:06 -04002529 devnull = dentry_open(&selinux_null, O_RDWR, cred);
Al Viro45525b22012-10-16 13:30:07 -04002530 if (IS_ERR(devnull))
2531 devnull = NULL;
2532 /* replace all the matching ones with this */
2533 do {
2534 replace_fd(n - 1, devnull, 0);
2535 } while ((n = iterate_fd(files, n, match_file, cred)) != 0);
2536 if (devnull)
Al Viroc3c073f2012-08-21 22:32:06 -04002537 fput(devnull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538}
2539
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540/*
David Howellsa6f76f22008-11-14 10:39:24 +11002541 * Prepare a process for imminent new credential changes due to exec
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 */
David Howellsa6f76f22008-11-14 10:39:24 +11002543static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544{
David Howellsa6f76f22008-11-14 10:39:24 +11002545 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 struct rlimit *rlim, *initrlim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 int rc, i;
2548
David Howellsa6f76f22008-11-14 10:39:24 +11002549 new_tsec = bprm->cred->security;
2550 if (new_tsec->sid == new_tsec->osid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 return;
2552
2553 /* Close files for which the new task SID is not authorized. */
David Howellsa6f76f22008-11-14 10:39:24 +11002554 flush_unauthorized_files(bprm->cred, current->files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555
David Howellsa6f76f22008-11-14 10:39:24 +11002556 /* Always clear parent death signal on SID transitions. */
2557 current->pdeath_signal = 0;
2558
2559 /* Check whether the new SID can inherit resource limits from the old
2560 * SID. If not, reset all soft limits to the lower of the current
2561 * task's hard limit and the init task's soft limit.
2562 *
2563 * Note that the setting of hard limits (even to lower them) can be
2564 * controlled by the setrlimit check. The inclusion of the init task's
2565 * soft limit into the computation is to avoid resetting soft limits
2566 * higher than the default soft limit for cases where the default is
2567 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2568 */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002569 rc = avc_has_perm(&selinux_state,
2570 new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
David Howellsa6f76f22008-11-14 10:39:24 +11002571 PROCESS__RLIMITINH, NULL);
2572 if (rc) {
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002573 /* protect against do_prlimit() */
2574 task_lock(current);
David Howellsa6f76f22008-11-14 10:39:24 +11002575 for (i = 0; i < RLIM_NLIMITS; i++) {
2576 rlim = current->signal->rlim + i;
2577 initrlim = init_task.signal->rlim + i;
2578 rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
2579 }
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002580 task_unlock(current);
Nicolas Pitrebaa73d92016-11-11 00:10:10 -05002581 if (IS_ENABLED(CONFIG_POSIX_TIMERS))
2582 update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
David Howellsa6f76f22008-11-14 10:39:24 +11002583 }
2584}
2585
2586/*
2587 * Clean up the process immediately after the installation of new credentials
2588 * due to exec
2589 */
2590static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
2591{
2592 const struct task_security_struct *tsec = current_security();
2593 struct itimerval itimer;
David Howellsa6f76f22008-11-14 10:39:24 +11002594 u32 osid, sid;
2595 int rc, i;
David Howellsa6f76f22008-11-14 10:39:24 +11002596
David Howellsa6f76f22008-11-14 10:39:24 +11002597 osid = tsec->osid;
2598 sid = tsec->sid;
2599
2600 if (sid == osid)
2601 return;
2602
2603 /* Check whether the new SID can inherit signal state from the old SID.
2604 * If not, clear itimers to avoid subsequent signal generation and
2605 * flush and unblock signals.
2606 *
2607 * This must occur _after_ the task SID has been updated so that any
2608 * kill done after the flush will be checked against the new SID.
2609 */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002610 rc = avc_has_perm(&selinux_state,
2611 osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 if (rc) {
Nicolas Pitrebaa73d92016-11-11 00:10:10 -05002613 if (IS_ENABLED(CONFIG_POSIX_TIMERS)) {
2614 memset(&itimer, 0, sizeof itimer);
2615 for (i = 0; i < 3; i++)
2616 do_setitimer(i, &itimer, NULL);
2617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterov9e7c8f82015-06-04 16:22:16 -04002619 if (!fatal_signal_pending(current)) {
2620 flush_sigqueue(&current->pending);
2621 flush_sigqueue(&current->signal->shared_pending);
David Howells3bcac022009-04-29 13:45:05 +01002622 flush_signal_handlers(current, 1);
2623 sigemptyset(&current->blocked);
Oleg Nesterov9e7c8f82015-06-04 16:22:16 -04002624 recalc_sigpending();
David Howells3bcac022009-04-29 13:45:05 +01002625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 spin_unlock_irq(&current->sighand->siglock);
2627 }
2628
David Howellsa6f76f22008-11-14 10:39:24 +11002629 /* Wake up the parent if it is waiting so that it can recheck
2630 * wait permission to the new task SID. */
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002631 read_lock(&tasklist_lock);
Oleg Nesterov0b7570e2009-09-23 15:56:46 -07002632 __wake_up_parent(current, current->real_parent);
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002633 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634}
2635
2636/* superblock security operations */
2637
2638static int selinux_sb_alloc_security(struct super_block *sb)
2639{
2640 return superblock_alloc_security(sb);
2641}
2642
2643static void selinux_sb_free_security(struct super_block *sb)
2644{
2645 superblock_free_security(sb);
2646}
2647
2648static inline int match_prefix(char *prefix, int plen, char *option, int olen)
2649{
2650 if (plen > olen)
2651 return 0;
2652
2653 return !memcmp(prefix, option, plen);
2654}
2655
2656static inline int selinux_option(char *option, int len)
2657{
Eric Paris832cbd92008-04-01 13:24:09 -04002658 return (match_prefix(CONTEXT_STR, sizeof(CONTEXT_STR)-1, option, len) ||
2659 match_prefix(FSCONTEXT_STR, sizeof(FSCONTEXT_STR)-1, option, len) ||
2660 match_prefix(DEFCONTEXT_STR, sizeof(DEFCONTEXT_STR)-1, option, len) ||
David P. Quigley11689d42009-01-16 09:22:03 -05002661 match_prefix(ROOTCONTEXT_STR, sizeof(ROOTCONTEXT_STR)-1, option, len) ||
2662 match_prefix(LABELSUPP_STR, sizeof(LABELSUPP_STR)-1, option, len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663}
2664
2665static inline void take_option(char **to, char *from, int *first, int len)
2666{
2667 if (!*first) {
2668 **to = ',';
2669 *to += 1;
Cory Olmo3528a952006-09-29 01:58:44 -07002670 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 *first = 0;
2672 memcpy(*to, from, len);
2673 *to += len;
2674}
2675
Eric Paris828dfe12008-04-17 13:17:49 -04002676static inline void take_selinux_option(char **to, char *from, int *first,
2677 int len)
Cory Olmo3528a952006-09-29 01:58:44 -07002678{
2679 int current_size = 0;
2680
2681 if (!*first) {
2682 **to = '|';
2683 *to += 1;
Eric Paris828dfe12008-04-17 13:17:49 -04002684 } else
Cory Olmo3528a952006-09-29 01:58:44 -07002685 *first = 0;
2686
2687 while (current_size < len) {
2688 if (*from != '"') {
2689 **to = *from;
2690 *to += 1;
2691 }
2692 from += 1;
2693 current_size += 1;
2694 }
2695}
2696
Eric Parise0007522008-03-05 10:31:54 -05002697static int selinux_sb_copy_data(char *orig, char *copy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698{
2699 int fnosec, fsec, rc = 0;
2700 char *in_save, *in_curr, *in_end;
2701 char *sec_curr, *nosec_save, *nosec;
Cory Olmo3528a952006-09-29 01:58:44 -07002702 int open_quote = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703
2704 in_curr = orig;
2705 sec_curr = copy;
2706
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 nosec = (char *)get_zeroed_page(GFP_KERNEL);
2708 if (!nosec) {
2709 rc = -ENOMEM;
2710 goto out;
2711 }
2712
2713 nosec_save = nosec;
2714 fnosec = fsec = 1;
2715 in_save = in_end = orig;
2716
2717 do {
Cory Olmo3528a952006-09-29 01:58:44 -07002718 if (*in_end == '"')
2719 open_quote = !open_quote;
2720 if ((*in_end == ',' && open_quote == 0) ||
2721 *in_end == '\0') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 int len = in_end - in_curr;
2723
2724 if (selinux_option(in_curr, len))
Cory Olmo3528a952006-09-29 01:58:44 -07002725 take_selinux_option(&sec_curr, in_curr, &fsec, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 else
2727 take_option(&nosec, in_curr, &fnosec, len);
2728
2729 in_curr = in_end + 1;
2730 }
2731 } while (*in_end++);
2732
Eric Paris6931dfc2005-06-30 02:58:51 -07002733 strcpy(in_save, nosec_save);
Gerald Schaeferda3caa22005-06-21 17:15:18 -07002734 free_page((unsigned long)nosec_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735out:
2736 return rc;
2737}
2738
Al Viro204cc0c2018-12-13 13:41:47 -05002739static int selinux_sb_eat_lsm_opts(char *options, void **mnt_opts)
Al Viro5b400232018-12-12 20:13:29 -05002740{
2741 char *s = (char *)get_zeroed_page(GFP_KERNEL);
2742 int err;
2743
2744 if (!s)
2745 return -ENOMEM;
2746 err = selinux_sb_copy_data(options, s);
2747 if (!err)
Al Viro204cc0c2018-12-13 13:41:47 -05002748 err = selinux_parse_opts_str(s, mnt_opts);
Al Viro5b400232018-12-12 20:13:29 -05002749 free_page((unsigned long)s);
2750 return err;
2751}
2752
Al Viro204cc0c2018-12-13 13:41:47 -05002753static int selinux_sb_remount(struct super_block *sb, void *mnt_opts)
Eric Paris026eb162011-03-03 16:09:14 -05002754{
Al Viro204cc0c2018-12-13 13:41:47 -05002755 struct security_mnt_opts *opts = mnt_opts;
Al Viroc039bc32018-12-01 23:06:57 -05002756 int i, *flags;
2757 char **mount_options;
Eric Paris026eb162011-03-03 16:09:14 -05002758 struct superblock_security_struct *sbsec = sb->s_security;
2759
2760 if (!(sbsec->flags & SE_SBINITIALIZED))
2761 return 0;
2762
Al Viro204cc0c2018-12-13 13:41:47 -05002763 if (!opts)
2764 return 0;
2765
Al Viroc039bc32018-12-01 23:06:57 -05002766 mount_options = opts->mnt_opts;
2767 flags = opts->mnt_opts_flags;
Eric Paris026eb162011-03-03 16:09:14 -05002768
Al Viroc039bc32018-12-01 23:06:57 -05002769 for (i = 0; i < opts->num_mnt_opts; i++) {
Eric Paris026eb162011-03-03 16:09:14 -05002770 u32 sid;
Al Viroc039bc32018-12-01 23:06:57 -05002771 int rc;
Eric Paris026eb162011-03-03 16:09:14 -05002772
Eric Paris12f348b2012-10-09 10:56:25 -04002773 if (flags[i] == SBLABEL_MNT)
Eric Paris026eb162011-03-03 16:09:14 -05002774 continue;
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002775 rc = security_context_str_to_sid(&selinux_state,
2776 mount_options[i], &sid,
2777 GFP_KERNEL);
Eric Paris026eb162011-03-03 16:09:14 -05002778 if (rc) {
peter enderborgc103a912018-06-12 10:09:03 +02002779 pr_warn("SELinux: security_context_str_to_sid"
Linus Torvalds29b1deb2013-12-15 11:17:45 -08002780 "(%s) failed for (dev %s, type %s) errno=%d\n",
2781 mount_options[i], sb->s_id, sb->s_type->name, rc);
Al Viroc039bc32018-12-01 23:06:57 -05002782 return rc;
Eric Paris026eb162011-03-03 16:09:14 -05002783 }
Eric Paris026eb162011-03-03 16:09:14 -05002784 switch (flags[i]) {
2785 case FSCONTEXT_MNT:
2786 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
2787 goto out_bad_option;
2788 break;
2789 case CONTEXT_MNT:
2790 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
2791 goto out_bad_option;
2792 break;
2793 case ROOTCONTEXT_MNT: {
2794 struct inode_security_struct *root_isec;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002795 root_isec = backing_inode_security(sb->s_root);
Eric Paris026eb162011-03-03 16:09:14 -05002796
2797 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
2798 goto out_bad_option;
2799 break;
2800 }
2801 case DEFCONTEXT_MNT:
2802 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
2803 goto out_bad_option;
2804 break;
2805 default:
Al Viroc039bc32018-12-01 23:06:57 -05002806 return -EINVAL;
Eric Paris026eb162011-03-03 16:09:14 -05002807 }
2808 }
Al Viroc039bc32018-12-01 23:06:57 -05002809 return 0;
Eric Paris026eb162011-03-03 16:09:14 -05002810
Eric Paris026eb162011-03-03 16:09:14 -05002811out_bad_option:
peter enderborgc103a912018-06-12 10:09:03 +02002812 pr_warn("SELinux: unable to change security options "
Linus Torvalds29b1deb2013-12-15 11:17:45 -08002813 "during remount (dev %s, type=%s)\n", sb->s_id,
2814 sb->s_type->name);
Al Viroc039bc32018-12-01 23:06:57 -05002815 return -EINVAL;
Eric Paris026eb162011-03-03 16:09:14 -05002816}
2817
Al Viroa10d7c22018-12-05 11:58:35 -05002818static int selinux_sb_kern_mount(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819{
David Howells88e67f32008-11-14 10:39:21 +11002820 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002821 struct common_audit_data ad;
James Morris74192242008-12-19 11:41:10 +11002822
Eric Paris50c205f2012-04-04 15:01:43 -04002823 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002824 ad.u.dentry = sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002825 return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826}
2827
David Howells726c3342006-06-23 02:02:58 -07002828static int selinux_sb_statfs(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829{
David Howells88e67f32008-11-14 10:39:21 +11002830 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002831 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832
Eric Paris50c205f2012-04-04 15:01:43 -04002833 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002834 ad.u.dentry = dentry->d_sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002835 return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836}
2837
Al Viro808d4e32012-10-11 11:42:01 -04002838static int selinux_mount(const char *dev_name,
Al Viro8a04c432016-03-25 14:52:53 -04002839 const struct path *path,
Al Viro808d4e32012-10-11 11:42:01 -04002840 const char *type,
Eric Paris828dfe12008-04-17 13:17:49 -04002841 unsigned long flags,
2842 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843{
David Howells88e67f32008-11-14 10:39:21 +11002844 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845
2846 if (flags & MS_REMOUNT)
Al Virod8c95842011-12-07 18:16:57 -05002847 return superblock_has_perm(cred, path->dentry->d_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002848 FILESYSTEM__REMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 else
Eric Paris2875fa02011-04-28 16:04:24 -04002850 return path_has_perm(cred, path, FILE__MOUNTON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851}
2852
2853static int selinux_umount(struct vfsmount *mnt, int flags)
2854{
David Howells88e67f32008-11-14 10:39:21 +11002855 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856
David Howells88e67f32008-11-14 10:39:21 +11002857 return superblock_has_perm(cred, mnt->mnt_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002858 FILESYSTEM__UNMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859}
2860
2861/* inode security operations */
2862
2863static int selinux_inode_alloc_security(struct inode *inode)
2864{
2865 return inode_alloc_security(inode);
2866}
2867
2868static void selinux_inode_free_security(struct inode *inode)
2869{
2870 inode_free_security(inode);
2871}
2872
David Quigleyd47be3d2013-05-22 12:50:34 -04002873static int selinux_dentry_init_security(struct dentry *dentry, int mode,
Al Viro4f3ccd72016-07-20 16:06:15 -04002874 const struct qstr *name, void **ctx,
David Quigleyd47be3d2013-05-22 12:50:34 -04002875 u32 *ctxlen)
2876{
David Quigleyd47be3d2013-05-22 12:50:34 -04002877 u32 newsid;
2878 int rc;
2879
Vivek Goyalc957f6d2016-07-13 10:44:51 -04002880 rc = selinux_determine_inode_label(current_security(),
2881 d_inode(dentry->d_parent), name,
David Howellsc3c188b2015-07-10 17:19:58 -04002882 inode_mode_to_security_class(mode),
2883 &newsid);
2884 if (rc)
2885 return rc;
David Quigleyd47be3d2013-05-22 12:50:34 -04002886
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002887 return security_sid_to_context(&selinux_state, newsid, (char **)ctx,
2888 ctxlen);
David Quigleyd47be3d2013-05-22 12:50:34 -04002889}
2890
Vivek Goyala518b0a2016-07-13 10:44:53 -04002891static int selinux_dentry_create_files_as(struct dentry *dentry, int mode,
2892 struct qstr *name,
2893 const struct cred *old,
2894 struct cred *new)
2895{
2896 u32 newsid;
2897 int rc;
2898 struct task_security_struct *tsec;
2899
2900 rc = selinux_determine_inode_label(old->security,
2901 d_inode(dentry->d_parent), name,
2902 inode_mode_to_security_class(mode),
2903 &newsid);
2904 if (rc)
2905 return rc;
2906
2907 tsec = new->security;
2908 tsec->create_sid = newsid;
2909 return 0;
2910}
2911
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002912static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +09002913 const struct qstr *qstr,
2914 const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -05002915 void **value, size_t *len)
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002916{
Paul Moore5fb49872010-04-22 14:46:19 -04002917 const struct task_security_struct *tsec = current_security();
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002918 struct superblock_security_struct *sbsec;
Corentin LABBEc0d4f462017-10-04 20:32:17 +02002919 u32 newsid, clen;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002920 int rc;
Tetsuo Handa95489062013-07-25 05:44:02 +09002921 char *context;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002922
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002923 sbsec = dir->i_sb->s_security;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002924
David Howells275bb412008-11-14 10:39:19 +11002925 newsid = tsec->create_sid;
2926
Vivek Goyalc957f6d2016-07-13 10:44:51 -04002927 rc = selinux_determine_inode_label(current_security(),
David Howellsc3c188b2015-07-10 17:19:58 -04002928 dir, qstr,
2929 inode_mode_to_security_class(inode->i_mode),
2930 &newsid);
2931 if (rc)
2932 return rc;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002933
Eric Paris296fddf2006-09-25 23:32:00 -07002934 /* Possibly defer initialization to selinux_complete_init. */
David P. Quigley0d90a7e2009-01-16 09:22:02 -05002935 if (sbsec->flags & SE_SBINITIALIZED) {
Eric Paris296fddf2006-09-25 23:32:00 -07002936 struct inode_security_struct *isec = inode->i_security;
2937 isec->sclass = inode_mode_to_security_class(inode->i_mode);
2938 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05002939 isec->initialized = LABEL_INITIALIZED;
Eric Paris296fddf2006-09-25 23:32:00 -07002940 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002941
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002942 if (!selinux_state.initialized || !(sbsec->flags & SBLABEL_MNT))
Stephen Smalley25a74f32005-11-08 21:34:33 -08002943 return -EOPNOTSUPP;
2944
Tetsuo Handa95489062013-07-25 05:44:02 +09002945 if (name)
2946 *name = XATTR_SELINUX_SUFFIX;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002947
2948 if (value && len) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002949 rc = security_sid_to_context_force(&selinux_state, newsid,
2950 &context, &clen);
Tetsuo Handa95489062013-07-25 05:44:02 +09002951 if (rc)
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002952 return rc;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002953 *value = context;
2954 *len = clen;
2955 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002956
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002957 return 0;
2958}
2959
Al Viro4acdaf22011-07-26 01:42:34 -04002960static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961{
2962 return may_create(dir, dentry, SECCLASS_FILE);
2963}
2964
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2966{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 return may_link(dir, old_dentry, MAY_LINK);
2968}
2969
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
2971{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 return may_link(dir, dentry, MAY_UNLINK);
2973}
2974
2975static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
2976{
2977 return may_create(dir, dentry, SECCLASS_LNK_FILE);
2978}
2979
Al Viro18bb1db2011-07-26 01:41:39 -04002980static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981{
2982 return may_create(dir, dentry, SECCLASS_DIR);
2983}
2984
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
2986{
2987 return may_link(dir, dentry, MAY_RMDIR);
2988}
2989
Al Viro1a67aaf2011-07-26 01:52:52 -04002990static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 return may_create(dir, dentry, inode_mode_to_security_class(mode));
2993}
2994
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
Eric Paris828dfe12008-04-17 13:17:49 -04002996 struct inode *new_inode, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997{
2998 return may_rename(old_inode, old_dentry, new_inode, new_dentry);
2999}
3000
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001static int selinux_inode_readlink(struct dentry *dentry)
3002{
David Howells88e67f32008-11-14 10:39:21 +11003003 const struct cred *cred = current_cred();
3004
Eric Paris2875fa02011-04-28 16:04:24 -04003005 return dentry_has_perm(cred, dentry, FILE__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006}
3007
NeilBrownbda0be72015-03-23 13:37:39 +11003008static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode,
3009 bool rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010{
David Howells88e67f32008-11-14 10:39:21 +11003011 const struct cred *cred = current_cred();
NeilBrownbda0be72015-03-23 13:37:39 +11003012 struct common_audit_data ad;
3013 struct inode_security_struct *isec;
3014 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015
NeilBrownbda0be72015-03-23 13:37:39 +11003016 validate_creds(cred);
3017
3018 ad.type = LSM_AUDIT_DATA_DENTRY;
3019 ad.u.dentry = dentry;
3020 sid = cred_sid(cred);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05003021 isec = inode_security_rcu(inode, rcu);
3022 if (IS_ERR(isec))
3023 return PTR_ERR(isec);
NeilBrownbda0be72015-03-23 13:37:39 +11003024
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003025 return avc_has_perm_flags(&selinux_state,
3026 sid, isec->sid, isec->sclass, FILE__READ, &ad,
NeilBrownbda0be72015-03-23 13:37:39 +11003027 rcu ? MAY_NOT_BLOCK : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028}
3029
Eric Parisd4cf970d2012-04-04 15:01:42 -04003030static noinline int audit_inode_permission(struct inode *inode,
3031 u32 perms, u32 audited, u32 denied,
Stephen Smalley626b9742014-04-29 11:29:04 -07003032 int result,
Eric Parisd4cf970d2012-04-04 15:01:42 -04003033 unsigned flags)
3034{
3035 struct common_audit_data ad;
Eric Parisd4cf970d2012-04-04 15:01:42 -04003036 struct inode_security_struct *isec = inode->i_security;
3037 int rc;
3038
Eric Paris50c205f2012-04-04 15:01:43 -04003039 ad.type = LSM_AUDIT_DATA_INODE;
Eric Parisd4cf970d2012-04-04 15:01:42 -04003040 ad.u.inode = inode;
3041
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003042 rc = slow_avc_audit(&selinux_state,
3043 current_sid(), isec->sid, isec->sclass, perms,
Stephen Smalley626b9742014-04-29 11:29:04 -07003044 audited, denied, result, &ad, flags);
Eric Parisd4cf970d2012-04-04 15:01:42 -04003045 if (rc)
3046 return rc;
3047 return 0;
3048}
3049
Al Viroe74f71e2011-06-20 19:38:15 -04003050static int selinux_inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051{
David Howells88e67f32008-11-14 10:39:21 +11003052 const struct cred *cred = current_cred();
Eric Parisb782e0a2010-07-23 11:44:03 -04003053 u32 perms;
3054 bool from_access;
Al Virocf1dd1d2011-06-20 19:44:08 -04003055 unsigned flags = mask & MAY_NOT_BLOCK;
Eric Paris2e334052012-04-04 15:01:42 -04003056 struct inode_security_struct *isec;
3057 u32 sid;
3058 struct av_decision avd;
3059 int rc, rc2;
3060 u32 audited, denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061
Eric Parisb782e0a2010-07-23 11:44:03 -04003062 from_access = mask & MAY_ACCESS;
Eric Parisd09ca732010-07-23 11:43:57 -04003063 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
3064
Eric Parisb782e0a2010-07-23 11:44:03 -04003065 /* No permission to check. Existence test. */
3066 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068
Eric Paris2e334052012-04-04 15:01:42 -04003069 validate_creds(cred);
Eric Parisb782e0a2010-07-23 11:44:03 -04003070
Eric Paris2e334052012-04-04 15:01:42 -04003071 if (unlikely(IS_PRIVATE(inode)))
3072 return 0;
Eric Parisb782e0a2010-07-23 11:44:03 -04003073
3074 perms = file_mask_to_av(inode->i_mode, mask);
3075
Eric Paris2e334052012-04-04 15:01:42 -04003076 sid = cred_sid(cred);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05003077 isec = inode_security_rcu(inode, flags & MAY_NOT_BLOCK);
3078 if (IS_ERR(isec))
3079 return PTR_ERR(isec);
Eric Paris2e334052012-04-04 15:01:42 -04003080
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003081 rc = avc_has_perm_noaudit(&selinux_state,
3082 sid, isec->sid, isec->sclass, perms, 0, &avd);
Eric Paris2e334052012-04-04 15:01:42 -04003083 audited = avc_audit_required(perms, &avd, rc,
3084 from_access ? FILE__AUDIT_ACCESS : 0,
3085 &denied);
3086 if (likely(!audited))
3087 return rc;
3088
Stephen Smalley626b9742014-04-29 11:29:04 -07003089 rc2 = audit_inode_permission(inode, perms, audited, denied, rc, flags);
Eric Paris2e334052012-04-04 15:01:42 -04003090 if (rc2)
3091 return rc2;
3092 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093}
3094
3095static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
3096{
David Howells88e67f32008-11-14 10:39:21 +11003097 const struct cred *cred = current_cred();
Stephen Smalleyccb54472017-05-12 12:41:24 -04003098 struct inode *inode = d_backing_inode(dentry);
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003099 unsigned int ia_valid = iattr->ia_valid;
Eric Paris95dbf732012-04-04 13:45:34 -04003100 __u32 av = FILE__WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003102 /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
3103 if (ia_valid & ATTR_FORCE) {
3104 ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
3105 ATTR_FORCE);
3106 if (!ia_valid)
3107 return 0;
3108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003110 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
3111 ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
Eric Paris2875fa02011-04-28 16:04:24 -04003112 return dentry_has_perm(cred, dentry, FILE__SETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003114 if (selinux_policycap_openperm() &&
Stephen Smalleyccb54472017-05-12 12:41:24 -04003115 inode->i_sb->s_magic != SOCKFS_MAGIC &&
3116 (ia_valid & ATTR_SIZE) &&
3117 !(ia_valid & ATTR_FILE))
Eric Paris95dbf732012-04-04 13:45:34 -04003118 av |= FILE__OPEN;
3119
3120 return dentry_has_perm(cred, dentry, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121}
3122
Al Viro3f7036a2015-03-08 19:28:30 -04003123static int selinux_inode_getattr(const struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124{
Al Viro3f7036a2015-03-08 19:28:30 -04003125 return path_has_perm(current_cred(), path, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126}
3127
Stephen Smalleydb590002017-04-20 11:31:30 -04003128static bool has_cap_mac_admin(bool audit)
3129{
3130 const struct cred *cred = current_cred();
3131 int cap_audit = audit ? SECURITY_CAP_AUDIT : SECURITY_CAP_NOAUDIT;
3132
3133 if (cap_capable(cred, &init_user_ns, CAP_MAC_ADMIN, cap_audit))
3134 return false;
3135 if (cred_has_capability(cred, CAP_MAC_ADMIN, cap_audit, true))
3136 return false;
3137 return true;
3138}
3139
David Howells8f0cfa52008-04-29 00:59:41 -07003140static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
3141 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142{
David Howellsc6f493d2015-03-17 22:26:22 +00003143 struct inode *inode = d_backing_inode(dentry);
Paul Moore20cdef82016-04-04 14:14:42 -04003144 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 struct superblock_security_struct *sbsec;
Thomas Liu2bf49692009-07-14 12:14:09 -04003146 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11003147 u32 newsid, sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 int rc = 0;
3149
Eric W. Biederman6b240302017-10-02 09:38:20 -05003150 if (strcmp(name, XATTR_NAME_SELINUX)) {
3151 rc = cap_inode_setxattr(dentry, name, value, size, flags);
3152 if (rc)
3153 return rc;
3154
3155 /* Not an attribute we recognize, so just check the
3156 ordinary setattr permission. */
3157 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159
3160 sbsec = inode->i_sb->s_security;
Eric Paris12f348b2012-10-09 10:56:25 -04003161 if (!(sbsec->flags & SBLABEL_MNT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 return -EOPNOTSUPP;
3163
Serge E. Hallyn2e149672011-03-23 16:43:26 -07003164 if (!inode_owner_or_capable(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 return -EPERM;
3166
Eric Paris50c205f2012-04-04 15:01:43 -04003167 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04003168 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169
Paul Moore20cdef82016-04-04 14:14:42 -04003170 isec = backing_inode_security(dentry);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003171 rc = avc_has_perm(&selinux_state,
3172 sid, isec->sid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 FILE__RELABELFROM, &ad);
3174 if (rc)
3175 return rc;
3176
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003177 rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3178 GFP_KERNEL);
Stephen Smalley12b29f32008-05-07 13:03:20 -04003179 if (rc == -EINVAL) {
Stephen Smalleydb590002017-04-20 11:31:30 -04003180 if (!has_cap_mac_admin(true)) {
Eric Parisd6ea83e2012-04-04 13:45:49 -04003181 struct audit_buffer *ab;
3182 size_t audit_size;
Eric Parisd6ea83e2012-04-04 13:45:49 -04003183
3184 /* We strip a nul only if it is at the end, otherwise the
3185 * context contains a nul and we should audit that */
Al Viroe3fea3f2012-06-09 08:15:16 +01003186 if (value) {
Colin Ian Kingadd24372017-10-14 13:46:55 +01003187 const char *str = value;
3188
Al Viroe3fea3f2012-06-09 08:15:16 +01003189 if (str[size - 1] == '\0')
3190 audit_size = size - 1;
3191 else
3192 audit_size = size;
3193 } else {
Al Viroe3fea3f2012-06-09 08:15:16 +01003194 audit_size = 0;
3195 }
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04003196 ab = audit_log_start(audit_context(),
3197 GFP_ATOMIC, AUDIT_SELINUX_ERR);
Eric Parisd6ea83e2012-04-04 13:45:49 -04003198 audit_log_format(ab, "op=setxattr invalid_context=");
3199 audit_log_n_untrustedstring(ab, value, audit_size);
3200 audit_log_end(ab);
3201
Stephen Smalley12b29f32008-05-07 13:03:20 -04003202 return rc;
Eric Parisd6ea83e2012-04-04 13:45:49 -04003203 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003204 rc = security_context_to_sid_force(&selinux_state, value,
3205 size, &newsid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04003206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 if (rc)
3208 return rc;
3209
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003210 rc = avc_has_perm(&selinux_state,
3211 sid, newsid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 FILE__RELABELTO, &ad);
3213 if (rc)
3214 return rc;
3215
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003216 rc = security_validate_transition(&selinux_state, isec->sid, newsid,
3217 sid, isec->sclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 if (rc)
3219 return rc;
3220
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003221 return avc_has_perm(&selinux_state,
3222 newsid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 sbsec->sid,
3224 SECCLASS_FILESYSTEM,
3225 FILESYSTEM__ASSOCIATE,
3226 &ad);
3227}
3228
David Howells8f0cfa52008-04-29 00:59:41 -07003229static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
Eric Parisf5269712008-05-14 11:27:45 -04003230 const void *value, size_t size,
David Howells8f0cfa52008-04-29 00:59:41 -07003231 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232{
David Howellsc6f493d2015-03-17 22:26:22 +00003233 struct inode *inode = d_backing_inode(dentry);
Paul Moore20cdef82016-04-04 14:14:42 -04003234 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 u32 newsid;
3236 int rc;
3237
3238 if (strcmp(name, XATTR_NAME_SELINUX)) {
3239 /* Not an attribute we recognize, so nothing to do. */
3240 return;
3241 }
3242
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003243 rc = security_context_to_sid_force(&selinux_state, value, size,
3244 &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 if (rc) {
peter enderborgc103a912018-06-12 10:09:03 +02003246 pr_err("SELinux: unable to map context to SID"
Stephen Smalley12b29f32008-05-07 13:03:20 -04003247 "for (%s, %lu), rc=%d\n",
3248 inode->i_sb->s_id, inode->i_ino, -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 return;
3250 }
3251
Paul Moore20cdef82016-04-04 14:14:42 -04003252 isec = backing_inode_security(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003253 spin_lock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003254 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05003256 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003257 spin_unlock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003258
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 return;
3260}
3261
David Howells8f0cfa52008-04-29 00:59:41 -07003262static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263{
David Howells88e67f32008-11-14 10:39:21 +11003264 const struct cred *cred = current_cred();
3265
Eric Paris2875fa02011-04-28 16:04:24 -04003266 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267}
3268
Eric Paris828dfe12008-04-17 13:17:49 -04003269static int selinux_inode_listxattr(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270{
David Howells88e67f32008-11-14 10:39:21 +11003271 const struct cred *cred = current_cred();
3272
Eric Paris2875fa02011-04-28 16:04:24 -04003273 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274}
3275
David Howells8f0cfa52008-04-29 00:59:41 -07003276static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277{
Eric W. Biederman6b240302017-10-02 09:38:20 -05003278 if (strcmp(name, XATTR_NAME_SELINUX)) {
3279 int rc = cap_inode_removexattr(dentry, name);
3280 if (rc)
3281 return rc;
3282
3283 /* Not an attribute we recognize, so just check the
3284 ordinary setattr permission. */
3285 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287
3288 /* No one is allowed to remove a SELinux security label.
3289 You can change the label, but all data must be labeled. */
3290 return -EACCES;
3291}
3292
James Morrisd381d8a2005-10-30 14:59:22 -08003293/*
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003294 * Copy the inode security context value to the user.
James Morrisd381d8a2005-10-30 14:59:22 -08003295 *
3296 * Permission check is handled by selinux_inode_getxattr hook.
3297 */
Andreas Gruenbacherea861df2015-12-24 11:09:39 -05003298static int selinux_inode_getsecurity(struct inode *inode, const char *name, void **buffer, bool alloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299{
David P. Quigley42492592008-02-04 22:29:39 -08003300 u32 size;
3301 int error;
3302 char *context = NULL;
Paul Moore20cdef82016-04-04 14:14:42 -04003303 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00003305 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3306 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003308 /*
3309 * If the caller has CAP_MAC_ADMIN, then get the raw context
3310 * value even if it is not defined by current policy; otherwise,
3311 * use the in-core value under current policy.
3312 * Use the non-auditing forms of the permission checks since
3313 * getxattr may be called by unprivileged processes commonly
3314 * and lack of permission just means that we fall back to the
3315 * in-core context value, not a denial.
3316 */
Paul Moore20cdef82016-04-04 14:14:42 -04003317 isec = inode_security(inode);
Stephen Smalleydb590002017-04-20 11:31:30 -04003318 if (has_cap_mac_admin(false))
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003319 error = security_sid_to_context_force(&selinux_state,
3320 isec->sid, &context,
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003321 &size);
3322 else
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003323 error = security_sid_to_context(&selinux_state, isec->sid,
3324 &context, &size);
David P. Quigley42492592008-02-04 22:29:39 -08003325 if (error)
3326 return error;
3327 error = size;
3328 if (alloc) {
3329 *buffer = context;
3330 goto out_nofree;
3331 }
3332 kfree(context);
3333out_nofree:
3334 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335}
3336
3337static int selinux_inode_setsecurity(struct inode *inode, const char *name,
Eric Paris828dfe12008-04-17 13:17:49 -04003338 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339{
Paul Moore2c971652016-04-19 16:36:28 -04003340 struct inode_security_struct *isec = inode_security_novalidate(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 u32 newsid;
3342 int rc;
3343
3344 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3345 return -EOPNOTSUPP;
3346
3347 if (!value || !size)
3348 return -EACCES;
3349
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003350 rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3351 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 if (rc)
3353 return rc;
3354
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003355 spin_lock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003356 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05003358 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003359 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 return 0;
3361}
3362
3363static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
3364{
3365 const int len = sizeof(XATTR_NAME_SELINUX);
3366 if (buffer && len <= buffer_size)
3367 memcpy(buffer, XATTR_NAME_SELINUX, len);
3368 return len;
3369}
3370
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05003371static void selinux_inode_getsecid(struct inode *inode, u32 *secid)
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02003372{
Andreas Gruenbachere817c2f2016-02-18 12:04:08 +01003373 struct inode_security_struct *isec = inode_security_novalidate(inode);
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02003374 *secid = isec->sid;
3375}
3376
Vivek Goyal56909eb2016-07-13 10:44:48 -04003377static int selinux_inode_copy_up(struct dentry *src, struct cred **new)
3378{
3379 u32 sid;
3380 struct task_security_struct *tsec;
3381 struct cred *new_creds = *new;
3382
3383 if (new_creds == NULL) {
3384 new_creds = prepare_creds();
3385 if (!new_creds)
3386 return -ENOMEM;
3387 }
3388
3389 tsec = new_creds->security;
3390 /* Get label from overlay inode and set it in create_sid */
3391 selinux_inode_getsecid(d_inode(src), &sid);
3392 tsec->create_sid = sid;
3393 *new = new_creds;
3394 return 0;
3395}
3396
Vivek Goyal19472b62016-07-13 10:44:50 -04003397static int selinux_inode_copy_up_xattr(const char *name)
3398{
3399 /* The copy_up hook above sets the initial context on an inode, but we
3400 * don't then want to overwrite it by blindly copying all the lower
3401 * xattrs up. Instead, we have to filter out SELinux-related xattrs.
3402 */
3403 if (strcmp(name, XATTR_NAME_SELINUX) == 0)
3404 return 1; /* Discard */
3405 /*
3406 * Any other attribute apart from SELINUX is not claimed, supported
3407 * by selinux.
3408 */
3409 return -EOPNOTSUPP;
3410}
3411
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412/* file security operations */
3413
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003414static int selinux_revalidate_file_permission(struct file *file, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415{
David Howells88e67f32008-11-14 10:39:21 +11003416 const struct cred *cred = current_cred();
Al Viro496ad9a2013-01-23 17:07:38 -05003417 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
3420 if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
3421 mask |= MAY_APPEND;
3422
Paul Moore389fb8002009-03-27 17:10:34 -04003423 return file_has_perm(cred, file,
3424 file_mask_to_av(inode->i_mode, mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425}
3426
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003427static int selinux_file_permission(struct file *file, int mask)
3428{
Al Viro496ad9a2013-01-23 17:07:38 -05003429 struct inode *inode = file_inode(file);
Stephen Smalley20dda182009-06-22 14:54:53 -04003430 struct file_security_struct *fsec = file->f_security;
Andreas Gruenbacherb1973672016-01-05 23:12:33 +01003431 struct inode_security_struct *isec;
Stephen Smalley20dda182009-06-22 14:54:53 -04003432 u32 sid = current_sid();
3433
Paul Moore389fb8002009-03-27 17:10:34 -04003434 if (!mask)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003435 /* No permission to check. Existence test. */
3436 return 0;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003437
Andreas Gruenbacherb1973672016-01-05 23:12:33 +01003438 isec = inode_security(inode);
Stephen Smalley20dda182009-06-22 14:54:53 -04003439 if (sid == fsec->sid && fsec->isid == isec->sid &&
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003440 fsec->pseqno == avc_policy_seqno(&selinux_state))
Eric Paris83d49852012-04-04 13:45:40 -04003441 /* No change since file_open check. */
Stephen Smalley20dda182009-06-22 14:54:53 -04003442 return 0;
3443
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003444 return selinux_revalidate_file_permission(file, mask);
3445}
3446
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447static int selinux_file_alloc_security(struct file *file)
3448{
3449 return file_alloc_security(file);
3450}
3451
3452static void selinux_file_free_security(struct file *file)
3453{
3454 file_free_security(file);
3455}
3456
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003457/*
3458 * Check whether a task has the ioctl permission and cmd
3459 * operation to an inode.
3460 */
Geliang Tang1d2a1682015-10-21 17:44:27 -04003461static int ioctl_has_perm(const struct cred *cred, struct file *file,
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003462 u32 requested, u16 cmd)
3463{
3464 struct common_audit_data ad;
3465 struct file_security_struct *fsec = file->f_security;
3466 struct inode *inode = file_inode(file);
Paul Moore20cdef82016-04-04 14:14:42 -04003467 struct inode_security_struct *isec;
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003468 struct lsm_ioctlop_audit ioctl;
3469 u32 ssid = cred_sid(cred);
3470 int rc;
3471 u8 driver = cmd >> 8;
3472 u8 xperm = cmd & 0xff;
3473
3474 ad.type = LSM_AUDIT_DATA_IOCTL_OP;
3475 ad.u.op = &ioctl;
3476 ad.u.op->cmd = cmd;
3477 ad.u.op->path = file->f_path;
3478
3479 if (ssid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003480 rc = avc_has_perm(&selinux_state,
3481 ssid, fsec->sid,
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003482 SECCLASS_FD,
3483 FD__USE,
3484 &ad);
3485 if (rc)
3486 goto out;
3487 }
3488
3489 if (unlikely(IS_PRIVATE(inode)))
3490 return 0;
3491
Paul Moore20cdef82016-04-04 14:14:42 -04003492 isec = inode_security(inode);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003493 rc = avc_has_extended_perms(&selinux_state,
3494 ssid, isec->sid, isec->sclass,
3495 requested, driver, xperm, &ad);
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003496out:
3497 return rc;
3498}
3499
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500static int selinux_file_ioctl(struct file *file, unsigned int cmd,
3501 unsigned long arg)
3502{
David Howells88e67f32008-11-14 10:39:21 +11003503 const struct cred *cred = current_cred();
Eric Paris0b24dcb2011-02-25 15:39:20 -05003504 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505
Eric Paris0b24dcb2011-02-25 15:39:20 -05003506 switch (cmd) {
3507 case FIONREAD:
3508 /* fall through */
3509 case FIBMAP:
3510 /* fall through */
3511 case FIGETBSZ:
3512 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003513 case FS_IOC_GETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003514 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003515 case FS_IOC_GETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003516 error = file_has_perm(cred, file, FILE__GETATTR);
3517 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518
Al Viro2f99c362012-03-23 16:04:05 -04003519 case FS_IOC_SETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003520 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003521 case FS_IOC_SETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003522 error = file_has_perm(cred, file, FILE__SETATTR);
3523 break;
3524
3525 /* sys_ioctl() checks */
3526 case FIONBIO:
3527 /* fall through */
3528 case FIOASYNC:
3529 error = file_has_perm(cred, file, 0);
3530 break;
3531
3532 case KDSKBENT:
3533 case KDSKBSENT:
Eric Paris6a9de492012-01-03 12:25:14 -05003534 error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04003535 SECURITY_CAP_AUDIT, true);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003536 break;
3537
3538 /* default case assumes that the command will go
3539 * to the file's ioctl() function.
3540 */
3541 default:
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003542 error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003543 }
3544 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545}
3546
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003547static int default_noexec;
3548
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
3550{
David Howells88e67f32008-11-14 10:39:21 +11003551 const struct cred *cred = current_cred();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003552 u32 sid = cred_sid(cred);
David Howellsd84f4f92008-11-14 10:39:23 +11003553 int rc = 0;
David Howells88e67f32008-11-14 10:39:21 +11003554
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003555 if (default_noexec &&
Stephen Smalley892e8ca2015-07-10 09:40:59 -04003556 (prot & PROT_EXEC) && (!file || IS_PRIVATE(file_inode(file)) ||
3557 (!shared && (prot & PROT_WRITE)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 /*
3559 * We are making executable an anonymous mapping or a
3560 * private file mapping that will also be writable.
3561 * This has an additional check.
3562 */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003563 rc = avc_has_perm(&selinux_state,
3564 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003565 PROCESS__EXECMEM, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 if (rc)
David Howellsd84f4f92008-11-14 10:39:23 +11003567 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569
3570 if (file) {
3571 /* read access is always possible with a mapping */
3572 u32 av = FILE__READ;
3573
3574 /* write access only matters if the mapping is shared */
3575 if (shared && (prot & PROT_WRITE))
3576 av |= FILE__WRITE;
3577
3578 if (prot & PROT_EXEC)
3579 av |= FILE__EXECUTE;
3580
David Howells88e67f32008-11-14 10:39:21 +11003581 return file_has_perm(cred, file, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 }
David Howellsd84f4f92008-11-14 10:39:23 +11003583
3584error:
3585 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586}
3587
Al Viroe5467852012-05-30 13:30:51 -04003588static int selinux_mmap_addr(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07003590 int rc = 0;
Paul Moore98883bf2014-03-19 16:46:11 -04003591
3592 if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
3593 u32 sid = current_sid();
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003594 rc = avc_has_perm(&selinux_state,
3595 sid, sid, SECCLASS_MEMPROTECT,
Paul Moore98883bf2014-03-19 16:46:11 -04003596 MEMPROTECT__MMAP_ZERO, NULL);
3597 }
3598
3599 return rc;
Al Viroe5467852012-05-30 13:30:51 -04003600}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601
Al Viroe5467852012-05-30 13:30:51 -04003602static int selinux_mmap_file(struct file *file, unsigned long reqprot,
3603 unsigned long prot, unsigned long flags)
3604{
Stephen Smalley3ba4bf52017-05-05 09:14:48 -04003605 struct common_audit_data ad;
3606 int rc;
3607
3608 if (file) {
3609 ad.type = LSM_AUDIT_DATA_FILE;
3610 ad.u.file = file;
3611 rc = inode_has_perm(current_cred(), file_inode(file),
3612 FILE__MAP, &ad);
3613 if (rc)
3614 return rc;
3615 }
3616
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003617 if (selinux_state.checkreqprot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 prot = reqprot;
3619
3620 return file_map_prot_check(file, prot,
3621 (flags & MAP_TYPE) == MAP_SHARED);
3622}
3623
3624static int selinux_file_mprotect(struct vm_area_struct *vma,
3625 unsigned long reqprot,
3626 unsigned long prot)
3627{
David Howells88e67f32008-11-14 10:39:21 +11003628 const struct cred *cred = current_cred();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003629 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003631 if (selinux_state.checkreqprot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 prot = reqprot;
3633
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003634 if (default_noexec &&
3635 (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
James Morrisd541bbe2009-01-29 12:19:51 +11003636 int rc = 0;
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003637 if (vma->vm_start >= vma->vm_mm->start_brk &&
3638 vma->vm_end <= vma->vm_mm->brk) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003639 rc = avc_has_perm(&selinux_state,
3640 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003641 PROCESS__EXECHEAP, NULL);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003642 } else if (!vma->vm_file &&
Stephen Smalleyc2316db2016-04-08 13:55:03 -04003643 ((vma->vm_start <= vma->vm_mm->start_stack &&
3644 vma->vm_end >= vma->vm_mm->start_stack) ||
Andy Lutomirskid17af502016-09-30 10:58:58 -07003645 vma_is_stack_for_current(vma))) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003646 rc = avc_has_perm(&selinux_state,
3647 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003648 PROCESS__EXECSTACK, NULL);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003649 } else if (vma->vm_file && vma->anon_vma) {
3650 /*
3651 * We are making executable a file mapping that has
3652 * had some COW done. Since pages might have been
3653 * written, check ability to execute the possibly
3654 * modified content. This typically should only
3655 * occur for text relocations.
3656 */
David Howellsd84f4f92008-11-14 10:39:23 +11003657 rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003658 }
Lorenzo Hernandez García-Hierro6b992192005-06-25 14:54:34 -07003659 if (rc)
3660 return rc;
3661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662
3663 return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
3664}
3665
3666static int selinux_file_lock(struct file *file, unsigned int cmd)
3667{
David Howells88e67f32008-11-14 10:39:21 +11003668 const struct cred *cred = current_cred();
3669
3670 return file_has_perm(cred, file, FILE__LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671}
3672
3673static int selinux_file_fcntl(struct file *file, unsigned int cmd,
3674 unsigned long arg)
3675{
David Howells88e67f32008-11-14 10:39:21 +11003676 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 int err = 0;
3678
3679 switch (cmd) {
Eric Paris828dfe12008-04-17 13:17:49 -04003680 case F_SETFL:
Eric Paris828dfe12008-04-17 13:17:49 -04003681 if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
David Howells88e67f32008-11-14 10:39:21 +11003682 err = file_has_perm(cred, file, FILE__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003684 }
3685 /* fall through */
3686 case F_SETOWN:
3687 case F_SETSIG:
3688 case F_GETFL:
3689 case F_GETOWN:
3690 case F_GETSIG:
Cyrill Gorcunov1d151c32012-07-30 14:43:00 -07003691 case F_GETOWNER_UIDS:
Eric Paris828dfe12008-04-17 13:17:49 -04003692 /* Just check FD__USE permission */
David Howells88e67f32008-11-14 10:39:21 +11003693 err = file_has_perm(cred, file, 0);
Eric Paris828dfe12008-04-17 13:17:49 -04003694 break;
3695 case F_GETLK:
3696 case F_SETLK:
3697 case F_SETLKW:
Jeff Layton0d3f7a22014-04-22 08:23:58 -04003698 case F_OFD_GETLK:
3699 case F_OFD_SETLK:
3700 case F_OFD_SETLKW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701#if BITS_PER_LONG == 32
Eric Paris828dfe12008-04-17 13:17:49 -04003702 case F_GETLK64:
3703 case F_SETLK64:
3704 case F_SETLKW64:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705#endif
David Howells88e67f32008-11-14 10:39:21 +11003706 err = file_has_perm(cred, file, FILE__LOCK);
Eric Paris828dfe12008-04-17 13:17:49 -04003707 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 }
3709
3710 return err;
3711}
3712
Jeff Laytone0b93ed2014-08-22 11:27:32 -04003713static void selinux_file_set_fowner(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 struct file_security_struct *fsec;
3716
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 fsec = file->f_security;
David Howells275bb412008-11-14 10:39:19 +11003718 fsec->fown_sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719}
3720
3721static int selinux_file_send_sigiotask(struct task_struct *tsk,
3722 struct fown_struct *fown, int signum)
3723{
Eric Paris828dfe12008-04-17 13:17:49 -04003724 struct file *file;
Stephen Smalley65c90bc2009-05-04 15:43:18 -04003725 u32 sid = task_sid(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 struct file_security_struct *fsec;
3728
3729 /* struct fown_struct is never outside the context of a struct file */
Eric Paris828dfe12008-04-17 13:17:49 -04003730 file = container_of(fown, struct file, f_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 fsec = file->f_security;
3733
3734 if (!signum)
3735 perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
3736 else
3737 perm = signal_to_av(signum);
3738
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003739 return avc_has_perm(&selinux_state,
3740 fsec->fown_sid, sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741 SECCLASS_PROCESS, perm, NULL);
3742}
3743
3744static int selinux_file_receive(struct file *file)
3745{
David Howells88e67f32008-11-14 10:39:21 +11003746 const struct cred *cred = current_cred();
3747
3748 return file_has_perm(cred, file, file_to_av(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749}
3750
Al Viro94817692018-07-10 14:13:18 -04003751static int selinux_file_open(struct file *file)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003752{
3753 struct file_security_struct *fsec;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003754 struct inode_security_struct *isec;
David Howellsd84f4f92008-11-14 10:39:23 +11003755
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003756 fsec = file->f_security;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05003757 isec = inode_security(file_inode(file));
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003758 /*
3759 * Save inode label and policy sequence number
3760 * at open-time so that selinux_file_permission
3761 * can determine whether revalidation is necessary.
3762 * Task label is already saved in the file security
3763 * struct as its SID.
3764 */
3765 fsec->isid = isec->sid;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003766 fsec->pseqno = avc_policy_seqno(&selinux_state);
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003767 /*
3768 * Since the inode label or policy seqno may have changed
3769 * between the selinux_inode_permission check and the saving
3770 * of state above, recheck that access is still permitted.
3771 * Otherwise, access might never be revalidated against the
3772 * new inode label or new policy.
3773 * This check is not redundant - do not remove.
3774 */
Al Viro94817692018-07-10 14:13:18 -04003775 return file_path_has_perm(file->f_cred, file, open_file_to_av(file));
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003776}
3777
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778/* task security operations */
3779
Tetsuo Handaa79be232017-03-28 23:08:45 +09003780static int selinux_task_alloc(struct task_struct *task,
3781 unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003783 u32 sid = current_sid();
3784
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003785 return avc_has_perm(&selinux_state,
3786 sid, sid, SECCLASS_PROCESS, PROCESS__FORK, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787}
3788
David Howellsf1752ee2008-11-14 10:39:17 +11003789/*
David Howellsee18d642009-09-02 09:14:21 +01003790 * allocate the SELinux part of blank credentials
3791 */
3792static int selinux_cred_alloc_blank(struct cred *cred, gfp_t gfp)
3793{
3794 struct task_security_struct *tsec;
3795
3796 tsec = kzalloc(sizeof(struct task_security_struct), gfp);
3797 if (!tsec)
3798 return -ENOMEM;
3799
3800 cred->security = tsec;
3801 return 0;
3802}
3803
3804/*
David Howellsf1752ee2008-11-14 10:39:17 +11003805 * detach and free the LSM part of a set of credentials
3806 */
3807static void selinux_cred_free(struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808{
David Howellsf1752ee2008-11-14 10:39:17 +11003809 struct task_security_struct *tsec = cred->security;
David Howellse0e81732009-09-02 09:13:40 +01003810
Tetsuo Handa2edeaa32011-02-07 13:36:10 +00003811 /*
3812 * cred->security == NULL if security_cred_alloc_blank() or
3813 * security_prepare_creds() returned an error.
3814 */
3815 BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE);
David Howellse0e81732009-09-02 09:13:40 +01003816 cred->security = (void *) 0x7UL;
David Howellsf1752ee2008-11-14 10:39:17 +11003817 kfree(tsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818}
3819
David Howellsd84f4f92008-11-14 10:39:23 +11003820/*
3821 * prepare a new set of credentials for modification
3822 */
3823static int selinux_cred_prepare(struct cred *new, const struct cred *old,
3824 gfp_t gfp)
3825{
3826 const struct task_security_struct *old_tsec;
3827 struct task_security_struct *tsec;
3828
3829 old_tsec = old->security;
3830
3831 tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp);
3832 if (!tsec)
3833 return -ENOMEM;
3834
3835 new->security = tsec;
3836 return 0;
3837}
3838
3839/*
David Howellsee18d642009-09-02 09:14:21 +01003840 * transfer the SELinux data to a blank set of creds
3841 */
3842static void selinux_cred_transfer(struct cred *new, const struct cred *old)
3843{
3844 const struct task_security_struct *old_tsec = old->security;
3845 struct task_security_struct *tsec = new->security;
3846
3847 *tsec = *old_tsec;
3848}
3849
Matthew Garrett3ec30112018-01-08 13:36:19 -08003850static void selinux_cred_getsecid(const struct cred *c, u32 *secid)
3851{
3852 *secid = cred_sid(c);
3853}
3854
David Howellsee18d642009-09-02 09:14:21 +01003855/*
David Howells3a3b7ce2008-11-14 10:39:28 +11003856 * set the security data for a kernel service
3857 * - all the creation contexts are set to unlabelled
3858 */
3859static int selinux_kernel_act_as(struct cred *new, u32 secid)
3860{
3861 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, secid,
David Howells3a3b7ce2008-11-14 10:39:28 +11003867 SECCLASS_KERNEL_SERVICE,
3868 KERNEL_SERVICE__USE_AS_OVERRIDE,
3869 NULL);
3870 if (ret == 0) {
3871 tsec->sid = secid;
3872 tsec->create_sid = 0;
3873 tsec->keycreate_sid = 0;
3874 tsec->sockcreate_sid = 0;
3875 }
3876 return ret;
3877}
3878
3879/*
3880 * set the file creation context in a security record to the same as the
3881 * objective context of the specified inode
3882 */
3883static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
3884{
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05003885 struct inode_security_struct *isec = inode_security(inode);
David Howells3a3b7ce2008-11-14 10:39:28 +11003886 struct task_security_struct *tsec = new->security;
3887 u32 sid = current_sid();
3888 int ret;
3889
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003890 ret = avc_has_perm(&selinux_state,
3891 sid, isec->sid,
David Howells3a3b7ce2008-11-14 10:39:28 +11003892 SECCLASS_KERNEL_SERVICE,
3893 KERNEL_SERVICE__CREATE_FILES_AS,
3894 NULL);
3895
3896 if (ret == 0)
3897 tsec->create_sid = isec->sid;
David Howellsef574712010-02-26 01:56:16 +00003898 return ret;
David Howells3a3b7ce2008-11-14 10:39:28 +11003899}
3900
Eric Parisdd8dbf22009-11-03 16:35:32 +11003901static int selinux_kernel_module_request(char *kmod_name)
Eric Paris25354c42009-08-13 09:45:03 -04003902{
Eric Parisdd8dbf22009-11-03 16:35:32 +11003903 struct common_audit_data ad;
3904
Eric Paris50c205f2012-04-04 15:01:43 -04003905 ad.type = LSM_AUDIT_DATA_KMOD;
Eric Parisdd8dbf22009-11-03 16:35:32 +11003906 ad.u.kmod_name = kmod_name;
3907
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003908 return avc_has_perm(&selinux_state,
3909 current_sid(), SECINITSID_KERNEL, SECCLASS_SYSTEM,
Eric Parisdd8dbf22009-11-03 16:35:32 +11003910 SYSTEM__MODULE_REQUEST, &ad);
Eric Paris25354c42009-08-13 09:45:03 -04003911}
3912
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003913static int selinux_kernel_module_from_file(struct file *file)
3914{
3915 struct common_audit_data ad;
3916 struct inode_security_struct *isec;
3917 struct file_security_struct *fsec;
3918 u32 sid = current_sid();
3919 int rc;
3920
3921 /* init_module */
3922 if (file == NULL)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003923 return avc_has_perm(&selinux_state,
3924 sid, sid, SECCLASS_SYSTEM,
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003925 SYSTEM__MODULE_LOAD, NULL);
3926
3927 /* finit_module */
Paul Moore20cdef82016-04-04 14:14:42 -04003928
Vivek Goyal43af5de2016-09-09 11:37:49 -04003929 ad.type = LSM_AUDIT_DATA_FILE;
3930 ad.u.file = file;
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003931
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003932 fsec = file->f_security;
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003933 if (sid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003934 rc = avc_has_perm(&selinux_state,
3935 sid, fsec->sid, SECCLASS_FD, FD__USE, &ad);
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003936 if (rc)
3937 return rc;
3938 }
3939
Paul Moore20cdef82016-04-04 14:14:42 -04003940 isec = inode_security(file_inode(file));
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003941 return avc_has_perm(&selinux_state,
3942 sid, isec->sid, SECCLASS_SYSTEM,
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003943 SYSTEM__MODULE_LOAD, &ad);
3944}
3945
3946static int selinux_kernel_read_file(struct file *file,
3947 enum kernel_read_file_id id)
3948{
3949 int rc = 0;
3950
3951 switch (id) {
3952 case READING_MODULE:
3953 rc = selinux_kernel_module_from_file(file);
3954 break;
3955 default:
3956 break;
3957 }
3958
3959 return rc;
3960}
3961
Mimi Zoharc77b8cd2018-07-13 14:06:02 -04003962static int selinux_kernel_load_data(enum kernel_load_data_id id)
3963{
3964 int rc = 0;
3965
3966 switch (id) {
3967 case LOADING_MODULE:
3968 rc = selinux_kernel_module_from_file(NULL);
3969 default:
3970 break;
3971 }
3972
3973 return rc;
3974}
3975
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
3977{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003978 return avc_has_perm(&selinux_state,
3979 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003980 PROCESS__SETPGID, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981}
3982
3983static int selinux_task_getpgid(struct task_struct *p)
3984{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003985 return avc_has_perm(&selinux_state,
3986 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003987 PROCESS__GETPGID, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988}
3989
3990static int selinux_task_getsid(struct task_struct *p)
3991{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003992 return avc_has_perm(&selinux_state,
3993 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003994 PROCESS__GETSESSION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995}
3996
David Quigleyf9008e4c2006-06-30 01:55:46 -07003997static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
3998{
David Howells275bb412008-11-14 10:39:19 +11003999 *secid = task_sid(p);
David Quigleyf9008e4c2006-06-30 01:55:46 -07004000}
4001
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002static int selinux_task_setnice(struct task_struct *p, int nice)
4003{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004004 return avc_has_perm(&selinux_state,
4005 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004006 PROCESS__SETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007}
4008
James Morris03e68062006-06-23 02:03:58 -07004009static int selinux_task_setioprio(struct task_struct *p, int ioprio)
4010{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004011 return avc_has_perm(&selinux_state,
4012 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004013 PROCESS__SETSCHED, NULL);
James Morris03e68062006-06-23 02:03:58 -07004014}
4015
David Quigleya1836a42006-06-30 01:55:49 -07004016static int selinux_task_getioprio(struct task_struct *p)
4017{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004018 return avc_has_perm(&selinux_state,
4019 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004020 PROCESS__GETSCHED, NULL);
David Quigleya1836a42006-06-30 01:55:49 -07004021}
4022
Corentin LABBE42985552017-10-04 20:32:18 +02004023static int selinux_task_prlimit(const struct cred *cred, const struct cred *tcred,
4024 unsigned int flags)
Stephen Smalley791ec492017-02-17 07:57:00 -05004025{
4026 u32 av = 0;
4027
Stephen Smalley84e68852017-02-28 09:35:08 -05004028 if (!flags)
4029 return 0;
Stephen Smalley791ec492017-02-17 07:57:00 -05004030 if (flags & LSM_PRLIMIT_WRITE)
4031 av |= PROCESS__SETRLIMIT;
4032 if (flags & LSM_PRLIMIT_READ)
4033 av |= PROCESS__GETRLIMIT;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004034 return avc_has_perm(&selinux_state,
4035 cred_sid(cred), cred_sid(tcred),
Stephen Smalley791ec492017-02-17 07:57:00 -05004036 SECCLASS_PROCESS, av, NULL);
4037}
4038
Jiri Slaby8fd00b42009-08-26 18:41:16 +02004039static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
4040 struct rlimit *new_rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041{
Jiri Slaby8fd00b42009-08-26 18:41:16 +02004042 struct rlimit *old_rlim = p->signal->rlim + resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043
4044 /* Control the ability to change the hard limit (whether
4045 lowering or raising it), so that the hard limit can
4046 later be used as a safe reset point for the soft limit
David Howellsd84f4f92008-11-14 10:39:23 +11004047 upon context transitions. See selinux_bprm_committing_creds. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 if (old_rlim->rlim_max != new_rlim->rlim_max)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004049 return avc_has_perm(&selinux_state,
4050 current_sid(), task_sid(p),
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004051 SECCLASS_PROCESS, PROCESS__SETRLIMIT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052
4053 return 0;
4054}
4055
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09004056static int selinux_task_setscheduler(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004058 return avc_has_perm(&selinux_state,
4059 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004060 PROCESS__SETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061}
4062
4063static int selinux_task_getscheduler(struct task_struct *p)
4064{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004065 return avc_has_perm(&selinux_state,
4066 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004067 PROCESS__GETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068}
4069
David Quigley35601542006-06-23 02:04:01 -07004070static int selinux_task_movememory(struct task_struct *p)
4071{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004072 return avc_has_perm(&selinux_state,
4073 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004074 PROCESS__SETSCHED, NULL);
David Quigley35601542006-06-23 02:04:01 -07004075}
4076
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02004077static int selinux_task_kill(struct task_struct *p, struct kernel_siginfo *info,
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004078 int sig, const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079{
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004080 u32 secid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 if (!sig)
4084 perm = PROCESS__SIGNULL; /* null signal; existence test */
4085 else
4086 perm = signal_to_av(sig);
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004087 if (!cred)
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004088 secid = current_sid();
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004089 else
4090 secid = cred_sid(cred);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004091 return avc_has_perm(&selinux_state,
4092 secid, task_sid(p), SECCLASS_PROCESS, perm, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093}
4094
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095static void selinux_task_to_inode(struct task_struct *p,
4096 struct inode *inode)
4097{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 struct inode_security_struct *isec = inode->i_security;
David Howells275bb412008-11-14 10:39:19 +11004099 u32 sid = task_sid(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004101 spin_lock(&isec->lock);
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01004102 isec->sclass = inode_mode_to_security_class(inode->i_mode);
David Howells275bb412008-11-14 10:39:19 +11004103 isec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004104 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004105 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106}
4107
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004109static int selinux_parse_skb_ipv4(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04004110 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111{
4112 int offset, ihlen, ret = -EINVAL;
4113 struct iphdr _iph, *ih;
4114
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03004115 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
4117 if (ih == NULL)
4118 goto out;
4119
4120 ihlen = ih->ihl * 4;
4121 if (ihlen < sizeof(_iph))
4122 goto out;
4123
Eric Paris48c62af2012-04-02 13:15:44 -04004124 ad->u.net->v4info.saddr = ih->saddr;
4125 ad->u.net->v4info.daddr = ih->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 ret = 0;
4127
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004128 if (proto)
4129 *proto = ih->protocol;
4130
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 switch (ih->protocol) {
Eric Paris828dfe12008-04-17 13:17:49 -04004132 case IPPROTO_TCP: {
4133 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134
Eric Paris828dfe12008-04-17 13:17:49 -04004135 if (ntohs(ih->frag_off) & IP_OFFSET)
4136 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137
4138 offset += ihlen;
4139 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4140 if (th == NULL)
4141 break;
4142
Eric Paris48c62af2012-04-02 13:15:44 -04004143 ad->u.net->sport = th->source;
4144 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147
Eric Paris828dfe12008-04-17 13:17:49 -04004148 case IPPROTO_UDP: {
4149 struct udphdr _udph, *uh;
4150
4151 if (ntohs(ih->frag_off) & IP_OFFSET)
4152 break;
4153
4154 offset += ihlen;
4155 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4156 if (uh == NULL)
4157 break;
4158
Eric Paris48c62af2012-04-02 13:15:44 -04004159 ad->u.net->sport = uh->source;
4160 ad->u.net->dport = uh->dest;
Eric Paris828dfe12008-04-17 13:17:49 -04004161 break;
4162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163
James Morris2ee92d42006-11-13 16:09:01 -08004164 case IPPROTO_DCCP: {
4165 struct dccp_hdr _dccph, *dh;
4166
4167 if (ntohs(ih->frag_off) & IP_OFFSET)
4168 break;
4169
4170 offset += ihlen;
4171 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4172 if (dh == NULL)
4173 break;
4174
Eric Paris48c62af2012-04-02 13:15:44 -04004175 ad->u.net->sport = dh->dccph_sport;
4176 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08004177 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004178 }
James Morris2ee92d42006-11-13 16:09:01 -08004179
Richard Hainesd4529302018-02-13 20:57:18 +00004180#if IS_ENABLED(CONFIG_IP_SCTP)
4181 case IPPROTO_SCTP: {
4182 struct sctphdr _sctph, *sh;
4183
4184 if (ntohs(ih->frag_off) & IP_OFFSET)
4185 break;
4186
4187 offset += ihlen;
4188 sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4189 if (sh == NULL)
4190 break;
4191
4192 ad->u.net->sport = sh->source;
4193 ad->u.net->dport = sh->dest;
4194 break;
4195 }
4196#endif
Eric Paris828dfe12008-04-17 13:17:49 -04004197 default:
4198 break;
4199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200out:
4201 return ret;
4202}
4203
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04004204#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205
4206/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004207static int selinux_parse_skb_ipv6(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04004208 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209{
4210 u8 nexthdr;
4211 int ret = -EINVAL, offset;
4212 struct ipv6hdr _ipv6h, *ip6;
Jesse Gross75f28112011-11-30 17:05:51 -08004213 __be16 frag_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03004215 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
4217 if (ip6 == NULL)
4218 goto out;
4219
Eric Paris48c62af2012-04-02 13:15:44 -04004220 ad->u.net->v6info.saddr = ip6->saddr;
4221 ad->u.net->v6info.daddr = ip6->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222 ret = 0;
4223
4224 nexthdr = ip6->nexthdr;
4225 offset += sizeof(_ipv6h);
Jesse Gross75f28112011-11-30 17:05:51 -08004226 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 if (offset < 0)
4228 goto out;
4229
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004230 if (proto)
4231 *proto = nexthdr;
4232
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 switch (nexthdr) {
4234 case IPPROTO_TCP: {
Eric Paris828dfe12008-04-17 13:17:49 -04004235 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236
4237 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4238 if (th == NULL)
4239 break;
4240
Eric Paris48c62af2012-04-02 13:15:44 -04004241 ad->u.net->sport = th->source;
4242 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 break;
4244 }
4245
4246 case IPPROTO_UDP: {
4247 struct udphdr _udph, *uh;
4248
4249 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4250 if (uh == NULL)
4251 break;
4252
Eric Paris48c62af2012-04-02 13:15:44 -04004253 ad->u.net->sport = uh->source;
4254 ad->u.net->dport = uh->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 break;
4256 }
4257
James Morris2ee92d42006-11-13 16:09:01 -08004258 case IPPROTO_DCCP: {
4259 struct dccp_hdr _dccph, *dh;
4260
4261 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4262 if (dh == NULL)
4263 break;
4264
Eric Paris48c62af2012-04-02 13:15:44 -04004265 ad->u.net->sport = dh->dccph_sport;
4266 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08004267 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004268 }
James Morris2ee92d42006-11-13 16:09:01 -08004269
Richard Hainesd4529302018-02-13 20:57:18 +00004270#if IS_ENABLED(CONFIG_IP_SCTP)
4271 case IPPROTO_SCTP: {
4272 struct sctphdr _sctph, *sh;
4273
4274 sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4275 if (sh == NULL)
4276 break;
4277
4278 ad->u.net->sport = sh->source;
4279 ad->u.net->dport = sh->dest;
4280 break;
4281 }
4282#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 /* includes fragments */
4284 default:
4285 break;
4286 }
4287out:
4288 return ret;
4289}
4290
4291#endif /* IPV6 */
4292
Thomas Liu2bf49692009-07-14 12:14:09 -04004293static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
David Howellscf9481e2008-07-27 21:31:07 +10004294 char **_addrp, int src, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295{
David Howellscf9481e2008-07-27 21:31:07 +10004296 char *addrp;
4297 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298
Eric Paris48c62af2012-04-02 13:15:44 -04004299 switch (ad->u.net->family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 case PF_INET:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004301 ret = selinux_parse_skb_ipv4(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10004302 if (ret)
4303 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04004304 addrp = (char *)(src ? &ad->u.net->v4info.saddr :
4305 &ad->u.net->v4info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10004306 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04004308#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 case PF_INET6:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004310 ret = selinux_parse_skb_ipv6(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10004311 if (ret)
4312 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04004313 addrp = (char *)(src ? &ad->u.net->v6info.saddr :
4314 &ad->u.net->v6info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10004315 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316#endif /* IPV6 */
4317 default:
David Howellscf9481e2008-07-27 21:31:07 +10004318 addrp = NULL;
4319 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 }
4321
David Howellscf9481e2008-07-27 21:31:07 +10004322parse_error:
peter enderborgc103a912018-06-12 10:09:03 +02004323 pr_warn(
David Howellscf9481e2008-07-27 21:31:07 +10004324 "SELinux: failure in selinux_parse_skb(),"
4325 " unable to parse packet\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 return ret;
David Howellscf9481e2008-07-27 21:31:07 +10004327
4328okay:
4329 if (_addrp)
4330 *_addrp = addrp;
4331 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332}
4333
Paul Moore4f6a9932007-03-01 14:35:22 -05004334/**
Paul Moore220deb92008-01-29 08:38:23 -05004335 * selinux_skb_peerlbl_sid - Determine the peer label of a packet
Paul Moore4f6a9932007-03-01 14:35:22 -05004336 * @skb: the packet
Paul Moore75e22912008-01-29 08:38:04 -05004337 * @family: protocol family
Paul Moore220deb92008-01-29 08:38:23 -05004338 * @sid: the packet's peer label SID
Paul Moore4f6a9932007-03-01 14:35:22 -05004339 *
4340 * Description:
Paul Moore220deb92008-01-29 08:38:23 -05004341 * Check the various different forms of network peer labeling and determine
4342 * the peer label/SID for the packet; most of the magic actually occurs in
4343 * the security server function security_net_peersid_cmp(). The function
4344 * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
4345 * or -EACCES if @sid is invalid due to inconsistencies with the different
4346 * peer labels.
Paul Moore4f6a9932007-03-01 14:35:22 -05004347 *
4348 */
Paul Moore220deb92008-01-29 08:38:23 -05004349static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
Paul Moore4f6a9932007-03-01 14:35:22 -05004350{
Paul Moore71f1cb02008-01-29 08:51:16 -05004351 int err;
Paul Moore4f6a9932007-03-01 14:35:22 -05004352 u32 xfrm_sid;
4353 u32 nlbl_sid;
Paul Moore220deb92008-01-29 08:38:23 -05004354 u32 nlbl_type;
Paul Moore4f6a9932007-03-01 14:35:22 -05004355
Paul Moore817eff72013-12-10 14:57:54 -05004356 err = selinux_xfrm_skb_sid(skb, &xfrm_sid);
Paul Moorebed4d7e2013-07-23 17:38:40 -04004357 if (unlikely(err))
4358 return -EACCES;
4359 err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
4360 if (unlikely(err))
4361 return -EACCES;
Paul Moore220deb92008-01-29 08:38:23 -05004362
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004363 err = security_net_peersid_resolve(&selinux_state, nlbl_sid,
4364 nlbl_type, xfrm_sid, sid);
Paul Moore71f1cb02008-01-29 08:51:16 -05004365 if (unlikely(err)) {
peter enderborgc103a912018-06-12 10:09:03 +02004366 pr_warn(
Paul Moore71f1cb02008-01-29 08:51:16 -05004367 "SELinux: failure in selinux_skb_peerlbl_sid(),"
4368 " unable to determine packet's peer label\n");
Paul Moore220deb92008-01-29 08:38:23 -05004369 return -EACCES;
Paul Moore71f1cb02008-01-29 08:51:16 -05004370 }
Paul Moore220deb92008-01-29 08:38:23 -05004371
4372 return 0;
Paul Moore4f6a9932007-03-01 14:35:22 -05004373}
4374
Paul Moore446b8022013-12-04 16:10:51 -05004375/**
4376 * selinux_conn_sid - Determine the child socket label for a connection
4377 * @sk_sid: the parent socket's SID
4378 * @skb_sid: the packet's SID
4379 * @conn_sid: the resulting connection SID
4380 *
4381 * If @skb_sid is valid then the user:role:type information from @sk_sid is
4382 * combined with the MLS information from @skb_sid in order to create
4383 * @conn_sid. If @skb_sid is not valid then then @conn_sid is simply a copy
4384 * of @sk_sid. Returns zero on success, negative values on failure.
4385 *
4386 */
4387static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid)
4388{
4389 int err = 0;
4390
4391 if (skb_sid != SECSID_NULL)
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004392 err = security_sid_mls_copy(&selinux_state, sk_sid, skb_sid,
4393 conn_sid);
Paul Moore446b8022013-12-04 16:10:51 -05004394 else
4395 *conn_sid = sk_sid;
4396
4397 return err;
4398}
4399
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400/* socket security operations */
Paul Moored4f2d972010-04-22 14:46:18 -04004401
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004402static int socket_sockcreate_sid(const struct task_security_struct *tsec,
4403 u16 secclass, u32 *socksid)
Paul Moored4f2d972010-04-22 14:46:18 -04004404{
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004405 if (tsec->sockcreate_sid > SECSID_NULL) {
4406 *socksid = tsec->sockcreate_sid;
4407 return 0;
4408 }
4409
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004410 return security_transition_sid(&selinux_state, tsec->sid, tsec->sid,
4411 secclass, NULL, socksid);
Paul Moored4f2d972010-04-22 14:46:18 -04004412}
4413
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004414static int sock_has_perm(struct sock *sk, u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415{
Paul Moore253bfae2010-04-22 14:46:19 -04004416 struct sk_security_struct *sksec = sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004417 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004418 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419
Paul Moore253bfae2010-04-22 14:46:19 -04004420 if (sksec->sid == SECINITSID_KERNEL)
4421 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422
Eric Paris50c205f2012-04-04 15:01:43 -04004423 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004424 ad.u.net = &net;
4425 ad.u.net->sk = sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004427 return avc_has_perm(&selinux_state,
4428 current_sid(), sksec->sid, sksec->sclass, perms,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004429 &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430}
4431
4432static int selinux_socket_create(int family, int type,
4433 int protocol, int kern)
4434{
Paul Moore5fb49872010-04-22 14:46:19 -04004435 const struct task_security_struct *tsec = current_security();
Paul Moored4f2d972010-04-22 14:46:18 -04004436 u32 newsid;
David Howells275bb412008-11-14 10:39:19 +11004437 u16 secclass;
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004438 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439
4440 if (kern)
Paul Moored4f2d972010-04-22 14:46:18 -04004441 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442
David Howells275bb412008-11-14 10:39:19 +11004443 secclass = socket_type_to_security_class(family, type, protocol);
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004444 rc = socket_sockcreate_sid(tsec, secclass, &newsid);
4445 if (rc)
4446 return rc;
4447
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004448 return avc_has_perm(&selinux_state,
4449 tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450}
4451
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07004452static int selinux_socket_post_create(struct socket *sock, int family,
4453 int type, int protocol, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454{
Paul Moore5fb49872010-04-22 14:46:19 -04004455 const struct task_security_struct *tsec = current_security();
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05004456 struct inode_security_struct *isec = inode_security_novalidate(SOCK_INODE(sock));
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004457 struct sk_security_struct *sksec;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004458 u16 sclass = socket_type_to_security_class(family, type, protocol);
4459 u32 sid = SECINITSID_KERNEL;
David Howells275bb412008-11-14 10:39:19 +11004460 int err = 0;
4461
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004462 if (!kern) {
4463 err = socket_sockcreate_sid(tsec, sclass, &sid);
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004464 if (err)
4465 return err;
4466 }
David Howells275bb412008-11-14 10:39:19 +11004467
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004468 isec->sclass = sclass;
4469 isec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004470 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004472 if (sock->sk) {
4473 sksec = sock->sk->sk_security;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004474 sksec->sclass = sclass;
4475 sksec->sid = sid;
Richard Hainesd4529302018-02-13 20:57:18 +00004476 /* Allows detection of the first association on this socket */
4477 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4478 sksec->sctp_assoc_state = SCTP_ASSOC_UNSET;
4479
Paul Moore389fb8002009-03-27 17:10:34 -04004480 err = selinux_netlbl_socket_post_create(sock->sk, family);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004481 }
4482
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07004483 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484}
4485
David Herrmann0b811db2018-05-04 16:28:21 +02004486static int selinux_socket_socketpair(struct socket *socka,
4487 struct socket *sockb)
4488{
4489 struct sk_security_struct *sksec_a = socka->sk->sk_security;
4490 struct sk_security_struct *sksec_b = sockb->sk->sk_security;
4491
4492 sksec_a->peer_sid = sksec_b->sid;
4493 sksec_b->peer_sid = sksec_a->sid;
4494
4495 return 0;
4496}
4497
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498/* Range of port numbers used to automatically bind.
4499 Need to determine whether we should perform a name_bind
4500 permission check between the socket and the port number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501
4502static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
4503{
Paul Moore253bfae2010-04-22 14:46:19 -04004504 struct sock *sk = sock->sk;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004505 struct sk_security_struct *sksec = sk->sk_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 u16 family;
4507 int err;
4508
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004509 err = sock_has_perm(sk, SOCKET__BIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 if (err)
4511 goto out;
4512
Richard Hainesd4529302018-02-13 20:57:18 +00004513 /* If PF_INET or PF_INET6, check name_bind permission for the port. */
Paul Moore253bfae2010-04-22 14:46:19 -04004514 family = sk->sk_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 if (family == PF_INET || family == PF_INET6) {
4516 char *addrp;
Thomas Liu2bf49692009-07-14 12:14:09 -04004517 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004518 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 struct sockaddr_in *addr4 = NULL;
4520 struct sockaddr_in6 *addr6 = NULL;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004521 u16 family_sa = address->sa_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522 unsigned short snum;
James Morrise399f982008-06-12 01:39:58 +10004523 u32 sid, node_perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524
Richard Hainesd4529302018-02-13 20:57:18 +00004525 /*
4526 * sctp_bindx(3) calls via selinux_sctp_bind_connect()
4527 * that validates multiple binding addresses. Because of this
4528 * need to check address->sa_family as it is possible to have
4529 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4530 */
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004531 switch (family_sa) {
4532 case AF_UNSPEC:
Richard Haines68741a8a2018-03-02 19:54:34 +00004533 case AF_INET:
4534 if (addrlen < sizeof(struct sockaddr_in))
4535 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536 addr4 = (struct sockaddr_in *)address;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004537 if (family_sa == AF_UNSPEC) {
4538 /* see __inet_bind(), we only want to allow
4539 * AF_UNSPEC if the address is INADDR_ANY
4540 */
4541 if (addr4->sin_addr.s_addr != htonl(INADDR_ANY))
4542 goto err_af;
4543 family_sa = AF_INET;
4544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545 snum = ntohs(addr4->sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546 addrp = (char *)&addr4->sin_addr.s_addr;
Richard Haines68741a8a2018-03-02 19:54:34 +00004547 break;
4548 case AF_INET6:
4549 if (addrlen < SIN6_LEN_RFC2133)
4550 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 addr6 = (struct sockaddr_in6 *)address;
4552 snum = ntohs(addr6->sin6_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 addrp = (char *)&addr6->sin6_addr.s6_addr;
Richard Haines68741a8a2018-03-02 19:54:34 +00004554 break;
4555 default:
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004556 goto err_af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 }
4558
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004559 ad.type = LSM_AUDIT_DATA_NET;
4560 ad.u.net = &net;
4561 ad.u.net->sport = htons(snum);
4562 ad.u.net->family = family_sa;
4563
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004564 if (snum) {
4565 int low, high;
4566
Eric W. Biederman0bbf87d2013-09-28 14:10:59 -07004567 inet_get_local_port_range(sock_net(sk), &low, &high);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004568
Krister Johansen4548b682017-01-20 17:49:11 -08004569 if (snum < max(inet_prot_sock(sock_net(sk)), low) ||
4570 snum > high) {
Paul Moore3e1121722008-04-10 10:48:14 -04004571 err = sel_netport_sid(sk->sk_protocol,
4572 snum, &sid);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004573 if (err)
4574 goto out;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004575 err = avc_has_perm(&selinux_state,
4576 sksec->sid, sid,
Paul Moore253bfae2010-04-22 14:46:19 -04004577 sksec->sclass,
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004578 SOCKET__NAME_BIND, &ad);
4579 if (err)
4580 goto out;
4581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 }
Eric Paris828dfe12008-04-17 13:17:49 -04004583
Paul Moore253bfae2010-04-22 14:46:19 -04004584 switch (sksec->sclass) {
James Morris13402582005-09-30 14:24:34 -04004585 case SECCLASS_TCP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 node_perm = TCP_SOCKET__NODE_BIND;
4587 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004588
James Morris13402582005-09-30 14:24:34 -04004589 case SECCLASS_UDP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 node_perm = UDP_SOCKET__NODE_BIND;
4591 break;
James Morris2ee92d42006-11-13 16:09:01 -08004592
4593 case SECCLASS_DCCP_SOCKET:
4594 node_perm = DCCP_SOCKET__NODE_BIND;
4595 break;
4596
Richard Hainesd4529302018-02-13 20:57:18 +00004597 case SECCLASS_SCTP_SOCKET:
4598 node_perm = SCTP_SOCKET__NODE_BIND;
4599 break;
4600
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601 default:
4602 node_perm = RAWIP_SOCKET__NODE_BIND;
4603 break;
4604 }
Eric Paris828dfe12008-04-17 13:17:49 -04004605
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004606 err = sel_netnode_sid(addrp, family_sa, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 if (err)
4608 goto out;
Eric Paris828dfe12008-04-17 13:17:49 -04004609
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004610 if (family_sa == AF_INET)
Eric Paris48c62af2012-04-02 13:15:44 -04004611 ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 else
Eric Paris48c62af2012-04-02 13:15:44 -04004613 ad.u.net->v6info.saddr = addr6->sin6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004615 err = avc_has_perm(&selinux_state,
4616 sksec->sid, sid,
Paul Moore253bfae2010-04-22 14:46:19 -04004617 sksec->sclass, node_perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 if (err)
4619 goto out;
4620 }
4621out:
4622 return err;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004623err_af:
4624 /* Note that SCTP services expect -EINVAL, others -EAFNOSUPPORT. */
4625 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4626 return -EINVAL;
4627 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628}
4629
Richard Hainesd4529302018-02-13 20:57:18 +00004630/* This supports connect(2) and SCTP connect services such as sctp_connectx(3)
Mauro Carvalho Chehab5fb94e92018-05-08 15:14:57 -03004631 * and sctp_sendmsg(3) as described in Documentation/security/LSM-sctp.rst
Richard Hainesd4529302018-02-13 20:57:18 +00004632 */
4633static int selinux_socket_connect_helper(struct socket *sock,
4634 struct sockaddr *address, int addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635{
Paul Moore014ab192008-10-10 10:16:33 -04004636 struct sock *sk = sock->sk;
Paul Moore253bfae2010-04-22 14:46:19 -04004637 struct sk_security_struct *sksec = sk->sk_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 int err;
4639
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004640 err = sock_has_perm(sk, SOCKET__CONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 if (err)
4642 return err;
4643
4644 /*
Richard Hainesd4529302018-02-13 20:57:18 +00004645 * If a TCP, DCCP or SCTP socket, check name_connect permission
4646 * for the port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 */
Paul Moore253bfae2010-04-22 14:46:19 -04004648 if (sksec->sclass == SECCLASS_TCP_SOCKET ||
Richard Hainesd4529302018-02-13 20:57:18 +00004649 sksec->sclass == SECCLASS_DCCP_SOCKET ||
4650 sksec->sclass == SECCLASS_SCTP_SOCKET) {
Thomas Liu2bf49692009-07-14 12:14:09 -04004651 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004652 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 struct sockaddr_in *addr4 = NULL;
4654 struct sockaddr_in6 *addr6 = NULL;
4655 unsigned short snum;
James Morris2ee92d42006-11-13 16:09:01 -08004656 u32 sid, perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657
Richard Hainesd4529302018-02-13 20:57:18 +00004658 /* sctp_connectx(3) calls via selinux_sctp_bind_connect()
4659 * that validates multiple connect addresses. Because of this
4660 * need to check address->sa_family as it is possible to have
4661 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4662 */
Richard Haines68741a8a2018-03-02 19:54:34 +00004663 switch (address->sa_family) {
4664 case AF_INET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 addr4 = (struct sockaddr_in *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004666 if (addrlen < sizeof(struct sockaddr_in))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 return -EINVAL;
4668 snum = ntohs(addr4->sin_port);
Richard Haines68741a8a2018-03-02 19:54:34 +00004669 break;
4670 case AF_INET6:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 addr6 = (struct sockaddr_in6 *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004672 if (addrlen < SIN6_LEN_RFC2133)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 return -EINVAL;
4674 snum = ntohs(addr6->sin6_port);
Richard Haines68741a8a2018-03-02 19:54:34 +00004675 break;
4676 default:
4677 /* Note that SCTP services expect -EINVAL, whereas
4678 * others expect -EAFNOSUPPORT.
4679 */
4680 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4681 return -EINVAL;
4682 else
4683 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 }
4685
Paul Moore3e1121722008-04-10 10:48:14 -04004686 err = sel_netport_sid(sk->sk_protocol, snum, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687 if (err)
Richard Hainesd4529302018-02-13 20:57:18 +00004688 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689
Richard Hainesd4529302018-02-13 20:57:18 +00004690 switch (sksec->sclass) {
4691 case SECCLASS_TCP_SOCKET:
4692 perm = TCP_SOCKET__NAME_CONNECT;
4693 break;
4694 case SECCLASS_DCCP_SOCKET:
4695 perm = DCCP_SOCKET__NAME_CONNECT;
4696 break;
4697 case SECCLASS_SCTP_SOCKET:
4698 perm = SCTP_SOCKET__NAME_CONNECT;
4699 break;
4700 }
James Morris2ee92d42006-11-13 16:09:01 -08004701
Eric Paris50c205f2012-04-04 15:01:43 -04004702 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004703 ad.u.net = &net;
4704 ad.u.net->dport = htons(snum);
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004705 ad.u.net->family = address->sa_family;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004706 err = avc_has_perm(&selinux_state,
4707 sksec->sid, sid, sksec->sclass, perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708 if (err)
Richard Hainesd4529302018-02-13 20:57:18 +00004709 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710 }
4711
Richard Hainesd4529302018-02-13 20:57:18 +00004712 return 0;
4713}
Paul Moore014ab192008-10-10 10:16:33 -04004714
Richard Hainesd4529302018-02-13 20:57:18 +00004715/* Supports connect(2), see comments in selinux_socket_connect_helper() */
4716static int selinux_socket_connect(struct socket *sock,
4717 struct sockaddr *address, int addrlen)
4718{
4719 int err;
4720 struct sock *sk = sock->sk;
4721
4722 err = selinux_socket_connect_helper(sock, address, addrlen);
4723 if (err)
4724 return err;
4725
4726 return selinux_netlbl_socket_connect(sk, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727}
4728
4729static int selinux_socket_listen(struct socket *sock, int backlog)
4730{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004731 return sock_has_perm(sock->sk, SOCKET__LISTEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732}
4733
4734static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
4735{
4736 int err;
4737 struct inode_security_struct *isec;
4738 struct inode_security_struct *newisec;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004739 u16 sclass;
4740 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004742 err = sock_has_perm(sock->sk, SOCKET__ACCEPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743 if (err)
4744 return err;
4745
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05004746 isec = inode_security_novalidate(SOCK_INODE(sock));
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004747 spin_lock(&isec->lock);
4748 sclass = isec->sclass;
4749 sid = isec->sid;
4750 spin_unlock(&isec->lock);
4751
4752 newisec = inode_security_novalidate(SOCK_INODE(newsock));
4753 newisec->sclass = sclass;
4754 newisec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004755 newisec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756
4757 return 0;
4758}
4759
4760static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
Eric Paris828dfe12008-04-17 13:17:49 -04004761 int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004763 return sock_has_perm(sock->sk, SOCKET__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764}
4765
4766static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
4767 int size, int flags)
4768{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004769 return sock_has_perm(sock->sk, SOCKET__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770}
4771
4772static int selinux_socket_getsockname(struct socket *sock)
4773{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004774 return sock_has_perm(sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775}
4776
4777static int selinux_socket_getpeername(struct socket *sock)
4778{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004779 return sock_has_perm(sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780}
4781
Eric Paris828dfe12008-04-17 13:17:49 -04004782static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783{
Paul Mooref8687af2006-10-30 15:22:15 -08004784 int err;
4785
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004786 err = sock_has_perm(sock->sk, SOCKET__SETOPT);
Paul Mooref8687af2006-10-30 15:22:15 -08004787 if (err)
4788 return err;
4789
4790 return selinux_netlbl_socket_setsockopt(sock, level, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791}
4792
4793static int selinux_socket_getsockopt(struct socket *sock, int level,
4794 int optname)
4795{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004796 return sock_has_perm(sock->sk, SOCKET__GETOPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797}
4798
4799static int selinux_socket_shutdown(struct socket *sock, int how)
4800{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004801 return sock_has_perm(sock->sk, SOCKET__SHUTDOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802}
4803
David S. Miller3610cda2011-01-05 15:38:53 -08004804static int selinux_socket_unix_stream_connect(struct sock *sock,
4805 struct sock *other,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806 struct sock *newsk)
4807{
David S. Miller3610cda2011-01-05 15:38:53 -08004808 struct sk_security_struct *sksec_sock = sock->sk_security;
4809 struct sk_security_struct *sksec_other = other->sk_security;
Paul Moore4d1e2452010-04-22 14:46:18 -04004810 struct sk_security_struct *sksec_new = newsk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004811 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004812 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 int err;
4814
Eric Paris50c205f2012-04-04 15:01:43 -04004815 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004816 ad.u.net = &net;
4817 ad.u.net->sk = other;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004819 err = avc_has_perm(&selinux_state,
4820 sksec_sock->sid, sksec_other->sid,
Paul Moore4d1e2452010-04-22 14:46:18 -04004821 sksec_other->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822 UNIX_STREAM_SOCKET__CONNECTTO, &ad);
4823 if (err)
4824 return err;
4825
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826 /* server child socket */
Paul Moore4d1e2452010-04-22 14:46:18 -04004827 sksec_new->peer_sid = sksec_sock->sid;
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004828 err = security_sid_mls_copy(&selinux_state, sksec_other->sid,
4829 sksec_sock->sid, &sksec_new->sid);
Paul Moore4d1e2452010-04-22 14:46:18 -04004830 if (err)
4831 return err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004832
Paul Moore4d1e2452010-04-22 14:46:18 -04004833 /* connecting socket */
4834 sksec_sock->peer_sid = sksec_new->sid;
4835
4836 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837}
4838
4839static int selinux_socket_unix_may_send(struct socket *sock,
4840 struct socket *other)
4841{
Paul Moore253bfae2010-04-22 14:46:19 -04004842 struct sk_security_struct *ssec = sock->sk->sk_security;
4843 struct sk_security_struct *osec = other->sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004844 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004845 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846
Eric Paris50c205f2012-04-04 15:01:43 -04004847 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004848 ad.u.net = &net;
4849 ad.u.net->sk = other->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004851 return avc_has_perm(&selinux_state,
4852 ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
Paul Moore253bfae2010-04-22 14:46:19 -04004853 &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854}
4855
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004856static int selinux_inet_sys_rcv_skb(struct net *ns, int ifindex,
4857 char *addrp, u16 family, u32 peer_sid,
Thomas Liu2bf49692009-07-14 12:14:09 -04004858 struct common_audit_data *ad)
Paul Mooreeffad8d2008-01-29 08:49:27 -05004859{
4860 int err;
4861 u32 if_sid;
4862 u32 node_sid;
4863
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004864 err = sel_netif_sid(ns, ifindex, &if_sid);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004865 if (err)
4866 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004867 err = avc_has_perm(&selinux_state,
4868 peer_sid, if_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004869 SECCLASS_NETIF, NETIF__INGRESS, ad);
4870 if (err)
4871 return err;
4872
4873 err = sel_netnode_sid(addrp, family, &node_sid);
4874 if (err)
4875 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004876 return avc_has_perm(&selinux_state,
4877 peer_sid, node_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004878 SECCLASS_NODE, NODE__RECVFROM, ad);
4879}
4880
Paul Moore220deb92008-01-29 08:38:23 -05004881static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
Paul Moored8395c82008-10-10 10:16:30 -04004882 u16 family)
Paul Moore220deb92008-01-29 08:38:23 -05004883{
Paul Moore277d3422008-12-31 12:54:11 -05004884 int err = 0;
Paul Moore220deb92008-01-29 08:38:23 -05004885 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004886 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004887 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004888 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04004889 char *addrp;
4890
Eric Paris50c205f2012-04-04 15:01:43 -04004891 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004892 ad.u.net = &net;
4893 ad.u.net->netif = skb->skb_iif;
4894 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004895 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4896 if (err)
4897 return err;
Paul Moore220deb92008-01-29 08:38:23 -05004898
Paul Moore58bfbb52009-03-27 17:10:41 -04004899 if (selinux_secmark_enabled()) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004900 err = avc_has_perm(&selinux_state,
4901 sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Moored8395c82008-10-10 10:16:30 -04004902 PACKET__RECV, &ad);
Paul Moore58bfbb52009-03-27 17:10:41 -04004903 if (err)
4904 return err;
4905 }
Paul Moore220deb92008-01-29 08:38:23 -05004906
Steffen Klassertb9679a72011-02-23 12:55:21 +01004907 err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
4908 if (err)
4909 return err;
4910 err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004911
James Morris4e5ab4c2006-06-09 00:33:33 -07004912 return err;
4913}
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004914
James Morris4e5ab4c2006-06-09 00:33:33 -07004915static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
4916{
Paul Moore220deb92008-01-29 08:38:23 -05004917 int err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004918 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004919 u16 family = sk->sk_family;
4920 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004921 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004922 struct lsm_network_audit net = {0,};
Paul Moore220deb92008-01-29 08:38:23 -05004923 char *addrp;
Paul Moored8395c82008-10-10 10:16:30 -04004924 u8 secmark_active;
4925 u8 peerlbl_active;
James Morris4e5ab4c2006-06-09 00:33:33 -07004926
James Morris4e5ab4c2006-06-09 00:33:33 -07004927 if (family != PF_INET && family != PF_INET6)
Paul Moore220deb92008-01-29 08:38:23 -05004928 return 0;
James Morris4e5ab4c2006-06-09 00:33:33 -07004929
4930 /* Handle mapped IPv4 packets arriving via IPv6 sockets */
Al Viro87fcd702006-12-04 22:00:55 +00004931 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
James Morris4e5ab4c2006-06-09 00:33:33 -07004932 family = PF_INET;
4933
Paul Moored8395c82008-10-10 10:16:30 -04004934 /* If any sort of compatibility mode is enabled then handoff processing
4935 * to the selinux_sock_rcv_skb_compat() function to deal with the
4936 * special handling. We do this in an attempt to keep this function
4937 * as fast and as clean as possible. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004938 if (!selinux_policycap_netpeer())
Paul Moored8395c82008-10-10 10:16:30 -04004939 return selinux_sock_rcv_skb_compat(sk, skb, family);
4940
4941 secmark_active = selinux_secmark_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04004942 peerlbl_active = selinux_peerlbl_enabled();
Paul Moored8395c82008-10-10 10:16:30 -04004943 if (!secmark_active && !peerlbl_active)
4944 return 0;
4945
Eric Paris50c205f2012-04-04 15:01:43 -04004946 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004947 ad.u.net = &net;
4948 ad.u.net->netif = skb->skb_iif;
4949 ad.u.net->family = family;
Paul Moore224dfbd2008-01-29 08:38:13 -05004950 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
James Morris4e5ab4c2006-06-09 00:33:33 -07004951 if (err)
Paul Moore220deb92008-01-29 08:38:23 -05004952 return err;
James Morris4e5ab4c2006-06-09 00:33:33 -07004953
Paul Moored8395c82008-10-10 10:16:30 -04004954 if (peerlbl_active) {
Paul Moored621d352008-01-29 08:43:36 -05004955 u32 peer_sid;
4956
4957 err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
4958 if (err)
4959 return err;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004960 err = selinux_inet_sys_rcv_skb(sock_net(sk), skb->skb_iif,
4961 addrp, family, peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04004962 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04004963 selinux_netlbl_err(skb, family, err, 0);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004964 return err;
Paul Mooredfaebe92008-10-10 10:16:31 -04004965 }
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004966 err = avc_has_perm(&selinux_state,
4967 sk_sid, peer_sid, SECCLASS_PEER,
Paul Moored621d352008-01-29 08:43:36 -05004968 PEER__RECV, &ad);
Chad Hanson46d01d62013-12-23 17:45:01 -05004969 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04004970 selinux_netlbl_err(skb, family, err, 0);
Chad Hanson46d01d62013-12-23 17:45:01 -05004971 return err;
4972 }
Paul Moored621d352008-01-29 08:43:36 -05004973 }
4974
Paul Moored8395c82008-10-10 10:16:30 -04004975 if (secmark_active) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004976 err = avc_has_perm(&selinux_state,
4977 sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004978 PACKET__RECV, &ad);
4979 if (err)
4980 return err;
4981 }
4982
Paul Moored621d352008-01-29 08:43:36 -05004983 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984}
4985
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004986static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
4987 int __user *optlen, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988{
4989 int err = 0;
4990 char *scontext;
4991 u32 scontext_len;
Paul Moore253bfae2010-04-22 14:46:19 -04004992 struct sk_security_struct *sksec = sock->sk->sk_security;
Paul Moore3de4bab2006-11-17 17:38:54 -05004993 u32 peer_sid = SECSID_NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994
Paul Moore253bfae2010-04-22 14:46:19 -04004995 if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
Richard Hainesd4529302018-02-13 20:57:18 +00004996 sksec->sclass == SECCLASS_TCP_SOCKET ||
4997 sksec->sclass == SECCLASS_SCTP_SOCKET)
Eric Parisdd3e7832010-04-07 15:08:46 -04004998 peer_sid = sksec->peer_sid;
Paul Moore253bfae2010-04-22 14:46:19 -04004999 if (peer_sid == SECSID_NULL)
5000 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005002 err = security_sid_to_context(&selinux_state, peer_sid, &scontext,
5003 &scontext_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004 if (err)
Paul Moore253bfae2010-04-22 14:46:19 -04005005 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006
5007 if (scontext_len > len) {
5008 err = -ERANGE;
5009 goto out_len;
5010 }
5011
5012 if (copy_to_user(optval, scontext, scontext_len))
5013 err = -EFAULT;
5014
5015out_len:
5016 if (put_user(scontext_len, optlen))
5017 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018 kfree(scontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 return err;
5020}
5021
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005022static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
Catherine Zhang2c7946a2006-03-20 22:41:23 -08005023{
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005024 u32 peer_secid = SECSID_NULL;
Paul Moore75e22912008-01-29 08:38:04 -05005025 u16 family;
Paul Moore899134f2016-03-28 15:19:10 -04005026 struct inode_security_struct *isec;
Catherine Zhang877ce7c2006-06-29 12:27:47 -07005027
Paul Mooreaa862902008-10-10 10:16:29 -04005028 if (skb && skb->protocol == htons(ETH_P_IP))
5029 family = PF_INET;
5030 else if (skb && skb->protocol == htons(ETH_P_IPV6))
5031 family = PF_INET6;
5032 else if (sock)
Paul Moore75e22912008-01-29 08:38:04 -05005033 family = sock->sk->sk_family;
Paul Moore75e22912008-01-29 08:38:04 -05005034 else
5035 goto out;
5036
Paul Moore899134f2016-03-28 15:19:10 -04005037 if (sock && family == PF_UNIX) {
5038 isec = inode_security_novalidate(SOCK_INODE(sock));
5039 peer_secid = isec->sid;
5040 } else if (skb)
Paul Moore220deb92008-01-29 08:38:23 -05005041 selinux_skb_peerlbl_sid(skb, family, &peer_secid);
Catherine Zhang2c7946a2006-03-20 22:41:23 -08005042
Paul Moore75e22912008-01-29 08:38:04 -05005043out:
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005044 *secid = peer_secid;
Paul Moore75e22912008-01-29 08:38:04 -05005045 if (peer_secid == SECSID_NULL)
5046 return -EINVAL;
5047 return 0;
Catherine Zhang2c7946a2006-03-20 22:41:23 -08005048}
5049
Al Viro7d877f32005-10-21 03:20:43 -04005050static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051{
Paul Moore84914b72010-04-22 14:46:18 -04005052 struct sk_security_struct *sksec;
5053
5054 sksec = kzalloc(sizeof(*sksec), priority);
5055 if (!sksec)
5056 return -ENOMEM;
5057
5058 sksec->peer_sid = SECINITSID_UNLABELED;
5059 sksec->sid = SECINITSID_UNLABELED;
Stephen Smalley5dee25d2015-07-10 17:19:57 -04005060 sksec->sclass = SECCLASS_SOCKET;
Paul Moore84914b72010-04-22 14:46:18 -04005061 selinux_netlbl_sk_security_reset(sksec);
5062 sk->sk_security = sksec;
5063
5064 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065}
5066
5067static void selinux_sk_free_security(struct sock *sk)
5068{
Paul Moore84914b72010-04-22 14:46:18 -04005069 struct sk_security_struct *sksec = sk->sk_security;
5070
5071 sk->sk_security = NULL;
5072 selinux_netlbl_sk_security_free(sksec);
5073 kfree(sksec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074}
5075
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005076static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
5077{
Eric Parisdd3e7832010-04-07 15:08:46 -04005078 struct sk_security_struct *sksec = sk->sk_security;
5079 struct sk_security_struct *newsksec = newsk->sk_security;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005080
Eric Parisdd3e7832010-04-07 15:08:46 -04005081 newsksec->sid = sksec->sid;
5082 newsksec->peer_sid = sksec->peer_sid;
5083 newsksec->sclass = sksec->sclass;
Paul Moore99f59ed2006-08-29 17:53:48 -07005084
Eric Parisdd3e7832010-04-07 15:08:46 -04005085 selinux_netlbl_sk_security_reset(newsksec);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005086}
5087
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07005088static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005089{
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005090 if (!sk)
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07005091 *secid = SECINITSID_ANY_SOCKET;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005092 else {
5093 struct sk_security_struct *sksec = sk->sk_security;
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005094
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07005095 *secid = sksec->sid;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005096 }
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005097}
5098
Eric Paris828dfe12008-04-17 13:17:49 -04005099static void selinux_sock_graft(struct sock *sk, struct socket *parent)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005100{
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05005101 struct inode_security_struct *isec =
5102 inode_security_novalidate(SOCK_INODE(parent));
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005103 struct sk_security_struct *sksec = sk->sk_security;
5104
Paul Moore2873ead2014-07-28 10:42:48 -04005105 if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
5106 sk->sk_family == PF_UNIX)
David Woodhouse2148ccc2006-09-29 15:50:25 -07005107 isec->sid = sksec->sid;
Paul Moore220deb92008-01-29 08:38:23 -05005108 sksec->sclass = isec->sclass;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005109}
5110
Richard Hainesd4529302018-02-13 20:57:18 +00005111/* Called whenever SCTP receives an INIT chunk. This happens when an incoming
5112 * connect(2), sctp_connectx(3) or sctp_sendmsg(3) (with no association
5113 * already present).
5114 */
5115static int selinux_sctp_assoc_request(struct sctp_endpoint *ep,
5116 struct sk_buff *skb)
5117{
5118 struct sk_security_struct *sksec = ep->base.sk->sk_security;
5119 struct common_audit_data ad;
5120 struct lsm_network_audit net = {0,};
5121 u8 peerlbl_active;
5122 u32 peer_sid = SECINITSID_UNLABELED;
5123 u32 conn_sid;
5124 int err = 0;
5125
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005126 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005127 return 0;
5128
5129 peerlbl_active = selinux_peerlbl_enabled();
5130
5131 if (peerlbl_active) {
5132 /* This will return peer_sid = SECSID_NULL if there are
5133 * no peer labels, see security_net_peersid_resolve().
5134 */
5135 err = selinux_skb_peerlbl_sid(skb, ep->base.sk->sk_family,
5136 &peer_sid);
5137 if (err)
5138 return err;
5139
5140 if (peer_sid == SECSID_NULL)
5141 peer_sid = SECINITSID_UNLABELED;
5142 }
5143
5144 if (sksec->sctp_assoc_state == SCTP_ASSOC_UNSET) {
5145 sksec->sctp_assoc_state = SCTP_ASSOC_SET;
5146
5147 /* Here as first association on socket. As the peer SID
5148 * was allowed by peer recv (and the netif/node checks),
5149 * then it is approved by policy and used as the primary
5150 * peer SID for getpeercon(3).
5151 */
5152 sksec->peer_sid = peer_sid;
5153 } else if (sksec->peer_sid != peer_sid) {
5154 /* Other association peer SIDs are checked to enforce
5155 * consistency among the peer SIDs.
5156 */
5157 ad.type = LSM_AUDIT_DATA_NET;
5158 ad.u.net = &net;
5159 ad.u.net->sk = ep->base.sk;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005160 err = avc_has_perm(&selinux_state,
5161 sksec->peer_sid, peer_sid, sksec->sclass,
Richard Hainesd4529302018-02-13 20:57:18 +00005162 SCTP_SOCKET__ASSOCIATION, &ad);
5163 if (err)
5164 return err;
5165 }
5166
5167 /* Compute the MLS component for the connection and store
5168 * the information in ep. This will be used by SCTP TCP type
5169 * sockets and peeled off connections as they cause a new
5170 * socket to be generated. selinux_sctp_sk_clone() will then
5171 * plug this into the new socket.
5172 */
5173 err = selinux_conn_sid(sksec->sid, peer_sid, &conn_sid);
5174 if (err)
5175 return err;
5176
5177 ep->secid = conn_sid;
5178 ep->peer_secid = peer_sid;
5179
5180 /* Set any NetLabel labels including CIPSO/CALIPSO options. */
5181 return selinux_netlbl_sctp_assoc_request(ep, skb);
5182}
5183
5184/* Check if sctp IPv4/IPv6 addresses are valid for binding or connecting
5185 * based on their @optname.
5186 */
5187static int selinux_sctp_bind_connect(struct sock *sk, int optname,
5188 struct sockaddr *address,
5189 int addrlen)
5190{
5191 int len, err = 0, walk_size = 0;
5192 void *addr_buf;
5193 struct sockaddr *addr;
5194 struct socket *sock;
5195
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005196 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005197 return 0;
5198
5199 /* Process one or more addresses that may be IPv4 or IPv6 */
5200 sock = sk->sk_socket;
5201 addr_buf = address;
5202
5203 while (walk_size < addrlen) {
5204 addr = addr_buf;
5205 switch (addr->sa_family) {
Alexey Kodanev4152dc92018-05-11 20:15:13 +03005206 case AF_UNSPEC:
Richard Hainesd4529302018-02-13 20:57:18 +00005207 case AF_INET:
5208 len = sizeof(struct sockaddr_in);
5209 break;
5210 case AF_INET6:
5211 len = sizeof(struct sockaddr_in6);
5212 break;
5213 default:
Alexey Kodanev4152dc92018-05-11 20:15:13 +03005214 return -EINVAL;
Richard Hainesd4529302018-02-13 20:57:18 +00005215 }
5216
5217 err = -EINVAL;
5218 switch (optname) {
5219 /* Bind checks */
5220 case SCTP_PRIMARY_ADDR:
5221 case SCTP_SET_PEER_PRIMARY_ADDR:
5222 case SCTP_SOCKOPT_BINDX_ADD:
5223 err = selinux_socket_bind(sock, addr, len);
5224 break;
5225 /* Connect checks */
5226 case SCTP_SOCKOPT_CONNECTX:
5227 case SCTP_PARAM_SET_PRIMARY:
5228 case SCTP_PARAM_ADD_IP:
5229 case SCTP_SENDMSG_CONNECT:
5230 err = selinux_socket_connect_helper(sock, addr, len);
5231 if (err)
5232 return err;
5233
5234 /* As selinux_sctp_bind_connect() is called by the
5235 * SCTP protocol layer, the socket is already locked,
5236 * therefore selinux_netlbl_socket_connect_locked() is
5237 * is called here. The situations handled are:
5238 * sctp_connectx(3), sctp_sendmsg(3), sendmsg(2),
5239 * whenever a new IP address is added or when a new
5240 * primary address is selected.
5241 * Note that an SCTP connect(2) call happens before
5242 * the SCTP protocol layer and is handled via
5243 * selinux_socket_connect().
5244 */
5245 err = selinux_netlbl_socket_connect_locked(sk, addr);
5246 break;
5247 }
5248
5249 if (err)
5250 return err;
5251
5252 addr_buf += len;
5253 walk_size += len;
5254 }
5255
5256 return 0;
5257}
5258
5259/* Called whenever a new socket is created by accept(2) or sctp_peeloff(3). */
5260static void selinux_sctp_sk_clone(struct sctp_endpoint *ep, struct sock *sk,
5261 struct sock *newsk)
5262{
5263 struct sk_security_struct *sksec = sk->sk_security;
5264 struct sk_security_struct *newsksec = newsk->sk_security;
5265
5266 /* If policy does not support SECCLASS_SCTP_SOCKET then call
5267 * the non-sctp clone version.
5268 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005269 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005270 return selinux_sk_clone_security(sk, newsk);
5271
5272 newsksec->sid = ep->secid;
5273 newsksec->peer_sid = ep->peer_secid;
5274 newsksec->sclass = sksec->sclass;
5275 selinux_netlbl_sctp_sk_clone(sk, newsk);
5276}
5277
Adrian Bunk9a673e52006-08-15 00:03:53 -07005278static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
5279 struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005280{
5281 struct sk_security_struct *sksec = sk->sk_security;
5282 int err;
Paul Moore0b1f24e2013-12-03 11:39:13 -05005283 u16 family = req->rsk_ops->family;
Paul Moore446b8022013-12-04 16:10:51 -05005284 u32 connsid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005285 u32 peersid;
5286
Paul Mooreaa862902008-10-10 10:16:29 -04005287 err = selinux_skb_peerlbl_sid(skb, family, &peersid);
Paul Moore220deb92008-01-29 08:38:23 -05005288 if (err)
5289 return err;
Paul Moore446b8022013-12-04 16:10:51 -05005290 err = selinux_conn_sid(sksec->sid, peersid, &connsid);
5291 if (err)
5292 return err;
5293 req->secid = connsid;
5294 req->peer_secid = peersid;
Venkat Yekkiralaa51c64f2006-07-27 22:01:34 -07005295
Paul Moore389fb8002009-03-27 17:10:34 -04005296 return selinux_netlbl_inet_conn_request(req, family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005297}
5298
Adrian Bunk9a673e52006-08-15 00:03:53 -07005299static void selinux_inet_csk_clone(struct sock *newsk,
5300 const struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005301{
5302 struct sk_security_struct *newsksec = newsk->sk_security;
5303
5304 newsksec->sid = req->secid;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005305 newsksec->peer_sid = req->peer_secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005306 /* NOTE: Ideally, we should also get the isec->sid for the
5307 new socket in sync, but we don't have the isec available yet.
5308 So we will wait until sock_graft to do it, by which
5309 time it will have been created and available. */
Paul Moore99f59ed2006-08-29 17:53:48 -07005310
Paul Moore9f2ad662006-11-17 17:38:53 -05005311 /* We don't need to take any sort of lock here as we are the only
5312 * thread with access to newsksec */
Paul Moore389fb8002009-03-27 17:10:34 -04005313 selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005314}
5315
Paul Moore014ab192008-10-10 10:16:33 -04005316static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005317{
Paul Mooreaa862902008-10-10 10:16:29 -04005318 u16 family = sk->sk_family;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005319 struct sk_security_struct *sksec = sk->sk_security;
5320
Paul Mooreaa862902008-10-10 10:16:29 -04005321 /* handle mapped IPv4 packets arriving via IPv6 sockets */
5322 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
5323 family = PF_INET;
5324
5325 selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005326}
5327
Eric Paris2606fd12010-10-13 16:24:41 -04005328static int selinux_secmark_relabel_packet(u32 sid)
5329{
5330 const struct task_security_struct *__tsec;
5331 u32 tsid;
5332
5333 __tsec = current_security();
5334 tsid = __tsec->sid;
5335
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005336 return avc_has_perm(&selinux_state,
5337 tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO,
5338 NULL);
Eric Paris2606fd12010-10-13 16:24:41 -04005339}
5340
5341static void selinux_secmark_refcount_inc(void)
5342{
5343 atomic_inc(&selinux_secmark_refcount);
5344}
5345
5346static void selinux_secmark_refcount_dec(void)
5347{
5348 atomic_dec(&selinux_secmark_refcount);
5349}
5350
Adrian Bunk9a673e52006-08-15 00:03:53 -07005351static void selinux_req_classify_flow(const struct request_sock *req,
5352 struct flowi *fl)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005353{
David S. Miller1d28f422011-03-12 00:29:39 -05005354 fl->flowi_secid = req->secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005355}
5356
Paul Moore5dbbaf22013-01-14 07:12:19 +00005357static int selinux_tun_dev_alloc_security(void **security)
5358{
5359 struct tun_security_struct *tunsec;
5360
5361 tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL);
5362 if (!tunsec)
5363 return -ENOMEM;
5364 tunsec->sid = current_sid();
5365
5366 *security = tunsec;
5367 return 0;
5368}
5369
5370static void selinux_tun_dev_free_security(void *security)
5371{
5372 kfree(security);
5373}
5374
Paul Mooreed6d76e2009-08-28 18:12:49 -04005375static int selinux_tun_dev_create(void)
5376{
5377 u32 sid = current_sid();
5378
5379 /* we aren't taking into account the "sockcreate" SID since the socket
5380 * that is being created here is not a socket in the traditional sense,
5381 * instead it is a private sock, accessible only to the kernel, and
5382 * representing a wide range of network traffic spanning multiple
5383 * connections unlike traditional sockets - check the TUN driver to
5384 * get a better understanding of why this socket is special */
5385
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005386 return avc_has_perm(&selinux_state,
5387 sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005388 NULL);
5389}
5390
Paul Moore5dbbaf22013-01-14 07:12:19 +00005391static int selinux_tun_dev_attach_queue(void *security)
Paul Mooreed6d76e2009-08-28 18:12:49 -04005392{
Paul Moore5dbbaf22013-01-14 07:12:19 +00005393 struct tun_security_struct *tunsec = security;
5394
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005395 return avc_has_perm(&selinux_state,
5396 current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET,
Paul Moore5dbbaf22013-01-14 07:12:19 +00005397 TUN_SOCKET__ATTACH_QUEUE, NULL);
5398}
5399
5400static int selinux_tun_dev_attach(struct sock *sk, void *security)
5401{
5402 struct tun_security_struct *tunsec = security;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005403 struct sk_security_struct *sksec = sk->sk_security;
5404
5405 /* we don't currently perform any NetLabel based labeling here and it
5406 * isn't clear that we would want to do so anyway; while we could apply
5407 * labeling without the support of the TUN user the resulting labeled
5408 * traffic from the other end of the connection would almost certainly
5409 * cause confusion to the TUN user that had no idea network labeling
5410 * protocols were being used */
5411
Paul Moore5dbbaf22013-01-14 07:12:19 +00005412 sksec->sid = tunsec->sid;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005413 sksec->sclass = SECCLASS_TUN_SOCKET;
Paul Moore5dbbaf22013-01-14 07:12:19 +00005414
5415 return 0;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005416}
5417
Paul Moore5dbbaf22013-01-14 07:12:19 +00005418static int selinux_tun_dev_open(void *security)
Paul Mooreed6d76e2009-08-28 18:12:49 -04005419{
Paul Moore5dbbaf22013-01-14 07:12:19 +00005420 struct tun_security_struct *tunsec = security;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005421 u32 sid = current_sid();
5422 int err;
5423
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005424 err = avc_has_perm(&selinux_state,
5425 sid, tunsec->sid, SECCLASS_TUN_SOCKET,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005426 TUN_SOCKET__RELABELFROM, NULL);
5427 if (err)
5428 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005429 err = avc_has_perm(&selinux_state,
5430 sid, sid, SECCLASS_TUN_SOCKET,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005431 TUN_SOCKET__RELABELTO, NULL);
5432 if (err)
5433 return err;
Paul Moore5dbbaf22013-01-14 07:12:19 +00005434 tunsec->sid = sid;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005435
5436 return 0;
5437}
5438
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
5440{
5441 int err = 0;
5442 u32 perm;
5443 struct nlmsghdr *nlh;
Paul Moore253bfae2010-04-22 14:46:19 -04005444 struct sk_security_struct *sksec = sk->sk_security;
Eric Paris828dfe12008-04-17 13:17:49 -04005445
Hong zhi guo77954982013-03-27 06:49:35 +00005446 if (skb->len < NLMSG_HDRLEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447 err = -EINVAL;
5448 goto out;
5449 }
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -07005450 nlh = nlmsg_hdr(skb);
Eric Paris828dfe12008-04-17 13:17:49 -04005451
Paul Moore253bfae2010-04-22 14:46:19 -04005452 err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453 if (err) {
5454 if (err == -EINVAL) {
Vladis Dronov76319942015-12-24 11:09:41 -05005455 pr_warn_ratelimited("SELinux: unrecognized netlink"
5456 " message: protocol=%hu nlmsg_type=%hu sclass=%s"
5457 " pig=%d comm=%s\n",
Marek Milkoviccded3ff2015-06-04 16:22:16 -04005458 sk->sk_protocol, nlh->nlmsg_type,
Vladis Dronov76319942015-12-24 11:09:41 -05005459 secclass_map[sksec->sclass - 1].name,
5460 task_pid_nr(current), current->comm);
Paul Mooree5a5ca92018-03-01 17:38:30 -05005461 if (!enforcing_enabled(&selinux_state) ||
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005462 security_get_allow_unknown(&selinux_state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463 err = 0;
5464 }
5465
5466 /* Ignore */
5467 if (err == -ENOENT)
5468 err = 0;
5469 goto out;
5470 }
5471
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005472 err = sock_has_perm(sk, perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473out:
5474 return err;
5475}
5476
5477#ifdef CONFIG_NETFILTER
5478
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005479static unsigned int selinux_ip_forward(struct sk_buff *skb,
5480 const struct net_device *indev,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005481 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482{
Paul Mooredfaebe92008-10-10 10:16:31 -04005483 int err;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005484 char *addrp;
5485 u32 peer_sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04005486 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005487 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05005488 u8 secmark_active;
Paul Moore948bf852008-10-10 10:16:32 -04005489 u8 netlbl_active;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005490 u8 peerlbl_active;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005491
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005492 if (!selinux_policycap_netpeer())
Paul Mooreeffad8d2008-01-29 08:49:27 -05005493 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005494
Paul Mooreeffad8d2008-01-29 08:49:27 -05005495 secmark_active = selinux_secmark_enabled();
Paul Moore948bf852008-10-10 10:16:32 -04005496 netlbl_active = netlbl_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04005497 peerlbl_active = selinux_peerlbl_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05005498 if (!secmark_active && !peerlbl_active)
5499 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005500
Paul Moored8395c82008-10-10 10:16:30 -04005501 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
5502 return NF_DROP;
5503
Eric Paris50c205f2012-04-04 15:01:43 -04005504 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005505 ad.u.net = &net;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005506 ad.u.net->netif = indev->ifindex;
Eric Paris48c62af2012-04-02 13:15:44 -04005507 ad.u.net->family = family;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005508 if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
5509 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510
Paul Mooredfaebe92008-10-10 10:16:31 -04005511 if (peerlbl_active) {
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005512 err = selinux_inet_sys_rcv_skb(dev_net(indev), indev->ifindex,
5513 addrp, family, peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04005514 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04005515 selinux_netlbl_err(skb, family, err, 1);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005516 return NF_DROP;
Paul Mooredfaebe92008-10-10 10:16:31 -04005517 }
5518 }
Paul Mooreeffad8d2008-01-29 08:49:27 -05005519
5520 if (secmark_active)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005521 if (avc_has_perm(&selinux_state,
5522 peer_sid, skb->secmark,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005523 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
5524 return NF_DROP;
5525
Paul Moore948bf852008-10-10 10:16:32 -04005526 if (netlbl_active)
5527 /* we do this in the FORWARD path and not the POST_ROUTING
5528 * path because we want to make sure we apply the necessary
5529 * labeling before IPsec is applied so we can leverage AH
5530 * protection */
5531 if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
5532 return NF_DROP;
5533
Paul Mooreeffad8d2008-01-29 08:49:27 -05005534 return NF_ACCEPT;
5535}
5536
Eric W. Biederman06198b32015-09-18 14:33:06 -05005537static unsigned int selinux_ipv4_forward(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005538 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005539 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005540{
David S. Miller238e54c2015-04-03 20:32:56 -04005541 return selinux_ip_forward(skb, state->in, PF_INET);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005542}
5543
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005544#if IS_ENABLED(CONFIG_IPV6)
Eric W. Biederman06198b32015-09-18 14:33:06 -05005545static unsigned int selinux_ipv6_forward(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005546 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005547 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005548{
David S. Miller238e54c2015-04-03 20:32:56 -04005549 return selinux_ip_forward(skb, state->in, PF_INET6);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005550}
5551#endif /* IPV6 */
5552
Paul Moore948bf852008-10-10 10:16:32 -04005553static unsigned int selinux_ip_output(struct sk_buff *skb,
5554 u16 family)
5555{
Paul Moore47180062013-12-04 16:10:45 -05005556 struct sock *sk;
Paul Moore948bf852008-10-10 10:16:32 -04005557 u32 sid;
5558
5559 if (!netlbl_enabled())
5560 return NF_ACCEPT;
5561
5562 /* we do this in the LOCAL_OUT path and not the POST_ROUTING path
5563 * because we want to make sure we apply the necessary labeling
5564 * before IPsec is applied so we can leverage AH protection */
Paul Moore47180062013-12-04 16:10:45 -05005565 sk = skb->sk;
5566 if (sk) {
5567 struct sk_security_struct *sksec;
5568
Eric Dumazete446f9d2015-10-08 05:01:55 -07005569 if (sk_listener(sk))
Paul Moore47180062013-12-04 16:10:45 -05005570 /* if the socket is the listening state then this
5571 * packet is a SYN-ACK packet which means it needs to
5572 * be labeled based on the connection/request_sock and
5573 * not the parent socket. unfortunately, we can't
5574 * lookup the request_sock yet as it isn't queued on
5575 * the parent socket until after the SYN-ACK is sent.
5576 * the "solution" is to simply pass the packet as-is
5577 * as any IP option based labeling should be copied
5578 * from the initial connection request (in the IP
5579 * layer). it is far from ideal, but until we get a
5580 * security label in the packet itself this is the
5581 * best we can do. */
5582 return NF_ACCEPT;
5583
5584 /* standard practice, label using the parent socket */
5585 sksec = sk->sk_security;
Paul Moore948bf852008-10-10 10:16:32 -04005586 sid = sksec->sid;
5587 } else
5588 sid = SECINITSID_KERNEL;
5589 if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
5590 return NF_DROP;
5591
5592 return NF_ACCEPT;
5593}
5594
Eric W. Biederman06198b32015-09-18 14:33:06 -05005595static unsigned int selinux_ipv4_output(void *priv,
Paul Moore948bf852008-10-10 10:16:32 -04005596 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005597 const struct nf_hook_state *state)
Paul Moore948bf852008-10-10 10:16:32 -04005598{
5599 return selinux_ip_output(skb, PF_INET);
5600}
5601
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005602#if IS_ENABLED(CONFIG_IPV6)
Huw Davies2917f572016-06-27 15:06:15 -04005603static unsigned int selinux_ipv6_output(void *priv,
5604 struct sk_buff *skb,
5605 const struct nf_hook_state *state)
5606{
5607 return selinux_ip_output(skb, PF_INET6);
5608}
5609#endif /* IPV6 */
5610
Paul Mooreeffad8d2008-01-29 08:49:27 -05005611static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
5612 int ifindex,
Paul Moored8395c82008-10-10 10:16:30 -04005613 u16 family)
James Morris4e5ab4c2006-06-09 00:33:33 -07005614{
Eric Dumazet54abc682015-11-08 10:54:07 -08005615 struct sock *sk = skb_to_full_sk(skb);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005616 struct sk_security_struct *sksec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005617 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005618 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04005619 char *addrp;
5620 u8 proto;
James Morris4e5ab4c2006-06-09 00:33:33 -07005621
Paul Mooreeffad8d2008-01-29 08:49:27 -05005622 if (sk == NULL)
5623 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005624 sksec = sk->sk_security;
James Morris4e5ab4c2006-06-09 00:33:33 -07005625
Eric Paris50c205f2012-04-04 15:01:43 -04005626 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005627 ad.u.net = &net;
5628 ad.u.net->netif = ifindex;
5629 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04005630 if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
5631 return NF_DROP;
5632
Paul Moore58bfbb52009-03-27 17:10:41 -04005633 if (selinux_secmark_enabled())
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005634 if (avc_has_perm(&selinux_state,
5635 sksec->sid, skb->secmark,
Paul Moored8395c82008-10-10 10:16:30 -04005636 SECCLASS_PACKET, PACKET__SEND, &ad))
Eric Paris2fe66ec2010-11-23 06:28:08 +00005637 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07005638
Steffen Klassertb9679a72011-02-23 12:55:21 +01005639 if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
5640 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07005641
Paul Mooreeffad8d2008-01-29 08:49:27 -05005642 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643}
5644
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005645static unsigned int selinux_ip_postroute(struct sk_buff *skb,
5646 const struct net_device *outdev,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005647 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648{
Paul Mooreeffad8d2008-01-29 08:49:27 -05005649 u32 secmark_perm;
5650 u32 peer_sid;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005651 int ifindex = outdev->ifindex;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005652 struct sock *sk;
Thomas Liu2bf49692009-07-14 12:14:09 -04005653 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005654 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05005655 char *addrp;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005656 u8 secmark_active;
5657 u8 peerlbl_active;
5658
Paul Mooreeffad8d2008-01-29 08:49:27 -05005659 /* If any sort of compatibility mode is enabled then handoff processing
5660 * to the selinux_ip_postroute_compat() function to deal with the
5661 * special handling. We do this in an attempt to keep this function
5662 * as fast and as clean as possible. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005663 if (!selinux_policycap_netpeer())
Paul Moored8395c82008-10-10 10:16:30 -04005664 return selinux_ip_postroute_compat(skb, ifindex, family);
Paul Moorec0828e52013-12-10 14:58:01 -05005665
Paul Mooreeffad8d2008-01-29 08:49:27 -05005666 secmark_active = selinux_secmark_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04005667 peerlbl_active = selinux_peerlbl_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05005668 if (!secmark_active && !peerlbl_active)
5669 return NF_ACCEPT;
5670
Eric Dumazet54abc682015-11-08 10:54:07 -08005671 sk = skb_to_full_sk(skb);
Paul Moorec0828e52013-12-10 14:58:01 -05005672
Paul Mooreeffad8d2008-01-29 08:49:27 -05005673#ifdef CONFIG_XFRM
5674 /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
5675 * packet transformation so allow the packet to pass without any checks
5676 * since we'll have another chance to perform access control checks
5677 * when the packet is on it's final way out.
5678 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
Paul Moorec0828e52013-12-10 14:58:01 -05005679 * is NULL, in this case go ahead and apply access control.
5680 * NOTE: if this is a local socket (skb->sk != NULL) that is in the
5681 * TCP listening state we cannot wait until the XFRM processing
5682 * is done as we will miss out on the SA label if we do;
5683 * unfortunately, this means more work, but it is only once per
5684 * connection. */
5685 if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL &&
Eric Dumazete446f9d2015-10-08 05:01:55 -07005686 !(sk && sk_listener(sk)))
Paul Mooreeffad8d2008-01-29 08:49:27 -05005687 return NF_ACCEPT;
5688#endif
Paul Mooreeffad8d2008-01-29 08:49:27 -05005689
Paul Moored8395c82008-10-10 10:16:30 -04005690 if (sk == NULL) {
Paul Moore446b8022013-12-04 16:10:51 -05005691 /* Without an associated socket the packet is either coming
5692 * from the kernel or it is being forwarded; check the packet
5693 * to determine which and if the packet is being forwarded
5694 * query the packet directly to determine the security label. */
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005695 if (skb->skb_iif) {
5696 secmark_perm = PACKET__FORWARD_OUT;
Paul Moored8395c82008-10-10 10:16:30 -04005697 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005698 return NF_DROP;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005699 } else {
5700 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04005701 peer_sid = SECINITSID_KERNEL;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005702 }
Eric Dumazete446f9d2015-10-08 05:01:55 -07005703 } else if (sk_listener(sk)) {
Paul Moore446b8022013-12-04 16:10:51 -05005704 /* Locally generated packet but the associated socket is in the
5705 * listening state which means this is a SYN-ACK packet. In
5706 * this particular case the correct security label is assigned
5707 * to the connection/request_sock but unfortunately we can't
5708 * query the request_sock as it isn't queued on the parent
5709 * socket until after the SYN-ACK packet is sent; the only
5710 * viable choice is to regenerate the label like we do in
5711 * selinux_inet_conn_request(). See also selinux_ip_output()
5712 * for similar problems. */
5713 u32 skb_sid;
Eric Dumazete446f9d2015-10-08 05:01:55 -07005714 struct sk_security_struct *sksec;
5715
Eric Dumazete446f9d2015-10-08 05:01:55 -07005716 sksec = sk->sk_security;
Paul Moore446b8022013-12-04 16:10:51 -05005717 if (selinux_skb_peerlbl_sid(skb, family, &skb_sid))
5718 return NF_DROP;
Paul Moorec0828e52013-12-10 14:58:01 -05005719 /* At this point, if the returned skb peerlbl is SECSID_NULL
5720 * and the packet has been through at least one XFRM
5721 * transformation then we must be dealing with the "final"
5722 * form of labeled IPsec packet; since we've already applied
5723 * all of our access controls on this packet we can safely
5724 * pass the packet. */
5725 if (skb_sid == SECSID_NULL) {
5726 switch (family) {
5727 case PF_INET:
5728 if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
5729 return NF_ACCEPT;
5730 break;
5731 case PF_INET6:
5732 if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED)
5733 return NF_ACCEPT;
Paul Moorea7a91a12014-09-03 10:51:59 -04005734 break;
Paul Moorec0828e52013-12-10 14:58:01 -05005735 default:
5736 return NF_DROP_ERR(-ECONNREFUSED);
5737 }
5738 }
Paul Moore446b8022013-12-04 16:10:51 -05005739 if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid))
5740 return NF_DROP;
5741 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04005742 } else {
Paul Moore446b8022013-12-04 16:10:51 -05005743 /* Locally generated packet, fetch the security label from the
5744 * associated socket. */
Paul Mooreeffad8d2008-01-29 08:49:27 -05005745 struct sk_security_struct *sksec = sk->sk_security;
5746 peer_sid = sksec->sid;
5747 secmark_perm = PACKET__SEND;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005748 }
5749
Eric Paris50c205f2012-04-04 15:01:43 -04005750 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005751 ad.u.net = &net;
5752 ad.u.net->netif = ifindex;
5753 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04005754 if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
Eric Paris04f6d702010-11-23 06:28:02 +00005755 return NF_DROP;
Paul Moored8395c82008-10-10 10:16:30 -04005756
Paul Mooreeffad8d2008-01-29 08:49:27 -05005757 if (secmark_active)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005758 if (avc_has_perm(&selinux_state,
5759 peer_sid, skb->secmark,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005760 SECCLASS_PACKET, secmark_perm, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005761 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005762
5763 if (peerlbl_active) {
5764 u32 if_sid;
5765 u32 node_sid;
5766
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005767 if (sel_netif_sid(dev_net(outdev), ifindex, &if_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005768 return NF_DROP;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005769 if (avc_has_perm(&selinux_state,
5770 peer_sid, if_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005771 SECCLASS_NETIF, NETIF__EGRESS, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005772 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005773
5774 if (sel_netnode_sid(addrp, family, &node_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005775 return NF_DROP;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005776 if (avc_has_perm(&selinux_state,
5777 peer_sid, node_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005778 SECCLASS_NODE, NODE__SENDTO, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005779 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005780 }
5781
5782 return NF_ACCEPT;
5783}
5784
Eric W. Biederman06198b32015-09-18 14:33:06 -05005785static unsigned int selinux_ipv4_postroute(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005786 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005787 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005788{
David S. Miller238e54c2015-04-03 20:32:56 -04005789 return selinux_ip_postroute(skb, state->out, PF_INET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790}
5791
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005792#if IS_ENABLED(CONFIG_IPV6)
Eric W. Biederman06198b32015-09-18 14:33:06 -05005793static unsigned int selinux_ipv6_postroute(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005794 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005795 const struct nf_hook_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796{
David S. Miller238e54c2015-04-03 20:32:56 -04005797 return selinux_ip_postroute(skb, state->out, PF_INET6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799#endif /* IPV6 */
5800
5801#endif /* CONFIG_NETFILTER */
5802
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
5804{
Stephen Smalley941fc5b2009-10-01 14:48:23 -04005805 return selinux_nlmsg_perm(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806}
5807
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005808static int ipc_alloc_security(struct kern_ipc_perm *perm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809 u16 sclass)
5810{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811 struct ipc_security_struct *isec;
5812
James Morris89d155e2005-10-30 14:59:21 -08005813 isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814 if (!isec)
5815 return -ENOMEM;
5816
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817 isec->sclass = sclass;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005818 isec->sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819 perm->security = isec;
5820
5821 return 0;
5822}
5823
5824static void ipc_free_security(struct kern_ipc_perm *perm)
5825{
5826 struct ipc_security_struct *isec = perm->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827 perm->security = NULL;
5828 kfree(isec);
5829}
5830
5831static int msg_msg_alloc_security(struct msg_msg *msg)
5832{
5833 struct msg_security_struct *msec;
5834
James Morris89d155e2005-10-30 14:59:21 -08005835 msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836 if (!msec)
5837 return -ENOMEM;
5838
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839 msec->sid = SECINITSID_UNLABELED;
5840 msg->security = msec;
5841
5842 return 0;
5843}
5844
5845static void msg_msg_free_security(struct msg_msg *msg)
5846{
5847 struct msg_security_struct *msec = msg->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848
5849 msg->security = NULL;
5850 kfree(msec);
5851}
5852
5853static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
Stephen Smalley6af963f2005-05-01 08:58:39 -07005854 u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005857 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005858 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860 isec = ipc_perms->security;
5861
Eric Paris50c205f2012-04-04 15:01:43 -04005862 ad.type = LSM_AUDIT_DATA_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863 ad.u.ipc_id = ipc_perms->key;
5864
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005865 return avc_has_perm(&selinux_state,
5866 sid, isec->sid, isec->sclass, perms, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867}
5868
5869static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
5870{
5871 return msg_msg_alloc_security(msg);
5872}
5873
5874static void selinux_msg_msg_free_security(struct msg_msg *msg)
5875{
5876 msg_msg_free_security(msg);
5877}
5878
5879/* message queue security operations */
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005880static int selinux_msg_queue_alloc_security(struct kern_ipc_perm *msq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005881{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005883 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005884 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885 int rc;
5886
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005887 rc = ipc_alloc_security(msq, SECCLASS_MSGQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888 if (rc)
5889 return rc;
5890
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005891 isec = msq->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892
Eric Paris50c205f2012-04-04 15:01:43 -04005893 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005894 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005896 rc = avc_has_perm(&selinux_state,
5897 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898 MSGQ__CREATE, &ad);
5899 if (rc) {
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005900 ipc_free_security(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005901 return rc;
5902 }
5903 return 0;
5904}
5905
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005906static void selinux_msg_queue_free_security(struct kern_ipc_perm *msq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005907{
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005908 ipc_free_security(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005909}
5910
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005911static int selinux_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005913 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005914 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005915 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005916
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005917 isec = msq->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918
Eric Paris50c205f2012-04-04 15:01:43 -04005919 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005920 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005922 return avc_has_perm(&selinux_state,
5923 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924 MSGQ__ASSOCIATE, &ad);
5925}
5926
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005927static int selinux_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928{
5929 int err;
5930 int perms;
5931
Eric Paris828dfe12008-04-17 13:17:49 -04005932 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933 case IPC_INFO:
5934 case MSG_INFO:
5935 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005936 return avc_has_perm(&selinux_state,
5937 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005938 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939 case IPC_STAT:
5940 case MSG_STAT:
Davidlohr Bueso23c8cec2018-04-10 16:35:30 -07005941 case MSG_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005942 perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
5943 break;
5944 case IPC_SET:
5945 perms = MSGQ__SETATTR;
5946 break;
5947 case IPC_RMID:
5948 perms = MSGQ__DESTROY;
5949 break;
5950 default:
5951 return 0;
5952 }
5953
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005954 err = ipc_has_perm(msq, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955 return err;
5956}
5957
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005958static int selinux_msg_queue_msgsnd(struct kern_ipc_perm *msq, struct msg_msg *msg, int msqflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005959{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005960 struct ipc_security_struct *isec;
5961 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005962 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005963 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005964 int rc;
5965
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005966 isec = msq->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005967 msec = msg->security;
5968
5969 /*
5970 * First time through, need to assign label to the message
5971 */
5972 if (msec->sid == SECINITSID_UNLABELED) {
5973 /*
5974 * Compute new sid based on current process and
5975 * message queue this message will be stored in
5976 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005977 rc = security_transition_sid(&selinux_state, sid, isec->sid,
5978 SECCLASS_MSG, NULL, &msec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979 if (rc)
5980 return rc;
5981 }
5982
Eric Paris50c205f2012-04-04 15:01:43 -04005983 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005984 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005985
5986 /* Can this process write to the queue? */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005987 rc = avc_has_perm(&selinux_state,
5988 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005989 MSGQ__WRITE, &ad);
5990 if (!rc)
5991 /* Can this process send the message */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005992 rc = avc_has_perm(&selinux_state,
5993 sid, msec->sid, SECCLASS_MSG,
David Howells275bb412008-11-14 10:39:19 +11005994 MSG__SEND, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005995 if (!rc)
5996 /* Can the message be put in the queue? */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005997 rc = avc_has_perm(&selinux_state,
5998 msec->sid, isec->sid, SECCLASS_MSGQ,
David Howells275bb412008-11-14 10:39:19 +11005999 MSGQ__ENQUEUE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006000
6001 return rc;
6002}
6003
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05006004static int selinux_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005 struct task_struct *target,
6006 long type, int mode)
6007{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006008 struct ipc_security_struct *isec;
6009 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006010 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006011 u32 sid = task_sid(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012 int rc;
6013
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05006014 isec = msq->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015 msec = msg->security;
6016
Eric Paris50c205f2012-04-04 15:01:43 -04006017 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05006018 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006020 rc = avc_has_perm(&selinux_state,
6021 sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022 SECCLASS_MSGQ, MSGQ__READ, &ad);
6023 if (!rc)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006024 rc = avc_has_perm(&selinux_state,
6025 sid, msec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026 SECCLASS_MSG, MSG__RECEIVE, &ad);
6027 return rc;
6028}
6029
6030/* Shared Memory security operations */
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006031static int selinux_shm_alloc_security(struct kern_ipc_perm *shp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006034 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006035 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036 int rc;
6037
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006038 rc = ipc_alloc_security(shp, SECCLASS_SHM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039 if (rc)
6040 return rc;
6041
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006042 isec = shp->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006043
Eric Paris50c205f2012-04-04 15:01:43 -04006044 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006045 ad.u.ipc_id = shp->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006046
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006047 rc = avc_has_perm(&selinux_state,
6048 sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049 SHM__CREATE, &ad);
6050 if (rc) {
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006051 ipc_free_security(shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052 return rc;
6053 }
6054 return 0;
6055}
6056
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006057static void selinux_shm_free_security(struct kern_ipc_perm *shp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058{
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006059 ipc_free_security(shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006060}
6061
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006062static int selinux_shm_associate(struct kern_ipc_perm *shp, int shmflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006064 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006065 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006066 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006068 isec = shp->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069
Eric Paris50c205f2012-04-04 15:01:43 -04006070 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006071 ad.u.ipc_id = shp->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006073 return avc_has_perm(&selinux_state,
6074 sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075 SHM__ASSOCIATE, &ad);
6076}
6077
6078/* Note, at this point, shp is locked down */
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006079static int selinux_shm_shmctl(struct kern_ipc_perm *shp, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080{
6081 int perms;
6082 int err;
6083
Eric Paris828dfe12008-04-17 13:17:49 -04006084 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085 case IPC_INFO:
6086 case SHM_INFO:
6087 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006088 return avc_has_perm(&selinux_state,
6089 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006090 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006091 case IPC_STAT:
6092 case SHM_STAT:
Davidlohr Buesoc21a6972018-04-10 16:35:23 -07006093 case SHM_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006094 perms = SHM__GETATTR | SHM__ASSOCIATE;
6095 break;
6096 case IPC_SET:
6097 perms = SHM__SETATTR;
6098 break;
6099 case SHM_LOCK:
6100 case SHM_UNLOCK:
6101 perms = SHM__LOCK;
6102 break;
6103 case IPC_RMID:
6104 perms = SHM__DESTROY;
6105 break;
6106 default:
6107 return 0;
6108 }
6109
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006110 err = ipc_has_perm(shp, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006111 return err;
6112}
6113
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006114static int selinux_shm_shmat(struct kern_ipc_perm *shp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006115 char __user *shmaddr, int shmflg)
6116{
6117 u32 perms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006118
6119 if (shmflg & SHM_RDONLY)
6120 perms = SHM__READ;
6121 else
6122 perms = SHM__READ | SHM__WRITE;
6123
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006124 return ipc_has_perm(shp, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006125}
6126
6127/* Semaphore security operations */
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006128static int selinux_sem_alloc_security(struct kern_ipc_perm *sma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006129{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006130 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006131 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006132 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006133 int rc;
6134
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006135 rc = ipc_alloc_security(sma, SECCLASS_SEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006136 if (rc)
6137 return rc;
6138
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006139 isec = sma->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006140
Eric Paris50c205f2012-04-04 15:01:43 -04006141 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006142 ad.u.ipc_id = sma->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006143
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006144 rc = avc_has_perm(&selinux_state,
6145 sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006146 SEM__CREATE, &ad);
6147 if (rc) {
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006148 ipc_free_security(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006149 return rc;
6150 }
6151 return 0;
6152}
6153
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006154static void selinux_sem_free_security(struct kern_ipc_perm *sma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006155{
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006156 ipc_free_security(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006157}
6158
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006159static int selinux_sem_associate(struct kern_ipc_perm *sma, int semflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006160{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006161 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006162 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006163 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006164
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006165 isec = sma->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006166
Eric Paris50c205f2012-04-04 15:01:43 -04006167 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006168 ad.u.ipc_id = sma->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006169
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006170 return avc_has_perm(&selinux_state,
6171 sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006172 SEM__ASSOCIATE, &ad);
6173}
6174
6175/* Note, at this point, sma is locked down */
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006176static int selinux_sem_semctl(struct kern_ipc_perm *sma, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006177{
6178 int err;
6179 u32 perms;
6180
Eric Paris828dfe12008-04-17 13:17:49 -04006181 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006182 case IPC_INFO:
6183 case SEM_INFO:
6184 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006185 return avc_has_perm(&selinux_state,
6186 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006187 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006188 case GETPID:
6189 case GETNCNT:
6190 case GETZCNT:
6191 perms = SEM__GETATTR;
6192 break;
6193 case GETVAL:
6194 case GETALL:
6195 perms = SEM__READ;
6196 break;
6197 case SETVAL:
6198 case SETALL:
6199 perms = SEM__WRITE;
6200 break;
6201 case IPC_RMID:
6202 perms = SEM__DESTROY;
6203 break;
6204 case IPC_SET:
6205 perms = SEM__SETATTR;
6206 break;
6207 case IPC_STAT:
6208 case SEM_STAT:
Davidlohr Buesoa280d6d2018-04-10 16:35:26 -07006209 case SEM_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210 perms = SEM__GETATTR | SEM__ASSOCIATE;
6211 break;
6212 default:
6213 return 0;
6214 }
6215
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006216 err = ipc_has_perm(sma, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217 return err;
6218}
6219
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006220static int selinux_sem_semop(struct kern_ipc_perm *sma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006221 struct sembuf *sops, unsigned nsops, int alter)
6222{
6223 u32 perms;
6224
6225 if (alter)
6226 perms = SEM__READ | SEM__WRITE;
6227 else
6228 perms = SEM__READ;
6229
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006230 return ipc_has_perm(sma, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006231}
6232
6233static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
6234{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006235 u32 av = 0;
6236
Linus Torvalds1da177e2005-04-16 15:20:36 -07006237 av = 0;
6238 if (flag & S_IRUGO)
6239 av |= IPC__UNIX_READ;
6240 if (flag & S_IWUGO)
6241 av |= IPC__UNIX_WRITE;
6242
6243 if (av == 0)
6244 return 0;
6245
Stephen Smalley6af963f2005-05-01 08:58:39 -07006246 return ipc_has_perm(ipcp, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006247}
6248
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02006249static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
6250{
6251 struct ipc_security_struct *isec = ipcp->security;
6252 *secid = isec->sid;
6253}
6254
Eric Paris828dfe12008-04-17 13:17:49 -04006255static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006256{
6257 if (inode)
6258 inode_doinit_with_dentry(inode, dentry);
6259}
6260
6261static int selinux_getprocattr(struct task_struct *p,
Al Viro04ff9702007-03-12 16:17:58 +00006262 char *name, char **value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006263{
David Howells275bb412008-11-14 10:39:19 +11006264 const struct task_security_struct *__tsec;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00006265 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006266 int error;
Al Viro04ff9702007-03-12 16:17:58 +00006267 unsigned len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006268
David Howells275bb412008-11-14 10:39:19 +11006269 rcu_read_lock();
6270 __tsec = __task_cred(p)->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006272 if (current != p) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006273 error = avc_has_perm(&selinux_state,
6274 current_sid(), __tsec->sid,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006275 SECCLASS_PROCESS, PROCESS__GETATTR, NULL);
6276 if (error)
6277 goto bad;
6278 }
6279
Linus Torvalds1da177e2005-04-16 15:20:36 -07006280 if (!strcmp(name, "current"))
David Howells275bb412008-11-14 10:39:19 +11006281 sid = __tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006282 else if (!strcmp(name, "prev"))
David Howells275bb412008-11-14 10:39:19 +11006283 sid = __tsec->osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006284 else if (!strcmp(name, "exec"))
David Howells275bb412008-11-14 10:39:19 +11006285 sid = __tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006286 else if (!strcmp(name, "fscreate"))
David Howells275bb412008-11-14 10:39:19 +11006287 sid = __tsec->create_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006288 else if (!strcmp(name, "keycreate"))
David Howells275bb412008-11-14 10:39:19 +11006289 sid = __tsec->keycreate_sid;
Eric Paris42c3e032006-06-26 00:26:03 -07006290 else if (!strcmp(name, "sockcreate"))
David Howells275bb412008-11-14 10:39:19 +11006291 sid = __tsec->sockcreate_sid;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006292 else {
6293 error = -EINVAL;
6294 goto bad;
6295 }
David Howells275bb412008-11-14 10:39:19 +11006296 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006297
6298 if (!sid)
6299 return 0;
6300
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006301 error = security_sid_to_context(&selinux_state, sid, value, &len);
Al Viro04ff9702007-03-12 16:17:58 +00006302 if (error)
6303 return error;
6304 return len;
David Howells275bb412008-11-14 10:39:19 +11006305
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006306bad:
David Howells275bb412008-11-14 10:39:19 +11006307 rcu_read_unlock();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006308 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006309}
6310
Stephen Smalleyb21507e2017-01-09 10:07:31 -05006311static int selinux_setprocattr(const char *name, void *value, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006312{
6313 struct task_security_struct *tsec;
David Howellsd84f4f92008-11-14 10:39:23 +11006314 struct cred *new;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006315 u32 mysid = current_sid(), sid = 0, ptsid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006316 int error;
6317 char *str = value;
6318
Linus Torvalds1da177e2005-04-16 15:20:36 -07006319 /*
6320 * Basic control over ability to set these attributes at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006321 */
6322 if (!strcmp(name, "exec"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006323 error = avc_has_perm(&selinux_state,
6324 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006325 PROCESS__SETEXEC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006326 else if (!strcmp(name, "fscreate"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006327 error = avc_has_perm(&selinux_state,
6328 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006329 PROCESS__SETFSCREATE, NULL);
Michael LeMay4eb582c2006-06-26 00:24:57 -07006330 else if (!strcmp(name, "keycreate"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006331 error = avc_has_perm(&selinux_state,
6332 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006333 PROCESS__SETKEYCREATE, NULL);
Eric Paris42c3e032006-06-26 00:26:03 -07006334 else if (!strcmp(name, "sockcreate"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006335 error = avc_has_perm(&selinux_state,
6336 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006337 PROCESS__SETSOCKCREATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006338 else if (!strcmp(name, "current"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006339 error = avc_has_perm(&selinux_state,
6340 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006341 PROCESS__SETCURRENT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006342 else
6343 error = -EINVAL;
6344 if (error)
6345 return error;
6346
6347 /* Obtain a SID for the context, if one was specified. */
Stephen Smalleya050a572017-01-31 11:54:04 -05006348 if (size && str[0] && str[0] != '\n') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006349 if (str[size-1] == '\n') {
6350 str[size-1] = 0;
6351 size--;
6352 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006353 error = security_context_to_sid(&selinux_state, value, size,
6354 &sid, GFP_KERNEL);
Stephen Smalley12b29f32008-05-07 13:03:20 -04006355 if (error == -EINVAL && !strcmp(name, "fscreate")) {
Stephen Smalleydb590002017-04-20 11:31:30 -04006356 if (!has_cap_mac_admin(true)) {
Eric Parisd6ea83e2012-04-04 13:45:49 -04006357 struct audit_buffer *ab;
6358 size_t audit_size;
6359
6360 /* We strip a nul only if it is at the end, otherwise the
6361 * context contains a nul and we should audit that */
6362 if (str[size - 1] == '\0')
6363 audit_size = size - 1;
6364 else
6365 audit_size = size;
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04006366 ab = audit_log_start(audit_context(),
6367 GFP_ATOMIC,
6368 AUDIT_SELINUX_ERR);
Eric Parisd6ea83e2012-04-04 13:45:49 -04006369 audit_log_format(ab, "op=fscreate invalid_context=");
6370 audit_log_n_untrustedstring(ab, value, audit_size);
6371 audit_log_end(ab);
6372
Stephen Smalley12b29f32008-05-07 13:03:20 -04006373 return error;
Eric Parisd6ea83e2012-04-04 13:45:49 -04006374 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006375 error = security_context_to_sid_force(
6376 &selinux_state,
6377 value, size, &sid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04006378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006379 if (error)
6380 return error;
6381 }
6382
David Howellsd84f4f92008-11-14 10:39:23 +11006383 new = prepare_creds();
6384 if (!new)
6385 return -ENOMEM;
6386
Linus Torvalds1da177e2005-04-16 15:20:36 -07006387 /* Permission checking based on the specified context is
6388 performed during the actual operation (execve,
6389 open/mkdir/...), when we know the full context of the
David Howellsd84f4f92008-11-14 10:39:23 +11006390 operation. See selinux_bprm_set_creds for the execve
Linus Torvalds1da177e2005-04-16 15:20:36 -07006391 checks and may_create for the file creation checks. The
6392 operation will then fail if the context is not permitted. */
David Howellsd84f4f92008-11-14 10:39:23 +11006393 tsec = new->security;
6394 if (!strcmp(name, "exec")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006395 tsec->exec_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006396 } else if (!strcmp(name, "fscreate")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006397 tsec->create_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006398 } else if (!strcmp(name, "keycreate")) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006399 error = avc_has_perm(&selinux_state,
6400 mysid, sid, SECCLASS_KEY, KEY__CREATE,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006401 NULL);
Michael LeMay4eb582c2006-06-26 00:24:57 -07006402 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11006403 goto abort_change;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006404 tsec->keycreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006405 } else if (!strcmp(name, "sockcreate")) {
Eric Paris42c3e032006-06-26 00:26:03 -07006406 tsec->sockcreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006407 } else if (!strcmp(name, "current")) {
6408 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006409 if (sid == 0)
David Howellsd84f4f92008-11-14 10:39:23 +11006410 goto abort_change;
KaiGai Koheid9250de2008-08-28 16:35:57 +09006411
David Howellsd84f4f92008-11-14 10:39:23 +11006412 /* Only allow single threaded processes to change context */
6413 error = -EPERM;
Oleg Nesterov5bb459b2009-07-10 03:48:23 +02006414 if (!current_is_single_threaded()) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006415 error = security_bounded_transition(&selinux_state,
6416 tsec->sid, sid);
David Howellsd84f4f92008-11-14 10:39:23 +11006417 if (error)
6418 goto abort_change;
Eric Paris828dfe12008-04-17 13:17:49 -04006419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006420
6421 /* Check permissions for the transition. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006422 error = avc_has_perm(&selinux_state,
6423 tsec->sid, sid, SECCLASS_PROCESS,
Eric Paris828dfe12008-04-17 13:17:49 -04006424 PROCESS__DYNTRANSITION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006425 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11006426 goto abort_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006427
6428 /* Check for ptracing, and update the task SID if ok.
6429 Otherwise, leave SID unchanged and fail. */
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006430 ptsid = ptrace_parent_sid();
Paul Moore0c6181c2016-03-30 21:41:21 -04006431 if (ptsid != 0) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006432 error = avc_has_perm(&selinux_state,
6433 ptsid, sid, SECCLASS_PROCESS,
David Howellsd84f4f92008-11-14 10:39:23 +11006434 PROCESS__PTRACE, NULL);
6435 if (error)
6436 goto abort_change;
6437 }
6438
6439 tsec->sid = sid;
6440 } else {
6441 error = -EINVAL;
6442 goto abort_change;
6443 }
6444
6445 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006446 return size;
David Howellsd84f4f92008-11-14 10:39:23 +11006447
6448abort_change:
6449 abort_creds(new);
6450 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006451}
6452
David Quigley746df9b2013-05-22 12:50:35 -04006453static int selinux_ismaclabel(const char *name)
6454{
6455 return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0);
6456}
6457
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006458static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
6459{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006460 return security_sid_to_context(&selinux_state, secid,
6461 secdata, seclen);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006462}
6463
David Howells7bf570d2008-04-29 20:52:51 +01006464static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
David Howells63cb3442008-01-15 23:47:35 +00006465{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006466 return security_context_to_sid(&selinux_state, secdata, seclen,
6467 secid, GFP_KERNEL);
David Howells63cb3442008-01-15 23:47:35 +00006468}
6469
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006470static void selinux_release_secctx(char *secdata, u32 seclen)
6471{
Paul Moore088999e2007-08-01 11:12:58 -04006472 kfree(secdata);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006473}
6474
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006475static void selinux_inode_invalidate_secctx(struct inode *inode)
6476{
6477 struct inode_security_struct *isec = inode->i_security;
6478
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01006479 spin_lock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006480 isec->initialized = LABEL_INVALID;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01006481 spin_unlock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006482}
6483
David P. Quigley1ee65e32009-09-03 14:25:57 -04006484/*
6485 * called with inode->i_mutex locked
6486 */
6487static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
6488{
6489 return selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, ctx, ctxlen, 0);
6490}
6491
6492/*
6493 * called with inode->i_mutex locked
6494 */
6495static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
6496{
6497 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0);
6498}
6499
6500static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
6501{
6502 int len = 0;
6503 len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX,
6504 ctx, true);
6505 if (len < 0)
6506 return len;
6507 *ctxlen = len;
6508 return 0;
6509}
Michael LeMayd7200242006-06-22 14:47:17 -07006510#ifdef CONFIG_KEYS
6511
David Howellsd84f4f92008-11-14 10:39:23 +11006512static int selinux_key_alloc(struct key *k, const struct cred *cred,
David Howells7e047ef2006-06-26 00:24:50 -07006513 unsigned long flags)
Michael LeMayd7200242006-06-22 14:47:17 -07006514{
David Howellsd84f4f92008-11-14 10:39:23 +11006515 const struct task_security_struct *tsec;
Michael LeMayd7200242006-06-22 14:47:17 -07006516 struct key_security_struct *ksec;
6517
6518 ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
6519 if (!ksec)
6520 return -ENOMEM;
6521
David Howellsd84f4f92008-11-14 10:39:23 +11006522 tsec = cred->security;
6523 if (tsec->keycreate_sid)
6524 ksec->sid = tsec->keycreate_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006525 else
David Howellsd84f4f92008-11-14 10:39:23 +11006526 ksec->sid = tsec->sid;
Michael LeMayd7200242006-06-22 14:47:17 -07006527
David Howells275bb412008-11-14 10:39:19 +11006528 k->security = ksec;
Michael LeMayd7200242006-06-22 14:47:17 -07006529 return 0;
6530}
6531
6532static void selinux_key_free(struct key *k)
6533{
6534 struct key_security_struct *ksec = k->security;
6535
6536 k->security = NULL;
6537 kfree(ksec);
6538}
6539
6540static int selinux_key_permission(key_ref_t key_ref,
David Howellsd84f4f92008-11-14 10:39:23 +11006541 const struct cred *cred,
David Howellsf5895942014-03-14 17:44:49 +00006542 unsigned perm)
Michael LeMayd7200242006-06-22 14:47:17 -07006543{
6544 struct key *key;
Michael LeMayd7200242006-06-22 14:47:17 -07006545 struct key_security_struct *ksec;
David Howells275bb412008-11-14 10:39:19 +11006546 u32 sid;
Michael LeMayd7200242006-06-22 14:47:17 -07006547
6548 /* if no specific permissions are requested, we skip the
6549 permission check. No serious, additional covert channels
6550 appear to be created. */
6551 if (perm == 0)
6552 return 0;
6553
David Howellsd84f4f92008-11-14 10:39:23 +11006554 sid = cred_sid(cred);
David Howells275bb412008-11-14 10:39:19 +11006555
6556 key = key_ref_to_ptr(key_ref);
6557 ksec = key->security;
6558
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006559 return avc_has_perm(&selinux_state,
6560 sid, ksec->sid, SECCLASS_KEY, perm, NULL);
Michael LeMayd7200242006-06-22 14:47:17 -07006561}
6562
David Howells70a5bb72008-04-29 01:01:26 -07006563static int selinux_key_getsecurity(struct key *key, char **_buffer)
6564{
6565 struct key_security_struct *ksec = key->security;
6566 char *context = NULL;
6567 unsigned len;
6568 int rc;
6569
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006570 rc = security_sid_to_context(&selinux_state, ksec->sid,
6571 &context, &len);
David Howells70a5bb72008-04-29 01:01:26 -07006572 if (!rc)
6573 rc = len;
6574 *_buffer = context;
6575 return rc;
6576}
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006577#endif
David Howells70a5bb72008-04-29 01:01:26 -07006578
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006579#ifdef CONFIG_SECURITY_INFINIBAND
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006580static int selinux_ib_pkey_access(void *ib_sec, u64 subnet_prefix, u16 pkey_val)
6581{
6582 struct common_audit_data ad;
6583 int err;
6584 u32 sid = 0;
6585 struct ib_security_struct *sec = ib_sec;
6586 struct lsm_ibpkey_audit ibpkey;
6587
Daniel Jurgens409dcf32017-05-19 15:48:59 +03006588 err = sel_ib_pkey_sid(subnet_prefix, pkey_val, &sid);
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006589 if (err)
6590 return err;
6591
6592 ad.type = LSM_AUDIT_DATA_IBPKEY;
6593 ibpkey.subnet_prefix = subnet_prefix;
6594 ibpkey.pkey = pkey_val;
6595 ad.u.ibpkey = &ibpkey;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006596 return avc_has_perm(&selinux_state,
6597 sec->sid, sid,
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006598 SECCLASS_INFINIBAND_PKEY,
6599 INFINIBAND_PKEY__ACCESS, &ad);
6600}
6601
Daniel Jurgensab861df2017-05-19 15:48:58 +03006602static int selinux_ib_endport_manage_subnet(void *ib_sec, const char *dev_name,
6603 u8 port_num)
6604{
6605 struct common_audit_data ad;
6606 int err;
6607 u32 sid = 0;
6608 struct ib_security_struct *sec = ib_sec;
6609 struct lsm_ibendport_audit ibendport;
6610
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006611 err = security_ib_endport_sid(&selinux_state, dev_name, port_num,
6612 &sid);
Daniel Jurgensab861df2017-05-19 15:48:58 +03006613
6614 if (err)
6615 return err;
6616
6617 ad.type = LSM_AUDIT_DATA_IBENDPORT;
6618 strncpy(ibendport.dev_name, dev_name, sizeof(ibendport.dev_name));
6619 ibendport.port = port_num;
6620 ad.u.ibendport = &ibendport;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006621 return avc_has_perm(&selinux_state,
6622 sec->sid, sid,
Daniel Jurgensab861df2017-05-19 15:48:58 +03006623 SECCLASS_INFINIBAND_ENDPORT,
6624 INFINIBAND_ENDPORT__MANAGE_SUBNET, &ad);
6625}
6626
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006627static int selinux_ib_alloc_security(void **ib_sec)
6628{
6629 struct ib_security_struct *sec;
6630
6631 sec = kzalloc(sizeof(*sec), GFP_KERNEL);
6632 if (!sec)
6633 return -ENOMEM;
6634 sec->sid = current_sid();
6635
6636 *ib_sec = sec;
6637 return 0;
6638}
6639
6640static void selinux_ib_free_security(void *ib_sec)
6641{
6642 kfree(ib_sec);
6643}
Michael LeMayd7200242006-06-22 14:47:17 -07006644#endif
6645
Chenbo Fengec27c352017-10-18 13:00:25 -07006646#ifdef CONFIG_BPF_SYSCALL
6647static int selinux_bpf(int cmd, union bpf_attr *attr,
6648 unsigned int size)
6649{
6650 u32 sid = current_sid();
6651 int ret;
6652
6653 switch (cmd) {
6654 case BPF_MAP_CREATE:
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006655 ret = avc_has_perm(&selinux_state,
6656 sid, sid, SECCLASS_BPF, BPF__MAP_CREATE,
Chenbo Fengec27c352017-10-18 13:00:25 -07006657 NULL);
6658 break;
6659 case BPF_PROG_LOAD:
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006660 ret = avc_has_perm(&selinux_state,
6661 sid, sid, SECCLASS_BPF, BPF__PROG_LOAD,
Chenbo Fengec27c352017-10-18 13:00:25 -07006662 NULL);
6663 break;
6664 default:
6665 ret = 0;
6666 break;
6667 }
6668
6669 return ret;
6670}
6671
6672static u32 bpf_map_fmode_to_av(fmode_t fmode)
6673{
6674 u32 av = 0;
6675
6676 if (fmode & FMODE_READ)
6677 av |= BPF__MAP_READ;
6678 if (fmode & FMODE_WRITE)
6679 av |= BPF__MAP_WRITE;
6680 return av;
6681}
6682
Chenbo Fengf66e4482017-10-18 13:00:26 -07006683/* This function will check the file pass through unix socket or binder to see
6684 * if it is a bpf related object. And apply correspinding checks on the bpf
6685 * object based on the type. The bpf maps and programs, not like other files and
6686 * socket, are using a shared anonymous inode inside the kernel as their inode.
6687 * So checking that inode cannot identify if the process have privilege to
6688 * access the bpf object and that's why we have to add this additional check in
6689 * selinux_file_receive and selinux_binder_transfer_files.
6690 */
6691static int bpf_fd_pass(struct file *file, u32 sid)
6692{
6693 struct bpf_security_struct *bpfsec;
6694 struct bpf_prog *prog;
6695 struct bpf_map *map;
6696 int ret;
6697
6698 if (file->f_op == &bpf_map_fops) {
6699 map = file->private_data;
6700 bpfsec = map->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006701 ret = avc_has_perm(&selinux_state,
6702 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengf66e4482017-10-18 13:00:26 -07006703 bpf_map_fmode_to_av(file->f_mode), NULL);
6704 if (ret)
6705 return ret;
6706 } else if (file->f_op == &bpf_prog_fops) {
6707 prog = file->private_data;
6708 bpfsec = prog->aux->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006709 ret = avc_has_perm(&selinux_state,
6710 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengf66e4482017-10-18 13:00:26 -07006711 BPF__PROG_RUN, NULL);
6712 if (ret)
6713 return ret;
6714 }
6715 return 0;
6716}
6717
Chenbo Fengec27c352017-10-18 13:00:25 -07006718static int selinux_bpf_map(struct bpf_map *map, fmode_t fmode)
6719{
6720 u32 sid = current_sid();
6721 struct bpf_security_struct *bpfsec;
6722
6723 bpfsec = map->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006724 return avc_has_perm(&selinux_state,
6725 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengec27c352017-10-18 13:00:25 -07006726 bpf_map_fmode_to_av(fmode), NULL);
6727}
6728
6729static int selinux_bpf_prog(struct bpf_prog *prog)
6730{
6731 u32 sid = current_sid();
6732 struct bpf_security_struct *bpfsec;
6733
6734 bpfsec = prog->aux->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006735 return avc_has_perm(&selinux_state,
6736 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengec27c352017-10-18 13:00:25 -07006737 BPF__PROG_RUN, NULL);
6738}
6739
6740static int selinux_bpf_map_alloc(struct bpf_map *map)
6741{
6742 struct bpf_security_struct *bpfsec;
6743
6744 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6745 if (!bpfsec)
6746 return -ENOMEM;
6747
6748 bpfsec->sid = current_sid();
6749 map->security = bpfsec;
6750
6751 return 0;
6752}
6753
6754static void selinux_bpf_map_free(struct bpf_map *map)
6755{
6756 struct bpf_security_struct *bpfsec = map->security;
6757
6758 map->security = NULL;
6759 kfree(bpfsec);
6760}
6761
6762static int selinux_bpf_prog_alloc(struct bpf_prog_aux *aux)
6763{
6764 struct bpf_security_struct *bpfsec;
6765
6766 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6767 if (!bpfsec)
6768 return -ENOMEM;
6769
6770 bpfsec->sid = current_sid();
6771 aux->security = bpfsec;
6772
6773 return 0;
6774}
6775
6776static void selinux_bpf_prog_free(struct bpf_prog_aux *aux)
6777{
6778 struct bpf_security_struct *bpfsec = aux->security;
6779
6780 aux->security = NULL;
6781 kfree(bpfsec);
6782}
6783#endif
6784
James Morrisca97d932017-02-15 00:18:51 +11006785static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {
Casey Schauflere20b0432015-05-02 15:11:36 -07006786 LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr),
6787 LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction),
6788 LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder),
6789 LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file),
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02006790
Casey Schauflere20b0432015-05-02 15:11:36 -07006791 LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check),
6792 LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme),
6793 LSM_HOOK_INIT(capget, selinux_capget),
6794 LSM_HOOK_INIT(capset, selinux_capset),
6795 LSM_HOOK_INIT(capable, selinux_capable),
6796 LSM_HOOK_INIT(quotactl, selinux_quotactl),
6797 LSM_HOOK_INIT(quota_on, selinux_quota_on),
6798 LSM_HOOK_INIT(syslog, selinux_syslog),
6799 LSM_HOOK_INIT(vm_enough_memory, selinux_vm_enough_memory),
Stephen Smalley79af7302015-01-21 10:54:10 -05006800
Casey Schauflere20b0432015-05-02 15:11:36 -07006801 LSM_HOOK_INIT(netlink_send, selinux_netlink_send),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006802
Casey Schauflere20b0432015-05-02 15:11:36 -07006803 LSM_HOOK_INIT(bprm_set_creds, selinux_bprm_set_creds),
6804 LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds),
6805 LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006806
Casey Schauflere20b0432015-05-02 15:11:36 -07006807 LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security),
6808 LSM_HOOK_INIT(sb_free_security, selinux_sb_free_security),
Al Viro5b400232018-12-12 20:13:29 -05006809 LSM_HOOK_INIT(sb_eat_lsm_opts, selinux_sb_eat_lsm_opts),
Al Viro204cc0c2018-12-13 13:41:47 -05006810 LSM_HOOK_INIT(sb_free_mnt_opts, selinux_free_mnt_opts),
Casey Schauflere20b0432015-05-02 15:11:36 -07006811 LSM_HOOK_INIT(sb_remount, selinux_sb_remount),
6812 LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount),
6813 LSM_HOOK_INIT(sb_show_options, selinux_sb_show_options),
6814 LSM_HOOK_INIT(sb_statfs, selinux_sb_statfs),
6815 LSM_HOOK_INIT(sb_mount, selinux_mount),
6816 LSM_HOOK_INIT(sb_umount, selinux_umount),
6817 LSM_HOOK_INIT(sb_set_mnt_opts, selinux_set_mnt_opts),
6818 LSM_HOOK_INIT(sb_clone_mnt_opts, selinux_sb_clone_mnt_opts),
6819 LSM_HOOK_INIT(sb_parse_opts_str, selinux_parse_opts_str),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006820
Casey Schauflere20b0432015-05-02 15:11:36 -07006821 LSM_HOOK_INIT(dentry_init_security, selinux_dentry_init_security),
Vivek Goyala518b0a2016-07-13 10:44:53 -04006822 LSM_HOOK_INIT(dentry_create_files_as, selinux_dentry_create_files_as),
Eric Parise0007522008-03-05 10:31:54 -05006823
Casey Schauflere20b0432015-05-02 15:11:36 -07006824 LSM_HOOK_INIT(inode_alloc_security, selinux_inode_alloc_security),
6825 LSM_HOOK_INIT(inode_free_security, selinux_inode_free_security),
6826 LSM_HOOK_INIT(inode_init_security, selinux_inode_init_security),
6827 LSM_HOOK_INIT(inode_create, selinux_inode_create),
6828 LSM_HOOK_INIT(inode_link, selinux_inode_link),
6829 LSM_HOOK_INIT(inode_unlink, selinux_inode_unlink),
6830 LSM_HOOK_INIT(inode_symlink, selinux_inode_symlink),
6831 LSM_HOOK_INIT(inode_mkdir, selinux_inode_mkdir),
6832 LSM_HOOK_INIT(inode_rmdir, selinux_inode_rmdir),
6833 LSM_HOOK_INIT(inode_mknod, selinux_inode_mknod),
6834 LSM_HOOK_INIT(inode_rename, selinux_inode_rename),
6835 LSM_HOOK_INIT(inode_readlink, selinux_inode_readlink),
6836 LSM_HOOK_INIT(inode_follow_link, selinux_inode_follow_link),
6837 LSM_HOOK_INIT(inode_permission, selinux_inode_permission),
6838 LSM_HOOK_INIT(inode_setattr, selinux_inode_setattr),
6839 LSM_HOOK_INIT(inode_getattr, selinux_inode_getattr),
6840 LSM_HOOK_INIT(inode_setxattr, selinux_inode_setxattr),
6841 LSM_HOOK_INIT(inode_post_setxattr, selinux_inode_post_setxattr),
6842 LSM_HOOK_INIT(inode_getxattr, selinux_inode_getxattr),
6843 LSM_HOOK_INIT(inode_listxattr, selinux_inode_listxattr),
6844 LSM_HOOK_INIT(inode_removexattr, selinux_inode_removexattr),
6845 LSM_HOOK_INIT(inode_getsecurity, selinux_inode_getsecurity),
6846 LSM_HOOK_INIT(inode_setsecurity, selinux_inode_setsecurity),
6847 LSM_HOOK_INIT(inode_listsecurity, selinux_inode_listsecurity),
6848 LSM_HOOK_INIT(inode_getsecid, selinux_inode_getsecid),
Vivek Goyal56909eb2016-07-13 10:44:48 -04006849 LSM_HOOK_INIT(inode_copy_up, selinux_inode_copy_up),
Vivek Goyal19472b62016-07-13 10:44:50 -04006850 LSM_HOOK_INIT(inode_copy_up_xattr, selinux_inode_copy_up_xattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006851
Casey Schauflere20b0432015-05-02 15:11:36 -07006852 LSM_HOOK_INIT(file_permission, selinux_file_permission),
6853 LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security),
6854 LSM_HOOK_INIT(file_free_security, selinux_file_free_security),
6855 LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl),
6856 LSM_HOOK_INIT(mmap_file, selinux_mmap_file),
6857 LSM_HOOK_INIT(mmap_addr, selinux_mmap_addr),
6858 LSM_HOOK_INIT(file_mprotect, selinux_file_mprotect),
6859 LSM_HOOK_INIT(file_lock, selinux_file_lock),
6860 LSM_HOOK_INIT(file_fcntl, selinux_file_fcntl),
6861 LSM_HOOK_INIT(file_set_fowner, selinux_file_set_fowner),
6862 LSM_HOOK_INIT(file_send_sigiotask, selinux_file_send_sigiotask),
6863 LSM_HOOK_INIT(file_receive, selinux_file_receive),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006864
Casey Schauflere20b0432015-05-02 15:11:36 -07006865 LSM_HOOK_INIT(file_open, selinux_file_open),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006866
Tetsuo Handaa79be232017-03-28 23:08:45 +09006867 LSM_HOOK_INIT(task_alloc, selinux_task_alloc),
Casey Schauflere20b0432015-05-02 15:11:36 -07006868 LSM_HOOK_INIT(cred_alloc_blank, selinux_cred_alloc_blank),
6869 LSM_HOOK_INIT(cred_free, selinux_cred_free),
6870 LSM_HOOK_INIT(cred_prepare, selinux_cred_prepare),
6871 LSM_HOOK_INIT(cred_transfer, selinux_cred_transfer),
Matthew Garrett3ec30112018-01-08 13:36:19 -08006872 LSM_HOOK_INIT(cred_getsecid, selinux_cred_getsecid),
Casey Schauflere20b0432015-05-02 15:11:36 -07006873 LSM_HOOK_INIT(kernel_act_as, selinux_kernel_act_as),
6874 LSM_HOOK_INIT(kernel_create_files_as, selinux_kernel_create_files_as),
6875 LSM_HOOK_INIT(kernel_module_request, selinux_kernel_module_request),
Mimi Zoharc77b8cd2018-07-13 14:06:02 -04006876 LSM_HOOK_INIT(kernel_load_data, selinux_kernel_load_data),
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07006877 LSM_HOOK_INIT(kernel_read_file, selinux_kernel_read_file),
Casey Schauflere20b0432015-05-02 15:11:36 -07006878 LSM_HOOK_INIT(task_setpgid, selinux_task_setpgid),
6879 LSM_HOOK_INIT(task_getpgid, selinux_task_getpgid),
6880 LSM_HOOK_INIT(task_getsid, selinux_task_getsid),
6881 LSM_HOOK_INIT(task_getsecid, selinux_task_getsecid),
6882 LSM_HOOK_INIT(task_setnice, selinux_task_setnice),
6883 LSM_HOOK_INIT(task_setioprio, selinux_task_setioprio),
6884 LSM_HOOK_INIT(task_getioprio, selinux_task_getioprio),
Stephen Smalley791ec492017-02-17 07:57:00 -05006885 LSM_HOOK_INIT(task_prlimit, selinux_task_prlimit),
Casey Schauflere20b0432015-05-02 15:11:36 -07006886 LSM_HOOK_INIT(task_setrlimit, selinux_task_setrlimit),
6887 LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler),
6888 LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler),
6889 LSM_HOOK_INIT(task_movememory, selinux_task_movememory),
6890 LSM_HOOK_INIT(task_kill, selinux_task_kill),
Casey Schauflere20b0432015-05-02 15:11:36 -07006891 LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode),
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09006892
Casey Schauflere20b0432015-05-02 15:11:36 -07006893 LSM_HOOK_INIT(ipc_permission, selinux_ipc_permission),
6894 LSM_HOOK_INIT(ipc_getsecid, selinux_ipc_getsecid),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006895
Casey Schauflere20b0432015-05-02 15:11:36 -07006896 LSM_HOOK_INIT(msg_msg_alloc_security, selinux_msg_msg_alloc_security),
6897 LSM_HOOK_INIT(msg_msg_free_security, selinux_msg_msg_free_security),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006898
Casey Schauflere20b0432015-05-02 15:11:36 -07006899 LSM_HOOK_INIT(msg_queue_alloc_security,
6900 selinux_msg_queue_alloc_security),
6901 LSM_HOOK_INIT(msg_queue_free_security, selinux_msg_queue_free_security),
6902 LSM_HOOK_INIT(msg_queue_associate, selinux_msg_queue_associate),
6903 LSM_HOOK_INIT(msg_queue_msgctl, selinux_msg_queue_msgctl),
6904 LSM_HOOK_INIT(msg_queue_msgsnd, selinux_msg_queue_msgsnd),
6905 LSM_HOOK_INIT(msg_queue_msgrcv, selinux_msg_queue_msgrcv),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006906
Casey Schauflere20b0432015-05-02 15:11:36 -07006907 LSM_HOOK_INIT(shm_alloc_security, selinux_shm_alloc_security),
6908 LSM_HOOK_INIT(shm_free_security, selinux_shm_free_security),
6909 LSM_HOOK_INIT(shm_associate, selinux_shm_associate),
6910 LSM_HOOK_INIT(shm_shmctl, selinux_shm_shmctl),
6911 LSM_HOOK_INIT(shm_shmat, selinux_shm_shmat),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006912
Casey Schauflere20b0432015-05-02 15:11:36 -07006913 LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security),
6914 LSM_HOOK_INIT(sem_free_security, selinux_sem_free_security),
6915 LSM_HOOK_INIT(sem_associate, selinux_sem_associate),
6916 LSM_HOOK_INIT(sem_semctl, selinux_sem_semctl),
6917 LSM_HOOK_INIT(sem_semop, selinux_sem_semop),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006918
Casey Schauflere20b0432015-05-02 15:11:36 -07006919 LSM_HOOK_INIT(d_instantiate, selinux_d_instantiate),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006920
Casey Schauflere20b0432015-05-02 15:11:36 -07006921 LSM_HOOK_INIT(getprocattr, selinux_getprocattr),
6922 LSM_HOOK_INIT(setprocattr, selinux_setprocattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006923
Casey Schauflere20b0432015-05-02 15:11:36 -07006924 LSM_HOOK_INIT(ismaclabel, selinux_ismaclabel),
6925 LSM_HOOK_INIT(secid_to_secctx, selinux_secid_to_secctx),
6926 LSM_HOOK_INIT(secctx_to_secid, selinux_secctx_to_secid),
6927 LSM_HOOK_INIT(release_secctx, selinux_release_secctx),
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006928 LSM_HOOK_INIT(inode_invalidate_secctx, selinux_inode_invalidate_secctx),
Casey Schauflere20b0432015-05-02 15:11:36 -07006929 LSM_HOOK_INIT(inode_notifysecctx, selinux_inode_notifysecctx),
6930 LSM_HOOK_INIT(inode_setsecctx, selinux_inode_setsecctx),
6931 LSM_HOOK_INIT(inode_getsecctx, selinux_inode_getsecctx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006932
Casey Schauflere20b0432015-05-02 15:11:36 -07006933 LSM_HOOK_INIT(unix_stream_connect, selinux_socket_unix_stream_connect),
6934 LSM_HOOK_INIT(unix_may_send, selinux_socket_unix_may_send),
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006935
Casey Schauflere20b0432015-05-02 15:11:36 -07006936 LSM_HOOK_INIT(socket_create, selinux_socket_create),
6937 LSM_HOOK_INIT(socket_post_create, selinux_socket_post_create),
David Herrmann0b811db2018-05-04 16:28:21 +02006938 LSM_HOOK_INIT(socket_socketpair, selinux_socket_socketpair),
Casey Schauflere20b0432015-05-02 15:11:36 -07006939 LSM_HOOK_INIT(socket_bind, selinux_socket_bind),
6940 LSM_HOOK_INIT(socket_connect, selinux_socket_connect),
6941 LSM_HOOK_INIT(socket_listen, selinux_socket_listen),
6942 LSM_HOOK_INIT(socket_accept, selinux_socket_accept),
6943 LSM_HOOK_INIT(socket_sendmsg, selinux_socket_sendmsg),
6944 LSM_HOOK_INIT(socket_recvmsg, selinux_socket_recvmsg),
6945 LSM_HOOK_INIT(socket_getsockname, selinux_socket_getsockname),
6946 LSM_HOOK_INIT(socket_getpeername, selinux_socket_getpeername),
6947 LSM_HOOK_INIT(socket_getsockopt, selinux_socket_getsockopt),
6948 LSM_HOOK_INIT(socket_setsockopt, selinux_socket_setsockopt),
6949 LSM_HOOK_INIT(socket_shutdown, selinux_socket_shutdown),
6950 LSM_HOOK_INIT(socket_sock_rcv_skb, selinux_socket_sock_rcv_skb),
6951 LSM_HOOK_INIT(socket_getpeersec_stream,
6952 selinux_socket_getpeersec_stream),
6953 LSM_HOOK_INIT(socket_getpeersec_dgram, selinux_socket_getpeersec_dgram),
6954 LSM_HOOK_INIT(sk_alloc_security, selinux_sk_alloc_security),
6955 LSM_HOOK_INIT(sk_free_security, selinux_sk_free_security),
6956 LSM_HOOK_INIT(sk_clone_security, selinux_sk_clone_security),
6957 LSM_HOOK_INIT(sk_getsecid, selinux_sk_getsecid),
6958 LSM_HOOK_INIT(sock_graft, selinux_sock_graft),
Richard Hainesd4529302018-02-13 20:57:18 +00006959 LSM_HOOK_INIT(sctp_assoc_request, selinux_sctp_assoc_request),
6960 LSM_HOOK_INIT(sctp_sk_clone, selinux_sctp_sk_clone),
6961 LSM_HOOK_INIT(sctp_bind_connect, selinux_sctp_bind_connect),
Casey Schauflere20b0432015-05-02 15:11:36 -07006962 LSM_HOOK_INIT(inet_conn_request, selinux_inet_conn_request),
6963 LSM_HOOK_INIT(inet_csk_clone, selinux_inet_csk_clone),
6964 LSM_HOOK_INIT(inet_conn_established, selinux_inet_conn_established),
6965 LSM_HOOK_INIT(secmark_relabel_packet, selinux_secmark_relabel_packet),
6966 LSM_HOOK_INIT(secmark_refcount_inc, selinux_secmark_refcount_inc),
6967 LSM_HOOK_INIT(secmark_refcount_dec, selinux_secmark_refcount_dec),
6968 LSM_HOOK_INIT(req_classify_flow, selinux_req_classify_flow),
6969 LSM_HOOK_INIT(tun_dev_alloc_security, selinux_tun_dev_alloc_security),
6970 LSM_HOOK_INIT(tun_dev_free_security, selinux_tun_dev_free_security),
6971 LSM_HOOK_INIT(tun_dev_create, selinux_tun_dev_create),
6972 LSM_HOOK_INIT(tun_dev_attach_queue, selinux_tun_dev_attach_queue),
6973 LSM_HOOK_INIT(tun_dev_attach, selinux_tun_dev_attach),
6974 LSM_HOOK_INIT(tun_dev_open, selinux_tun_dev_open),
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006975#ifdef CONFIG_SECURITY_INFINIBAND
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006976 LSM_HOOK_INIT(ib_pkey_access, selinux_ib_pkey_access),
Daniel Jurgensab861df2017-05-19 15:48:58 +03006977 LSM_HOOK_INIT(ib_endport_manage_subnet,
6978 selinux_ib_endport_manage_subnet),
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006979 LSM_HOOK_INIT(ib_alloc_security, selinux_ib_alloc_security),
6980 LSM_HOOK_INIT(ib_free_security, selinux_ib_free_security),
6981#endif
Trent Jaegerd28d1e02005-12-13 23:12:40 -08006982#ifdef CONFIG_SECURITY_NETWORK_XFRM
Casey Schauflere20b0432015-05-02 15:11:36 -07006983 LSM_HOOK_INIT(xfrm_policy_alloc_security, selinux_xfrm_policy_alloc),
6984 LSM_HOOK_INIT(xfrm_policy_clone_security, selinux_xfrm_policy_clone),
6985 LSM_HOOK_INIT(xfrm_policy_free_security, selinux_xfrm_policy_free),
6986 LSM_HOOK_INIT(xfrm_policy_delete_security, selinux_xfrm_policy_delete),
6987 LSM_HOOK_INIT(xfrm_state_alloc, selinux_xfrm_state_alloc),
6988 LSM_HOOK_INIT(xfrm_state_alloc_acquire,
6989 selinux_xfrm_state_alloc_acquire),
6990 LSM_HOOK_INIT(xfrm_state_free_security, selinux_xfrm_state_free),
6991 LSM_HOOK_INIT(xfrm_state_delete_security, selinux_xfrm_state_delete),
6992 LSM_HOOK_INIT(xfrm_policy_lookup, selinux_xfrm_policy_lookup),
6993 LSM_HOOK_INIT(xfrm_state_pol_flow_match,
6994 selinux_xfrm_state_pol_flow_match),
6995 LSM_HOOK_INIT(xfrm_decode_session, selinux_xfrm_decode_session),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006996#endif
Michael LeMayd7200242006-06-22 14:47:17 -07006997
6998#ifdef CONFIG_KEYS
Casey Schauflere20b0432015-05-02 15:11:36 -07006999 LSM_HOOK_INIT(key_alloc, selinux_key_alloc),
7000 LSM_HOOK_INIT(key_free, selinux_key_free),
7001 LSM_HOOK_INIT(key_permission, selinux_key_permission),
7002 LSM_HOOK_INIT(key_getsecurity, selinux_key_getsecurity),
Michael LeMayd7200242006-06-22 14:47:17 -07007003#endif
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02007004
7005#ifdef CONFIG_AUDIT
Casey Schauflere20b0432015-05-02 15:11:36 -07007006 LSM_HOOK_INIT(audit_rule_init, selinux_audit_rule_init),
7007 LSM_HOOK_INIT(audit_rule_known, selinux_audit_rule_known),
7008 LSM_HOOK_INIT(audit_rule_match, selinux_audit_rule_match),
7009 LSM_HOOK_INIT(audit_rule_free, selinux_audit_rule_free),
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02007010#endif
Chenbo Fengec27c352017-10-18 13:00:25 -07007011
7012#ifdef CONFIG_BPF_SYSCALL
7013 LSM_HOOK_INIT(bpf, selinux_bpf),
7014 LSM_HOOK_INIT(bpf_map, selinux_bpf_map),
7015 LSM_HOOK_INIT(bpf_prog, selinux_bpf_prog),
7016 LSM_HOOK_INIT(bpf_map_alloc_security, selinux_bpf_map_alloc),
7017 LSM_HOOK_INIT(bpf_prog_alloc_security, selinux_bpf_prog_alloc),
7018 LSM_HOOK_INIT(bpf_map_free_security, selinux_bpf_map_free),
7019 LSM_HOOK_INIT(bpf_prog_free_security, selinux_bpf_prog_free),
7020#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007021};
7022
7023static __init int selinux_init(void)
7024{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07007025 if (!security_module_enable("selinux")) {
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02007026 selinux_enabled = 0;
7027 return 0;
7028 }
7029
Linus Torvalds1da177e2005-04-16 15:20:36 -07007030 if (!selinux_enabled) {
peter enderborgc103a912018-06-12 10:09:03 +02007031 pr_info("SELinux: Disabled at boot.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007032 return 0;
7033 }
7034
peter enderborgc103a912018-06-12 10:09:03 +02007035 pr_info("SELinux: Initializing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007036
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007037 memset(&selinux_state, 0, sizeof(selinux_state));
Paul Mooree5a5ca92018-03-01 17:38:30 -05007038 enforcing_set(&selinux_state, selinux_enforcing_boot);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007039 selinux_state.checkreqprot = selinux_checkreqprot_boot;
7040 selinux_ss_init(&selinux_state.ss);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05007041 selinux_avc_init(&selinux_state.avc);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007042
Linus Torvalds1da177e2005-04-16 15:20:36 -07007043 /* Set the security state for the initial task. */
David Howellsd84f4f92008-11-14 10:39:23 +11007044 cred_init_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007045
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04007046 default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
7047
James Morris7cae7e22006-03-22 00:09:22 -08007048 sel_inode_cache = kmem_cache_create("selinux_inode_security",
7049 sizeof(struct inode_security_struct),
Paul Mundt20c2df82007-07-20 10:11:58 +09007050 0, SLAB_PANIC, NULL);
Sangwoo63205652015-10-21 17:44:30 -04007051 file_security_cache = kmem_cache_create("selinux_file_security",
7052 sizeof(struct file_security_struct),
7053 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007054 avc_init();
7055
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007056 avtab_cache_init();
7057
7058 ebitmap_cache_init();
7059
7060 hashtab_cache_init();
7061
Casey Schauflerd69dece52017-01-18 17:09:05 -08007062 security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks), "selinux");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007063
Paul Moore615e51f2014-06-26 14:33:56 -04007064 if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET))
7065 panic("SELinux: Unable to register AVC netcache callback\n");
7066
Daniel Jurgens8f408ab2017-05-19 15:48:53 +03007067 if (avc_add_callback(selinux_lsm_notifier_avc_callback, AVC_CALLBACK_RESET))
7068 panic("SELinux: Unable to register AVC LSM notifier callback\n");
7069
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007070 if (selinux_enforcing_boot)
peter enderborgc103a912018-06-12 10:09:03 +02007071 pr_debug("SELinux: Starting in enforcing mode\n");
Eric Paris828dfe12008-04-17 13:17:49 -04007072 else
peter enderborgc103a912018-06-12 10:09:03 +02007073 pr_debug("SELinux: Starting in permissive mode\n");
Michael LeMayd7200242006-06-22 14:47:17 -07007074
Linus Torvalds1da177e2005-04-16 15:20:36 -07007075 return 0;
7076}
7077
Al Viroe8c26252010-03-23 06:36:54 -04007078static void delayed_superblock_init(struct super_block *sb, void *unused)
7079{
Al Viro204cc0c2018-12-13 13:41:47 -05007080 selinux_set_mnt_opts(sb, NULL, 0, NULL);
Al Viroe8c26252010-03-23 06:36:54 -04007081}
7082
Linus Torvalds1da177e2005-04-16 15:20:36 -07007083void selinux_complete_init(void)
7084{
peter enderborgc103a912018-06-12 10:09:03 +02007085 pr_debug("SELinux: Completing initialization.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007086
7087 /* Set up any superblocks initialized prior to the policy load. */
peter enderborgc103a912018-06-12 10:09:03 +02007088 pr_debug("SELinux: Setting up existing superblocks.\n");
Al Viroe8c26252010-03-23 06:36:54 -04007089 iterate_supers(delayed_superblock_init, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007090}
7091
7092/* SELinux requires early initialization in order to label
7093 all processes and objects when they are created. */
Kees Cook3d6e5f62018-10-10 17:18:23 -07007094DEFINE_LSM(selinux) = {
Kees Cook07aed2f2018-10-10 17:18:24 -07007095 .name = "selinux",
Kees Cook3d6e5f62018-10-10 17:18:23 -07007096 .init = selinux_init,
7097};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007098
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007099#if defined(CONFIG_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007100
Florian Westphal591bb272017-07-26 11:40:52 +02007101static const struct nf_hook_ops selinux_nf_ops[] = {
Paul Mooreeffad8d2008-01-29 08:49:27 -05007102 {
7103 .hook = selinux_ipv4_postroute,
Alban Crequy2597a832012-05-14 03:56:39 +00007104 .pf = NFPROTO_IPV4,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007105 .hooknum = NF_INET_POST_ROUTING,
7106 .priority = NF_IP_PRI_SELINUX_LAST,
7107 },
7108 {
7109 .hook = selinux_ipv4_forward,
Alban Crequy2597a832012-05-14 03:56:39 +00007110 .pf = NFPROTO_IPV4,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007111 .hooknum = NF_INET_FORWARD,
7112 .priority = NF_IP_PRI_SELINUX_FIRST,
Paul Moore948bf852008-10-10 10:16:32 -04007113 },
7114 {
7115 .hook = selinux_ipv4_output,
Alban Crequy2597a832012-05-14 03:56:39 +00007116 .pf = NFPROTO_IPV4,
Paul Moore948bf852008-10-10 10:16:32 -04007117 .hooknum = NF_INET_LOCAL_OUT,
7118 .priority = NF_IP_PRI_SELINUX_FIRST,
Jiri Pirko25db6be2014-09-03 17:42:13 +02007119 },
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04007120#if IS_ENABLED(CONFIG_IPV6)
Paul Mooreeffad8d2008-01-29 08:49:27 -05007121 {
7122 .hook = selinux_ipv6_postroute,
Alban Crequy2597a832012-05-14 03:56:39 +00007123 .pf = NFPROTO_IPV6,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007124 .hooknum = NF_INET_POST_ROUTING,
7125 .priority = NF_IP6_PRI_SELINUX_LAST,
7126 },
7127 {
7128 .hook = selinux_ipv6_forward,
Alban Crequy2597a832012-05-14 03:56:39 +00007129 .pf = NFPROTO_IPV6,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007130 .hooknum = NF_INET_FORWARD,
7131 .priority = NF_IP6_PRI_SELINUX_FIRST,
Jiri Pirko25db6be2014-09-03 17:42:13 +02007132 },
Huw Davies2917f572016-06-27 15:06:15 -04007133 {
7134 .hook = selinux_ipv6_output,
7135 .pf = NFPROTO_IPV6,
7136 .hooknum = NF_INET_LOCAL_OUT,
7137 .priority = NF_IP6_PRI_SELINUX_FIRST,
7138 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07007139#endif /* IPV6 */
Jiri Pirko25db6be2014-09-03 17:42:13 +02007140};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007141
Florian Westphal8e71bf72017-04-21 11:49:09 +02007142static int __net_init selinux_nf_register(struct net *net)
7143{
7144 return nf_register_net_hooks(net, selinux_nf_ops,
7145 ARRAY_SIZE(selinux_nf_ops));
7146}
7147
7148static void __net_exit selinux_nf_unregister(struct net *net)
7149{
7150 nf_unregister_net_hooks(net, selinux_nf_ops,
7151 ARRAY_SIZE(selinux_nf_ops));
7152}
7153
7154static struct pernet_operations selinux_net_ops = {
7155 .init = selinux_nf_register,
7156 .exit = selinux_nf_unregister,
7157};
7158
Linus Torvalds1da177e2005-04-16 15:20:36 -07007159static int __init selinux_nf_ip_init(void)
7160{
Jiri Pirko25db6be2014-09-03 17:42:13 +02007161 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007162
7163 if (!selinux_enabled)
Jiri Pirko25db6be2014-09-03 17:42:13 +02007164 return 0;
Eric Parisfadcdb42007-02-22 18:11:31 -05007165
peter enderborgc103a912018-06-12 10:09:03 +02007166 pr_debug("SELinux: Registering netfilter hooks\n");
Eric Parisfadcdb42007-02-22 18:11:31 -05007167
Florian Westphal8e71bf72017-04-21 11:49:09 +02007168 err = register_pernet_subsys(&selinux_net_ops);
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07007169 if (err)
Florian Westphal8e71bf72017-04-21 11:49:09 +02007170 panic("SELinux: register_pernet_subsys: error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007171
Jiri Pirko25db6be2014-09-03 17:42:13 +02007172 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007173}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007174__initcall(selinux_nf_ip_init);
7175
7176#ifdef CONFIG_SECURITY_SELINUX_DISABLE
7177static void selinux_nf_ip_exit(void)
7178{
peter enderborgc103a912018-06-12 10:09:03 +02007179 pr_debug("SELinux: Unregistering netfilter hooks\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007180
Florian Westphal8e71bf72017-04-21 11:49:09 +02007181 unregister_pernet_subsys(&selinux_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007182}
7183#endif
7184
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007185#else /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007186
7187#ifdef CONFIG_SECURITY_SELINUX_DISABLE
7188#define selinux_nf_ip_exit()
7189#endif
7190
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007191#endif /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007192
7193#ifdef CONFIG_SECURITY_SELINUX_DISABLE
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007194int selinux_disable(struct selinux_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007195{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007196 if (state->initialized) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007197 /* Not permitted after initial policy load. */
7198 return -EINVAL;
7199 }
7200
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007201 if (state->disabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007202 /* Only do this once. */
7203 return -EINVAL;
7204 }
7205
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007206 state->disabled = 1;
7207
peter enderborgc103a912018-06-12 10:09:03 +02007208 pr_info("SELinux: Disabled at runtime.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007209
Stephen Smalley30d55282006-05-03 10:52:36 -04007210 selinux_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007211
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07007212 security_delete_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007213
Eric Parisaf8ff042009-09-20 21:23:01 -04007214 /* Try to destroy the avc node cache */
7215 avc_disable();
7216
Linus Torvalds1da177e2005-04-16 15:20:36 -07007217 /* Unregister netfilter hooks. */
7218 selinux_nf_ip_exit();
7219
7220 /* Unregister selinuxfs. */
7221 exit_sel_fs();
7222
7223 return 0;
7224}
7225#endif