blob: 620be0367c0b6ba9e68f2d242b3ab1f3dcc107c4 [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>
Eric Paris23970742006-09-25 23:32:01 -070082#include <linux/mutex.h>
Frank Mayharf06febc2008-09-12 09:54:39 -070083#include <linux/posix-timers.h>
Kees Cook00234592010-02-03 15:36:43 -080084#include <linux/syslog.h>
Serge E. Hallyn34867402011-03-23 16:43:17 -070085#include <linux/user_namespace.h>
Paul Gortmaker44fc7ea2011-05-26 20:52:10 -040086#include <linux/export.h>
Al Viro40401532012-02-13 03:58:52 +000087#include <linux/msg.h>
88#include <linux/shm.h>
Chenbo Fengec27c352017-10-18 13:00:25 -070089#include <linux/bpf.h>
David Howellse262e32d2018-11-01 23:07:23 +000090#include <uapi/linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92#include "avc.h"
93#include "objsec.h"
94#include "netif.h"
Paul Moore224dfbd2008-01-29 08:38:13 -050095#include "netnode.h"
Paul Moore3e1121722008-04-10 10:48:14 -040096#include "netport.h"
Daniel Jurgens409dcf32017-05-19 15:48:59 +030097#include "ibpkey.h"
Trent Jaegerd28d1e02005-12-13 23:12:40 -080098#include "xfrm.h"
Paul Moorec60475b2007-02-28 15:14:23 -050099#include "netlabel.h"
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +0200100#include "audit.h"
James Morris7b98a582011-08-30 12:52:32 +1000101#include "avc_ss.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500103struct selinux_state selinux_state;
104
Paul Moored621d352008-01-29 08:43:36 -0500105/* SECMARK reference count */
James Morris56a4ca92011-08-17 11:08:43 +1000106static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0);
Paul Moored621d352008-01-29 08:43:36 -0500107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108#ifdef CONFIG_SECURITY_SELINUX_DEVELOP
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500109static int selinux_enforcing_boot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111static int __init enforcing_setup(char *str)
112{
Eric Parisf5269712008-05-14 11:27:45 -0400113 unsigned long enforcing;
Jingoo Han29707b22014-02-05 15:13:14 +0900114 if (!kstrtoul(str, 0, &enforcing))
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500115 selinux_enforcing_boot = enforcing ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 return 1;
117}
118__setup("enforcing=", enforcing_setup);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500119#else
120#define selinux_enforcing_boot 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121#endif
122
Kees Cookbe6ec882018-10-01 17:08:57 -0700123int selinux_enabled __lsm_ro_after_init = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124#ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125static int __init selinux_enabled_setup(char *str)
126{
Eric Parisf5269712008-05-14 11:27:45 -0400127 unsigned long enabled;
Jingoo Han29707b22014-02-05 15:13:14 +0900128 if (!kstrtoul(str, 0, &enabled))
Eric Parisf5269712008-05-14 11:27:45 -0400129 selinux_enabled = enabled ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 return 1;
131}
132__setup("selinux=", selinux_enabled_setup);
133#endif
134
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500135static unsigned int selinux_checkreqprot_boot =
136 CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE;
137
138static int __init checkreqprot_setup(char *str)
139{
140 unsigned long checkreqprot;
141
142 if (!kstrtoul(str, 0, &checkreqprot))
143 selinux_checkreqprot_boot = checkreqprot ? 1 : 0;
144 return 1;
145}
146__setup("checkreqprot=", checkreqprot_setup);
147
Christoph Lametere18b8902006-12-06 20:33:20 -0800148static struct kmem_cache *sel_inode_cache;
Sangwoo63205652015-10-21 17:44:30 -0400149static struct kmem_cache *file_security_cache;
James Morris7cae7e22006-03-22 00:09:22 -0800150
Paul Moored621d352008-01-29 08:43:36 -0500151/**
152 * selinux_secmark_enabled - Check to see if SECMARK is currently enabled
153 *
154 * Description:
155 * This function checks the SECMARK reference counter to see if any SECMARK
156 * targets are currently configured, if the reference counter is greater than
157 * zero SECMARK is considered to be enabled. Returns true (1) if SECMARK is
Chris PeBenito2be4d742013-05-03 09:05:39 -0400158 * enabled, false (0) if SECMARK is disabled. If the always_check_network
159 * policy capability is enabled, SECMARK is always considered enabled.
Paul Moored621d352008-01-29 08:43:36 -0500160 *
161 */
162static int selinux_secmark_enabled(void)
163{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500164 return (selinux_policycap_alwaysnetwork() ||
165 atomic_read(&selinux_secmark_refcount));
Chris PeBenito2be4d742013-05-03 09:05:39 -0400166}
167
168/**
169 * selinux_peerlbl_enabled - Check to see if peer labeling is currently enabled
170 *
171 * Description:
172 * This function checks if NetLabel or labeled IPSEC is enabled. Returns true
173 * (1) if any are enabled or false (0) if neither are enabled. If the
174 * always_check_network policy capability is enabled, peer labeling
175 * is always considered enabled.
176 *
177 */
178static int selinux_peerlbl_enabled(void)
179{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500180 return (selinux_policycap_alwaysnetwork() ||
181 netlbl_enabled() || selinux_xfrm_enabled());
Paul Moored621d352008-01-29 08:43:36 -0500182}
183
Paul Moore615e51f2014-06-26 14:33:56 -0400184static int selinux_netcache_avc_callback(u32 event)
185{
186 if (event == AVC_CALLBACK_RESET) {
187 sel_netif_flush();
188 sel_netnode_flush();
189 sel_netport_flush();
190 synchronize_net();
191 }
192 return 0;
193}
194
Daniel Jurgens8f408ab2017-05-19 15:48:53 +0300195static int selinux_lsm_notifier_avc_callback(u32 event)
196{
Daniel Jurgens409dcf32017-05-19 15:48:59 +0300197 if (event == AVC_CALLBACK_RESET) {
198 sel_ib_pkey_flush();
Daniel Jurgens8f408ab2017-05-19 15:48:53 +0300199 call_lsm_notifier(LSM_POLICY_CHANGE, NULL);
Daniel Jurgens409dcf32017-05-19 15:48:59 +0300200 }
Daniel Jurgens8f408ab2017-05-19 15:48:53 +0300201
202 return 0;
203}
204
David Howellsd84f4f92008-11-14 10:39:23 +1100205/*
206 * initialise the security for the init task
207 */
208static void cred_init_security(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
David Howells3b11a1d2008-11-14 10:39:26 +1100210 struct cred *cred = (struct cred *) current->real_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 struct task_security_struct *tsec;
212
Casey Schauflerbbd36622018-11-12 09:30:56 -0800213 lsm_early_cred(cred);
214 tsec = selinux_cred(cred);
David Howellsd84f4f92008-11-14 10:39:23 +1100215 tsec->osid = tsec->sid = SECINITSID_KERNEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
David Howells275bb412008-11-14 10:39:19 +1100218/*
David Howells88e67f32008-11-14 10:39:21 +1100219 * get the security ID of a set of credentials
220 */
221static inline u32 cred_sid(const struct cred *cred)
222{
223 const struct task_security_struct *tsec;
224
Casey Schaufler0c6cfa62018-09-21 17:17:16 -0700225 tsec = selinux_cred(cred);
David Howells88e67f32008-11-14 10:39:21 +1100226 return tsec->sid;
227}
228
229/*
David Howells3b11a1d2008-11-14 10:39:26 +1100230 * get the objective security ID of a task
David Howells275bb412008-11-14 10:39:19 +1100231 */
232static inline u32 task_sid(const struct task_struct *task)
233{
David Howells275bb412008-11-14 10:39:19 +1100234 u32 sid;
235
236 rcu_read_lock();
David Howells88e67f32008-11-14 10:39:21 +1100237 sid = cred_sid(__task_cred(task));
David Howells275bb412008-11-14 10:39:19 +1100238 rcu_read_unlock();
239 return sid;
240}
241
David Howells88e67f32008-11-14 10:39:21 +1100242/* Allocate and free functions for each kind of security blob. */
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244static int inode_alloc_security(struct inode *inode)
245{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 struct inode_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +1100247 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Josef Bacika02fe132008-04-04 09:35:05 +1100249 isec = kmem_cache_zalloc(sel_inode_cache, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 if (!isec)
251 return -ENOMEM;
252
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +0100253 spin_lock_init(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 INIT_LIST_HEAD(&isec->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 isec->inode = inode;
256 isec->sid = SECINITSID_UNLABELED;
257 isec->sclass = SECCLASS_FILE;
David Howells275bb412008-11-14 10:39:19 +1100258 isec->task_sid = sid;
Andreas Gruenbacher42059112016-11-10 22:18:27 +0100259 isec->initialized = LABEL_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 inode->i_security = isec;
261
262 return 0;
263}
264
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500265static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry);
266
267/*
268 * Try reloading inode security labels that have been marked as invalid. The
269 * @may_sleep parameter indicates when sleeping and thus reloading labels is
Andreas Gruenbacher42059112016-11-10 22:18:27 +0100270 * allowed; when set to false, returns -ECHILD when the label is
Al Viroe9193282018-04-24 21:31:02 -0400271 * invalid. The @dentry parameter should be set to a dentry of the inode.
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500272 */
273static int __inode_security_revalidate(struct inode *inode,
Al Viroe9193282018-04-24 21:31:02 -0400274 struct dentry *dentry,
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500275 bool may_sleep)
276{
277 struct inode_security_struct *isec = inode->i_security;
278
279 might_sleep_if(may_sleep);
280
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500281 if (selinux_state.initialized &&
282 isec->initialized != LABEL_INITIALIZED) {
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500283 if (!may_sleep)
284 return -ECHILD;
285
286 /*
287 * Try reloading the inode security label. This will fail if
288 * @opt_dentry is NULL and no dentry for this inode can be
289 * found; in that case, continue using the old label.
290 */
Al Viroe9193282018-04-24 21:31:02 -0400291 inode_doinit_with_dentry(inode, dentry);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500292 }
293 return 0;
294}
295
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500296static struct inode_security_struct *inode_security_novalidate(struct inode *inode)
297{
298 return inode->i_security;
299}
300
301static struct inode_security_struct *inode_security_rcu(struct inode *inode, bool rcu)
302{
303 int error;
304
305 error = __inode_security_revalidate(inode, NULL, !rcu);
306 if (error)
307 return ERR_PTR(error);
308 return inode->i_security;
309}
310
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500311/*
312 * Get the security label of an inode.
313 */
314static struct inode_security_struct *inode_security(struct inode *inode)
315{
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500316 __inode_security_revalidate(inode, NULL, true);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500317 return inode->i_security;
318}
319
Paul Moore2c971652016-04-19 16:36:28 -0400320static struct inode_security_struct *backing_inode_security_novalidate(struct dentry *dentry)
321{
322 struct inode *inode = d_backing_inode(dentry);
323
324 return inode->i_security;
325}
326
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500327/*
328 * Get the security label of a dentry's backing inode.
329 */
330static struct inode_security_struct *backing_inode_security(struct dentry *dentry)
331{
332 struct inode *inode = d_backing_inode(dentry);
333
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500334 __inode_security_revalidate(inode, dentry, true);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500335 return inode->i_security;
336}
337
Steven Rostedt3dc91d42014-01-09 21:46:34 -0500338static void inode_free_rcu(struct rcu_head *head)
339{
340 struct inode_security_struct *isec;
341
342 isec = container_of(head, struct inode_security_struct, rcu);
343 kmem_cache_free(sel_inode_cache, isec);
344}
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346static void inode_free_security(struct inode *inode)
347{
348 struct inode_security_struct *isec = inode->i_security;
349 struct superblock_security_struct *sbsec = inode->i_sb->s_security;
350
Waiman Long9629d042015-07-10 17:19:56 -0400351 /*
352 * As not all inode security structures are in a list, we check for
353 * empty list outside of the lock to make sure that we won't waste
354 * time taking a lock doing nothing.
355 *
356 * The list_del_init() function can be safely called more than once.
357 * It should not be possible for this function to be called with
358 * concurrent list_add(), but for better safety against future changes
359 * in the code, we use list_empty_careful() here.
360 */
361 if (!list_empty_careful(&isec->list)) {
362 spin_lock(&sbsec->isec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 list_del_init(&isec->list);
Waiman Long9629d042015-07-10 17:19:56 -0400364 spin_unlock(&sbsec->isec_lock);
365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Steven Rostedt3dc91d42014-01-09 21:46:34 -0500367 /*
368 * The inode may still be referenced in a path walk and
369 * a call to selinux_inode_permission() can be made
370 * after inode_free_security() is called. Ideally, the VFS
371 * wouldn't do this, but fixing that is a much harder
372 * job. For now, simply free the i_security via RCU, and
373 * leave the current inode->i_security pointer intact.
374 * The inode will be freed after the RCU grace period too.
375 */
376 call_rcu(&isec->rcu, inode_free_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377}
378
379static int file_alloc_security(struct file *file)
380{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 struct file_security_struct *fsec;
David Howells275bb412008-11-14 10:39:19 +1100382 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Sangwoo63205652015-10-21 17:44:30 -0400384 fsec = kmem_cache_zalloc(file_security_cache, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 if (!fsec)
386 return -ENOMEM;
387
David Howells275bb412008-11-14 10:39:19 +1100388 fsec->sid = sid;
389 fsec->fown_sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 file->f_security = fsec;
391
392 return 0;
393}
394
395static void file_free_security(struct file *file)
396{
Casey Schauflerbb6c6b02018-09-21 17:22:32 -0700397 struct file_security_struct *fsec = selinux_file(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 file->f_security = NULL;
Sangwoo63205652015-10-21 17:44:30 -0400399 kmem_cache_free(file_security_cache, fsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
401
402static int superblock_alloc_security(struct super_block *sb)
403{
404 struct superblock_security_struct *sbsec;
405
James Morris89d155e2005-10-30 14:59:21 -0800406 sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (!sbsec)
408 return -ENOMEM;
409
Eric Parisbc7e9822006-09-25 23:32:02 -0700410 mutex_init(&sbsec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 INIT_LIST_HEAD(&sbsec->isec_head);
412 spin_lock_init(&sbsec->isec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 sbsec->sb = sb;
414 sbsec->sid = SECINITSID_UNLABELED;
415 sbsec->def_sid = SECINITSID_FILE;
Eric Parisc312feb2006-07-10 04:43:53 -0700416 sbsec->mntpoint_sid = SECINITSID_UNLABELED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 sb->s_security = sbsec;
418
419 return 0;
420}
421
422static void superblock_free_security(struct super_block *sb)
423{
424 struct superblock_security_struct *sbsec = sb->s_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 sb->s_security = NULL;
426 kfree(sbsec);
427}
428
Al Virobd323652018-12-13 15:04:59 -0500429struct selinux_mnt_opts {
430 const char *fscontext, *context, *rootcontext, *defcontext;
431};
432
Al Viro204cc0c2018-12-13 13:41:47 -0500433static void selinux_free_mnt_opts(void *mnt_opts)
434{
Al Virobd323652018-12-13 15:04:59 -0500435 struct selinux_mnt_opts *opts = mnt_opts;
436 kfree(opts->fscontext);
437 kfree(opts->context);
438 kfree(opts->rootcontext);
439 kfree(opts->defcontext);
Al Viro204cc0c2018-12-13 13:41:47 -0500440 kfree(opts);
441}
442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443static inline int inode_doinit(struct inode *inode)
444{
445 return inode_doinit_with_dentry(inode, NULL);
446}
447
448enum {
Eric Paris31e87932007-09-19 17:19:12 -0400449 Opt_error = -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 Opt_context = 1,
451 Opt_fscontext = 2,
Eric Parisc9180a52007-11-30 13:00:35 -0500452 Opt_defcontext = 3,
453 Opt_rootcontext = 4,
Al Viroda3d76a2018-12-17 10:14:16 -0500454 Opt_seclabel = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455};
456
Al Viroda3d76a2018-12-17 10:14:16 -0500457#define A(s, has_arg) {#s, sizeof(#s) - 1, Opt_##s, has_arg}
Al Viro169d68efb2018-12-14 22:44:50 -0500458static struct {
459 const char *name;
460 int len;
461 int opt;
462 bool has_arg;
463} tokens[] = {
Al Viroda3d76a2018-12-17 10:14:16 -0500464 A(context, true),
465 A(fscontext, true),
466 A(defcontext, true),
467 A(rootcontext, true),
468 A(seclabel, false),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469};
Al Viro169d68efb2018-12-14 22:44:50 -0500470#undef A
471
472static int match_opt_prefix(char *s, int l, char **arg)
473{
474 int i;
475
476 for (i = 0; i < ARRAY_SIZE(tokens); i++) {
477 size_t len = tokens[i].len;
478 if (len > l || memcmp(s, tokens[i].name, len))
479 continue;
480 if (tokens[i].has_arg) {
481 if (len == l || s[len] != '=')
482 continue;
483 *arg = s + len + 1;
484 } else if (len != l)
485 continue;
486 return tokens[i].opt;
487 }
488 return Opt_error;
489}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491#define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n"
492
Eric Parisc312feb2006-07-10 04:43:53 -0700493static int may_context_mount_sb_relabel(u32 sid,
494 struct superblock_security_struct *sbsec,
David Howells275bb412008-11-14 10:39:19 +1100495 const struct cred *cred)
Eric Parisc312feb2006-07-10 04:43:53 -0700496{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -0700497 const struct task_security_struct *tsec = selinux_cred(cred);
Eric Parisc312feb2006-07-10 04:43:53 -0700498 int rc;
499
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500500 rc = avc_has_perm(&selinux_state,
501 tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
Eric Parisc312feb2006-07-10 04:43:53 -0700502 FILESYSTEM__RELABELFROM, NULL);
503 if (rc)
504 return rc;
505
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500506 rc = avc_has_perm(&selinux_state,
507 tsec->sid, sid, SECCLASS_FILESYSTEM,
Eric Parisc312feb2006-07-10 04:43:53 -0700508 FILESYSTEM__RELABELTO, NULL);
509 return rc;
510}
511
Eric Paris08089252006-07-10 04:43:55 -0700512static int may_context_mount_inode_relabel(u32 sid,
513 struct superblock_security_struct *sbsec,
David Howells275bb412008-11-14 10:39:19 +1100514 const struct cred *cred)
Eric Paris08089252006-07-10 04:43:55 -0700515{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -0700516 const struct task_security_struct *tsec = selinux_cred(cred);
Eric Paris08089252006-07-10 04:43:55 -0700517 int rc;
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500518 rc = avc_has_perm(&selinux_state,
519 tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
Eric Paris08089252006-07-10 04:43:55 -0700520 FILESYSTEM__RELABELFROM, NULL);
521 if (rc)
522 return rc;
523
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500524 rc = avc_has_perm(&selinux_state,
525 sid, sbsec->sid, SECCLASS_FILESYSTEM,
Eric Paris08089252006-07-10 04:43:55 -0700526 FILESYSTEM__ASSOCIATE, NULL);
527 return rc;
528}
529
Eric Parisb43e7252012-10-10 14:27:35 -0400530static int selinux_is_sblabel_mnt(struct super_block *sb)
531{
532 struct superblock_security_struct *sbsec = sb->s_security;
533
Mark Salyzynd5f3a5f2015-02-04 11:34:30 -0500534 return sbsec->behavior == SECURITY_FS_USE_XATTR ||
535 sbsec->behavior == SECURITY_FS_USE_TRANS ||
536 sbsec->behavior == SECURITY_FS_USE_TASK ||
J. Bruce Fields9fc2b4b2015-06-04 15:57:25 -0400537 sbsec->behavior == SECURITY_FS_USE_NATIVE ||
Mark Salyzynd5f3a5f2015-02-04 11:34:30 -0500538 /* Special handling. Genfs but also in-core setxattr handler */
539 !strcmp(sb->s_type->name, "sysfs") ||
540 !strcmp(sb->s_type->name, "pstore") ||
541 !strcmp(sb->s_type->name, "debugfs") ||
Yongqin Liua2c7c6f2017-01-09 10:07:30 -0500542 !strcmp(sb->s_type->name, "tracefs") ||
Stephen Smalley2651225b2017-02-28 10:35:56 -0500543 !strcmp(sb->s_type->name, "rootfs") ||
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500544 (selinux_policycap_cgroupseclabel() &&
Stephen Smalley2651225b2017-02-28 10:35:56 -0500545 (!strcmp(sb->s_type->name, "cgroup") ||
546 !strcmp(sb->s_type->name, "cgroup2")));
Eric Parisb43e7252012-10-10 14:27:35 -0400547}
548
Eric Parisc9180a52007-11-30 13:00:35 -0500549static int sb_finish_set_opts(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
551 struct superblock_security_struct *sbsec = sb->s_security;
552 struct dentry *root = sb->s_root;
David Howellsc6f493d2015-03-17 22:26:22 +0000553 struct inode *root_inode = d_backing_inode(root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 int rc = 0;
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
557 /* Make sure that the xattr handler exists and that no
558 error other than -ENODATA is returned by getxattr on
559 the root directory. -ENODATA is ok, as this may be
560 the first boot of the SELinux kernel before we have
561 assigned xattr values to the filesystem. */
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200562 if (!(root_inode->i_opflags & IOP_XATTR)) {
peter enderborgc103a912018-06-12 10:09:03 +0200563 pr_warn("SELinux: (dev %s, type %s) has no "
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800564 "xattr support\n", sb->s_id, sb->s_type->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 rc = -EOPNOTSUPP;
566 goto out;
567 }
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200568
569 rc = __vfs_getxattr(root, root_inode, XATTR_NAME_SELINUX, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 if (rc < 0 && rc != -ENODATA) {
571 if (rc == -EOPNOTSUPP)
peter enderborgc103a912018-06-12 10:09:03 +0200572 pr_warn("SELinux: (dev %s, type "
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800573 "%s) has no security xattr handler\n",
574 sb->s_id, sb->s_type->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 else
peter enderborgc103a912018-06-12 10:09:03 +0200576 pr_warn("SELinux: (dev %s, type "
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800577 "%s) getxattr errno %d\n", sb->s_id,
578 sb->s_type->name, -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 goto out;
580 }
581 }
582
Eric Pariseadcabc2012-08-24 15:59:14 -0400583 sbsec->flags |= SE_SBINITIALIZED;
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400584
585 /*
586 * Explicitly set or clear SBLABEL_MNT. It's not sufficient to simply
587 * leave the flag untouched because sb_clone_mnt_opts might be handing
588 * us a superblock that needs the flag to be cleared.
589 */
Eric Parisb43e7252012-10-10 14:27:35 -0400590 if (selinux_is_sblabel_mnt(sb))
Eric Paris12f348b2012-10-09 10:56:25 -0400591 sbsec->flags |= SBLABEL_MNT;
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400592 else
593 sbsec->flags &= ~SBLABEL_MNT;
David P. Quigleyddd29ec2009-09-09 14:25:37 -0400594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 /* Initialize the root inode. */
Eric Parisc9180a52007-11-30 13:00:35 -0500596 rc = inode_doinit_with_dentry(root_inode, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 /* Initialize any other inodes associated with the superblock, e.g.
599 inodes created prior to initial policy load or inodes created
600 during get_sb by a pseudo filesystem that directly
601 populates itself. */
602 spin_lock(&sbsec->isec_lock);
Al Viro8d641242018-12-10 15:34:12 -0500603 while (!list_empty(&sbsec->isec_head)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 struct inode_security_struct *isec =
Al Viro8d641242018-12-10 15:34:12 -0500605 list_first_entry(&sbsec->isec_head,
Eric Parisc9180a52007-11-30 13:00:35 -0500606 struct inode_security_struct, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 struct inode *inode = isec->inode;
Stephen Smalley923190d2014-10-06 16:32:52 -0400608 list_del_init(&isec->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 spin_unlock(&sbsec->isec_lock);
610 inode = igrab(inode);
611 if (inode) {
Eric Parisc9180a52007-11-30 13:00:35 -0500612 if (!IS_PRIVATE(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 inode_doinit(inode);
614 iput(inode);
615 }
616 spin_lock(&sbsec->isec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 }
618 spin_unlock(&sbsec->isec_lock);
619out:
Eric Parisc9180a52007-11-30 13:00:35 -0500620 return rc;
621}
622
Eric Parisc9180a52007-11-30 13:00:35 -0500623static int bad_option(struct superblock_security_struct *sbsec, char flag,
624 u32 old_sid, u32 new_sid)
625{
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500626 char mnt_flags = sbsec->flags & SE_MNTMASK;
627
Eric Parisc9180a52007-11-30 13:00:35 -0500628 /* check if the old mount command had the same options */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500629 if (sbsec->flags & SE_SBINITIALIZED)
Eric Parisc9180a52007-11-30 13:00:35 -0500630 if (!(sbsec->flags & flag) ||
631 (old_sid != new_sid))
632 return 1;
633
634 /* check if we were passed the same options twice,
635 * aka someone passed context=a,context=b
636 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500637 if (!(sbsec->flags & SE_SBINITIALIZED))
638 if (mnt_flags & flag)
Eric Parisc9180a52007-11-30 13:00:35 -0500639 return 1;
640 return 0;
641}
Eric Parise0007522008-03-05 10:31:54 -0500642
Al Virobd323652018-12-13 15:04:59 -0500643static int parse_sid(struct super_block *sb, const char *s, u32 *sid)
644{
645 int rc = security_context_str_to_sid(&selinux_state, s,
646 sid, GFP_KERNEL);
647 if (rc)
648 pr_warn("SELinux: security_context_str_to_sid"
649 "(%s) failed for (dev %s, type %s) errno=%d\n",
650 s, sb->s_id, sb->s_type->name, rc);
651 return rc;
652}
653
Eric Parisc9180a52007-11-30 13:00:35 -0500654/*
655 * Allow filesystems with binary mount data to explicitly set mount point
656 * labeling information.
657 */
Eric Parise0007522008-03-05 10:31:54 -0500658static int selinux_set_mnt_opts(struct super_block *sb,
Al Viro204cc0c2018-12-13 13:41:47 -0500659 void *mnt_opts,
David Quigley649f6e72013-05-22 12:50:36 -0400660 unsigned long kern_flags,
661 unsigned long *set_kern_flags)
Eric Parisc9180a52007-11-30 13:00:35 -0500662{
David Howells275bb412008-11-14 10:39:19 +1100663 const struct cred *cred = current_cred();
Eric Parisc9180a52007-11-30 13:00:35 -0500664 struct superblock_security_struct *sbsec = sb->s_security;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500665 struct dentry *root = sbsec->sb->s_root;
Al Virobd323652018-12-13 15:04:59 -0500666 struct selinux_mnt_opts *opts = mnt_opts;
Paul Moore2c971652016-04-19 16:36:28 -0400667 struct inode_security_struct *root_isec;
Eric Parisc9180a52007-11-30 13:00:35 -0500668 u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
669 u32 defcontext_sid = 0;
Al Virobd323652018-12-13 15:04:59 -0500670 int rc = 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500671
672 mutex_lock(&sbsec->lock);
673
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500674 if (!selinux_state.initialized) {
Al Virobd323652018-12-13 15:04:59 -0500675 if (!opts) {
Eric Parisc9180a52007-11-30 13:00:35 -0500676 /* Defer initialization until selinux_complete_init,
677 after the initial policy is loaded and the security
678 server is ready to handle calls. */
Eric Parisc9180a52007-11-30 13:00:35 -0500679 goto out;
680 }
681 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +0200682 pr_warn("SELinux: Unable to set superblock options "
Eric Paris744ba352008-04-17 11:52:44 -0400683 "before the security server is initialized\n");
Eric Parisc9180a52007-11-30 13:00:35 -0500684 goto out;
685 }
David Quigley649f6e72013-05-22 12:50:36 -0400686 if (kern_flags && !set_kern_flags) {
687 /* Specifying internal flags without providing a place to
688 * place the results is not allowed */
689 rc = -EINVAL;
690 goto out;
691 }
Eric Parisc9180a52007-11-30 13:00:35 -0500692
693 /*
Eric Parise0007522008-03-05 10:31:54 -0500694 * Binary mount data FS will come through this function twice. Once
695 * from an explicit call and once from the generic calls from the vfs.
696 * Since the generic VFS calls will not contain any security mount data
697 * we need to skip the double mount verification.
698 *
699 * This does open a hole in which we will not notice if the first
700 * mount using this sb set explict options and a second mount using
701 * this sb does not set any security options. (The first options
702 * will be used for both mounts)
703 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500704 if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
Al Virobd323652018-12-13 15:04:59 -0500705 && !opts)
Eric Parisf5269712008-05-14 11:27:45 -0400706 goto out;
Eric Parise0007522008-03-05 10:31:54 -0500707
Paul Moore2c971652016-04-19 16:36:28 -0400708 root_isec = backing_inode_security_novalidate(root);
709
Eric Parise0007522008-03-05 10:31:54 -0500710 /*
Eric Parisc9180a52007-11-30 13:00:35 -0500711 * parse the mount options, check if they are valid sids.
712 * also check if someone is trying to mount the same sb more
713 * than once with different security options.
714 */
Al Virobd323652018-12-13 15:04:59 -0500715 if (opts) {
716 if (opts->fscontext) {
717 rc = parse_sid(sb, opts->fscontext, &fscontext_sid);
718 if (rc)
719 goto out;
Eric Parisc9180a52007-11-30 13:00:35 -0500720 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
721 fscontext_sid))
722 goto out_double_mount;
Eric Parisc9180a52007-11-30 13:00:35 -0500723 sbsec->flags |= FSCONTEXT_MNT;
Al Virobd323652018-12-13 15:04:59 -0500724 }
725 if (opts->context) {
726 rc = parse_sid(sb, opts->context, &context_sid);
727 if (rc)
728 goto out;
Eric Parisc9180a52007-11-30 13:00:35 -0500729 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
730 context_sid))
731 goto out_double_mount;
Eric Parisc9180a52007-11-30 13:00:35 -0500732 sbsec->flags |= CONTEXT_MNT;
Al Virobd323652018-12-13 15:04:59 -0500733 }
734 if (opts->rootcontext) {
735 rc = parse_sid(sb, opts->rootcontext, &rootcontext_sid);
736 if (rc)
737 goto out;
Eric Parisc9180a52007-11-30 13:00:35 -0500738 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
739 rootcontext_sid))
740 goto out_double_mount;
Eric Parisc9180a52007-11-30 13:00:35 -0500741 sbsec->flags |= ROOTCONTEXT_MNT;
Al Virobd323652018-12-13 15:04:59 -0500742 }
743 if (opts->defcontext) {
744 rc = parse_sid(sb, opts->defcontext, &defcontext_sid);
745 if (rc)
746 goto out;
Eric Parisc9180a52007-11-30 13:00:35 -0500747 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
748 defcontext_sid))
749 goto out_double_mount;
Eric Parisc9180a52007-11-30 13:00:35 -0500750 sbsec->flags |= DEFCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500751 }
752 }
753
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500754 if (sbsec->flags & SE_SBINITIALIZED) {
Eric Parisc9180a52007-11-30 13:00:35 -0500755 /* previously mounted with options, but not on this attempt? */
Al Virobd323652018-12-13 15:04:59 -0500756 if ((sbsec->flags & SE_MNTMASK) && !opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500757 goto out_double_mount;
758 rc = 0;
759 goto out;
760 }
761
James Morris089be432008-07-15 18:32:49 +1000762 if (strcmp(sb->s_type->name, "proc") == 0)
Stephen Smalley134509d2015-06-04 16:22:17 -0400763 sbsec->flags |= SE_SBPROC | SE_SBGENFS;
764
Stephen Smalley8e014722015-06-04 16:22:17 -0400765 if (!strcmp(sb->s_type->name, "debugfs") ||
Jeff Vander Stoep6a391182017-06-20 09:35:33 -0700766 !strcmp(sb->s_type->name, "tracefs") ||
Stephen Smalley8e014722015-06-04 16:22:17 -0400767 !strcmp(sb->s_type->name, "sysfs") ||
Antonio Murdaca901ef842017-02-09 17:02:42 +0100768 !strcmp(sb->s_type->name, "pstore") ||
769 !strcmp(sb->s_type->name, "cgroup") ||
770 !strcmp(sb->s_type->name, "cgroup2"))
Stephen Smalley134509d2015-06-04 16:22:17 -0400771 sbsec->flags |= SE_SBGENFS;
Eric Parisc9180a52007-11-30 13:00:35 -0500772
David Quigleyeb9ae682013-05-22 12:50:37 -0400773 if (!sbsec->behavior) {
774 /*
775 * Determine the labeling behavior to use for this
776 * filesystem type.
777 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500778 rc = security_fs_use(&selinux_state, sb);
David Quigleyeb9ae682013-05-22 12:50:37 -0400779 if (rc) {
peter enderborgc103a912018-06-12 10:09:03 +0200780 pr_warn("%s: security_fs_use(%s) returned %d\n",
David Quigleyeb9ae682013-05-22 12:50:37 -0400781 __func__, sb->s_type->name, rc);
782 goto out;
783 }
Eric Parisc9180a52007-11-30 13:00:35 -0500784 }
Seth Forsheeaad82892016-04-26 14:36:20 -0500785
786 /*
Stephen Smalley01593d32017-01-09 10:07:31 -0500787 * If this is a user namespace mount and the filesystem type is not
788 * explicitly whitelisted, then no contexts are allowed on the command
789 * line and security labels must be ignored.
Seth Forsheeaad82892016-04-26 14:36:20 -0500790 */
Stephen Smalley01593d32017-01-09 10:07:31 -0500791 if (sb->s_user_ns != &init_user_ns &&
792 strcmp(sb->s_type->name, "tmpfs") &&
793 strcmp(sb->s_type->name, "ramfs") &&
794 strcmp(sb->s_type->name, "devpts")) {
Seth Forsheeaad82892016-04-26 14:36:20 -0500795 if (context_sid || fscontext_sid || rootcontext_sid ||
796 defcontext_sid) {
797 rc = -EACCES;
798 goto out;
799 }
800 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
801 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500802 rc = security_transition_sid(&selinux_state,
803 current_sid(),
804 current_sid(),
Seth Forsheeaad82892016-04-26 14:36:20 -0500805 SECCLASS_FILE, NULL,
806 &sbsec->mntpoint_sid);
807 if (rc)
808 goto out;
809 }
810 goto out_set_opts;
811 }
812
Eric Parisc9180a52007-11-30 13:00:35 -0500813 /* sets the context of the superblock for the fs being mounted. */
814 if (fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100815 rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500816 if (rc)
817 goto out;
818
819 sbsec->sid = fscontext_sid;
820 }
821
822 /*
823 * Switch to using mount point labeling behavior.
824 * sets the label used on all file below the mountpoint, and will set
825 * the superblock context if not already set.
826 */
David Quigleyeb9ae682013-05-22 12:50:37 -0400827 if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !context_sid) {
828 sbsec->behavior = SECURITY_FS_USE_NATIVE;
829 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
830 }
831
Eric Parisc9180a52007-11-30 13:00:35 -0500832 if (context_sid) {
833 if (!fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100834 rc = may_context_mount_sb_relabel(context_sid, sbsec,
835 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500836 if (rc)
837 goto out;
838 sbsec->sid = context_sid;
839 } else {
David Howells275bb412008-11-14 10:39:19 +1100840 rc = may_context_mount_inode_relabel(context_sid, sbsec,
841 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500842 if (rc)
843 goto out;
844 }
845 if (!rootcontext_sid)
846 rootcontext_sid = context_sid;
847
848 sbsec->mntpoint_sid = context_sid;
849 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
850 }
851
852 if (rootcontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100853 rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec,
854 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500855 if (rc)
856 goto out;
857
858 root_isec->sid = rootcontext_sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -0500859 root_isec->initialized = LABEL_INITIALIZED;
Eric Parisc9180a52007-11-30 13:00:35 -0500860 }
861
862 if (defcontext_sid) {
David Quigleyeb9ae682013-05-22 12:50:37 -0400863 if (sbsec->behavior != SECURITY_FS_USE_XATTR &&
864 sbsec->behavior != SECURITY_FS_USE_NATIVE) {
Eric Parisc9180a52007-11-30 13:00:35 -0500865 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +0200866 pr_warn("SELinux: defcontext option is "
Eric Parisc9180a52007-11-30 13:00:35 -0500867 "invalid for this filesystem type\n");
868 goto out;
869 }
870
871 if (defcontext_sid != sbsec->def_sid) {
872 rc = may_context_mount_inode_relabel(defcontext_sid,
David Howells275bb412008-11-14 10:39:19 +1100873 sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500874 if (rc)
875 goto out;
876 }
877
878 sbsec->def_sid = defcontext_sid;
879 }
880
Seth Forsheeaad82892016-04-26 14:36:20 -0500881out_set_opts:
Eric Parisc9180a52007-11-30 13:00:35 -0500882 rc = sb_finish_set_opts(sb);
883out:
Eric Parisbc7e9822006-09-25 23:32:02 -0700884 mutex_unlock(&sbsec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 return rc;
Eric Parisc9180a52007-11-30 13:00:35 -0500886out_double_mount:
887 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +0200888 pr_warn("SELinux: mount invalid. Same superblock, different "
Al Virobd323652018-12-13 15:04:59 -0500889 "security settings for (dev %s, type %s)\n", sb->s_id,
890 sb->s_type->name);
Eric Parisc9180a52007-11-30 13:00:35 -0500891 goto out;
892}
893
Jeff Layton094f7b62013-04-01 08:14:24 -0400894static int selinux_cmp_sb_context(const struct super_block *oldsb,
895 const struct super_block *newsb)
896{
897 struct superblock_security_struct *old = oldsb->s_security;
898 struct superblock_security_struct *new = newsb->s_security;
899 char oldflags = old->flags & SE_MNTMASK;
900 char newflags = new->flags & SE_MNTMASK;
901
902 if (oldflags != newflags)
903 goto mismatch;
904 if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid)
905 goto mismatch;
906 if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid)
907 goto mismatch;
908 if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid)
909 goto mismatch;
910 if (oldflags & ROOTCONTEXT_MNT) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500911 struct inode_security_struct *oldroot = backing_inode_security(oldsb->s_root);
912 struct inode_security_struct *newroot = backing_inode_security(newsb->s_root);
Jeff Layton094f7b62013-04-01 08:14:24 -0400913 if (oldroot->sid != newroot->sid)
914 goto mismatch;
915 }
916 return 0;
917mismatch:
peter enderborgc103a912018-06-12 10:09:03 +0200918 pr_warn("SELinux: mount invalid. Same superblock, "
Jeff Layton094f7b62013-04-01 08:14:24 -0400919 "different security settings for (dev %s, "
920 "type %s)\n", newsb->s_id, newsb->s_type->name);
921 return -EBUSY;
922}
923
924static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400925 struct super_block *newsb,
926 unsigned long kern_flags,
927 unsigned long *set_kern_flags)
Eric Parisc9180a52007-11-30 13:00:35 -0500928{
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400929 int rc = 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500930 const struct superblock_security_struct *oldsbsec = oldsb->s_security;
931 struct superblock_security_struct *newsbsec = newsb->s_security;
932
933 int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT);
934 int set_context = (oldsbsec->flags & CONTEXT_MNT);
935 int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT);
936
Eric Paris0f5e6422008-04-21 16:24:11 -0400937 /*
938 * if the parent was able to be mounted it clearly had no special lsm
Al Viroe8c26252010-03-23 06:36:54 -0400939 * mount options. thus we can safely deal with this superblock later
Eric Paris0f5e6422008-04-21 16:24:11 -0400940 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500941 if (!selinux_state.initialized)
Jeff Layton094f7b62013-04-01 08:14:24 -0400942 return 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500943
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400944 /*
945 * Specifying internal flags without providing a place to
946 * place the results is not allowed.
947 */
948 if (kern_flags && !set_kern_flags)
949 return -EINVAL;
950
Eric Parisc9180a52007-11-30 13:00:35 -0500951 /* how can we clone if the old one wasn't set up?? */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500952 BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED));
Eric Parisc9180a52007-11-30 13:00:35 -0500953
Jeff Layton094f7b62013-04-01 08:14:24 -0400954 /* if fs is reusing a sb, make sure that the contexts match */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500955 if (newsbsec->flags & SE_SBINITIALIZED)
Jeff Layton094f7b62013-04-01 08:14:24 -0400956 return selinux_cmp_sb_context(oldsb, newsb);
Eric Paris5a552612008-04-09 14:08:35 -0400957
Eric Parisc9180a52007-11-30 13:00:35 -0500958 mutex_lock(&newsbsec->lock);
959
960 newsbsec->flags = oldsbsec->flags;
961
962 newsbsec->sid = oldsbsec->sid;
963 newsbsec->def_sid = oldsbsec->def_sid;
964 newsbsec->behavior = oldsbsec->behavior;
965
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400966 if (newsbsec->behavior == SECURITY_FS_USE_NATIVE &&
967 !(kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500968 rc = security_fs_use(&selinux_state, newsb);
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400969 if (rc)
970 goto out;
971 }
972
973 if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !set_context) {
974 newsbsec->behavior = SECURITY_FS_USE_NATIVE;
975 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
976 }
977
Eric Parisc9180a52007-11-30 13:00:35 -0500978 if (set_context) {
979 u32 sid = oldsbsec->mntpoint_sid;
980
981 if (!set_fscontext)
982 newsbsec->sid = sid;
983 if (!set_rootcontext) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500984 struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
Eric Parisc9180a52007-11-30 13:00:35 -0500985 newisec->sid = sid;
986 }
987 newsbsec->mntpoint_sid = sid;
988 }
989 if (set_rootcontext) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500990 const struct inode_security_struct *oldisec = backing_inode_security(oldsb->s_root);
991 struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
Eric Parisc9180a52007-11-30 13:00:35 -0500992
993 newisec->sid = oldisec->sid;
994 }
995
996 sb_finish_set_opts(newsb);
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400997out:
Eric Parisc9180a52007-11-30 13:00:35 -0500998 mutex_unlock(&newsbsec->lock);
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400999 return rc;
Eric Parisc9180a52007-11-30 13:00:35 -05001000}
1001
Al Viroba641862018-12-14 20:28:15 -05001002static int selinux_add_opt(int token, const char *s, void **mnt_opts)
Eric Parisc9180a52007-11-30 13:00:35 -05001003{
Al Viroba641862018-12-14 20:28:15 -05001004 struct selinux_mnt_opts *opts = *mnt_opts;
Eric Parisc9180a52007-11-30 13:00:35 -05001005
Al Viroda3d76a2018-12-17 10:14:16 -05001006 if (token == Opt_seclabel) /* eaten and completely ignored */
Al Viro169d68efb2018-12-14 22:44:50 -05001007 return 0;
Eric Parisc9180a52007-11-30 13:00:35 -05001008
Al Viroba641862018-12-14 20:28:15 -05001009 if (!opts) {
1010 opts = kzalloc(sizeof(struct selinux_mnt_opts), GFP_KERNEL);
1011 if (!opts)
1012 return -ENOMEM;
1013 *mnt_opts = opts;
1014 }
1015 if (!s)
1016 return -ENOMEM;
1017 switch (token) {
1018 case Opt_context:
1019 if (opts->context || opts->defcontext)
1020 goto Einval;
1021 opts->context = s;
1022 break;
1023 case Opt_fscontext:
1024 if (opts->fscontext)
1025 goto Einval;
1026 opts->fscontext = s;
1027 break;
1028 case Opt_rootcontext:
1029 if (opts->rootcontext)
1030 goto Einval;
1031 opts->rootcontext = s;
1032 break;
1033 case Opt_defcontext:
1034 if (opts->context || opts->defcontext)
1035 goto Einval;
1036 opts->defcontext = s;
1037 break;
1038 }
1039 return 0;
1040Einval:
1041 pr_warn(SEL_MOUNT_FAIL_MSG);
Al Viroba641862018-12-14 20:28:15 -05001042 return -EINVAL;
1043}
Eric Parisc9180a52007-11-30 13:00:35 -05001044
Al Viro757cbe52018-12-14 23:42:21 -05001045static int selinux_add_mnt_opt(const char *option, const char *val, int len,
1046 void **mnt_opts)
Eric Parisc9180a52007-11-30 13:00:35 -05001047{
Al Viro757cbe52018-12-14 23:42:21 -05001048 int token = Opt_error;
1049 int rc, i;
Eric Parisc9180a52007-11-30 13:00:35 -05001050
Al Viro757cbe52018-12-14 23:42:21 -05001051 for (i = 0; i < ARRAY_SIZE(tokens); i++) {
1052 if (strcmp(option, tokens[i].name) == 0) {
1053 token = tokens[i].opt;
Eric Parisc9180a52007-11-30 13:00:35 -05001054 break;
Eric Parisc9180a52007-11-30 13:00:35 -05001055 }
1056 }
1057
Al Viro757cbe52018-12-14 23:42:21 -05001058 if (token == Opt_error)
1059 return -EINVAL;
Eric Parise0007522008-03-05 10:31:54 -05001060
Al Viro757cbe52018-12-14 23:42:21 -05001061 if (token != Opt_seclabel)
1062 val = kmemdup_nul(val, len, GFP_KERNEL);
1063 rc = selinux_add_opt(token, val, mnt_opts);
1064 if (unlikely(rc)) {
1065 kfree(val);
1066 if (*mnt_opts) {
1067 selinux_free_mnt_opts(*mnt_opts);
1068 *mnt_opts = NULL;
1069 }
1070 }
1071 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Al Viroe3489f82018-12-13 00:24:36 -05001074static int show_sid(struct seq_file *m, u32 sid)
Eric Paris2069f452008-07-04 09:47:13 +10001075{
Al Viroe3489f82018-12-13 00:24:36 -05001076 char *context = NULL;
1077 u32 len;
1078 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Al Viroe3489f82018-12-13 00:24:36 -05001080 rc = security_sid_to_context(&selinux_state, sid,
1081 &context, &len);
1082 if (!rc) {
1083 bool has_comma = context && strchr(context, ',');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Eric Paris2069f452008-07-04 09:47:13 +10001085 if (has_comma)
1086 seq_putc(m, '\"');
Al Viroe3489f82018-12-13 00:24:36 -05001087 seq_escape(m, context, "\"\n\\");
Eric Paris2069f452008-07-04 09:47:13 +10001088 if (has_comma)
1089 seq_putc(m, '\"');
1090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 kfree(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 return rc;
1093}
Eric Paris2069f452008-07-04 09:47:13 +10001094
1095static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
1096{
Al Viroe3489f82018-12-13 00:24:36 -05001097 struct superblock_security_struct *sbsec = sb->s_security;
Eric Paris2069f452008-07-04 09:47:13 +10001098 int rc;
1099
Al Viroe3489f82018-12-13 00:24:36 -05001100 if (!(sbsec->flags & SE_SBINITIALIZED))
1101 return 0;
1102
1103 if (!selinux_state.initialized)
1104 return 0;
1105
1106 if (sbsec->flags & FSCONTEXT_MNT) {
1107 seq_putc(m, ',');
1108 seq_puts(m, FSCONTEXT_STR);
1109 rc = show_sid(m, sbsec->sid);
1110 if (rc)
1111 return rc;
Eric Paris383795c2008-07-29 17:07:26 -04001112 }
Al Viroe3489f82018-12-13 00:24:36 -05001113 if (sbsec->flags & CONTEXT_MNT) {
1114 seq_putc(m, ',');
1115 seq_puts(m, CONTEXT_STR);
1116 rc = show_sid(m, sbsec->mntpoint_sid);
1117 if (rc)
1118 return rc;
1119 }
1120 if (sbsec->flags & DEFCONTEXT_MNT) {
1121 seq_putc(m, ',');
1122 seq_puts(m, DEFCONTEXT_STR);
1123 rc = show_sid(m, sbsec->def_sid);
1124 if (rc)
1125 return rc;
1126 }
1127 if (sbsec->flags & ROOTCONTEXT_MNT) {
1128 struct dentry *root = sbsec->sb->s_root;
1129 struct inode_security_struct *isec = backing_inode_security(root);
1130 seq_putc(m, ',');
1131 seq_puts(m, ROOTCONTEXT_STR);
1132 rc = show_sid(m, isec->sid);
1133 if (rc)
1134 return rc;
1135 }
1136 if (sbsec->flags & SBLABEL_MNT) {
1137 seq_putc(m, ',');
1138 seq_puts(m, LABELSUPP_STR);
1139 }
1140 return 0;
Eric Paris2069f452008-07-04 09:47:13 +10001141}
1142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143static inline u16 inode_mode_to_security_class(umode_t mode)
1144{
1145 switch (mode & S_IFMT) {
1146 case S_IFSOCK:
1147 return SECCLASS_SOCK_FILE;
1148 case S_IFLNK:
1149 return SECCLASS_LNK_FILE;
1150 case S_IFREG:
1151 return SECCLASS_FILE;
1152 case S_IFBLK:
1153 return SECCLASS_BLK_FILE;
1154 case S_IFDIR:
1155 return SECCLASS_DIR;
1156 case S_IFCHR:
1157 return SECCLASS_CHR_FILE;
1158 case S_IFIFO:
1159 return SECCLASS_FIFO_FILE;
1160
1161 }
1162
1163 return SECCLASS_FILE;
1164}
1165
James Morris13402582005-09-30 14:24:34 -04001166static inline int default_protocol_stream(int protocol)
1167{
1168 return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP);
1169}
1170
1171static inline int default_protocol_dgram(int protocol)
1172{
1173 return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
1174}
1175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176static inline u16 socket_type_to_security_class(int family, int type, int protocol)
1177{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001178 int extsockclass = selinux_policycap_extsockclass();
Stephen Smalleyda69a532017-01-09 10:07:30 -05001179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 switch (family) {
1181 case PF_UNIX:
1182 switch (type) {
1183 case SOCK_STREAM:
1184 case SOCK_SEQPACKET:
1185 return SECCLASS_UNIX_STREAM_SOCKET;
1186 case SOCK_DGRAM:
Luis Ressel2a764b52017-07-25 15:13:41 -04001187 case SOCK_RAW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 return SECCLASS_UNIX_DGRAM_SOCKET;
1189 }
1190 break;
1191 case PF_INET:
1192 case PF_INET6:
1193 switch (type) {
1194 case SOCK_STREAM:
Stephen Smalleyda69a532017-01-09 10:07:30 -05001195 case SOCK_SEQPACKET:
James Morris13402582005-09-30 14:24:34 -04001196 if (default_protocol_stream(protocol))
1197 return SECCLASS_TCP_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001198 else if (extsockclass && protocol == IPPROTO_SCTP)
1199 return SECCLASS_SCTP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001200 else
1201 return SECCLASS_RAWIP_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 case SOCK_DGRAM:
James Morris13402582005-09-30 14:24:34 -04001203 if (default_protocol_dgram(protocol))
1204 return SECCLASS_UDP_SOCKET;
Stephen Smalleyef379792017-01-09 10:07:31 -05001205 else if (extsockclass && (protocol == IPPROTO_ICMP ||
1206 protocol == IPPROTO_ICMPV6))
Stephen Smalleyda69a532017-01-09 10:07:30 -05001207 return SECCLASS_ICMP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001208 else
1209 return SECCLASS_RAWIP_SOCKET;
James Morris2ee92d42006-11-13 16:09:01 -08001210 case SOCK_DCCP:
1211 return SECCLASS_DCCP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001212 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 return SECCLASS_RAWIP_SOCKET;
1214 }
1215 break;
1216 case PF_NETLINK:
1217 switch (protocol) {
1218 case NETLINK_ROUTE:
1219 return SECCLASS_NETLINK_ROUTE_SOCKET;
Pavel Emelyanov7f1fb602011-12-06 07:56:43 +00001220 case NETLINK_SOCK_DIAG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 return SECCLASS_NETLINK_TCPDIAG_SOCKET;
1222 case NETLINK_NFLOG:
1223 return SECCLASS_NETLINK_NFLOG_SOCKET;
1224 case NETLINK_XFRM:
1225 return SECCLASS_NETLINK_XFRM_SOCKET;
1226 case NETLINK_SELINUX:
1227 return SECCLASS_NETLINK_SELINUX_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001228 case NETLINK_ISCSI:
1229 return SECCLASS_NETLINK_ISCSI_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 case NETLINK_AUDIT:
1231 return SECCLASS_NETLINK_AUDIT_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001232 case NETLINK_FIB_LOOKUP:
1233 return SECCLASS_NETLINK_FIB_LOOKUP_SOCKET;
1234 case NETLINK_CONNECTOR:
1235 return SECCLASS_NETLINK_CONNECTOR_SOCKET;
1236 case NETLINK_NETFILTER:
1237 return SECCLASS_NETLINK_NETFILTER_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 case NETLINK_DNRTMSG:
1239 return SECCLASS_NETLINK_DNRT_SOCKET;
James Morris0c9b7942005-04-16 15:24:13 -07001240 case NETLINK_KOBJECT_UEVENT:
1241 return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001242 case NETLINK_GENERIC:
1243 return SECCLASS_NETLINK_GENERIC_SOCKET;
1244 case NETLINK_SCSITRANSPORT:
1245 return SECCLASS_NETLINK_SCSITRANSPORT_SOCKET;
1246 case NETLINK_RDMA:
1247 return SECCLASS_NETLINK_RDMA_SOCKET;
1248 case NETLINK_CRYPTO:
1249 return SECCLASS_NETLINK_CRYPTO_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 default:
1251 return SECCLASS_NETLINK_SOCKET;
1252 }
1253 case PF_PACKET:
1254 return SECCLASS_PACKET_SOCKET;
1255 case PF_KEY:
1256 return SECCLASS_KEY_SOCKET;
Christopher J. PeBenito3e3ff152006-06-09 00:25:03 -07001257 case PF_APPLETALK:
1258 return SECCLASS_APPLETALK_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 }
1260
Stephen Smalleyda69a532017-01-09 10:07:30 -05001261 if (extsockclass) {
1262 switch (family) {
1263 case PF_AX25:
1264 return SECCLASS_AX25_SOCKET;
1265 case PF_IPX:
1266 return SECCLASS_IPX_SOCKET;
1267 case PF_NETROM:
1268 return SECCLASS_NETROM_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001269 case PF_ATMPVC:
1270 return SECCLASS_ATMPVC_SOCKET;
1271 case PF_X25:
1272 return SECCLASS_X25_SOCKET;
1273 case PF_ROSE:
1274 return SECCLASS_ROSE_SOCKET;
1275 case PF_DECnet:
1276 return SECCLASS_DECNET_SOCKET;
1277 case PF_ATMSVC:
1278 return SECCLASS_ATMSVC_SOCKET;
1279 case PF_RDS:
1280 return SECCLASS_RDS_SOCKET;
1281 case PF_IRDA:
1282 return SECCLASS_IRDA_SOCKET;
1283 case PF_PPPOX:
1284 return SECCLASS_PPPOX_SOCKET;
1285 case PF_LLC:
1286 return SECCLASS_LLC_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001287 case PF_CAN:
1288 return SECCLASS_CAN_SOCKET;
1289 case PF_TIPC:
1290 return SECCLASS_TIPC_SOCKET;
1291 case PF_BLUETOOTH:
1292 return SECCLASS_BLUETOOTH_SOCKET;
1293 case PF_IUCV:
1294 return SECCLASS_IUCV_SOCKET;
1295 case PF_RXRPC:
1296 return SECCLASS_RXRPC_SOCKET;
1297 case PF_ISDN:
1298 return SECCLASS_ISDN_SOCKET;
1299 case PF_PHONET:
1300 return SECCLASS_PHONET_SOCKET;
1301 case PF_IEEE802154:
1302 return SECCLASS_IEEE802154_SOCKET;
1303 case PF_CAIF:
1304 return SECCLASS_CAIF_SOCKET;
1305 case PF_ALG:
1306 return SECCLASS_ALG_SOCKET;
1307 case PF_NFC:
1308 return SECCLASS_NFC_SOCKET;
1309 case PF_VSOCK:
1310 return SECCLASS_VSOCK_SOCKET;
1311 case PF_KCM:
1312 return SECCLASS_KCM_SOCKET;
1313 case PF_QIPCRTR:
1314 return SECCLASS_QIPCRTR_SOCKET;
Linus Torvalds3051bf32017-02-22 10:15:09 -08001315 case PF_SMC:
1316 return SECCLASS_SMC_SOCKET;
Björn Töpel68e8b842018-05-02 13:01:22 +02001317 case PF_XDP:
1318 return SECCLASS_XDP_SOCKET;
1319#if PF_MAX > 45
Stephen Smalleyda69a532017-01-09 10:07:30 -05001320#error New address family defined, please update this function.
1321#endif
1322 }
1323 }
1324
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 return SECCLASS_SOCKET;
1326}
1327
Stephen Smalley134509d2015-06-04 16:22:17 -04001328static int selinux_genfs_get_sid(struct dentry *dentry,
1329 u16 tclass,
1330 u16 flags,
1331 u32 *sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332{
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001333 int rc;
Al Virofc640052016-04-10 01:33:30 -04001334 struct super_block *sb = dentry->d_sb;
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001335 char *buffer, *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
Eric Paris828dfe12008-04-17 13:17:49 -04001337 buffer = (char *)__get_free_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 if (!buffer)
1339 return -ENOMEM;
1340
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001341 path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
1342 if (IS_ERR(path))
1343 rc = PTR_ERR(path);
1344 else {
Stephen Smalley134509d2015-06-04 16:22:17 -04001345 if (flags & SE_SBPROC) {
1346 /* each process gets a /proc/PID/ entry. Strip off the
1347 * PID part to get a valid selinux labeling.
1348 * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
1349 while (path[1] >= '0' && path[1] <= '9') {
1350 path[1] = '/';
1351 path++;
1352 }
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001353 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001354 rc = security_genfs_sid(&selinux_state, sb->s_type->name,
1355 path, tclass, sid);
Stephen Smalley7bb185e2018-09-04 16:51:36 -04001356 if (rc == -ENOENT) {
1357 /* No match in policy, mark as unlabeled. */
1358 *sid = SECINITSID_UNLABELED;
1359 rc = 0;
1360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 free_page((unsigned long)buffer);
1363 return rc;
1364}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
1366/* The inode's security attributes must be initialized before first use. */
1367static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
1368{
1369 struct superblock_security_struct *sbsec = NULL;
1370 struct inode_security_struct *isec = inode->i_security;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001371 u32 task_sid, sid = 0;
1372 u16 sclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 struct dentry *dentry;
1374#define INITCONTEXTLEN 255
1375 char *context = NULL;
1376 unsigned len = 0;
1377 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05001379 if (isec->initialized == LABEL_INITIALIZED)
Andreas Gruenbacher13457d02016-11-10 22:18:29 +01001380 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001382 spin_lock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05001383 if (isec->initialized == LABEL_INITIALIZED)
Eric Paris23970742006-09-25 23:32:01 -07001384 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Andreas Gruenbacher13457d02016-11-10 22:18:29 +01001386 if (isec->sclass == SECCLASS_FILE)
1387 isec->sclass = inode_mode_to_security_class(inode->i_mode);
1388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 sbsec = inode->i_sb->s_security;
David P. Quigley0d90a7e2009-01-16 09:22:02 -05001390 if (!(sbsec->flags & SE_SBINITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 /* Defer initialization until selinux_complete_init,
1392 after the initial policy is loaded and the security
1393 server is ready to handle calls. */
1394 spin_lock(&sbsec->isec_lock);
1395 if (list_empty(&isec->list))
1396 list_add(&isec->list, &sbsec->isec_head);
1397 spin_unlock(&sbsec->isec_lock);
Eric Paris23970742006-09-25 23:32:01 -07001398 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 }
1400
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001401 sclass = isec->sclass;
1402 task_sid = isec->task_sid;
1403 sid = isec->sid;
1404 isec->initialized = LABEL_PENDING;
1405 spin_unlock(&isec->lock);
1406
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 switch (sbsec->behavior) {
David Quigleyeb9ae682013-05-22 12:50:37 -04001408 case SECURITY_FS_USE_NATIVE:
1409 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 case SECURITY_FS_USE_XATTR:
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001411 if (!(inode->i_opflags & IOP_XATTR)) {
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001412 sid = sbsec->def_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 break;
1414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 /* Need a dentry, since the xattr API requires one.
1416 Life would be simpler if we could just pass the inode. */
1417 if (opt_dentry) {
1418 /* Called from d_instantiate or d_splice_alias. */
1419 dentry = dget(opt_dentry);
1420 } else {
Al Virob1271252018-04-25 10:28:38 -04001421 /*
1422 * Called from selinux_complete_init, try to find a dentry.
1423 * Some filesystems really want a connected one, so try
1424 * that first. We could split SECURITY_FS_USE_XATTR in
1425 * two, depending upon that...
1426 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 dentry = d_find_alias(inode);
Al Virob1271252018-04-25 10:28:38 -04001428 if (!dentry)
1429 dentry = d_find_any_alias(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 }
1431 if (!dentry) {
Eric Parisdf7f54c2009-03-09 14:35:58 -04001432 /*
1433 * this is can be hit on boot when a file is accessed
1434 * before the policy is loaded. When we load policy we
1435 * may find inodes that have no dentry on the
1436 * sbsec->isec_head list. No reason to complain as these
1437 * will get fixed up the next time we go through
1438 * inode_doinit with a dentry, before these inodes could
1439 * be used again by userspace.
1440 */
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001441 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 }
1443
1444 len = INITCONTEXTLEN;
Eric Paris4cb912f2009-02-12 14:50:05 -05001445 context = kmalloc(len+1, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 if (!context) {
1447 rc = -ENOMEM;
1448 dput(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001449 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 }
Eric Paris4cb912f2009-02-12 14:50:05 -05001451 context[len] = '\0';
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001452 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 if (rc == -ERANGE) {
James Morris314dabb2009-08-10 22:00:13 +10001454 kfree(context);
1455
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 /* Need a larger buffer. Query for the right size. */
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001457 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 if (rc < 0) {
1459 dput(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001460 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 len = rc;
Eric Paris4cb912f2009-02-12 14:50:05 -05001463 context = kmalloc(len+1, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 if (!context) {
1465 rc = -ENOMEM;
1466 dput(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001467 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 }
Eric Paris4cb912f2009-02-12 14:50:05 -05001469 context[len] = '\0';
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001470 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 }
1472 dput(dentry);
1473 if (rc < 0) {
1474 if (rc != -ENODATA) {
peter enderborgc103a912018-06-12 10:09:03 +02001475 pr_warn("SELinux: %s: getxattr returned "
Harvey Harrisondd6f9532008-03-06 10:03:59 +11001476 "%d for dev=%s ino=%ld\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 -rc, inode->i_sb->s_id, inode->i_ino);
1478 kfree(context);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001479 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 }
1481 /* Map ENODATA to the default file SID */
1482 sid = sbsec->def_sid;
1483 rc = 0;
1484 } else {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001485 rc = security_context_to_sid_default(&selinux_state,
1486 context, rc, &sid,
Stephen Smalley869ab512008-04-04 08:46:05 -04001487 sbsec->def_sid,
1488 GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 if (rc) {
Eric Paris4ba0a8a2009-02-12 15:01:10 -05001490 char *dev = inode->i_sb->s_id;
1491 unsigned long ino = inode->i_ino;
1492
1493 if (rc == -EINVAL) {
1494 if (printk_ratelimit())
peter enderborgc103a912018-06-12 10:09:03 +02001495 pr_notice("SELinux: inode=%lu on dev=%s was found to have an invalid "
Eric Paris4ba0a8a2009-02-12 15:01:10 -05001496 "context=%s. This indicates you may need to relabel the inode or the "
1497 "filesystem in question.\n", ino, dev, context);
1498 } else {
peter enderborgc103a912018-06-12 10:09:03 +02001499 pr_warn("SELinux: %s: context_to_sid(%s) "
Eric Paris4ba0a8a2009-02-12 15:01:10 -05001500 "returned %d for dev=%s ino=%ld\n",
1501 __func__, context, -rc, dev, ino);
1502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 kfree(context);
1504 /* Leave with the unlabeled SID */
1505 rc = 0;
1506 break;
1507 }
1508 }
1509 kfree(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 break;
1511 case SECURITY_FS_USE_TASK:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001512 sid = task_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 break;
1514 case SECURITY_FS_USE_TRANS:
1515 /* Default to the fs SID. */
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001516 sid = sbsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
1518 /* Try to obtain a transition SID. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001519 rc = security_transition_sid(&selinux_state, task_sid, sid,
1520 sclass, NULL, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 if (rc)
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001522 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 break;
Eric Parisc312feb2006-07-10 04:43:53 -07001524 case SECURITY_FS_USE_MNTPOINT:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001525 sid = sbsec->mntpoint_sid;
Eric Parisc312feb2006-07-10 04:43:53 -07001526 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 default:
Eric Parisc312feb2006-07-10 04:43:53 -07001528 /* Default to the fs superblock SID. */
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001529 sid = sbsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
Stephen Smalley134509d2015-06-04 16:22:17 -04001531 if ((sbsec->flags & SE_SBGENFS) && !S_ISLNK(inode->i_mode)) {
Paul Mooref64410e2014-03-19 16:46:18 -04001532 /* We must have a dentry to determine the label on
1533 * procfs inodes */
Al Virob1271252018-04-25 10:28:38 -04001534 if (opt_dentry) {
Paul Mooref64410e2014-03-19 16:46:18 -04001535 /* Called from d_instantiate or
1536 * d_splice_alias. */
1537 dentry = dget(opt_dentry);
Al Virob1271252018-04-25 10:28:38 -04001538 } else {
Paul Mooref64410e2014-03-19 16:46:18 -04001539 /* Called from selinux_complete_init, try to
Al Virob1271252018-04-25 10:28:38 -04001540 * find a dentry. Some filesystems really want
1541 * a connected one, so try that first.
1542 */
Paul Mooref64410e2014-03-19 16:46:18 -04001543 dentry = d_find_alias(inode);
Al Virob1271252018-04-25 10:28:38 -04001544 if (!dentry)
1545 dentry = d_find_any_alias(inode);
1546 }
Paul Mooref64410e2014-03-19 16:46:18 -04001547 /*
1548 * This can be hit on boot when a file is accessed
1549 * before the policy is loaded. When we load policy we
1550 * may find inodes that have no dentry on the
1551 * sbsec->isec_head list. No reason to complain as
1552 * these will get fixed up the next time we go through
1553 * inode_doinit() with a dentry, before these inodes
1554 * could be used again by userspace.
1555 */
1556 if (!dentry)
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001557 goto out;
1558 rc = selinux_genfs_get_sid(dentry, sclass,
Stephen Smalley134509d2015-06-04 16:22:17 -04001559 sbsec->flags, &sid);
Paul Mooref64410e2014-03-19 16:46:18 -04001560 dput(dentry);
1561 if (rc)
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001562 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 }
1564 break;
1565 }
1566
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001567out:
1568 spin_lock(&isec->lock);
1569 if (isec->initialized == LABEL_PENDING) {
1570 if (!sid || rc) {
1571 isec->initialized = LABEL_INVALID;
1572 goto out_unlock;
1573 }
1574
1575 isec->initialized = LABEL_INITIALIZED;
1576 isec->sid = sid;
1577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
Eric Paris23970742006-09-25 23:32:01 -07001579out_unlock:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001580 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 return rc;
1582}
1583
1584/* Convert a Linux signal to an access vector. */
1585static inline u32 signal_to_av(int sig)
1586{
1587 u32 perm = 0;
1588
1589 switch (sig) {
1590 case SIGCHLD:
1591 /* Commonly granted from child to parent. */
1592 perm = PROCESS__SIGCHLD;
1593 break;
1594 case SIGKILL:
1595 /* Cannot be caught or ignored */
1596 perm = PROCESS__SIGKILL;
1597 break;
1598 case SIGSTOP:
1599 /* Cannot be caught or ignored */
1600 perm = PROCESS__SIGSTOP;
1601 break;
1602 default:
1603 /* All other signals. */
1604 perm = PROCESS__SIGNAL;
1605 break;
1606 }
1607
1608 return perm;
1609}
1610
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001611#if CAP_LAST_CAP > 63
1612#error Fix SELinux to handle capabilities > 63.
1613#endif
1614
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615/* Check whether a task is allowed to use a capability. */
Eric Paris6a9de492012-01-03 12:25:14 -05001616static int cred_has_capability(const struct cred *cred,
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04001617 int cap, int audit, bool initns)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618{
Thomas Liu2bf49692009-07-14 12:14:09 -04001619 struct common_audit_data ad;
Eric Paris06112162008-11-11 22:02:50 +11001620 struct av_decision avd;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001621 u16 sclass;
David Howells3699c532009-01-06 22:27:01 +00001622 u32 sid = cred_sid(cred);
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001623 u32 av = CAP_TO_MASK(cap);
Eric Paris06112162008-11-11 22:02:50 +11001624 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
Eric Paris50c205f2012-04-04 15:01:43 -04001626 ad.type = LSM_AUDIT_DATA_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 ad.u.cap = cap;
1628
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001629 switch (CAP_TO_INDEX(cap)) {
1630 case 0:
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04001631 sclass = initns ? SECCLASS_CAPABILITY : SECCLASS_CAP_USERNS;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001632 break;
1633 case 1:
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04001634 sclass = initns ? SECCLASS_CAPABILITY2 : SECCLASS_CAP2_USERNS;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001635 break;
1636 default:
peter enderborgc103a912018-06-12 10:09:03 +02001637 pr_err("SELinux: out of range capability %d\n", cap);
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001638 BUG();
Eric Parisa35c6c832011-04-20 10:21:28 -04001639 return -EINVAL;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001640 }
Eric Paris06112162008-11-11 22:02:50 +11001641
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001642 rc = avc_has_perm_noaudit(&selinux_state,
1643 sid, sid, sclass, av, 0, &avd);
Eric Paris9ade0cf2011-04-25 16:26:29 -04001644 if (audit == SECURITY_CAP_AUDIT) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001645 int rc2 = avc_audit(&selinux_state,
1646 sid, sid, sclass, av, &avd, rc, &ad, 0);
Eric Paris9ade0cf2011-04-25 16:26:29 -04001647 if (rc2)
1648 return rc2;
1649 }
Eric Paris06112162008-11-11 22:02:50 +11001650 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651}
1652
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653/* Check whether a task has a particular permission to an inode.
1654 The 'adp' parameter is optional and allows other audit
1655 data to be passed (e.g. the dentry). */
David Howells88e67f32008-11-14 10:39:21 +11001656static int inode_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 struct inode *inode,
1658 u32 perms,
Linus Torvalds19e49832013-10-04 12:54:11 -07001659 struct common_audit_data *adp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 struct inode_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +11001662 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
David Howellse0e81732009-09-02 09:13:40 +01001664 validate_creds(cred);
1665
Eric Paris828dfe12008-04-17 13:17:49 -04001666 if (unlikely(IS_PRIVATE(inode)))
Stephen Smalleybbaca6c2007-02-14 00:34:16 -08001667 return 0;
1668
David Howells88e67f32008-11-14 10:39:21 +11001669 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 isec = inode->i_security;
1671
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001672 return avc_has_perm(&selinux_state,
1673 sid, isec->sid, isec->sclass, perms, adp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674}
1675
1676/* Same as inode_has_perm, but pass explicit audit data containing
1677 the dentry to help the auditing code to more easily generate the
1678 pathname if needed. */
David Howells88e67f32008-11-14 10:39:21 +11001679static inline int dentry_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 struct dentry *dentry,
1681 u32 av)
1682{
David Howellsc6f493d2015-03-17 22:26:22 +00001683 struct inode *inode = d_backing_inode(dentry);
Thomas Liu2bf49692009-07-14 12:14:09 -04001684 struct common_audit_data ad;
David Howells88e67f32008-11-14 10:39:21 +11001685
Eric Paris50c205f2012-04-04 15:01:43 -04001686 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Paris2875fa02011-04-28 16:04:24 -04001687 ad.u.dentry = dentry;
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05001688 __inode_security_revalidate(inode, dentry, true);
Linus Torvalds19e49832013-10-04 12:54:11 -07001689 return inode_has_perm(cred, inode, av, &ad);
Eric Paris2875fa02011-04-28 16:04:24 -04001690}
1691
1692/* Same as inode_has_perm, but pass explicit audit data containing
1693 the path to help the auditing code to more easily generate the
1694 pathname if needed. */
1695static inline int path_has_perm(const struct cred *cred,
Al Viro3f7036a2015-03-08 19:28:30 -04001696 const struct path *path,
Eric Paris2875fa02011-04-28 16:04:24 -04001697 u32 av)
1698{
David Howellsc6f493d2015-03-17 22:26:22 +00001699 struct inode *inode = d_backing_inode(path->dentry);
Eric Paris2875fa02011-04-28 16:04:24 -04001700 struct common_audit_data ad;
1701
Eric Paris50c205f2012-04-04 15:01:43 -04001702 ad.type = LSM_AUDIT_DATA_PATH;
Eric Paris2875fa02011-04-28 16:04:24 -04001703 ad.u.path = *path;
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05001704 __inode_security_revalidate(inode, path->dentry, true);
Linus Torvalds19e49832013-10-04 12:54:11 -07001705 return inode_has_perm(cred, inode, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706}
1707
David Howells13f8e982013-06-13 23:37:55 +01001708/* Same as path_has_perm, but uses the inode from the file struct. */
1709static inline int file_path_has_perm(const struct cred *cred,
1710 struct file *file,
1711 u32 av)
1712{
1713 struct common_audit_data ad;
1714
Vivek Goyal43af5de2016-09-09 11:37:49 -04001715 ad.type = LSM_AUDIT_DATA_FILE;
1716 ad.u.file = file;
Linus Torvalds19e49832013-10-04 12:54:11 -07001717 return inode_has_perm(cred, file_inode(file), av, &ad);
David Howells13f8e982013-06-13 23:37:55 +01001718}
1719
Chenbo Fengf66e4482017-10-18 13:00:26 -07001720#ifdef CONFIG_BPF_SYSCALL
1721static int bpf_fd_pass(struct file *file, u32 sid);
1722#endif
1723
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724/* Check whether a task can use an open file descriptor to
1725 access an inode in a given way. Check access to the
1726 descriptor itself, and then use dentry_has_perm to
1727 check a particular permission to the file.
1728 Access to the descriptor is implicitly granted if it
1729 has the same SID as the process. If av is zero, then
1730 access to the file is not checked, e.g. for cases
1731 where only the descriptor is affected like seek. */
David Howells88e67f32008-11-14 10:39:21 +11001732static int file_has_perm(const struct cred *cred,
1733 struct file *file,
1734 u32 av)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735{
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07001736 struct file_security_struct *fsec = selinux_file(file);
Al Viro496ad9a2013-01-23 17:07:38 -05001737 struct inode *inode = file_inode(file);
Thomas Liu2bf49692009-07-14 12:14:09 -04001738 struct common_audit_data ad;
David Howells88e67f32008-11-14 10:39:21 +11001739 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 int rc;
1741
Vivek Goyal43af5de2016-09-09 11:37:49 -04001742 ad.type = LSM_AUDIT_DATA_FILE;
1743 ad.u.file = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
David Howells275bb412008-11-14 10:39:19 +11001745 if (sid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001746 rc = avc_has_perm(&selinux_state,
1747 sid, fsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 SECCLASS_FD,
1749 FD__USE,
1750 &ad);
1751 if (rc)
David Howells88e67f32008-11-14 10:39:21 +11001752 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 }
1754
Chenbo Fengf66e4482017-10-18 13:00:26 -07001755#ifdef CONFIG_BPF_SYSCALL
1756 rc = bpf_fd_pass(file, cred_sid(cred));
1757 if (rc)
1758 return rc;
1759#endif
1760
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 /* av is zero if only checking access to the descriptor. */
David Howells88e67f32008-11-14 10:39:21 +11001762 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 if (av)
Linus Torvalds19e49832013-10-04 12:54:11 -07001764 rc = inode_has_perm(cred, inode, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
David Howells88e67f32008-11-14 10:39:21 +11001766out:
1767 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768}
1769
David Howellsc3c188b2015-07-10 17:19:58 -04001770/*
1771 * Determine the label for an inode that might be unioned.
1772 */
Vivek Goyalc957f6d2016-07-13 10:44:51 -04001773static int
1774selinux_determine_inode_label(const struct task_security_struct *tsec,
1775 struct inode *dir,
1776 const struct qstr *name, u16 tclass,
1777 u32 *_new_isid)
David Howellsc3c188b2015-07-10 17:19:58 -04001778{
1779 const struct superblock_security_struct *sbsec = dir->i_sb->s_security;
David Howellsc3c188b2015-07-10 17:19:58 -04001780
1781 if ((sbsec->flags & SE_SBINITIALIZED) &&
1782 (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)) {
1783 *_new_isid = sbsec->mntpoint_sid;
1784 } else if ((sbsec->flags & SBLABEL_MNT) &&
1785 tsec->create_sid) {
1786 *_new_isid = tsec->create_sid;
1787 } else {
Paul Moore20cdef82016-04-04 14:14:42 -04001788 const struct inode_security_struct *dsec = inode_security(dir);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001789 return security_transition_sid(&selinux_state, tsec->sid,
1790 dsec->sid, tclass,
David Howellsc3c188b2015-07-10 17:19:58 -04001791 name, _new_isid);
1792 }
1793
1794 return 0;
1795}
1796
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797/* Check whether a task can create a file. */
1798static int may_create(struct inode *dir,
1799 struct dentry *dentry,
1800 u16 tclass)
1801{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07001802 const struct task_security_struct *tsec = selinux_cred(current_cred());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 struct inode_security_struct *dsec;
1804 struct superblock_security_struct *sbsec;
David Howells275bb412008-11-14 10:39:19 +11001805 u32 sid, newsid;
Thomas Liu2bf49692009-07-14 12:14:09 -04001806 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 int rc;
1808
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001809 dsec = inode_security(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 sbsec = dir->i_sb->s_security;
1811
David Howells275bb412008-11-14 10:39:19 +11001812 sid = tsec->sid;
David Howells275bb412008-11-14 10:39:19 +11001813
Eric Paris50c205f2012-04-04 15:01:43 -04001814 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04001815 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001817 rc = avc_has_perm(&selinux_state,
1818 sid, dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 DIR__ADD_NAME | DIR__SEARCH,
1820 &ad);
1821 if (rc)
1822 return rc;
1823
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07001824 rc = selinux_determine_inode_label(selinux_cred(current_cred()), dir,
Vivek Goyalc957f6d2016-07-13 10:44:51 -04001825 &dentry->d_name, tclass, &newsid);
David Howellsc3c188b2015-07-10 17:19:58 -04001826 if (rc)
1827 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001829 rc = avc_has_perm(&selinux_state,
1830 sid, newsid, tclass, FILE__CREATE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 if (rc)
1832 return rc;
1833
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001834 return avc_has_perm(&selinux_state,
1835 newsid, sbsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 SECCLASS_FILESYSTEM,
1837 FILESYSTEM__ASSOCIATE, &ad);
1838}
1839
Eric Paris828dfe12008-04-17 13:17:49 -04001840#define MAY_LINK 0
1841#define MAY_UNLINK 1
1842#define MAY_RMDIR 2
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
1844/* Check whether a task can link, unlink, or rmdir a file/directory. */
1845static int may_link(struct inode *dir,
1846 struct dentry *dentry,
1847 int kind)
1848
1849{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 struct inode_security_struct *dsec, *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001851 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11001852 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 u32 av;
1854 int rc;
1855
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001856 dsec = inode_security(dir);
1857 isec = backing_inode_security(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
Eric Paris50c205f2012-04-04 15:01:43 -04001859 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04001860 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
1862 av = DIR__SEARCH;
1863 av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001864 rc = avc_has_perm(&selinux_state,
1865 sid, dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 if (rc)
1867 return rc;
1868
1869 switch (kind) {
1870 case MAY_LINK:
1871 av = FILE__LINK;
1872 break;
1873 case MAY_UNLINK:
1874 av = FILE__UNLINK;
1875 break;
1876 case MAY_RMDIR:
1877 av = DIR__RMDIR;
1878 break;
1879 default:
peter enderborgc103a912018-06-12 10:09:03 +02001880 pr_warn("SELinux: %s: unrecognized kind %d\n",
Eric Paris744ba352008-04-17 11:52:44 -04001881 __func__, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 return 0;
1883 }
1884
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001885 rc = avc_has_perm(&selinux_state,
1886 sid, isec->sid, isec->sclass, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 return rc;
1888}
1889
1890static inline int may_rename(struct inode *old_dir,
1891 struct dentry *old_dentry,
1892 struct inode *new_dir,
1893 struct dentry *new_dentry)
1894{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001896 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11001897 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 u32 av;
1899 int old_is_dir, new_is_dir;
1900 int rc;
1901
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001902 old_dsec = inode_security(old_dir);
1903 old_isec = backing_inode_security(old_dentry);
David Howellse36cb0b2015-01-29 12:02:35 +00001904 old_is_dir = d_is_dir(old_dentry);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001905 new_dsec = inode_security(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
Eric Paris50c205f2012-04-04 15:01:43 -04001907 ad.type = LSM_AUDIT_DATA_DENTRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
Eric Parisa2694342011-04-25 13:10:27 -04001909 ad.u.dentry = old_dentry;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001910 rc = avc_has_perm(&selinux_state,
1911 sid, old_dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 DIR__REMOVE_NAME | DIR__SEARCH, &ad);
1913 if (rc)
1914 return rc;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001915 rc = avc_has_perm(&selinux_state,
1916 sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 old_isec->sclass, FILE__RENAME, &ad);
1918 if (rc)
1919 return rc;
1920 if (old_is_dir && new_dir != old_dir) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001921 rc = avc_has_perm(&selinux_state,
1922 sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 old_isec->sclass, DIR__REPARENT, &ad);
1924 if (rc)
1925 return rc;
1926 }
1927
Eric Parisa2694342011-04-25 13:10:27 -04001928 ad.u.dentry = new_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 av = DIR__ADD_NAME | DIR__SEARCH;
David Howells2c616d42015-01-29 12:02:33 +00001930 if (d_is_positive(new_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 av |= DIR__REMOVE_NAME;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001932 rc = avc_has_perm(&selinux_state,
1933 sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 if (rc)
1935 return rc;
David Howells2c616d42015-01-29 12:02:33 +00001936 if (d_is_positive(new_dentry)) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001937 new_isec = backing_inode_security(new_dentry);
David Howellse36cb0b2015-01-29 12:02:35 +00001938 new_is_dir = d_is_dir(new_dentry);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001939 rc = avc_has_perm(&selinux_state,
1940 sid, new_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 new_isec->sclass,
1942 (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
1943 if (rc)
1944 return rc;
1945 }
1946
1947 return 0;
1948}
1949
1950/* Check whether a task can perform a filesystem operation. */
David Howells88e67f32008-11-14 10:39:21 +11001951static int superblock_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 struct super_block *sb,
1953 u32 perms,
Thomas Liu2bf49692009-07-14 12:14:09 -04001954 struct common_audit_data *ad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 struct superblock_security_struct *sbsec;
David Howells88e67f32008-11-14 10:39:21 +11001957 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 sbsec = sb->s_security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001960 return avc_has_perm(&selinux_state,
1961 sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962}
1963
1964/* Convert a Linux mode and permission mask to an access vector. */
1965static inline u32 file_mask_to_av(int mode, int mask)
1966{
1967 u32 av = 0;
1968
Al Virodba19c62011-07-25 20:49:29 -04001969 if (!S_ISDIR(mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 if (mask & MAY_EXEC)
1971 av |= FILE__EXECUTE;
1972 if (mask & MAY_READ)
1973 av |= FILE__READ;
1974
1975 if (mask & MAY_APPEND)
1976 av |= FILE__APPEND;
1977 else if (mask & MAY_WRITE)
1978 av |= FILE__WRITE;
1979
1980 } else {
1981 if (mask & MAY_EXEC)
1982 av |= DIR__SEARCH;
1983 if (mask & MAY_WRITE)
1984 av |= DIR__WRITE;
1985 if (mask & MAY_READ)
1986 av |= DIR__READ;
1987 }
1988
1989 return av;
1990}
1991
1992/* Convert a Linux file to an access vector. */
1993static inline u32 file_to_av(struct file *file)
1994{
1995 u32 av = 0;
1996
1997 if (file->f_mode & FMODE_READ)
1998 av |= FILE__READ;
1999 if (file->f_mode & FMODE_WRITE) {
2000 if (file->f_flags & O_APPEND)
2001 av |= FILE__APPEND;
2002 else
2003 av |= FILE__WRITE;
2004 }
Stephen Smalley0794c662008-03-17 08:55:18 -04002005 if (!av) {
2006 /*
2007 * Special file opened with flags 3 for ioctl-only use.
2008 */
2009 av = FILE__IOCTL;
2010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
2012 return av;
2013}
2014
Eric Paris8b6a5a32008-10-29 17:06:46 -04002015/*
2016 * Convert a file to an access vector and include the correct open
2017 * open permission.
2018 */
2019static inline u32 open_file_to_av(struct file *file)
2020{
2021 u32 av = file_to_av(file);
Stephen Smalleyccb54472017-05-12 12:41:24 -04002022 struct inode *inode = file_inode(file);
Eric Paris8b6a5a32008-10-29 17:06:46 -04002023
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002024 if (selinux_policycap_openperm() &&
2025 inode->i_sb->s_magic != SOCKFS_MAGIC)
Eric Paris49b7b8d2010-07-23 11:44:09 -04002026 av |= FILE__OPEN;
2027
Eric Paris8b6a5a32008-10-29 17:06:46 -04002028 return av;
2029}
2030
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031/* Hook functions begin here. */
2032
Stephen Smalley79af7302015-01-21 10:54:10 -05002033static int selinux_binder_set_context_mgr(struct task_struct *mgr)
2034{
2035 u32 mysid = current_sid();
2036 u32 mgrsid = task_sid(mgr);
2037
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002038 return avc_has_perm(&selinux_state,
2039 mysid, mgrsid, SECCLASS_BINDER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002040 BINDER__SET_CONTEXT_MGR, NULL);
2041}
2042
2043static int selinux_binder_transaction(struct task_struct *from,
2044 struct task_struct *to)
2045{
2046 u32 mysid = current_sid();
2047 u32 fromsid = task_sid(from);
2048 u32 tosid = task_sid(to);
2049 int rc;
2050
2051 if (mysid != fromsid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002052 rc = avc_has_perm(&selinux_state,
2053 mysid, fromsid, SECCLASS_BINDER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002054 BINDER__IMPERSONATE, NULL);
2055 if (rc)
2056 return rc;
2057 }
2058
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002059 return avc_has_perm(&selinux_state,
2060 fromsid, tosid, SECCLASS_BINDER, BINDER__CALL,
Stephen Smalley79af7302015-01-21 10:54:10 -05002061 NULL);
2062}
2063
2064static int selinux_binder_transfer_binder(struct task_struct *from,
2065 struct task_struct *to)
2066{
2067 u32 fromsid = task_sid(from);
2068 u32 tosid = task_sid(to);
2069
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002070 return avc_has_perm(&selinux_state,
2071 fromsid, tosid, SECCLASS_BINDER, BINDER__TRANSFER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002072 NULL);
2073}
2074
2075static int selinux_binder_transfer_file(struct task_struct *from,
2076 struct task_struct *to,
2077 struct file *file)
2078{
2079 u32 sid = task_sid(to);
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07002080 struct file_security_struct *fsec = selinux_file(file);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002081 struct dentry *dentry = file->f_path.dentry;
Paul Moore20cdef82016-04-04 14:14:42 -04002082 struct inode_security_struct *isec;
Stephen Smalley79af7302015-01-21 10:54:10 -05002083 struct common_audit_data ad;
2084 int rc;
2085
2086 ad.type = LSM_AUDIT_DATA_PATH;
2087 ad.u.path = file->f_path;
2088
2089 if (sid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002090 rc = avc_has_perm(&selinux_state,
2091 sid, fsec->sid,
Stephen Smalley79af7302015-01-21 10:54:10 -05002092 SECCLASS_FD,
2093 FD__USE,
2094 &ad);
2095 if (rc)
2096 return rc;
2097 }
2098
Chenbo Fengf66e4482017-10-18 13:00:26 -07002099#ifdef CONFIG_BPF_SYSCALL
2100 rc = bpf_fd_pass(file, sid);
2101 if (rc)
2102 return rc;
2103#endif
2104
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002105 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
Stephen Smalley79af7302015-01-21 10:54:10 -05002106 return 0;
2107
Paul Moore20cdef82016-04-04 14:14:42 -04002108 isec = backing_inode_security(dentry);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002109 return avc_has_perm(&selinux_state,
2110 sid, isec->sid, isec->sclass, file_to_av(file),
Stephen Smalley79af7302015-01-21 10:54:10 -05002111 &ad);
2112}
2113
Ingo Molnar9e488582009-05-07 19:26:19 +10002114static int selinux_ptrace_access_check(struct task_struct *child,
David Howells5cd9c582008-08-14 11:37:28 +01002115 unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002117 u32 sid = current_sid();
2118 u32 csid = task_sid(child);
Stephen Smalley006ebb42008-05-19 08:32:49 -04002119
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002120 if (mode & PTRACE_MODE_READ)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002121 return avc_has_perm(&selinux_state,
2122 sid, csid, SECCLASS_FILE, FILE__READ, NULL);
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002123
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002124 return avc_has_perm(&selinux_state,
2125 sid, csid, SECCLASS_PROCESS, PROCESS__PTRACE, NULL);
David Howells5cd9c582008-08-14 11:37:28 +01002126}
2127
2128static int selinux_ptrace_traceme(struct task_struct *parent)
2129{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002130 return avc_has_perm(&selinux_state,
2131 task_sid(parent), current_sid(), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002132 PROCESS__PTRACE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133}
2134
2135static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
Eric Paris828dfe12008-04-17 13:17:49 -04002136 kernel_cap_t *inheritable, kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002138 return avc_has_perm(&selinux_state,
2139 current_sid(), task_sid(target), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002140 PROCESS__GETCAP, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141}
2142
David Howellsd84f4f92008-11-14 10:39:23 +11002143static int selinux_capset(struct cred *new, const struct cred *old,
2144 const kernel_cap_t *effective,
2145 const kernel_cap_t *inheritable,
2146 const kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002148 return avc_has_perm(&selinux_state,
2149 cred_sid(old), cred_sid(new), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002150 PROCESS__SETCAP, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151}
2152
James Morris5626d3e2009-01-30 10:05:06 +11002153/*
2154 * (This comment used to live with the selinux_task_setuid hook,
2155 * which was removed).
2156 *
2157 * Since setuid only affects the current process, and since the SELinux
2158 * controls are not based on the Linux identity attributes, SELinux does not
2159 * need to control this operation. However, SELinux does control the use of
2160 * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
2161 */
2162
Eric Paris6a9de492012-01-03 12:25:14 -05002163static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
2164 int cap, int audit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165{
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04002166 return cred_has_capability(cred, cap, audit, ns == &init_user_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167}
2168
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
2170{
David Howells88e67f32008-11-14 10:39:21 +11002171 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 int rc = 0;
2173
2174 if (!sb)
2175 return 0;
2176
2177 switch (cmds) {
Eric Paris828dfe12008-04-17 13:17:49 -04002178 case Q_SYNC:
2179 case Q_QUOTAON:
2180 case Q_QUOTAOFF:
2181 case Q_SETINFO:
2182 case Q_SETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11002183 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04002184 break;
2185 case Q_GETFMT:
2186 case Q_GETINFO:
2187 case Q_GETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11002188 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04002189 break;
2190 default:
2191 rc = 0; /* let the kernel handle invalid cmds */
2192 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 }
2194 return rc;
2195}
2196
2197static int selinux_quota_on(struct dentry *dentry)
2198{
David Howells88e67f32008-11-14 10:39:21 +11002199 const struct cred *cred = current_cred();
2200
Eric Paris2875fa02011-04-28 16:04:24 -04002201 return dentry_has_perm(cred, dentry, FILE__QUOTAON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202}
2203
Eric Paris12b30522010-11-15 18:36:29 -05002204static int selinux_syslog(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 switch (type) {
Kees Cookd78ca3c2010-02-03 15:37:13 -08002207 case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */
2208 case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002209 return avc_has_perm(&selinux_state,
2210 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002211 SECCLASS_SYSTEM, SYSTEM__SYSLOG_READ, NULL);
Kees Cookd78ca3c2010-02-03 15:37:13 -08002212 case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */
2213 case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */
2214 /* Set level of messages printed to console */
2215 case SYSLOG_ACTION_CONSOLE_LEVEL:
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_CONSOLE,
2219 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 }
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002221 /* All other syslog types */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002222 return avc_has_perm(&selinux_state,
2223 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002224 SECCLASS_SYSTEM, SYSTEM__SYSLOG_MOD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225}
2226
2227/*
2228 * Check that a process has enough memory to allocate a new virtual
2229 * mapping. 0 means there is enough memory for the allocation to
2230 * succeed and -ENOMEM implies there is not.
2231 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 * Do not audit the selinux permission check, as this is applied to all
2233 * processes that allocate mappings.
2234 */
Alan Cox34b4e4a2007-08-22 14:01:28 -07002235static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236{
2237 int rc, cap_sys_admin = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002239 rc = cred_has_capability(current_cred(), CAP_SYS_ADMIN,
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04002240 SECURITY_CAP_NOAUDIT, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 if (rc == 0)
2242 cap_sys_admin = 1;
2243
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002244 return cap_sys_admin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245}
2246
2247/* binprm security operations */
2248
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002249static u32 ptrace_parent_sid(void)
Paul Moore0c6181c2016-03-30 21:41:21 -04002250{
2251 u32 sid = 0;
2252 struct task_struct *tracer;
2253
2254 rcu_read_lock();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002255 tracer = ptrace_parent(current);
Paul Moore0c6181c2016-03-30 21:41:21 -04002256 if (tracer)
2257 sid = task_sid(tracer);
2258 rcu_read_unlock();
2259
2260 return sid;
2261}
2262
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002263static int check_nnp_nosuid(const struct linux_binprm *bprm,
2264 const struct task_security_struct *old_tsec,
2265 const struct task_security_struct *new_tsec)
2266{
2267 int nnp = (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS);
Andy Lutomirski380cf5b2016-06-23 16:41:05 -05002268 int nosuid = !mnt_may_suid(bprm->file->f_path.mnt);
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002269 int rc;
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002270 u32 av;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002271
2272 if (!nnp && !nosuid)
2273 return 0; /* neither NNP nor nosuid */
2274
2275 if (new_tsec->sid == old_tsec->sid)
2276 return 0; /* No change in credentials */
2277
2278 /*
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002279 * If the policy enables the nnp_nosuid_transition policy capability,
2280 * then we permit transitions under NNP or nosuid if the
2281 * policy allows the corresponding permission between
2282 * the old and new contexts.
2283 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002284 if (selinux_policycap_nnp_nosuid_transition()) {
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002285 av = 0;
2286 if (nnp)
2287 av |= PROCESS2__NNP_TRANSITION;
2288 if (nosuid)
2289 av |= PROCESS2__NOSUID_TRANSITION;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002290 rc = avc_has_perm(&selinux_state,
2291 old_tsec->sid, new_tsec->sid,
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002292 SECCLASS_PROCESS2, av, NULL);
2293 if (!rc)
2294 return 0;
2295 }
2296
2297 /*
2298 * We also permit NNP or nosuid transitions to bounded SIDs,
2299 * i.e. SIDs that are guaranteed to only be allowed a subset
2300 * of the permissions of the current SID.
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002301 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002302 rc = security_bounded_transition(&selinux_state, old_tsec->sid,
2303 new_tsec->sid);
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002304 if (!rc)
2305 return 0;
2306
2307 /*
2308 * On failure, preserve the errno values for NNP vs nosuid.
2309 * NNP: Operation not permitted for caller.
2310 * nosuid: Permission denied to file.
2311 */
2312 if (nnp)
2313 return -EPERM;
2314 return -EACCES;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002315}
2316
David Howellsa6f76f22008-11-14 10:39:24 +11002317static int selinux_bprm_set_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318{
David Howellsa6f76f22008-11-14 10:39:24 +11002319 const struct task_security_struct *old_tsec;
2320 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 struct inode_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04002322 struct common_audit_data ad;
Al Viro496ad9a2013-01-23 17:07:38 -05002323 struct inode *inode = file_inode(bprm->file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 int rc;
2325
David Howellsa6f76f22008-11-14 10:39:24 +11002326 /* SELinux context only depends on initial program or script and not
2327 * the script interpreter */
Kees Cookddb4a142017-07-18 15:25:23 -07002328 if (bprm->called_set_creds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 return 0;
2330
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002331 old_tsec = selinux_cred(current_cred());
2332 new_tsec = selinux_cred(bprm->cred);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002333 isec = inode_security(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
2335 /* Default to the current task SID. */
David Howellsa6f76f22008-11-14 10:39:24 +11002336 new_tsec->sid = old_tsec->sid;
2337 new_tsec->osid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338
Michael LeMay28eba5b2006-06-27 02:53:42 -07002339 /* Reset fs, key, and sock SIDs on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002340 new_tsec->create_sid = 0;
2341 new_tsec->keycreate_sid = 0;
2342 new_tsec->sockcreate_sid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
David Howellsa6f76f22008-11-14 10:39:24 +11002344 if (old_tsec->exec_sid) {
2345 new_tsec->sid = old_tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 /* Reset exec SID on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002347 new_tsec->exec_sid = 0;
Andy Lutomirski259e5e62012-04-12 16:47:50 -05002348
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002349 /* Fail on NNP or nosuid if not an allowed transition. */
2350 rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
2351 if (rc)
2352 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 } else {
2354 /* Check for a default transition on this program. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002355 rc = security_transition_sid(&selinux_state, old_tsec->sid,
2356 isec->sid, SECCLASS_PROCESS, NULL,
Eric Paris652bb9b2011-02-01 11:05:40 -05002357 &new_tsec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 if (rc)
2359 return rc;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002360
2361 /*
2362 * Fallback to old SID on NNP or nosuid if not an allowed
2363 * transition.
2364 */
2365 rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
2366 if (rc)
2367 new_tsec->sid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 }
2369
Vivek Goyal43af5de2016-09-09 11:37:49 -04002370 ad.type = LSM_AUDIT_DATA_FILE;
2371 ad.u.file = bprm->file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
David Howellsa6f76f22008-11-14 10:39:24 +11002373 if (new_tsec->sid == old_tsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002374 rc = avc_has_perm(&selinux_state,
2375 old_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
2377 if (rc)
2378 return rc;
2379 } else {
2380 /* Check permissions for the transition. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002381 rc = avc_has_perm(&selinux_state,
2382 old_tsec->sid, new_tsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
2384 if (rc)
2385 return rc;
2386
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002387 rc = avc_has_perm(&selinux_state,
2388 new_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
2390 if (rc)
2391 return rc;
2392
David Howellsa6f76f22008-11-14 10:39:24 +11002393 /* Check for shared state */
2394 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002395 rc = avc_has_perm(&selinux_state,
2396 old_tsec->sid, new_tsec->sid,
David Howellsa6f76f22008-11-14 10:39:24 +11002397 SECCLASS_PROCESS, PROCESS__SHARE,
2398 NULL);
2399 if (rc)
2400 return -EPERM;
2401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
David Howellsa6f76f22008-11-14 10:39:24 +11002403 /* Make sure that anyone attempting to ptrace over a task that
2404 * changes its SID has the appropriate permit */
Eric W. Biederman9227dd22017-01-23 17:26:31 +13002405 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002406 u32 ptsid = ptrace_parent_sid();
David Howellsa6f76f22008-11-14 10:39:24 +11002407 if (ptsid != 0) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002408 rc = avc_has_perm(&selinux_state,
2409 ptsid, new_tsec->sid,
David Howellsa6f76f22008-11-14 10:39:24 +11002410 SECCLASS_PROCESS,
2411 PROCESS__PTRACE, NULL);
2412 if (rc)
2413 return -EPERM;
2414 }
2415 }
2416
2417 /* Clear any possibly unsafe personality bits on exec: */
2418 bprm->per_clear |= PER_CLEAR_ON_SETID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 /* Enable secure mode for SIDs transitions unless
2421 the noatsecure permission is granted between
2422 the two SIDs, i.e. ahp returns 0. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002423 rc = avc_has_perm(&selinux_state,
2424 old_tsec->sid, new_tsec->sid,
Kees Cook62874c32017-07-18 15:25:25 -07002425 SECCLASS_PROCESS, PROCESS__NOATSECURE,
2426 NULL);
2427 bprm->secureexec |= !!rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 }
2429
Kees Cook62874c32017-07-18 15:25:25 -07002430 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431}
2432
Al Viroc3c073f2012-08-21 22:32:06 -04002433static int match_file(const void *p, struct file *file, unsigned fd)
2434{
2435 return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0;
2436}
2437
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438/* Derived from fs/exec.c:flush_old_files. */
David Howells745ca242008-11-14 10:39:22 +11002439static inline void flush_unauthorized_files(const struct cred *cred,
2440 struct files_struct *files)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 struct file *file, *devnull = NULL;
Stephen Smalleyb20c8122006-09-25 23:32:03 -07002443 struct tty_struct *tty;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002444 int drop_tty = 0;
Al Viroc3c073f2012-08-21 22:32:06 -04002445 unsigned n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002447 tty = get_current_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 if (tty) {
Peter Hurley4a510962016-01-09 21:35:23 -08002449 spin_lock(&tty->files_lock);
Eric Paris37dd0bd2008-10-31 17:40:00 -04002450 if (!list_empty(&tty->tty_files)) {
Nick Piggind996b622010-08-18 04:37:36 +10002451 struct tty_file_private *file_priv;
Eric Paris37dd0bd2008-10-31 17:40:00 -04002452
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 /* Revalidate access to controlling tty.
David Howells13f8e982013-06-13 23:37:55 +01002454 Use file_path_has_perm on the tty path directly
2455 rather than using file_has_perm, as this particular
2456 open file may belong to another process and we are
2457 only interested in the inode-based check here. */
Nick Piggind996b622010-08-18 04:37:36 +10002458 file_priv = list_first_entry(&tty->tty_files,
2459 struct tty_file_private, list);
2460 file = file_priv->file;
David Howells13f8e982013-06-13 23:37:55 +01002461 if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE))
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002462 drop_tty = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 }
Peter Hurley4a510962016-01-09 21:35:23 -08002464 spin_unlock(&tty->files_lock);
Alan Cox452a00d2008-10-13 10:39:13 +01002465 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 }
Eric W. Biederman98a27ba2007-05-08 00:26:56 -07002467 /* Reset controlling tty. */
2468 if (drop_tty)
2469 no_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
2471 /* Revalidate access to inherited open files. */
Al Viroc3c073f2012-08-21 22:32:06 -04002472 n = iterate_fd(files, 0, match_file, cred);
2473 if (!n) /* none found? */
2474 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
Al Viroc3c073f2012-08-21 22:32:06 -04002476 devnull = dentry_open(&selinux_null, O_RDWR, cred);
Al Viro45525b22012-10-16 13:30:07 -04002477 if (IS_ERR(devnull))
2478 devnull = NULL;
2479 /* replace all the matching ones with this */
2480 do {
2481 replace_fd(n - 1, devnull, 0);
2482 } while ((n = iterate_fd(files, n, match_file, cred)) != 0);
2483 if (devnull)
Al Viroc3c073f2012-08-21 22:32:06 -04002484 fput(devnull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485}
2486
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487/*
David Howellsa6f76f22008-11-14 10:39:24 +11002488 * Prepare a process for imminent new credential changes due to exec
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 */
David Howellsa6f76f22008-11-14 10:39:24 +11002490static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491{
David Howellsa6f76f22008-11-14 10:39:24 +11002492 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 struct rlimit *rlim, *initrlim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 int rc, i;
2495
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002496 new_tsec = selinux_cred(bprm->cred);
David Howellsa6f76f22008-11-14 10:39:24 +11002497 if (new_tsec->sid == new_tsec->osid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 return;
2499
2500 /* Close files for which the new task SID is not authorized. */
David Howellsa6f76f22008-11-14 10:39:24 +11002501 flush_unauthorized_files(bprm->cred, current->files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502
David Howellsa6f76f22008-11-14 10:39:24 +11002503 /* Always clear parent death signal on SID transitions. */
2504 current->pdeath_signal = 0;
2505
2506 /* Check whether the new SID can inherit resource limits from the old
2507 * SID. If not, reset all soft limits to the lower of the current
2508 * task's hard limit and the init task's soft limit.
2509 *
2510 * Note that the setting of hard limits (even to lower them) can be
2511 * controlled by the setrlimit check. The inclusion of the init task's
2512 * soft limit into the computation is to avoid resetting soft limits
2513 * higher than the default soft limit for cases where the default is
2514 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2515 */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002516 rc = avc_has_perm(&selinux_state,
2517 new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
David Howellsa6f76f22008-11-14 10:39:24 +11002518 PROCESS__RLIMITINH, NULL);
2519 if (rc) {
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002520 /* protect against do_prlimit() */
2521 task_lock(current);
David Howellsa6f76f22008-11-14 10:39:24 +11002522 for (i = 0; i < RLIM_NLIMITS; i++) {
2523 rlim = current->signal->rlim + i;
2524 initrlim = init_task.signal->rlim + i;
2525 rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
2526 }
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002527 task_unlock(current);
Nicolas Pitrebaa73d92016-11-11 00:10:10 -05002528 if (IS_ENABLED(CONFIG_POSIX_TIMERS))
2529 update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
David Howellsa6f76f22008-11-14 10:39:24 +11002530 }
2531}
2532
2533/*
2534 * Clean up the process immediately after the installation of new credentials
2535 * due to exec
2536 */
2537static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
2538{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002539 const struct task_security_struct *tsec = selinux_cred(current_cred());
David Howellsa6f76f22008-11-14 10:39:24 +11002540 struct itimerval itimer;
David Howellsa6f76f22008-11-14 10:39:24 +11002541 u32 osid, sid;
2542 int rc, i;
David Howellsa6f76f22008-11-14 10:39:24 +11002543
David Howellsa6f76f22008-11-14 10:39:24 +11002544 osid = tsec->osid;
2545 sid = tsec->sid;
2546
2547 if (sid == osid)
2548 return;
2549
2550 /* Check whether the new SID can inherit signal state from the old SID.
2551 * If not, clear itimers to avoid subsequent signal generation and
2552 * flush and unblock signals.
2553 *
2554 * This must occur _after_ the task SID has been updated so that any
2555 * kill done after the flush will be checked against the new SID.
2556 */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002557 rc = avc_has_perm(&selinux_state,
2558 osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 if (rc) {
Nicolas Pitrebaa73d92016-11-11 00:10:10 -05002560 if (IS_ENABLED(CONFIG_POSIX_TIMERS)) {
2561 memset(&itimer, 0, sizeof itimer);
2562 for (i = 0; i < 3; i++)
2563 do_setitimer(i, &itimer, NULL);
2564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterov9e7c8f82015-06-04 16:22:16 -04002566 if (!fatal_signal_pending(current)) {
2567 flush_sigqueue(&current->pending);
2568 flush_sigqueue(&current->signal->shared_pending);
David Howells3bcac022009-04-29 13:45:05 +01002569 flush_signal_handlers(current, 1);
2570 sigemptyset(&current->blocked);
Oleg Nesterov9e7c8f82015-06-04 16:22:16 -04002571 recalc_sigpending();
David Howells3bcac022009-04-29 13:45:05 +01002572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 spin_unlock_irq(&current->sighand->siglock);
2574 }
2575
David Howellsa6f76f22008-11-14 10:39:24 +11002576 /* Wake up the parent if it is waiting so that it can recheck
2577 * wait permission to the new task SID. */
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002578 read_lock(&tasklist_lock);
Oleg Nesterov0b7570e2009-09-23 15:56:46 -07002579 __wake_up_parent(current, current->real_parent);
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002580 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581}
2582
2583/* superblock security operations */
2584
2585static int selinux_sb_alloc_security(struct super_block *sb)
2586{
2587 return superblock_alloc_security(sb);
2588}
2589
2590static void selinux_sb_free_security(struct super_block *sb)
2591{
2592 superblock_free_security(sb);
2593}
2594
Al Viro99dbbb52018-12-14 21:56:23 -05002595static inline int opt_len(const char *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596{
Al Viro99dbbb52018-12-14 21:56:23 -05002597 bool open_quote = false;
2598 int len;
2599 char c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600
Al Viro99dbbb52018-12-14 21:56:23 -05002601 for (len = 0; (c = s[len]) != '\0'; len++) {
2602 if (c == '"')
Cory Olmo3528a952006-09-29 01:58:44 -07002603 open_quote = !open_quote;
Al Viro99dbbb52018-12-14 21:56:23 -05002604 if (c == ',' && !open_quote)
2605 break;
2606 }
2607 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608}
2609
Al Viro204cc0c2018-12-13 13:41:47 -05002610static int selinux_sb_eat_lsm_opts(char *options, void **mnt_opts)
Eric Paris026eb162011-03-03 16:09:14 -05002611{
Al Viro99dbbb52018-12-14 21:56:23 -05002612 char *from = options;
2613 char *to = options;
2614 bool first = true;
Al Viro5b400232018-12-12 20:13:29 -05002615
Al Viro99dbbb52018-12-14 21:56:23 -05002616 while (1) {
2617 int len = opt_len(from);
2618 int token, rc;
2619 char *arg = NULL;
2620
2621 token = match_opt_prefix(from, len, &arg);
2622
2623 if (token != Opt_error) {
2624 char *p, *q;
2625
2626 /* strip quotes */
2627 if (arg) {
2628 for (p = q = arg; p < from + len; p++) {
2629 char c = *p;
2630 if (c != '"')
2631 *q++ = c;
2632 }
2633 arg = kmemdup_nul(arg, q - arg, GFP_KERNEL);
2634 }
2635 rc = selinux_add_opt(token, arg, mnt_opts);
2636 if (unlikely(rc)) {
2637 kfree(arg);
2638 if (*mnt_opts) {
2639 selinux_free_mnt_opts(*mnt_opts);
2640 *mnt_opts = NULL;
2641 }
2642 return rc;
2643 }
2644 } else {
2645 if (!first) { // copy with preceding comma
2646 from--;
2647 len++;
2648 }
2649 if (to != from)
2650 memmove(to, from, len);
2651 to += len;
2652 first = false;
2653 }
2654 if (!from[len])
2655 break;
2656 from += len + 1;
2657 }
2658 *to = '\0';
2659 return 0;
Al Viro5b400232018-12-12 20:13:29 -05002660}
2661
Al Viro204cc0c2018-12-13 13:41:47 -05002662static int selinux_sb_remount(struct super_block *sb, void *mnt_opts)
Eric Paris026eb162011-03-03 16:09:14 -05002663{
Al Virobd323652018-12-13 15:04:59 -05002664 struct selinux_mnt_opts *opts = mnt_opts;
Eric Paris026eb162011-03-03 16:09:14 -05002665 struct superblock_security_struct *sbsec = sb->s_security;
Al Virobd323652018-12-13 15:04:59 -05002666 u32 sid;
2667 int rc;
Eric Paris026eb162011-03-03 16:09:14 -05002668
2669 if (!(sbsec->flags & SE_SBINITIALIZED))
2670 return 0;
2671
Al Viro204cc0c2018-12-13 13:41:47 -05002672 if (!opts)
Eric Paris026eb162011-03-03 16:09:14 -05002673 return 0;
2674
Al Virobd323652018-12-13 15:04:59 -05002675 if (opts->fscontext) {
2676 rc = parse_sid(sb, opts->fscontext, &sid);
2677 if (rc)
Al Viroc039bc32018-12-01 23:06:57 -05002678 return rc;
Al Virobd323652018-12-13 15:04:59 -05002679 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
2680 goto out_bad_option;
Eric Paris026eb162011-03-03 16:09:14 -05002681 }
Al Virobd323652018-12-13 15:04:59 -05002682 if (opts->context) {
2683 rc = parse_sid(sb, opts->context, &sid);
2684 if (rc)
2685 return rc;
2686 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
2687 goto out_bad_option;
2688 }
2689 if (opts->rootcontext) {
2690 struct inode_security_struct *root_isec;
2691 root_isec = backing_inode_security(sb->s_root);
2692 rc = parse_sid(sb, opts->rootcontext, &sid);
2693 if (rc)
2694 return rc;
2695 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
2696 goto out_bad_option;
2697 }
2698 if (opts->defcontext) {
2699 rc = parse_sid(sb, opts->defcontext, &sid);
2700 if (rc)
2701 return rc;
2702 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
2703 goto out_bad_option;
Eric Paris026eb162011-03-03 16:09:14 -05002704 }
Al Viroc039bc32018-12-01 23:06:57 -05002705 return 0;
Eric Paris026eb162011-03-03 16:09:14 -05002706
Eric Paris026eb162011-03-03 16:09:14 -05002707out_bad_option:
peter enderborgc103a912018-06-12 10:09:03 +02002708 pr_warn("SELinux: unable to change security options "
Linus Torvalds29b1deb2013-12-15 11:17:45 -08002709 "during remount (dev %s, type=%s)\n", sb->s_id,
2710 sb->s_type->name);
Al Viroc039bc32018-12-01 23:06:57 -05002711 return -EINVAL;
Eric Paris026eb162011-03-03 16:09:14 -05002712}
2713
Al Viroa10d7c22018-12-05 11:58:35 -05002714static int selinux_sb_kern_mount(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715{
David Howells88e67f32008-11-14 10:39:21 +11002716 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002717 struct common_audit_data ad;
James Morris74192242008-12-19 11:41:10 +11002718
Eric Paris50c205f2012-04-04 15:01:43 -04002719 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002720 ad.u.dentry = sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002721 return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722}
2723
David Howells726c3342006-06-23 02:02:58 -07002724static int selinux_sb_statfs(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725{
David Howells88e67f32008-11-14 10:39:21 +11002726 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002727 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728
Eric Paris50c205f2012-04-04 15:01:43 -04002729 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002730 ad.u.dentry = dentry->d_sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002731 return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732}
2733
Al Viro808d4e32012-10-11 11:42:01 -04002734static int selinux_mount(const char *dev_name,
Al Viro8a04c432016-03-25 14:52:53 -04002735 const struct path *path,
Al Viro808d4e32012-10-11 11:42:01 -04002736 const char *type,
Eric Paris828dfe12008-04-17 13:17:49 -04002737 unsigned long flags,
2738 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739{
David Howells88e67f32008-11-14 10:39:21 +11002740 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741
2742 if (flags & MS_REMOUNT)
Al Virod8c95842011-12-07 18:16:57 -05002743 return superblock_has_perm(cred, path->dentry->d_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002744 FILESYSTEM__REMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 else
Eric Paris2875fa02011-04-28 16:04:24 -04002746 return path_has_perm(cred, path, FILE__MOUNTON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747}
2748
2749static int selinux_umount(struct vfsmount *mnt, int flags)
2750{
David Howells88e67f32008-11-14 10:39:21 +11002751 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752
David Howells88e67f32008-11-14 10:39:21 +11002753 return superblock_has_perm(cred, mnt->mnt_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002754 FILESYSTEM__UNMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755}
2756
2757/* inode security operations */
2758
2759static int selinux_inode_alloc_security(struct inode *inode)
2760{
2761 return inode_alloc_security(inode);
2762}
2763
2764static void selinux_inode_free_security(struct inode *inode)
2765{
2766 inode_free_security(inode);
2767}
2768
David Quigleyd47be3d2013-05-22 12:50:34 -04002769static int selinux_dentry_init_security(struct dentry *dentry, int mode,
Al Viro4f3ccd72016-07-20 16:06:15 -04002770 const struct qstr *name, void **ctx,
David Quigleyd47be3d2013-05-22 12:50:34 -04002771 u32 *ctxlen)
2772{
David Quigleyd47be3d2013-05-22 12:50:34 -04002773 u32 newsid;
2774 int rc;
2775
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002776 rc = selinux_determine_inode_label(selinux_cred(current_cred()),
Vivek Goyalc957f6d2016-07-13 10:44:51 -04002777 d_inode(dentry->d_parent), name,
David Howellsc3c188b2015-07-10 17:19:58 -04002778 inode_mode_to_security_class(mode),
2779 &newsid);
2780 if (rc)
2781 return rc;
David Quigleyd47be3d2013-05-22 12:50:34 -04002782
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002783 return security_sid_to_context(&selinux_state, newsid, (char **)ctx,
2784 ctxlen);
David Quigleyd47be3d2013-05-22 12:50:34 -04002785}
2786
Vivek Goyala518b0a2016-07-13 10:44:53 -04002787static int selinux_dentry_create_files_as(struct dentry *dentry, int mode,
2788 struct qstr *name,
2789 const struct cred *old,
2790 struct cred *new)
2791{
2792 u32 newsid;
2793 int rc;
2794 struct task_security_struct *tsec;
2795
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002796 rc = selinux_determine_inode_label(selinux_cred(old),
Vivek Goyala518b0a2016-07-13 10:44:53 -04002797 d_inode(dentry->d_parent), name,
2798 inode_mode_to_security_class(mode),
2799 &newsid);
2800 if (rc)
2801 return rc;
2802
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002803 tsec = selinux_cred(new);
Vivek Goyala518b0a2016-07-13 10:44:53 -04002804 tsec->create_sid = newsid;
2805 return 0;
2806}
2807
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002808static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +09002809 const struct qstr *qstr,
2810 const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -05002811 void **value, size_t *len)
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002812{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002813 const struct task_security_struct *tsec = selinux_cred(current_cred());
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002814 struct superblock_security_struct *sbsec;
Corentin LABBEc0d4f462017-10-04 20:32:17 +02002815 u32 newsid, clen;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002816 int rc;
Tetsuo Handa95489062013-07-25 05:44:02 +09002817 char *context;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002818
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002819 sbsec = dir->i_sb->s_security;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002820
David Howells275bb412008-11-14 10:39:19 +11002821 newsid = tsec->create_sid;
2822
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002823 rc = selinux_determine_inode_label(selinux_cred(current_cred()),
David Howellsc3c188b2015-07-10 17:19:58 -04002824 dir, qstr,
2825 inode_mode_to_security_class(inode->i_mode),
2826 &newsid);
2827 if (rc)
2828 return rc;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002829
Eric Paris296fddf2006-09-25 23:32:00 -07002830 /* Possibly defer initialization to selinux_complete_init. */
David P. Quigley0d90a7e2009-01-16 09:22:02 -05002831 if (sbsec->flags & SE_SBINITIALIZED) {
Eric Paris296fddf2006-09-25 23:32:00 -07002832 struct inode_security_struct *isec = inode->i_security;
2833 isec->sclass = inode_mode_to_security_class(inode->i_mode);
2834 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05002835 isec->initialized = LABEL_INITIALIZED;
Eric Paris296fddf2006-09-25 23:32:00 -07002836 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002837
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002838 if (!selinux_state.initialized || !(sbsec->flags & SBLABEL_MNT))
Stephen Smalley25a74f32005-11-08 21:34:33 -08002839 return -EOPNOTSUPP;
2840
Tetsuo Handa95489062013-07-25 05:44:02 +09002841 if (name)
2842 *name = XATTR_SELINUX_SUFFIX;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002843
2844 if (value && len) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002845 rc = security_sid_to_context_force(&selinux_state, newsid,
2846 &context, &clen);
Tetsuo Handa95489062013-07-25 05:44:02 +09002847 if (rc)
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002848 return rc;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002849 *value = context;
2850 *len = clen;
2851 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002852
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002853 return 0;
2854}
2855
Al Viro4acdaf22011-07-26 01:42:34 -04002856static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857{
2858 return may_create(dir, dentry, SECCLASS_FILE);
2859}
2860
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2862{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 return may_link(dir, old_dentry, MAY_LINK);
2864}
2865
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
2867{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 return may_link(dir, dentry, MAY_UNLINK);
2869}
2870
2871static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
2872{
2873 return may_create(dir, dentry, SECCLASS_LNK_FILE);
2874}
2875
Al Viro18bb1db2011-07-26 01:41:39 -04002876static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877{
2878 return may_create(dir, dentry, SECCLASS_DIR);
2879}
2880
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
2882{
2883 return may_link(dir, dentry, MAY_RMDIR);
2884}
2885
Al Viro1a67aaf2011-07-26 01:52:52 -04002886static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 return may_create(dir, dentry, inode_mode_to_security_class(mode));
2889}
2890
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
Eric Paris828dfe12008-04-17 13:17:49 -04002892 struct inode *new_inode, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893{
2894 return may_rename(old_inode, old_dentry, new_inode, new_dentry);
2895}
2896
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897static int selinux_inode_readlink(struct dentry *dentry)
2898{
David Howells88e67f32008-11-14 10:39:21 +11002899 const struct cred *cred = current_cred();
2900
Eric Paris2875fa02011-04-28 16:04:24 -04002901 return dentry_has_perm(cred, dentry, FILE__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902}
2903
NeilBrownbda0be72015-03-23 13:37:39 +11002904static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode,
2905 bool rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906{
David Howells88e67f32008-11-14 10:39:21 +11002907 const struct cred *cred = current_cred();
NeilBrownbda0be72015-03-23 13:37:39 +11002908 struct common_audit_data ad;
2909 struct inode_security_struct *isec;
2910 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911
NeilBrownbda0be72015-03-23 13:37:39 +11002912 validate_creds(cred);
2913
2914 ad.type = LSM_AUDIT_DATA_DENTRY;
2915 ad.u.dentry = dentry;
2916 sid = cred_sid(cred);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05002917 isec = inode_security_rcu(inode, rcu);
2918 if (IS_ERR(isec))
2919 return PTR_ERR(isec);
NeilBrownbda0be72015-03-23 13:37:39 +11002920
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002921 return avc_has_perm_flags(&selinux_state,
2922 sid, isec->sid, isec->sclass, FILE__READ, &ad,
NeilBrownbda0be72015-03-23 13:37:39 +11002923 rcu ? MAY_NOT_BLOCK : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924}
2925
Eric Parisd4cf970d2012-04-04 15:01:42 -04002926static noinline int audit_inode_permission(struct inode *inode,
2927 u32 perms, u32 audited, u32 denied,
Stephen Smalley626b9742014-04-29 11:29:04 -07002928 int result,
Eric Parisd4cf970d2012-04-04 15:01:42 -04002929 unsigned flags)
2930{
2931 struct common_audit_data ad;
Eric Parisd4cf970d2012-04-04 15:01:42 -04002932 struct inode_security_struct *isec = inode->i_security;
2933 int rc;
2934
Eric Paris50c205f2012-04-04 15:01:43 -04002935 ad.type = LSM_AUDIT_DATA_INODE;
Eric Parisd4cf970d2012-04-04 15:01:42 -04002936 ad.u.inode = inode;
2937
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002938 rc = slow_avc_audit(&selinux_state,
2939 current_sid(), isec->sid, isec->sclass, perms,
Stephen Smalley626b9742014-04-29 11:29:04 -07002940 audited, denied, result, &ad, flags);
Eric Parisd4cf970d2012-04-04 15:01:42 -04002941 if (rc)
2942 return rc;
2943 return 0;
2944}
2945
Al Viroe74f71e2011-06-20 19:38:15 -04002946static int selinux_inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947{
David Howells88e67f32008-11-14 10:39:21 +11002948 const struct cred *cred = current_cred();
Eric Parisb782e0a2010-07-23 11:44:03 -04002949 u32 perms;
2950 bool from_access;
Al Virocf1dd1d2011-06-20 19:44:08 -04002951 unsigned flags = mask & MAY_NOT_BLOCK;
Eric Paris2e334052012-04-04 15:01:42 -04002952 struct inode_security_struct *isec;
2953 u32 sid;
2954 struct av_decision avd;
2955 int rc, rc2;
2956 u32 audited, denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957
Eric Parisb782e0a2010-07-23 11:44:03 -04002958 from_access = mask & MAY_ACCESS;
Eric Parisd09ca732010-07-23 11:43:57 -04002959 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
2960
Eric Parisb782e0a2010-07-23 11:44:03 -04002961 /* No permission to check. Existence test. */
2962 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964
Eric Paris2e334052012-04-04 15:01:42 -04002965 validate_creds(cred);
Eric Parisb782e0a2010-07-23 11:44:03 -04002966
Eric Paris2e334052012-04-04 15:01:42 -04002967 if (unlikely(IS_PRIVATE(inode)))
2968 return 0;
Eric Parisb782e0a2010-07-23 11:44:03 -04002969
2970 perms = file_mask_to_av(inode->i_mode, mask);
2971
Eric Paris2e334052012-04-04 15:01:42 -04002972 sid = cred_sid(cred);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05002973 isec = inode_security_rcu(inode, flags & MAY_NOT_BLOCK);
2974 if (IS_ERR(isec))
2975 return PTR_ERR(isec);
Eric Paris2e334052012-04-04 15:01:42 -04002976
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002977 rc = avc_has_perm_noaudit(&selinux_state,
2978 sid, isec->sid, isec->sclass, perms, 0, &avd);
Eric Paris2e334052012-04-04 15:01:42 -04002979 audited = avc_audit_required(perms, &avd, rc,
2980 from_access ? FILE__AUDIT_ACCESS : 0,
2981 &denied);
2982 if (likely(!audited))
2983 return rc;
2984
Stephen Smalley626b9742014-04-29 11:29:04 -07002985 rc2 = audit_inode_permission(inode, perms, audited, denied, rc, flags);
Eric Paris2e334052012-04-04 15:01:42 -04002986 if (rc2)
2987 return rc2;
2988 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989}
2990
2991static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
2992{
David Howells88e67f32008-11-14 10:39:21 +11002993 const struct cred *cred = current_cred();
Stephen Smalleyccb54472017-05-12 12:41:24 -04002994 struct inode *inode = d_backing_inode(dentry);
Amerigo Wangbc6a6002009-08-20 19:29:02 -07002995 unsigned int ia_valid = iattr->ia_valid;
Eric Paris95dbf732012-04-04 13:45:34 -04002996 __u32 av = FILE__WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997
Amerigo Wangbc6a6002009-08-20 19:29:02 -07002998 /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
2999 if (ia_valid & ATTR_FORCE) {
3000 ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
3001 ATTR_FORCE);
3002 if (!ia_valid)
3003 return 0;
3004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003006 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
3007 ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
Eric Paris2875fa02011-04-28 16:04:24 -04003008 return dentry_has_perm(cred, dentry, FILE__SETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003010 if (selinux_policycap_openperm() &&
Stephen Smalleyccb54472017-05-12 12:41:24 -04003011 inode->i_sb->s_magic != SOCKFS_MAGIC &&
3012 (ia_valid & ATTR_SIZE) &&
3013 !(ia_valid & ATTR_FILE))
Eric Paris95dbf732012-04-04 13:45:34 -04003014 av |= FILE__OPEN;
3015
3016 return dentry_has_perm(cred, dentry, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017}
3018
Al Viro3f7036a2015-03-08 19:28:30 -04003019static int selinux_inode_getattr(const struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020{
Al Viro3f7036a2015-03-08 19:28:30 -04003021 return path_has_perm(current_cred(), path, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022}
3023
Stephen Smalleydb590002017-04-20 11:31:30 -04003024static bool has_cap_mac_admin(bool audit)
3025{
3026 const struct cred *cred = current_cred();
3027 int cap_audit = audit ? SECURITY_CAP_AUDIT : SECURITY_CAP_NOAUDIT;
3028
3029 if (cap_capable(cred, &init_user_ns, CAP_MAC_ADMIN, cap_audit))
3030 return false;
3031 if (cred_has_capability(cred, CAP_MAC_ADMIN, cap_audit, true))
3032 return false;
3033 return true;
3034}
3035
David Howells8f0cfa52008-04-29 00:59:41 -07003036static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
3037 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038{
David Howellsc6f493d2015-03-17 22:26:22 +00003039 struct inode *inode = d_backing_inode(dentry);
Paul Moore20cdef82016-04-04 14:14:42 -04003040 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 struct superblock_security_struct *sbsec;
Thomas Liu2bf49692009-07-14 12:14:09 -04003042 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11003043 u32 newsid, sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 int rc = 0;
3045
Eric W. Biederman6b240302017-10-02 09:38:20 -05003046 if (strcmp(name, XATTR_NAME_SELINUX)) {
3047 rc = cap_inode_setxattr(dentry, name, value, size, flags);
3048 if (rc)
3049 return rc;
3050
3051 /* Not an attribute we recognize, so just check the
3052 ordinary setattr permission. */
3053 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055
3056 sbsec = inode->i_sb->s_security;
Eric Paris12f348b2012-10-09 10:56:25 -04003057 if (!(sbsec->flags & SBLABEL_MNT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 return -EOPNOTSUPP;
3059
Serge E. Hallyn2e149672011-03-23 16:43:26 -07003060 if (!inode_owner_or_capable(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 return -EPERM;
3062
Eric Paris50c205f2012-04-04 15:01:43 -04003063 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04003064 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065
Paul Moore20cdef82016-04-04 14:14:42 -04003066 isec = backing_inode_security(dentry);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003067 rc = avc_has_perm(&selinux_state,
3068 sid, isec->sid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 FILE__RELABELFROM, &ad);
3070 if (rc)
3071 return rc;
3072
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003073 rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3074 GFP_KERNEL);
Stephen Smalley12b29f32008-05-07 13:03:20 -04003075 if (rc == -EINVAL) {
Stephen Smalleydb590002017-04-20 11:31:30 -04003076 if (!has_cap_mac_admin(true)) {
Eric Parisd6ea83e2012-04-04 13:45:49 -04003077 struct audit_buffer *ab;
3078 size_t audit_size;
Eric Parisd6ea83e2012-04-04 13:45:49 -04003079
3080 /* We strip a nul only if it is at the end, otherwise the
3081 * context contains a nul and we should audit that */
Al Viroe3fea3f2012-06-09 08:15:16 +01003082 if (value) {
Colin Ian Kingadd24372017-10-14 13:46:55 +01003083 const char *str = value;
3084
Al Viroe3fea3f2012-06-09 08:15:16 +01003085 if (str[size - 1] == '\0')
3086 audit_size = size - 1;
3087 else
3088 audit_size = size;
3089 } else {
Al Viroe3fea3f2012-06-09 08:15:16 +01003090 audit_size = 0;
3091 }
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04003092 ab = audit_log_start(audit_context(),
3093 GFP_ATOMIC, AUDIT_SELINUX_ERR);
Eric Parisd6ea83e2012-04-04 13:45:49 -04003094 audit_log_format(ab, "op=setxattr invalid_context=");
3095 audit_log_n_untrustedstring(ab, value, audit_size);
3096 audit_log_end(ab);
3097
Stephen Smalley12b29f32008-05-07 13:03:20 -04003098 return rc;
Eric Parisd6ea83e2012-04-04 13:45:49 -04003099 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003100 rc = security_context_to_sid_force(&selinux_state, value,
3101 size, &newsid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04003102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 if (rc)
3104 return rc;
3105
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003106 rc = avc_has_perm(&selinux_state,
3107 sid, newsid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 FILE__RELABELTO, &ad);
3109 if (rc)
3110 return rc;
3111
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003112 rc = security_validate_transition(&selinux_state, isec->sid, newsid,
3113 sid, isec->sclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 if (rc)
3115 return rc;
3116
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003117 return avc_has_perm(&selinux_state,
3118 newsid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 sbsec->sid,
3120 SECCLASS_FILESYSTEM,
3121 FILESYSTEM__ASSOCIATE,
3122 &ad);
3123}
3124
David Howells8f0cfa52008-04-29 00:59:41 -07003125static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
Eric Parisf5269712008-05-14 11:27:45 -04003126 const void *value, size_t size,
David Howells8f0cfa52008-04-29 00:59:41 -07003127 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128{
David Howellsc6f493d2015-03-17 22:26:22 +00003129 struct inode *inode = d_backing_inode(dentry);
Paul Moore20cdef82016-04-04 14:14:42 -04003130 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 u32 newsid;
3132 int rc;
3133
3134 if (strcmp(name, XATTR_NAME_SELINUX)) {
3135 /* Not an attribute we recognize, so nothing to do. */
3136 return;
3137 }
3138
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003139 rc = security_context_to_sid_force(&selinux_state, value, size,
3140 &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 if (rc) {
peter enderborgc103a912018-06-12 10:09:03 +02003142 pr_err("SELinux: unable to map context to SID"
Stephen Smalley12b29f32008-05-07 13:03:20 -04003143 "for (%s, %lu), rc=%d\n",
3144 inode->i_sb->s_id, inode->i_ino, -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 return;
3146 }
3147
Paul Moore20cdef82016-04-04 14:14:42 -04003148 isec = backing_inode_security(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003149 spin_lock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003150 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05003152 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003153 spin_unlock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003154
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 return;
3156}
3157
David Howells8f0cfa52008-04-29 00:59:41 -07003158static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159{
David Howells88e67f32008-11-14 10:39:21 +11003160 const struct cred *cred = current_cred();
3161
Eric Paris2875fa02011-04-28 16:04:24 -04003162 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163}
3164
Eric Paris828dfe12008-04-17 13:17:49 -04003165static int selinux_inode_listxattr(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166{
David Howells88e67f32008-11-14 10:39:21 +11003167 const struct cred *cred = current_cred();
3168
Eric Paris2875fa02011-04-28 16:04:24 -04003169 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170}
3171
David Howells8f0cfa52008-04-29 00:59:41 -07003172static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173{
Eric W. Biederman6b240302017-10-02 09:38:20 -05003174 if (strcmp(name, XATTR_NAME_SELINUX)) {
3175 int rc = cap_inode_removexattr(dentry, name);
3176 if (rc)
3177 return rc;
3178
3179 /* Not an attribute we recognize, so just check the
3180 ordinary setattr permission. */
3181 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183
3184 /* No one is allowed to remove a SELinux security label.
3185 You can change the label, but all data must be labeled. */
3186 return -EACCES;
3187}
3188
James Morrisd381d8a2005-10-30 14:59:22 -08003189/*
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003190 * Copy the inode security context value to the user.
James Morrisd381d8a2005-10-30 14:59:22 -08003191 *
3192 * Permission check is handled by selinux_inode_getxattr hook.
3193 */
Andreas Gruenbacherea861df2015-12-24 11:09:39 -05003194static int selinux_inode_getsecurity(struct inode *inode, const char *name, void **buffer, bool alloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195{
David P. Quigley42492592008-02-04 22:29:39 -08003196 u32 size;
3197 int error;
3198 char *context = NULL;
Paul Moore20cdef82016-04-04 14:14:42 -04003199 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00003201 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3202 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003204 /*
3205 * If the caller has CAP_MAC_ADMIN, then get the raw context
3206 * value even if it is not defined by current policy; otherwise,
3207 * use the in-core value under current policy.
3208 * Use the non-auditing forms of the permission checks since
3209 * getxattr may be called by unprivileged processes commonly
3210 * and lack of permission just means that we fall back to the
3211 * in-core context value, not a denial.
3212 */
Paul Moore20cdef82016-04-04 14:14:42 -04003213 isec = inode_security(inode);
Stephen Smalleydb590002017-04-20 11:31:30 -04003214 if (has_cap_mac_admin(false))
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003215 error = security_sid_to_context_force(&selinux_state,
3216 isec->sid, &context,
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003217 &size);
3218 else
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003219 error = security_sid_to_context(&selinux_state, isec->sid,
3220 &context, &size);
David P. Quigley42492592008-02-04 22:29:39 -08003221 if (error)
3222 return error;
3223 error = size;
3224 if (alloc) {
3225 *buffer = context;
3226 goto out_nofree;
3227 }
3228 kfree(context);
3229out_nofree:
3230 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231}
3232
3233static int selinux_inode_setsecurity(struct inode *inode, const char *name,
Eric Paris828dfe12008-04-17 13:17:49 -04003234 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235{
Paul Moore2c971652016-04-19 16:36:28 -04003236 struct inode_security_struct *isec = inode_security_novalidate(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 u32 newsid;
3238 int rc;
3239
3240 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3241 return -EOPNOTSUPP;
3242
3243 if (!value || !size)
3244 return -EACCES;
3245
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003246 rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3247 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 if (rc)
3249 return rc;
3250
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003251 spin_lock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003252 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05003254 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003255 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 return 0;
3257}
3258
3259static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
3260{
3261 const int len = sizeof(XATTR_NAME_SELINUX);
3262 if (buffer && len <= buffer_size)
3263 memcpy(buffer, XATTR_NAME_SELINUX, len);
3264 return len;
3265}
3266
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05003267static void selinux_inode_getsecid(struct inode *inode, u32 *secid)
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02003268{
Andreas Gruenbachere817c2f2016-02-18 12:04:08 +01003269 struct inode_security_struct *isec = inode_security_novalidate(inode);
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02003270 *secid = isec->sid;
3271}
3272
Vivek Goyal56909eb2016-07-13 10:44:48 -04003273static int selinux_inode_copy_up(struct dentry *src, struct cred **new)
3274{
3275 u32 sid;
3276 struct task_security_struct *tsec;
3277 struct cred *new_creds = *new;
3278
3279 if (new_creds == NULL) {
3280 new_creds = prepare_creds();
3281 if (!new_creds)
3282 return -ENOMEM;
3283 }
3284
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07003285 tsec = selinux_cred(new_creds);
Vivek Goyal56909eb2016-07-13 10:44:48 -04003286 /* Get label from overlay inode and set it in create_sid */
3287 selinux_inode_getsecid(d_inode(src), &sid);
3288 tsec->create_sid = sid;
3289 *new = new_creds;
3290 return 0;
3291}
3292
Vivek Goyal19472b62016-07-13 10:44:50 -04003293static int selinux_inode_copy_up_xattr(const char *name)
3294{
3295 /* The copy_up hook above sets the initial context on an inode, but we
3296 * don't then want to overwrite it by blindly copying all the lower
3297 * xattrs up. Instead, we have to filter out SELinux-related xattrs.
3298 */
3299 if (strcmp(name, XATTR_NAME_SELINUX) == 0)
3300 return 1; /* Discard */
3301 /*
3302 * Any other attribute apart from SELINUX is not claimed, supported
3303 * by selinux.
3304 */
3305 return -EOPNOTSUPP;
3306}
3307
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308/* file security operations */
3309
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003310static int selinux_revalidate_file_permission(struct file *file, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311{
David Howells88e67f32008-11-14 10:39:21 +11003312 const struct cred *cred = current_cred();
Al Viro496ad9a2013-01-23 17:07:38 -05003313 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
3316 if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
3317 mask |= MAY_APPEND;
3318
Paul Moore389fb8002009-03-27 17:10:34 -04003319 return file_has_perm(cred, file,
3320 file_mask_to_av(inode->i_mode, mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321}
3322
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003323static int selinux_file_permission(struct file *file, int mask)
3324{
Al Viro496ad9a2013-01-23 17:07:38 -05003325 struct inode *inode = file_inode(file);
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07003326 struct file_security_struct *fsec = selinux_file(file);
Andreas Gruenbacherb1973672016-01-05 23:12:33 +01003327 struct inode_security_struct *isec;
Stephen Smalley20dda182009-06-22 14:54:53 -04003328 u32 sid = current_sid();
3329
Paul Moore389fb8002009-03-27 17:10:34 -04003330 if (!mask)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003331 /* No permission to check. Existence test. */
3332 return 0;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003333
Andreas Gruenbacherb1973672016-01-05 23:12:33 +01003334 isec = inode_security(inode);
Stephen Smalley20dda182009-06-22 14:54:53 -04003335 if (sid == fsec->sid && fsec->isid == isec->sid &&
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003336 fsec->pseqno == avc_policy_seqno(&selinux_state))
Eric Paris83d49852012-04-04 13:45:40 -04003337 /* No change since file_open check. */
Stephen Smalley20dda182009-06-22 14:54:53 -04003338 return 0;
3339
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003340 return selinux_revalidate_file_permission(file, mask);
3341}
3342
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343static int selinux_file_alloc_security(struct file *file)
3344{
3345 return file_alloc_security(file);
3346}
3347
3348static void selinux_file_free_security(struct file *file)
3349{
3350 file_free_security(file);
3351}
3352
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003353/*
3354 * Check whether a task has the ioctl permission and cmd
3355 * operation to an inode.
3356 */
Geliang Tang1d2a1682015-10-21 17:44:27 -04003357static int ioctl_has_perm(const struct cred *cred, struct file *file,
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003358 u32 requested, u16 cmd)
3359{
3360 struct common_audit_data ad;
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07003361 struct file_security_struct *fsec = selinux_file(file);
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003362 struct inode *inode = file_inode(file);
Paul Moore20cdef82016-04-04 14:14:42 -04003363 struct inode_security_struct *isec;
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003364 struct lsm_ioctlop_audit ioctl;
3365 u32 ssid = cred_sid(cred);
3366 int rc;
3367 u8 driver = cmd >> 8;
3368 u8 xperm = cmd & 0xff;
3369
3370 ad.type = LSM_AUDIT_DATA_IOCTL_OP;
3371 ad.u.op = &ioctl;
3372 ad.u.op->cmd = cmd;
3373 ad.u.op->path = file->f_path;
3374
3375 if (ssid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003376 rc = avc_has_perm(&selinux_state,
3377 ssid, fsec->sid,
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003378 SECCLASS_FD,
3379 FD__USE,
3380 &ad);
3381 if (rc)
3382 goto out;
3383 }
3384
3385 if (unlikely(IS_PRIVATE(inode)))
3386 return 0;
3387
Paul Moore20cdef82016-04-04 14:14:42 -04003388 isec = inode_security(inode);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003389 rc = avc_has_extended_perms(&selinux_state,
3390 ssid, isec->sid, isec->sclass,
3391 requested, driver, xperm, &ad);
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003392out:
3393 return rc;
3394}
3395
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396static int selinux_file_ioctl(struct file *file, unsigned int cmd,
3397 unsigned long arg)
3398{
David Howells88e67f32008-11-14 10:39:21 +11003399 const struct cred *cred = current_cred();
Eric Paris0b24dcb2011-02-25 15:39:20 -05003400 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401
Eric Paris0b24dcb2011-02-25 15:39:20 -05003402 switch (cmd) {
3403 case FIONREAD:
3404 /* fall through */
3405 case FIBMAP:
3406 /* fall through */
3407 case FIGETBSZ:
3408 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003409 case FS_IOC_GETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003410 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003411 case FS_IOC_GETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003412 error = file_has_perm(cred, file, FILE__GETATTR);
3413 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414
Al Viro2f99c362012-03-23 16:04:05 -04003415 case FS_IOC_SETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003416 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003417 case FS_IOC_SETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003418 error = file_has_perm(cred, file, FILE__SETATTR);
3419 break;
3420
3421 /* sys_ioctl() checks */
3422 case FIONBIO:
3423 /* fall through */
3424 case FIOASYNC:
3425 error = file_has_perm(cred, file, 0);
3426 break;
3427
3428 case KDSKBENT:
3429 case KDSKBSENT:
Eric Paris6a9de492012-01-03 12:25:14 -05003430 error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04003431 SECURITY_CAP_AUDIT, true);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003432 break;
3433
3434 /* default case assumes that the command will go
3435 * to the file's ioctl() function.
3436 */
3437 default:
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003438 error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003439 }
3440 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441}
3442
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003443static int default_noexec;
3444
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
3446{
David Howells88e67f32008-11-14 10:39:21 +11003447 const struct cred *cred = current_cred();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003448 u32 sid = cred_sid(cred);
David Howellsd84f4f92008-11-14 10:39:23 +11003449 int rc = 0;
David Howells88e67f32008-11-14 10:39:21 +11003450
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003451 if (default_noexec &&
Stephen Smalley892e8ca2015-07-10 09:40:59 -04003452 (prot & PROT_EXEC) && (!file || IS_PRIVATE(file_inode(file)) ||
3453 (!shared && (prot & PROT_WRITE)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 /*
3455 * We are making executable an anonymous mapping or a
3456 * private file mapping that will also be writable.
3457 * This has an additional check.
3458 */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003459 rc = avc_has_perm(&selinux_state,
3460 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003461 PROCESS__EXECMEM, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 if (rc)
David Howellsd84f4f92008-11-14 10:39:23 +11003463 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465
3466 if (file) {
3467 /* read access is always possible with a mapping */
3468 u32 av = FILE__READ;
3469
3470 /* write access only matters if the mapping is shared */
3471 if (shared && (prot & PROT_WRITE))
3472 av |= FILE__WRITE;
3473
3474 if (prot & PROT_EXEC)
3475 av |= FILE__EXECUTE;
3476
David Howells88e67f32008-11-14 10:39:21 +11003477 return file_has_perm(cred, file, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 }
David Howellsd84f4f92008-11-14 10:39:23 +11003479
3480error:
3481 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482}
3483
Al Viroe5467852012-05-30 13:30:51 -04003484static int selinux_mmap_addr(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07003486 int rc = 0;
Paul Moore98883bf2014-03-19 16:46:11 -04003487
3488 if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
3489 u32 sid = current_sid();
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003490 rc = avc_has_perm(&selinux_state,
3491 sid, sid, SECCLASS_MEMPROTECT,
Paul Moore98883bf2014-03-19 16:46:11 -04003492 MEMPROTECT__MMAP_ZERO, NULL);
3493 }
3494
3495 return rc;
Al Viroe5467852012-05-30 13:30:51 -04003496}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497
Al Viroe5467852012-05-30 13:30:51 -04003498static int selinux_mmap_file(struct file *file, unsigned long reqprot,
3499 unsigned long prot, unsigned long flags)
3500{
Stephen Smalley3ba4bf52017-05-05 09:14:48 -04003501 struct common_audit_data ad;
3502 int rc;
3503
3504 if (file) {
3505 ad.type = LSM_AUDIT_DATA_FILE;
3506 ad.u.file = file;
3507 rc = inode_has_perm(current_cred(), file_inode(file),
3508 FILE__MAP, &ad);
3509 if (rc)
3510 return rc;
3511 }
3512
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003513 if (selinux_state.checkreqprot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 prot = reqprot;
3515
3516 return file_map_prot_check(file, prot,
3517 (flags & MAP_TYPE) == MAP_SHARED);
3518}
3519
3520static int selinux_file_mprotect(struct vm_area_struct *vma,
3521 unsigned long reqprot,
3522 unsigned long prot)
3523{
David Howells88e67f32008-11-14 10:39:21 +11003524 const struct cred *cred = current_cred();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003525 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003527 if (selinux_state.checkreqprot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 prot = reqprot;
3529
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003530 if (default_noexec &&
3531 (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
James Morrisd541bbe2009-01-29 12:19:51 +11003532 int rc = 0;
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003533 if (vma->vm_start >= vma->vm_mm->start_brk &&
3534 vma->vm_end <= vma->vm_mm->brk) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003535 rc = avc_has_perm(&selinux_state,
3536 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003537 PROCESS__EXECHEAP, NULL);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003538 } else if (!vma->vm_file &&
Stephen Smalleyc2316db2016-04-08 13:55:03 -04003539 ((vma->vm_start <= vma->vm_mm->start_stack &&
3540 vma->vm_end >= vma->vm_mm->start_stack) ||
Andy Lutomirskid17af502016-09-30 10:58:58 -07003541 vma_is_stack_for_current(vma))) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003542 rc = avc_has_perm(&selinux_state,
3543 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003544 PROCESS__EXECSTACK, NULL);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003545 } else if (vma->vm_file && vma->anon_vma) {
3546 /*
3547 * We are making executable a file mapping that has
3548 * had some COW done. Since pages might have been
3549 * written, check ability to execute the possibly
3550 * modified content. This typically should only
3551 * occur for text relocations.
3552 */
David Howellsd84f4f92008-11-14 10:39:23 +11003553 rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003554 }
Lorenzo Hernandez García-Hierro6b992192005-06-25 14:54:34 -07003555 if (rc)
3556 return rc;
3557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558
3559 return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
3560}
3561
3562static int selinux_file_lock(struct file *file, unsigned int cmd)
3563{
David Howells88e67f32008-11-14 10:39:21 +11003564 const struct cred *cred = current_cred();
3565
3566 return file_has_perm(cred, file, FILE__LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567}
3568
3569static int selinux_file_fcntl(struct file *file, unsigned int cmd,
3570 unsigned long arg)
3571{
David Howells88e67f32008-11-14 10:39:21 +11003572 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 int err = 0;
3574
3575 switch (cmd) {
Eric Paris828dfe12008-04-17 13:17:49 -04003576 case F_SETFL:
Eric Paris828dfe12008-04-17 13:17:49 -04003577 if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
David Howells88e67f32008-11-14 10:39:21 +11003578 err = file_has_perm(cred, file, FILE__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003580 }
3581 /* fall through */
3582 case F_SETOWN:
3583 case F_SETSIG:
3584 case F_GETFL:
3585 case F_GETOWN:
3586 case F_GETSIG:
Cyrill Gorcunov1d151c32012-07-30 14:43:00 -07003587 case F_GETOWNER_UIDS:
Eric Paris828dfe12008-04-17 13:17:49 -04003588 /* Just check FD__USE permission */
David Howells88e67f32008-11-14 10:39:21 +11003589 err = file_has_perm(cred, file, 0);
Eric Paris828dfe12008-04-17 13:17:49 -04003590 break;
3591 case F_GETLK:
3592 case F_SETLK:
3593 case F_SETLKW:
Jeff Layton0d3f7a22014-04-22 08:23:58 -04003594 case F_OFD_GETLK:
3595 case F_OFD_SETLK:
3596 case F_OFD_SETLKW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597#if BITS_PER_LONG == 32
Eric Paris828dfe12008-04-17 13:17:49 -04003598 case F_GETLK64:
3599 case F_SETLK64:
3600 case F_SETLKW64:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601#endif
David Howells88e67f32008-11-14 10:39:21 +11003602 err = file_has_perm(cred, file, FILE__LOCK);
Eric Paris828dfe12008-04-17 13:17:49 -04003603 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 }
3605
3606 return err;
3607}
3608
Jeff Laytone0b93ed2014-08-22 11:27:32 -04003609static void selinux_file_set_fowner(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 struct file_security_struct *fsec;
3612
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07003613 fsec = selinux_file(file);
David Howells275bb412008-11-14 10:39:19 +11003614 fsec->fown_sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615}
3616
3617static int selinux_file_send_sigiotask(struct task_struct *tsk,
3618 struct fown_struct *fown, int signum)
3619{
Eric Paris828dfe12008-04-17 13:17:49 -04003620 struct file *file;
Stephen Smalley65c90bc2009-05-04 15:43:18 -04003621 u32 sid = task_sid(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 struct file_security_struct *fsec;
3624
3625 /* struct fown_struct is never outside the context of a struct file */
Eric Paris828dfe12008-04-17 13:17:49 -04003626 file = container_of(fown, struct file, f_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07003628 fsec = selinux_file(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629
3630 if (!signum)
3631 perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
3632 else
3633 perm = signal_to_av(signum);
3634
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003635 return avc_has_perm(&selinux_state,
3636 fsec->fown_sid, sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 SECCLASS_PROCESS, perm, NULL);
3638}
3639
3640static int selinux_file_receive(struct file *file)
3641{
David Howells88e67f32008-11-14 10:39:21 +11003642 const struct cred *cred = current_cred();
3643
3644 return file_has_perm(cred, file, file_to_av(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645}
3646
Al Viro94817692018-07-10 14:13:18 -04003647static int selinux_file_open(struct file *file)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003648{
3649 struct file_security_struct *fsec;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003650 struct inode_security_struct *isec;
David Howellsd84f4f92008-11-14 10:39:23 +11003651
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07003652 fsec = selinux_file(file);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05003653 isec = inode_security(file_inode(file));
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003654 /*
3655 * Save inode label and policy sequence number
3656 * at open-time so that selinux_file_permission
3657 * can determine whether revalidation is necessary.
3658 * Task label is already saved in the file security
3659 * struct as its SID.
3660 */
3661 fsec->isid = isec->sid;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003662 fsec->pseqno = avc_policy_seqno(&selinux_state);
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003663 /*
3664 * Since the inode label or policy seqno may have changed
3665 * between the selinux_inode_permission check and the saving
3666 * of state above, recheck that access is still permitted.
3667 * Otherwise, access might never be revalidated against the
3668 * new inode label or new policy.
3669 * This check is not redundant - do not remove.
3670 */
Al Viro94817692018-07-10 14:13:18 -04003671 return file_path_has_perm(file->f_cred, file, open_file_to_av(file));
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003672}
3673
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674/* task security operations */
3675
Tetsuo Handaa79be232017-03-28 23:08:45 +09003676static int selinux_task_alloc(struct task_struct *task,
3677 unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003679 u32 sid = current_sid();
3680
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003681 return avc_has_perm(&selinux_state,
3682 sid, sid, SECCLASS_PROCESS, PROCESS__FORK, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683}
3684
David Howellsf1752ee2008-11-14 10:39:17 +11003685/*
David Howellsd84f4f92008-11-14 10:39:23 +11003686 * prepare a new set of credentials for modification
3687 */
3688static int selinux_cred_prepare(struct cred *new, const struct cred *old,
3689 gfp_t gfp)
3690{
Casey Schauflerbbd36622018-11-12 09:30:56 -08003691 const struct task_security_struct *old_tsec = selinux_cred(old);
3692 struct task_security_struct *tsec = selinux_cred(new);
David Howellsd84f4f92008-11-14 10:39:23 +11003693
Casey Schauflerbbd36622018-11-12 09:30:56 -08003694 *tsec = *old_tsec;
David Howellsd84f4f92008-11-14 10:39:23 +11003695 return 0;
3696}
3697
3698/*
David Howellsee18d642009-09-02 09:14:21 +01003699 * transfer the SELinux data to a blank set of creds
3700 */
3701static void selinux_cred_transfer(struct cred *new, const struct cred *old)
3702{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07003703 const struct task_security_struct *old_tsec = selinux_cred(old);
3704 struct task_security_struct *tsec = selinux_cred(new);
David Howellsee18d642009-09-02 09:14:21 +01003705
3706 *tsec = *old_tsec;
3707}
3708
Matthew Garrett3ec30112018-01-08 13:36:19 -08003709static void selinux_cred_getsecid(const struct cred *c, u32 *secid)
3710{
3711 *secid = cred_sid(c);
3712}
3713
David Howellsee18d642009-09-02 09:14:21 +01003714/*
David Howells3a3b7ce2008-11-14 10:39:28 +11003715 * set the security data for a kernel service
3716 * - all the creation contexts are set to unlabelled
3717 */
3718static int selinux_kernel_act_as(struct cred *new, u32 secid)
3719{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07003720 struct task_security_struct *tsec = selinux_cred(new);
David Howells3a3b7ce2008-11-14 10:39:28 +11003721 u32 sid = current_sid();
3722 int ret;
3723
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003724 ret = avc_has_perm(&selinux_state,
3725 sid, secid,
David Howells3a3b7ce2008-11-14 10:39:28 +11003726 SECCLASS_KERNEL_SERVICE,
3727 KERNEL_SERVICE__USE_AS_OVERRIDE,
3728 NULL);
3729 if (ret == 0) {
3730 tsec->sid = secid;
3731 tsec->create_sid = 0;
3732 tsec->keycreate_sid = 0;
3733 tsec->sockcreate_sid = 0;
3734 }
3735 return ret;
3736}
3737
3738/*
3739 * set the file creation context in a security record to the same as the
3740 * objective context of the specified inode
3741 */
3742static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
3743{
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05003744 struct inode_security_struct *isec = inode_security(inode);
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07003745 struct task_security_struct *tsec = selinux_cred(new);
David Howells3a3b7ce2008-11-14 10:39:28 +11003746 u32 sid = current_sid();
3747 int ret;
3748
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003749 ret = avc_has_perm(&selinux_state,
3750 sid, isec->sid,
David Howells3a3b7ce2008-11-14 10:39:28 +11003751 SECCLASS_KERNEL_SERVICE,
3752 KERNEL_SERVICE__CREATE_FILES_AS,
3753 NULL);
3754
3755 if (ret == 0)
3756 tsec->create_sid = isec->sid;
David Howellsef574712010-02-26 01:56:16 +00003757 return ret;
David Howells3a3b7ce2008-11-14 10:39:28 +11003758}
3759
Eric Parisdd8dbf22009-11-03 16:35:32 +11003760static int selinux_kernel_module_request(char *kmod_name)
Eric Paris25354c42009-08-13 09:45:03 -04003761{
Eric Parisdd8dbf22009-11-03 16:35:32 +11003762 struct common_audit_data ad;
3763
Eric Paris50c205f2012-04-04 15:01:43 -04003764 ad.type = LSM_AUDIT_DATA_KMOD;
Eric Parisdd8dbf22009-11-03 16:35:32 +11003765 ad.u.kmod_name = kmod_name;
3766
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003767 return avc_has_perm(&selinux_state,
3768 current_sid(), SECINITSID_KERNEL, SECCLASS_SYSTEM,
Eric Parisdd8dbf22009-11-03 16:35:32 +11003769 SYSTEM__MODULE_REQUEST, &ad);
Eric Paris25354c42009-08-13 09:45:03 -04003770}
3771
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003772static int selinux_kernel_module_from_file(struct file *file)
3773{
3774 struct common_audit_data ad;
3775 struct inode_security_struct *isec;
3776 struct file_security_struct *fsec;
3777 u32 sid = current_sid();
3778 int rc;
3779
3780 /* init_module */
3781 if (file == NULL)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003782 return avc_has_perm(&selinux_state,
3783 sid, sid, SECCLASS_SYSTEM,
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003784 SYSTEM__MODULE_LOAD, NULL);
3785
3786 /* finit_module */
Paul Moore20cdef82016-04-04 14:14:42 -04003787
Vivek Goyal43af5de2016-09-09 11:37:49 -04003788 ad.type = LSM_AUDIT_DATA_FILE;
3789 ad.u.file = file;
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003790
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07003791 fsec = selinux_file(file);
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003792 if (sid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003793 rc = avc_has_perm(&selinux_state,
3794 sid, fsec->sid, SECCLASS_FD, FD__USE, &ad);
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003795 if (rc)
3796 return rc;
3797 }
3798
Paul Moore20cdef82016-04-04 14:14:42 -04003799 isec = inode_security(file_inode(file));
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003800 return avc_has_perm(&selinux_state,
3801 sid, isec->sid, SECCLASS_SYSTEM,
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003802 SYSTEM__MODULE_LOAD, &ad);
3803}
3804
3805static int selinux_kernel_read_file(struct file *file,
3806 enum kernel_read_file_id id)
3807{
3808 int rc = 0;
3809
3810 switch (id) {
3811 case READING_MODULE:
3812 rc = selinux_kernel_module_from_file(file);
3813 break;
3814 default:
3815 break;
3816 }
3817
3818 return rc;
3819}
3820
Mimi Zoharc77b8cd2018-07-13 14:06:02 -04003821static int selinux_kernel_load_data(enum kernel_load_data_id id)
3822{
3823 int rc = 0;
3824
3825 switch (id) {
3826 case LOADING_MODULE:
3827 rc = selinux_kernel_module_from_file(NULL);
3828 default:
3829 break;
3830 }
3831
3832 return rc;
3833}
3834
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
3836{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003837 return avc_has_perm(&selinux_state,
3838 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003839 PROCESS__SETPGID, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840}
3841
3842static int selinux_task_getpgid(struct task_struct *p)
3843{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003844 return avc_has_perm(&selinux_state,
3845 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003846 PROCESS__GETPGID, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847}
3848
3849static int selinux_task_getsid(struct task_struct *p)
3850{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003851 return avc_has_perm(&selinux_state,
3852 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003853 PROCESS__GETSESSION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854}
3855
David Quigleyf9008e4c2006-06-30 01:55:46 -07003856static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
3857{
David Howells275bb412008-11-14 10:39:19 +11003858 *secid = task_sid(p);
David Quigleyf9008e4c2006-06-30 01:55:46 -07003859}
3860
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861static int selinux_task_setnice(struct task_struct *p, int nice)
3862{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003863 return avc_has_perm(&selinux_state,
3864 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003865 PROCESS__SETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866}
3867
James Morris03e68062006-06-23 02:03:58 -07003868static int selinux_task_setioprio(struct task_struct *p, int ioprio)
3869{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003870 return avc_has_perm(&selinux_state,
3871 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003872 PROCESS__SETSCHED, NULL);
James Morris03e68062006-06-23 02:03:58 -07003873}
3874
David Quigleya1836a42006-06-30 01:55:49 -07003875static int selinux_task_getioprio(struct task_struct *p)
3876{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003877 return avc_has_perm(&selinux_state,
3878 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003879 PROCESS__GETSCHED, NULL);
David Quigleya1836a42006-06-30 01:55:49 -07003880}
3881
Corentin LABBE42985552017-10-04 20:32:18 +02003882static int selinux_task_prlimit(const struct cred *cred, const struct cred *tcred,
3883 unsigned int flags)
Stephen Smalley791ec492017-02-17 07:57:00 -05003884{
3885 u32 av = 0;
3886
Stephen Smalley84e68852017-02-28 09:35:08 -05003887 if (!flags)
3888 return 0;
Stephen Smalley791ec492017-02-17 07:57:00 -05003889 if (flags & LSM_PRLIMIT_WRITE)
3890 av |= PROCESS__SETRLIMIT;
3891 if (flags & LSM_PRLIMIT_READ)
3892 av |= PROCESS__GETRLIMIT;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003893 return avc_has_perm(&selinux_state,
3894 cred_sid(cred), cred_sid(tcred),
Stephen Smalley791ec492017-02-17 07:57:00 -05003895 SECCLASS_PROCESS, av, NULL);
3896}
3897
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003898static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
3899 struct rlimit *new_rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900{
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003901 struct rlimit *old_rlim = p->signal->rlim + resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902
3903 /* Control the ability to change the hard limit (whether
3904 lowering or raising it), so that the hard limit can
3905 later be used as a safe reset point for the soft limit
David Howellsd84f4f92008-11-14 10:39:23 +11003906 upon context transitions. See selinux_bprm_committing_creds. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 if (old_rlim->rlim_max != new_rlim->rlim_max)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003908 return avc_has_perm(&selinux_state,
3909 current_sid(), task_sid(p),
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003910 SECCLASS_PROCESS, PROCESS__SETRLIMIT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911
3912 return 0;
3913}
3914
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09003915static int selinux_task_setscheduler(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003917 return avc_has_perm(&selinux_state,
3918 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003919 PROCESS__SETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920}
3921
3922static int selinux_task_getscheduler(struct task_struct *p)
3923{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003924 return avc_has_perm(&selinux_state,
3925 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003926 PROCESS__GETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927}
3928
David Quigley35601542006-06-23 02:04:01 -07003929static int selinux_task_movememory(struct task_struct *p)
3930{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003931 return avc_has_perm(&selinux_state,
3932 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003933 PROCESS__SETSCHED, NULL);
David Quigley35601542006-06-23 02:04:01 -07003934}
3935
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02003936static int selinux_task_kill(struct task_struct *p, struct kernel_siginfo *info,
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04003937 int sig, const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938{
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04003939 u32 secid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 if (!sig)
3943 perm = PROCESS__SIGNULL; /* null signal; existence test */
3944 else
3945 perm = signal_to_av(sig);
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04003946 if (!cred)
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003947 secid = current_sid();
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04003948 else
3949 secid = cred_sid(cred);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003950 return avc_has_perm(&selinux_state,
3951 secid, task_sid(p), SECCLASS_PROCESS, perm, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952}
3953
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954static void selinux_task_to_inode(struct task_struct *p,
3955 struct inode *inode)
3956{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 struct inode_security_struct *isec = inode->i_security;
David Howells275bb412008-11-14 10:39:19 +11003958 u32 sid = task_sid(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003960 spin_lock(&isec->lock);
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01003961 isec->sclass = inode_mode_to_security_class(inode->i_mode);
David Howells275bb412008-11-14 10:39:19 +11003962 isec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05003963 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003964 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965}
3966
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003968static int selinux_parse_skb_ipv4(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04003969 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970{
3971 int offset, ihlen, ret = -EINVAL;
3972 struct iphdr _iph, *ih;
3973
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03003974 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
3976 if (ih == NULL)
3977 goto out;
3978
3979 ihlen = ih->ihl * 4;
3980 if (ihlen < sizeof(_iph))
3981 goto out;
3982
Eric Paris48c62af2012-04-02 13:15:44 -04003983 ad->u.net->v4info.saddr = ih->saddr;
3984 ad->u.net->v4info.daddr = ih->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 ret = 0;
3986
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06003987 if (proto)
3988 *proto = ih->protocol;
3989
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 switch (ih->protocol) {
Eric Paris828dfe12008-04-17 13:17:49 -04003991 case IPPROTO_TCP: {
3992 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993
Eric Paris828dfe12008-04-17 13:17:49 -04003994 if (ntohs(ih->frag_off) & IP_OFFSET)
3995 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996
3997 offset += ihlen;
3998 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
3999 if (th == NULL)
4000 break;
4001
Eric Paris48c62af2012-04-02 13:15:44 -04004002 ad->u.net->sport = th->source;
4003 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006
Eric Paris828dfe12008-04-17 13:17:49 -04004007 case IPPROTO_UDP: {
4008 struct udphdr _udph, *uh;
4009
4010 if (ntohs(ih->frag_off) & IP_OFFSET)
4011 break;
4012
4013 offset += ihlen;
4014 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4015 if (uh == NULL)
4016 break;
4017
Eric Paris48c62af2012-04-02 13:15:44 -04004018 ad->u.net->sport = uh->source;
4019 ad->u.net->dport = uh->dest;
Eric Paris828dfe12008-04-17 13:17:49 -04004020 break;
4021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022
James Morris2ee92d42006-11-13 16:09:01 -08004023 case IPPROTO_DCCP: {
4024 struct dccp_hdr _dccph, *dh;
4025
4026 if (ntohs(ih->frag_off) & IP_OFFSET)
4027 break;
4028
4029 offset += ihlen;
4030 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4031 if (dh == NULL)
4032 break;
4033
Eric Paris48c62af2012-04-02 13:15:44 -04004034 ad->u.net->sport = dh->dccph_sport;
4035 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08004036 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004037 }
James Morris2ee92d42006-11-13 16:09:01 -08004038
Richard Hainesd4529302018-02-13 20:57:18 +00004039#if IS_ENABLED(CONFIG_IP_SCTP)
4040 case IPPROTO_SCTP: {
4041 struct sctphdr _sctph, *sh;
4042
4043 if (ntohs(ih->frag_off) & IP_OFFSET)
4044 break;
4045
4046 offset += ihlen;
4047 sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4048 if (sh == NULL)
4049 break;
4050
4051 ad->u.net->sport = sh->source;
4052 ad->u.net->dport = sh->dest;
4053 break;
4054 }
4055#endif
Eric Paris828dfe12008-04-17 13:17:49 -04004056 default:
4057 break;
4058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059out:
4060 return ret;
4061}
4062
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04004063#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064
4065/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004066static int selinux_parse_skb_ipv6(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04004067 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068{
4069 u8 nexthdr;
4070 int ret = -EINVAL, offset;
4071 struct ipv6hdr _ipv6h, *ip6;
Jesse Gross75f28112011-11-30 17:05:51 -08004072 __be16 frag_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03004074 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
4076 if (ip6 == NULL)
4077 goto out;
4078
Eric Paris48c62af2012-04-02 13:15:44 -04004079 ad->u.net->v6info.saddr = ip6->saddr;
4080 ad->u.net->v6info.daddr = ip6->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081 ret = 0;
4082
4083 nexthdr = ip6->nexthdr;
4084 offset += sizeof(_ipv6h);
Jesse Gross75f28112011-11-30 17:05:51 -08004085 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 if (offset < 0)
4087 goto out;
4088
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004089 if (proto)
4090 *proto = nexthdr;
4091
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 switch (nexthdr) {
4093 case IPPROTO_TCP: {
Eric Paris828dfe12008-04-17 13:17:49 -04004094 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095
4096 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4097 if (th == NULL)
4098 break;
4099
Eric Paris48c62af2012-04-02 13:15:44 -04004100 ad->u.net->sport = th->source;
4101 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 break;
4103 }
4104
4105 case IPPROTO_UDP: {
4106 struct udphdr _udph, *uh;
4107
4108 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4109 if (uh == NULL)
4110 break;
4111
Eric Paris48c62af2012-04-02 13:15:44 -04004112 ad->u.net->sport = uh->source;
4113 ad->u.net->dport = uh->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 break;
4115 }
4116
James Morris2ee92d42006-11-13 16:09:01 -08004117 case IPPROTO_DCCP: {
4118 struct dccp_hdr _dccph, *dh;
4119
4120 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4121 if (dh == NULL)
4122 break;
4123
Eric Paris48c62af2012-04-02 13:15:44 -04004124 ad->u.net->sport = dh->dccph_sport;
4125 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08004126 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004127 }
James Morris2ee92d42006-11-13 16:09:01 -08004128
Richard Hainesd4529302018-02-13 20:57:18 +00004129#if IS_ENABLED(CONFIG_IP_SCTP)
4130 case IPPROTO_SCTP: {
4131 struct sctphdr _sctph, *sh;
4132
4133 sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4134 if (sh == NULL)
4135 break;
4136
4137 ad->u.net->sport = sh->source;
4138 ad->u.net->dport = sh->dest;
4139 break;
4140 }
4141#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 /* includes fragments */
4143 default:
4144 break;
4145 }
4146out:
4147 return ret;
4148}
4149
4150#endif /* IPV6 */
4151
Thomas Liu2bf49692009-07-14 12:14:09 -04004152static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
David Howellscf9481e2008-07-27 21:31:07 +10004153 char **_addrp, int src, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154{
David Howellscf9481e2008-07-27 21:31:07 +10004155 char *addrp;
4156 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157
Eric Paris48c62af2012-04-02 13:15:44 -04004158 switch (ad->u.net->family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 case PF_INET:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004160 ret = selinux_parse_skb_ipv4(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10004161 if (ret)
4162 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04004163 addrp = (char *)(src ? &ad->u.net->v4info.saddr :
4164 &ad->u.net->v4info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10004165 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04004167#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 case PF_INET6:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004169 ret = selinux_parse_skb_ipv6(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10004170 if (ret)
4171 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04004172 addrp = (char *)(src ? &ad->u.net->v6info.saddr :
4173 &ad->u.net->v6info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10004174 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175#endif /* IPV6 */
4176 default:
David Howellscf9481e2008-07-27 21:31:07 +10004177 addrp = NULL;
4178 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 }
4180
David Howellscf9481e2008-07-27 21:31:07 +10004181parse_error:
peter enderborgc103a912018-06-12 10:09:03 +02004182 pr_warn(
David Howellscf9481e2008-07-27 21:31:07 +10004183 "SELinux: failure in selinux_parse_skb(),"
4184 " unable to parse packet\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 return ret;
David Howellscf9481e2008-07-27 21:31:07 +10004186
4187okay:
4188 if (_addrp)
4189 *_addrp = addrp;
4190 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191}
4192
Paul Moore4f6a9932007-03-01 14:35:22 -05004193/**
Paul Moore220deb92008-01-29 08:38:23 -05004194 * selinux_skb_peerlbl_sid - Determine the peer label of a packet
Paul Moore4f6a9932007-03-01 14:35:22 -05004195 * @skb: the packet
Paul Moore75e22912008-01-29 08:38:04 -05004196 * @family: protocol family
Paul Moore220deb92008-01-29 08:38:23 -05004197 * @sid: the packet's peer label SID
Paul Moore4f6a9932007-03-01 14:35:22 -05004198 *
4199 * Description:
Paul Moore220deb92008-01-29 08:38:23 -05004200 * Check the various different forms of network peer labeling and determine
4201 * the peer label/SID for the packet; most of the magic actually occurs in
4202 * the security server function security_net_peersid_cmp(). The function
4203 * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
4204 * or -EACCES if @sid is invalid due to inconsistencies with the different
4205 * peer labels.
Paul Moore4f6a9932007-03-01 14:35:22 -05004206 *
4207 */
Paul Moore220deb92008-01-29 08:38:23 -05004208static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
Paul Moore4f6a9932007-03-01 14:35:22 -05004209{
Paul Moore71f1cb02008-01-29 08:51:16 -05004210 int err;
Paul Moore4f6a9932007-03-01 14:35:22 -05004211 u32 xfrm_sid;
4212 u32 nlbl_sid;
Paul Moore220deb92008-01-29 08:38:23 -05004213 u32 nlbl_type;
Paul Moore4f6a9932007-03-01 14:35:22 -05004214
Paul Moore817eff72013-12-10 14:57:54 -05004215 err = selinux_xfrm_skb_sid(skb, &xfrm_sid);
Paul Moorebed4d7e2013-07-23 17:38:40 -04004216 if (unlikely(err))
4217 return -EACCES;
4218 err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
4219 if (unlikely(err))
4220 return -EACCES;
Paul Moore220deb92008-01-29 08:38:23 -05004221
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004222 err = security_net_peersid_resolve(&selinux_state, nlbl_sid,
4223 nlbl_type, xfrm_sid, sid);
Paul Moore71f1cb02008-01-29 08:51:16 -05004224 if (unlikely(err)) {
peter enderborgc103a912018-06-12 10:09:03 +02004225 pr_warn(
Paul Moore71f1cb02008-01-29 08:51:16 -05004226 "SELinux: failure in selinux_skb_peerlbl_sid(),"
4227 " unable to determine packet's peer label\n");
Paul Moore220deb92008-01-29 08:38:23 -05004228 return -EACCES;
Paul Moore71f1cb02008-01-29 08:51:16 -05004229 }
Paul Moore220deb92008-01-29 08:38:23 -05004230
4231 return 0;
Paul Moore4f6a9932007-03-01 14:35:22 -05004232}
4233
Paul Moore446b8022013-12-04 16:10:51 -05004234/**
4235 * selinux_conn_sid - Determine the child socket label for a connection
4236 * @sk_sid: the parent socket's SID
4237 * @skb_sid: the packet's SID
4238 * @conn_sid: the resulting connection SID
4239 *
4240 * If @skb_sid is valid then the user:role:type information from @sk_sid is
4241 * combined with the MLS information from @skb_sid in order to create
4242 * @conn_sid. If @skb_sid is not valid then then @conn_sid is simply a copy
4243 * of @sk_sid. Returns zero on success, negative values on failure.
4244 *
4245 */
4246static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid)
4247{
4248 int err = 0;
4249
4250 if (skb_sid != SECSID_NULL)
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004251 err = security_sid_mls_copy(&selinux_state, sk_sid, skb_sid,
4252 conn_sid);
Paul Moore446b8022013-12-04 16:10:51 -05004253 else
4254 *conn_sid = sk_sid;
4255
4256 return err;
4257}
4258
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259/* socket security operations */
Paul Moored4f2d972010-04-22 14:46:18 -04004260
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004261static int socket_sockcreate_sid(const struct task_security_struct *tsec,
4262 u16 secclass, u32 *socksid)
Paul Moored4f2d972010-04-22 14:46:18 -04004263{
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004264 if (tsec->sockcreate_sid > SECSID_NULL) {
4265 *socksid = tsec->sockcreate_sid;
4266 return 0;
4267 }
4268
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004269 return security_transition_sid(&selinux_state, tsec->sid, tsec->sid,
4270 secclass, NULL, socksid);
Paul Moored4f2d972010-04-22 14:46:18 -04004271}
4272
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004273static int sock_has_perm(struct sock *sk, u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274{
Paul Moore253bfae2010-04-22 14:46:19 -04004275 struct sk_security_struct *sksec = sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004276 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004277 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278
Paul Moore253bfae2010-04-22 14:46:19 -04004279 if (sksec->sid == SECINITSID_KERNEL)
4280 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281
Eric Paris50c205f2012-04-04 15:01:43 -04004282 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004283 ad.u.net = &net;
4284 ad.u.net->sk = sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004286 return avc_has_perm(&selinux_state,
4287 current_sid(), sksec->sid, sksec->sclass, perms,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004288 &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289}
4290
4291static int selinux_socket_create(int family, int type,
4292 int protocol, int kern)
4293{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07004294 const struct task_security_struct *tsec = selinux_cred(current_cred());
Paul Moored4f2d972010-04-22 14:46:18 -04004295 u32 newsid;
David Howells275bb412008-11-14 10:39:19 +11004296 u16 secclass;
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004297 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298
4299 if (kern)
Paul Moored4f2d972010-04-22 14:46:18 -04004300 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301
David Howells275bb412008-11-14 10:39:19 +11004302 secclass = socket_type_to_security_class(family, type, protocol);
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004303 rc = socket_sockcreate_sid(tsec, secclass, &newsid);
4304 if (rc)
4305 return rc;
4306
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004307 return avc_has_perm(&selinux_state,
4308 tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309}
4310
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07004311static int selinux_socket_post_create(struct socket *sock, int family,
4312 int type, int protocol, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07004314 const struct task_security_struct *tsec = selinux_cred(current_cred());
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05004315 struct inode_security_struct *isec = inode_security_novalidate(SOCK_INODE(sock));
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004316 struct sk_security_struct *sksec;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004317 u16 sclass = socket_type_to_security_class(family, type, protocol);
4318 u32 sid = SECINITSID_KERNEL;
David Howells275bb412008-11-14 10:39:19 +11004319 int err = 0;
4320
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004321 if (!kern) {
4322 err = socket_sockcreate_sid(tsec, sclass, &sid);
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004323 if (err)
4324 return err;
4325 }
David Howells275bb412008-11-14 10:39:19 +11004326
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004327 isec->sclass = sclass;
4328 isec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004329 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004331 if (sock->sk) {
4332 sksec = sock->sk->sk_security;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004333 sksec->sclass = sclass;
4334 sksec->sid = sid;
Richard Hainesd4529302018-02-13 20:57:18 +00004335 /* Allows detection of the first association on this socket */
4336 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4337 sksec->sctp_assoc_state = SCTP_ASSOC_UNSET;
4338
Paul Moore389fb8002009-03-27 17:10:34 -04004339 err = selinux_netlbl_socket_post_create(sock->sk, family);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004340 }
4341
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07004342 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343}
4344
David Herrmann0b811db2018-05-04 16:28:21 +02004345static int selinux_socket_socketpair(struct socket *socka,
4346 struct socket *sockb)
4347{
4348 struct sk_security_struct *sksec_a = socka->sk->sk_security;
4349 struct sk_security_struct *sksec_b = sockb->sk->sk_security;
4350
4351 sksec_a->peer_sid = sksec_b->sid;
4352 sksec_b->peer_sid = sksec_a->sid;
4353
4354 return 0;
4355}
4356
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357/* Range of port numbers used to automatically bind.
4358 Need to determine whether we should perform a name_bind
4359 permission check between the socket and the port number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360
4361static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
4362{
Paul Moore253bfae2010-04-22 14:46:19 -04004363 struct sock *sk = sock->sk;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004364 struct sk_security_struct *sksec = sk->sk_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 u16 family;
4366 int err;
4367
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004368 err = sock_has_perm(sk, SOCKET__BIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 if (err)
4370 goto out;
4371
Richard Hainesd4529302018-02-13 20:57:18 +00004372 /* If PF_INET or PF_INET6, check name_bind permission for the port. */
Paul Moore253bfae2010-04-22 14:46:19 -04004373 family = sk->sk_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 if (family == PF_INET || family == PF_INET6) {
4375 char *addrp;
Thomas Liu2bf49692009-07-14 12:14:09 -04004376 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004377 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 struct sockaddr_in *addr4 = NULL;
4379 struct sockaddr_in6 *addr6 = NULL;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004380 u16 family_sa = address->sa_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 unsigned short snum;
James Morrise399f982008-06-12 01:39:58 +10004382 u32 sid, node_perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383
Richard Hainesd4529302018-02-13 20:57:18 +00004384 /*
4385 * sctp_bindx(3) calls via selinux_sctp_bind_connect()
4386 * that validates multiple binding addresses. Because of this
4387 * need to check address->sa_family as it is possible to have
4388 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4389 */
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004390 switch (family_sa) {
4391 case AF_UNSPEC:
Richard Haines68741a8a2018-03-02 19:54:34 +00004392 case AF_INET:
4393 if (addrlen < sizeof(struct sockaddr_in))
4394 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 addr4 = (struct sockaddr_in *)address;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004396 if (family_sa == AF_UNSPEC) {
4397 /* see __inet_bind(), we only want to allow
4398 * AF_UNSPEC if the address is INADDR_ANY
4399 */
4400 if (addr4->sin_addr.s_addr != htonl(INADDR_ANY))
4401 goto err_af;
4402 family_sa = AF_INET;
4403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 snum = ntohs(addr4->sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 addrp = (char *)&addr4->sin_addr.s_addr;
Richard Haines68741a8a2018-03-02 19:54:34 +00004406 break;
4407 case AF_INET6:
4408 if (addrlen < SIN6_LEN_RFC2133)
4409 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 addr6 = (struct sockaddr_in6 *)address;
4411 snum = ntohs(addr6->sin6_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412 addrp = (char *)&addr6->sin6_addr.s6_addr;
Richard Haines68741a8a2018-03-02 19:54:34 +00004413 break;
4414 default:
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004415 goto err_af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 }
4417
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004418 ad.type = LSM_AUDIT_DATA_NET;
4419 ad.u.net = &net;
4420 ad.u.net->sport = htons(snum);
4421 ad.u.net->family = family_sa;
4422
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004423 if (snum) {
4424 int low, high;
4425
Eric W. Biederman0bbf87d2013-09-28 14:10:59 -07004426 inet_get_local_port_range(sock_net(sk), &low, &high);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004427
Krister Johansen4548b682017-01-20 17:49:11 -08004428 if (snum < max(inet_prot_sock(sock_net(sk)), low) ||
4429 snum > high) {
Paul Moore3e1121722008-04-10 10:48:14 -04004430 err = sel_netport_sid(sk->sk_protocol,
4431 snum, &sid);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004432 if (err)
4433 goto out;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004434 err = avc_has_perm(&selinux_state,
4435 sksec->sid, sid,
Paul Moore253bfae2010-04-22 14:46:19 -04004436 sksec->sclass,
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004437 SOCKET__NAME_BIND, &ad);
4438 if (err)
4439 goto out;
4440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 }
Eric Paris828dfe12008-04-17 13:17:49 -04004442
Paul Moore253bfae2010-04-22 14:46:19 -04004443 switch (sksec->sclass) {
James Morris13402582005-09-30 14:24:34 -04004444 case SECCLASS_TCP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 node_perm = TCP_SOCKET__NODE_BIND;
4446 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004447
James Morris13402582005-09-30 14:24:34 -04004448 case SECCLASS_UDP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 node_perm = UDP_SOCKET__NODE_BIND;
4450 break;
James Morris2ee92d42006-11-13 16:09:01 -08004451
4452 case SECCLASS_DCCP_SOCKET:
4453 node_perm = DCCP_SOCKET__NODE_BIND;
4454 break;
4455
Richard Hainesd4529302018-02-13 20:57:18 +00004456 case SECCLASS_SCTP_SOCKET:
4457 node_perm = SCTP_SOCKET__NODE_BIND;
4458 break;
4459
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 default:
4461 node_perm = RAWIP_SOCKET__NODE_BIND;
4462 break;
4463 }
Eric Paris828dfe12008-04-17 13:17:49 -04004464
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004465 err = sel_netnode_sid(addrp, family_sa, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 if (err)
4467 goto out;
Eric Paris828dfe12008-04-17 13:17:49 -04004468
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004469 if (family_sa == AF_INET)
Eric Paris48c62af2012-04-02 13:15:44 -04004470 ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 else
Eric Paris48c62af2012-04-02 13:15:44 -04004472 ad.u.net->v6info.saddr = addr6->sin6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004474 err = avc_has_perm(&selinux_state,
4475 sksec->sid, sid,
Paul Moore253bfae2010-04-22 14:46:19 -04004476 sksec->sclass, node_perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 if (err)
4478 goto out;
4479 }
4480out:
4481 return err;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004482err_af:
4483 /* Note that SCTP services expect -EINVAL, others -EAFNOSUPPORT. */
4484 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4485 return -EINVAL;
4486 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487}
4488
Richard Hainesd4529302018-02-13 20:57:18 +00004489/* This supports connect(2) and SCTP connect services such as sctp_connectx(3)
Mauro Carvalho Chehab5fb94e92018-05-08 15:14:57 -03004490 * and sctp_sendmsg(3) as described in Documentation/security/LSM-sctp.rst
Richard Hainesd4529302018-02-13 20:57:18 +00004491 */
4492static int selinux_socket_connect_helper(struct socket *sock,
4493 struct sockaddr *address, int addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494{
Paul Moore014ab192008-10-10 10:16:33 -04004495 struct sock *sk = sock->sk;
Paul Moore253bfae2010-04-22 14:46:19 -04004496 struct sk_security_struct *sksec = sk->sk_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 int err;
4498
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004499 err = sock_has_perm(sk, SOCKET__CONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 if (err)
4501 return err;
4502
4503 /*
Richard Hainesd4529302018-02-13 20:57:18 +00004504 * If a TCP, DCCP or SCTP socket, check name_connect permission
4505 * for the port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 */
Paul Moore253bfae2010-04-22 14:46:19 -04004507 if (sksec->sclass == SECCLASS_TCP_SOCKET ||
Richard Hainesd4529302018-02-13 20:57:18 +00004508 sksec->sclass == SECCLASS_DCCP_SOCKET ||
4509 sksec->sclass == SECCLASS_SCTP_SOCKET) {
Thomas Liu2bf49692009-07-14 12:14:09 -04004510 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004511 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 struct sockaddr_in *addr4 = NULL;
4513 struct sockaddr_in6 *addr6 = NULL;
4514 unsigned short snum;
James Morris2ee92d42006-11-13 16:09:01 -08004515 u32 sid, perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516
Richard Hainesd4529302018-02-13 20:57:18 +00004517 /* sctp_connectx(3) calls via selinux_sctp_bind_connect()
4518 * that validates multiple connect addresses. Because of this
4519 * need to check address->sa_family as it is possible to have
4520 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4521 */
Richard Haines68741a8a2018-03-02 19:54:34 +00004522 switch (address->sa_family) {
4523 case AF_INET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 addr4 = (struct sockaddr_in *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004525 if (addrlen < sizeof(struct sockaddr_in))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 return -EINVAL;
4527 snum = ntohs(addr4->sin_port);
Richard Haines68741a8a2018-03-02 19:54:34 +00004528 break;
4529 case AF_INET6:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 addr6 = (struct sockaddr_in6 *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004531 if (addrlen < SIN6_LEN_RFC2133)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 return -EINVAL;
4533 snum = ntohs(addr6->sin6_port);
Richard Haines68741a8a2018-03-02 19:54:34 +00004534 break;
4535 default:
4536 /* Note that SCTP services expect -EINVAL, whereas
4537 * others expect -EAFNOSUPPORT.
4538 */
4539 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4540 return -EINVAL;
4541 else
4542 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543 }
4544
Paul Moore3e1121722008-04-10 10:48:14 -04004545 err = sel_netport_sid(sk->sk_protocol, snum, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546 if (err)
Richard Hainesd4529302018-02-13 20:57:18 +00004547 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548
Richard Hainesd4529302018-02-13 20:57:18 +00004549 switch (sksec->sclass) {
4550 case SECCLASS_TCP_SOCKET:
4551 perm = TCP_SOCKET__NAME_CONNECT;
4552 break;
4553 case SECCLASS_DCCP_SOCKET:
4554 perm = DCCP_SOCKET__NAME_CONNECT;
4555 break;
4556 case SECCLASS_SCTP_SOCKET:
4557 perm = SCTP_SOCKET__NAME_CONNECT;
4558 break;
4559 }
James Morris2ee92d42006-11-13 16:09:01 -08004560
Eric Paris50c205f2012-04-04 15:01:43 -04004561 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004562 ad.u.net = &net;
4563 ad.u.net->dport = htons(snum);
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004564 ad.u.net->family = address->sa_family;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004565 err = avc_has_perm(&selinux_state,
4566 sksec->sid, sid, sksec->sclass, perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 if (err)
Richard Hainesd4529302018-02-13 20:57:18 +00004568 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569 }
4570
Richard Hainesd4529302018-02-13 20:57:18 +00004571 return 0;
4572}
Paul Moore014ab192008-10-10 10:16:33 -04004573
Richard Hainesd4529302018-02-13 20:57:18 +00004574/* Supports connect(2), see comments in selinux_socket_connect_helper() */
4575static int selinux_socket_connect(struct socket *sock,
4576 struct sockaddr *address, int addrlen)
4577{
4578 int err;
4579 struct sock *sk = sock->sk;
4580
4581 err = selinux_socket_connect_helper(sock, address, addrlen);
4582 if (err)
4583 return err;
4584
4585 return selinux_netlbl_socket_connect(sk, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586}
4587
4588static int selinux_socket_listen(struct socket *sock, int backlog)
4589{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004590 return sock_has_perm(sock->sk, SOCKET__LISTEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591}
4592
4593static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
4594{
4595 int err;
4596 struct inode_security_struct *isec;
4597 struct inode_security_struct *newisec;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004598 u16 sclass;
4599 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004601 err = sock_has_perm(sock->sk, SOCKET__ACCEPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602 if (err)
4603 return err;
4604
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05004605 isec = inode_security_novalidate(SOCK_INODE(sock));
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004606 spin_lock(&isec->lock);
4607 sclass = isec->sclass;
4608 sid = isec->sid;
4609 spin_unlock(&isec->lock);
4610
4611 newisec = inode_security_novalidate(SOCK_INODE(newsock));
4612 newisec->sclass = sclass;
4613 newisec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004614 newisec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615
4616 return 0;
4617}
4618
4619static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
Eric Paris828dfe12008-04-17 13:17:49 -04004620 int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004622 return sock_has_perm(sock->sk, SOCKET__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623}
4624
4625static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
4626 int size, int flags)
4627{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004628 return sock_has_perm(sock->sk, SOCKET__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629}
4630
4631static int selinux_socket_getsockname(struct socket *sock)
4632{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004633 return sock_has_perm(sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634}
4635
4636static int selinux_socket_getpeername(struct socket *sock)
4637{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004638 return sock_has_perm(sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639}
4640
Eric Paris828dfe12008-04-17 13:17:49 -04004641static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642{
Paul Mooref8687af2006-10-30 15:22:15 -08004643 int err;
4644
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004645 err = sock_has_perm(sock->sk, SOCKET__SETOPT);
Paul Mooref8687af2006-10-30 15:22:15 -08004646 if (err)
4647 return err;
4648
4649 return selinux_netlbl_socket_setsockopt(sock, level, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650}
4651
4652static int selinux_socket_getsockopt(struct socket *sock, int level,
4653 int optname)
4654{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004655 return sock_has_perm(sock->sk, SOCKET__GETOPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656}
4657
4658static int selinux_socket_shutdown(struct socket *sock, int how)
4659{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004660 return sock_has_perm(sock->sk, SOCKET__SHUTDOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661}
4662
David S. Miller3610cda2011-01-05 15:38:53 -08004663static int selinux_socket_unix_stream_connect(struct sock *sock,
4664 struct sock *other,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 struct sock *newsk)
4666{
David S. Miller3610cda2011-01-05 15:38:53 -08004667 struct sk_security_struct *sksec_sock = sock->sk_security;
4668 struct sk_security_struct *sksec_other = other->sk_security;
Paul Moore4d1e2452010-04-22 14:46:18 -04004669 struct sk_security_struct *sksec_new = newsk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004670 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004671 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672 int err;
4673
Eric Paris50c205f2012-04-04 15:01:43 -04004674 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004675 ad.u.net = &net;
4676 ad.u.net->sk = other;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004678 err = avc_has_perm(&selinux_state,
4679 sksec_sock->sid, sksec_other->sid,
Paul Moore4d1e2452010-04-22 14:46:18 -04004680 sksec_other->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 UNIX_STREAM_SOCKET__CONNECTTO, &ad);
4682 if (err)
4683 return err;
4684
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685 /* server child socket */
Paul Moore4d1e2452010-04-22 14:46:18 -04004686 sksec_new->peer_sid = sksec_sock->sid;
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004687 err = security_sid_mls_copy(&selinux_state, sksec_other->sid,
4688 sksec_sock->sid, &sksec_new->sid);
Paul Moore4d1e2452010-04-22 14:46:18 -04004689 if (err)
4690 return err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004691
Paul Moore4d1e2452010-04-22 14:46:18 -04004692 /* connecting socket */
4693 sksec_sock->peer_sid = sksec_new->sid;
4694
4695 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696}
4697
4698static int selinux_socket_unix_may_send(struct socket *sock,
4699 struct socket *other)
4700{
Paul Moore253bfae2010-04-22 14:46:19 -04004701 struct sk_security_struct *ssec = sock->sk->sk_security;
4702 struct sk_security_struct *osec = other->sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004703 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004704 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705
Eric Paris50c205f2012-04-04 15:01:43 -04004706 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004707 ad.u.net = &net;
4708 ad.u.net->sk = other->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004710 return avc_has_perm(&selinux_state,
4711 ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
Paul Moore253bfae2010-04-22 14:46:19 -04004712 &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713}
4714
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004715static int selinux_inet_sys_rcv_skb(struct net *ns, int ifindex,
4716 char *addrp, u16 family, u32 peer_sid,
Thomas Liu2bf49692009-07-14 12:14:09 -04004717 struct common_audit_data *ad)
Paul Mooreeffad8d2008-01-29 08:49:27 -05004718{
4719 int err;
4720 u32 if_sid;
4721 u32 node_sid;
4722
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004723 err = sel_netif_sid(ns, ifindex, &if_sid);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004724 if (err)
4725 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004726 err = avc_has_perm(&selinux_state,
4727 peer_sid, if_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004728 SECCLASS_NETIF, NETIF__INGRESS, ad);
4729 if (err)
4730 return err;
4731
4732 err = sel_netnode_sid(addrp, family, &node_sid);
4733 if (err)
4734 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004735 return avc_has_perm(&selinux_state,
4736 peer_sid, node_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004737 SECCLASS_NODE, NODE__RECVFROM, ad);
4738}
4739
Paul Moore220deb92008-01-29 08:38:23 -05004740static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
Paul Moored8395c82008-10-10 10:16:30 -04004741 u16 family)
Paul Moore220deb92008-01-29 08:38:23 -05004742{
Paul Moore277d3422008-12-31 12:54:11 -05004743 int err = 0;
Paul Moore220deb92008-01-29 08:38:23 -05004744 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004745 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004746 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004747 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04004748 char *addrp;
4749
Eric Paris50c205f2012-04-04 15:01:43 -04004750 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004751 ad.u.net = &net;
4752 ad.u.net->netif = skb->skb_iif;
4753 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004754 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4755 if (err)
4756 return err;
Paul Moore220deb92008-01-29 08:38:23 -05004757
Paul Moore58bfbb52009-03-27 17:10:41 -04004758 if (selinux_secmark_enabled()) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004759 err = avc_has_perm(&selinux_state,
4760 sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Moored8395c82008-10-10 10:16:30 -04004761 PACKET__RECV, &ad);
Paul Moore58bfbb52009-03-27 17:10:41 -04004762 if (err)
4763 return err;
4764 }
Paul Moore220deb92008-01-29 08:38:23 -05004765
Steffen Klassertb9679a72011-02-23 12:55:21 +01004766 err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
4767 if (err)
4768 return err;
4769 err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004770
James Morris4e5ab4c2006-06-09 00:33:33 -07004771 return err;
4772}
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004773
James Morris4e5ab4c2006-06-09 00:33:33 -07004774static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
4775{
Paul Moore220deb92008-01-29 08:38:23 -05004776 int err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004777 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004778 u16 family = sk->sk_family;
4779 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004780 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004781 struct lsm_network_audit net = {0,};
Paul Moore220deb92008-01-29 08:38:23 -05004782 char *addrp;
Paul Moored8395c82008-10-10 10:16:30 -04004783 u8 secmark_active;
4784 u8 peerlbl_active;
James Morris4e5ab4c2006-06-09 00:33:33 -07004785
James Morris4e5ab4c2006-06-09 00:33:33 -07004786 if (family != PF_INET && family != PF_INET6)
Paul Moore220deb92008-01-29 08:38:23 -05004787 return 0;
James Morris4e5ab4c2006-06-09 00:33:33 -07004788
4789 /* Handle mapped IPv4 packets arriving via IPv6 sockets */
Al Viro87fcd702006-12-04 22:00:55 +00004790 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
James Morris4e5ab4c2006-06-09 00:33:33 -07004791 family = PF_INET;
4792
Paul Moored8395c82008-10-10 10:16:30 -04004793 /* If any sort of compatibility mode is enabled then handoff processing
4794 * to the selinux_sock_rcv_skb_compat() function to deal with the
4795 * special handling. We do this in an attempt to keep this function
4796 * as fast and as clean as possible. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004797 if (!selinux_policycap_netpeer())
Paul Moored8395c82008-10-10 10:16:30 -04004798 return selinux_sock_rcv_skb_compat(sk, skb, family);
4799
4800 secmark_active = selinux_secmark_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04004801 peerlbl_active = selinux_peerlbl_enabled();
Paul Moored8395c82008-10-10 10:16:30 -04004802 if (!secmark_active && !peerlbl_active)
4803 return 0;
4804
Eric Paris50c205f2012-04-04 15:01:43 -04004805 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004806 ad.u.net = &net;
4807 ad.u.net->netif = skb->skb_iif;
4808 ad.u.net->family = family;
Paul Moore224dfbd2008-01-29 08:38:13 -05004809 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
James Morris4e5ab4c2006-06-09 00:33:33 -07004810 if (err)
Paul Moore220deb92008-01-29 08:38:23 -05004811 return err;
James Morris4e5ab4c2006-06-09 00:33:33 -07004812
Paul Moored8395c82008-10-10 10:16:30 -04004813 if (peerlbl_active) {
Paul Moored621d352008-01-29 08:43:36 -05004814 u32 peer_sid;
4815
4816 err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
4817 if (err)
4818 return err;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004819 err = selinux_inet_sys_rcv_skb(sock_net(sk), skb->skb_iif,
4820 addrp, family, peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04004821 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04004822 selinux_netlbl_err(skb, family, err, 0);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004823 return err;
Paul Mooredfaebe92008-10-10 10:16:31 -04004824 }
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004825 err = avc_has_perm(&selinux_state,
4826 sk_sid, peer_sid, SECCLASS_PEER,
Paul Moored621d352008-01-29 08:43:36 -05004827 PEER__RECV, &ad);
Chad Hanson46d01d62013-12-23 17:45:01 -05004828 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04004829 selinux_netlbl_err(skb, family, err, 0);
Chad Hanson46d01d62013-12-23 17:45:01 -05004830 return err;
4831 }
Paul Moored621d352008-01-29 08:43:36 -05004832 }
4833
Paul Moored8395c82008-10-10 10:16:30 -04004834 if (secmark_active) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004835 err = avc_has_perm(&selinux_state,
4836 sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004837 PACKET__RECV, &ad);
4838 if (err)
4839 return err;
4840 }
4841
Paul Moored621d352008-01-29 08:43:36 -05004842 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843}
4844
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004845static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
4846 int __user *optlen, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847{
4848 int err = 0;
4849 char *scontext;
4850 u32 scontext_len;
Paul Moore253bfae2010-04-22 14:46:19 -04004851 struct sk_security_struct *sksec = sock->sk->sk_security;
Paul Moore3de4bab2006-11-17 17:38:54 -05004852 u32 peer_sid = SECSID_NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853
Paul Moore253bfae2010-04-22 14:46:19 -04004854 if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
Richard Hainesd4529302018-02-13 20:57:18 +00004855 sksec->sclass == SECCLASS_TCP_SOCKET ||
4856 sksec->sclass == SECCLASS_SCTP_SOCKET)
Eric Parisdd3e7832010-04-07 15:08:46 -04004857 peer_sid = sksec->peer_sid;
Paul Moore253bfae2010-04-22 14:46:19 -04004858 if (peer_sid == SECSID_NULL)
4859 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004861 err = security_sid_to_context(&selinux_state, peer_sid, &scontext,
4862 &scontext_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 if (err)
Paul Moore253bfae2010-04-22 14:46:19 -04004864 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865
4866 if (scontext_len > len) {
4867 err = -ERANGE;
4868 goto out_len;
4869 }
4870
4871 if (copy_to_user(optval, scontext, scontext_len))
4872 err = -EFAULT;
4873
4874out_len:
4875 if (put_user(scontext_len, optlen))
4876 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877 kfree(scontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 return err;
4879}
4880
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004881static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004882{
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004883 u32 peer_secid = SECSID_NULL;
Paul Moore75e22912008-01-29 08:38:04 -05004884 u16 family;
Paul Moore899134f2016-03-28 15:19:10 -04004885 struct inode_security_struct *isec;
Catherine Zhang877ce7c2006-06-29 12:27:47 -07004886
Paul Mooreaa862902008-10-10 10:16:29 -04004887 if (skb && skb->protocol == htons(ETH_P_IP))
4888 family = PF_INET;
4889 else if (skb && skb->protocol == htons(ETH_P_IPV6))
4890 family = PF_INET6;
4891 else if (sock)
Paul Moore75e22912008-01-29 08:38:04 -05004892 family = sock->sk->sk_family;
Paul Moore75e22912008-01-29 08:38:04 -05004893 else
4894 goto out;
4895
Paul Moore899134f2016-03-28 15:19:10 -04004896 if (sock && family == PF_UNIX) {
4897 isec = inode_security_novalidate(SOCK_INODE(sock));
4898 peer_secid = isec->sid;
4899 } else if (skb)
Paul Moore220deb92008-01-29 08:38:23 -05004900 selinux_skb_peerlbl_sid(skb, family, &peer_secid);
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004901
Paul Moore75e22912008-01-29 08:38:04 -05004902out:
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004903 *secid = peer_secid;
Paul Moore75e22912008-01-29 08:38:04 -05004904 if (peer_secid == SECSID_NULL)
4905 return -EINVAL;
4906 return 0;
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004907}
4908
Al Viro7d877f32005-10-21 03:20:43 -04004909static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910{
Paul Moore84914b72010-04-22 14:46:18 -04004911 struct sk_security_struct *sksec;
4912
4913 sksec = kzalloc(sizeof(*sksec), priority);
4914 if (!sksec)
4915 return -ENOMEM;
4916
4917 sksec->peer_sid = SECINITSID_UNLABELED;
4918 sksec->sid = SECINITSID_UNLABELED;
Stephen Smalley5dee25d2015-07-10 17:19:57 -04004919 sksec->sclass = SECCLASS_SOCKET;
Paul Moore84914b72010-04-22 14:46:18 -04004920 selinux_netlbl_sk_security_reset(sksec);
4921 sk->sk_security = sksec;
4922
4923 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924}
4925
4926static void selinux_sk_free_security(struct sock *sk)
4927{
Paul Moore84914b72010-04-22 14:46:18 -04004928 struct sk_security_struct *sksec = sk->sk_security;
4929
4930 sk->sk_security = NULL;
4931 selinux_netlbl_sk_security_free(sksec);
4932 kfree(sksec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933}
4934
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004935static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
4936{
Eric Parisdd3e7832010-04-07 15:08:46 -04004937 struct sk_security_struct *sksec = sk->sk_security;
4938 struct sk_security_struct *newsksec = newsk->sk_security;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004939
Eric Parisdd3e7832010-04-07 15:08:46 -04004940 newsksec->sid = sksec->sid;
4941 newsksec->peer_sid = sksec->peer_sid;
4942 newsksec->sclass = sksec->sclass;
Paul Moore99f59ed2006-08-29 17:53:48 -07004943
Eric Parisdd3e7832010-04-07 15:08:46 -04004944 selinux_netlbl_sk_security_reset(newsksec);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004945}
4946
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07004947static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004948{
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004949 if (!sk)
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07004950 *secid = SECINITSID_ANY_SOCKET;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004951 else {
4952 struct sk_security_struct *sksec = sk->sk_security;
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004953
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07004954 *secid = sksec->sid;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004955 }
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004956}
4957
Eric Paris828dfe12008-04-17 13:17:49 -04004958static void selinux_sock_graft(struct sock *sk, struct socket *parent)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004959{
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05004960 struct inode_security_struct *isec =
4961 inode_security_novalidate(SOCK_INODE(parent));
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004962 struct sk_security_struct *sksec = sk->sk_security;
4963
Paul Moore2873ead2014-07-28 10:42:48 -04004964 if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
4965 sk->sk_family == PF_UNIX)
David Woodhouse2148ccc2006-09-29 15:50:25 -07004966 isec->sid = sksec->sid;
Paul Moore220deb92008-01-29 08:38:23 -05004967 sksec->sclass = isec->sclass;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004968}
4969
Richard Hainesd4529302018-02-13 20:57:18 +00004970/* Called whenever SCTP receives an INIT chunk. This happens when an incoming
4971 * connect(2), sctp_connectx(3) or sctp_sendmsg(3) (with no association
4972 * already present).
4973 */
4974static int selinux_sctp_assoc_request(struct sctp_endpoint *ep,
4975 struct sk_buff *skb)
4976{
4977 struct sk_security_struct *sksec = ep->base.sk->sk_security;
4978 struct common_audit_data ad;
4979 struct lsm_network_audit net = {0,};
4980 u8 peerlbl_active;
4981 u32 peer_sid = SECINITSID_UNLABELED;
4982 u32 conn_sid;
4983 int err = 0;
4984
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004985 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00004986 return 0;
4987
4988 peerlbl_active = selinux_peerlbl_enabled();
4989
4990 if (peerlbl_active) {
4991 /* This will return peer_sid = SECSID_NULL if there are
4992 * no peer labels, see security_net_peersid_resolve().
4993 */
4994 err = selinux_skb_peerlbl_sid(skb, ep->base.sk->sk_family,
4995 &peer_sid);
4996 if (err)
4997 return err;
4998
4999 if (peer_sid == SECSID_NULL)
5000 peer_sid = SECINITSID_UNLABELED;
5001 }
5002
5003 if (sksec->sctp_assoc_state == SCTP_ASSOC_UNSET) {
5004 sksec->sctp_assoc_state = SCTP_ASSOC_SET;
5005
5006 /* Here as first association on socket. As the peer SID
5007 * was allowed by peer recv (and the netif/node checks),
5008 * then it is approved by policy and used as the primary
5009 * peer SID for getpeercon(3).
5010 */
5011 sksec->peer_sid = peer_sid;
5012 } else if (sksec->peer_sid != peer_sid) {
5013 /* Other association peer SIDs are checked to enforce
5014 * consistency among the peer SIDs.
5015 */
5016 ad.type = LSM_AUDIT_DATA_NET;
5017 ad.u.net = &net;
5018 ad.u.net->sk = ep->base.sk;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005019 err = avc_has_perm(&selinux_state,
5020 sksec->peer_sid, peer_sid, sksec->sclass,
Richard Hainesd4529302018-02-13 20:57:18 +00005021 SCTP_SOCKET__ASSOCIATION, &ad);
5022 if (err)
5023 return err;
5024 }
5025
5026 /* Compute the MLS component for the connection and store
5027 * the information in ep. This will be used by SCTP TCP type
5028 * sockets and peeled off connections as they cause a new
5029 * socket to be generated. selinux_sctp_sk_clone() will then
5030 * plug this into the new socket.
5031 */
5032 err = selinux_conn_sid(sksec->sid, peer_sid, &conn_sid);
5033 if (err)
5034 return err;
5035
5036 ep->secid = conn_sid;
5037 ep->peer_secid = peer_sid;
5038
5039 /* Set any NetLabel labels including CIPSO/CALIPSO options. */
5040 return selinux_netlbl_sctp_assoc_request(ep, skb);
5041}
5042
5043/* Check if sctp IPv4/IPv6 addresses are valid for binding or connecting
5044 * based on their @optname.
5045 */
5046static int selinux_sctp_bind_connect(struct sock *sk, int optname,
5047 struct sockaddr *address,
5048 int addrlen)
5049{
5050 int len, err = 0, walk_size = 0;
5051 void *addr_buf;
5052 struct sockaddr *addr;
5053 struct socket *sock;
5054
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005055 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005056 return 0;
5057
5058 /* Process one or more addresses that may be IPv4 or IPv6 */
5059 sock = sk->sk_socket;
5060 addr_buf = address;
5061
5062 while (walk_size < addrlen) {
Ondrej Mosnacekc1383252018-11-13 16:16:08 +01005063 if (walk_size + sizeof(sa_family_t) > addrlen)
5064 return -EINVAL;
5065
Richard Hainesd4529302018-02-13 20:57:18 +00005066 addr = addr_buf;
5067 switch (addr->sa_family) {
Alexey Kodanev4152dc92018-05-11 20:15:13 +03005068 case AF_UNSPEC:
Richard Hainesd4529302018-02-13 20:57:18 +00005069 case AF_INET:
5070 len = sizeof(struct sockaddr_in);
5071 break;
5072 case AF_INET6:
5073 len = sizeof(struct sockaddr_in6);
5074 break;
5075 default:
Alexey Kodanev4152dc92018-05-11 20:15:13 +03005076 return -EINVAL;
Richard Hainesd4529302018-02-13 20:57:18 +00005077 }
5078
5079 err = -EINVAL;
5080 switch (optname) {
5081 /* Bind checks */
5082 case SCTP_PRIMARY_ADDR:
5083 case SCTP_SET_PEER_PRIMARY_ADDR:
5084 case SCTP_SOCKOPT_BINDX_ADD:
5085 err = selinux_socket_bind(sock, addr, len);
5086 break;
5087 /* Connect checks */
5088 case SCTP_SOCKOPT_CONNECTX:
5089 case SCTP_PARAM_SET_PRIMARY:
5090 case SCTP_PARAM_ADD_IP:
5091 case SCTP_SENDMSG_CONNECT:
5092 err = selinux_socket_connect_helper(sock, addr, len);
5093 if (err)
5094 return err;
5095
5096 /* As selinux_sctp_bind_connect() is called by the
5097 * SCTP protocol layer, the socket is already locked,
5098 * therefore selinux_netlbl_socket_connect_locked() is
5099 * is called here. The situations handled are:
5100 * sctp_connectx(3), sctp_sendmsg(3), sendmsg(2),
5101 * whenever a new IP address is added or when a new
5102 * primary address is selected.
5103 * Note that an SCTP connect(2) call happens before
5104 * the SCTP protocol layer and is handled via
5105 * selinux_socket_connect().
5106 */
5107 err = selinux_netlbl_socket_connect_locked(sk, addr);
5108 break;
5109 }
5110
5111 if (err)
5112 return err;
5113
5114 addr_buf += len;
5115 walk_size += len;
5116 }
5117
5118 return 0;
5119}
5120
5121/* Called whenever a new socket is created by accept(2) or sctp_peeloff(3). */
5122static void selinux_sctp_sk_clone(struct sctp_endpoint *ep, struct sock *sk,
5123 struct sock *newsk)
5124{
5125 struct sk_security_struct *sksec = sk->sk_security;
5126 struct sk_security_struct *newsksec = newsk->sk_security;
5127
5128 /* If policy does not support SECCLASS_SCTP_SOCKET then call
5129 * the non-sctp clone version.
5130 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005131 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005132 return selinux_sk_clone_security(sk, newsk);
5133
5134 newsksec->sid = ep->secid;
5135 newsksec->peer_sid = ep->peer_secid;
5136 newsksec->sclass = sksec->sclass;
5137 selinux_netlbl_sctp_sk_clone(sk, newsk);
5138}
5139
Adrian Bunk9a673e52006-08-15 00:03:53 -07005140static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
5141 struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005142{
5143 struct sk_security_struct *sksec = sk->sk_security;
5144 int err;
Paul Moore0b1f24e2013-12-03 11:39:13 -05005145 u16 family = req->rsk_ops->family;
Paul Moore446b8022013-12-04 16:10:51 -05005146 u32 connsid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005147 u32 peersid;
5148
Paul Mooreaa862902008-10-10 10:16:29 -04005149 err = selinux_skb_peerlbl_sid(skb, family, &peersid);
Paul Moore220deb92008-01-29 08:38:23 -05005150 if (err)
5151 return err;
Paul Moore446b8022013-12-04 16:10:51 -05005152 err = selinux_conn_sid(sksec->sid, peersid, &connsid);
5153 if (err)
5154 return err;
5155 req->secid = connsid;
5156 req->peer_secid = peersid;
Venkat Yekkiralaa51c64f2006-07-27 22:01:34 -07005157
Paul Moore389fb8002009-03-27 17:10:34 -04005158 return selinux_netlbl_inet_conn_request(req, family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005159}
5160
Adrian Bunk9a673e52006-08-15 00:03:53 -07005161static void selinux_inet_csk_clone(struct sock *newsk,
5162 const struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005163{
5164 struct sk_security_struct *newsksec = newsk->sk_security;
5165
5166 newsksec->sid = req->secid;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005167 newsksec->peer_sid = req->peer_secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005168 /* NOTE: Ideally, we should also get the isec->sid for the
5169 new socket in sync, but we don't have the isec available yet.
5170 So we will wait until sock_graft to do it, by which
5171 time it will have been created and available. */
Paul Moore99f59ed2006-08-29 17:53:48 -07005172
Paul Moore9f2ad662006-11-17 17:38:53 -05005173 /* We don't need to take any sort of lock here as we are the only
5174 * thread with access to newsksec */
Paul Moore389fb8002009-03-27 17:10:34 -04005175 selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005176}
5177
Paul Moore014ab192008-10-10 10:16:33 -04005178static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005179{
Paul Mooreaa862902008-10-10 10:16:29 -04005180 u16 family = sk->sk_family;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005181 struct sk_security_struct *sksec = sk->sk_security;
5182
Paul Mooreaa862902008-10-10 10:16:29 -04005183 /* handle mapped IPv4 packets arriving via IPv6 sockets */
5184 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
5185 family = PF_INET;
5186
5187 selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005188}
5189
Eric Paris2606fd12010-10-13 16:24:41 -04005190static int selinux_secmark_relabel_packet(u32 sid)
5191{
5192 const struct task_security_struct *__tsec;
5193 u32 tsid;
5194
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07005195 __tsec = selinux_cred(current_cred());
Eric Paris2606fd12010-10-13 16:24:41 -04005196 tsid = __tsec->sid;
5197
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005198 return avc_has_perm(&selinux_state,
5199 tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO,
5200 NULL);
Eric Paris2606fd12010-10-13 16:24:41 -04005201}
5202
5203static void selinux_secmark_refcount_inc(void)
5204{
5205 atomic_inc(&selinux_secmark_refcount);
5206}
5207
5208static void selinux_secmark_refcount_dec(void)
5209{
5210 atomic_dec(&selinux_secmark_refcount);
5211}
5212
Adrian Bunk9a673e52006-08-15 00:03:53 -07005213static void selinux_req_classify_flow(const struct request_sock *req,
5214 struct flowi *fl)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005215{
David S. Miller1d28f422011-03-12 00:29:39 -05005216 fl->flowi_secid = req->secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005217}
5218
Paul Moore5dbbaf22013-01-14 07:12:19 +00005219static int selinux_tun_dev_alloc_security(void **security)
5220{
5221 struct tun_security_struct *tunsec;
5222
5223 tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL);
5224 if (!tunsec)
5225 return -ENOMEM;
5226 tunsec->sid = current_sid();
5227
5228 *security = tunsec;
5229 return 0;
5230}
5231
5232static void selinux_tun_dev_free_security(void *security)
5233{
5234 kfree(security);
5235}
5236
Paul Mooreed6d76e2009-08-28 18:12:49 -04005237static int selinux_tun_dev_create(void)
5238{
5239 u32 sid = current_sid();
5240
5241 /* we aren't taking into account the "sockcreate" SID since the socket
5242 * that is being created here is not a socket in the traditional sense,
5243 * instead it is a private sock, accessible only to the kernel, and
5244 * representing a wide range of network traffic spanning multiple
5245 * connections unlike traditional sockets - check the TUN driver to
5246 * get a better understanding of why this socket is special */
5247
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005248 return avc_has_perm(&selinux_state,
5249 sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005250 NULL);
5251}
5252
Paul Moore5dbbaf22013-01-14 07:12:19 +00005253static int selinux_tun_dev_attach_queue(void *security)
Paul Mooreed6d76e2009-08-28 18:12:49 -04005254{
Paul Moore5dbbaf22013-01-14 07:12:19 +00005255 struct tun_security_struct *tunsec = security;
5256
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005257 return avc_has_perm(&selinux_state,
5258 current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET,
Paul Moore5dbbaf22013-01-14 07:12:19 +00005259 TUN_SOCKET__ATTACH_QUEUE, NULL);
5260}
5261
5262static int selinux_tun_dev_attach(struct sock *sk, void *security)
5263{
5264 struct tun_security_struct *tunsec = security;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005265 struct sk_security_struct *sksec = sk->sk_security;
5266
5267 /* we don't currently perform any NetLabel based labeling here and it
5268 * isn't clear that we would want to do so anyway; while we could apply
5269 * labeling without the support of the TUN user the resulting labeled
5270 * traffic from the other end of the connection would almost certainly
5271 * cause confusion to the TUN user that had no idea network labeling
5272 * protocols were being used */
5273
Paul Moore5dbbaf22013-01-14 07:12:19 +00005274 sksec->sid = tunsec->sid;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005275 sksec->sclass = SECCLASS_TUN_SOCKET;
Paul Moore5dbbaf22013-01-14 07:12:19 +00005276
5277 return 0;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005278}
5279
Paul Moore5dbbaf22013-01-14 07:12:19 +00005280static int selinux_tun_dev_open(void *security)
Paul Mooreed6d76e2009-08-28 18:12:49 -04005281{
Paul Moore5dbbaf22013-01-14 07:12:19 +00005282 struct tun_security_struct *tunsec = security;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005283 u32 sid = current_sid();
5284 int err;
5285
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005286 err = avc_has_perm(&selinux_state,
5287 sid, tunsec->sid, SECCLASS_TUN_SOCKET,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005288 TUN_SOCKET__RELABELFROM, NULL);
5289 if (err)
5290 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005291 err = avc_has_perm(&selinux_state,
5292 sid, sid, SECCLASS_TUN_SOCKET,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005293 TUN_SOCKET__RELABELTO, NULL);
5294 if (err)
5295 return err;
Paul Moore5dbbaf22013-01-14 07:12:19 +00005296 tunsec->sid = sid;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005297
5298 return 0;
5299}
5300
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
5302{
5303 int err = 0;
5304 u32 perm;
5305 struct nlmsghdr *nlh;
Paul Moore253bfae2010-04-22 14:46:19 -04005306 struct sk_security_struct *sksec = sk->sk_security;
Eric Paris828dfe12008-04-17 13:17:49 -04005307
Hong zhi guo77954982013-03-27 06:49:35 +00005308 if (skb->len < NLMSG_HDRLEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309 err = -EINVAL;
5310 goto out;
5311 }
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -07005312 nlh = nlmsg_hdr(skb);
Eric Paris828dfe12008-04-17 13:17:49 -04005313
Paul Moore253bfae2010-04-22 14:46:19 -04005314 err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005315 if (err) {
5316 if (err == -EINVAL) {
Vladis Dronov76319942015-12-24 11:09:41 -05005317 pr_warn_ratelimited("SELinux: unrecognized netlink"
5318 " message: protocol=%hu nlmsg_type=%hu sclass=%s"
5319 " pig=%d comm=%s\n",
Marek Milkoviccded3ff2015-06-04 16:22:16 -04005320 sk->sk_protocol, nlh->nlmsg_type,
Vladis Dronov76319942015-12-24 11:09:41 -05005321 secclass_map[sksec->sclass - 1].name,
5322 task_pid_nr(current), current->comm);
Paul Mooree5a5ca92018-03-01 17:38:30 -05005323 if (!enforcing_enabled(&selinux_state) ||
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005324 security_get_allow_unknown(&selinux_state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325 err = 0;
5326 }
5327
5328 /* Ignore */
5329 if (err == -ENOENT)
5330 err = 0;
5331 goto out;
5332 }
5333
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005334 err = sock_has_perm(sk, perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335out:
5336 return err;
5337}
5338
5339#ifdef CONFIG_NETFILTER
5340
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005341static unsigned int selinux_ip_forward(struct sk_buff *skb,
5342 const struct net_device *indev,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005343 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344{
Paul Mooredfaebe92008-10-10 10:16:31 -04005345 int err;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005346 char *addrp;
5347 u32 peer_sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04005348 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005349 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05005350 u8 secmark_active;
Paul Moore948bf852008-10-10 10:16:32 -04005351 u8 netlbl_active;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005352 u8 peerlbl_active;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005353
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005354 if (!selinux_policycap_netpeer())
Paul Mooreeffad8d2008-01-29 08:49:27 -05005355 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005356
Paul Mooreeffad8d2008-01-29 08:49:27 -05005357 secmark_active = selinux_secmark_enabled();
Paul Moore948bf852008-10-10 10:16:32 -04005358 netlbl_active = netlbl_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04005359 peerlbl_active = selinux_peerlbl_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05005360 if (!secmark_active && !peerlbl_active)
5361 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005362
Paul Moored8395c82008-10-10 10:16:30 -04005363 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
5364 return NF_DROP;
5365
Eric Paris50c205f2012-04-04 15:01:43 -04005366 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005367 ad.u.net = &net;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005368 ad.u.net->netif = indev->ifindex;
Eric Paris48c62af2012-04-02 13:15:44 -04005369 ad.u.net->family = family;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005370 if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
5371 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372
Paul Mooredfaebe92008-10-10 10:16:31 -04005373 if (peerlbl_active) {
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005374 err = selinux_inet_sys_rcv_skb(dev_net(indev), indev->ifindex,
5375 addrp, family, peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04005376 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04005377 selinux_netlbl_err(skb, family, err, 1);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005378 return NF_DROP;
Paul Mooredfaebe92008-10-10 10:16:31 -04005379 }
5380 }
Paul Mooreeffad8d2008-01-29 08:49:27 -05005381
5382 if (secmark_active)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005383 if (avc_has_perm(&selinux_state,
5384 peer_sid, skb->secmark,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005385 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
5386 return NF_DROP;
5387
Paul Moore948bf852008-10-10 10:16:32 -04005388 if (netlbl_active)
5389 /* we do this in the FORWARD path and not the POST_ROUTING
5390 * path because we want to make sure we apply the necessary
5391 * labeling before IPsec is applied so we can leverage AH
5392 * protection */
5393 if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
5394 return NF_DROP;
5395
Paul Mooreeffad8d2008-01-29 08:49:27 -05005396 return NF_ACCEPT;
5397}
5398
Eric W. Biederman06198b32015-09-18 14:33:06 -05005399static unsigned int selinux_ipv4_forward(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005400 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005401 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005402{
David S. Miller238e54c2015-04-03 20:32:56 -04005403 return selinux_ip_forward(skb, state->in, PF_INET);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005404}
5405
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005406#if IS_ENABLED(CONFIG_IPV6)
Eric W. Biederman06198b32015-09-18 14:33:06 -05005407static unsigned int selinux_ipv6_forward(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005408 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005409 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005410{
David S. Miller238e54c2015-04-03 20:32:56 -04005411 return selinux_ip_forward(skb, state->in, PF_INET6);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005412}
5413#endif /* IPV6 */
5414
Paul Moore948bf852008-10-10 10:16:32 -04005415static unsigned int selinux_ip_output(struct sk_buff *skb,
5416 u16 family)
5417{
Paul Moore47180062013-12-04 16:10:45 -05005418 struct sock *sk;
Paul Moore948bf852008-10-10 10:16:32 -04005419 u32 sid;
5420
5421 if (!netlbl_enabled())
5422 return NF_ACCEPT;
5423
5424 /* we do this in the LOCAL_OUT path and not the POST_ROUTING path
5425 * because we want to make sure we apply the necessary labeling
5426 * before IPsec is applied so we can leverage AH protection */
Paul Moore47180062013-12-04 16:10:45 -05005427 sk = skb->sk;
5428 if (sk) {
5429 struct sk_security_struct *sksec;
5430
Eric Dumazete446f9d2015-10-08 05:01:55 -07005431 if (sk_listener(sk))
Paul Moore47180062013-12-04 16:10:45 -05005432 /* if the socket is the listening state then this
5433 * packet is a SYN-ACK packet which means it needs to
5434 * be labeled based on the connection/request_sock and
5435 * not the parent socket. unfortunately, we can't
5436 * lookup the request_sock yet as it isn't queued on
5437 * the parent socket until after the SYN-ACK is sent.
5438 * the "solution" is to simply pass the packet as-is
5439 * as any IP option based labeling should be copied
5440 * from the initial connection request (in the IP
5441 * layer). it is far from ideal, but until we get a
5442 * security label in the packet itself this is the
5443 * best we can do. */
5444 return NF_ACCEPT;
5445
5446 /* standard practice, label using the parent socket */
5447 sksec = sk->sk_security;
Paul Moore948bf852008-10-10 10:16:32 -04005448 sid = sksec->sid;
5449 } else
5450 sid = SECINITSID_KERNEL;
5451 if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
5452 return NF_DROP;
5453
5454 return NF_ACCEPT;
5455}
5456
Eric W. Biederman06198b32015-09-18 14:33:06 -05005457static unsigned int selinux_ipv4_output(void *priv,
Paul Moore948bf852008-10-10 10:16:32 -04005458 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005459 const struct nf_hook_state *state)
Paul Moore948bf852008-10-10 10:16:32 -04005460{
5461 return selinux_ip_output(skb, PF_INET);
5462}
5463
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005464#if IS_ENABLED(CONFIG_IPV6)
Huw Davies2917f572016-06-27 15:06:15 -04005465static unsigned int selinux_ipv6_output(void *priv,
5466 struct sk_buff *skb,
5467 const struct nf_hook_state *state)
5468{
5469 return selinux_ip_output(skb, PF_INET6);
5470}
5471#endif /* IPV6 */
5472
Paul Mooreeffad8d2008-01-29 08:49:27 -05005473static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
5474 int ifindex,
Paul Moored8395c82008-10-10 10:16:30 -04005475 u16 family)
James Morris4e5ab4c2006-06-09 00:33:33 -07005476{
Eric Dumazet54abc682015-11-08 10:54:07 -08005477 struct sock *sk = skb_to_full_sk(skb);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005478 struct sk_security_struct *sksec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005479 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005480 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04005481 char *addrp;
5482 u8 proto;
James Morris4e5ab4c2006-06-09 00:33:33 -07005483
Paul Mooreeffad8d2008-01-29 08:49:27 -05005484 if (sk == NULL)
5485 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005486 sksec = sk->sk_security;
James Morris4e5ab4c2006-06-09 00:33:33 -07005487
Eric Paris50c205f2012-04-04 15:01:43 -04005488 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005489 ad.u.net = &net;
5490 ad.u.net->netif = ifindex;
5491 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04005492 if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
5493 return NF_DROP;
5494
Paul Moore58bfbb52009-03-27 17:10:41 -04005495 if (selinux_secmark_enabled())
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005496 if (avc_has_perm(&selinux_state,
5497 sksec->sid, skb->secmark,
Paul Moored8395c82008-10-10 10:16:30 -04005498 SECCLASS_PACKET, PACKET__SEND, &ad))
Eric Paris2fe66ec2010-11-23 06:28:08 +00005499 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07005500
Steffen Klassertb9679a72011-02-23 12:55:21 +01005501 if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
5502 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07005503
Paul Mooreeffad8d2008-01-29 08:49:27 -05005504 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505}
5506
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005507static unsigned int selinux_ip_postroute(struct sk_buff *skb,
5508 const struct net_device *outdev,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005509 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510{
Paul Mooreeffad8d2008-01-29 08:49:27 -05005511 u32 secmark_perm;
5512 u32 peer_sid;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005513 int ifindex = outdev->ifindex;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005514 struct sock *sk;
Thomas Liu2bf49692009-07-14 12:14:09 -04005515 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005516 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05005517 char *addrp;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005518 u8 secmark_active;
5519 u8 peerlbl_active;
5520
Paul Mooreeffad8d2008-01-29 08:49:27 -05005521 /* If any sort of compatibility mode is enabled then handoff processing
5522 * to the selinux_ip_postroute_compat() function to deal with the
5523 * special handling. We do this in an attempt to keep this function
5524 * as fast and as clean as possible. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005525 if (!selinux_policycap_netpeer())
Paul Moored8395c82008-10-10 10:16:30 -04005526 return selinux_ip_postroute_compat(skb, ifindex, family);
Paul Moorec0828e52013-12-10 14:58:01 -05005527
Paul Mooreeffad8d2008-01-29 08:49:27 -05005528 secmark_active = selinux_secmark_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04005529 peerlbl_active = selinux_peerlbl_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05005530 if (!secmark_active && !peerlbl_active)
5531 return NF_ACCEPT;
5532
Eric Dumazet54abc682015-11-08 10:54:07 -08005533 sk = skb_to_full_sk(skb);
Paul Moorec0828e52013-12-10 14:58:01 -05005534
Paul Mooreeffad8d2008-01-29 08:49:27 -05005535#ifdef CONFIG_XFRM
5536 /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
5537 * packet transformation so allow the packet to pass without any checks
5538 * since we'll have another chance to perform access control checks
5539 * when the packet is on it's final way out.
5540 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
Paul Moorec0828e52013-12-10 14:58:01 -05005541 * is NULL, in this case go ahead and apply access control.
5542 * NOTE: if this is a local socket (skb->sk != NULL) that is in the
5543 * TCP listening state we cannot wait until the XFRM processing
5544 * is done as we will miss out on the SA label if we do;
5545 * unfortunately, this means more work, but it is only once per
5546 * connection. */
5547 if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL &&
Eric Dumazete446f9d2015-10-08 05:01:55 -07005548 !(sk && sk_listener(sk)))
Paul Mooreeffad8d2008-01-29 08:49:27 -05005549 return NF_ACCEPT;
5550#endif
Paul Mooreeffad8d2008-01-29 08:49:27 -05005551
Paul Moored8395c82008-10-10 10:16:30 -04005552 if (sk == NULL) {
Paul Moore446b8022013-12-04 16:10:51 -05005553 /* Without an associated socket the packet is either coming
5554 * from the kernel or it is being forwarded; check the packet
5555 * to determine which and if the packet is being forwarded
5556 * query the packet directly to determine the security label. */
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005557 if (skb->skb_iif) {
5558 secmark_perm = PACKET__FORWARD_OUT;
Paul Moored8395c82008-10-10 10:16:30 -04005559 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005560 return NF_DROP;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005561 } else {
5562 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04005563 peer_sid = SECINITSID_KERNEL;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005564 }
Eric Dumazete446f9d2015-10-08 05:01:55 -07005565 } else if (sk_listener(sk)) {
Paul Moore446b8022013-12-04 16:10:51 -05005566 /* Locally generated packet but the associated socket is in the
5567 * listening state which means this is a SYN-ACK packet. In
5568 * this particular case the correct security label is assigned
5569 * to the connection/request_sock but unfortunately we can't
5570 * query the request_sock as it isn't queued on the parent
5571 * socket until after the SYN-ACK packet is sent; the only
5572 * viable choice is to regenerate the label like we do in
5573 * selinux_inet_conn_request(). See also selinux_ip_output()
5574 * for similar problems. */
5575 u32 skb_sid;
Eric Dumazete446f9d2015-10-08 05:01:55 -07005576 struct sk_security_struct *sksec;
5577
Eric Dumazete446f9d2015-10-08 05:01:55 -07005578 sksec = sk->sk_security;
Paul Moore446b8022013-12-04 16:10:51 -05005579 if (selinux_skb_peerlbl_sid(skb, family, &skb_sid))
5580 return NF_DROP;
Paul Moorec0828e52013-12-10 14:58:01 -05005581 /* At this point, if the returned skb peerlbl is SECSID_NULL
5582 * and the packet has been through at least one XFRM
5583 * transformation then we must be dealing with the "final"
5584 * form of labeled IPsec packet; since we've already applied
5585 * all of our access controls on this packet we can safely
5586 * pass the packet. */
5587 if (skb_sid == SECSID_NULL) {
5588 switch (family) {
5589 case PF_INET:
5590 if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
5591 return NF_ACCEPT;
5592 break;
5593 case PF_INET6:
5594 if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED)
5595 return NF_ACCEPT;
Paul Moorea7a91a12014-09-03 10:51:59 -04005596 break;
Paul Moorec0828e52013-12-10 14:58:01 -05005597 default:
5598 return NF_DROP_ERR(-ECONNREFUSED);
5599 }
5600 }
Paul Moore446b8022013-12-04 16:10:51 -05005601 if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid))
5602 return NF_DROP;
5603 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04005604 } else {
Paul Moore446b8022013-12-04 16:10:51 -05005605 /* Locally generated packet, fetch the security label from the
5606 * associated socket. */
Paul Mooreeffad8d2008-01-29 08:49:27 -05005607 struct sk_security_struct *sksec = sk->sk_security;
5608 peer_sid = sksec->sid;
5609 secmark_perm = PACKET__SEND;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005610 }
5611
Eric Paris50c205f2012-04-04 15:01:43 -04005612 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005613 ad.u.net = &net;
5614 ad.u.net->netif = ifindex;
5615 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04005616 if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
Eric Paris04f6d702010-11-23 06:28:02 +00005617 return NF_DROP;
Paul Moored8395c82008-10-10 10:16:30 -04005618
Paul Mooreeffad8d2008-01-29 08:49:27 -05005619 if (secmark_active)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005620 if (avc_has_perm(&selinux_state,
5621 peer_sid, skb->secmark,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005622 SECCLASS_PACKET, secmark_perm, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005623 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005624
5625 if (peerlbl_active) {
5626 u32 if_sid;
5627 u32 node_sid;
5628
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005629 if (sel_netif_sid(dev_net(outdev), ifindex, &if_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005630 return NF_DROP;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005631 if (avc_has_perm(&selinux_state,
5632 peer_sid, if_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005633 SECCLASS_NETIF, NETIF__EGRESS, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005634 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005635
5636 if (sel_netnode_sid(addrp, family, &node_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005637 return NF_DROP;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005638 if (avc_has_perm(&selinux_state,
5639 peer_sid, node_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005640 SECCLASS_NODE, NODE__SENDTO, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005641 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005642 }
5643
5644 return NF_ACCEPT;
5645}
5646
Eric W. Biederman06198b32015-09-18 14:33:06 -05005647static unsigned int selinux_ipv4_postroute(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005648 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005649 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005650{
David S. Miller238e54c2015-04-03 20:32:56 -04005651 return selinux_ip_postroute(skb, state->out, PF_INET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652}
5653
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005654#if IS_ENABLED(CONFIG_IPV6)
Eric W. Biederman06198b32015-09-18 14:33:06 -05005655static unsigned int selinux_ipv6_postroute(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005656 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005657 const struct nf_hook_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658{
David S. Miller238e54c2015-04-03 20:32:56 -04005659 return selinux_ip_postroute(skb, state->out, PF_INET6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661#endif /* IPV6 */
5662
5663#endif /* CONFIG_NETFILTER */
5664
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
5666{
Stephen Smalley941fc5b2009-10-01 14:48:23 -04005667 return selinux_nlmsg_perm(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668}
5669
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005670static int ipc_alloc_security(struct kern_ipc_perm *perm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671 u16 sclass)
5672{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673 struct ipc_security_struct *isec;
5674
James Morris89d155e2005-10-30 14:59:21 -08005675 isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676 if (!isec)
5677 return -ENOMEM;
5678
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679 isec->sclass = sclass;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005680 isec->sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681 perm->security = isec;
5682
5683 return 0;
5684}
5685
5686static void ipc_free_security(struct kern_ipc_perm *perm)
5687{
5688 struct ipc_security_struct *isec = perm->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689 perm->security = NULL;
5690 kfree(isec);
5691}
5692
5693static int msg_msg_alloc_security(struct msg_msg *msg)
5694{
5695 struct msg_security_struct *msec;
5696
James Morris89d155e2005-10-30 14:59:21 -08005697 msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005698 if (!msec)
5699 return -ENOMEM;
5700
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701 msec->sid = SECINITSID_UNLABELED;
5702 msg->security = msec;
5703
5704 return 0;
5705}
5706
5707static void msg_msg_free_security(struct msg_msg *msg)
5708{
5709 struct msg_security_struct *msec = msg->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710
5711 msg->security = NULL;
5712 kfree(msec);
5713}
5714
5715static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
Stephen Smalley6af963f2005-05-01 08:58:39 -07005716 u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005719 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005720 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722 isec = ipc_perms->security;
5723
Eric Paris50c205f2012-04-04 15:01:43 -04005724 ad.type = LSM_AUDIT_DATA_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725 ad.u.ipc_id = ipc_perms->key;
5726
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005727 return avc_has_perm(&selinux_state,
5728 sid, isec->sid, isec->sclass, perms, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729}
5730
5731static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
5732{
5733 return msg_msg_alloc_security(msg);
5734}
5735
5736static void selinux_msg_msg_free_security(struct msg_msg *msg)
5737{
5738 msg_msg_free_security(msg);
5739}
5740
5741/* message queue security operations */
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005742static int selinux_msg_queue_alloc_security(struct kern_ipc_perm *msq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005745 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005746 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747 int rc;
5748
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005749 rc = ipc_alloc_security(msq, SECCLASS_MSGQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750 if (rc)
5751 return rc;
5752
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005753 isec = msq->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754
Eric Paris50c205f2012-04-04 15:01:43 -04005755 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005756 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005758 rc = avc_has_perm(&selinux_state,
5759 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005760 MSGQ__CREATE, &ad);
5761 if (rc) {
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005762 ipc_free_security(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763 return rc;
5764 }
5765 return 0;
5766}
5767
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005768static void selinux_msg_queue_free_security(struct kern_ipc_perm *msq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769{
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005770 ipc_free_security(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771}
5772
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005773static int selinux_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005776 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005777 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005779 isec = msq->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780
Eric Paris50c205f2012-04-04 15:01:43 -04005781 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005782 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005783
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005784 return avc_has_perm(&selinux_state,
5785 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005786 MSGQ__ASSOCIATE, &ad);
5787}
5788
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005789static int selinux_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790{
5791 int err;
5792 int perms;
5793
Eric Paris828dfe12008-04-17 13:17:49 -04005794 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005795 case IPC_INFO:
5796 case MSG_INFO:
5797 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005798 return avc_has_perm(&selinux_state,
5799 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005800 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801 case IPC_STAT:
5802 case MSG_STAT:
Davidlohr Bueso23c8cec2018-04-10 16:35:30 -07005803 case MSG_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804 perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
5805 break;
5806 case IPC_SET:
5807 perms = MSGQ__SETATTR;
5808 break;
5809 case IPC_RMID:
5810 perms = MSGQ__DESTROY;
5811 break;
5812 default:
5813 return 0;
5814 }
5815
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005816 err = ipc_has_perm(msq, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817 return err;
5818}
5819
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005820static int selinux_msg_queue_msgsnd(struct kern_ipc_perm *msq, struct msg_msg *msg, int msqflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822 struct ipc_security_struct *isec;
5823 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005824 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005825 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826 int rc;
5827
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005828 isec = msq->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005829 msec = msg->security;
5830
5831 /*
5832 * First time through, need to assign label to the message
5833 */
5834 if (msec->sid == SECINITSID_UNLABELED) {
5835 /*
5836 * Compute new sid based on current process and
5837 * message queue this message will be stored in
5838 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005839 rc = security_transition_sid(&selinux_state, sid, isec->sid,
5840 SECCLASS_MSG, NULL, &msec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841 if (rc)
5842 return rc;
5843 }
5844
Eric Paris50c205f2012-04-04 15:01:43 -04005845 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005846 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005847
5848 /* Can this process write to the queue? */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005849 rc = avc_has_perm(&selinux_state,
5850 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851 MSGQ__WRITE, &ad);
5852 if (!rc)
5853 /* Can this process send the message */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005854 rc = avc_has_perm(&selinux_state,
5855 sid, msec->sid, SECCLASS_MSG,
David Howells275bb412008-11-14 10:39:19 +11005856 MSG__SEND, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857 if (!rc)
5858 /* Can the message be put in the queue? */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005859 rc = avc_has_perm(&selinux_state,
5860 msec->sid, isec->sid, SECCLASS_MSGQ,
David Howells275bb412008-11-14 10:39:19 +11005861 MSGQ__ENQUEUE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862
5863 return rc;
5864}
5865
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005866static int selinux_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867 struct task_struct *target,
5868 long type, int mode)
5869{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870 struct ipc_security_struct *isec;
5871 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005872 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005873 u32 sid = task_sid(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874 int rc;
5875
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005876 isec = msq->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877 msec = msg->security;
5878
Eric Paris50c205f2012-04-04 15:01:43 -04005879 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005880 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005881
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005882 rc = avc_has_perm(&selinux_state,
5883 sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005884 SECCLASS_MSGQ, MSGQ__READ, &ad);
5885 if (!rc)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005886 rc = avc_has_perm(&selinux_state,
5887 sid, msec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888 SECCLASS_MSG, MSG__RECEIVE, &ad);
5889 return rc;
5890}
5891
5892/* Shared Memory security operations */
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005893static int selinux_shm_alloc_security(struct kern_ipc_perm *shp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005896 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005897 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898 int rc;
5899
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005900 rc = ipc_alloc_security(shp, SECCLASS_SHM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005901 if (rc)
5902 return rc;
5903
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005904 isec = shp->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905
Eric Paris50c205f2012-04-04 15:01:43 -04005906 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005907 ad.u.ipc_id = shp->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005909 rc = avc_has_perm(&selinux_state,
5910 sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005911 SHM__CREATE, &ad);
5912 if (rc) {
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005913 ipc_free_security(shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914 return rc;
5915 }
5916 return 0;
5917}
5918
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005919static void selinux_shm_free_security(struct kern_ipc_perm *shp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005920{
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005921 ipc_free_security(shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922}
5923
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005924static int selinux_shm_associate(struct kern_ipc_perm *shp, int shmflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005925{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005926 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005927 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005928 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005929
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005930 isec = shp->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931
Eric Paris50c205f2012-04-04 15:01:43 -04005932 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005933 ad.u.ipc_id = shp->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005935 return avc_has_perm(&selinux_state,
5936 sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005937 SHM__ASSOCIATE, &ad);
5938}
5939
5940/* Note, at this point, shp is locked down */
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005941static int selinux_shm_shmctl(struct kern_ipc_perm *shp, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005942{
5943 int perms;
5944 int err;
5945
Eric Paris828dfe12008-04-17 13:17:49 -04005946 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005947 case IPC_INFO:
5948 case SHM_INFO:
5949 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005950 return avc_has_perm(&selinux_state,
5951 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005952 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005953 case IPC_STAT:
5954 case SHM_STAT:
Davidlohr Buesoc21a6972018-04-10 16:35:23 -07005955 case SHM_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005956 perms = SHM__GETATTR | SHM__ASSOCIATE;
5957 break;
5958 case IPC_SET:
5959 perms = SHM__SETATTR;
5960 break;
5961 case SHM_LOCK:
5962 case SHM_UNLOCK:
5963 perms = SHM__LOCK;
5964 break;
5965 case IPC_RMID:
5966 perms = SHM__DESTROY;
5967 break;
5968 default:
5969 return 0;
5970 }
5971
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005972 err = ipc_has_perm(shp, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005973 return err;
5974}
5975
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005976static int selinux_shm_shmat(struct kern_ipc_perm *shp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977 char __user *shmaddr, int shmflg)
5978{
5979 u32 perms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005980
5981 if (shmflg & SHM_RDONLY)
5982 perms = SHM__READ;
5983 else
5984 perms = SHM__READ | SHM__WRITE;
5985
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005986 return ipc_has_perm(shp, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987}
5988
5989/* Semaphore security operations */
Eric W. Biedermanaefad952018-03-22 20:52:43 -05005990static int selinux_sem_alloc_security(struct kern_ipc_perm *sma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005992 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005993 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005994 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005995 int rc;
5996
Eric W. Biedermanaefad952018-03-22 20:52:43 -05005997 rc = ipc_alloc_security(sma, SECCLASS_SEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998 if (rc)
5999 return rc;
6000
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006001 isec = sma->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002
Eric Paris50c205f2012-04-04 15:01:43 -04006003 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006004 ad.u.ipc_id = sma->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006006 rc = avc_has_perm(&selinux_state,
6007 sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006008 SEM__CREATE, &ad);
6009 if (rc) {
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006010 ipc_free_security(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011 return rc;
6012 }
6013 return 0;
6014}
6015
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006016static void selinux_sem_free_security(struct kern_ipc_perm *sma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006017{
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006018 ipc_free_security(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019}
6020
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006021static int selinux_sem_associate(struct kern_ipc_perm *sma, int semflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006023 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006024 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006025 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006027 isec = sma->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028
Eric Paris50c205f2012-04-04 15:01:43 -04006029 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006030 ad.u.ipc_id = sma->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006031
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006032 return avc_has_perm(&selinux_state,
6033 sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034 SEM__ASSOCIATE, &ad);
6035}
6036
6037/* Note, at this point, sma is locked down */
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006038static int selinux_sem_semctl(struct kern_ipc_perm *sma, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039{
6040 int err;
6041 u32 perms;
6042
Eric Paris828dfe12008-04-17 13:17:49 -04006043 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044 case IPC_INFO:
6045 case SEM_INFO:
6046 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006047 return avc_has_perm(&selinux_state,
6048 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006049 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050 case GETPID:
6051 case GETNCNT:
6052 case GETZCNT:
6053 perms = SEM__GETATTR;
6054 break;
6055 case GETVAL:
6056 case GETALL:
6057 perms = SEM__READ;
6058 break;
6059 case SETVAL:
6060 case SETALL:
6061 perms = SEM__WRITE;
6062 break;
6063 case IPC_RMID:
6064 perms = SEM__DESTROY;
6065 break;
6066 case IPC_SET:
6067 perms = SEM__SETATTR;
6068 break;
6069 case IPC_STAT:
6070 case SEM_STAT:
Davidlohr Buesoa280d6d2018-04-10 16:35:26 -07006071 case SEM_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072 perms = SEM__GETATTR | SEM__ASSOCIATE;
6073 break;
6074 default:
6075 return 0;
6076 }
6077
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006078 err = ipc_has_perm(sma, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079 return err;
6080}
6081
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006082static int selinux_sem_semop(struct kern_ipc_perm *sma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006083 struct sembuf *sops, unsigned nsops, int alter)
6084{
6085 u32 perms;
6086
6087 if (alter)
6088 perms = SEM__READ | SEM__WRITE;
6089 else
6090 perms = SEM__READ;
6091
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006092 return ipc_has_perm(sma, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093}
6094
6095static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
6096{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097 u32 av = 0;
6098
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099 av = 0;
6100 if (flag & S_IRUGO)
6101 av |= IPC__UNIX_READ;
6102 if (flag & S_IWUGO)
6103 av |= IPC__UNIX_WRITE;
6104
6105 if (av == 0)
6106 return 0;
6107
Stephen Smalley6af963f2005-05-01 08:58:39 -07006108 return ipc_has_perm(ipcp, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006109}
6110
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02006111static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
6112{
6113 struct ipc_security_struct *isec = ipcp->security;
6114 *secid = isec->sid;
6115}
6116
Eric Paris828dfe12008-04-17 13:17:49 -04006117static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006118{
6119 if (inode)
6120 inode_doinit_with_dentry(inode, dentry);
6121}
6122
6123static int selinux_getprocattr(struct task_struct *p,
Al Viro04ff9702007-03-12 16:17:58 +00006124 char *name, char **value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006125{
David Howells275bb412008-11-14 10:39:19 +11006126 const struct task_security_struct *__tsec;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00006127 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006128 int error;
Al Viro04ff9702007-03-12 16:17:58 +00006129 unsigned len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006130
David Howells275bb412008-11-14 10:39:19 +11006131 rcu_read_lock();
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07006132 __tsec = selinux_cred(__task_cred(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006133
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006134 if (current != p) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006135 error = avc_has_perm(&selinux_state,
6136 current_sid(), __tsec->sid,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006137 SECCLASS_PROCESS, PROCESS__GETATTR, NULL);
6138 if (error)
6139 goto bad;
6140 }
6141
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142 if (!strcmp(name, "current"))
David Howells275bb412008-11-14 10:39:19 +11006143 sid = __tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006144 else if (!strcmp(name, "prev"))
David Howells275bb412008-11-14 10:39:19 +11006145 sid = __tsec->osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006146 else if (!strcmp(name, "exec"))
David Howells275bb412008-11-14 10:39:19 +11006147 sid = __tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006148 else if (!strcmp(name, "fscreate"))
David Howells275bb412008-11-14 10:39:19 +11006149 sid = __tsec->create_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006150 else if (!strcmp(name, "keycreate"))
David Howells275bb412008-11-14 10:39:19 +11006151 sid = __tsec->keycreate_sid;
Eric Paris42c3e032006-06-26 00:26:03 -07006152 else if (!strcmp(name, "sockcreate"))
David Howells275bb412008-11-14 10:39:19 +11006153 sid = __tsec->sockcreate_sid;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006154 else {
6155 error = -EINVAL;
6156 goto bad;
6157 }
David Howells275bb412008-11-14 10:39:19 +11006158 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006159
6160 if (!sid)
6161 return 0;
6162
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006163 error = security_sid_to_context(&selinux_state, sid, value, &len);
Al Viro04ff9702007-03-12 16:17:58 +00006164 if (error)
6165 return error;
6166 return len;
David Howells275bb412008-11-14 10:39:19 +11006167
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006168bad:
David Howells275bb412008-11-14 10:39:19 +11006169 rcu_read_unlock();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006170 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006171}
6172
Stephen Smalleyb21507e2017-01-09 10:07:31 -05006173static int selinux_setprocattr(const char *name, void *value, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006174{
6175 struct task_security_struct *tsec;
David Howellsd84f4f92008-11-14 10:39:23 +11006176 struct cred *new;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006177 u32 mysid = current_sid(), sid = 0, ptsid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006178 int error;
6179 char *str = value;
6180
Linus Torvalds1da177e2005-04-16 15:20:36 -07006181 /*
6182 * Basic control over ability to set these attributes at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006183 */
6184 if (!strcmp(name, "exec"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006185 error = avc_has_perm(&selinux_state,
6186 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006187 PROCESS__SETEXEC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006188 else if (!strcmp(name, "fscreate"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006189 error = avc_has_perm(&selinux_state,
6190 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006191 PROCESS__SETFSCREATE, NULL);
Michael LeMay4eb582c2006-06-26 00:24:57 -07006192 else if (!strcmp(name, "keycreate"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006193 error = avc_has_perm(&selinux_state,
6194 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006195 PROCESS__SETKEYCREATE, NULL);
Eric Paris42c3e032006-06-26 00:26:03 -07006196 else if (!strcmp(name, "sockcreate"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006197 error = avc_has_perm(&selinux_state,
6198 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006199 PROCESS__SETSOCKCREATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006200 else if (!strcmp(name, "current"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006201 error = avc_has_perm(&selinux_state,
6202 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006203 PROCESS__SETCURRENT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204 else
6205 error = -EINVAL;
6206 if (error)
6207 return error;
6208
6209 /* Obtain a SID for the context, if one was specified. */
Stephen Smalleya050a572017-01-31 11:54:04 -05006210 if (size && str[0] && str[0] != '\n') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211 if (str[size-1] == '\n') {
6212 str[size-1] = 0;
6213 size--;
6214 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006215 error = security_context_to_sid(&selinux_state, value, size,
6216 &sid, GFP_KERNEL);
Stephen Smalley12b29f32008-05-07 13:03:20 -04006217 if (error == -EINVAL && !strcmp(name, "fscreate")) {
Stephen Smalleydb590002017-04-20 11:31:30 -04006218 if (!has_cap_mac_admin(true)) {
Eric Parisd6ea83e2012-04-04 13:45:49 -04006219 struct audit_buffer *ab;
6220 size_t audit_size;
6221
6222 /* We strip a nul only if it is at the end, otherwise the
6223 * context contains a nul and we should audit that */
6224 if (str[size - 1] == '\0')
6225 audit_size = size - 1;
6226 else
6227 audit_size = size;
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04006228 ab = audit_log_start(audit_context(),
6229 GFP_ATOMIC,
6230 AUDIT_SELINUX_ERR);
Eric Parisd6ea83e2012-04-04 13:45:49 -04006231 audit_log_format(ab, "op=fscreate invalid_context=");
6232 audit_log_n_untrustedstring(ab, value, audit_size);
6233 audit_log_end(ab);
6234
Stephen Smalley12b29f32008-05-07 13:03:20 -04006235 return error;
Eric Parisd6ea83e2012-04-04 13:45:49 -04006236 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006237 error = security_context_to_sid_force(
6238 &selinux_state,
6239 value, size, &sid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04006240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006241 if (error)
6242 return error;
6243 }
6244
David Howellsd84f4f92008-11-14 10:39:23 +11006245 new = prepare_creds();
6246 if (!new)
6247 return -ENOMEM;
6248
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249 /* Permission checking based on the specified context is
6250 performed during the actual operation (execve,
6251 open/mkdir/...), when we know the full context of the
David Howellsd84f4f92008-11-14 10:39:23 +11006252 operation. See selinux_bprm_set_creds for the execve
Linus Torvalds1da177e2005-04-16 15:20:36 -07006253 checks and may_create for the file creation checks. The
6254 operation will then fail if the context is not permitted. */
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07006255 tsec = selinux_cred(new);
David Howellsd84f4f92008-11-14 10:39:23 +11006256 if (!strcmp(name, "exec")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006257 tsec->exec_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006258 } else if (!strcmp(name, "fscreate")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006259 tsec->create_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006260 } else if (!strcmp(name, "keycreate")) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006261 error = avc_has_perm(&selinux_state,
6262 mysid, sid, SECCLASS_KEY, KEY__CREATE,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006263 NULL);
Michael LeMay4eb582c2006-06-26 00:24:57 -07006264 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11006265 goto abort_change;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006266 tsec->keycreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006267 } else if (!strcmp(name, "sockcreate")) {
Eric Paris42c3e032006-06-26 00:26:03 -07006268 tsec->sockcreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006269 } else if (!strcmp(name, "current")) {
6270 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271 if (sid == 0)
David Howellsd84f4f92008-11-14 10:39:23 +11006272 goto abort_change;
KaiGai Koheid9250de2008-08-28 16:35:57 +09006273
David Howellsd84f4f92008-11-14 10:39:23 +11006274 /* Only allow single threaded processes to change context */
6275 error = -EPERM;
Oleg Nesterov5bb459b2009-07-10 03:48:23 +02006276 if (!current_is_single_threaded()) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006277 error = security_bounded_transition(&selinux_state,
6278 tsec->sid, sid);
David Howellsd84f4f92008-11-14 10:39:23 +11006279 if (error)
6280 goto abort_change;
Eric Paris828dfe12008-04-17 13:17:49 -04006281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006282
6283 /* Check permissions for the transition. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006284 error = avc_has_perm(&selinux_state,
6285 tsec->sid, sid, SECCLASS_PROCESS,
Eric Paris828dfe12008-04-17 13:17:49 -04006286 PROCESS__DYNTRANSITION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006287 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11006288 goto abort_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006289
6290 /* Check for ptracing, and update the task SID if ok.
6291 Otherwise, leave SID unchanged and fail. */
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006292 ptsid = ptrace_parent_sid();
Paul Moore0c6181c2016-03-30 21:41:21 -04006293 if (ptsid != 0) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006294 error = avc_has_perm(&selinux_state,
6295 ptsid, sid, SECCLASS_PROCESS,
David Howellsd84f4f92008-11-14 10:39:23 +11006296 PROCESS__PTRACE, NULL);
6297 if (error)
6298 goto abort_change;
6299 }
6300
6301 tsec->sid = sid;
6302 } else {
6303 error = -EINVAL;
6304 goto abort_change;
6305 }
6306
6307 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006308 return size;
David Howellsd84f4f92008-11-14 10:39:23 +11006309
6310abort_change:
6311 abort_creds(new);
6312 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006313}
6314
David Quigley746df9b2013-05-22 12:50:35 -04006315static int selinux_ismaclabel(const char *name)
6316{
6317 return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0);
6318}
6319
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006320static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
6321{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006322 return security_sid_to_context(&selinux_state, secid,
6323 secdata, seclen);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006324}
6325
David Howells7bf570d2008-04-29 20:52:51 +01006326static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
David Howells63cb3442008-01-15 23:47:35 +00006327{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006328 return security_context_to_sid(&selinux_state, secdata, seclen,
6329 secid, GFP_KERNEL);
David Howells63cb3442008-01-15 23:47:35 +00006330}
6331
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006332static void selinux_release_secctx(char *secdata, u32 seclen)
6333{
Paul Moore088999e2007-08-01 11:12:58 -04006334 kfree(secdata);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006335}
6336
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006337static void selinux_inode_invalidate_secctx(struct inode *inode)
6338{
6339 struct inode_security_struct *isec = inode->i_security;
6340
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01006341 spin_lock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006342 isec->initialized = LABEL_INVALID;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01006343 spin_unlock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006344}
6345
David P. Quigley1ee65e32009-09-03 14:25:57 -04006346/*
6347 * called with inode->i_mutex locked
6348 */
6349static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
6350{
6351 return selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, ctx, ctxlen, 0);
6352}
6353
6354/*
6355 * called with inode->i_mutex locked
6356 */
6357static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
6358{
6359 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0);
6360}
6361
6362static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
6363{
6364 int len = 0;
6365 len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX,
6366 ctx, true);
6367 if (len < 0)
6368 return len;
6369 *ctxlen = len;
6370 return 0;
6371}
Michael LeMayd7200242006-06-22 14:47:17 -07006372#ifdef CONFIG_KEYS
6373
David Howellsd84f4f92008-11-14 10:39:23 +11006374static int selinux_key_alloc(struct key *k, const struct cred *cred,
David Howells7e047ef2006-06-26 00:24:50 -07006375 unsigned long flags)
Michael LeMayd7200242006-06-22 14:47:17 -07006376{
David Howellsd84f4f92008-11-14 10:39:23 +11006377 const struct task_security_struct *tsec;
Michael LeMayd7200242006-06-22 14:47:17 -07006378 struct key_security_struct *ksec;
6379
6380 ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
6381 if (!ksec)
6382 return -ENOMEM;
6383
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07006384 tsec = selinux_cred(cred);
David Howellsd84f4f92008-11-14 10:39:23 +11006385 if (tsec->keycreate_sid)
6386 ksec->sid = tsec->keycreate_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006387 else
David Howellsd84f4f92008-11-14 10:39:23 +11006388 ksec->sid = tsec->sid;
Michael LeMayd7200242006-06-22 14:47:17 -07006389
David Howells275bb412008-11-14 10:39:19 +11006390 k->security = ksec;
Michael LeMayd7200242006-06-22 14:47:17 -07006391 return 0;
6392}
6393
6394static void selinux_key_free(struct key *k)
6395{
6396 struct key_security_struct *ksec = k->security;
6397
6398 k->security = NULL;
6399 kfree(ksec);
6400}
6401
6402static int selinux_key_permission(key_ref_t key_ref,
David Howellsd84f4f92008-11-14 10:39:23 +11006403 const struct cred *cred,
David Howellsf5895942014-03-14 17:44:49 +00006404 unsigned perm)
Michael LeMayd7200242006-06-22 14:47:17 -07006405{
6406 struct key *key;
Michael LeMayd7200242006-06-22 14:47:17 -07006407 struct key_security_struct *ksec;
David Howells275bb412008-11-14 10:39:19 +11006408 u32 sid;
Michael LeMayd7200242006-06-22 14:47:17 -07006409
6410 /* if no specific permissions are requested, we skip the
6411 permission check. No serious, additional covert channels
6412 appear to be created. */
6413 if (perm == 0)
6414 return 0;
6415
David Howellsd84f4f92008-11-14 10:39:23 +11006416 sid = cred_sid(cred);
David Howells275bb412008-11-14 10:39:19 +11006417
6418 key = key_ref_to_ptr(key_ref);
6419 ksec = key->security;
6420
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006421 return avc_has_perm(&selinux_state,
6422 sid, ksec->sid, SECCLASS_KEY, perm, NULL);
Michael LeMayd7200242006-06-22 14:47:17 -07006423}
6424
David Howells70a5bb72008-04-29 01:01:26 -07006425static int selinux_key_getsecurity(struct key *key, char **_buffer)
6426{
6427 struct key_security_struct *ksec = key->security;
6428 char *context = NULL;
6429 unsigned len;
6430 int rc;
6431
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006432 rc = security_sid_to_context(&selinux_state, ksec->sid,
6433 &context, &len);
David Howells70a5bb72008-04-29 01:01:26 -07006434 if (!rc)
6435 rc = len;
6436 *_buffer = context;
6437 return rc;
6438}
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006439#endif
David Howells70a5bb72008-04-29 01:01:26 -07006440
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006441#ifdef CONFIG_SECURITY_INFINIBAND
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006442static int selinux_ib_pkey_access(void *ib_sec, u64 subnet_prefix, u16 pkey_val)
6443{
6444 struct common_audit_data ad;
6445 int err;
6446 u32 sid = 0;
6447 struct ib_security_struct *sec = ib_sec;
6448 struct lsm_ibpkey_audit ibpkey;
6449
Daniel Jurgens409dcf32017-05-19 15:48:59 +03006450 err = sel_ib_pkey_sid(subnet_prefix, pkey_val, &sid);
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006451 if (err)
6452 return err;
6453
6454 ad.type = LSM_AUDIT_DATA_IBPKEY;
6455 ibpkey.subnet_prefix = subnet_prefix;
6456 ibpkey.pkey = pkey_val;
6457 ad.u.ibpkey = &ibpkey;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006458 return avc_has_perm(&selinux_state,
6459 sec->sid, sid,
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006460 SECCLASS_INFINIBAND_PKEY,
6461 INFINIBAND_PKEY__ACCESS, &ad);
6462}
6463
Daniel Jurgensab861df2017-05-19 15:48:58 +03006464static int selinux_ib_endport_manage_subnet(void *ib_sec, const char *dev_name,
6465 u8 port_num)
6466{
6467 struct common_audit_data ad;
6468 int err;
6469 u32 sid = 0;
6470 struct ib_security_struct *sec = ib_sec;
6471 struct lsm_ibendport_audit ibendport;
6472
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006473 err = security_ib_endport_sid(&selinux_state, dev_name, port_num,
6474 &sid);
Daniel Jurgensab861df2017-05-19 15:48:58 +03006475
6476 if (err)
6477 return err;
6478
6479 ad.type = LSM_AUDIT_DATA_IBENDPORT;
6480 strncpy(ibendport.dev_name, dev_name, sizeof(ibendport.dev_name));
6481 ibendport.port = port_num;
6482 ad.u.ibendport = &ibendport;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006483 return avc_has_perm(&selinux_state,
6484 sec->sid, sid,
Daniel Jurgensab861df2017-05-19 15:48:58 +03006485 SECCLASS_INFINIBAND_ENDPORT,
6486 INFINIBAND_ENDPORT__MANAGE_SUBNET, &ad);
6487}
6488
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006489static int selinux_ib_alloc_security(void **ib_sec)
6490{
6491 struct ib_security_struct *sec;
6492
6493 sec = kzalloc(sizeof(*sec), GFP_KERNEL);
6494 if (!sec)
6495 return -ENOMEM;
6496 sec->sid = current_sid();
6497
6498 *ib_sec = sec;
6499 return 0;
6500}
6501
6502static void selinux_ib_free_security(void *ib_sec)
6503{
6504 kfree(ib_sec);
6505}
Michael LeMayd7200242006-06-22 14:47:17 -07006506#endif
6507
Chenbo Fengec27c352017-10-18 13:00:25 -07006508#ifdef CONFIG_BPF_SYSCALL
6509static int selinux_bpf(int cmd, union bpf_attr *attr,
6510 unsigned int size)
6511{
6512 u32 sid = current_sid();
6513 int ret;
6514
6515 switch (cmd) {
6516 case BPF_MAP_CREATE:
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006517 ret = avc_has_perm(&selinux_state,
6518 sid, sid, SECCLASS_BPF, BPF__MAP_CREATE,
Chenbo Fengec27c352017-10-18 13:00:25 -07006519 NULL);
6520 break;
6521 case BPF_PROG_LOAD:
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006522 ret = avc_has_perm(&selinux_state,
6523 sid, sid, SECCLASS_BPF, BPF__PROG_LOAD,
Chenbo Fengec27c352017-10-18 13:00:25 -07006524 NULL);
6525 break;
6526 default:
6527 ret = 0;
6528 break;
6529 }
6530
6531 return ret;
6532}
6533
6534static u32 bpf_map_fmode_to_av(fmode_t fmode)
6535{
6536 u32 av = 0;
6537
6538 if (fmode & FMODE_READ)
6539 av |= BPF__MAP_READ;
6540 if (fmode & FMODE_WRITE)
6541 av |= BPF__MAP_WRITE;
6542 return av;
6543}
6544
Chenbo Fengf66e4482017-10-18 13:00:26 -07006545/* This function will check the file pass through unix socket or binder to see
6546 * if it is a bpf related object. And apply correspinding checks on the bpf
6547 * object based on the type. The bpf maps and programs, not like other files and
6548 * socket, are using a shared anonymous inode inside the kernel as their inode.
6549 * So checking that inode cannot identify if the process have privilege to
6550 * access the bpf object and that's why we have to add this additional check in
6551 * selinux_file_receive and selinux_binder_transfer_files.
6552 */
6553static int bpf_fd_pass(struct file *file, u32 sid)
6554{
6555 struct bpf_security_struct *bpfsec;
6556 struct bpf_prog *prog;
6557 struct bpf_map *map;
6558 int ret;
6559
6560 if (file->f_op == &bpf_map_fops) {
6561 map = file->private_data;
6562 bpfsec = map->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006563 ret = avc_has_perm(&selinux_state,
6564 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengf66e4482017-10-18 13:00:26 -07006565 bpf_map_fmode_to_av(file->f_mode), NULL);
6566 if (ret)
6567 return ret;
6568 } else if (file->f_op == &bpf_prog_fops) {
6569 prog = file->private_data;
6570 bpfsec = prog->aux->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006571 ret = avc_has_perm(&selinux_state,
6572 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengf66e4482017-10-18 13:00:26 -07006573 BPF__PROG_RUN, NULL);
6574 if (ret)
6575 return ret;
6576 }
6577 return 0;
6578}
6579
Chenbo Fengec27c352017-10-18 13:00:25 -07006580static int selinux_bpf_map(struct bpf_map *map, fmode_t fmode)
6581{
6582 u32 sid = current_sid();
6583 struct bpf_security_struct *bpfsec;
6584
6585 bpfsec = map->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006586 return avc_has_perm(&selinux_state,
6587 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengec27c352017-10-18 13:00:25 -07006588 bpf_map_fmode_to_av(fmode), NULL);
6589}
6590
6591static int selinux_bpf_prog(struct bpf_prog *prog)
6592{
6593 u32 sid = current_sid();
6594 struct bpf_security_struct *bpfsec;
6595
6596 bpfsec = prog->aux->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006597 return avc_has_perm(&selinux_state,
6598 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengec27c352017-10-18 13:00:25 -07006599 BPF__PROG_RUN, NULL);
6600}
6601
6602static int selinux_bpf_map_alloc(struct bpf_map *map)
6603{
6604 struct bpf_security_struct *bpfsec;
6605
6606 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6607 if (!bpfsec)
6608 return -ENOMEM;
6609
6610 bpfsec->sid = current_sid();
6611 map->security = bpfsec;
6612
6613 return 0;
6614}
6615
6616static void selinux_bpf_map_free(struct bpf_map *map)
6617{
6618 struct bpf_security_struct *bpfsec = map->security;
6619
6620 map->security = NULL;
6621 kfree(bpfsec);
6622}
6623
6624static int selinux_bpf_prog_alloc(struct bpf_prog_aux *aux)
6625{
6626 struct bpf_security_struct *bpfsec;
6627
6628 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6629 if (!bpfsec)
6630 return -ENOMEM;
6631
6632 bpfsec->sid = current_sid();
6633 aux->security = bpfsec;
6634
6635 return 0;
6636}
6637
6638static void selinux_bpf_prog_free(struct bpf_prog_aux *aux)
6639{
6640 struct bpf_security_struct *bpfsec = aux->security;
6641
6642 aux->security = NULL;
6643 kfree(bpfsec);
6644}
6645#endif
6646
Casey Schauflerbbd36622018-11-12 09:30:56 -08006647struct lsm_blob_sizes selinux_blob_sizes __lsm_ro_after_init = {
6648 .lbs_cred = sizeof(struct task_security_struct),
6649};
6650
James Morrisca97d932017-02-15 00:18:51 +11006651static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {
Casey Schauflere20b0432015-05-02 15:11:36 -07006652 LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr),
6653 LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction),
6654 LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder),
6655 LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file),
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02006656
Casey Schauflere20b0432015-05-02 15:11:36 -07006657 LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check),
6658 LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme),
6659 LSM_HOOK_INIT(capget, selinux_capget),
6660 LSM_HOOK_INIT(capset, selinux_capset),
6661 LSM_HOOK_INIT(capable, selinux_capable),
6662 LSM_HOOK_INIT(quotactl, selinux_quotactl),
6663 LSM_HOOK_INIT(quota_on, selinux_quota_on),
6664 LSM_HOOK_INIT(syslog, selinux_syslog),
6665 LSM_HOOK_INIT(vm_enough_memory, selinux_vm_enough_memory),
Stephen Smalley79af7302015-01-21 10:54:10 -05006666
Casey Schauflere20b0432015-05-02 15:11:36 -07006667 LSM_HOOK_INIT(netlink_send, selinux_netlink_send),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006668
Casey Schauflere20b0432015-05-02 15:11:36 -07006669 LSM_HOOK_INIT(bprm_set_creds, selinux_bprm_set_creds),
6670 LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds),
6671 LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006672
Casey Schauflere20b0432015-05-02 15:11:36 -07006673 LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security),
6674 LSM_HOOK_INIT(sb_free_security, selinux_sb_free_security),
Al Viro5b400232018-12-12 20:13:29 -05006675 LSM_HOOK_INIT(sb_eat_lsm_opts, selinux_sb_eat_lsm_opts),
Al Viro204cc0c2018-12-13 13:41:47 -05006676 LSM_HOOK_INIT(sb_free_mnt_opts, selinux_free_mnt_opts),
Casey Schauflere20b0432015-05-02 15:11:36 -07006677 LSM_HOOK_INIT(sb_remount, selinux_sb_remount),
6678 LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount),
6679 LSM_HOOK_INIT(sb_show_options, selinux_sb_show_options),
6680 LSM_HOOK_INIT(sb_statfs, selinux_sb_statfs),
6681 LSM_HOOK_INIT(sb_mount, selinux_mount),
6682 LSM_HOOK_INIT(sb_umount, selinux_umount),
6683 LSM_HOOK_INIT(sb_set_mnt_opts, selinux_set_mnt_opts),
6684 LSM_HOOK_INIT(sb_clone_mnt_opts, selinux_sb_clone_mnt_opts),
Al Viro757cbe52018-12-14 23:42:21 -05006685 LSM_HOOK_INIT(sb_add_mnt_opt, selinux_add_mnt_opt),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006686
Casey Schauflere20b0432015-05-02 15:11:36 -07006687 LSM_HOOK_INIT(dentry_init_security, selinux_dentry_init_security),
Vivek Goyala518b0a2016-07-13 10:44:53 -04006688 LSM_HOOK_INIT(dentry_create_files_as, selinux_dentry_create_files_as),
Eric Parise0007522008-03-05 10:31:54 -05006689
Casey Schauflere20b0432015-05-02 15:11:36 -07006690 LSM_HOOK_INIT(inode_alloc_security, selinux_inode_alloc_security),
6691 LSM_HOOK_INIT(inode_free_security, selinux_inode_free_security),
6692 LSM_HOOK_INIT(inode_init_security, selinux_inode_init_security),
6693 LSM_HOOK_INIT(inode_create, selinux_inode_create),
6694 LSM_HOOK_INIT(inode_link, selinux_inode_link),
6695 LSM_HOOK_INIT(inode_unlink, selinux_inode_unlink),
6696 LSM_HOOK_INIT(inode_symlink, selinux_inode_symlink),
6697 LSM_HOOK_INIT(inode_mkdir, selinux_inode_mkdir),
6698 LSM_HOOK_INIT(inode_rmdir, selinux_inode_rmdir),
6699 LSM_HOOK_INIT(inode_mknod, selinux_inode_mknod),
6700 LSM_HOOK_INIT(inode_rename, selinux_inode_rename),
6701 LSM_HOOK_INIT(inode_readlink, selinux_inode_readlink),
6702 LSM_HOOK_INIT(inode_follow_link, selinux_inode_follow_link),
6703 LSM_HOOK_INIT(inode_permission, selinux_inode_permission),
6704 LSM_HOOK_INIT(inode_setattr, selinux_inode_setattr),
6705 LSM_HOOK_INIT(inode_getattr, selinux_inode_getattr),
6706 LSM_HOOK_INIT(inode_setxattr, selinux_inode_setxattr),
6707 LSM_HOOK_INIT(inode_post_setxattr, selinux_inode_post_setxattr),
6708 LSM_HOOK_INIT(inode_getxattr, selinux_inode_getxattr),
6709 LSM_HOOK_INIT(inode_listxattr, selinux_inode_listxattr),
6710 LSM_HOOK_INIT(inode_removexattr, selinux_inode_removexattr),
6711 LSM_HOOK_INIT(inode_getsecurity, selinux_inode_getsecurity),
6712 LSM_HOOK_INIT(inode_setsecurity, selinux_inode_setsecurity),
6713 LSM_HOOK_INIT(inode_listsecurity, selinux_inode_listsecurity),
6714 LSM_HOOK_INIT(inode_getsecid, selinux_inode_getsecid),
Vivek Goyal56909eb2016-07-13 10:44:48 -04006715 LSM_HOOK_INIT(inode_copy_up, selinux_inode_copy_up),
Vivek Goyal19472b62016-07-13 10:44:50 -04006716 LSM_HOOK_INIT(inode_copy_up_xattr, selinux_inode_copy_up_xattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006717
Casey Schauflere20b0432015-05-02 15:11:36 -07006718 LSM_HOOK_INIT(file_permission, selinux_file_permission),
6719 LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security),
6720 LSM_HOOK_INIT(file_free_security, selinux_file_free_security),
6721 LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl),
6722 LSM_HOOK_INIT(mmap_file, selinux_mmap_file),
6723 LSM_HOOK_INIT(mmap_addr, selinux_mmap_addr),
6724 LSM_HOOK_INIT(file_mprotect, selinux_file_mprotect),
6725 LSM_HOOK_INIT(file_lock, selinux_file_lock),
6726 LSM_HOOK_INIT(file_fcntl, selinux_file_fcntl),
6727 LSM_HOOK_INIT(file_set_fowner, selinux_file_set_fowner),
6728 LSM_HOOK_INIT(file_send_sigiotask, selinux_file_send_sigiotask),
6729 LSM_HOOK_INIT(file_receive, selinux_file_receive),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006730
Casey Schauflere20b0432015-05-02 15:11:36 -07006731 LSM_HOOK_INIT(file_open, selinux_file_open),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006732
Tetsuo Handaa79be232017-03-28 23:08:45 +09006733 LSM_HOOK_INIT(task_alloc, selinux_task_alloc),
Casey Schauflere20b0432015-05-02 15:11:36 -07006734 LSM_HOOK_INIT(cred_prepare, selinux_cred_prepare),
6735 LSM_HOOK_INIT(cred_transfer, selinux_cred_transfer),
Matthew Garrett3ec30112018-01-08 13:36:19 -08006736 LSM_HOOK_INIT(cred_getsecid, selinux_cred_getsecid),
Casey Schauflere20b0432015-05-02 15:11:36 -07006737 LSM_HOOK_INIT(kernel_act_as, selinux_kernel_act_as),
6738 LSM_HOOK_INIT(kernel_create_files_as, selinux_kernel_create_files_as),
6739 LSM_HOOK_INIT(kernel_module_request, selinux_kernel_module_request),
Mimi Zoharc77b8cd2018-07-13 14:06:02 -04006740 LSM_HOOK_INIT(kernel_load_data, selinux_kernel_load_data),
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07006741 LSM_HOOK_INIT(kernel_read_file, selinux_kernel_read_file),
Casey Schauflere20b0432015-05-02 15:11:36 -07006742 LSM_HOOK_INIT(task_setpgid, selinux_task_setpgid),
6743 LSM_HOOK_INIT(task_getpgid, selinux_task_getpgid),
6744 LSM_HOOK_INIT(task_getsid, selinux_task_getsid),
6745 LSM_HOOK_INIT(task_getsecid, selinux_task_getsecid),
6746 LSM_HOOK_INIT(task_setnice, selinux_task_setnice),
6747 LSM_HOOK_INIT(task_setioprio, selinux_task_setioprio),
6748 LSM_HOOK_INIT(task_getioprio, selinux_task_getioprio),
Stephen Smalley791ec492017-02-17 07:57:00 -05006749 LSM_HOOK_INIT(task_prlimit, selinux_task_prlimit),
Casey Schauflere20b0432015-05-02 15:11:36 -07006750 LSM_HOOK_INIT(task_setrlimit, selinux_task_setrlimit),
6751 LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler),
6752 LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler),
6753 LSM_HOOK_INIT(task_movememory, selinux_task_movememory),
6754 LSM_HOOK_INIT(task_kill, selinux_task_kill),
Casey Schauflere20b0432015-05-02 15:11:36 -07006755 LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode),
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09006756
Casey Schauflere20b0432015-05-02 15:11:36 -07006757 LSM_HOOK_INIT(ipc_permission, selinux_ipc_permission),
6758 LSM_HOOK_INIT(ipc_getsecid, selinux_ipc_getsecid),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006759
Casey Schauflere20b0432015-05-02 15:11:36 -07006760 LSM_HOOK_INIT(msg_msg_alloc_security, selinux_msg_msg_alloc_security),
6761 LSM_HOOK_INIT(msg_msg_free_security, selinux_msg_msg_free_security),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006762
Casey Schauflere20b0432015-05-02 15:11:36 -07006763 LSM_HOOK_INIT(msg_queue_alloc_security,
6764 selinux_msg_queue_alloc_security),
6765 LSM_HOOK_INIT(msg_queue_free_security, selinux_msg_queue_free_security),
6766 LSM_HOOK_INIT(msg_queue_associate, selinux_msg_queue_associate),
6767 LSM_HOOK_INIT(msg_queue_msgctl, selinux_msg_queue_msgctl),
6768 LSM_HOOK_INIT(msg_queue_msgsnd, selinux_msg_queue_msgsnd),
6769 LSM_HOOK_INIT(msg_queue_msgrcv, selinux_msg_queue_msgrcv),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006770
Casey Schauflere20b0432015-05-02 15:11:36 -07006771 LSM_HOOK_INIT(shm_alloc_security, selinux_shm_alloc_security),
6772 LSM_HOOK_INIT(shm_free_security, selinux_shm_free_security),
6773 LSM_HOOK_INIT(shm_associate, selinux_shm_associate),
6774 LSM_HOOK_INIT(shm_shmctl, selinux_shm_shmctl),
6775 LSM_HOOK_INIT(shm_shmat, selinux_shm_shmat),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006776
Casey Schauflere20b0432015-05-02 15:11:36 -07006777 LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security),
6778 LSM_HOOK_INIT(sem_free_security, selinux_sem_free_security),
6779 LSM_HOOK_INIT(sem_associate, selinux_sem_associate),
6780 LSM_HOOK_INIT(sem_semctl, selinux_sem_semctl),
6781 LSM_HOOK_INIT(sem_semop, selinux_sem_semop),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006782
Casey Schauflere20b0432015-05-02 15:11:36 -07006783 LSM_HOOK_INIT(d_instantiate, selinux_d_instantiate),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006784
Casey Schauflere20b0432015-05-02 15:11:36 -07006785 LSM_HOOK_INIT(getprocattr, selinux_getprocattr),
6786 LSM_HOOK_INIT(setprocattr, selinux_setprocattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006787
Casey Schauflere20b0432015-05-02 15:11:36 -07006788 LSM_HOOK_INIT(ismaclabel, selinux_ismaclabel),
6789 LSM_HOOK_INIT(secid_to_secctx, selinux_secid_to_secctx),
6790 LSM_HOOK_INIT(secctx_to_secid, selinux_secctx_to_secid),
6791 LSM_HOOK_INIT(release_secctx, selinux_release_secctx),
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006792 LSM_HOOK_INIT(inode_invalidate_secctx, selinux_inode_invalidate_secctx),
Casey Schauflere20b0432015-05-02 15:11:36 -07006793 LSM_HOOK_INIT(inode_notifysecctx, selinux_inode_notifysecctx),
6794 LSM_HOOK_INIT(inode_setsecctx, selinux_inode_setsecctx),
6795 LSM_HOOK_INIT(inode_getsecctx, selinux_inode_getsecctx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006796
Casey Schauflere20b0432015-05-02 15:11:36 -07006797 LSM_HOOK_INIT(unix_stream_connect, selinux_socket_unix_stream_connect),
6798 LSM_HOOK_INIT(unix_may_send, selinux_socket_unix_may_send),
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006799
Casey Schauflere20b0432015-05-02 15:11:36 -07006800 LSM_HOOK_INIT(socket_create, selinux_socket_create),
6801 LSM_HOOK_INIT(socket_post_create, selinux_socket_post_create),
David Herrmann0b811db2018-05-04 16:28:21 +02006802 LSM_HOOK_INIT(socket_socketpair, selinux_socket_socketpair),
Casey Schauflere20b0432015-05-02 15:11:36 -07006803 LSM_HOOK_INIT(socket_bind, selinux_socket_bind),
6804 LSM_HOOK_INIT(socket_connect, selinux_socket_connect),
6805 LSM_HOOK_INIT(socket_listen, selinux_socket_listen),
6806 LSM_HOOK_INIT(socket_accept, selinux_socket_accept),
6807 LSM_HOOK_INIT(socket_sendmsg, selinux_socket_sendmsg),
6808 LSM_HOOK_INIT(socket_recvmsg, selinux_socket_recvmsg),
6809 LSM_HOOK_INIT(socket_getsockname, selinux_socket_getsockname),
6810 LSM_HOOK_INIT(socket_getpeername, selinux_socket_getpeername),
6811 LSM_HOOK_INIT(socket_getsockopt, selinux_socket_getsockopt),
6812 LSM_HOOK_INIT(socket_setsockopt, selinux_socket_setsockopt),
6813 LSM_HOOK_INIT(socket_shutdown, selinux_socket_shutdown),
6814 LSM_HOOK_INIT(socket_sock_rcv_skb, selinux_socket_sock_rcv_skb),
6815 LSM_HOOK_INIT(socket_getpeersec_stream,
6816 selinux_socket_getpeersec_stream),
6817 LSM_HOOK_INIT(socket_getpeersec_dgram, selinux_socket_getpeersec_dgram),
6818 LSM_HOOK_INIT(sk_alloc_security, selinux_sk_alloc_security),
6819 LSM_HOOK_INIT(sk_free_security, selinux_sk_free_security),
6820 LSM_HOOK_INIT(sk_clone_security, selinux_sk_clone_security),
6821 LSM_HOOK_INIT(sk_getsecid, selinux_sk_getsecid),
6822 LSM_HOOK_INIT(sock_graft, selinux_sock_graft),
Richard Hainesd4529302018-02-13 20:57:18 +00006823 LSM_HOOK_INIT(sctp_assoc_request, selinux_sctp_assoc_request),
6824 LSM_HOOK_INIT(sctp_sk_clone, selinux_sctp_sk_clone),
6825 LSM_HOOK_INIT(sctp_bind_connect, selinux_sctp_bind_connect),
Casey Schauflere20b0432015-05-02 15:11:36 -07006826 LSM_HOOK_INIT(inet_conn_request, selinux_inet_conn_request),
6827 LSM_HOOK_INIT(inet_csk_clone, selinux_inet_csk_clone),
6828 LSM_HOOK_INIT(inet_conn_established, selinux_inet_conn_established),
6829 LSM_HOOK_INIT(secmark_relabel_packet, selinux_secmark_relabel_packet),
6830 LSM_HOOK_INIT(secmark_refcount_inc, selinux_secmark_refcount_inc),
6831 LSM_HOOK_INIT(secmark_refcount_dec, selinux_secmark_refcount_dec),
6832 LSM_HOOK_INIT(req_classify_flow, selinux_req_classify_flow),
6833 LSM_HOOK_INIT(tun_dev_alloc_security, selinux_tun_dev_alloc_security),
6834 LSM_HOOK_INIT(tun_dev_free_security, selinux_tun_dev_free_security),
6835 LSM_HOOK_INIT(tun_dev_create, selinux_tun_dev_create),
6836 LSM_HOOK_INIT(tun_dev_attach_queue, selinux_tun_dev_attach_queue),
6837 LSM_HOOK_INIT(tun_dev_attach, selinux_tun_dev_attach),
6838 LSM_HOOK_INIT(tun_dev_open, selinux_tun_dev_open),
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006839#ifdef CONFIG_SECURITY_INFINIBAND
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006840 LSM_HOOK_INIT(ib_pkey_access, selinux_ib_pkey_access),
Daniel Jurgensab861df2017-05-19 15:48:58 +03006841 LSM_HOOK_INIT(ib_endport_manage_subnet,
6842 selinux_ib_endport_manage_subnet),
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006843 LSM_HOOK_INIT(ib_alloc_security, selinux_ib_alloc_security),
6844 LSM_HOOK_INIT(ib_free_security, selinux_ib_free_security),
6845#endif
Trent Jaegerd28d1e02005-12-13 23:12:40 -08006846#ifdef CONFIG_SECURITY_NETWORK_XFRM
Casey Schauflere20b0432015-05-02 15:11:36 -07006847 LSM_HOOK_INIT(xfrm_policy_alloc_security, selinux_xfrm_policy_alloc),
6848 LSM_HOOK_INIT(xfrm_policy_clone_security, selinux_xfrm_policy_clone),
6849 LSM_HOOK_INIT(xfrm_policy_free_security, selinux_xfrm_policy_free),
6850 LSM_HOOK_INIT(xfrm_policy_delete_security, selinux_xfrm_policy_delete),
6851 LSM_HOOK_INIT(xfrm_state_alloc, selinux_xfrm_state_alloc),
6852 LSM_HOOK_INIT(xfrm_state_alloc_acquire,
6853 selinux_xfrm_state_alloc_acquire),
6854 LSM_HOOK_INIT(xfrm_state_free_security, selinux_xfrm_state_free),
6855 LSM_HOOK_INIT(xfrm_state_delete_security, selinux_xfrm_state_delete),
6856 LSM_HOOK_INIT(xfrm_policy_lookup, selinux_xfrm_policy_lookup),
6857 LSM_HOOK_INIT(xfrm_state_pol_flow_match,
6858 selinux_xfrm_state_pol_flow_match),
6859 LSM_HOOK_INIT(xfrm_decode_session, selinux_xfrm_decode_session),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006860#endif
Michael LeMayd7200242006-06-22 14:47:17 -07006861
6862#ifdef CONFIG_KEYS
Casey Schauflere20b0432015-05-02 15:11:36 -07006863 LSM_HOOK_INIT(key_alloc, selinux_key_alloc),
6864 LSM_HOOK_INIT(key_free, selinux_key_free),
6865 LSM_HOOK_INIT(key_permission, selinux_key_permission),
6866 LSM_HOOK_INIT(key_getsecurity, selinux_key_getsecurity),
Michael LeMayd7200242006-06-22 14:47:17 -07006867#endif
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02006868
6869#ifdef CONFIG_AUDIT
Casey Schauflere20b0432015-05-02 15:11:36 -07006870 LSM_HOOK_INIT(audit_rule_init, selinux_audit_rule_init),
6871 LSM_HOOK_INIT(audit_rule_known, selinux_audit_rule_known),
6872 LSM_HOOK_INIT(audit_rule_match, selinux_audit_rule_match),
6873 LSM_HOOK_INIT(audit_rule_free, selinux_audit_rule_free),
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02006874#endif
Chenbo Fengec27c352017-10-18 13:00:25 -07006875
6876#ifdef CONFIG_BPF_SYSCALL
6877 LSM_HOOK_INIT(bpf, selinux_bpf),
6878 LSM_HOOK_INIT(bpf_map, selinux_bpf_map),
6879 LSM_HOOK_INIT(bpf_prog, selinux_bpf_prog),
6880 LSM_HOOK_INIT(bpf_map_alloc_security, selinux_bpf_map_alloc),
6881 LSM_HOOK_INIT(bpf_prog_alloc_security, selinux_bpf_prog_alloc),
6882 LSM_HOOK_INIT(bpf_map_free_security, selinux_bpf_map_free),
6883 LSM_HOOK_INIT(bpf_prog_free_security, selinux_bpf_prog_free),
6884#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006885};
6886
6887static __init int selinux_init(void)
6888{
peter enderborgc103a912018-06-12 10:09:03 +02006889 pr_info("SELinux: Initializing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006890
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006891 memset(&selinux_state, 0, sizeof(selinux_state));
Paul Mooree5a5ca92018-03-01 17:38:30 -05006892 enforcing_set(&selinux_state, selinux_enforcing_boot);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006893 selinux_state.checkreqprot = selinux_checkreqprot_boot;
6894 selinux_ss_init(&selinux_state.ss);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006895 selinux_avc_init(&selinux_state.avc);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006896
Linus Torvalds1da177e2005-04-16 15:20:36 -07006897 /* Set the security state for the initial task. */
David Howellsd84f4f92008-11-14 10:39:23 +11006898 cred_init_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006899
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04006900 default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
6901
James Morris7cae7e22006-03-22 00:09:22 -08006902 sel_inode_cache = kmem_cache_create("selinux_inode_security",
6903 sizeof(struct inode_security_struct),
Paul Mundt20c2df82007-07-20 10:11:58 +09006904 0, SLAB_PANIC, NULL);
Sangwoo63205652015-10-21 17:44:30 -04006905 file_security_cache = kmem_cache_create("selinux_file_security",
6906 sizeof(struct file_security_struct),
6907 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006908 avc_init();
6909
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006910 avtab_cache_init();
6911
6912 ebitmap_cache_init();
6913
6914 hashtab_cache_init();
6915
Casey Schauflerd69dece52017-01-18 17:09:05 -08006916 security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks), "selinux");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006917
Paul Moore615e51f2014-06-26 14:33:56 -04006918 if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET))
6919 panic("SELinux: Unable to register AVC netcache callback\n");
6920
Daniel Jurgens8f408ab2017-05-19 15:48:53 +03006921 if (avc_add_callback(selinux_lsm_notifier_avc_callback, AVC_CALLBACK_RESET))
6922 panic("SELinux: Unable to register AVC LSM notifier callback\n");
6923
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006924 if (selinux_enforcing_boot)
peter enderborgc103a912018-06-12 10:09:03 +02006925 pr_debug("SELinux: Starting in enforcing mode\n");
Eric Paris828dfe12008-04-17 13:17:49 -04006926 else
peter enderborgc103a912018-06-12 10:09:03 +02006927 pr_debug("SELinux: Starting in permissive mode\n");
Michael LeMayd7200242006-06-22 14:47:17 -07006928
Linus Torvalds1da177e2005-04-16 15:20:36 -07006929 return 0;
6930}
6931
Al Viroe8c26252010-03-23 06:36:54 -04006932static void delayed_superblock_init(struct super_block *sb, void *unused)
6933{
Al Viro204cc0c2018-12-13 13:41:47 -05006934 selinux_set_mnt_opts(sb, NULL, 0, NULL);
Al Viroe8c26252010-03-23 06:36:54 -04006935}
6936
Linus Torvalds1da177e2005-04-16 15:20:36 -07006937void selinux_complete_init(void)
6938{
peter enderborgc103a912018-06-12 10:09:03 +02006939 pr_debug("SELinux: Completing initialization.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006940
6941 /* Set up any superblocks initialized prior to the policy load. */
peter enderborgc103a912018-06-12 10:09:03 +02006942 pr_debug("SELinux: Setting up existing superblocks.\n");
Al Viroe8c26252010-03-23 06:36:54 -04006943 iterate_supers(delayed_superblock_init, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006944}
6945
6946/* SELinux requires early initialization in order to label
6947 all processes and objects when they are created. */
Kees Cook3d6e5f62018-10-10 17:18:23 -07006948DEFINE_LSM(selinux) = {
Kees Cook07aed2f2018-10-10 17:18:24 -07006949 .name = "selinux",
Kees Cook14bd99c2018-09-19 19:57:06 -07006950 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
Kees Cookc5459b82018-09-13 22:28:48 -07006951 .enabled = &selinux_enabled,
Casey Schauflerbbd36622018-11-12 09:30:56 -08006952 .blobs = &selinux_blob_sizes,
Kees Cook3d6e5f62018-10-10 17:18:23 -07006953 .init = selinux_init,
6954};
Linus Torvalds1da177e2005-04-16 15:20:36 -07006955
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08006956#if defined(CONFIG_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006957
Florian Westphal591bb272017-07-26 11:40:52 +02006958static const struct nf_hook_ops selinux_nf_ops[] = {
Paul Mooreeffad8d2008-01-29 08:49:27 -05006959 {
6960 .hook = selinux_ipv4_postroute,
Alban Crequy2597a832012-05-14 03:56:39 +00006961 .pf = NFPROTO_IPV4,
Paul Mooreeffad8d2008-01-29 08:49:27 -05006962 .hooknum = NF_INET_POST_ROUTING,
6963 .priority = NF_IP_PRI_SELINUX_LAST,
6964 },
6965 {
6966 .hook = selinux_ipv4_forward,
Alban Crequy2597a832012-05-14 03:56:39 +00006967 .pf = NFPROTO_IPV4,
Paul Mooreeffad8d2008-01-29 08:49:27 -05006968 .hooknum = NF_INET_FORWARD,
6969 .priority = NF_IP_PRI_SELINUX_FIRST,
Paul Moore948bf852008-10-10 10:16:32 -04006970 },
6971 {
6972 .hook = selinux_ipv4_output,
Alban Crequy2597a832012-05-14 03:56:39 +00006973 .pf = NFPROTO_IPV4,
Paul Moore948bf852008-10-10 10:16:32 -04006974 .hooknum = NF_INET_LOCAL_OUT,
6975 .priority = NF_IP_PRI_SELINUX_FIRST,
Jiri Pirko25db6be2014-09-03 17:42:13 +02006976 },
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04006977#if IS_ENABLED(CONFIG_IPV6)
Paul Mooreeffad8d2008-01-29 08:49:27 -05006978 {
6979 .hook = selinux_ipv6_postroute,
Alban Crequy2597a832012-05-14 03:56:39 +00006980 .pf = NFPROTO_IPV6,
Paul Mooreeffad8d2008-01-29 08:49:27 -05006981 .hooknum = NF_INET_POST_ROUTING,
6982 .priority = NF_IP6_PRI_SELINUX_LAST,
6983 },
6984 {
6985 .hook = selinux_ipv6_forward,
Alban Crequy2597a832012-05-14 03:56:39 +00006986 .pf = NFPROTO_IPV6,
Paul Mooreeffad8d2008-01-29 08:49:27 -05006987 .hooknum = NF_INET_FORWARD,
6988 .priority = NF_IP6_PRI_SELINUX_FIRST,
Jiri Pirko25db6be2014-09-03 17:42:13 +02006989 },
Huw Davies2917f572016-06-27 15:06:15 -04006990 {
6991 .hook = selinux_ipv6_output,
6992 .pf = NFPROTO_IPV6,
6993 .hooknum = NF_INET_LOCAL_OUT,
6994 .priority = NF_IP6_PRI_SELINUX_FIRST,
6995 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07006996#endif /* IPV6 */
Jiri Pirko25db6be2014-09-03 17:42:13 +02006997};
Linus Torvalds1da177e2005-04-16 15:20:36 -07006998
Florian Westphal8e71bf72017-04-21 11:49:09 +02006999static int __net_init selinux_nf_register(struct net *net)
7000{
7001 return nf_register_net_hooks(net, selinux_nf_ops,
7002 ARRAY_SIZE(selinux_nf_ops));
7003}
7004
7005static void __net_exit selinux_nf_unregister(struct net *net)
7006{
7007 nf_unregister_net_hooks(net, selinux_nf_ops,
7008 ARRAY_SIZE(selinux_nf_ops));
7009}
7010
7011static struct pernet_operations selinux_net_ops = {
7012 .init = selinux_nf_register,
7013 .exit = selinux_nf_unregister,
7014};
7015
Linus Torvalds1da177e2005-04-16 15:20:36 -07007016static int __init selinux_nf_ip_init(void)
7017{
Jiri Pirko25db6be2014-09-03 17:42:13 +02007018 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007019
7020 if (!selinux_enabled)
Jiri Pirko25db6be2014-09-03 17:42:13 +02007021 return 0;
Eric Parisfadcdb42007-02-22 18:11:31 -05007022
peter enderborgc103a912018-06-12 10:09:03 +02007023 pr_debug("SELinux: Registering netfilter hooks\n");
Eric Parisfadcdb42007-02-22 18:11:31 -05007024
Florian Westphal8e71bf72017-04-21 11:49:09 +02007025 err = register_pernet_subsys(&selinux_net_ops);
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07007026 if (err)
Florian Westphal8e71bf72017-04-21 11:49:09 +02007027 panic("SELinux: register_pernet_subsys: error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007028
Jiri Pirko25db6be2014-09-03 17:42:13 +02007029 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007030}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007031__initcall(selinux_nf_ip_init);
7032
7033#ifdef CONFIG_SECURITY_SELINUX_DISABLE
7034static void selinux_nf_ip_exit(void)
7035{
peter enderborgc103a912018-06-12 10:09:03 +02007036 pr_debug("SELinux: Unregistering netfilter hooks\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007037
Florian Westphal8e71bf72017-04-21 11:49:09 +02007038 unregister_pernet_subsys(&selinux_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007039}
7040#endif
7041
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007042#else /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007043
7044#ifdef CONFIG_SECURITY_SELINUX_DISABLE
7045#define selinux_nf_ip_exit()
7046#endif
7047
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007048#endif /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007049
7050#ifdef CONFIG_SECURITY_SELINUX_DISABLE
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007051int selinux_disable(struct selinux_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007052{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007053 if (state->initialized) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007054 /* Not permitted after initial policy load. */
7055 return -EINVAL;
7056 }
7057
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007058 if (state->disabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007059 /* Only do this once. */
7060 return -EINVAL;
7061 }
7062
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007063 state->disabled = 1;
7064
peter enderborgc103a912018-06-12 10:09:03 +02007065 pr_info("SELinux: Disabled at runtime.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007066
Stephen Smalley30d55282006-05-03 10:52:36 -04007067 selinux_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007068
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07007069 security_delete_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007070
Eric Parisaf8ff042009-09-20 21:23:01 -04007071 /* Try to destroy the avc node cache */
7072 avc_disable();
7073
Linus Torvalds1da177e2005-04-16 15:20:36 -07007074 /* Unregister netfilter hooks. */
7075 selinux_nf_ip_exit();
7076
7077 /* Unregister selinuxfs. */
7078 exit_sel_fs();
7079
7080 return 0;
7081}
7082#endif