blob: 75e7568cf607fa9ded2fe4f1d085628e4635c01b [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * NSA Security-Enhanced Linux (SELinux) security module
4 *
5 * This file contains the SELinux hook function implementations.
6 *
Stephen Smalley7efbb602017-08-17 13:32:36 -04007 * Authors: Stephen Smalley, <sds@tycho.nsa.gov>
Eric Paris828dfe12008-04-17 13:17:49 -04008 * Chris Vance, <cvance@nai.com>
9 * Wayne Salamon, <wsalamon@nai.com>
10 * James Morris <jmorris@redhat.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
12 * Copyright (C) 2001,2002 Networks Associates Technology, Inc.
Eric Paris2069f452008-07-04 09:47:13 +100013 * Copyright (C) 2003-2008 Red Hat, Inc., James Morris <jmorris@redhat.com>
14 * Eric Paris <eparis@redhat.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * Copyright (C) 2004-2005 Trusted Computer Solutions, Inc.
Eric Paris828dfe12008-04-17 13:17:49 -040016 * <dgoeddel@trustedcs.com>
Paul Mooreed6d76e2009-08-28 18:12:49 -040017 * Copyright (C) 2006, 2007, 2009 Hewlett-Packard Development Company, L.P.
Paul Moore82c21bf2011-08-01 11:10:33 +000018 * Paul Moore <paul@paul-moore.com>
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +090019 * Copyright (C) 2007 Hitachi Software Engineering Co., Ltd.
Eric Paris828dfe12008-04-17 13:17:49 -040020 * Yuichi Nakamura <ynakam@hitachisoft.jp>
Daniel Jurgens3a976fa2017-05-19 15:48:56 +030021 * Copyright (C) 2016 Mellanox Technologies
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 */
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/init.h>
Eric Paris0b24dcb2011-02-25 15:39:20 -050025#include <linux/kd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/kernel.h>
Scott Brandenb89999d02020-10-02 10:38:15 -070027#include <linux/kernel_read_file.h>
Roland McGrath0d094ef2008-07-25 19:45:49 -070028#include <linux/tracehook.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/errno.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010030#include <linux/sched/signal.h>
Ingo Molnar29930022017-02-08 18:51:36 +010031#include <linux/sched/task.h>
Casey Schaufler3c4ed7b2015-05-02 15:10:46 -070032#include <linux/lsm_hooks.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/xattr.h>
34#include <linux/capability.h>
35#include <linux/unistd.h>
36#include <linux/mm.h>
37#include <linux/mman.h>
38#include <linux/slab.h>
39#include <linux/pagemap.h>
Eric Paris0b24dcb2011-02-25 15:39:20 -050040#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/swap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/spinlock.h>
43#include <linux/syscalls.h>
Eric Paris2a7dba32011-02-01 11:05:39 -050044#include <linux/dcache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/file.h>
Al Viro9f3acc32008-04-24 07:44:08 -040046#include <linux/fdtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/namei.h>
48#include <linux/mount.h>
David Howells442155c2018-11-01 23:07:24 +000049#include <linux/fs_context.h>
50#include <linux/fs_parser.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/netfilter_ipv4.h>
52#include <linux/netfilter_ipv6.h>
53#include <linux/tty.h>
54#include <net/icmp.h>
Stephen Hemminger227b60f2007-10-10 17:30:46 -070055#include <net/ip.h> /* for local_port_range[] */
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <net/tcp.h> /* struct or_callable used in sock_rcv_skb */
Paul Moore47180062013-12-04 16:10:45 -050057#include <net/inet_connection_sock.h>
Paul Moore220deb92008-01-29 08:38:23 -050058#include <net/net_namespace.h>
Paul Moored621d352008-01-29 08:43:36 -050059#include <net/netlabel.h>
Eric Parisf5269712008-05-14 11:27:45 -040060#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <asm/ioctls.h>
Arun Sharma600634972011-07-26 16:09:06 -070062#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <linux/bitops.h>
64#include <linux/interrupt.h>
65#include <linux/netdevice.h> /* for network interface checks */
Hong zhi guo77954982013-03-27 06:49:35 +000066#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <linux/tcp.h>
68#include <linux/udp.h>
James Morris2ee92d42006-11-13 16:09:01 -080069#include <linux/dccp.h>
Richard Hainesd4529302018-02-13 20:57:18 +000070#include <linux/sctp.h>
71#include <net/sctp/structs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <linux/quota.h>
73#include <linux/un.h> /* for Unix socket types */
74#include <net/af_unix.h> /* for Unix socket types */
75#include <linux/parser.h>
76#include <linux/nfs_mount.h>
77#include <net/ipv6.h>
78#include <linux/hugetlb.h>
79#include <linux/personality.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#include <linux/audit.h>
Eric Paris6931dfc2005-06-30 02:58:51 -070081#include <linux/string.h>
Eric Paris23970742006-09-25 23:32:01 -070082#include <linux/mutex.h>
Frank Mayharf06febc2008-09-12 09:54:39 -070083#include <linux/posix-timers.h>
Kees Cook00234592010-02-03 15:36:43 -080084#include <linux/syslog.h>
Serge E. Hallyn34867402011-03-23 16:43:17 -070085#include <linux/user_namespace.h>
Paul Gortmaker44fc7ea2011-05-26 20:52:10 -040086#include <linux/export.h>
Al Viro40401532012-02-13 03:58:52 +000087#include <linux/msg.h>
88#include <linux/shm.h>
Chenbo Fengec27c352017-10-18 13:00:25 -070089#include <linux/bpf.h>
Ondrej Mosnacekec882da2019-02-22 15:57:17 +010090#include <linux/kernfs.h>
91#include <linux/stringhash.h> /* for hashlen_string() */
David Howellse262e32d2018-11-01 23:07:23 +000092#include <uapi/linux/mount.h>
Aaron Goidelac5656d2019-08-12 11:20:00 -040093#include <linux/fsnotify.h>
94#include <linux/fanotify.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 Moore3e112172008-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 Smalley6c5a6822019-12-17 09:15:10 -0500113static int selinux_enforcing_boot __initdata;
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
Stephen Smalley6c5a6822019-12-17 09:15:10 -0500127int selinux_enabled_boot __initdata = 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))
Stephen Smalley6c5a6822019-12-17 09:15:10 -0500133 selinux_enabled_boot = 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
Stephen Smalleye9c38f92020-01-08 11:24:47 -0500146 if (!kstrtoul(str, 0, &checkreqprot)) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500147 selinux_checkreqprot_boot = checkreqprot ? 1 : 0;
Stephen Smalleye9c38f92020-01-08 11:24:47 -0500148 if (checkreqprot)
149 pr_warn("SELinux: checkreqprot set to 1 via kernel parameter. This is deprecated and will be rejected in a future kernel release.\n");
150 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500151 return 1;
152}
153__setup("checkreqprot=", checkreqprot_setup);
154
Paul Moored621d352008-01-29 08:43:36 -0500155/**
156 * selinux_secmark_enabled - Check to see if SECMARK is currently enabled
157 *
158 * Description:
159 * This function checks the SECMARK reference counter to see if any SECMARK
160 * targets are currently configured, if the reference counter is greater than
161 * zero SECMARK is considered to be enabled. Returns true (1) if SECMARK is
Chris PeBenito2be4d742013-05-03 09:05:39 -0400162 * enabled, false (0) if SECMARK is disabled. If the always_check_network
163 * policy capability is enabled, SECMARK is always considered enabled.
Paul Moored621d352008-01-29 08:43:36 -0500164 *
165 */
166static int selinux_secmark_enabled(void)
167{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500168 return (selinux_policycap_alwaysnetwork() ||
169 atomic_read(&selinux_secmark_refcount));
Chris PeBenito2be4d742013-05-03 09:05:39 -0400170}
171
172/**
173 * selinux_peerlbl_enabled - Check to see if peer labeling is currently enabled
174 *
175 * Description:
176 * This function checks if NetLabel or labeled IPSEC is enabled. Returns true
177 * (1) if any are enabled or false (0) if neither are enabled. If the
178 * always_check_network policy capability is enabled, peer labeling
179 * is always considered enabled.
180 *
181 */
182static int selinux_peerlbl_enabled(void)
183{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500184 return (selinux_policycap_alwaysnetwork() ||
185 netlbl_enabled() || selinux_xfrm_enabled());
Paul Moored621d352008-01-29 08:43:36 -0500186}
187
Paul Moore615e51f2014-06-26 14:33:56 -0400188static int selinux_netcache_avc_callback(u32 event)
189{
190 if (event == AVC_CALLBACK_RESET) {
191 sel_netif_flush();
192 sel_netnode_flush();
193 sel_netport_flush();
194 synchronize_net();
195 }
196 return 0;
197}
198
Daniel Jurgens8f408ab2017-05-19 15:48:53 +0300199static int selinux_lsm_notifier_avc_callback(u32 event)
200{
Daniel Jurgens409dcf32017-05-19 15:48:59 +0300201 if (event == AVC_CALLBACK_RESET) {
202 sel_ib_pkey_flush();
Janne Karhunen42df7442019-06-14 15:20:14 +0300203 call_blocking_lsm_notifier(LSM_POLICY_CHANGE, NULL);
Daniel Jurgens409dcf32017-05-19 15:48:59 +0300204 }
Daniel Jurgens8f408ab2017-05-19 15:48:53 +0300205
206 return 0;
207}
208
David Howellsd84f4f92008-11-14 10:39:23 +1100209/*
210 * initialise the security for the init task
211 */
212static void cred_init_security(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
David Howells3b11a1d2008-11-14 10:39:26 +1100214 struct cred *cred = (struct cred *) current->real_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 struct task_security_struct *tsec;
216
Casey Schauflerbbd36622018-11-12 09:30:56 -0800217 tsec = selinux_cred(cred);
David Howellsd84f4f92008-11-14 10:39:23 +1100218 tsec->osid = tsec->sid = SECINITSID_KERNEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
David Howells275bb412008-11-14 10:39:19 +1100221/*
David Howells88e67f32008-11-14 10:39:21 +1100222 * get the security ID of a set of credentials
223 */
224static inline u32 cred_sid(const struct cred *cred)
225{
226 const struct task_security_struct *tsec;
227
Casey Schaufler0c6cfa62018-09-21 17:17:16 -0700228 tsec = selinux_cred(cred);
David Howells88e67f32008-11-14 10:39:21 +1100229 return tsec->sid;
230}
231
232/*
David Howells3b11a1d2008-11-14 10:39:26 +1100233 * get the objective security ID of a task
David Howells275bb412008-11-14 10:39:19 +1100234 */
235static inline u32 task_sid(const struct task_struct *task)
236{
David Howells275bb412008-11-14 10:39:19 +1100237 u32 sid;
238
239 rcu_read_lock();
David Howells88e67f32008-11-14 10:39:21 +1100240 sid = cred_sid(__task_cred(task));
David Howells275bb412008-11-14 10:39:19 +1100241 rcu_read_unlock();
242 return sid;
243}
244
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500245static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry);
246
247/*
248 * Try reloading inode security labels that have been marked as invalid. The
249 * @may_sleep parameter indicates when sleeping and thus reloading labels is
Andreas Gruenbacher42059112016-11-10 22:18:27 +0100250 * allowed; when set to false, returns -ECHILD when the label is
Al Viroe9193282018-04-24 21:31:02 -0400251 * invalid. The @dentry parameter should be set to a dentry of the inode.
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500252 */
253static int __inode_security_revalidate(struct inode *inode,
Al Viroe9193282018-04-24 21:31:02 -0400254 struct dentry *dentry,
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500255 bool may_sleep)
256{
Casey Schaufler80788c22018-09-21 17:19:11 -0700257 struct inode_security_struct *isec = selinux_inode(inode);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500258
259 might_sleep_if(may_sleep);
260
Ondrej Mosnacek65cddd52020-01-07 14:31:53 +0100261 if (selinux_initialized(&selinux_state) &&
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500262 isec->initialized != LABEL_INITIALIZED) {
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500263 if (!may_sleep)
264 return -ECHILD;
265
266 /*
267 * Try reloading the inode security label. This will fail if
268 * @opt_dentry is NULL and no dentry for this inode can be
269 * found; in that case, continue using the old label.
270 */
Al Viroe9193282018-04-24 21:31:02 -0400271 inode_doinit_with_dentry(inode, dentry);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500272 }
273 return 0;
274}
275
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500276static struct inode_security_struct *inode_security_novalidate(struct inode *inode)
277{
Casey Schaufler80788c22018-09-21 17:19:11 -0700278 return selinux_inode(inode);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500279}
280
281static struct inode_security_struct *inode_security_rcu(struct inode *inode, bool rcu)
282{
283 int error;
284
285 error = __inode_security_revalidate(inode, NULL, !rcu);
286 if (error)
287 return ERR_PTR(error);
Casey Schaufler80788c22018-09-21 17:19:11 -0700288 return selinux_inode(inode);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500289}
290
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500291/*
292 * Get the security label of an inode.
293 */
294static struct inode_security_struct *inode_security(struct inode *inode)
295{
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500296 __inode_security_revalidate(inode, NULL, true);
Casey Schaufler80788c22018-09-21 17:19:11 -0700297 return selinux_inode(inode);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500298}
299
Paul Moore2c971652016-04-19 16:36:28 -0400300static struct inode_security_struct *backing_inode_security_novalidate(struct dentry *dentry)
301{
302 struct inode *inode = d_backing_inode(dentry);
303
Casey Schaufler80788c22018-09-21 17:19:11 -0700304 return selinux_inode(inode);
Paul Moore2c971652016-04-19 16:36:28 -0400305}
306
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500307/*
308 * Get the security label of a dentry's backing inode.
309 */
310static struct inode_security_struct *backing_inode_security(struct dentry *dentry)
311{
312 struct inode *inode = d_backing_inode(dentry);
313
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500314 __inode_security_revalidate(inode, dentry, true);
Casey Schaufler80788c22018-09-21 17:19:11 -0700315 return selinux_inode(inode);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500316}
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318static void inode_free_security(struct inode *inode)
319{
Casey Schaufler80788c22018-09-21 17:19:11 -0700320 struct inode_security_struct *isec = selinux_inode(inode);
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700321 struct superblock_security_struct *sbsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700323 if (!isec)
324 return;
325 sbsec = inode->i_sb->s_security;
Waiman Long9629d042015-07-10 17:19:56 -0400326 /*
327 * As not all inode security structures are in a list, we check for
328 * empty list outside of the lock to make sure that we won't waste
329 * time taking a lock doing nothing.
330 *
331 * The list_del_init() function can be safely called more than once.
332 * It should not be possible for this function to be called with
333 * concurrent list_add(), but for better safety against future changes
334 * in the code, we use list_empty_careful() here.
335 */
336 if (!list_empty_careful(&isec->list)) {
337 spin_lock(&sbsec->isec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 list_del_init(&isec->list);
Waiman Long9629d042015-07-10 17:19:56 -0400339 spin_unlock(&sbsec->isec_lock);
340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343static void superblock_free_security(struct super_block *sb)
344{
345 struct superblock_security_struct *sbsec = sb->s_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 sb->s_security = NULL;
347 kfree(sbsec);
348}
349
Al Virobd323652018-12-13 15:04:59 -0500350struct selinux_mnt_opts {
351 const char *fscontext, *context, *rootcontext, *defcontext;
352};
353
Al Viro204cc0c2018-12-13 13:41:47 -0500354static void selinux_free_mnt_opts(void *mnt_opts)
355{
Al Virobd323652018-12-13 15:04:59 -0500356 struct selinux_mnt_opts *opts = mnt_opts;
357 kfree(opts->fscontext);
358 kfree(opts->context);
359 kfree(opts->rootcontext);
360 kfree(opts->defcontext);
Al Viro204cc0c2018-12-13 13:41:47 -0500361 kfree(opts);
362}
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364enum {
Eric Paris31e87932007-09-19 17:19:12 -0400365 Opt_error = -1,
David Howells442155c2018-11-01 23:07:24 +0000366 Opt_context = 0,
367 Opt_defcontext = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 Opt_fscontext = 2,
David Howells442155c2018-11-01 23:07:24 +0000369 Opt_rootcontext = 3,
370 Opt_seclabel = 4,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371};
372
Al Viroda3d76a2018-12-17 10:14:16 -0500373#define A(s, has_arg) {#s, sizeof(#s) - 1, Opt_##s, has_arg}
Al Viro169d68efb2018-12-14 22:44:50 -0500374static struct {
375 const char *name;
376 int len;
377 int opt;
378 bool has_arg;
379} tokens[] = {
Al Viroda3d76a2018-12-17 10:14:16 -0500380 A(context, true),
381 A(fscontext, true),
382 A(defcontext, true),
383 A(rootcontext, true),
384 A(seclabel, false),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385};
Al Viro169d68efb2018-12-14 22:44:50 -0500386#undef A
387
388static int match_opt_prefix(char *s, int l, char **arg)
389{
390 int i;
391
392 for (i = 0; i < ARRAY_SIZE(tokens); i++) {
393 size_t len = tokens[i].len;
394 if (len > l || memcmp(s, tokens[i].name, len))
395 continue;
396 if (tokens[i].has_arg) {
397 if (len == l || s[len] != '=')
398 continue;
399 *arg = s + len + 1;
400 } else if (len != l)
401 continue;
402 return tokens[i].opt;
403 }
404 return Opt_error;
405}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407#define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n"
408
Eric Parisc312feb2006-07-10 04:43:53 -0700409static int may_context_mount_sb_relabel(u32 sid,
410 struct superblock_security_struct *sbsec,
David Howells275bb412008-11-14 10:39:19 +1100411 const struct cred *cred)
Eric Parisc312feb2006-07-10 04:43:53 -0700412{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -0700413 const struct task_security_struct *tsec = selinux_cred(cred);
Eric Parisc312feb2006-07-10 04:43:53 -0700414 int rc;
415
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500416 rc = avc_has_perm(&selinux_state,
417 tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
Eric Parisc312feb2006-07-10 04:43:53 -0700418 FILESYSTEM__RELABELFROM, NULL);
419 if (rc)
420 return rc;
421
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500422 rc = avc_has_perm(&selinux_state,
423 tsec->sid, sid, SECCLASS_FILESYSTEM,
Eric Parisc312feb2006-07-10 04:43:53 -0700424 FILESYSTEM__RELABELTO, NULL);
425 return rc;
426}
427
Eric Paris08089252006-07-10 04:43:55 -0700428static int may_context_mount_inode_relabel(u32 sid,
429 struct superblock_security_struct *sbsec,
David Howells275bb412008-11-14 10:39:19 +1100430 const struct cred *cred)
Eric Paris08089252006-07-10 04:43:55 -0700431{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -0700432 const struct task_security_struct *tsec = selinux_cred(cred);
Eric Paris08089252006-07-10 04:43:55 -0700433 int rc;
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500434 rc = avc_has_perm(&selinux_state,
435 tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
Eric Paris08089252006-07-10 04:43:55 -0700436 FILESYSTEM__RELABELFROM, NULL);
437 if (rc)
438 return rc;
439
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500440 rc = avc_has_perm(&selinux_state,
441 sid, sbsec->sid, SECCLASS_FILESYSTEM,
Eric Paris08089252006-07-10 04:43:55 -0700442 FILESYSTEM__ASSOCIATE, NULL);
443 return rc;
444}
445
Ondrej Mosnaceka83d6dd2018-12-21 21:18:52 +0100446static int selinux_is_genfs_special_handling(struct super_block *sb)
Eric Parisb43e7252012-10-10 14:27:35 -0400447{
Ondrej Mosnaceka83d6dd2018-12-21 21:18:52 +0100448 /* Special handling. Genfs but also in-core setxattr handler */
449 return !strcmp(sb->s_type->name, "sysfs") ||
Mark Salyzynd5f3a5f2015-02-04 11:34:30 -0500450 !strcmp(sb->s_type->name, "pstore") ||
451 !strcmp(sb->s_type->name, "debugfs") ||
Yongqin Liua2c7c6f2017-01-09 10:07:30 -0500452 !strcmp(sb->s_type->name, "tracefs") ||
Stephen Smalley2651225b2017-02-28 10:35:56 -0500453 !strcmp(sb->s_type->name, "rootfs") ||
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500454 (selinux_policycap_cgroupseclabel() &&
Stephen Smalley2651225b2017-02-28 10:35:56 -0500455 (!strcmp(sb->s_type->name, "cgroup") ||
456 !strcmp(sb->s_type->name, "cgroup2")));
Eric Parisb43e7252012-10-10 14:27:35 -0400457}
458
Ondrej Mosnaceka83d6dd2018-12-21 21:18:52 +0100459static int selinux_is_sblabel_mnt(struct super_block *sb)
460{
461 struct superblock_security_struct *sbsec = sb->s_security;
462
463 /*
464 * IMPORTANT: Double-check logic in this function when adding a new
465 * SECURITY_FS_USE_* definition!
466 */
467 BUILD_BUG_ON(SECURITY_FS_USE_MAX != 7);
468
469 switch (sbsec->behavior) {
470 case SECURITY_FS_USE_XATTR:
471 case SECURITY_FS_USE_TRANS:
472 case SECURITY_FS_USE_TASK:
473 case SECURITY_FS_USE_NATIVE:
474 return 1;
475
476 case SECURITY_FS_USE_GENFS:
477 return selinux_is_genfs_special_handling(sb);
478
479 /* Never allow relabeling on context mounts */
480 case SECURITY_FS_USE_MNTPOINT:
481 case SECURITY_FS_USE_NONE:
482 default:
483 return 0;
484 }
485}
486
Eric Parisc9180a52007-11-30 13:00:35 -0500487static int sb_finish_set_opts(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
489 struct superblock_security_struct *sbsec = sb->s_security;
490 struct dentry *root = sb->s_root;
David Howellsc6f493d2015-03-17 22:26:22 +0000491 struct inode *root_inode = d_backing_inode(root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 int rc = 0;
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
495 /* Make sure that the xattr handler exists and that no
496 error other than -ENODATA is returned by getxattr on
497 the root directory. -ENODATA is ok, as this may be
498 the first boot of the SELinux kernel before we have
499 assigned xattr values to the filesystem. */
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200500 if (!(root_inode->i_opflags & IOP_XATTR)) {
peter enderborgc103a912018-06-12 10:09:03 +0200501 pr_warn("SELinux: (dev %s, type %s) has no "
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800502 "xattr support\n", sb->s_id, sb->s_type->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 rc = -EOPNOTSUPP;
504 goto out;
505 }
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200506
507 rc = __vfs_getxattr(root, root_inode, XATTR_NAME_SELINUX, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 if (rc < 0 && rc != -ENODATA) {
509 if (rc == -EOPNOTSUPP)
peter enderborgc103a912018-06-12 10:09:03 +0200510 pr_warn("SELinux: (dev %s, type "
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800511 "%s) has no security xattr handler\n",
512 sb->s_id, sb->s_type->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 else
peter enderborgc103a912018-06-12 10:09:03 +0200514 pr_warn("SELinux: (dev %s, type "
Linus Torvalds29b1deb2013-12-15 11:17:45 -0800515 "%s) getxattr errno %d\n", sb->s_id,
516 sb->s_type->name, -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 goto out;
518 }
519 }
520
Eric Pariseadcabc2012-08-24 15:59:14 -0400521 sbsec->flags |= SE_SBINITIALIZED;
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400522
523 /*
524 * Explicitly set or clear SBLABEL_MNT. It's not sufficient to simply
525 * leave the flag untouched because sb_clone_mnt_opts might be handing
526 * us a superblock that needs the flag to be cleared.
527 */
Eric Parisb43e7252012-10-10 14:27:35 -0400528 if (selinux_is_sblabel_mnt(sb))
Eric Paris12f348b2012-10-09 10:56:25 -0400529 sbsec->flags |= SBLABEL_MNT;
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400530 else
531 sbsec->flags &= ~SBLABEL_MNT;
David P. Quigleyddd29ec2009-09-09 14:25:37 -0400532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 /* Initialize the root inode. */
Eric Parisc9180a52007-11-30 13:00:35 -0500534 rc = inode_doinit_with_dentry(root_inode, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 /* Initialize any other inodes associated with the superblock, e.g.
537 inodes created prior to initial policy load or inodes created
538 during get_sb by a pseudo filesystem that directly
539 populates itself. */
540 spin_lock(&sbsec->isec_lock);
Al Viro8d641242018-12-10 15:34:12 -0500541 while (!list_empty(&sbsec->isec_head)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 struct inode_security_struct *isec =
Al Viro8d641242018-12-10 15:34:12 -0500543 list_first_entry(&sbsec->isec_head,
Eric Parisc9180a52007-11-30 13:00:35 -0500544 struct inode_security_struct, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 struct inode *inode = isec->inode;
Stephen Smalley923190d2014-10-06 16:32:52 -0400546 list_del_init(&isec->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 spin_unlock(&sbsec->isec_lock);
548 inode = igrab(inode);
549 if (inode) {
Eric Parisc9180a52007-11-30 13:00:35 -0500550 if (!IS_PRIVATE(inode))
Paul Moorecb89e242020-01-10 16:32:10 -0500551 inode_doinit_with_dentry(inode, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 iput(inode);
553 }
554 spin_lock(&sbsec->isec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
556 spin_unlock(&sbsec->isec_lock);
557out:
Eric Parisc9180a52007-11-30 13:00:35 -0500558 return rc;
559}
560
Eric Parisc9180a52007-11-30 13:00:35 -0500561static int bad_option(struct superblock_security_struct *sbsec, char flag,
562 u32 old_sid, u32 new_sid)
563{
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500564 char mnt_flags = sbsec->flags & SE_MNTMASK;
565
Eric Parisc9180a52007-11-30 13:00:35 -0500566 /* check if the old mount command had the same options */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500567 if (sbsec->flags & SE_SBINITIALIZED)
Eric Parisc9180a52007-11-30 13:00:35 -0500568 if (!(sbsec->flags & flag) ||
569 (old_sid != new_sid))
570 return 1;
571
572 /* check if we were passed the same options twice,
573 * aka someone passed context=a,context=b
574 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500575 if (!(sbsec->flags & SE_SBINITIALIZED))
576 if (mnt_flags & flag)
Eric Parisc9180a52007-11-30 13:00:35 -0500577 return 1;
578 return 0;
579}
Eric Parise0007522008-03-05 10:31:54 -0500580
Al Virobd323652018-12-13 15:04:59 -0500581static int parse_sid(struct super_block *sb, const char *s, u32 *sid)
582{
583 int rc = security_context_str_to_sid(&selinux_state, s,
584 sid, GFP_KERNEL);
585 if (rc)
586 pr_warn("SELinux: security_context_str_to_sid"
587 "(%s) failed for (dev %s, type %s) errno=%d\n",
588 s, sb->s_id, sb->s_type->name, rc);
589 return rc;
590}
591
Eric Parisc9180a52007-11-30 13:00:35 -0500592/*
593 * Allow filesystems with binary mount data to explicitly set mount point
594 * labeling information.
595 */
Eric Parise0007522008-03-05 10:31:54 -0500596static int selinux_set_mnt_opts(struct super_block *sb,
Al Viro204cc0c2018-12-13 13:41:47 -0500597 void *mnt_opts,
David Quigley649f6e72013-05-22 12:50:36 -0400598 unsigned long kern_flags,
599 unsigned long *set_kern_flags)
Eric Parisc9180a52007-11-30 13:00:35 -0500600{
David Howells275bb412008-11-14 10:39:19 +1100601 const struct cred *cred = current_cred();
Eric Parisc9180a52007-11-30 13:00:35 -0500602 struct superblock_security_struct *sbsec = sb->s_security;
Ondrej Mosnacekb159e862020-11-04 13:01:10 +0100603 struct dentry *root = sb->s_root;
Al Virobd323652018-12-13 15:04:59 -0500604 struct selinux_mnt_opts *opts = mnt_opts;
Paul Moore2c971652016-04-19 16:36:28 -0400605 struct inode_security_struct *root_isec;
Eric Parisc9180a52007-11-30 13:00:35 -0500606 u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
607 u32 defcontext_sid = 0;
Al Virobd323652018-12-13 15:04:59 -0500608 int rc = 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500609
610 mutex_lock(&sbsec->lock);
611
Ondrej Mosnacek65cddd52020-01-07 14:31:53 +0100612 if (!selinux_initialized(&selinux_state)) {
Al Virobd323652018-12-13 15:04:59 -0500613 if (!opts) {
Eric Parisc9180a52007-11-30 13:00:35 -0500614 /* Defer initialization until selinux_complete_init,
615 after the initial policy is loaded and the security
616 server is ready to handle calls. */
Eric Parisc9180a52007-11-30 13:00:35 -0500617 goto out;
618 }
619 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +0200620 pr_warn("SELinux: Unable to set superblock options "
Eric Paris744ba352008-04-17 11:52:44 -0400621 "before the security server is initialized\n");
Eric Parisc9180a52007-11-30 13:00:35 -0500622 goto out;
623 }
David Quigley649f6e72013-05-22 12:50:36 -0400624 if (kern_flags && !set_kern_flags) {
625 /* Specifying internal flags without providing a place to
626 * place the results is not allowed */
627 rc = -EINVAL;
628 goto out;
629 }
Eric Parisc9180a52007-11-30 13:00:35 -0500630
631 /*
Eric Parise0007522008-03-05 10:31:54 -0500632 * Binary mount data FS will come through this function twice. Once
633 * from an explicit call and once from the generic calls from the vfs.
634 * Since the generic VFS calls will not contain any security mount data
635 * we need to skip the double mount verification.
636 *
637 * This does open a hole in which we will not notice if the first
638 * mount using this sb set explict options and a second mount using
639 * this sb does not set any security options. (The first options
640 * will be used for both mounts)
641 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500642 if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
Al Virobd323652018-12-13 15:04:59 -0500643 && !opts)
Eric Parisf5269712008-05-14 11:27:45 -0400644 goto out;
Eric Parise0007522008-03-05 10:31:54 -0500645
Paul Moore2c971652016-04-19 16:36:28 -0400646 root_isec = backing_inode_security_novalidate(root);
647
Eric Parise0007522008-03-05 10:31:54 -0500648 /*
Eric Parisc9180a52007-11-30 13:00:35 -0500649 * parse the mount options, check if they are valid sids.
650 * also check if someone is trying to mount the same sb more
651 * than once with different security options.
652 */
Al Virobd323652018-12-13 15:04:59 -0500653 if (opts) {
654 if (opts->fscontext) {
655 rc = parse_sid(sb, opts->fscontext, &fscontext_sid);
656 if (rc)
657 goto out;
Eric Parisc9180a52007-11-30 13:00:35 -0500658 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
659 fscontext_sid))
660 goto out_double_mount;
Eric Parisc9180a52007-11-30 13:00:35 -0500661 sbsec->flags |= FSCONTEXT_MNT;
Al Virobd323652018-12-13 15:04:59 -0500662 }
663 if (opts->context) {
664 rc = parse_sid(sb, opts->context, &context_sid);
665 if (rc)
666 goto out;
Eric Parisc9180a52007-11-30 13:00:35 -0500667 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
668 context_sid))
669 goto out_double_mount;
Eric Parisc9180a52007-11-30 13:00:35 -0500670 sbsec->flags |= CONTEXT_MNT;
Al Virobd323652018-12-13 15:04:59 -0500671 }
672 if (opts->rootcontext) {
673 rc = parse_sid(sb, opts->rootcontext, &rootcontext_sid);
674 if (rc)
675 goto out;
Eric Parisc9180a52007-11-30 13:00:35 -0500676 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
677 rootcontext_sid))
678 goto out_double_mount;
Eric Parisc9180a52007-11-30 13:00:35 -0500679 sbsec->flags |= ROOTCONTEXT_MNT;
Al Virobd323652018-12-13 15:04:59 -0500680 }
681 if (opts->defcontext) {
682 rc = parse_sid(sb, opts->defcontext, &defcontext_sid);
683 if (rc)
684 goto out;
Eric Parisc9180a52007-11-30 13:00:35 -0500685 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
686 defcontext_sid))
687 goto out_double_mount;
Eric Parisc9180a52007-11-30 13:00:35 -0500688 sbsec->flags |= DEFCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500689 }
690 }
691
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500692 if (sbsec->flags & SE_SBINITIALIZED) {
Eric Parisc9180a52007-11-30 13:00:35 -0500693 /* previously mounted with options, but not on this attempt? */
Al Virobd323652018-12-13 15:04:59 -0500694 if ((sbsec->flags & SE_MNTMASK) && !opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500695 goto out_double_mount;
696 rc = 0;
697 goto out;
698 }
699
James Morris089be432008-07-15 18:32:49 +1000700 if (strcmp(sb->s_type->name, "proc") == 0)
Stephen Smalley134509d2015-06-04 16:22:17 -0400701 sbsec->flags |= SE_SBPROC | SE_SBGENFS;
702
Stephen Smalley8e014722015-06-04 16:22:17 -0400703 if (!strcmp(sb->s_type->name, "debugfs") ||
Jeff Vander Stoep6a391182017-06-20 09:35:33 -0700704 !strcmp(sb->s_type->name, "tracefs") ||
Hridya Valsarajua20456a2020-02-01 17:46:23 -0800705 !strcmp(sb->s_type->name, "binder") ||
Connor O'Brien4ca54d32020-02-07 10:01:49 -0800706 !strcmp(sb->s_type->name, "bpf") ||
Ondrej Mosnacekb7540262019-02-22 15:57:14 +0100707 !strcmp(sb->s_type->name, "pstore"))
708 sbsec->flags |= SE_SBGENFS;
709
710 if (!strcmp(sb->s_type->name, "sysfs") ||
Antonio Murdaca901ef842017-02-09 17:02:42 +0100711 !strcmp(sb->s_type->name, "cgroup") ||
712 !strcmp(sb->s_type->name, "cgroup2"))
Ondrej Mosnacekb7540262019-02-22 15:57:14 +0100713 sbsec->flags |= SE_SBGENFS | SE_SBGENFS_XATTR;
Eric Parisc9180a52007-11-30 13:00:35 -0500714
David Quigleyeb9ae682013-05-22 12:50:37 -0400715 if (!sbsec->behavior) {
716 /*
717 * Determine the labeling behavior to use for this
718 * filesystem type.
719 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500720 rc = security_fs_use(&selinux_state, sb);
David Quigleyeb9ae682013-05-22 12:50:37 -0400721 if (rc) {
peter enderborgc103a912018-06-12 10:09:03 +0200722 pr_warn("%s: security_fs_use(%s) returned %d\n",
David Quigleyeb9ae682013-05-22 12:50:37 -0400723 __func__, sb->s_type->name, rc);
724 goto out;
725 }
Eric Parisc9180a52007-11-30 13:00:35 -0500726 }
Seth Forsheeaad82892016-04-26 14:36:20 -0500727
728 /*
Stephen Smalley01593d32017-01-09 10:07:31 -0500729 * If this is a user namespace mount and the filesystem type is not
730 * explicitly whitelisted, then no contexts are allowed on the command
731 * line and security labels must be ignored.
Seth Forsheeaad82892016-04-26 14:36:20 -0500732 */
Stephen Smalley01593d32017-01-09 10:07:31 -0500733 if (sb->s_user_ns != &init_user_ns &&
734 strcmp(sb->s_type->name, "tmpfs") &&
735 strcmp(sb->s_type->name, "ramfs") &&
736 strcmp(sb->s_type->name, "devpts")) {
Seth Forsheeaad82892016-04-26 14:36:20 -0500737 if (context_sid || fscontext_sid || rootcontext_sid ||
738 defcontext_sid) {
739 rc = -EACCES;
740 goto out;
741 }
742 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
743 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500744 rc = security_transition_sid(&selinux_state,
745 current_sid(),
746 current_sid(),
Seth Forsheeaad82892016-04-26 14:36:20 -0500747 SECCLASS_FILE, NULL,
748 &sbsec->mntpoint_sid);
749 if (rc)
750 goto out;
751 }
752 goto out_set_opts;
753 }
754
Eric Parisc9180a52007-11-30 13:00:35 -0500755 /* sets the context of the superblock for the fs being mounted. */
756 if (fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100757 rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500758 if (rc)
759 goto out;
760
761 sbsec->sid = fscontext_sid;
762 }
763
764 /*
765 * Switch to using mount point labeling behavior.
766 * sets the label used on all file below the mountpoint, and will set
767 * the superblock context if not already set.
768 */
David Quigleyeb9ae682013-05-22 12:50:37 -0400769 if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !context_sid) {
770 sbsec->behavior = SECURITY_FS_USE_NATIVE;
771 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
772 }
773
Eric Parisc9180a52007-11-30 13:00:35 -0500774 if (context_sid) {
775 if (!fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100776 rc = may_context_mount_sb_relabel(context_sid, sbsec,
777 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500778 if (rc)
779 goto out;
780 sbsec->sid = context_sid;
781 } else {
David Howells275bb412008-11-14 10:39:19 +1100782 rc = may_context_mount_inode_relabel(context_sid, sbsec,
783 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500784 if (rc)
785 goto out;
786 }
787 if (!rootcontext_sid)
788 rootcontext_sid = context_sid;
789
790 sbsec->mntpoint_sid = context_sid;
791 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
792 }
793
794 if (rootcontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100795 rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec,
796 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500797 if (rc)
798 goto out;
799
800 root_isec->sid = rootcontext_sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -0500801 root_isec->initialized = LABEL_INITIALIZED;
Eric Parisc9180a52007-11-30 13:00:35 -0500802 }
803
804 if (defcontext_sid) {
David Quigleyeb9ae682013-05-22 12:50:37 -0400805 if (sbsec->behavior != SECURITY_FS_USE_XATTR &&
806 sbsec->behavior != SECURITY_FS_USE_NATIVE) {
Eric Parisc9180a52007-11-30 13:00:35 -0500807 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +0200808 pr_warn("SELinux: defcontext option is "
Eric Parisc9180a52007-11-30 13:00:35 -0500809 "invalid for this filesystem type\n");
810 goto out;
811 }
812
813 if (defcontext_sid != sbsec->def_sid) {
814 rc = may_context_mount_inode_relabel(defcontext_sid,
David Howells275bb412008-11-14 10:39:19 +1100815 sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500816 if (rc)
817 goto out;
818 }
819
820 sbsec->def_sid = defcontext_sid;
821 }
822
Seth Forsheeaad82892016-04-26 14:36:20 -0500823out_set_opts:
Eric Parisc9180a52007-11-30 13:00:35 -0500824 rc = sb_finish_set_opts(sb);
825out:
Eric Parisbc7e9822006-09-25 23:32:02 -0700826 mutex_unlock(&sbsec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 return rc;
Eric Parisc9180a52007-11-30 13:00:35 -0500828out_double_mount:
829 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +0200830 pr_warn("SELinux: mount invalid. Same superblock, different "
Al Virobd323652018-12-13 15:04:59 -0500831 "security settings for (dev %s, type %s)\n", sb->s_id,
832 sb->s_type->name);
Eric Parisc9180a52007-11-30 13:00:35 -0500833 goto out;
834}
835
Jeff Layton094f7b62013-04-01 08:14:24 -0400836static int selinux_cmp_sb_context(const struct super_block *oldsb,
837 const struct super_block *newsb)
838{
839 struct superblock_security_struct *old = oldsb->s_security;
840 struct superblock_security_struct *new = newsb->s_security;
841 char oldflags = old->flags & SE_MNTMASK;
842 char newflags = new->flags & SE_MNTMASK;
843
844 if (oldflags != newflags)
845 goto mismatch;
846 if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid)
847 goto mismatch;
848 if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid)
849 goto mismatch;
850 if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid)
851 goto mismatch;
852 if (oldflags & ROOTCONTEXT_MNT) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500853 struct inode_security_struct *oldroot = backing_inode_security(oldsb->s_root);
854 struct inode_security_struct *newroot = backing_inode_security(newsb->s_root);
Jeff Layton094f7b62013-04-01 08:14:24 -0400855 if (oldroot->sid != newroot->sid)
856 goto mismatch;
857 }
858 return 0;
859mismatch:
peter enderborgc103a912018-06-12 10:09:03 +0200860 pr_warn("SELinux: mount invalid. Same superblock, "
Jeff Layton094f7b62013-04-01 08:14:24 -0400861 "different security settings for (dev %s, "
862 "type %s)\n", newsb->s_id, newsb->s_type->name);
863 return -EBUSY;
864}
865
866static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400867 struct super_block *newsb,
868 unsigned long kern_flags,
869 unsigned long *set_kern_flags)
Eric Parisc9180a52007-11-30 13:00:35 -0500870{
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400871 int rc = 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500872 const struct superblock_security_struct *oldsbsec = oldsb->s_security;
873 struct superblock_security_struct *newsbsec = newsb->s_security;
874
875 int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT);
876 int set_context = (oldsbsec->flags & CONTEXT_MNT);
877 int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT);
878
Eric Paris0f5e6422008-04-21 16:24:11 -0400879 /*
880 * if the parent was able to be mounted it clearly had no special lsm
Al Viroe8c26252010-03-23 06:36:54 -0400881 * mount options. thus we can safely deal with this superblock later
Eric Paris0f5e6422008-04-21 16:24:11 -0400882 */
Ondrej Mosnacek65cddd52020-01-07 14:31:53 +0100883 if (!selinux_initialized(&selinux_state))
Jeff Layton094f7b62013-04-01 08:14:24 -0400884 return 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500885
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400886 /*
887 * Specifying internal flags without providing a place to
888 * place the results is not allowed.
889 */
890 if (kern_flags && !set_kern_flags)
891 return -EINVAL;
892
Eric Parisc9180a52007-11-30 13:00:35 -0500893 /* how can we clone if the old one wasn't set up?? */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500894 BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED));
Eric Parisc9180a52007-11-30 13:00:35 -0500895
Jeff Layton094f7b62013-04-01 08:14:24 -0400896 /* if fs is reusing a sb, make sure that the contexts match */
J. Bruce Fields3815a242019-03-05 16:17:58 -0500897 if (newsbsec->flags & SE_SBINITIALIZED) {
898 if ((kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context)
899 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
Jeff Layton094f7b62013-04-01 08:14:24 -0400900 return selinux_cmp_sb_context(oldsb, newsb);
J. Bruce Fields3815a242019-03-05 16:17:58 -0500901 }
Eric Paris5a552612008-04-09 14:08:35 -0400902
Eric Parisc9180a52007-11-30 13:00:35 -0500903 mutex_lock(&newsbsec->lock);
904
905 newsbsec->flags = oldsbsec->flags;
906
907 newsbsec->sid = oldsbsec->sid;
908 newsbsec->def_sid = oldsbsec->def_sid;
909 newsbsec->behavior = oldsbsec->behavior;
910
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400911 if (newsbsec->behavior == SECURITY_FS_USE_NATIVE &&
912 !(kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500913 rc = security_fs_use(&selinux_state, newsb);
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400914 if (rc)
915 goto out;
916 }
917
918 if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !set_context) {
919 newsbsec->behavior = SECURITY_FS_USE_NATIVE;
920 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
921 }
922
Eric Parisc9180a52007-11-30 13:00:35 -0500923 if (set_context) {
924 u32 sid = oldsbsec->mntpoint_sid;
925
926 if (!set_fscontext)
927 newsbsec->sid = sid;
928 if (!set_rootcontext) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500929 struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
Eric Parisc9180a52007-11-30 13:00:35 -0500930 newisec->sid = sid;
931 }
932 newsbsec->mntpoint_sid = sid;
933 }
934 if (set_rootcontext) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500935 const struct inode_security_struct *oldisec = backing_inode_security(oldsb->s_root);
936 struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
Eric Parisc9180a52007-11-30 13:00:35 -0500937
938 newisec->sid = oldisec->sid;
939 }
940
941 sb_finish_set_opts(newsb);
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400942out:
Eric Parisc9180a52007-11-30 13:00:35 -0500943 mutex_unlock(&newsbsec->lock);
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400944 return rc;
Eric Parisc9180a52007-11-30 13:00:35 -0500945}
946
Al Viroba641862018-12-14 20:28:15 -0500947static int selinux_add_opt(int token, const char *s, void **mnt_opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500948{
Al Viroba641862018-12-14 20:28:15 -0500949 struct selinux_mnt_opts *opts = *mnt_opts;
Eric Parisc9180a52007-11-30 13:00:35 -0500950
Al Viroda3d76a2018-12-17 10:14:16 -0500951 if (token == Opt_seclabel) /* eaten and completely ignored */
Al Viro169d68efb2018-12-14 22:44:50 -0500952 return 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500953
Al Viroba641862018-12-14 20:28:15 -0500954 if (!opts) {
955 opts = kzalloc(sizeof(struct selinux_mnt_opts), GFP_KERNEL);
956 if (!opts)
957 return -ENOMEM;
958 *mnt_opts = opts;
959 }
960 if (!s)
961 return -ENOMEM;
962 switch (token) {
963 case Opt_context:
964 if (opts->context || opts->defcontext)
965 goto Einval;
966 opts->context = s;
967 break;
968 case Opt_fscontext:
969 if (opts->fscontext)
970 goto Einval;
971 opts->fscontext = s;
972 break;
973 case Opt_rootcontext:
974 if (opts->rootcontext)
975 goto Einval;
976 opts->rootcontext = s;
977 break;
978 case Opt_defcontext:
979 if (opts->context || opts->defcontext)
980 goto Einval;
981 opts->defcontext = s;
982 break;
983 }
984 return 0;
985Einval:
986 pr_warn(SEL_MOUNT_FAIL_MSG);
Al Viroba641862018-12-14 20:28:15 -0500987 return -EINVAL;
988}
Eric Parisc9180a52007-11-30 13:00:35 -0500989
Al Viro757cbe52018-12-14 23:42:21 -0500990static int selinux_add_mnt_opt(const char *option, const char *val, int len,
991 void **mnt_opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500992{
Al Viro757cbe52018-12-14 23:42:21 -0500993 int token = Opt_error;
994 int rc, i;
Eric Parisc9180a52007-11-30 13:00:35 -0500995
Al Viro757cbe52018-12-14 23:42:21 -0500996 for (i = 0; i < ARRAY_SIZE(tokens); i++) {
997 if (strcmp(option, tokens[i].name) == 0) {
998 token = tokens[i].opt;
Eric Parisc9180a52007-11-30 13:00:35 -0500999 break;
Eric Parisc9180a52007-11-30 13:00:35 -05001000 }
1001 }
1002
Al Viro757cbe52018-12-14 23:42:21 -05001003 if (token == Opt_error)
1004 return -EINVAL;
Eric Parise0007522008-03-05 10:31:54 -05001005
Gen Zhange2e0e092019-06-12 21:28:21 +08001006 if (token != Opt_seclabel) {
Al Viro757cbe52018-12-14 23:42:21 -05001007 val = kmemdup_nul(val, len, GFP_KERNEL);
Gen Zhange2e0e092019-06-12 21:28:21 +08001008 if (!val) {
1009 rc = -ENOMEM;
1010 goto free_opt;
1011 }
1012 }
Al Viro757cbe52018-12-14 23:42:21 -05001013 rc = selinux_add_opt(token, val, mnt_opts);
1014 if (unlikely(rc)) {
1015 kfree(val);
Gen Zhange2e0e092019-06-12 21:28:21 +08001016 goto free_opt;
1017 }
1018 return rc;
1019
1020free_opt:
1021 if (*mnt_opts) {
1022 selinux_free_mnt_opts(*mnt_opts);
1023 *mnt_opts = NULL;
Al Viro757cbe52018-12-14 23:42:21 -05001024 }
1025 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Al Viroe3489f82018-12-13 00:24:36 -05001028static int show_sid(struct seq_file *m, u32 sid)
Eric Paris2069f452008-07-04 09:47:13 +10001029{
Al Viroe3489f82018-12-13 00:24:36 -05001030 char *context = NULL;
1031 u32 len;
1032 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Al Viroe3489f82018-12-13 00:24:36 -05001034 rc = security_sid_to_context(&selinux_state, sid,
1035 &context, &len);
1036 if (!rc) {
1037 bool has_comma = context && strchr(context, ',');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
David Howells442155c2018-11-01 23:07:24 +00001039 seq_putc(m, '=');
Eric Paris2069f452008-07-04 09:47:13 +10001040 if (has_comma)
1041 seq_putc(m, '\"');
Al Viroe3489f82018-12-13 00:24:36 -05001042 seq_escape(m, context, "\"\n\\");
Eric Paris2069f452008-07-04 09:47:13 +10001043 if (has_comma)
1044 seq_putc(m, '\"');
1045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 kfree(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 return rc;
1048}
Eric Paris2069f452008-07-04 09:47:13 +10001049
1050static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
1051{
Al Viroe3489f82018-12-13 00:24:36 -05001052 struct superblock_security_struct *sbsec = sb->s_security;
Eric Paris2069f452008-07-04 09:47:13 +10001053 int rc;
1054
Al Viroe3489f82018-12-13 00:24:36 -05001055 if (!(sbsec->flags & SE_SBINITIALIZED))
1056 return 0;
1057
Ondrej Mosnacek65cddd52020-01-07 14:31:53 +01001058 if (!selinux_initialized(&selinux_state))
Al Viroe3489f82018-12-13 00:24:36 -05001059 return 0;
1060
1061 if (sbsec->flags & FSCONTEXT_MNT) {
1062 seq_putc(m, ',');
1063 seq_puts(m, FSCONTEXT_STR);
1064 rc = show_sid(m, sbsec->sid);
1065 if (rc)
1066 return rc;
Eric Paris383795c2008-07-29 17:07:26 -04001067 }
Al Viroe3489f82018-12-13 00:24:36 -05001068 if (sbsec->flags & CONTEXT_MNT) {
1069 seq_putc(m, ',');
1070 seq_puts(m, CONTEXT_STR);
1071 rc = show_sid(m, sbsec->mntpoint_sid);
1072 if (rc)
1073 return rc;
1074 }
1075 if (sbsec->flags & DEFCONTEXT_MNT) {
1076 seq_putc(m, ',');
1077 seq_puts(m, DEFCONTEXT_STR);
1078 rc = show_sid(m, sbsec->def_sid);
1079 if (rc)
1080 return rc;
1081 }
1082 if (sbsec->flags & ROOTCONTEXT_MNT) {
Ondrej Mosnacekb159e862020-11-04 13:01:10 +01001083 struct dentry *root = sb->s_root;
Al Viroe3489f82018-12-13 00:24:36 -05001084 struct inode_security_struct *isec = backing_inode_security(root);
1085 seq_putc(m, ',');
1086 seq_puts(m, ROOTCONTEXT_STR);
1087 rc = show_sid(m, isec->sid);
1088 if (rc)
1089 return rc;
1090 }
1091 if (sbsec->flags & SBLABEL_MNT) {
1092 seq_putc(m, ',');
David Howells442155c2018-11-01 23:07:24 +00001093 seq_puts(m, SECLABEL_STR);
Al Viroe3489f82018-12-13 00:24:36 -05001094 }
1095 return 0;
Eric Paris2069f452008-07-04 09:47:13 +10001096}
1097
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098static inline u16 inode_mode_to_security_class(umode_t mode)
1099{
1100 switch (mode & S_IFMT) {
1101 case S_IFSOCK:
1102 return SECCLASS_SOCK_FILE;
1103 case S_IFLNK:
1104 return SECCLASS_LNK_FILE;
1105 case S_IFREG:
1106 return SECCLASS_FILE;
1107 case S_IFBLK:
1108 return SECCLASS_BLK_FILE;
1109 case S_IFDIR:
1110 return SECCLASS_DIR;
1111 case S_IFCHR:
1112 return SECCLASS_CHR_FILE;
1113 case S_IFIFO:
1114 return SECCLASS_FIFO_FILE;
1115
1116 }
1117
1118 return SECCLASS_FILE;
1119}
1120
James Morris13402582005-09-30 14:24:34 -04001121static inline int default_protocol_stream(int protocol)
1122{
Paolo Abeni95ca9072020-12-16 12:55:27 +01001123 return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP ||
1124 protocol == IPPROTO_MPTCP);
James Morris13402582005-09-30 14:24:34 -04001125}
1126
1127static inline int default_protocol_dgram(int protocol)
1128{
1129 return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
1130}
1131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132static inline u16 socket_type_to_security_class(int family, int type, int protocol)
1133{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001134 int extsockclass = selinux_policycap_extsockclass();
Stephen Smalleyda69a532017-01-09 10:07:30 -05001135
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 switch (family) {
1137 case PF_UNIX:
1138 switch (type) {
1139 case SOCK_STREAM:
1140 case SOCK_SEQPACKET:
1141 return SECCLASS_UNIX_STREAM_SOCKET;
1142 case SOCK_DGRAM:
Luis Ressel2a764b52017-07-25 15:13:41 -04001143 case SOCK_RAW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 return SECCLASS_UNIX_DGRAM_SOCKET;
1145 }
1146 break;
1147 case PF_INET:
1148 case PF_INET6:
1149 switch (type) {
1150 case SOCK_STREAM:
Stephen Smalleyda69a532017-01-09 10:07:30 -05001151 case SOCK_SEQPACKET:
James Morris13402582005-09-30 14:24:34 -04001152 if (default_protocol_stream(protocol))
1153 return SECCLASS_TCP_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001154 else if (extsockclass && protocol == IPPROTO_SCTP)
1155 return SECCLASS_SCTP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001156 else
1157 return SECCLASS_RAWIP_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 case SOCK_DGRAM:
James Morris13402582005-09-30 14:24:34 -04001159 if (default_protocol_dgram(protocol))
1160 return SECCLASS_UDP_SOCKET;
Stephen Smalleyef379792017-01-09 10:07:31 -05001161 else if (extsockclass && (protocol == IPPROTO_ICMP ||
1162 protocol == IPPROTO_ICMPV6))
Stephen Smalleyda69a532017-01-09 10:07:30 -05001163 return SECCLASS_ICMP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001164 else
1165 return SECCLASS_RAWIP_SOCKET;
James Morris2ee92d42006-11-13 16:09:01 -08001166 case SOCK_DCCP:
1167 return SECCLASS_DCCP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001168 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 return SECCLASS_RAWIP_SOCKET;
1170 }
1171 break;
1172 case PF_NETLINK:
1173 switch (protocol) {
1174 case NETLINK_ROUTE:
1175 return SECCLASS_NETLINK_ROUTE_SOCKET;
Pavel Emelyanov7f1fb602011-12-06 07:56:43 +00001176 case NETLINK_SOCK_DIAG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 return SECCLASS_NETLINK_TCPDIAG_SOCKET;
1178 case NETLINK_NFLOG:
1179 return SECCLASS_NETLINK_NFLOG_SOCKET;
1180 case NETLINK_XFRM:
1181 return SECCLASS_NETLINK_XFRM_SOCKET;
1182 case NETLINK_SELINUX:
1183 return SECCLASS_NETLINK_SELINUX_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001184 case NETLINK_ISCSI:
1185 return SECCLASS_NETLINK_ISCSI_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 case NETLINK_AUDIT:
1187 return SECCLASS_NETLINK_AUDIT_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001188 case NETLINK_FIB_LOOKUP:
1189 return SECCLASS_NETLINK_FIB_LOOKUP_SOCKET;
1190 case NETLINK_CONNECTOR:
1191 return SECCLASS_NETLINK_CONNECTOR_SOCKET;
1192 case NETLINK_NETFILTER:
1193 return SECCLASS_NETLINK_NETFILTER_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 case NETLINK_DNRTMSG:
1195 return SECCLASS_NETLINK_DNRT_SOCKET;
James Morris0c9b7942005-04-16 15:24:13 -07001196 case NETLINK_KOBJECT_UEVENT:
1197 return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001198 case NETLINK_GENERIC:
1199 return SECCLASS_NETLINK_GENERIC_SOCKET;
1200 case NETLINK_SCSITRANSPORT:
1201 return SECCLASS_NETLINK_SCSITRANSPORT_SOCKET;
1202 case NETLINK_RDMA:
1203 return SECCLASS_NETLINK_RDMA_SOCKET;
1204 case NETLINK_CRYPTO:
1205 return SECCLASS_NETLINK_CRYPTO_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 default:
1207 return SECCLASS_NETLINK_SOCKET;
1208 }
1209 case PF_PACKET:
1210 return SECCLASS_PACKET_SOCKET;
1211 case PF_KEY:
1212 return SECCLASS_KEY_SOCKET;
Christopher J. PeBenito3e3ff152006-06-09 00:25:03 -07001213 case PF_APPLETALK:
1214 return SECCLASS_APPLETALK_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 }
1216
Stephen Smalleyda69a532017-01-09 10:07:30 -05001217 if (extsockclass) {
1218 switch (family) {
1219 case PF_AX25:
1220 return SECCLASS_AX25_SOCKET;
1221 case PF_IPX:
1222 return SECCLASS_IPX_SOCKET;
1223 case PF_NETROM:
1224 return SECCLASS_NETROM_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001225 case PF_ATMPVC:
1226 return SECCLASS_ATMPVC_SOCKET;
1227 case PF_X25:
1228 return SECCLASS_X25_SOCKET;
1229 case PF_ROSE:
1230 return SECCLASS_ROSE_SOCKET;
1231 case PF_DECnet:
1232 return SECCLASS_DECNET_SOCKET;
1233 case PF_ATMSVC:
1234 return SECCLASS_ATMSVC_SOCKET;
1235 case PF_RDS:
1236 return SECCLASS_RDS_SOCKET;
1237 case PF_IRDA:
1238 return SECCLASS_IRDA_SOCKET;
1239 case PF_PPPOX:
1240 return SECCLASS_PPPOX_SOCKET;
1241 case PF_LLC:
1242 return SECCLASS_LLC_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001243 case PF_CAN:
1244 return SECCLASS_CAN_SOCKET;
1245 case PF_TIPC:
1246 return SECCLASS_TIPC_SOCKET;
1247 case PF_BLUETOOTH:
1248 return SECCLASS_BLUETOOTH_SOCKET;
1249 case PF_IUCV:
1250 return SECCLASS_IUCV_SOCKET;
1251 case PF_RXRPC:
1252 return SECCLASS_RXRPC_SOCKET;
1253 case PF_ISDN:
1254 return SECCLASS_ISDN_SOCKET;
1255 case PF_PHONET:
1256 return SECCLASS_PHONET_SOCKET;
1257 case PF_IEEE802154:
1258 return SECCLASS_IEEE802154_SOCKET;
1259 case PF_CAIF:
1260 return SECCLASS_CAIF_SOCKET;
1261 case PF_ALG:
1262 return SECCLASS_ALG_SOCKET;
1263 case PF_NFC:
1264 return SECCLASS_NFC_SOCKET;
1265 case PF_VSOCK:
1266 return SECCLASS_VSOCK_SOCKET;
1267 case PF_KCM:
1268 return SECCLASS_KCM_SOCKET;
1269 case PF_QIPCRTR:
1270 return SECCLASS_QIPCRTR_SOCKET;
Linus Torvalds3051bf32017-02-22 10:15:09 -08001271 case PF_SMC:
1272 return SECCLASS_SMC_SOCKET;
Björn Töpel68e8b842018-05-02 13:01:22 +02001273 case PF_XDP:
1274 return SECCLASS_XDP_SOCKET;
1275#if PF_MAX > 45
Stephen Smalleyda69a532017-01-09 10:07:30 -05001276#error New address family defined, please update this function.
1277#endif
1278 }
1279 }
1280
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 return SECCLASS_SOCKET;
1282}
1283
Stephen Smalley134509d2015-06-04 16:22:17 -04001284static int selinux_genfs_get_sid(struct dentry *dentry,
1285 u16 tclass,
1286 u16 flags,
1287 u32 *sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288{
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001289 int rc;
Al Virofc640052016-04-10 01:33:30 -04001290 struct super_block *sb = dentry->d_sb;
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001291 char *buffer, *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Eric Paris828dfe12008-04-17 13:17:49 -04001293 buffer = (char *)__get_free_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 if (!buffer)
1295 return -ENOMEM;
1296
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001297 path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
1298 if (IS_ERR(path))
1299 rc = PTR_ERR(path);
1300 else {
Stephen Smalley134509d2015-06-04 16:22:17 -04001301 if (flags & SE_SBPROC) {
1302 /* each process gets a /proc/PID/ entry. Strip off the
1303 * PID part to get a valid selinux labeling.
1304 * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
1305 while (path[1] >= '0' && path[1] <= '9') {
1306 path[1] = '/';
1307 path++;
1308 }
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001309 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001310 rc = security_genfs_sid(&selinux_state, sb->s_type->name,
1311 path, tclass, sid);
Stephen Smalley7bb185e2018-09-04 16:51:36 -04001312 if (rc == -ENOENT) {
1313 /* No match in policy, mark as unlabeled. */
1314 *sid = SECINITSID_UNLABELED;
1315 rc = 0;
1316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 free_page((unsigned long)buffer);
1319 return rc;
1320}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
Ondrej Mosnacekb7540262019-02-22 15:57:14 +01001322static int inode_doinit_use_xattr(struct inode *inode, struct dentry *dentry,
1323 u32 def_sid, u32 *sid)
1324{
1325#define INITCONTEXTLEN 255
1326 char *context;
1327 unsigned int len;
1328 int rc;
1329
1330 len = INITCONTEXTLEN;
1331 context = kmalloc(len + 1, GFP_NOFS);
1332 if (!context)
1333 return -ENOMEM;
1334
1335 context[len] = '\0';
1336 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
1337 if (rc == -ERANGE) {
1338 kfree(context);
1339
1340 /* Need a larger buffer. Query for the right size. */
1341 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, NULL, 0);
1342 if (rc < 0)
1343 return rc;
1344
1345 len = rc;
1346 context = kmalloc(len + 1, GFP_NOFS);
1347 if (!context)
1348 return -ENOMEM;
1349
1350 context[len] = '\0';
1351 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX,
1352 context, len);
1353 }
1354 if (rc < 0) {
1355 kfree(context);
1356 if (rc != -ENODATA) {
1357 pr_warn("SELinux: %s: getxattr returned %d for dev=%s ino=%ld\n",
1358 __func__, -rc, inode->i_sb->s_id, inode->i_ino);
1359 return rc;
1360 }
1361 *sid = def_sid;
1362 return 0;
1363 }
1364
1365 rc = security_context_to_sid_default(&selinux_state, context, rc, sid,
1366 def_sid, GFP_NOFS);
1367 if (rc) {
1368 char *dev = inode->i_sb->s_id;
1369 unsigned long ino = inode->i_ino;
1370
1371 if (rc == -EINVAL) {
1372 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",
1373 ino, dev, context);
1374 } else {
1375 pr_warn("SELinux: %s: context_to_sid(%s) returned %d for dev=%s ino=%ld\n",
1376 __func__, context, -rc, dev, ino);
1377 }
1378 }
1379 kfree(context);
1380 return 0;
1381}
1382
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383/* The inode's security attributes must be initialized before first use. */
1384static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
1385{
1386 struct superblock_security_struct *sbsec = NULL;
Casey Schaufler80788c22018-09-21 17:19:11 -07001387 struct inode_security_struct *isec = selinux_inode(inode);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001388 u32 task_sid, sid = 0;
1389 u16 sclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05001393 if (isec->initialized == LABEL_INITIALIZED)
Andreas Gruenbacher13457d02016-11-10 22:18:29 +01001394 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001396 spin_lock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05001397 if (isec->initialized == LABEL_INITIALIZED)
Eric Paris23970742006-09-25 23:32:01 -07001398 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Andreas Gruenbacher13457d02016-11-10 22:18:29 +01001400 if (isec->sclass == SECCLASS_FILE)
1401 isec->sclass = inode_mode_to_security_class(inode->i_mode);
1402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 sbsec = inode->i_sb->s_security;
David P. Quigley0d90a7e2009-01-16 09:22:02 -05001404 if (!(sbsec->flags & SE_SBINITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 /* Defer initialization until selinux_complete_init,
1406 after the initial policy is loaded and the security
1407 server is ready to handle calls. */
1408 spin_lock(&sbsec->isec_lock);
1409 if (list_empty(&isec->list))
1410 list_add(&isec->list, &sbsec->isec_head);
1411 spin_unlock(&sbsec->isec_lock);
Eric Paris23970742006-09-25 23:32:01 -07001412 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 }
1414
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001415 sclass = isec->sclass;
1416 task_sid = isec->task_sid;
1417 sid = isec->sid;
1418 isec->initialized = LABEL_PENDING;
1419 spin_unlock(&isec->lock);
1420
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 switch (sbsec->behavior) {
David Quigleyeb9ae682013-05-22 12:50:37 -04001422 case SECURITY_FS_USE_NATIVE:
1423 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 case SECURITY_FS_USE_XATTR:
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001425 if (!(inode->i_opflags & IOP_XATTR)) {
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001426 sid = sbsec->def_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 break;
1428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 /* Need a dentry, since the xattr API requires one.
1430 Life would be simpler if we could just pass the inode. */
1431 if (opt_dentry) {
1432 /* Called from d_instantiate or d_splice_alias. */
1433 dentry = dget(opt_dentry);
1434 } else {
Al Virob1271252018-04-25 10:28:38 -04001435 /*
1436 * Called from selinux_complete_init, try to find a dentry.
1437 * Some filesystems really want a connected one, so try
1438 * that first. We could split SECURITY_FS_USE_XATTR in
1439 * two, depending upon that...
1440 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 dentry = d_find_alias(inode);
Al Virob1271252018-04-25 10:28:38 -04001442 if (!dentry)
1443 dentry = d_find_any_alias(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 }
1445 if (!dentry) {
Eric Parisdf7f54c2009-03-09 14:35:58 -04001446 /*
1447 * this is can be hit on boot when a file is accessed
1448 * before the policy is loaded. When we load policy we
1449 * may find inodes that have no dentry on the
1450 * sbsec->isec_head list. No reason to complain as these
1451 * will get fixed up the next time we go through
1452 * inode_doinit with a dentry, before these inodes could
1453 * be used again by userspace.
1454 */
Paul Moore200ea5a2020-11-03 11:49:38 -05001455 goto out_invalid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 }
1457
Ondrej Mosnacekb7540262019-02-22 15:57:14 +01001458 rc = inode_doinit_use_xattr(inode, dentry, sbsec->def_sid,
1459 &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 dput(dentry);
Ondrej Mosnacekb7540262019-02-22 15:57:14 +01001461 if (rc)
1462 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 break;
1464 case SECURITY_FS_USE_TASK:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001465 sid = task_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 break;
1467 case SECURITY_FS_USE_TRANS:
1468 /* Default to the fs SID. */
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001469 sid = sbsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
1471 /* Try to obtain a transition SID. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001472 rc = security_transition_sid(&selinux_state, task_sid, sid,
1473 sclass, NULL, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 if (rc)
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001475 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 break;
Eric Parisc312feb2006-07-10 04:43:53 -07001477 case SECURITY_FS_USE_MNTPOINT:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001478 sid = sbsec->mntpoint_sid;
Eric Parisc312feb2006-07-10 04:43:53 -07001479 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 default:
Eric Parisc312feb2006-07-10 04:43:53 -07001481 /* Default to the fs superblock SID. */
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001482 sid = sbsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
Christian Göttsche7470d0d12020-01-28 20:16:48 +01001484 if ((sbsec->flags & SE_SBGENFS) &&
1485 (!S_ISLNK(inode->i_mode) ||
1486 selinux_policycap_genfs_seclabel_symlinks())) {
Paul Mooref64410e2014-03-19 16:46:18 -04001487 /* We must have a dentry to determine the label on
1488 * procfs inodes */
Al Virob1271252018-04-25 10:28:38 -04001489 if (opt_dentry) {
Paul Mooref64410e2014-03-19 16:46:18 -04001490 /* Called from d_instantiate or
1491 * d_splice_alias. */
1492 dentry = dget(opt_dentry);
Al Virob1271252018-04-25 10:28:38 -04001493 } else {
Paul Mooref64410e2014-03-19 16:46:18 -04001494 /* Called from selinux_complete_init, try to
Al Virob1271252018-04-25 10:28:38 -04001495 * find a dentry. Some filesystems really want
1496 * a connected one, so try that first.
1497 */
Paul Mooref64410e2014-03-19 16:46:18 -04001498 dentry = d_find_alias(inode);
Al Virob1271252018-04-25 10:28:38 -04001499 if (!dentry)
1500 dentry = d_find_any_alias(inode);
1501 }
Paul Mooref64410e2014-03-19 16:46:18 -04001502 /*
1503 * This can be hit on boot when a file is accessed
1504 * before the policy is loaded. When we load policy we
1505 * may find inodes that have no dentry on the
1506 * sbsec->isec_head list. No reason to complain as
1507 * these will get fixed up the next time we go through
1508 * inode_doinit() with a dentry, before these inodes
1509 * could be used again by userspace.
1510 */
1511 if (!dentry)
Paul Moore200ea5a2020-11-03 11:49:38 -05001512 goto out_invalid;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001513 rc = selinux_genfs_get_sid(dentry, sclass,
Stephen Smalley134509d2015-06-04 16:22:17 -04001514 sbsec->flags, &sid);
Ondrej Mosnacekb7540262019-02-22 15:57:14 +01001515 if (rc) {
1516 dput(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001517 goto out;
Ondrej Mosnacekb7540262019-02-22 15:57:14 +01001518 }
1519
1520 if ((sbsec->flags & SE_SBGENFS_XATTR) &&
1521 (inode->i_opflags & IOP_XATTR)) {
1522 rc = inode_doinit_use_xattr(inode, dentry,
1523 sid, &sid);
1524 if (rc) {
1525 dput(dentry);
1526 goto out;
1527 }
1528 }
1529 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 }
1531 break;
1532 }
1533
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001534out:
1535 spin_lock(&isec->lock);
1536 if (isec->initialized == LABEL_PENDING) {
Paul Moore200ea5a2020-11-03 11:49:38 -05001537 if (rc) {
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001538 isec->initialized = LABEL_INVALID;
1539 goto out_unlock;
1540 }
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001541 isec->initialized = LABEL_INITIALIZED;
1542 isec->sid = sid;
1543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
Eric Paris23970742006-09-25 23:32:01 -07001545out_unlock:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001546 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 return rc;
Paul Moore200ea5a2020-11-03 11:49:38 -05001548
1549out_invalid:
1550 spin_lock(&isec->lock);
1551 if (isec->initialized == LABEL_PENDING) {
1552 isec->initialized = LABEL_INVALID;
1553 isec->sid = sid;
1554 }
1555 spin_unlock(&isec->lock);
1556 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557}
1558
1559/* Convert a Linux signal to an access vector. */
1560static inline u32 signal_to_av(int sig)
1561{
1562 u32 perm = 0;
1563
1564 switch (sig) {
1565 case SIGCHLD:
1566 /* Commonly granted from child to parent. */
1567 perm = PROCESS__SIGCHLD;
1568 break;
1569 case SIGKILL:
1570 /* Cannot be caught or ignored */
1571 perm = PROCESS__SIGKILL;
1572 break;
1573 case SIGSTOP:
1574 /* Cannot be caught or ignored */
1575 perm = PROCESS__SIGSTOP;
1576 break;
1577 default:
1578 /* All other signals. */
1579 perm = PROCESS__SIGNAL;
1580 break;
1581 }
1582
1583 return perm;
1584}
1585
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001586#if CAP_LAST_CAP > 63
1587#error Fix SELinux to handle capabilities > 63.
1588#endif
1589
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590/* Check whether a task is allowed to use a capability. */
Eric Paris6a9de492012-01-03 12:25:14 -05001591static int cred_has_capability(const struct cred *cred,
Micah Mortonc1a85a02019-01-07 16:10:53 -08001592 int cap, unsigned int opts, bool initns)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593{
Thomas Liu2bf49692009-07-14 12:14:09 -04001594 struct common_audit_data ad;
Eric Paris06112162008-11-11 22:02:50 +11001595 struct av_decision avd;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001596 u16 sclass;
David Howells3699c532009-01-06 22:27:01 +00001597 u32 sid = cred_sid(cred);
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001598 u32 av = CAP_TO_MASK(cap);
Eric Paris06112162008-11-11 22:02:50 +11001599 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
Eric Paris50c205f2012-04-04 15:01:43 -04001601 ad.type = LSM_AUDIT_DATA_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 ad.u.cap = cap;
1603
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001604 switch (CAP_TO_INDEX(cap)) {
1605 case 0:
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04001606 sclass = initns ? SECCLASS_CAPABILITY : SECCLASS_CAP_USERNS;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001607 break;
1608 case 1:
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04001609 sclass = initns ? SECCLASS_CAPABILITY2 : SECCLASS_CAP2_USERNS;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001610 break;
1611 default:
peter enderborgc103a912018-06-12 10:09:03 +02001612 pr_err("SELinux: out of range capability %d\n", cap);
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001613 BUG();
Eric Parisa35c6c832011-04-20 10:21:28 -04001614 return -EINVAL;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001615 }
Eric Paris06112162008-11-11 22:02:50 +11001616
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001617 rc = avc_has_perm_noaudit(&selinux_state,
1618 sid, sid, sclass, av, 0, &avd);
Micah Mortonc1a85a02019-01-07 16:10:53 -08001619 if (!(opts & CAP_OPT_NOAUDIT)) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001620 int rc2 = avc_audit(&selinux_state,
1621 sid, sid, sclass, av, &avd, rc, &ad, 0);
Eric Paris9ade0cf2011-04-25 16:26:29 -04001622 if (rc2)
1623 return rc2;
1624 }
Eric Paris06112162008-11-11 22:02:50 +11001625 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626}
1627
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628/* Check whether a task has a particular permission to an inode.
1629 The 'adp' parameter is optional and allows other audit
1630 data to be passed (e.g. the dentry). */
David Howells88e67f32008-11-14 10:39:21 +11001631static int inode_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 struct inode *inode,
1633 u32 perms,
Linus Torvalds19e49832013-10-04 12:54:11 -07001634 struct common_audit_data *adp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 struct inode_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +11001637 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
David Howellse0e81732009-09-02 09:13:40 +01001639 validate_creds(cred);
1640
Eric Paris828dfe12008-04-17 13:17:49 -04001641 if (unlikely(IS_PRIVATE(inode)))
Stephen Smalleybbaca6c2007-02-14 00:34:16 -08001642 return 0;
1643
David Howells88e67f32008-11-14 10:39:21 +11001644 sid = cred_sid(cred);
Casey Schaufler80788c22018-09-21 17:19:11 -07001645 isec = selinux_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001647 return avc_has_perm(&selinux_state,
1648 sid, isec->sid, isec->sclass, perms, adp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649}
1650
1651/* Same as inode_has_perm, but pass explicit audit data containing
1652 the dentry to help the auditing code to more easily generate the
1653 pathname if needed. */
David Howells88e67f32008-11-14 10:39:21 +11001654static inline int dentry_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 struct dentry *dentry,
1656 u32 av)
1657{
David Howellsc6f493d2015-03-17 22:26:22 +00001658 struct inode *inode = d_backing_inode(dentry);
Thomas Liu2bf49692009-07-14 12:14:09 -04001659 struct common_audit_data ad;
David Howells88e67f32008-11-14 10:39:21 +11001660
Eric Paris50c205f2012-04-04 15:01:43 -04001661 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Paris2875fa02011-04-28 16:04:24 -04001662 ad.u.dentry = dentry;
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05001663 __inode_security_revalidate(inode, dentry, true);
Linus Torvalds19e49832013-10-04 12:54:11 -07001664 return inode_has_perm(cred, inode, av, &ad);
Eric Paris2875fa02011-04-28 16:04:24 -04001665}
1666
1667/* Same as inode_has_perm, but pass explicit audit data containing
1668 the path to help the auditing code to more easily generate the
1669 pathname if needed. */
1670static inline int path_has_perm(const struct cred *cred,
Al Viro3f7036a2015-03-08 19:28:30 -04001671 const struct path *path,
Eric Paris2875fa02011-04-28 16:04:24 -04001672 u32 av)
1673{
David Howellsc6f493d2015-03-17 22:26:22 +00001674 struct inode *inode = d_backing_inode(path->dentry);
Eric Paris2875fa02011-04-28 16:04:24 -04001675 struct common_audit_data ad;
1676
Eric Paris50c205f2012-04-04 15:01:43 -04001677 ad.type = LSM_AUDIT_DATA_PATH;
Eric Paris2875fa02011-04-28 16:04:24 -04001678 ad.u.path = *path;
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05001679 __inode_security_revalidate(inode, path->dentry, true);
Linus Torvalds19e49832013-10-04 12:54:11 -07001680 return inode_has_perm(cred, inode, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681}
1682
David Howells13f8e982013-06-13 23:37:55 +01001683/* Same as path_has_perm, but uses the inode from the file struct. */
1684static inline int file_path_has_perm(const struct cred *cred,
1685 struct file *file,
1686 u32 av)
1687{
1688 struct common_audit_data ad;
1689
Vivek Goyal43af5de2016-09-09 11:37:49 -04001690 ad.type = LSM_AUDIT_DATA_FILE;
1691 ad.u.file = file;
Linus Torvalds19e49832013-10-04 12:54:11 -07001692 return inode_has_perm(cred, file_inode(file), av, &ad);
David Howells13f8e982013-06-13 23:37:55 +01001693}
1694
Chenbo Fengf66e4482017-10-18 13:00:26 -07001695#ifdef CONFIG_BPF_SYSCALL
1696static int bpf_fd_pass(struct file *file, u32 sid);
1697#endif
1698
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699/* Check whether a task can use an open file descriptor to
1700 access an inode in a given way. Check access to the
1701 descriptor itself, and then use dentry_has_perm to
1702 check a particular permission to the file.
1703 Access to the descriptor is implicitly granted if it
1704 has the same SID as the process. If av is zero, then
1705 access to the file is not checked, e.g. for cases
1706 where only the descriptor is affected like seek. */
David Howells88e67f32008-11-14 10:39:21 +11001707static int file_has_perm(const struct cred *cred,
1708 struct file *file,
1709 u32 av)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710{
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07001711 struct file_security_struct *fsec = selinux_file(file);
Al Viro496ad9a2013-01-23 17:07:38 -05001712 struct inode *inode = file_inode(file);
Thomas Liu2bf49692009-07-14 12:14:09 -04001713 struct common_audit_data ad;
David Howells88e67f32008-11-14 10:39:21 +11001714 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 int rc;
1716
Vivek Goyal43af5de2016-09-09 11:37:49 -04001717 ad.type = LSM_AUDIT_DATA_FILE;
1718 ad.u.file = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
David Howells275bb412008-11-14 10:39:19 +11001720 if (sid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001721 rc = avc_has_perm(&selinux_state,
1722 sid, fsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 SECCLASS_FD,
1724 FD__USE,
1725 &ad);
1726 if (rc)
David Howells88e67f32008-11-14 10:39:21 +11001727 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 }
1729
Chenbo Fengf66e4482017-10-18 13:00:26 -07001730#ifdef CONFIG_BPF_SYSCALL
1731 rc = bpf_fd_pass(file, cred_sid(cred));
1732 if (rc)
1733 return rc;
1734#endif
1735
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 /* av is zero if only checking access to the descriptor. */
David Howells88e67f32008-11-14 10:39:21 +11001737 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 if (av)
Linus Torvalds19e49832013-10-04 12:54:11 -07001739 rc = inode_has_perm(cred, inode, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
David Howells88e67f32008-11-14 10:39:21 +11001741out:
1742 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743}
1744
David Howellsc3c188b2015-07-10 17:19:58 -04001745/*
1746 * Determine the label for an inode that might be unioned.
1747 */
Vivek Goyalc957f6d2016-07-13 10:44:51 -04001748static int
1749selinux_determine_inode_label(const struct task_security_struct *tsec,
1750 struct inode *dir,
1751 const struct qstr *name, u16 tclass,
1752 u32 *_new_isid)
David Howellsc3c188b2015-07-10 17:19:58 -04001753{
1754 const struct superblock_security_struct *sbsec = dir->i_sb->s_security;
David Howellsc3c188b2015-07-10 17:19:58 -04001755
1756 if ((sbsec->flags & SE_SBINITIALIZED) &&
1757 (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)) {
1758 *_new_isid = sbsec->mntpoint_sid;
1759 } else if ((sbsec->flags & SBLABEL_MNT) &&
1760 tsec->create_sid) {
1761 *_new_isid = tsec->create_sid;
1762 } else {
Paul Moore20cdef82016-04-04 14:14:42 -04001763 const struct inode_security_struct *dsec = inode_security(dir);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001764 return security_transition_sid(&selinux_state, tsec->sid,
1765 dsec->sid, tclass,
David Howellsc3c188b2015-07-10 17:19:58 -04001766 name, _new_isid);
1767 }
1768
1769 return 0;
1770}
1771
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772/* Check whether a task can create a file. */
1773static int may_create(struct inode *dir,
1774 struct dentry *dentry,
1775 u16 tclass)
1776{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07001777 const struct task_security_struct *tsec = selinux_cred(current_cred());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 struct inode_security_struct *dsec;
1779 struct superblock_security_struct *sbsec;
David Howells275bb412008-11-14 10:39:19 +11001780 u32 sid, newsid;
Thomas Liu2bf49692009-07-14 12:14:09 -04001781 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 int rc;
1783
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001784 dsec = inode_security(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 sbsec = dir->i_sb->s_security;
1786
David Howells275bb412008-11-14 10:39:19 +11001787 sid = tsec->sid;
David Howells275bb412008-11-14 10:39:19 +11001788
Eric Paris50c205f2012-04-04 15:01:43 -04001789 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04001790 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001792 rc = avc_has_perm(&selinux_state,
1793 sid, dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 DIR__ADD_NAME | DIR__SEARCH,
1795 &ad);
1796 if (rc)
1797 return rc;
1798
Yang Guo210a2922019-12-12 10:02:24 +08001799 rc = selinux_determine_inode_label(tsec, dir, &dentry->d_name, tclass,
1800 &newsid);
David Howellsc3c188b2015-07-10 17:19:58 -04001801 if (rc)
1802 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001804 rc = avc_has_perm(&selinux_state,
1805 sid, newsid, tclass, FILE__CREATE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 if (rc)
1807 return rc;
1808
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001809 return avc_has_perm(&selinux_state,
1810 newsid, sbsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 SECCLASS_FILESYSTEM,
1812 FILESYSTEM__ASSOCIATE, &ad);
1813}
1814
Eric Paris828dfe12008-04-17 13:17:49 -04001815#define MAY_LINK 0
1816#define MAY_UNLINK 1
1817#define MAY_RMDIR 2
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
1819/* Check whether a task can link, unlink, or rmdir a file/directory. */
1820static int may_link(struct inode *dir,
1821 struct dentry *dentry,
1822 int kind)
1823
1824{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 struct inode_security_struct *dsec, *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001826 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11001827 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 u32 av;
1829 int rc;
1830
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001831 dsec = inode_security(dir);
1832 isec = backing_inode_security(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
Eric Paris50c205f2012-04-04 15:01:43 -04001834 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04001835 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
1837 av = DIR__SEARCH;
1838 av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001839 rc = avc_has_perm(&selinux_state,
1840 sid, dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 if (rc)
1842 return rc;
1843
1844 switch (kind) {
1845 case MAY_LINK:
1846 av = FILE__LINK;
1847 break;
1848 case MAY_UNLINK:
1849 av = FILE__UNLINK;
1850 break;
1851 case MAY_RMDIR:
1852 av = DIR__RMDIR;
1853 break;
1854 default:
peter enderborgc103a912018-06-12 10:09:03 +02001855 pr_warn("SELinux: %s: unrecognized kind %d\n",
Eric Paris744ba352008-04-17 11:52:44 -04001856 __func__, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 return 0;
1858 }
1859
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001860 rc = avc_has_perm(&selinux_state,
1861 sid, isec->sid, isec->sclass, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 return rc;
1863}
1864
1865static inline int may_rename(struct inode *old_dir,
1866 struct dentry *old_dentry,
1867 struct inode *new_dir,
1868 struct dentry *new_dentry)
1869{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001871 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11001872 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 u32 av;
1874 int old_is_dir, new_is_dir;
1875 int rc;
1876
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001877 old_dsec = inode_security(old_dir);
1878 old_isec = backing_inode_security(old_dentry);
David Howellse36cb0b2015-01-29 12:02:35 +00001879 old_is_dir = d_is_dir(old_dentry);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001880 new_dsec = inode_security(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Eric Paris50c205f2012-04-04 15:01:43 -04001882 ad.type = LSM_AUDIT_DATA_DENTRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
Eric Parisa2694342011-04-25 13:10:27 -04001884 ad.u.dentry = old_dentry;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001885 rc = avc_has_perm(&selinux_state,
1886 sid, old_dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 DIR__REMOVE_NAME | DIR__SEARCH, &ad);
1888 if (rc)
1889 return rc;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001890 rc = avc_has_perm(&selinux_state,
1891 sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 old_isec->sclass, FILE__RENAME, &ad);
1893 if (rc)
1894 return rc;
1895 if (old_is_dir && new_dir != old_dir) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001896 rc = avc_has_perm(&selinux_state,
1897 sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 old_isec->sclass, DIR__REPARENT, &ad);
1899 if (rc)
1900 return rc;
1901 }
1902
Eric Parisa2694342011-04-25 13:10:27 -04001903 ad.u.dentry = new_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 av = DIR__ADD_NAME | DIR__SEARCH;
David Howells2c616d42015-01-29 12:02:33 +00001905 if (d_is_positive(new_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 av |= DIR__REMOVE_NAME;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001907 rc = avc_has_perm(&selinux_state,
1908 sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 if (rc)
1910 return rc;
David Howells2c616d42015-01-29 12:02:33 +00001911 if (d_is_positive(new_dentry)) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001912 new_isec = backing_inode_security(new_dentry);
David Howellse36cb0b2015-01-29 12:02:35 +00001913 new_is_dir = d_is_dir(new_dentry);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001914 rc = avc_has_perm(&selinux_state,
1915 sid, new_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 new_isec->sclass,
1917 (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
1918 if (rc)
1919 return rc;
1920 }
1921
1922 return 0;
1923}
1924
1925/* Check whether a task can perform a filesystem operation. */
David Howells88e67f32008-11-14 10:39:21 +11001926static int superblock_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 struct super_block *sb,
1928 u32 perms,
Thomas Liu2bf49692009-07-14 12:14:09 -04001929 struct common_audit_data *ad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 struct superblock_security_struct *sbsec;
David Howells88e67f32008-11-14 10:39:21 +11001932 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 sbsec = sb->s_security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001935 return avc_has_perm(&selinux_state,
1936 sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937}
1938
1939/* Convert a Linux mode and permission mask to an access vector. */
1940static inline u32 file_mask_to_av(int mode, int mask)
1941{
1942 u32 av = 0;
1943
Al Virodba19c62011-07-25 20:49:29 -04001944 if (!S_ISDIR(mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 if (mask & MAY_EXEC)
1946 av |= FILE__EXECUTE;
1947 if (mask & MAY_READ)
1948 av |= FILE__READ;
1949
1950 if (mask & MAY_APPEND)
1951 av |= FILE__APPEND;
1952 else if (mask & MAY_WRITE)
1953 av |= FILE__WRITE;
1954
1955 } else {
1956 if (mask & MAY_EXEC)
1957 av |= DIR__SEARCH;
1958 if (mask & MAY_WRITE)
1959 av |= DIR__WRITE;
1960 if (mask & MAY_READ)
1961 av |= DIR__READ;
1962 }
1963
1964 return av;
1965}
1966
1967/* Convert a Linux file to an access vector. */
1968static inline u32 file_to_av(struct file *file)
1969{
1970 u32 av = 0;
1971
1972 if (file->f_mode & FMODE_READ)
1973 av |= FILE__READ;
1974 if (file->f_mode & FMODE_WRITE) {
1975 if (file->f_flags & O_APPEND)
1976 av |= FILE__APPEND;
1977 else
1978 av |= FILE__WRITE;
1979 }
Stephen Smalley0794c662008-03-17 08:55:18 -04001980 if (!av) {
1981 /*
1982 * Special file opened with flags 3 for ioctl-only use.
1983 */
1984 av = FILE__IOCTL;
1985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
1987 return av;
1988}
1989
Eric Paris8b6a5a32008-10-29 17:06:46 -04001990/*
Randy Dunlapc76a2f92020-08-07 09:51:34 -07001991 * Convert a file to an access vector and include the correct
Eric Paris8b6a5a32008-10-29 17:06:46 -04001992 * open permission.
1993 */
1994static inline u32 open_file_to_av(struct file *file)
1995{
1996 u32 av = file_to_av(file);
Stephen Smalleyccb54472017-05-12 12:41:24 -04001997 struct inode *inode = file_inode(file);
Eric Paris8b6a5a32008-10-29 17:06:46 -04001998
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001999 if (selinux_policycap_openperm() &&
2000 inode->i_sb->s_magic != SOCKFS_MAGIC)
Eric Paris49b7b8d2010-07-23 11:44:09 -04002001 av |= FILE__OPEN;
2002
Eric Paris8b6a5a32008-10-29 17:06:46 -04002003 return av;
2004}
2005
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006/* Hook functions begin here. */
2007
Stephen Smalley79af7302015-01-21 10:54:10 -05002008static int selinux_binder_set_context_mgr(struct task_struct *mgr)
2009{
2010 u32 mysid = current_sid();
2011 u32 mgrsid = task_sid(mgr);
2012
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002013 return avc_has_perm(&selinux_state,
2014 mysid, mgrsid, SECCLASS_BINDER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002015 BINDER__SET_CONTEXT_MGR, NULL);
2016}
2017
2018static int selinux_binder_transaction(struct task_struct *from,
2019 struct task_struct *to)
2020{
2021 u32 mysid = current_sid();
2022 u32 fromsid = task_sid(from);
2023 u32 tosid = task_sid(to);
2024 int rc;
2025
2026 if (mysid != fromsid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002027 rc = avc_has_perm(&selinux_state,
2028 mysid, fromsid, SECCLASS_BINDER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002029 BINDER__IMPERSONATE, NULL);
2030 if (rc)
2031 return rc;
2032 }
2033
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002034 return avc_has_perm(&selinux_state,
2035 fromsid, tosid, SECCLASS_BINDER, BINDER__CALL,
Stephen Smalley79af7302015-01-21 10:54:10 -05002036 NULL);
2037}
2038
2039static int selinux_binder_transfer_binder(struct task_struct *from,
2040 struct task_struct *to)
2041{
2042 u32 fromsid = task_sid(from);
2043 u32 tosid = task_sid(to);
2044
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002045 return avc_has_perm(&selinux_state,
2046 fromsid, tosid, SECCLASS_BINDER, BINDER__TRANSFER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002047 NULL);
2048}
2049
2050static int selinux_binder_transfer_file(struct task_struct *from,
2051 struct task_struct *to,
2052 struct file *file)
2053{
2054 u32 sid = task_sid(to);
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07002055 struct file_security_struct *fsec = selinux_file(file);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002056 struct dentry *dentry = file->f_path.dentry;
Paul Moore20cdef82016-04-04 14:14:42 -04002057 struct inode_security_struct *isec;
Stephen Smalley79af7302015-01-21 10:54:10 -05002058 struct common_audit_data ad;
2059 int rc;
2060
2061 ad.type = LSM_AUDIT_DATA_PATH;
2062 ad.u.path = file->f_path;
2063
2064 if (sid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002065 rc = avc_has_perm(&selinux_state,
2066 sid, fsec->sid,
Stephen Smalley79af7302015-01-21 10:54:10 -05002067 SECCLASS_FD,
2068 FD__USE,
2069 &ad);
2070 if (rc)
2071 return rc;
2072 }
2073
Chenbo Fengf66e4482017-10-18 13:00:26 -07002074#ifdef CONFIG_BPF_SYSCALL
2075 rc = bpf_fd_pass(file, sid);
2076 if (rc)
2077 return rc;
2078#endif
2079
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002080 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
Stephen Smalley79af7302015-01-21 10:54:10 -05002081 return 0;
2082
Paul Moore20cdef82016-04-04 14:14:42 -04002083 isec = backing_inode_security(dentry);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002084 return avc_has_perm(&selinux_state,
2085 sid, isec->sid, isec->sclass, file_to_av(file),
Stephen Smalley79af7302015-01-21 10:54:10 -05002086 &ad);
2087}
2088
Ingo Molnar9e488582009-05-07 19:26:19 +10002089static int selinux_ptrace_access_check(struct task_struct *child,
David Howells5cd9c582008-08-14 11:37:28 +01002090 unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002092 u32 sid = current_sid();
2093 u32 csid = task_sid(child);
Stephen Smalley006ebb42008-05-19 08:32:49 -04002094
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002095 if (mode & PTRACE_MODE_READ)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002096 return avc_has_perm(&selinux_state,
2097 sid, csid, SECCLASS_FILE, FILE__READ, NULL);
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002098
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002099 return avc_has_perm(&selinux_state,
2100 sid, csid, SECCLASS_PROCESS, PROCESS__PTRACE, NULL);
David Howells5cd9c582008-08-14 11:37:28 +01002101}
2102
2103static int selinux_ptrace_traceme(struct task_struct *parent)
2104{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002105 return avc_has_perm(&selinux_state,
2106 task_sid(parent), current_sid(), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002107 PROCESS__PTRACE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108}
2109
2110static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
Eric Paris828dfe12008-04-17 13:17:49 -04002111 kernel_cap_t *inheritable, kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002113 return avc_has_perm(&selinux_state,
2114 current_sid(), task_sid(target), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002115 PROCESS__GETCAP, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116}
2117
David Howellsd84f4f92008-11-14 10:39:23 +11002118static int selinux_capset(struct cred *new, const struct cred *old,
2119 const kernel_cap_t *effective,
2120 const kernel_cap_t *inheritable,
2121 const kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002123 return avc_has_perm(&selinux_state,
2124 cred_sid(old), cred_sid(new), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002125 PROCESS__SETCAP, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126}
2127
James Morris5626d3e2009-01-30 10:05:06 +11002128/*
2129 * (This comment used to live with the selinux_task_setuid hook,
2130 * which was removed).
2131 *
2132 * Since setuid only affects the current process, and since the SELinux
2133 * controls are not based on the Linux identity attributes, SELinux does not
2134 * need to control this operation. However, SELinux does control the use of
2135 * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
2136 */
2137
Eric Paris6a9de492012-01-03 12:25:14 -05002138static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
Micah Mortonc1a85a02019-01-07 16:10:53 -08002139 int cap, unsigned int opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140{
Micah Mortonc1a85a02019-01-07 16:10:53 -08002141 return cred_has_capability(cred, cap, opts, ns == &init_user_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142}
2143
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
2145{
David Howells88e67f32008-11-14 10:39:21 +11002146 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 int rc = 0;
2148
2149 if (!sb)
2150 return 0;
2151
2152 switch (cmds) {
Eric Paris828dfe12008-04-17 13:17:49 -04002153 case Q_SYNC:
2154 case Q_QUOTAON:
2155 case Q_QUOTAOFF:
2156 case Q_SETINFO:
2157 case Q_SETQUOTA:
Richard Hainese4cfa052020-02-20 15:32:34 +00002158 case Q_XQUOTAOFF:
2159 case Q_XQUOTAON:
2160 case Q_XSETQLIM:
David Howells88e67f32008-11-14 10:39:21 +11002161 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04002162 break;
2163 case Q_GETFMT:
2164 case Q_GETINFO:
2165 case Q_GETQUOTA:
Richard Hainese4cfa052020-02-20 15:32:34 +00002166 case Q_XGETQUOTA:
2167 case Q_XGETQSTAT:
2168 case Q_XGETQSTATV:
2169 case Q_XGETNEXTQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11002170 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04002171 break;
2172 default:
2173 rc = 0; /* let the kernel handle invalid cmds */
2174 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 }
2176 return rc;
2177}
2178
2179static int selinux_quota_on(struct dentry *dentry)
2180{
David Howells88e67f32008-11-14 10:39:21 +11002181 const struct cred *cred = current_cred();
2182
Eric Paris2875fa02011-04-28 16:04:24 -04002183 return dentry_has_perm(cred, dentry, FILE__QUOTAON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184}
2185
Eric Paris12b30522010-11-15 18:36:29 -05002186static int selinux_syslog(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 switch (type) {
Kees Cookd78ca3c2010-02-03 15:37:13 -08002189 case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */
2190 case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002191 return avc_has_perm(&selinux_state,
2192 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002193 SECCLASS_SYSTEM, SYSTEM__SYSLOG_READ, NULL);
Kees Cookd78ca3c2010-02-03 15:37:13 -08002194 case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */
2195 case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */
2196 /* Set level of messages printed to console */
2197 case SYSLOG_ACTION_CONSOLE_LEVEL:
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002198 return avc_has_perm(&selinux_state,
2199 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002200 SECCLASS_SYSTEM, SYSTEM__SYSLOG_CONSOLE,
2201 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 }
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002203 /* All other syslog types */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002204 return avc_has_perm(&selinux_state,
2205 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002206 SECCLASS_SYSTEM, SYSTEM__SYSLOG_MOD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207}
2208
2209/*
2210 * Check that a process has enough memory to allocate a new virtual
2211 * mapping. 0 means there is enough memory for the allocation to
2212 * succeed and -ENOMEM implies there is not.
2213 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 * Do not audit the selinux permission check, as this is applied to all
2215 * processes that allocate mappings.
2216 */
Alan Cox34b4e4a2007-08-22 14:01:28 -07002217static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218{
2219 int rc, cap_sys_admin = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002221 rc = cred_has_capability(current_cred(), CAP_SYS_ADMIN,
Micah Mortonc1a85a02019-01-07 16:10:53 -08002222 CAP_OPT_NOAUDIT, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 if (rc == 0)
2224 cap_sys_admin = 1;
2225
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002226 return cap_sys_admin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227}
2228
2229/* binprm security operations */
2230
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002231static u32 ptrace_parent_sid(void)
Paul Moore0c6181c2016-03-30 21:41:21 -04002232{
2233 u32 sid = 0;
2234 struct task_struct *tracer;
2235
2236 rcu_read_lock();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002237 tracer = ptrace_parent(current);
Paul Moore0c6181c2016-03-30 21:41:21 -04002238 if (tracer)
2239 sid = task_sid(tracer);
2240 rcu_read_unlock();
2241
2242 return sid;
2243}
2244
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002245static int check_nnp_nosuid(const struct linux_binprm *bprm,
2246 const struct task_security_struct *old_tsec,
2247 const struct task_security_struct *new_tsec)
2248{
2249 int nnp = (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS);
Andy Lutomirski380cf5b2016-06-23 16:41:05 -05002250 int nosuid = !mnt_may_suid(bprm->file->f_path.mnt);
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002251 int rc;
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002252 u32 av;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002253
2254 if (!nnp && !nosuid)
2255 return 0; /* neither NNP nor nosuid */
2256
2257 if (new_tsec->sid == old_tsec->sid)
2258 return 0; /* No change in credentials */
2259
2260 /*
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002261 * If the policy enables the nnp_nosuid_transition policy capability,
2262 * then we permit transitions under NNP or nosuid if the
2263 * policy allows the corresponding permission between
2264 * the old and new contexts.
2265 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002266 if (selinux_policycap_nnp_nosuid_transition()) {
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002267 av = 0;
2268 if (nnp)
2269 av |= PROCESS2__NNP_TRANSITION;
2270 if (nosuid)
2271 av |= PROCESS2__NOSUID_TRANSITION;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002272 rc = avc_has_perm(&selinux_state,
2273 old_tsec->sid, new_tsec->sid,
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002274 SECCLASS_PROCESS2, av, NULL);
2275 if (!rc)
2276 return 0;
2277 }
2278
2279 /*
2280 * We also permit NNP or nosuid transitions to bounded SIDs,
2281 * i.e. SIDs that are guaranteed to only be allowed a subset
2282 * of the permissions of the current SID.
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002283 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002284 rc = security_bounded_transition(&selinux_state, old_tsec->sid,
2285 new_tsec->sid);
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002286 if (!rc)
2287 return 0;
2288
2289 /*
2290 * On failure, preserve the errno values for NNP vs nosuid.
2291 * NNP: Operation not permitted for caller.
2292 * nosuid: Permission denied to file.
2293 */
2294 if (nnp)
2295 return -EPERM;
2296 return -EACCES;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002297}
2298
Eric W. Biedermanb8bff592020-03-22 15:46:24 -05002299static int selinux_bprm_creds_for_exec(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300{
David Howellsa6f76f22008-11-14 10:39:24 +11002301 const struct task_security_struct *old_tsec;
2302 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 struct inode_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04002304 struct common_audit_data ad;
Al Viro496ad9a2013-01-23 17:07:38 -05002305 struct inode *inode = file_inode(bprm->file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 int rc;
2307
David Howellsa6f76f22008-11-14 10:39:24 +11002308 /* SELinux context only depends on initial program or script and not
2309 * the script interpreter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002311 old_tsec = selinux_cred(current_cred());
2312 new_tsec = selinux_cred(bprm->cred);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002313 isec = inode_security(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
2315 /* Default to the current task SID. */
David Howellsa6f76f22008-11-14 10:39:24 +11002316 new_tsec->sid = old_tsec->sid;
2317 new_tsec->osid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
Michael LeMay28eba5b2006-06-27 02:53:42 -07002319 /* Reset fs, key, and sock SIDs on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002320 new_tsec->create_sid = 0;
2321 new_tsec->keycreate_sid = 0;
2322 new_tsec->sockcreate_sid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
David Howellsa6f76f22008-11-14 10:39:24 +11002324 if (old_tsec->exec_sid) {
2325 new_tsec->sid = old_tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 /* Reset exec SID on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002327 new_tsec->exec_sid = 0;
Andy Lutomirski259e5e62012-04-12 16:47:50 -05002328
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002329 /* Fail on NNP or nosuid if not an allowed transition. */
2330 rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
2331 if (rc)
2332 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 } else {
2334 /* Check for a default transition on this program. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002335 rc = security_transition_sid(&selinux_state, old_tsec->sid,
2336 isec->sid, SECCLASS_PROCESS, NULL,
Eric Paris652bb9b2011-02-01 11:05:40 -05002337 &new_tsec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 if (rc)
2339 return rc;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002340
2341 /*
2342 * Fallback to old SID on NNP or nosuid if not an allowed
2343 * transition.
2344 */
2345 rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
2346 if (rc)
2347 new_tsec->sid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 }
2349
Vivek Goyal43af5de2016-09-09 11:37:49 -04002350 ad.type = LSM_AUDIT_DATA_FILE;
2351 ad.u.file = bprm->file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
David Howellsa6f76f22008-11-14 10:39:24 +11002353 if (new_tsec->sid == old_tsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002354 rc = avc_has_perm(&selinux_state,
2355 old_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
2357 if (rc)
2358 return rc;
2359 } else {
2360 /* Check permissions for the transition. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002361 rc = avc_has_perm(&selinux_state,
2362 old_tsec->sid, new_tsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
2364 if (rc)
2365 return rc;
2366
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002367 rc = avc_has_perm(&selinux_state,
2368 new_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
2370 if (rc)
2371 return rc;
2372
David Howellsa6f76f22008-11-14 10:39:24 +11002373 /* Check for shared state */
2374 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002375 rc = avc_has_perm(&selinux_state,
2376 old_tsec->sid, new_tsec->sid,
David Howellsa6f76f22008-11-14 10:39:24 +11002377 SECCLASS_PROCESS, PROCESS__SHARE,
2378 NULL);
2379 if (rc)
2380 return -EPERM;
2381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
David Howellsa6f76f22008-11-14 10:39:24 +11002383 /* Make sure that anyone attempting to ptrace over a task that
2384 * changes its SID has the appropriate permit */
Eric W. Biederman9227dd22017-01-23 17:26:31 +13002385 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002386 u32 ptsid = ptrace_parent_sid();
David Howellsa6f76f22008-11-14 10:39:24 +11002387 if (ptsid != 0) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002388 rc = avc_has_perm(&selinux_state,
2389 ptsid, new_tsec->sid,
David Howellsa6f76f22008-11-14 10:39:24 +11002390 SECCLASS_PROCESS,
2391 PROCESS__PTRACE, NULL);
2392 if (rc)
2393 return -EPERM;
2394 }
2395 }
2396
2397 /* Clear any possibly unsafe personality bits on exec: */
2398 bprm->per_clear |= PER_CLEAR_ON_SETID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 /* Enable secure mode for SIDs transitions unless
2401 the noatsecure permission is granted between
2402 the two SIDs, i.e. ahp returns 0. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002403 rc = avc_has_perm(&selinux_state,
2404 old_tsec->sid, new_tsec->sid,
Kees Cook62874c32017-07-18 15:25:25 -07002405 SECCLASS_PROCESS, PROCESS__NOATSECURE,
2406 NULL);
2407 bprm->secureexec |= !!rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 }
2409
Kees Cook62874c32017-07-18 15:25:25 -07002410 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411}
2412
Al Viroc3c073f2012-08-21 22:32:06 -04002413static int match_file(const void *p, struct file *file, unsigned fd)
2414{
2415 return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0;
2416}
2417
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418/* Derived from fs/exec.c:flush_old_files. */
David Howells745ca242008-11-14 10:39:22 +11002419static inline void flush_unauthorized_files(const struct cred *cred,
2420 struct files_struct *files)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 struct file *file, *devnull = NULL;
Stephen Smalleyb20c8122006-09-25 23:32:03 -07002423 struct tty_struct *tty;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002424 int drop_tty = 0;
Al Viroc3c073f2012-08-21 22:32:06 -04002425 unsigned n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002427 tty = get_current_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 if (tty) {
Peter Hurley4a510962016-01-09 21:35:23 -08002429 spin_lock(&tty->files_lock);
Eric Paris37dd0bd2008-10-31 17:40:00 -04002430 if (!list_empty(&tty->tty_files)) {
Nick Piggind996b622010-08-18 04:37:36 +10002431 struct tty_file_private *file_priv;
Eric Paris37dd0bd2008-10-31 17:40:00 -04002432
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 /* Revalidate access to controlling tty.
David Howells13f8e982013-06-13 23:37:55 +01002434 Use file_path_has_perm on the tty path directly
2435 rather than using file_has_perm, as this particular
2436 open file may belong to another process and we are
2437 only interested in the inode-based check here. */
Nick Piggind996b622010-08-18 04:37:36 +10002438 file_priv = list_first_entry(&tty->tty_files,
2439 struct tty_file_private, list);
2440 file = file_priv->file;
David Howells13f8e982013-06-13 23:37:55 +01002441 if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE))
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002442 drop_tty = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 }
Peter Hurley4a510962016-01-09 21:35:23 -08002444 spin_unlock(&tty->files_lock);
Alan Cox452a00d2008-10-13 10:39:13 +01002445 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 }
Eric W. Biederman98a27ba2007-05-08 00:26:56 -07002447 /* Reset controlling tty. */
2448 if (drop_tty)
2449 no_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
2451 /* Revalidate access to inherited open files. */
Al Viroc3c073f2012-08-21 22:32:06 -04002452 n = iterate_fd(files, 0, match_file, cred);
2453 if (!n) /* none found? */
2454 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455
Al Viroc3c073f2012-08-21 22:32:06 -04002456 devnull = dentry_open(&selinux_null, O_RDWR, cred);
Al Viro45525b22012-10-16 13:30:07 -04002457 if (IS_ERR(devnull))
2458 devnull = NULL;
2459 /* replace all the matching ones with this */
2460 do {
2461 replace_fd(n - 1, devnull, 0);
2462 } while ((n = iterate_fd(files, n, match_file, cred)) != 0);
2463 if (devnull)
Al Viroc3c073f2012-08-21 22:32:06 -04002464 fput(devnull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465}
2466
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467/*
David Howellsa6f76f22008-11-14 10:39:24 +11002468 * Prepare a process for imminent new credential changes due to exec
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 */
David Howellsa6f76f22008-11-14 10:39:24 +11002470static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471{
David Howellsa6f76f22008-11-14 10:39:24 +11002472 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 struct rlimit *rlim, *initrlim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 int rc, i;
2475
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002476 new_tsec = selinux_cred(bprm->cred);
David Howellsa6f76f22008-11-14 10:39:24 +11002477 if (new_tsec->sid == new_tsec->osid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 return;
2479
2480 /* Close files for which the new task SID is not authorized. */
David Howellsa6f76f22008-11-14 10:39:24 +11002481 flush_unauthorized_files(bprm->cred, current->files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
David Howellsa6f76f22008-11-14 10:39:24 +11002483 /* Always clear parent death signal on SID transitions. */
2484 current->pdeath_signal = 0;
2485
2486 /* Check whether the new SID can inherit resource limits from the old
2487 * SID. If not, reset all soft limits to the lower of the current
2488 * task's hard limit and the init task's soft limit.
2489 *
2490 * Note that the setting of hard limits (even to lower them) can be
2491 * controlled by the setrlimit check. The inclusion of the init task's
2492 * soft limit into the computation is to avoid resetting soft limits
2493 * higher than the default soft limit for cases where the default is
2494 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2495 */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002496 rc = avc_has_perm(&selinux_state,
2497 new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
David Howellsa6f76f22008-11-14 10:39:24 +11002498 PROCESS__RLIMITINH, NULL);
2499 if (rc) {
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002500 /* protect against do_prlimit() */
2501 task_lock(current);
David Howellsa6f76f22008-11-14 10:39:24 +11002502 for (i = 0; i < RLIM_NLIMITS; i++) {
2503 rlim = current->signal->rlim + i;
2504 initrlim = init_task.signal->rlim + i;
2505 rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
2506 }
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002507 task_unlock(current);
Nicolas Pitrebaa73d92016-11-11 00:10:10 -05002508 if (IS_ENABLED(CONFIG_POSIX_TIMERS))
2509 update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
David Howellsa6f76f22008-11-14 10:39:24 +11002510 }
2511}
2512
2513/*
2514 * Clean up the process immediately after the installation of new credentials
2515 * due to exec
2516 */
2517static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
2518{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002519 const struct task_security_struct *tsec = selinux_cred(current_cred());
David Howellsa6f76f22008-11-14 10:39:24 +11002520 u32 osid, sid;
Arnd Bergmannddbc7d02019-10-25 21:37:43 +02002521 int rc;
David Howellsa6f76f22008-11-14 10:39:24 +11002522
David Howellsa6f76f22008-11-14 10:39:24 +11002523 osid = tsec->osid;
2524 sid = tsec->sid;
2525
2526 if (sid == osid)
2527 return;
2528
2529 /* Check whether the new SID can inherit signal state from the old SID.
2530 * If not, clear itimers to avoid subsequent signal generation and
2531 * flush and unblock signals.
2532 *
2533 * This must occur _after_ the task SID has been updated so that any
2534 * kill done after the flush will be checked against the new SID.
2535 */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002536 rc = avc_has_perm(&selinux_state,
2537 osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 if (rc) {
Arnd Bergmannddbc7d02019-10-25 21:37:43 +02002539 clear_itimer();
2540
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterov9e7c8f82015-06-04 16:22:16 -04002542 if (!fatal_signal_pending(current)) {
2543 flush_sigqueue(&current->pending);
2544 flush_sigqueue(&current->signal->shared_pending);
David Howells3bcac022009-04-29 13:45:05 +01002545 flush_signal_handlers(current, 1);
2546 sigemptyset(&current->blocked);
Oleg Nesterov9e7c8f82015-06-04 16:22:16 -04002547 recalc_sigpending();
David Howells3bcac022009-04-29 13:45:05 +01002548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 spin_unlock_irq(&current->sighand->siglock);
2550 }
2551
David Howellsa6f76f22008-11-14 10:39:24 +11002552 /* Wake up the parent if it is waiting so that it can recheck
2553 * wait permission to the new task SID. */
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002554 read_lock(&tasklist_lock);
Oleg Nesterov0b7570e2009-09-23 15:56:46 -07002555 __wake_up_parent(current, current->real_parent);
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002556 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557}
2558
2559/* superblock security operations */
2560
2561static int selinux_sb_alloc_security(struct super_block *sb)
2562{
Paul Moorecb89e242020-01-10 16:32:10 -05002563 struct superblock_security_struct *sbsec;
2564
2565 sbsec = kzalloc(sizeof(struct superblock_security_struct), GFP_KERNEL);
2566 if (!sbsec)
2567 return -ENOMEM;
2568
2569 mutex_init(&sbsec->lock);
2570 INIT_LIST_HEAD(&sbsec->isec_head);
2571 spin_lock_init(&sbsec->isec_lock);
Paul Moorecb89e242020-01-10 16:32:10 -05002572 sbsec->sid = SECINITSID_UNLABELED;
2573 sbsec->def_sid = SECINITSID_FILE;
2574 sbsec->mntpoint_sid = SECINITSID_UNLABELED;
2575 sb->s_security = sbsec;
2576
2577 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578}
2579
2580static void selinux_sb_free_security(struct super_block *sb)
2581{
2582 superblock_free_security(sb);
2583}
2584
Al Viro99dbbb52018-12-14 21:56:23 -05002585static inline int opt_len(const char *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586{
Al Viro99dbbb52018-12-14 21:56:23 -05002587 bool open_quote = false;
2588 int len;
2589 char c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590
Al Viro99dbbb52018-12-14 21:56:23 -05002591 for (len = 0; (c = s[len]) != '\0'; len++) {
2592 if (c == '"')
Cory Olmo3528a952006-09-29 01:58:44 -07002593 open_quote = !open_quote;
Al Viro99dbbb52018-12-14 21:56:23 -05002594 if (c == ',' && !open_quote)
2595 break;
2596 }
2597 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598}
2599
Al Viro204cc0c2018-12-13 13:41:47 -05002600static int selinux_sb_eat_lsm_opts(char *options, void **mnt_opts)
Eric Paris026eb162011-03-03 16:09:14 -05002601{
Al Viro99dbbb52018-12-14 21:56:23 -05002602 char *from = options;
2603 char *to = options;
2604 bool first = true;
Gen Zhangfec63752019-06-12 21:55:38 +08002605 int rc;
Al Viro5b400232018-12-12 20:13:29 -05002606
Al Viro99dbbb52018-12-14 21:56:23 -05002607 while (1) {
2608 int len = opt_len(from);
Gen Zhangfec63752019-06-12 21:55:38 +08002609 int token;
Al Viro99dbbb52018-12-14 21:56:23 -05002610 char *arg = NULL;
2611
2612 token = match_opt_prefix(from, len, &arg);
2613
2614 if (token != Opt_error) {
2615 char *p, *q;
2616
2617 /* strip quotes */
2618 if (arg) {
2619 for (p = q = arg; p < from + len; p++) {
2620 char c = *p;
2621 if (c != '"')
2622 *q++ = c;
2623 }
2624 arg = kmemdup_nul(arg, q - arg, GFP_KERNEL);
Gen Zhangfec63752019-06-12 21:55:38 +08002625 if (!arg) {
2626 rc = -ENOMEM;
2627 goto free_opt;
2628 }
Al Viro99dbbb52018-12-14 21:56:23 -05002629 }
2630 rc = selinux_add_opt(token, arg, mnt_opts);
2631 if (unlikely(rc)) {
2632 kfree(arg);
Gen Zhangfec63752019-06-12 21:55:38 +08002633 goto free_opt;
Al Viro99dbbb52018-12-14 21:56:23 -05002634 }
2635 } else {
2636 if (!first) { // copy with preceding comma
2637 from--;
2638 len++;
2639 }
2640 if (to != from)
2641 memmove(to, from, len);
2642 to += len;
2643 first = false;
2644 }
2645 if (!from[len])
2646 break;
2647 from += len + 1;
2648 }
2649 *to = '\0';
2650 return 0;
Gen Zhangfec63752019-06-12 21:55:38 +08002651
2652free_opt:
2653 if (*mnt_opts) {
2654 selinux_free_mnt_opts(*mnt_opts);
2655 *mnt_opts = NULL;
2656 }
2657 return rc;
Al Viro5b400232018-12-12 20:13:29 -05002658}
2659
Al Viro204cc0c2018-12-13 13:41:47 -05002660static int selinux_sb_remount(struct super_block *sb, void *mnt_opts)
Eric Paris026eb162011-03-03 16:09:14 -05002661{
Al Virobd323652018-12-13 15:04:59 -05002662 struct selinux_mnt_opts *opts = mnt_opts;
Eric Paris026eb162011-03-03 16:09:14 -05002663 struct superblock_security_struct *sbsec = sb->s_security;
Al Virobd323652018-12-13 15:04:59 -05002664 u32 sid;
2665 int rc;
Eric Paris026eb162011-03-03 16:09:14 -05002666
2667 if (!(sbsec->flags & SE_SBINITIALIZED))
2668 return 0;
2669
Al Viro204cc0c2018-12-13 13:41:47 -05002670 if (!opts)
Eric Paris026eb162011-03-03 16:09:14 -05002671 return 0;
2672
Al Virobd323652018-12-13 15:04:59 -05002673 if (opts->fscontext) {
2674 rc = parse_sid(sb, opts->fscontext, &sid);
2675 if (rc)
Al Viroc039bc32018-12-01 23:06:57 -05002676 return rc;
Al Virobd323652018-12-13 15:04:59 -05002677 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
2678 goto out_bad_option;
Eric Paris026eb162011-03-03 16:09:14 -05002679 }
Al Virobd323652018-12-13 15:04:59 -05002680 if (opts->context) {
2681 rc = parse_sid(sb, opts->context, &sid);
2682 if (rc)
2683 return rc;
2684 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
2685 goto out_bad_option;
2686 }
2687 if (opts->rootcontext) {
2688 struct inode_security_struct *root_isec;
2689 root_isec = backing_inode_security(sb->s_root);
2690 rc = parse_sid(sb, opts->rootcontext, &sid);
2691 if (rc)
2692 return rc;
2693 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
2694 goto out_bad_option;
2695 }
2696 if (opts->defcontext) {
2697 rc = parse_sid(sb, opts->defcontext, &sid);
2698 if (rc)
2699 return rc;
2700 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
2701 goto out_bad_option;
Eric Paris026eb162011-03-03 16:09:14 -05002702 }
Al Viroc039bc32018-12-01 23:06:57 -05002703 return 0;
Eric Paris026eb162011-03-03 16:09:14 -05002704
Eric Paris026eb162011-03-03 16:09:14 -05002705out_bad_option:
peter enderborgc103a912018-06-12 10:09:03 +02002706 pr_warn("SELinux: unable to change security options "
Linus Torvalds29b1deb2013-12-15 11:17:45 -08002707 "during remount (dev %s, type=%s)\n", sb->s_id,
2708 sb->s_type->name);
Al Viroc039bc32018-12-01 23:06:57 -05002709 return -EINVAL;
Eric Paris026eb162011-03-03 16:09:14 -05002710}
2711
Al Viroa10d7c22018-12-05 11:58:35 -05002712static int selinux_sb_kern_mount(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713{
David Howells88e67f32008-11-14 10:39:21 +11002714 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002715 struct common_audit_data ad;
James Morris74192242008-12-19 11:41:10 +11002716
Eric Paris50c205f2012-04-04 15:01:43 -04002717 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002718 ad.u.dentry = sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002719 return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720}
2721
David Howells726c3342006-06-23 02:02:58 -07002722static int selinux_sb_statfs(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723{
David Howells88e67f32008-11-14 10:39:21 +11002724 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002725 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726
Eric Paris50c205f2012-04-04 15:01:43 -04002727 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002728 ad.u.dentry = dentry->d_sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002729 return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730}
2731
Al Viro808d4e32012-10-11 11:42:01 -04002732static int selinux_mount(const char *dev_name,
Al Viro8a04c432016-03-25 14:52:53 -04002733 const struct path *path,
Al Viro808d4e32012-10-11 11:42:01 -04002734 const char *type,
Eric Paris828dfe12008-04-17 13:17:49 -04002735 unsigned long flags,
2736 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737{
David Howells88e67f32008-11-14 10:39:21 +11002738 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739
2740 if (flags & MS_REMOUNT)
Al Virod8c95842011-12-07 18:16:57 -05002741 return superblock_has_perm(cred, path->dentry->d_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002742 FILESYSTEM__REMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 else
Eric Paris2875fa02011-04-28 16:04:24 -04002744 return path_has_perm(cred, path, FILE__MOUNTON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745}
2746
Stephen Smalley98aa0032020-01-17 15:24:07 -05002747static int selinux_move_mount(const struct path *from_path,
2748 const struct path *to_path)
2749{
2750 const struct cred *cred = current_cred();
2751
2752 return path_has_perm(cred, to_path, FILE__MOUNTON);
2753}
2754
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755static int selinux_umount(struct vfsmount *mnt, int flags)
2756{
David Howells88e67f32008-11-14 10:39:21 +11002757 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758
David Howells88e67f32008-11-14 10:39:21 +11002759 return superblock_has_perm(cred, mnt->mnt_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002760 FILESYSTEM__UNMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761}
2762
Al Viro0b520752018-12-23 16:02:47 -05002763static int selinux_fs_context_dup(struct fs_context *fc,
2764 struct fs_context *src_fc)
2765{
2766 const struct selinux_mnt_opts *src = src_fc->security;
2767 struct selinux_mnt_opts *opts;
2768
2769 if (!src)
2770 return 0;
2771
2772 fc->security = kzalloc(sizeof(struct selinux_mnt_opts), GFP_KERNEL);
2773 if (!fc->security)
2774 return -ENOMEM;
2775
2776 opts = fc->security;
2777
2778 if (src->fscontext) {
2779 opts->fscontext = kstrdup(src->fscontext, GFP_KERNEL);
2780 if (!opts->fscontext)
2781 return -ENOMEM;
2782 }
2783 if (src->context) {
2784 opts->context = kstrdup(src->context, GFP_KERNEL);
2785 if (!opts->context)
2786 return -ENOMEM;
2787 }
2788 if (src->rootcontext) {
2789 opts->rootcontext = kstrdup(src->rootcontext, GFP_KERNEL);
2790 if (!opts->rootcontext)
2791 return -ENOMEM;
2792 }
2793 if (src->defcontext) {
2794 opts->defcontext = kstrdup(src->defcontext, GFP_KERNEL);
2795 if (!opts->defcontext)
2796 return -ENOMEM;
2797 }
2798 return 0;
2799}
2800
Al Virod7167b12019-09-07 07:23:15 -04002801static const struct fs_parameter_spec selinux_fs_parameters[] = {
David Howells442155c2018-11-01 23:07:24 +00002802 fsparam_string(CONTEXT_STR, Opt_context),
2803 fsparam_string(DEFCONTEXT_STR, Opt_defcontext),
2804 fsparam_string(FSCONTEXT_STR, Opt_fscontext),
2805 fsparam_string(ROOTCONTEXT_STR, Opt_rootcontext),
2806 fsparam_flag (SECLABEL_STR, Opt_seclabel),
2807 {}
2808};
2809
David Howells442155c2018-11-01 23:07:24 +00002810static int selinux_fs_context_parse_param(struct fs_context *fc,
2811 struct fs_parameter *param)
2812{
2813 struct fs_parse_result result;
2814 int opt, rc;
2815
Al Virod7167b12019-09-07 07:23:15 -04002816 opt = fs_parse(fc, selinux_fs_parameters, param, &result);
David Howells442155c2018-11-01 23:07:24 +00002817 if (opt < 0)
2818 return opt;
2819
2820 rc = selinux_add_opt(opt, param->string, &fc->security);
2821 if (!rc) {
2822 param->string = NULL;
2823 rc = 1;
2824 }
2825 return rc;
2826}
2827
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828/* inode security operations */
2829
2830static int selinux_inode_alloc_security(struct inode *inode)
2831{
Paul Moorecb89e242020-01-10 16:32:10 -05002832 struct inode_security_struct *isec = selinux_inode(inode);
2833 u32 sid = current_sid();
2834
2835 spin_lock_init(&isec->lock);
2836 INIT_LIST_HEAD(&isec->list);
2837 isec->inode = inode;
2838 isec->sid = SECINITSID_UNLABELED;
2839 isec->sclass = SECCLASS_FILE;
2840 isec->task_sid = sid;
2841 isec->initialized = LABEL_INVALID;
2842
2843 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844}
2845
2846static void selinux_inode_free_security(struct inode *inode)
2847{
2848 inode_free_security(inode);
2849}
2850
David Quigleyd47be3d2013-05-22 12:50:34 -04002851static int selinux_dentry_init_security(struct dentry *dentry, int mode,
Al Viro4f3ccd72016-07-20 16:06:15 -04002852 const struct qstr *name, void **ctx,
David Quigleyd47be3d2013-05-22 12:50:34 -04002853 u32 *ctxlen)
2854{
David Quigleyd47be3d2013-05-22 12:50:34 -04002855 u32 newsid;
2856 int rc;
2857
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002858 rc = selinux_determine_inode_label(selinux_cred(current_cred()),
Vivek Goyalc957f6d2016-07-13 10:44:51 -04002859 d_inode(dentry->d_parent), name,
David Howellsc3c188b2015-07-10 17:19:58 -04002860 inode_mode_to_security_class(mode),
2861 &newsid);
2862 if (rc)
2863 return rc;
David Quigleyd47be3d2013-05-22 12:50:34 -04002864
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002865 return security_sid_to_context(&selinux_state, newsid, (char **)ctx,
2866 ctxlen);
David Quigleyd47be3d2013-05-22 12:50:34 -04002867}
2868
Vivek Goyala518b0a2016-07-13 10:44:53 -04002869static int selinux_dentry_create_files_as(struct dentry *dentry, int mode,
2870 struct qstr *name,
2871 const struct cred *old,
2872 struct cred *new)
2873{
2874 u32 newsid;
2875 int rc;
2876 struct task_security_struct *tsec;
2877
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002878 rc = selinux_determine_inode_label(selinux_cred(old),
Vivek Goyala518b0a2016-07-13 10:44:53 -04002879 d_inode(dentry->d_parent), name,
2880 inode_mode_to_security_class(mode),
2881 &newsid);
2882 if (rc)
2883 return rc;
2884
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002885 tsec = selinux_cred(new);
Vivek Goyala518b0a2016-07-13 10:44:53 -04002886 tsec->create_sid = newsid;
2887 return 0;
2888}
2889
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002890static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +09002891 const struct qstr *qstr,
2892 const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -05002893 void **value, size_t *len)
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002894{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002895 const struct task_security_struct *tsec = selinux_cred(current_cred());
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002896 struct superblock_security_struct *sbsec;
Corentin LABBEc0d4f462017-10-04 20:32:17 +02002897 u32 newsid, clen;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002898 int rc;
Tetsuo Handa95489062013-07-25 05:44:02 +09002899 char *context;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002900
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002901 sbsec = dir->i_sb->s_security;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002902
David Howells275bb412008-11-14 10:39:19 +11002903 newsid = tsec->create_sid;
2904
Yang Guo210a2922019-12-12 10:02:24 +08002905 rc = selinux_determine_inode_label(tsec, dir, qstr,
David Howellsc3c188b2015-07-10 17:19:58 -04002906 inode_mode_to_security_class(inode->i_mode),
2907 &newsid);
2908 if (rc)
2909 return rc;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002910
Eric Paris296fddf2006-09-25 23:32:00 -07002911 /* Possibly defer initialization to selinux_complete_init. */
David P. Quigley0d90a7e2009-01-16 09:22:02 -05002912 if (sbsec->flags & SE_SBINITIALIZED) {
Casey Schaufler80788c22018-09-21 17:19:11 -07002913 struct inode_security_struct *isec = selinux_inode(inode);
Eric Paris296fddf2006-09-25 23:32:00 -07002914 isec->sclass = inode_mode_to_security_class(inode->i_mode);
2915 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05002916 isec->initialized = LABEL_INITIALIZED;
Eric Paris296fddf2006-09-25 23:32:00 -07002917 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002918
Ondrej Mosnacek65cddd52020-01-07 14:31:53 +01002919 if (!selinux_initialized(&selinux_state) ||
2920 !(sbsec->flags & SBLABEL_MNT))
Stephen Smalley25a74f32005-11-08 21:34:33 -08002921 return -EOPNOTSUPP;
2922
Tetsuo Handa95489062013-07-25 05:44:02 +09002923 if (name)
2924 *name = XATTR_SELINUX_SUFFIX;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002925
2926 if (value && len) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002927 rc = security_sid_to_context_force(&selinux_state, newsid,
2928 &context, &clen);
Tetsuo Handa95489062013-07-25 05:44:02 +09002929 if (rc)
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002930 return rc;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002931 *value = context;
2932 *len = clen;
2933 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002934
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002935 return 0;
2936}
2937
Al Viro4acdaf22011-07-26 01:42:34 -04002938static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939{
2940 return may_create(dir, dentry, SECCLASS_FILE);
2941}
2942
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2944{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 return may_link(dir, old_dentry, MAY_LINK);
2946}
2947
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
2949{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 return may_link(dir, dentry, MAY_UNLINK);
2951}
2952
2953static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
2954{
2955 return may_create(dir, dentry, SECCLASS_LNK_FILE);
2956}
2957
Al Viro18bb1db2011-07-26 01:41:39 -04002958static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959{
2960 return may_create(dir, dentry, SECCLASS_DIR);
2961}
2962
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
2964{
2965 return may_link(dir, dentry, MAY_RMDIR);
2966}
2967
Al Viro1a67aaf2011-07-26 01:52:52 -04002968static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 return may_create(dir, dentry, inode_mode_to_security_class(mode));
2971}
2972
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
Eric Paris828dfe12008-04-17 13:17:49 -04002974 struct inode *new_inode, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975{
2976 return may_rename(old_inode, old_dentry, new_inode, new_dentry);
2977}
2978
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979static int selinux_inode_readlink(struct dentry *dentry)
2980{
David Howells88e67f32008-11-14 10:39:21 +11002981 const struct cred *cred = current_cred();
2982
Eric Paris2875fa02011-04-28 16:04:24 -04002983 return dentry_has_perm(cred, dentry, FILE__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984}
2985
NeilBrownbda0be72015-03-23 13:37:39 +11002986static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode,
2987 bool rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988{
David Howells88e67f32008-11-14 10:39:21 +11002989 const struct cred *cred = current_cred();
NeilBrownbda0be72015-03-23 13:37:39 +11002990 struct common_audit_data ad;
2991 struct inode_security_struct *isec;
2992 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993
NeilBrownbda0be72015-03-23 13:37:39 +11002994 validate_creds(cred);
2995
2996 ad.type = LSM_AUDIT_DATA_DENTRY;
2997 ad.u.dentry = dentry;
2998 sid = cred_sid(cred);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05002999 isec = inode_security_rcu(inode, rcu);
3000 if (IS_ERR(isec))
3001 return PTR_ERR(isec);
NeilBrownbda0be72015-03-23 13:37:39 +11003002
Stephen Smalley1a370792019-11-22 12:22:44 -05003003 return avc_has_perm_flags(&selinux_state,
3004 sid, isec->sid, isec->sclass, FILE__READ, &ad,
3005 rcu ? MAY_NOT_BLOCK : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006}
3007
Eric Parisd4cf970d2012-04-04 15:01:42 -04003008static noinline int audit_inode_permission(struct inode *inode,
3009 u32 perms, u32 audited, u32 denied,
Stephen Smalley0188d5c2019-11-22 12:22:45 -05003010 int result)
Eric Parisd4cf970d2012-04-04 15:01:42 -04003011{
3012 struct common_audit_data ad;
Casey Schaufler80788c22018-09-21 17:19:11 -07003013 struct inode_security_struct *isec = selinux_inode(inode);
Eric Parisd4cf970d2012-04-04 15:01:42 -04003014 int rc;
3015
Eric Paris50c205f2012-04-04 15:01:43 -04003016 ad.type = LSM_AUDIT_DATA_INODE;
Eric Parisd4cf970d2012-04-04 15:01:42 -04003017 ad.u.inode = inode;
3018
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003019 rc = slow_avc_audit(&selinux_state,
3020 current_sid(), isec->sid, isec->sclass, perms,
Stephen Smalley0188d5c2019-11-22 12:22:45 -05003021 audited, denied, result, &ad);
Eric Parisd4cf970d2012-04-04 15:01:42 -04003022 if (rc)
3023 return rc;
3024 return 0;
3025}
3026
Al Viroe74f71e2011-06-20 19:38:15 -04003027static int selinux_inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028{
David Howells88e67f32008-11-14 10:39:21 +11003029 const struct cred *cred = current_cred();
Eric Parisb782e0a2010-07-23 11:44:03 -04003030 u32 perms;
3031 bool from_access;
Stephen Smalley5298d0b2019-11-22 16:16:56 -05003032 bool no_block = mask & MAY_NOT_BLOCK;
Eric Paris2e334052012-04-04 15:01:42 -04003033 struct inode_security_struct *isec;
3034 u32 sid;
3035 struct av_decision avd;
3036 int rc, rc2;
3037 u32 audited, denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038
Eric Parisb782e0a2010-07-23 11:44:03 -04003039 from_access = mask & MAY_ACCESS;
Eric Parisd09ca732010-07-23 11:43:57 -04003040 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
3041
Eric Parisb782e0a2010-07-23 11:44:03 -04003042 /* No permission to check. Existence test. */
3043 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045
Eric Paris2e334052012-04-04 15:01:42 -04003046 validate_creds(cred);
Eric Parisb782e0a2010-07-23 11:44:03 -04003047
Eric Paris2e334052012-04-04 15:01:42 -04003048 if (unlikely(IS_PRIVATE(inode)))
3049 return 0;
Eric Parisb782e0a2010-07-23 11:44:03 -04003050
3051 perms = file_mask_to_av(inode->i_mode, mask);
3052
Eric Paris2e334052012-04-04 15:01:42 -04003053 sid = cred_sid(cred);
Stephen Smalley5298d0b2019-11-22 16:16:56 -05003054 isec = inode_security_rcu(inode, no_block);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05003055 if (IS_ERR(isec))
3056 return PTR_ERR(isec);
Eric Paris2e334052012-04-04 15:01:42 -04003057
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003058 rc = avc_has_perm_noaudit(&selinux_state,
Stephen Smalley3a28cff2018-12-12 10:10:55 -05003059 sid, isec->sid, isec->sclass, perms,
Stephen Smalley5298d0b2019-11-22 16:16:56 -05003060 no_block ? AVC_NONBLOCKING : 0,
Stephen Smalley3a28cff2018-12-12 10:10:55 -05003061 &avd);
Eric Paris2e334052012-04-04 15:01:42 -04003062 audited = avc_audit_required(perms, &avd, rc,
3063 from_access ? FILE__AUDIT_ACCESS : 0,
3064 &denied);
3065 if (likely(!audited))
3066 return rc;
3067
Stephen Smalley0188d5c2019-11-22 12:22:45 -05003068 /* fall back to ref-walk if we have to generate audit */
Stephen Smalley5298d0b2019-11-22 16:16:56 -05003069 if (no_block)
Stephen Smalley0188d5c2019-11-22 12:22:45 -05003070 return -ECHILD;
3071
3072 rc2 = audit_inode_permission(inode, perms, audited, denied, rc);
Eric Paris2e334052012-04-04 15:01:42 -04003073 if (rc2)
3074 return rc2;
3075 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076}
3077
3078static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
3079{
David Howells88e67f32008-11-14 10:39:21 +11003080 const struct cred *cred = current_cred();
Stephen Smalleyccb54472017-05-12 12:41:24 -04003081 struct inode *inode = d_backing_inode(dentry);
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003082 unsigned int ia_valid = iattr->ia_valid;
Eric Paris95dbf732012-04-04 13:45:34 -04003083 __u32 av = FILE__WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003085 /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
3086 if (ia_valid & ATTR_FORCE) {
3087 ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
3088 ATTR_FORCE);
3089 if (!ia_valid)
3090 return 0;
3091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003093 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
3094 ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
Eric Paris2875fa02011-04-28 16:04:24 -04003095 return dentry_has_perm(cred, dentry, FILE__SETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003097 if (selinux_policycap_openperm() &&
Stephen Smalleyccb54472017-05-12 12:41:24 -04003098 inode->i_sb->s_magic != SOCKFS_MAGIC &&
3099 (ia_valid & ATTR_SIZE) &&
3100 !(ia_valid & ATTR_FILE))
Eric Paris95dbf732012-04-04 13:45:34 -04003101 av |= FILE__OPEN;
3102
3103 return dentry_has_perm(cred, dentry, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104}
3105
Al Viro3f7036a2015-03-08 19:28:30 -04003106static int selinux_inode_getattr(const struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107{
Al Viro3f7036a2015-03-08 19:28:30 -04003108 return path_has_perm(current_cred(), path, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109}
3110
Stephen Smalleydb590002017-04-20 11:31:30 -04003111static bool has_cap_mac_admin(bool audit)
3112{
3113 const struct cred *cred = current_cred();
Micah Mortonc1a85a02019-01-07 16:10:53 -08003114 unsigned int opts = audit ? CAP_OPT_NONE : CAP_OPT_NOAUDIT;
Stephen Smalleydb590002017-04-20 11:31:30 -04003115
Micah Mortonc1a85a02019-01-07 16:10:53 -08003116 if (cap_capable(cred, &init_user_ns, CAP_MAC_ADMIN, opts))
Stephen Smalleydb590002017-04-20 11:31:30 -04003117 return false;
Micah Mortonc1a85a02019-01-07 16:10:53 -08003118 if (cred_has_capability(cred, CAP_MAC_ADMIN, opts, true))
Stephen Smalleydb590002017-04-20 11:31:30 -04003119 return false;
3120 return true;
3121}
3122
David Howells8f0cfa52008-04-29 00:59:41 -07003123static int selinux_inode_setxattr(struct dentry *dentry, const char *name,
3124 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125{
David Howellsc6f493d2015-03-17 22:26:22 +00003126 struct inode *inode = d_backing_inode(dentry);
Paul Moore20cdef82016-04-04 14:14:42 -04003127 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 struct superblock_security_struct *sbsec;
Thomas Liu2bf49692009-07-14 12:14:09 -04003129 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11003130 u32 newsid, sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 int rc = 0;
3132
Eric W. Biederman6b240302017-10-02 09:38:20 -05003133 if (strcmp(name, XATTR_NAME_SELINUX)) {
3134 rc = cap_inode_setxattr(dentry, name, value, size, flags);
3135 if (rc)
3136 return rc;
3137
3138 /* Not an attribute we recognize, so just check the
3139 ordinary setattr permission. */
3140 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142
Ondrej Mosnacek65cddd52020-01-07 14:31:53 +01003143 if (!selinux_initialized(&selinux_state))
Jonathan Lebon3e3e24b2019-09-12 09:30:07 -04003144 return (inode_owner_or_capable(inode) ? 0 : -EPERM);
3145
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 sbsec = inode->i_sb->s_security;
Eric Paris12f348b2012-10-09 10:56:25 -04003147 if (!(sbsec->flags & SBLABEL_MNT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 return -EOPNOTSUPP;
3149
Serge E. Hallyn2e149672011-03-23 16:43:26 -07003150 if (!inode_owner_or_capable(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 return -EPERM;
3152
Eric Paris50c205f2012-04-04 15:01:43 -04003153 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04003154 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155
Paul Moore20cdef82016-04-04 14:14:42 -04003156 isec = backing_inode_security(dentry);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003157 rc = avc_has_perm(&selinux_state,
3158 sid, isec->sid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 FILE__RELABELFROM, &ad);
3160 if (rc)
3161 return rc;
3162
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003163 rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3164 GFP_KERNEL);
Stephen Smalley12b29f32008-05-07 13:03:20 -04003165 if (rc == -EINVAL) {
Stephen Smalleydb590002017-04-20 11:31:30 -04003166 if (!has_cap_mac_admin(true)) {
Eric Parisd6ea83e2012-04-04 13:45:49 -04003167 struct audit_buffer *ab;
3168 size_t audit_size;
Eric Parisd6ea83e2012-04-04 13:45:49 -04003169
3170 /* We strip a nul only if it is at the end, otherwise the
3171 * context contains a nul and we should audit that */
Al Viroe3fea3f2012-06-09 08:15:16 +01003172 if (value) {
Colin Ian Kingadd24372017-10-14 13:46:55 +01003173 const char *str = value;
3174
Al Viroe3fea3f2012-06-09 08:15:16 +01003175 if (str[size - 1] == '\0')
3176 audit_size = size - 1;
3177 else
3178 audit_size = size;
3179 } else {
Al Viroe3fea3f2012-06-09 08:15:16 +01003180 audit_size = 0;
3181 }
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04003182 ab = audit_log_start(audit_context(),
3183 GFP_ATOMIC, AUDIT_SELINUX_ERR);
Eric Parisd6ea83e2012-04-04 13:45:49 -04003184 audit_log_format(ab, "op=setxattr invalid_context=");
3185 audit_log_n_untrustedstring(ab, value, audit_size);
3186 audit_log_end(ab);
3187
Stephen Smalley12b29f32008-05-07 13:03:20 -04003188 return rc;
Eric Parisd6ea83e2012-04-04 13:45:49 -04003189 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003190 rc = security_context_to_sid_force(&selinux_state, value,
3191 size, &newsid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04003192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 if (rc)
3194 return rc;
3195
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003196 rc = avc_has_perm(&selinux_state,
3197 sid, newsid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 FILE__RELABELTO, &ad);
3199 if (rc)
3200 return rc;
3201
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003202 rc = security_validate_transition(&selinux_state, isec->sid, newsid,
3203 sid, isec->sclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 if (rc)
3205 return rc;
3206
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003207 return avc_has_perm(&selinux_state,
3208 newsid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 sbsec->sid,
3210 SECCLASS_FILESYSTEM,
3211 FILESYSTEM__ASSOCIATE,
3212 &ad);
3213}
3214
David Howells8f0cfa52008-04-29 00:59:41 -07003215static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
Eric Parisf5269712008-05-14 11:27:45 -04003216 const void *value, size_t size,
David Howells8f0cfa52008-04-29 00:59:41 -07003217 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218{
David Howellsc6f493d2015-03-17 22:26:22 +00003219 struct inode *inode = d_backing_inode(dentry);
Paul Moore20cdef82016-04-04 14:14:42 -04003220 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 u32 newsid;
3222 int rc;
3223
3224 if (strcmp(name, XATTR_NAME_SELINUX)) {
3225 /* Not an attribute we recognize, so nothing to do. */
3226 return;
3227 }
3228
Ondrej Mosnacek65cddd52020-01-07 14:31:53 +01003229 if (!selinux_initialized(&selinux_state)) {
Jonathan Lebon3e3e24b2019-09-12 09:30:07 -04003230 /* If we haven't even been initialized, then we can't validate
3231 * against a policy, so leave the label as invalid. It may
3232 * resolve to a valid label on the next revalidation try if
3233 * we've since initialized.
3234 */
3235 return;
3236 }
3237
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003238 rc = security_context_to_sid_force(&selinux_state, value, size,
3239 &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 if (rc) {
peter enderborgc103a912018-06-12 10:09:03 +02003241 pr_err("SELinux: unable to map context to SID"
Stephen Smalley12b29f32008-05-07 13:03:20 -04003242 "for (%s, %lu), rc=%d\n",
3243 inode->i_sb->s_id, inode->i_ino, -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 return;
3245 }
3246
Paul Moore20cdef82016-04-04 14:14:42 -04003247 isec = backing_inode_security(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003248 spin_lock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003249 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05003251 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003252 spin_unlock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003253
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 return;
3255}
3256
David Howells8f0cfa52008-04-29 00:59:41 -07003257static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
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
Eric Paris828dfe12008-04-17 13:17:49 -04003264static int selinux_inode_listxattr(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265{
David Howells88e67f32008-11-14 10:39:21 +11003266 const struct cred *cred = current_cred();
3267
Eric Paris2875fa02011-04-28 16:04:24 -04003268 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269}
3270
David Howells8f0cfa52008-04-29 00:59:41 -07003271static int selinux_inode_removexattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272{
Eric W. Biederman6b240302017-10-02 09:38:20 -05003273 if (strcmp(name, XATTR_NAME_SELINUX)) {
3274 int rc = cap_inode_removexattr(dentry, name);
3275 if (rc)
3276 return rc;
3277
3278 /* Not an attribute we recognize, so just check the
3279 ordinary setattr permission. */
3280 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282
Stephen Smalley9530a3e2020-08-20 13:00:40 -04003283 if (!selinux_initialized(&selinux_state))
3284 return 0;
3285
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 /* No one is allowed to remove a SELinux security label.
3287 You can change the label, but all data must be labeled. */
3288 return -EACCES;
3289}
3290
Aaron Goidelac5656d2019-08-12 11:20:00 -04003291static int selinux_path_notify(const struct path *path, u64 mask,
3292 unsigned int obj_type)
3293{
3294 int ret;
3295 u32 perm;
3296
3297 struct common_audit_data ad;
3298
3299 ad.type = LSM_AUDIT_DATA_PATH;
3300 ad.u.path = *path;
3301
3302 /*
3303 * Set permission needed based on the type of mark being set.
3304 * Performs an additional check for sb watches.
3305 */
3306 switch (obj_type) {
3307 case FSNOTIFY_OBJ_TYPE_VFSMOUNT:
3308 perm = FILE__WATCH_MOUNT;
3309 break;
3310 case FSNOTIFY_OBJ_TYPE_SB:
3311 perm = FILE__WATCH_SB;
3312 ret = superblock_has_perm(current_cred(), path->dentry->d_sb,
3313 FILESYSTEM__WATCH, &ad);
3314 if (ret)
3315 return ret;
3316 break;
3317 case FSNOTIFY_OBJ_TYPE_INODE:
3318 perm = FILE__WATCH;
3319 break;
3320 default:
3321 return -EINVAL;
3322 }
3323
3324 /* blocking watches require the file:watch_with_perm permission */
3325 if (mask & (ALL_FSNOTIFY_PERM_EVENTS))
3326 perm |= FILE__WATCH_WITH_PERM;
3327
3328 /* watches on read-like events need the file:watch_reads permission */
3329 if (mask & (FS_ACCESS | FS_ACCESS_PERM | FS_CLOSE_NOWRITE))
3330 perm |= FILE__WATCH_READS;
3331
3332 return path_has_perm(current_cred(), path, perm);
3333}
3334
James Morrisd381d8a2005-10-30 14:59:22 -08003335/*
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003336 * Copy the inode security context value to the user.
James Morrisd381d8a2005-10-30 14:59:22 -08003337 *
3338 * Permission check is handled by selinux_inode_getxattr hook.
3339 */
Andreas Gruenbacherea861df2015-12-24 11:09:39 -05003340static int selinux_inode_getsecurity(struct inode *inode, const char *name, void **buffer, bool alloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341{
David P. Quigley42492592008-02-04 22:29:39 -08003342 u32 size;
3343 int error;
3344 char *context = NULL;
Paul Moore20cdef82016-04-04 14:14:42 -04003345 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346
Jonathan Lebonc8e22262020-05-28 10:39:40 -04003347 /*
3348 * If we're not initialized yet, then we can't validate contexts, so
3349 * just let vfs_getxattr fall back to using the on-disk xattr.
3350 */
3351 if (!selinux_initialized(&selinux_state) ||
3352 strcmp(name, XATTR_SELINUX_SUFFIX))
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00003353 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003355 /*
3356 * If the caller has CAP_MAC_ADMIN, then get the raw context
3357 * value even if it is not defined by current policy; otherwise,
3358 * use the in-core value under current policy.
3359 * Use the non-auditing forms of the permission checks since
3360 * getxattr may be called by unprivileged processes commonly
3361 * and lack of permission just means that we fall back to the
3362 * in-core context value, not a denial.
3363 */
Paul Moore20cdef82016-04-04 14:14:42 -04003364 isec = inode_security(inode);
Stephen Smalleydb590002017-04-20 11:31:30 -04003365 if (has_cap_mac_admin(false))
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003366 error = security_sid_to_context_force(&selinux_state,
3367 isec->sid, &context,
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003368 &size);
3369 else
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003370 error = security_sid_to_context(&selinux_state, isec->sid,
3371 &context, &size);
David P. Quigley42492592008-02-04 22:29:39 -08003372 if (error)
3373 return error;
3374 error = size;
3375 if (alloc) {
3376 *buffer = context;
3377 goto out_nofree;
3378 }
3379 kfree(context);
3380out_nofree:
3381 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382}
3383
3384static int selinux_inode_setsecurity(struct inode *inode, const char *name,
Eric Paris828dfe12008-04-17 13:17:49 -04003385 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386{
Paul Moore2c971652016-04-19 16:36:28 -04003387 struct inode_security_struct *isec = inode_security_novalidate(inode);
Ondrej Mosnacek53e0c2a2018-12-21 21:18:53 +01003388 struct superblock_security_struct *sbsec = inode->i_sb->s_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 u32 newsid;
3390 int rc;
3391
3392 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3393 return -EOPNOTSUPP;
3394
Ondrej Mosnacek53e0c2a2018-12-21 21:18:53 +01003395 if (!(sbsec->flags & SBLABEL_MNT))
3396 return -EOPNOTSUPP;
3397
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 if (!value || !size)
3399 return -EACCES;
3400
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003401 rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3402 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 if (rc)
3404 return rc;
3405
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003406 spin_lock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003407 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05003409 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003410 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 return 0;
3412}
3413
3414static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
3415{
3416 const int len = sizeof(XATTR_NAME_SELINUX);
3417 if (buffer && len <= buffer_size)
3418 memcpy(buffer, XATTR_NAME_SELINUX, len);
3419 return len;
3420}
3421
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05003422static void selinux_inode_getsecid(struct inode *inode, u32 *secid)
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02003423{
Andreas Gruenbachere817c2f2016-02-18 12:04:08 +01003424 struct inode_security_struct *isec = inode_security_novalidate(inode);
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02003425 *secid = isec->sid;
3426}
3427
Vivek Goyal56909eb2016-07-13 10:44:48 -04003428static int selinux_inode_copy_up(struct dentry *src, struct cred **new)
3429{
3430 u32 sid;
3431 struct task_security_struct *tsec;
3432 struct cred *new_creds = *new;
3433
3434 if (new_creds == NULL) {
3435 new_creds = prepare_creds();
3436 if (!new_creds)
3437 return -ENOMEM;
3438 }
3439
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07003440 tsec = selinux_cred(new_creds);
Vivek Goyal56909eb2016-07-13 10:44:48 -04003441 /* Get label from overlay inode and set it in create_sid */
3442 selinux_inode_getsecid(d_inode(src), &sid);
3443 tsec->create_sid = sid;
3444 *new = new_creds;
3445 return 0;
3446}
3447
Vivek Goyal19472b62016-07-13 10:44:50 -04003448static int selinux_inode_copy_up_xattr(const char *name)
3449{
3450 /* The copy_up hook above sets the initial context on an inode, but we
3451 * don't then want to overwrite it by blindly copying all the lower
3452 * xattrs up. Instead, we have to filter out SELinux-related xattrs.
3453 */
3454 if (strcmp(name, XATTR_NAME_SELINUX) == 0)
3455 return 1; /* Discard */
3456 /*
3457 * Any other attribute apart from SELINUX is not claimed, supported
3458 * by selinux.
3459 */
3460 return -EOPNOTSUPP;
3461}
3462
Ondrej Mosnacekec882da2019-02-22 15:57:17 +01003463/* kernfs node operations */
3464
YueHaibingc72c4cd2019-03-22 22:04:00 +08003465static int selinux_kernfs_init_security(struct kernfs_node *kn_dir,
3466 struct kernfs_node *kn)
Ondrej Mosnacekec882da2019-02-22 15:57:17 +01003467{
Stephen Smalley169ce0c2019-09-04 10:32:48 -04003468 const struct task_security_struct *tsec = selinux_cred(current_cred());
Ondrej Mosnacekec882da2019-02-22 15:57:17 +01003469 u32 parent_sid, newsid, clen;
3470 int rc;
3471 char *context;
3472
Ondrej Mosnacek1537ad12019-04-03 09:29:41 +02003473 rc = kernfs_xattr_get(kn_dir, XATTR_NAME_SELINUX, NULL, 0);
Ondrej Mosnacekec882da2019-02-22 15:57:17 +01003474 if (rc == -ENODATA)
3475 return 0;
3476 else if (rc < 0)
3477 return rc;
3478
3479 clen = (u32)rc;
3480 context = kmalloc(clen, GFP_KERNEL);
3481 if (!context)
3482 return -ENOMEM;
3483
Ondrej Mosnacek1537ad12019-04-03 09:29:41 +02003484 rc = kernfs_xattr_get(kn_dir, XATTR_NAME_SELINUX, context, clen);
Ondrej Mosnacekec882da2019-02-22 15:57:17 +01003485 if (rc < 0) {
3486 kfree(context);
3487 return rc;
3488 }
3489
3490 rc = security_context_to_sid(&selinux_state, context, clen, &parent_sid,
3491 GFP_KERNEL);
3492 kfree(context);
3493 if (rc)
3494 return rc;
3495
3496 if (tsec->create_sid) {
3497 newsid = tsec->create_sid;
3498 } else {
3499 u16 secclass = inode_mode_to_security_class(kn->mode);
3500 struct qstr q;
3501
3502 q.name = kn->name;
3503 q.hash_len = hashlen_string(kn_dir, kn->name);
3504
3505 rc = security_transition_sid(&selinux_state, tsec->sid,
3506 parent_sid, secclass, &q,
3507 &newsid);
3508 if (rc)
3509 return rc;
3510 }
3511
3512 rc = security_sid_to_context_force(&selinux_state, newsid,
3513 &context, &clen);
3514 if (rc)
3515 return rc;
3516
Ondrej Mosnacek1537ad12019-04-03 09:29:41 +02003517 rc = kernfs_xattr_set(kn, XATTR_NAME_SELINUX, context, clen,
3518 XATTR_CREATE);
Ondrej Mosnacekec882da2019-02-22 15:57:17 +01003519 kfree(context);
3520 return rc;
3521}
3522
3523
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524/* file security operations */
3525
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003526static int selinux_revalidate_file_permission(struct file *file, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527{
David Howells88e67f32008-11-14 10:39:21 +11003528 const struct cred *cred = current_cred();
Al Viro496ad9a2013-01-23 17:07:38 -05003529 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
3532 if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
3533 mask |= MAY_APPEND;
3534
Paul Moore389fb8002009-03-27 17:10:34 -04003535 return file_has_perm(cred, file,
3536 file_mask_to_av(inode->i_mode, mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537}
3538
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003539static int selinux_file_permission(struct file *file, int mask)
3540{
Al Viro496ad9a2013-01-23 17:07:38 -05003541 struct inode *inode = file_inode(file);
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07003542 struct file_security_struct *fsec = selinux_file(file);
Andreas Gruenbacherb1973672016-01-05 23:12:33 +01003543 struct inode_security_struct *isec;
Stephen Smalley20dda182009-06-22 14:54:53 -04003544 u32 sid = current_sid();
3545
Paul Moore389fb8002009-03-27 17:10:34 -04003546 if (!mask)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003547 /* No permission to check. Existence test. */
3548 return 0;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003549
Andreas Gruenbacherb1973672016-01-05 23:12:33 +01003550 isec = inode_security(inode);
Stephen Smalley20dda182009-06-22 14:54:53 -04003551 if (sid == fsec->sid && fsec->isid == isec->sid &&
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003552 fsec->pseqno == avc_policy_seqno(&selinux_state))
Eric Paris83d49852012-04-04 13:45:40 -04003553 /* No change since file_open check. */
Stephen Smalley20dda182009-06-22 14:54:53 -04003554 return 0;
3555
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003556 return selinux_revalidate_file_permission(file, mask);
3557}
3558
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559static int selinux_file_alloc_security(struct file *file)
3560{
Paul Moorecb89e242020-01-10 16:32:10 -05003561 struct file_security_struct *fsec = selinux_file(file);
3562 u32 sid = current_sid();
3563
3564 fsec->sid = sid;
3565 fsec->fown_sid = sid;
3566
3567 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568}
3569
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003570/*
3571 * Check whether a task has the ioctl permission and cmd
3572 * operation to an inode.
3573 */
Geliang Tang1d2a1682015-10-21 17:44:27 -04003574static int ioctl_has_perm(const struct cred *cred, struct file *file,
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003575 u32 requested, u16 cmd)
3576{
3577 struct common_audit_data ad;
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07003578 struct file_security_struct *fsec = selinux_file(file);
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003579 struct inode *inode = file_inode(file);
Paul Moore20cdef82016-04-04 14:14:42 -04003580 struct inode_security_struct *isec;
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003581 struct lsm_ioctlop_audit ioctl;
3582 u32 ssid = cred_sid(cred);
3583 int rc;
3584 u8 driver = cmd >> 8;
3585 u8 xperm = cmd & 0xff;
3586
3587 ad.type = LSM_AUDIT_DATA_IOCTL_OP;
3588 ad.u.op = &ioctl;
3589 ad.u.op->cmd = cmd;
3590 ad.u.op->path = file->f_path;
3591
3592 if (ssid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003593 rc = avc_has_perm(&selinux_state,
3594 ssid, fsec->sid,
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003595 SECCLASS_FD,
3596 FD__USE,
3597 &ad);
3598 if (rc)
3599 goto out;
3600 }
3601
3602 if (unlikely(IS_PRIVATE(inode)))
3603 return 0;
3604
Paul Moore20cdef82016-04-04 14:14:42 -04003605 isec = inode_security(inode);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003606 rc = avc_has_extended_perms(&selinux_state,
3607 ssid, isec->sid, isec->sclass,
3608 requested, driver, xperm, &ad);
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003609out:
3610 return rc;
3611}
3612
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613static int selinux_file_ioctl(struct file *file, unsigned int cmd,
3614 unsigned long arg)
3615{
David Howells88e67f32008-11-14 10:39:21 +11003616 const struct cred *cred = current_cred();
Eric Paris0b24dcb2011-02-25 15:39:20 -05003617 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618
Eric Paris0b24dcb2011-02-25 15:39:20 -05003619 switch (cmd) {
3620 case FIONREAD:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003621 case FIBMAP:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003622 case FIGETBSZ:
Al Viro2f99c362012-03-23 16:04:05 -04003623 case FS_IOC_GETFLAGS:
Al Viro2f99c362012-03-23 16:04:05 -04003624 case FS_IOC_GETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003625 error = file_has_perm(cred, file, FILE__GETATTR);
3626 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627
Al Viro2f99c362012-03-23 16:04:05 -04003628 case FS_IOC_SETFLAGS:
Al Viro2f99c362012-03-23 16:04:05 -04003629 case FS_IOC_SETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003630 error = file_has_perm(cred, file, FILE__SETATTR);
3631 break;
3632
3633 /* sys_ioctl() checks */
3634 case FIONBIO:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003635 case FIOASYNC:
3636 error = file_has_perm(cred, file, 0);
3637 break;
3638
3639 case KDSKBENT:
3640 case KDSKBSENT:
Eric Paris6a9de492012-01-03 12:25:14 -05003641 error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
Micah Mortonc1a85a02019-01-07 16:10:53 -08003642 CAP_OPT_NONE, true);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003643 break;
3644
3645 /* default case assumes that the command will go
3646 * to the file's ioctl() function.
3647 */
3648 default:
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003649 error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003650 }
3651 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652}
3653
Stephen Smalleyb78b7d52020-01-08 12:23:56 -05003654static int default_noexec __ro_after_init;
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003655
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
3657{
David Howells88e67f32008-11-14 10:39:21 +11003658 const struct cred *cred = current_cred();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003659 u32 sid = cred_sid(cred);
David Howellsd84f4f92008-11-14 10:39:23 +11003660 int rc = 0;
David Howells88e67f32008-11-14 10:39:21 +11003661
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003662 if (default_noexec &&
Stephen Smalley892e8ca2015-07-10 09:40:59 -04003663 (prot & PROT_EXEC) && (!file || IS_PRIVATE(file_inode(file)) ||
3664 (!shared && (prot & PROT_WRITE)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 /*
3666 * We are making executable an anonymous mapping or a
3667 * private file mapping that will also be writable.
3668 * This has an additional check.
3669 */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003670 rc = avc_has_perm(&selinux_state,
3671 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003672 PROCESS__EXECMEM, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 if (rc)
David Howellsd84f4f92008-11-14 10:39:23 +11003674 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676
3677 if (file) {
3678 /* read access is always possible with a mapping */
3679 u32 av = FILE__READ;
3680
3681 /* write access only matters if the mapping is shared */
3682 if (shared && (prot & PROT_WRITE))
3683 av |= FILE__WRITE;
3684
3685 if (prot & PROT_EXEC)
3686 av |= FILE__EXECUTE;
3687
David Howells88e67f32008-11-14 10:39:21 +11003688 return file_has_perm(cred, file, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 }
David Howellsd84f4f92008-11-14 10:39:23 +11003690
3691error:
3692 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693}
3694
Al Viroe5467852012-05-30 13:30:51 -04003695static int selinux_mmap_addr(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07003697 int rc = 0;
Paul Moore98883bf2014-03-19 16:46:11 -04003698
3699 if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
3700 u32 sid = current_sid();
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003701 rc = avc_has_perm(&selinux_state,
3702 sid, sid, SECCLASS_MEMPROTECT,
Paul Moore98883bf2014-03-19 16:46:11 -04003703 MEMPROTECT__MMAP_ZERO, NULL);
3704 }
3705
3706 return rc;
Al Viroe5467852012-05-30 13:30:51 -04003707}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708
Al Viroe5467852012-05-30 13:30:51 -04003709static int selinux_mmap_file(struct file *file, unsigned long reqprot,
3710 unsigned long prot, unsigned long flags)
3711{
Stephen Smalley3ba4bf52017-05-05 09:14:48 -04003712 struct common_audit_data ad;
3713 int rc;
3714
3715 if (file) {
3716 ad.type = LSM_AUDIT_DATA_FILE;
3717 ad.u.file = file;
3718 rc = inode_has_perm(current_cred(), file_inode(file),
3719 FILE__MAP, &ad);
3720 if (rc)
3721 return rc;
3722 }
3723
Lakshmi Ramasubramanian8861d0a2020-09-14 10:31:57 -07003724 if (checkreqprot_get(&selinux_state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 prot = reqprot;
3726
3727 return file_map_prot_check(file, prot,
3728 (flags & MAP_TYPE) == MAP_SHARED);
3729}
3730
3731static int selinux_file_mprotect(struct vm_area_struct *vma,
3732 unsigned long reqprot,
3733 unsigned long prot)
3734{
David Howells88e67f32008-11-14 10:39:21 +11003735 const struct cred *cred = current_cred();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003736 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737
Lakshmi Ramasubramanian8861d0a2020-09-14 10:31:57 -07003738 if (checkreqprot_get(&selinux_state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 prot = reqprot;
3740
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003741 if (default_noexec &&
3742 (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
James Morrisd541bbe2009-01-29 12:19:51 +11003743 int rc = 0;
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003744 if (vma->vm_start >= vma->vm_mm->start_brk &&
3745 vma->vm_end <= vma->vm_mm->brk) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003746 rc = avc_has_perm(&selinux_state,
3747 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003748 PROCESS__EXECHEAP, NULL);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003749 } else if (!vma->vm_file &&
Stephen Smalleyc2316db2016-04-08 13:55:03 -04003750 ((vma->vm_start <= vma->vm_mm->start_stack &&
3751 vma->vm_end >= vma->vm_mm->start_stack) ||
Andy Lutomirskid17af502016-09-30 10:58:58 -07003752 vma_is_stack_for_current(vma))) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003753 rc = avc_has_perm(&selinux_state,
3754 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003755 PROCESS__EXECSTACK, NULL);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003756 } else if (vma->vm_file && vma->anon_vma) {
3757 /*
3758 * We are making executable a file mapping that has
3759 * had some COW done. Since pages might have been
3760 * written, check ability to execute the possibly
3761 * modified content. This typically should only
3762 * occur for text relocations.
3763 */
David Howellsd84f4f92008-11-14 10:39:23 +11003764 rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003765 }
Lorenzo Hernandez García-Hierro6b992192005-06-25 14:54:34 -07003766 if (rc)
3767 return rc;
3768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769
3770 return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
3771}
3772
3773static int selinux_file_lock(struct file *file, unsigned int cmd)
3774{
David Howells88e67f32008-11-14 10:39:21 +11003775 const struct cred *cred = current_cred();
3776
3777 return file_has_perm(cred, file, FILE__LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778}
3779
3780static int selinux_file_fcntl(struct file *file, unsigned int cmd,
3781 unsigned long arg)
3782{
David Howells88e67f32008-11-14 10:39:21 +11003783 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 int err = 0;
3785
3786 switch (cmd) {
Eric Paris828dfe12008-04-17 13:17:49 -04003787 case F_SETFL:
Eric Paris828dfe12008-04-17 13:17:49 -04003788 if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
David Howells88e67f32008-11-14 10:39:21 +11003789 err = file_has_perm(cred, file, FILE__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003791 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003792 fallthrough;
Eric Paris828dfe12008-04-17 13:17:49 -04003793 case F_SETOWN:
3794 case F_SETSIG:
3795 case F_GETFL:
3796 case F_GETOWN:
3797 case F_GETSIG:
Cyrill Gorcunov1d151c32012-07-30 14:43:00 -07003798 case F_GETOWNER_UIDS:
Eric Paris828dfe12008-04-17 13:17:49 -04003799 /* Just check FD__USE permission */
David Howells88e67f32008-11-14 10:39:21 +11003800 err = file_has_perm(cred, file, 0);
Eric Paris828dfe12008-04-17 13:17:49 -04003801 break;
3802 case F_GETLK:
3803 case F_SETLK:
3804 case F_SETLKW:
Jeff Layton0d3f7a22014-04-22 08:23:58 -04003805 case F_OFD_GETLK:
3806 case F_OFD_SETLK:
3807 case F_OFD_SETLKW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808#if BITS_PER_LONG == 32
Eric Paris828dfe12008-04-17 13:17:49 -04003809 case F_GETLK64:
3810 case F_SETLK64:
3811 case F_SETLKW64:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812#endif
David Howells88e67f32008-11-14 10:39:21 +11003813 err = file_has_perm(cred, file, FILE__LOCK);
Eric Paris828dfe12008-04-17 13:17:49 -04003814 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 }
3816
3817 return err;
3818}
3819
Jeff Laytone0b93ed2014-08-22 11:27:32 -04003820static void selinux_file_set_fowner(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 struct file_security_struct *fsec;
3823
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07003824 fsec = selinux_file(file);
David Howells275bb412008-11-14 10:39:19 +11003825 fsec->fown_sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826}
3827
3828static int selinux_file_send_sigiotask(struct task_struct *tsk,
3829 struct fown_struct *fown, int signum)
3830{
Eric Paris828dfe12008-04-17 13:17:49 -04003831 struct file *file;
Stephen Smalley65c90bc2009-05-04 15:43:18 -04003832 u32 sid = task_sid(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 struct file_security_struct *fsec;
3835
3836 /* struct fown_struct is never outside the context of a struct file */
Eric Paris828dfe12008-04-17 13:17:49 -04003837 file = container_of(fown, struct file, f_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07003839 fsec = selinux_file(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840
3841 if (!signum)
3842 perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
3843 else
3844 perm = signal_to_av(signum);
3845
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003846 return avc_has_perm(&selinux_state,
3847 fsec->fown_sid, sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 SECCLASS_PROCESS, perm, NULL);
3849}
3850
3851static int selinux_file_receive(struct file *file)
3852{
David Howells88e67f32008-11-14 10:39:21 +11003853 const struct cred *cred = current_cred();
3854
3855 return file_has_perm(cred, file, file_to_av(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856}
3857
Al Viro94817692018-07-10 14:13:18 -04003858static int selinux_file_open(struct file *file)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003859{
3860 struct file_security_struct *fsec;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003861 struct inode_security_struct *isec;
David Howellsd84f4f92008-11-14 10:39:23 +11003862
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07003863 fsec = selinux_file(file);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05003864 isec = inode_security(file_inode(file));
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003865 /*
3866 * Save inode label and policy sequence number
3867 * at open-time so that selinux_file_permission
3868 * can determine whether revalidation is necessary.
3869 * Task label is already saved in the file security
3870 * struct as its SID.
3871 */
3872 fsec->isid = isec->sid;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003873 fsec->pseqno = avc_policy_seqno(&selinux_state);
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003874 /*
3875 * Since the inode label or policy seqno may have changed
3876 * between the selinux_inode_permission check and the saving
3877 * of state above, recheck that access is still permitted.
3878 * Otherwise, access might never be revalidated against the
3879 * new inode label or new policy.
3880 * This check is not redundant - do not remove.
3881 */
Al Viro94817692018-07-10 14:13:18 -04003882 return file_path_has_perm(file->f_cred, file, open_file_to_av(file));
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003883}
3884
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885/* task security operations */
3886
Tetsuo Handaa79be232017-03-28 23:08:45 +09003887static int selinux_task_alloc(struct task_struct *task,
3888 unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003890 u32 sid = current_sid();
3891
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003892 return avc_has_perm(&selinux_state,
3893 sid, sid, SECCLASS_PROCESS, PROCESS__FORK, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894}
3895
David Howellsf1752ee2008-11-14 10:39:17 +11003896/*
David Howellsd84f4f92008-11-14 10:39:23 +11003897 * prepare a new set of credentials for modification
3898 */
3899static int selinux_cred_prepare(struct cred *new, const struct cred *old,
3900 gfp_t gfp)
3901{
Casey Schauflerbbd36622018-11-12 09:30:56 -08003902 const struct task_security_struct *old_tsec = selinux_cred(old);
3903 struct task_security_struct *tsec = selinux_cred(new);
David Howellsd84f4f92008-11-14 10:39:23 +11003904
Casey Schauflerbbd36622018-11-12 09:30:56 -08003905 *tsec = *old_tsec;
David Howellsd84f4f92008-11-14 10:39:23 +11003906 return 0;
3907}
3908
3909/*
David Howellsee18d642009-09-02 09:14:21 +01003910 * transfer the SELinux data to a blank set of creds
3911 */
3912static void selinux_cred_transfer(struct cred *new, const struct cred *old)
3913{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07003914 const struct task_security_struct *old_tsec = selinux_cred(old);
3915 struct task_security_struct *tsec = selinux_cred(new);
David Howellsee18d642009-09-02 09:14:21 +01003916
3917 *tsec = *old_tsec;
3918}
3919
Matthew Garrett3ec30112018-01-08 13:36:19 -08003920static void selinux_cred_getsecid(const struct cred *c, u32 *secid)
3921{
3922 *secid = cred_sid(c);
3923}
3924
David Howellsee18d642009-09-02 09:14:21 +01003925/*
David Howells3a3b7ce2008-11-14 10:39:28 +11003926 * set the security data for a kernel service
3927 * - all the creation contexts are set to unlabelled
3928 */
3929static int selinux_kernel_act_as(struct cred *new, u32 secid)
3930{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07003931 struct task_security_struct *tsec = selinux_cred(new);
David Howells3a3b7ce2008-11-14 10:39:28 +11003932 u32 sid = current_sid();
3933 int ret;
3934
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003935 ret = avc_has_perm(&selinux_state,
3936 sid, secid,
David Howells3a3b7ce2008-11-14 10:39:28 +11003937 SECCLASS_KERNEL_SERVICE,
3938 KERNEL_SERVICE__USE_AS_OVERRIDE,
3939 NULL);
3940 if (ret == 0) {
3941 tsec->sid = secid;
3942 tsec->create_sid = 0;
3943 tsec->keycreate_sid = 0;
3944 tsec->sockcreate_sid = 0;
3945 }
3946 return ret;
3947}
3948
3949/*
3950 * set the file creation context in a security record to the same as the
3951 * objective context of the specified inode
3952 */
3953static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
3954{
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05003955 struct inode_security_struct *isec = inode_security(inode);
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07003956 struct task_security_struct *tsec = selinux_cred(new);
David Howells3a3b7ce2008-11-14 10:39:28 +11003957 u32 sid = current_sid();
3958 int ret;
3959
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003960 ret = avc_has_perm(&selinux_state,
3961 sid, isec->sid,
David Howells3a3b7ce2008-11-14 10:39:28 +11003962 SECCLASS_KERNEL_SERVICE,
3963 KERNEL_SERVICE__CREATE_FILES_AS,
3964 NULL);
3965
3966 if (ret == 0)
3967 tsec->create_sid = isec->sid;
David Howellsef574712010-02-26 01:56:16 +00003968 return ret;
David Howells3a3b7ce2008-11-14 10:39:28 +11003969}
3970
Eric Parisdd8dbf22009-11-03 16:35:32 +11003971static int selinux_kernel_module_request(char *kmod_name)
Eric Paris25354c42009-08-13 09:45:03 -04003972{
Eric Parisdd8dbf22009-11-03 16:35:32 +11003973 struct common_audit_data ad;
3974
Eric Paris50c205f2012-04-04 15:01:43 -04003975 ad.type = LSM_AUDIT_DATA_KMOD;
Eric Parisdd8dbf22009-11-03 16:35:32 +11003976 ad.u.kmod_name = kmod_name;
3977
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003978 return avc_has_perm(&selinux_state,
3979 current_sid(), SECINITSID_KERNEL, SECCLASS_SYSTEM,
Eric Parisdd8dbf22009-11-03 16:35:32 +11003980 SYSTEM__MODULE_REQUEST, &ad);
Eric Paris25354c42009-08-13 09:45:03 -04003981}
3982
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003983static int selinux_kernel_module_from_file(struct file *file)
3984{
3985 struct common_audit_data ad;
3986 struct inode_security_struct *isec;
3987 struct file_security_struct *fsec;
3988 u32 sid = current_sid();
3989 int rc;
3990
3991 /* init_module */
3992 if (file == NULL)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003993 return avc_has_perm(&selinux_state,
3994 sid, sid, SECCLASS_SYSTEM,
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07003995 SYSTEM__MODULE_LOAD, NULL);
3996
3997 /* finit_module */
Paul Moore20cdef82016-04-04 14:14:42 -04003998
Vivek Goyal43af5de2016-09-09 11:37:49 -04003999 ad.type = LSM_AUDIT_DATA_FILE;
4000 ad.u.file = file;
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07004001
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07004002 fsec = selinux_file(file);
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07004003 if (sid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004004 rc = avc_has_perm(&selinux_state,
4005 sid, fsec->sid, SECCLASS_FD, FD__USE, &ad);
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07004006 if (rc)
4007 return rc;
4008 }
4009
Paul Moore20cdef82016-04-04 14:14:42 -04004010 isec = inode_security(file_inode(file));
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004011 return avc_has_perm(&selinux_state,
4012 sid, isec->sid, SECCLASS_SYSTEM,
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07004013 SYSTEM__MODULE_LOAD, &ad);
4014}
4015
4016static int selinux_kernel_read_file(struct file *file,
Kees Cook2039bda2020-10-02 10:38:23 -07004017 enum kernel_read_file_id id,
4018 bool contents)
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07004019{
4020 int rc = 0;
4021
4022 switch (id) {
4023 case READING_MODULE:
Kees Cook2039bda2020-10-02 10:38:23 -07004024 rc = selinux_kernel_module_from_file(contents ? file : NULL);
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07004025 break;
4026 default:
4027 break;
4028 }
4029
4030 return rc;
4031}
4032
Kees Cookb64fcae2020-10-02 10:38:20 -07004033static int selinux_kernel_load_data(enum kernel_load_data_id id, bool contents)
Mimi Zoharc77b8cd2018-07-13 14:06:02 -04004034{
4035 int rc = 0;
4036
4037 switch (id) {
4038 case LOADING_MODULE:
4039 rc = selinux_kernel_module_from_file(NULL);
Gustavo A. R. Silvab2d99bc2020-11-20 12:32:26 -06004040 break;
Mimi Zoharc77b8cd2018-07-13 14:06:02 -04004041 default:
4042 break;
4043 }
4044
4045 return rc;
4046}
4047
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
4049{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004050 return avc_has_perm(&selinux_state,
4051 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004052 PROCESS__SETPGID, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053}
4054
4055static int selinux_task_getpgid(struct task_struct *p)
4056{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004057 return avc_has_perm(&selinux_state,
4058 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004059 PROCESS__GETPGID, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060}
4061
4062static int selinux_task_getsid(struct task_struct *p)
4063{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004064 return avc_has_perm(&selinux_state,
4065 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004066 PROCESS__GETSESSION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067}
4068
David Quigleyf9008e4c2006-06-30 01:55:46 -07004069static void selinux_task_getsecid(struct task_struct *p, u32 *secid)
4070{
David Howells275bb412008-11-14 10:39:19 +11004071 *secid = task_sid(p);
David Quigleyf9008e4c2006-06-30 01:55:46 -07004072}
4073
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074static int selinux_task_setnice(struct task_struct *p, int nice)
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__SETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079}
4080
James Morris03e68062006-06-23 02:03:58 -07004081static int selinux_task_setioprio(struct task_struct *p, int ioprio)
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);
James Morris03e68062006-06-23 02:03:58 -07004086}
4087
David Quigleya1836a42006-06-30 01:55:49 -07004088static int selinux_task_getioprio(struct task_struct *p)
4089{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004090 return avc_has_perm(&selinux_state,
4091 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004092 PROCESS__GETSCHED, NULL);
David Quigleya1836a42006-06-30 01:55:49 -07004093}
4094
Corentin LABBE42985552017-10-04 20:32:18 +02004095static int selinux_task_prlimit(const struct cred *cred, const struct cred *tcred,
4096 unsigned int flags)
Stephen Smalley791ec492017-02-17 07:57:00 -05004097{
4098 u32 av = 0;
4099
Stephen Smalley84e68852017-02-28 09:35:08 -05004100 if (!flags)
4101 return 0;
Stephen Smalley791ec492017-02-17 07:57:00 -05004102 if (flags & LSM_PRLIMIT_WRITE)
4103 av |= PROCESS__SETRLIMIT;
4104 if (flags & LSM_PRLIMIT_READ)
4105 av |= PROCESS__GETRLIMIT;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004106 return avc_has_perm(&selinux_state,
4107 cred_sid(cred), cred_sid(tcred),
Stephen Smalley791ec492017-02-17 07:57:00 -05004108 SECCLASS_PROCESS, av, NULL);
4109}
4110
Jiri Slaby8fd00b42009-08-26 18:41:16 +02004111static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
4112 struct rlimit *new_rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113{
Jiri Slaby8fd00b42009-08-26 18:41:16 +02004114 struct rlimit *old_rlim = p->signal->rlim + resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115
4116 /* Control the ability to change the hard limit (whether
4117 lowering or raising it), so that the hard limit can
4118 later be used as a safe reset point for the soft limit
David Howellsd84f4f92008-11-14 10:39:23 +11004119 upon context transitions. See selinux_bprm_committing_creds. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 if (old_rlim->rlim_max != new_rlim->rlim_max)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004121 return avc_has_perm(&selinux_state,
4122 current_sid(), task_sid(p),
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004123 SECCLASS_PROCESS, PROCESS__SETRLIMIT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124
4125 return 0;
4126}
4127
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09004128static int selinux_task_setscheduler(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004130 return avc_has_perm(&selinux_state,
4131 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004132 PROCESS__SETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133}
4134
4135static int selinux_task_getscheduler(struct task_struct *p)
4136{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004137 return avc_has_perm(&selinux_state,
4138 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004139 PROCESS__GETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140}
4141
David Quigley35601542006-06-23 02:04:01 -07004142static int selinux_task_movememory(struct task_struct *p)
4143{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004144 return avc_has_perm(&selinux_state,
4145 current_sid(), task_sid(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004146 PROCESS__SETSCHED, NULL);
David Quigley35601542006-06-23 02:04:01 -07004147}
4148
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02004149static int selinux_task_kill(struct task_struct *p, struct kernel_siginfo *info,
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004150 int sig, const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151{
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004152 u32 secid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 if (!sig)
4156 perm = PROCESS__SIGNULL; /* null signal; existence test */
4157 else
4158 perm = signal_to_av(sig);
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004159 if (!cred)
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004160 secid = current_sid();
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004161 else
4162 secid = cred_sid(cred);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004163 return avc_has_perm(&selinux_state,
4164 secid, task_sid(p), SECCLASS_PROCESS, perm, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165}
4166
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167static void selinux_task_to_inode(struct task_struct *p,
4168 struct inode *inode)
4169{
Casey Schaufler80788c22018-09-21 17:19:11 -07004170 struct inode_security_struct *isec = selinux_inode(inode);
David Howells275bb412008-11-14 10:39:19 +11004171 u32 sid = task_sid(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004173 spin_lock(&isec->lock);
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01004174 isec->sclass = inode_mode_to_security_class(inode->i_mode);
David Howells275bb412008-11-14 10:39:19 +11004175 isec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004176 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004177 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178}
4179
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004181static int selinux_parse_skb_ipv4(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04004182 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183{
4184 int offset, ihlen, ret = -EINVAL;
4185 struct iphdr _iph, *ih;
4186
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03004187 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
4189 if (ih == NULL)
4190 goto out;
4191
4192 ihlen = ih->ihl * 4;
4193 if (ihlen < sizeof(_iph))
4194 goto out;
4195
Eric Paris48c62af2012-04-02 13:15:44 -04004196 ad->u.net->v4info.saddr = ih->saddr;
4197 ad->u.net->v4info.daddr = ih->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 ret = 0;
4199
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004200 if (proto)
4201 *proto = ih->protocol;
4202
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 switch (ih->protocol) {
Eric Paris828dfe12008-04-17 13:17:49 -04004204 case IPPROTO_TCP: {
4205 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206
Eric Paris828dfe12008-04-17 13:17:49 -04004207 if (ntohs(ih->frag_off) & IP_OFFSET)
4208 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209
4210 offset += ihlen;
4211 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4212 if (th == NULL)
4213 break;
4214
Eric Paris48c62af2012-04-02 13:15:44 -04004215 ad->u.net->sport = th->source;
4216 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219
Eric Paris828dfe12008-04-17 13:17:49 -04004220 case IPPROTO_UDP: {
4221 struct udphdr _udph, *uh;
4222
4223 if (ntohs(ih->frag_off) & IP_OFFSET)
4224 break;
4225
4226 offset += ihlen;
4227 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4228 if (uh == NULL)
4229 break;
4230
Eric Paris48c62af2012-04-02 13:15:44 -04004231 ad->u.net->sport = uh->source;
4232 ad->u.net->dport = uh->dest;
Eric Paris828dfe12008-04-17 13:17:49 -04004233 break;
4234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235
James Morris2ee92d42006-11-13 16:09:01 -08004236 case IPPROTO_DCCP: {
4237 struct dccp_hdr _dccph, *dh;
4238
4239 if (ntohs(ih->frag_off) & IP_OFFSET)
4240 break;
4241
4242 offset += ihlen;
4243 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4244 if (dh == NULL)
4245 break;
4246
Eric Paris48c62af2012-04-02 13:15:44 -04004247 ad->u.net->sport = dh->dccph_sport;
4248 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08004249 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004250 }
James Morris2ee92d42006-11-13 16:09:01 -08004251
Richard Hainesd4529302018-02-13 20:57:18 +00004252#if IS_ENABLED(CONFIG_IP_SCTP)
4253 case IPPROTO_SCTP: {
4254 struct sctphdr _sctph, *sh;
4255
4256 if (ntohs(ih->frag_off) & IP_OFFSET)
4257 break;
4258
4259 offset += ihlen;
4260 sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4261 if (sh == NULL)
4262 break;
4263
4264 ad->u.net->sport = sh->source;
4265 ad->u.net->dport = sh->dest;
4266 break;
4267 }
4268#endif
Eric Paris828dfe12008-04-17 13:17:49 -04004269 default:
4270 break;
4271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272out:
4273 return ret;
4274}
4275
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04004276#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277
4278/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004279static int selinux_parse_skb_ipv6(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04004280 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281{
4282 u8 nexthdr;
4283 int ret = -EINVAL, offset;
4284 struct ipv6hdr _ipv6h, *ip6;
Jesse Gross75f28112011-11-30 17:05:51 -08004285 __be16 frag_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03004287 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
4289 if (ip6 == NULL)
4290 goto out;
4291
Eric Paris48c62af2012-04-02 13:15:44 -04004292 ad->u.net->v6info.saddr = ip6->saddr;
4293 ad->u.net->v6info.daddr = ip6->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 ret = 0;
4295
4296 nexthdr = ip6->nexthdr;
4297 offset += sizeof(_ipv6h);
Jesse Gross75f28112011-11-30 17:05:51 -08004298 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 if (offset < 0)
4300 goto out;
4301
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004302 if (proto)
4303 *proto = nexthdr;
4304
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 switch (nexthdr) {
4306 case IPPROTO_TCP: {
Eric Paris828dfe12008-04-17 13:17:49 -04004307 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308
4309 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4310 if (th == NULL)
4311 break;
4312
Eric Paris48c62af2012-04-02 13:15:44 -04004313 ad->u.net->sport = th->source;
4314 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 break;
4316 }
4317
4318 case IPPROTO_UDP: {
4319 struct udphdr _udph, *uh;
4320
4321 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4322 if (uh == NULL)
4323 break;
4324
Eric Paris48c62af2012-04-02 13:15:44 -04004325 ad->u.net->sport = uh->source;
4326 ad->u.net->dport = uh->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 break;
4328 }
4329
James Morris2ee92d42006-11-13 16:09:01 -08004330 case IPPROTO_DCCP: {
4331 struct dccp_hdr _dccph, *dh;
4332
4333 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4334 if (dh == NULL)
4335 break;
4336
Eric Paris48c62af2012-04-02 13:15:44 -04004337 ad->u.net->sport = dh->dccph_sport;
4338 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08004339 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004340 }
James Morris2ee92d42006-11-13 16:09:01 -08004341
Richard Hainesd4529302018-02-13 20:57:18 +00004342#if IS_ENABLED(CONFIG_IP_SCTP)
4343 case IPPROTO_SCTP: {
4344 struct sctphdr _sctph, *sh;
4345
4346 sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4347 if (sh == NULL)
4348 break;
4349
4350 ad->u.net->sport = sh->source;
4351 ad->u.net->dport = sh->dest;
4352 break;
4353 }
4354#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 /* includes fragments */
4356 default:
4357 break;
4358 }
4359out:
4360 return ret;
4361}
4362
4363#endif /* IPV6 */
4364
Thomas Liu2bf49692009-07-14 12:14:09 -04004365static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
David Howellscf9481e2008-07-27 21:31:07 +10004366 char **_addrp, int src, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367{
David Howellscf9481e2008-07-27 21:31:07 +10004368 char *addrp;
4369 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370
Eric Paris48c62af2012-04-02 13:15:44 -04004371 switch (ad->u.net->family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372 case PF_INET:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004373 ret = selinux_parse_skb_ipv4(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10004374 if (ret)
4375 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04004376 addrp = (char *)(src ? &ad->u.net->v4info.saddr :
4377 &ad->u.net->v4info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10004378 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04004380#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 case PF_INET6:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004382 ret = selinux_parse_skb_ipv6(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10004383 if (ret)
4384 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04004385 addrp = (char *)(src ? &ad->u.net->v6info.saddr :
4386 &ad->u.net->v6info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10004387 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388#endif /* IPV6 */
4389 default:
David Howellscf9481e2008-07-27 21:31:07 +10004390 addrp = NULL;
4391 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 }
4393
David Howellscf9481e2008-07-27 21:31:07 +10004394parse_error:
peter enderborgc103a912018-06-12 10:09:03 +02004395 pr_warn(
David Howellscf9481e2008-07-27 21:31:07 +10004396 "SELinux: failure in selinux_parse_skb(),"
4397 " unable to parse packet\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 return ret;
David Howellscf9481e2008-07-27 21:31:07 +10004399
4400okay:
4401 if (_addrp)
4402 *_addrp = addrp;
4403 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404}
4405
Paul Moore4f6a9932007-03-01 14:35:22 -05004406/**
Paul Moore220deb92008-01-29 08:38:23 -05004407 * selinux_skb_peerlbl_sid - Determine the peer label of a packet
Paul Moore4f6a9932007-03-01 14:35:22 -05004408 * @skb: the packet
Paul Moore75e22912008-01-29 08:38:04 -05004409 * @family: protocol family
Paul Moore220deb92008-01-29 08:38:23 -05004410 * @sid: the packet's peer label SID
Paul Moore4f6a9932007-03-01 14:35:22 -05004411 *
4412 * Description:
Paul Moore220deb92008-01-29 08:38:23 -05004413 * Check the various different forms of network peer labeling and determine
4414 * the peer label/SID for the packet; most of the magic actually occurs in
4415 * the security server function security_net_peersid_cmp(). The function
4416 * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
4417 * or -EACCES if @sid is invalid due to inconsistencies with the different
4418 * peer labels.
Paul Moore4f6a9932007-03-01 14:35:22 -05004419 *
4420 */
Paul Moore220deb92008-01-29 08:38:23 -05004421static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
Paul Moore4f6a9932007-03-01 14:35:22 -05004422{
Paul Moore71f1cb02008-01-29 08:51:16 -05004423 int err;
Paul Moore4f6a9932007-03-01 14:35:22 -05004424 u32 xfrm_sid;
4425 u32 nlbl_sid;
Paul Moore220deb92008-01-29 08:38:23 -05004426 u32 nlbl_type;
Paul Moore4f6a9932007-03-01 14:35:22 -05004427
Paul Moore817eff72013-12-10 14:57:54 -05004428 err = selinux_xfrm_skb_sid(skb, &xfrm_sid);
Paul Moorebed4d7e2013-07-23 17:38:40 -04004429 if (unlikely(err))
4430 return -EACCES;
4431 err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
4432 if (unlikely(err))
4433 return -EACCES;
Paul Moore220deb92008-01-29 08:38:23 -05004434
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004435 err = security_net_peersid_resolve(&selinux_state, nlbl_sid,
4436 nlbl_type, xfrm_sid, sid);
Paul Moore71f1cb02008-01-29 08:51:16 -05004437 if (unlikely(err)) {
peter enderborgc103a912018-06-12 10:09:03 +02004438 pr_warn(
Paul Moore71f1cb02008-01-29 08:51:16 -05004439 "SELinux: failure in selinux_skb_peerlbl_sid(),"
4440 " unable to determine packet's peer label\n");
Paul Moore220deb92008-01-29 08:38:23 -05004441 return -EACCES;
Paul Moore71f1cb02008-01-29 08:51:16 -05004442 }
Paul Moore220deb92008-01-29 08:38:23 -05004443
4444 return 0;
Paul Moore4f6a9932007-03-01 14:35:22 -05004445}
4446
Paul Moore446b8022013-12-04 16:10:51 -05004447/**
4448 * selinux_conn_sid - Determine the child socket label for a connection
4449 * @sk_sid: the parent socket's SID
4450 * @skb_sid: the packet's SID
4451 * @conn_sid: the resulting connection SID
4452 *
4453 * If @skb_sid is valid then the user:role:type information from @sk_sid is
4454 * combined with the MLS information from @skb_sid in order to create
Randy Dunlapc76a2f92020-08-07 09:51:34 -07004455 * @conn_sid. If @skb_sid is not valid then @conn_sid is simply a copy
Paul Moore446b8022013-12-04 16:10:51 -05004456 * of @sk_sid. Returns zero on success, negative values on failure.
4457 *
4458 */
4459static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid)
4460{
4461 int err = 0;
4462
4463 if (skb_sid != SECSID_NULL)
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004464 err = security_sid_mls_copy(&selinux_state, sk_sid, skb_sid,
4465 conn_sid);
Paul Moore446b8022013-12-04 16:10:51 -05004466 else
4467 *conn_sid = sk_sid;
4468
4469 return err;
4470}
4471
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472/* socket security operations */
Paul Moored4f2d972010-04-22 14:46:18 -04004473
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004474static int socket_sockcreate_sid(const struct task_security_struct *tsec,
4475 u16 secclass, u32 *socksid)
Paul Moored4f2d972010-04-22 14:46:18 -04004476{
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004477 if (tsec->sockcreate_sid > SECSID_NULL) {
4478 *socksid = tsec->sockcreate_sid;
4479 return 0;
4480 }
4481
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004482 return security_transition_sid(&selinux_state, tsec->sid, tsec->sid,
4483 secclass, NULL, socksid);
Paul Moored4f2d972010-04-22 14:46:18 -04004484}
4485
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004486static int sock_has_perm(struct sock *sk, u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487{
Paul Moore253bfae2010-04-22 14:46:19 -04004488 struct sk_security_struct *sksec = sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004489 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004490 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491
Paul Moore253bfae2010-04-22 14:46:19 -04004492 if (sksec->sid == SECINITSID_KERNEL)
4493 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494
Eric Paris50c205f2012-04-04 15:01:43 -04004495 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004496 ad.u.net = &net;
4497 ad.u.net->sk = sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004499 return avc_has_perm(&selinux_state,
4500 current_sid(), sksec->sid, sksec->sclass, perms,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004501 &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502}
4503
4504static int selinux_socket_create(int family, int type,
4505 int protocol, int kern)
4506{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07004507 const struct task_security_struct *tsec = selinux_cred(current_cred());
Paul Moored4f2d972010-04-22 14:46:18 -04004508 u32 newsid;
David Howells275bb412008-11-14 10:39:19 +11004509 u16 secclass;
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004510 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511
4512 if (kern)
Paul Moored4f2d972010-04-22 14:46:18 -04004513 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514
David Howells275bb412008-11-14 10:39:19 +11004515 secclass = socket_type_to_security_class(family, type, protocol);
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004516 rc = socket_sockcreate_sid(tsec, secclass, &newsid);
4517 if (rc)
4518 return rc;
4519
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004520 return avc_has_perm(&selinux_state,
4521 tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522}
4523
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07004524static int selinux_socket_post_create(struct socket *sock, int family,
4525 int type, int protocol, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07004527 const struct task_security_struct *tsec = selinux_cred(current_cred());
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05004528 struct inode_security_struct *isec = inode_security_novalidate(SOCK_INODE(sock));
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004529 struct sk_security_struct *sksec;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004530 u16 sclass = socket_type_to_security_class(family, type, protocol);
4531 u32 sid = SECINITSID_KERNEL;
David Howells275bb412008-11-14 10:39:19 +11004532 int err = 0;
4533
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004534 if (!kern) {
4535 err = socket_sockcreate_sid(tsec, sclass, &sid);
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004536 if (err)
4537 return err;
4538 }
David Howells275bb412008-11-14 10:39:19 +11004539
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004540 isec->sclass = sclass;
4541 isec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004542 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004544 if (sock->sk) {
4545 sksec = sock->sk->sk_security;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004546 sksec->sclass = sclass;
4547 sksec->sid = sid;
Richard Hainesd4529302018-02-13 20:57:18 +00004548 /* Allows detection of the first association on this socket */
4549 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4550 sksec->sctp_assoc_state = SCTP_ASSOC_UNSET;
4551
Paul Moore389fb8002009-03-27 17:10:34 -04004552 err = selinux_netlbl_socket_post_create(sock->sk, family);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004553 }
4554
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07004555 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556}
4557
David Herrmann0b811db2018-05-04 16:28:21 +02004558static int selinux_socket_socketpair(struct socket *socka,
4559 struct socket *sockb)
4560{
4561 struct sk_security_struct *sksec_a = socka->sk->sk_security;
4562 struct sk_security_struct *sksec_b = sockb->sk->sk_security;
4563
4564 sksec_a->peer_sid = sksec_b->sid;
4565 sksec_b->peer_sid = sksec_a->sid;
4566
4567 return 0;
4568}
4569
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570/* Range of port numbers used to automatically bind.
4571 Need to determine whether we should perform a name_bind
4572 permission check between the socket and the port number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573
4574static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
4575{
Paul Moore253bfae2010-04-22 14:46:19 -04004576 struct sock *sk = sock->sk;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004577 struct sk_security_struct *sksec = sk->sk_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 u16 family;
4579 int err;
4580
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004581 err = sock_has_perm(sk, SOCKET__BIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 if (err)
4583 goto out;
4584
Richard Hainesd4529302018-02-13 20:57:18 +00004585 /* If PF_INET or PF_INET6, check name_bind permission for the port. */
Paul Moore253bfae2010-04-22 14:46:19 -04004586 family = sk->sk_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 if (family == PF_INET || family == PF_INET6) {
4588 char *addrp;
Thomas Liu2bf49692009-07-14 12:14:09 -04004589 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004590 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 struct sockaddr_in *addr4 = NULL;
4592 struct sockaddr_in6 *addr6 = NULL;
Tetsuo Handac750e692019-04-12 19:59:34 +09004593 u16 family_sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594 unsigned short snum;
James Morrise399f982008-06-12 01:39:58 +10004595 u32 sid, node_perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596
Richard Hainesd4529302018-02-13 20:57:18 +00004597 /*
4598 * sctp_bindx(3) calls via selinux_sctp_bind_connect()
4599 * that validates multiple binding addresses. Because of this
4600 * need to check address->sa_family as it is possible to have
4601 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4602 */
Tetsuo Handac750e692019-04-12 19:59:34 +09004603 if (addrlen < offsetofend(struct sockaddr, sa_family))
4604 return -EINVAL;
4605 family_sa = address->sa_family;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004606 switch (family_sa) {
4607 case AF_UNSPEC:
Richard Haines68741a8a2018-03-02 19:54:34 +00004608 case AF_INET:
4609 if (addrlen < sizeof(struct sockaddr_in))
4610 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 addr4 = (struct sockaddr_in *)address;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004612 if (family_sa == AF_UNSPEC) {
4613 /* see __inet_bind(), we only want to allow
4614 * AF_UNSPEC if the address is INADDR_ANY
4615 */
4616 if (addr4->sin_addr.s_addr != htonl(INADDR_ANY))
4617 goto err_af;
4618 family_sa = AF_INET;
4619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 snum = ntohs(addr4->sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 addrp = (char *)&addr4->sin_addr.s_addr;
Richard Haines68741a8a2018-03-02 19:54:34 +00004622 break;
4623 case AF_INET6:
4624 if (addrlen < SIN6_LEN_RFC2133)
4625 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626 addr6 = (struct sockaddr_in6 *)address;
4627 snum = ntohs(addr6->sin6_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628 addrp = (char *)&addr6->sin6_addr.s6_addr;
Richard Haines68741a8a2018-03-02 19:54:34 +00004629 break;
4630 default:
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004631 goto err_af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632 }
4633
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004634 ad.type = LSM_AUDIT_DATA_NET;
4635 ad.u.net = &net;
4636 ad.u.net->sport = htons(snum);
4637 ad.u.net->family = family_sa;
4638
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004639 if (snum) {
4640 int low, high;
4641
Eric W. Biederman0bbf87d2013-09-28 14:10:59 -07004642 inet_get_local_port_range(sock_net(sk), &low, &high);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004643
Maciej Żenczykowski82f31eb2019-11-25 15:37:04 -08004644 if (inet_port_requires_bind_service(sock_net(sk), snum) ||
4645 snum < low || snum > high) {
Paul Moore3e112172008-04-10 10:48:14 -04004646 err = sel_netport_sid(sk->sk_protocol,
4647 snum, &sid);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004648 if (err)
4649 goto out;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004650 err = avc_has_perm(&selinux_state,
4651 sksec->sid, sid,
Paul Moore253bfae2010-04-22 14:46:19 -04004652 sksec->sclass,
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004653 SOCKET__NAME_BIND, &ad);
4654 if (err)
4655 goto out;
4656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 }
Eric Paris828dfe12008-04-17 13:17:49 -04004658
Paul Moore253bfae2010-04-22 14:46:19 -04004659 switch (sksec->sclass) {
James Morris13402582005-09-30 14:24:34 -04004660 case SECCLASS_TCP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 node_perm = TCP_SOCKET__NODE_BIND;
4662 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004663
James Morris13402582005-09-30 14:24:34 -04004664 case SECCLASS_UDP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 node_perm = UDP_SOCKET__NODE_BIND;
4666 break;
James Morris2ee92d42006-11-13 16:09:01 -08004667
4668 case SECCLASS_DCCP_SOCKET:
4669 node_perm = DCCP_SOCKET__NODE_BIND;
4670 break;
4671
Richard Hainesd4529302018-02-13 20:57:18 +00004672 case SECCLASS_SCTP_SOCKET:
4673 node_perm = SCTP_SOCKET__NODE_BIND;
4674 break;
4675
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676 default:
4677 node_perm = RAWIP_SOCKET__NODE_BIND;
4678 break;
4679 }
Eric Paris828dfe12008-04-17 13:17:49 -04004680
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004681 err = sel_netnode_sid(addrp, family_sa, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 if (err)
4683 goto out;
Eric Paris828dfe12008-04-17 13:17:49 -04004684
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004685 if (family_sa == AF_INET)
Eric Paris48c62af2012-04-02 13:15:44 -04004686 ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687 else
Eric Paris48c62af2012-04-02 13:15:44 -04004688 ad.u.net->v6info.saddr = addr6->sin6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004690 err = avc_has_perm(&selinux_state,
4691 sksec->sid, sid,
Paul Moore253bfae2010-04-22 14:46:19 -04004692 sksec->sclass, node_perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693 if (err)
4694 goto out;
4695 }
4696out:
4697 return err;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004698err_af:
4699 /* Note that SCTP services expect -EINVAL, others -EAFNOSUPPORT. */
4700 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4701 return -EINVAL;
4702 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703}
4704
Richard Hainesd4529302018-02-13 20:57:18 +00004705/* This supports connect(2) and SCTP connect services such as sctp_connectx(3)
Kees Cookd61330c2019-02-17 14:08:36 -08004706 * and sctp_sendmsg(3) as described in Documentation/security/SCTP.rst
Richard Hainesd4529302018-02-13 20:57:18 +00004707 */
4708static int selinux_socket_connect_helper(struct socket *sock,
4709 struct sockaddr *address, int addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710{
Paul Moore014ab192008-10-10 10:16:33 -04004711 struct sock *sk = sock->sk;
Paul Moore253bfae2010-04-22 14:46:19 -04004712 struct sk_security_struct *sksec = sk->sk_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713 int err;
4714
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004715 err = sock_has_perm(sk, SOCKET__CONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716 if (err)
4717 return err;
Paolo Abeni05174c92019-05-10 19:12:33 +02004718 if (addrlen < offsetofend(struct sockaddr, sa_family))
4719 return -EINVAL;
4720
4721 /* connect(AF_UNSPEC) has special handling, as it is a documented
4722 * way to disconnect the socket
4723 */
4724 if (address->sa_family == AF_UNSPEC)
4725 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726
4727 /*
Richard Hainesd4529302018-02-13 20:57:18 +00004728 * If a TCP, DCCP or SCTP socket, check name_connect permission
4729 * for the port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 */
Paul Moore253bfae2010-04-22 14:46:19 -04004731 if (sksec->sclass == SECCLASS_TCP_SOCKET ||
Richard Hainesd4529302018-02-13 20:57:18 +00004732 sksec->sclass == SECCLASS_DCCP_SOCKET ||
4733 sksec->sclass == SECCLASS_SCTP_SOCKET) {
Thomas Liu2bf49692009-07-14 12:14:09 -04004734 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004735 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 struct sockaddr_in *addr4 = NULL;
4737 struct sockaddr_in6 *addr6 = NULL;
4738 unsigned short snum;
James Morris2ee92d42006-11-13 16:09:01 -08004739 u32 sid, perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740
Richard Hainesd4529302018-02-13 20:57:18 +00004741 /* sctp_connectx(3) calls via selinux_sctp_bind_connect()
4742 * that validates multiple connect addresses. Because of this
4743 * need to check address->sa_family as it is possible to have
4744 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4745 */
Richard Haines68741a8a2018-03-02 19:54:34 +00004746 switch (address->sa_family) {
4747 case AF_INET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748 addr4 = (struct sockaddr_in *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004749 if (addrlen < sizeof(struct sockaddr_in))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750 return -EINVAL;
4751 snum = ntohs(addr4->sin_port);
Richard Haines68741a8a2018-03-02 19:54:34 +00004752 break;
4753 case AF_INET6:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754 addr6 = (struct sockaddr_in6 *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004755 if (addrlen < SIN6_LEN_RFC2133)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756 return -EINVAL;
4757 snum = ntohs(addr6->sin6_port);
Richard Haines68741a8a2018-03-02 19:54:34 +00004758 break;
4759 default:
4760 /* Note that SCTP services expect -EINVAL, whereas
4761 * others expect -EAFNOSUPPORT.
4762 */
4763 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4764 return -EINVAL;
4765 else
4766 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767 }
4768
Paul Moore3e112172008-04-10 10:48:14 -04004769 err = sel_netport_sid(sk->sk_protocol, snum, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770 if (err)
Richard Hainesd4529302018-02-13 20:57:18 +00004771 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772
Richard Hainesd4529302018-02-13 20:57:18 +00004773 switch (sksec->sclass) {
4774 case SECCLASS_TCP_SOCKET:
4775 perm = TCP_SOCKET__NAME_CONNECT;
4776 break;
4777 case SECCLASS_DCCP_SOCKET:
4778 perm = DCCP_SOCKET__NAME_CONNECT;
4779 break;
4780 case SECCLASS_SCTP_SOCKET:
4781 perm = SCTP_SOCKET__NAME_CONNECT;
4782 break;
4783 }
James Morris2ee92d42006-11-13 16:09:01 -08004784
Eric Paris50c205f2012-04-04 15:01:43 -04004785 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004786 ad.u.net = &net;
4787 ad.u.net->dport = htons(snum);
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004788 ad.u.net->family = address->sa_family;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004789 err = avc_has_perm(&selinux_state,
4790 sksec->sid, sid, sksec->sclass, perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 if (err)
Richard Hainesd4529302018-02-13 20:57:18 +00004792 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793 }
4794
Richard Hainesd4529302018-02-13 20:57:18 +00004795 return 0;
4796}
Paul Moore014ab192008-10-10 10:16:33 -04004797
Richard Hainesd4529302018-02-13 20:57:18 +00004798/* Supports connect(2), see comments in selinux_socket_connect_helper() */
4799static int selinux_socket_connect(struct socket *sock,
4800 struct sockaddr *address, int addrlen)
4801{
4802 int err;
4803 struct sock *sk = sock->sk;
4804
4805 err = selinux_socket_connect_helper(sock, address, addrlen);
4806 if (err)
4807 return err;
4808
4809 return selinux_netlbl_socket_connect(sk, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810}
4811
4812static int selinux_socket_listen(struct socket *sock, int backlog)
4813{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004814 return sock_has_perm(sock->sk, SOCKET__LISTEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815}
4816
4817static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
4818{
4819 int err;
4820 struct inode_security_struct *isec;
4821 struct inode_security_struct *newisec;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004822 u16 sclass;
4823 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004825 err = sock_has_perm(sock->sk, SOCKET__ACCEPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826 if (err)
4827 return err;
4828
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05004829 isec = inode_security_novalidate(SOCK_INODE(sock));
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004830 spin_lock(&isec->lock);
4831 sclass = isec->sclass;
4832 sid = isec->sid;
4833 spin_unlock(&isec->lock);
4834
4835 newisec = inode_security_novalidate(SOCK_INODE(newsock));
4836 newisec->sclass = sclass;
4837 newisec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004838 newisec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839
4840 return 0;
4841}
4842
4843static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
Eric Paris828dfe12008-04-17 13:17:49 -04004844 int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004846 return sock_has_perm(sock->sk, SOCKET__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847}
4848
4849static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
4850 int size, int flags)
4851{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004852 return sock_has_perm(sock->sk, SOCKET__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853}
4854
4855static int selinux_socket_getsockname(struct socket *sock)
4856{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004857 return sock_has_perm(sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858}
4859
4860static int selinux_socket_getpeername(struct socket *sock)
4861{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004862 return sock_has_perm(sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863}
4864
Eric Paris828dfe12008-04-17 13:17:49 -04004865static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866{
Paul Mooref8687af2006-10-30 15:22:15 -08004867 int err;
4868
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004869 err = sock_has_perm(sock->sk, SOCKET__SETOPT);
Paul Mooref8687af2006-10-30 15:22:15 -08004870 if (err)
4871 return err;
4872
4873 return selinux_netlbl_socket_setsockopt(sock, level, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874}
4875
4876static int selinux_socket_getsockopt(struct socket *sock, int level,
4877 int optname)
4878{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004879 return sock_has_perm(sock->sk, SOCKET__GETOPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880}
4881
4882static int selinux_socket_shutdown(struct socket *sock, int how)
4883{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004884 return sock_has_perm(sock->sk, SOCKET__SHUTDOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885}
4886
David S. Miller3610cda2011-01-05 15:38:53 -08004887static int selinux_socket_unix_stream_connect(struct sock *sock,
4888 struct sock *other,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 struct sock *newsk)
4890{
David S. Miller3610cda2011-01-05 15:38:53 -08004891 struct sk_security_struct *sksec_sock = sock->sk_security;
4892 struct sk_security_struct *sksec_other = other->sk_security;
Paul Moore4d1e2452010-04-22 14:46:18 -04004893 struct sk_security_struct *sksec_new = newsk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004894 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004895 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896 int err;
4897
Eric Paris50c205f2012-04-04 15:01:43 -04004898 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004899 ad.u.net = &net;
4900 ad.u.net->sk = other;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004902 err = avc_has_perm(&selinux_state,
4903 sksec_sock->sid, sksec_other->sid,
Paul Moore4d1e2452010-04-22 14:46:18 -04004904 sksec_other->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 UNIX_STREAM_SOCKET__CONNECTTO, &ad);
4906 if (err)
4907 return err;
4908
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 /* server child socket */
Paul Moore4d1e2452010-04-22 14:46:18 -04004910 sksec_new->peer_sid = sksec_sock->sid;
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004911 err = security_sid_mls_copy(&selinux_state, sksec_other->sid,
4912 sksec_sock->sid, &sksec_new->sid);
Paul Moore4d1e2452010-04-22 14:46:18 -04004913 if (err)
4914 return err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07004915
Paul Moore4d1e2452010-04-22 14:46:18 -04004916 /* connecting socket */
4917 sksec_sock->peer_sid = sksec_new->sid;
4918
4919 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920}
4921
4922static int selinux_socket_unix_may_send(struct socket *sock,
4923 struct socket *other)
4924{
Paul Moore253bfae2010-04-22 14:46:19 -04004925 struct sk_security_struct *ssec = sock->sk->sk_security;
4926 struct sk_security_struct *osec = other->sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004927 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004928 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929
Eric Paris50c205f2012-04-04 15:01:43 -04004930 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004931 ad.u.net = &net;
4932 ad.u.net->sk = other->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004934 return avc_has_perm(&selinux_state,
4935 ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
Paul Moore253bfae2010-04-22 14:46:19 -04004936 &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937}
4938
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004939static int selinux_inet_sys_rcv_skb(struct net *ns, int ifindex,
4940 char *addrp, u16 family, u32 peer_sid,
Thomas Liu2bf49692009-07-14 12:14:09 -04004941 struct common_audit_data *ad)
Paul Mooreeffad8d2008-01-29 08:49:27 -05004942{
4943 int err;
4944 u32 if_sid;
4945 u32 node_sid;
4946
Paul Moorecbe0d6e2014-09-10 17:09:57 -04004947 err = sel_netif_sid(ns, ifindex, &if_sid);
Paul Mooreeffad8d2008-01-29 08:49:27 -05004948 if (err)
4949 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004950 err = avc_has_perm(&selinux_state,
4951 peer_sid, if_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004952 SECCLASS_NETIF, NETIF__INGRESS, ad);
4953 if (err)
4954 return err;
4955
4956 err = sel_netnode_sid(addrp, family, &node_sid);
4957 if (err)
4958 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004959 return avc_has_perm(&selinux_state,
4960 peer_sid, node_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05004961 SECCLASS_NODE, NODE__RECVFROM, ad);
4962}
4963
Paul Moore220deb92008-01-29 08:38:23 -05004964static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
Paul Moored8395c82008-10-10 10:16:30 -04004965 u16 family)
Paul Moore220deb92008-01-29 08:38:23 -05004966{
Paul Moore277d3422008-12-31 12:54:11 -05004967 int err = 0;
Paul Moore220deb92008-01-29 08:38:23 -05004968 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05004969 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04004970 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004971 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04004972 char *addrp;
4973
Eric Paris50c205f2012-04-04 15:01:43 -04004974 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004975 ad.u.net = &net;
4976 ad.u.net->netif = skb->skb_iif;
4977 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04004978 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
4979 if (err)
4980 return err;
Paul Moore220deb92008-01-29 08:38:23 -05004981
Paul Moore58bfbb52009-03-27 17:10:41 -04004982 if (selinux_secmark_enabled()) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004983 err = avc_has_perm(&selinux_state,
4984 sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Moored8395c82008-10-10 10:16:30 -04004985 PACKET__RECV, &ad);
Paul Moore58bfbb52009-03-27 17:10:41 -04004986 if (err)
4987 return err;
4988 }
Paul Moore220deb92008-01-29 08:38:23 -05004989
Steffen Klassertb9679a72011-02-23 12:55:21 +01004990 err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
4991 if (err)
4992 return err;
4993 err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004994
James Morris4e5ab4c2006-06-09 00:33:33 -07004995 return err;
4996}
Trent Jaegerd28d1e02005-12-13 23:12:40 -08004997
James Morris4e5ab4c2006-06-09 00:33:33 -07004998static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
4999{
Paul Moore220deb92008-01-29 08:38:23 -05005000 int err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005001 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05005002 u16 family = sk->sk_family;
5003 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04005004 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005005 struct lsm_network_audit net = {0,};
Paul Moore220deb92008-01-29 08:38:23 -05005006 char *addrp;
Paul Moored8395c82008-10-10 10:16:30 -04005007 u8 secmark_active;
5008 u8 peerlbl_active;
James Morris4e5ab4c2006-06-09 00:33:33 -07005009
James Morris4e5ab4c2006-06-09 00:33:33 -07005010 if (family != PF_INET && family != PF_INET6)
Paul Moore220deb92008-01-29 08:38:23 -05005011 return 0;
James Morris4e5ab4c2006-06-09 00:33:33 -07005012
5013 /* Handle mapped IPv4 packets arriving via IPv6 sockets */
Al Viro87fcd702006-12-04 22:00:55 +00005014 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
James Morris4e5ab4c2006-06-09 00:33:33 -07005015 family = PF_INET;
5016
Paul Moored8395c82008-10-10 10:16:30 -04005017 /* If any sort of compatibility mode is enabled then handoff processing
5018 * to the selinux_sock_rcv_skb_compat() function to deal with the
5019 * special handling. We do this in an attempt to keep this function
5020 * as fast and as clean as possible. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005021 if (!selinux_policycap_netpeer())
Paul Moored8395c82008-10-10 10:16:30 -04005022 return selinux_sock_rcv_skb_compat(sk, skb, family);
5023
5024 secmark_active = selinux_secmark_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04005025 peerlbl_active = selinux_peerlbl_enabled();
Paul Moored8395c82008-10-10 10:16:30 -04005026 if (!secmark_active && !peerlbl_active)
5027 return 0;
5028
Eric Paris50c205f2012-04-04 15:01:43 -04005029 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005030 ad.u.net = &net;
5031 ad.u.net->netif = skb->skb_iif;
5032 ad.u.net->family = family;
Paul Moore224dfbd2008-01-29 08:38:13 -05005033 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
James Morris4e5ab4c2006-06-09 00:33:33 -07005034 if (err)
Paul Moore220deb92008-01-29 08:38:23 -05005035 return err;
James Morris4e5ab4c2006-06-09 00:33:33 -07005036
Paul Moored8395c82008-10-10 10:16:30 -04005037 if (peerlbl_active) {
Paul Moored621d352008-01-29 08:43:36 -05005038 u32 peer_sid;
5039
5040 err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
5041 if (err)
5042 return err;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005043 err = selinux_inet_sys_rcv_skb(sock_net(sk), skb->skb_iif,
5044 addrp, family, peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04005045 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04005046 selinux_netlbl_err(skb, family, err, 0);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005047 return err;
Paul Mooredfaebe92008-10-10 10:16:31 -04005048 }
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005049 err = avc_has_perm(&selinux_state,
5050 sk_sid, peer_sid, SECCLASS_PEER,
Paul Moored621d352008-01-29 08:43:36 -05005051 PEER__RECV, &ad);
Chad Hanson46d01d62013-12-23 17:45:01 -05005052 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04005053 selinux_netlbl_err(skb, family, err, 0);
Chad Hanson46d01d62013-12-23 17:45:01 -05005054 return err;
5055 }
Paul Moored621d352008-01-29 08:43:36 -05005056 }
5057
Paul Moored8395c82008-10-10 10:16:30 -04005058 if (secmark_active) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005059 err = avc_has_perm(&selinux_state,
5060 sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005061 PACKET__RECV, &ad);
5062 if (err)
5063 return err;
5064 }
5065
Paul Moored621d352008-01-29 08:43:36 -05005066 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067}
5068
Catherine Zhang2c7946a2006-03-20 22:41:23 -08005069static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
5070 int __user *optlen, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071{
5072 int err = 0;
5073 char *scontext;
5074 u32 scontext_len;
Paul Moore253bfae2010-04-22 14:46:19 -04005075 struct sk_security_struct *sksec = sock->sk->sk_security;
Paul Moore3de4bab2006-11-17 17:38:54 -05005076 u32 peer_sid = SECSID_NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077
Paul Moore253bfae2010-04-22 14:46:19 -04005078 if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
Richard Hainesd4529302018-02-13 20:57:18 +00005079 sksec->sclass == SECCLASS_TCP_SOCKET ||
5080 sksec->sclass == SECCLASS_SCTP_SOCKET)
Eric Parisdd3e7832010-04-07 15:08:46 -04005081 peer_sid = sksec->peer_sid;
Paul Moore253bfae2010-04-22 14:46:19 -04005082 if (peer_sid == SECSID_NULL)
5083 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005085 err = security_sid_to_context(&selinux_state, peer_sid, &scontext,
5086 &scontext_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087 if (err)
Paul Moore253bfae2010-04-22 14:46:19 -04005088 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089
5090 if (scontext_len > len) {
5091 err = -ERANGE;
5092 goto out_len;
5093 }
5094
5095 if (copy_to_user(optval, scontext, scontext_len))
5096 err = -EFAULT;
5097
5098out_len:
5099 if (put_user(scontext_len, optlen))
5100 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101 kfree(scontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102 return err;
5103}
5104
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005105static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
Catherine Zhang2c7946a2006-03-20 22:41:23 -08005106{
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005107 u32 peer_secid = SECSID_NULL;
Paul Moore75e22912008-01-29 08:38:04 -05005108 u16 family;
Paul Moore899134f2016-03-28 15:19:10 -04005109 struct inode_security_struct *isec;
Catherine Zhang877ce7c2006-06-29 12:27:47 -07005110
Paul Mooreaa862902008-10-10 10:16:29 -04005111 if (skb && skb->protocol == htons(ETH_P_IP))
5112 family = PF_INET;
5113 else if (skb && skb->protocol == htons(ETH_P_IPV6))
5114 family = PF_INET6;
5115 else if (sock)
Paul Moore75e22912008-01-29 08:38:04 -05005116 family = sock->sk->sk_family;
Paul Moore75e22912008-01-29 08:38:04 -05005117 else
5118 goto out;
5119
Paul Moore899134f2016-03-28 15:19:10 -04005120 if (sock && family == PF_UNIX) {
5121 isec = inode_security_novalidate(SOCK_INODE(sock));
5122 peer_secid = isec->sid;
5123 } else if (skb)
Paul Moore220deb92008-01-29 08:38:23 -05005124 selinux_skb_peerlbl_sid(skb, family, &peer_secid);
Catherine Zhang2c7946a2006-03-20 22:41:23 -08005125
Paul Moore75e22912008-01-29 08:38:04 -05005126out:
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005127 *secid = peer_secid;
Paul Moore75e22912008-01-29 08:38:04 -05005128 if (peer_secid == SECSID_NULL)
5129 return -EINVAL;
5130 return 0;
Catherine Zhang2c7946a2006-03-20 22:41:23 -08005131}
5132
Al Viro7d877f32005-10-21 03:20:43 -04005133static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134{
Paul Moore84914b72010-04-22 14:46:18 -04005135 struct sk_security_struct *sksec;
5136
5137 sksec = kzalloc(sizeof(*sksec), priority);
5138 if (!sksec)
5139 return -ENOMEM;
5140
5141 sksec->peer_sid = SECINITSID_UNLABELED;
5142 sksec->sid = SECINITSID_UNLABELED;
Stephen Smalley5dee25d2015-07-10 17:19:57 -04005143 sksec->sclass = SECCLASS_SOCKET;
Paul Moore84914b72010-04-22 14:46:18 -04005144 selinux_netlbl_sk_security_reset(sksec);
5145 sk->sk_security = sksec;
5146
5147 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148}
5149
5150static void selinux_sk_free_security(struct sock *sk)
5151{
Paul Moore84914b72010-04-22 14:46:18 -04005152 struct sk_security_struct *sksec = sk->sk_security;
5153
5154 sk->sk_security = NULL;
5155 selinux_netlbl_sk_security_free(sksec);
5156 kfree(sksec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157}
5158
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005159static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
5160{
Eric Parisdd3e7832010-04-07 15:08:46 -04005161 struct sk_security_struct *sksec = sk->sk_security;
5162 struct sk_security_struct *newsksec = newsk->sk_security;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005163
Eric Parisdd3e7832010-04-07 15:08:46 -04005164 newsksec->sid = sksec->sid;
5165 newsksec->peer_sid = sksec->peer_sid;
5166 newsksec->sclass = sksec->sclass;
Paul Moore99f59ed2006-08-29 17:53:48 -07005167
Eric Parisdd3e7832010-04-07 15:08:46 -04005168 selinux_netlbl_sk_security_reset(newsksec);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005169}
5170
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07005171static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005172{
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005173 if (!sk)
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07005174 *secid = SECINITSID_ANY_SOCKET;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005175 else {
5176 struct sk_security_struct *sksec = sk->sk_security;
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005177
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07005178 *secid = sksec->sid;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005179 }
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005180}
5181
Eric Paris828dfe12008-04-17 13:17:49 -04005182static void selinux_sock_graft(struct sock *sk, struct socket *parent)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005183{
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05005184 struct inode_security_struct *isec =
5185 inode_security_novalidate(SOCK_INODE(parent));
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005186 struct sk_security_struct *sksec = sk->sk_security;
5187
Paul Moore2873ead2014-07-28 10:42:48 -04005188 if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
5189 sk->sk_family == PF_UNIX)
David Woodhouse2148ccc2006-09-29 15:50:25 -07005190 isec->sid = sksec->sid;
Paul Moore220deb92008-01-29 08:38:23 -05005191 sksec->sclass = isec->sclass;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005192}
5193
Richard Hainesd4529302018-02-13 20:57:18 +00005194/* Called whenever SCTP receives an INIT chunk. This happens when an incoming
5195 * connect(2), sctp_connectx(3) or sctp_sendmsg(3) (with no association
5196 * already present).
5197 */
5198static int selinux_sctp_assoc_request(struct sctp_endpoint *ep,
5199 struct sk_buff *skb)
5200{
5201 struct sk_security_struct *sksec = ep->base.sk->sk_security;
5202 struct common_audit_data ad;
5203 struct lsm_network_audit net = {0,};
5204 u8 peerlbl_active;
5205 u32 peer_sid = SECINITSID_UNLABELED;
5206 u32 conn_sid;
5207 int err = 0;
5208
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005209 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005210 return 0;
5211
5212 peerlbl_active = selinux_peerlbl_enabled();
5213
5214 if (peerlbl_active) {
5215 /* This will return peer_sid = SECSID_NULL if there are
5216 * no peer labels, see security_net_peersid_resolve().
5217 */
5218 err = selinux_skb_peerlbl_sid(skb, ep->base.sk->sk_family,
5219 &peer_sid);
5220 if (err)
5221 return err;
5222
5223 if (peer_sid == SECSID_NULL)
5224 peer_sid = SECINITSID_UNLABELED;
5225 }
5226
5227 if (sksec->sctp_assoc_state == SCTP_ASSOC_UNSET) {
5228 sksec->sctp_assoc_state = SCTP_ASSOC_SET;
5229
5230 /* Here as first association on socket. As the peer SID
5231 * was allowed by peer recv (and the netif/node checks),
5232 * then it is approved by policy and used as the primary
5233 * peer SID for getpeercon(3).
5234 */
5235 sksec->peer_sid = peer_sid;
5236 } else if (sksec->peer_sid != peer_sid) {
5237 /* Other association peer SIDs are checked to enforce
5238 * consistency among the peer SIDs.
5239 */
5240 ad.type = LSM_AUDIT_DATA_NET;
5241 ad.u.net = &net;
5242 ad.u.net->sk = ep->base.sk;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005243 err = avc_has_perm(&selinux_state,
5244 sksec->peer_sid, peer_sid, sksec->sclass,
Richard Hainesd4529302018-02-13 20:57:18 +00005245 SCTP_SOCKET__ASSOCIATION, &ad);
5246 if (err)
5247 return err;
5248 }
5249
5250 /* Compute the MLS component for the connection and store
5251 * the information in ep. This will be used by SCTP TCP type
5252 * sockets and peeled off connections as they cause a new
5253 * socket to be generated. selinux_sctp_sk_clone() will then
5254 * plug this into the new socket.
5255 */
5256 err = selinux_conn_sid(sksec->sid, peer_sid, &conn_sid);
5257 if (err)
5258 return err;
5259
5260 ep->secid = conn_sid;
5261 ep->peer_secid = peer_sid;
5262
5263 /* Set any NetLabel labels including CIPSO/CALIPSO options. */
5264 return selinux_netlbl_sctp_assoc_request(ep, skb);
5265}
5266
5267/* Check if sctp IPv4/IPv6 addresses are valid for binding or connecting
5268 * based on their @optname.
5269 */
5270static int selinux_sctp_bind_connect(struct sock *sk, int optname,
5271 struct sockaddr *address,
5272 int addrlen)
5273{
5274 int len, err = 0, walk_size = 0;
5275 void *addr_buf;
5276 struct sockaddr *addr;
5277 struct socket *sock;
5278
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005279 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005280 return 0;
5281
5282 /* Process one or more addresses that may be IPv4 or IPv6 */
5283 sock = sk->sk_socket;
5284 addr_buf = address;
5285
5286 while (walk_size < addrlen) {
Ondrej Mosnacekc1383252018-11-13 16:16:08 +01005287 if (walk_size + sizeof(sa_family_t) > addrlen)
5288 return -EINVAL;
5289
Richard Hainesd4529302018-02-13 20:57:18 +00005290 addr = addr_buf;
5291 switch (addr->sa_family) {
Alexey Kodanev4152dc92018-05-11 20:15:13 +03005292 case AF_UNSPEC:
Richard Hainesd4529302018-02-13 20:57:18 +00005293 case AF_INET:
5294 len = sizeof(struct sockaddr_in);
5295 break;
5296 case AF_INET6:
5297 len = sizeof(struct sockaddr_in6);
5298 break;
5299 default:
Alexey Kodanev4152dc92018-05-11 20:15:13 +03005300 return -EINVAL;
Richard Hainesd4529302018-02-13 20:57:18 +00005301 }
5302
Xin Long292c9972019-03-09 00:07:34 +08005303 if (walk_size + len > addrlen)
5304 return -EINVAL;
5305
Richard Hainesd4529302018-02-13 20:57:18 +00005306 err = -EINVAL;
5307 switch (optname) {
5308 /* Bind checks */
5309 case SCTP_PRIMARY_ADDR:
5310 case SCTP_SET_PEER_PRIMARY_ADDR:
5311 case SCTP_SOCKOPT_BINDX_ADD:
5312 err = selinux_socket_bind(sock, addr, len);
5313 break;
5314 /* Connect checks */
5315 case SCTP_SOCKOPT_CONNECTX:
5316 case SCTP_PARAM_SET_PRIMARY:
5317 case SCTP_PARAM_ADD_IP:
5318 case SCTP_SENDMSG_CONNECT:
5319 err = selinux_socket_connect_helper(sock, addr, len);
5320 if (err)
5321 return err;
5322
5323 /* As selinux_sctp_bind_connect() is called by the
5324 * SCTP protocol layer, the socket is already locked,
Randy Dunlapc76a2f92020-08-07 09:51:34 -07005325 * therefore selinux_netlbl_socket_connect_locked()
Richard Hainesd4529302018-02-13 20:57:18 +00005326 * is called here. The situations handled are:
5327 * sctp_connectx(3), sctp_sendmsg(3), sendmsg(2),
5328 * whenever a new IP address is added or when a new
5329 * primary address is selected.
5330 * Note that an SCTP connect(2) call happens before
5331 * the SCTP protocol layer and is handled via
5332 * selinux_socket_connect().
5333 */
5334 err = selinux_netlbl_socket_connect_locked(sk, addr);
5335 break;
5336 }
5337
5338 if (err)
5339 return err;
5340
5341 addr_buf += len;
5342 walk_size += len;
5343 }
5344
5345 return 0;
5346}
5347
5348/* Called whenever a new socket is created by accept(2) or sctp_peeloff(3). */
5349static void selinux_sctp_sk_clone(struct sctp_endpoint *ep, struct sock *sk,
5350 struct sock *newsk)
5351{
5352 struct sk_security_struct *sksec = sk->sk_security;
5353 struct sk_security_struct *newsksec = newsk->sk_security;
5354
5355 /* If policy does not support SECCLASS_SCTP_SOCKET then call
5356 * the non-sctp clone version.
5357 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005358 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005359 return selinux_sk_clone_security(sk, newsk);
5360
5361 newsksec->sid = ep->secid;
5362 newsksec->peer_sid = ep->peer_secid;
5363 newsksec->sclass = sksec->sclass;
5364 selinux_netlbl_sctp_sk_clone(sk, newsk);
5365}
5366
Florian Westphal41dd9592020-11-30 16:36:29 +01005367static int selinux_inet_conn_request(const struct sock *sk, struct sk_buff *skb,
Adrian Bunk9a673e52006-08-15 00:03:53 -07005368 struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005369{
5370 struct sk_security_struct *sksec = sk->sk_security;
5371 int err;
Paul Moore0b1f24e2013-12-03 11:39:13 -05005372 u16 family = req->rsk_ops->family;
Paul Moore446b8022013-12-04 16:10:51 -05005373 u32 connsid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005374 u32 peersid;
5375
Paul Mooreaa862902008-10-10 10:16:29 -04005376 err = selinux_skb_peerlbl_sid(skb, family, &peersid);
Paul Moore220deb92008-01-29 08:38:23 -05005377 if (err)
5378 return err;
Paul Moore446b8022013-12-04 16:10:51 -05005379 err = selinux_conn_sid(sksec->sid, peersid, &connsid);
5380 if (err)
5381 return err;
5382 req->secid = connsid;
5383 req->peer_secid = peersid;
Venkat Yekkiralaa51c64f2006-07-27 22:01:34 -07005384
Paul Moore389fb8002009-03-27 17:10:34 -04005385 return selinux_netlbl_inet_conn_request(req, family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005386}
5387
Adrian Bunk9a673e52006-08-15 00:03:53 -07005388static void selinux_inet_csk_clone(struct sock *newsk,
5389 const struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005390{
5391 struct sk_security_struct *newsksec = newsk->sk_security;
5392
5393 newsksec->sid = req->secid;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005394 newsksec->peer_sid = req->peer_secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005395 /* NOTE: Ideally, we should also get the isec->sid for the
5396 new socket in sync, but we don't have the isec available yet.
5397 So we will wait until sock_graft to do it, by which
5398 time it will have been created and available. */
Paul Moore99f59ed2006-08-29 17:53:48 -07005399
Paul Moore9f2ad662006-11-17 17:38:53 -05005400 /* We don't need to take any sort of lock here as we are the only
5401 * thread with access to newsksec */
Paul Moore389fb8002009-03-27 17:10:34 -04005402 selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005403}
5404
Paul Moore014ab192008-10-10 10:16:33 -04005405static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005406{
Paul Mooreaa862902008-10-10 10:16:29 -04005407 u16 family = sk->sk_family;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005408 struct sk_security_struct *sksec = sk->sk_security;
5409
Paul Mooreaa862902008-10-10 10:16:29 -04005410 /* handle mapped IPv4 packets arriving via IPv6 sockets */
5411 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
5412 family = PF_INET;
5413
5414 selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005415}
5416
Eric Paris2606fd12010-10-13 16:24:41 -04005417static int selinux_secmark_relabel_packet(u32 sid)
5418{
5419 const struct task_security_struct *__tsec;
5420 u32 tsid;
5421
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07005422 __tsec = selinux_cred(current_cred());
Eric Paris2606fd12010-10-13 16:24:41 -04005423 tsid = __tsec->sid;
5424
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005425 return avc_has_perm(&selinux_state,
5426 tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO,
5427 NULL);
Eric Paris2606fd12010-10-13 16:24:41 -04005428}
5429
5430static void selinux_secmark_refcount_inc(void)
5431{
5432 atomic_inc(&selinux_secmark_refcount);
5433}
5434
5435static void selinux_secmark_refcount_dec(void)
5436{
5437 atomic_dec(&selinux_secmark_refcount);
5438}
5439
Adrian Bunk9a673e52006-08-15 00:03:53 -07005440static void selinux_req_classify_flow(const struct request_sock *req,
Paul Moore3df98d72020-09-27 22:38:26 -04005441 struct flowi_common *flic)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005442{
Paul Moore3df98d72020-09-27 22:38:26 -04005443 flic->flowic_secid = req->secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005444}
5445
Paul Moore5dbbaf22013-01-14 07:12:19 +00005446static int selinux_tun_dev_alloc_security(void **security)
5447{
5448 struct tun_security_struct *tunsec;
5449
5450 tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL);
5451 if (!tunsec)
5452 return -ENOMEM;
5453 tunsec->sid = current_sid();
5454
5455 *security = tunsec;
5456 return 0;
5457}
5458
5459static void selinux_tun_dev_free_security(void *security)
5460{
5461 kfree(security);
5462}
5463
Paul Mooreed6d76e2009-08-28 18:12:49 -04005464static int selinux_tun_dev_create(void)
5465{
5466 u32 sid = current_sid();
5467
5468 /* we aren't taking into account the "sockcreate" SID since the socket
5469 * that is being created here is not a socket in the traditional sense,
5470 * instead it is a private sock, accessible only to the kernel, and
5471 * representing a wide range of network traffic spanning multiple
5472 * connections unlike traditional sockets - check the TUN driver to
5473 * get a better understanding of why this socket is special */
5474
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005475 return avc_has_perm(&selinux_state,
5476 sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005477 NULL);
5478}
5479
Paul Moore5dbbaf22013-01-14 07:12:19 +00005480static int selinux_tun_dev_attach_queue(void *security)
Paul Mooreed6d76e2009-08-28 18:12:49 -04005481{
Paul Moore5dbbaf22013-01-14 07:12:19 +00005482 struct tun_security_struct *tunsec = security;
5483
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005484 return avc_has_perm(&selinux_state,
5485 current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET,
Paul Moore5dbbaf22013-01-14 07:12:19 +00005486 TUN_SOCKET__ATTACH_QUEUE, NULL);
5487}
5488
5489static int selinux_tun_dev_attach(struct sock *sk, void *security)
5490{
5491 struct tun_security_struct *tunsec = security;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005492 struct sk_security_struct *sksec = sk->sk_security;
5493
5494 /* we don't currently perform any NetLabel based labeling here and it
5495 * isn't clear that we would want to do so anyway; while we could apply
5496 * labeling without the support of the TUN user the resulting labeled
5497 * traffic from the other end of the connection would almost certainly
5498 * cause confusion to the TUN user that had no idea network labeling
5499 * protocols were being used */
5500
Paul Moore5dbbaf22013-01-14 07:12:19 +00005501 sksec->sid = tunsec->sid;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005502 sksec->sclass = SECCLASS_TUN_SOCKET;
Paul Moore5dbbaf22013-01-14 07:12:19 +00005503
5504 return 0;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005505}
5506
Paul Moore5dbbaf22013-01-14 07:12:19 +00005507static int selinux_tun_dev_open(void *security)
Paul Mooreed6d76e2009-08-28 18:12:49 -04005508{
Paul Moore5dbbaf22013-01-14 07:12:19 +00005509 struct tun_security_struct *tunsec = security;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005510 u32 sid = current_sid();
5511 int err;
5512
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005513 err = avc_has_perm(&selinux_state,
5514 sid, tunsec->sid, SECCLASS_TUN_SOCKET,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005515 TUN_SOCKET__RELABELFROM, NULL);
5516 if (err)
5517 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005518 err = avc_has_perm(&selinux_state,
5519 sid, sid, SECCLASS_TUN_SOCKET,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005520 TUN_SOCKET__RELABELTO, NULL);
5521 if (err)
5522 return err;
Paul Moore5dbbaf22013-01-14 07:12:19 +00005523 tunsec->sid = sid;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005524
5525 return 0;
5526}
5527
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528#ifdef CONFIG_NETFILTER
5529
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005530static unsigned int selinux_ip_forward(struct sk_buff *skb,
5531 const struct net_device *indev,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005532 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533{
Paul Mooredfaebe92008-10-10 10:16:31 -04005534 int err;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005535 char *addrp;
5536 u32 peer_sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04005537 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005538 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05005539 u8 secmark_active;
Paul Moore948bf852008-10-10 10:16:32 -04005540 u8 netlbl_active;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005541 u8 peerlbl_active;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005542
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005543 if (!selinux_policycap_netpeer())
Paul Mooreeffad8d2008-01-29 08:49:27 -05005544 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005545
Paul Mooreeffad8d2008-01-29 08:49:27 -05005546 secmark_active = selinux_secmark_enabled();
Paul Moore948bf852008-10-10 10:16:32 -04005547 netlbl_active = netlbl_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04005548 peerlbl_active = selinux_peerlbl_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05005549 if (!secmark_active && !peerlbl_active)
5550 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005551
Paul Moored8395c82008-10-10 10:16:30 -04005552 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
5553 return NF_DROP;
5554
Eric Paris50c205f2012-04-04 15:01:43 -04005555 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005556 ad.u.net = &net;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005557 ad.u.net->netif = indev->ifindex;
Eric Paris48c62af2012-04-02 13:15:44 -04005558 ad.u.net->family = family;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005559 if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
5560 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561
Paul Mooredfaebe92008-10-10 10:16:31 -04005562 if (peerlbl_active) {
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005563 err = selinux_inet_sys_rcv_skb(dev_net(indev), indev->ifindex,
5564 addrp, family, peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04005565 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04005566 selinux_netlbl_err(skb, family, err, 1);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005567 return NF_DROP;
Paul Mooredfaebe92008-10-10 10:16:31 -04005568 }
5569 }
Paul Mooreeffad8d2008-01-29 08:49:27 -05005570
5571 if (secmark_active)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005572 if (avc_has_perm(&selinux_state,
5573 peer_sid, skb->secmark,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005574 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
5575 return NF_DROP;
5576
Paul Moore948bf852008-10-10 10:16:32 -04005577 if (netlbl_active)
5578 /* we do this in the FORWARD path and not the POST_ROUTING
5579 * path because we want to make sure we apply the necessary
5580 * labeling before IPsec is applied so we can leverage AH
5581 * protection */
5582 if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
5583 return NF_DROP;
5584
Paul Mooreeffad8d2008-01-29 08:49:27 -05005585 return NF_ACCEPT;
5586}
5587
Eric W. Biederman06198b32015-09-18 14:33:06 -05005588static unsigned int selinux_ipv4_forward(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005589 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005590 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005591{
David S. Miller238e54c2015-04-03 20:32:56 -04005592 return selinux_ip_forward(skb, state->in, PF_INET);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005593}
5594
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005595#if IS_ENABLED(CONFIG_IPV6)
Eric W. Biederman06198b32015-09-18 14:33:06 -05005596static unsigned int selinux_ipv6_forward(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005597 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005598 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005599{
David S. Miller238e54c2015-04-03 20:32:56 -04005600 return selinux_ip_forward(skb, state->in, PF_INET6);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005601}
5602#endif /* IPV6 */
5603
Paul Moore948bf852008-10-10 10:16:32 -04005604static unsigned int selinux_ip_output(struct sk_buff *skb,
5605 u16 family)
5606{
Paul Moore47180062013-12-04 16:10:45 -05005607 struct sock *sk;
Paul Moore948bf852008-10-10 10:16:32 -04005608 u32 sid;
5609
5610 if (!netlbl_enabled())
5611 return NF_ACCEPT;
5612
5613 /* we do this in the LOCAL_OUT path and not the POST_ROUTING path
5614 * because we want to make sure we apply the necessary labeling
5615 * before IPsec is applied so we can leverage AH protection */
Paul Moore47180062013-12-04 16:10:45 -05005616 sk = skb->sk;
5617 if (sk) {
5618 struct sk_security_struct *sksec;
5619
Eric Dumazete446f9d2015-10-08 05:01:55 -07005620 if (sk_listener(sk))
Paul Moore47180062013-12-04 16:10:45 -05005621 /* if the socket is the listening state then this
5622 * packet is a SYN-ACK packet which means it needs to
5623 * be labeled based on the connection/request_sock and
5624 * not the parent socket. unfortunately, we can't
5625 * lookup the request_sock yet as it isn't queued on
5626 * the parent socket until after the SYN-ACK is sent.
5627 * the "solution" is to simply pass the packet as-is
5628 * as any IP option based labeling should be copied
5629 * from the initial connection request (in the IP
5630 * layer). it is far from ideal, but until we get a
5631 * security label in the packet itself this is the
5632 * best we can do. */
5633 return NF_ACCEPT;
5634
5635 /* standard practice, label using the parent socket */
5636 sksec = sk->sk_security;
Paul Moore948bf852008-10-10 10:16:32 -04005637 sid = sksec->sid;
5638 } else
5639 sid = SECINITSID_KERNEL;
5640 if (selinux_netlbl_skbuff_setsid(skb, family, sid) != 0)
5641 return NF_DROP;
5642
5643 return NF_ACCEPT;
5644}
5645
Eric W. Biederman06198b32015-09-18 14:33:06 -05005646static unsigned int selinux_ipv4_output(void *priv,
Paul Moore948bf852008-10-10 10:16:32 -04005647 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005648 const struct nf_hook_state *state)
Paul Moore948bf852008-10-10 10:16:32 -04005649{
5650 return selinux_ip_output(skb, PF_INET);
5651}
5652
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005653#if IS_ENABLED(CONFIG_IPV6)
Huw Davies2917f572016-06-27 15:06:15 -04005654static unsigned int selinux_ipv6_output(void *priv,
5655 struct sk_buff *skb,
5656 const struct nf_hook_state *state)
5657{
5658 return selinux_ip_output(skb, PF_INET6);
5659}
5660#endif /* IPV6 */
5661
Paul Mooreeffad8d2008-01-29 08:49:27 -05005662static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
5663 int ifindex,
Paul Moored8395c82008-10-10 10:16:30 -04005664 u16 family)
James Morris4e5ab4c2006-06-09 00:33:33 -07005665{
Eric Dumazet54abc682015-11-08 10:54:07 -08005666 struct sock *sk = skb_to_full_sk(skb);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005667 struct sk_security_struct *sksec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005668 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005669 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04005670 char *addrp;
5671 u8 proto;
James Morris4e5ab4c2006-06-09 00:33:33 -07005672
Paul Mooreeffad8d2008-01-29 08:49:27 -05005673 if (sk == NULL)
5674 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005675 sksec = sk->sk_security;
James Morris4e5ab4c2006-06-09 00:33:33 -07005676
Eric Paris50c205f2012-04-04 15:01:43 -04005677 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005678 ad.u.net = &net;
5679 ad.u.net->netif = ifindex;
5680 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04005681 if (selinux_parse_skb(skb, &ad, &addrp, 0, &proto))
5682 return NF_DROP;
5683
Paul Moore58bfbb52009-03-27 17:10:41 -04005684 if (selinux_secmark_enabled())
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005685 if (avc_has_perm(&selinux_state,
5686 sksec->sid, skb->secmark,
Paul Moored8395c82008-10-10 10:16:30 -04005687 SECCLASS_PACKET, PACKET__SEND, &ad))
Eric Paris2fe66ec2010-11-23 06:28:08 +00005688 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07005689
Steffen Klassertb9679a72011-02-23 12:55:21 +01005690 if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
5691 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07005692
Paul Mooreeffad8d2008-01-29 08:49:27 -05005693 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694}
5695
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005696static unsigned int selinux_ip_postroute(struct sk_buff *skb,
5697 const struct net_device *outdev,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005698 u16 family)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699{
Paul Mooreeffad8d2008-01-29 08:49:27 -05005700 u32 secmark_perm;
5701 u32 peer_sid;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005702 int ifindex = outdev->ifindex;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005703 struct sock *sk;
Thomas Liu2bf49692009-07-14 12:14:09 -04005704 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005705 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05005706 char *addrp;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005707 u8 secmark_active;
5708 u8 peerlbl_active;
5709
Paul Mooreeffad8d2008-01-29 08:49:27 -05005710 /* If any sort of compatibility mode is enabled then handoff processing
5711 * to the selinux_ip_postroute_compat() function to deal with the
5712 * special handling. We do this in an attempt to keep this function
5713 * as fast and as clean as possible. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005714 if (!selinux_policycap_netpeer())
Paul Moored8395c82008-10-10 10:16:30 -04005715 return selinux_ip_postroute_compat(skb, ifindex, family);
Paul Moorec0828e52013-12-10 14:58:01 -05005716
Paul Mooreeffad8d2008-01-29 08:49:27 -05005717 secmark_active = selinux_secmark_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04005718 peerlbl_active = selinux_peerlbl_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05005719 if (!secmark_active && !peerlbl_active)
5720 return NF_ACCEPT;
5721
Eric Dumazet54abc682015-11-08 10:54:07 -08005722 sk = skb_to_full_sk(skb);
Paul Moorec0828e52013-12-10 14:58:01 -05005723
Paul Mooreeffad8d2008-01-29 08:49:27 -05005724#ifdef CONFIG_XFRM
5725 /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
5726 * packet transformation so allow the packet to pass without any checks
5727 * since we'll have another chance to perform access control checks
5728 * when the packet is on it's final way out.
5729 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
Paul Moorec0828e52013-12-10 14:58:01 -05005730 * is NULL, in this case go ahead and apply access control.
5731 * NOTE: if this is a local socket (skb->sk != NULL) that is in the
5732 * TCP listening state we cannot wait until the XFRM processing
5733 * is done as we will miss out on the SA label if we do;
5734 * unfortunately, this means more work, but it is only once per
5735 * connection. */
5736 if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL &&
Eric Dumazete446f9d2015-10-08 05:01:55 -07005737 !(sk && sk_listener(sk)))
Paul Mooreeffad8d2008-01-29 08:49:27 -05005738 return NF_ACCEPT;
5739#endif
Paul Mooreeffad8d2008-01-29 08:49:27 -05005740
Paul Moored8395c82008-10-10 10:16:30 -04005741 if (sk == NULL) {
Paul Moore446b8022013-12-04 16:10:51 -05005742 /* Without an associated socket the packet is either coming
5743 * from the kernel or it is being forwarded; check the packet
5744 * to determine which and if the packet is being forwarded
5745 * query the packet directly to determine the security label. */
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005746 if (skb->skb_iif) {
5747 secmark_perm = PACKET__FORWARD_OUT;
Paul Moored8395c82008-10-10 10:16:30 -04005748 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005749 return NF_DROP;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005750 } else {
5751 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04005752 peer_sid = SECINITSID_KERNEL;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005753 }
Eric Dumazete446f9d2015-10-08 05:01:55 -07005754 } else if (sk_listener(sk)) {
Paul Moore446b8022013-12-04 16:10:51 -05005755 /* Locally generated packet but the associated socket is in the
5756 * listening state which means this is a SYN-ACK packet. In
5757 * this particular case the correct security label is assigned
5758 * to the connection/request_sock but unfortunately we can't
5759 * query the request_sock as it isn't queued on the parent
5760 * socket until after the SYN-ACK packet is sent; the only
5761 * viable choice is to regenerate the label like we do in
5762 * selinux_inet_conn_request(). See also selinux_ip_output()
5763 * for similar problems. */
5764 u32 skb_sid;
Eric Dumazete446f9d2015-10-08 05:01:55 -07005765 struct sk_security_struct *sksec;
5766
Eric Dumazete446f9d2015-10-08 05:01:55 -07005767 sksec = sk->sk_security;
Paul Moore446b8022013-12-04 16:10:51 -05005768 if (selinux_skb_peerlbl_sid(skb, family, &skb_sid))
5769 return NF_DROP;
Paul Moorec0828e52013-12-10 14:58:01 -05005770 /* At this point, if the returned skb peerlbl is SECSID_NULL
5771 * and the packet has been through at least one XFRM
5772 * transformation then we must be dealing with the "final"
5773 * form of labeled IPsec packet; since we've already applied
5774 * all of our access controls on this packet we can safely
5775 * pass the packet. */
5776 if (skb_sid == SECSID_NULL) {
5777 switch (family) {
5778 case PF_INET:
5779 if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
5780 return NF_ACCEPT;
5781 break;
5782 case PF_INET6:
5783 if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED)
5784 return NF_ACCEPT;
Paul Moorea7a91a12014-09-03 10:51:59 -04005785 break;
Paul Moorec0828e52013-12-10 14:58:01 -05005786 default:
5787 return NF_DROP_ERR(-ECONNREFUSED);
5788 }
5789 }
Paul Moore446b8022013-12-04 16:10:51 -05005790 if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid))
5791 return NF_DROP;
5792 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04005793 } else {
Paul Moore446b8022013-12-04 16:10:51 -05005794 /* Locally generated packet, fetch the security label from the
5795 * associated socket. */
Paul Mooreeffad8d2008-01-29 08:49:27 -05005796 struct sk_security_struct *sksec = sk->sk_security;
5797 peer_sid = sksec->sid;
5798 secmark_perm = PACKET__SEND;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005799 }
5800
Eric Paris50c205f2012-04-04 15:01:43 -04005801 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005802 ad.u.net = &net;
5803 ad.u.net->netif = ifindex;
5804 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04005805 if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
Eric Paris04f6d702010-11-23 06:28:02 +00005806 return NF_DROP;
Paul Moored8395c82008-10-10 10:16:30 -04005807
Paul Mooreeffad8d2008-01-29 08:49:27 -05005808 if (secmark_active)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005809 if (avc_has_perm(&selinux_state,
5810 peer_sid, skb->secmark,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005811 SECCLASS_PACKET, secmark_perm, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005812 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005813
5814 if (peerlbl_active) {
5815 u32 if_sid;
5816 u32 node_sid;
5817
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005818 if (sel_netif_sid(dev_net(outdev), ifindex, &if_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005819 return NF_DROP;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005820 if (avc_has_perm(&selinux_state,
5821 peer_sid, if_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005822 SECCLASS_NETIF, NETIF__EGRESS, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005823 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005824
5825 if (sel_netnode_sid(addrp, family, &node_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005826 return NF_DROP;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005827 if (avc_has_perm(&selinux_state,
5828 peer_sid, node_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005829 SECCLASS_NODE, NODE__SENDTO, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005830 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005831 }
5832
5833 return NF_ACCEPT;
5834}
5835
Eric W. Biederman06198b32015-09-18 14:33:06 -05005836static unsigned int selinux_ipv4_postroute(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005837 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005838 const struct nf_hook_state *state)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005839{
David S. Miller238e54c2015-04-03 20:32:56 -04005840 return selinux_ip_postroute(skb, state->out, PF_INET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841}
5842
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04005843#if IS_ENABLED(CONFIG_IPV6)
Eric W. Biederman06198b32015-09-18 14:33:06 -05005844static unsigned int selinux_ipv6_postroute(void *priv,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005845 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -04005846 const struct nf_hook_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005847{
David S. Miller238e54c2015-04-03 20:32:56 -04005848 return selinux_ip_postroute(skb, state->out, PF_INET6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005850#endif /* IPV6 */
5851
5852#endif /* CONFIG_NETFILTER */
5853
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
5855{
Paul Moorefb739742020-04-28 09:59:02 -04005856 int rc = 0;
5857 unsigned int msg_len;
5858 unsigned int data_len = skb->len;
5859 unsigned char *data = skb->data;
Huaisheng Yedf4779b2020-01-13 23:03:31 +08005860 struct nlmsghdr *nlh;
5861 struct sk_security_struct *sksec = sk->sk_security;
Paul Moorefb739742020-04-28 09:59:02 -04005862 u16 sclass = sksec->sclass;
5863 u32 perm;
Huaisheng Yedf4779b2020-01-13 23:03:31 +08005864
Paul Moorefb739742020-04-28 09:59:02 -04005865 while (data_len >= nlmsg_total_size(0)) {
5866 nlh = (struct nlmsghdr *)data;
Huaisheng Yedf4779b2020-01-13 23:03:31 +08005867
Paul Moorefb739742020-04-28 09:59:02 -04005868 /* NOTE: the nlmsg_len field isn't reliably set by some netlink
5869 * users which means we can't reject skb's with bogus
5870 * length fields; our solution is to follow what
5871 * netlink_rcv_skb() does and simply skip processing at
5872 * messages with length fields that are clearly junk
5873 */
5874 if (nlh->nlmsg_len < NLMSG_HDRLEN || nlh->nlmsg_len > data_len)
5875 return 0;
5876
5877 rc = selinux_nlmsg_lookup(sclass, nlh->nlmsg_type, &perm);
5878 if (rc == 0) {
5879 rc = sock_has_perm(sk, perm);
5880 if (rc)
5881 return rc;
5882 } else if (rc == -EINVAL) {
5883 /* -EINVAL is a missing msg/perm mapping */
Huaisheng Yedf4779b2020-01-13 23:03:31 +08005884 pr_warn_ratelimited("SELinux: unrecognized netlink"
Paul Moorefb739742020-04-28 09:59:02 -04005885 " message: protocol=%hu nlmsg_type=%hu sclass=%s"
5886 " pid=%d comm=%s\n",
5887 sk->sk_protocol, nlh->nlmsg_type,
5888 secclass_map[sclass - 1].name,
5889 task_pid_nr(current), current->comm);
5890 if (enforcing_enabled(&selinux_state) &&
5891 !security_get_allow_unknown(&selinux_state))
5892 return rc;
5893 rc = 0;
5894 } else if (rc == -ENOENT) {
5895 /* -ENOENT is a missing socket/class mapping, ignore */
5896 rc = 0;
5897 } else {
5898 return rc;
Huaisheng Yedf4779b2020-01-13 23:03:31 +08005899 }
5900
Paul Moorefb739742020-04-28 09:59:02 -04005901 /* move to the next message after applying netlink padding */
5902 msg_len = NLMSG_ALIGN(nlh->nlmsg_len);
5903 if (msg_len >= data_len)
5904 return 0;
5905 data_len -= msg_len;
5906 data += msg_len;
Huaisheng Yedf4779b2020-01-13 23:03:31 +08005907 }
5908
Paul Moorefb739742020-04-28 09:59:02 -04005909 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910}
5911
Casey Schauflerecd5f822018-11-20 11:55:02 -08005912static void ipc_init_security(struct ipc_security_struct *isec, u16 sclass)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005913{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914 isec->sclass = sclass;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005915 isec->sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005916}
5917
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
Stephen Smalley6af963f2005-05-01 08:58:39 -07005919 u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005920{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005922 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005923 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924
Casey Schaufler7c653822018-09-21 17:19:45 -07005925 isec = selinux_ipc(ipc_perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005926
Eric Paris50c205f2012-04-04 15:01:43 -04005927 ad.type = LSM_AUDIT_DATA_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928 ad.u.ipc_id = ipc_perms->key;
5929
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005930 return avc_has_perm(&selinux_state,
5931 sid, isec->sid, isec->sclass, perms, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005932}
5933
5934static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
5935{
Huaisheng Yeb82f3f62020-01-10 17:58:56 +08005936 struct msg_security_struct *msec;
5937
5938 msec = selinux_msg_msg(msg);
5939 msec->sid = SECINITSID_UNLABELED;
5940
5941 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005942}
5943
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944/* message queue security operations */
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005945static int selinux_msg_queue_alloc_security(struct kern_ipc_perm *msq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005947 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005948 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005949 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005950 int rc;
5951
Casey Schauflerecd5f822018-11-20 11:55:02 -08005952 isec = selinux_ipc(msq);
5953 ipc_init_security(isec, SECCLASS_MSGQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954
Eric Paris50c205f2012-04-04 15:01:43 -04005955 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005956 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005958 rc = avc_has_perm(&selinux_state,
5959 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005960 MSGQ__CREATE, &ad);
Casey Schauflerecd5f822018-11-20 11:55:02 -08005961 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005962}
5963
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005964static int selinux_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005965{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005967 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11005968 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005969
Casey Schaufler7c653822018-09-21 17:19:45 -07005970 isec = selinux_ipc(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005971
Eric Paris50c205f2012-04-04 15:01:43 -04005972 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005973 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005974
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005975 return avc_has_perm(&selinux_state,
5976 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977 MSGQ__ASSOCIATE, &ad);
5978}
5979
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05005980static int selinux_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005981{
5982 int err;
5983 int perms;
5984
Eric Paris828dfe12008-04-17 13:17:49 -04005985 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986 case IPC_INFO:
5987 case MSG_INFO:
5988 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005989 return avc_has_perm(&selinux_state,
5990 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005991 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005992 case IPC_STAT:
5993 case MSG_STAT:
Davidlohr Bueso23c8cec2018-04-10 16:35:30 -07005994 case MSG_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005995 perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
5996 break;
5997 case IPC_SET:
5998 perms = MSGQ__SETATTR;
5999 break;
6000 case IPC_RMID:
6001 perms = MSGQ__DESTROY;
6002 break;
6003 default:
6004 return 0;
6005 }
6006
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05006007 err = ipc_has_perm(msq, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006008 return err;
6009}
6010
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05006011static int selinux_msg_queue_msgsnd(struct kern_ipc_perm *msq, struct msg_msg *msg, int msqflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013 struct ipc_security_struct *isec;
6014 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006015 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006016 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006017 int rc;
6018
Casey Schaufler7c653822018-09-21 17:19:45 -07006019 isec = selinux_ipc(msq);
6020 msec = selinux_msg_msg(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006021
6022 /*
6023 * First time through, need to assign label to the message
6024 */
6025 if (msec->sid == SECINITSID_UNLABELED) {
6026 /*
6027 * Compute new sid based on current process and
6028 * message queue this message will be stored in
6029 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006030 rc = security_transition_sid(&selinux_state, sid, isec->sid,
6031 SECCLASS_MSG, NULL, &msec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032 if (rc)
6033 return rc;
6034 }
6035
Eric Paris50c205f2012-04-04 15:01:43 -04006036 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05006037 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038
6039 /* Can this process write to the queue? */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006040 rc = avc_has_perm(&selinux_state,
6041 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006042 MSGQ__WRITE, &ad);
6043 if (!rc)
6044 /* Can this process send the message */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006045 rc = avc_has_perm(&selinux_state,
6046 sid, msec->sid, SECCLASS_MSG,
David Howells275bb412008-11-14 10:39:19 +11006047 MSG__SEND, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048 if (!rc)
6049 /* Can the message be put in the queue? */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006050 rc = avc_has_perm(&selinux_state,
6051 msec->sid, isec->sid, SECCLASS_MSGQ,
David Howells275bb412008-11-14 10:39:19 +11006052 MSGQ__ENQUEUE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053
6054 return rc;
6055}
6056
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05006057static int selinux_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058 struct task_struct *target,
6059 long type, int mode)
6060{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006061 struct ipc_security_struct *isec;
6062 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006063 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006064 u32 sid = task_sid(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006065 int rc;
6066
Casey Schaufler7c653822018-09-21 17:19:45 -07006067 isec = selinux_ipc(msq);
6068 msec = selinux_msg_msg(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069
Eric Paris50c205f2012-04-04 15:01:43 -04006070 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05006071 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006073 rc = avc_has_perm(&selinux_state,
6074 sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075 SECCLASS_MSGQ, MSGQ__READ, &ad);
6076 if (!rc)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006077 rc = avc_has_perm(&selinux_state,
6078 sid, msec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079 SECCLASS_MSG, MSG__RECEIVE, &ad);
6080 return rc;
6081}
6082
6083/* Shared Memory security operations */
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006084static int selinux_shm_alloc_security(struct kern_ipc_perm *shp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006087 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006088 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006089 int rc;
6090
Casey Schauflerecd5f822018-11-20 11:55:02 -08006091 isec = selinux_ipc(shp);
6092 ipc_init_security(isec, SECCLASS_SHM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093
Eric Paris50c205f2012-04-04 15:01:43 -04006094 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006095 ad.u.ipc_id = shp->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006096
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006097 rc = avc_has_perm(&selinux_state,
6098 sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099 SHM__CREATE, &ad);
Casey Schauflerecd5f822018-11-20 11:55:02 -08006100 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101}
6102
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006103static int selinux_shm_associate(struct kern_ipc_perm *shp, int shmflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006105 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006106 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006107 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006108
Casey Schaufler7c653822018-09-21 17:19:45 -07006109 isec = selinux_ipc(shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006110
Eric Paris50c205f2012-04-04 15:01:43 -04006111 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006112 ad.u.ipc_id = shp->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006113
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006114 return avc_has_perm(&selinux_state,
6115 sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006116 SHM__ASSOCIATE, &ad);
6117}
6118
6119/* Note, at this point, shp is locked down */
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006120static int selinux_shm_shmctl(struct kern_ipc_perm *shp, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006121{
6122 int perms;
6123 int err;
6124
Eric Paris828dfe12008-04-17 13:17:49 -04006125 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006126 case IPC_INFO:
6127 case SHM_INFO:
6128 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006129 return avc_has_perm(&selinux_state,
6130 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006131 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006132 case IPC_STAT:
6133 case SHM_STAT:
Davidlohr Buesoc21a6972018-04-10 16:35:23 -07006134 case SHM_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006135 perms = SHM__GETATTR | SHM__ASSOCIATE;
6136 break;
6137 case IPC_SET:
6138 perms = SHM__SETATTR;
6139 break;
6140 case SHM_LOCK:
6141 case SHM_UNLOCK:
6142 perms = SHM__LOCK;
6143 break;
6144 case IPC_RMID:
6145 perms = SHM__DESTROY;
6146 break;
6147 default:
6148 return 0;
6149 }
6150
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006151 err = ipc_has_perm(shp, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006152 return err;
6153}
6154
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006155static int selinux_shm_shmat(struct kern_ipc_perm *shp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006156 char __user *shmaddr, int shmflg)
6157{
6158 u32 perms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006159
6160 if (shmflg & SHM_RDONLY)
6161 perms = SHM__READ;
6162 else
6163 perms = SHM__READ | SHM__WRITE;
6164
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006165 return ipc_has_perm(shp, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006166}
6167
6168/* Semaphore security operations */
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006169static int selinux_sem_alloc_security(struct kern_ipc_perm *sma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006170{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006171 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006172 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006173 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006174 int rc;
6175
Casey Schauflerecd5f822018-11-20 11:55:02 -08006176 isec = selinux_ipc(sma);
6177 ipc_init_security(isec, SECCLASS_SEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006178
Eric Paris50c205f2012-04-04 15:01:43 -04006179 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006180 ad.u.ipc_id = sma->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006181
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006182 rc = avc_has_perm(&selinux_state,
6183 sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006184 SEM__CREATE, &ad);
Casey Schauflerecd5f822018-11-20 11:55:02 -08006185 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006186}
6187
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006188static int selinux_sem_associate(struct kern_ipc_perm *sma, int semflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006189{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006190 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006191 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006192 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006193
Casey Schaufler7c653822018-09-21 17:19:45 -07006194 isec = selinux_ipc(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006195
Eric Paris50c205f2012-04-04 15:01:43 -04006196 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006197 ad.u.ipc_id = sma->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006198
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006199 return avc_has_perm(&selinux_state,
6200 sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006201 SEM__ASSOCIATE, &ad);
6202}
6203
6204/* Note, at this point, sma is locked down */
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006205static int selinux_sem_semctl(struct kern_ipc_perm *sma, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006206{
6207 int err;
6208 u32 perms;
6209
Eric Paris828dfe12008-04-17 13:17:49 -04006210 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211 case IPC_INFO:
6212 case SEM_INFO:
6213 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006214 return avc_has_perm(&selinux_state,
6215 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006216 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217 case GETPID:
6218 case GETNCNT:
6219 case GETZCNT:
6220 perms = SEM__GETATTR;
6221 break;
6222 case GETVAL:
6223 case GETALL:
6224 perms = SEM__READ;
6225 break;
6226 case SETVAL:
6227 case SETALL:
6228 perms = SEM__WRITE;
6229 break;
6230 case IPC_RMID:
6231 perms = SEM__DESTROY;
6232 break;
6233 case IPC_SET:
6234 perms = SEM__SETATTR;
6235 break;
6236 case IPC_STAT:
6237 case SEM_STAT:
Davidlohr Buesoa280d6d2018-04-10 16:35:26 -07006238 case SEM_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006239 perms = SEM__GETATTR | SEM__ASSOCIATE;
6240 break;
6241 default:
6242 return 0;
6243 }
6244
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006245 err = ipc_has_perm(sma, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246 return err;
6247}
6248
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006249static int selinux_sem_semop(struct kern_ipc_perm *sma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250 struct sembuf *sops, unsigned nsops, int alter)
6251{
6252 u32 perms;
6253
6254 if (alter)
6255 perms = SEM__READ | SEM__WRITE;
6256 else
6257 perms = SEM__READ;
6258
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006259 return ipc_has_perm(sma, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006260}
6261
6262static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
6263{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006264 u32 av = 0;
6265
Linus Torvalds1da177e2005-04-16 15:20:36 -07006266 av = 0;
6267 if (flag & S_IRUGO)
6268 av |= IPC__UNIX_READ;
6269 if (flag & S_IWUGO)
6270 av |= IPC__UNIX_WRITE;
6271
6272 if (av == 0)
6273 return 0;
6274
Stephen Smalley6af963f2005-05-01 08:58:39 -07006275 return ipc_has_perm(ipcp, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006276}
6277
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02006278static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
6279{
Casey Schaufler7c653822018-09-21 17:19:45 -07006280 struct ipc_security_struct *isec = selinux_ipc(ipcp);
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02006281 *secid = isec->sid;
6282}
6283
Eric Paris828dfe12008-04-17 13:17:49 -04006284static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006285{
6286 if (inode)
6287 inode_doinit_with_dentry(inode, dentry);
6288}
6289
6290static int selinux_getprocattr(struct task_struct *p,
Al Viro04ff9702007-03-12 16:17:58 +00006291 char *name, char **value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006292{
David Howells275bb412008-11-14 10:39:19 +11006293 const struct task_security_struct *__tsec;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00006294 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006295 int error;
Al Viro04ff9702007-03-12 16:17:58 +00006296 unsigned len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006297
David Howells275bb412008-11-14 10:39:19 +11006298 rcu_read_lock();
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07006299 __tsec = selinux_cred(__task_cred(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006300
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006301 if (current != p) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006302 error = avc_has_perm(&selinux_state,
6303 current_sid(), __tsec->sid,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006304 SECCLASS_PROCESS, PROCESS__GETATTR, NULL);
6305 if (error)
6306 goto bad;
6307 }
6308
Linus Torvalds1da177e2005-04-16 15:20:36 -07006309 if (!strcmp(name, "current"))
David Howells275bb412008-11-14 10:39:19 +11006310 sid = __tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006311 else if (!strcmp(name, "prev"))
David Howells275bb412008-11-14 10:39:19 +11006312 sid = __tsec->osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006313 else if (!strcmp(name, "exec"))
David Howells275bb412008-11-14 10:39:19 +11006314 sid = __tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006315 else if (!strcmp(name, "fscreate"))
David Howells275bb412008-11-14 10:39:19 +11006316 sid = __tsec->create_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006317 else if (!strcmp(name, "keycreate"))
David Howells275bb412008-11-14 10:39:19 +11006318 sid = __tsec->keycreate_sid;
Eric Paris42c3e032006-06-26 00:26:03 -07006319 else if (!strcmp(name, "sockcreate"))
David Howells275bb412008-11-14 10:39:19 +11006320 sid = __tsec->sockcreate_sid;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006321 else {
6322 error = -EINVAL;
6323 goto bad;
6324 }
David Howells275bb412008-11-14 10:39:19 +11006325 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006326
6327 if (!sid)
6328 return 0;
6329
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006330 error = security_sid_to_context(&selinux_state, sid, value, &len);
Al Viro04ff9702007-03-12 16:17:58 +00006331 if (error)
6332 return error;
6333 return len;
David Howells275bb412008-11-14 10:39:19 +11006334
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006335bad:
David Howells275bb412008-11-14 10:39:19 +11006336 rcu_read_unlock();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006337 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006338}
6339
Stephen Smalleyb21507e2017-01-09 10:07:31 -05006340static int selinux_setprocattr(const char *name, void *value, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006341{
6342 struct task_security_struct *tsec;
David Howellsd84f4f92008-11-14 10:39:23 +11006343 struct cred *new;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006344 u32 mysid = current_sid(), sid = 0, ptsid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006345 int error;
6346 char *str = value;
6347
Linus Torvalds1da177e2005-04-16 15:20:36 -07006348 /*
6349 * Basic control over ability to set these attributes at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006350 */
6351 if (!strcmp(name, "exec"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006352 error = avc_has_perm(&selinux_state,
6353 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006354 PROCESS__SETEXEC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006355 else if (!strcmp(name, "fscreate"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006356 error = avc_has_perm(&selinux_state,
6357 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006358 PROCESS__SETFSCREATE, NULL);
Michael LeMay4eb582c2006-06-26 00:24:57 -07006359 else if (!strcmp(name, "keycreate"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006360 error = avc_has_perm(&selinux_state,
6361 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006362 PROCESS__SETKEYCREATE, NULL);
Eric Paris42c3e032006-06-26 00:26:03 -07006363 else if (!strcmp(name, "sockcreate"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006364 error = avc_has_perm(&selinux_state,
6365 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006366 PROCESS__SETSOCKCREATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006367 else if (!strcmp(name, "current"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006368 error = avc_has_perm(&selinux_state,
6369 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006370 PROCESS__SETCURRENT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006371 else
6372 error = -EINVAL;
6373 if (error)
6374 return error;
6375
6376 /* Obtain a SID for the context, if one was specified. */
Stephen Smalleya050a572017-01-31 11:54:04 -05006377 if (size && str[0] && str[0] != '\n') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006378 if (str[size-1] == '\n') {
6379 str[size-1] = 0;
6380 size--;
6381 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006382 error = security_context_to_sid(&selinux_state, value, size,
6383 &sid, GFP_KERNEL);
Stephen Smalley12b29f32008-05-07 13:03:20 -04006384 if (error == -EINVAL && !strcmp(name, "fscreate")) {
Stephen Smalleydb590002017-04-20 11:31:30 -04006385 if (!has_cap_mac_admin(true)) {
Eric Parisd6ea83e2012-04-04 13:45:49 -04006386 struct audit_buffer *ab;
6387 size_t audit_size;
6388
6389 /* We strip a nul only if it is at the end, otherwise the
6390 * context contains a nul and we should audit that */
6391 if (str[size - 1] == '\0')
6392 audit_size = size - 1;
6393 else
6394 audit_size = size;
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04006395 ab = audit_log_start(audit_context(),
6396 GFP_ATOMIC,
6397 AUDIT_SELINUX_ERR);
Eric Parisd6ea83e2012-04-04 13:45:49 -04006398 audit_log_format(ab, "op=fscreate invalid_context=");
6399 audit_log_n_untrustedstring(ab, value, audit_size);
6400 audit_log_end(ab);
6401
Stephen Smalley12b29f32008-05-07 13:03:20 -04006402 return error;
Eric Parisd6ea83e2012-04-04 13:45:49 -04006403 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006404 error = security_context_to_sid_force(
6405 &selinux_state,
6406 value, size, &sid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04006407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006408 if (error)
6409 return error;
6410 }
6411
David Howellsd84f4f92008-11-14 10:39:23 +11006412 new = prepare_creds();
6413 if (!new)
6414 return -ENOMEM;
6415
Linus Torvalds1da177e2005-04-16 15:20:36 -07006416 /* Permission checking based on the specified context is
6417 performed during the actual operation (execve,
6418 open/mkdir/...), when we know the full context of the
Eric W. Biedermanb8bff592020-03-22 15:46:24 -05006419 operation. See selinux_bprm_creds_for_exec for the execve
Linus Torvalds1da177e2005-04-16 15:20:36 -07006420 checks and may_create for the file creation checks. The
6421 operation will then fail if the context is not permitted. */
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07006422 tsec = selinux_cred(new);
David Howellsd84f4f92008-11-14 10:39:23 +11006423 if (!strcmp(name, "exec")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006424 tsec->exec_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006425 } else if (!strcmp(name, "fscreate")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006426 tsec->create_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006427 } else if (!strcmp(name, "keycreate")) {
Ondrej Mosnacek464c2582019-06-12 10:12:26 +02006428 if (sid) {
6429 error = avc_has_perm(&selinux_state, mysid, sid,
6430 SECCLASS_KEY, KEY__CREATE, NULL);
6431 if (error)
6432 goto abort_change;
6433 }
Michael LeMay4eb582c2006-06-26 00:24:57 -07006434 tsec->keycreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006435 } else if (!strcmp(name, "sockcreate")) {
Eric Paris42c3e032006-06-26 00:26:03 -07006436 tsec->sockcreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006437 } else if (!strcmp(name, "current")) {
6438 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006439 if (sid == 0)
David Howellsd84f4f92008-11-14 10:39:23 +11006440 goto abort_change;
KaiGai Koheid9250de2008-08-28 16:35:57 +09006441
David Howellsd84f4f92008-11-14 10:39:23 +11006442 /* Only allow single threaded processes to change context */
6443 error = -EPERM;
Oleg Nesterov5bb459b2009-07-10 03:48:23 +02006444 if (!current_is_single_threaded()) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006445 error = security_bounded_transition(&selinux_state,
6446 tsec->sid, sid);
David Howellsd84f4f92008-11-14 10:39:23 +11006447 if (error)
6448 goto abort_change;
Eric Paris828dfe12008-04-17 13:17:49 -04006449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006450
6451 /* Check permissions for the transition. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006452 error = avc_has_perm(&selinux_state,
6453 tsec->sid, sid, SECCLASS_PROCESS,
Eric Paris828dfe12008-04-17 13:17:49 -04006454 PROCESS__DYNTRANSITION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006455 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11006456 goto abort_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006457
6458 /* Check for ptracing, and update the task SID if ok.
6459 Otherwise, leave SID unchanged and fail. */
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006460 ptsid = ptrace_parent_sid();
Paul Moore0c6181c2016-03-30 21:41:21 -04006461 if (ptsid != 0) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006462 error = avc_has_perm(&selinux_state,
6463 ptsid, sid, SECCLASS_PROCESS,
David Howellsd84f4f92008-11-14 10:39:23 +11006464 PROCESS__PTRACE, NULL);
6465 if (error)
6466 goto abort_change;
6467 }
6468
6469 tsec->sid = sid;
6470 } else {
6471 error = -EINVAL;
6472 goto abort_change;
6473 }
6474
6475 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006476 return size;
David Howellsd84f4f92008-11-14 10:39:23 +11006477
6478abort_change:
6479 abort_creds(new);
6480 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006481}
6482
David Quigley746df9b2013-05-22 12:50:35 -04006483static int selinux_ismaclabel(const char *name)
6484{
6485 return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0);
6486}
6487
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006488static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
6489{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006490 return security_sid_to_context(&selinux_state, secid,
6491 secdata, seclen);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006492}
6493
David Howells7bf570d2008-04-29 20:52:51 +01006494static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
David Howells63cb3442008-01-15 23:47:35 +00006495{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006496 return security_context_to_sid(&selinux_state, secdata, seclen,
6497 secid, GFP_KERNEL);
David Howells63cb3442008-01-15 23:47:35 +00006498}
6499
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006500static void selinux_release_secctx(char *secdata, u32 seclen)
6501{
Paul Moore088999e2007-08-01 11:12:58 -04006502 kfree(secdata);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006503}
6504
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006505static void selinux_inode_invalidate_secctx(struct inode *inode)
6506{
Casey Schaufler80788c22018-09-21 17:19:11 -07006507 struct inode_security_struct *isec = selinux_inode(inode);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006508
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01006509 spin_lock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006510 isec->initialized = LABEL_INVALID;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01006511 spin_unlock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006512}
6513
David P. Quigley1ee65e32009-09-03 14:25:57 -04006514/*
6515 * called with inode->i_mutex locked
6516 */
6517static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
6518{
Ondrej Mosnacek53e0c2a2018-12-21 21:18:53 +01006519 int rc = selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX,
6520 ctx, ctxlen, 0);
6521 /* Do not return error when suppressing label (SBLABEL_MNT not set). */
6522 return rc == -EOPNOTSUPP ? 0 : rc;
David P. Quigley1ee65e32009-09-03 14:25:57 -04006523}
6524
6525/*
6526 * called with inode->i_mutex locked
6527 */
6528static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
6529{
6530 return __vfs_setxattr_noperm(dentry, XATTR_NAME_SELINUX, ctx, ctxlen, 0);
6531}
6532
6533static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
6534{
6535 int len = 0;
6536 len = selinux_inode_getsecurity(inode, XATTR_SELINUX_SUFFIX,
6537 ctx, true);
6538 if (len < 0)
6539 return len;
6540 *ctxlen = len;
6541 return 0;
6542}
Michael LeMayd7200242006-06-22 14:47:17 -07006543#ifdef CONFIG_KEYS
6544
David Howellsd84f4f92008-11-14 10:39:23 +11006545static int selinux_key_alloc(struct key *k, const struct cred *cred,
David Howells7e047ef2006-06-26 00:24:50 -07006546 unsigned long flags)
Michael LeMayd7200242006-06-22 14:47:17 -07006547{
David Howellsd84f4f92008-11-14 10:39:23 +11006548 const struct task_security_struct *tsec;
Michael LeMayd7200242006-06-22 14:47:17 -07006549 struct key_security_struct *ksec;
6550
6551 ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
6552 if (!ksec)
6553 return -ENOMEM;
6554
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07006555 tsec = selinux_cred(cred);
David Howellsd84f4f92008-11-14 10:39:23 +11006556 if (tsec->keycreate_sid)
6557 ksec->sid = tsec->keycreate_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006558 else
David Howellsd84f4f92008-11-14 10:39:23 +11006559 ksec->sid = tsec->sid;
Michael LeMayd7200242006-06-22 14:47:17 -07006560
David Howells275bb412008-11-14 10:39:19 +11006561 k->security = ksec;
Michael LeMayd7200242006-06-22 14:47:17 -07006562 return 0;
6563}
6564
6565static void selinux_key_free(struct key *k)
6566{
6567 struct key_security_struct *ksec = k->security;
6568
6569 k->security = NULL;
6570 kfree(ksec);
6571}
6572
6573static int selinux_key_permission(key_ref_t key_ref,
David Howellsd84f4f92008-11-14 10:39:23 +11006574 const struct cred *cred,
David Howells8c0637e2020-05-12 15:16:29 +01006575 enum key_need_perm need_perm)
Michael LeMayd7200242006-06-22 14:47:17 -07006576{
6577 struct key *key;
Michael LeMayd7200242006-06-22 14:47:17 -07006578 struct key_security_struct *ksec;
David Howells8c0637e2020-05-12 15:16:29 +01006579 u32 perm, sid;
Michael LeMayd7200242006-06-22 14:47:17 -07006580
David Howells8c0637e2020-05-12 15:16:29 +01006581 switch (need_perm) {
6582 case KEY_NEED_VIEW:
6583 perm = KEY__VIEW;
6584 break;
6585 case KEY_NEED_READ:
6586 perm = KEY__READ;
6587 break;
6588 case KEY_NEED_WRITE:
6589 perm = KEY__WRITE;
6590 break;
6591 case KEY_NEED_SEARCH:
6592 perm = KEY__SEARCH;
6593 break;
6594 case KEY_NEED_LINK:
6595 perm = KEY__LINK;
6596 break;
6597 case KEY_NEED_SETATTR:
6598 perm = KEY__SETATTR;
6599 break;
6600 case KEY_NEED_UNLINK:
6601 case KEY_SYSADMIN_OVERRIDE:
6602 case KEY_AUTHTOKEN_OVERRIDE:
6603 case KEY_DEFER_PERM_CHECK:
Michael LeMayd7200242006-06-22 14:47:17 -07006604 return 0;
David Howells8c0637e2020-05-12 15:16:29 +01006605 default:
6606 WARN_ON(1);
6607 return -EPERM;
6608
6609 }
Michael LeMayd7200242006-06-22 14:47:17 -07006610
David Howellsd84f4f92008-11-14 10:39:23 +11006611 sid = cred_sid(cred);
David Howells275bb412008-11-14 10:39:19 +11006612 key = key_ref_to_ptr(key_ref);
6613 ksec = key->security;
6614
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006615 return avc_has_perm(&selinux_state,
Linus Torvalds028db3e2019-07-10 18:43:43 -07006616 sid, ksec->sid, SECCLASS_KEY, perm, NULL);
Michael LeMayd7200242006-06-22 14:47:17 -07006617}
6618
David Howells70a5bb72008-04-29 01:01:26 -07006619static int selinux_key_getsecurity(struct key *key, char **_buffer)
6620{
6621 struct key_security_struct *ksec = key->security;
6622 char *context = NULL;
6623 unsigned len;
6624 int rc;
6625
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006626 rc = security_sid_to_context(&selinux_state, ksec->sid,
6627 &context, &len);
David Howells70a5bb72008-04-29 01:01:26 -07006628 if (!rc)
6629 rc = len;
6630 *_buffer = context;
6631 return rc;
6632}
David Howells3e412cc2020-01-14 17:07:13 +00006633
6634#ifdef CONFIG_KEY_NOTIFICATIONS
6635static int selinux_watch_key(struct key *key)
6636{
6637 struct key_security_struct *ksec = key->security;
6638 u32 sid = current_sid();
6639
6640 return avc_has_perm(&selinux_state,
6641 sid, ksec->sid, SECCLASS_KEY, KEY__VIEW, NULL);
6642}
6643#endif
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006644#endif
David Howells70a5bb72008-04-29 01:01:26 -07006645
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006646#ifdef CONFIG_SECURITY_INFINIBAND
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006647static int selinux_ib_pkey_access(void *ib_sec, u64 subnet_prefix, u16 pkey_val)
6648{
6649 struct common_audit_data ad;
6650 int err;
6651 u32 sid = 0;
6652 struct ib_security_struct *sec = ib_sec;
6653 struct lsm_ibpkey_audit ibpkey;
6654
Daniel Jurgens409dcf32017-05-19 15:48:59 +03006655 err = sel_ib_pkey_sid(subnet_prefix, pkey_val, &sid);
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006656 if (err)
6657 return err;
6658
6659 ad.type = LSM_AUDIT_DATA_IBPKEY;
6660 ibpkey.subnet_prefix = subnet_prefix;
6661 ibpkey.pkey = pkey_val;
6662 ad.u.ibpkey = &ibpkey;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006663 return avc_has_perm(&selinux_state,
6664 sec->sid, sid,
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006665 SECCLASS_INFINIBAND_PKEY,
6666 INFINIBAND_PKEY__ACCESS, &ad);
6667}
6668
Daniel Jurgensab861df2017-05-19 15:48:58 +03006669static int selinux_ib_endport_manage_subnet(void *ib_sec, const char *dev_name,
6670 u8 port_num)
6671{
6672 struct common_audit_data ad;
6673 int err;
6674 u32 sid = 0;
6675 struct ib_security_struct *sec = ib_sec;
6676 struct lsm_ibendport_audit ibendport;
6677
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006678 err = security_ib_endport_sid(&selinux_state, dev_name, port_num,
6679 &sid);
Daniel Jurgensab861df2017-05-19 15:48:58 +03006680
6681 if (err)
6682 return err;
6683
6684 ad.type = LSM_AUDIT_DATA_IBENDPORT;
6685 strncpy(ibendport.dev_name, dev_name, sizeof(ibendport.dev_name));
6686 ibendport.port = port_num;
6687 ad.u.ibendport = &ibendport;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006688 return avc_has_perm(&selinux_state,
6689 sec->sid, sid,
Daniel Jurgensab861df2017-05-19 15:48:58 +03006690 SECCLASS_INFINIBAND_ENDPORT,
6691 INFINIBAND_ENDPORT__MANAGE_SUBNET, &ad);
6692}
6693
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006694static int selinux_ib_alloc_security(void **ib_sec)
6695{
6696 struct ib_security_struct *sec;
6697
6698 sec = kzalloc(sizeof(*sec), GFP_KERNEL);
6699 if (!sec)
6700 return -ENOMEM;
6701 sec->sid = current_sid();
6702
6703 *ib_sec = sec;
6704 return 0;
6705}
6706
6707static void selinux_ib_free_security(void *ib_sec)
6708{
6709 kfree(ib_sec);
6710}
Michael LeMayd7200242006-06-22 14:47:17 -07006711#endif
6712
Chenbo Fengec27c352017-10-18 13:00:25 -07006713#ifdef CONFIG_BPF_SYSCALL
6714static int selinux_bpf(int cmd, union bpf_attr *attr,
6715 unsigned int size)
6716{
6717 u32 sid = current_sid();
6718 int ret;
6719
6720 switch (cmd) {
6721 case BPF_MAP_CREATE:
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006722 ret = avc_has_perm(&selinux_state,
6723 sid, sid, SECCLASS_BPF, BPF__MAP_CREATE,
Chenbo Fengec27c352017-10-18 13:00:25 -07006724 NULL);
6725 break;
6726 case BPF_PROG_LOAD:
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006727 ret = avc_has_perm(&selinux_state,
6728 sid, sid, SECCLASS_BPF, BPF__PROG_LOAD,
Chenbo Fengec27c352017-10-18 13:00:25 -07006729 NULL);
6730 break;
6731 default:
6732 ret = 0;
6733 break;
6734 }
6735
6736 return ret;
6737}
6738
6739static u32 bpf_map_fmode_to_av(fmode_t fmode)
6740{
6741 u32 av = 0;
6742
6743 if (fmode & FMODE_READ)
6744 av |= BPF__MAP_READ;
6745 if (fmode & FMODE_WRITE)
6746 av |= BPF__MAP_WRITE;
6747 return av;
6748}
6749
Chenbo Fengf66e4482017-10-18 13:00:26 -07006750/* This function will check the file pass through unix socket or binder to see
6751 * if it is a bpf related object. And apply correspinding checks on the bpf
6752 * object based on the type. The bpf maps and programs, not like other files and
6753 * socket, are using a shared anonymous inode inside the kernel as their inode.
6754 * So checking that inode cannot identify if the process have privilege to
6755 * access the bpf object and that's why we have to add this additional check in
6756 * selinux_file_receive and selinux_binder_transfer_files.
6757 */
6758static int bpf_fd_pass(struct file *file, u32 sid)
6759{
6760 struct bpf_security_struct *bpfsec;
6761 struct bpf_prog *prog;
6762 struct bpf_map *map;
6763 int ret;
6764
6765 if (file->f_op == &bpf_map_fops) {
6766 map = file->private_data;
6767 bpfsec = map->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006768 ret = avc_has_perm(&selinux_state,
6769 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengf66e4482017-10-18 13:00:26 -07006770 bpf_map_fmode_to_av(file->f_mode), NULL);
6771 if (ret)
6772 return ret;
6773 } else if (file->f_op == &bpf_prog_fops) {
6774 prog = file->private_data;
6775 bpfsec = prog->aux->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006776 ret = avc_has_perm(&selinux_state,
6777 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengf66e4482017-10-18 13:00:26 -07006778 BPF__PROG_RUN, NULL);
6779 if (ret)
6780 return ret;
6781 }
6782 return 0;
6783}
6784
Chenbo Fengec27c352017-10-18 13:00:25 -07006785static int selinux_bpf_map(struct bpf_map *map, fmode_t fmode)
6786{
6787 u32 sid = current_sid();
6788 struct bpf_security_struct *bpfsec;
6789
6790 bpfsec = map->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006791 return avc_has_perm(&selinux_state,
6792 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengec27c352017-10-18 13:00:25 -07006793 bpf_map_fmode_to_av(fmode), NULL);
6794}
6795
6796static int selinux_bpf_prog(struct bpf_prog *prog)
6797{
6798 u32 sid = current_sid();
6799 struct bpf_security_struct *bpfsec;
6800
6801 bpfsec = prog->aux->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006802 return avc_has_perm(&selinux_state,
6803 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengec27c352017-10-18 13:00:25 -07006804 BPF__PROG_RUN, NULL);
6805}
6806
6807static int selinux_bpf_map_alloc(struct bpf_map *map)
6808{
6809 struct bpf_security_struct *bpfsec;
6810
6811 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6812 if (!bpfsec)
6813 return -ENOMEM;
6814
6815 bpfsec->sid = current_sid();
6816 map->security = bpfsec;
6817
6818 return 0;
6819}
6820
6821static void selinux_bpf_map_free(struct bpf_map *map)
6822{
6823 struct bpf_security_struct *bpfsec = map->security;
6824
6825 map->security = NULL;
6826 kfree(bpfsec);
6827}
6828
6829static int selinux_bpf_prog_alloc(struct bpf_prog_aux *aux)
6830{
6831 struct bpf_security_struct *bpfsec;
6832
6833 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6834 if (!bpfsec)
6835 return -ENOMEM;
6836
6837 bpfsec->sid = current_sid();
6838 aux->security = bpfsec;
6839
6840 return 0;
6841}
6842
6843static void selinux_bpf_prog_free(struct bpf_prog_aux *aux)
6844{
6845 struct bpf_security_struct *bpfsec = aux->security;
6846
6847 aux->security = NULL;
6848 kfree(bpfsec);
6849}
6850#endif
6851
Stephen Smalley59438b462019-11-27 12:04:36 -05006852static int selinux_lockdown(enum lockdown_reason what)
6853{
6854 struct common_audit_data ad;
6855 u32 sid = current_sid();
6856 int invalid_reason = (what <= LOCKDOWN_NONE) ||
6857 (what == LOCKDOWN_INTEGRITY_MAX) ||
6858 (what >= LOCKDOWN_CONFIDENTIALITY_MAX);
6859
6860 if (WARN(invalid_reason, "Invalid lockdown reason")) {
6861 audit_log(audit_context(),
6862 GFP_ATOMIC, AUDIT_SELINUX_ERR,
6863 "lockdown_reason=invalid");
6864 return -EINVAL;
6865 }
6866
6867 ad.type = LSM_AUDIT_DATA_LOCKDOWN;
6868 ad.u.reason = what;
6869
6870 if (what <= LOCKDOWN_INTEGRITY_MAX)
6871 return avc_has_perm(&selinux_state,
6872 sid, sid, SECCLASS_LOCKDOWN,
6873 LOCKDOWN__INTEGRITY, &ad);
6874 else
6875 return avc_has_perm(&selinux_state,
6876 sid, sid, SECCLASS_LOCKDOWN,
6877 LOCKDOWN__CONFIDENTIALITY, &ad);
6878}
6879
Casey Schauflerbbd36622018-11-12 09:30:56 -08006880struct lsm_blob_sizes selinux_blob_sizes __lsm_ro_after_init = {
6881 .lbs_cred = sizeof(struct task_security_struct),
Casey Schaufler33bf60c2018-11-12 12:02:49 -08006882 .lbs_file = sizeof(struct file_security_struct),
Casey Schauflerafb1cbe32018-09-21 17:19:29 -07006883 .lbs_inode = sizeof(struct inode_security_struct),
Casey Schauflerecd5f822018-11-20 11:55:02 -08006884 .lbs_ipc = sizeof(struct ipc_security_struct),
6885 .lbs_msg_msg = sizeof(struct msg_security_struct),
Casey Schauflerbbd36622018-11-12 09:30:56 -08006886};
6887
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -04006888#ifdef CONFIG_PERF_EVENTS
6889static int selinux_perf_event_open(struct perf_event_attr *attr, int type)
6890{
6891 u32 requested, sid = current_sid();
6892
6893 if (type == PERF_SECURITY_OPEN)
6894 requested = PERF_EVENT__OPEN;
6895 else if (type == PERF_SECURITY_CPU)
6896 requested = PERF_EVENT__CPU;
6897 else if (type == PERF_SECURITY_KERNEL)
6898 requested = PERF_EVENT__KERNEL;
6899 else if (type == PERF_SECURITY_TRACEPOINT)
6900 requested = PERF_EVENT__TRACEPOINT;
6901 else
6902 return -EINVAL;
6903
6904 return avc_has_perm(&selinux_state, sid, sid, SECCLASS_PERF_EVENT,
6905 requested, NULL);
6906}
6907
6908static int selinux_perf_event_alloc(struct perf_event *event)
6909{
6910 struct perf_event_security_struct *perfsec;
6911
6912 perfsec = kzalloc(sizeof(*perfsec), GFP_KERNEL);
6913 if (!perfsec)
6914 return -ENOMEM;
6915
6916 perfsec->sid = current_sid();
6917 event->security = perfsec;
6918
6919 return 0;
6920}
6921
6922static void selinux_perf_event_free(struct perf_event *event)
6923{
6924 struct perf_event_security_struct *perfsec = event->security;
6925
6926 event->security = NULL;
6927 kfree(perfsec);
6928}
6929
6930static int selinux_perf_event_read(struct perf_event *event)
6931{
6932 struct perf_event_security_struct *perfsec = event->security;
6933 u32 sid = current_sid();
6934
6935 return avc_has_perm(&selinux_state, sid, perfsec->sid,
6936 SECCLASS_PERF_EVENT, PERF_EVENT__READ, NULL);
6937}
6938
6939static int selinux_perf_event_write(struct perf_event *event)
6940{
6941 struct perf_event_security_struct *perfsec = event->security;
6942 u32 sid = current_sid();
6943
6944 return avc_has_perm(&selinux_state, sid, perfsec->sid,
6945 SECCLASS_PERF_EVENT, PERF_EVENT__WRITE, NULL);
6946}
6947#endif
6948
Ondrej Mosnacekcfff75d2020-01-08 15:09:58 +01006949/*
6950 * IMPORTANT NOTE: When adding new hooks, please be careful to keep this order:
6951 * 1. any hooks that don't belong to (2.) or (3.) below,
6952 * 2. hooks that both access structures allocated by other hooks, and allocate
6953 * structures that can be later accessed by other hooks (mostly "cloning"
6954 * hooks),
6955 * 3. hooks that only allocate structures that can be later accessed by other
6956 * hooks ("allocating" hooks).
6957 *
6958 * Please follow block comment delimiters in the list to keep this order.
6959 *
6960 * This ordering is needed for SELinux runtime disable to work at least somewhat
6961 * safely. Breaking the ordering rules above might lead to NULL pointer derefs
6962 * when disabling SELinux at runtime.
6963 */
James Morrisca97d932017-02-15 00:18:51 +11006964static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {
Casey Schauflere20b0432015-05-02 15:11:36 -07006965 LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr),
6966 LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction),
6967 LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder),
6968 LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file),
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02006969
Casey Schauflere20b0432015-05-02 15:11:36 -07006970 LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check),
6971 LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme),
6972 LSM_HOOK_INIT(capget, selinux_capget),
6973 LSM_HOOK_INIT(capset, selinux_capset),
6974 LSM_HOOK_INIT(capable, selinux_capable),
6975 LSM_HOOK_INIT(quotactl, selinux_quotactl),
6976 LSM_HOOK_INIT(quota_on, selinux_quota_on),
6977 LSM_HOOK_INIT(syslog, selinux_syslog),
6978 LSM_HOOK_INIT(vm_enough_memory, selinux_vm_enough_memory),
Stephen Smalley79af7302015-01-21 10:54:10 -05006979
Casey Schauflere20b0432015-05-02 15:11:36 -07006980 LSM_HOOK_INIT(netlink_send, selinux_netlink_send),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006981
Eric W. Biedermanb8bff592020-03-22 15:46:24 -05006982 LSM_HOOK_INIT(bprm_creds_for_exec, selinux_bprm_creds_for_exec),
Casey Schauflere20b0432015-05-02 15:11:36 -07006983 LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds),
6984 LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006985
Casey Schauflere20b0432015-05-02 15:11:36 -07006986 LSM_HOOK_INIT(sb_free_security, selinux_sb_free_security),
Al Viro204cc0c2018-12-13 13:41:47 -05006987 LSM_HOOK_INIT(sb_free_mnt_opts, selinux_free_mnt_opts),
Casey Schauflere20b0432015-05-02 15:11:36 -07006988 LSM_HOOK_INIT(sb_remount, selinux_sb_remount),
6989 LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount),
6990 LSM_HOOK_INIT(sb_show_options, selinux_sb_show_options),
6991 LSM_HOOK_INIT(sb_statfs, selinux_sb_statfs),
6992 LSM_HOOK_INIT(sb_mount, selinux_mount),
6993 LSM_HOOK_INIT(sb_umount, selinux_umount),
6994 LSM_HOOK_INIT(sb_set_mnt_opts, selinux_set_mnt_opts),
6995 LSM_HOOK_INIT(sb_clone_mnt_opts, selinux_sb_clone_mnt_opts),
Linus Torvalds1da177e2005-04-16 15:20:36 -07006996
Stephen Smalley98aa0032020-01-17 15:24:07 -05006997 LSM_HOOK_INIT(move_mount, selinux_move_mount),
6998
Casey Schauflere20b0432015-05-02 15:11:36 -07006999 LSM_HOOK_INIT(dentry_init_security, selinux_dentry_init_security),
Vivek Goyala518b0a2016-07-13 10:44:53 -04007000 LSM_HOOK_INIT(dentry_create_files_as, selinux_dentry_create_files_as),
Eric Parise0007522008-03-05 10:31:54 -05007001
Casey Schauflere20b0432015-05-02 15:11:36 -07007002 LSM_HOOK_INIT(inode_free_security, selinux_inode_free_security),
7003 LSM_HOOK_INIT(inode_init_security, selinux_inode_init_security),
7004 LSM_HOOK_INIT(inode_create, selinux_inode_create),
7005 LSM_HOOK_INIT(inode_link, selinux_inode_link),
7006 LSM_HOOK_INIT(inode_unlink, selinux_inode_unlink),
7007 LSM_HOOK_INIT(inode_symlink, selinux_inode_symlink),
7008 LSM_HOOK_INIT(inode_mkdir, selinux_inode_mkdir),
7009 LSM_HOOK_INIT(inode_rmdir, selinux_inode_rmdir),
7010 LSM_HOOK_INIT(inode_mknod, selinux_inode_mknod),
7011 LSM_HOOK_INIT(inode_rename, selinux_inode_rename),
7012 LSM_HOOK_INIT(inode_readlink, selinux_inode_readlink),
7013 LSM_HOOK_INIT(inode_follow_link, selinux_inode_follow_link),
7014 LSM_HOOK_INIT(inode_permission, selinux_inode_permission),
7015 LSM_HOOK_INIT(inode_setattr, selinux_inode_setattr),
7016 LSM_HOOK_INIT(inode_getattr, selinux_inode_getattr),
7017 LSM_HOOK_INIT(inode_setxattr, selinux_inode_setxattr),
7018 LSM_HOOK_INIT(inode_post_setxattr, selinux_inode_post_setxattr),
7019 LSM_HOOK_INIT(inode_getxattr, selinux_inode_getxattr),
7020 LSM_HOOK_INIT(inode_listxattr, selinux_inode_listxattr),
7021 LSM_HOOK_INIT(inode_removexattr, selinux_inode_removexattr),
7022 LSM_HOOK_INIT(inode_getsecurity, selinux_inode_getsecurity),
7023 LSM_HOOK_INIT(inode_setsecurity, selinux_inode_setsecurity),
7024 LSM_HOOK_INIT(inode_listsecurity, selinux_inode_listsecurity),
7025 LSM_HOOK_INIT(inode_getsecid, selinux_inode_getsecid),
Vivek Goyal56909eb2016-07-13 10:44:48 -04007026 LSM_HOOK_INIT(inode_copy_up, selinux_inode_copy_up),
Vivek Goyal19472b62016-07-13 10:44:50 -04007027 LSM_HOOK_INIT(inode_copy_up_xattr, selinux_inode_copy_up_xattr),
Aaron Goidelac5656d2019-08-12 11:20:00 -04007028 LSM_HOOK_INIT(path_notify, selinux_path_notify),
Linus Torvalds1da177e2005-04-16 15:20:36 -07007029
Ondrej Mosnacekec882da2019-02-22 15:57:17 +01007030 LSM_HOOK_INIT(kernfs_init_security, selinux_kernfs_init_security),
7031
Casey Schauflere20b0432015-05-02 15:11:36 -07007032 LSM_HOOK_INIT(file_permission, selinux_file_permission),
7033 LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07007034 LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl),
7035 LSM_HOOK_INIT(mmap_file, selinux_mmap_file),
7036 LSM_HOOK_INIT(mmap_addr, selinux_mmap_addr),
7037 LSM_HOOK_INIT(file_mprotect, selinux_file_mprotect),
7038 LSM_HOOK_INIT(file_lock, selinux_file_lock),
7039 LSM_HOOK_INIT(file_fcntl, selinux_file_fcntl),
7040 LSM_HOOK_INIT(file_set_fowner, selinux_file_set_fowner),
7041 LSM_HOOK_INIT(file_send_sigiotask, selinux_file_send_sigiotask),
7042 LSM_HOOK_INIT(file_receive, selinux_file_receive),
Linus Torvalds1da177e2005-04-16 15:20:36 -07007043
Casey Schauflere20b0432015-05-02 15:11:36 -07007044 LSM_HOOK_INIT(file_open, selinux_file_open),
Linus Torvalds1da177e2005-04-16 15:20:36 -07007045
Tetsuo Handaa79be232017-03-28 23:08:45 +09007046 LSM_HOOK_INIT(task_alloc, selinux_task_alloc),
Casey Schauflere20b0432015-05-02 15:11:36 -07007047 LSM_HOOK_INIT(cred_prepare, selinux_cred_prepare),
7048 LSM_HOOK_INIT(cred_transfer, selinux_cred_transfer),
Matthew Garrett3ec30112018-01-08 13:36:19 -08007049 LSM_HOOK_INIT(cred_getsecid, selinux_cred_getsecid),
Casey Schauflere20b0432015-05-02 15:11:36 -07007050 LSM_HOOK_INIT(kernel_act_as, selinux_kernel_act_as),
7051 LSM_HOOK_INIT(kernel_create_files_as, selinux_kernel_create_files_as),
7052 LSM_HOOK_INIT(kernel_module_request, selinux_kernel_module_request),
Mimi Zoharc77b8cd2018-07-13 14:06:02 -04007053 LSM_HOOK_INIT(kernel_load_data, selinux_kernel_load_data),
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07007054 LSM_HOOK_INIT(kernel_read_file, selinux_kernel_read_file),
Casey Schauflere20b0432015-05-02 15:11:36 -07007055 LSM_HOOK_INIT(task_setpgid, selinux_task_setpgid),
7056 LSM_HOOK_INIT(task_getpgid, selinux_task_getpgid),
7057 LSM_HOOK_INIT(task_getsid, selinux_task_getsid),
7058 LSM_HOOK_INIT(task_getsecid, selinux_task_getsecid),
7059 LSM_HOOK_INIT(task_setnice, selinux_task_setnice),
7060 LSM_HOOK_INIT(task_setioprio, selinux_task_setioprio),
7061 LSM_HOOK_INIT(task_getioprio, selinux_task_getioprio),
Stephen Smalley791ec492017-02-17 07:57:00 -05007062 LSM_HOOK_INIT(task_prlimit, selinux_task_prlimit),
Casey Schauflere20b0432015-05-02 15:11:36 -07007063 LSM_HOOK_INIT(task_setrlimit, selinux_task_setrlimit),
7064 LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler),
7065 LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler),
7066 LSM_HOOK_INIT(task_movememory, selinux_task_movememory),
7067 LSM_HOOK_INIT(task_kill, selinux_task_kill),
Casey Schauflere20b0432015-05-02 15:11:36 -07007068 LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode),
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09007069
Casey Schauflere20b0432015-05-02 15:11:36 -07007070 LSM_HOOK_INIT(ipc_permission, selinux_ipc_permission),
7071 LSM_HOOK_INIT(ipc_getsecid, selinux_ipc_getsecid),
Linus Torvalds1da177e2005-04-16 15:20:36 -07007072
Casey Schauflere20b0432015-05-02 15:11:36 -07007073 LSM_HOOK_INIT(msg_queue_associate, selinux_msg_queue_associate),
7074 LSM_HOOK_INIT(msg_queue_msgctl, selinux_msg_queue_msgctl),
7075 LSM_HOOK_INIT(msg_queue_msgsnd, selinux_msg_queue_msgsnd),
7076 LSM_HOOK_INIT(msg_queue_msgrcv, selinux_msg_queue_msgrcv),
Linus Torvalds1da177e2005-04-16 15:20:36 -07007077
Casey Schauflere20b0432015-05-02 15:11:36 -07007078 LSM_HOOK_INIT(shm_associate, selinux_shm_associate),
7079 LSM_HOOK_INIT(shm_shmctl, selinux_shm_shmctl),
7080 LSM_HOOK_INIT(shm_shmat, selinux_shm_shmat),
Linus Torvalds1da177e2005-04-16 15:20:36 -07007081
Casey Schauflere20b0432015-05-02 15:11:36 -07007082 LSM_HOOK_INIT(sem_associate, selinux_sem_associate),
7083 LSM_HOOK_INIT(sem_semctl, selinux_sem_semctl),
7084 LSM_HOOK_INIT(sem_semop, selinux_sem_semop),
Linus Torvalds1da177e2005-04-16 15:20:36 -07007085
Casey Schauflere20b0432015-05-02 15:11:36 -07007086 LSM_HOOK_INIT(d_instantiate, selinux_d_instantiate),
Linus Torvalds1da177e2005-04-16 15:20:36 -07007087
Casey Schauflere20b0432015-05-02 15:11:36 -07007088 LSM_HOOK_INIT(getprocattr, selinux_getprocattr),
7089 LSM_HOOK_INIT(setprocattr, selinux_setprocattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07007090
Casey Schauflere20b0432015-05-02 15:11:36 -07007091 LSM_HOOK_INIT(ismaclabel, selinux_ismaclabel),
Casey Schauflere20b0432015-05-02 15:11:36 -07007092 LSM_HOOK_INIT(secctx_to_secid, selinux_secctx_to_secid),
7093 LSM_HOOK_INIT(release_secctx, selinux_release_secctx),
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05007094 LSM_HOOK_INIT(inode_invalidate_secctx, selinux_inode_invalidate_secctx),
Casey Schauflere20b0432015-05-02 15:11:36 -07007095 LSM_HOOK_INIT(inode_notifysecctx, selinux_inode_notifysecctx),
7096 LSM_HOOK_INIT(inode_setsecctx, selinux_inode_setsecctx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07007097
Casey Schauflere20b0432015-05-02 15:11:36 -07007098 LSM_HOOK_INIT(unix_stream_connect, selinux_socket_unix_stream_connect),
7099 LSM_HOOK_INIT(unix_may_send, selinux_socket_unix_may_send),
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07007100
Casey Schauflere20b0432015-05-02 15:11:36 -07007101 LSM_HOOK_INIT(socket_create, selinux_socket_create),
7102 LSM_HOOK_INIT(socket_post_create, selinux_socket_post_create),
David Herrmann0b811db2018-05-04 16:28:21 +02007103 LSM_HOOK_INIT(socket_socketpair, selinux_socket_socketpair),
Casey Schauflere20b0432015-05-02 15:11:36 -07007104 LSM_HOOK_INIT(socket_bind, selinux_socket_bind),
7105 LSM_HOOK_INIT(socket_connect, selinux_socket_connect),
7106 LSM_HOOK_INIT(socket_listen, selinux_socket_listen),
7107 LSM_HOOK_INIT(socket_accept, selinux_socket_accept),
7108 LSM_HOOK_INIT(socket_sendmsg, selinux_socket_sendmsg),
7109 LSM_HOOK_INIT(socket_recvmsg, selinux_socket_recvmsg),
7110 LSM_HOOK_INIT(socket_getsockname, selinux_socket_getsockname),
7111 LSM_HOOK_INIT(socket_getpeername, selinux_socket_getpeername),
7112 LSM_HOOK_INIT(socket_getsockopt, selinux_socket_getsockopt),
7113 LSM_HOOK_INIT(socket_setsockopt, selinux_socket_setsockopt),
7114 LSM_HOOK_INIT(socket_shutdown, selinux_socket_shutdown),
7115 LSM_HOOK_INIT(socket_sock_rcv_skb, selinux_socket_sock_rcv_skb),
7116 LSM_HOOK_INIT(socket_getpeersec_stream,
7117 selinux_socket_getpeersec_stream),
7118 LSM_HOOK_INIT(socket_getpeersec_dgram, selinux_socket_getpeersec_dgram),
Casey Schauflere20b0432015-05-02 15:11:36 -07007119 LSM_HOOK_INIT(sk_free_security, selinux_sk_free_security),
7120 LSM_HOOK_INIT(sk_clone_security, selinux_sk_clone_security),
7121 LSM_HOOK_INIT(sk_getsecid, selinux_sk_getsecid),
7122 LSM_HOOK_INIT(sock_graft, selinux_sock_graft),
Richard Hainesd4529302018-02-13 20:57:18 +00007123 LSM_HOOK_INIT(sctp_assoc_request, selinux_sctp_assoc_request),
7124 LSM_HOOK_INIT(sctp_sk_clone, selinux_sctp_sk_clone),
7125 LSM_HOOK_INIT(sctp_bind_connect, selinux_sctp_bind_connect),
Casey Schauflere20b0432015-05-02 15:11:36 -07007126 LSM_HOOK_INIT(inet_conn_request, selinux_inet_conn_request),
7127 LSM_HOOK_INIT(inet_csk_clone, selinux_inet_csk_clone),
7128 LSM_HOOK_INIT(inet_conn_established, selinux_inet_conn_established),
7129 LSM_HOOK_INIT(secmark_relabel_packet, selinux_secmark_relabel_packet),
7130 LSM_HOOK_INIT(secmark_refcount_inc, selinux_secmark_refcount_inc),
7131 LSM_HOOK_INIT(secmark_refcount_dec, selinux_secmark_refcount_dec),
7132 LSM_HOOK_INIT(req_classify_flow, selinux_req_classify_flow),
Casey Schauflere20b0432015-05-02 15:11:36 -07007133 LSM_HOOK_INIT(tun_dev_free_security, selinux_tun_dev_free_security),
7134 LSM_HOOK_INIT(tun_dev_create, selinux_tun_dev_create),
7135 LSM_HOOK_INIT(tun_dev_attach_queue, selinux_tun_dev_attach_queue),
7136 LSM_HOOK_INIT(tun_dev_attach, selinux_tun_dev_attach),
7137 LSM_HOOK_INIT(tun_dev_open, selinux_tun_dev_open),
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03007138#ifdef CONFIG_SECURITY_INFINIBAND
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03007139 LSM_HOOK_INIT(ib_pkey_access, selinux_ib_pkey_access),
Daniel Jurgensab861df2017-05-19 15:48:58 +03007140 LSM_HOOK_INIT(ib_endport_manage_subnet,
7141 selinux_ib_endport_manage_subnet),
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03007142 LSM_HOOK_INIT(ib_free_security, selinux_ib_free_security),
7143#endif
Trent Jaegerd28d1e02005-12-13 23:12:40 -08007144#ifdef CONFIG_SECURITY_NETWORK_XFRM
Casey Schauflere20b0432015-05-02 15:11:36 -07007145 LSM_HOOK_INIT(xfrm_policy_free_security, selinux_xfrm_policy_free),
7146 LSM_HOOK_INIT(xfrm_policy_delete_security, selinux_xfrm_policy_delete),
Casey Schauflere20b0432015-05-02 15:11:36 -07007147 LSM_HOOK_INIT(xfrm_state_free_security, selinux_xfrm_state_free),
7148 LSM_HOOK_INIT(xfrm_state_delete_security, selinux_xfrm_state_delete),
7149 LSM_HOOK_INIT(xfrm_policy_lookup, selinux_xfrm_policy_lookup),
7150 LSM_HOOK_INIT(xfrm_state_pol_flow_match,
7151 selinux_xfrm_state_pol_flow_match),
7152 LSM_HOOK_INIT(xfrm_decode_session, selinux_xfrm_decode_session),
Linus Torvalds1da177e2005-04-16 15:20:36 -07007153#endif
Michael LeMayd7200242006-06-22 14:47:17 -07007154
7155#ifdef CONFIG_KEYS
Casey Schauflere20b0432015-05-02 15:11:36 -07007156 LSM_HOOK_INIT(key_free, selinux_key_free),
7157 LSM_HOOK_INIT(key_permission, selinux_key_permission),
7158 LSM_HOOK_INIT(key_getsecurity, selinux_key_getsecurity),
David Howells3e412cc2020-01-14 17:07:13 +00007159#ifdef CONFIG_KEY_NOTIFICATIONS
7160 LSM_HOOK_INIT(watch_key, selinux_watch_key),
7161#endif
Michael LeMayd7200242006-06-22 14:47:17 -07007162#endif
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02007163
7164#ifdef CONFIG_AUDIT
Casey Schauflere20b0432015-05-02 15:11:36 -07007165 LSM_HOOK_INIT(audit_rule_known, selinux_audit_rule_known),
7166 LSM_HOOK_INIT(audit_rule_match, selinux_audit_rule_match),
7167 LSM_HOOK_INIT(audit_rule_free, selinux_audit_rule_free),
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02007168#endif
Chenbo Fengec27c352017-10-18 13:00:25 -07007169
7170#ifdef CONFIG_BPF_SYSCALL
7171 LSM_HOOK_INIT(bpf, selinux_bpf),
7172 LSM_HOOK_INIT(bpf_map, selinux_bpf_map),
7173 LSM_HOOK_INIT(bpf_prog, selinux_bpf_prog),
Chenbo Fengec27c352017-10-18 13:00:25 -07007174 LSM_HOOK_INIT(bpf_map_free_security, selinux_bpf_map_free),
7175 LSM_HOOK_INIT(bpf_prog_free_security, selinux_bpf_prog_free),
7176#endif
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -04007177
7178#ifdef CONFIG_PERF_EVENTS
7179 LSM_HOOK_INIT(perf_event_open, selinux_perf_event_open),
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -04007180 LSM_HOOK_INIT(perf_event_free, selinux_perf_event_free),
7181 LSM_HOOK_INIT(perf_event_read, selinux_perf_event_read),
7182 LSM_HOOK_INIT(perf_event_write, selinux_perf_event_write),
7183#endif
Stephen Smalley59438b462019-11-27 12:04:36 -05007184
7185 LSM_HOOK_INIT(locked_down, selinux_lockdown),
Ondrej Mosnacekcfff75d2020-01-08 15:09:58 +01007186
7187 /*
7188 * PUT "CLONING" (ACCESSING + ALLOCATING) HOOKS HERE
7189 */
7190 LSM_HOOK_INIT(fs_context_dup, selinux_fs_context_dup),
7191 LSM_HOOK_INIT(fs_context_parse_param, selinux_fs_context_parse_param),
7192 LSM_HOOK_INIT(sb_eat_lsm_opts, selinux_sb_eat_lsm_opts),
7193 LSM_HOOK_INIT(sb_add_mnt_opt, selinux_add_mnt_opt),
7194#ifdef CONFIG_SECURITY_NETWORK_XFRM
7195 LSM_HOOK_INIT(xfrm_policy_clone_security, selinux_xfrm_policy_clone),
7196#endif
7197
7198 /*
7199 * PUT "ALLOCATING" HOOKS HERE
7200 */
7201 LSM_HOOK_INIT(msg_msg_alloc_security, selinux_msg_msg_alloc_security),
7202 LSM_HOOK_INIT(msg_queue_alloc_security,
7203 selinux_msg_queue_alloc_security),
7204 LSM_HOOK_INIT(shm_alloc_security, selinux_shm_alloc_security),
7205 LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security),
7206 LSM_HOOK_INIT(inode_alloc_security, selinux_inode_alloc_security),
7207 LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security),
7208 LSM_HOOK_INIT(secid_to_secctx, selinux_secid_to_secctx),
7209 LSM_HOOK_INIT(inode_getsecctx, selinux_inode_getsecctx),
7210 LSM_HOOK_INIT(sk_alloc_security, selinux_sk_alloc_security),
7211 LSM_HOOK_INIT(tun_dev_alloc_security, selinux_tun_dev_alloc_security),
7212#ifdef CONFIG_SECURITY_INFINIBAND
7213 LSM_HOOK_INIT(ib_alloc_security, selinux_ib_alloc_security),
7214#endif
7215#ifdef CONFIG_SECURITY_NETWORK_XFRM
7216 LSM_HOOK_INIT(xfrm_policy_alloc_security, selinux_xfrm_policy_alloc),
7217 LSM_HOOK_INIT(xfrm_state_alloc, selinux_xfrm_state_alloc),
7218 LSM_HOOK_INIT(xfrm_state_alloc_acquire,
7219 selinux_xfrm_state_alloc_acquire),
7220#endif
7221#ifdef CONFIG_KEYS
7222 LSM_HOOK_INIT(key_alloc, selinux_key_alloc),
7223#endif
7224#ifdef CONFIG_AUDIT
7225 LSM_HOOK_INIT(audit_rule_init, selinux_audit_rule_init),
7226#endif
7227#ifdef CONFIG_BPF_SYSCALL
7228 LSM_HOOK_INIT(bpf_map_alloc_security, selinux_bpf_map_alloc),
7229 LSM_HOOK_INIT(bpf_prog_alloc_security, selinux_bpf_prog_alloc),
7230#endif
7231#ifdef CONFIG_PERF_EVENTS
7232 LSM_HOOK_INIT(perf_event_alloc, selinux_perf_event_alloc),
7233#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007234};
7235
7236static __init int selinux_init(void)
7237{
peter enderborgc103a912018-06-12 10:09:03 +02007238 pr_info("SELinux: Initializing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007239
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007240 memset(&selinux_state, 0, sizeof(selinux_state));
Paul Mooree5a5ca92018-03-01 17:38:30 -05007241 enforcing_set(&selinux_state, selinux_enforcing_boot);
Lakshmi Ramasubramanian8861d0a2020-09-14 10:31:57 -07007242 checkreqprot_set(&selinux_state, selinux_checkreqprot_boot);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05007243 selinux_avc_init(&selinux_state.avc);
Ondrej Mosnacek4b36cb72020-01-17 14:15:14 +01007244 mutex_init(&selinux_state.status_lock);
Stephen Smalley9ff9abc2020-08-26 13:28:53 -04007245 mutex_init(&selinux_state.policy_mutex);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007246
Linus Torvalds1da177e2005-04-16 15:20:36 -07007247 /* Set the security state for the initial task. */
David Howellsd84f4f92008-11-14 10:39:23 +11007248 cred_init_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007249
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04007250 default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
7251
Linus Torvalds1da177e2005-04-16 15:20:36 -07007252 avc_init();
7253
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007254 avtab_cache_init();
7255
7256 ebitmap_cache_init();
7257
7258 hashtab_cache_init();
7259
Casey Schauflerd69dece52017-01-18 17:09:05 -08007260 security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks), "selinux");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007261
Paul Moore615e51f2014-06-26 14:33:56 -04007262 if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET))
7263 panic("SELinux: Unable to register AVC netcache callback\n");
7264
Daniel Jurgens8f408ab2017-05-19 15:48:53 +03007265 if (avc_add_callback(selinux_lsm_notifier_avc_callback, AVC_CALLBACK_RESET))
7266 panic("SELinux: Unable to register AVC LSM notifier callback\n");
7267
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007268 if (selinux_enforcing_boot)
peter enderborgc103a912018-06-12 10:09:03 +02007269 pr_debug("SELinux: Starting in enforcing mode\n");
Eric Paris828dfe12008-04-17 13:17:49 -04007270 else
peter enderborgc103a912018-06-12 10:09:03 +02007271 pr_debug("SELinux: Starting in permissive mode\n");
Michael LeMayd7200242006-06-22 14:47:17 -07007272
Al Virod7167b12019-09-07 07:23:15 -04007273 fs_validate_description("selinux", selinux_fs_parameters);
David Howells442155c2018-11-01 23:07:24 +00007274
Linus Torvalds1da177e2005-04-16 15:20:36 -07007275 return 0;
7276}
7277
Al Viroe8c26252010-03-23 06:36:54 -04007278static void delayed_superblock_init(struct super_block *sb, void *unused)
7279{
Al Viro204cc0c2018-12-13 13:41:47 -05007280 selinux_set_mnt_opts(sb, NULL, 0, NULL);
Al Viroe8c26252010-03-23 06:36:54 -04007281}
7282
Linus Torvalds1da177e2005-04-16 15:20:36 -07007283void selinux_complete_init(void)
7284{
peter enderborgc103a912018-06-12 10:09:03 +02007285 pr_debug("SELinux: Completing initialization.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007286
7287 /* Set up any superblocks initialized prior to the policy load. */
peter enderborgc103a912018-06-12 10:09:03 +02007288 pr_debug("SELinux: Setting up existing superblocks.\n");
Al Viroe8c26252010-03-23 06:36:54 -04007289 iterate_supers(delayed_superblock_init, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007290}
7291
7292/* SELinux requires early initialization in order to label
7293 all processes and objects when they are created. */
Kees Cook3d6e5f62018-10-10 17:18:23 -07007294DEFINE_LSM(selinux) = {
Kees Cook07aed2f2018-10-10 17:18:24 -07007295 .name = "selinux",
Kees Cook14bd99c2018-09-19 19:57:06 -07007296 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
Stephen Smalley6c5a6822019-12-17 09:15:10 -05007297 .enabled = &selinux_enabled_boot,
Casey Schauflerbbd36622018-11-12 09:30:56 -08007298 .blobs = &selinux_blob_sizes,
Kees Cook3d6e5f62018-10-10 17:18:23 -07007299 .init = selinux_init,
7300};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007301
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007302#if defined(CONFIG_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007303
Florian Westphal591bb272017-07-26 11:40:52 +02007304static const struct nf_hook_ops selinux_nf_ops[] = {
Paul Mooreeffad8d2008-01-29 08:49:27 -05007305 {
7306 .hook = selinux_ipv4_postroute,
Alban Crequy2597a832012-05-14 03:56:39 +00007307 .pf = NFPROTO_IPV4,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007308 .hooknum = NF_INET_POST_ROUTING,
7309 .priority = NF_IP_PRI_SELINUX_LAST,
7310 },
7311 {
7312 .hook = selinux_ipv4_forward,
Alban Crequy2597a832012-05-14 03:56:39 +00007313 .pf = NFPROTO_IPV4,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007314 .hooknum = NF_INET_FORWARD,
7315 .priority = NF_IP_PRI_SELINUX_FIRST,
Paul Moore948bf852008-10-10 10:16:32 -04007316 },
7317 {
7318 .hook = selinux_ipv4_output,
Alban Crequy2597a832012-05-14 03:56:39 +00007319 .pf = NFPROTO_IPV4,
Paul Moore948bf852008-10-10 10:16:32 -04007320 .hooknum = NF_INET_LOCAL_OUT,
7321 .priority = NF_IP_PRI_SELINUX_FIRST,
Jiri Pirko25db6be2014-09-03 17:42:13 +02007322 },
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04007323#if IS_ENABLED(CONFIG_IPV6)
Paul Mooreeffad8d2008-01-29 08:49:27 -05007324 {
7325 .hook = selinux_ipv6_postroute,
Alban Crequy2597a832012-05-14 03:56:39 +00007326 .pf = NFPROTO_IPV6,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007327 .hooknum = NF_INET_POST_ROUTING,
7328 .priority = NF_IP6_PRI_SELINUX_LAST,
7329 },
7330 {
7331 .hook = selinux_ipv6_forward,
Alban Crequy2597a832012-05-14 03:56:39 +00007332 .pf = NFPROTO_IPV6,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007333 .hooknum = NF_INET_FORWARD,
7334 .priority = NF_IP6_PRI_SELINUX_FIRST,
Jiri Pirko25db6be2014-09-03 17:42:13 +02007335 },
Huw Davies2917f572016-06-27 15:06:15 -04007336 {
7337 .hook = selinux_ipv6_output,
7338 .pf = NFPROTO_IPV6,
7339 .hooknum = NF_INET_LOCAL_OUT,
7340 .priority = NF_IP6_PRI_SELINUX_FIRST,
7341 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07007342#endif /* IPV6 */
Jiri Pirko25db6be2014-09-03 17:42:13 +02007343};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007344
Florian Westphal8e71bf72017-04-21 11:49:09 +02007345static int __net_init selinux_nf_register(struct net *net)
7346{
7347 return nf_register_net_hooks(net, selinux_nf_ops,
7348 ARRAY_SIZE(selinux_nf_ops));
7349}
7350
7351static void __net_exit selinux_nf_unregister(struct net *net)
7352{
7353 nf_unregister_net_hooks(net, selinux_nf_ops,
7354 ARRAY_SIZE(selinux_nf_ops));
7355}
7356
7357static struct pernet_operations selinux_net_ops = {
7358 .init = selinux_nf_register,
7359 .exit = selinux_nf_unregister,
7360};
7361
Linus Torvalds1da177e2005-04-16 15:20:36 -07007362static int __init selinux_nf_ip_init(void)
7363{
Jiri Pirko25db6be2014-09-03 17:42:13 +02007364 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007365
Stephen Smalley6c5a6822019-12-17 09:15:10 -05007366 if (!selinux_enabled_boot)
Jiri Pirko25db6be2014-09-03 17:42:13 +02007367 return 0;
Eric Parisfadcdb42007-02-22 18:11:31 -05007368
peter enderborgc103a912018-06-12 10:09:03 +02007369 pr_debug("SELinux: Registering netfilter hooks\n");
Eric Parisfadcdb42007-02-22 18:11:31 -05007370
Florian Westphal8e71bf72017-04-21 11:49:09 +02007371 err = register_pernet_subsys(&selinux_net_ops);
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07007372 if (err)
Florian Westphal8e71bf72017-04-21 11:49:09 +02007373 panic("SELinux: register_pernet_subsys: error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007374
Jiri Pirko25db6be2014-09-03 17:42:13 +02007375 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007376}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007377__initcall(selinux_nf_ip_init);
7378
7379#ifdef CONFIG_SECURITY_SELINUX_DISABLE
7380static void selinux_nf_ip_exit(void)
7381{
peter enderborgc103a912018-06-12 10:09:03 +02007382 pr_debug("SELinux: Unregistering netfilter hooks\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007383
Florian Westphal8e71bf72017-04-21 11:49:09 +02007384 unregister_pernet_subsys(&selinux_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007385}
7386#endif
7387
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007388#else /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007389
7390#ifdef CONFIG_SECURITY_SELINUX_DISABLE
7391#define selinux_nf_ip_exit()
7392#endif
7393
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007394#endif /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007395
7396#ifdef CONFIG_SECURITY_SELINUX_DISABLE
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007397int selinux_disable(struct selinux_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007398{
Ondrej Mosnacek65cddd52020-01-07 14:31:53 +01007399 if (selinux_initialized(state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007400 /* Not permitted after initial policy load. */
7401 return -EINVAL;
7402 }
7403
Ondrej Mosnacek65cddd52020-01-07 14:31:53 +01007404 if (selinux_disabled(state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007405 /* Only do this once. */
7406 return -EINVAL;
7407 }
7408
Ondrej Mosnacek65cddd52020-01-07 14:31:53 +01007409 selinux_mark_disabled(state);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007410
peter enderborgc103a912018-06-12 10:09:03 +02007411 pr_info("SELinux: Disabled at runtime.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007412
Ondrej Mosnacekcfff75d2020-01-08 15:09:58 +01007413 /*
7414 * Unregister netfilter hooks.
7415 * Must be done before security_delete_hooks() to avoid breaking
7416 * runtime disable.
7417 */
7418 selinux_nf_ip_exit();
7419
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07007420 security_delete_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007421
Eric Parisaf8ff042009-09-20 21:23:01 -04007422 /* Try to destroy the avc node cache */
7423 avc_disable();
7424
Linus Torvalds1da177e2005-04-16 15:20:36 -07007425 /* Unregister selinuxfs. */
7426 exit_sel_fs();
7427
7428 return 0;
7429}
7430#endif