blob: 4ba83de5fa800595d0bac47bd3076cb0ceb6fc01 [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
Al Viro0b520752018-12-23 16:02:47 -05002767static int selinux_fs_context_dup(struct fs_context *fc,
2768 struct fs_context *src_fc)
2769{
2770 const struct selinux_mnt_opts *src = src_fc->security;
2771 struct selinux_mnt_opts *opts;
2772
2773 if (!src)
2774 return 0;
2775
2776 fc->security = kzalloc(sizeof(struct selinux_mnt_opts), GFP_KERNEL);
2777 if (!fc->security)
2778 return -ENOMEM;
2779
2780 opts = fc->security;
2781
2782 if (src->fscontext) {
2783 opts->fscontext = kstrdup(src->fscontext, GFP_KERNEL);
2784 if (!opts->fscontext)
2785 return -ENOMEM;
2786 }
2787 if (src->context) {
2788 opts->context = kstrdup(src->context, GFP_KERNEL);
2789 if (!opts->context)
2790 return -ENOMEM;
2791 }
2792 if (src->rootcontext) {
2793 opts->rootcontext = kstrdup(src->rootcontext, GFP_KERNEL);
2794 if (!opts->rootcontext)
2795 return -ENOMEM;
2796 }
2797 if (src->defcontext) {
2798 opts->defcontext = kstrdup(src->defcontext, GFP_KERNEL);
2799 if (!opts->defcontext)
2800 return -ENOMEM;
2801 }
2802 return 0;
2803}
2804
David Howells442155c2018-11-01 23:07:24 +00002805static const struct fs_parameter_spec selinux_param_specs[] = {
2806 fsparam_string(CONTEXT_STR, Opt_context),
2807 fsparam_string(DEFCONTEXT_STR, Opt_defcontext),
2808 fsparam_string(FSCONTEXT_STR, Opt_fscontext),
2809 fsparam_string(ROOTCONTEXT_STR, Opt_rootcontext),
2810 fsparam_flag (SECLABEL_STR, Opt_seclabel),
2811 {}
2812};
2813
2814static const struct fs_parameter_description selinux_fs_parameters = {
2815 .name = "SELinux",
2816 .specs = selinux_param_specs,
2817};
2818
2819static int selinux_fs_context_parse_param(struct fs_context *fc,
2820 struct fs_parameter *param)
2821{
2822 struct fs_parse_result result;
2823 int opt, rc;
2824
2825 opt = fs_parse(fc, &selinux_fs_parameters, param, &result);
2826 if (opt < 0)
2827 return opt;
2828
2829 rc = selinux_add_opt(opt, param->string, &fc->security);
2830 if (!rc) {
2831 param->string = NULL;
2832 rc = 1;
2833 }
2834 return rc;
2835}
2836
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837/* inode security operations */
2838
2839static int selinux_inode_alloc_security(struct inode *inode)
2840{
2841 return inode_alloc_security(inode);
2842}
2843
2844static void selinux_inode_free_security(struct inode *inode)
2845{
2846 inode_free_security(inode);
2847}
2848
David Quigleyd47be3d2013-05-22 12:50:34 -04002849static int selinux_dentry_init_security(struct dentry *dentry, int mode,
Al Viro4f3ccd72016-07-20 16:06:15 -04002850 const struct qstr *name, void **ctx,
David Quigleyd47be3d2013-05-22 12:50:34 -04002851 u32 *ctxlen)
2852{
David Quigleyd47be3d2013-05-22 12:50:34 -04002853 u32 newsid;
2854 int rc;
2855
Vivek Goyalc957f6d2016-07-13 10:44:51 -04002856 rc = selinux_determine_inode_label(current_security(),
2857 d_inode(dentry->d_parent), name,
David Howellsc3c188b2015-07-10 17:19:58 -04002858 inode_mode_to_security_class(mode),
2859 &newsid);
2860 if (rc)
2861 return rc;
David Quigleyd47be3d2013-05-22 12:50:34 -04002862
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002863 return security_sid_to_context(&selinux_state, newsid, (char **)ctx,
2864 ctxlen);
David Quigleyd47be3d2013-05-22 12:50:34 -04002865}
2866
Vivek Goyala518b0a2016-07-13 10:44:53 -04002867static int selinux_dentry_create_files_as(struct dentry *dentry, int mode,
2868 struct qstr *name,
2869 const struct cred *old,
2870 struct cred *new)
2871{
2872 u32 newsid;
2873 int rc;
2874 struct task_security_struct *tsec;
2875
2876 rc = selinux_determine_inode_label(old->security,
2877 d_inode(dentry->d_parent), name,
2878 inode_mode_to_security_class(mode),
2879 &newsid);
2880 if (rc)
2881 return rc;
2882
2883 tsec = new->security;
2884 tsec->create_sid = newsid;
2885 return 0;
2886}
2887
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002888static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +09002889 const struct qstr *qstr,
2890 const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -05002891 void **value, size_t *len)
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002892{
Paul Moore5fb49872010-04-22 14:46:19 -04002893 const struct task_security_struct *tsec = current_security();
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002894 struct superblock_security_struct *sbsec;
Corentin LABBEc0d4f462017-10-04 20:32:17 +02002895 u32 newsid, clen;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002896 int rc;
Tetsuo Handa95489062013-07-25 05:44:02 +09002897 char *context;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002898
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002899 sbsec = dir->i_sb->s_security;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002900
David Howells275bb412008-11-14 10:39:19 +11002901 newsid = tsec->create_sid;
2902
Vivek Goyalc957f6d2016-07-13 10:44:51 -04002903 rc = selinux_determine_inode_label(current_security(),
David Howellsc3c188b2015-07-10 17:19:58 -04002904 dir, qstr,
2905 inode_mode_to_security_class(inode->i_mode),
2906 &newsid);
2907 if (rc)
2908 return rc;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002909
Eric Paris296fddf2006-09-25 23:32:00 -07002910 /* Possibly defer initialization to selinux_complete_init. */
David P. Quigley0d90a7e2009-01-16 09:22:02 -05002911 if (sbsec->flags & SE_SBINITIALIZED) {
Eric Paris296fddf2006-09-25 23:32:00 -07002912 struct inode_security_struct *isec = inode->i_security;
2913 isec->sclass = inode_mode_to_security_class(inode->i_mode);
2914 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05002915 isec->initialized = LABEL_INITIALIZED;
Eric Paris296fddf2006-09-25 23:32:00 -07002916 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002917
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002918 if (!selinux_state.initialized || !(sbsec->flags & SBLABEL_MNT))
Stephen Smalley25a74f32005-11-08 21:34:33 -08002919 return -EOPNOTSUPP;
2920
Tetsuo Handa95489062013-07-25 05:44:02 +09002921 if (name)
2922 *name = XATTR_SELINUX_SUFFIX;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002923
2924 if (value && len) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002925 rc = security_sid_to_context_force(&selinux_state, newsid,
2926 &context, &clen);
Tetsuo Handa95489062013-07-25 05:44:02 +09002927 if (rc)
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002928 return rc;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002929 *value = context;
2930 *len = clen;
2931 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002932
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002933 return 0;
2934}
2935
Al Viro4acdaf22011-07-26 01:42:34 -04002936static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937{
2938 return may_create(dir, dentry, SECCLASS_FILE);
2939}
2940
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2942{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 return may_link(dir, old_dentry, MAY_LINK);
2944}
2945
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
2947{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 return may_link(dir, dentry, MAY_UNLINK);
2949}
2950
2951static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
2952{
2953 return may_create(dir, dentry, SECCLASS_LNK_FILE);
2954}
2955
Al Viro18bb1db2011-07-26 01:41:39 -04002956static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957{
2958 return may_create(dir, dentry, SECCLASS_DIR);
2959}
2960
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
2962{
2963 return may_link(dir, dentry, MAY_RMDIR);
2964}
2965
Al Viro1a67aaf2011-07-26 01:52:52 -04002966static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 return may_create(dir, dentry, inode_mode_to_security_class(mode));
2969}
2970
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
Eric Paris828dfe12008-04-17 13:17:49 -04002972 struct inode *new_inode, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973{
2974 return may_rename(old_inode, old_dentry, new_inode, new_dentry);
2975}
2976
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977static int selinux_inode_readlink(struct dentry *dentry)
2978{
David Howells88e67f32008-11-14 10:39:21 +11002979 const struct cred *cred = current_cred();
2980
Eric Paris2875fa02011-04-28 16:04:24 -04002981 return dentry_has_perm(cred, dentry, FILE__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982}
2983
NeilBrownbda0be72015-03-23 13:37:39 +11002984static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode,
2985 bool rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986{
David Howells88e67f32008-11-14 10:39:21 +11002987 const struct cred *cred = current_cred();
NeilBrownbda0be72015-03-23 13:37:39 +11002988 struct common_audit_data ad;
2989 struct inode_security_struct *isec;
2990 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991
NeilBrownbda0be72015-03-23 13:37:39 +11002992 validate_creds(cred);
2993
2994 ad.type = LSM_AUDIT_DATA_DENTRY;
2995 ad.u.dentry = dentry;
2996 sid = cred_sid(cred);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05002997 isec = inode_security_rcu(inode, rcu);
2998 if (IS_ERR(isec))
2999 return PTR_ERR(isec);
NeilBrownbda0be72015-03-23 13:37:39 +11003000
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003001 return avc_has_perm_flags(&selinux_state,
3002 sid, isec->sid, isec->sclass, FILE__READ, &ad,
NeilBrownbda0be72015-03-23 13:37:39 +11003003 rcu ? MAY_NOT_BLOCK : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004}
3005
Eric Parisd4cf970d2012-04-04 15:01:42 -04003006static noinline int audit_inode_permission(struct inode *inode,
3007 u32 perms, u32 audited, u32 denied,
Stephen Smalley626b9742014-04-29 11:29:04 -07003008 int result,
Eric Parisd4cf970d2012-04-04 15:01:42 -04003009 unsigned flags)
3010{
3011 struct common_audit_data ad;
Eric Parisd4cf970d2012-04-04 15:01:42 -04003012 struct inode_security_struct *isec = inode->i_security;
3013 int rc;
3014
Eric Paris50c205f2012-04-04 15:01:43 -04003015 ad.type = LSM_AUDIT_DATA_INODE;
Eric Parisd4cf970d2012-04-04 15:01:42 -04003016 ad.u.inode = inode;
3017
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003018 rc = slow_avc_audit(&selinux_state,
3019 current_sid(), isec->sid, isec->sclass, perms,
Stephen Smalley626b9742014-04-29 11:29:04 -07003020 audited, denied, result, &ad, flags);
Eric Parisd4cf970d2012-04-04 15:01:42 -04003021 if (rc)
3022 return rc;
3023 return 0;
3024}
3025
Al Viroe74f71e2011-06-20 19:38:15 -04003026static int selinux_inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027{
David Howells88e67f32008-11-14 10:39:21 +11003028 const struct cred *cred = current_cred();
Eric Parisb782e0a2010-07-23 11:44:03 -04003029 u32 perms;
3030 bool from_access;
Al Virocf1dd1d2011-06-20 19:44:08 -04003031 unsigned flags = mask & MAY_NOT_BLOCK;
Eric Paris2e334052012-04-04 15:01:42 -04003032 struct inode_security_struct *isec;
3033 u32 sid;
3034 struct av_decision avd;
3035 int rc, rc2;
3036 u32 audited, denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037
Eric Parisb782e0a2010-07-23 11:44:03 -04003038 from_access = mask & MAY_ACCESS;
Eric Parisd09ca732010-07-23 11:43:57 -04003039 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
3040
Eric Parisb782e0a2010-07-23 11:44:03 -04003041 /* No permission to check. Existence test. */
3042 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044
Eric Paris2e334052012-04-04 15:01:42 -04003045 validate_creds(cred);
Eric Parisb782e0a2010-07-23 11:44:03 -04003046
Eric Paris2e334052012-04-04 15:01:42 -04003047 if (unlikely(IS_PRIVATE(inode)))
3048 return 0;
Eric Parisb782e0a2010-07-23 11:44:03 -04003049
3050 perms = file_mask_to_av(inode->i_mode, mask);
3051
Eric Paris2e334052012-04-04 15:01:42 -04003052 sid = cred_sid(cred);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05003053 isec = inode_security_rcu(inode, flags & MAY_NOT_BLOCK);
3054 if (IS_ERR(isec))
3055 return PTR_ERR(isec);
Eric Paris2e334052012-04-04 15:01:42 -04003056
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003057 rc = avc_has_perm_noaudit(&selinux_state,
3058 sid, isec->sid, isec->sclass, perms, 0, &avd);
Eric Paris2e334052012-04-04 15:01:42 -04003059 audited = avc_audit_required(perms, &avd, rc,
3060 from_access ? FILE__AUDIT_ACCESS : 0,
3061 &denied);
3062 if (likely(!audited))
3063 return rc;
3064
Stephen Smalley626b9742014-04-29 11:29:04 -07003065 rc2 = audit_inode_permission(inode, perms, audited, denied, rc, flags);
Eric Paris2e334052012-04-04 15:01:42 -04003066 if (rc2)
3067 return rc2;
3068 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069}
3070
3071static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
3072{
David Howells88e67f32008-11-14 10:39:21 +11003073 const struct cred *cred = current_cred();
Stephen Smalleyccb54472017-05-12 12:41:24 -04003074 struct inode *inode = d_backing_inode(dentry);
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003075 unsigned int ia_valid = iattr->ia_valid;
Eric Paris95dbf732012-04-04 13:45:34 -04003076 __u32 av = FILE__WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003078 /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
3079 if (ia_valid & ATTR_FORCE) {
3080 ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
3081 ATTR_FORCE);
3082 if (!ia_valid)
3083 return 0;
3084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003086 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
3087 ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
Eric Paris2875fa02011-04-28 16:04:24 -04003088 return dentry_has_perm(cred, dentry, FILE__SETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003090 if (selinux_policycap_openperm() &&
Stephen Smalleyccb54472017-05-12 12:41:24 -04003091 inode->i_sb->s_magic != SOCKFS_MAGIC &&
3092 (ia_valid & ATTR_SIZE) &&
3093 !(ia_valid & ATTR_FILE))
Eric Paris95dbf732012-04-04 13:45:34 -04003094 av |= FILE__OPEN;
3095
3096 return dentry_has_perm(cred, dentry, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097}
3098
Al Viro3f7036a2015-03-08 19:28:30 -04003099static int selinux_inode_getattr(const struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100{
Al Viro3f7036a2015-03-08 19:28:30 -04003101 return path_has_perm(current_cred(), path, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102}
3103
Stephen Smalleydb590002017-04-20 11:31:30 -04003104static bool has_cap_mac_admin(bool audit)
3105{
3106 const struct cred *cred = current_cred();
3107 int cap_audit = audit ? SECURITY_CAP_AUDIT : SECURITY_CAP_NOAUDIT;
3108
3109 if (cap_capable(cred, &init_user_ns, CAP_MAC_ADMIN, cap_audit))
3110 return false;
3111 if (cred_has_capability(cred, CAP_MAC_ADMIN, cap_audit, true))
3112 return false;
3113 return true;
3114}
3115
David Howells8f0cfa52008-04-29 00:59:41 -07003116static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
3117 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118{
David Howellsc6f493d2015-03-17 22:26:22 +00003119 struct inode *inode = d_backing_inode(dentry);
Paul Moore20cdef82016-04-04 14:14:42 -04003120 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 struct superblock_security_struct *sbsec;
Thomas Liu2bf49692009-07-14 12:14:09 -04003122 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11003123 u32 newsid, sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 int rc = 0;
3125
Eric W. Biederman6b240302017-10-02 09:38:20 -05003126 if (strcmp(name, XATTR_NAME_SELINUX)) {
3127 rc = cap_inode_setxattr(dentry, name, value, size, flags);
3128 if (rc)
3129 return rc;
3130
3131 /* Not an attribute we recognize, so just check the
3132 ordinary setattr permission. */
3133 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135
3136 sbsec = inode->i_sb->s_security;
Eric Paris12f348b2012-10-09 10:56:25 -04003137 if (!(sbsec->flags & SBLABEL_MNT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 return -EOPNOTSUPP;
3139
Serge E. Hallyn2e149672011-03-23 16:43:26 -07003140 if (!inode_owner_or_capable(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 return -EPERM;
3142
Eric Paris50c205f2012-04-04 15:01:43 -04003143 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04003144 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145
Paul Moore20cdef82016-04-04 14:14:42 -04003146 isec = backing_inode_security(dentry);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003147 rc = avc_has_perm(&selinux_state,
3148 sid, isec->sid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 FILE__RELABELFROM, &ad);
3150 if (rc)
3151 return rc;
3152
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003153 rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3154 GFP_KERNEL);
Stephen Smalley12b29f32008-05-07 13:03:20 -04003155 if (rc == -EINVAL) {
Stephen Smalleydb590002017-04-20 11:31:30 -04003156 if (!has_cap_mac_admin(true)) {
Eric Parisd6ea83e2012-04-04 13:45:49 -04003157 struct audit_buffer *ab;
3158 size_t audit_size;
Eric Parisd6ea83e2012-04-04 13:45:49 -04003159
3160 /* We strip a nul only if it is at the end, otherwise the
3161 * context contains a nul and we should audit that */
Al Viroe3fea3f2012-06-09 08:15:16 +01003162 if (value) {
Colin Ian Kingadd24372017-10-14 13:46:55 +01003163 const char *str = value;
3164
Al Viroe3fea3f2012-06-09 08:15:16 +01003165 if (str[size - 1] == '\0')
3166 audit_size = size - 1;
3167 else
3168 audit_size = size;
3169 } else {
Al Viroe3fea3f2012-06-09 08:15:16 +01003170 audit_size = 0;
3171 }
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04003172 ab = audit_log_start(audit_context(),
3173 GFP_ATOMIC, AUDIT_SELINUX_ERR);
Eric Parisd6ea83e2012-04-04 13:45:49 -04003174 audit_log_format(ab, "op=setxattr invalid_context=");
3175 audit_log_n_untrustedstring(ab, value, audit_size);
3176 audit_log_end(ab);
3177
Stephen Smalley12b29f32008-05-07 13:03:20 -04003178 return rc;
Eric Parisd6ea83e2012-04-04 13:45:49 -04003179 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003180 rc = security_context_to_sid_force(&selinux_state, value,
3181 size, &newsid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04003182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 if (rc)
3184 return rc;
3185
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003186 rc = avc_has_perm(&selinux_state,
3187 sid, newsid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 FILE__RELABELTO, &ad);
3189 if (rc)
3190 return rc;
3191
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003192 rc = security_validate_transition(&selinux_state, isec->sid, newsid,
3193 sid, isec->sclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 if (rc)
3195 return rc;
3196
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003197 return avc_has_perm(&selinux_state,
3198 newsid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 sbsec->sid,
3200 SECCLASS_FILESYSTEM,
3201 FILESYSTEM__ASSOCIATE,
3202 &ad);
3203}
3204
David Howells8f0cfa52008-04-29 00:59:41 -07003205static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
Eric Parisf5269712008-05-14 11:27:45 -04003206 const void *value, size_t size,
David Howells8f0cfa52008-04-29 00:59:41 -07003207 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208{
David Howellsc6f493d2015-03-17 22:26:22 +00003209 struct inode *inode = d_backing_inode(dentry);
Paul Moore20cdef82016-04-04 14:14:42 -04003210 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 u32 newsid;
3212 int rc;
3213
3214 if (strcmp(name, XATTR_NAME_SELINUX)) {
3215 /* Not an attribute we recognize, so nothing to do. */
3216 return;
3217 }
3218
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003219 rc = security_context_to_sid_force(&selinux_state, value, size,
3220 &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 if (rc) {
peter enderborgc103a912018-06-12 10:09:03 +02003222 pr_err("SELinux: unable to map context to SID"
Stephen Smalley12b29f32008-05-07 13:03:20 -04003223 "for (%s, %lu), rc=%d\n",
3224 inode->i_sb->s_id, inode->i_ino, -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 return;
3226 }
3227
Paul Moore20cdef82016-04-04 14:14:42 -04003228 isec = backing_inode_security(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003229 spin_lock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003230 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05003232 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003233 spin_unlock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003234
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 return;
3236}
3237
David Howells8f0cfa52008-04-29 00:59:41 -07003238static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239{
David Howells88e67f32008-11-14 10:39:21 +11003240 const struct cred *cred = current_cred();
3241
Eric Paris2875fa02011-04-28 16:04:24 -04003242 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243}
3244
Eric Paris828dfe12008-04-17 13:17:49 -04003245static int selinux_inode_listxattr(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246{
David Howells88e67f32008-11-14 10:39:21 +11003247 const struct cred *cred = current_cred();
3248
Eric Paris2875fa02011-04-28 16:04:24 -04003249 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250}
3251
David Howells8f0cfa52008-04-29 00:59:41 -07003252static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253{
Eric W. Biederman6b240302017-10-02 09:38:20 -05003254 if (strcmp(name, XATTR_NAME_SELINUX)) {
3255 int rc = cap_inode_removexattr(dentry, name);
3256 if (rc)
3257 return rc;
3258
3259 /* Not an attribute we recognize, so just check the
3260 ordinary setattr permission. */
3261 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263
3264 /* No one is allowed to remove a SELinux security label.
3265 You can change the label, but all data must be labeled. */
3266 return -EACCES;
3267}
3268
James Morrisd381d8a2005-10-30 14:59:22 -08003269/*
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003270 * Copy the inode security context value to the user.
James Morrisd381d8a2005-10-30 14:59:22 -08003271 *
3272 * Permission check is handled by selinux_inode_getxattr hook.
3273 */
Andreas Gruenbacherea861df2015-12-24 11:09:39 -05003274static int selinux_inode_getsecurity(struct inode *inode, const char *name, void **buffer, bool alloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275{
David P. Quigley42492592008-02-04 22:29:39 -08003276 u32 size;
3277 int error;
3278 char *context = NULL;
Paul Moore20cdef82016-04-04 14:14:42 -04003279 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00003281 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3282 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003284 /*
3285 * If the caller has CAP_MAC_ADMIN, then get the raw context
3286 * value even if it is not defined by current policy; otherwise,
3287 * use the in-core value under current policy.
3288 * Use the non-auditing forms of the permission checks since
3289 * getxattr may be called by unprivileged processes commonly
3290 * and lack of permission just means that we fall back to the
3291 * in-core context value, not a denial.
3292 */
Paul Moore20cdef82016-04-04 14:14:42 -04003293 isec = inode_security(inode);
Stephen Smalleydb590002017-04-20 11:31:30 -04003294 if (has_cap_mac_admin(false))
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003295 error = security_sid_to_context_force(&selinux_state,
3296 isec->sid, &context,
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003297 &size);
3298 else
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003299 error = security_sid_to_context(&selinux_state, isec->sid,
3300 &context, &size);
David P. Quigley42492592008-02-04 22:29:39 -08003301 if (error)
3302 return error;
3303 error = size;
3304 if (alloc) {
3305 *buffer = context;
3306 goto out_nofree;
3307 }
3308 kfree(context);
3309out_nofree:
3310 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311}
3312
3313static int selinux_inode_setsecurity(struct inode *inode, const char *name,
Eric Paris828dfe12008-04-17 13:17:49 -04003314 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315{
Paul Moore2c971652016-04-19 16:36:28 -04003316 struct inode_security_struct *isec = inode_security_novalidate(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 u32 newsid;
3318 int rc;
3319
3320 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3321 return -EOPNOTSUPP;
3322
3323 if (!value || !size)
3324 return -EACCES;
3325
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003326 rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3327 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 if (rc)
3329 return rc;
3330
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003331 spin_lock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003332 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05003334 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003335 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 return 0;
3337}
3338
3339static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
3340{
3341 const int len = sizeof(XATTR_NAME_SELINUX);
3342 if (buffer && len <= buffer_size)
3343 memcpy(buffer, XATTR_NAME_SELINUX, len);
3344 return len;
3345}
3346
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05003347static void selinux_inode_getsecid(struct inode *inode, u32 *secid)
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02003348{
Andreas Gruenbachere817c2f2016-02-18 12:04:08 +01003349 struct inode_security_struct *isec = inode_security_novalidate(inode);
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02003350 *secid = isec->sid;
3351}
3352
Vivek Goyal56909eb2016-07-13 10:44:48 -04003353static int selinux_inode_copy_up(struct dentry *src, struct cred **new)
3354{
3355 u32 sid;
3356 struct task_security_struct *tsec;
3357 struct cred *new_creds = *new;
3358
3359 if (new_creds == NULL) {
3360 new_creds = prepare_creds();
3361 if (!new_creds)
3362 return -ENOMEM;
3363 }
3364
3365 tsec = new_creds->security;
3366 /* Get label from overlay inode and set it in create_sid */
3367 selinux_inode_getsecid(d_inode(src), &sid);
3368 tsec->create_sid = sid;
3369 *new = new_creds;
3370 return 0;
3371}
3372
Vivek Goyal19472b62016-07-13 10:44:50 -04003373static int selinux_inode_copy_up_xattr(const char *name)
3374{
3375 /* The copy_up hook above sets the initial context on an inode, but we
3376 * don't then want to overwrite it by blindly copying all the lower
3377 * xattrs up. Instead, we have to filter out SELinux-related xattrs.
3378 */
3379 if (strcmp(name, XATTR_NAME_SELINUX) == 0)
3380 return 1; /* Discard */
3381 /*
3382 * Any other attribute apart from SELINUX is not claimed, supported
3383 * by selinux.
3384 */
3385 return -EOPNOTSUPP;
3386}
3387
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388/* file security operations */
3389
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003390static int selinux_revalidate_file_permission(struct file *file, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391{
David Howells88e67f32008-11-14 10:39:21 +11003392 const struct cred *cred = current_cred();
Al Viro496ad9a2013-01-23 17:07:38 -05003393 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
3396 if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
3397 mask |= MAY_APPEND;
3398
Paul Moore389fb8002009-03-27 17:10:34 -04003399 return file_has_perm(cred, file,
3400 file_mask_to_av(inode->i_mode, mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401}
3402
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003403static int selinux_file_permission(struct file *file, int mask)
3404{
Al Viro496ad9a2013-01-23 17:07:38 -05003405 struct inode *inode = file_inode(file);
Stephen Smalley20dda182009-06-22 14:54:53 -04003406 struct file_security_struct *fsec = file->f_security;
Andreas Gruenbacherb1973672016-01-05 23:12:33 +01003407 struct inode_security_struct *isec;
Stephen Smalley20dda182009-06-22 14:54:53 -04003408 u32 sid = current_sid();
3409
Paul Moore389fb8002009-03-27 17:10:34 -04003410 if (!mask)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003411 /* No permission to check. Existence test. */
3412 return 0;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003413
Andreas Gruenbacherb1973672016-01-05 23:12:33 +01003414 isec = inode_security(inode);
Stephen Smalley20dda182009-06-22 14:54:53 -04003415 if (sid == fsec->sid && fsec->isid == isec->sid &&
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003416 fsec->pseqno == avc_policy_seqno(&selinux_state))
Eric Paris83d49852012-04-04 13:45:40 -04003417 /* No change since file_open check. */
Stephen Smalley20dda182009-06-22 14:54:53 -04003418 return 0;
3419
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003420 return selinux_revalidate_file_permission(file, mask);
3421}
3422
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423static int selinux_file_alloc_security(struct file *file)
3424{
3425 return file_alloc_security(file);
3426}
3427
3428static void selinux_file_free_security(struct file *file)
3429{
3430 file_free_security(file);
3431}
3432
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003433/*
3434 * Check whether a task has the ioctl permission and cmd
3435 * operation to an inode.
3436 */
Geliang Tang1d2a1682015-10-21 17:44:27 -04003437static int ioctl_has_perm(const struct cred *cred, struct file *file,
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003438 u32 requested, u16 cmd)
3439{
3440 struct common_audit_data ad;
3441 struct file_security_struct *fsec = file->f_security;
3442 struct inode *inode = file_inode(file);
Paul Moore20cdef82016-04-04 14:14:42 -04003443 struct inode_security_struct *isec;
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003444 struct lsm_ioctlop_audit ioctl;
3445 u32 ssid = cred_sid(cred);
3446 int rc;
3447 u8 driver = cmd >> 8;
3448 u8 xperm = cmd & 0xff;
3449
3450 ad.type = LSM_AUDIT_DATA_IOCTL_OP;
3451 ad.u.op = &ioctl;
3452 ad.u.op->cmd = cmd;
3453 ad.u.op->path = file->f_path;
3454
3455 if (ssid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003456 rc = avc_has_perm(&selinux_state,
3457 ssid, fsec->sid,
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003458 SECCLASS_FD,
3459 FD__USE,
3460 &ad);
3461 if (rc)
3462 goto out;
3463 }
3464
3465 if (unlikely(IS_PRIVATE(inode)))
3466 return 0;
3467
Paul Moore20cdef82016-04-04 14:14:42 -04003468 isec = inode_security(inode);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003469 rc = avc_has_extended_perms(&selinux_state,
3470 ssid, isec->sid, isec->sclass,
3471 requested, driver, xperm, &ad);
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003472out:
3473 return rc;
3474}
3475
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476static int selinux_file_ioctl(struct file *file, unsigned int cmd,
3477 unsigned long arg)
3478{
David Howells88e67f32008-11-14 10:39:21 +11003479 const struct cred *cred = current_cred();
Eric Paris0b24dcb2011-02-25 15:39:20 -05003480 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481
Eric Paris0b24dcb2011-02-25 15:39:20 -05003482 switch (cmd) {
3483 case FIONREAD:
3484 /* fall through */
3485 case FIBMAP:
3486 /* fall through */
3487 case FIGETBSZ:
3488 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003489 case FS_IOC_GETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003490 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003491 case FS_IOC_GETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003492 error = file_has_perm(cred, file, FILE__GETATTR);
3493 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494
Al Viro2f99c362012-03-23 16:04:05 -04003495 case FS_IOC_SETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003496 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003497 case FS_IOC_SETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003498 error = file_has_perm(cred, file, FILE__SETATTR);
3499 break;
3500
3501 /* sys_ioctl() checks */
3502 case FIONBIO:
3503 /* fall through */
3504 case FIOASYNC:
3505 error = file_has_perm(cred, file, 0);
3506 break;
3507
3508 case KDSKBENT:
3509 case KDSKBSENT:
Eric Paris6a9de492012-01-03 12:25:14 -05003510 error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04003511 SECURITY_CAP_AUDIT, true);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003512 break;
3513
3514 /* default case assumes that the command will go
3515 * to the file's ioctl() function.
3516 */
3517 default:
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003518 error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003519 }
3520 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521}
3522
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003523static int default_noexec;
3524
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
3526{
David Howells88e67f32008-11-14 10:39:21 +11003527 const struct cred *cred = current_cred();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003528 u32 sid = cred_sid(cred);
David Howellsd84f4f92008-11-14 10:39:23 +11003529 int rc = 0;
David Howells88e67f32008-11-14 10:39:21 +11003530
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003531 if (default_noexec &&
Stephen Smalley892e8ca2015-07-10 09:40:59 -04003532 (prot & PROT_EXEC) && (!file || IS_PRIVATE(file_inode(file)) ||
3533 (!shared && (prot & PROT_WRITE)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 /*
3535 * We are making executable an anonymous mapping or a
3536 * private file mapping that will also be writable.
3537 * This has an additional check.
3538 */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003539 rc = avc_has_perm(&selinux_state,
3540 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003541 PROCESS__EXECMEM, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 if (rc)
David Howellsd84f4f92008-11-14 10:39:23 +11003543 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545
3546 if (file) {
3547 /* read access is always possible with a mapping */
3548 u32 av = FILE__READ;
3549
3550 /* write access only matters if the mapping is shared */
3551 if (shared && (prot & PROT_WRITE))
3552 av |= FILE__WRITE;
3553
3554 if (prot & PROT_EXEC)
3555 av |= FILE__EXECUTE;
3556
David Howells88e67f32008-11-14 10:39:21 +11003557 return file_has_perm(cred, file, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 }
David Howellsd84f4f92008-11-14 10:39:23 +11003559
3560error:
3561 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562}
3563
Al Viroe5467852012-05-30 13:30:51 -04003564static int selinux_mmap_addr(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07003566 int rc = 0;
Paul Moore98883bf2014-03-19 16:46:11 -04003567
3568 if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
3569 u32 sid = current_sid();
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003570 rc = avc_has_perm(&selinux_state,
3571 sid, sid, SECCLASS_MEMPROTECT,
Paul Moore98883bf2014-03-19 16:46:11 -04003572 MEMPROTECT__MMAP_ZERO, NULL);
3573 }
3574
3575 return rc;
Al Viroe5467852012-05-30 13:30:51 -04003576}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577
Al Viroe5467852012-05-30 13:30:51 -04003578static int selinux_mmap_file(struct file *file, unsigned long reqprot,
3579 unsigned long prot, unsigned long flags)
3580{
Stephen Smalley3ba4bf52017-05-05 09:14:48 -04003581 struct common_audit_data ad;
3582 int rc;
3583
3584 if (file) {
3585 ad.type = LSM_AUDIT_DATA_FILE;
3586 ad.u.file = file;
3587 rc = inode_has_perm(current_cred(), file_inode(file),
3588 FILE__MAP, &ad);
3589 if (rc)
3590 return rc;
3591 }
3592
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003593 if (selinux_state.checkreqprot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 prot = reqprot;
3595
3596 return file_map_prot_check(file, prot,
3597 (flags & MAP_TYPE) == MAP_SHARED);
3598}
3599
3600static int selinux_file_mprotect(struct vm_area_struct *vma,
3601 unsigned long reqprot,
3602 unsigned long prot)
3603{
David Howells88e67f32008-11-14 10:39:21 +11003604 const struct cred *cred = current_cred();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003605 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003607 if (selinux_state.checkreqprot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 prot = reqprot;
3609
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003610 if (default_noexec &&
3611 (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
James Morrisd541bbe2009-01-29 12:19:51 +11003612 int rc = 0;
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003613 if (vma->vm_start >= vma->vm_mm->start_brk &&
3614 vma->vm_end <= vma->vm_mm->brk) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003615 rc = avc_has_perm(&selinux_state,
3616 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003617 PROCESS__EXECHEAP, NULL);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003618 } else if (!vma->vm_file &&
Stephen Smalleyc2316db2016-04-08 13:55:03 -04003619 ((vma->vm_start <= vma->vm_mm->start_stack &&
3620 vma->vm_end >= vma->vm_mm->start_stack) ||
Andy Lutomirskid17af502016-09-30 10:58:58 -07003621 vma_is_stack_for_current(vma))) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003622 rc = avc_has_perm(&selinux_state,
3623 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003624 PROCESS__EXECSTACK, NULL);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003625 } else if (vma->vm_file && vma->anon_vma) {
3626 /*
3627 * We are making executable a file mapping that has
3628 * had some COW done. Since pages might have been
3629 * written, check ability to execute the possibly
3630 * modified content. This typically should only
3631 * occur for text relocations.
3632 */
David Howellsd84f4f92008-11-14 10:39:23 +11003633 rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003634 }
Lorenzo Hernandez García-Hierro6b992192005-06-25 14:54:34 -07003635 if (rc)
3636 return rc;
3637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638
3639 return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
3640}
3641
3642static int selinux_file_lock(struct file *file, unsigned int cmd)
3643{
David Howells88e67f32008-11-14 10:39:21 +11003644 const struct cred *cred = current_cred();
3645
3646 return file_has_perm(cred, file, FILE__LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647}
3648
3649static int selinux_file_fcntl(struct file *file, unsigned int cmd,
3650 unsigned long arg)
3651{
David Howells88e67f32008-11-14 10:39:21 +11003652 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 int err = 0;
3654
3655 switch (cmd) {
Eric Paris828dfe12008-04-17 13:17:49 -04003656 case F_SETFL:
Eric Paris828dfe12008-04-17 13:17:49 -04003657 if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
David Howells88e67f32008-11-14 10:39:21 +11003658 err = file_has_perm(cred, file, FILE__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003660 }
3661 /* fall through */
3662 case F_SETOWN:
3663 case F_SETSIG:
3664 case F_GETFL:
3665 case F_GETOWN:
3666 case F_GETSIG:
Cyrill Gorcunov1d151c32012-07-30 14:43:00 -07003667 case F_GETOWNER_UIDS:
Eric Paris828dfe12008-04-17 13:17:49 -04003668 /* Just check FD__USE permission */
David Howells88e67f32008-11-14 10:39:21 +11003669 err = file_has_perm(cred, file, 0);
Eric Paris828dfe12008-04-17 13:17:49 -04003670 break;
3671 case F_GETLK:
3672 case F_SETLK:
3673 case F_SETLKW:
Jeff Layton0d3f7a22014-04-22 08:23:58 -04003674 case F_OFD_GETLK:
3675 case F_OFD_SETLK:
3676 case F_OFD_SETLKW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677#if BITS_PER_LONG == 32
Eric Paris828dfe12008-04-17 13:17:49 -04003678 case F_GETLK64:
3679 case F_SETLK64:
3680 case F_SETLKW64:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681#endif
David Howells88e67f32008-11-14 10:39:21 +11003682 err = file_has_perm(cred, file, FILE__LOCK);
Eric Paris828dfe12008-04-17 13:17:49 -04003683 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 }
3685
3686 return err;
3687}
3688
Jeff Laytone0b93ed2014-08-22 11:27:32 -04003689static void selinux_file_set_fowner(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 struct file_security_struct *fsec;
3692
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 fsec = file->f_security;
David Howells275bb412008-11-14 10:39:19 +11003694 fsec->fown_sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695}
3696
3697static int selinux_file_send_sigiotask(struct task_struct *tsk,
3698 struct fown_struct *fown, int signum)
3699{
Eric Paris828dfe12008-04-17 13:17:49 -04003700 struct file *file;
Stephen Smalley65c90bc2009-05-04 15:43:18 -04003701 u32 sid = task_sid(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 struct file_security_struct *fsec;
3704
3705 /* struct fown_struct is never outside the context of a struct file */
Eric Paris828dfe12008-04-17 13:17:49 -04003706 file = container_of(fown, struct file, f_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 fsec = file->f_security;
3709
3710 if (!signum)
3711 perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
3712 else
3713 perm = signal_to_av(signum);
3714
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003715 return avc_has_perm(&selinux_state,
3716 fsec->fown_sid, sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 SECCLASS_PROCESS, perm, NULL);
3718}
3719
3720static int selinux_file_receive(struct file *file)
3721{
David Howells88e67f32008-11-14 10:39:21 +11003722 const struct cred *cred = current_cred();
3723
3724 return file_has_perm(cred, file, file_to_av(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725}
3726
Al Viro94817692018-07-10 14:13:18 -04003727static int selinux_file_open(struct file *file)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003728{
3729 struct file_security_struct *fsec;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003730 struct inode_security_struct *isec;
David Howellsd84f4f92008-11-14 10:39:23 +11003731
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003732 fsec = file->f_security;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05003733 isec = inode_security(file_inode(file));
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003734 /*
3735 * Save inode label and policy sequence number
3736 * at open-time so that selinux_file_permission
3737 * can determine whether revalidation is necessary.
3738 * Task label is already saved in the file security
3739 * struct as its SID.
3740 */
3741 fsec->isid = isec->sid;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003742 fsec->pseqno = avc_policy_seqno(&selinux_state);
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003743 /*
3744 * Since the inode label or policy seqno may have changed
3745 * between the selinux_inode_permission check and the saving
3746 * of state above, recheck that access is still permitted.
3747 * Otherwise, access might never be revalidated against the
3748 * new inode label or new policy.
3749 * This check is not redundant - do not remove.
3750 */
Al Viro94817692018-07-10 14:13:18 -04003751 return file_path_has_perm(file->f_cred, file, open_file_to_av(file));
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003752}
3753
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754/* task security operations */
3755
Tetsuo Handaa79be232017-03-28 23:08:45 +09003756static int selinux_task_alloc(struct task_struct *task,
3757 unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003759 u32 sid = current_sid();
3760
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003761 return avc_has_perm(&selinux_state,
3762 sid, sid, SECCLASS_PROCESS, PROCESS__FORK, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763}
3764
David Howellsf1752ee2008-11-14 10:39:17 +11003765/*
David Howellsee18d642009-09-02 09:14:21 +01003766 * allocate the SELinux part of blank credentials
3767 */
3768static int selinux_cred_alloc_blank(struct cred *cred, gfp_t gfp)
3769{
3770 struct task_security_struct *tsec;
3771
3772 tsec = kzalloc(sizeof(struct task_security_struct), gfp);
3773 if (!tsec)
3774 return -ENOMEM;
3775
3776 cred->security = tsec;
3777 return 0;
3778}
3779
3780/*
David Howellsf1752ee2008-11-14 10:39:17 +11003781 * detach and free the LSM part of a set of credentials
3782 */
3783static void selinux_cred_free(struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784{
David Howellsf1752ee2008-11-14 10:39:17 +11003785 struct task_security_struct *tsec = cred->security;
David Howellse0e81732009-09-02 09:13:40 +01003786
Tetsuo Handa2edeaa32011-02-07 13:36:10 +00003787 /*
3788 * cred->security == NULL if security_cred_alloc_blank() or
3789 * security_prepare_creds() returned an error.
3790 */
3791 BUG_ON(cred->security && (unsigned long) cred->security < PAGE_SIZE);
David Howellse0e81732009-09-02 09:13:40 +01003792 cred->security = (void *) 0x7UL;
David Howellsf1752ee2008-11-14 10:39:17 +11003793 kfree(tsec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794}
3795
David Howellsd84f4f92008-11-14 10:39:23 +11003796/*
3797 * prepare a new set of credentials for modification
3798 */
3799static int selinux_cred_prepare(struct cred *new, const struct cred *old,
3800 gfp_t gfp)
3801{
3802 const struct task_security_struct *old_tsec;
3803 struct task_security_struct *tsec;
3804
3805 old_tsec = old->security;
3806
3807 tsec = kmemdup(old_tsec, sizeof(struct task_security_struct), gfp);
3808 if (!tsec)
3809 return -ENOMEM;
3810
3811 new->security = tsec;
3812 return 0;
3813}
3814
3815/*
David Howellsee18d642009-09-02 09:14:21 +01003816 * transfer the SELinux data to a blank set of creds
3817 */
3818static void selinux_cred_transfer(struct cred *new, const struct cred *old)
3819{
3820 const struct task_security_struct *old_tsec = old->security;
3821 struct task_security_struct *tsec = new->security;
3822
3823 *tsec = *old_tsec;
3824}
3825
Matthew Garrett3ec30112018-01-08 13:36:19 -08003826static void selinux_cred_getsecid(const struct cred *c, u32 *secid)
3827{
3828 *secid = cred_sid(c);
3829}
3830
David Howellsee18d642009-09-02 09:14:21 +01003831/*
David Howells3a3b7ce2008-11-14 10:39:28 +11003832 * set the security data for a kernel service
3833 * - all the creation contexts are set to unlabelled
3834 */
3835static int selinux_kernel_act_as(struct cred *new, u32 secid)
3836{
3837 struct task_security_struct *tsec = new->security;
3838 u32 sid = current_sid();
3839 int ret;
3840
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003841 ret = avc_has_perm(&selinux_state,
3842 sid, secid,
David Howells3a3b7ce2008-11-14 10:39:28 +11003843 SECCLASS_KERNEL_SERVICE,
3844 KERNEL_SERVICE__USE_AS_OVERRIDE,
3845 NULL);
3846 if (ret == 0) {
3847 tsec->sid = secid;
3848 tsec->create_sid = 0;
3849 tsec->keycreate_sid = 0;
3850 tsec->sockcreate_sid = 0;
3851 }
3852 return ret;
3853}
3854
3855/*
3856 * set the file creation context in a security record to the same as the
3857 * objective context of the specified inode
3858 */
3859static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
3860{
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05003861 struct inode_security_struct *isec = inode_security(inode);
David Howells3a3b7ce2008-11-14 10:39:28 +11003862 struct task_security_struct *tsec = new->security;
3863 u32 sid = current_sid();
3864 int ret;
3865
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003866 ret = avc_has_perm(&selinux_state,
3867 sid, isec->sid,
David Howells3a3b7ce2008-11-14 10:39:28 +11003868 SECCLASS_KERNEL_SERVICE,
3869 KERNEL_SERVICE__CREATE_FILES_AS,
3870 NULL);
3871
3872 if (ret == 0)
3873 tsec->create_sid = isec->sid;
David Howellsef574712010-02-26 01:56:16 +00003874 return ret;
David Howells3a3b7ce2008-11-14 10:39:28 +11003875}
3876
Eric Parisdd8dbf22009-11-03 16:35:32 +11003877static int selinux_kernel_module_request(char *kmod_name)
Eric Paris25354c42009-08-13 09:45:03 -04003878{
Eric Parisdd8dbf22009-11-03 16:35:32 +11003879 struct common_audit_data ad;
3880
Eric Paris50c205f2012-04-04 15:01:43 -04003881 ad.type = LSM_AUDIT_DATA_KMOD;
Eric Parisdd8dbf22009-11-03 16:35:32 +11003882 ad.u.kmod_name = kmod_name;
3883
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003884 return avc_has_perm(&selinux_state,
3885 current_sid(), SECINITSID_KERNEL, SECCLASS_SYSTEM,
Eric Parisdd8dbf22009-11-03 16:35:32 +11003886 SYSTEM__MODULE_REQUEST, &ad);
Eric Paris25354c42009-08-13 09:45:03 -04003887}
3888
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003889static int selinux_kernel_module_from_file(struct file *file)
3890{
3891 struct common_audit_data ad;
3892 struct inode_security_struct *isec;
3893 struct file_security_struct *fsec;
3894 u32 sid = current_sid();
3895 int rc;
3896
3897 /* init_module */
3898 if (file == NULL)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003899 return avc_has_perm(&selinux_state,
3900 sid, sid, SECCLASS_SYSTEM,
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003901 SYSTEM__MODULE_LOAD, NULL);
3902
3903 /* finit_module */
Paul Moore20cdef82016-04-04 14:14:42 -04003904
Vivek Goyal43af5de2016-09-09 11:37:49 -04003905 ad.type = LSM_AUDIT_DATA_FILE;
3906 ad.u.file = file;
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003907
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003908 fsec = file->f_security;
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003909 if (sid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003910 rc = avc_has_perm(&selinux_state,
3911 sid, fsec->sid, SECCLASS_FD, FD__USE, &ad);
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003912 if (rc)
3913 return rc;
3914 }
3915
Paul Moore20cdef82016-04-04 14:14:42 -04003916 isec = inode_security(file_inode(file));
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003917 return avc_has_perm(&selinux_state,
3918 sid, isec->sid, SECCLASS_SYSTEM,
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003919 SYSTEM__MODULE_LOAD, &ad);
3920}
3921
3922static int selinux_kernel_read_file(struct file *file,
3923 enum kernel_read_file_id id)
3924{
3925 int rc = 0;
3926
3927 switch (id) {
3928 case READING_MODULE:
3929 rc = selinux_kernel_module_from_file(file);
3930 break;
3931 default:
3932 break;
3933 }
3934
3935 return rc;
3936}
3937
Mimi Zoharc77b8cd2018-07-13 14:06:02 -04003938static int selinux_kernel_load_data(enum kernel_load_data_id id)
3939{
3940 int rc = 0;
3941
3942 switch (id) {
3943 case LOADING_MODULE:
3944 rc = selinux_kernel_module_from_file(NULL);
3945 default:
3946 break;
3947 }
3948
3949 return rc;
3950}
3951
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
3953{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003954 return avc_has_perm(&selinux_state,
3955 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003956 PROCESS__SETPGID, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957}
3958
3959static int selinux_task_getpgid(struct task_struct *p)
3960{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003961 return avc_has_perm(&selinux_state,
3962 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003963 PROCESS__GETPGID, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964}
3965
3966static int selinux_task_getsid(struct task_struct *p)
3967{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003968 return avc_has_perm(&selinux_state,
3969 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003970 PROCESS__GETSESSION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971}
3972
David Quigleyf9008e4c2006-06-30 01:55:46 -07003973static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
3974{
David Howells275bb412008-11-14 10:39:19 +11003975 *secid = task_sid(p);
David Quigleyf9008e4c2006-06-30 01:55:46 -07003976}
3977
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978static int selinux_task_setnice(struct task_struct *p, int nice)
3979{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003980 return avc_has_perm(&selinux_state,
3981 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003982 PROCESS__SETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983}
3984
James Morris03e68062006-06-23 02:03:58 -07003985static int selinux_task_setioprio(struct task_struct *p, int ioprio)
3986{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003987 return avc_has_perm(&selinux_state,
3988 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003989 PROCESS__SETSCHED, NULL);
James Morris03e68062006-06-23 02:03:58 -07003990}
3991
David Quigleya1836a42006-06-30 01:55:49 -07003992static int selinux_task_getioprio(struct task_struct *p)
3993{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003994 return avc_has_perm(&selinux_state,
3995 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003996 PROCESS__GETSCHED, NULL);
David Quigleya1836a42006-06-30 01:55:49 -07003997}
3998
Corentin LABBE42985552017-10-04 20:32:18 +02003999static int selinux_task_prlimit(const struct cred *cred, const struct cred *tcred,
4000 unsigned int flags)
Stephen Smalley791ec492017-02-17 07:57:00 -05004001{
4002 u32 av = 0;
4003
Stephen Smalley84e68852017-02-28 09:35:08 -05004004 if (!flags)
4005 return 0;
Stephen Smalley791ec492017-02-17 07:57:00 -05004006 if (flags & LSM_PRLIMIT_WRITE)
4007 av |= PROCESS__SETRLIMIT;
4008 if (flags & LSM_PRLIMIT_READ)
4009 av |= PROCESS__GETRLIMIT;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004010 return avc_has_perm(&selinux_state,
4011 cred_sid(cred), cred_sid(tcred),
Stephen Smalley791ec492017-02-17 07:57:00 -05004012 SECCLASS_PROCESS, av, NULL);
4013}
4014
Jiri Slaby8fd00b42009-08-26 18:41:16 +02004015static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
4016 struct rlimit *new_rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017{
Jiri Slaby8fd00b42009-08-26 18:41:16 +02004018 struct rlimit *old_rlim = p->signal->rlim + resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019
4020 /* Control the ability to change the hard limit (whether
4021 lowering or raising it), so that the hard limit can
4022 later be used as a safe reset point for the soft limit
David Howellsd84f4f92008-11-14 10:39:23 +11004023 upon context transitions. See selinux_bprm_committing_creds. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 if (old_rlim->rlim_max != new_rlim->rlim_max)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004025 return avc_has_perm(&selinux_state,
4026 current_sid(), task_sid(p),
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004027 SECCLASS_PROCESS, PROCESS__SETRLIMIT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028
4029 return 0;
4030}
4031
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09004032static int selinux_task_setscheduler(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004034 return avc_has_perm(&selinux_state,
4035 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004036 PROCESS__SETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037}
4038
4039static int selinux_task_getscheduler(struct task_struct *p)
4040{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004041 return avc_has_perm(&selinux_state,
4042 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004043 PROCESS__GETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044}
4045
David Quigley35601542006-06-23 02:04:01 -07004046static int selinux_task_movememory(struct task_struct *p)
4047{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004048 return avc_has_perm(&selinux_state,
4049 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004050 PROCESS__SETSCHED, NULL);
David Quigley35601542006-06-23 02:04:01 -07004051}
4052
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02004053static int selinux_task_kill(struct task_struct *p, struct kernel_siginfo *info,
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004054 int sig, const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055{
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004056 u32 secid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 if (!sig)
4060 perm = PROCESS__SIGNULL; /* null signal; existence test */
4061 else
4062 perm = signal_to_av(sig);
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004063 if (!cred)
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004064 secid = current_sid();
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004065 else
4066 secid = cred_sid(cred);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004067 return avc_has_perm(&selinux_state,
4068 secid, task_sid(p), SECCLASS_PROCESS, perm, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069}
4070
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071static void selinux_task_to_inode(struct task_struct *p,
4072 struct inode *inode)
4073{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 struct inode_security_struct *isec = inode->i_security;
David Howells275bb412008-11-14 10:39:19 +11004075 u32 sid = task_sid(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004077 spin_lock(&isec->lock);
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01004078 isec->sclass = inode_mode_to_security_class(inode->i_mode);
David Howells275bb412008-11-14 10:39:19 +11004079 isec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004080 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004081 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082}
4083
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004085static int selinux_parse_skb_ipv4(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04004086 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087{
4088 int offset, ihlen, ret = -EINVAL;
4089 struct iphdr _iph, *ih;
4090
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03004091 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
4093 if (ih == NULL)
4094 goto out;
4095
4096 ihlen = ih->ihl * 4;
4097 if (ihlen < sizeof(_iph))
4098 goto out;
4099
Eric Paris48c62af2012-04-02 13:15:44 -04004100 ad->u.net->v4info.saddr = ih->saddr;
4101 ad->u.net->v4info.daddr = ih->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 ret = 0;
4103
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004104 if (proto)
4105 *proto = ih->protocol;
4106
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 switch (ih->protocol) {
Eric Paris828dfe12008-04-17 13:17:49 -04004108 case IPPROTO_TCP: {
4109 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110
Eric Paris828dfe12008-04-17 13:17:49 -04004111 if (ntohs(ih->frag_off) & IP_OFFSET)
4112 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113
4114 offset += ihlen;
4115 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4116 if (th == NULL)
4117 break;
4118
Eric Paris48c62af2012-04-02 13:15:44 -04004119 ad->u.net->sport = th->source;
4120 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123
Eric Paris828dfe12008-04-17 13:17:49 -04004124 case IPPROTO_UDP: {
4125 struct udphdr _udph, *uh;
4126
4127 if (ntohs(ih->frag_off) & IP_OFFSET)
4128 break;
4129
4130 offset += ihlen;
4131 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4132 if (uh == NULL)
4133 break;
4134
Eric Paris48c62af2012-04-02 13:15:44 -04004135 ad->u.net->sport = uh->source;
4136 ad->u.net->dport = uh->dest;
Eric Paris828dfe12008-04-17 13:17:49 -04004137 break;
4138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139
James Morris2ee92d42006-11-13 16:09:01 -08004140 case IPPROTO_DCCP: {
4141 struct dccp_hdr _dccph, *dh;
4142
4143 if (ntohs(ih->frag_off) & IP_OFFSET)
4144 break;
4145
4146 offset += ihlen;
4147 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4148 if (dh == NULL)
4149 break;
4150
Eric Paris48c62af2012-04-02 13:15:44 -04004151 ad->u.net->sport = dh->dccph_sport;
4152 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08004153 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004154 }
James Morris2ee92d42006-11-13 16:09:01 -08004155
Richard Hainesd4529302018-02-13 20:57:18 +00004156#if IS_ENABLED(CONFIG_IP_SCTP)
4157 case IPPROTO_SCTP: {
4158 struct sctphdr _sctph, *sh;
4159
4160 if (ntohs(ih->frag_off) & IP_OFFSET)
4161 break;
4162
4163 offset += ihlen;
4164 sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4165 if (sh == NULL)
4166 break;
4167
4168 ad->u.net->sport = sh->source;
4169 ad->u.net->dport = sh->dest;
4170 break;
4171 }
4172#endif
Eric Paris828dfe12008-04-17 13:17:49 -04004173 default:
4174 break;
4175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176out:
4177 return ret;
4178}
4179
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04004180#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181
4182/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004183static int selinux_parse_skb_ipv6(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04004184 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185{
4186 u8 nexthdr;
4187 int ret = -EINVAL, offset;
4188 struct ipv6hdr _ipv6h, *ip6;
Jesse Gross75f28112011-11-30 17:05:51 -08004189 __be16 frag_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03004191 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
4193 if (ip6 == NULL)
4194 goto out;
4195
Eric Paris48c62af2012-04-02 13:15:44 -04004196 ad->u.net->v6info.saddr = ip6->saddr;
4197 ad->u.net->v6info.daddr = ip6->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 ret = 0;
4199
4200 nexthdr = ip6->nexthdr;
4201 offset += sizeof(_ipv6h);
Jesse Gross75f28112011-11-30 17:05:51 -08004202 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 if (offset < 0)
4204 goto out;
4205
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004206 if (proto)
4207 *proto = nexthdr;
4208
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 switch (nexthdr) {
4210 case IPPROTO_TCP: {
Eric Paris828dfe12008-04-17 13:17:49 -04004211 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212
4213 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4214 if (th == NULL)
4215 break;
4216
Eric Paris48c62af2012-04-02 13:15:44 -04004217 ad->u.net->sport = th->source;
4218 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 break;
4220 }
4221
4222 case IPPROTO_UDP: {
4223 struct udphdr _udph, *uh;
4224
4225 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4226 if (uh == NULL)
4227 break;
4228
Eric Paris48c62af2012-04-02 13:15:44 -04004229 ad->u.net->sport = uh->source;
4230 ad->u.net->dport = uh->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 break;
4232 }
4233
James Morris2ee92d42006-11-13 16:09:01 -08004234 case IPPROTO_DCCP: {
4235 struct dccp_hdr _dccph, *dh;
4236
4237 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4238 if (dh == NULL)
4239 break;
4240
Eric Paris48c62af2012-04-02 13:15:44 -04004241 ad->u.net->sport = dh->dccph_sport;
4242 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08004243 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004244 }
James Morris2ee92d42006-11-13 16:09:01 -08004245
Richard Hainesd4529302018-02-13 20:57:18 +00004246#if IS_ENABLED(CONFIG_IP_SCTP)
4247 case IPPROTO_SCTP: {
4248 struct sctphdr _sctph, *sh;
4249
4250 sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4251 if (sh == NULL)
4252 break;
4253
4254 ad->u.net->sport = sh->source;
4255 ad->u.net->dport = sh->dest;
4256 break;
4257 }
4258#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 /* includes fragments */
4260 default:
4261 break;
4262 }
4263out:
4264 return ret;
4265}
4266
4267#endif /* IPV6 */
4268
Thomas Liu2bf49692009-07-14 12:14:09 -04004269static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
David Howellscf9481e2008-07-27 21:31:07 +10004270 char **_addrp, int src, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271{
David Howellscf9481e2008-07-27 21:31:07 +10004272 char *addrp;
4273 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274
Eric Paris48c62af2012-04-02 13:15:44 -04004275 switch (ad->u.net->family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 case PF_INET:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004277 ret = selinux_parse_skb_ipv4(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10004278 if (ret)
4279 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04004280 addrp = (char *)(src ? &ad->u.net->v4info.saddr :
4281 &ad->u.net->v4info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10004282 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04004284#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 case PF_INET6:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004286 ret = selinux_parse_skb_ipv6(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10004287 if (ret)
4288 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04004289 addrp = (char *)(src ? &ad->u.net->v6info.saddr :
4290 &ad->u.net->v6info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10004291 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292#endif /* IPV6 */
4293 default:
David Howellscf9481e2008-07-27 21:31:07 +10004294 addrp = NULL;
4295 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 }
4297
David Howellscf9481e2008-07-27 21:31:07 +10004298parse_error:
peter enderborgc103a912018-06-12 10:09:03 +02004299 pr_warn(
David Howellscf9481e2008-07-27 21:31:07 +10004300 "SELinux: failure in selinux_parse_skb(),"
4301 " unable to parse packet\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 return ret;
David Howellscf9481e2008-07-27 21:31:07 +10004303
4304okay:
4305 if (_addrp)
4306 *_addrp = addrp;
4307 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308}
4309
Paul Moore4f6a9932007-03-01 14:35:22 -05004310/**
Paul Moore220deb92008-01-29 08:38:23 -05004311 * selinux_skb_peerlbl_sid - Determine the peer label of a packet
Paul Moore4f6a9932007-03-01 14:35:22 -05004312 * @skb: the packet
Paul Moore75e22912008-01-29 08:38:04 -05004313 * @family: protocol family
Paul Moore220deb92008-01-29 08:38:23 -05004314 * @sid: the packet's peer label SID
Paul Moore4f6a9932007-03-01 14:35:22 -05004315 *
4316 * Description:
Paul Moore220deb92008-01-29 08:38:23 -05004317 * Check the various different forms of network peer labeling and determine
4318 * the peer label/SID for the packet; most of the magic actually occurs in
4319 * the security server function security_net_peersid_cmp(). The function
4320 * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
4321 * or -EACCES if @sid is invalid due to inconsistencies with the different
4322 * peer labels.
Paul Moore4f6a9932007-03-01 14:35:22 -05004323 *
4324 */
Paul Moore220deb92008-01-29 08:38:23 -05004325static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
Paul Moore4f6a9932007-03-01 14:35:22 -05004326{
Paul Moore71f1cb02008-01-29 08:51:16 -05004327 int err;
Paul Moore4f6a9932007-03-01 14:35:22 -05004328 u32 xfrm_sid;
4329 u32 nlbl_sid;
Paul Moore220deb92008-01-29 08:38:23 -05004330 u32 nlbl_type;
Paul Moore4f6a9932007-03-01 14:35:22 -05004331
Paul Moore817eff72013-12-10 14:57:54 -05004332 err = selinux_xfrm_skb_sid(skb, &xfrm_sid);
Paul Moorebed4d7e2013-07-23 17:38:40 -04004333 if (unlikely(err))
4334 return -EACCES;
4335 err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
4336 if (unlikely(err))
4337 return -EACCES;
Paul Moore220deb92008-01-29 08:38:23 -05004338
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004339 err = security_net_peersid_resolve(&selinux_state, nlbl_sid,
4340 nlbl_type, xfrm_sid, sid);
Paul Moore71f1cb02008-01-29 08:51:16 -05004341 if (unlikely(err)) {
peter enderborgc103a912018-06-12 10:09:03 +02004342 pr_warn(
Paul Moore71f1cb02008-01-29 08:51:16 -05004343 "SELinux: failure in selinux_skb_peerlbl_sid(),"
4344 " unable to determine packet's peer label\n");
Paul Moore220deb92008-01-29 08:38:23 -05004345 return -EACCES;
Paul Moore71f1cb02008-01-29 08:51:16 -05004346 }
Paul Moore220deb92008-01-29 08:38:23 -05004347
4348 return 0;
Paul Moore4f6a9932007-03-01 14:35:22 -05004349}
4350
Paul Moore446b8022013-12-04 16:10:51 -05004351/**
4352 * selinux_conn_sid - Determine the child socket label for a connection
4353 * @sk_sid: the parent socket's SID
4354 * @skb_sid: the packet's SID
4355 * @conn_sid: the resulting connection SID
4356 *
4357 * If @skb_sid is valid then the user:role:type information from @sk_sid is
4358 * combined with the MLS information from @skb_sid in order to create
4359 * @conn_sid. If @skb_sid is not valid then then @conn_sid is simply a copy
4360 * of @sk_sid. Returns zero on success, negative values on failure.
4361 *
4362 */
4363static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid)
4364{
4365 int err = 0;
4366
4367 if (skb_sid != SECSID_NULL)
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004368 err = security_sid_mls_copy(&selinux_state, sk_sid, skb_sid,
4369 conn_sid);
Paul Moore446b8022013-12-04 16:10:51 -05004370 else
4371 *conn_sid = sk_sid;
4372
4373 return err;
4374}
4375
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376/* socket security operations */
Paul Moored4f2d972010-04-22 14:46:18 -04004377
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004378static int socket_sockcreate_sid(const struct task_security_struct *tsec,
4379 u16 secclass, u32 *socksid)
Paul Moored4f2d972010-04-22 14:46:18 -04004380{
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004381 if (tsec->sockcreate_sid > SECSID_NULL) {
4382 *socksid = tsec->sockcreate_sid;
4383 return 0;
4384 }
4385
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004386 return security_transition_sid(&selinux_state, tsec->sid, tsec->sid,
4387 secclass, NULL, socksid);
Paul Moored4f2d972010-04-22 14:46:18 -04004388}
4389
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004390static int sock_has_perm(struct sock *sk, u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391{
Paul Moore253bfae2010-04-22 14:46:19 -04004392 struct sk_security_struct *sksec = sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004393 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004394 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395
Paul Moore253bfae2010-04-22 14:46:19 -04004396 if (sksec->sid == SECINITSID_KERNEL)
4397 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398
Eric Paris50c205f2012-04-04 15:01:43 -04004399 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004400 ad.u.net = &net;
4401 ad.u.net->sk = sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004403 return avc_has_perm(&selinux_state,
4404 current_sid(), sksec->sid, sksec->sclass, perms,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004405 &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406}
4407
4408static int selinux_socket_create(int family, int type,
4409 int protocol, int kern)
4410{
Paul Moore5fb49872010-04-22 14:46:19 -04004411 const struct task_security_struct *tsec = current_security();
Paul Moored4f2d972010-04-22 14:46:18 -04004412 u32 newsid;
David Howells275bb412008-11-14 10:39:19 +11004413 u16 secclass;
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004414 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415
4416 if (kern)
Paul Moored4f2d972010-04-22 14:46:18 -04004417 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418
David Howells275bb412008-11-14 10:39:19 +11004419 secclass = socket_type_to_security_class(family, type, protocol);
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004420 rc = socket_sockcreate_sid(tsec, secclass, &newsid);
4421 if (rc)
4422 return rc;
4423
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004424 return avc_has_perm(&selinux_state,
4425 tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426}
4427
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07004428static int selinux_socket_post_create(struct socket *sock, int family,
4429 int type, int protocol, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430{
Paul Moore5fb49872010-04-22 14:46:19 -04004431 const struct task_security_struct *tsec = current_security();
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05004432 struct inode_security_struct *isec = inode_security_novalidate(SOCK_INODE(sock));
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004433 struct sk_security_struct *sksec;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004434 u16 sclass = socket_type_to_security_class(family, type, protocol);
4435 u32 sid = SECINITSID_KERNEL;
David Howells275bb412008-11-14 10:39:19 +11004436 int err = 0;
4437
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004438 if (!kern) {
4439 err = socket_sockcreate_sid(tsec, sclass, &sid);
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004440 if (err)
4441 return err;
4442 }
David Howells275bb412008-11-14 10:39:19 +11004443
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004444 isec->sclass = sclass;
4445 isec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004446 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004448 if (sock->sk) {
4449 sksec = sock->sk->sk_security;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004450 sksec->sclass = sclass;
4451 sksec->sid = sid;
Richard Hainesd4529302018-02-13 20:57:18 +00004452 /* Allows detection of the first association on this socket */
4453 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4454 sksec->sctp_assoc_state = SCTP_ASSOC_UNSET;
4455
Paul Moore389fb8002009-03-27 17:10:34 -04004456 err = selinux_netlbl_socket_post_create(sock->sk, family);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004457 }
4458
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07004459 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460}
4461
David Herrmann0b811db2018-05-04 16:28:21 +02004462static int selinux_socket_socketpair(struct socket *socka,
4463 struct socket *sockb)
4464{
4465 struct sk_security_struct *sksec_a = socka->sk->sk_security;
4466 struct sk_security_struct *sksec_b = sockb->sk->sk_security;
4467
4468 sksec_a->peer_sid = sksec_b->sid;
4469 sksec_b->peer_sid = sksec_a->sid;
4470
4471 return 0;
4472}
4473
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474/* Range of port numbers used to automatically bind.
4475 Need to determine whether we should perform a name_bind
4476 permission check between the socket and the port number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477
4478static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
4479{
Paul Moore253bfae2010-04-22 14:46:19 -04004480 struct sock *sk = sock->sk;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004481 struct sk_security_struct *sksec = sk->sk_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 u16 family;
4483 int err;
4484
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004485 err = sock_has_perm(sk, SOCKET__BIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 if (err)
4487 goto out;
4488
Richard Hainesd4529302018-02-13 20:57:18 +00004489 /* If PF_INET or PF_INET6, check name_bind permission for the port. */
Paul Moore253bfae2010-04-22 14:46:19 -04004490 family = sk->sk_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 if (family == PF_INET || family == PF_INET6) {
4492 char *addrp;
Thomas Liu2bf49692009-07-14 12:14:09 -04004493 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004494 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495 struct sockaddr_in *addr4 = NULL;
4496 struct sockaddr_in6 *addr6 = NULL;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004497 u16 family_sa = address->sa_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 unsigned short snum;
James Morrise399f982008-06-12 01:39:58 +10004499 u32 sid, node_perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500
Richard Hainesd4529302018-02-13 20:57:18 +00004501 /*
4502 * sctp_bindx(3) calls via selinux_sctp_bind_connect()
4503 * that validates multiple binding addresses. Because of this
4504 * need to check address->sa_family as it is possible to have
4505 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4506 */
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004507 switch (family_sa) {
4508 case AF_UNSPEC:
Richard Haines68741a8a2018-03-02 19:54:34 +00004509 case AF_INET:
4510 if (addrlen < sizeof(struct sockaddr_in))
4511 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 addr4 = (struct sockaddr_in *)address;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004513 if (family_sa == AF_UNSPEC) {
4514 /* see __inet_bind(), we only want to allow
4515 * AF_UNSPEC if the address is INADDR_ANY
4516 */
4517 if (addr4->sin_addr.s_addr != htonl(INADDR_ANY))
4518 goto err_af;
4519 family_sa = AF_INET;
4520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 snum = ntohs(addr4->sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522 addrp = (char *)&addr4->sin_addr.s_addr;
Richard Haines68741a8a2018-03-02 19:54:34 +00004523 break;
4524 case AF_INET6:
4525 if (addrlen < SIN6_LEN_RFC2133)
4526 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527 addr6 = (struct sockaddr_in6 *)address;
4528 snum = ntohs(addr6->sin6_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 addrp = (char *)&addr6->sin6_addr.s6_addr;
Richard Haines68741a8a2018-03-02 19:54:34 +00004530 break;
4531 default:
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004532 goto err_af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 }
4534
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004535 ad.type = LSM_AUDIT_DATA_NET;
4536 ad.u.net = &net;
4537 ad.u.net->sport = htons(snum);
4538 ad.u.net->family = family_sa;
4539
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004540 if (snum) {
4541 int low, high;
4542
Eric W. Biederman0bbf87d2013-09-28 14:10:59 -07004543 inet_get_local_port_range(sock_net(sk), &low, &high);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004544
Krister Johansen4548b682017-01-20 17:49:11 -08004545 if (snum < max(inet_prot_sock(sock_net(sk)), low) ||
4546 snum > high) {
Paul Moore3e1121722008-04-10 10:48:14 -04004547 err = sel_netport_sid(sk->sk_protocol,
4548 snum, &sid);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004549 if (err)
4550 goto out;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004551 err = avc_has_perm(&selinux_state,
4552 sksec->sid, sid,
Paul Moore253bfae2010-04-22 14:46:19 -04004553 sksec->sclass,
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004554 SOCKET__NAME_BIND, &ad);
4555 if (err)
4556 goto out;
4557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 }
Eric Paris828dfe12008-04-17 13:17:49 -04004559
Paul Moore253bfae2010-04-22 14:46:19 -04004560 switch (sksec->sclass) {
James Morris13402582005-09-30 14:24:34 -04004561 case SECCLASS_TCP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 node_perm = TCP_SOCKET__NODE_BIND;
4563 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004564
James Morris13402582005-09-30 14:24:34 -04004565 case SECCLASS_UDP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 node_perm = UDP_SOCKET__NODE_BIND;
4567 break;
James Morris2ee92d42006-11-13 16:09:01 -08004568
4569 case SECCLASS_DCCP_SOCKET:
4570 node_perm = DCCP_SOCKET__NODE_BIND;
4571 break;
4572
Richard Hainesd4529302018-02-13 20:57:18 +00004573 case SECCLASS_SCTP_SOCKET:
4574 node_perm = SCTP_SOCKET__NODE_BIND;
4575 break;
4576
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 default:
4578 node_perm = RAWIP_SOCKET__NODE_BIND;
4579 break;
4580 }
Eric Paris828dfe12008-04-17 13:17:49 -04004581
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004582 err = sel_netnode_sid(addrp, family_sa, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 if (err)
4584 goto out;
Eric Paris828dfe12008-04-17 13:17:49 -04004585
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004586 if (family_sa == AF_INET)
Eric Paris48c62af2012-04-02 13:15:44 -04004587 ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 else
Eric Paris48c62af2012-04-02 13:15:44 -04004589 ad.u.net->v6info.saddr = addr6->sin6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004591 err = avc_has_perm(&selinux_state,
4592 sksec->sid, sid,
Paul Moore253bfae2010-04-22 14:46:19 -04004593 sksec->sclass, node_perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594 if (err)
4595 goto out;
4596 }
4597out:
4598 return err;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004599err_af:
4600 /* Note that SCTP services expect -EINVAL, others -EAFNOSUPPORT. */
4601 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4602 return -EINVAL;
4603 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604}
4605
Richard Hainesd4529302018-02-13 20:57:18 +00004606/* This supports connect(2) and SCTP connect services such as sctp_connectx(3)
Mauro Carvalho Chehab5fb94e92018-05-08 15:14:57 -03004607 * and sctp_sendmsg(3) as described in Documentation/security/LSM-sctp.rst
Richard Hainesd4529302018-02-13 20:57:18 +00004608 */
4609static int selinux_socket_connect_helper(struct socket *sock,
4610 struct sockaddr *address, int addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611{
Paul Moore014ab192008-10-10 10:16:33 -04004612 struct sock *sk = sock->sk;
Paul Moore253bfae2010-04-22 14:46:19 -04004613 struct sk_security_struct *sksec = sk->sk_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614 int err;
4615
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004616 err = sock_has_perm(sk, SOCKET__CONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 if (err)
4618 return err;
4619
4620 /*
Richard Hainesd4529302018-02-13 20:57:18 +00004621 * If a TCP, DCCP or SCTP socket, check name_connect permission
4622 * for the port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623 */
Paul Moore253bfae2010-04-22 14:46:19 -04004624 if (sksec->sclass == SECCLASS_TCP_SOCKET ||
Richard Hainesd4529302018-02-13 20:57:18 +00004625 sksec->sclass == SECCLASS_DCCP_SOCKET ||
4626 sksec->sclass == SECCLASS_SCTP_SOCKET) {
Thomas Liu2bf49692009-07-14 12:14:09 -04004627 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004628 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629 struct sockaddr_in *addr4 = NULL;
4630 struct sockaddr_in6 *addr6 = NULL;
4631 unsigned short snum;
James Morris2ee92d42006-11-13 16:09:01 -08004632 u32 sid, perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633
Richard Hainesd4529302018-02-13 20:57:18 +00004634 /* sctp_connectx(3) calls via selinux_sctp_bind_connect()
4635 * that validates multiple connect addresses. Because of this
4636 * need to check address->sa_family as it is possible to have
4637 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4638 */
Richard Haines68741a8a2018-03-02 19:54:34 +00004639 switch (address->sa_family) {
4640 case AF_INET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 addr4 = (struct sockaddr_in *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004642 if (addrlen < sizeof(struct sockaddr_in))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 return -EINVAL;
4644 snum = ntohs(addr4->sin_port);
Richard Haines68741a8a2018-03-02 19:54:34 +00004645 break;
4646 case AF_INET6:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 addr6 = (struct sockaddr_in6 *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004648 if (addrlen < SIN6_LEN_RFC2133)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 return -EINVAL;
4650 snum = ntohs(addr6->sin6_port);
Richard Haines68741a8a2018-03-02 19:54:34 +00004651 break;
4652 default:
4653 /* Note that SCTP services expect -EINVAL, whereas
4654 * others expect -EAFNOSUPPORT.
4655 */
4656 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4657 return -EINVAL;
4658 else
4659 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660 }
4661
Paul Moore3e1121722008-04-10 10:48:14 -04004662 err = sel_netport_sid(sk->sk_protocol, snum, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 if (err)
Richard Hainesd4529302018-02-13 20:57:18 +00004664 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665
Richard Hainesd4529302018-02-13 20:57:18 +00004666 switch (sksec->sclass) {
4667 case SECCLASS_TCP_SOCKET:
4668 perm = TCP_SOCKET__NAME_CONNECT;
4669 break;
4670 case SECCLASS_DCCP_SOCKET:
4671 perm = DCCP_SOCKET__NAME_CONNECT;
4672 break;
4673 case SECCLASS_SCTP_SOCKET:
4674 perm = SCTP_SOCKET__NAME_CONNECT;
4675 break;
4676 }
James Morris2ee92d42006-11-13 16:09:01 -08004677
Eric Paris50c205f2012-04-04 15:01:43 -04004678 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004679 ad.u.net = &net;
4680 ad.u.net->dport = htons(snum);
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004681 ad.u.net->family = address->sa_family;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004682 err = avc_has_perm(&selinux_state,
4683 sksec->sid, sid, sksec->sclass, perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 if (err)
Richard Hainesd4529302018-02-13 20:57:18 +00004685 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686 }
4687
Richard Hainesd4529302018-02-13 20:57:18 +00004688 return 0;
4689}
Paul Moore014ab192008-10-10 10:16:33 -04004690
Richard Hainesd4529302018-02-13 20:57:18 +00004691/* Supports connect(2), see comments in selinux_socket_connect_helper() */
4692static int selinux_socket_connect(struct socket *sock,
4693 struct sockaddr *address, int addrlen)
4694{
4695 int err;
4696 struct sock *sk = sock->sk;
4697
4698 err = selinux_socket_connect_helper(sock, address, addrlen);
4699 if (err)
4700 return err;
4701
4702 return selinux_netlbl_socket_connect(sk, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703}
4704
4705static int selinux_socket_listen(struct socket *sock, int backlog)
4706{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004707 return sock_has_perm(sock->sk, SOCKET__LISTEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708}
4709
4710static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
4711{
4712 int err;
4713 struct inode_security_struct *isec;
4714 struct inode_security_struct *newisec;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004715 u16 sclass;
4716 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004718 err = sock_has_perm(sock->sk, SOCKET__ACCEPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 if (err)
4720 return err;
4721
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05004722 isec = inode_security_novalidate(SOCK_INODE(sock));
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004723 spin_lock(&isec->lock);
4724 sclass = isec->sclass;
4725 sid = isec->sid;
4726 spin_unlock(&isec->lock);
4727
4728 newisec = inode_security_novalidate(SOCK_INODE(newsock));
4729 newisec->sclass = sclass;
4730 newisec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004731 newisec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732
4733 return 0;
4734}
4735
4736static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
Eric Paris828dfe12008-04-17 13:17:49 -04004737 int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004739 return sock_has_perm(sock->sk, SOCKET__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740}
4741
4742static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
4743 int size, int flags)
4744{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004745 return sock_has_perm(sock->sk, SOCKET__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746}
4747
4748static int selinux_socket_getsockname(struct socket *sock)
4749{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004750 return sock_has_perm(sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751}
4752
4753static int selinux_socket_getpeername(struct socket *sock)
4754{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004755 return sock_has_perm(sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756}
4757
Eric Paris828dfe12008-04-17 13:17:49 -04004758static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759{
Paul Mooref8687af2006-10-30 15:22:15 -08004760 int err;
4761
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004762 err = sock_has_perm(sock->sk, SOCKET__SETOPT);
Paul Mooref8687af2006-10-30 15:22:15 -08004763 if (err)
4764 return err;
4765
4766 return selinux_netlbl_socket_setsockopt(sock, level, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767}
4768
4769static int selinux_socket_getsockopt(struct socket *sock, int level,
4770 int optname)
4771{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004772 return sock_has_perm(sock->sk, SOCKET__GETOPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773}
4774
4775static int selinux_socket_shutdown(struct socket *sock, int how)
4776{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004777 return sock_has_perm(sock->sk, SOCKET__SHUTDOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778}
4779
David S. Miller3610cda2011-01-05 15:38:53 -08004780static int selinux_socket_unix_stream_connect(struct sock *sock,
4781 struct sock *other,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 struct sock *newsk)
4783{
David S. Miller3610cda2011-01-05 15:38:53 -08004784 struct sk_security_struct *sksec_sock = sock->sk_security;
4785 struct sk_security_struct *sksec_other = other->sk_security;
Paul Moore4d1e2452010-04-22 14:46:18 -04004786 struct sk_security_struct *sksec_new = newsk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004787 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004788 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789 int err;
4790
Eric Paris50c205f2012-04-04 15:01:43 -04004791 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004792 ad.u.net = &net;
4793 ad.u.net->sk = other;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004795 err = avc_has_perm(&selinux_state,
4796 sksec_sock->sid, sksec_other->sid,
Paul Moore4d1e2452010-04-22 14:46:18 -04004797 sksec_other->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 UNIX_STREAM_SOCKET__CONNECTTO, &ad);
4799 if (err)
4800 return err;
4801
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802 /* server child socket */
Paul Moore4d1e2452010-04-22 14:46:18 -04004803 sksec_new->peer_sid = sksec_sock->sid;
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004804 err = security_sid_mls_copy(&selinux_state, sksec_other->sid,
4805 sksec_sock->sid, &sksec_new->sid);
Paul Moore4d1e2452010-04-22 14:46:18 -04004806 if (err)
4807 return err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004808
Paul Moore4d1e2452010-04-22 14:46:18 -04004809 /* connecting socket */
4810 sksec_sock->peer_sid = sksec_new->sid;
4811
4812 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813}
4814
4815static int selinux_socket_unix_may_send(struct socket *sock,
4816 struct socket *other)
4817{
Paul Moore253bfae2010-04-22 14:46:19 -04004818 struct sk_security_struct *ssec = sock->sk->sk_security;
4819 struct sk_security_struct *osec = other->sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004820 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004821 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822
Eric Paris50c205f2012-04-04 15:01:43 -04004823 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004824 ad.u.net = &net;
4825 ad.u.net->sk = other->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004827 return avc_has_perm(&selinux_state,
4828 ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
Paul Moore253bfae2010-04-22 14:46:19 -04004829 &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830}
4831
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004832static int selinux_inet_sys_rcv_skb(struct net *ns, int ifindex,
4833 char *addrp, u16 family, u32 peer_sid,
Thomas Liu2bf49692009-07-14 12:14:09 -04004834 struct common_audit_data *ad)
Paul Mooreeffad8d2008-01-29 08:49:27 -05004835{
4836 int err;
4837 u32 if_sid;
4838 u32 node_sid;
4839
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004840 err = sel_netif_sid(ns, ifindex, &if_sid);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004841 if (err)
4842 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004843 err = avc_has_perm(&selinux_state,
4844 peer_sid, if_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004845 SECCLASS_NETIF, NETIF__INGRESS, ad);
4846 if (err)
4847 return err;
4848
4849 err = sel_netnode_sid(addrp, family, &node_sid);
4850 if (err)
4851 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004852 return avc_has_perm(&selinux_state,
4853 peer_sid, node_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004854 SECCLASS_NODE, NODE__RECVFROM, ad);
4855}
4856
Paul Moore220deb92008-01-29 08:38:23 -05004857static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
Paul Moored8395c82008-10-10 10:16:30 -04004858 u16 family)
Paul Moore220deb92008-01-29 08:38:23 -05004859{
Paul Moore277d3422008-12-31 12:54:11 -05004860 int err = 0;
Paul Moore220deb92008-01-29 08:38:23 -05004861 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004862 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004863 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004864 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04004865 char *addrp;
4866
Eric Paris50c205f2012-04-04 15:01:43 -04004867 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004868 ad.u.net = &net;
4869 ad.u.net->netif = skb->skb_iif;
4870 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004871 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4872 if (err)
4873 return err;
Paul Moore220deb92008-01-29 08:38:23 -05004874
Paul Moore58bfbb52009-03-27 17:10:41 -04004875 if (selinux_secmark_enabled()) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004876 err = avc_has_perm(&selinux_state,
4877 sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Moored8395c82008-10-10 10:16:30 -04004878 PACKET__RECV, &ad);
Paul Moore58bfbb52009-03-27 17:10:41 -04004879 if (err)
4880 return err;
4881 }
Paul Moore220deb92008-01-29 08:38:23 -05004882
Steffen Klassertb9679a72011-02-23 12:55:21 +01004883 err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
4884 if (err)
4885 return err;
4886 err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004887
James Morris4e5ab4c2006-06-09 00:33:33 -07004888 return err;
4889}
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004890
James Morris4e5ab4c2006-06-09 00:33:33 -07004891static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
4892{
Paul Moore220deb92008-01-29 08:38:23 -05004893 int err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004894 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004895 u16 family = sk->sk_family;
4896 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004897 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004898 struct lsm_network_audit net = {0,};
Paul Moore220deb92008-01-29 08:38:23 -05004899 char *addrp;
Paul Moored8395c82008-10-10 10:16:30 -04004900 u8 secmark_active;
4901 u8 peerlbl_active;
James Morris4e5ab4c2006-06-09 00:33:33 -07004902
James Morris4e5ab4c2006-06-09 00:33:33 -07004903 if (family != PF_INET && family != PF_INET6)
Paul Moore220deb92008-01-29 08:38:23 -05004904 return 0;
James Morris4e5ab4c2006-06-09 00:33:33 -07004905
4906 /* Handle mapped IPv4 packets arriving via IPv6 sockets */
Al Viro87fcd702006-12-04 22:00:55 +00004907 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
James Morris4e5ab4c2006-06-09 00:33:33 -07004908 family = PF_INET;
4909
Paul Moored8395c82008-10-10 10:16:30 -04004910 /* If any sort of compatibility mode is enabled then handoff processing
4911 * to the selinux_sock_rcv_skb_compat() function to deal with the
4912 * special handling. We do this in an attempt to keep this function
4913 * as fast and as clean as possible. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004914 if (!selinux_policycap_netpeer())
Paul Moored8395c82008-10-10 10:16:30 -04004915 return selinux_sock_rcv_skb_compat(sk, skb, family);
4916
4917 secmark_active = selinux_secmark_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04004918 peerlbl_active = selinux_peerlbl_enabled();
Paul Moored8395c82008-10-10 10:16:30 -04004919 if (!secmark_active && !peerlbl_active)
4920 return 0;
4921
Eric Paris50c205f2012-04-04 15:01:43 -04004922 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004923 ad.u.net = &net;
4924 ad.u.net->netif = skb->skb_iif;
4925 ad.u.net->family = family;
Paul Moore224dfbd2008-01-29 08:38:13 -05004926 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
James Morris4e5ab4c2006-06-09 00:33:33 -07004927 if (err)
Paul Moore220deb92008-01-29 08:38:23 -05004928 return err;
James Morris4e5ab4c2006-06-09 00:33:33 -07004929
Paul Moored8395c82008-10-10 10:16:30 -04004930 if (peerlbl_active) {
Paul Moored621d352008-01-29 08:43:36 -05004931 u32 peer_sid;
4932
4933 err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
4934 if (err)
4935 return err;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004936 err = selinux_inet_sys_rcv_skb(sock_net(sk), skb->skb_iif,
4937 addrp, family, peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04004938 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04004939 selinux_netlbl_err(skb, family, err, 0);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004940 return err;
Paul Mooredfaebe92008-10-10 10:16:31 -04004941 }
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004942 err = avc_has_perm(&selinux_state,
4943 sk_sid, peer_sid, SECCLASS_PEER,
Paul Moored621d352008-01-29 08:43:36 -05004944 PEER__RECV, &ad);
Chad Hanson46d01d62013-12-23 17:45:01 -05004945 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04004946 selinux_netlbl_err(skb, family, err, 0);
Chad Hanson46d01d62013-12-23 17:45:01 -05004947 return err;
4948 }
Paul Moored621d352008-01-29 08:43:36 -05004949 }
4950
Paul Moored8395c82008-10-10 10:16:30 -04004951 if (secmark_active) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004952 err = avc_has_perm(&selinux_state,
4953 sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004954 PACKET__RECV, &ad);
4955 if (err)
4956 return err;
4957 }
4958
Paul Moored621d352008-01-29 08:43:36 -05004959 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960}
4961
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004962static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
4963 int __user *optlen, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964{
4965 int err = 0;
4966 char *scontext;
4967 u32 scontext_len;
Paul Moore253bfae2010-04-22 14:46:19 -04004968 struct sk_security_struct *sksec = sock->sk->sk_security;
Paul Moore3de4bab2006-11-17 17:38:54 -05004969 u32 peer_sid = SECSID_NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970
Paul Moore253bfae2010-04-22 14:46:19 -04004971 if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
Richard Hainesd4529302018-02-13 20:57:18 +00004972 sksec->sclass == SECCLASS_TCP_SOCKET ||
4973 sksec->sclass == SECCLASS_SCTP_SOCKET)
Eric Parisdd3e7832010-04-07 15:08:46 -04004974 peer_sid = sksec->peer_sid;
Paul Moore253bfae2010-04-22 14:46:19 -04004975 if (peer_sid == SECSID_NULL)
4976 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004978 err = security_sid_to_context(&selinux_state, peer_sid, &scontext,
4979 &scontext_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 if (err)
Paul Moore253bfae2010-04-22 14:46:19 -04004981 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982
4983 if (scontext_len > len) {
4984 err = -ERANGE;
4985 goto out_len;
4986 }
4987
4988 if (copy_to_user(optval, scontext, scontext_len))
4989 err = -EFAULT;
4990
4991out_len:
4992 if (put_user(scontext_len, optlen))
4993 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994 kfree(scontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 return err;
4996}
4997
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07004998static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004999{
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005000 u32 peer_secid = SECSID_NULL;
Paul Moore75e22912008-01-29 08:38:04 -05005001 u16 family;
Paul Moore899134f2016-03-28 15:19:10 -04005002 struct inode_security_struct *isec;
Catherine Zhang877ce7c2006-06-29 12:27:47 -07005003
Paul Mooreaa862902008-10-10 10:16:29 -04005004 if (skb && skb->protocol == htons(ETH_P_IP))
5005 family = PF_INET;
5006 else if (skb && skb->protocol == htons(ETH_P_IPV6))
5007 family = PF_INET6;
5008 else if (sock)
Paul Moore75e22912008-01-29 08:38:04 -05005009 family = sock->sk->sk_family;
Paul Moore75e22912008-01-29 08:38:04 -05005010 else
5011 goto out;
5012
Paul Moore899134f2016-03-28 15:19:10 -04005013 if (sock && family == PF_UNIX) {
5014 isec = inode_security_novalidate(SOCK_INODE(sock));
5015 peer_secid = isec->sid;
5016 } else if (skb)
Paul Moore220deb92008-01-29 08:38:23 -05005017 selinux_skb_peerlbl_sid(skb, family, &peer_secid);
Catherine Zhang2c7946a2006-03-20 22:41:23 -08005018
Paul Moore75e22912008-01-29 08:38:04 -05005019out:
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005020 *secid = peer_secid;
Paul Moore75e22912008-01-29 08:38:04 -05005021 if (peer_secid == SECSID_NULL)
5022 return -EINVAL;
5023 return 0;
Catherine Zhang2c7946a2006-03-20 22:41:23 -08005024}
5025
Al Viro7d877f32005-10-21 03:20:43 -04005026static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027{
Paul Moore84914b72010-04-22 14:46:18 -04005028 struct sk_security_struct *sksec;
5029
5030 sksec = kzalloc(sizeof(*sksec), priority);
5031 if (!sksec)
5032 return -ENOMEM;
5033
5034 sksec->peer_sid = SECINITSID_UNLABELED;
5035 sksec->sid = SECINITSID_UNLABELED;
Stephen Smalley5dee25d2015-07-10 17:19:57 -04005036 sksec->sclass = SECCLASS_SOCKET;
Paul Moore84914b72010-04-22 14:46:18 -04005037 selinux_netlbl_sk_security_reset(sksec);
5038 sk->sk_security = sksec;
5039
5040 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041}
5042
5043static void selinux_sk_free_security(struct sock *sk)
5044{
Paul Moore84914b72010-04-22 14:46:18 -04005045 struct sk_security_struct *sksec = sk->sk_security;
5046
5047 sk->sk_security = NULL;
5048 selinux_netlbl_sk_security_free(sksec);
5049 kfree(sksec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050}
5051
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005052static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
5053{
Eric Parisdd3e7832010-04-07 15:08:46 -04005054 struct sk_security_struct *sksec = sk->sk_security;
5055 struct sk_security_struct *newsksec = newsk->sk_security;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005056
Eric Parisdd3e7832010-04-07 15:08:46 -04005057 newsksec->sid = sksec->sid;
5058 newsksec->peer_sid = sksec->peer_sid;
5059 newsksec->sclass = sksec->sclass;
Paul Moore99f59ed2006-08-29 17:53:48 -07005060
Eric Parisdd3e7832010-04-07 15:08:46 -04005061 selinux_netlbl_sk_security_reset(newsksec);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005062}
5063
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07005064static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005065{
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005066 if (!sk)
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07005067 *secid = SECINITSID_ANY_SOCKET;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005068 else {
5069 struct sk_security_struct *sksec = sk->sk_security;
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005070
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07005071 *secid = sksec->sid;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005072 }
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005073}
5074
Eric Paris828dfe12008-04-17 13:17:49 -04005075static void selinux_sock_graft(struct sock *sk, struct socket *parent)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005076{
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05005077 struct inode_security_struct *isec =
5078 inode_security_novalidate(SOCK_INODE(parent));
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005079 struct sk_security_struct *sksec = sk->sk_security;
5080
Paul Moore2873ead2014-07-28 10:42:48 -04005081 if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
5082 sk->sk_family == PF_UNIX)
David Woodhouse2148ccc2006-09-29 15:50:25 -07005083 isec->sid = sksec->sid;
Paul Moore220deb92008-01-29 08:38:23 -05005084 sksec->sclass = isec->sclass;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005085}
5086
Richard Hainesd4529302018-02-13 20:57:18 +00005087/* Called whenever SCTP receives an INIT chunk. This happens when an incoming
5088 * connect(2), sctp_connectx(3) or sctp_sendmsg(3) (with no association
5089 * already present).
5090 */
5091static int selinux_sctp_assoc_request(struct sctp_endpoint *ep,
5092 struct sk_buff *skb)
5093{
5094 struct sk_security_struct *sksec = ep->base.sk->sk_security;
5095 struct common_audit_data ad;
5096 struct lsm_network_audit net = {0,};
5097 u8 peerlbl_active;
5098 u32 peer_sid = SECINITSID_UNLABELED;
5099 u32 conn_sid;
5100 int err = 0;
5101
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005102 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005103 return 0;
5104
5105 peerlbl_active = selinux_peerlbl_enabled();
5106
5107 if (peerlbl_active) {
5108 /* This will return peer_sid = SECSID_NULL if there are
5109 * no peer labels, see security_net_peersid_resolve().
5110 */
5111 err = selinux_skb_peerlbl_sid(skb, ep->base.sk->sk_family,
5112 &peer_sid);
5113 if (err)
5114 return err;
5115
5116 if (peer_sid == SECSID_NULL)
5117 peer_sid = SECINITSID_UNLABELED;
5118 }
5119
5120 if (sksec->sctp_assoc_state == SCTP_ASSOC_UNSET) {
5121 sksec->sctp_assoc_state = SCTP_ASSOC_SET;
5122
5123 /* Here as first association on socket. As the peer SID
5124 * was allowed by peer recv (and the netif/node checks),
5125 * then it is approved by policy and used as the primary
5126 * peer SID for getpeercon(3).
5127 */
5128 sksec->peer_sid = peer_sid;
5129 } else if (sksec->peer_sid != peer_sid) {
5130 /* Other association peer SIDs are checked to enforce
5131 * consistency among the peer SIDs.
5132 */
5133 ad.type = LSM_AUDIT_DATA_NET;
5134 ad.u.net = &net;
5135 ad.u.net->sk = ep->base.sk;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005136 err = avc_has_perm(&selinux_state,
5137 sksec->peer_sid, peer_sid, sksec->sclass,
Richard Hainesd4529302018-02-13 20:57:18 +00005138 SCTP_SOCKET__ASSOCIATION, &ad);
5139 if (err)
5140 return err;
5141 }
5142
5143 /* Compute the MLS component for the connection and store
5144 * the information in ep. This will be used by SCTP TCP type
5145 * sockets and peeled off connections as they cause a new
5146 * socket to be generated. selinux_sctp_sk_clone() will then
5147 * plug this into the new socket.
5148 */
5149 err = selinux_conn_sid(sksec->sid, peer_sid, &conn_sid);
5150 if (err)
5151 return err;
5152
5153 ep->secid = conn_sid;
5154 ep->peer_secid = peer_sid;
5155
5156 /* Set any NetLabel labels including CIPSO/CALIPSO options. */
5157 return selinux_netlbl_sctp_assoc_request(ep, skb);
5158}
5159
5160/* Check if sctp IPv4/IPv6 addresses are valid for binding or connecting
5161 * based on their @optname.
5162 */
5163static int selinux_sctp_bind_connect(struct sock *sk, int optname,
5164 struct sockaddr *address,
5165 int addrlen)
5166{
5167 int len, err = 0, walk_size = 0;
5168 void *addr_buf;
5169 struct sockaddr *addr;
5170 struct socket *sock;
5171
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005172 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005173 return 0;
5174
5175 /* Process one or more addresses that may be IPv4 or IPv6 */
5176 sock = sk->sk_socket;
5177 addr_buf = address;
5178
5179 while (walk_size < addrlen) {
Ondrej Mosnacekc1383252018-11-13 16:16:08 +01005180 if (walk_size + sizeof(sa_family_t) > addrlen)
5181 return -EINVAL;
5182
Richard Hainesd4529302018-02-13 20:57:18 +00005183 addr = addr_buf;
5184 switch (addr->sa_family) {
Alexey Kodanev4152dc92018-05-11 20:15:13 +03005185 case AF_UNSPEC:
Richard Hainesd4529302018-02-13 20:57:18 +00005186 case AF_INET:
5187 len = sizeof(struct sockaddr_in);
5188 break;
5189 case AF_INET6:
5190 len = sizeof(struct sockaddr_in6);
5191 break;
5192 default:
Alexey Kodanev4152dc92018-05-11 20:15:13 +03005193 return -EINVAL;
Richard Hainesd4529302018-02-13 20:57:18 +00005194 }
5195
5196 err = -EINVAL;
5197 switch (optname) {
5198 /* Bind checks */
5199 case SCTP_PRIMARY_ADDR:
5200 case SCTP_SET_PEER_PRIMARY_ADDR:
5201 case SCTP_SOCKOPT_BINDX_ADD:
5202 err = selinux_socket_bind(sock, addr, len);
5203 break;
5204 /* Connect checks */
5205 case SCTP_SOCKOPT_CONNECTX:
5206 case SCTP_PARAM_SET_PRIMARY:
5207 case SCTP_PARAM_ADD_IP:
5208 case SCTP_SENDMSG_CONNECT:
5209 err = selinux_socket_connect_helper(sock, addr, len);
5210 if (err)
5211 return err;
5212
5213 /* As selinux_sctp_bind_connect() is called by the
5214 * SCTP protocol layer, the socket is already locked,
5215 * therefore selinux_netlbl_socket_connect_locked() is
5216 * is called here. The situations handled are:
5217 * sctp_connectx(3), sctp_sendmsg(3), sendmsg(2),
5218 * whenever a new IP address is added or when a new
5219 * primary address is selected.
5220 * Note that an SCTP connect(2) call happens before
5221 * the SCTP protocol layer and is handled via
5222 * selinux_socket_connect().
5223 */
5224 err = selinux_netlbl_socket_connect_locked(sk, addr);
5225 break;
5226 }
5227
5228 if (err)
5229 return err;
5230
5231 addr_buf += len;
5232 walk_size += len;
5233 }
5234
5235 return 0;
5236}
5237
5238/* Called whenever a new socket is created by accept(2) or sctp_peeloff(3). */
5239static void selinux_sctp_sk_clone(struct sctp_endpoint *ep, struct sock *sk,
5240 struct sock *newsk)
5241{
5242 struct sk_security_struct *sksec = sk->sk_security;
5243 struct sk_security_struct *newsksec = newsk->sk_security;
5244
5245 /* If policy does not support SECCLASS_SCTP_SOCKET then call
5246 * the non-sctp clone version.
5247 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005248 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005249 return selinux_sk_clone_security(sk, newsk);
5250
5251 newsksec->sid = ep->secid;
5252 newsksec->peer_sid = ep->peer_secid;
5253 newsksec->sclass = sksec->sclass;
5254 selinux_netlbl_sctp_sk_clone(sk, newsk);
5255}
5256
Adrian Bunk9a673e52006-08-15 00:03:53 -07005257static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
5258 struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005259{
5260 struct sk_security_struct *sksec = sk->sk_security;
5261 int err;
Paul Moore0b1f24e2013-12-03 11:39:13 -05005262 u16 family = req->rsk_ops->family;
Paul Moore446b8022013-12-04 16:10:51 -05005263 u32 connsid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005264 u32 peersid;
5265
Paul Mooreaa862902008-10-10 10:16:29 -04005266 err = selinux_skb_peerlbl_sid(skb, family, &peersid);
Paul Moore220deb92008-01-29 08:38:23 -05005267 if (err)
5268 return err;
Paul Moore446b8022013-12-04 16:10:51 -05005269 err = selinux_conn_sid(sksec->sid, peersid, &connsid);
5270 if (err)
5271 return err;
5272 req->secid = connsid;
5273 req->peer_secid = peersid;
Venkat Yekkiralaa51c64f2006-07-27 22:01:34 -07005274
Paul Moore389fb8002009-03-27 17:10:34 -04005275 return selinux_netlbl_inet_conn_request(req, family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005276}
5277
Adrian Bunk9a673e52006-08-15 00:03:53 -07005278static void selinux_inet_csk_clone(struct sock *newsk,
5279 const struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005280{
5281 struct sk_security_struct *newsksec = newsk->sk_security;
5282
5283 newsksec->sid = req->secid;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005284 newsksec->peer_sid = req->peer_secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005285 /* NOTE: Ideally, we should also get the isec->sid for the
5286 new socket in sync, but we don't have the isec available yet.
5287 So we will wait until sock_graft to do it, by which
5288 time it will have been created and available. */
Paul Moore99f59ed2006-08-29 17:53:48 -07005289
Paul Moore9f2ad662006-11-17 17:38:53 -05005290 /* We don't need to take any sort of lock here as we are the only
5291 * thread with access to newsksec */
Paul Moore389fb8002009-03-27 17:10:34 -04005292 selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005293}
5294
Paul Moore014ab192008-10-10 10:16:33 -04005295static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005296{
Paul Mooreaa862902008-10-10 10:16:29 -04005297 u16 family = sk->sk_family;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005298 struct sk_security_struct *sksec = sk->sk_security;
5299
Paul Mooreaa862902008-10-10 10:16:29 -04005300 /* handle mapped IPv4 packets arriving via IPv6 sockets */
5301 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
5302 family = PF_INET;
5303
5304 selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005305}
5306
Eric Paris2606fd12010-10-13 16:24:41 -04005307static int selinux_secmark_relabel_packet(u32 sid)
5308{
5309 const struct task_security_struct *__tsec;
5310 u32 tsid;
5311
5312 __tsec = current_security();
5313 tsid = __tsec->sid;
5314
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005315 return avc_has_perm(&selinux_state,
5316 tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO,
5317 NULL);
Eric Paris2606fd12010-10-13 16:24:41 -04005318}
5319
5320static void selinux_secmark_refcount_inc(void)
5321{
5322 atomic_inc(&selinux_secmark_refcount);
5323}
5324
5325static void selinux_secmark_refcount_dec(void)
5326{
5327 atomic_dec(&selinux_secmark_refcount);
5328}
5329
Adrian Bunk9a673e52006-08-15 00:03:53 -07005330static void selinux_req_classify_flow(const struct request_sock *req,
5331 struct flowi *fl)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005332{
David S. Miller1d28f422011-03-12 00:29:39 -05005333 fl->flowi_secid = req->secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005334}
5335
Paul Moore5dbbaf22013-01-14 07:12:19 +00005336static int selinux_tun_dev_alloc_security(void **security)
5337{
5338 struct tun_security_struct *tunsec;
5339
5340 tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL);
5341 if (!tunsec)
5342 return -ENOMEM;
5343 tunsec->sid = current_sid();
5344
5345 *security = tunsec;
5346 return 0;
5347}
5348
5349static void selinux_tun_dev_free_security(void *security)
5350{
5351 kfree(security);
5352}
5353
Paul Mooreed6d76e2009-08-28 18:12:49 -04005354static int selinux_tun_dev_create(void)
5355{
5356 u32 sid = current_sid();
5357
5358 /* we aren't taking into account the "sockcreate" SID since the socket
5359 * that is being created here is not a socket in the traditional sense,
5360 * instead it is a private sock, accessible only to the kernel, and
5361 * representing a wide range of network traffic spanning multiple
5362 * connections unlike traditional sockets - check the TUN driver to
5363 * get a better understanding of why this socket is special */
5364
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005365 return avc_has_perm(&selinux_state,
5366 sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005367 NULL);
5368}
5369
Paul Moore5dbbaf22013-01-14 07:12:19 +00005370static int selinux_tun_dev_attach_queue(void *security)
Paul Mooreed6d76e2009-08-28 18:12:49 -04005371{
Paul Moore5dbbaf22013-01-14 07:12:19 +00005372 struct tun_security_struct *tunsec = security;
5373
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005374 return avc_has_perm(&selinux_state,
5375 current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET,
Paul Moore5dbbaf22013-01-14 07:12:19 +00005376 TUN_SOCKET__ATTACH_QUEUE, NULL);
5377}
5378
5379static int selinux_tun_dev_attach(struct sock *sk, void *security)
5380{
5381 struct tun_security_struct *tunsec = security;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005382 struct sk_security_struct *sksec = sk->sk_security;
5383
5384 /* we don't currently perform any NetLabel based labeling here and it
5385 * isn't clear that we would want to do so anyway; while we could apply
5386 * labeling without the support of the TUN user the resulting labeled
5387 * traffic from the other end of the connection would almost certainly
5388 * cause confusion to the TUN user that had no idea network labeling
5389 * protocols were being used */
5390
Paul Moore5dbbaf22013-01-14 07:12:19 +00005391 sksec->sid = tunsec->sid;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005392 sksec->sclass = SECCLASS_TUN_SOCKET;
Paul Moore5dbbaf22013-01-14 07:12:19 +00005393
5394 return 0;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005395}
5396
Paul Moore5dbbaf22013-01-14 07:12:19 +00005397static int selinux_tun_dev_open(void *security)
Paul Mooreed6d76e2009-08-28 18:12:49 -04005398{
Paul Moore5dbbaf22013-01-14 07:12:19 +00005399 struct tun_security_struct *tunsec = security;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005400 u32 sid = current_sid();
5401 int err;
5402
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005403 err = avc_has_perm(&selinux_state,
5404 sid, tunsec->sid, SECCLASS_TUN_SOCKET,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005405 TUN_SOCKET__RELABELFROM, NULL);
5406 if (err)
5407 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005408 err = avc_has_perm(&selinux_state,
5409 sid, sid, SECCLASS_TUN_SOCKET,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005410 TUN_SOCKET__RELABELTO, NULL);
5411 if (err)
5412 return err;
Paul Moore5dbbaf22013-01-14 07:12:19 +00005413 tunsec->sid = sid;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005414
5415 return 0;
5416}
5417
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
5419{
5420 int err = 0;
5421 u32 perm;
5422 struct nlmsghdr *nlh;
Paul Moore253bfae2010-04-22 14:46:19 -04005423 struct sk_security_struct *sksec = sk->sk_security;
Eric Paris828dfe12008-04-17 13:17:49 -04005424
Hong zhi guo77954982013-03-27 06:49:35 +00005425 if (skb->len < NLMSG_HDRLEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426 err = -EINVAL;
5427 goto out;
5428 }
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -07005429 nlh = nlmsg_hdr(skb);
Eric Paris828dfe12008-04-17 13:17:49 -04005430
Paul Moore253bfae2010-04-22 14:46:19 -04005431 err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432 if (err) {
5433 if (err == -EINVAL) {
Vladis Dronov76319942015-12-24 11:09:41 -05005434 pr_warn_ratelimited("SELinux: unrecognized netlink"
5435 " message: protocol=%hu nlmsg_type=%hu sclass=%s"
5436 " pig=%d comm=%s\n",
Marek Milkoviccded3ff2015-06-04 16:22:16 -04005437 sk->sk_protocol, nlh->nlmsg_type,
Vladis Dronov76319942015-12-24 11:09:41 -05005438 secclass_map[sksec->sclass - 1].name,
5439 task_pid_nr(current), current->comm);
Paul Mooree5a5ca92018-03-01 17:38:30 -05005440 if (!enforcing_enabled(&selinux_state) ||
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005441 security_get_allow_unknown(&selinux_state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 err = 0;
5443 }
5444
5445 /* Ignore */
5446 if (err == -ENOENT)
5447 err = 0;
5448 goto out;
5449 }
5450
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005451 err = sock_has_perm(sk, perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452out:
5453 return err;
5454}
5455
5456#ifdef CONFIG_NETFILTER
5457
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005458static unsigned int selinux_ip_forward(struct sk_buff *skb,
5459 const struct net_device *indev,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005460 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461{
Paul Mooredfaebe92008-10-10 10:16:31 -04005462 int err;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005463 char *addrp;
5464 u32 peer_sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04005465 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005466 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05005467 u8 secmark_active;
Paul Moore948bf852008-10-10 10:16:32 -04005468 u8 netlbl_active;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005469 u8 peerlbl_active;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005470
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005471 if (!selinux_policycap_netpeer())
Paul Mooreeffad8d2008-01-29 08:49:27 -05005472 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005473
Paul Mooreeffad8d2008-01-29 08:49:27 -05005474 secmark_active = selinux_secmark_enabled();
Paul Moore948bf852008-10-10 10:16:32 -04005475 netlbl_active = netlbl_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04005476 peerlbl_active = selinux_peerlbl_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05005477 if (!secmark_active && !peerlbl_active)
5478 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005479
Paul Moored8395c82008-10-10 10:16:30 -04005480 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
5481 return NF_DROP;
5482
Eric Paris50c205f2012-04-04 15:01:43 -04005483 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005484 ad.u.net = &net;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005485 ad.u.net->netif = indev->ifindex;
Eric Paris48c62af2012-04-02 13:15:44 -04005486 ad.u.net->family = family;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005487 if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
5488 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489
Paul Mooredfaebe92008-10-10 10:16:31 -04005490 if (peerlbl_active) {
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005491 err = selinux_inet_sys_rcv_skb(dev_net(indev), indev->ifindex,
5492 addrp, family, peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04005493 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04005494 selinux_netlbl_err(skb, family, err, 1);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005495 return NF_DROP;
Paul Mooredfaebe92008-10-10 10:16:31 -04005496 }
5497 }
Paul Mooreeffad8d2008-01-29 08:49:27 -05005498
5499 if (secmark_active)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005500 if (avc_has_perm(&selinux_state,
5501 peer_sid, skb->secmark,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005502 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
5503 return NF_DROP;
5504
Paul Moore948bf852008-10-10 10:16:32 -04005505 if (netlbl_active)
5506 /* we do this in the FORWARD path and not the POST_ROUTING
5507 * path because we want to make sure we apply the necessary
5508 * labeling before IPsec is applied so we can leverage AH
5509 * protection */
5510 if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
5511 return NF_DROP;
5512
Paul Mooreeffad8d2008-01-29 08:49:27 -05005513 return NF_ACCEPT;
5514}
5515
Eric W. Biederman06198b32015-09-18 14:33:06 -05005516static unsigned int selinux_ipv4_forward(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005517 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005518 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005519{
David S. Miller238e54c2015-04-03 20:32:56 -04005520 return selinux_ip_forward(skb, state->in, PF_INET);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005521}
5522
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005523#if IS_ENABLED(CONFIG_IPV6)
Eric W. Biederman06198b32015-09-18 14:33:06 -05005524static unsigned int selinux_ipv6_forward(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005525 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005526 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005527{
David S. Miller238e54c2015-04-03 20:32:56 -04005528 return selinux_ip_forward(skb, state->in, PF_INET6);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005529}
5530#endif /* IPV6 */
5531
Paul Moore948bf852008-10-10 10:16:32 -04005532static unsigned int selinux_ip_output(struct sk_buff *skb,
5533 u16 family)
5534{
Paul Moore47180062013-12-04 16:10:45 -05005535 struct sock *sk;
Paul Moore948bf852008-10-10 10:16:32 -04005536 u32 sid;
5537
5538 if (!netlbl_enabled())
5539 return NF_ACCEPT;
5540
5541 /* we do this in the LOCAL_OUT path and not the POST_ROUTING path
5542 * because we want to make sure we apply the necessary labeling
5543 * before IPsec is applied so we can leverage AH protection */
Paul Moore47180062013-12-04 16:10:45 -05005544 sk = skb->sk;
5545 if (sk) {
5546 struct sk_security_struct *sksec;
5547
Eric Dumazete446f9d2015-10-08 05:01:55 -07005548 if (sk_listener(sk))
Paul Moore47180062013-12-04 16:10:45 -05005549 /* if the socket is the listening state then this
5550 * packet is a SYN-ACK packet which means it needs to
5551 * be labeled based on the connection/request_sock and
5552 * not the parent socket. unfortunately, we can't
5553 * lookup the request_sock yet as it isn't queued on
5554 * the parent socket until after the SYN-ACK is sent.
5555 * the "solution" is to simply pass the packet as-is
5556 * as any IP option based labeling should be copied
5557 * from the initial connection request (in the IP
5558 * layer). it is far from ideal, but until we get a
5559 * security label in the packet itself this is the
5560 * best we can do. */
5561 return NF_ACCEPT;
5562
5563 /* standard practice, label using the parent socket */
5564 sksec = sk->sk_security;
Paul Moore948bf852008-10-10 10:16:32 -04005565 sid = sksec->sid;
5566 } else
5567 sid = SECINITSID_KERNEL;
5568 if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
5569 return NF_DROP;
5570
5571 return NF_ACCEPT;
5572}
5573
Eric W. Biederman06198b32015-09-18 14:33:06 -05005574static unsigned int selinux_ipv4_output(void *priv,
Paul Moore948bf852008-10-10 10:16:32 -04005575 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005576 const struct nf_hook_state *state)
Paul Moore948bf852008-10-10 10:16:32 -04005577{
5578 return selinux_ip_output(skb, PF_INET);
5579}
5580
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005581#if IS_ENABLED(CONFIG_IPV6)
Huw Davies2917f572016-06-27 15:06:15 -04005582static unsigned int selinux_ipv6_output(void *priv,
5583 struct sk_buff *skb,
5584 const struct nf_hook_state *state)
5585{
5586 return selinux_ip_output(skb, PF_INET6);
5587}
5588#endif /* IPV6 */
5589
Paul Mooreeffad8d2008-01-29 08:49:27 -05005590static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
5591 int ifindex,
Paul Moored8395c82008-10-10 10:16:30 -04005592 u16 family)
James Morris4e5ab4c2006-06-09 00:33:33 -07005593{
Eric Dumazet54abc682015-11-08 10:54:07 -08005594 struct sock *sk = skb_to_full_sk(skb);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005595 struct sk_security_struct *sksec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005596 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005597 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04005598 char *addrp;
5599 u8 proto;
James Morris4e5ab4c2006-06-09 00:33:33 -07005600
Paul Mooreeffad8d2008-01-29 08:49:27 -05005601 if (sk == NULL)
5602 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005603 sksec = sk->sk_security;
James Morris4e5ab4c2006-06-09 00:33:33 -07005604
Eric Paris50c205f2012-04-04 15:01:43 -04005605 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005606 ad.u.net = &net;
5607 ad.u.net->netif = ifindex;
5608 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04005609 if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
5610 return NF_DROP;
5611
Paul Moore58bfbb52009-03-27 17:10:41 -04005612 if (selinux_secmark_enabled())
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005613 if (avc_has_perm(&selinux_state,
5614 sksec->sid, skb->secmark,
Paul Moored8395c82008-10-10 10:16:30 -04005615 SECCLASS_PACKET, PACKET__SEND, &ad))
Eric Paris2fe66ec2010-11-23 06:28:08 +00005616 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07005617
Steffen Klassertb9679a72011-02-23 12:55:21 +01005618 if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
5619 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07005620
Paul Mooreeffad8d2008-01-29 08:49:27 -05005621 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622}
5623
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005624static unsigned int selinux_ip_postroute(struct sk_buff *skb,
5625 const struct net_device *outdev,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005626 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627{
Paul Mooreeffad8d2008-01-29 08:49:27 -05005628 u32 secmark_perm;
5629 u32 peer_sid;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005630 int ifindex = outdev->ifindex;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005631 struct sock *sk;
Thomas Liu2bf49692009-07-14 12:14:09 -04005632 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005633 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05005634 char *addrp;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005635 u8 secmark_active;
5636 u8 peerlbl_active;
5637
Paul Mooreeffad8d2008-01-29 08:49:27 -05005638 /* If any sort of compatibility mode is enabled then handoff processing
5639 * to the selinux_ip_postroute_compat() function to deal with the
5640 * special handling. We do this in an attempt to keep this function
5641 * as fast and as clean as possible. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005642 if (!selinux_policycap_netpeer())
Paul Moored8395c82008-10-10 10:16:30 -04005643 return selinux_ip_postroute_compat(skb, ifindex, family);
Paul Moorec0828e52013-12-10 14:58:01 -05005644
Paul Mooreeffad8d2008-01-29 08:49:27 -05005645 secmark_active = selinux_secmark_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04005646 peerlbl_active = selinux_peerlbl_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05005647 if (!secmark_active && !peerlbl_active)
5648 return NF_ACCEPT;
5649
Eric Dumazet54abc682015-11-08 10:54:07 -08005650 sk = skb_to_full_sk(skb);
Paul Moorec0828e52013-12-10 14:58:01 -05005651
Paul Mooreeffad8d2008-01-29 08:49:27 -05005652#ifdef CONFIG_XFRM
5653 /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
5654 * packet transformation so allow the packet to pass without any checks
5655 * since we'll have another chance to perform access control checks
5656 * when the packet is on it's final way out.
5657 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
Paul Moorec0828e52013-12-10 14:58:01 -05005658 * is NULL, in this case go ahead and apply access control.
5659 * NOTE: if this is a local socket (skb->sk != NULL) that is in the
5660 * TCP listening state we cannot wait until the XFRM processing
5661 * is done as we will miss out on the SA label if we do;
5662 * unfortunately, this means more work, but it is only once per
5663 * connection. */
5664 if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL &&
Eric Dumazete446f9d2015-10-08 05:01:55 -07005665 !(sk && sk_listener(sk)))
Paul Mooreeffad8d2008-01-29 08:49:27 -05005666 return NF_ACCEPT;
5667#endif
Paul Mooreeffad8d2008-01-29 08:49:27 -05005668
Paul Moored8395c82008-10-10 10:16:30 -04005669 if (sk == NULL) {
Paul Moore446b8022013-12-04 16:10:51 -05005670 /* Without an associated socket the packet is either coming
5671 * from the kernel or it is being forwarded; check the packet
5672 * to determine which and if the packet is being forwarded
5673 * query the packet directly to determine the security label. */
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005674 if (skb->skb_iif) {
5675 secmark_perm = PACKET__FORWARD_OUT;
Paul Moored8395c82008-10-10 10:16:30 -04005676 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005677 return NF_DROP;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005678 } else {
5679 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04005680 peer_sid = SECINITSID_KERNEL;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005681 }
Eric Dumazete446f9d2015-10-08 05:01:55 -07005682 } else if (sk_listener(sk)) {
Paul Moore446b8022013-12-04 16:10:51 -05005683 /* Locally generated packet but the associated socket is in the
5684 * listening state which means this is a SYN-ACK packet. In
5685 * this particular case the correct security label is assigned
5686 * to the connection/request_sock but unfortunately we can't
5687 * query the request_sock as it isn't queued on the parent
5688 * socket until after the SYN-ACK packet is sent; the only
5689 * viable choice is to regenerate the label like we do in
5690 * selinux_inet_conn_request(). See also selinux_ip_output()
5691 * for similar problems. */
5692 u32 skb_sid;
Eric Dumazete446f9d2015-10-08 05:01:55 -07005693 struct sk_security_struct *sksec;
5694
Eric Dumazete446f9d2015-10-08 05:01:55 -07005695 sksec = sk->sk_security;
Paul Moore446b8022013-12-04 16:10:51 -05005696 if (selinux_skb_peerlbl_sid(skb, family, &skb_sid))
5697 return NF_DROP;
Paul Moorec0828e52013-12-10 14:58:01 -05005698 /* At this point, if the returned skb peerlbl is SECSID_NULL
5699 * and the packet has been through at least one XFRM
5700 * transformation then we must be dealing with the "final"
5701 * form of labeled IPsec packet; since we've already applied
5702 * all of our access controls on this packet we can safely
5703 * pass the packet. */
5704 if (skb_sid == SECSID_NULL) {
5705 switch (family) {
5706 case PF_INET:
5707 if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
5708 return NF_ACCEPT;
5709 break;
5710 case PF_INET6:
5711 if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED)
5712 return NF_ACCEPT;
Paul Moorea7a91a12014-09-03 10:51:59 -04005713 break;
Paul Moorec0828e52013-12-10 14:58:01 -05005714 default:
5715 return NF_DROP_ERR(-ECONNREFUSED);
5716 }
5717 }
Paul Moore446b8022013-12-04 16:10:51 -05005718 if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid))
5719 return NF_DROP;
5720 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04005721 } else {
Paul Moore446b8022013-12-04 16:10:51 -05005722 /* Locally generated packet, fetch the security label from the
5723 * associated socket. */
Paul Mooreeffad8d2008-01-29 08:49:27 -05005724 struct sk_security_struct *sksec = sk->sk_security;
5725 peer_sid = sksec->sid;
5726 secmark_perm = PACKET__SEND;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005727 }
5728
Eric Paris50c205f2012-04-04 15:01:43 -04005729 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005730 ad.u.net = &net;
5731 ad.u.net->netif = ifindex;
5732 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04005733 if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
Eric Paris04f6d702010-11-23 06:28:02 +00005734 return NF_DROP;
Paul Moored8395c82008-10-10 10:16:30 -04005735
Paul Mooreeffad8d2008-01-29 08:49:27 -05005736 if (secmark_active)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005737 if (avc_has_perm(&selinux_state,
5738 peer_sid, skb->secmark,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005739 SECCLASS_PACKET, secmark_perm, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005740 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005741
5742 if (peerlbl_active) {
5743 u32 if_sid;
5744 u32 node_sid;
5745
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005746 if (sel_netif_sid(dev_net(outdev), ifindex, &if_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005747 return NF_DROP;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005748 if (avc_has_perm(&selinux_state,
5749 peer_sid, if_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005750 SECCLASS_NETIF, NETIF__EGRESS, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005751 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005752
5753 if (sel_netnode_sid(addrp, family, &node_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005754 return NF_DROP;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005755 if (avc_has_perm(&selinux_state,
5756 peer_sid, node_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005757 SECCLASS_NODE, NODE__SENDTO, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005758 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005759 }
5760
5761 return NF_ACCEPT;
5762}
5763
Eric W. Biederman06198b32015-09-18 14:33:06 -05005764static unsigned int selinux_ipv4_postroute(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005765 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005766 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005767{
David S. Miller238e54c2015-04-03 20:32:56 -04005768 return selinux_ip_postroute(skb, state->out, PF_INET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769}
5770
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005771#if IS_ENABLED(CONFIG_IPV6)
Eric W. Biederman06198b32015-09-18 14:33:06 -05005772static unsigned int selinux_ipv6_postroute(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005773 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005774 const struct nf_hook_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775{
David S. Miller238e54c2015-04-03 20:32:56 -04005776 return selinux_ip_postroute(skb, state->out, PF_INET6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778#endif /* IPV6 */
5779
5780#endif /* CONFIG_NETFILTER */
5781
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
5783{
Stephen Smalley941fc5b2009-10-01 14:48:23 -04005784 return selinux_nlmsg_perm(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005785}
5786
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005787static int ipc_alloc_security(struct kern_ipc_perm *perm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788 u16 sclass)
5789{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790 struct ipc_security_struct *isec;
5791
James Morris89d155e2005-10-30 14:59:21 -08005792 isec = kzalloc(sizeof(struct ipc_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793 if (!isec)
5794 return -ENOMEM;
5795
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796 isec->sclass = sclass;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005797 isec->sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798 perm->security = isec;
5799
5800 return 0;
5801}
5802
5803static void ipc_free_security(struct kern_ipc_perm *perm)
5804{
5805 struct ipc_security_struct *isec = perm->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806 perm->security = NULL;
5807 kfree(isec);
5808}
5809
5810static int msg_msg_alloc_security(struct msg_msg *msg)
5811{
5812 struct msg_security_struct *msec;
5813
James Morris89d155e2005-10-30 14:59:21 -08005814 msec = kzalloc(sizeof(struct msg_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005815 if (!msec)
5816 return -ENOMEM;
5817
Linus Torvalds1da177e2005-04-16 15:20:36 -07005818 msec->sid = SECINITSID_UNLABELED;
5819 msg->security = msec;
5820
5821 return 0;
5822}
5823
5824static void msg_msg_free_security(struct msg_msg *msg)
5825{
5826 struct msg_security_struct *msec = msg->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827
5828 msg->security = NULL;
5829 kfree(msec);
5830}
5831
5832static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
Stephen Smalley6af963f2005-05-01 08:58:39 -07005833 u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005834{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005836 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005837 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839 isec = ipc_perms->security;
5840
Eric Paris50c205f2012-04-04 15:01:43 -04005841 ad.type = LSM_AUDIT_DATA_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842 ad.u.ipc_id = ipc_perms->key;
5843
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005844 return avc_has_perm(&selinux_state,
5845 sid, isec->sid, isec->sclass, perms, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846}
5847
5848static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
5849{
5850 return msg_msg_alloc_security(msg);
5851}
5852
5853static void selinux_msg_msg_free_security(struct msg_msg *msg)
5854{
5855 msg_msg_free_security(msg);
5856}
5857
5858/* message queue security operations */
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005859static int selinux_msg_queue_alloc_security(struct kern_ipc_perm *msq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005861 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005862 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005863 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864 int rc;
5865
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005866 rc = ipc_alloc_security(msq, SECCLASS_MSGQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867 if (rc)
5868 return rc;
5869
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005870 isec = msq->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005871
Eric Paris50c205f2012-04-04 15:01:43 -04005872 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005873 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005875 rc = avc_has_perm(&selinux_state,
5876 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877 MSGQ__CREATE, &ad);
5878 if (rc) {
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005879 ipc_free_security(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880 return rc;
5881 }
5882 return 0;
5883}
5884
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005885static void selinux_msg_queue_free_security(struct kern_ipc_perm *msq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886{
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005887 ipc_free_security(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888}
5889
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005890static int selinux_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005893 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005894 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005896 isec = msq->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005897
Eric Paris50c205f2012-04-04 15:01:43 -04005898 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005899 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005901 return avc_has_perm(&selinux_state,
5902 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903 MSGQ__ASSOCIATE, &ad);
5904}
5905
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005906static int selinux_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005907{
5908 int err;
5909 int perms;
5910
Eric Paris828dfe12008-04-17 13:17:49 -04005911 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912 case IPC_INFO:
5913 case MSG_INFO:
5914 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005915 return avc_has_perm(&selinux_state,
5916 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005917 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918 case IPC_STAT:
5919 case MSG_STAT:
Davidlohr Bueso23c8cec2018-04-10 16:35:30 -07005920 case MSG_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921 perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
5922 break;
5923 case IPC_SET:
5924 perms = MSGQ__SETATTR;
5925 break;
5926 case IPC_RMID:
5927 perms = MSGQ__DESTROY;
5928 break;
5929 default:
5930 return 0;
5931 }
5932
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005933 err = ipc_has_perm(msq, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934 return err;
5935}
5936
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005937static int selinux_msg_queue_msgsnd(struct kern_ipc_perm *msq, struct msg_msg *msg, int msqflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939 struct ipc_security_struct *isec;
5940 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005941 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005942 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005943 int rc;
5944
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005945 isec = msq->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946 msec = msg->security;
5947
5948 /*
5949 * First time through, need to assign label to the message
5950 */
5951 if (msec->sid == SECINITSID_UNLABELED) {
5952 /*
5953 * Compute new sid based on current process and
5954 * message queue this message will be stored in
5955 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005956 rc = security_transition_sid(&selinux_state, sid, isec->sid,
5957 SECCLASS_MSG, NULL, &msec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005958 if (rc)
5959 return rc;
5960 }
5961
Eric Paris50c205f2012-04-04 15:01:43 -04005962 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005963 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005964
5965 /* Can this process write to the queue? */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005966 rc = avc_has_perm(&selinux_state,
5967 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005968 MSGQ__WRITE, &ad);
5969 if (!rc)
5970 /* Can this process send the message */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005971 rc = avc_has_perm(&selinux_state,
5972 sid, msec->sid, SECCLASS_MSG,
David Howells275bb412008-11-14 10:39:19 +11005973 MSG__SEND, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005974 if (!rc)
5975 /* Can the message be put in the queue? */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005976 rc = avc_has_perm(&selinux_state,
5977 msec->sid, isec->sid, SECCLASS_MSGQ,
David Howells275bb412008-11-14 10:39:19 +11005978 MSGQ__ENQUEUE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979
5980 return rc;
5981}
5982
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005983static int selinux_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984 struct task_struct *target,
5985 long type, int mode)
5986{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987 struct ipc_security_struct *isec;
5988 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005989 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005990 u32 sid = task_sid(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991 int rc;
5992
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005993 isec = msq->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005994 msec = msg->security;
5995
Eric Paris50c205f2012-04-04 15:01:43 -04005996 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005997 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005999 rc = avc_has_perm(&selinux_state,
6000 sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006001 SECCLASS_MSGQ, MSGQ__READ, &ad);
6002 if (!rc)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006003 rc = avc_has_perm(&selinux_state,
6004 sid, msec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005 SECCLASS_MSG, MSG__RECEIVE, &ad);
6006 return rc;
6007}
6008
6009/* Shared Memory security operations */
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006010static int selinux_shm_alloc_security(struct kern_ipc_perm *shp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006013 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006014 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015 int rc;
6016
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006017 rc = ipc_alloc_security(shp, SECCLASS_SHM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018 if (rc)
6019 return rc;
6020
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006021 isec = shp->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022
Eric Paris50c205f2012-04-04 15:01:43 -04006023 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006024 ad.u.ipc_id = shp->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006025
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006026 rc = avc_has_perm(&selinux_state,
6027 sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028 SHM__CREATE, &ad);
6029 if (rc) {
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006030 ipc_free_security(shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006031 return rc;
6032 }
6033 return 0;
6034}
6035
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006036static void selinux_shm_free_security(struct kern_ipc_perm *shp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006037{
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006038 ipc_free_security(shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039}
6040
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006041static int selinux_shm_associate(struct kern_ipc_perm *shp, int shmflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006042{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006043 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006044 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006045 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006046
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006047 isec = shp->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048
Eric Paris50c205f2012-04-04 15:01:43 -04006049 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006050 ad.u.ipc_id = shp->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006052 return avc_has_perm(&selinux_state,
6053 sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006054 SHM__ASSOCIATE, &ad);
6055}
6056
6057/* Note, at this point, shp is locked down */
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006058static int selinux_shm_shmctl(struct kern_ipc_perm *shp, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059{
6060 int perms;
6061 int err;
6062
Eric Paris828dfe12008-04-17 13:17:49 -04006063 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006064 case IPC_INFO:
6065 case SHM_INFO:
6066 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006067 return avc_has_perm(&selinux_state,
6068 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006069 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070 case IPC_STAT:
6071 case SHM_STAT:
Davidlohr Buesoc21a6972018-04-10 16:35:23 -07006072 case SHM_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006073 perms = SHM__GETATTR | SHM__ASSOCIATE;
6074 break;
6075 case IPC_SET:
6076 perms = SHM__SETATTR;
6077 break;
6078 case SHM_LOCK:
6079 case SHM_UNLOCK:
6080 perms = SHM__LOCK;
6081 break;
6082 case IPC_RMID:
6083 perms = SHM__DESTROY;
6084 break;
6085 default:
6086 return 0;
6087 }
6088
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006089 err = ipc_has_perm(shp, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006090 return err;
6091}
6092
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006093static int selinux_shm_shmat(struct kern_ipc_perm *shp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006094 char __user *shmaddr, int shmflg)
6095{
6096 u32 perms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097
6098 if (shmflg & SHM_RDONLY)
6099 perms = SHM__READ;
6100 else
6101 perms = SHM__READ | SHM__WRITE;
6102
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006103 return ipc_has_perm(shp, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104}
6105
6106/* Semaphore security operations */
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006107static int selinux_sem_alloc_security(struct kern_ipc_perm *sma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006108{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006109 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006110 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006111 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006112 int rc;
6113
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006114 rc = ipc_alloc_security(sma, SECCLASS_SEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006115 if (rc)
6116 return rc;
6117
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006118 isec = sma->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006119
Eric Paris50c205f2012-04-04 15:01:43 -04006120 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006121 ad.u.ipc_id = sma->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006122
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006123 rc = avc_has_perm(&selinux_state,
6124 sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006125 SEM__CREATE, &ad);
6126 if (rc) {
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006127 ipc_free_security(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006128 return rc;
6129 }
6130 return 0;
6131}
6132
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006133static void selinux_sem_free_security(struct kern_ipc_perm *sma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006134{
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006135 ipc_free_security(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006136}
6137
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006138static int selinux_sem_associate(struct kern_ipc_perm *sma, int semflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006139{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006140 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006141 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006142 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006143
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006144 isec = sma->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006145
Eric Paris50c205f2012-04-04 15:01:43 -04006146 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006147 ad.u.ipc_id = sma->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006148
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006149 return avc_has_perm(&selinux_state,
6150 sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006151 SEM__ASSOCIATE, &ad);
6152}
6153
6154/* Note, at this point, sma is locked down */
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006155static int selinux_sem_semctl(struct kern_ipc_perm *sma, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006156{
6157 int err;
6158 u32 perms;
6159
Eric Paris828dfe12008-04-17 13:17:49 -04006160 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006161 case IPC_INFO:
6162 case SEM_INFO:
6163 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006164 return avc_has_perm(&selinux_state,
6165 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006166 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006167 case GETPID:
6168 case GETNCNT:
6169 case GETZCNT:
6170 perms = SEM__GETATTR;
6171 break;
6172 case GETVAL:
6173 case GETALL:
6174 perms = SEM__READ;
6175 break;
6176 case SETVAL:
6177 case SETALL:
6178 perms = SEM__WRITE;
6179 break;
6180 case IPC_RMID:
6181 perms = SEM__DESTROY;
6182 break;
6183 case IPC_SET:
6184 perms = SEM__SETATTR;
6185 break;
6186 case IPC_STAT:
6187 case SEM_STAT:
Davidlohr Buesoa280d6d2018-04-10 16:35:26 -07006188 case SEM_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006189 perms = SEM__GETATTR | SEM__ASSOCIATE;
6190 break;
6191 default:
6192 return 0;
6193 }
6194
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006195 err = ipc_has_perm(sma, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006196 return err;
6197}
6198
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006199static int selinux_sem_semop(struct kern_ipc_perm *sma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006200 struct sembuf *sops, unsigned nsops, int alter)
6201{
6202 u32 perms;
6203
6204 if (alter)
6205 perms = SEM__READ | SEM__WRITE;
6206 else
6207 perms = SEM__READ;
6208
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006209 return ipc_has_perm(sma, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210}
6211
6212static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
6213{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006214 u32 av = 0;
6215
Linus Torvalds1da177e2005-04-16 15:20:36 -07006216 av = 0;
6217 if (flag & S_IRUGO)
6218 av |= IPC__UNIX_READ;
6219 if (flag & S_IWUGO)
6220 av |= IPC__UNIX_WRITE;
6221
6222 if (av == 0)
6223 return 0;
6224
Stephen Smalley6af963f2005-05-01 08:58:39 -07006225 return ipc_has_perm(ipcp, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006226}
6227
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02006228static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
6229{
6230 struct ipc_security_struct *isec = ipcp->security;
6231 *secid = isec->sid;
6232}
6233
Eric Paris828dfe12008-04-17 13:17:49 -04006234static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006235{
6236 if (inode)
6237 inode_doinit_with_dentry(inode, dentry);
6238}
6239
6240static int selinux_getprocattr(struct task_struct *p,
Al Viro04ff9702007-03-12 16:17:58 +00006241 char *name, char **value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006242{
David Howells275bb412008-11-14 10:39:19 +11006243 const struct task_security_struct *__tsec;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00006244 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006245 int error;
Al Viro04ff9702007-03-12 16:17:58 +00006246 unsigned len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006247
David Howells275bb412008-11-14 10:39:19 +11006248 rcu_read_lock();
6249 __tsec = __task_cred(p)->security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006251 if (current != p) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006252 error = avc_has_perm(&selinux_state,
6253 current_sid(), __tsec->sid,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006254 SECCLASS_PROCESS, PROCESS__GETATTR, NULL);
6255 if (error)
6256 goto bad;
6257 }
6258
Linus Torvalds1da177e2005-04-16 15:20:36 -07006259 if (!strcmp(name, "current"))
David Howells275bb412008-11-14 10:39:19 +11006260 sid = __tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006261 else if (!strcmp(name, "prev"))
David Howells275bb412008-11-14 10:39:19 +11006262 sid = __tsec->osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006263 else if (!strcmp(name, "exec"))
David Howells275bb412008-11-14 10:39:19 +11006264 sid = __tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006265 else if (!strcmp(name, "fscreate"))
David Howells275bb412008-11-14 10:39:19 +11006266 sid = __tsec->create_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006267 else if (!strcmp(name, "keycreate"))
David Howells275bb412008-11-14 10:39:19 +11006268 sid = __tsec->keycreate_sid;
Eric Paris42c3e032006-06-26 00:26:03 -07006269 else if (!strcmp(name, "sockcreate"))
David Howells275bb412008-11-14 10:39:19 +11006270 sid = __tsec->sockcreate_sid;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006271 else {
6272 error = -EINVAL;
6273 goto bad;
6274 }
David Howells275bb412008-11-14 10:39:19 +11006275 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006276
6277 if (!sid)
6278 return 0;
6279
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006280 error = security_sid_to_context(&selinux_state, sid, value, &len);
Al Viro04ff9702007-03-12 16:17:58 +00006281 if (error)
6282 return error;
6283 return len;
David Howells275bb412008-11-14 10:39:19 +11006284
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006285bad:
David Howells275bb412008-11-14 10:39:19 +11006286 rcu_read_unlock();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006287 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006288}
6289
Stephen Smalleyb21507e2017-01-09 10:07:31 -05006290static int selinux_setprocattr(const char *name, void *value, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006291{
6292 struct task_security_struct *tsec;
David Howellsd84f4f92008-11-14 10:39:23 +11006293 struct cred *new;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006294 u32 mysid = current_sid(), sid = 0, ptsid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006295 int error;
6296 char *str = value;
6297
Linus Torvalds1da177e2005-04-16 15:20:36 -07006298 /*
6299 * Basic control over ability to set these attributes at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006300 */
6301 if (!strcmp(name, "exec"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006302 error = avc_has_perm(&selinux_state,
6303 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006304 PROCESS__SETEXEC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006305 else if (!strcmp(name, "fscreate"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006306 error = avc_has_perm(&selinux_state,
6307 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006308 PROCESS__SETFSCREATE, NULL);
Michael LeMay4eb582c2006-06-26 00:24:57 -07006309 else if (!strcmp(name, "keycreate"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006310 error = avc_has_perm(&selinux_state,
6311 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006312 PROCESS__SETKEYCREATE, NULL);
Eric Paris42c3e032006-06-26 00:26:03 -07006313 else if (!strcmp(name, "sockcreate"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006314 error = avc_has_perm(&selinux_state,
6315 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006316 PROCESS__SETSOCKCREATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006317 else if (!strcmp(name, "current"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006318 error = avc_has_perm(&selinux_state,
6319 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006320 PROCESS__SETCURRENT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006321 else
6322 error = -EINVAL;
6323 if (error)
6324 return error;
6325
6326 /* Obtain a SID for the context, if one was specified. */
Stephen Smalleya050a572017-01-31 11:54:04 -05006327 if (size && str[0] && str[0] != '\n') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006328 if (str[size-1] == '\n') {
6329 str[size-1] = 0;
6330 size--;
6331 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006332 error = security_context_to_sid(&selinux_state, value, size,
6333 &sid, GFP_KERNEL);
Stephen Smalley12b29f32008-05-07 13:03:20 -04006334 if (error == -EINVAL && !strcmp(name, "fscreate")) {
Stephen Smalleydb590002017-04-20 11:31:30 -04006335 if (!has_cap_mac_admin(true)) {
Eric Parisd6ea83e2012-04-04 13:45:49 -04006336 struct audit_buffer *ab;
6337 size_t audit_size;
6338
6339 /* We strip a nul only if it is at the end, otherwise the
6340 * context contains a nul and we should audit that */
6341 if (str[size - 1] == '\0')
6342 audit_size = size - 1;
6343 else
6344 audit_size = size;
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04006345 ab = audit_log_start(audit_context(),
6346 GFP_ATOMIC,
6347 AUDIT_SELINUX_ERR);
Eric Parisd6ea83e2012-04-04 13:45:49 -04006348 audit_log_format(ab, "op=fscreate invalid_context=");
6349 audit_log_n_untrustedstring(ab, value, audit_size);
6350 audit_log_end(ab);
6351
Stephen Smalley12b29f32008-05-07 13:03:20 -04006352 return error;
Eric Parisd6ea83e2012-04-04 13:45:49 -04006353 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006354 error = security_context_to_sid_force(
6355 &selinux_state,
6356 value, size, &sid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04006357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006358 if (error)
6359 return error;
6360 }
6361
David Howellsd84f4f92008-11-14 10:39:23 +11006362 new = prepare_creds();
6363 if (!new)
6364 return -ENOMEM;
6365
Linus Torvalds1da177e2005-04-16 15:20:36 -07006366 /* Permission checking based on the specified context is
6367 performed during the actual operation (execve,
6368 open/mkdir/...), when we know the full context of the
David Howellsd84f4f92008-11-14 10:39:23 +11006369 operation. See selinux_bprm_set_creds for the execve
Linus Torvalds1da177e2005-04-16 15:20:36 -07006370 checks and may_create for the file creation checks. The
6371 operation will then fail if the context is not permitted. */
David Howellsd84f4f92008-11-14 10:39:23 +11006372 tsec = new->security;
6373 if (!strcmp(name, "exec")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006374 tsec->exec_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006375 } else if (!strcmp(name, "fscreate")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006376 tsec->create_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006377 } else if (!strcmp(name, "keycreate")) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006378 error = avc_has_perm(&selinux_state,
6379 mysid, sid, SECCLASS_KEY, KEY__CREATE,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006380 NULL);
Michael LeMay4eb582c2006-06-26 00:24:57 -07006381 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11006382 goto abort_change;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006383 tsec->keycreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006384 } else if (!strcmp(name, "sockcreate")) {
Eric Paris42c3e032006-06-26 00:26:03 -07006385 tsec->sockcreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006386 } else if (!strcmp(name, "current")) {
6387 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006388 if (sid == 0)
David Howellsd84f4f92008-11-14 10:39:23 +11006389 goto abort_change;
KaiGai Koheid9250de2008-08-28 16:35:57 +09006390
David Howellsd84f4f92008-11-14 10:39:23 +11006391 /* Only allow single threaded processes to change context */
6392 error = -EPERM;
Oleg Nesterov5bb459b2009-07-10 03:48:23 +02006393 if (!current_is_single_threaded()) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006394 error = security_bounded_transition(&selinux_state,
6395 tsec->sid, sid);
David Howellsd84f4f92008-11-14 10:39:23 +11006396 if (error)
6397 goto abort_change;
Eric Paris828dfe12008-04-17 13:17:49 -04006398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006399
6400 /* Check permissions for the transition. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006401 error = avc_has_perm(&selinux_state,
6402 tsec->sid, sid, SECCLASS_PROCESS,
Eric Paris828dfe12008-04-17 13:17:49 -04006403 PROCESS__DYNTRANSITION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006404 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11006405 goto abort_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006406
6407 /* Check for ptracing, and update the task SID if ok.
6408 Otherwise, leave SID unchanged and fail. */
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006409 ptsid = ptrace_parent_sid();
Paul Moore0c6181c2016-03-30 21:41:21 -04006410 if (ptsid != 0) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006411 error = avc_has_perm(&selinux_state,
6412 ptsid, sid, SECCLASS_PROCESS,
David Howellsd84f4f92008-11-14 10:39:23 +11006413 PROCESS__PTRACE, NULL);
6414 if (error)
6415 goto abort_change;
6416 }
6417
6418 tsec->sid = sid;
6419 } else {
6420 error = -EINVAL;
6421 goto abort_change;
6422 }
6423
6424 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006425 return size;
David Howellsd84f4f92008-11-14 10:39:23 +11006426
6427abort_change:
6428 abort_creds(new);
6429 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006430}
6431
David Quigley746df9b2013-05-22 12:50:35 -04006432static int selinux_ismaclabel(const char *name)
6433{
6434 return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0);
6435}
6436
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006437static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
6438{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006439 return security_sid_to_context(&selinux_state, secid,
6440 secdata, seclen);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006441}
6442
David Howells7bf570d2008-04-29 20:52:51 +01006443static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
David Howells63cb3442008-01-15 23:47:35 +00006444{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006445 return security_context_to_sid(&selinux_state, secdata, seclen,
6446 secid, GFP_KERNEL);
David Howells63cb3442008-01-15 23:47:35 +00006447}
6448
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006449static void selinux_release_secctx(char *secdata, u32 seclen)
6450{
Paul Moore088999e2007-08-01 11:12:58 -04006451 kfree(secdata);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006452}
6453
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006454static void selinux_inode_invalidate_secctx(struct inode *inode)
6455{
6456 struct inode_security_struct *isec = inode->i_security;
6457
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01006458 spin_lock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006459 isec->initialized = LABEL_INVALID;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01006460 spin_unlock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006461}
6462
David P. Quigley1ee65e32009-09-03 14:25:57 -04006463/*
6464 * called with inode->i_mutex locked
6465 */
6466static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
6467{
6468 return selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX, ctx, ctxlen, 0);
6469}
6470
6471/*
6472 * called with inode->i_mutex locked
6473 */
6474static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
6475{
6476 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0);
6477}
6478
6479static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
6480{
6481 int len = 0;
6482 len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX,
6483 ctx, true);
6484 if (len < 0)
6485 return len;
6486 *ctxlen = len;
6487 return 0;
6488}
Michael LeMayd7200242006-06-22 14:47:17 -07006489#ifdef CONFIG_KEYS
6490
David Howellsd84f4f92008-11-14 10:39:23 +11006491static int selinux_key_alloc(struct key *k, const struct cred *cred,
David Howells7e047ef2006-06-26 00:24:50 -07006492 unsigned long flags)
Michael LeMayd7200242006-06-22 14:47:17 -07006493{
David Howellsd84f4f92008-11-14 10:39:23 +11006494 const struct task_security_struct *tsec;
Michael LeMayd7200242006-06-22 14:47:17 -07006495 struct key_security_struct *ksec;
6496
6497 ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
6498 if (!ksec)
6499 return -ENOMEM;
6500
David Howellsd84f4f92008-11-14 10:39:23 +11006501 tsec = cred->security;
6502 if (tsec->keycreate_sid)
6503 ksec->sid = tsec->keycreate_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006504 else
David Howellsd84f4f92008-11-14 10:39:23 +11006505 ksec->sid = tsec->sid;
Michael LeMayd7200242006-06-22 14:47:17 -07006506
David Howells275bb412008-11-14 10:39:19 +11006507 k->security = ksec;
Michael LeMayd7200242006-06-22 14:47:17 -07006508 return 0;
6509}
6510
6511static void selinux_key_free(struct key *k)
6512{
6513 struct key_security_struct *ksec = k->security;
6514
6515 k->security = NULL;
6516 kfree(ksec);
6517}
6518
6519static int selinux_key_permission(key_ref_t key_ref,
David Howellsd84f4f92008-11-14 10:39:23 +11006520 const struct cred *cred,
David Howellsf5895942014-03-14 17:44:49 +00006521 unsigned perm)
Michael LeMayd7200242006-06-22 14:47:17 -07006522{
6523 struct key *key;
Michael LeMayd7200242006-06-22 14:47:17 -07006524 struct key_security_struct *ksec;
David Howells275bb412008-11-14 10:39:19 +11006525 u32 sid;
Michael LeMayd7200242006-06-22 14:47:17 -07006526
6527 /* if no specific permissions are requested, we skip the
6528 permission check. No serious, additional covert channels
6529 appear to be created. */
6530 if (perm == 0)
6531 return 0;
6532
David Howellsd84f4f92008-11-14 10:39:23 +11006533 sid = cred_sid(cred);
David Howells275bb412008-11-14 10:39:19 +11006534
6535 key = key_ref_to_ptr(key_ref);
6536 ksec = key->security;
6537
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006538 return avc_has_perm(&selinux_state,
6539 sid, ksec->sid, SECCLASS_KEY, perm, NULL);
Michael LeMayd7200242006-06-22 14:47:17 -07006540}
6541
David Howells70a5bb72008-04-29 01:01:26 -07006542static int selinux_key_getsecurity(struct key *key, char **_buffer)
6543{
6544 struct key_security_struct *ksec = key->security;
6545 char *context = NULL;
6546 unsigned len;
6547 int rc;
6548
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006549 rc = security_sid_to_context(&selinux_state, ksec->sid,
6550 &context, &len);
David Howells70a5bb72008-04-29 01:01:26 -07006551 if (!rc)
6552 rc = len;
6553 *_buffer = context;
6554 return rc;
6555}
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006556#endif
David Howells70a5bb72008-04-29 01:01:26 -07006557
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006558#ifdef CONFIG_SECURITY_INFINIBAND
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006559static int selinux_ib_pkey_access(void *ib_sec, u64 subnet_prefix, u16 pkey_val)
6560{
6561 struct common_audit_data ad;
6562 int err;
6563 u32 sid = 0;
6564 struct ib_security_struct *sec = ib_sec;
6565 struct lsm_ibpkey_audit ibpkey;
6566
Daniel Jurgens409dcf32017-05-19 15:48:59 +03006567 err = sel_ib_pkey_sid(subnet_prefix, pkey_val, &sid);
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006568 if (err)
6569 return err;
6570
6571 ad.type = LSM_AUDIT_DATA_IBPKEY;
6572 ibpkey.subnet_prefix = subnet_prefix;
6573 ibpkey.pkey = pkey_val;
6574 ad.u.ibpkey = &ibpkey;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006575 return avc_has_perm(&selinux_state,
6576 sec->sid, sid,
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006577 SECCLASS_INFINIBAND_PKEY,
6578 INFINIBAND_PKEY__ACCESS, &ad);
6579}
6580
Daniel Jurgensab861df2017-05-19 15:48:58 +03006581static int selinux_ib_endport_manage_subnet(void *ib_sec, const char *dev_name,
6582 u8 port_num)
6583{
6584 struct common_audit_data ad;
6585 int err;
6586 u32 sid = 0;
6587 struct ib_security_struct *sec = ib_sec;
6588 struct lsm_ibendport_audit ibendport;
6589
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006590 err = security_ib_endport_sid(&selinux_state, dev_name, port_num,
6591 &sid);
Daniel Jurgensab861df2017-05-19 15:48:58 +03006592
6593 if (err)
6594 return err;
6595
6596 ad.type = LSM_AUDIT_DATA_IBENDPORT;
6597 strncpy(ibendport.dev_name, dev_name, sizeof(ibendport.dev_name));
6598 ibendport.port = port_num;
6599 ad.u.ibendport = &ibendport;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006600 return avc_has_perm(&selinux_state,
6601 sec->sid, sid,
Daniel Jurgensab861df2017-05-19 15:48:58 +03006602 SECCLASS_INFINIBAND_ENDPORT,
6603 INFINIBAND_ENDPORT__MANAGE_SUBNET, &ad);
6604}
6605
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006606static int selinux_ib_alloc_security(void **ib_sec)
6607{
6608 struct ib_security_struct *sec;
6609
6610 sec = kzalloc(sizeof(*sec), GFP_KERNEL);
6611 if (!sec)
6612 return -ENOMEM;
6613 sec->sid = current_sid();
6614
6615 *ib_sec = sec;
6616 return 0;
6617}
6618
6619static void selinux_ib_free_security(void *ib_sec)
6620{
6621 kfree(ib_sec);
6622}
Michael LeMayd7200242006-06-22 14:47:17 -07006623#endif
6624
Chenbo Fengec27c352017-10-18 13:00:25 -07006625#ifdef CONFIG_BPF_SYSCALL
6626static int selinux_bpf(int cmd, union bpf_attr *attr,
6627 unsigned int size)
6628{
6629 u32 sid = current_sid();
6630 int ret;
6631
6632 switch (cmd) {
6633 case BPF_MAP_CREATE:
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006634 ret = avc_has_perm(&selinux_state,
6635 sid, sid, SECCLASS_BPF, BPF__MAP_CREATE,
Chenbo Fengec27c352017-10-18 13:00:25 -07006636 NULL);
6637 break;
6638 case BPF_PROG_LOAD:
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006639 ret = avc_has_perm(&selinux_state,
6640 sid, sid, SECCLASS_BPF, BPF__PROG_LOAD,
Chenbo Fengec27c352017-10-18 13:00:25 -07006641 NULL);
6642 break;
6643 default:
6644 ret = 0;
6645 break;
6646 }
6647
6648 return ret;
6649}
6650
6651static u32 bpf_map_fmode_to_av(fmode_t fmode)
6652{
6653 u32 av = 0;
6654
6655 if (fmode & FMODE_READ)
6656 av |= BPF__MAP_READ;
6657 if (fmode & FMODE_WRITE)
6658 av |= BPF__MAP_WRITE;
6659 return av;
6660}
6661
Chenbo Fengf66e4482017-10-18 13:00:26 -07006662/* This function will check the file pass through unix socket or binder to see
6663 * if it is a bpf related object. And apply correspinding checks on the bpf
6664 * object based on the type. The bpf maps and programs, not like other files and
6665 * socket, are using a shared anonymous inode inside the kernel as their inode.
6666 * So checking that inode cannot identify if the process have privilege to
6667 * access the bpf object and that's why we have to add this additional check in
6668 * selinux_file_receive and selinux_binder_transfer_files.
6669 */
6670static int bpf_fd_pass(struct file *file, u32 sid)
6671{
6672 struct bpf_security_struct *bpfsec;
6673 struct bpf_prog *prog;
6674 struct bpf_map *map;
6675 int ret;
6676
6677 if (file->f_op == &bpf_map_fops) {
6678 map = file->private_data;
6679 bpfsec = map->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006680 ret = avc_has_perm(&selinux_state,
6681 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengf66e4482017-10-18 13:00:26 -07006682 bpf_map_fmode_to_av(file->f_mode), NULL);
6683 if (ret)
6684 return ret;
6685 } else if (file->f_op == &bpf_prog_fops) {
6686 prog = file->private_data;
6687 bpfsec = prog->aux->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006688 ret = avc_has_perm(&selinux_state,
6689 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengf66e4482017-10-18 13:00:26 -07006690 BPF__PROG_RUN, NULL);
6691 if (ret)
6692 return ret;
6693 }
6694 return 0;
6695}
6696
Chenbo Fengec27c352017-10-18 13:00:25 -07006697static int selinux_bpf_map(struct bpf_map *map, fmode_t fmode)
6698{
6699 u32 sid = current_sid();
6700 struct bpf_security_struct *bpfsec;
6701
6702 bpfsec = map->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006703 return avc_has_perm(&selinux_state,
6704 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengec27c352017-10-18 13:00:25 -07006705 bpf_map_fmode_to_av(fmode), NULL);
6706}
6707
6708static int selinux_bpf_prog(struct bpf_prog *prog)
6709{
6710 u32 sid = current_sid();
6711 struct bpf_security_struct *bpfsec;
6712
6713 bpfsec = prog->aux->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006714 return avc_has_perm(&selinux_state,
6715 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengec27c352017-10-18 13:00:25 -07006716 BPF__PROG_RUN, NULL);
6717}
6718
6719static int selinux_bpf_map_alloc(struct bpf_map *map)
6720{
6721 struct bpf_security_struct *bpfsec;
6722
6723 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6724 if (!bpfsec)
6725 return -ENOMEM;
6726
6727 bpfsec->sid = current_sid();
6728 map->security = bpfsec;
6729
6730 return 0;
6731}
6732
6733static void selinux_bpf_map_free(struct bpf_map *map)
6734{
6735 struct bpf_security_struct *bpfsec = map->security;
6736
6737 map->security = NULL;
6738 kfree(bpfsec);
6739}
6740
6741static int selinux_bpf_prog_alloc(struct bpf_prog_aux *aux)
6742{
6743 struct bpf_security_struct *bpfsec;
6744
6745 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6746 if (!bpfsec)
6747 return -ENOMEM;
6748
6749 bpfsec->sid = current_sid();
6750 aux->security = bpfsec;
6751
6752 return 0;
6753}
6754
6755static void selinux_bpf_prog_free(struct bpf_prog_aux *aux)
6756{
6757 struct bpf_security_struct *bpfsec = aux->security;
6758
6759 aux->security = NULL;
6760 kfree(bpfsec);
6761}
6762#endif
6763
James Morrisca97d932017-02-15 00:18:51 +11006764static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {
Casey Schauflere20b0432015-05-02 15:11:36 -07006765 LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr),
6766 LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction),
6767 LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder),
6768 LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file),
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02006769
Casey Schauflere20b0432015-05-02 15:11:36 -07006770 LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check),
6771 LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme),
6772 LSM_HOOK_INIT(capget, selinux_capget),
6773 LSM_HOOK_INIT(capset, selinux_capset),
6774 LSM_HOOK_INIT(capable, selinux_capable),
6775 LSM_HOOK_INIT(quotactl, selinux_quotactl),
6776 LSM_HOOK_INIT(quota_on, selinux_quota_on),
6777 LSM_HOOK_INIT(syslog, selinux_syslog),
6778 LSM_HOOK_INIT(vm_enough_memory, selinux_vm_enough_memory),
Stephen Smalley79af7302015-01-21 10:54:10 -05006779
Casey Schauflere20b0432015-05-02 15:11:36 -07006780 LSM_HOOK_INIT(netlink_send, selinux_netlink_send),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006781
Casey Schauflere20b0432015-05-02 15:11:36 -07006782 LSM_HOOK_INIT(bprm_set_creds, selinux_bprm_set_creds),
6783 LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds),
6784 LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006785
Al Viro0b520752018-12-23 16:02:47 -05006786 LSM_HOOK_INIT(fs_context_dup, selinux_fs_context_dup),
David Howells442155c2018-11-01 23:07:24 +00006787 LSM_HOOK_INIT(fs_context_parse_param, selinux_fs_context_parse_param),
6788
Casey Schauflere20b0432015-05-02 15:11:36 -07006789 LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security),
6790 LSM_HOOK_INIT(sb_free_security, selinux_sb_free_security),
Al Viro5b400232018-12-12 20:13:29 -05006791 LSM_HOOK_INIT(sb_eat_lsm_opts, selinux_sb_eat_lsm_opts),
Al Viro204cc0c2018-12-13 13:41:47 -05006792 LSM_HOOK_INIT(sb_free_mnt_opts, selinux_free_mnt_opts),
Casey Schauflere20b0432015-05-02 15:11:36 -07006793 LSM_HOOK_INIT(sb_remount, selinux_sb_remount),
6794 LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount),
6795 LSM_HOOK_INIT(sb_show_options, selinux_sb_show_options),
6796 LSM_HOOK_INIT(sb_statfs, selinux_sb_statfs),
6797 LSM_HOOK_INIT(sb_mount, selinux_mount),
6798 LSM_HOOK_INIT(sb_umount, selinux_umount),
6799 LSM_HOOK_INIT(sb_set_mnt_opts, selinux_set_mnt_opts),
6800 LSM_HOOK_INIT(sb_clone_mnt_opts, selinux_sb_clone_mnt_opts),
Al Viro757cbe52018-12-14 23:42:21 -05006801 LSM_HOOK_INIT(sb_add_mnt_opt, selinux_add_mnt_opt),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006802
Casey Schauflere20b0432015-05-02 15:11:36 -07006803 LSM_HOOK_INIT(dentry_init_security, selinux_dentry_init_security),
Vivek Goyala518b0a2016-07-13 10:44:53 -04006804 LSM_HOOK_INIT(dentry_create_files_as, selinux_dentry_create_files_as),
Eric Parise0007522008-03-05 10:31:54 -05006805
Casey Schauflere20b0432015-05-02 15:11:36 -07006806 LSM_HOOK_INIT(inode_alloc_security, selinux_inode_alloc_security),
6807 LSM_HOOK_INIT(inode_free_security, selinux_inode_free_security),
6808 LSM_HOOK_INIT(inode_init_security, selinux_inode_init_security),
6809 LSM_HOOK_INIT(inode_create, selinux_inode_create),
6810 LSM_HOOK_INIT(inode_link, selinux_inode_link),
6811 LSM_HOOK_INIT(inode_unlink, selinux_inode_unlink),
6812 LSM_HOOK_INIT(inode_symlink, selinux_inode_symlink),
6813 LSM_HOOK_INIT(inode_mkdir, selinux_inode_mkdir),
6814 LSM_HOOK_INIT(inode_rmdir, selinux_inode_rmdir),
6815 LSM_HOOK_INIT(inode_mknod, selinux_inode_mknod),
6816 LSM_HOOK_INIT(inode_rename, selinux_inode_rename),
6817 LSM_HOOK_INIT(inode_readlink, selinux_inode_readlink),
6818 LSM_HOOK_INIT(inode_follow_link, selinux_inode_follow_link),
6819 LSM_HOOK_INIT(inode_permission, selinux_inode_permission),
6820 LSM_HOOK_INIT(inode_setattr, selinux_inode_setattr),
6821 LSM_HOOK_INIT(inode_getattr, selinux_inode_getattr),
6822 LSM_HOOK_INIT(inode_setxattr, selinux_inode_setxattr),
6823 LSM_HOOK_INIT(inode_post_setxattr, selinux_inode_post_setxattr),
6824 LSM_HOOK_INIT(inode_getxattr, selinux_inode_getxattr),
6825 LSM_HOOK_INIT(inode_listxattr, selinux_inode_listxattr),
6826 LSM_HOOK_INIT(inode_removexattr, selinux_inode_removexattr),
6827 LSM_HOOK_INIT(inode_getsecurity, selinux_inode_getsecurity),
6828 LSM_HOOK_INIT(inode_setsecurity, selinux_inode_setsecurity),
6829 LSM_HOOK_INIT(inode_listsecurity, selinux_inode_listsecurity),
6830 LSM_HOOK_INIT(inode_getsecid, selinux_inode_getsecid),
Vivek Goyal56909eb2016-07-13 10:44:48 -04006831 LSM_HOOK_INIT(inode_copy_up, selinux_inode_copy_up),
Vivek Goyal19472b62016-07-13 10:44:50 -04006832 LSM_HOOK_INIT(inode_copy_up_xattr, selinux_inode_copy_up_xattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006833
Casey Schauflere20b0432015-05-02 15:11:36 -07006834 LSM_HOOK_INIT(file_permission, selinux_file_permission),
6835 LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security),
6836 LSM_HOOK_INIT(file_free_security, selinux_file_free_security),
6837 LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl),
6838 LSM_HOOK_INIT(mmap_file, selinux_mmap_file),
6839 LSM_HOOK_INIT(mmap_addr, selinux_mmap_addr),
6840 LSM_HOOK_INIT(file_mprotect, selinux_file_mprotect),
6841 LSM_HOOK_INIT(file_lock, selinux_file_lock),
6842 LSM_HOOK_INIT(file_fcntl, selinux_file_fcntl),
6843 LSM_HOOK_INIT(file_set_fowner, selinux_file_set_fowner),
6844 LSM_HOOK_INIT(file_send_sigiotask, selinux_file_send_sigiotask),
6845 LSM_HOOK_INIT(file_receive, selinux_file_receive),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006846
Casey Schauflere20b0432015-05-02 15:11:36 -07006847 LSM_HOOK_INIT(file_open, selinux_file_open),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006848
Tetsuo Handaa79be232017-03-28 23:08:45 +09006849 LSM_HOOK_INIT(task_alloc, selinux_task_alloc),
Casey Schauflere20b0432015-05-02 15:11:36 -07006850 LSM_HOOK_INIT(cred_alloc_blank, selinux_cred_alloc_blank),
6851 LSM_HOOK_INIT(cred_free, selinux_cred_free),
6852 LSM_HOOK_INIT(cred_prepare, selinux_cred_prepare),
6853 LSM_HOOK_INIT(cred_transfer, selinux_cred_transfer),
Matthew Garrett3ec30112018-01-08 13:36:19 -08006854 LSM_HOOK_INIT(cred_getsecid, selinux_cred_getsecid),
Casey Schauflere20b0432015-05-02 15:11:36 -07006855 LSM_HOOK_INIT(kernel_act_as, selinux_kernel_act_as),
6856 LSM_HOOK_INIT(kernel_create_files_as, selinux_kernel_create_files_as),
6857 LSM_HOOK_INIT(kernel_module_request, selinux_kernel_module_request),
Mimi Zoharc77b8cd2018-07-13 14:06:02 -04006858 LSM_HOOK_INIT(kernel_load_data, selinux_kernel_load_data),
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07006859 LSM_HOOK_INIT(kernel_read_file, selinux_kernel_read_file),
Casey Schauflere20b0432015-05-02 15:11:36 -07006860 LSM_HOOK_INIT(task_setpgid, selinux_task_setpgid),
6861 LSM_HOOK_INIT(task_getpgid, selinux_task_getpgid),
6862 LSM_HOOK_INIT(task_getsid, selinux_task_getsid),
6863 LSM_HOOK_INIT(task_getsecid, selinux_task_getsecid),
6864 LSM_HOOK_INIT(task_setnice, selinux_task_setnice),
6865 LSM_HOOK_INIT(task_setioprio, selinux_task_setioprio),
6866 LSM_HOOK_INIT(task_getioprio, selinux_task_getioprio),
Stephen Smalley791ec492017-02-17 07:57:00 -05006867 LSM_HOOK_INIT(task_prlimit, selinux_task_prlimit),
Casey Schauflere20b0432015-05-02 15:11:36 -07006868 LSM_HOOK_INIT(task_setrlimit, selinux_task_setrlimit),
6869 LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler),
6870 LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler),
6871 LSM_HOOK_INIT(task_movememory, selinux_task_movememory),
6872 LSM_HOOK_INIT(task_kill, selinux_task_kill),
Casey Schauflere20b0432015-05-02 15:11:36 -07006873 LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode),
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09006874
Casey Schauflere20b0432015-05-02 15:11:36 -07006875 LSM_HOOK_INIT(ipc_permission, selinux_ipc_permission),
6876 LSM_HOOK_INIT(ipc_getsecid, selinux_ipc_getsecid),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006877
Casey Schauflere20b0432015-05-02 15:11:36 -07006878 LSM_HOOK_INIT(msg_msg_alloc_security, selinux_msg_msg_alloc_security),
6879 LSM_HOOK_INIT(msg_msg_free_security, selinux_msg_msg_free_security),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006880
Casey Schauflere20b0432015-05-02 15:11:36 -07006881 LSM_HOOK_INIT(msg_queue_alloc_security,
6882 selinux_msg_queue_alloc_security),
6883 LSM_HOOK_INIT(msg_queue_free_security, selinux_msg_queue_free_security),
6884 LSM_HOOK_INIT(msg_queue_associate, selinux_msg_queue_associate),
6885 LSM_HOOK_INIT(msg_queue_msgctl, selinux_msg_queue_msgctl),
6886 LSM_HOOK_INIT(msg_queue_msgsnd, selinux_msg_queue_msgsnd),
6887 LSM_HOOK_INIT(msg_queue_msgrcv, selinux_msg_queue_msgrcv),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006888
Casey Schauflere20b0432015-05-02 15:11:36 -07006889 LSM_HOOK_INIT(shm_alloc_security, selinux_shm_alloc_security),
6890 LSM_HOOK_INIT(shm_free_security, selinux_shm_free_security),
6891 LSM_HOOK_INIT(shm_associate, selinux_shm_associate),
6892 LSM_HOOK_INIT(shm_shmctl, selinux_shm_shmctl),
6893 LSM_HOOK_INIT(shm_shmat, selinux_shm_shmat),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006894
Casey Schauflere20b0432015-05-02 15:11:36 -07006895 LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security),
6896 LSM_HOOK_INIT(sem_free_security, selinux_sem_free_security),
6897 LSM_HOOK_INIT(sem_associate, selinux_sem_associate),
6898 LSM_HOOK_INIT(sem_semctl, selinux_sem_semctl),
6899 LSM_HOOK_INIT(sem_semop, selinux_sem_semop),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006900
Casey Schauflere20b0432015-05-02 15:11:36 -07006901 LSM_HOOK_INIT(d_instantiate, selinux_d_instantiate),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006902
Casey Schauflere20b0432015-05-02 15:11:36 -07006903 LSM_HOOK_INIT(getprocattr, selinux_getprocattr),
6904 LSM_HOOK_INIT(setprocattr, selinux_setprocattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006905
Casey Schauflere20b0432015-05-02 15:11:36 -07006906 LSM_HOOK_INIT(ismaclabel, selinux_ismaclabel),
6907 LSM_HOOK_INIT(secid_to_secctx, selinux_secid_to_secctx),
6908 LSM_HOOK_INIT(secctx_to_secid, selinux_secctx_to_secid),
6909 LSM_HOOK_INIT(release_secctx, selinux_release_secctx),
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006910 LSM_HOOK_INIT(inode_invalidate_secctx, selinux_inode_invalidate_secctx),
Casey Schauflere20b0432015-05-02 15:11:36 -07006911 LSM_HOOK_INIT(inode_notifysecctx, selinux_inode_notifysecctx),
6912 LSM_HOOK_INIT(inode_setsecctx, selinux_inode_setsecctx),
6913 LSM_HOOK_INIT(inode_getsecctx, selinux_inode_getsecctx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006914
Casey Schauflere20b0432015-05-02 15:11:36 -07006915 LSM_HOOK_INIT(unix_stream_connect, selinux_socket_unix_stream_connect),
6916 LSM_HOOK_INIT(unix_may_send, selinux_socket_unix_may_send),
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006917
Casey Schauflere20b0432015-05-02 15:11:36 -07006918 LSM_HOOK_INIT(socket_create, selinux_socket_create),
6919 LSM_HOOK_INIT(socket_post_create, selinux_socket_post_create),
David Herrmann0b811db2018-05-04 16:28:21 +02006920 LSM_HOOK_INIT(socket_socketpair, selinux_socket_socketpair),
Casey Schauflere20b0432015-05-02 15:11:36 -07006921 LSM_HOOK_INIT(socket_bind, selinux_socket_bind),
6922 LSM_HOOK_INIT(socket_connect, selinux_socket_connect),
6923 LSM_HOOK_INIT(socket_listen, selinux_socket_listen),
6924 LSM_HOOK_INIT(socket_accept, selinux_socket_accept),
6925 LSM_HOOK_INIT(socket_sendmsg, selinux_socket_sendmsg),
6926 LSM_HOOK_INIT(socket_recvmsg, selinux_socket_recvmsg),
6927 LSM_HOOK_INIT(socket_getsockname, selinux_socket_getsockname),
6928 LSM_HOOK_INIT(socket_getpeername, selinux_socket_getpeername),
6929 LSM_HOOK_INIT(socket_getsockopt, selinux_socket_getsockopt),
6930 LSM_HOOK_INIT(socket_setsockopt, selinux_socket_setsockopt),
6931 LSM_HOOK_INIT(socket_shutdown, selinux_socket_shutdown),
6932 LSM_HOOK_INIT(socket_sock_rcv_skb, selinux_socket_sock_rcv_skb),
6933 LSM_HOOK_INIT(socket_getpeersec_stream,
6934 selinux_socket_getpeersec_stream),
6935 LSM_HOOK_INIT(socket_getpeersec_dgram, selinux_socket_getpeersec_dgram),
6936 LSM_HOOK_INIT(sk_alloc_security, selinux_sk_alloc_security),
6937 LSM_HOOK_INIT(sk_free_security, selinux_sk_free_security),
6938 LSM_HOOK_INIT(sk_clone_security, selinux_sk_clone_security),
6939 LSM_HOOK_INIT(sk_getsecid, selinux_sk_getsecid),
6940 LSM_HOOK_INIT(sock_graft, selinux_sock_graft),
Richard Hainesd4529302018-02-13 20:57:18 +00006941 LSM_HOOK_INIT(sctp_assoc_request, selinux_sctp_assoc_request),
6942 LSM_HOOK_INIT(sctp_sk_clone, selinux_sctp_sk_clone),
6943 LSM_HOOK_INIT(sctp_bind_connect, selinux_sctp_bind_connect),
Casey Schauflere20b0432015-05-02 15:11:36 -07006944 LSM_HOOK_INIT(inet_conn_request, selinux_inet_conn_request),
6945 LSM_HOOK_INIT(inet_csk_clone, selinux_inet_csk_clone),
6946 LSM_HOOK_INIT(inet_conn_established, selinux_inet_conn_established),
6947 LSM_HOOK_INIT(secmark_relabel_packet, selinux_secmark_relabel_packet),
6948 LSM_HOOK_INIT(secmark_refcount_inc, selinux_secmark_refcount_inc),
6949 LSM_HOOK_INIT(secmark_refcount_dec, selinux_secmark_refcount_dec),
6950 LSM_HOOK_INIT(req_classify_flow, selinux_req_classify_flow),
6951 LSM_HOOK_INIT(tun_dev_alloc_security, selinux_tun_dev_alloc_security),
6952 LSM_HOOK_INIT(tun_dev_free_security, selinux_tun_dev_free_security),
6953 LSM_HOOK_INIT(tun_dev_create, selinux_tun_dev_create),
6954 LSM_HOOK_INIT(tun_dev_attach_queue, selinux_tun_dev_attach_queue),
6955 LSM_HOOK_INIT(tun_dev_attach, selinux_tun_dev_attach),
6956 LSM_HOOK_INIT(tun_dev_open, selinux_tun_dev_open),
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006957#ifdef CONFIG_SECURITY_INFINIBAND
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006958 LSM_HOOK_INIT(ib_pkey_access, selinux_ib_pkey_access),
Daniel Jurgensab861df2017-05-19 15:48:58 +03006959 LSM_HOOK_INIT(ib_endport_manage_subnet,
6960 selinux_ib_endport_manage_subnet),
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006961 LSM_HOOK_INIT(ib_alloc_security, selinux_ib_alloc_security),
6962 LSM_HOOK_INIT(ib_free_security, selinux_ib_free_security),
6963#endif
Trent Jaegerd28d1e02005-12-13 23:12:40 -08006964#ifdef CONFIG_SECURITY_NETWORK_XFRM
Casey Schauflere20b0432015-05-02 15:11:36 -07006965 LSM_HOOK_INIT(xfrm_policy_alloc_security, selinux_xfrm_policy_alloc),
6966 LSM_HOOK_INIT(xfrm_policy_clone_security, selinux_xfrm_policy_clone),
6967 LSM_HOOK_INIT(xfrm_policy_free_security, selinux_xfrm_policy_free),
6968 LSM_HOOK_INIT(xfrm_policy_delete_security, selinux_xfrm_policy_delete),
6969 LSM_HOOK_INIT(xfrm_state_alloc, selinux_xfrm_state_alloc),
6970 LSM_HOOK_INIT(xfrm_state_alloc_acquire,
6971 selinux_xfrm_state_alloc_acquire),
6972 LSM_HOOK_INIT(xfrm_state_free_security, selinux_xfrm_state_free),
6973 LSM_HOOK_INIT(xfrm_state_delete_security, selinux_xfrm_state_delete),
6974 LSM_HOOK_INIT(xfrm_policy_lookup, selinux_xfrm_policy_lookup),
6975 LSM_HOOK_INIT(xfrm_state_pol_flow_match,
6976 selinux_xfrm_state_pol_flow_match),
6977 LSM_HOOK_INIT(xfrm_decode_session, selinux_xfrm_decode_session),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006978#endif
Michael LeMayd7200242006-06-22 14:47:17 -07006979
6980#ifdef CONFIG_KEYS
Casey Schauflere20b0432015-05-02 15:11:36 -07006981 LSM_HOOK_INIT(key_alloc, selinux_key_alloc),
6982 LSM_HOOK_INIT(key_free, selinux_key_free),
6983 LSM_HOOK_INIT(key_permission, selinux_key_permission),
6984 LSM_HOOK_INIT(key_getsecurity, selinux_key_getsecurity),
Michael LeMayd7200242006-06-22 14:47:17 -07006985#endif
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02006986
6987#ifdef CONFIG_AUDIT
Casey Schauflere20b0432015-05-02 15:11:36 -07006988 LSM_HOOK_INIT(audit_rule_init, selinux_audit_rule_init),
6989 LSM_HOOK_INIT(audit_rule_known, selinux_audit_rule_known),
6990 LSM_HOOK_INIT(audit_rule_match, selinux_audit_rule_match),
6991 LSM_HOOK_INIT(audit_rule_free, selinux_audit_rule_free),
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02006992#endif
Chenbo Fengec27c352017-10-18 13:00:25 -07006993
6994#ifdef CONFIG_BPF_SYSCALL
6995 LSM_HOOK_INIT(bpf, selinux_bpf),
6996 LSM_HOOK_INIT(bpf_map, selinux_bpf_map),
6997 LSM_HOOK_INIT(bpf_prog, selinux_bpf_prog),
6998 LSM_HOOK_INIT(bpf_map_alloc_security, selinux_bpf_map_alloc),
6999 LSM_HOOK_INIT(bpf_prog_alloc_security, selinux_bpf_prog_alloc),
7000 LSM_HOOK_INIT(bpf_map_free_security, selinux_bpf_map_free),
7001 LSM_HOOK_INIT(bpf_prog_free_security, selinux_bpf_prog_free),
7002#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007003};
7004
7005static __init int selinux_init(void)
7006{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07007007 if (!security_module_enable("selinux")) {
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02007008 selinux_enabled = 0;
7009 return 0;
7010 }
7011
Linus Torvalds1da177e2005-04-16 15:20:36 -07007012 if (!selinux_enabled) {
peter enderborgc103a912018-06-12 10:09:03 +02007013 pr_info("SELinux: Disabled at boot.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007014 return 0;
7015 }
7016
peter enderborgc103a912018-06-12 10:09:03 +02007017 pr_info("SELinux: Initializing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007018
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007019 memset(&selinux_state, 0, sizeof(selinux_state));
Paul Mooree5a5ca92018-03-01 17:38:30 -05007020 enforcing_set(&selinux_state, selinux_enforcing_boot);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007021 selinux_state.checkreqprot = selinux_checkreqprot_boot;
7022 selinux_ss_init(&selinux_state.ss);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05007023 selinux_avc_init(&selinux_state.avc);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007024
Linus Torvalds1da177e2005-04-16 15:20:36 -07007025 /* Set the security state for the initial task. */
David Howellsd84f4f92008-11-14 10:39:23 +11007026 cred_init_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007027
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04007028 default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
7029
James Morris7cae7e22006-03-22 00:09:22 -08007030 sel_inode_cache = kmem_cache_create("selinux_inode_security",
7031 sizeof(struct inode_security_struct),
Paul Mundt20c2df82007-07-20 10:11:58 +09007032 0, SLAB_PANIC, NULL);
Sangwoo63205652015-10-21 17:44:30 -04007033 file_security_cache = kmem_cache_create("selinux_file_security",
7034 sizeof(struct file_security_struct),
7035 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007036 avc_init();
7037
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007038 avtab_cache_init();
7039
7040 ebitmap_cache_init();
7041
7042 hashtab_cache_init();
7043
Casey Schauflerd69dece52017-01-18 17:09:05 -08007044 security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks), "selinux");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007045
Paul Moore615e51f2014-06-26 14:33:56 -04007046 if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET))
7047 panic("SELinux: Unable to register AVC netcache callback\n");
7048
Daniel Jurgens8f408ab2017-05-19 15:48:53 +03007049 if (avc_add_callback(selinux_lsm_notifier_avc_callback, AVC_CALLBACK_RESET))
7050 panic("SELinux: Unable to register AVC LSM notifier callback\n");
7051
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007052 if (selinux_enforcing_boot)
peter enderborgc103a912018-06-12 10:09:03 +02007053 pr_debug("SELinux: Starting in enforcing mode\n");
Eric Paris828dfe12008-04-17 13:17:49 -04007054 else
peter enderborgc103a912018-06-12 10:09:03 +02007055 pr_debug("SELinux: Starting in permissive mode\n");
Michael LeMayd7200242006-06-22 14:47:17 -07007056
David Howells442155c2018-11-01 23:07:24 +00007057 fs_validate_description(&selinux_fs_parameters);
7058
Linus Torvalds1da177e2005-04-16 15:20:36 -07007059 return 0;
7060}
7061
Al Viroe8c26252010-03-23 06:36:54 -04007062static void delayed_superblock_init(struct super_block *sb, void *unused)
7063{
Al Viro204cc0c2018-12-13 13:41:47 -05007064 selinux_set_mnt_opts(sb, NULL, 0, NULL);
Al Viroe8c26252010-03-23 06:36:54 -04007065}
7066
Linus Torvalds1da177e2005-04-16 15:20:36 -07007067void selinux_complete_init(void)
7068{
peter enderborgc103a912018-06-12 10:09:03 +02007069 pr_debug("SELinux: Completing initialization.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007070
7071 /* Set up any superblocks initialized prior to the policy load. */
peter enderborgc103a912018-06-12 10:09:03 +02007072 pr_debug("SELinux: Setting up existing superblocks.\n");
Al Viroe8c26252010-03-23 06:36:54 -04007073 iterate_supers(delayed_superblock_init, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007074}
7075
7076/* SELinux requires early initialization in order to label
7077 all processes and objects when they are created. */
Kees Cook3d6e5f62018-10-10 17:18:23 -07007078DEFINE_LSM(selinux) = {
Kees Cook07aed2f2018-10-10 17:18:24 -07007079 .name = "selinux",
Kees Cook3d6e5f62018-10-10 17:18:23 -07007080 .init = selinux_init,
7081};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007082
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007083#if defined(CONFIG_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007084
Florian Westphal591bb272017-07-26 11:40:52 +02007085static const struct nf_hook_ops selinux_nf_ops[] = {
Paul Mooreeffad8d2008-01-29 08:49:27 -05007086 {
7087 .hook = selinux_ipv4_postroute,
Alban Crequy2597a832012-05-14 03:56:39 +00007088 .pf = NFPROTO_IPV4,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007089 .hooknum = NF_INET_POST_ROUTING,
7090 .priority = NF_IP_PRI_SELINUX_LAST,
7091 },
7092 {
7093 .hook = selinux_ipv4_forward,
Alban Crequy2597a832012-05-14 03:56:39 +00007094 .pf = NFPROTO_IPV4,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007095 .hooknum = NF_INET_FORWARD,
7096 .priority = NF_IP_PRI_SELINUX_FIRST,
Paul Moore948bf852008-10-10 10:16:32 -04007097 },
7098 {
7099 .hook = selinux_ipv4_output,
Alban Crequy2597a832012-05-14 03:56:39 +00007100 .pf = NFPROTO_IPV4,
Paul Moore948bf852008-10-10 10:16:32 -04007101 .hooknum = NF_INET_LOCAL_OUT,
7102 .priority = NF_IP_PRI_SELINUX_FIRST,
Jiri Pirko25db6be2014-09-03 17:42:13 +02007103 },
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04007104#if IS_ENABLED(CONFIG_IPV6)
Paul Mooreeffad8d2008-01-29 08:49:27 -05007105 {
7106 .hook = selinux_ipv6_postroute,
Alban Crequy2597a832012-05-14 03:56:39 +00007107 .pf = NFPROTO_IPV6,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007108 .hooknum = NF_INET_POST_ROUTING,
7109 .priority = NF_IP6_PRI_SELINUX_LAST,
7110 },
7111 {
7112 .hook = selinux_ipv6_forward,
Alban Crequy2597a832012-05-14 03:56:39 +00007113 .pf = NFPROTO_IPV6,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007114 .hooknum = NF_INET_FORWARD,
7115 .priority = NF_IP6_PRI_SELINUX_FIRST,
Jiri Pirko25db6be2014-09-03 17:42:13 +02007116 },
Huw Davies2917f572016-06-27 15:06:15 -04007117 {
7118 .hook = selinux_ipv6_output,
7119 .pf = NFPROTO_IPV6,
7120 .hooknum = NF_INET_LOCAL_OUT,
7121 .priority = NF_IP6_PRI_SELINUX_FIRST,
7122 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07007123#endif /* IPV6 */
Jiri Pirko25db6be2014-09-03 17:42:13 +02007124};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007125
Florian Westphal8e71bf72017-04-21 11:49:09 +02007126static int __net_init selinux_nf_register(struct net *net)
7127{
7128 return nf_register_net_hooks(net, selinux_nf_ops,
7129 ARRAY_SIZE(selinux_nf_ops));
7130}
7131
7132static void __net_exit selinux_nf_unregister(struct net *net)
7133{
7134 nf_unregister_net_hooks(net, selinux_nf_ops,
7135 ARRAY_SIZE(selinux_nf_ops));
7136}
7137
7138static struct pernet_operations selinux_net_ops = {
7139 .init = selinux_nf_register,
7140 .exit = selinux_nf_unregister,
7141};
7142
Linus Torvalds1da177e2005-04-16 15:20:36 -07007143static int __init selinux_nf_ip_init(void)
7144{
Jiri Pirko25db6be2014-09-03 17:42:13 +02007145 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007146
7147 if (!selinux_enabled)
Jiri Pirko25db6be2014-09-03 17:42:13 +02007148 return 0;
Eric Parisfadcdb42007-02-22 18:11:31 -05007149
peter enderborgc103a912018-06-12 10:09:03 +02007150 pr_debug("SELinux: Registering netfilter hooks\n");
Eric Parisfadcdb42007-02-22 18:11:31 -05007151
Florian Westphal8e71bf72017-04-21 11:49:09 +02007152 err = register_pernet_subsys(&selinux_net_ops);
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07007153 if (err)
Florian Westphal8e71bf72017-04-21 11:49:09 +02007154 panic("SELinux: register_pernet_subsys: error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007155
Jiri Pirko25db6be2014-09-03 17:42:13 +02007156 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007157}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007158__initcall(selinux_nf_ip_init);
7159
7160#ifdef CONFIG_SECURITY_SELINUX_DISABLE
7161static void selinux_nf_ip_exit(void)
7162{
peter enderborgc103a912018-06-12 10:09:03 +02007163 pr_debug("SELinux: Unregistering netfilter hooks\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007164
Florian Westphal8e71bf72017-04-21 11:49:09 +02007165 unregister_pernet_subsys(&selinux_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007166}
7167#endif
7168
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007169#else /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007170
7171#ifdef CONFIG_SECURITY_SELINUX_DISABLE
7172#define selinux_nf_ip_exit()
7173#endif
7174
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007175#endif /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007176
7177#ifdef CONFIG_SECURITY_SELINUX_DISABLE
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007178int selinux_disable(struct selinux_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007179{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007180 if (state->initialized) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007181 /* Not permitted after initial policy load. */
7182 return -EINVAL;
7183 }
7184
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007185 if (state->disabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007186 /* Only do this once. */
7187 return -EINVAL;
7188 }
7189
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007190 state->disabled = 1;
7191
peter enderborgc103a912018-06-12 10:09:03 +02007192 pr_info("SELinux: Disabled at runtime.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007193
Stephen Smalley30d55282006-05-03 10:52:36 -04007194 selinux_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007195
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07007196 security_delete_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007197
Eric Parisaf8ff042009-09-20 21:23:01 -04007198 /* Try to destroy the avc node cache */
7199 avc_disable();
7200
Linus Torvalds1da177e2005-04-16 15:20:36 -07007201 /* Unregister netfilter hooks. */
7202 selinux_nf_ip_exit();
7203
7204 /* Unregister selinuxfs. */
7205 exit_sel_fs();
7206
7207 return 0;
7208}
7209#endif