blob: f99381e97d73fa6348586fddc9c157327223efc5 [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>
David Howells442155c2018-11-01 23:07:24 +000051#include <linux/fs_context.h>
52#include <linux/fs_parser.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <linux/netfilter_ipv4.h>
54#include <linux/netfilter_ipv6.h>
55#include <linux/tty.h>
56#include <net/icmp.h>
Stephen Hemminger227b60f2007-10-10 17:30:46 -070057#include <net/ip.h> /* for local_port_range[] */
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <net/tcp.h> /* struct or_callable used in sock_rcv_skb */
Paul Moore47180062013-12-04 16:10:45 -050059#include <net/inet_connection_sock.h>
Paul Moore220deb92008-01-29 08:38:23 -050060#include <net/net_namespace.h>
Paul Moored621d352008-01-29 08:43:36 -050061#include <net/netlabel.h>
Eric Parisf5269712008-05-14 11:27:45 -040062#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <asm/ioctls.h>
Arun Sharma600634972011-07-26 16:09:06 -070064#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <linux/bitops.h>
66#include <linux/interrupt.h>
67#include <linux/netdevice.h> /* for network interface checks */
Hong zhi guo77954982013-03-27 06:49:35 +000068#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include <linux/tcp.h>
70#include <linux/udp.h>
James Morris2ee92d42006-11-13 16:09:01 -080071#include <linux/dccp.h>
Richard Hainesd4529302018-02-13 20:57:18 +000072#include <linux/sctp.h>
73#include <net/sctp/structs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#include <linux/quota.h>
75#include <linux/un.h> /* for Unix socket types */
76#include <net/af_unix.h> /* for Unix socket types */
77#include <linux/parser.h>
78#include <linux/nfs_mount.h>
79#include <net/ipv6.h>
80#include <linux/hugetlb.h>
81#include <linux/personality.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#include <linux/audit.h>
Eric Paris6931dfc2005-06-30 02:58:51 -070083#include <linux/string.h>
Catherine Zhang877ce7c2006-06-29 12:27:47 -070084#include <linux/selinux.h>
Eric Paris23970742006-09-25 23:32:01 -070085#include <linux/mutex.h>
Frank Mayharf06febc2008-09-12 09:54:39 -070086#include <linux/posix-timers.h>
Kees Cook00234592010-02-03 15:36:43 -080087#include <linux/syslog.h>
Serge E. Hallyn34867402011-03-23 16:43:17 -070088#include <linux/user_namespace.h>
Paul Gortmaker44fc7ea2011-05-26 20:52:10 -040089#include <linux/export.h>
Al Viro40401532012-02-13 03:58:52 +000090#include <linux/msg.h>
91#include <linux/shm.h>
Chenbo Fengec27c352017-10-18 13:00:25 -070092#include <linux/bpf.h>
David Howellse262e32d2018-11-01 23:07:23 +000093#include <uapi/linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95#include "avc.h"
96#include "objsec.h"
97#include "netif.h"
Paul Moore224dfbd2008-01-29 08:38:13 -050098#include "netnode.h"
Paul Moore3e1121722008-04-10 10:48:14 -040099#include "netport.h"
Daniel Jurgens409dcf32017-05-19 15:48:59 +0300100#include "ibpkey.h"
Trent Jaegerd28d1e02005-12-13 23:12:40 -0800101#include "xfrm.h"
Paul Moorec60475b2007-02-28 15:14:23 -0500102#include "netlabel.h"
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +0200103#include "audit.h"
James Morris7b98a582011-08-30 12:52:32 +1000104#include "avc_ss.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500106struct selinux_state selinux_state;
107
Paul Moored621d352008-01-29 08:43:36 -0500108/* SECMARK reference count */
James Morris56a4ca92011-08-17 11:08:43 +1000109static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0);
Paul Moored621d352008-01-29 08:43:36 -0500110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111#ifdef CONFIG_SECURITY_SELINUX_DEVELOP
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500112static int selinux_enforcing_boot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114static int __init enforcing_setup(char *str)
115{
Eric Parisf5269712008-05-14 11:27:45 -0400116 unsigned long enforcing;
Jingoo Han29707b22014-02-05 15:13:14 +0900117 if (!kstrtoul(str, 0, &enforcing))
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500118 selinux_enforcing_boot = enforcing ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 return 1;
120}
121__setup("enforcing=", enforcing_setup);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500122#else
123#define selinux_enforcing_boot 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124#endif
125
126#ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM
127int selinux_enabled = CONFIG_SECURITY_SELINUX_BOOTPARAM_VALUE;
128
129static int __init selinux_enabled_setup(char *str)
130{
Eric Parisf5269712008-05-14 11:27:45 -0400131 unsigned long enabled;
Jingoo Han29707b22014-02-05 15:13:14 +0900132 if (!kstrtoul(str, 0, &enabled))
Eric Parisf5269712008-05-14 11:27:45 -0400133 selinux_enabled = enabled ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 return 1;
135}
136__setup("selinux=", selinux_enabled_setup);
Stephen Smalley30d55282006-05-03 10:52:36 -0400137#else
138int selinux_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139#endif
140
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500141static unsigned int selinux_checkreqprot_boot =
142 CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE;
143
144static int __init checkreqprot_setup(char *str)
145{
146 unsigned long checkreqprot;
147
148 if (!kstrtoul(str, 0, &checkreqprot))
149 selinux_checkreqprot_boot = checkreqprot ? 1 : 0;
150 return 1;
151}
152__setup("checkreqprot=", checkreqprot_setup);
153
Christoph Lametere18b8902006-12-06 20:33:20 -0800154static struct kmem_cache *sel_inode_cache;
Sangwoo63205652015-10-21 17:44:30 -0400155static struct kmem_cache *file_security_cache;
James Morris7cae7e22006-03-22 00:09:22 -0800156
Paul Moored621d352008-01-29 08:43:36 -0500157/**
158 * selinux_secmark_enabled - Check to see if SECMARK is currently enabled
159 *
160 * Description:
161 * This function checks the SECMARK reference counter to see if any SECMARK
162 * targets are currently configured, if the reference counter is greater than
163 * zero SECMARK is considered to be enabled. Returns true (1) if SECMARK is
Chris PeBenito2be4d742013-05-03 09:05:39 -0400164 * enabled, false (0) if SECMARK is disabled. If the always_check_network
165 * policy capability is enabled, SECMARK is always considered enabled.
Paul Moored621d352008-01-29 08:43:36 -0500166 *
167 */
168static int selinux_secmark_enabled(void)
169{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500170 return (selinux_policycap_alwaysnetwork() ||
171 atomic_read(&selinux_secmark_refcount));
Chris PeBenito2be4d742013-05-03 09:05:39 -0400172}
173
174/**
175 * selinux_peerlbl_enabled - Check to see if peer labeling is currently enabled
176 *
177 * Description:
178 * This function checks if NetLabel or labeled IPSEC is enabled. Returns true
179 * (1) if any are enabled or false (0) if neither are enabled. If the
180 * always_check_network policy capability is enabled, peer labeling
181 * is always considered enabled.
182 *
183 */
184static int selinux_peerlbl_enabled(void)
185{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500186 return (selinux_policycap_alwaysnetwork() ||
187 netlbl_enabled() || selinux_xfrm_enabled());
Paul Moored621d352008-01-29 08:43:36 -0500188}
189
Paul Moore615e51f2014-06-26 14:33:56 -0400190static int selinux_netcache_avc_callback(u32 event)
191{
192 if (event == AVC_CALLBACK_RESET) {
193 sel_netif_flush();
194 sel_netnode_flush();
195 sel_netport_flush();
196 synchronize_net();
197 }
198 return 0;
199}
200
Daniel Jurgens8f408ab2017-05-19 15:48:53 +0300201static int selinux_lsm_notifier_avc_callback(u32 event)
202{
Daniel Jurgens409dcf32017-05-19 15:48:59 +0300203 if (event == AVC_CALLBACK_RESET) {
204 sel_ib_pkey_flush();
Daniel Jurgens8f408ab2017-05-19 15:48:53 +0300205 call_lsm_notifier(LSM_POLICY_CHANGE, NULL);
Daniel Jurgens409dcf32017-05-19 15:48:59 +0300206 }
Daniel Jurgens8f408ab2017-05-19 15:48:53 +0300207
208 return 0;
209}
210
David Howellsd84f4f92008-11-14 10:39:23 +1100211/*
212 * initialise the security for the init task
213 */
214static void cred_init_security(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
David Howells3b11a1d2008-11-14 10:39:26 +1100216 struct cred *cred = (struct cred *) current->real_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 struct task_security_struct *tsec;
218
James Morris89d155e2005-10-30 14:59:21 -0800219 tsec = kzalloc(sizeof(struct task_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 if (!tsec)
David Howellsd84f4f92008-11-14 10:39:23 +1100221 panic("SELinux: Failed to initialize initial task.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
David Howellsd84f4f92008-11-14 10:39:23 +1100223 tsec->osid = tsec->sid = SECINITSID_KERNEL;
David Howellsf1752ee2008-11-14 10:39:17 +1100224 cred->security = tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
David Howells275bb412008-11-14 10:39:19 +1100227/*
David Howells88e67f32008-11-14 10:39:21 +1100228 * get the security ID of a set of credentials
229 */
230static inline u32 cred_sid(const struct cred *cred)
231{
232 const struct task_security_struct *tsec;
233
234 tsec = cred->security;
235 return tsec->sid;
236}
237
238/*
David Howells3b11a1d2008-11-14 10:39:26 +1100239 * get the objective security ID of a task
David Howells275bb412008-11-14 10:39:19 +1100240 */
241static inline u32 task_sid(const struct task_struct *task)
242{
David Howells275bb412008-11-14 10:39:19 +1100243 u32 sid;
244
245 rcu_read_lock();
David Howells88e67f32008-11-14 10:39:21 +1100246 sid = cred_sid(__task_cred(task));
David Howells275bb412008-11-14 10:39:19 +1100247 rcu_read_unlock();
248 return sid;
249}
250
David Howells88e67f32008-11-14 10:39:21 +1100251/* Allocate and free functions for each kind of security blob. */
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253static int inode_alloc_security(struct inode *inode)
254{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 struct inode_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +1100256 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Josef Bacika02fe132008-04-04 09:35:05 +1100258 isec = kmem_cache_zalloc(sel_inode_cache, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 if (!isec)
260 return -ENOMEM;
261
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +0100262 spin_lock_init(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 INIT_LIST_HEAD(&isec->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 isec->inode = inode;
265 isec->sid = SECINITSID_UNLABELED;
266 isec->sclass = SECCLASS_FILE;
David Howells275bb412008-11-14 10:39:19 +1100267 isec->task_sid = sid;
Andreas Gruenbacher42059112016-11-10 22:18:27 +0100268 isec->initialized = LABEL_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 inode->i_security = isec;
270
271 return 0;
272}
273
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500274static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry);
275
276/*
277 * Try reloading inode security labels that have been marked as invalid. The
278 * @may_sleep parameter indicates when sleeping and thus reloading labels is
Andreas Gruenbacher42059112016-11-10 22:18:27 +0100279 * allowed; when set to false, returns -ECHILD when the label is
Al Viroe9193282018-04-24 21:31:02 -0400280 * invalid. The @dentry parameter should be set to a dentry of the inode.
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500281 */
282static int __inode_security_revalidate(struct inode *inode,
Al Viroe9193282018-04-24 21:31:02 -0400283 struct dentry *dentry,
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500284 bool may_sleep)
285{
286 struct inode_security_struct *isec = inode->i_security;
287
288 might_sleep_if(may_sleep);
289
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500290 if (selinux_state.initialized &&
291 isec->initialized != LABEL_INITIALIZED) {
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500292 if (!may_sleep)
293 return -ECHILD;
294
295 /*
296 * Try reloading the inode security label. This will fail if
297 * @opt_dentry is NULL and no dentry for this inode can be
298 * found; in that case, continue using the old label.
299 */
Al Viroe9193282018-04-24 21:31:02 -0400300 inode_doinit_with_dentry(inode, dentry);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500301 }
302 return 0;
303}
304
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500305static struct inode_security_struct *inode_security_novalidate(struct inode *inode)
306{
307 return inode->i_security;
308}
309
310static struct inode_security_struct *inode_security_rcu(struct inode *inode, bool rcu)
311{
312 int error;
313
314 error = __inode_security_revalidate(inode, NULL, !rcu);
315 if (error)
316 return ERR_PTR(error);
317 return inode->i_security;
318}
319
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500320/*
321 * Get the security label of an inode.
322 */
323static struct inode_security_struct *inode_security(struct inode *inode)
324{
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500325 __inode_security_revalidate(inode, NULL, true);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500326 return inode->i_security;
327}
328
Paul Moore2c971652016-04-19 16:36:28 -0400329static struct inode_security_struct *backing_inode_security_novalidate(struct dentry *dentry)
330{
331 struct inode *inode = d_backing_inode(dentry);
332
333 return inode->i_security;
334}
335
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500336/*
337 * Get the security label of a dentry's backing inode.
338 */
339static struct inode_security_struct *backing_inode_security(struct dentry *dentry)
340{
341 struct inode *inode = d_backing_inode(dentry);
342
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500343 __inode_security_revalidate(inode, dentry, true);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500344 return inode->i_security;
345}
346
Steven Rostedt3dc91d42014-01-09 21:46:34 -0500347static void inode_free_rcu(struct rcu_head *head)
348{
349 struct inode_security_struct *isec;
350
351 isec = container_of(head, struct inode_security_struct, rcu);
352 kmem_cache_free(sel_inode_cache, isec);
353}
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355static void inode_free_security(struct inode *inode)
356{
357 struct inode_security_struct *isec = inode->i_security;
358 struct superblock_security_struct *sbsec = inode->i_sb->s_security;
359
Waiman Long9629d042015-07-10 17:19:56 -0400360 /*
361 * As not all inode security structures are in a list, we check for
362 * empty list outside of the lock to make sure that we won't waste
363 * time taking a lock doing nothing.
364 *
365 * The list_del_init() function can be safely called more than once.
366 * It should not be possible for this function to be called with
367 * concurrent list_add(), but for better safety against future changes
368 * in the code, we use list_empty_careful() here.
369 */
370 if (!list_empty_careful(&isec->list)) {
371 spin_lock(&sbsec->isec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 list_del_init(&isec->list);
Waiman Long9629d042015-07-10 17:19:56 -0400373 spin_unlock(&sbsec->isec_lock);
374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Steven Rostedt3dc91d42014-01-09 21:46:34 -0500376 /*
377 * The inode may still be referenced in a path walk and
378 * a call to selinux_inode_permission() can be made
379 * after inode_free_security() is called. Ideally, the VFS
380 * wouldn't do this, but fixing that is a much harder
381 * job. For now, simply free the i_security via RCU, and
382 * leave the current inode->i_security pointer intact.
383 * The inode will be freed after the RCU grace period too.
384 */
385 call_rcu(&isec->rcu, inode_free_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
388static int file_alloc_security(struct file *file)
389{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 struct file_security_struct *fsec;
David Howells275bb412008-11-14 10:39:19 +1100391 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Sangwoo63205652015-10-21 17:44:30 -0400393 fsec = kmem_cache_zalloc(file_security_cache, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 if (!fsec)
395 return -ENOMEM;
396
David Howells275bb412008-11-14 10:39:19 +1100397 fsec->sid = sid;
398 fsec->fown_sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 file->f_security = fsec;
400
401 return 0;
402}
403
404static void file_free_security(struct file *file)
405{
406 struct file_security_struct *fsec = file->f_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 file->f_security = NULL;
Sangwoo63205652015-10-21 17:44:30 -0400408 kmem_cache_free(file_security_cache, fsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409}
410
411static int superblock_alloc_security(struct super_block *sb)
412{
413 struct superblock_security_struct *sbsec;
414
James Morris89d155e2005-10-30 14:59:21 -0800415 sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 if (!sbsec)
417 return -ENOMEM;
418
Eric Parisbc7e9822006-09-25 23:32:02 -0700419 mutex_init(&sbsec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 INIT_LIST_HEAD(&sbsec->isec_head);
421 spin_lock_init(&sbsec->isec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 sbsec->sb = sb;
423 sbsec->sid = SECINITSID_UNLABELED;
424 sbsec->def_sid = SECINITSID_FILE;
Eric Parisc312feb2006-07-10 04:43:53 -0700425 sbsec->mntpoint_sid = SECINITSID_UNLABELED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 sb->s_security = sbsec;
427
428 return 0;
429}
430
431static void superblock_free_security(struct super_block *sb)
432{
433 struct superblock_security_struct *sbsec = sb->s_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 sb->s_security = NULL;
435 kfree(sbsec);
436}
437
Al Virobd323652018-12-13 15:04:59 -0500438struct selinux_mnt_opts {
439 const char *fscontext, *context, *rootcontext, *defcontext;
440};
441
Al Viro204cc0c2018-12-13 13:41:47 -0500442static void selinux_free_mnt_opts(void *mnt_opts)
443{
Al Virobd323652018-12-13 15:04:59 -0500444 struct selinux_mnt_opts *opts = mnt_opts;
445 kfree(opts->fscontext);
446 kfree(opts->context);
447 kfree(opts->rootcontext);
448 kfree(opts->defcontext);
Al Viro204cc0c2018-12-13 13:41:47 -0500449 kfree(opts);
450}
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452static inline int inode_doinit(struct inode *inode)
453{
454 return inode_doinit_with_dentry(inode, NULL);
455}
456
457enum {
Eric Paris31e87932007-09-19 17:19:12 -0400458 Opt_error = -1,
David Howells442155c2018-11-01 23:07:24 +0000459 Opt_context = 0,
460 Opt_defcontext = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 Opt_fscontext = 2,
David Howells442155c2018-11-01 23:07:24 +0000462 Opt_rootcontext = 3,
463 Opt_seclabel = 4,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464};
465
Al Viroda3d76a2018-12-17 10:14:16 -0500466#define A(s, has_arg) {#s, sizeof(#s) - 1, Opt_##s, has_arg}
Al Viro169d68efb2018-12-14 22:44:50 -0500467static struct {
468 const char *name;
469 int len;
470 int opt;
471 bool has_arg;
472} tokens[] = {
Al Viroda3d76a2018-12-17 10:14:16 -0500473 A(context, true),
474 A(fscontext, true),
475 A(defcontext, true),
476 A(rootcontext, true),
477 A(seclabel, false),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478};
Al Viro169d68efb2018-12-14 22:44:50 -0500479#undef A
480
481static int match_opt_prefix(char *s, int l, char **arg)
482{
483 int i;
484
485 for (i = 0; i < ARRAY_SIZE(tokens); i++) {
486 size_t len = tokens[i].len;
487 if (len > l || memcmp(s, tokens[i].name, len))
488 continue;
489 if (tokens[i].has_arg) {
490 if (len == l || s[len] != '=')
491 continue;
492 *arg = s + len + 1;
493 } else if (len != l)
494 continue;
495 return tokens[i].opt;
496 }
497 return Opt_error;
498}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500#define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n"
501
Eric Parisc312feb2006-07-10 04:43:53 -0700502static int may_context_mount_sb_relabel(u32 sid,
503 struct superblock_security_struct *sbsec,
David Howells275bb412008-11-14 10:39:19 +1100504 const struct cred *cred)
Eric Parisc312feb2006-07-10 04:43:53 -0700505{
David Howells275bb412008-11-14 10:39:19 +1100506 const struct task_security_struct *tsec = cred->security;
Eric Parisc312feb2006-07-10 04:43:53 -0700507 int rc;
508
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500509 rc = avc_has_perm(&selinux_state,
510 tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
Eric Parisc312feb2006-07-10 04:43:53 -0700511 FILESYSTEM__RELABELFROM, NULL);
512 if (rc)
513 return rc;
514
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500515 rc = avc_has_perm(&selinux_state,
516 tsec->sid, sid, SECCLASS_FILESYSTEM,
Eric Parisc312feb2006-07-10 04:43:53 -0700517 FILESYSTEM__RELABELTO, NULL);
518 return rc;
519}
520
Eric Paris08089252006-07-10 04:43:55 -0700521static int may_context_mount_inode_relabel(u32 sid,
522 struct superblock_security_struct *sbsec,
David Howells275bb412008-11-14 10:39:19 +1100523 const struct cred *cred)
Eric Paris08089252006-07-10 04:43:55 -0700524{
David Howells275bb412008-11-14 10:39:19 +1100525 const struct task_security_struct *tsec = cred->security;
Eric Paris08089252006-07-10 04:43:55 -0700526 int rc;
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500527 rc = avc_has_perm(&selinux_state,
528 tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
Eric Paris08089252006-07-10 04:43:55 -0700529 FILESYSTEM__RELABELFROM, NULL);
530 if (rc)
531 return rc;
532
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500533 rc = avc_has_perm(&selinux_state,
534 sid, sbsec->sid, SECCLASS_FILESYSTEM,
Eric Paris08089252006-07-10 04:43:55 -0700535 FILESYSTEM__ASSOCIATE, NULL);
536 return rc;
537}
538
Eric Parisb43e7252012-10-10 14:27:35 -0400539static int selinux_is_sblabel_mnt(struct super_block *sb)
540{
541 struct superblock_security_struct *sbsec = sb->s_security;
542
Mark Salyzynd5f3a5f2015-02-04 11:34:30 -0500543 return sbsec->behavior == SECURITY_FS_USE_XATTR ||
544 sbsec->behavior == SECURITY_FS_USE_TRANS ||
545 sbsec->behavior == SECURITY_FS_USE_TASK ||
J. Bruce Fields9fc2b4b2015-06-04 15:57:25 -0400546 sbsec->behavior == SECURITY_FS_USE_NATIVE ||
Mark Salyzynd5f3a5f2015-02-04 11:34:30 -0500547 /* Special handling. Genfs but also in-core setxattr handler */
548 !strcmp(sb->s_type->name, "sysfs") ||
549 !strcmp(sb->s_type->name, "pstore") ||
550 !strcmp(sb->s_type->name, "debugfs") ||
Yongqin Liua2c7c6f2017-01-09 10:07:30 -0500551 !strcmp(sb->s_type->name, "tracefs") ||
Stephen Smalley2651225b2017-02-28 10:35:56 -0500552 !strcmp(sb->s_type->name, "rootfs") ||
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500553 (selinux_policycap_cgroupseclabel() &&
Stephen Smalley2651225b2017-02-28 10:35:56 -0500554 (!strcmp(sb->s_type->name, "cgroup") ||
555 !strcmp(sb->s_type->name, "cgroup2")));
Eric Parisb43e7252012-10-10 14:27:35 -0400556}
557
Eric Parisc9180a52007-11-30 13:00:35 -0500558static int sb_finish_set_opts(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
560 struct superblock_security_struct *sbsec = sb->s_security;
561 struct dentry *root = sb->s_root;
David Howellsc6f493d2015-03-17 22:26:22 +0000562 struct inode *root_inode = d_backing_inode(root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 int rc = 0;
564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
566 /* Make sure that the xattr handler exists and that no
567 error other than -ENODATA is returned by getxattr on
568 the root directory. -ENODATA is ok, as this may be
569 the first boot of the SELinux kernel before we have
570 assigned xattr values to the filesystem. */
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200571 if (!(root_inode->i_opflags & IOP_XATTR)) {
peter enderborgc103a912018-06-12 10:09:03 +0200572 pr_warn("SELinux: (dev %s, type %s) has no "
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800573 "xattr support\n", sb->s_id, sb->s_type->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 rc = -EOPNOTSUPP;
575 goto out;
576 }
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200577
578 rc = __vfs_getxattr(root, root_inode, XATTR_NAME_SELINUX, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if (rc < 0 && rc != -ENODATA) {
580 if (rc == -EOPNOTSUPP)
peter enderborgc103a912018-06-12 10:09:03 +0200581 pr_warn("SELinux: (dev %s, type "
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800582 "%s) has no security xattr handler\n",
583 sb->s_id, sb->s_type->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 else
peter enderborgc103a912018-06-12 10:09:03 +0200585 pr_warn("SELinux: (dev %s, type "
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800586 "%s) getxattr errno %d\n", sb->s_id,
587 sb->s_type->name, -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 goto out;
589 }
590 }
591
Eric Pariseadcabc2012-08-24 15:59:14 -0400592 sbsec->flags |= SE_SBINITIALIZED;
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400593
594 /*
595 * Explicitly set or clear SBLABEL_MNT. It's not sufficient to simply
596 * leave the flag untouched because sb_clone_mnt_opts might be handing
597 * us a superblock that needs the flag to be cleared.
598 */
Eric Parisb43e7252012-10-10 14:27:35 -0400599 if (selinux_is_sblabel_mnt(sb))
Eric Paris12f348b2012-10-09 10:56:25 -0400600 sbsec->flags |= SBLABEL_MNT;
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400601 else
602 sbsec->flags &= ~SBLABEL_MNT;
David P. Quigleyddd29ec2009-09-09 14:25:37 -0400603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 /* Initialize the root inode. */
Eric Parisc9180a52007-11-30 13:00:35 -0500605 rc = inode_doinit_with_dentry(root_inode, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 /* Initialize any other inodes associated with the superblock, e.g.
608 inodes created prior to initial policy load or inodes created
609 during get_sb by a pseudo filesystem that directly
610 populates itself. */
611 spin_lock(&sbsec->isec_lock);
Al Viro8d641242018-12-10 15:34:12 -0500612 while (!list_empty(&sbsec->isec_head)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 struct inode_security_struct *isec =
Al Viro8d641242018-12-10 15:34:12 -0500614 list_first_entry(&sbsec->isec_head,
Eric Parisc9180a52007-11-30 13:00:35 -0500615 struct inode_security_struct, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 struct inode *inode = isec->inode;
Stephen Smalley923190d2014-10-06 16:32:52 -0400617 list_del_init(&isec->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 spin_unlock(&sbsec->isec_lock);
619 inode = igrab(inode);
620 if (inode) {
Eric Parisc9180a52007-11-30 13:00:35 -0500621 if (!IS_PRIVATE(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 inode_doinit(inode);
623 iput(inode);
624 }
625 spin_lock(&sbsec->isec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
627 spin_unlock(&sbsec->isec_lock);
628out:
Eric Parisc9180a52007-11-30 13:00:35 -0500629 return rc;
630}
631
Eric Parisc9180a52007-11-30 13:00:35 -0500632static int bad_option(struct superblock_security_struct *sbsec, char flag,
633 u32 old_sid, u32 new_sid)
634{
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500635 char mnt_flags = sbsec->flags & SE_MNTMASK;
636
Eric Parisc9180a52007-11-30 13:00:35 -0500637 /* check if the old mount command had the same options */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500638 if (sbsec->flags & SE_SBINITIALIZED)
Eric Parisc9180a52007-11-30 13:00:35 -0500639 if (!(sbsec->flags & flag) ||
640 (old_sid != new_sid))
641 return 1;
642
643 /* check if we were passed the same options twice,
644 * aka someone passed context=a,context=b
645 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500646 if (!(sbsec->flags & SE_SBINITIALIZED))
647 if (mnt_flags & flag)
Eric Parisc9180a52007-11-30 13:00:35 -0500648 return 1;
649 return 0;
650}
Eric Parise0007522008-03-05 10:31:54 -0500651
Al Virobd323652018-12-13 15:04:59 -0500652static int parse_sid(struct super_block *sb, const char *s, u32 *sid)
653{
654 int rc = security_context_str_to_sid(&selinux_state, s,
655 sid, GFP_KERNEL);
656 if (rc)
657 pr_warn("SELinux: security_context_str_to_sid"
658 "(%s) failed for (dev %s, type %s) errno=%d\n",
659 s, sb->s_id, sb->s_type->name, rc);
660 return rc;
661}
662
Eric Parisc9180a52007-11-30 13:00:35 -0500663/*
664 * Allow filesystems with binary mount data to explicitly set mount point
665 * labeling information.
666 */
Eric Parise0007522008-03-05 10:31:54 -0500667static int selinux_set_mnt_opts(struct super_block *sb,
Al Viro204cc0c2018-12-13 13:41:47 -0500668 void *mnt_opts,
David Quigley649f6e72013-05-22 12:50:36 -0400669 unsigned long kern_flags,
670 unsigned long *set_kern_flags)
Eric Parisc9180a52007-11-30 13:00:35 -0500671{
David Howells275bb412008-11-14 10:39:19 +1100672 const struct cred *cred = current_cred();
Eric Parisc9180a52007-11-30 13:00:35 -0500673 struct superblock_security_struct *sbsec = sb->s_security;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500674 struct dentry *root = sbsec->sb->s_root;
Al Virobd323652018-12-13 15:04:59 -0500675 struct selinux_mnt_opts *opts = mnt_opts;
Paul Moore2c971652016-04-19 16:36:28 -0400676 struct inode_security_struct *root_isec;
Eric Parisc9180a52007-11-30 13:00:35 -0500677 u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
678 u32 defcontext_sid = 0;
Al Virobd323652018-12-13 15:04:59 -0500679 int rc = 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500680
681 mutex_lock(&sbsec->lock);
682
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500683 if (!selinux_state.initialized) {
Al Virobd323652018-12-13 15:04:59 -0500684 if (!opts) {
Eric Parisc9180a52007-11-30 13:00:35 -0500685 /* Defer initialization until selinux_complete_init,
686 after the initial policy is loaded and the security
687 server is ready to handle calls. */
Eric Parisc9180a52007-11-30 13:00:35 -0500688 goto out;
689 }
690 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +0200691 pr_warn("SELinux: Unable to set superblock options "
Eric Paris744ba352008-04-17 11:52:44 -0400692 "before the security server is initialized\n");
Eric Parisc9180a52007-11-30 13:00:35 -0500693 goto out;
694 }
David Quigley649f6e72013-05-22 12:50:36 -0400695 if (kern_flags && !set_kern_flags) {
696 /* Specifying internal flags without providing a place to
697 * place the results is not allowed */
698 rc = -EINVAL;
699 goto out;
700 }
Eric Parisc9180a52007-11-30 13:00:35 -0500701
702 /*
Eric Parise0007522008-03-05 10:31:54 -0500703 * Binary mount data FS will come through this function twice. Once
704 * from an explicit call and once from the generic calls from the vfs.
705 * Since the generic VFS calls will not contain any security mount data
706 * we need to skip the double mount verification.
707 *
708 * This does open a hole in which we will not notice if the first
709 * mount using this sb set explict options and a second mount using
710 * this sb does not set any security options. (The first options
711 * will be used for both mounts)
712 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500713 if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
Al Virobd323652018-12-13 15:04:59 -0500714 && !opts)
Eric Parisf5269712008-05-14 11:27:45 -0400715 goto out;
Eric Parise0007522008-03-05 10:31:54 -0500716
Paul Moore2c971652016-04-19 16:36:28 -0400717 root_isec = backing_inode_security_novalidate(root);
718
Eric Parise0007522008-03-05 10:31:54 -0500719 /*
Eric Parisc9180a52007-11-30 13:00:35 -0500720 * parse the mount options, check if they are valid sids.
721 * also check if someone is trying to mount the same sb more
722 * than once with different security options.
723 */
Al Virobd323652018-12-13 15:04:59 -0500724 if (opts) {
725 if (opts->fscontext) {
726 rc = parse_sid(sb, opts->fscontext, &fscontext_sid);
727 if (rc)
728 goto out;
Eric Parisc9180a52007-11-30 13:00:35 -0500729 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
730 fscontext_sid))
731 goto out_double_mount;
Eric Parisc9180a52007-11-30 13:00:35 -0500732 sbsec->flags |= FSCONTEXT_MNT;
Al Virobd323652018-12-13 15:04:59 -0500733 }
734 if (opts->context) {
735 rc = parse_sid(sb, opts->context, &context_sid);
736 if (rc)
737 goto out;
Eric Parisc9180a52007-11-30 13:00:35 -0500738 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
739 context_sid))
740 goto out_double_mount;
Eric Parisc9180a52007-11-30 13:00:35 -0500741 sbsec->flags |= CONTEXT_MNT;
Al Virobd323652018-12-13 15:04:59 -0500742 }
743 if (opts->rootcontext) {
744 rc = parse_sid(sb, opts->rootcontext, &rootcontext_sid);
745 if (rc)
746 goto out;
Eric Parisc9180a52007-11-30 13:00:35 -0500747 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
748 rootcontext_sid))
749 goto out_double_mount;
Eric Parisc9180a52007-11-30 13:00:35 -0500750 sbsec->flags |= ROOTCONTEXT_MNT;
Al Virobd323652018-12-13 15:04:59 -0500751 }
752 if (opts->defcontext) {
753 rc = parse_sid(sb, opts->defcontext, &defcontext_sid);
754 if (rc)
755 goto out;
Eric Parisc9180a52007-11-30 13:00:35 -0500756 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
757 defcontext_sid))
758 goto out_double_mount;
Eric Parisc9180a52007-11-30 13:00:35 -0500759 sbsec->flags |= DEFCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500760 }
761 }
762
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500763 if (sbsec->flags & SE_SBINITIALIZED) {
Eric Parisc9180a52007-11-30 13:00:35 -0500764 /* previously mounted with options, but not on this attempt? */
Al Virobd323652018-12-13 15:04:59 -0500765 if ((sbsec->flags & SE_MNTMASK) && !opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500766 goto out_double_mount;
767 rc = 0;
768 goto out;
769 }
770
James Morris089be432008-07-15 18:32:49 +1000771 if (strcmp(sb->s_type->name, "proc") == 0)
Stephen Smalley134509d2015-06-04 16:22:17 -0400772 sbsec->flags |= SE_SBPROC | SE_SBGENFS;
773
Stephen Smalley8e014722015-06-04 16:22:17 -0400774 if (!strcmp(sb->s_type->name, "debugfs") ||
Jeff Vander Stoep6a391182017-06-20 09:35:33 -0700775 !strcmp(sb->s_type->name, "tracefs") ||
Stephen Smalley8e014722015-06-04 16:22:17 -0400776 !strcmp(sb->s_type->name, "sysfs") ||
Antonio Murdaca901ef842017-02-09 17:02:42 +0100777 !strcmp(sb->s_type->name, "pstore") ||
778 !strcmp(sb->s_type->name, "cgroup") ||
779 !strcmp(sb->s_type->name, "cgroup2"))
Stephen Smalley134509d2015-06-04 16:22:17 -0400780 sbsec->flags |= SE_SBGENFS;
Eric Parisc9180a52007-11-30 13:00:35 -0500781
David Quigleyeb9ae682013-05-22 12:50:37 -0400782 if (!sbsec->behavior) {
783 /*
784 * Determine the labeling behavior to use for this
785 * filesystem type.
786 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500787 rc = security_fs_use(&selinux_state, sb);
David Quigleyeb9ae682013-05-22 12:50:37 -0400788 if (rc) {
peter enderborgc103a912018-06-12 10:09:03 +0200789 pr_warn("%s: security_fs_use(%s) returned %d\n",
David Quigleyeb9ae682013-05-22 12:50:37 -0400790 __func__, sb->s_type->name, rc);
791 goto out;
792 }
Eric Parisc9180a52007-11-30 13:00:35 -0500793 }
Seth Forsheeaad82892016-04-26 14:36:20 -0500794
795 /*
Stephen Smalley01593d32017-01-09 10:07:31 -0500796 * If this is a user namespace mount and the filesystem type is not
797 * explicitly whitelisted, then no contexts are allowed on the command
798 * line and security labels must be ignored.
Seth Forsheeaad82892016-04-26 14:36:20 -0500799 */
Stephen Smalley01593d32017-01-09 10:07:31 -0500800 if (sb->s_user_ns != &init_user_ns &&
801 strcmp(sb->s_type->name, "tmpfs") &&
802 strcmp(sb->s_type->name, "ramfs") &&
803 strcmp(sb->s_type->name, "devpts")) {
Seth Forsheeaad82892016-04-26 14:36:20 -0500804 if (context_sid || fscontext_sid || rootcontext_sid ||
805 defcontext_sid) {
806 rc = -EACCES;
807 goto out;
808 }
809 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
810 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500811 rc = security_transition_sid(&selinux_state,
812 current_sid(),
813 current_sid(),
Seth Forsheeaad82892016-04-26 14:36:20 -0500814 SECCLASS_FILE, NULL,
815 &sbsec->mntpoint_sid);
816 if (rc)
817 goto out;
818 }
819 goto out_set_opts;
820 }
821
Eric Parisc9180a52007-11-30 13:00:35 -0500822 /* sets the context of the superblock for the fs being mounted. */
823 if (fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100824 rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500825 if (rc)
826 goto out;
827
828 sbsec->sid = fscontext_sid;
829 }
830
831 /*
832 * Switch to using mount point labeling behavior.
833 * sets the label used on all file below the mountpoint, and will set
834 * the superblock context if not already set.
835 */
David Quigleyeb9ae682013-05-22 12:50:37 -0400836 if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !context_sid) {
837 sbsec->behavior = SECURITY_FS_USE_NATIVE;
838 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
839 }
840
Eric Parisc9180a52007-11-30 13:00:35 -0500841 if (context_sid) {
842 if (!fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100843 rc = may_context_mount_sb_relabel(context_sid, sbsec,
844 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500845 if (rc)
846 goto out;
847 sbsec->sid = context_sid;
848 } else {
David Howells275bb412008-11-14 10:39:19 +1100849 rc = may_context_mount_inode_relabel(context_sid, sbsec,
850 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500851 if (rc)
852 goto out;
853 }
854 if (!rootcontext_sid)
855 rootcontext_sid = context_sid;
856
857 sbsec->mntpoint_sid = context_sid;
858 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
859 }
860
861 if (rootcontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100862 rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec,
863 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500864 if (rc)
865 goto out;
866
867 root_isec->sid = rootcontext_sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -0500868 root_isec->initialized = LABEL_INITIALIZED;
Eric Parisc9180a52007-11-30 13:00:35 -0500869 }
870
871 if (defcontext_sid) {
David Quigleyeb9ae682013-05-22 12:50:37 -0400872 if (sbsec->behavior != SECURITY_FS_USE_XATTR &&
873 sbsec->behavior != SECURITY_FS_USE_NATIVE) {
Eric Parisc9180a52007-11-30 13:00:35 -0500874 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +0200875 pr_warn("SELinux: defcontext option is "
Eric Parisc9180a52007-11-30 13:00:35 -0500876 "invalid for this filesystem type\n");
877 goto out;
878 }
879
880 if (defcontext_sid != sbsec->def_sid) {
881 rc = may_context_mount_inode_relabel(defcontext_sid,
David Howells275bb412008-11-14 10:39:19 +1100882 sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500883 if (rc)
884 goto out;
885 }
886
887 sbsec->def_sid = defcontext_sid;
888 }
889
Seth Forsheeaad82892016-04-26 14:36:20 -0500890out_set_opts:
Eric Parisc9180a52007-11-30 13:00:35 -0500891 rc = sb_finish_set_opts(sb);
892out:
Eric Parisbc7e9822006-09-25 23:32:02 -0700893 mutex_unlock(&sbsec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 return rc;
Eric Parisc9180a52007-11-30 13:00:35 -0500895out_double_mount:
896 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +0200897 pr_warn("SELinux: mount invalid. Same superblock, different "
Al Virobd323652018-12-13 15:04:59 -0500898 "security settings for (dev %s, type %s)\n", sb->s_id,
899 sb->s_type->name);
Eric Parisc9180a52007-11-30 13:00:35 -0500900 goto out;
901}
902
Jeff Layton094f7b62013-04-01 08:14:24 -0400903static int selinux_cmp_sb_context(const struct super_block *oldsb,
904 const struct super_block *newsb)
905{
906 struct superblock_security_struct *old = oldsb->s_security;
907 struct superblock_security_struct *new = newsb->s_security;
908 char oldflags = old->flags & SE_MNTMASK;
909 char newflags = new->flags & SE_MNTMASK;
910
911 if (oldflags != newflags)
912 goto mismatch;
913 if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid)
914 goto mismatch;
915 if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid)
916 goto mismatch;
917 if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid)
918 goto mismatch;
919 if (oldflags & ROOTCONTEXT_MNT) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500920 struct inode_security_struct *oldroot = backing_inode_security(oldsb->s_root);
921 struct inode_security_struct *newroot = backing_inode_security(newsb->s_root);
Jeff Layton094f7b62013-04-01 08:14:24 -0400922 if (oldroot->sid != newroot->sid)
923 goto mismatch;
924 }
925 return 0;
926mismatch:
peter enderborgc103a912018-06-12 10:09:03 +0200927 pr_warn("SELinux: mount invalid. Same superblock, "
Jeff Layton094f7b62013-04-01 08:14:24 -0400928 "different security settings for (dev %s, "
929 "type %s)\n", newsb->s_id, newsb->s_type->name);
930 return -EBUSY;
931}
932
933static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400934 struct super_block *newsb,
935 unsigned long kern_flags,
936 unsigned long *set_kern_flags)
Eric Parisc9180a52007-11-30 13:00:35 -0500937{
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400938 int rc = 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500939 const struct superblock_security_struct *oldsbsec = oldsb->s_security;
940 struct superblock_security_struct *newsbsec = newsb->s_security;
941
942 int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT);
943 int set_context = (oldsbsec->flags & CONTEXT_MNT);
944 int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT);
945
Eric Paris0f5e6422008-04-21 16:24:11 -0400946 /*
947 * if the parent was able to be mounted it clearly had no special lsm
Al Viroe8c26252010-03-23 06:36:54 -0400948 * mount options. thus we can safely deal with this superblock later
Eric Paris0f5e6422008-04-21 16:24:11 -0400949 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500950 if (!selinux_state.initialized)
Jeff Layton094f7b62013-04-01 08:14:24 -0400951 return 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500952
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400953 /*
954 * Specifying internal flags without providing a place to
955 * place the results is not allowed.
956 */
957 if (kern_flags && !set_kern_flags)
958 return -EINVAL;
959
Eric Parisc9180a52007-11-30 13:00:35 -0500960 /* how can we clone if the old one wasn't set up?? */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500961 BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED));
Eric Parisc9180a52007-11-30 13:00:35 -0500962
Jeff Layton094f7b62013-04-01 08:14:24 -0400963 /* if fs is reusing a sb, make sure that the contexts match */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500964 if (newsbsec->flags & SE_SBINITIALIZED)
Jeff Layton094f7b62013-04-01 08:14:24 -0400965 return selinux_cmp_sb_context(oldsb, newsb);
Eric Paris5a552612008-04-09 14:08:35 -0400966
Eric Parisc9180a52007-11-30 13:00:35 -0500967 mutex_lock(&newsbsec->lock);
968
969 newsbsec->flags = oldsbsec->flags;
970
971 newsbsec->sid = oldsbsec->sid;
972 newsbsec->def_sid = oldsbsec->def_sid;
973 newsbsec->behavior = oldsbsec->behavior;
974
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400975 if (newsbsec->behavior == SECURITY_FS_USE_NATIVE &&
976 !(kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500977 rc = security_fs_use(&selinux_state, newsb);
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400978 if (rc)
979 goto out;
980 }
981
982 if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !set_context) {
983 newsbsec->behavior = SECURITY_FS_USE_NATIVE;
984 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
985 }
986
Eric Parisc9180a52007-11-30 13:00:35 -0500987 if (set_context) {
988 u32 sid = oldsbsec->mntpoint_sid;
989
990 if (!set_fscontext)
991 newsbsec->sid = sid;
992 if (!set_rootcontext) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500993 struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
Eric Parisc9180a52007-11-30 13:00:35 -0500994 newisec->sid = sid;
995 }
996 newsbsec->mntpoint_sid = sid;
997 }
998 if (set_rootcontext) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500999 const struct inode_security_struct *oldisec = backing_inode_security(oldsb->s_root);
1000 struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
Eric Parisc9180a52007-11-30 13:00:35 -05001001
1002 newisec->sid = oldisec->sid;
1003 }
1004
1005 sb_finish_set_opts(newsb);
Scott Mayhew0b4d3452017-06-05 11:45:04 -04001006out:
Eric Parisc9180a52007-11-30 13:00:35 -05001007 mutex_unlock(&newsbsec->lock);
Scott Mayhew0b4d3452017-06-05 11:45:04 -04001008 return rc;
Eric Parisc9180a52007-11-30 13:00:35 -05001009}
1010
Al Viroba641862018-12-14 20:28:15 -05001011static int selinux_add_opt(int token, const char *s, void **mnt_opts)
Eric Parisc9180a52007-11-30 13:00:35 -05001012{
Al Viroba641862018-12-14 20:28:15 -05001013 struct selinux_mnt_opts *opts = *mnt_opts;
Eric Parisc9180a52007-11-30 13:00:35 -05001014
Al Viroda3d76a2018-12-17 10:14:16 -05001015 if (token == Opt_seclabel) /* eaten and completely ignored */
Al Viro169d68efb2018-12-14 22:44:50 -05001016 return 0;
Eric Parisc9180a52007-11-30 13:00:35 -05001017
Al Viroba641862018-12-14 20:28:15 -05001018 if (!opts) {
1019 opts = kzalloc(sizeof(struct selinux_mnt_opts), GFP_KERNEL);
1020 if (!opts)
1021 return -ENOMEM;
1022 *mnt_opts = opts;
1023 }
1024 if (!s)
1025 return -ENOMEM;
1026 switch (token) {
1027 case Opt_context:
1028 if (opts->context || opts->defcontext)
1029 goto Einval;
1030 opts->context = s;
1031 break;
1032 case Opt_fscontext:
1033 if (opts->fscontext)
1034 goto Einval;
1035 opts->fscontext = s;
1036 break;
1037 case Opt_rootcontext:
1038 if (opts->rootcontext)
1039 goto Einval;
1040 opts->rootcontext = s;
1041 break;
1042 case Opt_defcontext:
1043 if (opts->context || opts->defcontext)
1044 goto Einval;
1045 opts->defcontext = s;
1046 break;
1047 }
1048 return 0;
1049Einval:
1050 pr_warn(SEL_MOUNT_FAIL_MSG);
Al Viroba641862018-12-14 20:28:15 -05001051 return -EINVAL;
1052}
Eric Parisc9180a52007-11-30 13:00:35 -05001053
Al Viro757cbe52018-12-14 23:42:21 -05001054static int selinux_add_mnt_opt(const char *option, const char *val, int len,
1055 void **mnt_opts)
Eric Parisc9180a52007-11-30 13:00:35 -05001056{
Al Viro757cbe52018-12-14 23:42:21 -05001057 int token = Opt_error;
1058 int rc, i;
Eric Parisc9180a52007-11-30 13:00:35 -05001059
Al Viro757cbe52018-12-14 23:42:21 -05001060 for (i = 0; i < ARRAY_SIZE(tokens); i++) {
1061 if (strcmp(option, tokens[i].name) == 0) {
1062 token = tokens[i].opt;
Eric Parisc9180a52007-11-30 13:00:35 -05001063 break;
Eric Parisc9180a52007-11-30 13:00:35 -05001064 }
1065 }
1066
Al Viro757cbe52018-12-14 23:42:21 -05001067 if (token == Opt_error)
1068 return -EINVAL;
Eric Parise0007522008-03-05 10:31:54 -05001069
Al Viro757cbe52018-12-14 23:42:21 -05001070 if (token != Opt_seclabel)
1071 val = kmemdup_nul(val, len, GFP_KERNEL);
1072 rc = selinux_add_opt(token, val, mnt_opts);
1073 if (unlikely(rc)) {
1074 kfree(val);
1075 if (*mnt_opts) {
1076 selinux_free_mnt_opts(*mnt_opts);
1077 *mnt_opts = NULL;
1078 }
1079 }
1080 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Al Viroe3489f82018-12-13 00:24:36 -05001083static int show_sid(struct seq_file *m, u32 sid)
Eric Paris2069f452008-07-04 09:47:13 +10001084{
Al Viroe3489f82018-12-13 00:24:36 -05001085 char *context = NULL;
1086 u32 len;
1087 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Al Viroe3489f82018-12-13 00:24:36 -05001089 rc = security_sid_to_context(&selinux_state, sid,
1090 &context, &len);
1091 if (!rc) {
1092 bool has_comma = context && strchr(context, ',');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
David Howells442155c2018-11-01 23:07:24 +00001094 seq_putc(m, '=');
Eric Paris2069f452008-07-04 09:47:13 +10001095 if (has_comma)
1096 seq_putc(m, '\"');
Al Viroe3489f82018-12-13 00:24:36 -05001097 seq_escape(m, context, "\"\n\\");
Eric Paris2069f452008-07-04 09:47:13 +10001098 if (has_comma)
1099 seq_putc(m, '\"');
1100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 kfree(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 return rc;
1103}
Eric Paris2069f452008-07-04 09:47:13 +10001104
1105static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
1106{
Al Viroe3489f82018-12-13 00:24:36 -05001107 struct superblock_security_struct *sbsec = sb->s_security;
Eric Paris2069f452008-07-04 09:47:13 +10001108 int rc;
1109
Al Viroe3489f82018-12-13 00:24:36 -05001110 if (!(sbsec->flags & SE_SBINITIALIZED))
1111 return 0;
1112
1113 if (!selinux_state.initialized)
1114 return 0;
1115
1116 if (sbsec->flags & FSCONTEXT_MNT) {
1117 seq_putc(m, ',');
1118 seq_puts(m, FSCONTEXT_STR);
1119 rc = show_sid(m, sbsec->sid);
1120 if (rc)
1121 return rc;
Eric Paris383795c2008-07-29 17:07:26 -04001122 }
Al Viroe3489f82018-12-13 00:24:36 -05001123 if (sbsec->flags & CONTEXT_MNT) {
1124 seq_putc(m, ',');
1125 seq_puts(m, CONTEXT_STR);
1126 rc = show_sid(m, sbsec->mntpoint_sid);
1127 if (rc)
1128 return rc;
1129 }
1130 if (sbsec->flags & DEFCONTEXT_MNT) {
1131 seq_putc(m, ',');
1132 seq_puts(m, DEFCONTEXT_STR);
1133 rc = show_sid(m, sbsec->def_sid);
1134 if (rc)
1135 return rc;
1136 }
1137 if (sbsec->flags & ROOTCONTEXT_MNT) {
1138 struct dentry *root = sbsec->sb->s_root;
1139 struct inode_security_struct *isec = backing_inode_security(root);
1140 seq_putc(m, ',');
1141 seq_puts(m, ROOTCONTEXT_STR);
1142 rc = show_sid(m, isec->sid);
1143 if (rc)
1144 return rc;
1145 }
1146 if (sbsec->flags & SBLABEL_MNT) {
1147 seq_putc(m, ',');
David Howells442155c2018-11-01 23:07:24 +00001148 seq_puts(m, SECLABEL_STR);
Al Viroe3489f82018-12-13 00:24:36 -05001149 }
1150 return 0;
Eric Paris2069f452008-07-04 09:47:13 +10001151}
1152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153static inline u16 inode_mode_to_security_class(umode_t mode)
1154{
1155 switch (mode & S_IFMT) {
1156 case S_IFSOCK:
1157 return SECCLASS_SOCK_FILE;
1158 case S_IFLNK:
1159 return SECCLASS_LNK_FILE;
1160 case S_IFREG:
1161 return SECCLASS_FILE;
1162 case S_IFBLK:
1163 return SECCLASS_BLK_FILE;
1164 case S_IFDIR:
1165 return SECCLASS_DIR;
1166 case S_IFCHR:
1167 return SECCLASS_CHR_FILE;
1168 case S_IFIFO:
1169 return SECCLASS_FIFO_FILE;
1170
1171 }
1172
1173 return SECCLASS_FILE;
1174}
1175
James Morris13402582005-09-30 14:24:34 -04001176static inline int default_protocol_stream(int protocol)
1177{
1178 return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP);
1179}
1180
1181static inline int default_protocol_dgram(int protocol)
1182{
1183 return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
1184}
1185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186static inline u16 socket_type_to_security_class(int family, int type, int protocol)
1187{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001188 int extsockclass = selinux_policycap_extsockclass();
Stephen Smalleyda69a532017-01-09 10:07:30 -05001189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 switch (family) {
1191 case PF_UNIX:
1192 switch (type) {
1193 case SOCK_STREAM:
1194 case SOCK_SEQPACKET:
1195 return SECCLASS_UNIX_STREAM_SOCKET;
1196 case SOCK_DGRAM:
Luis Ressel2a764b52017-07-25 15:13:41 -04001197 case SOCK_RAW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 return SECCLASS_UNIX_DGRAM_SOCKET;
1199 }
1200 break;
1201 case PF_INET:
1202 case PF_INET6:
1203 switch (type) {
1204 case SOCK_STREAM:
Stephen Smalleyda69a532017-01-09 10:07:30 -05001205 case SOCK_SEQPACKET:
James Morris13402582005-09-30 14:24:34 -04001206 if (default_protocol_stream(protocol))
1207 return SECCLASS_TCP_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001208 else if (extsockclass && protocol == IPPROTO_SCTP)
1209 return SECCLASS_SCTP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001210 else
1211 return SECCLASS_RAWIP_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 case SOCK_DGRAM:
James Morris13402582005-09-30 14:24:34 -04001213 if (default_protocol_dgram(protocol))
1214 return SECCLASS_UDP_SOCKET;
Stephen Smalleyef379792017-01-09 10:07:31 -05001215 else if (extsockclass && (protocol == IPPROTO_ICMP ||
1216 protocol == IPPROTO_ICMPV6))
Stephen Smalleyda69a532017-01-09 10:07:30 -05001217 return SECCLASS_ICMP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001218 else
1219 return SECCLASS_RAWIP_SOCKET;
James Morris2ee92d42006-11-13 16:09:01 -08001220 case SOCK_DCCP:
1221 return SECCLASS_DCCP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001222 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 return SECCLASS_RAWIP_SOCKET;
1224 }
1225 break;
1226 case PF_NETLINK:
1227 switch (protocol) {
1228 case NETLINK_ROUTE:
1229 return SECCLASS_NETLINK_ROUTE_SOCKET;
Pavel Emelyanov7f1fb602011-12-06 07:56:43 +00001230 case NETLINK_SOCK_DIAG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 return SECCLASS_NETLINK_TCPDIAG_SOCKET;
1232 case NETLINK_NFLOG:
1233 return SECCLASS_NETLINK_NFLOG_SOCKET;
1234 case NETLINK_XFRM:
1235 return SECCLASS_NETLINK_XFRM_SOCKET;
1236 case NETLINK_SELINUX:
1237 return SECCLASS_NETLINK_SELINUX_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001238 case NETLINK_ISCSI:
1239 return SECCLASS_NETLINK_ISCSI_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 case NETLINK_AUDIT:
1241 return SECCLASS_NETLINK_AUDIT_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001242 case NETLINK_FIB_LOOKUP:
1243 return SECCLASS_NETLINK_FIB_LOOKUP_SOCKET;
1244 case NETLINK_CONNECTOR:
1245 return SECCLASS_NETLINK_CONNECTOR_SOCKET;
1246 case NETLINK_NETFILTER:
1247 return SECCLASS_NETLINK_NETFILTER_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 case NETLINK_DNRTMSG:
1249 return SECCLASS_NETLINK_DNRT_SOCKET;
James Morris0c9b7942005-04-16 15:24:13 -07001250 case NETLINK_KOBJECT_UEVENT:
1251 return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001252 case NETLINK_GENERIC:
1253 return SECCLASS_NETLINK_GENERIC_SOCKET;
1254 case NETLINK_SCSITRANSPORT:
1255 return SECCLASS_NETLINK_SCSITRANSPORT_SOCKET;
1256 case NETLINK_RDMA:
1257 return SECCLASS_NETLINK_RDMA_SOCKET;
1258 case NETLINK_CRYPTO:
1259 return SECCLASS_NETLINK_CRYPTO_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 default:
1261 return SECCLASS_NETLINK_SOCKET;
1262 }
1263 case PF_PACKET:
1264 return SECCLASS_PACKET_SOCKET;
1265 case PF_KEY:
1266 return SECCLASS_KEY_SOCKET;
Christopher J. PeBenito3e3ff152006-06-09 00:25:03 -07001267 case PF_APPLETALK:
1268 return SECCLASS_APPLETALK_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 }
1270
Stephen Smalleyda69a532017-01-09 10:07:30 -05001271 if (extsockclass) {
1272 switch (family) {
1273 case PF_AX25:
1274 return SECCLASS_AX25_SOCKET;
1275 case PF_IPX:
1276 return SECCLASS_IPX_SOCKET;
1277 case PF_NETROM:
1278 return SECCLASS_NETROM_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001279 case PF_ATMPVC:
1280 return SECCLASS_ATMPVC_SOCKET;
1281 case PF_X25:
1282 return SECCLASS_X25_SOCKET;
1283 case PF_ROSE:
1284 return SECCLASS_ROSE_SOCKET;
1285 case PF_DECnet:
1286 return SECCLASS_DECNET_SOCKET;
1287 case PF_ATMSVC:
1288 return SECCLASS_ATMSVC_SOCKET;
1289 case PF_RDS:
1290 return SECCLASS_RDS_SOCKET;
1291 case PF_IRDA:
1292 return SECCLASS_IRDA_SOCKET;
1293 case PF_PPPOX:
1294 return SECCLASS_PPPOX_SOCKET;
1295 case PF_LLC:
1296 return SECCLASS_LLC_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001297 case PF_CAN:
1298 return SECCLASS_CAN_SOCKET;
1299 case PF_TIPC:
1300 return SECCLASS_TIPC_SOCKET;
1301 case PF_BLUETOOTH:
1302 return SECCLASS_BLUETOOTH_SOCKET;
1303 case PF_IUCV:
1304 return SECCLASS_IUCV_SOCKET;
1305 case PF_RXRPC:
1306 return SECCLASS_RXRPC_SOCKET;
1307 case PF_ISDN:
1308 return SECCLASS_ISDN_SOCKET;
1309 case PF_PHONET:
1310 return SECCLASS_PHONET_SOCKET;
1311 case PF_IEEE802154:
1312 return SECCLASS_IEEE802154_SOCKET;
1313 case PF_CAIF:
1314 return SECCLASS_CAIF_SOCKET;
1315 case PF_ALG:
1316 return SECCLASS_ALG_SOCKET;
1317 case PF_NFC:
1318 return SECCLASS_NFC_SOCKET;
1319 case PF_VSOCK:
1320 return SECCLASS_VSOCK_SOCKET;
1321 case PF_KCM:
1322 return SECCLASS_KCM_SOCKET;
1323 case PF_QIPCRTR:
1324 return SECCLASS_QIPCRTR_SOCKET;
Linus Torvalds3051bf32017-02-22 10:15:09 -08001325 case PF_SMC:
1326 return SECCLASS_SMC_SOCKET;
Björn Töpel68e8b842018-05-02 13:01:22 +02001327 case PF_XDP:
1328 return SECCLASS_XDP_SOCKET;
1329#if PF_MAX > 45
Stephen Smalleyda69a532017-01-09 10:07:30 -05001330#error New address family defined, please update this function.
1331#endif
1332 }
1333 }
1334
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 return SECCLASS_SOCKET;
1336}
1337
Stephen Smalley134509d2015-06-04 16:22:17 -04001338static int selinux_genfs_get_sid(struct dentry *dentry,
1339 u16 tclass,
1340 u16 flags,
1341 u32 *sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001343 int rc;
Al Virofc640052016-04-10 01:33:30 -04001344 struct super_block *sb = dentry->d_sb;
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001345 char *buffer, *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
Eric Paris828dfe12008-04-17 13:17:49 -04001347 buffer = (char *)__get_free_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 if (!buffer)
1349 return -ENOMEM;
1350
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001351 path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
1352 if (IS_ERR(path))
1353 rc = PTR_ERR(path);
1354 else {
Stephen Smalley134509d2015-06-04 16:22:17 -04001355 if (flags & SE_SBPROC) {
1356 /* each process gets a /proc/PID/ entry. Strip off the
1357 * PID part to get a valid selinux labeling.
1358 * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
1359 while (path[1] >= '0' && path[1] <= '9') {
1360 path[1] = '/';
1361 path++;
1362 }
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001363 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001364 rc = security_genfs_sid(&selinux_state, sb->s_type->name,
1365 path, tclass, sid);
Stephen Smalley7bb185e2018-09-04 16:51:36 -04001366 if (rc == -ENOENT) {
1367 /* No match in policy, mark as unlabeled. */
1368 *sid = SECINITSID_UNLABELED;
1369 rc = 0;
1370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 free_page((unsigned long)buffer);
1373 return rc;
1374}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
1376/* The inode's security attributes must be initialized before first use. */
1377static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
1378{
1379 struct superblock_security_struct *sbsec = NULL;
1380 struct inode_security_struct *isec = inode->i_security;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001381 u32 task_sid, sid = 0;
1382 u16 sclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 struct dentry *dentry;
1384#define INITCONTEXTLEN 255
1385 char *context = NULL;
1386 unsigned len = 0;
1387 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05001389 if (isec->initialized == LABEL_INITIALIZED)
Andreas Gruenbacher13457d02016-11-10 22:18:29 +01001390 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001392 spin_lock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05001393 if (isec->initialized == LABEL_INITIALIZED)
Eric Paris23970742006-09-25 23:32:01 -07001394 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
Andreas Gruenbacher13457d02016-11-10 22:18:29 +01001396 if (isec->sclass == SECCLASS_FILE)
1397 isec->sclass = inode_mode_to_security_class(inode->i_mode);
1398
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 sbsec = inode->i_sb->s_security;
David P. Quigley0d90a7e2009-01-16 09:22:02 -05001400 if (!(sbsec->flags & SE_SBINITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 /* Defer initialization until selinux_complete_init,
1402 after the initial policy is loaded and the security
1403 server is ready to handle calls. */
1404 spin_lock(&sbsec->isec_lock);
1405 if (list_empty(&isec->list))
1406 list_add(&isec->list, &sbsec->isec_head);
1407 spin_unlock(&sbsec->isec_lock);
Eric Paris23970742006-09-25 23:32:01 -07001408 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 }
1410
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001411 sclass = isec->sclass;
1412 task_sid = isec->task_sid;
1413 sid = isec->sid;
1414 isec->initialized = LABEL_PENDING;
1415 spin_unlock(&isec->lock);
1416
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 switch (sbsec->behavior) {
David Quigleyeb9ae682013-05-22 12:50:37 -04001418 case SECURITY_FS_USE_NATIVE:
1419 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 case SECURITY_FS_USE_XATTR:
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001421 if (!(inode->i_opflags & IOP_XATTR)) {
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001422 sid = sbsec->def_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 break;
1424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 /* Need a dentry, since the xattr API requires one.
1426 Life would be simpler if we could just pass the inode. */
1427 if (opt_dentry) {
1428 /* Called from d_instantiate or d_splice_alias. */
1429 dentry = dget(opt_dentry);
1430 } else {
Al Virob1271252018-04-25 10:28:38 -04001431 /*
1432 * Called from selinux_complete_init, try to find a dentry.
1433 * Some filesystems really want a connected one, so try
1434 * that first. We could split SECURITY_FS_USE_XATTR in
1435 * two, depending upon that...
1436 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 dentry = d_find_alias(inode);
Al Virob1271252018-04-25 10:28:38 -04001438 if (!dentry)
1439 dentry = d_find_any_alias(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 }
1441 if (!dentry) {
Eric Parisdf7f54c2009-03-09 14:35:58 -04001442 /*
1443 * this is can be hit on boot when a file is accessed
1444 * before the policy is loaded. When we load policy we
1445 * may find inodes that have no dentry on the
1446 * sbsec->isec_head list. No reason to complain as these
1447 * will get fixed up the next time we go through
1448 * inode_doinit with a dentry, before these inodes could
1449 * be used again by userspace.
1450 */
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001451 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 }
1453
1454 len = INITCONTEXTLEN;
Eric Paris4cb912f2009-02-12 14:50:05 -05001455 context = kmalloc(len+1, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 if (!context) {
1457 rc = -ENOMEM;
1458 dput(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001459 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 }
Eric Paris4cb912f2009-02-12 14:50:05 -05001461 context[len] = '\0';
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001462 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 if (rc == -ERANGE) {
James Morris314dabb2009-08-10 22:00:13 +10001464 kfree(context);
1465
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 /* Need a larger buffer. Query for the right size. */
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001467 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 if (rc < 0) {
1469 dput(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001470 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 len = rc;
Eric Paris4cb912f2009-02-12 14:50:05 -05001473 context = kmalloc(len+1, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 if (!context) {
1475 rc = -ENOMEM;
1476 dput(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001477 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 }
Eric Paris4cb912f2009-02-12 14:50:05 -05001479 context[len] = '\0';
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001480 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 }
1482 dput(dentry);
1483 if (rc < 0) {
1484 if (rc != -ENODATA) {
peter enderborgc103a912018-06-12 10:09:03 +02001485 pr_warn("SELinux: %s: getxattr returned "
Harvey Harrisondd6f9532008-03-06 10:03:59 +11001486 "%d for dev=%s ino=%ld\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 -rc, inode->i_sb->s_id, inode->i_ino);
1488 kfree(context);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001489 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 }
1491 /* Map ENODATA to the default file SID */
1492 sid = sbsec->def_sid;
1493 rc = 0;
1494 } else {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001495 rc = security_context_to_sid_default(&selinux_state,
1496 context, rc, &sid,
Stephen Smalley869ab512008-04-04 08:46:05 -04001497 sbsec->def_sid,
1498 GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 if (rc) {
Eric Paris4ba0a8a2009-02-12 15:01:10 -05001500 char *dev = inode->i_sb->s_id;
1501 unsigned long ino = inode->i_ino;
1502
1503 if (rc == -EINVAL) {
1504 if (printk_ratelimit())
peter enderborgc103a912018-06-12 10:09:03 +02001505 pr_notice("SELinux: inode=%lu on dev=%s was found to have an invalid "
Eric Paris4ba0a8a2009-02-12 15:01:10 -05001506 "context=%s. This indicates you may need to relabel the inode or the "
1507 "filesystem in question.\n", ino, dev, context);
1508 } else {
peter enderborgc103a912018-06-12 10:09:03 +02001509 pr_warn("SELinux: %s: context_to_sid(%s) "
Eric Paris4ba0a8a2009-02-12 15:01:10 -05001510 "returned %d for dev=%s ino=%ld\n",
1511 __func__, context, -rc, dev, ino);
1512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 kfree(context);
1514 /* Leave with the unlabeled SID */
1515 rc = 0;
1516 break;
1517 }
1518 }
1519 kfree(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 break;
1521 case SECURITY_FS_USE_TASK:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001522 sid = task_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 break;
1524 case SECURITY_FS_USE_TRANS:
1525 /* Default to the fs SID. */
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001526 sid = sbsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
1528 /* Try to obtain a transition SID. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001529 rc = security_transition_sid(&selinux_state, task_sid, sid,
1530 sclass, NULL, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 if (rc)
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001532 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 break;
Eric Parisc312feb2006-07-10 04:43:53 -07001534 case SECURITY_FS_USE_MNTPOINT:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001535 sid = sbsec->mntpoint_sid;
Eric Parisc312feb2006-07-10 04:43:53 -07001536 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 default:
Eric Parisc312feb2006-07-10 04:43:53 -07001538 /* Default to the fs superblock SID. */
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001539 sid = sbsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
Stephen Smalley134509d2015-06-04 16:22:17 -04001541 if ((sbsec->flags & SE_SBGENFS) && !S_ISLNK(inode->i_mode)) {
Paul Mooref64410e2014-03-19 16:46:18 -04001542 /* We must have a dentry to determine the label on
1543 * procfs inodes */
Al Virob1271252018-04-25 10:28:38 -04001544 if (opt_dentry) {
Paul Mooref64410e2014-03-19 16:46:18 -04001545 /* Called from d_instantiate or
1546 * d_splice_alias. */
1547 dentry = dget(opt_dentry);
Al Virob1271252018-04-25 10:28:38 -04001548 } else {
Paul Mooref64410e2014-03-19 16:46:18 -04001549 /* Called from selinux_complete_init, try to
Al Virob1271252018-04-25 10:28:38 -04001550 * find a dentry. Some filesystems really want
1551 * a connected one, so try that first.
1552 */
Paul Mooref64410e2014-03-19 16:46:18 -04001553 dentry = d_find_alias(inode);
Al Virob1271252018-04-25 10:28:38 -04001554 if (!dentry)
1555 dentry = d_find_any_alias(inode);
1556 }
Paul Mooref64410e2014-03-19 16:46:18 -04001557 /*
1558 * This can be hit on boot when a file is accessed
1559 * before the policy is loaded. When we load policy we
1560 * may find inodes that have no dentry on the
1561 * sbsec->isec_head list. No reason to complain as
1562 * these will get fixed up the next time we go through
1563 * inode_doinit() with a dentry, before these inodes
1564 * could be used again by userspace.
1565 */
1566 if (!dentry)
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001567 goto out;
1568 rc = selinux_genfs_get_sid(dentry, sclass,
Stephen Smalley134509d2015-06-04 16:22:17 -04001569 sbsec->flags, &sid);
Paul Mooref64410e2014-03-19 16:46:18 -04001570 dput(dentry);
1571 if (rc)
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001572 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 }
1574 break;
1575 }
1576
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001577out:
1578 spin_lock(&isec->lock);
1579 if (isec->initialized == LABEL_PENDING) {
1580 if (!sid || rc) {
1581 isec->initialized = LABEL_INVALID;
1582 goto out_unlock;
1583 }
1584
1585 isec->initialized = LABEL_INITIALIZED;
1586 isec->sid = sid;
1587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
Eric Paris23970742006-09-25 23:32:01 -07001589out_unlock:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001590 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 return rc;
1592}
1593
1594/* Convert a Linux signal to an access vector. */
1595static inline u32 signal_to_av(int sig)
1596{
1597 u32 perm = 0;
1598
1599 switch (sig) {
1600 case SIGCHLD:
1601 /* Commonly granted from child to parent. */
1602 perm = PROCESS__SIGCHLD;
1603 break;
1604 case SIGKILL:
1605 /* Cannot be caught or ignored */
1606 perm = PROCESS__SIGKILL;
1607 break;
1608 case SIGSTOP:
1609 /* Cannot be caught or ignored */
1610 perm = PROCESS__SIGSTOP;
1611 break;
1612 default:
1613 /* All other signals. */
1614 perm = PROCESS__SIGNAL;
1615 break;
1616 }
1617
1618 return perm;
1619}
1620
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001621#if CAP_LAST_CAP > 63
1622#error Fix SELinux to handle capabilities > 63.
1623#endif
1624
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625/* Check whether a task is allowed to use a capability. */
Eric Paris6a9de492012-01-03 12:25:14 -05001626static int cred_has_capability(const struct cred *cred,
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04001627 int cap, int audit, bool initns)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628{
Thomas Liu2bf49692009-07-14 12:14:09 -04001629 struct common_audit_data ad;
Eric Paris06112162008-11-11 22:02:50 +11001630 struct av_decision avd;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001631 u16 sclass;
David Howells3699c532009-01-06 22:27:01 +00001632 u32 sid = cred_sid(cred);
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001633 u32 av = CAP_TO_MASK(cap);
Eric Paris06112162008-11-11 22:02:50 +11001634 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
Eric Paris50c205f2012-04-04 15:01:43 -04001636 ad.type = LSM_AUDIT_DATA_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 ad.u.cap = cap;
1638
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001639 switch (CAP_TO_INDEX(cap)) {
1640 case 0:
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04001641 sclass = initns ? SECCLASS_CAPABILITY : SECCLASS_CAP_USERNS;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001642 break;
1643 case 1:
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04001644 sclass = initns ? SECCLASS_CAPABILITY2 : SECCLASS_CAP2_USERNS;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001645 break;
1646 default:
peter enderborgc103a912018-06-12 10:09:03 +02001647 pr_err("SELinux: out of range capability %d\n", cap);
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001648 BUG();
Eric Parisa35c6c832011-04-20 10:21:28 -04001649 return -EINVAL;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001650 }
Eric Paris06112162008-11-11 22:02:50 +11001651
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001652 rc = avc_has_perm_noaudit(&selinux_state,
1653 sid, sid, sclass, av, 0, &avd);
Eric Paris9ade0cf2011-04-25 16:26:29 -04001654 if (audit == SECURITY_CAP_AUDIT) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001655 int rc2 = avc_audit(&selinux_state,
1656 sid, sid, sclass, av, &avd, rc, &ad, 0);
Eric Paris9ade0cf2011-04-25 16:26:29 -04001657 if (rc2)
1658 return rc2;
1659 }
Eric Paris06112162008-11-11 22:02:50 +11001660 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661}
1662
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663/* Check whether a task has a particular permission to an inode.
1664 The 'adp' parameter is optional and allows other audit
1665 data to be passed (e.g. the dentry). */
David Howells88e67f32008-11-14 10:39:21 +11001666static int inode_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 struct inode *inode,
1668 u32 perms,
Linus Torvalds19e49832013-10-04 12:54:11 -07001669 struct common_audit_data *adp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 struct inode_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +11001672 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
David Howellse0e81732009-09-02 09:13:40 +01001674 validate_creds(cred);
1675
Eric Paris828dfe12008-04-17 13:17:49 -04001676 if (unlikely(IS_PRIVATE(inode)))
Stephen Smalleybbaca6c2007-02-14 00:34:16 -08001677 return 0;
1678
David Howells88e67f32008-11-14 10:39:21 +11001679 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 isec = inode->i_security;
1681
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001682 return avc_has_perm(&selinux_state,
1683 sid, isec->sid, isec->sclass, perms, adp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684}
1685
1686/* Same as inode_has_perm, but pass explicit audit data containing
1687 the dentry to help the auditing code to more easily generate the
1688 pathname if needed. */
David Howells88e67f32008-11-14 10:39:21 +11001689static inline int dentry_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 struct dentry *dentry,
1691 u32 av)
1692{
David Howellsc6f493d2015-03-17 22:26:22 +00001693 struct inode *inode = d_backing_inode(dentry);
Thomas Liu2bf49692009-07-14 12:14:09 -04001694 struct common_audit_data ad;
David Howells88e67f32008-11-14 10:39:21 +11001695
Eric Paris50c205f2012-04-04 15:01:43 -04001696 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Paris2875fa02011-04-28 16:04:24 -04001697 ad.u.dentry = dentry;
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05001698 __inode_security_revalidate(inode, dentry, true);
Linus Torvalds19e49832013-10-04 12:54:11 -07001699 return inode_has_perm(cred, inode, av, &ad);
Eric Paris2875fa02011-04-28 16:04:24 -04001700}
1701
1702/* Same as inode_has_perm, but pass explicit audit data containing
1703 the path to help the auditing code to more easily generate the
1704 pathname if needed. */
1705static inline int path_has_perm(const struct cred *cred,
Al Viro3f7036a2015-03-08 19:28:30 -04001706 const struct path *path,
Eric Paris2875fa02011-04-28 16:04:24 -04001707 u32 av)
1708{
David Howellsc6f493d2015-03-17 22:26:22 +00001709 struct inode *inode = d_backing_inode(path->dentry);
Eric Paris2875fa02011-04-28 16:04:24 -04001710 struct common_audit_data ad;
1711
Eric Paris50c205f2012-04-04 15:01:43 -04001712 ad.type = LSM_AUDIT_DATA_PATH;
Eric Paris2875fa02011-04-28 16:04:24 -04001713 ad.u.path = *path;
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05001714 __inode_security_revalidate(inode, path->dentry, true);
Linus Torvalds19e49832013-10-04 12:54:11 -07001715 return inode_has_perm(cred, inode, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716}
1717
David Howells13f8e982013-06-13 23:37:55 +01001718/* Same as path_has_perm, but uses the inode from the file struct. */
1719static inline int file_path_has_perm(const struct cred *cred,
1720 struct file *file,
1721 u32 av)
1722{
1723 struct common_audit_data ad;
1724
Vivek Goyal43af5de2016-09-09 11:37:49 -04001725 ad.type = LSM_AUDIT_DATA_FILE;
1726 ad.u.file = file;
Linus Torvalds19e49832013-10-04 12:54:11 -07001727 return inode_has_perm(cred, file_inode(file), av, &ad);
David Howells13f8e982013-06-13 23:37:55 +01001728}
1729
Chenbo Fengf66e4482017-10-18 13:00:26 -07001730#ifdef CONFIG_BPF_SYSCALL
1731static int bpf_fd_pass(struct file *file, u32 sid);
1732#endif
1733
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734/* Check whether a task can use an open file descriptor to
1735 access an inode in a given way. Check access to the
1736 descriptor itself, and then use dentry_has_perm to
1737 check a particular permission to the file.
1738 Access to the descriptor is implicitly granted if it
1739 has the same SID as the process. If av is zero, then
1740 access to the file is not checked, e.g. for cases
1741 where only the descriptor is affected like seek. */
David Howells88e67f32008-11-14 10:39:21 +11001742static int file_has_perm(const struct cred *cred,
1743 struct file *file,
1744 u32 av)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 struct file_security_struct *fsec = file->f_security;
Al Viro496ad9a2013-01-23 17:07:38 -05001747 struct inode *inode = file_inode(file);
Thomas Liu2bf49692009-07-14 12:14:09 -04001748 struct common_audit_data ad;
David Howells88e67f32008-11-14 10:39:21 +11001749 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 int rc;
1751
Vivek Goyal43af5de2016-09-09 11:37:49 -04001752 ad.type = LSM_AUDIT_DATA_FILE;
1753 ad.u.file = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
David Howells275bb412008-11-14 10:39:19 +11001755 if (sid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001756 rc = avc_has_perm(&selinux_state,
1757 sid, fsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 SECCLASS_FD,
1759 FD__USE,
1760 &ad);
1761 if (rc)
David Howells88e67f32008-11-14 10:39:21 +11001762 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 }
1764
Chenbo Fengf66e4482017-10-18 13:00:26 -07001765#ifdef CONFIG_BPF_SYSCALL
1766 rc = bpf_fd_pass(file, cred_sid(cred));
1767 if (rc)
1768 return rc;
1769#endif
1770
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 /* av is zero if only checking access to the descriptor. */
David Howells88e67f32008-11-14 10:39:21 +11001772 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 if (av)
Linus Torvalds19e49832013-10-04 12:54:11 -07001774 rc = inode_has_perm(cred, inode, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
David Howells88e67f32008-11-14 10:39:21 +11001776out:
1777 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778}
1779
David Howellsc3c188b2015-07-10 17:19:58 -04001780/*
1781 * Determine the label for an inode that might be unioned.
1782 */
Vivek Goyalc957f6d2016-07-13 10:44:51 -04001783static int
1784selinux_determine_inode_label(const struct task_security_struct *tsec,
1785 struct inode *dir,
1786 const struct qstr *name, u16 tclass,
1787 u32 *_new_isid)
David Howellsc3c188b2015-07-10 17:19:58 -04001788{
1789 const struct superblock_security_struct *sbsec = dir->i_sb->s_security;
David Howellsc3c188b2015-07-10 17:19:58 -04001790
1791 if ((sbsec->flags & SE_SBINITIALIZED) &&
1792 (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)) {
1793 *_new_isid = sbsec->mntpoint_sid;
1794 } else if ((sbsec->flags & SBLABEL_MNT) &&
1795 tsec->create_sid) {
1796 *_new_isid = tsec->create_sid;
1797 } else {
Paul Moore20cdef82016-04-04 14:14:42 -04001798 const struct inode_security_struct *dsec = inode_security(dir);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001799 return security_transition_sid(&selinux_state, tsec->sid,
1800 dsec->sid, tclass,
David Howellsc3c188b2015-07-10 17:19:58 -04001801 name, _new_isid);
1802 }
1803
1804 return 0;
1805}
1806
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807/* Check whether a task can create a file. */
1808static int may_create(struct inode *dir,
1809 struct dentry *dentry,
1810 u16 tclass)
1811{
Paul Moore5fb49872010-04-22 14:46:19 -04001812 const struct task_security_struct *tsec = current_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 struct inode_security_struct *dsec;
1814 struct superblock_security_struct *sbsec;
David Howells275bb412008-11-14 10:39:19 +11001815 u32 sid, newsid;
Thomas Liu2bf49692009-07-14 12:14:09 -04001816 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 int rc;
1818
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001819 dsec = inode_security(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 sbsec = dir->i_sb->s_security;
1821
David Howells275bb412008-11-14 10:39:19 +11001822 sid = tsec->sid;
David Howells275bb412008-11-14 10:39:19 +11001823
Eric Paris50c205f2012-04-04 15:01:43 -04001824 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04001825 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001827 rc = avc_has_perm(&selinux_state,
1828 sid, dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 DIR__ADD_NAME | DIR__SEARCH,
1830 &ad);
1831 if (rc)
1832 return rc;
1833
Vivek Goyalc957f6d2016-07-13 10:44:51 -04001834 rc = selinux_determine_inode_label(current_security(), dir,
1835 &dentry->d_name, tclass, &newsid);
David Howellsc3c188b2015-07-10 17:19:58 -04001836 if (rc)
1837 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001839 rc = avc_has_perm(&selinux_state,
1840 sid, newsid, tclass, FILE__CREATE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 if (rc)
1842 return rc;
1843
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001844 return avc_has_perm(&selinux_state,
1845 newsid, sbsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 SECCLASS_FILESYSTEM,
1847 FILESYSTEM__ASSOCIATE, &ad);
1848}
1849
Eric Paris828dfe12008-04-17 13:17:49 -04001850#define MAY_LINK 0
1851#define MAY_UNLINK 1
1852#define MAY_RMDIR 2
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
1854/* Check whether a task can link, unlink, or rmdir a file/directory. */
1855static int may_link(struct inode *dir,
1856 struct dentry *dentry,
1857 int kind)
1858
1859{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 struct inode_security_struct *dsec, *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001861 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11001862 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 u32 av;
1864 int rc;
1865
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001866 dsec = inode_security(dir);
1867 isec = backing_inode_security(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
Eric Paris50c205f2012-04-04 15:01:43 -04001869 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04001870 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
1872 av = DIR__SEARCH;
1873 av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001874 rc = avc_has_perm(&selinux_state,
1875 sid, dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 if (rc)
1877 return rc;
1878
1879 switch (kind) {
1880 case MAY_LINK:
1881 av = FILE__LINK;
1882 break;
1883 case MAY_UNLINK:
1884 av = FILE__UNLINK;
1885 break;
1886 case MAY_RMDIR:
1887 av = DIR__RMDIR;
1888 break;
1889 default:
peter enderborgc103a912018-06-12 10:09:03 +02001890 pr_warn("SELinux: %s: unrecognized kind %d\n",
Eric Paris744ba352008-04-17 11:52:44 -04001891 __func__, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 return 0;
1893 }
1894
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001895 rc = avc_has_perm(&selinux_state,
1896 sid, isec->sid, isec->sclass, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 return rc;
1898}
1899
1900static inline int may_rename(struct inode *old_dir,
1901 struct dentry *old_dentry,
1902 struct inode *new_dir,
1903 struct dentry *new_dentry)
1904{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001906 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11001907 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 u32 av;
1909 int old_is_dir, new_is_dir;
1910 int rc;
1911
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001912 old_dsec = inode_security(old_dir);
1913 old_isec = backing_inode_security(old_dentry);
David Howellse36cb0b2015-01-29 12:02:35 +00001914 old_is_dir = d_is_dir(old_dentry);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001915 new_dsec = inode_security(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
Eric Paris50c205f2012-04-04 15:01:43 -04001917 ad.type = LSM_AUDIT_DATA_DENTRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
Eric Parisa2694342011-04-25 13:10:27 -04001919 ad.u.dentry = old_dentry;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001920 rc = avc_has_perm(&selinux_state,
1921 sid, old_dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 DIR__REMOVE_NAME | DIR__SEARCH, &ad);
1923 if (rc)
1924 return rc;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001925 rc = avc_has_perm(&selinux_state,
1926 sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 old_isec->sclass, FILE__RENAME, &ad);
1928 if (rc)
1929 return rc;
1930 if (old_is_dir && new_dir != old_dir) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001931 rc = avc_has_perm(&selinux_state,
1932 sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 old_isec->sclass, DIR__REPARENT, &ad);
1934 if (rc)
1935 return rc;
1936 }
1937
Eric Parisa2694342011-04-25 13:10:27 -04001938 ad.u.dentry = new_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 av = DIR__ADD_NAME | DIR__SEARCH;
David Howells2c616d42015-01-29 12:02:33 +00001940 if (d_is_positive(new_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 av |= DIR__REMOVE_NAME;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001942 rc = avc_has_perm(&selinux_state,
1943 sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 if (rc)
1945 return rc;
David Howells2c616d42015-01-29 12:02:33 +00001946 if (d_is_positive(new_dentry)) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001947 new_isec = backing_inode_security(new_dentry);
David Howellse36cb0b2015-01-29 12:02:35 +00001948 new_is_dir = d_is_dir(new_dentry);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001949 rc = avc_has_perm(&selinux_state,
1950 sid, new_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 new_isec->sclass,
1952 (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
1953 if (rc)
1954 return rc;
1955 }
1956
1957 return 0;
1958}
1959
1960/* Check whether a task can perform a filesystem operation. */
David Howells88e67f32008-11-14 10:39:21 +11001961static int superblock_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 struct super_block *sb,
1963 u32 perms,
Thomas Liu2bf49692009-07-14 12:14:09 -04001964 struct common_audit_data *ad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 struct superblock_security_struct *sbsec;
David Howells88e67f32008-11-14 10:39:21 +11001967 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 sbsec = sb->s_security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001970 return avc_has_perm(&selinux_state,
1971 sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972}
1973
1974/* Convert a Linux mode and permission mask to an access vector. */
1975static inline u32 file_mask_to_av(int mode, int mask)
1976{
1977 u32 av = 0;
1978
Al Virodba19c62011-07-25 20:49:29 -04001979 if (!S_ISDIR(mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 if (mask & MAY_EXEC)
1981 av |= FILE__EXECUTE;
1982 if (mask & MAY_READ)
1983 av |= FILE__READ;
1984
1985 if (mask & MAY_APPEND)
1986 av |= FILE__APPEND;
1987 else if (mask & MAY_WRITE)
1988 av |= FILE__WRITE;
1989
1990 } else {
1991 if (mask & MAY_EXEC)
1992 av |= DIR__SEARCH;
1993 if (mask & MAY_WRITE)
1994 av |= DIR__WRITE;
1995 if (mask & MAY_READ)
1996 av |= DIR__READ;
1997 }
1998
1999 return av;
2000}
2001
2002/* Convert a Linux file to an access vector. */
2003static inline u32 file_to_av(struct file *file)
2004{
2005 u32 av = 0;
2006
2007 if (file->f_mode & FMODE_READ)
2008 av |= FILE__READ;
2009 if (file->f_mode & FMODE_WRITE) {
2010 if (file->f_flags & O_APPEND)
2011 av |= FILE__APPEND;
2012 else
2013 av |= FILE__WRITE;
2014 }
Stephen Smalley0794c662008-03-17 08:55:18 -04002015 if (!av) {
2016 /*
2017 * Special file opened with flags 3 for ioctl-only use.
2018 */
2019 av = FILE__IOCTL;
2020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
2022 return av;
2023}
2024
Eric Paris8b6a5a32008-10-29 17:06:46 -04002025/*
2026 * Convert a file to an access vector and include the correct open
2027 * open permission.
2028 */
2029static inline u32 open_file_to_av(struct file *file)
2030{
2031 u32 av = file_to_av(file);
Stephen Smalleyccb54472017-05-12 12:41:24 -04002032 struct inode *inode = file_inode(file);
Eric Paris8b6a5a32008-10-29 17:06:46 -04002033
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002034 if (selinux_policycap_openperm() &&
2035 inode->i_sb->s_magic != SOCKFS_MAGIC)
Eric Paris49b7b8d2010-07-23 11:44:09 -04002036 av |= FILE__OPEN;
2037
Eric Paris8b6a5a32008-10-29 17:06:46 -04002038 return av;
2039}
2040
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041/* Hook functions begin here. */
2042
Stephen Smalley79af7302015-01-21 10:54:10 -05002043static int selinux_binder_set_context_mgr(struct task_struct *mgr)
2044{
2045 u32 mysid = current_sid();
2046 u32 mgrsid = task_sid(mgr);
2047
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002048 return avc_has_perm(&selinux_state,
2049 mysid, mgrsid, SECCLASS_BINDER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002050 BINDER__SET_CONTEXT_MGR, NULL);
2051}
2052
2053static int selinux_binder_transaction(struct task_struct *from,
2054 struct task_struct *to)
2055{
2056 u32 mysid = current_sid();
2057 u32 fromsid = task_sid(from);
2058 u32 tosid = task_sid(to);
2059 int rc;
2060
2061 if (mysid != fromsid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002062 rc = avc_has_perm(&selinux_state,
2063 mysid, fromsid, SECCLASS_BINDER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002064 BINDER__IMPERSONATE, NULL);
2065 if (rc)
2066 return rc;
2067 }
2068
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002069 return avc_has_perm(&selinux_state,
2070 fromsid, tosid, SECCLASS_BINDER, BINDER__CALL,
Stephen Smalley79af7302015-01-21 10:54:10 -05002071 NULL);
2072}
2073
2074static int selinux_binder_transfer_binder(struct task_struct *from,
2075 struct task_struct *to)
2076{
2077 u32 fromsid = task_sid(from);
2078 u32 tosid = task_sid(to);
2079
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002080 return avc_has_perm(&selinux_state,
2081 fromsid, tosid, SECCLASS_BINDER, BINDER__TRANSFER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002082 NULL);
2083}
2084
2085static int selinux_binder_transfer_file(struct task_struct *from,
2086 struct task_struct *to,
2087 struct file *file)
2088{
2089 u32 sid = task_sid(to);
2090 struct file_security_struct *fsec = file->f_security;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002091 struct dentry *dentry = file->f_path.dentry;
Paul Moore20cdef82016-04-04 14:14:42 -04002092 struct inode_security_struct *isec;
Stephen Smalley79af7302015-01-21 10:54:10 -05002093 struct common_audit_data ad;
2094 int rc;
2095
2096 ad.type = LSM_AUDIT_DATA_PATH;
2097 ad.u.path = file->f_path;
2098
2099 if (sid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002100 rc = avc_has_perm(&selinux_state,
2101 sid, fsec->sid,
Stephen Smalley79af7302015-01-21 10:54:10 -05002102 SECCLASS_FD,
2103 FD__USE,
2104 &ad);
2105 if (rc)
2106 return rc;
2107 }
2108
Chenbo Fengf66e4482017-10-18 13:00:26 -07002109#ifdef CONFIG_BPF_SYSCALL
2110 rc = bpf_fd_pass(file, sid);
2111 if (rc)
2112 return rc;
2113#endif
2114
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002115 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
Stephen Smalley79af7302015-01-21 10:54:10 -05002116 return 0;
2117
Paul Moore20cdef82016-04-04 14:14:42 -04002118 isec = backing_inode_security(dentry);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002119 return avc_has_perm(&selinux_state,
2120 sid, isec->sid, isec->sclass, file_to_av(file),
Stephen Smalley79af7302015-01-21 10:54:10 -05002121 &ad);
2122}
2123
Ingo Molnar9e488582009-05-07 19:26:19 +10002124static int selinux_ptrace_access_check(struct task_struct *child,
David Howells5cd9c582008-08-14 11:37:28 +01002125 unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002127 u32 sid = current_sid();
2128 u32 csid = task_sid(child);
Stephen Smalley006ebb42008-05-19 08:32:49 -04002129
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002130 if (mode & PTRACE_MODE_READ)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002131 return avc_has_perm(&selinux_state,
2132 sid, csid, SECCLASS_FILE, FILE__READ, NULL);
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002133
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002134 return avc_has_perm(&selinux_state,
2135 sid, csid, SECCLASS_PROCESS, PROCESS__PTRACE, NULL);
David Howells5cd9c582008-08-14 11:37:28 +01002136}
2137
2138static int selinux_ptrace_traceme(struct task_struct *parent)
2139{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002140 return avc_has_perm(&selinux_state,
2141 task_sid(parent), current_sid(), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002142 PROCESS__PTRACE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143}
2144
2145static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
Eric Paris828dfe12008-04-17 13:17:49 -04002146 kernel_cap_t *inheritable, 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 current_sid(), task_sid(target), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002150 PROCESS__GETCAP, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151}
2152
David Howellsd84f4f92008-11-14 10:39:23 +11002153static int selinux_capset(struct cred *new, const struct cred *old,
2154 const kernel_cap_t *effective,
2155 const kernel_cap_t *inheritable,
2156 const kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002158 return avc_has_perm(&selinux_state,
2159 cred_sid(old), cred_sid(new), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002160 PROCESS__SETCAP, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161}
2162
James Morris5626d3e2009-01-30 10:05:06 +11002163/*
2164 * (This comment used to live with the selinux_task_setuid hook,
2165 * which was removed).
2166 *
2167 * Since setuid only affects the current process, and since the SELinux
2168 * controls are not based on the Linux identity attributes, SELinux does not
2169 * need to control this operation. However, SELinux does control the use of
2170 * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
2171 */
2172
Eric Paris6a9de492012-01-03 12:25:14 -05002173static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
2174 int cap, int audit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175{
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04002176 return cred_has_capability(cred, cap, audit, ns == &init_user_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177}
2178
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
2180{
David Howells88e67f32008-11-14 10:39:21 +11002181 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 int rc = 0;
2183
2184 if (!sb)
2185 return 0;
2186
2187 switch (cmds) {
Eric Paris828dfe12008-04-17 13:17:49 -04002188 case Q_SYNC:
2189 case Q_QUOTAON:
2190 case Q_QUOTAOFF:
2191 case Q_SETINFO:
2192 case Q_SETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11002193 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04002194 break;
2195 case Q_GETFMT:
2196 case Q_GETINFO:
2197 case Q_GETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11002198 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04002199 break;
2200 default:
2201 rc = 0; /* let the kernel handle invalid cmds */
2202 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 }
2204 return rc;
2205}
2206
2207static int selinux_quota_on(struct dentry *dentry)
2208{
David Howells88e67f32008-11-14 10:39:21 +11002209 const struct cred *cred = current_cred();
2210
Eric Paris2875fa02011-04-28 16:04:24 -04002211 return dentry_has_perm(cred, dentry, FILE__QUOTAON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212}
2213
Eric Paris12b30522010-11-15 18:36:29 -05002214static int selinux_syslog(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 switch (type) {
Kees Cookd78ca3c2010-02-03 15:37:13 -08002217 case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */
2218 case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002219 return avc_has_perm(&selinux_state,
2220 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002221 SECCLASS_SYSTEM, SYSTEM__SYSLOG_READ, NULL);
Kees Cookd78ca3c2010-02-03 15:37:13 -08002222 case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */
2223 case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */
2224 /* Set level of messages printed to console */
2225 case SYSLOG_ACTION_CONSOLE_LEVEL:
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002226 return avc_has_perm(&selinux_state,
2227 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002228 SECCLASS_SYSTEM, SYSTEM__SYSLOG_CONSOLE,
2229 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 }
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002231 /* All other syslog types */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002232 return avc_has_perm(&selinux_state,
2233 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002234 SECCLASS_SYSTEM, SYSTEM__SYSLOG_MOD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235}
2236
2237/*
2238 * Check that a process has enough memory to allocate a new virtual
2239 * mapping. 0 means there is enough memory for the allocation to
2240 * succeed and -ENOMEM implies there is not.
2241 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 * Do not audit the selinux permission check, as this is applied to all
2243 * processes that allocate mappings.
2244 */
Alan Cox34b4e4a2007-08-22 14:01:28 -07002245static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246{
2247 int rc, cap_sys_admin = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002249 rc = cred_has_capability(current_cred(), CAP_SYS_ADMIN,
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04002250 SECURITY_CAP_NOAUDIT, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 if (rc == 0)
2252 cap_sys_admin = 1;
2253
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002254 return cap_sys_admin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255}
2256
2257/* binprm security operations */
2258
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002259static u32 ptrace_parent_sid(void)
Paul Moore0c6181c2016-03-30 21:41:21 -04002260{
2261 u32 sid = 0;
2262 struct task_struct *tracer;
2263
2264 rcu_read_lock();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002265 tracer = ptrace_parent(current);
Paul Moore0c6181c2016-03-30 21:41:21 -04002266 if (tracer)
2267 sid = task_sid(tracer);
2268 rcu_read_unlock();
2269
2270 return sid;
2271}
2272
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002273static int check_nnp_nosuid(const struct linux_binprm *bprm,
2274 const struct task_security_struct *old_tsec,
2275 const struct task_security_struct *new_tsec)
2276{
2277 int nnp = (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS);
Andy Lutomirski380cf5b2016-06-23 16:41:05 -05002278 int nosuid = !mnt_may_suid(bprm->file->f_path.mnt);
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002279 int rc;
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002280 u32 av;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002281
2282 if (!nnp && !nosuid)
2283 return 0; /* neither NNP nor nosuid */
2284
2285 if (new_tsec->sid == old_tsec->sid)
2286 return 0; /* No change in credentials */
2287
2288 /*
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002289 * If the policy enables the nnp_nosuid_transition policy capability,
2290 * then we permit transitions under NNP or nosuid if the
2291 * policy allows the corresponding permission between
2292 * the old and new contexts.
2293 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002294 if (selinux_policycap_nnp_nosuid_transition()) {
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002295 av = 0;
2296 if (nnp)
2297 av |= PROCESS2__NNP_TRANSITION;
2298 if (nosuid)
2299 av |= PROCESS2__NOSUID_TRANSITION;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002300 rc = avc_has_perm(&selinux_state,
2301 old_tsec->sid, new_tsec->sid,
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002302 SECCLASS_PROCESS2, av, NULL);
2303 if (!rc)
2304 return 0;
2305 }
2306
2307 /*
2308 * We also permit NNP or nosuid transitions to bounded SIDs,
2309 * i.e. SIDs that are guaranteed to only be allowed a subset
2310 * of the permissions of the current SID.
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002311 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002312 rc = security_bounded_transition(&selinux_state, old_tsec->sid,
2313 new_tsec->sid);
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002314 if (!rc)
2315 return 0;
2316
2317 /*
2318 * On failure, preserve the errno values for NNP vs nosuid.
2319 * NNP: Operation not permitted for caller.
2320 * nosuid: Permission denied to file.
2321 */
2322 if (nnp)
2323 return -EPERM;
2324 return -EACCES;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002325}
2326
David Howellsa6f76f22008-11-14 10:39:24 +11002327static int selinux_bprm_set_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328{
David Howellsa6f76f22008-11-14 10:39:24 +11002329 const struct task_security_struct *old_tsec;
2330 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 struct inode_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04002332 struct common_audit_data ad;
Al Viro496ad9a2013-01-23 17:07:38 -05002333 struct inode *inode = file_inode(bprm->file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 int rc;
2335
David Howellsa6f76f22008-11-14 10:39:24 +11002336 /* SELinux context only depends on initial program or script and not
2337 * the script interpreter */
Kees Cookddb4a142017-07-18 15:25:23 -07002338 if (bprm->called_set_creds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 return 0;
2340
David Howellsa6f76f22008-11-14 10:39:24 +11002341 old_tsec = current_security();
2342 new_tsec = bprm->cred->security;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002343 isec = inode_security(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
2345 /* Default to the current task SID. */
David Howellsa6f76f22008-11-14 10:39:24 +11002346 new_tsec->sid = old_tsec->sid;
2347 new_tsec->osid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
Michael LeMay28eba5b2006-06-27 02:53:42 -07002349 /* Reset fs, key, and sock SIDs on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002350 new_tsec->create_sid = 0;
2351 new_tsec->keycreate_sid = 0;
2352 new_tsec->sockcreate_sid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353
David Howellsa6f76f22008-11-14 10:39:24 +11002354 if (old_tsec->exec_sid) {
2355 new_tsec->sid = old_tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 /* Reset exec SID on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002357 new_tsec->exec_sid = 0;
Andy Lutomirski259e5e62012-04-12 16:47:50 -05002358
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002359 /* Fail on NNP or nosuid if not an allowed transition. */
2360 rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
2361 if (rc)
2362 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 } else {
2364 /* Check for a default transition on this program. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002365 rc = security_transition_sid(&selinux_state, old_tsec->sid,
2366 isec->sid, SECCLASS_PROCESS, NULL,
Eric Paris652bb9b2011-02-01 11:05:40 -05002367 &new_tsec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 if (rc)
2369 return rc;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002370
2371 /*
2372 * Fallback to old SID on NNP or nosuid if not an allowed
2373 * transition.
2374 */
2375 rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
2376 if (rc)
2377 new_tsec->sid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 }
2379
Vivek Goyal43af5de2016-09-09 11:37:49 -04002380 ad.type = LSM_AUDIT_DATA_FILE;
2381 ad.u.file = bprm->file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
David Howellsa6f76f22008-11-14 10:39:24 +11002383 if (new_tsec->sid == old_tsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002384 rc = avc_has_perm(&selinux_state,
2385 old_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
2387 if (rc)
2388 return rc;
2389 } else {
2390 /* Check permissions for the transition. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002391 rc = avc_has_perm(&selinux_state,
2392 old_tsec->sid, new_tsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
2394 if (rc)
2395 return rc;
2396
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002397 rc = avc_has_perm(&selinux_state,
2398 new_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
2400 if (rc)
2401 return rc;
2402
David Howellsa6f76f22008-11-14 10:39:24 +11002403 /* Check for shared state */
2404 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002405 rc = avc_has_perm(&selinux_state,
2406 old_tsec->sid, new_tsec->sid,
David Howellsa6f76f22008-11-14 10:39:24 +11002407 SECCLASS_PROCESS, PROCESS__SHARE,
2408 NULL);
2409 if (rc)
2410 return -EPERM;
2411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412
David Howellsa6f76f22008-11-14 10:39:24 +11002413 /* Make sure that anyone attempting to ptrace over a task that
2414 * changes its SID has the appropriate permit */
Eric W. Biederman9227dd22017-01-23 17:26:31 +13002415 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002416 u32 ptsid = ptrace_parent_sid();
David Howellsa6f76f22008-11-14 10:39:24 +11002417 if (ptsid != 0) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002418 rc = avc_has_perm(&selinux_state,
2419 ptsid, new_tsec->sid,
David Howellsa6f76f22008-11-14 10:39:24 +11002420 SECCLASS_PROCESS,
2421 PROCESS__PTRACE, NULL);
2422 if (rc)
2423 return -EPERM;
2424 }
2425 }
2426
2427 /* Clear any possibly unsafe personality bits on exec: */
2428 bprm->per_clear |= PER_CLEAR_ON_SETID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 /* Enable secure mode for SIDs transitions unless
2431 the noatsecure permission is granted between
2432 the two SIDs, i.e. ahp returns 0. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002433 rc = avc_has_perm(&selinux_state,
2434 old_tsec->sid, new_tsec->sid,
Kees Cook62874c32017-07-18 15:25:25 -07002435 SECCLASS_PROCESS, PROCESS__NOATSECURE,
2436 NULL);
2437 bprm->secureexec |= !!rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 }
2439
Kees Cook62874c32017-07-18 15:25:25 -07002440 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441}
2442
Al Viroc3c073f2012-08-21 22:32:06 -04002443static int match_file(const void *p, struct file *file, unsigned fd)
2444{
2445 return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0;
2446}
2447
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448/* Derived from fs/exec.c:flush_old_files. */
David Howells745ca242008-11-14 10:39:22 +11002449static inline void flush_unauthorized_files(const struct cred *cred,
2450 struct files_struct *files)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 struct file *file, *devnull = NULL;
Stephen Smalleyb20c8122006-09-25 23:32:03 -07002453 struct tty_struct *tty;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002454 int drop_tty = 0;
Al Viroc3c073f2012-08-21 22:32:06 -04002455 unsigned n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002457 tty = get_current_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 if (tty) {
Peter Hurley4a510962016-01-09 21:35:23 -08002459 spin_lock(&tty->files_lock);
Eric Paris37dd0bd2008-10-31 17:40:00 -04002460 if (!list_empty(&tty->tty_files)) {
Nick Piggind996b622010-08-18 04:37:36 +10002461 struct tty_file_private *file_priv;
Eric Paris37dd0bd2008-10-31 17:40:00 -04002462
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 /* Revalidate access to controlling tty.
David Howells13f8e982013-06-13 23:37:55 +01002464 Use file_path_has_perm on the tty path directly
2465 rather than using file_has_perm, as this particular
2466 open file may belong to another process and we are
2467 only interested in the inode-based check here. */
Nick Piggind996b622010-08-18 04:37:36 +10002468 file_priv = list_first_entry(&tty->tty_files,
2469 struct tty_file_private, list);
2470 file = file_priv->file;
David Howells13f8e982013-06-13 23:37:55 +01002471 if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE))
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002472 drop_tty = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 }
Peter Hurley4a510962016-01-09 21:35:23 -08002474 spin_unlock(&tty->files_lock);
Alan Cox452a00d2008-10-13 10:39:13 +01002475 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 }
Eric W. Biederman98a27ba2007-05-08 00:26:56 -07002477 /* Reset controlling tty. */
2478 if (drop_tty)
2479 no_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480
2481 /* Revalidate access to inherited open files. */
Al Viroc3c073f2012-08-21 22:32:06 -04002482 n = iterate_fd(files, 0, match_file, cred);
2483 if (!n) /* none found? */
2484 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
Al Viroc3c073f2012-08-21 22:32:06 -04002486 devnull = dentry_open(&selinux_null, O_RDWR, cred);
Al Viro45525b22012-10-16 13:30:07 -04002487 if (IS_ERR(devnull))
2488 devnull = NULL;
2489 /* replace all the matching ones with this */
2490 do {
2491 replace_fd(n - 1, devnull, 0);
2492 } while ((n = iterate_fd(files, n, match_file, cred)) != 0);
2493 if (devnull)
Al Viroc3c073f2012-08-21 22:32:06 -04002494 fput(devnull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495}
2496
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497/*
David Howellsa6f76f22008-11-14 10:39:24 +11002498 * Prepare a process for imminent new credential changes due to exec
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 */
David Howellsa6f76f22008-11-14 10:39:24 +11002500static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501{
David Howellsa6f76f22008-11-14 10:39:24 +11002502 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 struct rlimit *rlim, *initrlim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 int rc, i;
2505
David Howellsa6f76f22008-11-14 10:39:24 +11002506 new_tsec = bprm->cred->security;
2507 if (new_tsec->sid == new_tsec->osid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 return;
2509
2510 /* Close files for which the new task SID is not authorized. */
David Howellsa6f76f22008-11-14 10:39:24 +11002511 flush_unauthorized_files(bprm->cred, current->files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512
David Howellsa6f76f22008-11-14 10:39:24 +11002513 /* Always clear parent death signal on SID transitions. */
2514 current->pdeath_signal = 0;
2515
2516 /* Check whether the new SID can inherit resource limits from the old
2517 * SID. If not, reset all soft limits to the lower of the current
2518 * task's hard limit and the init task's soft limit.
2519 *
2520 * Note that the setting of hard limits (even to lower them) can be
2521 * controlled by the setrlimit check. The inclusion of the init task's
2522 * soft limit into the computation is to avoid resetting soft limits
2523 * higher than the default soft limit for cases where the default is
2524 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2525 */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002526 rc = avc_has_perm(&selinux_state,
2527 new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
David Howellsa6f76f22008-11-14 10:39:24 +11002528 PROCESS__RLIMITINH, NULL);
2529 if (rc) {
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002530 /* protect against do_prlimit() */
2531 task_lock(current);
David Howellsa6f76f22008-11-14 10:39:24 +11002532 for (i = 0; i < RLIM_NLIMITS; i++) {
2533 rlim = current->signal->rlim + i;
2534 initrlim = init_task.signal->rlim + i;
2535 rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
2536 }
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002537 task_unlock(current);
Nicolas Pitrebaa73d92016-11-11 00:10:10 -05002538 if (IS_ENABLED(CONFIG_POSIX_TIMERS))
2539 update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
David Howellsa6f76f22008-11-14 10:39:24 +11002540 }
2541}
2542
2543/*
2544 * Clean up the process immediately after the installation of new credentials
2545 * due to exec
2546 */
2547static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
2548{
2549 const struct task_security_struct *tsec = current_security();
2550 struct itimerval itimer;
David Howellsa6f76f22008-11-14 10:39:24 +11002551 u32 osid, sid;
2552 int rc, i;
David Howellsa6f76f22008-11-14 10:39:24 +11002553
David Howellsa6f76f22008-11-14 10:39:24 +11002554 osid = tsec->osid;
2555 sid = tsec->sid;
2556
2557 if (sid == osid)
2558 return;
2559
2560 /* Check whether the new SID can inherit signal state from the old SID.
2561 * If not, clear itimers to avoid subsequent signal generation and
2562 * flush and unblock signals.
2563 *
2564 * This must occur _after_ the task SID has been updated so that any
2565 * kill done after the flush will be checked against the new SID.
2566 */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002567 rc = avc_has_perm(&selinux_state,
2568 osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 if (rc) {
Nicolas Pitrebaa73d92016-11-11 00:10:10 -05002570 if (IS_ENABLED(CONFIG_POSIX_TIMERS)) {
2571 memset(&itimer, 0, sizeof itimer);
2572 for (i = 0; i < 3; i++)
2573 do_setitimer(i, &itimer, NULL);
2574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterov9e7c8f82015-06-04 16:22:16 -04002576 if (!fatal_signal_pending(current)) {
2577 flush_sigqueue(&current->pending);
2578 flush_sigqueue(&current->signal->shared_pending);
David Howells3bcac022009-04-29 13:45:05 +01002579 flush_signal_handlers(current, 1);
2580 sigemptyset(&current->blocked);
Oleg Nesterov9e7c8f82015-06-04 16:22:16 -04002581 recalc_sigpending();
David Howells3bcac022009-04-29 13:45:05 +01002582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 spin_unlock_irq(&current->sighand->siglock);
2584 }
2585
David Howellsa6f76f22008-11-14 10:39:24 +11002586 /* Wake up the parent if it is waiting so that it can recheck
2587 * wait permission to the new task SID. */
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002588 read_lock(&tasklist_lock);
Oleg Nesterov0b7570e2009-09-23 15:56:46 -07002589 __wake_up_parent(current, current->real_parent);
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002590 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591}
2592
2593/* superblock security operations */
2594
2595static int selinux_sb_alloc_security(struct super_block *sb)
2596{
2597 return superblock_alloc_security(sb);
2598}
2599
2600static void selinux_sb_free_security(struct super_block *sb)
2601{
2602 superblock_free_security(sb);
2603}
2604
Al Viro99dbbb52018-12-14 21:56:23 -05002605static inline int opt_len(const char *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606{
Al Viro99dbbb52018-12-14 21:56:23 -05002607 bool open_quote = false;
2608 int len;
2609 char c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610
Al Viro99dbbb52018-12-14 21:56:23 -05002611 for (len = 0; (c = s[len]) != '\0'; len++) {
2612 if (c == '"')
Cory Olmo3528a952006-09-29 01:58:44 -07002613 open_quote = !open_quote;
Al Viro99dbbb52018-12-14 21:56:23 -05002614 if (c == ',' && !open_quote)
2615 break;
2616 }
2617 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618}
2619
Al Viro204cc0c2018-12-13 13:41:47 -05002620static int selinux_sb_eat_lsm_opts(char *options, void **mnt_opts)
Eric Paris026eb162011-03-03 16:09:14 -05002621{
Al Viro99dbbb52018-12-14 21:56:23 -05002622 char *from = options;
2623 char *to = options;
2624 bool first = true;
Al Viro5b400232018-12-12 20:13:29 -05002625
Al Viro99dbbb52018-12-14 21:56:23 -05002626 while (1) {
2627 int len = opt_len(from);
2628 int token, rc;
2629 char *arg = NULL;
2630
2631 token = match_opt_prefix(from, len, &arg);
2632
2633 if (token != Opt_error) {
2634 char *p, *q;
2635
2636 /* strip quotes */
2637 if (arg) {
2638 for (p = q = arg; p < from + len; p++) {
2639 char c = *p;
2640 if (c != '"')
2641 *q++ = c;
2642 }
2643 arg = kmemdup_nul(arg, q - arg, GFP_KERNEL);
2644 }
2645 rc = selinux_add_opt(token, arg, mnt_opts);
2646 if (unlikely(rc)) {
2647 kfree(arg);
2648 if (*mnt_opts) {
2649 selinux_free_mnt_opts(*mnt_opts);
2650 *mnt_opts = NULL;
2651 }
2652 return rc;
2653 }
2654 } else {
2655 if (!first) { // copy with preceding comma
2656 from--;
2657 len++;
2658 }
2659 if (to != from)
2660 memmove(to, from, len);
2661 to += len;
2662 first = false;
2663 }
2664 if (!from[len])
2665 break;
2666 from += len + 1;
2667 }
2668 *to = '\0';
2669 return 0;
Al Viro5b400232018-12-12 20:13:29 -05002670}
2671
Al Viro204cc0c2018-12-13 13:41:47 -05002672static int selinux_sb_remount(struct super_block *sb, void *mnt_opts)
Eric Paris026eb162011-03-03 16:09:14 -05002673{
Al Virobd323652018-12-13 15:04:59 -05002674 struct selinux_mnt_opts *opts = mnt_opts;
Eric Paris026eb162011-03-03 16:09:14 -05002675 struct superblock_security_struct *sbsec = sb->s_security;
Al Virobd323652018-12-13 15:04:59 -05002676 u32 sid;
2677 int rc;
Eric Paris026eb162011-03-03 16:09:14 -05002678
2679 if (!(sbsec->flags & SE_SBINITIALIZED))
2680 return 0;
2681
Al Viro204cc0c2018-12-13 13:41:47 -05002682 if (!opts)
Eric Paris026eb162011-03-03 16:09:14 -05002683 return 0;
2684
Al Virobd323652018-12-13 15:04:59 -05002685 if (opts->fscontext) {
2686 rc = parse_sid(sb, opts->fscontext, &sid);
2687 if (rc)
Al Viroc039bc32018-12-01 23:06:57 -05002688 return rc;
Al Virobd323652018-12-13 15:04:59 -05002689 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
2690 goto out_bad_option;
Eric Paris026eb162011-03-03 16:09:14 -05002691 }
Al Virobd323652018-12-13 15:04:59 -05002692 if (opts->context) {
2693 rc = parse_sid(sb, opts->context, &sid);
2694 if (rc)
2695 return rc;
2696 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
2697 goto out_bad_option;
2698 }
2699 if (opts->rootcontext) {
2700 struct inode_security_struct *root_isec;
2701 root_isec = backing_inode_security(sb->s_root);
2702 rc = parse_sid(sb, opts->rootcontext, &sid);
2703 if (rc)
2704 return rc;
2705 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
2706 goto out_bad_option;
2707 }
2708 if (opts->defcontext) {
2709 rc = parse_sid(sb, opts->defcontext, &sid);
2710 if (rc)
2711 return rc;
2712 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
2713 goto out_bad_option;
Eric Paris026eb162011-03-03 16:09:14 -05002714 }
Al Viroc039bc32018-12-01 23:06:57 -05002715 return 0;
Eric Paris026eb162011-03-03 16:09:14 -05002716
Eric Paris026eb162011-03-03 16:09:14 -05002717out_bad_option:
peter enderborgc103a912018-06-12 10:09:03 +02002718 pr_warn("SELinux: unable to change security options "
Linus Torvalds29b1deb2013-12-15 11:17:45 -08002719 "during remount (dev %s, type=%s)\n", sb->s_id,
2720 sb->s_type->name);
Al Viroc039bc32018-12-01 23:06:57 -05002721 return -EINVAL;
Eric Paris026eb162011-03-03 16:09:14 -05002722}
2723
Al Viroa10d7c22018-12-05 11:58:35 -05002724static int selinux_sb_kern_mount(struct super_block *sb)
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;
James Morris74192242008-12-19 11:41:10 +11002728
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 = sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002731 return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732}
2733
David Howells726c3342006-06-23 02:02:58 -07002734static int selinux_sb_statfs(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735{
David Howells88e67f32008-11-14 10:39:21 +11002736 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002737 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738
Eric Paris50c205f2012-04-04 15:01:43 -04002739 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002740 ad.u.dentry = dentry->d_sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002741 return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742}
2743
Al Viro808d4e32012-10-11 11:42:01 -04002744static int selinux_mount(const char *dev_name,
Al Viro8a04c432016-03-25 14:52:53 -04002745 const struct path *path,
Al Viro808d4e32012-10-11 11:42:01 -04002746 const char *type,
Eric Paris828dfe12008-04-17 13:17:49 -04002747 unsigned long flags,
2748 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749{
David Howells88e67f32008-11-14 10:39:21 +11002750 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751
2752 if (flags & MS_REMOUNT)
Al Virod8c95842011-12-07 18:16:57 -05002753 return superblock_has_perm(cred, path->dentry->d_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002754 FILESYSTEM__REMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 else
Eric Paris2875fa02011-04-28 16:04:24 -04002756 return path_has_perm(cred, path, FILE__MOUNTON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757}
2758
2759static int selinux_umount(struct vfsmount *mnt, int flags)
2760{
David Howells88e67f32008-11-14 10:39:21 +11002761 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762
David Howells88e67f32008-11-14 10:39:21 +11002763 return superblock_has_perm(cred, mnt->mnt_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002764 FILESYSTEM__UNMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765}
2766
David Howells442155c2018-11-01 23:07:24 +00002767static const struct fs_parameter_spec selinux_param_specs[] = {
2768 fsparam_string(CONTEXT_STR, Opt_context),
2769 fsparam_string(DEFCONTEXT_STR, Opt_defcontext),
2770 fsparam_string(FSCONTEXT_STR, Opt_fscontext),
2771 fsparam_string(ROOTCONTEXT_STR, Opt_rootcontext),
2772 fsparam_flag (SECLABEL_STR, Opt_seclabel),
2773 {}
2774};
2775
2776static const struct fs_parameter_description selinux_fs_parameters = {
2777 .name = "SELinux",
2778 .specs = selinux_param_specs,
2779};
2780
2781static int selinux_fs_context_parse_param(struct fs_context *fc,
2782 struct fs_parameter *param)
2783{
2784 struct fs_parse_result result;
2785 int opt, rc;
2786
2787 opt = fs_parse(fc, &selinux_fs_parameters, param, &result);
2788 if (opt < 0)
2789 return opt;
2790
2791 rc = selinux_add_opt(opt, param->string, &fc->security);
2792 if (!rc) {
2793 param->string = NULL;
2794 rc = 1;
2795 }
2796 return rc;
2797}
2798
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799/* inode security operations */
2800
2801static int selinux_inode_alloc_security(struct inode *inode)
2802{
2803 return inode_alloc_security(inode);
2804}
2805
2806static void selinux_inode_free_security(struct inode *inode)
2807{
2808 inode_free_security(inode);
2809}
2810
David Quigleyd47be3d2013-05-22 12:50:34 -04002811static int selinux_dentry_init_security(struct dentry *dentry, int mode,
Al Viro4f3ccd72016-07-20 16:06:15 -04002812 const struct qstr *name, void **ctx,
David Quigleyd47be3d2013-05-22 12:50:34 -04002813 u32 *ctxlen)
2814{
David Quigleyd47be3d2013-05-22 12:50:34 -04002815 u32 newsid;
2816 int rc;
2817
Vivek Goyalc957f6d2016-07-13 10:44:51 -04002818 rc = selinux_determine_inode_label(current_security(),
2819 d_inode(dentry->d_parent), name,
David Howellsc3c188b2015-07-10 17:19:58 -04002820 inode_mode_to_security_class(mode),
2821 &newsid);
2822 if (rc)
2823 return rc;
David Quigleyd47be3d2013-05-22 12:50:34 -04002824
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002825 return security_sid_to_context(&selinux_state, newsid, (char **)ctx,
2826 ctxlen);
David Quigleyd47be3d2013-05-22 12:50:34 -04002827}
2828
Vivek Goyala518b0a2016-07-13 10:44:53 -04002829static int selinux_dentry_create_files_as(struct dentry *dentry, int mode,
2830 struct qstr *name,
2831 const struct cred *old,
2832 struct cred *new)
2833{
2834 u32 newsid;
2835 int rc;
2836 struct task_security_struct *tsec;
2837
2838 rc = selinux_determine_inode_label(old->security,
2839 d_inode(dentry->d_parent), name,
2840 inode_mode_to_security_class(mode),
2841 &newsid);
2842 if (rc)
2843 return rc;
2844
2845 tsec = new->security;
2846 tsec->create_sid = newsid;
2847 return 0;
2848}
2849
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002850static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +09002851 const struct qstr *qstr,
2852 const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -05002853 void **value, size_t *len)
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002854{
Paul Moore5fb49872010-04-22 14:46:19 -04002855 const struct task_security_struct *tsec = current_security();
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002856 struct superblock_security_struct *sbsec;
Corentin LABBEc0d4f462017-10-04 20:32:17 +02002857 u32 newsid, clen;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002858 int rc;
Tetsuo Handa95489062013-07-25 05:44:02 +09002859 char *context;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002860
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002861 sbsec = dir->i_sb->s_security;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002862
David Howells275bb412008-11-14 10:39:19 +11002863 newsid = tsec->create_sid;
2864
Vivek Goyalc957f6d2016-07-13 10:44:51 -04002865 rc = selinux_determine_inode_label(current_security(),
David Howellsc3c188b2015-07-10 17:19:58 -04002866 dir, qstr,
2867 inode_mode_to_security_class(inode->i_mode),
2868 &newsid);
2869 if (rc)
2870 return rc;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002871
Eric Paris296fddf2006-09-25 23:32:00 -07002872 /* Possibly defer initialization to selinux_complete_init. */
David P. Quigley0d90a7e2009-01-16 09:22:02 -05002873 if (sbsec->flags & SE_SBINITIALIZED) {
Eric Paris296fddf2006-09-25 23:32:00 -07002874 struct inode_security_struct *isec = inode->i_security;
2875 isec->sclass = inode_mode_to_security_class(inode->i_mode);
2876 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05002877 isec->initialized = LABEL_INITIALIZED;
Eric Paris296fddf2006-09-25 23:32:00 -07002878 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002879
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002880 if (!selinux_state.initialized || !(sbsec->flags & SBLABEL_MNT))
Stephen Smalley25a74f32005-11-08 21:34:33 -08002881 return -EOPNOTSUPP;
2882
Tetsuo Handa95489062013-07-25 05:44:02 +09002883 if (name)
2884 *name = XATTR_SELINUX_SUFFIX;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002885
2886 if (value && len) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002887 rc = security_sid_to_context_force(&selinux_state, newsid,
2888 &context, &clen);
Tetsuo Handa95489062013-07-25 05:44:02 +09002889 if (rc)
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002890 return rc;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002891 *value = context;
2892 *len = clen;
2893 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002894
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002895 return 0;
2896}
2897
Al Viro4acdaf22011-07-26 01:42:34 -04002898static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899{
2900 return may_create(dir, dentry, SECCLASS_FILE);
2901}
2902
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2904{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 return may_link(dir, old_dentry, MAY_LINK);
2906}
2907
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
2909{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 return may_link(dir, dentry, MAY_UNLINK);
2911}
2912
2913static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
2914{
2915 return may_create(dir, dentry, SECCLASS_LNK_FILE);
2916}
2917
Al Viro18bb1db2011-07-26 01:41:39 -04002918static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919{
2920 return may_create(dir, dentry, SECCLASS_DIR);
2921}
2922
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
2924{
2925 return may_link(dir, dentry, MAY_RMDIR);
2926}
2927
Al Viro1a67aaf2011-07-26 01:52:52 -04002928static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 return may_create(dir, dentry, inode_mode_to_security_class(mode));
2931}
2932
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
Eric Paris828dfe12008-04-17 13:17:49 -04002934 struct inode *new_inode, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935{
2936 return may_rename(old_inode, old_dentry, new_inode, new_dentry);
2937}
2938
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939static int selinux_inode_readlink(struct dentry *dentry)
2940{
David Howells88e67f32008-11-14 10:39:21 +11002941 const struct cred *cred = current_cred();
2942
Eric Paris2875fa02011-04-28 16:04:24 -04002943 return dentry_has_perm(cred, dentry, FILE__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944}
2945
NeilBrownbda0be72015-03-23 13:37:39 +11002946static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode,
2947 bool rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948{
David Howells88e67f32008-11-14 10:39:21 +11002949 const struct cred *cred = current_cred();
NeilBrownbda0be72015-03-23 13:37:39 +11002950 struct common_audit_data ad;
2951 struct inode_security_struct *isec;
2952 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953
NeilBrownbda0be72015-03-23 13:37:39 +11002954 validate_creds(cred);
2955
2956 ad.type = LSM_AUDIT_DATA_DENTRY;
2957 ad.u.dentry = dentry;
2958 sid = cred_sid(cred);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05002959 isec = inode_security_rcu(inode, rcu);
2960 if (IS_ERR(isec))
2961 return PTR_ERR(isec);
NeilBrownbda0be72015-03-23 13:37:39 +11002962
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002963 return avc_has_perm_flags(&selinux_state,
2964 sid, isec->sid, isec->sclass, FILE__READ, &ad,
NeilBrownbda0be72015-03-23 13:37:39 +11002965 rcu ? MAY_NOT_BLOCK : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966}
2967
Eric Parisd4cf970d2012-04-04 15:01:42 -04002968static noinline int audit_inode_permission(struct inode *inode,
2969 u32 perms, u32 audited, u32 denied,
Stephen Smalley626b9742014-04-29 11:29:04 -07002970 int result,
Eric Parisd4cf970d2012-04-04 15:01:42 -04002971 unsigned flags)
2972{
2973 struct common_audit_data ad;
Eric Parisd4cf970d2012-04-04 15:01:42 -04002974 struct inode_security_struct *isec = inode->i_security;
2975 int rc;
2976
Eric Paris50c205f2012-04-04 15:01:43 -04002977 ad.type = LSM_AUDIT_DATA_INODE;
Eric Parisd4cf970d2012-04-04 15:01:42 -04002978 ad.u.inode = inode;
2979
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002980 rc = slow_avc_audit(&selinux_state,
2981 current_sid(), isec->sid, isec->sclass, perms,
Stephen Smalley626b9742014-04-29 11:29:04 -07002982 audited, denied, result, &ad, flags);
Eric Parisd4cf970d2012-04-04 15:01:42 -04002983 if (rc)
2984 return rc;
2985 return 0;
2986}
2987
Al Viroe74f71e2011-06-20 19:38:15 -04002988static int selinux_inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989{
David Howells88e67f32008-11-14 10:39:21 +11002990 const struct cred *cred = current_cred();
Eric Parisb782e0a2010-07-23 11:44:03 -04002991 u32 perms;
2992 bool from_access;
Al Virocf1dd1d2011-06-20 19:44:08 -04002993 unsigned flags = mask & MAY_NOT_BLOCK;
Eric Paris2e334052012-04-04 15:01:42 -04002994 struct inode_security_struct *isec;
2995 u32 sid;
2996 struct av_decision avd;
2997 int rc, rc2;
2998 u32 audited, denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999
Eric Parisb782e0a2010-07-23 11:44:03 -04003000 from_access = mask & MAY_ACCESS;
Eric Parisd09ca732010-07-23 11:43:57 -04003001 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
3002
Eric Parisb782e0a2010-07-23 11:44:03 -04003003 /* No permission to check. Existence test. */
3004 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006
Eric Paris2e334052012-04-04 15:01:42 -04003007 validate_creds(cred);
Eric Parisb782e0a2010-07-23 11:44:03 -04003008
Eric Paris2e334052012-04-04 15:01:42 -04003009 if (unlikely(IS_PRIVATE(inode)))
3010 return 0;
Eric Parisb782e0a2010-07-23 11:44:03 -04003011
3012 perms = file_mask_to_av(inode->i_mode, mask);
3013
Eric Paris2e334052012-04-04 15:01:42 -04003014 sid = cred_sid(cred);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05003015 isec = inode_security_rcu(inode, flags & MAY_NOT_BLOCK);
3016 if (IS_ERR(isec))
3017 return PTR_ERR(isec);
Eric Paris2e334052012-04-04 15:01:42 -04003018
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003019 rc = avc_has_perm_noaudit(&selinux_state,
3020 sid, isec->sid, isec->sclass, perms, 0, &avd);
Eric Paris2e334052012-04-04 15:01:42 -04003021 audited = avc_audit_required(perms, &avd, rc,
3022 from_access ? FILE__AUDIT_ACCESS : 0,
3023 &denied);
3024 if (likely(!audited))
3025 return rc;
3026
Stephen Smalley626b9742014-04-29 11:29:04 -07003027 rc2 = audit_inode_permission(inode, perms, audited, denied, rc, flags);
Eric Paris2e334052012-04-04 15:01:42 -04003028 if (rc2)
3029 return rc2;
3030 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031}
3032
3033static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
3034{
David Howells88e67f32008-11-14 10:39:21 +11003035 const struct cred *cred = current_cred();
Stephen Smalleyccb54472017-05-12 12:41:24 -04003036 struct inode *inode = d_backing_inode(dentry);
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003037 unsigned int ia_valid = iattr->ia_valid;
Eric Paris95dbf732012-04-04 13:45:34 -04003038 __u32 av = FILE__WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003040 /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
3041 if (ia_valid & ATTR_FORCE) {
3042 ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
3043 ATTR_FORCE);
3044 if (!ia_valid)
3045 return 0;
3046 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003048 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
3049 ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
Eric Paris2875fa02011-04-28 16:04:24 -04003050 return dentry_has_perm(cred, dentry, FILE__SETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003052 if (selinux_policycap_openperm() &&
Stephen Smalleyccb54472017-05-12 12:41:24 -04003053 inode->i_sb->s_magic != SOCKFS_MAGIC &&
3054 (ia_valid & ATTR_SIZE) &&
3055 !(ia_valid & ATTR_FILE))
Eric Paris95dbf732012-04-04 13:45:34 -04003056 av |= FILE__OPEN;
3057
3058 return dentry_has_perm(cred, dentry, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059}
3060
Al Viro3f7036a2015-03-08 19:28:30 -04003061static int selinux_inode_getattr(const struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062{
Al Viro3f7036a2015-03-08 19:28:30 -04003063 return path_has_perm(current_cred(), path, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064}
3065
Stephen Smalleydb590002017-04-20 11:31:30 -04003066static bool has_cap_mac_admin(bool audit)
3067{
3068 const struct cred *cred = current_cred();
3069 int cap_audit = audit ? SECURITY_CAP_AUDIT : SECURITY_CAP_NOAUDIT;
3070
3071 if (cap_capable(cred, &init_user_ns, CAP_MAC_ADMIN, cap_audit))
3072 return false;
3073 if (cred_has_capability(cred, CAP_MAC_ADMIN, cap_audit, true))
3074 return false;
3075 return true;
3076}
3077
David Howells8f0cfa52008-04-29 00:59:41 -07003078static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
3079 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080{
David Howellsc6f493d2015-03-17 22:26:22 +00003081 struct inode *inode = d_backing_inode(dentry);
Paul Moore20cdef82016-04-04 14:14:42 -04003082 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 struct superblock_security_struct *sbsec;
Thomas Liu2bf49692009-07-14 12:14:09 -04003084 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11003085 u32 newsid, sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 int rc = 0;
3087
Eric W. Biederman6b240302017-10-02 09:38:20 -05003088 if (strcmp(name, XATTR_NAME_SELINUX)) {
3089 rc = cap_inode_setxattr(dentry, name, value, size, flags);
3090 if (rc)
3091 return rc;
3092
3093 /* Not an attribute we recognize, so just check the
3094 ordinary setattr permission. */
3095 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097
3098 sbsec = inode->i_sb->s_security;
Eric Paris12f348b2012-10-09 10:56:25 -04003099 if (!(sbsec->flags & SBLABEL_MNT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 return -EOPNOTSUPP;
3101
Serge E. Hallyn2e149672011-03-23 16:43:26 -07003102 if (!inode_owner_or_capable(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 return -EPERM;
3104
Eric Paris50c205f2012-04-04 15:01:43 -04003105 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04003106 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107
Paul Moore20cdef82016-04-04 14:14:42 -04003108 isec = backing_inode_security(dentry);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003109 rc = avc_has_perm(&selinux_state,
3110 sid, isec->sid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 FILE__RELABELFROM, &ad);
3112 if (rc)
3113 return rc;
3114
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003115 rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3116 GFP_KERNEL);
Stephen Smalley12b29f32008-05-07 13:03:20 -04003117 if (rc == -EINVAL) {
Stephen Smalleydb590002017-04-20 11:31:30 -04003118 if (!has_cap_mac_admin(true)) {
Eric Parisd6ea83e2012-04-04 13:45:49 -04003119 struct audit_buffer *ab;
3120 size_t audit_size;
Eric Parisd6ea83e2012-04-04 13:45:49 -04003121
3122 /* We strip a nul only if it is at the end, otherwise the
3123 * context contains a nul and we should audit that */
Al Viroe3fea3f2012-06-09 08:15:16 +01003124 if (value) {
Colin Ian Kingadd24372017-10-14 13:46:55 +01003125 const char *str = value;
3126
Al Viroe3fea3f2012-06-09 08:15:16 +01003127 if (str[size - 1] == '\0')
3128 audit_size = size - 1;
3129 else
3130 audit_size = size;
3131 } else {
Al Viroe3fea3f2012-06-09 08:15:16 +01003132 audit_size = 0;
3133 }
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04003134 ab = audit_log_start(audit_context(),
3135 GFP_ATOMIC, AUDIT_SELINUX_ERR);
Eric Parisd6ea83e2012-04-04 13:45:49 -04003136 audit_log_format(ab, "op=setxattr invalid_context=");
3137 audit_log_n_untrustedstring(ab, value, audit_size);
3138 audit_log_end(ab);
3139
Stephen Smalley12b29f32008-05-07 13:03:20 -04003140 return rc;
Eric Parisd6ea83e2012-04-04 13:45:49 -04003141 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003142 rc = security_context_to_sid_force(&selinux_state, value,
3143 size, &newsid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04003144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 if (rc)
3146 return rc;
3147
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003148 rc = avc_has_perm(&selinux_state,
3149 sid, newsid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 FILE__RELABELTO, &ad);
3151 if (rc)
3152 return rc;
3153
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003154 rc = security_validate_transition(&selinux_state, isec->sid, newsid,
3155 sid, isec->sclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 if (rc)
3157 return rc;
3158
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003159 return avc_has_perm(&selinux_state,
3160 newsid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 sbsec->sid,
3162 SECCLASS_FILESYSTEM,
3163 FILESYSTEM__ASSOCIATE,
3164 &ad);
3165}
3166
David Howells8f0cfa52008-04-29 00:59:41 -07003167static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
Eric Parisf5269712008-05-14 11:27:45 -04003168 const void *value, size_t size,
David Howells8f0cfa52008-04-29 00:59:41 -07003169 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170{
David Howellsc6f493d2015-03-17 22:26:22 +00003171 struct inode *inode = d_backing_inode(dentry);
Paul Moore20cdef82016-04-04 14:14:42 -04003172 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 u32 newsid;
3174 int rc;
3175
3176 if (strcmp(name, XATTR_NAME_SELINUX)) {
3177 /* Not an attribute we recognize, so nothing to do. */
3178 return;
3179 }
3180
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003181 rc = security_context_to_sid_force(&selinux_state, value, size,
3182 &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 if (rc) {
peter enderborgc103a912018-06-12 10:09:03 +02003184 pr_err("SELinux: unable to map context to SID"
Stephen Smalley12b29f32008-05-07 13:03:20 -04003185 "for (%s, %lu), rc=%d\n",
3186 inode->i_sb->s_id, inode->i_ino, -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 return;
3188 }
3189
Paul Moore20cdef82016-04-04 14:14:42 -04003190 isec = backing_inode_security(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003191 spin_lock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003192 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05003194 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003195 spin_unlock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003196
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 return;
3198}
3199
David Howells8f0cfa52008-04-29 00:59:41 -07003200static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201{
David Howells88e67f32008-11-14 10:39:21 +11003202 const struct cred *cred = current_cred();
3203
Eric Paris2875fa02011-04-28 16:04:24 -04003204 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205}
3206
Eric Paris828dfe12008-04-17 13:17:49 -04003207static int selinux_inode_listxattr(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208{
David Howells88e67f32008-11-14 10:39:21 +11003209 const struct cred *cred = current_cred();
3210
Eric Paris2875fa02011-04-28 16:04:24 -04003211 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212}
3213
David Howells8f0cfa52008-04-29 00:59:41 -07003214static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215{
Eric W. Biederman6b240302017-10-02 09:38:20 -05003216 if (strcmp(name, XATTR_NAME_SELINUX)) {
3217 int rc = cap_inode_removexattr(dentry, name);
3218 if (rc)
3219 return rc;
3220
3221 /* Not an attribute we recognize, so just check the
3222 ordinary setattr permission. */
3223 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225
3226 /* No one is allowed to remove a SELinux security label.
3227 You can change the label, but all data must be labeled. */
3228 return -EACCES;
3229}
3230
James Morrisd381d8a2005-10-30 14:59:22 -08003231/*
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003232 * Copy the inode security context value to the user.
James Morrisd381d8a2005-10-30 14:59:22 -08003233 *
3234 * Permission check is handled by selinux_inode_getxattr hook.
3235 */
Andreas Gruenbacherea861df2015-12-24 11:09:39 -05003236static int selinux_inode_getsecurity(struct inode *inode, const char *name, void **buffer, bool alloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237{
David P. Quigley42492592008-02-04 22:29:39 -08003238 u32 size;
3239 int error;
3240 char *context = NULL;
Paul Moore20cdef82016-04-04 14:14:42 -04003241 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00003243 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3244 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003246 /*
3247 * If the caller has CAP_MAC_ADMIN, then get the raw context
3248 * value even if it is not defined by current policy; otherwise,
3249 * use the in-core value under current policy.
3250 * Use the non-auditing forms of the permission checks since
3251 * getxattr may be called by unprivileged processes commonly
3252 * and lack of permission just means that we fall back to the
3253 * in-core context value, not a denial.
3254 */
Paul Moore20cdef82016-04-04 14:14:42 -04003255 isec = inode_security(inode);
Stephen Smalleydb590002017-04-20 11:31:30 -04003256 if (has_cap_mac_admin(false))
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003257 error = security_sid_to_context_force(&selinux_state,
3258 isec->sid, &context,
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003259 &size);
3260 else
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003261 error = security_sid_to_context(&selinux_state, isec->sid,
3262 &context, &size);
David P. Quigley42492592008-02-04 22:29:39 -08003263 if (error)
3264 return error;
3265 error = size;
3266 if (alloc) {
3267 *buffer = context;
3268 goto out_nofree;
3269 }
3270 kfree(context);
3271out_nofree:
3272 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273}
3274
3275static int selinux_inode_setsecurity(struct inode *inode, const char *name,
Eric Paris828dfe12008-04-17 13:17:49 -04003276 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277{
Paul Moore2c971652016-04-19 16:36:28 -04003278 struct inode_security_struct *isec = inode_security_novalidate(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 u32 newsid;
3280 int rc;
3281
3282 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3283 return -EOPNOTSUPP;
3284
3285 if (!value || !size)
3286 return -EACCES;
3287
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003288 rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3289 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 if (rc)
3291 return rc;
3292
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003293 spin_lock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003294 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05003296 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003297 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 return 0;
3299}
3300
3301static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
3302{
3303 const int len = sizeof(XATTR_NAME_SELINUX);
3304 if (buffer && len <= buffer_size)
3305 memcpy(buffer, XATTR_NAME_SELINUX, len);
3306 return len;
3307}
3308
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05003309static void selinux_inode_getsecid(struct inode *inode, u32 *secid)
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02003310{
Andreas Gruenbachere817c2f2016-02-18 12:04:08 +01003311 struct inode_security_struct *isec = inode_security_novalidate(inode);
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02003312 *secid = isec->sid;
3313}
3314
Vivek Goyal56909eb2016-07-13 10:44:48 -04003315static int selinux_inode_copy_up(struct dentry *src, struct cred **new)
3316{
3317 u32 sid;
3318 struct task_security_struct *tsec;
3319 struct cred *new_creds = *new;
3320
3321 if (new_creds == NULL) {
3322 new_creds = prepare_creds();
3323 if (!new_creds)
3324 return -ENOMEM;
3325 }
3326
3327 tsec = new_creds->security;
3328 /* Get label from overlay inode and set it in create_sid */
3329 selinux_inode_getsecid(d_inode(src), &sid);
3330 tsec->create_sid = sid;
3331 *new = new_creds;
3332 return 0;
3333}
3334
Vivek Goyal19472b62016-07-13 10:44:50 -04003335static int selinux_inode_copy_up_xattr(const char *name)
3336{
3337 /* The copy_up hook above sets the initial context on an inode, but we
3338 * don't then want to overwrite it by blindly copying all the lower
3339 * xattrs up. Instead, we have to filter out SELinux-related xattrs.
3340 */
3341 if (strcmp(name, XATTR_NAME_SELINUX) == 0)
3342 return 1; /* Discard */
3343 /*
3344 * Any other attribute apart from SELINUX is not claimed, supported
3345 * by selinux.
3346 */
3347 return -EOPNOTSUPP;
3348}
3349
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350/* file security operations */
3351
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003352static int selinux_revalidate_file_permission(struct file *file, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353{
David Howells88e67f32008-11-14 10:39:21 +11003354 const struct cred *cred = current_cred();
Al Viro496ad9a2013-01-23 17:07:38 -05003355 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
3358 if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
3359 mask |= MAY_APPEND;
3360
Paul Moore389fb8002009-03-27 17:10:34 -04003361 return file_has_perm(cred, file,
3362 file_mask_to_av(inode->i_mode, mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363}
3364
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003365static int selinux_file_permission(struct file *file, int mask)
3366{
Al Viro496ad9a2013-01-23 17:07:38 -05003367 struct inode *inode = file_inode(file);
Stephen Smalley20dda182009-06-22 14:54:53 -04003368 struct file_security_struct *fsec = file->f_security;
Andreas Gruenbacherb1973672016-01-05 23:12:33 +01003369 struct inode_security_struct *isec;
Stephen Smalley20dda182009-06-22 14:54:53 -04003370 u32 sid = current_sid();
3371
Paul Moore389fb8002009-03-27 17:10:34 -04003372 if (!mask)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003373 /* No permission to check. Existence test. */
3374 return 0;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003375
Andreas Gruenbacherb1973672016-01-05 23:12:33 +01003376 isec = inode_security(inode);
Stephen Smalley20dda182009-06-22 14:54:53 -04003377 if (sid == fsec->sid && fsec->isid == isec->sid &&
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003378 fsec->pseqno == avc_policy_seqno(&selinux_state))
Eric Paris83d49852012-04-04 13:45:40 -04003379 /* No change since file_open check. */
Stephen Smalley20dda182009-06-22 14:54:53 -04003380 return 0;
3381
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003382 return selinux_revalidate_file_permission(file, mask);
3383}
3384
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385static int selinux_file_alloc_security(struct file *file)
3386{
3387 return file_alloc_security(file);
3388}
3389
3390static void selinux_file_free_security(struct file *file)
3391{
3392 file_free_security(file);
3393}
3394
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003395/*
3396 * Check whether a task has the ioctl permission and cmd
3397 * operation to an inode.
3398 */
Geliang Tang1d2a1682015-10-21 17:44:27 -04003399static int ioctl_has_perm(const struct cred *cred, struct file *file,
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003400 u32 requested, u16 cmd)
3401{
3402 struct common_audit_data ad;
3403 struct file_security_struct *fsec = file->f_security;
3404 struct inode *inode = file_inode(file);
Paul Moore20cdef82016-04-04 14:14:42 -04003405 struct inode_security_struct *isec;
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003406 struct lsm_ioctlop_audit ioctl;
3407 u32 ssid = cred_sid(cred);
3408 int rc;
3409 u8 driver = cmd >> 8;
3410 u8 xperm = cmd & 0xff;
3411
3412 ad.type = LSM_AUDIT_DATA_IOCTL_OP;
3413 ad.u.op = &ioctl;
3414 ad.u.op->cmd = cmd;
3415 ad.u.op->path = file->f_path;
3416
3417 if (ssid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003418 rc = avc_has_perm(&selinux_state,
3419 ssid, fsec->sid,
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003420 SECCLASS_FD,
3421 FD__USE,
3422 &ad);
3423 if (rc)
3424 goto out;
3425 }
3426
3427 if (unlikely(IS_PRIVATE(inode)))
3428 return 0;
3429
Paul Moore20cdef82016-04-04 14:14:42 -04003430 isec = inode_security(inode);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003431 rc = avc_has_extended_perms(&selinux_state,
3432 ssid, isec->sid, isec->sclass,
3433 requested, driver, xperm, &ad);
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003434out:
3435 return rc;
3436}
3437
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438static int selinux_file_ioctl(struct file *file, unsigned int cmd,
3439 unsigned long arg)
3440{
David Howells88e67f32008-11-14 10:39:21 +11003441 const struct cred *cred = current_cred();
Eric Paris0b24dcb2011-02-25 15:39:20 -05003442 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443
Eric Paris0b24dcb2011-02-25 15:39:20 -05003444 switch (cmd) {
3445 case FIONREAD:
3446 /* fall through */
3447 case FIBMAP:
3448 /* fall through */
3449 case FIGETBSZ:
3450 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003451 case FS_IOC_GETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003452 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003453 case FS_IOC_GETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003454 error = file_has_perm(cred, file, FILE__GETATTR);
3455 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456
Al Viro2f99c362012-03-23 16:04:05 -04003457 case FS_IOC_SETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003458 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003459 case FS_IOC_SETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003460 error = file_has_perm(cred, file, FILE__SETATTR);
3461 break;
3462
3463 /* sys_ioctl() checks */
3464 case FIONBIO:
3465 /* fall through */
3466 case FIOASYNC:
3467 error = file_has_perm(cred, file, 0);
3468 break;
3469
3470 case KDSKBENT:
3471 case KDSKBSENT:
Eric Paris6a9de492012-01-03 12:25:14 -05003472 error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04003473 SECURITY_CAP_AUDIT, true);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003474 break;
3475
3476 /* default case assumes that the command will go
3477 * to the file's ioctl() function.
3478 */
3479 default:
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003480 error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003481 }
3482 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483}
3484
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003485static int default_noexec;
3486
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
3488{
David Howells88e67f32008-11-14 10:39:21 +11003489 const struct cred *cred = current_cred();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003490 u32 sid = cred_sid(cred);
David Howellsd84f4f92008-11-14 10:39:23 +11003491 int rc = 0;
David Howells88e67f32008-11-14 10:39:21 +11003492
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003493 if (default_noexec &&
Stephen Smalley892e8ca2015-07-10 09:40:59 -04003494 (prot & PROT_EXEC) && (!file || IS_PRIVATE(file_inode(file)) ||
3495 (!shared && (prot & PROT_WRITE)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 /*
3497 * We are making executable an anonymous mapping or a
3498 * private file mapping that will also be writable.
3499 * This has an additional check.
3500 */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003501 rc = avc_has_perm(&selinux_state,
3502 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003503 PROCESS__EXECMEM, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 if (rc)
David Howellsd84f4f92008-11-14 10:39:23 +11003505 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507
3508 if (file) {
3509 /* read access is always possible with a mapping */
3510 u32 av = FILE__READ;
3511
3512 /* write access only matters if the mapping is shared */
3513 if (shared && (prot & PROT_WRITE))
3514 av |= FILE__WRITE;
3515
3516 if (prot & PROT_EXEC)
3517 av |= FILE__EXECUTE;
3518
David Howells88e67f32008-11-14 10:39:21 +11003519 return file_has_perm(cred, file, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 }
David Howellsd84f4f92008-11-14 10:39:23 +11003521
3522error:
3523 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524}
3525
Al Viroe5467852012-05-30 13:30:51 -04003526static int selinux_mmap_addr(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07003528 int rc = 0;
Paul Moore98883bf2014-03-19 16:46:11 -04003529
3530 if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
3531 u32 sid = current_sid();
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003532 rc = avc_has_perm(&selinux_state,
3533 sid, sid, SECCLASS_MEMPROTECT,
Paul Moore98883bf2014-03-19 16:46:11 -04003534 MEMPROTECT__MMAP_ZERO, NULL);
3535 }
3536
3537 return rc;
Al Viroe5467852012-05-30 13:30:51 -04003538}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539
Al Viroe5467852012-05-30 13:30:51 -04003540static int selinux_mmap_file(struct file *file, unsigned long reqprot,
3541 unsigned long prot, unsigned long flags)
3542{
Stephen Smalley3ba4bf52017-05-05 09:14:48 -04003543 struct common_audit_data ad;
3544 int rc;
3545
3546 if (file) {
3547 ad.type = LSM_AUDIT_DATA_FILE;
3548 ad.u.file = file;
3549 rc = inode_has_perm(current_cred(), file_inode(file),
3550 FILE__MAP, &ad);
3551 if (rc)
3552 return rc;
3553 }
3554
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003555 if (selinux_state.checkreqprot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 prot = reqprot;
3557
3558 return file_map_prot_check(file, prot,
3559 (flags & MAP_TYPE) == MAP_SHARED);
3560}
3561
3562static int selinux_file_mprotect(struct vm_area_struct *vma,
3563 unsigned long reqprot,
3564 unsigned long prot)
3565{
David Howells88e67f32008-11-14 10:39:21 +11003566 const struct cred *cred = current_cred();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003567 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003569 if (selinux_state.checkreqprot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 prot = reqprot;
3571
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003572 if (default_noexec &&
3573 (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
James Morrisd541bbe2009-01-29 12:19:51 +11003574 int rc = 0;
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003575 if (vma->vm_start >= vma->vm_mm->start_brk &&
3576 vma->vm_end <= vma->vm_mm->brk) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003577 rc = avc_has_perm(&selinux_state,
3578 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003579 PROCESS__EXECHEAP, NULL);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003580 } else if (!vma->vm_file &&
Stephen Smalleyc2316db2016-04-08 13:55:03 -04003581 ((vma->vm_start <= vma->vm_mm->start_stack &&
3582 vma->vm_end >= vma->vm_mm->start_stack) ||
Andy Lutomirskid17af502016-09-30 10:58:58 -07003583 vma_is_stack_for_current(vma))) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003584 rc = avc_has_perm(&selinux_state,
3585 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003586 PROCESS__EXECSTACK, NULL);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003587 } else if (vma->vm_file && vma->anon_vma) {
3588 /*
3589 * We are making executable a file mapping that has
3590 * had some COW done. Since pages might have been
3591 * written, check ability to execute the possibly
3592 * modified content. This typically should only
3593 * occur for text relocations.
3594 */
David Howellsd84f4f92008-11-14 10:39:23 +11003595 rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003596 }
Lorenzo Hernandez García-Hierro6b992192005-06-25 14:54:34 -07003597 if (rc)
3598 return rc;
3599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600
3601 return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
3602}
3603
3604static int selinux_file_lock(struct file *file, unsigned int cmd)
3605{
David Howells88e67f32008-11-14 10:39:21 +11003606 const struct cred *cred = current_cred();
3607
3608 return file_has_perm(cred, file, FILE__LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609}
3610
3611static int selinux_file_fcntl(struct file *file, unsigned int cmd,
3612 unsigned long arg)
3613{
David Howells88e67f32008-11-14 10:39:21 +11003614 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 int err = 0;
3616
3617 switch (cmd) {
Eric Paris828dfe12008-04-17 13:17:49 -04003618 case F_SETFL:
Eric Paris828dfe12008-04-17 13:17:49 -04003619 if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
David Howells88e67f32008-11-14 10:39:21 +11003620 err = file_has_perm(cred, file, FILE__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003622 }
3623 /* fall through */
3624 case F_SETOWN:
3625 case F_SETSIG:
3626 case F_GETFL:
3627 case F_GETOWN:
3628 case F_GETSIG:
Cyrill Gorcunov1d151c32012-07-30 14:43:00 -07003629 case F_GETOWNER_UIDS:
Eric Paris828dfe12008-04-17 13:17:49 -04003630 /* Just check FD__USE permission */
David Howells88e67f32008-11-14 10:39:21 +11003631 err = file_has_perm(cred, file, 0);
Eric Paris828dfe12008-04-17 13:17:49 -04003632 break;
3633 case F_GETLK:
3634 case F_SETLK:
3635 case F_SETLKW:
Jeff Layton0d3f7a22014-04-22 08:23:58 -04003636 case F_OFD_GETLK:
3637 case F_OFD_SETLK:
3638 case F_OFD_SETLKW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639#if BITS_PER_LONG == 32
Eric Paris828dfe12008-04-17 13:17:49 -04003640 case F_GETLK64:
3641 case F_SETLK64:
3642 case F_SETLKW64:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643#endif
David Howells88e67f32008-11-14 10:39:21 +11003644 err = file_has_perm(cred, file, FILE__LOCK);
Eric Paris828dfe12008-04-17 13:17:49 -04003645 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 }
3647
3648 return err;
3649}
3650
Jeff Laytone0b93ed2014-08-22 11:27:32 -04003651static void selinux_file_set_fowner(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 struct file_security_struct *fsec;
3654
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 fsec = file->f_security;
David Howells275bb412008-11-14 10:39:19 +11003656 fsec->fown_sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657}
3658
3659static int selinux_file_send_sigiotask(struct task_struct *tsk,
3660 struct fown_struct *fown, int signum)
3661{
Eric Paris828dfe12008-04-17 13:17:49 -04003662 struct file *file;
Stephen Smalley65c90bc2009-05-04 15:43:18 -04003663 u32 sid = task_sid(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 struct file_security_struct *fsec;
3666
3667 /* struct fown_struct is never outside the context of a struct file */
Eric Paris828dfe12008-04-17 13:17:49 -04003668 file = container_of(fown, struct file, f_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 fsec = file->f_security;
3671
3672 if (!signum)
3673 perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
3674 else
3675 perm = signal_to_av(signum);
3676
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003677 return avc_has_perm(&selinux_state,
3678 fsec->fown_sid, sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 SECCLASS_PROCESS, perm, NULL);
3680}
3681
3682static int selinux_file_receive(struct file *file)
3683{
David Howells88e67f32008-11-14 10:39:21 +11003684 const struct cred *cred = current_cred();
3685
3686 return file_has_perm(cred, file, file_to_av(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687}
3688
Al Viro94817692018-07-10 14:13:18 -04003689static int selinux_file_open(struct file *file)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003690{
3691 struct file_security_struct *fsec;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003692 struct inode_security_struct *isec;
David Howellsd84f4f92008-11-14 10:39:23 +11003693
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003694 fsec = file->f_security;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05003695 isec = inode_security(file_inode(file));
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003696 /*
3697 * Save inode label and policy sequence number
3698 * at open-time so that selinux_file_permission
3699 * can determine whether revalidation is necessary.
3700 * Task label is already saved in the file security
3701 * struct as its SID.
3702 */
3703 fsec->isid = isec->sid;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003704 fsec->pseqno = avc_policy_seqno(&selinux_state);
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003705 /*
3706 * Since the inode label or policy seqno may have changed
3707 * between the selinux_inode_permission check and the saving
3708 * of state above, recheck that access is still permitted.
3709 * Otherwise, access might never be revalidated against the
3710 * new inode label or new policy.
3711 * This check is not redundant - do not remove.
3712 */
Al Viro94817692018-07-10 14:13:18 -04003713 return file_path_has_perm(file->f_cred, file, open_file_to_av(file));
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003714}
3715
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716/* task security operations */
3717
Tetsuo Handaa79be232017-03-28 23:08:45 +09003718static int selinux_task_alloc(struct task_struct *task,
3719 unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003721 u32 sid = current_sid();
3722
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003723 return avc_has_perm(&selinux_state,
3724 sid, sid, SECCLASS_PROCESS, PROCESS__FORK, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725}
3726
David Howellsf1752ee2008-11-14 10:39:17 +11003727/*
David Howellsee18d642009-09-02 09:14:21 +01003728 * allocate the SELinux part of blank credentials
3729 */
3730static int selinux_cred_alloc_blank(struct cred *cred, gfp_t gfp)
3731{
3732 struct task_security_struct *tsec;
3733
3734 tsec = kzalloc(sizeof(struct task_security_struct), gfp);
3735 if (!tsec)
3736 return -ENOMEM;
3737
3738 cred->security = tsec;
3739 return 0;
3740}
3741
3742/*
David Howellsf1752ee2008-11-14 10:39:17 +11003743 * detach and free the LSM part of a set of credentials
3744 */
3745static void selinux_cred_free(struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746{
David Howellsf1752ee2008-11-14 10:39:17 +11003747 struct task_security_struct *tsec = cred->security;
David Howellse0e81732009-09-02 09:13:40 +01003748
Tetsuo Handa2edeaa32011-02-07 13:36:10 +00003749 /*
3750 * cred->security == NULL if security_cred_alloc_blank() or
3751 * security_prepare_creds() returned an error.
3752 */
3753 BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE);
David Howellse0e81732009-09-02 09:13:40 +01003754 cred->security = (void *) 0x7UL;
David Howellsf1752ee2008-11-14 10:39:17 +11003755 kfree(tsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756}
3757
David Howellsd84f4f92008-11-14 10:39:23 +11003758/*
3759 * prepare a new set of credentials for modification
3760 */
3761static int selinux_cred_prepare(struct cred *new, const struct cred *old,
3762 gfp_t gfp)
3763{
3764 const struct task_security_struct *old_tsec;
3765 struct task_security_struct *tsec;
3766
3767 old_tsec = old->security;
3768
3769 tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp);
3770 if (!tsec)
3771 return -ENOMEM;
3772
3773 new->security = tsec;
3774 return 0;
3775}
3776
3777/*
David Howellsee18d642009-09-02 09:14:21 +01003778 * transfer the SELinux data to a blank set of creds
3779 */
3780static void selinux_cred_transfer(struct cred *new, const struct cred *old)
3781{
3782 const struct task_security_struct *old_tsec = old->security;
3783 struct task_security_struct *tsec = new->security;
3784
3785 *tsec = *old_tsec;
3786}
3787
Matthew Garrett3ec30112018-01-08 13:36:19 -08003788static void selinux_cred_getsecid(const struct cred *c, u32 *secid)
3789{
3790 *secid = cred_sid(c);
3791}
3792
David Howellsee18d642009-09-02 09:14:21 +01003793/*
David Howells3a3b7ce2008-11-14 10:39:28 +11003794 * set the security data for a kernel service
3795 * - all the creation contexts are set to unlabelled
3796 */
3797static int selinux_kernel_act_as(struct cred *new, u32 secid)
3798{
3799 struct task_security_struct *tsec = new->security;
3800 u32 sid = current_sid();
3801 int ret;
3802
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003803 ret = avc_has_perm(&selinux_state,
3804 sid, secid,
David Howells3a3b7ce2008-11-14 10:39:28 +11003805 SECCLASS_KERNEL_SERVICE,
3806 KERNEL_SERVICE__USE_AS_OVERRIDE,
3807 NULL);
3808 if (ret == 0) {
3809 tsec->sid = secid;
3810 tsec->create_sid = 0;
3811 tsec->keycreate_sid = 0;
3812 tsec->sockcreate_sid = 0;
3813 }
3814 return ret;
3815}
3816
3817/*
3818 * set the file creation context in a security record to the same as the
3819 * objective context of the specified inode
3820 */
3821static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
3822{
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05003823 struct inode_security_struct *isec = inode_security(inode);
David Howells3a3b7ce2008-11-14 10:39:28 +11003824 struct task_security_struct *tsec = new->security;
3825 u32 sid = current_sid();
3826 int ret;
3827
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003828 ret = avc_has_perm(&selinux_state,
3829 sid, isec->sid,
David Howells3a3b7ce2008-11-14 10:39:28 +11003830 SECCLASS_KERNEL_SERVICE,
3831 KERNEL_SERVICE__CREATE_FILES_AS,
3832 NULL);
3833
3834 if (ret == 0)
3835 tsec->create_sid = isec->sid;
David Howellsef574712010-02-26 01:56:16 +00003836 return ret;
David Howells3a3b7ce2008-11-14 10:39:28 +11003837}
3838
Eric Parisdd8dbf22009-11-03 16:35:32 +11003839static int selinux_kernel_module_request(char *kmod_name)
Eric Paris25354c42009-08-13 09:45:03 -04003840{
Eric Parisdd8dbf22009-11-03 16:35:32 +11003841 struct common_audit_data ad;
3842
Eric Paris50c205f2012-04-04 15:01:43 -04003843 ad.type = LSM_AUDIT_DATA_KMOD;
Eric Parisdd8dbf22009-11-03 16:35:32 +11003844 ad.u.kmod_name = kmod_name;
3845
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003846 return avc_has_perm(&selinux_state,
3847 current_sid(), SECINITSID_KERNEL, SECCLASS_SYSTEM,
Eric Parisdd8dbf22009-11-03 16:35:32 +11003848 SYSTEM__MODULE_REQUEST, &ad);
Eric Paris25354c42009-08-13 09:45:03 -04003849}
3850
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003851static int selinux_kernel_module_from_file(struct file *file)
3852{
3853 struct common_audit_data ad;
3854 struct inode_security_struct *isec;
3855 struct file_security_struct *fsec;
3856 u32 sid = current_sid();
3857 int rc;
3858
3859 /* init_module */
3860 if (file == NULL)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003861 return avc_has_perm(&selinux_state,
3862 sid, sid, SECCLASS_SYSTEM,
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003863 SYSTEM__MODULE_LOAD, NULL);
3864
3865 /* finit_module */
Paul Moore20cdef82016-04-04 14:14:42 -04003866
Vivek Goyal43af5de2016-09-09 11:37:49 -04003867 ad.type = LSM_AUDIT_DATA_FILE;
3868 ad.u.file = file;
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003869
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003870 fsec = file->f_security;
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003871 if (sid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003872 rc = avc_has_perm(&selinux_state,
3873 sid, fsec->sid, SECCLASS_FD, FD__USE, &ad);
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003874 if (rc)
3875 return rc;
3876 }
3877
Paul Moore20cdef82016-04-04 14:14:42 -04003878 isec = inode_security(file_inode(file));
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003879 return avc_has_perm(&selinux_state,
3880 sid, isec->sid, SECCLASS_SYSTEM,
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003881 SYSTEM__MODULE_LOAD, &ad);
3882}
3883
3884static int selinux_kernel_read_file(struct file *file,
3885 enum kernel_read_file_id id)
3886{
3887 int rc = 0;
3888
3889 switch (id) {
3890 case READING_MODULE:
3891 rc = selinux_kernel_module_from_file(file);
3892 break;
3893 default:
3894 break;
3895 }
3896
3897 return rc;
3898}
3899
Mimi Zoharc77b8cd2018-07-13 14:06:02 -04003900static int selinux_kernel_load_data(enum kernel_load_data_id id)
3901{
3902 int rc = 0;
3903
3904 switch (id) {
3905 case LOADING_MODULE:
3906 rc = selinux_kernel_module_from_file(NULL);
3907 default:
3908 break;
3909 }
3910
3911 return rc;
3912}
3913
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
3915{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003916 return avc_has_perm(&selinux_state,
3917 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003918 PROCESS__SETPGID, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919}
3920
3921static int selinux_task_getpgid(struct task_struct *p)
3922{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003923 return avc_has_perm(&selinux_state,
3924 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003925 PROCESS__GETPGID, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926}
3927
3928static int selinux_task_getsid(struct task_struct *p)
3929{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003930 return avc_has_perm(&selinux_state,
3931 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003932 PROCESS__GETSESSION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933}
3934
David Quigleyf9008e4c2006-06-30 01:55:46 -07003935static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
3936{
David Howells275bb412008-11-14 10:39:19 +11003937 *secid = task_sid(p);
David Quigleyf9008e4c2006-06-30 01:55:46 -07003938}
3939
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940static int selinux_task_setnice(struct task_struct *p, int nice)
3941{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003942 return avc_has_perm(&selinux_state,
3943 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003944 PROCESS__SETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945}
3946
James Morris03e68062006-06-23 02:03:58 -07003947static int selinux_task_setioprio(struct task_struct *p, int ioprio)
3948{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003949 return avc_has_perm(&selinux_state,
3950 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003951 PROCESS__SETSCHED, NULL);
James Morris03e68062006-06-23 02:03:58 -07003952}
3953
David Quigleya1836a42006-06-30 01:55:49 -07003954static int selinux_task_getioprio(struct task_struct *p)
3955{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003956 return avc_has_perm(&selinux_state,
3957 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003958 PROCESS__GETSCHED, NULL);
David Quigleya1836a42006-06-30 01:55:49 -07003959}
3960
Corentin LABBE42985552017-10-04 20:32:18 +02003961static int selinux_task_prlimit(const struct cred *cred, const struct cred *tcred,
3962 unsigned int flags)
Stephen Smalley791ec492017-02-17 07:57:00 -05003963{
3964 u32 av = 0;
3965
Stephen Smalley84e68852017-02-28 09:35:08 -05003966 if (!flags)
3967 return 0;
Stephen Smalley791ec492017-02-17 07:57:00 -05003968 if (flags & LSM_PRLIMIT_WRITE)
3969 av |= PROCESS__SETRLIMIT;
3970 if (flags & LSM_PRLIMIT_READ)
3971 av |= PROCESS__GETRLIMIT;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003972 return avc_has_perm(&selinux_state,
3973 cred_sid(cred), cred_sid(tcred),
Stephen Smalley791ec492017-02-17 07:57:00 -05003974 SECCLASS_PROCESS, av, NULL);
3975}
3976
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003977static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
3978 struct rlimit *new_rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979{
Jiri Slaby8fd00b42009-08-26 18:41:16 +02003980 struct rlimit *old_rlim = p->signal->rlim + resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981
3982 /* Control the ability to change the hard limit (whether
3983 lowering or raising it), so that the hard limit can
3984 later be used as a safe reset point for the soft limit
David Howellsd84f4f92008-11-14 10:39:23 +11003985 upon context transitions. See selinux_bprm_committing_creds. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 if (old_rlim->rlim_max != new_rlim->rlim_max)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003987 return avc_has_perm(&selinux_state,
3988 current_sid(), task_sid(p),
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003989 SECCLASS_PROCESS, PROCESS__SETRLIMIT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990
3991 return 0;
3992}
3993
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09003994static int selinux_task_setscheduler(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003996 return avc_has_perm(&selinux_state,
3997 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003998 PROCESS__SETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999}
4000
4001static int selinux_task_getscheduler(struct task_struct *p)
4002{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004003 return avc_has_perm(&selinux_state,
4004 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004005 PROCESS__GETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006}
4007
David Quigley35601542006-06-23 02:04:01 -07004008static int selinux_task_movememory(struct task_struct *p)
4009{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004010 return avc_has_perm(&selinux_state,
4011 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004012 PROCESS__SETSCHED, NULL);
David Quigley35601542006-06-23 02:04:01 -07004013}
4014
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02004015static int selinux_task_kill(struct task_struct *p, struct kernel_siginfo *info,
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004016 int sig, const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017{
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004018 u32 secid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 if (!sig)
4022 perm = PROCESS__SIGNULL; /* null signal; existence test */
4023 else
4024 perm = signal_to_av(sig);
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004025 if (!cred)
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004026 secid = current_sid();
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004027 else
4028 secid = cred_sid(cred);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004029 return avc_has_perm(&selinux_state,
4030 secid, task_sid(p), SECCLASS_PROCESS, perm, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031}
4032
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033static void selinux_task_to_inode(struct task_struct *p,
4034 struct inode *inode)
4035{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 struct inode_security_struct *isec = inode->i_security;
David Howells275bb412008-11-14 10:39:19 +11004037 u32 sid = task_sid(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004039 spin_lock(&isec->lock);
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01004040 isec->sclass = inode_mode_to_security_class(inode->i_mode);
David Howells275bb412008-11-14 10:39:19 +11004041 isec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004042 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004043 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044}
4045
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004047static int selinux_parse_skb_ipv4(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04004048 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049{
4050 int offset, ihlen, ret = -EINVAL;
4051 struct iphdr _iph, *ih;
4052
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03004053 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
4055 if (ih == NULL)
4056 goto out;
4057
4058 ihlen = ih->ihl * 4;
4059 if (ihlen < sizeof(_iph))
4060 goto out;
4061
Eric Paris48c62af2012-04-02 13:15:44 -04004062 ad->u.net->v4info.saddr = ih->saddr;
4063 ad->u.net->v4info.daddr = ih->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 ret = 0;
4065
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004066 if (proto)
4067 *proto = ih->protocol;
4068
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 switch (ih->protocol) {
Eric Paris828dfe12008-04-17 13:17:49 -04004070 case IPPROTO_TCP: {
4071 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072
Eric Paris828dfe12008-04-17 13:17:49 -04004073 if (ntohs(ih->frag_off) & IP_OFFSET)
4074 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075
4076 offset += ihlen;
4077 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4078 if (th == NULL)
4079 break;
4080
Eric Paris48c62af2012-04-02 13:15:44 -04004081 ad->u.net->sport = th->source;
4082 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085
Eric Paris828dfe12008-04-17 13:17:49 -04004086 case IPPROTO_UDP: {
4087 struct udphdr _udph, *uh;
4088
4089 if (ntohs(ih->frag_off) & IP_OFFSET)
4090 break;
4091
4092 offset += ihlen;
4093 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4094 if (uh == NULL)
4095 break;
4096
Eric Paris48c62af2012-04-02 13:15:44 -04004097 ad->u.net->sport = uh->source;
4098 ad->u.net->dport = uh->dest;
Eric Paris828dfe12008-04-17 13:17:49 -04004099 break;
4100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101
James Morris2ee92d42006-11-13 16:09:01 -08004102 case IPPROTO_DCCP: {
4103 struct dccp_hdr _dccph, *dh;
4104
4105 if (ntohs(ih->frag_off) & IP_OFFSET)
4106 break;
4107
4108 offset += ihlen;
4109 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4110 if (dh == NULL)
4111 break;
4112
Eric Paris48c62af2012-04-02 13:15:44 -04004113 ad->u.net->sport = dh->dccph_sport;
4114 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08004115 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004116 }
James Morris2ee92d42006-11-13 16:09:01 -08004117
Richard Hainesd4529302018-02-13 20:57:18 +00004118#if IS_ENABLED(CONFIG_IP_SCTP)
4119 case IPPROTO_SCTP: {
4120 struct sctphdr _sctph, *sh;
4121
4122 if (ntohs(ih->frag_off) & IP_OFFSET)
4123 break;
4124
4125 offset += ihlen;
4126 sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4127 if (sh == NULL)
4128 break;
4129
4130 ad->u.net->sport = sh->source;
4131 ad->u.net->dport = sh->dest;
4132 break;
4133 }
4134#endif
Eric Paris828dfe12008-04-17 13:17:49 -04004135 default:
4136 break;
4137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138out:
4139 return ret;
4140}
4141
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04004142#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143
4144/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004145static int selinux_parse_skb_ipv6(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04004146 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147{
4148 u8 nexthdr;
4149 int ret = -EINVAL, offset;
4150 struct ipv6hdr _ipv6h, *ip6;
Jesse Gross75f28112011-11-30 17:05:51 -08004151 __be16 frag_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03004153 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
4155 if (ip6 == NULL)
4156 goto out;
4157
Eric Paris48c62af2012-04-02 13:15:44 -04004158 ad->u.net->v6info.saddr = ip6->saddr;
4159 ad->u.net->v6info.daddr = ip6->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 ret = 0;
4161
4162 nexthdr = ip6->nexthdr;
4163 offset += sizeof(_ipv6h);
Jesse Gross75f28112011-11-30 17:05:51 -08004164 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 if (offset < 0)
4166 goto out;
4167
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004168 if (proto)
4169 *proto = nexthdr;
4170
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 switch (nexthdr) {
4172 case IPPROTO_TCP: {
Eric Paris828dfe12008-04-17 13:17:49 -04004173 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174
4175 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4176 if (th == NULL)
4177 break;
4178
Eric Paris48c62af2012-04-02 13:15:44 -04004179 ad->u.net->sport = th->source;
4180 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 break;
4182 }
4183
4184 case IPPROTO_UDP: {
4185 struct udphdr _udph, *uh;
4186
4187 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4188 if (uh == NULL)
4189 break;
4190
Eric Paris48c62af2012-04-02 13:15:44 -04004191 ad->u.net->sport = uh->source;
4192 ad->u.net->dport = uh->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 break;
4194 }
4195
James Morris2ee92d42006-11-13 16:09:01 -08004196 case IPPROTO_DCCP: {
4197 struct dccp_hdr _dccph, *dh;
4198
4199 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4200 if (dh == NULL)
4201 break;
4202
Eric Paris48c62af2012-04-02 13:15:44 -04004203 ad->u.net->sport = dh->dccph_sport;
4204 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08004205 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004206 }
James Morris2ee92d42006-11-13 16:09:01 -08004207
Richard Hainesd4529302018-02-13 20:57:18 +00004208#if IS_ENABLED(CONFIG_IP_SCTP)
4209 case IPPROTO_SCTP: {
4210 struct sctphdr _sctph, *sh;
4211
4212 sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4213 if (sh == NULL)
4214 break;
4215
4216 ad->u.net->sport = sh->source;
4217 ad->u.net->dport = sh->dest;
4218 break;
4219 }
4220#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 /* includes fragments */
4222 default:
4223 break;
4224 }
4225out:
4226 return ret;
4227}
4228
4229#endif /* IPV6 */
4230
Thomas Liu2bf49692009-07-14 12:14:09 -04004231static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
David Howellscf9481e2008-07-27 21:31:07 +10004232 char **_addrp, int src, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233{
David Howellscf9481e2008-07-27 21:31:07 +10004234 char *addrp;
4235 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236
Eric Paris48c62af2012-04-02 13:15:44 -04004237 switch (ad->u.net->family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 case PF_INET:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004239 ret = selinux_parse_skb_ipv4(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10004240 if (ret)
4241 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04004242 addrp = (char *)(src ? &ad->u.net->v4info.saddr :
4243 &ad->u.net->v4info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10004244 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04004246#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 case PF_INET6:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004248 ret = selinux_parse_skb_ipv6(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10004249 if (ret)
4250 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04004251 addrp = (char *)(src ? &ad->u.net->v6info.saddr :
4252 &ad->u.net->v6info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10004253 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254#endif /* IPV6 */
4255 default:
David Howellscf9481e2008-07-27 21:31:07 +10004256 addrp = NULL;
4257 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 }
4259
David Howellscf9481e2008-07-27 21:31:07 +10004260parse_error:
peter enderborgc103a912018-06-12 10:09:03 +02004261 pr_warn(
David Howellscf9481e2008-07-27 21:31:07 +10004262 "SELinux: failure in selinux_parse_skb(),"
4263 " unable to parse packet\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 return ret;
David Howellscf9481e2008-07-27 21:31:07 +10004265
4266okay:
4267 if (_addrp)
4268 *_addrp = addrp;
4269 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270}
4271
Paul Moore4f6a9932007-03-01 14:35:22 -05004272/**
Paul Moore220deb92008-01-29 08:38:23 -05004273 * selinux_skb_peerlbl_sid - Determine the peer label of a packet
Paul Moore4f6a9932007-03-01 14:35:22 -05004274 * @skb: the packet
Paul Moore75e22912008-01-29 08:38:04 -05004275 * @family: protocol family
Paul Moore220deb92008-01-29 08:38:23 -05004276 * @sid: the packet's peer label SID
Paul Moore4f6a9932007-03-01 14:35:22 -05004277 *
4278 * Description:
Paul Moore220deb92008-01-29 08:38:23 -05004279 * Check the various different forms of network peer labeling and determine
4280 * the peer label/SID for the packet; most of the magic actually occurs in
4281 * the security server function security_net_peersid_cmp(). The function
4282 * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
4283 * or -EACCES if @sid is invalid due to inconsistencies with the different
4284 * peer labels.
Paul Moore4f6a9932007-03-01 14:35:22 -05004285 *
4286 */
Paul Moore220deb92008-01-29 08:38:23 -05004287static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
Paul Moore4f6a9932007-03-01 14:35:22 -05004288{
Paul Moore71f1cb02008-01-29 08:51:16 -05004289 int err;
Paul Moore4f6a9932007-03-01 14:35:22 -05004290 u32 xfrm_sid;
4291 u32 nlbl_sid;
Paul Moore220deb92008-01-29 08:38:23 -05004292 u32 nlbl_type;
Paul Moore4f6a9932007-03-01 14:35:22 -05004293
Paul Moore817eff72013-12-10 14:57:54 -05004294 err = selinux_xfrm_skb_sid(skb, &xfrm_sid);
Paul Moorebed4d7e2013-07-23 17:38:40 -04004295 if (unlikely(err))
4296 return -EACCES;
4297 err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
4298 if (unlikely(err))
4299 return -EACCES;
Paul Moore220deb92008-01-29 08:38:23 -05004300
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004301 err = security_net_peersid_resolve(&selinux_state, nlbl_sid,
4302 nlbl_type, xfrm_sid, sid);
Paul Moore71f1cb02008-01-29 08:51:16 -05004303 if (unlikely(err)) {
peter enderborgc103a912018-06-12 10:09:03 +02004304 pr_warn(
Paul Moore71f1cb02008-01-29 08:51:16 -05004305 "SELinux: failure in selinux_skb_peerlbl_sid(),"
4306 " unable to determine packet's peer label\n");
Paul Moore220deb92008-01-29 08:38:23 -05004307 return -EACCES;
Paul Moore71f1cb02008-01-29 08:51:16 -05004308 }
Paul Moore220deb92008-01-29 08:38:23 -05004309
4310 return 0;
Paul Moore4f6a9932007-03-01 14:35:22 -05004311}
4312
Paul Moore446b8022013-12-04 16:10:51 -05004313/**
4314 * selinux_conn_sid - Determine the child socket label for a connection
4315 * @sk_sid: the parent socket's SID
4316 * @skb_sid: the packet's SID
4317 * @conn_sid: the resulting connection SID
4318 *
4319 * If @skb_sid is valid then the user:role:type information from @sk_sid is
4320 * combined with the MLS information from @skb_sid in order to create
4321 * @conn_sid. If @skb_sid is not valid then then @conn_sid is simply a copy
4322 * of @sk_sid. Returns zero on success, negative values on failure.
4323 *
4324 */
4325static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid)
4326{
4327 int err = 0;
4328
4329 if (skb_sid != SECSID_NULL)
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004330 err = security_sid_mls_copy(&selinux_state, sk_sid, skb_sid,
4331 conn_sid);
Paul Moore446b8022013-12-04 16:10:51 -05004332 else
4333 *conn_sid = sk_sid;
4334
4335 return err;
4336}
4337
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338/* socket security operations */
Paul Moored4f2d972010-04-22 14:46:18 -04004339
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004340static int socket_sockcreate_sid(const struct task_security_struct *tsec,
4341 u16 secclass, u32 *socksid)
Paul Moored4f2d972010-04-22 14:46:18 -04004342{
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004343 if (tsec->sockcreate_sid > SECSID_NULL) {
4344 *socksid = tsec->sockcreate_sid;
4345 return 0;
4346 }
4347
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004348 return security_transition_sid(&selinux_state, tsec->sid, tsec->sid,
4349 secclass, NULL, socksid);
Paul Moored4f2d972010-04-22 14:46:18 -04004350}
4351
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004352static int sock_has_perm(struct sock *sk, u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353{
Paul Moore253bfae2010-04-22 14:46:19 -04004354 struct sk_security_struct *sksec = sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004355 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004356 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357
Paul Moore253bfae2010-04-22 14:46:19 -04004358 if (sksec->sid == SECINITSID_KERNEL)
4359 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360
Eric Paris50c205f2012-04-04 15:01:43 -04004361 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004362 ad.u.net = &net;
4363 ad.u.net->sk = sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004365 return avc_has_perm(&selinux_state,
4366 current_sid(), sksec->sid, sksec->sclass, perms,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004367 &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368}
4369
4370static int selinux_socket_create(int family, int type,
4371 int protocol, int kern)
4372{
Paul Moore5fb49872010-04-22 14:46:19 -04004373 const struct task_security_struct *tsec = current_security();
Paul Moored4f2d972010-04-22 14:46:18 -04004374 u32 newsid;
David Howells275bb412008-11-14 10:39:19 +11004375 u16 secclass;
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004376 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377
4378 if (kern)
Paul Moored4f2d972010-04-22 14:46:18 -04004379 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380
David Howells275bb412008-11-14 10:39:19 +11004381 secclass = socket_type_to_security_class(family, type, protocol);
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004382 rc = socket_sockcreate_sid(tsec, secclass, &newsid);
4383 if (rc)
4384 return rc;
4385
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004386 return avc_has_perm(&selinux_state,
4387 tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388}
4389
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07004390static int selinux_socket_post_create(struct socket *sock, int family,
4391 int type, int protocol, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392{
Paul Moore5fb49872010-04-22 14:46:19 -04004393 const struct task_security_struct *tsec = current_security();
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05004394 struct inode_security_struct *isec = inode_security_novalidate(SOCK_INODE(sock));
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004395 struct sk_security_struct *sksec;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004396 u16 sclass = socket_type_to_security_class(family, type, protocol);
4397 u32 sid = SECINITSID_KERNEL;
David Howells275bb412008-11-14 10:39:19 +11004398 int err = 0;
4399
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004400 if (!kern) {
4401 err = socket_sockcreate_sid(tsec, sclass, &sid);
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004402 if (err)
4403 return err;
4404 }
David Howells275bb412008-11-14 10:39:19 +11004405
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004406 isec->sclass = sclass;
4407 isec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004408 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004410 if (sock->sk) {
4411 sksec = sock->sk->sk_security;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004412 sksec->sclass = sclass;
4413 sksec->sid = sid;
Richard Hainesd4529302018-02-13 20:57:18 +00004414 /* Allows detection of the first association on this socket */
4415 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4416 sksec->sctp_assoc_state = SCTP_ASSOC_UNSET;
4417
Paul Moore389fb8002009-03-27 17:10:34 -04004418 err = selinux_netlbl_socket_post_create(sock->sk, family);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004419 }
4420
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07004421 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422}
4423
David Herrmann0b811db2018-05-04 16:28:21 +02004424static int selinux_socket_socketpair(struct socket *socka,
4425 struct socket *sockb)
4426{
4427 struct sk_security_struct *sksec_a = socka->sk->sk_security;
4428 struct sk_security_struct *sksec_b = sockb->sk->sk_security;
4429
4430 sksec_a->peer_sid = sksec_b->sid;
4431 sksec_b->peer_sid = sksec_a->sid;
4432
4433 return 0;
4434}
4435
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436/* Range of port numbers used to automatically bind.
4437 Need to determine whether we should perform a name_bind
4438 permission check between the socket and the port number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439
4440static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
4441{
Paul Moore253bfae2010-04-22 14:46:19 -04004442 struct sock *sk = sock->sk;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004443 struct sk_security_struct *sksec = sk->sk_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 u16 family;
4445 int err;
4446
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004447 err = sock_has_perm(sk, SOCKET__BIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 if (err)
4449 goto out;
4450
Richard Hainesd4529302018-02-13 20:57:18 +00004451 /* If PF_INET or PF_INET6, check name_bind permission for the port. */
Paul Moore253bfae2010-04-22 14:46:19 -04004452 family = sk->sk_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 if (family == PF_INET || family == PF_INET6) {
4454 char *addrp;
Thomas Liu2bf49692009-07-14 12:14:09 -04004455 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004456 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 struct sockaddr_in *addr4 = NULL;
4458 struct sockaddr_in6 *addr6 = NULL;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004459 u16 family_sa = address->sa_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 unsigned short snum;
James Morrise399f982008-06-12 01:39:58 +10004461 u32 sid, node_perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462
Richard Hainesd4529302018-02-13 20:57:18 +00004463 /*
4464 * sctp_bindx(3) calls via selinux_sctp_bind_connect()
4465 * that validates multiple binding addresses. Because of this
4466 * need to check address->sa_family as it is possible to have
4467 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4468 */
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004469 switch (family_sa) {
4470 case AF_UNSPEC:
Richard Haines68741a8a2018-03-02 19:54:34 +00004471 case AF_INET:
4472 if (addrlen < sizeof(struct sockaddr_in))
4473 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 addr4 = (struct sockaddr_in *)address;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004475 if (family_sa == AF_UNSPEC) {
4476 /* see __inet_bind(), we only want to allow
4477 * AF_UNSPEC if the address is INADDR_ANY
4478 */
4479 if (addr4->sin_addr.s_addr != htonl(INADDR_ANY))
4480 goto err_af;
4481 family_sa = AF_INET;
4482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 snum = ntohs(addr4->sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 addrp = (char *)&addr4->sin_addr.s_addr;
Richard Haines68741a8a2018-03-02 19:54:34 +00004485 break;
4486 case AF_INET6:
4487 if (addrlen < SIN6_LEN_RFC2133)
4488 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 addr6 = (struct sockaddr_in6 *)address;
4490 snum = ntohs(addr6->sin6_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 addrp = (char *)&addr6->sin6_addr.s6_addr;
Richard Haines68741a8a2018-03-02 19:54:34 +00004492 break;
4493 default:
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004494 goto err_af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495 }
4496
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004497 ad.type = LSM_AUDIT_DATA_NET;
4498 ad.u.net = &net;
4499 ad.u.net->sport = htons(snum);
4500 ad.u.net->family = family_sa;
4501
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004502 if (snum) {
4503 int low, high;
4504
Eric W. Biederman0bbf87d2013-09-28 14:10:59 -07004505 inet_get_local_port_range(sock_net(sk), &low, &high);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004506
Krister Johansen4548b682017-01-20 17:49:11 -08004507 if (snum < max(inet_prot_sock(sock_net(sk)), low) ||
4508 snum > high) {
Paul Moore3e1121722008-04-10 10:48:14 -04004509 err = sel_netport_sid(sk->sk_protocol,
4510 snum, &sid);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004511 if (err)
4512 goto out;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004513 err = avc_has_perm(&selinux_state,
4514 sksec->sid, sid,
Paul Moore253bfae2010-04-22 14:46:19 -04004515 sksec->sclass,
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004516 SOCKET__NAME_BIND, &ad);
4517 if (err)
4518 goto out;
4519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520 }
Eric Paris828dfe12008-04-17 13:17:49 -04004521
Paul Moore253bfae2010-04-22 14:46:19 -04004522 switch (sksec->sclass) {
James Morris13402582005-09-30 14:24:34 -04004523 case SECCLASS_TCP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 node_perm = TCP_SOCKET__NODE_BIND;
4525 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004526
James Morris13402582005-09-30 14:24:34 -04004527 case SECCLASS_UDP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 node_perm = UDP_SOCKET__NODE_BIND;
4529 break;
James Morris2ee92d42006-11-13 16:09:01 -08004530
4531 case SECCLASS_DCCP_SOCKET:
4532 node_perm = DCCP_SOCKET__NODE_BIND;
4533 break;
4534
Richard Hainesd4529302018-02-13 20:57:18 +00004535 case SECCLASS_SCTP_SOCKET:
4536 node_perm = SCTP_SOCKET__NODE_BIND;
4537 break;
4538
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 default:
4540 node_perm = RAWIP_SOCKET__NODE_BIND;
4541 break;
4542 }
Eric Paris828dfe12008-04-17 13:17:49 -04004543
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004544 err = sel_netnode_sid(addrp, family_sa, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545 if (err)
4546 goto out;
Eric Paris828dfe12008-04-17 13:17:49 -04004547
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004548 if (family_sa == AF_INET)
Eric Paris48c62af2012-04-02 13:15:44 -04004549 ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550 else
Eric Paris48c62af2012-04-02 13:15:44 -04004551 ad.u.net->v6info.saddr = addr6->sin6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004553 err = avc_has_perm(&selinux_state,
4554 sksec->sid, sid,
Paul Moore253bfae2010-04-22 14:46:19 -04004555 sksec->sclass, node_perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556 if (err)
4557 goto out;
4558 }
4559out:
4560 return err;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004561err_af:
4562 /* Note that SCTP services expect -EINVAL, others -EAFNOSUPPORT. */
4563 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4564 return -EINVAL;
4565 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566}
4567
Richard Hainesd4529302018-02-13 20:57:18 +00004568/* This supports connect(2) and SCTP connect services such as sctp_connectx(3)
Mauro Carvalho Chehab5fb94e92018-05-08 15:14:57 -03004569 * and sctp_sendmsg(3) as described in Documentation/security/LSM-sctp.rst
Richard Hainesd4529302018-02-13 20:57:18 +00004570 */
4571static int selinux_socket_connect_helper(struct socket *sock,
4572 struct sockaddr *address, int addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573{
Paul Moore014ab192008-10-10 10:16:33 -04004574 struct sock *sk = sock->sk;
Paul Moore253bfae2010-04-22 14:46:19 -04004575 struct sk_security_struct *sksec = sk->sk_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 int err;
4577
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004578 err = sock_has_perm(sk, SOCKET__CONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 if (err)
4580 return err;
4581
4582 /*
Richard Hainesd4529302018-02-13 20:57:18 +00004583 * If a TCP, DCCP or SCTP socket, check name_connect permission
4584 * for the port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 */
Paul Moore253bfae2010-04-22 14:46:19 -04004586 if (sksec->sclass == SECCLASS_TCP_SOCKET ||
Richard Hainesd4529302018-02-13 20:57:18 +00004587 sksec->sclass == SECCLASS_DCCP_SOCKET ||
4588 sksec->sclass == SECCLASS_SCTP_SOCKET) {
Thomas Liu2bf49692009-07-14 12:14:09 -04004589 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004590 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 struct sockaddr_in *addr4 = NULL;
4592 struct sockaddr_in6 *addr6 = NULL;
4593 unsigned short snum;
James Morris2ee92d42006-11-13 16:09:01 -08004594 u32 sid, perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595
Richard Hainesd4529302018-02-13 20:57:18 +00004596 /* sctp_connectx(3) calls via selinux_sctp_bind_connect()
4597 * that validates multiple connect addresses. Because of this
4598 * need to check address->sa_family as it is possible to have
4599 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4600 */
Richard Haines68741a8a2018-03-02 19:54:34 +00004601 switch (address->sa_family) {
4602 case AF_INET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 addr4 = (struct sockaddr_in *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004604 if (addrlen < sizeof(struct sockaddr_in))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605 return -EINVAL;
4606 snum = ntohs(addr4->sin_port);
Richard Haines68741a8a2018-03-02 19:54:34 +00004607 break;
4608 case AF_INET6:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 addr6 = (struct sockaddr_in6 *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004610 if (addrlen < SIN6_LEN_RFC2133)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 return -EINVAL;
4612 snum = ntohs(addr6->sin6_port);
Richard Haines68741a8a2018-03-02 19:54:34 +00004613 break;
4614 default:
4615 /* Note that SCTP services expect -EINVAL, whereas
4616 * others expect -EAFNOSUPPORT.
4617 */
4618 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4619 return -EINVAL;
4620 else
4621 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 }
4623
Paul Moore3e1121722008-04-10 10:48:14 -04004624 err = sel_netport_sid(sk->sk_protocol, snum, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 if (err)
Richard Hainesd4529302018-02-13 20:57:18 +00004626 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627
Richard Hainesd4529302018-02-13 20:57:18 +00004628 switch (sksec->sclass) {
4629 case SECCLASS_TCP_SOCKET:
4630 perm = TCP_SOCKET__NAME_CONNECT;
4631 break;
4632 case SECCLASS_DCCP_SOCKET:
4633 perm = DCCP_SOCKET__NAME_CONNECT;
4634 break;
4635 case SECCLASS_SCTP_SOCKET:
4636 perm = SCTP_SOCKET__NAME_CONNECT;
4637 break;
4638 }
James Morris2ee92d42006-11-13 16:09:01 -08004639
Eric Paris50c205f2012-04-04 15:01:43 -04004640 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004641 ad.u.net = &net;
4642 ad.u.net->dport = htons(snum);
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004643 ad.u.net->family = address->sa_family;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004644 err = avc_has_perm(&selinux_state,
4645 sksec->sid, sid, sksec->sclass, perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 if (err)
Richard Hainesd4529302018-02-13 20:57:18 +00004647 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 }
4649
Richard Hainesd4529302018-02-13 20:57:18 +00004650 return 0;
4651}
Paul Moore014ab192008-10-10 10:16:33 -04004652
Richard Hainesd4529302018-02-13 20:57:18 +00004653/* Supports connect(2), see comments in selinux_socket_connect_helper() */
4654static int selinux_socket_connect(struct socket *sock,
4655 struct sockaddr *address, int addrlen)
4656{
4657 int err;
4658 struct sock *sk = sock->sk;
4659
4660 err = selinux_socket_connect_helper(sock, address, addrlen);
4661 if (err)
4662 return err;
4663
4664 return selinux_netlbl_socket_connect(sk, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665}
4666
4667static int selinux_socket_listen(struct socket *sock, int backlog)
4668{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004669 return sock_has_perm(sock->sk, SOCKET__LISTEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670}
4671
4672static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
4673{
4674 int err;
4675 struct inode_security_struct *isec;
4676 struct inode_security_struct *newisec;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004677 u16 sclass;
4678 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004680 err = sock_has_perm(sock->sk, SOCKET__ACCEPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 if (err)
4682 return err;
4683
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05004684 isec = inode_security_novalidate(SOCK_INODE(sock));
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004685 spin_lock(&isec->lock);
4686 sclass = isec->sclass;
4687 sid = isec->sid;
4688 spin_unlock(&isec->lock);
4689
4690 newisec = inode_security_novalidate(SOCK_INODE(newsock));
4691 newisec->sclass = sclass;
4692 newisec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004693 newisec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694
4695 return 0;
4696}
4697
4698static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
Eric Paris828dfe12008-04-17 13:17:49 -04004699 int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004701 return sock_has_perm(sock->sk, SOCKET__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702}
4703
4704static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
4705 int size, int flags)
4706{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004707 return sock_has_perm(sock->sk, SOCKET__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708}
4709
4710static int selinux_socket_getsockname(struct socket *sock)
4711{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004712 return sock_has_perm(sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713}
4714
4715static int selinux_socket_getpeername(struct socket *sock)
4716{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004717 return sock_has_perm(sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718}
4719
Eric Paris828dfe12008-04-17 13:17:49 -04004720static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721{
Paul Mooref8687af2006-10-30 15:22:15 -08004722 int err;
4723
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004724 err = sock_has_perm(sock->sk, SOCKET__SETOPT);
Paul Mooref8687af2006-10-30 15:22:15 -08004725 if (err)
4726 return err;
4727
4728 return selinux_netlbl_socket_setsockopt(sock, level, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729}
4730
4731static int selinux_socket_getsockopt(struct socket *sock, int level,
4732 int optname)
4733{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004734 return sock_has_perm(sock->sk, SOCKET__GETOPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735}
4736
4737static int selinux_socket_shutdown(struct socket *sock, int how)
4738{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004739 return sock_has_perm(sock->sk, SOCKET__SHUTDOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740}
4741
David S. Miller3610cda2011-01-05 15:38:53 -08004742static int selinux_socket_unix_stream_connect(struct sock *sock,
4743 struct sock *other,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 struct sock *newsk)
4745{
David S. Miller3610cda2011-01-05 15:38:53 -08004746 struct sk_security_struct *sksec_sock = sock->sk_security;
4747 struct sk_security_struct *sksec_other = other->sk_security;
Paul Moore4d1e2452010-04-22 14:46:18 -04004748 struct sk_security_struct *sksec_new = newsk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004749 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004750 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751 int err;
4752
Eric Paris50c205f2012-04-04 15:01:43 -04004753 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004754 ad.u.net = &net;
4755 ad.u.net->sk = other;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004757 err = avc_has_perm(&selinux_state,
4758 sksec_sock->sid, sksec_other->sid,
Paul Moore4d1e2452010-04-22 14:46:18 -04004759 sksec_other->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 UNIX_STREAM_SOCKET__CONNECTTO, &ad);
4761 if (err)
4762 return err;
4763
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 /* server child socket */
Paul Moore4d1e2452010-04-22 14:46:18 -04004765 sksec_new->peer_sid = sksec_sock->sid;
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004766 err = security_sid_mls_copy(&selinux_state, sksec_other->sid,
4767 sksec_sock->sid, &sksec_new->sid);
Paul Moore4d1e2452010-04-22 14:46:18 -04004768 if (err)
4769 return err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004770
Paul Moore4d1e2452010-04-22 14:46:18 -04004771 /* connecting socket */
4772 sksec_sock->peer_sid = sksec_new->sid;
4773
4774 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775}
4776
4777static int selinux_socket_unix_may_send(struct socket *sock,
4778 struct socket *other)
4779{
Paul Moore253bfae2010-04-22 14:46:19 -04004780 struct sk_security_struct *ssec = sock->sk->sk_security;
4781 struct sk_security_struct *osec = other->sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004782 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004783 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784
Eric Paris50c205f2012-04-04 15:01:43 -04004785 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004786 ad.u.net = &net;
4787 ad.u.net->sk = other->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004789 return avc_has_perm(&selinux_state,
4790 ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
Paul Moore253bfae2010-04-22 14:46:19 -04004791 &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792}
4793
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004794static int selinux_inet_sys_rcv_skb(struct net *ns, int ifindex,
4795 char *addrp, u16 family, u32 peer_sid,
Thomas Liu2bf49692009-07-14 12:14:09 -04004796 struct common_audit_data *ad)
Paul Mooreeffad8d2008-01-29 08:49:27 -05004797{
4798 int err;
4799 u32 if_sid;
4800 u32 node_sid;
4801
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004802 err = sel_netif_sid(ns, ifindex, &if_sid);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004803 if (err)
4804 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004805 err = avc_has_perm(&selinux_state,
4806 peer_sid, if_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004807 SECCLASS_NETIF, NETIF__INGRESS, ad);
4808 if (err)
4809 return err;
4810
4811 err = sel_netnode_sid(addrp, family, &node_sid);
4812 if (err)
4813 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004814 return avc_has_perm(&selinux_state,
4815 peer_sid, node_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004816 SECCLASS_NODE, NODE__RECVFROM, ad);
4817}
4818
Paul Moore220deb92008-01-29 08:38:23 -05004819static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
Paul Moored8395c82008-10-10 10:16:30 -04004820 u16 family)
Paul Moore220deb92008-01-29 08:38:23 -05004821{
Paul Moore277d3422008-12-31 12:54:11 -05004822 int err = 0;
Paul Moore220deb92008-01-29 08:38:23 -05004823 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004824 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004825 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004826 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04004827 char *addrp;
4828
Eric Paris50c205f2012-04-04 15:01:43 -04004829 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004830 ad.u.net = &net;
4831 ad.u.net->netif = skb->skb_iif;
4832 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004833 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4834 if (err)
4835 return err;
Paul Moore220deb92008-01-29 08:38:23 -05004836
Paul Moore58bfbb52009-03-27 17:10:41 -04004837 if (selinux_secmark_enabled()) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004838 err = avc_has_perm(&selinux_state,
4839 sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Moored8395c82008-10-10 10:16:30 -04004840 PACKET__RECV, &ad);
Paul Moore58bfbb52009-03-27 17:10:41 -04004841 if (err)
4842 return err;
4843 }
Paul Moore220deb92008-01-29 08:38:23 -05004844
Steffen Klassertb9679a72011-02-23 12:55:21 +01004845 err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
4846 if (err)
4847 return err;
4848 err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004849
James Morris4e5ab4c2006-06-09 00:33:33 -07004850 return err;
4851}
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004852
James Morris4e5ab4c2006-06-09 00:33:33 -07004853static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
4854{
Paul Moore220deb92008-01-29 08:38:23 -05004855 int err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004856 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004857 u16 family = sk->sk_family;
4858 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004859 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004860 struct lsm_network_audit net = {0,};
Paul Moore220deb92008-01-29 08:38:23 -05004861 char *addrp;
Paul Moored8395c82008-10-10 10:16:30 -04004862 u8 secmark_active;
4863 u8 peerlbl_active;
James Morris4e5ab4c2006-06-09 00:33:33 -07004864
James Morris4e5ab4c2006-06-09 00:33:33 -07004865 if (family != PF_INET && family != PF_INET6)
Paul Moore220deb92008-01-29 08:38:23 -05004866 return 0;
James Morris4e5ab4c2006-06-09 00:33:33 -07004867
4868 /* Handle mapped IPv4 packets arriving via IPv6 sockets */
Al Viro87fcd702006-12-04 22:00:55 +00004869 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
James Morris4e5ab4c2006-06-09 00:33:33 -07004870 family = PF_INET;
4871
Paul Moored8395c82008-10-10 10:16:30 -04004872 /* If any sort of compatibility mode is enabled then handoff processing
4873 * to the selinux_sock_rcv_skb_compat() function to deal with the
4874 * special handling. We do this in an attempt to keep this function
4875 * as fast and as clean as possible. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004876 if (!selinux_policycap_netpeer())
Paul Moored8395c82008-10-10 10:16:30 -04004877 return selinux_sock_rcv_skb_compat(sk, skb, family);
4878
4879 secmark_active = selinux_secmark_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04004880 peerlbl_active = selinux_peerlbl_enabled();
Paul Moored8395c82008-10-10 10:16:30 -04004881 if (!secmark_active && !peerlbl_active)
4882 return 0;
4883
Eric Paris50c205f2012-04-04 15:01:43 -04004884 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004885 ad.u.net = &net;
4886 ad.u.net->netif = skb->skb_iif;
4887 ad.u.net->family = family;
Paul Moore224dfbd2008-01-29 08:38:13 -05004888 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
James Morris4e5ab4c2006-06-09 00:33:33 -07004889 if (err)
Paul Moore220deb92008-01-29 08:38:23 -05004890 return err;
James Morris4e5ab4c2006-06-09 00:33:33 -07004891
Paul Moored8395c82008-10-10 10:16:30 -04004892 if (peerlbl_active) {
Paul Moored621d352008-01-29 08:43:36 -05004893 u32 peer_sid;
4894
4895 err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
4896 if (err)
4897 return err;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004898 err = selinux_inet_sys_rcv_skb(sock_net(sk), skb->skb_iif,
4899 addrp, family, peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04004900 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04004901 selinux_netlbl_err(skb, family, err, 0);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004902 return err;
Paul Mooredfaebe92008-10-10 10:16:31 -04004903 }
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004904 err = avc_has_perm(&selinux_state,
4905 sk_sid, peer_sid, SECCLASS_PEER,
Paul Moored621d352008-01-29 08:43:36 -05004906 PEER__RECV, &ad);
Chad Hanson46d01d62013-12-23 17:45:01 -05004907 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04004908 selinux_netlbl_err(skb, family, err, 0);
Chad Hanson46d01d62013-12-23 17:45:01 -05004909 return err;
4910 }
Paul Moored621d352008-01-29 08:43:36 -05004911 }
4912
Paul Moored8395c82008-10-10 10:16:30 -04004913 if (secmark_active) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004914 err = avc_has_perm(&selinux_state,
4915 sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004916 PACKET__RECV, &ad);
4917 if (err)
4918 return err;
4919 }
4920
Paul Moored621d352008-01-29 08:43:36 -05004921 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922}
4923
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004924static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
4925 int __user *optlen, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926{
4927 int err = 0;
4928 char *scontext;
4929 u32 scontext_len;
Paul Moore253bfae2010-04-22 14:46:19 -04004930 struct sk_security_struct *sksec = sock->sk->sk_security;
Paul Moore3de4bab2006-11-17 17:38:54 -05004931 u32 peer_sid = SECSID_NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932
Paul Moore253bfae2010-04-22 14:46:19 -04004933 if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
Richard Hainesd4529302018-02-13 20:57:18 +00004934 sksec->sclass == SECCLASS_TCP_SOCKET ||
4935 sksec->sclass == SECCLASS_SCTP_SOCKET)
Eric Parisdd3e7832010-04-07 15:08:46 -04004936 peer_sid = sksec->peer_sid;
Paul Moore253bfae2010-04-22 14:46:19 -04004937 if (peer_sid == SECSID_NULL)
4938 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004940 err = security_sid_to_context(&selinux_state, peer_sid, &scontext,
4941 &scontext_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 if (err)
Paul Moore253bfae2010-04-22 14:46:19 -04004943 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944
4945 if (scontext_len > len) {
4946 err = -ERANGE;
4947 goto out_len;
4948 }
4949
4950 if (copy_to_user(optval, scontext, scontext_len))
4951 err = -EFAULT;
4952
4953out_len:
4954 if (put_user(scontext_len, optlen))
4955 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 kfree(scontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957 return err;
4958}
4959
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004960static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004961{
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004962 u32 peer_secid = SECSID_NULL;
Paul Moore75e22912008-01-29 08:38:04 -05004963 u16 family;
Paul Moore899134f2016-03-28 15:19:10 -04004964 struct inode_security_struct *isec;
Catherine Zhang877ce7c2006-06-29 12:27:47 -07004965
Paul Mooreaa862902008-10-10 10:16:29 -04004966 if (skb && skb->protocol == htons(ETH_P_IP))
4967 family = PF_INET;
4968 else if (skb && skb->protocol == htons(ETH_P_IPV6))
4969 family = PF_INET6;
4970 else if (sock)
Paul Moore75e22912008-01-29 08:38:04 -05004971 family = sock->sk->sk_family;
Paul Moore75e22912008-01-29 08:38:04 -05004972 else
4973 goto out;
4974
Paul Moore899134f2016-03-28 15:19:10 -04004975 if (sock && family == PF_UNIX) {
4976 isec = inode_security_novalidate(SOCK_INODE(sock));
4977 peer_secid = isec->sid;
4978 } else if (skb)
Paul Moore220deb92008-01-29 08:38:23 -05004979 selinux_skb_peerlbl_sid(skb, family, &peer_secid);
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004980
Paul Moore75e22912008-01-29 08:38:04 -05004981out:
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004982 *secid = peer_secid;
Paul Moore75e22912008-01-29 08:38:04 -05004983 if (peer_secid == SECSID_NULL)
4984 return -EINVAL;
4985 return 0;
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004986}
4987
Al Viro7d877f32005-10-21 03:20:43 -04004988static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989{
Paul Moore84914b72010-04-22 14:46:18 -04004990 struct sk_security_struct *sksec;
4991
4992 sksec = kzalloc(sizeof(*sksec), priority);
4993 if (!sksec)
4994 return -ENOMEM;
4995
4996 sksec->peer_sid = SECINITSID_UNLABELED;
4997 sksec->sid = SECINITSID_UNLABELED;
Stephen Smalley5dee25d2015-07-10 17:19:57 -04004998 sksec->sclass = SECCLASS_SOCKET;
Paul Moore84914b72010-04-22 14:46:18 -04004999 selinux_netlbl_sk_security_reset(sksec);
5000 sk->sk_security = sksec;
5001
5002 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003}
5004
5005static void selinux_sk_free_security(struct sock *sk)
5006{
Paul Moore84914b72010-04-22 14:46:18 -04005007 struct sk_security_struct *sksec = sk->sk_security;
5008
5009 sk->sk_security = NULL;
5010 selinux_netlbl_sk_security_free(sksec);
5011 kfree(sksec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012}
5013
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005014static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
5015{
Eric Parisdd3e7832010-04-07 15:08:46 -04005016 struct sk_security_struct *sksec = sk->sk_security;
5017 struct sk_security_struct *newsksec = newsk->sk_security;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005018
Eric Parisdd3e7832010-04-07 15:08:46 -04005019 newsksec->sid = sksec->sid;
5020 newsksec->peer_sid = sksec->peer_sid;
5021 newsksec->sclass = sksec->sclass;
Paul Moore99f59ed2006-08-29 17:53:48 -07005022
Eric Parisdd3e7832010-04-07 15:08:46 -04005023 selinux_netlbl_sk_security_reset(newsksec);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005024}
5025
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07005026static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005027{
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005028 if (!sk)
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07005029 *secid = SECINITSID_ANY_SOCKET;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005030 else {
5031 struct sk_security_struct *sksec = sk->sk_security;
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005032
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07005033 *secid = sksec->sid;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005034 }
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005035}
5036
Eric Paris828dfe12008-04-17 13:17:49 -04005037static void selinux_sock_graft(struct sock *sk, struct socket *parent)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005038{
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05005039 struct inode_security_struct *isec =
5040 inode_security_novalidate(SOCK_INODE(parent));
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005041 struct sk_security_struct *sksec = sk->sk_security;
5042
Paul Moore2873ead2014-07-28 10:42:48 -04005043 if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
5044 sk->sk_family == PF_UNIX)
David Woodhouse2148ccc2006-09-29 15:50:25 -07005045 isec->sid = sksec->sid;
Paul Moore220deb92008-01-29 08:38:23 -05005046 sksec->sclass = isec->sclass;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005047}
5048
Richard Hainesd4529302018-02-13 20:57:18 +00005049/* Called whenever SCTP receives an INIT chunk. This happens when an incoming
5050 * connect(2), sctp_connectx(3) or sctp_sendmsg(3) (with no association
5051 * already present).
5052 */
5053static int selinux_sctp_assoc_request(struct sctp_endpoint *ep,
5054 struct sk_buff *skb)
5055{
5056 struct sk_security_struct *sksec = ep->base.sk->sk_security;
5057 struct common_audit_data ad;
5058 struct lsm_network_audit net = {0,};
5059 u8 peerlbl_active;
5060 u32 peer_sid = SECINITSID_UNLABELED;
5061 u32 conn_sid;
5062 int err = 0;
5063
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005064 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005065 return 0;
5066
5067 peerlbl_active = selinux_peerlbl_enabled();
5068
5069 if (peerlbl_active) {
5070 /* This will return peer_sid = SECSID_NULL if there are
5071 * no peer labels, see security_net_peersid_resolve().
5072 */
5073 err = selinux_skb_peerlbl_sid(skb, ep->base.sk->sk_family,
5074 &peer_sid);
5075 if (err)
5076 return err;
5077
5078 if (peer_sid == SECSID_NULL)
5079 peer_sid = SECINITSID_UNLABELED;
5080 }
5081
5082 if (sksec->sctp_assoc_state == SCTP_ASSOC_UNSET) {
5083 sksec->sctp_assoc_state = SCTP_ASSOC_SET;
5084
5085 /* Here as first association on socket. As the peer SID
5086 * was allowed by peer recv (and the netif/node checks),
5087 * then it is approved by policy and used as the primary
5088 * peer SID for getpeercon(3).
5089 */
5090 sksec->peer_sid = peer_sid;
5091 } else if (sksec->peer_sid != peer_sid) {
5092 /* Other association peer SIDs are checked to enforce
5093 * consistency among the peer SIDs.
5094 */
5095 ad.type = LSM_AUDIT_DATA_NET;
5096 ad.u.net = &net;
5097 ad.u.net->sk = ep->base.sk;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005098 err = avc_has_perm(&selinux_state,
5099 sksec->peer_sid, peer_sid, sksec->sclass,
Richard Hainesd4529302018-02-13 20:57:18 +00005100 SCTP_SOCKET__ASSOCIATION, &ad);
5101 if (err)
5102 return err;
5103 }
5104
5105 /* Compute the MLS component for the connection and store
5106 * the information in ep. This will be used by SCTP TCP type
5107 * sockets and peeled off connections as they cause a new
5108 * socket to be generated. selinux_sctp_sk_clone() will then
5109 * plug this into the new socket.
5110 */
5111 err = selinux_conn_sid(sksec->sid, peer_sid, &conn_sid);
5112 if (err)
5113 return err;
5114
5115 ep->secid = conn_sid;
5116 ep->peer_secid = peer_sid;
5117
5118 /* Set any NetLabel labels including CIPSO/CALIPSO options. */
5119 return selinux_netlbl_sctp_assoc_request(ep, skb);
5120}
5121
5122/* Check if sctp IPv4/IPv6 addresses are valid for binding or connecting
5123 * based on their @optname.
5124 */
5125static int selinux_sctp_bind_connect(struct sock *sk, int optname,
5126 struct sockaddr *address,
5127 int addrlen)
5128{
5129 int len, err = 0, walk_size = 0;
5130 void *addr_buf;
5131 struct sockaddr *addr;
5132 struct socket *sock;
5133
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005134 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005135 return 0;
5136
5137 /* Process one or more addresses that may be IPv4 or IPv6 */
5138 sock = sk->sk_socket;
5139 addr_buf = address;
5140
5141 while (walk_size < addrlen) {
Ondrej Mosnacekc1383252018-11-13 16:16:08 +01005142 if (walk_size + sizeof(sa_family_t) > addrlen)
5143 return -EINVAL;
5144
Richard Hainesd4529302018-02-13 20:57:18 +00005145 addr = addr_buf;
5146 switch (addr->sa_family) {
Alexey Kodanev4152dc92018-05-11 20:15:13 +03005147 case AF_UNSPEC:
Richard Hainesd4529302018-02-13 20:57:18 +00005148 case AF_INET:
5149 len = sizeof(struct sockaddr_in);
5150 break;
5151 case AF_INET6:
5152 len = sizeof(struct sockaddr_in6);
5153 break;
5154 default:
Alexey Kodanev4152dc92018-05-11 20:15:13 +03005155 return -EINVAL;
Richard Hainesd4529302018-02-13 20:57:18 +00005156 }
5157
5158 err = -EINVAL;
5159 switch (optname) {
5160 /* Bind checks */
5161 case SCTP_PRIMARY_ADDR:
5162 case SCTP_SET_PEER_PRIMARY_ADDR:
5163 case SCTP_SOCKOPT_BINDX_ADD:
5164 err = selinux_socket_bind(sock, addr, len);
5165 break;
5166 /* Connect checks */
5167 case SCTP_SOCKOPT_CONNECTX:
5168 case SCTP_PARAM_SET_PRIMARY:
5169 case SCTP_PARAM_ADD_IP:
5170 case SCTP_SENDMSG_CONNECT:
5171 err = selinux_socket_connect_helper(sock, addr, len);
5172 if (err)
5173 return err;
5174
5175 /* As selinux_sctp_bind_connect() is called by the
5176 * SCTP protocol layer, the socket is already locked,
5177 * therefore selinux_netlbl_socket_connect_locked() is
5178 * is called here. The situations handled are:
5179 * sctp_connectx(3), sctp_sendmsg(3), sendmsg(2),
5180 * whenever a new IP address is added or when a new
5181 * primary address is selected.
5182 * Note that an SCTP connect(2) call happens before
5183 * the SCTP protocol layer and is handled via
5184 * selinux_socket_connect().
5185 */
5186 err = selinux_netlbl_socket_connect_locked(sk, addr);
5187 break;
5188 }
5189
5190 if (err)
5191 return err;
5192
5193 addr_buf += len;
5194 walk_size += len;
5195 }
5196
5197 return 0;
5198}
5199
5200/* Called whenever a new socket is created by accept(2) or sctp_peeloff(3). */
5201static void selinux_sctp_sk_clone(struct sctp_endpoint *ep, struct sock *sk,
5202 struct sock *newsk)
5203{
5204 struct sk_security_struct *sksec = sk->sk_security;
5205 struct sk_security_struct *newsksec = newsk->sk_security;
5206
5207 /* If policy does not support SECCLASS_SCTP_SOCKET then call
5208 * the non-sctp clone version.
5209 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005210 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005211 return selinux_sk_clone_security(sk, newsk);
5212
5213 newsksec->sid = ep->secid;
5214 newsksec->peer_sid = ep->peer_secid;
5215 newsksec->sclass = sksec->sclass;
5216 selinux_netlbl_sctp_sk_clone(sk, newsk);
5217}
5218
Adrian Bunk9a673e52006-08-15 00:03:53 -07005219static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
5220 struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005221{
5222 struct sk_security_struct *sksec = sk->sk_security;
5223 int err;
Paul Moore0b1f24e2013-12-03 11:39:13 -05005224 u16 family = req->rsk_ops->family;
Paul Moore446b8022013-12-04 16:10:51 -05005225 u32 connsid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005226 u32 peersid;
5227
Paul Mooreaa862902008-10-10 10:16:29 -04005228 err = selinux_skb_peerlbl_sid(skb, family, &peersid);
Paul Moore220deb92008-01-29 08:38:23 -05005229 if (err)
5230 return err;
Paul Moore446b8022013-12-04 16:10:51 -05005231 err = selinux_conn_sid(sksec->sid, peersid, &connsid);
5232 if (err)
5233 return err;
5234 req->secid = connsid;
5235 req->peer_secid = peersid;
Venkat Yekkiralaa51c64f2006-07-27 22:01:34 -07005236
Paul Moore389fb8002009-03-27 17:10:34 -04005237 return selinux_netlbl_inet_conn_request(req, family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005238}
5239
Adrian Bunk9a673e52006-08-15 00:03:53 -07005240static void selinux_inet_csk_clone(struct sock *newsk,
5241 const struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005242{
5243 struct sk_security_struct *newsksec = newsk->sk_security;
5244
5245 newsksec->sid = req->secid;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005246 newsksec->peer_sid = req->peer_secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005247 /* NOTE: Ideally, we should also get the isec->sid for the
5248 new socket in sync, but we don't have the isec available yet.
5249 So we will wait until sock_graft to do it, by which
5250 time it will have been created and available. */
Paul Moore99f59ed2006-08-29 17:53:48 -07005251
Paul Moore9f2ad662006-11-17 17:38:53 -05005252 /* We don't need to take any sort of lock here as we are the only
5253 * thread with access to newsksec */
Paul Moore389fb8002009-03-27 17:10:34 -04005254 selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005255}
5256
Paul Moore014ab192008-10-10 10:16:33 -04005257static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005258{
Paul Mooreaa862902008-10-10 10:16:29 -04005259 u16 family = sk->sk_family;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005260 struct sk_security_struct *sksec = sk->sk_security;
5261
Paul Mooreaa862902008-10-10 10:16:29 -04005262 /* handle mapped IPv4 packets arriving via IPv6 sockets */
5263 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
5264 family = PF_INET;
5265
5266 selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005267}
5268
Eric Paris2606fd12010-10-13 16:24:41 -04005269static int selinux_secmark_relabel_packet(u32 sid)
5270{
5271 const struct task_security_struct *__tsec;
5272 u32 tsid;
5273
5274 __tsec = current_security();
5275 tsid = __tsec->sid;
5276
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005277 return avc_has_perm(&selinux_state,
5278 tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO,
5279 NULL);
Eric Paris2606fd12010-10-13 16:24:41 -04005280}
5281
5282static void selinux_secmark_refcount_inc(void)
5283{
5284 atomic_inc(&selinux_secmark_refcount);
5285}
5286
5287static void selinux_secmark_refcount_dec(void)
5288{
5289 atomic_dec(&selinux_secmark_refcount);
5290}
5291
Adrian Bunk9a673e52006-08-15 00:03:53 -07005292static void selinux_req_classify_flow(const struct request_sock *req,
5293 struct flowi *fl)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005294{
David S. Miller1d28f422011-03-12 00:29:39 -05005295 fl->flowi_secid = req->secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005296}
5297
Paul Moore5dbbaf22013-01-14 07:12:19 +00005298static int selinux_tun_dev_alloc_security(void **security)
5299{
5300 struct tun_security_struct *tunsec;
5301
5302 tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL);
5303 if (!tunsec)
5304 return -ENOMEM;
5305 tunsec->sid = current_sid();
5306
5307 *security = tunsec;
5308 return 0;
5309}
5310
5311static void selinux_tun_dev_free_security(void *security)
5312{
5313 kfree(security);
5314}
5315
Paul Mooreed6d76e2009-08-28 18:12:49 -04005316static int selinux_tun_dev_create(void)
5317{
5318 u32 sid = current_sid();
5319
5320 /* we aren't taking into account the "sockcreate" SID since the socket
5321 * that is being created here is not a socket in the traditional sense,
5322 * instead it is a private sock, accessible only to the kernel, and
5323 * representing a wide range of network traffic spanning multiple
5324 * connections unlike traditional sockets - check the TUN driver to
5325 * get a better understanding of why this socket is special */
5326
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005327 return avc_has_perm(&selinux_state,
5328 sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005329 NULL);
5330}
5331
Paul Moore5dbbaf22013-01-14 07:12:19 +00005332static int selinux_tun_dev_attach_queue(void *security)
Paul Mooreed6d76e2009-08-28 18:12:49 -04005333{
Paul Moore5dbbaf22013-01-14 07:12:19 +00005334 struct tun_security_struct *tunsec = security;
5335
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005336 return avc_has_perm(&selinux_state,
5337 current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET,
Paul Moore5dbbaf22013-01-14 07:12:19 +00005338 TUN_SOCKET__ATTACH_QUEUE, NULL);
5339}
5340
5341static int selinux_tun_dev_attach(struct sock *sk, void *security)
5342{
5343 struct tun_security_struct *tunsec = security;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005344 struct sk_security_struct *sksec = sk->sk_security;
5345
5346 /* we don't currently perform any NetLabel based labeling here and it
5347 * isn't clear that we would want to do so anyway; while we could apply
5348 * labeling without the support of the TUN user the resulting labeled
5349 * traffic from the other end of the connection would almost certainly
5350 * cause confusion to the TUN user that had no idea network labeling
5351 * protocols were being used */
5352
Paul Moore5dbbaf22013-01-14 07:12:19 +00005353 sksec->sid = tunsec->sid;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005354 sksec->sclass = SECCLASS_TUN_SOCKET;
Paul Moore5dbbaf22013-01-14 07:12:19 +00005355
5356 return 0;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005357}
5358
Paul Moore5dbbaf22013-01-14 07:12:19 +00005359static int selinux_tun_dev_open(void *security)
Paul Mooreed6d76e2009-08-28 18:12:49 -04005360{
Paul Moore5dbbaf22013-01-14 07:12:19 +00005361 struct tun_security_struct *tunsec = security;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005362 u32 sid = current_sid();
5363 int err;
5364
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005365 err = avc_has_perm(&selinux_state,
5366 sid, tunsec->sid, SECCLASS_TUN_SOCKET,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005367 TUN_SOCKET__RELABELFROM, NULL);
5368 if (err)
5369 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005370 err = avc_has_perm(&selinux_state,
5371 sid, sid, SECCLASS_TUN_SOCKET,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005372 TUN_SOCKET__RELABELTO, NULL);
5373 if (err)
5374 return err;
Paul Moore5dbbaf22013-01-14 07:12:19 +00005375 tunsec->sid = sid;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005376
5377 return 0;
5378}
5379
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
5381{
5382 int err = 0;
5383 u32 perm;
5384 struct nlmsghdr *nlh;
Paul Moore253bfae2010-04-22 14:46:19 -04005385 struct sk_security_struct *sksec = sk->sk_security;
Eric Paris828dfe12008-04-17 13:17:49 -04005386
Hong zhi guo77954982013-03-27 06:49:35 +00005387 if (skb->len < NLMSG_HDRLEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388 err = -EINVAL;
5389 goto out;
5390 }
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -07005391 nlh = nlmsg_hdr(skb);
Eric Paris828dfe12008-04-17 13:17:49 -04005392
Paul Moore253bfae2010-04-22 14:46:19 -04005393 err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394 if (err) {
5395 if (err == -EINVAL) {
Vladis Dronov76319942015-12-24 11:09:41 -05005396 pr_warn_ratelimited("SELinux: unrecognized netlink"
5397 " message: protocol=%hu nlmsg_type=%hu sclass=%s"
5398 " pig=%d comm=%s\n",
Marek Milkoviccded3ff2015-06-04 16:22:16 -04005399 sk->sk_protocol, nlh->nlmsg_type,
Vladis Dronov76319942015-12-24 11:09:41 -05005400 secclass_map[sksec->sclass - 1].name,
5401 task_pid_nr(current), current->comm);
Paul Mooree5a5ca92018-03-01 17:38:30 -05005402 if (!enforcing_enabled(&selinux_state) ||
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005403 security_get_allow_unknown(&selinux_state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404 err = 0;
5405 }
5406
5407 /* Ignore */
5408 if (err == -ENOENT)
5409 err = 0;
5410 goto out;
5411 }
5412
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005413 err = sock_has_perm(sk, perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414out:
5415 return err;
5416}
5417
5418#ifdef CONFIG_NETFILTER
5419
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005420static unsigned int selinux_ip_forward(struct sk_buff *skb,
5421 const struct net_device *indev,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005422 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423{
Paul Mooredfaebe92008-10-10 10:16:31 -04005424 int err;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005425 char *addrp;
5426 u32 peer_sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04005427 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005428 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05005429 u8 secmark_active;
Paul Moore948bf852008-10-10 10:16:32 -04005430 u8 netlbl_active;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005431 u8 peerlbl_active;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005432
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005433 if (!selinux_policycap_netpeer())
Paul Mooreeffad8d2008-01-29 08:49:27 -05005434 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005435
Paul Mooreeffad8d2008-01-29 08:49:27 -05005436 secmark_active = selinux_secmark_enabled();
Paul Moore948bf852008-10-10 10:16:32 -04005437 netlbl_active = netlbl_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04005438 peerlbl_active = selinux_peerlbl_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05005439 if (!secmark_active && !peerlbl_active)
5440 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005441
Paul Moored8395c82008-10-10 10:16:30 -04005442 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
5443 return NF_DROP;
5444
Eric Paris50c205f2012-04-04 15:01:43 -04005445 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005446 ad.u.net = &net;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005447 ad.u.net->netif = indev->ifindex;
Eric Paris48c62af2012-04-02 13:15:44 -04005448 ad.u.net->family = family;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005449 if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
5450 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451
Paul Mooredfaebe92008-10-10 10:16:31 -04005452 if (peerlbl_active) {
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005453 err = selinux_inet_sys_rcv_skb(dev_net(indev), indev->ifindex,
5454 addrp, family, peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04005455 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04005456 selinux_netlbl_err(skb, family, err, 1);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005457 return NF_DROP;
Paul Mooredfaebe92008-10-10 10:16:31 -04005458 }
5459 }
Paul Mooreeffad8d2008-01-29 08:49:27 -05005460
5461 if (secmark_active)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005462 if (avc_has_perm(&selinux_state,
5463 peer_sid, skb->secmark,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005464 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
5465 return NF_DROP;
5466
Paul Moore948bf852008-10-10 10:16:32 -04005467 if (netlbl_active)
5468 /* we do this in the FORWARD path and not the POST_ROUTING
5469 * path because we want to make sure we apply the necessary
5470 * labeling before IPsec is applied so we can leverage AH
5471 * protection */
5472 if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
5473 return NF_DROP;
5474
Paul Mooreeffad8d2008-01-29 08:49:27 -05005475 return NF_ACCEPT;
5476}
5477
Eric W. Biederman06198b32015-09-18 14:33:06 -05005478static unsigned int selinux_ipv4_forward(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005479 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005480 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005481{
David S. Miller238e54c2015-04-03 20:32:56 -04005482 return selinux_ip_forward(skb, state->in, PF_INET);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005483}
5484
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005485#if IS_ENABLED(CONFIG_IPV6)
Eric W. Biederman06198b32015-09-18 14:33:06 -05005486static unsigned int selinux_ipv6_forward(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005487 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005488 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005489{
David S. Miller238e54c2015-04-03 20:32:56 -04005490 return selinux_ip_forward(skb, state->in, PF_INET6);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005491}
5492#endif /* IPV6 */
5493
Paul Moore948bf852008-10-10 10:16:32 -04005494static unsigned int selinux_ip_output(struct sk_buff *skb,
5495 u16 family)
5496{
Paul Moore47180062013-12-04 16:10:45 -05005497 struct sock *sk;
Paul Moore948bf852008-10-10 10:16:32 -04005498 u32 sid;
5499
5500 if (!netlbl_enabled())
5501 return NF_ACCEPT;
5502
5503 /* we do this in the LOCAL_OUT path and not the POST_ROUTING path
5504 * because we want to make sure we apply the necessary labeling
5505 * before IPsec is applied so we can leverage AH protection */
Paul Moore47180062013-12-04 16:10:45 -05005506 sk = skb->sk;
5507 if (sk) {
5508 struct sk_security_struct *sksec;
5509
Eric Dumazete446f9d2015-10-08 05:01:55 -07005510 if (sk_listener(sk))
Paul Moore47180062013-12-04 16:10:45 -05005511 /* if the socket is the listening state then this
5512 * packet is a SYN-ACK packet which means it needs to
5513 * be labeled based on the connection/request_sock and
5514 * not the parent socket. unfortunately, we can't
5515 * lookup the request_sock yet as it isn't queued on
5516 * the parent socket until after the SYN-ACK is sent.
5517 * the "solution" is to simply pass the packet as-is
5518 * as any IP option based labeling should be copied
5519 * from the initial connection request (in the IP
5520 * layer). it is far from ideal, but until we get a
5521 * security label in the packet itself this is the
5522 * best we can do. */
5523 return NF_ACCEPT;
5524
5525 /* standard practice, label using the parent socket */
5526 sksec = sk->sk_security;
Paul Moore948bf852008-10-10 10:16:32 -04005527 sid = sksec->sid;
5528 } else
5529 sid = SECINITSID_KERNEL;
5530 if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
5531 return NF_DROP;
5532
5533 return NF_ACCEPT;
5534}
5535
Eric W. Biederman06198b32015-09-18 14:33:06 -05005536static unsigned int selinux_ipv4_output(void *priv,
Paul Moore948bf852008-10-10 10:16:32 -04005537 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005538 const struct nf_hook_state *state)
Paul Moore948bf852008-10-10 10:16:32 -04005539{
5540 return selinux_ip_output(skb, PF_INET);
5541}
5542
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005543#if IS_ENABLED(CONFIG_IPV6)
Huw Davies2917f572016-06-27 15:06:15 -04005544static unsigned int selinux_ipv6_output(void *priv,
5545 struct sk_buff *skb,
5546 const struct nf_hook_state *state)
5547{
5548 return selinux_ip_output(skb, PF_INET6);
5549}
5550#endif /* IPV6 */
5551
Paul Mooreeffad8d2008-01-29 08:49:27 -05005552static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
5553 int ifindex,
Paul Moored8395c82008-10-10 10:16:30 -04005554 u16 family)
James Morris4e5ab4c2006-06-09 00:33:33 -07005555{
Eric Dumazet54abc682015-11-08 10:54:07 -08005556 struct sock *sk = skb_to_full_sk(skb);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005557 struct sk_security_struct *sksec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005558 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005559 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04005560 char *addrp;
5561 u8 proto;
James Morris4e5ab4c2006-06-09 00:33:33 -07005562
Paul Mooreeffad8d2008-01-29 08:49:27 -05005563 if (sk == NULL)
5564 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005565 sksec = sk->sk_security;
James Morris4e5ab4c2006-06-09 00:33:33 -07005566
Eric Paris50c205f2012-04-04 15:01:43 -04005567 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005568 ad.u.net = &net;
5569 ad.u.net->netif = ifindex;
5570 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04005571 if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
5572 return NF_DROP;
5573
Paul Moore58bfbb52009-03-27 17:10:41 -04005574 if (selinux_secmark_enabled())
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005575 if (avc_has_perm(&selinux_state,
5576 sksec->sid, skb->secmark,
Paul Moored8395c82008-10-10 10:16:30 -04005577 SECCLASS_PACKET, PACKET__SEND, &ad))
Eric Paris2fe66ec2010-11-23 06:28:08 +00005578 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07005579
Steffen Klassertb9679a72011-02-23 12:55:21 +01005580 if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
5581 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07005582
Paul Mooreeffad8d2008-01-29 08:49:27 -05005583 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584}
5585
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005586static unsigned int selinux_ip_postroute(struct sk_buff *skb,
5587 const struct net_device *outdev,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005588 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589{
Paul Mooreeffad8d2008-01-29 08:49:27 -05005590 u32 secmark_perm;
5591 u32 peer_sid;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005592 int ifindex = outdev->ifindex;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005593 struct sock *sk;
Thomas Liu2bf49692009-07-14 12:14:09 -04005594 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005595 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05005596 char *addrp;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005597 u8 secmark_active;
5598 u8 peerlbl_active;
5599
Paul Mooreeffad8d2008-01-29 08:49:27 -05005600 /* If any sort of compatibility mode is enabled then handoff processing
5601 * to the selinux_ip_postroute_compat() function to deal with the
5602 * special handling. We do this in an attempt to keep this function
5603 * as fast and as clean as possible. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005604 if (!selinux_policycap_netpeer())
Paul Moored8395c82008-10-10 10:16:30 -04005605 return selinux_ip_postroute_compat(skb, ifindex, family);
Paul Moorec0828e52013-12-10 14:58:01 -05005606
Paul Mooreeffad8d2008-01-29 08:49:27 -05005607 secmark_active = selinux_secmark_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04005608 peerlbl_active = selinux_peerlbl_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05005609 if (!secmark_active && !peerlbl_active)
5610 return NF_ACCEPT;
5611
Eric Dumazet54abc682015-11-08 10:54:07 -08005612 sk = skb_to_full_sk(skb);
Paul Moorec0828e52013-12-10 14:58:01 -05005613
Paul Mooreeffad8d2008-01-29 08:49:27 -05005614#ifdef CONFIG_XFRM
5615 /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
5616 * packet transformation so allow the packet to pass without any checks
5617 * since we'll have another chance to perform access control checks
5618 * when the packet is on it's final way out.
5619 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
Paul Moorec0828e52013-12-10 14:58:01 -05005620 * is NULL, in this case go ahead and apply access control.
5621 * NOTE: if this is a local socket (skb->sk != NULL) that is in the
5622 * TCP listening state we cannot wait until the XFRM processing
5623 * is done as we will miss out on the SA label if we do;
5624 * unfortunately, this means more work, but it is only once per
5625 * connection. */
5626 if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL &&
Eric Dumazete446f9d2015-10-08 05:01:55 -07005627 !(sk && sk_listener(sk)))
Paul Mooreeffad8d2008-01-29 08:49:27 -05005628 return NF_ACCEPT;
5629#endif
Paul Mooreeffad8d2008-01-29 08:49:27 -05005630
Paul Moored8395c82008-10-10 10:16:30 -04005631 if (sk == NULL) {
Paul Moore446b8022013-12-04 16:10:51 -05005632 /* Without an associated socket the packet is either coming
5633 * from the kernel or it is being forwarded; check the packet
5634 * to determine which and if the packet is being forwarded
5635 * query the packet directly to determine the security label. */
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005636 if (skb->skb_iif) {
5637 secmark_perm = PACKET__FORWARD_OUT;
Paul Moored8395c82008-10-10 10:16:30 -04005638 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005639 return NF_DROP;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005640 } else {
5641 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04005642 peer_sid = SECINITSID_KERNEL;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005643 }
Eric Dumazete446f9d2015-10-08 05:01:55 -07005644 } else if (sk_listener(sk)) {
Paul Moore446b8022013-12-04 16:10:51 -05005645 /* Locally generated packet but the associated socket is in the
5646 * listening state which means this is a SYN-ACK packet. In
5647 * this particular case the correct security label is assigned
5648 * to the connection/request_sock but unfortunately we can't
5649 * query the request_sock as it isn't queued on the parent
5650 * socket until after the SYN-ACK packet is sent; the only
5651 * viable choice is to regenerate the label like we do in
5652 * selinux_inet_conn_request(). See also selinux_ip_output()
5653 * for similar problems. */
5654 u32 skb_sid;
Eric Dumazete446f9d2015-10-08 05:01:55 -07005655 struct sk_security_struct *sksec;
5656
Eric Dumazete446f9d2015-10-08 05:01:55 -07005657 sksec = sk->sk_security;
Paul Moore446b8022013-12-04 16:10:51 -05005658 if (selinux_skb_peerlbl_sid(skb, family, &skb_sid))
5659 return NF_DROP;
Paul Moorec0828e52013-12-10 14:58:01 -05005660 /* At this point, if the returned skb peerlbl is SECSID_NULL
5661 * and the packet has been through at least one XFRM
5662 * transformation then we must be dealing with the "final"
5663 * form of labeled IPsec packet; since we've already applied
5664 * all of our access controls on this packet we can safely
5665 * pass the packet. */
5666 if (skb_sid == SECSID_NULL) {
5667 switch (family) {
5668 case PF_INET:
5669 if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
5670 return NF_ACCEPT;
5671 break;
5672 case PF_INET6:
5673 if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED)
5674 return NF_ACCEPT;
Paul Moorea7a91a12014-09-03 10:51:59 -04005675 break;
Paul Moorec0828e52013-12-10 14:58:01 -05005676 default:
5677 return NF_DROP_ERR(-ECONNREFUSED);
5678 }
5679 }
Paul Moore446b8022013-12-04 16:10:51 -05005680 if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid))
5681 return NF_DROP;
5682 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04005683 } else {
Paul Moore446b8022013-12-04 16:10:51 -05005684 /* Locally generated packet, fetch the security label from the
5685 * associated socket. */
Paul Mooreeffad8d2008-01-29 08:49:27 -05005686 struct sk_security_struct *sksec = sk->sk_security;
5687 peer_sid = sksec->sid;
5688 secmark_perm = PACKET__SEND;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005689 }
5690
Eric Paris50c205f2012-04-04 15:01:43 -04005691 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005692 ad.u.net = &net;
5693 ad.u.net->netif = ifindex;
5694 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04005695 if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
Eric Paris04f6d702010-11-23 06:28:02 +00005696 return NF_DROP;
Paul Moored8395c82008-10-10 10:16:30 -04005697
Paul Mooreeffad8d2008-01-29 08:49:27 -05005698 if (secmark_active)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005699 if (avc_has_perm(&selinux_state,
5700 peer_sid, skb->secmark,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005701 SECCLASS_PACKET, secmark_perm, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005702 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005703
5704 if (peerlbl_active) {
5705 u32 if_sid;
5706 u32 node_sid;
5707
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005708 if (sel_netif_sid(dev_net(outdev), ifindex, &if_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005709 return NF_DROP;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005710 if (avc_has_perm(&selinux_state,
5711 peer_sid, if_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005712 SECCLASS_NETIF, NETIF__EGRESS, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005713 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005714
5715 if (sel_netnode_sid(addrp, family, &node_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005716 return NF_DROP;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005717 if (avc_has_perm(&selinux_state,
5718 peer_sid, node_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005719 SECCLASS_NODE, NODE__SENDTO, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005720 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005721 }
5722
5723 return NF_ACCEPT;
5724}
5725
Eric W. Biederman06198b32015-09-18 14:33:06 -05005726static unsigned int selinux_ipv4_postroute(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005727 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005728 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005729{
David S. Miller238e54c2015-04-03 20:32:56 -04005730 return selinux_ip_postroute(skb, state->out, PF_INET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731}
5732
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005733#if IS_ENABLED(CONFIG_IPV6)
Eric W. Biederman06198b32015-09-18 14:33:06 -05005734static unsigned int selinux_ipv6_postroute(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005735 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005736 const struct nf_hook_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737{
David S. Miller238e54c2015-04-03 20:32:56 -04005738 return selinux_ip_postroute(skb, state->out, PF_INET6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740#endif /* IPV6 */
5741
5742#endif /* CONFIG_NETFILTER */
5743
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
5745{
Stephen Smalley941fc5b2009-10-01 14:48:23 -04005746 return selinux_nlmsg_perm(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747}
5748
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005749static int ipc_alloc_security(struct kern_ipc_perm *perm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750 u16 sclass)
5751{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752 struct ipc_security_struct *isec;
5753
James Morris89d155e2005-10-30 14:59:21 -08005754 isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755 if (!isec)
5756 return -ENOMEM;
5757
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758 isec->sclass = sclass;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005759 isec->sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005760 perm->security = isec;
5761
5762 return 0;
5763}
5764
5765static void ipc_free_security(struct kern_ipc_perm *perm)
5766{
5767 struct ipc_security_struct *isec = perm->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768 perm->security = NULL;
5769 kfree(isec);
5770}
5771
5772static int msg_msg_alloc_security(struct msg_msg *msg)
5773{
5774 struct msg_security_struct *msec;
5775
James Morris89d155e2005-10-30 14:59:21 -08005776 msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777 if (!msec)
5778 return -ENOMEM;
5779
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780 msec->sid = SECINITSID_UNLABELED;
5781 msg->security = msec;
5782
5783 return 0;
5784}
5785
5786static void msg_msg_free_security(struct msg_msg *msg)
5787{
5788 struct msg_security_struct *msec = msg->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005789
5790 msg->security = NULL;
5791 kfree(msec);
5792}
5793
5794static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
Stephen Smalley6af963f2005-05-01 08:58:39 -07005795 u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005798 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005799 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801 isec = ipc_perms->security;
5802
Eric Paris50c205f2012-04-04 15:01:43 -04005803 ad.type = LSM_AUDIT_DATA_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804 ad.u.ipc_id = ipc_perms->key;
5805
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005806 return avc_has_perm(&selinux_state,
5807 sid, isec->sid, isec->sclass, perms, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808}
5809
5810static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
5811{
5812 return msg_msg_alloc_security(msg);
5813}
5814
5815static void selinux_msg_msg_free_security(struct msg_msg *msg)
5816{
5817 msg_msg_free_security(msg);
5818}
5819
5820/* message queue security operations */
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005821static int selinux_msg_queue_alloc_security(struct kern_ipc_perm *msq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005823 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005824 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005825 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826 int rc;
5827
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005828 rc = ipc_alloc_security(msq, SECCLASS_MSGQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005829 if (rc)
5830 return rc;
5831
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005832 isec = msq->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833
Eric Paris50c205f2012-04-04 15:01:43 -04005834 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005835 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005837 rc = avc_has_perm(&selinux_state,
5838 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839 MSGQ__CREATE, &ad);
5840 if (rc) {
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005841 ipc_free_security(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842 return rc;
5843 }
5844 return 0;
5845}
5846
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005847static void selinux_msg_queue_free_security(struct kern_ipc_perm *msq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848{
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005849 ipc_free_security(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005850}
5851
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005852static int selinux_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005855 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005856 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005858 isec = msq->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859
Eric Paris50c205f2012-04-04 15:01:43 -04005860 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005861 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005863 return avc_has_perm(&selinux_state,
5864 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005865 MSGQ__ASSOCIATE, &ad);
5866}
5867
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005868static int selinux_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869{
5870 int err;
5871 int perms;
5872
Eric Paris828dfe12008-04-17 13:17:49 -04005873 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874 case IPC_INFO:
5875 case MSG_INFO:
5876 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005877 return avc_has_perm(&selinux_state,
5878 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005879 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880 case IPC_STAT:
5881 case MSG_STAT:
Davidlohr Bueso23c8cec2018-04-10 16:35:30 -07005882 case MSG_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005883 perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
5884 break;
5885 case IPC_SET:
5886 perms = MSGQ__SETATTR;
5887 break;
5888 case IPC_RMID:
5889 perms = MSGQ__DESTROY;
5890 break;
5891 default:
5892 return 0;
5893 }
5894
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005895 err = ipc_has_perm(msq, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005896 return err;
5897}
5898
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005899static int selinux_msg_queue_msgsnd(struct kern_ipc_perm *msq, struct msg_msg *msg, int msqflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005901 struct ipc_security_struct *isec;
5902 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005903 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005904 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905 int rc;
5906
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005907 isec = msq->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908 msec = msg->security;
5909
5910 /*
5911 * First time through, need to assign label to the message
5912 */
5913 if (msec->sid == SECINITSID_UNLABELED) {
5914 /*
5915 * Compute new sid based on current process and
5916 * message queue this message will be stored in
5917 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005918 rc = security_transition_sid(&selinux_state, sid, isec->sid,
5919 SECCLASS_MSG, NULL, &msec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005920 if (rc)
5921 return rc;
5922 }
5923
Eric Paris50c205f2012-04-04 15:01:43 -04005924 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005925 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005926
5927 /* Can this process write to the queue? */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005928 rc = avc_has_perm(&selinux_state,
5929 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005930 MSGQ__WRITE, &ad);
5931 if (!rc)
5932 /* Can this process send the message */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005933 rc = avc_has_perm(&selinux_state,
5934 sid, msec->sid, SECCLASS_MSG,
David Howells275bb412008-11-14 10:39:19 +11005935 MSG__SEND, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005936 if (!rc)
5937 /* Can the message be put in the queue? */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005938 rc = avc_has_perm(&selinux_state,
5939 msec->sid, isec->sid, SECCLASS_MSGQ,
David Howells275bb412008-11-14 10:39:19 +11005940 MSGQ__ENQUEUE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941
5942 return rc;
5943}
5944
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005945static int selinux_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946 struct task_struct *target,
5947 long type, int mode)
5948{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005949 struct ipc_security_struct *isec;
5950 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005951 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005952 u32 sid = task_sid(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005953 int rc;
5954
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005955 isec = msq->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005956 msec = msg->security;
5957
Eric Paris50c205f2012-04-04 15:01:43 -04005958 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005959 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005960
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005961 rc = avc_has_perm(&selinux_state,
5962 sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005963 SECCLASS_MSGQ, MSGQ__READ, &ad);
5964 if (!rc)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005965 rc = avc_has_perm(&selinux_state,
5966 sid, msec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005967 SECCLASS_MSG, MSG__RECEIVE, &ad);
5968 return rc;
5969}
5970
5971/* Shared Memory security operations */
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005972static int selinux_shm_alloc_security(struct kern_ipc_perm *shp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005973{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005974 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005975 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005976 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977 int rc;
5978
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005979 rc = ipc_alloc_security(shp, SECCLASS_SHM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005980 if (rc)
5981 return rc;
5982
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005983 isec = shp->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984
Eric Paris50c205f2012-04-04 15:01:43 -04005985 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005986 ad.u.ipc_id = shp->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005988 rc = avc_has_perm(&selinux_state,
5989 sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005990 SHM__CREATE, &ad);
5991 if (rc) {
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005992 ipc_free_security(shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993 return rc;
5994 }
5995 return 0;
5996}
5997
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005998static void selinux_shm_free_security(struct kern_ipc_perm *shp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999{
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006000 ipc_free_security(shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006001}
6002
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006003static int selinux_shm_associate(struct kern_ipc_perm *shp, int shmflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006004{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006006 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006007 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006008
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006009 isec = shp->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010
Eric Paris50c205f2012-04-04 15:01:43 -04006011 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006012 ad.u.ipc_id = shp->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006014 return avc_has_perm(&selinux_state,
6015 sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006016 SHM__ASSOCIATE, &ad);
6017}
6018
6019/* Note, at this point, shp is locked down */
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006020static int selinux_shm_shmctl(struct kern_ipc_perm *shp, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006021{
6022 int perms;
6023 int err;
6024
Eric Paris828dfe12008-04-17 13:17:49 -04006025 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026 case IPC_INFO:
6027 case SHM_INFO:
6028 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006029 return avc_has_perm(&selinux_state,
6030 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006031 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032 case IPC_STAT:
6033 case SHM_STAT:
Davidlohr Buesoc21a6972018-04-10 16:35:23 -07006034 case SHM_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006035 perms = SHM__GETATTR | SHM__ASSOCIATE;
6036 break;
6037 case IPC_SET:
6038 perms = SHM__SETATTR;
6039 break;
6040 case SHM_LOCK:
6041 case SHM_UNLOCK:
6042 perms = SHM__LOCK;
6043 break;
6044 case IPC_RMID:
6045 perms = SHM__DESTROY;
6046 break;
6047 default:
6048 return 0;
6049 }
6050
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006051 err = ipc_has_perm(shp, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052 return err;
6053}
6054
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006055static int selinux_shm_shmat(struct kern_ipc_perm *shp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056 char __user *shmaddr, int shmflg)
6057{
6058 u32 perms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059
6060 if (shmflg & SHM_RDONLY)
6061 perms = SHM__READ;
6062 else
6063 perms = SHM__READ | SHM__WRITE;
6064
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006065 return ipc_has_perm(shp, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006066}
6067
6068/* Semaphore security operations */
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006069static int selinux_sem_alloc_security(struct kern_ipc_perm *sma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006072 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006073 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006074 int rc;
6075
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006076 rc = ipc_alloc_security(sma, SECCLASS_SEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006077 if (rc)
6078 return rc;
6079
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006080 isec = sma->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081
Eric Paris50c205f2012-04-04 15:01:43 -04006082 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006083 ad.u.ipc_id = sma->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006085 rc = avc_has_perm(&selinux_state,
6086 sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006087 SEM__CREATE, &ad);
6088 if (rc) {
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006089 ipc_free_security(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006090 return rc;
6091 }
6092 return 0;
6093}
6094
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006095static void selinux_sem_free_security(struct kern_ipc_perm *sma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006096{
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006097 ipc_free_security(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006098}
6099
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006100static int selinux_sem_associate(struct kern_ipc_perm *sma, int semflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006102 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006103 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006104 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006105
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006106 isec = sma->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107
Eric Paris50c205f2012-04-04 15:01:43 -04006108 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006109 ad.u.ipc_id = sma->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006110
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006111 return avc_has_perm(&selinux_state,
6112 sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006113 SEM__ASSOCIATE, &ad);
6114}
6115
6116/* Note, at this point, sma is locked down */
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006117static int selinux_sem_semctl(struct kern_ipc_perm *sma, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006118{
6119 int err;
6120 u32 perms;
6121
Eric Paris828dfe12008-04-17 13:17:49 -04006122 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006123 case IPC_INFO:
6124 case SEM_INFO:
6125 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006126 return avc_has_perm(&selinux_state,
6127 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006128 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006129 case GETPID:
6130 case GETNCNT:
6131 case GETZCNT:
6132 perms = SEM__GETATTR;
6133 break;
6134 case GETVAL:
6135 case GETALL:
6136 perms = SEM__READ;
6137 break;
6138 case SETVAL:
6139 case SETALL:
6140 perms = SEM__WRITE;
6141 break;
6142 case IPC_RMID:
6143 perms = SEM__DESTROY;
6144 break;
6145 case IPC_SET:
6146 perms = SEM__SETATTR;
6147 break;
6148 case IPC_STAT:
6149 case SEM_STAT:
Davidlohr Buesoa280d6d2018-04-10 16:35:26 -07006150 case SEM_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006151 perms = SEM__GETATTR | SEM__ASSOCIATE;
6152 break;
6153 default:
6154 return 0;
6155 }
6156
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006157 err = ipc_has_perm(sma, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006158 return err;
6159}
6160
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006161static int selinux_sem_semop(struct kern_ipc_perm *sma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006162 struct sembuf *sops, unsigned nsops, int alter)
6163{
6164 u32 perms;
6165
6166 if (alter)
6167 perms = SEM__READ | SEM__WRITE;
6168 else
6169 perms = SEM__READ;
6170
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006171 return ipc_has_perm(sma, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006172}
6173
6174static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
6175{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006176 u32 av = 0;
6177
Linus Torvalds1da177e2005-04-16 15:20:36 -07006178 av = 0;
6179 if (flag & S_IRUGO)
6180 av |= IPC__UNIX_READ;
6181 if (flag & S_IWUGO)
6182 av |= IPC__UNIX_WRITE;
6183
6184 if (av == 0)
6185 return 0;
6186
Stephen Smalley6af963f2005-05-01 08:58:39 -07006187 return ipc_has_perm(ipcp, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006188}
6189
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02006190static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
6191{
6192 struct ipc_security_struct *isec = ipcp->security;
6193 *secid = isec->sid;
6194}
6195
Eric Paris828dfe12008-04-17 13:17:49 -04006196static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006197{
6198 if (inode)
6199 inode_doinit_with_dentry(inode, dentry);
6200}
6201
6202static int selinux_getprocattr(struct task_struct *p,
Al Viro04ff9702007-03-12 16:17:58 +00006203 char *name, char **value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204{
David Howells275bb412008-11-14 10:39:19 +11006205 const struct task_security_struct *__tsec;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00006206 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006207 int error;
Al Viro04ff9702007-03-12 16:17:58 +00006208 unsigned len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006209
David Howells275bb412008-11-14 10:39:19 +11006210 rcu_read_lock();
6211 __tsec = __task_cred(p)->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006212
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006213 if (current != p) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006214 error = avc_has_perm(&selinux_state,
6215 current_sid(), __tsec->sid,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006216 SECCLASS_PROCESS, PROCESS__GETATTR, NULL);
6217 if (error)
6218 goto bad;
6219 }
6220
Linus Torvalds1da177e2005-04-16 15:20:36 -07006221 if (!strcmp(name, "current"))
David Howells275bb412008-11-14 10:39:19 +11006222 sid = __tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006223 else if (!strcmp(name, "prev"))
David Howells275bb412008-11-14 10:39:19 +11006224 sid = __tsec->osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006225 else if (!strcmp(name, "exec"))
David Howells275bb412008-11-14 10:39:19 +11006226 sid = __tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006227 else if (!strcmp(name, "fscreate"))
David Howells275bb412008-11-14 10:39:19 +11006228 sid = __tsec->create_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006229 else if (!strcmp(name, "keycreate"))
David Howells275bb412008-11-14 10:39:19 +11006230 sid = __tsec->keycreate_sid;
Eric Paris42c3e032006-06-26 00:26:03 -07006231 else if (!strcmp(name, "sockcreate"))
David Howells275bb412008-11-14 10:39:19 +11006232 sid = __tsec->sockcreate_sid;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006233 else {
6234 error = -EINVAL;
6235 goto bad;
6236 }
David Howells275bb412008-11-14 10:39:19 +11006237 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006238
6239 if (!sid)
6240 return 0;
6241
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006242 error = security_sid_to_context(&selinux_state, sid, value, &len);
Al Viro04ff9702007-03-12 16:17:58 +00006243 if (error)
6244 return error;
6245 return len;
David Howells275bb412008-11-14 10:39:19 +11006246
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006247bad:
David Howells275bb412008-11-14 10:39:19 +11006248 rcu_read_unlock();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006249 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250}
6251
Stephen Smalleyb21507e2017-01-09 10:07:31 -05006252static int selinux_setprocattr(const char *name, void *value, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006253{
6254 struct task_security_struct *tsec;
David Howellsd84f4f92008-11-14 10:39:23 +11006255 struct cred *new;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006256 u32 mysid = current_sid(), sid = 0, ptsid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006257 int error;
6258 char *str = value;
6259
Linus Torvalds1da177e2005-04-16 15:20:36 -07006260 /*
6261 * Basic control over ability to set these attributes at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006262 */
6263 if (!strcmp(name, "exec"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006264 error = avc_has_perm(&selinux_state,
6265 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006266 PROCESS__SETEXEC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006267 else if (!strcmp(name, "fscreate"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006268 error = avc_has_perm(&selinux_state,
6269 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006270 PROCESS__SETFSCREATE, NULL);
Michael LeMay4eb582c2006-06-26 00:24:57 -07006271 else if (!strcmp(name, "keycreate"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006272 error = avc_has_perm(&selinux_state,
6273 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006274 PROCESS__SETKEYCREATE, NULL);
Eric Paris42c3e032006-06-26 00:26:03 -07006275 else if (!strcmp(name, "sockcreate"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006276 error = avc_has_perm(&selinux_state,
6277 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006278 PROCESS__SETSOCKCREATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006279 else if (!strcmp(name, "current"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006280 error = avc_has_perm(&selinux_state,
6281 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006282 PROCESS__SETCURRENT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006283 else
6284 error = -EINVAL;
6285 if (error)
6286 return error;
6287
6288 /* Obtain a SID for the context, if one was specified. */
Stephen Smalleya050a572017-01-31 11:54:04 -05006289 if (size && str[0] && str[0] != '\n') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006290 if (str[size-1] == '\n') {
6291 str[size-1] = 0;
6292 size--;
6293 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006294 error = security_context_to_sid(&selinux_state, value, size,
6295 &sid, GFP_KERNEL);
Stephen Smalley12b29f32008-05-07 13:03:20 -04006296 if (error == -EINVAL && !strcmp(name, "fscreate")) {
Stephen Smalleydb590002017-04-20 11:31:30 -04006297 if (!has_cap_mac_admin(true)) {
Eric Parisd6ea83e2012-04-04 13:45:49 -04006298 struct audit_buffer *ab;
6299 size_t audit_size;
6300
6301 /* We strip a nul only if it is at the end, otherwise the
6302 * context contains a nul and we should audit that */
6303 if (str[size - 1] == '\0')
6304 audit_size = size - 1;
6305 else
6306 audit_size = size;
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04006307 ab = audit_log_start(audit_context(),
6308 GFP_ATOMIC,
6309 AUDIT_SELINUX_ERR);
Eric Parisd6ea83e2012-04-04 13:45:49 -04006310 audit_log_format(ab, "op=fscreate invalid_context=");
6311 audit_log_n_untrustedstring(ab, value, audit_size);
6312 audit_log_end(ab);
6313
Stephen Smalley12b29f32008-05-07 13:03:20 -04006314 return error;
Eric Parisd6ea83e2012-04-04 13:45:49 -04006315 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006316 error = security_context_to_sid_force(
6317 &selinux_state,
6318 value, size, &sid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04006319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006320 if (error)
6321 return error;
6322 }
6323
David Howellsd84f4f92008-11-14 10:39:23 +11006324 new = prepare_creds();
6325 if (!new)
6326 return -ENOMEM;
6327
Linus Torvalds1da177e2005-04-16 15:20:36 -07006328 /* Permission checking based on the specified context is
6329 performed during the actual operation (execve,
6330 open/mkdir/...), when we know the full context of the
David Howellsd84f4f92008-11-14 10:39:23 +11006331 operation. See selinux_bprm_set_creds for the execve
Linus Torvalds1da177e2005-04-16 15:20:36 -07006332 checks and may_create for the file creation checks. The
6333 operation will then fail if the context is not permitted. */
David Howellsd84f4f92008-11-14 10:39:23 +11006334 tsec = new->security;
6335 if (!strcmp(name, "exec")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006336 tsec->exec_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006337 } else if (!strcmp(name, "fscreate")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006338 tsec->create_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006339 } else if (!strcmp(name, "keycreate")) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006340 error = avc_has_perm(&selinux_state,
6341 mysid, sid, SECCLASS_KEY, KEY__CREATE,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006342 NULL);
Michael LeMay4eb582c2006-06-26 00:24:57 -07006343 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11006344 goto abort_change;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006345 tsec->keycreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006346 } else if (!strcmp(name, "sockcreate")) {
Eric Paris42c3e032006-06-26 00:26:03 -07006347 tsec->sockcreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006348 } else if (!strcmp(name, "current")) {
6349 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006350 if (sid == 0)
David Howellsd84f4f92008-11-14 10:39:23 +11006351 goto abort_change;
KaiGai Koheid9250de2008-08-28 16:35:57 +09006352
David Howellsd84f4f92008-11-14 10:39:23 +11006353 /* Only allow single threaded processes to change context */
6354 error = -EPERM;
Oleg Nesterov5bb459b2009-07-10 03:48:23 +02006355 if (!current_is_single_threaded()) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006356 error = security_bounded_transition(&selinux_state,
6357 tsec->sid, sid);
David Howellsd84f4f92008-11-14 10:39:23 +11006358 if (error)
6359 goto abort_change;
Eric Paris828dfe12008-04-17 13:17:49 -04006360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006361
6362 /* Check permissions for the transition. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006363 error = avc_has_perm(&selinux_state,
6364 tsec->sid, sid, SECCLASS_PROCESS,
Eric Paris828dfe12008-04-17 13:17:49 -04006365 PROCESS__DYNTRANSITION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006366 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11006367 goto abort_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006368
6369 /* Check for ptracing, and update the task SID if ok.
6370 Otherwise, leave SID unchanged and fail. */
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006371 ptsid = ptrace_parent_sid();
Paul Moore0c6181c2016-03-30 21:41:21 -04006372 if (ptsid != 0) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006373 error = avc_has_perm(&selinux_state,
6374 ptsid, sid, SECCLASS_PROCESS,
David Howellsd84f4f92008-11-14 10:39:23 +11006375 PROCESS__PTRACE, NULL);
6376 if (error)
6377 goto abort_change;
6378 }
6379
6380 tsec->sid = sid;
6381 } else {
6382 error = -EINVAL;
6383 goto abort_change;
6384 }
6385
6386 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006387 return size;
David Howellsd84f4f92008-11-14 10:39:23 +11006388
6389abort_change:
6390 abort_creds(new);
6391 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006392}
6393
David Quigley746df9b2013-05-22 12:50:35 -04006394static int selinux_ismaclabel(const char *name)
6395{
6396 return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0);
6397}
6398
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006399static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
6400{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006401 return security_sid_to_context(&selinux_state, secid,
6402 secdata, seclen);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006403}
6404
David Howells7bf570d2008-04-29 20:52:51 +01006405static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
David Howells63cb3442008-01-15 23:47:35 +00006406{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006407 return security_context_to_sid(&selinux_state, secdata, seclen,
6408 secid, GFP_KERNEL);
David Howells63cb3442008-01-15 23:47:35 +00006409}
6410
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006411static void selinux_release_secctx(char *secdata, u32 seclen)
6412{
Paul Moore088999e2007-08-01 11:12:58 -04006413 kfree(secdata);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006414}
6415
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006416static void selinux_inode_invalidate_secctx(struct inode *inode)
6417{
6418 struct inode_security_struct *isec = inode->i_security;
6419
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01006420 spin_lock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006421 isec->initialized = LABEL_INVALID;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01006422 spin_unlock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006423}
6424
David P. Quigley1ee65e32009-09-03 14:25:57 -04006425/*
6426 * called with inode->i_mutex locked
6427 */
6428static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
6429{
6430 return selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, ctx, ctxlen, 0);
6431}
6432
6433/*
6434 * called with inode->i_mutex locked
6435 */
6436static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
6437{
6438 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0);
6439}
6440
6441static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
6442{
6443 int len = 0;
6444 len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX,
6445 ctx, true);
6446 if (len < 0)
6447 return len;
6448 *ctxlen = len;
6449 return 0;
6450}
Michael LeMayd7200242006-06-22 14:47:17 -07006451#ifdef CONFIG_KEYS
6452
David Howellsd84f4f92008-11-14 10:39:23 +11006453static int selinux_key_alloc(struct key *k, const struct cred *cred,
David Howells7e047ef2006-06-26 00:24:50 -07006454 unsigned long flags)
Michael LeMayd7200242006-06-22 14:47:17 -07006455{
David Howellsd84f4f92008-11-14 10:39:23 +11006456 const struct task_security_struct *tsec;
Michael LeMayd7200242006-06-22 14:47:17 -07006457 struct key_security_struct *ksec;
6458
6459 ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
6460 if (!ksec)
6461 return -ENOMEM;
6462
David Howellsd84f4f92008-11-14 10:39:23 +11006463 tsec = cred->security;
6464 if (tsec->keycreate_sid)
6465 ksec->sid = tsec->keycreate_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006466 else
David Howellsd84f4f92008-11-14 10:39:23 +11006467 ksec->sid = tsec->sid;
Michael LeMayd7200242006-06-22 14:47:17 -07006468
David Howells275bb412008-11-14 10:39:19 +11006469 k->security = ksec;
Michael LeMayd7200242006-06-22 14:47:17 -07006470 return 0;
6471}
6472
6473static void selinux_key_free(struct key *k)
6474{
6475 struct key_security_struct *ksec = k->security;
6476
6477 k->security = NULL;
6478 kfree(ksec);
6479}
6480
6481static int selinux_key_permission(key_ref_t key_ref,
David Howellsd84f4f92008-11-14 10:39:23 +11006482 const struct cred *cred,
David Howellsf5895942014-03-14 17:44:49 +00006483 unsigned perm)
Michael LeMayd7200242006-06-22 14:47:17 -07006484{
6485 struct key *key;
Michael LeMayd7200242006-06-22 14:47:17 -07006486 struct key_security_struct *ksec;
David Howells275bb412008-11-14 10:39:19 +11006487 u32 sid;
Michael LeMayd7200242006-06-22 14:47:17 -07006488
6489 /* if no specific permissions are requested, we skip the
6490 permission check. No serious, additional covert channels
6491 appear to be created. */
6492 if (perm == 0)
6493 return 0;
6494
David Howellsd84f4f92008-11-14 10:39:23 +11006495 sid = cred_sid(cred);
David Howells275bb412008-11-14 10:39:19 +11006496
6497 key = key_ref_to_ptr(key_ref);
6498 ksec = key->security;
6499
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006500 return avc_has_perm(&selinux_state,
6501 sid, ksec->sid, SECCLASS_KEY, perm, NULL);
Michael LeMayd7200242006-06-22 14:47:17 -07006502}
6503
David Howells70a5bb72008-04-29 01:01:26 -07006504static int selinux_key_getsecurity(struct key *key, char **_buffer)
6505{
6506 struct key_security_struct *ksec = key->security;
6507 char *context = NULL;
6508 unsigned len;
6509 int rc;
6510
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006511 rc = security_sid_to_context(&selinux_state, ksec->sid,
6512 &context, &len);
David Howells70a5bb72008-04-29 01:01:26 -07006513 if (!rc)
6514 rc = len;
6515 *_buffer = context;
6516 return rc;
6517}
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006518#endif
David Howells70a5bb72008-04-29 01:01:26 -07006519
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006520#ifdef CONFIG_SECURITY_INFINIBAND
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006521static int selinux_ib_pkey_access(void *ib_sec, u64 subnet_prefix, u16 pkey_val)
6522{
6523 struct common_audit_data ad;
6524 int err;
6525 u32 sid = 0;
6526 struct ib_security_struct *sec = ib_sec;
6527 struct lsm_ibpkey_audit ibpkey;
6528
Daniel Jurgens409dcf32017-05-19 15:48:59 +03006529 err = sel_ib_pkey_sid(subnet_prefix, pkey_val, &sid);
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006530 if (err)
6531 return err;
6532
6533 ad.type = LSM_AUDIT_DATA_IBPKEY;
6534 ibpkey.subnet_prefix = subnet_prefix;
6535 ibpkey.pkey = pkey_val;
6536 ad.u.ibpkey = &ibpkey;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006537 return avc_has_perm(&selinux_state,
6538 sec->sid, sid,
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006539 SECCLASS_INFINIBAND_PKEY,
6540 INFINIBAND_PKEY__ACCESS, &ad);
6541}
6542
Daniel Jurgensab861df2017-05-19 15:48:58 +03006543static int selinux_ib_endport_manage_subnet(void *ib_sec, const char *dev_name,
6544 u8 port_num)
6545{
6546 struct common_audit_data ad;
6547 int err;
6548 u32 sid = 0;
6549 struct ib_security_struct *sec = ib_sec;
6550 struct lsm_ibendport_audit ibendport;
6551
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006552 err = security_ib_endport_sid(&selinux_state, dev_name, port_num,
6553 &sid);
Daniel Jurgensab861df2017-05-19 15:48:58 +03006554
6555 if (err)
6556 return err;
6557
6558 ad.type = LSM_AUDIT_DATA_IBENDPORT;
6559 strncpy(ibendport.dev_name, dev_name, sizeof(ibendport.dev_name));
6560 ibendport.port = port_num;
6561 ad.u.ibendport = &ibendport;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006562 return avc_has_perm(&selinux_state,
6563 sec->sid, sid,
Daniel Jurgensab861df2017-05-19 15:48:58 +03006564 SECCLASS_INFINIBAND_ENDPORT,
6565 INFINIBAND_ENDPORT__MANAGE_SUBNET, &ad);
6566}
6567
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006568static int selinux_ib_alloc_security(void **ib_sec)
6569{
6570 struct ib_security_struct *sec;
6571
6572 sec = kzalloc(sizeof(*sec), GFP_KERNEL);
6573 if (!sec)
6574 return -ENOMEM;
6575 sec->sid = current_sid();
6576
6577 *ib_sec = sec;
6578 return 0;
6579}
6580
6581static void selinux_ib_free_security(void *ib_sec)
6582{
6583 kfree(ib_sec);
6584}
Michael LeMayd7200242006-06-22 14:47:17 -07006585#endif
6586
Chenbo Fengec27c352017-10-18 13:00:25 -07006587#ifdef CONFIG_BPF_SYSCALL
6588static int selinux_bpf(int cmd, union bpf_attr *attr,
6589 unsigned int size)
6590{
6591 u32 sid = current_sid();
6592 int ret;
6593
6594 switch (cmd) {
6595 case BPF_MAP_CREATE:
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006596 ret = avc_has_perm(&selinux_state,
6597 sid, sid, SECCLASS_BPF, BPF__MAP_CREATE,
Chenbo Fengec27c352017-10-18 13:00:25 -07006598 NULL);
6599 break;
6600 case BPF_PROG_LOAD:
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006601 ret = avc_has_perm(&selinux_state,
6602 sid, sid, SECCLASS_BPF, BPF__PROG_LOAD,
Chenbo Fengec27c352017-10-18 13:00:25 -07006603 NULL);
6604 break;
6605 default:
6606 ret = 0;
6607 break;
6608 }
6609
6610 return ret;
6611}
6612
6613static u32 bpf_map_fmode_to_av(fmode_t fmode)
6614{
6615 u32 av = 0;
6616
6617 if (fmode & FMODE_READ)
6618 av |= BPF__MAP_READ;
6619 if (fmode & FMODE_WRITE)
6620 av |= BPF__MAP_WRITE;
6621 return av;
6622}
6623
Chenbo Fengf66e4482017-10-18 13:00:26 -07006624/* This function will check the file pass through unix socket or binder to see
6625 * if it is a bpf related object. And apply correspinding checks on the bpf
6626 * object based on the type. The bpf maps and programs, not like other files and
6627 * socket, are using a shared anonymous inode inside the kernel as their inode.
6628 * So checking that inode cannot identify if the process have privilege to
6629 * access the bpf object and that's why we have to add this additional check in
6630 * selinux_file_receive and selinux_binder_transfer_files.
6631 */
6632static int bpf_fd_pass(struct file *file, u32 sid)
6633{
6634 struct bpf_security_struct *bpfsec;
6635 struct bpf_prog *prog;
6636 struct bpf_map *map;
6637 int ret;
6638
6639 if (file->f_op == &bpf_map_fops) {
6640 map = file->private_data;
6641 bpfsec = map->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006642 ret = avc_has_perm(&selinux_state,
6643 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengf66e4482017-10-18 13:00:26 -07006644 bpf_map_fmode_to_av(file->f_mode), NULL);
6645 if (ret)
6646 return ret;
6647 } else if (file->f_op == &bpf_prog_fops) {
6648 prog = file->private_data;
6649 bpfsec = prog->aux->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006650 ret = avc_has_perm(&selinux_state,
6651 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengf66e4482017-10-18 13:00:26 -07006652 BPF__PROG_RUN, NULL);
6653 if (ret)
6654 return ret;
6655 }
6656 return 0;
6657}
6658
Chenbo Fengec27c352017-10-18 13:00:25 -07006659static int selinux_bpf_map(struct bpf_map *map, fmode_t fmode)
6660{
6661 u32 sid = current_sid();
6662 struct bpf_security_struct *bpfsec;
6663
6664 bpfsec = map->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006665 return avc_has_perm(&selinux_state,
6666 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengec27c352017-10-18 13:00:25 -07006667 bpf_map_fmode_to_av(fmode), NULL);
6668}
6669
6670static int selinux_bpf_prog(struct bpf_prog *prog)
6671{
6672 u32 sid = current_sid();
6673 struct bpf_security_struct *bpfsec;
6674
6675 bpfsec = prog->aux->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006676 return avc_has_perm(&selinux_state,
6677 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengec27c352017-10-18 13:00:25 -07006678 BPF__PROG_RUN, NULL);
6679}
6680
6681static int selinux_bpf_map_alloc(struct bpf_map *map)
6682{
6683 struct bpf_security_struct *bpfsec;
6684
6685 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6686 if (!bpfsec)
6687 return -ENOMEM;
6688
6689 bpfsec->sid = current_sid();
6690 map->security = bpfsec;
6691
6692 return 0;
6693}
6694
6695static void selinux_bpf_map_free(struct bpf_map *map)
6696{
6697 struct bpf_security_struct *bpfsec = map->security;
6698
6699 map->security = NULL;
6700 kfree(bpfsec);
6701}
6702
6703static int selinux_bpf_prog_alloc(struct bpf_prog_aux *aux)
6704{
6705 struct bpf_security_struct *bpfsec;
6706
6707 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6708 if (!bpfsec)
6709 return -ENOMEM;
6710
6711 bpfsec->sid = current_sid();
6712 aux->security = bpfsec;
6713
6714 return 0;
6715}
6716
6717static void selinux_bpf_prog_free(struct bpf_prog_aux *aux)
6718{
6719 struct bpf_security_struct *bpfsec = aux->security;
6720
6721 aux->security = NULL;
6722 kfree(bpfsec);
6723}
6724#endif
6725
James Morrisca97d932017-02-15 00:18:51 +11006726static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {
Casey Schauflere20b0432015-05-02 15:11:36 -07006727 LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr),
6728 LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction),
6729 LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder),
6730 LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file),
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02006731
Casey Schauflere20b0432015-05-02 15:11:36 -07006732 LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check),
6733 LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme),
6734 LSM_HOOK_INIT(capget, selinux_capget),
6735 LSM_HOOK_INIT(capset, selinux_capset),
6736 LSM_HOOK_INIT(capable, selinux_capable),
6737 LSM_HOOK_INIT(quotactl, selinux_quotactl),
6738 LSM_HOOK_INIT(quota_on, selinux_quota_on),
6739 LSM_HOOK_INIT(syslog, selinux_syslog),
6740 LSM_HOOK_INIT(vm_enough_memory, selinux_vm_enough_memory),
Stephen Smalley79af7302015-01-21 10:54:10 -05006741
Casey Schauflere20b0432015-05-02 15:11:36 -07006742 LSM_HOOK_INIT(netlink_send, selinux_netlink_send),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006743
Casey Schauflere20b0432015-05-02 15:11:36 -07006744 LSM_HOOK_INIT(bprm_set_creds, selinux_bprm_set_creds),
6745 LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds),
6746 LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006747
David Howells442155c2018-11-01 23:07:24 +00006748 LSM_HOOK_INIT(fs_context_parse_param, selinux_fs_context_parse_param),
6749
Casey Schauflere20b0432015-05-02 15:11:36 -07006750 LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security),
6751 LSM_HOOK_INIT(sb_free_security, selinux_sb_free_security),
Al Viro5b400232018-12-12 20:13:29 -05006752 LSM_HOOK_INIT(sb_eat_lsm_opts, selinux_sb_eat_lsm_opts),
Al Viro204cc0c2018-12-13 13:41:47 -05006753 LSM_HOOK_INIT(sb_free_mnt_opts, selinux_free_mnt_opts),
Casey Schauflere20b0432015-05-02 15:11:36 -07006754 LSM_HOOK_INIT(sb_remount, selinux_sb_remount),
6755 LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount),
6756 LSM_HOOK_INIT(sb_show_options, selinux_sb_show_options),
6757 LSM_HOOK_INIT(sb_statfs, selinux_sb_statfs),
6758 LSM_HOOK_INIT(sb_mount, selinux_mount),
6759 LSM_HOOK_INIT(sb_umount, selinux_umount),
6760 LSM_HOOK_INIT(sb_set_mnt_opts, selinux_set_mnt_opts),
6761 LSM_HOOK_INIT(sb_clone_mnt_opts, selinux_sb_clone_mnt_opts),
Al Viro757cbe52018-12-14 23:42:21 -05006762 LSM_HOOK_INIT(sb_add_mnt_opt, selinux_add_mnt_opt),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006763
Casey Schauflere20b0432015-05-02 15:11:36 -07006764 LSM_HOOK_INIT(dentry_init_security, selinux_dentry_init_security),
Vivek Goyala518b0a2016-07-13 10:44:53 -04006765 LSM_HOOK_INIT(dentry_create_files_as, selinux_dentry_create_files_as),
Eric Parise0007522008-03-05 10:31:54 -05006766
Casey Schauflere20b0432015-05-02 15:11:36 -07006767 LSM_HOOK_INIT(inode_alloc_security, selinux_inode_alloc_security),
6768 LSM_HOOK_INIT(inode_free_security, selinux_inode_free_security),
6769 LSM_HOOK_INIT(inode_init_security, selinux_inode_init_security),
6770 LSM_HOOK_INIT(inode_create, selinux_inode_create),
6771 LSM_HOOK_INIT(inode_link, selinux_inode_link),
6772 LSM_HOOK_INIT(inode_unlink, selinux_inode_unlink),
6773 LSM_HOOK_INIT(inode_symlink, selinux_inode_symlink),
6774 LSM_HOOK_INIT(inode_mkdir, selinux_inode_mkdir),
6775 LSM_HOOK_INIT(inode_rmdir, selinux_inode_rmdir),
6776 LSM_HOOK_INIT(inode_mknod, selinux_inode_mknod),
6777 LSM_HOOK_INIT(inode_rename, selinux_inode_rename),
6778 LSM_HOOK_INIT(inode_readlink, selinux_inode_readlink),
6779 LSM_HOOK_INIT(inode_follow_link, selinux_inode_follow_link),
6780 LSM_HOOK_INIT(inode_permission, selinux_inode_permission),
6781 LSM_HOOK_INIT(inode_setattr, selinux_inode_setattr),
6782 LSM_HOOK_INIT(inode_getattr, selinux_inode_getattr),
6783 LSM_HOOK_INIT(inode_setxattr, selinux_inode_setxattr),
6784 LSM_HOOK_INIT(inode_post_setxattr, selinux_inode_post_setxattr),
6785 LSM_HOOK_INIT(inode_getxattr, selinux_inode_getxattr),
6786 LSM_HOOK_INIT(inode_listxattr, selinux_inode_listxattr),
6787 LSM_HOOK_INIT(inode_removexattr, selinux_inode_removexattr),
6788 LSM_HOOK_INIT(inode_getsecurity, selinux_inode_getsecurity),
6789 LSM_HOOK_INIT(inode_setsecurity, selinux_inode_setsecurity),
6790 LSM_HOOK_INIT(inode_listsecurity, selinux_inode_listsecurity),
6791 LSM_HOOK_INIT(inode_getsecid, selinux_inode_getsecid),
Vivek Goyal56909eb2016-07-13 10:44:48 -04006792 LSM_HOOK_INIT(inode_copy_up, selinux_inode_copy_up),
Vivek Goyal19472b62016-07-13 10:44:50 -04006793 LSM_HOOK_INIT(inode_copy_up_xattr, selinux_inode_copy_up_xattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006794
Casey Schauflere20b0432015-05-02 15:11:36 -07006795 LSM_HOOK_INIT(file_permission, selinux_file_permission),
6796 LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security),
6797 LSM_HOOK_INIT(file_free_security, selinux_file_free_security),
6798 LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl),
6799 LSM_HOOK_INIT(mmap_file, selinux_mmap_file),
6800 LSM_HOOK_INIT(mmap_addr, selinux_mmap_addr),
6801 LSM_HOOK_INIT(file_mprotect, selinux_file_mprotect),
6802 LSM_HOOK_INIT(file_lock, selinux_file_lock),
6803 LSM_HOOK_INIT(file_fcntl, selinux_file_fcntl),
6804 LSM_HOOK_INIT(file_set_fowner, selinux_file_set_fowner),
6805 LSM_HOOK_INIT(file_send_sigiotask, selinux_file_send_sigiotask),
6806 LSM_HOOK_INIT(file_receive, selinux_file_receive),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006807
Casey Schauflere20b0432015-05-02 15:11:36 -07006808 LSM_HOOK_INIT(file_open, selinux_file_open),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006809
Tetsuo Handaa79be232017-03-28 23:08:45 +09006810 LSM_HOOK_INIT(task_alloc, selinux_task_alloc),
Casey Schauflere20b0432015-05-02 15:11:36 -07006811 LSM_HOOK_INIT(cred_alloc_blank, selinux_cred_alloc_blank),
6812 LSM_HOOK_INIT(cred_free, selinux_cred_free),
6813 LSM_HOOK_INIT(cred_prepare, selinux_cred_prepare),
6814 LSM_HOOK_INIT(cred_transfer, selinux_cred_transfer),
Matthew Garrett3ec30112018-01-08 13:36:19 -08006815 LSM_HOOK_INIT(cred_getsecid, selinux_cred_getsecid),
Casey Schauflere20b0432015-05-02 15:11:36 -07006816 LSM_HOOK_INIT(kernel_act_as, selinux_kernel_act_as),
6817 LSM_HOOK_INIT(kernel_create_files_as, selinux_kernel_create_files_as),
6818 LSM_HOOK_INIT(kernel_module_request, selinux_kernel_module_request),
Mimi Zoharc77b8cd2018-07-13 14:06:02 -04006819 LSM_HOOK_INIT(kernel_load_data, selinux_kernel_load_data),
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07006820 LSM_HOOK_INIT(kernel_read_file, selinux_kernel_read_file),
Casey Schauflere20b0432015-05-02 15:11:36 -07006821 LSM_HOOK_INIT(task_setpgid, selinux_task_setpgid),
6822 LSM_HOOK_INIT(task_getpgid, selinux_task_getpgid),
6823 LSM_HOOK_INIT(task_getsid, selinux_task_getsid),
6824 LSM_HOOK_INIT(task_getsecid, selinux_task_getsecid),
6825 LSM_HOOK_INIT(task_setnice, selinux_task_setnice),
6826 LSM_HOOK_INIT(task_setioprio, selinux_task_setioprio),
6827 LSM_HOOK_INIT(task_getioprio, selinux_task_getioprio),
Stephen Smalley791ec492017-02-17 07:57:00 -05006828 LSM_HOOK_INIT(task_prlimit, selinux_task_prlimit),
Casey Schauflere20b0432015-05-02 15:11:36 -07006829 LSM_HOOK_INIT(task_setrlimit, selinux_task_setrlimit),
6830 LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler),
6831 LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler),
6832 LSM_HOOK_INIT(task_movememory, selinux_task_movememory),
6833 LSM_HOOK_INIT(task_kill, selinux_task_kill),
Casey Schauflere20b0432015-05-02 15:11:36 -07006834 LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode),
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09006835
Casey Schauflere20b0432015-05-02 15:11:36 -07006836 LSM_HOOK_INIT(ipc_permission, selinux_ipc_permission),
6837 LSM_HOOK_INIT(ipc_getsecid, selinux_ipc_getsecid),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006838
Casey Schauflere20b0432015-05-02 15:11:36 -07006839 LSM_HOOK_INIT(msg_msg_alloc_security, selinux_msg_msg_alloc_security),
6840 LSM_HOOK_INIT(msg_msg_free_security, selinux_msg_msg_free_security),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006841
Casey Schauflere20b0432015-05-02 15:11:36 -07006842 LSM_HOOK_INIT(msg_queue_alloc_security,
6843 selinux_msg_queue_alloc_security),
6844 LSM_HOOK_INIT(msg_queue_free_security, selinux_msg_queue_free_security),
6845 LSM_HOOK_INIT(msg_queue_associate, selinux_msg_queue_associate),
6846 LSM_HOOK_INIT(msg_queue_msgctl, selinux_msg_queue_msgctl),
6847 LSM_HOOK_INIT(msg_queue_msgsnd, selinux_msg_queue_msgsnd),
6848 LSM_HOOK_INIT(msg_queue_msgrcv, selinux_msg_queue_msgrcv),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006849
Casey Schauflere20b0432015-05-02 15:11:36 -07006850 LSM_HOOK_INIT(shm_alloc_security, selinux_shm_alloc_security),
6851 LSM_HOOK_INIT(shm_free_security, selinux_shm_free_security),
6852 LSM_HOOK_INIT(shm_associate, selinux_shm_associate),
6853 LSM_HOOK_INIT(shm_shmctl, selinux_shm_shmctl),
6854 LSM_HOOK_INIT(shm_shmat, selinux_shm_shmat),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006855
Casey Schauflere20b0432015-05-02 15:11:36 -07006856 LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security),
6857 LSM_HOOK_INIT(sem_free_security, selinux_sem_free_security),
6858 LSM_HOOK_INIT(sem_associate, selinux_sem_associate),
6859 LSM_HOOK_INIT(sem_semctl, selinux_sem_semctl),
6860 LSM_HOOK_INIT(sem_semop, selinux_sem_semop),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006861
Casey Schauflere20b0432015-05-02 15:11:36 -07006862 LSM_HOOK_INIT(d_instantiate, selinux_d_instantiate),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006863
Casey Schauflere20b0432015-05-02 15:11:36 -07006864 LSM_HOOK_INIT(getprocattr, selinux_getprocattr),
6865 LSM_HOOK_INIT(setprocattr, selinux_setprocattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006866
Casey Schauflere20b0432015-05-02 15:11:36 -07006867 LSM_HOOK_INIT(ismaclabel, selinux_ismaclabel),
6868 LSM_HOOK_INIT(secid_to_secctx, selinux_secid_to_secctx),
6869 LSM_HOOK_INIT(secctx_to_secid, selinux_secctx_to_secid),
6870 LSM_HOOK_INIT(release_secctx, selinux_release_secctx),
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006871 LSM_HOOK_INIT(inode_invalidate_secctx, selinux_inode_invalidate_secctx),
Casey Schauflere20b0432015-05-02 15:11:36 -07006872 LSM_HOOK_INIT(inode_notifysecctx, selinux_inode_notifysecctx),
6873 LSM_HOOK_INIT(inode_setsecctx, selinux_inode_setsecctx),
6874 LSM_HOOK_INIT(inode_getsecctx, selinux_inode_getsecctx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006875
Casey Schauflere20b0432015-05-02 15:11:36 -07006876 LSM_HOOK_INIT(unix_stream_connect, selinux_socket_unix_stream_connect),
6877 LSM_HOOK_INIT(unix_may_send, selinux_socket_unix_may_send),
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006878
Casey Schauflere20b0432015-05-02 15:11:36 -07006879 LSM_HOOK_INIT(socket_create, selinux_socket_create),
6880 LSM_HOOK_INIT(socket_post_create, selinux_socket_post_create),
David Herrmann0b811db2018-05-04 16:28:21 +02006881 LSM_HOOK_INIT(socket_socketpair, selinux_socket_socketpair),
Casey Schauflere20b0432015-05-02 15:11:36 -07006882 LSM_HOOK_INIT(socket_bind, selinux_socket_bind),
6883 LSM_HOOK_INIT(socket_connect, selinux_socket_connect),
6884 LSM_HOOK_INIT(socket_listen, selinux_socket_listen),
6885 LSM_HOOK_INIT(socket_accept, selinux_socket_accept),
6886 LSM_HOOK_INIT(socket_sendmsg, selinux_socket_sendmsg),
6887 LSM_HOOK_INIT(socket_recvmsg, selinux_socket_recvmsg),
6888 LSM_HOOK_INIT(socket_getsockname, selinux_socket_getsockname),
6889 LSM_HOOK_INIT(socket_getpeername, selinux_socket_getpeername),
6890 LSM_HOOK_INIT(socket_getsockopt, selinux_socket_getsockopt),
6891 LSM_HOOK_INIT(socket_setsockopt, selinux_socket_setsockopt),
6892 LSM_HOOK_INIT(socket_shutdown, selinux_socket_shutdown),
6893 LSM_HOOK_INIT(socket_sock_rcv_skb, selinux_socket_sock_rcv_skb),
6894 LSM_HOOK_INIT(socket_getpeersec_stream,
6895 selinux_socket_getpeersec_stream),
6896 LSM_HOOK_INIT(socket_getpeersec_dgram, selinux_socket_getpeersec_dgram),
6897 LSM_HOOK_INIT(sk_alloc_security, selinux_sk_alloc_security),
6898 LSM_HOOK_INIT(sk_free_security, selinux_sk_free_security),
6899 LSM_HOOK_INIT(sk_clone_security, selinux_sk_clone_security),
6900 LSM_HOOK_INIT(sk_getsecid, selinux_sk_getsecid),
6901 LSM_HOOK_INIT(sock_graft, selinux_sock_graft),
Richard Hainesd4529302018-02-13 20:57:18 +00006902 LSM_HOOK_INIT(sctp_assoc_request, selinux_sctp_assoc_request),
6903 LSM_HOOK_INIT(sctp_sk_clone, selinux_sctp_sk_clone),
6904 LSM_HOOK_INIT(sctp_bind_connect, selinux_sctp_bind_connect),
Casey Schauflere20b0432015-05-02 15:11:36 -07006905 LSM_HOOK_INIT(inet_conn_request, selinux_inet_conn_request),
6906 LSM_HOOK_INIT(inet_csk_clone, selinux_inet_csk_clone),
6907 LSM_HOOK_INIT(inet_conn_established, selinux_inet_conn_established),
6908 LSM_HOOK_INIT(secmark_relabel_packet, selinux_secmark_relabel_packet),
6909 LSM_HOOK_INIT(secmark_refcount_inc, selinux_secmark_refcount_inc),
6910 LSM_HOOK_INIT(secmark_refcount_dec, selinux_secmark_refcount_dec),
6911 LSM_HOOK_INIT(req_classify_flow, selinux_req_classify_flow),
6912 LSM_HOOK_INIT(tun_dev_alloc_security, selinux_tun_dev_alloc_security),
6913 LSM_HOOK_INIT(tun_dev_free_security, selinux_tun_dev_free_security),
6914 LSM_HOOK_INIT(tun_dev_create, selinux_tun_dev_create),
6915 LSM_HOOK_INIT(tun_dev_attach_queue, selinux_tun_dev_attach_queue),
6916 LSM_HOOK_INIT(tun_dev_attach, selinux_tun_dev_attach),
6917 LSM_HOOK_INIT(tun_dev_open, selinux_tun_dev_open),
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006918#ifdef CONFIG_SECURITY_INFINIBAND
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006919 LSM_HOOK_INIT(ib_pkey_access, selinux_ib_pkey_access),
Daniel Jurgensab861df2017-05-19 15:48:58 +03006920 LSM_HOOK_INIT(ib_endport_manage_subnet,
6921 selinux_ib_endport_manage_subnet),
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006922 LSM_HOOK_INIT(ib_alloc_security, selinux_ib_alloc_security),
6923 LSM_HOOK_INIT(ib_free_security, selinux_ib_free_security),
6924#endif
Trent Jaegerd28d1e02005-12-13 23:12:40 -08006925#ifdef CONFIG_SECURITY_NETWORK_XFRM
Casey Schauflere20b0432015-05-02 15:11:36 -07006926 LSM_HOOK_INIT(xfrm_policy_alloc_security, selinux_xfrm_policy_alloc),
6927 LSM_HOOK_INIT(xfrm_policy_clone_security, selinux_xfrm_policy_clone),
6928 LSM_HOOK_INIT(xfrm_policy_free_security, selinux_xfrm_policy_free),
6929 LSM_HOOK_INIT(xfrm_policy_delete_security, selinux_xfrm_policy_delete),
6930 LSM_HOOK_INIT(xfrm_state_alloc, selinux_xfrm_state_alloc),
6931 LSM_HOOK_INIT(xfrm_state_alloc_acquire,
6932 selinux_xfrm_state_alloc_acquire),
6933 LSM_HOOK_INIT(xfrm_state_free_security, selinux_xfrm_state_free),
6934 LSM_HOOK_INIT(xfrm_state_delete_security, selinux_xfrm_state_delete),
6935 LSM_HOOK_INIT(xfrm_policy_lookup, selinux_xfrm_policy_lookup),
6936 LSM_HOOK_INIT(xfrm_state_pol_flow_match,
6937 selinux_xfrm_state_pol_flow_match),
6938 LSM_HOOK_INIT(xfrm_decode_session, selinux_xfrm_decode_session),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006939#endif
Michael LeMayd7200242006-06-22 14:47:17 -07006940
6941#ifdef CONFIG_KEYS
Casey Schauflere20b0432015-05-02 15:11:36 -07006942 LSM_HOOK_INIT(key_alloc, selinux_key_alloc),
6943 LSM_HOOK_INIT(key_free, selinux_key_free),
6944 LSM_HOOK_INIT(key_permission, selinux_key_permission),
6945 LSM_HOOK_INIT(key_getsecurity, selinux_key_getsecurity),
Michael LeMayd7200242006-06-22 14:47:17 -07006946#endif
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02006947
6948#ifdef CONFIG_AUDIT
Casey Schauflere20b0432015-05-02 15:11:36 -07006949 LSM_HOOK_INIT(audit_rule_init, selinux_audit_rule_init),
6950 LSM_HOOK_INIT(audit_rule_known, selinux_audit_rule_known),
6951 LSM_HOOK_INIT(audit_rule_match, selinux_audit_rule_match),
6952 LSM_HOOK_INIT(audit_rule_free, selinux_audit_rule_free),
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02006953#endif
Chenbo Fengec27c352017-10-18 13:00:25 -07006954
6955#ifdef CONFIG_BPF_SYSCALL
6956 LSM_HOOK_INIT(bpf, selinux_bpf),
6957 LSM_HOOK_INIT(bpf_map, selinux_bpf_map),
6958 LSM_HOOK_INIT(bpf_prog, selinux_bpf_prog),
6959 LSM_HOOK_INIT(bpf_map_alloc_security, selinux_bpf_map_alloc),
6960 LSM_HOOK_INIT(bpf_prog_alloc_security, selinux_bpf_prog_alloc),
6961 LSM_HOOK_INIT(bpf_map_free_security, selinux_bpf_map_free),
6962 LSM_HOOK_INIT(bpf_prog_free_security, selinux_bpf_prog_free),
6963#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006964};
6965
6966static __init int selinux_init(void)
6967{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07006968 if (!security_module_enable("selinux")) {
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02006969 selinux_enabled = 0;
6970 return 0;
6971 }
6972
Linus Torvalds1da177e2005-04-16 15:20:36 -07006973 if (!selinux_enabled) {
peter enderborgc103a912018-06-12 10:09:03 +02006974 pr_info("SELinux: Disabled at boot.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006975 return 0;
6976 }
6977
peter enderborgc103a912018-06-12 10:09:03 +02006978 pr_info("SELinux: Initializing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006979
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006980 memset(&selinux_state, 0, sizeof(selinux_state));
Paul Mooree5a5ca92018-03-01 17:38:30 -05006981 enforcing_set(&selinux_state, selinux_enforcing_boot);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006982 selinux_state.checkreqprot = selinux_checkreqprot_boot;
6983 selinux_ss_init(&selinux_state.ss);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006984 selinux_avc_init(&selinux_state.avc);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006985
Linus Torvalds1da177e2005-04-16 15:20:36 -07006986 /* Set the security state for the initial task. */
David Howellsd84f4f92008-11-14 10:39:23 +11006987 cred_init_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006988
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04006989 default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
6990
James Morris7cae7e22006-03-22 00:09:22 -08006991 sel_inode_cache = kmem_cache_create("selinux_inode_security",
6992 sizeof(struct inode_security_struct),
Paul Mundt20c2df82007-07-20 10:11:58 +09006993 0, SLAB_PANIC, NULL);
Sangwoo63205652015-10-21 17:44:30 -04006994 file_security_cache = kmem_cache_create("selinux_file_security",
6995 sizeof(struct file_security_struct),
6996 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006997 avc_init();
6998
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006999 avtab_cache_init();
7000
7001 ebitmap_cache_init();
7002
7003 hashtab_cache_init();
7004
Casey Schauflerd69dece52017-01-18 17:09:05 -08007005 security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks), "selinux");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007006
Paul Moore615e51f2014-06-26 14:33:56 -04007007 if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET))
7008 panic("SELinux: Unable to register AVC netcache callback\n");
7009
Daniel Jurgens8f408ab2017-05-19 15:48:53 +03007010 if (avc_add_callback(selinux_lsm_notifier_avc_callback, AVC_CALLBACK_RESET))
7011 panic("SELinux: Unable to register AVC LSM notifier callback\n");
7012
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007013 if (selinux_enforcing_boot)
peter enderborgc103a912018-06-12 10:09:03 +02007014 pr_debug("SELinux: Starting in enforcing mode\n");
Eric Paris828dfe12008-04-17 13:17:49 -04007015 else
peter enderborgc103a912018-06-12 10:09:03 +02007016 pr_debug("SELinux: Starting in permissive mode\n");
Michael LeMayd7200242006-06-22 14:47:17 -07007017
David Howells442155c2018-11-01 23:07:24 +00007018 fs_validate_description(&selinux_fs_parameters);
7019
Linus Torvalds1da177e2005-04-16 15:20:36 -07007020 return 0;
7021}
7022
Al Viroe8c26252010-03-23 06:36:54 -04007023static void delayed_superblock_init(struct super_block *sb, void *unused)
7024{
Al Viro204cc0c2018-12-13 13:41:47 -05007025 selinux_set_mnt_opts(sb, NULL, 0, NULL);
Al Viroe8c26252010-03-23 06:36:54 -04007026}
7027
Linus Torvalds1da177e2005-04-16 15:20:36 -07007028void selinux_complete_init(void)
7029{
peter enderborgc103a912018-06-12 10:09:03 +02007030 pr_debug("SELinux: Completing initialization.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007031
7032 /* Set up any superblocks initialized prior to the policy load. */
peter enderborgc103a912018-06-12 10:09:03 +02007033 pr_debug("SELinux: Setting up existing superblocks.\n");
Al Viroe8c26252010-03-23 06:36:54 -04007034 iterate_supers(delayed_superblock_init, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007035}
7036
7037/* SELinux requires early initialization in order to label
7038 all processes and objects when they are created. */
Kees Cook3d6e5f62018-10-10 17:18:23 -07007039DEFINE_LSM(selinux) = {
Kees Cook07aed2f2018-10-10 17:18:24 -07007040 .name = "selinux",
Kees Cook3d6e5f62018-10-10 17:18:23 -07007041 .init = selinux_init,
7042};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007043
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007044#if defined(CONFIG_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007045
Florian Westphal591bb272017-07-26 11:40:52 +02007046static const struct nf_hook_ops selinux_nf_ops[] = {
Paul Mooreeffad8d2008-01-29 08:49:27 -05007047 {
7048 .hook = selinux_ipv4_postroute,
Alban Crequy2597a832012-05-14 03:56:39 +00007049 .pf = NFPROTO_IPV4,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007050 .hooknum = NF_INET_POST_ROUTING,
7051 .priority = NF_IP_PRI_SELINUX_LAST,
7052 },
7053 {
7054 .hook = selinux_ipv4_forward,
Alban Crequy2597a832012-05-14 03:56:39 +00007055 .pf = NFPROTO_IPV4,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007056 .hooknum = NF_INET_FORWARD,
7057 .priority = NF_IP_PRI_SELINUX_FIRST,
Paul Moore948bf852008-10-10 10:16:32 -04007058 },
7059 {
7060 .hook = selinux_ipv4_output,
Alban Crequy2597a832012-05-14 03:56:39 +00007061 .pf = NFPROTO_IPV4,
Paul Moore948bf852008-10-10 10:16:32 -04007062 .hooknum = NF_INET_LOCAL_OUT,
7063 .priority = NF_IP_PRI_SELINUX_FIRST,
Jiri Pirko25db6be2014-09-03 17:42:13 +02007064 },
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04007065#if IS_ENABLED(CONFIG_IPV6)
Paul Mooreeffad8d2008-01-29 08:49:27 -05007066 {
7067 .hook = selinux_ipv6_postroute,
Alban Crequy2597a832012-05-14 03:56:39 +00007068 .pf = NFPROTO_IPV6,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007069 .hooknum = NF_INET_POST_ROUTING,
7070 .priority = NF_IP6_PRI_SELINUX_LAST,
7071 },
7072 {
7073 .hook = selinux_ipv6_forward,
Alban Crequy2597a832012-05-14 03:56:39 +00007074 .pf = NFPROTO_IPV6,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007075 .hooknum = NF_INET_FORWARD,
7076 .priority = NF_IP6_PRI_SELINUX_FIRST,
Jiri Pirko25db6be2014-09-03 17:42:13 +02007077 },
Huw Davies2917f572016-06-27 15:06:15 -04007078 {
7079 .hook = selinux_ipv6_output,
7080 .pf = NFPROTO_IPV6,
7081 .hooknum = NF_INET_LOCAL_OUT,
7082 .priority = NF_IP6_PRI_SELINUX_FIRST,
7083 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07007084#endif /* IPV6 */
Jiri Pirko25db6be2014-09-03 17:42:13 +02007085};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007086
Florian Westphal8e71bf72017-04-21 11:49:09 +02007087static int __net_init selinux_nf_register(struct net *net)
7088{
7089 return nf_register_net_hooks(net, selinux_nf_ops,
7090 ARRAY_SIZE(selinux_nf_ops));
7091}
7092
7093static void __net_exit selinux_nf_unregister(struct net *net)
7094{
7095 nf_unregister_net_hooks(net, selinux_nf_ops,
7096 ARRAY_SIZE(selinux_nf_ops));
7097}
7098
7099static struct pernet_operations selinux_net_ops = {
7100 .init = selinux_nf_register,
7101 .exit = selinux_nf_unregister,
7102};
7103
Linus Torvalds1da177e2005-04-16 15:20:36 -07007104static int __init selinux_nf_ip_init(void)
7105{
Jiri Pirko25db6be2014-09-03 17:42:13 +02007106 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007107
7108 if (!selinux_enabled)
Jiri Pirko25db6be2014-09-03 17:42:13 +02007109 return 0;
Eric Parisfadcdb42007-02-22 18:11:31 -05007110
peter enderborgc103a912018-06-12 10:09:03 +02007111 pr_debug("SELinux: Registering netfilter hooks\n");
Eric Parisfadcdb42007-02-22 18:11:31 -05007112
Florian Westphal8e71bf72017-04-21 11:49:09 +02007113 err = register_pernet_subsys(&selinux_net_ops);
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07007114 if (err)
Florian Westphal8e71bf72017-04-21 11:49:09 +02007115 panic("SELinux: register_pernet_subsys: error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007116
Jiri Pirko25db6be2014-09-03 17:42:13 +02007117 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007118}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007119__initcall(selinux_nf_ip_init);
7120
7121#ifdef CONFIG_SECURITY_SELINUX_DISABLE
7122static void selinux_nf_ip_exit(void)
7123{
peter enderborgc103a912018-06-12 10:09:03 +02007124 pr_debug("SELinux: Unregistering netfilter hooks\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007125
Florian Westphal8e71bf72017-04-21 11:49:09 +02007126 unregister_pernet_subsys(&selinux_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007127}
7128#endif
7129
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007130#else /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007131
7132#ifdef CONFIG_SECURITY_SELINUX_DISABLE
7133#define selinux_nf_ip_exit()
7134#endif
7135
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007136#endif /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007137
7138#ifdef CONFIG_SECURITY_SELINUX_DISABLE
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007139int selinux_disable(struct selinux_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007140{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007141 if (state->initialized) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007142 /* Not permitted after initial policy load. */
7143 return -EINVAL;
7144 }
7145
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007146 if (state->disabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007147 /* Only do this once. */
7148 return -EINVAL;
7149 }
7150
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007151 state->disabled = 1;
7152
peter enderborgc103a912018-06-12 10:09:03 +02007153 pr_info("SELinux: Disabled at runtime.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007154
Stephen Smalley30d55282006-05-03 10:52:36 -04007155 selinux_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007156
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07007157 security_delete_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007158
Eric Parisaf8ff042009-09-20 21:23:01 -04007159 /* Try to destroy the avc node cache */
7160 avc_disable();
7161
Linus Torvalds1da177e2005-04-16 15:20:36 -07007162 /* Unregister netfilter hooks. */
7163 selinux_nf_ip_exit();
7164
7165 /* Unregister selinuxfs. */
7166 exit_sel_fs();
7167
7168 return 0;
7169}
7170#endif