blob: b6e61524d68d25ce65a5f96ca4c5ed0f78b548f4 [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>
Eric Paris23970742006-09-25 23:32:01 -070084#include <linux/mutex.h>
Frank Mayharf06febc2008-09-12 09:54:39 -070085#include <linux/posix-timers.h>
Kees Cook00234592010-02-03 15:36:43 -080086#include <linux/syslog.h>
Serge E. Hallyn34867402011-03-23 16:43:17 -070087#include <linux/user_namespace.h>
Paul Gortmaker44fc7ea2011-05-26 20:52:10 -040088#include <linux/export.h>
Al Viro40401532012-02-13 03:58:52 +000089#include <linux/msg.h>
90#include <linux/shm.h>
Chenbo Fengec27c352017-10-18 13:00:25 -070091#include <linux/bpf.h>
Ondrej Mosnacekec882da2019-02-22 15:57:17 +010092#include <linux/kernfs.h>
93#include <linux/stringhash.h> /* for hashlen_string() */
David Howellse262e32d2018-11-01 23:07:23 +000094#include <uapi/linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96#include "avc.h"
97#include "objsec.h"
98#include "netif.h"
Paul Moore224dfbd2008-01-29 08:38:13 -050099#include "netnode.h"
Paul Moore3e1121722008-04-10 10:48:14 -0400100#include "netport.h"
Daniel Jurgens409dcf32017-05-19 15:48:59 +0300101#include "ibpkey.h"
Trent Jaegerd28d1e02005-12-13 23:12:40 -0800102#include "xfrm.h"
Paul Moorec60475b2007-02-28 15:14:23 -0500103#include "netlabel.h"
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +0200104#include "audit.h"
James Morris7b98a582011-08-30 12:52:32 +1000105#include "avc_ss.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500107struct selinux_state selinux_state;
108
Paul Moored621d352008-01-29 08:43:36 -0500109/* SECMARK reference count */
James Morris56a4ca92011-08-17 11:08:43 +1000110static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0);
Paul Moored621d352008-01-29 08:43:36 -0500111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112#ifdef CONFIG_SECURITY_SELINUX_DEVELOP
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500113static int selinux_enforcing_boot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115static int __init enforcing_setup(char *str)
116{
Eric Parisf5269712008-05-14 11:27:45 -0400117 unsigned long enforcing;
Jingoo Han29707b22014-02-05 15:13:14 +0900118 if (!kstrtoul(str, 0, &enforcing))
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500119 selinux_enforcing_boot = enforcing ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 return 1;
121}
122__setup("enforcing=", enforcing_setup);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500123#else
124#define selinux_enforcing_boot 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125#endif
126
Kees Cookbe6ec882018-10-01 17:08:57 -0700127int selinux_enabled __lsm_ro_after_init = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128#ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129static 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);
137#endif
138
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500139static unsigned int selinux_checkreqprot_boot =
140 CONFIG_SECURITY_SELINUX_CHECKREQPROT_VALUE;
141
142static int __init checkreqprot_setup(char *str)
143{
144 unsigned long checkreqprot;
145
146 if (!kstrtoul(str, 0, &checkreqprot))
147 selinux_checkreqprot_boot = checkreqprot ? 1 : 0;
148 return 1;
149}
150__setup("checkreqprot=", checkreqprot_setup);
151
Paul Moored621d352008-01-29 08:43:36 -0500152/**
153 * selinux_secmark_enabled - Check to see if SECMARK is currently enabled
154 *
155 * Description:
156 * This function checks the SECMARK reference counter to see if any SECMARK
157 * targets are currently configured, if the reference counter is greater than
158 * zero SECMARK is considered to be enabled. Returns true (1) if SECMARK is
Chris PeBenito2be4d742013-05-03 09:05:39 -0400159 * enabled, false (0) if SECMARK is disabled. If the always_check_network
160 * policy capability is enabled, SECMARK is always considered enabled.
Paul Moored621d352008-01-29 08:43:36 -0500161 *
162 */
163static int selinux_secmark_enabled(void)
164{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500165 return (selinux_policycap_alwaysnetwork() ||
166 atomic_read(&selinux_secmark_refcount));
Chris PeBenito2be4d742013-05-03 09:05:39 -0400167}
168
169/**
170 * selinux_peerlbl_enabled - Check to see if peer labeling is currently enabled
171 *
172 * Description:
173 * This function checks if NetLabel or labeled IPSEC is enabled. Returns true
174 * (1) if any are enabled or false (0) if neither are enabled. If the
175 * always_check_network policy capability is enabled, peer labeling
176 * is always considered enabled.
177 *
178 */
179static int selinux_peerlbl_enabled(void)
180{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500181 return (selinux_policycap_alwaysnetwork() ||
182 netlbl_enabled() || selinux_xfrm_enabled());
Paul Moored621d352008-01-29 08:43:36 -0500183}
184
Paul Moore615e51f2014-06-26 14:33:56 -0400185static int selinux_netcache_avc_callback(u32 event)
186{
187 if (event == AVC_CALLBACK_RESET) {
188 sel_netif_flush();
189 sel_netnode_flush();
190 sel_netport_flush();
191 synchronize_net();
192 }
193 return 0;
194}
195
Daniel Jurgens8f408ab2017-05-19 15:48:53 +0300196static int selinux_lsm_notifier_avc_callback(u32 event)
197{
Daniel Jurgens409dcf32017-05-19 15:48:59 +0300198 if (event == AVC_CALLBACK_RESET) {
199 sel_ib_pkey_flush();
Daniel Jurgens8f408ab2017-05-19 15:48:53 +0300200 call_lsm_notifier(LSM_POLICY_CHANGE, NULL);
Daniel Jurgens409dcf32017-05-19 15:48:59 +0300201 }
Daniel Jurgens8f408ab2017-05-19 15:48:53 +0300202
203 return 0;
204}
205
David Howellsd84f4f92008-11-14 10:39:23 +1100206/*
207 * initialise the security for the init task
208 */
209static void cred_init_security(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
David Howells3b11a1d2008-11-14 10:39:26 +1100211 struct cred *cred = (struct cred *) current->real_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 struct task_security_struct *tsec;
213
Casey Schauflerbbd36622018-11-12 09:30:56 -0800214 tsec = selinux_cred(cred);
David Howellsd84f4f92008-11-14 10:39:23 +1100215 tsec->osid = tsec->sid = SECINITSID_KERNEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
David Howells275bb412008-11-14 10:39:19 +1100218/*
David Howells88e67f32008-11-14 10:39:21 +1100219 * get the security ID of a set of credentials
220 */
221static inline u32 cred_sid(const struct cred *cred)
222{
223 const struct task_security_struct *tsec;
224
Casey Schaufler0c6cfa62018-09-21 17:17:16 -0700225 tsec = selinux_cred(cred);
David Howells88e67f32008-11-14 10:39:21 +1100226 return tsec->sid;
227}
228
229/*
David Howells3b11a1d2008-11-14 10:39:26 +1100230 * get the objective security ID of a task
David Howells275bb412008-11-14 10:39:19 +1100231 */
232static inline u32 task_sid(const struct task_struct *task)
233{
David Howells275bb412008-11-14 10:39:19 +1100234 u32 sid;
235
236 rcu_read_lock();
David Howells88e67f32008-11-14 10:39:21 +1100237 sid = cred_sid(__task_cred(task));
David Howells275bb412008-11-14 10:39:19 +1100238 rcu_read_unlock();
239 return sid;
240}
241
David Howells88e67f32008-11-14 10:39:21 +1100242/* Allocate and free functions for each kind of security blob. */
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244static int inode_alloc_security(struct inode *inode)
245{
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700246 struct inode_security_struct *isec = selinux_inode(inode);
David Howells275bb412008-11-14 10:39:19 +1100247 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +0100249 spin_lock_init(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 INIT_LIST_HEAD(&isec->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 isec->inode = inode;
252 isec->sid = SECINITSID_UNLABELED;
253 isec->sclass = SECCLASS_FILE;
David Howells275bb412008-11-14 10:39:19 +1100254 isec->task_sid = sid;
Andreas Gruenbacher42059112016-11-10 22:18:27 +0100255 isec->initialized = LABEL_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 return 0;
258}
259
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500260static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry);
261
262/*
263 * Try reloading inode security labels that have been marked as invalid. The
264 * @may_sleep parameter indicates when sleeping and thus reloading labels is
Andreas Gruenbacher42059112016-11-10 22:18:27 +0100265 * allowed; when set to false, returns -ECHILD when the label is
Al Viroe9193282018-04-24 21:31:02 -0400266 * invalid. The @dentry parameter should be set to a dentry of the inode.
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500267 */
268static int __inode_security_revalidate(struct inode *inode,
Al Viroe9193282018-04-24 21:31:02 -0400269 struct dentry *dentry,
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500270 bool may_sleep)
271{
Casey Schaufler80788c22018-09-21 17:19:11 -0700272 struct inode_security_struct *isec = selinux_inode(inode);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500273
274 might_sleep_if(may_sleep);
275
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500276 if (selinux_state.initialized &&
277 isec->initialized != LABEL_INITIALIZED) {
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500278 if (!may_sleep)
279 return -ECHILD;
280
281 /*
282 * Try reloading the inode security label. This will fail if
283 * @opt_dentry is NULL and no dentry for this inode can be
284 * found; in that case, continue using the old label.
285 */
Al Viroe9193282018-04-24 21:31:02 -0400286 inode_doinit_with_dentry(inode, dentry);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500287 }
288 return 0;
289}
290
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500291static struct inode_security_struct *inode_security_novalidate(struct inode *inode)
292{
Casey Schaufler80788c22018-09-21 17:19:11 -0700293 return selinux_inode(inode);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500294}
295
296static struct inode_security_struct *inode_security_rcu(struct inode *inode, bool rcu)
297{
298 int error;
299
300 error = __inode_security_revalidate(inode, NULL, !rcu);
301 if (error)
302 return ERR_PTR(error);
Casey Schaufler80788c22018-09-21 17:19:11 -0700303 return selinux_inode(inode);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500304}
305
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500306/*
307 * Get the security label of an inode.
308 */
309static struct inode_security_struct *inode_security(struct inode *inode)
310{
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500311 __inode_security_revalidate(inode, NULL, true);
Casey Schaufler80788c22018-09-21 17:19:11 -0700312 return selinux_inode(inode);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500313}
314
Paul Moore2c971652016-04-19 16:36:28 -0400315static struct inode_security_struct *backing_inode_security_novalidate(struct dentry *dentry)
316{
317 struct inode *inode = d_backing_inode(dentry);
318
Casey Schaufler80788c22018-09-21 17:19:11 -0700319 return selinux_inode(inode);
Paul Moore2c971652016-04-19 16:36:28 -0400320}
321
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500322/*
323 * Get the security label of a dentry's backing inode.
324 */
325static struct inode_security_struct *backing_inode_security(struct dentry *dentry)
326{
327 struct inode *inode = d_backing_inode(dentry);
328
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500329 __inode_security_revalidate(inode, dentry, true);
Casey Schaufler80788c22018-09-21 17:19:11 -0700330 return selinux_inode(inode);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500331}
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333static void inode_free_security(struct inode *inode)
334{
Casey Schaufler80788c22018-09-21 17:19:11 -0700335 struct inode_security_struct *isec = selinux_inode(inode);
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700336 struct superblock_security_struct *sbsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700338 if (!isec)
339 return;
340 sbsec = inode->i_sb->s_security;
Waiman Long9629d042015-07-10 17:19:56 -0400341 /*
342 * As not all inode security structures are in a list, we check for
343 * empty list outside of the lock to make sure that we won't waste
344 * time taking a lock doing nothing.
345 *
346 * The list_del_init() function can be safely called more than once.
347 * It should not be possible for this function to be called with
348 * concurrent list_add(), but for better safety against future changes
349 * in the code, we use list_empty_careful() here.
350 */
351 if (!list_empty_careful(&isec->list)) {
352 spin_lock(&sbsec->isec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 list_del_init(&isec->list);
Waiman Long9629d042015-07-10 17:19:56 -0400354 spin_unlock(&sbsec->isec_lock);
355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356}
357
358static int file_alloc_security(struct file *file)
359{
Casey Schaufler33bf60c2018-11-12 12:02:49 -0800360 struct file_security_struct *fsec = selinux_file(file);
David Howells275bb412008-11-14 10:39:19 +1100361 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
David Howells275bb412008-11-14 10:39:19 +1100363 fsec->sid = sid;
364 fsec->fown_sid = sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 return 0;
367}
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369static int superblock_alloc_security(struct super_block *sb)
370{
371 struct superblock_security_struct *sbsec;
372
James Morris89d155e2005-10-30 14:59:21 -0800373 sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 if (!sbsec)
375 return -ENOMEM;
376
Eric Parisbc7e9822006-09-25 23:32:02 -0700377 mutex_init(&sbsec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 INIT_LIST_HEAD(&sbsec->isec_head);
379 spin_lock_init(&sbsec->isec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 sbsec->sb = sb;
381 sbsec->sid = SECINITSID_UNLABELED;
382 sbsec->def_sid = SECINITSID_FILE;
Eric Parisc312feb2006-07-10 04:43:53 -0700383 sbsec->mntpoint_sid = SECINITSID_UNLABELED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 sb->s_security = sbsec;
385
386 return 0;
387}
388
389static void superblock_free_security(struct super_block *sb)
390{
391 struct superblock_security_struct *sbsec = sb->s_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 sb->s_security = NULL;
393 kfree(sbsec);
394}
395
Al Virobd323652018-12-13 15:04:59 -0500396struct selinux_mnt_opts {
397 const char *fscontext, *context, *rootcontext, *defcontext;
398};
399
Al Viro204cc0c2018-12-13 13:41:47 -0500400static void selinux_free_mnt_opts(void *mnt_opts)
401{
Al Virobd323652018-12-13 15:04:59 -0500402 struct selinux_mnt_opts *opts = mnt_opts;
403 kfree(opts->fscontext);
404 kfree(opts->context);
405 kfree(opts->rootcontext);
406 kfree(opts->defcontext);
Al Viro204cc0c2018-12-13 13:41:47 -0500407 kfree(opts);
408}
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410static inline int inode_doinit(struct inode *inode)
411{
412 return inode_doinit_with_dentry(inode, NULL);
413}
414
415enum {
Eric Paris31e87932007-09-19 17:19:12 -0400416 Opt_error = -1,
David Howells442155c2018-11-01 23:07:24 +0000417 Opt_context = 0,
418 Opt_defcontext = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 Opt_fscontext = 2,
David Howells442155c2018-11-01 23:07:24 +0000420 Opt_rootcontext = 3,
421 Opt_seclabel = 4,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422};
423
Al Viroda3d76a2018-12-17 10:14:16 -0500424#define A(s, has_arg) {#s, sizeof(#s) - 1, Opt_##s, has_arg}
Al Viro169d68efb2018-12-14 22:44:50 -0500425static struct {
426 const char *name;
427 int len;
428 int opt;
429 bool has_arg;
430} tokens[] = {
Al Viroda3d76a2018-12-17 10:14:16 -0500431 A(context, true),
432 A(fscontext, true),
433 A(defcontext, true),
434 A(rootcontext, true),
435 A(seclabel, false),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436};
Al Viro169d68efb2018-12-14 22:44:50 -0500437#undef A
438
439static int match_opt_prefix(char *s, int l, char **arg)
440{
441 int i;
442
443 for (i = 0; i < ARRAY_SIZE(tokens); i++) {
444 size_t len = tokens[i].len;
445 if (len > l || memcmp(s, tokens[i].name, len))
446 continue;
447 if (tokens[i].has_arg) {
448 if (len == l || s[len] != '=')
449 continue;
450 *arg = s + len + 1;
451 } else if (len != l)
452 continue;
453 return tokens[i].opt;
454 }
455 return Opt_error;
456}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458#define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n"
459
Eric Parisc312feb2006-07-10 04:43:53 -0700460static int may_context_mount_sb_relabel(u32 sid,
461 struct superblock_security_struct *sbsec,
David Howells275bb412008-11-14 10:39:19 +1100462 const struct cred *cred)
Eric Parisc312feb2006-07-10 04:43:53 -0700463{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -0700464 const struct task_security_struct *tsec = selinux_cred(cred);
Eric Parisc312feb2006-07-10 04:43:53 -0700465 int rc;
466
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500467 rc = avc_has_perm(&selinux_state,
468 tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
Eric Parisc312feb2006-07-10 04:43:53 -0700469 FILESYSTEM__RELABELFROM, NULL);
470 if (rc)
471 return rc;
472
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500473 rc = avc_has_perm(&selinux_state,
474 tsec->sid, sid, SECCLASS_FILESYSTEM,
Eric Parisc312feb2006-07-10 04:43:53 -0700475 FILESYSTEM__RELABELTO, NULL);
476 return rc;
477}
478
Eric Paris08089252006-07-10 04:43:55 -0700479static int may_context_mount_inode_relabel(u32 sid,
480 struct superblock_security_struct *sbsec,
David Howells275bb412008-11-14 10:39:19 +1100481 const struct cred *cred)
Eric Paris08089252006-07-10 04:43:55 -0700482{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -0700483 const struct task_security_struct *tsec = selinux_cred(cred);
Eric Paris08089252006-07-10 04:43:55 -0700484 int rc;
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500485 rc = avc_has_perm(&selinux_state,
486 tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
Eric Paris08089252006-07-10 04:43:55 -0700487 FILESYSTEM__RELABELFROM, NULL);
488 if (rc)
489 return rc;
490
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500491 rc = avc_has_perm(&selinux_state,
492 sid, sbsec->sid, SECCLASS_FILESYSTEM,
Eric Paris08089252006-07-10 04:43:55 -0700493 FILESYSTEM__ASSOCIATE, NULL);
494 return rc;
495}
496
Ondrej Mosnaceka83d6dd2018-12-21 21:18:52 +0100497static int selinux_is_genfs_special_handling(struct super_block *sb)
Eric Parisb43e7252012-10-10 14:27:35 -0400498{
Ondrej Mosnaceka83d6dd2018-12-21 21:18:52 +0100499 /* Special handling. Genfs but also in-core setxattr handler */
500 return !strcmp(sb->s_type->name, "sysfs") ||
Mark Salyzynd5f3a5f2015-02-04 11:34:30 -0500501 !strcmp(sb->s_type->name, "pstore") ||
502 !strcmp(sb->s_type->name, "debugfs") ||
Yongqin Liua2c7c6f2017-01-09 10:07:30 -0500503 !strcmp(sb->s_type->name, "tracefs") ||
Stephen Smalley2651225b2017-02-28 10:35:56 -0500504 !strcmp(sb->s_type->name, "rootfs") ||
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500505 (selinux_policycap_cgroupseclabel() &&
Stephen Smalley2651225b2017-02-28 10:35:56 -0500506 (!strcmp(sb->s_type->name, "cgroup") ||
507 !strcmp(sb->s_type->name, "cgroup2")));
Eric Parisb43e7252012-10-10 14:27:35 -0400508}
509
Ondrej Mosnaceka83d6dd2018-12-21 21:18:52 +0100510static int selinux_is_sblabel_mnt(struct super_block *sb)
511{
512 struct superblock_security_struct *sbsec = sb->s_security;
513
514 /*
515 * IMPORTANT: Double-check logic in this function when adding a new
516 * SECURITY_FS_USE_* definition!
517 */
518 BUILD_BUG_ON(SECURITY_FS_USE_MAX != 7);
519
520 switch (sbsec->behavior) {
521 case SECURITY_FS_USE_XATTR:
522 case SECURITY_FS_USE_TRANS:
523 case SECURITY_FS_USE_TASK:
524 case SECURITY_FS_USE_NATIVE:
525 return 1;
526
527 case SECURITY_FS_USE_GENFS:
528 return selinux_is_genfs_special_handling(sb);
529
530 /* Never allow relabeling on context mounts */
531 case SECURITY_FS_USE_MNTPOINT:
532 case SECURITY_FS_USE_NONE:
533 default:
534 return 0;
535 }
536}
537
Eric Parisc9180a52007-11-30 13:00:35 -0500538static int sb_finish_set_opts(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
540 struct superblock_security_struct *sbsec = sb->s_security;
541 struct dentry *root = sb->s_root;
David Howellsc6f493d2015-03-17 22:26:22 +0000542 struct inode *root_inode = d_backing_inode(root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 int rc = 0;
544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
546 /* Make sure that the xattr handler exists and that no
547 error other than -ENODATA is returned by getxattr on
548 the root directory. -ENODATA is ok, as this may be
549 the first boot of the SELinux kernel before we have
550 assigned xattr values to the filesystem. */
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200551 if (!(root_inode->i_opflags & IOP_XATTR)) {
peter enderborgc103a912018-06-12 10:09:03 +0200552 pr_warn("SELinux: (dev %s, type %s) has no "
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800553 "xattr support\n", sb->s_id, sb->s_type->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 rc = -EOPNOTSUPP;
555 goto out;
556 }
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200557
558 rc = __vfs_getxattr(root, root_inode, XATTR_NAME_SELINUX, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 if (rc < 0 && rc != -ENODATA) {
560 if (rc == -EOPNOTSUPP)
peter enderborgc103a912018-06-12 10:09:03 +0200561 pr_warn("SELinux: (dev %s, type "
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800562 "%s) has no security xattr handler\n",
563 sb->s_id, sb->s_type->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 else
peter enderborgc103a912018-06-12 10:09:03 +0200565 pr_warn("SELinux: (dev %s, type "
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800566 "%s) getxattr errno %d\n", sb->s_id,
567 sb->s_type->name, -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 goto out;
569 }
570 }
571
Eric Pariseadcabc2012-08-24 15:59:14 -0400572 sbsec->flags |= SE_SBINITIALIZED;
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400573
574 /*
575 * Explicitly set or clear SBLABEL_MNT. It's not sufficient to simply
576 * leave the flag untouched because sb_clone_mnt_opts might be handing
577 * us a superblock that needs the flag to be cleared.
578 */
Eric Parisb43e7252012-10-10 14:27:35 -0400579 if (selinux_is_sblabel_mnt(sb))
Eric Paris12f348b2012-10-09 10:56:25 -0400580 sbsec->flags |= SBLABEL_MNT;
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400581 else
582 sbsec->flags &= ~SBLABEL_MNT;
David P. Quigleyddd29ec2009-09-09 14:25:37 -0400583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 /* Initialize the root inode. */
Eric Parisc9180a52007-11-30 13:00:35 -0500585 rc = inode_doinit_with_dentry(root_inode, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 /* Initialize any other inodes associated with the superblock, e.g.
588 inodes created prior to initial policy load or inodes created
589 during get_sb by a pseudo filesystem that directly
590 populates itself. */
591 spin_lock(&sbsec->isec_lock);
Al Viro8d641242018-12-10 15:34:12 -0500592 while (!list_empty(&sbsec->isec_head)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 struct inode_security_struct *isec =
Al Viro8d641242018-12-10 15:34:12 -0500594 list_first_entry(&sbsec->isec_head,
Eric Parisc9180a52007-11-30 13:00:35 -0500595 struct inode_security_struct, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 struct inode *inode = isec->inode;
Stephen Smalley923190d2014-10-06 16:32:52 -0400597 list_del_init(&isec->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 spin_unlock(&sbsec->isec_lock);
599 inode = igrab(inode);
600 if (inode) {
Eric Parisc9180a52007-11-30 13:00:35 -0500601 if (!IS_PRIVATE(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 inode_doinit(inode);
603 iput(inode);
604 }
605 spin_lock(&sbsec->isec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
607 spin_unlock(&sbsec->isec_lock);
608out:
Eric Parisc9180a52007-11-30 13:00:35 -0500609 return rc;
610}
611
Eric Parisc9180a52007-11-30 13:00:35 -0500612static int bad_option(struct superblock_security_struct *sbsec, char flag,
613 u32 old_sid, u32 new_sid)
614{
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500615 char mnt_flags = sbsec->flags & SE_MNTMASK;
616
Eric Parisc9180a52007-11-30 13:00:35 -0500617 /* check if the old mount command had the same options */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500618 if (sbsec->flags & SE_SBINITIALIZED)
Eric Parisc9180a52007-11-30 13:00:35 -0500619 if (!(sbsec->flags & flag) ||
620 (old_sid != new_sid))
621 return 1;
622
623 /* check if we were passed the same options twice,
624 * aka someone passed context=a,context=b
625 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500626 if (!(sbsec->flags & SE_SBINITIALIZED))
627 if (mnt_flags & flag)
Eric Parisc9180a52007-11-30 13:00:35 -0500628 return 1;
629 return 0;
630}
Eric Parise0007522008-03-05 10:31:54 -0500631
Al Virobd323652018-12-13 15:04:59 -0500632static int parse_sid(struct super_block *sb, const char *s, u32 *sid)
633{
634 int rc = security_context_str_to_sid(&selinux_state, s,
635 sid, GFP_KERNEL);
636 if (rc)
637 pr_warn("SELinux: security_context_str_to_sid"
638 "(%s) failed for (dev %s, type %s) errno=%d\n",
639 s, sb->s_id, sb->s_type->name, rc);
640 return rc;
641}
642
Eric Parisc9180a52007-11-30 13:00:35 -0500643/*
644 * Allow filesystems with binary mount data to explicitly set mount point
645 * labeling information.
646 */
Eric Parise0007522008-03-05 10:31:54 -0500647static int selinux_set_mnt_opts(struct super_block *sb,
Al Viro204cc0c2018-12-13 13:41:47 -0500648 void *mnt_opts,
David Quigley649f6e72013-05-22 12:50:36 -0400649 unsigned long kern_flags,
650 unsigned long *set_kern_flags)
Eric Parisc9180a52007-11-30 13:00:35 -0500651{
David Howells275bb412008-11-14 10:39:19 +1100652 const struct cred *cred = current_cred();
Eric Parisc9180a52007-11-30 13:00:35 -0500653 struct superblock_security_struct *sbsec = sb->s_security;
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500654 struct dentry *root = sbsec->sb->s_root;
Al Virobd323652018-12-13 15:04:59 -0500655 struct selinux_mnt_opts *opts = mnt_opts;
Paul Moore2c971652016-04-19 16:36:28 -0400656 struct inode_security_struct *root_isec;
Eric Parisc9180a52007-11-30 13:00:35 -0500657 u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
658 u32 defcontext_sid = 0;
Al Virobd323652018-12-13 15:04:59 -0500659 int rc = 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500660
661 mutex_lock(&sbsec->lock);
662
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500663 if (!selinux_state.initialized) {
Al Virobd323652018-12-13 15:04:59 -0500664 if (!opts) {
Eric Parisc9180a52007-11-30 13:00:35 -0500665 /* Defer initialization until selinux_complete_init,
666 after the initial policy is loaded and the security
667 server is ready to handle calls. */
Eric Parisc9180a52007-11-30 13:00:35 -0500668 goto out;
669 }
670 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +0200671 pr_warn("SELinux: Unable to set superblock options "
Eric Paris744ba352008-04-17 11:52:44 -0400672 "before the security server is initialized\n");
Eric Parisc9180a52007-11-30 13:00:35 -0500673 goto out;
674 }
David Quigley649f6e72013-05-22 12:50:36 -0400675 if (kern_flags && !set_kern_flags) {
676 /* Specifying internal flags without providing a place to
677 * place the results is not allowed */
678 rc = -EINVAL;
679 goto out;
680 }
Eric Parisc9180a52007-11-30 13:00:35 -0500681
682 /*
Eric Parise0007522008-03-05 10:31:54 -0500683 * Binary mount data FS will come through this function twice. Once
684 * from an explicit call and once from the generic calls from the vfs.
685 * Since the generic VFS calls will not contain any security mount data
686 * we need to skip the double mount verification.
687 *
688 * This does open a hole in which we will not notice if the first
689 * mount using this sb set explict options and a second mount using
690 * this sb does not set any security options. (The first options
691 * will be used for both mounts)
692 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500693 if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
Al Virobd323652018-12-13 15:04:59 -0500694 && !opts)
Eric Parisf5269712008-05-14 11:27:45 -0400695 goto out;
Eric Parise0007522008-03-05 10:31:54 -0500696
Paul Moore2c971652016-04-19 16:36:28 -0400697 root_isec = backing_inode_security_novalidate(root);
698
Eric Parise0007522008-03-05 10:31:54 -0500699 /*
Eric Parisc9180a52007-11-30 13:00:35 -0500700 * parse the mount options, check if they are valid sids.
701 * also check if someone is trying to mount the same sb more
702 * than once with different security options.
703 */
Al Virobd323652018-12-13 15:04:59 -0500704 if (opts) {
705 if (opts->fscontext) {
706 rc = parse_sid(sb, opts->fscontext, &fscontext_sid);
707 if (rc)
708 goto out;
Eric Parisc9180a52007-11-30 13:00:35 -0500709 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
710 fscontext_sid))
711 goto out_double_mount;
Eric Parisc9180a52007-11-30 13:00:35 -0500712 sbsec->flags |= FSCONTEXT_MNT;
Al Virobd323652018-12-13 15:04:59 -0500713 }
714 if (opts->context) {
715 rc = parse_sid(sb, opts->context, &context_sid);
716 if (rc)
717 goto out;
Eric Parisc9180a52007-11-30 13:00:35 -0500718 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
719 context_sid))
720 goto out_double_mount;
Eric Parisc9180a52007-11-30 13:00:35 -0500721 sbsec->flags |= CONTEXT_MNT;
Al Virobd323652018-12-13 15:04:59 -0500722 }
723 if (opts->rootcontext) {
724 rc = parse_sid(sb, opts->rootcontext, &rootcontext_sid);
725 if (rc)
726 goto out;
Eric Parisc9180a52007-11-30 13:00:35 -0500727 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
728 rootcontext_sid))
729 goto out_double_mount;
Eric Parisc9180a52007-11-30 13:00:35 -0500730 sbsec->flags |= ROOTCONTEXT_MNT;
Al Virobd323652018-12-13 15:04:59 -0500731 }
732 if (opts->defcontext) {
733 rc = parse_sid(sb, opts->defcontext, &defcontext_sid);
734 if (rc)
735 goto out;
Eric Parisc9180a52007-11-30 13:00:35 -0500736 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
737 defcontext_sid))
738 goto out_double_mount;
Eric Parisc9180a52007-11-30 13:00:35 -0500739 sbsec->flags |= DEFCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500740 }
741 }
742
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500743 if (sbsec->flags & SE_SBINITIALIZED) {
Eric Parisc9180a52007-11-30 13:00:35 -0500744 /* previously mounted with options, but not on this attempt? */
Al Virobd323652018-12-13 15:04:59 -0500745 if ((sbsec->flags & SE_MNTMASK) && !opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500746 goto out_double_mount;
747 rc = 0;
748 goto out;
749 }
750
James Morris089be432008-07-15 18:32:49 +1000751 if (strcmp(sb->s_type->name, "proc") == 0)
Stephen Smalley134509d2015-06-04 16:22:17 -0400752 sbsec->flags |= SE_SBPROC | SE_SBGENFS;
753
Stephen Smalley8e014722015-06-04 16:22:17 -0400754 if (!strcmp(sb->s_type->name, "debugfs") ||
Jeff Vander Stoep6a391182017-06-20 09:35:33 -0700755 !strcmp(sb->s_type->name, "tracefs") ||
Ondrej Mosnacekb7540262019-02-22 15:57:14 +0100756 !strcmp(sb->s_type->name, "pstore"))
757 sbsec->flags |= SE_SBGENFS;
758
759 if (!strcmp(sb->s_type->name, "sysfs") ||
Antonio Murdaca901ef842017-02-09 17:02:42 +0100760 !strcmp(sb->s_type->name, "cgroup") ||
761 !strcmp(sb->s_type->name, "cgroup2"))
Ondrej Mosnacekb7540262019-02-22 15:57:14 +0100762 sbsec->flags |= SE_SBGENFS | SE_SBGENFS_XATTR;
Eric Parisc9180a52007-11-30 13:00:35 -0500763
David Quigleyeb9ae682013-05-22 12:50:37 -0400764 if (!sbsec->behavior) {
765 /*
766 * Determine the labeling behavior to use for this
767 * filesystem type.
768 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500769 rc = security_fs_use(&selinux_state, sb);
David Quigleyeb9ae682013-05-22 12:50:37 -0400770 if (rc) {
peter enderborgc103a912018-06-12 10:09:03 +0200771 pr_warn("%s: security_fs_use(%s) returned %d\n",
David Quigleyeb9ae682013-05-22 12:50:37 -0400772 __func__, sb->s_type->name, rc);
773 goto out;
774 }
Eric Parisc9180a52007-11-30 13:00:35 -0500775 }
Seth Forsheeaad82892016-04-26 14:36:20 -0500776
777 /*
Stephen Smalley01593d32017-01-09 10:07:31 -0500778 * If this is a user namespace mount and the filesystem type is not
779 * explicitly whitelisted, then no contexts are allowed on the command
780 * line and security labels must be ignored.
Seth Forsheeaad82892016-04-26 14:36:20 -0500781 */
Stephen Smalley01593d32017-01-09 10:07:31 -0500782 if (sb->s_user_ns != &init_user_ns &&
783 strcmp(sb->s_type->name, "tmpfs") &&
784 strcmp(sb->s_type->name, "ramfs") &&
785 strcmp(sb->s_type->name, "devpts")) {
Seth Forsheeaad82892016-04-26 14:36:20 -0500786 if (context_sid || fscontext_sid || rootcontext_sid ||
787 defcontext_sid) {
788 rc = -EACCES;
789 goto out;
790 }
791 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
792 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500793 rc = security_transition_sid(&selinux_state,
794 current_sid(),
795 current_sid(),
Seth Forsheeaad82892016-04-26 14:36:20 -0500796 SECCLASS_FILE, NULL,
797 &sbsec->mntpoint_sid);
798 if (rc)
799 goto out;
800 }
801 goto out_set_opts;
802 }
803
Eric Parisc9180a52007-11-30 13:00:35 -0500804 /* sets the context of the superblock for the fs being mounted. */
805 if (fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100806 rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500807 if (rc)
808 goto out;
809
810 sbsec->sid = fscontext_sid;
811 }
812
813 /*
814 * Switch to using mount point labeling behavior.
815 * sets the label used on all file below the mountpoint, and will set
816 * the superblock context if not already set.
817 */
David Quigleyeb9ae682013-05-22 12:50:37 -0400818 if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !context_sid) {
819 sbsec->behavior = SECURITY_FS_USE_NATIVE;
820 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
821 }
822
Eric Parisc9180a52007-11-30 13:00:35 -0500823 if (context_sid) {
824 if (!fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100825 rc = may_context_mount_sb_relabel(context_sid, sbsec,
826 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500827 if (rc)
828 goto out;
829 sbsec->sid = context_sid;
830 } else {
David Howells275bb412008-11-14 10:39:19 +1100831 rc = may_context_mount_inode_relabel(context_sid, sbsec,
832 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500833 if (rc)
834 goto out;
835 }
836 if (!rootcontext_sid)
837 rootcontext_sid = context_sid;
838
839 sbsec->mntpoint_sid = context_sid;
840 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
841 }
842
843 if (rootcontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100844 rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec,
845 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500846 if (rc)
847 goto out;
848
849 root_isec->sid = rootcontext_sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -0500850 root_isec->initialized = LABEL_INITIALIZED;
Eric Parisc9180a52007-11-30 13:00:35 -0500851 }
852
853 if (defcontext_sid) {
David Quigleyeb9ae682013-05-22 12:50:37 -0400854 if (sbsec->behavior != SECURITY_FS_USE_XATTR &&
855 sbsec->behavior != SECURITY_FS_USE_NATIVE) {
Eric Parisc9180a52007-11-30 13:00:35 -0500856 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +0200857 pr_warn("SELinux: defcontext option is "
Eric Parisc9180a52007-11-30 13:00:35 -0500858 "invalid for this filesystem type\n");
859 goto out;
860 }
861
862 if (defcontext_sid != sbsec->def_sid) {
863 rc = may_context_mount_inode_relabel(defcontext_sid,
David Howells275bb412008-11-14 10:39:19 +1100864 sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500865 if (rc)
866 goto out;
867 }
868
869 sbsec->def_sid = defcontext_sid;
870 }
871
Seth Forsheeaad82892016-04-26 14:36:20 -0500872out_set_opts:
Eric Parisc9180a52007-11-30 13:00:35 -0500873 rc = sb_finish_set_opts(sb);
874out:
Eric Parisbc7e9822006-09-25 23:32:02 -0700875 mutex_unlock(&sbsec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return rc;
Eric Parisc9180a52007-11-30 13:00:35 -0500877out_double_mount:
878 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +0200879 pr_warn("SELinux: mount invalid. Same superblock, different "
Al Virobd323652018-12-13 15:04:59 -0500880 "security settings for (dev %s, type %s)\n", sb->s_id,
881 sb->s_type->name);
Eric Parisc9180a52007-11-30 13:00:35 -0500882 goto out;
883}
884
Jeff Layton094f7b62013-04-01 08:14:24 -0400885static int selinux_cmp_sb_context(const struct super_block *oldsb,
886 const struct super_block *newsb)
887{
888 struct superblock_security_struct *old = oldsb->s_security;
889 struct superblock_security_struct *new = newsb->s_security;
890 char oldflags = old->flags & SE_MNTMASK;
891 char newflags = new->flags & SE_MNTMASK;
892
893 if (oldflags != newflags)
894 goto mismatch;
895 if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid)
896 goto mismatch;
897 if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid)
898 goto mismatch;
899 if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid)
900 goto mismatch;
901 if (oldflags & ROOTCONTEXT_MNT) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500902 struct inode_security_struct *oldroot = backing_inode_security(oldsb->s_root);
903 struct inode_security_struct *newroot = backing_inode_security(newsb->s_root);
Jeff Layton094f7b62013-04-01 08:14:24 -0400904 if (oldroot->sid != newroot->sid)
905 goto mismatch;
906 }
907 return 0;
908mismatch:
peter enderborgc103a912018-06-12 10:09:03 +0200909 pr_warn("SELinux: mount invalid. Same superblock, "
Jeff Layton094f7b62013-04-01 08:14:24 -0400910 "different security settings for (dev %s, "
911 "type %s)\n", newsb->s_id, newsb->s_type->name);
912 return -EBUSY;
913}
914
915static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400916 struct super_block *newsb,
917 unsigned long kern_flags,
918 unsigned long *set_kern_flags)
Eric Parisc9180a52007-11-30 13:00:35 -0500919{
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400920 int rc = 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500921 const struct superblock_security_struct *oldsbsec = oldsb->s_security;
922 struct superblock_security_struct *newsbsec = newsb->s_security;
923
924 int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT);
925 int set_context = (oldsbsec->flags & CONTEXT_MNT);
926 int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT);
927
Eric Paris0f5e6422008-04-21 16:24:11 -0400928 /*
929 * if the parent was able to be mounted it clearly had no special lsm
Al Viroe8c26252010-03-23 06:36:54 -0400930 * mount options. thus we can safely deal with this superblock later
Eric Paris0f5e6422008-04-21 16:24:11 -0400931 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500932 if (!selinux_state.initialized)
Jeff Layton094f7b62013-04-01 08:14:24 -0400933 return 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500934
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400935 /*
936 * Specifying internal flags without providing a place to
937 * place the results is not allowed.
938 */
939 if (kern_flags && !set_kern_flags)
940 return -EINVAL;
941
Eric Parisc9180a52007-11-30 13:00:35 -0500942 /* how can we clone if the old one wasn't set up?? */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500943 BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED));
Eric Parisc9180a52007-11-30 13:00:35 -0500944
Jeff Layton094f7b62013-04-01 08:14:24 -0400945 /* if fs is reusing a sb, make sure that the contexts match */
J. Bruce Fields3815a242019-03-05 16:17:58 -0500946 if (newsbsec->flags & SE_SBINITIALIZED) {
947 if ((kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context)
948 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
Jeff Layton094f7b62013-04-01 08:14:24 -0400949 return selinux_cmp_sb_context(oldsb, newsb);
J. Bruce Fields3815a242019-03-05 16:17:58 -0500950 }
Eric Paris5a552612008-04-09 14:08:35 -0400951
Eric Parisc9180a52007-11-30 13:00:35 -0500952 mutex_lock(&newsbsec->lock);
953
954 newsbsec->flags = oldsbsec->flags;
955
956 newsbsec->sid = oldsbsec->sid;
957 newsbsec->def_sid = oldsbsec->def_sid;
958 newsbsec->behavior = oldsbsec->behavior;
959
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400960 if (newsbsec->behavior == SECURITY_FS_USE_NATIVE &&
961 !(kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500962 rc = security_fs_use(&selinux_state, newsb);
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400963 if (rc)
964 goto out;
965 }
966
967 if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !set_context) {
968 newsbsec->behavior = SECURITY_FS_USE_NATIVE;
969 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
970 }
971
Eric Parisc9180a52007-11-30 13:00:35 -0500972 if (set_context) {
973 u32 sid = oldsbsec->mntpoint_sid;
974
975 if (!set_fscontext)
976 newsbsec->sid = sid;
977 if (!set_rootcontext) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500978 struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
Eric Parisc9180a52007-11-30 13:00:35 -0500979 newisec->sid = sid;
980 }
981 newsbsec->mntpoint_sid = sid;
982 }
983 if (set_rootcontext) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500984 const struct inode_security_struct *oldisec = backing_inode_security(oldsb->s_root);
985 struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
Eric Parisc9180a52007-11-30 13:00:35 -0500986
987 newisec->sid = oldisec->sid;
988 }
989
990 sb_finish_set_opts(newsb);
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400991out:
Eric Parisc9180a52007-11-30 13:00:35 -0500992 mutex_unlock(&newsbsec->lock);
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400993 return rc;
Eric Parisc9180a52007-11-30 13:00:35 -0500994}
995
Al Viroba641862018-12-14 20:28:15 -0500996static int selinux_add_opt(int token, const char *s, void **mnt_opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500997{
Al Viroba641862018-12-14 20:28:15 -0500998 struct selinux_mnt_opts *opts = *mnt_opts;
Eric Parisc9180a52007-11-30 13:00:35 -0500999
Al Viroda3d76a2018-12-17 10:14:16 -05001000 if (token == Opt_seclabel) /* eaten and completely ignored */
Al Viro169d68efb2018-12-14 22:44:50 -05001001 return 0;
Eric Parisc9180a52007-11-30 13:00:35 -05001002
Al Viroba641862018-12-14 20:28:15 -05001003 if (!opts) {
1004 opts = kzalloc(sizeof(struct selinux_mnt_opts), GFP_KERNEL);
1005 if (!opts)
1006 return -ENOMEM;
1007 *mnt_opts = opts;
1008 }
1009 if (!s)
1010 return -ENOMEM;
1011 switch (token) {
1012 case Opt_context:
1013 if (opts->context || opts->defcontext)
1014 goto Einval;
1015 opts->context = s;
1016 break;
1017 case Opt_fscontext:
1018 if (opts->fscontext)
1019 goto Einval;
1020 opts->fscontext = s;
1021 break;
1022 case Opt_rootcontext:
1023 if (opts->rootcontext)
1024 goto Einval;
1025 opts->rootcontext = s;
1026 break;
1027 case Opt_defcontext:
1028 if (opts->context || opts->defcontext)
1029 goto Einval;
1030 opts->defcontext = s;
1031 break;
1032 }
1033 return 0;
1034Einval:
1035 pr_warn(SEL_MOUNT_FAIL_MSG);
Al Viroba641862018-12-14 20:28:15 -05001036 return -EINVAL;
1037}
Eric Parisc9180a52007-11-30 13:00:35 -05001038
Al Viro757cbe52018-12-14 23:42:21 -05001039static int selinux_add_mnt_opt(const char *option, const char *val, int len,
1040 void **mnt_opts)
Eric Parisc9180a52007-11-30 13:00:35 -05001041{
Al Viro757cbe52018-12-14 23:42:21 -05001042 int token = Opt_error;
1043 int rc, i;
Eric Parisc9180a52007-11-30 13:00:35 -05001044
Al Viro757cbe52018-12-14 23:42:21 -05001045 for (i = 0; i < ARRAY_SIZE(tokens); i++) {
1046 if (strcmp(option, tokens[i].name) == 0) {
1047 token = tokens[i].opt;
Eric Parisc9180a52007-11-30 13:00:35 -05001048 break;
Eric Parisc9180a52007-11-30 13:00:35 -05001049 }
1050 }
1051
Al Viro757cbe52018-12-14 23:42:21 -05001052 if (token == Opt_error)
1053 return -EINVAL;
Eric Parise0007522008-03-05 10:31:54 -05001054
Al Viro757cbe52018-12-14 23:42:21 -05001055 if (token != Opt_seclabel)
1056 val = kmemdup_nul(val, len, GFP_KERNEL);
1057 rc = selinux_add_opt(token, val, mnt_opts);
1058 if (unlikely(rc)) {
1059 kfree(val);
1060 if (*mnt_opts) {
1061 selinux_free_mnt_opts(*mnt_opts);
1062 *mnt_opts = NULL;
1063 }
1064 }
1065 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Al Viroe3489f82018-12-13 00:24:36 -05001068static int show_sid(struct seq_file *m, u32 sid)
Eric Paris2069f452008-07-04 09:47:13 +10001069{
Al Viroe3489f82018-12-13 00:24:36 -05001070 char *context = NULL;
1071 u32 len;
1072 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Al Viroe3489f82018-12-13 00:24:36 -05001074 rc = security_sid_to_context(&selinux_state, sid,
1075 &context, &len);
1076 if (!rc) {
1077 bool has_comma = context && strchr(context, ',');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
David Howells442155c2018-11-01 23:07:24 +00001079 seq_putc(m, '=');
Eric Paris2069f452008-07-04 09:47:13 +10001080 if (has_comma)
1081 seq_putc(m, '\"');
Al Viroe3489f82018-12-13 00:24:36 -05001082 seq_escape(m, context, "\"\n\\");
Eric Paris2069f452008-07-04 09:47:13 +10001083 if (has_comma)
1084 seq_putc(m, '\"');
1085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 kfree(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 return rc;
1088}
Eric Paris2069f452008-07-04 09:47:13 +10001089
1090static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
1091{
Al Viroe3489f82018-12-13 00:24:36 -05001092 struct superblock_security_struct *sbsec = sb->s_security;
Eric Paris2069f452008-07-04 09:47:13 +10001093 int rc;
1094
Al Viroe3489f82018-12-13 00:24:36 -05001095 if (!(sbsec->flags & SE_SBINITIALIZED))
1096 return 0;
1097
1098 if (!selinux_state.initialized)
1099 return 0;
1100
1101 if (sbsec->flags & FSCONTEXT_MNT) {
1102 seq_putc(m, ',');
1103 seq_puts(m, FSCONTEXT_STR);
1104 rc = show_sid(m, sbsec->sid);
1105 if (rc)
1106 return rc;
Eric Paris383795c2008-07-29 17:07:26 -04001107 }
Al Viroe3489f82018-12-13 00:24:36 -05001108 if (sbsec->flags & CONTEXT_MNT) {
1109 seq_putc(m, ',');
1110 seq_puts(m, CONTEXT_STR);
1111 rc = show_sid(m, sbsec->mntpoint_sid);
1112 if (rc)
1113 return rc;
1114 }
1115 if (sbsec->flags & DEFCONTEXT_MNT) {
1116 seq_putc(m, ',');
1117 seq_puts(m, DEFCONTEXT_STR);
1118 rc = show_sid(m, sbsec->def_sid);
1119 if (rc)
1120 return rc;
1121 }
1122 if (sbsec->flags & ROOTCONTEXT_MNT) {
1123 struct dentry *root = sbsec->sb->s_root;
1124 struct inode_security_struct *isec = backing_inode_security(root);
1125 seq_putc(m, ',');
1126 seq_puts(m, ROOTCONTEXT_STR);
1127 rc = show_sid(m, isec->sid);
1128 if (rc)
1129 return rc;
1130 }
1131 if (sbsec->flags & SBLABEL_MNT) {
1132 seq_putc(m, ',');
David Howells442155c2018-11-01 23:07:24 +00001133 seq_puts(m, SECLABEL_STR);
Al Viroe3489f82018-12-13 00:24:36 -05001134 }
1135 return 0;
Eric Paris2069f452008-07-04 09:47:13 +10001136}
1137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138static inline u16 inode_mode_to_security_class(umode_t mode)
1139{
1140 switch (mode & S_IFMT) {
1141 case S_IFSOCK:
1142 return SECCLASS_SOCK_FILE;
1143 case S_IFLNK:
1144 return SECCLASS_LNK_FILE;
1145 case S_IFREG:
1146 return SECCLASS_FILE;
1147 case S_IFBLK:
1148 return SECCLASS_BLK_FILE;
1149 case S_IFDIR:
1150 return SECCLASS_DIR;
1151 case S_IFCHR:
1152 return SECCLASS_CHR_FILE;
1153 case S_IFIFO:
1154 return SECCLASS_FIFO_FILE;
1155
1156 }
1157
1158 return SECCLASS_FILE;
1159}
1160
James Morris13402582005-09-30 14:24:34 -04001161static inline int default_protocol_stream(int protocol)
1162{
1163 return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP);
1164}
1165
1166static inline int default_protocol_dgram(int protocol)
1167{
1168 return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
1169}
1170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171static inline u16 socket_type_to_security_class(int family, int type, int protocol)
1172{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001173 int extsockclass = selinux_policycap_extsockclass();
Stephen Smalleyda69a532017-01-09 10:07:30 -05001174
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 switch (family) {
1176 case PF_UNIX:
1177 switch (type) {
1178 case SOCK_STREAM:
1179 case SOCK_SEQPACKET:
1180 return SECCLASS_UNIX_STREAM_SOCKET;
1181 case SOCK_DGRAM:
Luis Ressel2a764b52017-07-25 15:13:41 -04001182 case SOCK_RAW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 return SECCLASS_UNIX_DGRAM_SOCKET;
1184 }
1185 break;
1186 case PF_INET:
1187 case PF_INET6:
1188 switch (type) {
1189 case SOCK_STREAM:
Stephen Smalleyda69a532017-01-09 10:07:30 -05001190 case SOCK_SEQPACKET:
James Morris13402582005-09-30 14:24:34 -04001191 if (default_protocol_stream(protocol))
1192 return SECCLASS_TCP_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001193 else if (extsockclass && protocol == IPPROTO_SCTP)
1194 return SECCLASS_SCTP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001195 else
1196 return SECCLASS_RAWIP_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 case SOCK_DGRAM:
James Morris13402582005-09-30 14:24:34 -04001198 if (default_protocol_dgram(protocol))
1199 return SECCLASS_UDP_SOCKET;
Stephen Smalleyef379792017-01-09 10:07:31 -05001200 else if (extsockclass && (protocol == IPPROTO_ICMP ||
1201 protocol == IPPROTO_ICMPV6))
Stephen Smalleyda69a532017-01-09 10:07:30 -05001202 return SECCLASS_ICMP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001203 else
1204 return SECCLASS_RAWIP_SOCKET;
James Morris2ee92d42006-11-13 16:09:01 -08001205 case SOCK_DCCP:
1206 return SECCLASS_DCCP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001207 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 return SECCLASS_RAWIP_SOCKET;
1209 }
1210 break;
1211 case PF_NETLINK:
1212 switch (protocol) {
1213 case NETLINK_ROUTE:
1214 return SECCLASS_NETLINK_ROUTE_SOCKET;
Pavel Emelyanov7f1fb602011-12-06 07:56:43 +00001215 case NETLINK_SOCK_DIAG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 return SECCLASS_NETLINK_TCPDIAG_SOCKET;
1217 case NETLINK_NFLOG:
1218 return SECCLASS_NETLINK_NFLOG_SOCKET;
1219 case NETLINK_XFRM:
1220 return SECCLASS_NETLINK_XFRM_SOCKET;
1221 case NETLINK_SELINUX:
1222 return SECCLASS_NETLINK_SELINUX_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001223 case NETLINK_ISCSI:
1224 return SECCLASS_NETLINK_ISCSI_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 case NETLINK_AUDIT:
1226 return SECCLASS_NETLINK_AUDIT_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001227 case NETLINK_FIB_LOOKUP:
1228 return SECCLASS_NETLINK_FIB_LOOKUP_SOCKET;
1229 case NETLINK_CONNECTOR:
1230 return SECCLASS_NETLINK_CONNECTOR_SOCKET;
1231 case NETLINK_NETFILTER:
1232 return SECCLASS_NETLINK_NETFILTER_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 case NETLINK_DNRTMSG:
1234 return SECCLASS_NETLINK_DNRT_SOCKET;
James Morris0c9b7942005-04-16 15:24:13 -07001235 case NETLINK_KOBJECT_UEVENT:
1236 return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001237 case NETLINK_GENERIC:
1238 return SECCLASS_NETLINK_GENERIC_SOCKET;
1239 case NETLINK_SCSITRANSPORT:
1240 return SECCLASS_NETLINK_SCSITRANSPORT_SOCKET;
1241 case NETLINK_RDMA:
1242 return SECCLASS_NETLINK_RDMA_SOCKET;
1243 case NETLINK_CRYPTO:
1244 return SECCLASS_NETLINK_CRYPTO_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 default:
1246 return SECCLASS_NETLINK_SOCKET;
1247 }
1248 case PF_PACKET:
1249 return SECCLASS_PACKET_SOCKET;
1250 case PF_KEY:
1251 return SECCLASS_KEY_SOCKET;
Christopher J. PeBenito3e3ff152006-06-09 00:25:03 -07001252 case PF_APPLETALK:
1253 return SECCLASS_APPLETALK_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 }
1255
Stephen Smalleyda69a532017-01-09 10:07:30 -05001256 if (extsockclass) {
1257 switch (family) {
1258 case PF_AX25:
1259 return SECCLASS_AX25_SOCKET;
1260 case PF_IPX:
1261 return SECCLASS_IPX_SOCKET;
1262 case PF_NETROM:
1263 return SECCLASS_NETROM_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001264 case PF_ATMPVC:
1265 return SECCLASS_ATMPVC_SOCKET;
1266 case PF_X25:
1267 return SECCLASS_X25_SOCKET;
1268 case PF_ROSE:
1269 return SECCLASS_ROSE_SOCKET;
1270 case PF_DECnet:
1271 return SECCLASS_DECNET_SOCKET;
1272 case PF_ATMSVC:
1273 return SECCLASS_ATMSVC_SOCKET;
1274 case PF_RDS:
1275 return SECCLASS_RDS_SOCKET;
1276 case PF_IRDA:
1277 return SECCLASS_IRDA_SOCKET;
1278 case PF_PPPOX:
1279 return SECCLASS_PPPOX_SOCKET;
1280 case PF_LLC:
1281 return SECCLASS_LLC_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001282 case PF_CAN:
1283 return SECCLASS_CAN_SOCKET;
1284 case PF_TIPC:
1285 return SECCLASS_TIPC_SOCKET;
1286 case PF_BLUETOOTH:
1287 return SECCLASS_BLUETOOTH_SOCKET;
1288 case PF_IUCV:
1289 return SECCLASS_IUCV_SOCKET;
1290 case PF_RXRPC:
1291 return SECCLASS_RXRPC_SOCKET;
1292 case PF_ISDN:
1293 return SECCLASS_ISDN_SOCKET;
1294 case PF_PHONET:
1295 return SECCLASS_PHONET_SOCKET;
1296 case PF_IEEE802154:
1297 return SECCLASS_IEEE802154_SOCKET;
1298 case PF_CAIF:
1299 return SECCLASS_CAIF_SOCKET;
1300 case PF_ALG:
1301 return SECCLASS_ALG_SOCKET;
1302 case PF_NFC:
1303 return SECCLASS_NFC_SOCKET;
1304 case PF_VSOCK:
1305 return SECCLASS_VSOCK_SOCKET;
1306 case PF_KCM:
1307 return SECCLASS_KCM_SOCKET;
1308 case PF_QIPCRTR:
1309 return SECCLASS_QIPCRTR_SOCKET;
Linus Torvalds3051bf32017-02-22 10:15:09 -08001310 case PF_SMC:
1311 return SECCLASS_SMC_SOCKET;
Björn Töpel68e8b842018-05-02 13:01:22 +02001312 case PF_XDP:
1313 return SECCLASS_XDP_SOCKET;
1314#if PF_MAX > 45
Stephen Smalleyda69a532017-01-09 10:07:30 -05001315#error New address family defined, please update this function.
1316#endif
1317 }
1318 }
1319
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 return SECCLASS_SOCKET;
1321}
1322
Stephen Smalley134509d2015-06-04 16:22:17 -04001323static int selinux_genfs_get_sid(struct dentry *dentry,
1324 u16 tclass,
1325 u16 flags,
1326 u32 *sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327{
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001328 int rc;
Al Virofc640052016-04-10 01:33:30 -04001329 struct super_block *sb = dentry->d_sb;
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001330 char *buffer, *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Eric Paris828dfe12008-04-17 13:17:49 -04001332 buffer = (char *)__get_free_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 if (!buffer)
1334 return -ENOMEM;
1335
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001336 path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
1337 if (IS_ERR(path))
1338 rc = PTR_ERR(path);
1339 else {
Stephen Smalley134509d2015-06-04 16:22:17 -04001340 if (flags & SE_SBPROC) {
1341 /* each process gets a /proc/PID/ entry. Strip off the
1342 * PID part to get a valid selinux labeling.
1343 * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
1344 while (path[1] >= '0' && path[1] <= '9') {
1345 path[1] = '/';
1346 path++;
1347 }
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001348 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001349 rc = security_genfs_sid(&selinux_state, sb->s_type->name,
1350 path, tclass, sid);
Stephen Smalley7bb185e2018-09-04 16:51:36 -04001351 if (rc == -ENOENT) {
1352 /* No match in policy, mark as unlabeled. */
1353 *sid = SECINITSID_UNLABELED;
1354 rc = 0;
1355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 free_page((unsigned long)buffer);
1358 return rc;
1359}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Ondrej Mosnacekb7540262019-02-22 15:57:14 +01001361static int inode_doinit_use_xattr(struct inode *inode, struct dentry *dentry,
1362 u32 def_sid, u32 *sid)
1363{
1364#define INITCONTEXTLEN 255
1365 char *context;
1366 unsigned int len;
1367 int rc;
1368
1369 len = INITCONTEXTLEN;
1370 context = kmalloc(len + 1, GFP_NOFS);
1371 if (!context)
1372 return -ENOMEM;
1373
1374 context[len] = '\0';
1375 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
1376 if (rc == -ERANGE) {
1377 kfree(context);
1378
1379 /* Need a larger buffer. Query for the right size. */
1380 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, NULL, 0);
1381 if (rc < 0)
1382 return rc;
1383
1384 len = rc;
1385 context = kmalloc(len + 1, GFP_NOFS);
1386 if (!context)
1387 return -ENOMEM;
1388
1389 context[len] = '\0';
1390 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX,
1391 context, len);
1392 }
1393 if (rc < 0) {
1394 kfree(context);
1395 if (rc != -ENODATA) {
1396 pr_warn("SELinux: %s: getxattr returned %d for dev=%s ino=%ld\n",
1397 __func__, -rc, inode->i_sb->s_id, inode->i_ino);
1398 return rc;
1399 }
1400 *sid = def_sid;
1401 return 0;
1402 }
1403
1404 rc = security_context_to_sid_default(&selinux_state, context, rc, sid,
1405 def_sid, GFP_NOFS);
1406 if (rc) {
1407 char *dev = inode->i_sb->s_id;
1408 unsigned long ino = inode->i_ino;
1409
1410 if (rc == -EINVAL) {
1411 pr_notice_ratelimited("SELinux: inode=%lu on dev=%s was found to have an invalid context=%s. This indicates you may need to relabel the inode or the filesystem in question.\n",
1412 ino, dev, context);
1413 } else {
1414 pr_warn("SELinux: %s: context_to_sid(%s) returned %d for dev=%s ino=%ld\n",
1415 __func__, context, -rc, dev, ino);
1416 }
1417 }
1418 kfree(context);
1419 return 0;
1420}
1421
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422/* The inode's security attributes must be initialized before first use. */
1423static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
1424{
1425 struct superblock_security_struct *sbsec = NULL;
Casey Schaufler80788c22018-09-21 17:19:11 -07001426 struct inode_security_struct *isec = selinux_inode(inode);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001427 u32 task_sid, sid = 0;
1428 u16 sclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05001432 if (isec->initialized == LABEL_INITIALIZED)
Andreas Gruenbacher13457d02016-11-10 22:18:29 +01001433 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001435 spin_lock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05001436 if (isec->initialized == LABEL_INITIALIZED)
Eric Paris23970742006-09-25 23:32:01 -07001437 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Andreas Gruenbacher13457d02016-11-10 22:18:29 +01001439 if (isec->sclass == SECCLASS_FILE)
1440 isec->sclass = inode_mode_to_security_class(inode->i_mode);
1441
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 sbsec = inode->i_sb->s_security;
David P. Quigley0d90a7e2009-01-16 09:22:02 -05001443 if (!(sbsec->flags & SE_SBINITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 /* Defer initialization until selinux_complete_init,
1445 after the initial policy is loaded and the security
1446 server is ready to handle calls. */
1447 spin_lock(&sbsec->isec_lock);
1448 if (list_empty(&isec->list))
1449 list_add(&isec->list, &sbsec->isec_head);
1450 spin_unlock(&sbsec->isec_lock);
Eric Paris23970742006-09-25 23:32:01 -07001451 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 }
1453
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001454 sclass = isec->sclass;
1455 task_sid = isec->task_sid;
1456 sid = isec->sid;
1457 isec->initialized = LABEL_PENDING;
1458 spin_unlock(&isec->lock);
1459
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 switch (sbsec->behavior) {
David Quigleyeb9ae682013-05-22 12:50:37 -04001461 case SECURITY_FS_USE_NATIVE:
1462 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 case SECURITY_FS_USE_XATTR:
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001464 if (!(inode->i_opflags & IOP_XATTR)) {
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001465 sid = sbsec->def_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 break;
1467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 /* Need a dentry, since the xattr API requires one.
1469 Life would be simpler if we could just pass the inode. */
1470 if (opt_dentry) {
1471 /* Called from d_instantiate or d_splice_alias. */
1472 dentry = dget(opt_dentry);
1473 } else {
Al Virob1271252018-04-25 10:28:38 -04001474 /*
1475 * Called from selinux_complete_init, try to find a dentry.
1476 * Some filesystems really want a connected one, so try
1477 * that first. We could split SECURITY_FS_USE_XATTR in
1478 * two, depending upon that...
1479 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 dentry = d_find_alias(inode);
Al Virob1271252018-04-25 10:28:38 -04001481 if (!dentry)
1482 dentry = d_find_any_alias(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 }
1484 if (!dentry) {
Eric Parisdf7f54c2009-03-09 14:35:58 -04001485 /*
1486 * this is can be hit on boot when a file is accessed
1487 * before the policy is loaded. When we load policy we
1488 * may find inodes that have no dentry on the
1489 * sbsec->isec_head list. No reason to complain as these
1490 * will get fixed up the next time we go through
1491 * inode_doinit with a dentry, before these inodes could
1492 * be used again by userspace.
1493 */
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001494 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 }
1496
Ondrej Mosnacekb7540262019-02-22 15:57:14 +01001497 rc = inode_doinit_use_xattr(inode, dentry, sbsec->def_sid,
1498 &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 dput(dentry);
Ondrej Mosnacekb7540262019-02-22 15:57:14 +01001500 if (rc)
1501 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 break;
1503 case SECURITY_FS_USE_TASK:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001504 sid = task_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 break;
1506 case SECURITY_FS_USE_TRANS:
1507 /* Default to the fs SID. */
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001508 sid = sbsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
1510 /* Try to obtain a transition SID. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001511 rc = security_transition_sid(&selinux_state, task_sid, sid,
1512 sclass, NULL, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 if (rc)
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001514 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 break;
Eric Parisc312feb2006-07-10 04:43:53 -07001516 case SECURITY_FS_USE_MNTPOINT:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001517 sid = sbsec->mntpoint_sid;
Eric Parisc312feb2006-07-10 04:43:53 -07001518 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 default:
Eric Parisc312feb2006-07-10 04:43:53 -07001520 /* Default to the fs superblock SID. */
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001521 sid = sbsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Stephen Smalley134509d2015-06-04 16:22:17 -04001523 if ((sbsec->flags & SE_SBGENFS) && !S_ISLNK(inode->i_mode)) {
Paul Mooref64410e2014-03-19 16:46:18 -04001524 /* We must have a dentry to determine the label on
1525 * procfs inodes */
Al Virob1271252018-04-25 10:28:38 -04001526 if (opt_dentry) {
Paul Mooref64410e2014-03-19 16:46:18 -04001527 /* Called from d_instantiate or
1528 * d_splice_alias. */
1529 dentry = dget(opt_dentry);
Al Virob1271252018-04-25 10:28:38 -04001530 } else {
Paul Mooref64410e2014-03-19 16:46:18 -04001531 /* Called from selinux_complete_init, try to
Al Virob1271252018-04-25 10:28:38 -04001532 * find a dentry. Some filesystems really want
1533 * a connected one, so try that first.
1534 */
Paul Mooref64410e2014-03-19 16:46:18 -04001535 dentry = d_find_alias(inode);
Al Virob1271252018-04-25 10:28:38 -04001536 if (!dentry)
1537 dentry = d_find_any_alias(inode);
1538 }
Paul Mooref64410e2014-03-19 16:46:18 -04001539 /*
1540 * This can be hit on boot when a file is accessed
1541 * before the policy is loaded. When we load policy we
1542 * may find inodes that have no dentry on the
1543 * sbsec->isec_head list. No reason to complain as
1544 * these will get fixed up the next time we go through
1545 * inode_doinit() with a dentry, before these inodes
1546 * could be used again by userspace.
1547 */
1548 if (!dentry)
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001549 goto out;
1550 rc = selinux_genfs_get_sid(dentry, sclass,
Stephen Smalley134509d2015-06-04 16:22:17 -04001551 sbsec->flags, &sid);
Ondrej Mosnacekb7540262019-02-22 15:57:14 +01001552 if (rc) {
1553 dput(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001554 goto out;
Ondrej Mosnacekb7540262019-02-22 15:57:14 +01001555 }
1556
1557 if ((sbsec->flags & SE_SBGENFS_XATTR) &&
1558 (inode->i_opflags & IOP_XATTR)) {
1559 rc = inode_doinit_use_xattr(inode, dentry,
1560 sid, &sid);
1561 if (rc) {
1562 dput(dentry);
1563 goto out;
1564 }
1565 }
1566 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 }
1568 break;
1569 }
1570
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001571out:
1572 spin_lock(&isec->lock);
1573 if (isec->initialized == LABEL_PENDING) {
1574 if (!sid || rc) {
1575 isec->initialized = LABEL_INVALID;
1576 goto out_unlock;
1577 }
1578
1579 isec->initialized = LABEL_INITIALIZED;
1580 isec->sid = sid;
1581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
Eric Paris23970742006-09-25 23:32:01 -07001583out_unlock:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001584 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 return rc;
1586}
1587
1588/* Convert a Linux signal to an access vector. */
1589static inline u32 signal_to_av(int sig)
1590{
1591 u32 perm = 0;
1592
1593 switch (sig) {
1594 case SIGCHLD:
1595 /* Commonly granted from child to parent. */
1596 perm = PROCESS__SIGCHLD;
1597 break;
1598 case SIGKILL:
1599 /* Cannot be caught or ignored */
1600 perm = PROCESS__SIGKILL;
1601 break;
1602 case SIGSTOP:
1603 /* Cannot be caught or ignored */
1604 perm = PROCESS__SIGSTOP;
1605 break;
1606 default:
1607 /* All other signals. */
1608 perm = PROCESS__SIGNAL;
1609 break;
1610 }
1611
1612 return perm;
1613}
1614
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001615#if CAP_LAST_CAP > 63
1616#error Fix SELinux to handle capabilities > 63.
1617#endif
1618
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619/* Check whether a task is allowed to use a capability. */
Eric Paris6a9de492012-01-03 12:25:14 -05001620static int cred_has_capability(const struct cred *cred,
Micah Mortonc1a85a02019-01-07 16:10:53 -08001621 int cap, unsigned int opts, bool initns)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622{
Thomas Liu2bf49692009-07-14 12:14:09 -04001623 struct common_audit_data ad;
Eric Paris06112162008-11-11 22:02:50 +11001624 struct av_decision avd;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001625 u16 sclass;
David Howells3699c532009-01-06 22:27:01 +00001626 u32 sid = cred_sid(cred);
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001627 u32 av = CAP_TO_MASK(cap);
Eric Paris06112162008-11-11 22:02:50 +11001628 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Eric Paris50c205f2012-04-04 15:01:43 -04001630 ad.type = LSM_AUDIT_DATA_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 ad.u.cap = cap;
1632
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001633 switch (CAP_TO_INDEX(cap)) {
1634 case 0:
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04001635 sclass = initns ? SECCLASS_CAPABILITY : SECCLASS_CAP_USERNS;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001636 break;
1637 case 1:
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04001638 sclass = initns ? SECCLASS_CAPABILITY2 : SECCLASS_CAP2_USERNS;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001639 break;
1640 default:
peter enderborgc103a912018-06-12 10:09:03 +02001641 pr_err("SELinux: out of range capability %d\n", cap);
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001642 BUG();
Eric Parisa35c6c832011-04-20 10:21:28 -04001643 return -EINVAL;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001644 }
Eric Paris06112162008-11-11 22:02:50 +11001645
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001646 rc = avc_has_perm_noaudit(&selinux_state,
1647 sid, sid, sclass, av, 0, &avd);
Micah Mortonc1a85a02019-01-07 16:10:53 -08001648 if (!(opts & CAP_OPT_NOAUDIT)) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001649 int rc2 = avc_audit(&selinux_state,
1650 sid, sid, sclass, av, &avd, rc, &ad, 0);
Eric Paris9ade0cf2011-04-25 16:26:29 -04001651 if (rc2)
1652 return rc2;
1653 }
Eric Paris06112162008-11-11 22:02:50 +11001654 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655}
1656
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657/* Check whether a task has a particular permission to an inode.
1658 The 'adp' parameter is optional and allows other audit
1659 data to be passed (e.g. the dentry). */
David Howells88e67f32008-11-14 10:39:21 +11001660static int inode_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 struct inode *inode,
1662 u32 perms,
Linus Torvalds19e49832013-10-04 12:54:11 -07001663 struct common_audit_data *adp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 struct inode_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +11001666 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
David Howellse0e81732009-09-02 09:13:40 +01001668 validate_creds(cred);
1669
Eric Paris828dfe12008-04-17 13:17:49 -04001670 if (unlikely(IS_PRIVATE(inode)))
Stephen Smalleybbaca6c2007-02-14 00:34:16 -08001671 return 0;
1672
David Howells88e67f32008-11-14 10:39:21 +11001673 sid = cred_sid(cred);
Casey Schaufler80788c22018-09-21 17:19:11 -07001674 isec = selinux_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001676 return avc_has_perm(&selinux_state,
1677 sid, isec->sid, isec->sclass, perms, adp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678}
1679
1680/* Same as inode_has_perm, but pass explicit audit data containing
1681 the dentry to help the auditing code to more easily generate the
1682 pathname if needed. */
David Howells88e67f32008-11-14 10:39:21 +11001683static inline int dentry_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 struct dentry *dentry,
1685 u32 av)
1686{
David Howellsc6f493d2015-03-17 22:26:22 +00001687 struct inode *inode = d_backing_inode(dentry);
Thomas Liu2bf49692009-07-14 12:14:09 -04001688 struct common_audit_data ad;
David Howells88e67f32008-11-14 10:39:21 +11001689
Eric Paris50c205f2012-04-04 15:01:43 -04001690 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Paris2875fa02011-04-28 16:04:24 -04001691 ad.u.dentry = dentry;
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05001692 __inode_security_revalidate(inode, dentry, true);
Linus Torvalds19e49832013-10-04 12:54:11 -07001693 return inode_has_perm(cred, inode, av, &ad);
Eric Paris2875fa02011-04-28 16:04:24 -04001694}
1695
1696/* Same as inode_has_perm, but pass explicit audit data containing
1697 the path to help the auditing code to more easily generate the
1698 pathname if needed. */
1699static inline int path_has_perm(const struct cred *cred,
Al Viro3f7036a2015-03-08 19:28:30 -04001700 const struct path *path,
Eric Paris2875fa02011-04-28 16:04:24 -04001701 u32 av)
1702{
David Howellsc6f493d2015-03-17 22:26:22 +00001703 struct inode *inode = d_backing_inode(path->dentry);
Eric Paris2875fa02011-04-28 16:04:24 -04001704 struct common_audit_data ad;
1705
Eric Paris50c205f2012-04-04 15:01:43 -04001706 ad.type = LSM_AUDIT_DATA_PATH;
Eric Paris2875fa02011-04-28 16:04:24 -04001707 ad.u.path = *path;
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05001708 __inode_security_revalidate(inode, path->dentry, true);
Linus Torvalds19e49832013-10-04 12:54:11 -07001709 return inode_has_perm(cred, inode, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710}
1711
David Howells13f8e982013-06-13 23:37:55 +01001712/* Same as path_has_perm, but uses the inode from the file struct. */
1713static inline int file_path_has_perm(const struct cred *cred,
1714 struct file *file,
1715 u32 av)
1716{
1717 struct common_audit_data ad;
1718
Vivek Goyal43af5de2016-09-09 11:37:49 -04001719 ad.type = LSM_AUDIT_DATA_FILE;
1720 ad.u.file = file;
Linus Torvalds19e49832013-10-04 12:54:11 -07001721 return inode_has_perm(cred, file_inode(file), av, &ad);
David Howells13f8e982013-06-13 23:37:55 +01001722}
1723
Chenbo Fengf66e4482017-10-18 13:00:26 -07001724#ifdef CONFIG_BPF_SYSCALL
1725static int bpf_fd_pass(struct file *file, u32 sid);
1726#endif
1727
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728/* Check whether a task can use an open file descriptor to
1729 access an inode in a given way. Check access to the
1730 descriptor itself, and then use dentry_has_perm to
1731 check a particular permission to the file.
1732 Access to the descriptor is implicitly granted if it
1733 has the same SID as the process. If av is zero, then
1734 access to the file is not checked, e.g. for cases
1735 where only the descriptor is affected like seek. */
David Howells88e67f32008-11-14 10:39:21 +11001736static int file_has_perm(const struct cred *cred,
1737 struct file *file,
1738 u32 av)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739{
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07001740 struct file_security_struct *fsec = selinux_file(file);
Al Viro496ad9a2013-01-23 17:07:38 -05001741 struct inode *inode = file_inode(file);
Thomas Liu2bf49692009-07-14 12:14:09 -04001742 struct common_audit_data ad;
David Howells88e67f32008-11-14 10:39:21 +11001743 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 int rc;
1745
Vivek Goyal43af5de2016-09-09 11:37:49 -04001746 ad.type = LSM_AUDIT_DATA_FILE;
1747 ad.u.file = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
David Howells275bb412008-11-14 10:39:19 +11001749 if (sid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001750 rc = avc_has_perm(&selinux_state,
1751 sid, fsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 SECCLASS_FD,
1753 FD__USE,
1754 &ad);
1755 if (rc)
David Howells88e67f32008-11-14 10:39:21 +11001756 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 }
1758
Chenbo Fengf66e4482017-10-18 13:00:26 -07001759#ifdef CONFIG_BPF_SYSCALL
1760 rc = bpf_fd_pass(file, cred_sid(cred));
1761 if (rc)
1762 return rc;
1763#endif
1764
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 /* av is zero if only checking access to the descriptor. */
David Howells88e67f32008-11-14 10:39:21 +11001766 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 if (av)
Linus Torvalds19e49832013-10-04 12:54:11 -07001768 rc = inode_has_perm(cred, inode, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
David Howells88e67f32008-11-14 10:39:21 +11001770out:
1771 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772}
1773
David Howellsc3c188b2015-07-10 17:19:58 -04001774/*
1775 * Determine the label for an inode that might be unioned.
1776 */
Vivek Goyalc957f6d2016-07-13 10:44:51 -04001777static int
1778selinux_determine_inode_label(const struct task_security_struct *tsec,
1779 struct inode *dir,
1780 const struct qstr *name, u16 tclass,
1781 u32 *_new_isid)
David Howellsc3c188b2015-07-10 17:19:58 -04001782{
1783 const struct superblock_security_struct *sbsec = dir->i_sb->s_security;
David Howellsc3c188b2015-07-10 17:19:58 -04001784
1785 if ((sbsec->flags & SE_SBINITIALIZED) &&
1786 (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)) {
1787 *_new_isid = sbsec->mntpoint_sid;
1788 } else if ((sbsec->flags & SBLABEL_MNT) &&
1789 tsec->create_sid) {
1790 *_new_isid = tsec->create_sid;
1791 } else {
Paul Moore20cdef82016-04-04 14:14:42 -04001792 const struct inode_security_struct *dsec = inode_security(dir);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001793 return security_transition_sid(&selinux_state, tsec->sid,
1794 dsec->sid, tclass,
David Howellsc3c188b2015-07-10 17:19:58 -04001795 name, _new_isid);
1796 }
1797
1798 return 0;
1799}
1800
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801/* Check whether a task can create a file. */
1802static int may_create(struct inode *dir,
1803 struct dentry *dentry,
1804 u16 tclass)
1805{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07001806 const struct task_security_struct *tsec = selinux_cred(current_cred());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 struct inode_security_struct *dsec;
1808 struct superblock_security_struct *sbsec;
David Howells275bb412008-11-14 10:39:19 +11001809 u32 sid, newsid;
Thomas Liu2bf49692009-07-14 12:14:09 -04001810 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 int rc;
1812
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001813 dsec = inode_security(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 sbsec = dir->i_sb->s_security;
1815
David Howells275bb412008-11-14 10:39:19 +11001816 sid = tsec->sid;
David Howells275bb412008-11-14 10:39:19 +11001817
Eric Paris50c205f2012-04-04 15:01:43 -04001818 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04001819 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001821 rc = avc_has_perm(&selinux_state,
1822 sid, dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 DIR__ADD_NAME | DIR__SEARCH,
1824 &ad);
1825 if (rc)
1826 return rc;
1827
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07001828 rc = selinux_determine_inode_label(selinux_cred(current_cred()), dir,
Vivek Goyalc957f6d2016-07-13 10:44:51 -04001829 &dentry->d_name, tclass, &newsid);
David Howellsc3c188b2015-07-10 17:19:58 -04001830 if (rc)
1831 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001833 rc = avc_has_perm(&selinux_state,
1834 sid, newsid, tclass, FILE__CREATE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 if (rc)
1836 return rc;
1837
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001838 return avc_has_perm(&selinux_state,
1839 newsid, sbsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 SECCLASS_FILESYSTEM,
1841 FILESYSTEM__ASSOCIATE, &ad);
1842}
1843
Eric Paris828dfe12008-04-17 13:17:49 -04001844#define MAY_LINK 0
1845#define MAY_UNLINK 1
1846#define MAY_RMDIR 2
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
1848/* Check whether a task can link, unlink, or rmdir a file/directory. */
1849static int may_link(struct inode *dir,
1850 struct dentry *dentry,
1851 int kind)
1852
1853{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 struct inode_security_struct *dsec, *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001855 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11001856 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 u32 av;
1858 int rc;
1859
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001860 dsec = inode_security(dir);
1861 isec = backing_inode_security(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
Eric Paris50c205f2012-04-04 15:01:43 -04001863 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04001864 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
1866 av = DIR__SEARCH;
1867 av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001868 rc = avc_has_perm(&selinux_state,
1869 sid, dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 if (rc)
1871 return rc;
1872
1873 switch (kind) {
1874 case MAY_LINK:
1875 av = FILE__LINK;
1876 break;
1877 case MAY_UNLINK:
1878 av = FILE__UNLINK;
1879 break;
1880 case MAY_RMDIR:
1881 av = DIR__RMDIR;
1882 break;
1883 default:
peter enderborgc103a912018-06-12 10:09:03 +02001884 pr_warn("SELinux: %s: unrecognized kind %d\n",
Eric Paris744ba352008-04-17 11:52:44 -04001885 __func__, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 return 0;
1887 }
1888
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001889 rc = avc_has_perm(&selinux_state,
1890 sid, isec->sid, isec->sclass, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 return rc;
1892}
1893
1894static inline int may_rename(struct inode *old_dir,
1895 struct dentry *old_dentry,
1896 struct inode *new_dir,
1897 struct dentry *new_dentry)
1898{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001900 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11001901 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 u32 av;
1903 int old_is_dir, new_is_dir;
1904 int rc;
1905
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001906 old_dsec = inode_security(old_dir);
1907 old_isec = backing_inode_security(old_dentry);
David Howellse36cb0b2015-01-29 12:02:35 +00001908 old_is_dir = d_is_dir(old_dentry);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001909 new_dsec = inode_security(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
Eric Paris50c205f2012-04-04 15:01:43 -04001911 ad.type = LSM_AUDIT_DATA_DENTRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
Eric Parisa2694342011-04-25 13:10:27 -04001913 ad.u.dentry = old_dentry;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001914 rc = avc_has_perm(&selinux_state,
1915 sid, old_dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 DIR__REMOVE_NAME | DIR__SEARCH, &ad);
1917 if (rc)
1918 return rc;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001919 rc = avc_has_perm(&selinux_state,
1920 sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 old_isec->sclass, FILE__RENAME, &ad);
1922 if (rc)
1923 return rc;
1924 if (old_is_dir && new_dir != old_dir) {
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, DIR__REPARENT, &ad);
1928 if (rc)
1929 return rc;
1930 }
1931
Eric Parisa2694342011-04-25 13:10:27 -04001932 ad.u.dentry = new_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 av = DIR__ADD_NAME | DIR__SEARCH;
David Howells2c616d42015-01-29 12:02:33 +00001934 if (d_is_positive(new_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 av |= DIR__REMOVE_NAME;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001936 rc = avc_has_perm(&selinux_state,
1937 sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 if (rc)
1939 return rc;
David Howells2c616d42015-01-29 12:02:33 +00001940 if (d_is_positive(new_dentry)) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001941 new_isec = backing_inode_security(new_dentry);
David Howellse36cb0b2015-01-29 12:02:35 +00001942 new_is_dir = d_is_dir(new_dentry);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001943 rc = avc_has_perm(&selinux_state,
1944 sid, new_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 new_isec->sclass,
1946 (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
1947 if (rc)
1948 return rc;
1949 }
1950
1951 return 0;
1952}
1953
1954/* Check whether a task can perform a filesystem operation. */
David Howells88e67f32008-11-14 10:39:21 +11001955static int superblock_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 struct super_block *sb,
1957 u32 perms,
Thomas Liu2bf49692009-07-14 12:14:09 -04001958 struct common_audit_data *ad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 struct superblock_security_struct *sbsec;
David Howells88e67f32008-11-14 10:39:21 +11001961 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 sbsec = sb->s_security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001964 return avc_has_perm(&selinux_state,
1965 sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966}
1967
1968/* Convert a Linux mode and permission mask to an access vector. */
1969static inline u32 file_mask_to_av(int mode, int mask)
1970{
1971 u32 av = 0;
1972
Al Virodba19c62011-07-25 20:49:29 -04001973 if (!S_ISDIR(mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 if (mask & MAY_EXEC)
1975 av |= FILE__EXECUTE;
1976 if (mask & MAY_READ)
1977 av |= FILE__READ;
1978
1979 if (mask & MAY_APPEND)
1980 av |= FILE__APPEND;
1981 else if (mask & MAY_WRITE)
1982 av |= FILE__WRITE;
1983
1984 } else {
1985 if (mask & MAY_EXEC)
1986 av |= DIR__SEARCH;
1987 if (mask & MAY_WRITE)
1988 av |= DIR__WRITE;
1989 if (mask & MAY_READ)
1990 av |= DIR__READ;
1991 }
1992
1993 return av;
1994}
1995
1996/* Convert a Linux file to an access vector. */
1997static inline u32 file_to_av(struct file *file)
1998{
1999 u32 av = 0;
2000
2001 if (file->f_mode & FMODE_READ)
2002 av |= FILE__READ;
2003 if (file->f_mode & FMODE_WRITE) {
2004 if (file->f_flags & O_APPEND)
2005 av |= FILE__APPEND;
2006 else
2007 av |= FILE__WRITE;
2008 }
Stephen Smalley0794c662008-03-17 08:55:18 -04002009 if (!av) {
2010 /*
2011 * Special file opened with flags 3 for ioctl-only use.
2012 */
2013 av = FILE__IOCTL;
2014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
2016 return av;
2017}
2018
Eric Paris8b6a5a32008-10-29 17:06:46 -04002019/*
2020 * Convert a file to an access vector and include the correct open
2021 * open permission.
2022 */
2023static inline u32 open_file_to_av(struct file *file)
2024{
2025 u32 av = file_to_av(file);
Stephen Smalleyccb54472017-05-12 12:41:24 -04002026 struct inode *inode = file_inode(file);
Eric Paris8b6a5a32008-10-29 17:06:46 -04002027
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002028 if (selinux_policycap_openperm() &&
2029 inode->i_sb->s_magic != SOCKFS_MAGIC)
Eric Paris49b7b8d2010-07-23 11:44:09 -04002030 av |= FILE__OPEN;
2031
Eric Paris8b6a5a32008-10-29 17:06:46 -04002032 return av;
2033}
2034
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035/* Hook functions begin here. */
2036
Stephen Smalley79af7302015-01-21 10:54:10 -05002037static int selinux_binder_set_context_mgr(struct task_struct *mgr)
2038{
2039 u32 mysid = current_sid();
2040 u32 mgrsid = task_sid(mgr);
2041
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002042 return avc_has_perm(&selinux_state,
2043 mysid, mgrsid, SECCLASS_BINDER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002044 BINDER__SET_CONTEXT_MGR, NULL);
2045}
2046
2047static int selinux_binder_transaction(struct task_struct *from,
2048 struct task_struct *to)
2049{
2050 u32 mysid = current_sid();
2051 u32 fromsid = task_sid(from);
2052 u32 tosid = task_sid(to);
2053 int rc;
2054
2055 if (mysid != fromsid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002056 rc = avc_has_perm(&selinux_state,
2057 mysid, fromsid, SECCLASS_BINDER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002058 BINDER__IMPERSONATE, NULL);
2059 if (rc)
2060 return rc;
2061 }
2062
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002063 return avc_has_perm(&selinux_state,
2064 fromsid, tosid, SECCLASS_BINDER, BINDER__CALL,
Stephen Smalley79af7302015-01-21 10:54:10 -05002065 NULL);
2066}
2067
2068static int selinux_binder_transfer_binder(struct task_struct *from,
2069 struct task_struct *to)
2070{
2071 u32 fromsid = task_sid(from);
2072 u32 tosid = task_sid(to);
2073
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002074 return avc_has_perm(&selinux_state,
2075 fromsid, tosid, SECCLASS_BINDER, BINDER__TRANSFER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002076 NULL);
2077}
2078
2079static int selinux_binder_transfer_file(struct task_struct *from,
2080 struct task_struct *to,
2081 struct file *file)
2082{
2083 u32 sid = task_sid(to);
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07002084 struct file_security_struct *fsec = selinux_file(file);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002085 struct dentry *dentry = file->f_path.dentry;
Paul Moore20cdef82016-04-04 14:14:42 -04002086 struct inode_security_struct *isec;
Stephen Smalley79af7302015-01-21 10:54:10 -05002087 struct common_audit_data ad;
2088 int rc;
2089
2090 ad.type = LSM_AUDIT_DATA_PATH;
2091 ad.u.path = file->f_path;
2092
2093 if (sid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002094 rc = avc_has_perm(&selinux_state,
2095 sid, fsec->sid,
Stephen Smalley79af7302015-01-21 10:54:10 -05002096 SECCLASS_FD,
2097 FD__USE,
2098 &ad);
2099 if (rc)
2100 return rc;
2101 }
2102
Chenbo Fengf66e4482017-10-18 13:00:26 -07002103#ifdef CONFIG_BPF_SYSCALL
2104 rc = bpf_fd_pass(file, sid);
2105 if (rc)
2106 return rc;
2107#endif
2108
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002109 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
Stephen Smalley79af7302015-01-21 10:54:10 -05002110 return 0;
2111
Paul Moore20cdef82016-04-04 14:14:42 -04002112 isec = backing_inode_security(dentry);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002113 return avc_has_perm(&selinux_state,
2114 sid, isec->sid, isec->sclass, file_to_av(file),
Stephen Smalley79af7302015-01-21 10:54:10 -05002115 &ad);
2116}
2117
Ingo Molnar9e488582009-05-07 19:26:19 +10002118static int selinux_ptrace_access_check(struct task_struct *child,
David Howells5cd9c582008-08-14 11:37:28 +01002119 unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002121 u32 sid = current_sid();
2122 u32 csid = task_sid(child);
Stephen Smalley006ebb42008-05-19 08:32:49 -04002123
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002124 if (mode & PTRACE_MODE_READ)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002125 return avc_has_perm(&selinux_state,
2126 sid, csid, SECCLASS_FILE, FILE__READ, NULL);
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002127
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002128 return avc_has_perm(&selinux_state,
2129 sid, csid, SECCLASS_PROCESS, PROCESS__PTRACE, NULL);
David Howells5cd9c582008-08-14 11:37:28 +01002130}
2131
2132static int selinux_ptrace_traceme(struct task_struct *parent)
2133{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002134 return avc_has_perm(&selinux_state,
2135 task_sid(parent), current_sid(), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002136 PROCESS__PTRACE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137}
2138
2139static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
Eric Paris828dfe12008-04-17 13:17:49 -04002140 kernel_cap_t *inheritable, kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002142 return avc_has_perm(&selinux_state,
2143 current_sid(), task_sid(target), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002144 PROCESS__GETCAP, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145}
2146
David Howellsd84f4f92008-11-14 10:39:23 +11002147static int selinux_capset(struct cred *new, const struct cred *old,
2148 const kernel_cap_t *effective,
2149 const kernel_cap_t *inheritable,
2150 const kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002152 return avc_has_perm(&selinux_state,
2153 cred_sid(old), cred_sid(new), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002154 PROCESS__SETCAP, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155}
2156
James Morris5626d3e2009-01-30 10:05:06 +11002157/*
2158 * (This comment used to live with the selinux_task_setuid hook,
2159 * which was removed).
2160 *
2161 * Since setuid only affects the current process, and since the SELinux
2162 * controls are not based on the Linux identity attributes, SELinux does not
2163 * need to control this operation. However, SELinux does control the use of
2164 * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
2165 */
2166
Eric Paris6a9de492012-01-03 12:25:14 -05002167static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
Micah Mortonc1a85a02019-01-07 16:10:53 -08002168 int cap, unsigned int opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169{
Micah Mortonc1a85a02019-01-07 16:10:53 -08002170 return cred_has_capability(cred, cap, opts, ns == &init_user_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171}
2172
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
2174{
David Howells88e67f32008-11-14 10:39:21 +11002175 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 int rc = 0;
2177
2178 if (!sb)
2179 return 0;
2180
2181 switch (cmds) {
Eric Paris828dfe12008-04-17 13:17:49 -04002182 case Q_SYNC:
2183 case Q_QUOTAON:
2184 case Q_QUOTAOFF:
2185 case Q_SETINFO:
2186 case Q_SETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11002187 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04002188 break;
2189 case Q_GETFMT:
2190 case Q_GETINFO:
2191 case Q_GETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11002192 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04002193 break;
2194 default:
2195 rc = 0; /* let the kernel handle invalid cmds */
2196 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 }
2198 return rc;
2199}
2200
2201static int selinux_quota_on(struct dentry *dentry)
2202{
David Howells88e67f32008-11-14 10:39:21 +11002203 const struct cred *cred = current_cred();
2204
Eric Paris2875fa02011-04-28 16:04:24 -04002205 return dentry_has_perm(cred, dentry, FILE__QUOTAON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206}
2207
Eric Paris12b30522010-11-15 18:36:29 -05002208static int selinux_syslog(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 switch (type) {
Kees Cookd78ca3c2010-02-03 15:37:13 -08002211 case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */
2212 case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002213 return avc_has_perm(&selinux_state,
2214 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002215 SECCLASS_SYSTEM, SYSTEM__SYSLOG_READ, NULL);
Kees Cookd78ca3c2010-02-03 15:37:13 -08002216 case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */
2217 case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */
2218 /* Set level of messages printed to console */
2219 case SYSLOG_ACTION_CONSOLE_LEVEL:
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002220 return avc_has_perm(&selinux_state,
2221 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002222 SECCLASS_SYSTEM, SYSTEM__SYSLOG_CONSOLE,
2223 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 }
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002225 /* All other syslog types */
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_MOD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229}
2230
2231/*
2232 * Check that a process has enough memory to allocate a new virtual
2233 * mapping. 0 means there is enough memory for the allocation to
2234 * succeed and -ENOMEM implies there is not.
2235 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 * Do not audit the selinux permission check, as this is applied to all
2237 * processes that allocate mappings.
2238 */
Alan Cox34b4e4a2007-08-22 14:01:28 -07002239static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240{
2241 int rc, cap_sys_admin = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002243 rc = cred_has_capability(current_cred(), CAP_SYS_ADMIN,
Micah Mortonc1a85a02019-01-07 16:10:53 -08002244 CAP_OPT_NOAUDIT, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 if (rc == 0)
2246 cap_sys_admin = 1;
2247
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002248 return cap_sys_admin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249}
2250
2251/* binprm security operations */
2252
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002253static u32 ptrace_parent_sid(void)
Paul Moore0c6181c2016-03-30 21:41:21 -04002254{
2255 u32 sid = 0;
2256 struct task_struct *tracer;
2257
2258 rcu_read_lock();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002259 tracer = ptrace_parent(current);
Paul Moore0c6181c2016-03-30 21:41:21 -04002260 if (tracer)
2261 sid = task_sid(tracer);
2262 rcu_read_unlock();
2263
2264 return sid;
2265}
2266
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002267static int check_nnp_nosuid(const struct linux_binprm *bprm,
2268 const struct task_security_struct *old_tsec,
2269 const struct task_security_struct *new_tsec)
2270{
2271 int nnp = (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS);
Andy Lutomirski380cf5b2016-06-23 16:41:05 -05002272 int nosuid = !mnt_may_suid(bprm->file->f_path.mnt);
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002273 int rc;
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002274 u32 av;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002275
2276 if (!nnp && !nosuid)
2277 return 0; /* neither NNP nor nosuid */
2278
2279 if (new_tsec->sid == old_tsec->sid)
2280 return 0; /* No change in credentials */
2281
2282 /*
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002283 * If the policy enables the nnp_nosuid_transition policy capability,
2284 * then we permit transitions under NNP or nosuid if the
2285 * policy allows the corresponding permission between
2286 * the old and new contexts.
2287 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002288 if (selinux_policycap_nnp_nosuid_transition()) {
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002289 av = 0;
2290 if (nnp)
2291 av |= PROCESS2__NNP_TRANSITION;
2292 if (nosuid)
2293 av |= PROCESS2__NOSUID_TRANSITION;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002294 rc = avc_has_perm(&selinux_state,
2295 old_tsec->sid, new_tsec->sid,
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002296 SECCLASS_PROCESS2, av, NULL);
2297 if (!rc)
2298 return 0;
2299 }
2300
2301 /*
2302 * We also permit NNP or nosuid transitions to bounded SIDs,
2303 * i.e. SIDs that are guaranteed to only be allowed a subset
2304 * of the permissions of the current SID.
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002305 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002306 rc = security_bounded_transition(&selinux_state, old_tsec->sid,
2307 new_tsec->sid);
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002308 if (!rc)
2309 return 0;
2310
2311 /*
2312 * On failure, preserve the errno values for NNP vs nosuid.
2313 * NNP: Operation not permitted for caller.
2314 * nosuid: Permission denied to file.
2315 */
2316 if (nnp)
2317 return -EPERM;
2318 return -EACCES;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002319}
2320
David Howellsa6f76f22008-11-14 10:39:24 +11002321static int selinux_bprm_set_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322{
David Howellsa6f76f22008-11-14 10:39:24 +11002323 const struct task_security_struct *old_tsec;
2324 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 struct inode_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04002326 struct common_audit_data ad;
Al Viro496ad9a2013-01-23 17:07:38 -05002327 struct inode *inode = file_inode(bprm->file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 int rc;
2329
David Howellsa6f76f22008-11-14 10:39:24 +11002330 /* SELinux context only depends on initial program or script and not
2331 * the script interpreter */
Kees Cookddb4a142017-07-18 15:25:23 -07002332 if (bprm->called_set_creds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 return 0;
2334
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002335 old_tsec = selinux_cred(current_cred());
2336 new_tsec = selinux_cred(bprm->cred);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002337 isec = inode_security(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338
2339 /* Default to the current task SID. */
David Howellsa6f76f22008-11-14 10:39:24 +11002340 new_tsec->sid = old_tsec->sid;
2341 new_tsec->osid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
Michael LeMay28eba5b2006-06-27 02:53:42 -07002343 /* Reset fs, key, and sock SIDs on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002344 new_tsec->create_sid = 0;
2345 new_tsec->keycreate_sid = 0;
2346 new_tsec->sockcreate_sid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
David Howellsa6f76f22008-11-14 10:39:24 +11002348 if (old_tsec->exec_sid) {
2349 new_tsec->sid = old_tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 /* Reset exec SID on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002351 new_tsec->exec_sid = 0;
Andy Lutomirski259e5e62012-04-12 16:47:50 -05002352
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002353 /* Fail on NNP or nosuid if not an allowed transition. */
2354 rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
2355 if (rc)
2356 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 } else {
2358 /* Check for a default transition on this program. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002359 rc = security_transition_sid(&selinux_state, old_tsec->sid,
2360 isec->sid, SECCLASS_PROCESS, NULL,
Eric Paris652bb9b2011-02-01 11:05:40 -05002361 &new_tsec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 if (rc)
2363 return rc;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002364
2365 /*
2366 * Fallback to old SID on NNP or nosuid if not an allowed
2367 * transition.
2368 */
2369 rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
2370 if (rc)
2371 new_tsec->sid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 }
2373
Vivek Goyal43af5de2016-09-09 11:37:49 -04002374 ad.type = LSM_AUDIT_DATA_FILE;
2375 ad.u.file = bprm->file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
David Howellsa6f76f22008-11-14 10:39:24 +11002377 if (new_tsec->sid == old_tsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002378 rc = avc_has_perm(&selinux_state,
2379 old_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
2381 if (rc)
2382 return rc;
2383 } else {
2384 /* Check permissions for the transition. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002385 rc = avc_has_perm(&selinux_state,
2386 old_tsec->sid, new_tsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
2388 if (rc)
2389 return rc;
2390
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002391 rc = avc_has_perm(&selinux_state,
2392 new_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
2394 if (rc)
2395 return rc;
2396
David Howellsa6f76f22008-11-14 10:39:24 +11002397 /* Check for shared state */
2398 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002399 rc = avc_has_perm(&selinux_state,
2400 old_tsec->sid, new_tsec->sid,
David Howellsa6f76f22008-11-14 10:39:24 +11002401 SECCLASS_PROCESS, PROCESS__SHARE,
2402 NULL);
2403 if (rc)
2404 return -EPERM;
2405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
David Howellsa6f76f22008-11-14 10:39:24 +11002407 /* Make sure that anyone attempting to ptrace over a task that
2408 * changes its SID has the appropriate permit */
Eric W. Biederman9227dd22017-01-23 17:26:31 +13002409 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002410 u32 ptsid = ptrace_parent_sid();
David Howellsa6f76f22008-11-14 10:39:24 +11002411 if (ptsid != 0) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002412 rc = avc_has_perm(&selinux_state,
2413 ptsid, new_tsec->sid,
David Howellsa6f76f22008-11-14 10:39:24 +11002414 SECCLASS_PROCESS,
2415 PROCESS__PTRACE, NULL);
2416 if (rc)
2417 return -EPERM;
2418 }
2419 }
2420
2421 /* Clear any possibly unsafe personality bits on exec: */
2422 bprm->per_clear |= PER_CLEAR_ON_SETID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 /* Enable secure mode for SIDs transitions unless
2425 the noatsecure permission is granted between
2426 the two SIDs, i.e. ahp returns 0. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002427 rc = avc_has_perm(&selinux_state,
2428 old_tsec->sid, new_tsec->sid,
Kees Cook62874c32017-07-18 15:25:25 -07002429 SECCLASS_PROCESS, PROCESS__NOATSECURE,
2430 NULL);
2431 bprm->secureexec |= !!rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 }
2433
Kees Cook62874c32017-07-18 15:25:25 -07002434 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435}
2436
Al Viroc3c073f2012-08-21 22:32:06 -04002437static int match_file(const void *p, struct file *file, unsigned fd)
2438{
2439 return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0;
2440}
2441
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442/* Derived from fs/exec.c:flush_old_files. */
David Howells745ca242008-11-14 10:39:22 +11002443static inline void flush_unauthorized_files(const struct cred *cred,
2444 struct files_struct *files)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 struct file *file, *devnull = NULL;
Stephen Smalleyb20c8122006-09-25 23:32:03 -07002447 struct tty_struct *tty;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002448 int drop_tty = 0;
Al Viroc3c073f2012-08-21 22:32:06 -04002449 unsigned n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002451 tty = get_current_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 if (tty) {
Peter Hurley4a510962016-01-09 21:35:23 -08002453 spin_lock(&tty->files_lock);
Eric Paris37dd0bd2008-10-31 17:40:00 -04002454 if (!list_empty(&tty->tty_files)) {
Nick Piggind996b622010-08-18 04:37:36 +10002455 struct tty_file_private *file_priv;
Eric Paris37dd0bd2008-10-31 17:40:00 -04002456
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 /* Revalidate access to controlling tty.
David Howells13f8e982013-06-13 23:37:55 +01002458 Use file_path_has_perm on the tty path directly
2459 rather than using file_has_perm, as this particular
2460 open file may belong to another process and we are
2461 only interested in the inode-based check here. */
Nick Piggind996b622010-08-18 04:37:36 +10002462 file_priv = list_first_entry(&tty->tty_files,
2463 struct tty_file_private, list);
2464 file = file_priv->file;
David Howells13f8e982013-06-13 23:37:55 +01002465 if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE))
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002466 drop_tty = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 }
Peter Hurley4a510962016-01-09 21:35:23 -08002468 spin_unlock(&tty->files_lock);
Alan Cox452a00d2008-10-13 10:39:13 +01002469 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 }
Eric W. Biederman98a27ba2007-05-08 00:26:56 -07002471 /* Reset controlling tty. */
2472 if (drop_tty)
2473 no_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474
2475 /* Revalidate access to inherited open files. */
Al Viroc3c073f2012-08-21 22:32:06 -04002476 n = iterate_fd(files, 0, match_file, cred);
2477 if (!n) /* none found? */
2478 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479
Al Viroc3c073f2012-08-21 22:32:06 -04002480 devnull = dentry_open(&selinux_null, O_RDWR, cred);
Al Viro45525b22012-10-16 13:30:07 -04002481 if (IS_ERR(devnull))
2482 devnull = NULL;
2483 /* replace all the matching ones with this */
2484 do {
2485 replace_fd(n - 1, devnull, 0);
2486 } while ((n = iterate_fd(files, n, match_file, cred)) != 0);
2487 if (devnull)
Al Viroc3c073f2012-08-21 22:32:06 -04002488 fput(devnull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489}
2490
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491/*
David Howellsa6f76f22008-11-14 10:39:24 +11002492 * Prepare a process for imminent new credential changes due to exec
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 */
David Howellsa6f76f22008-11-14 10:39:24 +11002494static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495{
David Howellsa6f76f22008-11-14 10:39:24 +11002496 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 struct rlimit *rlim, *initrlim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 int rc, i;
2499
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002500 new_tsec = selinux_cred(bprm->cred);
David Howellsa6f76f22008-11-14 10:39:24 +11002501 if (new_tsec->sid == new_tsec->osid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 return;
2503
2504 /* Close files for which the new task SID is not authorized. */
David Howellsa6f76f22008-11-14 10:39:24 +11002505 flush_unauthorized_files(bprm->cred, current->files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506
David Howellsa6f76f22008-11-14 10:39:24 +11002507 /* Always clear parent death signal on SID transitions. */
2508 current->pdeath_signal = 0;
2509
2510 /* Check whether the new SID can inherit resource limits from the old
2511 * SID. If not, reset all soft limits to the lower of the current
2512 * task's hard limit and the init task's soft limit.
2513 *
2514 * Note that the setting of hard limits (even to lower them) can be
2515 * controlled by the setrlimit check. The inclusion of the init task's
2516 * soft limit into the computation is to avoid resetting soft limits
2517 * higher than the default soft limit for cases where the default is
2518 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2519 */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002520 rc = avc_has_perm(&selinux_state,
2521 new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
David Howellsa6f76f22008-11-14 10:39:24 +11002522 PROCESS__RLIMITINH, NULL);
2523 if (rc) {
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002524 /* protect against do_prlimit() */
2525 task_lock(current);
David Howellsa6f76f22008-11-14 10:39:24 +11002526 for (i = 0; i < RLIM_NLIMITS; i++) {
2527 rlim = current->signal->rlim + i;
2528 initrlim = init_task.signal->rlim + i;
2529 rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
2530 }
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002531 task_unlock(current);
Nicolas Pitrebaa73d92016-11-11 00:10:10 -05002532 if (IS_ENABLED(CONFIG_POSIX_TIMERS))
2533 update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
David Howellsa6f76f22008-11-14 10:39:24 +11002534 }
2535}
2536
2537/*
2538 * Clean up the process immediately after the installation of new credentials
2539 * due to exec
2540 */
2541static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
2542{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002543 const struct task_security_struct *tsec = selinux_cred(current_cred());
David Howellsa6f76f22008-11-14 10:39:24 +11002544 struct itimerval itimer;
David Howellsa6f76f22008-11-14 10:39:24 +11002545 u32 osid, sid;
2546 int rc, i;
David Howellsa6f76f22008-11-14 10:39:24 +11002547
David Howellsa6f76f22008-11-14 10:39:24 +11002548 osid = tsec->osid;
2549 sid = tsec->sid;
2550
2551 if (sid == osid)
2552 return;
2553
2554 /* Check whether the new SID can inherit signal state from the old SID.
2555 * If not, clear itimers to avoid subsequent signal generation and
2556 * flush and unblock signals.
2557 *
2558 * This must occur _after_ the task SID has been updated so that any
2559 * kill done after the flush will be checked against the new SID.
2560 */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002561 rc = avc_has_perm(&selinux_state,
2562 osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 if (rc) {
Nicolas Pitrebaa73d92016-11-11 00:10:10 -05002564 if (IS_ENABLED(CONFIG_POSIX_TIMERS)) {
2565 memset(&itimer, 0, sizeof itimer);
2566 for (i = 0; i < 3; i++)
2567 do_setitimer(i, &itimer, NULL);
2568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterov9e7c8f82015-06-04 16:22:16 -04002570 if (!fatal_signal_pending(current)) {
2571 flush_sigqueue(&current->pending);
2572 flush_sigqueue(&current->signal->shared_pending);
David Howells3bcac022009-04-29 13:45:05 +01002573 flush_signal_handlers(current, 1);
2574 sigemptyset(&current->blocked);
Oleg Nesterov9e7c8f82015-06-04 16:22:16 -04002575 recalc_sigpending();
David Howells3bcac022009-04-29 13:45:05 +01002576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 spin_unlock_irq(&current->sighand->siglock);
2578 }
2579
David Howellsa6f76f22008-11-14 10:39:24 +11002580 /* Wake up the parent if it is waiting so that it can recheck
2581 * wait permission to the new task SID. */
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002582 read_lock(&tasklist_lock);
Oleg Nesterov0b7570e2009-09-23 15:56:46 -07002583 __wake_up_parent(current, current->real_parent);
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002584 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585}
2586
2587/* superblock security operations */
2588
2589static int selinux_sb_alloc_security(struct super_block *sb)
2590{
2591 return superblock_alloc_security(sb);
2592}
2593
2594static void selinux_sb_free_security(struct super_block *sb)
2595{
2596 superblock_free_security(sb);
2597}
2598
Al Viro99dbbb52018-12-14 21:56:23 -05002599static inline int opt_len(const char *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600{
Al Viro99dbbb52018-12-14 21:56:23 -05002601 bool open_quote = false;
2602 int len;
2603 char c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604
Al Viro99dbbb52018-12-14 21:56:23 -05002605 for (len = 0; (c = s[len]) != '\0'; len++) {
2606 if (c == '"')
Cory Olmo3528a952006-09-29 01:58:44 -07002607 open_quote = !open_quote;
Al Viro99dbbb52018-12-14 21:56:23 -05002608 if (c == ',' && !open_quote)
2609 break;
2610 }
2611 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612}
2613
Al Viro204cc0c2018-12-13 13:41:47 -05002614static int selinux_sb_eat_lsm_opts(char *options, void **mnt_opts)
Eric Paris026eb162011-03-03 16:09:14 -05002615{
Al Viro99dbbb52018-12-14 21:56:23 -05002616 char *from = options;
2617 char *to = options;
2618 bool first = true;
Al Viro5b400232018-12-12 20:13:29 -05002619
Al Viro99dbbb52018-12-14 21:56:23 -05002620 while (1) {
2621 int len = opt_len(from);
2622 int token, rc;
2623 char *arg = NULL;
2624
2625 token = match_opt_prefix(from, len, &arg);
2626
2627 if (token != Opt_error) {
2628 char *p, *q;
2629
2630 /* strip quotes */
2631 if (arg) {
2632 for (p = q = arg; p < from + len; p++) {
2633 char c = *p;
2634 if (c != '"')
2635 *q++ = c;
2636 }
2637 arg = kmemdup_nul(arg, q - arg, GFP_KERNEL);
2638 }
2639 rc = selinux_add_opt(token, arg, mnt_opts);
2640 if (unlikely(rc)) {
2641 kfree(arg);
2642 if (*mnt_opts) {
2643 selinux_free_mnt_opts(*mnt_opts);
2644 *mnt_opts = NULL;
2645 }
2646 return rc;
2647 }
2648 } else {
2649 if (!first) { // copy with preceding comma
2650 from--;
2651 len++;
2652 }
2653 if (to != from)
2654 memmove(to, from, len);
2655 to += len;
2656 first = false;
2657 }
2658 if (!from[len])
2659 break;
2660 from += len + 1;
2661 }
2662 *to = '\0';
2663 return 0;
Al Viro5b400232018-12-12 20:13:29 -05002664}
2665
Al Viro204cc0c2018-12-13 13:41:47 -05002666static int selinux_sb_remount(struct super_block *sb, void *mnt_opts)
Eric Paris026eb162011-03-03 16:09:14 -05002667{
Al Virobd323652018-12-13 15:04:59 -05002668 struct selinux_mnt_opts *opts = mnt_opts;
Eric Paris026eb162011-03-03 16:09:14 -05002669 struct superblock_security_struct *sbsec = sb->s_security;
Al Virobd323652018-12-13 15:04:59 -05002670 u32 sid;
2671 int rc;
Eric Paris026eb162011-03-03 16:09:14 -05002672
2673 if (!(sbsec->flags & SE_SBINITIALIZED))
2674 return 0;
2675
Al Viro204cc0c2018-12-13 13:41:47 -05002676 if (!opts)
Eric Paris026eb162011-03-03 16:09:14 -05002677 return 0;
2678
Al Virobd323652018-12-13 15:04:59 -05002679 if (opts->fscontext) {
2680 rc = parse_sid(sb, opts->fscontext, &sid);
2681 if (rc)
Al Viroc039bc32018-12-01 23:06:57 -05002682 return rc;
Al Virobd323652018-12-13 15:04:59 -05002683 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
2684 goto out_bad_option;
Eric Paris026eb162011-03-03 16:09:14 -05002685 }
Al Virobd323652018-12-13 15:04:59 -05002686 if (opts->context) {
2687 rc = parse_sid(sb, opts->context, &sid);
2688 if (rc)
2689 return rc;
2690 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
2691 goto out_bad_option;
2692 }
2693 if (opts->rootcontext) {
2694 struct inode_security_struct *root_isec;
2695 root_isec = backing_inode_security(sb->s_root);
2696 rc = parse_sid(sb, opts->rootcontext, &sid);
2697 if (rc)
2698 return rc;
2699 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
2700 goto out_bad_option;
2701 }
2702 if (opts->defcontext) {
2703 rc = parse_sid(sb, opts->defcontext, &sid);
2704 if (rc)
2705 return rc;
2706 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
2707 goto out_bad_option;
Eric Paris026eb162011-03-03 16:09:14 -05002708 }
Al Viroc039bc32018-12-01 23:06:57 -05002709 return 0;
Eric Paris026eb162011-03-03 16:09:14 -05002710
Eric Paris026eb162011-03-03 16:09:14 -05002711out_bad_option:
peter enderborgc103a912018-06-12 10:09:03 +02002712 pr_warn("SELinux: unable to change security options "
Linus Torvalds29b1deb2013-12-15 11:17:45 -08002713 "during remount (dev %s, type=%s)\n", sb->s_id,
2714 sb->s_type->name);
Al Viroc039bc32018-12-01 23:06:57 -05002715 return -EINVAL;
Eric Paris026eb162011-03-03 16:09:14 -05002716}
2717
Al Viroa10d7c22018-12-05 11:58:35 -05002718static int selinux_sb_kern_mount(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719{
David Howells88e67f32008-11-14 10:39:21 +11002720 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002721 struct common_audit_data ad;
James Morris74192242008-12-19 11:41:10 +11002722
Eric Paris50c205f2012-04-04 15:01:43 -04002723 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002724 ad.u.dentry = sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002725 return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726}
2727
David Howells726c3342006-06-23 02:02:58 -07002728static int selinux_sb_statfs(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729{
David Howells88e67f32008-11-14 10:39:21 +11002730 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002731 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732
Eric Paris50c205f2012-04-04 15:01:43 -04002733 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002734 ad.u.dentry = dentry->d_sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002735 return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736}
2737
Al Viro808d4e32012-10-11 11:42:01 -04002738static int selinux_mount(const char *dev_name,
Al Viro8a04c432016-03-25 14:52:53 -04002739 const struct path *path,
Al Viro808d4e32012-10-11 11:42:01 -04002740 const char *type,
Eric Paris828dfe12008-04-17 13:17:49 -04002741 unsigned long flags,
2742 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743{
David Howells88e67f32008-11-14 10:39:21 +11002744 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745
2746 if (flags & MS_REMOUNT)
Al Virod8c95842011-12-07 18:16:57 -05002747 return superblock_has_perm(cred, path->dentry->d_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002748 FILESYSTEM__REMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 else
Eric Paris2875fa02011-04-28 16:04:24 -04002750 return path_has_perm(cred, path, FILE__MOUNTON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751}
2752
2753static int selinux_umount(struct vfsmount *mnt, int flags)
2754{
David Howells88e67f32008-11-14 10:39:21 +11002755 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756
David Howells88e67f32008-11-14 10:39:21 +11002757 return superblock_has_perm(cred, mnt->mnt_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002758 FILESYSTEM__UNMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759}
2760
Al Viro0b520752018-12-23 16:02:47 -05002761static int selinux_fs_context_dup(struct fs_context *fc,
2762 struct fs_context *src_fc)
2763{
2764 const struct selinux_mnt_opts *src = src_fc->security;
2765 struct selinux_mnt_opts *opts;
2766
2767 if (!src)
2768 return 0;
2769
2770 fc->security = kzalloc(sizeof(struct selinux_mnt_opts), GFP_KERNEL);
2771 if (!fc->security)
2772 return -ENOMEM;
2773
2774 opts = fc->security;
2775
2776 if (src->fscontext) {
2777 opts->fscontext = kstrdup(src->fscontext, GFP_KERNEL);
2778 if (!opts->fscontext)
2779 return -ENOMEM;
2780 }
2781 if (src->context) {
2782 opts->context = kstrdup(src->context, GFP_KERNEL);
2783 if (!opts->context)
2784 return -ENOMEM;
2785 }
2786 if (src->rootcontext) {
2787 opts->rootcontext = kstrdup(src->rootcontext, GFP_KERNEL);
2788 if (!opts->rootcontext)
2789 return -ENOMEM;
2790 }
2791 if (src->defcontext) {
2792 opts->defcontext = kstrdup(src->defcontext, GFP_KERNEL);
2793 if (!opts->defcontext)
2794 return -ENOMEM;
2795 }
2796 return 0;
2797}
2798
David Howells442155c2018-11-01 23:07:24 +00002799static const struct fs_parameter_spec selinux_param_specs[] = {
2800 fsparam_string(CONTEXT_STR, Opt_context),
2801 fsparam_string(DEFCONTEXT_STR, Opt_defcontext),
2802 fsparam_string(FSCONTEXT_STR, Opt_fscontext),
2803 fsparam_string(ROOTCONTEXT_STR, Opt_rootcontext),
2804 fsparam_flag (SECLABEL_STR, Opt_seclabel),
2805 {}
2806};
2807
2808static const struct fs_parameter_description selinux_fs_parameters = {
2809 .name = "SELinux",
2810 .specs = selinux_param_specs,
2811};
2812
2813static int selinux_fs_context_parse_param(struct fs_context *fc,
2814 struct fs_parameter *param)
2815{
2816 struct fs_parse_result result;
2817 int opt, rc;
2818
2819 opt = fs_parse(fc, &selinux_fs_parameters, param, &result);
2820 if (opt < 0)
2821 return opt;
2822
2823 rc = selinux_add_opt(opt, param->string, &fc->security);
2824 if (!rc) {
2825 param->string = NULL;
2826 rc = 1;
2827 }
2828 return rc;
2829}
2830
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831/* inode security operations */
2832
2833static int selinux_inode_alloc_security(struct inode *inode)
2834{
2835 return inode_alloc_security(inode);
2836}
2837
2838static void selinux_inode_free_security(struct inode *inode)
2839{
2840 inode_free_security(inode);
2841}
2842
David Quigleyd47be3d2013-05-22 12:50:34 -04002843static int selinux_dentry_init_security(struct dentry *dentry, int mode,
Al Viro4f3ccd72016-07-20 16:06:15 -04002844 const struct qstr *name, void **ctx,
David Quigleyd47be3d2013-05-22 12:50:34 -04002845 u32 *ctxlen)
2846{
David Quigleyd47be3d2013-05-22 12:50:34 -04002847 u32 newsid;
2848 int rc;
2849
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002850 rc = selinux_determine_inode_label(selinux_cred(current_cred()),
Vivek Goyalc957f6d2016-07-13 10:44:51 -04002851 d_inode(dentry->d_parent), name,
David Howellsc3c188b2015-07-10 17:19:58 -04002852 inode_mode_to_security_class(mode),
2853 &newsid);
2854 if (rc)
2855 return rc;
David Quigleyd47be3d2013-05-22 12:50:34 -04002856
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002857 return security_sid_to_context(&selinux_state, newsid, (char **)ctx,
2858 ctxlen);
David Quigleyd47be3d2013-05-22 12:50:34 -04002859}
2860
Vivek Goyala518b0a2016-07-13 10:44:53 -04002861static int selinux_dentry_create_files_as(struct dentry *dentry, int mode,
2862 struct qstr *name,
2863 const struct cred *old,
2864 struct cred *new)
2865{
2866 u32 newsid;
2867 int rc;
2868 struct task_security_struct *tsec;
2869
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002870 rc = selinux_determine_inode_label(selinux_cred(old),
Vivek Goyala518b0a2016-07-13 10:44:53 -04002871 d_inode(dentry->d_parent), name,
2872 inode_mode_to_security_class(mode),
2873 &newsid);
2874 if (rc)
2875 return rc;
2876
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002877 tsec = selinux_cred(new);
Vivek Goyala518b0a2016-07-13 10:44:53 -04002878 tsec->create_sid = newsid;
2879 return 0;
2880}
2881
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002882static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +09002883 const struct qstr *qstr,
2884 const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -05002885 void **value, size_t *len)
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002886{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002887 const struct task_security_struct *tsec = selinux_cred(current_cred());
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002888 struct superblock_security_struct *sbsec;
Corentin LABBEc0d4f462017-10-04 20:32:17 +02002889 u32 newsid, clen;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002890 int rc;
Tetsuo Handa95489062013-07-25 05:44:02 +09002891 char *context;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002892
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002893 sbsec = dir->i_sb->s_security;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002894
David Howells275bb412008-11-14 10:39:19 +11002895 newsid = tsec->create_sid;
2896
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002897 rc = selinux_determine_inode_label(selinux_cred(current_cred()),
David Howellsc3c188b2015-07-10 17:19:58 -04002898 dir, qstr,
2899 inode_mode_to_security_class(inode->i_mode),
2900 &newsid);
2901 if (rc)
2902 return rc;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002903
Eric Paris296fddf2006-09-25 23:32:00 -07002904 /* Possibly defer initialization to selinux_complete_init. */
David P. Quigley0d90a7e2009-01-16 09:22:02 -05002905 if (sbsec->flags & SE_SBINITIALIZED) {
Casey Schaufler80788c22018-09-21 17:19:11 -07002906 struct inode_security_struct *isec = selinux_inode(inode);
Eric Paris296fddf2006-09-25 23:32:00 -07002907 isec->sclass = inode_mode_to_security_class(inode->i_mode);
2908 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05002909 isec->initialized = LABEL_INITIALIZED;
Eric Paris296fddf2006-09-25 23:32:00 -07002910 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002911
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002912 if (!selinux_state.initialized || !(sbsec->flags & SBLABEL_MNT))
Stephen Smalley25a74f32005-11-08 21:34:33 -08002913 return -EOPNOTSUPP;
2914
Tetsuo Handa95489062013-07-25 05:44:02 +09002915 if (name)
2916 *name = XATTR_SELINUX_SUFFIX;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002917
2918 if (value && len) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002919 rc = security_sid_to_context_force(&selinux_state, newsid,
2920 &context, &clen);
Tetsuo Handa95489062013-07-25 05:44:02 +09002921 if (rc)
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002922 return rc;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002923 *value = context;
2924 *len = clen;
2925 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002926
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002927 return 0;
2928}
2929
Al Viro4acdaf22011-07-26 01:42:34 -04002930static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931{
2932 return may_create(dir, dentry, SECCLASS_FILE);
2933}
2934
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2936{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 return may_link(dir, old_dentry, MAY_LINK);
2938}
2939
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
2941{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 return may_link(dir, dentry, MAY_UNLINK);
2943}
2944
2945static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
2946{
2947 return may_create(dir, dentry, SECCLASS_LNK_FILE);
2948}
2949
Al Viro18bb1db2011-07-26 01:41:39 -04002950static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951{
2952 return may_create(dir, dentry, SECCLASS_DIR);
2953}
2954
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
2956{
2957 return may_link(dir, dentry, MAY_RMDIR);
2958}
2959
Al Viro1a67aaf2011-07-26 01:52:52 -04002960static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 return may_create(dir, dentry, inode_mode_to_security_class(mode));
2963}
2964
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
Eric Paris828dfe12008-04-17 13:17:49 -04002966 struct inode *new_inode, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967{
2968 return may_rename(old_inode, old_dentry, new_inode, new_dentry);
2969}
2970
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971static int selinux_inode_readlink(struct dentry *dentry)
2972{
David Howells88e67f32008-11-14 10:39:21 +11002973 const struct cred *cred = current_cred();
2974
Eric Paris2875fa02011-04-28 16:04:24 -04002975 return dentry_has_perm(cred, dentry, FILE__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976}
2977
NeilBrownbda0be72015-03-23 13:37:39 +11002978static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode,
2979 bool rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980{
David Howells88e67f32008-11-14 10:39:21 +11002981 const struct cred *cred = current_cred();
NeilBrownbda0be72015-03-23 13:37:39 +11002982 struct common_audit_data ad;
2983 struct inode_security_struct *isec;
2984 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985
NeilBrownbda0be72015-03-23 13:37:39 +11002986 validate_creds(cred);
2987
2988 ad.type = LSM_AUDIT_DATA_DENTRY;
2989 ad.u.dentry = dentry;
2990 sid = cred_sid(cred);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05002991 isec = inode_security_rcu(inode, rcu);
2992 if (IS_ERR(isec))
2993 return PTR_ERR(isec);
NeilBrownbda0be72015-03-23 13:37:39 +11002994
Stephen Smalleye46e01e2018-12-12 10:10:56 -05002995 return avc_has_perm(&selinux_state,
2996 sid, isec->sid, isec->sclass, FILE__READ, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997}
2998
Eric Parisd4cf970d2012-04-04 15:01:42 -04002999static noinline int audit_inode_permission(struct inode *inode,
3000 u32 perms, u32 audited, u32 denied,
Stephen Smalley626b9742014-04-29 11:29:04 -07003001 int result,
Eric Parisd4cf970d2012-04-04 15:01:42 -04003002 unsigned flags)
3003{
3004 struct common_audit_data ad;
Casey Schaufler80788c22018-09-21 17:19:11 -07003005 struct inode_security_struct *isec = selinux_inode(inode);
Eric Parisd4cf970d2012-04-04 15:01:42 -04003006 int rc;
3007
Eric Paris50c205f2012-04-04 15:01:43 -04003008 ad.type = LSM_AUDIT_DATA_INODE;
Eric Parisd4cf970d2012-04-04 15:01:42 -04003009 ad.u.inode = inode;
3010
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003011 rc = slow_avc_audit(&selinux_state,
3012 current_sid(), isec->sid, isec->sclass, perms,
Stephen Smalley626b9742014-04-29 11:29:04 -07003013 audited, denied, result, &ad, flags);
Eric Parisd4cf970d2012-04-04 15:01:42 -04003014 if (rc)
3015 return rc;
3016 return 0;
3017}
3018
Al Viroe74f71e2011-06-20 19:38:15 -04003019static int selinux_inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020{
David Howells88e67f32008-11-14 10:39:21 +11003021 const struct cred *cred = current_cred();
Eric Parisb782e0a2010-07-23 11:44:03 -04003022 u32 perms;
3023 bool from_access;
Al Virocf1dd1d2011-06-20 19:44:08 -04003024 unsigned flags = mask & MAY_NOT_BLOCK;
Eric Paris2e334052012-04-04 15:01:42 -04003025 struct inode_security_struct *isec;
3026 u32 sid;
3027 struct av_decision avd;
3028 int rc, rc2;
3029 u32 audited, denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030
Eric Parisb782e0a2010-07-23 11:44:03 -04003031 from_access = mask & MAY_ACCESS;
Eric Parisd09ca732010-07-23 11:43:57 -04003032 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
3033
Eric Parisb782e0a2010-07-23 11:44:03 -04003034 /* No permission to check. Existence test. */
3035 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037
Eric Paris2e334052012-04-04 15:01:42 -04003038 validate_creds(cred);
Eric Parisb782e0a2010-07-23 11:44:03 -04003039
Eric Paris2e334052012-04-04 15:01:42 -04003040 if (unlikely(IS_PRIVATE(inode)))
3041 return 0;
Eric Parisb782e0a2010-07-23 11:44:03 -04003042
3043 perms = file_mask_to_av(inode->i_mode, mask);
3044
Eric Paris2e334052012-04-04 15:01:42 -04003045 sid = cred_sid(cred);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05003046 isec = inode_security_rcu(inode, flags & MAY_NOT_BLOCK);
3047 if (IS_ERR(isec))
3048 return PTR_ERR(isec);
Eric Paris2e334052012-04-04 15:01:42 -04003049
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003050 rc = avc_has_perm_noaudit(&selinux_state,
Stephen Smalley3a28cff2018-12-12 10:10:55 -05003051 sid, isec->sid, isec->sclass, perms,
3052 (flags & MAY_NOT_BLOCK) ? AVC_NONBLOCKING : 0,
3053 &avd);
Eric Paris2e334052012-04-04 15:01:42 -04003054 audited = avc_audit_required(perms, &avd, rc,
3055 from_access ? FILE__AUDIT_ACCESS : 0,
3056 &denied);
3057 if (likely(!audited))
3058 return rc;
3059
Stephen Smalley626b9742014-04-29 11:29:04 -07003060 rc2 = audit_inode_permission(inode, perms, audited, denied, rc, flags);
Eric Paris2e334052012-04-04 15:01:42 -04003061 if (rc2)
3062 return rc2;
3063 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064}
3065
3066static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
3067{
David Howells88e67f32008-11-14 10:39:21 +11003068 const struct cred *cred = current_cred();
Stephen Smalleyccb54472017-05-12 12:41:24 -04003069 struct inode *inode = d_backing_inode(dentry);
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003070 unsigned int ia_valid = iattr->ia_valid;
Eric Paris95dbf732012-04-04 13:45:34 -04003071 __u32 av = FILE__WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003073 /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
3074 if (ia_valid & ATTR_FORCE) {
3075 ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
3076 ATTR_FORCE);
3077 if (!ia_valid)
3078 return 0;
3079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003081 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
3082 ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
Eric Paris2875fa02011-04-28 16:04:24 -04003083 return dentry_has_perm(cred, dentry, FILE__SETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003085 if (selinux_policycap_openperm() &&
Stephen Smalleyccb54472017-05-12 12:41:24 -04003086 inode->i_sb->s_magic != SOCKFS_MAGIC &&
3087 (ia_valid & ATTR_SIZE) &&
3088 !(ia_valid & ATTR_FILE))
Eric Paris95dbf732012-04-04 13:45:34 -04003089 av |= FILE__OPEN;
3090
3091 return dentry_has_perm(cred, dentry, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092}
3093
Al Viro3f7036a2015-03-08 19:28:30 -04003094static int selinux_inode_getattr(const struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095{
Al Viro3f7036a2015-03-08 19:28:30 -04003096 return path_has_perm(current_cred(), path, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097}
3098
Stephen Smalleydb590002017-04-20 11:31:30 -04003099static bool has_cap_mac_admin(bool audit)
3100{
3101 const struct cred *cred = current_cred();
Micah Mortonc1a85a02019-01-07 16:10:53 -08003102 unsigned int opts = audit ? CAP_OPT_NONE : CAP_OPT_NOAUDIT;
Stephen Smalleydb590002017-04-20 11:31:30 -04003103
Micah Mortonc1a85a02019-01-07 16:10:53 -08003104 if (cap_capable(cred, &init_user_ns, CAP_MAC_ADMIN, opts))
Stephen Smalleydb590002017-04-20 11:31:30 -04003105 return false;
Micah Mortonc1a85a02019-01-07 16:10:53 -08003106 if (cred_has_capability(cred, CAP_MAC_ADMIN, opts, true))
Stephen Smalleydb590002017-04-20 11:31:30 -04003107 return false;
3108 return true;
3109}
3110
David Howells8f0cfa52008-04-29 00:59:41 -07003111static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
3112 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113{
David Howellsc6f493d2015-03-17 22:26:22 +00003114 struct inode *inode = d_backing_inode(dentry);
Paul Moore20cdef82016-04-04 14:14:42 -04003115 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 struct superblock_security_struct *sbsec;
Thomas Liu2bf49692009-07-14 12:14:09 -04003117 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11003118 u32 newsid, sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 int rc = 0;
3120
Eric W. Biederman6b240302017-10-02 09:38:20 -05003121 if (strcmp(name, XATTR_NAME_SELINUX)) {
3122 rc = cap_inode_setxattr(dentry, name, value, size, flags);
3123 if (rc)
3124 return rc;
3125
3126 /* Not an attribute we recognize, so just check the
3127 ordinary setattr permission. */
3128 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130
3131 sbsec = inode->i_sb->s_security;
Eric Paris12f348b2012-10-09 10:56:25 -04003132 if (!(sbsec->flags & SBLABEL_MNT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 return -EOPNOTSUPP;
3134
Serge E. Hallyn2e149672011-03-23 16:43:26 -07003135 if (!inode_owner_or_capable(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 return -EPERM;
3137
Eric Paris50c205f2012-04-04 15:01:43 -04003138 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04003139 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140
Paul Moore20cdef82016-04-04 14:14:42 -04003141 isec = backing_inode_security(dentry);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003142 rc = avc_has_perm(&selinux_state,
3143 sid, isec->sid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 FILE__RELABELFROM, &ad);
3145 if (rc)
3146 return rc;
3147
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003148 rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3149 GFP_KERNEL);
Stephen Smalley12b29f32008-05-07 13:03:20 -04003150 if (rc == -EINVAL) {
Stephen Smalleydb590002017-04-20 11:31:30 -04003151 if (!has_cap_mac_admin(true)) {
Eric Parisd6ea83e2012-04-04 13:45:49 -04003152 struct audit_buffer *ab;
3153 size_t audit_size;
Eric Parisd6ea83e2012-04-04 13:45:49 -04003154
3155 /* We strip a nul only if it is at the end, otherwise the
3156 * context contains a nul and we should audit that */
Al Viroe3fea3f2012-06-09 08:15:16 +01003157 if (value) {
Colin Ian Kingadd24372017-10-14 13:46:55 +01003158 const char *str = value;
3159
Al Viroe3fea3f2012-06-09 08:15:16 +01003160 if (str[size - 1] == '\0')
3161 audit_size = size - 1;
3162 else
3163 audit_size = size;
3164 } else {
Al Viroe3fea3f2012-06-09 08:15:16 +01003165 audit_size = 0;
3166 }
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04003167 ab = audit_log_start(audit_context(),
3168 GFP_ATOMIC, AUDIT_SELINUX_ERR);
Eric Parisd6ea83e2012-04-04 13:45:49 -04003169 audit_log_format(ab, "op=setxattr invalid_context=");
3170 audit_log_n_untrustedstring(ab, value, audit_size);
3171 audit_log_end(ab);
3172
Stephen Smalley12b29f32008-05-07 13:03:20 -04003173 return rc;
Eric Parisd6ea83e2012-04-04 13:45:49 -04003174 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003175 rc = security_context_to_sid_force(&selinux_state, value,
3176 size, &newsid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04003177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 if (rc)
3179 return rc;
3180
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003181 rc = avc_has_perm(&selinux_state,
3182 sid, newsid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 FILE__RELABELTO, &ad);
3184 if (rc)
3185 return rc;
3186
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003187 rc = security_validate_transition(&selinux_state, isec->sid, newsid,
3188 sid, isec->sclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 if (rc)
3190 return rc;
3191
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003192 return avc_has_perm(&selinux_state,
3193 newsid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 sbsec->sid,
3195 SECCLASS_FILESYSTEM,
3196 FILESYSTEM__ASSOCIATE,
3197 &ad);
3198}
3199
David Howells8f0cfa52008-04-29 00:59:41 -07003200static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
Eric Parisf5269712008-05-14 11:27:45 -04003201 const void *value, size_t size,
David Howells8f0cfa52008-04-29 00:59:41 -07003202 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203{
David Howellsc6f493d2015-03-17 22:26:22 +00003204 struct inode *inode = d_backing_inode(dentry);
Paul Moore20cdef82016-04-04 14:14:42 -04003205 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 u32 newsid;
3207 int rc;
3208
3209 if (strcmp(name, XATTR_NAME_SELINUX)) {
3210 /* Not an attribute we recognize, so nothing to do. */
3211 return;
3212 }
3213
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003214 rc = security_context_to_sid_force(&selinux_state, value, size,
3215 &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 if (rc) {
peter enderborgc103a912018-06-12 10:09:03 +02003217 pr_err("SELinux: unable to map context to SID"
Stephen Smalley12b29f32008-05-07 13:03:20 -04003218 "for (%s, %lu), rc=%d\n",
3219 inode->i_sb->s_id, inode->i_ino, -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 return;
3221 }
3222
Paul Moore20cdef82016-04-04 14:14:42 -04003223 isec = backing_inode_security(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003224 spin_lock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003225 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05003227 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003228 spin_unlock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003229
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 return;
3231}
3232
David Howells8f0cfa52008-04-29 00:59:41 -07003233static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234{
David Howells88e67f32008-11-14 10:39:21 +11003235 const struct cred *cred = current_cred();
3236
Eric Paris2875fa02011-04-28 16:04:24 -04003237 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238}
3239
Eric Paris828dfe12008-04-17 13:17:49 -04003240static int selinux_inode_listxattr(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241{
David Howells88e67f32008-11-14 10:39:21 +11003242 const struct cred *cred = current_cred();
3243
Eric Paris2875fa02011-04-28 16:04:24 -04003244 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245}
3246
David Howells8f0cfa52008-04-29 00:59:41 -07003247static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248{
Eric W. Biederman6b240302017-10-02 09:38:20 -05003249 if (strcmp(name, XATTR_NAME_SELINUX)) {
3250 int rc = cap_inode_removexattr(dentry, name);
3251 if (rc)
3252 return rc;
3253
3254 /* Not an attribute we recognize, so just check the
3255 ordinary setattr permission. */
3256 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258
3259 /* No one is allowed to remove a SELinux security label.
3260 You can change the label, but all data must be labeled. */
3261 return -EACCES;
3262}
3263
James Morrisd381d8a2005-10-30 14:59:22 -08003264/*
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003265 * Copy the inode security context value to the user.
James Morrisd381d8a2005-10-30 14:59:22 -08003266 *
3267 * Permission check is handled by selinux_inode_getxattr hook.
3268 */
Andreas Gruenbacherea861df2015-12-24 11:09:39 -05003269static int selinux_inode_getsecurity(struct inode *inode, const char *name, void **buffer, bool alloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270{
David P. Quigley42492592008-02-04 22:29:39 -08003271 u32 size;
3272 int error;
3273 char *context = NULL;
Paul Moore20cdef82016-04-04 14:14:42 -04003274 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00003276 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3277 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003279 /*
3280 * If the caller has CAP_MAC_ADMIN, then get the raw context
3281 * value even if it is not defined by current policy; otherwise,
3282 * use the in-core value under current policy.
3283 * Use the non-auditing forms of the permission checks since
3284 * getxattr may be called by unprivileged processes commonly
3285 * and lack of permission just means that we fall back to the
3286 * in-core context value, not a denial.
3287 */
Paul Moore20cdef82016-04-04 14:14:42 -04003288 isec = inode_security(inode);
Stephen Smalleydb590002017-04-20 11:31:30 -04003289 if (has_cap_mac_admin(false))
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003290 error = security_sid_to_context_force(&selinux_state,
3291 isec->sid, &context,
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003292 &size);
3293 else
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003294 error = security_sid_to_context(&selinux_state, isec->sid,
3295 &context, &size);
David P. Quigley42492592008-02-04 22:29:39 -08003296 if (error)
3297 return error;
3298 error = size;
3299 if (alloc) {
3300 *buffer = context;
3301 goto out_nofree;
3302 }
3303 kfree(context);
3304out_nofree:
3305 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306}
3307
3308static int selinux_inode_setsecurity(struct inode *inode, const char *name,
Eric Paris828dfe12008-04-17 13:17:49 -04003309 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310{
Paul Moore2c971652016-04-19 16:36:28 -04003311 struct inode_security_struct *isec = inode_security_novalidate(inode);
Ondrej Mosnacek53e0c2a2018-12-21 21:18:53 +01003312 struct superblock_security_struct *sbsec = inode->i_sb->s_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 u32 newsid;
3314 int rc;
3315
3316 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3317 return -EOPNOTSUPP;
3318
Ondrej Mosnacek53e0c2a2018-12-21 21:18:53 +01003319 if (!(sbsec->flags & SBLABEL_MNT))
3320 return -EOPNOTSUPP;
3321
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 if (!value || !size)
3323 return -EACCES;
3324
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003325 rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3326 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 if (rc)
3328 return rc;
3329
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003330 spin_lock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003331 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05003333 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003334 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 return 0;
3336}
3337
3338static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
3339{
3340 const int len = sizeof(XATTR_NAME_SELINUX);
3341 if (buffer && len <= buffer_size)
3342 memcpy(buffer, XATTR_NAME_SELINUX, len);
3343 return len;
3344}
3345
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05003346static void selinux_inode_getsecid(struct inode *inode, u32 *secid)
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02003347{
Andreas Gruenbachere817c2f2016-02-18 12:04:08 +01003348 struct inode_security_struct *isec = inode_security_novalidate(inode);
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02003349 *secid = isec->sid;
3350}
3351
Vivek Goyal56909eb2016-07-13 10:44:48 -04003352static int selinux_inode_copy_up(struct dentry *src, struct cred **new)
3353{
3354 u32 sid;
3355 struct task_security_struct *tsec;
3356 struct cred *new_creds = *new;
3357
3358 if (new_creds == NULL) {
3359 new_creds = prepare_creds();
3360 if (!new_creds)
3361 return -ENOMEM;
3362 }
3363
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07003364 tsec = selinux_cred(new_creds);
Vivek Goyal56909eb2016-07-13 10:44:48 -04003365 /* Get label from overlay inode and set it in create_sid */
3366 selinux_inode_getsecid(d_inode(src), &sid);
3367 tsec->create_sid = sid;
3368 *new = new_creds;
3369 return 0;
3370}
3371
Vivek Goyal19472b62016-07-13 10:44:50 -04003372static int selinux_inode_copy_up_xattr(const char *name)
3373{
3374 /* The copy_up hook above sets the initial context on an inode, but we
3375 * don't then want to overwrite it by blindly copying all the lower
3376 * xattrs up. Instead, we have to filter out SELinux-related xattrs.
3377 */
3378 if (strcmp(name, XATTR_NAME_SELINUX) == 0)
3379 return 1; /* Discard */
3380 /*
3381 * Any other attribute apart from SELINUX is not claimed, supported
3382 * by selinux.
3383 */
3384 return -EOPNOTSUPP;
3385}
3386
Ondrej Mosnacekec882da2019-02-22 15:57:17 +01003387/* kernfs node operations */
3388
YueHaibingc72c4cd2019-03-22 22:04:00 +08003389static int selinux_kernfs_init_security(struct kernfs_node *kn_dir,
3390 struct kernfs_node *kn)
Ondrej Mosnacekec882da2019-02-22 15:57:17 +01003391{
3392 const struct task_security_struct *tsec = current_security();
3393 u32 parent_sid, newsid, clen;
3394 int rc;
3395 char *context;
3396
3397 rc = kernfs_security_xattr_get(kn_dir, XATTR_SELINUX_SUFFIX, NULL, 0);
3398 if (rc == -ENODATA)
3399 return 0;
3400 else if (rc < 0)
3401 return rc;
3402
3403 clen = (u32)rc;
3404 context = kmalloc(clen, GFP_KERNEL);
3405 if (!context)
3406 return -ENOMEM;
3407
3408 rc = kernfs_security_xattr_get(kn_dir, XATTR_SELINUX_SUFFIX, context,
3409 clen);
3410 if (rc < 0) {
3411 kfree(context);
3412 return rc;
3413 }
3414
3415 rc = security_context_to_sid(&selinux_state, context, clen, &parent_sid,
3416 GFP_KERNEL);
3417 kfree(context);
3418 if (rc)
3419 return rc;
3420
3421 if (tsec->create_sid) {
3422 newsid = tsec->create_sid;
3423 } else {
3424 u16 secclass = inode_mode_to_security_class(kn->mode);
3425 struct qstr q;
3426
3427 q.name = kn->name;
3428 q.hash_len = hashlen_string(kn_dir, kn->name);
3429
3430 rc = security_transition_sid(&selinux_state, tsec->sid,
3431 parent_sid, secclass, &q,
3432 &newsid);
3433 if (rc)
3434 return rc;
3435 }
3436
3437 rc = security_sid_to_context_force(&selinux_state, newsid,
3438 &context, &clen);
3439 if (rc)
3440 return rc;
3441
3442 rc = kernfs_security_xattr_set(kn, XATTR_SELINUX_SUFFIX, context, clen,
3443 XATTR_CREATE);
3444 kfree(context);
3445 return rc;
3446}
3447
3448
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449/* file security operations */
3450
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003451static int selinux_revalidate_file_permission(struct file *file, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452{
David Howells88e67f32008-11-14 10:39:21 +11003453 const struct cred *cred = current_cred();
Al Viro496ad9a2013-01-23 17:07:38 -05003454 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
3457 if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
3458 mask |= MAY_APPEND;
3459
Paul Moore389fb8002009-03-27 17:10:34 -04003460 return file_has_perm(cred, file,
3461 file_mask_to_av(inode->i_mode, mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462}
3463
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003464static int selinux_file_permission(struct file *file, int mask)
3465{
Al Viro496ad9a2013-01-23 17:07:38 -05003466 struct inode *inode = file_inode(file);
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07003467 struct file_security_struct *fsec = selinux_file(file);
Andreas Gruenbacherb1973672016-01-05 23:12:33 +01003468 struct inode_security_struct *isec;
Stephen Smalley20dda182009-06-22 14:54:53 -04003469 u32 sid = current_sid();
3470
Paul Moore389fb8002009-03-27 17:10:34 -04003471 if (!mask)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003472 /* No permission to check. Existence test. */
3473 return 0;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003474
Andreas Gruenbacherb1973672016-01-05 23:12:33 +01003475 isec = inode_security(inode);
Stephen Smalley20dda182009-06-22 14:54:53 -04003476 if (sid == fsec->sid && fsec->isid == isec->sid &&
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003477 fsec->pseqno == avc_policy_seqno(&selinux_state))
Eric Paris83d49852012-04-04 13:45:40 -04003478 /* No change since file_open check. */
Stephen Smalley20dda182009-06-22 14:54:53 -04003479 return 0;
3480
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003481 return selinux_revalidate_file_permission(file, mask);
3482}
3483
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484static int selinux_file_alloc_security(struct file *file)
3485{
3486 return file_alloc_security(file);
3487}
3488
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003489/*
3490 * Check whether a task has the ioctl permission and cmd
3491 * operation to an inode.
3492 */
Geliang Tang1d2a1682015-10-21 17:44:27 -04003493static int ioctl_has_perm(const struct cred *cred, struct file *file,
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003494 u32 requested, u16 cmd)
3495{
3496 struct common_audit_data ad;
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07003497 struct file_security_struct *fsec = selinux_file(file);
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003498 struct inode *inode = file_inode(file);
Paul Moore20cdef82016-04-04 14:14:42 -04003499 struct inode_security_struct *isec;
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003500 struct lsm_ioctlop_audit ioctl;
3501 u32 ssid = cred_sid(cred);
3502 int rc;
3503 u8 driver = cmd >> 8;
3504 u8 xperm = cmd & 0xff;
3505
3506 ad.type = LSM_AUDIT_DATA_IOCTL_OP;
3507 ad.u.op = &ioctl;
3508 ad.u.op->cmd = cmd;
3509 ad.u.op->path = file->f_path;
3510
3511 if (ssid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003512 rc = avc_has_perm(&selinux_state,
3513 ssid, fsec->sid,
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003514 SECCLASS_FD,
3515 FD__USE,
3516 &ad);
3517 if (rc)
3518 goto out;
3519 }
3520
3521 if (unlikely(IS_PRIVATE(inode)))
3522 return 0;
3523
Paul Moore20cdef82016-04-04 14:14:42 -04003524 isec = inode_security(inode);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003525 rc = avc_has_extended_perms(&selinux_state,
3526 ssid, isec->sid, isec->sclass,
3527 requested, driver, xperm, &ad);
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003528out:
3529 return rc;
3530}
3531
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532static int selinux_file_ioctl(struct file *file, unsigned int cmd,
3533 unsigned long arg)
3534{
David Howells88e67f32008-11-14 10:39:21 +11003535 const struct cred *cred = current_cred();
Eric Paris0b24dcb2011-02-25 15:39:20 -05003536 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537
Eric Paris0b24dcb2011-02-25 15:39:20 -05003538 switch (cmd) {
3539 case FIONREAD:
3540 /* fall through */
3541 case FIBMAP:
3542 /* fall through */
3543 case FIGETBSZ:
3544 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003545 case FS_IOC_GETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003546 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003547 case FS_IOC_GETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003548 error = file_has_perm(cred, file, FILE__GETATTR);
3549 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550
Al Viro2f99c362012-03-23 16:04:05 -04003551 case FS_IOC_SETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003552 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003553 case FS_IOC_SETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003554 error = file_has_perm(cred, file, FILE__SETATTR);
3555 break;
3556
3557 /* sys_ioctl() checks */
3558 case FIONBIO:
3559 /* fall through */
3560 case FIOASYNC:
3561 error = file_has_perm(cred, file, 0);
3562 break;
3563
3564 case KDSKBENT:
3565 case KDSKBSENT:
Eric Paris6a9de492012-01-03 12:25:14 -05003566 error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
Micah Mortonc1a85a02019-01-07 16:10:53 -08003567 CAP_OPT_NONE, true);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003568 break;
3569
3570 /* default case assumes that the command will go
3571 * to the file's ioctl() function.
3572 */
3573 default:
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003574 error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003575 }
3576 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577}
3578
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003579static int default_noexec;
3580
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
3582{
David Howells88e67f32008-11-14 10:39:21 +11003583 const struct cred *cred = current_cred();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003584 u32 sid = cred_sid(cred);
David Howellsd84f4f92008-11-14 10:39:23 +11003585 int rc = 0;
David Howells88e67f32008-11-14 10:39:21 +11003586
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003587 if (default_noexec &&
Stephen Smalley892e8ca2015-07-10 09:40:59 -04003588 (prot & PROT_EXEC) && (!file || IS_PRIVATE(file_inode(file)) ||
3589 (!shared && (prot & PROT_WRITE)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 /*
3591 * We are making executable an anonymous mapping or a
3592 * private file mapping that will also be writable.
3593 * This has an additional check.
3594 */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003595 rc = avc_has_perm(&selinux_state,
3596 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003597 PROCESS__EXECMEM, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 if (rc)
David Howellsd84f4f92008-11-14 10:39:23 +11003599 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601
3602 if (file) {
3603 /* read access is always possible with a mapping */
3604 u32 av = FILE__READ;
3605
3606 /* write access only matters if the mapping is shared */
3607 if (shared && (prot & PROT_WRITE))
3608 av |= FILE__WRITE;
3609
3610 if (prot & PROT_EXEC)
3611 av |= FILE__EXECUTE;
3612
David Howells88e67f32008-11-14 10:39:21 +11003613 return file_has_perm(cred, file, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 }
David Howellsd84f4f92008-11-14 10:39:23 +11003615
3616error:
3617 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618}
3619
Al Viroe5467852012-05-30 13:30:51 -04003620static int selinux_mmap_addr(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07003622 int rc = 0;
Paul Moore98883bf2014-03-19 16:46:11 -04003623
3624 if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
3625 u32 sid = current_sid();
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003626 rc = avc_has_perm(&selinux_state,
3627 sid, sid, SECCLASS_MEMPROTECT,
Paul Moore98883bf2014-03-19 16:46:11 -04003628 MEMPROTECT__MMAP_ZERO, NULL);
3629 }
3630
3631 return rc;
Al Viroe5467852012-05-30 13:30:51 -04003632}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633
Al Viroe5467852012-05-30 13:30:51 -04003634static int selinux_mmap_file(struct file *file, unsigned long reqprot,
3635 unsigned long prot, unsigned long flags)
3636{
Stephen Smalley3ba4bf52017-05-05 09:14:48 -04003637 struct common_audit_data ad;
3638 int rc;
3639
3640 if (file) {
3641 ad.type = LSM_AUDIT_DATA_FILE;
3642 ad.u.file = file;
3643 rc = inode_has_perm(current_cred(), file_inode(file),
3644 FILE__MAP, &ad);
3645 if (rc)
3646 return rc;
3647 }
3648
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003649 if (selinux_state.checkreqprot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 prot = reqprot;
3651
3652 return file_map_prot_check(file, prot,
3653 (flags & MAP_TYPE) == MAP_SHARED);
3654}
3655
3656static int selinux_file_mprotect(struct vm_area_struct *vma,
3657 unsigned long reqprot,
3658 unsigned long prot)
3659{
David Howells88e67f32008-11-14 10:39:21 +11003660 const struct cred *cred = current_cred();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003661 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003663 if (selinux_state.checkreqprot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 prot = reqprot;
3665
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003666 if (default_noexec &&
3667 (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
James Morrisd541bbe2009-01-29 12:19:51 +11003668 int rc = 0;
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003669 if (vma->vm_start >= vma->vm_mm->start_brk &&
3670 vma->vm_end <= vma->vm_mm->brk) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003671 rc = avc_has_perm(&selinux_state,
3672 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003673 PROCESS__EXECHEAP, NULL);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003674 } else if (!vma->vm_file &&
Stephen Smalleyc2316db2016-04-08 13:55:03 -04003675 ((vma->vm_start <= vma->vm_mm->start_stack &&
3676 vma->vm_end >= vma->vm_mm->start_stack) ||
Andy Lutomirskid17af502016-09-30 10:58:58 -07003677 vma_is_stack_for_current(vma))) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003678 rc = avc_has_perm(&selinux_state,
3679 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003680 PROCESS__EXECSTACK, NULL);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003681 } else if (vma->vm_file && vma->anon_vma) {
3682 /*
3683 * We are making executable a file mapping that has
3684 * had some COW done. Since pages might have been
3685 * written, check ability to execute the possibly
3686 * modified content. This typically should only
3687 * occur for text relocations.
3688 */
David Howellsd84f4f92008-11-14 10:39:23 +11003689 rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003690 }
Lorenzo Hernandez García-Hierro6b992192005-06-25 14:54:34 -07003691 if (rc)
3692 return rc;
3693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694
3695 return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
3696}
3697
3698static int selinux_file_lock(struct file *file, unsigned int cmd)
3699{
David Howells88e67f32008-11-14 10:39:21 +11003700 const struct cred *cred = current_cred();
3701
3702 return file_has_perm(cred, file, FILE__LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703}
3704
3705static int selinux_file_fcntl(struct file *file, unsigned int cmd,
3706 unsigned long arg)
3707{
David Howells88e67f32008-11-14 10:39:21 +11003708 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 int err = 0;
3710
3711 switch (cmd) {
Eric Paris828dfe12008-04-17 13:17:49 -04003712 case F_SETFL:
Eric Paris828dfe12008-04-17 13:17:49 -04003713 if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
David Howells88e67f32008-11-14 10:39:21 +11003714 err = file_has_perm(cred, file, FILE__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003716 }
3717 /* fall through */
3718 case F_SETOWN:
3719 case F_SETSIG:
3720 case F_GETFL:
3721 case F_GETOWN:
3722 case F_GETSIG:
Cyrill Gorcunov1d151c32012-07-30 14:43:00 -07003723 case F_GETOWNER_UIDS:
Eric Paris828dfe12008-04-17 13:17:49 -04003724 /* Just check FD__USE permission */
David Howells88e67f32008-11-14 10:39:21 +11003725 err = file_has_perm(cred, file, 0);
Eric Paris828dfe12008-04-17 13:17:49 -04003726 break;
3727 case F_GETLK:
3728 case F_SETLK:
3729 case F_SETLKW:
Jeff Layton0d3f7a22014-04-22 08:23:58 -04003730 case F_OFD_GETLK:
3731 case F_OFD_SETLK:
3732 case F_OFD_SETLKW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733#if BITS_PER_LONG == 32
Eric Paris828dfe12008-04-17 13:17:49 -04003734 case F_GETLK64:
3735 case F_SETLK64:
3736 case F_SETLKW64:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737#endif
David Howells88e67f32008-11-14 10:39:21 +11003738 err = file_has_perm(cred, file, FILE__LOCK);
Eric Paris828dfe12008-04-17 13:17:49 -04003739 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 }
3741
3742 return err;
3743}
3744
Jeff Laytone0b93ed2014-08-22 11:27:32 -04003745static void selinux_file_set_fowner(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 struct file_security_struct *fsec;
3748
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07003749 fsec = selinux_file(file);
David Howells275bb412008-11-14 10:39:19 +11003750 fsec->fown_sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751}
3752
3753static int selinux_file_send_sigiotask(struct task_struct *tsk,
3754 struct fown_struct *fown, int signum)
3755{
Eric Paris828dfe12008-04-17 13:17:49 -04003756 struct file *file;
Stephen Smalley65c90bc2009-05-04 15:43:18 -04003757 u32 sid = task_sid(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 struct file_security_struct *fsec;
3760
3761 /* struct fown_struct is never outside the context of a struct file */
Eric Paris828dfe12008-04-17 13:17:49 -04003762 file = container_of(fown, struct file, f_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07003764 fsec = selinux_file(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765
3766 if (!signum)
3767 perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
3768 else
3769 perm = signal_to_av(signum);
3770
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003771 return avc_has_perm(&selinux_state,
3772 fsec->fown_sid, sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 SECCLASS_PROCESS, perm, NULL);
3774}
3775
3776static int selinux_file_receive(struct file *file)
3777{
David Howells88e67f32008-11-14 10:39:21 +11003778 const struct cred *cred = current_cred();
3779
3780 return file_has_perm(cred, file, file_to_av(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781}
3782
Al Viro94817692018-07-10 14:13:18 -04003783static int selinux_file_open(struct file *file)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003784{
3785 struct file_security_struct *fsec;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003786 struct inode_security_struct *isec;
David Howellsd84f4f92008-11-14 10:39:23 +11003787
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07003788 fsec = selinux_file(file);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05003789 isec = inode_security(file_inode(file));
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003790 /*
3791 * Save inode label and policy sequence number
3792 * at open-time so that selinux_file_permission
3793 * can determine whether revalidation is necessary.
3794 * Task label is already saved in the file security
3795 * struct as its SID.
3796 */
3797 fsec->isid = isec->sid;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003798 fsec->pseqno = avc_policy_seqno(&selinux_state);
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003799 /*
3800 * Since the inode label or policy seqno may have changed
3801 * between the selinux_inode_permission check and the saving
3802 * of state above, recheck that access is still permitted.
3803 * Otherwise, access might never be revalidated against the
3804 * new inode label or new policy.
3805 * This check is not redundant - do not remove.
3806 */
Al Viro94817692018-07-10 14:13:18 -04003807 return file_path_has_perm(file->f_cred, file, open_file_to_av(file));
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003808}
3809
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810/* task security operations */
3811
Tetsuo Handaa79be232017-03-28 23:08:45 +09003812static int selinux_task_alloc(struct task_struct *task,
3813 unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003815 u32 sid = current_sid();
3816
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003817 return avc_has_perm(&selinux_state,
3818 sid, sid, SECCLASS_PROCESS, PROCESS__FORK, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819}
3820
David Howellsf1752ee2008-11-14 10:39:17 +11003821/*
David Howellsd84f4f92008-11-14 10:39:23 +11003822 * prepare a new set of credentials for modification
3823 */
3824static int selinux_cred_prepare(struct cred *new, const struct cred *old,
3825 gfp_t gfp)
3826{
Casey Schauflerbbd36622018-11-12 09:30:56 -08003827 const struct task_security_struct *old_tsec = selinux_cred(old);
3828 struct task_security_struct *tsec = selinux_cred(new);
David Howellsd84f4f92008-11-14 10:39:23 +11003829
Casey Schauflerbbd36622018-11-12 09:30:56 -08003830 *tsec = *old_tsec;
David Howellsd84f4f92008-11-14 10:39:23 +11003831 return 0;
3832}
3833
3834/*
David Howellsee18d642009-09-02 09:14:21 +01003835 * transfer the SELinux data to a blank set of creds
3836 */
3837static void selinux_cred_transfer(struct cred *new, const struct cred *old)
3838{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07003839 const struct task_security_struct *old_tsec = selinux_cred(old);
3840 struct task_security_struct *tsec = selinux_cred(new);
David Howellsee18d642009-09-02 09:14:21 +01003841
3842 *tsec = *old_tsec;
3843}
3844
Matthew Garrett3ec30112018-01-08 13:36:19 -08003845static void selinux_cred_getsecid(const struct cred *c, u32 *secid)
3846{
3847 *secid = cred_sid(c);
3848}
3849
David Howellsee18d642009-09-02 09:14:21 +01003850/*
David Howells3a3b7ce2008-11-14 10:39:28 +11003851 * set the security data for a kernel service
3852 * - all the creation contexts are set to unlabelled
3853 */
3854static int selinux_kernel_act_as(struct cred *new, u32 secid)
3855{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07003856 struct task_security_struct *tsec = selinux_cred(new);
David Howells3a3b7ce2008-11-14 10:39:28 +11003857 u32 sid = current_sid();
3858 int ret;
3859
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003860 ret = avc_has_perm(&selinux_state,
3861 sid, secid,
David Howells3a3b7ce2008-11-14 10:39:28 +11003862 SECCLASS_KERNEL_SERVICE,
3863 KERNEL_SERVICE__USE_AS_OVERRIDE,
3864 NULL);
3865 if (ret == 0) {
3866 tsec->sid = secid;
3867 tsec->create_sid = 0;
3868 tsec->keycreate_sid = 0;
3869 tsec->sockcreate_sid = 0;
3870 }
3871 return ret;
3872}
3873
3874/*
3875 * set the file creation context in a security record to the same as the
3876 * objective context of the specified inode
3877 */
3878static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
3879{
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05003880 struct inode_security_struct *isec = inode_security(inode);
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07003881 struct task_security_struct *tsec = selinux_cred(new);
David Howells3a3b7ce2008-11-14 10:39:28 +11003882 u32 sid = current_sid();
3883 int ret;
3884
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003885 ret = avc_has_perm(&selinux_state,
3886 sid, isec->sid,
David Howells3a3b7ce2008-11-14 10:39:28 +11003887 SECCLASS_KERNEL_SERVICE,
3888 KERNEL_SERVICE__CREATE_FILES_AS,
3889 NULL);
3890
3891 if (ret == 0)
3892 tsec->create_sid = isec->sid;
David Howellsef574712010-02-26 01:56:16 +00003893 return ret;
David Howells3a3b7ce2008-11-14 10:39:28 +11003894}
3895
Eric Parisdd8dbf22009-11-03 16:35:32 +11003896static int selinux_kernel_module_request(char *kmod_name)
Eric Paris25354c42009-08-13 09:45:03 -04003897{
Eric Parisdd8dbf22009-11-03 16:35:32 +11003898 struct common_audit_data ad;
3899
Eric Paris50c205f2012-04-04 15:01:43 -04003900 ad.type = LSM_AUDIT_DATA_KMOD;
Eric Parisdd8dbf22009-11-03 16:35:32 +11003901 ad.u.kmod_name = kmod_name;
3902
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003903 return avc_has_perm(&selinux_state,
3904 current_sid(), SECINITSID_KERNEL, SECCLASS_SYSTEM,
Eric Parisdd8dbf22009-11-03 16:35:32 +11003905 SYSTEM__MODULE_REQUEST, &ad);
Eric Paris25354c42009-08-13 09:45:03 -04003906}
3907
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003908static int selinux_kernel_module_from_file(struct file *file)
3909{
3910 struct common_audit_data ad;
3911 struct inode_security_struct *isec;
3912 struct file_security_struct *fsec;
3913 u32 sid = current_sid();
3914 int rc;
3915
3916 /* init_module */
3917 if (file == NULL)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003918 return avc_has_perm(&selinux_state,
3919 sid, sid, SECCLASS_SYSTEM,
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003920 SYSTEM__MODULE_LOAD, NULL);
3921
3922 /* finit_module */
Paul Moore20cdef82016-04-04 14:14:42 -04003923
Vivek Goyal43af5de2016-09-09 11:37:49 -04003924 ad.type = LSM_AUDIT_DATA_FILE;
3925 ad.u.file = file;
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003926
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07003927 fsec = selinux_file(file);
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003928 if (sid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003929 rc = avc_has_perm(&selinux_state,
3930 sid, fsec->sid, SECCLASS_FD, FD__USE, &ad);
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003931 if (rc)
3932 return rc;
3933 }
3934
Paul Moore20cdef82016-04-04 14:14:42 -04003935 isec = inode_security(file_inode(file));
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003936 return avc_has_perm(&selinux_state,
3937 sid, isec->sid, SECCLASS_SYSTEM,
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003938 SYSTEM__MODULE_LOAD, &ad);
3939}
3940
3941static int selinux_kernel_read_file(struct file *file,
3942 enum kernel_read_file_id id)
3943{
3944 int rc = 0;
3945
3946 switch (id) {
3947 case READING_MODULE:
3948 rc = selinux_kernel_module_from_file(file);
3949 break;
3950 default:
3951 break;
3952 }
3953
3954 return rc;
3955}
3956
Mimi Zoharc77b8cd2018-07-13 14:06:02 -04003957static int selinux_kernel_load_data(enum kernel_load_data_id id)
3958{
3959 int rc = 0;
3960
3961 switch (id) {
3962 case LOADING_MODULE:
3963 rc = selinux_kernel_module_from_file(NULL);
3964 default:
3965 break;
3966 }
3967
3968 return rc;
3969}
3970
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
3972{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003973 return avc_has_perm(&selinux_state,
3974 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003975 PROCESS__SETPGID, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976}
3977
3978static int selinux_task_getpgid(struct task_struct *p)
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__GETPGID, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983}
3984
3985static int selinux_task_getsid(struct task_struct *p)
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__GETSESSION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990}
3991
David Quigleyf9008e4c2006-06-30 01:55:46 -07003992static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
3993{
David Howells275bb412008-11-14 10:39:19 +11003994 *secid = task_sid(p);
David Quigleyf9008e4c2006-06-30 01:55:46 -07003995}
3996
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997static int selinux_task_setnice(struct task_struct *p, int nice)
3998{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003999 return avc_has_perm(&selinux_state,
4000 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004001 PROCESS__SETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002}
4003
James Morris03e68062006-06-23 02:03:58 -07004004static int selinux_task_setioprio(struct task_struct *p, int ioprio)
4005{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004006 return avc_has_perm(&selinux_state,
4007 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004008 PROCESS__SETSCHED, NULL);
James Morris03e68062006-06-23 02:03:58 -07004009}
4010
David Quigleya1836a42006-06-30 01:55:49 -07004011static int selinux_task_getioprio(struct task_struct *p)
4012{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004013 return avc_has_perm(&selinux_state,
4014 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004015 PROCESS__GETSCHED, NULL);
David Quigleya1836a42006-06-30 01:55:49 -07004016}
4017
Corentin LABBE42985552017-10-04 20:32:18 +02004018static int selinux_task_prlimit(const struct cred *cred, const struct cred *tcred,
4019 unsigned int flags)
Stephen Smalley791ec492017-02-17 07:57:00 -05004020{
4021 u32 av = 0;
4022
Stephen Smalley84e68852017-02-28 09:35:08 -05004023 if (!flags)
4024 return 0;
Stephen Smalley791ec492017-02-17 07:57:00 -05004025 if (flags & LSM_PRLIMIT_WRITE)
4026 av |= PROCESS__SETRLIMIT;
4027 if (flags & LSM_PRLIMIT_READ)
4028 av |= PROCESS__GETRLIMIT;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004029 return avc_has_perm(&selinux_state,
4030 cred_sid(cred), cred_sid(tcred),
Stephen Smalley791ec492017-02-17 07:57:00 -05004031 SECCLASS_PROCESS, av, NULL);
4032}
4033
Jiri Slaby8fd00b42009-08-26 18:41:16 +02004034static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
4035 struct rlimit *new_rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036{
Jiri Slaby8fd00b42009-08-26 18:41:16 +02004037 struct rlimit *old_rlim = p->signal->rlim + resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038
4039 /* Control the ability to change the hard limit (whether
4040 lowering or raising it), so that the hard limit can
4041 later be used as a safe reset point for the soft limit
David Howellsd84f4f92008-11-14 10:39:23 +11004042 upon context transitions. See selinux_bprm_committing_creds. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 if (old_rlim->rlim_max != new_rlim->rlim_max)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004044 return avc_has_perm(&selinux_state,
4045 current_sid(), task_sid(p),
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004046 SECCLASS_PROCESS, PROCESS__SETRLIMIT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047
4048 return 0;
4049}
4050
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09004051static int selinux_task_setscheduler(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004053 return avc_has_perm(&selinux_state,
4054 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004055 PROCESS__SETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056}
4057
4058static int selinux_task_getscheduler(struct task_struct *p)
4059{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004060 return avc_has_perm(&selinux_state,
4061 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004062 PROCESS__GETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063}
4064
David Quigley35601542006-06-23 02:04:01 -07004065static int selinux_task_movememory(struct task_struct *p)
4066{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004067 return avc_has_perm(&selinux_state,
4068 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004069 PROCESS__SETSCHED, NULL);
David Quigley35601542006-06-23 02:04:01 -07004070}
4071
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02004072static int selinux_task_kill(struct task_struct *p, struct kernel_siginfo *info,
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004073 int sig, const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074{
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004075 u32 secid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 if (!sig)
4079 perm = PROCESS__SIGNULL; /* null signal; existence test */
4080 else
4081 perm = signal_to_av(sig);
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004082 if (!cred)
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004083 secid = current_sid();
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004084 else
4085 secid = cred_sid(cred);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004086 return avc_has_perm(&selinux_state,
4087 secid, task_sid(p), SECCLASS_PROCESS, perm, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088}
4089
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090static void selinux_task_to_inode(struct task_struct *p,
4091 struct inode *inode)
4092{
Casey Schaufler80788c22018-09-21 17:19:11 -07004093 struct inode_security_struct *isec = selinux_inode(inode);
David Howells275bb412008-11-14 10:39:19 +11004094 u32 sid = task_sid(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004096 spin_lock(&isec->lock);
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01004097 isec->sclass = inode_mode_to_security_class(inode->i_mode);
David Howells275bb412008-11-14 10:39:19 +11004098 isec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004099 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004100 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101}
4102
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004104static int selinux_parse_skb_ipv4(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04004105 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106{
4107 int offset, ihlen, ret = -EINVAL;
4108 struct iphdr _iph, *ih;
4109
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03004110 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
4112 if (ih == NULL)
4113 goto out;
4114
4115 ihlen = ih->ihl * 4;
4116 if (ihlen < sizeof(_iph))
4117 goto out;
4118
Eric Paris48c62af2012-04-02 13:15:44 -04004119 ad->u.net->v4info.saddr = ih->saddr;
4120 ad->u.net->v4info.daddr = ih->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 ret = 0;
4122
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004123 if (proto)
4124 *proto = ih->protocol;
4125
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 switch (ih->protocol) {
Eric Paris828dfe12008-04-17 13:17:49 -04004127 case IPPROTO_TCP: {
4128 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129
Eric Paris828dfe12008-04-17 13:17:49 -04004130 if (ntohs(ih->frag_off) & IP_OFFSET)
4131 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132
4133 offset += ihlen;
4134 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4135 if (th == NULL)
4136 break;
4137
Eric Paris48c62af2012-04-02 13:15:44 -04004138 ad->u.net->sport = th->source;
4139 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142
Eric Paris828dfe12008-04-17 13:17:49 -04004143 case IPPROTO_UDP: {
4144 struct udphdr _udph, *uh;
4145
4146 if (ntohs(ih->frag_off) & IP_OFFSET)
4147 break;
4148
4149 offset += ihlen;
4150 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4151 if (uh == NULL)
4152 break;
4153
Eric Paris48c62af2012-04-02 13:15:44 -04004154 ad->u.net->sport = uh->source;
4155 ad->u.net->dport = uh->dest;
Eric Paris828dfe12008-04-17 13:17:49 -04004156 break;
4157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158
James Morris2ee92d42006-11-13 16:09:01 -08004159 case IPPROTO_DCCP: {
4160 struct dccp_hdr _dccph, *dh;
4161
4162 if (ntohs(ih->frag_off) & IP_OFFSET)
4163 break;
4164
4165 offset += ihlen;
4166 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4167 if (dh == NULL)
4168 break;
4169
Eric Paris48c62af2012-04-02 13:15:44 -04004170 ad->u.net->sport = dh->dccph_sport;
4171 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08004172 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004173 }
James Morris2ee92d42006-11-13 16:09:01 -08004174
Richard Hainesd4529302018-02-13 20:57:18 +00004175#if IS_ENABLED(CONFIG_IP_SCTP)
4176 case IPPROTO_SCTP: {
4177 struct sctphdr _sctph, *sh;
4178
4179 if (ntohs(ih->frag_off) & IP_OFFSET)
4180 break;
4181
4182 offset += ihlen;
4183 sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4184 if (sh == NULL)
4185 break;
4186
4187 ad->u.net->sport = sh->source;
4188 ad->u.net->dport = sh->dest;
4189 break;
4190 }
4191#endif
Eric Paris828dfe12008-04-17 13:17:49 -04004192 default:
4193 break;
4194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195out:
4196 return ret;
4197}
4198
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04004199#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200
4201/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004202static int selinux_parse_skb_ipv6(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04004203 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204{
4205 u8 nexthdr;
4206 int ret = -EINVAL, offset;
4207 struct ipv6hdr _ipv6h, *ip6;
Jesse Gross75f28112011-11-30 17:05:51 -08004208 __be16 frag_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03004210 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
4212 if (ip6 == NULL)
4213 goto out;
4214
Eric Paris48c62af2012-04-02 13:15:44 -04004215 ad->u.net->v6info.saddr = ip6->saddr;
4216 ad->u.net->v6info.daddr = ip6->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 ret = 0;
4218
4219 nexthdr = ip6->nexthdr;
4220 offset += sizeof(_ipv6h);
Jesse Gross75f28112011-11-30 17:05:51 -08004221 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222 if (offset < 0)
4223 goto out;
4224
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004225 if (proto)
4226 *proto = nexthdr;
4227
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 switch (nexthdr) {
4229 case IPPROTO_TCP: {
Eric Paris828dfe12008-04-17 13:17:49 -04004230 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231
4232 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4233 if (th == NULL)
4234 break;
4235
Eric Paris48c62af2012-04-02 13:15:44 -04004236 ad->u.net->sport = th->source;
4237 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 break;
4239 }
4240
4241 case IPPROTO_UDP: {
4242 struct udphdr _udph, *uh;
4243
4244 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4245 if (uh == NULL)
4246 break;
4247
Eric Paris48c62af2012-04-02 13:15:44 -04004248 ad->u.net->sport = uh->source;
4249 ad->u.net->dport = uh->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 break;
4251 }
4252
James Morris2ee92d42006-11-13 16:09:01 -08004253 case IPPROTO_DCCP: {
4254 struct dccp_hdr _dccph, *dh;
4255
4256 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4257 if (dh == NULL)
4258 break;
4259
Eric Paris48c62af2012-04-02 13:15:44 -04004260 ad->u.net->sport = dh->dccph_sport;
4261 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08004262 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004263 }
James Morris2ee92d42006-11-13 16:09:01 -08004264
Richard Hainesd4529302018-02-13 20:57:18 +00004265#if IS_ENABLED(CONFIG_IP_SCTP)
4266 case IPPROTO_SCTP: {
4267 struct sctphdr _sctph, *sh;
4268
4269 sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4270 if (sh == NULL)
4271 break;
4272
4273 ad->u.net->sport = sh->source;
4274 ad->u.net->dport = sh->dest;
4275 break;
4276 }
4277#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 /* includes fragments */
4279 default:
4280 break;
4281 }
4282out:
4283 return ret;
4284}
4285
4286#endif /* IPV6 */
4287
Thomas Liu2bf49692009-07-14 12:14:09 -04004288static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
David Howellscf9481e2008-07-27 21:31:07 +10004289 char **_addrp, int src, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290{
David Howellscf9481e2008-07-27 21:31:07 +10004291 char *addrp;
4292 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293
Eric Paris48c62af2012-04-02 13:15:44 -04004294 switch (ad->u.net->family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 case PF_INET:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004296 ret = selinux_parse_skb_ipv4(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10004297 if (ret)
4298 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04004299 addrp = (char *)(src ? &ad->u.net->v4info.saddr :
4300 &ad->u.net->v4info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10004301 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04004303#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 case PF_INET6:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004305 ret = selinux_parse_skb_ipv6(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10004306 if (ret)
4307 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04004308 addrp = (char *)(src ? &ad->u.net->v6info.saddr :
4309 &ad->u.net->v6info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10004310 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311#endif /* IPV6 */
4312 default:
David Howellscf9481e2008-07-27 21:31:07 +10004313 addrp = NULL;
4314 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 }
4316
David Howellscf9481e2008-07-27 21:31:07 +10004317parse_error:
peter enderborgc103a912018-06-12 10:09:03 +02004318 pr_warn(
David Howellscf9481e2008-07-27 21:31:07 +10004319 "SELinux: failure in selinux_parse_skb(),"
4320 " unable to parse packet\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 return ret;
David Howellscf9481e2008-07-27 21:31:07 +10004322
4323okay:
4324 if (_addrp)
4325 *_addrp = addrp;
4326 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327}
4328
Paul Moore4f6a9932007-03-01 14:35:22 -05004329/**
Paul Moore220deb92008-01-29 08:38:23 -05004330 * selinux_skb_peerlbl_sid - Determine the peer label of a packet
Paul Moore4f6a9932007-03-01 14:35:22 -05004331 * @skb: the packet
Paul Moore75e22912008-01-29 08:38:04 -05004332 * @family: protocol family
Paul Moore220deb92008-01-29 08:38:23 -05004333 * @sid: the packet's peer label SID
Paul Moore4f6a9932007-03-01 14:35:22 -05004334 *
4335 * Description:
Paul Moore220deb92008-01-29 08:38:23 -05004336 * Check the various different forms of network peer labeling and determine
4337 * the peer label/SID for the packet; most of the magic actually occurs in
4338 * the security server function security_net_peersid_cmp(). The function
4339 * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
4340 * or -EACCES if @sid is invalid due to inconsistencies with the different
4341 * peer labels.
Paul Moore4f6a9932007-03-01 14:35:22 -05004342 *
4343 */
Paul Moore220deb92008-01-29 08:38:23 -05004344static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
Paul Moore4f6a9932007-03-01 14:35:22 -05004345{
Paul Moore71f1cb02008-01-29 08:51:16 -05004346 int err;
Paul Moore4f6a9932007-03-01 14:35:22 -05004347 u32 xfrm_sid;
4348 u32 nlbl_sid;
Paul Moore220deb92008-01-29 08:38:23 -05004349 u32 nlbl_type;
Paul Moore4f6a9932007-03-01 14:35:22 -05004350
Paul Moore817eff72013-12-10 14:57:54 -05004351 err = selinux_xfrm_skb_sid(skb, &xfrm_sid);
Paul Moorebed4d7e2013-07-23 17:38:40 -04004352 if (unlikely(err))
4353 return -EACCES;
4354 err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
4355 if (unlikely(err))
4356 return -EACCES;
Paul Moore220deb92008-01-29 08:38:23 -05004357
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004358 err = security_net_peersid_resolve(&selinux_state, nlbl_sid,
4359 nlbl_type, xfrm_sid, sid);
Paul Moore71f1cb02008-01-29 08:51:16 -05004360 if (unlikely(err)) {
peter enderborgc103a912018-06-12 10:09:03 +02004361 pr_warn(
Paul Moore71f1cb02008-01-29 08:51:16 -05004362 "SELinux: failure in selinux_skb_peerlbl_sid(),"
4363 " unable to determine packet's peer label\n");
Paul Moore220deb92008-01-29 08:38:23 -05004364 return -EACCES;
Paul Moore71f1cb02008-01-29 08:51:16 -05004365 }
Paul Moore220deb92008-01-29 08:38:23 -05004366
4367 return 0;
Paul Moore4f6a9932007-03-01 14:35:22 -05004368}
4369
Paul Moore446b8022013-12-04 16:10:51 -05004370/**
4371 * selinux_conn_sid - Determine the child socket label for a connection
4372 * @sk_sid: the parent socket's SID
4373 * @skb_sid: the packet's SID
4374 * @conn_sid: the resulting connection SID
4375 *
4376 * If @skb_sid is valid then the user:role:type information from @sk_sid is
4377 * combined with the MLS information from @skb_sid in order to create
4378 * @conn_sid. If @skb_sid is not valid then then @conn_sid is simply a copy
4379 * of @sk_sid. Returns zero on success, negative values on failure.
4380 *
4381 */
4382static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid)
4383{
4384 int err = 0;
4385
4386 if (skb_sid != SECSID_NULL)
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004387 err = security_sid_mls_copy(&selinux_state, sk_sid, skb_sid,
4388 conn_sid);
Paul Moore446b8022013-12-04 16:10:51 -05004389 else
4390 *conn_sid = sk_sid;
4391
4392 return err;
4393}
4394
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395/* socket security operations */
Paul Moored4f2d972010-04-22 14:46:18 -04004396
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004397static int socket_sockcreate_sid(const struct task_security_struct *tsec,
4398 u16 secclass, u32 *socksid)
Paul Moored4f2d972010-04-22 14:46:18 -04004399{
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004400 if (tsec->sockcreate_sid > SECSID_NULL) {
4401 *socksid = tsec->sockcreate_sid;
4402 return 0;
4403 }
4404
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004405 return security_transition_sid(&selinux_state, tsec->sid, tsec->sid,
4406 secclass, NULL, socksid);
Paul Moored4f2d972010-04-22 14:46:18 -04004407}
4408
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004409static int sock_has_perm(struct sock *sk, u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410{
Paul Moore253bfae2010-04-22 14:46:19 -04004411 struct sk_security_struct *sksec = sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004412 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004413 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414
Paul Moore253bfae2010-04-22 14:46:19 -04004415 if (sksec->sid == SECINITSID_KERNEL)
4416 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417
Eric Paris50c205f2012-04-04 15:01:43 -04004418 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004419 ad.u.net = &net;
4420 ad.u.net->sk = sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004422 return avc_has_perm(&selinux_state,
4423 current_sid(), sksec->sid, sksec->sclass, perms,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004424 &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425}
4426
4427static int selinux_socket_create(int family, int type,
4428 int protocol, int kern)
4429{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07004430 const struct task_security_struct *tsec = selinux_cred(current_cred());
Paul Moored4f2d972010-04-22 14:46:18 -04004431 u32 newsid;
David Howells275bb412008-11-14 10:39:19 +11004432 u16 secclass;
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004433 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434
4435 if (kern)
Paul Moored4f2d972010-04-22 14:46:18 -04004436 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437
David Howells275bb412008-11-14 10:39:19 +11004438 secclass = socket_type_to_security_class(family, type, protocol);
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004439 rc = socket_sockcreate_sid(tsec, secclass, &newsid);
4440 if (rc)
4441 return rc;
4442
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004443 return avc_has_perm(&selinux_state,
4444 tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445}
4446
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07004447static int selinux_socket_post_create(struct socket *sock, int family,
4448 int type, int protocol, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07004450 const struct task_security_struct *tsec = selinux_cred(current_cred());
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05004451 struct inode_security_struct *isec = inode_security_novalidate(SOCK_INODE(sock));
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004452 struct sk_security_struct *sksec;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004453 u16 sclass = socket_type_to_security_class(family, type, protocol);
4454 u32 sid = SECINITSID_KERNEL;
David Howells275bb412008-11-14 10:39:19 +11004455 int err = 0;
4456
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004457 if (!kern) {
4458 err = socket_sockcreate_sid(tsec, sclass, &sid);
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004459 if (err)
4460 return err;
4461 }
David Howells275bb412008-11-14 10:39:19 +11004462
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004463 isec->sclass = sclass;
4464 isec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004465 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004467 if (sock->sk) {
4468 sksec = sock->sk->sk_security;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004469 sksec->sclass = sclass;
4470 sksec->sid = sid;
Richard Hainesd4529302018-02-13 20:57:18 +00004471 /* Allows detection of the first association on this socket */
4472 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4473 sksec->sctp_assoc_state = SCTP_ASSOC_UNSET;
4474
Paul Moore389fb8002009-03-27 17:10:34 -04004475 err = selinux_netlbl_socket_post_create(sock->sk, family);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004476 }
4477
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07004478 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479}
4480
David Herrmann0b811db2018-05-04 16:28:21 +02004481static int selinux_socket_socketpair(struct socket *socka,
4482 struct socket *sockb)
4483{
4484 struct sk_security_struct *sksec_a = socka->sk->sk_security;
4485 struct sk_security_struct *sksec_b = sockb->sk->sk_security;
4486
4487 sksec_a->peer_sid = sksec_b->sid;
4488 sksec_b->peer_sid = sksec_a->sid;
4489
4490 return 0;
4491}
4492
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493/* Range of port numbers used to automatically bind.
4494 Need to determine whether we should perform a name_bind
4495 permission check between the socket and the port number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496
4497static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
4498{
Paul Moore253bfae2010-04-22 14:46:19 -04004499 struct sock *sk = sock->sk;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004500 struct sk_security_struct *sksec = sk->sk_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 u16 family;
4502 int err;
4503
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004504 err = sock_has_perm(sk, SOCKET__BIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 if (err)
4506 goto out;
4507
Richard Hainesd4529302018-02-13 20:57:18 +00004508 /* If PF_INET or PF_INET6, check name_bind permission for the port. */
Paul Moore253bfae2010-04-22 14:46:19 -04004509 family = sk->sk_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 if (family == PF_INET || family == PF_INET6) {
4511 char *addrp;
Thomas Liu2bf49692009-07-14 12:14:09 -04004512 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004513 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 struct sockaddr_in *addr4 = NULL;
4515 struct sockaddr_in6 *addr6 = NULL;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004516 u16 family_sa = address->sa_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517 unsigned short snum;
James Morrise399f982008-06-12 01:39:58 +10004518 u32 sid, node_perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519
Richard Hainesd4529302018-02-13 20:57:18 +00004520 /*
4521 * sctp_bindx(3) calls via selinux_sctp_bind_connect()
4522 * that validates multiple binding addresses. Because of this
4523 * need to check address->sa_family as it is possible to have
4524 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4525 */
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004526 switch (family_sa) {
4527 case AF_UNSPEC:
Richard Haines68741a8a2018-03-02 19:54:34 +00004528 case AF_INET:
4529 if (addrlen < sizeof(struct sockaddr_in))
4530 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 addr4 = (struct sockaddr_in *)address;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004532 if (family_sa == AF_UNSPEC) {
4533 /* see __inet_bind(), we only want to allow
4534 * AF_UNSPEC if the address is INADDR_ANY
4535 */
4536 if (addr4->sin_addr.s_addr != htonl(INADDR_ANY))
4537 goto err_af;
4538 family_sa = AF_INET;
4539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540 snum = ntohs(addr4->sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541 addrp = (char *)&addr4->sin_addr.s_addr;
Richard Haines68741a8a2018-03-02 19:54:34 +00004542 break;
4543 case AF_INET6:
4544 if (addrlen < SIN6_LEN_RFC2133)
4545 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546 addr6 = (struct sockaddr_in6 *)address;
4547 snum = ntohs(addr6->sin6_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548 addrp = (char *)&addr6->sin6_addr.s6_addr;
Richard Haines68741a8a2018-03-02 19:54:34 +00004549 break;
4550 default:
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004551 goto err_af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552 }
4553
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004554 ad.type = LSM_AUDIT_DATA_NET;
4555 ad.u.net = &net;
4556 ad.u.net->sport = htons(snum);
4557 ad.u.net->family = family_sa;
4558
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004559 if (snum) {
4560 int low, high;
4561
Eric W. Biederman0bbf87d2013-09-28 14:10:59 -07004562 inet_get_local_port_range(sock_net(sk), &low, &high);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004563
Krister Johansen4548b682017-01-20 17:49:11 -08004564 if (snum < max(inet_prot_sock(sock_net(sk)), low) ||
4565 snum > high) {
Paul Moore3e1121722008-04-10 10:48:14 -04004566 err = sel_netport_sid(sk->sk_protocol,
4567 snum, &sid);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004568 if (err)
4569 goto out;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004570 err = avc_has_perm(&selinux_state,
4571 sksec->sid, sid,
Paul Moore253bfae2010-04-22 14:46:19 -04004572 sksec->sclass,
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004573 SOCKET__NAME_BIND, &ad);
4574 if (err)
4575 goto out;
4576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 }
Eric Paris828dfe12008-04-17 13:17:49 -04004578
Paul Moore253bfae2010-04-22 14:46:19 -04004579 switch (sksec->sclass) {
James Morris13402582005-09-30 14:24:34 -04004580 case SECCLASS_TCP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 node_perm = TCP_SOCKET__NODE_BIND;
4582 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004583
James Morris13402582005-09-30 14:24:34 -04004584 case SECCLASS_UDP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 node_perm = UDP_SOCKET__NODE_BIND;
4586 break;
James Morris2ee92d42006-11-13 16:09:01 -08004587
4588 case SECCLASS_DCCP_SOCKET:
4589 node_perm = DCCP_SOCKET__NODE_BIND;
4590 break;
4591
Richard Hainesd4529302018-02-13 20:57:18 +00004592 case SECCLASS_SCTP_SOCKET:
4593 node_perm = SCTP_SOCKET__NODE_BIND;
4594 break;
4595
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 default:
4597 node_perm = RAWIP_SOCKET__NODE_BIND;
4598 break;
4599 }
Eric Paris828dfe12008-04-17 13:17:49 -04004600
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004601 err = sel_netnode_sid(addrp, family_sa, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602 if (err)
4603 goto out;
Eric Paris828dfe12008-04-17 13:17:49 -04004604
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004605 if (family_sa == AF_INET)
Eric Paris48c62af2012-04-02 13:15:44 -04004606 ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 else
Eric Paris48c62af2012-04-02 13:15:44 -04004608 ad.u.net->v6info.saddr = addr6->sin6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004610 err = avc_has_perm(&selinux_state,
4611 sksec->sid, sid,
Paul Moore253bfae2010-04-22 14:46:19 -04004612 sksec->sclass, node_perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 if (err)
4614 goto out;
4615 }
4616out:
4617 return err;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004618err_af:
4619 /* Note that SCTP services expect -EINVAL, others -EAFNOSUPPORT. */
4620 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4621 return -EINVAL;
4622 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623}
4624
Richard Hainesd4529302018-02-13 20:57:18 +00004625/* This supports connect(2) and SCTP connect services such as sctp_connectx(3)
Kees Cookd61330c2019-02-17 14:08:36 -08004626 * and sctp_sendmsg(3) as described in Documentation/security/SCTP.rst
Richard Hainesd4529302018-02-13 20:57:18 +00004627 */
4628static int selinux_socket_connect_helper(struct socket *sock,
4629 struct sockaddr *address, int addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630{
Paul Moore014ab192008-10-10 10:16:33 -04004631 struct sock *sk = sock->sk;
Paul Moore253bfae2010-04-22 14:46:19 -04004632 struct sk_security_struct *sksec = sk->sk_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 int err;
4634
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004635 err = sock_has_perm(sk, SOCKET__CONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 if (err)
4637 return err;
4638
4639 /*
Richard Hainesd4529302018-02-13 20:57:18 +00004640 * If a TCP, DCCP or SCTP socket, check name_connect permission
4641 * for the port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 */
Paul Moore253bfae2010-04-22 14:46:19 -04004643 if (sksec->sclass == SECCLASS_TCP_SOCKET ||
Richard Hainesd4529302018-02-13 20:57:18 +00004644 sksec->sclass == SECCLASS_DCCP_SOCKET ||
4645 sksec->sclass == SECCLASS_SCTP_SOCKET) {
Thomas Liu2bf49692009-07-14 12:14:09 -04004646 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004647 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 struct sockaddr_in *addr4 = NULL;
4649 struct sockaddr_in6 *addr6 = NULL;
4650 unsigned short snum;
James Morris2ee92d42006-11-13 16:09:01 -08004651 u32 sid, perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652
Richard Hainesd4529302018-02-13 20:57:18 +00004653 /* sctp_connectx(3) calls via selinux_sctp_bind_connect()
4654 * that validates multiple connect addresses. Because of this
4655 * need to check address->sa_family as it is possible to have
4656 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4657 */
Richard Haines68741a8a2018-03-02 19:54:34 +00004658 switch (address->sa_family) {
4659 case AF_INET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660 addr4 = (struct sockaddr_in *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004661 if (addrlen < sizeof(struct sockaddr_in))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 return -EINVAL;
4663 snum = ntohs(addr4->sin_port);
Richard Haines68741a8a2018-03-02 19:54:34 +00004664 break;
4665 case AF_INET6:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 addr6 = (struct sockaddr_in6 *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004667 if (addrlen < SIN6_LEN_RFC2133)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668 return -EINVAL;
4669 snum = ntohs(addr6->sin6_port);
Richard Haines68741a8a2018-03-02 19:54:34 +00004670 break;
4671 default:
4672 /* Note that SCTP services expect -EINVAL, whereas
4673 * others expect -EAFNOSUPPORT.
4674 */
4675 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4676 return -EINVAL;
4677 else
4678 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 }
4680
Paul Moore3e1121722008-04-10 10:48:14 -04004681 err = sel_netport_sid(sk->sk_protocol, snum, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 if (err)
Richard Hainesd4529302018-02-13 20:57:18 +00004683 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684
Richard Hainesd4529302018-02-13 20:57:18 +00004685 switch (sksec->sclass) {
4686 case SECCLASS_TCP_SOCKET:
4687 perm = TCP_SOCKET__NAME_CONNECT;
4688 break;
4689 case SECCLASS_DCCP_SOCKET:
4690 perm = DCCP_SOCKET__NAME_CONNECT;
4691 break;
4692 case SECCLASS_SCTP_SOCKET:
4693 perm = SCTP_SOCKET__NAME_CONNECT;
4694 break;
4695 }
James Morris2ee92d42006-11-13 16:09:01 -08004696
Eric Paris50c205f2012-04-04 15:01:43 -04004697 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004698 ad.u.net = &net;
4699 ad.u.net->dport = htons(snum);
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004700 ad.u.net->family = address->sa_family;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004701 err = avc_has_perm(&selinux_state,
4702 sksec->sid, sid, sksec->sclass, perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 if (err)
Richard Hainesd4529302018-02-13 20:57:18 +00004704 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705 }
4706
Richard Hainesd4529302018-02-13 20:57:18 +00004707 return 0;
4708}
Paul Moore014ab192008-10-10 10:16:33 -04004709
Richard Hainesd4529302018-02-13 20:57:18 +00004710/* Supports connect(2), see comments in selinux_socket_connect_helper() */
4711static int selinux_socket_connect(struct socket *sock,
4712 struct sockaddr *address, int addrlen)
4713{
4714 int err;
4715 struct sock *sk = sock->sk;
4716
4717 err = selinux_socket_connect_helper(sock, address, addrlen);
4718 if (err)
4719 return err;
4720
4721 return selinux_netlbl_socket_connect(sk, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722}
4723
4724static int selinux_socket_listen(struct socket *sock, int backlog)
4725{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004726 return sock_has_perm(sock->sk, SOCKET__LISTEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727}
4728
4729static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
4730{
4731 int err;
4732 struct inode_security_struct *isec;
4733 struct inode_security_struct *newisec;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004734 u16 sclass;
4735 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004737 err = sock_has_perm(sock->sk, SOCKET__ACCEPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 if (err)
4739 return err;
4740
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05004741 isec = inode_security_novalidate(SOCK_INODE(sock));
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004742 spin_lock(&isec->lock);
4743 sclass = isec->sclass;
4744 sid = isec->sid;
4745 spin_unlock(&isec->lock);
4746
4747 newisec = inode_security_novalidate(SOCK_INODE(newsock));
4748 newisec->sclass = sclass;
4749 newisec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004750 newisec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751
4752 return 0;
4753}
4754
4755static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
Eric Paris828dfe12008-04-17 13:17:49 -04004756 int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004758 return sock_has_perm(sock->sk, SOCKET__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759}
4760
4761static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
4762 int size, int flags)
4763{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004764 return sock_has_perm(sock->sk, SOCKET__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765}
4766
4767static int selinux_socket_getsockname(struct socket *sock)
4768{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004769 return sock_has_perm(sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770}
4771
4772static int selinux_socket_getpeername(struct socket *sock)
4773{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004774 return sock_has_perm(sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775}
4776
Eric Paris828dfe12008-04-17 13:17:49 -04004777static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778{
Paul Mooref8687af2006-10-30 15:22:15 -08004779 int err;
4780
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004781 err = sock_has_perm(sock->sk, SOCKET__SETOPT);
Paul Mooref8687af2006-10-30 15:22:15 -08004782 if (err)
4783 return err;
4784
4785 return selinux_netlbl_socket_setsockopt(sock, level, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786}
4787
4788static int selinux_socket_getsockopt(struct socket *sock, int level,
4789 int optname)
4790{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004791 return sock_has_perm(sock->sk, SOCKET__GETOPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792}
4793
4794static int selinux_socket_shutdown(struct socket *sock, int how)
4795{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004796 return sock_has_perm(sock->sk, SOCKET__SHUTDOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797}
4798
David S. Miller3610cda2011-01-05 15:38:53 -08004799static int selinux_socket_unix_stream_connect(struct sock *sock,
4800 struct sock *other,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 struct sock *newsk)
4802{
David S. Miller3610cda2011-01-05 15:38:53 -08004803 struct sk_security_struct *sksec_sock = sock->sk_security;
4804 struct sk_security_struct *sksec_other = other->sk_security;
Paul Moore4d1e2452010-04-22 14:46:18 -04004805 struct sk_security_struct *sksec_new = newsk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004806 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004807 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808 int err;
4809
Eric Paris50c205f2012-04-04 15:01:43 -04004810 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004811 ad.u.net = &net;
4812 ad.u.net->sk = other;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004814 err = avc_has_perm(&selinux_state,
4815 sksec_sock->sid, sksec_other->sid,
Paul Moore4d1e2452010-04-22 14:46:18 -04004816 sksec_other->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817 UNIX_STREAM_SOCKET__CONNECTTO, &ad);
4818 if (err)
4819 return err;
4820
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821 /* server child socket */
Paul Moore4d1e2452010-04-22 14:46:18 -04004822 sksec_new->peer_sid = sksec_sock->sid;
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004823 err = security_sid_mls_copy(&selinux_state, sksec_other->sid,
4824 sksec_sock->sid, &sksec_new->sid);
Paul Moore4d1e2452010-04-22 14:46:18 -04004825 if (err)
4826 return err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004827
Paul Moore4d1e2452010-04-22 14:46:18 -04004828 /* connecting socket */
4829 sksec_sock->peer_sid = sksec_new->sid;
4830
4831 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832}
4833
4834static int selinux_socket_unix_may_send(struct socket *sock,
4835 struct socket *other)
4836{
Paul Moore253bfae2010-04-22 14:46:19 -04004837 struct sk_security_struct *ssec = sock->sk->sk_security;
4838 struct sk_security_struct *osec = other->sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004839 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004840 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841
Eric Paris50c205f2012-04-04 15:01:43 -04004842 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004843 ad.u.net = &net;
4844 ad.u.net->sk = other->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004846 return avc_has_perm(&selinux_state,
4847 ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
Paul Moore253bfae2010-04-22 14:46:19 -04004848 &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849}
4850
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004851static int selinux_inet_sys_rcv_skb(struct net *ns, int ifindex,
4852 char *addrp, u16 family, u32 peer_sid,
Thomas Liu2bf49692009-07-14 12:14:09 -04004853 struct common_audit_data *ad)
Paul Mooreeffad8d2008-01-29 08:49:27 -05004854{
4855 int err;
4856 u32 if_sid;
4857 u32 node_sid;
4858
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004859 err = sel_netif_sid(ns, ifindex, &if_sid);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004860 if (err)
4861 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004862 err = avc_has_perm(&selinux_state,
4863 peer_sid, if_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004864 SECCLASS_NETIF, NETIF__INGRESS, ad);
4865 if (err)
4866 return err;
4867
4868 err = sel_netnode_sid(addrp, family, &node_sid);
4869 if (err)
4870 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004871 return avc_has_perm(&selinux_state,
4872 peer_sid, node_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004873 SECCLASS_NODE, NODE__RECVFROM, ad);
4874}
4875
Paul Moore220deb92008-01-29 08:38:23 -05004876static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
Paul Moored8395c82008-10-10 10:16:30 -04004877 u16 family)
Paul Moore220deb92008-01-29 08:38:23 -05004878{
Paul Moore277d3422008-12-31 12:54:11 -05004879 int err = 0;
Paul Moore220deb92008-01-29 08:38:23 -05004880 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004881 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004882 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004883 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04004884 char *addrp;
4885
Eric Paris50c205f2012-04-04 15:01:43 -04004886 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004887 ad.u.net = &net;
4888 ad.u.net->netif = skb->skb_iif;
4889 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004890 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4891 if (err)
4892 return err;
Paul Moore220deb92008-01-29 08:38:23 -05004893
Paul Moore58bfbb52009-03-27 17:10:41 -04004894 if (selinux_secmark_enabled()) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004895 err = avc_has_perm(&selinux_state,
4896 sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Moored8395c82008-10-10 10:16:30 -04004897 PACKET__RECV, &ad);
Paul Moore58bfbb52009-03-27 17:10:41 -04004898 if (err)
4899 return err;
4900 }
Paul Moore220deb92008-01-29 08:38:23 -05004901
Steffen Klassertb9679a72011-02-23 12:55:21 +01004902 err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
4903 if (err)
4904 return err;
4905 err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004906
James Morris4e5ab4c2006-06-09 00:33:33 -07004907 return err;
4908}
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004909
James Morris4e5ab4c2006-06-09 00:33:33 -07004910static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
4911{
Paul Moore220deb92008-01-29 08:38:23 -05004912 int err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004913 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004914 u16 family = sk->sk_family;
4915 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004916 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004917 struct lsm_network_audit net = {0,};
Paul Moore220deb92008-01-29 08:38:23 -05004918 char *addrp;
Paul Moored8395c82008-10-10 10:16:30 -04004919 u8 secmark_active;
4920 u8 peerlbl_active;
James Morris4e5ab4c2006-06-09 00:33:33 -07004921
James Morris4e5ab4c2006-06-09 00:33:33 -07004922 if (family != PF_INET && family != PF_INET6)
Paul Moore220deb92008-01-29 08:38:23 -05004923 return 0;
James Morris4e5ab4c2006-06-09 00:33:33 -07004924
4925 /* Handle mapped IPv4 packets arriving via IPv6 sockets */
Al Viro87fcd702006-12-04 22:00:55 +00004926 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
James Morris4e5ab4c2006-06-09 00:33:33 -07004927 family = PF_INET;
4928
Paul Moored8395c82008-10-10 10:16:30 -04004929 /* If any sort of compatibility mode is enabled then handoff processing
4930 * to the selinux_sock_rcv_skb_compat() function to deal with the
4931 * special handling. We do this in an attempt to keep this function
4932 * as fast and as clean as possible. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004933 if (!selinux_policycap_netpeer())
Paul Moored8395c82008-10-10 10:16:30 -04004934 return selinux_sock_rcv_skb_compat(sk, skb, family);
4935
4936 secmark_active = selinux_secmark_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04004937 peerlbl_active = selinux_peerlbl_enabled();
Paul Moored8395c82008-10-10 10:16:30 -04004938 if (!secmark_active && !peerlbl_active)
4939 return 0;
4940
Eric Paris50c205f2012-04-04 15:01:43 -04004941 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004942 ad.u.net = &net;
4943 ad.u.net->netif = skb->skb_iif;
4944 ad.u.net->family = family;
Paul Moore224dfbd2008-01-29 08:38:13 -05004945 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
James Morris4e5ab4c2006-06-09 00:33:33 -07004946 if (err)
Paul Moore220deb92008-01-29 08:38:23 -05004947 return err;
James Morris4e5ab4c2006-06-09 00:33:33 -07004948
Paul Moored8395c82008-10-10 10:16:30 -04004949 if (peerlbl_active) {
Paul Moored621d352008-01-29 08:43:36 -05004950 u32 peer_sid;
4951
4952 err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
4953 if (err)
4954 return err;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004955 err = selinux_inet_sys_rcv_skb(sock_net(sk), skb->skb_iif,
4956 addrp, family, peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04004957 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04004958 selinux_netlbl_err(skb, family, err, 0);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004959 return err;
Paul Mooredfaebe92008-10-10 10:16:31 -04004960 }
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004961 err = avc_has_perm(&selinux_state,
4962 sk_sid, peer_sid, SECCLASS_PEER,
Paul Moored621d352008-01-29 08:43:36 -05004963 PEER__RECV, &ad);
Chad Hanson46d01d62013-12-23 17:45:01 -05004964 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04004965 selinux_netlbl_err(skb, family, err, 0);
Chad Hanson46d01d62013-12-23 17:45:01 -05004966 return err;
4967 }
Paul Moored621d352008-01-29 08:43:36 -05004968 }
4969
Paul Moored8395c82008-10-10 10:16:30 -04004970 if (secmark_active) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004971 err = avc_has_perm(&selinux_state,
4972 sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004973 PACKET__RECV, &ad);
4974 if (err)
4975 return err;
4976 }
4977
Paul Moored621d352008-01-29 08:43:36 -05004978 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979}
4980
Catherine Zhang2c7946a2006-03-20 22:41:23 -08004981static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
4982 int __user *optlen, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983{
4984 int err = 0;
4985 char *scontext;
4986 u32 scontext_len;
Paul Moore253bfae2010-04-22 14:46:19 -04004987 struct sk_security_struct *sksec = sock->sk->sk_security;
Paul Moore3de4bab2006-11-17 17:38:54 -05004988 u32 peer_sid = SECSID_NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989
Paul Moore253bfae2010-04-22 14:46:19 -04004990 if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
Richard Hainesd4529302018-02-13 20:57:18 +00004991 sksec->sclass == SECCLASS_TCP_SOCKET ||
4992 sksec->sclass == SECCLASS_SCTP_SOCKET)
Eric Parisdd3e7832010-04-07 15:08:46 -04004993 peer_sid = sksec->peer_sid;
Paul Moore253bfae2010-04-22 14:46:19 -04004994 if (peer_sid == SECSID_NULL)
4995 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004997 err = security_sid_to_context(&selinux_state, peer_sid, &scontext,
4998 &scontext_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 if (err)
Paul Moore253bfae2010-04-22 14:46:19 -04005000 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001
5002 if (scontext_len > len) {
5003 err = -ERANGE;
5004 goto out_len;
5005 }
5006
5007 if (copy_to_user(optval, scontext, scontext_len))
5008 err = -EFAULT;
5009
5010out_len:
5011 if (put_user(scontext_len, optlen))
5012 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013 kfree(scontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 return err;
5015}
5016
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005017static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
Catherine Zhang2c7946a2006-03-20 22:41:23 -08005018{
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005019 u32 peer_secid = SECSID_NULL;
Paul Moore75e22912008-01-29 08:38:04 -05005020 u16 family;
Paul Moore899134f2016-03-28 15:19:10 -04005021 struct inode_security_struct *isec;
Catherine Zhang877ce7c2006-06-29 12:27:47 -07005022
Paul Mooreaa862902008-10-10 10:16:29 -04005023 if (skb && skb->protocol == htons(ETH_P_IP))
5024 family = PF_INET;
5025 else if (skb && skb->protocol == htons(ETH_P_IPV6))
5026 family = PF_INET6;
5027 else if (sock)
Paul Moore75e22912008-01-29 08:38:04 -05005028 family = sock->sk->sk_family;
Paul Moore75e22912008-01-29 08:38:04 -05005029 else
5030 goto out;
5031
Paul Moore899134f2016-03-28 15:19:10 -04005032 if (sock && family == PF_UNIX) {
5033 isec = inode_security_novalidate(SOCK_INODE(sock));
5034 peer_secid = isec->sid;
5035 } else if (skb)
Paul Moore220deb92008-01-29 08:38:23 -05005036 selinux_skb_peerlbl_sid(skb, family, &peer_secid);
Catherine Zhang2c7946a2006-03-20 22:41:23 -08005037
Paul Moore75e22912008-01-29 08:38:04 -05005038out:
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005039 *secid = peer_secid;
Paul Moore75e22912008-01-29 08:38:04 -05005040 if (peer_secid == SECSID_NULL)
5041 return -EINVAL;
5042 return 0;
Catherine Zhang2c7946a2006-03-20 22:41:23 -08005043}
5044
Al Viro7d877f32005-10-21 03:20:43 -04005045static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046{
Paul Moore84914b72010-04-22 14:46:18 -04005047 struct sk_security_struct *sksec;
5048
5049 sksec = kzalloc(sizeof(*sksec), priority);
5050 if (!sksec)
5051 return -ENOMEM;
5052
5053 sksec->peer_sid = SECINITSID_UNLABELED;
5054 sksec->sid = SECINITSID_UNLABELED;
Stephen Smalley5dee25d2015-07-10 17:19:57 -04005055 sksec->sclass = SECCLASS_SOCKET;
Paul Moore84914b72010-04-22 14:46:18 -04005056 selinux_netlbl_sk_security_reset(sksec);
5057 sk->sk_security = sksec;
5058
5059 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060}
5061
5062static void selinux_sk_free_security(struct sock *sk)
5063{
Paul Moore84914b72010-04-22 14:46:18 -04005064 struct sk_security_struct *sksec = sk->sk_security;
5065
5066 sk->sk_security = NULL;
5067 selinux_netlbl_sk_security_free(sksec);
5068 kfree(sksec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069}
5070
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005071static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
5072{
Eric Parisdd3e7832010-04-07 15:08:46 -04005073 struct sk_security_struct *sksec = sk->sk_security;
5074 struct sk_security_struct *newsksec = newsk->sk_security;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005075
Eric Parisdd3e7832010-04-07 15:08:46 -04005076 newsksec->sid = sksec->sid;
5077 newsksec->peer_sid = sksec->peer_sid;
5078 newsksec->sclass = sksec->sclass;
Paul Moore99f59ed2006-08-29 17:53:48 -07005079
Eric Parisdd3e7832010-04-07 15:08:46 -04005080 selinux_netlbl_sk_security_reset(newsksec);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005081}
5082
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07005083static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005084{
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005085 if (!sk)
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07005086 *secid = SECINITSID_ANY_SOCKET;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005087 else {
5088 struct sk_security_struct *sksec = sk->sk_security;
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005089
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07005090 *secid = sksec->sid;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005091 }
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005092}
5093
Eric Paris828dfe12008-04-17 13:17:49 -04005094static void selinux_sock_graft(struct sock *sk, struct socket *parent)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005095{
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05005096 struct inode_security_struct *isec =
5097 inode_security_novalidate(SOCK_INODE(parent));
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005098 struct sk_security_struct *sksec = sk->sk_security;
5099
Paul Moore2873ead2014-07-28 10:42:48 -04005100 if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
5101 sk->sk_family == PF_UNIX)
David Woodhouse2148ccc2006-09-29 15:50:25 -07005102 isec->sid = sksec->sid;
Paul Moore220deb92008-01-29 08:38:23 -05005103 sksec->sclass = isec->sclass;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005104}
5105
Richard Hainesd4529302018-02-13 20:57:18 +00005106/* Called whenever SCTP receives an INIT chunk. This happens when an incoming
5107 * connect(2), sctp_connectx(3) or sctp_sendmsg(3) (with no association
5108 * already present).
5109 */
5110static int selinux_sctp_assoc_request(struct sctp_endpoint *ep,
5111 struct sk_buff *skb)
5112{
5113 struct sk_security_struct *sksec = ep->base.sk->sk_security;
5114 struct common_audit_data ad;
5115 struct lsm_network_audit net = {0,};
5116 u8 peerlbl_active;
5117 u32 peer_sid = SECINITSID_UNLABELED;
5118 u32 conn_sid;
5119 int err = 0;
5120
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005121 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005122 return 0;
5123
5124 peerlbl_active = selinux_peerlbl_enabled();
5125
5126 if (peerlbl_active) {
5127 /* This will return peer_sid = SECSID_NULL if there are
5128 * no peer labels, see security_net_peersid_resolve().
5129 */
5130 err = selinux_skb_peerlbl_sid(skb, ep->base.sk->sk_family,
5131 &peer_sid);
5132 if (err)
5133 return err;
5134
5135 if (peer_sid == SECSID_NULL)
5136 peer_sid = SECINITSID_UNLABELED;
5137 }
5138
5139 if (sksec->sctp_assoc_state == SCTP_ASSOC_UNSET) {
5140 sksec->sctp_assoc_state = SCTP_ASSOC_SET;
5141
5142 /* Here as first association on socket. As the peer SID
5143 * was allowed by peer recv (and the netif/node checks),
5144 * then it is approved by policy and used as the primary
5145 * peer SID for getpeercon(3).
5146 */
5147 sksec->peer_sid = peer_sid;
5148 } else if (sksec->peer_sid != peer_sid) {
5149 /* Other association peer SIDs are checked to enforce
5150 * consistency among the peer SIDs.
5151 */
5152 ad.type = LSM_AUDIT_DATA_NET;
5153 ad.u.net = &net;
5154 ad.u.net->sk = ep->base.sk;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005155 err = avc_has_perm(&selinux_state,
5156 sksec->peer_sid, peer_sid, sksec->sclass,
Richard Hainesd4529302018-02-13 20:57:18 +00005157 SCTP_SOCKET__ASSOCIATION, &ad);
5158 if (err)
5159 return err;
5160 }
5161
5162 /* Compute the MLS component for the connection and store
5163 * the information in ep. This will be used by SCTP TCP type
5164 * sockets and peeled off connections as they cause a new
5165 * socket to be generated. selinux_sctp_sk_clone() will then
5166 * plug this into the new socket.
5167 */
5168 err = selinux_conn_sid(sksec->sid, peer_sid, &conn_sid);
5169 if (err)
5170 return err;
5171
5172 ep->secid = conn_sid;
5173 ep->peer_secid = peer_sid;
5174
5175 /* Set any NetLabel labels including CIPSO/CALIPSO options. */
5176 return selinux_netlbl_sctp_assoc_request(ep, skb);
5177}
5178
5179/* Check if sctp IPv4/IPv6 addresses are valid for binding or connecting
5180 * based on their @optname.
5181 */
5182static int selinux_sctp_bind_connect(struct sock *sk, int optname,
5183 struct sockaddr *address,
5184 int addrlen)
5185{
5186 int len, err = 0, walk_size = 0;
5187 void *addr_buf;
5188 struct sockaddr *addr;
5189 struct socket *sock;
5190
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005191 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005192 return 0;
5193
5194 /* Process one or more addresses that may be IPv4 or IPv6 */
5195 sock = sk->sk_socket;
5196 addr_buf = address;
5197
5198 while (walk_size < addrlen) {
Ondrej Mosnacekc1383252018-11-13 16:16:08 +01005199 if (walk_size + sizeof(sa_family_t) > addrlen)
5200 return -EINVAL;
5201
Richard Hainesd4529302018-02-13 20:57:18 +00005202 addr = addr_buf;
5203 switch (addr->sa_family) {
Alexey Kodanev4152dc92018-05-11 20:15:13 +03005204 case AF_UNSPEC:
Richard Hainesd4529302018-02-13 20:57:18 +00005205 case AF_INET:
5206 len = sizeof(struct sockaddr_in);
5207 break;
5208 case AF_INET6:
5209 len = sizeof(struct sockaddr_in6);
5210 break;
5211 default:
Alexey Kodanev4152dc92018-05-11 20:15:13 +03005212 return -EINVAL;
Richard Hainesd4529302018-02-13 20:57:18 +00005213 }
5214
Xin Long292c9972019-03-09 00:07:34 +08005215 if (walk_size + len > addrlen)
5216 return -EINVAL;
5217
Richard Hainesd4529302018-02-13 20:57:18 +00005218 err = -EINVAL;
5219 switch (optname) {
5220 /* Bind checks */
5221 case SCTP_PRIMARY_ADDR:
5222 case SCTP_SET_PEER_PRIMARY_ADDR:
5223 case SCTP_SOCKOPT_BINDX_ADD:
5224 err = selinux_socket_bind(sock, addr, len);
5225 break;
5226 /* Connect checks */
5227 case SCTP_SOCKOPT_CONNECTX:
5228 case SCTP_PARAM_SET_PRIMARY:
5229 case SCTP_PARAM_ADD_IP:
5230 case SCTP_SENDMSG_CONNECT:
5231 err = selinux_socket_connect_helper(sock, addr, len);
5232 if (err)
5233 return err;
5234
5235 /* As selinux_sctp_bind_connect() is called by the
5236 * SCTP protocol layer, the socket is already locked,
5237 * therefore selinux_netlbl_socket_connect_locked() is
5238 * is called here. The situations handled are:
5239 * sctp_connectx(3), sctp_sendmsg(3), sendmsg(2),
5240 * whenever a new IP address is added or when a new
5241 * primary address is selected.
5242 * Note that an SCTP connect(2) call happens before
5243 * the SCTP protocol layer and is handled via
5244 * selinux_socket_connect().
5245 */
5246 err = selinux_netlbl_socket_connect_locked(sk, addr);
5247 break;
5248 }
5249
5250 if (err)
5251 return err;
5252
5253 addr_buf += len;
5254 walk_size += len;
5255 }
5256
5257 return 0;
5258}
5259
5260/* Called whenever a new socket is created by accept(2) or sctp_peeloff(3). */
5261static void selinux_sctp_sk_clone(struct sctp_endpoint *ep, struct sock *sk,
5262 struct sock *newsk)
5263{
5264 struct sk_security_struct *sksec = sk->sk_security;
5265 struct sk_security_struct *newsksec = newsk->sk_security;
5266
5267 /* If policy does not support SECCLASS_SCTP_SOCKET then call
5268 * the non-sctp clone version.
5269 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005270 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005271 return selinux_sk_clone_security(sk, newsk);
5272
5273 newsksec->sid = ep->secid;
5274 newsksec->peer_sid = ep->peer_secid;
5275 newsksec->sclass = sksec->sclass;
5276 selinux_netlbl_sctp_sk_clone(sk, newsk);
5277}
5278
Adrian Bunk9a673e52006-08-15 00:03:53 -07005279static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
5280 struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005281{
5282 struct sk_security_struct *sksec = sk->sk_security;
5283 int err;
Paul Moore0b1f24e2013-12-03 11:39:13 -05005284 u16 family = req->rsk_ops->family;
Paul Moore446b8022013-12-04 16:10:51 -05005285 u32 connsid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005286 u32 peersid;
5287
Paul Mooreaa862902008-10-10 10:16:29 -04005288 err = selinux_skb_peerlbl_sid(skb, family, &peersid);
Paul Moore220deb92008-01-29 08:38:23 -05005289 if (err)
5290 return err;
Paul Moore446b8022013-12-04 16:10:51 -05005291 err = selinux_conn_sid(sksec->sid, peersid, &connsid);
5292 if (err)
5293 return err;
5294 req->secid = connsid;
5295 req->peer_secid = peersid;
Venkat Yekkiralaa51c64f2006-07-27 22:01:34 -07005296
Paul Moore389fb8002009-03-27 17:10:34 -04005297 return selinux_netlbl_inet_conn_request(req, family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005298}
5299
Adrian Bunk9a673e52006-08-15 00:03:53 -07005300static void selinux_inet_csk_clone(struct sock *newsk,
5301 const struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005302{
5303 struct sk_security_struct *newsksec = newsk->sk_security;
5304
5305 newsksec->sid = req->secid;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005306 newsksec->peer_sid = req->peer_secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005307 /* NOTE: Ideally, we should also get the isec->sid for the
5308 new socket in sync, but we don't have the isec available yet.
5309 So we will wait until sock_graft to do it, by which
5310 time it will have been created and available. */
Paul Moore99f59ed2006-08-29 17:53:48 -07005311
Paul Moore9f2ad662006-11-17 17:38:53 -05005312 /* We don't need to take any sort of lock here as we are the only
5313 * thread with access to newsksec */
Paul Moore389fb8002009-03-27 17:10:34 -04005314 selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005315}
5316
Paul Moore014ab192008-10-10 10:16:33 -04005317static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005318{
Paul Mooreaa862902008-10-10 10:16:29 -04005319 u16 family = sk->sk_family;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005320 struct sk_security_struct *sksec = sk->sk_security;
5321
Paul Mooreaa862902008-10-10 10:16:29 -04005322 /* handle mapped IPv4 packets arriving via IPv6 sockets */
5323 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
5324 family = PF_INET;
5325
5326 selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005327}
5328
Eric Paris2606fd12010-10-13 16:24:41 -04005329static int selinux_secmark_relabel_packet(u32 sid)
5330{
5331 const struct task_security_struct *__tsec;
5332 u32 tsid;
5333
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07005334 __tsec = selinux_cred(current_cred());
Eric Paris2606fd12010-10-13 16:24:41 -04005335 tsid = __tsec->sid;
5336
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005337 return avc_has_perm(&selinux_state,
5338 tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO,
5339 NULL);
Eric Paris2606fd12010-10-13 16:24:41 -04005340}
5341
5342static void selinux_secmark_refcount_inc(void)
5343{
5344 atomic_inc(&selinux_secmark_refcount);
5345}
5346
5347static void selinux_secmark_refcount_dec(void)
5348{
5349 atomic_dec(&selinux_secmark_refcount);
5350}
5351
Adrian Bunk9a673e52006-08-15 00:03:53 -07005352static void selinux_req_classify_flow(const struct request_sock *req,
5353 struct flowi *fl)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005354{
David S. Miller1d28f422011-03-12 00:29:39 -05005355 fl->flowi_secid = req->secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005356}
5357
Paul Moore5dbbaf22013-01-14 07:12:19 +00005358static int selinux_tun_dev_alloc_security(void **security)
5359{
5360 struct tun_security_struct *tunsec;
5361
5362 tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL);
5363 if (!tunsec)
5364 return -ENOMEM;
5365 tunsec->sid = current_sid();
5366
5367 *security = tunsec;
5368 return 0;
5369}
5370
5371static void selinux_tun_dev_free_security(void *security)
5372{
5373 kfree(security);
5374}
5375
Paul Mooreed6d76e2009-08-28 18:12:49 -04005376static int selinux_tun_dev_create(void)
5377{
5378 u32 sid = current_sid();
5379
5380 /* we aren't taking into account the "sockcreate" SID since the socket
5381 * that is being created here is not a socket in the traditional sense,
5382 * instead it is a private sock, accessible only to the kernel, and
5383 * representing a wide range of network traffic spanning multiple
5384 * connections unlike traditional sockets - check the TUN driver to
5385 * get a better understanding of why this socket is special */
5386
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005387 return avc_has_perm(&selinux_state,
5388 sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005389 NULL);
5390}
5391
Paul Moore5dbbaf22013-01-14 07:12:19 +00005392static int selinux_tun_dev_attach_queue(void *security)
Paul Mooreed6d76e2009-08-28 18:12:49 -04005393{
Paul Moore5dbbaf22013-01-14 07:12:19 +00005394 struct tun_security_struct *tunsec = security;
5395
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005396 return avc_has_perm(&selinux_state,
5397 current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET,
Paul Moore5dbbaf22013-01-14 07:12:19 +00005398 TUN_SOCKET__ATTACH_QUEUE, NULL);
5399}
5400
5401static int selinux_tun_dev_attach(struct sock *sk, void *security)
5402{
5403 struct tun_security_struct *tunsec = security;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005404 struct sk_security_struct *sksec = sk->sk_security;
5405
5406 /* we don't currently perform any NetLabel based labeling here and it
5407 * isn't clear that we would want to do so anyway; while we could apply
5408 * labeling without the support of the TUN user the resulting labeled
5409 * traffic from the other end of the connection would almost certainly
5410 * cause confusion to the TUN user that had no idea network labeling
5411 * protocols were being used */
5412
Paul Moore5dbbaf22013-01-14 07:12:19 +00005413 sksec->sid = tunsec->sid;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005414 sksec->sclass = SECCLASS_TUN_SOCKET;
Paul Moore5dbbaf22013-01-14 07:12:19 +00005415
5416 return 0;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005417}
5418
Paul Moore5dbbaf22013-01-14 07:12:19 +00005419static int selinux_tun_dev_open(void *security)
Paul Mooreed6d76e2009-08-28 18:12:49 -04005420{
Paul Moore5dbbaf22013-01-14 07:12:19 +00005421 struct tun_security_struct *tunsec = security;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005422 u32 sid = current_sid();
5423 int err;
5424
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005425 err = avc_has_perm(&selinux_state,
5426 sid, tunsec->sid, SECCLASS_TUN_SOCKET,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005427 TUN_SOCKET__RELABELFROM, NULL);
5428 if (err)
5429 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005430 err = avc_has_perm(&selinux_state,
5431 sid, sid, SECCLASS_TUN_SOCKET,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005432 TUN_SOCKET__RELABELTO, NULL);
5433 if (err)
5434 return err;
Paul Moore5dbbaf22013-01-14 07:12:19 +00005435 tunsec->sid = sid;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005436
5437 return 0;
5438}
5439
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
5441{
5442 int err = 0;
5443 u32 perm;
5444 struct nlmsghdr *nlh;
Paul Moore253bfae2010-04-22 14:46:19 -04005445 struct sk_security_struct *sksec = sk->sk_security;
Eric Paris828dfe12008-04-17 13:17:49 -04005446
Hong zhi guo77954982013-03-27 06:49:35 +00005447 if (skb->len < NLMSG_HDRLEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448 err = -EINVAL;
5449 goto out;
5450 }
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -07005451 nlh = nlmsg_hdr(skb);
Eric Paris828dfe12008-04-17 13:17:49 -04005452
Paul Moore253bfae2010-04-22 14:46:19 -04005453 err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454 if (err) {
5455 if (err == -EINVAL) {
Vladis Dronov76319942015-12-24 11:09:41 -05005456 pr_warn_ratelimited("SELinux: unrecognized netlink"
5457 " message: protocol=%hu nlmsg_type=%hu sclass=%s"
5458 " pig=%d comm=%s\n",
Marek Milkoviccded3ff2015-06-04 16:22:16 -04005459 sk->sk_protocol, nlh->nlmsg_type,
Vladis Dronov76319942015-12-24 11:09:41 -05005460 secclass_map[sksec->sclass - 1].name,
5461 task_pid_nr(current), current->comm);
Paul Mooree5a5ca92018-03-01 17:38:30 -05005462 if (!enforcing_enabled(&selinux_state) ||
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005463 security_get_allow_unknown(&selinux_state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464 err = 0;
5465 }
5466
5467 /* Ignore */
5468 if (err == -ENOENT)
5469 err = 0;
5470 goto out;
5471 }
5472
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005473 err = sock_has_perm(sk, perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474out:
5475 return err;
5476}
5477
5478#ifdef CONFIG_NETFILTER
5479
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005480static unsigned int selinux_ip_forward(struct sk_buff *skb,
5481 const struct net_device *indev,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005482 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483{
Paul Mooredfaebe92008-10-10 10:16:31 -04005484 int err;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005485 char *addrp;
5486 u32 peer_sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04005487 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005488 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05005489 u8 secmark_active;
Paul Moore948bf852008-10-10 10:16:32 -04005490 u8 netlbl_active;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005491 u8 peerlbl_active;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005492
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005493 if (!selinux_policycap_netpeer())
Paul Mooreeffad8d2008-01-29 08:49:27 -05005494 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005495
Paul Mooreeffad8d2008-01-29 08:49:27 -05005496 secmark_active = selinux_secmark_enabled();
Paul Moore948bf852008-10-10 10:16:32 -04005497 netlbl_active = netlbl_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04005498 peerlbl_active = selinux_peerlbl_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05005499 if (!secmark_active && !peerlbl_active)
5500 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005501
Paul Moored8395c82008-10-10 10:16:30 -04005502 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
5503 return NF_DROP;
5504
Eric Paris50c205f2012-04-04 15:01:43 -04005505 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005506 ad.u.net = &net;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005507 ad.u.net->netif = indev->ifindex;
Eric Paris48c62af2012-04-02 13:15:44 -04005508 ad.u.net->family = family;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005509 if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
5510 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511
Paul Mooredfaebe92008-10-10 10:16:31 -04005512 if (peerlbl_active) {
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005513 err = selinux_inet_sys_rcv_skb(dev_net(indev), indev->ifindex,
5514 addrp, family, peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04005515 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04005516 selinux_netlbl_err(skb, family, err, 1);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005517 return NF_DROP;
Paul Mooredfaebe92008-10-10 10:16:31 -04005518 }
5519 }
Paul Mooreeffad8d2008-01-29 08:49:27 -05005520
5521 if (secmark_active)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005522 if (avc_has_perm(&selinux_state,
5523 peer_sid, skb->secmark,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005524 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
5525 return NF_DROP;
5526
Paul Moore948bf852008-10-10 10:16:32 -04005527 if (netlbl_active)
5528 /* we do this in the FORWARD path and not the POST_ROUTING
5529 * path because we want to make sure we apply the necessary
5530 * labeling before IPsec is applied so we can leverage AH
5531 * protection */
5532 if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
5533 return NF_DROP;
5534
Paul Mooreeffad8d2008-01-29 08:49:27 -05005535 return NF_ACCEPT;
5536}
5537
Eric W. Biederman06198b32015-09-18 14:33:06 -05005538static unsigned int selinux_ipv4_forward(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005539 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005540 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005541{
David S. Miller238e54c2015-04-03 20:32:56 -04005542 return selinux_ip_forward(skb, state->in, PF_INET);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005543}
5544
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005545#if IS_ENABLED(CONFIG_IPV6)
Eric W. Biederman06198b32015-09-18 14:33:06 -05005546static unsigned int selinux_ipv6_forward(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005547 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005548 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005549{
David S. Miller238e54c2015-04-03 20:32:56 -04005550 return selinux_ip_forward(skb, state->in, PF_INET6);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005551}
5552#endif /* IPV6 */
5553
Paul Moore948bf852008-10-10 10:16:32 -04005554static unsigned int selinux_ip_output(struct sk_buff *skb,
5555 u16 family)
5556{
Paul Moore47180062013-12-04 16:10:45 -05005557 struct sock *sk;
Paul Moore948bf852008-10-10 10:16:32 -04005558 u32 sid;
5559
5560 if (!netlbl_enabled())
5561 return NF_ACCEPT;
5562
5563 /* we do this in the LOCAL_OUT path and not the POST_ROUTING path
5564 * because we want to make sure we apply the necessary labeling
5565 * before IPsec is applied so we can leverage AH protection */
Paul Moore47180062013-12-04 16:10:45 -05005566 sk = skb->sk;
5567 if (sk) {
5568 struct sk_security_struct *sksec;
5569
Eric Dumazete446f9d2015-10-08 05:01:55 -07005570 if (sk_listener(sk))
Paul Moore47180062013-12-04 16:10:45 -05005571 /* if the socket is the listening state then this
5572 * packet is a SYN-ACK packet which means it needs to
5573 * be labeled based on the connection/request_sock and
5574 * not the parent socket. unfortunately, we can't
5575 * lookup the request_sock yet as it isn't queued on
5576 * the parent socket until after the SYN-ACK is sent.
5577 * the "solution" is to simply pass the packet as-is
5578 * as any IP option based labeling should be copied
5579 * from the initial connection request (in the IP
5580 * layer). it is far from ideal, but until we get a
5581 * security label in the packet itself this is the
5582 * best we can do. */
5583 return NF_ACCEPT;
5584
5585 /* standard practice, label using the parent socket */
5586 sksec = sk->sk_security;
Paul Moore948bf852008-10-10 10:16:32 -04005587 sid = sksec->sid;
5588 } else
5589 sid = SECINITSID_KERNEL;
5590 if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
5591 return NF_DROP;
5592
5593 return NF_ACCEPT;
5594}
5595
Eric W. Biederman06198b32015-09-18 14:33:06 -05005596static unsigned int selinux_ipv4_output(void *priv,
Paul Moore948bf852008-10-10 10:16:32 -04005597 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005598 const struct nf_hook_state *state)
Paul Moore948bf852008-10-10 10:16:32 -04005599{
5600 return selinux_ip_output(skb, PF_INET);
5601}
5602
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005603#if IS_ENABLED(CONFIG_IPV6)
Huw Davies2917f572016-06-27 15:06:15 -04005604static unsigned int selinux_ipv6_output(void *priv,
5605 struct sk_buff *skb,
5606 const struct nf_hook_state *state)
5607{
5608 return selinux_ip_output(skb, PF_INET6);
5609}
5610#endif /* IPV6 */
5611
Paul Mooreeffad8d2008-01-29 08:49:27 -05005612static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
5613 int ifindex,
Paul Moored8395c82008-10-10 10:16:30 -04005614 u16 family)
James Morris4e5ab4c2006-06-09 00:33:33 -07005615{
Eric Dumazet54abc682015-11-08 10:54:07 -08005616 struct sock *sk = skb_to_full_sk(skb);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005617 struct sk_security_struct *sksec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005618 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005619 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04005620 char *addrp;
5621 u8 proto;
James Morris4e5ab4c2006-06-09 00:33:33 -07005622
Paul Mooreeffad8d2008-01-29 08:49:27 -05005623 if (sk == NULL)
5624 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005625 sksec = sk->sk_security;
James Morris4e5ab4c2006-06-09 00:33:33 -07005626
Eric Paris50c205f2012-04-04 15:01:43 -04005627 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005628 ad.u.net = &net;
5629 ad.u.net->netif = ifindex;
5630 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04005631 if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
5632 return NF_DROP;
5633
Paul Moore58bfbb52009-03-27 17:10:41 -04005634 if (selinux_secmark_enabled())
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005635 if (avc_has_perm(&selinux_state,
5636 sksec->sid, skb->secmark,
Paul Moored8395c82008-10-10 10:16:30 -04005637 SECCLASS_PACKET, PACKET__SEND, &ad))
Eric Paris2fe66ec2010-11-23 06:28:08 +00005638 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07005639
Steffen Klassertb9679a72011-02-23 12:55:21 +01005640 if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
5641 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07005642
Paul Mooreeffad8d2008-01-29 08:49:27 -05005643 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644}
5645
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005646static unsigned int selinux_ip_postroute(struct sk_buff *skb,
5647 const struct net_device *outdev,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005648 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649{
Paul Mooreeffad8d2008-01-29 08:49:27 -05005650 u32 secmark_perm;
5651 u32 peer_sid;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005652 int ifindex = outdev->ifindex;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005653 struct sock *sk;
Thomas Liu2bf49692009-07-14 12:14:09 -04005654 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005655 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05005656 char *addrp;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005657 u8 secmark_active;
5658 u8 peerlbl_active;
5659
Paul Mooreeffad8d2008-01-29 08:49:27 -05005660 /* If any sort of compatibility mode is enabled then handoff processing
5661 * to the selinux_ip_postroute_compat() function to deal with the
5662 * special handling. We do this in an attempt to keep this function
5663 * as fast and as clean as possible. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005664 if (!selinux_policycap_netpeer())
Paul Moored8395c82008-10-10 10:16:30 -04005665 return selinux_ip_postroute_compat(skb, ifindex, family);
Paul Moorec0828e52013-12-10 14:58:01 -05005666
Paul Mooreeffad8d2008-01-29 08:49:27 -05005667 secmark_active = selinux_secmark_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04005668 peerlbl_active = selinux_peerlbl_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05005669 if (!secmark_active && !peerlbl_active)
5670 return NF_ACCEPT;
5671
Eric Dumazet54abc682015-11-08 10:54:07 -08005672 sk = skb_to_full_sk(skb);
Paul Moorec0828e52013-12-10 14:58:01 -05005673
Paul Mooreeffad8d2008-01-29 08:49:27 -05005674#ifdef CONFIG_XFRM
5675 /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
5676 * packet transformation so allow the packet to pass without any checks
5677 * since we'll have another chance to perform access control checks
5678 * when the packet is on it's final way out.
5679 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
Paul Moorec0828e52013-12-10 14:58:01 -05005680 * is NULL, in this case go ahead and apply access control.
5681 * NOTE: if this is a local socket (skb->sk != NULL) that is in the
5682 * TCP listening state we cannot wait until the XFRM processing
5683 * is done as we will miss out on the SA label if we do;
5684 * unfortunately, this means more work, but it is only once per
5685 * connection. */
5686 if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL &&
Eric Dumazete446f9d2015-10-08 05:01:55 -07005687 !(sk && sk_listener(sk)))
Paul Mooreeffad8d2008-01-29 08:49:27 -05005688 return NF_ACCEPT;
5689#endif
Paul Mooreeffad8d2008-01-29 08:49:27 -05005690
Paul Moored8395c82008-10-10 10:16:30 -04005691 if (sk == NULL) {
Paul Moore446b8022013-12-04 16:10:51 -05005692 /* Without an associated socket the packet is either coming
5693 * from the kernel or it is being forwarded; check the packet
5694 * to determine which and if the packet is being forwarded
5695 * query the packet directly to determine the security label. */
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005696 if (skb->skb_iif) {
5697 secmark_perm = PACKET__FORWARD_OUT;
Paul Moored8395c82008-10-10 10:16:30 -04005698 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005699 return NF_DROP;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005700 } else {
5701 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04005702 peer_sid = SECINITSID_KERNEL;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005703 }
Eric Dumazete446f9d2015-10-08 05:01:55 -07005704 } else if (sk_listener(sk)) {
Paul Moore446b8022013-12-04 16:10:51 -05005705 /* Locally generated packet but the associated socket is in the
5706 * listening state which means this is a SYN-ACK packet. In
5707 * this particular case the correct security label is assigned
5708 * to the connection/request_sock but unfortunately we can't
5709 * query the request_sock as it isn't queued on the parent
5710 * socket until after the SYN-ACK packet is sent; the only
5711 * viable choice is to regenerate the label like we do in
5712 * selinux_inet_conn_request(). See also selinux_ip_output()
5713 * for similar problems. */
5714 u32 skb_sid;
Eric Dumazete446f9d2015-10-08 05:01:55 -07005715 struct sk_security_struct *sksec;
5716
Eric Dumazete446f9d2015-10-08 05:01:55 -07005717 sksec = sk->sk_security;
Paul Moore446b8022013-12-04 16:10:51 -05005718 if (selinux_skb_peerlbl_sid(skb, family, &skb_sid))
5719 return NF_DROP;
Paul Moorec0828e52013-12-10 14:58:01 -05005720 /* At this point, if the returned skb peerlbl is SECSID_NULL
5721 * and the packet has been through at least one XFRM
5722 * transformation then we must be dealing with the "final"
5723 * form of labeled IPsec packet; since we've already applied
5724 * all of our access controls on this packet we can safely
5725 * pass the packet. */
5726 if (skb_sid == SECSID_NULL) {
5727 switch (family) {
5728 case PF_INET:
5729 if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
5730 return NF_ACCEPT;
5731 break;
5732 case PF_INET6:
5733 if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED)
5734 return NF_ACCEPT;
Paul Moorea7a91a12014-09-03 10:51:59 -04005735 break;
Paul Moorec0828e52013-12-10 14:58:01 -05005736 default:
5737 return NF_DROP_ERR(-ECONNREFUSED);
5738 }
5739 }
Paul Moore446b8022013-12-04 16:10:51 -05005740 if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid))
5741 return NF_DROP;
5742 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04005743 } else {
Paul Moore446b8022013-12-04 16:10:51 -05005744 /* Locally generated packet, fetch the security label from the
5745 * associated socket. */
Paul Mooreeffad8d2008-01-29 08:49:27 -05005746 struct sk_security_struct *sksec = sk->sk_security;
5747 peer_sid = sksec->sid;
5748 secmark_perm = PACKET__SEND;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005749 }
5750
Eric Paris50c205f2012-04-04 15:01:43 -04005751 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005752 ad.u.net = &net;
5753 ad.u.net->netif = ifindex;
5754 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04005755 if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
Eric Paris04f6d702010-11-23 06:28:02 +00005756 return NF_DROP;
Paul Moored8395c82008-10-10 10:16:30 -04005757
Paul Mooreeffad8d2008-01-29 08:49:27 -05005758 if (secmark_active)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005759 if (avc_has_perm(&selinux_state,
5760 peer_sid, skb->secmark,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005761 SECCLASS_PACKET, secmark_perm, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005762 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005763
5764 if (peerlbl_active) {
5765 u32 if_sid;
5766 u32 node_sid;
5767
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005768 if (sel_netif_sid(dev_net(outdev), ifindex, &if_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005769 return NF_DROP;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005770 if (avc_has_perm(&selinux_state,
5771 peer_sid, if_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005772 SECCLASS_NETIF, NETIF__EGRESS, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005773 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005774
5775 if (sel_netnode_sid(addrp, family, &node_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005776 return NF_DROP;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005777 if (avc_has_perm(&selinux_state,
5778 peer_sid, node_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005779 SECCLASS_NODE, NODE__SENDTO, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005780 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005781 }
5782
5783 return NF_ACCEPT;
5784}
5785
Eric W. Biederman06198b32015-09-18 14:33:06 -05005786static unsigned int selinux_ipv4_postroute(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005787 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005788 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005789{
David S. Miller238e54c2015-04-03 20:32:56 -04005790 return selinux_ip_postroute(skb, state->out, PF_INET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791}
5792
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005793#if IS_ENABLED(CONFIG_IPV6)
Eric W. Biederman06198b32015-09-18 14:33:06 -05005794static unsigned int selinux_ipv6_postroute(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005795 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005796 const struct nf_hook_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797{
David S. Miller238e54c2015-04-03 20:32:56 -04005798 return selinux_ip_postroute(skb, state->out, PF_INET6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800#endif /* IPV6 */
5801
5802#endif /* CONFIG_NETFILTER */
5803
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
5805{
Stephen Smalley941fc5b2009-10-01 14:48:23 -04005806 return selinux_nlmsg_perm(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005807}
5808
Casey Schauflerecd5f822018-11-20 11:55:02 -08005809static void ipc_init_security(struct ipc_security_struct *isec, u16 sclass)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005810{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811 isec->sclass = sclass;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005812 isec->sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005813}
5814
5815static int msg_msg_alloc_security(struct msg_msg *msg)
5816{
5817 struct msg_security_struct *msec;
5818
Casey Schauflerecd5f822018-11-20 11:55:02 -08005819 msec = selinux_msg_msg(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820 msec->sid = SECINITSID_UNLABELED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821
5822 return 0;
5823}
5824
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
Stephen Smalley6af963f2005-05-01 08:58:39 -07005826 u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005829 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005830 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831
Casey Schaufler7c653822018-09-21 17:19:45 -07005832 isec = selinux_ipc(ipc_perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833
Eric Paris50c205f2012-04-04 15:01:43 -04005834 ad.type = LSM_AUDIT_DATA_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835 ad.u.ipc_id = ipc_perms->key;
5836
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005837 return avc_has_perm(&selinux_state,
5838 sid, isec->sid, isec->sclass, perms, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839}
5840
5841static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
5842{
5843 return msg_msg_alloc_security(msg);
5844}
5845
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846/* message queue security operations */
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005847static int selinux_msg_queue_alloc_security(struct kern_ipc_perm *msq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005850 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005851 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005852 int rc;
5853
Casey Schauflerecd5f822018-11-20 11:55:02 -08005854 isec = selinux_ipc(msq);
5855 ipc_init_security(isec, SECCLASS_MSGQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856
Eric Paris50c205f2012-04-04 15:01:43 -04005857 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005858 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005860 rc = avc_has_perm(&selinux_state,
5861 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862 MSGQ__CREATE, &ad);
Casey Schauflerecd5f822018-11-20 11:55:02 -08005863 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864}
5865
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005866static int selinux_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005868 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005869 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005870 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005871
Casey Schaufler7c653822018-09-21 17:19:45 -07005872 isec = selinux_ipc(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873
Eric Paris50c205f2012-04-04 15:01:43 -04005874 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005875 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005877 return avc_has_perm(&selinux_state,
5878 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005879 MSGQ__ASSOCIATE, &ad);
5880}
5881
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005882static int selinux_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005883{
5884 int err;
5885 int perms;
5886
Eric Paris828dfe12008-04-17 13:17:49 -04005887 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888 case IPC_INFO:
5889 case MSG_INFO:
5890 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005891 return avc_has_perm(&selinux_state,
5892 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005893 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894 case IPC_STAT:
5895 case MSG_STAT:
Davidlohr Bueso23c8cec2018-04-10 16:35:30 -07005896 case MSG_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005897 perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
5898 break;
5899 case IPC_SET:
5900 perms = MSGQ__SETATTR;
5901 break;
5902 case IPC_RMID:
5903 perms = MSGQ__DESTROY;
5904 break;
5905 default:
5906 return 0;
5907 }
5908
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005909 err = ipc_has_perm(msq, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910 return err;
5911}
5912
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005913static int selinux_msg_queue_msgsnd(struct kern_ipc_perm *msq, struct msg_msg *msg, int msqflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005915 struct ipc_security_struct *isec;
5916 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005917 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005918 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919 int rc;
5920
Casey Schaufler7c653822018-09-21 17:19:45 -07005921 isec = selinux_ipc(msq);
5922 msec = selinux_msg_msg(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923
5924 /*
5925 * First time through, need to assign label to the message
5926 */
5927 if (msec->sid == SECINITSID_UNLABELED) {
5928 /*
5929 * Compute new sid based on current process and
5930 * message queue this message will be stored in
5931 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005932 rc = security_transition_sid(&selinux_state, sid, isec->sid,
5933 SECCLASS_MSG, NULL, &msec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934 if (rc)
5935 return rc;
5936 }
5937
Eric Paris50c205f2012-04-04 15:01:43 -04005938 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005939 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940
5941 /* Can this process write to the queue? */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005942 rc = avc_has_perm(&selinux_state,
5943 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944 MSGQ__WRITE, &ad);
5945 if (!rc)
5946 /* Can this process send the message */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005947 rc = avc_has_perm(&selinux_state,
5948 sid, msec->sid, SECCLASS_MSG,
David Howells275bb412008-11-14 10:39:19 +11005949 MSG__SEND, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005950 if (!rc)
5951 /* Can the message be put in the queue? */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005952 rc = avc_has_perm(&selinux_state,
5953 msec->sid, isec->sid, SECCLASS_MSGQ,
David Howells275bb412008-11-14 10:39:19 +11005954 MSGQ__ENQUEUE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955
5956 return rc;
5957}
5958
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005959static int selinux_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005960 struct task_struct *target,
5961 long type, int mode)
5962{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005963 struct ipc_security_struct *isec;
5964 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005965 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005966 u32 sid = task_sid(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005967 int rc;
5968
Casey Schaufler7c653822018-09-21 17:19:45 -07005969 isec = selinux_ipc(msq);
5970 msec = selinux_msg_msg(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005971
Eric Paris50c205f2012-04-04 15:01:43 -04005972 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005973 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005974
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005975 rc = avc_has_perm(&selinux_state,
5976 sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977 SECCLASS_MSGQ, MSGQ__READ, &ad);
5978 if (!rc)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005979 rc = avc_has_perm(&selinux_state,
5980 sid, msec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005981 SECCLASS_MSG, MSG__RECEIVE, &ad);
5982 return rc;
5983}
5984
5985/* Shared Memory security operations */
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005986static int selinux_shm_alloc_security(struct kern_ipc_perm *shp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005988 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005989 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005990 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991 int rc;
5992
Casey Schauflerecd5f822018-11-20 11:55:02 -08005993 isec = selinux_ipc(shp);
5994 ipc_init_security(isec, SECCLASS_SHM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005995
Eric Paris50c205f2012-04-04 15:01:43 -04005996 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biederman7191adf2018-03-22 21:08:27 -05005997 ad.u.ipc_id = shp->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, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006001 SHM__CREATE, &ad);
Casey Schauflerecd5f822018-11-20 11:55:02 -08006002 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006003}
6004
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006005static int selinux_shm_associate(struct kern_ipc_perm *shp, int shmflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006006{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006007 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006008 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006009 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010
Casey Schaufler7c653822018-09-21 17:19:45 -07006011 isec = selinux_ipc(shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012
Eric Paris50c205f2012-04-04 15:01:43 -04006013 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006014 ad.u.ipc_id = shp->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006016 return avc_has_perm(&selinux_state,
6017 sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018 SHM__ASSOCIATE, &ad);
6019}
6020
6021/* Note, at this point, shp is locked down */
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006022static int selinux_shm_shmctl(struct kern_ipc_perm *shp, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006023{
6024 int perms;
6025 int err;
6026
Eric Paris828dfe12008-04-17 13:17:49 -04006027 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028 case IPC_INFO:
6029 case SHM_INFO:
6030 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006031 return avc_has_perm(&selinux_state,
6032 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006033 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034 case IPC_STAT:
6035 case SHM_STAT:
Davidlohr Buesoc21a6972018-04-10 16:35:23 -07006036 case SHM_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006037 perms = SHM__GETATTR | SHM__ASSOCIATE;
6038 break;
6039 case IPC_SET:
6040 perms = SHM__SETATTR;
6041 break;
6042 case SHM_LOCK:
6043 case SHM_UNLOCK:
6044 perms = SHM__LOCK;
6045 break;
6046 case IPC_RMID:
6047 perms = SHM__DESTROY;
6048 break;
6049 default:
6050 return 0;
6051 }
6052
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006053 err = ipc_has_perm(shp, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006054 return err;
6055}
6056
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006057static int selinux_shm_shmat(struct kern_ipc_perm *shp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058 char __user *shmaddr, int shmflg)
6059{
6060 u32 perms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006061
6062 if (shmflg & SHM_RDONLY)
6063 perms = SHM__READ;
6064 else
6065 perms = SHM__READ | SHM__WRITE;
6066
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006067 return ipc_has_perm(shp, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068}
6069
6070/* Semaphore security operations */
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006071static int selinux_sem_alloc_security(struct kern_ipc_perm *sma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006073 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006074 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006075 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076 int rc;
6077
Casey Schauflerecd5f822018-11-20 11:55:02 -08006078 isec = selinux_ipc(sma);
6079 ipc_init_security(isec, SECCLASS_SEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080
Eric Paris50c205f2012-04-04 15:01:43 -04006081 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006082 ad.u.ipc_id = sma->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006083
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006084 rc = avc_has_perm(&selinux_state,
6085 sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086 SEM__CREATE, &ad);
Casey Schauflerecd5f822018-11-20 11:55:02 -08006087 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088}
6089
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006090static int selinux_sem_associate(struct kern_ipc_perm *sma, int semflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006091{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006092 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006093 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006094 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095
Casey Schaufler7c653822018-09-21 17:19:45 -07006096 isec = selinux_ipc(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097
Eric Paris50c205f2012-04-04 15:01:43 -04006098 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006099 ad.u.ipc_id = sma->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006100
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006101 return avc_has_perm(&selinux_state,
6102 sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006103 SEM__ASSOCIATE, &ad);
6104}
6105
6106/* Note, at this point, sma is locked down */
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006107static int selinux_sem_semctl(struct kern_ipc_perm *sma, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006108{
6109 int err;
6110 u32 perms;
6111
Eric Paris828dfe12008-04-17 13:17:49 -04006112 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006113 case IPC_INFO:
6114 case SEM_INFO:
6115 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006116 return avc_has_perm(&selinux_state,
6117 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006118 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006119 case GETPID:
6120 case GETNCNT:
6121 case GETZCNT:
6122 perms = SEM__GETATTR;
6123 break;
6124 case GETVAL:
6125 case GETALL:
6126 perms = SEM__READ;
6127 break;
6128 case SETVAL:
6129 case SETALL:
6130 perms = SEM__WRITE;
6131 break;
6132 case IPC_RMID:
6133 perms = SEM__DESTROY;
6134 break;
6135 case IPC_SET:
6136 perms = SEM__SETATTR;
6137 break;
6138 case IPC_STAT:
6139 case SEM_STAT:
Davidlohr Buesoa280d6d2018-04-10 16:35:26 -07006140 case SEM_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006141 perms = SEM__GETATTR | SEM__ASSOCIATE;
6142 break;
6143 default:
6144 return 0;
6145 }
6146
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006147 err = ipc_has_perm(sma, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006148 return err;
6149}
6150
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006151static int selinux_sem_semop(struct kern_ipc_perm *sma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006152 struct sembuf *sops, unsigned nsops, int alter)
6153{
6154 u32 perms;
6155
6156 if (alter)
6157 perms = SEM__READ | SEM__WRITE;
6158 else
6159 perms = SEM__READ;
6160
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006161 return ipc_has_perm(sma, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006162}
6163
6164static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
6165{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006166 u32 av = 0;
6167
Linus Torvalds1da177e2005-04-16 15:20:36 -07006168 av = 0;
6169 if (flag & S_IRUGO)
6170 av |= IPC__UNIX_READ;
6171 if (flag & S_IWUGO)
6172 av |= IPC__UNIX_WRITE;
6173
6174 if (av == 0)
6175 return 0;
6176
Stephen Smalley6af963f2005-05-01 08:58:39 -07006177 return ipc_has_perm(ipcp, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006178}
6179
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02006180static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
6181{
Casey Schaufler7c653822018-09-21 17:19:45 -07006182 struct ipc_security_struct *isec = selinux_ipc(ipcp);
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02006183 *secid = isec->sid;
6184}
6185
Eric Paris828dfe12008-04-17 13:17:49 -04006186static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006187{
6188 if (inode)
6189 inode_doinit_with_dentry(inode, dentry);
6190}
6191
6192static int selinux_getprocattr(struct task_struct *p,
Al Viro04ff9702007-03-12 16:17:58 +00006193 char *name, char **value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006194{
David Howells275bb412008-11-14 10:39:19 +11006195 const struct task_security_struct *__tsec;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00006196 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006197 int error;
Al Viro04ff9702007-03-12 16:17:58 +00006198 unsigned len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006199
David Howells275bb412008-11-14 10:39:19 +11006200 rcu_read_lock();
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07006201 __tsec = selinux_cred(__task_cred(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006202
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006203 if (current != p) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006204 error = avc_has_perm(&selinux_state,
6205 current_sid(), __tsec->sid,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006206 SECCLASS_PROCESS, PROCESS__GETATTR, NULL);
6207 if (error)
6208 goto bad;
6209 }
6210
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211 if (!strcmp(name, "current"))
David Howells275bb412008-11-14 10:39:19 +11006212 sid = __tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006213 else if (!strcmp(name, "prev"))
David Howells275bb412008-11-14 10:39:19 +11006214 sid = __tsec->osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006215 else if (!strcmp(name, "exec"))
David Howells275bb412008-11-14 10:39:19 +11006216 sid = __tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217 else if (!strcmp(name, "fscreate"))
David Howells275bb412008-11-14 10:39:19 +11006218 sid = __tsec->create_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006219 else if (!strcmp(name, "keycreate"))
David Howells275bb412008-11-14 10:39:19 +11006220 sid = __tsec->keycreate_sid;
Eric Paris42c3e032006-06-26 00:26:03 -07006221 else if (!strcmp(name, "sockcreate"))
David Howells275bb412008-11-14 10:39:19 +11006222 sid = __tsec->sockcreate_sid;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006223 else {
6224 error = -EINVAL;
6225 goto bad;
6226 }
David Howells275bb412008-11-14 10:39:19 +11006227 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006228
6229 if (!sid)
6230 return 0;
6231
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006232 error = security_sid_to_context(&selinux_state, sid, value, &len);
Al Viro04ff9702007-03-12 16:17:58 +00006233 if (error)
6234 return error;
6235 return len;
David Howells275bb412008-11-14 10:39:19 +11006236
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006237bad:
David Howells275bb412008-11-14 10:39:19 +11006238 rcu_read_unlock();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006239 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006240}
6241
Stephen Smalleyb21507e2017-01-09 10:07:31 -05006242static int selinux_setprocattr(const char *name, void *value, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243{
6244 struct task_security_struct *tsec;
David Howellsd84f4f92008-11-14 10:39:23 +11006245 struct cred *new;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006246 u32 mysid = current_sid(), sid = 0, ptsid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006247 int error;
6248 char *str = value;
6249
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250 /*
6251 * Basic control over ability to set these attributes at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006252 */
6253 if (!strcmp(name, "exec"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006254 error = avc_has_perm(&selinux_state,
6255 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006256 PROCESS__SETEXEC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006257 else if (!strcmp(name, "fscreate"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006258 error = avc_has_perm(&selinux_state,
6259 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006260 PROCESS__SETFSCREATE, NULL);
Michael LeMay4eb582c2006-06-26 00:24:57 -07006261 else if (!strcmp(name, "keycreate"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006262 error = avc_has_perm(&selinux_state,
6263 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006264 PROCESS__SETKEYCREATE, NULL);
Eric Paris42c3e032006-06-26 00:26:03 -07006265 else if (!strcmp(name, "sockcreate"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006266 error = avc_has_perm(&selinux_state,
6267 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006268 PROCESS__SETSOCKCREATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006269 else if (!strcmp(name, "current"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006270 error = avc_has_perm(&selinux_state,
6271 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006272 PROCESS__SETCURRENT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006273 else
6274 error = -EINVAL;
6275 if (error)
6276 return error;
6277
6278 /* Obtain a SID for the context, if one was specified. */
Stephen Smalleya050a572017-01-31 11:54:04 -05006279 if (size && str[0] && str[0] != '\n') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006280 if (str[size-1] == '\n') {
6281 str[size-1] = 0;
6282 size--;
6283 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006284 error = security_context_to_sid(&selinux_state, value, size,
6285 &sid, GFP_KERNEL);
Stephen Smalley12b29f32008-05-07 13:03:20 -04006286 if (error == -EINVAL && !strcmp(name, "fscreate")) {
Stephen Smalleydb590002017-04-20 11:31:30 -04006287 if (!has_cap_mac_admin(true)) {
Eric Parisd6ea83e2012-04-04 13:45:49 -04006288 struct audit_buffer *ab;
6289 size_t audit_size;
6290
6291 /* We strip a nul only if it is at the end, otherwise the
6292 * context contains a nul and we should audit that */
6293 if (str[size - 1] == '\0')
6294 audit_size = size - 1;
6295 else
6296 audit_size = size;
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04006297 ab = audit_log_start(audit_context(),
6298 GFP_ATOMIC,
6299 AUDIT_SELINUX_ERR);
Eric Parisd6ea83e2012-04-04 13:45:49 -04006300 audit_log_format(ab, "op=fscreate invalid_context=");
6301 audit_log_n_untrustedstring(ab, value, audit_size);
6302 audit_log_end(ab);
6303
Stephen Smalley12b29f32008-05-07 13:03:20 -04006304 return error;
Eric Parisd6ea83e2012-04-04 13:45:49 -04006305 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006306 error = security_context_to_sid_force(
6307 &selinux_state,
6308 value, size, &sid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04006309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006310 if (error)
6311 return error;
6312 }
6313
David Howellsd84f4f92008-11-14 10:39:23 +11006314 new = prepare_creds();
6315 if (!new)
6316 return -ENOMEM;
6317
Linus Torvalds1da177e2005-04-16 15:20:36 -07006318 /* Permission checking based on the specified context is
6319 performed during the actual operation (execve,
6320 open/mkdir/...), when we know the full context of the
David Howellsd84f4f92008-11-14 10:39:23 +11006321 operation. See selinux_bprm_set_creds for the execve
Linus Torvalds1da177e2005-04-16 15:20:36 -07006322 checks and may_create for the file creation checks. The
6323 operation will then fail if the context is not permitted. */
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07006324 tsec = selinux_cred(new);
David Howellsd84f4f92008-11-14 10:39:23 +11006325 if (!strcmp(name, "exec")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006326 tsec->exec_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006327 } else if (!strcmp(name, "fscreate")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006328 tsec->create_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006329 } else if (!strcmp(name, "keycreate")) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006330 error = avc_has_perm(&selinux_state,
6331 mysid, sid, SECCLASS_KEY, KEY__CREATE,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006332 NULL);
Michael LeMay4eb582c2006-06-26 00:24:57 -07006333 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11006334 goto abort_change;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006335 tsec->keycreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006336 } else if (!strcmp(name, "sockcreate")) {
Eric Paris42c3e032006-06-26 00:26:03 -07006337 tsec->sockcreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006338 } else if (!strcmp(name, "current")) {
6339 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006340 if (sid == 0)
David Howellsd84f4f92008-11-14 10:39:23 +11006341 goto abort_change;
KaiGai Koheid9250de2008-08-28 16:35:57 +09006342
David Howellsd84f4f92008-11-14 10:39:23 +11006343 /* Only allow single threaded processes to change context */
6344 error = -EPERM;
Oleg Nesterov5bb459b2009-07-10 03:48:23 +02006345 if (!current_is_single_threaded()) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006346 error = security_bounded_transition(&selinux_state,
6347 tsec->sid, sid);
David Howellsd84f4f92008-11-14 10:39:23 +11006348 if (error)
6349 goto abort_change;
Eric Paris828dfe12008-04-17 13:17:49 -04006350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006351
6352 /* Check permissions for the transition. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006353 error = avc_has_perm(&selinux_state,
6354 tsec->sid, sid, SECCLASS_PROCESS,
Eric Paris828dfe12008-04-17 13:17:49 -04006355 PROCESS__DYNTRANSITION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006356 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11006357 goto abort_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006358
6359 /* Check for ptracing, and update the task SID if ok.
6360 Otherwise, leave SID unchanged and fail. */
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006361 ptsid = ptrace_parent_sid();
Paul Moore0c6181c2016-03-30 21:41:21 -04006362 if (ptsid != 0) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006363 error = avc_has_perm(&selinux_state,
6364 ptsid, sid, SECCLASS_PROCESS,
David Howellsd84f4f92008-11-14 10:39:23 +11006365 PROCESS__PTRACE, NULL);
6366 if (error)
6367 goto abort_change;
6368 }
6369
6370 tsec->sid = sid;
6371 } else {
6372 error = -EINVAL;
6373 goto abort_change;
6374 }
6375
6376 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377 return size;
David Howellsd84f4f92008-11-14 10:39:23 +11006378
6379abort_change:
6380 abort_creds(new);
6381 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006382}
6383
David Quigley746df9b2013-05-22 12:50:35 -04006384static int selinux_ismaclabel(const char *name)
6385{
6386 return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0);
6387}
6388
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006389static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
6390{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006391 return security_sid_to_context(&selinux_state, secid,
6392 secdata, seclen);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006393}
6394
David Howells7bf570d2008-04-29 20:52:51 +01006395static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
David Howells63cb3442008-01-15 23:47:35 +00006396{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006397 return security_context_to_sid(&selinux_state, secdata, seclen,
6398 secid, GFP_KERNEL);
David Howells63cb3442008-01-15 23:47:35 +00006399}
6400
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006401static void selinux_release_secctx(char *secdata, u32 seclen)
6402{
Paul Moore088999e2007-08-01 11:12:58 -04006403 kfree(secdata);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006404}
6405
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006406static void selinux_inode_invalidate_secctx(struct inode *inode)
6407{
Casey Schaufler80788c22018-09-21 17:19:11 -07006408 struct inode_security_struct *isec = selinux_inode(inode);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006409
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01006410 spin_lock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006411 isec->initialized = LABEL_INVALID;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01006412 spin_unlock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006413}
6414
David P. Quigley1ee65e32009-09-03 14:25:57 -04006415/*
6416 * called with inode->i_mutex locked
6417 */
6418static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
6419{
Ondrej Mosnacek53e0c2a2018-12-21 21:18:53 +01006420 int rc = selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX,
6421 ctx, ctxlen, 0);
6422 /* Do not return error when suppressing label (SBLABEL_MNT not set). */
6423 return rc == -EOPNOTSUPP ? 0 : rc;
David P. Quigley1ee65e32009-09-03 14:25:57 -04006424}
6425
6426/*
6427 * called with inode->i_mutex locked
6428 */
6429static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
6430{
6431 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0);
6432}
6433
6434static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
6435{
6436 int len = 0;
6437 len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX,
6438 ctx, true);
6439 if (len < 0)
6440 return len;
6441 *ctxlen = len;
6442 return 0;
6443}
Michael LeMayd7200242006-06-22 14:47:17 -07006444#ifdef CONFIG_KEYS
6445
David Howellsd84f4f92008-11-14 10:39:23 +11006446static int selinux_key_alloc(struct key *k, const struct cred *cred,
David Howells7e047ef2006-06-26 00:24:50 -07006447 unsigned long flags)
Michael LeMayd7200242006-06-22 14:47:17 -07006448{
David Howellsd84f4f92008-11-14 10:39:23 +11006449 const struct task_security_struct *tsec;
Michael LeMayd7200242006-06-22 14:47:17 -07006450 struct key_security_struct *ksec;
6451
6452 ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
6453 if (!ksec)
6454 return -ENOMEM;
6455
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07006456 tsec = selinux_cred(cred);
David Howellsd84f4f92008-11-14 10:39:23 +11006457 if (tsec->keycreate_sid)
6458 ksec->sid = tsec->keycreate_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006459 else
David Howellsd84f4f92008-11-14 10:39:23 +11006460 ksec->sid = tsec->sid;
Michael LeMayd7200242006-06-22 14:47:17 -07006461
David Howells275bb412008-11-14 10:39:19 +11006462 k->security = ksec;
Michael LeMayd7200242006-06-22 14:47:17 -07006463 return 0;
6464}
6465
6466static void selinux_key_free(struct key *k)
6467{
6468 struct key_security_struct *ksec = k->security;
6469
6470 k->security = NULL;
6471 kfree(ksec);
6472}
6473
6474static int selinux_key_permission(key_ref_t key_ref,
David Howellsd84f4f92008-11-14 10:39:23 +11006475 const struct cred *cred,
David Howellsf5895942014-03-14 17:44:49 +00006476 unsigned perm)
Michael LeMayd7200242006-06-22 14:47:17 -07006477{
6478 struct key *key;
Michael LeMayd7200242006-06-22 14:47:17 -07006479 struct key_security_struct *ksec;
David Howells275bb412008-11-14 10:39:19 +11006480 u32 sid;
Michael LeMayd7200242006-06-22 14:47:17 -07006481
6482 /* if no specific permissions are requested, we skip the
6483 permission check. No serious, additional covert channels
6484 appear to be created. */
6485 if (perm == 0)
6486 return 0;
6487
David Howellsd84f4f92008-11-14 10:39:23 +11006488 sid = cred_sid(cred);
David Howells275bb412008-11-14 10:39:19 +11006489
6490 key = key_ref_to_ptr(key_ref);
6491 ksec = key->security;
6492
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006493 return avc_has_perm(&selinux_state,
6494 sid, ksec->sid, SECCLASS_KEY, perm, NULL);
Michael LeMayd7200242006-06-22 14:47:17 -07006495}
6496
David Howells70a5bb72008-04-29 01:01:26 -07006497static int selinux_key_getsecurity(struct key *key, char **_buffer)
6498{
6499 struct key_security_struct *ksec = key->security;
6500 char *context = NULL;
6501 unsigned len;
6502 int rc;
6503
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006504 rc = security_sid_to_context(&selinux_state, ksec->sid,
6505 &context, &len);
David Howells70a5bb72008-04-29 01:01:26 -07006506 if (!rc)
6507 rc = len;
6508 *_buffer = context;
6509 return rc;
6510}
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006511#endif
David Howells70a5bb72008-04-29 01:01:26 -07006512
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006513#ifdef CONFIG_SECURITY_INFINIBAND
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006514static int selinux_ib_pkey_access(void *ib_sec, u64 subnet_prefix, u16 pkey_val)
6515{
6516 struct common_audit_data ad;
6517 int err;
6518 u32 sid = 0;
6519 struct ib_security_struct *sec = ib_sec;
6520 struct lsm_ibpkey_audit ibpkey;
6521
Daniel Jurgens409dcf32017-05-19 15:48:59 +03006522 err = sel_ib_pkey_sid(subnet_prefix, pkey_val, &sid);
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006523 if (err)
6524 return err;
6525
6526 ad.type = LSM_AUDIT_DATA_IBPKEY;
6527 ibpkey.subnet_prefix = subnet_prefix;
6528 ibpkey.pkey = pkey_val;
6529 ad.u.ibpkey = &ibpkey;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006530 return avc_has_perm(&selinux_state,
6531 sec->sid, sid,
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006532 SECCLASS_INFINIBAND_PKEY,
6533 INFINIBAND_PKEY__ACCESS, &ad);
6534}
6535
Daniel Jurgensab861df2017-05-19 15:48:58 +03006536static int selinux_ib_endport_manage_subnet(void *ib_sec, const char *dev_name,
6537 u8 port_num)
6538{
6539 struct common_audit_data ad;
6540 int err;
6541 u32 sid = 0;
6542 struct ib_security_struct *sec = ib_sec;
6543 struct lsm_ibendport_audit ibendport;
6544
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006545 err = security_ib_endport_sid(&selinux_state, dev_name, port_num,
6546 &sid);
Daniel Jurgensab861df2017-05-19 15:48:58 +03006547
6548 if (err)
6549 return err;
6550
6551 ad.type = LSM_AUDIT_DATA_IBENDPORT;
6552 strncpy(ibendport.dev_name, dev_name, sizeof(ibendport.dev_name));
6553 ibendport.port = port_num;
6554 ad.u.ibendport = &ibendport;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006555 return avc_has_perm(&selinux_state,
6556 sec->sid, sid,
Daniel Jurgensab861df2017-05-19 15:48:58 +03006557 SECCLASS_INFINIBAND_ENDPORT,
6558 INFINIBAND_ENDPORT__MANAGE_SUBNET, &ad);
6559}
6560
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006561static int selinux_ib_alloc_security(void **ib_sec)
6562{
6563 struct ib_security_struct *sec;
6564
6565 sec = kzalloc(sizeof(*sec), GFP_KERNEL);
6566 if (!sec)
6567 return -ENOMEM;
6568 sec->sid = current_sid();
6569
6570 *ib_sec = sec;
6571 return 0;
6572}
6573
6574static void selinux_ib_free_security(void *ib_sec)
6575{
6576 kfree(ib_sec);
6577}
Michael LeMayd7200242006-06-22 14:47:17 -07006578#endif
6579
Chenbo Fengec27c352017-10-18 13:00:25 -07006580#ifdef CONFIG_BPF_SYSCALL
6581static int selinux_bpf(int cmd, union bpf_attr *attr,
6582 unsigned int size)
6583{
6584 u32 sid = current_sid();
6585 int ret;
6586
6587 switch (cmd) {
6588 case BPF_MAP_CREATE:
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006589 ret = avc_has_perm(&selinux_state,
6590 sid, sid, SECCLASS_BPF, BPF__MAP_CREATE,
Chenbo Fengec27c352017-10-18 13:00:25 -07006591 NULL);
6592 break;
6593 case BPF_PROG_LOAD:
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006594 ret = avc_has_perm(&selinux_state,
6595 sid, sid, SECCLASS_BPF, BPF__PROG_LOAD,
Chenbo Fengec27c352017-10-18 13:00:25 -07006596 NULL);
6597 break;
6598 default:
6599 ret = 0;
6600 break;
6601 }
6602
6603 return ret;
6604}
6605
6606static u32 bpf_map_fmode_to_av(fmode_t fmode)
6607{
6608 u32 av = 0;
6609
6610 if (fmode & FMODE_READ)
6611 av |= BPF__MAP_READ;
6612 if (fmode & FMODE_WRITE)
6613 av |= BPF__MAP_WRITE;
6614 return av;
6615}
6616
Chenbo Fengf66e4482017-10-18 13:00:26 -07006617/* This function will check the file pass through unix socket or binder to see
6618 * if it is a bpf related object. And apply correspinding checks on the bpf
6619 * object based on the type. The bpf maps and programs, not like other files and
6620 * socket, are using a shared anonymous inode inside the kernel as their inode.
6621 * So checking that inode cannot identify if the process have privilege to
6622 * access the bpf object and that's why we have to add this additional check in
6623 * selinux_file_receive and selinux_binder_transfer_files.
6624 */
6625static int bpf_fd_pass(struct file *file, u32 sid)
6626{
6627 struct bpf_security_struct *bpfsec;
6628 struct bpf_prog *prog;
6629 struct bpf_map *map;
6630 int ret;
6631
6632 if (file->f_op == &bpf_map_fops) {
6633 map = file->private_data;
6634 bpfsec = map->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006635 ret = avc_has_perm(&selinux_state,
6636 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengf66e4482017-10-18 13:00:26 -07006637 bpf_map_fmode_to_av(file->f_mode), NULL);
6638 if (ret)
6639 return ret;
6640 } else if (file->f_op == &bpf_prog_fops) {
6641 prog = file->private_data;
6642 bpfsec = prog->aux->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006643 ret = avc_has_perm(&selinux_state,
6644 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengf66e4482017-10-18 13:00:26 -07006645 BPF__PROG_RUN, NULL);
6646 if (ret)
6647 return ret;
6648 }
6649 return 0;
6650}
6651
Chenbo Fengec27c352017-10-18 13:00:25 -07006652static int selinux_bpf_map(struct bpf_map *map, fmode_t fmode)
6653{
6654 u32 sid = current_sid();
6655 struct bpf_security_struct *bpfsec;
6656
6657 bpfsec = map->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006658 return avc_has_perm(&selinux_state,
6659 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengec27c352017-10-18 13:00:25 -07006660 bpf_map_fmode_to_av(fmode), NULL);
6661}
6662
6663static int selinux_bpf_prog(struct bpf_prog *prog)
6664{
6665 u32 sid = current_sid();
6666 struct bpf_security_struct *bpfsec;
6667
6668 bpfsec = prog->aux->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006669 return avc_has_perm(&selinux_state,
6670 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengec27c352017-10-18 13:00:25 -07006671 BPF__PROG_RUN, NULL);
6672}
6673
6674static int selinux_bpf_map_alloc(struct bpf_map *map)
6675{
6676 struct bpf_security_struct *bpfsec;
6677
6678 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6679 if (!bpfsec)
6680 return -ENOMEM;
6681
6682 bpfsec->sid = current_sid();
6683 map->security = bpfsec;
6684
6685 return 0;
6686}
6687
6688static void selinux_bpf_map_free(struct bpf_map *map)
6689{
6690 struct bpf_security_struct *bpfsec = map->security;
6691
6692 map->security = NULL;
6693 kfree(bpfsec);
6694}
6695
6696static int selinux_bpf_prog_alloc(struct bpf_prog_aux *aux)
6697{
6698 struct bpf_security_struct *bpfsec;
6699
6700 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6701 if (!bpfsec)
6702 return -ENOMEM;
6703
6704 bpfsec->sid = current_sid();
6705 aux->security = bpfsec;
6706
6707 return 0;
6708}
6709
6710static void selinux_bpf_prog_free(struct bpf_prog_aux *aux)
6711{
6712 struct bpf_security_struct *bpfsec = aux->security;
6713
6714 aux->security = NULL;
6715 kfree(bpfsec);
6716}
6717#endif
6718
Casey Schauflerbbd36622018-11-12 09:30:56 -08006719struct lsm_blob_sizes selinux_blob_sizes __lsm_ro_after_init = {
6720 .lbs_cred = sizeof(struct task_security_struct),
Casey Schaufler33bf60c2018-11-12 12:02:49 -08006721 .lbs_file = sizeof(struct file_security_struct),
Casey Schauflerafb1cbe32018-09-21 17:19:29 -07006722 .lbs_inode = sizeof(struct inode_security_struct),
Casey Schauflerecd5f822018-11-20 11:55:02 -08006723 .lbs_ipc = sizeof(struct ipc_security_struct),
6724 .lbs_msg_msg = sizeof(struct msg_security_struct),
Casey Schauflerbbd36622018-11-12 09:30:56 -08006725};
6726
James Morrisca97d932017-02-15 00:18:51 +11006727static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {
Casey Schauflere20b0432015-05-02 15:11:36 -07006728 LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr),
6729 LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction),
6730 LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder),
6731 LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file),
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02006732
Casey Schauflere20b0432015-05-02 15:11:36 -07006733 LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check),
6734 LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme),
6735 LSM_HOOK_INIT(capget, selinux_capget),
6736 LSM_HOOK_INIT(capset, selinux_capset),
6737 LSM_HOOK_INIT(capable, selinux_capable),
6738 LSM_HOOK_INIT(quotactl, selinux_quotactl),
6739 LSM_HOOK_INIT(quota_on, selinux_quota_on),
6740 LSM_HOOK_INIT(syslog, selinux_syslog),
6741 LSM_HOOK_INIT(vm_enough_memory, selinux_vm_enough_memory),
Stephen Smalley79af7302015-01-21 10:54:10 -05006742
Casey Schauflere20b0432015-05-02 15:11:36 -07006743 LSM_HOOK_INIT(netlink_send, selinux_netlink_send),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006744
Casey Schauflere20b0432015-05-02 15:11:36 -07006745 LSM_HOOK_INIT(bprm_set_creds, selinux_bprm_set_creds),
6746 LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds),
6747 LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006748
Al Viro0b520752018-12-23 16:02:47 -05006749 LSM_HOOK_INIT(fs_context_dup, selinux_fs_context_dup),
David Howells442155c2018-11-01 23:07:24 +00006750 LSM_HOOK_INIT(fs_context_parse_param, selinux_fs_context_parse_param),
6751
Casey Schauflere20b0432015-05-02 15:11:36 -07006752 LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security),
6753 LSM_HOOK_INIT(sb_free_security, selinux_sb_free_security),
Al Viro5b400232018-12-12 20:13:29 -05006754 LSM_HOOK_INIT(sb_eat_lsm_opts, selinux_sb_eat_lsm_opts),
Al Viro204cc0c2018-12-13 13:41:47 -05006755 LSM_HOOK_INIT(sb_free_mnt_opts, selinux_free_mnt_opts),
Casey Schauflere20b0432015-05-02 15:11:36 -07006756 LSM_HOOK_INIT(sb_remount, selinux_sb_remount),
6757 LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount),
6758 LSM_HOOK_INIT(sb_show_options, selinux_sb_show_options),
6759 LSM_HOOK_INIT(sb_statfs, selinux_sb_statfs),
6760 LSM_HOOK_INIT(sb_mount, selinux_mount),
6761 LSM_HOOK_INIT(sb_umount, selinux_umount),
6762 LSM_HOOK_INIT(sb_set_mnt_opts, selinux_set_mnt_opts),
6763 LSM_HOOK_INIT(sb_clone_mnt_opts, selinux_sb_clone_mnt_opts),
Al Viro757cbe52018-12-14 23:42:21 -05006764 LSM_HOOK_INIT(sb_add_mnt_opt, selinux_add_mnt_opt),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006765
Casey Schauflere20b0432015-05-02 15:11:36 -07006766 LSM_HOOK_INIT(dentry_init_security, selinux_dentry_init_security),
Vivek Goyala518b0a2016-07-13 10:44:53 -04006767 LSM_HOOK_INIT(dentry_create_files_as, selinux_dentry_create_files_as),
Eric Parise0007522008-03-05 10:31:54 -05006768
Casey Schauflere20b0432015-05-02 15:11:36 -07006769 LSM_HOOK_INIT(inode_alloc_security, selinux_inode_alloc_security),
6770 LSM_HOOK_INIT(inode_free_security, selinux_inode_free_security),
6771 LSM_HOOK_INIT(inode_init_security, selinux_inode_init_security),
6772 LSM_HOOK_INIT(inode_create, selinux_inode_create),
6773 LSM_HOOK_INIT(inode_link, selinux_inode_link),
6774 LSM_HOOK_INIT(inode_unlink, selinux_inode_unlink),
6775 LSM_HOOK_INIT(inode_symlink, selinux_inode_symlink),
6776 LSM_HOOK_INIT(inode_mkdir, selinux_inode_mkdir),
6777 LSM_HOOK_INIT(inode_rmdir, selinux_inode_rmdir),
6778 LSM_HOOK_INIT(inode_mknod, selinux_inode_mknod),
6779 LSM_HOOK_INIT(inode_rename, selinux_inode_rename),
6780 LSM_HOOK_INIT(inode_readlink, selinux_inode_readlink),
6781 LSM_HOOK_INIT(inode_follow_link, selinux_inode_follow_link),
6782 LSM_HOOK_INIT(inode_permission, selinux_inode_permission),
6783 LSM_HOOK_INIT(inode_setattr, selinux_inode_setattr),
6784 LSM_HOOK_INIT(inode_getattr, selinux_inode_getattr),
6785 LSM_HOOK_INIT(inode_setxattr, selinux_inode_setxattr),
6786 LSM_HOOK_INIT(inode_post_setxattr, selinux_inode_post_setxattr),
6787 LSM_HOOK_INIT(inode_getxattr, selinux_inode_getxattr),
6788 LSM_HOOK_INIT(inode_listxattr, selinux_inode_listxattr),
6789 LSM_HOOK_INIT(inode_removexattr, selinux_inode_removexattr),
6790 LSM_HOOK_INIT(inode_getsecurity, selinux_inode_getsecurity),
6791 LSM_HOOK_INIT(inode_setsecurity, selinux_inode_setsecurity),
6792 LSM_HOOK_INIT(inode_listsecurity, selinux_inode_listsecurity),
6793 LSM_HOOK_INIT(inode_getsecid, selinux_inode_getsecid),
Vivek Goyal56909eb2016-07-13 10:44:48 -04006794 LSM_HOOK_INIT(inode_copy_up, selinux_inode_copy_up),
Vivek Goyal19472b62016-07-13 10:44:50 -04006795 LSM_HOOK_INIT(inode_copy_up_xattr, selinux_inode_copy_up_xattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006796
Ondrej Mosnacekec882da2019-02-22 15:57:17 +01006797 LSM_HOOK_INIT(kernfs_init_security, selinux_kernfs_init_security),
6798
Casey Schauflere20b0432015-05-02 15:11:36 -07006799 LSM_HOOK_INIT(file_permission, selinux_file_permission),
6800 LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07006801 LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl),
6802 LSM_HOOK_INIT(mmap_file, selinux_mmap_file),
6803 LSM_HOOK_INIT(mmap_addr, selinux_mmap_addr),
6804 LSM_HOOK_INIT(file_mprotect, selinux_file_mprotect),
6805 LSM_HOOK_INIT(file_lock, selinux_file_lock),
6806 LSM_HOOK_INIT(file_fcntl, selinux_file_fcntl),
6807 LSM_HOOK_INIT(file_set_fowner, selinux_file_set_fowner),
6808 LSM_HOOK_INIT(file_send_sigiotask, selinux_file_send_sigiotask),
6809 LSM_HOOK_INIT(file_receive, selinux_file_receive),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006810
Casey Schauflere20b0432015-05-02 15:11:36 -07006811 LSM_HOOK_INIT(file_open, selinux_file_open),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006812
Tetsuo Handaa79be232017-03-28 23:08:45 +09006813 LSM_HOOK_INIT(task_alloc, selinux_task_alloc),
Casey Schauflere20b0432015-05-02 15:11:36 -07006814 LSM_HOOK_INIT(cred_prepare, selinux_cred_prepare),
6815 LSM_HOOK_INIT(cred_transfer, selinux_cred_transfer),
Matthew Garrett3ec30112018-01-08 13:36:19 -08006816 LSM_HOOK_INIT(cred_getsecid, selinux_cred_getsecid),
Casey Schauflere20b0432015-05-02 15:11:36 -07006817 LSM_HOOK_INIT(kernel_act_as, selinux_kernel_act_as),
6818 LSM_HOOK_INIT(kernel_create_files_as, selinux_kernel_create_files_as),
6819 LSM_HOOK_INIT(kernel_module_request, selinux_kernel_module_request),
Mimi Zoharc77b8cd2018-07-13 14:06:02 -04006820 LSM_HOOK_INIT(kernel_load_data, selinux_kernel_load_data),
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07006821 LSM_HOOK_INIT(kernel_read_file, selinux_kernel_read_file),
Casey Schauflere20b0432015-05-02 15:11:36 -07006822 LSM_HOOK_INIT(task_setpgid, selinux_task_setpgid),
6823 LSM_HOOK_INIT(task_getpgid, selinux_task_getpgid),
6824 LSM_HOOK_INIT(task_getsid, selinux_task_getsid),
6825 LSM_HOOK_INIT(task_getsecid, selinux_task_getsecid),
6826 LSM_HOOK_INIT(task_setnice, selinux_task_setnice),
6827 LSM_HOOK_INIT(task_setioprio, selinux_task_setioprio),
6828 LSM_HOOK_INIT(task_getioprio, selinux_task_getioprio),
Stephen Smalley791ec492017-02-17 07:57:00 -05006829 LSM_HOOK_INIT(task_prlimit, selinux_task_prlimit),
Casey Schauflere20b0432015-05-02 15:11:36 -07006830 LSM_HOOK_INIT(task_setrlimit, selinux_task_setrlimit),
6831 LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler),
6832 LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler),
6833 LSM_HOOK_INIT(task_movememory, selinux_task_movememory),
6834 LSM_HOOK_INIT(task_kill, selinux_task_kill),
Casey Schauflere20b0432015-05-02 15:11:36 -07006835 LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode),
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09006836
Casey Schauflere20b0432015-05-02 15:11:36 -07006837 LSM_HOOK_INIT(ipc_permission, selinux_ipc_permission),
6838 LSM_HOOK_INIT(ipc_getsecid, selinux_ipc_getsecid),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006839
Casey Schauflere20b0432015-05-02 15:11:36 -07006840 LSM_HOOK_INIT(msg_msg_alloc_security, selinux_msg_msg_alloc_security),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006841
Casey Schauflere20b0432015-05-02 15:11:36 -07006842 LSM_HOOK_INIT(msg_queue_alloc_security,
6843 selinux_msg_queue_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07006844 LSM_HOOK_INIT(msg_queue_associate, selinux_msg_queue_associate),
6845 LSM_HOOK_INIT(msg_queue_msgctl, selinux_msg_queue_msgctl),
6846 LSM_HOOK_INIT(msg_queue_msgsnd, selinux_msg_queue_msgsnd),
6847 LSM_HOOK_INIT(msg_queue_msgrcv, selinux_msg_queue_msgrcv),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006848
Casey Schauflere20b0432015-05-02 15:11:36 -07006849 LSM_HOOK_INIT(shm_alloc_security, selinux_shm_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07006850 LSM_HOOK_INIT(shm_associate, selinux_shm_associate),
6851 LSM_HOOK_INIT(shm_shmctl, selinux_shm_shmctl),
6852 LSM_HOOK_INIT(shm_shmat, selinux_shm_shmat),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006853
Casey Schauflere20b0432015-05-02 15:11:36 -07006854 LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07006855 LSM_HOOK_INIT(sem_associate, selinux_sem_associate),
6856 LSM_HOOK_INIT(sem_semctl, selinux_sem_semctl),
6857 LSM_HOOK_INIT(sem_semop, selinux_sem_semop),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006858
Casey Schauflere20b0432015-05-02 15:11:36 -07006859 LSM_HOOK_INIT(d_instantiate, selinux_d_instantiate),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006860
Casey Schauflere20b0432015-05-02 15:11:36 -07006861 LSM_HOOK_INIT(getprocattr, selinux_getprocattr),
6862 LSM_HOOK_INIT(setprocattr, selinux_setprocattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006863
Casey Schauflere20b0432015-05-02 15:11:36 -07006864 LSM_HOOK_INIT(ismaclabel, selinux_ismaclabel),
6865 LSM_HOOK_INIT(secid_to_secctx, selinux_secid_to_secctx),
6866 LSM_HOOK_INIT(secctx_to_secid, selinux_secctx_to_secid),
6867 LSM_HOOK_INIT(release_secctx, selinux_release_secctx),
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006868 LSM_HOOK_INIT(inode_invalidate_secctx, selinux_inode_invalidate_secctx),
Casey Schauflere20b0432015-05-02 15:11:36 -07006869 LSM_HOOK_INIT(inode_notifysecctx, selinux_inode_notifysecctx),
6870 LSM_HOOK_INIT(inode_setsecctx, selinux_inode_setsecctx),
6871 LSM_HOOK_INIT(inode_getsecctx, selinux_inode_getsecctx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006872
Casey Schauflere20b0432015-05-02 15:11:36 -07006873 LSM_HOOK_INIT(unix_stream_connect, selinux_socket_unix_stream_connect),
6874 LSM_HOOK_INIT(unix_may_send, selinux_socket_unix_may_send),
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006875
Casey Schauflere20b0432015-05-02 15:11:36 -07006876 LSM_HOOK_INIT(socket_create, selinux_socket_create),
6877 LSM_HOOK_INIT(socket_post_create, selinux_socket_post_create),
David Herrmann0b811db2018-05-04 16:28:21 +02006878 LSM_HOOK_INIT(socket_socketpair, selinux_socket_socketpair),
Casey Schauflere20b0432015-05-02 15:11:36 -07006879 LSM_HOOK_INIT(socket_bind, selinux_socket_bind),
6880 LSM_HOOK_INIT(socket_connect, selinux_socket_connect),
6881 LSM_HOOK_INIT(socket_listen, selinux_socket_listen),
6882 LSM_HOOK_INIT(socket_accept, selinux_socket_accept),
6883 LSM_HOOK_INIT(socket_sendmsg, selinux_socket_sendmsg),
6884 LSM_HOOK_INIT(socket_recvmsg, selinux_socket_recvmsg),
6885 LSM_HOOK_INIT(socket_getsockname, selinux_socket_getsockname),
6886 LSM_HOOK_INIT(socket_getpeername, selinux_socket_getpeername),
6887 LSM_HOOK_INIT(socket_getsockopt, selinux_socket_getsockopt),
6888 LSM_HOOK_INIT(socket_setsockopt, selinux_socket_setsockopt),
6889 LSM_HOOK_INIT(socket_shutdown, selinux_socket_shutdown),
6890 LSM_HOOK_INIT(socket_sock_rcv_skb, selinux_socket_sock_rcv_skb),
6891 LSM_HOOK_INIT(socket_getpeersec_stream,
6892 selinux_socket_getpeersec_stream),
6893 LSM_HOOK_INIT(socket_getpeersec_dgram, selinux_socket_getpeersec_dgram),
6894 LSM_HOOK_INIT(sk_alloc_security, selinux_sk_alloc_security),
6895 LSM_HOOK_INIT(sk_free_security, selinux_sk_free_security),
6896 LSM_HOOK_INIT(sk_clone_security, selinux_sk_clone_security),
6897 LSM_HOOK_INIT(sk_getsecid, selinux_sk_getsecid),
6898 LSM_HOOK_INIT(sock_graft, selinux_sock_graft),
Richard Hainesd4529302018-02-13 20:57:18 +00006899 LSM_HOOK_INIT(sctp_assoc_request, selinux_sctp_assoc_request),
6900 LSM_HOOK_INIT(sctp_sk_clone, selinux_sctp_sk_clone),
6901 LSM_HOOK_INIT(sctp_bind_connect, selinux_sctp_bind_connect),
Casey Schauflere20b0432015-05-02 15:11:36 -07006902 LSM_HOOK_INIT(inet_conn_request, selinux_inet_conn_request),
6903 LSM_HOOK_INIT(inet_csk_clone, selinux_inet_csk_clone),
6904 LSM_HOOK_INIT(inet_conn_established, selinux_inet_conn_established),
6905 LSM_HOOK_INIT(secmark_relabel_packet, selinux_secmark_relabel_packet),
6906 LSM_HOOK_INIT(secmark_refcount_inc, selinux_secmark_refcount_inc),
6907 LSM_HOOK_INIT(secmark_refcount_dec, selinux_secmark_refcount_dec),
6908 LSM_HOOK_INIT(req_classify_flow, selinux_req_classify_flow),
6909 LSM_HOOK_INIT(tun_dev_alloc_security, selinux_tun_dev_alloc_security),
6910 LSM_HOOK_INIT(tun_dev_free_security, selinux_tun_dev_free_security),
6911 LSM_HOOK_INIT(tun_dev_create, selinux_tun_dev_create),
6912 LSM_HOOK_INIT(tun_dev_attach_queue, selinux_tun_dev_attach_queue),
6913 LSM_HOOK_INIT(tun_dev_attach, selinux_tun_dev_attach),
6914 LSM_HOOK_INIT(tun_dev_open, selinux_tun_dev_open),
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006915#ifdef CONFIG_SECURITY_INFINIBAND
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006916 LSM_HOOK_INIT(ib_pkey_access, selinux_ib_pkey_access),
Daniel Jurgensab861df2017-05-19 15:48:58 +03006917 LSM_HOOK_INIT(ib_endport_manage_subnet,
6918 selinux_ib_endport_manage_subnet),
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006919 LSM_HOOK_INIT(ib_alloc_security, selinux_ib_alloc_security),
6920 LSM_HOOK_INIT(ib_free_security, selinux_ib_free_security),
6921#endif
Trent Jaegerd28d1e02005-12-13 23:12:40 -08006922#ifdef CONFIG_SECURITY_NETWORK_XFRM
Casey Schauflere20b0432015-05-02 15:11:36 -07006923 LSM_HOOK_INIT(xfrm_policy_alloc_security, selinux_xfrm_policy_alloc),
6924 LSM_HOOK_INIT(xfrm_policy_clone_security, selinux_xfrm_policy_clone),
6925 LSM_HOOK_INIT(xfrm_policy_free_security, selinux_xfrm_policy_free),
6926 LSM_HOOK_INIT(xfrm_policy_delete_security, selinux_xfrm_policy_delete),
6927 LSM_HOOK_INIT(xfrm_state_alloc, selinux_xfrm_state_alloc),
6928 LSM_HOOK_INIT(xfrm_state_alloc_acquire,
6929 selinux_xfrm_state_alloc_acquire),
6930 LSM_HOOK_INIT(xfrm_state_free_security, selinux_xfrm_state_free),
6931 LSM_HOOK_INIT(xfrm_state_delete_security, selinux_xfrm_state_delete),
6932 LSM_HOOK_INIT(xfrm_policy_lookup, selinux_xfrm_policy_lookup),
6933 LSM_HOOK_INIT(xfrm_state_pol_flow_match,
6934 selinux_xfrm_state_pol_flow_match),
6935 LSM_HOOK_INIT(xfrm_decode_session, selinux_xfrm_decode_session),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006936#endif
Michael LeMayd7200242006-06-22 14:47:17 -07006937
6938#ifdef CONFIG_KEYS
Casey Schauflere20b0432015-05-02 15:11:36 -07006939 LSM_HOOK_INIT(key_alloc, selinux_key_alloc),
6940 LSM_HOOK_INIT(key_free, selinux_key_free),
6941 LSM_HOOK_INIT(key_permission, selinux_key_permission),
6942 LSM_HOOK_INIT(key_getsecurity, selinux_key_getsecurity),
Michael LeMayd7200242006-06-22 14:47:17 -07006943#endif
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02006944
6945#ifdef CONFIG_AUDIT
Casey Schauflere20b0432015-05-02 15:11:36 -07006946 LSM_HOOK_INIT(audit_rule_init, selinux_audit_rule_init),
6947 LSM_HOOK_INIT(audit_rule_known, selinux_audit_rule_known),
6948 LSM_HOOK_INIT(audit_rule_match, selinux_audit_rule_match),
6949 LSM_HOOK_INIT(audit_rule_free, selinux_audit_rule_free),
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02006950#endif
Chenbo Fengec27c352017-10-18 13:00:25 -07006951
6952#ifdef CONFIG_BPF_SYSCALL
6953 LSM_HOOK_INIT(bpf, selinux_bpf),
6954 LSM_HOOK_INIT(bpf_map, selinux_bpf_map),
6955 LSM_HOOK_INIT(bpf_prog, selinux_bpf_prog),
6956 LSM_HOOK_INIT(bpf_map_alloc_security, selinux_bpf_map_alloc),
6957 LSM_HOOK_INIT(bpf_prog_alloc_security, selinux_bpf_prog_alloc),
6958 LSM_HOOK_INIT(bpf_map_free_security, selinux_bpf_map_free),
6959 LSM_HOOK_INIT(bpf_prog_free_security, selinux_bpf_prog_free),
6960#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006961};
6962
6963static __init int selinux_init(void)
6964{
peter enderborgc103a912018-06-12 10:09:03 +02006965 pr_info("SELinux: Initializing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006966
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006967 memset(&selinux_state, 0, sizeof(selinux_state));
Paul Mooree5a5ca92018-03-01 17:38:30 -05006968 enforcing_set(&selinux_state, selinux_enforcing_boot);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006969 selinux_state.checkreqprot = selinux_checkreqprot_boot;
6970 selinux_ss_init(&selinux_state.ss);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006971 selinux_avc_init(&selinux_state.avc);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006972
Linus Torvalds1da177e2005-04-16 15:20:36 -07006973 /* Set the security state for the initial task. */
David Howellsd84f4f92008-11-14 10:39:23 +11006974 cred_init_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006975
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04006976 default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
6977
Linus Torvalds1da177e2005-04-16 15:20:36 -07006978 avc_init();
6979
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006980 avtab_cache_init();
6981
6982 ebitmap_cache_init();
6983
6984 hashtab_cache_init();
6985
Casey Schauflerd69dece52017-01-18 17:09:05 -08006986 security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks), "selinux");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006987
Paul Moore615e51f2014-06-26 14:33:56 -04006988 if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET))
6989 panic("SELinux: Unable to register AVC netcache callback\n");
6990
Daniel Jurgens8f408ab2017-05-19 15:48:53 +03006991 if (avc_add_callback(selinux_lsm_notifier_avc_callback, AVC_CALLBACK_RESET))
6992 panic("SELinux: Unable to register AVC LSM notifier callback\n");
6993
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006994 if (selinux_enforcing_boot)
peter enderborgc103a912018-06-12 10:09:03 +02006995 pr_debug("SELinux: Starting in enforcing mode\n");
Eric Paris828dfe12008-04-17 13:17:49 -04006996 else
peter enderborgc103a912018-06-12 10:09:03 +02006997 pr_debug("SELinux: Starting in permissive mode\n");
Michael LeMayd7200242006-06-22 14:47:17 -07006998
David Howells442155c2018-11-01 23:07:24 +00006999 fs_validate_description(&selinux_fs_parameters);
7000
Linus Torvalds1da177e2005-04-16 15:20:36 -07007001 return 0;
7002}
7003
Al Viroe8c26252010-03-23 06:36:54 -04007004static void delayed_superblock_init(struct super_block *sb, void *unused)
7005{
Al Viro204cc0c2018-12-13 13:41:47 -05007006 selinux_set_mnt_opts(sb, NULL, 0, NULL);
Al Viroe8c26252010-03-23 06:36:54 -04007007}
7008
Linus Torvalds1da177e2005-04-16 15:20:36 -07007009void selinux_complete_init(void)
7010{
peter enderborgc103a912018-06-12 10:09:03 +02007011 pr_debug("SELinux: Completing initialization.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007012
7013 /* Set up any superblocks initialized prior to the policy load. */
peter enderborgc103a912018-06-12 10:09:03 +02007014 pr_debug("SELinux: Setting up existing superblocks.\n");
Al Viroe8c26252010-03-23 06:36:54 -04007015 iterate_supers(delayed_superblock_init, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007016}
7017
7018/* SELinux requires early initialization in order to label
7019 all processes and objects when they are created. */
Kees Cook3d6e5f62018-10-10 17:18:23 -07007020DEFINE_LSM(selinux) = {
Kees Cook07aed2f2018-10-10 17:18:24 -07007021 .name = "selinux",
Kees Cook14bd99c2018-09-19 19:57:06 -07007022 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
Kees Cookc5459b82018-09-13 22:28:48 -07007023 .enabled = &selinux_enabled,
Casey Schauflerbbd36622018-11-12 09:30:56 -08007024 .blobs = &selinux_blob_sizes,
Kees Cook3d6e5f62018-10-10 17:18:23 -07007025 .init = selinux_init,
7026};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007027
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007028#if defined(CONFIG_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007029
Florian Westphal591bb272017-07-26 11:40:52 +02007030static const struct nf_hook_ops selinux_nf_ops[] = {
Paul Mooreeffad8d2008-01-29 08:49:27 -05007031 {
7032 .hook = selinux_ipv4_postroute,
Alban Crequy2597a832012-05-14 03:56:39 +00007033 .pf = NFPROTO_IPV4,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007034 .hooknum = NF_INET_POST_ROUTING,
7035 .priority = NF_IP_PRI_SELINUX_LAST,
7036 },
7037 {
7038 .hook = selinux_ipv4_forward,
Alban Crequy2597a832012-05-14 03:56:39 +00007039 .pf = NFPROTO_IPV4,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007040 .hooknum = NF_INET_FORWARD,
7041 .priority = NF_IP_PRI_SELINUX_FIRST,
Paul Moore948bf852008-10-10 10:16:32 -04007042 },
7043 {
7044 .hook = selinux_ipv4_output,
Alban Crequy2597a832012-05-14 03:56:39 +00007045 .pf = NFPROTO_IPV4,
Paul Moore948bf852008-10-10 10:16:32 -04007046 .hooknum = NF_INET_LOCAL_OUT,
7047 .priority = NF_IP_PRI_SELINUX_FIRST,
Jiri Pirko25db6be2014-09-03 17:42:13 +02007048 },
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04007049#if IS_ENABLED(CONFIG_IPV6)
Paul Mooreeffad8d2008-01-29 08:49:27 -05007050 {
7051 .hook = selinux_ipv6_postroute,
Alban Crequy2597a832012-05-14 03:56:39 +00007052 .pf = NFPROTO_IPV6,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007053 .hooknum = NF_INET_POST_ROUTING,
7054 .priority = NF_IP6_PRI_SELINUX_LAST,
7055 },
7056 {
7057 .hook = selinux_ipv6_forward,
Alban Crequy2597a832012-05-14 03:56:39 +00007058 .pf = NFPROTO_IPV6,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007059 .hooknum = NF_INET_FORWARD,
7060 .priority = NF_IP6_PRI_SELINUX_FIRST,
Jiri Pirko25db6be2014-09-03 17:42:13 +02007061 },
Huw Davies2917f572016-06-27 15:06:15 -04007062 {
7063 .hook = selinux_ipv6_output,
7064 .pf = NFPROTO_IPV6,
7065 .hooknum = NF_INET_LOCAL_OUT,
7066 .priority = NF_IP6_PRI_SELINUX_FIRST,
7067 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07007068#endif /* IPV6 */
Jiri Pirko25db6be2014-09-03 17:42:13 +02007069};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007070
Florian Westphal8e71bf72017-04-21 11:49:09 +02007071static int __net_init selinux_nf_register(struct net *net)
7072{
7073 return nf_register_net_hooks(net, selinux_nf_ops,
7074 ARRAY_SIZE(selinux_nf_ops));
7075}
7076
7077static void __net_exit selinux_nf_unregister(struct net *net)
7078{
7079 nf_unregister_net_hooks(net, selinux_nf_ops,
7080 ARRAY_SIZE(selinux_nf_ops));
7081}
7082
7083static struct pernet_operations selinux_net_ops = {
7084 .init = selinux_nf_register,
7085 .exit = selinux_nf_unregister,
7086};
7087
Linus Torvalds1da177e2005-04-16 15:20:36 -07007088static int __init selinux_nf_ip_init(void)
7089{
Jiri Pirko25db6be2014-09-03 17:42:13 +02007090 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007091
7092 if (!selinux_enabled)
Jiri Pirko25db6be2014-09-03 17:42:13 +02007093 return 0;
Eric Parisfadcdb42007-02-22 18:11:31 -05007094
peter enderborgc103a912018-06-12 10:09:03 +02007095 pr_debug("SELinux: Registering netfilter hooks\n");
Eric Parisfadcdb42007-02-22 18:11:31 -05007096
Florian Westphal8e71bf72017-04-21 11:49:09 +02007097 err = register_pernet_subsys(&selinux_net_ops);
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07007098 if (err)
Florian Westphal8e71bf72017-04-21 11:49:09 +02007099 panic("SELinux: register_pernet_subsys: error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007100
Jiri Pirko25db6be2014-09-03 17:42:13 +02007101 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007102}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007103__initcall(selinux_nf_ip_init);
7104
7105#ifdef CONFIG_SECURITY_SELINUX_DISABLE
7106static void selinux_nf_ip_exit(void)
7107{
peter enderborgc103a912018-06-12 10:09:03 +02007108 pr_debug("SELinux: Unregistering netfilter hooks\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007109
Florian Westphal8e71bf72017-04-21 11:49:09 +02007110 unregister_pernet_subsys(&selinux_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007111}
7112#endif
7113
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007114#else /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007115
7116#ifdef CONFIG_SECURITY_SELINUX_DISABLE
7117#define selinux_nf_ip_exit()
7118#endif
7119
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007120#endif /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007121
7122#ifdef CONFIG_SECURITY_SELINUX_DISABLE
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007123int selinux_disable(struct selinux_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007124{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007125 if (state->initialized) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007126 /* Not permitted after initial policy load. */
7127 return -EINVAL;
7128 }
7129
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007130 if (state->disabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007131 /* Only do this once. */
7132 return -EINVAL;
7133 }
7134
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007135 state->disabled = 1;
7136
peter enderborgc103a912018-06-12 10:09:03 +02007137 pr_info("SELinux: Disabled at runtime.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007138
Stephen Smalley30d55282006-05-03 10:52:36 -04007139 selinux_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007140
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07007141 security_delete_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007142
Eric Parisaf8ff042009-09-20 21:23:01 -04007143 /* Try to destroy the avc node cache */
7144 avc_disable();
7145
Linus Torvalds1da177e2005-04-16 15:20:36 -07007146 /* Unregister netfilter hooks. */
7147 selinux_nf_ip_exit();
7148
7149 /* Unregister selinuxfs. */
7150 exit_sel_fs();
7151
7152 return 0;
7153}
7154#endif