blob: 7f6068489a02499a6acafeacc783a7ce3f121300 [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)
Eric Parisc9180a52007-11-30 13:00:35 -05001010{
Al Viroba641862018-12-14 20:28:15 -05001011 struct selinux_mnt_opts *opts = *mnt_opts;
Eric Parisc9180a52007-11-30 13:00:35 -05001012
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;
Eric Parisc9180a52007-11-30 13:00:35 -05001015
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}
Eric Parisc9180a52007-11-30 13:00:35 -05001051
Al Viro757cbe52018-12-14 23:42:21 -05001052static int selinux_add_mnt_opt(const char *option, const char *val, int len,
1053 void **mnt_opts)
Eric Parisc9180a52007-11-30 13:00:35 -05001054{
Al Viro757cbe52018-12-14 23:42:21 -05001055 int token = Opt_error;
1056 int rc, i;
Eric Parisc9180a52007-11-30 13:00:35 -05001057
Al Viro757cbe52018-12-14 23:42:21 -05001058 for (i = 0; i < ARRAY_SIZE(tokens); i++) {
1059 if (strcmp(option, tokens[i].name) == 0) {
1060 token = tokens[i].opt;
Eric Parisc9180a52007-11-30 13:00:35 -05001061 break;
Eric Parisc9180a52007-11-30 13:00:35 -05001062 }
1063 }
1064
Al Viro757cbe52018-12-14 23:42:21 -05001065 if (token == Opt_error)
1066 return -EINVAL;
Eric Parise0007522008-03-05 10:31:54 -05001067
Al Viro757cbe52018-12-14 23:42:21 -05001068 if (token != Opt_seclabel)
1069 val = kmemdup_nul(val, len, GFP_KERNEL);
1070 rc = selinux_add_opt(token, val, mnt_opts);
1071 if (unlikely(rc)) {
1072 kfree(val);
1073 if (*mnt_opts) {
1074 selinux_free_mnt_opts(*mnt_opts);
1075 *mnt_opts = NULL;
1076 }
1077 }
1078 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Al Viroe3489f82018-12-13 00:24:36 -05001081static int show_sid(struct seq_file *m, u32 sid)
Eric Paris2069f452008-07-04 09:47:13 +10001082{
Al Viroe3489f82018-12-13 00:24:36 -05001083 char *context = NULL;
1084 u32 len;
1085 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Al Viroe3489f82018-12-13 00:24:36 -05001087 rc = security_sid_to_context(&selinux_state, sid,
1088 &context, &len);
1089 if (!rc) {
1090 bool has_comma = context && strchr(context, ',');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Eric Paris2069f452008-07-04 09:47:13 +10001092 if (has_comma)
1093 seq_putc(m, '\"');
Al Viroe3489f82018-12-13 00:24:36 -05001094 seq_escape(m, context, "\"\n\\");
Eric Paris2069f452008-07-04 09:47:13 +10001095 if (has_comma)
1096 seq_putc(m, '\"');
1097 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 kfree(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 return rc;
1100}
Eric Paris2069f452008-07-04 09:47:13 +10001101
1102static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
1103{
Al Viroe3489f82018-12-13 00:24:36 -05001104 struct superblock_security_struct *sbsec = sb->s_security;
Eric Paris2069f452008-07-04 09:47:13 +10001105 int rc;
1106
Al Viroe3489f82018-12-13 00:24:36 -05001107 if (!(sbsec->flags & SE_SBINITIALIZED))
1108 return 0;
1109
1110 if (!selinux_state.initialized)
1111 return 0;
1112
1113 if (sbsec->flags & FSCONTEXT_MNT) {
1114 seq_putc(m, ',');
1115 seq_puts(m, FSCONTEXT_STR);
1116 rc = show_sid(m, sbsec->sid);
1117 if (rc)
1118 return rc;
Eric Paris383795c2008-07-29 17:07:26 -04001119 }
Al Viroe3489f82018-12-13 00:24:36 -05001120 if (sbsec->flags & CONTEXT_MNT) {
1121 seq_putc(m, ',');
1122 seq_puts(m, CONTEXT_STR);
1123 rc = show_sid(m, sbsec->mntpoint_sid);
1124 if (rc)
1125 return rc;
1126 }
1127 if (sbsec->flags & DEFCONTEXT_MNT) {
1128 seq_putc(m, ',');
1129 seq_puts(m, DEFCONTEXT_STR);
1130 rc = show_sid(m, sbsec->def_sid);
1131 if (rc)
1132 return rc;
1133 }
1134 if (sbsec->flags & ROOTCONTEXT_MNT) {
1135 struct dentry *root = sbsec->sb->s_root;
1136 struct inode_security_struct *isec = backing_inode_security(root);
1137 seq_putc(m, ',');
1138 seq_puts(m, ROOTCONTEXT_STR);
1139 rc = show_sid(m, isec->sid);
1140 if (rc)
1141 return rc;
1142 }
1143 if (sbsec->flags & SBLABEL_MNT) {
1144 seq_putc(m, ',');
1145 seq_puts(m, LABELSUPP_STR);
1146 }
1147 return 0;
Eric Paris2069f452008-07-04 09:47:13 +10001148}
1149
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150static inline u16 inode_mode_to_security_class(umode_t mode)
1151{
1152 switch (mode & S_IFMT) {
1153 case S_IFSOCK:
1154 return SECCLASS_SOCK_FILE;
1155 case S_IFLNK:
1156 return SECCLASS_LNK_FILE;
1157 case S_IFREG:
1158 return SECCLASS_FILE;
1159 case S_IFBLK:
1160 return SECCLASS_BLK_FILE;
1161 case S_IFDIR:
1162 return SECCLASS_DIR;
1163 case S_IFCHR:
1164 return SECCLASS_CHR_FILE;
1165 case S_IFIFO:
1166 return SECCLASS_FIFO_FILE;
1167
1168 }
1169
1170 return SECCLASS_FILE;
1171}
1172
James Morris13402582005-09-30 14:24:34 -04001173static inline int default_protocol_stream(int protocol)
1174{
1175 return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP);
1176}
1177
1178static inline int default_protocol_dgram(int protocol)
1179{
1180 return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
1181}
1182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183static inline u16 socket_type_to_security_class(int family, int type, int protocol)
1184{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001185 int extsockclass = selinux_policycap_extsockclass();
Stephen Smalleyda69a532017-01-09 10:07:30 -05001186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 switch (family) {
1188 case PF_UNIX:
1189 switch (type) {
1190 case SOCK_STREAM:
1191 case SOCK_SEQPACKET:
1192 return SECCLASS_UNIX_STREAM_SOCKET;
1193 case SOCK_DGRAM:
Luis Ressel2a764b52017-07-25 15:13:41 -04001194 case SOCK_RAW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 return SECCLASS_UNIX_DGRAM_SOCKET;
1196 }
1197 break;
1198 case PF_INET:
1199 case PF_INET6:
1200 switch (type) {
1201 case SOCK_STREAM:
Stephen Smalleyda69a532017-01-09 10:07:30 -05001202 case SOCK_SEQPACKET:
James Morris13402582005-09-30 14:24:34 -04001203 if (default_protocol_stream(protocol))
1204 return SECCLASS_TCP_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001205 else if (extsockclass && protocol == IPPROTO_SCTP)
1206 return SECCLASS_SCTP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001207 else
1208 return SECCLASS_RAWIP_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 case SOCK_DGRAM:
James Morris13402582005-09-30 14:24:34 -04001210 if (default_protocol_dgram(protocol))
1211 return SECCLASS_UDP_SOCKET;
Stephen Smalleyef379792017-01-09 10:07:31 -05001212 else if (extsockclass && (protocol == IPPROTO_ICMP ||
1213 protocol == IPPROTO_ICMPV6))
Stephen Smalleyda69a532017-01-09 10:07:30 -05001214 return SECCLASS_ICMP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001215 else
1216 return SECCLASS_RAWIP_SOCKET;
James Morris2ee92d42006-11-13 16:09:01 -08001217 case SOCK_DCCP:
1218 return SECCLASS_DCCP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001219 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 return SECCLASS_RAWIP_SOCKET;
1221 }
1222 break;
1223 case PF_NETLINK:
1224 switch (protocol) {
1225 case NETLINK_ROUTE:
1226 return SECCLASS_NETLINK_ROUTE_SOCKET;
Pavel Emelyanov7f1fb602011-12-06 07:56:43 +00001227 case NETLINK_SOCK_DIAG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 return SECCLASS_NETLINK_TCPDIAG_SOCKET;
1229 case NETLINK_NFLOG:
1230 return SECCLASS_NETLINK_NFLOG_SOCKET;
1231 case NETLINK_XFRM:
1232 return SECCLASS_NETLINK_XFRM_SOCKET;
1233 case NETLINK_SELINUX:
1234 return SECCLASS_NETLINK_SELINUX_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001235 case NETLINK_ISCSI:
1236 return SECCLASS_NETLINK_ISCSI_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 case NETLINK_AUDIT:
1238 return SECCLASS_NETLINK_AUDIT_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001239 case NETLINK_FIB_LOOKUP:
1240 return SECCLASS_NETLINK_FIB_LOOKUP_SOCKET;
1241 case NETLINK_CONNECTOR:
1242 return SECCLASS_NETLINK_CONNECTOR_SOCKET;
1243 case NETLINK_NETFILTER:
1244 return SECCLASS_NETLINK_NETFILTER_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 case NETLINK_DNRTMSG:
1246 return SECCLASS_NETLINK_DNRT_SOCKET;
James Morris0c9b7942005-04-16 15:24:13 -07001247 case NETLINK_KOBJECT_UEVENT:
1248 return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001249 case NETLINK_GENERIC:
1250 return SECCLASS_NETLINK_GENERIC_SOCKET;
1251 case NETLINK_SCSITRANSPORT:
1252 return SECCLASS_NETLINK_SCSITRANSPORT_SOCKET;
1253 case NETLINK_RDMA:
1254 return SECCLASS_NETLINK_RDMA_SOCKET;
1255 case NETLINK_CRYPTO:
1256 return SECCLASS_NETLINK_CRYPTO_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 default:
1258 return SECCLASS_NETLINK_SOCKET;
1259 }
1260 case PF_PACKET:
1261 return SECCLASS_PACKET_SOCKET;
1262 case PF_KEY:
1263 return SECCLASS_KEY_SOCKET;
Christopher J. PeBenito3e3ff152006-06-09 00:25:03 -07001264 case PF_APPLETALK:
1265 return SECCLASS_APPLETALK_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 }
1267
Stephen Smalleyda69a532017-01-09 10:07:30 -05001268 if (extsockclass) {
1269 switch (family) {
1270 case PF_AX25:
1271 return SECCLASS_AX25_SOCKET;
1272 case PF_IPX:
1273 return SECCLASS_IPX_SOCKET;
1274 case PF_NETROM:
1275 return SECCLASS_NETROM_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001276 case PF_ATMPVC:
1277 return SECCLASS_ATMPVC_SOCKET;
1278 case PF_X25:
1279 return SECCLASS_X25_SOCKET;
1280 case PF_ROSE:
1281 return SECCLASS_ROSE_SOCKET;
1282 case PF_DECnet:
1283 return SECCLASS_DECNET_SOCKET;
1284 case PF_ATMSVC:
1285 return SECCLASS_ATMSVC_SOCKET;
1286 case PF_RDS:
1287 return SECCLASS_RDS_SOCKET;
1288 case PF_IRDA:
1289 return SECCLASS_IRDA_SOCKET;
1290 case PF_PPPOX:
1291 return SECCLASS_PPPOX_SOCKET;
1292 case PF_LLC:
1293 return SECCLASS_LLC_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001294 case PF_CAN:
1295 return SECCLASS_CAN_SOCKET;
1296 case PF_TIPC:
1297 return SECCLASS_TIPC_SOCKET;
1298 case PF_BLUETOOTH:
1299 return SECCLASS_BLUETOOTH_SOCKET;
1300 case PF_IUCV:
1301 return SECCLASS_IUCV_SOCKET;
1302 case PF_RXRPC:
1303 return SECCLASS_RXRPC_SOCKET;
1304 case PF_ISDN:
1305 return SECCLASS_ISDN_SOCKET;
1306 case PF_PHONET:
1307 return SECCLASS_PHONET_SOCKET;
1308 case PF_IEEE802154:
1309 return SECCLASS_IEEE802154_SOCKET;
1310 case PF_CAIF:
1311 return SECCLASS_CAIF_SOCKET;
1312 case PF_ALG:
1313 return SECCLASS_ALG_SOCKET;
1314 case PF_NFC:
1315 return SECCLASS_NFC_SOCKET;
1316 case PF_VSOCK:
1317 return SECCLASS_VSOCK_SOCKET;
1318 case PF_KCM:
1319 return SECCLASS_KCM_SOCKET;
1320 case PF_QIPCRTR:
1321 return SECCLASS_QIPCRTR_SOCKET;
Linus Torvalds3051bf32017-02-22 10:15:09 -08001322 case PF_SMC:
1323 return SECCLASS_SMC_SOCKET;
Björn Töpel68e8b842018-05-02 13:01:22 +02001324 case PF_XDP:
1325 return SECCLASS_XDP_SOCKET;
1326#if PF_MAX > 45
Stephen Smalleyda69a532017-01-09 10:07:30 -05001327#error New address family defined, please update this function.
1328#endif
1329 }
1330 }
1331
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 return SECCLASS_SOCKET;
1333}
1334
Stephen Smalley134509d2015-06-04 16:22:17 -04001335static int selinux_genfs_get_sid(struct dentry *dentry,
1336 u16 tclass,
1337 u16 flags,
1338 u32 *sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001340 int rc;
Al Virofc640052016-04-10 01:33:30 -04001341 struct super_block *sb = dentry->d_sb;
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001342 char *buffer, *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Eric Paris828dfe12008-04-17 13:17:49 -04001344 buffer = (char *)__get_free_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 if (!buffer)
1346 return -ENOMEM;
1347
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001348 path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
1349 if (IS_ERR(path))
1350 rc = PTR_ERR(path);
1351 else {
Stephen Smalley134509d2015-06-04 16:22:17 -04001352 if (flags & SE_SBPROC) {
1353 /* each process gets a /proc/PID/ entry. Strip off the
1354 * PID part to get a valid selinux labeling.
1355 * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
1356 while (path[1] >= '0' && path[1] <= '9') {
1357 path[1] = '/';
1358 path++;
1359 }
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001360 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001361 rc = security_genfs_sid(&selinux_state, sb->s_type->name,
1362 path, tclass, sid);
Stephen Smalley7bb185e2018-09-04 16:51:36 -04001363 if (rc == -ENOENT) {
1364 /* No match in policy, mark as unlabeled. */
1365 *sid = SECINITSID_UNLABELED;
1366 rc = 0;
1367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 free_page((unsigned long)buffer);
1370 return rc;
1371}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
1373/* The inode's security attributes must be initialized before first use. */
1374static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
1375{
1376 struct superblock_security_struct *sbsec = NULL;
1377 struct inode_security_struct *isec = inode->i_security;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001378 u32 task_sid, sid = 0;
1379 u16 sclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 struct dentry *dentry;
1381#define INITCONTEXTLEN 255
1382 char *context = NULL;
1383 unsigned len = 0;
1384 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05001386 if (isec->initialized == LABEL_INITIALIZED)
Andreas Gruenbacher13457d02016-11-10 22:18:29 +01001387 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001389 spin_lock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05001390 if (isec->initialized == LABEL_INITIALIZED)
Eric Paris23970742006-09-25 23:32:01 -07001391 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Andreas Gruenbacher13457d02016-11-10 22:18:29 +01001393 if (isec->sclass == SECCLASS_FILE)
1394 isec->sclass = inode_mode_to_security_class(inode->i_mode);
1395
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 sbsec = inode->i_sb->s_security;
David P. Quigley0d90a7e2009-01-16 09:22:02 -05001397 if (!(sbsec->flags & SE_SBINITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 /* Defer initialization until selinux_complete_init,
1399 after the initial policy is loaded and the security
1400 server is ready to handle calls. */
1401 spin_lock(&sbsec->isec_lock);
1402 if (list_empty(&isec->list))
1403 list_add(&isec->list, &sbsec->isec_head);
1404 spin_unlock(&sbsec->isec_lock);
Eric Paris23970742006-09-25 23:32:01 -07001405 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 }
1407
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001408 sclass = isec->sclass;
1409 task_sid = isec->task_sid;
1410 sid = isec->sid;
1411 isec->initialized = LABEL_PENDING;
1412 spin_unlock(&isec->lock);
1413
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 switch (sbsec->behavior) {
David Quigleyeb9ae682013-05-22 12:50:37 -04001415 case SECURITY_FS_USE_NATIVE:
1416 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 case SECURITY_FS_USE_XATTR:
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001418 if (!(inode->i_opflags & IOP_XATTR)) {
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001419 sid = sbsec->def_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 break;
1421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 /* Need a dentry, since the xattr API requires one.
1423 Life would be simpler if we could just pass the inode. */
1424 if (opt_dentry) {
1425 /* Called from d_instantiate or d_splice_alias. */
1426 dentry = dget(opt_dentry);
1427 } else {
Al Virob1271252018-04-25 10:28:38 -04001428 /*
1429 * Called from selinux_complete_init, try to find a dentry.
1430 * Some filesystems really want a connected one, so try
1431 * that first. We could split SECURITY_FS_USE_XATTR in
1432 * two, depending upon that...
1433 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 dentry = d_find_alias(inode);
Al Virob1271252018-04-25 10:28:38 -04001435 if (!dentry)
1436 dentry = d_find_any_alias(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 }
1438 if (!dentry) {
Eric Parisdf7f54c2009-03-09 14:35:58 -04001439 /*
1440 * this is can be hit on boot when a file is accessed
1441 * before the policy is loaded. When we load policy we
1442 * may find inodes that have no dentry on the
1443 * sbsec->isec_head list. No reason to complain as these
1444 * will get fixed up the next time we go through
1445 * inode_doinit with a dentry, before these inodes could
1446 * be used again by userspace.
1447 */
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001448 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 }
1450
1451 len = INITCONTEXTLEN;
Eric Paris4cb912f2009-02-12 14:50:05 -05001452 context = kmalloc(len+1, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 if (!context) {
1454 rc = -ENOMEM;
1455 dput(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001456 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 }
Eric Paris4cb912f2009-02-12 14:50:05 -05001458 context[len] = '\0';
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001459 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 if (rc == -ERANGE) {
James Morris314dabb2009-08-10 22:00:13 +10001461 kfree(context);
1462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 /* Need a larger buffer. Query for the right size. */
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001464 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 if (rc < 0) {
1466 dput(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001467 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 len = rc;
Eric Paris4cb912f2009-02-12 14:50:05 -05001470 context = kmalloc(len+1, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 if (!context) {
1472 rc = -ENOMEM;
1473 dput(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001474 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 }
Eric Paris4cb912f2009-02-12 14:50:05 -05001476 context[len] = '\0';
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001477 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 }
1479 dput(dentry);
1480 if (rc < 0) {
1481 if (rc != -ENODATA) {
peter enderborgc103a912018-06-12 10:09:03 +02001482 pr_warn("SELinux: %s: getxattr returned "
Harvey Harrisondd6f9532008-03-06 10:03:59 +11001483 "%d for dev=%s ino=%ld\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 -rc, inode->i_sb->s_id, inode->i_ino);
1485 kfree(context);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001486 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 }
1488 /* Map ENODATA to the default file SID */
1489 sid = sbsec->def_sid;
1490 rc = 0;
1491 } else {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001492 rc = security_context_to_sid_default(&selinux_state,
1493 context, rc, &sid,
Stephen Smalley869ab512008-04-04 08:46:05 -04001494 sbsec->def_sid,
1495 GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 if (rc) {
Eric Paris4ba0a8a2009-02-12 15:01:10 -05001497 char *dev = inode->i_sb->s_id;
1498 unsigned long ino = inode->i_ino;
1499
1500 if (rc == -EINVAL) {
1501 if (printk_ratelimit())
peter enderborgc103a912018-06-12 10:09:03 +02001502 pr_notice("SELinux: inode=%lu on dev=%s was found to have an invalid "
Eric Paris4ba0a8a2009-02-12 15:01:10 -05001503 "context=%s. This indicates you may need to relabel the inode or the "
1504 "filesystem in question.\n", ino, dev, context);
1505 } else {
peter enderborgc103a912018-06-12 10:09:03 +02001506 pr_warn("SELinux: %s: context_to_sid(%s) "
Eric Paris4ba0a8a2009-02-12 15:01:10 -05001507 "returned %d for dev=%s ino=%ld\n",
1508 __func__, context, -rc, dev, ino);
1509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 kfree(context);
1511 /* Leave with the unlabeled SID */
1512 rc = 0;
1513 break;
1514 }
1515 }
1516 kfree(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 break;
1518 case SECURITY_FS_USE_TASK:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001519 sid = task_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 break;
1521 case SECURITY_FS_USE_TRANS:
1522 /* Default to the fs SID. */
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001523 sid = sbsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
1525 /* Try to obtain a transition SID. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001526 rc = security_transition_sid(&selinux_state, task_sid, sid,
1527 sclass, NULL, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 if (rc)
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001529 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 break;
Eric Parisc312feb2006-07-10 04:43:53 -07001531 case SECURITY_FS_USE_MNTPOINT:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001532 sid = sbsec->mntpoint_sid;
Eric Parisc312feb2006-07-10 04:43:53 -07001533 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 default:
Eric Parisc312feb2006-07-10 04:43:53 -07001535 /* Default to the fs superblock SID. */
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001536 sid = sbsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
Stephen Smalley134509d2015-06-04 16:22:17 -04001538 if ((sbsec->flags & SE_SBGENFS) && !S_ISLNK(inode->i_mode)) {
Paul Mooref64410e2014-03-19 16:46:18 -04001539 /* We must have a dentry to determine the label on
1540 * procfs inodes */
Al Virob1271252018-04-25 10:28:38 -04001541 if (opt_dentry) {
Paul Mooref64410e2014-03-19 16:46:18 -04001542 /* Called from d_instantiate or
1543 * d_splice_alias. */
1544 dentry = dget(opt_dentry);
Al Virob1271252018-04-25 10:28:38 -04001545 } else {
Paul Mooref64410e2014-03-19 16:46:18 -04001546 /* Called from selinux_complete_init, try to
Al Virob1271252018-04-25 10:28:38 -04001547 * find a dentry. Some filesystems really want
1548 * a connected one, so try that first.
1549 */
Paul Mooref64410e2014-03-19 16:46:18 -04001550 dentry = d_find_alias(inode);
Al Virob1271252018-04-25 10:28:38 -04001551 if (!dentry)
1552 dentry = d_find_any_alias(inode);
1553 }
Paul Mooref64410e2014-03-19 16:46:18 -04001554 /*
1555 * This can be hit on boot when a file is accessed
1556 * before the policy is loaded. When we load policy we
1557 * may find inodes that have no dentry on the
1558 * sbsec->isec_head list. No reason to complain as
1559 * these will get fixed up the next time we go through
1560 * inode_doinit() with a dentry, before these inodes
1561 * could be used again by userspace.
1562 */
1563 if (!dentry)
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001564 goto out;
1565 rc = selinux_genfs_get_sid(dentry, sclass,
Stephen Smalley134509d2015-06-04 16:22:17 -04001566 sbsec->flags, &sid);
Paul Mooref64410e2014-03-19 16:46:18 -04001567 dput(dentry);
1568 if (rc)
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001569 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 }
1571 break;
1572 }
1573
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001574out:
1575 spin_lock(&isec->lock);
1576 if (isec->initialized == LABEL_PENDING) {
1577 if (!sid || rc) {
1578 isec->initialized = LABEL_INVALID;
1579 goto out_unlock;
1580 }
1581
1582 isec->initialized = LABEL_INITIALIZED;
1583 isec->sid = sid;
1584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Eric Paris23970742006-09-25 23:32:01 -07001586out_unlock:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001587 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 return rc;
1589}
1590
1591/* Convert a Linux signal to an access vector. */
1592static inline u32 signal_to_av(int sig)
1593{
1594 u32 perm = 0;
1595
1596 switch (sig) {
1597 case SIGCHLD:
1598 /* Commonly granted from child to parent. */
1599 perm = PROCESS__SIGCHLD;
1600 break;
1601 case SIGKILL:
1602 /* Cannot be caught or ignored */
1603 perm = PROCESS__SIGKILL;
1604 break;
1605 case SIGSTOP:
1606 /* Cannot be caught or ignored */
1607 perm = PROCESS__SIGSTOP;
1608 break;
1609 default:
1610 /* All other signals. */
1611 perm = PROCESS__SIGNAL;
1612 break;
1613 }
1614
1615 return perm;
1616}
1617
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001618#if CAP_LAST_CAP > 63
1619#error Fix SELinux to handle capabilities > 63.
1620#endif
1621
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622/* Check whether a task is allowed to use a capability. */
Eric Paris6a9de492012-01-03 12:25:14 -05001623static int cred_has_capability(const struct cred *cred,
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04001624 int cap, int audit, bool initns)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625{
Thomas Liu2bf49692009-07-14 12:14:09 -04001626 struct common_audit_data ad;
Eric Paris06112162008-11-11 22:02:50 +11001627 struct av_decision avd;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001628 u16 sclass;
David Howells3699c532009-01-06 22:27:01 +00001629 u32 sid = cred_sid(cred);
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001630 u32 av = CAP_TO_MASK(cap);
Eric Paris06112162008-11-11 22:02:50 +11001631 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
Eric Paris50c205f2012-04-04 15:01:43 -04001633 ad.type = LSM_AUDIT_DATA_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 ad.u.cap = cap;
1635
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001636 switch (CAP_TO_INDEX(cap)) {
1637 case 0:
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04001638 sclass = initns ? SECCLASS_CAPABILITY : SECCLASS_CAP_USERNS;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001639 break;
1640 case 1:
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04001641 sclass = initns ? SECCLASS_CAPABILITY2 : SECCLASS_CAP2_USERNS;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001642 break;
1643 default:
peter enderborgc103a912018-06-12 10:09:03 +02001644 pr_err("SELinux: out of range capability %d\n", cap);
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001645 BUG();
Eric Parisa35c6c832011-04-20 10:21:28 -04001646 return -EINVAL;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001647 }
Eric Paris06112162008-11-11 22:02:50 +11001648
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001649 rc = avc_has_perm_noaudit(&selinux_state,
1650 sid, sid, sclass, av, 0, &avd);
Eric Paris9ade0cf2011-04-25 16:26:29 -04001651 if (audit == SECURITY_CAP_AUDIT) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001652 int rc2 = avc_audit(&selinux_state,
1653 sid, sid, sclass, av, &avd, rc, &ad, 0);
Eric Paris9ade0cf2011-04-25 16:26:29 -04001654 if (rc2)
1655 return rc2;
1656 }
Eric Paris06112162008-11-11 22:02:50 +11001657 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658}
1659
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660/* Check whether a task has a particular permission to an inode.
1661 The 'adp' parameter is optional and allows other audit
1662 data to be passed (e.g. the dentry). */
David Howells88e67f32008-11-14 10:39:21 +11001663static int inode_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 struct inode *inode,
1665 u32 perms,
Linus Torvalds19e49832013-10-04 12:54:11 -07001666 struct common_audit_data *adp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 struct inode_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +11001669 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
David Howellse0e81732009-09-02 09:13:40 +01001671 validate_creds(cred);
1672
Eric Paris828dfe12008-04-17 13:17:49 -04001673 if (unlikely(IS_PRIVATE(inode)))
Stephen Smalleybbaca6c2007-02-14 00:34:16 -08001674 return 0;
1675
David Howells88e67f32008-11-14 10:39:21 +11001676 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 isec = inode->i_security;
1678
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001679 return avc_has_perm(&selinux_state,
1680 sid, isec->sid, isec->sclass, perms, adp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681}
1682
1683/* Same as inode_has_perm, but pass explicit audit data containing
1684 the dentry to help the auditing code to more easily generate the
1685 pathname if needed. */
David Howells88e67f32008-11-14 10:39:21 +11001686static inline int dentry_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 struct dentry *dentry,
1688 u32 av)
1689{
David Howellsc6f493d2015-03-17 22:26:22 +00001690 struct inode *inode = d_backing_inode(dentry);
Thomas Liu2bf49692009-07-14 12:14:09 -04001691 struct common_audit_data ad;
David Howells88e67f32008-11-14 10:39:21 +11001692
Eric Paris50c205f2012-04-04 15:01:43 -04001693 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Paris2875fa02011-04-28 16:04:24 -04001694 ad.u.dentry = dentry;
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05001695 __inode_security_revalidate(inode, dentry, true);
Linus Torvalds19e49832013-10-04 12:54:11 -07001696 return inode_has_perm(cred, inode, av, &ad);
Eric Paris2875fa02011-04-28 16:04:24 -04001697}
1698
1699/* Same as inode_has_perm, but pass explicit audit data containing
1700 the path to help the auditing code to more easily generate the
1701 pathname if needed. */
1702static inline int path_has_perm(const struct cred *cred,
Al Viro3f7036a2015-03-08 19:28:30 -04001703 const struct path *path,
Eric Paris2875fa02011-04-28 16:04:24 -04001704 u32 av)
1705{
David Howellsc6f493d2015-03-17 22:26:22 +00001706 struct inode *inode = d_backing_inode(path->dentry);
Eric Paris2875fa02011-04-28 16:04:24 -04001707 struct common_audit_data ad;
1708
Eric Paris50c205f2012-04-04 15:01:43 -04001709 ad.type = LSM_AUDIT_DATA_PATH;
Eric Paris2875fa02011-04-28 16:04:24 -04001710 ad.u.path = *path;
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05001711 __inode_security_revalidate(inode, path->dentry, true);
Linus Torvalds19e49832013-10-04 12:54:11 -07001712 return inode_has_perm(cred, inode, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713}
1714
David Howells13f8e982013-06-13 23:37:55 +01001715/* Same as path_has_perm, but uses the inode from the file struct. */
1716static inline int file_path_has_perm(const struct cred *cred,
1717 struct file *file,
1718 u32 av)
1719{
1720 struct common_audit_data ad;
1721
Vivek Goyal43af5de2016-09-09 11:37:49 -04001722 ad.type = LSM_AUDIT_DATA_FILE;
1723 ad.u.file = file;
Linus Torvalds19e49832013-10-04 12:54:11 -07001724 return inode_has_perm(cred, file_inode(file), av, &ad);
David Howells13f8e982013-06-13 23:37:55 +01001725}
1726
Chenbo Fengf66e4482017-10-18 13:00:26 -07001727#ifdef CONFIG_BPF_SYSCALL
1728static int bpf_fd_pass(struct file *file, u32 sid);
1729#endif
1730
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731/* Check whether a task can use an open file descriptor to
1732 access an inode in a given way. Check access to the
1733 descriptor itself, and then use dentry_has_perm to
1734 check a particular permission to the file.
1735 Access to the descriptor is implicitly granted if it
1736 has the same SID as the process. If av is zero, then
1737 access to the file is not checked, e.g. for cases
1738 where only the descriptor is affected like seek. */
David Howells88e67f32008-11-14 10:39:21 +11001739static int file_has_perm(const struct cred *cred,
1740 struct file *file,
1741 u32 av)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 struct file_security_struct *fsec = file->f_security;
Al Viro496ad9a2013-01-23 17:07:38 -05001744 struct inode *inode = file_inode(file);
Thomas Liu2bf49692009-07-14 12:14:09 -04001745 struct common_audit_data ad;
David Howells88e67f32008-11-14 10:39:21 +11001746 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 int rc;
1748
Vivek Goyal43af5de2016-09-09 11:37:49 -04001749 ad.type = LSM_AUDIT_DATA_FILE;
1750 ad.u.file = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
David Howells275bb412008-11-14 10:39:19 +11001752 if (sid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001753 rc = avc_has_perm(&selinux_state,
1754 sid, fsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 SECCLASS_FD,
1756 FD__USE,
1757 &ad);
1758 if (rc)
David Howells88e67f32008-11-14 10:39:21 +11001759 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 }
1761
Chenbo Fengf66e4482017-10-18 13:00:26 -07001762#ifdef CONFIG_BPF_SYSCALL
1763 rc = bpf_fd_pass(file, cred_sid(cred));
1764 if (rc)
1765 return rc;
1766#endif
1767
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 /* av is zero if only checking access to the descriptor. */
David Howells88e67f32008-11-14 10:39:21 +11001769 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 if (av)
Linus Torvalds19e49832013-10-04 12:54:11 -07001771 rc = inode_has_perm(cred, inode, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
David Howells88e67f32008-11-14 10:39:21 +11001773out:
1774 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775}
1776
David Howellsc3c188b2015-07-10 17:19:58 -04001777/*
1778 * Determine the label for an inode that might be unioned.
1779 */
Vivek Goyalc957f6d2016-07-13 10:44:51 -04001780static int
1781selinux_determine_inode_label(const struct task_security_struct *tsec,
1782 struct inode *dir,
1783 const struct qstr *name, u16 tclass,
1784 u32 *_new_isid)
David Howellsc3c188b2015-07-10 17:19:58 -04001785{
1786 const struct superblock_security_struct *sbsec = dir->i_sb->s_security;
David Howellsc3c188b2015-07-10 17:19:58 -04001787
1788 if ((sbsec->flags & SE_SBINITIALIZED) &&
1789 (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)) {
1790 *_new_isid = sbsec->mntpoint_sid;
1791 } else if ((sbsec->flags & SBLABEL_MNT) &&
1792 tsec->create_sid) {
1793 *_new_isid = tsec->create_sid;
1794 } else {
Paul Moore20cdef82016-04-04 14:14:42 -04001795 const struct inode_security_struct *dsec = inode_security(dir);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001796 return security_transition_sid(&selinux_state, tsec->sid,
1797 dsec->sid, tclass,
David Howellsc3c188b2015-07-10 17:19:58 -04001798 name, _new_isid);
1799 }
1800
1801 return 0;
1802}
1803
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804/* Check whether a task can create a file. */
1805static int may_create(struct inode *dir,
1806 struct dentry *dentry,
1807 u16 tclass)
1808{
Paul Moore5fb49872010-04-22 14:46:19 -04001809 const struct task_security_struct *tsec = current_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 struct inode_security_struct *dsec;
1811 struct superblock_security_struct *sbsec;
David Howells275bb412008-11-14 10:39:19 +11001812 u32 sid, newsid;
Thomas Liu2bf49692009-07-14 12:14:09 -04001813 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 int rc;
1815
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001816 dsec = inode_security(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 sbsec = dir->i_sb->s_security;
1818
David Howells275bb412008-11-14 10:39:19 +11001819 sid = tsec->sid;
David Howells275bb412008-11-14 10:39:19 +11001820
Eric Paris50c205f2012-04-04 15:01:43 -04001821 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04001822 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001824 rc = avc_has_perm(&selinux_state,
1825 sid, dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 DIR__ADD_NAME | DIR__SEARCH,
1827 &ad);
1828 if (rc)
1829 return rc;
1830
Vivek Goyalc957f6d2016-07-13 10:44:51 -04001831 rc = selinux_determine_inode_label(current_security(), dir,
1832 &dentry->d_name, tclass, &newsid);
David Howellsc3c188b2015-07-10 17:19:58 -04001833 if (rc)
1834 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001836 rc = avc_has_perm(&selinux_state,
1837 sid, newsid, tclass, FILE__CREATE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 if (rc)
1839 return rc;
1840
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001841 return avc_has_perm(&selinux_state,
1842 newsid, sbsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 SECCLASS_FILESYSTEM,
1844 FILESYSTEM__ASSOCIATE, &ad);
1845}
1846
Eric Paris828dfe12008-04-17 13:17:49 -04001847#define MAY_LINK 0
1848#define MAY_UNLINK 1
1849#define MAY_RMDIR 2
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
1851/* Check whether a task can link, unlink, or rmdir a file/directory. */
1852static int may_link(struct inode *dir,
1853 struct dentry *dentry,
1854 int kind)
1855
1856{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 struct inode_security_struct *dsec, *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001858 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11001859 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 u32 av;
1861 int rc;
1862
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001863 dsec = inode_security(dir);
1864 isec = backing_inode_security(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
Eric Paris50c205f2012-04-04 15:01:43 -04001866 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04001867 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
1869 av = DIR__SEARCH;
1870 av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001871 rc = avc_has_perm(&selinux_state,
1872 sid, dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 if (rc)
1874 return rc;
1875
1876 switch (kind) {
1877 case MAY_LINK:
1878 av = FILE__LINK;
1879 break;
1880 case MAY_UNLINK:
1881 av = FILE__UNLINK;
1882 break;
1883 case MAY_RMDIR:
1884 av = DIR__RMDIR;
1885 break;
1886 default:
peter enderborgc103a912018-06-12 10:09:03 +02001887 pr_warn("SELinux: %s: unrecognized kind %d\n",
Eric Paris744ba352008-04-17 11:52:44 -04001888 __func__, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 return 0;
1890 }
1891
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001892 rc = avc_has_perm(&selinux_state,
1893 sid, isec->sid, isec->sclass, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 return rc;
1895}
1896
1897static inline int may_rename(struct inode *old_dir,
1898 struct dentry *old_dentry,
1899 struct inode *new_dir,
1900 struct dentry *new_dentry)
1901{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001903 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11001904 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 u32 av;
1906 int old_is_dir, new_is_dir;
1907 int rc;
1908
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001909 old_dsec = inode_security(old_dir);
1910 old_isec = backing_inode_security(old_dentry);
David Howellse36cb0b2015-01-29 12:02:35 +00001911 old_is_dir = d_is_dir(old_dentry);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001912 new_dsec = inode_security(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
Eric Paris50c205f2012-04-04 15:01:43 -04001914 ad.type = LSM_AUDIT_DATA_DENTRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
Eric Parisa2694342011-04-25 13:10:27 -04001916 ad.u.dentry = old_dentry;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001917 rc = avc_has_perm(&selinux_state,
1918 sid, old_dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 DIR__REMOVE_NAME | DIR__SEARCH, &ad);
1920 if (rc)
1921 return rc;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001922 rc = avc_has_perm(&selinux_state,
1923 sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 old_isec->sclass, FILE__RENAME, &ad);
1925 if (rc)
1926 return rc;
1927 if (old_is_dir && new_dir != old_dir) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001928 rc = avc_has_perm(&selinux_state,
1929 sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 old_isec->sclass, DIR__REPARENT, &ad);
1931 if (rc)
1932 return rc;
1933 }
1934
Eric Parisa2694342011-04-25 13:10:27 -04001935 ad.u.dentry = new_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 av = DIR__ADD_NAME | DIR__SEARCH;
David Howells2c616d42015-01-29 12:02:33 +00001937 if (d_is_positive(new_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 av |= DIR__REMOVE_NAME;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001939 rc = avc_has_perm(&selinux_state,
1940 sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 if (rc)
1942 return rc;
David Howells2c616d42015-01-29 12:02:33 +00001943 if (d_is_positive(new_dentry)) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001944 new_isec = backing_inode_security(new_dentry);
David Howellse36cb0b2015-01-29 12:02:35 +00001945 new_is_dir = d_is_dir(new_dentry);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001946 rc = avc_has_perm(&selinux_state,
1947 sid, new_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 new_isec->sclass,
1949 (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
1950 if (rc)
1951 return rc;
1952 }
1953
1954 return 0;
1955}
1956
1957/* Check whether a task can perform a filesystem operation. */
David Howells88e67f32008-11-14 10:39:21 +11001958static int superblock_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 struct super_block *sb,
1960 u32 perms,
Thomas Liu2bf49692009-07-14 12:14:09 -04001961 struct common_audit_data *ad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 struct superblock_security_struct *sbsec;
David Howells88e67f32008-11-14 10:39:21 +11001964 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 sbsec = sb->s_security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001967 return avc_has_perm(&selinux_state,
1968 sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969}
1970
1971/* Convert a Linux mode and permission mask to an access vector. */
1972static inline u32 file_mask_to_av(int mode, int mask)
1973{
1974 u32 av = 0;
1975
Al Virodba19c62011-07-25 20:49:29 -04001976 if (!S_ISDIR(mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 if (mask & MAY_EXEC)
1978 av |= FILE__EXECUTE;
1979 if (mask & MAY_READ)
1980 av |= FILE__READ;
1981
1982 if (mask & MAY_APPEND)
1983 av |= FILE__APPEND;
1984 else if (mask & MAY_WRITE)
1985 av |= FILE__WRITE;
1986
1987 } else {
1988 if (mask & MAY_EXEC)
1989 av |= DIR__SEARCH;
1990 if (mask & MAY_WRITE)
1991 av |= DIR__WRITE;
1992 if (mask & MAY_READ)
1993 av |= DIR__READ;
1994 }
1995
1996 return av;
1997}
1998
1999/* Convert a Linux file to an access vector. */
2000static inline u32 file_to_av(struct file *file)
2001{
2002 u32 av = 0;
2003
2004 if (file->f_mode & FMODE_READ)
2005 av |= FILE__READ;
2006 if (file->f_mode & FMODE_WRITE) {
2007 if (file->f_flags & O_APPEND)
2008 av |= FILE__APPEND;
2009 else
2010 av |= FILE__WRITE;
2011 }
Stephen Smalley0794c662008-03-17 08:55:18 -04002012 if (!av) {
2013 /*
2014 * Special file opened with flags 3 for ioctl-only use.
2015 */
2016 av = FILE__IOCTL;
2017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
2019 return av;
2020}
2021
Eric Paris8b6a5a32008-10-29 17:06:46 -04002022/*
2023 * Convert a file to an access vector and include the correct open
2024 * open permission.
2025 */
2026static inline u32 open_file_to_av(struct file *file)
2027{
2028 u32 av = file_to_av(file);
Stephen Smalleyccb54472017-05-12 12:41:24 -04002029 struct inode *inode = file_inode(file);
Eric Paris8b6a5a32008-10-29 17:06:46 -04002030
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002031 if (selinux_policycap_openperm() &&
2032 inode->i_sb->s_magic != SOCKFS_MAGIC)
Eric Paris49b7b8d2010-07-23 11:44:09 -04002033 av |= FILE__OPEN;
2034
Eric Paris8b6a5a32008-10-29 17:06:46 -04002035 return av;
2036}
2037
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038/* Hook functions begin here. */
2039
Stephen Smalley79af7302015-01-21 10:54:10 -05002040static int selinux_binder_set_context_mgr(struct task_struct *mgr)
2041{
2042 u32 mysid = current_sid();
2043 u32 mgrsid = task_sid(mgr);
2044
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002045 return avc_has_perm(&selinux_state,
2046 mysid, mgrsid, SECCLASS_BINDER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002047 BINDER__SET_CONTEXT_MGR, NULL);
2048}
2049
2050static int selinux_binder_transaction(struct task_struct *from,
2051 struct task_struct *to)
2052{
2053 u32 mysid = current_sid();
2054 u32 fromsid = task_sid(from);
2055 u32 tosid = task_sid(to);
2056 int rc;
2057
2058 if (mysid != fromsid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002059 rc = avc_has_perm(&selinux_state,
2060 mysid, fromsid, SECCLASS_BINDER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002061 BINDER__IMPERSONATE, NULL);
2062 if (rc)
2063 return rc;
2064 }
2065
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002066 return avc_has_perm(&selinux_state,
2067 fromsid, tosid, SECCLASS_BINDER, BINDER__CALL,
Stephen Smalley79af7302015-01-21 10:54:10 -05002068 NULL);
2069}
2070
2071static int selinux_binder_transfer_binder(struct task_struct *from,
2072 struct task_struct *to)
2073{
2074 u32 fromsid = task_sid(from);
2075 u32 tosid = task_sid(to);
2076
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002077 return avc_has_perm(&selinux_state,
2078 fromsid, tosid, SECCLASS_BINDER, BINDER__TRANSFER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002079 NULL);
2080}
2081
2082static int selinux_binder_transfer_file(struct task_struct *from,
2083 struct task_struct *to,
2084 struct file *file)
2085{
2086 u32 sid = task_sid(to);
2087 struct file_security_struct *fsec = file->f_security;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002088 struct dentry *dentry = file->f_path.dentry;
Paul Moore20cdef82016-04-04 14:14:42 -04002089 struct inode_security_struct *isec;
Stephen Smalley79af7302015-01-21 10:54:10 -05002090 struct common_audit_data ad;
2091 int rc;
2092
2093 ad.type = LSM_AUDIT_DATA_PATH;
2094 ad.u.path = file->f_path;
2095
2096 if (sid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002097 rc = avc_has_perm(&selinux_state,
2098 sid, fsec->sid,
Stephen Smalley79af7302015-01-21 10:54:10 -05002099 SECCLASS_FD,
2100 FD__USE,
2101 &ad);
2102 if (rc)
2103 return rc;
2104 }
2105
Chenbo Fengf66e4482017-10-18 13:00:26 -07002106#ifdef CONFIG_BPF_SYSCALL
2107 rc = bpf_fd_pass(file, sid);
2108 if (rc)
2109 return rc;
2110#endif
2111
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002112 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
Stephen Smalley79af7302015-01-21 10:54:10 -05002113 return 0;
2114
Paul Moore20cdef82016-04-04 14:14:42 -04002115 isec = backing_inode_security(dentry);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002116 return avc_has_perm(&selinux_state,
2117 sid, isec->sid, isec->sclass, file_to_av(file),
Stephen Smalley79af7302015-01-21 10:54:10 -05002118 &ad);
2119}
2120
Ingo Molnar9e488582009-05-07 19:26:19 +10002121static int selinux_ptrace_access_check(struct task_struct *child,
David Howells5cd9c582008-08-14 11:37:28 +01002122 unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002124 u32 sid = current_sid();
2125 u32 csid = task_sid(child);
Stephen Smalley006ebb42008-05-19 08:32:49 -04002126
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002127 if (mode & PTRACE_MODE_READ)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002128 return avc_has_perm(&selinux_state,
2129 sid, csid, SECCLASS_FILE, FILE__READ, NULL);
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002130
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002131 return avc_has_perm(&selinux_state,
2132 sid, csid, SECCLASS_PROCESS, PROCESS__PTRACE, NULL);
David Howells5cd9c582008-08-14 11:37:28 +01002133}
2134
2135static int selinux_ptrace_traceme(struct task_struct *parent)
2136{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002137 return avc_has_perm(&selinux_state,
2138 task_sid(parent), current_sid(), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002139 PROCESS__PTRACE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140}
2141
2142static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
Eric Paris828dfe12008-04-17 13:17:49 -04002143 kernel_cap_t *inheritable, kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002145 return avc_has_perm(&selinux_state,
2146 current_sid(), task_sid(target), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002147 PROCESS__GETCAP, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148}
2149
David Howellsd84f4f92008-11-14 10:39:23 +11002150static int selinux_capset(struct cred *new, const struct cred *old,
2151 const kernel_cap_t *effective,
2152 const kernel_cap_t *inheritable,
2153 const kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002155 return avc_has_perm(&selinux_state,
2156 cred_sid(old), cred_sid(new), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002157 PROCESS__SETCAP, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158}
2159
James Morris5626d3e2009-01-30 10:05:06 +11002160/*
2161 * (This comment used to live with the selinux_task_setuid hook,
2162 * which was removed).
2163 *
2164 * Since setuid only affects the current process, and since the SELinux
2165 * controls are not based on the Linux identity attributes, SELinux does not
2166 * need to control this operation. However, SELinux does control the use of
2167 * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
2168 */
2169
Eric Paris6a9de492012-01-03 12:25:14 -05002170static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
2171 int cap, int audit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172{
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04002173 return cred_has_capability(cred, cap, audit, ns == &init_user_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174}
2175
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
2177{
David Howells88e67f32008-11-14 10:39:21 +11002178 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 int rc = 0;
2180
2181 if (!sb)
2182 return 0;
2183
2184 switch (cmds) {
Eric Paris828dfe12008-04-17 13:17:49 -04002185 case Q_SYNC:
2186 case Q_QUOTAON:
2187 case Q_QUOTAOFF:
2188 case Q_SETINFO:
2189 case Q_SETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11002190 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04002191 break;
2192 case Q_GETFMT:
2193 case Q_GETINFO:
2194 case Q_GETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11002195 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04002196 break;
2197 default:
2198 rc = 0; /* let the kernel handle invalid cmds */
2199 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 }
2201 return rc;
2202}
2203
2204static int selinux_quota_on(struct dentry *dentry)
2205{
David Howells88e67f32008-11-14 10:39:21 +11002206 const struct cred *cred = current_cred();
2207
Eric Paris2875fa02011-04-28 16:04:24 -04002208 return dentry_has_perm(cred, dentry, FILE__QUOTAON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209}
2210
Eric Paris12b30522010-11-15 18:36:29 -05002211static int selinux_syslog(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 switch (type) {
Kees Cookd78ca3c2010-02-03 15:37:13 -08002214 case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */
2215 case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002216 return avc_has_perm(&selinux_state,
2217 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002218 SECCLASS_SYSTEM, SYSTEM__SYSLOG_READ, NULL);
Kees Cookd78ca3c2010-02-03 15:37:13 -08002219 case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */
2220 case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */
2221 /* Set level of messages printed to console */
2222 case SYSLOG_ACTION_CONSOLE_LEVEL:
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_CONSOLE,
2226 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 }
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002228 /* All other syslog types */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002229 return avc_has_perm(&selinux_state,
2230 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002231 SECCLASS_SYSTEM, SYSTEM__SYSLOG_MOD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232}
2233
2234/*
2235 * Check that a process has enough memory to allocate a new virtual
2236 * mapping. 0 means there is enough memory for the allocation to
2237 * succeed and -ENOMEM implies there is not.
2238 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 * Do not audit the selinux permission check, as this is applied to all
2240 * processes that allocate mappings.
2241 */
Alan Cox34b4e4a2007-08-22 14:01:28 -07002242static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243{
2244 int rc, cap_sys_admin = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002246 rc = cred_has_capability(current_cred(), CAP_SYS_ADMIN,
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04002247 SECURITY_CAP_NOAUDIT, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 if (rc == 0)
2249 cap_sys_admin = 1;
2250
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002251 return cap_sys_admin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252}
2253
2254/* binprm security operations */
2255
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002256static u32 ptrace_parent_sid(void)
Paul Moore0c6181c2016-03-30 21:41:21 -04002257{
2258 u32 sid = 0;
2259 struct task_struct *tracer;
2260
2261 rcu_read_lock();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002262 tracer = ptrace_parent(current);
Paul Moore0c6181c2016-03-30 21:41:21 -04002263 if (tracer)
2264 sid = task_sid(tracer);
2265 rcu_read_unlock();
2266
2267 return sid;
2268}
2269
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002270static int check_nnp_nosuid(const struct linux_binprm *bprm,
2271 const struct task_security_struct *old_tsec,
2272 const struct task_security_struct *new_tsec)
2273{
2274 int nnp = (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS);
Andy Lutomirski380cf5b2016-06-23 16:41:05 -05002275 int nosuid = !mnt_may_suid(bprm->file->f_path.mnt);
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002276 int rc;
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002277 u32 av;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002278
2279 if (!nnp && !nosuid)
2280 return 0; /* neither NNP nor nosuid */
2281
2282 if (new_tsec->sid == old_tsec->sid)
2283 return 0; /* No change in credentials */
2284
2285 /*
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002286 * If the policy enables the nnp_nosuid_transition policy capability,
2287 * then we permit transitions under NNP or nosuid if the
2288 * policy allows the corresponding permission between
2289 * the old and new contexts.
2290 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002291 if (selinux_policycap_nnp_nosuid_transition()) {
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002292 av = 0;
2293 if (nnp)
2294 av |= PROCESS2__NNP_TRANSITION;
2295 if (nosuid)
2296 av |= PROCESS2__NOSUID_TRANSITION;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002297 rc = avc_has_perm(&selinux_state,
2298 old_tsec->sid, new_tsec->sid,
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002299 SECCLASS_PROCESS2, av, NULL);
2300 if (!rc)
2301 return 0;
2302 }
2303
2304 /*
2305 * We also permit NNP or nosuid transitions to bounded SIDs,
2306 * i.e. SIDs that are guaranteed to only be allowed a subset
2307 * of the permissions of the current SID.
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002308 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002309 rc = security_bounded_transition(&selinux_state, old_tsec->sid,
2310 new_tsec->sid);
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002311 if (!rc)
2312 return 0;
2313
2314 /*
2315 * On failure, preserve the errno values for NNP vs nosuid.
2316 * NNP: Operation not permitted for caller.
2317 * nosuid: Permission denied to file.
2318 */
2319 if (nnp)
2320 return -EPERM;
2321 return -EACCES;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002322}
2323
David Howellsa6f76f22008-11-14 10:39:24 +11002324static int selinux_bprm_set_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325{
David Howellsa6f76f22008-11-14 10:39:24 +11002326 const struct task_security_struct *old_tsec;
2327 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 struct inode_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04002329 struct common_audit_data ad;
Al Viro496ad9a2013-01-23 17:07:38 -05002330 struct inode *inode = file_inode(bprm->file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 int rc;
2332
David Howellsa6f76f22008-11-14 10:39:24 +11002333 /* SELinux context only depends on initial program or script and not
2334 * the script interpreter */
Kees Cookddb4a142017-07-18 15:25:23 -07002335 if (bprm->called_set_creds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 return 0;
2337
David Howellsa6f76f22008-11-14 10:39:24 +11002338 old_tsec = current_security();
2339 new_tsec = bprm->cred->security;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002340 isec = inode_security(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
2342 /* Default to the current task SID. */
David Howellsa6f76f22008-11-14 10:39:24 +11002343 new_tsec->sid = old_tsec->sid;
2344 new_tsec->osid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345
Michael LeMay28eba5b2006-06-27 02:53:42 -07002346 /* Reset fs, key, and sock SIDs on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002347 new_tsec->create_sid = 0;
2348 new_tsec->keycreate_sid = 0;
2349 new_tsec->sockcreate_sid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
David Howellsa6f76f22008-11-14 10:39:24 +11002351 if (old_tsec->exec_sid) {
2352 new_tsec->sid = old_tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 /* Reset exec SID on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002354 new_tsec->exec_sid = 0;
Andy Lutomirski259e5e62012-04-12 16:47:50 -05002355
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002356 /* Fail on NNP or nosuid if not an allowed transition. */
2357 rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
2358 if (rc)
2359 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 } else {
2361 /* Check for a default transition on this program. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002362 rc = security_transition_sid(&selinux_state, old_tsec->sid,
2363 isec->sid, SECCLASS_PROCESS, NULL,
Eric Paris652bb9b2011-02-01 11:05:40 -05002364 &new_tsec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 if (rc)
2366 return rc;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002367
2368 /*
2369 * Fallback to old SID on NNP or nosuid if not an allowed
2370 * transition.
2371 */
2372 rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
2373 if (rc)
2374 new_tsec->sid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 }
2376
Vivek Goyal43af5de2016-09-09 11:37:49 -04002377 ad.type = LSM_AUDIT_DATA_FILE;
2378 ad.u.file = bprm->file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379
David Howellsa6f76f22008-11-14 10:39:24 +11002380 if (new_tsec->sid == old_tsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002381 rc = avc_has_perm(&selinux_state,
2382 old_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
2384 if (rc)
2385 return rc;
2386 } else {
2387 /* Check permissions for the transition. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002388 rc = avc_has_perm(&selinux_state,
2389 old_tsec->sid, new_tsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
2391 if (rc)
2392 return rc;
2393
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002394 rc = avc_has_perm(&selinux_state,
2395 new_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
2397 if (rc)
2398 return rc;
2399
David Howellsa6f76f22008-11-14 10:39:24 +11002400 /* Check for shared state */
2401 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002402 rc = avc_has_perm(&selinux_state,
2403 old_tsec->sid, new_tsec->sid,
David Howellsa6f76f22008-11-14 10:39:24 +11002404 SECCLASS_PROCESS, PROCESS__SHARE,
2405 NULL);
2406 if (rc)
2407 return -EPERM;
2408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409
David Howellsa6f76f22008-11-14 10:39:24 +11002410 /* Make sure that anyone attempting to ptrace over a task that
2411 * changes its SID has the appropriate permit */
Eric W. Biederman9227dd22017-01-23 17:26:31 +13002412 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002413 u32 ptsid = ptrace_parent_sid();
David Howellsa6f76f22008-11-14 10:39:24 +11002414 if (ptsid != 0) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002415 rc = avc_has_perm(&selinux_state,
2416 ptsid, new_tsec->sid,
David Howellsa6f76f22008-11-14 10:39:24 +11002417 SECCLASS_PROCESS,
2418 PROCESS__PTRACE, NULL);
2419 if (rc)
2420 return -EPERM;
2421 }
2422 }
2423
2424 /* Clear any possibly unsafe personality bits on exec: */
2425 bprm->per_clear |= PER_CLEAR_ON_SETID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 /* Enable secure mode for SIDs transitions unless
2428 the noatsecure permission is granted between
2429 the two SIDs, i.e. ahp returns 0. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002430 rc = avc_has_perm(&selinux_state,
2431 old_tsec->sid, new_tsec->sid,
Kees Cook62874c32017-07-18 15:25:25 -07002432 SECCLASS_PROCESS, PROCESS__NOATSECURE,
2433 NULL);
2434 bprm->secureexec |= !!rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 }
2436
Kees Cook62874c32017-07-18 15:25:25 -07002437 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438}
2439
Al Viroc3c073f2012-08-21 22:32:06 -04002440static int match_file(const void *p, struct file *file, unsigned fd)
2441{
2442 return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0;
2443}
2444
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445/* Derived from fs/exec.c:flush_old_files. */
David Howells745ca242008-11-14 10:39:22 +11002446static inline void flush_unauthorized_files(const struct cred *cred,
2447 struct files_struct *files)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 struct file *file, *devnull = NULL;
Stephen Smalleyb20c8122006-09-25 23:32:03 -07002450 struct tty_struct *tty;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002451 int drop_tty = 0;
Al Viroc3c073f2012-08-21 22:32:06 -04002452 unsigned n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002454 tty = get_current_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 if (tty) {
Peter Hurley4a510962016-01-09 21:35:23 -08002456 spin_lock(&tty->files_lock);
Eric Paris37dd0bd2008-10-31 17:40:00 -04002457 if (!list_empty(&tty->tty_files)) {
Nick Piggind996b622010-08-18 04:37:36 +10002458 struct tty_file_private *file_priv;
Eric Paris37dd0bd2008-10-31 17:40:00 -04002459
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 /* Revalidate access to controlling tty.
David Howells13f8e982013-06-13 23:37:55 +01002461 Use file_path_has_perm on the tty path directly
2462 rather than using file_has_perm, as this particular
2463 open file may belong to another process and we are
2464 only interested in the inode-based check here. */
Nick Piggind996b622010-08-18 04:37:36 +10002465 file_priv = list_first_entry(&tty->tty_files,
2466 struct tty_file_private, list);
2467 file = file_priv->file;
David Howells13f8e982013-06-13 23:37:55 +01002468 if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE))
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002469 drop_tty = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 }
Peter Hurley4a510962016-01-09 21:35:23 -08002471 spin_unlock(&tty->files_lock);
Alan Cox452a00d2008-10-13 10:39:13 +01002472 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 }
Eric W. Biederman98a27ba2007-05-08 00:26:56 -07002474 /* Reset controlling tty. */
2475 if (drop_tty)
2476 no_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477
2478 /* Revalidate access to inherited open files. */
Al Viroc3c073f2012-08-21 22:32:06 -04002479 n = iterate_fd(files, 0, match_file, cred);
2480 if (!n) /* none found? */
2481 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
Al Viroc3c073f2012-08-21 22:32:06 -04002483 devnull = dentry_open(&selinux_null, O_RDWR, cred);
Al Viro45525b22012-10-16 13:30:07 -04002484 if (IS_ERR(devnull))
2485 devnull = NULL;
2486 /* replace all the matching ones with this */
2487 do {
2488 replace_fd(n - 1, devnull, 0);
2489 } while ((n = iterate_fd(files, n, match_file, cred)) != 0);
2490 if (devnull)
Al Viroc3c073f2012-08-21 22:32:06 -04002491 fput(devnull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492}
2493
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494/*
David Howellsa6f76f22008-11-14 10:39:24 +11002495 * Prepare a process for imminent new credential changes due to exec
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 */
David Howellsa6f76f22008-11-14 10:39:24 +11002497static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498{
David Howellsa6f76f22008-11-14 10:39:24 +11002499 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 struct rlimit *rlim, *initrlim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 int rc, i;
2502
David Howellsa6f76f22008-11-14 10:39:24 +11002503 new_tsec = bprm->cred->security;
2504 if (new_tsec->sid == new_tsec->osid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 return;
2506
2507 /* Close files for which the new task SID is not authorized. */
David Howellsa6f76f22008-11-14 10:39:24 +11002508 flush_unauthorized_files(bprm->cred, current->files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509
David Howellsa6f76f22008-11-14 10:39:24 +11002510 /* Always clear parent death signal on SID transitions. */
2511 current->pdeath_signal = 0;
2512
2513 /* Check whether the new SID can inherit resource limits from the old
2514 * SID. If not, reset all soft limits to the lower of the current
2515 * task's hard limit and the init task's soft limit.
2516 *
2517 * Note that the setting of hard limits (even to lower them) can be
2518 * controlled by the setrlimit check. The inclusion of the init task's
2519 * soft limit into the computation is to avoid resetting soft limits
2520 * higher than the default soft limit for cases where the default is
2521 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2522 */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002523 rc = avc_has_perm(&selinux_state,
2524 new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
David Howellsa6f76f22008-11-14 10:39:24 +11002525 PROCESS__RLIMITINH, NULL);
2526 if (rc) {
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002527 /* protect against do_prlimit() */
2528 task_lock(current);
David Howellsa6f76f22008-11-14 10:39:24 +11002529 for (i = 0; i < RLIM_NLIMITS; i++) {
2530 rlim = current->signal->rlim + i;
2531 initrlim = init_task.signal->rlim + i;
2532 rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
2533 }
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002534 task_unlock(current);
Nicolas Pitrebaa73d92016-11-11 00:10:10 -05002535 if (IS_ENABLED(CONFIG_POSIX_TIMERS))
2536 update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
David Howellsa6f76f22008-11-14 10:39:24 +11002537 }
2538}
2539
2540/*
2541 * Clean up the process immediately after the installation of new credentials
2542 * due to exec
2543 */
2544static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
2545{
2546 const struct task_security_struct *tsec = current_security();
2547 struct itimerval itimer;
David Howellsa6f76f22008-11-14 10:39:24 +11002548 u32 osid, sid;
2549 int rc, i;
David Howellsa6f76f22008-11-14 10:39:24 +11002550
David Howellsa6f76f22008-11-14 10:39:24 +11002551 osid = tsec->osid;
2552 sid = tsec->sid;
2553
2554 if (sid == osid)
2555 return;
2556
2557 /* Check whether the new SID can inherit signal state from the old SID.
2558 * If not, clear itimers to avoid subsequent signal generation and
2559 * flush and unblock signals.
2560 *
2561 * This must occur _after_ the task SID has been updated so that any
2562 * kill done after the flush will be checked against the new SID.
2563 */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002564 rc = avc_has_perm(&selinux_state,
2565 osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 if (rc) {
Nicolas Pitrebaa73d92016-11-11 00:10:10 -05002567 if (IS_ENABLED(CONFIG_POSIX_TIMERS)) {
2568 memset(&itimer, 0, sizeof itimer);
2569 for (i = 0; i < 3; i++)
2570 do_setitimer(i, &itimer, NULL);
2571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterov9e7c8f82015-06-04 16:22:16 -04002573 if (!fatal_signal_pending(current)) {
2574 flush_sigqueue(&current->pending);
2575 flush_sigqueue(&current->signal->shared_pending);
David Howells3bcac022009-04-29 13:45:05 +01002576 flush_signal_handlers(current, 1);
2577 sigemptyset(&current->blocked);
Oleg Nesterov9e7c8f82015-06-04 16:22:16 -04002578 recalc_sigpending();
David Howells3bcac022009-04-29 13:45:05 +01002579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 spin_unlock_irq(&current->sighand->siglock);
2581 }
2582
David Howellsa6f76f22008-11-14 10:39:24 +11002583 /* Wake up the parent if it is waiting so that it can recheck
2584 * wait permission to the new task SID. */
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002585 read_lock(&tasklist_lock);
Oleg Nesterov0b7570e2009-09-23 15:56:46 -07002586 __wake_up_parent(current, current->real_parent);
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002587 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588}
2589
2590/* superblock security operations */
2591
2592static int selinux_sb_alloc_security(struct super_block *sb)
2593{
2594 return superblock_alloc_security(sb);
2595}
2596
2597static void selinux_sb_free_security(struct super_block *sb)
2598{
2599 superblock_free_security(sb);
2600}
2601
Al Viro99dbbb52018-12-14 21:56:23 -05002602static inline int opt_len(const char *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603{
Al Viro99dbbb52018-12-14 21:56:23 -05002604 bool open_quote = false;
2605 int len;
2606 char c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607
Al Viro99dbbb52018-12-14 21:56:23 -05002608 for (len = 0; (c = s[len]) != '\0'; len++) {
2609 if (c == '"')
Cory Olmo3528a952006-09-29 01:58:44 -07002610 open_quote = !open_quote;
Al Viro99dbbb52018-12-14 21:56:23 -05002611 if (c == ',' && !open_quote)
2612 break;
2613 }
2614 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615}
2616
Al Viro204cc0c2018-12-13 13:41:47 -05002617static int selinux_sb_eat_lsm_opts(char *options, void **mnt_opts)
Eric Paris026eb162011-03-03 16:09:14 -05002618{
Al Viro99dbbb52018-12-14 21:56:23 -05002619 char *from = options;
2620 char *to = options;
2621 bool first = true;
Al Viro5b400232018-12-12 20:13:29 -05002622
Al Viro99dbbb52018-12-14 21:56:23 -05002623 while (1) {
2624 int len = opt_len(from);
2625 int token, rc;
2626 char *arg = NULL;
2627
2628 token = match_opt_prefix(from, len, &arg);
2629
2630 if (token != Opt_error) {
2631 char *p, *q;
2632
2633 /* strip quotes */
2634 if (arg) {
2635 for (p = q = arg; p < from + len; p++) {
2636 char c = *p;
2637 if (c != '"')
2638 *q++ = c;
2639 }
2640 arg = kmemdup_nul(arg, q - arg, GFP_KERNEL);
2641 }
2642 rc = selinux_add_opt(token, arg, mnt_opts);
2643 if (unlikely(rc)) {
2644 kfree(arg);
2645 if (*mnt_opts) {
2646 selinux_free_mnt_opts(*mnt_opts);
2647 *mnt_opts = NULL;
2648 }
2649 return rc;
2650 }
2651 } else {
2652 if (!first) { // copy with preceding comma
2653 from--;
2654 len++;
2655 }
2656 if (to != from)
2657 memmove(to, from, len);
2658 to += len;
2659 first = false;
2660 }
2661 if (!from[len])
2662 break;
2663 from += len + 1;
2664 }
2665 *to = '\0';
2666 return 0;
Al Viro5b400232018-12-12 20:13:29 -05002667}
2668
Al Viro204cc0c2018-12-13 13:41:47 -05002669static int selinux_sb_remount(struct super_block *sb, void *mnt_opts)
Eric Paris026eb162011-03-03 16:09:14 -05002670{
Al Virobd323652018-12-13 15:04:59 -05002671 struct selinux_mnt_opts *opts = mnt_opts;
Eric Paris026eb162011-03-03 16:09:14 -05002672 struct superblock_security_struct *sbsec = sb->s_security;
Al Virobd323652018-12-13 15:04:59 -05002673 u32 sid;
2674 int rc;
Eric Paris026eb162011-03-03 16:09:14 -05002675
2676 if (!(sbsec->flags & SE_SBINITIALIZED))
2677 return 0;
2678
Al Viro204cc0c2018-12-13 13:41:47 -05002679 if (!opts)
Eric Paris026eb162011-03-03 16:09:14 -05002680 return 0;
2681
Al Virobd323652018-12-13 15:04:59 -05002682 if (opts->fscontext) {
2683 rc = parse_sid(sb, opts->fscontext, &sid);
2684 if (rc)
Al Viroc039bc32018-12-01 23:06:57 -05002685 return rc;
Al Virobd323652018-12-13 15:04:59 -05002686 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
2687 goto out_bad_option;
Eric Paris026eb162011-03-03 16:09:14 -05002688 }
Al Virobd323652018-12-13 15:04:59 -05002689 if (opts->context) {
2690 rc = parse_sid(sb, opts->context, &sid);
2691 if (rc)
2692 return rc;
2693 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
2694 goto out_bad_option;
2695 }
2696 if (opts->rootcontext) {
2697 struct inode_security_struct *root_isec;
2698 root_isec = backing_inode_security(sb->s_root);
2699 rc = parse_sid(sb, opts->rootcontext, &sid);
2700 if (rc)
2701 return rc;
2702 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
2703 goto out_bad_option;
2704 }
2705 if (opts->defcontext) {
2706 rc = parse_sid(sb, opts->defcontext, &sid);
2707 if (rc)
2708 return rc;
2709 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
2710 goto out_bad_option;
Eric Paris026eb162011-03-03 16:09:14 -05002711 }
Al Viroc039bc32018-12-01 23:06:57 -05002712 return 0;
Eric Paris026eb162011-03-03 16:09:14 -05002713
Eric Paris026eb162011-03-03 16:09:14 -05002714out_bad_option:
peter enderborgc103a912018-06-12 10:09:03 +02002715 pr_warn("SELinux: unable to change security options "
Linus Torvalds29b1deb2013-12-15 11:17:45 -08002716 "during remount (dev %s, type=%s)\n", sb->s_id,
2717 sb->s_type->name);
Al Viroc039bc32018-12-01 23:06:57 -05002718 return -EINVAL;
Eric Paris026eb162011-03-03 16:09:14 -05002719}
2720
Al Viroa10d7c22018-12-05 11:58:35 -05002721static int selinux_sb_kern_mount(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722{
David Howells88e67f32008-11-14 10:39:21 +11002723 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002724 struct common_audit_data ad;
James Morris74192242008-12-19 11:41:10 +11002725
Eric Paris50c205f2012-04-04 15:01:43 -04002726 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002727 ad.u.dentry = sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002728 return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729}
2730
David Howells726c3342006-06-23 02:02:58 -07002731static int selinux_sb_statfs(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732{
David Howells88e67f32008-11-14 10:39:21 +11002733 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002734 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735
Eric Paris50c205f2012-04-04 15:01:43 -04002736 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002737 ad.u.dentry = dentry->d_sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002738 return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739}
2740
Al Viro808d4e32012-10-11 11:42:01 -04002741static int selinux_mount(const char *dev_name,
Al Viro8a04c432016-03-25 14:52:53 -04002742 const struct path *path,
Al Viro808d4e32012-10-11 11:42:01 -04002743 const char *type,
Eric Paris828dfe12008-04-17 13:17:49 -04002744 unsigned long flags,
2745 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746{
David Howells88e67f32008-11-14 10:39:21 +11002747 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748
2749 if (flags & MS_REMOUNT)
Al Virod8c95842011-12-07 18:16:57 -05002750 return superblock_has_perm(cred, path->dentry->d_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002751 FILESYSTEM__REMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 else
Eric Paris2875fa02011-04-28 16:04:24 -04002753 return path_has_perm(cred, path, FILE__MOUNTON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754}
2755
2756static int selinux_umount(struct vfsmount *mnt, int flags)
2757{
David Howells88e67f32008-11-14 10:39:21 +11002758 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759
David Howells88e67f32008-11-14 10:39:21 +11002760 return superblock_has_perm(cred, mnt->mnt_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002761 FILESYSTEM__UNMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762}
2763
2764/* inode security operations */
2765
2766static int selinux_inode_alloc_security(struct inode *inode)
2767{
2768 return inode_alloc_security(inode);
2769}
2770
2771static void selinux_inode_free_security(struct inode *inode)
2772{
2773 inode_free_security(inode);
2774}
2775
David Quigleyd47be3d2013-05-22 12:50:34 -04002776static int selinux_dentry_init_security(struct dentry *dentry, int mode,
Al Viro4f3ccd72016-07-20 16:06:15 -04002777 const struct qstr *name, void **ctx,
David Quigleyd47be3d2013-05-22 12:50:34 -04002778 u32 *ctxlen)
2779{
David Quigleyd47be3d2013-05-22 12:50:34 -04002780 u32 newsid;
2781 int rc;
2782
Vivek Goyalc957f6d2016-07-13 10:44:51 -04002783 rc = selinux_determine_inode_label(current_security(),
2784 d_inode(dentry->d_parent), name,
David Howellsc3c188b2015-07-10 17:19:58 -04002785 inode_mode_to_security_class(mode),
2786 &newsid);
2787 if (rc)
2788 return rc;
David Quigleyd47be3d2013-05-22 12:50:34 -04002789
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002790 return security_sid_to_context(&selinux_state, newsid, (char **)ctx,
2791 ctxlen);
David Quigleyd47be3d2013-05-22 12:50:34 -04002792}
2793
Vivek Goyala518b0a2016-07-13 10:44:53 -04002794static int selinux_dentry_create_files_as(struct dentry *dentry, int mode,
2795 struct qstr *name,
2796 const struct cred *old,
2797 struct cred *new)
2798{
2799 u32 newsid;
2800 int rc;
2801 struct task_security_struct *tsec;
2802
2803 rc = selinux_determine_inode_label(old->security,
2804 d_inode(dentry->d_parent), name,
2805 inode_mode_to_security_class(mode),
2806 &newsid);
2807 if (rc)
2808 return rc;
2809
2810 tsec = new->security;
2811 tsec->create_sid = newsid;
2812 return 0;
2813}
2814
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002815static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +09002816 const struct qstr *qstr,
2817 const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -05002818 void **value, size_t *len)
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002819{
Paul Moore5fb49872010-04-22 14:46:19 -04002820 const struct task_security_struct *tsec = current_security();
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002821 struct superblock_security_struct *sbsec;
Corentin LABBEc0d4f462017-10-04 20:32:17 +02002822 u32 newsid, clen;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002823 int rc;
Tetsuo Handa95489062013-07-25 05:44:02 +09002824 char *context;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002825
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002826 sbsec = dir->i_sb->s_security;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002827
David Howells275bb412008-11-14 10:39:19 +11002828 newsid = tsec->create_sid;
2829
Vivek Goyalc957f6d2016-07-13 10:44:51 -04002830 rc = selinux_determine_inode_label(current_security(),
David Howellsc3c188b2015-07-10 17:19:58 -04002831 dir, qstr,
2832 inode_mode_to_security_class(inode->i_mode),
2833 &newsid);
2834 if (rc)
2835 return rc;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002836
Eric Paris296fddf2006-09-25 23:32:00 -07002837 /* Possibly defer initialization to selinux_complete_init. */
David P. Quigley0d90a7e2009-01-16 09:22:02 -05002838 if (sbsec->flags & SE_SBINITIALIZED) {
Eric Paris296fddf2006-09-25 23:32:00 -07002839 struct inode_security_struct *isec = inode->i_security;
2840 isec->sclass = inode_mode_to_security_class(inode->i_mode);
2841 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05002842 isec->initialized = LABEL_INITIALIZED;
Eric Paris296fddf2006-09-25 23:32:00 -07002843 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002844
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002845 if (!selinux_state.initialized || !(sbsec->flags & SBLABEL_MNT))
Stephen Smalley25a74f32005-11-08 21:34:33 -08002846 return -EOPNOTSUPP;
2847
Tetsuo Handa95489062013-07-25 05:44:02 +09002848 if (name)
2849 *name = XATTR_SELINUX_SUFFIX;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002850
2851 if (value && len) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002852 rc = security_sid_to_context_force(&selinux_state, newsid,
2853 &context, &clen);
Tetsuo Handa95489062013-07-25 05:44:02 +09002854 if (rc)
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002855 return rc;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002856 *value = context;
2857 *len = clen;
2858 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002859
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002860 return 0;
2861}
2862
Al Viro4acdaf22011-07-26 01:42:34 -04002863static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864{
2865 return may_create(dir, dentry, SECCLASS_FILE);
2866}
2867
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2869{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 return may_link(dir, old_dentry, MAY_LINK);
2871}
2872
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
2874{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 return may_link(dir, dentry, MAY_UNLINK);
2876}
2877
2878static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
2879{
2880 return may_create(dir, dentry, SECCLASS_LNK_FILE);
2881}
2882
Al Viro18bb1db2011-07-26 01:41:39 -04002883static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884{
2885 return may_create(dir, dentry, SECCLASS_DIR);
2886}
2887
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
2889{
2890 return may_link(dir, dentry, MAY_RMDIR);
2891}
2892
Al Viro1a67aaf2011-07-26 01:52:52 -04002893static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 return may_create(dir, dentry, inode_mode_to_security_class(mode));
2896}
2897
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
Eric Paris828dfe12008-04-17 13:17:49 -04002899 struct inode *new_inode, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900{
2901 return may_rename(old_inode, old_dentry, new_inode, new_dentry);
2902}
2903
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904static int selinux_inode_readlink(struct dentry *dentry)
2905{
David Howells88e67f32008-11-14 10:39:21 +11002906 const struct cred *cred = current_cred();
2907
Eric Paris2875fa02011-04-28 16:04:24 -04002908 return dentry_has_perm(cred, dentry, FILE__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909}
2910
NeilBrownbda0be72015-03-23 13:37:39 +11002911static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode,
2912 bool rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913{
David Howells88e67f32008-11-14 10:39:21 +11002914 const struct cred *cred = current_cred();
NeilBrownbda0be72015-03-23 13:37:39 +11002915 struct common_audit_data ad;
2916 struct inode_security_struct *isec;
2917 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918
NeilBrownbda0be72015-03-23 13:37:39 +11002919 validate_creds(cred);
2920
2921 ad.type = LSM_AUDIT_DATA_DENTRY;
2922 ad.u.dentry = dentry;
2923 sid = cred_sid(cred);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05002924 isec = inode_security_rcu(inode, rcu);
2925 if (IS_ERR(isec))
2926 return PTR_ERR(isec);
NeilBrownbda0be72015-03-23 13:37:39 +11002927
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002928 return avc_has_perm_flags(&selinux_state,
2929 sid, isec->sid, isec->sclass, FILE__READ, &ad,
NeilBrownbda0be72015-03-23 13:37:39 +11002930 rcu ? MAY_NOT_BLOCK : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931}
2932
Eric Parisd4cf970d2012-04-04 15:01:42 -04002933static noinline int audit_inode_permission(struct inode *inode,
2934 u32 perms, u32 audited, u32 denied,
Stephen Smalley626b9742014-04-29 11:29:04 -07002935 int result,
Eric Parisd4cf970d2012-04-04 15:01:42 -04002936 unsigned flags)
2937{
2938 struct common_audit_data ad;
Eric Parisd4cf970d2012-04-04 15:01:42 -04002939 struct inode_security_struct *isec = inode->i_security;
2940 int rc;
2941
Eric Paris50c205f2012-04-04 15:01:43 -04002942 ad.type = LSM_AUDIT_DATA_INODE;
Eric Parisd4cf970d2012-04-04 15:01:42 -04002943 ad.u.inode = inode;
2944
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002945 rc = slow_avc_audit(&selinux_state,
2946 current_sid(), isec->sid, isec->sclass, perms,
Stephen Smalley626b9742014-04-29 11:29:04 -07002947 audited, denied, result, &ad, flags);
Eric Parisd4cf970d2012-04-04 15:01:42 -04002948 if (rc)
2949 return rc;
2950 return 0;
2951}
2952
Al Viroe74f71e2011-06-20 19:38:15 -04002953static int selinux_inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954{
David Howells88e67f32008-11-14 10:39:21 +11002955 const struct cred *cred = current_cred();
Eric Parisb782e0a2010-07-23 11:44:03 -04002956 u32 perms;
2957 bool from_access;
Al Virocf1dd1d2011-06-20 19:44:08 -04002958 unsigned flags = mask & MAY_NOT_BLOCK;
Eric Paris2e334052012-04-04 15:01:42 -04002959 struct inode_security_struct *isec;
2960 u32 sid;
2961 struct av_decision avd;
2962 int rc, rc2;
2963 u32 audited, denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964
Eric Parisb782e0a2010-07-23 11:44:03 -04002965 from_access = mask & MAY_ACCESS;
Eric Parisd09ca732010-07-23 11:43:57 -04002966 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
2967
Eric Parisb782e0a2010-07-23 11:44:03 -04002968 /* No permission to check. Existence test. */
2969 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971
Eric Paris2e334052012-04-04 15:01:42 -04002972 validate_creds(cred);
Eric Parisb782e0a2010-07-23 11:44:03 -04002973
Eric Paris2e334052012-04-04 15:01:42 -04002974 if (unlikely(IS_PRIVATE(inode)))
2975 return 0;
Eric Parisb782e0a2010-07-23 11:44:03 -04002976
2977 perms = file_mask_to_av(inode->i_mode, mask);
2978
Eric Paris2e334052012-04-04 15:01:42 -04002979 sid = cred_sid(cred);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05002980 isec = inode_security_rcu(inode, flags & MAY_NOT_BLOCK);
2981 if (IS_ERR(isec))
2982 return PTR_ERR(isec);
Eric Paris2e334052012-04-04 15:01:42 -04002983
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002984 rc = avc_has_perm_noaudit(&selinux_state,
Stephen Smalley3a28cff2018-12-12 10:10:55 -05002985 sid, isec->sid, isec->sclass, perms,
2986 (flags & MAY_NOT_BLOCK) ? AVC_NONBLOCKING : 0,
2987 &avd);
Eric Paris2e334052012-04-04 15:01:42 -04002988 audited = avc_audit_required(perms, &avd, rc,
2989 from_access ? FILE__AUDIT_ACCESS : 0,
2990 &denied);
2991 if (likely(!audited))
2992 return rc;
2993
Stephen Smalley626b9742014-04-29 11:29:04 -07002994 rc2 = audit_inode_permission(inode, perms, audited, denied, rc, flags);
Eric Paris2e334052012-04-04 15:01:42 -04002995 if (rc2)
2996 return rc2;
2997 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998}
2999
3000static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
3001{
David Howells88e67f32008-11-14 10:39:21 +11003002 const struct cred *cred = current_cred();
Stephen Smalleyccb54472017-05-12 12:41:24 -04003003 struct inode *inode = d_backing_inode(dentry);
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003004 unsigned int ia_valid = iattr->ia_valid;
Eric Paris95dbf732012-04-04 13:45:34 -04003005 __u32 av = FILE__WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003007 /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
3008 if (ia_valid & ATTR_FORCE) {
3009 ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
3010 ATTR_FORCE);
3011 if (!ia_valid)
3012 return 0;
3013 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003015 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
3016 ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
Eric Paris2875fa02011-04-28 16:04:24 -04003017 return dentry_has_perm(cred, dentry, FILE__SETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003019 if (selinux_policycap_openperm() &&
Stephen Smalleyccb54472017-05-12 12:41:24 -04003020 inode->i_sb->s_magic != SOCKFS_MAGIC &&
3021 (ia_valid & ATTR_SIZE) &&
3022 !(ia_valid & ATTR_FILE))
Eric Paris95dbf732012-04-04 13:45:34 -04003023 av |= FILE__OPEN;
3024
3025 return dentry_has_perm(cred, dentry, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026}
3027
Al Viro3f7036a2015-03-08 19:28:30 -04003028static int selinux_inode_getattr(const struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029{
Al Viro3f7036a2015-03-08 19:28:30 -04003030 return path_has_perm(current_cred(), path, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031}
3032
Stephen Smalleydb590002017-04-20 11:31:30 -04003033static bool has_cap_mac_admin(bool audit)
3034{
3035 const struct cred *cred = current_cred();
3036 int cap_audit = audit ? SECURITY_CAP_AUDIT : SECURITY_CAP_NOAUDIT;
3037
3038 if (cap_capable(cred, &init_user_ns, CAP_MAC_ADMIN, cap_audit))
3039 return false;
3040 if (cred_has_capability(cred, CAP_MAC_ADMIN, cap_audit, true))
3041 return false;
3042 return true;
3043}
3044
David Howells8f0cfa52008-04-29 00:59:41 -07003045static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
3046 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047{
David Howellsc6f493d2015-03-17 22:26:22 +00003048 struct inode *inode = d_backing_inode(dentry);
Paul Moore20cdef82016-04-04 14:14:42 -04003049 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 struct superblock_security_struct *sbsec;
Thomas Liu2bf49692009-07-14 12:14:09 -04003051 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11003052 u32 newsid, sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 int rc = 0;
3054
Eric W. Biederman6b240302017-10-02 09:38:20 -05003055 if (strcmp(name, XATTR_NAME_SELINUX)) {
3056 rc = cap_inode_setxattr(dentry, name, value, size, flags);
3057 if (rc)
3058 return rc;
3059
3060 /* Not an attribute we recognize, so just check the
3061 ordinary setattr permission. */
3062 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064
3065 sbsec = inode->i_sb->s_security;
Eric Paris12f348b2012-10-09 10:56:25 -04003066 if (!(sbsec->flags & SBLABEL_MNT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 return -EOPNOTSUPP;
3068
Serge E. Hallyn2e149672011-03-23 16:43:26 -07003069 if (!inode_owner_or_capable(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 return -EPERM;
3071
Eric Paris50c205f2012-04-04 15:01:43 -04003072 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04003073 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074
Paul Moore20cdef82016-04-04 14:14:42 -04003075 isec = backing_inode_security(dentry);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003076 rc = avc_has_perm(&selinux_state,
3077 sid, isec->sid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 FILE__RELABELFROM, &ad);
3079 if (rc)
3080 return rc;
3081
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003082 rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3083 GFP_KERNEL);
Stephen Smalley12b29f32008-05-07 13:03:20 -04003084 if (rc == -EINVAL) {
Stephen Smalleydb590002017-04-20 11:31:30 -04003085 if (!has_cap_mac_admin(true)) {
Eric Parisd6ea83e2012-04-04 13:45:49 -04003086 struct audit_buffer *ab;
3087 size_t audit_size;
Eric Parisd6ea83e2012-04-04 13:45:49 -04003088
3089 /* We strip a nul only if it is at the end, otherwise the
3090 * context contains a nul and we should audit that */
Al Viroe3fea3f2012-06-09 08:15:16 +01003091 if (value) {
Colin Ian Kingadd24372017-10-14 13:46:55 +01003092 const char *str = value;
3093
Al Viroe3fea3f2012-06-09 08:15:16 +01003094 if (str[size - 1] == '\0')
3095 audit_size = size - 1;
3096 else
3097 audit_size = size;
3098 } else {
Al Viroe3fea3f2012-06-09 08:15:16 +01003099 audit_size = 0;
3100 }
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04003101 ab = audit_log_start(audit_context(),
3102 GFP_ATOMIC, AUDIT_SELINUX_ERR);
Eric Parisd6ea83e2012-04-04 13:45:49 -04003103 audit_log_format(ab, "op=setxattr invalid_context=");
3104 audit_log_n_untrustedstring(ab, value, audit_size);
3105 audit_log_end(ab);
3106
Stephen Smalley12b29f32008-05-07 13:03:20 -04003107 return rc;
Eric Parisd6ea83e2012-04-04 13:45:49 -04003108 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003109 rc = security_context_to_sid_force(&selinux_state, value,
3110 size, &newsid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04003111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 if (rc)
3113 return rc;
3114
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003115 rc = avc_has_perm(&selinux_state,
3116 sid, newsid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 FILE__RELABELTO, &ad);
3118 if (rc)
3119 return rc;
3120
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003121 rc = security_validate_transition(&selinux_state, isec->sid, newsid,
3122 sid, isec->sclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 if (rc)
3124 return rc;
3125
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003126 return avc_has_perm(&selinux_state,
3127 newsid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 sbsec->sid,
3129 SECCLASS_FILESYSTEM,
3130 FILESYSTEM__ASSOCIATE,
3131 &ad);
3132}
3133
David Howells8f0cfa52008-04-29 00:59:41 -07003134static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
Eric Parisf5269712008-05-14 11:27:45 -04003135 const void *value, size_t size,
David Howells8f0cfa52008-04-29 00:59:41 -07003136 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137{
David Howellsc6f493d2015-03-17 22:26:22 +00003138 struct inode *inode = d_backing_inode(dentry);
Paul Moore20cdef82016-04-04 14:14:42 -04003139 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 u32 newsid;
3141 int rc;
3142
3143 if (strcmp(name, XATTR_NAME_SELINUX)) {
3144 /* Not an attribute we recognize, so nothing to do. */
3145 return;
3146 }
3147
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003148 rc = security_context_to_sid_force(&selinux_state, value, size,
3149 &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 if (rc) {
peter enderborgc103a912018-06-12 10:09:03 +02003151 pr_err("SELinux: unable to map context to SID"
Stephen Smalley12b29f32008-05-07 13:03:20 -04003152 "for (%s, %lu), rc=%d\n",
3153 inode->i_sb->s_id, inode->i_ino, -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 return;
3155 }
3156
Paul Moore20cdef82016-04-04 14:14:42 -04003157 isec = backing_inode_security(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003158 spin_lock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003159 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05003161 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003162 spin_unlock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003163
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 return;
3165}
3166
David Howells8f0cfa52008-04-29 00:59:41 -07003167static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168{
David Howells88e67f32008-11-14 10:39:21 +11003169 const struct cred *cred = current_cred();
3170
Eric Paris2875fa02011-04-28 16:04:24 -04003171 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172}
3173
Eric Paris828dfe12008-04-17 13:17:49 -04003174static int selinux_inode_listxattr(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175{
David Howells88e67f32008-11-14 10:39:21 +11003176 const struct cred *cred = current_cred();
3177
Eric Paris2875fa02011-04-28 16:04:24 -04003178 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179}
3180
David Howells8f0cfa52008-04-29 00:59:41 -07003181static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182{
Eric W. Biederman6b240302017-10-02 09:38:20 -05003183 if (strcmp(name, XATTR_NAME_SELINUX)) {
3184 int rc = cap_inode_removexattr(dentry, name);
3185 if (rc)
3186 return rc;
3187
3188 /* Not an attribute we recognize, so just check the
3189 ordinary setattr permission. */
3190 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192
3193 /* No one is allowed to remove a SELinux security label.
3194 You can change the label, but all data must be labeled. */
3195 return -EACCES;
3196}
3197
James Morrisd381d8a2005-10-30 14:59:22 -08003198/*
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003199 * Copy the inode security context value to the user.
James Morrisd381d8a2005-10-30 14:59:22 -08003200 *
3201 * Permission check is handled by selinux_inode_getxattr hook.
3202 */
Andreas Gruenbacherea861df2015-12-24 11:09:39 -05003203static int selinux_inode_getsecurity(struct inode *inode, const char *name, void **buffer, bool alloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204{
David P. Quigley42492592008-02-04 22:29:39 -08003205 u32 size;
3206 int error;
3207 char *context = NULL;
Paul Moore20cdef82016-04-04 14:14:42 -04003208 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00003210 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3211 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003213 /*
3214 * If the caller has CAP_MAC_ADMIN, then get the raw context
3215 * value even if it is not defined by current policy; otherwise,
3216 * use the in-core value under current policy.
3217 * Use the non-auditing forms of the permission checks since
3218 * getxattr may be called by unprivileged processes commonly
3219 * and lack of permission just means that we fall back to the
3220 * in-core context value, not a denial.
3221 */
Paul Moore20cdef82016-04-04 14:14:42 -04003222 isec = inode_security(inode);
Stephen Smalleydb590002017-04-20 11:31:30 -04003223 if (has_cap_mac_admin(false))
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003224 error = security_sid_to_context_force(&selinux_state,
3225 isec->sid, &context,
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003226 &size);
3227 else
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003228 error = security_sid_to_context(&selinux_state, isec->sid,
3229 &context, &size);
David P. Quigley42492592008-02-04 22:29:39 -08003230 if (error)
3231 return error;
3232 error = size;
3233 if (alloc) {
3234 *buffer = context;
3235 goto out_nofree;
3236 }
3237 kfree(context);
3238out_nofree:
3239 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240}
3241
3242static int selinux_inode_setsecurity(struct inode *inode, const char *name,
Eric Paris828dfe12008-04-17 13:17:49 -04003243 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244{
Paul Moore2c971652016-04-19 16:36:28 -04003245 struct inode_security_struct *isec = inode_security_novalidate(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 u32 newsid;
3247 int rc;
3248
3249 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3250 return -EOPNOTSUPP;
3251
3252 if (!value || !size)
3253 return -EACCES;
3254
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003255 rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3256 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 if (rc)
3258 return rc;
3259
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003260 spin_lock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003261 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05003263 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003264 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 return 0;
3266}
3267
3268static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
3269{
3270 const int len = sizeof(XATTR_NAME_SELINUX);
3271 if (buffer && len <= buffer_size)
3272 memcpy(buffer, XATTR_NAME_SELINUX, len);
3273 return len;
3274}
3275
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05003276static void selinux_inode_getsecid(struct inode *inode, u32 *secid)
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02003277{
Andreas Gruenbachere817c2f2016-02-18 12:04:08 +01003278 struct inode_security_struct *isec = inode_security_novalidate(inode);
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02003279 *secid = isec->sid;
3280}
3281
Vivek Goyal56909eb2016-07-13 10:44:48 -04003282static int selinux_inode_copy_up(struct dentry *src, struct cred **new)
3283{
3284 u32 sid;
3285 struct task_security_struct *tsec;
3286 struct cred *new_creds = *new;
3287
3288 if (new_creds == NULL) {
3289 new_creds = prepare_creds();
3290 if (!new_creds)
3291 return -ENOMEM;
3292 }
3293
3294 tsec = new_creds->security;
3295 /* Get label from overlay inode and set it in create_sid */
3296 selinux_inode_getsecid(d_inode(src), &sid);
3297 tsec->create_sid = sid;
3298 *new = new_creds;
3299 return 0;
3300}
3301
Vivek Goyal19472b62016-07-13 10:44:50 -04003302static int selinux_inode_copy_up_xattr(const char *name)
3303{
3304 /* The copy_up hook above sets the initial context on an inode, but we
3305 * don't then want to overwrite it by blindly copying all the lower
3306 * xattrs up. Instead, we have to filter out SELinux-related xattrs.
3307 */
3308 if (strcmp(name, XATTR_NAME_SELINUX) == 0)
3309 return 1; /* Discard */
3310 /*
3311 * Any other attribute apart from SELINUX is not claimed, supported
3312 * by selinux.
3313 */
3314 return -EOPNOTSUPP;
3315}
3316
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317/* file security operations */
3318
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003319static int selinux_revalidate_file_permission(struct file *file, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320{
David Howells88e67f32008-11-14 10:39:21 +11003321 const struct cred *cred = current_cred();
Al Viro496ad9a2013-01-23 17:07:38 -05003322 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
3325 if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
3326 mask |= MAY_APPEND;
3327
Paul Moore389fb8002009-03-27 17:10:34 -04003328 return file_has_perm(cred, file,
3329 file_mask_to_av(inode->i_mode, mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330}
3331
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003332static int selinux_file_permission(struct file *file, int mask)
3333{
Al Viro496ad9a2013-01-23 17:07:38 -05003334 struct inode *inode = file_inode(file);
Stephen Smalley20dda182009-06-22 14:54:53 -04003335 struct file_security_struct *fsec = file->f_security;
Andreas Gruenbacherb1973672016-01-05 23:12:33 +01003336 struct inode_security_struct *isec;
Stephen Smalley20dda182009-06-22 14:54:53 -04003337 u32 sid = current_sid();
3338
Paul Moore389fb8002009-03-27 17:10:34 -04003339 if (!mask)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003340 /* No permission to check. Existence test. */
3341 return 0;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003342
Andreas Gruenbacherb1973672016-01-05 23:12:33 +01003343 isec = inode_security(inode);
Stephen Smalley20dda182009-06-22 14:54:53 -04003344 if (sid == fsec->sid && fsec->isid == isec->sid &&
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003345 fsec->pseqno == avc_policy_seqno(&selinux_state))
Eric Paris83d49852012-04-04 13:45:40 -04003346 /* No change since file_open check. */
Stephen Smalley20dda182009-06-22 14:54:53 -04003347 return 0;
3348
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003349 return selinux_revalidate_file_permission(file, mask);
3350}
3351
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352static int selinux_file_alloc_security(struct file *file)
3353{
3354 return file_alloc_security(file);
3355}
3356
3357static void selinux_file_free_security(struct file *file)
3358{
3359 file_free_security(file);
3360}
3361
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003362/*
3363 * Check whether a task has the ioctl permission and cmd
3364 * operation to an inode.
3365 */
Geliang Tang1d2a1682015-10-21 17:44:27 -04003366static int ioctl_has_perm(const struct cred *cred, struct file *file,
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003367 u32 requested, u16 cmd)
3368{
3369 struct common_audit_data ad;
3370 struct file_security_struct *fsec = file->f_security;
3371 struct inode *inode = file_inode(file);
Paul Moore20cdef82016-04-04 14:14:42 -04003372 struct inode_security_struct *isec;
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003373 struct lsm_ioctlop_audit ioctl;
3374 u32 ssid = cred_sid(cred);
3375 int rc;
3376 u8 driver = cmd >> 8;
3377 u8 xperm = cmd & 0xff;
3378
3379 ad.type = LSM_AUDIT_DATA_IOCTL_OP;
3380 ad.u.op = &ioctl;
3381 ad.u.op->cmd = cmd;
3382 ad.u.op->path = file->f_path;
3383
3384 if (ssid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003385 rc = avc_has_perm(&selinux_state,
3386 ssid, fsec->sid,
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003387 SECCLASS_FD,
3388 FD__USE,
3389 &ad);
3390 if (rc)
3391 goto out;
3392 }
3393
3394 if (unlikely(IS_PRIVATE(inode)))
3395 return 0;
3396
Paul Moore20cdef82016-04-04 14:14:42 -04003397 isec = inode_security(inode);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003398 rc = avc_has_extended_perms(&selinux_state,
3399 ssid, isec->sid, isec->sclass,
3400 requested, driver, xperm, &ad);
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003401out:
3402 return rc;
3403}
3404
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405static int selinux_file_ioctl(struct file *file, unsigned int cmd,
3406 unsigned long arg)
3407{
David Howells88e67f32008-11-14 10:39:21 +11003408 const struct cred *cred = current_cred();
Eric Paris0b24dcb2011-02-25 15:39:20 -05003409 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410
Eric Paris0b24dcb2011-02-25 15:39:20 -05003411 switch (cmd) {
3412 case FIONREAD:
3413 /* fall through */
3414 case FIBMAP:
3415 /* fall through */
3416 case FIGETBSZ:
3417 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003418 case FS_IOC_GETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003419 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003420 case FS_IOC_GETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003421 error = file_has_perm(cred, file, FILE__GETATTR);
3422 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423
Al Viro2f99c362012-03-23 16:04:05 -04003424 case FS_IOC_SETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003425 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003426 case FS_IOC_SETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003427 error = file_has_perm(cred, file, FILE__SETATTR);
3428 break;
3429
3430 /* sys_ioctl() checks */
3431 case FIONBIO:
3432 /* fall through */
3433 case FIOASYNC:
3434 error = file_has_perm(cred, file, 0);
3435 break;
3436
3437 case KDSKBENT:
3438 case KDSKBSENT:
Eric Paris6a9de492012-01-03 12:25:14 -05003439 error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04003440 SECURITY_CAP_AUDIT, true);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003441 break;
3442
3443 /* default case assumes that the command will go
3444 * to the file's ioctl() function.
3445 */
3446 default:
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003447 error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003448 }
3449 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450}
3451
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003452static int default_noexec;
3453
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
3455{
David Howells88e67f32008-11-14 10:39:21 +11003456 const struct cred *cred = current_cred();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003457 u32 sid = cred_sid(cred);
David Howellsd84f4f92008-11-14 10:39:23 +11003458 int rc = 0;
David Howells88e67f32008-11-14 10:39:21 +11003459
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003460 if (default_noexec &&
Stephen Smalley892e8ca2015-07-10 09:40:59 -04003461 (prot & PROT_EXEC) && (!file || IS_PRIVATE(file_inode(file)) ||
3462 (!shared && (prot & PROT_WRITE)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 /*
3464 * We are making executable an anonymous mapping or a
3465 * private file mapping that will also be writable.
3466 * This has an additional check.
3467 */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003468 rc = avc_has_perm(&selinux_state,
3469 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003470 PROCESS__EXECMEM, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 if (rc)
David Howellsd84f4f92008-11-14 10:39:23 +11003472 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474
3475 if (file) {
3476 /* read access is always possible with a mapping */
3477 u32 av = FILE__READ;
3478
3479 /* write access only matters if the mapping is shared */
3480 if (shared && (prot & PROT_WRITE))
3481 av |= FILE__WRITE;
3482
3483 if (prot & PROT_EXEC)
3484 av |= FILE__EXECUTE;
3485
David Howells88e67f32008-11-14 10:39:21 +11003486 return file_has_perm(cred, file, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 }
David Howellsd84f4f92008-11-14 10:39:23 +11003488
3489error:
3490 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491}
3492
Al Viroe5467852012-05-30 13:30:51 -04003493static int selinux_mmap_addr(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07003495 int rc = 0;
Paul Moore98883bf2014-03-19 16:46:11 -04003496
3497 if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
3498 u32 sid = current_sid();
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003499 rc = avc_has_perm(&selinux_state,
3500 sid, sid, SECCLASS_MEMPROTECT,
Paul Moore98883bf2014-03-19 16:46:11 -04003501 MEMPROTECT__MMAP_ZERO, NULL);
3502 }
3503
3504 return rc;
Al Viroe5467852012-05-30 13:30:51 -04003505}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506
Al Viroe5467852012-05-30 13:30:51 -04003507static int selinux_mmap_file(struct file *file, unsigned long reqprot,
3508 unsigned long prot, unsigned long flags)
3509{
Stephen Smalley3ba4bf52017-05-05 09:14:48 -04003510 struct common_audit_data ad;
3511 int rc;
3512
3513 if (file) {
3514 ad.type = LSM_AUDIT_DATA_FILE;
3515 ad.u.file = file;
3516 rc = inode_has_perm(current_cred(), file_inode(file),
3517 FILE__MAP, &ad);
3518 if (rc)
3519 return rc;
3520 }
3521
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003522 if (selinux_state.checkreqprot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 prot = reqprot;
3524
3525 return file_map_prot_check(file, prot,
3526 (flags & MAP_TYPE) == MAP_SHARED);
3527}
3528
3529static int selinux_file_mprotect(struct vm_area_struct *vma,
3530 unsigned long reqprot,
3531 unsigned long prot)
3532{
David Howells88e67f32008-11-14 10:39:21 +11003533 const struct cred *cred = current_cred();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003534 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003536 if (selinux_state.checkreqprot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 prot = reqprot;
3538
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003539 if (default_noexec &&
3540 (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
James Morrisd541bbe2009-01-29 12:19:51 +11003541 int rc = 0;
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003542 if (vma->vm_start >= vma->vm_mm->start_brk &&
3543 vma->vm_end <= vma->vm_mm->brk) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003544 rc = avc_has_perm(&selinux_state,
3545 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003546 PROCESS__EXECHEAP, NULL);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003547 } else if (!vma->vm_file &&
Stephen Smalleyc2316db2016-04-08 13:55:03 -04003548 ((vma->vm_start <= vma->vm_mm->start_stack &&
3549 vma->vm_end >= vma->vm_mm->start_stack) ||
Andy Lutomirskid17af502016-09-30 10:58:58 -07003550 vma_is_stack_for_current(vma))) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003551 rc = avc_has_perm(&selinux_state,
3552 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003553 PROCESS__EXECSTACK, NULL);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003554 } else if (vma->vm_file && vma->anon_vma) {
3555 /*
3556 * We are making executable a file mapping that has
3557 * had some COW done. Since pages might have been
3558 * written, check ability to execute the possibly
3559 * modified content. This typically should only
3560 * occur for text relocations.
3561 */
David Howellsd84f4f92008-11-14 10:39:23 +11003562 rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003563 }
Lorenzo Hernandez García-Hierro6b992192005-06-25 14:54:34 -07003564 if (rc)
3565 return rc;
3566 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567
3568 return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
3569}
3570
3571static int selinux_file_lock(struct file *file, unsigned int cmd)
3572{
David Howells88e67f32008-11-14 10:39:21 +11003573 const struct cred *cred = current_cred();
3574
3575 return file_has_perm(cred, file, FILE__LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576}
3577
3578static int selinux_file_fcntl(struct file *file, unsigned int cmd,
3579 unsigned long arg)
3580{
David Howells88e67f32008-11-14 10:39:21 +11003581 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 int err = 0;
3583
3584 switch (cmd) {
Eric Paris828dfe12008-04-17 13:17:49 -04003585 case F_SETFL:
Eric Paris828dfe12008-04-17 13:17:49 -04003586 if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
David Howells88e67f32008-11-14 10:39:21 +11003587 err = file_has_perm(cred, file, FILE__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003589 }
3590 /* fall through */
3591 case F_SETOWN:
3592 case F_SETSIG:
3593 case F_GETFL:
3594 case F_GETOWN:
3595 case F_GETSIG:
Cyrill Gorcunov1d151c32012-07-30 14:43:00 -07003596 case F_GETOWNER_UIDS:
Eric Paris828dfe12008-04-17 13:17:49 -04003597 /* Just check FD__USE permission */
David Howells88e67f32008-11-14 10:39:21 +11003598 err = file_has_perm(cred, file, 0);
Eric Paris828dfe12008-04-17 13:17:49 -04003599 break;
3600 case F_GETLK:
3601 case F_SETLK:
3602 case F_SETLKW:
Jeff Layton0d3f7a22014-04-22 08:23:58 -04003603 case F_OFD_GETLK:
3604 case F_OFD_SETLK:
3605 case F_OFD_SETLKW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606#if BITS_PER_LONG == 32
Eric Paris828dfe12008-04-17 13:17:49 -04003607 case F_GETLK64:
3608 case F_SETLK64:
3609 case F_SETLKW64:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610#endif
David Howells88e67f32008-11-14 10:39:21 +11003611 err = file_has_perm(cred, file, FILE__LOCK);
Eric Paris828dfe12008-04-17 13:17:49 -04003612 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 }
3614
3615 return err;
3616}
3617
Jeff Laytone0b93ed2014-08-22 11:27:32 -04003618static void selinux_file_set_fowner(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 struct file_security_struct *fsec;
3621
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 fsec = file->f_security;
David Howells275bb412008-11-14 10:39:19 +11003623 fsec->fown_sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624}
3625
3626static int selinux_file_send_sigiotask(struct task_struct *tsk,
3627 struct fown_struct *fown, int signum)
3628{
Eric Paris828dfe12008-04-17 13:17:49 -04003629 struct file *file;
Stephen Smalley65c90bc2009-05-04 15:43:18 -04003630 u32 sid = task_sid(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 struct file_security_struct *fsec;
3633
3634 /* struct fown_struct is never outside the context of a struct file */
Eric Paris828dfe12008-04-17 13:17:49 -04003635 file = container_of(fown, struct file, f_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 fsec = file->f_security;
3638
3639 if (!signum)
3640 perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
3641 else
3642 perm = signal_to_av(signum);
3643
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003644 return avc_has_perm(&selinux_state,
3645 fsec->fown_sid, sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 SECCLASS_PROCESS, perm, NULL);
3647}
3648
3649static int selinux_file_receive(struct file *file)
3650{
David Howells88e67f32008-11-14 10:39:21 +11003651 const struct cred *cred = current_cred();
3652
3653 return file_has_perm(cred, file, file_to_av(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654}
3655
Al Viro94817692018-07-10 14:13:18 -04003656static int selinux_file_open(struct file *file)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003657{
3658 struct file_security_struct *fsec;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003659 struct inode_security_struct *isec;
David Howellsd84f4f92008-11-14 10:39:23 +11003660
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003661 fsec = file->f_security;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05003662 isec = inode_security(file_inode(file));
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003663 /*
3664 * Save inode label and policy sequence number
3665 * at open-time so that selinux_file_permission
3666 * can determine whether revalidation is necessary.
3667 * Task label is already saved in the file security
3668 * struct as its SID.
3669 */
3670 fsec->isid = isec->sid;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003671 fsec->pseqno = avc_policy_seqno(&selinux_state);
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003672 /*
3673 * Since the inode label or policy seqno may have changed
3674 * between the selinux_inode_permission check and the saving
3675 * of state above, recheck that access is still permitted.
3676 * Otherwise, access might never be revalidated against the
3677 * new inode label or new policy.
3678 * This check is not redundant - do not remove.
3679 */
Al Viro94817692018-07-10 14:13:18 -04003680 return file_path_has_perm(file->f_cred, file, open_file_to_av(file));
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003681}
3682
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683/* task security operations */
3684
Tetsuo Handaa79be232017-03-28 23:08:45 +09003685static int selinux_task_alloc(struct task_struct *task,
3686 unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003688 u32 sid = current_sid();
3689
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003690 return avc_has_perm(&selinux_state,
3691 sid, sid, SECCLASS_PROCESS, PROCESS__FORK, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692}
3693
David Howellsf1752ee2008-11-14 10:39:17 +11003694/*
David Howellsee18d642009-09-02 09:14:21 +01003695 * allocate the SELinux part of blank credentials
3696 */
3697static int selinux_cred_alloc_blank(struct cred *cred, gfp_t gfp)
3698{
3699 struct task_security_struct *tsec;
3700
3701 tsec = kzalloc(sizeof(struct task_security_struct), gfp);
3702 if (!tsec)
3703 return -ENOMEM;
3704
3705 cred->security = tsec;
3706 return 0;
3707}
3708
3709/*
David Howellsf1752ee2008-11-14 10:39:17 +11003710 * detach and free the LSM part of a set of credentials
3711 */
3712static void selinux_cred_free(struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713{
David Howellsf1752ee2008-11-14 10:39:17 +11003714 struct task_security_struct *tsec = cred->security;
David Howellse0e81732009-09-02 09:13:40 +01003715
Tetsuo Handa2edeaa32011-02-07 13:36:10 +00003716 /*
3717 * cred->security == NULL if security_cred_alloc_blank() or
3718 * security_prepare_creds() returned an error.
3719 */
3720 BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE);
David Howellse0e81732009-09-02 09:13:40 +01003721 cred->security = (void *) 0x7UL;
David Howellsf1752ee2008-11-14 10:39:17 +11003722 kfree(tsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723}
3724
David Howellsd84f4f92008-11-14 10:39:23 +11003725/*
3726 * prepare a new set of credentials for modification
3727 */
3728static int selinux_cred_prepare(struct cred *new, const struct cred *old,
3729 gfp_t gfp)
3730{
3731 const struct task_security_struct *old_tsec;
3732 struct task_security_struct *tsec;
3733
3734 old_tsec = old->security;
3735
3736 tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp);
3737 if (!tsec)
3738 return -ENOMEM;
3739
3740 new->security = tsec;
3741 return 0;
3742}
3743
3744/*
David Howellsee18d642009-09-02 09:14:21 +01003745 * transfer the SELinux data to a blank set of creds
3746 */
3747static void selinux_cred_transfer(struct cred *new, const struct cred *old)
3748{
3749 const struct task_security_struct *old_tsec = old->security;
3750 struct task_security_struct *tsec = new->security;
3751
3752 *tsec = *old_tsec;
3753}
3754
Matthew Garrett3ec30112018-01-08 13:36:19 -08003755static void selinux_cred_getsecid(const struct cred *c, u32 *secid)
3756{
3757 *secid = cred_sid(c);
3758}
3759
David Howellsee18d642009-09-02 09:14:21 +01003760/*
David Howells3a3b7ce2008-11-14 10:39:28 +11003761 * set the security data for a kernel service
3762 * - all the creation contexts are set to unlabelled
3763 */
3764static int selinux_kernel_act_as(struct cred *new, u32 secid)
3765{
3766 struct task_security_struct *tsec = new->security;
3767 u32 sid = current_sid();
3768 int ret;
3769
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003770 ret = avc_has_perm(&selinux_state,
3771 sid, secid,
David Howells3a3b7ce2008-11-14 10:39:28 +11003772 SECCLASS_KERNEL_SERVICE,
3773 KERNEL_SERVICE__USE_AS_OVERRIDE,
3774 NULL);
3775 if (ret == 0) {
3776 tsec->sid = secid;
3777 tsec->create_sid = 0;
3778 tsec->keycreate_sid = 0;
3779 tsec->sockcreate_sid = 0;
3780 }
3781 return ret;
3782}
3783
3784/*
3785 * set the file creation context in a security record to the same as the
3786 * objective context of the specified inode
3787 */
3788static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
3789{
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05003790 struct inode_security_struct *isec = inode_security(inode);
David Howells3a3b7ce2008-11-14 10:39:28 +11003791 struct task_security_struct *tsec = new->security;
3792 u32 sid = current_sid();
3793 int ret;
3794
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003795 ret = avc_has_perm(&selinux_state,
3796 sid, isec->sid,
David Howells3a3b7ce2008-11-14 10:39:28 +11003797 SECCLASS_KERNEL_SERVICE,
3798 KERNEL_SERVICE__CREATE_FILES_AS,
3799 NULL);
3800
3801 if (ret == 0)
3802 tsec->create_sid = isec->sid;
David Howellsef574712010-02-26 01:56:16 +00003803 return ret;
David Howells3a3b7ce2008-11-14 10:39:28 +11003804}
3805
Eric Parisdd8dbf22009-11-03 16:35:32 +11003806static int selinux_kernel_module_request(char *kmod_name)
Eric Paris25354c42009-08-13 09:45:03 -04003807{
Eric Parisdd8dbf22009-11-03 16:35:32 +11003808 struct common_audit_data ad;
3809
Eric Paris50c205f2012-04-04 15:01:43 -04003810 ad.type = LSM_AUDIT_DATA_KMOD;
Eric Parisdd8dbf22009-11-03 16:35:32 +11003811 ad.u.kmod_name = kmod_name;
3812
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003813 return avc_has_perm(&selinux_state,
3814 current_sid(), SECINITSID_KERNEL, SECCLASS_SYSTEM,
Eric Parisdd8dbf22009-11-03 16:35:32 +11003815 SYSTEM__MODULE_REQUEST, &ad);
Eric Paris25354c42009-08-13 09:45:03 -04003816}
3817
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003818static int selinux_kernel_module_from_file(struct file *file)
3819{
3820 struct common_audit_data ad;
3821 struct inode_security_struct *isec;
3822 struct file_security_struct *fsec;
3823 u32 sid = current_sid();
3824 int rc;
3825
3826 /* init_module */
3827 if (file == NULL)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003828 return avc_has_perm(&selinux_state,
3829 sid, sid, SECCLASS_SYSTEM,
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003830 SYSTEM__MODULE_LOAD, NULL);
3831
3832 /* finit_module */
Paul Moore20cdef82016-04-04 14:14:42 -04003833
Vivek Goyal43af5de2016-09-09 11:37:49 -04003834 ad.type = LSM_AUDIT_DATA_FILE;
3835 ad.u.file = file;
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003836
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003837 fsec = file->f_security;
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003838 if (sid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003839 rc = avc_has_perm(&selinux_state,
3840 sid, fsec->sid, SECCLASS_FD, FD__USE, &ad);
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003841 if (rc)
3842 return rc;
3843 }
3844
Paul Moore20cdef82016-04-04 14:14:42 -04003845 isec = inode_security(file_inode(file));
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003846 return avc_has_perm(&selinux_state,
3847 sid, isec->sid, SECCLASS_SYSTEM,
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003848 SYSTEM__MODULE_LOAD, &ad);
3849}
3850
3851static int selinux_kernel_read_file(struct file *file,
3852 enum kernel_read_file_id id)
3853{
3854 int rc = 0;
3855
3856 switch (id) {
3857 case READING_MODULE:
3858 rc = selinux_kernel_module_from_file(file);
3859 break;
3860 default:
3861 break;
3862 }
3863
3864 return rc;
3865}
3866
Mimi Zoharc77b8cd2018-07-13 14:06:02 -04003867static int selinux_kernel_load_data(enum kernel_load_data_id id)
3868{
3869 int rc = 0;
3870
3871 switch (id) {
3872 case LOADING_MODULE:
3873 rc = selinux_kernel_module_from_file(NULL);
3874 default:
3875 break;
3876 }
3877
3878 return rc;
3879}
3880
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
3882{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003883 return avc_has_perm(&selinux_state,
3884 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003885 PROCESS__SETPGID, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886}
3887
3888static int selinux_task_getpgid(struct task_struct *p)
3889{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003890 return avc_has_perm(&selinux_state,
3891 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003892 PROCESS__GETPGID, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893}
3894
3895static int selinux_task_getsid(struct task_struct *p)
3896{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003897 return avc_has_perm(&selinux_state,
3898 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003899 PROCESS__GETSESSION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900}
3901
David Quigleyf9008e4c2006-06-30 01:55:46 -07003902static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
3903{
David Howells275bb412008-11-14 10:39:19 +11003904 *secid = task_sid(p);
David Quigleyf9008e4c2006-06-30 01:55:46 -07003905}
3906
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907static int selinux_task_setnice(struct task_struct *p, int nice)
3908{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003909 return avc_has_perm(&selinux_state,
3910 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003911 PROCESS__SETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912}
3913
James Morris03e68062006-06-23 02:03:58 -07003914static int selinux_task_setioprio(struct task_struct *p, int ioprio)
3915{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003916 return avc_has_perm(&selinux_state,
3917 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003918 PROCESS__SETSCHED, NULL);
James Morris03e68062006-06-23 02:03:58 -07003919}
3920
David Quigleya1836a42006-06-30 01:55:49 -07003921static int selinux_task_getioprio(struct task_struct *p)
3922{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003923 return avc_has_perm(&selinux_state,
3924 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003925 PROCESS__GETSCHED, NULL);
David Quigleya1836a42006-06-30 01:55:49 -07003926}
3927
Corentin LABBE42985552017-10-04 20:32:18 +02003928static int selinux_task_prlimit(const struct cred *cred, const struct cred *tcred,
3929 unsigned int flags)
Stephen Smalley791ec492017-02-17 07:57:00 -05003930{
3931 u32 av = 0;
3932
Stephen Smalley84e68852017-02-28 09:35:08 -05003933 if (!flags)
3934 return 0;
Stephen Smalley791ec492017-02-17 07:57:00 -05003935 if (flags & LSM_PRLIMIT_WRITE)
3936 av |= PROCESS__SETRLIMIT;
3937 if (flags & LSM_PRLIMIT_READ)
3938 av |= PROCESS__GETRLIMIT;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003939 return avc_has_perm(&selinux_state,
3940 cred_sid(cred), cred_sid(tcred),
Stephen Smalley791ec492017-02-17 07:57:00 -05003941 SECCLASS_PROCESS, av, NULL);
3942}
3943
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003944static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
3945 struct rlimit *new_rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946{
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003947 struct rlimit *old_rlim = p->signal->rlim + resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948
3949 /* Control the ability to change the hard limit (whether
3950 lowering or raising it), so that the hard limit can
3951 later be used as a safe reset point for the soft limit
David Howellsd84f4f92008-11-14 10:39:23 +11003952 upon context transitions. See selinux_bprm_committing_creds. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 if (old_rlim->rlim_max != new_rlim->rlim_max)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003954 return avc_has_perm(&selinux_state,
3955 current_sid(), task_sid(p),
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003956 SECCLASS_PROCESS, PROCESS__SETRLIMIT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957
3958 return 0;
3959}
3960
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09003961static int selinux_task_setscheduler(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003963 return avc_has_perm(&selinux_state,
3964 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003965 PROCESS__SETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966}
3967
3968static int selinux_task_getscheduler(struct task_struct *p)
3969{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003970 return avc_has_perm(&selinux_state,
3971 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003972 PROCESS__GETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973}
3974
David Quigley35601542006-06-23 02:04:01 -07003975static int selinux_task_movememory(struct task_struct *p)
3976{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003977 return avc_has_perm(&selinux_state,
3978 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003979 PROCESS__SETSCHED, NULL);
David Quigley35601542006-06-23 02:04:01 -07003980}
3981
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02003982static int selinux_task_kill(struct task_struct *p, struct kernel_siginfo *info,
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04003983 int sig, const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984{
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04003985 u32 secid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 if (!sig)
3989 perm = PROCESS__SIGNULL; /* null signal; existence test */
3990 else
3991 perm = signal_to_av(sig);
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04003992 if (!cred)
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003993 secid = current_sid();
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04003994 else
3995 secid = cred_sid(cred);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003996 return avc_has_perm(&selinux_state,
3997 secid, task_sid(p), SECCLASS_PROCESS, perm, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998}
3999
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000static void selinux_task_to_inode(struct task_struct *p,
4001 struct inode *inode)
4002{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 struct inode_security_struct *isec = inode->i_security;
David Howells275bb412008-11-14 10:39:19 +11004004 u32 sid = task_sid(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004006 spin_lock(&isec->lock);
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01004007 isec->sclass = inode_mode_to_security_class(inode->i_mode);
David Howells275bb412008-11-14 10:39:19 +11004008 isec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004009 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004010 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011}
4012
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004014static int selinux_parse_skb_ipv4(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04004015 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016{
4017 int offset, ihlen, ret = -EINVAL;
4018 struct iphdr _iph, *ih;
4019
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03004020 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
4022 if (ih == NULL)
4023 goto out;
4024
4025 ihlen = ih->ihl * 4;
4026 if (ihlen < sizeof(_iph))
4027 goto out;
4028
Eric Paris48c62af2012-04-02 13:15:44 -04004029 ad->u.net->v4info.saddr = ih->saddr;
4030 ad->u.net->v4info.daddr = ih->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 ret = 0;
4032
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004033 if (proto)
4034 *proto = ih->protocol;
4035
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 switch (ih->protocol) {
Eric Paris828dfe12008-04-17 13:17:49 -04004037 case IPPROTO_TCP: {
4038 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039
Eric Paris828dfe12008-04-17 13:17:49 -04004040 if (ntohs(ih->frag_off) & IP_OFFSET)
4041 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042
4043 offset += ihlen;
4044 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4045 if (th == NULL)
4046 break;
4047
Eric Paris48c62af2012-04-02 13:15:44 -04004048 ad->u.net->sport = th->source;
4049 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052
Eric Paris828dfe12008-04-17 13:17:49 -04004053 case IPPROTO_UDP: {
4054 struct udphdr _udph, *uh;
4055
4056 if (ntohs(ih->frag_off) & IP_OFFSET)
4057 break;
4058
4059 offset += ihlen;
4060 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4061 if (uh == NULL)
4062 break;
4063
Eric Paris48c62af2012-04-02 13:15:44 -04004064 ad->u.net->sport = uh->source;
4065 ad->u.net->dport = uh->dest;
Eric Paris828dfe12008-04-17 13:17:49 -04004066 break;
4067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068
James Morris2ee92d42006-11-13 16:09:01 -08004069 case IPPROTO_DCCP: {
4070 struct dccp_hdr _dccph, *dh;
4071
4072 if (ntohs(ih->frag_off) & IP_OFFSET)
4073 break;
4074
4075 offset += ihlen;
4076 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4077 if (dh == NULL)
4078 break;
4079
Eric Paris48c62af2012-04-02 13:15:44 -04004080 ad->u.net->sport = dh->dccph_sport;
4081 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08004082 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004083 }
James Morris2ee92d42006-11-13 16:09:01 -08004084
Richard Hainesd4529302018-02-13 20:57:18 +00004085#if IS_ENABLED(CONFIG_IP_SCTP)
4086 case IPPROTO_SCTP: {
4087 struct sctphdr _sctph, *sh;
4088
4089 if (ntohs(ih->frag_off) & IP_OFFSET)
4090 break;
4091
4092 offset += ihlen;
4093 sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4094 if (sh == NULL)
4095 break;
4096
4097 ad->u.net->sport = sh->source;
4098 ad->u.net->dport = sh->dest;
4099 break;
4100 }
4101#endif
Eric Paris828dfe12008-04-17 13:17:49 -04004102 default:
4103 break;
4104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105out:
4106 return ret;
4107}
4108
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04004109#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110
4111/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004112static int selinux_parse_skb_ipv6(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04004113 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114{
4115 u8 nexthdr;
4116 int ret = -EINVAL, offset;
4117 struct ipv6hdr _ipv6h, *ip6;
Jesse Gross75f28112011-11-30 17:05:51 -08004118 __be16 frag_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03004120 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
4122 if (ip6 == NULL)
4123 goto out;
4124
Eric Paris48c62af2012-04-02 13:15:44 -04004125 ad->u.net->v6info.saddr = ip6->saddr;
4126 ad->u.net->v6info.daddr = ip6->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 ret = 0;
4128
4129 nexthdr = ip6->nexthdr;
4130 offset += sizeof(_ipv6h);
Jesse Gross75f28112011-11-30 17:05:51 -08004131 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 if (offset < 0)
4133 goto out;
4134
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004135 if (proto)
4136 *proto = nexthdr;
4137
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138 switch (nexthdr) {
4139 case IPPROTO_TCP: {
Eric Paris828dfe12008-04-17 13:17:49 -04004140 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141
4142 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4143 if (th == NULL)
4144 break;
4145
Eric Paris48c62af2012-04-02 13:15:44 -04004146 ad->u.net->sport = th->source;
4147 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 break;
4149 }
4150
4151 case IPPROTO_UDP: {
4152 struct udphdr _udph, *uh;
4153
4154 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4155 if (uh == NULL)
4156 break;
4157
Eric Paris48c62af2012-04-02 13:15:44 -04004158 ad->u.net->sport = uh->source;
4159 ad->u.net->dport = uh->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 break;
4161 }
4162
James Morris2ee92d42006-11-13 16:09:01 -08004163 case IPPROTO_DCCP: {
4164 struct dccp_hdr _dccph, *dh;
4165
4166 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4167 if (dh == NULL)
4168 break;
4169
Eric Paris48c62af2012-04-02 13:15:44 -04004170 ad->u.net->sport = dh->dccph_sport;
4171 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08004172 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004173 }
James Morris2ee92d42006-11-13 16:09:01 -08004174
Richard Hainesd4529302018-02-13 20:57:18 +00004175#if IS_ENABLED(CONFIG_IP_SCTP)
4176 case IPPROTO_SCTP: {
4177 struct sctphdr _sctph, *sh;
4178
4179 sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4180 if (sh == NULL)
4181 break;
4182
4183 ad->u.net->sport = sh->source;
4184 ad->u.net->dport = sh->dest;
4185 break;
4186 }
4187#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 /* includes fragments */
4189 default:
4190 break;
4191 }
4192out:
4193 return ret;
4194}
4195
4196#endif /* IPV6 */
4197
Thomas Liu2bf49692009-07-14 12:14:09 -04004198static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
David Howellscf9481e2008-07-27 21:31:07 +10004199 char **_addrp, int src, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200{
David Howellscf9481e2008-07-27 21:31:07 +10004201 char *addrp;
4202 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203
Eric Paris48c62af2012-04-02 13:15:44 -04004204 switch (ad->u.net->family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205 case PF_INET:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004206 ret = selinux_parse_skb_ipv4(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10004207 if (ret)
4208 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04004209 addrp = (char *)(src ? &ad->u.net->v4info.saddr :
4210 &ad->u.net->v4info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10004211 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04004213#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 case PF_INET6:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004215 ret = selinux_parse_skb_ipv6(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10004216 if (ret)
4217 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04004218 addrp = (char *)(src ? &ad->u.net->v6info.saddr :
4219 &ad->u.net->v6info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10004220 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221#endif /* IPV6 */
4222 default:
David Howellscf9481e2008-07-27 21:31:07 +10004223 addrp = NULL;
4224 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 }
4226
David Howellscf9481e2008-07-27 21:31:07 +10004227parse_error:
peter enderborgc103a912018-06-12 10:09:03 +02004228 pr_warn(
David Howellscf9481e2008-07-27 21:31:07 +10004229 "SELinux: failure in selinux_parse_skb(),"
4230 " unable to parse packet\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 return ret;
David Howellscf9481e2008-07-27 21:31:07 +10004232
4233okay:
4234 if (_addrp)
4235 *_addrp = addrp;
4236 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237}
4238
Paul Moore4f6a9932007-03-01 14:35:22 -05004239/**
Paul Moore220deb92008-01-29 08:38:23 -05004240 * selinux_skb_peerlbl_sid - Determine the peer label of a packet
Paul Moore4f6a9932007-03-01 14:35:22 -05004241 * @skb: the packet
Paul Moore75e22912008-01-29 08:38:04 -05004242 * @family: protocol family
Paul Moore220deb92008-01-29 08:38:23 -05004243 * @sid: the packet's peer label SID
Paul Moore4f6a9932007-03-01 14:35:22 -05004244 *
4245 * Description:
Paul Moore220deb92008-01-29 08:38:23 -05004246 * Check the various different forms of network peer labeling and determine
4247 * the peer label/SID for the packet; most of the magic actually occurs in
4248 * the security server function security_net_peersid_cmp(). The function
4249 * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
4250 * or -EACCES if @sid is invalid due to inconsistencies with the different
4251 * peer labels.
Paul Moore4f6a9932007-03-01 14:35:22 -05004252 *
4253 */
Paul Moore220deb92008-01-29 08:38:23 -05004254static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
Paul Moore4f6a9932007-03-01 14:35:22 -05004255{
Paul Moore71f1cb02008-01-29 08:51:16 -05004256 int err;
Paul Moore4f6a9932007-03-01 14:35:22 -05004257 u32 xfrm_sid;
4258 u32 nlbl_sid;
Paul Moore220deb92008-01-29 08:38:23 -05004259 u32 nlbl_type;
Paul Moore4f6a9932007-03-01 14:35:22 -05004260
Paul Moore817eff72013-12-10 14:57:54 -05004261 err = selinux_xfrm_skb_sid(skb, &xfrm_sid);
Paul Moorebed4d7e2013-07-23 17:38:40 -04004262 if (unlikely(err))
4263 return -EACCES;
4264 err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
4265 if (unlikely(err))
4266 return -EACCES;
Paul Moore220deb92008-01-29 08:38:23 -05004267
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004268 err = security_net_peersid_resolve(&selinux_state, nlbl_sid,
4269 nlbl_type, xfrm_sid, sid);
Paul Moore71f1cb02008-01-29 08:51:16 -05004270 if (unlikely(err)) {
peter enderborgc103a912018-06-12 10:09:03 +02004271 pr_warn(
Paul Moore71f1cb02008-01-29 08:51:16 -05004272 "SELinux: failure in selinux_skb_peerlbl_sid(),"
4273 " unable to determine packet's peer label\n");
Paul Moore220deb92008-01-29 08:38:23 -05004274 return -EACCES;
Paul Moore71f1cb02008-01-29 08:51:16 -05004275 }
Paul Moore220deb92008-01-29 08:38:23 -05004276
4277 return 0;
Paul Moore4f6a9932007-03-01 14:35:22 -05004278}
4279
Paul Moore446b8022013-12-04 16:10:51 -05004280/**
4281 * selinux_conn_sid - Determine the child socket label for a connection
4282 * @sk_sid: the parent socket's SID
4283 * @skb_sid: the packet's SID
4284 * @conn_sid: the resulting connection SID
4285 *
4286 * If @skb_sid is valid then the user:role:type information from @sk_sid is
4287 * combined with the MLS information from @skb_sid in order to create
4288 * @conn_sid. If @skb_sid is not valid then then @conn_sid is simply a copy
4289 * of @sk_sid. Returns zero on success, negative values on failure.
4290 *
4291 */
4292static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid)
4293{
4294 int err = 0;
4295
4296 if (skb_sid != SECSID_NULL)
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004297 err = security_sid_mls_copy(&selinux_state, sk_sid, skb_sid,
4298 conn_sid);
Paul Moore446b8022013-12-04 16:10:51 -05004299 else
4300 *conn_sid = sk_sid;
4301
4302 return err;
4303}
4304
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305/* socket security operations */
Paul Moored4f2d972010-04-22 14:46:18 -04004306
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004307static int socket_sockcreate_sid(const struct task_security_struct *tsec,
4308 u16 secclass, u32 *socksid)
Paul Moored4f2d972010-04-22 14:46:18 -04004309{
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004310 if (tsec->sockcreate_sid > SECSID_NULL) {
4311 *socksid = tsec->sockcreate_sid;
4312 return 0;
4313 }
4314
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004315 return security_transition_sid(&selinux_state, tsec->sid, tsec->sid,
4316 secclass, NULL, socksid);
Paul Moored4f2d972010-04-22 14:46:18 -04004317}
4318
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004319static int sock_has_perm(struct sock *sk, u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320{
Paul Moore253bfae2010-04-22 14:46:19 -04004321 struct sk_security_struct *sksec = sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004322 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004323 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324
Paul Moore253bfae2010-04-22 14:46:19 -04004325 if (sksec->sid == SECINITSID_KERNEL)
4326 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327
Eric Paris50c205f2012-04-04 15:01:43 -04004328 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004329 ad.u.net = &net;
4330 ad.u.net->sk = sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004332 return avc_has_perm(&selinux_state,
4333 current_sid(), sksec->sid, sksec->sclass, perms,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004334 &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335}
4336
4337static int selinux_socket_create(int family, int type,
4338 int protocol, int kern)
4339{
Paul Moore5fb49872010-04-22 14:46:19 -04004340 const struct task_security_struct *tsec = current_security();
Paul Moored4f2d972010-04-22 14:46:18 -04004341 u32 newsid;
David Howells275bb412008-11-14 10:39:19 +11004342 u16 secclass;
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004343 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344
4345 if (kern)
Paul Moored4f2d972010-04-22 14:46:18 -04004346 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347
David Howells275bb412008-11-14 10:39:19 +11004348 secclass = socket_type_to_security_class(family, type, protocol);
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004349 rc = socket_sockcreate_sid(tsec, secclass, &newsid);
4350 if (rc)
4351 return rc;
4352
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004353 return avc_has_perm(&selinux_state,
4354 tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355}
4356
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07004357static int selinux_socket_post_create(struct socket *sock, int family,
4358 int type, int protocol, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359{
Paul Moore5fb49872010-04-22 14:46:19 -04004360 const struct task_security_struct *tsec = current_security();
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05004361 struct inode_security_struct *isec = inode_security_novalidate(SOCK_INODE(sock));
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004362 struct sk_security_struct *sksec;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004363 u16 sclass = socket_type_to_security_class(family, type, protocol);
4364 u32 sid = SECINITSID_KERNEL;
David Howells275bb412008-11-14 10:39:19 +11004365 int err = 0;
4366
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004367 if (!kern) {
4368 err = socket_sockcreate_sid(tsec, sclass, &sid);
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004369 if (err)
4370 return err;
4371 }
David Howells275bb412008-11-14 10:39:19 +11004372
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004373 isec->sclass = sclass;
4374 isec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004375 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004377 if (sock->sk) {
4378 sksec = sock->sk->sk_security;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004379 sksec->sclass = sclass;
4380 sksec->sid = sid;
Richard Hainesd4529302018-02-13 20:57:18 +00004381 /* Allows detection of the first association on this socket */
4382 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4383 sksec->sctp_assoc_state = SCTP_ASSOC_UNSET;
4384
Paul Moore389fb8002009-03-27 17:10:34 -04004385 err = selinux_netlbl_socket_post_create(sock->sk, family);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004386 }
4387
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07004388 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389}
4390
David Herrmann0b811db2018-05-04 16:28:21 +02004391static int selinux_socket_socketpair(struct socket *socka,
4392 struct socket *sockb)
4393{
4394 struct sk_security_struct *sksec_a = socka->sk->sk_security;
4395 struct sk_security_struct *sksec_b = sockb->sk->sk_security;
4396
4397 sksec_a->peer_sid = sksec_b->sid;
4398 sksec_b->peer_sid = sksec_a->sid;
4399
4400 return 0;
4401}
4402
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403/* Range of port numbers used to automatically bind.
4404 Need to determine whether we should perform a name_bind
4405 permission check between the socket and the port number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406
4407static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
4408{
Paul Moore253bfae2010-04-22 14:46:19 -04004409 struct sock *sk = sock->sk;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004410 struct sk_security_struct *sksec = sk->sk_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 u16 family;
4412 int err;
4413
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004414 err = sock_has_perm(sk, SOCKET__BIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415 if (err)
4416 goto out;
4417
Richard Hainesd4529302018-02-13 20:57:18 +00004418 /* If PF_INET or PF_INET6, check name_bind permission for the port. */
Paul Moore253bfae2010-04-22 14:46:19 -04004419 family = sk->sk_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 if (family == PF_INET || family == PF_INET6) {
4421 char *addrp;
Thomas Liu2bf49692009-07-14 12:14:09 -04004422 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004423 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424 struct sockaddr_in *addr4 = NULL;
4425 struct sockaddr_in6 *addr6 = NULL;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004426 u16 family_sa = address->sa_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 unsigned short snum;
James Morrise399f982008-06-12 01:39:58 +10004428 u32 sid, node_perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429
Richard Hainesd4529302018-02-13 20:57:18 +00004430 /*
4431 * sctp_bindx(3) calls via selinux_sctp_bind_connect()
4432 * that validates multiple binding addresses. Because of this
4433 * need to check address->sa_family as it is possible to have
4434 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4435 */
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004436 switch (family_sa) {
4437 case AF_UNSPEC:
Richard Haines68741a8a2018-03-02 19:54:34 +00004438 case AF_INET:
4439 if (addrlen < sizeof(struct sockaddr_in))
4440 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 addr4 = (struct sockaddr_in *)address;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004442 if (family_sa == AF_UNSPEC) {
4443 /* see __inet_bind(), we only want to allow
4444 * AF_UNSPEC if the address is INADDR_ANY
4445 */
4446 if (addr4->sin_addr.s_addr != htonl(INADDR_ANY))
4447 goto err_af;
4448 family_sa = AF_INET;
4449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 snum = ntohs(addr4->sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 addrp = (char *)&addr4->sin_addr.s_addr;
Richard Haines68741a8a2018-03-02 19:54:34 +00004452 break;
4453 case AF_INET6:
4454 if (addrlen < SIN6_LEN_RFC2133)
4455 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456 addr6 = (struct sockaddr_in6 *)address;
4457 snum = ntohs(addr6->sin6_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 addrp = (char *)&addr6->sin6_addr.s6_addr;
Richard Haines68741a8a2018-03-02 19:54:34 +00004459 break;
4460 default:
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004461 goto err_af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462 }
4463
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004464 ad.type = LSM_AUDIT_DATA_NET;
4465 ad.u.net = &net;
4466 ad.u.net->sport = htons(snum);
4467 ad.u.net->family = family_sa;
4468
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004469 if (snum) {
4470 int low, high;
4471
Eric W. Biederman0bbf87d2013-09-28 14:10:59 -07004472 inet_get_local_port_range(sock_net(sk), &low, &high);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004473
Krister Johansen4548b682017-01-20 17:49:11 -08004474 if (snum < max(inet_prot_sock(sock_net(sk)), low) ||
4475 snum > high) {
Paul Moore3e1121722008-04-10 10:48:14 -04004476 err = sel_netport_sid(sk->sk_protocol,
4477 snum, &sid);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004478 if (err)
4479 goto out;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004480 err = avc_has_perm(&selinux_state,
4481 sksec->sid, sid,
Paul Moore253bfae2010-04-22 14:46:19 -04004482 sksec->sclass,
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004483 SOCKET__NAME_BIND, &ad);
4484 if (err)
4485 goto out;
4486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 }
Eric Paris828dfe12008-04-17 13:17:49 -04004488
Paul Moore253bfae2010-04-22 14:46:19 -04004489 switch (sksec->sclass) {
James Morris13402582005-09-30 14:24:34 -04004490 case SECCLASS_TCP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 node_perm = TCP_SOCKET__NODE_BIND;
4492 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004493
James Morris13402582005-09-30 14:24:34 -04004494 case SECCLASS_UDP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495 node_perm = UDP_SOCKET__NODE_BIND;
4496 break;
James Morris2ee92d42006-11-13 16:09:01 -08004497
4498 case SECCLASS_DCCP_SOCKET:
4499 node_perm = DCCP_SOCKET__NODE_BIND;
4500 break;
4501
Richard Hainesd4529302018-02-13 20:57:18 +00004502 case SECCLASS_SCTP_SOCKET:
4503 node_perm = SCTP_SOCKET__NODE_BIND;
4504 break;
4505
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 default:
4507 node_perm = RAWIP_SOCKET__NODE_BIND;
4508 break;
4509 }
Eric Paris828dfe12008-04-17 13:17:49 -04004510
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004511 err = sel_netnode_sid(addrp, family_sa, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 if (err)
4513 goto out;
Eric Paris828dfe12008-04-17 13:17:49 -04004514
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004515 if (family_sa == AF_INET)
Eric Paris48c62af2012-04-02 13:15:44 -04004516 ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517 else
Eric Paris48c62af2012-04-02 13:15:44 -04004518 ad.u.net->v6info.saddr = addr6->sin6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004520 err = avc_has_perm(&selinux_state,
4521 sksec->sid, sid,
Paul Moore253bfae2010-04-22 14:46:19 -04004522 sksec->sclass, node_perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523 if (err)
4524 goto out;
4525 }
4526out:
4527 return err;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004528err_af:
4529 /* Note that SCTP services expect -EINVAL, others -EAFNOSUPPORT. */
4530 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4531 return -EINVAL;
4532 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533}
4534
Richard Hainesd4529302018-02-13 20:57:18 +00004535/* This supports connect(2) and SCTP connect services such as sctp_connectx(3)
Mauro Carvalho Chehab5fb94e92018-05-08 15:14:57 -03004536 * and sctp_sendmsg(3) as described in Documentation/security/LSM-sctp.rst
Richard Hainesd4529302018-02-13 20:57:18 +00004537 */
4538static int selinux_socket_connect_helper(struct socket *sock,
4539 struct sockaddr *address, int addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540{
Paul Moore014ab192008-10-10 10:16:33 -04004541 struct sock *sk = sock->sk;
Paul Moore253bfae2010-04-22 14:46:19 -04004542 struct sk_security_struct *sksec = sk->sk_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543 int err;
4544
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004545 err = sock_has_perm(sk, SOCKET__CONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546 if (err)
4547 return err;
4548
4549 /*
Richard Hainesd4529302018-02-13 20:57:18 +00004550 * If a TCP, DCCP or SCTP socket, check name_connect permission
4551 * for the port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552 */
Paul Moore253bfae2010-04-22 14:46:19 -04004553 if (sksec->sclass == SECCLASS_TCP_SOCKET ||
Richard Hainesd4529302018-02-13 20:57:18 +00004554 sksec->sclass == SECCLASS_DCCP_SOCKET ||
4555 sksec->sclass == SECCLASS_SCTP_SOCKET) {
Thomas Liu2bf49692009-07-14 12:14:09 -04004556 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004557 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 struct sockaddr_in *addr4 = NULL;
4559 struct sockaddr_in6 *addr6 = NULL;
4560 unsigned short snum;
James Morris2ee92d42006-11-13 16:09:01 -08004561 u32 sid, perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562
Richard Hainesd4529302018-02-13 20:57:18 +00004563 /* sctp_connectx(3) calls via selinux_sctp_bind_connect()
4564 * that validates multiple connect addresses. Because of this
4565 * need to check address->sa_family as it is possible to have
4566 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4567 */
Richard Haines68741a8a2018-03-02 19:54:34 +00004568 switch (address->sa_family) {
4569 case AF_INET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 addr4 = (struct sockaddr_in *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004571 if (addrlen < sizeof(struct sockaddr_in))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 return -EINVAL;
4573 snum = ntohs(addr4->sin_port);
Richard Haines68741a8a2018-03-02 19:54:34 +00004574 break;
4575 case AF_INET6:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 addr6 = (struct sockaddr_in6 *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004577 if (addrlen < SIN6_LEN_RFC2133)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 return -EINVAL;
4579 snum = ntohs(addr6->sin6_port);
Richard Haines68741a8a2018-03-02 19:54:34 +00004580 break;
4581 default:
4582 /* Note that SCTP services expect -EINVAL, whereas
4583 * others expect -EAFNOSUPPORT.
4584 */
4585 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4586 return -EINVAL;
4587 else
4588 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 }
4590
Paul Moore3e1121722008-04-10 10:48:14 -04004591 err = sel_netport_sid(sk->sk_protocol, snum, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 if (err)
Richard Hainesd4529302018-02-13 20:57:18 +00004593 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594
Richard Hainesd4529302018-02-13 20:57:18 +00004595 switch (sksec->sclass) {
4596 case SECCLASS_TCP_SOCKET:
4597 perm = TCP_SOCKET__NAME_CONNECT;
4598 break;
4599 case SECCLASS_DCCP_SOCKET:
4600 perm = DCCP_SOCKET__NAME_CONNECT;
4601 break;
4602 case SECCLASS_SCTP_SOCKET:
4603 perm = SCTP_SOCKET__NAME_CONNECT;
4604 break;
4605 }
James Morris2ee92d42006-11-13 16:09:01 -08004606
Eric Paris50c205f2012-04-04 15:01:43 -04004607 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004608 ad.u.net = &net;
4609 ad.u.net->dport = htons(snum);
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004610 ad.u.net->family = address->sa_family;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004611 err = avc_has_perm(&selinux_state,
4612 sksec->sid, sid, sksec->sclass, perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 if (err)
Richard Hainesd4529302018-02-13 20:57:18 +00004614 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 }
4616
Richard Hainesd4529302018-02-13 20:57:18 +00004617 return 0;
4618}
Paul Moore014ab192008-10-10 10:16:33 -04004619
Richard Hainesd4529302018-02-13 20:57:18 +00004620/* Supports connect(2), see comments in selinux_socket_connect_helper() */
4621static int selinux_socket_connect(struct socket *sock,
4622 struct sockaddr *address, int addrlen)
4623{
4624 int err;
4625 struct sock *sk = sock->sk;
4626
4627 err = selinux_socket_connect_helper(sock, address, addrlen);
4628 if (err)
4629 return err;
4630
4631 return selinux_netlbl_socket_connect(sk, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632}
4633
4634static int selinux_socket_listen(struct socket *sock, int backlog)
4635{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004636 return sock_has_perm(sock->sk, SOCKET__LISTEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637}
4638
4639static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
4640{
4641 int err;
4642 struct inode_security_struct *isec;
4643 struct inode_security_struct *newisec;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004644 u16 sclass;
4645 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004647 err = sock_has_perm(sock->sk, SOCKET__ACCEPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 if (err)
4649 return err;
4650
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05004651 isec = inode_security_novalidate(SOCK_INODE(sock));
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004652 spin_lock(&isec->lock);
4653 sclass = isec->sclass;
4654 sid = isec->sid;
4655 spin_unlock(&isec->lock);
4656
4657 newisec = inode_security_novalidate(SOCK_INODE(newsock));
4658 newisec->sclass = sclass;
4659 newisec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004660 newisec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661
4662 return 0;
4663}
4664
4665static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
Eric Paris828dfe12008-04-17 13:17:49 -04004666 int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004668 return sock_has_perm(sock->sk, SOCKET__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669}
4670
4671static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
4672 int size, int flags)
4673{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004674 return sock_has_perm(sock->sk, SOCKET__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675}
4676
4677static int selinux_socket_getsockname(struct socket *sock)
4678{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004679 return sock_has_perm(sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680}
4681
4682static int selinux_socket_getpeername(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
Eric Paris828dfe12008-04-17 13:17:49 -04004687static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688{
Paul Mooref8687af2006-10-30 15:22:15 -08004689 int err;
4690
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004691 err = sock_has_perm(sock->sk, SOCKET__SETOPT);
Paul Mooref8687af2006-10-30 15:22:15 -08004692 if (err)
4693 return err;
4694
4695 return selinux_netlbl_socket_setsockopt(sock, level, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696}
4697
4698static int selinux_socket_getsockopt(struct socket *sock, int level,
4699 int optname)
4700{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004701 return sock_has_perm(sock->sk, SOCKET__GETOPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702}
4703
4704static int selinux_socket_shutdown(struct socket *sock, int how)
4705{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004706 return sock_has_perm(sock->sk, SOCKET__SHUTDOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707}
4708
David S. Miller3610cda2011-01-05 15:38:53 -08004709static int selinux_socket_unix_stream_connect(struct sock *sock,
4710 struct sock *other,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711 struct sock *newsk)
4712{
David S. Miller3610cda2011-01-05 15:38:53 -08004713 struct sk_security_struct *sksec_sock = sock->sk_security;
4714 struct sk_security_struct *sksec_other = other->sk_security;
Paul Moore4d1e2452010-04-22 14:46:18 -04004715 struct sk_security_struct *sksec_new = newsk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004716 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004717 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718 int err;
4719
Eric Paris50c205f2012-04-04 15:01:43 -04004720 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004721 ad.u.net = &net;
4722 ad.u.net->sk = other;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004724 err = avc_has_perm(&selinux_state,
4725 sksec_sock->sid, sksec_other->sid,
Paul Moore4d1e2452010-04-22 14:46:18 -04004726 sksec_other->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727 UNIX_STREAM_SOCKET__CONNECTTO, &ad);
4728 if (err)
4729 return err;
4730
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731 /* server child socket */
Paul Moore4d1e2452010-04-22 14:46:18 -04004732 sksec_new->peer_sid = sksec_sock->sid;
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004733 err = security_sid_mls_copy(&selinux_state, sksec_other->sid,
4734 sksec_sock->sid, &sksec_new->sid);
Paul Moore4d1e2452010-04-22 14:46:18 -04004735 if (err)
4736 return err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004737
Paul Moore4d1e2452010-04-22 14:46:18 -04004738 /* connecting socket */
4739 sksec_sock->peer_sid = sksec_new->sid;
4740
4741 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742}
4743
4744static int selinux_socket_unix_may_send(struct socket *sock,
4745 struct socket *other)
4746{
Paul Moore253bfae2010-04-22 14:46:19 -04004747 struct sk_security_struct *ssec = sock->sk->sk_security;
4748 struct sk_security_struct *osec = other->sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004749 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004750 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751
Eric Paris50c205f2012-04-04 15:01:43 -04004752 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004753 ad.u.net = &net;
4754 ad.u.net->sk = other->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004756 return avc_has_perm(&selinux_state,
4757 ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
Paul Moore253bfae2010-04-22 14:46:19 -04004758 &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759}
4760
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004761static int selinux_inet_sys_rcv_skb(struct net *ns, int ifindex,
4762 char *addrp, u16 family, u32 peer_sid,
Thomas Liu2bf49692009-07-14 12:14:09 -04004763 struct common_audit_data *ad)
Paul Mooreeffad8d2008-01-29 08:49:27 -05004764{
4765 int err;
4766 u32 if_sid;
4767 u32 node_sid;
4768
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004769 err = sel_netif_sid(ns, ifindex, &if_sid);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004770 if (err)
4771 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004772 err = avc_has_perm(&selinux_state,
4773 peer_sid, if_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004774 SECCLASS_NETIF, NETIF__INGRESS, ad);
4775 if (err)
4776 return err;
4777
4778 err = sel_netnode_sid(addrp, family, &node_sid);
4779 if (err)
4780 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004781 return avc_has_perm(&selinux_state,
4782 peer_sid, node_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004783 SECCLASS_NODE, NODE__RECVFROM, ad);
4784}
4785
Paul Moore220deb92008-01-29 08:38:23 -05004786static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
Paul Moored8395c82008-10-10 10:16:30 -04004787 u16 family)
Paul Moore220deb92008-01-29 08:38:23 -05004788{
Paul Moore277d3422008-12-31 12:54:11 -05004789 int err = 0;
Paul Moore220deb92008-01-29 08:38:23 -05004790 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004791 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004792 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004793 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04004794 char *addrp;
4795
Eric Paris50c205f2012-04-04 15:01:43 -04004796 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004797 ad.u.net = &net;
4798 ad.u.net->netif = skb->skb_iif;
4799 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004800 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4801 if (err)
4802 return err;
Paul Moore220deb92008-01-29 08:38:23 -05004803
Paul Moore58bfbb52009-03-27 17:10:41 -04004804 if (selinux_secmark_enabled()) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004805 err = avc_has_perm(&selinux_state,
4806 sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Moored8395c82008-10-10 10:16:30 -04004807 PACKET__RECV, &ad);
Paul Moore58bfbb52009-03-27 17:10:41 -04004808 if (err)
4809 return err;
4810 }
Paul Moore220deb92008-01-29 08:38:23 -05004811
Steffen Klassertb9679a72011-02-23 12:55:21 +01004812 err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
4813 if (err)
4814 return err;
4815 err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004816
James Morris4e5ab4c2006-06-09 00:33:33 -07004817 return err;
4818}
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004819
James Morris4e5ab4c2006-06-09 00:33:33 -07004820static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
4821{
Paul Moore220deb92008-01-29 08:38:23 -05004822 int err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004823 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004824 u16 family = sk->sk_family;
4825 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004826 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004827 struct lsm_network_audit net = {0,};
Paul Moore220deb92008-01-29 08:38:23 -05004828 char *addrp;
Paul Moored8395c82008-10-10 10:16:30 -04004829 u8 secmark_active;
4830 u8 peerlbl_active;
James Morris4e5ab4c2006-06-09 00:33:33 -07004831
James Morris4e5ab4c2006-06-09 00:33:33 -07004832 if (family != PF_INET && family != PF_INET6)
Paul Moore220deb92008-01-29 08:38:23 -05004833 return 0;
James Morris4e5ab4c2006-06-09 00:33:33 -07004834
4835 /* Handle mapped IPv4 packets arriving via IPv6 sockets */
Al Viro87fcd702006-12-04 22:00:55 +00004836 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
James Morris4e5ab4c2006-06-09 00:33:33 -07004837 family = PF_INET;
4838
Paul Moored8395c82008-10-10 10:16:30 -04004839 /* If any sort of compatibility mode is enabled then handoff processing
4840 * to the selinux_sock_rcv_skb_compat() function to deal with the
4841 * special handling. We do this in an attempt to keep this function
4842 * as fast and as clean as possible. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004843 if (!selinux_policycap_netpeer())
Paul Moored8395c82008-10-10 10:16:30 -04004844 return selinux_sock_rcv_skb_compat(sk, skb, family);
4845
4846 secmark_active = selinux_secmark_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04004847 peerlbl_active = selinux_peerlbl_enabled();
Paul Moored8395c82008-10-10 10:16:30 -04004848 if (!secmark_active && !peerlbl_active)
4849 return 0;
4850
Eric Paris50c205f2012-04-04 15:01:43 -04004851 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004852 ad.u.net = &net;
4853 ad.u.net->netif = skb->skb_iif;
4854 ad.u.net->family = family;
Paul Moore224dfbd2008-01-29 08:38:13 -05004855 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
James Morris4e5ab4c2006-06-09 00:33:33 -07004856 if (err)
Paul Moore220deb92008-01-29 08:38:23 -05004857 return err;
James Morris4e5ab4c2006-06-09 00:33:33 -07004858
Paul Moored8395c82008-10-10 10:16:30 -04004859 if (peerlbl_active) {
Paul Moored621d352008-01-29 08:43:36 -05004860 u32 peer_sid;
4861
4862 err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
4863 if (err)
4864 return err;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004865 err = selinux_inet_sys_rcv_skb(sock_net(sk), skb->skb_iif,
4866 addrp, family, peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04004867 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04004868 selinux_netlbl_err(skb, family, err, 0);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004869 return err;
Paul Mooredfaebe92008-10-10 10:16:31 -04004870 }
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004871 err = avc_has_perm(&selinux_state,
4872 sk_sid, peer_sid, SECCLASS_PEER,
Paul Moored621d352008-01-29 08:43:36 -05004873 PEER__RECV, &ad);
Chad Hanson46d01d62013-12-23 17:45:01 -05004874 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04004875 selinux_netlbl_err(skb, family, err, 0);
Chad Hanson46d01d62013-12-23 17:45:01 -05004876 return err;
4877 }
Paul Moored621d352008-01-29 08:43:36 -05004878 }
4879
Paul Moored8395c82008-10-10 10:16:30 -04004880 if (secmark_active) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004881 err = avc_has_perm(&selinux_state,
4882 sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004883 PACKET__RECV, &ad);
4884 if (err)
4885 return err;
4886 }
4887
Paul Moored621d352008-01-29 08:43:36 -05004888 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889}
4890
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004891static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
4892 int __user *optlen, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893{
4894 int err = 0;
4895 char *scontext;
4896 u32 scontext_len;
Paul Moore253bfae2010-04-22 14:46:19 -04004897 struct sk_security_struct *sksec = sock->sk->sk_security;
Paul Moore3de4bab2006-11-17 17:38:54 -05004898 u32 peer_sid = SECSID_NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899
Paul Moore253bfae2010-04-22 14:46:19 -04004900 if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
Richard Hainesd4529302018-02-13 20:57:18 +00004901 sksec->sclass == SECCLASS_TCP_SOCKET ||
4902 sksec->sclass == SECCLASS_SCTP_SOCKET)
Eric Parisdd3e7832010-04-07 15:08:46 -04004903 peer_sid = sksec->peer_sid;
Paul Moore253bfae2010-04-22 14:46:19 -04004904 if (peer_sid == SECSID_NULL)
4905 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004907 err = security_sid_to_context(&selinux_state, peer_sid, &scontext,
4908 &scontext_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 if (err)
Paul Moore253bfae2010-04-22 14:46:19 -04004910 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911
4912 if (scontext_len > len) {
4913 err = -ERANGE;
4914 goto out_len;
4915 }
4916
4917 if (copy_to_user(optval, scontext, scontext_len))
4918 err = -EFAULT;
4919
4920out_len:
4921 if (put_user(scontext_len, optlen))
4922 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 kfree(scontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 return err;
4925}
4926
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004927static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004928{
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004929 u32 peer_secid = SECSID_NULL;
Paul Moore75e22912008-01-29 08:38:04 -05004930 u16 family;
Paul Moore899134f2016-03-28 15:19:10 -04004931 struct inode_security_struct *isec;
Catherine Zhang877ce7c2006-06-29 12:27:47 -07004932
Paul Mooreaa862902008-10-10 10:16:29 -04004933 if (skb && skb->protocol == htons(ETH_P_IP))
4934 family = PF_INET;
4935 else if (skb && skb->protocol == htons(ETH_P_IPV6))
4936 family = PF_INET6;
4937 else if (sock)
Paul Moore75e22912008-01-29 08:38:04 -05004938 family = sock->sk->sk_family;
Paul Moore75e22912008-01-29 08:38:04 -05004939 else
4940 goto out;
4941
Paul Moore899134f2016-03-28 15:19:10 -04004942 if (sock && family == PF_UNIX) {
4943 isec = inode_security_novalidate(SOCK_INODE(sock));
4944 peer_secid = isec->sid;
4945 } else if (skb)
Paul Moore220deb92008-01-29 08:38:23 -05004946 selinux_skb_peerlbl_sid(skb, family, &peer_secid);
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004947
Paul Moore75e22912008-01-29 08:38:04 -05004948out:
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004949 *secid = peer_secid;
Paul Moore75e22912008-01-29 08:38:04 -05004950 if (peer_secid == SECSID_NULL)
4951 return -EINVAL;
4952 return 0;
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004953}
4954
Al Viro7d877f32005-10-21 03:20:43 -04004955static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956{
Paul Moore84914b72010-04-22 14:46:18 -04004957 struct sk_security_struct *sksec;
4958
4959 sksec = kzalloc(sizeof(*sksec), priority);
4960 if (!sksec)
4961 return -ENOMEM;
4962
4963 sksec->peer_sid = SECINITSID_UNLABELED;
4964 sksec->sid = SECINITSID_UNLABELED;
Stephen Smalley5dee25d2015-07-10 17:19:57 -04004965 sksec->sclass = SECCLASS_SOCKET;
Paul Moore84914b72010-04-22 14:46:18 -04004966 selinux_netlbl_sk_security_reset(sksec);
4967 sk->sk_security = sksec;
4968
4969 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970}
4971
4972static void selinux_sk_free_security(struct sock *sk)
4973{
Paul Moore84914b72010-04-22 14:46:18 -04004974 struct sk_security_struct *sksec = sk->sk_security;
4975
4976 sk->sk_security = NULL;
4977 selinux_netlbl_sk_security_free(sksec);
4978 kfree(sksec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979}
4980
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004981static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
4982{
Eric Parisdd3e7832010-04-07 15:08:46 -04004983 struct sk_security_struct *sksec = sk->sk_security;
4984 struct sk_security_struct *newsksec = newsk->sk_security;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004985
Eric Parisdd3e7832010-04-07 15:08:46 -04004986 newsksec->sid = sksec->sid;
4987 newsksec->peer_sid = sksec->peer_sid;
4988 newsksec->sclass = sksec->sclass;
Paul Moore99f59ed2006-08-29 17:53:48 -07004989
Eric Parisdd3e7832010-04-07 15:08:46 -04004990 selinux_netlbl_sk_security_reset(newsksec);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004991}
4992
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07004993static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004994{
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004995 if (!sk)
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07004996 *secid = SECINITSID_ANY_SOCKET;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004997 else {
4998 struct sk_security_struct *sksec = sk->sk_security;
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004999
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07005000 *secid = sksec->sid;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005001 }
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005002}
5003
Eric Paris828dfe12008-04-17 13:17:49 -04005004static void selinux_sock_graft(struct sock *sk, struct socket *parent)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005005{
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05005006 struct inode_security_struct *isec =
5007 inode_security_novalidate(SOCK_INODE(parent));
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005008 struct sk_security_struct *sksec = sk->sk_security;
5009
Paul Moore2873ead2014-07-28 10:42:48 -04005010 if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
5011 sk->sk_family == PF_UNIX)
David Woodhouse2148ccc2006-09-29 15:50:25 -07005012 isec->sid = sksec->sid;
Paul Moore220deb92008-01-29 08:38:23 -05005013 sksec->sclass = isec->sclass;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005014}
5015
Richard Hainesd4529302018-02-13 20:57:18 +00005016/* Called whenever SCTP receives an INIT chunk. This happens when an incoming
5017 * connect(2), sctp_connectx(3) or sctp_sendmsg(3) (with no association
5018 * already present).
5019 */
5020static int selinux_sctp_assoc_request(struct sctp_endpoint *ep,
5021 struct sk_buff *skb)
5022{
5023 struct sk_security_struct *sksec = ep->base.sk->sk_security;
5024 struct common_audit_data ad;
5025 struct lsm_network_audit net = {0,};
5026 u8 peerlbl_active;
5027 u32 peer_sid = SECINITSID_UNLABELED;
5028 u32 conn_sid;
5029 int err = 0;
5030
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005031 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005032 return 0;
5033
5034 peerlbl_active = selinux_peerlbl_enabled();
5035
5036 if (peerlbl_active) {
5037 /* This will return peer_sid = SECSID_NULL if there are
5038 * no peer labels, see security_net_peersid_resolve().
5039 */
5040 err = selinux_skb_peerlbl_sid(skb, ep->base.sk->sk_family,
5041 &peer_sid);
5042 if (err)
5043 return err;
5044
5045 if (peer_sid == SECSID_NULL)
5046 peer_sid = SECINITSID_UNLABELED;
5047 }
5048
5049 if (sksec->sctp_assoc_state == SCTP_ASSOC_UNSET) {
5050 sksec->sctp_assoc_state = SCTP_ASSOC_SET;
5051
5052 /* Here as first association on socket. As the peer SID
5053 * was allowed by peer recv (and the netif/node checks),
5054 * then it is approved by policy and used as the primary
5055 * peer SID for getpeercon(3).
5056 */
5057 sksec->peer_sid = peer_sid;
5058 } else if (sksec->peer_sid != peer_sid) {
5059 /* Other association peer SIDs are checked to enforce
5060 * consistency among the peer SIDs.
5061 */
5062 ad.type = LSM_AUDIT_DATA_NET;
5063 ad.u.net = &net;
5064 ad.u.net->sk = ep->base.sk;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005065 err = avc_has_perm(&selinux_state,
5066 sksec->peer_sid, peer_sid, sksec->sclass,
Richard Hainesd4529302018-02-13 20:57:18 +00005067 SCTP_SOCKET__ASSOCIATION, &ad);
5068 if (err)
5069 return err;
5070 }
5071
5072 /* Compute the MLS component for the connection and store
5073 * the information in ep. This will be used by SCTP TCP type
5074 * sockets and peeled off connections as they cause a new
5075 * socket to be generated. selinux_sctp_sk_clone() will then
5076 * plug this into the new socket.
5077 */
5078 err = selinux_conn_sid(sksec->sid, peer_sid, &conn_sid);
5079 if (err)
5080 return err;
5081
5082 ep->secid = conn_sid;
5083 ep->peer_secid = peer_sid;
5084
5085 /* Set any NetLabel labels including CIPSO/CALIPSO options. */
5086 return selinux_netlbl_sctp_assoc_request(ep, skb);
5087}
5088
5089/* Check if sctp IPv4/IPv6 addresses are valid for binding or connecting
5090 * based on their @optname.
5091 */
5092static int selinux_sctp_bind_connect(struct sock *sk, int optname,
5093 struct sockaddr *address,
5094 int addrlen)
5095{
5096 int len, err = 0, walk_size = 0;
5097 void *addr_buf;
5098 struct sockaddr *addr;
5099 struct socket *sock;
5100
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005101 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005102 return 0;
5103
5104 /* Process one or more addresses that may be IPv4 or IPv6 */
5105 sock = sk->sk_socket;
5106 addr_buf = address;
5107
5108 while (walk_size < addrlen) {
Ondrej Mosnacekc1383252018-11-13 16:16:08 +01005109 if (walk_size + sizeof(sa_family_t) > addrlen)
5110 return -EINVAL;
5111
Richard Hainesd4529302018-02-13 20:57:18 +00005112 addr = addr_buf;
5113 switch (addr->sa_family) {
Alexey Kodanev4152dc92018-05-11 20:15:13 +03005114 case AF_UNSPEC:
Richard Hainesd4529302018-02-13 20:57:18 +00005115 case AF_INET:
5116 len = sizeof(struct sockaddr_in);
5117 break;
5118 case AF_INET6:
5119 len = sizeof(struct sockaddr_in6);
5120 break;
5121 default:
Alexey Kodanev4152dc92018-05-11 20:15:13 +03005122 return -EINVAL;
Richard Hainesd4529302018-02-13 20:57:18 +00005123 }
5124
5125 err = -EINVAL;
5126 switch (optname) {
5127 /* Bind checks */
5128 case SCTP_PRIMARY_ADDR:
5129 case SCTP_SET_PEER_PRIMARY_ADDR:
5130 case SCTP_SOCKOPT_BINDX_ADD:
5131 err = selinux_socket_bind(sock, addr, len);
5132 break;
5133 /* Connect checks */
5134 case SCTP_SOCKOPT_CONNECTX:
5135 case SCTP_PARAM_SET_PRIMARY:
5136 case SCTP_PARAM_ADD_IP:
5137 case SCTP_SENDMSG_CONNECT:
5138 err = selinux_socket_connect_helper(sock, addr, len);
5139 if (err)
5140 return err;
5141
5142 /* As selinux_sctp_bind_connect() is called by the
5143 * SCTP protocol layer, the socket is already locked,
5144 * therefore selinux_netlbl_socket_connect_locked() is
5145 * is called here. The situations handled are:
5146 * sctp_connectx(3), sctp_sendmsg(3), sendmsg(2),
5147 * whenever a new IP address is added or when a new
5148 * primary address is selected.
5149 * Note that an SCTP connect(2) call happens before
5150 * the SCTP protocol layer and is handled via
5151 * selinux_socket_connect().
5152 */
5153 err = selinux_netlbl_socket_connect_locked(sk, addr);
5154 break;
5155 }
5156
5157 if (err)
5158 return err;
5159
5160 addr_buf += len;
5161 walk_size += len;
5162 }
5163
5164 return 0;
5165}
5166
5167/* Called whenever a new socket is created by accept(2) or sctp_peeloff(3). */
5168static void selinux_sctp_sk_clone(struct sctp_endpoint *ep, struct sock *sk,
5169 struct sock *newsk)
5170{
5171 struct sk_security_struct *sksec = sk->sk_security;
5172 struct sk_security_struct *newsksec = newsk->sk_security;
5173
5174 /* If policy does not support SECCLASS_SCTP_SOCKET then call
5175 * the non-sctp clone version.
5176 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005177 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005178 return selinux_sk_clone_security(sk, newsk);
5179
5180 newsksec->sid = ep->secid;
5181 newsksec->peer_sid = ep->peer_secid;
5182 newsksec->sclass = sksec->sclass;
5183 selinux_netlbl_sctp_sk_clone(sk, newsk);
5184}
5185
Adrian Bunk9a673e52006-08-15 00:03:53 -07005186static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
5187 struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005188{
5189 struct sk_security_struct *sksec = sk->sk_security;
5190 int err;
Paul Moore0b1f24e2013-12-03 11:39:13 -05005191 u16 family = req->rsk_ops->family;
Paul Moore446b8022013-12-04 16:10:51 -05005192 u32 connsid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005193 u32 peersid;
5194
Paul Mooreaa862902008-10-10 10:16:29 -04005195 err = selinux_skb_peerlbl_sid(skb, family, &peersid);
Paul Moore220deb92008-01-29 08:38:23 -05005196 if (err)
5197 return err;
Paul Moore446b8022013-12-04 16:10:51 -05005198 err = selinux_conn_sid(sksec->sid, peersid, &connsid);
5199 if (err)
5200 return err;
5201 req->secid = connsid;
5202 req->peer_secid = peersid;
Venkat Yekkiralaa51c64f2006-07-27 22:01:34 -07005203
Paul Moore389fb8002009-03-27 17:10:34 -04005204 return selinux_netlbl_inet_conn_request(req, family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005205}
5206
Adrian Bunk9a673e52006-08-15 00:03:53 -07005207static void selinux_inet_csk_clone(struct sock *newsk,
5208 const struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005209{
5210 struct sk_security_struct *newsksec = newsk->sk_security;
5211
5212 newsksec->sid = req->secid;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005213 newsksec->peer_sid = req->peer_secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005214 /* NOTE: Ideally, we should also get the isec->sid for the
5215 new socket in sync, but we don't have the isec available yet.
5216 So we will wait until sock_graft to do it, by which
5217 time it will have been created and available. */
Paul Moore99f59ed2006-08-29 17:53:48 -07005218
Paul Moore9f2ad662006-11-17 17:38:53 -05005219 /* We don't need to take any sort of lock here as we are the only
5220 * thread with access to newsksec */
Paul Moore389fb8002009-03-27 17:10:34 -04005221 selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005222}
5223
Paul Moore014ab192008-10-10 10:16:33 -04005224static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005225{
Paul Mooreaa862902008-10-10 10:16:29 -04005226 u16 family = sk->sk_family;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005227 struct sk_security_struct *sksec = sk->sk_security;
5228
Paul Mooreaa862902008-10-10 10:16:29 -04005229 /* handle mapped IPv4 packets arriving via IPv6 sockets */
5230 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
5231 family = PF_INET;
5232
5233 selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005234}
5235
Eric Paris2606fd12010-10-13 16:24:41 -04005236static int selinux_secmark_relabel_packet(u32 sid)
5237{
5238 const struct task_security_struct *__tsec;
5239 u32 tsid;
5240
5241 __tsec = current_security();
5242 tsid = __tsec->sid;
5243
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005244 return avc_has_perm(&selinux_state,
5245 tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO,
5246 NULL);
Eric Paris2606fd12010-10-13 16:24:41 -04005247}
5248
5249static void selinux_secmark_refcount_inc(void)
5250{
5251 atomic_inc(&selinux_secmark_refcount);
5252}
5253
5254static void selinux_secmark_refcount_dec(void)
5255{
5256 atomic_dec(&selinux_secmark_refcount);
5257}
5258
Adrian Bunk9a673e52006-08-15 00:03:53 -07005259static void selinux_req_classify_flow(const struct request_sock *req,
5260 struct flowi *fl)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005261{
David S. Miller1d28f422011-03-12 00:29:39 -05005262 fl->flowi_secid = req->secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005263}
5264
Paul Moore5dbbaf22013-01-14 07:12:19 +00005265static int selinux_tun_dev_alloc_security(void **security)
5266{
5267 struct tun_security_struct *tunsec;
5268
5269 tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL);
5270 if (!tunsec)
5271 return -ENOMEM;
5272 tunsec->sid = current_sid();
5273
5274 *security = tunsec;
5275 return 0;
5276}
5277
5278static void selinux_tun_dev_free_security(void *security)
5279{
5280 kfree(security);
5281}
5282
Paul Mooreed6d76e2009-08-28 18:12:49 -04005283static int selinux_tun_dev_create(void)
5284{
5285 u32 sid = current_sid();
5286
5287 /* we aren't taking into account the "sockcreate" SID since the socket
5288 * that is being created here is not a socket in the traditional sense,
5289 * instead it is a private sock, accessible only to the kernel, and
5290 * representing a wide range of network traffic spanning multiple
5291 * connections unlike traditional sockets - check the TUN driver to
5292 * get a better understanding of why this socket is special */
5293
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005294 return avc_has_perm(&selinux_state,
5295 sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005296 NULL);
5297}
5298
Paul Moore5dbbaf22013-01-14 07:12:19 +00005299static int selinux_tun_dev_attach_queue(void *security)
Paul Mooreed6d76e2009-08-28 18:12:49 -04005300{
Paul Moore5dbbaf22013-01-14 07:12:19 +00005301 struct tun_security_struct *tunsec = security;
5302
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005303 return avc_has_perm(&selinux_state,
5304 current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET,
Paul Moore5dbbaf22013-01-14 07:12:19 +00005305 TUN_SOCKET__ATTACH_QUEUE, NULL);
5306}
5307
5308static int selinux_tun_dev_attach(struct sock *sk, void *security)
5309{
5310 struct tun_security_struct *tunsec = security;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005311 struct sk_security_struct *sksec = sk->sk_security;
5312
5313 /* we don't currently perform any NetLabel based labeling here and it
5314 * isn't clear that we would want to do so anyway; while we could apply
5315 * labeling without the support of the TUN user the resulting labeled
5316 * traffic from the other end of the connection would almost certainly
5317 * cause confusion to the TUN user that had no idea network labeling
5318 * protocols were being used */
5319
Paul Moore5dbbaf22013-01-14 07:12:19 +00005320 sksec->sid = tunsec->sid;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005321 sksec->sclass = SECCLASS_TUN_SOCKET;
Paul Moore5dbbaf22013-01-14 07:12:19 +00005322
5323 return 0;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005324}
5325
Paul Moore5dbbaf22013-01-14 07:12:19 +00005326static int selinux_tun_dev_open(void *security)
Paul Mooreed6d76e2009-08-28 18:12:49 -04005327{
Paul Moore5dbbaf22013-01-14 07:12:19 +00005328 struct tun_security_struct *tunsec = security;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005329 u32 sid = current_sid();
5330 int err;
5331
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005332 err = avc_has_perm(&selinux_state,
5333 sid, tunsec->sid, SECCLASS_TUN_SOCKET,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005334 TUN_SOCKET__RELABELFROM, NULL);
5335 if (err)
5336 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005337 err = avc_has_perm(&selinux_state,
5338 sid, sid, SECCLASS_TUN_SOCKET,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005339 TUN_SOCKET__RELABELTO, NULL);
5340 if (err)
5341 return err;
Paul Moore5dbbaf22013-01-14 07:12:19 +00005342 tunsec->sid = sid;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005343
5344 return 0;
5345}
5346
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
5348{
5349 int err = 0;
5350 u32 perm;
5351 struct nlmsghdr *nlh;
Paul Moore253bfae2010-04-22 14:46:19 -04005352 struct sk_security_struct *sksec = sk->sk_security;
Eric Paris828dfe12008-04-17 13:17:49 -04005353
Hong zhi guo77954982013-03-27 06:49:35 +00005354 if (skb->len < NLMSG_HDRLEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355 err = -EINVAL;
5356 goto out;
5357 }
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -07005358 nlh = nlmsg_hdr(skb);
Eric Paris828dfe12008-04-17 13:17:49 -04005359
Paul Moore253bfae2010-04-22 14:46:19 -04005360 err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361 if (err) {
5362 if (err == -EINVAL) {
Vladis Dronov76319942015-12-24 11:09:41 -05005363 pr_warn_ratelimited("SELinux: unrecognized netlink"
5364 " message: protocol=%hu nlmsg_type=%hu sclass=%s"
5365 " pig=%d comm=%s\n",
Marek Milkoviccded3ff2015-06-04 16:22:16 -04005366 sk->sk_protocol, nlh->nlmsg_type,
Vladis Dronov76319942015-12-24 11:09:41 -05005367 secclass_map[sksec->sclass - 1].name,
5368 task_pid_nr(current), current->comm);
Paul Mooree5a5ca92018-03-01 17:38:30 -05005369 if (!enforcing_enabled(&selinux_state) ||
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005370 security_get_allow_unknown(&selinux_state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371 err = 0;
5372 }
5373
5374 /* Ignore */
5375 if (err == -ENOENT)
5376 err = 0;
5377 goto out;
5378 }
5379
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005380 err = sock_has_perm(sk, perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381out:
5382 return err;
5383}
5384
5385#ifdef CONFIG_NETFILTER
5386
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005387static unsigned int selinux_ip_forward(struct sk_buff *skb,
5388 const struct net_device *indev,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005389 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390{
Paul Mooredfaebe92008-10-10 10:16:31 -04005391 int err;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005392 char *addrp;
5393 u32 peer_sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04005394 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005395 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05005396 u8 secmark_active;
Paul Moore948bf852008-10-10 10:16:32 -04005397 u8 netlbl_active;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005398 u8 peerlbl_active;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005399
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005400 if (!selinux_policycap_netpeer())
Paul Mooreeffad8d2008-01-29 08:49:27 -05005401 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005402
Paul Mooreeffad8d2008-01-29 08:49:27 -05005403 secmark_active = selinux_secmark_enabled();
Paul Moore948bf852008-10-10 10:16:32 -04005404 netlbl_active = netlbl_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04005405 peerlbl_active = selinux_peerlbl_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05005406 if (!secmark_active && !peerlbl_active)
5407 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005408
Paul Moored8395c82008-10-10 10:16:30 -04005409 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
5410 return NF_DROP;
5411
Eric Paris50c205f2012-04-04 15:01:43 -04005412 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005413 ad.u.net = &net;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005414 ad.u.net->netif = indev->ifindex;
Eric Paris48c62af2012-04-02 13:15:44 -04005415 ad.u.net->family = family;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005416 if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
5417 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418
Paul Mooredfaebe92008-10-10 10:16:31 -04005419 if (peerlbl_active) {
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005420 err = selinux_inet_sys_rcv_skb(dev_net(indev), indev->ifindex,
5421 addrp, family, peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04005422 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04005423 selinux_netlbl_err(skb, family, err, 1);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005424 return NF_DROP;
Paul Mooredfaebe92008-10-10 10:16:31 -04005425 }
5426 }
Paul Mooreeffad8d2008-01-29 08:49:27 -05005427
5428 if (secmark_active)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005429 if (avc_has_perm(&selinux_state,
5430 peer_sid, skb->secmark,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005431 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
5432 return NF_DROP;
5433
Paul Moore948bf852008-10-10 10:16:32 -04005434 if (netlbl_active)
5435 /* we do this in the FORWARD path and not the POST_ROUTING
5436 * path because we want to make sure we apply the necessary
5437 * labeling before IPsec is applied so we can leverage AH
5438 * protection */
5439 if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
5440 return NF_DROP;
5441
Paul Mooreeffad8d2008-01-29 08:49:27 -05005442 return NF_ACCEPT;
5443}
5444
Eric W. Biederman06198b32015-09-18 14:33:06 -05005445static unsigned int selinux_ipv4_forward(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005446 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005447 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005448{
David S. Miller238e54c2015-04-03 20:32:56 -04005449 return selinux_ip_forward(skb, state->in, PF_INET);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005450}
5451
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005452#if IS_ENABLED(CONFIG_IPV6)
Eric W. Biederman06198b32015-09-18 14:33:06 -05005453static unsigned int selinux_ipv6_forward(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005454 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005455 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005456{
David S. Miller238e54c2015-04-03 20:32:56 -04005457 return selinux_ip_forward(skb, state->in, PF_INET6);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005458}
5459#endif /* IPV6 */
5460
Paul Moore948bf852008-10-10 10:16:32 -04005461static unsigned int selinux_ip_output(struct sk_buff *skb,
5462 u16 family)
5463{
Paul Moore47180062013-12-04 16:10:45 -05005464 struct sock *sk;
Paul Moore948bf852008-10-10 10:16:32 -04005465 u32 sid;
5466
5467 if (!netlbl_enabled())
5468 return NF_ACCEPT;
5469
5470 /* we do this in the LOCAL_OUT path and not the POST_ROUTING path
5471 * because we want to make sure we apply the necessary labeling
5472 * before IPsec is applied so we can leverage AH protection */
Paul Moore47180062013-12-04 16:10:45 -05005473 sk = skb->sk;
5474 if (sk) {
5475 struct sk_security_struct *sksec;
5476
Eric Dumazete446f9d2015-10-08 05:01:55 -07005477 if (sk_listener(sk))
Paul Moore47180062013-12-04 16:10:45 -05005478 /* if the socket is the listening state then this
5479 * packet is a SYN-ACK packet which means it needs to
5480 * be labeled based on the connection/request_sock and
5481 * not the parent socket. unfortunately, we can't
5482 * lookup the request_sock yet as it isn't queued on
5483 * the parent socket until after the SYN-ACK is sent.
5484 * the "solution" is to simply pass the packet as-is
5485 * as any IP option based labeling should be copied
5486 * from the initial connection request (in the IP
5487 * layer). it is far from ideal, but until we get a
5488 * security label in the packet itself this is the
5489 * best we can do. */
5490 return NF_ACCEPT;
5491
5492 /* standard practice, label using the parent socket */
5493 sksec = sk->sk_security;
Paul Moore948bf852008-10-10 10:16:32 -04005494 sid = sksec->sid;
5495 } else
5496 sid = SECINITSID_KERNEL;
5497 if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
5498 return NF_DROP;
5499
5500 return NF_ACCEPT;
5501}
5502
Eric W. Biederman06198b32015-09-18 14:33:06 -05005503static unsigned int selinux_ipv4_output(void *priv,
Paul Moore948bf852008-10-10 10:16:32 -04005504 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005505 const struct nf_hook_state *state)
Paul Moore948bf852008-10-10 10:16:32 -04005506{
5507 return selinux_ip_output(skb, PF_INET);
5508}
5509
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005510#if IS_ENABLED(CONFIG_IPV6)
Huw Davies2917f572016-06-27 15:06:15 -04005511static unsigned int selinux_ipv6_output(void *priv,
5512 struct sk_buff *skb,
5513 const struct nf_hook_state *state)
5514{
5515 return selinux_ip_output(skb, PF_INET6);
5516}
5517#endif /* IPV6 */
5518
Paul Mooreeffad8d2008-01-29 08:49:27 -05005519static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
5520 int ifindex,
Paul Moored8395c82008-10-10 10:16:30 -04005521 u16 family)
James Morris4e5ab4c2006-06-09 00:33:33 -07005522{
Eric Dumazet54abc682015-11-08 10:54:07 -08005523 struct sock *sk = skb_to_full_sk(skb);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005524 struct sk_security_struct *sksec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005525 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005526 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04005527 char *addrp;
5528 u8 proto;
James Morris4e5ab4c2006-06-09 00:33:33 -07005529
Paul Mooreeffad8d2008-01-29 08:49:27 -05005530 if (sk == NULL)
5531 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005532 sksec = sk->sk_security;
James Morris4e5ab4c2006-06-09 00:33:33 -07005533
Eric Paris50c205f2012-04-04 15:01:43 -04005534 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005535 ad.u.net = &net;
5536 ad.u.net->netif = ifindex;
5537 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04005538 if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
5539 return NF_DROP;
5540
Paul Moore58bfbb52009-03-27 17:10:41 -04005541 if (selinux_secmark_enabled())
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005542 if (avc_has_perm(&selinux_state,
5543 sksec->sid, skb->secmark,
Paul Moored8395c82008-10-10 10:16:30 -04005544 SECCLASS_PACKET, PACKET__SEND, &ad))
Eric Paris2fe66ec2010-11-23 06:28:08 +00005545 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07005546
Steffen Klassertb9679a72011-02-23 12:55:21 +01005547 if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
5548 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07005549
Paul Mooreeffad8d2008-01-29 08:49:27 -05005550 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551}
5552
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005553static unsigned int selinux_ip_postroute(struct sk_buff *skb,
5554 const struct net_device *outdev,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005555 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556{
Paul Mooreeffad8d2008-01-29 08:49:27 -05005557 u32 secmark_perm;
5558 u32 peer_sid;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005559 int ifindex = outdev->ifindex;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005560 struct sock *sk;
Thomas Liu2bf49692009-07-14 12:14:09 -04005561 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005562 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05005563 char *addrp;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005564 u8 secmark_active;
5565 u8 peerlbl_active;
5566
Paul Mooreeffad8d2008-01-29 08:49:27 -05005567 /* If any sort of compatibility mode is enabled then handoff processing
5568 * to the selinux_ip_postroute_compat() function to deal with the
5569 * special handling. We do this in an attempt to keep this function
5570 * as fast and as clean as possible. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005571 if (!selinux_policycap_netpeer())
Paul Moored8395c82008-10-10 10:16:30 -04005572 return selinux_ip_postroute_compat(skb, ifindex, family);
Paul Moorec0828e52013-12-10 14:58:01 -05005573
Paul Mooreeffad8d2008-01-29 08:49:27 -05005574 secmark_active = selinux_secmark_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04005575 peerlbl_active = selinux_peerlbl_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05005576 if (!secmark_active && !peerlbl_active)
5577 return NF_ACCEPT;
5578
Eric Dumazet54abc682015-11-08 10:54:07 -08005579 sk = skb_to_full_sk(skb);
Paul Moorec0828e52013-12-10 14:58:01 -05005580
Paul Mooreeffad8d2008-01-29 08:49:27 -05005581#ifdef CONFIG_XFRM
5582 /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
5583 * packet transformation so allow the packet to pass without any checks
5584 * since we'll have another chance to perform access control checks
5585 * when the packet is on it's final way out.
5586 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
Paul Moorec0828e52013-12-10 14:58:01 -05005587 * is NULL, in this case go ahead and apply access control.
5588 * NOTE: if this is a local socket (skb->sk != NULL) that is in the
5589 * TCP listening state we cannot wait until the XFRM processing
5590 * is done as we will miss out on the SA label if we do;
5591 * unfortunately, this means more work, but it is only once per
5592 * connection. */
5593 if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL &&
Eric Dumazete446f9d2015-10-08 05:01:55 -07005594 !(sk && sk_listener(sk)))
Paul Mooreeffad8d2008-01-29 08:49:27 -05005595 return NF_ACCEPT;
5596#endif
Paul Mooreeffad8d2008-01-29 08:49:27 -05005597
Paul Moored8395c82008-10-10 10:16:30 -04005598 if (sk == NULL) {
Paul Moore446b8022013-12-04 16:10:51 -05005599 /* Without an associated socket the packet is either coming
5600 * from the kernel or it is being forwarded; check the packet
5601 * to determine which and if the packet is being forwarded
5602 * query the packet directly to determine the security label. */
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005603 if (skb->skb_iif) {
5604 secmark_perm = PACKET__FORWARD_OUT;
Paul Moored8395c82008-10-10 10:16:30 -04005605 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005606 return NF_DROP;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005607 } else {
5608 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04005609 peer_sid = SECINITSID_KERNEL;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005610 }
Eric Dumazete446f9d2015-10-08 05:01:55 -07005611 } else if (sk_listener(sk)) {
Paul Moore446b8022013-12-04 16:10:51 -05005612 /* Locally generated packet but the associated socket is in the
5613 * listening state which means this is a SYN-ACK packet. In
5614 * this particular case the correct security label is assigned
5615 * to the connection/request_sock but unfortunately we can't
5616 * query the request_sock as it isn't queued on the parent
5617 * socket until after the SYN-ACK packet is sent; the only
5618 * viable choice is to regenerate the label like we do in
5619 * selinux_inet_conn_request(). See also selinux_ip_output()
5620 * for similar problems. */
5621 u32 skb_sid;
Eric Dumazete446f9d2015-10-08 05:01:55 -07005622 struct sk_security_struct *sksec;
5623
Eric Dumazete446f9d2015-10-08 05:01:55 -07005624 sksec = sk->sk_security;
Paul Moore446b8022013-12-04 16:10:51 -05005625 if (selinux_skb_peerlbl_sid(skb, family, &skb_sid))
5626 return NF_DROP;
Paul Moorec0828e52013-12-10 14:58:01 -05005627 /* At this point, if the returned skb peerlbl is SECSID_NULL
5628 * and the packet has been through at least one XFRM
5629 * transformation then we must be dealing with the "final"
5630 * form of labeled IPsec packet; since we've already applied
5631 * all of our access controls on this packet we can safely
5632 * pass the packet. */
5633 if (skb_sid == SECSID_NULL) {
5634 switch (family) {
5635 case PF_INET:
5636 if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
5637 return NF_ACCEPT;
5638 break;
5639 case PF_INET6:
5640 if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED)
5641 return NF_ACCEPT;
Paul Moorea7a91a12014-09-03 10:51:59 -04005642 break;
Paul Moorec0828e52013-12-10 14:58:01 -05005643 default:
5644 return NF_DROP_ERR(-ECONNREFUSED);
5645 }
5646 }
Paul Moore446b8022013-12-04 16:10:51 -05005647 if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid))
5648 return NF_DROP;
5649 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04005650 } else {
Paul Moore446b8022013-12-04 16:10:51 -05005651 /* Locally generated packet, fetch the security label from the
5652 * associated socket. */
Paul Mooreeffad8d2008-01-29 08:49:27 -05005653 struct sk_security_struct *sksec = sk->sk_security;
5654 peer_sid = sksec->sid;
5655 secmark_perm = PACKET__SEND;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005656 }
5657
Eric Paris50c205f2012-04-04 15:01:43 -04005658 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005659 ad.u.net = &net;
5660 ad.u.net->netif = ifindex;
5661 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04005662 if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
Eric Paris04f6d702010-11-23 06:28:02 +00005663 return NF_DROP;
Paul Moored8395c82008-10-10 10:16:30 -04005664
Paul Mooreeffad8d2008-01-29 08:49:27 -05005665 if (secmark_active)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005666 if (avc_has_perm(&selinux_state,
5667 peer_sid, skb->secmark,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005668 SECCLASS_PACKET, secmark_perm, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005669 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005670
5671 if (peerlbl_active) {
5672 u32 if_sid;
5673 u32 node_sid;
5674
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005675 if (sel_netif_sid(dev_net(outdev), ifindex, &if_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005676 return NF_DROP;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005677 if (avc_has_perm(&selinux_state,
5678 peer_sid, if_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005679 SECCLASS_NETIF, NETIF__EGRESS, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005680 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005681
5682 if (sel_netnode_sid(addrp, family, &node_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005683 return NF_DROP;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005684 if (avc_has_perm(&selinux_state,
5685 peer_sid, node_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005686 SECCLASS_NODE, NODE__SENDTO, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005687 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005688 }
5689
5690 return NF_ACCEPT;
5691}
5692
Eric W. Biederman06198b32015-09-18 14:33:06 -05005693static unsigned int selinux_ipv4_postroute(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005694 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005695 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005696{
David S. Miller238e54c2015-04-03 20:32:56 -04005697 return selinux_ip_postroute(skb, state->out, PF_INET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005698}
5699
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005700#if IS_ENABLED(CONFIG_IPV6)
Eric W. Biederman06198b32015-09-18 14:33:06 -05005701static unsigned int selinux_ipv6_postroute(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005702 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005703 const struct nf_hook_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704{
David S. Miller238e54c2015-04-03 20:32:56 -04005705 return selinux_ip_postroute(skb, state->out, PF_INET6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707#endif /* IPV6 */
5708
5709#endif /* CONFIG_NETFILTER */
5710
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
5712{
Stephen Smalley941fc5b2009-10-01 14:48:23 -04005713 return selinux_nlmsg_perm(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714}
5715
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005716static int ipc_alloc_security(struct kern_ipc_perm *perm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717 u16 sclass)
5718{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719 struct ipc_security_struct *isec;
5720
James Morris89d155e2005-10-30 14:59:21 -08005721 isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722 if (!isec)
5723 return -ENOMEM;
5724
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725 isec->sclass = sclass;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005726 isec->sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727 perm->security = isec;
5728
5729 return 0;
5730}
5731
5732static void ipc_free_security(struct kern_ipc_perm *perm)
5733{
5734 struct ipc_security_struct *isec = perm->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735 perm->security = NULL;
5736 kfree(isec);
5737}
5738
5739static int msg_msg_alloc_security(struct msg_msg *msg)
5740{
5741 struct msg_security_struct *msec;
5742
James Morris89d155e2005-10-30 14:59:21 -08005743 msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744 if (!msec)
5745 return -ENOMEM;
5746
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747 msec->sid = SECINITSID_UNLABELED;
5748 msg->security = msec;
5749
5750 return 0;
5751}
5752
5753static void msg_msg_free_security(struct msg_msg *msg)
5754{
5755 struct msg_security_struct *msec = msg->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756
5757 msg->security = NULL;
5758 kfree(msec);
5759}
5760
5761static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
Stephen Smalley6af963f2005-05-01 08:58:39 -07005762 u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005765 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005766 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768 isec = ipc_perms->security;
5769
Eric Paris50c205f2012-04-04 15:01:43 -04005770 ad.type = LSM_AUDIT_DATA_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771 ad.u.ipc_id = ipc_perms->key;
5772
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005773 return avc_has_perm(&selinux_state,
5774 sid, isec->sid, isec->sclass, perms, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775}
5776
5777static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
5778{
5779 return msg_msg_alloc_security(msg);
5780}
5781
5782static void selinux_msg_msg_free_security(struct msg_msg *msg)
5783{
5784 msg_msg_free_security(msg);
5785}
5786
5787/* message queue security operations */
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005788static int selinux_msg_queue_alloc_security(struct kern_ipc_perm *msq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005789{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005791 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005792 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793 int rc;
5794
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005795 rc = ipc_alloc_security(msq, SECCLASS_MSGQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796 if (rc)
5797 return rc;
5798
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005799 isec = msq->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800
Eric Paris50c205f2012-04-04 15:01:43 -04005801 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005802 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005804 rc = avc_has_perm(&selinux_state,
5805 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806 MSGQ__CREATE, &ad);
5807 if (rc) {
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005808 ipc_free_security(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809 return rc;
5810 }
5811 return 0;
5812}
5813
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005814static void selinux_msg_queue_free_security(struct kern_ipc_perm *msq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005815{
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005816 ipc_free_security(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817}
5818
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005819static int selinux_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005822 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005823 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005824
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005825 isec = msq->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826
Eric Paris50c205f2012-04-04 15:01:43 -04005827 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005828 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005829
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005830 return avc_has_perm(&selinux_state,
5831 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832 MSGQ__ASSOCIATE, &ad);
5833}
5834
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005835static int selinux_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836{
5837 int err;
5838 int perms;
5839
Eric Paris828dfe12008-04-17 13:17:49 -04005840 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841 case IPC_INFO:
5842 case MSG_INFO:
5843 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005844 return avc_has_perm(&selinux_state,
5845 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005846 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005847 case IPC_STAT:
5848 case MSG_STAT:
Davidlohr Bueso23c8cec2018-04-10 16:35:30 -07005849 case MSG_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005850 perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
5851 break;
5852 case IPC_SET:
5853 perms = MSGQ__SETATTR;
5854 break;
5855 case IPC_RMID:
5856 perms = MSGQ__DESTROY;
5857 break;
5858 default:
5859 return 0;
5860 }
5861
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005862 err = ipc_has_perm(msq, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863 return err;
5864}
5865
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005866static int selinux_msg_queue_msgsnd(struct kern_ipc_perm *msq, struct msg_msg *msg, int msqflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005868 struct ipc_security_struct *isec;
5869 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005870 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005871 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005872 int rc;
5873
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005874 isec = msq->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005875 msec = msg->security;
5876
5877 /*
5878 * First time through, need to assign label to the message
5879 */
5880 if (msec->sid == SECINITSID_UNLABELED) {
5881 /*
5882 * Compute new sid based on current process and
5883 * message queue this message will be stored in
5884 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005885 rc = security_transition_sid(&selinux_state, sid, isec->sid,
5886 SECCLASS_MSG, NULL, &msec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005887 if (rc)
5888 return rc;
5889 }
5890
Eric Paris50c205f2012-04-04 15:01:43 -04005891 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005892 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005893
5894 /* Can this process write to the queue? */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005895 rc = avc_has_perm(&selinux_state,
5896 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005897 MSGQ__WRITE, &ad);
5898 if (!rc)
5899 /* Can this process send the message */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005900 rc = avc_has_perm(&selinux_state,
5901 sid, msec->sid, SECCLASS_MSG,
David Howells275bb412008-11-14 10:39:19 +11005902 MSG__SEND, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903 if (!rc)
5904 /* Can the message be put in the queue? */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005905 rc = avc_has_perm(&selinux_state,
5906 msec->sid, isec->sid, SECCLASS_MSGQ,
David Howells275bb412008-11-14 10:39:19 +11005907 MSGQ__ENQUEUE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908
5909 return rc;
5910}
5911
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005912static int selinux_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005913 struct task_struct *target,
5914 long type, int mode)
5915{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005916 struct ipc_security_struct *isec;
5917 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005918 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005919 u32 sid = task_sid(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005920 int rc;
5921
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005922 isec = msq->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923 msec = msg->security;
5924
Eric Paris50c205f2012-04-04 15:01:43 -04005925 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005926 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005927
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005928 rc = avc_has_perm(&selinux_state,
5929 sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005930 SECCLASS_MSGQ, MSGQ__READ, &ad);
5931 if (!rc)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005932 rc = avc_has_perm(&selinux_state,
5933 sid, msec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934 SECCLASS_MSG, MSG__RECEIVE, &ad);
5935 return rc;
5936}
5937
5938/* Shared Memory security operations */
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005939static int selinux_shm_alloc_security(struct kern_ipc_perm *shp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005942 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005943 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944 int rc;
5945
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005946 rc = ipc_alloc_security(shp, SECCLASS_SHM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005947 if (rc)
5948 return rc;
5949
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005950 isec = shp->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951
Eric Paris50c205f2012-04-04 15:01:43 -04005952 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005953 ad.u.ipc_id = shp->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005955 rc = avc_has_perm(&selinux_state,
5956 sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957 SHM__CREATE, &ad);
5958 if (rc) {
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005959 ipc_free_security(shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005960 return rc;
5961 }
5962 return 0;
5963}
5964
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005965static void selinux_shm_free_security(struct kern_ipc_perm *shp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966{
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005967 ipc_free_security(shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005968}
5969
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005970static int selinux_shm_associate(struct kern_ipc_perm *shp, int shmflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005971{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005972 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005973 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005974 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005975
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005976 isec = shp->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977
Eric Paris50c205f2012-04-04 15:01:43 -04005978 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005979 ad.u.ipc_id = shp->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005980
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005981 return avc_has_perm(&selinux_state,
5982 sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005983 SHM__ASSOCIATE, &ad);
5984}
5985
5986/* Note, at this point, shp is locked down */
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005987static int selinux_shm_shmctl(struct kern_ipc_perm *shp, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005988{
5989 int perms;
5990 int err;
5991
Eric Paris828dfe12008-04-17 13:17:49 -04005992 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993 case IPC_INFO:
5994 case SHM_INFO:
5995 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005996 return avc_has_perm(&selinux_state,
5997 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005998 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999 case IPC_STAT:
6000 case SHM_STAT:
Davidlohr Buesoc21a6972018-04-10 16:35:23 -07006001 case SHM_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002 perms = SHM__GETATTR | SHM__ASSOCIATE;
6003 break;
6004 case IPC_SET:
6005 perms = SHM__SETATTR;
6006 break;
6007 case SHM_LOCK:
6008 case SHM_UNLOCK:
6009 perms = SHM__LOCK;
6010 break;
6011 case IPC_RMID:
6012 perms = SHM__DESTROY;
6013 break;
6014 default:
6015 return 0;
6016 }
6017
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006018 err = ipc_has_perm(shp, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019 return err;
6020}
6021
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006022static int selinux_shm_shmat(struct kern_ipc_perm *shp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006023 char __user *shmaddr, int shmflg)
6024{
6025 u32 perms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026
6027 if (shmflg & SHM_RDONLY)
6028 perms = SHM__READ;
6029 else
6030 perms = SHM__READ | SHM__WRITE;
6031
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006032 return ipc_has_perm(shp, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033}
6034
6035/* Semaphore security operations */
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006036static int selinux_sem_alloc_security(struct kern_ipc_perm *sma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006037{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006039 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006040 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006041 int rc;
6042
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006043 rc = ipc_alloc_security(sma, SECCLASS_SEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044 if (rc)
6045 return rc;
6046
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006047 isec = sma->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048
Eric Paris50c205f2012-04-04 15:01:43 -04006049 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006050 ad.u.ipc_id = sma->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006052 rc = avc_has_perm(&selinux_state,
6053 sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006054 SEM__CREATE, &ad);
6055 if (rc) {
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006056 ipc_free_security(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006057 return rc;
6058 }
6059 return 0;
6060}
6061
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006062static void selinux_sem_free_security(struct kern_ipc_perm *sma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063{
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006064 ipc_free_security(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006065}
6066
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006067static int selinux_sem_associate(struct kern_ipc_perm *sma, int semflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006070 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006071 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006073 isec = sma->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006074
Eric Paris50c205f2012-04-04 15:01:43 -04006075 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006076 ad.u.ipc_id = sma->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006077
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006078 return avc_has_perm(&selinux_state,
6079 sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080 SEM__ASSOCIATE, &ad);
6081}
6082
6083/* Note, at this point, sma is locked down */
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006084static int selinux_sem_semctl(struct kern_ipc_perm *sma, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085{
6086 int err;
6087 u32 perms;
6088
Eric Paris828dfe12008-04-17 13:17:49 -04006089 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006090 case IPC_INFO:
6091 case SEM_INFO:
6092 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006093 return avc_has_perm(&selinux_state,
6094 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006095 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006096 case GETPID:
6097 case GETNCNT:
6098 case GETZCNT:
6099 perms = SEM__GETATTR;
6100 break;
6101 case GETVAL:
6102 case GETALL:
6103 perms = SEM__READ;
6104 break;
6105 case SETVAL:
6106 case SETALL:
6107 perms = SEM__WRITE;
6108 break;
6109 case IPC_RMID:
6110 perms = SEM__DESTROY;
6111 break;
6112 case IPC_SET:
6113 perms = SEM__SETATTR;
6114 break;
6115 case IPC_STAT:
6116 case SEM_STAT:
Davidlohr Buesoa280d6d2018-04-10 16:35:26 -07006117 case SEM_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006118 perms = SEM__GETATTR | SEM__ASSOCIATE;
6119 break;
6120 default:
6121 return 0;
6122 }
6123
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006124 err = ipc_has_perm(sma, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006125 return err;
6126}
6127
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006128static int selinux_sem_semop(struct kern_ipc_perm *sma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006129 struct sembuf *sops, unsigned nsops, int alter)
6130{
6131 u32 perms;
6132
6133 if (alter)
6134 perms = SEM__READ | SEM__WRITE;
6135 else
6136 perms = SEM__READ;
6137
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006138 return ipc_has_perm(sma, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006139}
6140
6141static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
6142{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006143 u32 av = 0;
6144
Linus Torvalds1da177e2005-04-16 15:20:36 -07006145 av = 0;
6146 if (flag & S_IRUGO)
6147 av |= IPC__UNIX_READ;
6148 if (flag & S_IWUGO)
6149 av |= IPC__UNIX_WRITE;
6150
6151 if (av == 0)
6152 return 0;
6153
Stephen Smalley6af963f2005-05-01 08:58:39 -07006154 return ipc_has_perm(ipcp, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006155}
6156
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02006157static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
6158{
6159 struct ipc_security_struct *isec = ipcp->security;
6160 *secid = isec->sid;
6161}
6162
Eric Paris828dfe12008-04-17 13:17:49 -04006163static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006164{
6165 if (inode)
6166 inode_doinit_with_dentry(inode, dentry);
6167}
6168
6169static int selinux_getprocattr(struct task_struct *p,
Al Viro04ff9702007-03-12 16:17:58 +00006170 char *name, char **value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006171{
David Howells275bb412008-11-14 10:39:19 +11006172 const struct task_security_struct *__tsec;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00006173 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006174 int error;
Al Viro04ff9702007-03-12 16:17:58 +00006175 unsigned len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006176
David Howells275bb412008-11-14 10:39:19 +11006177 rcu_read_lock();
6178 __tsec = __task_cred(p)->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006179
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006180 if (current != p) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006181 error = avc_has_perm(&selinux_state,
6182 current_sid(), __tsec->sid,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006183 SECCLASS_PROCESS, PROCESS__GETATTR, NULL);
6184 if (error)
6185 goto bad;
6186 }
6187
Linus Torvalds1da177e2005-04-16 15:20:36 -07006188 if (!strcmp(name, "current"))
David Howells275bb412008-11-14 10:39:19 +11006189 sid = __tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006190 else if (!strcmp(name, "prev"))
David Howells275bb412008-11-14 10:39:19 +11006191 sid = __tsec->osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006192 else if (!strcmp(name, "exec"))
David Howells275bb412008-11-14 10:39:19 +11006193 sid = __tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006194 else if (!strcmp(name, "fscreate"))
David Howells275bb412008-11-14 10:39:19 +11006195 sid = __tsec->create_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006196 else if (!strcmp(name, "keycreate"))
David Howells275bb412008-11-14 10:39:19 +11006197 sid = __tsec->keycreate_sid;
Eric Paris42c3e032006-06-26 00:26:03 -07006198 else if (!strcmp(name, "sockcreate"))
David Howells275bb412008-11-14 10:39:19 +11006199 sid = __tsec->sockcreate_sid;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006200 else {
6201 error = -EINVAL;
6202 goto bad;
6203 }
David Howells275bb412008-11-14 10:39:19 +11006204 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205
6206 if (!sid)
6207 return 0;
6208
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006209 error = security_sid_to_context(&selinux_state, sid, value, &len);
Al Viro04ff9702007-03-12 16:17:58 +00006210 if (error)
6211 return error;
6212 return len;
David Howells275bb412008-11-14 10:39:19 +11006213
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006214bad:
David Howells275bb412008-11-14 10:39:19 +11006215 rcu_read_unlock();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006216 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217}
6218
Stephen Smalleyb21507e2017-01-09 10:07:31 -05006219static int selinux_setprocattr(const char *name, void *value, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220{
6221 struct task_security_struct *tsec;
David Howellsd84f4f92008-11-14 10:39:23 +11006222 struct cred *new;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006223 u32 mysid = current_sid(), sid = 0, ptsid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006224 int error;
6225 char *str = value;
6226
Linus Torvalds1da177e2005-04-16 15:20:36 -07006227 /*
6228 * Basic control over ability to set these attributes at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006229 */
6230 if (!strcmp(name, "exec"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006231 error = avc_has_perm(&selinux_state,
6232 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006233 PROCESS__SETEXEC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006234 else if (!strcmp(name, "fscreate"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006235 error = avc_has_perm(&selinux_state,
6236 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006237 PROCESS__SETFSCREATE, NULL);
Michael LeMay4eb582c2006-06-26 00:24:57 -07006238 else if (!strcmp(name, "keycreate"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006239 error = avc_has_perm(&selinux_state,
6240 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006241 PROCESS__SETKEYCREATE, NULL);
Eric Paris42c3e032006-06-26 00:26:03 -07006242 else if (!strcmp(name, "sockcreate"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006243 error = avc_has_perm(&selinux_state,
6244 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006245 PROCESS__SETSOCKCREATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246 else if (!strcmp(name, "current"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006247 error = avc_has_perm(&selinux_state,
6248 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006249 PROCESS__SETCURRENT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250 else
6251 error = -EINVAL;
6252 if (error)
6253 return error;
6254
6255 /* Obtain a SID for the context, if one was specified. */
Stephen Smalleya050a572017-01-31 11:54:04 -05006256 if (size && str[0] && str[0] != '\n') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006257 if (str[size-1] == '\n') {
6258 str[size-1] = 0;
6259 size--;
6260 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006261 error = security_context_to_sid(&selinux_state, value, size,
6262 &sid, GFP_KERNEL);
Stephen Smalley12b29f32008-05-07 13:03:20 -04006263 if (error == -EINVAL && !strcmp(name, "fscreate")) {
Stephen Smalleydb590002017-04-20 11:31:30 -04006264 if (!has_cap_mac_admin(true)) {
Eric Parisd6ea83e2012-04-04 13:45:49 -04006265 struct audit_buffer *ab;
6266 size_t audit_size;
6267
6268 /* We strip a nul only if it is at the end, otherwise the
6269 * context contains a nul and we should audit that */
6270 if (str[size - 1] == '\0')
6271 audit_size = size - 1;
6272 else
6273 audit_size = size;
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04006274 ab = audit_log_start(audit_context(),
6275 GFP_ATOMIC,
6276 AUDIT_SELINUX_ERR);
Eric Parisd6ea83e2012-04-04 13:45:49 -04006277 audit_log_format(ab, "op=fscreate invalid_context=");
6278 audit_log_n_untrustedstring(ab, value, audit_size);
6279 audit_log_end(ab);
6280
Stephen Smalley12b29f32008-05-07 13:03:20 -04006281 return error;
Eric Parisd6ea83e2012-04-04 13:45:49 -04006282 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006283 error = security_context_to_sid_force(
6284 &selinux_state,
6285 value, size, &sid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04006286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006287 if (error)
6288 return error;
6289 }
6290
David Howellsd84f4f92008-11-14 10:39:23 +11006291 new = prepare_creds();
6292 if (!new)
6293 return -ENOMEM;
6294
Linus Torvalds1da177e2005-04-16 15:20:36 -07006295 /* Permission checking based on the specified context is
6296 performed during the actual operation (execve,
6297 open/mkdir/...), when we know the full context of the
David Howellsd84f4f92008-11-14 10:39:23 +11006298 operation. See selinux_bprm_set_creds for the execve
Linus Torvalds1da177e2005-04-16 15:20:36 -07006299 checks and may_create for the file creation checks. The
6300 operation will then fail if the context is not permitted. */
David Howellsd84f4f92008-11-14 10:39:23 +11006301 tsec = new->security;
6302 if (!strcmp(name, "exec")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006303 tsec->exec_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006304 } else if (!strcmp(name, "fscreate")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006305 tsec->create_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006306 } else if (!strcmp(name, "keycreate")) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006307 error = avc_has_perm(&selinux_state,
6308 mysid, sid, SECCLASS_KEY, KEY__CREATE,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006309 NULL);
Michael LeMay4eb582c2006-06-26 00:24:57 -07006310 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11006311 goto abort_change;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006312 tsec->keycreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006313 } else if (!strcmp(name, "sockcreate")) {
Eric Paris42c3e032006-06-26 00:26:03 -07006314 tsec->sockcreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006315 } else if (!strcmp(name, "current")) {
6316 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006317 if (sid == 0)
David Howellsd84f4f92008-11-14 10:39:23 +11006318 goto abort_change;
KaiGai Koheid9250de2008-08-28 16:35:57 +09006319
David Howellsd84f4f92008-11-14 10:39:23 +11006320 /* Only allow single threaded processes to change context */
6321 error = -EPERM;
Oleg Nesterov5bb459b2009-07-10 03:48:23 +02006322 if (!current_is_single_threaded()) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006323 error = security_bounded_transition(&selinux_state,
6324 tsec->sid, sid);
David Howellsd84f4f92008-11-14 10:39:23 +11006325 if (error)
6326 goto abort_change;
Eric Paris828dfe12008-04-17 13:17:49 -04006327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006328
6329 /* Check permissions for the transition. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006330 error = avc_has_perm(&selinux_state,
6331 tsec->sid, sid, SECCLASS_PROCESS,
Eric Paris828dfe12008-04-17 13:17:49 -04006332 PROCESS__DYNTRANSITION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006333 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11006334 goto abort_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006335
6336 /* Check for ptracing, and update the task SID if ok.
6337 Otherwise, leave SID unchanged and fail. */
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006338 ptsid = ptrace_parent_sid();
Paul Moore0c6181c2016-03-30 21:41:21 -04006339 if (ptsid != 0) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006340 error = avc_has_perm(&selinux_state,
6341 ptsid, sid, SECCLASS_PROCESS,
David Howellsd84f4f92008-11-14 10:39:23 +11006342 PROCESS__PTRACE, NULL);
6343 if (error)
6344 goto abort_change;
6345 }
6346
6347 tsec->sid = sid;
6348 } else {
6349 error = -EINVAL;
6350 goto abort_change;
6351 }
6352
6353 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006354 return size;
David Howellsd84f4f92008-11-14 10:39:23 +11006355
6356abort_change:
6357 abort_creds(new);
6358 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006359}
6360
David Quigley746df9b2013-05-22 12:50:35 -04006361static int selinux_ismaclabel(const char *name)
6362{
6363 return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0);
6364}
6365
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006366static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
6367{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006368 return security_sid_to_context(&selinux_state, secid,
6369 secdata, seclen);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006370}
6371
David Howells7bf570d2008-04-29 20:52:51 +01006372static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
David Howells63cb3442008-01-15 23:47:35 +00006373{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006374 return security_context_to_sid(&selinux_state, secdata, seclen,
6375 secid, GFP_KERNEL);
David Howells63cb3442008-01-15 23:47:35 +00006376}
6377
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006378static void selinux_release_secctx(char *secdata, u32 seclen)
6379{
Paul Moore088999e2007-08-01 11:12:58 -04006380 kfree(secdata);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006381}
6382
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006383static void selinux_inode_invalidate_secctx(struct inode *inode)
6384{
6385 struct inode_security_struct *isec = inode->i_security;
6386
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01006387 spin_lock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006388 isec->initialized = LABEL_INVALID;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01006389 spin_unlock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006390}
6391
David P. Quigley1ee65e32009-09-03 14:25:57 -04006392/*
6393 * called with inode->i_mutex locked
6394 */
6395static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
6396{
6397 return selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, ctx, ctxlen, 0);
6398}
6399
6400/*
6401 * called with inode->i_mutex locked
6402 */
6403static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
6404{
6405 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0);
6406}
6407
6408static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
6409{
6410 int len = 0;
6411 len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX,
6412 ctx, true);
6413 if (len < 0)
6414 return len;
6415 *ctxlen = len;
6416 return 0;
6417}
Michael LeMayd7200242006-06-22 14:47:17 -07006418#ifdef CONFIG_KEYS
6419
David Howellsd84f4f92008-11-14 10:39:23 +11006420static int selinux_key_alloc(struct key *k, const struct cred *cred,
David Howells7e047ef2006-06-26 00:24:50 -07006421 unsigned long flags)
Michael LeMayd7200242006-06-22 14:47:17 -07006422{
David Howellsd84f4f92008-11-14 10:39:23 +11006423 const struct task_security_struct *tsec;
Michael LeMayd7200242006-06-22 14:47:17 -07006424 struct key_security_struct *ksec;
6425
6426 ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
6427 if (!ksec)
6428 return -ENOMEM;
6429
David Howellsd84f4f92008-11-14 10:39:23 +11006430 tsec = cred->security;
6431 if (tsec->keycreate_sid)
6432 ksec->sid = tsec->keycreate_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006433 else
David Howellsd84f4f92008-11-14 10:39:23 +11006434 ksec->sid = tsec->sid;
Michael LeMayd7200242006-06-22 14:47:17 -07006435
David Howells275bb412008-11-14 10:39:19 +11006436 k->security = ksec;
Michael LeMayd7200242006-06-22 14:47:17 -07006437 return 0;
6438}
6439
6440static void selinux_key_free(struct key *k)
6441{
6442 struct key_security_struct *ksec = k->security;
6443
6444 k->security = NULL;
6445 kfree(ksec);
6446}
6447
6448static int selinux_key_permission(key_ref_t key_ref,
David Howellsd84f4f92008-11-14 10:39:23 +11006449 const struct cred *cred,
David Howellsf5895942014-03-14 17:44:49 +00006450 unsigned perm)
Michael LeMayd7200242006-06-22 14:47:17 -07006451{
6452 struct key *key;
Michael LeMayd7200242006-06-22 14:47:17 -07006453 struct key_security_struct *ksec;
David Howells275bb412008-11-14 10:39:19 +11006454 u32 sid;
Michael LeMayd7200242006-06-22 14:47:17 -07006455
6456 /* if no specific permissions are requested, we skip the
6457 permission check. No serious, additional covert channels
6458 appear to be created. */
6459 if (perm == 0)
6460 return 0;
6461
David Howellsd84f4f92008-11-14 10:39:23 +11006462 sid = cred_sid(cred);
David Howells275bb412008-11-14 10:39:19 +11006463
6464 key = key_ref_to_ptr(key_ref);
6465 ksec = key->security;
6466
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006467 return avc_has_perm(&selinux_state,
6468 sid, ksec->sid, SECCLASS_KEY, perm, NULL);
Michael LeMayd7200242006-06-22 14:47:17 -07006469}
6470
David Howells70a5bb72008-04-29 01:01:26 -07006471static int selinux_key_getsecurity(struct key *key, char **_buffer)
6472{
6473 struct key_security_struct *ksec = key->security;
6474 char *context = NULL;
6475 unsigned len;
6476 int rc;
6477
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006478 rc = security_sid_to_context(&selinux_state, ksec->sid,
6479 &context, &len);
David Howells70a5bb72008-04-29 01:01:26 -07006480 if (!rc)
6481 rc = len;
6482 *_buffer = context;
6483 return rc;
6484}
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006485#endif
David Howells70a5bb72008-04-29 01:01:26 -07006486
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006487#ifdef CONFIG_SECURITY_INFINIBAND
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006488static int selinux_ib_pkey_access(void *ib_sec, u64 subnet_prefix, u16 pkey_val)
6489{
6490 struct common_audit_data ad;
6491 int err;
6492 u32 sid = 0;
6493 struct ib_security_struct *sec = ib_sec;
6494 struct lsm_ibpkey_audit ibpkey;
6495
Daniel Jurgens409dcf32017-05-19 15:48:59 +03006496 err = sel_ib_pkey_sid(subnet_prefix, pkey_val, &sid);
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006497 if (err)
6498 return err;
6499
6500 ad.type = LSM_AUDIT_DATA_IBPKEY;
6501 ibpkey.subnet_prefix = subnet_prefix;
6502 ibpkey.pkey = pkey_val;
6503 ad.u.ibpkey = &ibpkey;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006504 return avc_has_perm(&selinux_state,
6505 sec->sid, sid,
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006506 SECCLASS_INFINIBAND_PKEY,
6507 INFINIBAND_PKEY__ACCESS, &ad);
6508}
6509
Daniel Jurgensab861df2017-05-19 15:48:58 +03006510static int selinux_ib_endport_manage_subnet(void *ib_sec, const char *dev_name,
6511 u8 port_num)
6512{
6513 struct common_audit_data ad;
6514 int err;
6515 u32 sid = 0;
6516 struct ib_security_struct *sec = ib_sec;
6517 struct lsm_ibendport_audit ibendport;
6518
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006519 err = security_ib_endport_sid(&selinux_state, dev_name, port_num,
6520 &sid);
Daniel Jurgensab861df2017-05-19 15:48:58 +03006521
6522 if (err)
6523 return err;
6524
6525 ad.type = LSM_AUDIT_DATA_IBENDPORT;
6526 strncpy(ibendport.dev_name, dev_name, sizeof(ibendport.dev_name));
6527 ibendport.port = port_num;
6528 ad.u.ibendport = &ibendport;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006529 return avc_has_perm(&selinux_state,
6530 sec->sid, sid,
Daniel Jurgensab861df2017-05-19 15:48:58 +03006531 SECCLASS_INFINIBAND_ENDPORT,
6532 INFINIBAND_ENDPORT__MANAGE_SUBNET, &ad);
6533}
6534
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006535static int selinux_ib_alloc_security(void **ib_sec)
6536{
6537 struct ib_security_struct *sec;
6538
6539 sec = kzalloc(sizeof(*sec), GFP_KERNEL);
6540 if (!sec)
6541 return -ENOMEM;
6542 sec->sid = current_sid();
6543
6544 *ib_sec = sec;
6545 return 0;
6546}
6547
6548static void selinux_ib_free_security(void *ib_sec)
6549{
6550 kfree(ib_sec);
6551}
Michael LeMayd7200242006-06-22 14:47:17 -07006552#endif
6553
Chenbo Fengec27c352017-10-18 13:00:25 -07006554#ifdef CONFIG_BPF_SYSCALL
6555static int selinux_bpf(int cmd, union bpf_attr *attr,
6556 unsigned int size)
6557{
6558 u32 sid = current_sid();
6559 int ret;
6560
6561 switch (cmd) {
6562 case BPF_MAP_CREATE:
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006563 ret = avc_has_perm(&selinux_state,
6564 sid, sid, SECCLASS_BPF, BPF__MAP_CREATE,
Chenbo Fengec27c352017-10-18 13:00:25 -07006565 NULL);
6566 break;
6567 case BPF_PROG_LOAD:
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006568 ret = avc_has_perm(&selinux_state,
6569 sid, sid, SECCLASS_BPF, BPF__PROG_LOAD,
Chenbo Fengec27c352017-10-18 13:00:25 -07006570 NULL);
6571 break;
6572 default:
6573 ret = 0;
6574 break;
6575 }
6576
6577 return ret;
6578}
6579
6580static u32 bpf_map_fmode_to_av(fmode_t fmode)
6581{
6582 u32 av = 0;
6583
6584 if (fmode & FMODE_READ)
6585 av |= BPF__MAP_READ;
6586 if (fmode & FMODE_WRITE)
6587 av |= BPF__MAP_WRITE;
6588 return av;
6589}
6590
Chenbo Fengf66e4482017-10-18 13:00:26 -07006591/* This function will check the file pass through unix socket or binder to see
6592 * if it is a bpf related object. And apply correspinding checks on the bpf
6593 * object based on the type. The bpf maps and programs, not like other files and
6594 * socket, are using a shared anonymous inode inside the kernel as their inode.
6595 * So checking that inode cannot identify if the process have privilege to
6596 * access the bpf object and that's why we have to add this additional check in
6597 * selinux_file_receive and selinux_binder_transfer_files.
6598 */
6599static int bpf_fd_pass(struct file *file, u32 sid)
6600{
6601 struct bpf_security_struct *bpfsec;
6602 struct bpf_prog *prog;
6603 struct bpf_map *map;
6604 int ret;
6605
6606 if (file->f_op == &bpf_map_fops) {
6607 map = file->private_data;
6608 bpfsec = map->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006609 ret = avc_has_perm(&selinux_state,
6610 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengf66e4482017-10-18 13:00:26 -07006611 bpf_map_fmode_to_av(file->f_mode), NULL);
6612 if (ret)
6613 return ret;
6614 } else if (file->f_op == &bpf_prog_fops) {
6615 prog = file->private_data;
6616 bpfsec = prog->aux->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006617 ret = avc_has_perm(&selinux_state,
6618 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengf66e4482017-10-18 13:00:26 -07006619 BPF__PROG_RUN, NULL);
6620 if (ret)
6621 return ret;
6622 }
6623 return 0;
6624}
6625
Chenbo Fengec27c352017-10-18 13:00:25 -07006626static int selinux_bpf_map(struct bpf_map *map, fmode_t fmode)
6627{
6628 u32 sid = current_sid();
6629 struct bpf_security_struct *bpfsec;
6630
6631 bpfsec = map->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006632 return avc_has_perm(&selinux_state,
6633 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengec27c352017-10-18 13:00:25 -07006634 bpf_map_fmode_to_av(fmode), NULL);
6635}
6636
6637static int selinux_bpf_prog(struct bpf_prog *prog)
6638{
6639 u32 sid = current_sid();
6640 struct bpf_security_struct *bpfsec;
6641
6642 bpfsec = prog->aux->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006643 return avc_has_perm(&selinux_state,
6644 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengec27c352017-10-18 13:00:25 -07006645 BPF__PROG_RUN, NULL);
6646}
6647
6648static int selinux_bpf_map_alloc(struct bpf_map *map)
6649{
6650 struct bpf_security_struct *bpfsec;
6651
6652 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6653 if (!bpfsec)
6654 return -ENOMEM;
6655
6656 bpfsec->sid = current_sid();
6657 map->security = bpfsec;
6658
6659 return 0;
6660}
6661
6662static void selinux_bpf_map_free(struct bpf_map *map)
6663{
6664 struct bpf_security_struct *bpfsec = map->security;
6665
6666 map->security = NULL;
6667 kfree(bpfsec);
6668}
6669
6670static int selinux_bpf_prog_alloc(struct bpf_prog_aux *aux)
6671{
6672 struct bpf_security_struct *bpfsec;
6673
6674 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6675 if (!bpfsec)
6676 return -ENOMEM;
6677
6678 bpfsec->sid = current_sid();
6679 aux->security = bpfsec;
6680
6681 return 0;
6682}
6683
6684static void selinux_bpf_prog_free(struct bpf_prog_aux *aux)
6685{
6686 struct bpf_security_struct *bpfsec = aux->security;
6687
6688 aux->security = NULL;
6689 kfree(bpfsec);
6690}
6691#endif
6692
James Morrisca97d932017-02-15 00:18:51 +11006693static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {
Casey Schauflere20b0432015-05-02 15:11:36 -07006694 LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr),
6695 LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction),
6696 LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder),
6697 LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file),
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02006698
Casey Schauflere20b0432015-05-02 15:11:36 -07006699 LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check),
6700 LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme),
6701 LSM_HOOK_INIT(capget, selinux_capget),
6702 LSM_HOOK_INIT(capset, selinux_capset),
6703 LSM_HOOK_INIT(capable, selinux_capable),
6704 LSM_HOOK_INIT(quotactl, selinux_quotactl),
6705 LSM_HOOK_INIT(quota_on, selinux_quota_on),
6706 LSM_HOOK_INIT(syslog, selinux_syslog),
6707 LSM_HOOK_INIT(vm_enough_memory, selinux_vm_enough_memory),
Stephen Smalley79af7302015-01-21 10:54:10 -05006708
Casey Schauflere20b0432015-05-02 15:11:36 -07006709 LSM_HOOK_INIT(netlink_send, selinux_netlink_send),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006710
Casey Schauflere20b0432015-05-02 15:11:36 -07006711 LSM_HOOK_INIT(bprm_set_creds, selinux_bprm_set_creds),
6712 LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds),
6713 LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006714
Casey Schauflere20b0432015-05-02 15:11:36 -07006715 LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security),
6716 LSM_HOOK_INIT(sb_free_security, selinux_sb_free_security),
Al Viro5b400232018-12-12 20:13:29 -05006717 LSM_HOOK_INIT(sb_eat_lsm_opts, selinux_sb_eat_lsm_opts),
Al Viro204cc0c2018-12-13 13:41:47 -05006718 LSM_HOOK_INIT(sb_free_mnt_opts, selinux_free_mnt_opts),
Casey Schauflere20b0432015-05-02 15:11:36 -07006719 LSM_HOOK_INIT(sb_remount, selinux_sb_remount),
6720 LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount),
6721 LSM_HOOK_INIT(sb_show_options, selinux_sb_show_options),
6722 LSM_HOOK_INIT(sb_statfs, selinux_sb_statfs),
6723 LSM_HOOK_INIT(sb_mount, selinux_mount),
6724 LSM_HOOK_INIT(sb_umount, selinux_umount),
6725 LSM_HOOK_INIT(sb_set_mnt_opts, selinux_set_mnt_opts),
6726 LSM_HOOK_INIT(sb_clone_mnt_opts, selinux_sb_clone_mnt_opts),
Al Viro757cbe52018-12-14 23:42:21 -05006727 LSM_HOOK_INIT(sb_add_mnt_opt, selinux_add_mnt_opt),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006728
Casey Schauflere20b0432015-05-02 15:11:36 -07006729 LSM_HOOK_INIT(dentry_init_security, selinux_dentry_init_security),
Vivek Goyala518b0a2016-07-13 10:44:53 -04006730 LSM_HOOK_INIT(dentry_create_files_as, selinux_dentry_create_files_as),
Eric Parise0007522008-03-05 10:31:54 -05006731
Casey Schauflere20b0432015-05-02 15:11:36 -07006732 LSM_HOOK_INIT(inode_alloc_security, selinux_inode_alloc_security),
6733 LSM_HOOK_INIT(inode_free_security, selinux_inode_free_security),
6734 LSM_HOOK_INIT(inode_init_security, selinux_inode_init_security),
6735 LSM_HOOK_INIT(inode_create, selinux_inode_create),
6736 LSM_HOOK_INIT(inode_link, selinux_inode_link),
6737 LSM_HOOK_INIT(inode_unlink, selinux_inode_unlink),
6738 LSM_HOOK_INIT(inode_symlink, selinux_inode_symlink),
6739 LSM_HOOK_INIT(inode_mkdir, selinux_inode_mkdir),
6740 LSM_HOOK_INIT(inode_rmdir, selinux_inode_rmdir),
6741 LSM_HOOK_INIT(inode_mknod, selinux_inode_mknod),
6742 LSM_HOOK_INIT(inode_rename, selinux_inode_rename),
6743 LSM_HOOK_INIT(inode_readlink, selinux_inode_readlink),
6744 LSM_HOOK_INIT(inode_follow_link, selinux_inode_follow_link),
6745 LSM_HOOK_INIT(inode_permission, selinux_inode_permission),
6746 LSM_HOOK_INIT(inode_setattr, selinux_inode_setattr),
6747 LSM_HOOK_INIT(inode_getattr, selinux_inode_getattr),
6748 LSM_HOOK_INIT(inode_setxattr, selinux_inode_setxattr),
6749 LSM_HOOK_INIT(inode_post_setxattr, selinux_inode_post_setxattr),
6750 LSM_HOOK_INIT(inode_getxattr, selinux_inode_getxattr),
6751 LSM_HOOK_INIT(inode_listxattr, selinux_inode_listxattr),
6752 LSM_HOOK_INIT(inode_removexattr, selinux_inode_removexattr),
6753 LSM_HOOK_INIT(inode_getsecurity, selinux_inode_getsecurity),
6754 LSM_HOOK_INIT(inode_setsecurity, selinux_inode_setsecurity),
6755 LSM_HOOK_INIT(inode_listsecurity, selinux_inode_listsecurity),
6756 LSM_HOOK_INIT(inode_getsecid, selinux_inode_getsecid),
Vivek Goyal56909eb2016-07-13 10:44:48 -04006757 LSM_HOOK_INIT(inode_copy_up, selinux_inode_copy_up),
Vivek Goyal19472b62016-07-13 10:44:50 -04006758 LSM_HOOK_INIT(inode_copy_up_xattr, selinux_inode_copy_up_xattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006759
Casey Schauflere20b0432015-05-02 15:11:36 -07006760 LSM_HOOK_INIT(file_permission, selinux_file_permission),
6761 LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security),
6762 LSM_HOOK_INIT(file_free_security, selinux_file_free_security),
6763 LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl),
6764 LSM_HOOK_INIT(mmap_file, selinux_mmap_file),
6765 LSM_HOOK_INIT(mmap_addr, selinux_mmap_addr),
6766 LSM_HOOK_INIT(file_mprotect, selinux_file_mprotect),
6767 LSM_HOOK_INIT(file_lock, selinux_file_lock),
6768 LSM_HOOK_INIT(file_fcntl, selinux_file_fcntl),
6769 LSM_HOOK_INIT(file_set_fowner, selinux_file_set_fowner),
6770 LSM_HOOK_INIT(file_send_sigiotask, selinux_file_send_sigiotask),
6771 LSM_HOOK_INIT(file_receive, selinux_file_receive),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006772
Casey Schauflere20b0432015-05-02 15:11:36 -07006773 LSM_HOOK_INIT(file_open, selinux_file_open),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006774
Tetsuo Handaa79be232017-03-28 23:08:45 +09006775 LSM_HOOK_INIT(task_alloc, selinux_task_alloc),
Casey Schauflere20b0432015-05-02 15:11:36 -07006776 LSM_HOOK_INIT(cred_alloc_blank, selinux_cred_alloc_blank),
6777 LSM_HOOK_INIT(cred_free, selinux_cred_free),
6778 LSM_HOOK_INIT(cred_prepare, selinux_cred_prepare),
6779 LSM_HOOK_INIT(cred_transfer, selinux_cred_transfer),
Matthew Garrett3ec30112018-01-08 13:36:19 -08006780 LSM_HOOK_INIT(cred_getsecid, selinux_cred_getsecid),
Casey Schauflere20b0432015-05-02 15:11:36 -07006781 LSM_HOOK_INIT(kernel_act_as, selinux_kernel_act_as),
6782 LSM_HOOK_INIT(kernel_create_files_as, selinux_kernel_create_files_as),
6783 LSM_HOOK_INIT(kernel_module_request, selinux_kernel_module_request),
Mimi Zoharc77b8cd2018-07-13 14:06:02 -04006784 LSM_HOOK_INIT(kernel_load_data, selinux_kernel_load_data),
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07006785 LSM_HOOK_INIT(kernel_read_file, selinux_kernel_read_file),
Casey Schauflere20b0432015-05-02 15:11:36 -07006786 LSM_HOOK_INIT(task_setpgid, selinux_task_setpgid),
6787 LSM_HOOK_INIT(task_getpgid, selinux_task_getpgid),
6788 LSM_HOOK_INIT(task_getsid, selinux_task_getsid),
6789 LSM_HOOK_INIT(task_getsecid, selinux_task_getsecid),
6790 LSM_HOOK_INIT(task_setnice, selinux_task_setnice),
6791 LSM_HOOK_INIT(task_setioprio, selinux_task_setioprio),
6792 LSM_HOOK_INIT(task_getioprio, selinux_task_getioprio),
Stephen Smalley791ec492017-02-17 07:57:00 -05006793 LSM_HOOK_INIT(task_prlimit, selinux_task_prlimit),
Casey Schauflere20b0432015-05-02 15:11:36 -07006794 LSM_HOOK_INIT(task_setrlimit, selinux_task_setrlimit),
6795 LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler),
6796 LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler),
6797 LSM_HOOK_INIT(task_movememory, selinux_task_movememory),
6798 LSM_HOOK_INIT(task_kill, selinux_task_kill),
Casey Schauflere20b0432015-05-02 15:11:36 -07006799 LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode),
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09006800
Casey Schauflere20b0432015-05-02 15:11:36 -07006801 LSM_HOOK_INIT(ipc_permission, selinux_ipc_permission),
6802 LSM_HOOK_INIT(ipc_getsecid, selinux_ipc_getsecid),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006803
Casey Schauflere20b0432015-05-02 15:11:36 -07006804 LSM_HOOK_INIT(msg_msg_alloc_security, selinux_msg_msg_alloc_security),
6805 LSM_HOOK_INIT(msg_msg_free_security, selinux_msg_msg_free_security),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006806
Casey Schauflere20b0432015-05-02 15:11:36 -07006807 LSM_HOOK_INIT(msg_queue_alloc_security,
6808 selinux_msg_queue_alloc_security),
6809 LSM_HOOK_INIT(msg_queue_free_security, selinux_msg_queue_free_security),
6810 LSM_HOOK_INIT(msg_queue_associate, selinux_msg_queue_associate),
6811 LSM_HOOK_INIT(msg_queue_msgctl, selinux_msg_queue_msgctl),
6812 LSM_HOOK_INIT(msg_queue_msgsnd, selinux_msg_queue_msgsnd),
6813 LSM_HOOK_INIT(msg_queue_msgrcv, selinux_msg_queue_msgrcv),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006814
Casey Schauflere20b0432015-05-02 15:11:36 -07006815 LSM_HOOK_INIT(shm_alloc_security, selinux_shm_alloc_security),
6816 LSM_HOOK_INIT(shm_free_security, selinux_shm_free_security),
6817 LSM_HOOK_INIT(shm_associate, selinux_shm_associate),
6818 LSM_HOOK_INIT(shm_shmctl, selinux_shm_shmctl),
6819 LSM_HOOK_INIT(shm_shmat, selinux_shm_shmat),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006820
Casey Schauflere20b0432015-05-02 15:11:36 -07006821 LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security),
6822 LSM_HOOK_INIT(sem_free_security, selinux_sem_free_security),
6823 LSM_HOOK_INIT(sem_associate, selinux_sem_associate),
6824 LSM_HOOK_INIT(sem_semctl, selinux_sem_semctl),
6825 LSM_HOOK_INIT(sem_semop, selinux_sem_semop),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006826
Casey Schauflere20b0432015-05-02 15:11:36 -07006827 LSM_HOOK_INIT(d_instantiate, selinux_d_instantiate),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006828
Casey Schauflere20b0432015-05-02 15:11:36 -07006829 LSM_HOOK_INIT(getprocattr, selinux_getprocattr),
6830 LSM_HOOK_INIT(setprocattr, selinux_setprocattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006831
Casey Schauflere20b0432015-05-02 15:11:36 -07006832 LSM_HOOK_INIT(ismaclabel, selinux_ismaclabel),
6833 LSM_HOOK_INIT(secid_to_secctx, selinux_secid_to_secctx),
6834 LSM_HOOK_INIT(secctx_to_secid, selinux_secctx_to_secid),
6835 LSM_HOOK_INIT(release_secctx, selinux_release_secctx),
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006836 LSM_HOOK_INIT(inode_invalidate_secctx, selinux_inode_invalidate_secctx),
Casey Schauflere20b0432015-05-02 15:11:36 -07006837 LSM_HOOK_INIT(inode_notifysecctx, selinux_inode_notifysecctx),
6838 LSM_HOOK_INIT(inode_setsecctx, selinux_inode_setsecctx),
6839 LSM_HOOK_INIT(inode_getsecctx, selinux_inode_getsecctx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006840
Casey Schauflere20b0432015-05-02 15:11:36 -07006841 LSM_HOOK_INIT(unix_stream_connect, selinux_socket_unix_stream_connect),
6842 LSM_HOOK_INIT(unix_may_send, selinux_socket_unix_may_send),
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006843
Casey Schauflere20b0432015-05-02 15:11:36 -07006844 LSM_HOOK_INIT(socket_create, selinux_socket_create),
6845 LSM_HOOK_INIT(socket_post_create, selinux_socket_post_create),
David Herrmann0b811db2018-05-04 16:28:21 +02006846 LSM_HOOK_INIT(socket_socketpair, selinux_socket_socketpair),
Casey Schauflere20b0432015-05-02 15:11:36 -07006847 LSM_HOOK_INIT(socket_bind, selinux_socket_bind),
6848 LSM_HOOK_INIT(socket_connect, selinux_socket_connect),
6849 LSM_HOOK_INIT(socket_listen, selinux_socket_listen),
6850 LSM_HOOK_INIT(socket_accept, selinux_socket_accept),
6851 LSM_HOOK_INIT(socket_sendmsg, selinux_socket_sendmsg),
6852 LSM_HOOK_INIT(socket_recvmsg, selinux_socket_recvmsg),
6853 LSM_HOOK_INIT(socket_getsockname, selinux_socket_getsockname),
6854 LSM_HOOK_INIT(socket_getpeername, selinux_socket_getpeername),
6855 LSM_HOOK_INIT(socket_getsockopt, selinux_socket_getsockopt),
6856 LSM_HOOK_INIT(socket_setsockopt, selinux_socket_setsockopt),
6857 LSM_HOOK_INIT(socket_shutdown, selinux_socket_shutdown),
6858 LSM_HOOK_INIT(socket_sock_rcv_skb, selinux_socket_sock_rcv_skb),
6859 LSM_HOOK_INIT(socket_getpeersec_stream,
6860 selinux_socket_getpeersec_stream),
6861 LSM_HOOK_INIT(socket_getpeersec_dgram, selinux_socket_getpeersec_dgram),
6862 LSM_HOOK_INIT(sk_alloc_security, selinux_sk_alloc_security),
6863 LSM_HOOK_INIT(sk_free_security, selinux_sk_free_security),
6864 LSM_HOOK_INIT(sk_clone_security, selinux_sk_clone_security),
6865 LSM_HOOK_INIT(sk_getsecid, selinux_sk_getsecid),
6866 LSM_HOOK_INIT(sock_graft, selinux_sock_graft),
Richard Hainesd4529302018-02-13 20:57:18 +00006867 LSM_HOOK_INIT(sctp_assoc_request, selinux_sctp_assoc_request),
6868 LSM_HOOK_INIT(sctp_sk_clone, selinux_sctp_sk_clone),
6869 LSM_HOOK_INIT(sctp_bind_connect, selinux_sctp_bind_connect),
Casey Schauflere20b0432015-05-02 15:11:36 -07006870 LSM_HOOK_INIT(inet_conn_request, selinux_inet_conn_request),
6871 LSM_HOOK_INIT(inet_csk_clone, selinux_inet_csk_clone),
6872 LSM_HOOK_INIT(inet_conn_established, selinux_inet_conn_established),
6873 LSM_HOOK_INIT(secmark_relabel_packet, selinux_secmark_relabel_packet),
6874 LSM_HOOK_INIT(secmark_refcount_inc, selinux_secmark_refcount_inc),
6875 LSM_HOOK_INIT(secmark_refcount_dec, selinux_secmark_refcount_dec),
6876 LSM_HOOK_INIT(req_classify_flow, selinux_req_classify_flow),
6877 LSM_HOOK_INIT(tun_dev_alloc_security, selinux_tun_dev_alloc_security),
6878 LSM_HOOK_INIT(tun_dev_free_security, selinux_tun_dev_free_security),
6879 LSM_HOOK_INIT(tun_dev_create, selinux_tun_dev_create),
6880 LSM_HOOK_INIT(tun_dev_attach_queue, selinux_tun_dev_attach_queue),
6881 LSM_HOOK_INIT(tun_dev_attach, selinux_tun_dev_attach),
6882 LSM_HOOK_INIT(tun_dev_open, selinux_tun_dev_open),
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006883#ifdef CONFIG_SECURITY_INFINIBAND
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006884 LSM_HOOK_INIT(ib_pkey_access, selinux_ib_pkey_access),
Daniel Jurgensab861df2017-05-19 15:48:58 +03006885 LSM_HOOK_INIT(ib_endport_manage_subnet,
6886 selinux_ib_endport_manage_subnet),
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006887 LSM_HOOK_INIT(ib_alloc_security, selinux_ib_alloc_security),
6888 LSM_HOOK_INIT(ib_free_security, selinux_ib_free_security),
6889#endif
Trent Jaegerd28d1e02005-12-13 23:12:40 -08006890#ifdef CONFIG_SECURITY_NETWORK_XFRM
Casey Schauflere20b0432015-05-02 15:11:36 -07006891 LSM_HOOK_INIT(xfrm_policy_alloc_security, selinux_xfrm_policy_alloc),
6892 LSM_HOOK_INIT(xfrm_policy_clone_security, selinux_xfrm_policy_clone),
6893 LSM_HOOK_INIT(xfrm_policy_free_security, selinux_xfrm_policy_free),
6894 LSM_HOOK_INIT(xfrm_policy_delete_security, selinux_xfrm_policy_delete),
6895 LSM_HOOK_INIT(xfrm_state_alloc, selinux_xfrm_state_alloc),
6896 LSM_HOOK_INIT(xfrm_state_alloc_acquire,
6897 selinux_xfrm_state_alloc_acquire),
6898 LSM_HOOK_INIT(xfrm_state_free_security, selinux_xfrm_state_free),
6899 LSM_HOOK_INIT(xfrm_state_delete_security, selinux_xfrm_state_delete),
6900 LSM_HOOK_INIT(xfrm_policy_lookup, selinux_xfrm_policy_lookup),
6901 LSM_HOOK_INIT(xfrm_state_pol_flow_match,
6902 selinux_xfrm_state_pol_flow_match),
6903 LSM_HOOK_INIT(xfrm_decode_session, selinux_xfrm_decode_session),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006904#endif
Michael LeMayd7200242006-06-22 14:47:17 -07006905
6906#ifdef CONFIG_KEYS
Casey Schauflere20b0432015-05-02 15:11:36 -07006907 LSM_HOOK_INIT(key_alloc, selinux_key_alloc),
6908 LSM_HOOK_INIT(key_free, selinux_key_free),
6909 LSM_HOOK_INIT(key_permission, selinux_key_permission),
6910 LSM_HOOK_INIT(key_getsecurity, selinux_key_getsecurity),
Michael LeMayd7200242006-06-22 14:47:17 -07006911#endif
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02006912
6913#ifdef CONFIG_AUDIT
Casey Schauflere20b0432015-05-02 15:11:36 -07006914 LSM_HOOK_INIT(audit_rule_init, selinux_audit_rule_init),
6915 LSM_HOOK_INIT(audit_rule_known, selinux_audit_rule_known),
6916 LSM_HOOK_INIT(audit_rule_match, selinux_audit_rule_match),
6917 LSM_HOOK_INIT(audit_rule_free, selinux_audit_rule_free),
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02006918#endif
Chenbo Fengec27c352017-10-18 13:00:25 -07006919
6920#ifdef CONFIG_BPF_SYSCALL
6921 LSM_HOOK_INIT(bpf, selinux_bpf),
6922 LSM_HOOK_INIT(bpf_map, selinux_bpf_map),
6923 LSM_HOOK_INIT(bpf_prog, selinux_bpf_prog),
6924 LSM_HOOK_INIT(bpf_map_alloc_security, selinux_bpf_map_alloc),
6925 LSM_HOOK_INIT(bpf_prog_alloc_security, selinux_bpf_prog_alloc),
6926 LSM_HOOK_INIT(bpf_map_free_security, selinux_bpf_map_free),
6927 LSM_HOOK_INIT(bpf_prog_free_security, selinux_bpf_prog_free),
6928#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006929};
6930
6931static __init int selinux_init(void)
6932{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07006933 if (!security_module_enable("selinux")) {
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02006934 selinux_enabled = 0;
6935 return 0;
6936 }
6937
Linus Torvalds1da177e2005-04-16 15:20:36 -07006938 if (!selinux_enabled) {
peter enderborgc103a912018-06-12 10:09:03 +02006939 pr_info("SELinux: Disabled at boot.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006940 return 0;
6941 }
6942
peter enderborgc103a912018-06-12 10:09:03 +02006943 pr_info("SELinux: Initializing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006944
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006945 memset(&selinux_state, 0, sizeof(selinux_state));
Paul Mooree5a5ca92018-03-01 17:38:30 -05006946 enforcing_set(&selinux_state, selinux_enforcing_boot);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006947 selinux_state.checkreqprot = selinux_checkreqprot_boot;
6948 selinux_ss_init(&selinux_state.ss);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006949 selinux_avc_init(&selinux_state.avc);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006950
Linus Torvalds1da177e2005-04-16 15:20:36 -07006951 /* Set the security state for the initial task. */
David Howellsd84f4f92008-11-14 10:39:23 +11006952 cred_init_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006953
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04006954 default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
6955
James Morris7cae7e22006-03-22 00:09:22 -08006956 sel_inode_cache = kmem_cache_create("selinux_inode_security",
6957 sizeof(struct inode_security_struct),
Paul Mundt20c2df82007-07-20 10:11:58 +09006958 0, SLAB_PANIC, NULL);
Sangwoo63205652015-10-21 17:44:30 -04006959 file_security_cache = kmem_cache_create("selinux_file_security",
6960 sizeof(struct file_security_struct),
6961 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006962 avc_init();
6963
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006964 avtab_cache_init();
6965
6966 ebitmap_cache_init();
6967
6968 hashtab_cache_init();
6969
Casey Schauflerd69dece52017-01-18 17:09:05 -08006970 security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks), "selinux");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006971
Paul Moore615e51f2014-06-26 14:33:56 -04006972 if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET))
6973 panic("SELinux: Unable to register AVC netcache callback\n");
6974
Daniel Jurgens8f408ab2017-05-19 15:48:53 +03006975 if (avc_add_callback(selinux_lsm_notifier_avc_callback, AVC_CALLBACK_RESET))
6976 panic("SELinux: Unable to register AVC LSM notifier callback\n");
6977
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006978 if (selinux_enforcing_boot)
peter enderborgc103a912018-06-12 10:09:03 +02006979 pr_debug("SELinux: Starting in enforcing mode\n");
Eric Paris828dfe12008-04-17 13:17:49 -04006980 else
peter enderborgc103a912018-06-12 10:09:03 +02006981 pr_debug("SELinux: Starting in permissive mode\n");
Michael LeMayd7200242006-06-22 14:47:17 -07006982
Linus Torvalds1da177e2005-04-16 15:20:36 -07006983 return 0;
6984}
6985
Al Viroe8c26252010-03-23 06:36:54 -04006986static void delayed_superblock_init(struct super_block *sb, void *unused)
6987{
Al Viro204cc0c2018-12-13 13:41:47 -05006988 selinux_set_mnt_opts(sb, NULL, 0, NULL);
Al Viroe8c26252010-03-23 06:36:54 -04006989}
6990
Linus Torvalds1da177e2005-04-16 15:20:36 -07006991void selinux_complete_init(void)
6992{
peter enderborgc103a912018-06-12 10:09:03 +02006993 pr_debug("SELinux: Completing initialization.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006994
6995 /* Set up any superblocks initialized prior to the policy load. */
peter enderborgc103a912018-06-12 10:09:03 +02006996 pr_debug("SELinux: Setting up existing superblocks.\n");
Al Viroe8c26252010-03-23 06:36:54 -04006997 iterate_supers(delayed_superblock_init, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006998}
6999
7000/* SELinux requires early initialization in order to label
7001 all processes and objects when they are created. */
Kees Cook3d6e5f62018-10-10 17:18:23 -07007002DEFINE_LSM(selinux) = {
Kees Cook07aed2f2018-10-10 17:18:24 -07007003 .name = "selinux",
Kees Cook3d6e5f62018-10-10 17:18:23 -07007004 .init = selinux_init,
7005};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007006
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007007#if defined(CONFIG_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007008
Florian Westphal591bb272017-07-26 11:40:52 +02007009static const struct nf_hook_ops selinux_nf_ops[] = {
Paul Mooreeffad8d2008-01-29 08:49:27 -05007010 {
7011 .hook = selinux_ipv4_postroute,
Alban Crequy2597a832012-05-14 03:56:39 +00007012 .pf = NFPROTO_IPV4,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007013 .hooknum = NF_INET_POST_ROUTING,
7014 .priority = NF_IP_PRI_SELINUX_LAST,
7015 },
7016 {
7017 .hook = selinux_ipv4_forward,
Alban Crequy2597a832012-05-14 03:56:39 +00007018 .pf = NFPROTO_IPV4,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007019 .hooknum = NF_INET_FORWARD,
7020 .priority = NF_IP_PRI_SELINUX_FIRST,
Paul Moore948bf852008-10-10 10:16:32 -04007021 },
7022 {
7023 .hook = selinux_ipv4_output,
Alban Crequy2597a832012-05-14 03:56:39 +00007024 .pf = NFPROTO_IPV4,
Paul Moore948bf852008-10-10 10:16:32 -04007025 .hooknum = NF_INET_LOCAL_OUT,
7026 .priority = NF_IP_PRI_SELINUX_FIRST,
Jiri Pirko25db6be2014-09-03 17:42:13 +02007027 },
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04007028#if IS_ENABLED(CONFIG_IPV6)
Paul Mooreeffad8d2008-01-29 08:49:27 -05007029 {
7030 .hook = selinux_ipv6_postroute,
Alban Crequy2597a832012-05-14 03:56:39 +00007031 .pf = NFPROTO_IPV6,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007032 .hooknum = NF_INET_POST_ROUTING,
7033 .priority = NF_IP6_PRI_SELINUX_LAST,
7034 },
7035 {
7036 .hook = selinux_ipv6_forward,
Alban Crequy2597a832012-05-14 03:56:39 +00007037 .pf = NFPROTO_IPV6,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007038 .hooknum = NF_INET_FORWARD,
7039 .priority = NF_IP6_PRI_SELINUX_FIRST,
Jiri Pirko25db6be2014-09-03 17:42:13 +02007040 },
Huw Davies2917f572016-06-27 15:06:15 -04007041 {
7042 .hook = selinux_ipv6_output,
7043 .pf = NFPROTO_IPV6,
7044 .hooknum = NF_INET_LOCAL_OUT,
7045 .priority = NF_IP6_PRI_SELINUX_FIRST,
7046 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07007047#endif /* IPV6 */
Jiri Pirko25db6be2014-09-03 17:42:13 +02007048};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007049
Florian Westphal8e71bf72017-04-21 11:49:09 +02007050static int __net_init selinux_nf_register(struct net *net)
7051{
7052 return nf_register_net_hooks(net, selinux_nf_ops,
7053 ARRAY_SIZE(selinux_nf_ops));
7054}
7055
7056static void __net_exit selinux_nf_unregister(struct net *net)
7057{
7058 nf_unregister_net_hooks(net, selinux_nf_ops,
7059 ARRAY_SIZE(selinux_nf_ops));
7060}
7061
7062static struct pernet_operations selinux_net_ops = {
7063 .init = selinux_nf_register,
7064 .exit = selinux_nf_unregister,
7065};
7066
Linus Torvalds1da177e2005-04-16 15:20:36 -07007067static int __init selinux_nf_ip_init(void)
7068{
Jiri Pirko25db6be2014-09-03 17:42:13 +02007069 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007070
7071 if (!selinux_enabled)
Jiri Pirko25db6be2014-09-03 17:42:13 +02007072 return 0;
Eric Parisfadcdb42007-02-22 18:11:31 -05007073
peter enderborgc103a912018-06-12 10:09:03 +02007074 pr_debug("SELinux: Registering netfilter hooks\n");
Eric Parisfadcdb42007-02-22 18:11:31 -05007075
Florian Westphal8e71bf72017-04-21 11:49:09 +02007076 err = register_pernet_subsys(&selinux_net_ops);
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07007077 if (err)
Florian Westphal8e71bf72017-04-21 11:49:09 +02007078 panic("SELinux: register_pernet_subsys: error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007079
Jiri Pirko25db6be2014-09-03 17:42:13 +02007080 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007081}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007082__initcall(selinux_nf_ip_init);
7083
7084#ifdef CONFIG_SECURITY_SELINUX_DISABLE
7085static void selinux_nf_ip_exit(void)
7086{
peter enderborgc103a912018-06-12 10:09:03 +02007087 pr_debug("SELinux: Unregistering netfilter hooks\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007088
Florian Westphal8e71bf72017-04-21 11:49:09 +02007089 unregister_pernet_subsys(&selinux_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007090}
7091#endif
7092
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007093#else /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007094
7095#ifdef CONFIG_SECURITY_SELINUX_DISABLE
7096#define selinux_nf_ip_exit()
7097#endif
7098
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007099#endif /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007100
7101#ifdef CONFIG_SECURITY_SELINUX_DISABLE
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007102int selinux_disable(struct selinux_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007103{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007104 if (state->initialized) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007105 /* Not permitted after initial policy load. */
7106 return -EINVAL;
7107 }
7108
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007109 if (state->disabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007110 /* Only do this once. */
7111 return -EINVAL;
7112 }
7113
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007114 state->disabled = 1;
7115
peter enderborgc103a912018-06-12 10:09:03 +02007116 pr_info("SELinux: Disabled at runtime.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007117
Stephen Smalley30d55282006-05-03 10:52:36 -04007118 selinux_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007119
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07007120 security_delete_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007121
Eric Parisaf8ff042009-09-20 21:23:01 -04007122 /* Try to destroy the avc node cache */
7123 avc_disable();
7124
Linus Torvalds1da177e2005-04-16 15:20:36 -07007125 /* Unregister netfilter hooks. */
7126 selinux_nf_ip_exit();
7127
7128 /* Unregister selinuxfs. */
7129 exit_sel_fs();
7130
7131 return 0;
7132}
7133#endif