blob: dde4ecc0cd186ca584ec87f7063aeff170f5ccee [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/*
Paul Mooreeb1231f2021-02-18 15:13:40 -0500233 * get the subjective security ID of a task
234 */
235static inline u32 task_sid_subj(const struct task_struct *task)
236{
237 u32 sid;
238
239 rcu_read_lock();
240 sid = cred_sid(rcu_dereference(task->cred));
241 rcu_read_unlock();
242 return sid;
243}
244
245/*
David Howells3b11a1d2008-11-14 10:39:26 +1100246 * get the objective security ID of a task
David Howells275bb412008-11-14 10:39:19 +1100247 */
Paul Mooreeb1231f2021-02-18 15:13:40 -0500248static inline u32 task_sid_obj(const struct task_struct *task)
David Howells275bb412008-11-14 10:39:19 +1100249{
David Howells275bb412008-11-14 10:39:19 +1100250 u32 sid;
251
252 rcu_read_lock();
David Howells88e67f32008-11-14 10:39:21 +1100253 sid = cred_sid(__task_cred(task));
David Howells275bb412008-11-14 10:39:19 +1100254 rcu_read_unlock();
255 return sid;
256}
257
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500258static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry);
259
260/*
261 * Try reloading inode security labels that have been marked as invalid. The
262 * @may_sleep parameter indicates when sleeping and thus reloading labels is
Andreas Gruenbacher42059112016-11-10 22:18:27 +0100263 * allowed; when set to false, returns -ECHILD when the label is
Al Viroe9193282018-04-24 21:31:02 -0400264 * invalid. The @dentry parameter should be set to a dentry of the inode.
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500265 */
266static int __inode_security_revalidate(struct inode *inode,
Al Viroe9193282018-04-24 21:31:02 -0400267 struct dentry *dentry,
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500268 bool may_sleep)
269{
Casey Schaufler80788c22018-09-21 17:19:11 -0700270 struct inode_security_struct *isec = selinux_inode(inode);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500271
272 might_sleep_if(may_sleep);
273
Ondrej Mosnacek65cddd52020-01-07 14:31:53 +0100274 if (selinux_initialized(&selinux_state) &&
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500275 isec->initialized != LABEL_INITIALIZED) {
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500276 if (!may_sleep)
277 return -ECHILD;
278
279 /*
280 * Try reloading the inode security label. This will fail if
281 * @opt_dentry is NULL and no dentry for this inode can be
282 * found; in that case, continue using the old label.
283 */
Al Viroe9193282018-04-24 21:31:02 -0400284 inode_doinit_with_dentry(inode, dentry);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500285 }
286 return 0;
287}
288
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500289static struct inode_security_struct *inode_security_novalidate(struct inode *inode)
290{
Casey Schaufler80788c22018-09-21 17:19:11 -0700291 return selinux_inode(inode);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500292}
293
294static struct inode_security_struct *inode_security_rcu(struct inode *inode, bool rcu)
295{
296 int error;
297
298 error = __inode_security_revalidate(inode, NULL, !rcu);
299 if (error)
300 return ERR_PTR(error);
Casey Schaufler80788c22018-09-21 17:19:11 -0700301 return selinux_inode(inode);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500302}
303
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500304/*
305 * Get the security label of an inode.
306 */
307static struct inode_security_struct *inode_security(struct inode *inode)
308{
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500309 __inode_security_revalidate(inode, NULL, true);
Casey Schaufler80788c22018-09-21 17:19:11 -0700310 return selinux_inode(inode);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500311}
312
Paul Moore2c971652016-04-19 16:36:28 -0400313static struct inode_security_struct *backing_inode_security_novalidate(struct dentry *dentry)
314{
315 struct inode *inode = d_backing_inode(dentry);
316
Casey Schaufler80788c22018-09-21 17:19:11 -0700317 return selinux_inode(inode);
Paul Moore2c971652016-04-19 16:36:28 -0400318}
319
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500320/*
321 * Get the security label of a dentry's backing inode.
322 */
323static struct inode_security_struct *backing_inode_security(struct dentry *dentry)
324{
325 struct inode *inode = d_backing_inode(dentry);
326
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500327 __inode_security_revalidate(inode, dentry, true);
Casey Schaufler80788c22018-09-21 17:19:11 -0700328 return selinux_inode(inode);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500329}
330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331static void inode_free_security(struct inode *inode)
332{
Casey Schaufler80788c22018-09-21 17:19:11 -0700333 struct inode_security_struct *isec = selinux_inode(inode);
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700334 struct superblock_security_struct *sbsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700336 if (!isec)
337 return;
Casey Schaufler1aea7802021-04-22 17:41:15 +0200338 sbsec = selinux_superblock(inode->i_sb);
Waiman Long9629d042015-07-10 17:19:56 -0400339 /*
340 * As not all inode security structures are in a list, we check for
341 * empty list outside of the lock to make sure that we won't waste
342 * time taking a lock doing nothing.
343 *
344 * The list_del_init() function can be safely called more than once.
345 * It should not be possible for this function to be called with
346 * concurrent list_add(), but for better safety against future changes
347 * in the code, we use list_empty_careful() here.
348 */
349 if (!list_empty_careful(&isec->list)) {
350 spin_lock(&sbsec->isec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 list_del_init(&isec->list);
Waiman Long9629d042015-07-10 17:19:56 -0400352 spin_unlock(&sbsec->isec_lock);
353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
355
Al Virobd323652018-12-13 15:04:59 -0500356struct selinux_mnt_opts {
357 const char *fscontext, *context, *rootcontext, *defcontext;
358};
359
Al Viro204cc0c2018-12-13 13:41:47 -0500360static void selinux_free_mnt_opts(void *mnt_opts)
361{
Al Virobd323652018-12-13 15:04:59 -0500362 struct selinux_mnt_opts *opts = mnt_opts;
363 kfree(opts->fscontext);
364 kfree(opts->context);
365 kfree(opts->rootcontext);
366 kfree(opts->defcontext);
Al Viro204cc0c2018-12-13 13:41:47 -0500367 kfree(opts);
368}
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370enum {
Eric Paris31e87932007-09-19 17:19:12 -0400371 Opt_error = -1,
David Howells442155c2018-11-01 23:07:24 +0000372 Opt_context = 0,
373 Opt_defcontext = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 Opt_fscontext = 2,
David Howells442155c2018-11-01 23:07:24 +0000375 Opt_rootcontext = 3,
376 Opt_seclabel = 4,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377};
378
Al Viroda3d76a2018-12-17 10:14:16 -0500379#define A(s, has_arg) {#s, sizeof(#s) - 1, Opt_##s, has_arg}
Al Viro169d68efb2018-12-14 22:44:50 -0500380static struct {
381 const char *name;
382 int len;
383 int opt;
384 bool has_arg;
385} tokens[] = {
Al Viroda3d76a2018-12-17 10:14:16 -0500386 A(context, true),
387 A(fscontext, true),
388 A(defcontext, true),
389 A(rootcontext, true),
390 A(seclabel, false),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391};
Al Viro169d68efb2018-12-14 22:44:50 -0500392#undef A
393
394static int match_opt_prefix(char *s, int l, char **arg)
395{
396 int i;
397
398 for (i = 0; i < ARRAY_SIZE(tokens); i++) {
399 size_t len = tokens[i].len;
400 if (len > l || memcmp(s, tokens[i].name, len))
401 continue;
402 if (tokens[i].has_arg) {
403 if (len == l || s[len] != '=')
404 continue;
405 *arg = s + len + 1;
406 } else if (len != l)
407 continue;
408 return tokens[i].opt;
409 }
410 return Opt_error;
411}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413#define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n"
414
Eric Parisc312feb2006-07-10 04:43:53 -0700415static int may_context_mount_sb_relabel(u32 sid,
416 struct superblock_security_struct *sbsec,
David Howells275bb412008-11-14 10:39:19 +1100417 const struct cred *cred)
Eric Parisc312feb2006-07-10 04:43:53 -0700418{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -0700419 const struct task_security_struct *tsec = selinux_cred(cred);
Eric Parisc312feb2006-07-10 04:43:53 -0700420 int rc;
421
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500422 rc = avc_has_perm(&selinux_state,
423 tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
Eric Parisc312feb2006-07-10 04:43:53 -0700424 FILESYSTEM__RELABELFROM, NULL);
425 if (rc)
426 return rc;
427
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500428 rc = avc_has_perm(&selinux_state,
429 tsec->sid, sid, SECCLASS_FILESYSTEM,
Eric Parisc312feb2006-07-10 04:43:53 -0700430 FILESYSTEM__RELABELTO, NULL);
431 return rc;
432}
433
Eric Paris08089252006-07-10 04:43:55 -0700434static int may_context_mount_inode_relabel(u32 sid,
435 struct superblock_security_struct *sbsec,
David Howells275bb412008-11-14 10:39:19 +1100436 const struct cred *cred)
Eric Paris08089252006-07-10 04:43:55 -0700437{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -0700438 const struct task_security_struct *tsec = selinux_cred(cred);
Eric Paris08089252006-07-10 04:43:55 -0700439 int rc;
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500440 rc = avc_has_perm(&selinux_state,
441 tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
Eric Paris08089252006-07-10 04:43:55 -0700442 FILESYSTEM__RELABELFROM, NULL);
443 if (rc)
444 return rc;
445
Stephen Smalley6b6bc622018-03-05 11:47:56 -0500446 rc = avc_has_perm(&selinux_state,
447 sid, sbsec->sid, SECCLASS_FILESYSTEM,
Eric Paris08089252006-07-10 04:43:55 -0700448 FILESYSTEM__ASSOCIATE, NULL);
449 return rc;
450}
451
Ondrej Mosnaceka83d6dd2018-12-21 21:18:52 +0100452static int selinux_is_genfs_special_handling(struct super_block *sb)
Eric Parisb43e7252012-10-10 14:27:35 -0400453{
Ondrej Mosnaceka83d6dd2018-12-21 21:18:52 +0100454 /* Special handling. Genfs but also in-core setxattr handler */
455 return !strcmp(sb->s_type->name, "sysfs") ||
Mark Salyzynd5f3a5f2015-02-04 11:34:30 -0500456 !strcmp(sb->s_type->name, "pstore") ||
457 !strcmp(sb->s_type->name, "debugfs") ||
Yongqin Liua2c7c6f2017-01-09 10:07:30 -0500458 !strcmp(sb->s_type->name, "tracefs") ||
Stephen Smalley2651225b2017-02-28 10:35:56 -0500459 !strcmp(sb->s_type->name, "rootfs") ||
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500460 (selinux_policycap_cgroupseclabel() &&
Stephen Smalley2651225b2017-02-28 10:35:56 -0500461 (!strcmp(sb->s_type->name, "cgroup") ||
462 !strcmp(sb->s_type->name, "cgroup2")));
Eric Parisb43e7252012-10-10 14:27:35 -0400463}
464
Ondrej Mosnaceka83d6dd2018-12-21 21:18:52 +0100465static int selinux_is_sblabel_mnt(struct super_block *sb)
466{
Casey Schaufler1aea7802021-04-22 17:41:15 +0200467 struct superblock_security_struct *sbsec = selinux_superblock(sb);
Ondrej Mosnaceka83d6dd2018-12-21 21:18:52 +0100468
469 /*
470 * IMPORTANT: Double-check logic in this function when adding a new
471 * SECURITY_FS_USE_* definition!
472 */
473 BUILD_BUG_ON(SECURITY_FS_USE_MAX != 7);
474
475 switch (sbsec->behavior) {
476 case SECURITY_FS_USE_XATTR:
477 case SECURITY_FS_USE_TRANS:
478 case SECURITY_FS_USE_TASK:
479 case SECURITY_FS_USE_NATIVE:
480 return 1;
481
482 case SECURITY_FS_USE_GENFS:
483 return selinux_is_genfs_special_handling(sb);
484
485 /* Never allow relabeling on context mounts */
486 case SECURITY_FS_USE_MNTPOINT:
487 case SECURITY_FS_USE_NONE:
488 default:
489 return 0;
490 }
491}
492
Ondrej Mosnacek08abe462021-01-13 13:38:02 +0100493static int sb_check_xattr_support(struct super_block *sb)
494{
495 struct superblock_security_struct *sbsec = sb->s_security;
496 struct dentry *root = sb->s_root;
497 struct inode *root_inode = d_backing_inode(root);
498 u32 sid;
499 int rc;
500
501 /*
502 * Make sure that the xattr handler exists and that no
503 * error other than -ENODATA is returned by getxattr on
504 * the root directory. -ENODATA is ok, as this may be
505 * the first boot of the SELinux kernel before we have
506 * assigned xattr values to the filesystem.
507 */
508 if (!(root_inode->i_opflags & IOP_XATTR)) {
509 pr_warn("SELinux: (dev %s, type %s) has no xattr support\n",
510 sb->s_id, sb->s_type->name);
511 goto fallback;
512 }
513
514 rc = __vfs_getxattr(root, root_inode, XATTR_NAME_SELINUX, NULL, 0);
515 if (rc < 0 && rc != -ENODATA) {
516 if (rc == -EOPNOTSUPP) {
517 pr_warn("SELinux: (dev %s, type %s) has no security xattr handler\n",
518 sb->s_id, sb->s_type->name);
519 goto fallback;
520 } else {
521 pr_warn("SELinux: (dev %s, type %s) getxattr errno %d\n",
522 sb->s_id, sb->s_type->name, -rc);
523 return rc;
524 }
525 }
526 return 0;
527
528fallback:
529 /* No xattr support - try to fallback to genfs if possible. */
530 rc = security_genfs_sid(&selinux_state, sb->s_type->name, "/",
531 SECCLASS_DIR, &sid);
532 if (rc)
533 return -EOPNOTSUPP;
534
535 pr_warn("SELinux: (dev %s, type %s) falling back to genfs\n",
536 sb->s_id, sb->s_type->name);
537 sbsec->behavior = SECURITY_FS_USE_GENFS;
538 sbsec->sid = sid;
539 return 0;
540}
541
Eric Parisc9180a52007-11-30 13:00:35 -0500542static int sb_finish_set_opts(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
Casey Schaufler1aea7802021-04-22 17:41:15 +0200544 struct superblock_security_struct *sbsec = selinux_superblock(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 struct dentry *root = sb->s_root;
David Howellsc6f493d2015-03-17 22:26:22 +0000546 struct inode *root_inode = d_backing_inode(root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 int rc = 0;
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
Ondrej Mosnacek08abe462021-01-13 13:38:02 +0100550 rc = sb_check_xattr_support(sb);
551 if (rc)
552 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
554
Eric Pariseadcabc2012-08-24 15:59:14 -0400555 sbsec->flags |= SE_SBINITIALIZED;
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400556
557 /*
558 * Explicitly set or clear SBLABEL_MNT. It's not sufficient to simply
559 * leave the flag untouched because sb_clone_mnt_opts might be handing
560 * us a superblock that needs the flag to be cleared.
561 */
Eric Parisb43e7252012-10-10 14:27:35 -0400562 if (selinux_is_sblabel_mnt(sb))
Eric Paris12f348b2012-10-09 10:56:25 -0400563 sbsec->flags |= SBLABEL_MNT;
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400564 else
565 sbsec->flags &= ~SBLABEL_MNT;
David P. Quigleyddd29ec2009-09-09 14:25:37 -0400566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 /* Initialize the root inode. */
Eric Parisc9180a52007-11-30 13:00:35 -0500568 rc = inode_doinit_with_dentry(root_inode, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 /* Initialize any other inodes associated with the superblock, e.g.
571 inodes created prior to initial policy load or inodes created
572 during get_sb by a pseudo filesystem that directly
573 populates itself. */
574 spin_lock(&sbsec->isec_lock);
Al Viro8d641242018-12-10 15:34:12 -0500575 while (!list_empty(&sbsec->isec_head)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 struct inode_security_struct *isec =
Al Viro8d641242018-12-10 15:34:12 -0500577 list_first_entry(&sbsec->isec_head,
Eric Parisc9180a52007-11-30 13:00:35 -0500578 struct inode_security_struct, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 struct inode *inode = isec->inode;
Stephen Smalley923190d2014-10-06 16:32:52 -0400580 list_del_init(&isec->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 spin_unlock(&sbsec->isec_lock);
582 inode = igrab(inode);
583 if (inode) {
Eric Parisc9180a52007-11-30 13:00:35 -0500584 if (!IS_PRIVATE(inode))
Paul Moorecb89e242020-01-10 16:32:10 -0500585 inode_doinit_with_dentry(inode, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 iput(inode);
587 }
588 spin_lock(&sbsec->isec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 }
590 spin_unlock(&sbsec->isec_lock);
Eric Parisc9180a52007-11-30 13:00:35 -0500591 return rc;
592}
593
Eric Parisc9180a52007-11-30 13:00:35 -0500594static int bad_option(struct superblock_security_struct *sbsec, char flag,
595 u32 old_sid, u32 new_sid)
596{
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500597 char mnt_flags = sbsec->flags & SE_MNTMASK;
598
Eric Parisc9180a52007-11-30 13:00:35 -0500599 /* check if the old mount command had the same options */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500600 if (sbsec->flags & SE_SBINITIALIZED)
Eric Parisc9180a52007-11-30 13:00:35 -0500601 if (!(sbsec->flags & flag) ||
602 (old_sid != new_sid))
603 return 1;
604
605 /* check if we were passed the same options twice,
606 * aka someone passed context=a,context=b
607 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500608 if (!(sbsec->flags & SE_SBINITIALIZED))
609 if (mnt_flags & flag)
Eric Parisc9180a52007-11-30 13:00:35 -0500610 return 1;
611 return 0;
612}
Eric Parise0007522008-03-05 10:31:54 -0500613
Scott Mayhewcc274ae2021-12-15 16:28:40 -0500614static int parse_sid(struct super_block *sb, const char *s, u32 *sid,
615 gfp_t gfp)
Al Virobd323652018-12-13 15:04:59 -0500616{
617 int rc = security_context_str_to_sid(&selinux_state, s,
Scott Mayhewcc274ae2021-12-15 16:28:40 -0500618 sid, gfp);
Al Virobd323652018-12-13 15:04:59 -0500619 if (rc)
620 pr_warn("SELinux: security_context_str_to_sid"
621 "(%s) failed for (dev %s, type %s) errno=%d\n",
622 s, sb->s_id, sb->s_type->name, rc);
623 return rc;
624}
625
Eric Parisc9180a52007-11-30 13:00:35 -0500626/*
627 * Allow filesystems with binary mount data to explicitly set mount point
628 * labeling information.
629 */
Eric Parise0007522008-03-05 10:31:54 -0500630static int selinux_set_mnt_opts(struct super_block *sb,
Al Viro204cc0c2018-12-13 13:41:47 -0500631 void *mnt_opts,
David Quigley649f6e72013-05-22 12:50:36 -0400632 unsigned long kern_flags,
633 unsigned long *set_kern_flags)
Eric Parisc9180a52007-11-30 13:00:35 -0500634{
David Howells275bb412008-11-14 10:39:19 +1100635 const struct cred *cred = current_cred();
Casey Schaufler1aea7802021-04-22 17:41:15 +0200636 struct superblock_security_struct *sbsec = selinux_superblock(sb);
Ondrej Mosnacekb159e862020-11-04 13:01:10 +0100637 struct dentry *root = sb->s_root;
Al Virobd323652018-12-13 15:04:59 -0500638 struct selinux_mnt_opts *opts = mnt_opts;
Paul Moore2c971652016-04-19 16:36:28 -0400639 struct inode_security_struct *root_isec;
Eric Parisc9180a52007-11-30 13:00:35 -0500640 u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
641 u32 defcontext_sid = 0;
Al Virobd323652018-12-13 15:04:59 -0500642 int rc = 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500643
644 mutex_lock(&sbsec->lock);
645
Ondrej Mosnacek65cddd52020-01-07 14:31:53 +0100646 if (!selinux_initialized(&selinux_state)) {
Al Virobd323652018-12-13 15:04:59 -0500647 if (!opts) {
Eric Parisc9180a52007-11-30 13:00:35 -0500648 /* Defer initialization until selinux_complete_init,
649 after the initial policy is loaded and the security
650 server is ready to handle calls. */
Eric Parisc9180a52007-11-30 13:00:35 -0500651 goto out;
652 }
653 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +0200654 pr_warn("SELinux: Unable to set superblock options "
Eric Paris744ba352008-04-17 11:52:44 -0400655 "before the security server is initialized\n");
Eric Parisc9180a52007-11-30 13:00:35 -0500656 goto out;
657 }
David Quigley649f6e72013-05-22 12:50:36 -0400658 if (kern_flags && !set_kern_flags) {
659 /* Specifying internal flags without providing a place to
660 * place the results is not allowed */
661 rc = -EINVAL;
662 goto out;
663 }
Eric Parisc9180a52007-11-30 13:00:35 -0500664
665 /*
Eric Parise0007522008-03-05 10:31:54 -0500666 * Binary mount data FS will come through this function twice. Once
667 * from an explicit call and once from the generic calls from the vfs.
668 * Since the generic VFS calls will not contain any security mount data
669 * we need to skip the double mount verification.
670 *
671 * This does open a hole in which we will not notice if the first
672 * mount using this sb set explict options and a second mount using
673 * this sb does not set any security options. (The first options
674 * will be used for both mounts)
675 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500676 if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
Al Virobd323652018-12-13 15:04:59 -0500677 && !opts)
Eric Parisf5269712008-05-14 11:27:45 -0400678 goto out;
Eric Parise0007522008-03-05 10:31:54 -0500679
Paul Moore2c971652016-04-19 16:36:28 -0400680 root_isec = backing_inode_security_novalidate(root);
681
Eric Parise0007522008-03-05 10:31:54 -0500682 /*
Eric Parisc9180a52007-11-30 13:00:35 -0500683 * parse the mount options, check if they are valid sids.
684 * also check if someone is trying to mount the same sb more
685 * than once with different security options.
686 */
Al Virobd323652018-12-13 15:04:59 -0500687 if (opts) {
688 if (opts->fscontext) {
Scott Mayhewcc274ae2021-12-15 16:28:40 -0500689 rc = parse_sid(sb, opts->fscontext, &fscontext_sid,
690 GFP_KERNEL);
Al Virobd323652018-12-13 15:04:59 -0500691 if (rc)
692 goto out;
Eric Parisc9180a52007-11-30 13:00:35 -0500693 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
694 fscontext_sid))
695 goto out_double_mount;
Eric Parisc9180a52007-11-30 13:00:35 -0500696 sbsec->flags |= FSCONTEXT_MNT;
Al Virobd323652018-12-13 15:04:59 -0500697 }
698 if (opts->context) {
Scott Mayhewcc274ae2021-12-15 16:28:40 -0500699 rc = parse_sid(sb, opts->context, &context_sid,
700 GFP_KERNEL);
Al Virobd323652018-12-13 15:04:59 -0500701 if (rc)
702 goto out;
Eric Parisc9180a52007-11-30 13:00:35 -0500703 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
704 context_sid))
705 goto out_double_mount;
Eric Parisc9180a52007-11-30 13:00:35 -0500706 sbsec->flags |= CONTEXT_MNT;
Al Virobd323652018-12-13 15:04:59 -0500707 }
708 if (opts->rootcontext) {
Scott Mayhewcc274ae2021-12-15 16:28:40 -0500709 rc = parse_sid(sb, opts->rootcontext, &rootcontext_sid,
710 GFP_KERNEL);
Al Virobd323652018-12-13 15:04:59 -0500711 if (rc)
712 goto out;
Eric Parisc9180a52007-11-30 13:00:35 -0500713 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
714 rootcontext_sid))
715 goto out_double_mount;
Eric Parisc9180a52007-11-30 13:00:35 -0500716 sbsec->flags |= ROOTCONTEXT_MNT;
Al Virobd323652018-12-13 15:04:59 -0500717 }
718 if (opts->defcontext) {
Scott Mayhewcc274ae2021-12-15 16:28:40 -0500719 rc = parse_sid(sb, opts->defcontext, &defcontext_sid,
720 GFP_KERNEL);
Al Virobd323652018-12-13 15:04:59 -0500721 if (rc)
722 goto out;
Eric Parisc9180a52007-11-30 13:00:35 -0500723 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
724 defcontext_sid))
725 goto out_double_mount;
Eric Parisc9180a52007-11-30 13:00:35 -0500726 sbsec->flags |= DEFCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500727 }
728 }
729
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500730 if (sbsec->flags & SE_SBINITIALIZED) {
Eric Parisc9180a52007-11-30 13:00:35 -0500731 /* previously mounted with options, but not on this attempt? */
Al Virobd323652018-12-13 15:04:59 -0500732 if ((sbsec->flags & SE_MNTMASK) && !opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500733 goto out_double_mount;
734 rc = 0;
735 goto out;
736 }
737
James Morris089be432008-07-15 18:32:49 +1000738 if (strcmp(sb->s_type->name, "proc") == 0)
Stephen Smalley134509d2015-06-04 16:22:17 -0400739 sbsec->flags |= SE_SBPROC | SE_SBGENFS;
740
Stephen Smalley8e014722015-06-04 16:22:17 -0400741 if (!strcmp(sb->s_type->name, "debugfs") ||
Jeff Vander Stoep6a391182017-06-20 09:35:33 -0700742 !strcmp(sb->s_type->name, "tracefs") ||
Hridya Valsarajua20456a2020-02-01 17:46:23 -0800743 !strcmp(sb->s_type->name, "binder") ||
Connor O'Brien4ca54d32020-02-07 10:01:49 -0800744 !strcmp(sb->s_type->name, "bpf") ||
Christian Göttsche8a764ef2021-09-28 17:39:31 +0200745 !strcmp(sb->s_type->name, "pstore") ||
746 !strcmp(sb->s_type->name, "securityfs"))
Ondrej Mosnacekb7540262019-02-22 15:57:14 +0100747 sbsec->flags |= SE_SBGENFS;
748
749 if (!strcmp(sb->s_type->name, "sysfs") ||
Antonio Murdaca901ef842017-02-09 17:02:42 +0100750 !strcmp(sb->s_type->name, "cgroup") ||
751 !strcmp(sb->s_type->name, "cgroup2"))
Ondrej Mosnacekb7540262019-02-22 15:57:14 +0100752 sbsec->flags |= SE_SBGENFS | SE_SBGENFS_XATTR;
Eric Parisc9180a52007-11-30 13:00:35 -0500753
David Quigleyeb9ae682013-05-22 12:50:37 -0400754 if (!sbsec->behavior) {
755 /*
756 * Determine the labeling behavior to use for this
757 * filesystem type.
758 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500759 rc = security_fs_use(&selinux_state, sb);
David Quigleyeb9ae682013-05-22 12:50:37 -0400760 if (rc) {
peter enderborgc103a912018-06-12 10:09:03 +0200761 pr_warn("%s: security_fs_use(%s) returned %d\n",
David Quigleyeb9ae682013-05-22 12:50:37 -0400762 __func__, sb->s_type->name, rc);
763 goto out;
764 }
Eric Parisc9180a52007-11-30 13:00:35 -0500765 }
Seth Forsheeaad82892016-04-26 14:36:20 -0500766
767 /*
Stephen Smalley01593d32017-01-09 10:07:31 -0500768 * If this is a user namespace mount and the filesystem type is not
769 * explicitly whitelisted, then no contexts are allowed on the command
770 * line and security labels must be ignored.
Seth Forsheeaad82892016-04-26 14:36:20 -0500771 */
Stephen Smalley01593d32017-01-09 10:07:31 -0500772 if (sb->s_user_ns != &init_user_ns &&
773 strcmp(sb->s_type->name, "tmpfs") &&
774 strcmp(sb->s_type->name, "ramfs") &&
Vivek Goyal7fa2e792021-02-11 13:03:03 -0500775 strcmp(sb->s_type->name, "devpts") &&
776 strcmp(sb->s_type->name, "overlay")) {
Seth Forsheeaad82892016-04-26 14:36:20 -0500777 if (context_sid || fscontext_sid || rootcontext_sid ||
778 defcontext_sid) {
779 rc = -EACCES;
780 goto out;
781 }
782 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
783 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500784 rc = security_transition_sid(&selinux_state,
785 current_sid(),
786 current_sid(),
Seth Forsheeaad82892016-04-26 14:36:20 -0500787 SECCLASS_FILE, NULL,
788 &sbsec->mntpoint_sid);
789 if (rc)
790 goto out;
791 }
792 goto out_set_opts;
793 }
794
Eric Parisc9180a52007-11-30 13:00:35 -0500795 /* sets the context of the superblock for the fs being mounted. */
796 if (fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100797 rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500798 if (rc)
799 goto out;
800
801 sbsec->sid = fscontext_sid;
802 }
803
804 /*
805 * Switch to using mount point labeling behavior.
806 * sets the label used on all file below the mountpoint, and will set
807 * the superblock context if not already set.
808 */
David Quigleyeb9ae682013-05-22 12:50:37 -0400809 if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !context_sid) {
810 sbsec->behavior = SECURITY_FS_USE_NATIVE;
811 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
812 }
813
Eric Parisc9180a52007-11-30 13:00:35 -0500814 if (context_sid) {
815 if (!fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100816 rc = may_context_mount_sb_relabel(context_sid, sbsec,
817 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500818 if (rc)
819 goto out;
820 sbsec->sid = context_sid;
821 } else {
David Howells275bb412008-11-14 10:39:19 +1100822 rc = may_context_mount_inode_relabel(context_sid, sbsec,
823 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500824 if (rc)
825 goto out;
826 }
827 if (!rootcontext_sid)
828 rootcontext_sid = context_sid;
829
830 sbsec->mntpoint_sid = context_sid;
831 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
832 }
833
834 if (rootcontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100835 rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec,
836 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500837 if (rc)
838 goto out;
839
840 root_isec->sid = rootcontext_sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -0500841 root_isec->initialized = LABEL_INITIALIZED;
Eric Parisc9180a52007-11-30 13:00:35 -0500842 }
843
844 if (defcontext_sid) {
David Quigleyeb9ae682013-05-22 12:50:37 -0400845 if (sbsec->behavior != SECURITY_FS_USE_XATTR &&
846 sbsec->behavior != SECURITY_FS_USE_NATIVE) {
Eric Parisc9180a52007-11-30 13:00:35 -0500847 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +0200848 pr_warn("SELinux: defcontext option is "
Eric Parisc9180a52007-11-30 13:00:35 -0500849 "invalid for this filesystem type\n");
850 goto out;
851 }
852
853 if (defcontext_sid != sbsec->def_sid) {
854 rc = may_context_mount_inode_relabel(defcontext_sid,
David Howells275bb412008-11-14 10:39:19 +1100855 sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500856 if (rc)
857 goto out;
858 }
859
860 sbsec->def_sid = defcontext_sid;
861 }
862
Seth Forsheeaad82892016-04-26 14:36:20 -0500863out_set_opts:
Eric Parisc9180a52007-11-30 13:00:35 -0500864 rc = sb_finish_set_opts(sb);
865out:
Eric Parisbc7e9822006-09-25 23:32:02 -0700866 mutex_unlock(&sbsec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 return rc;
Eric Parisc9180a52007-11-30 13:00:35 -0500868out_double_mount:
869 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +0200870 pr_warn("SELinux: mount invalid. Same superblock, different "
Al Virobd323652018-12-13 15:04:59 -0500871 "security settings for (dev %s, type %s)\n", sb->s_id,
872 sb->s_type->name);
Eric Parisc9180a52007-11-30 13:00:35 -0500873 goto out;
874}
875
Jeff Layton094f7b62013-04-01 08:14:24 -0400876static int selinux_cmp_sb_context(const struct super_block *oldsb,
877 const struct super_block *newsb)
878{
Casey Schaufler1aea7802021-04-22 17:41:15 +0200879 struct superblock_security_struct *old = selinux_superblock(oldsb);
880 struct superblock_security_struct *new = selinux_superblock(newsb);
Jeff Layton094f7b62013-04-01 08:14:24 -0400881 char oldflags = old->flags & SE_MNTMASK;
882 char newflags = new->flags & SE_MNTMASK;
883
884 if (oldflags != newflags)
885 goto mismatch;
886 if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid)
887 goto mismatch;
888 if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid)
889 goto mismatch;
890 if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid)
891 goto mismatch;
892 if (oldflags & ROOTCONTEXT_MNT) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500893 struct inode_security_struct *oldroot = backing_inode_security(oldsb->s_root);
894 struct inode_security_struct *newroot = backing_inode_security(newsb->s_root);
Jeff Layton094f7b62013-04-01 08:14:24 -0400895 if (oldroot->sid != newroot->sid)
896 goto mismatch;
897 }
898 return 0;
899mismatch:
peter enderborgc103a912018-06-12 10:09:03 +0200900 pr_warn("SELinux: mount invalid. Same superblock, "
Jeff Layton094f7b62013-04-01 08:14:24 -0400901 "different security settings for (dev %s, "
902 "type %s)\n", newsb->s_id, newsb->s_type->name);
903 return -EBUSY;
904}
905
906static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400907 struct super_block *newsb,
908 unsigned long kern_flags,
909 unsigned long *set_kern_flags)
Eric Parisc9180a52007-11-30 13:00:35 -0500910{
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400911 int rc = 0;
Casey Schaufler1aea7802021-04-22 17:41:15 +0200912 const struct superblock_security_struct *oldsbsec =
913 selinux_superblock(oldsb);
914 struct superblock_security_struct *newsbsec = selinux_superblock(newsb);
Eric Parisc9180a52007-11-30 13:00:35 -0500915
916 int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT);
917 int set_context = (oldsbsec->flags & CONTEXT_MNT);
918 int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT);
919
Eric Paris0f5e6422008-04-21 16:24:11 -0400920 /*
921 * if the parent was able to be mounted it clearly had no special lsm
Al Viroe8c26252010-03-23 06:36:54 -0400922 * mount options. thus we can safely deal with this superblock later
Eric Paris0f5e6422008-04-21 16:24:11 -0400923 */
Ondrej Mosnacek65cddd52020-01-07 14:31:53 +0100924 if (!selinux_initialized(&selinux_state))
Jeff Layton094f7b62013-04-01 08:14:24 -0400925 return 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500926
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400927 /*
928 * Specifying internal flags without providing a place to
929 * place the results is not allowed.
930 */
931 if (kern_flags && !set_kern_flags)
932 return -EINVAL;
933
Eric Parisc9180a52007-11-30 13:00:35 -0500934 /* how can we clone if the old one wasn't set up?? */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500935 BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED));
Eric Parisc9180a52007-11-30 13:00:35 -0500936
Jeff Layton094f7b62013-04-01 08:14:24 -0400937 /* if fs is reusing a sb, make sure that the contexts match */
J. Bruce Fields3815a242019-03-05 16:17:58 -0500938 if (newsbsec->flags & SE_SBINITIALIZED) {
939 if ((kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context)
940 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
Jeff Layton094f7b62013-04-01 08:14:24 -0400941 return selinux_cmp_sb_context(oldsb, newsb);
J. Bruce Fields3815a242019-03-05 16:17:58 -0500942 }
Eric Paris5a552612008-04-09 14:08:35 -0400943
Eric Parisc9180a52007-11-30 13:00:35 -0500944 mutex_lock(&newsbsec->lock);
945
946 newsbsec->flags = oldsbsec->flags;
947
948 newsbsec->sid = oldsbsec->sid;
949 newsbsec->def_sid = oldsbsec->def_sid;
950 newsbsec->behavior = oldsbsec->behavior;
951
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400952 if (newsbsec->behavior == SECURITY_FS_USE_NATIVE &&
953 !(kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500954 rc = security_fs_use(&selinux_state, newsb);
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400955 if (rc)
956 goto out;
957 }
958
959 if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !set_context) {
960 newsbsec->behavior = SECURITY_FS_USE_NATIVE;
961 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
962 }
963
Eric Parisc9180a52007-11-30 13:00:35 -0500964 if (set_context) {
965 u32 sid = oldsbsec->mntpoint_sid;
966
967 if (!set_fscontext)
968 newsbsec->sid = sid;
969 if (!set_rootcontext) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500970 struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
Eric Parisc9180a52007-11-30 13:00:35 -0500971 newisec->sid = sid;
972 }
973 newsbsec->mntpoint_sid = sid;
974 }
975 if (set_rootcontext) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500976 const struct inode_security_struct *oldisec = backing_inode_security(oldsb->s_root);
977 struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
Eric Parisc9180a52007-11-30 13:00:35 -0500978
979 newisec->sid = oldisec->sid;
980 }
981
982 sb_finish_set_opts(newsb);
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400983out:
Eric Parisc9180a52007-11-30 13:00:35 -0500984 mutex_unlock(&newsbsec->lock);
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400985 return rc;
Eric Parisc9180a52007-11-30 13:00:35 -0500986}
987
Al Viroba641862018-12-14 20:28:15 -0500988static int selinux_add_opt(int token, const char *s, void **mnt_opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500989{
Al Viroba641862018-12-14 20:28:15 -0500990 struct selinux_mnt_opts *opts = *mnt_opts;
Eric Parisc9180a52007-11-30 13:00:35 -0500991
Al Viroda3d76a2018-12-17 10:14:16 -0500992 if (token == Opt_seclabel) /* eaten and completely ignored */
Al Viro169d68efb2018-12-14 22:44:50 -0500993 return 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500994
Al Viroba641862018-12-14 20:28:15 -0500995 if (!opts) {
996 opts = kzalloc(sizeof(struct selinux_mnt_opts), GFP_KERNEL);
997 if (!opts)
998 return -ENOMEM;
999 *mnt_opts = opts;
1000 }
1001 if (!s)
1002 return -ENOMEM;
1003 switch (token) {
1004 case Opt_context:
1005 if (opts->context || opts->defcontext)
1006 goto Einval;
1007 opts->context = s;
1008 break;
1009 case Opt_fscontext:
1010 if (opts->fscontext)
1011 goto Einval;
1012 opts->fscontext = s;
1013 break;
1014 case Opt_rootcontext:
1015 if (opts->rootcontext)
1016 goto Einval;
1017 opts->rootcontext = s;
1018 break;
1019 case Opt_defcontext:
1020 if (opts->context || opts->defcontext)
1021 goto Einval;
1022 opts->defcontext = s;
1023 break;
1024 }
1025 return 0;
1026Einval:
1027 pr_warn(SEL_MOUNT_FAIL_MSG);
Al Viroba641862018-12-14 20:28:15 -05001028 return -EINVAL;
1029}
Eric Parisc9180a52007-11-30 13:00:35 -05001030
Al Viro757cbe52018-12-14 23:42:21 -05001031static int selinux_add_mnt_opt(const char *option, const char *val, int len,
1032 void **mnt_opts)
Eric Parisc9180a52007-11-30 13:00:35 -05001033{
Al Viro757cbe52018-12-14 23:42:21 -05001034 int token = Opt_error;
1035 int rc, i;
Eric Parisc9180a52007-11-30 13:00:35 -05001036
Al Viro757cbe52018-12-14 23:42:21 -05001037 for (i = 0; i < ARRAY_SIZE(tokens); i++) {
1038 if (strcmp(option, tokens[i].name) == 0) {
1039 token = tokens[i].opt;
Eric Parisc9180a52007-11-30 13:00:35 -05001040 break;
Eric Parisc9180a52007-11-30 13:00:35 -05001041 }
1042 }
1043
Al Viro757cbe52018-12-14 23:42:21 -05001044 if (token == Opt_error)
1045 return -EINVAL;
Eric Parise0007522008-03-05 10:31:54 -05001046
Gen Zhange2e0e092019-06-12 21:28:21 +08001047 if (token != Opt_seclabel) {
Al Viro757cbe52018-12-14 23:42:21 -05001048 val = kmemdup_nul(val, len, GFP_KERNEL);
Gen Zhange2e0e092019-06-12 21:28:21 +08001049 if (!val) {
1050 rc = -ENOMEM;
1051 goto free_opt;
1052 }
1053 }
Al Viro757cbe52018-12-14 23:42:21 -05001054 rc = selinux_add_opt(token, val, mnt_opts);
1055 if (unlikely(rc)) {
1056 kfree(val);
Gen Zhange2e0e092019-06-12 21:28:21 +08001057 goto free_opt;
1058 }
1059 return rc;
1060
1061free_opt:
1062 if (*mnt_opts) {
1063 selinux_free_mnt_opts(*mnt_opts);
1064 *mnt_opts = NULL;
Al Viro757cbe52018-12-14 23:42:21 -05001065 }
1066 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Al Viroe3489f82018-12-13 00:24:36 -05001069static int show_sid(struct seq_file *m, u32 sid)
Eric Paris2069f452008-07-04 09:47:13 +10001070{
Al Viroe3489f82018-12-13 00:24:36 -05001071 char *context = NULL;
1072 u32 len;
1073 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
Al Viroe3489f82018-12-13 00:24:36 -05001075 rc = security_sid_to_context(&selinux_state, sid,
1076 &context, &len);
1077 if (!rc) {
1078 bool has_comma = context && strchr(context, ',');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
David Howells442155c2018-11-01 23:07:24 +00001080 seq_putc(m, '=');
Eric Paris2069f452008-07-04 09:47:13 +10001081 if (has_comma)
1082 seq_putc(m, '\"');
Al Viroe3489f82018-12-13 00:24:36 -05001083 seq_escape(m, context, "\"\n\\");
Eric Paris2069f452008-07-04 09:47:13 +10001084 if (has_comma)
1085 seq_putc(m, '\"');
1086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 kfree(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 return rc;
1089}
Eric Paris2069f452008-07-04 09:47:13 +10001090
1091static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
1092{
Casey Schaufler1aea7802021-04-22 17:41:15 +02001093 struct superblock_security_struct *sbsec = selinux_superblock(sb);
Eric Paris2069f452008-07-04 09:47:13 +10001094 int rc;
1095
Al Viroe3489f82018-12-13 00:24:36 -05001096 if (!(sbsec->flags & SE_SBINITIALIZED))
1097 return 0;
1098
Ondrej Mosnacek65cddd52020-01-07 14:31:53 +01001099 if (!selinux_initialized(&selinux_state))
Al Viroe3489f82018-12-13 00:24:36 -05001100 return 0;
1101
1102 if (sbsec->flags & FSCONTEXT_MNT) {
1103 seq_putc(m, ',');
1104 seq_puts(m, FSCONTEXT_STR);
1105 rc = show_sid(m, sbsec->sid);
1106 if (rc)
1107 return rc;
Eric Paris383795c2008-07-29 17:07:26 -04001108 }
Al Viroe3489f82018-12-13 00:24:36 -05001109 if (sbsec->flags & CONTEXT_MNT) {
1110 seq_putc(m, ',');
1111 seq_puts(m, CONTEXT_STR);
1112 rc = show_sid(m, sbsec->mntpoint_sid);
1113 if (rc)
1114 return rc;
1115 }
1116 if (sbsec->flags & DEFCONTEXT_MNT) {
1117 seq_putc(m, ',');
1118 seq_puts(m, DEFCONTEXT_STR);
1119 rc = show_sid(m, sbsec->def_sid);
1120 if (rc)
1121 return rc;
1122 }
1123 if (sbsec->flags & ROOTCONTEXT_MNT) {
Ondrej Mosnacekb159e862020-11-04 13:01:10 +01001124 struct dentry *root = sb->s_root;
Al Viroe3489f82018-12-13 00:24:36 -05001125 struct inode_security_struct *isec = backing_inode_security(root);
1126 seq_putc(m, ',');
1127 seq_puts(m, ROOTCONTEXT_STR);
1128 rc = show_sid(m, isec->sid);
1129 if (rc)
1130 return rc;
1131 }
1132 if (sbsec->flags & SBLABEL_MNT) {
1133 seq_putc(m, ',');
David Howells442155c2018-11-01 23:07:24 +00001134 seq_puts(m, SECLABEL_STR);
Al Viroe3489f82018-12-13 00:24:36 -05001135 }
1136 return 0;
Eric Paris2069f452008-07-04 09:47:13 +10001137}
1138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139static inline u16 inode_mode_to_security_class(umode_t mode)
1140{
1141 switch (mode & S_IFMT) {
1142 case S_IFSOCK:
1143 return SECCLASS_SOCK_FILE;
1144 case S_IFLNK:
1145 return SECCLASS_LNK_FILE;
1146 case S_IFREG:
1147 return SECCLASS_FILE;
1148 case S_IFBLK:
1149 return SECCLASS_BLK_FILE;
1150 case S_IFDIR:
1151 return SECCLASS_DIR;
1152 case S_IFCHR:
1153 return SECCLASS_CHR_FILE;
1154 case S_IFIFO:
1155 return SECCLASS_FIFO_FILE;
1156
1157 }
1158
1159 return SECCLASS_FILE;
1160}
1161
James Morris13402582005-09-30 14:24:34 -04001162static inline int default_protocol_stream(int protocol)
1163{
Paolo Abeni95ca9072020-12-16 12:55:27 +01001164 return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP ||
1165 protocol == IPPROTO_MPTCP);
James Morris13402582005-09-30 14:24:34 -04001166}
1167
1168static inline int default_protocol_dgram(int protocol)
1169{
1170 return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
1171}
1172
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173static inline u16 socket_type_to_security_class(int family, int type, int protocol)
1174{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001175 int extsockclass = selinux_policycap_extsockclass();
Stephen Smalleyda69a532017-01-09 10:07:30 -05001176
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 switch (family) {
1178 case PF_UNIX:
1179 switch (type) {
1180 case SOCK_STREAM:
1181 case SOCK_SEQPACKET:
1182 return SECCLASS_UNIX_STREAM_SOCKET;
1183 case SOCK_DGRAM:
Luis Ressel2a764b52017-07-25 15:13:41 -04001184 case SOCK_RAW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 return SECCLASS_UNIX_DGRAM_SOCKET;
1186 }
1187 break;
1188 case PF_INET:
1189 case PF_INET6:
1190 switch (type) {
1191 case SOCK_STREAM:
Stephen Smalleyda69a532017-01-09 10:07:30 -05001192 case SOCK_SEQPACKET:
James Morris13402582005-09-30 14:24:34 -04001193 if (default_protocol_stream(protocol))
1194 return SECCLASS_TCP_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001195 else if (extsockclass && protocol == IPPROTO_SCTP)
1196 return SECCLASS_SCTP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001197 else
1198 return SECCLASS_RAWIP_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 case SOCK_DGRAM:
James Morris13402582005-09-30 14:24:34 -04001200 if (default_protocol_dgram(protocol))
1201 return SECCLASS_UDP_SOCKET;
Stephen Smalleyef379792017-01-09 10:07:31 -05001202 else if (extsockclass && (protocol == IPPROTO_ICMP ||
1203 protocol == IPPROTO_ICMPV6))
Stephen Smalleyda69a532017-01-09 10:07:30 -05001204 return SECCLASS_ICMP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001205 else
1206 return SECCLASS_RAWIP_SOCKET;
James Morris2ee92d42006-11-13 16:09:01 -08001207 case SOCK_DCCP:
1208 return SECCLASS_DCCP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001209 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 return SECCLASS_RAWIP_SOCKET;
1211 }
1212 break;
1213 case PF_NETLINK:
1214 switch (protocol) {
1215 case NETLINK_ROUTE:
1216 return SECCLASS_NETLINK_ROUTE_SOCKET;
Pavel Emelyanov7f1fb602011-12-06 07:56:43 +00001217 case NETLINK_SOCK_DIAG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 return SECCLASS_NETLINK_TCPDIAG_SOCKET;
1219 case NETLINK_NFLOG:
1220 return SECCLASS_NETLINK_NFLOG_SOCKET;
1221 case NETLINK_XFRM:
1222 return SECCLASS_NETLINK_XFRM_SOCKET;
1223 case NETLINK_SELINUX:
1224 return SECCLASS_NETLINK_SELINUX_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001225 case NETLINK_ISCSI:
1226 return SECCLASS_NETLINK_ISCSI_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 case NETLINK_AUDIT:
1228 return SECCLASS_NETLINK_AUDIT_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001229 case NETLINK_FIB_LOOKUP:
1230 return SECCLASS_NETLINK_FIB_LOOKUP_SOCKET;
1231 case NETLINK_CONNECTOR:
1232 return SECCLASS_NETLINK_CONNECTOR_SOCKET;
1233 case NETLINK_NETFILTER:
1234 return SECCLASS_NETLINK_NETFILTER_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 case NETLINK_DNRTMSG:
1236 return SECCLASS_NETLINK_DNRT_SOCKET;
James Morris0c9b7942005-04-16 15:24:13 -07001237 case NETLINK_KOBJECT_UEVENT:
1238 return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001239 case NETLINK_GENERIC:
1240 return SECCLASS_NETLINK_GENERIC_SOCKET;
1241 case NETLINK_SCSITRANSPORT:
1242 return SECCLASS_NETLINK_SCSITRANSPORT_SOCKET;
1243 case NETLINK_RDMA:
1244 return SECCLASS_NETLINK_RDMA_SOCKET;
1245 case NETLINK_CRYPTO:
1246 return SECCLASS_NETLINK_CRYPTO_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 default:
1248 return SECCLASS_NETLINK_SOCKET;
1249 }
1250 case PF_PACKET:
1251 return SECCLASS_PACKET_SOCKET;
1252 case PF_KEY:
1253 return SECCLASS_KEY_SOCKET;
Christopher J. PeBenito3e3ff152006-06-09 00:25:03 -07001254 case PF_APPLETALK:
1255 return SECCLASS_APPLETALK_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 }
1257
Stephen Smalleyda69a532017-01-09 10:07:30 -05001258 if (extsockclass) {
1259 switch (family) {
1260 case PF_AX25:
1261 return SECCLASS_AX25_SOCKET;
1262 case PF_IPX:
1263 return SECCLASS_IPX_SOCKET;
1264 case PF_NETROM:
1265 return SECCLASS_NETROM_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001266 case PF_ATMPVC:
1267 return SECCLASS_ATMPVC_SOCKET;
1268 case PF_X25:
1269 return SECCLASS_X25_SOCKET;
1270 case PF_ROSE:
1271 return SECCLASS_ROSE_SOCKET;
1272 case PF_DECnet:
1273 return SECCLASS_DECNET_SOCKET;
1274 case PF_ATMSVC:
1275 return SECCLASS_ATMSVC_SOCKET;
1276 case PF_RDS:
1277 return SECCLASS_RDS_SOCKET;
1278 case PF_IRDA:
1279 return SECCLASS_IRDA_SOCKET;
1280 case PF_PPPOX:
1281 return SECCLASS_PPPOX_SOCKET;
1282 case PF_LLC:
1283 return SECCLASS_LLC_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001284 case PF_CAN:
1285 return SECCLASS_CAN_SOCKET;
1286 case PF_TIPC:
1287 return SECCLASS_TIPC_SOCKET;
1288 case PF_BLUETOOTH:
1289 return SECCLASS_BLUETOOTH_SOCKET;
1290 case PF_IUCV:
1291 return SECCLASS_IUCV_SOCKET;
1292 case PF_RXRPC:
1293 return SECCLASS_RXRPC_SOCKET;
1294 case PF_ISDN:
1295 return SECCLASS_ISDN_SOCKET;
1296 case PF_PHONET:
1297 return SECCLASS_PHONET_SOCKET;
1298 case PF_IEEE802154:
1299 return SECCLASS_IEEE802154_SOCKET;
1300 case PF_CAIF:
1301 return SECCLASS_CAIF_SOCKET;
1302 case PF_ALG:
1303 return SECCLASS_ALG_SOCKET;
1304 case PF_NFC:
1305 return SECCLASS_NFC_SOCKET;
1306 case PF_VSOCK:
1307 return SECCLASS_VSOCK_SOCKET;
1308 case PF_KCM:
1309 return SECCLASS_KCM_SOCKET;
1310 case PF_QIPCRTR:
1311 return SECCLASS_QIPCRTR_SOCKET;
Linus Torvalds3051bf32017-02-22 10:15:09 -08001312 case PF_SMC:
1313 return SECCLASS_SMC_SOCKET;
Björn Töpel68e8b842018-05-02 13:01:22 +02001314 case PF_XDP:
1315 return SECCLASS_XDP_SOCKET;
Jeremy Kerrbc49d812021-07-29 10:20:39 +08001316 case PF_MCTP:
1317 return SECCLASS_MCTP_SOCKET;
1318#if PF_MAX > 46
Stephen Smalleyda69a532017-01-09 10:07:30 -05001319#error New address family defined, please update this function.
1320#endif
1321 }
1322 }
1323
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 return SECCLASS_SOCKET;
1325}
1326
Stephen Smalley134509d2015-06-04 16:22:17 -04001327static int selinux_genfs_get_sid(struct dentry *dentry,
1328 u16 tclass,
1329 u16 flags,
1330 u32 *sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331{
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001332 int rc;
Al Virofc640052016-04-10 01:33:30 -04001333 struct super_block *sb = dentry->d_sb;
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001334 char *buffer, *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Eric Paris828dfe12008-04-17 13:17:49 -04001336 buffer = (char *)__get_free_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 if (!buffer)
1338 return -ENOMEM;
1339
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001340 path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
1341 if (IS_ERR(path))
1342 rc = PTR_ERR(path);
1343 else {
Stephen Smalley134509d2015-06-04 16:22:17 -04001344 if (flags & SE_SBPROC) {
1345 /* each process gets a /proc/PID/ entry. Strip off the
1346 * PID part to get a valid selinux labeling.
1347 * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
1348 while (path[1] >= '0' && path[1] <= '9') {
1349 path[1] = '/';
1350 path++;
1351 }
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001352 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001353 rc = security_genfs_sid(&selinux_state, sb->s_type->name,
1354 path, tclass, sid);
Stephen Smalley7bb185e2018-09-04 16:51:36 -04001355 if (rc == -ENOENT) {
1356 /* No match in policy, mark as unlabeled. */
1357 *sid = SECINITSID_UNLABELED;
1358 rc = 0;
1359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 free_page((unsigned long)buffer);
1362 return rc;
1363}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Ondrej Mosnacekb7540262019-02-22 15:57:14 +01001365static int inode_doinit_use_xattr(struct inode *inode, struct dentry *dentry,
1366 u32 def_sid, u32 *sid)
1367{
1368#define INITCONTEXTLEN 255
1369 char *context;
1370 unsigned int len;
1371 int rc;
1372
1373 len = INITCONTEXTLEN;
1374 context = kmalloc(len + 1, GFP_NOFS);
1375 if (!context)
1376 return -ENOMEM;
1377
1378 context[len] = '\0';
1379 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
1380 if (rc == -ERANGE) {
1381 kfree(context);
1382
1383 /* Need a larger buffer. Query for the right size. */
1384 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, NULL, 0);
1385 if (rc < 0)
1386 return rc;
1387
1388 len = rc;
1389 context = kmalloc(len + 1, GFP_NOFS);
1390 if (!context)
1391 return -ENOMEM;
1392
1393 context[len] = '\0';
1394 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX,
1395 context, len);
1396 }
1397 if (rc < 0) {
1398 kfree(context);
1399 if (rc != -ENODATA) {
1400 pr_warn("SELinux: %s: getxattr returned %d for dev=%s ino=%ld\n",
1401 __func__, -rc, inode->i_sb->s_id, inode->i_ino);
1402 return rc;
1403 }
1404 *sid = def_sid;
1405 return 0;
1406 }
1407
1408 rc = security_context_to_sid_default(&selinux_state, context, rc, sid,
1409 def_sid, GFP_NOFS);
1410 if (rc) {
1411 char *dev = inode->i_sb->s_id;
1412 unsigned long ino = inode->i_ino;
1413
1414 if (rc == -EINVAL) {
1415 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",
1416 ino, dev, context);
1417 } else {
1418 pr_warn("SELinux: %s: context_to_sid(%s) returned %d for dev=%s ino=%ld\n",
1419 __func__, context, -rc, dev, ino);
1420 }
1421 }
1422 kfree(context);
1423 return 0;
1424}
1425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426/* The inode's security attributes must be initialized before first use. */
1427static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
1428{
1429 struct superblock_security_struct *sbsec = NULL;
Casey Schaufler80788c22018-09-21 17:19:11 -07001430 struct inode_security_struct *isec = selinux_inode(inode);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001431 u32 task_sid, sid = 0;
1432 u16 sclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05001436 if (isec->initialized == LABEL_INITIALIZED)
Andreas Gruenbacher13457d02016-11-10 22:18:29 +01001437 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001439 spin_lock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05001440 if (isec->initialized == LABEL_INITIALIZED)
Eric Paris23970742006-09-25 23:32:01 -07001441 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
Andreas Gruenbacher13457d02016-11-10 22:18:29 +01001443 if (isec->sclass == SECCLASS_FILE)
1444 isec->sclass = inode_mode_to_security_class(inode->i_mode);
1445
Casey Schaufler1aea7802021-04-22 17:41:15 +02001446 sbsec = selinux_superblock(inode->i_sb);
David P. Quigley0d90a7e2009-01-16 09:22:02 -05001447 if (!(sbsec->flags & SE_SBINITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 /* Defer initialization until selinux_complete_init,
1449 after the initial policy is loaded and the security
1450 server is ready to handle calls. */
1451 spin_lock(&sbsec->isec_lock);
1452 if (list_empty(&isec->list))
1453 list_add(&isec->list, &sbsec->isec_head);
1454 spin_unlock(&sbsec->isec_lock);
Eric Paris23970742006-09-25 23:32:01 -07001455 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 }
1457
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001458 sclass = isec->sclass;
1459 task_sid = isec->task_sid;
1460 sid = isec->sid;
1461 isec->initialized = LABEL_PENDING;
1462 spin_unlock(&isec->lock);
1463
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 switch (sbsec->behavior) {
David Quigleyeb9ae682013-05-22 12:50:37 -04001465 case SECURITY_FS_USE_NATIVE:
1466 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 case SECURITY_FS_USE_XATTR:
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001468 if (!(inode->i_opflags & IOP_XATTR)) {
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001469 sid = sbsec->def_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 break;
1471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 /* Need a dentry, since the xattr API requires one.
1473 Life would be simpler if we could just pass the inode. */
1474 if (opt_dentry) {
1475 /* Called from d_instantiate or d_splice_alias. */
1476 dentry = dget(opt_dentry);
1477 } else {
Al Virob1271252018-04-25 10:28:38 -04001478 /*
1479 * Called from selinux_complete_init, try to find a dentry.
1480 * Some filesystems really want a connected one, so try
1481 * that first. We could split SECURITY_FS_USE_XATTR in
1482 * two, depending upon that...
1483 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 dentry = d_find_alias(inode);
Al Virob1271252018-04-25 10:28:38 -04001485 if (!dentry)
1486 dentry = d_find_any_alias(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 }
1488 if (!dentry) {
Eric Parisdf7f54c2009-03-09 14:35:58 -04001489 /*
1490 * this is can be hit on boot when a file is accessed
1491 * before the policy is loaded. When we load policy we
1492 * may find inodes that have no dentry on the
1493 * sbsec->isec_head list. No reason to complain as these
1494 * will get fixed up the next time we go through
1495 * inode_doinit with a dentry, before these inodes could
1496 * be used again by userspace.
1497 */
Paul Moore200ea5a2020-11-03 11:49:38 -05001498 goto out_invalid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 }
1500
Ondrej Mosnacekb7540262019-02-22 15:57:14 +01001501 rc = inode_doinit_use_xattr(inode, dentry, sbsec->def_sid,
1502 &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 dput(dentry);
Ondrej Mosnacekb7540262019-02-22 15:57:14 +01001504 if (rc)
1505 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 break;
1507 case SECURITY_FS_USE_TASK:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001508 sid = task_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 break;
1510 case SECURITY_FS_USE_TRANS:
1511 /* Default to the fs SID. */
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001512 sid = sbsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
1514 /* Try to obtain a transition SID. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001515 rc = security_transition_sid(&selinux_state, task_sid, sid,
1516 sclass, NULL, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 if (rc)
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001518 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 break;
Eric Parisc312feb2006-07-10 04:43:53 -07001520 case SECURITY_FS_USE_MNTPOINT:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001521 sid = sbsec->mntpoint_sid;
Eric Parisc312feb2006-07-10 04:43:53 -07001522 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 default:
Eric Parisc312feb2006-07-10 04:43:53 -07001524 /* Default to the fs superblock SID. */
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001525 sid = sbsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Christian Göttsche7470d0d12020-01-28 20:16:48 +01001527 if ((sbsec->flags & SE_SBGENFS) &&
1528 (!S_ISLNK(inode->i_mode) ||
1529 selinux_policycap_genfs_seclabel_symlinks())) {
Paul Mooref64410e2014-03-19 16:46:18 -04001530 /* We must have a dentry to determine the label on
1531 * procfs inodes */
Al Virob1271252018-04-25 10:28:38 -04001532 if (opt_dentry) {
Paul Mooref64410e2014-03-19 16:46:18 -04001533 /* Called from d_instantiate or
1534 * d_splice_alias. */
1535 dentry = dget(opt_dentry);
Al Virob1271252018-04-25 10:28:38 -04001536 } else {
Paul Mooref64410e2014-03-19 16:46:18 -04001537 /* Called from selinux_complete_init, try to
Al Virob1271252018-04-25 10:28:38 -04001538 * find a dentry. Some filesystems really want
1539 * a connected one, so try that first.
1540 */
Paul Mooref64410e2014-03-19 16:46:18 -04001541 dentry = d_find_alias(inode);
Al Virob1271252018-04-25 10:28:38 -04001542 if (!dentry)
1543 dentry = d_find_any_alias(inode);
1544 }
Paul Mooref64410e2014-03-19 16:46:18 -04001545 /*
1546 * This can be hit on boot when a file is accessed
1547 * before the policy is loaded. When we load policy we
1548 * may find inodes that have no dentry on the
1549 * sbsec->isec_head list. No reason to complain as
1550 * these will get fixed up the next time we go through
1551 * inode_doinit() with a dentry, before these inodes
1552 * could be used again by userspace.
1553 */
1554 if (!dentry)
Paul Moore200ea5a2020-11-03 11:49:38 -05001555 goto out_invalid;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001556 rc = selinux_genfs_get_sid(dentry, sclass,
Stephen Smalley134509d2015-06-04 16:22:17 -04001557 sbsec->flags, &sid);
Ondrej Mosnacekb7540262019-02-22 15:57:14 +01001558 if (rc) {
1559 dput(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001560 goto out;
Ondrej Mosnacekb7540262019-02-22 15:57:14 +01001561 }
1562
1563 if ((sbsec->flags & SE_SBGENFS_XATTR) &&
1564 (inode->i_opflags & IOP_XATTR)) {
1565 rc = inode_doinit_use_xattr(inode, dentry,
1566 sid, &sid);
1567 if (rc) {
1568 dput(dentry);
1569 goto out;
1570 }
1571 }
1572 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 }
1574 break;
1575 }
1576
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001577out:
1578 spin_lock(&isec->lock);
1579 if (isec->initialized == LABEL_PENDING) {
Paul Moore200ea5a2020-11-03 11:49:38 -05001580 if (rc) {
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001581 isec->initialized = LABEL_INVALID;
1582 goto out_unlock;
1583 }
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001584 isec->initialized = LABEL_INITIALIZED;
1585 isec->sid = sid;
1586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Eric Paris23970742006-09-25 23:32:01 -07001588out_unlock:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001589 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 return rc;
Paul Moore200ea5a2020-11-03 11:49:38 -05001591
1592out_invalid:
1593 spin_lock(&isec->lock);
1594 if (isec->initialized == LABEL_PENDING) {
1595 isec->initialized = LABEL_INVALID;
1596 isec->sid = sid;
1597 }
1598 spin_unlock(&isec->lock);
1599 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600}
1601
1602/* Convert a Linux signal to an access vector. */
1603static inline u32 signal_to_av(int sig)
1604{
1605 u32 perm = 0;
1606
1607 switch (sig) {
1608 case SIGCHLD:
1609 /* Commonly granted from child to parent. */
1610 perm = PROCESS__SIGCHLD;
1611 break;
1612 case SIGKILL:
1613 /* Cannot be caught or ignored */
1614 perm = PROCESS__SIGKILL;
1615 break;
1616 case SIGSTOP:
1617 /* Cannot be caught or ignored */
1618 perm = PROCESS__SIGSTOP;
1619 break;
1620 default:
1621 /* All other signals. */
1622 perm = PROCESS__SIGNAL;
1623 break;
1624 }
1625
1626 return perm;
1627}
1628
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001629#if CAP_LAST_CAP > 63
1630#error Fix SELinux to handle capabilities > 63.
1631#endif
1632
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633/* Check whether a task is allowed to use a capability. */
Eric Paris6a9de492012-01-03 12:25:14 -05001634static int cred_has_capability(const struct cred *cred,
Micah Mortonc1a85a02019-01-07 16:10:53 -08001635 int cap, unsigned int opts, bool initns)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636{
Thomas Liu2bf49692009-07-14 12:14:09 -04001637 struct common_audit_data ad;
Eric Paris06112162008-11-11 22:02:50 +11001638 struct av_decision avd;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001639 u16 sclass;
David Howells3699c532009-01-06 22:27:01 +00001640 u32 sid = cred_sid(cred);
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001641 u32 av = CAP_TO_MASK(cap);
Eric Paris06112162008-11-11 22:02:50 +11001642 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Eric Paris50c205f2012-04-04 15:01:43 -04001644 ad.type = LSM_AUDIT_DATA_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 ad.u.cap = cap;
1646
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001647 switch (CAP_TO_INDEX(cap)) {
1648 case 0:
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04001649 sclass = initns ? SECCLASS_CAPABILITY : SECCLASS_CAP_USERNS;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001650 break;
1651 case 1:
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04001652 sclass = initns ? SECCLASS_CAPABILITY2 : SECCLASS_CAP2_USERNS;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001653 break;
1654 default:
peter enderborgc103a912018-06-12 10:09:03 +02001655 pr_err("SELinux: out of range capability %d\n", cap);
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001656 BUG();
Eric Parisa35c6c832011-04-20 10:21:28 -04001657 return -EINVAL;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001658 }
Eric Paris06112162008-11-11 22:02:50 +11001659
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001660 rc = avc_has_perm_noaudit(&selinux_state,
1661 sid, sid, sclass, av, 0, &avd);
Micah Mortonc1a85a02019-01-07 16:10:53 -08001662 if (!(opts & CAP_OPT_NOAUDIT)) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001663 int rc2 = avc_audit(&selinux_state,
Al Virod99cf132021-01-16 15:57:49 -05001664 sid, sid, sclass, av, &avd, rc, &ad);
Eric Paris9ade0cf2011-04-25 16:26:29 -04001665 if (rc2)
1666 return rc2;
1667 }
Eric Paris06112162008-11-11 22:02:50 +11001668 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669}
1670
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671/* Check whether a task has a particular permission to an inode.
1672 The 'adp' parameter is optional and allows other audit
1673 data to be passed (e.g. the dentry). */
David Howells88e67f32008-11-14 10:39:21 +11001674static int inode_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 struct inode *inode,
1676 u32 perms,
Linus Torvalds19e49832013-10-04 12:54:11 -07001677 struct common_audit_data *adp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 struct inode_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +11001680 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
David Howellse0e81732009-09-02 09:13:40 +01001682 validate_creds(cred);
1683
Eric Paris828dfe12008-04-17 13:17:49 -04001684 if (unlikely(IS_PRIVATE(inode)))
Stephen Smalleybbaca6c2007-02-14 00:34:16 -08001685 return 0;
1686
David Howells88e67f32008-11-14 10:39:21 +11001687 sid = cred_sid(cred);
Casey Schaufler80788c22018-09-21 17:19:11 -07001688 isec = selinux_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001690 return avc_has_perm(&selinux_state,
1691 sid, isec->sid, isec->sclass, perms, adp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692}
1693
1694/* Same as inode_has_perm, but pass explicit audit data containing
1695 the dentry to help the auditing code to more easily generate the
1696 pathname if needed. */
David Howells88e67f32008-11-14 10:39:21 +11001697static inline int dentry_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 struct dentry *dentry,
1699 u32 av)
1700{
David Howellsc6f493d2015-03-17 22:26:22 +00001701 struct inode *inode = d_backing_inode(dentry);
Thomas Liu2bf49692009-07-14 12:14:09 -04001702 struct common_audit_data ad;
David Howells88e67f32008-11-14 10:39:21 +11001703
Eric Paris50c205f2012-04-04 15:01:43 -04001704 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Paris2875fa02011-04-28 16:04:24 -04001705 ad.u.dentry = dentry;
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05001706 __inode_security_revalidate(inode, dentry, true);
Linus Torvalds19e49832013-10-04 12:54:11 -07001707 return inode_has_perm(cred, inode, av, &ad);
Eric Paris2875fa02011-04-28 16:04:24 -04001708}
1709
1710/* Same as inode_has_perm, but pass explicit audit data containing
1711 the path to help the auditing code to more easily generate the
1712 pathname if needed. */
1713static inline int path_has_perm(const struct cred *cred,
Al Viro3f7036a2015-03-08 19:28:30 -04001714 const struct path *path,
Eric Paris2875fa02011-04-28 16:04:24 -04001715 u32 av)
1716{
David Howellsc6f493d2015-03-17 22:26:22 +00001717 struct inode *inode = d_backing_inode(path->dentry);
Eric Paris2875fa02011-04-28 16:04:24 -04001718 struct common_audit_data ad;
1719
Eric Paris50c205f2012-04-04 15:01:43 -04001720 ad.type = LSM_AUDIT_DATA_PATH;
Eric Paris2875fa02011-04-28 16:04:24 -04001721 ad.u.path = *path;
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05001722 __inode_security_revalidate(inode, path->dentry, true);
Linus Torvalds19e49832013-10-04 12:54:11 -07001723 return inode_has_perm(cred, inode, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724}
1725
David Howells13f8e982013-06-13 23:37:55 +01001726/* Same as path_has_perm, but uses the inode from the file struct. */
1727static inline int file_path_has_perm(const struct cred *cred,
1728 struct file *file,
1729 u32 av)
1730{
1731 struct common_audit_data ad;
1732
Vivek Goyal43af5de2016-09-09 11:37:49 -04001733 ad.type = LSM_AUDIT_DATA_FILE;
1734 ad.u.file = file;
Linus Torvalds19e49832013-10-04 12:54:11 -07001735 return inode_has_perm(cred, file_inode(file), av, &ad);
David Howells13f8e982013-06-13 23:37:55 +01001736}
1737
Chenbo Fengf66e4482017-10-18 13:00:26 -07001738#ifdef CONFIG_BPF_SYSCALL
1739static int bpf_fd_pass(struct file *file, u32 sid);
1740#endif
1741
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742/* Check whether a task can use an open file descriptor to
1743 access an inode in a given way. Check access to the
1744 descriptor itself, and then use dentry_has_perm to
1745 check a particular permission to the file.
1746 Access to the descriptor is implicitly granted if it
1747 has the same SID as the process. If av is zero, then
1748 access to the file is not checked, e.g. for cases
1749 where only the descriptor is affected like seek. */
David Howells88e67f32008-11-14 10:39:21 +11001750static int file_has_perm(const struct cred *cred,
1751 struct file *file,
1752 u32 av)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753{
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07001754 struct file_security_struct *fsec = selinux_file(file);
Al Viro496ad9a2013-01-23 17:07:38 -05001755 struct inode *inode = file_inode(file);
Thomas Liu2bf49692009-07-14 12:14:09 -04001756 struct common_audit_data ad;
David Howells88e67f32008-11-14 10:39:21 +11001757 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 int rc;
1759
Vivek Goyal43af5de2016-09-09 11:37:49 -04001760 ad.type = LSM_AUDIT_DATA_FILE;
1761 ad.u.file = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
David Howells275bb412008-11-14 10:39:19 +11001763 if (sid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001764 rc = avc_has_perm(&selinux_state,
1765 sid, fsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 SECCLASS_FD,
1767 FD__USE,
1768 &ad);
1769 if (rc)
David Howells88e67f32008-11-14 10:39:21 +11001770 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 }
1772
Chenbo Fengf66e4482017-10-18 13:00:26 -07001773#ifdef CONFIG_BPF_SYSCALL
1774 rc = bpf_fd_pass(file, cred_sid(cred));
1775 if (rc)
1776 return rc;
1777#endif
1778
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 /* av is zero if only checking access to the descriptor. */
David Howells88e67f32008-11-14 10:39:21 +11001780 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 if (av)
Linus Torvalds19e49832013-10-04 12:54:11 -07001782 rc = inode_has_perm(cred, inode, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
David Howells88e67f32008-11-14 10:39:21 +11001784out:
1785 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786}
1787
David Howellsc3c188b2015-07-10 17:19:58 -04001788/*
1789 * Determine the label for an inode that might be unioned.
1790 */
Vivek Goyalc957f6d2016-07-13 10:44:51 -04001791static int
1792selinux_determine_inode_label(const struct task_security_struct *tsec,
1793 struct inode *dir,
1794 const struct qstr *name, u16 tclass,
1795 u32 *_new_isid)
David Howellsc3c188b2015-07-10 17:19:58 -04001796{
Casey Schaufler1aea7802021-04-22 17:41:15 +02001797 const struct superblock_security_struct *sbsec =
1798 selinux_superblock(dir->i_sb);
David Howellsc3c188b2015-07-10 17:19:58 -04001799
1800 if ((sbsec->flags & SE_SBINITIALIZED) &&
1801 (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)) {
1802 *_new_isid = sbsec->mntpoint_sid;
1803 } else if ((sbsec->flags & SBLABEL_MNT) &&
1804 tsec->create_sid) {
1805 *_new_isid = tsec->create_sid;
1806 } else {
Paul Moore20cdef82016-04-04 14:14:42 -04001807 const struct inode_security_struct *dsec = inode_security(dir);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001808 return security_transition_sid(&selinux_state, tsec->sid,
1809 dsec->sid, tclass,
David Howellsc3c188b2015-07-10 17:19:58 -04001810 name, _new_isid);
1811 }
1812
1813 return 0;
1814}
1815
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816/* Check whether a task can create a file. */
1817static int may_create(struct inode *dir,
1818 struct dentry *dentry,
1819 u16 tclass)
1820{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07001821 const struct task_security_struct *tsec = selinux_cred(current_cred());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 struct inode_security_struct *dsec;
1823 struct superblock_security_struct *sbsec;
David Howells275bb412008-11-14 10:39:19 +11001824 u32 sid, newsid;
Thomas Liu2bf49692009-07-14 12:14:09 -04001825 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 int rc;
1827
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001828 dsec = inode_security(dir);
Casey Schaufler1aea7802021-04-22 17:41:15 +02001829 sbsec = selinux_superblock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
David Howells275bb412008-11-14 10:39:19 +11001831 sid = tsec->sid;
David Howells275bb412008-11-14 10:39:19 +11001832
Eric Paris50c205f2012-04-04 15:01:43 -04001833 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04001834 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001836 rc = avc_has_perm(&selinux_state,
1837 sid, dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 DIR__ADD_NAME | DIR__SEARCH,
1839 &ad);
1840 if (rc)
1841 return rc;
1842
Yang Guo210a2922019-12-12 10:02:24 +08001843 rc = selinux_determine_inode_label(tsec, dir, &dentry->d_name, tclass,
1844 &newsid);
David Howellsc3c188b2015-07-10 17:19:58 -04001845 if (rc)
1846 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001848 rc = avc_has_perm(&selinux_state,
1849 sid, newsid, tclass, FILE__CREATE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 if (rc)
1851 return rc;
1852
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001853 return avc_has_perm(&selinux_state,
1854 newsid, sbsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 SECCLASS_FILESYSTEM,
1856 FILESYSTEM__ASSOCIATE, &ad);
1857}
1858
Eric Paris828dfe12008-04-17 13:17:49 -04001859#define MAY_LINK 0
1860#define MAY_UNLINK 1
1861#define MAY_RMDIR 2
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
1863/* Check whether a task can link, unlink, or rmdir a file/directory. */
1864static int may_link(struct inode *dir,
1865 struct dentry *dentry,
1866 int kind)
1867
1868{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 struct inode_security_struct *dsec, *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001870 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11001871 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 u32 av;
1873 int rc;
1874
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001875 dsec = inode_security(dir);
1876 isec = backing_inode_security(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
Eric Paris50c205f2012-04-04 15:01:43 -04001878 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04001879 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
1881 av = DIR__SEARCH;
1882 av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001883 rc = avc_has_perm(&selinux_state,
1884 sid, dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 if (rc)
1886 return rc;
1887
1888 switch (kind) {
1889 case MAY_LINK:
1890 av = FILE__LINK;
1891 break;
1892 case MAY_UNLINK:
1893 av = FILE__UNLINK;
1894 break;
1895 case MAY_RMDIR:
1896 av = DIR__RMDIR;
1897 break;
1898 default:
peter enderborgc103a912018-06-12 10:09:03 +02001899 pr_warn("SELinux: %s: unrecognized kind %d\n",
Eric Paris744ba352008-04-17 11:52:44 -04001900 __func__, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 return 0;
1902 }
1903
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001904 rc = avc_has_perm(&selinux_state,
1905 sid, isec->sid, isec->sclass, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 return rc;
1907}
1908
1909static inline int may_rename(struct inode *old_dir,
1910 struct dentry *old_dentry,
1911 struct inode *new_dir,
1912 struct dentry *new_dentry)
1913{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001915 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11001916 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 u32 av;
1918 int old_is_dir, new_is_dir;
1919 int rc;
1920
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001921 old_dsec = inode_security(old_dir);
1922 old_isec = backing_inode_security(old_dentry);
David Howellse36cb0b2015-01-29 12:02:35 +00001923 old_is_dir = d_is_dir(old_dentry);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001924 new_dsec = inode_security(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
Eric Paris50c205f2012-04-04 15:01:43 -04001926 ad.type = LSM_AUDIT_DATA_DENTRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
Eric Parisa2694342011-04-25 13:10:27 -04001928 ad.u.dentry = old_dentry;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001929 rc = avc_has_perm(&selinux_state,
1930 sid, old_dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 DIR__REMOVE_NAME | DIR__SEARCH, &ad);
1932 if (rc)
1933 return rc;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001934 rc = avc_has_perm(&selinux_state,
1935 sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 old_isec->sclass, FILE__RENAME, &ad);
1937 if (rc)
1938 return rc;
1939 if (old_is_dir && new_dir != old_dir) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001940 rc = avc_has_perm(&selinux_state,
1941 sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 old_isec->sclass, DIR__REPARENT, &ad);
1943 if (rc)
1944 return rc;
1945 }
1946
Eric Parisa2694342011-04-25 13:10:27 -04001947 ad.u.dentry = new_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 av = DIR__ADD_NAME | DIR__SEARCH;
David Howells2c616d42015-01-29 12:02:33 +00001949 if (d_is_positive(new_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 av |= DIR__REMOVE_NAME;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001951 rc = avc_has_perm(&selinux_state,
1952 sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 if (rc)
1954 return rc;
David Howells2c616d42015-01-29 12:02:33 +00001955 if (d_is_positive(new_dentry)) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001956 new_isec = backing_inode_security(new_dentry);
David Howellse36cb0b2015-01-29 12:02:35 +00001957 new_is_dir = d_is_dir(new_dentry);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001958 rc = avc_has_perm(&selinux_state,
1959 sid, new_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 new_isec->sclass,
1961 (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
1962 if (rc)
1963 return rc;
1964 }
1965
1966 return 0;
1967}
1968
1969/* Check whether a task can perform a filesystem operation. */
David Howells88e67f32008-11-14 10:39:21 +11001970static int superblock_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 struct super_block *sb,
1972 u32 perms,
Thomas Liu2bf49692009-07-14 12:14:09 -04001973 struct common_audit_data *ad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 struct superblock_security_struct *sbsec;
David Howells88e67f32008-11-14 10:39:21 +11001976 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
Casey Schaufler1aea7802021-04-22 17:41:15 +02001978 sbsec = selinux_superblock(sb);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05001979 return avc_has_perm(&selinux_state,
1980 sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981}
1982
1983/* Convert a Linux mode and permission mask to an access vector. */
1984static inline u32 file_mask_to_av(int mode, int mask)
1985{
1986 u32 av = 0;
1987
Al Virodba19c62011-07-25 20:49:29 -04001988 if (!S_ISDIR(mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 if (mask & MAY_EXEC)
1990 av |= FILE__EXECUTE;
1991 if (mask & MAY_READ)
1992 av |= FILE__READ;
1993
1994 if (mask & MAY_APPEND)
1995 av |= FILE__APPEND;
1996 else if (mask & MAY_WRITE)
1997 av |= FILE__WRITE;
1998
1999 } else {
2000 if (mask & MAY_EXEC)
2001 av |= DIR__SEARCH;
2002 if (mask & MAY_WRITE)
2003 av |= DIR__WRITE;
2004 if (mask & MAY_READ)
2005 av |= DIR__READ;
2006 }
2007
2008 return av;
2009}
2010
2011/* Convert a Linux file to an access vector. */
2012static inline u32 file_to_av(struct file *file)
2013{
2014 u32 av = 0;
2015
2016 if (file->f_mode & FMODE_READ)
2017 av |= FILE__READ;
2018 if (file->f_mode & FMODE_WRITE) {
2019 if (file->f_flags & O_APPEND)
2020 av |= FILE__APPEND;
2021 else
2022 av |= FILE__WRITE;
2023 }
Stephen Smalley0794c662008-03-17 08:55:18 -04002024 if (!av) {
2025 /*
2026 * Special file opened with flags 3 for ioctl-only use.
2027 */
2028 av = FILE__IOCTL;
2029 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
2031 return av;
2032}
2033
Eric Paris8b6a5a32008-10-29 17:06:46 -04002034/*
Randy Dunlapc76a2f92020-08-07 09:51:34 -07002035 * Convert a file to an access vector and include the correct
Eric Paris8b6a5a32008-10-29 17:06:46 -04002036 * open permission.
2037 */
2038static inline u32 open_file_to_av(struct file *file)
2039{
2040 u32 av = file_to_av(file);
Stephen Smalleyccb54472017-05-12 12:41:24 -04002041 struct inode *inode = file_inode(file);
Eric Paris8b6a5a32008-10-29 17:06:46 -04002042
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002043 if (selinux_policycap_openperm() &&
2044 inode->i_sb->s_magic != SOCKFS_MAGIC)
Eric Paris49b7b8d2010-07-23 11:44:09 -04002045 av |= FILE__OPEN;
2046
Eric Paris8b6a5a32008-10-29 17:06:46 -04002047 return av;
2048}
2049
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050/* Hook functions begin here. */
2051
Todd Kjos52f88692021-10-12 09:56:13 -07002052static int selinux_binder_set_context_mgr(const struct cred *mgr)
Stephen Smalley79af7302015-01-21 10:54:10 -05002053{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002054 return avc_has_perm(&selinux_state,
Todd Kjos52f88692021-10-12 09:56:13 -07002055 current_sid(), cred_sid(mgr), SECCLASS_BINDER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002056 BINDER__SET_CONTEXT_MGR, NULL);
2057}
2058
Todd Kjos52f88692021-10-12 09:56:13 -07002059static int selinux_binder_transaction(const struct cred *from,
2060 const struct cred *to)
Stephen Smalley79af7302015-01-21 10:54:10 -05002061{
2062 u32 mysid = current_sid();
Todd Kjos52f88692021-10-12 09:56:13 -07002063 u32 fromsid = cred_sid(from);
2064 u32 tosid = cred_sid(to);
Stephen Smalley79af7302015-01-21 10:54:10 -05002065 int rc;
2066
2067 if (mysid != fromsid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002068 rc = avc_has_perm(&selinux_state,
2069 mysid, fromsid, SECCLASS_BINDER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002070 BINDER__IMPERSONATE, NULL);
2071 if (rc)
2072 return rc;
2073 }
2074
Todd Kjos52f88692021-10-12 09:56:13 -07002075 return avc_has_perm(&selinux_state, fromsid, tosid,
Paul Mooreeb1231f2021-02-18 15:13:40 -05002076 SECCLASS_BINDER, BINDER__CALL, NULL);
Stephen Smalley79af7302015-01-21 10:54:10 -05002077}
2078
Todd Kjos52f88692021-10-12 09:56:13 -07002079static int selinux_binder_transfer_binder(const struct cred *from,
2080 const struct cred *to)
Stephen Smalley79af7302015-01-21 10:54:10 -05002081{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002082 return avc_has_perm(&selinux_state,
Todd Kjos52f88692021-10-12 09:56:13 -07002083 cred_sid(from), cred_sid(to),
Paul Mooreeb1231f2021-02-18 15:13:40 -05002084 SECCLASS_BINDER, BINDER__TRANSFER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002085 NULL);
2086}
2087
Todd Kjos52f88692021-10-12 09:56:13 -07002088static int selinux_binder_transfer_file(const struct cred *from,
2089 const struct cred *to,
Stephen Smalley79af7302015-01-21 10:54:10 -05002090 struct file *file)
2091{
Todd Kjos52f88692021-10-12 09:56:13 -07002092 u32 sid = cred_sid(to);
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07002093 struct file_security_struct *fsec = selinux_file(file);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002094 struct dentry *dentry = file->f_path.dentry;
Paul Moore20cdef82016-04-04 14:14:42 -04002095 struct inode_security_struct *isec;
Stephen Smalley79af7302015-01-21 10:54:10 -05002096 struct common_audit_data ad;
2097 int rc;
2098
2099 ad.type = LSM_AUDIT_DATA_PATH;
2100 ad.u.path = file->f_path;
2101
2102 if (sid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002103 rc = avc_has_perm(&selinux_state,
2104 sid, fsec->sid,
Stephen Smalley79af7302015-01-21 10:54:10 -05002105 SECCLASS_FD,
2106 FD__USE,
2107 &ad);
2108 if (rc)
2109 return rc;
2110 }
2111
Chenbo Fengf66e4482017-10-18 13:00:26 -07002112#ifdef CONFIG_BPF_SYSCALL
2113 rc = bpf_fd_pass(file, sid);
2114 if (rc)
2115 return rc;
2116#endif
2117
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002118 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
Stephen Smalley79af7302015-01-21 10:54:10 -05002119 return 0;
2120
Paul Moore20cdef82016-04-04 14:14:42 -04002121 isec = backing_inode_security(dentry);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002122 return avc_has_perm(&selinux_state,
2123 sid, isec->sid, isec->sclass, file_to_av(file),
Stephen Smalley79af7302015-01-21 10:54:10 -05002124 &ad);
2125}
2126
Ingo Molnar9e488582009-05-07 19:26:19 +10002127static int selinux_ptrace_access_check(struct task_struct *child,
Paul Mooreeb1231f2021-02-18 15:13:40 -05002128 unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002130 u32 sid = current_sid();
Paul Mooreeb1231f2021-02-18 15:13:40 -05002131 u32 csid = task_sid_obj(child);
Stephen Smalley006ebb42008-05-19 08:32:49 -04002132
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002133 if (mode & PTRACE_MODE_READ)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002134 return avc_has_perm(&selinux_state,
2135 sid, csid, SECCLASS_FILE, FILE__READ, NULL);
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002136
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002137 return avc_has_perm(&selinux_state,
2138 sid, csid, SECCLASS_PROCESS, PROCESS__PTRACE, NULL);
David Howells5cd9c582008-08-14 11:37:28 +01002139}
2140
2141static int selinux_ptrace_traceme(struct task_struct *parent)
2142{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002143 return avc_has_perm(&selinux_state,
Paul Moorea3727a82021-09-23 09:50:11 -04002144 task_sid_obj(parent), task_sid_obj(current),
Paul Mooreeb1231f2021-02-18 15:13:40 -05002145 SECCLASS_PROCESS, PROCESS__PTRACE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146}
2147
2148static int selinux_capget(struct task_struct *target, kernel_cap_t *effective,
Eric Paris828dfe12008-04-17 13:17:49 -04002149 kernel_cap_t *inheritable, kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002151 return avc_has_perm(&selinux_state,
Paul Mooreeb1231f2021-02-18 15:13:40 -05002152 current_sid(), task_sid_obj(target), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002153 PROCESS__GETCAP, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154}
2155
David Howellsd84f4f92008-11-14 10:39:23 +11002156static int selinux_capset(struct cred *new, const struct cred *old,
2157 const kernel_cap_t *effective,
2158 const kernel_cap_t *inheritable,
2159 const kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002161 return avc_has_perm(&selinux_state,
2162 cred_sid(old), cred_sid(new), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002163 PROCESS__SETCAP, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164}
2165
James Morris5626d3e2009-01-30 10:05:06 +11002166/*
2167 * (This comment used to live with the selinux_task_setuid hook,
2168 * which was removed).
2169 *
2170 * Since setuid only affects the current process, and since the SELinux
2171 * controls are not based on the Linux identity attributes, SELinux does not
2172 * need to control this operation. However, SELinux does control the use of
2173 * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
2174 */
2175
Eric Paris6a9de492012-01-03 12:25:14 -05002176static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
Micah Mortonc1a85a02019-01-07 16:10:53 -08002177 int cap, unsigned int opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178{
Micah Mortonc1a85a02019-01-07 16:10:53 -08002179 return cred_has_capability(cred, cap, opts, ns == &init_user_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180}
2181
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182static int selinux_quotactl(int cmds, int type, int id, struct super_block *sb)
2183{
David Howells88e67f32008-11-14 10:39:21 +11002184 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 int rc = 0;
2186
2187 if (!sb)
2188 return 0;
2189
2190 switch (cmds) {
Eric Paris828dfe12008-04-17 13:17:49 -04002191 case Q_SYNC:
2192 case Q_QUOTAON:
2193 case Q_QUOTAOFF:
2194 case Q_SETINFO:
2195 case Q_SETQUOTA:
Richard Hainese4cfa052020-02-20 15:32:34 +00002196 case Q_XQUOTAOFF:
2197 case Q_XQUOTAON:
2198 case Q_XSETQLIM:
David Howells88e67f32008-11-14 10:39:21 +11002199 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04002200 break;
2201 case Q_GETFMT:
2202 case Q_GETINFO:
2203 case Q_GETQUOTA:
Richard Hainese4cfa052020-02-20 15:32:34 +00002204 case Q_XGETQUOTA:
2205 case Q_XGETQSTAT:
2206 case Q_XGETQSTATV:
2207 case Q_XGETNEXTQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11002208 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04002209 break;
2210 default:
2211 rc = 0; /* let the kernel handle invalid cmds */
2212 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 }
2214 return rc;
2215}
2216
2217static int selinux_quota_on(struct dentry *dentry)
2218{
David Howells88e67f32008-11-14 10:39:21 +11002219 const struct cred *cred = current_cred();
2220
Eric Paris2875fa02011-04-28 16:04:24 -04002221 return dentry_has_perm(cred, dentry, FILE__QUOTAON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222}
2223
Eric Paris12b30522010-11-15 18:36:29 -05002224static int selinux_syslog(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 switch (type) {
Kees Cookd78ca3c2010-02-03 15:37:13 -08002227 case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */
2228 case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002229 return avc_has_perm(&selinux_state,
2230 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002231 SECCLASS_SYSTEM, SYSTEM__SYSLOG_READ, NULL);
Kees Cookd78ca3c2010-02-03 15:37:13 -08002232 case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */
2233 case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */
2234 /* Set level of messages printed to console */
2235 case SYSLOG_ACTION_CONSOLE_LEVEL:
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002236 return avc_has_perm(&selinux_state,
2237 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002238 SECCLASS_SYSTEM, SYSTEM__SYSLOG_CONSOLE,
2239 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 }
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002241 /* All other syslog types */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002242 return avc_has_perm(&selinux_state,
2243 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002244 SECCLASS_SYSTEM, SYSTEM__SYSLOG_MOD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245}
2246
2247/*
2248 * Check that a process has enough memory to allocate a new virtual
2249 * mapping. 0 means there is enough memory for the allocation to
2250 * succeed and -ENOMEM implies there is not.
2251 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 * Do not audit the selinux permission check, as this is applied to all
2253 * processes that allocate mappings.
2254 */
Alan Cox34b4e4a2007-08-22 14:01:28 -07002255static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256{
2257 int rc, cap_sys_admin = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002259 rc = cred_has_capability(current_cred(), CAP_SYS_ADMIN,
Micah Mortonc1a85a02019-01-07 16:10:53 -08002260 CAP_OPT_NOAUDIT, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 if (rc == 0)
2262 cap_sys_admin = 1;
2263
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002264 return cap_sys_admin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265}
2266
2267/* binprm security operations */
2268
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002269static u32 ptrace_parent_sid(void)
Paul Moore0c6181c2016-03-30 21:41:21 -04002270{
2271 u32 sid = 0;
2272 struct task_struct *tracer;
2273
2274 rcu_read_lock();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002275 tracer = ptrace_parent(current);
Paul Moore0c6181c2016-03-30 21:41:21 -04002276 if (tracer)
Paul Mooreeb1231f2021-02-18 15:13:40 -05002277 sid = task_sid_obj(tracer);
Paul Moore0c6181c2016-03-30 21:41:21 -04002278 rcu_read_unlock();
2279
2280 return sid;
2281}
2282
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002283static int check_nnp_nosuid(const struct linux_binprm *bprm,
2284 const struct task_security_struct *old_tsec,
2285 const struct task_security_struct *new_tsec)
2286{
2287 int nnp = (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS);
Andy Lutomirski380cf5b2016-06-23 16:41:05 -05002288 int nosuid = !mnt_may_suid(bprm->file->f_path.mnt);
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002289 int rc;
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002290 u32 av;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002291
2292 if (!nnp && !nosuid)
2293 return 0; /* neither NNP nor nosuid */
2294
2295 if (new_tsec->sid == old_tsec->sid)
2296 return 0; /* No change in credentials */
2297
2298 /*
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002299 * If the policy enables the nnp_nosuid_transition policy capability,
2300 * then we permit transitions under NNP or nosuid if the
2301 * policy allows the corresponding permission between
2302 * the old and new contexts.
2303 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002304 if (selinux_policycap_nnp_nosuid_transition()) {
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002305 av = 0;
2306 if (nnp)
2307 av |= PROCESS2__NNP_TRANSITION;
2308 if (nosuid)
2309 av |= PROCESS2__NOSUID_TRANSITION;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002310 rc = avc_has_perm(&selinux_state,
2311 old_tsec->sid, new_tsec->sid,
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002312 SECCLASS_PROCESS2, av, NULL);
2313 if (!rc)
2314 return 0;
2315 }
2316
2317 /*
2318 * We also permit NNP or nosuid transitions to bounded SIDs,
2319 * i.e. SIDs that are guaranteed to only be allowed a subset
2320 * of the permissions of the current SID.
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002321 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002322 rc = security_bounded_transition(&selinux_state, old_tsec->sid,
2323 new_tsec->sid);
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002324 if (!rc)
2325 return 0;
2326
2327 /*
2328 * On failure, preserve the errno values for NNP vs nosuid.
2329 * NNP: Operation not permitted for caller.
2330 * nosuid: Permission denied to file.
2331 */
2332 if (nnp)
2333 return -EPERM;
2334 return -EACCES;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002335}
2336
Eric W. Biedermanb8bff592020-03-22 15:46:24 -05002337static int selinux_bprm_creds_for_exec(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338{
David Howellsa6f76f22008-11-14 10:39:24 +11002339 const struct task_security_struct *old_tsec;
2340 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 struct inode_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04002342 struct common_audit_data ad;
Al Viro496ad9a2013-01-23 17:07:38 -05002343 struct inode *inode = file_inode(bprm->file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 int rc;
2345
David Howellsa6f76f22008-11-14 10:39:24 +11002346 /* SELinux context only depends on initial program or script and not
2347 * the script interpreter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002349 old_tsec = selinux_cred(current_cred());
2350 new_tsec = selinux_cred(bprm->cred);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002351 isec = inode_security(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
2353 /* Default to the current task SID. */
David Howellsa6f76f22008-11-14 10:39:24 +11002354 new_tsec->sid = old_tsec->sid;
2355 new_tsec->osid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356
Michael LeMay28eba5b2006-06-27 02:53:42 -07002357 /* Reset fs, key, and sock SIDs on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002358 new_tsec->create_sid = 0;
2359 new_tsec->keycreate_sid = 0;
2360 new_tsec->sockcreate_sid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
David Howellsa6f76f22008-11-14 10:39:24 +11002362 if (old_tsec->exec_sid) {
2363 new_tsec->sid = old_tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 /* Reset exec SID on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002365 new_tsec->exec_sid = 0;
Andy Lutomirski259e5e62012-04-12 16:47:50 -05002366
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002367 /* Fail on NNP or nosuid if not an allowed transition. */
2368 rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
2369 if (rc)
2370 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 } else {
2372 /* Check for a default transition on this program. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002373 rc = security_transition_sid(&selinux_state, old_tsec->sid,
2374 isec->sid, SECCLASS_PROCESS, NULL,
Eric Paris652bb9b2011-02-01 11:05:40 -05002375 &new_tsec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 if (rc)
2377 return rc;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002378
2379 /*
2380 * Fallback to old SID on NNP or nosuid if not an allowed
2381 * transition.
2382 */
2383 rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
2384 if (rc)
2385 new_tsec->sid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 }
2387
Vivek Goyal43af5de2016-09-09 11:37:49 -04002388 ad.type = LSM_AUDIT_DATA_FILE;
2389 ad.u.file = bprm->file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
David Howellsa6f76f22008-11-14 10:39:24 +11002391 if (new_tsec->sid == old_tsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002392 rc = avc_has_perm(&selinux_state,
2393 old_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
2395 if (rc)
2396 return rc;
2397 } else {
2398 /* Check permissions for the transition. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002399 rc = avc_has_perm(&selinux_state,
2400 old_tsec->sid, new_tsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
2402 if (rc)
2403 return rc;
2404
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002405 rc = avc_has_perm(&selinux_state,
2406 new_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
2408 if (rc)
2409 return rc;
2410
David Howellsa6f76f22008-11-14 10:39:24 +11002411 /* Check for shared state */
2412 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002413 rc = avc_has_perm(&selinux_state,
2414 old_tsec->sid, new_tsec->sid,
David Howellsa6f76f22008-11-14 10:39:24 +11002415 SECCLASS_PROCESS, PROCESS__SHARE,
2416 NULL);
2417 if (rc)
2418 return -EPERM;
2419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420
David Howellsa6f76f22008-11-14 10:39:24 +11002421 /* Make sure that anyone attempting to ptrace over a task that
2422 * changes its SID has the appropriate permit */
Eric W. Biederman9227dd22017-01-23 17:26:31 +13002423 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002424 u32 ptsid = ptrace_parent_sid();
David Howellsa6f76f22008-11-14 10:39:24 +11002425 if (ptsid != 0) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002426 rc = avc_has_perm(&selinux_state,
2427 ptsid, new_tsec->sid,
David Howellsa6f76f22008-11-14 10:39:24 +11002428 SECCLASS_PROCESS,
2429 PROCESS__PTRACE, NULL);
2430 if (rc)
2431 return -EPERM;
2432 }
2433 }
2434
2435 /* Clear any possibly unsafe personality bits on exec: */
2436 bprm->per_clear |= PER_CLEAR_ON_SETID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 /* Enable secure mode for SIDs transitions unless
2439 the noatsecure permission is granted between
2440 the two SIDs, i.e. ahp returns 0. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002441 rc = avc_has_perm(&selinux_state,
2442 old_tsec->sid, new_tsec->sid,
Kees Cook62874c32017-07-18 15:25:25 -07002443 SECCLASS_PROCESS, PROCESS__NOATSECURE,
2444 NULL);
2445 bprm->secureexec |= !!rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 }
2447
Kees Cook62874c32017-07-18 15:25:25 -07002448 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449}
2450
Al Viroc3c073f2012-08-21 22:32:06 -04002451static int match_file(const void *p, struct file *file, unsigned fd)
2452{
2453 return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0;
2454}
2455
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456/* Derived from fs/exec.c:flush_old_files. */
David Howells745ca242008-11-14 10:39:22 +11002457static inline void flush_unauthorized_files(const struct cred *cred,
2458 struct files_struct *files)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 struct file *file, *devnull = NULL;
Stephen Smalleyb20c8122006-09-25 23:32:03 -07002461 struct tty_struct *tty;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002462 int drop_tty = 0;
Al Viroc3c073f2012-08-21 22:32:06 -04002463 unsigned n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002465 tty = get_current_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 if (tty) {
Peter Hurley4a510962016-01-09 21:35:23 -08002467 spin_lock(&tty->files_lock);
Eric Paris37dd0bd2008-10-31 17:40:00 -04002468 if (!list_empty(&tty->tty_files)) {
Nick Piggind996b622010-08-18 04:37:36 +10002469 struct tty_file_private *file_priv;
Eric Paris37dd0bd2008-10-31 17:40:00 -04002470
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 /* Revalidate access to controlling tty.
David Howells13f8e982013-06-13 23:37:55 +01002472 Use file_path_has_perm on the tty path directly
2473 rather than using file_has_perm, as this particular
2474 open file may belong to another process and we are
2475 only interested in the inode-based check here. */
Nick Piggind996b622010-08-18 04:37:36 +10002476 file_priv = list_first_entry(&tty->tty_files,
2477 struct tty_file_private, list);
2478 file = file_priv->file;
David Howells13f8e982013-06-13 23:37:55 +01002479 if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE))
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002480 drop_tty = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 }
Peter Hurley4a510962016-01-09 21:35:23 -08002482 spin_unlock(&tty->files_lock);
Alan Cox452a00d2008-10-13 10:39:13 +01002483 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 }
Eric W. Biederman98a27ba2007-05-08 00:26:56 -07002485 /* Reset controlling tty. */
2486 if (drop_tty)
2487 no_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
2489 /* Revalidate access to inherited open files. */
Al Viroc3c073f2012-08-21 22:32:06 -04002490 n = iterate_fd(files, 0, match_file, cred);
2491 if (!n) /* none found? */
2492 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
Al Viroc3c073f2012-08-21 22:32:06 -04002494 devnull = dentry_open(&selinux_null, O_RDWR, cred);
Al Viro45525b22012-10-16 13:30:07 -04002495 if (IS_ERR(devnull))
2496 devnull = NULL;
2497 /* replace all the matching ones with this */
2498 do {
2499 replace_fd(n - 1, devnull, 0);
2500 } while ((n = iterate_fd(files, n, match_file, cred)) != 0);
2501 if (devnull)
Al Viroc3c073f2012-08-21 22:32:06 -04002502 fput(devnull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503}
2504
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505/*
David Howellsa6f76f22008-11-14 10:39:24 +11002506 * Prepare a process for imminent new credential changes due to exec
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 */
David Howellsa6f76f22008-11-14 10:39:24 +11002508static void selinux_bprm_committing_creds(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509{
David Howellsa6f76f22008-11-14 10:39:24 +11002510 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 struct rlimit *rlim, *initrlim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 int rc, i;
2513
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002514 new_tsec = selinux_cred(bprm->cred);
David Howellsa6f76f22008-11-14 10:39:24 +11002515 if (new_tsec->sid == new_tsec->osid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 return;
2517
2518 /* Close files for which the new task SID is not authorized. */
David Howellsa6f76f22008-11-14 10:39:24 +11002519 flush_unauthorized_files(bprm->cred, current->files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
David Howellsa6f76f22008-11-14 10:39:24 +11002521 /* Always clear parent death signal on SID transitions. */
2522 current->pdeath_signal = 0;
2523
2524 /* Check whether the new SID can inherit resource limits from the old
2525 * SID. If not, reset all soft limits to the lower of the current
2526 * task's hard limit and the init task's soft limit.
2527 *
2528 * Note that the setting of hard limits (even to lower them) can be
2529 * controlled by the setrlimit check. The inclusion of the init task's
2530 * soft limit into the computation is to avoid resetting soft limits
2531 * higher than the default soft limit for cases where the default is
2532 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2533 */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002534 rc = avc_has_perm(&selinux_state,
2535 new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
David Howellsa6f76f22008-11-14 10:39:24 +11002536 PROCESS__RLIMITINH, NULL);
2537 if (rc) {
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002538 /* protect against do_prlimit() */
2539 task_lock(current);
David Howellsa6f76f22008-11-14 10:39:24 +11002540 for (i = 0; i < RLIM_NLIMITS; i++) {
2541 rlim = current->signal->rlim + i;
2542 initrlim = init_task.signal->rlim + i;
2543 rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
2544 }
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002545 task_unlock(current);
Nicolas Pitrebaa73d92016-11-11 00:10:10 -05002546 if (IS_ENABLED(CONFIG_POSIX_TIMERS))
2547 update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
David Howellsa6f76f22008-11-14 10:39:24 +11002548 }
2549}
2550
2551/*
2552 * Clean up the process immediately after the installation of new credentials
2553 * due to exec
2554 */
2555static void selinux_bprm_committed_creds(struct linux_binprm *bprm)
2556{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002557 const struct task_security_struct *tsec = selinux_cred(current_cred());
David Howellsa6f76f22008-11-14 10:39:24 +11002558 u32 osid, sid;
Arnd Bergmannddbc7d02019-10-25 21:37:43 +02002559 int rc;
David Howellsa6f76f22008-11-14 10:39:24 +11002560
David Howellsa6f76f22008-11-14 10:39:24 +11002561 osid = tsec->osid;
2562 sid = tsec->sid;
2563
2564 if (sid == osid)
2565 return;
2566
2567 /* Check whether the new SID can inherit signal state from the old SID.
2568 * If not, clear itimers to avoid subsequent signal generation and
2569 * flush and unblock signals.
2570 *
2571 * This must occur _after_ the task SID has been updated so that any
2572 * kill done after the flush will be checked against the new SID.
2573 */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05002574 rc = avc_has_perm(&selinux_state,
2575 osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 if (rc) {
Arnd Bergmannddbc7d02019-10-25 21:37:43 +02002577 clear_itimer();
2578
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterov9e7c8f82015-06-04 16:22:16 -04002580 if (!fatal_signal_pending(current)) {
2581 flush_sigqueue(&current->pending);
2582 flush_sigqueue(&current->signal->shared_pending);
David Howells3bcac022009-04-29 13:45:05 +01002583 flush_signal_handlers(current, 1);
2584 sigemptyset(&current->blocked);
Oleg Nesterov9e7c8f82015-06-04 16:22:16 -04002585 recalc_sigpending();
David Howells3bcac022009-04-29 13:45:05 +01002586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 spin_unlock_irq(&current->sighand->siglock);
2588 }
2589
David Howellsa6f76f22008-11-14 10:39:24 +11002590 /* Wake up the parent if it is waiting so that it can recheck
2591 * wait permission to the new task SID. */
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002592 read_lock(&tasklist_lock);
Oleg Nesterov0b7570e2009-09-23 15:56:46 -07002593 __wake_up_parent(current, current->real_parent);
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002594 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595}
2596
2597/* superblock security operations */
2598
2599static int selinux_sb_alloc_security(struct super_block *sb)
2600{
Casey Schaufler1aea7802021-04-22 17:41:15 +02002601 struct superblock_security_struct *sbsec = selinux_superblock(sb);
Paul Moorecb89e242020-01-10 16:32:10 -05002602
2603 mutex_init(&sbsec->lock);
2604 INIT_LIST_HEAD(&sbsec->isec_head);
2605 spin_lock_init(&sbsec->isec_lock);
Paul Moorecb89e242020-01-10 16:32:10 -05002606 sbsec->sid = SECINITSID_UNLABELED;
2607 sbsec->def_sid = SECINITSID_FILE;
2608 sbsec->mntpoint_sid = SECINITSID_UNLABELED;
Paul Moorecb89e242020-01-10 16:32:10 -05002609
2610 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611}
2612
Al Viro99dbbb52018-12-14 21:56:23 -05002613static inline int opt_len(const char *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614{
Al Viro99dbbb52018-12-14 21:56:23 -05002615 bool open_quote = false;
2616 int len;
2617 char c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618
Al Viro99dbbb52018-12-14 21:56:23 -05002619 for (len = 0; (c = s[len]) != '\0'; len++) {
2620 if (c == '"')
Cory Olmo3528a952006-09-29 01:58:44 -07002621 open_quote = !open_quote;
Al Viro99dbbb52018-12-14 21:56:23 -05002622 if (c == ',' && !open_quote)
2623 break;
2624 }
2625 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626}
2627
Al Viro204cc0c2018-12-13 13:41:47 -05002628static int selinux_sb_eat_lsm_opts(char *options, void **mnt_opts)
Eric Paris026eb162011-03-03 16:09:14 -05002629{
Al Viro99dbbb52018-12-14 21:56:23 -05002630 char *from = options;
2631 char *to = options;
2632 bool first = true;
Gen Zhangfec63752019-06-12 21:55:38 +08002633 int rc;
Al Viro5b400232018-12-12 20:13:29 -05002634
Al Viro99dbbb52018-12-14 21:56:23 -05002635 while (1) {
2636 int len = opt_len(from);
Gen Zhangfec63752019-06-12 21:55:38 +08002637 int token;
Al Viro99dbbb52018-12-14 21:56:23 -05002638 char *arg = NULL;
2639
2640 token = match_opt_prefix(from, len, &arg);
2641
2642 if (token != Opt_error) {
2643 char *p, *q;
2644
2645 /* strip quotes */
2646 if (arg) {
2647 for (p = q = arg; p < from + len; p++) {
2648 char c = *p;
2649 if (c != '"')
2650 *q++ = c;
2651 }
2652 arg = kmemdup_nul(arg, q - arg, GFP_KERNEL);
Gen Zhangfec63752019-06-12 21:55:38 +08002653 if (!arg) {
2654 rc = -ENOMEM;
2655 goto free_opt;
2656 }
Al Viro99dbbb52018-12-14 21:56:23 -05002657 }
2658 rc = selinux_add_opt(token, arg, mnt_opts);
2659 if (unlikely(rc)) {
2660 kfree(arg);
Gen Zhangfec63752019-06-12 21:55:38 +08002661 goto free_opt;
Al Viro99dbbb52018-12-14 21:56:23 -05002662 }
2663 } else {
2664 if (!first) { // copy with preceding comma
2665 from--;
2666 len++;
2667 }
2668 if (to != from)
2669 memmove(to, from, len);
2670 to += len;
2671 first = false;
2672 }
2673 if (!from[len])
2674 break;
2675 from += len + 1;
2676 }
2677 *to = '\0';
2678 return 0;
Gen Zhangfec63752019-06-12 21:55:38 +08002679
2680free_opt:
2681 if (*mnt_opts) {
2682 selinux_free_mnt_opts(*mnt_opts);
2683 *mnt_opts = NULL;
2684 }
2685 return rc;
Al Viro5b400232018-12-12 20:13:29 -05002686}
2687
Olga Kornievskaia69c4a422021-02-26 22:37:55 -05002688static int selinux_sb_mnt_opts_compat(struct super_block *sb, void *mnt_opts)
2689{
2690 struct selinux_mnt_opts *opts = mnt_opts;
2691 struct superblock_security_struct *sbsec = sb->s_security;
2692 u32 sid;
2693 int rc;
2694
2695 /*
2696 * Superblock not initialized (i.e. no options) - reject if any
2697 * options specified, otherwise accept.
2698 */
2699 if (!(sbsec->flags & SE_SBINITIALIZED))
2700 return opts ? 1 : 0;
2701
2702 /*
2703 * Superblock initialized and no options specified - reject if
2704 * superblock has any options set, otherwise accept.
2705 */
2706 if (!opts)
2707 return (sbsec->flags & SE_MNTMASK) ? 1 : 0;
2708
2709 if (opts->fscontext) {
Scott Mayhewcc274ae2021-12-15 16:28:40 -05002710 rc = parse_sid(sb, opts->fscontext, &sid, GFP_NOWAIT);
Olga Kornievskaia69c4a422021-02-26 22:37:55 -05002711 if (rc)
2712 return 1;
2713 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
2714 return 1;
2715 }
2716 if (opts->context) {
Scott Mayhewcc274ae2021-12-15 16:28:40 -05002717 rc = parse_sid(sb, opts->context, &sid, GFP_NOWAIT);
Olga Kornievskaia69c4a422021-02-26 22:37:55 -05002718 if (rc)
2719 return 1;
2720 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
2721 return 1;
2722 }
2723 if (opts->rootcontext) {
2724 struct inode_security_struct *root_isec;
2725
2726 root_isec = backing_inode_security(sb->s_root);
Scott Mayhewcc274ae2021-12-15 16:28:40 -05002727 rc = parse_sid(sb, opts->rootcontext, &sid, GFP_NOWAIT);
Olga Kornievskaia69c4a422021-02-26 22:37:55 -05002728 if (rc)
2729 return 1;
2730 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
2731 return 1;
2732 }
2733 if (opts->defcontext) {
Scott Mayhewcc274ae2021-12-15 16:28:40 -05002734 rc = parse_sid(sb, opts->defcontext, &sid, GFP_NOWAIT);
Olga Kornievskaia69c4a422021-02-26 22:37:55 -05002735 if (rc)
2736 return 1;
2737 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
2738 return 1;
2739 }
2740 return 0;
2741}
2742
Al Viro204cc0c2018-12-13 13:41:47 -05002743static int selinux_sb_remount(struct super_block *sb, void *mnt_opts)
Eric Paris026eb162011-03-03 16:09:14 -05002744{
Al Virobd323652018-12-13 15:04:59 -05002745 struct selinux_mnt_opts *opts = mnt_opts;
Casey Schaufler1aea7802021-04-22 17:41:15 +02002746 struct superblock_security_struct *sbsec = selinux_superblock(sb);
Al Virobd323652018-12-13 15:04:59 -05002747 u32 sid;
2748 int rc;
Eric Paris026eb162011-03-03 16:09:14 -05002749
2750 if (!(sbsec->flags & SE_SBINITIALIZED))
2751 return 0;
2752
Al Viro204cc0c2018-12-13 13:41:47 -05002753 if (!opts)
Eric Paris026eb162011-03-03 16:09:14 -05002754 return 0;
2755
Al Virobd323652018-12-13 15:04:59 -05002756 if (opts->fscontext) {
Scott Mayhewcc274ae2021-12-15 16:28:40 -05002757 rc = parse_sid(sb, opts->fscontext, &sid, GFP_KERNEL);
Al Virobd323652018-12-13 15:04:59 -05002758 if (rc)
Al Viroc039bc32018-12-01 23:06:57 -05002759 return rc;
Al Virobd323652018-12-13 15:04:59 -05002760 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid, sid))
2761 goto out_bad_option;
Eric Paris026eb162011-03-03 16:09:14 -05002762 }
Al Virobd323652018-12-13 15:04:59 -05002763 if (opts->context) {
Scott Mayhewcc274ae2021-12-15 16:28:40 -05002764 rc = parse_sid(sb, opts->context, &sid, GFP_KERNEL);
Al Virobd323652018-12-13 15:04:59 -05002765 if (rc)
2766 return rc;
2767 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid, sid))
2768 goto out_bad_option;
2769 }
2770 if (opts->rootcontext) {
2771 struct inode_security_struct *root_isec;
2772 root_isec = backing_inode_security(sb->s_root);
Scott Mayhewcc274ae2021-12-15 16:28:40 -05002773 rc = parse_sid(sb, opts->rootcontext, &sid, GFP_KERNEL);
Al Virobd323652018-12-13 15:04:59 -05002774 if (rc)
2775 return rc;
2776 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid, sid))
2777 goto out_bad_option;
2778 }
2779 if (opts->defcontext) {
Scott Mayhewcc274ae2021-12-15 16:28:40 -05002780 rc = parse_sid(sb, opts->defcontext, &sid, GFP_KERNEL);
Al Virobd323652018-12-13 15:04:59 -05002781 if (rc)
2782 return rc;
2783 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid, sid))
2784 goto out_bad_option;
Eric Paris026eb162011-03-03 16:09:14 -05002785 }
Al Viroc039bc32018-12-01 23:06:57 -05002786 return 0;
Eric Paris026eb162011-03-03 16:09:14 -05002787
Eric Paris026eb162011-03-03 16:09:14 -05002788out_bad_option:
peter enderborgc103a912018-06-12 10:09:03 +02002789 pr_warn("SELinux: unable to change security options "
Linus Torvalds29b1deb2013-12-15 11:17:45 -08002790 "during remount (dev %s, type=%s)\n", sb->s_id,
2791 sb->s_type->name);
Al Viroc039bc32018-12-01 23:06:57 -05002792 return -EINVAL;
Eric Paris026eb162011-03-03 16:09:14 -05002793}
2794
Al Viroa10d7c22018-12-05 11:58:35 -05002795static int selinux_sb_kern_mount(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796{
David Howells88e67f32008-11-14 10:39:21 +11002797 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002798 struct common_audit_data ad;
James Morris74192242008-12-19 11:41:10 +11002799
Eric Paris50c205f2012-04-04 15:01:43 -04002800 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002801 ad.u.dentry = sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002802 return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803}
2804
David Howells726c3342006-06-23 02:02:58 -07002805static int selinux_sb_statfs(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806{
David Howells88e67f32008-11-14 10:39:21 +11002807 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002808 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809
Eric Paris50c205f2012-04-04 15:01:43 -04002810 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002811 ad.u.dentry = dentry->d_sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002812 return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813}
2814
Al Viro808d4e32012-10-11 11:42:01 -04002815static int selinux_mount(const char *dev_name,
Al Viro8a04c432016-03-25 14:52:53 -04002816 const struct path *path,
Al Viro808d4e32012-10-11 11:42:01 -04002817 const char *type,
Eric Paris828dfe12008-04-17 13:17:49 -04002818 unsigned long flags,
2819 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820{
David Howells88e67f32008-11-14 10:39:21 +11002821 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822
2823 if (flags & MS_REMOUNT)
Al Virod8c95842011-12-07 18:16:57 -05002824 return superblock_has_perm(cred, path->dentry->d_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002825 FILESYSTEM__REMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 else
Eric Paris2875fa02011-04-28 16:04:24 -04002827 return path_has_perm(cred, path, FILE__MOUNTON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828}
2829
Stephen Smalley98aa0032020-01-17 15:24:07 -05002830static int selinux_move_mount(const struct path *from_path,
2831 const struct path *to_path)
2832{
2833 const struct cred *cred = current_cred();
2834
2835 return path_has_perm(cred, to_path, FILE__MOUNTON);
2836}
2837
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838static int selinux_umount(struct vfsmount *mnt, int flags)
2839{
David Howells88e67f32008-11-14 10:39:21 +11002840 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841
David Howells88e67f32008-11-14 10:39:21 +11002842 return superblock_has_perm(cred, mnt->mnt_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002843 FILESYSTEM__UNMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844}
2845
Al Viro0b520752018-12-23 16:02:47 -05002846static int selinux_fs_context_dup(struct fs_context *fc,
2847 struct fs_context *src_fc)
2848{
2849 const struct selinux_mnt_opts *src = src_fc->security;
2850 struct selinux_mnt_opts *opts;
2851
2852 if (!src)
2853 return 0;
2854
2855 fc->security = kzalloc(sizeof(struct selinux_mnt_opts), GFP_KERNEL);
2856 if (!fc->security)
2857 return -ENOMEM;
2858
2859 opts = fc->security;
2860
2861 if (src->fscontext) {
2862 opts->fscontext = kstrdup(src->fscontext, GFP_KERNEL);
2863 if (!opts->fscontext)
2864 return -ENOMEM;
2865 }
2866 if (src->context) {
2867 opts->context = kstrdup(src->context, GFP_KERNEL);
2868 if (!opts->context)
2869 return -ENOMEM;
2870 }
2871 if (src->rootcontext) {
2872 opts->rootcontext = kstrdup(src->rootcontext, GFP_KERNEL);
2873 if (!opts->rootcontext)
2874 return -ENOMEM;
2875 }
2876 if (src->defcontext) {
2877 opts->defcontext = kstrdup(src->defcontext, GFP_KERNEL);
2878 if (!opts->defcontext)
2879 return -ENOMEM;
2880 }
2881 return 0;
2882}
2883
Al Virod7167b12019-09-07 07:23:15 -04002884static const struct fs_parameter_spec selinux_fs_parameters[] = {
David Howells442155c2018-11-01 23:07:24 +00002885 fsparam_string(CONTEXT_STR, Opt_context),
2886 fsparam_string(DEFCONTEXT_STR, Opt_defcontext),
2887 fsparam_string(FSCONTEXT_STR, Opt_fscontext),
2888 fsparam_string(ROOTCONTEXT_STR, Opt_rootcontext),
2889 fsparam_flag (SECLABEL_STR, Opt_seclabel),
2890 {}
2891};
2892
David Howells442155c2018-11-01 23:07:24 +00002893static int selinux_fs_context_parse_param(struct fs_context *fc,
2894 struct fs_parameter *param)
2895{
2896 struct fs_parse_result result;
2897 int opt, rc;
2898
Al Virod7167b12019-09-07 07:23:15 -04002899 opt = fs_parse(fc, selinux_fs_parameters, param, &result);
David Howells442155c2018-11-01 23:07:24 +00002900 if (opt < 0)
2901 return opt;
2902
2903 rc = selinux_add_opt(opt, param->string, &fc->security);
2904 if (!rc) {
2905 param->string = NULL;
2906 rc = 1;
2907 }
2908 return rc;
2909}
2910
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911/* inode security operations */
2912
2913static int selinux_inode_alloc_security(struct inode *inode)
2914{
Paul Moorecb89e242020-01-10 16:32:10 -05002915 struct inode_security_struct *isec = selinux_inode(inode);
2916 u32 sid = current_sid();
2917
2918 spin_lock_init(&isec->lock);
2919 INIT_LIST_HEAD(&isec->list);
2920 isec->inode = inode;
2921 isec->sid = SECINITSID_UNLABELED;
2922 isec->sclass = SECCLASS_FILE;
2923 isec->task_sid = sid;
2924 isec->initialized = LABEL_INVALID;
2925
2926 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927}
2928
2929static void selinux_inode_free_security(struct inode *inode)
2930{
2931 inode_free_security(inode);
2932}
2933
David Quigleyd47be3d2013-05-22 12:50:34 -04002934static int selinux_dentry_init_security(struct dentry *dentry, int mode,
Vivek Goyal15bf3232021-10-12 09:23:07 -04002935 const struct qstr *name,
2936 const char **xattr_name, void **ctx,
David Quigleyd47be3d2013-05-22 12:50:34 -04002937 u32 *ctxlen)
2938{
David Quigleyd47be3d2013-05-22 12:50:34 -04002939 u32 newsid;
2940 int rc;
2941
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002942 rc = selinux_determine_inode_label(selinux_cred(current_cred()),
Vivek Goyalc957f6d2016-07-13 10:44:51 -04002943 d_inode(dentry->d_parent), name,
David Howellsc3c188b2015-07-10 17:19:58 -04002944 inode_mode_to_security_class(mode),
2945 &newsid);
2946 if (rc)
2947 return rc;
David Quigleyd47be3d2013-05-22 12:50:34 -04002948
Vivek Goyal15bf3232021-10-12 09:23:07 -04002949 if (xattr_name)
2950 *xattr_name = XATTR_NAME_SELINUX;
2951
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002952 return security_sid_to_context(&selinux_state, newsid, (char **)ctx,
2953 ctxlen);
David Quigleyd47be3d2013-05-22 12:50:34 -04002954}
2955
Vivek Goyala518b0a2016-07-13 10:44:53 -04002956static int selinux_dentry_create_files_as(struct dentry *dentry, int mode,
2957 struct qstr *name,
2958 const struct cred *old,
2959 struct cred *new)
2960{
2961 u32 newsid;
2962 int rc;
2963 struct task_security_struct *tsec;
2964
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002965 rc = selinux_determine_inode_label(selinux_cred(old),
Vivek Goyala518b0a2016-07-13 10:44:53 -04002966 d_inode(dentry->d_parent), name,
2967 inode_mode_to_security_class(mode),
2968 &newsid);
2969 if (rc)
2970 return rc;
2971
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002972 tsec = selinux_cred(new);
Vivek Goyala518b0a2016-07-13 10:44:53 -04002973 tsec->create_sid = newsid;
2974 return 0;
2975}
2976
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002977static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +09002978 const struct qstr *qstr,
2979 const char **name,
Eric Paris2a7dba32011-02-01 11:05:39 -05002980 void **value, size_t *len)
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002981{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002982 const struct task_security_struct *tsec = selinux_cred(current_cred());
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002983 struct superblock_security_struct *sbsec;
Corentin LABBEc0d4f462017-10-04 20:32:17 +02002984 u32 newsid, clen;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002985 int rc;
Tetsuo Handa95489062013-07-25 05:44:02 +09002986 char *context;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002987
Casey Schaufler1aea7802021-04-22 17:41:15 +02002988 sbsec = selinux_superblock(dir->i_sb);
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002989
David Howells275bb412008-11-14 10:39:19 +11002990 newsid = tsec->create_sid;
2991
Yang Guo210a2922019-12-12 10:02:24 +08002992 rc = selinux_determine_inode_label(tsec, dir, qstr,
David Howellsc3c188b2015-07-10 17:19:58 -04002993 inode_mode_to_security_class(inode->i_mode),
2994 &newsid);
2995 if (rc)
2996 return rc;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002997
Eric Paris296fddf2006-09-25 23:32:00 -07002998 /* Possibly defer initialization to selinux_complete_init. */
David P. Quigley0d90a7e2009-01-16 09:22:02 -05002999 if (sbsec->flags & SE_SBINITIALIZED) {
Casey Schaufler80788c22018-09-21 17:19:11 -07003000 struct inode_security_struct *isec = selinux_inode(inode);
Eric Paris296fddf2006-09-25 23:32:00 -07003001 isec->sclass = inode_mode_to_security_class(inode->i_mode);
3002 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05003003 isec->initialized = LABEL_INITIALIZED;
Eric Paris296fddf2006-09-25 23:32:00 -07003004 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07003005
Ondrej Mosnacek65cddd52020-01-07 14:31:53 +01003006 if (!selinux_initialized(&selinux_state) ||
3007 !(sbsec->flags & SBLABEL_MNT))
Stephen Smalley25a74f32005-11-08 21:34:33 -08003008 return -EOPNOTSUPP;
3009
Tetsuo Handa95489062013-07-25 05:44:02 +09003010 if (name)
3011 *name = XATTR_SELINUX_SUFFIX;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07003012
3013 if (value && len) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003014 rc = security_sid_to_context_force(&selinux_state, newsid,
3015 &context, &clen);
Tetsuo Handa95489062013-07-25 05:44:02 +09003016 if (rc)
Stephen Smalley570bc1c2005-09-09 13:01:43 -07003017 return rc;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07003018 *value = context;
3019 *len = clen;
3020 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07003021
Stephen Smalley5e41ff92005-09-09 13:01:35 -07003022 return 0;
3023}
3024
Daniel Colascione29cd6592021-01-08 14:22:22 -08003025static int selinux_inode_init_security_anon(struct inode *inode,
3026 const struct qstr *name,
3027 const struct inode *context_inode)
3028{
3029 const struct task_security_struct *tsec = selinux_cred(current_cred());
3030 struct common_audit_data ad;
3031 struct inode_security_struct *isec;
3032 int rc;
3033
3034 if (unlikely(!selinux_initialized(&selinux_state)))
3035 return 0;
3036
3037 isec = selinux_inode(inode);
3038
3039 /*
3040 * We only get here once per ephemeral inode. The inode has
3041 * been initialized via inode_alloc_security but is otherwise
3042 * untouched.
3043 */
3044
3045 if (context_inode) {
3046 struct inode_security_struct *context_isec =
3047 selinux_inode(context_inode);
3048 if (context_isec->initialized != LABEL_INITIALIZED) {
3049 pr_err("SELinux: context_inode is not initialized");
3050 return -EACCES;
3051 }
3052
3053 isec->sclass = context_isec->sclass;
3054 isec->sid = context_isec->sid;
3055 } else {
3056 isec->sclass = SECCLASS_ANON_INODE;
3057 rc = security_transition_sid(
3058 &selinux_state, tsec->sid, tsec->sid,
3059 isec->sclass, name, &isec->sid);
3060 if (rc)
3061 return rc;
3062 }
3063
3064 isec->initialized = LABEL_INITIALIZED;
3065 /*
3066 * Now that we've initialized security, check whether we're
3067 * allowed to actually create this type of anonymous inode.
3068 */
3069
3070 ad.type = LSM_AUDIT_DATA_INODE;
3071 ad.u.inode = inode;
3072
3073 return avc_has_perm(&selinux_state,
3074 tsec->sid,
3075 isec->sid,
3076 isec->sclass,
3077 FILE__CREATE,
3078 &ad);
3079}
3080
Al Viro4acdaf22011-07-26 01:42:34 -04003081static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082{
3083 return may_create(dir, dentry, SECCLASS_FILE);
3084}
3085
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
3087{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 return may_link(dir, old_dentry, MAY_LINK);
3089}
3090
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
3092{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 return may_link(dir, dentry, MAY_UNLINK);
3094}
3095
3096static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
3097{
3098 return may_create(dir, dentry, SECCLASS_LNK_FILE);
3099}
3100
Al Viro18bb1db2011-07-26 01:41:39 -04003101static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102{
3103 return may_create(dir, dentry, SECCLASS_DIR);
3104}
3105
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
3107{
3108 return may_link(dir, dentry, MAY_RMDIR);
3109}
3110
Al Viro1a67aaf2011-07-26 01:52:52 -04003111static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 return may_create(dir, dentry, inode_mode_to_security_class(mode));
3114}
3115
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
Eric Paris828dfe12008-04-17 13:17:49 -04003117 struct inode *new_inode, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118{
3119 return may_rename(old_inode, old_dentry, new_inode, new_dentry);
3120}
3121
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122static int selinux_inode_readlink(struct dentry *dentry)
3123{
David Howells88e67f32008-11-14 10:39:21 +11003124 const struct cred *cred = current_cred();
3125
Eric Paris2875fa02011-04-28 16:04:24 -04003126 return dentry_has_perm(cred, dentry, FILE__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127}
3128
NeilBrownbda0be72015-03-23 13:37:39 +11003129static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode,
3130 bool rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131{
David Howells88e67f32008-11-14 10:39:21 +11003132 const struct cred *cred = current_cred();
NeilBrownbda0be72015-03-23 13:37:39 +11003133 struct common_audit_data ad;
3134 struct inode_security_struct *isec;
3135 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136
NeilBrownbda0be72015-03-23 13:37:39 +11003137 validate_creds(cred);
3138
3139 ad.type = LSM_AUDIT_DATA_DENTRY;
3140 ad.u.dentry = dentry;
3141 sid = cred_sid(cred);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05003142 isec = inode_security_rcu(inode, rcu);
3143 if (IS_ERR(isec))
3144 return PTR_ERR(isec);
NeilBrownbda0be72015-03-23 13:37:39 +11003145
Al Virod99cf132021-01-16 15:57:49 -05003146 return avc_has_perm(&selinux_state,
3147 sid, isec->sid, isec->sclass, FILE__READ, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148}
3149
Eric Parisd4cf970d2012-04-04 15:01:42 -04003150static noinline int audit_inode_permission(struct inode *inode,
3151 u32 perms, u32 audited, u32 denied,
Stephen Smalley0188d5c2019-11-22 12:22:45 -05003152 int result)
Eric Parisd4cf970d2012-04-04 15:01:42 -04003153{
3154 struct common_audit_data ad;
Casey Schaufler80788c22018-09-21 17:19:11 -07003155 struct inode_security_struct *isec = selinux_inode(inode);
Eric Parisd4cf970d2012-04-04 15:01:42 -04003156
Eric Paris50c205f2012-04-04 15:01:43 -04003157 ad.type = LSM_AUDIT_DATA_INODE;
Eric Parisd4cf970d2012-04-04 15:01:42 -04003158 ad.u.inode = inode;
3159
Al Virob17ec222021-01-16 15:40:54 -05003160 return slow_avc_audit(&selinux_state,
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003161 current_sid(), isec->sid, isec->sclass, perms,
Stephen Smalley0188d5c2019-11-22 12:22:45 -05003162 audited, denied, result, &ad);
Eric Parisd4cf970d2012-04-04 15:01:42 -04003163}
3164
Al Viroe74f71e2011-06-20 19:38:15 -04003165static int selinux_inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166{
David Howells88e67f32008-11-14 10:39:21 +11003167 const struct cred *cred = current_cred();
Eric Parisb782e0a2010-07-23 11:44:03 -04003168 u32 perms;
3169 bool from_access;
Stephen Smalley5298d0b2019-11-22 16:16:56 -05003170 bool no_block = mask & MAY_NOT_BLOCK;
Eric Paris2e334052012-04-04 15:01:42 -04003171 struct inode_security_struct *isec;
3172 u32 sid;
3173 struct av_decision avd;
3174 int rc, rc2;
3175 u32 audited, denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176
Eric Parisb782e0a2010-07-23 11:44:03 -04003177 from_access = mask & MAY_ACCESS;
Eric Parisd09ca732010-07-23 11:43:57 -04003178 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
3179
Eric Parisb782e0a2010-07-23 11:44:03 -04003180 /* No permission to check. Existence test. */
3181 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183
Eric Paris2e334052012-04-04 15:01:42 -04003184 validate_creds(cred);
Eric Parisb782e0a2010-07-23 11:44:03 -04003185
Eric Paris2e334052012-04-04 15:01:42 -04003186 if (unlikely(IS_PRIVATE(inode)))
3187 return 0;
Eric Parisb782e0a2010-07-23 11:44:03 -04003188
3189 perms = file_mask_to_av(inode->i_mode, mask);
3190
Eric Paris2e334052012-04-04 15:01:42 -04003191 sid = cred_sid(cred);
Stephen Smalley5298d0b2019-11-22 16:16:56 -05003192 isec = inode_security_rcu(inode, no_block);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05003193 if (IS_ERR(isec))
3194 return PTR_ERR(isec);
Eric Paris2e334052012-04-04 15:01:42 -04003195
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003196 rc = avc_has_perm_noaudit(&selinux_state,
Al Virob17ec222021-01-16 15:40:54 -05003197 sid, isec->sid, isec->sclass, perms, 0,
Stephen Smalley3a28cff2018-12-12 10:10:55 -05003198 &avd);
Eric Paris2e334052012-04-04 15:01:42 -04003199 audited = avc_audit_required(perms, &avd, rc,
3200 from_access ? FILE__AUDIT_ACCESS : 0,
3201 &denied);
3202 if (likely(!audited))
3203 return rc;
3204
Stephen Smalley0188d5c2019-11-22 12:22:45 -05003205 rc2 = audit_inode_permission(inode, perms, audited, denied, rc);
Eric Paris2e334052012-04-04 15:01:42 -04003206 if (rc2)
3207 return rc2;
3208 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209}
3210
3211static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
3212{
David Howells88e67f32008-11-14 10:39:21 +11003213 const struct cred *cred = current_cred();
Stephen Smalleyccb54472017-05-12 12:41:24 -04003214 struct inode *inode = d_backing_inode(dentry);
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003215 unsigned int ia_valid = iattr->ia_valid;
Eric Paris95dbf732012-04-04 13:45:34 -04003216 __u32 av = FILE__WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003218 /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
3219 if (ia_valid & ATTR_FORCE) {
3220 ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
3221 ATTR_FORCE);
3222 if (!ia_valid)
3223 return 0;
3224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003226 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
3227 ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
Eric Paris2875fa02011-04-28 16:04:24 -04003228 return dentry_has_perm(cred, dentry, FILE__SETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003230 if (selinux_policycap_openperm() &&
Stephen Smalleyccb54472017-05-12 12:41:24 -04003231 inode->i_sb->s_magic != SOCKFS_MAGIC &&
3232 (ia_valid & ATTR_SIZE) &&
3233 !(ia_valid & ATTR_FILE))
Eric Paris95dbf732012-04-04 13:45:34 -04003234 av |= FILE__OPEN;
3235
3236 return dentry_has_perm(cred, dentry, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237}
3238
Al Viro3f7036a2015-03-08 19:28:30 -04003239static int selinux_inode_getattr(const struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240{
Al Viro3f7036a2015-03-08 19:28:30 -04003241 return path_has_perm(current_cred(), path, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242}
3243
Stephen Smalleydb590002017-04-20 11:31:30 -04003244static bool has_cap_mac_admin(bool audit)
3245{
3246 const struct cred *cred = current_cred();
Micah Mortonc1a85a02019-01-07 16:10:53 -08003247 unsigned int opts = audit ? CAP_OPT_NONE : CAP_OPT_NOAUDIT;
Stephen Smalleydb590002017-04-20 11:31:30 -04003248
Micah Mortonc1a85a02019-01-07 16:10:53 -08003249 if (cap_capable(cred, &init_user_ns, CAP_MAC_ADMIN, opts))
Stephen Smalleydb590002017-04-20 11:31:30 -04003250 return false;
Micah Mortonc1a85a02019-01-07 16:10:53 -08003251 if (cred_has_capability(cred, CAP_MAC_ADMIN, opts, true))
Stephen Smalleydb590002017-04-20 11:31:30 -04003252 return false;
3253 return true;
3254}
3255
Christian Brauner71bc3562021-01-21 14:19:29 +01003256static int selinux_inode_setxattr(struct user_namespace *mnt_userns,
3257 struct dentry *dentry, const char *name,
David Howells8f0cfa52008-04-29 00:59:41 -07003258 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259{
David Howellsc6f493d2015-03-17 22:26:22 +00003260 struct inode *inode = d_backing_inode(dentry);
Paul Moore20cdef82016-04-04 14:14:42 -04003261 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 struct superblock_security_struct *sbsec;
Thomas Liu2bf49692009-07-14 12:14:09 -04003263 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11003264 u32 newsid, sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 int rc = 0;
3266
Eric W. Biederman6b240302017-10-02 09:38:20 -05003267 if (strcmp(name, XATTR_NAME_SELINUX)) {
3268 rc = cap_inode_setxattr(dentry, name, value, size, flags);
3269 if (rc)
3270 return rc;
3271
3272 /* Not an attribute we recognize, so just check the
3273 ordinary setattr permission. */
3274 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276
Ondrej Mosnacek65cddd52020-01-07 14:31:53 +01003277 if (!selinux_initialized(&selinux_state))
Christian Brauner71bc3562021-01-21 14:19:29 +01003278 return (inode_owner_or_capable(mnt_userns, inode) ? 0 : -EPERM);
Jonathan Lebon3e3e24b2019-09-12 09:30:07 -04003279
Casey Schaufler1aea7802021-04-22 17:41:15 +02003280 sbsec = selinux_superblock(inode->i_sb);
Eric Paris12f348b2012-10-09 10:56:25 -04003281 if (!(sbsec->flags & SBLABEL_MNT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 return -EOPNOTSUPP;
3283
Christian Brauner71bc3562021-01-21 14:19:29 +01003284 if (!inode_owner_or_capable(mnt_userns, inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 return -EPERM;
3286
Eric Paris50c205f2012-04-04 15:01:43 -04003287 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04003288 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289
Paul Moore20cdef82016-04-04 14:14:42 -04003290 isec = backing_inode_security(dentry);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003291 rc = avc_has_perm(&selinux_state,
3292 sid, isec->sid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 FILE__RELABELFROM, &ad);
3294 if (rc)
3295 return rc;
3296
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003297 rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3298 GFP_KERNEL);
Stephen Smalley12b29f32008-05-07 13:03:20 -04003299 if (rc == -EINVAL) {
Stephen Smalleydb590002017-04-20 11:31:30 -04003300 if (!has_cap_mac_admin(true)) {
Eric Parisd6ea83e2012-04-04 13:45:49 -04003301 struct audit_buffer *ab;
3302 size_t audit_size;
Eric Parisd6ea83e2012-04-04 13:45:49 -04003303
3304 /* We strip a nul only if it is at the end, otherwise the
3305 * context contains a nul and we should audit that */
Al Viroe3fea3f2012-06-09 08:15:16 +01003306 if (value) {
Colin Ian Kingadd24372017-10-14 13:46:55 +01003307 const char *str = value;
3308
Al Viroe3fea3f2012-06-09 08:15:16 +01003309 if (str[size - 1] == '\0')
3310 audit_size = size - 1;
3311 else
3312 audit_size = size;
3313 } else {
Al Viroe3fea3f2012-06-09 08:15:16 +01003314 audit_size = 0;
3315 }
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04003316 ab = audit_log_start(audit_context(),
3317 GFP_ATOMIC, AUDIT_SELINUX_ERR);
Austin Kim893c47d2021-07-14 01:11:27 +01003318 if (!ab)
3319 return rc;
Eric Parisd6ea83e2012-04-04 13:45:49 -04003320 audit_log_format(ab, "op=setxattr invalid_context=");
3321 audit_log_n_untrustedstring(ab, value, audit_size);
3322 audit_log_end(ab);
3323
Stephen Smalley12b29f32008-05-07 13:03:20 -04003324 return rc;
Eric Parisd6ea83e2012-04-04 13:45:49 -04003325 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003326 rc = security_context_to_sid_force(&selinux_state, value,
3327 size, &newsid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04003328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 if (rc)
3330 return rc;
3331
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003332 rc = avc_has_perm(&selinux_state,
3333 sid, newsid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 FILE__RELABELTO, &ad);
3335 if (rc)
3336 return rc;
3337
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003338 rc = security_validate_transition(&selinux_state, isec->sid, newsid,
3339 sid, isec->sclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 if (rc)
3341 return rc;
3342
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003343 return avc_has_perm(&selinux_state,
3344 newsid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 sbsec->sid,
3346 SECCLASS_FILESYSTEM,
3347 FILESYSTEM__ASSOCIATE,
3348 &ad);
3349}
3350
David Howells8f0cfa52008-04-29 00:59:41 -07003351static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
Eric Parisf5269712008-05-14 11:27:45 -04003352 const void *value, size_t size,
David Howells8f0cfa52008-04-29 00:59:41 -07003353 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354{
David Howellsc6f493d2015-03-17 22:26:22 +00003355 struct inode *inode = d_backing_inode(dentry);
Paul Moore20cdef82016-04-04 14:14:42 -04003356 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 u32 newsid;
3358 int rc;
3359
3360 if (strcmp(name, XATTR_NAME_SELINUX)) {
3361 /* Not an attribute we recognize, so nothing to do. */
3362 return;
3363 }
3364
Ondrej Mosnacek65cddd52020-01-07 14:31:53 +01003365 if (!selinux_initialized(&selinux_state)) {
Jonathan Lebon3e3e24b2019-09-12 09:30:07 -04003366 /* If we haven't even been initialized, then we can't validate
3367 * against a policy, so leave the label as invalid. It may
3368 * resolve to a valid label on the next revalidation try if
3369 * we've since initialized.
3370 */
3371 return;
3372 }
3373
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003374 rc = security_context_to_sid_force(&selinux_state, value, size,
3375 &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 if (rc) {
peter enderborgc103a912018-06-12 10:09:03 +02003377 pr_err("SELinux: unable to map context to SID"
Stephen Smalley12b29f32008-05-07 13:03:20 -04003378 "for (%s, %lu), rc=%d\n",
3379 inode->i_sb->s_id, inode->i_ino, -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 return;
3381 }
3382
Paul Moore20cdef82016-04-04 14:14:42 -04003383 isec = backing_inode_security(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003384 spin_lock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003385 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05003387 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003388 spin_unlock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003389
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 return;
3391}
3392
David Howells8f0cfa52008-04-29 00:59:41 -07003393static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394{
David Howells88e67f32008-11-14 10:39:21 +11003395 const struct cred *cred = current_cred();
3396
Eric Paris2875fa02011-04-28 16:04:24 -04003397 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398}
3399
Eric Paris828dfe12008-04-17 13:17:49 -04003400static int selinux_inode_listxattr(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401{
David Howells88e67f32008-11-14 10:39:21 +11003402 const struct cred *cred = current_cred();
3403
Eric Paris2875fa02011-04-28 16:04:24 -04003404 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405}
3406
Christian Brauner71bc3562021-01-21 14:19:29 +01003407static int selinux_inode_removexattr(struct user_namespace *mnt_userns,
3408 struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409{
Eric W. Biederman6b240302017-10-02 09:38:20 -05003410 if (strcmp(name, XATTR_NAME_SELINUX)) {
Christian Brauner71bc3562021-01-21 14:19:29 +01003411 int rc = cap_inode_removexattr(mnt_userns, dentry, name);
Eric W. Biederman6b240302017-10-02 09:38:20 -05003412 if (rc)
3413 return rc;
3414
3415 /* Not an attribute we recognize, so just check the
3416 ordinary setattr permission. */
3417 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419
Stephen Smalley9530a3e2020-08-20 13:00:40 -04003420 if (!selinux_initialized(&selinux_state))
3421 return 0;
3422
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 /* No one is allowed to remove a SELinux security label.
3424 You can change the label, but all data must be labeled. */
3425 return -EACCES;
3426}
3427
Aaron Goidelac5656d2019-08-12 11:20:00 -04003428static int selinux_path_notify(const struct path *path, u64 mask,
3429 unsigned int obj_type)
3430{
3431 int ret;
3432 u32 perm;
3433
3434 struct common_audit_data ad;
3435
3436 ad.type = LSM_AUDIT_DATA_PATH;
3437 ad.u.path = *path;
3438
3439 /*
3440 * Set permission needed based on the type of mark being set.
3441 * Performs an additional check for sb watches.
3442 */
3443 switch (obj_type) {
3444 case FSNOTIFY_OBJ_TYPE_VFSMOUNT:
3445 perm = FILE__WATCH_MOUNT;
3446 break;
3447 case FSNOTIFY_OBJ_TYPE_SB:
3448 perm = FILE__WATCH_SB;
3449 ret = superblock_has_perm(current_cred(), path->dentry->d_sb,
3450 FILESYSTEM__WATCH, &ad);
3451 if (ret)
3452 return ret;
3453 break;
3454 case FSNOTIFY_OBJ_TYPE_INODE:
3455 perm = FILE__WATCH;
3456 break;
3457 default:
3458 return -EINVAL;
3459 }
3460
3461 /* blocking watches require the file:watch_with_perm permission */
3462 if (mask & (ALL_FSNOTIFY_PERM_EVENTS))
3463 perm |= FILE__WATCH_WITH_PERM;
3464
3465 /* watches on read-like events need the file:watch_reads permission */
3466 if (mask & (FS_ACCESS | FS_ACCESS_PERM | FS_CLOSE_NOWRITE))
3467 perm |= FILE__WATCH_READS;
3468
3469 return path_has_perm(current_cred(), path, perm);
3470}
3471
James Morrisd381d8a2005-10-30 14:59:22 -08003472/*
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003473 * Copy the inode security context value to the user.
James Morrisd381d8a2005-10-30 14:59:22 -08003474 *
3475 * Permission check is handled by selinux_inode_getxattr hook.
3476 */
Christian Brauner71bc3562021-01-21 14:19:29 +01003477static int selinux_inode_getsecurity(struct user_namespace *mnt_userns,
3478 struct inode *inode, const char *name,
3479 void **buffer, bool alloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480{
David P. Quigley42492592008-02-04 22:29:39 -08003481 u32 size;
3482 int error;
3483 char *context = NULL;
Paul Moore20cdef82016-04-04 14:14:42 -04003484 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485
Jonathan Lebonc8e22262020-05-28 10:39:40 -04003486 /*
3487 * If we're not initialized yet, then we can't validate contexts, so
3488 * just let vfs_getxattr fall back to using the on-disk xattr.
3489 */
3490 if (!selinux_initialized(&selinux_state) ||
3491 strcmp(name, XATTR_SELINUX_SUFFIX))
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00003492 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003494 /*
3495 * If the caller has CAP_MAC_ADMIN, then get the raw context
3496 * value even if it is not defined by current policy; otherwise,
3497 * use the in-core value under current policy.
3498 * Use the non-auditing forms of the permission checks since
3499 * getxattr may be called by unprivileged processes commonly
3500 * and lack of permission just means that we fall back to the
3501 * in-core context value, not a denial.
3502 */
Paul Moore20cdef82016-04-04 14:14:42 -04003503 isec = inode_security(inode);
Stephen Smalleydb590002017-04-20 11:31:30 -04003504 if (has_cap_mac_admin(false))
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003505 error = security_sid_to_context_force(&selinux_state,
3506 isec->sid, &context,
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003507 &size);
3508 else
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003509 error = security_sid_to_context(&selinux_state, isec->sid,
3510 &context, &size);
David P. Quigley42492592008-02-04 22:29:39 -08003511 if (error)
3512 return error;
3513 error = size;
3514 if (alloc) {
3515 *buffer = context;
3516 goto out_nofree;
3517 }
3518 kfree(context);
3519out_nofree:
3520 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521}
3522
3523static int selinux_inode_setsecurity(struct inode *inode, const char *name,
Eric Paris828dfe12008-04-17 13:17:49 -04003524 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525{
Paul Moore2c971652016-04-19 16:36:28 -04003526 struct inode_security_struct *isec = inode_security_novalidate(inode);
Casey Schaufler1aea7802021-04-22 17:41:15 +02003527 struct superblock_security_struct *sbsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 u32 newsid;
3529 int rc;
3530
3531 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3532 return -EOPNOTSUPP;
3533
Casey Schaufler1aea7802021-04-22 17:41:15 +02003534 sbsec = selinux_superblock(inode->i_sb);
Ondrej Mosnacek53e0c2a2018-12-21 21:18:53 +01003535 if (!(sbsec->flags & SBLABEL_MNT))
3536 return -EOPNOTSUPP;
3537
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 if (!value || !size)
3539 return -EACCES;
3540
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003541 rc = security_context_to_sid(&selinux_state, value, size, &newsid,
3542 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 if (rc)
3544 return rc;
3545
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003546 spin_lock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003547 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05003549 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003550 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 return 0;
3552}
3553
3554static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
3555{
3556 const int len = sizeof(XATTR_NAME_SELINUX);
Amir Goldsteina9ffe682020-12-19 12:05:27 +02003557
3558 if (!selinux_initialized(&selinux_state))
3559 return 0;
3560
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 if (buffer && len <= buffer_size)
3562 memcpy(buffer, XATTR_NAME_SELINUX, len);
3563 return len;
3564}
3565
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05003566static void selinux_inode_getsecid(struct inode *inode, u32 *secid)
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02003567{
Andreas Gruenbachere817c2f2016-02-18 12:04:08 +01003568 struct inode_security_struct *isec = inode_security_novalidate(inode);
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02003569 *secid = isec->sid;
3570}
3571
Vivek Goyal56909eb2016-07-13 10:44:48 -04003572static int selinux_inode_copy_up(struct dentry *src, struct cred **new)
3573{
3574 u32 sid;
3575 struct task_security_struct *tsec;
3576 struct cred *new_creds = *new;
3577
3578 if (new_creds == NULL) {
3579 new_creds = prepare_creds();
3580 if (!new_creds)
3581 return -ENOMEM;
3582 }
3583
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07003584 tsec = selinux_cred(new_creds);
Vivek Goyal56909eb2016-07-13 10:44:48 -04003585 /* Get label from overlay inode and set it in create_sid */
3586 selinux_inode_getsecid(d_inode(src), &sid);
3587 tsec->create_sid = sid;
3588 *new = new_creds;
3589 return 0;
3590}
3591
Vivek Goyal19472b62016-07-13 10:44:50 -04003592static int selinux_inode_copy_up_xattr(const char *name)
3593{
3594 /* The copy_up hook above sets the initial context on an inode, but we
3595 * don't then want to overwrite it by blindly copying all the lower
3596 * xattrs up. Instead, we have to filter out SELinux-related xattrs.
3597 */
3598 if (strcmp(name, XATTR_NAME_SELINUX) == 0)
3599 return 1; /* Discard */
3600 /*
3601 * Any other attribute apart from SELINUX is not claimed, supported
3602 * by selinux.
3603 */
3604 return -EOPNOTSUPP;
3605}
3606
Ondrej Mosnacekec882da2019-02-22 15:57:17 +01003607/* kernfs node operations */
3608
YueHaibingc72c4cd2019-03-22 22:04:00 +08003609static int selinux_kernfs_init_security(struct kernfs_node *kn_dir,
3610 struct kernfs_node *kn)
Ondrej Mosnacekec882da2019-02-22 15:57:17 +01003611{
Stephen Smalley169ce0c2019-09-04 10:32:48 -04003612 const struct task_security_struct *tsec = selinux_cred(current_cred());
Ondrej Mosnacekec882da2019-02-22 15:57:17 +01003613 u32 parent_sid, newsid, clen;
3614 int rc;
3615 char *context;
3616
Ondrej Mosnacek1537ad12019-04-03 09:29:41 +02003617 rc = kernfs_xattr_get(kn_dir, XATTR_NAME_SELINUX, NULL, 0);
Ondrej Mosnacekec882da2019-02-22 15:57:17 +01003618 if (rc == -ENODATA)
3619 return 0;
3620 else if (rc < 0)
3621 return rc;
3622
3623 clen = (u32)rc;
3624 context = kmalloc(clen, GFP_KERNEL);
3625 if (!context)
3626 return -ENOMEM;
3627
Ondrej Mosnacek1537ad12019-04-03 09:29:41 +02003628 rc = kernfs_xattr_get(kn_dir, XATTR_NAME_SELINUX, context, clen);
Ondrej Mosnacekec882da2019-02-22 15:57:17 +01003629 if (rc < 0) {
3630 kfree(context);
3631 return rc;
3632 }
3633
3634 rc = security_context_to_sid(&selinux_state, context, clen, &parent_sid,
3635 GFP_KERNEL);
3636 kfree(context);
3637 if (rc)
3638 return rc;
3639
3640 if (tsec->create_sid) {
3641 newsid = tsec->create_sid;
3642 } else {
3643 u16 secclass = inode_mode_to_security_class(kn->mode);
3644 struct qstr q;
3645
3646 q.name = kn->name;
3647 q.hash_len = hashlen_string(kn_dir, kn->name);
3648
3649 rc = security_transition_sid(&selinux_state, tsec->sid,
3650 parent_sid, secclass, &q,
3651 &newsid);
3652 if (rc)
3653 return rc;
3654 }
3655
3656 rc = security_sid_to_context_force(&selinux_state, newsid,
3657 &context, &clen);
3658 if (rc)
3659 return rc;
3660
Ondrej Mosnacek1537ad12019-04-03 09:29:41 +02003661 rc = kernfs_xattr_set(kn, XATTR_NAME_SELINUX, context, clen,
3662 XATTR_CREATE);
Ondrej Mosnacekec882da2019-02-22 15:57:17 +01003663 kfree(context);
3664 return rc;
3665}
3666
3667
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668/* file security operations */
3669
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003670static int selinux_revalidate_file_permission(struct file *file, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671{
David Howells88e67f32008-11-14 10:39:21 +11003672 const struct cred *cred = current_cred();
Al Viro496ad9a2013-01-23 17:07:38 -05003673 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
3676 if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
3677 mask |= MAY_APPEND;
3678
Paul Moore389fb8002009-03-27 17:10:34 -04003679 return file_has_perm(cred, file,
3680 file_mask_to_av(inode->i_mode, mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681}
3682
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003683static int selinux_file_permission(struct file *file, int mask)
3684{
Al Viro496ad9a2013-01-23 17:07:38 -05003685 struct inode *inode = file_inode(file);
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07003686 struct file_security_struct *fsec = selinux_file(file);
Andreas Gruenbacherb1973672016-01-05 23:12:33 +01003687 struct inode_security_struct *isec;
Stephen Smalley20dda182009-06-22 14:54:53 -04003688 u32 sid = current_sid();
3689
Paul Moore389fb8002009-03-27 17:10:34 -04003690 if (!mask)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003691 /* No permission to check. Existence test. */
3692 return 0;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003693
Andreas Gruenbacherb1973672016-01-05 23:12:33 +01003694 isec = inode_security(inode);
Stephen Smalley20dda182009-06-22 14:54:53 -04003695 if (sid == fsec->sid && fsec->isid == isec->sid &&
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003696 fsec->pseqno == avc_policy_seqno(&selinux_state))
Eric Paris83d49852012-04-04 13:45:40 -04003697 /* No change since file_open check. */
Stephen Smalley20dda182009-06-22 14:54:53 -04003698 return 0;
3699
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003700 return selinux_revalidate_file_permission(file, mask);
3701}
3702
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703static int selinux_file_alloc_security(struct file *file)
3704{
Paul Moorecb89e242020-01-10 16:32:10 -05003705 struct file_security_struct *fsec = selinux_file(file);
3706 u32 sid = current_sid();
3707
3708 fsec->sid = sid;
3709 fsec->fown_sid = sid;
3710
3711 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712}
3713
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003714/*
3715 * Check whether a task has the ioctl permission and cmd
3716 * operation to an inode.
3717 */
Geliang Tang1d2a1682015-10-21 17:44:27 -04003718static int ioctl_has_perm(const struct cred *cred, struct file *file,
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003719 u32 requested, u16 cmd)
3720{
3721 struct common_audit_data ad;
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07003722 struct file_security_struct *fsec = selinux_file(file);
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003723 struct inode *inode = file_inode(file);
Paul Moore20cdef82016-04-04 14:14:42 -04003724 struct inode_security_struct *isec;
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003725 struct lsm_ioctlop_audit ioctl;
3726 u32 ssid = cred_sid(cred);
3727 int rc;
3728 u8 driver = cmd >> 8;
3729 u8 xperm = cmd & 0xff;
3730
3731 ad.type = LSM_AUDIT_DATA_IOCTL_OP;
3732 ad.u.op = &ioctl;
3733 ad.u.op->cmd = cmd;
3734 ad.u.op->path = file->f_path;
3735
3736 if (ssid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003737 rc = avc_has_perm(&selinux_state,
3738 ssid, fsec->sid,
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003739 SECCLASS_FD,
3740 FD__USE,
3741 &ad);
3742 if (rc)
3743 goto out;
3744 }
3745
3746 if (unlikely(IS_PRIVATE(inode)))
3747 return 0;
3748
Paul Moore20cdef82016-04-04 14:14:42 -04003749 isec = inode_security(inode);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003750 rc = avc_has_extended_perms(&selinux_state,
3751 ssid, isec->sid, isec->sclass,
3752 requested, driver, xperm, &ad);
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003753out:
3754 return rc;
3755}
3756
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757static int selinux_file_ioctl(struct file *file, unsigned int cmd,
3758 unsigned long arg)
3759{
David Howells88e67f32008-11-14 10:39:21 +11003760 const struct cred *cred = current_cred();
Eric Paris0b24dcb2011-02-25 15:39:20 -05003761 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762
Eric Paris0b24dcb2011-02-25 15:39:20 -05003763 switch (cmd) {
3764 case FIONREAD:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003765 case FIBMAP:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003766 case FIGETBSZ:
Al Viro2f99c362012-03-23 16:04:05 -04003767 case FS_IOC_GETFLAGS:
Al Viro2f99c362012-03-23 16:04:05 -04003768 case FS_IOC_GETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003769 error = file_has_perm(cred, file, FILE__GETATTR);
3770 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771
Al Viro2f99c362012-03-23 16:04:05 -04003772 case FS_IOC_SETFLAGS:
Al Viro2f99c362012-03-23 16:04:05 -04003773 case FS_IOC_SETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003774 error = file_has_perm(cred, file, FILE__SETATTR);
3775 break;
3776
3777 /* sys_ioctl() checks */
3778 case FIONBIO:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003779 case FIOASYNC:
3780 error = file_has_perm(cred, file, 0);
3781 break;
3782
3783 case KDSKBENT:
3784 case KDSKBSENT:
Eric Paris6a9de492012-01-03 12:25:14 -05003785 error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
Micah Mortonc1a85a02019-01-07 16:10:53 -08003786 CAP_OPT_NONE, true);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003787 break;
3788
3789 /* default case assumes that the command will go
3790 * to the file's ioctl() function.
3791 */
3792 default:
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003793 error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003794 }
3795 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796}
3797
Stephen Smalleyb78b7d52020-01-08 12:23:56 -05003798static int default_noexec __ro_after_init;
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003799
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
3801{
David Howells88e67f32008-11-14 10:39:21 +11003802 const struct cred *cred = current_cred();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003803 u32 sid = cred_sid(cred);
David Howellsd84f4f92008-11-14 10:39:23 +11003804 int rc = 0;
David Howells88e67f32008-11-14 10:39:21 +11003805
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003806 if (default_noexec &&
Stephen Smalley892e8ca2015-07-10 09:40:59 -04003807 (prot & PROT_EXEC) && (!file || IS_PRIVATE(file_inode(file)) ||
3808 (!shared && (prot & PROT_WRITE)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 /*
3810 * We are making executable an anonymous mapping or a
3811 * private file mapping that will also be writable.
3812 * This has an additional check.
3813 */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003814 rc = avc_has_perm(&selinux_state,
3815 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003816 PROCESS__EXECMEM, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 if (rc)
David Howellsd84f4f92008-11-14 10:39:23 +11003818 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820
3821 if (file) {
3822 /* read access is always possible with a mapping */
3823 u32 av = FILE__READ;
3824
3825 /* write access only matters if the mapping is shared */
3826 if (shared && (prot & PROT_WRITE))
3827 av |= FILE__WRITE;
3828
3829 if (prot & PROT_EXEC)
3830 av |= FILE__EXECUTE;
3831
David Howells88e67f32008-11-14 10:39:21 +11003832 return file_has_perm(cred, file, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 }
David Howellsd84f4f92008-11-14 10:39:23 +11003834
3835error:
3836 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837}
3838
Al Viroe5467852012-05-30 13:30:51 -04003839static int selinux_mmap_addr(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07003841 int rc = 0;
Paul Moore98883bf2014-03-19 16:46:11 -04003842
3843 if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
3844 u32 sid = current_sid();
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003845 rc = avc_has_perm(&selinux_state,
3846 sid, sid, SECCLASS_MEMPROTECT,
Paul Moore98883bf2014-03-19 16:46:11 -04003847 MEMPROTECT__MMAP_ZERO, NULL);
3848 }
3849
3850 return rc;
Al Viroe5467852012-05-30 13:30:51 -04003851}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852
Al Viroe5467852012-05-30 13:30:51 -04003853static int selinux_mmap_file(struct file *file, unsigned long reqprot,
3854 unsigned long prot, unsigned long flags)
3855{
Stephen Smalley3ba4bf52017-05-05 09:14:48 -04003856 struct common_audit_data ad;
3857 int rc;
3858
3859 if (file) {
3860 ad.type = LSM_AUDIT_DATA_FILE;
3861 ad.u.file = file;
3862 rc = inode_has_perm(current_cred(), file_inode(file),
3863 FILE__MAP, &ad);
3864 if (rc)
3865 return rc;
3866 }
3867
Lakshmi Ramasubramanian8861d0a2020-09-14 10:31:57 -07003868 if (checkreqprot_get(&selinux_state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 prot = reqprot;
3870
3871 return file_map_prot_check(file, prot,
3872 (flags & MAP_TYPE) == MAP_SHARED);
3873}
3874
3875static int selinux_file_mprotect(struct vm_area_struct *vma,
3876 unsigned long reqprot,
3877 unsigned long prot)
3878{
David Howells88e67f32008-11-14 10:39:21 +11003879 const struct cred *cred = current_cred();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003880 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881
Lakshmi Ramasubramanian8861d0a2020-09-14 10:31:57 -07003882 if (checkreqprot_get(&selinux_state))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 prot = reqprot;
3884
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003885 if (default_noexec &&
3886 (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
James Morrisd541bbe2009-01-29 12:19:51 +11003887 int rc = 0;
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003888 if (vma->vm_start >= vma->vm_mm->start_brk &&
3889 vma->vm_end <= vma->vm_mm->brk) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003890 rc = avc_has_perm(&selinux_state,
3891 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003892 PROCESS__EXECHEAP, NULL);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003893 } else if (!vma->vm_file &&
Stephen Smalleyc2316db2016-04-08 13:55:03 -04003894 ((vma->vm_start <= vma->vm_mm->start_stack &&
3895 vma->vm_end >= vma->vm_mm->start_stack) ||
Andy Lutomirskid17af502016-09-30 10:58:58 -07003896 vma_is_stack_for_current(vma))) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003897 rc = avc_has_perm(&selinux_state,
3898 sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003899 PROCESS__EXECSTACK, NULL);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003900 } else if (vma->vm_file && vma->anon_vma) {
3901 /*
3902 * We are making executable a file mapping that has
3903 * had some COW done. Since pages might have been
3904 * written, check ability to execute the possibly
3905 * modified content. This typically should only
3906 * occur for text relocations.
3907 */
David Howellsd84f4f92008-11-14 10:39:23 +11003908 rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
Stephen Smalleydb4c9642006-02-01 03:05:54 -08003909 }
Lorenzo Hernandez García-Hierro6b992192005-06-25 14:54:34 -07003910 if (rc)
3911 return rc;
3912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913
3914 return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
3915}
3916
3917static int selinux_file_lock(struct file *file, unsigned int cmd)
3918{
David Howells88e67f32008-11-14 10:39:21 +11003919 const struct cred *cred = current_cred();
3920
3921 return file_has_perm(cred, file, FILE__LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922}
3923
3924static int selinux_file_fcntl(struct file *file, unsigned int cmd,
3925 unsigned long arg)
3926{
David Howells88e67f32008-11-14 10:39:21 +11003927 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928 int err = 0;
3929
3930 switch (cmd) {
Eric Paris828dfe12008-04-17 13:17:49 -04003931 case F_SETFL:
Eric Paris828dfe12008-04-17 13:17:49 -04003932 if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
David Howells88e67f32008-11-14 10:39:21 +11003933 err = file_has_perm(cred, file, FILE__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003935 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003936 fallthrough;
Eric Paris828dfe12008-04-17 13:17:49 -04003937 case F_SETOWN:
3938 case F_SETSIG:
3939 case F_GETFL:
3940 case F_GETOWN:
3941 case F_GETSIG:
Cyrill Gorcunov1d151c32012-07-30 14:43:00 -07003942 case F_GETOWNER_UIDS:
Eric Paris828dfe12008-04-17 13:17:49 -04003943 /* Just check FD__USE permission */
David Howells88e67f32008-11-14 10:39:21 +11003944 err = file_has_perm(cred, file, 0);
Eric Paris828dfe12008-04-17 13:17:49 -04003945 break;
3946 case F_GETLK:
3947 case F_SETLK:
3948 case F_SETLKW:
Jeff Layton0d3f7a22014-04-22 08:23:58 -04003949 case F_OFD_GETLK:
3950 case F_OFD_SETLK:
3951 case F_OFD_SETLKW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952#if BITS_PER_LONG == 32
Eric Paris828dfe12008-04-17 13:17:49 -04003953 case F_GETLK64:
3954 case F_SETLK64:
3955 case F_SETLKW64:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956#endif
David Howells88e67f32008-11-14 10:39:21 +11003957 err = file_has_perm(cred, file, FILE__LOCK);
Eric Paris828dfe12008-04-17 13:17:49 -04003958 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 }
3960
3961 return err;
3962}
3963
Jeff Laytone0b93ed2014-08-22 11:27:32 -04003964static void selinux_file_set_fowner(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 struct file_security_struct *fsec;
3967
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07003968 fsec = selinux_file(file);
David Howells275bb412008-11-14 10:39:19 +11003969 fsec->fown_sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970}
3971
3972static int selinux_file_send_sigiotask(struct task_struct *tsk,
3973 struct fown_struct *fown, int signum)
3974{
Eric Paris828dfe12008-04-17 13:17:49 -04003975 struct file *file;
Paul Mooreeb1231f2021-02-18 15:13:40 -05003976 u32 sid = task_sid_obj(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 struct file_security_struct *fsec;
3979
3980 /* struct fown_struct is never outside the context of a struct file */
Eric Paris828dfe12008-04-17 13:17:49 -04003981 file = container_of(fown, struct file, f_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07003983 fsec = selinux_file(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984
3985 if (!signum)
3986 perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
3987 else
3988 perm = signal_to_av(signum);
3989
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003990 return avc_has_perm(&selinux_state,
3991 fsec->fown_sid, sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 SECCLASS_PROCESS, perm, NULL);
3993}
3994
3995static int selinux_file_receive(struct file *file)
3996{
David Howells88e67f32008-11-14 10:39:21 +11003997 const struct cred *cred = current_cred();
3998
3999 return file_has_perm(cred, file, file_to_av(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000}
4001
Al Viro94817692018-07-10 14:13:18 -04004002static int selinux_file_open(struct file *file)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09004003{
4004 struct file_security_struct *fsec;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09004005 struct inode_security_struct *isec;
David Howellsd84f4f92008-11-14 10:39:23 +11004006
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07004007 fsec = selinux_file(file);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05004008 isec = inode_security(file_inode(file));
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09004009 /*
4010 * Save inode label and policy sequence number
4011 * at open-time so that selinux_file_permission
4012 * can determine whether revalidation is necessary.
4013 * Task label is already saved in the file security
4014 * struct as its SID.
4015 */
4016 fsec->isid = isec->sid;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004017 fsec->pseqno = avc_policy_seqno(&selinux_state);
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09004018 /*
4019 * Since the inode label or policy seqno may have changed
4020 * between the selinux_inode_permission check and the saving
4021 * of state above, recheck that access is still permitted.
4022 * Otherwise, access might never be revalidated against the
4023 * new inode label or new policy.
4024 * This check is not redundant - do not remove.
4025 */
Al Viro94817692018-07-10 14:13:18 -04004026 return file_path_has_perm(file->f_cred, file, open_file_to_av(file));
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09004027}
4028
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029/* task security operations */
4030
Tetsuo Handaa79be232017-03-28 23:08:45 +09004031static int selinux_task_alloc(struct task_struct *task,
4032 unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004034 u32 sid = current_sid();
4035
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004036 return avc_has_perm(&selinux_state,
4037 sid, sid, SECCLASS_PROCESS, PROCESS__FORK, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038}
4039
David Howellsf1752ee2008-11-14 10:39:17 +11004040/*
David Howellsd84f4f92008-11-14 10:39:23 +11004041 * prepare a new set of credentials for modification
4042 */
4043static int selinux_cred_prepare(struct cred *new, const struct cred *old,
4044 gfp_t gfp)
4045{
Casey Schauflerbbd36622018-11-12 09:30:56 -08004046 const struct task_security_struct *old_tsec = selinux_cred(old);
4047 struct task_security_struct *tsec = selinux_cred(new);
David Howellsd84f4f92008-11-14 10:39:23 +11004048
Casey Schauflerbbd36622018-11-12 09:30:56 -08004049 *tsec = *old_tsec;
David Howellsd84f4f92008-11-14 10:39:23 +11004050 return 0;
4051}
4052
4053/*
David Howellsee18d642009-09-02 09:14:21 +01004054 * transfer the SELinux data to a blank set of creds
4055 */
4056static void selinux_cred_transfer(struct cred *new, const struct cred *old)
4057{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07004058 const struct task_security_struct *old_tsec = selinux_cred(old);
4059 struct task_security_struct *tsec = selinux_cred(new);
David Howellsee18d642009-09-02 09:14:21 +01004060
4061 *tsec = *old_tsec;
4062}
4063
Matthew Garrett3ec30112018-01-08 13:36:19 -08004064static void selinux_cred_getsecid(const struct cred *c, u32 *secid)
4065{
4066 *secid = cred_sid(c);
4067}
4068
David Howellsee18d642009-09-02 09:14:21 +01004069/*
David Howells3a3b7ce2008-11-14 10:39:28 +11004070 * set the security data for a kernel service
4071 * - all the creation contexts are set to unlabelled
4072 */
4073static int selinux_kernel_act_as(struct cred *new, u32 secid)
4074{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07004075 struct task_security_struct *tsec = selinux_cred(new);
David Howells3a3b7ce2008-11-14 10:39:28 +11004076 u32 sid = current_sid();
4077 int ret;
4078
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004079 ret = avc_has_perm(&selinux_state,
4080 sid, secid,
David Howells3a3b7ce2008-11-14 10:39:28 +11004081 SECCLASS_KERNEL_SERVICE,
4082 KERNEL_SERVICE__USE_AS_OVERRIDE,
4083 NULL);
4084 if (ret == 0) {
4085 tsec->sid = secid;
4086 tsec->create_sid = 0;
4087 tsec->keycreate_sid = 0;
4088 tsec->sockcreate_sid = 0;
4089 }
4090 return ret;
4091}
4092
4093/*
4094 * set the file creation context in a security record to the same as the
4095 * objective context of the specified inode
4096 */
4097static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
4098{
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05004099 struct inode_security_struct *isec = inode_security(inode);
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07004100 struct task_security_struct *tsec = selinux_cred(new);
David Howells3a3b7ce2008-11-14 10:39:28 +11004101 u32 sid = current_sid();
4102 int ret;
4103
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004104 ret = avc_has_perm(&selinux_state,
4105 sid, isec->sid,
David Howells3a3b7ce2008-11-14 10:39:28 +11004106 SECCLASS_KERNEL_SERVICE,
4107 KERNEL_SERVICE__CREATE_FILES_AS,
4108 NULL);
4109
4110 if (ret == 0)
4111 tsec->create_sid = isec->sid;
David Howellsef574712010-02-26 01:56:16 +00004112 return ret;
David Howells3a3b7ce2008-11-14 10:39:28 +11004113}
4114
Eric Parisdd8dbf22009-11-03 16:35:32 +11004115static int selinux_kernel_module_request(char *kmod_name)
Eric Paris25354c42009-08-13 09:45:03 -04004116{
Eric Parisdd8dbf22009-11-03 16:35:32 +11004117 struct common_audit_data ad;
4118
Eric Paris50c205f2012-04-04 15:01:43 -04004119 ad.type = LSM_AUDIT_DATA_KMOD;
Eric Parisdd8dbf22009-11-03 16:35:32 +11004120 ad.u.kmod_name = kmod_name;
4121
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004122 return avc_has_perm(&selinux_state,
4123 current_sid(), SECINITSID_KERNEL, SECCLASS_SYSTEM,
Eric Parisdd8dbf22009-11-03 16:35:32 +11004124 SYSTEM__MODULE_REQUEST, &ad);
Eric Paris25354c42009-08-13 09:45:03 -04004125}
4126
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07004127static int selinux_kernel_module_from_file(struct file *file)
4128{
4129 struct common_audit_data ad;
4130 struct inode_security_struct *isec;
4131 struct file_security_struct *fsec;
4132 u32 sid = current_sid();
4133 int rc;
4134
4135 /* init_module */
4136 if (file == NULL)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004137 return avc_has_perm(&selinux_state,
4138 sid, sid, SECCLASS_SYSTEM,
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07004139 SYSTEM__MODULE_LOAD, NULL);
4140
4141 /* finit_module */
Paul Moore20cdef82016-04-04 14:14:42 -04004142
Vivek Goyal43af5de2016-09-09 11:37:49 -04004143 ad.type = LSM_AUDIT_DATA_FILE;
4144 ad.u.file = file;
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07004145
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07004146 fsec = selinux_file(file);
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07004147 if (sid != fsec->sid) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004148 rc = avc_has_perm(&selinux_state,
4149 sid, fsec->sid, SECCLASS_FD, FD__USE, &ad);
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07004150 if (rc)
4151 return rc;
4152 }
4153
Paul Moore20cdef82016-04-04 14:14:42 -04004154 isec = inode_security(file_inode(file));
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004155 return avc_has_perm(&selinux_state,
4156 sid, isec->sid, SECCLASS_SYSTEM,
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07004157 SYSTEM__MODULE_LOAD, &ad);
4158}
4159
4160static int selinux_kernel_read_file(struct file *file,
Kees Cook2039bda2020-10-02 10:38:23 -07004161 enum kernel_read_file_id id,
4162 bool contents)
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07004163{
4164 int rc = 0;
4165
4166 switch (id) {
4167 case READING_MODULE:
Kees Cook2039bda2020-10-02 10:38:23 -07004168 rc = selinux_kernel_module_from_file(contents ? file : NULL);
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07004169 break;
4170 default:
4171 break;
4172 }
4173
4174 return rc;
4175}
4176
Kees Cookb64fcae2020-10-02 10:38:20 -07004177static int selinux_kernel_load_data(enum kernel_load_data_id id, bool contents)
Mimi Zoharc77b8cd2018-07-13 14:06:02 -04004178{
4179 int rc = 0;
4180
4181 switch (id) {
4182 case LOADING_MODULE:
4183 rc = selinux_kernel_module_from_file(NULL);
Gustavo A. R. Silvab2d99bc2020-11-20 12:32:26 -06004184 break;
Mimi Zoharc77b8cd2018-07-13 14:06:02 -04004185 default:
4186 break;
4187 }
4188
4189 return rc;
4190}
4191
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
4193{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004194 return avc_has_perm(&selinux_state,
Paul Mooreeb1231f2021-02-18 15:13:40 -05004195 current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004196 PROCESS__SETPGID, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197}
4198
4199static int selinux_task_getpgid(struct task_struct *p)
4200{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004201 return avc_has_perm(&selinux_state,
Paul Mooreeb1231f2021-02-18 15:13:40 -05004202 current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004203 PROCESS__GETPGID, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204}
4205
4206static int selinux_task_getsid(struct task_struct *p)
4207{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004208 return avc_has_perm(&selinux_state,
Paul Mooreeb1231f2021-02-18 15:13:40 -05004209 current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004210 PROCESS__GETSESSION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211}
4212
Paul Mooreeb1231f2021-02-18 15:13:40 -05004213static void selinux_task_getsecid_subj(struct task_struct *p, u32 *secid)
David Quigleyf9008e4c2006-06-30 01:55:46 -07004214{
Paul Mooreeb1231f2021-02-18 15:13:40 -05004215 *secid = task_sid_subj(p);
4216}
4217
4218static void selinux_task_getsecid_obj(struct task_struct *p, u32 *secid)
4219{
4220 *secid = task_sid_obj(p);
David Quigleyf9008e4c2006-06-30 01:55:46 -07004221}
4222
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223static int selinux_task_setnice(struct task_struct *p, int nice)
4224{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004225 return avc_has_perm(&selinux_state,
Paul Mooreeb1231f2021-02-18 15:13:40 -05004226 current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004227 PROCESS__SETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228}
4229
James Morris03e68062006-06-23 02:03:58 -07004230static int selinux_task_setioprio(struct task_struct *p, int ioprio)
4231{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004232 return avc_has_perm(&selinux_state,
Paul Mooreeb1231f2021-02-18 15:13:40 -05004233 current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004234 PROCESS__SETSCHED, NULL);
James Morris03e68062006-06-23 02:03:58 -07004235}
4236
David Quigleya1836a42006-06-30 01:55:49 -07004237static int selinux_task_getioprio(struct task_struct *p)
4238{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004239 return avc_has_perm(&selinux_state,
Paul Mooreeb1231f2021-02-18 15:13:40 -05004240 current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004241 PROCESS__GETSCHED, NULL);
David Quigleya1836a42006-06-30 01:55:49 -07004242}
4243
Corentin LABBE42985552017-10-04 20:32:18 +02004244static int selinux_task_prlimit(const struct cred *cred, const struct cred *tcred,
4245 unsigned int flags)
Stephen Smalley791ec492017-02-17 07:57:00 -05004246{
4247 u32 av = 0;
4248
Stephen Smalley84e68852017-02-28 09:35:08 -05004249 if (!flags)
4250 return 0;
Stephen Smalley791ec492017-02-17 07:57:00 -05004251 if (flags & LSM_PRLIMIT_WRITE)
4252 av |= PROCESS__SETRLIMIT;
4253 if (flags & LSM_PRLIMIT_READ)
4254 av |= PROCESS__GETRLIMIT;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004255 return avc_has_perm(&selinux_state,
4256 cred_sid(cred), cred_sid(tcred),
Stephen Smalley791ec492017-02-17 07:57:00 -05004257 SECCLASS_PROCESS, av, NULL);
4258}
4259
Jiri Slaby8fd00b42009-08-26 18:41:16 +02004260static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
4261 struct rlimit *new_rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262{
Jiri Slaby8fd00b42009-08-26 18:41:16 +02004263 struct rlimit *old_rlim = p->signal->rlim + resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264
4265 /* Control the ability to change the hard limit (whether
4266 lowering or raising it), so that the hard limit can
4267 later be used as a safe reset point for the soft limit
David Howellsd84f4f92008-11-14 10:39:23 +11004268 upon context transitions. See selinux_bprm_committing_creds. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 if (old_rlim->rlim_max != new_rlim->rlim_max)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004270 return avc_has_perm(&selinux_state,
Paul Mooreeb1231f2021-02-18 15:13:40 -05004271 current_sid(), task_sid_obj(p),
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004272 SECCLASS_PROCESS, PROCESS__SETRLIMIT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273
4274 return 0;
4275}
4276
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09004277static int selinux_task_setscheduler(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004279 return avc_has_perm(&selinux_state,
Paul Mooreeb1231f2021-02-18 15:13:40 -05004280 current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004281 PROCESS__SETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282}
4283
4284static int selinux_task_getscheduler(struct task_struct *p)
4285{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004286 return avc_has_perm(&selinux_state,
Paul Mooreeb1231f2021-02-18 15:13:40 -05004287 current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004288 PROCESS__GETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289}
4290
David Quigley35601542006-06-23 02:04:01 -07004291static int selinux_task_movememory(struct task_struct *p)
4292{
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004293 return avc_has_perm(&selinux_state,
Paul Mooreeb1231f2021-02-18 15:13:40 -05004294 current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004295 PROCESS__SETSCHED, NULL);
David Quigley35601542006-06-23 02:04:01 -07004296}
4297
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02004298static int selinux_task_kill(struct task_struct *p, struct kernel_siginfo *info,
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004299 int sig, const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300{
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004301 u32 secid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 if (!sig)
4305 perm = PROCESS__SIGNULL; /* null signal; existence test */
4306 else
4307 perm = signal_to_av(sig);
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004308 if (!cred)
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004309 secid = current_sid();
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004310 else
4311 secid = cred_sid(cred);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004312 return avc_has_perm(&selinux_state,
Paul Mooreeb1231f2021-02-18 15:13:40 -05004313 secid, task_sid_obj(p), SECCLASS_PROCESS, perm, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314}
4315
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316static void selinux_task_to_inode(struct task_struct *p,
4317 struct inode *inode)
4318{
Casey Schaufler80788c22018-09-21 17:19:11 -07004319 struct inode_security_struct *isec = selinux_inode(inode);
Paul Mooreeb1231f2021-02-18 15:13:40 -05004320 u32 sid = task_sid_obj(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004322 spin_lock(&isec->lock);
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01004323 isec->sclass = inode_mode_to_security_class(inode->i_mode);
David Howells275bb412008-11-14 10:39:19 +11004324 isec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004325 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004326 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327}
4328
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004330static int selinux_parse_skb_ipv4(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04004331 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332{
4333 int offset, ihlen, ret = -EINVAL;
4334 struct iphdr _iph, *ih;
4335
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03004336 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
4338 if (ih == NULL)
4339 goto out;
4340
4341 ihlen = ih->ihl * 4;
4342 if (ihlen < sizeof(_iph))
4343 goto out;
4344
Eric Paris48c62af2012-04-02 13:15:44 -04004345 ad->u.net->v4info.saddr = ih->saddr;
4346 ad->u.net->v4info.daddr = ih->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 ret = 0;
4348
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004349 if (proto)
4350 *proto = ih->protocol;
4351
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 switch (ih->protocol) {
Eric Paris828dfe12008-04-17 13:17:49 -04004353 case IPPROTO_TCP: {
4354 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355
Eric Paris828dfe12008-04-17 13:17:49 -04004356 if (ntohs(ih->frag_off) & IP_OFFSET)
4357 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358
4359 offset += ihlen;
4360 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4361 if (th == NULL)
4362 break;
4363
Eric Paris48c62af2012-04-02 13:15:44 -04004364 ad->u.net->sport = th->source;
4365 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368
Eric Paris828dfe12008-04-17 13:17:49 -04004369 case IPPROTO_UDP: {
4370 struct udphdr _udph, *uh;
4371
4372 if (ntohs(ih->frag_off) & IP_OFFSET)
4373 break;
4374
4375 offset += ihlen;
4376 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4377 if (uh == NULL)
4378 break;
4379
Eric Paris48c62af2012-04-02 13:15:44 -04004380 ad->u.net->sport = uh->source;
4381 ad->u.net->dport = uh->dest;
Eric Paris828dfe12008-04-17 13:17:49 -04004382 break;
4383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384
James Morris2ee92d42006-11-13 16:09:01 -08004385 case IPPROTO_DCCP: {
4386 struct dccp_hdr _dccph, *dh;
4387
4388 if (ntohs(ih->frag_off) & IP_OFFSET)
4389 break;
4390
4391 offset += ihlen;
4392 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4393 if (dh == NULL)
4394 break;
4395
Eric Paris48c62af2012-04-02 13:15:44 -04004396 ad->u.net->sport = dh->dccph_sport;
4397 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08004398 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004399 }
James Morris2ee92d42006-11-13 16:09:01 -08004400
Richard Hainesd4529302018-02-13 20:57:18 +00004401#if IS_ENABLED(CONFIG_IP_SCTP)
4402 case IPPROTO_SCTP: {
4403 struct sctphdr _sctph, *sh;
4404
4405 if (ntohs(ih->frag_off) & IP_OFFSET)
4406 break;
4407
4408 offset += ihlen;
4409 sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4410 if (sh == NULL)
4411 break;
4412
4413 ad->u.net->sport = sh->source;
4414 ad->u.net->dport = sh->dest;
4415 break;
4416 }
4417#endif
Eric Paris828dfe12008-04-17 13:17:49 -04004418 default:
4419 break;
4420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421out:
4422 return ret;
4423}
4424
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04004425#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426
4427/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004428static int selinux_parse_skb_ipv6(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04004429 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430{
4431 u8 nexthdr;
4432 int ret = -EINVAL, offset;
4433 struct ipv6hdr _ipv6h, *ip6;
Jesse Gross75f28112011-11-30 17:05:51 -08004434 __be16 frag_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03004436 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
4438 if (ip6 == NULL)
4439 goto out;
4440
Eric Paris48c62af2012-04-02 13:15:44 -04004441 ad->u.net->v6info.saddr = ip6->saddr;
4442 ad->u.net->v6info.daddr = ip6->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 ret = 0;
4444
4445 nexthdr = ip6->nexthdr;
4446 offset += sizeof(_ipv6h);
Jesse Gross75f28112011-11-30 17:05:51 -08004447 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 if (offset < 0)
4449 goto out;
4450
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004451 if (proto)
4452 *proto = nexthdr;
4453
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 switch (nexthdr) {
4455 case IPPROTO_TCP: {
Eric Paris828dfe12008-04-17 13:17:49 -04004456 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457
4458 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4459 if (th == NULL)
4460 break;
4461
Eric Paris48c62af2012-04-02 13:15:44 -04004462 ad->u.net->sport = th->source;
4463 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464 break;
4465 }
4466
4467 case IPPROTO_UDP: {
4468 struct udphdr _udph, *uh;
4469
4470 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4471 if (uh == NULL)
4472 break;
4473
Eric Paris48c62af2012-04-02 13:15:44 -04004474 ad->u.net->sport = uh->source;
4475 ad->u.net->dport = uh->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 break;
4477 }
4478
James Morris2ee92d42006-11-13 16:09:01 -08004479 case IPPROTO_DCCP: {
4480 struct dccp_hdr _dccph, *dh;
4481
4482 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4483 if (dh == NULL)
4484 break;
4485
Eric Paris48c62af2012-04-02 13:15:44 -04004486 ad->u.net->sport = dh->dccph_sport;
4487 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08004488 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004489 }
James Morris2ee92d42006-11-13 16:09:01 -08004490
Richard Hainesd4529302018-02-13 20:57:18 +00004491#if IS_ENABLED(CONFIG_IP_SCTP)
4492 case IPPROTO_SCTP: {
4493 struct sctphdr _sctph, *sh;
4494
4495 sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4496 if (sh == NULL)
4497 break;
4498
4499 ad->u.net->sport = sh->source;
4500 ad->u.net->dport = sh->dest;
4501 break;
4502 }
4503#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504 /* includes fragments */
4505 default:
4506 break;
4507 }
4508out:
4509 return ret;
4510}
4511
4512#endif /* IPV6 */
4513
Thomas Liu2bf49692009-07-14 12:14:09 -04004514static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
David Howellscf9481e2008-07-27 21:31:07 +10004515 char **_addrp, int src, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516{
David Howellscf9481e2008-07-27 21:31:07 +10004517 char *addrp;
4518 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519
Eric Paris48c62af2012-04-02 13:15:44 -04004520 switch (ad->u.net->family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 case PF_INET:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004522 ret = selinux_parse_skb_ipv4(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10004523 if (ret)
4524 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04004525 addrp = (char *)(src ? &ad->u.net->v4info.saddr :
4526 &ad->u.net->v4info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10004527 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04004529#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 case PF_INET6:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004531 ret = selinux_parse_skb_ipv6(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10004532 if (ret)
4533 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04004534 addrp = (char *)(src ? &ad->u.net->v6info.saddr :
4535 &ad->u.net->v6info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10004536 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537#endif /* IPV6 */
4538 default:
David Howellscf9481e2008-07-27 21:31:07 +10004539 addrp = NULL;
4540 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541 }
4542
David Howellscf9481e2008-07-27 21:31:07 +10004543parse_error:
peter enderborgc103a912018-06-12 10:09:03 +02004544 pr_warn(
David Howellscf9481e2008-07-27 21:31:07 +10004545 "SELinux: failure in selinux_parse_skb(),"
4546 " unable to parse packet\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 return ret;
David Howellscf9481e2008-07-27 21:31:07 +10004548
4549okay:
4550 if (_addrp)
4551 *_addrp = addrp;
4552 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553}
4554
Paul Moore4f6a9932007-03-01 14:35:22 -05004555/**
Paul Moore220deb92008-01-29 08:38:23 -05004556 * selinux_skb_peerlbl_sid - Determine the peer label of a packet
Paul Moore4f6a9932007-03-01 14:35:22 -05004557 * @skb: the packet
Paul Moore75e22912008-01-29 08:38:04 -05004558 * @family: protocol family
Paul Moore220deb92008-01-29 08:38:23 -05004559 * @sid: the packet's peer label SID
Paul Moore4f6a9932007-03-01 14:35:22 -05004560 *
4561 * Description:
Paul Moore220deb92008-01-29 08:38:23 -05004562 * Check the various different forms of network peer labeling and determine
4563 * the peer label/SID for the packet; most of the magic actually occurs in
4564 * the security server function security_net_peersid_cmp(). The function
4565 * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
4566 * or -EACCES if @sid is invalid due to inconsistencies with the different
4567 * peer labels.
Paul Moore4f6a9932007-03-01 14:35:22 -05004568 *
4569 */
Paul Moore220deb92008-01-29 08:38:23 -05004570static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
Paul Moore4f6a9932007-03-01 14:35:22 -05004571{
Paul Moore71f1cb02008-01-29 08:51:16 -05004572 int err;
Paul Moore4f6a9932007-03-01 14:35:22 -05004573 u32 xfrm_sid;
4574 u32 nlbl_sid;
Paul Moore220deb92008-01-29 08:38:23 -05004575 u32 nlbl_type;
Paul Moore4f6a9932007-03-01 14:35:22 -05004576
Paul Moore817eff72013-12-10 14:57:54 -05004577 err = selinux_xfrm_skb_sid(skb, &xfrm_sid);
Paul Moorebed4d7e2013-07-23 17:38:40 -04004578 if (unlikely(err))
4579 return -EACCES;
4580 err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
4581 if (unlikely(err))
4582 return -EACCES;
Paul Moore220deb92008-01-29 08:38:23 -05004583
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004584 err = security_net_peersid_resolve(&selinux_state, nlbl_sid,
4585 nlbl_type, xfrm_sid, sid);
Paul Moore71f1cb02008-01-29 08:51:16 -05004586 if (unlikely(err)) {
peter enderborgc103a912018-06-12 10:09:03 +02004587 pr_warn(
Paul Moore71f1cb02008-01-29 08:51:16 -05004588 "SELinux: failure in selinux_skb_peerlbl_sid(),"
4589 " unable to determine packet's peer label\n");
Paul Moore220deb92008-01-29 08:38:23 -05004590 return -EACCES;
Paul Moore71f1cb02008-01-29 08:51:16 -05004591 }
Paul Moore220deb92008-01-29 08:38:23 -05004592
4593 return 0;
Paul Moore4f6a9932007-03-01 14:35:22 -05004594}
4595
Paul Moore446b8022013-12-04 16:10:51 -05004596/**
4597 * selinux_conn_sid - Determine the child socket label for a connection
4598 * @sk_sid: the parent socket's SID
4599 * @skb_sid: the packet's SID
4600 * @conn_sid: the resulting connection SID
4601 *
4602 * If @skb_sid is valid then the user:role:type information from @sk_sid is
4603 * combined with the MLS information from @skb_sid in order to create
Randy Dunlapc76a2f92020-08-07 09:51:34 -07004604 * @conn_sid. If @skb_sid is not valid then @conn_sid is simply a copy
Paul Moore446b8022013-12-04 16:10:51 -05004605 * of @sk_sid. Returns zero on success, negative values on failure.
4606 *
4607 */
4608static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid)
4609{
4610 int err = 0;
4611
4612 if (skb_sid != SECSID_NULL)
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004613 err = security_sid_mls_copy(&selinux_state, sk_sid, skb_sid,
4614 conn_sid);
Paul Moore446b8022013-12-04 16:10:51 -05004615 else
4616 *conn_sid = sk_sid;
4617
4618 return err;
4619}
4620
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621/* socket security operations */
Paul Moored4f2d972010-04-22 14:46:18 -04004622
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004623static int socket_sockcreate_sid(const struct task_security_struct *tsec,
4624 u16 secclass, u32 *socksid)
Paul Moored4f2d972010-04-22 14:46:18 -04004625{
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004626 if (tsec->sockcreate_sid > SECSID_NULL) {
4627 *socksid = tsec->sockcreate_sid;
4628 return 0;
4629 }
4630
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004631 return security_transition_sid(&selinux_state, tsec->sid, tsec->sid,
4632 secclass, NULL, socksid);
Paul Moored4f2d972010-04-22 14:46:18 -04004633}
4634
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004635static int sock_has_perm(struct sock *sk, u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636{
Paul Moore253bfae2010-04-22 14:46:19 -04004637 struct sk_security_struct *sksec = sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004638 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004639 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640
Paul Moore253bfae2010-04-22 14:46:19 -04004641 if (sksec->sid == SECINITSID_KERNEL)
4642 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643
Eric Paris50c205f2012-04-04 15:01:43 -04004644 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004645 ad.u.net = &net;
4646 ad.u.net->sk = sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004648 return avc_has_perm(&selinux_state,
4649 current_sid(), sksec->sid, sksec->sclass, perms,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004650 &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651}
4652
4653static int selinux_socket_create(int family, int type,
4654 int protocol, int kern)
4655{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07004656 const struct task_security_struct *tsec = selinux_cred(current_cred());
Paul Moored4f2d972010-04-22 14:46:18 -04004657 u32 newsid;
David Howells275bb412008-11-14 10:39:19 +11004658 u16 secclass;
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004659 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660
4661 if (kern)
Paul Moored4f2d972010-04-22 14:46:18 -04004662 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663
David Howells275bb412008-11-14 10:39:19 +11004664 secclass = socket_type_to_security_class(family, type, protocol);
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004665 rc = socket_sockcreate_sid(tsec, secclass, &newsid);
4666 if (rc)
4667 return rc;
4668
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004669 return avc_has_perm(&selinux_state,
4670 tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671}
4672
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07004673static int selinux_socket_post_create(struct socket *sock, int family,
4674 int type, int protocol, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07004676 const struct task_security_struct *tsec = selinux_cred(current_cred());
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05004677 struct inode_security_struct *isec = inode_security_novalidate(SOCK_INODE(sock));
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004678 struct sk_security_struct *sksec;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004679 u16 sclass = socket_type_to_security_class(family, type, protocol);
4680 u32 sid = SECINITSID_KERNEL;
David Howells275bb412008-11-14 10:39:19 +11004681 int err = 0;
4682
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004683 if (!kern) {
4684 err = socket_sockcreate_sid(tsec, sclass, &sid);
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004685 if (err)
4686 return err;
4687 }
David Howells275bb412008-11-14 10:39:19 +11004688
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004689 isec->sclass = sclass;
4690 isec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004691 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004693 if (sock->sk) {
4694 sksec = sock->sk->sk_security;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004695 sksec->sclass = sclass;
4696 sksec->sid = sid;
Richard Hainesd4529302018-02-13 20:57:18 +00004697 /* Allows detection of the first association on this socket */
4698 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4699 sksec->sctp_assoc_state = SCTP_ASSOC_UNSET;
4700
Paul Moore389fb8002009-03-27 17:10:34 -04004701 err = selinux_netlbl_socket_post_create(sock->sk, family);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004702 }
4703
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07004704 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705}
4706
David Herrmann0b811db2018-05-04 16:28:21 +02004707static int selinux_socket_socketpair(struct socket *socka,
4708 struct socket *sockb)
4709{
4710 struct sk_security_struct *sksec_a = socka->sk->sk_security;
4711 struct sk_security_struct *sksec_b = sockb->sk->sk_security;
4712
4713 sksec_a->peer_sid = sksec_b->sid;
4714 sksec_b->peer_sid = sksec_a->sid;
4715
4716 return 0;
4717}
4718
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719/* Range of port numbers used to automatically bind.
4720 Need to determine whether we should perform a name_bind
4721 permission check between the socket and the port number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722
4723static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
4724{
Paul Moore253bfae2010-04-22 14:46:19 -04004725 struct sock *sk = sock->sk;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004726 struct sk_security_struct *sksec = sk->sk_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727 u16 family;
4728 int err;
4729
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004730 err = sock_has_perm(sk, SOCKET__BIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731 if (err)
4732 goto out;
4733
Richard Hainesd4529302018-02-13 20:57:18 +00004734 /* If PF_INET or PF_INET6, check name_bind permission for the port. */
Paul Moore253bfae2010-04-22 14:46:19 -04004735 family = sk->sk_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 if (family == PF_INET || family == PF_INET6) {
4737 char *addrp;
Thomas Liu2bf49692009-07-14 12:14:09 -04004738 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004739 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 struct sockaddr_in *addr4 = NULL;
4741 struct sockaddr_in6 *addr6 = NULL;
Tetsuo Handac750e692019-04-12 19:59:34 +09004742 u16 family_sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743 unsigned short snum;
James Morrise399f982008-06-12 01:39:58 +10004744 u32 sid, node_perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745
Richard Hainesd4529302018-02-13 20:57:18 +00004746 /*
4747 * sctp_bindx(3) calls via selinux_sctp_bind_connect()
4748 * that validates multiple binding addresses. Because of this
4749 * need to check address->sa_family as it is possible to have
4750 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4751 */
Tetsuo Handac750e692019-04-12 19:59:34 +09004752 if (addrlen < offsetofend(struct sockaddr, sa_family))
4753 return -EINVAL;
4754 family_sa = address->sa_family;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004755 switch (family_sa) {
4756 case AF_UNSPEC:
Richard Haines68741a8a2018-03-02 19:54:34 +00004757 case AF_INET:
4758 if (addrlen < sizeof(struct sockaddr_in))
4759 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 addr4 = (struct sockaddr_in *)address;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004761 if (family_sa == AF_UNSPEC) {
4762 /* see __inet_bind(), we only want to allow
4763 * AF_UNSPEC if the address is INADDR_ANY
4764 */
4765 if (addr4->sin_addr.s_addr != htonl(INADDR_ANY))
4766 goto err_af;
4767 family_sa = AF_INET;
4768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 snum = ntohs(addr4->sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770 addrp = (char *)&addr4->sin_addr.s_addr;
Richard Haines68741a8a2018-03-02 19:54:34 +00004771 break;
4772 case AF_INET6:
4773 if (addrlen < SIN6_LEN_RFC2133)
4774 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 addr6 = (struct sockaddr_in6 *)address;
4776 snum = ntohs(addr6->sin6_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 addrp = (char *)&addr6->sin6_addr.s6_addr;
Richard Haines68741a8a2018-03-02 19:54:34 +00004778 break;
4779 default:
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004780 goto err_af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 }
4782
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004783 ad.type = LSM_AUDIT_DATA_NET;
4784 ad.u.net = &net;
4785 ad.u.net->sport = htons(snum);
4786 ad.u.net->family = family_sa;
4787
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004788 if (snum) {
4789 int low, high;
4790
Eric W. Biederman0bbf87d2013-09-28 14:10:59 -07004791 inet_get_local_port_range(sock_net(sk), &low, &high);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004792
Maciej Żenczykowski82f31eb2019-11-25 15:37:04 -08004793 if (inet_port_requires_bind_service(sock_net(sk), snum) ||
4794 snum < low || snum > high) {
Paul Moore3e112172008-04-10 10:48:14 -04004795 err = sel_netport_sid(sk->sk_protocol,
4796 snum, &sid);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004797 if (err)
4798 goto out;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004799 err = avc_has_perm(&selinux_state,
4800 sksec->sid, sid,
Paul Moore253bfae2010-04-22 14:46:19 -04004801 sksec->sclass,
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004802 SOCKET__NAME_BIND, &ad);
4803 if (err)
4804 goto out;
4805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806 }
Eric Paris828dfe12008-04-17 13:17:49 -04004807
Paul Moore253bfae2010-04-22 14:46:19 -04004808 switch (sksec->sclass) {
James Morris13402582005-09-30 14:24:34 -04004809 case SECCLASS_TCP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810 node_perm = TCP_SOCKET__NODE_BIND;
4811 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004812
James Morris13402582005-09-30 14:24:34 -04004813 case SECCLASS_UDP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814 node_perm = UDP_SOCKET__NODE_BIND;
4815 break;
James Morris2ee92d42006-11-13 16:09:01 -08004816
4817 case SECCLASS_DCCP_SOCKET:
4818 node_perm = DCCP_SOCKET__NODE_BIND;
4819 break;
4820
Richard Hainesd4529302018-02-13 20:57:18 +00004821 case SECCLASS_SCTP_SOCKET:
4822 node_perm = SCTP_SOCKET__NODE_BIND;
4823 break;
4824
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825 default:
4826 node_perm = RAWIP_SOCKET__NODE_BIND;
4827 break;
4828 }
Eric Paris828dfe12008-04-17 13:17:49 -04004829
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004830 err = sel_netnode_sid(addrp, family_sa, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 if (err)
4832 goto out;
Eric Paris828dfe12008-04-17 13:17:49 -04004833
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004834 if (family_sa == AF_INET)
Eric Paris48c62af2012-04-02 13:15:44 -04004835 ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836 else
Eric Paris48c62af2012-04-02 13:15:44 -04004837 ad.u.net->v6info.saddr = addr6->sin6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004839 err = avc_has_perm(&selinux_state,
4840 sksec->sid, sid,
Paul Moore253bfae2010-04-22 14:46:19 -04004841 sksec->sclass, node_perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842 if (err)
4843 goto out;
4844 }
4845out:
4846 return err;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004847err_af:
4848 /* Note that SCTP services expect -EINVAL, others -EAFNOSUPPORT. */
4849 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4850 return -EINVAL;
4851 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852}
4853
Richard Hainesd4529302018-02-13 20:57:18 +00004854/* This supports connect(2) and SCTP connect services such as sctp_connectx(3)
Kees Cookd61330c2019-02-17 14:08:36 -08004855 * and sctp_sendmsg(3) as described in Documentation/security/SCTP.rst
Richard Hainesd4529302018-02-13 20:57:18 +00004856 */
4857static int selinux_socket_connect_helper(struct socket *sock,
4858 struct sockaddr *address, int addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859{
Paul Moore014ab192008-10-10 10:16:33 -04004860 struct sock *sk = sock->sk;
Paul Moore253bfae2010-04-22 14:46:19 -04004861 struct sk_security_struct *sksec = sk->sk_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 int err;
4863
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004864 err = sock_has_perm(sk, SOCKET__CONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865 if (err)
4866 return err;
Paolo Abeni05174c92019-05-10 19:12:33 +02004867 if (addrlen < offsetofend(struct sockaddr, sa_family))
4868 return -EINVAL;
4869
4870 /* connect(AF_UNSPEC) has special handling, as it is a documented
4871 * way to disconnect the socket
4872 */
4873 if (address->sa_family == AF_UNSPEC)
4874 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875
4876 /*
Richard Hainesd4529302018-02-13 20:57:18 +00004877 * If a TCP, DCCP or SCTP socket, check name_connect permission
4878 * for the port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879 */
Paul Moore253bfae2010-04-22 14:46:19 -04004880 if (sksec->sclass == SECCLASS_TCP_SOCKET ||
Richard Hainesd4529302018-02-13 20:57:18 +00004881 sksec->sclass == SECCLASS_DCCP_SOCKET ||
4882 sksec->sclass == SECCLASS_SCTP_SOCKET) {
Thomas Liu2bf49692009-07-14 12:14:09 -04004883 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004884 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885 struct sockaddr_in *addr4 = NULL;
4886 struct sockaddr_in6 *addr6 = NULL;
4887 unsigned short snum;
James Morris2ee92d42006-11-13 16:09:01 -08004888 u32 sid, perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889
Richard Hainesd4529302018-02-13 20:57:18 +00004890 /* sctp_connectx(3) calls via selinux_sctp_bind_connect()
4891 * that validates multiple connect addresses. Because of this
4892 * need to check address->sa_family as it is possible to have
4893 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4894 */
Richard Haines68741a8a2018-03-02 19:54:34 +00004895 switch (address->sa_family) {
4896 case AF_INET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 addr4 = (struct sockaddr_in *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004898 if (addrlen < sizeof(struct sockaddr_in))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899 return -EINVAL;
4900 snum = ntohs(addr4->sin_port);
Richard Haines68741a8a2018-03-02 19:54:34 +00004901 break;
4902 case AF_INET6:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903 addr6 = (struct sockaddr_in6 *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004904 if (addrlen < SIN6_LEN_RFC2133)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 return -EINVAL;
4906 snum = ntohs(addr6->sin6_port);
Richard Haines68741a8a2018-03-02 19:54:34 +00004907 break;
4908 default:
4909 /* Note that SCTP services expect -EINVAL, whereas
4910 * others expect -EAFNOSUPPORT.
4911 */
4912 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4913 return -EINVAL;
4914 else
4915 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 }
4917
Paul Moore3e112172008-04-10 10:48:14 -04004918 err = sel_netport_sid(sk->sk_protocol, snum, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 if (err)
Richard Hainesd4529302018-02-13 20:57:18 +00004920 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921
Richard Hainesd4529302018-02-13 20:57:18 +00004922 switch (sksec->sclass) {
4923 case SECCLASS_TCP_SOCKET:
4924 perm = TCP_SOCKET__NAME_CONNECT;
4925 break;
4926 case SECCLASS_DCCP_SOCKET:
4927 perm = DCCP_SOCKET__NAME_CONNECT;
4928 break;
4929 case SECCLASS_SCTP_SOCKET:
4930 perm = SCTP_SOCKET__NAME_CONNECT;
4931 break;
4932 }
James Morris2ee92d42006-11-13 16:09:01 -08004933
Eric Paris50c205f2012-04-04 15:01:43 -04004934 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004935 ad.u.net = &net;
4936 ad.u.net->dport = htons(snum);
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004937 ad.u.net->family = address->sa_family;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05004938 err = avc_has_perm(&selinux_state,
4939 sksec->sid, sid, sksec->sclass, perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 if (err)
Richard Hainesd4529302018-02-13 20:57:18 +00004941 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 }
4943
Richard Hainesd4529302018-02-13 20:57:18 +00004944 return 0;
4945}
Paul Moore014ab192008-10-10 10:16:33 -04004946
Richard Hainesd4529302018-02-13 20:57:18 +00004947/* Supports connect(2), see comments in selinux_socket_connect_helper() */
4948static int selinux_socket_connect(struct socket *sock,
4949 struct sockaddr *address, int addrlen)
4950{
4951 int err;
4952 struct sock *sk = sock->sk;
4953
4954 err = selinux_socket_connect_helper(sock, address, addrlen);
4955 if (err)
4956 return err;
4957
4958 return selinux_netlbl_socket_connect(sk, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959}
4960
4961static int selinux_socket_listen(struct socket *sock, int backlog)
4962{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004963 return sock_has_perm(sock->sk, SOCKET__LISTEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964}
4965
4966static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
4967{
4968 int err;
4969 struct inode_security_struct *isec;
4970 struct inode_security_struct *newisec;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004971 u16 sclass;
4972 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004974 err = sock_has_perm(sock->sk, SOCKET__ACCEPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975 if (err)
4976 return err;
4977
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05004978 isec = inode_security_novalidate(SOCK_INODE(sock));
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004979 spin_lock(&isec->lock);
4980 sclass = isec->sclass;
4981 sid = isec->sid;
4982 spin_unlock(&isec->lock);
4983
4984 newisec = inode_security_novalidate(SOCK_INODE(newsock));
4985 newisec->sclass = sclass;
4986 newisec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004987 newisec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988
4989 return 0;
4990}
4991
4992static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
Eric Paris828dfe12008-04-17 13:17:49 -04004993 int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004995 return sock_has_perm(sock->sk, SOCKET__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996}
4997
4998static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
4999 int size, int flags)
5000{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005001 return sock_has_perm(sock->sk, SOCKET__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002}
5003
5004static int selinux_socket_getsockname(struct socket *sock)
5005{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005006 return sock_has_perm(sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007}
5008
5009static int selinux_socket_getpeername(struct socket *sock)
5010{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005011 return sock_has_perm(sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012}
5013
Eric Paris828dfe12008-04-17 13:17:49 -04005014static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015{
Paul Mooref8687af2006-10-30 15:22:15 -08005016 int err;
5017
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005018 err = sock_has_perm(sock->sk, SOCKET__SETOPT);
Paul Mooref8687af2006-10-30 15:22:15 -08005019 if (err)
5020 return err;
5021
5022 return selinux_netlbl_socket_setsockopt(sock, level, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023}
5024
5025static int selinux_socket_getsockopt(struct socket *sock, int level,
5026 int optname)
5027{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005028 return sock_has_perm(sock->sk, SOCKET__GETOPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029}
5030
5031static int selinux_socket_shutdown(struct socket *sock, int how)
5032{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005033 return sock_has_perm(sock->sk, SOCKET__SHUTDOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034}
5035
David S. Miller3610cda2011-01-05 15:38:53 -08005036static int selinux_socket_unix_stream_connect(struct sock *sock,
5037 struct sock *other,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 struct sock *newsk)
5039{
David S. Miller3610cda2011-01-05 15:38:53 -08005040 struct sk_security_struct *sksec_sock = sock->sk_security;
5041 struct sk_security_struct *sksec_other = other->sk_security;
Paul Moore4d1e2452010-04-22 14:46:18 -04005042 struct sk_security_struct *sksec_new = newsk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04005043 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005044 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045 int err;
5046
Eric Paris50c205f2012-04-04 15:01:43 -04005047 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005048 ad.u.net = &net;
5049 ad.u.net->sk = other;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005051 err = avc_has_perm(&selinux_state,
5052 sksec_sock->sid, sksec_other->sid,
Paul Moore4d1e2452010-04-22 14:46:18 -04005053 sksec_other->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054 UNIX_STREAM_SOCKET__CONNECTTO, &ad);
5055 if (err)
5056 return err;
5057
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058 /* server child socket */
Paul Moore4d1e2452010-04-22 14:46:18 -04005059 sksec_new->peer_sid = sksec_sock->sid;
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005060 err = security_sid_mls_copy(&selinux_state, sksec_other->sid,
5061 sksec_sock->sid, &sksec_new->sid);
Paul Moore4d1e2452010-04-22 14:46:18 -04005062 if (err)
5063 return err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005064
Paul Moore4d1e2452010-04-22 14:46:18 -04005065 /* connecting socket */
5066 sksec_sock->peer_sid = sksec_new->sid;
5067
5068 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069}
5070
5071static int selinux_socket_unix_may_send(struct socket *sock,
5072 struct socket *other)
5073{
Paul Moore253bfae2010-04-22 14:46:19 -04005074 struct sk_security_struct *ssec = sock->sk->sk_security;
5075 struct sk_security_struct *osec = other->sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04005076 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005077 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078
Eric Paris50c205f2012-04-04 15:01:43 -04005079 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005080 ad.u.net = &net;
5081 ad.u.net->sk = other->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005083 return avc_has_perm(&selinux_state,
5084 ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
Paul Moore253bfae2010-04-22 14:46:19 -04005085 &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086}
5087
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005088static int selinux_inet_sys_rcv_skb(struct net *ns, int ifindex,
5089 char *addrp, u16 family, u32 peer_sid,
Thomas Liu2bf49692009-07-14 12:14:09 -04005090 struct common_audit_data *ad)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005091{
5092 int err;
5093 u32 if_sid;
5094 u32 node_sid;
5095
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005096 err = sel_netif_sid(ns, ifindex, &if_sid);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005097 if (err)
5098 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005099 err = avc_has_perm(&selinux_state,
5100 peer_sid, if_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005101 SECCLASS_NETIF, NETIF__INGRESS, ad);
5102 if (err)
5103 return err;
5104
5105 err = sel_netnode_sid(addrp, family, &node_sid);
5106 if (err)
5107 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005108 return avc_has_perm(&selinux_state,
5109 peer_sid, node_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005110 SECCLASS_NODE, NODE__RECVFROM, ad);
5111}
5112
Paul Moore220deb92008-01-29 08:38:23 -05005113static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
Paul Moored8395c82008-10-10 10:16:30 -04005114 u16 family)
Paul Moore220deb92008-01-29 08:38:23 -05005115{
Paul Moore277d3422008-12-31 12:54:11 -05005116 int err = 0;
Paul Moore220deb92008-01-29 08:38:23 -05005117 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05005118 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04005119 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005120 struct lsm_network_audit net = {0,};
Paul Moored8395c82008-10-10 10:16:30 -04005121 char *addrp;
5122
Eric Paris50c205f2012-04-04 15:01:43 -04005123 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005124 ad.u.net = &net;
5125 ad.u.net->netif = skb->skb_iif;
5126 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04005127 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
5128 if (err)
5129 return err;
Paul Moore220deb92008-01-29 08:38:23 -05005130
Paul Moore58bfbb52009-03-27 17:10:41 -04005131 if (selinux_secmark_enabled()) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005132 err = avc_has_perm(&selinux_state,
5133 sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Moored8395c82008-10-10 10:16:30 -04005134 PACKET__RECV, &ad);
Paul Moore58bfbb52009-03-27 17:10:41 -04005135 if (err)
5136 return err;
5137 }
Paul Moore220deb92008-01-29 08:38:23 -05005138
Steffen Klassertb9679a72011-02-23 12:55:21 +01005139 err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
5140 if (err)
5141 return err;
5142 err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005143
James Morris4e5ab4c2006-06-09 00:33:33 -07005144 return err;
5145}
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005146
James Morris4e5ab4c2006-06-09 00:33:33 -07005147static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
5148{
Paul Moore220deb92008-01-29 08:38:23 -05005149 int err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005150 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05005151 u16 family = sk->sk_family;
5152 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04005153 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005154 struct lsm_network_audit net = {0,};
Paul Moore220deb92008-01-29 08:38:23 -05005155 char *addrp;
Paul Moored8395c82008-10-10 10:16:30 -04005156 u8 secmark_active;
5157 u8 peerlbl_active;
James Morris4e5ab4c2006-06-09 00:33:33 -07005158
James Morris4e5ab4c2006-06-09 00:33:33 -07005159 if (family != PF_INET && family != PF_INET6)
Paul Moore220deb92008-01-29 08:38:23 -05005160 return 0;
James Morris4e5ab4c2006-06-09 00:33:33 -07005161
5162 /* Handle mapped IPv4 packets arriving via IPv6 sockets */
Al Viro87fcd702006-12-04 22:00:55 +00005163 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
James Morris4e5ab4c2006-06-09 00:33:33 -07005164 family = PF_INET;
5165
Paul Moored8395c82008-10-10 10:16:30 -04005166 /* If any sort of compatibility mode is enabled then handoff processing
5167 * to the selinux_sock_rcv_skb_compat() function to deal with the
5168 * special handling. We do this in an attempt to keep this function
5169 * as fast and as clean as possible. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005170 if (!selinux_policycap_netpeer())
Paul Moored8395c82008-10-10 10:16:30 -04005171 return selinux_sock_rcv_skb_compat(sk, skb, family);
5172
5173 secmark_active = selinux_secmark_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04005174 peerlbl_active = selinux_peerlbl_enabled();
Paul Moored8395c82008-10-10 10:16:30 -04005175 if (!secmark_active && !peerlbl_active)
5176 return 0;
5177
Eric Paris50c205f2012-04-04 15:01:43 -04005178 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005179 ad.u.net = &net;
5180 ad.u.net->netif = skb->skb_iif;
5181 ad.u.net->family = family;
Paul Moore224dfbd2008-01-29 08:38:13 -05005182 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
James Morris4e5ab4c2006-06-09 00:33:33 -07005183 if (err)
Paul Moore220deb92008-01-29 08:38:23 -05005184 return err;
James Morris4e5ab4c2006-06-09 00:33:33 -07005185
Paul Moored8395c82008-10-10 10:16:30 -04005186 if (peerlbl_active) {
Paul Moored621d352008-01-29 08:43:36 -05005187 u32 peer_sid;
5188
5189 err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
5190 if (err)
5191 return err;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005192 err = selinux_inet_sys_rcv_skb(sock_net(sk), skb->skb_iif,
5193 addrp, family, peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04005194 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04005195 selinux_netlbl_err(skb, family, err, 0);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005196 return err;
Paul Mooredfaebe92008-10-10 10:16:31 -04005197 }
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005198 err = avc_has_perm(&selinux_state,
5199 sk_sid, peer_sid, SECCLASS_PEER,
Paul Moored621d352008-01-29 08:43:36 -05005200 PEER__RECV, &ad);
Chad Hanson46d01d62013-12-23 17:45:01 -05005201 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04005202 selinux_netlbl_err(skb, family, err, 0);
Chad Hanson46d01d62013-12-23 17:45:01 -05005203 return err;
5204 }
Paul Moored621d352008-01-29 08:43:36 -05005205 }
5206
Paul Moored8395c82008-10-10 10:16:30 -04005207 if (secmark_active) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005208 err = avc_has_perm(&selinux_state,
5209 sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005210 PACKET__RECV, &ad);
5211 if (err)
5212 return err;
5213 }
5214
Paul Moored621d352008-01-29 08:43:36 -05005215 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216}
5217
Catherine Zhang2c7946a2006-03-20 22:41:23 -08005218static int selinux_socket_getpeersec_stream(struct socket *sock, char __user *optval,
5219 int __user *optlen, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220{
5221 int err = 0;
5222 char *scontext;
5223 u32 scontext_len;
Paul Moore253bfae2010-04-22 14:46:19 -04005224 struct sk_security_struct *sksec = sock->sk->sk_security;
Paul Moore3de4bab2006-11-17 17:38:54 -05005225 u32 peer_sid = SECSID_NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226
Paul Moore253bfae2010-04-22 14:46:19 -04005227 if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
Richard Hainesd4529302018-02-13 20:57:18 +00005228 sksec->sclass == SECCLASS_TCP_SOCKET ||
5229 sksec->sclass == SECCLASS_SCTP_SOCKET)
Eric Parisdd3e7832010-04-07 15:08:46 -04005230 peer_sid = sksec->peer_sid;
Paul Moore253bfae2010-04-22 14:46:19 -04005231 if (peer_sid == SECSID_NULL)
5232 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005234 err = security_sid_to_context(&selinux_state, peer_sid, &scontext,
5235 &scontext_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236 if (err)
Paul Moore253bfae2010-04-22 14:46:19 -04005237 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238
5239 if (scontext_len > len) {
5240 err = -ERANGE;
5241 goto out_len;
5242 }
5243
5244 if (copy_to_user(optval, scontext, scontext_len))
5245 err = -EFAULT;
5246
5247out_len:
5248 if (put_user(scontext_len, optlen))
5249 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250 kfree(scontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251 return err;
5252}
5253
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005254static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid)
Catherine Zhang2c7946a2006-03-20 22:41:23 -08005255{
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005256 u32 peer_secid = SECSID_NULL;
Paul Moore75e22912008-01-29 08:38:04 -05005257 u16 family;
Paul Moore899134f2016-03-28 15:19:10 -04005258 struct inode_security_struct *isec;
Catherine Zhang877ce7c2006-06-29 12:27:47 -07005259
Paul Mooreaa862902008-10-10 10:16:29 -04005260 if (skb && skb->protocol == htons(ETH_P_IP))
5261 family = PF_INET;
5262 else if (skb && skb->protocol == htons(ETH_P_IPV6))
5263 family = PF_INET6;
5264 else if (sock)
Paul Moore75e22912008-01-29 08:38:04 -05005265 family = sock->sk->sk_family;
Paul Moore75e22912008-01-29 08:38:04 -05005266 else
5267 goto out;
5268
Paul Moore899134f2016-03-28 15:19:10 -04005269 if (sock && family == PF_UNIX) {
5270 isec = inode_security_novalidate(SOCK_INODE(sock));
5271 peer_secid = isec->sid;
5272 } else if (skb)
Paul Moore220deb92008-01-29 08:38:23 -05005273 selinux_skb_peerlbl_sid(skb, family, &peer_secid);
Catherine Zhang2c7946a2006-03-20 22:41:23 -08005274
Paul Moore75e22912008-01-29 08:38:04 -05005275out:
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005276 *secid = peer_secid;
Paul Moore75e22912008-01-29 08:38:04 -05005277 if (peer_secid == SECSID_NULL)
5278 return -EINVAL;
5279 return 0;
Catherine Zhang2c7946a2006-03-20 22:41:23 -08005280}
5281
Al Viro7d877f32005-10-21 03:20:43 -04005282static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283{
Paul Moore84914b72010-04-22 14:46:18 -04005284 struct sk_security_struct *sksec;
5285
5286 sksec = kzalloc(sizeof(*sksec), priority);
5287 if (!sksec)
5288 return -ENOMEM;
5289
5290 sksec->peer_sid = SECINITSID_UNLABELED;
5291 sksec->sid = SECINITSID_UNLABELED;
Stephen Smalley5dee25d2015-07-10 17:19:57 -04005292 sksec->sclass = SECCLASS_SOCKET;
Paul Moore84914b72010-04-22 14:46:18 -04005293 selinux_netlbl_sk_security_reset(sksec);
5294 sk->sk_security = sksec;
5295
5296 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297}
5298
5299static void selinux_sk_free_security(struct sock *sk)
5300{
Paul Moore84914b72010-04-22 14:46:18 -04005301 struct sk_security_struct *sksec = sk->sk_security;
5302
5303 sk->sk_security = NULL;
5304 selinux_netlbl_sk_security_free(sksec);
5305 kfree(sksec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306}
5307
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005308static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
5309{
Eric Parisdd3e7832010-04-07 15:08:46 -04005310 struct sk_security_struct *sksec = sk->sk_security;
5311 struct sk_security_struct *newsksec = newsk->sk_security;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005312
Eric Parisdd3e7832010-04-07 15:08:46 -04005313 newsksec->sid = sksec->sid;
5314 newsksec->peer_sid = sksec->peer_sid;
5315 newsksec->sclass = sksec->sclass;
Paul Moore99f59ed2006-08-29 17:53:48 -07005316
Eric Parisdd3e7832010-04-07 15:08:46 -04005317 selinux_netlbl_sk_security_reset(newsksec);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005318}
5319
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07005320static void selinux_sk_getsecid(struct sock *sk, u32 *secid)
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005321{
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005322 if (!sk)
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07005323 *secid = SECINITSID_ANY_SOCKET;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005324 else {
5325 struct sk_security_struct *sksec = sk->sk_security;
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005326
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07005327 *secid = sksec->sid;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005328 }
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005329}
5330
Eric Paris828dfe12008-04-17 13:17:49 -04005331static void selinux_sock_graft(struct sock *sk, struct socket *parent)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005332{
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05005333 struct inode_security_struct *isec =
5334 inode_security_novalidate(SOCK_INODE(parent));
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005335 struct sk_security_struct *sksec = sk->sk_security;
5336
Paul Moore2873ead2014-07-28 10:42:48 -04005337 if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
5338 sk->sk_family == PF_UNIX)
David Woodhouse2148ccc2006-09-29 15:50:25 -07005339 isec->sid = sksec->sid;
Paul Moore220deb92008-01-29 08:38:23 -05005340 sksec->sclass = isec->sclass;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005341}
5342
Richard Hainesd4529302018-02-13 20:57:18 +00005343/* Called whenever SCTP receives an INIT chunk. This happens when an incoming
5344 * connect(2), sctp_connectx(3) or sctp_sendmsg(3) (with no association
5345 * already present).
5346 */
Xin Longc081d532021-11-02 08:02:47 -04005347static int selinux_sctp_assoc_request(struct sctp_association *asoc,
Richard Hainesd4529302018-02-13 20:57:18 +00005348 struct sk_buff *skb)
5349{
Xin Longc081d532021-11-02 08:02:47 -04005350 struct sk_security_struct *sksec = asoc->base.sk->sk_security;
Richard Hainesd4529302018-02-13 20:57:18 +00005351 struct common_audit_data ad;
5352 struct lsm_network_audit net = {0,};
5353 u8 peerlbl_active;
5354 u32 peer_sid = SECINITSID_UNLABELED;
5355 u32 conn_sid;
5356 int err = 0;
5357
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005358 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005359 return 0;
5360
5361 peerlbl_active = selinux_peerlbl_enabled();
5362
5363 if (peerlbl_active) {
5364 /* This will return peer_sid = SECSID_NULL if there are
5365 * no peer labels, see security_net_peersid_resolve().
5366 */
Xin Longc081d532021-11-02 08:02:47 -04005367 err = selinux_skb_peerlbl_sid(skb, asoc->base.sk->sk_family,
Richard Hainesd4529302018-02-13 20:57:18 +00005368 &peer_sid);
5369 if (err)
5370 return err;
5371
5372 if (peer_sid == SECSID_NULL)
5373 peer_sid = SECINITSID_UNLABELED;
5374 }
5375
5376 if (sksec->sctp_assoc_state == SCTP_ASSOC_UNSET) {
5377 sksec->sctp_assoc_state = SCTP_ASSOC_SET;
5378
5379 /* Here as first association on socket. As the peer SID
5380 * was allowed by peer recv (and the netif/node checks),
5381 * then it is approved by policy and used as the primary
5382 * peer SID for getpeercon(3).
5383 */
5384 sksec->peer_sid = peer_sid;
5385 } else if (sksec->peer_sid != peer_sid) {
5386 /* Other association peer SIDs are checked to enforce
5387 * consistency among the peer SIDs.
5388 */
5389 ad.type = LSM_AUDIT_DATA_NET;
5390 ad.u.net = &net;
Xin Longc081d532021-11-02 08:02:47 -04005391 ad.u.net->sk = asoc->base.sk;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005392 err = avc_has_perm(&selinux_state,
5393 sksec->peer_sid, peer_sid, sksec->sclass,
Richard Hainesd4529302018-02-13 20:57:18 +00005394 SCTP_SOCKET__ASSOCIATION, &ad);
5395 if (err)
5396 return err;
5397 }
5398
5399 /* Compute the MLS component for the connection and store
Xin Longc081d532021-11-02 08:02:47 -04005400 * the information in asoc. This will be used by SCTP TCP type
Richard Hainesd4529302018-02-13 20:57:18 +00005401 * sockets and peeled off connections as they cause a new
5402 * socket to be generated. selinux_sctp_sk_clone() will then
5403 * plug this into the new socket.
5404 */
5405 err = selinux_conn_sid(sksec->sid, peer_sid, &conn_sid);
5406 if (err)
5407 return err;
5408
Xin Longc081d532021-11-02 08:02:47 -04005409 asoc->secid = conn_sid;
5410 asoc->peer_secid = peer_sid;
Richard Hainesd4529302018-02-13 20:57:18 +00005411
5412 /* Set any NetLabel labels including CIPSO/CALIPSO options. */
Xin Longc081d532021-11-02 08:02:47 -04005413 return selinux_netlbl_sctp_assoc_request(asoc, skb);
Richard Hainesd4529302018-02-13 20:57:18 +00005414}
5415
5416/* Check if sctp IPv4/IPv6 addresses are valid for binding or connecting
5417 * based on their @optname.
5418 */
5419static int selinux_sctp_bind_connect(struct sock *sk, int optname,
5420 struct sockaddr *address,
5421 int addrlen)
5422{
5423 int len, err = 0, walk_size = 0;
5424 void *addr_buf;
5425 struct sockaddr *addr;
5426 struct socket *sock;
5427
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005428 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005429 return 0;
5430
5431 /* Process one or more addresses that may be IPv4 or IPv6 */
5432 sock = sk->sk_socket;
5433 addr_buf = address;
5434
5435 while (walk_size < addrlen) {
Ondrej Mosnacekc1383252018-11-13 16:16:08 +01005436 if (walk_size + sizeof(sa_family_t) > addrlen)
5437 return -EINVAL;
5438
Richard Hainesd4529302018-02-13 20:57:18 +00005439 addr = addr_buf;
5440 switch (addr->sa_family) {
Alexey Kodanev4152dc92018-05-11 20:15:13 +03005441 case AF_UNSPEC:
Richard Hainesd4529302018-02-13 20:57:18 +00005442 case AF_INET:
5443 len = sizeof(struct sockaddr_in);
5444 break;
5445 case AF_INET6:
5446 len = sizeof(struct sockaddr_in6);
5447 break;
5448 default:
Alexey Kodanev4152dc92018-05-11 20:15:13 +03005449 return -EINVAL;
Richard Hainesd4529302018-02-13 20:57:18 +00005450 }
5451
Xin Long292c9972019-03-09 00:07:34 +08005452 if (walk_size + len > addrlen)
5453 return -EINVAL;
5454
Richard Hainesd4529302018-02-13 20:57:18 +00005455 err = -EINVAL;
5456 switch (optname) {
5457 /* Bind checks */
5458 case SCTP_PRIMARY_ADDR:
5459 case SCTP_SET_PEER_PRIMARY_ADDR:
5460 case SCTP_SOCKOPT_BINDX_ADD:
5461 err = selinux_socket_bind(sock, addr, len);
5462 break;
5463 /* Connect checks */
5464 case SCTP_SOCKOPT_CONNECTX:
5465 case SCTP_PARAM_SET_PRIMARY:
5466 case SCTP_PARAM_ADD_IP:
5467 case SCTP_SENDMSG_CONNECT:
5468 err = selinux_socket_connect_helper(sock, addr, len);
5469 if (err)
5470 return err;
5471
5472 /* As selinux_sctp_bind_connect() is called by the
5473 * SCTP protocol layer, the socket is already locked,
Randy Dunlapc76a2f92020-08-07 09:51:34 -07005474 * therefore selinux_netlbl_socket_connect_locked()
Richard Hainesd4529302018-02-13 20:57:18 +00005475 * is called here. The situations handled are:
5476 * sctp_connectx(3), sctp_sendmsg(3), sendmsg(2),
5477 * whenever a new IP address is added or when a new
5478 * primary address is selected.
5479 * Note that an SCTP connect(2) call happens before
5480 * the SCTP protocol layer and is handled via
5481 * selinux_socket_connect().
5482 */
5483 err = selinux_netlbl_socket_connect_locked(sk, addr);
5484 break;
5485 }
5486
5487 if (err)
5488 return err;
5489
5490 addr_buf += len;
5491 walk_size += len;
5492 }
5493
5494 return 0;
5495}
5496
5497/* Called whenever a new socket is created by accept(2) or sctp_peeloff(3). */
Xin Longc081d532021-11-02 08:02:47 -04005498static void selinux_sctp_sk_clone(struct sctp_association *asoc, struct sock *sk,
Richard Hainesd4529302018-02-13 20:57:18 +00005499 struct sock *newsk)
5500{
5501 struct sk_security_struct *sksec = sk->sk_security;
5502 struct sk_security_struct *newsksec = newsk->sk_security;
5503
5504 /* If policy does not support SECCLASS_SCTP_SOCKET then call
5505 * the non-sctp clone version.
5506 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005507 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005508 return selinux_sk_clone_security(sk, newsk);
5509
Paul Moore32a370a2021-11-12 12:07:02 -05005510 newsksec->sid = asoc->secid;
Xin Longc081d532021-11-02 08:02:47 -04005511 newsksec->peer_sid = asoc->peer_secid;
Richard Hainesd4529302018-02-13 20:57:18 +00005512 newsksec->sclass = sksec->sclass;
5513 selinux_netlbl_sctp_sk_clone(sk, newsk);
5514}
5515
Florian Westphal41dd9592020-11-30 16:36:29 +01005516static int selinux_inet_conn_request(const struct sock *sk, struct sk_buff *skb,
Adrian Bunk9a673e52006-08-15 00:03:53 -07005517 struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005518{
5519 struct sk_security_struct *sksec = sk->sk_security;
5520 int err;
Paul Moore0b1f24e2013-12-03 11:39:13 -05005521 u16 family = req->rsk_ops->family;
Paul Moore446b8022013-12-04 16:10:51 -05005522 u32 connsid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005523 u32 peersid;
5524
Paul Mooreaa862902008-10-10 10:16:29 -04005525 err = selinux_skb_peerlbl_sid(skb, family, &peersid);
Paul Moore220deb92008-01-29 08:38:23 -05005526 if (err)
5527 return err;
Paul Moore446b8022013-12-04 16:10:51 -05005528 err = selinux_conn_sid(sksec->sid, peersid, &connsid);
5529 if (err)
5530 return err;
5531 req->secid = connsid;
5532 req->peer_secid = peersid;
Venkat Yekkiralaa51c64f2006-07-27 22:01:34 -07005533
Paul Moore389fb8002009-03-27 17:10:34 -04005534 return selinux_netlbl_inet_conn_request(req, family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005535}
5536
Adrian Bunk9a673e52006-08-15 00:03:53 -07005537static void selinux_inet_csk_clone(struct sock *newsk,
5538 const struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005539{
5540 struct sk_security_struct *newsksec = newsk->sk_security;
5541
5542 newsksec->sid = req->secid;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005543 newsksec->peer_sid = req->peer_secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005544 /* NOTE: Ideally, we should also get the isec->sid for the
5545 new socket in sync, but we don't have the isec available yet.
5546 So we will wait until sock_graft to do it, by which
5547 time it will have been created and available. */
Paul Moore99f59ed2006-08-29 17:53:48 -07005548
Paul Moore9f2ad662006-11-17 17:38:53 -05005549 /* We don't need to take any sort of lock here as we are the only
5550 * thread with access to newsksec */
Paul Moore389fb8002009-03-27 17:10:34 -04005551 selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005552}
5553
Paul Moore014ab192008-10-10 10:16:33 -04005554static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005555{
Paul Mooreaa862902008-10-10 10:16:29 -04005556 u16 family = sk->sk_family;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005557 struct sk_security_struct *sksec = sk->sk_security;
5558
Paul Mooreaa862902008-10-10 10:16:29 -04005559 /* handle mapped IPv4 packets arriving via IPv6 sockets */
5560 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
5561 family = PF_INET;
5562
5563 selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005564}
5565
Eric Paris2606fd12010-10-13 16:24:41 -04005566static int selinux_secmark_relabel_packet(u32 sid)
5567{
5568 const struct task_security_struct *__tsec;
5569 u32 tsid;
5570
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07005571 __tsec = selinux_cred(current_cred());
Eric Paris2606fd12010-10-13 16:24:41 -04005572 tsid = __tsec->sid;
5573
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005574 return avc_has_perm(&selinux_state,
5575 tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO,
5576 NULL);
Eric Paris2606fd12010-10-13 16:24:41 -04005577}
5578
5579static void selinux_secmark_refcount_inc(void)
5580{
5581 atomic_inc(&selinux_secmark_refcount);
5582}
5583
5584static void selinux_secmark_refcount_dec(void)
5585{
5586 atomic_dec(&selinux_secmark_refcount);
5587}
5588
Adrian Bunk9a673e52006-08-15 00:03:53 -07005589static void selinux_req_classify_flow(const struct request_sock *req,
Paul Moore3df98d72020-09-27 22:38:26 -04005590 struct flowi_common *flic)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005591{
Paul Moore3df98d72020-09-27 22:38:26 -04005592 flic->flowic_secid = req->secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005593}
5594
Paul Moore5dbbaf22013-01-14 07:12:19 +00005595static int selinux_tun_dev_alloc_security(void **security)
5596{
5597 struct tun_security_struct *tunsec;
5598
5599 tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL);
5600 if (!tunsec)
5601 return -ENOMEM;
5602 tunsec->sid = current_sid();
5603
5604 *security = tunsec;
5605 return 0;
5606}
5607
5608static void selinux_tun_dev_free_security(void *security)
5609{
5610 kfree(security);
5611}
5612
Paul Mooreed6d76e2009-08-28 18:12:49 -04005613static int selinux_tun_dev_create(void)
5614{
5615 u32 sid = current_sid();
5616
5617 /* we aren't taking into account the "sockcreate" SID since the socket
5618 * that is being created here is not a socket in the traditional sense,
5619 * instead it is a private sock, accessible only to the kernel, and
5620 * representing a wide range of network traffic spanning multiple
5621 * connections unlike traditional sockets - check the TUN driver to
5622 * get a better understanding of why this socket is special */
5623
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005624 return avc_has_perm(&selinux_state,
5625 sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005626 NULL);
5627}
5628
Paul Moore5dbbaf22013-01-14 07:12:19 +00005629static int selinux_tun_dev_attach_queue(void *security)
Paul Mooreed6d76e2009-08-28 18:12:49 -04005630{
Paul Moore5dbbaf22013-01-14 07:12:19 +00005631 struct tun_security_struct *tunsec = security;
5632
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005633 return avc_has_perm(&selinux_state,
5634 current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET,
Paul Moore5dbbaf22013-01-14 07:12:19 +00005635 TUN_SOCKET__ATTACH_QUEUE, NULL);
5636}
5637
5638static int selinux_tun_dev_attach(struct sock *sk, void *security)
5639{
5640 struct tun_security_struct *tunsec = security;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005641 struct sk_security_struct *sksec = sk->sk_security;
5642
5643 /* we don't currently perform any NetLabel based labeling here and it
5644 * isn't clear that we would want to do so anyway; while we could apply
5645 * labeling without the support of the TUN user the resulting labeled
5646 * traffic from the other end of the connection would almost certainly
5647 * cause confusion to the TUN user that had no idea network labeling
5648 * protocols were being used */
5649
Paul Moore5dbbaf22013-01-14 07:12:19 +00005650 sksec->sid = tunsec->sid;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005651 sksec->sclass = SECCLASS_TUN_SOCKET;
Paul Moore5dbbaf22013-01-14 07:12:19 +00005652
5653 return 0;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005654}
5655
Paul Moore5dbbaf22013-01-14 07:12:19 +00005656static int selinux_tun_dev_open(void *security)
Paul Mooreed6d76e2009-08-28 18:12:49 -04005657{
Paul Moore5dbbaf22013-01-14 07:12:19 +00005658 struct tun_security_struct *tunsec = security;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005659 u32 sid = current_sid();
5660 int err;
5661
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005662 err = avc_has_perm(&selinux_state,
5663 sid, tunsec->sid, SECCLASS_TUN_SOCKET,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005664 TUN_SOCKET__RELABELFROM, NULL);
5665 if (err)
5666 return err;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005667 err = avc_has_perm(&selinux_state,
5668 sid, sid, SECCLASS_TUN_SOCKET,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005669 TUN_SOCKET__RELABELTO, NULL);
5670 if (err)
5671 return err;
Paul Moore5dbbaf22013-01-14 07:12:19 +00005672 tunsec->sid = sid;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005673
5674 return 0;
5675}
5676
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677#ifdef CONFIG_NETFILTER
5678
Florian Westphal4342f702021-10-11 22:22:29 +02005679static unsigned int selinux_ip_forward(void *priv, struct sk_buff *skb,
5680 const struct nf_hook_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681{
Paul Moore1d1e1de2021-10-11 17:50:48 -04005682 int ifindex;
5683 u16 family;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005684 char *addrp;
5685 u32 peer_sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04005686 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005687 struct lsm_network_audit net = {0,};
Paul Moore1d1e1de2021-10-11 17:50:48 -04005688 int secmark_active, peerlbl_active;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005689
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005690 if (!selinux_policycap_netpeer())
Paul Mooreeffad8d2008-01-29 08:49:27 -05005691 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005692
Paul Mooreeffad8d2008-01-29 08:49:27 -05005693 secmark_active = selinux_secmark_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04005694 peerlbl_active = selinux_peerlbl_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05005695 if (!secmark_active && !peerlbl_active)
5696 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005697
Paul Moore1d1e1de2021-10-11 17:50:48 -04005698 family = state->pf;
Paul Moored8395c82008-10-10 10:16:30 -04005699 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
5700 return NF_DROP;
5701
Paul Moore1d1e1de2021-10-11 17:50:48 -04005702 ifindex = state->in->ifindex;
Eric Paris50c205f2012-04-04 15:01:43 -04005703 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005704 ad.u.net = &net;
Paul Moore1d1e1de2021-10-11 17:50:48 -04005705 ad.u.net->netif = ifindex;
Eric Paris48c62af2012-04-02 13:15:44 -04005706 ad.u.net->family = family;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005707 if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
5708 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709
Paul Mooredfaebe92008-10-10 10:16:31 -04005710 if (peerlbl_active) {
Paul Moore1d1e1de2021-10-11 17:50:48 -04005711 int err;
5712
5713 err = selinux_inet_sys_rcv_skb(state->net, ifindex,
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005714 addrp, family, peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04005715 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04005716 selinux_netlbl_err(skb, family, err, 1);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005717 return NF_DROP;
Paul Mooredfaebe92008-10-10 10:16:31 -04005718 }
5719 }
Paul Mooreeffad8d2008-01-29 08:49:27 -05005720
5721 if (secmark_active)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005722 if (avc_has_perm(&selinux_state,
5723 peer_sid, skb->secmark,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005724 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
5725 return NF_DROP;
5726
Paul Moore1d1e1de2021-10-11 17:50:48 -04005727 if (netlbl_enabled())
Paul Moore948bf852008-10-10 10:16:32 -04005728 /* we do this in the FORWARD path and not the POST_ROUTING
5729 * path because we want to make sure we apply the necessary
5730 * labeling before IPsec is applied so we can leverage AH
5731 * protection */
5732 if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
5733 return NF_DROP;
5734
Paul Mooreeffad8d2008-01-29 08:49:27 -05005735 return NF_ACCEPT;
5736}
5737
Florian Westphal4342f702021-10-11 22:22:29 +02005738static unsigned int selinux_ip_output(void *priv, struct sk_buff *skb,
5739 const struct nf_hook_state *state)
Paul Moore948bf852008-10-10 10:16:32 -04005740{
Paul Moore47180062013-12-04 16:10:45 -05005741 struct sock *sk;
Paul Moore948bf852008-10-10 10:16:32 -04005742 u32 sid;
5743
5744 if (!netlbl_enabled())
5745 return NF_ACCEPT;
5746
5747 /* we do this in the LOCAL_OUT path and not the POST_ROUTING path
5748 * because we want to make sure we apply the necessary labeling
5749 * before IPsec is applied so we can leverage AH protection */
Paul Moore47180062013-12-04 16:10:45 -05005750 sk = skb->sk;
5751 if (sk) {
5752 struct sk_security_struct *sksec;
5753
Eric Dumazete446f9d2015-10-08 05:01:55 -07005754 if (sk_listener(sk))
Paul Moore47180062013-12-04 16:10:45 -05005755 /* if the socket is the listening state then this
5756 * packet is a SYN-ACK packet which means it needs to
5757 * be labeled based on the connection/request_sock and
5758 * not the parent socket. unfortunately, we can't
5759 * lookup the request_sock yet as it isn't queued on
5760 * the parent socket until after the SYN-ACK is sent.
5761 * the "solution" is to simply pass the packet as-is
5762 * as any IP option based labeling should be copied
5763 * from the initial connection request (in the IP
5764 * layer). it is far from ideal, but until we get a
5765 * security label in the packet itself this is the
5766 * best we can do. */
5767 return NF_ACCEPT;
5768
5769 /* standard practice, label using the parent socket */
5770 sksec = sk->sk_security;
Paul Moore948bf852008-10-10 10:16:32 -04005771 sid = sksec->sid;
5772 } else
5773 sid = SECINITSID_KERNEL;
Paul Moore1d1e1de2021-10-11 17:50:48 -04005774 if (selinux_netlbl_skbuff_setsid(skb, state->pf, sid) != 0)
Paul Moore948bf852008-10-10 10:16:32 -04005775 return NF_DROP;
5776
5777 return NF_ACCEPT;
5778}
5779
Huw Davies2917f572016-06-27 15:06:15 -04005780
Paul Mooreeffad8d2008-01-29 08:49:27 -05005781static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
Paul Moore1d1e1de2021-10-11 17:50:48 -04005782 const struct nf_hook_state *state)
James Morris4e5ab4c2006-06-09 00:33:33 -07005783{
Paul Moore1d1e1de2021-10-11 17:50:48 -04005784 struct sock *sk;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005785 struct sk_security_struct *sksec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005786 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005787 struct lsm_network_audit net = {0,};
Tom Rix732bc2f2021-12-24 07:07:39 -08005788 u8 proto = 0;
James Morris4e5ab4c2006-06-09 00:33:33 -07005789
Paul Moore1d1e1de2021-10-11 17:50:48 -04005790 sk = skb_to_full_sk(skb);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005791 if (sk == NULL)
5792 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005793 sksec = sk->sk_security;
James Morris4e5ab4c2006-06-09 00:33:33 -07005794
Eric Paris50c205f2012-04-04 15:01:43 -04005795 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005796 ad.u.net = &net;
Paul Moore1d1e1de2021-10-11 17:50:48 -04005797 ad.u.net->netif = state->out->ifindex;
5798 ad.u.net->family = state->pf;
5799 if (selinux_parse_skb(skb, &ad, NULL, 0, &proto))
Paul Moored8395c82008-10-10 10:16:30 -04005800 return NF_DROP;
5801
Paul Moore58bfbb52009-03-27 17:10:41 -04005802 if (selinux_secmark_enabled())
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005803 if (avc_has_perm(&selinux_state,
5804 sksec->sid, skb->secmark,
Paul Moored8395c82008-10-10 10:16:30 -04005805 SECCLASS_PACKET, PACKET__SEND, &ad))
Eric Paris2fe66ec2010-11-23 06:28:08 +00005806 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07005807
Steffen Klassertb9679a72011-02-23 12:55:21 +01005808 if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
5809 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07005810
Paul Mooreeffad8d2008-01-29 08:49:27 -05005811 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005812}
5813
Florian Westphal4342f702021-10-11 22:22:29 +02005814static unsigned int selinux_ip_postroute(void *priv,
5815 struct sk_buff *skb,
5816 const struct nf_hook_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817{
Paul Moore1d1e1de2021-10-11 17:50:48 -04005818 u16 family;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005819 u32 secmark_perm;
5820 u32 peer_sid;
Paul Moore1d1e1de2021-10-11 17:50:48 -04005821 int ifindex;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005822 struct sock *sk;
Thomas Liu2bf49692009-07-14 12:14:09 -04005823 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04005824 struct lsm_network_audit net = {0,};
Paul Mooreeffad8d2008-01-29 08:49:27 -05005825 char *addrp;
Paul Moore1d1e1de2021-10-11 17:50:48 -04005826 int secmark_active, peerlbl_active;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005827
Paul Mooreeffad8d2008-01-29 08:49:27 -05005828 /* If any sort of compatibility mode is enabled then handoff processing
5829 * to the selinux_ip_postroute_compat() function to deal with the
5830 * special handling. We do this in an attempt to keep this function
5831 * as fast and as clean as possible. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005832 if (!selinux_policycap_netpeer())
Paul Moore1d1e1de2021-10-11 17:50:48 -04005833 return selinux_ip_postroute_compat(skb, state);
Paul Moorec0828e52013-12-10 14:58:01 -05005834
Paul Mooreeffad8d2008-01-29 08:49:27 -05005835 secmark_active = selinux_secmark_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04005836 peerlbl_active = selinux_peerlbl_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05005837 if (!secmark_active && !peerlbl_active)
5838 return NF_ACCEPT;
5839
Eric Dumazet54abc682015-11-08 10:54:07 -08005840 sk = skb_to_full_sk(skb);
Paul Moorec0828e52013-12-10 14:58:01 -05005841
Paul Mooreeffad8d2008-01-29 08:49:27 -05005842#ifdef CONFIG_XFRM
5843 /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
5844 * packet transformation so allow the packet to pass without any checks
5845 * since we'll have another chance to perform access control checks
5846 * when the packet is on it's final way out.
5847 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
Paul Moorec0828e52013-12-10 14:58:01 -05005848 * is NULL, in this case go ahead and apply access control.
5849 * NOTE: if this is a local socket (skb->sk != NULL) that is in the
5850 * TCP listening state we cannot wait until the XFRM processing
5851 * is done as we will miss out on the SA label if we do;
5852 * unfortunately, this means more work, but it is only once per
5853 * connection. */
5854 if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL &&
Eric Dumazete446f9d2015-10-08 05:01:55 -07005855 !(sk && sk_listener(sk)))
Paul Mooreeffad8d2008-01-29 08:49:27 -05005856 return NF_ACCEPT;
5857#endif
Paul Mooreeffad8d2008-01-29 08:49:27 -05005858
Paul Moore1d1e1de2021-10-11 17:50:48 -04005859 family = state->pf;
Paul Moored8395c82008-10-10 10:16:30 -04005860 if (sk == NULL) {
Paul Moore446b8022013-12-04 16:10:51 -05005861 /* Without an associated socket the packet is either coming
5862 * from the kernel or it is being forwarded; check the packet
5863 * to determine which and if the packet is being forwarded
5864 * query the packet directly to determine the security label. */
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005865 if (skb->skb_iif) {
5866 secmark_perm = PACKET__FORWARD_OUT;
Paul Moored8395c82008-10-10 10:16:30 -04005867 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005868 return NF_DROP;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005869 } else {
5870 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04005871 peer_sid = SECINITSID_KERNEL;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005872 }
Eric Dumazete446f9d2015-10-08 05:01:55 -07005873 } else if (sk_listener(sk)) {
Paul Moore446b8022013-12-04 16:10:51 -05005874 /* Locally generated packet but the associated socket is in the
5875 * listening state which means this is a SYN-ACK packet. In
5876 * this particular case the correct security label is assigned
5877 * to the connection/request_sock but unfortunately we can't
5878 * query the request_sock as it isn't queued on the parent
5879 * socket until after the SYN-ACK packet is sent; the only
5880 * viable choice is to regenerate the label like we do in
5881 * selinux_inet_conn_request(). See also selinux_ip_output()
5882 * for similar problems. */
5883 u32 skb_sid;
Eric Dumazete446f9d2015-10-08 05:01:55 -07005884 struct sk_security_struct *sksec;
5885
Eric Dumazete446f9d2015-10-08 05:01:55 -07005886 sksec = sk->sk_security;
Paul Moore446b8022013-12-04 16:10:51 -05005887 if (selinux_skb_peerlbl_sid(skb, family, &skb_sid))
5888 return NF_DROP;
Paul Moorec0828e52013-12-10 14:58:01 -05005889 /* At this point, if the returned skb peerlbl is SECSID_NULL
5890 * and the packet has been through at least one XFRM
5891 * transformation then we must be dealing with the "final"
5892 * form of labeled IPsec packet; since we've already applied
5893 * all of our access controls on this packet we can safely
5894 * pass the packet. */
5895 if (skb_sid == SECSID_NULL) {
5896 switch (family) {
5897 case PF_INET:
5898 if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
5899 return NF_ACCEPT;
5900 break;
5901 case PF_INET6:
5902 if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED)
5903 return NF_ACCEPT;
Paul Moorea7a91a12014-09-03 10:51:59 -04005904 break;
Paul Moorec0828e52013-12-10 14:58:01 -05005905 default:
5906 return NF_DROP_ERR(-ECONNREFUSED);
5907 }
5908 }
Paul Moore446b8022013-12-04 16:10:51 -05005909 if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid))
5910 return NF_DROP;
5911 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04005912 } else {
Paul Moore446b8022013-12-04 16:10:51 -05005913 /* Locally generated packet, fetch the security label from the
5914 * associated socket. */
Paul Mooreeffad8d2008-01-29 08:49:27 -05005915 struct sk_security_struct *sksec = sk->sk_security;
5916 peer_sid = sksec->sid;
5917 secmark_perm = PACKET__SEND;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005918 }
5919
Paul Moore1d1e1de2021-10-11 17:50:48 -04005920 ifindex = state->out->ifindex;
Eric Paris50c205f2012-04-04 15:01:43 -04005921 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04005922 ad.u.net = &net;
5923 ad.u.net->netif = ifindex;
5924 ad.u.net->family = family;
Paul Moored8395c82008-10-10 10:16:30 -04005925 if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
Eric Paris04f6d702010-11-23 06:28:02 +00005926 return NF_DROP;
Paul Moored8395c82008-10-10 10:16:30 -04005927
Paul Mooreeffad8d2008-01-29 08:49:27 -05005928 if (secmark_active)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005929 if (avc_has_perm(&selinux_state,
5930 peer_sid, skb->secmark,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005931 SECCLASS_PACKET, secmark_perm, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005932 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005933
5934 if (peerlbl_active) {
5935 u32 if_sid;
5936 u32 node_sid;
5937
Paul Moore1d1e1de2021-10-11 17:50:48 -04005938 if (sel_netif_sid(state->net, ifindex, &if_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005939 return NF_DROP;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005940 if (avc_has_perm(&selinux_state,
5941 peer_sid, if_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005942 SECCLASS_NETIF, NETIF__EGRESS, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005943 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005944
5945 if (sel_netnode_sid(addrp, family, &node_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005946 return NF_DROP;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005947 if (avc_has_perm(&selinux_state,
5948 peer_sid, node_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005949 SECCLASS_NODE, NODE__SENDTO, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005950 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005951 }
5952
5953 return NF_ACCEPT;
5954}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955#endif /* CONFIG_NETFILTER */
5956
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
5958{
Paul Moorefb739742020-04-28 09:59:02 -04005959 int rc = 0;
5960 unsigned int msg_len;
5961 unsigned int data_len = skb->len;
5962 unsigned char *data = skb->data;
Huaisheng Yedf4779b2020-01-13 23:03:31 +08005963 struct nlmsghdr *nlh;
5964 struct sk_security_struct *sksec = sk->sk_security;
Paul Moorefb739742020-04-28 09:59:02 -04005965 u16 sclass = sksec->sclass;
5966 u32 perm;
Huaisheng Yedf4779b2020-01-13 23:03:31 +08005967
Paul Moorefb739742020-04-28 09:59:02 -04005968 while (data_len >= nlmsg_total_size(0)) {
5969 nlh = (struct nlmsghdr *)data;
Huaisheng Yedf4779b2020-01-13 23:03:31 +08005970
Paul Moorefb739742020-04-28 09:59:02 -04005971 /* NOTE: the nlmsg_len field isn't reliably set by some netlink
5972 * users which means we can't reject skb's with bogus
5973 * length fields; our solution is to follow what
5974 * netlink_rcv_skb() does and simply skip processing at
5975 * messages with length fields that are clearly junk
5976 */
5977 if (nlh->nlmsg_len < NLMSG_HDRLEN || nlh->nlmsg_len > data_len)
5978 return 0;
5979
5980 rc = selinux_nlmsg_lookup(sclass, nlh->nlmsg_type, &perm);
5981 if (rc == 0) {
5982 rc = sock_has_perm(sk, perm);
5983 if (rc)
5984 return rc;
5985 } else if (rc == -EINVAL) {
5986 /* -EINVAL is a missing msg/perm mapping */
Huaisheng Yedf4779b2020-01-13 23:03:31 +08005987 pr_warn_ratelimited("SELinux: unrecognized netlink"
Paul Moorefb739742020-04-28 09:59:02 -04005988 " message: protocol=%hu nlmsg_type=%hu sclass=%s"
5989 " pid=%d comm=%s\n",
5990 sk->sk_protocol, nlh->nlmsg_type,
5991 secclass_map[sclass - 1].name,
5992 task_pid_nr(current), current->comm);
5993 if (enforcing_enabled(&selinux_state) &&
5994 !security_get_allow_unknown(&selinux_state))
5995 return rc;
5996 rc = 0;
5997 } else if (rc == -ENOENT) {
5998 /* -ENOENT is a missing socket/class mapping, ignore */
5999 rc = 0;
6000 } else {
6001 return rc;
Huaisheng Yedf4779b2020-01-13 23:03:31 +08006002 }
6003
Paul Moorefb739742020-04-28 09:59:02 -04006004 /* move to the next message after applying netlink padding */
6005 msg_len = NLMSG_ALIGN(nlh->nlmsg_len);
6006 if (msg_len >= data_len)
6007 return 0;
6008 data_len -= msg_len;
6009 data += msg_len;
Huaisheng Yedf4779b2020-01-13 23:03:31 +08006010 }
6011
Paul Moorefb739742020-04-28 09:59:02 -04006012 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013}
6014
Casey Schauflerecd5f822018-11-20 11:55:02 -08006015static void ipc_init_security(struct ipc_security_struct *isec, u16 sclass)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006016{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006017 isec->sclass = sclass;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006018 isec->sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019}
6020
Linus Torvalds1da177e2005-04-16 15:20:36 -07006021static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
Stephen Smalley6af963f2005-05-01 08:58:39 -07006022 u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006023{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006024 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006025 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006026 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027
Casey Schaufler7c653822018-09-21 17:19:45 -07006028 isec = selinux_ipc(ipc_perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006029
Eric Paris50c205f2012-04-04 15:01:43 -04006030 ad.type = LSM_AUDIT_DATA_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006031 ad.u.ipc_id = ipc_perms->key;
6032
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006033 return avc_has_perm(&selinux_state,
6034 sid, isec->sid, isec->sclass, perms, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006035}
6036
6037static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
6038{
Huaisheng Yeb82f3f62020-01-10 17:58:56 +08006039 struct msg_security_struct *msec;
6040
6041 msec = selinux_msg_msg(msg);
6042 msec->sid = SECINITSID_UNLABELED;
6043
6044 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045}
6046
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047/* message queue security operations */
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05006048static int selinux_msg_queue_alloc_security(struct kern_ipc_perm *msq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006051 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006052 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053 int rc;
6054
Casey Schauflerecd5f822018-11-20 11:55:02 -08006055 isec = selinux_ipc(msq);
6056 ipc_init_security(isec, SECCLASS_MSGQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006057
Eric Paris50c205f2012-04-04 15:01:43 -04006058 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05006059 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006060
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006061 rc = avc_has_perm(&selinux_state,
6062 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063 MSGQ__CREATE, &ad);
Casey Schauflerecd5f822018-11-20 11:55:02 -08006064 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006065}
6066
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05006067static int selinux_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006070 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006071 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072
Casey Schaufler7c653822018-09-21 17:19:45 -07006073 isec = selinux_ipc(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006074
Eric Paris50c205f2012-04-04 15:01:43 -04006075 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05006076 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006077
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006078 return avc_has_perm(&selinux_state,
6079 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080 MSGQ__ASSOCIATE, &ad);
6081}
6082
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05006083static int selinux_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084{
6085 int err;
6086 int perms;
6087
Eric Paris828dfe12008-04-17 13:17:49 -04006088 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006089 case IPC_INFO:
6090 case MSG_INFO:
6091 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006092 return avc_has_perm(&selinux_state,
6093 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006094 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095 case IPC_STAT:
6096 case MSG_STAT:
Davidlohr Bueso23c8cec2018-04-10 16:35:30 -07006097 case MSG_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006098 perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
6099 break;
6100 case IPC_SET:
6101 perms = MSGQ__SETATTR;
6102 break;
6103 case IPC_RMID:
6104 perms = MSGQ__DESTROY;
6105 break;
6106 default:
6107 return 0;
6108 }
6109
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05006110 err = ipc_has_perm(msq, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006111 return err;
6112}
6113
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05006114static int selinux_msg_queue_msgsnd(struct kern_ipc_perm *msq, struct msg_msg *msg, int msqflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006115{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006116 struct ipc_security_struct *isec;
6117 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006118 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006119 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120 int rc;
6121
Casey Schaufler7c653822018-09-21 17:19:45 -07006122 isec = selinux_ipc(msq);
6123 msec = selinux_msg_msg(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006124
6125 /*
6126 * First time through, need to assign label to the message
6127 */
6128 if (msec->sid == SECINITSID_UNLABELED) {
6129 /*
6130 * Compute new sid based on current process and
6131 * message queue this message will be stored in
6132 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006133 rc = security_transition_sid(&selinux_state, sid, isec->sid,
6134 SECCLASS_MSG, NULL, &msec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006135 if (rc)
6136 return rc;
6137 }
6138
Eric Paris50c205f2012-04-04 15:01:43 -04006139 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05006140 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006141
6142 /* Can this process write to the queue? */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006143 rc = avc_has_perm(&selinux_state,
6144 sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006145 MSGQ__WRITE, &ad);
6146 if (!rc)
6147 /* Can this process send the message */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006148 rc = avc_has_perm(&selinux_state,
6149 sid, msec->sid, SECCLASS_MSG,
David Howells275bb412008-11-14 10:39:19 +11006150 MSG__SEND, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006151 if (!rc)
6152 /* Can the message be put in the queue? */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006153 rc = avc_has_perm(&selinux_state,
6154 msec->sid, isec->sid, SECCLASS_MSGQ,
David Howells275bb412008-11-14 10:39:19 +11006155 MSGQ__ENQUEUE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006156
6157 return rc;
6158}
6159
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05006160static int selinux_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006161 struct task_struct *target,
6162 long type, int mode)
6163{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006164 struct ipc_security_struct *isec;
6165 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006166 struct common_audit_data ad;
Paul Moorea3727a82021-09-23 09:50:11 -04006167 u32 sid = task_sid_obj(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006168 int rc;
6169
Casey Schaufler7c653822018-09-21 17:19:45 -07006170 isec = selinux_ipc(msq);
6171 msec = selinux_msg_msg(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006172
Eric Paris50c205f2012-04-04 15:01:43 -04006173 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05006174 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006175
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006176 rc = avc_has_perm(&selinux_state,
6177 sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006178 SECCLASS_MSGQ, MSGQ__READ, &ad);
6179 if (!rc)
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006180 rc = avc_has_perm(&selinux_state,
6181 sid, msec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006182 SECCLASS_MSG, MSG__RECEIVE, &ad);
6183 return rc;
6184}
6185
6186/* Shared Memory security operations */
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006187static int selinux_shm_alloc_security(struct kern_ipc_perm *shp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006188{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006189 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006190 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006191 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006192 int rc;
6193
Casey Schauflerecd5f822018-11-20 11:55:02 -08006194 isec = selinux_ipc(shp);
6195 ipc_init_security(isec, SECCLASS_SHM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006196
Eric Paris50c205f2012-04-04 15:01:43 -04006197 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006198 ad.u.ipc_id = shp->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006199
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006200 rc = avc_has_perm(&selinux_state,
6201 sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006202 SHM__CREATE, &ad);
Casey Schauflerecd5f822018-11-20 11:55:02 -08006203 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204}
6205
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006206static int selinux_shm_associate(struct kern_ipc_perm *shp, int shmflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006207{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006208 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006209 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006210 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211
Casey Schaufler7c653822018-09-21 17:19:45 -07006212 isec = selinux_ipc(shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006213
Eric Paris50c205f2012-04-04 15:01:43 -04006214 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006215 ad.u.ipc_id = shp->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006216
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006217 return avc_has_perm(&selinux_state,
6218 sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219 SHM__ASSOCIATE, &ad);
6220}
6221
6222/* Note, at this point, shp is locked down */
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006223static int selinux_shm_shmctl(struct kern_ipc_perm *shp, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006224{
6225 int perms;
6226 int err;
6227
Eric Paris828dfe12008-04-17 13:17:49 -04006228 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006229 case IPC_INFO:
6230 case SHM_INFO:
6231 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006232 return avc_has_perm(&selinux_state,
6233 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006234 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006235 case IPC_STAT:
6236 case SHM_STAT:
Davidlohr Buesoc21a6972018-04-10 16:35:23 -07006237 case SHM_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006238 perms = SHM__GETATTR | SHM__ASSOCIATE;
6239 break;
6240 case IPC_SET:
6241 perms = SHM__SETATTR;
6242 break;
6243 case SHM_LOCK:
6244 case SHM_UNLOCK:
6245 perms = SHM__LOCK;
6246 break;
6247 case IPC_RMID:
6248 perms = SHM__DESTROY;
6249 break;
6250 default:
6251 return 0;
6252 }
6253
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006254 err = ipc_has_perm(shp, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006255 return err;
6256}
6257
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006258static int selinux_shm_shmat(struct kern_ipc_perm *shp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006259 char __user *shmaddr, int shmflg)
6260{
6261 u32 perms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006262
6263 if (shmflg & SHM_RDONLY)
6264 perms = SHM__READ;
6265 else
6266 perms = SHM__READ | SHM__WRITE;
6267
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006268 return ipc_has_perm(shp, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006269}
6270
6271/* Semaphore security operations */
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006272static int selinux_sem_alloc_security(struct kern_ipc_perm *sma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006273{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006274 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006275 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006276 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006277 int rc;
6278
Casey Schauflerecd5f822018-11-20 11:55:02 -08006279 isec = selinux_ipc(sma);
6280 ipc_init_security(isec, SECCLASS_SEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006281
Eric Paris50c205f2012-04-04 15:01:43 -04006282 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006283 ad.u.ipc_id = sma->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006284
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006285 rc = avc_has_perm(&selinux_state,
6286 sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006287 SEM__CREATE, &ad);
Casey Schauflerecd5f822018-11-20 11:55:02 -08006288 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006289}
6290
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006291static int selinux_sem_associate(struct kern_ipc_perm *sma, int semflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006292{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006293 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006294 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006295 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006296
Casey Schaufler7c653822018-09-21 17:19:45 -07006297 isec = selinux_ipc(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006298
Eric Paris50c205f2012-04-04 15:01:43 -04006299 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006300 ad.u.ipc_id = sma->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006301
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006302 return avc_has_perm(&selinux_state,
6303 sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006304 SEM__ASSOCIATE, &ad);
6305}
6306
6307/* Note, at this point, sma is locked down */
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006308static int selinux_sem_semctl(struct kern_ipc_perm *sma, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006309{
6310 int err;
6311 u32 perms;
6312
Eric Paris828dfe12008-04-17 13:17:49 -04006313 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006314 case IPC_INFO:
6315 case SEM_INFO:
6316 /* No specific object, just general system-wide information. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006317 return avc_has_perm(&selinux_state,
6318 current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006319 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006320 case GETPID:
6321 case GETNCNT:
6322 case GETZCNT:
6323 perms = SEM__GETATTR;
6324 break;
6325 case GETVAL:
6326 case GETALL:
6327 perms = SEM__READ;
6328 break;
6329 case SETVAL:
6330 case SETALL:
6331 perms = SEM__WRITE;
6332 break;
6333 case IPC_RMID:
6334 perms = SEM__DESTROY;
6335 break;
6336 case IPC_SET:
6337 perms = SEM__SETATTR;
6338 break;
6339 case IPC_STAT:
6340 case SEM_STAT:
Davidlohr Buesoa280d6d2018-04-10 16:35:26 -07006341 case SEM_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006342 perms = SEM__GETATTR | SEM__ASSOCIATE;
6343 break;
6344 default:
6345 return 0;
6346 }
6347
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006348 err = ipc_has_perm(sma, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006349 return err;
6350}
6351
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006352static int selinux_sem_semop(struct kern_ipc_perm *sma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006353 struct sembuf *sops, unsigned nsops, int alter)
6354{
6355 u32 perms;
6356
6357 if (alter)
6358 perms = SEM__READ | SEM__WRITE;
6359 else
6360 perms = SEM__READ;
6361
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006362 return ipc_has_perm(sma, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006363}
6364
6365static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
6366{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006367 u32 av = 0;
6368
Linus Torvalds1da177e2005-04-16 15:20:36 -07006369 av = 0;
6370 if (flag & S_IRUGO)
6371 av |= IPC__UNIX_READ;
6372 if (flag & S_IWUGO)
6373 av |= IPC__UNIX_WRITE;
6374
6375 if (av == 0)
6376 return 0;
6377
Stephen Smalley6af963f2005-05-01 08:58:39 -07006378 return ipc_has_perm(ipcp, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006379}
6380
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02006381static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
6382{
Casey Schaufler7c653822018-09-21 17:19:45 -07006383 struct ipc_security_struct *isec = selinux_ipc(ipcp);
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02006384 *secid = isec->sid;
6385}
6386
Eric Paris828dfe12008-04-17 13:17:49 -04006387static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006388{
6389 if (inode)
6390 inode_doinit_with_dentry(inode, dentry);
6391}
6392
6393static int selinux_getprocattr(struct task_struct *p,
Al Viro04ff9702007-03-12 16:17:58 +00006394 char *name, char **value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006395{
David Howells275bb412008-11-14 10:39:19 +11006396 const struct task_security_struct *__tsec;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00006397 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006398 int error;
Al Viro04ff9702007-03-12 16:17:58 +00006399 unsigned len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006400
David Howells275bb412008-11-14 10:39:19 +11006401 rcu_read_lock();
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07006402 __tsec = selinux_cred(__task_cred(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006403
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006404 if (current != p) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006405 error = avc_has_perm(&selinux_state,
6406 current_sid(), __tsec->sid,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006407 SECCLASS_PROCESS, PROCESS__GETATTR, NULL);
6408 if (error)
6409 goto bad;
6410 }
6411
Linus Torvalds1da177e2005-04-16 15:20:36 -07006412 if (!strcmp(name, "current"))
David Howells275bb412008-11-14 10:39:19 +11006413 sid = __tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006414 else if (!strcmp(name, "prev"))
David Howells275bb412008-11-14 10:39:19 +11006415 sid = __tsec->osid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006416 else if (!strcmp(name, "exec"))
David Howells275bb412008-11-14 10:39:19 +11006417 sid = __tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006418 else if (!strcmp(name, "fscreate"))
David Howells275bb412008-11-14 10:39:19 +11006419 sid = __tsec->create_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006420 else if (!strcmp(name, "keycreate"))
David Howells275bb412008-11-14 10:39:19 +11006421 sid = __tsec->keycreate_sid;
Eric Paris42c3e032006-06-26 00:26:03 -07006422 else if (!strcmp(name, "sockcreate"))
David Howells275bb412008-11-14 10:39:19 +11006423 sid = __tsec->sockcreate_sid;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006424 else {
6425 error = -EINVAL;
6426 goto bad;
6427 }
David Howells275bb412008-11-14 10:39:19 +11006428 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006429
6430 if (!sid)
6431 return 0;
6432
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006433 error = security_sid_to_context(&selinux_state, sid, value, &len);
Al Viro04ff9702007-03-12 16:17:58 +00006434 if (error)
6435 return error;
6436 return len;
David Howells275bb412008-11-14 10:39:19 +11006437
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006438bad:
David Howells275bb412008-11-14 10:39:19 +11006439 rcu_read_unlock();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006440 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006441}
6442
Stephen Smalleyb21507e2017-01-09 10:07:31 -05006443static int selinux_setprocattr(const char *name, void *value, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006444{
6445 struct task_security_struct *tsec;
David Howellsd84f4f92008-11-14 10:39:23 +11006446 struct cred *new;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006447 u32 mysid = current_sid(), sid = 0, ptsid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006448 int error;
6449 char *str = value;
6450
Linus Torvalds1da177e2005-04-16 15:20:36 -07006451 /*
6452 * Basic control over ability to set these attributes at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006453 */
6454 if (!strcmp(name, "exec"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006455 error = avc_has_perm(&selinux_state,
6456 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006457 PROCESS__SETEXEC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006458 else if (!strcmp(name, "fscreate"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006459 error = avc_has_perm(&selinux_state,
6460 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006461 PROCESS__SETFSCREATE, NULL);
Michael LeMay4eb582c2006-06-26 00:24:57 -07006462 else if (!strcmp(name, "keycreate"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006463 error = avc_has_perm(&selinux_state,
6464 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006465 PROCESS__SETKEYCREATE, NULL);
Eric Paris42c3e032006-06-26 00:26:03 -07006466 else if (!strcmp(name, "sockcreate"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006467 error = avc_has_perm(&selinux_state,
6468 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006469 PROCESS__SETSOCKCREATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006470 else if (!strcmp(name, "current"))
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006471 error = avc_has_perm(&selinux_state,
6472 mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006473 PROCESS__SETCURRENT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006474 else
6475 error = -EINVAL;
6476 if (error)
6477 return error;
6478
6479 /* Obtain a SID for the context, if one was specified. */
Stephen Smalleya050a572017-01-31 11:54:04 -05006480 if (size && str[0] && str[0] != '\n') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006481 if (str[size-1] == '\n') {
6482 str[size-1] = 0;
6483 size--;
6484 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006485 error = security_context_to_sid(&selinux_state, value, size,
6486 &sid, GFP_KERNEL);
Stephen Smalley12b29f32008-05-07 13:03:20 -04006487 if (error == -EINVAL && !strcmp(name, "fscreate")) {
Stephen Smalleydb590002017-04-20 11:31:30 -04006488 if (!has_cap_mac_admin(true)) {
Eric Parisd6ea83e2012-04-04 13:45:49 -04006489 struct audit_buffer *ab;
6490 size_t audit_size;
6491
6492 /* We strip a nul only if it is at the end, otherwise the
6493 * context contains a nul and we should audit that */
6494 if (str[size - 1] == '\0')
6495 audit_size = size - 1;
6496 else
6497 audit_size = size;
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04006498 ab = audit_log_start(audit_context(),
6499 GFP_ATOMIC,
6500 AUDIT_SELINUX_ERR);
Austin Kim893c47d2021-07-14 01:11:27 +01006501 if (!ab)
6502 return error;
Eric Parisd6ea83e2012-04-04 13:45:49 -04006503 audit_log_format(ab, "op=fscreate invalid_context=");
6504 audit_log_n_untrustedstring(ab, value, audit_size);
6505 audit_log_end(ab);
6506
Stephen Smalley12b29f32008-05-07 13:03:20 -04006507 return error;
Eric Parisd6ea83e2012-04-04 13:45:49 -04006508 }
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006509 error = security_context_to_sid_force(
6510 &selinux_state,
6511 value, size, &sid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04006512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006513 if (error)
6514 return error;
6515 }
6516
David Howellsd84f4f92008-11-14 10:39:23 +11006517 new = prepare_creds();
6518 if (!new)
6519 return -ENOMEM;
6520
Linus Torvalds1da177e2005-04-16 15:20:36 -07006521 /* Permission checking based on the specified context is
6522 performed during the actual operation (execve,
6523 open/mkdir/...), when we know the full context of the
Eric W. Biedermanb8bff592020-03-22 15:46:24 -05006524 operation. See selinux_bprm_creds_for_exec for the execve
Linus Torvalds1da177e2005-04-16 15:20:36 -07006525 checks and may_create for the file creation checks. The
6526 operation will then fail if the context is not permitted. */
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07006527 tsec = selinux_cred(new);
David Howellsd84f4f92008-11-14 10:39:23 +11006528 if (!strcmp(name, "exec")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006529 tsec->exec_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006530 } else if (!strcmp(name, "fscreate")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006531 tsec->create_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006532 } else if (!strcmp(name, "keycreate")) {
Ondrej Mosnacek464c2582019-06-12 10:12:26 +02006533 if (sid) {
6534 error = avc_has_perm(&selinux_state, mysid, sid,
6535 SECCLASS_KEY, KEY__CREATE, NULL);
6536 if (error)
6537 goto abort_change;
6538 }
Michael LeMay4eb582c2006-06-26 00:24:57 -07006539 tsec->keycreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006540 } else if (!strcmp(name, "sockcreate")) {
Eric Paris42c3e032006-06-26 00:26:03 -07006541 tsec->sockcreate_sid = sid;
David Howellsd84f4f92008-11-14 10:39:23 +11006542 } else if (!strcmp(name, "current")) {
6543 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006544 if (sid == 0)
David Howellsd84f4f92008-11-14 10:39:23 +11006545 goto abort_change;
KaiGai Koheid9250de2008-08-28 16:35:57 +09006546
David Howellsd84f4f92008-11-14 10:39:23 +11006547 /* Only allow single threaded processes to change context */
6548 error = -EPERM;
Oleg Nesterov5bb459b2009-07-10 03:48:23 +02006549 if (!current_is_single_threaded()) {
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006550 error = security_bounded_transition(&selinux_state,
6551 tsec->sid, sid);
David Howellsd84f4f92008-11-14 10:39:23 +11006552 if (error)
6553 goto abort_change;
Eric Paris828dfe12008-04-17 13:17:49 -04006554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006555
6556 /* Check permissions for the transition. */
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006557 error = avc_has_perm(&selinux_state,
6558 tsec->sid, sid, SECCLASS_PROCESS,
Eric Paris828dfe12008-04-17 13:17:49 -04006559 PROCESS__DYNTRANSITION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006560 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11006561 goto abort_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006562
6563 /* Check for ptracing, and update the task SID if ok.
6564 Otherwise, leave SID unchanged and fail. */
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006565 ptsid = ptrace_parent_sid();
Paul Moore0c6181c2016-03-30 21:41:21 -04006566 if (ptsid != 0) {
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006567 error = avc_has_perm(&selinux_state,
6568 ptsid, sid, SECCLASS_PROCESS,
David Howellsd84f4f92008-11-14 10:39:23 +11006569 PROCESS__PTRACE, NULL);
6570 if (error)
6571 goto abort_change;
6572 }
6573
6574 tsec->sid = sid;
6575 } else {
6576 error = -EINVAL;
6577 goto abort_change;
6578 }
6579
6580 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006581 return size;
David Howellsd84f4f92008-11-14 10:39:23 +11006582
6583abort_change:
6584 abort_creds(new);
6585 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006586}
6587
David Quigley746df9b2013-05-22 12:50:35 -04006588static int selinux_ismaclabel(const char *name)
6589{
6590 return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0);
6591}
6592
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006593static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
6594{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006595 return security_sid_to_context(&selinux_state, secid,
6596 secdata, seclen);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006597}
6598
David Howells7bf570d2008-04-29 20:52:51 +01006599static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
David Howells63cb3442008-01-15 23:47:35 +00006600{
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006601 return security_context_to_sid(&selinux_state, secdata, seclen,
6602 secid, GFP_KERNEL);
David Howells63cb3442008-01-15 23:47:35 +00006603}
6604
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006605static void selinux_release_secctx(char *secdata, u32 seclen)
6606{
Paul Moore088999e2007-08-01 11:12:58 -04006607 kfree(secdata);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006608}
6609
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006610static void selinux_inode_invalidate_secctx(struct inode *inode)
6611{
Casey Schaufler80788c22018-09-21 17:19:11 -07006612 struct inode_security_struct *isec = selinux_inode(inode);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006613
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01006614 spin_lock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006615 isec->initialized = LABEL_INVALID;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01006616 spin_unlock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006617}
6618
David P. Quigley1ee65e32009-09-03 14:25:57 -04006619/*
6620 * called with inode->i_mutex locked
6621 */
6622static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
6623{
Ondrej Mosnacek53e0c2a2018-12-21 21:18:53 +01006624 int rc = selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX,
6625 ctx, ctxlen, 0);
6626 /* Do not return error when suppressing label (SBLABEL_MNT not set). */
6627 return rc == -EOPNOTSUPP ? 0 : rc;
David P. Quigley1ee65e32009-09-03 14:25:57 -04006628}
6629
6630/*
6631 * called with inode->i_mutex locked
6632 */
6633static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
6634{
Tycho Andersenc7c7a1a12021-01-21 14:19:28 +01006635 return __vfs_setxattr_noperm(&init_user_ns, dentry, XATTR_NAME_SELINUX,
6636 ctx, ctxlen, 0);
David P. Quigley1ee65e32009-09-03 14:25:57 -04006637}
6638
6639static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
6640{
6641 int len = 0;
Christian Brauner71bc3562021-01-21 14:19:29 +01006642 len = selinux_inode_getsecurity(&init_user_ns, inode,
6643 XATTR_SELINUX_SUFFIX, ctx, true);
David P. Quigley1ee65e32009-09-03 14:25:57 -04006644 if (len < 0)
6645 return len;
6646 *ctxlen = len;
6647 return 0;
6648}
Michael LeMayd7200242006-06-22 14:47:17 -07006649#ifdef CONFIG_KEYS
6650
David Howellsd84f4f92008-11-14 10:39:23 +11006651static int selinux_key_alloc(struct key *k, const struct cred *cred,
David Howells7e047ef2006-06-26 00:24:50 -07006652 unsigned long flags)
Michael LeMayd7200242006-06-22 14:47:17 -07006653{
David Howellsd84f4f92008-11-14 10:39:23 +11006654 const struct task_security_struct *tsec;
Michael LeMayd7200242006-06-22 14:47:17 -07006655 struct key_security_struct *ksec;
6656
6657 ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
6658 if (!ksec)
6659 return -ENOMEM;
6660
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07006661 tsec = selinux_cred(cred);
David Howellsd84f4f92008-11-14 10:39:23 +11006662 if (tsec->keycreate_sid)
6663 ksec->sid = tsec->keycreate_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006664 else
David Howellsd84f4f92008-11-14 10:39:23 +11006665 ksec->sid = tsec->sid;
Michael LeMayd7200242006-06-22 14:47:17 -07006666
David Howells275bb412008-11-14 10:39:19 +11006667 k->security = ksec;
Michael LeMayd7200242006-06-22 14:47:17 -07006668 return 0;
6669}
6670
6671static void selinux_key_free(struct key *k)
6672{
6673 struct key_security_struct *ksec = k->security;
6674
6675 k->security = NULL;
6676 kfree(ksec);
6677}
6678
6679static int selinux_key_permission(key_ref_t key_ref,
David Howellsd84f4f92008-11-14 10:39:23 +11006680 const struct cred *cred,
David Howells8c0637e2020-05-12 15:16:29 +01006681 enum key_need_perm need_perm)
Michael LeMayd7200242006-06-22 14:47:17 -07006682{
6683 struct key *key;
Michael LeMayd7200242006-06-22 14:47:17 -07006684 struct key_security_struct *ksec;
David Howells8c0637e2020-05-12 15:16:29 +01006685 u32 perm, sid;
Michael LeMayd7200242006-06-22 14:47:17 -07006686
David Howells8c0637e2020-05-12 15:16:29 +01006687 switch (need_perm) {
6688 case KEY_NEED_VIEW:
6689 perm = KEY__VIEW;
6690 break;
6691 case KEY_NEED_READ:
6692 perm = KEY__READ;
6693 break;
6694 case KEY_NEED_WRITE:
6695 perm = KEY__WRITE;
6696 break;
6697 case KEY_NEED_SEARCH:
6698 perm = KEY__SEARCH;
6699 break;
6700 case KEY_NEED_LINK:
6701 perm = KEY__LINK;
6702 break;
6703 case KEY_NEED_SETATTR:
6704 perm = KEY__SETATTR;
6705 break;
6706 case KEY_NEED_UNLINK:
6707 case KEY_SYSADMIN_OVERRIDE:
6708 case KEY_AUTHTOKEN_OVERRIDE:
6709 case KEY_DEFER_PERM_CHECK:
Michael LeMayd7200242006-06-22 14:47:17 -07006710 return 0;
David Howells8c0637e2020-05-12 15:16:29 +01006711 default:
6712 WARN_ON(1);
6713 return -EPERM;
6714
6715 }
Michael LeMayd7200242006-06-22 14:47:17 -07006716
David Howellsd84f4f92008-11-14 10:39:23 +11006717 sid = cred_sid(cred);
David Howells275bb412008-11-14 10:39:19 +11006718 key = key_ref_to_ptr(key_ref);
6719 ksec = key->security;
6720
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006721 return avc_has_perm(&selinux_state,
Linus Torvalds028db3e2019-07-10 18:43:43 -07006722 sid, ksec->sid, SECCLASS_KEY, perm, NULL);
Michael LeMayd7200242006-06-22 14:47:17 -07006723}
6724
David Howells70a5bb72008-04-29 01:01:26 -07006725static int selinux_key_getsecurity(struct key *key, char **_buffer)
6726{
6727 struct key_security_struct *ksec = key->security;
6728 char *context = NULL;
6729 unsigned len;
6730 int rc;
6731
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006732 rc = security_sid_to_context(&selinux_state, ksec->sid,
6733 &context, &len);
David Howells70a5bb72008-04-29 01:01:26 -07006734 if (!rc)
6735 rc = len;
6736 *_buffer = context;
6737 return rc;
6738}
David Howells3e412cc2020-01-14 17:07:13 +00006739
6740#ifdef CONFIG_KEY_NOTIFICATIONS
6741static int selinux_watch_key(struct key *key)
6742{
6743 struct key_security_struct *ksec = key->security;
6744 u32 sid = current_sid();
6745
6746 return avc_has_perm(&selinux_state,
6747 sid, ksec->sid, SECCLASS_KEY, KEY__VIEW, NULL);
6748}
6749#endif
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006750#endif
David Howells70a5bb72008-04-29 01:01:26 -07006751
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006752#ifdef CONFIG_SECURITY_INFINIBAND
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006753static int selinux_ib_pkey_access(void *ib_sec, u64 subnet_prefix, u16 pkey_val)
6754{
6755 struct common_audit_data ad;
6756 int err;
6757 u32 sid = 0;
6758 struct ib_security_struct *sec = ib_sec;
6759 struct lsm_ibpkey_audit ibpkey;
6760
Daniel Jurgens409dcf32017-05-19 15:48:59 +03006761 err = sel_ib_pkey_sid(subnet_prefix, pkey_val, &sid);
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006762 if (err)
6763 return err;
6764
6765 ad.type = LSM_AUDIT_DATA_IBPKEY;
6766 ibpkey.subnet_prefix = subnet_prefix;
6767 ibpkey.pkey = pkey_val;
6768 ad.u.ibpkey = &ibpkey;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006769 return avc_has_perm(&selinux_state,
6770 sec->sid, sid,
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006771 SECCLASS_INFINIBAND_PKEY,
6772 INFINIBAND_PKEY__ACCESS, &ad);
6773}
6774
Daniel Jurgensab861df2017-05-19 15:48:58 +03006775static int selinux_ib_endport_manage_subnet(void *ib_sec, const char *dev_name,
6776 u8 port_num)
6777{
6778 struct common_audit_data ad;
6779 int err;
6780 u32 sid = 0;
6781 struct ib_security_struct *sec = ib_sec;
6782 struct lsm_ibendport_audit ibendport;
6783
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006784 err = security_ib_endport_sid(&selinux_state, dev_name, port_num,
6785 &sid);
Daniel Jurgensab861df2017-05-19 15:48:58 +03006786
6787 if (err)
6788 return err;
6789
6790 ad.type = LSM_AUDIT_DATA_IBENDPORT;
Ondrej Mosnacek869cbee2021-05-12 16:32:10 +02006791 ibendport.dev_name = dev_name;
Daniel Jurgensab861df2017-05-19 15:48:58 +03006792 ibendport.port = port_num;
6793 ad.u.ibendport = &ibendport;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006794 return avc_has_perm(&selinux_state,
6795 sec->sid, sid,
Daniel Jurgensab861df2017-05-19 15:48:58 +03006796 SECCLASS_INFINIBAND_ENDPORT,
6797 INFINIBAND_ENDPORT__MANAGE_SUBNET, &ad);
6798}
6799
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006800static int selinux_ib_alloc_security(void **ib_sec)
6801{
6802 struct ib_security_struct *sec;
6803
6804 sec = kzalloc(sizeof(*sec), GFP_KERNEL);
6805 if (!sec)
6806 return -ENOMEM;
6807 sec->sid = current_sid();
6808
6809 *ib_sec = sec;
6810 return 0;
6811}
6812
6813static void selinux_ib_free_security(void *ib_sec)
6814{
6815 kfree(ib_sec);
6816}
Michael LeMayd7200242006-06-22 14:47:17 -07006817#endif
6818
Chenbo Fengec27c352017-10-18 13:00:25 -07006819#ifdef CONFIG_BPF_SYSCALL
6820static int selinux_bpf(int cmd, union bpf_attr *attr,
6821 unsigned int size)
6822{
6823 u32 sid = current_sid();
6824 int ret;
6825
6826 switch (cmd) {
6827 case BPF_MAP_CREATE:
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006828 ret = avc_has_perm(&selinux_state,
6829 sid, sid, SECCLASS_BPF, BPF__MAP_CREATE,
Chenbo Fengec27c352017-10-18 13:00:25 -07006830 NULL);
6831 break;
6832 case BPF_PROG_LOAD:
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006833 ret = avc_has_perm(&selinux_state,
6834 sid, sid, SECCLASS_BPF, BPF__PROG_LOAD,
Chenbo Fengec27c352017-10-18 13:00:25 -07006835 NULL);
6836 break;
6837 default:
6838 ret = 0;
6839 break;
6840 }
6841
6842 return ret;
6843}
6844
6845static u32 bpf_map_fmode_to_av(fmode_t fmode)
6846{
6847 u32 av = 0;
6848
6849 if (fmode & FMODE_READ)
6850 av |= BPF__MAP_READ;
6851 if (fmode & FMODE_WRITE)
6852 av |= BPF__MAP_WRITE;
6853 return av;
6854}
6855
Chenbo Fengf66e4482017-10-18 13:00:26 -07006856/* This function will check the file pass through unix socket or binder to see
6857 * if it is a bpf related object. And apply correspinding checks on the bpf
6858 * object based on the type. The bpf maps and programs, not like other files and
6859 * socket, are using a shared anonymous inode inside the kernel as their inode.
6860 * So checking that inode cannot identify if the process have privilege to
6861 * access the bpf object and that's why we have to add this additional check in
6862 * selinux_file_receive and selinux_binder_transfer_files.
6863 */
6864static int bpf_fd_pass(struct file *file, u32 sid)
6865{
6866 struct bpf_security_struct *bpfsec;
6867 struct bpf_prog *prog;
6868 struct bpf_map *map;
6869 int ret;
6870
6871 if (file->f_op == &bpf_map_fops) {
6872 map = file->private_data;
6873 bpfsec = map->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006874 ret = avc_has_perm(&selinux_state,
6875 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengf66e4482017-10-18 13:00:26 -07006876 bpf_map_fmode_to_av(file->f_mode), NULL);
6877 if (ret)
6878 return ret;
6879 } else if (file->f_op == &bpf_prog_fops) {
6880 prog = file->private_data;
6881 bpfsec = prog->aux->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006882 ret = avc_has_perm(&selinux_state,
6883 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengf66e4482017-10-18 13:00:26 -07006884 BPF__PROG_RUN, NULL);
6885 if (ret)
6886 return ret;
6887 }
6888 return 0;
6889}
6890
Chenbo Fengec27c352017-10-18 13:00:25 -07006891static int selinux_bpf_map(struct bpf_map *map, fmode_t fmode)
6892{
6893 u32 sid = current_sid();
6894 struct bpf_security_struct *bpfsec;
6895
6896 bpfsec = map->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006897 return avc_has_perm(&selinux_state,
6898 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengec27c352017-10-18 13:00:25 -07006899 bpf_map_fmode_to_av(fmode), NULL);
6900}
6901
6902static int selinux_bpf_prog(struct bpf_prog *prog)
6903{
6904 u32 sid = current_sid();
6905 struct bpf_security_struct *bpfsec;
6906
6907 bpfsec = prog->aux->security;
Stephen Smalley6b6bc622018-03-05 11:47:56 -05006908 return avc_has_perm(&selinux_state,
6909 sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengec27c352017-10-18 13:00:25 -07006910 BPF__PROG_RUN, NULL);
6911}
6912
6913static int selinux_bpf_map_alloc(struct bpf_map *map)
6914{
6915 struct bpf_security_struct *bpfsec;
6916
6917 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6918 if (!bpfsec)
6919 return -ENOMEM;
6920
6921 bpfsec->sid = current_sid();
6922 map->security = bpfsec;
6923
6924 return 0;
6925}
6926
6927static void selinux_bpf_map_free(struct bpf_map *map)
6928{
6929 struct bpf_security_struct *bpfsec = map->security;
6930
6931 map->security = NULL;
6932 kfree(bpfsec);
6933}
6934
6935static int selinux_bpf_prog_alloc(struct bpf_prog_aux *aux)
6936{
6937 struct bpf_security_struct *bpfsec;
6938
6939 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6940 if (!bpfsec)
6941 return -ENOMEM;
6942
6943 bpfsec->sid = current_sid();
6944 aux->security = bpfsec;
6945
6946 return 0;
6947}
6948
6949static void selinux_bpf_prog_free(struct bpf_prog_aux *aux)
6950{
6951 struct bpf_security_struct *bpfsec = aux->security;
6952
6953 aux->security = NULL;
6954 kfree(bpfsec);
6955}
6956#endif
6957
Casey Schauflerbbd36622018-11-12 09:30:56 -08006958struct lsm_blob_sizes selinux_blob_sizes __lsm_ro_after_init = {
6959 .lbs_cred = sizeof(struct task_security_struct),
Casey Schaufler33bf60c2018-11-12 12:02:49 -08006960 .lbs_file = sizeof(struct file_security_struct),
Casey Schauflerafb1cbe32018-09-21 17:19:29 -07006961 .lbs_inode = sizeof(struct inode_security_struct),
Casey Schauflerecd5f822018-11-20 11:55:02 -08006962 .lbs_ipc = sizeof(struct ipc_security_struct),
6963 .lbs_msg_msg = sizeof(struct msg_security_struct),
Casey Schaufler1aea7802021-04-22 17:41:15 +02006964 .lbs_superblock = sizeof(struct superblock_security_struct),
Casey Schauflerbbd36622018-11-12 09:30:56 -08006965};
6966
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -04006967#ifdef CONFIG_PERF_EVENTS
6968static int selinux_perf_event_open(struct perf_event_attr *attr, int type)
6969{
6970 u32 requested, sid = current_sid();
6971
6972 if (type == PERF_SECURITY_OPEN)
6973 requested = PERF_EVENT__OPEN;
6974 else if (type == PERF_SECURITY_CPU)
6975 requested = PERF_EVENT__CPU;
6976 else if (type == PERF_SECURITY_KERNEL)
6977 requested = PERF_EVENT__KERNEL;
6978 else if (type == PERF_SECURITY_TRACEPOINT)
6979 requested = PERF_EVENT__TRACEPOINT;
6980 else
6981 return -EINVAL;
6982
6983 return avc_has_perm(&selinux_state, sid, sid, SECCLASS_PERF_EVENT,
6984 requested, NULL);
6985}
6986
6987static int selinux_perf_event_alloc(struct perf_event *event)
6988{
6989 struct perf_event_security_struct *perfsec;
6990
6991 perfsec = kzalloc(sizeof(*perfsec), GFP_KERNEL);
6992 if (!perfsec)
6993 return -ENOMEM;
6994
6995 perfsec->sid = current_sid();
6996 event->security = perfsec;
6997
6998 return 0;
6999}
7000
7001static void selinux_perf_event_free(struct perf_event *event)
7002{
7003 struct perf_event_security_struct *perfsec = event->security;
7004
7005 event->security = NULL;
7006 kfree(perfsec);
7007}
7008
7009static int selinux_perf_event_read(struct perf_event *event)
7010{
7011 struct perf_event_security_struct *perfsec = event->security;
7012 u32 sid = current_sid();
7013
7014 return avc_has_perm(&selinux_state, sid, perfsec->sid,
7015 SECCLASS_PERF_EVENT, PERF_EVENT__READ, NULL);
7016}
7017
7018static int selinux_perf_event_write(struct perf_event *event)
7019{
7020 struct perf_event_security_struct *perfsec = event->security;
7021 u32 sid = current_sid();
7022
7023 return avc_has_perm(&selinux_state, sid, perfsec->sid,
7024 SECCLASS_PERF_EVENT, PERF_EVENT__WRITE, NULL);
7025}
7026#endif
7027
Paul Moore740b0342021-02-23 18:16:45 -05007028#ifdef CONFIG_IO_URING
7029/**
7030 * selinux_uring_override_creds - check the requested cred override
7031 * @new: the target creds
7032 *
7033 * Check to see if the current task is allowed to override it's credentials
7034 * to service an io_uring operation.
7035 */
7036static int selinux_uring_override_creds(const struct cred *new)
7037{
7038 return avc_has_perm(&selinux_state, current_sid(), cred_sid(new),
7039 SECCLASS_IO_URING, IO_URING__OVERRIDE_CREDS, NULL);
7040}
7041
7042/**
7043 * selinux_uring_sqpoll - check if a io_uring polling thread can be created
7044 *
7045 * Check to see if the current task is allowed to create a new io_uring
7046 * kernel polling thread.
7047 */
7048static int selinux_uring_sqpoll(void)
7049{
7050 int sid = current_sid();
7051
7052 return avc_has_perm(&selinux_state, sid, sid,
7053 SECCLASS_IO_URING, IO_URING__SQPOLL, NULL);
7054}
7055#endif /* CONFIG_IO_URING */
7056
Ondrej Mosnacekcfff75d2020-01-08 15:09:58 +01007057/*
7058 * IMPORTANT NOTE: When adding new hooks, please be careful to keep this order:
7059 * 1. any hooks that don't belong to (2.) or (3.) below,
7060 * 2. hooks that both access structures allocated by other hooks, and allocate
7061 * structures that can be later accessed by other hooks (mostly "cloning"
7062 * hooks),
7063 * 3. hooks that only allocate structures that can be later accessed by other
7064 * hooks ("allocating" hooks).
7065 *
7066 * Please follow block comment delimiters in the list to keep this order.
7067 *
7068 * This ordering is needed for SELinux runtime disable to work at least somewhat
7069 * safely. Breaking the ordering rules above might lead to NULL pointer derefs
7070 * when disabling SELinux at runtime.
7071 */
James Morrisca97d932017-02-15 00:18:51 +11007072static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {
Casey Schauflere20b0432015-05-02 15:11:36 -07007073 LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr),
7074 LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction),
7075 LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder),
7076 LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file),
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02007077
Casey Schauflere20b0432015-05-02 15:11:36 -07007078 LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check),
7079 LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme),
7080 LSM_HOOK_INIT(capget, selinux_capget),
7081 LSM_HOOK_INIT(capset, selinux_capset),
7082 LSM_HOOK_INIT(capable, selinux_capable),
7083 LSM_HOOK_INIT(quotactl, selinux_quotactl),
7084 LSM_HOOK_INIT(quota_on, selinux_quota_on),
7085 LSM_HOOK_INIT(syslog, selinux_syslog),
7086 LSM_HOOK_INIT(vm_enough_memory, selinux_vm_enough_memory),
Stephen Smalley79af7302015-01-21 10:54:10 -05007087
Casey Schauflere20b0432015-05-02 15:11:36 -07007088 LSM_HOOK_INIT(netlink_send, selinux_netlink_send),
Linus Torvalds1da177e2005-04-16 15:20:36 -07007089
Eric W. Biedermanb8bff592020-03-22 15:46:24 -05007090 LSM_HOOK_INIT(bprm_creds_for_exec, selinux_bprm_creds_for_exec),
Casey Schauflere20b0432015-05-02 15:11:36 -07007091 LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds),
7092 LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds),
Linus Torvalds1da177e2005-04-16 15:20:36 -07007093
Al Viro204cc0c2018-12-13 13:41:47 -05007094 LSM_HOOK_INIT(sb_free_mnt_opts, selinux_free_mnt_opts),
Olga Kornievskaia69c4a422021-02-26 22:37:55 -05007095 LSM_HOOK_INIT(sb_mnt_opts_compat, selinux_sb_mnt_opts_compat),
Casey Schauflere20b0432015-05-02 15:11:36 -07007096 LSM_HOOK_INIT(sb_remount, selinux_sb_remount),
7097 LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount),
7098 LSM_HOOK_INIT(sb_show_options, selinux_sb_show_options),
7099 LSM_HOOK_INIT(sb_statfs, selinux_sb_statfs),
7100 LSM_HOOK_INIT(sb_mount, selinux_mount),
7101 LSM_HOOK_INIT(sb_umount, selinux_umount),
7102 LSM_HOOK_INIT(sb_set_mnt_opts, selinux_set_mnt_opts),
7103 LSM_HOOK_INIT(sb_clone_mnt_opts, selinux_sb_clone_mnt_opts),
Linus Torvalds1da177e2005-04-16 15:20:36 -07007104
Stephen Smalley98aa0032020-01-17 15:24:07 -05007105 LSM_HOOK_INIT(move_mount, selinux_move_mount),
7106
Casey Schauflere20b0432015-05-02 15:11:36 -07007107 LSM_HOOK_INIT(dentry_init_security, selinux_dentry_init_security),
Vivek Goyala518b0a2016-07-13 10:44:53 -04007108 LSM_HOOK_INIT(dentry_create_files_as, selinux_dentry_create_files_as),
Eric Parise0007522008-03-05 10:31:54 -05007109
Casey Schauflere20b0432015-05-02 15:11:36 -07007110 LSM_HOOK_INIT(inode_free_security, selinux_inode_free_security),
7111 LSM_HOOK_INIT(inode_init_security, selinux_inode_init_security),
Daniel Colascione29cd6592021-01-08 14:22:22 -08007112 LSM_HOOK_INIT(inode_init_security_anon, selinux_inode_init_security_anon),
Casey Schauflere20b0432015-05-02 15:11:36 -07007113 LSM_HOOK_INIT(inode_create, selinux_inode_create),
7114 LSM_HOOK_INIT(inode_link, selinux_inode_link),
7115 LSM_HOOK_INIT(inode_unlink, selinux_inode_unlink),
7116 LSM_HOOK_INIT(inode_symlink, selinux_inode_symlink),
7117 LSM_HOOK_INIT(inode_mkdir, selinux_inode_mkdir),
7118 LSM_HOOK_INIT(inode_rmdir, selinux_inode_rmdir),
7119 LSM_HOOK_INIT(inode_mknod, selinux_inode_mknod),
7120 LSM_HOOK_INIT(inode_rename, selinux_inode_rename),
7121 LSM_HOOK_INIT(inode_readlink, selinux_inode_readlink),
7122 LSM_HOOK_INIT(inode_follow_link, selinux_inode_follow_link),
7123 LSM_HOOK_INIT(inode_permission, selinux_inode_permission),
7124 LSM_HOOK_INIT(inode_setattr, selinux_inode_setattr),
7125 LSM_HOOK_INIT(inode_getattr, selinux_inode_getattr),
7126 LSM_HOOK_INIT(inode_setxattr, selinux_inode_setxattr),
7127 LSM_HOOK_INIT(inode_post_setxattr, selinux_inode_post_setxattr),
7128 LSM_HOOK_INIT(inode_getxattr, selinux_inode_getxattr),
7129 LSM_HOOK_INIT(inode_listxattr, selinux_inode_listxattr),
7130 LSM_HOOK_INIT(inode_removexattr, selinux_inode_removexattr),
7131 LSM_HOOK_INIT(inode_getsecurity, selinux_inode_getsecurity),
7132 LSM_HOOK_INIT(inode_setsecurity, selinux_inode_setsecurity),
7133 LSM_HOOK_INIT(inode_listsecurity, selinux_inode_listsecurity),
7134 LSM_HOOK_INIT(inode_getsecid, selinux_inode_getsecid),
Vivek Goyal56909eb2016-07-13 10:44:48 -04007135 LSM_HOOK_INIT(inode_copy_up, selinux_inode_copy_up),
Vivek Goyal19472b62016-07-13 10:44:50 -04007136 LSM_HOOK_INIT(inode_copy_up_xattr, selinux_inode_copy_up_xattr),
Aaron Goidelac5656d2019-08-12 11:20:00 -04007137 LSM_HOOK_INIT(path_notify, selinux_path_notify),
Linus Torvalds1da177e2005-04-16 15:20:36 -07007138
Ondrej Mosnacekec882da2019-02-22 15:57:17 +01007139 LSM_HOOK_INIT(kernfs_init_security, selinux_kernfs_init_security),
7140
Casey Schauflere20b0432015-05-02 15:11:36 -07007141 LSM_HOOK_INIT(file_permission, selinux_file_permission),
7142 LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07007143 LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl),
7144 LSM_HOOK_INIT(mmap_file, selinux_mmap_file),
7145 LSM_HOOK_INIT(mmap_addr, selinux_mmap_addr),
7146 LSM_HOOK_INIT(file_mprotect, selinux_file_mprotect),
7147 LSM_HOOK_INIT(file_lock, selinux_file_lock),
7148 LSM_HOOK_INIT(file_fcntl, selinux_file_fcntl),
7149 LSM_HOOK_INIT(file_set_fowner, selinux_file_set_fowner),
7150 LSM_HOOK_INIT(file_send_sigiotask, selinux_file_send_sigiotask),
7151 LSM_HOOK_INIT(file_receive, selinux_file_receive),
Linus Torvalds1da177e2005-04-16 15:20:36 -07007152
Casey Schauflere20b0432015-05-02 15:11:36 -07007153 LSM_HOOK_INIT(file_open, selinux_file_open),
Linus Torvalds1da177e2005-04-16 15:20:36 -07007154
Tetsuo Handaa79be232017-03-28 23:08:45 +09007155 LSM_HOOK_INIT(task_alloc, selinux_task_alloc),
Casey Schauflere20b0432015-05-02 15:11:36 -07007156 LSM_HOOK_INIT(cred_prepare, selinux_cred_prepare),
7157 LSM_HOOK_INIT(cred_transfer, selinux_cred_transfer),
Matthew Garrett3ec30112018-01-08 13:36:19 -08007158 LSM_HOOK_INIT(cred_getsecid, selinux_cred_getsecid),
Casey Schauflere20b0432015-05-02 15:11:36 -07007159 LSM_HOOK_INIT(kernel_act_as, selinux_kernel_act_as),
7160 LSM_HOOK_INIT(kernel_create_files_as, selinux_kernel_create_files_as),
7161 LSM_HOOK_INIT(kernel_module_request, selinux_kernel_module_request),
Mimi Zoharc77b8cd2018-07-13 14:06:02 -04007162 LSM_HOOK_INIT(kernel_load_data, selinux_kernel_load_data),
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07007163 LSM_HOOK_INIT(kernel_read_file, selinux_kernel_read_file),
Casey Schauflere20b0432015-05-02 15:11:36 -07007164 LSM_HOOK_INIT(task_setpgid, selinux_task_setpgid),
7165 LSM_HOOK_INIT(task_getpgid, selinux_task_getpgid),
7166 LSM_HOOK_INIT(task_getsid, selinux_task_getsid),
Paul Mooreeb1231f2021-02-18 15:13:40 -05007167 LSM_HOOK_INIT(task_getsecid_subj, selinux_task_getsecid_subj),
7168 LSM_HOOK_INIT(task_getsecid_obj, selinux_task_getsecid_obj),
Casey Schauflere20b0432015-05-02 15:11:36 -07007169 LSM_HOOK_INIT(task_setnice, selinux_task_setnice),
7170 LSM_HOOK_INIT(task_setioprio, selinux_task_setioprio),
7171 LSM_HOOK_INIT(task_getioprio, selinux_task_getioprio),
Stephen Smalley791ec492017-02-17 07:57:00 -05007172 LSM_HOOK_INIT(task_prlimit, selinux_task_prlimit),
Casey Schauflere20b0432015-05-02 15:11:36 -07007173 LSM_HOOK_INIT(task_setrlimit, selinux_task_setrlimit),
7174 LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler),
7175 LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler),
7176 LSM_HOOK_INIT(task_movememory, selinux_task_movememory),
7177 LSM_HOOK_INIT(task_kill, selinux_task_kill),
Casey Schauflere20b0432015-05-02 15:11:36 -07007178 LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode),
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09007179
Casey Schauflere20b0432015-05-02 15:11:36 -07007180 LSM_HOOK_INIT(ipc_permission, selinux_ipc_permission),
7181 LSM_HOOK_INIT(ipc_getsecid, selinux_ipc_getsecid),
Linus Torvalds1da177e2005-04-16 15:20:36 -07007182
Casey Schauflere20b0432015-05-02 15:11:36 -07007183 LSM_HOOK_INIT(msg_queue_associate, selinux_msg_queue_associate),
7184 LSM_HOOK_INIT(msg_queue_msgctl, selinux_msg_queue_msgctl),
7185 LSM_HOOK_INIT(msg_queue_msgsnd, selinux_msg_queue_msgsnd),
7186 LSM_HOOK_INIT(msg_queue_msgrcv, selinux_msg_queue_msgrcv),
Linus Torvalds1da177e2005-04-16 15:20:36 -07007187
Casey Schauflere20b0432015-05-02 15:11:36 -07007188 LSM_HOOK_INIT(shm_associate, selinux_shm_associate),
7189 LSM_HOOK_INIT(shm_shmctl, selinux_shm_shmctl),
7190 LSM_HOOK_INIT(shm_shmat, selinux_shm_shmat),
Linus Torvalds1da177e2005-04-16 15:20:36 -07007191
Casey Schauflere20b0432015-05-02 15:11:36 -07007192 LSM_HOOK_INIT(sem_associate, selinux_sem_associate),
7193 LSM_HOOK_INIT(sem_semctl, selinux_sem_semctl),
7194 LSM_HOOK_INIT(sem_semop, selinux_sem_semop),
Linus Torvalds1da177e2005-04-16 15:20:36 -07007195
Casey Schauflere20b0432015-05-02 15:11:36 -07007196 LSM_HOOK_INIT(d_instantiate, selinux_d_instantiate),
Linus Torvalds1da177e2005-04-16 15:20:36 -07007197
Casey Schauflere20b0432015-05-02 15:11:36 -07007198 LSM_HOOK_INIT(getprocattr, selinux_getprocattr),
7199 LSM_HOOK_INIT(setprocattr, selinux_setprocattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07007200
Casey Schauflere20b0432015-05-02 15:11:36 -07007201 LSM_HOOK_INIT(ismaclabel, selinux_ismaclabel),
Casey Schauflere20b0432015-05-02 15:11:36 -07007202 LSM_HOOK_INIT(secctx_to_secid, selinux_secctx_to_secid),
7203 LSM_HOOK_INIT(release_secctx, selinux_release_secctx),
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05007204 LSM_HOOK_INIT(inode_invalidate_secctx, selinux_inode_invalidate_secctx),
Casey Schauflere20b0432015-05-02 15:11:36 -07007205 LSM_HOOK_INIT(inode_notifysecctx, selinux_inode_notifysecctx),
7206 LSM_HOOK_INIT(inode_setsecctx, selinux_inode_setsecctx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07007207
Casey Schauflere20b0432015-05-02 15:11:36 -07007208 LSM_HOOK_INIT(unix_stream_connect, selinux_socket_unix_stream_connect),
7209 LSM_HOOK_INIT(unix_may_send, selinux_socket_unix_may_send),
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07007210
Casey Schauflere20b0432015-05-02 15:11:36 -07007211 LSM_HOOK_INIT(socket_create, selinux_socket_create),
7212 LSM_HOOK_INIT(socket_post_create, selinux_socket_post_create),
David Herrmann0b811db2018-05-04 16:28:21 +02007213 LSM_HOOK_INIT(socket_socketpair, selinux_socket_socketpair),
Casey Schauflere20b0432015-05-02 15:11:36 -07007214 LSM_HOOK_INIT(socket_bind, selinux_socket_bind),
7215 LSM_HOOK_INIT(socket_connect, selinux_socket_connect),
7216 LSM_HOOK_INIT(socket_listen, selinux_socket_listen),
7217 LSM_HOOK_INIT(socket_accept, selinux_socket_accept),
7218 LSM_HOOK_INIT(socket_sendmsg, selinux_socket_sendmsg),
7219 LSM_HOOK_INIT(socket_recvmsg, selinux_socket_recvmsg),
7220 LSM_HOOK_INIT(socket_getsockname, selinux_socket_getsockname),
7221 LSM_HOOK_INIT(socket_getpeername, selinux_socket_getpeername),
7222 LSM_HOOK_INIT(socket_getsockopt, selinux_socket_getsockopt),
7223 LSM_HOOK_INIT(socket_setsockopt, selinux_socket_setsockopt),
7224 LSM_HOOK_INIT(socket_shutdown, selinux_socket_shutdown),
7225 LSM_HOOK_INIT(socket_sock_rcv_skb, selinux_socket_sock_rcv_skb),
7226 LSM_HOOK_INIT(socket_getpeersec_stream,
7227 selinux_socket_getpeersec_stream),
7228 LSM_HOOK_INIT(socket_getpeersec_dgram, selinux_socket_getpeersec_dgram),
Casey Schauflere20b0432015-05-02 15:11:36 -07007229 LSM_HOOK_INIT(sk_free_security, selinux_sk_free_security),
7230 LSM_HOOK_INIT(sk_clone_security, selinux_sk_clone_security),
7231 LSM_HOOK_INIT(sk_getsecid, selinux_sk_getsecid),
7232 LSM_HOOK_INIT(sock_graft, selinux_sock_graft),
Richard Hainesd4529302018-02-13 20:57:18 +00007233 LSM_HOOK_INIT(sctp_assoc_request, selinux_sctp_assoc_request),
7234 LSM_HOOK_INIT(sctp_sk_clone, selinux_sctp_sk_clone),
7235 LSM_HOOK_INIT(sctp_bind_connect, selinux_sctp_bind_connect),
Casey Schauflere20b0432015-05-02 15:11:36 -07007236 LSM_HOOK_INIT(inet_conn_request, selinux_inet_conn_request),
7237 LSM_HOOK_INIT(inet_csk_clone, selinux_inet_csk_clone),
7238 LSM_HOOK_INIT(inet_conn_established, selinux_inet_conn_established),
7239 LSM_HOOK_INIT(secmark_relabel_packet, selinux_secmark_relabel_packet),
7240 LSM_HOOK_INIT(secmark_refcount_inc, selinux_secmark_refcount_inc),
7241 LSM_HOOK_INIT(secmark_refcount_dec, selinux_secmark_refcount_dec),
7242 LSM_HOOK_INIT(req_classify_flow, selinux_req_classify_flow),
Casey Schauflere20b0432015-05-02 15:11:36 -07007243 LSM_HOOK_INIT(tun_dev_free_security, selinux_tun_dev_free_security),
7244 LSM_HOOK_INIT(tun_dev_create, selinux_tun_dev_create),
7245 LSM_HOOK_INIT(tun_dev_attach_queue, selinux_tun_dev_attach_queue),
7246 LSM_HOOK_INIT(tun_dev_attach, selinux_tun_dev_attach),
7247 LSM_HOOK_INIT(tun_dev_open, selinux_tun_dev_open),
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03007248#ifdef CONFIG_SECURITY_INFINIBAND
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03007249 LSM_HOOK_INIT(ib_pkey_access, selinux_ib_pkey_access),
Daniel Jurgensab861df2017-05-19 15:48:58 +03007250 LSM_HOOK_INIT(ib_endport_manage_subnet,
7251 selinux_ib_endport_manage_subnet),
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03007252 LSM_HOOK_INIT(ib_free_security, selinux_ib_free_security),
7253#endif
Trent Jaegerd28d1e02005-12-13 23:12:40 -08007254#ifdef CONFIG_SECURITY_NETWORK_XFRM
Casey Schauflere20b0432015-05-02 15:11:36 -07007255 LSM_HOOK_INIT(xfrm_policy_free_security, selinux_xfrm_policy_free),
7256 LSM_HOOK_INIT(xfrm_policy_delete_security, selinux_xfrm_policy_delete),
Casey Schauflere20b0432015-05-02 15:11:36 -07007257 LSM_HOOK_INIT(xfrm_state_free_security, selinux_xfrm_state_free),
7258 LSM_HOOK_INIT(xfrm_state_delete_security, selinux_xfrm_state_delete),
7259 LSM_HOOK_INIT(xfrm_policy_lookup, selinux_xfrm_policy_lookup),
7260 LSM_HOOK_INIT(xfrm_state_pol_flow_match,
7261 selinux_xfrm_state_pol_flow_match),
7262 LSM_HOOK_INIT(xfrm_decode_session, selinux_xfrm_decode_session),
Linus Torvalds1da177e2005-04-16 15:20:36 -07007263#endif
Michael LeMayd7200242006-06-22 14:47:17 -07007264
7265#ifdef CONFIG_KEYS
Casey Schauflere20b0432015-05-02 15:11:36 -07007266 LSM_HOOK_INIT(key_free, selinux_key_free),
7267 LSM_HOOK_INIT(key_permission, selinux_key_permission),
7268 LSM_HOOK_INIT(key_getsecurity, selinux_key_getsecurity),
David Howells3e412cc2020-01-14 17:07:13 +00007269#ifdef CONFIG_KEY_NOTIFICATIONS
7270 LSM_HOOK_INIT(watch_key, selinux_watch_key),
7271#endif
Michael LeMayd7200242006-06-22 14:47:17 -07007272#endif
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02007273
7274#ifdef CONFIG_AUDIT
Casey Schauflere20b0432015-05-02 15:11:36 -07007275 LSM_HOOK_INIT(audit_rule_known, selinux_audit_rule_known),
7276 LSM_HOOK_INIT(audit_rule_match, selinux_audit_rule_match),
7277 LSM_HOOK_INIT(audit_rule_free, selinux_audit_rule_free),
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02007278#endif
Chenbo Fengec27c352017-10-18 13:00:25 -07007279
7280#ifdef CONFIG_BPF_SYSCALL
7281 LSM_HOOK_INIT(bpf, selinux_bpf),
7282 LSM_HOOK_INIT(bpf_map, selinux_bpf_map),
7283 LSM_HOOK_INIT(bpf_prog, selinux_bpf_prog),
Chenbo Fengec27c352017-10-18 13:00:25 -07007284 LSM_HOOK_INIT(bpf_map_free_security, selinux_bpf_map_free),
7285 LSM_HOOK_INIT(bpf_prog_free_security, selinux_bpf_prog_free),
7286#endif
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -04007287
7288#ifdef CONFIG_PERF_EVENTS
7289 LSM_HOOK_INIT(perf_event_open, selinux_perf_event_open),
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -04007290 LSM_HOOK_INIT(perf_event_free, selinux_perf_event_free),
7291 LSM_HOOK_INIT(perf_event_read, selinux_perf_event_read),
7292 LSM_HOOK_INIT(perf_event_write, selinux_perf_event_write),
7293#endif
Stephen Smalley59438b462019-11-27 12:04:36 -05007294
Paul Moore740b0342021-02-23 18:16:45 -05007295#ifdef CONFIG_IO_URING
7296 LSM_HOOK_INIT(uring_override_creds, selinux_uring_override_creds),
7297 LSM_HOOK_INIT(uring_sqpoll, selinux_uring_sqpoll),
7298#endif
Ondrej Mosnacekcfff75d2020-01-08 15:09:58 +01007299
7300 /*
7301 * PUT "CLONING" (ACCESSING + ALLOCATING) HOOKS HERE
7302 */
7303 LSM_HOOK_INIT(fs_context_dup, selinux_fs_context_dup),
7304 LSM_HOOK_INIT(fs_context_parse_param, selinux_fs_context_parse_param),
7305 LSM_HOOK_INIT(sb_eat_lsm_opts, selinux_sb_eat_lsm_opts),
7306 LSM_HOOK_INIT(sb_add_mnt_opt, selinux_add_mnt_opt),
7307#ifdef CONFIG_SECURITY_NETWORK_XFRM
7308 LSM_HOOK_INIT(xfrm_policy_clone_security, selinux_xfrm_policy_clone),
7309#endif
7310
7311 /*
7312 * PUT "ALLOCATING" HOOKS HERE
7313 */
7314 LSM_HOOK_INIT(msg_msg_alloc_security, selinux_msg_msg_alloc_security),
7315 LSM_HOOK_INIT(msg_queue_alloc_security,
7316 selinux_msg_queue_alloc_security),
7317 LSM_HOOK_INIT(shm_alloc_security, selinux_shm_alloc_security),
7318 LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security),
7319 LSM_HOOK_INIT(inode_alloc_security, selinux_inode_alloc_security),
7320 LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security),
7321 LSM_HOOK_INIT(secid_to_secctx, selinux_secid_to_secctx),
7322 LSM_HOOK_INIT(inode_getsecctx, selinux_inode_getsecctx),
7323 LSM_HOOK_INIT(sk_alloc_security, selinux_sk_alloc_security),
7324 LSM_HOOK_INIT(tun_dev_alloc_security, selinux_tun_dev_alloc_security),
7325#ifdef CONFIG_SECURITY_INFINIBAND
7326 LSM_HOOK_INIT(ib_alloc_security, selinux_ib_alloc_security),
7327#endif
7328#ifdef CONFIG_SECURITY_NETWORK_XFRM
7329 LSM_HOOK_INIT(xfrm_policy_alloc_security, selinux_xfrm_policy_alloc),
7330 LSM_HOOK_INIT(xfrm_state_alloc, selinux_xfrm_state_alloc),
7331 LSM_HOOK_INIT(xfrm_state_alloc_acquire,
7332 selinux_xfrm_state_alloc_acquire),
7333#endif
7334#ifdef CONFIG_KEYS
7335 LSM_HOOK_INIT(key_alloc, selinux_key_alloc),
7336#endif
7337#ifdef CONFIG_AUDIT
7338 LSM_HOOK_INIT(audit_rule_init, selinux_audit_rule_init),
7339#endif
7340#ifdef CONFIG_BPF_SYSCALL
7341 LSM_HOOK_INIT(bpf_map_alloc_security, selinux_bpf_map_alloc),
7342 LSM_HOOK_INIT(bpf_prog_alloc_security, selinux_bpf_prog_alloc),
7343#endif
7344#ifdef CONFIG_PERF_EVENTS
7345 LSM_HOOK_INIT(perf_event_alloc, selinux_perf_event_alloc),
7346#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007347};
7348
7349static __init int selinux_init(void)
7350{
peter enderborgc103a912018-06-12 10:09:03 +02007351 pr_info("SELinux: Initializing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007352
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007353 memset(&selinux_state, 0, sizeof(selinux_state));
Paul Mooree5a5ca92018-03-01 17:38:30 -05007354 enforcing_set(&selinux_state, selinux_enforcing_boot);
Lakshmi Ramasubramanian8861d0a2020-09-14 10:31:57 -07007355 checkreqprot_set(&selinux_state, selinux_checkreqprot_boot);
Stephen Smalley6b6bc622018-03-05 11:47:56 -05007356 selinux_avc_init(&selinux_state.avc);
Ondrej Mosnacek4b36cb72020-01-17 14:15:14 +01007357 mutex_init(&selinux_state.status_lock);
Stephen Smalley9ff9abc2020-08-26 13:28:53 -04007358 mutex_init(&selinux_state.policy_mutex);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007359
Linus Torvalds1da177e2005-04-16 15:20:36 -07007360 /* Set the security state for the initial task. */
David Howellsd84f4f92008-11-14 10:39:23 +11007361 cred_init_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007362
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04007363 default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
7364
Linus Torvalds1da177e2005-04-16 15:20:36 -07007365 avc_init();
7366
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007367 avtab_cache_init();
7368
7369 ebitmap_cache_init();
7370
7371 hashtab_cache_init();
7372
Casey Schauflerd69dece52017-01-18 17:09:05 -08007373 security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks), "selinux");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007374
Paul Moore615e51f2014-06-26 14:33:56 -04007375 if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET))
7376 panic("SELinux: Unable to register AVC netcache callback\n");
7377
Daniel Jurgens8f408ab2017-05-19 15:48:53 +03007378 if (avc_add_callback(selinux_lsm_notifier_avc_callback, AVC_CALLBACK_RESET))
7379 panic("SELinux: Unable to register AVC LSM notifier callback\n");
7380
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007381 if (selinux_enforcing_boot)
peter enderborgc103a912018-06-12 10:09:03 +02007382 pr_debug("SELinux: Starting in enforcing mode\n");
Eric Paris828dfe12008-04-17 13:17:49 -04007383 else
peter enderborgc103a912018-06-12 10:09:03 +02007384 pr_debug("SELinux: Starting in permissive mode\n");
Michael LeMayd7200242006-06-22 14:47:17 -07007385
Al Virod7167b12019-09-07 07:23:15 -04007386 fs_validate_description("selinux", selinux_fs_parameters);
David Howells442155c2018-11-01 23:07:24 +00007387
Linus Torvalds1da177e2005-04-16 15:20:36 -07007388 return 0;
7389}
7390
Al Viroe8c26252010-03-23 06:36:54 -04007391static void delayed_superblock_init(struct super_block *sb, void *unused)
7392{
Al Viro204cc0c2018-12-13 13:41:47 -05007393 selinux_set_mnt_opts(sb, NULL, 0, NULL);
Al Viroe8c26252010-03-23 06:36:54 -04007394}
7395
Linus Torvalds1da177e2005-04-16 15:20:36 -07007396void selinux_complete_init(void)
7397{
peter enderborgc103a912018-06-12 10:09:03 +02007398 pr_debug("SELinux: Completing initialization.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007399
7400 /* Set up any superblocks initialized prior to the policy load. */
peter enderborgc103a912018-06-12 10:09:03 +02007401 pr_debug("SELinux: Setting up existing superblocks.\n");
Al Viroe8c26252010-03-23 06:36:54 -04007402 iterate_supers(delayed_superblock_init, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007403}
7404
7405/* SELinux requires early initialization in order to label
7406 all processes and objects when they are created. */
Kees Cook3d6e5f62018-10-10 17:18:23 -07007407DEFINE_LSM(selinux) = {
Kees Cook07aed2f2018-10-10 17:18:24 -07007408 .name = "selinux",
Kees Cook14bd99c2018-09-19 19:57:06 -07007409 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
Stephen Smalley6c5a6822019-12-17 09:15:10 -05007410 .enabled = &selinux_enabled_boot,
Casey Schauflerbbd36622018-11-12 09:30:56 -08007411 .blobs = &selinux_blob_sizes,
Kees Cook3d6e5f62018-10-10 17:18:23 -07007412 .init = selinux_init,
7413};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007414
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007415#if defined(CONFIG_NETFILTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007416
Florian Westphal591bb272017-07-26 11:40:52 +02007417static const struct nf_hook_ops selinux_nf_ops[] = {
Paul Mooreeffad8d2008-01-29 08:49:27 -05007418 {
Florian Westphal4342f702021-10-11 22:22:29 +02007419 .hook = selinux_ip_postroute,
Alban Crequy2597a832012-05-14 03:56:39 +00007420 .pf = NFPROTO_IPV4,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007421 .hooknum = NF_INET_POST_ROUTING,
7422 .priority = NF_IP_PRI_SELINUX_LAST,
7423 },
7424 {
Florian Westphal4342f702021-10-11 22:22:29 +02007425 .hook = selinux_ip_forward,
Alban Crequy2597a832012-05-14 03:56:39 +00007426 .pf = NFPROTO_IPV4,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007427 .hooknum = NF_INET_FORWARD,
7428 .priority = NF_IP_PRI_SELINUX_FIRST,
Paul Moore948bf852008-10-10 10:16:32 -04007429 },
7430 {
Florian Westphal4342f702021-10-11 22:22:29 +02007431 .hook = selinux_ip_output,
Alban Crequy2597a832012-05-14 03:56:39 +00007432 .pf = NFPROTO_IPV4,
Paul Moore948bf852008-10-10 10:16:32 -04007433 .hooknum = NF_INET_LOCAL_OUT,
7434 .priority = NF_IP_PRI_SELINUX_FIRST,
Jiri Pirko25db6be2014-09-03 17:42:13 +02007435 },
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04007436#if IS_ENABLED(CONFIG_IPV6)
Paul Mooreeffad8d2008-01-29 08:49:27 -05007437 {
Florian Westphal4342f702021-10-11 22:22:29 +02007438 .hook = selinux_ip_postroute,
Alban Crequy2597a832012-05-14 03:56:39 +00007439 .pf = NFPROTO_IPV6,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007440 .hooknum = NF_INET_POST_ROUTING,
7441 .priority = NF_IP6_PRI_SELINUX_LAST,
7442 },
7443 {
Florian Westphal4342f702021-10-11 22:22:29 +02007444 .hook = selinux_ip_forward,
Alban Crequy2597a832012-05-14 03:56:39 +00007445 .pf = NFPROTO_IPV6,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007446 .hooknum = NF_INET_FORWARD,
7447 .priority = NF_IP6_PRI_SELINUX_FIRST,
Jiri Pirko25db6be2014-09-03 17:42:13 +02007448 },
Huw Davies2917f572016-06-27 15:06:15 -04007449 {
Florian Westphal4342f702021-10-11 22:22:29 +02007450 .hook = selinux_ip_output,
Huw Davies2917f572016-06-27 15:06:15 -04007451 .pf = NFPROTO_IPV6,
7452 .hooknum = NF_INET_LOCAL_OUT,
7453 .priority = NF_IP6_PRI_SELINUX_FIRST,
7454 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07007455#endif /* IPV6 */
Jiri Pirko25db6be2014-09-03 17:42:13 +02007456};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007457
Florian Westphal8e71bf72017-04-21 11:49:09 +02007458static int __net_init selinux_nf_register(struct net *net)
7459{
7460 return nf_register_net_hooks(net, selinux_nf_ops,
7461 ARRAY_SIZE(selinux_nf_ops));
7462}
7463
7464static void __net_exit selinux_nf_unregister(struct net *net)
7465{
7466 nf_unregister_net_hooks(net, selinux_nf_ops,
7467 ARRAY_SIZE(selinux_nf_ops));
7468}
7469
7470static struct pernet_operations selinux_net_ops = {
7471 .init = selinux_nf_register,
7472 .exit = selinux_nf_unregister,
7473};
7474
Linus Torvalds1da177e2005-04-16 15:20:36 -07007475static int __init selinux_nf_ip_init(void)
7476{
Jiri Pirko25db6be2014-09-03 17:42:13 +02007477 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007478
Stephen Smalley6c5a6822019-12-17 09:15:10 -05007479 if (!selinux_enabled_boot)
Jiri Pirko25db6be2014-09-03 17:42:13 +02007480 return 0;
Eric Parisfadcdb42007-02-22 18:11:31 -05007481
peter enderborgc103a912018-06-12 10:09:03 +02007482 pr_debug("SELinux: Registering netfilter hooks\n");
Eric Parisfadcdb42007-02-22 18:11:31 -05007483
Florian Westphal8e71bf72017-04-21 11:49:09 +02007484 err = register_pernet_subsys(&selinux_net_ops);
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07007485 if (err)
Florian Westphal8e71bf72017-04-21 11:49:09 +02007486 panic("SELinux: register_pernet_subsys: error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007487
Jiri Pirko25db6be2014-09-03 17:42:13 +02007488 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007489}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007490__initcall(selinux_nf_ip_init);
7491
7492#ifdef CONFIG_SECURITY_SELINUX_DISABLE
7493static void selinux_nf_ip_exit(void)
7494{
peter enderborgc103a912018-06-12 10:09:03 +02007495 pr_debug("SELinux: Unregistering netfilter hooks\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007496
Florian Westphal8e71bf72017-04-21 11:49:09 +02007497 unregister_pernet_subsys(&selinux_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007498}
7499#endif
7500
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007501#else /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007502
7503#ifdef CONFIG_SECURITY_SELINUX_DISABLE
7504#define selinux_nf_ip_exit()
7505#endif
7506
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007507#endif /* CONFIG_NETFILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007508
7509#ifdef CONFIG_SECURITY_SELINUX_DISABLE
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007510int selinux_disable(struct selinux_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007511{
Ondrej Mosnacek65cddd52020-01-07 14:31:53 +01007512 if (selinux_initialized(state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007513 /* Not permitted after initial policy load. */
7514 return -EINVAL;
7515 }
7516
Ondrej Mosnacek65cddd52020-01-07 14:31:53 +01007517 if (selinux_disabled(state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007518 /* Only do this once. */
7519 return -EINVAL;
7520 }
7521
Ondrej Mosnacek65cddd52020-01-07 14:31:53 +01007522 selinux_mark_disabled(state);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007523
peter enderborgc103a912018-06-12 10:09:03 +02007524 pr_info("SELinux: Disabled at runtime.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007525
Ondrej Mosnacekcfff75d2020-01-08 15:09:58 +01007526 /*
7527 * Unregister netfilter hooks.
7528 * Must be done before security_delete_hooks() to avoid breaking
7529 * runtime disable.
7530 */
7531 selinux_nf_ip_exit();
7532
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07007533 security_delete_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007534
Eric Parisaf8ff042009-09-20 21:23:01 -04007535 /* Try to destroy the avc node cache */
7536 avc_disable();
7537
Linus Torvalds1da177e2005-04-16 15:20:36 -07007538 /* Unregister selinuxfs. */
7539 exit_sel_fs();
7540
7541 return 0;
7542}
7543#endif