blob: fea66f6b31bf50435519fcd324ed54e3a785bb2f [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
Gen Zhange2e0e092019-06-12 21:28:21 +08001055 if (token != Opt_seclabel) {
Al Viro757cbe52018-12-14 23:42:21 -05001056 val = kmemdup_nul(val, len, GFP_KERNEL);
Gen Zhange2e0e092019-06-12 21:28:21 +08001057 if (!val) {
1058 rc = -ENOMEM;
1059 goto free_opt;
1060 }
1061 }
Al Viro757cbe52018-12-14 23:42:21 -05001062 rc = selinux_add_opt(token, val, mnt_opts);
1063 if (unlikely(rc)) {
1064 kfree(val);
Gen Zhange2e0e092019-06-12 21:28:21 +08001065 goto free_opt;
1066 }
1067 return rc;
1068
1069free_opt:
1070 if (*mnt_opts) {
1071 selinux_free_mnt_opts(*mnt_opts);
1072 *mnt_opts = NULL;
Al Viro757cbe52018-12-14 23:42:21 -05001073 }
1074 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Al Viroe3489f82018-12-13 00:24:36 -05001077static int show_sid(struct seq_file *m, u32 sid)
Eric Paris2069f452008-07-04 09:47:13 +10001078{
Al Viroe3489f82018-12-13 00:24:36 -05001079 char *context = NULL;
1080 u32 len;
1081 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Al Viroe3489f82018-12-13 00:24:36 -05001083 rc = security_sid_to_context(&selinux_state, sid,
1084 &context, &len);
1085 if (!rc) {
1086 bool has_comma = context && strchr(context, ',');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
David Howells442155c2018-11-01 23:07:24 +00001088 seq_putc(m, '=');
Eric Paris2069f452008-07-04 09:47:13 +10001089 if (has_comma)
1090 seq_putc(m, '\"');
Al Viroe3489f82018-12-13 00:24:36 -05001091 seq_escape(m, context, "\"\n\\");
Eric Paris2069f452008-07-04 09:47:13 +10001092 if (has_comma)
1093 seq_putc(m, '\"');
1094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 kfree(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 return rc;
1097}
Eric Paris2069f452008-07-04 09:47:13 +10001098
1099static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
1100{
Al Viroe3489f82018-12-13 00:24:36 -05001101 struct superblock_security_struct *sbsec = sb->s_security;
Eric Paris2069f452008-07-04 09:47:13 +10001102 int rc;
1103
Al Viroe3489f82018-12-13 00:24:36 -05001104 if (!(sbsec->flags & SE_SBINITIALIZED))
1105 return 0;
1106
1107 if (!selinux_state.initialized)
1108 return 0;
1109
1110 if (sbsec->flags & FSCONTEXT_MNT) {
1111 seq_putc(m, ',');
1112 seq_puts(m, FSCONTEXT_STR);
1113 rc = show_sid(m, sbsec->sid);
1114 if (rc)
1115 return rc;
Eric Paris383795c2008-07-29 17:07:26 -04001116 }
Al Viroe3489f82018-12-13 00:24:36 -05001117 if (sbsec->flags & CONTEXT_MNT) {
1118 seq_putc(m, ',');
1119 seq_puts(m, CONTEXT_STR);
1120 rc = show_sid(m, sbsec->mntpoint_sid);
1121 if (rc)
1122 return rc;
1123 }
1124 if (sbsec->flags & DEFCONTEXT_MNT) {
1125 seq_putc(m, ',');
1126 seq_puts(m, DEFCONTEXT_STR);
1127 rc = show_sid(m, sbsec->def_sid);
1128 if (rc)
1129 return rc;
1130 }
1131 if (sbsec->flags & ROOTCONTEXT_MNT) {
1132 struct dentry *root = sbsec->sb->s_root;
1133 struct inode_security_struct *isec = backing_inode_security(root);
1134 seq_putc(m, ',');
1135 seq_puts(m, ROOTCONTEXT_STR);
1136 rc = show_sid(m, isec->sid);
1137 if (rc)
1138 return rc;
1139 }
1140 if (sbsec->flags & SBLABEL_MNT) {
1141 seq_putc(m, ',');
David Howells442155c2018-11-01 23:07:24 +00001142 seq_puts(m, SECLABEL_STR);
Al Viroe3489f82018-12-13 00:24:36 -05001143 }
1144 return 0;
Eric Paris2069f452008-07-04 09:47:13 +10001145}
1146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147static inline u16 inode_mode_to_security_class(umode_t mode)
1148{
1149 switch (mode & S_IFMT) {
1150 case S_IFSOCK:
1151 return SECCLASS_SOCK_FILE;
1152 case S_IFLNK:
1153 return SECCLASS_LNK_FILE;
1154 case S_IFREG:
1155 return SECCLASS_FILE;
1156 case S_IFBLK:
1157 return SECCLASS_BLK_FILE;
1158 case S_IFDIR:
1159 return SECCLASS_DIR;
1160 case S_IFCHR:
1161 return SECCLASS_CHR_FILE;
1162 case S_IFIFO:
1163 return SECCLASS_FIFO_FILE;
1164
1165 }
1166
1167 return SECCLASS_FILE;
1168}
1169
James Morris13402582005-09-30 14:24:34 -04001170static inline int default_protocol_stream(int protocol)
1171{
1172 return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP);
1173}
1174
1175static inline int default_protocol_dgram(int protocol)
1176{
1177 return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
1178}
1179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180static inline u16 socket_type_to_security_class(int family, int type, int protocol)
1181{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001182 int extsockclass = selinux_policycap_extsockclass();
Stephen Smalleyda69a532017-01-09 10:07:30 -05001183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 switch (family) {
1185 case PF_UNIX:
1186 switch (type) {
1187 case SOCK_STREAM:
1188 case SOCK_SEQPACKET:
1189 return SECCLASS_UNIX_STREAM_SOCKET;
1190 case SOCK_DGRAM:
Luis Ressel2a764b52017-07-25 15:13:41 -04001191 case SOCK_RAW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 return SECCLASS_UNIX_DGRAM_SOCKET;
1193 }
1194 break;
1195 case PF_INET:
1196 case PF_INET6:
1197 switch (type) {
1198 case SOCK_STREAM:
Stephen Smalleyda69a532017-01-09 10:07:30 -05001199 case SOCK_SEQPACKET:
James Morris13402582005-09-30 14:24:34 -04001200 if (default_protocol_stream(protocol))
1201 return SECCLASS_TCP_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001202 else if (extsockclass && protocol == IPPROTO_SCTP)
1203 return SECCLASS_SCTP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001204 else
1205 return SECCLASS_RAWIP_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 case SOCK_DGRAM:
James Morris13402582005-09-30 14:24:34 -04001207 if (default_protocol_dgram(protocol))
1208 return SECCLASS_UDP_SOCKET;
Stephen Smalleyef379792017-01-09 10:07:31 -05001209 else if (extsockclass && (protocol == IPPROTO_ICMP ||
1210 protocol == IPPROTO_ICMPV6))
Stephen Smalleyda69a532017-01-09 10:07:30 -05001211 return SECCLASS_ICMP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001212 else
1213 return SECCLASS_RAWIP_SOCKET;
James Morris2ee92d42006-11-13 16:09:01 -08001214 case SOCK_DCCP:
1215 return SECCLASS_DCCP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001216 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 return SECCLASS_RAWIP_SOCKET;
1218 }
1219 break;
1220 case PF_NETLINK:
1221 switch (protocol) {
1222 case NETLINK_ROUTE:
1223 return SECCLASS_NETLINK_ROUTE_SOCKET;
Pavel Emelyanov7f1fb602011-12-06 07:56:43 +00001224 case NETLINK_SOCK_DIAG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 return SECCLASS_NETLINK_TCPDIAG_SOCKET;
1226 case NETLINK_NFLOG:
1227 return SECCLASS_NETLINK_NFLOG_SOCKET;
1228 case NETLINK_XFRM:
1229 return SECCLASS_NETLINK_XFRM_SOCKET;
1230 case NETLINK_SELINUX:
1231 return SECCLASS_NETLINK_SELINUX_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001232 case NETLINK_ISCSI:
1233 return SECCLASS_NETLINK_ISCSI_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 case NETLINK_AUDIT:
1235 return SECCLASS_NETLINK_AUDIT_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001236 case NETLINK_FIB_LOOKUP:
1237 return SECCLASS_NETLINK_FIB_LOOKUP_SOCKET;
1238 case NETLINK_CONNECTOR:
1239 return SECCLASS_NETLINK_CONNECTOR_SOCKET;
1240 case NETLINK_NETFILTER:
1241 return SECCLASS_NETLINK_NETFILTER_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 case NETLINK_DNRTMSG:
1243 return SECCLASS_NETLINK_DNRT_SOCKET;
James Morris0c9b7942005-04-16 15:24:13 -07001244 case NETLINK_KOBJECT_UEVENT:
1245 return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001246 case NETLINK_GENERIC:
1247 return SECCLASS_NETLINK_GENERIC_SOCKET;
1248 case NETLINK_SCSITRANSPORT:
1249 return SECCLASS_NETLINK_SCSITRANSPORT_SOCKET;
1250 case NETLINK_RDMA:
1251 return SECCLASS_NETLINK_RDMA_SOCKET;
1252 case NETLINK_CRYPTO:
1253 return SECCLASS_NETLINK_CRYPTO_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 default:
1255 return SECCLASS_NETLINK_SOCKET;
1256 }
1257 case PF_PACKET:
1258 return SECCLASS_PACKET_SOCKET;
1259 case PF_KEY:
1260 return SECCLASS_KEY_SOCKET;
Christopher J. PeBenito3e3ff152006-06-09 00:25:03 -07001261 case PF_APPLETALK:
1262 return SECCLASS_APPLETALK_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 }
1264
Stephen Smalleyda69a532017-01-09 10:07:30 -05001265 if (extsockclass) {
1266 switch (family) {
1267 case PF_AX25:
1268 return SECCLASS_AX25_SOCKET;
1269 case PF_IPX:
1270 return SECCLASS_IPX_SOCKET;
1271 case PF_NETROM:
1272 return SECCLASS_NETROM_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001273 case PF_ATMPVC:
1274 return SECCLASS_ATMPVC_SOCKET;
1275 case PF_X25:
1276 return SECCLASS_X25_SOCKET;
1277 case PF_ROSE:
1278 return SECCLASS_ROSE_SOCKET;
1279 case PF_DECnet:
1280 return SECCLASS_DECNET_SOCKET;
1281 case PF_ATMSVC:
1282 return SECCLASS_ATMSVC_SOCKET;
1283 case PF_RDS:
1284 return SECCLASS_RDS_SOCKET;
1285 case PF_IRDA:
1286 return SECCLASS_IRDA_SOCKET;
1287 case PF_PPPOX:
1288 return SECCLASS_PPPOX_SOCKET;
1289 case PF_LLC:
1290 return SECCLASS_LLC_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001291 case PF_CAN:
1292 return SECCLASS_CAN_SOCKET;
1293 case PF_TIPC:
1294 return SECCLASS_TIPC_SOCKET;
1295 case PF_BLUETOOTH:
1296 return SECCLASS_BLUETOOTH_SOCKET;
1297 case PF_IUCV:
1298 return SECCLASS_IUCV_SOCKET;
1299 case PF_RXRPC:
1300 return SECCLASS_RXRPC_SOCKET;
1301 case PF_ISDN:
1302 return SECCLASS_ISDN_SOCKET;
1303 case PF_PHONET:
1304 return SECCLASS_PHONET_SOCKET;
1305 case PF_IEEE802154:
1306 return SECCLASS_IEEE802154_SOCKET;
1307 case PF_CAIF:
1308 return SECCLASS_CAIF_SOCKET;
1309 case PF_ALG:
1310 return SECCLASS_ALG_SOCKET;
1311 case PF_NFC:
1312 return SECCLASS_NFC_SOCKET;
1313 case PF_VSOCK:
1314 return SECCLASS_VSOCK_SOCKET;
1315 case PF_KCM:
1316 return SECCLASS_KCM_SOCKET;
1317 case PF_QIPCRTR:
1318 return SECCLASS_QIPCRTR_SOCKET;
Linus Torvalds3051bf32017-02-22 10:15:09 -08001319 case PF_SMC:
1320 return SECCLASS_SMC_SOCKET;
Björn Töpel68e8b842018-05-02 13:01:22 +02001321 case PF_XDP:
1322 return SECCLASS_XDP_SOCKET;
1323#if PF_MAX > 45
Stephen Smalleyda69a532017-01-09 10:07:30 -05001324#error New address family defined, please update this function.
1325#endif
1326 }
1327 }
1328
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 return SECCLASS_SOCKET;
1330}
1331
Stephen Smalley134509d2015-06-04 16:22:17 -04001332static int selinux_genfs_get_sid(struct dentry *dentry,
1333 u16 tclass,
1334 u16 flags,
1335 u32 *sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336{
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001337 int rc;
Al Virofc640052016-04-10 01:33:30 -04001338 struct super_block *sb = dentry->d_sb;
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001339 char *buffer, *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Eric Paris828dfe12008-04-17 13:17:49 -04001341 buffer = (char *)__get_free_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 if (!buffer)
1343 return -ENOMEM;
1344
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001345 path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
1346 if (IS_ERR(path))
1347 rc = PTR_ERR(path);
1348 else {
Stephen Smalley134509d2015-06-04 16:22:17 -04001349 if (flags & SE_SBPROC) {
1350 /* each process gets a /proc/PID/ entry. Strip off the
1351 * PID part to get a valid selinux labeling.
1352 * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
1353 while (path[1] >= '0' && path[1] <= '9') {
1354 path[1] = '/';
1355 path++;
1356 }
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001357 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001358 rc = security_genfs_sid(&selinux_state, sb->s_type->name,
1359 path, tclass, sid);
Stephen Smalley7bb185e2018-09-04 16:51:36 -04001360 if (rc == -ENOENT) {
1361 /* No match in policy, mark as unlabeled. */
1362 *sid = SECINITSID_UNLABELED;
1363 rc = 0;
1364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 free_page((unsigned long)buffer);
1367 return rc;
1368}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Ondrej Mosnacekb7540262019-02-22 15:57:14 +01001370static int inode_doinit_use_xattr(struct inode *inode, struct dentry *dentry,
1371 u32 def_sid, u32 *sid)
1372{
1373#define INITCONTEXTLEN 255
1374 char *context;
1375 unsigned int len;
1376 int rc;
1377
1378 len = INITCONTEXTLEN;
1379 context = kmalloc(len + 1, GFP_NOFS);
1380 if (!context)
1381 return -ENOMEM;
1382
1383 context[len] = '\0';
1384 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
1385 if (rc == -ERANGE) {
1386 kfree(context);
1387
1388 /* Need a larger buffer. Query for the right size. */
1389 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, NULL, 0);
1390 if (rc < 0)
1391 return rc;
1392
1393 len = rc;
1394 context = kmalloc(len + 1, GFP_NOFS);
1395 if (!context)
1396 return -ENOMEM;
1397
1398 context[len] = '\0';
1399 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX,
1400 context, len);
1401 }
1402 if (rc < 0) {
1403 kfree(context);
1404 if (rc != -ENODATA) {
1405 pr_warn("SELinux: %s: getxattr returned %d for dev=%s ino=%ld\n",
1406 __func__, -rc, inode->i_sb->s_id, inode->i_ino);
1407 return rc;
1408 }
1409 *sid = def_sid;
1410 return 0;
1411 }
1412
1413 rc = security_context_to_sid_default(&selinux_state, context, rc, sid,
1414 def_sid, GFP_NOFS);
1415 if (rc) {
1416 char *dev = inode->i_sb->s_id;
1417 unsigned long ino = inode->i_ino;
1418
1419 if (rc == -EINVAL) {
1420 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",
1421 ino, dev, context);
1422 } else {
1423 pr_warn("SELinux: %s: context_to_sid(%s) returned %d for dev=%s ino=%ld\n",
1424 __func__, context, -rc, dev, ino);
1425 }
1426 }
1427 kfree(context);
1428 return 0;
1429}
1430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431/* The inode's security attributes must be initialized before first use. */
1432static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
1433{
1434 struct superblock_security_struct *sbsec = NULL;
Casey Schaufler80788c22018-09-21 17:19:11 -07001435 struct inode_security_struct *isec = selinux_inode(inode);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001436 u32 task_sid, sid = 0;
1437 u16 sclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05001441 if (isec->initialized == LABEL_INITIALIZED)
Andreas Gruenbacher13457d02016-11-10 22:18:29 +01001442 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001444 spin_lock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05001445 if (isec->initialized == LABEL_INITIALIZED)
Eric Paris23970742006-09-25 23:32:01 -07001446 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Andreas Gruenbacher13457d02016-11-10 22:18:29 +01001448 if (isec->sclass == SECCLASS_FILE)
1449 isec->sclass = inode_mode_to_security_class(inode->i_mode);
1450
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 sbsec = inode->i_sb->s_security;
David P. Quigley0d90a7e2009-01-16 09:22:02 -05001452 if (!(sbsec->flags & SE_SBINITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 /* Defer initialization until selinux_complete_init,
1454 after the initial policy is loaded and the security
1455 server is ready to handle calls. */
1456 spin_lock(&sbsec->isec_lock);
1457 if (list_empty(&isec->list))
1458 list_add(&isec->list, &sbsec->isec_head);
1459 spin_unlock(&sbsec->isec_lock);
Eric Paris23970742006-09-25 23:32:01 -07001460 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 }
1462
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001463 sclass = isec->sclass;
1464 task_sid = isec->task_sid;
1465 sid = isec->sid;
1466 isec->initialized = LABEL_PENDING;
1467 spin_unlock(&isec->lock);
1468
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 switch (sbsec->behavior) {
David Quigleyeb9ae682013-05-22 12:50:37 -04001470 case SECURITY_FS_USE_NATIVE:
1471 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 case SECURITY_FS_USE_XATTR:
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001473 if (!(inode->i_opflags & IOP_XATTR)) {
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001474 sid = sbsec->def_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 break;
1476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 /* Need a dentry, since the xattr API requires one.
1478 Life would be simpler if we could just pass the inode. */
1479 if (opt_dentry) {
1480 /* Called from d_instantiate or d_splice_alias. */
1481 dentry = dget(opt_dentry);
1482 } else {
Al Virob1271252018-04-25 10:28:38 -04001483 /*
1484 * Called from selinux_complete_init, try to find a dentry.
1485 * Some filesystems really want a connected one, so try
1486 * that first. We could split SECURITY_FS_USE_XATTR in
1487 * two, depending upon that...
1488 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 dentry = d_find_alias(inode);
Al Virob1271252018-04-25 10:28:38 -04001490 if (!dentry)
1491 dentry = d_find_any_alias(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 }
1493 if (!dentry) {
Eric Parisdf7f54c2009-03-09 14:35:58 -04001494 /*
1495 * this is can be hit on boot when a file is accessed
1496 * before the policy is loaded. When we load policy we
1497 * may find inodes that have no dentry on the
1498 * sbsec->isec_head list. No reason to complain as these
1499 * will get fixed up the next time we go through
1500 * inode_doinit with a dentry, before these inodes could
1501 * be used again by userspace.
1502 */
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001503 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 }
1505
Ondrej Mosnacekb7540262019-02-22 15:57:14 +01001506 rc = inode_doinit_use_xattr(inode, dentry, sbsec->def_sid,
1507 &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 dput(dentry);
Ondrej Mosnacekb7540262019-02-22 15:57:14 +01001509 if (rc)
1510 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 break;
1512 case SECURITY_FS_USE_TASK:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001513 sid = task_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 break;
1515 case SECURITY_FS_USE_TRANS:
1516 /* Default to the fs SID. */
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001517 sid = sbsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
1519 /* Try to obtain a transition SID. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001520 rc = security_transition_sid(&selinux_state, task_sid, sid,
1521 sclass, NULL, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 if (rc)
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001523 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 break;
Eric Parisc312feb2006-07-10 04:43:53 -07001525 case SECURITY_FS_USE_MNTPOINT:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001526 sid = sbsec->mntpoint_sid;
Eric Parisc312feb2006-07-10 04:43:53 -07001527 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 default:
Eric Parisc312feb2006-07-10 04:43:53 -07001529 /* Default to the fs superblock SID. */
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001530 sid = sbsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
Stephen Smalley134509d2015-06-04 16:22:17 -04001532 if ((sbsec->flags & SE_SBGENFS) && !S_ISLNK(inode->i_mode)) {
Paul Mooref64410e2014-03-19 16:46:18 -04001533 /* We must have a dentry to determine the label on
1534 * procfs inodes */
Al Virob1271252018-04-25 10:28:38 -04001535 if (opt_dentry) {
Paul Mooref64410e2014-03-19 16:46:18 -04001536 /* Called from d_instantiate or
1537 * d_splice_alias. */
1538 dentry = dget(opt_dentry);
Al Virob1271252018-04-25 10:28:38 -04001539 } else {
Paul Mooref64410e2014-03-19 16:46:18 -04001540 /* Called from selinux_complete_init, try to
Al Virob1271252018-04-25 10:28:38 -04001541 * find a dentry. Some filesystems really want
1542 * a connected one, so try that first.
1543 */
Paul Mooref64410e2014-03-19 16:46:18 -04001544 dentry = d_find_alias(inode);
Al Virob1271252018-04-25 10:28:38 -04001545 if (!dentry)
1546 dentry = d_find_any_alias(inode);
1547 }
Paul Mooref64410e2014-03-19 16:46:18 -04001548 /*
1549 * This can be hit on boot when a file is accessed
1550 * before the policy is loaded. When we load policy we
1551 * may find inodes that have no dentry on the
1552 * sbsec->isec_head list. No reason to complain as
1553 * these will get fixed up the next time we go through
1554 * inode_doinit() with a dentry, before these inodes
1555 * could be used again by userspace.
1556 */
1557 if (!dentry)
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001558 goto out;
1559 rc = selinux_genfs_get_sid(dentry, sclass,
Stephen Smalley134509d2015-06-04 16:22:17 -04001560 sbsec->flags, &sid);
Ondrej Mosnacekb7540262019-02-22 15:57:14 +01001561 if (rc) {
1562 dput(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001563 goto out;
Ondrej Mosnacekb7540262019-02-22 15:57:14 +01001564 }
1565
1566 if ((sbsec->flags & SE_SBGENFS_XATTR) &&
1567 (inode->i_opflags & IOP_XATTR)) {
1568 rc = inode_doinit_use_xattr(inode, dentry,
1569 sid, &sid);
1570 if (rc) {
1571 dput(dentry);
1572 goto out;
1573 }
1574 }
1575 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 }
1577 break;
1578 }
1579
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001580out:
1581 spin_lock(&isec->lock);
1582 if (isec->initialized == LABEL_PENDING) {
1583 if (!sid || rc) {
1584 isec->initialized = LABEL_INVALID;
1585 goto out_unlock;
1586 }
1587
1588 isec->initialized = LABEL_INITIALIZED;
1589 isec->sid = sid;
1590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
Eric Paris23970742006-09-25 23:32:01 -07001592out_unlock:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001593 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 return rc;
1595}
1596
1597/* Convert a Linux signal to an access vector. */
1598static inline u32 signal_to_av(int sig)
1599{
1600 u32 perm = 0;
1601
1602 switch (sig) {
1603 case SIGCHLD:
1604 /* Commonly granted from child to parent. */
1605 perm = PROCESS__SIGCHLD;
1606 break;
1607 case SIGKILL:
1608 /* Cannot be caught or ignored */
1609 perm = PROCESS__SIGKILL;
1610 break;
1611 case SIGSTOP:
1612 /* Cannot be caught or ignored */
1613 perm = PROCESS__SIGSTOP;
1614 break;
1615 default:
1616 /* All other signals. */
1617 perm = PROCESS__SIGNAL;
1618 break;
1619 }
1620
1621 return perm;
1622}
1623
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001624#if CAP_LAST_CAP > 63
1625#error Fix SELinux to handle capabilities > 63.
1626#endif
1627
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628/* Check whether a task is allowed to use a capability. */
Eric Paris6a9de492012-01-03 12:25:14 -05001629static int cred_has_capability(const struct cred *cred,
Micah Mortonc1a85a02019-01-07 16:10:53 -08001630 int cap, unsigned int opts, bool initns)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631{
Thomas Liu2bf49692009-07-14 12:14:09 -04001632 struct common_audit_data ad;
Eric Paris06112162008-11-11 22:02:50 +11001633 struct av_decision avd;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001634 u16 sclass;
David Howells3699c532009-01-06 22:27:01 +00001635 u32 sid = cred_sid(cred);
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001636 u32 av = CAP_TO_MASK(cap);
Eric Paris06112162008-11-11 22:02:50 +11001637 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Eric Paris50c205f2012-04-04 15:01:43 -04001639 ad.type = LSM_AUDIT_DATA_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 ad.u.cap = cap;
1641
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001642 switch (CAP_TO_INDEX(cap)) {
1643 case 0:
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04001644 sclass = initns ? SECCLASS_CAPABILITY : SECCLASS_CAP_USERNS;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001645 break;
1646 case 1:
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04001647 sclass = initns ? SECCLASS_CAPABILITY2 : SECCLASS_CAP2_USERNS;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001648 break;
1649 default:
peter enderborgc103a912018-06-12 10:09:03 +02001650 pr_err("SELinux: out of range capability %d\n", cap);
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001651 BUG();
Eric Parisa35c6c832011-04-20 10:21:28 -04001652 return -EINVAL;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001653 }
Eric Paris06112162008-11-11 22:02:50 +11001654
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001655 rc = avc_has_perm_noaudit(&selinux_state,
1656 sid, sid, sclass, av, 0, &avd);
Micah Mortonc1a85a02019-01-07 16:10:53 -08001657 if (!(opts & CAP_OPT_NOAUDIT)) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001658 int rc2 = avc_audit(&selinux_state,
1659 sid, sid, sclass, av, &avd, rc, &ad, 0);
Eric Paris9ade0cf2011-04-25 16:26:29 -04001660 if (rc2)
1661 return rc2;
1662 }
Eric Paris06112162008-11-11 22:02:50 +11001663 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664}
1665
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666/* Check whether a task has a particular permission to an inode.
1667 The 'adp' parameter is optional and allows other audit
1668 data to be passed (e.g. the dentry). */
David Howells88e67f32008-11-14 10:39:21 +11001669static int inode_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 struct inode *inode,
1671 u32 perms,
Linus Torvalds19e49832013-10-04 12:54:11 -07001672 struct common_audit_data *adp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 struct inode_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +11001675 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
David Howellse0e81732009-09-02 09:13:40 +01001677 validate_creds(cred);
1678
Eric Paris828dfe12008-04-17 13:17:49 -04001679 if (unlikely(IS_PRIVATE(inode)))
Stephen Smalleybbaca6c2007-02-14 00:34:16 -08001680 return 0;
1681
David Howells88e67f32008-11-14 10:39:21 +11001682 sid = cred_sid(cred);
Casey Schaufler80788c22018-09-21 17:19:11 -07001683 isec = selinux_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001685 return avc_has_perm(&selinux_state,
1686 sid, isec->sid, isec->sclass, perms, adp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687}
1688
1689/* Same as inode_has_perm, but pass explicit audit data containing
1690 the dentry to help the auditing code to more easily generate the
1691 pathname if needed. */
David Howells88e67f32008-11-14 10:39:21 +11001692static inline int dentry_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 struct dentry *dentry,
1694 u32 av)
1695{
David Howellsc6f493d2015-03-17 22:26:22 +00001696 struct inode *inode = d_backing_inode(dentry);
Thomas Liu2bf49692009-07-14 12:14:09 -04001697 struct common_audit_data ad;
David Howells88e67f32008-11-14 10:39:21 +11001698
Eric Paris50c205f2012-04-04 15:01:43 -04001699 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Paris2875fa02011-04-28 16:04:24 -04001700 ad.u.dentry = dentry;
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05001701 __inode_security_revalidate(inode, dentry, true);
Linus Torvalds19e49832013-10-04 12:54:11 -07001702 return inode_has_perm(cred, inode, av, &ad);
Eric Paris2875fa02011-04-28 16:04:24 -04001703}
1704
1705/* Same as inode_has_perm, but pass explicit audit data containing
1706 the path to help the auditing code to more easily generate the
1707 pathname if needed. */
1708static inline int path_has_perm(const struct cred *cred,
Al Viro3f7036a2015-03-08 19:28:30 -04001709 const struct path *path,
Eric Paris2875fa02011-04-28 16:04:24 -04001710 u32 av)
1711{
David Howellsc6f493d2015-03-17 22:26:22 +00001712 struct inode *inode = d_backing_inode(path->dentry);
Eric Paris2875fa02011-04-28 16:04:24 -04001713 struct common_audit_data ad;
1714
Eric Paris50c205f2012-04-04 15:01:43 -04001715 ad.type = LSM_AUDIT_DATA_PATH;
Eric Paris2875fa02011-04-28 16:04:24 -04001716 ad.u.path = *path;
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05001717 __inode_security_revalidate(inode, path->dentry, true);
Linus Torvalds19e49832013-10-04 12:54:11 -07001718 return inode_has_perm(cred, inode, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719}
1720
David Howells13f8e982013-06-13 23:37:55 +01001721/* Same as path_has_perm, but uses the inode from the file struct. */
1722static inline int file_path_has_perm(const struct cred *cred,
1723 struct file *file,
1724 u32 av)
1725{
1726 struct common_audit_data ad;
1727
Vivek Goyal43af5de2016-09-09 11:37:49 -04001728 ad.type = LSM_AUDIT_DATA_FILE;
1729 ad.u.file = file;
Linus Torvalds19e49832013-10-04 12:54:11 -07001730 return inode_has_perm(cred, file_inode(file), av, &ad);
David Howells13f8e982013-06-13 23:37:55 +01001731}
1732
Chenbo Fengf66e4482017-10-18 13:00:26 -07001733#ifdef CONFIG_BPF_SYSCALL
1734static int bpf_fd_pass(struct file *file, u32 sid);
1735#endif
1736
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737/* Check whether a task can use an open file descriptor to
1738 access an inode in a given way. Check access to the
1739 descriptor itself, and then use dentry_has_perm to
1740 check a particular permission to the file.
1741 Access to the descriptor is implicitly granted if it
1742 has the same SID as the process. If av is zero, then
1743 access to the file is not checked, e.g. for cases
1744 where only the descriptor is affected like seek. */
David Howells88e67f32008-11-14 10:39:21 +11001745static int file_has_perm(const struct cred *cred,
1746 struct file *file,
1747 u32 av)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748{
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07001749 struct file_security_struct *fsec = selinux_file(file);
Al Viro496ad9a2013-01-23 17:07:38 -05001750 struct inode *inode = file_inode(file);
Thomas Liu2bf49692009-07-14 12:14:09 -04001751 struct common_audit_data ad;
David Howells88e67f32008-11-14 10:39:21 +11001752 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 int rc;
1754
Vivek Goyal43af5de2016-09-09 11:37:49 -04001755 ad.type = LSM_AUDIT_DATA_FILE;
1756 ad.u.file = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
David Howells275bb412008-11-14 10:39:19 +11001758 if (sid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001759 rc = avc_has_perm(&selinux_state,
1760 sid, fsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 SECCLASS_FD,
1762 FD__USE,
1763 &ad);
1764 if (rc)
David Howells88e67f32008-11-14 10:39:21 +11001765 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 }
1767
Chenbo Fengf66e4482017-10-18 13:00:26 -07001768#ifdef CONFIG_BPF_SYSCALL
1769 rc = bpf_fd_pass(file, cred_sid(cred));
1770 if (rc)
1771 return rc;
1772#endif
1773
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 /* av is zero if only checking access to the descriptor. */
David Howells88e67f32008-11-14 10:39:21 +11001775 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 if (av)
Linus Torvalds19e49832013-10-04 12:54:11 -07001777 rc = inode_has_perm(cred, inode, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
David Howells88e67f32008-11-14 10:39:21 +11001779out:
1780 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781}
1782
David Howellsc3c188b2015-07-10 17:19:58 -04001783/*
1784 * Determine the label for an inode that might be unioned.
1785 */
Vivek Goyalc957f6d2016-07-13 10:44:51 -04001786static int
1787selinux_determine_inode_label(const struct task_security_struct *tsec,
1788 struct inode *dir,
1789 const struct qstr *name, u16 tclass,
1790 u32 *_new_isid)
David Howellsc3c188b2015-07-10 17:19:58 -04001791{
1792 const struct superblock_security_struct *sbsec = dir->i_sb->s_security;
David Howellsc3c188b2015-07-10 17:19:58 -04001793
1794 if ((sbsec->flags & SE_SBINITIALIZED) &&
1795 (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)) {
1796 *_new_isid = sbsec->mntpoint_sid;
1797 } else if ((sbsec->flags & SBLABEL_MNT) &&
1798 tsec->create_sid) {
1799 *_new_isid = tsec->create_sid;
1800 } else {
Paul Moore20cdef82016-04-04 14:14:42 -04001801 const struct inode_security_struct *dsec = inode_security(dir);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001802 return security_transition_sid(&selinux_state, tsec->sid,
1803 dsec->sid, tclass,
David Howellsc3c188b2015-07-10 17:19:58 -04001804 name, _new_isid);
1805 }
1806
1807 return 0;
1808}
1809
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810/* Check whether a task can create a file. */
1811static int may_create(struct inode *dir,
1812 struct dentry *dentry,
1813 u16 tclass)
1814{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07001815 const struct task_security_struct *tsec = selinux_cred(current_cred());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 struct inode_security_struct *dsec;
1817 struct superblock_security_struct *sbsec;
David Howells275bb412008-11-14 10:39:19 +11001818 u32 sid, newsid;
Thomas Liu2bf49692009-07-14 12:14:09 -04001819 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 int rc;
1821
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001822 dsec = inode_security(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 sbsec = dir->i_sb->s_security;
1824
David Howells275bb412008-11-14 10:39:19 +11001825 sid = tsec->sid;
David Howells275bb412008-11-14 10:39:19 +11001826
Eric Paris50c205f2012-04-04 15:01:43 -04001827 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04001828 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001830 rc = avc_has_perm(&selinux_state,
1831 sid, dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 DIR__ADD_NAME | DIR__SEARCH,
1833 &ad);
1834 if (rc)
1835 return rc;
1836
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07001837 rc = selinux_determine_inode_label(selinux_cred(current_cred()), dir,
Vivek Goyalc957f6d2016-07-13 10:44:51 -04001838 &dentry->d_name, tclass, &newsid);
David Howellsc3c188b2015-07-10 17:19:58 -04001839 if (rc)
1840 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001842 rc = avc_has_perm(&selinux_state,
1843 sid, newsid, tclass, FILE__CREATE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 if (rc)
1845 return rc;
1846
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001847 return avc_has_perm(&selinux_state,
1848 newsid, sbsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 SECCLASS_FILESYSTEM,
1850 FILESYSTEM__ASSOCIATE, &ad);
1851}
1852
Eric Paris828dfe12008-04-17 13:17:49 -04001853#define MAY_LINK 0
1854#define MAY_UNLINK 1
1855#define MAY_RMDIR 2
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
1857/* Check whether a task can link, unlink, or rmdir a file/directory. */
1858static int may_link(struct inode *dir,
1859 struct dentry *dentry,
1860 int kind)
1861
1862{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 struct inode_security_struct *dsec, *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001864 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11001865 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 u32 av;
1867 int rc;
1868
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001869 dsec = inode_security(dir);
1870 isec = backing_inode_security(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
Eric Paris50c205f2012-04-04 15:01:43 -04001872 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04001873 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
1875 av = DIR__SEARCH;
1876 av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001877 rc = avc_has_perm(&selinux_state,
1878 sid, dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 if (rc)
1880 return rc;
1881
1882 switch (kind) {
1883 case MAY_LINK:
1884 av = FILE__LINK;
1885 break;
1886 case MAY_UNLINK:
1887 av = FILE__UNLINK;
1888 break;
1889 case MAY_RMDIR:
1890 av = DIR__RMDIR;
1891 break;
1892 default:
peter enderborgc103a912018-06-12 10:09:03 +02001893 pr_warn("SELinux: %s: unrecognized kind %d\n",
Eric Paris744ba352008-04-17 11:52:44 -04001894 __func__, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 return 0;
1896 }
1897
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001898 rc = avc_has_perm(&selinux_state,
1899 sid, isec->sid, isec->sclass, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 return rc;
1901}
1902
1903static inline int may_rename(struct inode *old_dir,
1904 struct dentry *old_dentry,
1905 struct inode *new_dir,
1906 struct dentry *new_dentry)
1907{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001909 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11001910 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 u32 av;
1912 int old_is_dir, new_is_dir;
1913 int rc;
1914
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001915 old_dsec = inode_security(old_dir);
1916 old_isec = backing_inode_security(old_dentry);
David Howellse36cb0b2015-01-29 12:02:35 +00001917 old_is_dir = d_is_dir(old_dentry);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001918 new_dsec = inode_security(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
Eric Paris50c205f2012-04-04 15:01:43 -04001920 ad.type = LSM_AUDIT_DATA_DENTRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
Eric Parisa2694342011-04-25 13:10:27 -04001922 ad.u.dentry = old_dentry;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001923 rc = avc_has_perm(&selinux_state,
1924 sid, old_dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 DIR__REMOVE_NAME | DIR__SEARCH, &ad);
1926 if (rc)
1927 return rc;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001928 rc = avc_has_perm(&selinux_state,
1929 sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 old_isec->sclass, FILE__RENAME, &ad);
1931 if (rc)
1932 return rc;
1933 if (old_is_dir && new_dir != old_dir) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001934 rc = avc_has_perm(&selinux_state,
1935 sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 old_isec->sclass, DIR__REPARENT, &ad);
1937 if (rc)
1938 return rc;
1939 }
1940
Eric Parisa2694342011-04-25 13:10:27 -04001941 ad.u.dentry = new_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 av = DIR__ADD_NAME | DIR__SEARCH;
David Howells2c616d42015-01-29 12:02:33 +00001943 if (d_is_positive(new_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 av |= DIR__REMOVE_NAME;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001945 rc = avc_has_perm(&selinux_state,
1946 sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 if (rc)
1948 return rc;
David Howells2c616d42015-01-29 12:02:33 +00001949 if (d_is_positive(new_dentry)) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001950 new_isec = backing_inode_security(new_dentry);
David Howellse36cb0b2015-01-29 12:02:35 +00001951 new_is_dir = d_is_dir(new_dentry);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001952 rc = avc_has_perm(&selinux_state,
1953 sid, new_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 new_isec->sclass,
1955 (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
1956 if (rc)
1957 return rc;
1958 }
1959
1960 return 0;
1961}
1962
1963/* Check whether a task can perform a filesystem operation. */
David Howells88e67f32008-11-14 10:39:21 +11001964static int superblock_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 struct super_block *sb,
1966 u32 perms,
Thomas Liu2bf49692009-07-14 12:14:09 -04001967 struct common_audit_data *ad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 struct superblock_security_struct *sbsec;
David Howells88e67f32008-11-14 10:39:21 +11001970 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 sbsec = sb->s_security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001973 return avc_has_perm(&selinux_state,
1974 sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975}
1976
1977/* Convert a Linux mode and permission mask to an access vector. */
1978static inline u32 file_mask_to_av(int mode, int mask)
1979{
1980 u32 av = 0;
1981
Al Virodba19c62011-07-25 20:49:29 -04001982 if (!S_ISDIR(mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 if (mask & MAY_EXEC)
1984 av |= FILE__EXECUTE;
1985 if (mask & MAY_READ)
1986 av |= FILE__READ;
1987
1988 if (mask & MAY_APPEND)
1989 av |= FILE__APPEND;
1990 else if (mask & MAY_WRITE)
1991 av |= FILE__WRITE;
1992
1993 } else {
1994 if (mask & MAY_EXEC)
1995 av |= DIR__SEARCH;
1996 if (mask & MAY_WRITE)
1997 av |= DIR__WRITE;
1998 if (mask & MAY_READ)
1999 av |= DIR__READ;
2000 }
2001
2002 return av;
2003}
2004
2005/* Convert a Linux file to an access vector. */
2006static inline u32 file_to_av(struct file *file)
2007{
2008 u32 av = 0;
2009
2010 if (file->f_mode & FMODE_READ)
2011 av |= FILE__READ;
2012 if (file->f_mode & FMODE_WRITE) {
2013 if (file->f_flags & O_APPEND)
2014 av |= FILE__APPEND;
2015 else
2016 av |= FILE__WRITE;
2017 }
Stephen Smalley0794c662008-03-17 08:55:18 -04002018 if (!av) {
2019 /*
2020 * Special file opened with flags 3 for ioctl-only use.
2021 */
2022 av = FILE__IOCTL;
2023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
2025 return av;
2026}
2027
Eric Paris8b6a5a32008-10-29 17:06:46 -04002028/*
2029 * Convert a file to an access vector and include the correct open
2030 * open permission.
2031 */
2032static inline u32 open_file_to_av(struct file *file)
2033{
2034 u32 av = file_to_av(file);
Stephen Smalleyccb54472017-05-12 12:41:24 -04002035 struct inode *inode = file_inode(file);
Eric Paris8b6a5a32008-10-29 17:06:46 -04002036
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002037 if (selinux_policycap_openperm() &&
2038 inode->i_sb->s_magic != SOCKFS_MAGIC)
Eric Paris49b7b8d2010-07-23 11:44:09 -04002039 av |= FILE__OPEN;
2040
Eric Paris8b6a5a32008-10-29 17:06:46 -04002041 return av;
2042}
2043
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044/* Hook functions begin here. */
2045
Stephen Smalley79af7302015-01-21 10:54:10 -05002046static int selinux_binder_set_context_mgr(struct task_struct *mgr)
2047{
2048 u32 mysid = current_sid();
2049 u32 mgrsid = task_sid(mgr);
2050
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002051 return avc_has_perm(&selinux_state,
2052 mysid, mgrsid, SECCLASS_BINDER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002053 BINDER__SET_CONTEXT_MGR, NULL);
2054}
2055
2056static int selinux_binder_transaction(struct task_struct *from,
2057 struct task_struct *to)
2058{
2059 u32 mysid = current_sid();
2060 u32 fromsid = task_sid(from);
2061 u32 tosid = task_sid(to);
2062 int rc;
2063
2064 if (mysid != fromsid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002065 rc = avc_has_perm(&selinux_state,
2066 mysid, fromsid, SECCLASS_BINDER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002067 BINDER__IMPERSONATE, NULL);
2068 if (rc)
2069 return rc;
2070 }
2071
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002072 return avc_has_perm(&selinux_state,
2073 fromsid, tosid, SECCLASS_BINDER, BINDER__CALL,
Stephen Smalley79af7302015-01-21 10:54:10 -05002074 NULL);
2075}
2076
2077static int selinux_binder_transfer_binder(struct task_struct *from,
2078 struct task_struct *to)
2079{
2080 u32 fromsid = task_sid(from);
2081 u32 tosid = task_sid(to);
2082
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002083 return avc_has_perm(&selinux_state,
2084 fromsid, tosid, SECCLASS_BINDER, BINDER__TRANSFER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002085 NULL);
2086}
2087
2088static int selinux_binder_transfer_file(struct task_struct *from,
2089 struct task_struct *to,
2090 struct file *file)
2091{
2092 u32 sid = task_sid(to);
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07002093 struct file_security_struct *fsec = selinux_file(file);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002094 struct dentry *dentry = file->f_path.dentry;
Paul Moore20cdef82016-04-04 14:14:42 -04002095 struct inode_security_struct *isec;
Stephen Smalley79af7302015-01-21 10:54:10 -05002096 struct common_audit_data ad;
2097 int rc;
2098
2099 ad.type = LSM_AUDIT_DATA_PATH;
2100 ad.u.path = file->f_path;
2101
2102 if (sid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002103 rc = avc_has_perm(&selinux_state,
2104 sid, fsec->sid,
Stephen Smalley79af7302015-01-21 10:54:10 -05002105 SECCLASS_FD,
2106 FD__USE,
2107 &ad);
2108 if (rc)
2109 return rc;
2110 }
2111
Chenbo Fengf66e4482017-10-18 13:00:26 -07002112#ifdef CONFIG_BPF_SYSCALL
2113 rc = bpf_fd_pass(file, sid);
2114 if (rc)
2115 return rc;
2116#endif
2117
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002118 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
Stephen Smalley79af7302015-01-21 10:54:10 -05002119 return 0;
2120
Paul Moore20cdef82016-04-04 14:14:42 -04002121 isec = backing_inode_security(dentry);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002122 return avc_has_perm(&selinux_state,
2123 sid, isec->sid, isec->sclass, file_to_av(file),
Stephen Smalley79af7302015-01-21 10:54:10 -05002124 &ad);
2125}
2126
Ingo Molnar9e488582009-05-07 19:26:19 +10002127static int selinux_ptrace_access_check(struct task_struct *child,
David Howells5cd9c582008-08-14 11:37:28 +01002128 unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002130 u32 sid = current_sid();
2131 u32 csid = task_sid(child);
Stephen Smalley006ebb42008-05-19 08:32:49 -04002132
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002133 if (mode & PTRACE_MODE_READ)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002134 return avc_has_perm(&selinux_state,
2135 sid, csid, SECCLASS_FILE, FILE__READ, NULL);
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002136
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002137 return avc_has_perm(&selinux_state,
2138 sid, csid, SECCLASS_PROCESS, PROCESS__PTRACE, NULL);
David Howells5cd9c582008-08-14 11:37:28 +01002139}
2140
2141static int selinux_ptrace_traceme(struct task_struct *parent)
2142{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002143 return avc_has_perm(&selinux_state,
2144 task_sid(parent), current_sid(), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002145 PROCESS__PTRACE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146}
2147
2148static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
Eric Paris828dfe12008-04-17 13:17:49 -04002149 kernel_cap_t *inheritable, kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002151 return avc_has_perm(&selinux_state,
2152 current_sid(), task_sid(target), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002153 PROCESS__GETCAP, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154}
2155
David Howellsd84f4f92008-11-14 10:39:23 +11002156static int selinux_capset(struct cred *new, const struct cred *old,
2157 const kernel_cap_t *effective,
2158 const kernel_cap_t *inheritable,
2159 const kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002161 return avc_has_perm(&selinux_state,
2162 cred_sid(old), cred_sid(new), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002163 PROCESS__SETCAP, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164}
2165
James Morris5626d3e2009-01-30 10:05:06 +11002166/*
2167 * (This comment used to live with the selinux_task_setuid hook,
2168 * which was removed).
2169 *
2170 * Since setuid only affects the current process, and since the SELinux
2171 * controls are not based on the Linux identity attributes, SELinux does not
2172 * need to control this operation. However, SELinux does control the use of
2173 * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
2174 */
2175
Eric Paris6a9de492012-01-03 12:25:14 -05002176static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
Micah Mortonc1a85a02019-01-07 16:10:53 -08002177 int cap, unsigned int opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178{
Micah Mortonc1a85a02019-01-07 16:10:53 -08002179 return cred_has_capability(cred, cap, opts, ns == &init_user_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180}
2181
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
2183{
David Howells88e67f32008-11-14 10:39:21 +11002184 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 int rc = 0;
2186
2187 if (!sb)
2188 return 0;
2189
2190 switch (cmds) {
Eric Paris828dfe12008-04-17 13:17:49 -04002191 case Q_SYNC:
2192 case Q_QUOTAON:
2193 case Q_QUOTAOFF:
2194 case Q_SETINFO:
2195 case Q_SETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11002196 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04002197 break;
2198 case Q_GETFMT:
2199 case Q_GETINFO:
2200 case Q_GETQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11002201 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04002202 break;
2203 default:
2204 rc = 0; /* let the kernel handle invalid cmds */
2205 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 }
2207 return rc;
2208}
2209
2210static int selinux_quota_on(struct dentry *dentry)
2211{
David Howells88e67f32008-11-14 10:39:21 +11002212 const struct cred *cred = current_cred();
2213
Eric Paris2875fa02011-04-28 16:04:24 -04002214 return dentry_has_perm(cred, dentry, FILE__QUOTAON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215}
2216
Eric Paris12b30522010-11-15 18:36:29 -05002217static int selinux_syslog(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 switch (type) {
Kees Cookd78ca3c2010-02-03 15:37:13 -08002220 case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */
2221 case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002222 return avc_has_perm(&selinux_state,
2223 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002224 SECCLASS_SYSTEM, SYSTEM__SYSLOG_READ, NULL);
Kees Cookd78ca3c2010-02-03 15:37:13 -08002225 case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */
2226 case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */
2227 /* Set level of messages printed to console */
2228 case SYSLOG_ACTION_CONSOLE_LEVEL:
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002229 return avc_has_perm(&selinux_state,
2230 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002231 SECCLASS_SYSTEM, SYSTEM__SYSLOG_CONSOLE,
2232 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 }
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002234 /* All other syslog types */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002235 return avc_has_perm(&selinux_state,
2236 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002237 SECCLASS_SYSTEM, SYSTEM__SYSLOG_MOD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238}
2239
2240/*
2241 * Check that a process has enough memory to allocate a new virtual
2242 * mapping. 0 means there is enough memory for the allocation to
2243 * succeed and -ENOMEM implies there is not.
2244 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 * Do not audit the selinux permission check, as this is applied to all
2246 * processes that allocate mappings.
2247 */
Alan Cox34b4e4a2007-08-22 14:01:28 -07002248static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249{
2250 int rc, cap_sys_admin = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002252 rc = cred_has_capability(current_cred(), CAP_SYS_ADMIN,
Micah Mortonc1a85a02019-01-07 16:10:53 -08002253 CAP_OPT_NOAUDIT, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 if (rc == 0)
2255 cap_sys_admin = 1;
2256
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002257 return cap_sys_admin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258}
2259
2260/* binprm security operations */
2261
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002262static u32 ptrace_parent_sid(void)
Paul Moore0c6181c2016-03-30 21:41:21 -04002263{
2264 u32 sid = 0;
2265 struct task_struct *tracer;
2266
2267 rcu_read_lock();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002268 tracer = ptrace_parent(current);
Paul Moore0c6181c2016-03-30 21:41:21 -04002269 if (tracer)
2270 sid = task_sid(tracer);
2271 rcu_read_unlock();
2272
2273 return sid;
2274}
2275
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002276static int check_nnp_nosuid(const struct linux_binprm *bprm,
2277 const struct task_security_struct *old_tsec,
2278 const struct task_security_struct *new_tsec)
2279{
2280 int nnp = (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS);
Andy Lutomirski380cf5b2016-06-23 16:41:05 -05002281 int nosuid = !mnt_may_suid(bprm->file->f_path.mnt);
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002282 int rc;
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002283 u32 av;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002284
2285 if (!nnp && !nosuid)
2286 return 0; /* neither NNP nor nosuid */
2287
2288 if (new_tsec->sid == old_tsec->sid)
2289 return 0; /* No change in credentials */
2290
2291 /*
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002292 * If the policy enables the nnp_nosuid_transition policy capability,
2293 * then we permit transitions under NNP or nosuid if the
2294 * policy allows the corresponding permission between
2295 * the old and new contexts.
2296 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002297 if (selinux_policycap_nnp_nosuid_transition()) {
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002298 av = 0;
2299 if (nnp)
2300 av |= PROCESS2__NNP_TRANSITION;
2301 if (nosuid)
2302 av |= PROCESS2__NOSUID_TRANSITION;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002303 rc = avc_has_perm(&selinux_state,
2304 old_tsec->sid, new_tsec->sid,
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002305 SECCLASS_PROCESS2, av, NULL);
2306 if (!rc)
2307 return 0;
2308 }
2309
2310 /*
2311 * We also permit NNP or nosuid transitions to bounded SIDs,
2312 * i.e. SIDs that are guaranteed to only be allowed a subset
2313 * of the permissions of the current SID.
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002314 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002315 rc = security_bounded_transition(&selinux_state, old_tsec->sid,
2316 new_tsec->sid);
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002317 if (!rc)
2318 return 0;
2319
2320 /*
2321 * On failure, preserve the errno values for NNP vs nosuid.
2322 * NNP: Operation not permitted for caller.
2323 * nosuid: Permission denied to file.
2324 */
2325 if (nnp)
2326 return -EPERM;
2327 return -EACCES;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002328}
2329
David Howellsa6f76f22008-11-14 10:39:24 +11002330static int selinux_bprm_set_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331{
David Howellsa6f76f22008-11-14 10:39:24 +11002332 const struct task_security_struct *old_tsec;
2333 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 struct inode_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04002335 struct common_audit_data ad;
Al Viro496ad9a2013-01-23 17:07:38 -05002336 struct inode *inode = file_inode(bprm->file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 int rc;
2338
David Howellsa6f76f22008-11-14 10:39:24 +11002339 /* SELinux context only depends on initial program or script and not
2340 * the script interpreter */
Kees Cookddb4a142017-07-18 15:25:23 -07002341 if (bprm->called_set_creds)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 return 0;
2343
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002344 old_tsec = selinux_cred(current_cred());
2345 new_tsec = selinux_cred(bprm->cred);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002346 isec = inode_security(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
2348 /* Default to the current task SID. */
David Howellsa6f76f22008-11-14 10:39:24 +11002349 new_tsec->sid = old_tsec->sid;
2350 new_tsec->osid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
Michael LeMay28eba5b2006-06-27 02:53:42 -07002352 /* Reset fs, key, and sock SIDs on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002353 new_tsec->create_sid = 0;
2354 new_tsec->keycreate_sid = 0;
2355 new_tsec->sockcreate_sid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356
David Howellsa6f76f22008-11-14 10:39:24 +11002357 if (old_tsec->exec_sid) {
2358 new_tsec->sid = old_tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 /* Reset exec SID on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002360 new_tsec->exec_sid = 0;
Andy Lutomirski259e5e62012-04-12 16:47:50 -05002361
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002362 /* Fail on NNP or nosuid if not an allowed transition. */
2363 rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
2364 if (rc)
2365 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 } else {
2367 /* Check for a default transition on this program. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002368 rc = security_transition_sid(&selinux_state, old_tsec->sid,
2369 isec->sid, SECCLASS_PROCESS, NULL,
Eric Paris652bb9b2011-02-01 11:05:40 -05002370 &new_tsec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 if (rc)
2372 return rc;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002373
2374 /*
2375 * Fallback to old SID on NNP or nosuid if not an allowed
2376 * transition.
2377 */
2378 rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
2379 if (rc)
2380 new_tsec->sid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 }
2382
Vivek Goyal43af5de2016-09-09 11:37:49 -04002383 ad.type = LSM_AUDIT_DATA_FILE;
2384 ad.u.file = bprm->file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
David Howellsa6f76f22008-11-14 10:39:24 +11002386 if (new_tsec->sid == old_tsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002387 rc = avc_has_perm(&selinux_state,
2388 old_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
2390 if (rc)
2391 return rc;
2392 } else {
2393 /* Check permissions for the transition. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002394 rc = avc_has_perm(&selinux_state,
2395 old_tsec->sid, new_tsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
2397 if (rc)
2398 return rc;
2399
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002400 rc = avc_has_perm(&selinux_state,
2401 new_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
2403 if (rc)
2404 return rc;
2405
David Howellsa6f76f22008-11-14 10:39:24 +11002406 /* Check for shared state */
2407 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002408 rc = avc_has_perm(&selinux_state,
2409 old_tsec->sid, new_tsec->sid,
David Howellsa6f76f22008-11-14 10:39:24 +11002410 SECCLASS_PROCESS, PROCESS__SHARE,
2411 NULL);
2412 if (rc)
2413 return -EPERM;
2414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415
David Howellsa6f76f22008-11-14 10:39:24 +11002416 /* Make sure that anyone attempting to ptrace over a task that
2417 * changes its SID has the appropriate permit */
Eric W. Biederman9227dd22017-01-23 17:26:31 +13002418 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002419 u32 ptsid = ptrace_parent_sid();
David Howellsa6f76f22008-11-14 10:39:24 +11002420 if (ptsid != 0) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002421 rc = avc_has_perm(&selinux_state,
2422 ptsid, new_tsec->sid,
David Howellsa6f76f22008-11-14 10:39:24 +11002423 SECCLASS_PROCESS,
2424 PROCESS__PTRACE, NULL);
2425 if (rc)
2426 return -EPERM;
2427 }
2428 }
2429
2430 /* Clear any possibly unsafe personality bits on exec: */
2431 bprm->per_clear |= PER_CLEAR_ON_SETID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 /* Enable secure mode for SIDs transitions unless
2434 the noatsecure permission is granted between
2435 the two SIDs, i.e. ahp returns 0. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002436 rc = avc_has_perm(&selinux_state,
2437 old_tsec->sid, new_tsec->sid,
Kees Cook62874c32017-07-18 15:25:25 -07002438 SECCLASS_PROCESS, PROCESS__NOATSECURE,
2439 NULL);
2440 bprm->secureexec |= !!rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 }
2442
Kees Cook62874c32017-07-18 15:25:25 -07002443 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444}
2445
Al Viroc3c073f2012-08-21 22:32:06 -04002446static int match_file(const void *p, struct file *file, unsigned fd)
2447{
2448 return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0;
2449}
2450
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451/* Derived from fs/exec.c:flush_old_files. */
David Howells745ca242008-11-14 10:39:22 +11002452static inline void flush_unauthorized_files(const struct cred *cred,
2453 struct files_struct *files)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 struct file *file, *devnull = NULL;
Stephen Smalleyb20c8122006-09-25 23:32:03 -07002456 struct tty_struct *tty;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002457 int drop_tty = 0;
Al Viroc3c073f2012-08-21 22:32:06 -04002458 unsigned n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002460 tty = get_current_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 if (tty) {
Peter Hurley4a510962016-01-09 21:35:23 -08002462 spin_lock(&tty->files_lock);
Eric Paris37dd0bd2008-10-31 17:40:00 -04002463 if (!list_empty(&tty->tty_files)) {
Nick Piggind996b622010-08-18 04:37:36 +10002464 struct tty_file_private *file_priv;
Eric Paris37dd0bd2008-10-31 17:40:00 -04002465
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 /* Revalidate access to controlling tty.
David Howells13f8e982013-06-13 23:37:55 +01002467 Use file_path_has_perm on the tty path directly
2468 rather than using file_has_perm, as this particular
2469 open file may belong to another process and we are
2470 only interested in the inode-based check here. */
Nick Piggind996b622010-08-18 04:37:36 +10002471 file_priv = list_first_entry(&tty->tty_files,
2472 struct tty_file_private, list);
2473 file = file_priv->file;
David Howells13f8e982013-06-13 23:37:55 +01002474 if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE))
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002475 drop_tty = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 }
Peter Hurley4a510962016-01-09 21:35:23 -08002477 spin_unlock(&tty->files_lock);
Alan Cox452a00d2008-10-13 10:39:13 +01002478 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 }
Eric W. Biederman98a27ba2007-05-08 00:26:56 -07002480 /* Reset controlling tty. */
2481 if (drop_tty)
2482 no_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
2484 /* Revalidate access to inherited open files. */
Al Viroc3c073f2012-08-21 22:32:06 -04002485 n = iterate_fd(files, 0, match_file, cred);
2486 if (!n) /* none found? */
2487 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
Al Viroc3c073f2012-08-21 22:32:06 -04002489 devnull = dentry_open(&selinux_null, O_RDWR, cred);
Al Viro45525b22012-10-16 13:30:07 -04002490 if (IS_ERR(devnull))
2491 devnull = NULL;
2492 /* replace all the matching ones with this */
2493 do {
2494 replace_fd(n - 1, devnull, 0);
2495 } while ((n = iterate_fd(files, n, match_file, cred)) != 0);
2496 if (devnull)
Al Viroc3c073f2012-08-21 22:32:06 -04002497 fput(devnull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498}
2499
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500/*
David Howellsa6f76f22008-11-14 10:39:24 +11002501 * Prepare a process for imminent new credential changes due to exec
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 */
David Howellsa6f76f22008-11-14 10:39:24 +11002503static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504{
David Howellsa6f76f22008-11-14 10:39:24 +11002505 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 struct rlimit *rlim, *initrlim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 int rc, i;
2508
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002509 new_tsec = selinux_cred(bprm->cred);
David Howellsa6f76f22008-11-14 10:39:24 +11002510 if (new_tsec->sid == new_tsec->osid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 return;
2512
2513 /* Close files for which the new task SID is not authorized. */
David Howellsa6f76f22008-11-14 10:39:24 +11002514 flush_unauthorized_files(bprm->cred, current->files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515
David Howellsa6f76f22008-11-14 10:39:24 +11002516 /* Always clear parent death signal on SID transitions. */
2517 current->pdeath_signal = 0;
2518
2519 /* Check whether the new SID can inherit resource limits from the old
2520 * SID. If not, reset all soft limits to the lower of the current
2521 * task's hard limit and the init task's soft limit.
2522 *
2523 * Note that the setting of hard limits (even to lower them) can be
2524 * controlled by the setrlimit check. The inclusion of the init task's
2525 * soft limit into the computation is to avoid resetting soft limits
2526 * higher than the default soft limit for cases where the default is
2527 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2528 */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002529 rc = avc_has_perm(&selinux_state,
2530 new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
David Howellsa6f76f22008-11-14 10:39:24 +11002531 PROCESS__RLIMITINH, NULL);
2532 if (rc) {
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002533 /* protect against do_prlimit() */
2534 task_lock(current);
David Howellsa6f76f22008-11-14 10:39:24 +11002535 for (i = 0; i < RLIM_NLIMITS; i++) {
2536 rlim = current->signal->rlim + i;
2537 initrlim = init_task.signal->rlim + i;
2538 rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
2539 }
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002540 task_unlock(current);
Nicolas Pitrebaa73d92016-11-11 00:10:10 -05002541 if (IS_ENABLED(CONFIG_POSIX_TIMERS))
2542 update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
David Howellsa6f76f22008-11-14 10:39:24 +11002543 }
2544}
2545
2546/*
2547 * Clean up the process immediately after the installation of new credentials
2548 * due to exec
2549 */
2550static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
2551{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002552 const struct task_security_struct *tsec = selinux_cred(current_cred());
David Howellsa6f76f22008-11-14 10:39:24 +11002553 struct itimerval itimer;
David Howellsa6f76f22008-11-14 10:39:24 +11002554 u32 osid, sid;
2555 int rc, i;
David Howellsa6f76f22008-11-14 10:39:24 +11002556
David Howellsa6f76f22008-11-14 10:39:24 +11002557 osid = tsec->osid;
2558 sid = tsec->sid;
2559
2560 if (sid == osid)
2561 return;
2562
2563 /* Check whether the new SID can inherit signal state from the old SID.
2564 * If not, clear itimers to avoid subsequent signal generation and
2565 * flush and unblock signals.
2566 *
2567 * This must occur _after_ the task SID has been updated so that any
2568 * kill done after the flush will be checked against the new SID.
2569 */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002570 rc = avc_has_perm(&selinux_state,
2571 osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 if (rc) {
Nicolas Pitrebaa73d92016-11-11 00:10:10 -05002573 if (IS_ENABLED(CONFIG_POSIX_TIMERS)) {
2574 memset(&itimer, 0, sizeof itimer);
2575 for (i = 0; i < 3; i++)
2576 do_setitimer(i, &itimer, NULL);
2577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterov9e7c8f82015-06-04 16:22:16 -04002579 if (!fatal_signal_pending(current)) {
2580 flush_sigqueue(&current->pending);
2581 flush_sigqueue(&current->signal->shared_pending);
David Howells3bcac022009-04-29 13:45:05 +01002582 flush_signal_handlers(current, 1);
2583 sigemptyset(&current->blocked);
Oleg Nesterov9e7c8f82015-06-04 16:22:16 -04002584 recalc_sigpending();
David Howells3bcac022009-04-29 13:45:05 +01002585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 spin_unlock_irq(&current->sighand->siglock);
2587 }
2588
David Howellsa6f76f22008-11-14 10:39:24 +11002589 /* Wake up the parent if it is waiting so that it can recheck
2590 * wait permission to the new task SID. */
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002591 read_lock(&tasklist_lock);
Oleg Nesterov0b7570e2009-09-23 15:56:46 -07002592 __wake_up_parent(current, current->real_parent);
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002593 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594}
2595
2596/* superblock security operations */
2597
2598static int selinux_sb_alloc_security(struct super_block *sb)
2599{
2600 return superblock_alloc_security(sb);
2601}
2602
2603static void selinux_sb_free_security(struct super_block *sb)
2604{
2605 superblock_free_security(sb);
2606}
2607
Al Viro99dbbb52018-12-14 21:56:23 -05002608static inline int opt_len(const char *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609{
Al Viro99dbbb52018-12-14 21:56:23 -05002610 bool open_quote = false;
2611 int len;
2612 char c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613
Al Viro99dbbb52018-12-14 21:56:23 -05002614 for (len = 0; (c = s[len]) != '\0'; len++) {
2615 if (c == '"')
Cory Olmo3528a952006-09-29 01:58:44 -07002616 open_quote = !open_quote;
Al Viro99dbbb52018-12-14 21:56:23 -05002617 if (c == ',' && !open_quote)
2618 break;
2619 }
2620 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621}
2622
Al Viro204cc0c2018-12-13 13:41:47 -05002623static int selinux_sb_eat_lsm_opts(char *options, void **mnt_opts)
Eric Paris026eb162011-03-03 16:09:14 -05002624{
Al Viro99dbbb52018-12-14 21:56:23 -05002625 char *from = options;
2626 char *to = options;
2627 bool first = true;
Gen Zhangfec63752019-06-12 21:55:38 +08002628 int rc;
Al Viro5b400232018-12-12 20:13:29 -05002629
Al Viro99dbbb52018-12-14 21:56:23 -05002630 while (1) {
2631 int len = opt_len(from);
Gen Zhangfec63752019-06-12 21:55:38 +08002632 int token;
Al Viro99dbbb52018-12-14 21:56:23 -05002633 char *arg = NULL;
2634
2635 token = match_opt_prefix(from, len, &arg);
2636
2637 if (token != Opt_error) {
2638 char *p, *q;
2639
2640 /* strip quotes */
2641 if (arg) {
2642 for (p = q = arg; p < from + len; p++) {
2643 char c = *p;
2644 if (c != '"')
2645 *q++ = c;
2646 }
2647 arg = kmemdup_nul(arg, q - arg, GFP_KERNEL);
Gen Zhangfec63752019-06-12 21:55:38 +08002648 if (!arg) {
2649 rc = -ENOMEM;
2650 goto free_opt;
2651 }
Al Viro99dbbb52018-12-14 21:56:23 -05002652 }
2653 rc = selinux_add_opt(token, arg, mnt_opts);
2654 if (unlikely(rc)) {
2655 kfree(arg);
Gen Zhangfec63752019-06-12 21:55:38 +08002656 goto free_opt;
Al Viro99dbbb52018-12-14 21:56:23 -05002657 }
2658 } else {
2659 if (!first) { // copy with preceding comma
2660 from--;
2661 len++;
2662 }
2663 if (to != from)
2664 memmove(to, from, len);
2665 to += len;
2666 first = false;
2667 }
2668 if (!from[len])
2669 break;
2670 from += len + 1;
2671 }
2672 *to = '\0';
2673 return 0;
Gen Zhangfec63752019-06-12 21:55:38 +08002674
2675free_opt:
2676 if (*mnt_opts) {
2677 selinux_free_mnt_opts(*mnt_opts);
2678 *mnt_opts = NULL;
2679 }
2680 return rc;
Al Viro5b400232018-12-12 20:13:29 -05002681}
2682
Al Viro204cc0c2018-12-13 13:41:47 -05002683static int selinux_sb_remount(struct super_block *sb, void *mnt_opts)
Eric Paris026eb162011-03-03 16:09:14 -05002684{
Al Virobd323652018-12-13 15:04:59 -05002685 struct selinux_mnt_opts *opts = mnt_opts;
Eric Paris026eb162011-03-03 16:09:14 -05002686 struct superblock_security_struct *sbsec = sb->s_security;
Al Virobd323652018-12-13 15:04:59 -05002687 u32 sid;
2688 int rc;
Eric Paris026eb162011-03-03 16:09:14 -05002689
2690 if (!(sbsec->flags & SE_SBINITIALIZED))
2691 return 0;
2692
Al Viro204cc0c2018-12-13 13:41:47 -05002693 if (!opts)
Eric Paris026eb162011-03-03 16:09:14 -05002694 return 0;
2695
Al Virobd323652018-12-13 15:04:59 -05002696 if (opts->fscontext) {
2697 rc = parse_sid(sb, opts->fscontext, &sid);
2698 if (rc)
Al Viroc039bc32018-12-01 23:06:57 -05002699 return rc;
Al Virobd323652018-12-13 15:04:59 -05002700 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
2701 goto out_bad_option;
Eric Paris026eb162011-03-03 16:09:14 -05002702 }
Al Virobd323652018-12-13 15:04:59 -05002703 if (opts->context) {
2704 rc = parse_sid(sb, opts->context, &sid);
2705 if (rc)
2706 return rc;
2707 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
2708 goto out_bad_option;
2709 }
2710 if (opts->rootcontext) {
2711 struct inode_security_struct *root_isec;
2712 root_isec = backing_inode_security(sb->s_root);
2713 rc = parse_sid(sb, opts->rootcontext, &sid);
2714 if (rc)
2715 return rc;
2716 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
2717 goto out_bad_option;
2718 }
2719 if (opts->defcontext) {
2720 rc = parse_sid(sb, opts->defcontext, &sid);
2721 if (rc)
2722 return rc;
2723 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
2724 goto out_bad_option;
Eric Paris026eb162011-03-03 16:09:14 -05002725 }
Al Viroc039bc32018-12-01 23:06:57 -05002726 return 0;
Eric Paris026eb162011-03-03 16:09:14 -05002727
Eric Paris026eb162011-03-03 16:09:14 -05002728out_bad_option:
peter enderborgc103a912018-06-12 10:09:03 +02002729 pr_warn("SELinux: unable to change security options "
Linus Torvalds29b1deb2013-12-15 11:17:45 -08002730 "during remount (dev %s, type=%s)\n", sb->s_id,
2731 sb->s_type->name);
Al Viroc039bc32018-12-01 23:06:57 -05002732 return -EINVAL;
Eric Paris026eb162011-03-03 16:09:14 -05002733}
2734
Al Viroa10d7c22018-12-05 11:58:35 -05002735static int selinux_sb_kern_mount(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736{
David Howells88e67f32008-11-14 10:39:21 +11002737 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002738 struct common_audit_data ad;
James Morris74192242008-12-19 11:41:10 +11002739
Eric Paris50c205f2012-04-04 15:01:43 -04002740 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002741 ad.u.dentry = sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002742 return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743}
2744
David Howells726c3342006-06-23 02:02:58 -07002745static int selinux_sb_statfs(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746{
David Howells88e67f32008-11-14 10:39:21 +11002747 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002748 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749
Eric Paris50c205f2012-04-04 15:01:43 -04002750 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002751 ad.u.dentry = dentry->d_sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002752 return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753}
2754
Al Viro808d4e32012-10-11 11:42:01 -04002755static int selinux_mount(const char *dev_name,
Al Viro8a04c432016-03-25 14:52:53 -04002756 const struct path *path,
Al Viro808d4e32012-10-11 11:42:01 -04002757 const char *type,
Eric Paris828dfe12008-04-17 13:17:49 -04002758 unsigned long flags,
2759 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760{
David Howells88e67f32008-11-14 10:39:21 +11002761 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762
2763 if (flags & MS_REMOUNT)
Al Virod8c95842011-12-07 18:16:57 -05002764 return superblock_has_perm(cred, path->dentry->d_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002765 FILESYSTEM__REMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 else
Eric Paris2875fa02011-04-28 16:04:24 -04002767 return path_has_perm(cred, path, FILE__MOUNTON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768}
2769
2770static int selinux_umount(struct vfsmount *mnt, int flags)
2771{
David Howells88e67f32008-11-14 10:39:21 +11002772 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773
David Howells88e67f32008-11-14 10:39:21 +11002774 return superblock_has_perm(cred, mnt->mnt_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002775 FILESYSTEM__UNMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776}
2777
Al Viro0b520752018-12-23 16:02:47 -05002778static int selinux_fs_context_dup(struct fs_context *fc,
2779 struct fs_context *src_fc)
2780{
2781 const struct selinux_mnt_opts *src = src_fc->security;
2782 struct selinux_mnt_opts *opts;
2783
2784 if (!src)
2785 return 0;
2786
2787 fc->security = kzalloc(sizeof(struct selinux_mnt_opts), GFP_KERNEL);
2788 if (!fc->security)
2789 return -ENOMEM;
2790
2791 opts = fc->security;
2792
2793 if (src->fscontext) {
2794 opts->fscontext = kstrdup(src->fscontext, GFP_KERNEL);
2795 if (!opts->fscontext)
2796 return -ENOMEM;
2797 }
2798 if (src->context) {
2799 opts->context = kstrdup(src->context, GFP_KERNEL);
2800 if (!opts->context)
2801 return -ENOMEM;
2802 }
2803 if (src->rootcontext) {
2804 opts->rootcontext = kstrdup(src->rootcontext, GFP_KERNEL);
2805 if (!opts->rootcontext)
2806 return -ENOMEM;
2807 }
2808 if (src->defcontext) {
2809 opts->defcontext = kstrdup(src->defcontext, GFP_KERNEL);
2810 if (!opts->defcontext)
2811 return -ENOMEM;
2812 }
2813 return 0;
2814}
2815
David Howells442155c2018-11-01 23:07:24 +00002816static const struct fs_parameter_spec selinux_param_specs[] = {
2817 fsparam_string(CONTEXT_STR, Opt_context),
2818 fsparam_string(DEFCONTEXT_STR, Opt_defcontext),
2819 fsparam_string(FSCONTEXT_STR, Opt_fscontext),
2820 fsparam_string(ROOTCONTEXT_STR, Opt_rootcontext),
2821 fsparam_flag (SECLABEL_STR, Opt_seclabel),
2822 {}
2823};
2824
2825static const struct fs_parameter_description selinux_fs_parameters = {
2826 .name = "SELinux",
2827 .specs = selinux_param_specs,
2828};
2829
2830static int selinux_fs_context_parse_param(struct fs_context *fc,
2831 struct fs_parameter *param)
2832{
2833 struct fs_parse_result result;
2834 int opt, rc;
2835
2836 opt = fs_parse(fc, &selinux_fs_parameters, param, &result);
2837 if (opt < 0)
2838 return opt;
2839
2840 rc = selinux_add_opt(opt, param->string, &fc->security);
2841 if (!rc) {
2842 param->string = NULL;
2843 rc = 1;
2844 }
2845 return rc;
2846}
2847
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848/* inode security operations */
2849
2850static int selinux_inode_alloc_security(struct inode *inode)
2851{
2852 return inode_alloc_security(inode);
2853}
2854
2855static void selinux_inode_free_security(struct inode *inode)
2856{
2857 inode_free_security(inode);
2858}
2859
David Quigleyd47be3d2013-05-22 12:50:34 -04002860static int selinux_dentry_init_security(struct dentry *dentry, int mode,
Al Viro4f3ccd72016-07-20 16:06:15 -04002861 const struct qstr *name, void **ctx,
David Quigleyd47be3d2013-05-22 12:50:34 -04002862 u32 *ctxlen)
2863{
David Quigleyd47be3d2013-05-22 12:50:34 -04002864 u32 newsid;
2865 int rc;
2866
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002867 rc = selinux_determine_inode_label(selinux_cred(current_cred()),
Vivek Goyalc957f6d2016-07-13 10:44:51 -04002868 d_inode(dentry->d_parent), name,
David Howellsc3c188b2015-07-10 17:19:58 -04002869 inode_mode_to_security_class(mode),
2870 &newsid);
2871 if (rc)
2872 return rc;
David Quigleyd47be3d2013-05-22 12:50:34 -04002873
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002874 return security_sid_to_context(&selinux_state, newsid, (char **)ctx,
2875 ctxlen);
David Quigleyd47be3d2013-05-22 12:50:34 -04002876}
2877
Vivek Goyala518b0a2016-07-13 10:44:53 -04002878static int selinux_dentry_create_files_as(struct dentry *dentry, int mode,
2879 struct qstr *name,
2880 const struct cred *old,
2881 struct cred *new)
2882{
2883 u32 newsid;
2884 int rc;
2885 struct task_security_struct *tsec;
2886
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002887 rc = selinux_determine_inode_label(selinux_cred(old),
Vivek Goyala518b0a2016-07-13 10:44:53 -04002888 d_inode(dentry->d_parent), name,
2889 inode_mode_to_security_class(mode),
2890 &newsid);
2891 if (rc)
2892 return rc;
2893
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002894 tsec = selinux_cred(new);
Vivek Goyala518b0a2016-07-13 10:44:53 -04002895 tsec->create_sid = newsid;
2896 return 0;
2897}
2898
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002899static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +09002900 const struct qstr *qstr,
2901 const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -05002902 void **value, size_t *len)
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002903{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002904 const struct task_security_struct *tsec = selinux_cred(current_cred());
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002905 struct superblock_security_struct *sbsec;
Corentin LABBEc0d4f462017-10-04 20:32:17 +02002906 u32 newsid, clen;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002907 int rc;
Tetsuo Handa95489062013-07-25 05:44:02 +09002908 char *context;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002909
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002910 sbsec = dir->i_sb->s_security;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002911
David Howells275bb412008-11-14 10:39:19 +11002912 newsid = tsec->create_sid;
2913
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002914 rc = selinux_determine_inode_label(selinux_cred(current_cred()),
David Howellsc3c188b2015-07-10 17:19:58 -04002915 dir, qstr,
2916 inode_mode_to_security_class(inode->i_mode),
2917 &newsid);
2918 if (rc)
2919 return rc;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002920
Eric Paris296fddf2006-09-25 23:32:00 -07002921 /* Possibly defer initialization to selinux_complete_init. */
David P. Quigley0d90a7e2009-01-16 09:22:02 -05002922 if (sbsec->flags & SE_SBINITIALIZED) {
Casey Schaufler80788c22018-09-21 17:19:11 -07002923 struct inode_security_struct *isec = selinux_inode(inode);
Eric Paris296fddf2006-09-25 23:32:00 -07002924 isec->sclass = inode_mode_to_security_class(inode->i_mode);
2925 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05002926 isec->initialized = LABEL_INITIALIZED;
Eric Paris296fddf2006-09-25 23:32:00 -07002927 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002928
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002929 if (!selinux_state.initialized || !(sbsec->flags & SBLABEL_MNT))
Stephen Smalley25a74f32005-11-08 21:34:33 -08002930 return -EOPNOTSUPP;
2931
Tetsuo Handa95489062013-07-25 05:44:02 +09002932 if (name)
2933 *name = XATTR_SELINUX_SUFFIX;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002934
2935 if (value && len) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002936 rc = security_sid_to_context_force(&selinux_state, newsid,
2937 &context, &clen);
Tetsuo Handa95489062013-07-25 05:44:02 +09002938 if (rc)
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002939 return rc;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002940 *value = context;
2941 *len = clen;
2942 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002943
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002944 return 0;
2945}
2946
Al Viro4acdaf22011-07-26 01:42:34 -04002947static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948{
2949 return may_create(dir, dentry, SECCLASS_FILE);
2950}
2951
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2953{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 return may_link(dir, old_dentry, MAY_LINK);
2955}
2956
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
2958{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 return may_link(dir, dentry, MAY_UNLINK);
2960}
2961
2962static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
2963{
2964 return may_create(dir, dentry, SECCLASS_LNK_FILE);
2965}
2966
Al Viro18bb1db2011-07-26 01:41:39 -04002967static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968{
2969 return may_create(dir, dentry, SECCLASS_DIR);
2970}
2971
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
2973{
2974 return may_link(dir, dentry, MAY_RMDIR);
2975}
2976
Al Viro1a67aaf2011-07-26 01:52:52 -04002977static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 return may_create(dir, dentry, inode_mode_to_security_class(mode));
2980}
2981
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
Eric Paris828dfe12008-04-17 13:17:49 -04002983 struct inode *new_inode, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984{
2985 return may_rename(old_inode, old_dentry, new_inode, new_dentry);
2986}
2987
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988static int selinux_inode_readlink(struct dentry *dentry)
2989{
David Howells88e67f32008-11-14 10:39:21 +11002990 const struct cred *cred = current_cred();
2991
Eric Paris2875fa02011-04-28 16:04:24 -04002992 return dentry_has_perm(cred, dentry, FILE__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993}
2994
NeilBrownbda0be72015-03-23 13:37:39 +11002995static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode,
2996 bool rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997{
David Howells88e67f32008-11-14 10:39:21 +11002998 const struct cred *cred = current_cred();
NeilBrownbda0be72015-03-23 13:37:39 +11002999 struct common_audit_data ad;
3000 struct inode_security_struct *isec;
3001 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002
NeilBrownbda0be72015-03-23 13:37:39 +11003003 validate_creds(cred);
3004
3005 ad.type = LSM_AUDIT_DATA_DENTRY;
3006 ad.u.dentry = dentry;
3007 sid = cred_sid(cred);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05003008 isec = inode_security_rcu(inode, rcu);
3009 if (IS_ERR(isec))
3010 return PTR_ERR(isec);
NeilBrownbda0be72015-03-23 13:37:39 +11003011
Stephen Smalleye46e01e2018-12-12 10:10:56 -05003012 return avc_has_perm(&selinux_state,
3013 sid, isec->sid, isec->sclass, FILE__READ, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014}
3015
Eric Parisd4cf970d2012-04-04 15:01:42 -04003016static noinline int audit_inode_permission(struct inode *inode,
3017 u32 perms, u32 audited, u32 denied,
Stephen Smalley626b9742014-04-29 11:29:04 -07003018 int result,
Eric Parisd4cf970d2012-04-04 15:01:42 -04003019 unsigned flags)
3020{
3021 struct common_audit_data ad;
Casey Schaufler80788c22018-09-21 17:19:11 -07003022 struct inode_security_struct *isec = selinux_inode(inode);
Eric Parisd4cf970d2012-04-04 15:01:42 -04003023 int rc;
3024
Eric Paris50c205f2012-04-04 15:01:43 -04003025 ad.type = LSM_AUDIT_DATA_INODE;
Eric Parisd4cf970d2012-04-04 15:01:42 -04003026 ad.u.inode = inode;
3027
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003028 rc = slow_avc_audit(&selinux_state,
3029 current_sid(), isec->sid, isec->sclass, perms,
Stephen Smalley626b9742014-04-29 11:29:04 -07003030 audited, denied, result, &ad, flags);
Eric Parisd4cf970d2012-04-04 15:01:42 -04003031 if (rc)
3032 return rc;
3033 return 0;
3034}
3035
Al Viroe74f71e2011-06-20 19:38:15 -04003036static int selinux_inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037{
David Howells88e67f32008-11-14 10:39:21 +11003038 const struct cred *cred = current_cred();
Eric Parisb782e0a2010-07-23 11:44:03 -04003039 u32 perms;
3040 bool from_access;
Al Virocf1dd1d2011-06-20 19:44:08 -04003041 unsigned flags = mask & MAY_NOT_BLOCK;
Eric Paris2e334052012-04-04 15:01:42 -04003042 struct inode_security_struct *isec;
3043 u32 sid;
3044 struct av_decision avd;
3045 int rc, rc2;
3046 u32 audited, denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047
Eric Parisb782e0a2010-07-23 11:44:03 -04003048 from_access = mask & MAY_ACCESS;
Eric Parisd09ca732010-07-23 11:43:57 -04003049 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
3050
Eric Parisb782e0a2010-07-23 11:44:03 -04003051 /* No permission to check. Existence test. */
3052 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054
Eric Paris2e334052012-04-04 15:01:42 -04003055 validate_creds(cred);
Eric Parisb782e0a2010-07-23 11:44:03 -04003056
Eric Paris2e334052012-04-04 15:01:42 -04003057 if (unlikely(IS_PRIVATE(inode)))
3058 return 0;
Eric Parisb782e0a2010-07-23 11:44:03 -04003059
3060 perms = file_mask_to_av(inode->i_mode, mask);
3061
Eric Paris2e334052012-04-04 15:01:42 -04003062 sid = cred_sid(cred);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05003063 isec = inode_security_rcu(inode, flags & MAY_NOT_BLOCK);
3064 if (IS_ERR(isec))
3065 return PTR_ERR(isec);
Eric Paris2e334052012-04-04 15:01:42 -04003066
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003067 rc = avc_has_perm_noaudit(&selinux_state,
Stephen Smalley3a28cff2018-12-12 10:10:55 -05003068 sid, isec->sid, isec->sclass, perms,
3069 (flags & MAY_NOT_BLOCK) ? AVC_NONBLOCKING : 0,
3070 &avd);
Eric Paris2e334052012-04-04 15:01:42 -04003071 audited = avc_audit_required(perms, &avd, rc,
3072 from_access ? FILE__AUDIT_ACCESS : 0,
3073 &denied);
3074 if (likely(!audited))
3075 return rc;
3076
Stephen Smalley626b9742014-04-29 11:29:04 -07003077 rc2 = audit_inode_permission(inode, perms, audited, denied, rc, flags);
Eric Paris2e334052012-04-04 15:01:42 -04003078 if (rc2)
3079 return rc2;
3080 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081}
3082
3083static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
3084{
David Howells88e67f32008-11-14 10:39:21 +11003085 const struct cred *cred = current_cred();
Stephen Smalleyccb54472017-05-12 12:41:24 -04003086 struct inode *inode = d_backing_inode(dentry);
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003087 unsigned int ia_valid = iattr->ia_valid;
Eric Paris95dbf732012-04-04 13:45:34 -04003088 __u32 av = FILE__WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003090 /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
3091 if (ia_valid & ATTR_FORCE) {
3092 ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
3093 ATTR_FORCE);
3094 if (!ia_valid)
3095 return 0;
3096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003098 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
3099 ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
Eric Paris2875fa02011-04-28 16:04:24 -04003100 return dentry_has_perm(cred, dentry, FILE__SETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003102 if (selinux_policycap_openperm() &&
Stephen Smalleyccb54472017-05-12 12:41:24 -04003103 inode->i_sb->s_magic != SOCKFS_MAGIC &&
3104 (ia_valid & ATTR_SIZE) &&
3105 !(ia_valid & ATTR_FILE))
Eric Paris95dbf732012-04-04 13:45:34 -04003106 av |= FILE__OPEN;
3107
3108 return dentry_has_perm(cred, dentry, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109}
3110
Al Viro3f7036a2015-03-08 19:28:30 -04003111static int selinux_inode_getattr(const struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112{
Al Viro3f7036a2015-03-08 19:28:30 -04003113 return path_has_perm(current_cred(), path, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114}
3115
Stephen Smalleydb590002017-04-20 11:31:30 -04003116static bool has_cap_mac_admin(bool audit)
3117{
3118 const struct cred *cred = current_cred();
Micah Mortonc1a85a02019-01-07 16:10:53 -08003119 unsigned int opts = audit ? CAP_OPT_NONE : CAP_OPT_NOAUDIT;
Stephen Smalleydb590002017-04-20 11:31:30 -04003120
Micah Mortonc1a85a02019-01-07 16:10:53 -08003121 if (cap_capable(cred, &init_user_ns, CAP_MAC_ADMIN, opts))
Stephen Smalleydb590002017-04-20 11:31:30 -04003122 return false;
Micah Mortonc1a85a02019-01-07 16:10:53 -08003123 if (cred_has_capability(cred, CAP_MAC_ADMIN, opts, true))
Stephen Smalleydb590002017-04-20 11:31:30 -04003124 return false;
3125 return true;
3126}
3127
David Howells8f0cfa52008-04-29 00:59:41 -07003128static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
3129 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130{
David Howellsc6f493d2015-03-17 22:26:22 +00003131 struct inode *inode = d_backing_inode(dentry);
Paul Moore20cdef82016-04-04 14:14:42 -04003132 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 struct superblock_security_struct *sbsec;
Thomas Liu2bf49692009-07-14 12:14:09 -04003134 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11003135 u32 newsid, sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 int rc = 0;
3137
Eric W. Biederman6b240302017-10-02 09:38:20 -05003138 if (strcmp(name, XATTR_NAME_SELINUX)) {
3139 rc = cap_inode_setxattr(dentry, name, value, size, flags);
3140 if (rc)
3141 return rc;
3142
3143 /* Not an attribute we recognize, so just check the
3144 ordinary setattr permission. */
3145 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147
3148 sbsec = inode->i_sb->s_security;
Eric Paris12f348b2012-10-09 10:56:25 -04003149 if (!(sbsec->flags & SBLABEL_MNT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 return -EOPNOTSUPP;
3151
Serge E. Hallyn2e149672011-03-23 16:43:26 -07003152 if (!inode_owner_or_capable(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 return -EPERM;
3154
Eric Paris50c205f2012-04-04 15:01:43 -04003155 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04003156 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157
Paul Moore20cdef82016-04-04 14:14:42 -04003158 isec = backing_inode_security(dentry);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003159 rc = avc_has_perm(&selinux_state,
3160 sid, isec->sid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 FILE__RELABELFROM, &ad);
3162 if (rc)
3163 return rc;
3164
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003165 rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3166 GFP_KERNEL);
Stephen Smalley12b29f32008-05-07 13:03:20 -04003167 if (rc == -EINVAL) {
Stephen Smalleydb590002017-04-20 11:31:30 -04003168 if (!has_cap_mac_admin(true)) {
Eric Parisd6ea83e2012-04-04 13:45:49 -04003169 struct audit_buffer *ab;
3170 size_t audit_size;
Eric Parisd6ea83e2012-04-04 13:45:49 -04003171
3172 /* We strip a nul only if it is at the end, otherwise the
3173 * context contains a nul and we should audit that */
Al Viroe3fea3f2012-06-09 08:15:16 +01003174 if (value) {
Colin Ian Kingadd24372017-10-14 13:46:55 +01003175 const char *str = value;
3176
Al Viroe3fea3f2012-06-09 08:15:16 +01003177 if (str[size - 1] == '\0')
3178 audit_size = size - 1;
3179 else
3180 audit_size = size;
3181 } else {
Al Viroe3fea3f2012-06-09 08:15:16 +01003182 audit_size = 0;
3183 }
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04003184 ab = audit_log_start(audit_context(),
3185 GFP_ATOMIC, AUDIT_SELINUX_ERR);
Eric Parisd6ea83e2012-04-04 13:45:49 -04003186 audit_log_format(ab, "op=setxattr invalid_context=");
3187 audit_log_n_untrustedstring(ab, value, audit_size);
3188 audit_log_end(ab);
3189
Stephen Smalley12b29f32008-05-07 13:03:20 -04003190 return rc;
Eric Parisd6ea83e2012-04-04 13:45:49 -04003191 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003192 rc = security_context_to_sid_force(&selinux_state, value,
3193 size, &newsid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04003194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 if (rc)
3196 return rc;
3197
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003198 rc = avc_has_perm(&selinux_state,
3199 sid, newsid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 FILE__RELABELTO, &ad);
3201 if (rc)
3202 return rc;
3203
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003204 rc = security_validate_transition(&selinux_state, isec->sid, newsid,
3205 sid, isec->sclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 if (rc)
3207 return rc;
3208
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003209 return avc_has_perm(&selinux_state,
3210 newsid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 sbsec->sid,
3212 SECCLASS_FILESYSTEM,
3213 FILESYSTEM__ASSOCIATE,
3214 &ad);
3215}
3216
David Howells8f0cfa52008-04-29 00:59:41 -07003217static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
Eric Parisf5269712008-05-14 11:27:45 -04003218 const void *value, size_t size,
David Howells8f0cfa52008-04-29 00:59:41 -07003219 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220{
David Howellsc6f493d2015-03-17 22:26:22 +00003221 struct inode *inode = d_backing_inode(dentry);
Paul Moore20cdef82016-04-04 14:14:42 -04003222 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 u32 newsid;
3224 int rc;
3225
3226 if (strcmp(name, XATTR_NAME_SELINUX)) {
3227 /* Not an attribute we recognize, so nothing to do. */
3228 return;
3229 }
3230
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003231 rc = security_context_to_sid_force(&selinux_state, value, size,
3232 &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 if (rc) {
peter enderborgc103a912018-06-12 10:09:03 +02003234 pr_err("SELinux: unable to map context to SID"
Stephen Smalley12b29f32008-05-07 13:03:20 -04003235 "for (%s, %lu), rc=%d\n",
3236 inode->i_sb->s_id, inode->i_ino, -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 return;
3238 }
3239
Paul Moore20cdef82016-04-04 14:14:42 -04003240 isec = backing_inode_security(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003241 spin_lock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003242 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05003244 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003245 spin_unlock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003246
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 return;
3248}
3249
David Howells8f0cfa52008-04-29 00:59:41 -07003250static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251{
David Howells88e67f32008-11-14 10:39:21 +11003252 const struct cred *cred = current_cred();
3253
Eric Paris2875fa02011-04-28 16:04:24 -04003254 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255}
3256
Eric Paris828dfe12008-04-17 13:17:49 -04003257static int selinux_inode_listxattr(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258{
David Howells88e67f32008-11-14 10:39:21 +11003259 const struct cred *cred = current_cred();
3260
Eric Paris2875fa02011-04-28 16:04:24 -04003261 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262}
3263
David Howells8f0cfa52008-04-29 00:59:41 -07003264static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265{
Eric W. Biederman6b240302017-10-02 09:38:20 -05003266 if (strcmp(name, XATTR_NAME_SELINUX)) {
3267 int rc = cap_inode_removexattr(dentry, name);
3268 if (rc)
3269 return rc;
3270
3271 /* Not an attribute we recognize, so just check the
3272 ordinary setattr permission. */
3273 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275
3276 /* No one is allowed to remove a SELinux security label.
3277 You can change the label, but all data must be labeled. */
3278 return -EACCES;
3279}
3280
James Morrisd381d8a2005-10-30 14:59:22 -08003281/*
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003282 * Copy the inode security context value to the user.
James Morrisd381d8a2005-10-30 14:59:22 -08003283 *
3284 * Permission check is handled by selinux_inode_getxattr hook.
3285 */
Andreas Gruenbacherea861df2015-12-24 11:09:39 -05003286static int selinux_inode_getsecurity(struct inode *inode, const char *name, void **buffer, bool alloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287{
David P. Quigley42492592008-02-04 22:29:39 -08003288 u32 size;
3289 int error;
3290 char *context = NULL;
Paul Moore20cdef82016-04-04 14:14:42 -04003291 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00003293 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3294 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003296 /*
3297 * If the caller has CAP_MAC_ADMIN, then get the raw context
3298 * value even if it is not defined by current policy; otherwise,
3299 * use the in-core value under current policy.
3300 * Use the non-auditing forms of the permission checks since
3301 * getxattr may be called by unprivileged processes commonly
3302 * and lack of permission just means that we fall back to the
3303 * in-core context value, not a denial.
3304 */
Paul Moore20cdef82016-04-04 14:14:42 -04003305 isec = inode_security(inode);
Stephen Smalleydb590002017-04-20 11:31:30 -04003306 if (has_cap_mac_admin(false))
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003307 error = security_sid_to_context_force(&selinux_state,
3308 isec->sid, &context,
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003309 &size);
3310 else
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003311 error = security_sid_to_context(&selinux_state, isec->sid,
3312 &context, &size);
David P. Quigley42492592008-02-04 22:29:39 -08003313 if (error)
3314 return error;
3315 error = size;
3316 if (alloc) {
3317 *buffer = context;
3318 goto out_nofree;
3319 }
3320 kfree(context);
3321out_nofree:
3322 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323}
3324
3325static int selinux_inode_setsecurity(struct inode *inode, const char *name,
Eric Paris828dfe12008-04-17 13:17:49 -04003326 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327{
Paul Moore2c971652016-04-19 16:36:28 -04003328 struct inode_security_struct *isec = inode_security_novalidate(inode);
Ondrej Mosnacek53e0c2a2018-12-21 21:18:53 +01003329 struct superblock_security_struct *sbsec = inode->i_sb->s_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 u32 newsid;
3331 int rc;
3332
3333 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3334 return -EOPNOTSUPP;
3335
Ondrej Mosnacek53e0c2a2018-12-21 21:18:53 +01003336 if (!(sbsec->flags & SBLABEL_MNT))
3337 return -EOPNOTSUPP;
3338
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 if (!value || !size)
3340 return -EACCES;
3341
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003342 rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3343 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 if (rc)
3345 return rc;
3346
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003347 spin_lock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003348 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05003350 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003351 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 return 0;
3353}
3354
3355static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
3356{
3357 const int len = sizeof(XATTR_NAME_SELINUX);
3358 if (buffer && len <= buffer_size)
3359 memcpy(buffer, XATTR_NAME_SELINUX, len);
3360 return len;
3361}
3362
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05003363static void selinux_inode_getsecid(struct inode *inode, u32 *secid)
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02003364{
Andreas Gruenbachere817c2f2016-02-18 12:04:08 +01003365 struct inode_security_struct *isec = inode_security_novalidate(inode);
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02003366 *secid = isec->sid;
3367}
3368
Vivek Goyal56909eb2016-07-13 10:44:48 -04003369static int selinux_inode_copy_up(struct dentry *src, struct cred **new)
3370{
3371 u32 sid;
3372 struct task_security_struct *tsec;
3373 struct cred *new_creds = *new;
3374
3375 if (new_creds == NULL) {
3376 new_creds = prepare_creds();
3377 if (!new_creds)
3378 return -ENOMEM;
3379 }
3380
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07003381 tsec = selinux_cred(new_creds);
Vivek Goyal56909eb2016-07-13 10:44:48 -04003382 /* Get label from overlay inode and set it in create_sid */
3383 selinux_inode_getsecid(d_inode(src), &sid);
3384 tsec->create_sid = sid;
3385 *new = new_creds;
3386 return 0;
3387}
3388
Vivek Goyal19472b62016-07-13 10:44:50 -04003389static int selinux_inode_copy_up_xattr(const char *name)
3390{
3391 /* The copy_up hook above sets the initial context on an inode, but we
3392 * don't then want to overwrite it by blindly copying all the lower
3393 * xattrs up. Instead, we have to filter out SELinux-related xattrs.
3394 */
3395 if (strcmp(name, XATTR_NAME_SELINUX) == 0)
3396 return 1; /* Discard */
3397 /*
3398 * Any other attribute apart from SELINUX is not claimed, supported
3399 * by selinux.
3400 */
3401 return -EOPNOTSUPP;
3402}
3403
Ondrej Mosnacekec882da2019-02-22 15:57:17 +01003404/* kernfs node operations */
3405
YueHaibingc72c4cd2019-03-22 22:04:00 +08003406static int selinux_kernfs_init_security(struct kernfs_node *kn_dir,
3407 struct kernfs_node *kn)
Ondrej Mosnacekec882da2019-02-22 15:57:17 +01003408{
3409 const struct task_security_struct *tsec = current_security();
3410 u32 parent_sid, newsid, clen;
3411 int rc;
3412 char *context;
3413
Ondrej Mosnacek1537ad12019-04-03 09:29:41 +02003414 rc = kernfs_xattr_get(kn_dir, XATTR_NAME_SELINUX, NULL, 0);
Ondrej Mosnacekec882da2019-02-22 15:57:17 +01003415 if (rc == -ENODATA)
3416 return 0;
3417 else if (rc < 0)
3418 return rc;
3419
3420 clen = (u32)rc;
3421 context = kmalloc(clen, GFP_KERNEL);
3422 if (!context)
3423 return -ENOMEM;
3424
Ondrej Mosnacek1537ad12019-04-03 09:29:41 +02003425 rc = kernfs_xattr_get(kn_dir, XATTR_NAME_SELINUX, context, clen);
Ondrej Mosnacekec882da2019-02-22 15:57:17 +01003426 if (rc < 0) {
3427 kfree(context);
3428 return rc;
3429 }
3430
3431 rc = security_context_to_sid(&selinux_state, context, clen, &parent_sid,
3432 GFP_KERNEL);
3433 kfree(context);
3434 if (rc)
3435 return rc;
3436
3437 if (tsec->create_sid) {
3438 newsid = tsec->create_sid;
3439 } else {
3440 u16 secclass = inode_mode_to_security_class(kn->mode);
3441 struct qstr q;
3442
3443 q.name = kn->name;
3444 q.hash_len = hashlen_string(kn_dir, kn->name);
3445
3446 rc = security_transition_sid(&selinux_state, tsec->sid,
3447 parent_sid, secclass, &q,
3448 &newsid);
3449 if (rc)
3450 return rc;
3451 }
3452
3453 rc = security_sid_to_context_force(&selinux_state, newsid,
3454 &context, &clen);
3455 if (rc)
3456 return rc;
3457
Ondrej Mosnacek1537ad12019-04-03 09:29:41 +02003458 rc = kernfs_xattr_set(kn, XATTR_NAME_SELINUX, context, clen,
3459 XATTR_CREATE);
Ondrej Mosnacekec882da2019-02-22 15:57:17 +01003460 kfree(context);
3461 return rc;
3462}
3463
3464
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465/* file security operations */
3466
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003467static int selinux_revalidate_file_permission(struct file *file, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468{
David Howells88e67f32008-11-14 10:39:21 +11003469 const struct cred *cred = current_cred();
Al Viro496ad9a2013-01-23 17:07:38 -05003470 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
3473 if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
3474 mask |= MAY_APPEND;
3475
Paul Moore389fb8002009-03-27 17:10:34 -04003476 return file_has_perm(cred, file,
3477 file_mask_to_av(inode->i_mode, mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478}
3479
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003480static int selinux_file_permission(struct file *file, int mask)
3481{
Al Viro496ad9a2013-01-23 17:07:38 -05003482 struct inode *inode = file_inode(file);
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07003483 struct file_security_struct *fsec = selinux_file(file);
Andreas Gruenbacherb1973672016-01-05 23:12:33 +01003484 struct inode_security_struct *isec;
Stephen Smalley20dda182009-06-22 14:54:53 -04003485 u32 sid = current_sid();
3486
Paul Moore389fb8002009-03-27 17:10:34 -04003487 if (!mask)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003488 /* No permission to check. Existence test. */
3489 return 0;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003490
Andreas Gruenbacherb1973672016-01-05 23:12:33 +01003491 isec = inode_security(inode);
Stephen Smalley20dda182009-06-22 14:54:53 -04003492 if (sid == fsec->sid && fsec->isid == isec->sid &&
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003493 fsec->pseqno == avc_policy_seqno(&selinux_state))
Eric Paris83d49852012-04-04 13:45:40 -04003494 /* No change since file_open check. */
Stephen Smalley20dda182009-06-22 14:54:53 -04003495 return 0;
3496
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003497 return selinux_revalidate_file_permission(file, mask);
3498}
3499
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500static int selinux_file_alloc_security(struct file *file)
3501{
3502 return file_alloc_security(file);
3503}
3504
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003505/*
3506 * Check whether a task has the ioctl permission and cmd
3507 * operation to an inode.
3508 */
Geliang Tang1d2a1682015-10-21 17:44:27 -04003509static int ioctl_has_perm(const struct cred *cred, struct file *file,
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003510 u32 requested, u16 cmd)
3511{
3512 struct common_audit_data ad;
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07003513 struct file_security_struct *fsec = selinux_file(file);
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003514 struct inode *inode = file_inode(file);
Paul Moore20cdef82016-04-04 14:14:42 -04003515 struct inode_security_struct *isec;
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003516 struct lsm_ioctlop_audit ioctl;
3517 u32 ssid = cred_sid(cred);
3518 int rc;
3519 u8 driver = cmd >> 8;
3520 u8 xperm = cmd & 0xff;
3521
3522 ad.type = LSM_AUDIT_DATA_IOCTL_OP;
3523 ad.u.op = &ioctl;
3524 ad.u.op->cmd = cmd;
3525 ad.u.op->path = file->f_path;
3526
3527 if (ssid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003528 rc = avc_has_perm(&selinux_state,
3529 ssid, fsec->sid,
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003530 SECCLASS_FD,
3531 FD__USE,
3532 &ad);
3533 if (rc)
3534 goto out;
3535 }
3536
3537 if (unlikely(IS_PRIVATE(inode)))
3538 return 0;
3539
Paul Moore20cdef82016-04-04 14:14:42 -04003540 isec = inode_security(inode);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003541 rc = avc_has_extended_perms(&selinux_state,
3542 ssid, isec->sid, isec->sclass,
3543 requested, driver, xperm, &ad);
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003544out:
3545 return rc;
3546}
3547
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548static int selinux_file_ioctl(struct file *file, unsigned int cmd,
3549 unsigned long arg)
3550{
David Howells88e67f32008-11-14 10:39:21 +11003551 const struct cred *cred = current_cred();
Eric Paris0b24dcb2011-02-25 15:39:20 -05003552 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553
Eric Paris0b24dcb2011-02-25 15:39:20 -05003554 switch (cmd) {
3555 case FIONREAD:
3556 /* fall through */
3557 case FIBMAP:
3558 /* fall through */
3559 case FIGETBSZ:
3560 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003561 case FS_IOC_GETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003562 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003563 case FS_IOC_GETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003564 error = file_has_perm(cred, file, FILE__GETATTR);
3565 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566
Al Viro2f99c362012-03-23 16:04:05 -04003567 case FS_IOC_SETFLAGS:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003568 /* fall through */
Al Viro2f99c362012-03-23 16:04:05 -04003569 case FS_IOC_SETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003570 error = file_has_perm(cred, file, FILE__SETATTR);
3571 break;
3572
3573 /* sys_ioctl() checks */
3574 case FIONBIO:
3575 /* fall through */
3576 case FIOASYNC:
3577 error = file_has_perm(cred, file, 0);
3578 break;
3579
3580 case KDSKBENT:
3581 case KDSKBSENT:
Eric Paris6a9de492012-01-03 12:25:14 -05003582 error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
Micah Mortonc1a85a02019-01-07 16:10:53 -08003583 CAP_OPT_NONE, true);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003584 break;
3585
3586 /* default case assumes that the command will go
3587 * to the file's ioctl() function.
3588 */
3589 default:
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003590 error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003591 }
3592 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593}
3594
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003595static int default_noexec;
3596
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
3598{
David Howells88e67f32008-11-14 10:39:21 +11003599 const struct cred *cred = current_cred();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003600 u32 sid = cred_sid(cred);
David Howellsd84f4f92008-11-14 10:39:23 +11003601 int rc = 0;
David Howells88e67f32008-11-14 10:39:21 +11003602
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003603 if (default_noexec &&
Stephen Smalley892e8ca2015-07-10 09:40:59 -04003604 (prot & PROT_EXEC) && (!file || IS_PRIVATE(file_inode(file)) ||
3605 (!shared && (prot & PROT_WRITE)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 /*
3607 * We are making executable an anonymous mapping or a
3608 * private file mapping that will also be writable.
3609 * This has an additional check.
3610 */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003611 rc = avc_has_perm(&selinux_state,
3612 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003613 PROCESS__EXECMEM, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 if (rc)
David Howellsd84f4f92008-11-14 10:39:23 +11003615 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617
3618 if (file) {
3619 /* read access is always possible with a mapping */
3620 u32 av = FILE__READ;
3621
3622 /* write access only matters if the mapping is shared */
3623 if (shared && (prot & PROT_WRITE))
3624 av |= FILE__WRITE;
3625
3626 if (prot & PROT_EXEC)
3627 av |= FILE__EXECUTE;
3628
David Howells88e67f32008-11-14 10:39:21 +11003629 return file_has_perm(cred, file, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 }
David Howellsd84f4f92008-11-14 10:39:23 +11003631
3632error:
3633 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634}
3635
Al Viroe5467852012-05-30 13:30:51 -04003636static int selinux_mmap_addr(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07003638 int rc = 0;
Paul Moore98883bf2014-03-19 16:46:11 -04003639
3640 if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
3641 u32 sid = current_sid();
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003642 rc = avc_has_perm(&selinux_state,
3643 sid, sid, SECCLASS_MEMPROTECT,
Paul Moore98883bf2014-03-19 16:46:11 -04003644 MEMPROTECT__MMAP_ZERO, NULL);
3645 }
3646
3647 return rc;
Al Viroe5467852012-05-30 13:30:51 -04003648}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649
Al Viroe5467852012-05-30 13:30:51 -04003650static int selinux_mmap_file(struct file *file, unsigned long reqprot,
3651 unsigned long prot, unsigned long flags)
3652{
Stephen Smalley3ba4bf52017-05-05 09:14:48 -04003653 struct common_audit_data ad;
3654 int rc;
3655
3656 if (file) {
3657 ad.type = LSM_AUDIT_DATA_FILE;
3658 ad.u.file = file;
3659 rc = inode_has_perm(current_cred(), file_inode(file),
3660 FILE__MAP, &ad);
3661 if (rc)
3662 return rc;
3663 }
3664
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003665 if (selinux_state.checkreqprot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 prot = reqprot;
3667
3668 return file_map_prot_check(file, prot,
3669 (flags & MAP_TYPE) == MAP_SHARED);
3670}
3671
3672static int selinux_file_mprotect(struct vm_area_struct *vma,
3673 unsigned long reqprot,
3674 unsigned long prot)
3675{
David Howells88e67f32008-11-14 10:39:21 +11003676 const struct cred *cred = current_cred();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003677 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003679 if (selinux_state.checkreqprot)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 prot = reqprot;
3681
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003682 if (default_noexec &&
3683 (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
James Morrisd541bbe2009-01-29 12:19:51 +11003684 int rc = 0;
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003685 if (vma->vm_start >= vma->vm_mm->start_brk &&
3686 vma->vm_end <= vma->vm_mm->brk) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003687 rc = avc_has_perm(&selinux_state,
3688 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003689 PROCESS__EXECHEAP, NULL);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003690 } else if (!vma->vm_file &&
Stephen Smalleyc2316db2016-04-08 13:55:03 -04003691 ((vma->vm_start <= vma->vm_mm->start_stack &&
3692 vma->vm_end >= vma->vm_mm->start_stack) ||
Andy Lutomirskid17af502016-09-30 10:58:58 -07003693 vma_is_stack_for_current(vma))) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003694 rc = avc_has_perm(&selinux_state,
3695 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003696 PROCESS__EXECSTACK, NULL);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003697 } else if (vma->vm_file && vma->anon_vma) {
3698 /*
3699 * We are making executable a file mapping that has
3700 * had some COW done. Since pages might have been
3701 * written, check ability to execute the possibly
3702 * modified content. This typically should only
3703 * occur for text relocations.
3704 */
David Howellsd84f4f92008-11-14 10:39:23 +11003705 rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003706 }
Lorenzo Hernandez García-Hierro6b992192005-06-25 14:54:34 -07003707 if (rc)
3708 return rc;
3709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710
3711 return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
3712}
3713
3714static int selinux_file_lock(struct file *file, unsigned int cmd)
3715{
David Howells88e67f32008-11-14 10:39:21 +11003716 const struct cred *cred = current_cred();
3717
3718 return file_has_perm(cred, file, FILE__LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719}
3720
3721static int selinux_file_fcntl(struct file *file, unsigned int cmd,
3722 unsigned long arg)
3723{
David Howells88e67f32008-11-14 10:39:21 +11003724 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 int err = 0;
3726
3727 switch (cmd) {
Eric Paris828dfe12008-04-17 13:17:49 -04003728 case F_SETFL:
Eric Paris828dfe12008-04-17 13:17:49 -04003729 if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
David Howells88e67f32008-11-14 10:39:21 +11003730 err = file_has_perm(cred, file, FILE__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003732 }
3733 /* fall through */
3734 case F_SETOWN:
3735 case F_SETSIG:
3736 case F_GETFL:
3737 case F_GETOWN:
3738 case F_GETSIG:
Cyrill Gorcunov1d151c32012-07-30 14:43:00 -07003739 case F_GETOWNER_UIDS:
Eric Paris828dfe12008-04-17 13:17:49 -04003740 /* Just check FD__USE permission */
David Howells88e67f32008-11-14 10:39:21 +11003741 err = file_has_perm(cred, file, 0);
Eric Paris828dfe12008-04-17 13:17:49 -04003742 break;
3743 case F_GETLK:
3744 case F_SETLK:
3745 case F_SETLKW:
Jeff Layton0d3f7a22014-04-22 08:23:58 -04003746 case F_OFD_GETLK:
3747 case F_OFD_SETLK:
3748 case F_OFD_SETLKW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749#if BITS_PER_LONG == 32
Eric Paris828dfe12008-04-17 13:17:49 -04003750 case F_GETLK64:
3751 case F_SETLK64:
3752 case F_SETLKW64:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753#endif
David Howells88e67f32008-11-14 10:39:21 +11003754 err = file_has_perm(cred, file, FILE__LOCK);
Eric Paris828dfe12008-04-17 13:17:49 -04003755 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 }
3757
3758 return err;
3759}
3760
Jeff Laytone0b93ed2014-08-22 11:27:32 -04003761static void selinux_file_set_fowner(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 struct file_security_struct *fsec;
3764
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07003765 fsec = selinux_file(file);
David Howells275bb412008-11-14 10:39:19 +11003766 fsec->fown_sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767}
3768
3769static int selinux_file_send_sigiotask(struct task_struct *tsk,
3770 struct fown_struct *fown, int signum)
3771{
Eric Paris828dfe12008-04-17 13:17:49 -04003772 struct file *file;
Stephen Smalley65c90bc2009-05-04 15:43:18 -04003773 u32 sid = task_sid(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 struct file_security_struct *fsec;
3776
3777 /* struct fown_struct is never outside the context of a struct file */
Eric Paris828dfe12008-04-17 13:17:49 -04003778 file = container_of(fown, struct file, f_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07003780 fsec = selinux_file(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781
3782 if (!signum)
3783 perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
3784 else
3785 perm = signal_to_av(signum);
3786
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003787 return avc_has_perm(&selinux_state,
3788 fsec->fown_sid, sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 SECCLASS_PROCESS, perm, NULL);
3790}
3791
3792static int selinux_file_receive(struct file *file)
3793{
David Howells88e67f32008-11-14 10:39:21 +11003794 const struct cred *cred = current_cred();
3795
3796 return file_has_perm(cred, file, file_to_av(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797}
3798
Al Viro94817692018-07-10 14:13:18 -04003799static int selinux_file_open(struct file *file)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003800{
3801 struct file_security_struct *fsec;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003802 struct inode_security_struct *isec;
David Howellsd84f4f92008-11-14 10:39:23 +11003803
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07003804 fsec = selinux_file(file);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05003805 isec = inode_security(file_inode(file));
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003806 /*
3807 * Save inode label and policy sequence number
3808 * at open-time so that selinux_file_permission
3809 * can determine whether revalidation is necessary.
3810 * Task label is already saved in the file security
3811 * struct as its SID.
3812 */
3813 fsec->isid = isec->sid;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003814 fsec->pseqno = avc_policy_seqno(&selinux_state);
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003815 /*
3816 * Since the inode label or policy seqno may have changed
3817 * between the selinux_inode_permission check and the saving
3818 * of state above, recheck that access is still permitted.
3819 * Otherwise, access might never be revalidated against the
3820 * new inode label or new policy.
3821 * This check is not redundant - do not remove.
3822 */
Al Viro94817692018-07-10 14:13:18 -04003823 return file_path_has_perm(file->f_cred, file, open_file_to_av(file));
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003824}
3825
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826/* task security operations */
3827
Tetsuo Handaa79be232017-03-28 23:08:45 +09003828static int selinux_task_alloc(struct task_struct *task,
3829 unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003831 u32 sid = current_sid();
3832
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003833 return avc_has_perm(&selinux_state,
3834 sid, sid, SECCLASS_PROCESS, PROCESS__FORK, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835}
3836
David Howellsf1752ee2008-11-14 10:39:17 +11003837/*
David Howellsd84f4f92008-11-14 10:39:23 +11003838 * prepare a new set of credentials for modification
3839 */
3840static int selinux_cred_prepare(struct cred *new, const struct cred *old,
3841 gfp_t gfp)
3842{
Casey Schauflerbbd36622018-11-12 09:30:56 -08003843 const struct task_security_struct *old_tsec = selinux_cred(old);
3844 struct task_security_struct *tsec = selinux_cred(new);
David Howellsd84f4f92008-11-14 10:39:23 +11003845
Casey Schauflerbbd36622018-11-12 09:30:56 -08003846 *tsec = *old_tsec;
David Howellsd84f4f92008-11-14 10:39:23 +11003847 return 0;
3848}
3849
3850/*
David Howellsee18d642009-09-02 09:14:21 +01003851 * transfer the SELinux data to a blank set of creds
3852 */
3853static void selinux_cred_transfer(struct cred *new, const struct cred *old)
3854{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07003855 const struct task_security_struct *old_tsec = selinux_cred(old);
3856 struct task_security_struct *tsec = selinux_cred(new);
David Howellsee18d642009-09-02 09:14:21 +01003857
3858 *tsec = *old_tsec;
3859}
3860
Matthew Garrett3ec30112018-01-08 13:36:19 -08003861static void selinux_cred_getsecid(const struct cred *c, u32 *secid)
3862{
3863 *secid = cred_sid(c);
3864}
3865
David Howellsee18d642009-09-02 09:14:21 +01003866/*
David Howells3a3b7ce2008-11-14 10:39:28 +11003867 * set the security data for a kernel service
3868 * - all the creation contexts are set to unlabelled
3869 */
3870static int selinux_kernel_act_as(struct cred *new, u32 secid)
3871{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07003872 struct task_security_struct *tsec = selinux_cred(new);
David Howells3a3b7ce2008-11-14 10:39:28 +11003873 u32 sid = current_sid();
3874 int ret;
3875
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003876 ret = avc_has_perm(&selinux_state,
3877 sid, secid,
David Howells3a3b7ce2008-11-14 10:39:28 +11003878 SECCLASS_KERNEL_SERVICE,
3879 KERNEL_SERVICE__USE_AS_OVERRIDE,
3880 NULL);
3881 if (ret == 0) {
3882 tsec->sid = secid;
3883 tsec->create_sid = 0;
3884 tsec->keycreate_sid = 0;
3885 tsec->sockcreate_sid = 0;
3886 }
3887 return ret;
3888}
3889
3890/*
3891 * set the file creation context in a security record to the same as the
3892 * objective context of the specified inode
3893 */
3894static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
3895{
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05003896 struct inode_security_struct *isec = inode_security(inode);
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07003897 struct task_security_struct *tsec = selinux_cred(new);
David Howells3a3b7ce2008-11-14 10:39:28 +11003898 u32 sid = current_sid();
3899 int ret;
3900
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003901 ret = avc_has_perm(&selinux_state,
3902 sid, isec->sid,
David Howells3a3b7ce2008-11-14 10:39:28 +11003903 SECCLASS_KERNEL_SERVICE,
3904 KERNEL_SERVICE__CREATE_FILES_AS,
3905 NULL);
3906
3907 if (ret == 0)
3908 tsec->create_sid = isec->sid;
David Howellsef574712010-02-26 01:56:16 +00003909 return ret;
David Howells3a3b7ce2008-11-14 10:39:28 +11003910}
3911
Eric Parisdd8dbf22009-11-03 16:35:32 +11003912static int selinux_kernel_module_request(char *kmod_name)
Eric Paris25354c42009-08-13 09:45:03 -04003913{
Eric Parisdd8dbf22009-11-03 16:35:32 +11003914 struct common_audit_data ad;
3915
Eric Paris50c205f2012-04-04 15:01:43 -04003916 ad.type = LSM_AUDIT_DATA_KMOD;
Eric Parisdd8dbf22009-11-03 16:35:32 +11003917 ad.u.kmod_name = kmod_name;
3918
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003919 return avc_has_perm(&selinux_state,
3920 current_sid(), SECINITSID_KERNEL, SECCLASS_SYSTEM,
Eric Parisdd8dbf22009-11-03 16:35:32 +11003921 SYSTEM__MODULE_REQUEST, &ad);
Eric Paris25354c42009-08-13 09:45:03 -04003922}
3923
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003924static int selinux_kernel_module_from_file(struct file *file)
3925{
3926 struct common_audit_data ad;
3927 struct inode_security_struct *isec;
3928 struct file_security_struct *fsec;
3929 u32 sid = current_sid();
3930 int rc;
3931
3932 /* init_module */
3933 if (file == NULL)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003934 return avc_has_perm(&selinux_state,
3935 sid, sid, SECCLASS_SYSTEM,
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003936 SYSTEM__MODULE_LOAD, NULL);
3937
3938 /* finit_module */
Paul Moore20cdef82016-04-04 14:14:42 -04003939
Vivek Goyal43af5de2016-09-09 11:37:49 -04003940 ad.type = LSM_AUDIT_DATA_FILE;
3941 ad.u.file = file;
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003942
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07003943 fsec = selinux_file(file);
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003944 if (sid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003945 rc = avc_has_perm(&selinux_state,
3946 sid, fsec->sid, SECCLASS_FD, FD__USE, &ad);
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003947 if (rc)
3948 return rc;
3949 }
3950
Paul Moore20cdef82016-04-04 14:14:42 -04003951 isec = inode_security(file_inode(file));
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003952 return avc_has_perm(&selinux_state,
3953 sid, isec->sid, SECCLASS_SYSTEM,
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003954 SYSTEM__MODULE_LOAD, &ad);
3955}
3956
3957static int selinux_kernel_read_file(struct file *file,
3958 enum kernel_read_file_id id)
3959{
3960 int rc = 0;
3961
3962 switch (id) {
3963 case READING_MODULE:
3964 rc = selinux_kernel_module_from_file(file);
3965 break;
3966 default:
3967 break;
3968 }
3969
3970 return rc;
3971}
3972
Mimi Zoharc77b8cd2018-07-13 14:06:02 -04003973static int selinux_kernel_load_data(enum kernel_load_data_id id)
3974{
3975 int rc = 0;
3976
3977 switch (id) {
3978 case LOADING_MODULE:
3979 rc = selinux_kernel_module_from_file(NULL);
3980 default:
3981 break;
3982 }
3983
3984 return rc;
3985}
3986
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
3988{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003989 return avc_has_perm(&selinux_state,
3990 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003991 PROCESS__SETPGID, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992}
3993
3994static int selinux_task_getpgid(struct task_struct *p)
3995{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003996 return avc_has_perm(&selinux_state,
3997 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003998 PROCESS__GETPGID, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999}
4000
4001static int selinux_task_getsid(struct task_struct *p)
4002{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004003 return avc_has_perm(&selinux_state,
4004 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004005 PROCESS__GETSESSION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006}
4007
David Quigleyf9008e4c2006-06-30 01:55:46 -07004008static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
4009{
David Howells275bb412008-11-14 10:39:19 +11004010 *secid = task_sid(p);
David Quigleyf9008e4c2006-06-30 01:55:46 -07004011}
4012
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013static int selinux_task_setnice(struct task_struct *p, int nice)
4014{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004015 return avc_has_perm(&selinux_state,
4016 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004017 PROCESS__SETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018}
4019
James Morris03e68062006-06-23 02:03:58 -07004020static int selinux_task_setioprio(struct task_struct *p, int ioprio)
4021{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004022 return avc_has_perm(&selinux_state,
4023 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004024 PROCESS__SETSCHED, NULL);
James Morris03e68062006-06-23 02:03:58 -07004025}
4026
David Quigleya1836a42006-06-30 01:55:49 -07004027static int selinux_task_getioprio(struct task_struct *p)
4028{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004029 return avc_has_perm(&selinux_state,
4030 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004031 PROCESS__GETSCHED, NULL);
David Quigleya1836a42006-06-30 01:55:49 -07004032}
4033
Corentin LABBE42985552017-10-04 20:32:18 +02004034static int selinux_task_prlimit(const struct cred *cred, const struct cred *tcred,
4035 unsigned int flags)
Stephen Smalley791ec492017-02-17 07:57:00 -05004036{
4037 u32 av = 0;
4038
Stephen Smalley84e68852017-02-28 09:35:08 -05004039 if (!flags)
4040 return 0;
Stephen Smalley791ec492017-02-17 07:57:00 -05004041 if (flags & LSM_PRLIMIT_WRITE)
4042 av |= PROCESS__SETRLIMIT;
4043 if (flags & LSM_PRLIMIT_READ)
4044 av |= PROCESS__GETRLIMIT;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004045 return avc_has_perm(&selinux_state,
4046 cred_sid(cred), cred_sid(tcred),
Stephen Smalley791ec492017-02-17 07:57:00 -05004047 SECCLASS_PROCESS, av, NULL);
4048}
4049
Jiri Slaby8fd00b42009-08-26 18:41:16 +02004050static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
4051 struct rlimit *new_rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052{
Jiri Slaby8fd00b42009-08-26 18:41:16 +02004053 struct rlimit *old_rlim = p->signal->rlim + resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054
4055 /* Control the ability to change the hard limit (whether
4056 lowering or raising it), so that the hard limit can
4057 later be used as a safe reset point for the soft limit
David Howellsd84f4f92008-11-14 10:39:23 +11004058 upon context transitions. See selinux_bprm_committing_creds. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 if (old_rlim->rlim_max != new_rlim->rlim_max)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004060 return avc_has_perm(&selinux_state,
4061 current_sid(), task_sid(p),
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004062 SECCLASS_PROCESS, PROCESS__SETRLIMIT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063
4064 return 0;
4065}
4066
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09004067static int selinux_task_setscheduler(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004069 return avc_has_perm(&selinux_state,
4070 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004071 PROCESS__SETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072}
4073
4074static int selinux_task_getscheduler(struct task_struct *p)
4075{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004076 return avc_has_perm(&selinux_state,
4077 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004078 PROCESS__GETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079}
4080
David Quigley35601542006-06-23 02:04:01 -07004081static int selinux_task_movememory(struct task_struct *p)
4082{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004083 return avc_has_perm(&selinux_state,
4084 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004085 PROCESS__SETSCHED, NULL);
David Quigley35601542006-06-23 02:04:01 -07004086}
4087
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02004088static int selinux_task_kill(struct task_struct *p, struct kernel_siginfo *info,
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004089 int sig, const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090{
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004091 u32 secid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 if (!sig)
4095 perm = PROCESS__SIGNULL; /* null signal; existence test */
4096 else
4097 perm = signal_to_av(sig);
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004098 if (!cred)
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004099 secid = current_sid();
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004100 else
4101 secid = cred_sid(cred);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004102 return avc_has_perm(&selinux_state,
4103 secid, task_sid(p), SECCLASS_PROCESS, perm, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104}
4105
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106static void selinux_task_to_inode(struct task_struct *p,
4107 struct inode *inode)
4108{
Casey Schaufler80788c22018-09-21 17:19:11 -07004109 struct inode_security_struct *isec = selinux_inode(inode);
David Howells275bb412008-11-14 10:39:19 +11004110 u32 sid = task_sid(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004112 spin_lock(&isec->lock);
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01004113 isec->sclass = inode_mode_to_security_class(inode->i_mode);
David Howells275bb412008-11-14 10:39:19 +11004114 isec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004115 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004116 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117}
4118
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004120static int selinux_parse_skb_ipv4(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04004121 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122{
4123 int offset, ihlen, ret = -EINVAL;
4124 struct iphdr _iph, *ih;
4125
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03004126 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
4128 if (ih == NULL)
4129 goto out;
4130
4131 ihlen = ih->ihl * 4;
4132 if (ihlen < sizeof(_iph))
4133 goto out;
4134
Eric Paris48c62af2012-04-02 13:15:44 -04004135 ad->u.net->v4info.saddr = ih->saddr;
4136 ad->u.net->v4info.daddr = ih->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 ret = 0;
4138
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004139 if (proto)
4140 *proto = ih->protocol;
4141
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 switch (ih->protocol) {
Eric Paris828dfe12008-04-17 13:17:49 -04004143 case IPPROTO_TCP: {
4144 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145
Eric Paris828dfe12008-04-17 13:17:49 -04004146 if (ntohs(ih->frag_off) & IP_OFFSET)
4147 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148
4149 offset += ihlen;
4150 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4151 if (th == NULL)
4152 break;
4153
Eric Paris48c62af2012-04-02 13:15:44 -04004154 ad->u.net->sport = th->source;
4155 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158
Eric Paris828dfe12008-04-17 13:17:49 -04004159 case IPPROTO_UDP: {
4160 struct udphdr _udph, *uh;
4161
4162 if (ntohs(ih->frag_off) & IP_OFFSET)
4163 break;
4164
4165 offset += ihlen;
4166 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4167 if (uh == NULL)
4168 break;
4169
Eric Paris48c62af2012-04-02 13:15:44 -04004170 ad->u.net->sport = uh->source;
4171 ad->u.net->dport = uh->dest;
Eric Paris828dfe12008-04-17 13:17:49 -04004172 break;
4173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174
James Morris2ee92d42006-11-13 16:09:01 -08004175 case IPPROTO_DCCP: {
4176 struct dccp_hdr _dccph, *dh;
4177
4178 if (ntohs(ih->frag_off) & IP_OFFSET)
4179 break;
4180
4181 offset += ihlen;
4182 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4183 if (dh == NULL)
4184 break;
4185
Eric Paris48c62af2012-04-02 13:15:44 -04004186 ad->u.net->sport = dh->dccph_sport;
4187 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08004188 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004189 }
James Morris2ee92d42006-11-13 16:09:01 -08004190
Richard Hainesd4529302018-02-13 20:57:18 +00004191#if IS_ENABLED(CONFIG_IP_SCTP)
4192 case IPPROTO_SCTP: {
4193 struct sctphdr _sctph, *sh;
4194
4195 if (ntohs(ih->frag_off) & IP_OFFSET)
4196 break;
4197
4198 offset += ihlen;
4199 sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4200 if (sh == NULL)
4201 break;
4202
4203 ad->u.net->sport = sh->source;
4204 ad->u.net->dport = sh->dest;
4205 break;
4206 }
4207#endif
Eric Paris828dfe12008-04-17 13:17:49 -04004208 default:
4209 break;
4210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211out:
4212 return ret;
4213}
4214
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04004215#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216
4217/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004218static int selinux_parse_skb_ipv6(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04004219 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220{
4221 u8 nexthdr;
4222 int ret = -EINVAL, offset;
4223 struct ipv6hdr _ipv6h, *ip6;
Jesse Gross75f28112011-11-30 17:05:51 -08004224 __be16 frag_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03004226 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
4228 if (ip6 == NULL)
4229 goto out;
4230
Eric Paris48c62af2012-04-02 13:15:44 -04004231 ad->u.net->v6info.saddr = ip6->saddr;
4232 ad->u.net->v6info.daddr = ip6->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 ret = 0;
4234
4235 nexthdr = ip6->nexthdr;
4236 offset += sizeof(_ipv6h);
Jesse Gross75f28112011-11-30 17:05:51 -08004237 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 if (offset < 0)
4239 goto out;
4240
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004241 if (proto)
4242 *proto = nexthdr;
4243
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 switch (nexthdr) {
4245 case IPPROTO_TCP: {
Eric Paris828dfe12008-04-17 13:17:49 -04004246 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247
4248 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4249 if (th == NULL)
4250 break;
4251
Eric Paris48c62af2012-04-02 13:15:44 -04004252 ad->u.net->sport = th->source;
4253 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 break;
4255 }
4256
4257 case IPPROTO_UDP: {
4258 struct udphdr _udph, *uh;
4259
4260 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4261 if (uh == NULL)
4262 break;
4263
Eric Paris48c62af2012-04-02 13:15:44 -04004264 ad->u.net->sport = uh->source;
4265 ad->u.net->dport = uh->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 break;
4267 }
4268
James Morris2ee92d42006-11-13 16:09:01 -08004269 case IPPROTO_DCCP: {
4270 struct dccp_hdr _dccph, *dh;
4271
4272 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4273 if (dh == NULL)
4274 break;
4275
Eric Paris48c62af2012-04-02 13:15:44 -04004276 ad->u.net->sport = dh->dccph_sport;
4277 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08004278 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004279 }
James Morris2ee92d42006-11-13 16:09:01 -08004280
Richard Hainesd4529302018-02-13 20:57:18 +00004281#if IS_ENABLED(CONFIG_IP_SCTP)
4282 case IPPROTO_SCTP: {
4283 struct sctphdr _sctph, *sh;
4284
4285 sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4286 if (sh == NULL)
4287 break;
4288
4289 ad->u.net->sport = sh->source;
4290 ad->u.net->dport = sh->dest;
4291 break;
4292 }
4293#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 /* includes fragments */
4295 default:
4296 break;
4297 }
4298out:
4299 return ret;
4300}
4301
4302#endif /* IPV6 */
4303
Thomas Liu2bf49692009-07-14 12:14:09 -04004304static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
David Howellscf9481e2008-07-27 21:31:07 +10004305 char **_addrp, int src, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306{
David Howellscf9481e2008-07-27 21:31:07 +10004307 char *addrp;
4308 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309
Eric Paris48c62af2012-04-02 13:15:44 -04004310 switch (ad->u.net->family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 case PF_INET:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004312 ret = selinux_parse_skb_ipv4(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10004313 if (ret)
4314 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04004315 addrp = (char *)(src ? &ad->u.net->v4info.saddr :
4316 &ad->u.net->v4info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10004317 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04004319#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 case PF_INET6:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004321 ret = selinux_parse_skb_ipv6(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10004322 if (ret)
4323 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04004324 addrp = (char *)(src ? &ad->u.net->v6info.saddr :
4325 &ad->u.net->v6info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10004326 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327#endif /* IPV6 */
4328 default:
David Howellscf9481e2008-07-27 21:31:07 +10004329 addrp = NULL;
4330 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 }
4332
David Howellscf9481e2008-07-27 21:31:07 +10004333parse_error:
peter enderborgc103a912018-06-12 10:09:03 +02004334 pr_warn(
David Howellscf9481e2008-07-27 21:31:07 +10004335 "SELinux: failure in selinux_parse_skb(),"
4336 " unable to parse packet\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 return ret;
David Howellscf9481e2008-07-27 21:31:07 +10004338
4339okay:
4340 if (_addrp)
4341 *_addrp = addrp;
4342 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343}
4344
Paul Moore4f6a9932007-03-01 14:35:22 -05004345/**
Paul Moore220deb92008-01-29 08:38:23 -05004346 * selinux_skb_peerlbl_sid - Determine the peer label of a packet
Paul Moore4f6a9932007-03-01 14:35:22 -05004347 * @skb: the packet
Paul Moore75e22912008-01-29 08:38:04 -05004348 * @family: protocol family
Paul Moore220deb92008-01-29 08:38:23 -05004349 * @sid: the packet's peer label SID
Paul Moore4f6a9932007-03-01 14:35:22 -05004350 *
4351 * Description:
Paul Moore220deb92008-01-29 08:38:23 -05004352 * Check the various different forms of network peer labeling and determine
4353 * the peer label/SID for the packet; most of the magic actually occurs in
4354 * the security server function security_net_peersid_cmp(). The function
4355 * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
4356 * or -EACCES if @sid is invalid due to inconsistencies with the different
4357 * peer labels.
Paul Moore4f6a9932007-03-01 14:35:22 -05004358 *
4359 */
Paul Moore220deb92008-01-29 08:38:23 -05004360static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
Paul Moore4f6a9932007-03-01 14:35:22 -05004361{
Paul Moore71f1cb02008-01-29 08:51:16 -05004362 int err;
Paul Moore4f6a9932007-03-01 14:35:22 -05004363 u32 xfrm_sid;
4364 u32 nlbl_sid;
Paul Moore220deb92008-01-29 08:38:23 -05004365 u32 nlbl_type;
Paul Moore4f6a9932007-03-01 14:35:22 -05004366
Paul Moore817eff72013-12-10 14:57:54 -05004367 err = selinux_xfrm_skb_sid(skb, &xfrm_sid);
Paul Moorebed4d7e2013-07-23 17:38:40 -04004368 if (unlikely(err))
4369 return -EACCES;
4370 err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
4371 if (unlikely(err))
4372 return -EACCES;
Paul Moore220deb92008-01-29 08:38:23 -05004373
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004374 err = security_net_peersid_resolve(&selinux_state, nlbl_sid,
4375 nlbl_type, xfrm_sid, sid);
Paul Moore71f1cb02008-01-29 08:51:16 -05004376 if (unlikely(err)) {
peter enderborgc103a912018-06-12 10:09:03 +02004377 pr_warn(
Paul Moore71f1cb02008-01-29 08:51:16 -05004378 "SELinux: failure in selinux_skb_peerlbl_sid(),"
4379 " unable to determine packet's peer label\n");
Paul Moore220deb92008-01-29 08:38:23 -05004380 return -EACCES;
Paul Moore71f1cb02008-01-29 08:51:16 -05004381 }
Paul Moore220deb92008-01-29 08:38:23 -05004382
4383 return 0;
Paul Moore4f6a9932007-03-01 14:35:22 -05004384}
4385
Paul Moore446b8022013-12-04 16:10:51 -05004386/**
4387 * selinux_conn_sid - Determine the child socket label for a connection
4388 * @sk_sid: the parent socket's SID
4389 * @skb_sid: the packet's SID
4390 * @conn_sid: the resulting connection SID
4391 *
4392 * If @skb_sid is valid then the user:role:type information from @sk_sid is
4393 * combined with the MLS information from @skb_sid in order to create
4394 * @conn_sid. If @skb_sid is not valid then then @conn_sid is simply a copy
4395 * of @sk_sid. Returns zero on success, negative values on failure.
4396 *
4397 */
4398static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid)
4399{
4400 int err = 0;
4401
4402 if (skb_sid != SECSID_NULL)
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004403 err = security_sid_mls_copy(&selinux_state, sk_sid, skb_sid,
4404 conn_sid);
Paul Moore446b8022013-12-04 16:10:51 -05004405 else
4406 *conn_sid = sk_sid;
4407
4408 return err;
4409}
4410
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411/* socket security operations */
Paul Moored4f2d972010-04-22 14:46:18 -04004412
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004413static int socket_sockcreate_sid(const struct task_security_struct *tsec,
4414 u16 secclass, u32 *socksid)
Paul Moored4f2d972010-04-22 14:46:18 -04004415{
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004416 if (tsec->sockcreate_sid > SECSID_NULL) {
4417 *socksid = tsec->sockcreate_sid;
4418 return 0;
4419 }
4420
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004421 return security_transition_sid(&selinux_state, tsec->sid, tsec->sid,
4422 secclass, NULL, socksid);
Paul Moored4f2d972010-04-22 14:46:18 -04004423}
4424
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004425static int sock_has_perm(struct sock *sk, u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426{
Paul Moore253bfae2010-04-22 14:46:19 -04004427 struct sk_security_struct *sksec = sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004428 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004429 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430
Paul Moore253bfae2010-04-22 14:46:19 -04004431 if (sksec->sid == SECINITSID_KERNEL)
4432 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433
Eric Paris50c205f2012-04-04 15:01:43 -04004434 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004435 ad.u.net = &net;
4436 ad.u.net->sk = sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004438 return avc_has_perm(&selinux_state,
4439 current_sid(), sksec->sid, sksec->sclass, perms,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004440 &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441}
4442
4443static int selinux_socket_create(int family, int type,
4444 int protocol, int kern)
4445{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07004446 const struct task_security_struct *tsec = selinux_cred(current_cred());
Paul Moored4f2d972010-04-22 14:46:18 -04004447 u32 newsid;
David Howells275bb412008-11-14 10:39:19 +11004448 u16 secclass;
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004449 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450
4451 if (kern)
Paul Moored4f2d972010-04-22 14:46:18 -04004452 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453
David Howells275bb412008-11-14 10:39:19 +11004454 secclass = socket_type_to_security_class(family, type, protocol);
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004455 rc = socket_sockcreate_sid(tsec, secclass, &newsid);
4456 if (rc)
4457 return rc;
4458
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004459 return avc_has_perm(&selinux_state,
4460 tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461}
4462
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07004463static int selinux_socket_post_create(struct socket *sock, int family,
4464 int type, int protocol, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07004466 const struct task_security_struct *tsec = selinux_cred(current_cred());
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05004467 struct inode_security_struct *isec = inode_security_novalidate(SOCK_INODE(sock));
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004468 struct sk_security_struct *sksec;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004469 u16 sclass = socket_type_to_security_class(family, type, protocol);
4470 u32 sid = SECINITSID_KERNEL;
David Howells275bb412008-11-14 10:39:19 +11004471 int err = 0;
4472
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004473 if (!kern) {
4474 err = socket_sockcreate_sid(tsec, sclass, &sid);
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004475 if (err)
4476 return err;
4477 }
David Howells275bb412008-11-14 10:39:19 +11004478
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004479 isec->sclass = sclass;
4480 isec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004481 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004483 if (sock->sk) {
4484 sksec = sock->sk->sk_security;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004485 sksec->sclass = sclass;
4486 sksec->sid = sid;
Richard Hainesd4529302018-02-13 20:57:18 +00004487 /* Allows detection of the first association on this socket */
4488 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4489 sksec->sctp_assoc_state = SCTP_ASSOC_UNSET;
4490
Paul Moore389fb8002009-03-27 17:10:34 -04004491 err = selinux_netlbl_socket_post_create(sock->sk, family);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004492 }
4493
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07004494 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495}
4496
David Herrmann0b811db2018-05-04 16:28:21 +02004497static int selinux_socket_socketpair(struct socket *socka,
4498 struct socket *sockb)
4499{
4500 struct sk_security_struct *sksec_a = socka->sk->sk_security;
4501 struct sk_security_struct *sksec_b = sockb->sk->sk_security;
4502
4503 sksec_a->peer_sid = sksec_b->sid;
4504 sksec_b->peer_sid = sksec_a->sid;
4505
4506 return 0;
4507}
4508
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509/* Range of port numbers used to automatically bind.
4510 Need to determine whether we should perform a name_bind
4511 permission check between the socket and the port number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512
4513static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
4514{
Paul Moore253bfae2010-04-22 14:46:19 -04004515 struct sock *sk = sock->sk;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004516 struct sk_security_struct *sksec = sk->sk_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517 u16 family;
4518 int err;
4519
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004520 err = sock_has_perm(sk, SOCKET__BIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 if (err)
4522 goto out;
4523
Richard Hainesd4529302018-02-13 20:57:18 +00004524 /* If PF_INET or PF_INET6, check name_bind permission for the port. */
Paul Moore253bfae2010-04-22 14:46:19 -04004525 family = sk->sk_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 if (family == PF_INET || family == PF_INET6) {
4527 char *addrp;
Thomas Liu2bf49692009-07-14 12:14:09 -04004528 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004529 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 struct sockaddr_in *addr4 = NULL;
4531 struct sockaddr_in6 *addr6 = NULL;
Tetsuo Handac750e692019-04-12 19:59:34 +09004532 u16 family_sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 unsigned short snum;
James Morrise399f982008-06-12 01:39:58 +10004534 u32 sid, node_perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535
Richard Hainesd4529302018-02-13 20:57:18 +00004536 /*
4537 * sctp_bindx(3) calls via selinux_sctp_bind_connect()
4538 * that validates multiple binding addresses. Because of this
4539 * need to check address->sa_family as it is possible to have
4540 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4541 */
Tetsuo Handac750e692019-04-12 19:59:34 +09004542 if (addrlen < offsetofend(struct sockaddr, sa_family))
4543 return -EINVAL;
4544 family_sa = address->sa_family;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004545 switch (family_sa) {
4546 case AF_UNSPEC:
Richard Haines68741a8a2018-03-02 19:54:34 +00004547 case AF_INET:
4548 if (addrlen < sizeof(struct sockaddr_in))
4549 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550 addr4 = (struct sockaddr_in *)address;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004551 if (family_sa == AF_UNSPEC) {
4552 /* see __inet_bind(), we only want to allow
4553 * AF_UNSPEC if the address is INADDR_ANY
4554 */
4555 if (addr4->sin_addr.s_addr != htonl(INADDR_ANY))
4556 goto err_af;
4557 family_sa = AF_INET;
4558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 snum = ntohs(addr4->sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 addrp = (char *)&addr4->sin_addr.s_addr;
Richard Haines68741a8a2018-03-02 19:54:34 +00004561 break;
4562 case AF_INET6:
4563 if (addrlen < SIN6_LEN_RFC2133)
4564 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 addr6 = (struct sockaddr_in6 *)address;
4566 snum = ntohs(addr6->sin6_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 addrp = (char *)&addr6->sin6_addr.s6_addr;
Richard Haines68741a8a2018-03-02 19:54:34 +00004568 break;
4569 default:
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004570 goto err_af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 }
4572
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004573 ad.type = LSM_AUDIT_DATA_NET;
4574 ad.u.net = &net;
4575 ad.u.net->sport = htons(snum);
4576 ad.u.net->family = family_sa;
4577
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004578 if (snum) {
4579 int low, high;
4580
Eric W. Biederman0bbf87d2013-09-28 14:10:59 -07004581 inet_get_local_port_range(sock_net(sk), &low, &high);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004582
Krister Johansen4548b682017-01-20 17:49:11 -08004583 if (snum < max(inet_prot_sock(sock_net(sk)), low) ||
4584 snum > high) {
Paul Moore3e1121722008-04-10 10:48:14 -04004585 err = sel_netport_sid(sk->sk_protocol,
4586 snum, &sid);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004587 if (err)
4588 goto out;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004589 err = avc_has_perm(&selinux_state,
4590 sksec->sid, sid,
Paul Moore253bfae2010-04-22 14:46:19 -04004591 sksec->sclass,
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004592 SOCKET__NAME_BIND, &ad);
4593 if (err)
4594 goto out;
4595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 }
Eric Paris828dfe12008-04-17 13:17:49 -04004597
Paul Moore253bfae2010-04-22 14:46:19 -04004598 switch (sksec->sclass) {
James Morris13402582005-09-30 14:24:34 -04004599 case SECCLASS_TCP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600 node_perm = TCP_SOCKET__NODE_BIND;
4601 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004602
James Morris13402582005-09-30 14:24:34 -04004603 case SECCLASS_UDP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 node_perm = UDP_SOCKET__NODE_BIND;
4605 break;
James Morris2ee92d42006-11-13 16:09:01 -08004606
4607 case SECCLASS_DCCP_SOCKET:
4608 node_perm = DCCP_SOCKET__NODE_BIND;
4609 break;
4610
Richard Hainesd4529302018-02-13 20:57:18 +00004611 case SECCLASS_SCTP_SOCKET:
4612 node_perm = SCTP_SOCKET__NODE_BIND;
4613 break;
4614
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 default:
4616 node_perm = RAWIP_SOCKET__NODE_BIND;
4617 break;
4618 }
Eric Paris828dfe12008-04-17 13:17:49 -04004619
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004620 err = sel_netnode_sid(addrp, family_sa, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 if (err)
4622 goto out;
Eric Paris828dfe12008-04-17 13:17:49 -04004623
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004624 if (family_sa == AF_INET)
Eric Paris48c62af2012-04-02 13:15:44 -04004625 ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626 else
Eric Paris48c62af2012-04-02 13:15:44 -04004627 ad.u.net->v6info.saddr = addr6->sin6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004629 err = avc_has_perm(&selinux_state,
4630 sksec->sid, sid,
Paul Moore253bfae2010-04-22 14:46:19 -04004631 sksec->sclass, node_perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632 if (err)
4633 goto out;
4634 }
4635out:
4636 return err;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004637err_af:
4638 /* Note that SCTP services expect -EINVAL, others -EAFNOSUPPORT. */
4639 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4640 return -EINVAL;
4641 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642}
4643
Richard Hainesd4529302018-02-13 20:57:18 +00004644/* This supports connect(2) and SCTP connect services such as sctp_connectx(3)
Kees Cookd61330c2019-02-17 14:08:36 -08004645 * and sctp_sendmsg(3) as described in Documentation/security/SCTP.rst
Richard Hainesd4529302018-02-13 20:57:18 +00004646 */
4647static int selinux_socket_connect_helper(struct socket *sock,
4648 struct sockaddr *address, int addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649{
Paul Moore014ab192008-10-10 10:16:33 -04004650 struct sock *sk = sock->sk;
Paul Moore253bfae2010-04-22 14:46:19 -04004651 struct sk_security_struct *sksec = sk->sk_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652 int err;
4653
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004654 err = sock_has_perm(sk, SOCKET__CONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 if (err)
4656 return err;
Paolo Abeni05174c92019-05-10 19:12:33 +02004657 if (addrlen < offsetofend(struct sockaddr, sa_family))
4658 return -EINVAL;
4659
4660 /* connect(AF_UNSPEC) has special handling, as it is a documented
4661 * way to disconnect the socket
4662 */
4663 if (address->sa_family == AF_UNSPEC)
4664 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665
4666 /*
Richard Hainesd4529302018-02-13 20:57:18 +00004667 * If a TCP, DCCP or SCTP socket, check name_connect permission
4668 * for the port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 */
Paul Moore253bfae2010-04-22 14:46:19 -04004670 if (sksec->sclass == SECCLASS_TCP_SOCKET ||
Richard Hainesd4529302018-02-13 20:57:18 +00004671 sksec->sclass == SECCLASS_DCCP_SOCKET ||
4672 sksec->sclass == SECCLASS_SCTP_SOCKET) {
Thomas Liu2bf49692009-07-14 12:14:09 -04004673 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004674 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675 struct sockaddr_in *addr4 = NULL;
4676 struct sockaddr_in6 *addr6 = NULL;
4677 unsigned short snum;
James Morris2ee92d42006-11-13 16:09:01 -08004678 u32 sid, perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679
Richard Hainesd4529302018-02-13 20:57:18 +00004680 /* sctp_connectx(3) calls via selinux_sctp_bind_connect()
4681 * that validates multiple connect addresses. Because of this
4682 * need to check address->sa_family as it is possible to have
4683 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4684 */
Richard Haines68741a8a2018-03-02 19:54:34 +00004685 switch (address->sa_family) {
4686 case AF_INET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687 addr4 = (struct sockaddr_in *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004688 if (addrlen < sizeof(struct sockaddr_in))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 return -EINVAL;
4690 snum = ntohs(addr4->sin_port);
Richard Haines68741a8a2018-03-02 19:54:34 +00004691 break;
4692 case AF_INET6:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693 addr6 = (struct sockaddr_in6 *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004694 if (addrlen < SIN6_LEN_RFC2133)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 return -EINVAL;
4696 snum = ntohs(addr6->sin6_port);
Richard Haines68741a8a2018-03-02 19:54:34 +00004697 break;
4698 default:
4699 /* Note that SCTP services expect -EINVAL, whereas
4700 * others expect -EAFNOSUPPORT.
4701 */
4702 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4703 return -EINVAL;
4704 else
4705 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 }
4707
Paul Moore3e1121722008-04-10 10:48:14 -04004708 err = sel_netport_sid(sk->sk_protocol, snum, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 if (err)
Richard Hainesd4529302018-02-13 20:57:18 +00004710 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711
Richard Hainesd4529302018-02-13 20:57:18 +00004712 switch (sksec->sclass) {
4713 case SECCLASS_TCP_SOCKET:
4714 perm = TCP_SOCKET__NAME_CONNECT;
4715 break;
4716 case SECCLASS_DCCP_SOCKET:
4717 perm = DCCP_SOCKET__NAME_CONNECT;
4718 break;
4719 case SECCLASS_SCTP_SOCKET:
4720 perm = SCTP_SOCKET__NAME_CONNECT;
4721 break;
4722 }
James Morris2ee92d42006-11-13 16:09:01 -08004723
Eric Paris50c205f2012-04-04 15:01:43 -04004724 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004725 ad.u.net = &net;
4726 ad.u.net->dport = htons(snum);
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004727 ad.u.net->family = address->sa_family;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004728 err = avc_has_perm(&selinux_state,
4729 sksec->sid, sid, sksec->sclass, perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 if (err)
Richard Hainesd4529302018-02-13 20:57:18 +00004731 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732 }
4733
Richard Hainesd4529302018-02-13 20:57:18 +00004734 return 0;
4735}
Paul Moore014ab192008-10-10 10:16:33 -04004736
Richard Hainesd4529302018-02-13 20:57:18 +00004737/* Supports connect(2), see comments in selinux_socket_connect_helper() */
4738static int selinux_socket_connect(struct socket *sock,
4739 struct sockaddr *address, int addrlen)
4740{
4741 int err;
4742 struct sock *sk = sock->sk;
4743
4744 err = selinux_socket_connect_helper(sock, address, addrlen);
4745 if (err)
4746 return err;
4747
4748 return selinux_netlbl_socket_connect(sk, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749}
4750
4751static int selinux_socket_listen(struct socket *sock, int backlog)
4752{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004753 return sock_has_perm(sock->sk, SOCKET__LISTEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754}
4755
4756static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
4757{
4758 int err;
4759 struct inode_security_struct *isec;
4760 struct inode_security_struct *newisec;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004761 u16 sclass;
4762 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004764 err = sock_has_perm(sock->sk, SOCKET__ACCEPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 if (err)
4766 return err;
4767
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05004768 isec = inode_security_novalidate(SOCK_INODE(sock));
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004769 spin_lock(&isec->lock);
4770 sclass = isec->sclass;
4771 sid = isec->sid;
4772 spin_unlock(&isec->lock);
4773
4774 newisec = inode_security_novalidate(SOCK_INODE(newsock));
4775 newisec->sclass = sclass;
4776 newisec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004777 newisec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778
4779 return 0;
4780}
4781
4782static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
Eric Paris828dfe12008-04-17 13:17:49 -04004783 int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004785 return sock_has_perm(sock->sk, SOCKET__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786}
4787
4788static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
4789 int size, int flags)
4790{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004791 return sock_has_perm(sock->sk, SOCKET__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792}
4793
4794static int selinux_socket_getsockname(struct socket *sock)
4795{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004796 return sock_has_perm(sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797}
4798
4799static int selinux_socket_getpeername(struct socket *sock)
4800{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004801 return sock_has_perm(sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802}
4803
Eric Paris828dfe12008-04-17 13:17:49 -04004804static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805{
Paul Mooref8687af2006-10-30 15:22:15 -08004806 int err;
4807
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004808 err = sock_has_perm(sock->sk, SOCKET__SETOPT);
Paul Mooref8687af2006-10-30 15:22:15 -08004809 if (err)
4810 return err;
4811
4812 return selinux_netlbl_socket_setsockopt(sock, level, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813}
4814
4815static int selinux_socket_getsockopt(struct socket *sock, int level,
4816 int optname)
4817{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004818 return sock_has_perm(sock->sk, SOCKET__GETOPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819}
4820
4821static int selinux_socket_shutdown(struct socket *sock, int how)
4822{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004823 return sock_has_perm(sock->sk, SOCKET__SHUTDOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824}
4825
David S. Miller3610cda2011-01-05 15:38:53 -08004826static int selinux_socket_unix_stream_connect(struct sock *sock,
4827 struct sock *other,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828 struct sock *newsk)
4829{
David S. Miller3610cda2011-01-05 15:38:53 -08004830 struct sk_security_struct *sksec_sock = sock->sk_security;
4831 struct sk_security_struct *sksec_other = other->sk_security;
Paul Moore4d1e2452010-04-22 14:46:18 -04004832 struct sk_security_struct *sksec_new = newsk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004833 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004834 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835 int err;
4836
Eric Paris50c205f2012-04-04 15:01:43 -04004837 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004838 ad.u.net = &net;
4839 ad.u.net->sk = other;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004841 err = avc_has_perm(&selinux_state,
4842 sksec_sock->sid, sksec_other->sid,
Paul Moore4d1e2452010-04-22 14:46:18 -04004843 sksec_other->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 UNIX_STREAM_SOCKET__CONNECTTO, &ad);
4845 if (err)
4846 return err;
4847
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848 /* server child socket */
Paul Moore4d1e2452010-04-22 14:46:18 -04004849 sksec_new->peer_sid = sksec_sock->sid;
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004850 err = security_sid_mls_copy(&selinux_state, sksec_other->sid,
4851 sksec_sock->sid, &sksec_new->sid);
Paul Moore4d1e2452010-04-22 14:46:18 -04004852 if (err)
4853 return err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004854
Paul Moore4d1e2452010-04-22 14:46:18 -04004855 /* connecting socket */
4856 sksec_sock->peer_sid = sksec_new->sid;
4857
4858 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859}
4860
4861static int selinux_socket_unix_may_send(struct socket *sock,
4862 struct socket *other)
4863{
Paul Moore253bfae2010-04-22 14:46:19 -04004864 struct sk_security_struct *ssec = sock->sk->sk_security;
4865 struct sk_security_struct *osec = other->sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004866 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004867 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868
Eric Paris50c205f2012-04-04 15:01:43 -04004869 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004870 ad.u.net = &net;
4871 ad.u.net->sk = other->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004873 return avc_has_perm(&selinux_state,
4874 ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
Paul Moore253bfae2010-04-22 14:46:19 -04004875 &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876}
4877
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004878static int selinux_inet_sys_rcv_skb(struct net *ns, int ifindex,
4879 char *addrp, u16 family, u32 peer_sid,
Thomas Liu2bf49692009-07-14 12:14:09 -04004880 struct common_audit_data *ad)
Paul Mooreeffad8d2008-01-29 08:49:27 -05004881{
4882 int err;
4883 u32 if_sid;
4884 u32 node_sid;
4885
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004886 err = sel_netif_sid(ns, ifindex, &if_sid);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004887 if (err)
4888 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004889 err = avc_has_perm(&selinux_state,
4890 peer_sid, if_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004891 SECCLASS_NETIF, NETIF__INGRESS, ad);
4892 if (err)
4893 return err;
4894
4895 err = sel_netnode_sid(addrp, family, &node_sid);
4896 if (err)
4897 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004898 return avc_has_perm(&selinux_state,
4899 peer_sid, node_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004900 SECCLASS_NODE, NODE__RECVFROM, ad);
4901}
4902
Paul Moore220deb92008-01-29 08:38:23 -05004903static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
Paul Moored8395c82008-10-10 10:16:30 -04004904 u16 family)
Paul Moore220deb92008-01-29 08:38:23 -05004905{
Paul Moore277d3422008-12-31 12:54:11 -05004906 int err = 0;
Paul Moore220deb92008-01-29 08:38:23 -05004907 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004908 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004909 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004910 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04004911 char *addrp;
4912
Eric Paris50c205f2012-04-04 15:01:43 -04004913 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004914 ad.u.net = &net;
4915 ad.u.net->netif = skb->skb_iif;
4916 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004917 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4918 if (err)
4919 return err;
Paul Moore220deb92008-01-29 08:38:23 -05004920
Paul Moore58bfbb52009-03-27 17:10:41 -04004921 if (selinux_secmark_enabled()) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004922 err = avc_has_perm(&selinux_state,
4923 sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Moored8395c82008-10-10 10:16:30 -04004924 PACKET__RECV, &ad);
Paul Moore58bfbb52009-03-27 17:10:41 -04004925 if (err)
4926 return err;
4927 }
Paul Moore220deb92008-01-29 08:38:23 -05004928
Steffen Klassertb9679a72011-02-23 12:55:21 +01004929 err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
4930 if (err)
4931 return err;
4932 err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004933
James Morris4e5ab4c2006-06-09 00:33:33 -07004934 return err;
4935}
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004936
James Morris4e5ab4c2006-06-09 00:33:33 -07004937static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
4938{
Paul Moore220deb92008-01-29 08:38:23 -05004939 int err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004940 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004941 u16 family = sk->sk_family;
4942 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004943 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004944 struct lsm_network_audit net = {0,};
Paul Moore220deb92008-01-29 08:38:23 -05004945 char *addrp;
Paul Moored8395c82008-10-10 10:16:30 -04004946 u8 secmark_active;
4947 u8 peerlbl_active;
James Morris4e5ab4c2006-06-09 00:33:33 -07004948
James Morris4e5ab4c2006-06-09 00:33:33 -07004949 if (family != PF_INET && family != PF_INET6)
Paul Moore220deb92008-01-29 08:38:23 -05004950 return 0;
James Morris4e5ab4c2006-06-09 00:33:33 -07004951
4952 /* Handle mapped IPv4 packets arriving via IPv6 sockets */
Al Viro87fcd702006-12-04 22:00:55 +00004953 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
James Morris4e5ab4c2006-06-09 00:33:33 -07004954 family = PF_INET;
4955
Paul Moored8395c82008-10-10 10:16:30 -04004956 /* If any sort of compatibility mode is enabled then handoff processing
4957 * to the selinux_sock_rcv_skb_compat() function to deal with the
4958 * special handling. We do this in an attempt to keep this function
4959 * as fast and as clean as possible. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004960 if (!selinux_policycap_netpeer())
Paul Moored8395c82008-10-10 10:16:30 -04004961 return selinux_sock_rcv_skb_compat(sk, skb, family);
4962
4963 secmark_active = selinux_secmark_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04004964 peerlbl_active = selinux_peerlbl_enabled();
Paul Moored8395c82008-10-10 10:16:30 -04004965 if (!secmark_active && !peerlbl_active)
4966 return 0;
4967
Eric Paris50c205f2012-04-04 15:01:43 -04004968 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004969 ad.u.net = &net;
4970 ad.u.net->netif = skb->skb_iif;
4971 ad.u.net->family = family;
Paul Moore224dfbd2008-01-29 08:38:13 -05004972 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
James Morris4e5ab4c2006-06-09 00:33:33 -07004973 if (err)
Paul Moore220deb92008-01-29 08:38:23 -05004974 return err;
James Morris4e5ab4c2006-06-09 00:33:33 -07004975
Paul Moored8395c82008-10-10 10:16:30 -04004976 if (peerlbl_active) {
Paul Moored621d352008-01-29 08:43:36 -05004977 u32 peer_sid;
4978
4979 err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
4980 if (err)
4981 return err;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004982 err = selinux_inet_sys_rcv_skb(sock_net(sk), skb->skb_iif,
4983 addrp, family, peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04004984 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04004985 selinux_netlbl_err(skb, family, err, 0);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004986 return err;
Paul Mooredfaebe92008-10-10 10:16:31 -04004987 }
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004988 err = avc_has_perm(&selinux_state,
4989 sk_sid, peer_sid, SECCLASS_PEER,
Paul Moored621d352008-01-29 08:43:36 -05004990 PEER__RECV, &ad);
Chad Hanson46d01d62013-12-23 17:45:01 -05004991 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04004992 selinux_netlbl_err(skb, family, err, 0);
Chad Hanson46d01d62013-12-23 17:45:01 -05004993 return err;
4994 }
Paul Moored621d352008-01-29 08:43:36 -05004995 }
4996
Paul Moored8395c82008-10-10 10:16:30 -04004997 if (secmark_active) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004998 err = avc_has_perm(&selinux_state,
4999 sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005000 PACKET__RECV, &ad);
5001 if (err)
5002 return err;
5003 }
5004
Paul Moored621d352008-01-29 08:43:36 -05005005 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006}
5007
Catherine Zhang2c7946a2006-03-20 22:41:23 -08005008static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
5009 int __user *optlen, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010{
5011 int err = 0;
5012 char *scontext;
5013 u32 scontext_len;
Paul Moore253bfae2010-04-22 14:46:19 -04005014 struct sk_security_struct *sksec = sock->sk->sk_security;
Paul Moore3de4bab2006-11-17 17:38:54 -05005015 u32 peer_sid = SECSID_NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016
Paul Moore253bfae2010-04-22 14:46:19 -04005017 if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
Richard Hainesd4529302018-02-13 20:57:18 +00005018 sksec->sclass == SECCLASS_TCP_SOCKET ||
5019 sksec->sclass == SECCLASS_SCTP_SOCKET)
Eric Parisdd3e7832010-04-07 15:08:46 -04005020 peer_sid = sksec->peer_sid;
Paul Moore253bfae2010-04-22 14:46:19 -04005021 if (peer_sid == SECSID_NULL)
5022 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005024 err = security_sid_to_context(&selinux_state, peer_sid, &scontext,
5025 &scontext_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026 if (err)
Paul Moore253bfae2010-04-22 14:46:19 -04005027 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028
5029 if (scontext_len > len) {
5030 err = -ERANGE;
5031 goto out_len;
5032 }
5033
5034 if (copy_to_user(optval, scontext, scontext_len))
5035 err = -EFAULT;
5036
5037out_len:
5038 if (put_user(scontext_len, optlen))
5039 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040 kfree(scontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041 return err;
5042}
5043
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005044static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
Catherine Zhang2c7946a2006-03-20 22:41:23 -08005045{
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005046 u32 peer_secid = SECSID_NULL;
Paul Moore75e22912008-01-29 08:38:04 -05005047 u16 family;
Paul Moore899134f2016-03-28 15:19:10 -04005048 struct inode_security_struct *isec;
Catherine Zhang877ce7c2006-06-29 12:27:47 -07005049
Paul Mooreaa862902008-10-10 10:16:29 -04005050 if (skb && skb->protocol == htons(ETH_P_IP))
5051 family = PF_INET;
5052 else if (skb && skb->protocol == htons(ETH_P_IPV6))
5053 family = PF_INET6;
5054 else if (sock)
Paul Moore75e22912008-01-29 08:38:04 -05005055 family = sock->sk->sk_family;
Paul Moore75e22912008-01-29 08:38:04 -05005056 else
5057 goto out;
5058
Paul Moore899134f2016-03-28 15:19:10 -04005059 if (sock && family == PF_UNIX) {
5060 isec = inode_security_novalidate(SOCK_INODE(sock));
5061 peer_secid = isec->sid;
5062 } else if (skb)
Paul Moore220deb92008-01-29 08:38:23 -05005063 selinux_skb_peerlbl_sid(skb, family, &peer_secid);
Catherine Zhang2c7946a2006-03-20 22:41:23 -08005064
Paul Moore75e22912008-01-29 08:38:04 -05005065out:
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005066 *secid = peer_secid;
Paul Moore75e22912008-01-29 08:38:04 -05005067 if (peer_secid == SECSID_NULL)
5068 return -EINVAL;
5069 return 0;
Catherine Zhang2c7946a2006-03-20 22:41:23 -08005070}
5071
Al Viro7d877f32005-10-21 03:20:43 -04005072static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073{
Paul Moore84914b72010-04-22 14:46:18 -04005074 struct sk_security_struct *sksec;
5075
5076 sksec = kzalloc(sizeof(*sksec), priority);
5077 if (!sksec)
5078 return -ENOMEM;
5079
5080 sksec->peer_sid = SECINITSID_UNLABELED;
5081 sksec->sid = SECINITSID_UNLABELED;
Stephen Smalley5dee25d2015-07-10 17:19:57 -04005082 sksec->sclass = SECCLASS_SOCKET;
Paul Moore84914b72010-04-22 14:46:18 -04005083 selinux_netlbl_sk_security_reset(sksec);
5084 sk->sk_security = sksec;
5085
5086 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087}
5088
5089static void selinux_sk_free_security(struct sock *sk)
5090{
Paul Moore84914b72010-04-22 14:46:18 -04005091 struct sk_security_struct *sksec = sk->sk_security;
5092
5093 sk->sk_security = NULL;
5094 selinux_netlbl_sk_security_free(sksec);
5095 kfree(sksec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096}
5097
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005098static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
5099{
Eric Parisdd3e7832010-04-07 15:08:46 -04005100 struct sk_security_struct *sksec = sk->sk_security;
5101 struct sk_security_struct *newsksec = newsk->sk_security;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005102
Eric Parisdd3e7832010-04-07 15:08:46 -04005103 newsksec->sid = sksec->sid;
5104 newsksec->peer_sid = sksec->peer_sid;
5105 newsksec->sclass = sksec->sclass;
Paul Moore99f59ed2006-08-29 17:53:48 -07005106
Eric Parisdd3e7832010-04-07 15:08:46 -04005107 selinux_netlbl_sk_security_reset(newsksec);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005108}
5109
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07005110static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005111{
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005112 if (!sk)
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07005113 *secid = SECINITSID_ANY_SOCKET;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005114 else {
5115 struct sk_security_struct *sksec = sk->sk_security;
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005116
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07005117 *secid = sksec->sid;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005118 }
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005119}
5120
Eric Paris828dfe12008-04-17 13:17:49 -04005121static void selinux_sock_graft(struct sock *sk, struct socket *parent)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005122{
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05005123 struct inode_security_struct *isec =
5124 inode_security_novalidate(SOCK_INODE(parent));
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005125 struct sk_security_struct *sksec = sk->sk_security;
5126
Paul Moore2873ead2014-07-28 10:42:48 -04005127 if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
5128 sk->sk_family == PF_UNIX)
David Woodhouse2148ccc2006-09-29 15:50:25 -07005129 isec->sid = sksec->sid;
Paul Moore220deb92008-01-29 08:38:23 -05005130 sksec->sclass = isec->sclass;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005131}
5132
Richard Hainesd4529302018-02-13 20:57:18 +00005133/* Called whenever SCTP receives an INIT chunk. This happens when an incoming
5134 * connect(2), sctp_connectx(3) or sctp_sendmsg(3) (with no association
5135 * already present).
5136 */
5137static int selinux_sctp_assoc_request(struct sctp_endpoint *ep,
5138 struct sk_buff *skb)
5139{
5140 struct sk_security_struct *sksec = ep->base.sk->sk_security;
5141 struct common_audit_data ad;
5142 struct lsm_network_audit net = {0,};
5143 u8 peerlbl_active;
5144 u32 peer_sid = SECINITSID_UNLABELED;
5145 u32 conn_sid;
5146 int err = 0;
5147
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005148 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005149 return 0;
5150
5151 peerlbl_active = selinux_peerlbl_enabled();
5152
5153 if (peerlbl_active) {
5154 /* This will return peer_sid = SECSID_NULL if there are
5155 * no peer labels, see security_net_peersid_resolve().
5156 */
5157 err = selinux_skb_peerlbl_sid(skb, ep->base.sk->sk_family,
5158 &peer_sid);
5159 if (err)
5160 return err;
5161
5162 if (peer_sid == SECSID_NULL)
5163 peer_sid = SECINITSID_UNLABELED;
5164 }
5165
5166 if (sksec->sctp_assoc_state == SCTP_ASSOC_UNSET) {
5167 sksec->sctp_assoc_state = SCTP_ASSOC_SET;
5168
5169 /* Here as first association on socket. As the peer SID
5170 * was allowed by peer recv (and the netif/node checks),
5171 * then it is approved by policy and used as the primary
5172 * peer SID for getpeercon(3).
5173 */
5174 sksec->peer_sid = peer_sid;
5175 } else if (sksec->peer_sid != peer_sid) {
5176 /* Other association peer SIDs are checked to enforce
5177 * consistency among the peer SIDs.
5178 */
5179 ad.type = LSM_AUDIT_DATA_NET;
5180 ad.u.net = &net;
5181 ad.u.net->sk = ep->base.sk;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005182 err = avc_has_perm(&selinux_state,
5183 sksec->peer_sid, peer_sid, sksec->sclass,
Richard Hainesd4529302018-02-13 20:57:18 +00005184 SCTP_SOCKET__ASSOCIATION, &ad);
5185 if (err)
5186 return err;
5187 }
5188
5189 /* Compute the MLS component for the connection and store
5190 * the information in ep. This will be used by SCTP TCP type
5191 * sockets and peeled off connections as they cause a new
5192 * socket to be generated. selinux_sctp_sk_clone() will then
5193 * plug this into the new socket.
5194 */
5195 err = selinux_conn_sid(sksec->sid, peer_sid, &conn_sid);
5196 if (err)
5197 return err;
5198
5199 ep->secid = conn_sid;
5200 ep->peer_secid = peer_sid;
5201
5202 /* Set any NetLabel labels including CIPSO/CALIPSO options. */
5203 return selinux_netlbl_sctp_assoc_request(ep, skb);
5204}
5205
5206/* Check if sctp IPv4/IPv6 addresses are valid for binding or connecting
5207 * based on their @optname.
5208 */
5209static int selinux_sctp_bind_connect(struct sock *sk, int optname,
5210 struct sockaddr *address,
5211 int addrlen)
5212{
5213 int len, err = 0, walk_size = 0;
5214 void *addr_buf;
5215 struct sockaddr *addr;
5216 struct socket *sock;
5217
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005218 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005219 return 0;
5220
5221 /* Process one or more addresses that may be IPv4 or IPv6 */
5222 sock = sk->sk_socket;
5223 addr_buf = address;
5224
5225 while (walk_size < addrlen) {
Ondrej Mosnacekc1383252018-11-13 16:16:08 +01005226 if (walk_size + sizeof(sa_family_t) > addrlen)
5227 return -EINVAL;
5228
Richard Hainesd4529302018-02-13 20:57:18 +00005229 addr = addr_buf;
5230 switch (addr->sa_family) {
Alexey Kodanev4152dc92018-05-11 20:15:13 +03005231 case AF_UNSPEC:
Richard Hainesd4529302018-02-13 20:57:18 +00005232 case AF_INET:
5233 len = sizeof(struct sockaddr_in);
5234 break;
5235 case AF_INET6:
5236 len = sizeof(struct sockaddr_in6);
5237 break;
5238 default:
Alexey Kodanev4152dc92018-05-11 20:15:13 +03005239 return -EINVAL;
Richard Hainesd4529302018-02-13 20:57:18 +00005240 }
5241
Xin Long292c9972019-03-09 00:07:34 +08005242 if (walk_size + len > addrlen)
5243 return -EINVAL;
5244
Richard Hainesd4529302018-02-13 20:57:18 +00005245 err = -EINVAL;
5246 switch (optname) {
5247 /* Bind checks */
5248 case SCTP_PRIMARY_ADDR:
5249 case SCTP_SET_PEER_PRIMARY_ADDR:
5250 case SCTP_SOCKOPT_BINDX_ADD:
5251 err = selinux_socket_bind(sock, addr, len);
5252 break;
5253 /* Connect checks */
5254 case SCTP_SOCKOPT_CONNECTX:
5255 case SCTP_PARAM_SET_PRIMARY:
5256 case SCTP_PARAM_ADD_IP:
5257 case SCTP_SENDMSG_CONNECT:
5258 err = selinux_socket_connect_helper(sock, addr, len);
5259 if (err)
5260 return err;
5261
5262 /* As selinux_sctp_bind_connect() is called by the
5263 * SCTP protocol layer, the socket is already locked,
5264 * therefore selinux_netlbl_socket_connect_locked() is
5265 * is called here. The situations handled are:
5266 * sctp_connectx(3), sctp_sendmsg(3), sendmsg(2),
5267 * whenever a new IP address is added or when a new
5268 * primary address is selected.
5269 * Note that an SCTP connect(2) call happens before
5270 * the SCTP protocol layer and is handled via
5271 * selinux_socket_connect().
5272 */
5273 err = selinux_netlbl_socket_connect_locked(sk, addr);
5274 break;
5275 }
5276
5277 if (err)
5278 return err;
5279
5280 addr_buf += len;
5281 walk_size += len;
5282 }
5283
5284 return 0;
5285}
5286
5287/* Called whenever a new socket is created by accept(2) or sctp_peeloff(3). */
5288static void selinux_sctp_sk_clone(struct sctp_endpoint *ep, struct sock *sk,
5289 struct sock *newsk)
5290{
5291 struct sk_security_struct *sksec = sk->sk_security;
5292 struct sk_security_struct *newsksec = newsk->sk_security;
5293
5294 /* If policy does not support SECCLASS_SCTP_SOCKET then call
5295 * the non-sctp clone version.
5296 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005297 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005298 return selinux_sk_clone_security(sk, newsk);
5299
5300 newsksec->sid = ep->secid;
5301 newsksec->peer_sid = ep->peer_secid;
5302 newsksec->sclass = sksec->sclass;
5303 selinux_netlbl_sctp_sk_clone(sk, newsk);
5304}
5305
Adrian Bunk9a673e52006-08-15 00:03:53 -07005306static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
5307 struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005308{
5309 struct sk_security_struct *sksec = sk->sk_security;
5310 int err;
Paul Moore0b1f24e2013-12-03 11:39:13 -05005311 u16 family = req->rsk_ops->family;
Paul Moore446b8022013-12-04 16:10:51 -05005312 u32 connsid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005313 u32 peersid;
5314
Paul Mooreaa862902008-10-10 10:16:29 -04005315 err = selinux_skb_peerlbl_sid(skb, family, &peersid);
Paul Moore220deb92008-01-29 08:38:23 -05005316 if (err)
5317 return err;
Paul Moore446b8022013-12-04 16:10:51 -05005318 err = selinux_conn_sid(sksec->sid, peersid, &connsid);
5319 if (err)
5320 return err;
5321 req->secid = connsid;
5322 req->peer_secid = peersid;
Venkat Yekkiralaa51c64f2006-07-27 22:01:34 -07005323
Paul Moore389fb8002009-03-27 17:10:34 -04005324 return selinux_netlbl_inet_conn_request(req, family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005325}
5326
Adrian Bunk9a673e52006-08-15 00:03:53 -07005327static void selinux_inet_csk_clone(struct sock *newsk,
5328 const struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005329{
5330 struct sk_security_struct *newsksec = newsk->sk_security;
5331
5332 newsksec->sid = req->secid;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005333 newsksec->peer_sid = req->peer_secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005334 /* NOTE: Ideally, we should also get the isec->sid for the
5335 new socket in sync, but we don't have the isec available yet.
5336 So we will wait until sock_graft to do it, by which
5337 time it will have been created and available. */
Paul Moore99f59ed2006-08-29 17:53:48 -07005338
Paul Moore9f2ad662006-11-17 17:38:53 -05005339 /* We don't need to take any sort of lock here as we are the only
5340 * thread with access to newsksec */
Paul Moore389fb8002009-03-27 17:10:34 -04005341 selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005342}
5343
Paul Moore014ab192008-10-10 10:16:33 -04005344static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005345{
Paul Mooreaa862902008-10-10 10:16:29 -04005346 u16 family = sk->sk_family;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005347 struct sk_security_struct *sksec = sk->sk_security;
5348
Paul Mooreaa862902008-10-10 10:16:29 -04005349 /* handle mapped IPv4 packets arriving via IPv6 sockets */
5350 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
5351 family = PF_INET;
5352
5353 selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005354}
5355
Eric Paris2606fd12010-10-13 16:24:41 -04005356static int selinux_secmark_relabel_packet(u32 sid)
5357{
5358 const struct task_security_struct *__tsec;
5359 u32 tsid;
5360
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07005361 __tsec = selinux_cred(current_cred());
Eric Paris2606fd12010-10-13 16:24:41 -04005362 tsid = __tsec->sid;
5363
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005364 return avc_has_perm(&selinux_state,
5365 tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO,
5366 NULL);
Eric Paris2606fd12010-10-13 16:24:41 -04005367}
5368
5369static void selinux_secmark_refcount_inc(void)
5370{
5371 atomic_inc(&selinux_secmark_refcount);
5372}
5373
5374static void selinux_secmark_refcount_dec(void)
5375{
5376 atomic_dec(&selinux_secmark_refcount);
5377}
5378
Adrian Bunk9a673e52006-08-15 00:03:53 -07005379static void selinux_req_classify_flow(const struct request_sock *req,
5380 struct flowi *fl)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005381{
David S. Miller1d28f422011-03-12 00:29:39 -05005382 fl->flowi_secid = req->secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005383}
5384
Paul Moore5dbbaf22013-01-14 07:12:19 +00005385static int selinux_tun_dev_alloc_security(void **security)
5386{
5387 struct tun_security_struct *tunsec;
5388
5389 tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL);
5390 if (!tunsec)
5391 return -ENOMEM;
5392 tunsec->sid = current_sid();
5393
5394 *security = tunsec;
5395 return 0;
5396}
5397
5398static void selinux_tun_dev_free_security(void *security)
5399{
5400 kfree(security);
5401}
5402
Paul Mooreed6d76e2009-08-28 18:12:49 -04005403static int selinux_tun_dev_create(void)
5404{
5405 u32 sid = current_sid();
5406
5407 /* we aren't taking into account the "sockcreate" SID since the socket
5408 * that is being created here is not a socket in the traditional sense,
5409 * instead it is a private sock, accessible only to the kernel, and
5410 * representing a wide range of network traffic spanning multiple
5411 * connections unlike traditional sockets - check the TUN driver to
5412 * get a better understanding of why this socket is special */
5413
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005414 return avc_has_perm(&selinux_state,
5415 sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005416 NULL);
5417}
5418
Paul Moore5dbbaf22013-01-14 07:12:19 +00005419static int selinux_tun_dev_attach_queue(void *security)
Paul Mooreed6d76e2009-08-28 18:12:49 -04005420{
Paul Moore5dbbaf22013-01-14 07:12:19 +00005421 struct tun_security_struct *tunsec = security;
5422
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005423 return avc_has_perm(&selinux_state,
5424 current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET,
Paul Moore5dbbaf22013-01-14 07:12:19 +00005425 TUN_SOCKET__ATTACH_QUEUE, NULL);
5426}
5427
5428static int selinux_tun_dev_attach(struct sock *sk, void *security)
5429{
5430 struct tun_security_struct *tunsec = security;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005431 struct sk_security_struct *sksec = sk->sk_security;
5432
5433 /* we don't currently perform any NetLabel based labeling here and it
5434 * isn't clear that we would want to do so anyway; while we could apply
5435 * labeling without the support of the TUN user the resulting labeled
5436 * traffic from the other end of the connection would almost certainly
5437 * cause confusion to the TUN user that had no idea network labeling
5438 * protocols were being used */
5439
Paul Moore5dbbaf22013-01-14 07:12:19 +00005440 sksec->sid = tunsec->sid;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005441 sksec->sclass = SECCLASS_TUN_SOCKET;
Paul Moore5dbbaf22013-01-14 07:12:19 +00005442
5443 return 0;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005444}
5445
Paul Moore5dbbaf22013-01-14 07:12:19 +00005446static int selinux_tun_dev_open(void *security)
Paul Mooreed6d76e2009-08-28 18:12:49 -04005447{
Paul Moore5dbbaf22013-01-14 07:12:19 +00005448 struct tun_security_struct *tunsec = security;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005449 u32 sid = current_sid();
5450 int err;
5451
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005452 err = avc_has_perm(&selinux_state,
5453 sid, tunsec->sid, SECCLASS_TUN_SOCKET,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005454 TUN_SOCKET__RELABELFROM, NULL);
5455 if (err)
5456 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005457 err = avc_has_perm(&selinux_state,
5458 sid, sid, SECCLASS_TUN_SOCKET,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005459 TUN_SOCKET__RELABELTO, NULL);
5460 if (err)
5461 return err;
Paul Moore5dbbaf22013-01-14 07:12:19 +00005462 tunsec->sid = sid;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005463
5464 return 0;
5465}
5466
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467static int selinux_nlmsg_perm(struct sock *sk, struct sk_buff *skb)
5468{
5469 int err = 0;
5470 u32 perm;
5471 struct nlmsghdr *nlh;
Paul Moore253bfae2010-04-22 14:46:19 -04005472 struct sk_security_struct *sksec = sk->sk_security;
Eric Paris828dfe12008-04-17 13:17:49 -04005473
Hong zhi guo77954982013-03-27 06:49:35 +00005474 if (skb->len < NLMSG_HDRLEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475 err = -EINVAL;
5476 goto out;
5477 }
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -07005478 nlh = nlmsg_hdr(skb);
Eric Paris828dfe12008-04-17 13:17:49 -04005479
Paul Moore253bfae2010-04-22 14:46:19 -04005480 err = selinux_nlmsg_lookup(sksec->sclass, nlh->nlmsg_type, &perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481 if (err) {
5482 if (err == -EINVAL) {
Vladis Dronov76319942015-12-24 11:09:41 -05005483 pr_warn_ratelimited("SELinux: unrecognized netlink"
5484 " message: protocol=%hu nlmsg_type=%hu sclass=%s"
5485 " pig=%d comm=%s\n",
Marek Milkoviccded3ff2015-06-04 16:22:16 -04005486 sk->sk_protocol, nlh->nlmsg_type,
Vladis Dronov76319942015-12-24 11:09:41 -05005487 secclass_map[sksec->sclass - 1].name,
5488 task_pid_nr(current), current->comm);
Paul Mooree5a5ca92018-03-01 17:38:30 -05005489 if (!enforcing_enabled(&selinux_state) ||
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005490 security_get_allow_unknown(&selinux_state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491 err = 0;
5492 }
5493
5494 /* Ignore */
5495 if (err == -ENOENT)
5496 err = 0;
5497 goto out;
5498 }
5499
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005500 err = sock_has_perm(sk, perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501out:
5502 return err;
5503}
5504
5505#ifdef CONFIG_NETFILTER
5506
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005507static unsigned int selinux_ip_forward(struct sk_buff *skb,
5508 const struct net_device *indev,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005509 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510{
Paul Mooredfaebe92008-10-10 10:16:31 -04005511 int err;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005512 char *addrp;
5513 u32 peer_sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04005514 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005515 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05005516 u8 secmark_active;
Paul Moore948bf852008-10-10 10:16:32 -04005517 u8 netlbl_active;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005518 u8 peerlbl_active;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005519
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005520 if (!selinux_policycap_netpeer())
Paul Mooreeffad8d2008-01-29 08:49:27 -05005521 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005522
Paul Mooreeffad8d2008-01-29 08:49:27 -05005523 secmark_active = selinux_secmark_enabled();
Paul Moore948bf852008-10-10 10:16:32 -04005524 netlbl_active = netlbl_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04005525 peerlbl_active = selinux_peerlbl_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05005526 if (!secmark_active && !peerlbl_active)
5527 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005528
Paul Moored8395c82008-10-10 10:16:30 -04005529 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
5530 return NF_DROP;
5531
Eric Paris50c205f2012-04-04 15:01:43 -04005532 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005533 ad.u.net = &net;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005534 ad.u.net->netif = indev->ifindex;
Eric Paris48c62af2012-04-02 13:15:44 -04005535 ad.u.net->family = family;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005536 if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
5537 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538
Paul Mooredfaebe92008-10-10 10:16:31 -04005539 if (peerlbl_active) {
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005540 err = selinux_inet_sys_rcv_skb(dev_net(indev), indev->ifindex,
5541 addrp, family, peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04005542 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04005543 selinux_netlbl_err(skb, family, err, 1);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005544 return NF_DROP;
Paul Mooredfaebe92008-10-10 10:16:31 -04005545 }
5546 }
Paul Mooreeffad8d2008-01-29 08:49:27 -05005547
5548 if (secmark_active)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005549 if (avc_has_perm(&selinux_state,
5550 peer_sid, skb->secmark,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005551 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
5552 return NF_DROP;
5553
Paul Moore948bf852008-10-10 10:16:32 -04005554 if (netlbl_active)
5555 /* we do this in the FORWARD path and not the POST_ROUTING
5556 * path because we want to make sure we apply the necessary
5557 * labeling before IPsec is applied so we can leverage AH
5558 * protection */
5559 if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
5560 return NF_DROP;
5561
Paul Mooreeffad8d2008-01-29 08:49:27 -05005562 return NF_ACCEPT;
5563}
5564
Eric W. Biederman06198b32015-09-18 14:33:06 -05005565static unsigned int selinux_ipv4_forward(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005566 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005567 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005568{
David S. Miller238e54c2015-04-03 20:32:56 -04005569 return selinux_ip_forward(skb, state->in, PF_INET);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005570}
5571
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005572#if IS_ENABLED(CONFIG_IPV6)
Eric W. Biederman06198b32015-09-18 14:33:06 -05005573static unsigned int selinux_ipv6_forward(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005574 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005575 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005576{
David S. Miller238e54c2015-04-03 20:32:56 -04005577 return selinux_ip_forward(skb, state->in, PF_INET6);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005578}
5579#endif /* IPV6 */
5580
Paul Moore948bf852008-10-10 10:16:32 -04005581static unsigned int selinux_ip_output(struct sk_buff *skb,
5582 u16 family)
5583{
Paul Moore47180062013-12-04 16:10:45 -05005584 struct sock *sk;
Paul Moore948bf852008-10-10 10:16:32 -04005585 u32 sid;
5586
5587 if (!netlbl_enabled())
5588 return NF_ACCEPT;
5589
5590 /* we do this in the LOCAL_OUT path and not the POST_ROUTING path
5591 * because we want to make sure we apply the necessary labeling
5592 * before IPsec is applied so we can leverage AH protection */
Paul Moore47180062013-12-04 16:10:45 -05005593 sk = skb->sk;
5594 if (sk) {
5595 struct sk_security_struct *sksec;
5596
Eric Dumazete446f9d2015-10-08 05:01:55 -07005597 if (sk_listener(sk))
Paul Moore47180062013-12-04 16:10:45 -05005598 /* if the socket is the listening state then this
5599 * packet is a SYN-ACK packet which means it needs to
5600 * be labeled based on the connection/request_sock and
5601 * not the parent socket. unfortunately, we can't
5602 * lookup the request_sock yet as it isn't queued on
5603 * the parent socket until after the SYN-ACK is sent.
5604 * the "solution" is to simply pass the packet as-is
5605 * as any IP option based labeling should be copied
5606 * from the initial connection request (in the IP
5607 * layer). it is far from ideal, but until we get a
5608 * security label in the packet itself this is the
5609 * best we can do. */
5610 return NF_ACCEPT;
5611
5612 /* standard practice, label using the parent socket */
5613 sksec = sk->sk_security;
Paul Moore948bf852008-10-10 10:16:32 -04005614 sid = sksec->sid;
5615 } else
5616 sid = SECINITSID_KERNEL;
5617 if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
5618 return NF_DROP;
5619
5620 return NF_ACCEPT;
5621}
5622
Eric W. Biederman06198b32015-09-18 14:33:06 -05005623static unsigned int selinux_ipv4_output(void *priv,
Paul Moore948bf852008-10-10 10:16:32 -04005624 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005625 const struct nf_hook_state *state)
Paul Moore948bf852008-10-10 10:16:32 -04005626{
5627 return selinux_ip_output(skb, PF_INET);
5628}
5629
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005630#if IS_ENABLED(CONFIG_IPV6)
Huw Davies2917f572016-06-27 15:06:15 -04005631static unsigned int selinux_ipv6_output(void *priv,
5632 struct sk_buff *skb,
5633 const struct nf_hook_state *state)
5634{
5635 return selinux_ip_output(skb, PF_INET6);
5636}
5637#endif /* IPV6 */
5638
Paul Mooreeffad8d2008-01-29 08:49:27 -05005639static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
5640 int ifindex,
Paul Moored8395c82008-10-10 10:16:30 -04005641 u16 family)
James Morris4e5ab4c2006-06-09 00:33:33 -07005642{
Eric Dumazet54abc682015-11-08 10:54:07 -08005643 struct sock *sk = skb_to_full_sk(skb);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005644 struct sk_security_struct *sksec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005645 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005646 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04005647 char *addrp;
5648 u8 proto;
James Morris4e5ab4c2006-06-09 00:33:33 -07005649
Paul Mooreeffad8d2008-01-29 08:49:27 -05005650 if (sk == NULL)
5651 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005652 sksec = sk->sk_security;
James Morris4e5ab4c2006-06-09 00:33:33 -07005653
Eric Paris50c205f2012-04-04 15:01:43 -04005654 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005655 ad.u.net = &net;
5656 ad.u.net->netif = ifindex;
5657 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04005658 if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
5659 return NF_DROP;
5660
Paul Moore58bfbb52009-03-27 17:10:41 -04005661 if (selinux_secmark_enabled())
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005662 if (avc_has_perm(&selinux_state,
5663 sksec->sid, skb->secmark,
Paul Moored8395c82008-10-10 10:16:30 -04005664 SECCLASS_PACKET, PACKET__SEND, &ad))
Eric Paris2fe66ec2010-11-23 06:28:08 +00005665 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07005666
Steffen Klassertb9679a72011-02-23 12:55:21 +01005667 if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
5668 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07005669
Paul Mooreeffad8d2008-01-29 08:49:27 -05005670 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671}
5672
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005673static unsigned int selinux_ip_postroute(struct sk_buff *skb,
5674 const struct net_device *outdev,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005675 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676{
Paul Mooreeffad8d2008-01-29 08:49:27 -05005677 u32 secmark_perm;
5678 u32 peer_sid;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005679 int ifindex = outdev->ifindex;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005680 struct sock *sk;
Thomas Liu2bf49692009-07-14 12:14:09 -04005681 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005682 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05005683 char *addrp;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005684 u8 secmark_active;
5685 u8 peerlbl_active;
5686
Paul Mooreeffad8d2008-01-29 08:49:27 -05005687 /* If any sort of compatibility mode is enabled then handoff processing
5688 * to the selinux_ip_postroute_compat() function to deal with the
5689 * special handling. We do this in an attempt to keep this function
5690 * as fast and as clean as possible. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005691 if (!selinux_policycap_netpeer())
Paul Moored8395c82008-10-10 10:16:30 -04005692 return selinux_ip_postroute_compat(skb, ifindex, family);
Paul Moorec0828e52013-12-10 14:58:01 -05005693
Paul Mooreeffad8d2008-01-29 08:49:27 -05005694 secmark_active = selinux_secmark_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04005695 peerlbl_active = selinux_peerlbl_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05005696 if (!secmark_active && !peerlbl_active)
5697 return NF_ACCEPT;
5698
Eric Dumazet54abc682015-11-08 10:54:07 -08005699 sk = skb_to_full_sk(skb);
Paul Moorec0828e52013-12-10 14:58:01 -05005700
Paul Mooreeffad8d2008-01-29 08:49:27 -05005701#ifdef CONFIG_XFRM
5702 /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
5703 * packet transformation so allow the packet to pass without any checks
5704 * since we'll have another chance to perform access control checks
5705 * when the packet is on it's final way out.
5706 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
Paul Moorec0828e52013-12-10 14:58:01 -05005707 * is NULL, in this case go ahead and apply access control.
5708 * NOTE: if this is a local socket (skb->sk != NULL) that is in the
5709 * TCP listening state we cannot wait until the XFRM processing
5710 * is done as we will miss out on the SA label if we do;
5711 * unfortunately, this means more work, but it is only once per
5712 * connection. */
5713 if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL &&
Eric Dumazete446f9d2015-10-08 05:01:55 -07005714 !(sk && sk_listener(sk)))
Paul Mooreeffad8d2008-01-29 08:49:27 -05005715 return NF_ACCEPT;
5716#endif
Paul Mooreeffad8d2008-01-29 08:49:27 -05005717
Paul Moored8395c82008-10-10 10:16:30 -04005718 if (sk == NULL) {
Paul Moore446b8022013-12-04 16:10:51 -05005719 /* Without an associated socket the packet is either coming
5720 * from the kernel or it is being forwarded; check the packet
5721 * to determine which and if the packet is being forwarded
5722 * query the packet directly to determine the security label. */
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005723 if (skb->skb_iif) {
5724 secmark_perm = PACKET__FORWARD_OUT;
Paul Moored8395c82008-10-10 10:16:30 -04005725 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005726 return NF_DROP;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005727 } else {
5728 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04005729 peer_sid = SECINITSID_KERNEL;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005730 }
Eric Dumazete446f9d2015-10-08 05:01:55 -07005731 } else if (sk_listener(sk)) {
Paul Moore446b8022013-12-04 16:10:51 -05005732 /* Locally generated packet but the associated socket is in the
5733 * listening state which means this is a SYN-ACK packet. In
5734 * this particular case the correct security label is assigned
5735 * to the connection/request_sock but unfortunately we can't
5736 * query the request_sock as it isn't queued on the parent
5737 * socket until after the SYN-ACK packet is sent; the only
5738 * viable choice is to regenerate the label like we do in
5739 * selinux_inet_conn_request(). See also selinux_ip_output()
5740 * for similar problems. */
5741 u32 skb_sid;
Eric Dumazete446f9d2015-10-08 05:01:55 -07005742 struct sk_security_struct *sksec;
5743
Eric Dumazete446f9d2015-10-08 05:01:55 -07005744 sksec = sk->sk_security;
Paul Moore446b8022013-12-04 16:10:51 -05005745 if (selinux_skb_peerlbl_sid(skb, family, &skb_sid))
5746 return NF_DROP;
Paul Moorec0828e52013-12-10 14:58:01 -05005747 /* At this point, if the returned skb peerlbl is SECSID_NULL
5748 * and the packet has been through at least one XFRM
5749 * transformation then we must be dealing with the "final"
5750 * form of labeled IPsec packet; since we've already applied
5751 * all of our access controls on this packet we can safely
5752 * pass the packet. */
5753 if (skb_sid == SECSID_NULL) {
5754 switch (family) {
5755 case PF_INET:
5756 if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
5757 return NF_ACCEPT;
5758 break;
5759 case PF_INET6:
5760 if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED)
5761 return NF_ACCEPT;
Paul Moorea7a91a12014-09-03 10:51:59 -04005762 break;
Paul Moorec0828e52013-12-10 14:58:01 -05005763 default:
5764 return NF_DROP_ERR(-ECONNREFUSED);
5765 }
5766 }
Paul Moore446b8022013-12-04 16:10:51 -05005767 if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid))
5768 return NF_DROP;
5769 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04005770 } else {
Paul Moore446b8022013-12-04 16:10:51 -05005771 /* Locally generated packet, fetch the security label from the
5772 * associated socket. */
Paul Mooreeffad8d2008-01-29 08:49:27 -05005773 struct sk_security_struct *sksec = sk->sk_security;
5774 peer_sid = sksec->sid;
5775 secmark_perm = PACKET__SEND;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005776 }
5777
Eric Paris50c205f2012-04-04 15:01:43 -04005778 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005779 ad.u.net = &net;
5780 ad.u.net->netif = ifindex;
5781 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04005782 if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
Eric Paris04f6d702010-11-23 06:28:02 +00005783 return NF_DROP;
Paul Moored8395c82008-10-10 10:16:30 -04005784
Paul Mooreeffad8d2008-01-29 08:49:27 -05005785 if (secmark_active)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005786 if (avc_has_perm(&selinux_state,
5787 peer_sid, skb->secmark,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005788 SECCLASS_PACKET, secmark_perm, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005789 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005790
5791 if (peerlbl_active) {
5792 u32 if_sid;
5793 u32 node_sid;
5794
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005795 if (sel_netif_sid(dev_net(outdev), ifindex, &if_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005796 return NF_DROP;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005797 if (avc_has_perm(&selinux_state,
5798 peer_sid, if_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005799 SECCLASS_NETIF, NETIF__EGRESS, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005800 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005801
5802 if (sel_netnode_sid(addrp, family, &node_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005803 return NF_DROP;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005804 if (avc_has_perm(&selinux_state,
5805 peer_sid, node_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005806 SECCLASS_NODE, NODE__SENDTO, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005807 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005808 }
5809
5810 return NF_ACCEPT;
5811}
5812
Eric W. Biederman06198b32015-09-18 14:33:06 -05005813static unsigned int selinux_ipv4_postroute(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005814 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005815 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005816{
David S. Miller238e54c2015-04-03 20:32:56 -04005817 return selinux_ip_postroute(skb, state->out, PF_INET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005818}
5819
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005820#if IS_ENABLED(CONFIG_IPV6)
Eric W. Biederman06198b32015-09-18 14:33:06 -05005821static unsigned int selinux_ipv6_postroute(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005822 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005823 const struct nf_hook_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005824{
David S. Miller238e54c2015-04-03 20:32:56 -04005825 return selinux_ip_postroute(skb, state->out, PF_INET6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827#endif /* IPV6 */
5828
5829#endif /* CONFIG_NETFILTER */
5830
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
5832{
Stephen Smalley941fc5b2009-10-01 14:48:23 -04005833 return selinux_nlmsg_perm(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005834}
5835
Casey Schauflerecd5f822018-11-20 11:55:02 -08005836static void ipc_init_security(struct ipc_security_struct *isec, u16 sclass)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838 isec->sclass = sclass;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005839 isec->sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840}
5841
5842static int msg_msg_alloc_security(struct msg_msg *msg)
5843{
5844 struct msg_security_struct *msec;
5845
Casey Schauflerecd5f822018-11-20 11:55:02 -08005846 msec = selinux_msg_msg(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005847 msec->sid = SECINITSID_UNLABELED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848
5849 return 0;
5850}
5851
Linus Torvalds1da177e2005-04-16 15:20:36 -07005852static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
Stephen Smalley6af963f2005-05-01 08:58:39 -07005853 u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005856 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005857 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005858
Casey Schaufler7c653822018-09-21 17:19:45 -07005859 isec = selinux_ipc(ipc_perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860
Eric Paris50c205f2012-04-04 15:01:43 -04005861 ad.type = LSM_AUDIT_DATA_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862 ad.u.ipc_id = ipc_perms->key;
5863
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005864 return avc_has_perm(&selinux_state,
5865 sid, isec->sid, isec->sclass, perms, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866}
5867
5868static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
5869{
5870 return msg_msg_alloc_security(msg);
5871}
5872
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873/* message queue security operations */
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005874static int selinux_msg_queue_alloc_security(struct kern_ipc_perm *msq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005875{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005877 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005878 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005879 int rc;
5880
Casey Schauflerecd5f822018-11-20 11:55:02 -08005881 isec = selinux_ipc(msq);
5882 ipc_init_security(isec, SECCLASS_MSGQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005883
Eric Paris50c205f2012-04-04 15:01:43 -04005884 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005885 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005887 rc = avc_has_perm(&selinux_state,
5888 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005889 MSGQ__CREATE, &ad);
Casey Schauflerecd5f822018-11-20 11:55:02 -08005890 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891}
5892
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005893static int selinux_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005896 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005897 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898
Casey Schaufler7c653822018-09-21 17:19:45 -07005899 isec = selinux_ipc(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900
Eric Paris50c205f2012-04-04 15:01:43 -04005901 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005902 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005904 return avc_has_perm(&selinux_state,
5905 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005906 MSGQ__ASSOCIATE, &ad);
5907}
5908
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005909static int selinux_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910{
5911 int err;
5912 int perms;
5913
Eric Paris828dfe12008-04-17 13:17:49 -04005914 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005915 case IPC_INFO:
5916 case MSG_INFO:
5917 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005918 return avc_has_perm(&selinux_state,
5919 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005920 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921 case IPC_STAT:
5922 case MSG_STAT:
Davidlohr Bueso23c8cec2018-04-10 16:35:30 -07005923 case MSG_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924 perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
5925 break;
5926 case IPC_SET:
5927 perms = MSGQ__SETATTR;
5928 break;
5929 case IPC_RMID:
5930 perms = MSGQ__DESTROY;
5931 break;
5932 default:
5933 return 0;
5934 }
5935
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005936 err = ipc_has_perm(msq, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005937 return err;
5938}
5939
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005940static int selinux_msg_queue_msgsnd(struct kern_ipc_perm *msq, struct msg_msg *msg, int msqflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005942 struct ipc_security_struct *isec;
5943 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005944 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005945 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946 int rc;
5947
Casey Schaufler7c653822018-09-21 17:19:45 -07005948 isec = selinux_ipc(msq);
5949 msec = selinux_msg_msg(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005950
5951 /*
5952 * First time through, need to assign label to the message
5953 */
5954 if (msec->sid == SECINITSID_UNLABELED) {
5955 /*
5956 * Compute new sid based on current process and
5957 * message queue this message will be stored in
5958 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005959 rc = security_transition_sid(&selinux_state, sid, isec->sid,
5960 SECCLASS_MSG, NULL, &msec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005961 if (rc)
5962 return rc;
5963 }
5964
Eric Paris50c205f2012-04-04 15:01:43 -04005965 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005966 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005967
5968 /* Can this process write to the queue? */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005969 rc = avc_has_perm(&selinux_state,
5970 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005971 MSGQ__WRITE, &ad);
5972 if (!rc)
5973 /* Can this process send the message */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005974 rc = avc_has_perm(&selinux_state,
5975 sid, msec->sid, SECCLASS_MSG,
David Howells275bb412008-11-14 10:39:19 +11005976 MSG__SEND, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977 if (!rc)
5978 /* Can the message be put in the queue? */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005979 rc = avc_has_perm(&selinux_state,
5980 msec->sid, isec->sid, SECCLASS_MSGQ,
David Howells275bb412008-11-14 10:39:19 +11005981 MSGQ__ENQUEUE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982
5983 return rc;
5984}
5985
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005986static int selinux_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987 struct task_struct *target,
5988 long type, int mode)
5989{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005990 struct ipc_security_struct *isec;
5991 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005992 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005993 u32 sid = task_sid(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005994 int rc;
5995
Casey Schaufler7c653822018-09-21 17:19:45 -07005996 isec = selinux_ipc(msq);
5997 msec = selinux_msg_msg(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998
Eric Paris50c205f2012-04-04 15:01:43 -04005999 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05006000 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006001
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006002 rc = avc_has_perm(&selinux_state,
6003 sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006004 SECCLASS_MSGQ, MSGQ__READ, &ad);
6005 if (!rc)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006006 rc = avc_has_perm(&selinux_state,
6007 sid, msec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006008 SECCLASS_MSG, MSG__RECEIVE, &ad);
6009 return rc;
6010}
6011
6012/* Shared Memory security operations */
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006013static int selinux_shm_alloc_security(struct kern_ipc_perm *shp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006016 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006017 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018 int rc;
6019
Casey Schauflerecd5f822018-11-20 11:55:02 -08006020 isec = selinux_ipc(shp);
6021 ipc_init_security(isec, SECCLASS_SHM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022
Eric Paris50c205f2012-04-04 15:01:43 -04006023 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006024 ad.u.ipc_id = shp->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006025
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006026 rc = avc_has_perm(&selinux_state,
6027 sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028 SHM__CREATE, &ad);
Casey Schauflerecd5f822018-11-20 11:55:02 -08006029 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030}
6031
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006032static int selinux_shm_associate(struct kern_ipc_perm *shp, int shmflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006035 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006036 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006037
Casey Schaufler7c653822018-09-21 17:19:45 -07006038 isec = selinux_ipc(shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039
Eric Paris50c205f2012-04-04 15:01:43 -04006040 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006041 ad.u.ipc_id = shp->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006042
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006043 return avc_has_perm(&selinux_state,
6044 sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045 SHM__ASSOCIATE, &ad);
6046}
6047
6048/* Note, at this point, shp is locked down */
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006049static int selinux_shm_shmctl(struct kern_ipc_perm *shp, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050{
6051 int perms;
6052 int err;
6053
Eric Paris828dfe12008-04-17 13:17:49 -04006054 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006055 case IPC_INFO:
6056 case SHM_INFO:
6057 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006058 return avc_has_perm(&selinux_state,
6059 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006060 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006061 case IPC_STAT:
6062 case SHM_STAT:
Davidlohr Buesoc21a6972018-04-10 16:35:23 -07006063 case SHM_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006064 perms = SHM__GETATTR | SHM__ASSOCIATE;
6065 break;
6066 case IPC_SET:
6067 perms = SHM__SETATTR;
6068 break;
6069 case SHM_LOCK:
6070 case SHM_UNLOCK:
6071 perms = SHM__LOCK;
6072 break;
6073 case IPC_RMID:
6074 perms = SHM__DESTROY;
6075 break;
6076 default:
6077 return 0;
6078 }
6079
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006080 err = ipc_has_perm(shp, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081 return err;
6082}
6083
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006084static int selinux_shm_shmat(struct kern_ipc_perm *shp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085 char __user *shmaddr, int shmflg)
6086{
6087 u32 perms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088
6089 if (shmflg & SHM_RDONLY)
6090 perms = SHM__READ;
6091 else
6092 perms = SHM__READ | SHM__WRITE;
6093
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006094 return ipc_has_perm(shp, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095}
6096
6097/* Semaphore security operations */
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006098static int selinux_sem_alloc_security(struct kern_ipc_perm *sma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006100 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006101 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006102 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006103 int rc;
6104
Casey Schauflerecd5f822018-11-20 11:55:02 -08006105 isec = selinux_ipc(sma);
6106 ipc_init_security(isec, SECCLASS_SEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107
Eric Paris50c205f2012-04-04 15:01:43 -04006108 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006109 ad.u.ipc_id = sma->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006110
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006111 rc = avc_has_perm(&selinux_state,
6112 sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006113 SEM__CREATE, &ad);
Casey Schauflerecd5f822018-11-20 11:55:02 -08006114 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006115}
6116
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006117static int selinux_sem_associate(struct kern_ipc_perm *sma, int semflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006118{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006119 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006120 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006121 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006122
Casey Schaufler7c653822018-09-21 17:19:45 -07006123 isec = selinux_ipc(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006124
Eric Paris50c205f2012-04-04 15:01:43 -04006125 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006126 ad.u.ipc_id = sma->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006128 return avc_has_perm(&selinux_state,
6129 sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006130 SEM__ASSOCIATE, &ad);
6131}
6132
6133/* Note, at this point, sma is locked down */
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006134static int selinux_sem_semctl(struct kern_ipc_perm *sma, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006135{
6136 int err;
6137 u32 perms;
6138
Eric Paris828dfe12008-04-17 13:17:49 -04006139 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006140 case IPC_INFO:
6141 case SEM_INFO:
6142 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006143 return avc_has_perm(&selinux_state,
6144 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006145 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006146 case GETPID:
6147 case GETNCNT:
6148 case GETZCNT:
6149 perms = SEM__GETATTR;
6150 break;
6151 case GETVAL:
6152 case GETALL:
6153 perms = SEM__READ;
6154 break;
6155 case SETVAL:
6156 case SETALL:
6157 perms = SEM__WRITE;
6158 break;
6159 case IPC_RMID:
6160 perms = SEM__DESTROY;
6161 break;
6162 case IPC_SET:
6163 perms = SEM__SETATTR;
6164 break;
6165 case IPC_STAT:
6166 case SEM_STAT:
Davidlohr Buesoa280d6d2018-04-10 16:35:26 -07006167 case SEM_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006168 perms = SEM__GETATTR | SEM__ASSOCIATE;
6169 break;
6170 default:
6171 return 0;
6172 }
6173
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006174 err = ipc_has_perm(sma, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006175 return err;
6176}
6177
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006178static int selinux_sem_semop(struct kern_ipc_perm *sma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006179 struct sembuf *sops, unsigned nsops, int alter)
6180{
6181 u32 perms;
6182
6183 if (alter)
6184 perms = SEM__READ | SEM__WRITE;
6185 else
6186 perms = SEM__READ;
6187
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006188 return ipc_has_perm(sma, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006189}
6190
6191static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
6192{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006193 u32 av = 0;
6194
Linus Torvalds1da177e2005-04-16 15:20:36 -07006195 av = 0;
6196 if (flag & S_IRUGO)
6197 av |= IPC__UNIX_READ;
6198 if (flag & S_IWUGO)
6199 av |= IPC__UNIX_WRITE;
6200
6201 if (av == 0)
6202 return 0;
6203
Stephen Smalley6af963f2005-05-01 08:58:39 -07006204 return ipc_has_perm(ipcp, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205}
6206
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02006207static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
6208{
Casey Schaufler7c653822018-09-21 17:19:45 -07006209 struct ipc_security_struct *isec = selinux_ipc(ipcp);
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02006210 *secid = isec->sid;
6211}
6212
Eric Paris828dfe12008-04-17 13:17:49 -04006213static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006214{
6215 if (inode)
6216 inode_doinit_with_dentry(inode, dentry);
6217}
6218
6219static int selinux_getprocattr(struct task_struct *p,
Al Viro04ff9702007-03-12 16:17:58 +00006220 char *name, char **value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006221{
David Howells275bb412008-11-14 10:39:19 +11006222 const struct task_security_struct *__tsec;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00006223 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006224 int error;
Al Viro04ff9702007-03-12 16:17:58 +00006225 unsigned len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006226
David Howells275bb412008-11-14 10:39:19 +11006227 rcu_read_lock();
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07006228 __tsec = selinux_cred(__task_cred(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006229
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006230 if (current != p) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006231 error = avc_has_perm(&selinux_state,
6232 current_sid(), __tsec->sid,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006233 SECCLASS_PROCESS, PROCESS__GETATTR, NULL);
6234 if (error)
6235 goto bad;
6236 }
6237
Linus Torvalds1da177e2005-04-16 15:20:36 -07006238 if (!strcmp(name, "current"))
David Howells275bb412008-11-14 10:39:19 +11006239 sid = __tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006240 else if (!strcmp(name, "prev"))
David Howells275bb412008-11-14 10:39:19 +11006241 sid = __tsec->osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006242 else if (!strcmp(name, "exec"))
David Howells275bb412008-11-14 10:39:19 +11006243 sid = __tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006244 else if (!strcmp(name, "fscreate"))
David Howells275bb412008-11-14 10:39:19 +11006245 sid = __tsec->create_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006246 else if (!strcmp(name, "keycreate"))
David Howells275bb412008-11-14 10:39:19 +11006247 sid = __tsec->keycreate_sid;
Eric Paris42c3e032006-06-26 00:26:03 -07006248 else if (!strcmp(name, "sockcreate"))
David Howells275bb412008-11-14 10:39:19 +11006249 sid = __tsec->sockcreate_sid;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006250 else {
6251 error = -EINVAL;
6252 goto bad;
6253 }
David Howells275bb412008-11-14 10:39:19 +11006254 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006255
6256 if (!sid)
6257 return 0;
6258
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006259 error = security_sid_to_context(&selinux_state, sid, value, &len);
Al Viro04ff9702007-03-12 16:17:58 +00006260 if (error)
6261 return error;
6262 return len;
David Howells275bb412008-11-14 10:39:19 +11006263
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006264bad:
David Howells275bb412008-11-14 10:39:19 +11006265 rcu_read_unlock();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006266 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006267}
6268
Stephen Smalleyb21507e2017-01-09 10:07:31 -05006269static int selinux_setprocattr(const char *name, void *value, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006270{
6271 struct task_security_struct *tsec;
David Howellsd84f4f92008-11-14 10:39:23 +11006272 struct cred *new;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006273 u32 mysid = current_sid(), sid = 0, ptsid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006274 int error;
6275 char *str = value;
6276
Linus Torvalds1da177e2005-04-16 15:20:36 -07006277 /*
6278 * Basic control over ability to set these attributes at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006279 */
6280 if (!strcmp(name, "exec"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006281 error = avc_has_perm(&selinux_state,
6282 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006283 PROCESS__SETEXEC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006284 else if (!strcmp(name, "fscreate"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006285 error = avc_has_perm(&selinux_state,
6286 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006287 PROCESS__SETFSCREATE, NULL);
Michael LeMay4eb582c2006-06-26 00:24:57 -07006288 else if (!strcmp(name, "keycreate"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006289 error = avc_has_perm(&selinux_state,
6290 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006291 PROCESS__SETKEYCREATE, NULL);
Eric Paris42c3e032006-06-26 00:26:03 -07006292 else if (!strcmp(name, "sockcreate"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006293 error = avc_has_perm(&selinux_state,
6294 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006295 PROCESS__SETSOCKCREATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006296 else if (!strcmp(name, "current"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006297 error = avc_has_perm(&selinux_state,
6298 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006299 PROCESS__SETCURRENT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006300 else
6301 error = -EINVAL;
6302 if (error)
6303 return error;
6304
6305 /* Obtain a SID for the context, if one was specified. */
Stephen Smalleya050a572017-01-31 11:54:04 -05006306 if (size && str[0] && str[0] != '\n') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006307 if (str[size-1] == '\n') {
6308 str[size-1] = 0;
6309 size--;
6310 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006311 error = security_context_to_sid(&selinux_state, value, size,
6312 &sid, GFP_KERNEL);
Stephen Smalley12b29f32008-05-07 13:03:20 -04006313 if (error == -EINVAL && !strcmp(name, "fscreate")) {
Stephen Smalleydb590002017-04-20 11:31:30 -04006314 if (!has_cap_mac_admin(true)) {
Eric Parisd6ea83e2012-04-04 13:45:49 -04006315 struct audit_buffer *ab;
6316 size_t audit_size;
6317
6318 /* We strip a nul only if it is at the end, otherwise the
6319 * context contains a nul and we should audit that */
6320 if (str[size - 1] == '\0')
6321 audit_size = size - 1;
6322 else
6323 audit_size = size;
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04006324 ab = audit_log_start(audit_context(),
6325 GFP_ATOMIC,
6326 AUDIT_SELINUX_ERR);
Eric Parisd6ea83e2012-04-04 13:45:49 -04006327 audit_log_format(ab, "op=fscreate invalid_context=");
6328 audit_log_n_untrustedstring(ab, value, audit_size);
6329 audit_log_end(ab);
6330
Stephen Smalley12b29f32008-05-07 13:03:20 -04006331 return error;
Eric Parisd6ea83e2012-04-04 13:45:49 -04006332 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006333 error = security_context_to_sid_force(
6334 &selinux_state,
6335 value, size, &sid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04006336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006337 if (error)
6338 return error;
6339 }
6340
David Howellsd84f4f92008-11-14 10:39:23 +11006341 new = prepare_creds();
6342 if (!new)
6343 return -ENOMEM;
6344
Linus Torvalds1da177e2005-04-16 15:20:36 -07006345 /* Permission checking based on the specified context is
6346 performed during the actual operation (execve,
6347 open/mkdir/...), when we know the full context of the
David Howellsd84f4f92008-11-14 10:39:23 +11006348 operation. See selinux_bprm_set_creds for the execve
Linus Torvalds1da177e2005-04-16 15:20:36 -07006349 checks and may_create for the file creation checks. The
6350 operation will then fail if the context is not permitted. */
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07006351 tsec = selinux_cred(new);
David Howellsd84f4f92008-11-14 10:39:23 +11006352 if (!strcmp(name, "exec")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006353 tsec->exec_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006354 } else if (!strcmp(name, "fscreate")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006355 tsec->create_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006356 } else if (!strcmp(name, "keycreate")) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006357 error = avc_has_perm(&selinux_state,
6358 mysid, sid, SECCLASS_KEY, KEY__CREATE,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006359 NULL);
Michael LeMay4eb582c2006-06-26 00:24:57 -07006360 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11006361 goto abort_change;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006362 tsec->keycreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006363 } else if (!strcmp(name, "sockcreate")) {
Eric Paris42c3e032006-06-26 00:26:03 -07006364 tsec->sockcreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006365 } else if (!strcmp(name, "current")) {
6366 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006367 if (sid == 0)
David Howellsd84f4f92008-11-14 10:39:23 +11006368 goto abort_change;
KaiGai Koheid9250de2008-08-28 16:35:57 +09006369
David Howellsd84f4f92008-11-14 10:39:23 +11006370 /* Only allow single threaded processes to change context */
6371 error = -EPERM;
Oleg Nesterov5bb459b2009-07-10 03:48:23 +02006372 if (!current_is_single_threaded()) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006373 error = security_bounded_transition(&selinux_state,
6374 tsec->sid, sid);
David Howellsd84f4f92008-11-14 10:39:23 +11006375 if (error)
6376 goto abort_change;
Eric Paris828dfe12008-04-17 13:17:49 -04006377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006378
6379 /* Check permissions for the transition. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006380 error = avc_has_perm(&selinux_state,
6381 tsec->sid, sid, SECCLASS_PROCESS,
Eric Paris828dfe12008-04-17 13:17:49 -04006382 PROCESS__DYNTRANSITION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006383 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11006384 goto abort_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006385
6386 /* Check for ptracing, and update the task SID if ok.
6387 Otherwise, leave SID unchanged and fail. */
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006388 ptsid = ptrace_parent_sid();
Paul Moore0c6181c2016-03-30 21:41:21 -04006389 if (ptsid != 0) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006390 error = avc_has_perm(&selinux_state,
6391 ptsid, sid, SECCLASS_PROCESS,
David Howellsd84f4f92008-11-14 10:39:23 +11006392 PROCESS__PTRACE, NULL);
6393 if (error)
6394 goto abort_change;
6395 }
6396
6397 tsec->sid = sid;
6398 } else {
6399 error = -EINVAL;
6400 goto abort_change;
6401 }
6402
6403 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006404 return size;
David Howellsd84f4f92008-11-14 10:39:23 +11006405
6406abort_change:
6407 abort_creds(new);
6408 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006409}
6410
David Quigley746df9b2013-05-22 12:50:35 -04006411static int selinux_ismaclabel(const char *name)
6412{
6413 return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0);
6414}
6415
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006416static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
6417{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006418 return security_sid_to_context(&selinux_state, secid,
6419 secdata, seclen);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006420}
6421
David Howells7bf570d2008-04-29 20:52:51 +01006422static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
David Howells63cb3442008-01-15 23:47:35 +00006423{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006424 return security_context_to_sid(&selinux_state, secdata, seclen,
6425 secid, GFP_KERNEL);
David Howells63cb3442008-01-15 23:47:35 +00006426}
6427
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006428static void selinux_release_secctx(char *secdata, u32 seclen)
6429{
Paul Moore088999e2007-08-01 11:12:58 -04006430 kfree(secdata);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006431}
6432
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006433static void selinux_inode_invalidate_secctx(struct inode *inode)
6434{
Casey Schaufler80788c22018-09-21 17:19:11 -07006435 struct inode_security_struct *isec = selinux_inode(inode);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006436
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01006437 spin_lock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006438 isec->initialized = LABEL_INVALID;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01006439 spin_unlock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006440}
6441
David P. Quigley1ee65e32009-09-03 14:25:57 -04006442/*
6443 * called with inode->i_mutex locked
6444 */
6445static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
6446{
Ondrej Mosnacek53e0c2a2018-12-21 21:18:53 +01006447 int rc = selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX,
6448 ctx, ctxlen, 0);
6449 /* Do not return error when suppressing label (SBLABEL_MNT not set). */
6450 return rc == -EOPNOTSUPP ? 0 : rc;
David P. Quigley1ee65e32009-09-03 14:25:57 -04006451}
6452
6453/*
6454 * called with inode->i_mutex locked
6455 */
6456static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
6457{
6458 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0);
6459}
6460
6461static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
6462{
6463 int len = 0;
6464 len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX,
6465 ctx, true);
6466 if (len < 0)
6467 return len;
6468 *ctxlen = len;
6469 return 0;
6470}
Michael LeMayd7200242006-06-22 14:47:17 -07006471#ifdef CONFIG_KEYS
6472
David Howellsd84f4f92008-11-14 10:39:23 +11006473static int selinux_key_alloc(struct key *k, const struct cred *cred,
David Howells7e047ef2006-06-26 00:24:50 -07006474 unsigned long flags)
Michael LeMayd7200242006-06-22 14:47:17 -07006475{
David Howellsd84f4f92008-11-14 10:39:23 +11006476 const struct task_security_struct *tsec;
Michael LeMayd7200242006-06-22 14:47:17 -07006477 struct key_security_struct *ksec;
6478
6479 ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
6480 if (!ksec)
6481 return -ENOMEM;
6482
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07006483 tsec = selinux_cred(cred);
David Howellsd84f4f92008-11-14 10:39:23 +11006484 if (tsec->keycreate_sid)
6485 ksec->sid = tsec->keycreate_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006486 else
David Howellsd84f4f92008-11-14 10:39:23 +11006487 ksec->sid = tsec->sid;
Michael LeMayd7200242006-06-22 14:47:17 -07006488
David Howells275bb412008-11-14 10:39:19 +11006489 k->security = ksec;
Michael LeMayd7200242006-06-22 14:47:17 -07006490 return 0;
6491}
6492
6493static void selinux_key_free(struct key *k)
6494{
6495 struct key_security_struct *ksec = k->security;
6496
6497 k->security = NULL;
6498 kfree(ksec);
6499}
6500
6501static int selinux_key_permission(key_ref_t key_ref,
David Howellsd84f4f92008-11-14 10:39:23 +11006502 const struct cred *cred,
David Howellsf5895942014-03-14 17:44:49 +00006503 unsigned perm)
Michael LeMayd7200242006-06-22 14:47:17 -07006504{
6505 struct key *key;
Michael LeMayd7200242006-06-22 14:47:17 -07006506 struct key_security_struct *ksec;
David Howells275bb412008-11-14 10:39:19 +11006507 u32 sid;
Michael LeMayd7200242006-06-22 14:47:17 -07006508
6509 /* if no specific permissions are requested, we skip the
6510 permission check. No serious, additional covert channels
6511 appear to be created. */
6512 if (perm == 0)
6513 return 0;
6514
David Howellsd84f4f92008-11-14 10:39:23 +11006515 sid = cred_sid(cred);
David Howells275bb412008-11-14 10:39:19 +11006516
6517 key = key_ref_to_ptr(key_ref);
6518 ksec = key->security;
6519
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006520 return avc_has_perm(&selinux_state,
6521 sid, ksec->sid, SECCLASS_KEY, perm, NULL);
Michael LeMayd7200242006-06-22 14:47:17 -07006522}
6523
David Howells70a5bb72008-04-29 01:01:26 -07006524static int selinux_key_getsecurity(struct key *key, char **_buffer)
6525{
6526 struct key_security_struct *ksec = key->security;
6527 char *context = NULL;
6528 unsigned len;
6529 int rc;
6530
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006531 rc = security_sid_to_context(&selinux_state, ksec->sid,
6532 &context, &len);
David Howells70a5bb72008-04-29 01:01:26 -07006533 if (!rc)
6534 rc = len;
6535 *_buffer = context;
6536 return rc;
6537}
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006538#endif
David Howells70a5bb72008-04-29 01:01:26 -07006539
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006540#ifdef CONFIG_SECURITY_INFINIBAND
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006541static int selinux_ib_pkey_access(void *ib_sec, u64 subnet_prefix, u16 pkey_val)
6542{
6543 struct common_audit_data ad;
6544 int err;
6545 u32 sid = 0;
6546 struct ib_security_struct *sec = ib_sec;
6547 struct lsm_ibpkey_audit ibpkey;
6548
Daniel Jurgens409dcf32017-05-19 15:48:59 +03006549 err = sel_ib_pkey_sid(subnet_prefix, pkey_val, &sid);
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006550 if (err)
6551 return err;
6552
6553 ad.type = LSM_AUDIT_DATA_IBPKEY;
6554 ibpkey.subnet_prefix = subnet_prefix;
6555 ibpkey.pkey = pkey_val;
6556 ad.u.ibpkey = &ibpkey;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006557 return avc_has_perm(&selinux_state,
6558 sec->sid, sid,
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006559 SECCLASS_INFINIBAND_PKEY,
6560 INFINIBAND_PKEY__ACCESS, &ad);
6561}
6562
Daniel Jurgensab861df2017-05-19 15:48:58 +03006563static int selinux_ib_endport_manage_subnet(void *ib_sec, const char *dev_name,
6564 u8 port_num)
6565{
6566 struct common_audit_data ad;
6567 int err;
6568 u32 sid = 0;
6569 struct ib_security_struct *sec = ib_sec;
6570 struct lsm_ibendport_audit ibendport;
6571
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006572 err = security_ib_endport_sid(&selinux_state, dev_name, port_num,
6573 &sid);
Daniel Jurgensab861df2017-05-19 15:48:58 +03006574
6575 if (err)
6576 return err;
6577
6578 ad.type = LSM_AUDIT_DATA_IBENDPORT;
6579 strncpy(ibendport.dev_name, dev_name, sizeof(ibendport.dev_name));
6580 ibendport.port = port_num;
6581 ad.u.ibendport = &ibendport;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006582 return avc_has_perm(&selinux_state,
6583 sec->sid, sid,
Daniel Jurgensab861df2017-05-19 15:48:58 +03006584 SECCLASS_INFINIBAND_ENDPORT,
6585 INFINIBAND_ENDPORT__MANAGE_SUBNET, &ad);
6586}
6587
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006588static int selinux_ib_alloc_security(void **ib_sec)
6589{
6590 struct ib_security_struct *sec;
6591
6592 sec = kzalloc(sizeof(*sec), GFP_KERNEL);
6593 if (!sec)
6594 return -ENOMEM;
6595 sec->sid = current_sid();
6596
6597 *ib_sec = sec;
6598 return 0;
6599}
6600
6601static void selinux_ib_free_security(void *ib_sec)
6602{
6603 kfree(ib_sec);
6604}
Michael LeMayd7200242006-06-22 14:47:17 -07006605#endif
6606
Chenbo Fengec27c352017-10-18 13:00:25 -07006607#ifdef CONFIG_BPF_SYSCALL
6608static int selinux_bpf(int cmd, union bpf_attr *attr,
6609 unsigned int size)
6610{
6611 u32 sid = current_sid();
6612 int ret;
6613
6614 switch (cmd) {
6615 case BPF_MAP_CREATE:
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006616 ret = avc_has_perm(&selinux_state,
6617 sid, sid, SECCLASS_BPF, BPF__MAP_CREATE,
Chenbo Fengec27c352017-10-18 13:00:25 -07006618 NULL);
6619 break;
6620 case BPF_PROG_LOAD:
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006621 ret = avc_has_perm(&selinux_state,
6622 sid, sid, SECCLASS_BPF, BPF__PROG_LOAD,
Chenbo Fengec27c352017-10-18 13:00:25 -07006623 NULL);
6624 break;
6625 default:
6626 ret = 0;
6627 break;
6628 }
6629
6630 return ret;
6631}
6632
6633static u32 bpf_map_fmode_to_av(fmode_t fmode)
6634{
6635 u32 av = 0;
6636
6637 if (fmode & FMODE_READ)
6638 av |= BPF__MAP_READ;
6639 if (fmode & FMODE_WRITE)
6640 av |= BPF__MAP_WRITE;
6641 return av;
6642}
6643
Chenbo Fengf66e4482017-10-18 13:00:26 -07006644/* This function will check the file pass through unix socket or binder to see
6645 * if it is a bpf related object. And apply correspinding checks on the bpf
6646 * object based on the type. The bpf maps and programs, not like other files and
6647 * socket, are using a shared anonymous inode inside the kernel as their inode.
6648 * So checking that inode cannot identify if the process have privilege to
6649 * access the bpf object and that's why we have to add this additional check in
6650 * selinux_file_receive and selinux_binder_transfer_files.
6651 */
6652static int bpf_fd_pass(struct file *file, u32 sid)
6653{
6654 struct bpf_security_struct *bpfsec;
6655 struct bpf_prog *prog;
6656 struct bpf_map *map;
6657 int ret;
6658
6659 if (file->f_op == &bpf_map_fops) {
6660 map = file->private_data;
6661 bpfsec = map->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006662 ret = avc_has_perm(&selinux_state,
6663 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengf66e4482017-10-18 13:00:26 -07006664 bpf_map_fmode_to_av(file->f_mode), NULL);
6665 if (ret)
6666 return ret;
6667 } else if (file->f_op == &bpf_prog_fops) {
6668 prog = file->private_data;
6669 bpfsec = prog->aux->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006670 ret = avc_has_perm(&selinux_state,
6671 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengf66e4482017-10-18 13:00:26 -07006672 BPF__PROG_RUN, NULL);
6673 if (ret)
6674 return ret;
6675 }
6676 return 0;
6677}
6678
Chenbo Fengec27c352017-10-18 13:00:25 -07006679static int selinux_bpf_map(struct bpf_map *map, fmode_t fmode)
6680{
6681 u32 sid = current_sid();
6682 struct bpf_security_struct *bpfsec;
6683
6684 bpfsec = map->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006685 return avc_has_perm(&selinux_state,
6686 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengec27c352017-10-18 13:00:25 -07006687 bpf_map_fmode_to_av(fmode), NULL);
6688}
6689
6690static int selinux_bpf_prog(struct bpf_prog *prog)
6691{
6692 u32 sid = current_sid();
6693 struct bpf_security_struct *bpfsec;
6694
6695 bpfsec = prog->aux->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006696 return avc_has_perm(&selinux_state,
6697 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengec27c352017-10-18 13:00:25 -07006698 BPF__PROG_RUN, NULL);
6699}
6700
6701static int selinux_bpf_map_alloc(struct bpf_map *map)
6702{
6703 struct bpf_security_struct *bpfsec;
6704
6705 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6706 if (!bpfsec)
6707 return -ENOMEM;
6708
6709 bpfsec->sid = current_sid();
6710 map->security = bpfsec;
6711
6712 return 0;
6713}
6714
6715static void selinux_bpf_map_free(struct bpf_map *map)
6716{
6717 struct bpf_security_struct *bpfsec = map->security;
6718
6719 map->security = NULL;
6720 kfree(bpfsec);
6721}
6722
6723static int selinux_bpf_prog_alloc(struct bpf_prog_aux *aux)
6724{
6725 struct bpf_security_struct *bpfsec;
6726
6727 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6728 if (!bpfsec)
6729 return -ENOMEM;
6730
6731 bpfsec->sid = current_sid();
6732 aux->security = bpfsec;
6733
6734 return 0;
6735}
6736
6737static void selinux_bpf_prog_free(struct bpf_prog_aux *aux)
6738{
6739 struct bpf_security_struct *bpfsec = aux->security;
6740
6741 aux->security = NULL;
6742 kfree(bpfsec);
6743}
6744#endif
6745
Casey Schauflerbbd36622018-11-12 09:30:56 -08006746struct lsm_blob_sizes selinux_blob_sizes __lsm_ro_after_init = {
6747 .lbs_cred = sizeof(struct task_security_struct),
Casey Schaufler33bf60c2018-11-12 12:02:49 -08006748 .lbs_file = sizeof(struct file_security_struct),
Casey Schauflerafb1cbe32018-09-21 17:19:29 -07006749 .lbs_inode = sizeof(struct inode_security_struct),
Casey Schauflerecd5f822018-11-20 11:55:02 -08006750 .lbs_ipc = sizeof(struct ipc_security_struct),
6751 .lbs_msg_msg = sizeof(struct msg_security_struct),
Casey Schauflerbbd36622018-11-12 09:30:56 -08006752};
6753
James Morrisca97d932017-02-15 00:18:51 +11006754static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {
Casey Schauflere20b0432015-05-02 15:11:36 -07006755 LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr),
6756 LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction),
6757 LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder),
6758 LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file),
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02006759
Casey Schauflere20b0432015-05-02 15:11:36 -07006760 LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check),
6761 LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme),
6762 LSM_HOOK_INIT(capget, selinux_capget),
6763 LSM_HOOK_INIT(capset, selinux_capset),
6764 LSM_HOOK_INIT(capable, selinux_capable),
6765 LSM_HOOK_INIT(quotactl, selinux_quotactl),
6766 LSM_HOOK_INIT(quota_on, selinux_quota_on),
6767 LSM_HOOK_INIT(syslog, selinux_syslog),
6768 LSM_HOOK_INIT(vm_enough_memory, selinux_vm_enough_memory),
Stephen Smalley79af7302015-01-21 10:54:10 -05006769
Casey Schauflere20b0432015-05-02 15:11:36 -07006770 LSM_HOOK_INIT(netlink_send, selinux_netlink_send),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006771
Casey Schauflere20b0432015-05-02 15:11:36 -07006772 LSM_HOOK_INIT(bprm_set_creds, selinux_bprm_set_creds),
6773 LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds),
6774 LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006775
Al Viro0b520752018-12-23 16:02:47 -05006776 LSM_HOOK_INIT(fs_context_dup, selinux_fs_context_dup),
David Howells442155c2018-11-01 23:07:24 +00006777 LSM_HOOK_INIT(fs_context_parse_param, selinux_fs_context_parse_param),
6778
Casey Schauflere20b0432015-05-02 15:11:36 -07006779 LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security),
6780 LSM_HOOK_INIT(sb_free_security, selinux_sb_free_security),
Al Viro5b400232018-12-12 20:13:29 -05006781 LSM_HOOK_INIT(sb_eat_lsm_opts, selinux_sb_eat_lsm_opts),
Al Viro204cc0c2018-12-13 13:41:47 -05006782 LSM_HOOK_INIT(sb_free_mnt_opts, selinux_free_mnt_opts),
Casey Schauflere20b0432015-05-02 15:11:36 -07006783 LSM_HOOK_INIT(sb_remount, selinux_sb_remount),
6784 LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount),
6785 LSM_HOOK_INIT(sb_show_options, selinux_sb_show_options),
6786 LSM_HOOK_INIT(sb_statfs, selinux_sb_statfs),
6787 LSM_HOOK_INIT(sb_mount, selinux_mount),
6788 LSM_HOOK_INIT(sb_umount, selinux_umount),
6789 LSM_HOOK_INIT(sb_set_mnt_opts, selinux_set_mnt_opts),
6790 LSM_HOOK_INIT(sb_clone_mnt_opts, selinux_sb_clone_mnt_opts),
Al Viro757cbe52018-12-14 23:42:21 -05006791 LSM_HOOK_INIT(sb_add_mnt_opt, selinux_add_mnt_opt),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006792
Casey Schauflere20b0432015-05-02 15:11:36 -07006793 LSM_HOOK_INIT(dentry_init_security, selinux_dentry_init_security),
Vivek Goyala518b0a2016-07-13 10:44:53 -04006794 LSM_HOOK_INIT(dentry_create_files_as, selinux_dentry_create_files_as),
Eric Parise0007522008-03-05 10:31:54 -05006795
Casey Schauflere20b0432015-05-02 15:11:36 -07006796 LSM_HOOK_INIT(inode_alloc_security, selinux_inode_alloc_security),
6797 LSM_HOOK_INIT(inode_free_security, selinux_inode_free_security),
6798 LSM_HOOK_INIT(inode_init_security, selinux_inode_init_security),
6799 LSM_HOOK_INIT(inode_create, selinux_inode_create),
6800 LSM_HOOK_INIT(inode_link, selinux_inode_link),
6801 LSM_HOOK_INIT(inode_unlink, selinux_inode_unlink),
6802 LSM_HOOK_INIT(inode_symlink, selinux_inode_symlink),
6803 LSM_HOOK_INIT(inode_mkdir, selinux_inode_mkdir),
6804 LSM_HOOK_INIT(inode_rmdir, selinux_inode_rmdir),
6805 LSM_HOOK_INIT(inode_mknod, selinux_inode_mknod),
6806 LSM_HOOK_INIT(inode_rename, selinux_inode_rename),
6807 LSM_HOOK_INIT(inode_readlink, selinux_inode_readlink),
6808 LSM_HOOK_INIT(inode_follow_link, selinux_inode_follow_link),
6809 LSM_HOOK_INIT(inode_permission, selinux_inode_permission),
6810 LSM_HOOK_INIT(inode_setattr, selinux_inode_setattr),
6811 LSM_HOOK_INIT(inode_getattr, selinux_inode_getattr),
6812 LSM_HOOK_INIT(inode_setxattr, selinux_inode_setxattr),
6813 LSM_HOOK_INIT(inode_post_setxattr, selinux_inode_post_setxattr),
6814 LSM_HOOK_INIT(inode_getxattr, selinux_inode_getxattr),
6815 LSM_HOOK_INIT(inode_listxattr, selinux_inode_listxattr),
6816 LSM_HOOK_INIT(inode_removexattr, selinux_inode_removexattr),
6817 LSM_HOOK_INIT(inode_getsecurity, selinux_inode_getsecurity),
6818 LSM_HOOK_INIT(inode_setsecurity, selinux_inode_setsecurity),
6819 LSM_HOOK_INIT(inode_listsecurity, selinux_inode_listsecurity),
6820 LSM_HOOK_INIT(inode_getsecid, selinux_inode_getsecid),
Vivek Goyal56909eb2016-07-13 10:44:48 -04006821 LSM_HOOK_INIT(inode_copy_up, selinux_inode_copy_up),
Vivek Goyal19472b62016-07-13 10:44:50 -04006822 LSM_HOOK_INIT(inode_copy_up_xattr, selinux_inode_copy_up_xattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006823
Ondrej Mosnacekec882da2019-02-22 15:57:17 +01006824 LSM_HOOK_INIT(kernfs_init_security, selinux_kernfs_init_security),
6825
Casey Schauflere20b0432015-05-02 15:11:36 -07006826 LSM_HOOK_INIT(file_permission, selinux_file_permission),
6827 LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07006828 LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl),
6829 LSM_HOOK_INIT(mmap_file, selinux_mmap_file),
6830 LSM_HOOK_INIT(mmap_addr, selinux_mmap_addr),
6831 LSM_HOOK_INIT(file_mprotect, selinux_file_mprotect),
6832 LSM_HOOK_INIT(file_lock, selinux_file_lock),
6833 LSM_HOOK_INIT(file_fcntl, selinux_file_fcntl),
6834 LSM_HOOK_INIT(file_set_fowner, selinux_file_set_fowner),
6835 LSM_HOOK_INIT(file_send_sigiotask, selinux_file_send_sigiotask),
6836 LSM_HOOK_INIT(file_receive, selinux_file_receive),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006837
Casey Schauflere20b0432015-05-02 15:11:36 -07006838 LSM_HOOK_INIT(file_open, selinux_file_open),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006839
Tetsuo Handaa79be232017-03-28 23:08:45 +09006840 LSM_HOOK_INIT(task_alloc, selinux_task_alloc),
Casey Schauflere20b0432015-05-02 15:11:36 -07006841 LSM_HOOK_INIT(cred_prepare, selinux_cred_prepare),
6842 LSM_HOOK_INIT(cred_transfer, selinux_cred_transfer),
Matthew Garrett3ec30112018-01-08 13:36:19 -08006843 LSM_HOOK_INIT(cred_getsecid, selinux_cred_getsecid),
Casey Schauflere20b0432015-05-02 15:11:36 -07006844 LSM_HOOK_INIT(kernel_act_as, selinux_kernel_act_as),
6845 LSM_HOOK_INIT(kernel_create_files_as, selinux_kernel_create_files_as),
6846 LSM_HOOK_INIT(kernel_module_request, selinux_kernel_module_request),
Mimi Zoharc77b8cd2018-07-13 14:06:02 -04006847 LSM_HOOK_INIT(kernel_load_data, selinux_kernel_load_data),
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07006848 LSM_HOOK_INIT(kernel_read_file, selinux_kernel_read_file),
Casey Schauflere20b0432015-05-02 15:11:36 -07006849 LSM_HOOK_INIT(task_setpgid, selinux_task_setpgid),
6850 LSM_HOOK_INIT(task_getpgid, selinux_task_getpgid),
6851 LSM_HOOK_INIT(task_getsid, selinux_task_getsid),
6852 LSM_HOOK_INIT(task_getsecid, selinux_task_getsecid),
6853 LSM_HOOK_INIT(task_setnice, selinux_task_setnice),
6854 LSM_HOOK_INIT(task_setioprio, selinux_task_setioprio),
6855 LSM_HOOK_INIT(task_getioprio, selinux_task_getioprio),
Stephen Smalley791ec492017-02-17 07:57:00 -05006856 LSM_HOOK_INIT(task_prlimit, selinux_task_prlimit),
Casey Schauflere20b0432015-05-02 15:11:36 -07006857 LSM_HOOK_INIT(task_setrlimit, selinux_task_setrlimit),
6858 LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler),
6859 LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler),
6860 LSM_HOOK_INIT(task_movememory, selinux_task_movememory),
6861 LSM_HOOK_INIT(task_kill, selinux_task_kill),
Casey Schauflere20b0432015-05-02 15:11:36 -07006862 LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode),
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09006863
Casey Schauflere20b0432015-05-02 15:11:36 -07006864 LSM_HOOK_INIT(ipc_permission, selinux_ipc_permission),
6865 LSM_HOOK_INIT(ipc_getsecid, selinux_ipc_getsecid),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006866
Casey Schauflere20b0432015-05-02 15:11:36 -07006867 LSM_HOOK_INIT(msg_msg_alloc_security, selinux_msg_msg_alloc_security),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006868
Casey Schauflere20b0432015-05-02 15:11:36 -07006869 LSM_HOOK_INIT(msg_queue_alloc_security,
6870 selinux_msg_queue_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07006871 LSM_HOOK_INIT(msg_queue_associate, selinux_msg_queue_associate),
6872 LSM_HOOK_INIT(msg_queue_msgctl, selinux_msg_queue_msgctl),
6873 LSM_HOOK_INIT(msg_queue_msgsnd, selinux_msg_queue_msgsnd),
6874 LSM_HOOK_INIT(msg_queue_msgrcv, selinux_msg_queue_msgrcv),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006875
Casey Schauflere20b0432015-05-02 15:11:36 -07006876 LSM_HOOK_INIT(shm_alloc_security, selinux_shm_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07006877 LSM_HOOK_INIT(shm_associate, selinux_shm_associate),
6878 LSM_HOOK_INIT(shm_shmctl, selinux_shm_shmctl),
6879 LSM_HOOK_INIT(shm_shmat, selinux_shm_shmat),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006880
Casey Schauflere20b0432015-05-02 15:11:36 -07006881 LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07006882 LSM_HOOK_INIT(sem_associate, selinux_sem_associate),
6883 LSM_HOOK_INIT(sem_semctl, selinux_sem_semctl),
6884 LSM_HOOK_INIT(sem_semop, selinux_sem_semop),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006885
Casey Schauflere20b0432015-05-02 15:11:36 -07006886 LSM_HOOK_INIT(d_instantiate, selinux_d_instantiate),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006887
Casey Schauflere20b0432015-05-02 15:11:36 -07006888 LSM_HOOK_INIT(getprocattr, selinux_getprocattr),
6889 LSM_HOOK_INIT(setprocattr, selinux_setprocattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006890
Casey Schauflere20b0432015-05-02 15:11:36 -07006891 LSM_HOOK_INIT(ismaclabel, selinux_ismaclabel),
6892 LSM_HOOK_INIT(secid_to_secctx, selinux_secid_to_secctx),
6893 LSM_HOOK_INIT(secctx_to_secid, selinux_secctx_to_secid),
6894 LSM_HOOK_INIT(release_secctx, selinux_release_secctx),
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006895 LSM_HOOK_INIT(inode_invalidate_secctx, selinux_inode_invalidate_secctx),
Casey Schauflere20b0432015-05-02 15:11:36 -07006896 LSM_HOOK_INIT(inode_notifysecctx, selinux_inode_notifysecctx),
6897 LSM_HOOK_INIT(inode_setsecctx, selinux_inode_setsecctx),
6898 LSM_HOOK_INIT(inode_getsecctx, selinux_inode_getsecctx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006899
Casey Schauflere20b0432015-05-02 15:11:36 -07006900 LSM_HOOK_INIT(unix_stream_connect, selinux_socket_unix_stream_connect),
6901 LSM_HOOK_INIT(unix_may_send, selinux_socket_unix_may_send),
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006902
Casey Schauflere20b0432015-05-02 15:11:36 -07006903 LSM_HOOK_INIT(socket_create, selinux_socket_create),
6904 LSM_HOOK_INIT(socket_post_create, selinux_socket_post_create),
David Herrmann0b811db2018-05-04 16:28:21 +02006905 LSM_HOOK_INIT(socket_socketpair, selinux_socket_socketpair),
Casey Schauflere20b0432015-05-02 15:11:36 -07006906 LSM_HOOK_INIT(socket_bind, selinux_socket_bind),
6907 LSM_HOOK_INIT(socket_connect, selinux_socket_connect),
6908 LSM_HOOK_INIT(socket_listen, selinux_socket_listen),
6909 LSM_HOOK_INIT(socket_accept, selinux_socket_accept),
6910 LSM_HOOK_INIT(socket_sendmsg, selinux_socket_sendmsg),
6911 LSM_HOOK_INIT(socket_recvmsg, selinux_socket_recvmsg),
6912 LSM_HOOK_INIT(socket_getsockname, selinux_socket_getsockname),
6913 LSM_HOOK_INIT(socket_getpeername, selinux_socket_getpeername),
6914 LSM_HOOK_INIT(socket_getsockopt, selinux_socket_getsockopt),
6915 LSM_HOOK_INIT(socket_setsockopt, selinux_socket_setsockopt),
6916 LSM_HOOK_INIT(socket_shutdown, selinux_socket_shutdown),
6917 LSM_HOOK_INIT(socket_sock_rcv_skb, selinux_socket_sock_rcv_skb),
6918 LSM_HOOK_INIT(socket_getpeersec_stream,
6919 selinux_socket_getpeersec_stream),
6920 LSM_HOOK_INIT(socket_getpeersec_dgram, selinux_socket_getpeersec_dgram),
6921 LSM_HOOK_INIT(sk_alloc_security, selinux_sk_alloc_security),
6922 LSM_HOOK_INIT(sk_free_security, selinux_sk_free_security),
6923 LSM_HOOK_INIT(sk_clone_security, selinux_sk_clone_security),
6924 LSM_HOOK_INIT(sk_getsecid, selinux_sk_getsecid),
6925 LSM_HOOK_INIT(sock_graft, selinux_sock_graft),
Richard Hainesd4529302018-02-13 20:57:18 +00006926 LSM_HOOK_INIT(sctp_assoc_request, selinux_sctp_assoc_request),
6927 LSM_HOOK_INIT(sctp_sk_clone, selinux_sctp_sk_clone),
6928 LSM_HOOK_INIT(sctp_bind_connect, selinux_sctp_bind_connect),
Casey Schauflere20b0432015-05-02 15:11:36 -07006929 LSM_HOOK_INIT(inet_conn_request, selinux_inet_conn_request),
6930 LSM_HOOK_INIT(inet_csk_clone, selinux_inet_csk_clone),
6931 LSM_HOOK_INIT(inet_conn_established, selinux_inet_conn_established),
6932 LSM_HOOK_INIT(secmark_relabel_packet, selinux_secmark_relabel_packet),
6933 LSM_HOOK_INIT(secmark_refcount_inc, selinux_secmark_refcount_inc),
6934 LSM_HOOK_INIT(secmark_refcount_dec, selinux_secmark_refcount_dec),
6935 LSM_HOOK_INIT(req_classify_flow, selinux_req_classify_flow),
6936 LSM_HOOK_INIT(tun_dev_alloc_security, selinux_tun_dev_alloc_security),
6937 LSM_HOOK_INIT(tun_dev_free_security, selinux_tun_dev_free_security),
6938 LSM_HOOK_INIT(tun_dev_create, selinux_tun_dev_create),
6939 LSM_HOOK_INIT(tun_dev_attach_queue, selinux_tun_dev_attach_queue),
6940 LSM_HOOK_INIT(tun_dev_attach, selinux_tun_dev_attach),
6941 LSM_HOOK_INIT(tun_dev_open, selinux_tun_dev_open),
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006942#ifdef CONFIG_SECURITY_INFINIBAND
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006943 LSM_HOOK_INIT(ib_pkey_access, selinux_ib_pkey_access),
Daniel Jurgensab861df2017-05-19 15:48:58 +03006944 LSM_HOOK_INIT(ib_endport_manage_subnet,
6945 selinux_ib_endport_manage_subnet),
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006946 LSM_HOOK_INIT(ib_alloc_security, selinux_ib_alloc_security),
6947 LSM_HOOK_INIT(ib_free_security, selinux_ib_free_security),
6948#endif
Trent Jaegerd28d1e02005-12-13 23:12:40 -08006949#ifdef CONFIG_SECURITY_NETWORK_XFRM
Casey Schauflere20b0432015-05-02 15:11:36 -07006950 LSM_HOOK_INIT(xfrm_policy_alloc_security, selinux_xfrm_policy_alloc),
6951 LSM_HOOK_INIT(xfrm_policy_clone_security, selinux_xfrm_policy_clone),
6952 LSM_HOOK_INIT(xfrm_policy_free_security, selinux_xfrm_policy_free),
6953 LSM_HOOK_INIT(xfrm_policy_delete_security, selinux_xfrm_policy_delete),
6954 LSM_HOOK_INIT(xfrm_state_alloc, selinux_xfrm_state_alloc),
6955 LSM_HOOK_INIT(xfrm_state_alloc_acquire,
6956 selinux_xfrm_state_alloc_acquire),
6957 LSM_HOOK_INIT(xfrm_state_free_security, selinux_xfrm_state_free),
6958 LSM_HOOK_INIT(xfrm_state_delete_security, selinux_xfrm_state_delete),
6959 LSM_HOOK_INIT(xfrm_policy_lookup, selinux_xfrm_policy_lookup),
6960 LSM_HOOK_INIT(xfrm_state_pol_flow_match,
6961 selinux_xfrm_state_pol_flow_match),
6962 LSM_HOOK_INIT(xfrm_decode_session, selinux_xfrm_decode_session),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006963#endif
Michael LeMayd7200242006-06-22 14:47:17 -07006964
6965#ifdef CONFIG_KEYS
Casey Schauflere20b0432015-05-02 15:11:36 -07006966 LSM_HOOK_INIT(key_alloc, selinux_key_alloc),
6967 LSM_HOOK_INIT(key_free, selinux_key_free),
6968 LSM_HOOK_INIT(key_permission, selinux_key_permission),
6969 LSM_HOOK_INIT(key_getsecurity, selinux_key_getsecurity),
Michael LeMayd7200242006-06-22 14:47:17 -07006970#endif
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02006971
6972#ifdef CONFIG_AUDIT
Casey Schauflere20b0432015-05-02 15:11:36 -07006973 LSM_HOOK_INIT(audit_rule_init, selinux_audit_rule_init),
6974 LSM_HOOK_INIT(audit_rule_known, selinux_audit_rule_known),
6975 LSM_HOOK_INIT(audit_rule_match, selinux_audit_rule_match),
6976 LSM_HOOK_INIT(audit_rule_free, selinux_audit_rule_free),
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02006977#endif
Chenbo Fengec27c352017-10-18 13:00:25 -07006978
6979#ifdef CONFIG_BPF_SYSCALL
6980 LSM_HOOK_INIT(bpf, selinux_bpf),
6981 LSM_HOOK_INIT(bpf_map, selinux_bpf_map),
6982 LSM_HOOK_INIT(bpf_prog, selinux_bpf_prog),
6983 LSM_HOOK_INIT(bpf_map_alloc_security, selinux_bpf_map_alloc),
6984 LSM_HOOK_INIT(bpf_prog_alloc_security, selinux_bpf_prog_alloc),
6985 LSM_HOOK_INIT(bpf_map_free_security, selinux_bpf_map_free),
6986 LSM_HOOK_INIT(bpf_prog_free_security, selinux_bpf_prog_free),
6987#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006988};
6989
6990static __init int selinux_init(void)
6991{
peter enderborgc103a912018-06-12 10:09:03 +02006992 pr_info("SELinux: Initializing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006993
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006994 memset(&selinux_state, 0, sizeof(selinux_state));
Paul Mooree5a5ca92018-03-01 17:38:30 -05006995 enforcing_set(&selinux_state, selinux_enforcing_boot);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006996 selinux_state.checkreqprot = selinux_checkreqprot_boot;
6997 selinux_ss_init(&selinux_state.ss);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006998 selinux_avc_init(&selinux_state.avc);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006999
Linus Torvalds1da177e2005-04-16 15:20:36 -07007000 /* Set the security state for the initial task. */
David Howellsd84f4f92008-11-14 10:39:23 +11007001 cred_init_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007002
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04007003 default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
7004
Linus Torvalds1da177e2005-04-16 15:20:36 -07007005 avc_init();
7006
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007007 avtab_cache_init();
7008
7009 ebitmap_cache_init();
7010
7011 hashtab_cache_init();
7012
Casey Schauflerd69dece52017-01-18 17:09:05 -08007013 security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks), "selinux");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007014
Paul Moore615e51f2014-06-26 14:33:56 -04007015 if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET))
7016 panic("SELinux: Unable to register AVC netcache callback\n");
7017
Daniel Jurgens8f408ab2017-05-19 15:48:53 +03007018 if (avc_add_callback(selinux_lsm_notifier_avc_callback, AVC_CALLBACK_RESET))
7019 panic("SELinux: Unable to register AVC LSM notifier callback\n");
7020
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007021 if (selinux_enforcing_boot)
peter enderborgc103a912018-06-12 10:09:03 +02007022 pr_debug("SELinux: Starting in enforcing mode\n");
Eric Paris828dfe12008-04-17 13:17:49 -04007023 else
peter enderborgc103a912018-06-12 10:09:03 +02007024 pr_debug("SELinux: Starting in permissive mode\n");
Michael LeMayd7200242006-06-22 14:47:17 -07007025
David Howells442155c2018-11-01 23:07:24 +00007026 fs_validate_description(&selinux_fs_parameters);
7027
Linus Torvalds1da177e2005-04-16 15:20:36 -07007028 return 0;
7029}
7030
Al Viroe8c26252010-03-23 06:36:54 -04007031static void delayed_superblock_init(struct super_block *sb, void *unused)
7032{
Al Viro204cc0c2018-12-13 13:41:47 -05007033 selinux_set_mnt_opts(sb, NULL, 0, NULL);
Al Viroe8c26252010-03-23 06:36:54 -04007034}
7035
Linus Torvalds1da177e2005-04-16 15:20:36 -07007036void selinux_complete_init(void)
7037{
peter enderborgc103a912018-06-12 10:09:03 +02007038 pr_debug("SELinux: Completing initialization.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007039
7040 /* Set up any superblocks initialized prior to the policy load. */
peter enderborgc103a912018-06-12 10:09:03 +02007041 pr_debug("SELinux: Setting up existing superblocks.\n");
Al Viroe8c26252010-03-23 06:36:54 -04007042 iterate_supers(delayed_superblock_init, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007043}
7044
7045/* SELinux requires early initialization in order to label
7046 all processes and objects when they are created. */
Kees Cook3d6e5f62018-10-10 17:18:23 -07007047DEFINE_LSM(selinux) = {
Kees Cook07aed2f2018-10-10 17:18:24 -07007048 .name = "selinux",
Kees Cook14bd99c2018-09-19 19:57:06 -07007049 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
Kees Cookc5459b82018-09-13 22:28:48 -07007050 .enabled = &selinux_enabled,
Casey Schauflerbbd36622018-11-12 09:30:56 -08007051 .blobs = &selinux_blob_sizes,
Kees Cook3d6e5f62018-10-10 17:18:23 -07007052 .init = selinux_init,
7053};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007054
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007055#if defined(CONFIG_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007056
Florian Westphal591bb272017-07-26 11:40:52 +02007057static const struct nf_hook_ops selinux_nf_ops[] = {
Paul Mooreeffad8d2008-01-29 08:49:27 -05007058 {
7059 .hook = selinux_ipv4_postroute,
Alban Crequy2597a832012-05-14 03:56:39 +00007060 .pf = NFPROTO_IPV4,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007061 .hooknum = NF_INET_POST_ROUTING,
7062 .priority = NF_IP_PRI_SELINUX_LAST,
7063 },
7064 {
7065 .hook = selinux_ipv4_forward,
Alban Crequy2597a832012-05-14 03:56:39 +00007066 .pf = NFPROTO_IPV4,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007067 .hooknum = NF_INET_FORWARD,
7068 .priority = NF_IP_PRI_SELINUX_FIRST,
Paul Moore948bf852008-10-10 10:16:32 -04007069 },
7070 {
7071 .hook = selinux_ipv4_output,
Alban Crequy2597a832012-05-14 03:56:39 +00007072 .pf = NFPROTO_IPV4,
Paul Moore948bf852008-10-10 10:16:32 -04007073 .hooknum = NF_INET_LOCAL_OUT,
7074 .priority = NF_IP_PRI_SELINUX_FIRST,
Jiri Pirko25db6be2014-09-03 17:42:13 +02007075 },
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04007076#if IS_ENABLED(CONFIG_IPV6)
Paul Mooreeffad8d2008-01-29 08:49:27 -05007077 {
7078 .hook = selinux_ipv6_postroute,
Alban Crequy2597a832012-05-14 03:56:39 +00007079 .pf = NFPROTO_IPV6,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007080 .hooknum = NF_INET_POST_ROUTING,
7081 .priority = NF_IP6_PRI_SELINUX_LAST,
7082 },
7083 {
7084 .hook = selinux_ipv6_forward,
Alban Crequy2597a832012-05-14 03:56:39 +00007085 .pf = NFPROTO_IPV6,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007086 .hooknum = NF_INET_FORWARD,
7087 .priority = NF_IP6_PRI_SELINUX_FIRST,
Jiri Pirko25db6be2014-09-03 17:42:13 +02007088 },
Huw Davies2917f572016-06-27 15:06:15 -04007089 {
7090 .hook = selinux_ipv6_output,
7091 .pf = NFPROTO_IPV6,
7092 .hooknum = NF_INET_LOCAL_OUT,
7093 .priority = NF_IP6_PRI_SELINUX_FIRST,
7094 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07007095#endif /* IPV6 */
Jiri Pirko25db6be2014-09-03 17:42:13 +02007096};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007097
Florian Westphal8e71bf72017-04-21 11:49:09 +02007098static int __net_init selinux_nf_register(struct net *net)
7099{
7100 return nf_register_net_hooks(net, selinux_nf_ops,
7101 ARRAY_SIZE(selinux_nf_ops));
7102}
7103
7104static void __net_exit selinux_nf_unregister(struct net *net)
7105{
7106 nf_unregister_net_hooks(net, selinux_nf_ops,
7107 ARRAY_SIZE(selinux_nf_ops));
7108}
7109
7110static struct pernet_operations selinux_net_ops = {
7111 .init = selinux_nf_register,
7112 .exit = selinux_nf_unregister,
7113};
7114
Linus Torvalds1da177e2005-04-16 15:20:36 -07007115static int __init selinux_nf_ip_init(void)
7116{
Jiri Pirko25db6be2014-09-03 17:42:13 +02007117 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007118
7119 if (!selinux_enabled)
Jiri Pirko25db6be2014-09-03 17:42:13 +02007120 return 0;
Eric Parisfadcdb42007-02-22 18:11:31 -05007121
peter enderborgc103a912018-06-12 10:09:03 +02007122 pr_debug("SELinux: Registering netfilter hooks\n");
Eric Parisfadcdb42007-02-22 18:11:31 -05007123
Florian Westphal8e71bf72017-04-21 11:49:09 +02007124 err = register_pernet_subsys(&selinux_net_ops);
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07007125 if (err)
Florian Westphal8e71bf72017-04-21 11:49:09 +02007126 panic("SELinux: register_pernet_subsys: error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007127
Jiri Pirko25db6be2014-09-03 17:42:13 +02007128 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007129}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007130__initcall(selinux_nf_ip_init);
7131
7132#ifdef CONFIG_SECURITY_SELINUX_DISABLE
7133static void selinux_nf_ip_exit(void)
7134{
peter enderborgc103a912018-06-12 10:09:03 +02007135 pr_debug("SELinux: Unregistering netfilter hooks\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007136
Florian Westphal8e71bf72017-04-21 11:49:09 +02007137 unregister_pernet_subsys(&selinux_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007138}
7139#endif
7140
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007141#else /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007142
7143#ifdef CONFIG_SECURITY_SELINUX_DISABLE
7144#define selinux_nf_ip_exit()
7145#endif
7146
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007147#endif /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007148
7149#ifdef CONFIG_SECURITY_SELINUX_DISABLE
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007150int selinux_disable(struct selinux_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007151{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007152 if (state->initialized) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007153 /* Not permitted after initial policy load. */
7154 return -EINVAL;
7155 }
7156
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007157 if (state->disabled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007158 /* Only do this once. */
7159 return -EINVAL;
7160 }
7161
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007162 state->disabled = 1;
7163
peter enderborgc103a912018-06-12 10:09:03 +02007164 pr_info("SELinux: Disabled at runtime.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007165
Stephen Smalley30d55282006-05-03 10:52:36 -04007166 selinux_enabled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007167
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07007168 security_delete_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007169
Eric Parisaf8ff042009-09-20 21:23:01 -04007170 /* Try to destroy the avc node cache */
7171 avc_disable();
7172
Linus Torvalds1da177e2005-04-16 15:20:36 -07007173 /* Unregister netfilter hooks. */
7174 selinux_nf_ip_exit();
7175
7176 /* Unregister selinuxfs. */
7177 exit_sel_fs();
7178
7179 return 0;
7180}
7181#endif