blob: 5336d6671c5c62cbcbaadbceed872197676fcbc8 [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 Virobd323652018-12-13 15:04:59 -0500436struct selinux_mnt_opts {
437 const char *fscontext, *context, *rootcontext, *defcontext;
438};
439
Al Viro204cc0c2018-12-13 13:41:47 -0500440static void selinux_free_mnt_opts(void *mnt_opts)
441{
Al Virobd323652018-12-13 15:04:59 -0500442 struct selinux_mnt_opts *opts = mnt_opts;
443 kfree(opts->fscontext);
444 kfree(opts->context);
445 kfree(opts->rootcontext);
446 kfree(opts->defcontext);
Al Viro204cc0c2018-12-13 13:41:47 -0500447 kfree(opts);
448}
449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450static inline int inode_doinit(struct inode *inode)
451{
452 return inode_doinit_with_dentry(inode, NULL);
453}
454
455enum {
Eric Paris31e87932007-09-19 17:19:12 -0400456 Opt_error = -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 Opt_context = 1,
458 Opt_fscontext = 2,
Eric Parisc9180a52007-11-30 13:00:35 -0500459 Opt_defcontext = 3,
460 Opt_rootcontext = 4,
Al Viroda3d76a2018-12-17 10:14:16 -0500461 Opt_seclabel = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462};
463
Al Viroda3d76a2018-12-17 10:14:16 -0500464#define A(s, has_arg) {#s, sizeof(#s) - 1, Opt_##s, has_arg}
Al Viro169d68efb2018-12-14 22:44:50 -0500465static struct {
466 const char *name;
467 int len;
468 int opt;
469 bool has_arg;
470} tokens[] = {
Al Viroda3d76a2018-12-17 10:14:16 -0500471 A(context, true),
472 A(fscontext, true),
473 A(defcontext, true),
474 A(rootcontext, true),
475 A(seclabel, false),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476};
Al Viro169d68efb2018-12-14 22:44:50 -0500477#undef A
478
479static int match_opt_prefix(char *s, int l, char **arg)
480{
481 int i;
482
483 for (i = 0; i < ARRAY_SIZE(tokens); i++) {
484 size_t len = tokens[i].len;
485 if (len > l || memcmp(s, tokens[i].name, len))
486 continue;
487 if (tokens[i].has_arg) {
488 if (len == l || s[len] != '=')
489 continue;
490 *arg = s + len + 1;
491 } else if (len != l)
492 continue;
493 return tokens[i].opt;
494 }
495 return Opt_error;
496}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498#define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n"
499
Eric Parisc312feb2006-07-10 04:43:53 -0700500static int may_context_mount_sb_relabel(u32 sid,
501 struct superblock_security_struct *sbsec,
David Howells275bb412008-11-14 10:39:19 +1100502 const struct cred *cred)
Eric Parisc312feb2006-07-10 04:43:53 -0700503{
David Howells275bb412008-11-14 10:39:19 +1100504 const struct task_security_struct *tsec = cred->security;
Eric Parisc312feb2006-07-10 04:43:53 -0700505 int rc;
506
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500507 rc = avc_has_perm(&selinux_state,
508 tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
Eric Parisc312feb2006-07-10 04:43:53 -0700509 FILESYSTEM__RELABELFROM, NULL);
510 if (rc)
511 return rc;
512
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500513 rc = avc_has_perm(&selinux_state,
514 tsec->sid, sid, SECCLASS_FILESYSTEM,
Eric Parisc312feb2006-07-10 04:43:53 -0700515 FILESYSTEM__RELABELTO, NULL);
516 return rc;
517}
518
Eric Paris08089252006-07-10 04:43:55 -0700519static int may_context_mount_inode_relabel(u32 sid,
520 struct superblock_security_struct *sbsec,
David Howells275bb412008-11-14 10:39:19 +1100521 const struct cred *cred)
Eric Paris08089252006-07-10 04:43:55 -0700522{
David Howells275bb412008-11-14 10:39:19 +1100523 const struct task_security_struct *tsec = cred->security;
Eric Paris08089252006-07-10 04:43:55 -0700524 int rc;
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500525 rc = avc_has_perm(&selinux_state,
526 tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
Eric Paris08089252006-07-10 04:43:55 -0700527 FILESYSTEM__RELABELFROM, NULL);
528 if (rc)
529 return rc;
530
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500531 rc = avc_has_perm(&selinux_state,
532 sid, sbsec->sid, SECCLASS_FILESYSTEM,
Eric Paris08089252006-07-10 04:43:55 -0700533 FILESYSTEM__ASSOCIATE, NULL);
534 return rc;
535}
536
Eric Parisb43e7252012-10-10 14:27:35 -0400537static int selinux_is_sblabel_mnt(struct super_block *sb)
538{
539 struct superblock_security_struct *sbsec = sb->s_security;
540
Mark Salyzynd5f3a5f2015-02-04 11:34:30 -0500541 return sbsec->behavior == SECURITY_FS_USE_XATTR ||
542 sbsec->behavior == SECURITY_FS_USE_TRANS ||
543 sbsec->behavior == SECURITY_FS_USE_TASK ||
J. Bruce Fields9fc2b4b2015-06-04 15:57:25 -0400544 sbsec->behavior == SECURITY_FS_USE_NATIVE ||
Mark Salyzynd5f3a5f2015-02-04 11:34:30 -0500545 /* Special handling. Genfs but also in-core setxattr handler */
546 !strcmp(sb->s_type->name, "sysfs") ||
547 !strcmp(sb->s_type->name, "pstore") ||
548 !strcmp(sb->s_type->name, "debugfs") ||
Yongqin Liua2c7c6f2017-01-09 10:07:30 -0500549 !strcmp(sb->s_type->name, "tracefs") ||
Stephen Smalley2651225b2017-02-28 10:35:56 -0500550 !strcmp(sb->s_type->name, "rootfs") ||
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500551 (selinux_policycap_cgroupseclabel() &&
Stephen Smalley2651225b2017-02-28 10:35:56 -0500552 (!strcmp(sb->s_type->name, "cgroup") ||
553 !strcmp(sb->s_type->name, "cgroup2")));
Eric Parisb43e7252012-10-10 14:27:35 -0400554}
555
Eric Parisc9180a52007-11-30 13:00:35 -0500556static int sb_finish_set_opts(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
558 struct superblock_security_struct *sbsec = sb->s_security;
559 struct dentry *root = sb->s_root;
David Howellsc6f493d2015-03-17 22:26:22 +0000560 struct inode *root_inode = d_backing_inode(root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 int rc = 0;
562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
564 /* Make sure that the xattr handler exists and that no
565 error other than -ENODATA is returned by getxattr on
566 the root directory. -ENODATA is ok, as this may be
567 the first boot of the SELinux kernel before we have
568 assigned xattr values to the filesystem. */
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200569 if (!(root_inode->i_opflags & IOP_XATTR)) {
peter enderborgc103a912018-06-12 10:09:03 +0200570 pr_warn("SELinux: (dev %s, type %s) has no "
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800571 "xattr support\n", sb->s_id, sb->s_type->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 rc = -EOPNOTSUPP;
573 goto out;
574 }
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200575
576 rc = __vfs_getxattr(root, root_inode, XATTR_NAME_SELINUX, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 if (rc < 0 && rc != -ENODATA) {
578 if (rc == -EOPNOTSUPP)
peter enderborgc103a912018-06-12 10:09:03 +0200579 pr_warn("SELinux: (dev %s, type "
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800580 "%s) has no security xattr handler\n",
581 sb->s_id, sb->s_type->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 else
peter enderborgc103a912018-06-12 10:09:03 +0200583 pr_warn("SELinux: (dev %s, type "
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800584 "%s) getxattr errno %d\n", sb->s_id,
585 sb->s_type->name, -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 goto out;
587 }
588 }
589
Eric Pariseadcabc2012-08-24 15:59:14 -0400590 sbsec->flags |= SE_SBINITIALIZED;
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400591
592 /*
593 * Explicitly set or clear SBLABEL_MNT. It's not sufficient to simply
594 * leave the flag untouched because sb_clone_mnt_opts might be handing
595 * us a superblock that needs the flag to be cleared.
596 */
Eric Parisb43e7252012-10-10 14:27:35 -0400597 if (selinux_is_sblabel_mnt(sb))
Eric Paris12f348b2012-10-09 10:56:25 -0400598 sbsec->flags |= SBLABEL_MNT;
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400599 else
600 sbsec->flags &= ~SBLABEL_MNT;
David P. Quigleyddd29ec2009-09-09 14:25:37 -0400601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 /* Initialize the root inode. */
Eric Parisc9180a52007-11-30 13:00:35 -0500603 rc = inode_doinit_with_dentry(root_inode, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 /* Initialize any other inodes associated with the superblock, e.g.
606 inodes created prior to initial policy load or inodes created
607 during get_sb by a pseudo filesystem that directly
608 populates itself. */
609 spin_lock(&sbsec->isec_lock);
Al Viro8d641242018-12-10 15:34:12 -0500610 while (!list_empty(&sbsec->isec_head)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 struct inode_security_struct *isec =
Al Viro8d641242018-12-10 15:34:12 -0500612 list_first_entry(&sbsec->isec_head,
Eric Parisc9180a52007-11-30 13:00:35 -0500613 struct inode_security_struct, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 struct inode *inode = isec->inode;
Stephen Smalley923190d2014-10-06 16:32:52 -0400615 list_del_init(&isec->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 spin_unlock(&sbsec->isec_lock);
617 inode = igrab(inode);
618 if (inode) {
Eric Parisc9180a52007-11-30 13:00:35 -0500619 if (!IS_PRIVATE(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 inode_doinit(inode);
621 iput(inode);
622 }
623 spin_lock(&sbsec->isec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 }
625 spin_unlock(&sbsec->isec_lock);
626out:
Eric Parisc9180a52007-11-30 13:00:35 -0500627 return rc;
628}
629
Eric Parisc9180a52007-11-30 13:00:35 -0500630static int bad_option(struct superblock_security_struct *sbsec, char flag,
631 u32 old_sid, u32 new_sid)
632{
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500633 char mnt_flags = sbsec->flags & SE_MNTMASK;
634
Eric Parisc9180a52007-11-30 13:00:35 -0500635 /* check if the old mount command had the same options */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500636 if (sbsec->flags & SE_SBINITIALIZED)
Eric Parisc9180a52007-11-30 13:00:35 -0500637 if (!(sbsec->flags & flag) ||
638 (old_sid != new_sid))
639 return 1;
640
641 /* check if we were passed the same options twice,
642 * aka someone passed context=a,context=b
643 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500644 if (!(sbsec->flags & SE_SBINITIALIZED))
645 if (mnt_flags & flag)
Eric Parisc9180a52007-11-30 13:00:35 -0500646 return 1;
647 return 0;
648}
Eric Parise0007522008-03-05 10:31:54 -0500649
Al Virobd323652018-12-13 15:04:59 -0500650static int parse_sid(struct super_block *sb, const char *s, u32 *sid)
651{
652 int rc = security_context_str_to_sid(&selinux_state, s,
653 sid, GFP_KERNEL);
654 if (rc)
655 pr_warn("SELinux: security_context_str_to_sid"
656 "(%s) failed for (dev %s, type %s) errno=%d\n",
657 s, sb->s_id, sb->s_type->name, rc);
658 return rc;
659}
660
Eric Parisc9180a52007-11-30 13:00:35 -0500661/*
662 * Allow filesystems with binary mount data to explicitly set mount point
663 * labeling information.
664 */
Eric Parise0007522008-03-05 10:31:54 -0500665static int selinux_set_mnt_opts(struct super_block *sb,
Al Viro204cc0c2018-12-13 13:41:47 -0500666 void *mnt_opts,
David Quigley649f6e72013-05-22 12:50:36 -0400667 unsigned long kern_flags,
668 unsigned long *set_kern_flags)
Eric Parisc9180a52007-11-30 13:00:35 -0500669{
David Howells275bb412008-11-14 10:39:19 +1100670 const struct cred *cred = current_cred();
Eric Parisc9180a52007-11-30 13:00:35 -0500671 struct superblock_security_struct *sbsec = sb->s_security;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500672 struct dentry *root = sbsec->sb->s_root;
Al Virobd323652018-12-13 15:04:59 -0500673 struct selinux_mnt_opts *opts = mnt_opts;
Paul Moore2c971652016-04-19 16:36:28 -0400674 struct inode_security_struct *root_isec;
Eric Parisc9180a52007-11-30 13:00:35 -0500675 u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
676 u32 defcontext_sid = 0;
Al Virobd323652018-12-13 15:04:59 -0500677 int rc = 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500678
679 mutex_lock(&sbsec->lock);
680
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500681 if (!selinux_state.initialized) {
Al Virobd323652018-12-13 15:04:59 -0500682 if (!opts) {
Eric Parisc9180a52007-11-30 13:00:35 -0500683 /* Defer initialization until selinux_complete_init,
684 after the initial policy is loaded and the security
685 server is ready to handle calls. */
Eric Parisc9180a52007-11-30 13:00:35 -0500686 goto out;
687 }
688 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +0200689 pr_warn("SELinux: Unable to set superblock options "
Eric Paris744ba352008-04-17 11:52:44 -0400690 "before the security server is initialized\n");
Eric Parisc9180a52007-11-30 13:00:35 -0500691 goto out;
692 }
David Quigley649f6e72013-05-22 12:50:36 -0400693 if (kern_flags && !set_kern_flags) {
694 /* Specifying internal flags without providing a place to
695 * place the results is not allowed */
696 rc = -EINVAL;
697 goto out;
698 }
Eric Parisc9180a52007-11-30 13:00:35 -0500699
700 /*
Eric Parise0007522008-03-05 10:31:54 -0500701 * Binary mount data FS will come through this function twice. Once
702 * from an explicit call and once from the generic calls from the vfs.
703 * Since the generic VFS calls will not contain any security mount data
704 * we need to skip the double mount verification.
705 *
706 * This does open a hole in which we will not notice if the first
707 * mount using this sb set explict options and a second mount using
708 * this sb does not set any security options. (The first options
709 * will be used for both mounts)
710 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500711 if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
Al Virobd323652018-12-13 15:04:59 -0500712 && !opts)
Eric Parisf5269712008-05-14 11:27:45 -0400713 goto out;
Eric Parise0007522008-03-05 10:31:54 -0500714
Paul Moore2c971652016-04-19 16:36:28 -0400715 root_isec = backing_inode_security_novalidate(root);
716
Eric Parise0007522008-03-05 10:31:54 -0500717 /*
Eric Parisc9180a52007-11-30 13:00:35 -0500718 * parse the mount options, check if they are valid sids.
719 * also check if someone is trying to mount the same sb more
720 * than once with different security options.
721 */
Al Virobd323652018-12-13 15:04:59 -0500722 if (opts) {
723 if (opts->fscontext) {
724 rc = parse_sid(sb, opts->fscontext, &fscontext_sid);
725 if (rc)
726 goto out;
Eric Parisc9180a52007-11-30 13:00:35 -0500727 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
728 fscontext_sid))
729 goto out_double_mount;
Eric Parisc9180a52007-11-30 13:00:35 -0500730 sbsec->flags |= FSCONTEXT_MNT;
Al Virobd323652018-12-13 15:04:59 -0500731 }
732 if (opts->context) {
733 rc = parse_sid(sb, opts->context, &context_sid);
734 if (rc)
735 goto out;
Eric Parisc9180a52007-11-30 13:00:35 -0500736 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
737 context_sid))
738 goto out_double_mount;
Eric Parisc9180a52007-11-30 13:00:35 -0500739 sbsec->flags |= CONTEXT_MNT;
Al Virobd323652018-12-13 15:04:59 -0500740 }
741 if (opts->rootcontext) {
742 rc = parse_sid(sb, opts->rootcontext, &rootcontext_sid);
743 if (rc)
744 goto out;
Eric Parisc9180a52007-11-30 13:00:35 -0500745 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
746 rootcontext_sid))
747 goto out_double_mount;
Eric Parisc9180a52007-11-30 13:00:35 -0500748 sbsec->flags |= ROOTCONTEXT_MNT;
Al Virobd323652018-12-13 15:04:59 -0500749 }
750 if (opts->defcontext) {
751 rc = parse_sid(sb, opts->defcontext, &defcontext_sid);
752 if (rc)
753 goto out;
Eric Parisc9180a52007-11-30 13:00:35 -0500754 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
755 defcontext_sid))
756 goto out_double_mount;
Eric Parisc9180a52007-11-30 13:00:35 -0500757 sbsec->flags |= DEFCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500758 }
759 }
760
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500761 if (sbsec->flags & SE_SBINITIALIZED) {
Eric Parisc9180a52007-11-30 13:00:35 -0500762 /* previously mounted with options, but not on this attempt? */
Al Virobd323652018-12-13 15:04:59 -0500763 if ((sbsec->flags & SE_MNTMASK) && !opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500764 goto out_double_mount;
765 rc = 0;
766 goto out;
767 }
768
James Morris089be432008-07-15 18:32:49 +1000769 if (strcmp(sb->s_type->name, "proc") == 0)
Stephen Smalley134509d2015-06-04 16:22:17 -0400770 sbsec->flags |= SE_SBPROC | SE_SBGENFS;
771
Stephen Smalley8e014722015-06-04 16:22:17 -0400772 if (!strcmp(sb->s_type->name, "debugfs") ||
Jeff Vander Stoep6a391182017-06-20 09:35:33 -0700773 !strcmp(sb->s_type->name, "tracefs") ||
Stephen Smalley8e014722015-06-04 16:22:17 -0400774 !strcmp(sb->s_type->name, "sysfs") ||
Antonio Murdaca901ef842017-02-09 17:02:42 +0100775 !strcmp(sb->s_type->name, "pstore") ||
776 !strcmp(sb->s_type->name, "cgroup") ||
777 !strcmp(sb->s_type->name, "cgroup2"))
Stephen Smalley134509d2015-06-04 16:22:17 -0400778 sbsec->flags |= SE_SBGENFS;
Eric Parisc9180a52007-11-30 13:00:35 -0500779
David Quigleyeb9ae682013-05-22 12:50:37 -0400780 if (!sbsec->behavior) {
781 /*
782 * Determine the labeling behavior to use for this
783 * filesystem type.
784 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500785 rc = security_fs_use(&selinux_state, sb);
David Quigleyeb9ae682013-05-22 12:50:37 -0400786 if (rc) {
peter enderborgc103a912018-06-12 10:09:03 +0200787 pr_warn("%s: security_fs_use(%s) returned %d\n",
David Quigleyeb9ae682013-05-22 12:50:37 -0400788 __func__, sb->s_type->name, rc);
789 goto out;
790 }
Eric Parisc9180a52007-11-30 13:00:35 -0500791 }
Seth Forsheeaad82892016-04-26 14:36:20 -0500792
793 /*
Stephen Smalley01593d32017-01-09 10:07:31 -0500794 * If this is a user namespace mount and the filesystem type is not
795 * explicitly whitelisted, then no contexts are allowed on the command
796 * line and security labels must be ignored.
Seth Forsheeaad82892016-04-26 14:36:20 -0500797 */
Stephen Smalley01593d32017-01-09 10:07:31 -0500798 if (sb->s_user_ns != &init_user_ns &&
799 strcmp(sb->s_type->name, "tmpfs") &&
800 strcmp(sb->s_type->name, "ramfs") &&
801 strcmp(sb->s_type->name, "devpts")) {
Seth Forsheeaad82892016-04-26 14:36:20 -0500802 if (context_sid || fscontext_sid || rootcontext_sid ||
803 defcontext_sid) {
804 rc = -EACCES;
805 goto out;
806 }
807 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
808 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500809 rc = security_transition_sid(&selinux_state,
810 current_sid(),
811 current_sid(),
Seth Forsheeaad82892016-04-26 14:36:20 -0500812 SECCLASS_FILE, NULL,
813 &sbsec->mntpoint_sid);
814 if (rc)
815 goto out;
816 }
817 goto out_set_opts;
818 }
819
Eric Parisc9180a52007-11-30 13:00:35 -0500820 /* sets the context of the superblock for the fs being mounted. */
821 if (fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100822 rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500823 if (rc)
824 goto out;
825
826 sbsec->sid = fscontext_sid;
827 }
828
829 /*
830 * Switch to using mount point labeling behavior.
831 * sets the label used on all file below the mountpoint, and will set
832 * the superblock context if not already set.
833 */
David Quigleyeb9ae682013-05-22 12:50:37 -0400834 if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !context_sid) {
835 sbsec->behavior = SECURITY_FS_USE_NATIVE;
836 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
837 }
838
Eric Parisc9180a52007-11-30 13:00:35 -0500839 if (context_sid) {
840 if (!fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100841 rc = may_context_mount_sb_relabel(context_sid, sbsec,
842 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500843 if (rc)
844 goto out;
845 sbsec->sid = context_sid;
846 } else {
David Howells275bb412008-11-14 10:39:19 +1100847 rc = may_context_mount_inode_relabel(context_sid, sbsec,
848 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500849 if (rc)
850 goto out;
851 }
852 if (!rootcontext_sid)
853 rootcontext_sid = context_sid;
854
855 sbsec->mntpoint_sid = context_sid;
856 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
857 }
858
859 if (rootcontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100860 rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec,
861 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500862 if (rc)
863 goto out;
864
865 root_isec->sid = rootcontext_sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -0500866 root_isec->initialized = LABEL_INITIALIZED;
Eric Parisc9180a52007-11-30 13:00:35 -0500867 }
868
869 if (defcontext_sid) {
David Quigleyeb9ae682013-05-22 12:50:37 -0400870 if (sbsec->behavior != SECURITY_FS_USE_XATTR &&
871 sbsec->behavior != SECURITY_FS_USE_NATIVE) {
Eric Parisc9180a52007-11-30 13:00:35 -0500872 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +0200873 pr_warn("SELinux: defcontext option is "
Eric Parisc9180a52007-11-30 13:00:35 -0500874 "invalid for this filesystem type\n");
875 goto out;
876 }
877
878 if (defcontext_sid != sbsec->def_sid) {
879 rc = may_context_mount_inode_relabel(defcontext_sid,
David Howells275bb412008-11-14 10:39:19 +1100880 sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500881 if (rc)
882 goto out;
883 }
884
885 sbsec->def_sid = defcontext_sid;
886 }
887
Seth Forsheeaad82892016-04-26 14:36:20 -0500888out_set_opts:
Eric Parisc9180a52007-11-30 13:00:35 -0500889 rc = sb_finish_set_opts(sb);
890out:
Eric Parisbc7e9822006-09-25 23:32:02 -0700891 mutex_unlock(&sbsec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 return rc;
Eric Parisc9180a52007-11-30 13:00:35 -0500893out_double_mount:
894 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +0200895 pr_warn("SELinux: mount invalid. Same superblock, different "
Al Virobd323652018-12-13 15:04:59 -0500896 "security settings for (dev %s, type %s)\n", sb->s_id,
897 sb->s_type->name);
Eric Parisc9180a52007-11-30 13:00:35 -0500898 goto out;
899}
900
Jeff Layton094f7b62013-04-01 08:14:24 -0400901static int selinux_cmp_sb_context(const struct super_block *oldsb,
902 const struct super_block *newsb)
903{
904 struct superblock_security_struct *old = oldsb->s_security;
905 struct superblock_security_struct *new = newsb->s_security;
906 char oldflags = old->flags & SE_MNTMASK;
907 char newflags = new->flags & SE_MNTMASK;
908
909 if (oldflags != newflags)
910 goto mismatch;
911 if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid)
912 goto mismatch;
913 if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid)
914 goto mismatch;
915 if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid)
916 goto mismatch;
917 if (oldflags & ROOTCONTEXT_MNT) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500918 struct inode_security_struct *oldroot = backing_inode_security(oldsb->s_root);
919 struct inode_security_struct *newroot = backing_inode_security(newsb->s_root);
Jeff Layton094f7b62013-04-01 08:14:24 -0400920 if (oldroot->sid != newroot->sid)
921 goto mismatch;
922 }
923 return 0;
924mismatch:
peter enderborgc103a912018-06-12 10:09:03 +0200925 pr_warn("SELinux: mount invalid. Same superblock, "
Jeff Layton094f7b62013-04-01 08:14:24 -0400926 "different security settings for (dev %s, "
927 "type %s)\n", newsb->s_id, newsb->s_type->name);
928 return -EBUSY;
929}
930
931static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400932 struct super_block *newsb,
933 unsigned long kern_flags,
934 unsigned long *set_kern_flags)
Eric Parisc9180a52007-11-30 13:00:35 -0500935{
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400936 int rc = 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500937 const struct superblock_security_struct *oldsbsec = oldsb->s_security;
938 struct superblock_security_struct *newsbsec = newsb->s_security;
939
940 int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT);
941 int set_context = (oldsbsec->flags & CONTEXT_MNT);
942 int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT);
943
Eric Paris0f5e6422008-04-21 16:24:11 -0400944 /*
945 * if the parent was able to be mounted it clearly had no special lsm
Al Viroe8c26252010-03-23 06:36:54 -0400946 * mount options. thus we can safely deal with this superblock later
Eric Paris0f5e6422008-04-21 16:24:11 -0400947 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500948 if (!selinux_state.initialized)
Jeff Layton094f7b62013-04-01 08:14:24 -0400949 return 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500950
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400951 /*
952 * Specifying internal flags without providing a place to
953 * place the results is not allowed.
954 */
955 if (kern_flags && !set_kern_flags)
956 return -EINVAL;
957
Eric Parisc9180a52007-11-30 13:00:35 -0500958 /* how can we clone if the old one wasn't set up?? */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500959 BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED));
Eric Parisc9180a52007-11-30 13:00:35 -0500960
Jeff Layton094f7b62013-04-01 08:14:24 -0400961 /* if fs is reusing a sb, make sure that the contexts match */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500962 if (newsbsec->flags & SE_SBINITIALIZED)
Jeff Layton094f7b62013-04-01 08:14:24 -0400963 return selinux_cmp_sb_context(oldsb, newsb);
Eric Paris5a552612008-04-09 14:08:35 -0400964
Eric Parisc9180a52007-11-30 13:00:35 -0500965 mutex_lock(&newsbsec->lock);
966
967 newsbsec->flags = oldsbsec->flags;
968
969 newsbsec->sid = oldsbsec->sid;
970 newsbsec->def_sid = oldsbsec->def_sid;
971 newsbsec->behavior = oldsbsec->behavior;
972
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400973 if (newsbsec->behavior == SECURITY_FS_USE_NATIVE &&
974 !(kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500975 rc = security_fs_use(&selinux_state, newsb);
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400976 if (rc)
977 goto out;
978 }
979
980 if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !set_context) {
981 newsbsec->behavior = SECURITY_FS_USE_NATIVE;
982 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
983 }
984
Eric Parisc9180a52007-11-30 13:00:35 -0500985 if (set_context) {
986 u32 sid = oldsbsec->mntpoint_sid;
987
988 if (!set_fscontext)
989 newsbsec->sid = sid;
990 if (!set_rootcontext) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500991 struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
Eric Parisc9180a52007-11-30 13:00:35 -0500992 newisec->sid = sid;
993 }
994 newsbsec->mntpoint_sid = sid;
995 }
996 if (set_rootcontext) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500997 const struct inode_security_struct *oldisec = backing_inode_security(oldsb->s_root);
998 struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
Eric Parisc9180a52007-11-30 13:00:35 -0500999
1000 newisec->sid = oldisec->sid;
1001 }
1002
1003 sb_finish_set_opts(newsb);
Scott Mayhew0b4d3452017-06-05 11:45:04 -04001004out:
Eric Parisc9180a52007-11-30 13:00:35 -05001005 mutex_unlock(&newsbsec->lock);
Scott Mayhew0b4d3452017-06-05 11:45:04 -04001006 return rc;
Eric Parisc9180a52007-11-30 13:00:35 -05001007}
1008
Al Viroba641862018-12-14 20:28:15 -05001009static int selinux_add_opt(int token, const char *s, void **mnt_opts)
1010{
1011 struct selinux_mnt_opts *opts = *mnt_opts;
1012
Al Viroda3d76a2018-12-17 10:14:16 -05001013 if (token == Opt_seclabel) /* eaten and completely ignored */
Al Viro169d68efb2018-12-14 22:44:50 -05001014 return 0;
1015
Al Viroba641862018-12-14 20:28:15 -05001016 if (!opts) {
1017 opts = kzalloc(sizeof(struct selinux_mnt_opts), GFP_KERNEL);
1018 if (!opts)
1019 return -ENOMEM;
1020 *mnt_opts = opts;
1021 }
1022 if (!s)
1023 return -ENOMEM;
1024 switch (token) {
1025 case Opt_context:
1026 if (opts->context || opts->defcontext)
1027 goto Einval;
1028 opts->context = s;
1029 break;
1030 case Opt_fscontext:
1031 if (opts->fscontext)
1032 goto Einval;
1033 opts->fscontext = s;
1034 break;
1035 case Opt_rootcontext:
1036 if (opts->rootcontext)
1037 goto Einval;
1038 opts->rootcontext = s;
1039 break;
1040 case Opt_defcontext:
1041 if (opts->context || opts->defcontext)
1042 goto Einval;
1043 opts->defcontext = s;
1044 break;
1045 }
1046 return 0;
1047Einval:
1048 pr_warn(SEL_MOUNT_FAIL_MSG);
Al Viroba641862018-12-14 20:28:15 -05001049 return -EINVAL;
1050}
1051
Adrian Bunk2e1479d2008-03-17 22:29:23 +02001052static int selinux_parse_opts_str(char *options,
Al Viro204cc0c2018-12-13 13:41:47 -05001053 void **mnt_opts)
Eric Parisc9180a52007-11-30 13:00:35 -05001054{
Al Viro169d68efb2018-12-14 22:44:50 -05001055 char *p = options, *next;
1056 int rc;
Eric Parisc9180a52007-11-30 13:00:35 -05001057
1058 /* Standard string-based options. */
Al Viro169d68efb2018-12-14 22:44:50 -05001059 for (p = options; *p; p = next) {
1060 int token, len;
1061 char *arg = NULL;
Eric Parisc9180a52007-11-30 13:00:35 -05001062
Al Viro169d68efb2018-12-14 22:44:50 -05001063 next = strchr(p, '|');
1064 if (next) {
1065 len = next++ - p;
1066 } else {
1067 len = strlen(p);
1068 next = p + len;
1069 }
1070
1071 if (!len)
Eric Parisc9180a52007-11-30 13:00:35 -05001072 continue;
1073
Al Viro169d68efb2018-12-14 22:44:50 -05001074 token = match_opt_prefix(p, len, &arg);
1075 if (arg)
1076 arg = kmemdup_nul(arg, p + len - arg, GFP_KERNEL);
Al Viroba641862018-12-14 20:28:15 -05001077 rc = selinux_add_opt(token, arg, mnt_opts);
Al Viro169d68efb2018-12-14 22:44:50 -05001078 if (rc) {
Al Viroba641862018-12-14 20:28:15 -05001079 kfree(arg);
Al Viro169d68efb2018-12-14 22:44:50 -05001080 selinux_free_mnt_opts(*mnt_opts);
1081 *mnt_opts = NULL;
Al Viroba641862018-12-14 20:28:15 -05001082 return rc;
Eric Parisc9180a52007-11-30 13:00:35 -05001083 }
1084 }
Eric Parise0007522008-03-05 10:31:54 -05001085 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086}
1087
Al Viroe3489f82018-12-13 00:24:36 -05001088static int show_sid(struct seq_file *m, u32 sid)
Eric Paris2069f452008-07-04 09:47:13 +10001089{
Al Viroe3489f82018-12-13 00:24:36 -05001090 char *context = NULL;
1091 u32 len;
1092 int rc;
Eric Paris2069f452008-07-04 09:47:13 +10001093
Al Viroe3489f82018-12-13 00:24:36 -05001094 rc = security_sid_to_context(&selinux_state, sid,
1095 &context, &len);
1096 if (!rc) {
1097 bool has_comma = context && strchr(context, ',');
David P. Quigley11689d42009-01-16 09:22:03 -05001098
Eric Paris2069f452008-07-04 09:47:13 +10001099 if (has_comma)
1100 seq_putc(m, '\"');
Al Viroe3489f82018-12-13 00:24:36 -05001101 seq_escape(m, context, "\"\n\\");
Eric Paris2069f452008-07-04 09:47:13 +10001102 if (has_comma)
1103 seq_putc(m, '\"');
1104 }
Al Viroe3489f82018-12-13 00:24:36 -05001105 kfree(context);
1106 return rc;
Eric Paris2069f452008-07-04 09:47:13 +10001107}
1108
1109static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
1110{
Al Viroe3489f82018-12-13 00:24:36 -05001111 struct superblock_security_struct *sbsec = sb->s_security;
Eric Paris2069f452008-07-04 09:47:13 +10001112 int rc;
1113
Al Viroe3489f82018-12-13 00:24:36 -05001114 if (!(sbsec->flags & SE_SBINITIALIZED))
1115 return 0;
1116
1117 if (!selinux_state.initialized)
1118 return 0;
1119
1120 if (sbsec->flags & FSCONTEXT_MNT) {
1121 seq_putc(m, ',');
1122 seq_puts(m, FSCONTEXT_STR);
1123 rc = show_sid(m, sbsec->sid);
1124 if (rc)
1125 return rc;
Eric Paris383795c2008-07-29 17:07:26 -04001126 }
Al Viroe3489f82018-12-13 00:24:36 -05001127 if (sbsec->flags & CONTEXT_MNT) {
1128 seq_putc(m, ',');
1129 seq_puts(m, CONTEXT_STR);
1130 rc = show_sid(m, sbsec->mntpoint_sid);
1131 if (rc)
1132 return rc;
1133 }
1134 if (sbsec->flags & DEFCONTEXT_MNT) {
1135 seq_putc(m, ',');
1136 seq_puts(m, DEFCONTEXT_STR);
1137 rc = show_sid(m, sbsec->def_sid);
1138 if (rc)
1139 return rc;
1140 }
1141 if (sbsec->flags & ROOTCONTEXT_MNT) {
1142 struct dentry *root = sbsec->sb->s_root;
1143 struct inode_security_struct *isec = backing_inode_security(root);
1144 seq_putc(m, ',');
1145 seq_puts(m, ROOTCONTEXT_STR);
1146 rc = show_sid(m, isec->sid);
1147 if (rc)
1148 return rc;
1149 }
1150 if (sbsec->flags & SBLABEL_MNT) {
1151 seq_putc(m, ',');
1152 seq_puts(m, LABELSUPP_STR);
1153 }
1154 return 0;
Eric Paris2069f452008-07-04 09:47:13 +10001155}
1156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157static inline u16 inode_mode_to_security_class(umode_t mode)
1158{
1159 switch (mode & S_IFMT) {
1160 case S_IFSOCK:
1161 return SECCLASS_SOCK_FILE;
1162 case S_IFLNK:
1163 return SECCLASS_LNK_FILE;
1164 case S_IFREG:
1165 return SECCLASS_FILE;
1166 case S_IFBLK:
1167 return SECCLASS_BLK_FILE;
1168 case S_IFDIR:
1169 return SECCLASS_DIR;
1170 case S_IFCHR:
1171 return SECCLASS_CHR_FILE;
1172 case S_IFIFO:
1173 return SECCLASS_FIFO_FILE;
1174
1175 }
1176
1177 return SECCLASS_FILE;
1178}
1179
James Morris13402582005-09-30 14:24:34 -04001180static inline int default_protocol_stream(int protocol)
1181{
1182 return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP);
1183}
1184
1185static inline int default_protocol_dgram(int protocol)
1186{
1187 return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
1188}
1189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190static inline u16 socket_type_to_security_class(int family, int type, int protocol)
1191{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001192 int extsockclass = selinux_policycap_extsockclass();
Stephen Smalleyda69a532017-01-09 10:07:30 -05001193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 switch (family) {
1195 case PF_UNIX:
1196 switch (type) {
1197 case SOCK_STREAM:
1198 case SOCK_SEQPACKET:
1199 return SECCLASS_UNIX_STREAM_SOCKET;
1200 case SOCK_DGRAM:
Luis Ressel2a764b52017-07-25 15:13:41 -04001201 case SOCK_RAW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 return SECCLASS_UNIX_DGRAM_SOCKET;
1203 }
1204 break;
1205 case PF_INET:
1206 case PF_INET6:
1207 switch (type) {
1208 case SOCK_STREAM:
Stephen Smalleyda69a532017-01-09 10:07:30 -05001209 case SOCK_SEQPACKET:
James Morris13402582005-09-30 14:24:34 -04001210 if (default_protocol_stream(protocol))
1211 return SECCLASS_TCP_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001212 else if (extsockclass && protocol == IPPROTO_SCTP)
1213 return SECCLASS_SCTP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001214 else
1215 return SECCLASS_RAWIP_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 case SOCK_DGRAM:
James Morris13402582005-09-30 14:24:34 -04001217 if (default_protocol_dgram(protocol))
1218 return SECCLASS_UDP_SOCKET;
Stephen Smalleyef379792017-01-09 10:07:31 -05001219 else if (extsockclass && (protocol == IPPROTO_ICMP ||
1220 protocol == IPPROTO_ICMPV6))
Stephen Smalleyda69a532017-01-09 10:07:30 -05001221 return SECCLASS_ICMP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001222 else
1223 return SECCLASS_RAWIP_SOCKET;
James Morris2ee92d42006-11-13 16:09:01 -08001224 case SOCK_DCCP:
1225 return SECCLASS_DCCP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001226 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 return SECCLASS_RAWIP_SOCKET;
1228 }
1229 break;
1230 case PF_NETLINK:
1231 switch (protocol) {
1232 case NETLINK_ROUTE:
1233 return SECCLASS_NETLINK_ROUTE_SOCKET;
Pavel Emelyanov7f1fb602011-12-06 07:56:43 +00001234 case NETLINK_SOCK_DIAG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 return SECCLASS_NETLINK_TCPDIAG_SOCKET;
1236 case NETLINK_NFLOG:
1237 return SECCLASS_NETLINK_NFLOG_SOCKET;
1238 case NETLINK_XFRM:
1239 return SECCLASS_NETLINK_XFRM_SOCKET;
1240 case NETLINK_SELINUX:
1241 return SECCLASS_NETLINK_SELINUX_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001242 case NETLINK_ISCSI:
1243 return SECCLASS_NETLINK_ISCSI_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 case NETLINK_AUDIT:
1245 return SECCLASS_NETLINK_AUDIT_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001246 case NETLINK_FIB_LOOKUP:
1247 return SECCLASS_NETLINK_FIB_LOOKUP_SOCKET;
1248 case NETLINK_CONNECTOR:
1249 return SECCLASS_NETLINK_CONNECTOR_SOCKET;
1250 case NETLINK_NETFILTER:
1251 return SECCLASS_NETLINK_NETFILTER_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 case NETLINK_DNRTMSG:
1253 return SECCLASS_NETLINK_DNRT_SOCKET;
James Morris0c9b7942005-04-16 15:24:13 -07001254 case NETLINK_KOBJECT_UEVENT:
1255 return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001256 case NETLINK_GENERIC:
1257 return SECCLASS_NETLINK_GENERIC_SOCKET;
1258 case NETLINK_SCSITRANSPORT:
1259 return SECCLASS_NETLINK_SCSITRANSPORT_SOCKET;
1260 case NETLINK_RDMA:
1261 return SECCLASS_NETLINK_RDMA_SOCKET;
1262 case NETLINK_CRYPTO:
1263 return SECCLASS_NETLINK_CRYPTO_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 default:
1265 return SECCLASS_NETLINK_SOCKET;
1266 }
1267 case PF_PACKET:
1268 return SECCLASS_PACKET_SOCKET;
1269 case PF_KEY:
1270 return SECCLASS_KEY_SOCKET;
Christopher J. PeBenito3e3ff152006-06-09 00:25:03 -07001271 case PF_APPLETALK:
1272 return SECCLASS_APPLETALK_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 }
1274
Stephen Smalleyda69a532017-01-09 10:07:30 -05001275 if (extsockclass) {
1276 switch (family) {
1277 case PF_AX25:
1278 return SECCLASS_AX25_SOCKET;
1279 case PF_IPX:
1280 return SECCLASS_IPX_SOCKET;
1281 case PF_NETROM:
1282 return SECCLASS_NETROM_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001283 case PF_ATMPVC:
1284 return SECCLASS_ATMPVC_SOCKET;
1285 case PF_X25:
1286 return SECCLASS_X25_SOCKET;
1287 case PF_ROSE:
1288 return SECCLASS_ROSE_SOCKET;
1289 case PF_DECnet:
1290 return SECCLASS_DECNET_SOCKET;
1291 case PF_ATMSVC:
1292 return SECCLASS_ATMSVC_SOCKET;
1293 case PF_RDS:
1294 return SECCLASS_RDS_SOCKET;
1295 case PF_IRDA:
1296 return SECCLASS_IRDA_SOCKET;
1297 case PF_PPPOX:
1298 return SECCLASS_PPPOX_SOCKET;
1299 case PF_LLC:
1300 return SECCLASS_LLC_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001301 case PF_CAN:
1302 return SECCLASS_CAN_SOCKET;
1303 case PF_TIPC:
1304 return SECCLASS_TIPC_SOCKET;
1305 case PF_BLUETOOTH:
1306 return SECCLASS_BLUETOOTH_SOCKET;
1307 case PF_IUCV:
1308 return SECCLASS_IUCV_SOCKET;
1309 case PF_RXRPC:
1310 return SECCLASS_RXRPC_SOCKET;
1311 case PF_ISDN:
1312 return SECCLASS_ISDN_SOCKET;
1313 case PF_PHONET:
1314 return SECCLASS_PHONET_SOCKET;
1315 case PF_IEEE802154:
1316 return SECCLASS_IEEE802154_SOCKET;
1317 case PF_CAIF:
1318 return SECCLASS_CAIF_SOCKET;
1319 case PF_ALG:
1320 return SECCLASS_ALG_SOCKET;
1321 case PF_NFC:
1322 return SECCLASS_NFC_SOCKET;
1323 case PF_VSOCK:
1324 return SECCLASS_VSOCK_SOCKET;
1325 case PF_KCM:
1326 return SECCLASS_KCM_SOCKET;
1327 case PF_QIPCRTR:
1328 return SECCLASS_QIPCRTR_SOCKET;
Linus Torvalds3051bf32017-02-22 10:15:09 -08001329 case PF_SMC:
1330 return SECCLASS_SMC_SOCKET;
Björn Töpel68e8b842018-05-02 13:01:22 +02001331 case PF_XDP:
1332 return SECCLASS_XDP_SOCKET;
1333#if PF_MAX > 45
Stephen Smalleyda69a532017-01-09 10:07:30 -05001334#error New address family defined, please update this function.
1335#endif
1336 }
1337 }
1338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 return SECCLASS_SOCKET;
1340}
1341
Stephen Smalley134509d2015-06-04 16:22:17 -04001342static int selinux_genfs_get_sid(struct dentry *dentry,
1343 u16 tclass,
1344 u16 flags,
1345 u32 *sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001347 int rc;
Al Virofc640052016-04-10 01:33:30 -04001348 struct super_block *sb = dentry->d_sb;
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001349 char *buffer, *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Eric Paris828dfe12008-04-17 13:17:49 -04001351 buffer = (char *)__get_free_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 if (!buffer)
1353 return -ENOMEM;
1354
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001355 path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
1356 if (IS_ERR(path))
1357 rc = PTR_ERR(path);
1358 else {
Stephen Smalley134509d2015-06-04 16:22:17 -04001359 if (flags & SE_SBPROC) {
1360 /* each process gets a /proc/PID/ entry. Strip off the
1361 * PID part to get a valid selinux labeling.
1362 * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
1363 while (path[1] >= '0' && path[1] <= '9') {
1364 path[1] = '/';
1365 path++;
1366 }
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001367 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001368 rc = security_genfs_sid(&selinux_state, sb->s_type->name,
1369 path, tclass, sid);
Stephen Smalley7bb185e2018-09-04 16:51:36 -04001370 if (rc == -ENOENT) {
1371 /* No match in policy, mark as unlabeled. */
1372 *sid = SECINITSID_UNLABELED;
1373 rc = 0;
1374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 free_page((unsigned long)buffer);
1377 return rc;
1378}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380/* The inode's security attributes must be initialized before first use. */
1381static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
1382{
1383 struct superblock_security_struct *sbsec = NULL;
1384 struct inode_security_struct *isec = inode->i_security;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001385 u32 task_sid, sid = 0;
1386 u16 sclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 struct dentry *dentry;
1388#define INITCONTEXTLEN 255
1389 char *context = NULL;
1390 unsigned len = 0;
1391 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05001393 if (isec->initialized == LABEL_INITIALIZED)
Andreas Gruenbacher13457d02016-11-10 22:18:29 +01001394 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001396 spin_lock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05001397 if (isec->initialized == LABEL_INITIALIZED)
Eric Paris23970742006-09-25 23:32:01 -07001398 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Andreas Gruenbacher13457d02016-11-10 22:18:29 +01001400 if (isec->sclass == SECCLASS_FILE)
1401 isec->sclass = inode_mode_to_security_class(inode->i_mode);
1402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 sbsec = inode->i_sb->s_security;
David P. Quigley0d90a7e2009-01-16 09:22:02 -05001404 if (!(sbsec->flags & SE_SBINITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 /* Defer initialization until selinux_complete_init,
1406 after the initial policy is loaded and the security
1407 server is ready to handle calls. */
1408 spin_lock(&sbsec->isec_lock);
1409 if (list_empty(&isec->list))
1410 list_add(&isec->list, &sbsec->isec_head);
1411 spin_unlock(&sbsec->isec_lock);
Eric Paris23970742006-09-25 23:32:01 -07001412 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 }
1414
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001415 sclass = isec->sclass;
1416 task_sid = isec->task_sid;
1417 sid = isec->sid;
1418 isec->initialized = LABEL_PENDING;
1419 spin_unlock(&isec->lock);
1420
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 switch (sbsec->behavior) {
David Quigleyeb9ae682013-05-22 12:50:37 -04001422 case SECURITY_FS_USE_NATIVE:
1423 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 case SECURITY_FS_USE_XATTR:
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001425 if (!(inode->i_opflags & IOP_XATTR)) {
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001426 sid = sbsec->def_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 break;
1428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 /* Need a dentry, since the xattr API requires one.
1430 Life would be simpler if we could just pass the inode. */
1431 if (opt_dentry) {
1432 /* Called from d_instantiate or d_splice_alias. */
1433 dentry = dget(opt_dentry);
1434 } else {
Al Virob1271252018-04-25 10:28:38 -04001435 /*
1436 * Called from selinux_complete_init, try to find a dentry.
1437 * Some filesystems really want a connected one, so try
1438 * that first. We could split SECURITY_FS_USE_XATTR in
1439 * two, depending upon that...
1440 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 dentry = d_find_alias(inode);
Al Virob1271252018-04-25 10:28:38 -04001442 if (!dentry)
1443 dentry = d_find_any_alias(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 }
1445 if (!dentry) {
Eric Parisdf7f54c2009-03-09 14:35:58 -04001446 /*
1447 * this is can be hit on boot when a file is accessed
1448 * before the policy is loaded. When we load policy we
1449 * may find inodes that have no dentry on the
1450 * sbsec->isec_head list. No reason to complain as these
1451 * will get fixed up the next time we go through
1452 * inode_doinit with a dentry, before these inodes could
1453 * be used again by userspace.
1454 */
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001455 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 }
1457
1458 len = INITCONTEXTLEN;
Eric Paris4cb912f2009-02-12 14:50:05 -05001459 context = kmalloc(len+1, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 if (!context) {
1461 rc = -ENOMEM;
1462 dput(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001463 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 }
Eric Paris4cb912f2009-02-12 14:50:05 -05001465 context[len] = '\0';
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001466 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 if (rc == -ERANGE) {
James Morris314dabb2009-08-10 22:00:13 +10001468 kfree(context);
1469
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 /* Need a larger buffer. Query for the right size. */
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001471 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 if (rc < 0) {
1473 dput(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001474 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 len = rc;
Eric Paris4cb912f2009-02-12 14:50:05 -05001477 context = kmalloc(len+1, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 if (!context) {
1479 rc = -ENOMEM;
1480 dput(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001481 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 }
Eric Paris4cb912f2009-02-12 14:50:05 -05001483 context[len] = '\0';
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001484 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 }
1486 dput(dentry);
1487 if (rc < 0) {
1488 if (rc != -ENODATA) {
peter enderborgc103a912018-06-12 10:09:03 +02001489 pr_warn("SELinux: %s: getxattr returned "
Harvey Harrisondd6f9532008-03-06 10:03:59 +11001490 "%d for dev=%s ino=%ld\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 -rc, inode->i_sb->s_id, inode->i_ino);
1492 kfree(context);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001493 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 }
1495 /* Map ENODATA to the default file SID */
1496 sid = sbsec->def_sid;
1497 rc = 0;
1498 } else {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001499 rc = security_context_to_sid_default(&selinux_state,
1500 context, rc, &sid,
Stephen Smalley869ab512008-04-04 08:46:05 -04001501 sbsec->def_sid,
1502 GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 if (rc) {
Eric Paris4ba0a8a2009-02-12 15:01:10 -05001504 char *dev = inode->i_sb->s_id;
1505 unsigned long ino = inode->i_ino;
1506
1507 if (rc == -EINVAL) {
1508 if (printk_ratelimit())
peter enderborgc103a912018-06-12 10:09:03 +02001509 pr_notice("SELinux: inode=%lu on dev=%s was found to have an invalid "
Eric Paris4ba0a8a2009-02-12 15:01:10 -05001510 "context=%s. This indicates you may need to relabel the inode or the "
1511 "filesystem in question.\n", ino, dev, context);
1512 } else {
peter enderborgc103a912018-06-12 10:09:03 +02001513 pr_warn("SELinux: %s: context_to_sid(%s) "
Eric Paris4ba0a8a2009-02-12 15:01:10 -05001514 "returned %d for dev=%s ino=%ld\n",
1515 __func__, context, -rc, dev, ino);
1516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 kfree(context);
1518 /* Leave with the unlabeled SID */
1519 rc = 0;
1520 break;
1521 }
1522 }
1523 kfree(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 break;
1525 case SECURITY_FS_USE_TASK:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001526 sid = task_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 break;
1528 case SECURITY_FS_USE_TRANS:
1529 /* Default to the fs SID. */
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001530 sid = sbsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
1532 /* Try to obtain a transition SID. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001533 rc = security_transition_sid(&selinux_state, task_sid, sid,
1534 sclass, NULL, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 if (rc)
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001536 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 break;
Eric Parisc312feb2006-07-10 04:43:53 -07001538 case SECURITY_FS_USE_MNTPOINT:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001539 sid = sbsec->mntpoint_sid;
Eric Parisc312feb2006-07-10 04:43:53 -07001540 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 default:
Eric Parisc312feb2006-07-10 04:43:53 -07001542 /* Default to the fs superblock SID. */
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001543 sid = sbsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
Stephen Smalley134509d2015-06-04 16:22:17 -04001545 if ((sbsec->flags & SE_SBGENFS) && !S_ISLNK(inode->i_mode)) {
Paul Mooref64410e2014-03-19 16:46:18 -04001546 /* We must have a dentry to determine the label on
1547 * procfs inodes */
Al Virob1271252018-04-25 10:28:38 -04001548 if (opt_dentry) {
Paul Mooref64410e2014-03-19 16:46:18 -04001549 /* Called from d_instantiate or
1550 * d_splice_alias. */
1551 dentry = dget(opt_dentry);
Al Virob1271252018-04-25 10:28:38 -04001552 } else {
Paul Mooref64410e2014-03-19 16:46:18 -04001553 /* Called from selinux_complete_init, try to
Al Virob1271252018-04-25 10:28:38 -04001554 * find a dentry. Some filesystems really want
1555 * a connected one, so try that first.
1556 */
Paul Mooref64410e2014-03-19 16:46:18 -04001557 dentry = d_find_alias(inode);
Al Virob1271252018-04-25 10:28:38 -04001558 if (!dentry)
1559 dentry = d_find_any_alias(inode);
1560 }
Paul Mooref64410e2014-03-19 16:46:18 -04001561 /*
1562 * This can be hit on boot when a file is accessed
1563 * before the policy is loaded. When we load policy we
1564 * may find inodes that have no dentry on the
1565 * sbsec->isec_head list. No reason to complain as
1566 * these will get fixed up the next time we go through
1567 * inode_doinit() with a dentry, before these inodes
1568 * could be used again by userspace.
1569 */
1570 if (!dentry)
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001571 goto out;
1572 rc = selinux_genfs_get_sid(dentry, sclass,
Stephen Smalley134509d2015-06-04 16:22:17 -04001573 sbsec->flags, &sid);
Paul Mooref64410e2014-03-19 16:46:18 -04001574 dput(dentry);
1575 if (rc)
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001576 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 }
1578 break;
1579 }
1580
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001581out:
1582 spin_lock(&isec->lock);
1583 if (isec->initialized == LABEL_PENDING) {
1584 if (!sid || rc) {
1585 isec->initialized = LABEL_INVALID;
1586 goto out_unlock;
1587 }
1588
1589 isec->initialized = LABEL_INITIALIZED;
1590 isec->sid = sid;
1591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
Eric Paris23970742006-09-25 23:32:01 -07001593out_unlock:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001594 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 return rc;
1596}
1597
1598/* Convert a Linux signal to an access vector. */
1599static inline u32 signal_to_av(int sig)
1600{
1601 u32 perm = 0;
1602
1603 switch (sig) {
1604 case SIGCHLD:
1605 /* Commonly granted from child to parent. */
1606 perm = PROCESS__SIGCHLD;
1607 break;
1608 case SIGKILL:
1609 /* Cannot be caught or ignored */
1610 perm = PROCESS__SIGKILL;
1611 break;
1612 case SIGSTOP:
1613 /* Cannot be caught or ignored */
1614 perm = PROCESS__SIGSTOP;
1615 break;
1616 default:
1617 /* All other signals. */
1618 perm = PROCESS__SIGNAL;
1619 break;
1620 }
1621
1622 return perm;
1623}
1624
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001625#if CAP_LAST_CAP > 63
1626#error Fix SELinux to handle capabilities > 63.
1627#endif
1628
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629/* Check whether a task is allowed to use a capability. */
Eric Paris6a9de492012-01-03 12:25:14 -05001630static int cred_has_capability(const struct cred *cred,
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04001631 int cap, int audit, bool initns)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632{
Thomas Liu2bf49692009-07-14 12:14:09 -04001633 struct common_audit_data ad;
Eric Paris06112162008-11-11 22:02:50 +11001634 struct av_decision avd;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001635 u16 sclass;
David Howells3699c532009-01-06 22:27:01 +00001636 u32 sid = cred_sid(cred);
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001637 u32 av = CAP_TO_MASK(cap);
Eric Paris06112162008-11-11 22:02:50 +11001638 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
Eric Paris50c205f2012-04-04 15:01:43 -04001640 ad.type = LSM_AUDIT_DATA_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 ad.u.cap = cap;
1642
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001643 switch (CAP_TO_INDEX(cap)) {
1644 case 0:
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04001645 sclass = initns ? SECCLASS_CAPABILITY : SECCLASS_CAP_USERNS;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001646 break;
1647 case 1:
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04001648 sclass = initns ? SECCLASS_CAPABILITY2 : SECCLASS_CAP2_USERNS;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001649 break;
1650 default:
peter enderborgc103a912018-06-12 10:09:03 +02001651 pr_err("SELinux: out of range capability %d\n", cap);
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001652 BUG();
Eric Parisa35c6c832011-04-20 10:21:28 -04001653 return -EINVAL;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001654 }
Eric Paris06112162008-11-11 22:02:50 +11001655
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001656 rc = avc_has_perm_noaudit(&selinux_state,
1657 sid, sid, sclass, av, 0, &avd);
Eric Paris9ade0cf2011-04-25 16:26:29 -04001658 if (audit == SECURITY_CAP_AUDIT) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001659 int rc2 = avc_audit(&selinux_state,
1660 sid, sid, sclass, av, &avd, rc, &ad, 0);
Eric Paris9ade0cf2011-04-25 16:26:29 -04001661 if (rc2)
1662 return rc2;
1663 }
Eric Paris06112162008-11-11 22:02:50 +11001664 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665}
1666
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667/* Check whether a task has a particular permission to an inode.
1668 The 'adp' parameter is optional and allows other audit
1669 data to be passed (e.g. the dentry). */
David Howells88e67f32008-11-14 10:39:21 +11001670static int inode_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 struct inode *inode,
1672 u32 perms,
Linus Torvalds19e49832013-10-04 12:54:11 -07001673 struct common_audit_data *adp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 struct inode_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +11001676 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
David Howellse0e81732009-09-02 09:13:40 +01001678 validate_creds(cred);
1679
Eric Paris828dfe12008-04-17 13:17:49 -04001680 if (unlikely(IS_PRIVATE(inode)))
Stephen Smalleybbaca6c2007-02-14 00:34:16 -08001681 return 0;
1682
David Howells88e67f32008-11-14 10:39:21 +11001683 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 isec = inode->i_security;
1685
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001686 return avc_has_perm(&selinux_state,
1687 sid, isec->sid, isec->sclass, perms, adp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688}
1689
1690/* Same as inode_has_perm, but pass explicit audit data containing
1691 the dentry to help the auditing code to more easily generate the
1692 pathname if needed. */
David Howells88e67f32008-11-14 10:39:21 +11001693static inline int dentry_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 struct dentry *dentry,
1695 u32 av)
1696{
David Howellsc6f493d2015-03-17 22:26:22 +00001697 struct inode *inode = d_backing_inode(dentry);
Thomas Liu2bf49692009-07-14 12:14:09 -04001698 struct common_audit_data ad;
David Howells88e67f32008-11-14 10:39:21 +11001699
Eric Paris50c205f2012-04-04 15:01:43 -04001700 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Paris2875fa02011-04-28 16:04:24 -04001701 ad.u.dentry = dentry;
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05001702 __inode_security_revalidate(inode, dentry, true);
Linus Torvalds19e49832013-10-04 12:54:11 -07001703 return inode_has_perm(cred, inode, av, &ad);
Eric Paris2875fa02011-04-28 16:04:24 -04001704}
1705
1706/* Same as inode_has_perm, but pass explicit audit data containing
1707 the path to help the auditing code to more easily generate the
1708 pathname if needed. */
1709static inline int path_has_perm(const struct cred *cred,
Al Viro3f7036a2015-03-08 19:28:30 -04001710 const struct path *path,
Eric Paris2875fa02011-04-28 16:04:24 -04001711 u32 av)
1712{
David Howellsc6f493d2015-03-17 22:26:22 +00001713 struct inode *inode = d_backing_inode(path->dentry);
Eric Paris2875fa02011-04-28 16:04:24 -04001714 struct common_audit_data ad;
1715
Eric Paris50c205f2012-04-04 15:01:43 -04001716 ad.type = LSM_AUDIT_DATA_PATH;
Eric Paris2875fa02011-04-28 16:04:24 -04001717 ad.u.path = *path;
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05001718 __inode_security_revalidate(inode, path->dentry, true);
Linus Torvalds19e49832013-10-04 12:54:11 -07001719 return inode_has_perm(cred, inode, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720}
1721
David Howells13f8e982013-06-13 23:37:55 +01001722/* Same as path_has_perm, but uses the inode from the file struct. */
1723static inline int file_path_has_perm(const struct cred *cred,
1724 struct file *file,
1725 u32 av)
1726{
1727 struct common_audit_data ad;
1728
Vivek Goyal43af5de2016-09-09 11:37:49 -04001729 ad.type = LSM_AUDIT_DATA_FILE;
1730 ad.u.file = file;
Linus Torvalds19e49832013-10-04 12:54:11 -07001731 return inode_has_perm(cred, file_inode(file), av, &ad);
David Howells13f8e982013-06-13 23:37:55 +01001732}
1733
Chenbo Fengf66e4482017-10-18 13:00:26 -07001734#ifdef CONFIG_BPF_SYSCALL
1735static int bpf_fd_pass(struct file *file, u32 sid);
1736#endif
1737
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738/* Check whether a task can use an open file descriptor to
1739 access an inode in a given way. Check access to the
1740 descriptor itself, and then use dentry_has_perm to
1741 check a particular permission to the file.
1742 Access to the descriptor is implicitly granted if it
1743 has the same SID as the process. If av is zero, then
1744 access to the file is not checked, e.g. for cases
1745 where only the descriptor is affected like seek. */
David Howells88e67f32008-11-14 10:39:21 +11001746static int file_has_perm(const struct cred *cred,
1747 struct file *file,
1748 u32 av)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 struct file_security_struct *fsec = file->f_security;
Al Viro496ad9a2013-01-23 17:07:38 -05001751 struct inode *inode = file_inode(file);
Thomas Liu2bf49692009-07-14 12:14:09 -04001752 struct common_audit_data ad;
David Howells88e67f32008-11-14 10:39:21 +11001753 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 int rc;
1755
Vivek Goyal43af5de2016-09-09 11:37:49 -04001756 ad.type = LSM_AUDIT_DATA_FILE;
1757 ad.u.file = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
David Howells275bb412008-11-14 10:39:19 +11001759 if (sid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001760 rc = avc_has_perm(&selinux_state,
1761 sid, fsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 SECCLASS_FD,
1763 FD__USE,
1764 &ad);
1765 if (rc)
David Howells88e67f32008-11-14 10:39:21 +11001766 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 }
1768
Chenbo Fengf66e4482017-10-18 13:00:26 -07001769#ifdef CONFIG_BPF_SYSCALL
1770 rc = bpf_fd_pass(file, cred_sid(cred));
1771 if (rc)
1772 return rc;
1773#endif
1774
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 /* av is zero if only checking access to the descriptor. */
David Howells88e67f32008-11-14 10:39:21 +11001776 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 if (av)
Linus Torvalds19e49832013-10-04 12:54:11 -07001778 rc = inode_has_perm(cred, inode, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
David Howells88e67f32008-11-14 10:39:21 +11001780out:
1781 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782}
1783
David Howellsc3c188b2015-07-10 17:19:58 -04001784/*
1785 * Determine the label for an inode that might be unioned.
1786 */
Vivek Goyalc957f6d2016-07-13 10:44:51 -04001787static int
1788selinux_determine_inode_label(const struct task_security_struct *tsec,
1789 struct inode *dir,
1790 const struct qstr *name, u16 tclass,
1791 u32 *_new_isid)
David Howellsc3c188b2015-07-10 17:19:58 -04001792{
1793 const struct superblock_security_struct *sbsec = dir->i_sb->s_security;
David Howellsc3c188b2015-07-10 17:19:58 -04001794
1795 if ((sbsec->flags & SE_SBINITIALIZED) &&
1796 (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)) {
1797 *_new_isid = sbsec->mntpoint_sid;
1798 } else if ((sbsec->flags & SBLABEL_MNT) &&
1799 tsec->create_sid) {
1800 *_new_isid = tsec->create_sid;
1801 } else {
Paul Moore20cdef82016-04-04 14:14:42 -04001802 const struct inode_security_struct *dsec = inode_security(dir);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001803 return security_transition_sid(&selinux_state, tsec->sid,
1804 dsec->sid, tclass,
David Howellsc3c188b2015-07-10 17:19:58 -04001805 name, _new_isid);
1806 }
1807
1808 return 0;
1809}
1810
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811/* Check whether a task can create a file. */
1812static int may_create(struct inode *dir,
1813 struct dentry *dentry,
1814 u16 tclass)
1815{
Paul Moore5fb49872010-04-22 14:46:19 -04001816 const struct task_security_struct *tsec = current_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 struct inode_security_struct *dsec;
1818 struct superblock_security_struct *sbsec;
David Howells275bb412008-11-14 10:39:19 +11001819 u32 sid, newsid;
Thomas Liu2bf49692009-07-14 12:14:09 -04001820 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 int rc;
1822
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001823 dsec = inode_security(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 sbsec = dir->i_sb->s_security;
1825
David Howells275bb412008-11-14 10:39:19 +11001826 sid = tsec->sid;
David Howells275bb412008-11-14 10:39:19 +11001827
Eric Paris50c205f2012-04-04 15:01:43 -04001828 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04001829 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001831 rc = avc_has_perm(&selinux_state,
1832 sid, dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 DIR__ADD_NAME | DIR__SEARCH,
1834 &ad);
1835 if (rc)
1836 return rc;
1837
Vivek Goyalc957f6d2016-07-13 10:44:51 -04001838 rc = selinux_determine_inode_label(current_security(), dir,
1839 &dentry->d_name, tclass, &newsid);
David Howellsc3c188b2015-07-10 17:19:58 -04001840 if (rc)
1841 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001843 rc = avc_has_perm(&selinux_state,
1844 sid, newsid, tclass, FILE__CREATE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 if (rc)
1846 return rc;
1847
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001848 return avc_has_perm(&selinux_state,
1849 newsid, sbsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 SECCLASS_FILESYSTEM,
1851 FILESYSTEM__ASSOCIATE, &ad);
1852}
1853
Eric Paris828dfe12008-04-17 13:17:49 -04001854#define MAY_LINK 0
1855#define MAY_UNLINK 1
1856#define MAY_RMDIR 2
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
1858/* Check whether a task can link, unlink, or rmdir a file/directory. */
1859static int may_link(struct inode *dir,
1860 struct dentry *dentry,
1861 int kind)
1862
1863{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 struct inode_security_struct *dsec, *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001865 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11001866 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 u32 av;
1868 int rc;
1869
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001870 dsec = inode_security(dir);
1871 isec = backing_inode_security(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
Eric Paris50c205f2012-04-04 15:01:43 -04001873 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04001874 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
1876 av = DIR__SEARCH;
1877 av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001878 rc = avc_has_perm(&selinux_state,
1879 sid, dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 if (rc)
1881 return rc;
1882
1883 switch (kind) {
1884 case MAY_LINK:
1885 av = FILE__LINK;
1886 break;
1887 case MAY_UNLINK:
1888 av = FILE__UNLINK;
1889 break;
1890 case MAY_RMDIR:
1891 av = DIR__RMDIR;
1892 break;
1893 default:
peter enderborgc103a912018-06-12 10:09:03 +02001894 pr_warn("SELinux: %s: unrecognized kind %d\n",
Eric Paris744ba352008-04-17 11:52:44 -04001895 __func__, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 return 0;
1897 }
1898
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001899 rc = avc_has_perm(&selinux_state,
1900 sid, isec->sid, isec->sclass, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 return rc;
1902}
1903
1904static inline int may_rename(struct inode *old_dir,
1905 struct dentry *old_dentry,
1906 struct inode *new_dir,
1907 struct dentry *new_dentry)
1908{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001910 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11001911 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 u32 av;
1913 int old_is_dir, new_is_dir;
1914 int rc;
1915
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001916 old_dsec = inode_security(old_dir);
1917 old_isec = backing_inode_security(old_dentry);
David Howellse36cb0b2015-01-29 12:02:35 +00001918 old_is_dir = d_is_dir(old_dentry);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001919 new_dsec = inode_security(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
Eric Paris50c205f2012-04-04 15:01:43 -04001921 ad.type = LSM_AUDIT_DATA_DENTRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Eric Parisa2694342011-04-25 13:10:27 -04001923 ad.u.dentry = old_dentry;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001924 rc = avc_has_perm(&selinux_state,
1925 sid, old_dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 DIR__REMOVE_NAME | DIR__SEARCH, &ad);
1927 if (rc)
1928 return rc;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001929 rc = avc_has_perm(&selinux_state,
1930 sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 old_isec->sclass, FILE__RENAME, &ad);
1932 if (rc)
1933 return rc;
1934 if (old_is_dir && new_dir != old_dir) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001935 rc = avc_has_perm(&selinux_state,
1936 sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 old_isec->sclass, DIR__REPARENT, &ad);
1938 if (rc)
1939 return rc;
1940 }
1941
Eric Parisa2694342011-04-25 13:10:27 -04001942 ad.u.dentry = new_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 av = DIR__ADD_NAME | DIR__SEARCH;
David Howells2c616d42015-01-29 12:02:33 +00001944 if (d_is_positive(new_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 av |= DIR__REMOVE_NAME;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001946 rc = avc_has_perm(&selinux_state,
1947 sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 if (rc)
1949 return rc;
David Howells2c616d42015-01-29 12:02:33 +00001950 if (d_is_positive(new_dentry)) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001951 new_isec = backing_inode_security(new_dentry);
David Howellse36cb0b2015-01-29 12:02:35 +00001952 new_is_dir = d_is_dir(new_dentry);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001953 rc = avc_has_perm(&selinux_state,
1954 sid, new_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 new_isec->sclass,
1956 (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
1957 if (rc)
1958 return rc;
1959 }
1960
1961 return 0;
1962}
1963
1964/* Check whether a task can perform a filesystem operation. */
David Howells88e67f32008-11-14 10:39:21 +11001965static int superblock_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 struct super_block *sb,
1967 u32 perms,
Thomas Liu2bf49692009-07-14 12:14:09 -04001968 struct common_audit_data *ad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 struct superblock_security_struct *sbsec;
David Howells88e67f32008-11-14 10:39:21 +11001971 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 sbsec = sb->s_security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001974 return avc_has_perm(&selinux_state,
1975 sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976}
1977
1978/* Convert a Linux mode and permission mask to an access vector. */
1979static inline u32 file_mask_to_av(int mode, int mask)
1980{
1981 u32 av = 0;
1982
Al Virodba19c62011-07-25 20:49:29 -04001983 if (!S_ISDIR(mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 if (mask & MAY_EXEC)
1985 av |= FILE__EXECUTE;
1986 if (mask & MAY_READ)
1987 av |= FILE__READ;
1988
1989 if (mask & MAY_APPEND)
1990 av |= FILE__APPEND;
1991 else if (mask & MAY_WRITE)
1992 av |= FILE__WRITE;
1993
1994 } else {
1995 if (mask & MAY_EXEC)
1996 av |= DIR__SEARCH;
1997 if (mask & MAY_WRITE)
1998 av |= DIR__WRITE;
1999 if (mask & MAY_READ)
2000 av |= DIR__READ;
2001 }
2002
2003 return av;
2004}
2005
2006/* Convert a Linux file to an access vector. */
2007static inline u32 file_to_av(struct file *file)
2008{
2009 u32 av = 0;
2010
2011 if (file->f_mode & FMODE_READ)
2012 av |= FILE__READ;
2013 if (file->f_mode & FMODE_WRITE) {
2014 if (file->f_flags & O_APPEND)
2015 av |= FILE__APPEND;
2016 else
2017 av |= FILE__WRITE;
2018 }
Stephen Smalley0794c662008-03-17 08:55:18 -04002019 if (!av) {
2020 /*
2021 * Special file opened with flags 3 for ioctl-only use.
2022 */
2023 av = FILE__IOCTL;
2024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
2026 return av;
2027}
2028
Eric Paris8b6a5a32008-10-29 17:06:46 -04002029/*
2030 * Convert a file to an access vector and include the correct open
2031 * open permission.
2032 */
2033static inline u32 open_file_to_av(struct file *file)
2034{
2035 u32 av = file_to_av(file);
Stephen Smalleyccb54472017-05-12 12:41:24 -04002036 struct inode *inode = file_inode(file);
Eric Paris8b6a5a32008-10-29 17:06:46 -04002037
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002038 if (selinux_policycap_openperm() &&
2039 inode->i_sb->s_magic != SOCKFS_MAGIC)
Eric Paris49b7b8d2010-07-23 11:44:09 -04002040 av |= FILE__OPEN;
2041
Eric Paris8b6a5a32008-10-29 17:06:46 -04002042 return av;
2043}
2044
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045/* Hook functions begin here. */
2046
Stephen Smalley79af7302015-01-21 10:54:10 -05002047static int selinux_binder_set_context_mgr(struct task_struct *mgr)
2048{
2049 u32 mysid = current_sid();
2050 u32 mgrsid = task_sid(mgr);
2051
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002052 return avc_has_perm(&selinux_state,
2053 mysid, mgrsid, SECCLASS_BINDER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002054 BINDER__SET_CONTEXT_MGR, NULL);
2055}
2056
2057static int selinux_binder_transaction(struct task_struct *from,
2058 struct task_struct *to)
2059{
2060 u32 mysid = current_sid();
2061 u32 fromsid = task_sid(from);
2062 u32 tosid = task_sid(to);
2063 int rc;
2064
2065 if (mysid != fromsid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002066 rc = avc_has_perm(&selinux_state,
2067 mysid, fromsid, SECCLASS_BINDER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002068 BINDER__IMPERSONATE, NULL);
2069 if (rc)
2070 return rc;
2071 }
2072
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002073 return avc_has_perm(&selinux_state,
2074 fromsid, tosid, SECCLASS_BINDER, BINDER__CALL,
Stephen Smalley79af7302015-01-21 10:54:10 -05002075 NULL);
2076}
2077
2078static int selinux_binder_transfer_binder(struct task_struct *from,
2079 struct task_struct *to)
2080{
2081 u32 fromsid = task_sid(from);
2082 u32 tosid = task_sid(to);
2083
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002084 return avc_has_perm(&selinux_state,
2085 fromsid, tosid, SECCLASS_BINDER, BINDER__TRANSFER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002086 NULL);
2087}
2088
2089static int selinux_binder_transfer_file(struct task_struct *from,
2090 struct task_struct *to,
2091 struct file *file)
2092{
2093 u32 sid = task_sid(to);
2094 struct file_security_struct *fsec = file->f_security;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002095 struct dentry *dentry = file->f_path.dentry;
Paul Moore20cdef82016-04-04 14:14:42 -04002096 struct inode_security_struct *isec;
Stephen Smalley79af7302015-01-21 10:54:10 -05002097 struct common_audit_data ad;
2098 int rc;
2099
2100 ad.type = LSM_AUDIT_DATA_PATH;
2101 ad.u.path = file->f_path;
2102
2103 if (sid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002104 rc = avc_has_perm(&selinux_state,
2105 sid, fsec->sid,
Stephen Smalley79af7302015-01-21 10:54:10 -05002106 SECCLASS_FD,
2107 FD__USE,
2108 &ad);
2109 if (rc)
2110 return rc;
2111 }
2112
Chenbo Fengf66e4482017-10-18 13:00:26 -07002113#ifdef CONFIG_BPF_SYSCALL
2114 rc = bpf_fd_pass(file, sid);
2115 if (rc)
2116 return rc;
2117#endif
2118
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002119 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
Stephen Smalley79af7302015-01-21 10:54:10 -05002120 return 0;
2121
Paul Moore20cdef82016-04-04 14:14:42 -04002122 isec = backing_inode_security(dentry);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002123 return avc_has_perm(&selinux_state,
2124 sid, isec->sid, isec->sclass, file_to_av(file),
Stephen Smalley79af7302015-01-21 10:54:10 -05002125 &ad);
2126}
2127
Ingo Molnar9e488582009-05-07 19:26:19 +10002128static int selinux_ptrace_access_check(struct task_struct *child,
David Howells5cd9c582008-08-14 11:37:28 +01002129 unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002131 u32 sid = current_sid();
2132 u32 csid = task_sid(child);
Stephen Smalley006ebb42008-05-19 08:32:49 -04002133
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002134 if (mode & PTRACE_MODE_READ)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002135 return avc_has_perm(&selinux_state,
2136 sid, csid, SECCLASS_FILE, FILE__READ, NULL);
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002137
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002138 return avc_has_perm(&selinux_state,
2139 sid, csid, SECCLASS_PROCESS, PROCESS__PTRACE, NULL);
David Howells5cd9c582008-08-14 11:37:28 +01002140}
2141
2142static int selinux_ptrace_traceme(struct task_struct *parent)
2143{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002144 return avc_has_perm(&selinux_state,
2145 task_sid(parent), current_sid(), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002146 PROCESS__PTRACE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147}
2148
2149static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
Eric Paris828dfe12008-04-17 13:17:49 -04002150 kernel_cap_t *inheritable, kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002152 return avc_has_perm(&selinux_state,
2153 current_sid(), task_sid(target), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002154 PROCESS__GETCAP, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155}
2156
David Howellsd84f4f92008-11-14 10:39:23 +11002157static int selinux_capset(struct cred *new, const struct cred *old,
2158 const kernel_cap_t *effective,
2159 const kernel_cap_t *inheritable,
2160 const kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002162 return avc_has_perm(&selinux_state,
2163 cred_sid(old), cred_sid(new), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002164 PROCESS__SETCAP, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165}
2166
James Morris5626d3e2009-01-30 10:05:06 +11002167/*
2168 * (This comment used to live with the selinux_task_setuid hook,
2169 * which was removed).
2170 *
2171 * Since setuid only affects the current process, and since the SELinux
2172 * controls are not based on the Linux identity attributes, SELinux does not
2173 * need to control this operation. However, SELinux does control the use of
2174 * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
2175 */
2176
Eric Paris6a9de492012-01-03 12:25:14 -05002177static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
2178 int cap, int audit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179{
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04002180 return cred_has_capability(cred, cap, audit, ns == &init_user_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181}
2182
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
2184{
David Howells88e67f32008-11-14 10:39:21 +11002185 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 int rc = 0;
2187
2188 if (!sb)
2189 return 0;
2190
2191 switch (cmds) {
Eric Paris828dfe12008-04-17 13:17:49 -04002192 case Q_SYNC:
2193 case Q_QUOTAON:
2194 case Q_QUOTAOFF:
2195 case Q_SETINFO:
2196 case Q_SETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11002197 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04002198 break;
2199 case Q_GETFMT:
2200 case Q_GETINFO:
2201 case Q_GETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11002202 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04002203 break;
2204 default:
2205 rc = 0; /* let the kernel handle invalid cmds */
2206 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 }
2208 return rc;
2209}
2210
2211static int selinux_quota_on(struct dentry *dentry)
2212{
David Howells88e67f32008-11-14 10:39:21 +11002213 const struct cred *cred = current_cred();
2214
Eric Paris2875fa02011-04-28 16:04:24 -04002215 return dentry_has_perm(cred, dentry, FILE__QUOTAON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216}
2217
Eric Paris12b30522010-11-15 18:36:29 -05002218static int selinux_syslog(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 switch (type) {
Kees Cookd78ca3c2010-02-03 15:37:13 -08002221 case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */
2222 case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002223 return avc_has_perm(&selinux_state,
2224 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002225 SECCLASS_SYSTEM, SYSTEM__SYSLOG_READ, NULL);
Kees Cookd78ca3c2010-02-03 15:37:13 -08002226 case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */
2227 case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */
2228 /* Set level of messages printed to console */
2229 case SYSLOG_ACTION_CONSOLE_LEVEL:
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002230 return avc_has_perm(&selinux_state,
2231 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002232 SECCLASS_SYSTEM, SYSTEM__SYSLOG_CONSOLE,
2233 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 }
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002235 /* All other syslog types */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002236 return avc_has_perm(&selinux_state,
2237 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002238 SECCLASS_SYSTEM, SYSTEM__SYSLOG_MOD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239}
2240
2241/*
2242 * Check that a process has enough memory to allocate a new virtual
2243 * mapping. 0 means there is enough memory for the allocation to
2244 * succeed and -ENOMEM implies there is not.
2245 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 * Do not audit the selinux permission check, as this is applied to all
2247 * processes that allocate mappings.
2248 */
Alan Cox34b4e4a2007-08-22 14:01:28 -07002249static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250{
2251 int rc, cap_sys_admin = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002253 rc = cred_has_capability(current_cred(), CAP_SYS_ADMIN,
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04002254 SECURITY_CAP_NOAUDIT, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 if (rc == 0)
2256 cap_sys_admin = 1;
2257
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002258 return cap_sys_admin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259}
2260
2261/* binprm security operations */
2262
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002263static u32 ptrace_parent_sid(void)
Paul Moore0c6181c2016-03-30 21:41:21 -04002264{
2265 u32 sid = 0;
2266 struct task_struct *tracer;
2267
2268 rcu_read_lock();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002269 tracer = ptrace_parent(current);
Paul Moore0c6181c2016-03-30 21:41:21 -04002270 if (tracer)
2271 sid = task_sid(tracer);
2272 rcu_read_unlock();
2273
2274 return sid;
2275}
2276
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002277static int check_nnp_nosuid(const struct linux_binprm *bprm,
2278 const struct task_security_struct *old_tsec,
2279 const struct task_security_struct *new_tsec)
2280{
2281 int nnp = (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS);
Andy Lutomirski380cf5b2016-06-23 16:41:05 -05002282 int nosuid = !mnt_may_suid(bprm->file->f_path.mnt);
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002283 int rc;
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002284 u32 av;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002285
2286 if (!nnp && !nosuid)
2287 return 0; /* neither NNP nor nosuid */
2288
2289 if (new_tsec->sid == old_tsec->sid)
2290 return 0; /* No change in credentials */
2291
2292 /*
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002293 * If the policy enables the nnp_nosuid_transition policy capability,
2294 * then we permit transitions under NNP or nosuid if the
2295 * policy allows the corresponding permission between
2296 * the old and new contexts.
2297 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002298 if (selinux_policycap_nnp_nosuid_transition()) {
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002299 av = 0;
2300 if (nnp)
2301 av |= PROCESS2__NNP_TRANSITION;
2302 if (nosuid)
2303 av |= PROCESS2__NOSUID_TRANSITION;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002304 rc = avc_has_perm(&selinux_state,
2305 old_tsec->sid, new_tsec->sid,
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002306 SECCLASS_PROCESS2, av, NULL);
2307 if (!rc)
2308 return 0;
2309 }
2310
2311 /*
2312 * We also permit NNP or nosuid transitions to bounded SIDs,
2313 * i.e. SIDs that are guaranteed to only be allowed a subset
2314 * of the permissions of the current SID.
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002315 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002316 rc = security_bounded_transition(&selinux_state, old_tsec->sid,
2317 new_tsec->sid);
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002318 if (!rc)
2319 return 0;
2320
2321 /*
2322 * On failure, preserve the errno values for NNP vs nosuid.
2323 * NNP: Operation not permitted for caller.
2324 * nosuid: Permission denied to file.
2325 */
2326 if (nnp)
2327 return -EPERM;
2328 return -EACCES;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002329}
2330
David Howellsa6f76f22008-11-14 10:39:24 +11002331static int selinux_bprm_set_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332{
David Howellsa6f76f22008-11-14 10:39:24 +11002333 const struct task_security_struct *old_tsec;
2334 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 struct inode_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04002336 struct common_audit_data ad;
Al Viro496ad9a2013-01-23 17:07:38 -05002337 struct inode *inode = file_inode(bprm->file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 int rc;
2339
David Howellsa6f76f22008-11-14 10:39:24 +11002340 /* SELinux context only depends on initial program or script and not
2341 * the script interpreter */
Kees Cookddb4a142017-07-18 15:25:23 -07002342 if (bprm->called_set_creds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 return 0;
2344
David Howellsa6f76f22008-11-14 10:39:24 +11002345 old_tsec = current_security();
2346 new_tsec = bprm->cred->security;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002347 isec = inode_security(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
2349 /* Default to the current task SID. */
David Howellsa6f76f22008-11-14 10:39:24 +11002350 new_tsec->sid = old_tsec->sid;
2351 new_tsec->osid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
Michael LeMay28eba5b2006-06-27 02:53:42 -07002353 /* Reset fs, key, and sock SIDs on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002354 new_tsec->create_sid = 0;
2355 new_tsec->keycreate_sid = 0;
2356 new_tsec->sockcreate_sid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357
David Howellsa6f76f22008-11-14 10:39:24 +11002358 if (old_tsec->exec_sid) {
2359 new_tsec->sid = old_tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 /* Reset exec SID on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002361 new_tsec->exec_sid = 0;
Andy Lutomirski259e5e62012-04-12 16:47:50 -05002362
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002363 /* Fail on NNP or nosuid if not an allowed transition. */
2364 rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
2365 if (rc)
2366 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 } else {
2368 /* Check for a default transition on this program. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002369 rc = security_transition_sid(&selinux_state, old_tsec->sid,
2370 isec->sid, SECCLASS_PROCESS, NULL,
Eric Paris652bb9b2011-02-01 11:05:40 -05002371 &new_tsec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 if (rc)
2373 return rc;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002374
2375 /*
2376 * Fallback to old SID on NNP or nosuid if not an allowed
2377 * transition.
2378 */
2379 rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
2380 if (rc)
2381 new_tsec->sid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 }
2383
Vivek Goyal43af5de2016-09-09 11:37:49 -04002384 ad.type = LSM_AUDIT_DATA_FILE;
2385 ad.u.file = bprm->file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
David Howellsa6f76f22008-11-14 10:39:24 +11002387 if (new_tsec->sid == old_tsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002388 rc = avc_has_perm(&selinux_state,
2389 old_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
2391 if (rc)
2392 return rc;
2393 } else {
2394 /* Check permissions for the transition. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002395 rc = avc_has_perm(&selinux_state,
2396 old_tsec->sid, new_tsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
2398 if (rc)
2399 return rc;
2400
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002401 rc = avc_has_perm(&selinux_state,
2402 new_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
2404 if (rc)
2405 return rc;
2406
David Howellsa6f76f22008-11-14 10:39:24 +11002407 /* Check for shared state */
2408 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002409 rc = avc_has_perm(&selinux_state,
2410 old_tsec->sid, new_tsec->sid,
David Howellsa6f76f22008-11-14 10:39:24 +11002411 SECCLASS_PROCESS, PROCESS__SHARE,
2412 NULL);
2413 if (rc)
2414 return -EPERM;
2415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416
David Howellsa6f76f22008-11-14 10:39:24 +11002417 /* Make sure that anyone attempting to ptrace over a task that
2418 * changes its SID has the appropriate permit */
Eric W. Biederman9227dd22017-01-23 17:26:31 +13002419 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002420 u32 ptsid = ptrace_parent_sid();
David Howellsa6f76f22008-11-14 10:39:24 +11002421 if (ptsid != 0) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002422 rc = avc_has_perm(&selinux_state,
2423 ptsid, new_tsec->sid,
David Howellsa6f76f22008-11-14 10:39:24 +11002424 SECCLASS_PROCESS,
2425 PROCESS__PTRACE, NULL);
2426 if (rc)
2427 return -EPERM;
2428 }
2429 }
2430
2431 /* Clear any possibly unsafe personality bits on exec: */
2432 bprm->per_clear |= PER_CLEAR_ON_SETID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 /* Enable secure mode for SIDs transitions unless
2435 the noatsecure permission is granted between
2436 the two SIDs, i.e. ahp returns 0. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002437 rc = avc_has_perm(&selinux_state,
2438 old_tsec->sid, new_tsec->sid,
Kees Cook62874c32017-07-18 15:25:25 -07002439 SECCLASS_PROCESS, PROCESS__NOATSECURE,
2440 NULL);
2441 bprm->secureexec |= !!rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 }
2443
Kees Cook62874c32017-07-18 15:25:25 -07002444 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445}
2446
Al Viroc3c073f2012-08-21 22:32:06 -04002447static int match_file(const void *p, struct file *file, unsigned fd)
2448{
2449 return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0;
2450}
2451
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452/* Derived from fs/exec.c:flush_old_files. */
David Howells745ca242008-11-14 10:39:22 +11002453static inline void flush_unauthorized_files(const struct cred *cred,
2454 struct files_struct *files)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 struct file *file, *devnull = NULL;
Stephen Smalleyb20c8122006-09-25 23:32:03 -07002457 struct tty_struct *tty;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002458 int drop_tty = 0;
Al Viroc3c073f2012-08-21 22:32:06 -04002459 unsigned n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002461 tty = get_current_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 if (tty) {
Peter Hurley4a510962016-01-09 21:35:23 -08002463 spin_lock(&tty->files_lock);
Eric Paris37dd0bd2008-10-31 17:40:00 -04002464 if (!list_empty(&tty->tty_files)) {
Nick Piggind996b622010-08-18 04:37:36 +10002465 struct tty_file_private *file_priv;
Eric Paris37dd0bd2008-10-31 17:40:00 -04002466
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 /* Revalidate access to controlling tty.
David Howells13f8e982013-06-13 23:37:55 +01002468 Use file_path_has_perm on the tty path directly
2469 rather than using file_has_perm, as this particular
2470 open file may belong to another process and we are
2471 only interested in the inode-based check here. */
Nick Piggind996b622010-08-18 04:37:36 +10002472 file_priv = list_first_entry(&tty->tty_files,
2473 struct tty_file_private, list);
2474 file = file_priv->file;
David Howells13f8e982013-06-13 23:37:55 +01002475 if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE))
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002476 drop_tty = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 }
Peter Hurley4a510962016-01-09 21:35:23 -08002478 spin_unlock(&tty->files_lock);
Alan Cox452a00d2008-10-13 10:39:13 +01002479 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 }
Eric W. Biederman98a27ba2007-05-08 00:26:56 -07002481 /* Reset controlling tty. */
2482 if (drop_tty)
2483 no_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484
2485 /* Revalidate access to inherited open files. */
Al Viroc3c073f2012-08-21 22:32:06 -04002486 n = iterate_fd(files, 0, match_file, cred);
2487 if (!n) /* none found? */
2488 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489
Al Viroc3c073f2012-08-21 22:32:06 -04002490 devnull = dentry_open(&selinux_null, O_RDWR, cred);
Al Viro45525b22012-10-16 13:30:07 -04002491 if (IS_ERR(devnull))
2492 devnull = NULL;
2493 /* replace all the matching ones with this */
2494 do {
2495 replace_fd(n - 1, devnull, 0);
2496 } while ((n = iterate_fd(files, n, match_file, cred)) != 0);
2497 if (devnull)
Al Viroc3c073f2012-08-21 22:32:06 -04002498 fput(devnull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499}
2500
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501/*
David Howellsa6f76f22008-11-14 10:39:24 +11002502 * Prepare a process for imminent new credential changes due to exec
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 */
David Howellsa6f76f22008-11-14 10:39:24 +11002504static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505{
David Howellsa6f76f22008-11-14 10:39:24 +11002506 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 struct rlimit *rlim, *initrlim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 int rc, i;
2509
David Howellsa6f76f22008-11-14 10:39:24 +11002510 new_tsec = bprm->cred->security;
2511 if (new_tsec->sid == new_tsec->osid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 return;
2513
2514 /* Close files for which the new task SID is not authorized. */
David Howellsa6f76f22008-11-14 10:39:24 +11002515 flush_unauthorized_files(bprm->cred, current->files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516
David Howellsa6f76f22008-11-14 10:39:24 +11002517 /* Always clear parent death signal on SID transitions. */
2518 current->pdeath_signal = 0;
2519
2520 /* Check whether the new SID can inherit resource limits from the old
2521 * SID. If not, reset all soft limits to the lower of the current
2522 * task's hard limit and the init task's soft limit.
2523 *
2524 * Note that the setting of hard limits (even to lower them) can be
2525 * controlled by the setrlimit check. The inclusion of the init task's
2526 * soft limit into the computation is to avoid resetting soft limits
2527 * higher than the default soft limit for cases where the default is
2528 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2529 */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002530 rc = avc_has_perm(&selinux_state,
2531 new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
David Howellsa6f76f22008-11-14 10:39:24 +11002532 PROCESS__RLIMITINH, NULL);
2533 if (rc) {
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002534 /* protect against do_prlimit() */
2535 task_lock(current);
David Howellsa6f76f22008-11-14 10:39:24 +11002536 for (i = 0; i < RLIM_NLIMITS; i++) {
2537 rlim = current->signal->rlim + i;
2538 initrlim = init_task.signal->rlim + i;
2539 rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
2540 }
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002541 task_unlock(current);
Nicolas Pitrebaa73d92016-11-11 00:10:10 -05002542 if (IS_ENABLED(CONFIG_POSIX_TIMERS))
2543 update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
David Howellsa6f76f22008-11-14 10:39:24 +11002544 }
2545}
2546
2547/*
2548 * Clean up the process immediately after the installation of new credentials
2549 * due to exec
2550 */
2551static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
2552{
2553 const struct task_security_struct *tsec = current_security();
2554 struct itimerval itimer;
David Howellsa6f76f22008-11-14 10:39:24 +11002555 u32 osid, sid;
2556 int rc, i;
David Howellsa6f76f22008-11-14 10:39:24 +11002557
David Howellsa6f76f22008-11-14 10:39:24 +11002558 osid = tsec->osid;
2559 sid = tsec->sid;
2560
2561 if (sid == osid)
2562 return;
2563
2564 /* Check whether the new SID can inherit signal state from the old SID.
2565 * If not, clear itimers to avoid subsequent signal generation and
2566 * flush and unblock signals.
2567 *
2568 * This must occur _after_ the task SID has been updated so that any
2569 * kill done after the flush will be checked against the new SID.
2570 */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002571 rc = avc_has_perm(&selinux_state,
2572 osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 if (rc) {
Nicolas Pitrebaa73d92016-11-11 00:10:10 -05002574 if (IS_ENABLED(CONFIG_POSIX_TIMERS)) {
2575 memset(&itimer, 0, sizeof itimer);
2576 for (i = 0; i < 3; i++)
2577 do_setitimer(i, &itimer, NULL);
2578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterov9e7c8f82015-06-04 16:22:16 -04002580 if (!fatal_signal_pending(current)) {
2581 flush_sigqueue(&current->pending);
2582 flush_sigqueue(&current->signal->shared_pending);
David Howells3bcac022009-04-29 13:45:05 +01002583 flush_signal_handlers(current, 1);
2584 sigemptyset(&current->blocked);
Oleg Nesterov9e7c8f82015-06-04 16:22:16 -04002585 recalc_sigpending();
David Howells3bcac022009-04-29 13:45:05 +01002586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 spin_unlock_irq(&current->sighand->siglock);
2588 }
2589
David Howellsa6f76f22008-11-14 10:39:24 +11002590 /* Wake up the parent if it is waiting so that it can recheck
2591 * wait permission to the new task SID. */
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002592 read_lock(&tasklist_lock);
Oleg Nesterov0b7570e2009-09-23 15:56:46 -07002593 __wake_up_parent(current, current->real_parent);
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002594 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595}
2596
2597/* superblock security operations */
2598
2599static int selinux_sb_alloc_security(struct super_block *sb)
2600{
2601 return superblock_alloc_security(sb);
2602}
2603
2604static void selinux_sb_free_security(struct super_block *sb)
2605{
2606 superblock_free_security(sb);
2607}
2608
Al Viro99dbbb52018-12-14 21:56:23 -05002609static inline int opt_len(const char *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610{
Al Viro99dbbb52018-12-14 21:56:23 -05002611 bool open_quote = false;
2612 int len;
2613 char c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614
Al Viro99dbbb52018-12-14 21:56:23 -05002615 for (len = 0; (c = s[len]) != '\0'; len++) {
2616 if (c == '"')
Cory Olmo3528a952006-09-29 01:58:44 -07002617 open_quote = !open_quote;
Al Viro99dbbb52018-12-14 21:56:23 -05002618 if (c == ',' && !open_quote)
2619 break;
2620 }
2621 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622}
2623
Al Viro204cc0c2018-12-13 13:41:47 -05002624static int selinux_sb_eat_lsm_opts(char *options, void **mnt_opts)
Al Viro5b400232018-12-12 20:13:29 -05002625{
Al Viro99dbbb52018-12-14 21:56:23 -05002626 char *from = options;
2627 char *to = options;
2628 bool first = true;
Al Viro5b400232018-12-12 20:13:29 -05002629
Al Viro99dbbb52018-12-14 21:56:23 -05002630 while (1) {
2631 int len = opt_len(from);
2632 int token, rc;
2633 char *arg = NULL;
2634
2635 token = match_opt_prefix(from, len, &arg);
2636
2637 if (token != Opt_error) {
2638 char *p, *q;
2639
2640 /* strip quotes */
2641 if (arg) {
2642 for (p = q = arg; p < from + len; p++) {
2643 char c = *p;
2644 if (c != '"')
2645 *q++ = c;
2646 }
2647 arg = kmemdup_nul(arg, q - arg, GFP_KERNEL);
2648 }
2649 rc = selinux_add_opt(token, arg, mnt_opts);
2650 if (unlikely(rc)) {
2651 kfree(arg);
2652 if (*mnt_opts) {
2653 selinux_free_mnt_opts(*mnt_opts);
2654 *mnt_opts = NULL;
2655 }
2656 return rc;
2657 }
2658 } else {
2659 if (!first) { // copy with preceding comma
2660 from--;
2661 len++;
2662 }
2663 if (to != from)
2664 memmove(to, from, len);
2665 to += len;
2666 first = false;
2667 }
2668 if (!from[len])
2669 break;
2670 from += len + 1;
2671 }
2672 *to = '\0';
2673 return 0;
Al Viro5b400232018-12-12 20:13:29 -05002674}
2675
Al Viro204cc0c2018-12-13 13:41:47 -05002676static int selinux_sb_remount(struct super_block *sb, void *mnt_opts)
Eric Paris026eb162011-03-03 16:09:14 -05002677{
Al Virobd323652018-12-13 15:04:59 -05002678 struct selinux_mnt_opts *opts = mnt_opts;
Eric Paris026eb162011-03-03 16:09:14 -05002679 struct superblock_security_struct *sbsec = sb->s_security;
Al Virobd323652018-12-13 15:04:59 -05002680 u32 sid;
2681 int rc;
Eric Paris026eb162011-03-03 16:09:14 -05002682
2683 if (!(sbsec->flags & SE_SBINITIALIZED))
2684 return 0;
2685
Al Viro204cc0c2018-12-13 13:41:47 -05002686 if (!opts)
2687 return 0;
2688
Al Virobd323652018-12-13 15:04:59 -05002689 if (opts->fscontext) {
2690 rc = parse_sid(sb, opts->fscontext, &sid);
2691 if (rc)
Al Viroc039bc32018-12-01 23:06:57 -05002692 return rc;
Al Virobd323652018-12-13 15:04:59 -05002693 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
2694 goto out_bad_option;
2695 }
2696 if (opts->context) {
2697 rc = parse_sid(sb, opts->context, &sid);
2698 if (rc)
2699 return rc;
2700 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
2701 goto out_bad_option;
2702 }
2703 if (opts->rootcontext) {
2704 struct inode_security_struct *root_isec;
2705 root_isec = backing_inode_security(sb->s_root);
2706 rc = parse_sid(sb, opts->rootcontext, &sid);
2707 if (rc)
2708 return rc;
2709 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
2710 goto out_bad_option;
2711 }
2712 if (opts->defcontext) {
2713 rc = parse_sid(sb, opts->defcontext, &sid);
2714 if (rc)
2715 return rc;
2716 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
2717 goto out_bad_option;
Eric Paris026eb162011-03-03 16:09:14 -05002718 }
Al Viroc039bc32018-12-01 23:06:57 -05002719 return 0;
Eric Paris026eb162011-03-03 16:09:14 -05002720
Eric Paris026eb162011-03-03 16:09:14 -05002721out_bad_option:
peter enderborgc103a912018-06-12 10:09:03 +02002722 pr_warn("SELinux: unable to change security options "
Linus Torvalds29b1deb2013-12-15 11:17:45 -08002723 "during remount (dev %s, type=%s)\n", sb->s_id,
2724 sb->s_type->name);
Al Viroc039bc32018-12-01 23:06:57 -05002725 return -EINVAL;
Eric Paris026eb162011-03-03 16:09:14 -05002726}
2727
Al Viroa10d7c22018-12-05 11:58:35 -05002728static int selinux_sb_kern_mount(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729{
David Howells88e67f32008-11-14 10:39:21 +11002730 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002731 struct common_audit_data ad;
James Morris74192242008-12-19 11:41:10 +11002732
Eric Paris50c205f2012-04-04 15:01:43 -04002733 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002734 ad.u.dentry = sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002735 return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736}
2737
David Howells726c3342006-06-23 02:02:58 -07002738static int selinux_sb_statfs(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739{
David Howells88e67f32008-11-14 10:39:21 +11002740 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002741 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742
Eric Paris50c205f2012-04-04 15:01:43 -04002743 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002744 ad.u.dentry = dentry->d_sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002745 return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746}
2747
Al Viro808d4e32012-10-11 11:42:01 -04002748static int selinux_mount(const char *dev_name,
Al Viro8a04c432016-03-25 14:52:53 -04002749 const struct path *path,
Al Viro808d4e32012-10-11 11:42:01 -04002750 const char *type,
Eric Paris828dfe12008-04-17 13:17:49 -04002751 unsigned long flags,
2752 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753{
David Howells88e67f32008-11-14 10:39:21 +11002754 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755
2756 if (flags & MS_REMOUNT)
Al Virod8c95842011-12-07 18:16:57 -05002757 return superblock_has_perm(cred, path->dentry->d_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002758 FILESYSTEM__REMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 else
Eric Paris2875fa02011-04-28 16:04:24 -04002760 return path_has_perm(cred, path, FILE__MOUNTON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761}
2762
2763static int selinux_umount(struct vfsmount *mnt, int flags)
2764{
David Howells88e67f32008-11-14 10:39:21 +11002765 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766
David Howells88e67f32008-11-14 10:39:21 +11002767 return superblock_has_perm(cred, mnt->mnt_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002768 FILESYSTEM__UNMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769}
2770
2771/* inode security operations */
2772
2773static int selinux_inode_alloc_security(struct inode *inode)
2774{
2775 return inode_alloc_security(inode);
2776}
2777
2778static void selinux_inode_free_security(struct inode *inode)
2779{
2780 inode_free_security(inode);
2781}
2782
David Quigleyd47be3d2013-05-22 12:50:34 -04002783static int selinux_dentry_init_security(struct dentry *dentry, int mode,
Al Viro4f3ccd72016-07-20 16:06:15 -04002784 const struct qstr *name, void **ctx,
David Quigleyd47be3d2013-05-22 12:50:34 -04002785 u32 *ctxlen)
2786{
David Quigleyd47be3d2013-05-22 12:50:34 -04002787 u32 newsid;
2788 int rc;
2789
Vivek Goyalc957f6d2016-07-13 10:44:51 -04002790 rc = selinux_determine_inode_label(current_security(),
2791 d_inode(dentry->d_parent), name,
David Howellsc3c188b2015-07-10 17:19:58 -04002792 inode_mode_to_security_class(mode),
2793 &newsid);
2794 if (rc)
2795 return rc;
David Quigleyd47be3d2013-05-22 12:50:34 -04002796
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002797 return security_sid_to_context(&selinux_state, newsid, (char **)ctx,
2798 ctxlen);
David Quigleyd47be3d2013-05-22 12:50:34 -04002799}
2800
Vivek Goyala518b0a2016-07-13 10:44:53 -04002801static int selinux_dentry_create_files_as(struct dentry *dentry, int mode,
2802 struct qstr *name,
2803 const struct cred *old,
2804 struct cred *new)
2805{
2806 u32 newsid;
2807 int rc;
2808 struct task_security_struct *tsec;
2809
2810 rc = selinux_determine_inode_label(old->security,
2811 d_inode(dentry->d_parent), name,
2812 inode_mode_to_security_class(mode),
2813 &newsid);
2814 if (rc)
2815 return rc;
2816
2817 tsec = new->security;
2818 tsec->create_sid = newsid;
2819 return 0;
2820}
2821
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002822static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +09002823 const struct qstr *qstr,
2824 const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -05002825 void **value, size_t *len)
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002826{
Paul Moore5fb49872010-04-22 14:46:19 -04002827 const struct task_security_struct *tsec = current_security();
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002828 struct superblock_security_struct *sbsec;
Corentin LABBEc0d4f462017-10-04 20:32:17 +02002829 u32 newsid, clen;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002830 int rc;
Tetsuo Handa95489062013-07-25 05:44:02 +09002831 char *context;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002832
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002833 sbsec = dir->i_sb->s_security;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002834
David Howells275bb412008-11-14 10:39:19 +11002835 newsid = tsec->create_sid;
2836
Vivek Goyalc957f6d2016-07-13 10:44:51 -04002837 rc = selinux_determine_inode_label(current_security(),
David Howellsc3c188b2015-07-10 17:19:58 -04002838 dir, qstr,
2839 inode_mode_to_security_class(inode->i_mode),
2840 &newsid);
2841 if (rc)
2842 return rc;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002843
Eric Paris296fddf2006-09-25 23:32:00 -07002844 /* Possibly defer initialization to selinux_complete_init. */
David P. Quigley0d90a7e2009-01-16 09:22:02 -05002845 if (sbsec->flags & SE_SBINITIALIZED) {
Eric Paris296fddf2006-09-25 23:32:00 -07002846 struct inode_security_struct *isec = inode->i_security;
2847 isec->sclass = inode_mode_to_security_class(inode->i_mode);
2848 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05002849 isec->initialized = LABEL_INITIALIZED;
Eric Paris296fddf2006-09-25 23:32:00 -07002850 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002851
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002852 if (!selinux_state.initialized || !(sbsec->flags & SBLABEL_MNT))
Stephen Smalley25a74f32005-11-08 21:34:33 -08002853 return -EOPNOTSUPP;
2854
Tetsuo Handa95489062013-07-25 05:44:02 +09002855 if (name)
2856 *name = XATTR_SELINUX_SUFFIX;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002857
2858 if (value && len) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002859 rc = security_sid_to_context_force(&selinux_state, newsid,
2860 &context, &clen);
Tetsuo Handa95489062013-07-25 05:44:02 +09002861 if (rc)
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002862 return rc;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002863 *value = context;
2864 *len = clen;
2865 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002866
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002867 return 0;
2868}
2869
Al Viro4acdaf22011-07-26 01:42:34 -04002870static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871{
2872 return may_create(dir, dentry, SECCLASS_FILE);
2873}
2874
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2876{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 return may_link(dir, old_dentry, MAY_LINK);
2878}
2879
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
2881{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 return may_link(dir, dentry, MAY_UNLINK);
2883}
2884
2885static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
2886{
2887 return may_create(dir, dentry, SECCLASS_LNK_FILE);
2888}
2889
Al Viro18bb1db2011-07-26 01:41:39 -04002890static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891{
2892 return may_create(dir, dentry, SECCLASS_DIR);
2893}
2894
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
2896{
2897 return may_link(dir, dentry, MAY_RMDIR);
2898}
2899
Al Viro1a67aaf2011-07-26 01:52:52 -04002900static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 return may_create(dir, dentry, inode_mode_to_security_class(mode));
2903}
2904
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
Eric Paris828dfe12008-04-17 13:17:49 -04002906 struct inode *new_inode, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907{
2908 return may_rename(old_inode, old_dentry, new_inode, new_dentry);
2909}
2910
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911static int selinux_inode_readlink(struct dentry *dentry)
2912{
David Howells88e67f32008-11-14 10:39:21 +11002913 const struct cred *cred = current_cred();
2914
Eric Paris2875fa02011-04-28 16:04:24 -04002915 return dentry_has_perm(cred, dentry, FILE__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916}
2917
NeilBrownbda0be72015-03-23 13:37:39 +11002918static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode,
2919 bool rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920{
David Howells88e67f32008-11-14 10:39:21 +11002921 const struct cred *cred = current_cred();
NeilBrownbda0be72015-03-23 13:37:39 +11002922 struct common_audit_data ad;
2923 struct inode_security_struct *isec;
2924 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925
NeilBrownbda0be72015-03-23 13:37:39 +11002926 validate_creds(cred);
2927
2928 ad.type = LSM_AUDIT_DATA_DENTRY;
2929 ad.u.dentry = dentry;
2930 sid = cred_sid(cred);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05002931 isec = inode_security_rcu(inode, rcu);
2932 if (IS_ERR(isec))
2933 return PTR_ERR(isec);
NeilBrownbda0be72015-03-23 13:37:39 +11002934
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002935 return avc_has_perm_flags(&selinux_state,
2936 sid, isec->sid, isec->sclass, FILE__READ, &ad,
NeilBrownbda0be72015-03-23 13:37:39 +11002937 rcu ? MAY_NOT_BLOCK : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938}
2939
Eric Parisd4cf970d2012-04-04 15:01:42 -04002940static noinline int audit_inode_permission(struct inode *inode,
2941 u32 perms, u32 audited, u32 denied,
Stephen Smalley626b9742014-04-29 11:29:04 -07002942 int result,
Eric Parisd4cf970d2012-04-04 15:01:42 -04002943 unsigned flags)
2944{
2945 struct common_audit_data ad;
Eric Parisd4cf970d2012-04-04 15:01:42 -04002946 struct inode_security_struct *isec = inode->i_security;
2947 int rc;
2948
Eric Paris50c205f2012-04-04 15:01:43 -04002949 ad.type = LSM_AUDIT_DATA_INODE;
Eric Parisd4cf970d2012-04-04 15:01:42 -04002950 ad.u.inode = inode;
2951
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002952 rc = slow_avc_audit(&selinux_state,
2953 current_sid(), isec->sid, isec->sclass, perms,
Stephen Smalley626b9742014-04-29 11:29:04 -07002954 audited, denied, result, &ad, flags);
Eric Parisd4cf970d2012-04-04 15:01:42 -04002955 if (rc)
2956 return rc;
2957 return 0;
2958}
2959
Al Viroe74f71e2011-06-20 19:38:15 -04002960static int selinux_inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961{
David Howells88e67f32008-11-14 10:39:21 +11002962 const struct cred *cred = current_cred();
Eric Parisb782e0a2010-07-23 11:44:03 -04002963 u32 perms;
2964 bool from_access;
Al Virocf1dd1d2011-06-20 19:44:08 -04002965 unsigned flags = mask & MAY_NOT_BLOCK;
Eric Paris2e334052012-04-04 15:01:42 -04002966 struct inode_security_struct *isec;
2967 u32 sid;
2968 struct av_decision avd;
2969 int rc, rc2;
2970 u32 audited, denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971
Eric Parisb782e0a2010-07-23 11:44:03 -04002972 from_access = mask & MAY_ACCESS;
Eric Parisd09ca732010-07-23 11:43:57 -04002973 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
2974
Eric Parisb782e0a2010-07-23 11:44:03 -04002975 /* No permission to check. Existence test. */
2976 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
Eric Paris2e334052012-04-04 15:01:42 -04002979 validate_creds(cred);
Eric Parisb782e0a2010-07-23 11:44:03 -04002980
Eric Paris2e334052012-04-04 15:01:42 -04002981 if (unlikely(IS_PRIVATE(inode)))
2982 return 0;
Eric Parisb782e0a2010-07-23 11:44:03 -04002983
2984 perms = file_mask_to_av(inode->i_mode, mask);
2985
Eric Paris2e334052012-04-04 15:01:42 -04002986 sid = cred_sid(cred);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05002987 isec = inode_security_rcu(inode, flags & MAY_NOT_BLOCK);
2988 if (IS_ERR(isec))
2989 return PTR_ERR(isec);
Eric Paris2e334052012-04-04 15:01:42 -04002990
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002991 rc = avc_has_perm_noaudit(&selinux_state,
2992 sid, isec->sid, isec->sclass, perms, 0, &avd);
Eric Paris2e334052012-04-04 15:01:42 -04002993 audited = avc_audit_required(perms, &avd, rc,
2994 from_access ? FILE__AUDIT_ACCESS : 0,
2995 &denied);
2996 if (likely(!audited))
2997 return rc;
2998
Stephen Smalley626b9742014-04-29 11:29:04 -07002999 rc2 = audit_inode_permission(inode, perms, audited, denied, rc, flags);
Eric Paris2e334052012-04-04 15:01:42 -04003000 if (rc2)
3001 return rc2;
3002 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003}
3004
3005static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
3006{
David Howells88e67f32008-11-14 10:39:21 +11003007 const struct cred *cred = current_cred();
Stephen Smalleyccb54472017-05-12 12:41:24 -04003008 struct inode *inode = d_backing_inode(dentry);
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003009 unsigned int ia_valid = iattr->ia_valid;
Eric Paris95dbf732012-04-04 13:45:34 -04003010 __u32 av = FILE__WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003012 /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
3013 if (ia_valid & ATTR_FORCE) {
3014 ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
3015 ATTR_FORCE);
3016 if (!ia_valid)
3017 return 0;
3018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003020 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
3021 ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
Eric Paris2875fa02011-04-28 16:04:24 -04003022 return dentry_has_perm(cred, dentry, FILE__SETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003024 if (selinux_policycap_openperm() &&
Stephen Smalleyccb54472017-05-12 12:41:24 -04003025 inode->i_sb->s_magic != SOCKFS_MAGIC &&
3026 (ia_valid & ATTR_SIZE) &&
3027 !(ia_valid & ATTR_FILE))
Eric Paris95dbf732012-04-04 13:45:34 -04003028 av |= FILE__OPEN;
3029
3030 return dentry_has_perm(cred, dentry, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031}
3032
Al Viro3f7036a2015-03-08 19:28:30 -04003033static int selinux_inode_getattr(const struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034{
Al Viro3f7036a2015-03-08 19:28:30 -04003035 return path_has_perm(current_cred(), path, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036}
3037
Stephen Smalleydb590002017-04-20 11:31:30 -04003038static bool has_cap_mac_admin(bool audit)
3039{
3040 const struct cred *cred = current_cred();
3041 int cap_audit = audit ? SECURITY_CAP_AUDIT : SECURITY_CAP_NOAUDIT;
3042
3043 if (cap_capable(cred, &init_user_ns, CAP_MAC_ADMIN, cap_audit))
3044 return false;
3045 if (cred_has_capability(cred, CAP_MAC_ADMIN, cap_audit, true))
3046 return false;
3047 return true;
3048}
3049
David Howells8f0cfa52008-04-29 00:59:41 -07003050static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
3051 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052{
David Howellsc6f493d2015-03-17 22:26:22 +00003053 struct inode *inode = d_backing_inode(dentry);
Paul Moore20cdef82016-04-04 14:14:42 -04003054 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 struct superblock_security_struct *sbsec;
Thomas Liu2bf49692009-07-14 12:14:09 -04003056 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11003057 u32 newsid, sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 int rc = 0;
3059
Eric W. Biederman6b240302017-10-02 09:38:20 -05003060 if (strcmp(name, XATTR_NAME_SELINUX)) {
3061 rc = cap_inode_setxattr(dentry, name, value, size, flags);
3062 if (rc)
3063 return rc;
3064
3065 /* Not an attribute we recognize, so just check the
3066 ordinary setattr permission. */
3067 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069
3070 sbsec = inode->i_sb->s_security;
Eric Paris12f348b2012-10-09 10:56:25 -04003071 if (!(sbsec->flags & SBLABEL_MNT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 return -EOPNOTSUPP;
3073
Serge E. Hallyn2e149672011-03-23 16:43:26 -07003074 if (!inode_owner_or_capable(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 return -EPERM;
3076
Eric Paris50c205f2012-04-04 15:01:43 -04003077 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04003078 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079
Paul Moore20cdef82016-04-04 14:14:42 -04003080 isec = backing_inode_security(dentry);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003081 rc = avc_has_perm(&selinux_state,
3082 sid, isec->sid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 FILE__RELABELFROM, &ad);
3084 if (rc)
3085 return rc;
3086
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003087 rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3088 GFP_KERNEL);
Stephen Smalley12b29f32008-05-07 13:03:20 -04003089 if (rc == -EINVAL) {
Stephen Smalleydb590002017-04-20 11:31:30 -04003090 if (!has_cap_mac_admin(true)) {
Eric Parisd6ea83e2012-04-04 13:45:49 -04003091 struct audit_buffer *ab;
3092 size_t audit_size;
Eric Parisd6ea83e2012-04-04 13:45:49 -04003093
3094 /* We strip a nul only if it is at the end, otherwise the
3095 * context contains a nul and we should audit that */
Al Viroe3fea3f2012-06-09 08:15:16 +01003096 if (value) {
Colin Ian Kingadd24372017-10-14 13:46:55 +01003097 const char *str = value;
3098
Al Viroe3fea3f2012-06-09 08:15:16 +01003099 if (str[size - 1] == '\0')
3100 audit_size = size - 1;
3101 else
3102 audit_size = size;
3103 } else {
Al Viroe3fea3f2012-06-09 08:15:16 +01003104 audit_size = 0;
3105 }
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04003106 ab = audit_log_start(audit_context(),
3107 GFP_ATOMIC, AUDIT_SELINUX_ERR);
Eric Parisd6ea83e2012-04-04 13:45:49 -04003108 audit_log_format(ab, "op=setxattr invalid_context=");
3109 audit_log_n_untrustedstring(ab, value, audit_size);
3110 audit_log_end(ab);
3111
Stephen Smalley12b29f32008-05-07 13:03:20 -04003112 return rc;
Eric Parisd6ea83e2012-04-04 13:45:49 -04003113 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003114 rc = security_context_to_sid_force(&selinux_state, value,
3115 size, &newsid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04003116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 if (rc)
3118 return rc;
3119
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003120 rc = avc_has_perm(&selinux_state,
3121 sid, newsid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 FILE__RELABELTO, &ad);
3123 if (rc)
3124 return rc;
3125
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003126 rc = security_validate_transition(&selinux_state, isec->sid, newsid,
3127 sid, isec->sclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 if (rc)
3129 return rc;
3130
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003131 return avc_has_perm(&selinux_state,
3132 newsid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 sbsec->sid,
3134 SECCLASS_FILESYSTEM,
3135 FILESYSTEM__ASSOCIATE,
3136 &ad);
3137}
3138
David Howells8f0cfa52008-04-29 00:59:41 -07003139static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
Eric Parisf5269712008-05-14 11:27:45 -04003140 const void *value, size_t size,
David Howells8f0cfa52008-04-29 00:59:41 -07003141 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 u32 newsid;
3146 int rc;
3147
3148 if (strcmp(name, XATTR_NAME_SELINUX)) {
3149 /* Not an attribute we recognize, so nothing to do. */
3150 return;
3151 }
3152
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003153 rc = security_context_to_sid_force(&selinux_state, value, size,
3154 &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 if (rc) {
peter enderborgc103a912018-06-12 10:09:03 +02003156 pr_err("SELinux: unable to map context to SID"
Stephen Smalley12b29f32008-05-07 13:03:20 -04003157 "for (%s, %lu), rc=%d\n",
3158 inode->i_sb->s_id, inode->i_ino, -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 return;
3160 }
3161
Paul Moore20cdef82016-04-04 14:14:42 -04003162 isec = backing_inode_security(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003163 spin_lock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003164 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05003166 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003167 spin_unlock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003168
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 return;
3170}
3171
David Howells8f0cfa52008-04-29 00:59:41 -07003172static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173{
David Howells88e67f32008-11-14 10:39:21 +11003174 const struct cred *cred = current_cred();
3175
Eric Paris2875fa02011-04-28 16:04:24 -04003176 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177}
3178
Eric Paris828dfe12008-04-17 13:17:49 -04003179static int selinux_inode_listxattr(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180{
David Howells88e67f32008-11-14 10:39:21 +11003181 const struct cred *cred = current_cred();
3182
Eric Paris2875fa02011-04-28 16:04:24 -04003183 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184}
3185
David Howells8f0cfa52008-04-29 00:59:41 -07003186static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187{
Eric W. Biederman6b240302017-10-02 09:38:20 -05003188 if (strcmp(name, XATTR_NAME_SELINUX)) {
3189 int rc = cap_inode_removexattr(dentry, name);
3190 if (rc)
3191 return rc;
3192
3193 /* Not an attribute we recognize, so just check the
3194 ordinary setattr permission. */
3195 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197
3198 /* No one is allowed to remove a SELinux security label.
3199 You can change the label, but all data must be labeled. */
3200 return -EACCES;
3201}
3202
James Morrisd381d8a2005-10-30 14:59:22 -08003203/*
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003204 * Copy the inode security context value to the user.
James Morrisd381d8a2005-10-30 14:59:22 -08003205 *
3206 * Permission check is handled by selinux_inode_getxattr hook.
3207 */
Andreas Gruenbacherea861df2015-12-24 11:09:39 -05003208static int selinux_inode_getsecurity(struct inode *inode, const char *name, void **buffer, bool alloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209{
David P. Quigley42492592008-02-04 22:29:39 -08003210 u32 size;
3211 int error;
3212 char *context = NULL;
Paul Moore20cdef82016-04-04 14:14:42 -04003213 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00003215 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3216 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003218 /*
3219 * If the caller has CAP_MAC_ADMIN, then get the raw context
3220 * value even if it is not defined by current policy; otherwise,
3221 * use the in-core value under current policy.
3222 * Use the non-auditing forms of the permission checks since
3223 * getxattr may be called by unprivileged processes commonly
3224 * and lack of permission just means that we fall back to the
3225 * in-core context value, not a denial.
3226 */
Paul Moore20cdef82016-04-04 14:14:42 -04003227 isec = inode_security(inode);
Stephen Smalleydb590002017-04-20 11:31:30 -04003228 if (has_cap_mac_admin(false))
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003229 error = security_sid_to_context_force(&selinux_state,
3230 isec->sid, &context,
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003231 &size);
3232 else
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003233 error = security_sid_to_context(&selinux_state, isec->sid,
3234 &context, &size);
David P. Quigley42492592008-02-04 22:29:39 -08003235 if (error)
3236 return error;
3237 error = size;
3238 if (alloc) {
3239 *buffer = context;
3240 goto out_nofree;
3241 }
3242 kfree(context);
3243out_nofree:
3244 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245}
3246
3247static int selinux_inode_setsecurity(struct inode *inode, const char *name,
Eric Paris828dfe12008-04-17 13:17:49 -04003248 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249{
Paul Moore2c971652016-04-19 16:36:28 -04003250 struct inode_security_struct *isec = inode_security_novalidate(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 u32 newsid;
3252 int rc;
3253
3254 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3255 return -EOPNOTSUPP;
3256
3257 if (!value || !size)
3258 return -EACCES;
3259
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003260 rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3261 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 if (rc)
3263 return rc;
3264
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003265 spin_lock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003266 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05003268 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003269 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 return 0;
3271}
3272
3273static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
3274{
3275 const int len = sizeof(XATTR_NAME_SELINUX);
3276 if (buffer && len <= buffer_size)
3277 memcpy(buffer, XATTR_NAME_SELINUX, len);
3278 return len;
3279}
3280
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05003281static void selinux_inode_getsecid(struct inode *inode, u32 *secid)
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02003282{
Andreas Gruenbachere817c2f2016-02-18 12:04:08 +01003283 struct inode_security_struct *isec = inode_security_novalidate(inode);
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02003284 *secid = isec->sid;
3285}
3286
Vivek Goyal56909eb2016-07-13 10:44:48 -04003287static int selinux_inode_copy_up(struct dentry *src, struct cred **new)
3288{
3289 u32 sid;
3290 struct task_security_struct *tsec;
3291 struct cred *new_creds = *new;
3292
3293 if (new_creds == NULL) {
3294 new_creds = prepare_creds();
3295 if (!new_creds)
3296 return -ENOMEM;
3297 }
3298
3299 tsec = new_creds->security;
3300 /* Get label from overlay inode and set it in create_sid */
3301 selinux_inode_getsecid(d_inode(src), &sid);
3302 tsec->create_sid = sid;
3303 *new = new_creds;
3304 return 0;
3305}
3306
Vivek Goyal19472b62016-07-13 10:44:50 -04003307static int selinux_inode_copy_up_xattr(const char *name)
3308{
3309 /* The copy_up hook above sets the initial context on an inode, but we
3310 * don't then want to overwrite it by blindly copying all the lower
3311 * xattrs up. Instead, we have to filter out SELinux-related xattrs.
3312 */
3313 if (strcmp(name, XATTR_NAME_SELINUX) == 0)
3314 return 1; /* Discard */
3315 /*
3316 * Any other attribute apart from SELINUX is not claimed, supported
3317 * by selinux.
3318 */
3319 return -EOPNOTSUPP;
3320}
3321
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322/* file security operations */
3323
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003324static int selinux_revalidate_file_permission(struct file *file, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325{
David Howells88e67f32008-11-14 10:39:21 +11003326 const struct cred *cred = current_cred();
Al Viro496ad9a2013-01-23 17:07:38 -05003327 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
3330 if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
3331 mask |= MAY_APPEND;
3332
Paul Moore389fb8002009-03-27 17:10:34 -04003333 return file_has_perm(cred, file,
3334 file_mask_to_av(inode->i_mode, mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335}
3336
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003337static int selinux_file_permission(struct file *file, int mask)
3338{
Al Viro496ad9a2013-01-23 17:07:38 -05003339 struct inode *inode = file_inode(file);
Stephen Smalley20dda182009-06-22 14:54:53 -04003340 struct file_security_struct *fsec = file->f_security;
Andreas Gruenbacherb1973672016-01-05 23:12:33 +01003341 struct inode_security_struct *isec;
Stephen Smalley20dda182009-06-22 14:54:53 -04003342 u32 sid = current_sid();
3343
Paul Moore389fb8002009-03-27 17:10:34 -04003344 if (!mask)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003345 /* No permission to check. Existence test. */
3346 return 0;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003347
Andreas Gruenbacherb1973672016-01-05 23:12:33 +01003348 isec = inode_security(inode);
Stephen Smalley20dda182009-06-22 14:54:53 -04003349 if (sid == fsec->sid && fsec->isid == isec->sid &&
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003350 fsec->pseqno == avc_policy_seqno(&selinux_state))
Eric Paris83d49852012-04-04 13:45:40 -04003351 /* No change since file_open check. */
Stephen Smalley20dda182009-06-22 14:54:53 -04003352 return 0;
3353
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003354 return selinux_revalidate_file_permission(file, mask);
3355}
3356
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357static int selinux_file_alloc_security(struct file *file)
3358{
3359 return file_alloc_security(file);
3360}
3361
3362static void selinux_file_free_security(struct file *file)
3363{
3364 file_free_security(file);
3365}
3366
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003367/*
3368 * Check whether a task has the ioctl permission and cmd
3369 * operation to an inode.
3370 */
Geliang Tang1d2a1682015-10-21 17:44:27 -04003371static int ioctl_has_perm(const struct cred *cred, struct file *file,
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003372 u32 requested, u16 cmd)
3373{
3374 struct common_audit_data ad;
3375 struct file_security_struct *fsec = file->f_security;
3376 struct inode *inode = file_inode(file);
Paul Moore20cdef82016-04-04 14:14:42 -04003377 struct inode_security_struct *isec;
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003378 struct lsm_ioctlop_audit ioctl;
3379 u32 ssid = cred_sid(cred);
3380 int rc;
3381 u8 driver = cmd >> 8;
3382 u8 xperm = cmd & 0xff;
3383
3384 ad.type = LSM_AUDIT_DATA_IOCTL_OP;
3385 ad.u.op = &ioctl;
3386 ad.u.op->cmd = cmd;
3387 ad.u.op->path = file->f_path;
3388
3389 if (ssid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003390 rc = avc_has_perm(&selinux_state,
3391 ssid, fsec->sid,
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003392 SECCLASS_FD,
3393 FD__USE,
3394 &ad);
3395 if (rc)
3396 goto out;
3397 }
3398
3399 if (unlikely(IS_PRIVATE(inode)))
3400 return 0;
3401
Paul Moore20cdef82016-04-04 14:14:42 -04003402 isec = inode_security(inode);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003403 rc = avc_has_extended_perms(&selinux_state,
3404 ssid, isec->sid, isec->sclass,
3405 requested, driver, xperm, &ad);
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003406out:
3407 return rc;
3408}
3409
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410static int selinux_file_ioctl(struct file *file, unsigned int cmd,
3411 unsigned long arg)
3412{
David Howells88e67f32008-11-14 10:39:21 +11003413 const struct cred *cred = current_cred();
Eric Paris0b24dcb2011-02-25 15:39:20 -05003414 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415
Eric Paris0b24dcb2011-02-25 15:39:20 -05003416 switch (cmd) {
3417 case FIONREAD:
3418 /* fall through */
3419 case FIBMAP:
3420 /* fall through */
3421 case FIGETBSZ:
3422 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003423 case FS_IOC_GETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003424 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003425 case FS_IOC_GETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003426 error = file_has_perm(cred, file, FILE__GETATTR);
3427 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428
Al Viro2f99c362012-03-23 16:04:05 -04003429 case FS_IOC_SETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003430 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003431 case FS_IOC_SETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003432 error = file_has_perm(cred, file, FILE__SETATTR);
3433 break;
3434
3435 /* sys_ioctl() checks */
3436 case FIONBIO:
3437 /* fall through */
3438 case FIOASYNC:
3439 error = file_has_perm(cred, file, 0);
3440 break;
3441
3442 case KDSKBENT:
3443 case KDSKBSENT:
Eric Paris6a9de492012-01-03 12:25:14 -05003444 error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04003445 SECURITY_CAP_AUDIT, true);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003446 break;
3447
3448 /* default case assumes that the command will go
3449 * to the file's ioctl() function.
3450 */
3451 default:
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003452 error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003453 }
3454 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455}
3456
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003457static int default_noexec;
3458
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
3460{
David Howells88e67f32008-11-14 10:39:21 +11003461 const struct cred *cred = current_cred();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003462 u32 sid = cred_sid(cred);
David Howellsd84f4f92008-11-14 10:39:23 +11003463 int rc = 0;
David Howells88e67f32008-11-14 10:39:21 +11003464
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003465 if (default_noexec &&
Stephen Smalley892e8ca2015-07-10 09:40:59 -04003466 (prot & PROT_EXEC) && (!file || IS_PRIVATE(file_inode(file)) ||
3467 (!shared && (prot & PROT_WRITE)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 /*
3469 * We are making executable an anonymous mapping or a
3470 * private file mapping that will also be writable.
3471 * This has an additional check.
3472 */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003473 rc = avc_has_perm(&selinux_state,
3474 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003475 PROCESS__EXECMEM, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 if (rc)
David Howellsd84f4f92008-11-14 10:39:23 +11003477 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479
3480 if (file) {
3481 /* read access is always possible with a mapping */
3482 u32 av = FILE__READ;
3483
3484 /* write access only matters if the mapping is shared */
3485 if (shared && (prot & PROT_WRITE))
3486 av |= FILE__WRITE;
3487
3488 if (prot & PROT_EXEC)
3489 av |= FILE__EXECUTE;
3490
David Howells88e67f32008-11-14 10:39:21 +11003491 return file_has_perm(cred, file, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 }
David Howellsd84f4f92008-11-14 10:39:23 +11003493
3494error:
3495 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496}
3497
Al Viroe5467852012-05-30 13:30:51 -04003498static int selinux_mmap_addr(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07003500 int rc = 0;
Paul Moore98883bf2014-03-19 16:46:11 -04003501
3502 if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
3503 u32 sid = current_sid();
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003504 rc = avc_has_perm(&selinux_state,
3505 sid, sid, SECCLASS_MEMPROTECT,
Paul Moore98883bf2014-03-19 16:46:11 -04003506 MEMPROTECT__MMAP_ZERO, NULL);
3507 }
3508
3509 return rc;
Al Viroe5467852012-05-30 13:30:51 -04003510}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511
Al Viroe5467852012-05-30 13:30:51 -04003512static int selinux_mmap_file(struct file *file, unsigned long reqprot,
3513 unsigned long prot, unsigned long flags)
3514{
Stephen Smalley3ba4bf52017-05-05 09:14:48 -04003515 struct common_audit_data ad;
3516 int rc;
3517
3518 if (file) {
3519 ad.type = LSM_AUDIT_DATA_FILE;
3520 ad.u.file = file;
3521 rc = inode_has_perm(current_cred(), file_inode(file),
3522 FILE__MAP, &ad);
3523 if (rc)
3524 return rc;
3525 }
3526
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003527 if (selinux_state.checkreqprot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 prot = reqprot;
3529
3530 return file_map_prot_check(file, prot,
3531 (flags & MAP_TYPE) == MAP_SHARED);
3532}
3533
3534static int selinux_file_mprotect(struct vm_area_struct *vma,
3535 unsigned long reqprot,
3536 unsigned long prot)
3537{
David Howells88e67f32008-11-14 10:39:21 +11003538 const struct cred *cred = current_cred();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003539 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003541 if (selinux_state.checkreqprot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 prot = reqprot;
3543
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003544 if (default_noexec &&
3545 (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
James Morrisd541bbe2009-01-29 12:19:51 +11003546 int rc = 0;
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003547 if (vma->vm_start >= vma->vm_mm->start_brk &&
3548 vma->vm_end <= vma->vm_mm->brk) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003549 rc = avc_has_perm(&selinux_state,
3550 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003551 PROCESS__EXECHEAP, NULL);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003552 } else if (!vma->vm_file &&
Stephen Smalleyc2316db2016-04-08 13:55:03 -04003553 ((vma->vm_start <= vma->vm_mm->start_stack &&
3554 vma->vm_end >= vma->vm_mm->start_stack) ||
Andy Lutomirskid17af502016-09-30 10:58:58 -07003555 vma_is_stack_for_current(vma))) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003556 rc = avc_has_perm(&selinux_state,
3557 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003558 PROCESS__EXECSTACK, NULL);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003559 } else if (vma->vm_file && vma->anon_vma) {
3560 /*
3561 * We are making executable a file mapping that has
3562 * had some COW done. Since pages might have been
3563 * written, check ability to execute the possibly
3564 * modified content. This typically should only
3565 * occur for text relocations.
3566 */
David Howellsd84f4f92008-11-14 10:39:23 +11003567 rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003568 }
Lorenzo Hernandez García-Hierro6b992192005-06-25 14:54:34 -07003569 if (rc)
3570 return rc;
3571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572
3573 return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
3574}
3575
3576static int selinux_file_lock(struct file *file, unsigned int cmd)
3577{
David Howells88e67f32008-11-14 10:39:21 +11003578 const struct cred *cred = current_cred();
3579
3580 return file_has_perm(cred, file, FILE__LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581}
3582
3583static int selinux_file_fcntl(struct file *file, unsigned int cmd,
3584 unsigned long arg)
3585{
David Howells88e67f32008-11-14 10:39:21 +11003586 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 int err = 0;
3588
3589 switch (cmd) {
Eric Paris828dfe12008-04-17 13:17:49 -04003590 case F_SETFL:
Eric Paris828dfe12008-04-17 13:17:49 -04003591 if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
David Howells88e67f32008-11-14 10:39:21 +11003592 err = file_has_perm(cred, file, FILE__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003594 }
3595 /* fall through */
3596 case F_SETOWN:
3597 case F_SETSIG:
3598 case F_GETFL:
3599 case F_GETOWN:
3600 case F_GETSIG:
Cyrill Gorcunov1d151c32012-07-30 14:43:00 -07003601 case F_GETOWNER_UIDS:
Eric Paris828dfe12008-04-17 13:17:49 -04003602 /* Just check FD__USE permission */
David Howells88e67f32008-11-14 10:39:21 +11003603 err = file_has_perm(cred, file, 0);
Eric Paris828dfe12008-04-17 13:17:49 -04003604 break;
3605 case F_GETLK:
3606 case F_SETLK:
3607 case F_SETLKW:
Jeff Layton0d3f7a22014-04-22 08:23:58 -04003608 case F_OFD_GETLK:
3609 case F_OFD_SETLK:
3610 case F_OFD_SETLKW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611#if BITS_PER_LONG == 32
Eric Paris828dfe12008-04-17 13:17:49 -04003612 case F_GETLK64:
3613 case F_SETLK64:
3614 case F_SETLKW64:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615#endif
David Howells88e67f32008-11-14 10:39:21 +11003616 err = file_has_perm(cred, file, FILE__LOCK);
Eric Paris828dfe12008-04-17 13:17:49 -04003617 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 }
3619
3620 return err;
3621}
3622
Jeff Laytone0b93ed2014-08-22 11:27:32 -04003623static void selinux_file_set_fowner(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 struct file_security_struct *fsec;
3626
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 fsec = file->f_security;
David Howells275bb412008-11-14 10:39:19 +11003628 fsec->fown_sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629}
3630
3631static int selinux_file_send_sigiotask(struct task_struct *tsk,
3632 struct fown_struct *fown, int signum)
3633{
Eric Paris828dfe12008-04-17 13:17:49 -04003634 struct file *file;
Stephen Smalley65c90bc2009-05-04 15:43:18 -04003635 u32 sid = task_sid(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 struct file_security_struct *fsec;
3638
3639 /* struct fown_struct is never outside the context of a struct file */
Eric Paris828dfe12008-04-17 13:17:49 -04003640 file = container_of(fown, struct file, f_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 fsec = file->f_security;
3643
3644 if (!signum)
3645 perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
3646 else
3647 perm = signal_to_av(signum);
3648
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003649 return avc_has_perm(&selinux_state,
3650 fsec->fown_sid, sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 SECCLASS_PROCESS, perm, NULL);
3652}
3653
3654static int selinux_file_receive(struct file *file)
3655{
David Howells88e67f32008-11-14 10:39:21 +11003656 const struct cred *cred = current_cred();
3657
3658 return file_has_perm(cred, file, file_to_av(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659}
3660
Al Viro94817692018-07-10 14:13:18 -04003661static int selinux_file_open(struct file *file)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003662{
3663 struct file_security_struct *fsec;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003664 struct inode_security_struct *isec;
David Howellsd84f4f92008-11-14 10:39:23 +11003665
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003666 fsec = file->f_security;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05003667 isec = inode_security(file_inode(file));
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003668 /*
3669 * Save inode label and policy sequence number
3670 * at open-time so that selinux_file_permission
3671 * can determine whether revalidation is necessary.
3672 * Task label is already saved in the file security
3673 * struct as its SID.
3674 */
3675 fsec->isid = isec->sid;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003676 fsec->pseqno = avc_policy_seqno(&selinux_state);
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003677 /*
3678 * Since the inode label or policy seqno may have changed
3679 * between the selinux_inode_permission check and the saving
3680 * of state above, recheck that access is still permitted.
3681 * Otherwise, access might never be revalidated against the
3682 * new inode label or new policy.
3683 * This check is not redundant - do not remove.
3684 */
Al Viro94817692018-07-10 14:13:18 -04003685 return file_path_has_perm(file->f_cred, file, open_file_to_av(file));
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003686}
3687
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688/* task security operations */
3689
Tetsuo Handaa79be232017-03-28 23:08:45 +09003690static int selinux_task_alloc(struct task_struct *task,
3691 unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003693 u32 sid = current_sid();
3694
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003695 return avc_has_perm(&selinux_state,
3696 sid, sid, SECCLASS_PROCESS, PROCESS__FORK, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697}
3698
David Howellsf1752ee2008-11-14 10:39:17 +11003699/*
David Howellsee18d642009-09-02 09:14:21 +01003700 * allocate the SELinux part of blank credentials
3701 */
3702static int selinux_cred_alloc_blank(struct cred *cred, gfp_t gfp)
3703{
3704 struct task_security_struct *tsec;
3705
3706 tsec = kzalloc(sizeof(struct task_security_struct), gfp);
3707 if (!tsec)
3708 return -ENOMEM;
3709
3710 cred->security = tsec;
3711 return 0;
3712}
3713
3714/*
David Howellsf1752ee2008-11-14 10:39:17 +11003715 * detach and free the LSM part of a set of credentials
3716 */
3717static void selinux_cred_free(struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718{
David Howellsf1752ee2008-11-14 10:39:17 +11003719 struct task_security_struct *tsec = cred->security;
David Howellse0e81732009-09-02 09:13:40 +01003720
Tetsuo Handa2edeaa32011-02-07 13:36:10 +00003721 /*
3722 * cred->security == NULL if security_cred_alloc_blank() or
3723 * security_prepare_creds() returned an error.
3724 */
3725 BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE);
David Howellse0e81732009-09-02 09:13:40 +01003726 cred->security = (void *) 0x7UL;
David Howellsf1752ee2008-11-14 10:39:17 +11003727 kfree(tsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728}
3729
David Howellsd84f4f92008-11-14 10:39:23 +11003730/*
3731 * prepare a new set of credentials for modification
3732 */
3733static int selinux_cred_prepare(struct cred *new, const struct cred *old,
3734 gfp_t gfp)
3735{
3736 const struct task_security_struct *old_tsec;
3737 struct task_security_struct *tsec;
3738
3739 old_tsec = old->security;
3740
3741 tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp);
3742 if (!tsec)
3743 return -ENOMEM;
3744
3745 new->security = tsec;
3746 return 0;
3747}
3748
3749/*
David Howellsee18d642009-09-02 09:14:21 +01003750 * transfer the SELinux data to a blank set of creds
3751 */
3752static void selinux_cred_transfer(struct cred *new, const struct cred *old)
3753{
3754 const struct task_security_struct *old_tsec = old->security;
3755 struct task_security_struct *tsec = new->security;
3756
3757 *tsec = *old_tsec;
3758}
3759
Matthew Garrett3ec30112018-01-08 13:36:19 -08003760static void selinux_cred_getsecid(const struct cred *c, u32 *secid)
3761{
3762 *secid = cred_sid(c);
3763}
3764
David Howellsee18d642009-09-02 09:14:21 +01003765/*
David Howells3a3b7ce2008-11-14 10:39:28 +11003766 * set the security data for a kernel service
3767 * - all the creation contexts are set to unlabelled
3768 */
3769static int selinux_kernel_act_as(struct cred *new, u32 secid)
3770{
3771 struct task_security_struct *tsec = new->security;
3772 u32 sid = current_sid();
3773 int ret;
3774
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003775 ret = avc_has_perm(&selinux_state,
3776 sid, secid,
David Howells3a3b7ce2008-11-14 10:39:28 +11003777 SECCLASS_KERNEL_SERVICE,
3778 KERNEL_SERVICE__USE_AS_OVERRIDE,
3779 NULL);
3780 if (ret == 0) {
3781 tsec->sid = secid;
3782 tsec->create_sid = 0;
3783 tsec->keycreate_sid = 0;
3784 tsec->sockcreate_sid = 0;
3785 }
3786 return ret;
3787}
3788
3789/*
3790 * set the file creation context in a security record to the same as the
3791 * objective context of the specified inode
3792 */
3793static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
3794{
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05003795 struct inode_security_struct *isec = inode_security(inode);
David Howells3a3b7ce2008-11-14 10:39:28 +11003796 struct task_security_struct *tsec = new->security;
3797 u32 sid = current_sid();
3798 int ret;
3799
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003800 ret = avc_has_perm(&selinux_state,
3801 sid, isec->sid,
David Howells3a3b7ce2008-11-14 10:39:28 +11003802 SECCLASS_KERNEL_SERVICE,
3803 KERNEL_SERVICE__CREATE_FILES_AS,
3804 NULL);
3805
3806 if (ret == 0)
3807 tsec->create_sid = isec->sid;
David Howellsef574712010-02-26 01:56:16 +00003808 return ret;
David Howells3a3b7ce2008-11-14 10:39:28 +11003809}
3810
Eric Parisdd8dbf22009-11-03 16:35:32 +11003811static int selinux_kernel_module_request(char *kmod_name)
Eric Paris25354c42009-08-13 09:45:03 -04003812{
Eric Parisdd8dbf22009-11-03 16:35:32 +11003813 struct common_audit_data ad;
3814
Eric Paris50c205f2012-04-04 15:01:43 -04003815 ad.type = LSM_AUDIT_DATA_KMOD;
Eric Parisdd8dbf22009-11-03 16:35:32 +11003816 ad.u.kmod_name = kmod_name;
3817
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003818 return avc_has_perm(&selinux_state,
3819 current_sid(), SECINITSID_KERNEL, SECCLASS_SYSTEM,
Eric Parisdd8dbf22009-11-03 16:35:32 +11003820 SYSTEM__MODULE_REQUEST, &ad);
Eric Paris25354c42009-08-13 09:45:03 -04003821}
3822
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003823static int selinux_kernel_module_from_file(struct file *file)
3824{
3825 struct common_audit_data ad;
3826 struct inode_security_struct *isec;
3827 struct file_security_struct *fsec;
3828 u32 sid = current_sid();
3829 int rc;
3830
3831 /* init_module */
3832 if (file == NULL)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003833 return avc_has_perm(&selinux_state,
3834 sid, sid, SECCLASS_SYSTEM,
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003835 SYSTEM__MODULE_LOAD, NULL);
3836
3837 /* finit_module */
Paul Moore20cdef82016-04-04 14:14:42 -04003838
Vivek Goyal43af5de2016-09-09 11:37:49 -04003839 ad.type = LSM_AUDIT_DATA_FILE;
3840 ad.u.file = file;
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003841
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003842 fsec = file->f_security;
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003843 if (sid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003844 rc = avc_has_perm(&selinux_state,
3845 sid, fsec->sid, SECCLASS_FD, FD__USE, &ad);
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003846 if (rc)
3847 return rc;
3848 }
3849
Paul Moore20cdef82016-04-04 14:14:42 -04003850 isec = inode_security(file_inode(file));
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003851 return avc_has_perm(&selinux_state,
3852 sid, isec->sid, SECCLASS_SYSTEM,
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003853 SYSTEM__MODULE_LOAD, &ad);
3854}
3855
3856static int selinux_kernel_read_file(struct file *file,
3857 enum kernel_read_file_id id)
3858{
3859 int rc = 0;
3860
3861 switch (id) {
3862 case READING_MODULE:
3863 rc = selinux_kernel_module_from_file(file);
3864 break;
3865 default:
3866 break;
3867 }
3868
3869 return rc;
3870}
3871
Mimi Zoharc77b8cd2018-07-13 14:06:02 -04003872static int selinux_kernel_load_data(enum kernel_load_data_id id)
3873{
3874 int rc = 0;
3875
3876 switch (id) {
3877 case LOADING_MODULE:
3878 rc = selinux_kernel_module_from_file(NULL);
3879 default:
3880 break;
3881 }
3882
3883 return rc;
3884}
3885
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
3887{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003888 return avc_has_perm(&selinux_state,
3889 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003890 PROCESS__SETPGID, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891}
3892
3893static int selinux_task_getpgid(struct task_struct *p)
3894{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003895 return avc_has_perm(&selinux_state,
3896 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003897 PROCESS__GETPGID, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898}
3899
3900static int selinux_task_getsid(struct task_struct *p)
3901{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003902 return avc_has_perm(&selinux_state,
3903 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003904 PROCESS__GETSESSION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905}
3906
David Quigleyf9008e4c2006-06-30 01:55:46 -07003907static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
3908{
David Howells275bb412008-11-14 10:39:19 +11003909 *secid = task_sid(p);
David Quigleyf9008e4c2006-06-30 01:55:46 -07003910}
3911
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912static int selinux_task_setnice(struct task_struct *p, int nice)
3913{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003914 return avc_has_perm(&selinux_state,
3915 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003916 PROCESS__SETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917}
3918
James Morris03e68062006-06-23 02:03:58 -07003919static int selinux_task_setioprio(struct task_struct *p, int ioprio)
3920{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003921 return avc_has_perm(&selinux_state,
3922 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003923 PROCESS__SETSCHED, NULL);
James Morris03e68062006-06-23 02:03:58 -07003924}
3925
David Quigleya1836a42006-06-30 01:55:49 -07003926static int selinux_task_getioprio(struct task_struct *p)
3927{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003928 return avc_has_perm(&selinux_state,
3929 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003930 PROCESS__GETSCHED, NULL);
David Quigleya1836a42006-06-30 01:55:49 -07003931}
3932
Corentin LABBE42985552017-10-04 20:32:18 +02003933static int selinux_task_prlimit(const struct cred *cred, const struct cred *tcred,
3934 unsigned int flags)
Stephen Smalley791ec492017-02-17 07:57:00 -05003935{
3936 u32 av = 0;
3937
Stephen Smalley84e68852017-02-28 09:35:08 -05003938 if (!flags)
3939 return 0;
Stephen Smalley791ec492017-02-17 07:57:00 -05003940 if (flags & LSM_PRLIMIT_WRITE)
3941 av |= PROCESS__SETRLIMIT;
3942 if (flags & LSM_PRLIMIT_READ)
3943 av |= PROCESS__GETRLIMIT;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003944 return avc_has_perm(&selinux_state,
3945 cred_sid(cred), cred_sid(tcred),
Stephen Smalley791ec492017-02-17 07:57:00 -05003946 SECCLASS_PROCESS, av, NULL);
3947}
3948
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003949static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
3950 struct rlimit *new_rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951{
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003952 struct rlimit *old_rlim = p->signal->rlim + resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953
3954 /* Control the ability to change the hard limit (whether
3955 lowering or raising it), so that the hard limit can
3956 later be used as a safe reset point for the soft limit
David Howellsd84f4f92008-11-14 10:39:23 +11003957 upon context transitions. See selinux_bprm_committing_creds. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 if (old_rlim->rlim_max != new_rlim->rlim_max)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003959 return avc_has_perm(&selinux_state,
3960 current_sid(), task_sid(p),
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003961 SECCLASS_PROCESS, PROCESS__SETRLIMIT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962
3963 return 0;
3964}
3965
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09003966static int selinux_task_setscheduler(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003968 return avc_has_perm(&selinux_state,
3969 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003970 PROCESS__SETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971}
3972
3973static int selinux_task_getscheduler(struct task_struct *p)
3974{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003975 return avc_has_perm(&selinux_state,
3976 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003977 PROCESS__GETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978}
3979
David Quigley35601542006-06-23 02:04:01 -07003980static int selinux_task_movememory(struct task_struct *p)
3981{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003982 return avc_has_perm(&selinux_state,
3983 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003984 PROCESS__SETSCHED, NULL);
David Quigley35601542006-06-23 02:04:01 -07003985}
3986
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02003987static int selinux_task_kill(struct task_struct *p, struct kernel_siginfo *info,
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04003988 int sig, const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989{
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04003990 u32 secid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 if (!sig)
3994 perm = PROCESS__SIGNULL; /* null signal; existence test */
3995 else
3996 perm = signal_to_av(sig);
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04003997 if (!cred)
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003998 secid = current_sid();
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04003999 else
4000 secid = cred_sid(cred);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004001 return avc_has_perm(&selinux_state,
4002 secid, task_sid(p), SECCLASS_PROCESS, perm, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003}
4004
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005static void selinux_task_to_inode(struct task_struct *p,
4006 struct inode *inode)
4007{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 struct inode_security_struct *isec = inode->i_security;
David Howells275bb412008-11-14 10:39:19 +11004009 u32 sid = task_sid(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004011 spin_lock(&isec->lock);
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01004012 isec->sclass = inode_mode_to_security_class(inode->i_mode);
David Howells275bb412008-11-14 10:39:19 +11004013 isec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004014 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004015 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016}
4017
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004019static int selinux_parse_skb_ipv4(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04004020 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021{
4022 int offset, ihlen, ret = -EINVAL;
4023 struct iphdr _iph, *ih;
4024
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03004025 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
4027 if (ih == NULL)
4028 goto out;
4029
4030 ihlen = ih->ihl * 4;
4031 if (ihlen < sizeof(_iph))
4032 goto out;
4033
Eric Paris48c62af2012-04-02 13:15:44 -04004034 ad->u.net->v4info.saddr = ih->saddr;
4035 ad->u.net->v4info.daddr = ih->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 ret = 0;
4037
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004038 if (proto)
4039 *proto = ih->protocol;
4040
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041 switch (ih->protocol) {
Eric Paris828dfe12008-04-17 13:17:49 -04004042 case IPPROTO_TCP: {
4043 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044
Eric Paris828dfe12008-04-17 13:17:49 -04004045 if (ntohs(ih->frag_off) & IP_OFFSET)
4046 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047
4048 offset += ihlen;
4049 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4050 if (th == NULL)
4051 break;
4052
Eric Paris48c62af2012-04-02 13:15:44 -04004053 ad->u.net->sport = th->source;
4054 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057
Eric Paris828dfe12008-04-17 13:17:49 -04004058 case IPPROTO_UDP: {
4059 struct udphdr _udph, *uh;
4060
4061 if (ntohs(ih->frag_off) & IP_OFFSET)
4062 break;
4063
4064 offset += ihlen;
4065 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4066 if (uh == NULL)
4067 break;
4068
Eric Paris48c62af2012-04-02 13:15:44 -04004069 ad->u.net->sport = uh->source;
4070 ad->u.net->dport = uh->dest;
Eric Paris828dfe12008-04-17 13:17:49 -04004071 break;
4072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073
James Morris2ee92d42006-11-13 16:09:01 -08004074 case IPPROTO_DCCP: {
4075 struct dccp_hdr _dccph, *dh;
4076
4077 if (ntohs(ih->frag_off) & IP_OFFSET)
4078 break;
4079
4080 offset += ihlen;
4081 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4082 if (dh == NULL)
4083 break;
4084
Eric Paris48c62af2012-04-02 13:15:44 -04004085 ad->u.net->sport = dh->dccph_sport;
4086 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08004087 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004088 }
James Morris2ee92d42006-11-13 16:09:01 -08004089
Richard Hainesd4529302018-02-13 20:57:18 +00004090#if IS_ENABLED(CONFIG_IP_SCTP)
4091 case IPPROTO_SCTP: {
4092 struct sctphdr _sctph, *sh;
4093
4094 if (ntohs(ih->frag_off) & IP_OFFSET)
4095 break;
4096
4097 offset += ihlen;
4098 sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4099 if (sh == NULL)
4100 break;
4101
4102 ad->u.net->sport = sh->source;
4103 ad->u.net->dport = sh->dest;
4104 break;
4105 }
4106#endif
Eric Paris828dfe12008-04-17 13:17:49 -04004107 default:
4108 break;
4109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110out:
4111 return ret;
4112}
4113
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04004114#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115
4116/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004117static int selinux_parse_skb_ipv6(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04004118 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119{
4120 u8 nexthdr;
4121 int ret = -EINVAL, offset;
4122 struct ipv6hdr _ipv6h, *ip6;
Jesse Gross75f28112011-11-30 17:05:51 -08004123 __be16 frag_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03004125 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
4127 if (ip6 == NULL)
4128 goto out;
4129
Eric Paris48c62af2012-04-02 13:15:44 -04004130 ad->u.net->v6info.saddr = ip6->saddr;
4131 ad->u.net->v6info.daddr = ip6->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 ret = 0;
4133
4134 nexthdr = ip6->nexthdr;
4135 offset += sizeof(_ipv6h);
Jesse Gross75f28112011-11-30 17:05:51 -08004136 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 if (offset < 0)
4138 goto out;
4139
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004140 if (proto)
4141 *proto = nexthdr;
4142
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 switch (nexthdr) {
4144 case IPPROTO_TCP: {
Eric Paris828dfe12008-04-17 13:17:49 -04004145 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146
4147 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4148 if (th == NULL)
4149 break;
4150
Eric Paris48c62af2012-04-02 13:15:44 -04004151 ad->u.net->sport = th->source;
4152 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 break;
4154 }
4155
4156 case IPPROTO_UDP: {
4157 struct udphdr _udph, *uh;
4158
4159 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4160 if (uh == NULL)
4161 break;
4162
Eric Paris48c62af2012-04-02 13:15:44 -04004163 ad->u.net->sport = uh->source;
4164 ad->u.net->dport = uh->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 break;
4166 }
4167
James Morris2ee92d42006-11-13 16:09:01 -08004168 case IPPROTO_DCCP: {
4169 struct dccp_hdr _dccph, *dh;
4170
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 sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4185 if (sh == NULL)
4186 break;
4187
4188 ad->u.net->sport = sh->source;
4189 ad->u.net->dport = sh->dest;
4190 break;
4191 }
4192#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 /* includes fragments */
4194 default:
4195 break;
4196 }
4197out:
4198 return ret;
4199}
4200
4201#endif /* IPV6 */
4202
Thomas Liu2bf49692009-07-14 12:14:09 -04004203static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
David Howellscf9481e2008-07-27 21:31:07 +10004204 char **_addrp, int src, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205{
David Howellscf9481e2008-07-27 21:31:07 +10004206 char *addrp;
4207 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208
Eric Paris48c62af2012-04-02 13:15:44 -04004209 switch (ad->u.net->family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 case PF_INET:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004211 ret = selinux_parse_skb_ipv4(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10004212 if (ret)
4213 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04004214 addrp = (char *)(src ? &ad->u.net->v4info.saddr :
4215 &ad->u.net->v4info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10004216 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04004218#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 case PF_INET6:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004220 ret = selinux_parse_skb_ipv6(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10004221 if (ret)
4222 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04004223 addrp = (char *)(src ? &ad->u.net->v6info.saddr :
4224 &ad->u.net->v6info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10004225 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226#endif /* IPV6 */
4227 default:
David Howellscf9481e2008-07-27 21:31:07 +10004228 addrp = NULL;
4229 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 }
4231
David Howellscf9481e2008-07-27 21:31:07 +10004232parse_error:
peter enderborgc103a912018-06-12 10:09:03 +02004233 pr_warn(
David Howellscf9481e2008-07-27 21:31:07 +10004234 "SELinux: failure in selinux_parse_skb(),"
4235 " unable to parse packet\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 return ret;
David Howellscf9481e2008-07-27 21:31:07 +10004237
4238okay:
4239 if (_addrp)
4240 *_addrp = addrp;
4241 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242}
4243
Paul Moore4f6a9932007-03-01 14:35:22 -05004244/**
Paul Moore220deb92008-01-29 08:38:23 -05004245 * selinux_skb_peerlbl_sid - Determine the peer label of a packet
Paul Moore4f6a9932007-03-01 14:35:22 -05004246 * @skb: the packet
Paul Moore75e22912008-01-29 08:38:04 -05004247 * @family: protocol family
Paul Moore220deb92008-01-29 08:38:23 -05004248 * @sid: the packet's peer label SID
Paul Moore4f6a9932007-03-01 14:35:22 -05004249 *
4250 * Description:
Paul Moore220deb92008-01-29 08:38:23 -05004251 * Check the various different forms of network peer labeling and determine
4252 * the peer label/SID for the packet; most of the magic actually occurs in
4253 * the security server function security_net_peersid_cmp(). The function
4254 * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
4255 * or -EACCES if @sid is invalid due to inconsistencies with the different
4256 * peer labels.
Paul Moore4f6a9932007-03-01 14:35:22 -05004257 *
4258 */
Paul Moore220deb92008-01-29 08:38:23 -05004259static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
Paul Moore4f6a9932007-03-01 14:35:22 -05004260{
Paul Moore71f1cb02008-01-29 08:51:16 -05004261 int err;
Paul Moore4f6a9932007-03-01 14:35:22 -05004262 u32 xfrm_sid;
4263 u32 nlbl_sid;
Paul Moore220deb92008-01-29 08:38:23 -05004264 u32 nlbl_type;
Paul Moore4f6a9932007-03-01 14:35:22 -05004265
Paul Moore817eff72013-12-10 14:57:54 -05004266 err = selinux_xfrm_skb_sid(skb, &xfrm_sid);
Paul Moorebed4d7e2013-07-23 17:38:40 -04004267 if (unlikely(err))
4268 return -EACCES;
4269 err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
4270 if (unlikely(err))
4271 return -EACCES;
Paul Moore220deb92008-01-29 08:38:23 -05004272
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004273 err = security_net_peersid_resolve(&selinux_state, nlbl_sid,
4274 nlbl_type, xfrm_sid, sid);
Paul Moore71f1cb02008-01-29 08:51:16 -05004275 if (unlikely(err)) {
peter enderborgc103a912018-06-12 10:09:03 +02004276 pr_warn(
Paul Moore71f1cb02008-01-29 08:51:16 -05004277 "SELinux: failure in selinux_skb_peerlbl_sid(),"
4278 " unable to determine packet's peer label\n");
Paul Moore220deb92008-01-29 08:38:23 -05004279 return -EACCES;
Paul Moore71f1cb02008-01-29 08:51:16 -05004280 }
Paul Moore220deb92008-01-29 08:38:23 -05004281
4282 return 0;
Paul Moore4f6a9932007-03-01 14:35:22 -05004283}
4284
Paul Moore446b8022013-12-04 16:10:51 -05004285/**
4286 * selinux_conn_sid - Determine the child socket label for a connection
4287 * @sk_sid: the parent socket's SID
4288 * @skb_sid: the packet's SID
4289 * @conn_sid: the resulting connection SID
4290 *
4291 * If @skb_sid is valid then the user:role:type information from @sk_sid is
4292 * combined with the MLS information from @skb_sid in order to create
4293 * @conn_sid. If @skb_sid is not valid then then @conn_sid is simply a copy
4294 * of @sk_sid. Returns zero on success, negative values on failure.
4295 *
4296 */
4297static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid)
4298{
4299 int err = 0;
4300
4301 if (skb_sid != SECSID_NULL)
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004302 err = security_sid_mls_copy(&selinux_state, sk_sid, skb_sid,
4303 conn_sid);
Paul Moore446b8022013-12-04 16:10:51 -05004304 else
4305 *conn_sid = sk_sid;
4306
4307 return err;
4308}
4309
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310/* socket security operations */
Paul Moored4f2d972010-04-22 14:46:18 -04004311
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004312static int socket_sockcreate_sid(const struct task_security_struct *tsec,
4313 u16 secclass, u32 *socksid)
Paul Moored4f2d972010-04-22 14:46:18 -04004314{
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004315 if (tsec->sockcreate_sid > SECSID_NULL) {
4316 *socksid = tsec->sockcreate_sid;
4317 return 0;
4318 }
4319
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004320 return security_transition_sid(&selinux_state, tsec->sid, tsec->sid,
4321 secclass, NULL, socksid);
Paul Moored4f2d972010-04-22 14:46:18 -04004322}
4323
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004324static int sock_has_perm(struct sock *sk, u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325{
Paul Moore253bfae2010-04-22 14:46:19 -04004326 struct sk_security_struct *sksec = sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004327 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004328 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329
Paul Moore253bfae2010-04-22 14:46:19 -04004330 if (sksec->sid == SECINITSID_KERNEL)
4331 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332
Eric Paris50c205f2012-04-04 15:01:43 -04004333 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004334 ad.u.net = &net;
4335 ad.u.net->sk = sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004337 return avc_has_perm(&selinux_state,
4338 current_sid(), sksec->sid, sksec->sclass, perms,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004339 &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340}
4341
4342static int selinux_socket_create(int family, int type,
4343 int protocol, int kern)
4344{
Paul Moore5fb49872010-04-22 14:46:19 -04004345 const struct task_security_struct *tsec = current_security();
Paul Moored4f2d972010-04-22 14:46:18 -04004346 u32 newsid;
David Howells275bb412008-11-14 10:39:19 +11004347 u16 secclass;
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004348 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349
4350 if (kern)
Paul Moored4f2d972010-04-22 14:46:18 -04004351 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352
David Howells275bb412008-11-14 10:39:19 +11004353 secclass = socket_type_to_security_class(family, type, protocol);
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004354 rc = socket_sockcreate_sid(tsec, secclass, &newsid);
4355 if (rc)
4356 return rc;
4357
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004358 return avc_has_perm(&selinux_state,
4359 tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360}
4361
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07004362static int selinux_socket_post_create(struct socket *sock, int family,
4363 int type, int protocol, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364{
Paul Moore5fb49872010-04-22 14:46:19 -04004365 const struct task_security_struct *tsec = current_security();
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05004366 struct inode_security_struct *isec = inode_security_novalidate(SOCK_INODE(sock));
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004367 struct sk_security_struct *sksec;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004368 u16 sclass = socket_type_to_security_class(family, type, protocol);
4369 u32 sid = SECINITSID_KERNEL;
David Howells275bb412008-11-14 10:39:19 +11004370 int err = 0;
4371
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004372 if (!kern) {
4373 err = socket_sockcreate_sid(tsec, sclass, &sid);
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004374 if (err)
4375 return err;
4376 }
David Howells275bb412008-11-14 10:39:19 +11004377
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004378 isec->sclass = sclass;
4379 isec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004380 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004382 if (sock->sk) {
4383 sksec = sock->sk->sk_security;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004384 sksec->sclass = sclass;
4385 sksec->sid = sid;
Richard Hainesd4529302018-02-13 20:57:18 +00004386 /* Allows detection of the first association on this socket */
4387 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4388 sksec->sctp_assoc_state = SCTP_ASSOC_UNSET;
4389
Paul Moore389fb8002009-03-27 17:10:34 -04004390 err = selinux_netlbl_socket_post_create(sock->sk, family);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004391 }
4392
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07004393 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394}
4395
David Herrmann0b811db2018-05-04 16:28:21 +02004396static int selinux_socket_socketpair(struct socket *socka,
4397 struct socket *sockb)
4398{
4399 struct sk_security_struct *sksec_a = socka->sk->sk_security;
4400 struct sk_security_struct *sksec_b = sockb->sk->sk_security;
4401
4402 sksec_a->peer_sid = sksec_b->sid;
4403 sksec_b->peer_sid = sksec_a->sid;
4404
4405 return 0;
4406}
4407
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408/* Range of port numbers used to automatically bind.
4409 Need to determine whether we should perform a name_bind
4410 permission check between the socket and the port number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411
4412static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
4413{
Paul Moore253bfae2010-04-22 14:46:19 -04004414 struct sock *sk = sock->sk;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004415 struct sk_security_struct *sksec = sk->sk_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 u16 family;
4417 int err;
4418
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004419 err = sock_has_perm(sk, SOCKET__BIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 if (err)
4421 goto out;
4422
Richard Hainesd4529302018-02-13 20:57:18 +00004423 /* If PF_INET or PF_INET6, check name_bind permission for the port. */
Paul Moore253bfae2010-04-22 14:46:19 -04004424 family = sk->sk_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 if (family == PF_INET || family == PF_INET6) {
4426 char *addrp;
Thomas Liu2bf49692009-07-14 12:14:09 -04004427 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004428 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 struct sockaddr_in *addr4 = NULL;
4430 struct sockaddr_in6 *addr6 = NULL;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004431 u16 family_sa = address->sa_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 unsigned short snum;
James Morrise399f982008-06-12 01:39:58 +10004433 u32 sid, node_perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434
Richard Hainesd4529302018-02-13 20:57:18 +00004435 /*
4436 * sctp_bindx(3) calls via selinux_sctp_bind_connect()
4437 * that validates multiple binding addresses. Because of this
4438 * need to check address->sa_family as it is possible to have
4439 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4440 */
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004441 switch (family_sa) {
4442 case AF_UNSPEC:
Richard Haines68741a8a2018-03-02 19:54:34 +00004443 case AF_INET:
4444 if (addrlen < sizeof(struct sockaddr_in))
4445 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446 addr4 = (struct sockaddr_in *)address;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004447 if (family_sa == AF_UNSPEC) {
4448 /* see __inet_bind(), we only want to allow
4449 * AF_UNSPEC if the address is INADDR_ANY
4450 */
4451 if (addr4->sin_addr.s_addr != htonl(INADDR_ANY))
4452 goto err_af;
4453 family_sa = AF_INET;
4454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 snum = ntohs(addr4->sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456 addrp = (char *)&addr4->sin_addr.s_addr;
Richard Haines68741a8a2018-03-02 19:54:34 +00004457 break;
4458 case AF_INET6:
4459 if (addrlen < SIN6_LEN_RFC2133)
4460 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 addr6 = (struct sockaddr_in6 *)address;
4462 snum = ntohs(addr6->sin6_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 addrp = (char *)&addr6->sin6_addr.s6_addr;
Richard Haines68741a8a2018-03-02 19:54:34 +00004464 break;
4465 default:
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004466 goto err_af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467 }
4468
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004469 ad.type = LSM_AUDIT_DATA_NET;
4470 ad.u.net = &net;
4471 ad.u.net->sport = htons(snum);
4472 ad.u.net->family = family_sa;
4473
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004474 if (snum) {
4475 int low, high;
4476
Eric W. Biederman0bbf87d2013-09-28 14:10:59 -07004477 inet_get_local_port_range(sock_net(sk), &low, &high);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004478
Krister Johansen4548b682017-01-20 17:49:11 -08004479 if (snum < max(inet_prot_sock(sock_net(sk)), low) ||
4480 snum > high) {
Paul Moore3e1121722008-04-10 10:48:14 -04004481 err = sel_netport_sid(sk->sk_protocol,
4482 snum, &sid);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004483 if (err)
4484 goto out;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004485 err = avc_has_perm(&selinux_state,
4486 sksec->sid, sid,
Paul Moore253bfae2010-04-22 14:46:19 -04004487 sksec->sclass,
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004488 SOCKET__NAME_BIND, &ad);
4489 if (err)
4490 goto out;
4491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 }
Eric Paris828dfe12008-04-17 13:17:49 -04004493
Paul Moore253bfae2010-04-22 14:46:19 -04004494 switch (sksec->sclass) {
James Morris13402582005-09-30 14:24:34 -04004495 case SECCLASS_TCP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 node_perm = TCP_SOCKET__NODE_BIND;
4497 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004498
James Morris13402582005-09-30 14:24:34 -04004499 case SECCLASS_UDP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 node_perm = UDP_SOCKET__NODE_BIND;
4501 break;
James Morris2ee92d42006-11-13 16:09:01 -08004502
4503 case SECCLASS_DCCP_SOCKET:
4504 node_perm = DCCP_SOCKET__NODE_BIND;
4505 break;
4506
Richard Hainesd4529302018-02-13 20:57:18 +00004507 case SECCLASS_SCTP_SOCKET:
4508 node_perm = SCTP_SOCKET__NODE_BIND;
4509 break;
4510
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 default:
4512 node_perm = RAWIP_SOCKET__NODE_BIND;
4513 break;
4514 }
Eric Paris828dfe12008-04-17 13:17:49 -04004515
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004516 err = sel_netnode_sid(addrp, family_sa, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517 if (err)
4518 goto out;
Eric Paris828dfe12008-04-17 13:17:49 -04004519
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004520 if (family_sa == AF_INET)
Eric Paris48c62af2012-04-02 13:15:44 -04004521 ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522 else
Eric Paris48c62af2012-04-02 13:15:44 -04004523 ad.u.net->v6info.saddr = addr6->sin6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004525 err = avc_has_perm(&selinux_state,
4526 sksec->sid, sid,
Paul Moore253bfae2010-04-22 14:46:19 -04004527 sksec->sclass, node_perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 if (err)
4529 goto out;
4530 }
4531out:
4532 return err;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004533err_af:
4534 /* Note that SCTP services expect -EINVAL, others -EAFNOSUPPORT. */
4535 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4536 return -EINVAL;
4537 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538}
4539
Richard Hainesd4529302018-02-13 20:57:18 +00004540/* This supports connect(2) and SCTP connect services such as sctp_connectx(3)
Mauro Carvalho Chehab5fb94e92018-05-08 15:14:57 -03004541 * and sctp_sendmsg(3) as described in Documentation/security/LSM-sctp.rst
Richard Hainesd4529302018-02-13 20:57:18 +00004542 */
4543static int selinux_socket_connect_helper(struct socket *sock,
4544 struct sockaddr *address, int addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545{
Paul Moore014ab192008-10-10 10:16:33 -04004546 struct sock *sk = sock->sk;
Paul Moore253bfae2010-04-22 14:46:19 -04004547 struct sk_security_struct *sksec = sk->sk_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548 int err;
4549
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004550 err = sock_has_perm(sk, SOCKET__CONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 if (err)
4552 return err;
4553
4554 /*
Richard Hainesd4529302018-02-13 20:57:18 +00004555 * If a TCP, DCCP or SCTP socket, check name_connect permission
4556 * for the port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 */
Paul Moore253bfae2010-04-22 14:46:19 -04004558 if (sksec->sclass == SECCLASS_TCP_SOCKET ||
Richard Hainesd4529302018-02-13 20:57:18 +00004559 sksec->sclass == SECCLASS_DCCP_SOCKET ||
4560 sksec->sclass == SECCLASS_SCTP_SOCKET) {
Thomas Liu2bf49692009-07-14 12:14:09 -04004561 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004562 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 struct sockaddr_in *addr4 = NULL;
4564 struct sockaddr_in6 *addr6 = NULL;
4565 unsigned short snum;
James Morris2ee92d42006-11-13 16:09:01 -08004566 u32 sid, perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567
Richard Hainesd4529302018-02-13 20:57:18 +00004568 /* sctp_connectx(3) calls via selinux_sctp_bind_connect()
4569 * that validates multiple connect addresses. Because of this
4570 * need to check address->sa_family as it is possible to have
4571 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4572 */
Richard Haines68741a8a2018-03-02 19:54:34 +00004573 switch (address->sa_family) {
4574 case AF_INET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 addr4 = (struct sockaddr_in *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004576 if (addrlen < sizeof(struct sockaddr_in))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 return -EINVAL;
4578 snum = ntohs(addr4->sin_port);
Richard Haines68741a8a2018-03-02 19:54:34 +00004579 break;
4580 case AF_INET6:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 addr6 = (struct sockaddr_in6 *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004582 if (addrlen < SIN6_LEN_RFC2133)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 return -EINVAL;
4584 snum = ntohs(addr6->sin6_port);
Richard Haines68741a8a2018-03-02 19:54:34 +00004585 break;
4586 default:
4587 /* Note that SCTP services expect -EINVAL, whereas
4588 * others expect -EAFNOSUPPORT.
4589 */
4590 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4591 return -EINVAL;
4592 else
4593 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594 }
4595
Paul Moore3e1121722008-04-10 10:48:14 -04004596 err = sel_netport_sid(sk->sk_protocol, snum, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 if (err)
Richard Hainesd4529302018-02-13 20:57:18 +00004598 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599
Richard Hainesd4529302018-02-13 20:57:18 +00004600 switch (sksec->sclass) {
4601 case SECCLASS_TCP_SOCKET:
4602 perm = TCP_SOCKET__NAME_CONNECT;
4603 break;
4604 case SECCLASS_DCCP_SOCKET:
4605 perm = DCCP_SOCKET__NAME_CONNECT;
4606 break;
4607 case SECCLASS_SCTP_SOCKET:
4608 perm = SCTP_SOCKET__NAME_CONNECT;
4609 break;
4610 }
James Morris2ee92d42006-11-13 16:09:01 -08004611
Eric Paris50c205f2012-04-04 15:01:43 -04004612 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004613 ad.u.net = &net;
4614 ad.u.net->dport = htons(snum);
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004615 ad.u.net->family = address->sa_family;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004616 err = avc_has_perm(&selinux_state,
4617 sksec->sid, sid, sksec->sclass, perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 if (err)
Richard Hainesd4529302018-02-13 20:57:18 +00004619 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 }
4621
Richard Hainesd4529302018-02-13 20:57:18 +00004622 return 0;
4623}
Paul Moore014ab192008-10-10 10:16:33 -04004624
Richard Hainesd4529302018-02-13 20:57:18 +00004625/* Supports connect(2), see comments in selinux_socket_connect_helper() */
4626static int selinux_socket_connect(struct socket *sock,
4627 struct sockaddr *address, int addrlen)
4628{
4629 int err;
4630 struct sock *sk = sock->sk;
4631
4632 err = selinux_socket_connect_helper(sock, address, addrlen);
4633 if (err)
4634 return err;
4635
4636 return selinux_netlbl_socket_connect(sk, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637}
4638
4639static int selinux_socket_listen(struct socket *sock, int backlog)
4640{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004641 return sock_has_perm(sock->sk, SOCKET__LISTEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642}
4643
4644static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
4645{
4646 int err;
4647 struct inode_security_struct *isec;
4648 struct inode_security_struct *newisec;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004649 u16 sclass;
4650 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004652 err = sock_has_perm(sock->sk, SOCKET__ACCEPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 if (err)
4654 return err;
4655
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05004656 isec = inode_security_novalidate(SOCK_INODE(sock));
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004657 spin_lock(&isec->lock);
4658 sclass = isec->sclass;
4659 sid = isec->sid;
4660 spin_unlock(&isec->lock);
4661
4662 newisec = inode_security_novalidate(SOCK_INODE(newsock));
4663 newisec->sclass = sclass;
4664 newisec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004665 newisec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666
4667 return 0;
4668}
4669
4670static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
Eric Paris828dfe12008-04-17 13:17:49 -04004671 int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004673 return sock_has_perm(sock->sk, SOCKET__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674}
4675
4676static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
4677 int size, int flags)
4678{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004679 return sock_has_perm(sock->sk, SOCKET__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680}
4681
4682static int selinux_socket_getsockname(struct socket *sock)
4683{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004684 return sock_has_perm(sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685}
4686
4687static int selinux_socket_getpeername(struct socket *sock)
4688{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004689 return sock_has_perm(sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690}
4691
Eric Paris828dfe12008-04-17 13:17:49 -04004692static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693{
Paul Mooref8687af2006-10-30 15:22:15 -08004694 int err;
4695
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004696 err = sock_has_perm(sock->sk, SOCKET__SETOPT);
Paul Mooref8687af2006-10-30 15:22:15 -08004697 if (err)
4698 return err;
4699
4700 return selinux_netlbl_socket_setsockopt(sock, level, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701}
4702
4703static int selinux_socket_getsockopt(struct socket *sock, int level,
4704 int optname)
4705{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004706 return sock_has_perm(sock->sk, SOCKET__GETOPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707}
4708
4709static int selinux_socket_shutdown(struct socket *sock, int how)
4710{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004711 return sock_has_perm(sock->sk, SOCKET__SHUTDOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712}
4713
David S. Miller3610cda2011-01-05 15:38:53 -08004714static int selinux_socket_unix_stream_connect(struct sock *sock,
4715 struct sock *other,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716 struct sock *newsk)
4717{
David S. Miller3610cda2011-01-05 15:38:53 -08004718 struct sk_security_struct *sksec_sock = sock->sk_security;
4719 struct sk_security_struct *sksec_other = other->sk_security;
Paul Moore4d1e2452010-04-22 14:46:18 -04004720 struct sk_security_struct *sksec_new = newsk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004721 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004722 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723 int err;
4724
Eric Paris50c205f2012-04-04 15:01:43 -04004725 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004726 ad.u.net = &net;
4727 ad.u.net->sk = other;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004729 err = avc_has_perm(&selinux_state,
4730 sksec_sock->sid, sksec_other->sid,
Paul Moore4d1e2452010-04-22 14:46:18 -04004731 sksec_other->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732 UNIX_STREAM_SOCKET__CONNECTTO, &ad);
4733 if (err)
4734 return err;
4735
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 /* server child socket */
Paul Moore4d1e2452010-04-22 14:46:18 -04004737 sksec_new->peer_sid = sksec_sock->sid;
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004738 err = security_sid_mls_copy(&selinux_state, sksec_other->sid,
4739 sksec_sock->sid, &sksec_new->sid);
Paul Moore4d1e2452010-04-22 14:46:18 -04004740 if (err)
4741 return err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004742
Paul Moore4d1e2452010-04-22 14:46:18 -04004743 /* connecting socket */
4744 sksec_sock->peer_sid = sksec_new->sid;
4745
4746 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747}
4748
4749static int selinux_socket_unix_may_send(struct socket *sock,
4750 struct socket *other)
4751{
Paul Moore253bfae2010-04-22 14:46:19 -04004752 struct sk_security_struct *ssec = sock->sk->sk_security;
4753 struct sk_security_struct *osec = other->sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004754 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004755 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756
Eric Paris50c205f2012-04-04 15:01:43 -04004757 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004758 ad.u.net = &net;
4759 ad.u.net->sk = other->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004761 return avc_has_perm(&selinux_state,
4762 ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
Paul Moore253bfae2010-04-22 14:46:19 -04004763 &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764}
4765
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004766static int selinux_inet_sys_rcv_skb(struct net *ns, int ifindex,
4767 char *addrp, u16 family, u32 peer_sid,
Thomas Liu2bf49692009-07-14 12:14:09 -04004768 struct common_audit_data *ad)
Paul Mooreeffad8d2008-01-29 08:49:27 -05004769{
4770 int err;
4771 u32 if_sid;
4772 u32 node_sid;
4773
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004774 err = sel_netif_sid(ns, ifindex, &if_sid);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004775 if (err)
4776 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004777 err = avc_has_perm(&selinux_state,
4778 peer_sid, if_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004779 SECCLASS_NETIF, NETIF__INGRESS, ad);
4780 if (err)
4781 return err;
4782
4783 err = sel_netnode_sid(addrp, family, &node_sid);
4784 if (err)
4785 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004786 return avc_has_perm(&selinux_state,
4787 peer_sid, node_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004788 SECCLASS_NODE, NODE__RECVFROM, ad);
4789}
4790
Paul Moore220deb92008-01-29 08:38:23 -05004791static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
Paul Moored8395c82008-10-10 10:16:30 -04004792 u16 family)
Paul Moore220deb92008-01-29 08:38:23 -05004793{
Paul Moore277d3422008-12-31 12:54:11 -05004794 int err = 0;
Paul Moore220deb92008-01-29 08:38:23 -05004795 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004796 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004797 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004798 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04004799 char *addrp;
4800
Eric Paris50c205f2012-04-04 15:01:43 -04004801 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004802 ad.u.net = &net;
4803 ad.u.net->netif = skb->skb_iif;
4804 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004805 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4806 if (err)
4807 return err;
Paul Moore220deb92008-01-29 08:38:23 -05004808
Paul Moore58bfbb52009-03-27 17:10:41 -04004809 if (selinux_secmark_enabled()) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004810 err = avc_has_perm(&selinux_state,
4811 sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Moored8395c82008-10-10 10:16:30 -04004812 PACKET__RECV, &ad);
Paul Moore58bfbb52009-03-27 17:10:41 -04004813 if (err)
4814 return err;
4815 }
Paul Moore220deb92008-01-29 08:38:23 -05004816
Steffen Klassertb9679a72011-02-23 12:55:21 +01004817 err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
4818 if (err)
4819 return err;
4820 err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004821
James Morris4e5ab4c2006-06-09 00:33:33 -07004822 return err;
4823}
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004824
James Morris4e5ab4c2006-06-09 00:33:33 -07004825static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
4826{
Paul Moore220deb92008-01-29 08:38:23 -05004827 int err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004828 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004829 u16 family = sk->sk_family;
4830 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004831 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004832 struct lsm_network_audit net = {0,};
Paul Moore220deb92008-01-29 08:38:23 -05004833 char *addrp;
Paul Moored8395c82008-10-10 10:16:30 -04004834 u8 secmark_active;
4835 u8 peerlbl_active;
James Morris4e5ab4c2006-06-09 00:33:33 -07004836
James Morris4e5ab4c2006-06-09 00:33:33 -07004837 if (family != PF_INET && family != PF_INET6)
Paul Moore220deb92008-01-29 08:38:23 -05004838 return 0;
James Morris4e5ab4c2006-06-09 00:33:33 -07004839
4840 /* Handle mapped IPv4 packets arriving via IPv6 sockets */
Al Viro87fcd702006-12-04 22:00:55 +00004841 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
James Morris4e5ab4c2006-06-09 00:33:33 -07004842 family = PF_INET;
4843
Paul Moored8395c82008-10-10 10:16:30 -04004844 /* If any sort of compatibility mode is enabled then handoff processing
4845 * to the selinux_sock_rcv_skb_compat() function to deal with the
4846 * special handling. We do this in an attempt to keep this function
4847 * as fast and as clean as possible. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004848 if (!selinux_policycap_netpeer())
Paul Moored8395c82008-10-10 10:16:30 -04004849 return selinux_sock_rcv_skb_compat(sk, skb, family);
4850
4851 secmark_active = selinux_secmark_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04004852 peerlbl_active = selinux_peerlbl_enabled();
Paul Moored8395c82008-10-10 10:16:30 -04004853 if (!secmark_active && !peerlbl_active)
4854 return 0;
4855
Eric Paris50c205f2012-04-04 15:01:43 -04004856 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004857 ad.u.net = &net;
4858 ad.u.net->netif = skb->skb_iif;
4859 ad.u.net->family = family;
Paul Moore224dfbd2008-01-29 08:38:13 -05004860 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
James Morris4e5ab4c2006-06-09 00:33:33 -07004861 if (err)
Paul Moore220deb92008-01-29 08:38:23 -05004862 return err;
James Morris4e5ab4c2006-06-09 00:33:33 -07004863
Paul Moored8395c82008-10-10 10:16:30 -04004864 if (peerlbl_active) {
Paul Moored621d352008-01-29 08:43:36 -05004865 u32 peer_sid;
4866
4867 err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
4868 if (err)
4869 return err;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004870 err = selinux_inet_sys_rcv_skb(sock_net(sk), skb->skb_iif,
4871 addrp, family, peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04004872 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04004873 selinux_netlbl_err(skb, family, err, 0);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004874 return err;
Paul Mooredfaebe92008-10-10 10:16:31 -04004875 }
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004876 err = avc_has_perm(&selinux_state,
4877 sk_sid, peer_sid, SECCLASS_PEER,
Paul Moored621d352008-01-29 08:43:36 -05004878 PEER__RECV, &ad);
Chad Hanson46d01d62013-12-23 17:45:01 -05004879 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04004880 selinux_netlbl_err(skb, family, err, 0);
Chad Hanson46d01d62013-12-23 17:45:01 -05004881 return err;
4882 }
Paul Moored621d352008-01-29 08:43:36 -05004883 }
4884
Paul Moored8395c82008-10-10 10:16:30 -04004885 if (secmark_active) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004886 err = avc_has_perm(&selinux_state,
4887 sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004888 PACKET__RECV, &ad);
4889 if (err)
4890 return err;
4891 }
4892
Paul Moored621d352008-01-29 08:43:36 -05004893 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894}
4895
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004896static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
4897 int __user *optlen, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898{
4899 int err = 0;
4900 char *scontext;
4901 u32 scontext_len;
Paul Moore253bfae2010-04-22 14:46:19 -04004902 struct sk_security_struct *sksec = sock->sk->sk_security;
Paul Moore3de4bab2006-11-17 17:38:54 -05004903 u32 peer_sid = SECSID_NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904
Paul Moore253bfae2010-04-22 14:46:19 -04004905 if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
Richard Hainesd4529302018-02-13 20:57:18 +00004906 sksec->sclass == SECCLASS_TCP_SOCKET ||
4907 sksec->sclass == SECCLASS_SCTP_SOCKET)
Eric Parisdd3e7832010-04-07 15:08:46 -04004908 peer_sid = sksec->peer_sid;
Paul Moore253bfae2010-04-22 14:46:19 -04004909 if (peer_sid == SECSID_NULL)
4910 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004912 err = security_sid_to_context(&selinux_state, peer_sid, &scontext,
4913 &scontext_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 if (err)
Paul Moore253bfae2010-04-22 14:46:19 -04004915 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916
4917 if (scontext_len > len) {
4918 err = -ERANGE;
4919 goto out_len;
4920 }
4921
4922 if (copy_to_user(optval, scontext, scontext_len))
4923 err = -EFAULT;
4924
4925out_len:
4926 if (put_user(scontext_len, optlen))
4927 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 kfree(scontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929 return err;
4930}
4931
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004932static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004933{
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004934 u32 peer_secid = SECSID_NULL;
Paul Moore75e22912008-01-29 08:38:04 -05004935 u16 family;
Paul Moore899134f2016-03-28 15:19:10 -04004936 struct inode_security_struct *isec;
Catherine Zhang877ce7c2006-06-29 12:27:47 -07004937
Paul Mooreaa862902008-10-10 10:16:29 -04004938 if (skb && skb->protocol == htons(ETH_P_IP))
4939 family = PF_INET;
4940 else if (skb && skb->protocol == htons(ETH_P_IPV6))
4941 family = PF_INET6;
4942 else if (sock)
Paul Moore75e22912008-01-29 08:38:04 -05004943 family = sock->sk->sk_family;
Paul Moore75e22912008-01-29 08:38:04 -05004944 else
4945 goto out;
4946
Paul Moore899134f2016-03-28 15:19:10 -04004947 if (sock && family == PF_UNIX) {
4948 isec = inode_security_novalidate(SOCK_INODE(sock));
4949 peer_secid = isec->sid;
4950 } else if (skb)
Paul Moore220deb92008-01-29 08:38:23 -05004951 selinux_skb_peerlbl_sid(skb, family, &peer_secid);
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004952
Paul Moore75e22912008-01-29 08:38:04 -05004953out:
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004954 *secid = peer_secid;
Paul Moore75e22912008-01-29 08:38:04 -05004955 if (peer_secid == SECSID_NULL)
4956 return -EINVAL;
4957 return 0;
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004958}
4959
Al Viro7d877f32005-10-21 03:20:43 -04004960static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961{
Paul Moore84914b72010-04-22 14:46:18 -04004962 struct sk_security_struct *sksec;
4963
4964 sksec = kzalloc(sizeof(*sksec), priority);
4965 if (!sksec)
4966 return -ENOMEM;
4967
4968 sksec->peer_sid = SECINITSID_UNLABELED;
4969 sksec->sid = SECINITSID_UNLABELED;
Stephen Smalley5dee25d2015-07-10 17:19:57 -04004970 sksec->sclass = SECCLASS_SOCKET;
Paul Moore84914b72010-04-22 14:46:18 -04004971 selinux_netlbl_sk_security_reset(sksec);
4972 sk->sk_security = sksec;
4973
4974 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975}
4976
4977static void selinux_sk_free_security(struct sock *sk)
4978{
Paul Moore84914b72010-04-22 14:46:18 -04004979 struct sk_security_struct *sksec = sk->sk_security;
4980
4981 sk->sk_security = NULL;
4982 selinux_netlbl_sk_security_free(sksec);
4983 kfree(sksec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984}
4985
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004986static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
4987{
Eric Parisdd3e7832010-04-07 15:08:46 -04004988 struct sk_security_struct *sksec = sk->sk_security;
4989 struct sk_security_struct *newsksec = newsk->sk_security;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004990
Eric Parisdd3e7832010-04-07 15:08:46 -04004991 newsksec->sid = sksec->sid;
4992 newsksec->peer_sid = sksec->peer_sid;
4993 newsksec->sclass = sksec->sclass;
Paul Moore99f59ed2006-08-29 17:53:48 -07004994
Eric Parisdd3e7832010-04-07 15:08:46 -04004995 selinux_netlbl_sk_security_reset(newsksec);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004996}
4997
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07004998static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004999{
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005000 if (!sk)
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07005001 *secid = SECINITSID_ANY_SOCKET;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005002 else {
5003 struct sk_security_struct *sksec = sk->sk_security;
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005004
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07005005 *secid = sksec->sid;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005006 }
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005007}
5008
Eric Paris828dfe12008-04-17 13:17:49 -04005009static void selinux_sock_graft(struct sock *sk, struct socket *parent)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005010{
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05005011 struct inode_security_struct *isec =
5012 inode_security_novalidate(SOCK_INODE(parent));
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005013 struct sk_security_struct *sksec = sk->sk_security;
5014
Paul Moore2873ead2014-07-28 10:42:48 -04005015 if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
5016 sk->sk_family == PF_UNIX)
David Woodhouse2148ccc2006-09-29 15:50:25 -07005017 isec->sid = sksec->sid;
Paul Moore220deb92008-01-29 08:38:23 -05005018 sksec->sclass = isec->sclass;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005019}
5020
Richard Hainesd4529302018-02-13 20:57:18 +00005021/* Called whenever SCTP receives an INIT chunk. This happens when an incoming
5022 * connect(2), sctp_connectx(3) or sctp_sendmsg(3) (with no association
5023 * already present).
5024 */
5025static int selinux_sctp_assoc_request(struct sctp_endpoint *ep,
5026 struct sk_buff *skb)
5027{
5028 struct sk_security_struct *sksec = ep->base.sk->sk_security;
5029 struct common_audit_data ad;
5030 struct lsm_network_audit net = {0,};
5031 u8 peerlbl_active;
5032 u32 peer_sid = SECINITSID_UNLABELED;
5033 u32 conn_sid;
5034 int err = 0;
5035
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005036 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005037 return 0;
5038
5039 peerlbl_active = selinux_peerlbl_enabled();
5040
5041 if (peerlbl_active) {
5042 /* This will return peer_sid = SECSID_NULL if there are
5043 * no peer labels, see security_net_peersid_resolve().
5044 */
5045 err = selinux_skb_peerlbl_sid(skb, ep->base.sk->sk_family,
5046 &peer_sid);
5047 if (err)
5048 return err;
5049
5050 if (peer_sid == SECSID_NULL)
5051 peer_sid = SECINITSID_UNLABELED;
5052 }
5053
5054 if (sksec->sctp_assoc_state == SCTP_ASSOC_UNSET) {
5055 sksec->sctp_assoc_state = SCTP_ASSOC_SET;
5056
5057 /* Here as first association on socket. As the peer SID
5058 * was allowed by peer recv (and the netif/node checks),
5059 * then it is approved by policy and used as the primary
5060 * peer SID for getpeercon(3).
5061 */
5062 sksec->peer_sid = peer_sid;
5063 } else if (sksec->peer_sid != peer_sid) {
5064 /* Other association peer SIDs are checked to enforce
5065 * consistency among the peer SIDs.
5066 */
5067 ad.type = LSM_AUDIT_DATA_NET;
5068 ad.u.net = &net;
5069 ad.u.net->sk = ep->base.sk;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005070 err = avc_has_perm(&selinux_state,
5071 sksec->peer_sid, peer_sid, sksec->sclass,
Richard Hainesd4529302018-02-13 20:57:18 +00005072 SCTP_SOCKET__ASSOCIATION, &ad);
5073 if (err)
5074 return err;
5075 }
5076
5077 /* Compute the MLS component for the connection and store
5078 * the information in ep. This will be used by SCTP TCP type
5079 * sockets and peeled off connections as they cause a new
5080 * socket to be generated. selinux_sctp_sk_clone() will then
5081 * plug this into the new socket.
5082 */
5083 err = selinux_conn_sid(sksec->sid, peer_sid, &conn_sid);
5084 if (err)
5085 return err;
5086
5087 ep->secid = conn_sid;
5088 ep->peer_secid = peer_sid;
5089
5090 /* Set any NetLabel labels including CIPSO/CALIPSO options. */
5091 return selinux_netlbl_sctp_assoc_request(ep, skb);
5092}
5093
5094/* Check if sctp IPv4/IPv6 addresses are valid for binding or connecting
5095 * based on their @optname.
5096 */
5097static int selinux_sctp_bind_connect(struct sock *sk, int optname,
5098 struct sockaddr *address,
5099 int addrlen)
5100{
5101 int len, err = 0, walk_size = 0;
5102 void *addr_buf;
5103 struct sockaddr *addr;
5104 struct socket *sock;
5105
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005106 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005107 return 0;
5108
5109 /* Process one or more addresses that may be IPv4 or IPv6 */
5110 sock = sk->sk_socket;
5111 addr_buf = address;
5112
5113 while (walk_size < addrlen) {
5114 addr = addr_buf;
5115 switch (addr->sa_family) {
Alexey Kodanev4152dc92018-05-11 20:15:13 +03005116 case AF_UNSPEC:
Richard Hainesd4529302018-02-13 20:57:18 +00005117 case AF_INET:
5118 len = sizeof(struct sockaddr_in);
5119 break;
5120 case AF_INET6:
5121 len = sizeof(struct sockaddr_in6);
5122 break;
5123 default:
Alexey Kodanev4152dc92018-05-11 20:15:13 +03005124 return -EINVAL;
Richard Hainesd4529302018-02-13 20:57:18 +00005125 }
5126
5127 err = -EINVAL;
5128 switch (optname) {
5129 /* Bind checks */
5130 case SCTP_PRIMARY_ADDR:
5131 case SCTP_SET_PEER_PRIMARY_ADDR:
5132 case SCTP_SOCKOPT_BINDX_ADD:
5133 err = selinux_socket_bind(sock, addr, len);
5134 break;
5135 /* Connect checks */
5136 case SCTP_SOCKOPT_CONNECTX:
5137 case SCTP_PARAM_SET_PRIMARY:
5138 case SCTP_PARAM_ADD_IP:
5139 case SCTP_SENDMSG_CONNECT:
5140 err = selinux_socket_connect_helper(sock, addr, len);
5141 if (err)
5142 return err;
5143
5144 /* As selinux_sctp_bind_connect() is called by the
5145 * SCTP protocol layer, the socket is already locked,
5146 * therefore selinux_netlbl_socket_connect_locked() is
5147 * is called here. The situations handled are:
5148 * sctp_connectx(3), sctp_sendmsg(3), sendmsg(2),
5149 * whenever a new IP address is added or when a new
5150 * primary address is selected.
5151 * Note that an SCTP connect(2) call happens before
5152 * the SCTP protocol layer and is handled via
5153 * selinux_socket_connect().
5154 */
5155 err = selinux_netlbl_socket_connect_locked(sk, addr);
5156 break;
5157 }
5158
5159 if (err)
5160 return err;
5161
5162 addr_buf += len;
5163 walk_size += len;
5164 }
5165
5166 return 0;
5167}
5168
5169/* Called whenever a new socket is created by accept(2) or sctp_peeloff(3). */
5170static void selinux_sctp_sk_clone(struct sctp_endpoint *ep, struct sock *sk,
5171 struct sock *newsk)
5172{
5173 struct sk_security_struct *sksec = sk->sk_security;
5174 struct sk_security_struct *newsksec = newsk->sk_security;
5175
5176 /* If policy does not support SECCLASS_SCTP_SOCKET then call
5177 * the non-sctp clone version.
5178 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005179 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005180 return selinux_sk_clone_security(sk, newsk);
5181
5182 newsksec->sid = ep->secid;
5183 newsksec->peer_sid = ep->peer_secid;
5184 newsksec->sclass = sksec->sclass;
5185 selinux_netlbl_sctp_sk_clone(sk, newsk);
5186}
5187
Adrian Bunk9a673e52006-08-15 00:03:53 -07005188static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
5189 struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005190{
5191 struct sk_security_struct *sksec = sk->sk_security;
5192 int err;
Paul Moore0b1f24e2013-12-03 11:39:13 -05005193 u16 family = req->rsk_ops->family;
Paul Moore446b8022013-12-04 16:10:51 -05005194 u32 connsid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005195 u32 peersid;
5196
Paul Mooreaa862902008-10-10 10:16:29 -04005197 err = selinux_skb_peerlbl_sid(skb, family, &peersid);
Paul Moore220deb92008-01-29 08:38:23 -05005198 if (err)
5199 return err;
Paul Moore446b8022013-12-04 16:10:51 -05005200 err = selinux_conn_sid(sksec->sid, peersid, &connsid);
5201 if (err)
5202 return err;
5203 req->secid = connsid;
5204 req->peer_secid = peersid;
Venkat Yekkiralaa51c64f2006-07-27 22:01:34 -07005205
Paul Moore389fb8002009-03-27 17:10:34 -04005206 return selinux_netlbl_inet_conn_request(req, family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005207}
5208
Adrian Bunk9a673e52006-08-15 00:03:53 -07005209static void selinux_inet_csk_clone(struct sock *newsk,
5210 const struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005211{
5212 struct sk_security_struct *newsksec = newsk->sk_security;
5213
5214 newsksec->sid = req->secid;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005215 newsksec->peer_sid = req->peer_secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005216 /* NOTE: Ideally, we should also get the isec->sid for the
5217 new socket in sync, but we don't have the isec available yet.
5218 So we will wait until sock_graft to do it, by which
5219 time it will have been created and available. */
Paul Moore99f59ed2006-08-29 17:53:48 -07005220
Paul Moore9f2ad662006-11-17 17:38:53 -05005221 /* We don't need to take any sort of lock here as we are the only
5222 * thread with access to newsksec */
Paul Moore389fb8002009-03-27 17:10:34 -04005223 selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005224}
5225
Paul Moore014ab192008-10-10 10:16:33 -04005226static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005227{
Paul Mooreaa862902008-10-10 10:16:29 -04005228 u16 family = sk->sk_family;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005229 struct sk_security_struct *sksec = sk->sk_security;
5230
Paul Mooreaa862902008-10-10 10:16:29 -04005231 /* handle mapped IPv4 packets arriving via IPv6 sockets */
5232 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
5233 family = PF_INET;
5234
5235 selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005236}
5237
Eric Paris2606fd12010-10-13 16:24:41 -04005238static int selinux_secmark_relabel_packet(u32 sid)
5239{
5240 const struct task_security_struct *__tsec;
5241 u32 tsid;
5242
5243 __tsec = current_security();
5244 tsid = __tsec->sid;
5245
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005246 return avc_has_perm(&selinux_state,
5247 tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO,
5248 NULL);
Eric Paris2606fd12010-10-13 16:24:41 -04005249}
5250
5251static void selinux_secmark_refcount_inc(void)
5252{
5253 atomic_inc(&selinux_secmark_refcount);
5254}
5255
5256static void selinux_secmark_refcount_dec(void)
5257{
5258 atomic_dec(&selinux_secmark_refcount);
5259}
5260
Adrian Bunk9a673e52006-08-15 00:03:53 -07005261static void selinux_req_classify_flow(const struct request_sock *req,
5262 struct flowi *fl)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005263{
David S. Miller1d28f422011-03-12 00:29:39 -05005264 fl->flowi_secid = req->secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005265}
5266
Paul Moore5dbbaf22013-01-14 07:12:19 +00005267static int selinux_tun_dev_alloc_security(void **security)
5268{
5269 struct tun_security_struct *tunsec;
5270
5271 tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL);
5272 if (!tunsec)
5273 return -ENOMEM;
5274 tunsec->sid = current_sid();
5275
5276 *security = tunsec;
5277 return 0;
5278}
5279
5280static void selinux_tun_dev_free_security(void *security)
5281{
5282 kfree(security);
5283}
5284
Paul Mooreed6d76e2009-08-28 18:12:49 -04005285static int selinux_tun_dev_create(void)
5286{
5287 u32 sid = current_sid();
5288
5289 /* we aren't taking into account the "sockcreate" SID since the socket
5290 * that is being created here is not a socket in the traditional sense,
5291 * instead it is a private sock, accessible only to the kernel, and
5292 * representing a wide range of network traffic spanning multiple
5293 * connections unlike traditional sockets - check the TUN driver to
5294 * get a better understanding of why this socket is special */
5295
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005296 return avc_has_perm(&selinux_state,
5297 sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005298 NULL);
5299}
5300
Paul Moore5dbbaf22013-01-14 07:12:19 +00005301static int selinux_tun_dev_attach_queue(void *security)
Paul Mooreed6d76e2009-08-28 18:12:49 -04005302{
Paul Moore5dbbaf22013-01-14 07:12:19 +00005303 struct tun_security_struct *tunsec = security;
5304
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005305 return avc_has_perm(&selinux_state,
5306 current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET,
Paul Moore5dbbaf22013-01-14 07:12:19 +00005307 TUN_SOCKET__ATTACH_QUEUE, NULL);
5308}
5309
5310static int selinux_tun_dev_attach(struct sock *sk, void *security)
5311{
5312 struct tun_security_struct *tunsec = security;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005313 struct sk_security_struct *sksec = sk->sk_security;
5314
5315 /* we don't currently perform any NetLabel based labeling here and it
5316 * isn't clear that we would want to do so anyway; while we could apply
5317 * labeling without the support of the TUN user the resulting labeled
5318 * traffic from the other end of the connection would almost certainly
5319 * cause confusion to the TUN user that had no idea network labeling
5320 * protocols were being used */
5321
Paul Moore5dbbaf22013-01-14 07:12:19 +00005322 sksec->sid = tunsec->sid;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005323 sksec->sclass = SECCLASS_TUN_SOCKET;
Paul Moore5dbbaf22013-01-14 07:12:19 +00005324
5325 return 0;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005326}
5327
Paul Moore5dbbaf22013-01-14 07:12:19 +00005328static int selinux_tun_dev_open(void *security)
Paul Mooreed6d76e2009-08-28 18:12:49 -04005329{
Paul Moore5dbbaf22013-01-14 07:12:19 +00005330 struct tun_security_struct *tunsec = security;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005331 u32 sid = current_sid();
5332 int err;
5333
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005334 err = avc_has_perm(&selinux_state,
5335 sid, tunsec->sid, SECCLASS_TUN_SOCKET,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005336 TUN_SOCKET__RELABELFROM, NULL);
5337 if (err)
5338 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005339 err = avc_has_perm(&selinux_state,
5340 sid, sid, SECCLASS_TUN_SOCKET,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005341 TUN_SOCKET__RELABELTO, NULL);
5342 if (err)
5343 return err;
Paul Moore5dbbaf22013-01-14 07:12:19 +00005344 tunsec->sid = sid;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005345
5346 return 0;
5347}
5348
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
5350{
5351 int err = 0;
5352 u32 perm;
5353 struct nlmsghdr *nlh;
Paul Moore253bfae2010-04-22 14:46:19 -04005354 struct sk_security_struct *sksec = sk->sk_security;
Eric Paris828dfe12008-04-17 13:17:49 -04005355
Hong zhi guo77954982013-03-27 06:49:35 +00005356 if (skb->len < NLMSG_HDRLEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 err = -EINVAL;
5358 goto out;
5359 }
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -07005360 nlh = nlmsg_hdr(skb);
Eric Paris828dfe12008-04-17 13:17:49 -04005361
Paul Moore253bfae2010-04-22 14:46:19 -04005362 err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 if (err) {
5364 if (err == -EINVAL) {
Vladis Dronov76319942015-12-24 11:09:41 -05005365 pr_warn_ratelimited("SELinux: unrecognized netlink"
5366 " message: protocol=%hu nlmsg_type=%hu sclass=%s"
5367 " pig=%d comm=%s\n",
Marek Milkoviccded3ff2015-06-04 16:22:16 -04005368 sk->sk_protocol, nlh->nlmsg_type,
Vladis Dronov76319942015-12-24 11:09:41 -05005369 secclass_map[sksec->sclass - 1].name,
5370 task_pid_nr(current), current->comm);
Paul Mooree5a5ca92018-03-01 17:38:30 -05005371 if (!enforcing_enabled(&selinux_state) ||
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005372 security_get_allow_unknown(&selinux_state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373 err = 0;
5374 }
5375
5376 /* Ignore */
5377 if (err == -ENOENT)
5378 err = 0;
5379 goto out;
5380 }
5381
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005382 err = sock_has_perm(sk, perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383out:
5384 return err;
5385}
5386
5387#ifdef CONFIG_NETFILTER
5388
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005389static unsigned int selinux_ip_forward(struct sk_buff *skb,
5390 const struct net_device *indev,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005391 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392{
Paul Mooredfaebe92008-10-10 10:16:31 -04005393 int err;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005394 char *addrp;
5395 u32 peer_sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04005396 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005397 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05005398 u8 secmark_active;
Paul Moore948bf852008-10-10 10:16:32 -04005399 u8 netlbl_active;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005400 u8 peerlbl_active;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005401
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005402 if (!selinux_policycap_netpeer())
Paul Mooreeffad8d2008-01-29 08:49:27 -05005403 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005404
Paul Mooreeffad8d2008-01-29 08:49:27 -05005405 secmark_active = selinux_secmark_enabled();
Paul Moore948bf852008-10-10 10:16:32 -04005406 netlbl_active = netlbl_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04005407 peerlbl_active = selinux_peerlbl_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05005408 if (!secmark_active && !peerlbl_active)
5409 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005410
Paul Moored8395c82008-10-10 10:16:30 -04005411 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
5412 return NF_DROP;
5413
Eric Paris50c205f2012-04-04 15:01:43 -04005414 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005415 ad.u.net = &net;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005416 ad.u.net->netif = indev->ifindex;
Eric Paris48c62af2012-04-02 13:15:44 -04005417 ad.u.net->family = family;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005418 if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
5419 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420
Paul Mooredfaebe92008-10-10 10:16:31 -04005421 if (peerlbl_active) {
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005422 err = selinux_inet_sys_rcv_skb(dev_net(indev), indev->ifindex,
5423 addrp, family, peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04005424 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04005425 selinux_netlbl_err(skb, family, err, 1);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005426 return NF_DROP;
Paul Mooredfaebe92008-10-10 10:16:31 -04005427 }
5428 }
Paul Mooreeffad8d2008-01-29 08:49:27 -05005429
5430 if (secmark_active)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005431 if (avc_has_perm(&selinux_state,
5432 peer_sid, skb->secmark,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005433 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
5434 return NF_DROP;
5435
Paul Moore948bf852008-10-10 10:16:32 -04005436 if (netlbl_active)
5437 /* we do this in the FORWARD path and not the POST_ROUTING
5438 * path because we want to make sure we apply the necessary
5439 * labeling before IPsec is applied so we can leverage AH
5440 * protection */
5441 if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
5442 return NF_DROP;
5443
Paul Mooreeffad8d2008-01-29 08:49:27 -05005444 return NF_ACCEPT;
5445}
5446
Eric W. Biederman06198b32015-09-18 14:33:06 -05005447static unsigned int selinux_ipv4_forward(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005448 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005449 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005450{
David S. Miller238e54c2015-04-03 20:32:56 -04005451 return selinux_ip_forward(skb, state->in, PF_INET);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005452}
5453
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005454#if IS_ENABLED(CONFIG_IPV6)
Eric W. Biederman06198b32015-09-18 14:33:06 -05005455static unsigned int selinux_ipv6_forward(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005456 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005457 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005458{
David S. Miller238e54c2015-04-03 20:32:56 -04005459 return selinux_ip_forward(skb, state->in, PF_INET6);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005460}
5461#endif /* IPV6 */
5462
Paul Moore948bf852008-10-10 10:16:32 -04005463static unsigned int selinux_ip_output(struct sk_buff *skb,
5464 u16 family)
5465{
Paul Moore47180062013-12-04 16:10:45 -05005466 struct sock *sk;
Paul Moore948bf852008-10-10 10:16:32 -04005467 u32 sid;
5468
5469 if (!netlbl_enabled())
5470 return NF_ACCEPT;
5471
5472 /* we do this in the LOCAL_OUT path and not the POST_ROUTING path
5473 * because we want to make sure we apply the necessary labeling
5474 * before IPsec is applied so we can leverage AH protection */
Paul Moore47180062013-12-04 16:10:45 -05005475 sk = skb->sk;
5476 if (sk) {
5477 struct sk_security_struct *sksec;
5478
Eric Dumazete446f9d2015-10-08 05:01:55 -07005479 if (sk_listener(sk))
Paul Moore47180062013-12-04 16:10:45 -05005480 /* if the socket is the listening state then this
5481 * packet is a SYN-ACK packet which means it needs to
5482 * be labeled based on the connection/request_sock and
5483 * not the parent socket. unfortunately, we can't
5484 * lookup the request_sock yet as it isn't queued on
5485 * the parent socket until after the SYN-ACK is sent.
5486 * the "solution" is to simply pass the packet as-is
5487 * as any IP option based labeling should be copied
5488 * from the initial connection request (in the IP
5489 * layer). it is far from ideal, but until we get a
5490 * security label in the packet itself this is the
5491 * best we can do. */
5492 return NF_ACCEPT;
5493
5494 /* standard practice, label using the parent socket */
5495 sksec = sk->sk_security;
Paul Moore948bf852008-10-10 10:16:32 -04005496 sid = sksec->sid;
5497 } else
5498 sid = SECINITSID_KERNEL;
5499 if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
5500 return NF_DROP;
5501
5502 return NF_ACCEPT;
5503}
5504
Eric W. Biederman06198b32015-09-18 14:33:06 -05005505static unsigned int selinux_ipv4_output(void *priv,
Paul Moore948bf852008-10-10 10:16:32 -04005506 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005507 const struct nf_hook_state *state)
Paul Moore948bf852008-10-10 10:16:32 -04005508{
5509 return selinux_ip_output(skb, PF_INET);
5510}
5511
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005512#if IS_ENABLED(CONFIG_IPV6)
Huw Davies2917f572016-06-27 15:06:15 -04005513static unsigned int selinux_ipv6_output(void *priv,
5514 struct sk_buff *skb,
5515 const struct nf_hook_state *state)
5516{
5517 return selinux_ip_output(skb, PF_INET6);
5518}
5519#endif /* IPV6 */
5520
Paul Mooreeffad8d2008-01-29 08:49:27 -05005521static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
5522 int ifindex,
Paul Moored8395c82008-10-10 10:16:30 -04005523 u16 family)
James Morris4e5ab4c2006-06-09 00:33:33 -07005524{
Eric Dumazet54abc682015-11-08 10:54:07 -08005525 struct sock *sk = skb_to_full_sk(skb);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005526 struct sk_security_struct *sksec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005527 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005528 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04005529 char *addrp;
5530 u8 proto;
James Morris4e5ab4c2006-06-09 00:33:33 -07005531
Paul Mooreeffad8d2008-01-29 08:49:27 -05005532 if (sk == NULL)
5533 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005534 sksec = sk->sk_security;
James Morris4e5ab4c2006-06-09 00:33:33 -07005535
Eric Paris50c205f2012-04-04 15:01:43 -04005536 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005537 ad.u.net = &net;
5538 ad.u.net->netif = ifindex;
5539 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04005540 if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
5541 return NF_DROP;
5542
Paul Moore58bfbb52009-03-27 17:10:41 -04005543 if (selinux_secmark_enabled())
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005544 if (avc_has_perm(&selinux_state,
5545 sksec->sid, skb->secmark,
Paul Moored8395c82008-10-10 10:16:30 -04005546 SECCLASS_PACKET, PACKET__SEND, &ad))
Eric Paris2fe66ec2010-11-23 06:28:08 +00005547 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07005548
Steffen Klassertb9679a72011-02-23 12:55:21 +01005549 if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
5550 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07005551
Paul Mooreeffad8d2008-01-29 08:49:27 -05005552 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553}
5554
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005555static unsigned int selinux_ip_postroute(struct sk_buff *skb,
5556 const struct net_device *outdev,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005557 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558{
Paul Mooreeffad8d2008-01-29 08:49:27 -05005559 u32 secmark_perm;
5560 u32 peer_sid;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005561 int ifindex = outdev->ifindex;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005562 struct sock *sk;
Thomas Liu2bf49692009-07-14 12:14:09 -04005563 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005564 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05005565 char *addrp;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005566 u8 secmark_active;
5567 u8 peerlbl_active;
5568
Paul Mooreeffad8d2008-01-29 08:49:27 -05005569 /* If any sort of compatibility mode is enabled then handoff processing
5570 * to the selinux_ip_postroute_compat() function to deal with the
5571 * special handling. We do this in an attempt to keep this function
5572 * as fast and as clean as possible. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005573 if (!selinux_policycap_netpeer())
Paul Moored8395c82008-10-10 10:16:30 -04005574 return selinux_ip_postroute_compat(skb, ifindex, family);
Paul Moorec0828e52013-12-10 14:58:01 -05005575
Paul Mooreeffad8d2008-01-29 08:49:27 -05005576 secmark_active = selinux_secmark_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04005577 peerlbl_active = selinux_peerlbl_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05005578 if (!secmark_active && !peerlbl_active)
5579 return NF_ACCEPT;
5580
Eric Dumazet54abc682015-11-08 10:54:07 -08005581 sk = skb_to_full_sk(skb);
Paul Moorec0828e52013-12-10 14:58:01 -05005582
Paul Mooreeffad8d2008-01-29 08:49:27 -05005583#ifdef CONFIG_XFRM
5584 /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
5585 * packet transformation so allow the packet to pass without any checks
5586 * since we'll have another chance to perform access control checks
5587 * when the packet is on it's final way out.
5588 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
Paul Moorec0828e52013-12-10 14:58:01 -05005589 * is NULL, in this case go ahead and apply access control.
5590 * NOTE: if this is a local socket (skb->sk != NULL) that is in the
5591 * TCP listening state we cannot wait until the XFRM processing
5592 * is done as we will miss out on the SA label if we do;
5593 * unfortunately, this means more work, but it is only once per
5594 * connection. */
5595 if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL &&
Eric Dumazete446f9d2015-10-08 05:01:55 -07005596 !(sk && sk_listener(sk)))
Paul Mooreeffad8d2008-01-29 08:49:27 -05005597 return NF_ACCEPT;
5598#endif
Paul Mooreeffad8d2008-01-29 08:49:27 -05005599
Paul Moored8395c82008-10-10 10:16:30 -04005600 if (sk == NULL) {
Paul Moore446b8022013-12-04 16:10:51 -05005601 /* Without an associated socket the packet is either coming
5602 * from the kernel or it is being forwarded; check the packet
5603 * to determine which and if the packet is being forwarded
5604 * query the packet directly to determine the security label. */
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005605 if (skb->skb_iif) {
5606 secmark_perm = PACKET__FORWARD_OUT;
Paul Moored8395c82008-10-10 10:16:30 -04005607 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005608 return NF_DROP;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005609 } else {
5610 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04005611 peer_sid = SECINITSID_KERNEL;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005612 }
Eric Dumazete446f9d2015-10-08 05:01:55 -07005613 } else if (sk_listener(sk)) {
Paul Moore446b8022013-12-04 16:10:51 -05005614 /* Locally generated packet but the associated socket is in the
5615 * listening state which means this is a SYN-ACK packet. In
5616 * this particular case the correct security label is assigned
5617 * to the connection/request_sock but unfortunately we can't
5618 * query the request_sock as it isn't queued on the parent
5619 * socket until after the SYN-ACK packet is sent; the only
5620 * viable choice is to regenerate the label like we do in
5621 * selinux_inet_conn_request(). See also selinux_ip_output()
5622 * for similar problems. */
5623 u32 skb_sid;
Eric Dumazete446f9d2015-10-08 05:01:55 -07005624 struct sk_security_struct *sksec;
5625
Eric Dumazete446f9d2015-10-08 05:01:55 -07005626 sksec = sk->sk_security;
Paul Moore446b8022013-12-04 16:10:51 -05005627 if (selinux_skb_peerlbl_sid(skb, family, &skb_sid))
5628 return NF_DROP;
Paul Moorec0828e52013-12-10 14:58:01 -05005629 /* At this point, if the returned skb peerlbl is SECSID_NULL
5630 * and the packet has been through at least one XFRM
5631 * transformation then we must be dealing with the "final"
5632 * form of labeled IPsec packet; since we've already applied
5633 * all of our access controls on this packet we can safely
5634 * pass the packet. */
5635 if (skb_sid == SECSID_NULL) {
5636 switch (family) {
5637 case PF_INET:
5638 if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
5639 return NF_ACCEPT;
5640 break;
5641 case PF_INET6:
5642 if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED)
5643 return NF_ACCEPT;
Paul Moorea7a91a12014-09-03 10:51:59 -04005644 break;
Paul Moorec0828e52013-12-10 14:58:01 -05005645 default:
5646 return NF_DROP_ERR(-ECONNREFUSED);
5647 }
5648 }
Paul Moore446b8022013-12-04 16:10:51 -05005649 if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid))
5650 return NF_DROP;
5651 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04005652 } else {
Paul Moore446b8022013-12-04 16:10:51 -05005653 /* Locally generated packet, fetch the security label from the
5654 * associated socket. */
Paul Mooreeffad8d2008-01-29 08:49:27 -05005655 struct sk_security_struct *sksec = sk->sk_security;
5656 peer_sid = sksec->sid;
5657 secmark_perm = PACKET__SEND;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005658 }
5659
Eric Paris50c205f2012-04-04 15:01:43 -04005660 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005661 ad.u.net = &net;
5662 ad.u.net->netif = ifindex;
5663 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04005664 if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
Eric Paris04f6d702010-11-23 06:28:02 +00005665 return NF_DROP;
Paul Moored8395c82008-10-10 10:16:30 -04005666
Paul Mooreeffad8d2008-01-29 08:49:27 -05005667 if (secmark_active)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005668 if (avc_has_perm(&selinux_state,
5669 peer_sid, skb->secmark,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005670 SECCLASS_PACKET, secmark_perm, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005671 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005672
5673 if (peerlbl_active) {
5674 u32 if_sid;
5675 u32 node_sid;
5676
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005677 if (sel_netif_sid(dev_net(outdev), ifindex, &if_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005678 return NF_DROP;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005679 if (avc_has_perm(&selinux_state,
5680 peer_sid, if_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005681 SECCLASS_NETIF, NETIF__EGRESS, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005682 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005683
5684 if (sel_netnode_sid(addrp, family, &node_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005685 return NF_DROP;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005686 if (avc_has_perm(&selinux_state,
5687 peer_sid, node_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005688 SECCLASS_NODE, NODE__SENDTO, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005689 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005690 }
5691
5692 return NF_ACCEPT;
5693}
5694
Eric W. Biederman06198b32015-09-18 14:33:06 -05005695static unsigned int selinux_ipv4_postroute(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005696 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005697 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005698{
David S. Miller238e54c2015-04-03 20:32:56 -04005699 return selinux_ip_postroute(skb, state->out, PF_INET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700}
5701
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005702#if IS_ENABLED(CONFIG_IPV6)
Eric W. Biederman06198b32015-09-18 14:33:06 -05005703static unsigned int selinux_ipv6_postroute(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005704 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005705 const struct nf_hook_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706{
David S. Miller238e54c2015-04-03 20:32:56 -04005707 return selinux_ip_postroute(skb, state->out, PF_INET6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709#endif /* IPV6 */
5710
5711#endif /* CONFIG_NETFILTER */
5712
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
5714{
Stephen Smalley941fc5b2009-10-01 14:48:23 -04005715 return selinux_nlmsg_perm(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716}
5717
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005718static int ipc_alloc_security(struct kern_ipc_perm *perm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719 u16 sclass)
5720{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721 struct ipc_security_struct *isec;
5722
James Morris89d155e2005-10-30 14:59:21 -08005723 isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724 if (!isec)
5725 return -ENOMEM;
5726
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727 isec->sclass = sclass;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005728 isec->sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729 perm->security = isec;
5730
5731 return 0;
5732}
5733
5734static void ipc_free_security(struct kern_ipc_perm *perm)
5735{
5736 struct ipc_security_struct *isec = perm->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737 perm->security = NULL;
5738 kfree(isec);
5739}
5740
5741static int msg_msg_alloc_security(struct msg_msg *msg)
5742{
5743 struct msg_security_struct *msec;
5744
James Morris89d155e2005-10-30 14:59:21 -08005745 msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746 if (!msec)
5747 return -ENOMEM;
5748
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749 msec->sid = SECINITSID_UNLABELED;
5750 msg->security = msec;
5751
5752 return 0;
5753}
5754
5755static void msg_msg_free_security(struct msg_msg *msg)
5756{
5757 struct msg_security_struct *msec = msg->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758
5759 msg->security = NULL;
5760 kfree(msec);
5761}
5762
5763static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
Stephen Smalley6af963f2005-05-01 08:58:39 -07005764 u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005766 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005767 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005768 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770 isec = ipc_perms->security;
5771
Eric Paris50c205f2012-04-04 15:01:43 -04005772 ad.type = LSM_AUDIT_DATA_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773 ad.u.ipc_id = ipc_perms->key;
5774
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005775 return avc_has_perm(&selinux_state,
5776 sid, isec->sid, isec->sclass, perms, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777}
5778
5779static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
5780{
5781 return msg_msg_alloc_security(msg);
5782}
5783
5784static void selinux_msg_msg_free_security(struct msg_msg *msg)
5785{
5786 msg_msg_free_security(msg);
5787}
5788
5789/* message queue security operations */
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005790static int selinux_msg_queue_alloc_security(struct kern_ipc_perm *msq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005793 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005794 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005795 int rc;
5796
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005797 rc = ipc_alloc_security(msq, SECCLASS_MSGQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798 if (rc)
5799 return rc;
5800
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005801 isec = msq->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802
Eric Paris50c205f2012-04-04 15:01:43 -04005803 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005804 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005806 rc = avc_has_perm(&selinux_state,
5807 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808 MSGQ__CREATE, &ad);
5809 if (rc) {
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005810 ipc_free_security(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811 return rc;
5812 }
5813 return 0;
5814}
5815
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005816static void selinux_msg_queue_free_security(struct kern_ipc_perm *msq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817{
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005818 ipc_free_security(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819}
5820
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005821static int selinux_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005823 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005824 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005825 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005827 isec = msq->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828
Eric Paris50c205f2012-04-04 15:01:43 -04005829 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005830 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005832 return avc_has_perm(&selinux_state,
5833 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005834 MSGQ__ASSOCIATE, &ad);
5835}
5836
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005837static int selinux_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838{
5839 int err;
5840 int perms;
5841
Eric Paris828dfe12008-04-17 13:17:49 -04005842 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843 case IPC_INFO:
5844 case MSG_INFO:
5845 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005846 return avc_has_perm(&selinux_state,
5847 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005848 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849 case IPC_STAT:
5850 case MSG_STAT:
Davidlohr Bueso23c8cec2018-04-10 16:35:30 -07005851 case MSG_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005852 perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
5853 break;
5854 case IPC_SET:
5855 perms = MSGQ__SETATTR;
5856 break;
5857 case IPC_RMID:
5858 perms = MSGQ__DESTROY;
5859 break;
5860 default:
5861 return 0;
5862 }
5863
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005864 err = ipc_has_perm(msq, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005865 return err;
5866}
5867
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005868static int selinux_msg_queue_msgsnd(struct kern_ipc_perm *msq, struct msg_msg *msg, int msqflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870 struct ipc_security_struct *isec;
5871 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005872 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005873 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874 int rc;
5875
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005876 isec = msq->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877 msec = msg->security;
5878
5879 /*
5880 * First time through, need to assign label to the message
5881 */
5882 if (msec->sid == SECINITSID_UNLABELED) {
5883 /*
5884 * Compute new sid based on current process and
5885 * message queue this message will be stored in
5886 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005887 rc = security_transition_sid(&selinux_state, sid, isec->sid,
5888 SECCLASS_MSG, NULL, &msec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005889 if (rc)
5890 return rc;
5891 }
5892
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
5896 /* Can this process write to the queue? */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005897 rc = avc_has_perm(&selinux_state,
5898 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899 MSGQ__WRITE, &ad);
5900 if (!rc)
5901 /* Can this process send the message */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005902 rc = avc_has_perm(&selinux_state,
5903 sid, msec->sid, SECCLASS_MSG,
David Howells275bb412008-11-14 10:39:19 +11005904 MSG__SEND, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905 if (!rc)
5906 /* Can the message be put in the queue? */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005907 rc = avc_has_perm(&selinux_state,
5908 msec->sid, isec->sid, SECCLASS_MSGQ,
David Howells275bb412008-11-14 10:39:19 +11005909 MSGQ__ENQUEUE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910
5911 return rc;
5912}
5913
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005914static int selinux_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005915 struct task_struct *target,
5916 long type, int mode)
5917{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918 struct ipc_security_struct *isec;
5919 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005920 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005921 u32 sid = task_sid(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922 int rc;
5923
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005924 isec = msq->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005925 msec = msg->security;
5926
Eric Paris50c205f2012-04-04 15:01:43 -04005927 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005928 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005929
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005930 rc = avc_has_perm(&selinux_state,
5931 sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005932 SECCLASS_MSGQ, MSGQ__READ, &ad);
5933 if (!rc)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005934 rc = avc_has_perm(&selinux_state,
5935 sid, msec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005936 SECCLASS_MSG, MSG__RECEIVE, &ad);
5937 return rc;
5938}
5939
5940/* Shared Memory security operations */
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005941static int selinux_shm_alloc_security(struct kern_ipc_perm *shp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005942{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005943 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005944 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005945 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946 int rc;
5947
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005948 rc = ipc_alloc_security(shp, SECCLASS_SHM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005949 if (rc)
5950 return rc;
5951
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005952 isec = shp->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005953
Eric Paris50c205f2012-04-04 15:01:43 -04005954 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005955 ad.u.ipc_id = shp->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005956
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005957 rc = avc_has_perm(&selinux_state,
5958 sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005959 SHM__CREATE, &ad);
5960 if (rc) {
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005961 ipc_free_security(shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005962 return rc;
5963 }
5964 return 0;
5965}
5966
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005967static void selinux_shm_free_security(struct kern_ipc_perm *shp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005968{
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005969 ipc_free_security(shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005970}
5971
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005972static int selinux_shm_associate(struct kern_ipc_perm *shp, int shmflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005973{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005974 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005975 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005976 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005978 isec = shp->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979
Eric Paris50c205f2012-04-04 15:01:43 -04005980 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005981 ad.u.ipc_id = shp->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005983 return avc_has_perm(&selinux_state,
5984 sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005985 SHM__ASSOCIATE, &ad);
5986}
5987
5988/* Note, at this point, shp is locked down */
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005989static int selinux_shm_shmctl(struct kern_ipc_perm *shp, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005990{
5991 int perms;
5992 int err;
5993
Eric Paris828dfe12008-04-17 13:17:49 -04005994 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005995 case IPC_INFO:
5996 case SHM_INFO:
5997 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005998 return avc_has_perm(&selinux_state,
5999 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006000 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006001 case IPC_STAT:
6002 case SHM_STAT:
Davidlohr Buesoc21a6972018-04-10 16:35:23 -07006003 case SHM_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006004 perms = SHM__GETATTR | SHM__ASSOCIATE;
6005 break;
6006 case IPC_SET:
6007 perms = SHM__SETATTR;
6008 break;
6009 case SHM_LOCK:
6010 case SHM_UNLOCK:
6011 perms = SHM__LOCK;
6012 break;
6013 case IPC_RMID:
6014 perms = SHM__DESTROY;
6015 break;
6016 default:
6017 return 0;
6018 }
6019
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006020 err = ipc_has_perm(shp, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006021 return err;
6022}
6023
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006024static int selinux_shm_shmat(struct kern_ipc_perm *shp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006025 char __user *shmaddr, int shmflg)
6026{
6027 u32 perms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028
6029 if (shmflg & SHM_RDONLY)
6030 perms = SHM__READ;
6031 else
6032 perms = SHM__READ | SHM__WRITE;
6033
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006034 return ipc_has_perm(shp, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006035}
6036
6037/* Semaphore security operations */
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006038static int selinux_sem_alloc_security(struct kern_ipc_perm *sma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006041 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006042 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006043 int rc;
6044
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006045 rc = ipc_alloc_security(sma, SECCLASS_SEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006046 if (rc)
6047 return rc;
6048
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006049 isec = sma->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050
Eric Paris50c205f2012-04-04 15:01:43 -04006051 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006052 ad.u.ipc_id = sma->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006054 rc = avc_has_perm(&selinux_state,
6055 sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056 SEM__CREATE, &ad);
6057 if (rc) {
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006058 ipc_free_security(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059 return rc;
6060 }
6061 return 0;
6062}
6063
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006064static void selinux_sem_free_security(struct kern_ipc_perm *sma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006065{
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006066 ipc_free_security(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067}
6068
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006069static int selinux_sem_associate(struct kern_ipc_perm *sma, int semflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006072 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006073 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006074
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006075 isec = sma->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076
Eric Paris50c205f2012-04-04 15:01:43 -04006077 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006078 ad.u.ipc_id = sma->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006080 return avc_has_perm(&selinux_state,
6081 sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082 SEM__ASSOCIATE, &ad);
6083}
6084
6085/* Note, at this point, sma is locked down */
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006086static int selinux_sem_semctl(struct kern_ipc_perm *sma, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006087{
6088 int err;
6089 u32 perms;
6090
Eric Paris828dfe12008-04-17 13:17:49 -04006091 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006092 case IPC_INFO:
6093 case SEM_INFO:
6094 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006095 return avc_has_perm(&selinux_state,
6096 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006097 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006098 case GETPID:
6099 case GETNCNT:
6100 case GETZCNT:
6101 perms = SEM__GETATTR;
6102 break;
6103 case GETVAL:
6104 case GETALL:
6105 perms = SEM__READ;
6106 break;
6107 case SETVAL:
6108 case SETALL:
6109 perms = SEM__WRITE;
6110 break;
6111 case IPC_RMID:
6112 perms = SEM__DESTROY;
6113 break;
6114 case IPC_SET:
6115 perms = SEM__SETATTR;
6116 break;
6117 case IPC_STAT:
6118 case SEM_STAT:
Davidlohr Buesoa280d6d2018-04-10 16:35:26 -07006119 case SEM_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120 perms = SEM__GETATTR | SEM__ASSOCIATE;
6121 break;
6122 default:
6123 return 0;
6124 }
6125
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006126 err = ipc_has_perm(sma, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127 return err;
6128}
6129
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006130static int selinux_sem_semop(struct kern_ipc_perm *sma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131 struct sembuf *sops, unsigned nsops, int alter)
6132{
6133 u32 perms;
6134
6135 if (alter)
6136 perms = SEM__READ | SEM__WRITE;
6137 else
6138 perms = SEM__READ;
6139
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006140 return ipc_has_perm(sma, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006141}
6142
6143static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
6144{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006145 u32 av = 0;
6146
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147 av = 0;
6148 if (flag & S_IRUGO)
6149 av |= IPC__UNIX_READ;
6150 if (flag & S_IWUGO)
6151 av |= IPC__UNIX_WRITE;
6152
6153 if (av == 0)
6154 return 0;
6155
Stephen Smalley6af963f2005-05-01 08:58:39 -07006156 return ipc_has_perm(ipcp, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006157}
6158
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02006159static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
6160{
6161 struct ipc_security_struct *isec = ipcp->security;
6162 *secid = isec->sid;
6163}
6164
Eric Paris828dfe12008-04-17 13:17:49 -04006165static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006166{
6167 if (inode)
6168 inode_doinit_with_dentry(inode, dentry);
6169}
6170
6171static int selinux_getprocattr(struct task_struct *p,
Al Viro04ff9702007-03-12 16:17:58 +00006172 char *name, char **value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006173{
David Howells275bb412008-11-14 10:39:19 +11006174 const struct task_security_struct *__tsec;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00006175 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006176 int error;
Al Viro04ff9702007-03-12 16:17:58 +00006177 unsigned len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006178
David Howells275bb412008-11-14 10:39:19 +11006179 rcu_read_lock();
6180 __tsec = __task_cred(p)->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006181
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006182 if (current != p) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006183 error = avc_has_perm(&selinux_state,
6184 current_sid(), __tsec->sid,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006185 SECCLASS_PROCESS, PROCESS__GETATTR, NULL);
6186 if (error)
6187 goto bad;
6188 }
6189
Linus Torvalds1da177e2005-04-16 15:20:36 -07006190 if (!strcmp(name, "current"))
David Howells275bb412008-11-14 10:39:19 +11006191 sid = __tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006192 else if (!strcmp(name, "prev"))
David Howells275bb412008-11-14 10:39:19 +11006193 sid = __tsec->osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006194 else if (!strcmp(name, "exec"))
David Howells275bb412008-11-14 10:39:19 +11006195 sid = __tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006196 else if (!strcmp(name, "fscreate"))
David Howells275bb412008-11-14 10:39:19 +11006197 sid = __tsec->create_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006198 else if (!strcmp(name, "keycreate"))
David Howells275bb412008-11-14 10:39:19 +11006199 sid = __tsec->keycreate_sid;
Eric Paris42c3e032006-06-26 00:26:03 -07006200 else if (!strcmp(name, "sockcreate"))
David Howells275bb412008-11-14 10:39:19 +11006201 sid = __tsec->sockcreate_sid;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006202 else {
6203 error = -EINVAL;
6204 goto bad;
6205 }
David Howells275bb412008-11-14 10:39:19 +11006206 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006207
6208 if (!sid)
6209 return 0;
6210
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006211 error = security_sid_to_context(&selinux_state, sid, value, &len);
Al Viro04ff9702007-03-12 16:17:58 +00006212 if (error)
6213 return error;
6214 return len;
David Howells275bb412008-11-14 10:39:19 +11006215
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006216bad:
David Howells275bb412008-11-14 10:39:19 +11006217 rcu_read_unlock();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006218 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219}
6220
Stephen Smalleyb21507e2017-01-09 10:07:31 -05006221static int selinux_setprocattr(const char *name, void *value, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006222{
6223 struct task_security_struct *tsec;
David Howellsd84f4f92008-11-14 10:39:23 +11006224 struct cred *new;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006225 u32 mysid = current_sid(), sid = 0, ptsid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006226 int error;
6227 char *str = value;
6228
Linus Torvalds1da177e2005-04-16 15:20:36 -07006229 /*
6230 * Basic control over ability to set these attributes at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006231 */
6232 if (!strcmp(name, "exec"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006233 error = avc_has_perm(&selinux_state,
6234 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006235 PROCESS__SETEXEC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006236 else if (!strcmp(name, "fscreate"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006237 error = avc_has_perm(&selinux_state,
6238 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006239 PROCESS__SETFSCREATE, NULL);
Michael LeMay4eb582c2006-06-26 00:24:57 -07006240 else if (!strcmp(name, "keycreate"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006241 error = avc_has_perm(&selinux_state,
6242 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006243 PROCESS__SETKEYCREATE, NULL);
Eric Paris42c3e032006-06-26 00:26:03 -07006244 else if (!strcmp(name, "sockcreate"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006245 error = avc_has_perm(&selinux_state,
6246 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006247 PROCESS__SETSOCKCREATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006248 else if (!strcmp(name, "current"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006249 error = avc_has_perm(&selinux_state,
6250 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006251 PROCESS__SETCURRENT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006252 else
6253 error = -EINVAL;
6254 if (error)
6255 return error;
6256
6257 /* Obtain a SID for the context, if one was specified. */
Stephen Smalleya050a572017-01-31 11:54:04 -05006258 if (size && str[0] && str[0] != '\n') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006259 if (str[size-1] == '\n') {
6260 str[size-1] = 0;
6261 size--;
6262 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006263 error = security_context_to_sid(&selinux_state, value, size,
6264 &sid, GFP_KERNEL);
Stephen Smalley12b29f32008-05-07 13:03:20 -04006265 if (error == -EINVAL && !strcmp(name, "fscreate")) {
Stephen Smalleydb590002017-04-20 11:31:30 -04006266 if (!has_cap_mac_admin(true)) {
Eric Parisd6ea83e2012-04-04 13:45:49 -04006267 struct audit_buffer *ab;
6268 size_t audit_size;
6269
6270 /* We strip a nul only if it is at the end, otherwise the
6271 * context contains a nul and we should audit that */
6272 if (str[size - 1] == '\0')
6273 audit_size = size - 1;
6274 else
6275 audit_size = size;
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04006276 ab = audit_log_start(audit_context(),
6277 GFP_ATOMIC,
6278 AUDIT_SELINUX_ERR);
Eric Parisd6ea83e2012-04-04 13:45:49 -04006279 audit_log_format(ab, "op=fscreate invalid_context=");
6280 audit_log_n_untrustedstring(ab, value, audit_size);
6281 audit_log_end(ab);
6282
Stephen Smalley12b29f32008-05-07 13:03:20 -04006283 return error;
Eric Parisd6ea83e2012-04-04 13:45:49 -04006284 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006285 error = security_context_to_sid_force(
6286 &selinux_state,
6287 value, size, &sid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04006288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006289 if (error)
6290 return error;
6291 }
6292
David Howellsd84f4f92008-11-14 10:39:23 +11006293 new = prepare_creds();
6294 if (!new)
6295 return -ENOMEM;
6296
Linus Torvalds1da177e2005-04-16 15:20:36 -07006297 /* Permission checking based on the specified context is
6298 performed during the actual operation (execve,
6299 open/mkdir/...), when we know the full context of the
David Howellsd84f4f92008-11-14 10:39:23 +11006300 operation. See selinux_bprm_set_creds for the execve
Linus Torvalds1da177e2005-04-16 15:20:36 -07006301 checks and may_create for the file creation checks. The
6302 operation will then fail if the context is not permitted. */
David Howellsd84f4f92008-11-14 10:39:23 +11006303 tsec = new->security;
6304 if (!strcmp(name, "exec")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006305 tsec->exec_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006306 } else if (!strcmp(name, "fscreate")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006307 tsec->create_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006308 } else if (!strcmp(name, "keycreate")) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006309 error = avc_has_perm(&selinux_state,
6310 mysid, sid, SECCLASS_KEY, KEY__CREATE,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006311 NULL);
Michael LeMay4eb582c2006-06-26 00:24:57 -07006312 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11006313 goto abort_change;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006314 tsec->keycreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006315 } else if (!strcmp(name, "sockcreate")) {
Eric Paris42c3e032006-06-26 00:26:03 -07006316 tsec->sockcreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006317 } else if (!strcmp(name, "current")) {
6318 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006319 if (sid == 0)
David Howellsd84f4f92008-11-14 10:39:23 +11006320 goto abort_change;
KaiGai Koheid9250de2008-08-28 16:35:57 +09006321
David Howellsd84f4f92008-11-14 10:39:23 +11006322 /* Only allow single threaded processes to change context */
6323 error = -EPERM;
Oleg Nesterov5bb459b2009-07-10 03:48:23 +02006324 if (!current_is_single_threaded()) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006325 error = security_bounded_transition(&selinux_state,
6326 tsec->sid, sid);
David Howellsd84f4f92008-11-14 10:39:23 +11006327 if (error)
6328 goto abort_change;
Eric Paris828dfe12008-04-17 13:17:49 -04006329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330
6331 /* Check permissions for the transition. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006332 error = avc_has_perm(&selinux_state,
6333 tsec->sid, sid, SECCLASS_PROCESS,
Eric Paris828dfe12008-04-17 13:17:49 -04006334 PROCESS__DYNTRANSITION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006335 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11006336 goto abort_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006337
6338 /* Check for ptracing, and update the task SID if ok.
6339 Otherwise, leave SID unchanged and fail. */
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006340 ptsid = ptrace_parent_sid();
Paul Moore0c6181c2016-03-30 21:41:21 -04006341 if (ptsid != 0) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006342 error = avc_has_perm(&selinux_state,
6343 ptsid, sid, SECCLASS_PROCESS,
David Howellsd84f4f92008-11-14 10:39:23 +11006344 PROCESS__PTRACE, NULL);
6345 if (error)
6346 goto abort_change;
6347 }
6348
6349 tsec->sid = sid;
6350 } else {
6351 error = -EINVAL;
6352 goto abort_change;
6353 }
6354
6355 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006356 return size;
David Howellsd84f4f92008-11-14 10:39:23 +11006357
6358abort_change:
6359 abort_creds(new);
6360 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006361}
6362
David Quigley746df9b2013-05-22 12:50:35 -04006363static int selinux_ismaclabel(const char *name)
6364{
6365 return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0);
6366}
6367
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006368static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
6369{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006370 return security_sid_to_context(&selinux_state, secid,
6371 secdata, seclen);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006372}
6373
David Howells7bf570d2008-04-29 20:52:51 +01006374static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
David Howells63cb3442008-01-15 23:47:35 +00006375{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006376 return security_context_to_sid(&selinux_state, secdata, seclen,
6377 secid, GFP_KERNEL);
David Howells63cb3442008-01-15 23:47:35 +00006378}
6379
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006380static void selinux_release_secctx(char *secdata, u32 seclen)
6381{
Paul Moore088999e2007-08-01 11:12:58 -04006382 kfree(secdata);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006383}
6384
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006385static void selinux_inode_invalidate_secctx(struct inode *inode)
6386{
6387 struct inode_security_struct *isec = inode->i_security;
6388
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01006389 spin_lock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006390 isec->initialized = LABEL_INVALID;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01006391 spin_unlock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006392}
6393
David P. Quigley1ee65e32009-09-03 14:25:57 -04006394/*
6395 * called with inode->i_mutex locked
6396 */
6397static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
6398{
6399 return selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, ctx, ctxlen, 0);
6400}
6401
6402/*
6403 * called with inode->i_mutex locked
6404 */
6405static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
6406{
6407 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0);
6408}
6409
6410static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
6411{
6412 int len = 0;
6413 len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX,
6414 ctx, true);
6415 if (len < 0)
6416 return len;
6417 *ctxlen = len;
6418 return 0;
6419}
Michael LeMayd7200242006-06-22 14:47:17 -07006420#ifdef CONFIG_KEYS
6421
David Howellsd84f4f92008-11-14 10:39:23 +11006422static int selinux_key_alloc(struct key *k, const struct cred *cred,
David Howells7e047ef2006-06-26 00:24:50 -07006423 unsigned long flags)
Michael LeMayd7200242006-06-22 14:47:17 -07006424{
David Howellsd84f4f92008-11-14 10:39:23 +11006425 const struct task_security_struct *tsec;
Michael LeMayd7200242006-06-22 14:47:17 -07006426 struct key_security_struct *ksec;
6427
6428 ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
6429 if (!ksec)
6430 return -ENOMEM;
6431
David Howellsd84f4f92008-11-14 10:39:23 +11006432 tsec = cred->security;
6433 if (tsec->keycreate_sid)
6434 ksec->sid = tsec->keycreate_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006435 else
David Howellsd84f4f92008-11-14 10:39:23 +11006436 ksec->sid = tsec->sid;
Michael LeMayd7200242006-06-22 14:47:17 -07006437
David Howells275bb412008-11-14 10:39:19 +11006438 k->security = ksec;
Michael LeMayd7200242006-06-22 14:47:17 -07006439 return 0;
6440}
6441
6442static void selinux_key_free(struct key *k)
6443{
6444 struct key_security_struct *ksec = k->security;
6445
6446 k->security = NULL;
6447 kfree(ksec);
6448}
6449
6450static int selinux_key_permission(key_ref_t key_ref,
David Howellsd84f4f92008-11-14 10:39:23 +11006451 const struct cred *cred,
David Howellsf5895942014-03-14 17:44:49 +00006452 unsigned perm)
Michael LeMayd7200242006-06-22 14:47:17 -07006453{
6454 struct key *key;
Michael LeMayd7200242006-06-22 14:47:17 -07006455 struct key_security_struct *ksec;
David Howells275bb412008-11-14 10:39:19 +11006456 u32 sid;
Michael LeMayd7200242006-06-22 14:47:17 -07006457
6458 /* if no specific permissions are requested, we skip the
6459 permission check. No serious, additional covert channels
6460 appear to be created. */
6461 if (perm == 0)
6462 return 0;
6463
David Howellsd84f4f92008-11-14 10:39:23 +11006464 sid = cred_sid(cred);
David Howells275bb412008-11-14 10:39:19 +11006465
6466 key = key_ref_to_ptr(key_ref);
6467 ksec = key->security;
6468
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006469 return avc_has_perm(&selinux_state,
6470 sid, ksec->sid, SECCLASS_KEY, perm, NULL);
Michael LeMayd7200242006-06-22 14:47:17 -07006471}
6472
David Howells70a5bb72008-04-29 01:01:26 -07006473static int selinux_key_getsecurity(struct key *key, char **_buffer)
6474{
6475 struct key_security_struct *ksec = key->security;
6476 char *context = NULL;
6477 unsigned len;
6478 int rc;
6479
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006480 rc = security_sid_to_context(&selinux_state, ksec->sid,
6481 &context, &len);
David Howells70a5bb72008-04-29 01:01:26 -07006482 if (!rc)
6483 rc = len;
6484 *_buffer = context;
6485 return rc;
6486}
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006487#endif
David Howells70a5bb72008-04-29 01:01:26 -07006488
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006489#ifdef CONFIG_SECURITY_INFINIBAND
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006490static int selinux_ib_pkey_access(void *ib_sec, u64 subnet_prefix, u16 pkey_val)
6491{
6492 struct common_audit_data ad;
6493 int err;
6494 u32 sid = 0;
6495 struct ib_security_struct *sec = ib_sec;
6496 struct lsm_ibpkey_audit ibpkey;
6497
Daniel Jurgens409dcf32017-05-19 15:48:59 +03006498 err = sel_ib_pkey_sid(subnet_prefix, pkey_val, &sid);
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006499 if (err)
6500 return err;
6501
6502 ad.type = LSM_AUDIT_DATA_IBPKEY;
6503 ibpkey.subnet_prefix = subnet_prefix;
6504 ibpkey.pkey = pkey_val;
6505 ad.u.ibpkey = &ibpkey;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006506 return avc_has_perm(&selinux_state,
6507 sec->sid, sid,
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006508 SECCLASS_INFINIBAND_PKEY,
6509 INFINIBAND_PKEY__ACCESS, &ad);
6510}
6511
Daniel Jurgensab861df2017-05-19 15:48:58 +03006512static int selinux_ib_endport_manage_subnet(void *ib_sec, const char *dev_name,
6513 u8 port_num)
6514{
6515 struct common_audit_data ad;
6516 int err;
6517 u32 sid = 0;
6518 struct ib_security_struct *sec = ib_sec;
6519 struct lsm_ibendport_audit ibendport;
6520
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006521 err = security_ib_endport_sid(&selinux_state, dev_name, port_num,
6522 &sid);
Daniel Jurgensab861df2017-05-19 15:48:58 +03006523
6524 if (err)
6525 return err;
6526
6527 ad.type = LSM_AUDIT_DATA_IBENDPORT;
6528 strncpy(ibendport.dev_name, dev_name, sizeof(ibendport.dev_name));
6529 ibendport.port = port_num;
6530 ad.u.ibendport = &ibendport;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006531 return avc_has_perm(&selinux_state,
6532 sec->sid, sid,
Daniel Jurgensab861df2017-05-19 15:48:58 +03006533 SECCLASS_INFINIBAND_ENDPORT,
6534 INFINIBAND_ENDPORT__MANAGE_SUBNET, &ad);
6535}
6536
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006537static int selinux_ib_alloc_security(void **ib_sec)
6538{
6539 struct ib_security_struct *sec;
6540
6541 sec = kzalloc(sizeof(*sec), GFP_KERNEL);
6542 if (!sec)
6543 return -ENOMEM;
6544 sec->sid = current_sid();
6545
6546 *ib_sec = sec;
6547 return 0;
6548}
6549
6550static void selinux_ib_free_security(void *ib_sec)
6551{
6552 kfree(ib_sec);
6553}
Michael LeMayd7200242006-06-22 14:47:17 -07006554#endif
6555
Chenbo Fengec27c352017-10-18 13:00:25 -07006556#ifdef CONFIG_BPF_SYSCALL
6557static int selinux_bpf(int cmd, union bpf_attr *attr,
6558 unsigned int size)
6559{
6560 u32 sid = current_sid();
6561 int ret;
6562
6563 switch (cmd) {
6564 case BPF_MAP_CREATE:
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006565 ret = avc_has_perm(&selinux_state,
6566 sid, sid, SECCLASS_BPF, BPF__MAP_CREATE,
Chenbo Fengec27c352017-10-18 13:00:25 -07006567 NULL);
6568 break;
6569 case BPF_PROG_LOAD:
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006570 ret = avc_has_perm(&selinux_state,
6571 sid, sid, SECCLASS_BPF, BPF__PROG_LOAD,
Chenbo Fengec27c352017-10-18 13:00:25 -07006572 NULL);
6573 break;
6574 default:
6575 ret = 0;
6576 break;
6577 }
6578
6579 return ret;
6580}
6581
6582static u32 bpf_map_fmode_to_av(fmode_t fmode)
6583{
6584 u32 av = 0;
6585
6586 if (fmode & FMODE_READ)
6587 av |= BPF__MAP_READ;
6588 if (fmode & FMODE_WRITE)
6589 av |= BPF__MAP_WRITE;
6590 return av;
6591}
6592
Chenbo Fengf66e4482017-10-18 13:00:26 -07006593/* This function will check the file pass through unix socket or binder to see
6594 * if it is a bpf related object. And apply correspinding checks on the bpf
6595 * object based on the type. The bpf maps and programs, not like other files and
6596 * socket, are using a shared anonymous inode inside the kernel as their inode.
6597 * So checking that inode cannot identify if the process have privilege to
6598 * access the bpf object and that's why we have to add this additional check in
6599 * selinux_file_receive and selinux_binder_transfer_files.
6600 */
6601static int bpf_fd_pass(struct file *file, u32 sid)
6602{
6603 struct bpf_security_struct *bpfsec;
6604 struct bpf_prog *prog;
6605 struct bpf_map *map;
6606 int ret;
6607
6608 if (file->f_op == &bpf_map_fops) {
6609 map = file->private_data;
6610 bpfsec = map->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006611 ret = avc_has_perm(&selinux_state,
6612 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengf66e4482017-10-18 13:00:26 -07006613 bpf_map_fmode_to_av(file->f_mode), NULL);
6614 if (ret)
6615 return ret;
6616 } else if (file->f_op == &bpf_prog_fops) {
6617 prog = file->private_data;
6618 bpfsec = prog->aux->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006619 ret = avc_has_perm(&selinux_state,
6620 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengf66e4482017-10-18 13:00:26 -07006621 BPF__PROG_RUN, NULL);
6622 if (ret)
6623 return ret;
6624 }
6625 return 0;
6626}
6627
Chenbo Fengec27c352017-10-18 13:00:25 -07006628static int selinux_bpf_map(struct bpf_map *map, fmode_t fmode)
6629{
6630 u32 sid = current_sid();
6631 struct bpf_security_struct *bpfsec;
6632
6633 bpfsec = map->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006634 return avc_has_perm(&selinux_state,
6635 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengec27c352017-10-18 13:00:25 -07006636 bpf_map_fmode_to_av(fmode), NULL);
6637}
6638
6639static int selinux_bpf_prog(struct bpf_prog *prog)
6640{
6641 u32 sid = current_sid();
6642 struct bpf_security_struct *bpfsec;
6643
6644 bpfsec = prog->aux->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006645 return avc_has_perm(&selinux_state,
6646 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengec27c352017-10-18 13:00:25 -07006647 BPF__PROG_RUN, NULL);
6648}
6649
6650static int selinux_bpf_map_alloc(struct bpf_map *map)
6651{
6652 struct bpf_security_struct *bpfsec;
6653
6654 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6655 if (!bpfsec)
6656 return -ENOMEM;
6657
6658 bpfsec->sid = current_sid();
6659 map->security = bpfsec;
6660
6661 return 0;
6662}
6663
6664static void selinux_bpf_map_free(struct bpf_map *map)
6665{
6666 struct bpf_security_struct *bpfsec = map->security;
6667
6668 map->security = NULL;
6669 kfree(bpfsec);
6670}
6671
6672static int selinux_bpf_prog_alloc(struct bpf_prog_aux *aux)
6673{
6674 struct bpf_security_struct *bpfsec;
6675
6676 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6677 if (!bpfsec)
6678 return -ENOMEM;
6679
6680 bpfsec->sid = current_sid();
6681 aux->security = bpfsec;
6682
6683 return 0;
6684}
6685
6686static void selinux_bpf_prog_free(struct bpf_prog_aux *aux)
6687{
6688 struct bpf_security_struct *bpfsec = aux->security;
6689
6690 aux->security = NULL;
6691 kfree(bpfsec);
6692}
6693#endif
6694
James Morrisca97d932017-02-15 00:18:51 +11006695static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {
Casey Schauflere20b0432015-05-02 15:11:36 -07006696 LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr),
6697 LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction),
6698 LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder),
6699 LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file),
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02006700
Casey Schauflere20b0432015-05-02 15:11:36 -07006701 LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check),
6702 LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme),
6703 LSM_HOOK_INIT(capget, selinux_capget),
6704 LSM_HOOK_INIT(capset, selinux_capset),
6705 LSM_HOOK_INIT(capable, selinux_capable),
6706 LSM_HOOK_INIT(quotactl, selinux_quotactl),
6707 LSM_HOOK_INIT(quota_on, selinux_quota_on),
6708 LSM_HOOK_INIT(syslog, selinux_syslog),
6709 LSM_HOOK_INIT(vm_enough_memory, selinux_vm_enough_memory),
Stephen Smalley79af7302015-01-21 10:54:10 -05006710
Casey Schauflere20b0432015-05-02 15:11:36 -07006711 LSM_HOOK_INIT(netlink_send, selinux_netlink_send),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006712
Casey Schauflere20b0432015-05-02 15:11:36 -07006713 LSM_HOOK_INIT(bprm_set_creds, selinux_bprm_set_creds),
6714 LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds),
6715 LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006716
Casey Schauflere20b0432015-05-02 15:11:36 -07006717 LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security),
6718 LSM_HOOK_INIT(sb_free_security, selinux_sb_free_security),
Al Viro5b400232018-12-12 20:13:29 -05006719 LSM_HOOK_INIT(sb_eat_lsm_opts, selinux_sb_eat_lsm_opts),
Al Viro204cc0c2018-12-13 13:41:47 -05006720 LSM_HOOK_INIT(sb_free_mnt_opts, selinux_free_mnt_opts),
Casey Schauflere20b0432015-05-02 15:11:36 -07006721 LSM_HOOK_INIT(sb_remount, selinux_sb_remount),
6722 LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount),
6723 LSM_HOOK_INIT(sb_show_options, selinux_sb_show_options),
6724 LSM_HOOK_INIT(sb_statfs, selinux_sb_statfs),
6725 LSM_HOOK_INIT(sb_mount, selinux_mount),
6726 LSM_HOOK_INIT(sb_umount, selinux_umount),
6727 LSM_HOOK_INIT(sb_set_mnt_opts, selinux_set_mnt_opts),
6728 LSM_HOOK_INIT(sb_clone_mnt_opts, selinux_sb_clone_mnt_opts),
6729 LSM_HOOK_INIT(sb_parse_opts_str, selinux_parse_opts_str),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006730
Casey Schauflere20b0432015-05-02 15:11:36 -07006731 LSM_HOOK_INIT(dentry_init_security, selinux_dentry_init_security),
Vivek Goyala518b0a2016-07-13 10:44:53 -04006732 LSM_HOOK_INIT(dentry_create_files_as, selinux_dentry_create_files_as),
Eric Parise0007522008-03-05 10:31:54 -05006733
Casey Schauflere20b0432015-05-02 15:11:36 -07006734 LSM_HOOK_INIT(inode_alloc_security, selinux_inode_alloc_security),
6735 LSM_HOOK_INIT(inode_free_security, selinux_inode_free_security),
6736 LSM_HOOK_INIT(inode_init_security, selinux_inode_init_security),
6737 LSM_HOOK_INIT(inode_create, selinux_inode_create),
6738 LSM_HOOK_INIT(inode_link, selinux_inode_link),
6739 LSM_HOOK_INIT(inode_unlink, selinux_inode_unlink),
6740 LSM_HOOK_INIT(inode_symlink, selinux_inode_symlink),
6741 LSM_HOOK_INIT(inode_mkdir, selinux_inode_mkdir),
6742 LSM_HOOK_INIT(inode_rmdir, selinux_inode_rmdir),
6743 LSM_HOOK_INIT(inode_mknod, selinux_inode_mknod),
6744 LSM_HOOK_INIT(inode_rename, selinux_inode_rename),
6745 LSM_HOOK_INIT(inode_readlink, selinux_inode_readlink),
6746 LSM_HOOK_INIT(inode_follow_link, selinux_inode_follow_link),
6747 LSM_HOOK_INIT(inode_permission, selinux_inode_permission),
6748 LSM_HOOK_INIT(inode_setattr, selinux_inode_setattr),
6749 LSM_HOOK_INIT(inode_getattr, selinux_inode_getattr),
6750 LSM_HOOK_INIT(inode_setxattr, selinux_inode_setxattr),
6751 LSM_HOOK_INIT(inode_post_setxattr, selinux_inode_post_setxattr),
6752 LSM_HOOK_INIT(inode_getxattr, selinux_inode_getxattr),
6753 LSM_HOOK_INIT(inode_listxattr, selinux_inode_listxattr),
6754 LSM_HOOK_INIT(inode_removexattr, selinux_inode_removexattr),
6755 LSM_HOOK_INIT(inode_getsecurity, selinux_inode_getsecurity),
6756 LSM_HOOK_INIT(inode_setsecurity, selinux_inode_setsecurity),
6757 LSM_HOOK_INIT(inode_listsecurity, selinux_inode_listsecurity),
6758 LSM_HOOK_INIT(inode_getsecid, selinux_inode_getsecid),
Vivek Goyal56909eb2016-07-13 10:44:48 -04006759 LSM_HOOK_INIT(inode_copy_up, selinux_inode_copy_up),
Vivek Goyal19472b62016-07-13 10:44:50 -04006760 LSM_HOOK_INIT(inode_copy_up_xattr, selinux_inode_copy_up_xattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006761
Casey Schauflere20b0432015-05-02 15:11:36 -07006762 LSM_HOOK_INIT(file_permission, selinux_file_permission),
6763 LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security),
6764 LSM_HOOK_INIT(file_free_security, selinux_file_free_security),
6765 LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl),
6766 LSM_HOOK_INIT(mmap_file, selinux_mmap_file),
6767 LSM_HOOK_INIT(mmap_addr, selinux_mmap_addr),
6768 LSM_HOOK_INIT(file_mprotect, selinux_file_mprotect),
6769 LSM_HOOK_INIT(file_lock, selinux_file_lock),
6770 LSM_HOOK_INIT(file_fcntl, selinux_file_fcntl),
6771 LSM_HOOK_INIT(file_set_fowner, selinux_file_set_fowner),
6772 LSM_HOOK_INIT(file_send_sigiotask, selinux_file_send_sigiotask),
6773 LSM_HOOK_INIT(file_receive, selinux_file_receive),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006774
Casey Schauflere20b0432015-05-02 15:11:36 -07006775 LSM_HOOK_INIT(file_open, selinux_file_open),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006776
Tetsuo Handaa79be232017-03-28 23:08:45 +09006777 LSM_HOOK_INIT(task_alloc, selinux_task_alloc),
Casey Schauflere20b0432015-05-02 15:11:36 -07006778 LSM_HOOK_INIT(cred_alloc_blank, selinux_cred_alloc_blank),
6779 LSM_HOOK_INIT(cred_free, selinux_cred_free),
6780 LSM_HOOK_INIT(cred_prepare, selinux_cred_prepare),
6781 LSM_HOOK_INIT(cred_transfer, selinux_cred_transfer),
Matthew Garrett3ec30112018-01-08 13:36:19 -08006782 LSM_HOOK_INIT(cred_getsecid, selinux_cred_getsecid),
Casey Schauflere20b0432015-05-02 15:11:36 -07006783 LSM_HOOK_INIT(kernel_act_as, selinux_kernel_act_as),
6784 LSM_HOOK_INIT(kernel_create_files_as, selinux_kernel_create_files_as),
6785 LSM_HOOK_INIT(kernel_module_request, selinux_kernel_module_request),
Mimi Zoharc77b8cd2018-07-13 14:06:02 -04006786 LSM_HOOK_INIT(kernel_load_data, selinux_kernel_load_data),
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07006787 LSM_HOOK_INIT(kernel_read_file, selinux_kernel_read_file),
Casey Schauflere20b0432015-05-02 15:11:36 -07006788 LSM_HOOK_INIT(task_setpgid, selinux_task_setpgid),
6789 LSM_HOOK_INIT(task_getpgid, selinux_task_getpgid),
6790 LSM_HOOK_INIT(task_getsid, selinux_task_getsid),
6791 LSM_HOOK_INIT(task_getsecid, selinux_task_getsecid),
6792 LSM_HOOK_INIT(task_setnice, selinux_task_setnice),
6793 LSM_HOOK_INIT(task_setioprio, selinux_task_setioprio),
6794 LSM_HOOK_INIT(task_getioprio, selinux_task_getioprio),
Stephen Smalley791ec492017-02-17 07:57:00 -05006795 LSM_HOOK_INIT(task_prlimit, selinux_task_prlimit),
Casey Schauflere20b0432015-05-02 15:11:36 -07006796 LSM_HOOK_INIT(task_setrlimit, selinux_task_setrlimit),
6797 LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler),
6798 LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler),
6799 LSM_HOOK_INIT(task_movememory, selinux_task_movememory),
6800 LSM_HOOK_INIT(task_kill, selinux_task_kill),
Casey Schauflere20b0432015-05-02 15:11:36 -07006801 LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode),
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09006802
Casey Schauflere20b0432015-05-02 15:11:36 -07006803 LSM_HOOK_INIT(ipc_permission, selinux_ipc_permission),
6804 LSM_HOOK_INIT(ipc_getsecid, selinux_ipc_getsecid),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006805
Casey Schauflere20b0432015-05-02 15:11:36 -07006806 LSM_HOOK_INIT(msg_msg_alloc_security, selinux_msg_msg_alloc_security),
6807 LSM_HOOK_INIT(msg_msg_free_security, selinux_msg_msg_free_security),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006808
Casey Schauflere20b0432015-05-02 15:11:36 -07006809 LSM_HOOK_INIT(msg_queue_alloc_security,
6810 selinux_msg_queue_alloc_security),
6811 LSM_HOOK_INIT(msg_queue_free_security, selinux_msg_queue_free_security),
6812 LSM_HOOK_INIT(msg_queue_associate, selinux_msg_queue_associate),
6813 LSM_HOOK_INIT(msg_queue_msgctl, selinux_msg_queue_msgctl),
6814 LSM_HOOK_INIT(msg_queue_msgsnd, selinux_msg_queue_msgsnd),
6815 LSM_HOOK_INIT(msg_queue_msgrcv, selinux_msg_queue_msgrcv),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006816
Casey Schauflere20b0432015-05-02 15:11:36 -07006817 LSM_HOOK_INIT(shm_alloc_security, selinux_shm_alloc_security),
6818 LSM_HOOK_INIT(shm_free_security, selinux_shm_free_security),
6819 LSM_HOOK_INIT(shm_associate, selinux_shm_associate),
6820 LSM_HOOK_INIT(shm_shmctl, selinux_shm_shmctl),
6821 LSM_HOOK_INIT(shm_shmat, selinux_shm_shmat),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006822
Casey Schauflere20b0432015-05-02 15:11:36 -07006823 LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security),
6824 LSM_HOOK_INIT(sem_free_security, selinux_sem_free_security),
6825 LSM_HOOK_INIT(sem_associate, selinux_sem_associate),
6826 LSM_HOOK_INIT(sem_semctl, selinux_sem_semctl),
6827 LSM_HOOK_INIT(sem_semop, selinux_sem_semop),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006828
Casey Schauflere20b0432015-05-02 15:11:36 -07006829 LSM_HOOK_INIT(d_instantiate, selinux_d_instantiate),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006830
Casey Schauflere20b0432015-05-02 15:11:36 -07006831 LSM_HOOK_INIT(getprocattr, selinux_getprocattr),
6832 LSM_HOOK_INIT(setprocattr, selinux_setprocattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006833
Casey Schauflere20b0432015-05-02 15:11:36 -07006834 LSM_HOOK_INIT(ismaclabel, selinux_ismaclabel),
6835 LSM_HOOK_INIT(secid_to_secctx, selinux_secid_to_secctx),
6836 LSM_HOOK_INIT(secctx_to_secid, selinux_secctx_to_secid),
6837 LSM_HOOK_INIT(release_secctx, selinux_release_secctx),
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006838 LSM_HOOK_INIT(inode_invalidate_secctx, selinux_inode_invalidate_secctx),
Casey Schauflere20b0432015-05-02 15:11:36 -07006839 LSM_HOOK_INIT(inode_notifysecctx, selinux_inode_notifysecctx),
6840 LSM_HOOK_INIT(inode_setsecctx, selinux_inode_setsecctx),
6841 LSM_HOOK_INIT(inode_getsecctx, selinux_inode_getsecctx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006842
Casey Schauflere20b0432015-05-02 15:11:36 -07006843 LSM_HOOK_INIT(unix_stream_connect, selinux_socket_unix_stream_connect),
6844 LSM_HOOK_INIT(unix_may_send, selinux_socket_unix_may_send),
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006845
Casey Schauflere20b0432015-05-02 15:11:36 -07006846 LSM_HOOK_INIT(socket_create, selinux_socket_create),
6847 LSM_HOOK_INIT(socket_post_create, selinux_socket_post_create),
David Herrmann0b811db2018-05-04 16:28:21 +02006848 LSM_HOOK_INIT(socket_socketpair, selinux_socket_socketpair),
Casey Schauflere20b0432015-05-02 15:11:36 -07006849 LSM_HOOK_INIT(socket_bind, selinux_socket_bind),
6850 LSM_HOOK_INIT(socket_connect, selinux_socket_connect),
6851 LSM_HOOK_INIT(socket_listen, selinux_socket_listen),
6852 LSM_HOOK_INIT(socket_accept, selinux_socket_accept),
6853 LSM_HOOK_INIT(socket_sendmsg, selinux_socket_sendmsg),
6854 LSM_HOOK_INIT(socket_recvmsg, selinux_socket_recvmsg),
6855 LSM_HOOK_INIT(socket_getsockname, selinux_socket_getsockname),
6856 LSM_HOOK_INIT(socket_getpeername, selinux_socket_getpeername),
6857 LSM_HOOK_INIT(socket_getsockopt, selinux_socket_getsockopt),
6858 LSM_HOOK_INIT(socket_setsockopt, selinux_socket_setsockopt),
6859 LSM_HOOK_INIT(socket_shutdown, selinux_socket_shutdown),
6860 LSM_HOOK_INIT(socket_sock_rcv_skb, selinux_socket_sock_rcv_skb),
6861 LSM_HOOK_INIT(socket_getpeersec_stream,
6862 selinux_socket_getpeersec_stream),
6863 LSM_HOOK_INIT(socket_getpeersec_dgram, selinux_socket_getpeersec_dgram),
6864 LSM_HOOK_INIT(sk_alloc_security, selinux_sk_alloc_security),
6865 LSM_HOOK_INIT(sk_free_security, selinux_sk_free_security),
6866 LSM_HOOK_INIT(sk_clone_security, selinux_sk_clone_security),
6867 LSM_HOOK_INIT(sk_getsecid, selinux_sk_getsecid),
6868 LSM_HOOK_INIT(sock_graft, selinux_sock_graft),
Richard Hainesd4529302018-02-13 20:57:18 +00006869 LSM_HOOK_INIT(sctp_assoc_request, selinux_sctp_assoc_request),
6870 LSM_HOOK_INIT(sctp_sk_clone, selinux_sctp_sk_clone),
6871 LSM_HOOK_INIT(sctp_bind_connect, selinux_sctp_bind_connect),
Casey Schauflere20b0432015-05-02 15:11:36 -07006872 LSM_HOOK_INIT(inet_conn_request, selinux_inet_conn_request),
6873 LSM_HOOK_INIT(inet_csk_clone, selinux_inet_csk_clone),
6874 LSM_HOOK_INIT(inet_conn_established, selinux_inet_conn_established),
6875 LSM_HOOK_INIT(secmark_relabel_packet, selinux_secmark_relabel_packet),
6876 LSM_HOOK_INIT(secmark_refcount_inc, selinux_secmark_refcount_inc),
6877 LSM_HOOK_INIT(secmark_refcount_dec, selinux_secmark_refcount_dec),
6878 LSM_HOOK_INIT(req_classify_flow, selinux_req_classify_flow),
6879 LSM_HOOK_INIT(tun_dev_alloc_security, selinux_tun_dev_alloc_security),
6880 LSM_HOOK_INIT(tun_dev_free_security, selinux_tun_dev_free_security),
6881 LSM_HOOK_INIT(tun_dev_create, selinux_tun_dev_create),
6882 LSM_HOOK_INIT(tun_dev_attach_queue, selinux_tun_dev_attach_queue),
6883 LSM_HOOK_INIT(tun_dev_attach, selinux_tun_dev_attach),
6884 LSM_HOOK_INIT(tun_dev_open, selinux_tun_dev_open),
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006885#ifdef CONFIG_SECURITY_INFINIBAND
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006886 LSM_HOOK_INIT(ib_pkey_access, selinux_ib_pkey_access),
Daniel Jurgensab861df2017-05-19 15:48:58 +03006887 LSM_HOOK_INIT(ib_endport_manage_subnet,
6888 selinux_ib_endport_manage_subnet),
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006889 LSM_HOOK_INIT(ib_alloc_security, selinux_ib_alloc_security),
6890 LSM_HOOK_INIT(ib_free_security, selinux_ib_free_security),
6891#endif
Trent Jaegerd28d1e02005-12-13 23:12:40 -08006892#ifdef CONFIG_SECURITY_NETWORK_XFRM
Casey Schauflere20b0432015-05-02 15:11:36 -07006893 LSM_HOOK_INIT(xfrm_policy_alloc_security, selinux_xfrm_policy_alloc),
6894 LSM_HOOK_INIT(xfrm_policy_clone_security, selinux_xfrm_policy_clone),
6895 LSM_HOOK_INIT(xfrm_policy_free_security, selinux_xfrm_policy_free),
6896 LSM_HOOK_INIT(xfrm_policy_delete_security, selinux_xfrm_policy_delete),
6897 LSM_HOOK_INIT(xfrm_state_alloc, selinux_xfrm_state_alloc),
6898 LSM_HOOK_INIT(xfrm_state_alloc_acquire,
6899 selinux_xfrm_state_alloc_acquire),
6900 LSM_HOOK_INIT(xfrm_state_free_security, selinux_xfrm_state_free),
6901 LSM_HOOK_INIT(xfrm_state_delete_security, selinux_xfrm_state_delete),
6902 LSM_HOOK_INIT(xfrm_policy_lookup, selinux_xfrm_policy_lookup),
6903 LSM_HOOK_INIT(xfrm_state_pol_flow_match,
6904 selinux_xfrm_state_pol_flow_match),
6905 LSM_HOOK_INIT(xfrm_decode_session, selinux_xfrm_decode_session),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006906#endif
Michael LeMayd7200242006-06-22 14:47:17 -07006907
6908#ifdef CONFIG_KEYS
Casey Schauflere20b0432015-05-02 15:11:36 -07006909 LSM_HOOK_INIT(key_alloc, selinux_key_alloc),
6910 LSM_HOOK_INIT(key_free, selinux_key_free),
6911 LSM_HOOK_INIT(key_permission, selinux_key_permission),
6912 LSM_HOOK_INIT(key_getsecurity, selinux_key_getsecurity),
Michael LeMayd7200242006-06-22 14:47:17 -07006913#endif
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02006914
6915#ifdef CONFIG_AUDIT
Casey Schauflere20b0432015-05-02 15:11:36 -07006916 LSM_HOOK_INIT(audit_rule_init, selinux_audit_rule_init),
6917 LSM_HOOK_INIT(audit_rule_known, selinux_audit_rule_known),
6918 LSM_HOOK_INIT(audit_rule_match, selinux_audit_rule_match),
6919 LSM_HOOK_INIT(audit_rule_free, selinux_audit_rule_free),
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02006920#endif
Chenbo Fengec27c352017-10-18 13:00:25 -07006921
6922#ifdef CONFIG_BPF_SYSCALL
6923 LSM_HOOK_INIT(bpf, selinux_bpf),
6924 LSM_HOOK_INIT(bpf_map, selinux_bpf_map),
6925 LSM_HOOK_INIT(bpf_prog, selinux_bpf_prog),
6926 LSM_HOOK_INIT(bpf_map_alloc_security, selinux_bpf_map_alloc),
6927 LSM_HOOK_INIT(bpf_prog_alloc_security, selinux_bpf_prog_alloc),
6928 LSM_HOOK_INIT(bpf_map_free_security, selinux_bpf_map_free),
6929 LSM_HOOK_INIT(bpf_prog_free_security, selinux_bpf_prog_free),
6930#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006931};
6932
6933static __init int selinux_init(void)
6934{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07006935 if (!security_module_enable("selinux")) {
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02006936 selinux_enabled = 0;
6937 return 0;
6938 }
6939
Linus Torvalds1da177e2005-04-16 15:20:36 -07006940 if (!selinux_enabled) {
peter enderborgc103a912018-06-12 10:09:03 +02006941 pr_info("SELinux: Disabled at boot.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006942 return 0;
6943 }
6944
peter enderborgc103a912018-06-12 10:09:03 +02006945 pr_info("SELinux: Initializing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006946
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006947 memset(&selinux_state, 0, sizeof(selinux_state));
Paul Mooree5a5ca92018-03-01 17:38:30 -05006948 enforcing_set(&selinux_state, selinux_enforcing_boot);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006949 selinux_state.checkreqprot = selinux_checkreqprot_boot;
6950 selinux_ss_init(&selinux_state.ss);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006951 selinux_avc_init(&selinux_state.avc);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006952
Linus Torvalds1da177e2005-04-16 15:20:36 -07006953 /* Set the security state for the initial task. */
David Howellsd84f4f92008-11-14 10:39:23 +11006954 cred_init_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006955
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04006956 default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
6957
James Morris7cae7e22006-03-22 00:09:22 -08006958 sel_inode_cache = kmem_cache_create("selinux_inode_security",
6959 sizeof(struct inode_security_struct),
Paul Mundt20c2df82007-07-20 10:11:58 +09006960 0, SLAB_PANIC, NULL);
Sangwoo63205652015-10-21 17:44:30 -04006961 file_security_cache = kmem_cache_create("selinux_file_security",
6962 sizeof(struct file_security_struct),
6963 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006964 avc_init();
6965
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006966 avtab_cache_init();
6967
6968 ebitmap_cache_init();
6969
6970 hashtab_cache_init();
6971
Casey Schauflerd69dece52017-01-18 17:09:05 -08006972 security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks), "selinux");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006973
Paul Moore615e51f2014-06-26 14:33:56 -04006974 if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET))
6975 panic("SELinux: Unable to register AVC netcache callback\n");
6976
Daniel Jurgens8f408ab2017-05-19 15:48:53 +03006977 if (avc_add_callback(selinux_lsm_notifier_avc_callback, AVC_CALLBACK_RESET))
6978 panic("SELinux: Unable to register AVC LSM notifier callback\n");
6979
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006980 if (selinux_enforcing_boot)
peter enderborgc103a912018-06-12 10:09:03 +02006981 pr_debug("SELinux: Starting in enforcing mode\n");
Eric Paris828dfe12008-04-17 13:17:49 -04006982 else
peter enderborgc103a912018-06-12 10:09:03 +02006983 pr_debug("SELinux: Starting in permissive mode\n");
Michael LeMayd7200242006-06-22 14:47:17 -07006984
Linus Torvalds1da177e2005-04-16 15:20:36 -07006985 return 0;
6986}
6987
Al Viroe8c26252010-03-23 06:36:54 -04006988static void delayed_superblock_init(struct super_block *sb, void *unused)
6989{
Al Viro204cc0c2018-12-13 13:41:47 -05006990 selinux_set_mnt_opts(sb, NULL, 0, NULL);
Al Viroe8c26252010-03-23 06:36:54 -04006991}
6992
Linus Torvalds1da177e2005-04-16 15:20:36 -07006993void selinux_complete_init(void)
6994{
peter enderborgc103a912018-06-12 10:09:03 +02006995 pr_debug("SELinux: Completing initialization.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006996
6997 /* Set up any superblocks initialized prior to the policy load. */
peter enderborgc103a912018-06-12 10:09:03 +02006998 pr_debug("SELinux: Setting up existing superblocks.\n");
Al Viroe8c26252010-03-23 06:36:54 -04006999 iterate_supers(delayed_superblock_init, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007000}
7001
7002/* SELinux requires early initialization in order to label
7003 all processes and objects when they are created. */
Kees Cook3d6e5f62018-10-10 17:18:23 -07007004DEFINE_LSM(selinux) = {
Kees Cook07aed2f2018-10-10 17:18:24 -07007005 .name = "selinux",
Kees Cook3d6e5f62018-10-10 17:18:23 -07007006 .init = selinux_init,
7007};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007008
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007009#if defined(CONFIG_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007010
Florian Westphal591bb272017-07-26 11:40:52 +02007011static const struct nf_hook_ops selinux_nf_ops[] = {
Paul Mooreeffad8d2008-01-29 08:49:27 -05007012 {
7013 .hook = selinux_ipv4_postroute,
Alban Crequy2597a832012-05-14 03:56:39 +00007014 .pf = NFPROTO_IPV4,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007015 .hooknum = NF_INET_POST_ROUTING,
7016 .priority = NF_IP_PRI_SELINUX_LAST,
7017 },
7018 {
7019 .hook = selinux_ipv4_forward,
Alban Crequy2597a832012-05-14 03:56:39 +00007020 .pf = NFPROTO_IPV4,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007021 .hooknum = NF_INET_FORWARD,
7022 .priority = NF_IP_PRI_SELINUX_FIRST,
Paul Moore948bf852008-10-10 10:16:32 -04007023 },
7024 {
7025 .hook = selinux_ipv4_output,
Alban Crequy2597a832012-05-14 03:56:39 +00007026 .pf = NFPROTO_IPV4,
Paul Moore948bf852008-10-10 10:16:32 -04007027 .hooknum = NF_INET_LOCAL_OUT,
7028 .priority = NF_IP_PRI_SELINUX_FIRST,
Jiri Pirko25db6be2014-09-03 17:42:13 +02007029 },
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04007030#if IS_ENABLED(CONFIG_IPV6)
Paul Mooreeffad8d2008-01-29 08:49:27 -05007031 {
7032 .hook = selinux_ipv6_postroute,
Alban Crequy2597a832012-05-14 03:56:39 +00007033 .pf = NFPROTO_IPV6,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007034 .hooknum = NF_INET_POST_ROUTING,
7035 .priority = NF_IP6_PRI_SELINUX_LAST,
7036 },
7037 {
7038 .hook = selinux_ipv6_forward,
Alban Crequy2597a832012-05-14 03:56:39 +00007039 .pf = NFPROTO_IPV6,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007040 .hooknum = NF_INET_FORWARD,
7041 .priority = NF_IP6_PRI_SELINUX_FIRST,
Jiri Pirko25db6be2014-09-03 17:42:13 +02007042 },
Huw Davies2917f572016-06-27 15:06:15 -04007043 {
7044 .hook = selinux_ipv6_output,
7045 .pf = NFPROTO_IPV6,
7046 .hooknum = NF_INET_LOCAL_OUT,
7047 .priority = NF_IP6_PRI_SELINUX_FIRST,
7048 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07007049#endif /* IPV6 */
Jiri Pirko25db6be2014-09-03 17:42:13 +02007050};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007051
Florian Westphal8e71bf72017-04-21 11:49:09 +02007052static int __net_init selinux_nf_register(struct net *net)
7053{
7054 return nf_register_net_hooks(net, selinux_nf_ops,
7055 ARRAY_SIZE(selinux_nf_ops));
7056}
7057
7058static void __net_exit selinux_nf_unregister(struct net *net)
7059{
7060 nf_unregister_net_hooks(net, selinux_nf_ops,
7061 ARRAY_SIZE(selinux_nf_ops));
7062}
7063
7064static struct pernet_operations selinux_net_ops = {
7065 .init = selinux_nf_register,
7066 .exit = selinux_nf_unregister,
7067};
7068
Linus Torvalds1da177e2005-04-16 15:20:36 -07007069static int __init selinux_nf_ip_init(void)
7070{
Jiri Pirko25db6be2014-09-03 17:42:13 +02007071 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007072
7073 if (!selinux_enabled)
Jiri Pirko25db6be2014-09-03 17:42:13 +02007074 return 0;
Eric Parisfadcdb42007-02-22 18:11:31 -05007075
peter enderborgc103a912018-06-12 10:09:03 +02007076 pr_debug("SELinux: Registering netfilter hooks\n");
Eric Parisfadcdb42007-02-22 18:11:31 -05007077
Florian Westphal8e71bf72017-04-21 11:49:09 +02007078 err = register_pernet_subsys(&selinux_net_ops);
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07007079 if (err)
Florian Westphal8e71bf72017-04-21 11:49:09 +02007080 panic("SELinux: register_pernet_subsys: error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007081
Jiri Pirko25db6be2014-09-03 17:42:13 +02007082 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007083}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007084__initcall(selinux_nf_ip_init);
7085
7086#ifdef CONFIG_SECURITY_SELINUX_DISABLE
7087static void selinux_nf_ip_exit(void)
7088{
peter enderborgc103a912018-06-12 10:09:03 +02007089 pr_debug("SELinux: Unregistering netfilter hooks\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007090
Florian Westphal8e71bf72017-04-21 11:49:09 +02007091 unregister_pernet_subsys(&selinux_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007092}
7093#endif
7094
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007095#else /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007096
7097#ifdef CONFIG_SECURITY_SELINUX_DISABLE
7098#define selinux_nf_ip_exit()
7099#endif
7100
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007101#endif /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007102
7103#ifdef CONFIG_SECURITY_SELINUX_DISABLE
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007104int selinux_disable(struct selinux_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007105{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007106 if (state->initialized) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007107 /* Not permitted after initial policy load. */
7108 return -EINVAL;
7109 }
7110
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007111 if (state->disabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007112 /* Only do this once. */
7113 return -EINVAL;
7114 }
7115
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007116 state->disabled = 1;
7117
peter enderborgc103a912018-06-12 10:09:03 +02007118 pr_info("SELinux: Disabled at runtime.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007119
Stephen Smalley30d55282006-05-03 10:52:36 -04007120 selinux_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007121
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07007122 security_delete_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007123
Eric Parisaf8ff042009-09-20 21:23:01 -04007124 /* Try to destroy the avc node cache */
7125 avc_disable();
7126
Linus Torvalds1da177e2005-04-16 15:20:36 -07007127 /* Unregister netfilter hooks. */
7128 selinux_nf_ip_exit();
7129
7130 /* Unregister selinuxfs. */
7131 exit_sel_fs();
7132
7133 return 0;
7134}
7135#endif