blob: 3448454c82d033f7be8a6b942c462c6816360302 [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/*
Stephen Smalley90aa4f52023-07-18 13:13:35 -04003 * Security-Enhanced Linux (SELinux) security module
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * This file contains the SELinux hook function implementations.
6 *
Stephen Smalley0fe53222023-07-19 11:12:50 -04007 * Authors: Stephen Smalley, <stephen.smalley.work@gmail.com>
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/errno.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010029#include <linux/sched/signal.h>
Ingo Molnar29930022017-02-08 18:51:36 +010030#include <linux/sched/task.h>
Casey Schaufler3c4ed7b2015-05-02 15:10:46 -070031#include <linux/lsm_hooks.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/xattr.h>
33#include <linux/capability.h>
34#include <linux/unistd.h>
35#include <linux/mm.h>
36#include <linux/mman.h>
37#include <linux/slab.h>
38#include <linux/pagemap.h>
Eric Paris0b24dcb2011-02-25 15:39:20 -050039#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/swap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/spinlock.h>
42#include <linux/syscalls.h>
Eric Paris2a7dba32011-02-01 11:05:39 -050043#include <linux/dcache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/file.h>
Al Viro9f3acc32008-04-24 07:44:08 -040045#include <linux/fdtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/namei.h>
47#include <linux/mount.h>
David Howells442155c2018-11-01 23:07:24 +000048#include <linux/fs_context.h>
49#include <linux/fs_parser.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/netfilter_ipv4.h>
51#include <linux/netfilter_ipv6.h>
52#include <linux/tty.h>
53#include <net/icmp.h>
Stephen Hemminger227b60f2007-10-10 17:30:46 -070054#include <net/ip.h> /* for local_port_range[] */
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <net/tcp.h> /* struct or_callable used in sock_rcv_skb */
Paul Moore47180062013-12-04 16:10:45 -050056#include <net/inet_connection_sock.h>
Paul Moore220deb92008-01-29 08:38:23 -050057#include <net/net_namespace.h>
Paul Moored621d352008-01-29 08:43:36 -050058#include <net/netlabel.h>
Eric Parisf5269712008-05-14 11:27:45 -040059#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <asm/ioctls.h>
Arun Sharma600634972011-07-26 16:09:06 -070061#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <linux/bitops.h>
63#include <linux/interrupt.h>
64#include <linux/netdevice.h> /* for network interface checks */
Hong zhi guo77954982013-03-27 06:49:35 +000065#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <linux/tcp.h>
67#include <linux/udp.h>
James Morris2ee92d42006-11-13 16:09:01 -080068#include <linux/dccp.h>
Richard Hainesd4529302018-02-13 20:57:18 +000069#include <linux/sctp.h>
70#include <net/sctp/structs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#include <linux/quota.h>
72#include <linux/un.h> /* for Unix socket types */
73#include <net/af_unix.h> /* for Unix socket types */
74#include <linux/parser.h>
75#include <linux/nfs_mount.h>
76#include <net/ipv6.h>
77#include <linux/hugetlb.h>
78#include <linux/personality.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#include <linux/audit.h>
Eric Paris6931dfc2005-06-30 02:58:51 -070080#include <linux/string.h>
Eric Paris23970742006-09-25 23:32:01 -070081#include <linux/mutex.h>
Frank Mayharf06febc2008-09-12 09:54:39 -070082#include <linux/posix-timers.h>
Kees Cook00234592010-02-03 15:36:43 -080083#include <linux/syslog.h>
Serge E. Hallyn34867402011-03-23 16:43:17 -070084#include <linux/user_namespace.h>
Paul Gortmaker44fc7ea2011-05-26 20:52:10 -040085#include <linux/export.h>
Al Viro40401532012-02-13 03:58:52 +000086#include <linux/msg.h>
87#include <linux/shm.h>
Kent Overstreetbc46ef32023-12-11 13:58:25 -050088#include <uapi/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>
Pavel Begunkovb66509b2023-12-01 00:57:35 +000095#include <linux/io_uring/cmd.h>
Casey Schauflerf3b87882023-09-12 13:56:46 -070096#include <uapi/linux/lsm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98#include "avc.h"
99#include "objsec.h"
100#include "netif.h"
Paul Moore224dfbd2008-01-29 08:38:13 -0500101#include "netnode.h"
Paul Moore3e112172008-04-10 10:48:14 -0400102#include "netport.h"
Daniel Jurgens409dcf32017-05-19 15:48:59 +0300103#include "ibpkey.h"
Trent Jaegerd28d1e02005-12-13 23:12:40 -0800104#include "xfrm.h"
Paul Moorec60475b2007-02-28 15:14:23 -0500105#include "netlabel.h"
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +0200106#include "audit.h"
James Morris7b98a582011-08-30 12:52:32 +1000107#include "avc_ss.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Roberto Sassu6bcdfd22023-06-10 09:57:35 +0200109#define SELINUX_INODE_INIT_XATTRS 1
110
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500111struct selinux_state selinux_state;
112
Paul Moored621d352008-01-29 08:43:36 -0500113/* SECMARK reference count */
James Morris56a4ca92011-08-17 11:08:43 +1000114static atomic_t selinux_secmark_refcount = ATOMIC_INIT(0);
Paul Moored621d352008-01-29 08:43:36 -0500115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116#ifdef CONFIG_SECURITY_SELINUX_DEVELOP
Stephen Smalley6c5a6822019-12-17 09:15:10 -0500117static int selinux_enforcing_boot __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119static int __init enforcing_setup(char *str)
120{
Eric Parisf5269712008-05-14 11:27:45 -0400121 unsigned long enforcing;
Jingoo Han29707b22014-02-05 15:13:14 +0900122 if (!kstrtoul(str, 0, &enforcing))
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500123 selinux_enforcing_boot = enforcing ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 return 1;
125}
126__setup("enforcing=", enforcing_setup);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500127#else
128#define selinux_enforcing_boot 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129#endif
130
Stephen Smalley6c5a6822019-12-17 09:15:10 -0500131int selinux_enabled_boot __initdata = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132#ifdef CONFIG_SECURITY_SELINUX_BOOTPARAM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133static int __init selinux_enabled_setup(char *str)
134{
Eric Parisf5269712008-05-14 11:27:45 -0400135 unsigned long enabled;
Jingoo Han29707b22014-02-05 15:13:14 +0900136 if (!kstrtoul(str, 0, &enabled))
Stephen Smalley6c5a6822019-12-17 09:15:10 -0500137 selinux_enabled_boot = enabled ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 return 1;
139}
140__setup("selinux=", selinux_enabled_setup);
141#endif
142
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500143static int __init checkreqprot_setup(char *str)
144{
145 unsigned long checkreqprot;
146
Stephen Smalleye9c38f92020-01-08 11:24:47 -0500147 if (!kstrtoul(str, 0, &checkreqprot)) {
Stephen Smalleye9c38f92020-01-08 11:24:47 -0500148 if (checkreqprot)
Paul Moorea7e46762023-03-16 11:43:08 -0400149 pr_err("SELinux: checkreqprot set to 1 via kernel parameter. This is no longer supported.\n");
Stephen Smalleye9c38f92020-01-08 11:24:47 -0500150 }
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{
214 struct task_security_struct *tsec;
215
Paul Moorecdeea452022-01-27 10:45:59 -0500216 tsec = selinux_cred(unrcu_pointer(current->real_cred));
David Howellsd84f4f92008-11-14 10:39:23 +1100217 tsec->osid = tsec->sid = SECINITSID_KERNEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}
219
David Howells275bb412008-11-14 10:39:19 +1100220/*
David Howells88e67f32008-11-14 10:39:21 +1100221 * get the security ID of a set of credentials
222 */
223static inline u32 cred_sid(const struct cred *cred)
224{
225 const struct task_security_struct *tsec;
226
Casey Schaufler0c6cfa62018-09-21 17:17:16 -0700227 tsec = selinux_cred(cred);
David Howells88e67f32008-11-14 10:39:21 +1100228 return tsec->sid;
229}
230
Paolo Abenidd51fcd2023-07-19 13:37:49 +0200231static void __ad_net_init(struct common_audit_data *ad,
232 struct lsm_network_audit *net,
233 int ifindex, struct sock *sk, u16 family)
234{
235 ad->type = LSM_AUDIT_DATA_NET;
236 ad->u.net = net;
237 net->netif = ifindex;
238 net->sk = sk;
239 net->family = family;
240}
241
242static void ad_net_init_from_sk(struct common_audit_data *ad,
243 struct lsm_network_audit *net,
244 struct sock *sk)
245{
246 __ad_net_init(ad, net, 0, sk, 0);
247}
248
249static void ad_net_init_from_iif(struct common_audit_data *ad,
250 struct lsm_network_audit *net,
251 int ifindex, u16 family)
252{
Paul Moore38760432023-07-20 16:26:34 -0400253 __ad_net_init(ad, net, ifindex, NULL, family);
Paolo Abenidd51fcd2023-07-19 13:37:49 +0200254}
255
David Howells88e67f32008-11-14 10:39:21 +1100256/*
David Howells3b11a1d2008-11-14 10:39:26 +1100257 * get the objective security ID of a task
David Howells275bb412008-11-14 10:39:19 +1100258 */
Paul Mooreeb1231f2021-02-18 15:13:40 -0500259static inline u32 task_sid_obj(const struct task_struct *task)
David Howells275bb412008-11-14 10:39:19 +1100260{
David Howells275bb412008-11-14 10:39:19 +1100261 u32 sid;
262
263 rcu_read_lock();
David Howells88e67f32008-11-14 10:39:21 +1100264 sid = cred_sid(__task_cred(task));
David Howells275bb412008-11-14 10:39:19 +1100265 rcu_read_unlock();
266 return sid;
267}
268
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500269static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry);
270
271/*
272 * Try reloading inode security labels that have been marked as invalid. The
273 * @may_sleep parameter indicates when sleeping and thus reloading labels is
Andreas Gruenbacher42059112016-11-10 22:18:27 +0100274 * allowed; when set to false, returns -ECHILD when the label is
Al Viroe9193282018-04-24 21:31:02 -0400275 * invalid. The @dentry parameter should be set to a dentry of the inode.
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500276 */
277static int __inode_security_revalidate(struct inode *inode,
Al Viroe9193282018-04-24 21:31:02 -0400278 struct dentry *dentry,
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500279 bool may_sleep)
280{
Casey Schaufler80788c22018-09-21 17:19:11 -0700281 struct inode_security_struct *isec = selinux_inode(inode);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500282
283 might_sleep_if(may_sleep);
284
Stephen Smalleye67b7982023-03-09 13:30:37 -0500285 if (selinux_initialized() &&
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500286 isec->initialized != LABEL_INITIALIZED) {
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500287 if (!may_sleep)
288 return -ECHILD;
289
290 /*
291 * Try reloading the inode security label. This will fail if
292 * @opt_dentry is NULL and no dentry for this inode can be
293 * found; in that case, continue using the old label.
294 */
Al Viroe9193282018-04-24 21:31:02 -0400295 inode_doinit_with_dentry(inode, dentry);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500296 }
297 return 0;
298}
299
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500300static struct inode_security_struct *inode_security_novalidate(struct inode *inode)
301{
Casey Schaufler80788c22018-09-21 17:19:11 -0700302 return selinux_inode(inode);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500303}
304
305static struct inode_security_struct *inode_security_rcu(struct inode *inode, bool rcu)
306{
307 int error;
308
309 error = __inode_security_revalidate(inode, NULL, !rcu);
310 if (error)
311 return ERR_PTR(error);
Casey Schaufler80788c22018-09-21 17:19:11 -0700312 return selinux_inode(inode);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500313}
314
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500315/*
316 * Get the security label of an inode.
317 */
318static struct inode_security_struct *inode_security(struct inode *inode)
319{
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500320 __inode_security_revalidate(inode, NULL, true);
Casey Schaufler80788c22018-09-21 17:19:11 -0700321 return selinux_inode(inode);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500322}
323
Paul Moore2c971652016-04-19 16:36:28 -0400324static struct inode_security_struct *backing_inode_security_novalidate(struct dentry *dentry)
325{
326 struct inode *inode = d_backing_inode(dentry);
327
Casey Schaufler80788c22018-09-21 17:19:11 -0700328 return selinux_inode(inode);
Paul Moore2c971652016-04-19 16:36:28 -0400329}
330
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500331/*
332 * Get the security label of a dentry's backing inode.
333 */
334static struct inode_security_struct *backing_inode_security(struct dentry *dentry)
335{
336 struct inode *inode = d_backing_inode(dentry);
337
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -0500338 __inode_security_revalidate(inode, dentry, true);
Casey Schaufler80788c22018-09-21 17:19:11 -0700339 return selinux_inode(inode);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500340}
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342static void inode_free_security(struct inode *inode)
343{
Casey Schaufler80788c22018-09-21 17:19:11 -0700344 struct inode_security_struct *isec = selinux_inode(inode);
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700345 struct superblock_security_struct *sbsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Casey Schauflerafb1cbe32018-09-21 17:19:29 -0700347 if (!isec)
348 return;
Casey Schaufler1aea7802021-04-22 17:41:15 +0200349 sbsec = selinux_superblock(inode->i_sb);
Waiman Long9629d042015-07-10 17:19:56 -0400350 /*
351 * As not all inode security structures are in a list, we check for
352 * empty list outside of the lock to make sure that we won't waste
353 * time taking a lock doing nothing.
354 *
355 * The list_del_init() function can be safely called more than once.
356 * It should not be possible for this function to be called with
357 * concurrent list_add(), but for better safety against future changes
358 * in the code, we use list_empty_careful() here.
359 */
360 if (!list_empty_careful(&isec->list)) {
361 spin_lock(&sbsec->isec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 list_del_init(&isec->list);
Waiman Long9629d042015-07-10 17:19:56 -0400363 spin_unlock(&sbsec->isec_lock);
364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
366
Al Virobd323652018-12-13 15:04:59 -0500367struct selinux_mnt_opts {
Scott Mayhewb8b87fd2022-01-31 13:57:36 -0500368 u32 fscontext_sid;
369 u32 context_sid;
370 u32 rootcontext_sid;
371 u32 defcontext_sid;
Al Virobd323652018-12-13 15:04:59 -0500372};
373
Al Viro204cc0c2018-12-13 13:41:47 -0500374static void selinux_free_mnt_opts(void *mnt_opts)
375{
Ondrej Mosnacek70f41692022-02-02 13:55:29 +0100376 kfree(mnt_opts);
Al Viro204cc0c2018-12-13 13:41:47 -0500377}
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379enum {
Eric Paris31e87932007-09-19 17:19:12 -0400380 Opt_error = -1,
David Howells442155c2018-11-01 23:07:24 +0000381 Opt_context = 0,
382 Opt_defcontext = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 Opt_fscontext = 2,
David Howells442155c2018-11-01 23:07:24 +0000384 Opt_rootcontext = 3,
385 Opt_seclabel = 4,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386};
387
Al Viroda3d76a2018-12-17 10:14:16 -0500388#define A(s, has_arg) {#s, sizeof(#s) - 1, Opt_##s, has_arg}
Christian Göttsche4158cb62023-04-20 17:05:03 +0200389static const struct {
Al Viro169d68efb2018-12-14 22:44:50 -0500390 const char *name;
391 int len;
392 int opt;
393 bool has_arg;
394} tokens[] = {
Al Viroda3d76a2018-12-17 10:14:16 -0500395 A(context, true),
396 A(fscontext, true),
397 A(defcontext, true),
398 A(rootcontext, true),
399 A(seclabel, false),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400};
Al Viro169d68efb2018-12-14 22:44:50 -0500401#undef A
402
403static int match_opt_prefix(char *s, int l, char **arg)
404{
405 int i;
406
407 for (i = 0; i < ARRAY_SIZE(tokens); i++) {
408 size_t len = tokens[i].len;
409 if (len > l || memcmp(s, tokens[i].name, len))
410 continue;
411 if (tokens[i].has_arg) {
412 if (len == l || s[len] != '=')
413 continue;
414 *arg = s + len + 1;
415 } else if (len != l)
416 continue;
417 return tokens[i].opt;
418 }
419 return Opt_error;
420}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422#define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n"
423
Eric Parisc312feb2006-07-10 04:43:53 -0700424static int may_context_mount_sb_relabel(u32 sid,
425 struct superblock_security_struct *sbsec,
David Howells275bb412008-11-14 10:39:19 +1100426 const struct cred *cred)
Eric Parisc312feb2006-07-10 04:43:53 -0700427{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -0700428 const struct task_security_struct *tsec = selinux_cred(cred);
Eric Parisc312feb2006-07-10 04:43:53 -0700429 int rc;
430
Stephen Smalleye67b7982023-03-09 13:30:37 -0500431 rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
Eric Parisc312feb2006-07-10 04:43:53 -0700432 FILESYSTEM__RELABELFROM, NULL);
433 if (rc)
434 return rc;
435
Stephen Smalleye67b7982023-03-09 13:30:37 -0500436 rc = avc_has_perm(tsec->sid, sid, SECCLASS_FILESYSTEM,
Eric Parisc312feb2006-07-10 04:43:53 -0700437 FILESYSTEM__RELABELTO, NULL);
438 return rc;
439}
440
Eric Paris08089252006-07-10 04:43:55 -0700441static int may_context_mount_inode_relabel(u32 sid,
442 struct superblock_security_struct *sbsec,
David Howells275bb412008-11-14 10:39:19 +1100443 const struct cred *cred)
Eric Paris08089252006-07-10 04:43:55 -0700444{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -0700445 const struct task_security_struct *tsec = selinux_cred(cred);
Eric Paris08089252006-07-10 04:43:55 -0700446 int rc;
Stephen Smalleye67b7982023-03-09 13:30:37 -0500447 rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM,
Eric Paris08089252006-07-10 04:43:55 -0700448 FILESYSTEM__RELABELFROM, NULL);
449 if (rc)
450 return rc;
451
Stephen Smalleye67b7982023-03-09 13:30:37 -0500452 rc = avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM,
Eric Paris08089252006-07-10 04:43:55 -0700453 FILESYSTEM__ASSOCIATE, NULL);
454 return rc;
455}
456
Ondrej Mosnaceka83d6dd2018-12-21 21:18:52 +0100457static int selinux_is_genfs_special_handling(struct super_block *sb)
Eric Parisb43e7252012-10-10 14:27:35 -0400458{
Ondrej Mosnaceka83d6dd2018-12-21 21:18:52 +0100459 /* Special handling. Genfs but also in-core setxattr handler */
460 return !strcmp(sb->s_type->name, "sysfs") ||
Mark Salyzynd5f3a5f2015-02-04 11:34:30 -0500461 !strcmp(sb->s_type->name, "pstore") ||
462 !strcmp(sb->s_type->name, "debugfs") ||
Yongqin Liua2c7c6fb2017-01-09 10:07:30 -0500463 !strcmp(sb->s_type->name, "tracefs") ||
Stephen Smalley2651225b2017-02-28 10:35:56 -0500464 !strcmp(sb->s_type->name, "rootfs") ||
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500465 (selinux_policycap_cgroupseclabel() &&
Stephen Smalley2651225b2017-02-28 10:35:56 -0500466 (!strcmp(sb->s_type->name, "cgroup") ||
467 !strcmp(sb->s_type->name, "cgroup2")));
Eric Parisb43e7252012-10-10 14:27:35 -0400468}
469
Ondrej Mosnaceka83d6dd2018-12-21 21:18:52 +0100470static int selinux_is_sblabel_mnt(struct super_block *sb)
471{
Casey Schaufler1aea7802021-04-22 17:41:15 +0200472 struct superblock_security_struct *sbsec = selinux_superblock(sb);
Ondrej Mosnaceka83d6dd2018-12-21 21:18:52 +0100473
474 /*
475 * IMPORTANT: Double-check logic in this function when adding a new
476 * SECURITY_FS_USE_* definition!
477 */
478 BUILD_BUG_ON(SECURITY_FS_USE_MAX != 7);
479
480 switch (sbsec->behavior) {
481 case SECURITY_FS_USE_XATTR:
482 case SECURITY_FS_USE_TRANS:
483 case SECURITY_FS_USE_TASK:
484 case SECURITY_FS_USE_NATIVE:
485 return 1;
486
487 case SECURITY_FS_USE_GENFS:
488 return selinux_is_genfs_special_handling(sb);
489
490 /* Never allow relabeling on context mounts */
491 case SECURITY_FS_USE_MNTPOINT:
492 case SECURITY_FS_USE_NONE:
493 default:
494 return 0;
495 }
496}
497
Ondrej Mosnacek08abe462021-01-13 13:38:02 +0100498static int sb_check_xattr_support(struct super_block *sb)
499{
GONG, Ruiqi0266c252022-01-25 15:11:33 +0800500 struct superblock_security_struct *sbsec = selinux_superblock(sb);
Ondrej Mosnacek08abe462021-01-13 13:38:02 +0100501 struct dentry *root = sb->s_root;
502 struct inode *root_inode = d_backing_inode(root);
503 u32 sid;
504 int rc;
505
506 /*
507 * Make sure that the xattr handler exists and that no
508 * error other than -ENODATA is returned by getxattr on
509 * the root directory. -ENODATA is ok, as this may be
510 * the first boot of the SELinux kernel before we have
511 * assigned xattr values to the filesystem.
512 */
513 if (!(root_inode->i_opflags & IOP_XATTR)) {
514 pr_warn("SELinux: (dev %s, type %s) has no xattr support\n",
515 sb->s_id, sb->s_type->name);
516 goto fallback;
517 }
518
519 rc = __vfs_getxattr(root, root_inode, XATTR_NAME_SELINUX, NULL, 0);
520 if (rc < 0 && rc != -ENODATA) {
521 if (rc == -EOPNOTSUPP) {
522 pr_warn("SELinux: (dev %s, type %s) has no security xattr handler\n",
523 sb->s_id, sb->s_type->name);
524 goto fallback;
525 } else {
526 pr_warn("SELinux: (dev %s, type %s) getxattr errno %d\n",
527 sb->s_id, sb->s_type->name, -rc);
528 return rc;
529 }
530 }
531 return 0;
532
533fallback:
534 /* No xattr support - try to fallback to genfs if possible. */
Stephen Smalleye67b7982023-03-09 13:30:37 -0500535 rc = security_genfs_sid(sb->s_type->name, "/",
Ondrej Mosnacek08abe462021-01-13 13:38:02 +0100536 SECCLASS_DIR, &sid);
537 if (rc)
538 return -EOPNOTSUPP;
539
540 pr_warn("SELinux: (dev %s, type %s) falling back to genfs\n",
541 sb->s_id, sb->s_type->name);
542 sbsec->behavior = SECURITY_FS_USE_GENFS;
543 sbsec->sid = sid;
544 return 0;
545}
546
Eric Parisc9180a52007-11-30 13:00:35 -0500547static int sb_finish_set_opts(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
Casey Schaufler1aea7802021-04-22 17:41:15 +0200549 struct superblock_security_struct *sbsec = selinux_superblock(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 struct dentry *root = sb->s_root;
David Howellsc6f493d2015-03-17 22:26:22 +0000551 struct inode *root_inode = d_backing_inode(root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 int rc = 0;
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
Ondrej Mosnacek08abe462021-01-13 13:38:02 +0100555 rc = sb_check_xattr_support(sb);
556 if (rc)
557 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
559
Eric Pariseadcabc2012-08-24 15:59:14 -0400560 sbsec->flags |= SE_SBINITIALIZED;
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400561
562 /*
563 * Explicitly set or clear SBLABEL_MNT. It's not sufficient to simply
564 * leave the flag untouched because sb_clone_mnt_opts might be handing
565 * us a superblock that needs the flag to be cleared.
566 */
Eric Parisb43e7252012-10-10 14:27:35 -0400567 if (selinux_is_sblabel_mnt(sb))
Eric Paris12f348b2012-10-09 10:56:25 -0400568 sbsec->flags |= SBLABEL_MNT;
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400569 else
570 sbsec->flags &= ~SBLABEL_MNT;
David P. Quigleyddd29ec2009-09-09 14:25:37 -0400571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 /* Initialize the root inode. */
Eric Parisc9180a52007-11-30 13:00:35 -0500573 rc = inode_doinit_with_dentry(root_inode, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 /* Initialize any other inodes associated with the superblock, e.g.
576 inodes created prior to initial policy load or inodes created
577 during get_sb by a pseudo filesystem that directly
578 populates itself. */
579 spin_lock(&sbsec->isec_lock);
Al Viro8d641242018-12-10 15:34:12 -0500580 while (!list_empty(&sbsec->isec_head)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 struct inode_security_struct *isec =
Al Viro8d641242018-12-10 15:34:12 -0500582 list_first_entry(&sbsec->isec_head,
Eric Parisc9180a52007-11-30 13:00:35 -0500583 struct inode_security_struct, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 struct inode *inode = isec->inode;
Stephen Smalley923190d32014-10-06 16:32:52 -0400585 list_del_init(&isec->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 spin_unlock(&sbsec->isec_lock);
587 inode = igrab(inode);
588 if (inode) {
Eric Parisc9180a52007-11-30 13:00:35 -0500589 if (!IS_PRIVATE(inode))
Paul Moorecb89e242020-01-10 16:32:10 -0500590 inode_doinit_with_dentry(inode, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 iput(inode);
592 }
593 spin_lock(&sbsec->isec_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
595 spin_unlock(&sbsec->isec_lock);
Eric Parisc9180a52007-11-30 13:00:35 -0500596 return rc;
597}
598
Eric Parisc9180a52007-11-30 13:00:35 -0500599static int bad_option(struct superblock_security_struct *sbsec, char flag,
600 u32 old_sid, u32 new_sid)
601{
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500602 char mnt_flags = sbsec->flags & SE_MNTMASK;
603
Eric Parisc9180a52007-11-30 13:00:35 -0500604 /* check if the old mount command had the same options */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500605 if (sbsec->flags & SE_SBINITIALIZED)
Eric Parisc9180a52007-11-30 13:00:35 -0500606 if (!(sbsec->flags & flag) ||
607 (old_sid != new_sid))
608 return 1;
609
610 /* check if we were passed the same options twice,
611 * aka someone passed context=a,context=b
612 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500613 if (!(sbsec->flags & SE_SBINITIALIZED))
614 if (mnt_flags & flag)
Eric Parisc9180a52007-11-30 13:00:35 -0500615 return 1;
616 return 0;
617}
Eric Parise0007522008-03-05 10:31:54 -0500618
Eric Parisc9180a52007-11-30 13:00:35 -0500619/*
620 * Allow filesystems with binary mount data to explicitly set mount point
621 * labeling information.
622 */
Eric Parise0007522008-03-05 10:31:54 -0500623static int selinux_set_mnt_opts(struct super_block *sb,
Al Viro204cc0c2018-12-13 13:41:47 -0500624 void *mnt_opts,
David Quigley649f6e72013-05-22 12:50:36 -0400625 unsigned long kern_flags,
626 unsigned long *set_kern_flags)
Eric Parisc9180a52007-11-30 13:00:35 -0500627{
David Howells275bb412008-11-14 10:39:19 +1100628 const struct cred *cred = current_cred();
Casey Schaufler1aea7802021-04-22 17:41:15 +0200629 struct superblock_security_struct *sbsec = selinux_superblock(sb);
Ondrej Mosnacekb159e862020-11-04 13:01:10 +0100630 struct dentry *root = sb->s_root;
Al Virobd323652018-12-13 15:04:59 -0500631 struct selinux_mnt_opts *opts = mnt_opts;
Paul Moore2c971652016-04-19 16:36:28 -0400632 struct inode_security_struct *root_isec;
Eric Parisc9180a52007-11-30 13:00:35 -0500633 u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
634 u32 defcontext_sid = 0;
Al Virobd323652018-12-13 15:04:59 -0500635 int rc = 0;
Eric Parisc9180a52007-11-30 13:00:35 -0500636
Ondrej Mosnacekcec5fe72023-05-29 16:05:27 +0200637 /*
638 * Specifying internal flags without providing a place to
639 * place the results is not allowed
640 */
641 if (kern_flags && !set_kern_flags)
642 return -EINVAL;
643
Eric Parisc9180a52007-11-30 13:00:35 -0500644 mutex_lock(&sbsec->lock);
645
Stephen Smalleye67b7982023-03-09 13:30:37 -0500646 if (!selinux_initialized()) {
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. */
Ondrej Mosnacekcec5fe72023-05-29 16:05:27 +0200651 if (kern_flags & SECURITY_LSM_NATIVE_LABELS) {
652 sbsec->flags |= SE_SBNATIVE;
653 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
654 }
Eric Parisc9180a52007-11-30 13:00:35 -0500655 goto out;
656 }
657 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +0200658 pr_warn("SELinux: Unable to set superblock options "
Eric Paris744ba352008-04-17 11:52:44 -0400659 "before the security server is initialized\n");
Eric Parisc9180a52007-11-30 13:00:35 -0500660 goto out;
661 }
662
663 /*
Eric Parise0007522008-03-05 10:31:54 -0500664 * Binary mount data FS will come through this function twice. Once
665 * from an explicit call and once from the generic calls from the vfs.
666 * Since the generic VFS calls will not contain any security mount data
667 * we need to skip the double mount verification.
668 *
669 * This does open a hole in which we will not notice if the first
Jonas Lindner9691e4f2022-06-09 00:36:16 +0200670 * mount using this sb set explicit options and a second mount using
Eric Parise0007522008-03-05 10:31:54 -0500671 * this sb does not set any security options. (The first options
672 * will be used for both mounts)
673 */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500674 if ((sbsec->flags & SE_SBINITIALIZED) && (sb->s_type->fs_flags & FS_BINARY_MOUNTDATA)
Al Virobd323652018-12-13 15:04:59 -0500675 && !opts)
Eric Parisf5269712008-05-14 11:27:45 -0400676 goto out;
Eric Parise0007522008-03-05 10:31:54 -0500677
Paul Moore2c971652016-04-19 16:36:28 -0400678 root_isec = backing_inode_security_novalidate(root);
679
Eric Parise0007522008-03-05 10:31:54 -0500680 /*
Eric Parisc9180a52007-11-30 13:00:35 -0500681 * parse the mount options, check if they are valid sids.
682 * also check if someone is trying to mount the same sb more
683 * than once with different security options.
684 */
Al Virobd323652018-12-13 15:04:59 -0500685 if (opts) {
Ondrej Mosnacek70f41692022-02-02 13:55:29 +0100686 if (opts->fscontext_sid) {
687 fscontext_sid = opts->fscontext_sid;
Eric Parisc9180a52007-11-30 13:00:35 -0500688 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
689 fscontext_sid))
690 goto out_double_mount;
Eric Parisc9180a52007-11-30 13:00:35 -0500691 sbsec->flags |= FSCONTEXT_MNT;
Al Virobd323652018-12-13 15:04:59 -0500692 }
Ondrej Mosnacek70f41692022-02-02 13:55:29 +0100693 if (opts->context_sid) {
694 context_sid = opts->context_sid;
Eric Parisc9180a52007-11-30 13:00:35 -0500695 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
696 context_sid))
697 goto out_double_mount;
Eric Parisc9180a52007-11-30 13:00:35 -0500698 sbsec->flags |= CONTEXT_MNT;
Al Virobd323652018-12-13 15:04:59 -0500699 }
Ondrej Mosnacek70f41692022-02-02 13:55:29 +0100700 if (opts->rootcontext_sid) {
701 rootcontext_sid = opts->rootcontext_sid;
Eric Parisc9180a52007-11-30 13:00:35 -0500702 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
703 rootcontext_sid))
704 goto out_double_mount;
Eric Parisc9180a52007-11-30 13:00:35 -0500705 sbsec->flags |= ROOTCONTEXT_MNT;
Al Virobd323652018-12-13 15:04:59 -0500706 }
Ondrej Mosnacek70f41692022-02-02 13:55:29 +0100707 if (opts->defcontext_sid) {
708 defcontext_sid = opts->defcontext_sid;
Eric Parisc9180a52007-11-30 13:00:35 -0500709 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
710 defcontext_sid))
711 goto out_double_mount;
Eric Parisc9180a52007-11-30 13:00:35 -0500712 sbsec->flags |= DEFCONTEXT_MNT;
Eric Parisc9180a52007-11-30 13:00:35 -0500713 }
714 }
715
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500716 if (sbsec->flags & SE_SBINITIALIZED) {
Eric Parisc9180a52007-11-30 13:00:35 -0500717 /* previously mounted with options, but not on this attempt? */
Al Virobd323652018-12-13 15:04:59 -0500718 if ((sbsec->flags & SE_MNTMASK) && !opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500719 goto out_double_mount;
720 rc = 0;
721 goto out;
722 }
723
James Morris089be432008-07-15 18:32:49 +1000724 if (strcmp(sb->s_type->name, "proc") == 0)
Stephen Smalley134509d2015-06-04 16:22:17 -0400725 sbsec->flags |= SE_SBPROC | SE_SBGENFS;
726
Stephen Smalley8e014722015-06-04 16:22:17 -0400727 if (!strcmp(sb->s_type->name, "debugfs") ||
Jeff Vander Stoep6a391182017-06-20 09:35:33 -0700728 !strcmp(sb->s_type->name, "tracefs") ||
Hridya Valsarajua20456a2020-02-01 17:46:23 -0800729 !strcmp(sb->s_type->name, "binder") ||
Connor O'Brien4ca54d32020-02-07 10:01:49 -0800730 !strcmp(sb->s_type->name, "bpf") ||
Christian Göttsche8a764ef2021-09-28 17:39:31 +0200731 !strcmp(sb->s_type->name, "pstore") ||
732 !strcmp(sb->s_type->name, "securityfs"))
Ondrej Mosnacekb7540262019-02-22 15:57:14 +0100733 sbsec->flags |= SE_SBGENFS;
734
735 if (!strcmp(sb->s_type->name, "sysfs") ||
Antonio Murdaca901ef842017-02-09 17:02:42 +0100736 !strcmp(sb->s_type->name, "cgroup") ||
737 !strcmp(sb->s_type->name, "cgroup2"))
Ondrej Mosnacekb7540262019-02-22 15:57:14 +0100738 sbsec->flags |= SE_SBGENFS | SE_SBGENFS_XATTR;
Eric Parisc9180a52007-11-30 13:00:35 -0500739
David Quigleyeb9ae682013-05-22 12:50:37 -0400740 if (!sbsec->behavior) {
741 /*
742 * Determine the labeling behavior to use for this
743 * filesystem type.
744 */
Stephen Smalleye67b7982023-03-09 13:30:37 -0500745 rc = security_fs_use(sb);
David Quigleyeb9ae682013-05-22 12:50:37 -0400746 if (rc) {
peter enderborgc103a912018-06-12 10:09:03 +0200747 pr_warn("%s: security_fs_use(%s) returned %d\n",
David Quigleyeb9ae682013-05-22 12:50:37 -0400748 __func__, sb->s_type->name, rc);
749 goto out;
750 }
Eric Parisc9180a52007-11-30 13:00:35 -0500751 }
Seth Forsheeaad82892016-04-26 14:36:20 -0500752
753 /*
Stephen Smalley01593d32017-01-09 10:07:31 -0500754 * If this is a user namespace mount and the filesystem type is not
755 * explicitly whitelisted, then no contexts are allowed on the command
756 * line and security labels must be ignored.
Seth Forsheeaad82892016-04-26 14:36:20 -0500757 */
Stephen Smalley01593d32017-01-09 10:07:31 -0500758 if (sb->s_user_ns != &init_user_ns &&
759 strcmp(sb->s_type->name, "tmpfs") &&
760 strcmp(sb->s_type->name, "ramfs") &&
Vivek Goyal7fa2e792021-02-11 13:03:03 -0500761 strcmp(sb->s_type->name, "devpts") &&
762 strcmp(sb->s_type->name, "overlay")) {
Seth Forsheeaad82892016-04-26 14:36:20 -0500763 if (context_sid || fscontext_sid || rootcontext_sid ||
764 defcontext_sid) {
765 rc = -EACCES;
766 goto out;
767 }
768 if (sbsec->behavior == SECURITY_FS_USE_XATTR) {
769 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
Stephen Smalleye67b7982023-03-09 13:30:37 -0500770 rc = security_transition_sid(current_sid(),
Stephen Smalleyaa8e7122018-03-01 18:48:02 -0500771 current_sid(),
Seth Forsheeaad82892016-04-26 14:36:20 -0500772 SECCLASS_FILE, NULL,
773 &sbsec->mntpoint_sid);
774 if (rc)
775 goto out;
776 }
777 goto out_set_opts;
778 }
779
Eric Parisc9180a52007-11-30 13:00:35 -0500780 /* sets the context of the superblock for the fs being mounted. */
781 if (fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100782 rc = may_context_mount_sb_relabel(fscontext_sid, sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500783 if (rc)
784 goto out;
785
786 sbsec->sid = fscontext_sid;
787 }
788
789 /*
790 * Switch to using mount point labeling behavior.
791 * sets the label used on all file below the mountpoint, and will set
792 * the superblock context if not already set.
793 */
Ondrej Mosnacekcec5fe72023-05-29 16:05:27 +0200794 if (sbsec->flags & SE_SBNATIVE) {
795 /*
796 * This means we are initializing a superblock that has been
797 * mounted before the SELinux was initialized and the
798 * filesystem requested native labeling. We had already
799 * returned SECURITY_LSM_NATIVE_LABELS in *set_kern_flags
800 * in the original mount attempt, so now we just need to set
801 * the SECURITY_FS_USE_NATIVE behavior.
802 */
803 sbsec->behavior = SECURITY_FS_USE_NATIVE;
804 } else if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !context_sid) {
David Quigleyeb9ae682013-05-22 12:50:37 -0400805 sbsec->behavior = SECURITY_FS_USE_NATIVE;
806 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
807 }
808
Eric Parisc9180a52007-11-30 13:00:35 -0500809 if (context_sid) {
810 if (!fscontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100811 rc = may_context_mount_sb_relabel(context_sid, sbsec,
812 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500813 if (rc)
814 goto out;
815 sbsec->sid = context_sid;
816 } else {
David Howells275bb412008-11-14 10:39:19 +1100817 rc = may_context_mount_inode_relabel(context_sid, sbsec,
818 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500819 if (rc)
820 goto out;
821 }
822 if (!rootcontext_sid)
823 rootcontext_sid = context_sid;
824
825 sbsec->mntpoint_sid = context_sid;
826 sbsec->behavior = SECURITY_FS_USE_MNTPOINT;
827 }
828
829 if (rootcontext_sid) {
David Howells275bb412008-11-14 10:39:19 +1100830 rc = may_context_mount_inode_relabel(rootcontext_sid, sbsec,
831 cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500832 if (rc)
833 goto out;
834
835 root_isec->sid = rootcontext_sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -0500836 root_isec->initialized = LABEL_INITIALIZED;
Eric Parisc9180a52007-11-30 13:00:35 -0500837 }
838
839 if (defcontext_sid) {
David Quigleyeb9ae682013-05-22 12:50:37 -0400840 if (sbsec->behavior != SECURITY_FS_USE_XATTR &&
841 sbsec->behavior != SECURITY_FS_USE_NATIVE) {
Eric Parisc9180a52007-11-30 13:00:35 -0500842 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +0200843 pr_warn("SELinux: defcontext option is "
Eric Parisc9180a52007-11-30 13:00:35 -0500844 "invalid for this filesystem type\n");
845 goto out;
846 }
847
848 if (defcontext_sid != sbsec->def_sid) {
849 rc = may_context_mount_inode_relabel(defcontext_sid,
David Howells275bb412008-11-14 10:39:19 +1100850 sbsec, cred);
Eric Parisc9180a52007-11-30 13:00:35 -0500851 if (rc)
852 goto out;
853 }
854
855 sbsec->def_sid = defcontext_sid;
856 }
857
Seth Forsheeaad82892016-04-26 14:36:20 -0500858out_set_opts:
Eric Parisc9180a52007-11-30 13:00:35 -0500859 rc = sb_finish_set_opts(sb);
860out:
Eric Parisbc7e9822006-09-25 23:32:02 -0700861 mutex_unlock(&sbsec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 return rc;
Eric Parisc9180a52007-11-30 13:00:35 -0500863out_double_mount:
864 rc = -EINVAL;
peter enderborgc103a912018-06-12 10:09:03 +0200865 pr_warn("SELinux: mount invalid. Same superblock, different "
Al Virobd323652018-12-13 15:04:59 -0500866 "security settings for (dev %s, type %s)\n", sb->s_id,
867 sb->s_type->name);
Eric Parisc9180a52007-11-30 13:00:35 -0500868 goto out;
869}
870
Jeff Layton094f7b62013-04-01 08:14:24 -0400871static int selinux_cmp_sb_context(const struct super_block *oldsb,
872 const struct super_block *newsb)
873{
Casey Schaufler1aea7802021-04-22 17:41:15 +0200874 struct superblock_security_struct *old = selinux_superblock(oldsb);
875 struct superblock_security_struct *new = selinux_superblock(newsb);
Jeff Layton094f7b62013-04-01 08:14:24 -0400876 char oldflags = old->flags & SE_MNTMASK;
877 char newflags = new->flags & SE_MNTMASK;
878
879 if (oldflags != newflags)
880 goto mismatch;
881 if ((oldflags & FSCONTEXT_MNT) && old->sid != new->sid)
882 goto mismatch;
883 if ((oldflags & CONTEXT_MNT) && old->mntpoint_sid != new->mntpoint_sid)
884 goto mismatch;
885 if ((oldflags & DEFCONTEXT_MNT) && old->def_sid != new->def_sid)
886 goto mismatch;
887 if (oldflags & ROOTCONTEXT_MNT) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500888 struct inode_security_struct *oldroot = backing_inode_security(oldsb->s_root);
889 struct inode_security_struct *newroot = backing_inode_security(newsb->s_root);
Jeff Layton094f7b62013-04-01 08:14:24 -0400890 if (oldroot->sid != newroot->sid)
891 goto mismatch;
892 }
893 return 0;
894mismatch:
peter enderborgc103a912018-06-12 10:09:03 +0200895 pr_warn("SELinux: mount invalid. Same superblock, "
Jeff Layton094f7b62013-04-01 08:14:24 -0400896 "different security settings for (dev %s, "
897 "type %s)\n", newsb->s_id, newsb->s_type->name);
898 return -EBUSY;
899}
900
901static int selinux_sb_clone_mnt_opts(const struct super_block *oldsb,
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400902 struct super_block *newsb,
903 unsigned long kern_flags,
904 unsigned long *set_kern_flags)
Eric Parisc9180a52007-11-30 13:00:35 -0500905{
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400906 int rc = 0;
Casey Schaufler1aea7802021-04-22 17:41:15 +0200907 const struct superblock_security_struct *oldsbsec =
908 selinux_superblock(oldsb);
909 struct superblock_security_struct *newsbsec = selinux_superblock(newsb);
Eric Parisc9180a52007-11-30 13:00:35 -0500910
911 int set_fscontext = (oldsbsec->flags & FSCONTEXT_MNT);
912 int set_context = (oldsbsec->flags & CONTEXT_MNT);
913 int set_rootcontext = (oldsbsec->flags & ROOTCONTEXT_MNT);
914
Eric Paris0f5e6422008-04-21 16:24:11 -0400915 /*
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400916 * Specifying internal flags without providing a place to
917 * place the results is not allowed.
918 */
919 if (kern_flags && !set_kern_flags)
920 return -EINVAL;
921
Ondrej Mosnacekcec5fe72023-05-29 16:05:27 +0200922 mutex_lock(&newsbsec->lock);
923
924 /*
925 * if the parent was able to be mounted it clearly had no special lsm
926 * mount options. thus we can safely deal with this superblock later
927 */
928 if (!selinux_initialized()) {
929 if (kern_flags & SECURITY_LSM_NATIVE_LABELS) {
930 newsbsec->flags |= SE_SBNATIVE;
931 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
932 }
933 goto out;
934 }
935
Eric Parisc9180a52007-11-30 13:00:35 -0500936 /* how can we clone if the old one wasn't set up?? */
David P. Quigley0d90a7e2009-01-16 09:22:02 -0500937 BUG_ON(!(oldsbsec->flags & SE_SBINITIALIZED));
Eric Parisc9180a52007-11-30 13:00:35 -0500938
Jeff Layton094f7b62013-04-01 08:14:24 -0400939 /* if fs is reusing a sb, make sure that the contexts match */
J. Bruce Fields3815a242019-03-05 16:17:58 -0500940 if (newsbsec->flags & SE_SBINITIALIZED) {
Ondrej Mosnacekcec5fe72023-05-29 16:05:27 +0200941 mutex_unlock(&newsbsec->lock);
J. Bruce Fields3815a242019-03-05 16:17:58 -0500942 if ((kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context)
943 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
Jeff Layton094f7b62013-04-01 08:14:24 -0400944 return selinux_cmp_sb_context(oldsb, newsb);
J. Bruce Fields3815a242019-03-05 16:17:58 -0500945 }
Eric Paris5a552612008-04-09 14:08:35 -0400946
Eric Parisc9180a52007-11-30 13:00:35 -0500947 newsbsec->flags = oldsbsec->flags;
948
949 newsbsec->sid = oldsbsec->sid;
950 newsbsec->def_sid = oldsbsec->def_sid;
951 newsbsec->behavior = oldsbsec->behavior;
952
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400953 if (newsbsec->behavior == SECURITY_FS_USE_NATIVE &&
954 !(kern_flags & SECURITY_LSM_NATIVE_LABELS) && !set_context) {
Stephen Smalleye67b7982023-03-09 13:30:37 -0500955 rc = security_fs_use(newsb);
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400956 if (rc)
957 goto out;
958 }
959
960 if (kern_flags & SECURITY_LSM_NATIVE_LABELS && !set_context) {
961 newsbsec->behavior = SECURITY_FS_USE_NATIVE;
962 *set_kern_flags |= SECURITY_LSM_NATIVE_LABELS;
963 }
964
Eric Parisc9180a52007-11-30 13:00:35 -0500965 if (set_context) {
966 u32 sid = oldsbsec->mntpoint_sid;
967
968 if (!set_fscontext)
969 newsbsec->sid = sid;
970 if (!set_rootcontext) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500971 struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
Eric Parisc9180a52007-11-30 13:00:35 -0500972 newisec->sid = sid;
973 }
974 newsbsec->mntpoint_sid = sid;
975 }
976 if (set_rootcontext) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -0500977 const struct inode_security_struct *oldisec = backing_inode_security(oldsb->s_root);
978 struct inode_security_struct *newisec = backing_inode_security(newsb->s_root);
Eric Parisc9180a52007-11-30 13:00:35 -0500979
980 newisec->sid = oldisec->sid;
981 }
982
983 sb_finish_set_opts(newsb);
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400984out:
Eric Parisc9180a52007-11-30 13:00:35 -0500985 mutex_unlock(&newsbsec->lock);
Scott Mayhew0b4d3452017-06-05 11:45:04 -0400986 return rc;
Eric Parisc9180a52007-11-30 13:00:35 -0500987}
988
Xiu Jianfengef54ccb2022-06-17 17:44:12 +0800989/*
Christian Göttsche3d9047a2023-04-20 17:04:59 +0200990 * NOTE: the caller is responsible for freeing the memory even if on error.
Xiu Jianfengef54ccb2022-06-17 17:44:12 +0800991 */
Al Viroba641862018-12-14 20:28:15 -0500992static int selinux_add_opt(int token, const char *s, void **mnt_opts)
Eric Parisc9180a52007-11-30 13:00:35 -0500993{
Al Viroba641862018-12-14 20:28:15 -0500994 struct selinux_mnt_opts *opts = *mnt_opts;
Ondrej Mosnacek70f41692022-02-02 13:55:29 +0100995 u32 *dst_sid;
996 int rc;
Eric Parisc9180a52007-11-30 13:00:35 -0500997
Paul Moore6cd9d4b2021-12-21 15:01:29 -0500998 if (token == Opt_seclabel)
999 /* eaten and completely ignored */
Al Viro169d68efb2018-12-14 22:44:50 -05001000 return 0;
Bernard Zhao2e08df32021-12-10 04:03:58 -08001001 if (!s)
Xiu Jianfengef54ccb2022-06-17 17:44:12 +08001002 return -EINVAL;
Eric Parisc9180a52007-11-30 13:00:35 -05001003
Stephen Smalleye67b7982023-03-09 13:30:37 -05001004 if (!selinux_initialized()) {
Ondrej Mosnacek70f41692022-02-02 13:55:29 +01001005 pr_warn("SELinux: Unable to set superblock options before the security server is initialized\n");
1006 return -EINVAL;
1007 }
1008
Al Viroba641862018-12-14 20:28:15 -05001009 if (!opts) {
Paul Moore6cd9d4b2021-12-21 15:01:29 -05001010 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
Al Viroba641862018-12-14 20:28:15 -05001011 if (!opts)
1012 return -ENOMEM;
1013 *mnt_opts = opts;
1014 }
Bernard Zhao2e08df32021-12-10 04:03:58 -08001015
Al Viroba641862018-12-14 20:28:15 -05001016 switch (token) {
1017 case Opt_context:
Ondrej Mosnacek70f41692022-02-02 13:55:29 +01001018 if (opts->context_sid || opts->defcontext_sid)
Paul Moore6cd9d4b2021-12-21 15:01:29 -05001019 goto err;
Ondrej Mosnacek70f41692022-02-02 13:55:29 +01001020 dst_sid = &opts->context_sid;
Al Viroba641862018-12-14 20:28:15 -05001021 break;
1022 case Opt_fscontext:
Ondrej Mosnacek70f41692022-02-02 13:55:29 +01001023 if (opts->fscontext_sid)
Paul Moore6cd9d4b2021-12-21 15:01:29 -05001024 goto err;
Ondrej Mosnacek70f41692022-02-02 13:55:29 +01001025 dst_sid = &opts->fscontext_sid;
Al Viroba641862018-12-14 20:28:15 -05001026 break;
1027 case Opt_rootcontext:
Ondrej Mosnacek70f41692022-02-02 13:55:29 +01001028 if (opts->rootcontext_sid)
Paul Moore6cd9d4b2021-12-21 15:01:29 -05001029 goto err;
Ondrej Mosnacek70f41692022-02-02 13:55:29 +01001030 dst_sid = &opts->rootcontext_sid;
Al Viroba641862018-12-14 20:28:15 -05001031 break;
1032 case Opt_defcontext:
Ondrej Mosnacek70f41692022-02-02 13:55:29 +01001033 if (opts->context_sid || opts->defcontext_sid)
Paul Moore6cd9d4b2021-12-21 15:01:29 -05001034 goto err;
Ondrej Mosnacek70f41692022-02-02 13:55:29 +01001035 dst_sid = &opts->defcontext_sid;
Al Viroba641862018-12-14 20:28:15 -05001036 break;
Ondrej Mosnacek70f41692022-02-02 13:55:29 +01001037 default:
1038 WARN_ON(1);
1039 return -EINVAL;
Al Viroba641862018-12-14 20:28:15 -05001040 }
Stephen Smalleye67b7982023-03-09 13:30:37 -05001041 rc = security_context_str_to_sid(s, dst_sid, GFP_KERNEL);
Ondrej Mosnacek70f41692022-02-02 13:55:29 +01001042 if (rc)
1043 pr_warn("SELinux: security_context_str_to_sid (%s) failed with errno=%d\n",
1044 s, rc);
1045 return rc;
Eric Parisc9180a52007-11-30 13:00:35 -05001046
Paul Moore6cd9d4b2021-12-21 15:01:29 -05001047err:
Eric Parisc9180a52007-11-30 13:00:35 -05001048 pr_warn(SEL_MOUNT_FAIL_MSG);
1049 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Al Viroe3489f82018-12-13 00:24:36 -05001052static int show_sid(struct seq_file *m, u32 sid)
Eric Paris2069f452008-07-04 09:47:13 +10001053{
Al Viroe3489f82018-12-13 00:24:36 -05001054 char *context = NULL;
1055 u32 len;
1056 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Stephen Smalleye67b7982023-03-09 13:30:37 -05001058 rc = security_sid_to_context(sid, &context, &len);
Al Viroe3489f82018-12-13 00:24:36 -05001059 if (!rc) {
Christian Göttsche4d3d0ed2022-02-17 15:21:28 +01001060 bool has_comma = strchr(context, ',');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
David Howells442155c2018-11-01 23:07:24 +00001062 seq_putc(m, '=');
Eric Paris2069f452008-07-04 09:47:13 +10001063 if (has_comma)
1064 seq_putc(m, '\"');
Al Viroe3489f82018-12-13 00:24:36 -05001065 seq_escape(m, context, "\"\n\\");
Eric Paris2069f452008-07-04 09:47:13 +10001066 if (has_comma)
1067 seq_putc(m, '\"');
1068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 kfree(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 return rc;
1071}
Eric Paris2069f452008-07-04 09:47:13 +10001072
1073static int selinux_sb_show_options(struct seq_file *m, struct super_block *sb)
1074{
Casey Schaufler1aea7802021-04-22 17:41:15 +02001075 struct superblock_security_struct *sbsec = selinux_superblock(sb);
Eric Paris2069f452008-07-04 09:47:13 +10001076 int rc;
1077
Al Viroe3489f82018-12-13 00:24:36 -05001078 if (!(sbsec->flags & SE_SBINITIALIZED))
1079 return 0;
1080
Stephen Smalleye67b7982023-03-09 13:30:37 -05001081 if (!selinux_initialized())
Al Viroe3489f82018-12-13 00:24:36 -05001082 return 0;
1083
1084 if (sbsec->flags & FSCONTEXT_MNT) {
1085 seq_putc(m, ',');
1086 seq_puts(m, FSCONTEXT_STR);
1087 rc = show_sid(m, sbsec->sid);
1088 if (rc)
1089 return rc;
Eric Paris383795c2008-07-29 17:07:26 -04001090 }
Al Viroe3489f82018-12-13 00:24:36 -05001091 if (sbsec->flags & CONTEXT_MNT) {
1092 seq_putc(m, ',');
1093 seq_puts(m, CONTEXT_STR);
1094 rc = show_sid(m, sbsec->mntpoint_sid);
1095 if (rc)
1096 return rc;
1097 }
1098 if (sbsec->flags & DEFCONTEXT_MNT) {
1099 seq_putc(m, ',');
1100 seq_puts(m, DEFCONTEXT_STR);
1101 rc = show_sid(m, sbsec->def_sid);
1102 if (rc)
1103 return rc;
1104 }
1105 if (sbsec->flags & ROOTCONTEXT_MNT) {
Ondrej Mosnacekb159e862020-11-04 13:01:10 +01001106 struct dentry *root = sb->s_root;
Al Viroe3489f82018-12-13 00:24:36 -05001107 struct inode_security_struct *isec = backing_inode_security(root);
1108 seq_putc(m, ',');
1109 seq_puts(m, ROOTCONTEXT_STR);
1110 rc = show_sid(m, isec->sid);
1111 if (rc)
1112 return rc;
1113 }
1114 if (sbsec->flags & SBLABEL_MNT) {
1115 seq_putc(m, ',');
David Howells442155c2018-11-01 23:07:24 +00001116 seq_puts(m, SECLABEL_STR);
Al Viroe3489f82018-12-13 00:24:36 -05001117 }
1118 return 0;
Eric Paris2069f452008-07-04 09:47:13 +10001119}
1120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121static inline u16 inode_mode_to_security_class(umode_t mode)
1122{
1123 switch (mode & S_IFMT) {
1124 case S_IFSOCK:
1125 return SECCLASS_SOCK_FILE;
1126 case S_IFLNK:
1127 return SECCLASS_LNK_FILE;
1128 case S_IFREG:
1129 return SECCLASS_FILE;
1130 case S_IFBLK:
1131 return SECCLASS_BLK_FILE;
1132 case S_IFDIR:
1133 return SECCLASS_DIR;
1134 case S_IFCHR:
1135 return SECCLASS_CHR_FILE;
1136 case S_IFIFO:
1137 return SECCLASS_FIFO_FILE;
1138
1139 }
1140
1141 return SECCLASS_FILE;
1142}
1143
James Morris13402582005-09-30 14:24:34 -04001144static inline int default_protocol_stream(int protocol)
1145{
Paolo Abeni95ca9072020-12-16 12:55:27 +01001146 return (protocol == IPPROTO_IP || protocol == IPPROTO_TCP ||
1147 protocol == IPPROTO_MPTCP);
James Morris13402582005-09-30 14:24:34 -04001148}
1149
1150static inline int default_protocol_dgram(int protocol)
1151{
1152 return (protocol == IPPROTO_IP || protocol == IPPROTO_UDP);
1153}
1154
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155static inline u16 socket_type_to_security_class(int family, int type, int protocol)
1156{
Christian Göttschea13479b2023-07-06 15:23:27 +02001157 bool extsockclass = selinux_policycap_extsockclass();
Stephen Smalleyda69a532017-01-09 10:07:30 -05001158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 switch (family) {
1160 case PF_UNIX:
1161 switch (type) {
1162 case SOCK_STREAM:
1163 case SOCK_SEQPACKET:
1164 return SECCLASS_UNIX_STREAM_SOCKET;
1165 case SOCK_DGRAM:
Luis Ressel2a764b52017-07-25 15:13:41 -04001166 case SOCK_RAW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 return SECCLASS_UNIX_DGRAM_SOCKET;
1168 }
1169 break;
1170 case PF_INET:
1171 case PF_INET6:
1172 switch (type) {
1173 case SOCK_STREAM:
Stephen Smalleyda69a532017-01-09 10:07:30 -05001174 case SOCK_SEQPACKET:
James Morris13402582005-09-30 14:24:34 -04001175 if (default_protocol_stream(protocol))
1176 return SECCLASS_TCP_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001177 else if (extsockclass && protocol == IPPROTO_SCTP)
1178 return SECCLASS_SCTP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001179 else
1180 return SECCLASS_RAWIP_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 case SOCK_DGRAM:
James Morris13402582005-09-30 14:24:34 -04001182 if (default_protocol_dgram(protocol))
1183 return SECCLASS_UDP_SOCKET;
Stephen Smalleyef379792017-01-09 10:07:31 -05001184 else if (extsockclass && (protocol == IPPROTO_ICMP ||
1185 protocol == IPPROTO_ICMPV6))
Stephen Smalleyda69a532017-01-09 10:07:30 -05001186 return SECCLASS_ICMP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001187 else
1188 return SECCLASS_RAWIP_SOCKET;
James Morris2ee92d42006-11-13 16:09:01 -08001189 case SOCK_DCCP:
1190 return SECCLASS_DCCP_SOCKET;
James Morris13402582005-09-30 14:24:34 -04001191 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 return SECCLASS_RAWIP_SOCKET;
1193 }
1194 break;
1195 case PF_NETLINK:
1196 switch (protocol) {
1197 case NETLINK_ROUTE:
1198 return SECCLASS_NETLINK_ROUTE_SOCKET;
Pavel Emelyanov7f1fb602011-12-06 07:56:43 +00001199 case NETLINK_SOCK_DIAG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 return SECCLASS_NETLINK_TCPDIAG_SOCKET;
1201 case NETLINK_NFLOG:
1202 return SECCLASS_NETLINK_NFLOG_SOCKET;
1203 case NETLINK_XFRM:
1204 return SECCLASS_NETLINK_XFRM_SOCKET;
1205 case NETLINK_SELINUX:
1206 return SECCLASS_NETLINK_SELINUX_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001207 case NETLINK_ISCSI:
1208 return SECCLASS_NETLINK_ISCSI_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 case NETLINK_AUDIT:
1210 return SECCLASS_NETLINK_AUDIT_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001211 case NETLINK_FIB_LOOKUP:
1212 return SECCLASS_NETLINK_FIB_LOOKUP_SOCKET;
1213 case NETLINK_CONNECTOR:
1214 return SECCLASS_NETLINK_CONNECTOR_SOCKET;
1215 case NETLINK_NETFILTER:
1216 return SECCLASS_NETLINK_NETFILTER_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 case NETLINK_DNRTMSG:
1218 return SECCLASS_NETLINK_DNRT_SOCKET;
James Morris0c9b7942005-04-16 15:24:13 -07001219 case NETLINK_KOBJECT_UEVENT:
1220 return SECCLASS_NETLINK_KOBJECT_UEVENT_SOCKET;
Stephen Smalley6c6d2e92015-06-04 16:22:16 -04001221 case NETLINK_GENERIC:
1222 return SECCLASS_NETLINK_GENERIC_SOCKET;
1223 case NETLINK_SCSITRANSPORT:
1224 return SECCLASS_NETLINK_SCSITRANSPORT_SOCKET;
1225 case NETLINK_RDMA:
1226 return SECCLASS_NETLINK_RDMA_SOCKET;
1227 case NETLINK_CRYPTO:
1228 return SECCLASS_NETLINK_CRYPTO_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 default:
1230 return SECCLASS_NETLINK_SOCKET;
1231 }
1232 case PF_PACKET:
1233 return SECCLASS_PACKET_SOCKET;
1234 case PF_KEY:
1235 return SECCLASS_KEY_SOCKET;
Christopher J. PeBenito3e3ff152006-06-09 00:25:03 -07001236 case PF_APPLETALK:
1237 return SECCLASS_APPLETALK_SOCKET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 }
1239
Stephen Smalleyda69a532017-01-09 10:07:30 -05001240 if (extsockclass) {
1241 switch (family) {
1242 case PF_AX25:
1243 return SECCLASS_AX25_SOCKET;
1244 case PF_IPX:
1245 return SECCLASS_IPX_SOCKET;
1246 case PF_NETROM:
1247 return SECCLASS_NETROM_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001248 case PF_ATMPVC:
1249 return SECCLASS_ATMPVC_SOCKET;
1250 case PF_X25:
1251 return SECCLASS_X25_SOCKET;
1252 case PF_ROSE:
1253 return SECCLASS_ROSE_SOCKET;
1254 case PF_DECnet:
1255 return SECCLASS_DECNET_SOCKET;
1256 case PF_ATMSVC:
1257 return SECCLASS_ATMSVC_SOCKET;
1258 case PF_RDS:
1259 return SECCLASS_RDS_SOCKET;
1260 case PF_IRDA:
1261 return SECCLASS_IRDA_SOCKET;
1262 case PF_PPPOX:
1263 return SECCLASS_PPPOX_SOCKET;
1264 case PF_LLC:
1265 return SECCLASS_LLC_SOCKET;
Stephen Smalleyda69a532017-01-09 10:07:30 -05001266 case PF_CAN:
1267 return SECCLASS_CAN_SOCKET;
1268 case PF_TIPC:
1269 return SECCLASS_TIPC_SOCKET;
1270 case PF_BLUETOOTH:
1271 return SECCLASS_BLUETOOTH_SOCKET;
1272 case PF_IUCV:
1273 return SECCLASS_IUCV_SOCKET;
1274 case PF_RXRPC:
1275 return SECCLASS_RXRPC_SOCKET;
1276 case PF_ISDN:
1277 return SECCLASS_ISDN_SOCKET;
1278 case PF_PHONET:
1279 return SECCLASS_PHONET_SOCKET;
1280 case PF_IEEE802154:
1281 return SECCLASS_IEEE802154_SOCKET;
1282 case PF_CAIF:
1283 return SECCLASS_CAIF_SOCKET;
1284 case PF_ALG:
1285 return SECCLASS_ALG_SOCKET;
1286 case PF_NFC:
1287 return SECCLASS_NFC_SOCKET;
1288 case PF_VSOCK:
1289 return SECCLASS_VSOCK_SOCKET;
1290 case PF_KCM:
1291 return SECCLASS_KCM_SOCKET;
1292 case PF_QIPCRTR:
1293 return SECCLASS_QIPCRTR_SOCKET;
Linus Torvalds3051bf32017-02-22 10:15:09 -08001294 case PF_SMC:
1295 return SECCLASS_SMC_SOCKET;
Björn Töpel68e8b842018-05-02 13:01:22 +02001296 case PF_XDP:
1297 return SECCLASS_XDP_SOCKET;
Jeremy Kerrbc49d812021-07-29 10:20:39 +08001298 case PF_MCTP:
1299 return SECCLASS_MCTP_SOCKET;
1300#if PF_MAX > 46
Stephen Smalleyda69a532017-01-09 10:07:30 -05001301#error New address family defined, please update this function.
1302#endif
1303 }
1304 }
1305
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 return SECCLASS_SOCKET;
1307}
1308
Stephen Smalley134509d2015-06-04 16:22:17 -04001309static int selinux_genfs_get_sid(struct dentry *dentry,
1310 u16 tclass,
1311 u16 flags,
1312 u32 *sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313{
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001314 int rc;
Al Virofc640052016-04-10 01:33:30 -04001315 struct super_block *sb = dentry->d_sb;
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001316 char *buffer, *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Eric Paris828dfe12008-04-17 13:17:49 -04001318 buffer = (char *)__get_free_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 if (!buffer)
1320 return -ENOMEM;
1321
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001322 path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
1323 if (IS_ERR(path))
1324 rc = PTR_ERR(path);
1325 else {
Stephen Smalley134509d2015-06-04 16:22:17 -04001326 if (flags & SE_SBPROC) {
1327 /* each process gets a /proc/PID/ entry. Strip off the
1328 * PID part to get a valid selinux labeling.
1329 * e.g. /proc/1/net/rpc/nfs -> /net/rpc/nfs */
1330 while (path[1] >= '0' && path[1] <= '9') {
1331 path[1] = '/';
1332 path++;
1333 }
Lucian Adrian Grijincu8e6c9692011-02-01 18:42:22 +02001334 }
Stephen Smalleye67b7982023-03-09 13:30:37 -05001335 rc = security_genfs_sid(sb->s_type->name,
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001336 path, tclass, sid);
Stephen Smalley7bb185e2018-09-04 16:51:36 -04001337 if (rc == -ENOENT) {
1338 /* No match in policy, mark as unlabeled. */
1339 *sid = SECINITSID_UNLABELED;
1340 rc = 0;
1341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 free_page((unsigned long)buffer);
1344 return rc;
1345}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
Ondrej Mosnacekb7540262019-02-22 15:57:14 +01001347static int inode_doinit_use_xattr(struct inode *inode, struct dentry *dentry,
1348 u32 def_sid, u32 *sid)
1349{
1350#define INITCONTEXTLEN 255
1351 char *context;
1352 unsigned int len;
1353 int rc;
1354
1355 len = INITCONTEXTLEN;
1356 context = kmalloc(len + 1, GFP_NOFS);
1357 if (!context)
1358 return -ENOMEM;
1359
1360 context[len] = '\0';
1361 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, context, len);
1362 if (rc == -ERANGE) {
1363 kfree(context);
1364
1365 /* Need a larger buffer. Query for the right size. */
1366 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX, NULL, 0);
1367 if (rc < 0)
1368 return rc;
1369
1370 len = rc;
1371 context = kmalloc(len + 1, GFP_NOFS);
1372 if (!context)
1373 return -ENOMEM;
1374
1375 context[len] = '\0';
1376 rc = __vfs_getxattr(dentry, inode, XATTR_NAME_SELINUX,
1377 context, len);
1378 }
1379 if (rc < 0) {
1380 kfree(context);
1381 if (rc != -ENODATA) {
1382 pr_warn("SELinux: %s: getxattr returned %d for dev=%s ino=%ld\n",
1383 __func__, -rc, inode->i_sb->s_id, inode->i_ino);
1384 return rc;
1385 }
1386 *sid = def_sid;
1387 return 0;
1388 }
1389
Stephen Smalleye67b7982023-03-09 13:30:37 -05001390 rc = security_context_to_sid_default(context, rc, sid,
Ondrej Mosnacekb7540262019-02-22 15:57:14 +01001391 def_sid, GFP_NOFS);
1392 if (rc) {
1393 char *dev = inode->i_sb->s_id;
1394 unsigned long ino = inode->i_ino;
1395
1396 if (rc == -EINVAL) {
1397 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",
1398 ino, dev, context);
1399 } else {
1400 pr_warn("SELinux: %s: context_to_sid(%s) returned %d for dev=%s ino=%ld\n",
1401 __func__, context, -rc, dev, ino);
1402 }
1403 }
1404 kfree(context);
1405 return 0;
1406}
1407
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408/* The inode's security attributes must be initialized before first use. */
1409static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentry)
1410{
1411 struct superblock_security_struct *sbsec = NULL;
Casey Schaufler80788c22018-09-21 17:19:11 -07001412 struct inode_security_struct *isec = selinux_inode(inode);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001413 u32 task_sid, sid = 0;
1414 u16 sclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05001418 if (isec->initialized == LABEL_INITIALIZED)
Andreas Gruenbacher13457d02016-11-10 22:18:29 +01001419 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001421 spin_lock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05001422 if (isec->initialized == LABEL_INITIALIZED)
Eric Paris23970742006-09-25 23:32:01 -07001423 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Andreas Gruenbacher13457d02016-11-10 22:18:29 +01001425 if (isec->sclass == SECCLASS_FILE)
1426 isec->sclass = inode_mode_to_security_class(inode->i_mode);
1427
Casey Schaufler1aea7802021-04-22 17:41:15 +02001428 sbsec = selinux_superblock(inode->i_sb);
David P. Quigley0d90a7e2009-01-16 09:22:02 -05001429 if (!(sbsec->flags & SE_SBINITIALIZED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 /* Defer initialization until selinux_complete_init,
1431 after the initial policy is loaded and the security
1432 server is ready to handle calls. */
1433 spin_lock(&sbsec->isec_lock);
1434 if (list_empty(&isec->list))
1435 list_add(&isec->list, &sbsec->isec_head);
1436 spin_unlock(&sbsec->isec_lock);
Eric Paris23970742006-09-25 23:32:01 -07001437 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 }
1439
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001440 sclass = isec->sclass;
1441 task_sid = isec->task_sid;
1442 sid = isec->sid;
1443 isec->initialized = LABEL_PENDING;
1444 spin_unlock(&isec->lock);
1445
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 switch (sbsec->behavior) {
Ondrej Mosnacekcec5fe72023-05-29 16:05:27 +02001447 /*
1448 * In case of SECURITY_FS_USE_NATIVE we need to re-fetch the labels
1449 * via xattr when called from delayed_superblock_init().
1450 */
David Quigleyeb9ae682013-05-22 12:50:37 -04001451 case SECURITY_FS_USE_NATIVE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 case SECURITY_FS_USE_XATTR:
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +02001453 if (!(inode->i_opflags & IOP_XATTR)) {
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001454 sid = sbsec->def_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 break;
1456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 /* Need a dentry, since the xattr API requires one.
1458 Life would be simpler if we could just pass the inode. */
1459 if (opt_dentry) {
1460 /* Called from d_instantiate or d_splice_alias. */
1461 dentry = dget(opt_dentry);
1462 } else {
Al Virob1271252018-04-25 10:28:38 -04001463 /*
1464 * Called from selinux_complete_init, try to find a dentry.
1465 * Some filesystems really want a connected one, so try
1466 * that first. We could split SECURITY_FS_USE_XATTR in
1467 * two, depending upon that...
1468 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 dentry = d_find_alias(inode);
Al Virob1271252018-04-25 10:28:38 -04001470 if (!dentry)
1471 dentry = d_find_any_alias(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 }
1473 if (!dentry) {
Eric Parisdf7f54c2009-03-09 14:35:58 -04001474 /*
1475 * this is can be hit on boot when a file is accessed
1476 * before the policy is loaded. When we load policy we
1477 * may find inodes that have no dentry on the
1478 * sbsec->isec_head list. No reason to complain as these
1479 * will get fixed up the next time we go through
1480 * inode_doinit with a dentry, before these inodes could
1481 * be used again by userspace.
1482 */
Paul Moore200ea5a2020-11-03 11:49:38 -05001483 goto out_invalid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 }
1485
Ondrej Mosnacekb7540262019-02-22 15:57:14 +01001486 rc = inode_doinit_use_xattr(inode, dentry, sbsec->def_sid,
1487 &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 dput(dentry);
Ondrej Mosnacekb7540262019-02-22 15:57:14 +01001489 if (rc)
1490 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 break;
1492 case SECURITY_FS_USE_TASK:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001493 sid = task_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 break;
1495 case SECURITY_FS_USE_TRANS:
1496 /* Default to the fs SID. */
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001497 sid = sbsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
1499 /* Try to obtain a transition SID. */
Stephen Smalleye67b7982023-03-09 13:30:37 -05001500 rc = security_transition_sid(task_sid, sid,
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001501 sclass, NULL, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 if (rc)
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001503 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 break;
Eric Parisc312feb2006-07-10 04:43:53 -07001505 case SECURITY_FS_USE_MNTPOINT:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001506 sid = sbsec->mntpoint_sid;
Eric Parisc312feb2006-07-10 04:43:53 -07001507 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 default:
Eric Parisc312feb2006-07-10 04:43:53 -07001509 /* Default to the fs superblock SID. */
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001510 sid = sbsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Christian Göttsche7470d0d12020-01-28 20:16:48 +01001512 if ((sbsec->flags & SE_SBGENFS) &&
1513 (!S_ISLNK(inode->i_mode) ||
1514 selinux_policycap_genfs_seclabel_symlinks())) {
Paul Mooref64410e2014-03-19 16:46:18 -04001515 /* We must have a dentry to determine the label on
1516 * procfs inodes */
Al Virob1271252018-04-25 10:28:38 -04001517 if (opt_dentry) {
Paul Mooref64410e2014-03-19 16:46:18 -04001518 /* Called from d_instantiate or
1519 * d_splice_alias. */
1520 dentry = dget(opt_dentry);
Al Virob1271252018-04-25 10:28:38 -04001521 } else {
Paul Mooref64410e2014-03-19 16:46:18 -04001522 /* Called from selinux_complete_init, try to
Al Virob1271252018-04-25 10:28:38 -04001523 * find a dentry. Some filesystems really want
1524 * a connected one, so try that first.
1525 */
Paul Mooref64410e2014-03-19 16:46:18 -04001526 dentry = d_find_alias(inode);
Al Virob1271252018-04-25 10:28:38 -04001527 if (!dentry)
1528 dentry = d_find_any_alias(inode);
1529 }
Paul Mooref64410e2014-03-19 16:46:18 -04001530 /*
1531 * This can be hit on boot when a file is accessed
1532 * before the policy is loaded. When we load policy we
1533 * may find inodes that have no dentry on the
1534 * sbsec->isec_head list. No reason to complain as
1535 * these will get fixed up the next time we go through
1536 * inode_doinit() with a dentry, before these inodes
1537 * could be used again by userspace.
1538 */
1539 if (!dentry)
Paul Moore200ea5a2020-11-03 11:49:38 -05001540 goto out_invalid;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001541 rc = selinux_genfs_get_sid(dentry, sclass,
Stephen Smalley134509d2015-06-04 16:22:17 -04001542 sbsec->flags, &sid);
Ondrej Mosnacekb7540262019-02-22 15:57:14 +01001543 if (rc) {
1544 dput(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001545 goto out;
Ondrej Mosnacekb7540262019-02-22 15:57:14 +01001546 }
1547
1548 if ((sbsec->flags & SE_SBGENFS_XATTR) &&
1549 (inode->i_opflags & IOP_XATTR)) {
1550 rc = inode_doinit_use_xattr(inode, dentry,
1551 sid, &sid);
1552 if (rc) {
1553 dput(dentry);
1554 goto out;
1555 }
1556 }
1557 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 }
1559 break;
1560 }
1561
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001562out:
1563 spin_lock(&isec->lock);
1564 if (isec->initialized == LABEL_PENDING) {
Paul Moore200ea5a2020-11-03 11:49:38 -05001565 if (rc) {
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001566 isec->initialized = LABEL_INVALID;
1567 goto out_unlock;
1568 }
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001569 isec->initialized = LABEL_INITIALIZED;
1570 isec->sid = sid;
1571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
Eric Paris23970742006-09-25 23:32:01 -07001573out_unlock:
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01001574 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 return rc;
Paul Moore200ea5a2020-11-03 11:49:38 -05001576
1577out_invalid:
1578 spin_lock(&isec->lock);
1579 if (isec->initialized == LABEL_PENDING) {
1580 isec->initialized = LABEL_INVALID;
1581 isec->sid = sid;
1582 }
1583 spin_unlock(&isec->lock);
1584 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585}
1586
1587/* Convert a Linux signal to an access vector. */
1588static inline u32 signal_to_av(int sig)
1589{
1590 u32 perm = 0;
1591
1592 switch (sig) {
1593 case SIGCHLD:
1594 /* Commonly granted from child to parent. */
1595 perm = PROCESS__SIGCHLD;
1596 break;
1597 case SIGKILL:
1598 /* Cannot be caught or ignored */
1599 perm = PROCESS__SIGKILL;
1600 break;
1601 case SIGSTOP:
1602 /* Cannot be caught or ignored */
1603 perm = PROCESS__SIGSTOP;
1604 break;
1605 default:
1606 /* All other signals. */
1607 perm = PROCESS__SIGNAL;
1608 break;
1609 }
1610
1611 return perm;
1612}
1613
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001614#if CAP_LAST_CAP > 63
1615#error Fix SELinux to handle capabilities > 63.
1616#endif
1617
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618/* Check whether a task is allowed to use a capability. */
Eric Paris6a9de492012-01-03 12:25:14 -05001619static int cred_has_capability(const struct cred *cred,
Micah Mortonc1a85a02019-01-07 16:10:53 -08001620 int cap, unsigned int opts, bool initns)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621{
Thomas Liu2bf49692009-07-14 12:14:09 -04001622 struct common_audit_data ad;
Eric Paris06112162008-11-11 22:02:50 +11001623 struct av_decision avd;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001624 u16 sclass;
David Howells3699c532009-01-06 22:27:01 +00001625 u32 sid = cred_sid(cred);
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001626 u32 av = CAP_TO_MASK(cap);
Eric Paris06112162008-11-11 22:02:50 +11001627 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
Eric Paris50c205f2012-04-04 15:01:43 -04001629 ad.type = LSM_AUDIT_DATA_CAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 ad.u.cap = cap;
1631
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001632 switch (CAP_TO_INDEX(cap)) {
1633 case 0:
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04001634 sclass = initns ? SECCLASS_CAPABILITY : SECCLASS_CAP_USERNS;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001635 break;
1636 case 1:
Stephen Smalley8e4ff6f2016-04-08 13:52:00 -04001637 sclass = initns ? SECCLASS_CAPABILITY2 : SECCLASS_CAP2_USERNS;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001638 break;
1639 default:
peter enderborgc103a912018-06-12 10:09:03 +02001640 pr_err("SELinux: out of range capability %d\n", cap);
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001641 BUG();
Eric Parisa35c6c832011-04-20 10:21:28 -04001642 return -EINVAL;
Stephen Smalleyb68e4182008-02-07 11:21:04 -05001643 }
Eric Paris06112162008-11-11 22:02:50 +11001644
Stephen Smalleye67b7982023-03-09 13:30:37 -05001645 rc = avc_has_perm_noaudit(sid, sid, sclass, av, 0, &avd);
Micah Mortonc1a85a02019-01-07 16:10:53 -08001646 if (!(opts & CAP_OPT_NOAUDIT)) {
Stephen Smalleye67b7982023-03-09 13:30:37 -05001647 int rc2 = avc_audit(sid, sid, sclass, av, &avd, rc, &ad);
Eric Paris9ade0cf2011-04-25 16:26:29 -04001648 if (rc2)
1649 return rc2;
1650 }
Eric Paris06112162008-11-11 22:02:50 +11001651 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652}
1653
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654/* Check whether a task has a particular permission to an inode.
1655 The 'adp' parameter is optional and allows other audit
1656 data to be passed (e.g. the dentry). */
David Howells88e67f32008-11-14 10:39:21 +11001657static int inode_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 struct inode *inode,
1659 u32 perms,
Linus Torvalds19e49832013-10-04 12:54:11 -07001660 struct common_audit_data *adp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 struct inode_security_struct *isec;
David Howells275bb412008-11-14 10:39:19 +11001663 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
Eric Paris828dfe12008-04-17 13:17:49 -04001665 if (unlikely(IS_PRIVATE(inode)))
Stephen Smalleybbaca6c2007-02-14 00:34:16 -08001666 return 0;
1667
David Howells88e67f32008-11-14 10:39:21 +11001668 sid = cred_sid(cred);
Casey Schaufler80788c22018-09-21 17:19:11 -07001669 isec = selinux_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
Stephen Smalleye67b7982023-03-09 13:30:37 -05001671 return avc_has_perm(sid, isec->sid, isec->sclass, perms, adp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672}
1673
1674/* Same as inode_has_perm, but pass explicit audit data containing
1675 the dentry to help the auditing code to more easily generate the
1676 pathname if needed. */
David Howells88e67f32008-11-14 10:39:21 +11001677static inline int dentry_has_perm(const struct cred *cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 struct dentry *dentry,
1679 u32 av)
1680{
David Howellsc6f493d2015-03-17 22:26:22 +00001681 struct inode *inode = d_backing_inode(dentry);
Thomas Liu2bf49692009-07-14 12:14:09 -04001682 struct common_audit_data ad;
David Howells88e67f32008-11-14 10:39:21 +11001683
Eric Paris50c205f2012-04-04 15:01:43 -04001684 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Paris2875fa02011-04-28 16:04:24 -04001685 ad.u.dentry = dentry;
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05001686 __inode_security_revalidate(inode, dentry, true);
Linus Torvalds19e49832013-10-04 12:54:11 -07001687 return inode_has_perm(cred, inode, av, &ad);
Eric Paris2875fa02011-04-28 16:04:24 -04001688}
1689
1690/* Same as inode_has_perm, but pass explicit audit data containing
1691 the path to help the auditing code to more easily generate the
1692 pathname if needed. */
1693static inline int path_has_perm(const struct cred *cred,
Al Viro3f7036a2015-03-08 19:28:30 -04001694 const struct path *path,
Eric Paris2875fa02011-04-28 16:04:24 -04001695 u32 av)
1696{
David Howellsc6f493d2015-03-17 22:26:22 +00001697 struct inode *inode = d_backing_inode(path->dentry);
Eric Paris2875fa02011-04-28 16:04:24 -04001698 struct common_audit_data ad;
1699
Eric Paris50c205f2012-04-04 15:01:43 -04001700 ad.type = LSM_AUDIT_DATA_PATH;
Eric Paris2875fa02011-04-28 16:04:24 -04001701 ad.u.path = *path;
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05001702 __inode_security_revalidate(inode, path->dentry, true);
Linus Torvalds19e49832013-10-04 12:54:11 -07001703 return inode_has_perm(cred, inode, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704}
1705
David Howells13f8e982013-06-13 23:37:55 +01001706/* Same as path_has_perm, but uses the inode from the file struct. */
1707static inline int file_path_has_perm(const struct cred *cred,
1708 struct file *file,
1709 u32 av)
1710{
1711 struct common_audit_data ad;
1712
Vivek Goyal43af5de2016-09-09 11:37:49 -04001713 ad.type = LSM_AUDIT_DATA_FILE;
1714 ad.u.file = file;
Linus Torvalds19e49832013-10-04 12:54:11 -07001715 return inode_has_perm(cred, file_inode(file), av, &ad);
David Howells13f8e982013-06-13 23:37:55 +01001716}
1717
Chenbo Fengf66e4482017-10-18 13:00:26 -07001718#ifdef CONFIG_BPF_SYSCALL
Khadija Kamran8e4672d2023-08-12 20:31:08 +05001719static int bpf_fd_pass(const struct file *file, u32 sid);
Chenbo Fengf66e4482017-10-18 13:00:26 -07001720#endif
1721
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722/* Check whether a task can use an open file descriptor to
1723 access an inode in a given way. Check access to the
1724 descriptor itself, and then use dentry_has_perm to
1725 check a particular permission to the file.
1726 Access to the descriptor is implicitly granted if it
1727 has the same SID as the process. If av is zero, then
1728 access to the file is not checked, e.g. for cases
1729 where only the descriptor is affected like seek. */
David Howells88e67f32008-11-14 10:39:21 +11001730static int file_has_perm(const struct cred *cred,
1731 struct file *file,
1732 u32 av)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733{
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07001734 struct file_security_struct *fsec = selinux_file(file);
Al Viro496ad9a2013-01-23 17:07:38 -05001735 struct inode *inode = file_inode(file);
Thomas Liu2bf49692009-07-14 12:14:09 -04001736 struct common_audit_data ad;
David Howells88e67f32008-11-14 10:39:21 +11001737 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 int rc;
1739
Vivek Goyal43af5de2016-09-09 11:37:49 -04001740 ad.type = LSM_AUDIT_DATA_FILE;
1741 ad.u.file = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
David Howells275bb412008-11-14 10:39:19 +11001743 if (sid != fsec->sid) {
Stephen Smalleye67b7982023-03-09 13:30:37 -05001744 rc = avc_has_perm(sid, fsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 SECCLASS_FD,
1746 FD__USE,
1747 &ad);
1748 if (rc)
David Howells88e67f32008-11-14 10:39:21 +11001749 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 }
1751
Chenbo Fengf66e4482017-10-18 13:00:26 -07001752#ifdef CONFIG_BPF_SYSCALL
1753 rc = bpf_fd_pass(file, cred_sid(cred));
1754 if (rc)
1755 return rc;
1756#endif
1757
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 /* av is zero if only checking access to the descriptor. */
David Howells88e67f32008-11-14 10:39:21 +11001759 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 if (av)
Linus Torvalds19e49832013-10-04 12:54:11 -07001761 rc = inode_has_perm(cred, inode, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
David Howells88e67f32008-11-14 10:39:21 +11001763out:
1764 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765}
1766
David Howellsc3c188b2015-07-10 17:19:58 -04001767/*
1768 * Determine the label for an inode that might be unioned.
1769 */
Vivek Goyalc957f6d2016-07-13 10:44:51 -04001770static int
1771selinux_determine_inode_label(const struct task_security_struct *tsec,
1772 struct inode *dir,
1773 const struct qstr *name, u16 tclass,
1774 u32 *_new_isid)
David Howellsc3c188b2015-07-10 17:19:58 -04001775{
Casey Schaufler1aea7802021-04-22 17:41:15 +02001776 const struct superblock_security_struct *sbsec =
1777 selinux_superblock(dir->i_sb);
David Howellsc3c188b2015-07-10 17:19:58 -04001778
1779 if ((sbsec->flags & SE_SBINITIALIZED) &&
1780 (sbsec->behavior == SECURITY_FS_USE_MNTPOINT)) {
1781 *_new_isid = sbsec->mntpoint_sid;
1782 } else if ((sbsec->flags & SBLABEL_MNT) &&
1783 tsec->create_sid) {
1784 *_new_isid = tsec->create_sid;
1785 } else {
Paul Moore20cdef82016-04-04 14:14:42 -04001786 const struct inode_security_struct *dsec = inode_security(dir);
Stephen Smalleye67b7982023-03-09 13:30:37 -05001787 return security_transition_sid(tsec->sid,
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05001788 dsec->sid, tclass,
David Howellsc3c188b2015-07-10 17:19:58 -04001789 name, _new_isid);
1790 }
1791
1792 return 0;
1793}
1794
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795/* Check whether a task can create a file. */
1796static int may_create(struct inode *dir,
1797 struct dentry *dentry,
1798 u16 tclass)
1799{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07001800 const struct task_security_struct *tsec = selinux_cred(current_cred());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 struct inode_security_struct *dsec;
1802 struct superblock_security_struct *sbsec;
David Howells275bb412008-11-14 10:39:19 +11001803 u32 sid, newsid;
Thomas Liu2bf49692009-07-14 12:14:09 -04001804 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 int rc;
1806
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001807 dsec = inode_security(dir);
Casey Schaufler1aea7802021-04-22 17:41:15 +02001808 sbsec = selinux_superblock(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
David Howells275bb412008-11-14 10:39:19 +11001810 sid = tsec->sid;
David Howells275bb412008-11-14 10:39:19 +11001811
Eric Paris50c205f2012-04-04 15:01:43 -04001812 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04001813 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Stephen Smalleye67b7982023-03-09 13:30:37 -05001815 rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 DIR__ADD_NAME | DIR__SEARCH,
1817 &ad);
1818 if (rc)
1819 return rc;
1820
Yang Guo210a2922019-12-12 10:02:24 +08001821 rc = selinux_determine_inode_label(tsec, dir, &dentry->d_name, tclass,
1822 &newsid);
David Howellsc3c188b2015-07-10 17:19:58 -04001823 if (rc)
1824 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
Stephen Smalleye67b7982023-03-09 13:30:37 -05001826 rc = avc_has_perm(sid, newsid, tclass, FILE__CREATE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 if (rc)
1828 return rc;
1829
Stephen Smalleye67b7982023-03-09 13:30:37 -05001830 return avc_has_perm(newsid, sbsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 SECCLASS_FILESYSTEM,
1832 FILESYSTEM__ASSOCIATE, &ad);
1833}
1834
Eric Paris828dfe12008-04-17 13:17:49 -04001835#define MAY_LINK 0
1836#define MAY_UNLINK 1
1837#define MAY_RMDIR 2
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
1839/* Check whether a task can link, unlink, or rmdir a file/directory. */
1840static int may_link(struct inode *dir,
1841 struct dentry *dentry,
1842 int kind)
1843
1844{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 struct inode_security_struct *dsec, *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001846 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11001847 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 u32 av;
1849 int rc;
1850
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001851 dsec = inode_security(dir);
1852 isec = backing_inode_security(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
Eric Paris50c205f2012-04-04 15:01:43 -04001854 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04001855 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
1857 av = DIR__SEARCH;
1858 av |= (kind ? DIR__REMOVE_NAME : DIR__ADD_NAME);
Stephen Smalleye67b7982023-03-09 13:30:37 -05001859 rc = avc_has_perm(sid, dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 if (rc)
1861 return rc;
1862
1863 switch (kind) {
1864 case MAY_LINK:
1865 av = FILE__LINK;
1866 break;
1867 case MAY_UNLINK:
1868 av = FILE__UNLINK;
1869 break;
1870 case MAY_RMDIR:
1871 av = DIR__RMDIR;
1872 break;
1873 default:
peter enderborgc103a912018-06-12 10:09:03 +02001874 pr_warn("SELinux: %s: unrecognized kind %d\n",
Eric Paris744ba352008-04-17 11:52:44 -04001875 __func__, kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 return 0;
1877 }
1878
Stephen Smalleye67b7982023-03-09 13:30:37 -05001879 rc = avc_has_perm(sid, isec->sid, isec->sclass, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 return rc;
1881}
1882
1883static inline int may_rename(struct inode *old_dir,
1884 struct dentry *old_dentry,
1885 struct inode *new_dir,
1886 struct dentry *new_dentry)
1887{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 struct inode_security_struct *old_dsec, *new_dsec, *old_isec, *new_isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04001889 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11001890 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 u32 av;
1892 int old_is_dir, new_is_dir;
1893 int rc;
1894
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001895 old_dsec = inode_security(old_dir);
1896 old_isec = backing_inode_security(old_dentry);
David Howellse36cb0b2015-01-29 12:02:35 +00001897 old_is_dir = d_is_dir(old_dentry);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001898 new_dsec = inode_security(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
Eric Paris50c205f2012-04-04 15:01:43 -04001900 ad.type = LSM_AUDIT_DATA_DENTRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Eric Parisa2694342011-04-25 13:10:27 -04001902 ad.u.dentry = old_dentry;
Stephen Smalleye67b7982023-03-09 13:30:37 -05001903 rc = avc_has_perm(sid, old_dsec->sid, SECCLASS_DIR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 DIR__REMOVE_NAME | DIR__SEARCH, &ad);
1905 if (rc)
1906 return rc;
Stephen Smalleye67b7982023-03-09 13:30:37 -05001907 rc = avc_has_perm(sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 old_isec->sclass, FILE__RENAME, &ad);
1909 if (rc)
1910 return rc;
1911 if (old_is_dir && new_dir != old_dir) {
Stephen Smalleye67b7982023-03-09 13:30:37 -05001912 rc = avc_has_perm(sid, old_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 old_isec->sclass, DIR__REPARENT, &ad);
1914 if (rc)
1915 return rc;
1916 }
1917
Eric Parisa2694342011-04-25 13:10:27 -04001918 ad.u.dentry = new_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 av = DIR__ADD_NAME | DIR__SEARCH;
David Howells2c616d42015-01-29 12:02:33 +00001920 if (d_is_positive(new_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 av |= DIR__REMOVE_NAME;
Stephen Smalleye67b7982023-03-09 13:30:37 -05001922 rc = avc_has_perm(sid, new_dsec->sid, SECCLASS_DIR, av, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 if (rc)
1924 return rc;
David Howells2c616d42015-01-29 12:02:33 +00001925 if (d_is_positive(new_dentry)) {
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05001926 new_isec = backing_inode_security(new_dentry);
David Howellse36cb0b2015-01-29 12:02:35 +00001927 new_is_dir = d_is_dir(new_dentry);
Stephen Smalleye67b7982023-03-09 13:30:37 -05001928 rc = avc_has_perm(sid, new_isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 new_isec->sclass,
1930 (new_is_dir ? DIR__RMDIR : FILE__UNLINK), &ad);
1931 if (rc)
1932 return rc;
1933 }
1934
1935 return 0;
1936}
1937
1938/* Check whether a task can perform a filesystem operation. */
David Howells88e67f32008-11-14 10:39:21 +11001939static int superblock_has_perm(const struct cred *cred,
Khadija Kamran25cc71d2023-08-23 11:44:41 +05001940 const struct super_block *sb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 u32 perms,
Thomas Liu2bf49692009-07-14 12:14:09 -04001942 struct common_audit_data *ad)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 struct superblock_security_struct *sbsec;
David Howells88e67f32008-11-14 10:39:21 +11001945 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
Casey Schaufler1aea7802021-04-22 17:41:15 +02001947 sbsec = selinux_superblock(sb);
Stephen Smalleye67b7982023-03-09 13:30:37 -05001948 return avc_has_perm(sid, sbsec->sid, SECCLASS_FILESYSTEM, perms, ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949}
1950
1951/* Convert a Linux mode and permission mask to an access vector. */
1952static inline u32 file_mask_to_av(int mode, int mask)
1953{
1954 u32 av = 0;
1955
Al Virodba19c62011-07-25 20:49:29 -04001956 if (!S_ISDIR(mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 if (mask & MAY_EXEC)
1958 av |= FILE__EXECUTE;
1959 if (mask & MAY_READ)
1960 av |= FILE__READ;
1961
1962 if (mask & MAY_APPEND)
1963 av |= FILE__APPEND;
1964 else if (mask & MAY_WRITE)
1965 av |= FILE__WRITE;
1966
1967 } else {
1968 if (mask & MAY_EXEC)
1969 av |= DIR__SEARCH;
1970 if (mask & MAY_WRITE)
1971 av |= DIR__WRITE;
1972 if (mask & MAY_READ)
1973 av |= DIR__READ;
1974 }
1975
1976 return av;
1977}
1978
1979/* Convert a Linux file to an access vector. */
Khadija Kamran8e4672d2023-08-12 20:31:08 +05001980static inline u32 file_to_av(const struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981{
1982 u32 av = 0;
1983
1984 if (file->f_mode & FMODE_READ)
1985 av |= FILE__READ;
1986 if (file->f_mode & FMODE_WRITE) {
1987 if (file->f_flags & O_APPEND)
1988 av |= FILE__APPEND;
1989 else
1990 av |= FILE__WRITE;
1991 }
Stephen Smalley0794c662008-03-17 08:55:18 -04001992 if (!av) {
1993 /*
1994 * Special file opened with flags 3 for ioctl-only use.
1995 */
1996 av = FILE__IOCTL;
1997 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
1999 return av;
2000}
2001
Eric Paris8b6a5a32008-10-29 17:06:46 -04002002/*
Randy Dunlapc76a2f92020-08-07 09:51:34 -07002003 * Convert a file to an access vector and include the correct
Eric Paris8b6a5a32008-10-29 17:06:46 -04002004 * open permission.
2005 */
2006static inline u32 open_file_to_av(struct file *file)
2007{
2008 u32 av = file_to_av(file);
Stephen Smalleyccb54472017-05-12 12:41:24 -04002009 struct inode *inode = file_inode(file);
Eric Paris8b6a5a32008-10-29 17:06:46 -04002010
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002011 if (selinux_policycap_openperm() &&
2012 inode->i_sb->s_magic != SOCKFS_MAGIC)
Eric Paris49b7b8d2010-07-23 11:44:09 -04002013 av |= FILE__OPEN;
2014
Eric Paris8b6a5a32008-10-29 17:06:46 -04002015 return av;
2016}
2017
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018/* Hook functions begin here. */
2019
Todd Kjos52f88692021-10-12 09:56:13 -07002020static int selinux_binder_set_context_mgr(const struct cred *mgr)
Stephen Smalley79af7302015-01-21 10:54:10 -05002021{
Stephen Smalleye67b7982023-03-09 13:30:37 -05002022 return avc_has_perm(current_sid(), cred_sid(mgr), SECCLASS_BINDER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002023 BINDER__SET_CONTEXT_MGR, NULL);
2024}
2025
Todd Kjos52f88692021-10-12 09:56:13 -07002026static int selinux_binder_transaction(const struct cred *from,
2027 const struct cred *to)
Stephen Smalley79af7302015-01-21 10:54:10 -05002028{
2029 u32 mysid = current_sid();
Todd Kjos52f88692021-10-12 09:56:13 -07002030 u32 fromsid = cred_sid(from);
2031 u32 tosid = cred_sid(to);
Stephen Smalley79af7302015-01-21 10:54:10 -05002032 int rc;
2033
2034 if (mysid != fromsid) {
Stephen Smalleye67b7982023-03-09 13:30:37 -05002035 rc = avc_has_perm(mysid, fromsid, SECCLASS_BINDER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002036 BINDER__IMPERSONATE, NULL);
2037 if (rc)
2038 return rc;
2039 }
2040
Stephen Smalleye67b7982023-03-09 13:30:37 -05002041 return avc_has_perm(fromsid, tosid,
Paul Mooreeb1231f2021-02-18 15:13:40 -05002042 SECCLASS_BINDER, BINDER__CALL, NULL);
Stephen Smalley79af7302015-01-21 10:54:10 -05002043}
2044
Todd Kjos52f88692021-10-12 09:56:13 -07002045static int selinux_binder_transfer_binder(const struct cred *from,
2046 const struct cred *to)
Stephen Smalley79af7302015-01-21 10:54:10 -05002047{
Stephen Smalleye67b7982023-03-09 13:30:37 -05002048 return avc_has_perm(cred_sid(from), cred_sid(to),
Paul Mooreeb1231f2021-02-18 15:13:40 -05002049 SECCLASS_BINDER, BINDER__TRANSFER,
Stephen Smalley79af7302015-01-21 10:54:10 -05002050 NULL);
2051}
2052
Todd Kjos52f88692021-10-12 09:56:13 -07002053static int selinux_binder_transfer_file(const struct cred *from,
2054 const struct cred *to,
Khadija Kamran8e4672d2023-08-12 20:31:08 +05002055 const struct file *file)
Stephen Smalley79af7302015-01-21 10:54:10 -05002056{
Todd Kjos52f88692021-10-12 09:56:13 -07002057 u32 sid = cred_sid(to);
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07002058 struct file_security_struct *fsec = selinux_file(file);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002059 struct dentry *dentry = file->f_path.dentry;
Paul Moore20cdef82016-04-04 14:14:42 -04002060 struct inode_security_struct *isec;
Stephen Smalley79af7302015-01-21 10:54:10 -05002061 struct common_audit_data ad;
2062 int rc;
2063
2064 ad.type = LSM_AUDIT_DATA_PATH;
2065 ad.u.path = file->f_path;
2066
2067 if (sid != fsec->sid) {
Stephen Smalleye67b7982023-03-09 13:30:37 -05002068 rc = avc_has_perm(sid, fsec->sid,
Stephen Smalley79af7302015-01-21 10:54:10 -05002069 SECCLASS_FD,
2070 FD__USE,
2071 &ad);
2072 if (rc)
2073 return rc;
2074 }
2075
Chenbo Fengf66e4482017-10-18 13:00:26 -07002076#ifdef CONFIG_BPF_SYSCALL
2077 rc = bpf_fd_pass(file, sid);
2078 if (rc)
2079 return rc;
2080#endif
2081
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002082 if (unlikely(IS_PRIVATE(d_backing_inode(dentry))))
Stephen Smalley79af7302015-01-21 10:54:10 -05002083 return 0;
2084
Paul Moore20cdef82016-04-04 14:14:42 -04002085 isec = backing_inode_security(dentry);
Stephen Smalleye67b7982023-03-09 13:30:37 -05002086 return avc_has_perm(sid, isec->sid, isec->sclass, file_to_av(file),
Stephen Smalley79af7302015-01-21 10:54:10 -05002087 &ad);
2088}
2089
Ingo Molnar9e488582009-05-07 19:26:19 +10002090static int selinux_ptrace_access_check(struct task_struct *child,
Paul Mooreeb1231f2021-02-18 15:13:40 -05002091 unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002093 u32 sid = current_sid();
Paul Mooreeb1231f2021-02-18 15:13:40 -05002094 u32 csid = task_sid_obj(child);
Stephen Smalley006ebb42008-05-19 08:32:49 -04002095
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002096 if (mode & PTRACE_MODE_READ)
Stephen Smalleye67b7982023-03-09 13:30:37 -05002097 return avc_has_perm(sid, csid, SECCLASS_FILE, FILE__READ,
2098 NULL);
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002099
Stephen Smalleye67b7982023-03-09 13:30:37 -05002100 return avc_has_perm(sid, csid, SECCLASS_PROCESS, PROCESS__PTRACE,
2101 NULL);
David Howells5cd9c582008-08-14 11:37:28 +01002102}
2103
2104static int selinux_ptrace_traceme(struct task_struct *parent)
2105{
Stephen Smalleye67b7982023-03-09 13:30:37 -05002106 return avc_has_perm(task_sid_obj(parent), task_sid_obj(current),
Paul Mooreeb1231f2021-02-18 15:13:40 -05002107 SECCLASS_PROCESS, PROCESS__PTRACE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108}
2109
Khadija Kamran6672efb2023-08-07 11:59:29 +05002110static int selinux_capget(const struct task_struct *target, kernel_cap_t *effective,
Eric Paris828dfe12008-04-17 13:17:49 -04002111 kernel_cap_t *inheritable, kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112{
Stephen Smalleye67b7982023-03-09 13:30:37 -05002113 return avc_has_perm(current_sid(), task_sid_obj(target),
2114 SECCLASS_PROCESS, PROCESS__GETCAP, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115}
2116
David Howellsd84f4f92008-11-14 10:39:23 +11002117static int selinux_capset(struct cred *new, const struct cred *old,
2118 const kernel_cap_t *effective,
2119 const kernel_cap_t *inheritable,
2120 const kernel_cap_t *permitted)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121{
Stephen Smalleye67b7982023-03-09 13:30:37 -05002122 return avc_has_perm(cred_sid(old), cred_sid(new), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002123 PROCESS__SETCAP, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124}
2125
James Morris5626d3e2009-01-30 10:05:06 +11002126/*
2127 * (This comment used to live with the selinux_task_setuid hook,
2128 * which was removed).
2129 *
2130 * Since setuid only affects the current process, and since the SELinux
2131 * controls are not based on the Linux identity attributes, SELinux does not
2132 * need to control this operation. However, SELinux does control the use of
2133 * the CAP_SETUID and CAP_SETGID capabilities using the capable hook.
2134 */
2135
Eric Paris6a9de492012-01-03 12:25:14 -05002136static int selinux_capable(const struct cred *cred, struct user_namespace *ns,
Micah Mortonc1a85a02019-01-07 16:10:53 -08002137 int cap, unsigned int opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138{
Micah Mortonc1a85a02019-01-07 16:10:53 -08002139 return cred_has_capability(cred, cap, opts, ns == &init_user_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140}
2141
Khadija Kamran25cc71d2023-08-23 11:44:41 +05002142static int selinux_quotactl(int cmds, int type, int id, const struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143{
David Howells88e67f32008-11-14 10:39:21 +11002144 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 int rc = 0;
2146
2147 if (!sb)
2148 return 0;
2149
2150 switch (cmds) {
Eric Paris828dfe12008-04-17 13:17:49 -04002151 case Q_SYNC:
2152 case Q_QUOTAON:
2153 case Q_QUOTAOFF:
2154 case Q_SETINFO:
2155 case Q_SETQUOTA:
Richard Hainese4cfa052020-02-20 15:32:34 +00002156 case Q_XQUOTAOFF:
2157 case Q_XQUOTAON:
2158 case Q_XSETQLIM:
David Howells88e67f32008-11-14 10:39:21 +11002159 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAMOD, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04002160 break;
2161 case Q_GETFMT:
2162 case Q_GETINFO:
2163 case Q_GETQUOTA:
Richard Hainese4cfa052020-02-20 15:32:34 +00002164 case Q_XGETQUOTA:
2165 case Q_XGETQSTAT:
2166 case Q_XGETQSTATV:
2167 case Q_XGETNEXTQUOTA:
David Howells88e67f32008-11-14 10:39:21 +11002168 rc = superblock_has_perm(cred, sb, FILESYSTEM__QUOTAGET, NULL);
Eric Paris828dfe12008-04-17 13:17:49 -04002169 break;
2170 default:
2171 rc = 0; /* let the kernel handle invalid cmds */
2172 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 }
2174 return rc;
2175}
2176
2177static int selinux_quota_on(struct dentry *dentry)
2178{
David Howells88e67f32008-11-14 10:39:21 +11002179 const struct cred *cred = current_cred();
2180
Eric Paris2875fa02011-04-28 16:04:24 -04002181 return dentry_has_perm(cred, dentry, FILE__QUOTAON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182}
2183
Eric Paris12b30522010-11-15 18:36:29 -05002184static int selinux_syslog(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 switch (type) {
Kees Cookd78ca3c2010-02-03 15:37:13 -08002187 case SYSLOG_ACTION_READ_ALL: /* Read last kernel messages */
2188 case SYSLOG_ACTION_SIZE_BUFFER: /* Return size of the log buffer */
Stephen Smalleye67b7982023-03-09 13:30:37 -05002189 return avc_has_perm(current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002190 SECCLASS_SYSTEM, SYSTEM__SYSLOG_READ, NULL);
Kees Cookd78ca3c2010-02-03 15:37:13 -08002191 case SYSLOG_ACTION_CONSOLE_OFF: /* Disable logging to console */
2192 case SYSLOG_ACTION_CONSOLE_ON: /* Enable logging to console */
2193 /* Set level of messages printed to console */
2194 case SYSLOG_ACTION_CONSOLE_LEVEL:
Stephen Smalleye67b7982023-03-09 13:30:37 -05002195 return avc_has_perm(current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002196 SECCLASS_SYSTEM, SYSTEM__SYSLOG_CONSOLE,
2197 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 }
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002199 /* All other syslog types */
Stephen Smalleye67b7982023-03-09 13:30:37 -05002200 return avc_has_perm(current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002201 SECCLASS_SYSTEM, SYSTEM__SYSLOG_MOD, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202}
2203
2204/*
2205 * Check that a process has enough memory to allocate a new virtual
2206 * mapping. 0 means there is enough memory for the allocation to
2207 * succeed and -ENOMEM implies there is not.
2208 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 * Do not audit the selinux permission check, as this is applied to all
2210 * processes that allocate mappings.
2211 */
Alan Cox34b4e4a2007-08-22 14:01:28 -07002212static int selinux_vm_enough_memory(struct mm_struct *mm, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213{
2214 int rc, cap_sys_admin = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002216 rc = cred_has_capability(current_cred(), CAP_SYS_ADMIN,
Micah Mortonc1a85a02019-01-07 16:10:53 -08002217 CAP_OPT_NOAUDIT, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 if (rc == 0)
2219 cap_sys_admin = 1;
2220
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07002221 return cap_sys_admin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222}
2223
2224/* binprm security operations */
2225
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002226static u32 ptrace_parent_sid(void)
Paul Moore0c6181c2016-03-30 21:41:21 -04002227{
2228 u32 sid = 0;
2229 struct task_struct *tracer;
2230
2231 rcu_read_lock();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002232 tracer = ptrace_parent(current);
Paul Moore0c6181c2016-03-30 21:41:21 -04002233 if (tracer)
Paul Mooreeb1231f2021-02-18 15:13:40 -05002234 sid = task_sid_obj(tracer);
Paul Moore0c6181c2016-03-30 21:41:21 -04002235 rcu_read_unlock();
2236
2237 return sid;
2238}
2239
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002240static int check_nnp_nosuid(const struct linux_binprm *bprm,
2241 const struct task_security_struct *old_tsec,
2242 const struct task_security_struct *new_tsec)
2243{
2244 int nnp = (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS);
Andy Lutomirski380cf5b2016-06-23 16:41:05 -05002245 int nosuid = !mnt_may_suid(bprm->file->f_path.mnt);
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002246 int rc;
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002247 u32 av;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002248
2249 if (!nnp && !nosuid)
2250 return 0; /* neither NNP nor nosuid */
2251
2252 if (new_tsec->sid == old_tsec->sid)
2253 return 0; /* No change in credentials */
2254
2255 /*
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002256 * If the policy enables the nnp_nosuid_transition policy capability,
2257 * then we permit transitions under NNP or nosuid if the
2258 * policy allows the corresponding permission between
2259 * the old and new contexts.
2260 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002261 if (selinux_policycap_nnp_nosuid_transition()) {
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002262 av = 0;
2263 if (nnp)
2264 av |= PROCESS2__NNP_TRANSITION;
2265 if (nosuid)
2266 av |= PROCESS2__NOSUID_TRANSITION;
Stephen Smalleye67b7982023-03-09 13:30:37 -05002267 rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002268 SECCLASS_PROCESS2, av, NULL);
2269 if (!rc)
2270 return 0;
2271 }
2272
2273 /*
2274 * We also permit NNP or nosuid transitions to bounded SIDs,
2275 * i.e. SIDs that are guaranteed to only be allowed a subset
2276 * of the permissions of the current SID.
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002277 */
Stephen Smalleye67b7982023-03-09 13:30:37 -05002278 rc = security_bounded_transition(old_tsec->sid,
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002279 new_tsec->sid);
Stephen Smalleyaf63f412017-07-31 10:12:46 -04002280 if (!rc)
2281 return 0;
2282
2283 /*
2284 * On failure, preserve the errno values for NNP vs nosuid.
2285 * NNP: Operation not permitted for caller.
2286 * nosuid: Permission denied to file.
2287 */
2288 if (nnp)
2289 return -EPERM;
2290 return -EACCES;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002291}
2292
Eric W. Biedermanb8bff592020-03-22 15:46:24 -05002293static int selinux_bprm_creds_for_exec(struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294{
David Howellsa6f76f22008-11-14 10:39:24 +11002295 const struct task_security_struct *old_tsec;
2296 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 struct inode_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04002298 struct common_audit_data ad;
Al Viro496ad9a2013-01-23 17:07:38 -05002299 struct inode *inode = file_inode(bprm->file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 int rc;
2301
David Howellsa6f76f22008-11-14 10:39:24 +11002302 /* SELinux context only depends on initial program or script and not
2303 * the script interpreter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002305 old_tsec = selinux_cred(current_cred());
2306 new_tsec = selinux_cred(bprm->cred);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05002307 isec = inode_security(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
2309 /* Default to the current task SID. */
David Howellsa6f76f22008-11-14 10:39:24 +11002310 new_tsec->sid = old_tsec->sid;
2311 new_tsec->osid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
Michael LeMay28eba5b2006-06-27 02:53:42 -07002313 /* Reset fs, key, and sock SIDs on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002314 new_tsec->create_sid = 0;
2315 new_tsec->keycreate_sid = 0;
2316 new_tsec->sockcreate_sid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
Ondrej Mosnacekae254852023-11-14 16:51:16 +01002318 /*
2319 * Before policy is loaded, label any task outside kernel space
2320 * as SECINITSID_INIT, so that any userspace tasks surviving from
2321 * early boot end up with a label different from SECINITSID_KERNEL
2322 * (if the policy chooses to set SECINITSID_INIT != SECINITSID_KERNEL).
2323 */
2324 if (!selinux_initialized()) {
2325 new_tsec->sid = SECINITSID_INIT;
2326 /* also clear the exec_sid just in case */
2327 new_tsec->exec_sid = 0;
2328 return 0;
2329 }
2330
David Howellsa6f76f22008-11-14 10:39:24 +11002331 if (old_tsec->exec_sid) {
2332 new_tsec->sid = old_tsec->exec_sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 /* Reset exec SID on execve. */
David Howellsa6f76f22008-11-14 10:39:24 +11002334 new_tsec->exec_sid = 0;
Andy Lutomirski259e5e62012-04-12 16:47:50 -05002335
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002336 /* Fail on NNP or nosuid if not an allowed transition. */
2337 rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
2338 if (rc)
2339 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 } else {
2341 /* Check for a default transition on this program. */
Stephen Smalleye67b7982023-03-09 13:30:37 -05002342 rc = security_transition_sid(old_tsec->sid,
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002343 isec->sid, SECCLASS_PROCESS, NULL,
Eric Paris652bb9b2011-02-01 11:05:40 -05002344 &new_tsec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 if (rc)
2346 return rc;
Stephen Smalley7b0d0b42014-08-04 13:36:49 -04002347
2348 /*
2349 * Fallback to old SID on NNP or nosuid if not an allowed
2350 * transition.
2351 */
2352 rc = check_nnp_nosuid(bprm, old_tsec, new_tsec);
2353 if (rc)
2354 new_tsec->sid = old_tsec->sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 }
2356
Vivek Goyal43af5de2016-09-09 11:37:49 -04002357 ad.type = LSM_AUDIT_DATA_FILE;
2358 ad.u.file = bprm->file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
David Howellsa6f76f22008-11-14 10:39:24 +11002360 if (new_tsec->sid == old_tsec->sid) {
Stephen Smalleye67b7982023-03-09 13:30:37 -05002361 rc = avc_has_perm(old_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 SECCLASS_FILE, FILE__EXECUTE_NO_TRANS, &ad);
2363 if (rc)
2364 return rc;
2365 } else {
2366 /* Check permissions for the transition. */
Stephen Smalleye67b7982023-03-09 13:30:37 -05002367 rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 SECCLASS_PROCESS, PROCESS__TRANSITION, &ad);
2369 if (rc)
2370 return rc;
2371
Stephen Smalleye67b7982023-03-09 13:30:37 -05002372 rc = avc_has_perm(new_tsec->sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 SECCLASS_FILE, FILE__ENTRYPOINT, &ad);
2374 if (rc)
2375 return rc;
2376
David Howellsa6f76f22008-11-14 10:39:24 +11002377 /* Check for shared state */
2378 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
Stephen Smalleye67b7982023-03-09 13:30:37 -05002379 rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
David Howellsa6f76f22008-11-14 10:39:24 +11002380 SECCLASS_PROCESS, PROCESS__SHARE,
2381 NULL);
2382 if (rc)
2383 return -EPERM;
2384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
David Howellsa6f76f22008-11-14 10:39:24 +11002386 /* Make sure that anyone attempting to ptrace over a task that
2387 * changes its SID has the appropriate permit */
Eric W. Biederman9227dd22017-01-23 17:26:31 +13002388 if (bprm->unsafe & LSM_UNSAFE_PTRACE) {
Stephen Smalleybe0554c2017-01-09 10:07:31 -05002389 u32 ptsid = ptrace_parent_sid();
David Howellsa6f76f22008-11-14 10:39:24 +11002390 if (ptsid != 0) {
Stephen Smalleye67b7982023-03-09 13:30:37 -05002391 rc = avc_has_perm(ptsid, new_tsec->sid,
David Howellsa6f76f22008-11-14 10:39:24 +11002392 SECCLASS_PROCESS,
2393 PROCESS__PTRACE, NULL);
2394 if (rc)
2395 return -EPERM;
2396 }
2397 }
2398
2399 /* Clear any possibly unsafe personality bits on exec: */
2400 bprm->per_clear |= PER_CLEAR_ON_SETID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 /* Enable secure mode for SIDs transitions unless
2403 the noatsecure permission is granted between
2404 the two SIDs, i.e. ahp returns 0. */
Stephen Smalleye67b7982023-03-09 13:30:37 -05002405 rc = avc_has_perm(old_tsec->sid, new_tsec->sid,
Kees Cook62874c32017-07-18 15:25:25 -07002406 SECCLASS_PROCESS, PROCESS__NOATSECURE,
2407 NULL);
2408 bprm->secureexec |= !!rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 }
2410
Kees Cook62874c32017-07-18 15:25:25 -07002411 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412}
2413
Al Viroc3c073f2012-08-21 22:32:06 -04002414static int match_file(const void *p, struct file *file, unsigned fd)
2415{
2416 return file_has_perm(p, file, file_to_av(file)) ? fd + 1 : 0;
2417}
2418
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419/* Derived from fs/exec.c:flush_old_files. */
David Howells745ca242008-11-14 10:39:22 +11002420static inline void flush_unauthorized_files(const struct cred *cred,
2421 struct files_struct *files)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 struct file *file, *devnull = NULL;
Stephen Smalleyb20c8122006-09-25 23:32:03 -07002424 struct tty_struct *tty;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002425 int drop_tty = 0;
Al Viroc3c073f2012-08-21 22:32:06 -04002426 unsigned n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002428 tty = get_current_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 if (tty) {
Peter Hurley4a510962016-01-09 21:35:23 -08002430 spin_lock(&tty->files_lock);
Eric Paris37dd0bd2008-10-31 17:40:00 -04002431 if (!list_empty(&tty->tty_files)) {
Nick Piggind996b622010-08-18 04:37:36 +10002432 struct tty_file_private *file_priv;
Eric Paris37dd0bd2008-10-31 17:40:00 -04002433
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 /* Revalidate access to controlling tty.
David Howells13f8e982013-06-13 23:37:55 +01002435 Use file_path_has_perm on the tty path directly
2436 rather than using file_has_perm, as this particular
2437 open file may belong to another process and we are
2438 only interested in the inode-based check here. */
Nick Piggind996b622010-08-18 04:37:36 +10002439 file_priv = list_first_entry(&tty->tty_files,
2440 struct tty_file_private, list);
2441 file = file_priv->file;
David Howells13f8e982013-06-13 23:37:55 +01002442 if (file_path_has_perm(cred, file, FILE__READ | FILE__WRITE))
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002443 drop_tty = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 }
Peter Hurley4a510962016-01-09 21:35:23 -08002445 spin_unlock(&tty->files_lock);
Alan Cox452a00d2008-10-13 10:39:13 +01002446 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 }
Eric W. Biederman98a27ba2007-05-08 00:26:56 -07002448 /* Reset controlling tty. */
2449 if (drop_tty)
2450 no_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451
2452 /* Revalidate access to inherited open files. */
Al Viroc3c073f2012-08-21 22:32:06 -04002453 n = iterate_fd(files, 0, match_file, cred);
2454 if (!n) /* none found? */
2455 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
Al Viroc3c073f2012-08-21 22:32:06 -04002457 devnull = dentry_open(&selinux_null, O_RDWR, cred);
Al Viro45525b22012-10-16 13:30:07 -04002458 if (IS_ERR(devnull))
2459 devnull = NULL;
2460 /* replace all the matching ones with this */
2461 do {
2462 replace_fd(n - 1, devnull, 0);
2463 } while ((n = iterate_fd(files, n, match_file, cred)) != 0);
2464 if (devnull)
Al Viroc3c073f2012-08-21 22:32:06 -04002465 fput(devnull);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466}
2467
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468/*
David Howellsa6f76f22008-11-14 10:39:24 +11002469 * Prepare a process for imminent new credential changes due to exec
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 */
Khadija Kamran64fc9522023-08-23 12:47:56 +05002471static void selinux_bprm_committing_creds(const struct linux_binprm *bprm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472{
David Howellsa6f76f22008-11-14 10:39:24 +11002473 struct task_security_struct *new_tsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 struct rlimit *rlim, *initrlim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 int rc, i;
2476
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002477 new_tsec = selinux_cred(bprm->cred);
David Howellsa6f76f22008-11-14 10:39:24 +11002478 if (new_tsec->sid == new_tsec->osid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 return;
2480
2481 /* Close files for which the new task SID is not authorized. */
David Howellsa6f76f22008-11-14 10:39:24 +11002482 flush_unauthorized_files(bprm->cred, current->files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
David Howellsa6f76f22008-11-14 10:39:24 +11002484 /* Always clear parent death signal on SID transitions. */
2485 current->pdeath_signal = 0;
2486
2487 /* Check whether the new SID can inherit resource limits from the old
2488 * SID. If not, reset all soft limits to the lower of the current
2489 * task's hard limit and the init task's soft limit.
2490 *
2491 * Note that the setting of hard limits (even to lower them) can be
2492 * controlled by the setrlimit check. The inclusion of the init task's
2493 * soft limit into the computation is to avoid resetting soft limits
2494 * higher than the default soft limit for cases where the default is
2495 * lower than the hard limit, e.g. RLIMIT_CORE or RLIMIT_STACK.
2496 */
Stephen Smalleye67b7982023-03-09 13:30:37 -05002497 rc = avc_has_perm(new_tsec->osid, new_tsec->sid, SECCLASS_PROCESS,
David Howellsa6f76f22008-11-14 10:39:24 +11002498 PROCESS__RLIMITINH, NULL);
2499 if (rc) {
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002500 /* protect against do_prlimit() */
2501 task_lock(current);
David Howellsa6f76f22008-11-14 10:39:24 +11002502 for (i = 0; i < RLIM_NLIMITS; i++) {
2503 rlim = current->signal->rlim + i;
2504 initrlim = init_task.signal->rlim + i;
2505 rlim->rlim_cur = min(rlim->rlim_max, initrlim->rlim_cur);
2506 }
Oleg Nesteroveb2d55a2010-06-23 22:43:32 +02002507 task_unlock(current);
Nicolas Pitrebaa73d92016-11-11 00:10:10 -05002508 if (IS_ENABLED(CONFIG_POSIX_TIMERS))
2509 update_rlimit_cpu(current, rlimit(RLIMIT_CPU));
David Howellsa6f76f22008-11-14 10:39:24 +11002510 }
2511}
2512
2513/*
2514 * Clean up the process immediately after the installation of new credentials
2515 * due to exec
2516 */
Khadija Kamrana721f7b2023-08-23 13:16:40 +05002517static void selinux_bprm_committed_creds(const struct linux_binprm *bprm)
David Howellsa6f76f22008-11-14 10:39:24 +11002518{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002519 const struct task_security_struct *tsec = selinux_cred(current_cred());
David Howellsa6f76f22008-11-14 10:39:24 +11002520 u32 osid, sid;
Arnd Bergmannddbc7d02019-10-25 21:37:43 +02002521 int rc;
David Howellsa6f76f22008-11-14 10:39:24 +11002522
David Howellsa6f76f22008-11-14 10:39:24 +11002523 osid = tsec->osid;
2524 sid = tsec->sid;
2525
2526 if (sid == osid)
2527 return;
2528
2529 /* Check whether the new SID can inherit signal state from the old SID.
2530 * If not, clear itimers to avoid subsequent signal generation and
2531 * flush and unblock signals.
2532 *
2533 * This must occur _after_ the task SID has been updated so that any
2534 * kill done after the flush will be checked against the new SID.
2535 */
Stephen Smalleye67b7982023-03-09 13:30:37 -05002536 rc = avc_has_perm(osid, sid, SECCLASS_PROCESS, PROCESS__SIGINH, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 if (rc) {
Arnd Bergmannddbc7d02019-10-25 21:37:43 +02002538 clear_itimer();
2539
Paul Moore0e326df2022-01-27 10:56:13 -05002540 spin_lock_irq(&unrcu_pointer(current->sighand)->siglock);
Oleg Nesterov9e7c8f82015-06-04 16:22:16 -04002541 if (!fatal_signal_pending(current)) {
2542 flush_sigqueue(&current->pending);
2543 flush_sigqueue(&current->signal->shared_pending);
David Howells3bcac022009-04-29 13:45:05 +01002544 flush_signal_handlers(current, 1);
2545 sigemptyset(&current->blocked);
Oleg Nesterov9e7c8f82015-06-04 16:22:16 -04002546 recalc_sigpending();
David Howells3bcac022009-04-29 13:45:05 +01002547 }
Paul Moore0e326df2022-01-27 10:56:13 -05002548 spin_unlock_irq(&unrcu_pointer(current->sighand)->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 }
2550
David Howellsa6f76f22008-11-14 10:39:24 +11002551 /* Wake up the parent if it is waiting so that it can recheck
2552 * wait permission to the new task SID. */
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002553 read_lock(&tasklist_lock);
Paul Moore0e326df2022-01-27 10:56:13 -05002554 __wake_up_parent(current, unrcu_pointer(current->real_parent));
Oleg Nesterovecd6de32009-04-29 16:02:24 +02002555 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556}
2557
2558/* superblock security operations */
2559
2560static int selinux_sb_alloc_security(struct super_block *sb)
2561{
Casey Schaufler1aea7802021-04-22 17:41:15 +02002562 struct superblock_security_struct *sbsec = selinux_superblock(sb);
Paul Moorecb89e242020-01-10 16:32:10 -05002563
2564 mutex_init(&sbsec->lock);
2565 INIT_LIST_HEAD(&sbsec->isec_head);
2566 spin_lock_init(&sbsec->isec_lock);
Paul Moorecb89e242020-01-10 16:32:10 -05002567 sbsec->sid = SECINITSID_UNLABELED;
2568 sbsec->def_sid = SECINITSID_FILE;
2569 sbsec->mntpoint_sid = SECINITSID_UNLABELED;
Paul Moorecb89e242020-01-10 16:32:10 -05002570
2571 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572}
2573
Al Viro99dbbb52018-12-14 21:56:23 -05002574static inline int opt_len(const char *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575{
Al Viro99dbbb52018-12-14 21:56:23 -05002576 bool open_quote = false;
2577 int len;
2578 char c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579
Al Viro99dbbb52018-12-14 21:56:23 -05002580 for (len = 0; (c = s[len]) != '\0'; len++) {
2581 if (c == '"')
Cory Olmo3528a952006-09-29 01:58:44 -07002582 open_quote = !open_quote;
Al Viro99dbbb52018-12-14 21:56:23 -05002583 if (c == ',' && !open_quote)
2584 break;
2585 }
2586 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587}
2588
Al Viro204cc0c2018-12-13 13:41:47 -05002589static int selinux_sb_eat_lsm_opts(char *options, void **mnt_opts)
Eric Paris026eb162011-03-03 16:09:14 -05002590{
Al Viro99dbbb52018-12-14 21:56:23 -05002591 char *from = options;
2592 char *to = options;
2593 bool first = true;
Gen Zhangfec63752019-06-12 21:55:38 +08002594 int rc;
Al Viro5b400232018-12-12 20:13:29 -05002595
Al Viro99dbbb52018-12-14 21:56:23 -05002596 while (1) {
2597 int len = opt_len(from);
Gen Zhangfec63752019-06-12 21:55:38 +08002598 int token;
Al Viro99dbbb52018-12-14 21:56:23 -05002599 char *arg = NULL;
2600
2601 token = match_opt_prefix(from, len, &arg);
2602
2603 if (token != Opt_error) {
2604 char *p, *q;
2605
2606 /* strip quotes */
2607 if (arg) {
2608 for (p = q = arg; p < from + len; p++) {
2609 char c = *p;
2610 if (c != '"')
2611 *q++ = c;
2612 }
2613 arg = kmemdup_nul(arg, q - arg, GFP_KERNEL);
Gen Zhangfec63752019-06-12 21:55:38 +08002614 if (!arg) {
2615 rc = -ENOMEM;
2616 goto free_opt;
2617 }
Al Viro99dbbb52018-12-14 21:56:23 -05002618 }
2619 rc = selinux_add_opt(token, arg, mnt_opts);
Christian Göttschecad140d2022-06-15 17:38:39 +02002620 kfree(arg);
2621 arg = NULL;
Al Viro99dbbb52018-12-14 21:56:23 -05002622 if (unlikely(rc)) {
Gen Zhangfec63752019-06-12 21:55:38 +08002623 goto free_opt;
Al Viro99dbbb52018-12-14 21:56:23 -05002624 }
2625 } else {
2626 if (!first) { // copy with preceding comma
2627 from--;
2628 len++;
2629 }
2630 if (to != from)
2631 memmove(to, from, len);
2632 to += len;
2633 first = false;
2634 }
2635 if (!from[len])
2636 break;
2637 from += len + 1;
2638 }
2639 *to = '\0';
2640 return 0;
Gen Zhangfec63752019-06-12 21:55:38 +08002641
2642free_opt:
2643 if (*mnt_opts) {
2644 selinux_free_mnt_opts(*mnt_opts);
2645 *mnt_opts = NULL;
2646 }
2647 return rc;
Al Viro5b400232018-12-12 20:13:29 -05002648}
2649
Olga Kornievskaia69c4a422021-02-26 22:37:55 -05002650static int selinux_sb_mnt_opts_compat(struct super_block *sb, void *mnt_opts)
2651{
2652 struct selinux_mnt_opts *opts = mnt_opts;
GONG, Ruiqi0266c252022-01-25 15:11:33 +08002653 struct superblock_security_struct *sbsec = selinux_superblock(sb);
Olga Kornievskaia69c4a422021-02-26 22:37:55 -05002654
2655 /*
2656 * Superblock not initialized (i.e. no options) - reject if any
2657 * options specified, otherwise accept.
2658 */
2659 if (!(sbsec->flags & SE_SBINITIALIZED))
2660 return opts ? 1 : 0;
2661
2662 /*
2663 * Superblock initialized and no options specified - reject if
2664 * superblock has any options set, otherwise accept.
2665 */
2666 if (!opts)
2667 return (sbsec->flags & SE_MNTMASK) ? 1 : 0;
2668
Ondrej Mosnacek70f41692022-02-02 13:55:29 +01002669 if (opts->fscontext_sid) {
2670 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
2671 opts->fscontext_sid))
Olga Kornievskaia69c4a422021-02-26 22:37:55 -05002672 return 1;
2673 }
Ondrej Mosnacek70f41692022-02-02 13:55:29 +01002674 if (opts->context_sid) {
2675 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
2676 opts->context_sid))
Olga Kornievskaia69c4a422021-02-26 22:37:55 -05002677 return 1;
2678 }
Ondrej Mosnacek70f41692022-02-02 13:55:29 +01002679 if (opts->rootcontext_sid) {
2680 struct inode_security_struct *root_isec;
Olga Kornievskaia69c4a422021-02-26 22:37:55 -05002681
Ondrej Mosnacek70f41692022-02-02 13:55:29 +01002682 root_isec = backing_inode_security(sb->s_root);
2683 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
2684 opts->rootcontext_sid))
Olga Kornievskaia69c4a422021-02-26 22:37:55 -05002685 return 1;
Ondrej Mosnacek70f41692022-02-02 13:55:29 +01002686 }
2687 if (opts->defcontext_sid) {
2688 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
2689 opts->defcontext_sid))
Olga Kornievskaia69c4a422021-02-26 22:37:55 -05002690 return 1;
2691 }
2692 return 0;
2693}
2694
Al Viro204cc0c2018-12-13 13:41:47 -05002695static int selinux_sb_remount(struct super_block *sb, void *mnt_opts)
Eric Paris026eb162011-03-03 16:09:14 -05002696{
Al Virobd323652018-12-13 15:04:59 -05002697 struct selinux_mnt_opts *opts = mnt_opts;
Casey Schaufler1aea7802021-04-22 17:41:15 +02002698 struct superblock_security_struct *sbsec = selinux_superblock(sb);
Eric Paris026eb162011-03-03 16:09:14 -05002699
2700 if (!(sbsec->flags & SE_SBINITIALIZED))
2701 return 0;
2702
Al Viro204cc0c2018-12-13 13:41:47 -05002703 if (!opts)
Eric Paris026eb162011-03-03 16:09:14 -05002704 return 0;
2705
Ondrej Mosnacek70f41692022-02-02 13:55:29 +01002706 if (opts->fscontext_sid) {
Scott Mayhew6bc19682022-01-31 13:57:37 -05002707 if (bad_option(sbsec, FSCONTEXT_MNT, sbsec->sid,
2708 opts->fscontext_sid))
Al Virobd323652018-12-13 15:04:59 -05002709 goto out_bad_option;
Eric Paris026eb162011-03-03 16:09:14 -05002710 }
Ondrej Mosnacek70f41692022-02-02 13:55:29 +01002711 if (opts->context_sid) {
Scott Mayhew6bc19682022-01-31 13:57:37 -05002712 if (bad_option(sbsec, CONTEXT_MNT, sbsec->mntpoint_sid,
2713 opts->context_sid))
Al Virobd323652018-12-13 15:04:59 -05002714 goto out_bad_option;
2715 }
Ondrej Mosnacek70f41692022-02-02 13:55:29 +01002716 if (opts->rootcontext_sid) {
Al Virobd323652018-12-13 15:04:59 -05002717 struct inode_security_struct *root_isec;
2718 root_isec = backing_inode_security(sb->s_root);
Scott Mayhew6bc19682022-01-31 13:57:37 -05002719 if (bad_option(sbsec, ROOTCONTEXT_MNT, root_isec->sid,
2720 opts->rootcontext_sid))
Al Virobd323652018-12-13 15:04:59 -05002721 goto out_bad_option;
2722 }
Ondrej Mosnacek70f41692022-02-02 13:55:29 +01002723 if (opts->defcontext_sid) {
Scott Mayhew6bc19682022-01-31 13:57:37 -05002724 if (bad_option(sbsec, DEFCONTEXT_MNT, sbsec->def_sid,
2725 opts->defcontext_sid))
Al Virobd323652018-12-13 15:04:59 -05002726 goto out_bad_option;
Eric Paris026eb162011-03-03 16:09:14 -05002727 }
Al Viroc039bc32018-12-01 23:06:57 -05002728 return 0;
Eric Paris026eb162011-03-03 16:09:14 -05002729
Eric Paris026eb162011-03-03 16:09:14 -05002730out_bad_option:
peter enderborgc103a912018-06-12 10:09:03 +02002731 pr_warn("SELinux: unable to change security options "
Linus Torvalds29b1deb2013-12-15 11:17:45 -08002732 "during remount (dev %s, type=%s)\n", sb->s_id,
2733 sb->s_type->name);
Al Viroc039bc32018-12-01 23:06:57 -05002734 return -EINVAL;
Eric Paris026eb162011-03-03 16:09:14 -05002735}
2736
Khadija Kamran20a2aa42023-08-23 14:01:28 +05002737static int selinux_sb_kern_mount(const struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738{
David Howells88e67f32008-11-14 10:39:21 +11002739 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002740 struct common_audit_data ad;
James Morris74192242008-12-19 11:41:10 +11002741
Eric Paris50c205f2012-04-04 15:01:43 -04002742 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002743 ad.u.dentry = sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002744 return superblock_has_perm(cred, sb, FILESYSTEM__MOUNT, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745}
2746
David Howells726c3342006-06-23 02:02:58 -07002747static int selinux_sb_statfs(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748{
David Howells88e67f32008-11-14 10:39:21 +11002749 const struct cred *cred = current_cred();
Thomas Liu2bf49692009-07-14 12:14:09 -04002750 struct common_audit_data ad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751
Eric Paris50c205f2012-04-04 15:01:43 -04002752 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04002753 ad.u.dentry = dentry->d_sb->s_root;
David Howells88e67f32008-11-14 10:39:21 +11002754 return superblock_has_perm(cred, dentry->d_sb, FILESYSTEM__GETATTR, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755}
2756
Al Viro808d4e32012-10-11 11:42:01 -04002757static int selinux_mount(const char *dev_name,
Al Viro8a04c432016-03-25 14:52:53 -04002758 const struct path *path,
Al Viro808d4e32012-10-11 11:42:01 -04002759 const char *type,
Eric Paris828dfe12008-04-17 13:17:49 -04002760 unsigned long flags,
2761 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762{
David Howells88e67f32008-11-14 10:39:21 +11002763 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764
2765 if (flags & MS_REMOUNT)
Al Virod8c95842011-12-07 18:16:57 -05002766 return superblock_has_perm(cred, path->dentry->d_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002767 FILESYSTEM__REMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 else
Eric Paris2875fa02011-04-28 16:04:24 -04002769 return path_has_perm(cred, path, FILE__MOUNTON);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770}
2771
Stephen Smalley98aa003452020-01-17 15:24:07 -05002772static int selinux_move_mount(const struct path *from_path,
2773 const struct path *to_path)
2774{
2775 const struct cred *cred = current_cred();
2776
2777 return path_has_perm(cred, to_path, FILE__MOUNTON);
2778}
2779
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780static int selinux_umount(struct vfsmount *mnt, int flags)
2781{
David Howells88e67f32008-11-14 10:39:21 +11002782 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783
David Howells88e67f32008-11-14 10:39:21 +11002784 return superblock_has_perm(cred, mnt->mnt_sb,
Eric Paris828dfe12008-04-17 13:17:49 -04002785 FILESYSTEM__UNMOUNT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786}
2787
David Howellsd80a8f12023-08-08 07:34:20 -04002788static int selinux_fs_context_submount(struct fs_context *fc,
2789 struct super_block *reference)
2790{
Ondrej Mosnacekccf1dab2023-09-11 16:23:58 +02002791 const struct superblock_security_struct *sbsec = selinux_superblock(reference);
David Howellsd80a8f12023-08-08 07:34:20 -04002792 struct selinux_mnt_opts *opts;
2793
Ondrej Mosnacekccf1dab2023-09-11 16:23:58 +02002794 /*
2795 * Ensure that fc->security remains NULL when no options are set
2796 * as expected by selinux_set_mnt_opts().
2797 */
2798 if (!(sbsec->flags & (FSCONTEXT_MNT|CONTEXT_MNT|DEFCONTEXT_MNT)))
2799 return 0;
2800
David Howellsd80a8f12023-08-08 07:34:20 -04002801 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
2802 if (!opts)
2803 return -ENOMEM;
2804
David Howellsd80a8f12023-08-08 07:34:20 -04002805 if (sbsec->flags & FSCONTEXT_MNT)
2806 opts->fscontext_sid = sbsec->sid;
2807 if (sbsec->flags & CONTEXT_MNT)
2808 opts->context_sid = sbsec->mntpoint_sid;
2809 if (sbsec->flags & DEFCONTEXT_MNT)
2810 opts->defcontext_sid = sbsec->def_sid;
2811 fc->security = opts;
2812 return 0;
2813}
2814
Al Viro0b520752018-12-23 16:02:47 -05002815static int selinux_fs_context_dup(struct fs_context *fc,
2816 struct fs_context *src_fc)
2817{
2818 const struct selinux_mnt_opts *src = src_fc->security;
Al Viro0b520752018-12-23 16:02:47 -05002819
2820 if (!src)
2821 return 0;
2822
Ondrej Mosnacek70f41692022-02-02 13:55:29 +01002823 fc->security = kmemdup(src, sizeof(*src), GFP_KERNEL);
2824 return fc->security ? 0 : -ENOMEM;
Al Viro0b520752018-12-23 16:02:47 -05002825}
2826
Al Virod7167b12019-09-07 07:23:15 -04002827static const struct fs_parameter_spec selinux_fs_parameters[] = {
David Howells442155c2018-11-01 23:07:24 +00002828 fsparam_string(CONTEXT_STR, Opt_context),
2829 fsparam_string(DEFCONTEXT_STR, Opt_defcontext),
2830 fsparam_string(FSCONTEXT_STR, Opt_fscontext),
2831 fsparam_string(ROOTCONTEXT_STR, Opt_rootcontext),
2832 fsparam_flag (SECLABEL_STR, Opt_seclabel),
2833 {}
2834};
2835
David Howells442155c2018-11-01 23:07:24 +00002836static int selinux_fs_context_parse_param(struct fs_context *fc,
2837 struct fs_parameter *param)
2838{
2839 struct fs_parse_result result;
Christian Göttschecad140d2022-06-15 17:38:39 +02002840 int opt;
David Howells442155c2018-11-01 23:07:24 +00002841
Al Virod7167b12019-09-07 07:23:15 -04002842 opt = fs_parse(fc, selinux_fs_parameters, param, &result);
David Howells442155c2018-11-01 23:07:24 +00002843 if (opt < 0)
2844 return opt;
2845
Christian Göttschecad140d2022-06-15 17:38:39 +02002846 return selinux_add_opt(opt, param->string, &fc->security);
David Howells442155c2018-11-01 23:07:24 +00002847}
2848
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849/* inode security operations */
2850
2851static int selinux_inode_alloc_security(struct inode *inode)
2852{
Paul Moorecb89e242020-01-10 16:32:10 -05002853 struct inode_security_struct *isec = selinux_inode(inode);
2854 u32 sid = current_sid();
2855
2856 spin_lock_init(&isec->lock);
2857 INIT_LIST_HEAD(&isec->list);
2858 isec->inode = inode;
2859 isec->sid = SECINITSID_UNLABELED;
2860 isec->sclass = SECCLASS_FILE;
2861 isec->task_sid = sid;
2862 isec->initialized = LABEL_INVALID;
2863
2864 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865}
2866
2867static void selinux_inode_free_security(struct inode *inode)
2868{
2869 inode_free_security(inode);
2870}
2871
David Quigleyd47be3d2013-05-22 12:50:34 -04002872static int selinux_dentry_init_security(struct dentry *dentry, int mode,
Vivek Goyal15bf3232021-10-12 09:23:07 -04002873 const struct qstr *name,
2874 const char **xattr_name, void **ctx,
David Quigleyd47be3d2013-05-22 12:50:34 -04002875 u32 *ctxlen)
2876{
David Quigleyd47be3d2013-05-22 12:50:34 -04002877 u32 newsid;
2878 int rc;
2879
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002880 rc = selinux_determine_inode_label(selinux_cred(current_cred()),
Vivek Goyalc957f6d2016-07-13 10:44:51 -04002881 d_inode(dentry->d_parent), name,
David Howellsc3c188b2015-07-10 17:19:58 -04002882 inode_mode_to_security_class(mode),
2883 &newsid);
2884 if (rc)
2885 return rc;
David Quigleyd47be3d2013-05-22 12:50:34 -04002886
Vivek Goyal15bf3232021-10-12 09:23:07 -04002887 if (xattr_name)
2888 *xattr_name = XATTR_NAME_SELINUX;
2889
Stephen Smalleye67b7982023-03-09 13:30:37 -05002890 return security_sid_to_context(newsid, (char **)ctx,
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002891 ctxlen);
David Quigleyd47be3d2013-05-22 12:50:34 -04002892}
2893
Vivek Goyala518b0a2016-07-13 10:44:53 -04002894static int selinux_dentry_create_files_as(struct dentry *dentry, int mode,
2895 struct qstr *name,
2896 const struct cred *old,
2897 struct cred *new)
2898{
2899 u32 newsid;
2900 int rc;
2901 struct task_security_struct *tsec;
2902
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002903 rc = selinux_determine_inode_label(selinux_cred(old),
Vivek Goyala518b0a2016-07-13 10:44:53 -04002904 d_inode(dentry->d_parent), name,
2905 inode_mode_to_security_class(mode),
2906 &newsid);
2907 if (rc)
2908 return rc;
2909
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002910 tsec = selinux_cred(new);
Vivek Goyala518b0a2016-07-13 10:44:53 -04002911 tsec->create_sid = newsid;
2912 return 0;
2913}
2914
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002915static int selinux_inode_init_security(struct inode *inode, struct inode *dir,
Tetsuo Handa95489062013-07-25 05:44:02 +09002916 const struct qstr *qstr,
Roberto Sassu6bcdfd22023-06-10 09:57:35 +02002917 struct xattr *xattrs, int *xattr_count)
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002918{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07002919 const struct task_security_struct *tsec = selinux_cred(current_cred());
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002920 struct superblock_security_struct *sbsec;
Roberto Sassu6bcdfd22023-06-10 09:57:35 +02002921 struct xattr *xattr = lsm_get_xattr_slot(xattrs, xattr_count);
Corentin LABBEc0d4f462017-10-04 20:32:17 +02002922 u32 newsid, clen;
Paul Moore90593ca2024-01-18 20:44:21 -05002923 u16 newsclass;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002924 int rc;
Tetsuo Handa95489062013-07-25 05:44:02 +09002925 char *context;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002926
Casey Schaufler1aea7802021-04-22 17:41:15 +02002927 sbsec = selinux_superblock(dir->i_sb);
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002928
David Howells275bb412008-11-14 10:39:19 +11002929 newsid = tsec->create_sid;
Paul Moore90593ca2024-01-18 20:44:21 -05002930 newsclass = inode_mode_to_security_class(inode->i_mode);
2931 rc = selinux_determine_inode_label(tsec, dir, qstr, newsclass, &newsid);
David Howellsc3c188b2015-07-10 17:19:58 -04002932 if (rc)
2933 return rc;
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002934
Eric Paris296fddf2006-09-25 23:32:00 -07002935 /* Possibly defer initialization to selinux_complete_init. */
David P. Quigley0d90a7e2009-01-16 09:22:02 -05002936 if (sbsec->flags & SE_SBINITIALIZED) {
Casey Schaufler80788c22018-09-21 17:19:11 -07002937 struct inode_security_struct *isec = selinux_inode(inode);
Paul Moore90593ca2024-01-18 20:44:21 -05002938 isec->sclass = newsclass;
Eric Paris296fddf2006-09-25 23:32:00 -07002939 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05002940 isec->initialized = LABEL_INITIALIZED;
Eric Paris296fddf2006-09-25 23:32:00 -07002941 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002942
Stephen Smalleye67b7982023-03-09 13:30:37 -05002943 if (!selinux_initialized() ||
Ondrej Mosnacek65cddd52020-01-07 14:31:53 +01002944 !(sbsec->flags & SBLABEL_MNT))
Stephen Smalley25a74f32005-11-08 21:34:33 -08002945 return -EOPNOTSUPP;
2946
Roberto Sassu6bcdfd22023-06-10 09:57:35 +02002947 if (xattr) {
Stephen Smalleye67b7982023-03-09 13:30:37 -05002948 rc = security_sid_to_context_force(newsid,
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05002949 &context, &clen);
Tetsuo Handa95489062013-07-25 05:44:02 +09002950 if (rc)
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002951 return rc;
Roberto Sassu6bcdfd22023-06-10 09:57:35 +02002952 xattr->value = context;
2953 xattr->value_len = clen;
2954 xattr->name = XATTR_SELINUX_SUFFIX;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07002955 }
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002956
Stephen Smalley5e41ff92005-09-09 13:01:35 -07002957 return 0;
2958}
2959
Daniel Colascione29cd6592021-01-08 14:22:22 -08002960static int selinux_inode_init_security_anon(struct inode *inode,
2961 const struct qstr *name,
2962 const struct inode *context_inode)
2963{
2964 const struct task_security_struct *tsec = selinux_cred(current_cred());
2965 struct common_audit_data ad;
2966 struct inode_security_struct *isec;
2967 int rc;
2968
Stephen Smalleye67b7982023-03-09 13:30:37 -05002969 if (unlikely(!selinux_initialized()))
Daniel Colascione29cd6592021-01-08 14:22:22 -08002970 return 0;
2971
2972 isec = selinux_inode(inode);
2973
2974 /*
2975 * We only get here once per ephemeral inode. The inode has
2976 * been initialized via inode_alloc_security but is otherwise
2977 * untouched.
2978 */
2979
2980 if (context_inode) {
2981 struct inode_security_struct *context_isec =
2982 selinux_inode(context_inode);
2983 if (context_isec->initialized != LABEL_INITIALIZED) {
Christian Göttschee5faa832023-07-18 21:00:24 +02002984 pr_err("SELinux: context_inode is not initialized\n");
Daniel Colascione29cd6592021-01-08 14:22:22 -08002985 return -EACCES;
2986 }
2987
2988 isec->sclass = context_isec->sclass;
2989 isec->sid = context_isec->sid;
2990 } else {
2991 isec->sclass = SECCLASS_ANON_INODE;
2992 rc = security_transition_sid(
Stephen Smalleye67b7982023-03-09 13:30:37 -05002993 tsec->sid, tsec->sid,
Daniel Colascione29cd6592021-01-08 14:22:22 -08002994 isec->sclass, name, &isec->sid);
2995 if (rc)
2996 return rc;
2997 }
2998
2999 isec->initialized = LABEL_INITIALIZED;
3000 /*
3001 * Now that we've initialized security, check whether we're
3002 * allowed to actually create this type of anonymous inode.
3003 */
3004
Christian Göttschec29722f2022-03-08 18:09:26 +01003005 ad.type = LSM_AUDIT_DATA_ANONINODE;
3006 ad.u.anonclass = name ? (const char *)name->name : "?";
Daniel Colascione29cd6592021-01-08 14:22:22 -08003007
Stephen Smalleye67b7982023-03-09 13:30:37 -05003008 return avc_has_perm(tsec->sid,
Daniel Colascione29cd6592021-01-08 14:22:22 -08003009 isec->sid,
3010 isec->sclass,
3011 FILE__CREATE,
3012 &ad);
3013}
3014
Al Viro4acdaf22011-07-26 01:42:34 -04003015static int selinux_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016{
3017 return may_create(dir, dentry, SECCLASS_FILE);
3018}
3019
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020static int selinux_inode_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
3021{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 return may_link(dir, old_dentry, MAY_LINK);
3023}
3024
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025static int selinux_inode_unlink(struct inode *dir, struct dentry *dentry)
3026{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 return may_link(dir, dentry, MAY_UNLINK);
3028}
3029
3030static int selinux_inode_symlink(struct inode *dir, struct dentry *dentry, const char *name)
3031{
3032 return may_create(dir, dentry, SECCLASS_LNK_FILE);
3033}
3034
Al Viro18bb1db2011-07-26 01:41:39 -04003035static int selinux_inode_mkdir(struct inode *dir, struct dentry *dentry, umode_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036{
3037 return may_create(dir, dentry, SECCLASS_DIR);
3038}
3039
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040static int selinux_inode_rmdir(struct inode *dir, struct dentry *dentry)
3041{
3042 return may_link(dir, dentry, MAY_RMDIR);
3043}
3044
Al Viro1a67aaf2011-07-26 01:52:52 -04003045static int selinux_inode_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 return may_create(dir, dentry, inode_mode_to_security_class(mode));
3048}
3049
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050static int selinux_inode_rename(struct inode *old_inode, struct dentry *old_dentry,
Eric Paris828dfe12008-04-17 13:17:49 -04003051 struct inode *new_inode, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052{
3053 return may_rename(old_inode, old_dentry, new_inode, new_dentry);
3054}
3055
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056static int selinux_inode_readlink(struct dentry *dentry)
3057{
David Howells88e67f32008-11-14 10:39:21 +11003058 const struct cred *cred = current_cred();
3059
Eric Paris2875fa02011-04-28 16:04:24 -04003060 return dentry_has_perm(cred, dentry, FILE__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061}
3062
NeilBrownbda0be72015-03-23 13:37:39 +11003063static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode,
3064 bool rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065{
David Howells88e67f32008-11-14 10:39:21 +11003066 const struct cred *cred = current_cred();
NeilBrownbda0be72015-03-23 13:37:39 +11003067 struct common_audit_data ad;
3068 struct inode_security_struct *isec;
3069 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070
NeilBrownbda0be72015-03-23 13:37:39 +11003071 ad.type = LSM_AUDIT_DATA_DENTRY;
3072 ad.u.dentry = dentry;
3073 sid = cred_sid(cred);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05003074 isec = inode_security_rcu(inode, rcu);
3075 if (IS_ERR(isec))
3076 return PTR_ERR(isec);
NeilBrownbda0be72015-03-23 13:37:39 +11003077
Stephen Smalleye67b7982023-03-09 13:30:37 -05003078 return avc_has_perm(sid, isec->sid, isec->sclass, FILE__READ, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079}
3080
Eric Parisd4cf970d2012-04-04 15:01:42 -04003081static noinline int audit_inode_permission(struct inode *inode,
3082 u32 perms, u32 audited, u32 denied,
Stephen Smalley0188d5c2019-11-22 12:22:45 -05003083 int result)
Eric Parisd4cf970d2012-04-04 15:01:42 -04003084{
3085 struct common_audit_data ad;
Casey Schaufler80788c22018-09-21 17:19:11 -07003086 struct inode_security_struct *isec = selinux_inode(inode);
Eric Parisd4cf970d2012-04-04 15:01:42 -04003087
Eric Paris50c205f2012-04-04 15:01:43 -04003088 ad.type = LSM_AUDIT_DATA_INODE;
Eric Parisd4cf970d2012-04-04 15:01:42 -04003089 ad.u.inode = inode;
3090
Stephen Smalleye67b7982023-03-09 13:30:37 -05003091 return slow_avc_audit(current_sid(), isec->sid, isec->sclass, perms,
Stephen Smalley0188d5c2019-11-22 12:22:45 -05003092 audited, denied, result, &ad);
Eric Parisd4cf970d2012-04-04 15:01:42 -04003093}
3094
Al Viroe74f71e2011-06-20 19:38:15 -04003095static int selinux_inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096{
David Howells88e67f32008-11-14 10:39:21 +11003097 const struct cred *cred = current_cred();
Eric Parisb782e0a2010-07-23 11:44:03 -04003098 u32 perms;
3099 bool from_access;
Stephen Smalley5298d0b2019-11-22 16:16:56 -05003100 bool no_block = mask & MAY_NOT_BLOCK;
Eric Paris2e3340572012-04-04 15:01:42 -04003101 struct inode_security_struct *isec;
3102 u32 sid;
3103 struct av_decision avd;
3104 int rc, rc2;
3105 u32 audited, denied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106
Eric Parisb782e0a2010-07-23 11:44:03 -04003107 from_access = mask & MAY_ACCESS;
Eric Parisd09ca732010-07-23 11:43:57 -04003108 mask &= (MAY_READ|MAY_WRITE|MAY_EXEC|MAY_APPEND);
3109
Eric Parisb782e0a2010-07-23 11:44:03 -04003110 /* No permission to check. Existence test. */
3111 if (!mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113
Eric Paris2e3340572012-04-04 15:01:42 -04003114 if (unlikely(IS_PRIVATE(inode)))
3115 return 0;
Eric Parisb782e0a2010-07-23 11:44:03 -04003116
3117 perms = file_mask_to_av(inode->i_mode, mask);
3118
Eric Paris2e3340572012-04-04 15:01:42 -04003119 sid = cred_sid(cred);
Stephen Smalley5298d0b2019-11-22 16:16:56 -05003120 isec = inode_security_rcu(inode, no_block);
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05003121 if (IS_ERR(isec))
3122 return PTR_ERR(isec);
Eric Paris2e3340572012-04-04 15:01:42 -04003123
Stephen Smalleye67b7982023-03-09 13:30:37 -05003124 rc = avc_has_perm_noaudit(sid, isec->sid, isec->sclass, perms, 0,
Stephen Smalley3a28cff2018-12-12 10:10:55 -05003125 &avd);
Eric Paris2e3340572012-04-04 15:01:42 -04003126 audited = avc_audit_required(perms, &avd, rc,
3127 from_access ? FILE__AUDIT_ACCESS : 0,
3128 &denied);
3129 if (likely(!audited))
3130 return rc;
3131
Stephen Smalley0188d5c2019-11-22 12:22:45 -05003132 rc2 = audit_inode_permission(inode, perms, audited, denied, rc);
Eric Paris2e3340572012-04-04 15:01:42 -04003133 if (rc2)
3134 return rc2;
3135 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136}
3137
Roberto Sassu314a8dc2024-02-15 11:30:57 +01003138static int selinux_inode_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
3139 struct iattr *iattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140{
David Howells88e67f32008-11-14 10:39:21 +11003141 const struct cred *cred = current_cred();
Stephen Smalleyccb54472017-05-12 12:41:24 -04003142 struct inode *inode = d_backing_inode(dentry);
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003143 unsigned int ia_valid = iattr->ia_valid;
Eric Paris95dbf732012-04-04 13:45:34 -04003144 __u32 av = FILE__WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003146 /* ATTR_FORCE is just used for ATTR_KILL_S[UG]ID. */
3147 if (ia_valid & ATTR_FORCE) {
3148 ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_MODE |
3149 ATTR_FORCE);
3150 if (!ia_valid)
3151 return 0;
3152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153
Amerigo Wangbc6a6002009-08-20 19:29:02 -07003154 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID |
3155 ATTR_ATIME_SET | ATTR_MTIME_SET | ATTR_TIMES_SET))
Eric Paris2875fa02011-04-28 16:04:24 -04003156 return dentry_has_perm(cred, dentry, FILE__SETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003158 if (selinux_policycap_openperm() &&
Stephen Smalleyccb54472017-05-12 12:41:24 -04003159 inode->i_sb->s_magic != SOCKFS_MAGIC &&
3160 (ia_valid & ATTR_SIZE) &&
3161 !(ia_valid & ATTR_FILE))
Eric Paris95dbf732012-04-04 13:45:34 -04003162 av |= FILE__OPEN;
3163
3164 return dentry_has_perm(cred, dentry, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165}
3166
Al Viro3f7036a2015-03-08 19:28:30 -04003167static int selinux_inode_getattr(const struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168{
Al Viro3f7036a2015-03-08 19:28:30 -04003169 return path_has_perm(current_cred(), path, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170}
3171
Stephen Smalleydb590002017-04-20 11:31:30 -04003172static bool has_cap_mac_admin(bool audit)
3173{
3174 const struct cred *cred = current_cred();
Micah Mortonc1a85a02019-01-07 16:10:53 -08003175 unsigned int opts = audit ? CAP_OPT_NONE : CAP_OPT_NOAUDIT;
Stephen Smalleydb590002017-04-20 11:31:30 -04003176
Micah Mortonc1a85a02019-01-07 16:10:53 -08003177 if (cap_capable(cred, &init_user_ns, CAP_MAC_ADMIN, opts))
Stephen Smalleydb590002017-04-20 11:31:30 -04003178 return false;
Micah Mortonc1a85a02019-01-07 16:10:53 -08003179 if (cred_has_capability(cred, CAP_MAC_ADMIN, opts, true))
Stephen Smalleydb590002017-04-20 11:31:30 -04003180 return false;
3181 return true;
3182}
3183
Christian Brauner39f60c12023-01-13 12:49:23 +01003184static int selinux_inode_setxattr(struct mnt_idmap *idmap,
Christian Brauner71bc3562021-01-21 14:19:29 +01003185 struct dentry *dentry, const char *name,
David Howells8f0cfa52008-04-29 00:59:41 -07003186 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187{
David Howellsc6f493d2015-03-17 22:26:22 +00003188 struct inode *inode = d_backing_inode(dentry);
Paul Moore20cdef82016-04-04 14:14:42 -04003189 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 struct superblock_security_struct *sbsec;
Thomas Liu2bf49692009-07-14 12:14:09 -04003191 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11003192 u32 newsid, sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 int rc = 0;
3194
Eric W. Biederman6b240302017-10-02 09:38:20 -05003195 if (strcmp(name, XATTR_NAME_SELINUX)) {
3196 rc = cap_inode_setxattr(dentry, name, value, size, flags);
3197 if (rc)
3198 return rc;
3199
3200 /* Not an attribute we recognize, so just check the
3201 ordinary setattr permission. */
3202 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204
Stephen Smalleye67b7982023-03-09 13:30:37 -05003205 if (!selinux_initialized())
Christian Brauner01beba72023-01-13 12:49:26 +01003206 return (inode_owner_or_capable(idmap, inode) ? 0 : -EPERM);
Jonathan Lebon3e3e24b2019-09-12 09:30:07 -04003207
Casey Schaufler1aea7802021-04-22 17:41:15 +02003208 sbsec = selinux_superblock(inode->i_sb);
Eric Paris12f348b2012-10-09 10:56:25 -04003209 if (!(sbsec->flags & SBLABEL_MNT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 return -EOPNOTSUPP;
3211
Christian Brauner01beba72023-01-13 12:49:26 +01003212 if (!inode_owner_or_capable(idmap, inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 return -EPERM;
3214
Eric Paris50c205f2012-04-04 15:01:43 -04003215 ad.type = LSM_AUDIT_DATA_DENTRY;
Eric Parisa2694342011-04-25 13:10:27 -04003216 ad.u.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217
Paul Moore20cdef82016-04-04 14:14:42 -04003218 isec = backing_inode_security(dentry);
Stephen Smalleye67b7982023-03-09 13:30:37 -05003219 rc = avc_has_perm(sid, isec->sid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 FILE__RELABELFROM, &ad);
3221 if (rc)
3222 return rc;
3223
Stephen Smalleye67b7982023-03-09 13:30:37 -05003224 rc = security_context_to_sid(value, size, &newsid,
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003225 GFP_KERNEL);
Stephen Smalley12b29f32008-05-07 13:03:20 -04003226 if (rc == -EINVAL) {
Stephen Smalleydb590002017-04-20 11:31:30 -04003227 if (!has_cap_mac_admin(true)) {
Eric Parisd6ea83e2012-04-04 13:45:49 -04003228 struct audit_buffer *ab;
3229 size_t audit_size;
Eric Parisd6ea83e2012-04-04 13:45:49 -04003230
3231 /* We strip a nul only if it is at the end, otherwise the
3232 * context contains a nul and we should audit that */
Al Viroe3fea3f2012-06-09 08:15:16 +01003233 if (value) {
Colin Ian Kingadd24372017-10-14 13:46:55 +01003234 const char *str = value;
3235
Al Viroe3fea3f2012-06-09 08:15:16 +01003236 if (str[size - 1] == '\0')
3237 audit_size = size - 1;
3238 else
3239 audit_size = size;
3240 } else {
Al Viroe3fea3f2012-06-09 08:15:16 +01003241 audit_size = 0;
3242 }
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04003243 ab = audit_log_start(audit_context(),
3244 GFP_ATOMIC, AUDIT_SELINUX_ERR);
Austin Kim893c47d2021-07-14 01:11:27 +01003245 if (!ab)
3246 return rc;
Eric Parisd6ea83e2012-04-04 13:45:49 -04003247 audit_log_format(ab, "op=setxattr invalid_context=");
3248 audit_log_n_untrustedstring(ab, value, audit_size);
3249 audit_log_end(ab);
3250
Stephen Smalley12b29f32008-05-07 13:03:20 -04003251 return rc;
Eric Parisd6ea83e2012-04-04 13:45:49 -04003252 }
Stephen Smalleye67b7982023-03-09 13:30:37 -05003253 rc = security_context_to_sid_force(value,
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003254 size, &newsid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04003255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 if (rc)
3257 return rc;
3258
Stephen Smalleye67b7982023-03-09 13:30:37 -05003259 rc = avc_has_perm(sid, newsid, isec->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 FILE__RELABELTO, &ad);
3261 if (rc)
3262 return rc;
3263
Stephen Smalleye67b7982023-03-09 13:30:37 -05003264 rc = security_validate_transition(isec->sid, newsid,
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003265 sid, isec->sclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 if (rc)
3267 return rc;
3268
Stephen Smalleye67b7982023-03-09 13:30:37 -05003269 return avc_has_perm(newsid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 sbsec->sid,
3271 SECCLASS_FILESYSTEM,
3272 FILESYSTEM__ASSOCIATE,
3273 &ad);
3274}
3275
Christian Brauner700b7942023-01-13 12:49:24 +01003276static int selinux_inode_set_acl(struct mnt_idmap *idmap,
Christian Brauner1bdeb212022-09-22 17:17:08 +02003277 struct dentry *dentry, const char *acl_name,
3278 struct posix_acl *kacl)
3279{
3280 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3281}
3282
Christian Brauner700b7942023-01-13 12:49:24 +01003283static int selinux_inode_get_acl(struct mnt_idmap *idmap,
Christian Brauner1bdeb212022-09-22 17:17:08 +02003284 struct dentry *dentry, const char *acl_name)
3285{
3286 return dentry_has_perm(current_cred(), dentry, FILE__GETATTR);
3287}
3288
Christian Brauner700b7942023-01-13 12:49:24 +01003289static int selinux_inode_remove_acl(struct mnt_idmap *idmap,
Christian Brauner1bdeb212022-09-22 17:17:08 +02003290 struct dentry *dentry, const char *acl_name)
3291{
3292 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3293}
3294
David Howells8f0cfa52008-04-29 00:59:41 -07003295static void selinux_inode_post_setxattr(struct dentry *dentry, const char *name,
Eric Parisf5269712008-05-14 11:27:45 -04003296 const void *value, size_t size,
David Howells8f0cfa52008-04-29 00:59:41 -07003297 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298{
David Howellsc6f493d2015-03-17 22:26:22 +00003299 struct inode *inode = d_backing_inode(dentry);
Paul Moore20cdef82016-04-04 14:14:42 -04003300 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 u32 newsid;
3302 int rc;
3303
3304 if (strcmp(name, XATTR_NAME_SELINUX)) {
3305 /* Not an attribute we recognize, so nothing to do. */
3306 return;
3307 }
3308
Stephen Smalleye67b7982023-03-09 13:30:37 -05003309 if (!selinux_initialized()) {
Jonathan Lebon3e3e24b2019-09-12 09:30:07 -04003310 /* If we haven't even been initialized, then we can't validate
3311 * against a policy, so leave the label as invalid. It may
3312 * resolve to a valid label on the next revalidation try if
3313 * we've since initialized.
3314 */
3315 return;
3316 }
3317
Stephen Smalleye67b7982023-03-09 13:30:37 -05003318 rc = security_context_to_sid_force(value, size,
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003319 &newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 if (rc) {
peter enderborgc103a912018-06-12 10:09:03 +02003321 pr_err("SELinux: unable to map context to SID"
Stephen Smalley12b29f32008-05-07 13:03:20 -04003322 "for (%s, %lu), rc=%d\n",
3323 inode->i_sb->s_id, inode->i_ino, -rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 return;
3325 }
3326
Paul Moore20cdef82016-04-04 14:14:42 -04003327 isec = backing_inode_security(dentry);
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003328 spin_lock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003329 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05003331 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003332 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333}
3334
David Howells8f0cfa52008-04-29 00:59:41 -07003335static int selinux_inode_getxattr(struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336{
David Howells88e67f32008-11-14 10:39:21 +11003337 const struct cred *cred = current_cred();
3338
Eric Paris2875fa02011-04-28 16:04:24 -04003339 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340}
3341
Eric Paris828dfe12008-04-17 13:17:49 -04003342static int selinux_inode_listxattr(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343{
David Howells88e67f32008-11-14 10:39:21 +11003344 const struct cred *cred = current_cred();
3345
Eric Paris2875fa02011-04-28 16:04:24 -04003346 return dentry_has_perm(cred, dentry, FILE__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347}
3348
Christian Brauner39f60c12023-01-13 12:49:23 +01003349static int selinux_inode_removexattr(struct mnt_idmap *idmap,
Christian Brauner71bc3562021-01-21 14:19:29 +01003350 struct dentry *dentry, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351{
Eric W. Biederman6b240302017-10-02 09:38:20 -05003352 if (strcmp(name, XATTR_NAME_SELINUX)) {
Christian Brauner39f60c12023-01-13 12:49:23 +01003353 int rc = cap_inode_removexattr(idmap, dentry, name);
Eric W. Biederman6b240302017-10-02 09:38:20 -05003354 if (rc)
3355 return rc;
3356
3357 /* Not an attribute we recognize, so just check the
3358 ordinary setattr permission. */
3359 return dentry_has_perm(current_cred(), dentry, FILE__SETATTR);
3360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361
Stephen Smalleye67b7982023-03-09 13:30:37 -05003362 if (!selinux_initialized())
Stephen Smalley9530a3e2020-08-20 13:00:40 -04003363 return 0;
3364
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 /* No one is allowed to remove a SELinux security label.
3366 You can change the label, but all data must be labeled. */
3367 return -EACCES;
3368}
3369
Aaron Goidelac5656d2019-08-12 11:20:00 -04003370static int selinux_path_notify(const struct path *path, u64 mask,
3371 unsigned int obj_type)
3372{
3373 int ret;
3374 u32 perm;
3375
3376 struct common_audit_data ad;
3377
3378 ad.type = LSM_AUDIT_DATA_PATH;
3379 ad.u.path = *path;
3380
3381 /*
3382 * Set permission needed based on the type of mark being set.
3383 * Performs an additional check for sb watches.
3384 */
3385 switch (obj_type) {
3386 case FSNOTIFY_OBJ_TYPE_VFSMOUNT:
3387 perm = FILE__WATCH_MOUNT;
3388 break;
3389 case FSNOTIFY_OBJ_TYPE_SB:
3390 perm = FILE__WATCH_SB;
3391 ret = superblock_has_perm(current_cred(), path->dentry->d_sb,
3392 FILESYSTEM__WATCH, &ad);
3393 if (ret)
3394 return ret;
3395 break;
3396 case FSNOTIFY_OBJ_TYPE_INODE:
3397 perm = FILE__WATCH;
3398 break;
3399 default:
3400 return -EINVAL;
3401 }
3402
3403 /* blocking watches require the file:watch_with_perm permission */
3404 if (mask & (ALL_FSNOTIFY_PERM_EVENTS))
3405 perm |= FILE__WATCH_WITH_PERM;
3406
3407 /* watches on read-like events need the file:watch_reads permission */
3408 if (mask & (FS_ACCESS | FS_ACCESS_PERM | FS_CLOSE_NOWRITE))
3409 perm |= FILE__WATCH_READS;
3410
3411 return path_has_perm(current_cred(), path, perm);
3412}
3413
James Morrisd381d8a2005-10-30 14:59:22 -08003414/*
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003415 * Copy the inode security context value to the user.
James Morrisd381d8a2005-10-30 14:59:22 -08003416 *
3417 * Permission check is handled by selinux_inode_getxattr hook.
3418 */
Christian Brauner4609e1f2023-01-13 12:49:22 +01003419static int selinux_inode_getsecurity(struct mnt_idmap *idmap,
Christian Brauner71bc3562021-01-21 14:19:29 +01003420 struct inode *inode, const char *name,
3421 void **buffer, bool alloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422{
David P. Quigley42492592008-02-04 22:29:39 -08003423 u32 size;
3424 int error;
3425 char *context = NULL;
Paul Moore20cdef82016-04-04 14:14:42 -04003426 struct inode_security_struct *isec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427
Jonathan Lebonc8e22262020-05-28 10:39:40 -04003428 /*
3429 * If we're not initialized yet, then we can't validate contexts, so
3430 * just let vfs_getxattr fall back to using the on-disk xattr.
3431 */
Stephen Smalleye67b7982023-03-09 13:30:37 -05003432 if (!selinux_initialized() ||
Jonathan Lebonc8e22262020-05-28 10:39:40 -04003433 strcmp(name, XATTR_SELINUX_SUFFIX))
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00003434 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003436 /*
3437 * If the caller has CAP_MAC_ADMIN, then get the raw context
3438 * value even if it is not defined by current policy; otherwise,
3439 * use the in-core value under current policy.
3440 * Use the non-auditing forms of the permission checks since
3441 * getxattr may be called by unprivileged processes commonly
3442 * and lack of permission just means that we fall back to the
3443 * in-core context value, not a denial.
3444 */
Paul Moore20cdef82016-04-04 14:14:42 -04003445 isec = inode_security(inode);
Stephen Smalleydb590002017-04-20 11:31:30 -04003446 if (has_cap_mac_admin(false))
Stephen Smalleye67b7982023-03-09 13:30:37 -05003447 error = security_sid_to_context_force(isec->sid, &context,
Stephen Smalleyabc69bb2008-05-21 14:16:12 -04003448 &size);
3449 else
Stephen Smalleye67b7982023-03-09 13:30:37 -05003450 error = security_sid_to_context(isec->sid,
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003451 &context, &size);
David P. Quigley42492592008-02-04 22:29:39 -08003452 if (error)
3453 return error;
3454 error = size;
3455 if (alloc) {
3456 *buffer = context;
3457 goto out_nofree;
3458 }
3459 kfree(context);
3460out_nofree:
3461 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462}
3463
3464static int selinux_inode_setsecurity(struct inode *inode, const char *name,
Eric Paris828dfe12008-04-17 13:17:49 -04003465 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466{
Paul Moore2c971652016-04-19 16:36:28 -04003467 struct inode_security_struct *isec = inode_security_novalidate(inode);
Casey Schaufler1aea7802021-04-22 17:41:15 +02003468 struct superblock_security_struct *sbsec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 u32 newsid;
3470 int rc;
3471
3472 if (strcmp(name, XATTR_SELINUX_SUFFIX))
3473 return -EOPNOTSUPP;
3474
Casey Schaufler1aea7802021-04-22 17:41:15 +02003475 sbsec = selinux_superblock(inode->i_sb);
Ondrej Mosnacek53e0c2a2018-12-21 21:18:53 +01003476 if (!(sbsec->flags & SBLABEL_MNT))
3477 return -EOPNOTSUPP;
3478
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 if (!value || !size)
3480 return -EACCES;
3481
Stephen Smalleye67b7982023-03-09 13:30:37 -05003482 rc = security_context_to_sid(value, size, &newsid,
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05003483 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 if (rc)
3485 return rc;
3486
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003487 spin_lock(&isec->lock);
David Quigleyaa9c2662013-05-22 12:50:44 -04003488 isec->sclass = inode_mode_to_security_class(inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 isec->sid = newsid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05003490 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01003491 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 return 0;
3493}
3494
3495static int selinux_inode_listsecurity(struct inode *inode, char *buffer, size_t buffer_size)
3496{
3497 const int len = sizeof(XATTR_NAME_SELINUX);
Amir Goldsteina9ffe682020-12-19 12:05:27 +02003498
Stephen Smalleye67b7982023-03-09 13:30:37 -05003499 if (!selinux_initialized())
Amir Goldsteina9ffe682020-12-19 12:05:27 +02003500 return 0;
3501
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 if (buffer && len <= buffer_size)
3503 memcpy(buffer, XATTR_NAME_SELINUX, len);
3504 return len;
3505}
3506
Andreas Gruenbacherd6335d72015-12-24 11:09:39 -05003507static void selinux_inode_getsecid(struct inode *inode, u32 *secid)
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02003508{
Andreas Gruenbachere817c2f2016-02-18 12:04:08 +01003509 struct inode_security_struct *isec = inode_security_novalidate(inode);
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02003510 *secid = isec->sid;
3511}
3512
Vivek Goyal56909eb2016-07-13 10:44:48 -04003513static int selinux_inode_copy_up(struct dentry *src, struct cred **new)
3514{
3515 u32 sid;
3516 struct task_security_struct *tsec;
3517 struct cred *new_creds = *new;
3518
3519 if (new_creds == NULL) {
3520 new_creds = prepare_creds();
3521 if (!new_creds)
3522 return -ENOMEM;
3523 }
3524
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07003525 tsec = selinux_cred(new_creds);
Vivek Goyal56909eb2016-07-13 10:44:48 -04003526 /* Get label from overlay inode and set it in create_sid */
3527 selinux_inode_getsecid(d_inode(src), &sid);
3528 tsec->create_sid = sid;
3529 *new = new_creds;
3530 return 0;
3531}
3532
Vivek Goyal19472b62016-07-13 10:44:50 -04003533static int selinux_inode_copy_up_xattr(const char *name)
3534{
3535 /* The copy_up hook above sets the initial context on an inode, but we
3536 * don't then want to overwrite it by blindly copying all the lower
David Disseldorp7c655be2024-02-02 17:40:48 +11003537 * xattrs up. Instead, filter out SELinux-related xattrs following
3538 * policy load.
Vivek Goyal19472b62016-07-13 10:44:50 -04003539 */
David Disseldorp7c655be2024-02-02 17:40:48 +11003540 if (selinux_initialized() && strcmp(name, XATTR_NAME_SELINUX) == 0)
Vivek Goyal19472b62016-07-13 10:44:50 -04003541 return 1; /* Discard */
3542 /*
3543 * Any other attribute apart from SELINUX is not claimed, supported
3544 * by selinux.
3545 */
3546 return -EOPNOTSUPP;
3547}
3548
Ondrej Mosnacekec882da2019-02-22 15:57:17 +01003549/* kernfs node operations */
3550
YueHaibingc72c4cd2019-03-22 22:04:00 +08003551static int selinux_kernfs_init_security(struct kernfs_node *kn_dir,
3552 struct kernfs_node *kn)
Ondrej Mosnacekec882da2019-02-22 15:57:17 +01003553{
Stephen Smalley169ce0c2019-09-04 10:32:48 -04003554 const struct task_security_struct *tsec = selinux_cred(current_cred());
Ondrej Mosnacekec882da2019-02-22 15:57:17 +01003555 u32 parent_sid, newsid, clen;
3556 int rc;
3557 char *context;
3558
Ondrej Mosnacek1537ad12019-04-03 09:29:41 +02003559 rc = kernfs_xattr_get(kn_dir, XATTR_NAME_SELINUX, NULL, 0);
Ondrej Mosnacekec882da2019-02-22 15:57:17 +01003560 if (rc == -ENODATA)
3561 return 0;
3562 else if (rc < 0)
3563 return rc;
3564
3565 clen = (u32)rc;
3566 context = kmalloc(clen, GFP_KERNEL);
3567 if (!context)
3568 return -ENOMEM;
3569
Ondrej Mosnacek1537ad12019-04-03 09:29:41 +02003570 rc = kernfs_xattr_get(kn_dir, XATTR_NAME_SELINUX, context, clen);
Ondrej Mosnacekec882da2019-02-22 15:57:17 +01003571 if (rc < 0) {
3572 kfree(context);
3573 return rc;
3574 }
3575
Stephen Smalleye67b7982023-03-09 13:30:37 -05003576 rc = security_context_to_sid(context, clen, &parent_sid,
Ondrej Mosnacekec882da2019-02-22 15:57:17 +01003577 GFP_KERNEL);
3578 kfree(context);
3579 if (rc)
3580 return rc;
3581
3582 if (tsec->create_sid) {
3583 newsid = tsec->create_sid;
3584 } else {
3585 u16 secclass = inode_mode_to_security_class(kn->mode);
3586 struct qstr q;
3587
3588 q.name = kn->name;
3589 q.hash_len = hashlen_string(kn_dir, kn->name);
3590
Stephen Smalleye67b7982023-03-09 13:30:37 -05003591 rc = security_transition_sid(tsec->sid,
Ondrej Mosnacekec882da2019-02-22 15:57:17 +01003592 parent_sid, secclass, &q,
3593 &newsid);
3594 if (rc)
3595 return rc;
3596 }
3597
Stephen Smalleye67b7982023-03-09 13:30:37 -05003598 rc = security_sid_to_context_force(newsid,
Ondrej Mosnacekec882da2019-02-22 15:57:17 +01003599 &context, &clen);
3600 if (rc)
3601 return rc;
3602
Ondrej Mosnacek1537ad12019-04-03 09:29:41 +02003603 rc = kernfs_xattr_set(kn, XATTR_NAME_SELINUX, context, clen,
3604 XATTR_CREATE);
Ondrej Mosnacekec882da2019-02-22 15:57:17 +01003605 kfree(context);
3606 return rc;
3607}
3608
3609
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610/* file security operations */
3611
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003612static int selinux_revalidate_file_permission(struct file *file, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613{
David Howells88e67f32008-11-14 10:39:21 +11003614 const struct cred *cred = current_cred();
Al Viro496ad9a2013-01-23 17:07:38 -05003615 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 /* file_mask_to_av won't add FILE__WRITE if MAY_APPEND is set */
3618 if ((file->f_flags & O_APPEND) && (mask & MAY_WRITE))
3619 mask |= MAY_APPEND;
3620
Paul Moore389fb8002009-03-27 17:10:34 -04003621 return file_has_perm(cred, file,
3622 file_mask_to_av(inode->i_mode, mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623}
3624
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003625static int selinux_file_permission(struct file *file, int mask)
3626{
Al Viro496ad9a2013-01-23 17:07:38 -05003627 struct inode *inode = file_inode(file);
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07003628 struct file_security_struct *fsec = selinux_file(file);
Andreas Gruenbacherb1973672016-01-05 23:12:33 +01003629 struct inode_security_struct *isec;
Stephen Smalley20dda182009-06-22 14:54:53 -04003630 u32 sid = current_sid();
3631
Paul Moore389fb8002009-03-27 17:10:34 -04003632 if (!mask)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003633 /* No permission to check. Existence test. */
3634 return 0;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003635
Andreas Gruenbacherb1973672016-01-05 23:12:33 +01003636 isec = inode_security(inode);
Stephen Smalley20dda182009-06-22 14:54:53 -04003637 if (sid == fsec->sid && fsec->isid == isec->sid &&
Stephen Smalleye67b7982023-03-09 13:30:37 -05003638 fsec->pseqno == avc_policy_seqno())
Eric Paris83d49852012-04-04 13:45:40 -04003639 /* No change since file_open check. */
Stephen Smalley20dda182009-06-22 14:54:53 -04003640 return 0;
3641
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003642 return selinux_revalidate_file_permission(file, mask);
3643}
3644
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645static int selinux_file_alloc_security(struct file *file)
3646{
Paul Moorecb89e242020-01-10 16:32:10 -05003647 struct file_security_struct *fsec = selinux_file(file);
3648 u32 sid = current_sid();
3649
3650 fsec->sid = sid;
3651 fsec->fown_sid = sid;
3652
3653 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654}
3655
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003656/*
3657 * Check whether a task has the ioctl permission and cmd
3658 * operation to an inode.
3659 */
Geliang Tang1d2a1682015-10-21 17:44:27 -04003660static int ioctl_has_perm(const struct cred *cred, struct file *file,
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003661 u32 requested, u16 cmd)
3662{
3663 struct common_audit_data ad;
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07003664 struct file_security_struct *fsec = selinux_file(file);
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003665 struct inode *inode = file_inode(file);
Paul Moore20cdef82016-04-04 14:14:42 -04003666 struct inode_security_struct *isec;
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003667 struct lsm_ioctlop_audit ioctl;
3668 u32 ssid = cred_sid(cred);
3669 int rc;
3670 u8 driver = cmd >> 8;
3671 u8 xperm = cmd & 0xff;
3672
3673 ad.type = LSM_AUDIT_DATA_IOCTL_OP;
3674 ad.u.op = &ioctl;
3675 ad.u.op->cmd = cmd;
3676 ad.u.op->path = file->f_path;
3677
3678 if (ssid != fsec->sid) {
Stephen Smalleye67b7982023-03-09 13:30:37 -05003679 rc = avc_has_perm(ssid, fsec->sid,
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003680 SECCLASS_FD,
3681 FD__USE,
3682 &ad);
3683 if (rc)
3684 goto out;
3685 }
3686
3687 if (unlikely(IS_PRIVATE(inode)))
3688 return 0;
3689
Paul Moore20cdef82016-04-04 14:14:42 -04003690 isec = inode_security(inode);
Stephen Smalleye67b7982023-03-09 13:30:37 -05003691 rc = avc_has_extended_perms(ssid, isec->sid, isec->sclass,
Stephen Smalley6b6bc622018-03-05 11:47:56 -05003692 requested, driver, xperm, &ad);
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003693out:
3694 return rc;
3695}
3696
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697static int selinux_file_ioctl(struct file *file, unsigned int cmd,
3698 unsigned long arg)
3699{
David Howells88e67f32008-11-14 10:39:21 +11003700 const struct cred *cred = current_cred();
Eric Paris0b24dcb2011-02-25 15:39:20 -05003701 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702
Eric Paris0b24dcb2011-02-25 15:39:20 -05003703 switch (cmd) {
3704 case FIONREAD:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003705 case FIBMAP:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003706 case FIGETBSZ:
Al Viro2f99c362012-03-23 16:04:05 -04003707 case FS_IOC_GETFLAGS:
Al Viro2f99c362012-03-23 16:04:05 -04003708 case FS_IOC_GETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003709 error = file_has_perm(cred, file, FILE__GETATTR);
3710 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711
Al Viro2f99c362012-03-23 16:04:05 -04003712 case FS_IOC_SETFLAGS:
Al Viro2f99c362012-03-23 16:04:05 -04003713 case FS_IOC_SETVERSION:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003714 error = file_has_perm(cred, file, FILE__SETATTR);
3715 break;
3716
3717 /* sys_ioctl() checks */
3718 case FIONBIO:
Eric Paris0b24dcb2011-02-25 15:39:20 -05003719 case FIOASYNC:
3720 error = file_has_perm(cred, file, 0);
3721 break;
3722
3723 case KDSKBENT:
3724 case KDSKBSENT:
Eric Paris6a9de492012-01-03 12:25:14 -05003725 error = cred_has_capability(cred, CAP_SYS_TTY_CONFIG,
Micah Mortonc1a85a02019-01-07 16:10:53 -08003726 CAP_OPT_NONE, true);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003727 break;
3728
Richard Haines65881e12022-02-25 17:54:38 +00003729 case FIOCLEX:
3730 case FIONCLEX:
3731 if (!selinux_policycap_ioctl_skip_cloexec())
3732 error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd);
3733 break;
3734
Eric Paris0b24dcb2011-02-25 15:39:20 -05003735 /* default case assumes that the command will go
3736 * to the file's ioctl() function.
3737 */
3738 default:
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04003739 error = ioctl_has_perm(cred, file, FILE__IOCTL, (u16) cmd);
Eric Paris0b24dcb2011-02-25 15:39:20 -05003740 }
3741 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742}
3743
Alfred Piccionif1bb47a2023-12-19 10:09:09 +01003744static int selinux_file_ioctl_compat(struct file *file, unsigned int cmd,
3745 unsigned long arg)
3746{
3747 /*
3748 * If we are in a 64-bit kernel running 32-bit userspace, we need to
3749 * make sure we don't compare 32-bit flags to 64-bit flags.
3750 */
3751 switch (cmd) {
3752 case FS_IOC32_GETFLAGS:
3753 cmd = FS_IOC_GETFLAGS;
3754 break;
3755 case FS_IOC32_SETFLAGS:
3756 cmd = FS_IOC_SETFLAGS;
3757 break;
3758 case FS_IOC32_GETVERSION:
3759 cmd = FS_IOC_GETVERSION;
3760 break;
3761 case FS_IOC32_SETVERSION:
3762 cmd = FS_IOC_SETVERSION;
3763 break;
3764 default:
3765 break;
3766 }
3767
3768 return selinux_file_ioctl(file, cmd, arg);
3769}
3770
Stephen Smalleyb78b7d52020-01-08 12:23:56 -05003771static int default_noexec __ro_after_init;
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003772
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773static int file_map_prot_check(struct file *file, unsigned long prot, int shared)
3774{
David Howells88e67f32008-11-14 10:39:21 +11003775 const struct cred *cred = current_cred();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003776 u32 sid = cred_sid(cred);
David Howellsd84f4f92008-11-14 10:39:23 +11003777 int rc = 0;
David Howells88e67f32008-11-14 10:39:21 +11003778
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003779 if (default_noexec &&
Stephen Smalley892e8ca2015-07-10 09:40:59 -04003780 (prot & PROT_EXEC) && (!file || IS_PRIVATE(file_inode(file)) ||
3781 (!shared && (prot & PROT_WRITE)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 /*
3783 * We are making executable an anonymous mapping or a
3784 * private file mapping that will also be writable.
3785 * This has an additional check.
3786 */
Stephen Smalleye67b7982023-03-09 13:30:37 -05003787 rc = avc_has_perm(sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003788 PROCESS__EXECMEM, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 if (rc)
David Howellsd84f4f92008-11-14 10:39:23 +11003790 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792
3793 if (file) {
3794 /* read access is always possible with a mapping */
3795 u32 av = FILE__READ;
3796
3797 /* write access only matters if the mapping is shared */
3798 if (shared && (prot & PROT_WRITE))
3799 av |= FILE__WRITE;
3800
3801 if (prot & PROT_EXEC)
3802 av |= FILE__EXECUTE;
3803
David Howells88e67f32008-11-14 10:39:21 +11003804 return file_has_perm(cred, file, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 }
David Howellsd84f4f92008-11-14 10:39:23 +11003806
3807error:
3808 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809}
3810
Al Viroe5467852012-05-30 13:30:51 -04003811static int selinux_mmap_addr(unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812{
Casey Schauflerb1d9e6b2015-05-02 15:11:42 -07003813 int rc = 0;
Paul Moore98883bf2014-03-19 16:46:11 -04003814
3815 if (addr < CONFIG_LSM_MMAP_MIN_ADDR) {
3816 u32 sid = current_sid();
Stephen Smalleye67b7982023-03-09 13:30:37 -05003817 rc = avc_has_perm(sid, sid, SECCLASS_MEMPROTECT,
Paul Moore98883bf2014-03-19 16:46:11 -04003818 MEMPROTECT__MMAP_ZERO, NULL);
3819 }
3820
3821 return rc;
Al Viroe5467852012-05-30 13:30:51 -04003822}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823
Paul Moorea7e46762023-03-16 11:43:08 -04003824static int selinux_mmap_file(struct file *file,
3825 unsigned long reqprot __always_unused,
Al Viroe5467852012-05-30 13:30:51 -04003826 unsigned long prot, unsigned long flags)
3827{
Stephen Smalley3ba4bf52017-05-05 09:14:48 -04003828 struct common_audit_data ad;
3829 int rc;
3830
3831 if (file) {
3832 ad.type = LSM_AUDIT_DATA_FILE;
3833 ad.u.file = file;
3834 rc = inode_has_perm(current_cred(), file_inode(file),
3835 FILE__MAP, &ad);
3836 if (rc)
3837 return rc;
3838 }
3839
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840 return file_map_prot_check(file, prot,
3841 (flags & MAP_TYPE) == MAP_SHARED);
3842}
3843
3844static int selinux_file_mprotect(struct vm_area_struct *vma,
Paul Moorea7e46762023-03-16 11:43:08 -04003845 unsigned long reqprot __always_unused,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 unsigned long prot)
3847{
David Howells88e67f32008-11-14 10:39:21 +11003848 const struct cred *cred = current_cred();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003849 u32 sid = cred_sid(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04003851 if (default_noexec &&
3852 (prot & PROT_EXEC) && !(vma->vm_flags & VM_EXEC)) {
James Morrisd541bbe2009-01-29 12:19:51 +11003853 int rc = 0;
Kefeng Wang68df1ba2023-07-28 13:00:42 +08003854 if (vma_is_initial_heap(vma)) {
Stephen Smalleye67b7982023-03-09 13:30:37 -05003855 rc = avc_has_perm(sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003856 PROCESS__EXECHEAP, NULL);
Kefeng Wang68df1ba2023-07-28 13:00:42 +08003857 } else if (!vma->vm_file && (vma_is_initial_stack(vma) ||
Andy Lutomirskid17af502016-09-30 10:58:58 -07003858 vma_is_stack_for_current(vma))) {
Stephen Smalleye67b7982023-03-09 13:30:37 -05003859 rc = avc_has_perm(sid, sid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003860 PROCESS__EXECSTACK, NULL);
Stephen Smalleydb4c96412006-02-01 03:05:54 -08003861 } else if (vma->vm_file && vma->anon_vma) {
3862 /*
3863 * We are making executable a file mapping that has
3864 * had some COW done. Since pages might have been
3865 * written, check ability to execute the possibly
3866 * modified content. This typically should only
3867 * occur for text relocations.
3868 */
David Howellsd84f4f92008-11-14 10:39:23 +11003869 rc = file_has_perm(cred, vma->vm_file, FILE__EXECMOD);
Stephen Smalleydb4c96412006-02-01 03:05:54 -08003870 }
Lorenzo Hernandez García-Hierro6b992192005-06-25 14:54:34 -07003871 if (rc)
3872 return rc;
3873 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874
3875 return file_map_prot_check(vma->vm_file, prot, vma->vm_flags&VM_SHARED);
3876}
3877
3878static int selinux_file_lock(struct file *file, unsigned int cmd)
3879{
David Howells88e67f32008-11-14 10:39:21 +11003880 const struct cred *cred = current_cred();
3881
3882 return file_has_perm(cred, file, FILE__LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883}
3884
3885static int selinux_file_fcntl(struct file *file, unsigned int cmd,
3886 unsigned long arg)
3887{
David Howells88e67f32008-11-14 10:39:21 +11003888 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889 int err = 0;
3890
3891 switch (cmd) {
Eric Paris828dfe12008-04-17 13:17:49 -04003892 case F_SETFL:
Eric Paris828dfe12008-04-17 13:17:49 -04003893 if ((file->f_flags & O_APPEND) && !(arg & O_APPEND)) {
David Howells88e67f32008-11-14 10:39:21 +11003894 err = file_has_perm(cred, file, FILE__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 break;
Eric Paris828dfe12008-04-17 13:17:49 -04003896 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05003897 fallthrough;
Eric Paris828dfe12008-04-17 13:17:49 -04003898 case F_SETOWN:
3899 case F_SETSIG:
3900 case F_GETFL:
3901 case F_GETOWN:
3902 case F_GETSIG:
Cyrill Gorcunov1d151c32012-07-30 14:43:00 -07003903 case F_GETOWNER_UIDS:
Eric Paris828dfe12008-04-17 13:17:49 -04003904 /* Just check FD__USE permission */
David Howells88e67f32008-11-14 10:39:21 +11003905 err = file_has_perm(cred, file, 0);
Eric Paris828dfe12008-04-17 13:17:49 -04003906 break;
3907 case F_GETLK:
3908 case F_SETLK:
3909 case F_SETLKW:
Jeff Layton0d3f7a22014-04-22 08:23:58 -04003910 case F_OFD_GETLK:
3911 case F_OFD_SETLK:
3912 case F_OFD_SETLKW:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913#if BITS_PER_LONG == 32
Eric Paris828dfe12008-04-17 13:17:49 -04003914 case F_GETLK64:
3915 case F_SETLK64:
3916 case F_SETLKW64:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917#endif
David Howells88e67f32008-11-14 10:39:21 +11003918 err = file_has_perm(cred, file, FILE__LOCK);
Eric Paris828dfe12008-04-17 13:17:49 -04003919 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920 }
3921
3922 return err;
3923}
3924
Jeff Laytone0b93ed2014-08-22 11:27:32 -04003925static void selinux_file_set_fowner(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 struct file_security_struct *fsec;
3928
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07003929 fsec = selinux_file(file);
David Howells275bb412008-11-14 10:39:19 +11003930 fsec->fown_sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931}
3932
3933static int selinux_file_send_sigiotask(struct task_struct *tsk,
3934 struct fown_struct *fown, int signum)
3935{
Eric Paris828dfe12008-04-17 13:17:49 -04003936 struct file *file;
Paul Mooreeb1231f2021-02-18 15:13:40 -05003937 u32 sid = task_sid_obj(tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 struct file_security_struct *fsec;
3940
3941 /* struct fown_struct is never outside the context of a struct file */
Eric Paris828dfe12008-04-17 13:17:49 -04003942 file = container_of(fown, struct file, f_owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07003944 fsec = selinux_file(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945
3946 if (!signum)
3947 perm = signal_to_av(SIGIO); /* as per send_sigio_to_task */
3948 else
3949 perm = signal_to_av(signum);
3950
Stephen Smalleye67b7982023-03-09 13:30:37 -05003951 return avc_has_perm(fsec->fown_sid, sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 SECCLASS_PROCESS, perm, NULL);
3953}
3954
3955static int selinux_file_receive(struct file *file)
3956{
David Howells88e67f32008-11-14 10:39:21 +11003957 const struct cred *cred = current_cred();
3958
3959 return file_has_perm(cred, file, file_to_av(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960}
3961
Al Viro94817692018-07-10 14:13:18 -04003962static int selinux_file_open(struct file *file)
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003963{
3964 struct file_security_struct *fsec;
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003965 struct inode_security_struct *isec;
David Howellsd84f4f92008-11-14 10:39:23 +11003966
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07003967 fsec = selinux_file(file);
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05003968 isec = inode_security(file_inode(file));
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003969 /*
3970 * Save inode label and policy sequence number
3971 * at open-time so that selinux_file_permission
3972 * can determine whether revalidation is necessary.
3973 * Task label is already saved in the file security
3974 * struct as its SID.
3975 */
3976 fsec->isid = isec->sid;
Stephen Smalleye67b7982023-03-09 13:30:37 -05003977 fsec->pseqno = avc_policy_seqno();
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003978 /*
3979 * Since the inode label or policy seqno may have changed
3980 * between the selinux_inode_permission check and the saving
3981 * of state above, recheck that access is still permitted.
3982 * Otherwise, access might never be revalidated against the
3983 * new inode label or new policy.
3984 * This check is not redundant - do not remove.
3985 */
Al Viro94817692018-07-10 14:13:18 -04003986 return file_path_has_perm(file->f_cred, file, open_file_to_av(file));
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09003987}
3988
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989/* task security operations */
3990
Tetsuo Handaa79be232017-03-28 23:08:45 +09003991static int selinux_task_alloc(struct task_struct *task,
3992 unsigned long clone_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05003994 u32 sid = current_sid();
3995
Stephen Smalleye67b7982023-03-09 13:30:37 -05003996 return avc_has_perm(sid, sid, SECCLASS_PROCESS, PROCESS__FORK, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997}
3998
David Howellsf1752ee2008-11-14 10:39:17 +11003999/*
David Howellsd84f4f92008-11-14 10:39:23 +11004000 * prepare a new set of credentials for modification
4001 */
4002static int selinux_cred_prepare(struct cred *new, const struct cred *old,
4003 gfp_t gfp)
4004{
Casey Schauflerbbd36622018-11-12 09:30:56 -08004005 const struct task_security_struct *old_tsec = selinux_cred(old);
4006 struct task_security_struct *tsec = selinux_cred(new);
David Howellsd84f4f92008-11-14 10:39:23 +11004007
Casey Schauflerbbd36622018-11-12 09:30:56 -08004008 *tsec = *old_tsec;
David Howellsd84f4f92008-11-14 10:39:23 +11004009 return 0;
4010}
4011
4012/*
David Howellsee18d642009-09-02 09:14:21 +01004013 * transfer the SELinux data to a blank set of creds
4014 */
4015static void selinux_cred_transfer(struct cred *new, const struct cred *old)
4016{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07004017 const struct task_security_struct *old_tsec = selinux_cred(old);
4018 struct task_security_struct *tsec = selinux_cred(new);
David Howellsee18d642009-09-02 09:14:21 +01004019
4020 *tsec = *old_tsec;
4021}
4022
Matthew Garrett3ec30112018-01-08 13:36:19 -08004023static void selinux_cred_getsecid(const struct cred *c, u32 *secid)
4024{
4025 *secid = cred_sid(c);
4026}
4027
David Howellsee18d642009-09-02 09:14:21 +01004028/*
David Howells3a3b7ce2008-11-14 10:39:28 +11004029 * set the security data for a kernel service
4030 * - all the creation contexts are set to unlabelled
4031 */
4032static int selinux_kernel_act_as(struct cred *new, u32 secid)
4033{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07004034 struct task_security_struct *tsec = selinux_cred(new);
David Howells3a3b7ce2008-11-14 10:39:28 +11004035 u32 sid = current_sid();
4036 int ret;
4037
Stephen Smalleye67b7982023-03-09 13:30:37 -05004038 ret = avc_has_perm(sid, secid,
David Howells3a3b7ce2008-11-14 10:39:28 +11004039 SECCLASS_KERNEL_SERVICE,
4040 KERNEL_SERVICE__USE_AS_OVERRIDE,
4041 NULL);
4042 if (ret == 0) {
4043 tsec->sid = secid;
4044 tsec->create_sid = 0;
4045 tsec->keycreate_sid = 0;
4046 tsec->sockcreate_sid = 0;
4047 }
4048 return ret;
4049}
4050
4051/*
4052 * set the file creation context in a security record to the same as the
4053 * objective context of the specified inode
4054 */
4055static int selinux_kernel_create_files_as(struct cred *new, struct inode *inode)
4056{
Andreas Gruenbacher83da53c52015-12-24 11:09:39 -05004057 struct inode_security_struct *isec = inode_security(inode);
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07004058 struct task_security_struct *tsec = selinux_cred(new);
David Howells3a3b7ce2008-11-14 10:39:28 +11004059 u32 sid = current_sid();
4060 int ret;
4061
Stephen Smalleye67b7982023-03-09 13:30:37 -05004062 ret = avc_has_perm(sid, isec->sid,
David Howells3a3b7ce2008-11-14 10:39:28 +11004063 SECCLASS_KERNEL_SERVICE,
4064 KERNEL_SERVICE__CREATE_FILES_AS,
4065 NULL);
4066
4067 if (ret == 0)
4068 tsec->create_sid = isec->sid;
David Howellsef574712010-02-26 01:56:16 +00004069 return ret;
David Howells3a3b7ce2008-11-14 10:39:28 +11004070}
4071
Eric Parisdd8dbf22009-11-03 16:35:32 +11004072static int selinux_kernel_module_request(char *kmod_name)
Eric Paris25354c42009-08-13 09:45:03 -04004073{
Eric Parisdd8dbf22009-11-03 16:35:32 +11004074 struct common_audit_data ad;
4075
Eric Paris50c205f2012-04-04 15:01:43 -04004076 ad.type = LSM_AUDIT_DATA_KMOD;
Eric Parisdd8dbf22009-11-03 16:35:32 +11004077 ad.u.kmod_name = kmod_name;
4078
Stephen Smalleye67b7982023-03-09 13:30:37 -05004079 return avc_has_perm(current_sid(), SECINITSID_KERNEL, SECCLASS_SYSTEM,
Eric Parisdd8dbf22009-11-03 16:35:32 +11004080 SYSTEM__MODULE_REQUEST, &ad);
Eric Paris25354c42009-08-13 09:45:03 -04004081}
4082
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07004083static int selinux_kernel_module_from_file(struct file *file)
4084{
4085 struct common_audit_data ad;
4086 struct inode_security_struct *isec;
4087 struct file_security_struct *fsec;
4088 u32 sid = current_sid();
4089 int rc;
4090
4091 /* init_module */
4092 if (file == NULL)
Stephen Smalleye67b7982023-03-09 13:30:37 -05004093 return avc_has_perm(sid, sid, SECCLASS_SYSTEM,
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07004094 SYSTEM__MODULE_LOAD, NULL);
4095
4096 /* finit_module */
Paul Moore20cdef82016-04-04 14:14:42 -04004097
Vivek Goyal43af5de2016-09-09 11:37:49 -04004098 ad.type = LSM_AUDIT_DATA_FILE;
4099 ad.u.file = file;
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07004100
Casey Schauflerbb6c6b02018-09-21 17:22:32 -07004101 fsec = selinux_file(file);
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07004102 if (sid != fsec->sid) {
Stephen Smalleye67b7982023-03-09 13:30:37 -05004103 rc = avc_has_perm(sid, fsec->sid, SECCLASS_FD, FD__USE, &ad);
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07004104 if (rc)
4105 return rc;
4106 }
4107
Paul Moore20cdef82016-04-04 14:14:42 -04004108 isec = inode_security(file_inode(file));
Stephen Smalleye67b7982023-03-09 13:30:37 -05004109 return avc_has_perm(sid, isec->sid, SECCLASS_SYSTEM,
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07004110 SYSTEM__MODULE_LOAD, &ad);
4111}
4112
4113static int selinux_kernel_read_file(struct file *file,
Kees Cook2039bda2020-10-02 10:38:23 -07004114 enum kernel_read_file_id id,
4115 bool contents)
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07004116{
4117 int rc = 0;
4118
4119 switch (id) {
4120 case READING_MODULE:
Kees Cook2039bda2020-10-02 10:38:23 -07004121 rc = selinux_kernel_module_from_file(contents ? file : NULL);
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07004122 break;
4123 default:
4124 break;
4125 }
4126
4127 return rc;
4128}
4129
Kees Cookb64fcae2020-10-02 10:38:20 -07004130static int selinux_kernel_load_data(enum kernel_load_data_id id, bool contents)
Mimi Zoharc77b8cd2018-07-13 14:06:02 -04004131{
4132 int rc = 0;
4133
4134 switch (id) {
4135 case LOADING_MODULE:
4136 rc = selinux_kernel_module_from_file(NULL);
Gustavo A. R. Silvab2d99bc2020-11-20 12:32:26 -06004137 break;
Mimi Zoharc77b8cd2018-07-13 14:06:02 -04004138 default:
4139 break;
4140 }
4141
4142 return rc;
4143}
4144
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145static int selinux_task_setpgid(struct task_struct *p, pid_t pgid)
4146{
Stephen Smalleye67b7982023-03-09 13:30:37 -05004147 return avc_has_perm(current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004148 PROCESS__SETPGID, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149}
4150
4151static int selinux_task_getpgid(struct task_struct *p)
4152{
Stephen Smalleye67b7982023-03-09 13:30:37 -05004153 return avc_has_perm(current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004154 PROCESS__GETPGID, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155}
4156
4157static int selinux_task_getsid(struct task_struct *p)
4158{
Stephen Smalleye67b7982023-03-09 13:30:37 -05004159 return avc_has_perm(current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004160 PROCESS__GETSESSION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161}
4162
Paul Moore63269482021-09-29 11:01:21 -04004163static void selinux_current_getsecid_subj(u32 *secid)
David Quigleyf9008e4c2006-06-30 01:55:46 -07004164{
Paul Moore63269482021-09-29 11:01:21 -04004165 *secid = current_sid();
Paul Mooreeb1231f2021-02-18 15:13:40 -05004166}
4167
4168static void selinux_task_getsecid_obj(struct task_struct *p, u32 *secid)
4169{
4170 *secid = task_sid_obj(p);
David Quigleyf9008e4c2006-06-30 01:55:46 -07004171}
4172
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173static int selinux_task_setnice(struct task_struct *p, int nice)
4174{
Stephen Smalleye67b7982023-03-09 13:30:37 -05004175 return avc_has_perm(current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004176 PROCESS__SETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177}
4178
James Morris03e68062006-06-23 02:03:58 -07004179static int selinux_task_setioprio(struct task_struct *p, int ioprio)
4180{
Stephen Smalleye67b7982023-03-09 13:30:37 -05004181 return avc_has_perm(current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004182 PROCESS__SETSCHED, NULL);
James Morris03e68062006-06-23 02:03:58 -07004183}
4184
David Quigleya1836a42006-06-30 01:55:49 -07004185static int selinux_task_getioprio(struct task_struct *p)
4186{
Stephen Smalleye67b7982023-03-09 13:30:37 -05004187 return avc_has_perm(current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004188 PROCESS__GETSCHED, NULL);
David Quigleya1836a42006-06-30 01:55:49 -07004189}
4190
Corentin LABBE42985552017-10-04 20:32:18 +02004191static int selinux_task_prlimit(const struct cred *cred, const struct cred *tcred,
4192 unsigned int flags)
Stephen Smalley791ec492017-02-17 07:57:00 -05004193{
4194 u32 av = 0;
4195
Stephen Smalley84e68852017-02-28 09:35:08 -05004196 if (!flags)
4197 return 0;
Stephen Smalley791ec492017-02-17 07:57:00 -05004198 if (flags & LSM_PRLIMIT_WRITE)
4199 av |= PROCESS__SETRLIMIT;
4200 if (flags & LSM_PRLIMIT_READ)
4201 av |= PROCESS__GETRLIMIT;
Stephen Smalleye67b7982023-03-09 13:30:37 -05004202 return avc_has_perm(cred_sid(cred), cred_sid(tcred),
Stephen Smalley791ec492017-02-17 07:57:00 -05004203 SECCLASS_PROCESS, av, NULL);
4204}
4205
Jiri Slaby8fd00b42009-08-26 18:41:16 +02004206static int selinux_task_setrlimit(struct task_struct *p, unsigned int resource,
4207 struct rlimit *new_rlim)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208{
Jiri Slaby8fd00b42009-08-26 18:41:16 +02004209 struct rlimit *old_rlim = p->signal->rlim + resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210
4211 /* Control the ability to change the hard limit (whether
4212 lowering or raising it), so that the hard limit can
4213 later be used as a safe reset point for the soft limit
David Howellsd84f4f92008-11-14 10:39:23 +11004214 upon context transitions. See selinux_bprm_committing_creds. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 if (old_rlim->rlim_max != new_rlim->rlim_max)
Stephen Smalleye67b7982023-03-09 13:30:37 -05004216 return avc_has_perm(current_sid(), task_sid_obj(p),
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004217 SECCLASS_PROCESS, PROCESS__SETRLIMIT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218
4219 return 0;
4220}
4221
KOSAKI Motohirob0ae1982010-10-15 04:21:18 +09004222static int selinux_task_setscheduler(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223{
Stephen Smalleye67b7982023-03-09 13:30:37 -05004224 return avc_has_perm(current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004225 PROCESS__SETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226}
4227
4228static int selinux_task_getscheduler(struct task_struct *p)
4229{
Stephen Smalleye67b7982023-03-09 13:30:37 -05004230 return avc_has_perm(current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004231 PROCESS__GETSCHED, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232}
4233
David Quigley35601542006-06-23 02:04:01 -07004234static int selinux_task_movememory(struct task_struct *p)
4235{
Stephen Smalleye67b7982023-03-09 13:30:37 -05004236 return avc_has_perm(current_sid(), task_sid_obj(p), SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004237 PROCESS__SETSCHED, NULL);
David Quigley35601542006-06-23 02:04:01 -07004238}
4239
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02004240static int selinux_task_kill(struct task_struct *p, struct kernel_siginfo *info,
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004241 int sig, const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242{
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004243 u32 secid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 u32 perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 if (!sig)
4247 perm = PROCESS__SIGNULL; /* null signal; existence test */
4248 else
4249 perm = signal_to_av(sig);
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004250 if (!cred)
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004251 secid = current_sid();
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04004252 else
4253 secid = cred_sid(cred);
Stephen Smalleye67b7982023-03-09 13:30:37 -05004254 return avc_has_perm(secid, task_sid_obj(p), SECCLASS_PROCESS, perm, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255}
4256
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257static void selinux_task_to_inode(struct task_struct *p,
4258 struct inode *inode)
4259{
Casey Schaufler80788c22018-09-21 17:19:11 -07004260 struct inode_security_struct *isec = selinux_inode(inode);
Paul Mooreeb1231f2021-02-18 15:13:40 -05004261 u32 sid = task_sid_obj(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004263 spin_lock(&isec->lock);
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01004264 isec->sclass = inode_mode_to_security_class(inode->i_mode);
David Howells275bb412008-11-14 10:39:19 +11004265 isec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004266 isec->initialized = LABEL_INITIALIZED;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004267 spin_unlock(&isec->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268}
4269
Frederick Lawlered5d44d2022-08-15 11:20:28 -05004270static int selinux_userns_create(const struct cred *cred)
4271{
4272 u32 sid = current_sid();
4273
Stephen Smalleye67b7982023-03-09 13:30:37 -05004274 return avc_has_perm(sid, sid, SECCLASS_USER_NAMESPACE,
4275 USER_NAMESPACE__CREATE, NULL);
Frederick Lawlered5d44d2022-08-15 11:20:28 -05004276}
4277
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004279static int selinux_parse_skb_ipv4(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04004280 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281{
4282 int offset, ihlen, ret = -EINVAL;
4283 struct iphdr _iph, *ih;
4284
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03004285 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 ih = skb_header_pointer(skb, offset, sizeof(_iph), &_iph);
4287 if (ih == NULL)
4288 goto out;
4289
4290 ihlen = ih->ihl * 4;
4291 if (ihlen < sizeof(_iph))
4292 goto out;
4293
Eric Paris48c62af2012-04-02 13:15:44 -04004294 ad->u.net->v4info.saddr = ih->saddr;
4295 ad->u.net->v4info.daddr = ih->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 ret = 0;
4297
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004298 if (proto)
4299 *proto = ih->protocol;
4300
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 switch (ih->protocol) {
Eric Paris828dfe12008-04-17 13:17:49 -04004302 case IPPROTO_TCP: {
4303 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304
Eric Paris828dfe12008-04-17 13:17:49 -04004305 if (ntohs(ih->frag_off) & IP_OFFSET)
4306 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307
4308 offset += ihlen;
4309 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4310 if (th == NULL)
4311 break;
4312
Eric Paris48c62af2012-04-02 13:15:44 -04004313 ad->u.net->sport = th->source;
4314 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317
Eric Paris828dfe12008-04-17 13:17:49 -04004318 case IPPROTO_UDP: {
4319 struct udphdr _udph, *uh;
4320
4321 if (ntohs(ih->frag_off) & IP_OFFSET)
4322 break;
4323
4324 offset += ihlen;
4325 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4326 if (uh == NULL)
4327 break;
4328
Eric Paris48c62af2012-04-02 13:15:44 -04004329 ad->u.net->sport = uh->source;
4330 ad->u.net->dport = uh->dest;
Eric Paris828dfe12008-04-17 13:17:49 -04004331 break;
4332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333
James Morris2ee92d42006-11-13 16:09:01 -08004334 case IPPROTO_DCCP: {
4335 struct dccp_hdr _dccph, *dh;
4336
4337 if (ntohs(ih->frag_off) & IP_OFFSET)
4338 break;
4339
4340 offset += ihlen;
4341 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4342 if (dh == NULL)
4343 break;
4344
Eric Paris48c62af2012-04-02 13:15:44 -04004345 ad->u.net->sport = dh->dccph_sport;
4346 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08004347 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004348 }
James Morris2ee92d42006-11-13 16:09:01 -08004349
Richard Hainesd4529302018-02-13 20:57:18 +00004350#if IS_ENABLED(CONFIG_IP_SCTP)
4351 case IPPROTO_SCTP: {
4352 struct sctphdr _sctph, *sh;
4353
4354 if (ntohs(ih->frag_off) & IP_OFFSET)
4355 break;
4356
4357 offset += ihlen;
4358 sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4359 if (sh == NULL)
4360 break;
4361
4362 ad->u.net->sport = sh->source;
4363 ad->u.net->dport = sh->dest;
4364 break;
4365 }
4366#endif
Eric Paris828dfe12008-04-17 13:17:49 -04004367 default:
4368 break;
4369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370out:
4371 return ret;
4372}
4373
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04004374#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375
4376/* Returns error only if unable to parse addresses */
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004377static int selinux_parse_skb_ipv6(struct sk_buff *skb,
Thomas Liu2bf49692009-07-14 12:14:09 -04004378 struct common_audit_data *ad, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379{
4380 u8 nexthdr;
4381 int ret = -EINVAL, offset;
4382 struct ipv6hdr _ipv6h, *ip6;
Jesse Gross75f28112011-11-30 17:05:51 -08004383 __be16 frag_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03004385 offset = skb_network_offset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 ip6 = skb_header_pointer(skb, offset, sizeof(_ipv6h), &_ipv6h);
4387 if (ip6 == NULL)
4388 goto out;
4389
Eric Paris48c62af2012-04-02 13:15:44 -04004390 ad->u.net->v6info.saddr = ip6->saddr;
4391 ad->u.net->v6info.daddr = ip6->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 ret = 0;
4393
4394 nexthdr = ip6->nexthdr;
4395 offset += sizeof(_ipv6h);
Jesse Gross75f28112011-11-30 17:05:51 -08004396 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 if (offset < 0)
4398 goto out;
4399
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004400 if (proto)
4401 *proto = nexthdr;
4402
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 switch (nexthdr) {
4404 case IPPROTO_TCP: {
Eric Paris828dfe12008-04-17 13:17:49 -04004405 struct tcphdr _tcph, *th;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406
4407 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
4408 if (th == NULL)
4409 break;
4410
Eric Paris48c62af2012-04-02 13:15:44 -04004411 ad->u.net->sport = th->source;
4412 ad->u.net->dport = th->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 break;
4414 }
4415
4416 case IPPROTO_UDP: {
4417 struct udphdr _udph, *uh;
4418
4419 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
4420 if (uh == NULL)
4421 break;
4422
Eric Paris48c62af2012-04-02 13:15:44 -04004423 ad->u.net->sport = uh->source;
4424 ad->u.net->dport = uh->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 break;
4426 }
4427
James Morris2ee92d42006-11-13 16:09:01 -08004428 case IPPROTO_DCCP: {
4429 struct dccp_hdr _dccph, *dh;
4430
4431 dh = skb_header_pointer(skb, offset, sizeof(_dccph), &_dccph);
4432 if (dh == NULL)
4433 break;
4434
Eric Paris48c62af2012-04-02 13:15:44 -04004435 ad->u.net->sport = dh->dccph_sport;
4436 ad->u.net->dport = dh->dccph_dport;
James Morris2ee92d42006-11-13 16:09:01 -08004437 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004438 }
James Morris2ee92d42006-11-13 16:09:01 -08004439
Richard Hainesd4529302018-02-13 20:57:18 +00004440#if IS_ENABLED(CONFIG_IP_SCTP)
4441 case IPPROTO_SCTP: {
4442 struct sctphdr _sctph, *sh;
4443
4444 sh = skb_header_pointer(skb, offset, sizeof(_sctph), &_sctph);
4445 if (sh == NULL)
4446 break;
4447
4448 ad->u.net->sport = sh->source;
4449 ad->u.net->dport = sh->dest;
4450 break;
4451 }
4452#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 /* includes fragments */
4454 default:
4455 break;
4456 }
4457out:
4458 return ret;
4459}
4460
4461#endif /* IPV6 */
4462
Thomas Liu2bf49692009-07-14 12:14:09 -04004463static int selinux_parse_skb(struct sk_buff *skb, struct common_audit_data *ad,
David Howellscf9481e2008-07-27 21:31:07 +10004464 char **_addrp, int src, u8 *proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465{
David Howellscf9481e2008-07-27 21:31:07 +10004466 char *addrp;
4467 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468
Eric Paris48c62af2012-04-02 13:15:44 -04004469 switch (ad->u.net->family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 case PF_INET:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004471 ret = selinux_parse_skb_ipv4(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10004472 if (ret)
4473 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04004474 addrp = (char *)(src ? &ad->u.net->v4info.saddr :
4475 &ad->u.net->v4info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10004476 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04004478#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 case PF_INET6:
Venkat Yekkirala67f83cb2006-11-08 17:04:26 -06004480 ret = selinux_parse_skb_ipv6(skb, ad, proto);
David Howellscf9481e2008-07-27 21:31:07 +10004481 if (ret)
4482 goto parse_error;
Eric Paris48c62af2012-04-02 13:15:44 -04004483 addrp = (char *)(src ? &ad->u.net->v6info.saddr :
4484 &ad->u.net->v6info.daddr);
David Howellscf9481e2008-07-27 21:31:07 +10004485 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486#endif /* IPV6 */
4487 default:
David Howellscf9481e2008-07-27 21:31:07 +10004488 addrp = NULL;
4489 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 }
4491
David Howellscf9481e2008-07-27 21:31:07 +10004492parse_error:
peter enderborgc103a912018-06-12 10:09:03 +02004493 pr_warn(
David Howellscf9481e2008-07-27 21:31:07 +10004494 "SELinux: failure in selinux_parse_skb(),"
4495 " unable to parse packet\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 return ret;
David Howellscf9481e2008-07-27 21:31:07 +10004497
4498okay:
4499 if (_addrp)
4500 *_addrp = addrp;
4501 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502}
4503
Paul Moore4f6a9932007-03-01 14:35:22 -05004504/**
Paul Moore220deb92008-01-29 08:38:23 -05004505 * selinux_skb_peerlbl_sid - Determine the peer label of a packet
Paul Moore4f6a9932007-03-01 14:35:22 -05004506 * @skb: the packet
Paul Moore75e22912008-01-29 08:38:04 -05004507 * @family: protocol family
Paul Moore220deb92008-01-29 08:38:23 -05004508 * @sid: the packet's peer label SID
Paul Moore4f6a9932007-03-01 14:35:22 -05004509 *
4510 * Description:
Paul Moore220deb92008-01-29 08:38:23 -05004511 * Check the various different forms of network peer labeling and determine
4512 * the peer label/SID for the packet; most of the magic actually occurs in
4513 * the security server function security_net_peersid_cmp(). The function
4514 * returns zero if the value in @sid is valid (although it may be SECSID_NULL)
4515 * or -EACCES if @sid is invalid due to inconsistencies with the different
4516 * peer labels.
Paul Moore4f6a9932007-03-01 14:35:22 -05004517 *
4518 */
Paul Moore220deb92008-01-29 08:38:23 -05004519static int selinux_skb_peerlbl_sid(struct sk_buff *skb, u16 family, u32 *sid)
Paul Moore4f6a9932007-03-01 14:35:22 -05004520{
Paul Moore71f1cb02008-01-29 08:51:16 -05004521 int err;
Paul Moore4f6a9932007-03-01 14:35:22 -05004522 u32 xfrm_sid;
4523 u32 nlbl_sid;
Paul Moore220deb92008-01-29 08:38:23 -05004524 u32 nlbl_type;
Paul Moore4f6a9932007-03-01 14:35:22 -05004525
Paul Moore817eff72013-12-10 14:57:54 -05004526 err = selinux_xfrm_skb_sid(skb, &xfrm_sid);
Paul Moorebed4d7e2013-07-23 17:38:40 -04004527 if (unlikely(err))
4528 return -EACCES;
4529 err = selinux_netlbl_skbuff_getsid(skb, family, &nlbl_type, &nlbl_sid);
4530 if (unlikely(err))
4531 return -EACCES;
Paul Moore220deb92008-01-29 08:38:23 -05004532
Stephen Smalleye67b7982023-03-09 13:30:37 -05004533 err = security_net_peersid_resolve(nlbl_sid,
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004534 nlbl_type, xfrm_sid, sid);
Paul Moore71f1cb02008-01-29 08:51:16 -05004535 if (unlikely(err)) {
peter enderborgc103a912018-06-12 10:09:03 +02004536 pr_warn(
Paul Moore71f1cb02008-01-29 08:51:16 -05004537 "SELinux: failure in selinux_skb_peerlbl_sid(),"
4538 " unable to determine packet's peer label\n");
Paul Moore220deb92008-01-29 08:38:23 -05004539 return -EACCES;
Paul Moore71f1cb02008-01-29 08:51:16 -05004540 }
Paul Moore220deb92008-01-29 08:38:23 -05004541
4542 return 0;
Paul Moore4f6a9932007-03-01 14:35:22 -05004543}
4544
Paul Moore446b8022013-12-04 16:10:51 -05004545/**
4546 * selinux_conn_sid - Determine the child socket label for a connection
4547 * @sk_sid: the parent socket's SID
4548 * @skb_sid: the packet's SID
4549 * @conn_sid: the resulting connection SID
4550 *
4551 * If @skb_sid is valid then the user:role:type information from @sk_sid is
4552 * combined with the MLS information from @skb_sid in order to create
Randy Dunlapc76a2f92020-08-07 09:51:34 -07004553 * @conn_sid. If @skb_sid is not valid then @conn_sid is simply a copy
Paul Moore446b8022013-12-04 16:10:51 -05004554 * of @sk_sid. Returns zero on success, negative values on failure.
4555 *
4556 */
4557static int selinux_conn_sid(u32 sk_sid, u32 skb_sid, u32 *conn_sid)
4558{
4559 int err = 0;
4560
4561 if (skb_sid != SECSID_NULL)
Stephen Smalleye67b7982023-03-09 13:30:37 -05004562 err = security_sid_mls_copy(sk_sid, skb_sid,
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004563 conn_sid);
Paul Moore446b8022013-12-04 16:10:51 -05004564 else
4565 *conn_sid = sk_sid;
4566
4567 return err;
4568}
4569
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570/* socket security operations */
Paul Moored4f2d972010-04-22 14:46:18 -04004571
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004572static int socket_sockcreate_sid(const struct task_security_struct *tsec,
4573 u16 secclass, u32 *socksid)
Paul Moored4f2d972010-04-22 14:46:18 -04004574{
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004575 if (tsec->sockcreate_sid > SECSID_NULL) {
4576 *socksid = tsec->sockcreate_sid;
4577 return 0;
4578 }
4579
Stephen Smalleye67b7982023-03-09 13:30:37 -05004580 return security_transition_sid(tsec->sid, tsec->sid,
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05004581 secclass, NULL, socksid);
Paul Moored4f2d972010-04-22 14:46:18 -04004582}
4583
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004584static int sock_has_perm(struct sock *sk, u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585{
Paul Moore253bfae2010-04-22 14:46:19 -04004586 struct sk_security_struct *sksec = sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04004587 struct common_audit_data ad;
Paolo Abenidd51fcd2023-07-19 13:37:49 +02004588 struct lsm_network_audit net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589
Paul Moore253bfae2010-04-22 14:46:19 -04004590 if (sksec->sid == SECINITSID_KERNEL)
4591 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592
Ondrej Mosnacekae254852023-11-14 16:51:16 +01004593 /*
4594 * Before POLICYDB_CAP_USERSPACE_INITIAL_CONTEXT, sockets that
4595 * inherited the kernel context from early boot used to be skipped
4596 * here, so preserve that behavior unless the capability is set.
4597 *
4598 * By setting the capability the policy signals that it is ready
4599 * for this quirk to be fixed. Note that sockets created by a kernel
4600 * thread or a usermode helper executed without a transition will
4601 * still be skipped in this check regardless of the policycap
4602 * setting.
4603 */
4604 if (!selinux_policycap_userspace_initial_context() &&
4605 sksec->sid == SECINITSID_INIT)
4606 return 0;
4607
Paolo Abenidd51fcd2023-07-19 13:37:49 +02004608 ad_net_init_from_sk(&ad, &net, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609
Stephen Smalleye67b7982023-03-09 13:30:37 -05004610 return avc_has_perm(current_sid(), sksec->sid, sksec->sclass, perms,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004611 &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612}
4613
4614static int selinux_socket_create(int family, int type,
4615 int protocol, int kern)
4616{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07004617 const struct task_security_struct *tsec = selinux_cred(current_cred());
Paul Moored4f2d972010-04-22 14:46:18 -04004618 u32 newsid;
David Howells275bb412008-11-14 10:39:19 +11004619 u16 secclass;
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004620 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621
4622 if (kern)
Paul Moored4f2d972010-04-22 14:46:18 -04004623 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624
David Howells275bb412008-11-14 10:39:19 +11004625 secclass = socket_type_to_security_class(family, type, protocol);
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004626 rc = socket_sockcreate_sid(tsec, secclass, &newsid);
4627 if (rc)
4628 return rc;
4629
Stephen Smalleye67b7982023-03-09 13:30:37 -05004630 return avc_has_perm(tsec->sid, newsid, secclass, SOCKET__CREATE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631}
4632
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07004633static int selinux_socket_post_create(struct socket *sock, int family,
4634 int type, int protocol, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635{
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07004636 const struct task_security_struct *tsec = selinux_cred(current_cred());
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05004637 struct inode_security_struct *isec = inode_security_novalidate(SOCK_INODE(sock));
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004638 struct sk_security_struct *sksec;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004639 u16 sclass = socket_type_to_security_class(family, type, protocol);
4640 u32 sid = SECINITSID_KERNEL;
David Howells275bb412008-11-14 10:39:19 +11004641 int err = 0;
4642
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004643 if (!kern) {
4644 err = socket_sockcreate_sid(tsec, sclass, &sid);
Harry Ciao2ad18bd2011-03-02 13:32:34 +08004645 if (err)
4646 return err;
4647 }
David Howells275bb412008-11-14 10:39:19 +11004648
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004649 isec->sclass = sclass;
4650 isec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004651 isec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004653 if (sock->sk) {
4654 sksec = sock->sk->sk_security;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004655 sksec->sclass = sclass;
4656 sksec->sid = sid;
Richard Hainesd4529302018-02-13 20:57:18 +00004657 /* Allows detection of the first association on this socket */
4658 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4659 sksec->sctp_assoc_state = SCTP_ASSOC_UNSET;
4660
Paul Moore389fb8002009-03-27 17:10:34 -04004661 err = selinux_netlbl_socket_post_create(sock->sk, family);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07004662 }
4663
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07004664 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665}
4666
David Herrmann0b811db2018-05-04 16:28:21 +02004667static int selinux_socket_socketpair(struct socket *socka,
4668 struct socket *sockb)
4669{
4670 struct sk_security_struct *sksec_a = socka->sk->sk_security;
4671 struct sk_security_struct *sksec_b = sockb->sk->sk_security;
4672
4673 sksec_a->peer_sid = sksec_b->sid;
4674 sksec_b->peer_sid = sksec_a->sid;
4675
4676 return 0;
4677}
4678
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679/* Range of port numbers used to automatically bind.
4680 Need to determine whether we should perform a name_bind
4681 permission check between the socket and the port number. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682
4683static int selinux_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
4684{
Paul Moore253bfae2010-04-22 14:46:19 -04004685 struct sock *sk = sock->sk;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004686 struct sk_security_struct *sksec = sk->sk_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687 u16 family;
4688 int err;
4689
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004690 err = sock_has_perm(sk, SOCKET__BIND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 if (err)
4692 goto out;
4693
Richard Hainesd4529302018-02-13 20:57:18 +00004694 /* If PF_INET or PF_INET6, check name_bind permission for the port. */
Paul Moore253bfae2010-04-22 14:46:19 -04004695 family = sk->sk_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 if (family == PF_INET || family == PF_INET6) {
4697 char *addrp;
Thomas Liu2bf49692009-07-14 12:14:09 -04004698 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004699 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 struct sockaddr_in *addr4 = NULL;
4701 struct sockaddr_in6 *addr6 = NULL;
Tetsuo Handac750e692019-04-12 19:59:34 +09004702 u16 family_sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 unsigned short snum;
James Morrise399f982008-06-12 01:39:58 +10004704 u32 sid, node_perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705
Richard Hainesd4529302018-02-13 20:57:18 +00004706 /*
4707 * sctp_bindx(3) calls via selinux_sctp_bind_connect()
4708 * that validates multiple binding addresses. Because of this
4709 * need to check address->sa_family as it is possible to have
4710 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4711 */
Tetsuo Handac750e692019-04-12 19:59:34 +09004712 if (addrlen < offsetofend(struct sockaddr, sa_family))
4713 return -EINVAL;
4714 family_sa = address->sa_family;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004715 switch (family_sa) {
4716 case AF_UNSPEC:
Richard Haines68741a8a2018-03-02 19:54:34 +00004717 case AF_INET:
4718 if (addrlen < sizeof(struct sockaddr_in))
4719 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720 addr4 = (struct sockaddr_in *)address;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004721 if (family_sa == AF_UNSPEC) {
Mickaël Salaünbbf5a1d2024-01-03 17:34:15 +01004722 if (family == PF_INET6) {
4723 /* Length check from inet6_bind_sk() */
4724 if (addrlen < SIN6_LEN_RFC2133)
4725 return -EINVAL;
4726 /* Family check from __inet6_bind() */
4727 goto err_af;
4728 }
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004729 /* see __inet_bind(), we only want to allow
4730 * AF_UNSPEC if the address is INADDR_ANY
4731 */
4732 if (addr4->sin_addr.s_addr != htonl(INADDR_ANY))
4733 goto err_af;
4734 family_sa = AF_INET;
4735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 snum = ntohs(addr4->sin_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737 addrp = (char *)&addr4->sin_addr.s_addr;
Richard Haines68741a8a2018-03-02 19:54:34 +00004738 break;
4739 case AF_INET6:
4740 if (addrlen < SIN6_LEN_RFC2133)
4741 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742 addr6 = (struct sockaddr_in6 *)address;
4743 snum = ntohs(addr6->sin6_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 addrp = (char *)&addr6->sin6_addr.s6_addr;
Richard Haines68741a8a2018-03-02 19:54:34 +00004745 break;
4746 default:
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004747 goto err_af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748 }
4749
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004750 ad.type = LSM_AUDIT_DATA_NET;
4751 ad.u.net = &net;
4752 ad.u.net->sport = htons(snum);
4753 ad.u.net->family = family_sa;
4754
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004755 if (snum) {
4756 int low, high;
4757
Eric W. Biederman0bbf87d2013-09-28 14:10:59 -07004758 inet_get_local_port_range(sock_net(sk), &low, &high);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004759
Maciej Żenczykowski82f31eb2019-11-25 15:37:04 -08004760 if (inet_port_requires_bind_service(sock_net(sk), snum) ||
4761 snum < low || snum > high) {
Paul Moore3e112172008-04-10 10:48:14 -04004762 err = sel_netport_sid(sk->sk_protocol,
4763 snum, &sid);
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004764 if (err)
4765 goto out;
Stephen Smalleye67b7982023-03-09 13:30:37 -05004766 err = avc_has_perm(sksec->sid, sid,
Paul Moore253bfae2010-04-22 14:46:19 -04004767 sksec->sclass,
Stephen Hemminger227b60f2007-10-10 17:30:46 -07004768 SOCKET__NAME_BIND, &ad);
4769 if (err)
4770 goto out;
4771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 }
Eric Paris828dfe12008-04-17 13:17:49 -04004773
Paul Moore253bfae2010-04-22 14:46:19 -04004774 switch (sksec->sclass) {
James Morris13402582005-09-30 14:24:34 -04004775 case SECCLASS_TCP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776 node_perm = TCP_SOCKET__NODE_BIND;
4777 break;
Eric Paris828dfe12008-04-17 13:17:49 -04004778
James Morris13402582005-09-30 14:24:34 -04004779 case SECCLASS_UDP_SOCKET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780 node_perm = UDP_SOCKET__NODE_BIND;
4781 break;
James Morris2ee92d42006-11-13 16:09:01 -08004782
4783 case SECCLASS_DCCP_SOCKET:
4784 node_perm = DCCP_SOCKET__NODE_BIND;
4785 break;
4786
Richard Hainesd4529302018-02-13 20:57:18 +00004787 case SECCLASS_SCTP_SOCKET:
4788 node_perm = SCTP_SOCKET__NODE_BIND;
4789 break;
4790
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 default:
4792 node_perm = RAWIP_SOCKET__NODE_BIND;
4793 break;
4794 }
Eric Paris828dfe12008-04-17 13:17:49 -04004795
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004796 err = sel_netnode_sid(addrp, family_sa, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797 if (err)
4798 goto out;
Eric Paris828dfe12008-04-17 13:17:49 -04004799
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004800 if (family_sa == AF_INET)
Eric Paris48c62af2012-04-02 13:15:44 -04004801 ad.u.net->v4info.saddr = addr4->sin_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802 else
Eric Paris48c62af2012-04-02 13:15:44 -04004803 ad.u.net->v6info.saddr = addr6->sin6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804
Stephen Smalleye67b7982023-03-09 13:30:37 -05004805 err = avc_has_perm(sksec->sid, sid,
Paul Moore253bfae2010-04-22 14:46:19 -04004806 sksec->sclass, node_perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 if (err)
4808 goto out;
4809 }
4810out:
4811 return err;
Alexey Kodanev0f8db8c2018-05-11 20:15:11 +03004812err_af:
4813 /* Note that SCTP services expect -EINVAL, others -EAFNOSUPPORT. */
4814 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4815 return -EINVAL;
4816 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817}
4818
Richard Hainesd4529302018-02-13 20:57:18 +00004819/* This supports connect(2) and SCTP connect services such as sctp_connectx(3)
Kees Cookd61330c2019-02-17 14:08:36 -08004820 * and sctp_sendmsg(3) as described in Documentation/security/SCTP.rst
Richard Hainesd4529302018-02-13 20:57:18 +00004821 */
4822static int selinux_socket_connect_helper(struct socket *sock,
4823 struct sockaddr *address, int addrlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824{
Paul Moore014ab192008-10-10 10:16:33 -04004825 struct sock *sk = sock->sk;
Paul Moore253bfae2010-04-22 14:46:19 -04004826 struct sk_security_struct *sksec = sk->sk_security;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827 int err;
4828
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004829 err = sock_has_perm(sk, SOCKET__CONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830 if (err)
4831 return err;
Paolo Abeni05174c92019-05-10 19:12:33 +02004832 if (addrlen < offsetofend(struct sockaddr, sa_family))
4833 return -EINVAL;
4834
4835 /* connect(AF_UNSPEC) has special handling, as it is a documented
4836 * way to disconnect the socket
4837 */
4838 if (address->sa_family == AF_UNSPEC)
4839 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840
4841 /*
Richard Hainesd4529302018-02-13 20:57:18 +00004842 * If a TCP, DCCP or SCTP socket, check name_connect permission
4843 * for the port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 */
Paul Moore253bfae2010-04-22 14:46:19 -04004845 if (sksec->sclass == SECCLASS_TCP_SOCKET ||
Richard Hainesd4529302018-02-13 20:57:18 +00004846 sksec->sclass == SECCLASS_DCCP_SOCKET ||
4847 sksec->sclass == SECCLASS_SCTP_SOCKET) {
Thomas Liu2bf49692009-07-14 12:14:09 -04004848 struct common_audit_data ad;
Eric Paris48c62af2012-04-02 13:15:44 -04004849 struct lsm_network_audit net = {0,};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 struct sockaddr_in *addr4 = NULL;
4851 struct sockaddr_in6 *addr6 = NULL;
4852 unsigned short snum;
James Morris2ee92d42006-11-13 16:09:01 -08004853 u32 sid, perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854
Richard Hainesd4529302018-02-13 20:57:18 +00004855 /* sctp_connectx(3) calls via selinux_sctp_bind_connect()
4856 * that validates multiple connect addresses. Because of this
4857 * need to check address->sa_family as it is possible to have
4858 * sk->sk_family = PF_INET6 with addr->sa_family = AF_INET.
4859 */
Richard Haines68741a8a2018-03-02 19:54:34 +00004860 switch (address->sa_family) {
4861 case AF_INET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 addr4 = (struct sockaddr_in *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004863 if (addrlen < sizeof(struct sockaddr_in))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864 return -EINVAL;
4865 snum = ntohs(addr4->sin_port);
Richard Haines68741a8a2018-03-02 19:54:34 +00004866 break;
4867 case AF_INET6:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868 addr6 = (struct sockaddr_in6 *)address;
Stephen Smalley911656f2005-07-28 21:16:21 -07004869 if (addrlen < SIN6_LEN_RFC2133)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870 return -EINVAL;
4871 snum = ntohs(addr6->sin6_port);
Richard Haines68741a8a2018-03-02 19:54:34 +00004872 break;
4873 default:
4874 /* Note that SCTP services expect -EINVAL, whereas
4875 * others expect -EAFNOSUPPORT.
4876 */
4877 if (sksec->sclass == SECCLASS_SCTP_SOCKET)
4878 return -EINVAL;
4879 else
4880 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881 }
4882
Paul Moore3e112172008-04-10 10:48:14 -04004883 err = sel_netport_sid(sk->sk_protocol, snum, &sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884 if (err)
Richard Hainesd4529302018-02-13 20:57:18 +00004885 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886
Richard Hainesd4529302018-02-13 20:57:18 +00004887 switch (sksec->sclass) {
4888 case SECCLASS_TCP_SOCKET:
4889 perm = TCP_SOCKET__NAME_CONNECT;
4890 break;
4891 case SECCLASS_DCCP_SOCKET:
4892 perm = DCCP_SOCKET__NAME_CONNECT;
4893 break;
4894 case SECCLASS_SCTP_SOCKET:
4895 perm = SCTP_SOCKET__NAME_CONNECT;
4896 break;
4897 }
James Morris2ee92d42006-11-13 16:09:01 -08004898
Eric Paris50c205f2012-04-04 15:01:43 -04004899 ad.type = LSM_AUDIT_DATA_NET;
Eric Paris48c62af2012-04-02 13:15:44 -04004900 ad.u.net = &net;
4901 ad.u.net->dport = htons(snum);
Alexey Kodanev88b7d372018-05-11 20:15:12 +03004902 ad.u.net->family = address->sa_family;
Stephen Smalleye67b7982023-03-09 13:30:37 -05004903 err = avc_has_perm(sksec->sid, sid, sksec->sclass, perm, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904 if (err)
Richard Hainesd4529302018-02-13 20:57:18 +00004905 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906 }
4907
Richard Hainesd4529302018-02-13 20:57:18 +00004908 return 0;
4909}
Paul Moore014ab192008-10-10 10:16:33 -04004910
Richard Hainesd4529302018-02-13 20:57:18 +00004911/* Supports connect(2), see comments in selinux_socket_connect_helper() */
4912static int selinux_socket_connect(struct socket *sock,
4913 struct sockaddr *address, int addrlen)
4914{
4915 int err;
4916 struct sock *sk = sock->sk;
4917
4918 err = selinux_socket_connect_helper(sock, address, addrlen);
4919 if (err)
4920 return err;
4921
4922 return selinux_netlbl_socket_connect(sk, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923}
4924
4925static int selinux_socket_listen(struct socket *sock, int backlog)
4926{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004927 return sock_has_perm(sock->sk, SOCKET__LISTEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928}
4929
4930static int selinux_socket_accept(struct socket *sock, struct socket *newsock)
4931{
4932 int err;
4933 struct inode_security_struct *isec;
4934 struct inode_security_struct *newisec;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004935 u16 sclass;
4936 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004938 err = sock_has_perm(sock->sk, SOCKET__ACCEPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939 if (err)
4940 return err;
4941
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05004942 isec = inode_security_novalidate(SOCK_INODE(sock));
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01004943 spin_lock(&isec->lock);
4944 sclass = isec->sclass;
4945 sid = isec->sid;
4946 spin_unlock(&isec->lock);
4947
4948 newisec = inode_security_novalidate(SOCK_INODE(newsock));
4949 newisec->sclass = sclass;
4950 newisec->sid = sid;
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05004951 newisec->initialized = LABEL_INITIALIZED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952
4953 return 0;
4954}
4955
4956static int selinux_socket_sendmsg(struct socket *sock, struct msghdr *msg,
Eric Paris828dfe12008-04-17 13:17:49 -04004957 int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004959 return sock_has_perm(sock->sk, SOCKET__WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960}
4961
4962static int selinux_socket_recvmsg(struct socket *sock, struct msghdr *msg,
4963 int size, int flags)
4964{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004965 return sock_has_perm(sock->sk, SOCKET__READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966}
4967
4968static int selinux_socket_getsockname(struct socket *sock)
4969{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004970 return sock_has_perm(sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971}
4972
4973static int selinux_socket_getpeername(struct socket *sock)
4974{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004975 return sock_has_perm(sock->sk, SOCKET__GETATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976}
4977
Eric Paris828dfe12008-04-17 13:17:49 -04004978static int selinux_socket_setsockopt(struct socket *sock, int level, int optname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979{
Paul Mooref8687af2006-10-30 15:22:15 -08004980 int err;
4981
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004982 err = sock_has_perm(sock->sk, SOCKET__SETOPT);
Paul Mooref8687af2006-10-30 15:22:15 -08004983 if (err)
4984 return err;
4985
4986 return selinux_netlbl_socket_setsockopt(sock, level, optname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987}
4988
4989static int selinux_socket_getsockopt(struct socket *sock, int level,
4990 int optname)
4991{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004992 return sock_has_perm(sock->sk, SOCKET__GETOPT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993}
4994
4995static int selinux_socket_shutdown(struct socket *sock, int how)
4996{
Stephen Smalleybe0554c2017-01-09 10:07:31 -05004997 return sock_has_perm(sock->sk, SOCKET__SHUTDOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998}
4999
David S. Miller3610cda2011-01-05 15:38:53 -08005000static int selinux_socket_unix_stream_connect(struct sock *sock,
5001 struct sock *other,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002 struct sock *newsk)
5003{
David S. Miller3610cda2011-01-05 15:38:53 -08005004 struct sk_security_struct *sksec_sock = sock->sk_security;
5005 struct sk_security_struct *sksec_other = other->sk_security;
Paul Moore4d1e2452010-04-22 14:46:18 -04005006 struct sk_security_struct *sksec_new = newsk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04005007 struct common_audit_data ad;
Paolo Abenidd51fcd2023-07-19 13:37:49 +02005008 struct lsm_network_audit net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009 int err;
5010
Paolo Abenidd51fcd2023-07-19 13:37:49 +02005011 ad_net_init_from_sk(&ad, &net, other);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012
Stephen Smalleye67b7982023-03-09 13:30:37 -05005013 err = avc_has_perm(sksec_sock->sid, sksec_other->sid,
Paul Moore4d1e2452010-04-22 14:46:18 -04005014 sksec_other->sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015 UNIX_STREAM_SOCKET__CONNECTTO, &ad);
5016 if (err)
5017 return err;
5018
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 /* server child socket */
Paul Moore4d1e2452010-04-22 14:46:18 -04005020 sksec_new->peer_sid = sksec_sock->sid;
Stephen Smalleye67b7982023-03-09 13:30:37 -05005021 err = security_sid_mls_copy(sksec_other->sid,
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005022 sksec_sock->sid, &sksec_new->sid);
Paul Moore4d1e2452010-04-22 14:46:18 -04005023 if (err)
5024 return err;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005025
Paul Moore4d1e2452010-04-22 14:46:18 -04005026 /* connecting socket */
5027 sksec_sock->peer_sid = sksec_new->sid;
5028
5029 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030}
5031
5032static int selinux_socket_unix_may_send(struct socket *sock,
5033 struct socket *other)
5034{
Paul Moore253bfae2010-04-22 14:46:19 -04005035 struct sk_security_struct *ssec = sock->sk->sk_security;
5036 struct sk_security_struct *osec = other->sk->sk_security;
Thomas Liu2bf49692009-07-14 12:14:09 -04005037 struct common_audit_data ad;
Paolo Abenidd51fcd2023-07-19 13:37:49 +02005038 struct lsm_network_audit net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039
Paolo Abenidd51fcd2023-07-19 13:37:49 +02005040 ad_net_init_from_sk(&ad, &net, other->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041
Stephen Smalleye67b7982023-03-09 13:30:37 -05005042 return avc_has_perm(ssec->sid, osec->sid, osec->sclass, SOCKET__SENDTO,
Paul Moore253bfae2010-04-22 14:46:19 -04005043 &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044}
5045
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005046static int selinux_inet_sys_rcv_skb(struct net *ns, int ifindex,
5047 char *addrp, u16 family, u32 peer_sid,
Thomas Liu2bf49692009-07-14 12:14:09 -04005048 struct common_audit_data *ad)
Paul Mooreeffad8d2008-01-29 08:49:27 -05005049{
5050 int err;
5051 u32 if_sid;
5052 u32 node_sid;
5053
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005054 err = sel_netif_sid(ns, ifindex, &if_sid);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005055 if (err)
5056 return err;
Stephen Smalleye67b7982023-03-09 13:30:37 -05005057 err = avc_has_perm(peer_sid, if_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005058 SECCLASS_NETIF, NETIF__INGRESS, ad);
5059 if (err)
5060 return err;
5061
5062 err = sel_netnode_sid(addrp, family, &node_sid);
5063 if (err)
5064 return err;
Stephen Smalleye67b7982023-03-09 13:30:37 -05005065 return avc_has_perm(peer_sid, node_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005066 SECCLASS_NODE, NODE__RECVFROM, ad);
5067}
5068
Paul Moore220deb92008-01-29 08:38:23 -05005069static int selinux_sock_rcv_skb_compat(struct sock *sk, struct sk_buff *skb,
Paul Moored8395c82008-10-10 10:16:30 -04005070 u16 family)
Paul Moore220deb92008-01-29 08:38:23 -05005071{
Paul Moore277d3422008-12-31 12:54:11 -05005072 int err = 0;
Paul Moore220deb92008-01-29 08:38:23 -05005073 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05005074 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04005075 struct common_audit_data ad;
Paolo Abenidd51fcd2023-07-19 13:37:49 +02005076 struct lsm_network_audit net;
Paul Moored8395c82008-10-10 10:16:30 -04005077 char *addrp;
5078
Paolo Abenidd51fcd2023-07-19 13:37:49 +02005079 ad_net_init_from_iif(&ad, &net, skb->skb_iif, family);
Paul Moored8395c82008-10-10 10:16:30 -04005080 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
5081 if (err)
5082 return err;
Paul Moore220deb92008-01-29 08:38:23 -05005083
Paul Moore58bfbb52009-03-27 17:10:41 -04005084 if (selinux_secmark_enabled()) {
Stephen Smalleye67b7982023-03-09 13:30:37 -05005085 err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Moored8395c82008-10-10 10:16:30 -04005086 PACKET__RECV, &ad);
Paul Moore58bfbb52009-03-27 17:10:41 -04005087 if (err)
5088 return err;
5089 }
Paul Moore220deb92008-01-29 08:38:23 -05005090
Steffen Klassertb9679a72011-02-23 12:55:21 +01005091 err = selinux_netlbl_sock_rcv_skb(sksec, skb, family, &ad);
5092 if (err)
5093 return err;
5094 err = selinux_xfrm_sock_rcv_skb(sksec->sid, skb, &ad);
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005095
James Morris4e5ab4c2006-06-09 00:33:33 -07005096 return err;
5097}
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005098
James Morris4e5ab4c2006-06-09 00:33:33 -07005099static int selinux_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
5100{
Christian Göttschea13479b2023-07-06 15:23:27 +02005101 int err, peerlbl_active, secmark_active;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005102 struct sk_security_struct *sksec = sk->sk_security;
Paul Moore220deb92008-01-29 08:38:23 -05005103 u16 family = sk->sk_family;
5104 u32 sk_sid = sksec->sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04005105 struct common_audit_data ad;
Paolo Abenidd51fcd2023-07-19 13:37:49 +02005106 struct lsm_network_audit net;
Paul Moore220deb92008-01-29 08:38:23 -05005107 char *addrp;
James Morris4e5ab4c2006-06-09 00:33:33 -07005108
James Morris4e5ab4c2006-06-09 00:33:33 -07005109 if (family != PF_INET && family != PF_INET6)
Paul Moore220deb92008-01-29 08:38:23 -05005110 return 0;
James Morris4e5ab4c2006-06-09 00:33:33 -07005111
5112 /* Handle mapped IPv4 packets arriving via IPv6 sockets */
Al Viro87fcd702006-12-04 22:00:55 +00005113 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
James Morris4e5ab4c2006-06-09 00:33:33 -07005114 family = PF_INET;
5115
Paul Moored8395c82008-10-10 10:16:30 -04005116 /* If any sort of compatibility mode is enabled then handoff processing
5117 * to the selinux_sock_rcv_skb_compat() function to deal with the
5118 * special handling. We do this in an attempt to keep this function
5119 * as fast and as clean as possible. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005120 if (!selinux_policycap_netpeer())
Paul Moored8395c82008-10-10 10:16:30 -04005121 return selinux_sock_rcv_skb_compat(sk, skb, family);
5122
5123 secmark_active = selinux_secmark_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04005124 peerlbl_active = selinux_peerlbl_enabled();
Paul Moored8395c82008-10-10 10:16:30 -04005125 if (!secmark_active && !peerlbl_active)
5126 return 0;
5127
Paolo Abenidd51fcd2023-07-19 13:37:49 +02005128 ad_net_init_from_iif(&ad, &net, skb->skb_iif, family);
Paul Moore224dfbd2008-01-29 08:38:13 -05005129 err = selinux_parse_skb(skb, &ad, &addrp, 1, NULL);
James Morris4e5ab4c2006-06-09 00:33:33 -07005130 if (err)
Paul Moore220deb92008-01-29 08:38:23 -05005131 return err;
James Morris4e5ab4c2006-06-09 00:33:33 -07005132
Paul Moored8395c82008-10-10 10:16:30 -04005133 if (peerlbl_active) {
Paul Moored621d352008-01-29 08:43:36 -05005134 u32 peer_sid;
5135
5136 err = selinux_skb_peerlbl_sid(skb, family, &peer_sid);
5137 if (err)
5138 return err;
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005139 err = selinux_inet_sys_rcv_skb(sock_net(sk), skb->skb_iif,
5140 addrp, family, peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04005141 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04005142 selinux_netlbl_err(skb, family, err, 0);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005143 return err;
Paul Mooredfaebe92008-10-10 10:16:31 -04005144 }
Stephen Smalleye67b7982023-03-09 13:30:37 -05005145 err = avc_has_perm(sk_sid, peer_sid, SECCLASS_PEER,
Paul Moored621d352008-01-29 08:43:36 -05005146 PEER__RECV, &ad);
Chad Hanson46d01d62013-12-23 17:45:01 -05005147 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04005148 selinux_netlbl_err(skb, family, err, 0);
Chad Hanson46d01d62013-12-23 17:45:01 -05005149 return err;
5150 }
Paul Moored621d352008-01-29 08:43:36 -05005151 }
5152
Paul Moored8395c82008-10-10 10:16:30 -04005153 if (secmark_active) {
Stephen Smalleye67b7982023-03-09 13:30:37 -05005154 err = avc_has_perm(sk_sid, skb->secmark, SECCLASS_PACKET,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005155 PACKET__RECV, &ad);
5156 if (err)
5157 return err;
5158 }
5159
Paul Moored621d352008-01-29 08:43:36 -05005160 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161}
5162
Paul Mooreb10b9c32022-10-10 12:31:21 -04005163static int selinux_socket_getpeersec_stream(struct socket *sock,
5164 sockptr_t optval, sockptr_t optlen,
5165 unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166{
5167 int err = 0;
Paul Mooreb10b9c32022-10-10 12:31:21 -04005168 char *scontext = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169 u32 scontext_len;
Paul Moore253bfae2010-04-22 14:46:19 -04005170 struct sk_security_struct *sksec = sock->sk->sk_security;
Paul Moore3de4bab2006-11-17 17:38:54 -05005171 u32 peer_sid = SECSID_NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172
Paul Moore253bfae2010-04-22 14:46:19 -04005173 if (sksec->sclass == SECCLASS_UNIX_STREAM_SOCKET ||
Richard Hainesd4529302018-02-13 20:57:18 +00005174 sksec->sclass == SECCLASS_TCP_SOCKET ||
5175 sksec->sclass == SECCLASS_SCTP_SOCKET)
Eric Parisdd3e7832010-04-07 15:08:46 -04005176 peer_sid = sksec->peer_sid;
Paul Moore253bfae2010-04-22 14:46:19 -04005177 if (peer_sid == SECSID_NULL)
5178 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179
Stephen Smalleye67b7982023-03-09 13:30:37 -05005180 err = security_sid_to_context(peer_sid, &scontext,
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005181 &scontext_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182 if (err)
Paul Moore253bfae2010-04-22 14:46:19 -04005183 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184 if (scontext_len > len) {
5185 err = -ERANGE;
5186 goto out_len;
5187 }
5188
Paul Mooreb10b9c32022-10-10 12:31:21 -04005189 if (copy_to_sockptr(optval, scontext, scontext_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191out_len:
Paul Mooreb10b9c32022-10-10 12:31:21 -04005192 if (copy_to_sockptr(optlen, &scontext_len, sizeof(scontext_len)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194 kfree(scontext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195 return err;
5196}
5197
Paul Moorebfda63f2024-01-30 16:16:29 -05005198static int selinux_socket_getpeersec_dgram(struct socket *sock,
5199 struct sk_buff *skb, u32 *secid)
Catherine Zhang2c7946a2006-03-20 22:41:23 -08005200{
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005201 u32 peer_secid = SECSID_NULL;
Paul Moore75e22912008-01-29 08:38:04 -05005202 u16 family;
Catherine Zhang877ce7c2006-06-29 12:27:47 -07005203
Paul Mooreaa862902008-10-10 10:16:29 -04005204 if (skb && skb->protocol == htons(ETH_P_IP))
5205 family = PF_INET;
5206 else if (skb && skb->protocol == htons(ETH_P_IPV6))
5207 family = PF_INET6;
5208 else if (sock)
Paul Moore75e22912008-01-29 08:38:04 -05005209 family = sock->sk->sk_family;
Paul Moorebfda63f2024-01-30 16:16:29 -05005210 else {
5211 *secid = SECSID_NULL;
5212 return -EINVAL;
5213 }
Paul Moore75e22912008-01-29 08:38:04 -05005214
Paul Moore899134f2016-03-28 15:19:10 -04005215 if (sock && family == PF_UNIX) {
Paul Moorebfda63f2024-01-30 16:16:29 -05005216 struct inode_security_struct *isec;
Paul Moore899134f2016-03-28 15:19:10 -04005217 isec = inode_security_novalidate(SOCK_INODE(sock));
5218 peer_secid = isec->sid;
5219 } else if (skb)
Paul Moore220deb92008-01-29 08:38:23 -05005220 selinux_skb_peerlbl_sid(skb, family, &peer_secid);
Catherine Zhang2c7946a2006-03-20 22:41:23 -08005221
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07005222 *secid = peer_secid;
Paul Moore75e22912008-01-29 08:38:04 -05005223 if (peer_secid == SECSID_NULL)
Paul Moorebfda63f2024-01-30 16:16:29 -05005224 return -ENOPROTOOPT;
Paul Moore75e22912008-01-29 08:38:04 -05005225 return 0;
Catherine Zhang2c7946a2006-03-20 22:41:23 -08005226}
5227
Al Viro7d877f32005-10-21 03:20:43 -04005228static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229{
Paul Moore84914b72010-04-22 14:46:18 -04005230 struct sk_security_struct *sksec;
5231
5232 sksec = kzalloc(sizeof(*sksec), priority);
5233 if (!sksec)
5234 return -ENOMEM;
5235
5236 sksec->peer_sid = SECINITSID_UNLABELED;
5237 sksec->sid = SECINITSID_UNLABELED;
Stephen Smalley5dee25d2015-07-10 17:19:57 -04005238 sksec->sclass = SECCLASS_SOCKET;
Paul Moore84914b72010-04-22 14:46:18 -04005239 selinux_netlbl_sk_security_reset(sksec);
5240 sk->sk_security = sksec;
5241
5242 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243}
5244
5245static void selinux_sk_free_security(struct sock *sk)
5246{
Paul Moore84914b72010-04-22 14:46:18 -04005247 struct sk_security_struct *sksec = sk->sk_security;
5248
5249 sk->sk_security = NULL;
5250 selinux_netlbl_sk_security_free(sksec);
5251 kfree(sksec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252}
5253
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005254static void selinux_sk_clone_security(const struct sock *sk, struct sock *newsk)
5255{
Eric Parisdd3e7832010-04-07 15:08:46 -04005256 struct sk_security_struct *sksec = sk->sk_security;
5257 struct sk_security_struct *newsksec = newsk->sk_security;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005258
Eric Parisdd3e7832010-04-07 15:08:46 -04005259 newsksec->sid = sksec->sid;
5260 newsksec->peer_sid = sksec->peer_sid;
5261 newsksec->sclass = sksec->sclass;
Paul Moore99f59ed2006-08-29 17:53:48 -07005262
Eric Parisdd3e7832010-04-07 15:08:46 -04005263 selinux_netlbl_sk_security_reset(newsksec);
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005264}
5265
Guillaume Nault5b52ad32023-07-11 15:06:08 +02005266static void selinux_sk_getsecid(const struct sock *sk, u32 *secid)
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005267{
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005268 if (!sk)
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07005269 *secid = SECINITSID_ANY_SOCKET;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005270 else {
Guillaume Nault5b52ad32023-07-11 15:06:08 +02005271 const struct sk_security_struct *sksec = sk->sk_security;
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005272
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07005273 *secid = sksec->sid;
Venkat Yekkirala892c1412006-08-04 23:08:56 -07005274 }
Trent Jaegerd28d1e02005-12-13 23:12:40 -08005275}
5276
Eric Paris828dfe12008-04-17 13:17:49 -04005277static void selinux_sock_graft(struct sock *sk, struct socket *parent)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005278{
Andreas Gruenbacher5d226df2015-12-24 11:09:40 -05005279 struct inode_security_struct *isec =
5280 inode_security_novalidate(SOCK_INODE(parent));
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005281 struct sk_security_struct *sksec = sk->sk_security;
5282
Paul Moore2873ead2014-07-28 10:42:48 -04005283 if (sk->sk_family == PF_INET || sk->sk_family == PF_INET6 ||
5284 sk->sk_family == PF_UNIX)
David Woodhouse2148ccc2006-09-29 15:50:25 -07005285 isec->sid = sksec->sid;
Paul Moore220deb92008-01-29 08:38:23 -05005286 sksec->sclass = isec->sclass;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005287}
5288
Ondrej Mosnacek3eb8eaf2022-02-12 18:59:22 +01005289/*
5290 * Determines peer_secid for the asoc and updates socket's peer label
5291 * if it's the first association on the socket.
Richard Hainesd4529302018-02-13 20:57:18 +00005292 */
Ondrej Mosnacek3eb8eaf2022-02-12 18:59:22 +01005293static int selinux_sctp_process_new_assoc(struct sctp_association *asoc,
5294 struct sk_buff *skb)
Richard Hainesd4529302018-02-13 20:57:18 +00005295{
Ondrej Mosnacek3eb8eaf2022-02-12 18:59:22 +01005296 struct sock *sk = asoc->base.sk;
5297 u16 family = sk->sk_family;
5298 struct sk_security_struct *sksec = sk->sk_security;
Richard Hainesd4529302018-02-13 20:57:18 +00005299 struct common_audit_data ad;
Paolo Abenidd51fcd2023-07-19 13:37:49 +02005300 struct lsm_network_audit net;
Ondrej Mosnacek3eb8eaf2022-02-12 18:59:22 +01005301 int err;
Richard Hainesd4529302018-02-13 20:57:18 +00005302
Ondrej Mosnacek3eb8eaf2022-02-12 18:59:22 +01005303 /* handle mapped IPv4 packets arriving via IPv6 sockets */
5304 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
5305 family = PF_INET;
Richard Hainesd4529302018-02-13 20:57:18 +00005306
Ondrej Mosnacek3eb8eaf2022-02-12 18:59:22 +01005307 if (selinux_peerlbl_enabled()) {
5308 asoc->peer_secid = SECSID_NULL;
Richard Hainesd4529302018-02-13 20:57:18 +00005309
Richard Hainesd4529302018-02-13 20:57:18 +00005310 /* This will return peer_sid = SECSID_NULL if there are
5311 * no peer labels, see security_net_peersid_resolve().
5312 */
Ondrej Mosnacek3eb8eaf2022-02-12 18:59:22 +01005313 err = selinux_skb_peerlbl_sid(skb, family, &asoc->peer_secid);
Richard Hainesd4529302018-02-13 20:57:18 +00005314 if (err)
5315 return err;
5316
Ondrej Mosnacek3eb8eaf2022-02-12 18:59:22 +01005317 if (asoc->peer_secid == SECSID_NULL)
5318 asoc->peer_secid = SECINITSID_UNLABELED;
5319 } else {
5320 asoc->peer_secid = SECINITSID_UNLABELED;
Richard Hainesd4529302018-02-13 20:57:18 +00005321 }
5322
5323 if (sksec->sctp_assoc_state == SCTP_ASSOC_UNSET) {
5324 sksec->sctp_assoc_state = SCTP_ASSOC_SET;
5325
5326 /* Here as first association on socket. As the peer SID
5327 * was allowed by peer recv (and the netif/node checks),
5328 * then it is approved by policy and used as the primary
5329 * peer SID for getpeercon(3).
5330 */
Ondrej Mosnacek3eb8eaf2022-02-12 18:59:22 +01005331 sksec->peer_sid = asoc->peer_secid;
5332 } else if (sksec->peer_sid != asoc->peer_secid) {
Richard Hainesd4529302018-02-13 20:57:18 +00005333 /* Other association peer SIDs are checked to enforce
5334 * consistency among the peer SIDs.
5335 */
Paolo Abenidd51fcd2023-07-19 13:37:49 +02005336 ad_net_init_from_sk(&ad, &net, asoc->base.sk);
Stephen Smalleye67b7982023-03-09 13:30:37 -05005337 err = avc_has_perm(sksec->peer_sid, asoc->peer_secid,
Ondrej Mosnacek3eb8eaf2022-02-12 18:59:22 +01005338 sksec->sclass, SCTP_SOCKET__ASSOCIATION,
5339 &ad);
Richard Hainesd4529302018-02-13 20:57:18 +00005340 if (err)
5341 return err;
5342 }
Ondrej Mosnacek3eb8eaf2022-02-12 18:59:22 +01005343 return 0;
5344}
5345
5346/* Called whenever SCTP receives an INIT or COOKIE ECHO chunk. This
5347 * happens on an incoming connect(2), sctp_connectx(3) or
5348 * sctp_sendmsg(3) (with no association already present).
5349 */
5350static int selinux_sctp_assoc_request(struct sctp_association *asoc,
5351 struct sk_buff *skb)
5352{
5353 struct sk_security_struct *sksec = asoc->base.sk->sk_security;
5354 u32 conn_sid;
5355 int err;
5356
5357 if (!selinux_policycap_extsockclass())
5358 return 0;
5359
5360 err = selinux_sctp_process_new_assoc(asoc, skb);
5361 if (err)
5362 return err;
Richard Hainesd4529302018-02-13 20:57:18 +00005363
5364 /* Compute the MLS component for the connection and store
Xin Longc081d532021-11-02 08:02:47 -04005365 * the information in asoc. This will be used by SCTP TCP type
Richard Hainesd4529302018-02-13 20:57:18 +00005366 * sockets and peeled off connections as they cause a new
5367 * socket to be generated. selinux_sctp_sk_clone() will then
5368 * plug this into the new socket.
5369 */
Ondrej Mosnacek3eb8eaf2022-02-12 18:59:22 +01005370 err = selinux_conn_sid(sksec->sid, asoc->peer_secid, &conn_sid);
Richard Hainesd4529302018-02-13 20:57:18 +00005371 if (err)
5372 return err;
5373
Xin Longc081d532021-11-02 08:02:47 -04005374 asoc->secid = conn_sid;
Richard Hainesd4529302018-02-13 20:57:18 +00005375
5376 /* Set any NetLabel labels including CIPSO/CALIPSO options. */
Xin Longc081d532021-11-02 08:02:47 -04005377 return selinux_netlbl_sctp_assoc_request(asoc, skb);
Richard Hainesd4529302018-02-13 20:57:18 +00005378}
5379
Ondrej Mosnacek3eb8eaf2022-02-12 18:59:22 +01005380/* Called when SCTP receives a COOKIE ACK chunk as the final
5381 * response to an association request (initited by us).
5382 */
5383static int selinux_sctp_assoc_established(struct sctp_association *asoc,
5384 struct sk_buff *skb)
5385{
5386 struct sk_security_struct *sksec = asoc->base.sk->sk_security;
5387
5388 if (!selinux_policycap_extsockclass())
5389 return 0;
5390
5391 /* Inherit secid from the parent socket - this will be picked up
5392 * by selinux_sctp_sk_clone() if the association gets peeled off
5393 * into a new socket.
5394 */
5395 asoc->secid = sksec->sid;
5396
5397 return selinux_sctp_process_new_assoc(asoc, skb);
5398}
5399
Richard Hainesd4529302018-02-13 20:57:18 +00005400/* Check if sctp IPv4/IPv6 addresses are valid for binding or connecting
5401 * based on their @optname.
5402 */
5403static int selinux_sctp_bind_connect(struct sock *sk, int optname,
5404 struct sockaddr *address,
5405 int addrlen)
5406{
5407 int len, err = 0, walk_size = 0;
5408 void *addr_buf;
5409 struct sockaddr *addr;
5410 struct socket *sock;
5411
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005412 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005413 return 0;
5414
5415 /* Process one or more addresses that may be IPv4 or IPv6 */
5416 sock = sk->sk_socket;
5417 addr_buf = address;
5418
5419 while (walk_size < addrlen) {
Ondrej Mosnacekc1383252018-11-13 16:16:08 +01005420 if (walk_size + sizeof(sa_family_t) > addrlen)
5421 return -EINVAL;
5422
Richard Hainesd4529302018-02-13 20:57:18 +00005423 addr = addr_buf;
5424 switch (addr->sa_family) {
Alexey Kodanev4152dc92018-05-11 20:15:13 +03005425 case AF_UNSPEC:
Richard Hainesd4529302018-02-13 20:57:18 +00005426 case AF_INET:
5427 len = sizeof(struct sockaddr_in);
5428 break;
5429 case AF_INET6:
5430 len = sizeof(struct sockaddr_in6);
5431 break;
5432 default:
Alexey Kodanev4152dc92018-05-11 20:15:13 +03005433 return -EINVAL;
Richard Hainesd4529302018-02-13 20:57:18 +00005434 }
5435
Xin Long292c9972019-03-09 00:07:34 +08005436 if (walk_size + len > addrlen)
5437 return -EINVAL;
5438
Richard Hainesd4529302018-02-13 20:57:18 +00005439 err = -EINVAL;
5440 switch (optname) {
5441 /* Bind checks */
5442 case SCTP_PRIMARY_ADDR:
5443 case SCTP_SET_PEER_PRIMARY_ADDR:
5444 case SCTP_SOCKOPT_BINDX_ADD:
5445 err = selinux_socket_bind(sock, addr, len);
5446 break;
5447 /* Connect checks */
5448 case SCTP_SOCKOPT_CONNECTX:
5449 case SCTP_PARAM_SET_PRIMARY:
5450 case SCTP_PARAM_ADD_IP:
5451 case SCTP_SENDMSG_CONNECT:
5452 err = selinux_socket_connect_helper(sock, addr, len);
5453 if (err)
5454 return err;
5455
5456 /* As selinux_sctp_bind_connect() is called by the
5457 * SCTP protocol layer, the socket is already locked,
Randy Dunlapc76a2f92020-08-07 09:51:34 -07005458 * therefore selinux_netlbl_socket_connect_locked()
Richard Hainesd4529302018-02-13 20:57:18 +00005459 * is called here. The situations handled are:
5460 * sctp_connectx(3), sctp_sendmsg(3), sendmsg(2),
5461 * whenever a new IP address is added or when a new
5462 * primary address is selected.
5463 * Note that an SCTP connect(2) call happens before
5464 * the SCTP protocol layer and is handled via
5465 * selinux_socket_connect().
5466 */
5467 err = selinux_netlbl_socket_connect_locked(sk, addr);
5468 break;
5469 }
5470
5471 if (err)
5472 return err;
5473
5474 addr_buf += len;
5475 walk_size += len;
5476 }
5477
5478 return 0;
5479}
5480
5481/* Called whenever a new socket is created by accept(2) or sctp_peeloff(3). */
Xin Longc081d532021-11-02 08:02:47 -04005482static void selinux_sctp_sk_clone(struct sctp_association *asoc, struct sock *sk,
Richard Hainesd4529302018-02-13 20:57:18 +00005483 struct sock *newsk)
5484{
5485 struct sk_security_struct *sksec = sk->sk_security;
5486 struct sk_security_struct *newsksec = newsk->sk_security;
5487
5488 /* If policy does not support SECCLASS_SCTP_SOCKET then call
5489 * the non-sctp clone version.
5490 */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005491 if (!selinux_policycap_extsockclass())
Richard Hainesd4529302018-02-13 20:57:18 +00005492 return selinux_sk_clone_security(sk, newsk);
5493
Paul Moore32a370a2021-11-12 12:07:02 -05005494 newsksec->sid = asoc->secid;
Xin Longc081d532021-11-02 08:02:47 -04005495 newsksec->peer_sid = asoc->peer_secid;
Richard Hainesd4529302018-02-13 20:57:18 +00005496 newsksec->sclass = sksec->sclass;
5497 selinux_netlbl_sctp_sk_clone(sk, newsk);
5498}
5499
Paolo Abeni85c32222023-04-20 19:17:14 +02005500static int selinux_mptcp_add_subflow(struct sock *sk, struct sock *ssk)
5501{
5502 struct sk_security_struct *ssksec = ssk->sk_security;
5503 struct sk_security_struct *sksec = sk->sk_security;
5504
5505 ssksec->sclass = sksec->sclass;
5506 ssksec->sid = sksec->sid;
5507
5508 /* replace the existing subflow label deleting the existing one
5509 * and re-recreating a new label using the updated context
5510 */
5511 selinux_netlbl_sk_security_free(ssksec);
5512 return selinux_netlbl_socket_post_create(ssk, ssk->sk_family);
5513}
5514
Florian Westphal41dd9592020-11-30 16:36:29 +01005515static int selinux_inet_conn_request(const struct sock *sk, struct sk_buff *skb,
Adrian Bunk9a673e52006-08-15 00:03:53 -07005516 struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005517{
5518 struct sk_security_struct *sksec = sk->sk_security;
5519 int err;
Paul Moore0b1f24e2013-12-03 11:39:13 -05005520 u16 family = req->rsk_ops->family;
Paul Moore446b8022013-12-04 16:10:51 -05005521 u32 connsid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005522 u32 peersid;
5523
Paul Mooreaa862902008-10-10 10:16:29 -04005524 err = selinux_skb_peerlbl_sid(skb, family, &peersid);
Paul Moore220deb92008-01-29 08:38:23 -05005525 if (err)
5526 return err;
Paul Moore446b8022013-12-04 16:10:51 -05005527 err = selinux_conn_sid(sksec->sid, peersid, &connsid);
5528 if (err)
5529 return err;
5530 req->secid = connsid;
5531 req->peer_secid = peersid;
Venkat Yekkiralaa51c64f2006-07-27 22:01:34 -07005532
Paul Moore389fb8002009-03-27 17:10:34 -04005533 return selinux_netlbl_inet_conn_request(req, family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005534}
5535
Adrian Bunk9a673e52006-08-15 00:03:53 -07005536static void selinux_inet_csk_clone(struct sock *newsk,
5537 const struct request_sock *req)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005538{
5539 struct sk_security_struct *newsksec = newsk->sk_security;
5540
5541 newsksec->sid = req->secid;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005542 newsksec->peer_sid = req->peer_secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005543 /* NOTE: Ideally, we should also get the isec->sid for the
5544 new socket in sync, but we don't have the isec available yet.
5545 So we will wait until sock_graft to do it, by which
5546 time it will have been created and available. */
Paul Moore99f59ed2006-08-29 17:53:48 -07005547
Paul Moore9f2ad662006-11-17 17:38:53 -05005548 /* We don't need to take any sort of lock here as we are the only
5549 * thread with access to newsksec */
Paul Moore389fb8002009-03-27 17:10:34 -04005550 selinux_netlbl_inet_csk_clone(newsk, req->rsk_ops->family);
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005551}
5552
Paul Moore014ab192008-10-10 10:16:33 -04005553static void selinux_inet_conn_established(struct sock *sk, struct sk_buff *skb)
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005554{
Paul Mooreaa862902008-10-10 10:16:29 -04005555 u16 family = sk->sk_family;
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005556 struct sk_security_struct *sksec = sk->sk_security;
5557
Paul Mooreaa862902008-10-10 10:16:29 -04005558 /* handle mapped IPv4 packets arriving via IPv6 sockets */
5559 if (family == PF_INET6 && skb->protocol == htons(ETH_P_IP))
5560 family = PF_INET;
5561
5562 selinux_skb_peerlbl_sid(skb, family, &sksec->peer_sid);
Venkat Yekkirala6b877692006-11-08 17:04:09 -06005563}
5564
Eric Paris2606fd12010-10-13 16:24:41 -04005565static int selinux_secmark_relabel_packet(u32 sid)
5566{
Christian Göttschea13479b2023-07-06 15:23:27 +02005567 const struct task_security_struct *tsec;
Eric Paris2606fd12010-10-13 16:24:41 -04005568 u32 tsid;
5569
Christian Göttschea13479b2023-07-06 15:23:27 +02005570 tsec = selinux_cred(current_cred());
5571 tsid = tsec->sid;
Eric Paris2606fd12010-10-13 16:24:41 -04005572
Stephen Smalleye67b7982023-03-09 13:30:37 -05005573 return avc_has_perm(tsid, sid, SECCLASS_PACKET, PACKET__RELABELTO,
Stephen Smalley6b6bc622018-03-05 11:47:56 -05005574 NULL);
Eric Paris2606fd12010-10-13 16:24:41 -04005575}
5576
5577static void selinux_secmark_refcount_inc(void)
5578{
5579 atomic_inc(&selinux_secmark_refcount);
5580}
5581
5582static void selinux_secmark_refcount_dec(void)
5583{
5584 atomic_dec(&selinux_secmark_refcount);
5585}
5586
Adrian Bunk9a673e52006-08-15 00:03:53 -07005587static void selinux_req_classify_flow(const struct request_sock *req,
Paul Moore3df98d72020-09-27 22:38:26 -04005588 struct flowi_common *flic)
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005589{
Paul Moore3df98d72020-09-27 22:38:26 -04005590 flic->flowic_secid = req->secid;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005591}
5592
Paul Moore5dbbaf22013-01-14 07:12:19 +00005593static int selinux_tun_dev_alloc_security(void **security)
5594{
5595 struct tun_security_struct *tunsec;
5596
5597 tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL);
5598 if (!tunsec)
5599 return -ENOMEM;
5600 tunsec->sid = current_sid();
5601
5602 *security = tunsec;
5603 return 0;
5604}
5605
5606static void selinux_tun_dev_free_security(void *security)
5607{
5608 kfree(security);
5609}
5610
Paul Mooreed6d76e2009-08-28 18:12:49 -04005611static int selinux_tun_dev_create(void)
5612{
5613 u32 sid = current_sid();
5614
5615 /* we aren't taking into account the "sockcreate" SID since the socket
5616 * that is being created here is not a socket in the traditional sense,
5617 * instead it is a private sock, accessible only to the kernel, and
5618 * representing a wide range of network traffic spanning multiple
5619 * connections unlike traditional sockets - check the TUN driver to
5620 * get a better understanding of why this socket is special */
5621
Stephen Smalleye67b7982023-03-09 13:30:37 -05005622 return avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET, TUN_SOCKET__CREATE,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005623 NULL);
5624}
5625
Paul Moore5dbbaf22013-01-14 07:12:19 +00005626static int selinux_tun_dev_attach_queue(void *security)
Paul Mooreed6d76e2009-08-28 18:12:49 -04005627{
Paul Moore5dbbaf22013-01-14 07:12:19 +00005628 struct tun_security_struct *tunsec = security;
5629
Stephen Smalleye67b7982023-03-09 13:30:37 -05005630 return avc_has_perm(current_sid(), tunsec->sid, SECCLASS_TUN_SOCKET,
Paul Moore5dbbaf22013-01-14 07:12:19 +00005631 TUN_SOCKET__ATTACH_QUEUE, NULL);
5632}
5633
5634static int selinux_tun_dev_attach(struct sock *sk, void *security)
5635{
5636 struct tun_security_struct *tunsec = security;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005637 struct sk_security_struct *sksec = sk->sk_security;
5638
5639 /* we don't currently perform any NetLabel based labeling here and it
5640 * isn't clear that we would want to do so anyway; while we could apply
5641 * labeling without the support of the TUN user the resulting labeled
5642 * traffic from the other end of the connection would almost certainly
5643 * cause confusion to the TUN user that had no idea network labeling
5644 * protocols were being used */
5645
Paul Moore5dbbaf22013-01-14 07:12:19 +00005646 sksec->sid = tunsec->sid;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005647 sksec->sclass = SECCLASS_TUN_SOCKET;
Paul Moore5dbbaf22013-01-14 07:12:19 +00005648
5649 return 0;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005650}
5651
Paul Moore5dbbaf22013-01-14 07:12:19 +00005652static int selinux_tun_dev_open(void *security)
Paul Mooreed6d76e2009-08-28 18:12:49 -04005653{
Paul Moore5dbbaf22013-01-14 07:12:19 +00005654 struct tun_security_struct *tunsec = security;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005655 u32 sid = current_sid();
5656 int err;
5657
Stephen Smalleye67b7982023-03-09 13:30:37 -05005658 err = avc_has_perm(sid, tunsec->sid, SECCLASS_TUN_SOCKET,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005659 TUN_SOCKET__RELABELFROM, NULL);
5660 if (err)
5661 return err;
Stephen Smalleye67b7982023-03-09 13:30:37 -05005662 err = avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET,
Paul Mooreed6d76e2009-08-28 18:12:49 -04005663 TUN_SOCKET__RELABELTO, NULL);
5664 if (err)
5665 return err;
Paul Moore5dbbaf22013-01-14 07:12:19 +00005666 tunsec->sid = sid;
Paul Mooreed6d76e2009-08-28 18:12:49 -04005667
5668 return 0;
5669}
5670
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671#ifdef CONFIG_NETFILTER
5672
Florian Westphal4342f702021-10-11 22:22:29 +02005673static unsigned int selinux_ip_forward(void *priv, struct sk_buff *skb,
5674 const struct nf_hook_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675{
Paul Moore1d1e1de2021-10-11 17:50:48 -04005676 int ifindex;
5677 u16 family;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005678 char *addrp;
5679 u32 peer_sid;
Thomas Liu2bf49692009-07-14 12:14:09 -04005680 struct common_audit_data ad;
Paolo Abenidd51fcd2023-07-19 13:37:49 +02005681 struct lsm_network_audit net;
Paul Moore1d1e1de2021-10-11 17:50:48 -04005682 int secmark_active, peerlbl_active;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005683
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005684 if (!selinux_policycap_netpeer())
Paul Mooreeffad8d2008-01-29 08:49:27 -05005685 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005686
Paul Mooreeffad8d2008-01-29 08:49:27 -05005687 secmark_active = selinux_secmark_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04005688 peerlbl_active = selinux_peerlbl_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05005689 if (!secmark_active && !peerlbl_active)
5690 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005691
Paul Moore1d1e1de2021-10-11 17:50:48 -04005692 family = state->pf;
Paul Moored8395c82008-10-10 10:16:30 -04005693 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid) != 0)
5694 return NF_DROP;
5695
Paul Moore1d1e1de2021-10-11 17:50:48 -04005696 ifindex = state->in->ifindex;
Paolo Abenidd51fcd2023-07-19 13:37:49 +02005697 ad_net_init_from_iif(&ad, &net, ifindex, family);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005698 if (selinux_parse_skb(skb, &ad, &addrp, 1, NULL) != 0)
5699 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700
Paul Mooredfaebe92008-10-10 10:16:31 -04005701 if (peerlbl_active) {
Paul Moore1d1e1de2021-10-11 17:50:48 -04005702 int err;
5703
5704 err = selinux_inet_sys_rcv_skb(state->net, ifindex,
Paul Moorecbe0d6e2014-09-10 17:09:57 -04005705 addrp, family, peer_sid, &ad);
Paul Mooredfaebe92008-10-10 10:16:31 -04005706 if (err) {
Huw Daviesa04e71f2016-06-27 15:06:16 -04005707 selinux_netlbl_err(skb, family, err, 1);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005708 return NF_DROP;
Paul Mooredfaebe92008-10-10 10:16:31 -04005709 }
5710 }
Paul Mooreeffad8d2008-01-29 08:49:27 -05005711
5712 if (secmark_active)
Stephen Smalleye67b7982023-03-09 13:30:37 -05005713 if (avc_has_perm(peer_sid, skb->secmark,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005714 SECCLASS_PACKET, PACKET__FORWARD_IN, &ad))
5715 return NF_DROP;
5716
Paul Moore1d1e1de2021-10-11 17:50:48 -04005717 if (netlbl_enabled())
Paul Moore948bf852008-10-10 10:16:32 -04005718 /* we do this in the FORWARD path and not the POST_ROUTING
5719 * path because we want to make sure we apply the necessary
5720 * labeling before IPsec is applied so we can leverage AH
5721 * protection */
5722 if (selinux_netlbl_skbuff_setsid(skb, family, peer_sid) != 0)
5723 return NF_DROP;
5724
Paul Mooreeffad8d2008-01-29 08:49:27 -05005725 return NF_ACCEPT;
5726}
5727
Florian Westphal4342f702021-10-11 22:22:29 +02005728static unsigned int selinux_ip_output(void *priv, struct sk_buff *skb,
5729 const struct nf_hook_state *state)
Paul Moore948bf852008-10-10 10:16:32 -04005730{
Paul Moore47180062013-12-04 16:10:45 -05005731 struct sock *sk;
Paul Moore948bf852008-10-10 10:16:32 -04005732 u32 sid;
5733
5734 if (!netlbl_enabled())
5735 return NF_ACCEPT;
5736
5737 /* we do this in the LOCAL_OUT path and not the POST_ROUTING path
5738 * because we want to make sure we apply the necessary labeling
5739 * before IPsec is applied so we can leverage AH protection */
Paul Moore47180062013-12-04 16:10:45 -05005740 sk = skb->sk;
5741 if (sk) {
5742 struct sk_security_struct *sksec;
5743
Eric Dumazete446f9d2015-10-08 05:01:55 -07005744 if (sk_listener(sk))
Paul Moore47180062013-12-04 16:10:45 -05005745 /* if the socket is the listening state then this
5746 * packet is a SYN-ACK packet which means it needs to
5747 * be labeled based on the connection/request_sock and
5748 * not the parent socket. unfortunately, we can't
5749 * lookup the request_sock yet as it isn't queued on
5750 * the parent socket until after the SYN-ACK is sent.
5751 * the "solution" is to simply pass the packet as-is
5752 * as any IP option based labeling should be copied
5753 * from the initial connection request (in the IP
5754 * layer). it is far from ideal, but until we get a
5755 * security label in the packet itself this is the
5756 * best we can do. */
5757 return NF_ACCEPT;
5758
5759 /* standard practice, label using the parent socket */
5760 sksec = sk->sk_security;
Paul Moore948bf852008-10-10 10:16:32 -04005761 sid = sksec->sid;
5762 } else
5763 sid = SECINITSID_KERNEL;
Paul Moore1d1e1de2021-10-11 17:50:48 -04005764 if (selinux_netlbl_skbuff_setsid(skb, state->pf, sid) != 0)
Paul Moore948bf852008-10-10 10:16:32 -04005765 return NF_DROP;
5766
5767 return NF_ACCEPT;
5768}
5769
Huw Davies2917f572016-06-27 15:06:15 -04005770
Paul Mooreeffad8d2008-01-29 08:49:27 -05005771static unsigned int selinux_ip_postroute_compat(struct sk_buff *skb,
Paul Moore1d1e1de2021-10-11 17:50:48 -04005772 const struct nf_hook_state *state)
James Morris4e5ab4c2006-06-09 00:33:33 -07005773{
Paul Moore1d1e1de2021-10-11 17:50:48 -04005774 struct sock *sk;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005775 struct sk_security_struct *sksec;
Thomas Liu2bf49692009-07-14 12:14:09 -04005776 struct common_audit_data ad;
Paolo Abenidd51fcd2023-07-19 13:37:49 +02005777 struct lsm_network_audit net;
Tom Rix732bc2f2021-12-24 07:07:39 -08005778 u8 proto = 0;
James Morris4e5ab4c2006-06-09 00:33:33 -07005779
Paul Moore1d1e1de2021-10-11 17:50:48 -04005780 sk = skb_to_full_sk(skb);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005781 if (sk == NULL)
5782 return NF_ACCEPT;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07005783 sksec = sk->sk_security;
James Morris4e5ab4c2006-06-09 00:33:33 -07005784
Paolo Abenidd51fcd2023-07-19 13:37:49 +02005785 ad_net_init_from_iif(&ad, &net, state->out->ifindex, state->pf);
Paul Moore1d1e1de2021-10-11 17:50:48 -04005786 if (selinux_parse_skb(skb, &ad, NULL, 0, &proto))
Paul Moored8395c82008-10-10 10:16:30 -04005787 return NF_DROP;
5788
Paul Moore58bfbb52009-03-27 17:10:41 -04005789 if (selinux_secmark_enabled())
Stephen Smalleye67b7982023-03-09 13:30:37 -05005790 if (avc_has_perm(sksec->sid, skb->secmark,
Paul Moored8395c82008-10-10 10:16:30 -04005791 SECCLASS_PACKET, PACKET__SEND, &ad))
Eric Paris2fe66ec2010-11-23 06:28:08 +00005792 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07005793
Steffen Klassertb9679a72011-02-23 12:55:21 +01005794 if (selinux_xfrm_postroute_last(sksec->sid, skb, &ad, proto))
5795 return NF_DROP_ERR(-ECONNREFUSED);
James Morris4e5ab4c2006-06-09 00:33:33 -07005796
Paul Mooreeffad8d2008-01-29 08:49:27 -05005797 return NF_ACCEPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798}
5799
Florian Westphal4342f702021-10-11 22:22:29 +02005800static unsigned int selinux_ip_postroute(void *priv,
5801 struct sk_buff *skb,
5802 const struct nf_hook_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803{
Paul Moore1d1e1de2021-10-11 17:50:48 -04005804 u16 family;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005805 u32 secmark_perm;
5806 u32 peer_sid;
Paul Moore1d1e1de2021-10-11 17:50:48 -04005807 int ifindex;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005808 struct sock *sk;
Thomas Liu2bf49692009-07-14 12:14:09 -04005809 struct common_audit_data ad;
Paolo Abenidd51fcd2023-07-19 13:37:49 +02005810 struct lsm_network_audit net;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005811 char *addrp;
Paul Moore1d1e1de2021-10-11 17:50:48 -04005812 int secmark_active, peerlbl_active;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005813
Paul Mooreeffad8d2008-01-29 08:49:27 -05005814 /* If any sort of compatibility mode is enabled then handoff processing
5815 * to the selinux_ip_postroute_compat() function to deal with the
5816 * special handling. We do this in an attempt to keep this function
5817 * as fast and as clean as possible. */
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05005818 if (!selinux_policycap_netpeer())
Paul Moore1d1e1de2021-10-11 17:50:48 -04005819 return selinux_ip_postroute_compat(skb, state);
Paul Moorec0828e52013-12-10 14:58:01 -05005820
Paul Mooreeffad8d2008-01-29 08:49:27 -05005821 secmark_active = selinux_secmark_enabled();
Chris PeBenito2be4d742013-05-03 09:05:39 -04005822 peerlbl_active = selinux_peerlbl_enabled();
Paul Mooreeffad8d2008-01-29 08:49:27 -05005823 if (!secmark_active && !peerlbl_active)
5824 return NF_ACCEPT;
5825
Eric Dumazet54abc682015-11-08 10:54:07 -08005826 sk = skb_to_full_sk(skb);
Paul Moorec0828e52013-12-10 14:58:01 -05005827
Paul Mooreeffad8d2008-01-29 08:49:27 -05005828#ifdef CONFIG_XFRM
5829 /* If skb->dst->xfrm is non-NULL then the packet is undergoing an IPsec
5830 * packet transformation so allow the packet to pass without any checks
5831 * since we'll have another chance to perform access control checks
5832 * when the packet is on it's final way out.
5833 * NOTE: there appear to be some IPv6 multicast cases where skb->dst
Paul Moorec0828e52013-12-10 14:58:01 -05005834 * is NULL, in this case go ahead and apply access control.
5835 * NOTE: if this is a local socket (skb->sk != NULL) that is in the
5836 * TCP listening state we cannot wait until the XFRM processing
5837 * is done as we will miss out on the SA label if we do;
5838 * unfortunately, this means more work, but it is only once per
5839 * connection. */
5840 if (skb_dst(skb) != NULL && skb_dst(skb)->xfrm != NULL &&
Eric Dumazete446f9d2015-10-08 05:01:55 -07005841 !(sk && sk_listener(sk)))
Paul Mooreeffad8d2008-01-29 08:49:27 -05005842 return NF_ACCEPT;
5843#endif
Paul Mooreeffad8d2008-01-29 08:49:27 -05005844
Paul Moore1d1e1de2021-10-11 17:50:48 -04005845 family = state->pf;
Paul Moored8395c82008-10-10 10:16:30 -04005846 if (sk == NULL) {
Paul Moore446b8022013-12-04 16:10:51 -05005847 /* Without an associated socket the packet is either coming
5848 * from the kernel or it is being forwarded; check the packet
5849 * to determine which and if the packet is being forwarded
5850 * query the packet directly to determine the security label. */
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005851 if (skb->skb_iif) {
5852 secmark_perm = PACKET__FORWARD_OUT;
Paul Moored8395c82008-10-10 10:16:30 -04005853 if (selinux_skb_peerlbl_sid(skb, family, &peer_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005854 return NF_DROP;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005855 } else {
5856 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04005857 peer_sid = SECINITSID_KERNEL;
Steffen Klassert4a7ab3d2011-02-23 12:56:23 +01005858 }
Eric Dumazete446f9d2015-10-08 05:01:55 -07005859 } else if (sk_listener(sk)) {
Paul Moore446b8022013-12-04 16:10:51 -05005860 /* Locally generated packet but the associated socket is in the
5861 * listening state which means this is a SYN-ACK packet. In
5862 * this particular case the correct security label is assigned
5863 * to the connection/request_sock but unfortunately we can't
5864 * query the request_sock as it isn't queued on the parent
5865 * socket until after the SYN-ACK packet is sent; the only
5866 * viable choice is to regenerate the label like we do in
5867 * selinux_inet_conn_request(). See also selinux_ip_output()
5868 * for similar problems. */
5869 u32 skb_sid;
Eric Dumazete446f9d2015-10-08 05:01:55 -07005870 struct sk_security_struct *sksec;
5871
Eric Dumazete446f9d2015-10-08 05:01:55 -07005872 sksec = sk->sk_security;
Paul Moore446b8022013-12-04 16:10:51 -05005873 if (selinux_skb_peerlbl_sid(skb, family, &skb_sid))
5874 return NF_DROP;
Paul Moorec0828e52013-12-10 14:58:01 -05005875 /* At this point, if the returned skb peerlbl is SECSID_NULL
5876 * and the packet has been through at least one XFRM
5877 * transformation then we must be dealing with the "final"
5878 * form of labeled IPsec packet; since we've already applied
5879 * all of our access controls on this packet we can safely
5880 * pass the packet. */
5881 if (skb_sid == SECSID_NULL) {
5882 switch (family) {
5883 case PF_INET:
5884 if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
5885 return NF_ACCEPT;
5886 break;
5887 case PF_INET6:
5888 if (IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED)
5889 return NF_ACCEPT;
Paul Moorea7a91a12014-09-03 10:51:59 -04005890 break;
Paul Moorec0828e52013-12-10 14:58:01 -05005891 default:
5892 return NF_DROP_ERR(-ECONNREFUSED);
5893 }
5894 }
Paul Moore446b8022013-12-04 16:10:51 -05005895 if (selinux_conn_sid(sksec->sid, skb_sid, &peer_sid))
5896 return NF_DROP;
5897 secmark_perm = PACKET__SEND;
Paul Moored8395c82008-10-10 10:16:30 -04005898 } else {
Paul Moore446b8022013-12-04 16:10:51 -05005899 /* Locally generated packet, fetch the security label from the
5900 * associated socket. */
Paul Mooreeffad8d2008-01-29 08:49:27 -05005901 struct sk_security_struct *sksec = sk->sk_security;
5902 peer_sid = sksec->sid;
5903 secmark_perm = PACKET__SEND;
Paul Mooreeffad8d2008-01-29 08:49:27 -05005904 }
5905
Paul Moore1d1e1de2021-10-11 17:50:48 -04005906 ifindex = state->out->ifindex;
Paolo Abenidd51fcd2023-07-19 13:37:49 +02005907 ad_net_init_from_iif(&ad, &net, ifindex, family);
Paul Moored8395c82008-10-10 10:16:30 -04005908 if (selinux_parse_skb(skb, &ad, &addrp, 0, NULL))
Eric Paris04f6d702010-11-23 06:28:02 +00005909 return NF_DROP;
Paul Moored8395c82008-10-10 10:16:30 -04005910
Paul Mooreeffad8d2008-01-29 08:49:27 -05005911 if (secmark_active)
Stephen Smalleye67b7982023-03-09 13:30:37 -05005912 if (avc_has_perm(peer_sid, skb->secmark,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005913 SECCLASS_PACKET, secmark_perm, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005914 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005915
5916 if (peerlbl_active) {
5917 u32 if_sid;
5918 u32 node_sid;
5919
Paul Moore1d1e1de2021-10-11 17:50:48 -04005920 if (sel_netif_sid(state->net, ifindex, &if_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005921 return NF_DROP;
Stephen Smalleye67b7982023-03-09 13:30:37 -05005922 if (avc_has_perm(peer_sid, if_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005923 SECCLASS_NETIF, NETIF__EGRESS, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005924 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005925
5926 if (sel_netnode_sid(addrp, family, &node_sid))
Eric Paris04f6d702010-11-23 06:28:02 +00005927 return NF_DROP;
Stephen Smalleye67b7982023-03-09 13:30:37 -05005928 if (avc_has_perm(peer_sid, node_sid,
Paul Mooreeffad8d2008-01-29 08:49:27 -05005929 SECCLASS_NODE, NODE__SENDTO, &ad))
Eric Paris1f1aaf82010-11-16 11:52:57 +00005930 return NF_DROP_ERR(-ECONNREFUSED);
Paul Mooreeffad8d2008-01-29 08:49:27 -05005931 }
5932
5933 return NF_ACCEPT;
5934}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005935#endif /* CONFIG_NETFILTER */
5936
Linus Torvalds1da177e2005-04-16 15:20:36 -07005937static int selinux_netlink_send(struct sock *sk, struct sk_buff *skb)
5938{
Paul Moorefb739742020-04-28 09:59:02 -04005939 int rc = 0;
5940 unsigned int msg_len;
5941 unsigned int data_len = skb->len;
5942 unsigned char *data = skb->data;
Huaisheng Yedf4779b2020-01-13 23:03:31 +08005943 struct nlmsghdr *nlh;
5944 struct sk_security_struct *sksec = sk->sk_security;
Paul Moorefb739742020-04-28 09:59:02 -04005945 u16 sclass = sksec->sclass;
5946 u32 perm;
Huaisheng Yedf4779b2020-01-13 23:03:31 +08005947
Paul Moorefb739742020-04-28 09:59:02 -04005948 while (data_len >= nlmsg_total_size(0)) {
5949 nlh = (struct nlmsghdr *)data;
Huaisheng Yedf4779b2020-01-13 23:03:31 +08005950
Paul Moorefb739742020-04-28 09:59:02 -04005951 /* NOTE: the nlmsg_len field isn't reliably set by some netlink
5952 * users which means we can't reject skb's with bogus
5953 * length fields; our solution is to follow what
5954 * netlink_rcv_skb() does and simply skip processing at
5955 * messages with length fields that are clearly junk
5956 */
5957 if (nlh->nlmsg_len < NLMSG_HDRLEN || nlh->nlmsg_len > data_len)
5958 return 0;
5959
5960 rc = selinux_nlmsg_lookup(sclass, nlh->nlmsg_type, &perm);
5961 if (rc == 0) {
5962 rc = sock_has_perm(sk, perm);
5963 if (rc)
5964 return rc;
5965 } else if (rc == -EINVAL) {
5966 /* -EINVAL is a missing msg/perm mapping */
Huaisheng Yedf4779b2020-01-13 23:03:31 +08005967 pr_warn_ratelimited("SELinux: unrecognized netlink"
Paul Moorefb739742020-04-28 09:59:02 -04005968 " message: protocol=%hu nlmsg_type=%hu sclass=%s"
5969 " pid=%d comm=%s\n",
5970 sk->sk_protocol, nlh->nlmsg_type,
5971 secclass_map[sclass - 1].name,
5972 task_pid_nr(current), current->comm);
Stephen Smalleye67b7982023-03-09 13:30:37 -05005973 if (enforcing_enabled() &&
5974 !security_get_allow_unknown())
Paul Moorefb739742020-04-28 09:59:02 -04005975 return rc;
5976 rc = 0;
5977 } else if (rc == -ENOENT) {
5978 /* -ENOENT is a missing socket/class mapping, ignore */
5979 rc = 0;
5980 } else {
5981 return rc;
Huaisheng Yedf4779b2020-01-13 23:03:31 +08005982 }
5983
Paul Moorefb739742020-04-28 09:59:02 -04005984 /* move to the next message after applying netlink padding */
5985 msg_len = NLMSG_ALIGN(nlh->nlmsg_len);
5986 if (msg_len >= data_len)
5987 return 0;
5988 data_len -= msg_len;
5989 data += msg_len;
Huaisheng Yedf4779b2020-01-13 23:03:31 +08005990 }
5991
Paul Moorefb739742020-04-28 09:59:02 -04005992 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993}
5994
Casey Schauflerecd5f822018-11-20 11:55:02 -08005995static void ipc_init_security(struct ipc_security_struct *isec, u16 sclass)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997 isec->sclass = sclass;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05005998 isec->sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999}
6000
Linus Torvalds1da177e2005-04-16 15:20:36 -07006001static int ipc_has_perm(struct kern_ipc_perm *ipc_perms,
Stephen Smalley6af963f2005-05-01 08:58:39 -07006002 u32 perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006003{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006004 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006005 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006006 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006007
Casey Schaufler7c653822018-09-21 17:19:45 -07006008 isec = selinux_ipc(ipc_perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006009
Eric Paris50c205f2012-04-04 15:01:43 -04006010 ad.type = LSM_AUDIT_DATA_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011 ad.u.ipc_id = ipc_perms->key;
6012
Stephen Smalleye67b7982023-03-09 13:30:37 -05006013 return avc_has_perm(sid, isec->sid, isec->sclass, perms, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014}
6015
6016static int selinux_msg_msg_alloc_security(struct msg_msg *msg)
6017{
Huaisheng Yeb82f3f62020-01-10 17:58:56 +08006018 struct msg_security_struct *msec;
6019
6020 msec = selinux_msg_msg(msg);
6021 msec->sid = SECINITSID_UNLABELED;
6022
6023 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006024}
6025
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026/* message queue security operations */
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05006027static int selinux_msg_queue_alloc_security(struct kern_ipc_perm *msq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006029 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006030 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006031 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032
Casey Schauflerecd5f822018-11-20 11:55:02 -08006033 isec = selinux_ipc(msq);
6034 ipc_init_security(isec, SECCLASS_MSGQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006035
Eric Paris50c205f2012-04-04 15:01:43 -04006036 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05006037 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038
Stephen Smalleye67b7982023-03-09 13:30:37 -05006039 return avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
Xu Panda09b71ad2022-09-12 09:17:19 +00006040 MSGQ__CREATE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006041}
6042
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05006043static int selinux_msg_queue_associate(struct kern_ipc_perm *msq, int msqflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006046 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006047 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048
Casey Schaufler7c653822018-09-21 17:19:45 -07006049 isec = selinux_ipc(msq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050
Eric Paris50c205f2012-04-04 15:01:43 -04006051 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05006052 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053
Stephen Smalleye67b7982023-03-09 13:30:37 -05006054 return avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006055 MSGQ__ASSOCIATE, &ad);
6056}
6057
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05006058static int selinux_msg_queue_msgctl(struct kern_ipc_perm *msq, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059{
Christian Göttschea13479b2023-07-06 15:23:27 +02006060 u32 perms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006061
Eric Paris828dfe12008-04-17 13:17:49 -04006062 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063 case IPC_INFO:
6064 case MSG_INFO:
6065 /* No specific object, just general system-wide information. */
Stephen Smalleye67b7982023-03-09 13:30:37 -05006066 return avc_has_perm(current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006067 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068 case IPC_STAT:
6069 case MSG_STAT:
Davidlohr Bueso23c8cec2018-04-10 16:35:30 -07006070 case MSG_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071 perms = MSGQ__GETATTR | MSGQ__ASSOCIATE;
6072 break;
6073 case IPC_SET:
6074 perms = MSGQ__SETATTR;
6075 break;
6076 case IPC_RMID:
6077 perms = MSGQ__DESTROY;
6078 break;
6079 default:
6080 return 0;
6081 }
6082
Christian Göttschea13479b2023-07-06 15:23:27 +02006083 return ipc_has_perm(msq, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084}
6085
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05006086static int selinux_msg_queue_msgsnd(struct kern_ipc_perm *msq, struct msg_msg *msg, int msqflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006087{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088 struct ipc_security_struct *isec;
6089 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006090 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006091 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006092 int rc;
6093
Casey Schaufler7c653822018-09-21 17:19:45 -07006094 isec = selinux_ipc(msq);
6095 msec = selinux_msg_msg(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006096
6097 /*
6098 * First time through, need to assign label to the message
6099 */
6100 if (msec->sid == SECINITSID_UNLABELED) {
6101 /*
6102 * Compute new sid based on current process and
6103 * message queue this message will be stored in
6104 */
Stephen Smalleye67b7982023-03-09 13:30:37 -05006105 rc = security_transition_sid(sid, isec->sid,
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006106 SECCLASS_MSG, NULL, &msec->sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107 if (rc)
6108 return rc;
6109 }
6110
Eric Paris50c205f2012-04-04 15:01:43 -04006111 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05006112 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006113
6114 /* Can this process write to the queue? */
Stephen Smalleye67b7982023-03-09 13:30:37 -05006115 rc = avc_has_perm(sid, isec->sid, SECCLASS_MSGQ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006116 MSGQ__WRITE, &ad);
6117 if (!rc)
6118 /* Can this process send the message */
Stephen Smalleye67b7982023-03-09 13:30:37 -05006119 rc = avc_has_perm(sid, msec->sid, SECCLASS_MSG,
David Howells275bb412008-11-14 10:39:19 +11006120 MSG__SEND, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006121 if (!rc)
6122 /* Can the message be put in the queue? */
Stephen Smalleye67b7982023-03-09 13:30:37 -05006123 rc = avc_has_perm(msec->sid, isec->sid, SECCLASS_MSGQ,
David Howells275bb412008-11-14 10:39:19 +11006124 MSGQ__ENQUEUE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006125
6126 return rc;
6127}
6128
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05006129static int selinux_msg_queue_msgrcv(struct kern_ipc_perm *msq, struct msg_msg *msg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006130 struct task_struct *target,
6131 long type, int mode)
6132{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006133 struct ipc_security_struct *isec;
6134 struct msg_security_struct *msec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006135 struct common_audit_data ad;
Paul Moorea3727a82021-09-23 09:50:11 -04006136 u32 sid = task_sid_obj(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006137 int rc;
6138
Casey Schaufler7c653822018-09-21 17:19:45 -07006139 isec = selinux_ipc(msq);
6140 msec = selinux_msg_msg(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006141
Eric Paris50c205f2012-04-04 15:01:43 -04006142 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermand8c6e852018-03-22 21:22:26 -05006143 ad.u.ipc_id = msq->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006144
Stephen Smalleye67b7982023-03-09 13:30:37 -05006145 rc = avc_has_perm(sid, isec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006146 SECCLASS_MSGQ, MSGQ__READ, &ad);
6147 if (!rc)
Stephen Smalleye67b7982023-03-09 13:30:37 -05006148 rc = avc_has_perm(sid, msec->sid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006149 SECCLASS_MSG, MSG__RECEIVE, &ad);
6150 return rc;
6151}
6152
6153/* Shared Memory security operations */
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006154static int selinux_shm_alloc_security(struct kern_ipc_perm *shp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006155{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006156 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006157 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006158 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006159
Casey Schauflerecd5f822018-11-20 11:55:02 -08006160 isec = selinux_ipc(shp);
6161 ipc_init_security(isec, SECCLASS_SHM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006162
Eric Paris50c205f2012-04-04 15:01:43 -04006163 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006164 ad.u.ipc_id = shp->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006165
Stephen Smalleye67b7982023-03-09 13:30:37 -05006166 return avc_has_perm(sid, isec->sid, SECCLASS_SHM,
Xu Panda09b71ad2022-09-12 09:17:19 +00006167 SHM__CREATE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006168}
6169
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006170static int selinux_shm_associate(struct kern_ipc_perm *shp, int shmflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006171{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006172 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006173 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006174 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006175
Casey Schaufler7c653822018-09-21 17:19:45 -07006176 isec = selinux_ipc(shp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006177
Eric Paris50c205f2012-04-04 15:01:43 -04006178 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006179 ad.u.ipc_id = shp->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006180
Stephen Smalleye67b7982023-03-09 13:30:37 -05006181 return avc_has_perm(sid, isec->sid, SECCLASS_SHM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006182 SHM__ASSOCIATE, &ad);
6183}
6184
6185/* Note, at this point, shp is locked down */
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006186static int selinux_shm_shmctl(struct kern_ipc_perm *shp, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006187{
Christian Göttschea13479b2023-07-06 15:23:27 +02006188 u32 perms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006189
Eric Paris828dfe12008-04-17 13:17:49 -04006190 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006191 case IPC_INFO:
6192 case SHM_INFO:
6193 /* No specific object, just general system-wide information. */
Stephen Smalleye67b7982023-03-09 13:30:37 -05006194 return avc_has_perm(current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006195 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006196 case IPC_STAT:
6197 case SHM_STAT:
Davidlohr Buesoc21a6972018-04-10 16:35:23 -07006198 case SHM_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006199 perms = SHM__GETATTR | SHM__ASSOCIATE;
6200 break;
6201 case IPC_SET:
6202 perms = SHM__SETATTR;
6203 break;
6204 case SHM_LOCK:
6205 case SHM_UNLOCK:
6206 perms = SHM__LOCK;
6207 break;
6208 case IPC_RMID:
6209 perms = SHM__DESTROY;
6210 break;
6211 default:
6212 return 0;
6213 }
6214
Christian Göttschea13479b2023-07-06 15:23:27 +02006215 return ipc_has_perm(shp, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006216}
6217
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006218static int selinux_shm_shmat(struct kern_ipc_perm *shp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219 char __user *shmaddr, int shmflg)
6220{
6221 u32 perms;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006222
6223 if (shmflg & SHM_RDONLY)
6224 perms = SHM__READ;
6225 else
6226 perms = SHM__READ | SHM__WRITE;
6227
Eric W. Biederman7191adf2018-03-22 21:08:27 -05006228 return ipc_has_perm(shp, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006229}
6230
6231/* Semaphore security operations */
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006232static int selinux_sem_alloc_security(struct kern_ipc_perm *sma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006233{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006234 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006235 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006236 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006237
Casey Schauflerecd5f822018-11-20 11:55:02 -08006238 isec = selinux_ipc(sma);
6239 ipc_init_security(isec, SECCLASS_SEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006240
Eric Paris50c205f2012-04-04 15:01:43 -04006241 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006242 ad.u.ipc_id = sma->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243
Stephen Smalleye67b7982023-03-09 13:30:37 -05006244 return avc_has_perm(sid, isec->sid, SECCLASS_SEM,
Xu Panda09b71ad2022-09-12 09:17:19 +00006245 SEM__CREATE, &ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246}
6247
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006248static int selinux_sem_associate(struct kern_ipc_perm *sma, int semflg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250 struct ipc_security_struct *isec;
Thomas Liu2bf49692009-07-14 12:14:09 -04006251 struct common_audit_data ad;
David Howells275bb412008-11-14 10:39:19 +11006252 u32 sid = current_sid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006253
Casey Schaufler7c653822018-09-21 17:19:45 -07006254 isec = selinux_ipc(sma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006255
Eric Paris50c205f2012-04-04 15:01:43 -04006256 ad.type = LSM_AUDIT_DATA_IPC;
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006257 ad.u.ipc_id = sma->key;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006258
Stephen Smalleye67b7982023-03-09 13:30:37 -05006259 return avc_has_perm(sid, isec->sid, SECCLASS_SEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006260 SEM__ASSOCIATE, &ad);
6261}
6262
6263/* Note, at this point, sma is locked down */
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006264static int selinux_sem_semctl(struct kern_ipc_perm *sma, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006265{
6266 int err;
6267 u32 perms;
6268
Eric Paris828dfe12008-04-17 13:17:49 -04006269 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006270 case IPC_INFO:
6271 case SEM_INFO:
6272 /* No specific object, just general system-wide information. */
Stephen Smalleye67b7982023-03-09 13:30:37 -05006273 return avc_has_perm(current_sid(), SECINITSID_KERNEL,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006274 SECCLASS_SYSTEM, SYSTEM__IPC_INFO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006275 case GETPID:
6276 case GETNCNT:
6277 case GETZCNT:
6278 perms = SEM__GETATTR;
6279 break;
6280 case GETVAL:
6281 case GETALL:
6282 perms = SEM__READ;
6283 break;
6284 case SETVAL:
6285 case SETALL:
6286 perms = SEM__WRITE;
6287 break;
6288 case IPC_RMID:
6289 perms = SEM__DESTROY;
6290 break;
6291 case IPC_SET:
6292 perms = SEM__SETATTR;
6293 break;
6294 case IPC_STAT:
6295 case SEM_STAT:
Davidlohr Buesoa280d6d2018-04-10 16:35:26 -07006296 case SEM_STAT_ANY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006297 perms = SEM__GETATTR | SEM__ASSOCIATE;
6298 break;
6299 default:
6300 return 0;
6301 }
6302
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006303 err = ipc_has_perm(sma, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006304 return err;
6305}
6306
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006307static int selinux_sem_semop(struct kern_ipc_perm *sma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006308 struct sembuf *sops, unsigned nsops, int alter)
6309{
6310 u32 perms;
6311
6312 if (alter)
6313 perms = SEM__READ | SEM__WRITE;
6314 else
6315 perms = SEM__READ;
6316
Eric W. Biedermanaefad952018-03-22 20:52:43 -05006317 return ipc_has_perm(sma, perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006318}
6319
6320static int selinux_ipc_permission(struct kern_ipc_perm *ipcp, short flag)
6321{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006322 u32 av = 0;
6323
Linus Torvalds1da177e2005-04-16 15:20:36 -07006324 av = 0;
6325 if (flag & S_IRUGO)
6326 av |= IPC__UNIX_READ;
6327 if (flag & S_IWUGO)
6328 av |= IPC__UNIX_WRITE;
6329
6330 if (av == 0)
6331 return 0;
6332
Stephen Smalley6af963f2005-05-01 08:58:39 -07006333 return ipc_has_perm(ipcp, av);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006334}
6335
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02006336static void selinux_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
6337{
Casey Schaufler7c653822018-09-21 17:19:45 -07006338 struct ipc_security_struct *isec = selinux_ipc(ipcp);
Ahmed S. Darwish713a04ae2008-03-01 21:52:30 +02006339 *secid = isec->sid;
6340}
6341
Eric Paris828dfe12008-04-17 13:17:49 -04006342static void selinux_d_instantiate(struct dentry *dentry, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006343{
6344 if (inode)
6345 inode_doinit_with_dentry(inode, dentry);
6346}
6347
Casey Schaufler762c9342023-09-12 13:56:55 -07006348static int selinux_lsm_getattr(unsigned int attr, struct task_struct *p,
6349 char **value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006350{
David Howells275bb412008-11-14 10:39:19 +11006351 const struct task_security_struct *__tsec;
Dustin Kirkland8c8570f2005-11-03 17:15:16 +00006352 u32 sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006353 int error;
Al Viro04ff9702007-03-12 16:17:58 +00006354 unsigned len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006355
David Howells275bb412008-11-14 10:39:19 +11006356 rcu_read_lock();
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07006357 __tsec = selinux_cred(__task_cred(p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006358
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006359 if (current != p) {
Stephen Smalleye67b7982023-03-09 13:30:37 -05006360 error = avc_has_perm(current_sid(), __tsec->sid,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006361 SECCLASS_PROCESS, PROCESS__GETATTR, NULL);
6362 if (error)
6363 goto bad;
6364 }
6365
Casey Schaufler762c9342023-09-12 13:56:55 -07006366 switch (attr) {
6367 case LSM_ATTR_CURRENT:
David Howells275bb412008-11-14 10:39:19 +11006368 sid = __tsec->sid;
Casey Schaufler762c9342023-09-12 13:56:55 -07006369 break;
6370 case LSM_ATTR_PREV:
David Howells275bb412008-11-14 10:39:19 +11006371 sid = __tsec->osid;
Casey Schaufler762c9342023-09-12 13:56:55 -07006372 break;
6373 case LSM_ATTR_EXEC:
David Howells275bb412008-11-14 10:39:19 +11006374 sid = __tsec->exec_sid;
Casey Schaufler762c9342023-09-12 13:56:55 -07006375 break;
6376 case LSM_ATTR_FSCREATE:
David Howells275bb412008-11-14 10:39:19 +11006377 sid = __tsec->create_sid;
Casey Schaufler762c9342023-09-12 13:56:55 -07006378 break;
6379 case LSM_ATTR_KEYCREATE:
David Howells275bb412008-11-14 10:39:19 +11006380 sid = __tsec->keycreate_sid;
Casey Schaufler762c9342023-09-12 13:56:55 -07006381 break;
6382 case LSM_ATTR_SOCKCREATE:
David Howells275bb412008-11-14 10:39:19 +11006383 sid = __tsec->sockcreate_sid;
Casey Schaufler762c9342023-09-12 13:56:55 -07006384 break;
6385 default:
6386 error = -EOPNOTSUPP;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006387 goto bad;
6388 }
David Howells275bb412008-11-14 10:39:19 +11006389 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006390
6391 if (!sid)
6392 return 0;
6393
Stephen Smalleye67b7982023-03-09 13:30:37 -05006394 error = security_sid_to_context(sid, value, &len);
Al Viro04ff9702007-03-12 16:17:58 +00006395 if (error)
6396 return error;
6397 return len;
David Howells275bb412008-11-14 10:39:19 +11006398
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006399bad:
David Howells275bb412008-11-14 10:39:19 +11006400 rcu_read_unlock();
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006401 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006402}
6403
Casey Schaufler762c9342023-09-12 13:56:55 -07006404static int selinux_lsm_setattr(u64 attr, void *value, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006405{
6406 struct task_security_struct *tsec;
David Howellsd84f4f92008-11-14 10:39:23 +11006407 struct cred *new;
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006408 u32 mysid = current_sid(), sid = 0, ptsid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006409 int error;
6410 char *str = value;
6411
Linus Torvalds1da177e2005-04-16 15:20:36 -07006412 /*
6413 * Basic control over ability to set these attributes at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006414 */
Casey Schaufler762c9342023-09-12 13:56:55 -07006415 switch (attr) {
6416 case LSM_ATTR_EXEC:
Stephen Smalleye67b7982023-03-09 13:30:37 -05006417 error = avc_has_perm(mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006418 PROCESS__SETEXEC, NULL);
Casey Schaufler762c9342023-09-12 13:56:55 -07006419 break;
6420 case LSM_ATTR_FSCREATE:
Stephen Smalleye67b7982023-03-09 13:30:37 -05006421 error = avc_has_perm(mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006422 PROCESS__SETFSCREATE, NULL);
Casey Schaufler762c9342023-09-12 13:56:55 -07006423 break;
6424 case LSM_ATTR_KEYCREATE:
Stephen Smalleye67b7982023-03-09 13:30:37 -05006425 error = avc_has_perm(mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006426 PROCESS__SETKEYCREATE, NULL);
Casey Schaufler762c9342023-09-12 13:56:55 -07006427 break;
6428 case LSM_ATTR_SOCKCREATE:
Stephen Smalleye67b7982023-03-09 13:30:37 -05006429 error = avc_has_perm(mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006430 PROCESS__SETSOCKCREATE, NULL);
Casey Schaufler762c9342023-09-12 13:56:55 -07006431 break;
6432 case LSM_ATTR_CURRENT:
Stephen Smalleye67b7982023-03-09 13:30:37 -05006433 error = avc_has_perm(mysid, mysid, SECCLASS_PROCESS,
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006434 PROCESS__SETCURRENT, NULL);
Casey Schaufler762c9342023-09-12 13:56:55 -07006435 break;
6436 default:
6437 error = -EOPNOTSUPP;
6438 break;
6439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006440 if (error)
6441 return error;
6442
6443 /* Obtain a SID for the context, if one was specified. */
Stephen Smalleya050a572017-01-31 11:54:04 -05006444 if (size && str[0] && str[0] != '\n') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006445 if (str[size-1] == '\n') {
6446 str[size-1] = 0;
6447 size--;
6448 }
Stephen Smalleye67b7982023-03-09 13:30:37 -05006449 error = security_context_to_sid(value, size,
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006450 &sid, GFP_KERNEL);
Casey Schaufler762c9342023-09-12 13:56:55 -07006451 if (error == -EINVAL && attr == LSM_ATTR_FSCREATE) {
Stephen Smalleydb590002017-04-20 11:31:30 -04006452 if (!has_cap_mac_admin(true)) {
Eric Parisd6ea83e2012-04-04 13:45:49 -04006453 struct audit_buffer *ab;
6454 size_t audit_size;
6455
Casey Schaufler762c9342023-09-12 13:56:55 -07006456 /* We strip a nul only if it is at the end,
6457 * otherwise the context contains a nul and
6458 * we should audit that */
Eric Parisd6ea83e2012-04-04 13:45:49 -04006459 if (str[size - 1] == '\0')
6460 audit_size = size - 1;
6461 else
6462 audit_size = size;
Richard Guy Briggscdfb6b32018-05-12 21:58:20 -04006463 ab = audit_log_start(audit_context(),
6464 GFP_ATOMIC,
6465 AUDIT_SELINUX_ERR);
Austin Kim893c47d2021-07-14 01:11:27 +01006466 if (!ab)
6467 return error;
Eric Parisd6ea83e2012-04-04 13:45:49 -04006468 audit_log_format(ab, "op=fscreate invalid_context=");
Casey Schaufler762c9342023-09-12 13:56:55 -07006469 audit_log_n_untrustedstring(ab, value,
6470 audit_size);
Eric Parisd6ea83e2012-04-04 13:45:49 -04006471 audit_log_end(ab);
6472
Stephen Smalley12b29f32008-05-07 13:03:20 -04006473 return error;
Eric Parisd6ea83e2012-04-04 13:45:49 -04006474 }
Stephen Smalleye67b7982023-03-09 13:30:37 -05006475 error = security_context_to_sid_force(value, size,
6476 &sid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04006477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006478 if (error)
6479 return error;
6480 }
6481
David Howellsd84f4f92008-11-14 10:39:23 +11006482 new = prepare_creds();
6483 if (!new)
6484 return -ENOMEM;
6485
Linus Torvalds1da177e2005-04-16 15:20:36 -07006486 /* Permission checking based on the specified context is
6487 performed during the actual operation (execve,
6488 open/mkdir/...), when we know the full context of the
Eric W. Biedermanb8bff592020-03-22 15:46:24 -05006489 operation. See selinux_bprm_creds_for_exec for the execve
Linus Torvalds1da177e2005-04-16 15:20:36 -07006490 checks and may_create for the file creation checks. The
6491 operation will then fail if the context is not permitted. */
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07006492 tsec = selinux_cred(new);
Casey Schaufler762c9342023-09-12 13:56:55 -07006493 if (attr == LSM_ATTR_EXEC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006494 tsec->exec_sid = sid;
Casey Schaufler762c9342023-09-12 13:56:55 -07006495 } else if (attr == LSM_ATTR_FSCREATE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006496 tsec->create_sid = sid;
Casey Schaufler762c9342023-09-12 13:56:55 -07006497 } else if (attr == LSM_ATTR_KEYCREATE) {
Ondrej Mosnacek464c2582019-06-12 10:12:26 +02006498 if (sid) {
Stephen Smalleye67b7982023-03-09 13:30:37 -05006499 error = avc_has_perm(mysid, sid,
Ondrej Mosnacek464c2582019-06-12 10:12:26 +02006500 SECCLASS_KEY, KEY__CREATE, NULL);
6501 if (error)
6502 goto abort_change;
6503 }
Michael LeMay4eb582c2006-06-26 00:24:57 -07006504 tsec->keycreate_sid = sid;
Casey Schaufler762c9342023-09-12 13:56:55 -07006505 } else if (attr == LSM_ATTR_SOCKCREATE) {
Eric Paris42c3e032006-06-26 00:26:03 -07006506 tsec->sockcreate_sid = sid;
Casey Schaufler762c9342023-09-12 13:56:55 -07006507 } else if (attr == LSM_ATTR_CURRENT) {
David Howellsd84f4f92008-11-14 10:39:23 +11006508 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006509 if (sid == 0)
David Howellsd84f4f92008-11-14 10:39:23 +11006510 goto abort_change;
KaiGai Koheid9250de2008-08-28 16:35:57 +09006511
Oleg Nesterov5bb459b2009-07-10 03:48:23 +02006512 if (!current_is_single_threaded()) {
Stephen Smalleye67b7982023-03-09 13:30:37 -05006513 error = security_bounded_transition(tsec->sid, sid);
David Howellsd84f4f92008-11-14 10:39:23 +11006514 if (error)
6515 goto abort_change;
Eric Paris828dfe12008-04-17 13:17:49 -04006516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006517
6518 /* Check permissions for the transition. */
Stephen Smalleye67b7982023-03-09 13:30:37 -05006519 error = avc_has_perm(tsec->sid, sid, SECCLASS_PROCESS,
Eric Paris828dfe12008-04-17 13:17:49 -04006520 PROCESS__DYNTRANSITION, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006521 if (error)
David Howellsd84f4f92008-11-14 10:39:23 +11006522 goto abort_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006523
6524 /* Check for ptracing, and update the task SID if ok.
6525 Otherwise, leave SID unchanged and fail. */
Stephen Smalleybe0554c2017-01-09 10:07:31 -05006526 ptsid = ptrace_parent_sid();
Paul Moore0c6181c2016-03-30 21:41:21 -04006527 if (ptsid != 0) {
Stephen Smalleye67b7982023-03-09 13:30:37 -05006528 error = avc_has_perm(ptsid, sid, SECCLASS_PROCESS,
David Howellsd84f4f92008-11-14 10:39:23 +11006529 PROCESS__PTRACE, NULL);
6530 if (error)
6531 goto abort_change;
6532 }
6533
6534 tsec->sid = sid;
6535 } else {
6536 error = -EINVAL;
6537 goto abort_change;
6538 }
6539
6540 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006541 return size;
David Howellsd84f4f92008-11-14 10:39:23 +11006542
6543abort_change:
6544 abort_creds(new);
6545 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006546}
6547
Paul Moored7cf3412023-10-24 14:44:00 -04006548/**
6549 * selinux_getselfattr - Get SELinux current task attributes
6550 * @attr: the requested attribute
6551 * @ctx: buffer to receive the result
6552 * @size: buffer size (input), buffer size used (output)
6553 * @flags: unused
6554 *
6555 * Fill the passed user space @ctx with the details of the requested
6556 * attribute.
6557 *
6558 * Returns the number of attributes on success, an error code otherwise.
6559 * There will only ever be one attribute.
6560 */
Casey Schaufler762c9342023-09-12 13:56:55 -07006561static int selinux_getselfattr(unsigned int attr, struct lsm_ctx __user *ctx,
Casey Schauflera5a858f2024-03-14 11:31:26 -04006562 u32 *size, u32 flags)
Casey Schaufler762c9342023-09-12 13:56:55 -07006563{
Paul Moored7cf3412023-10-24 14:44:00 -04006564 int rc;
Mickaël Salaün86dc9692024-02-23 20:05:45 +01006565 char *val = NULL;
Paul Moored7cf3412023-10-24 14:44:00 -04006566 int val_len;
Casey Schaufler762c9342023-09-12 13:56:55 -07006567
Paul Moored7cf3412023-10-24 14:44:00 -04006568 val_len = selinux_lsm_getattr(attr, current, &val);
6569 if (val_len < 0)
6570 return val_len;
6571 rc = lsm_fill_user_ctx(ctx, size, val, val_len, LSM_ID_SELINUX, 0);
6572 kfree(val);
6573 return (!rc ? 1 : rc);
Casey Schaufler762c9342023-09-12 13:56:55 -07006574}
6575
6576static int selinux_setselfattr(unsigned int attr, struct lsm_ctx *ctx,
Casey Schauflera5a858f2024-03-14 11:31:26 -04006577 u32 size, u32 flags)
Casey Schaufler762c9342023-09-12 13:56:55 -07006578{
6579 int rc;
6580
6581 rc = selinux_lsm_setattr(attr, ctx->ctx, ctx->ctx_len);
6582 if (rc > 0)
6583 return 0;
6584 return rc;
6585}
6586
6587static int selinux_getprocattr(struct task_struct *p,
6588 const char *name, char **value)
6589{
6590 unsigned int attr = lsm_name_to_attr(name);
6591 int rc;
6592
6593 if (attr) {
6594 rc = selinux_lsm_getattr(attr, p, value);
6595 if (rc != -EOPNOTSUPP)
6596 return rc;
6597 }
6598
6599 return -EINVAL;
6600}
6601
6602static int selinux_setprocattr(const char *name, void *value, size_t size)
6603{
6604 int attr = lsm_name_to_attr(name);
6605
6606 if (attr)
6607 return selinux_lsm_setattr(attr, value, size);
6608 return -EINVAL;
6609}
6610
David Quigley746df9b2013-05-22 12:50:35 -04006611static int selinux_ismaclabel(const char *name)
6612{
6613 return (strcmp(name, XATTR_SELINUX_SUFFIX) == 0);
6614}
6615
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006616static int selinux_secid_to_secctx(u32 secid, char **secdata, u32 *seclen)
6617{
Stephen Smalleye67b7982023-03-09 13:30:37 -05006618 return security_sid_to_context(secid,
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006619 secdata, seclen);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006620}
6621
David Howells7bf570d2008-04-29 20:52:51 +01006622static int selinux_secctx_to_secid(const char *secdata, u32 seclen, u32 *secid)
David Howells63cb3442008-01-15 23:47:35 +00006623{
Stephen Smalleye67b7982023-03-09 13:30:37 -05006624 return security_context_to_sid(secdata, seclen,
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006625 secid, GFP_KERNEL);
David Howells63cb3442008-01-15 23:47:35 +00006626}
6627
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006628static void selinux_release_secctx(char *secdata, u32 seclen)
6629{
Paul Moore088999e2007-08-01 11:12:58 -04006630 kfree(secdata);
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07006631}
6632
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006633static void selinux_inode_invalidate_secctx(struct inode *inode)
6634{
Casey Schaufler80788c22018-09-21 17:19:11 -07006635 struct inode_security_struct *isec = selinux_inode(inode);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006636
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01006637 spin_lock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006638 isec->initialized = LABEL_INVALID;
Andreas Gruenbacher9287aed2016-11-15 11:06:40 +01006639 spin_unlock(&isec->lock);
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05006640}
6641
David P. Quigley1ee65e32009-09-03 14:25:57 -04006642/*
6643 * called with inode->i_mutex locked
6644 */
6645static int selinux_inode_notifysecctx(struct inode *inode, void *ctx, u32 ctxlen)
6646{
Ondrej Mosnacek53e0c2a2018-12-21 21:18:53 +01006647 int rc = selinux_inode_setsecurity(inode, XATTR_SELINUX_SUFFIX,
6648 ctx, ctxlen, 0);
6649 /* Do not return error when suppressing label (SBLABEL_MNT not set). */
6650 return rc == -EOPNOTSUPP ? 0 : rc;
David P. Quigley1ee65e32009-09-03 14:25:57 -04006651}
6652
6653/*
6654 * called with inode->i_mutex locked
6655 */
6656static int selinux_inode_setsecctx(struct dentry *dentry, void *ctx, u32 ctxlen)
6657{
Christian Brauner39f60c12023-01-13 12:49:23 +01006658 return __vfs_setxattr_noperm(&nop_mnt_idmap, dentry, XATTR_NAME_SELINUX,
Tycho Andersenc7c7a1a12021-01-21 14:19:28 +01006659 ctx, ctxlen, 0);
David P. Quigley1ee65e32009-09-03 14:25:57 -04006660}
6661
6662static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen)
6663{
6664 int len = 0;
Christian Brauner4609e1f2023-01-13 12:49:22 +01006665 len = selinux_inode_getsecurity(&nop_mnt_idmap, inode,
Christian Brauner71bc3562021-01-21 14:19:29 +01006666 XATTR_SELINUX_SUFFIX, ctx, true);
David P. Quigley1ee65e32009-09-03 14:25:57 -04006667 if (len < 0)
6668 return len;
6669 *ctxlen = len;
6670 return 0;
6671}
Michael LeMayd7200242006-06-22 14:47:17 -07006672#ifdef CONFIG_KEYS
6673
David Howellsd84f4f92008-11-14 10:39:23 +11006674static int selinux_key_alloc(struct key *k, const struct cred *cred,
David Howells7e047ef2006-06-26 00:24:50 -07006675 unsigned long flags)
Michael LeMayd7200242006-06-22 14:47:17 -07006676{
David Howellsd84f4f92008-11-14 10:39:23 +11006677 const struct task_security_struct *tsec;
Michael LeMayd7200242006-06-22 14:47:17 -07006678 struct key_security_struct *ksec;
6679
6680 ksec = kzalloc(sizeof(struct key_security_struct), GFP_KERNEL);
6681 if (!ksec)
6682 return -ENOMEM;
6683
Casey Schaufler0c6cfa62018-09-21 17:17:16 -07006684 tsec = selinux_cred(cred);
David Howellsd84f4f92008-11-14 10:39:23 +11006685 if (tsec->keycreate_sid)
6686 ksec->sid = tsec->keycreate_sid;
Michael LeMay4eb582c2006-06-26 00:24:57 -07006687 else
David Howellsd84f4f92008-11-14 10:39:23 +11006688 ksec->sid = tsec->sid;
Michael LeMayd7200242006-06-22 14:47:17 -07006689
David Howells275bb412008-11-14 10:39:19 +11006690 k->security = ksec;
Michael LeMayd7200242006-06-22 14:47:17 -07006691 return 0;
6692}
6693
6694static void selinux_key_free(struct key *k)
6695{
6696 struct key_security_struct *ksec = k->security;
6697
6698 k->security = NULL;
6699 kfree(ksec);
6700}
6701
6702static int selinux_key_permission(key_ref_t key_ref,
David Howellsd84f4f92008-11-14 10:39:23 +11006703 const struct cred *cred,
David Howells8c0637e2020-05-12 15:16:29 +01006704 enum key_need_perm need_perm)
Michael LeMayd7200242006-06-22 14:47:17 -07006705{
6706 struct key *key;
Michael LeMayd7200242006-06-22 14:47:17 -07006707 struct key_security_struct *ksec;
David Howells8c0637e2020-05-12 15:16:29 +01006708 u32 perm, sid;
Michael LeMayd7200242006-06-22 14:47:17 -07006709
David Howells8c0637e2020-05-12 15:16:29 +01006710 switch (need_perm) {
6711 case KEY_NEED_VIEW:
6712 perm = KEY__VIEW;
6713 break;
6714 case KEY_NEED_READ:
6715 perm = KEY__READ;
6716 break;
6717 case KEY_NEED_WRITE:
6718 perm = KEY__WRITE;
6719 break;
6720 case KEY_NEED_SEARCH:
6721 perm = KEY__SEARCH;
6722 break;
6723 case KEY_NEED_LINK:
6724 perm = KEY__LINK;
6725 break;
6726 case KEY_NEED_SETATTR:
6727 perm = KEY__SETATTR;
6728 break;
6729 case KEY_NEED_UNLINK:
6730 case KEY_SYSADMIN_OVERRIDE:
6731 case KEY_AUTHTOKEN_OVERRIDE:
6732 case KEY_DEFER_PERM_CHECK:
Michael LeMayd7200242006-06-22 14:47:17 -07006733 return 0;
David Howells8c0637e2020-05-12 15:16:29 +01006734 default:
6735 WARN_ON(1);
6736 return -EPERM;
6737
6738 }
Michael LeMayd7200242006-06-22 14:47:17 -07006739
David Howellsd84f4f92008-11-14 10:39:23 +11006740 sid = cred_sid(cred);
David Howells275bb412008-11-14 10:39:19 +11006741 key = key_ref_to_ptr(key_ref);
6742 ksec = key->security;
6743
Stephen Smalleye67b7982023-03-09 13:30:37 -05006744 return avc_has_perm(sid, ksec->sid, SECCLASS_KEY, perm, NULL);
Michael LeMayd7200242006-06-22 14:47:17 -07006745}
6746
David Howells70a5bb72008-04-29 01:01:26 -07006747static int selinux_key_getsecurity(struct key *key, char **_buffer)
6748{
6749 struct key_security_struct *ksec = key->security;
6750 char *context = NULL;
6751 unsigned len;
6752 int rc;
6753
Stephen Smalleye67b7982023-03-09 13:30:37 -05006754 rc = security_sid_to_context(ksec->sid,
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006755 &context, &len);
David Howells70a5bb72008-04-29 01:01:26 -07006756 if (!rc)
6757 rc = len;
6758 *_buffer = context;
6759 return rc;
6760}
David Howells3e412cc2020-01-14 17:07:13 +00006761
6762#ifdef CONFIG_KEY_NOTIFICATIONS
6763static int selinux_watch_key(struct key *key)
6764{
6765 struct key_security_struct *ksec = key->security;
6766 u32 sid = current_sid();
6767
Stephen Smalleye67b7982023-03-09 13:30:37 -05006768 return avc_has_perm(sid, ksec->sid, SECCLASS_KEY, KEY__VIEW, NULL);
David Howells3e412cc2020-01-14 17:07:13 +00006769}
6770#endif
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006771#endif
David Howells70a5bb72008-04-29 01:01:26 -07006772
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006773#ifdef CONFIG_SECURITY_INFINIBAND
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006774static int selinux_ib_pkey_access(void *ib_sec, u64 subnet_prefix, u16 pkey_val)
6775{
6776 struct common_audit_data ad;
6777 int err;
6778 u32 sid = 0;
6779 struct ib_security_struct *sec = ib_sec;
6780 struct lsm_ibpkey_audit ibpkey;
6781
Daniel Jurgens409dcf32017-05-19 15:48:59 +03006782 err = sel_ib_pkey_sid(subnet_prefix, pkey_val, &sid);
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006783 if (err)
6784 return err;
6785
6786 ad.type = LSM_AUDIT_DATA_IBPKEY;
6787 ibpkey.subnet_prefix = subnet_prefix;
6788 ibpkey.pkey = pkey_val;
6789 ad.u.ibpkey = &ibpkey;
Stephen Smalleye67b7982023-03-09 13:30:37 -05006790 return avc_has_perm(sec->sid, sid,
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03006791 SECCLASS_INFINIBAND_PKEY,
6792 INFINIBAND_PKEY__ACCESS, &ad);
6793}
6794
Daniel Jurgensab861df2017-05-19 15:48:58 +03006795static int selinux_ib_endport_manage_subnet(void *ib_sec, const char *dev_name,
6796 u8 port_num)
6797{
6798 struct common_audit_data ad;
6799 int err;
6800 u32 sid = 0;
6801 struct ib_security_struct *sec = ib_sec;
6802 struct lsm_ibendport_audit ibendport;
6803
Stephen Smalleye67b7982023-03-09 13:30:37 -05006804 err = security_ib_endport_sid(dev_name, port_num,
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05006805 &sid);
Daniel Jurgensab861df2017-05-19 15:48:58 +03006806
6807 if (err)
6808 return err;
6809
6810 ad.type = LSM_AUDIT_DATA_IBENDPORT;
Ondrej Mosnacek869cbee2021-05-12 16:32:10 +02006811 ibendport.dev_name = dev_name;
Daniel Jurgensab861df2017-05-19 15:48:58 +03006812 ibendport.port = port_num;
6813 ad.u.ibendport = &ibendport;
Stephen Smalleye67b7982023-03-09 13:30:37 -05006814 return avc_has_perm(sec->sid, sid,
Daniel Jurgensab861df2017-05-19 15:48:58 +03006815 SECCLASS_INFINIBAND_ENDPORT,
6816 INFINIBAND_ENDPORT__MANAGE_SUBNET, &ad);
6817}
6818
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03006819static int selinux_ib_alloc_security(void **ib_sec)
6820{
6821 struct ib_security_struct *sec;
6822
6823 sec = kzalloc(sizeof(*sec), GFP_KERNEL);
6824 if (!sec)
6825 return -ENOMEM;
6826 sec->sid = current_sid();
6827
6828 *ib_sec = sec;
6829 return 0;
6830}
6831
6832static void selinux_ib_free_security(void *ib_sec)
6833{
6834 kfree(ib_sec);
6835}
Michael LeMayd7200242006-06-22 14:47:17 -07006836#endif
6837
Chenbo Fengec27c352017-10-18 13:00:25 -07006838#ifdef CONFIG_BPF_SYSCALL
6839static int selinux_bpf(int cmd, union bpf_attr *attr,
6840 unsigned int size)
6841{
6842 u32 sid = current_sid();
6843 int ret;
6844
6845 switch (cmd) {
6846 case BPF_MAP_CREATE:
Stephen Smalleye67b7982023-03-09 13:30:37 -05006847 ret = avc_has_perm(sid, sid, SECCLASS_BPF, BPF__MAP_CREATE,
Chenbo Fengec27c352017-10-18 13:00:25 -07006848 NULL);
6849 break;
6850 case BPF_PROG_LOAD:
Stephen Smalleye67b7982023-03-09 13:30:37 -05006851 ret = avc_has_perm(sid, sid, SECCLASS_BPF, BPF__PROG_LOAD,
Chenbo Fengec27c352017-10-18 13:00:25 -07006852 NULL);
6853 break;
6854 default:
6855 ret = 0;
6856 break;
6857 }
6858
6859 return ret;
6860}
6861
6862static u32 bpf_map_fmode_to_av(fmode_t fmode)
6863{
6864 u32 av = 0;
6865
6866 if (fmode & FMODE_READ)
6867 av |= BPF__MAP_READ;
6868 if (fmode & FMODE_WRITE)
6869 av |= BPF__MAP_WRITE;
6870 return av;
6871}
6872
Chenbo Fengf66e4482017-10-18 13:00:26 -07006873/* This function will check the file pass through unix socket or binder to see
Jonas Lindner9691e4f2022-06-09 00:36:16 +02006874 * if it is a bpf related object. And apply corresponding checks on the bpf
Chenbo Fengf66e4482017-10-18 13:00:26 -07006875 * object based on the type. The bpf maps and programs, not like other files and
6876 * socket, are using a shared anonymous inode inside the kernel as their inode.
6877 * So checking that inode cannot identify if the process have privilege to
6878 * access the bpf object and that's why we have to add this additional check in
6879 * selinux_file_receive and selinux_binder_transfer_files.
6880 */
Khadija Kamran8e4672d2023-08-12 20:31:08 +05006881static int bpf_fd_pass(const struct file *file, u32 sid)
Chenbo Fengf66e4482017-10-18 13:00:26 -07006882{
6883 struct bpf_security_struct *bpfsec;
6884 struct bpf_prog *prog;
6885 struct bpf_map *map;
6886 int ret;
6887
6888 if (file->f_op == &bpf_map_fops) {
6889 map = file->private_data;
6890 bpfsec = map->security;
Stephen Smalleye67b7982023-03-09 13:30:37 -05006891 ret = avc_has_perm(sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengf66e4482017-10-18 13:00:26 -07006892 bpf_map_fmode_to_av(file->f_mode), NULL);
6893 if (ret)
6894 return ret;
6895 } else if (file->f_op == &bpf_prog_fops) {
6896 prog = file->private_data;
6897 bpfsec = prog->aux->security;
Stephen Smalleye67b7982023-03-09 13:30:37 -05006898 ret = avc_has_perm(sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengf66e4482017-10-18 13:00:26 -07006899 BPF__PROG_RUN, NULL);
6900 if (ret)
6901 return ret;
6902 }
6903 return 0;
6904}
6905
Chenbo Fengec27c352017-10-18 13:00:25 -07006906static int selinux_bpf_map(struct bpf_map *map, fmode_t fmode)
6907{
6908 u32 sid = current_sid();
6909 struct bpf_security_struct *bpfsec;
6910
6911 bpfsec = map->security;
Stephen Smalleye67b7982023-03-09 13:30:37 -05006912 return avc_has_perm(sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengec27c352017-10-18 13:00:25 -07006913 bpf_map_fmode_to_av(fmode), NULL);
6914}
6915
6916static int selinux_bpf_prog(struct bpf_prog *prog)
6917{
6918 u32 sid = current_sid();
6919 struct bpf_security_struct *bpfsec;
6920
6921 bpfsec = prog->aux->security;
Stephen Smalleye67b7982023-03-09 13:30:37 -05006922 return avc_has_perm(sid, bpfsec->sid, SECCLASS_BPF,
Chenbo Fengec27c352017-10-18 13:00:25 -07006923 BPF__PROG_RUN, NULL);
6924}
6925
Andrii Nakryikoa2431c72024-01-23 18:21:07 -08006926static int selinux_bpf_map_create(struct bpf_map *map, union bpf_attr *attr,
6927 struct bpf_token *token)
Chenbo Fengec27c352017-10-18 13:00:25 -07006928{
6929 struct bpf_security_struct *bpfsec;
6930
6931 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6932 if (!bpfsec)
6933 return -ENOMEM;
6934
6935 bpfsec->sid = current_sid();
6936 map->security = bpfsec;
6937
6938 return 0;
6939}
6940
6941static void selinux_bpf_map_free(struct bpf_map *map)
6942{
6943 struct bpf_security_struct *bpfsec = map->security;
6944
6945 map->security = NULL;
6946 kfree(bpfsec);
6947}
6948
Andrii Nakryiko1b677722024-01-23 18:21:06 -08006949static int selinux_bpf_prog_load(struct bpf_prog *prog, union bpf_attr *attr,
6950 struct bpf_token *token)
Chenbo Fengec27c352017-10-18 13:00:25 -07006951{
6952 struct bpf_security_struct *bpfsec;
6953
6954 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6955 if (!bpfsec)
6956 return -ENOMEM;
6957
6958 bpfsec->sid = current_sid();
Andrii Nakryiko1b677722024-01-23 18:21:06 -08006959 prog->aux->security = bpfsec;
Chenbo Fengec27c352017-10-18 13:00:25 -07006960
6961 return 0;
6962}
6963
Andrii Nakryiko1b677722024-01-23 18:21:06 -08006964static void selinux_bpf_prog_free(struct bpf_prog *prog)
Chenbo Fengec27c352017-10-18 13:00:25 -07006965{
Andrii Nakryiko1b677722024-01-23 18:21:06 -08006966 struct bpf_security_struct *bpfsec = prog->aux->security;
Chenbo Fengec27c352017-10-18 13:00:25 -07006967
Andrii Nakryiko1b677722024-01-23 18:21:06 -08006968 prog->aux->security = NULL;
Chenbo Fengec27c352017-10-18 13:00:25 -07006969 kfree(bpfsec);
6970}
Andrii Nakryiko00544932024-01-23 18:21:14 -08006971
6972static int selinux_bpf_token_create(struct bpf_token *token, union bpf_attr *attr,
6973 struct path *path)
6974{
6975 struct bpf_security_struct *bpfsec;
6976
6977 bpfsec = kzalloc(sizeof(*bpfsec), GFP_KERNEL);
6978 if (!bpfsec)
6979 return -ENOMEM;
6980
6981 bpfsec->sid = current_sid();
6982 token->security = bpfsec;
6983
6984 return 0;
6985}
6986
6987static void selinux_bpf_token_free(struct bpf_token *token)
6988{
6989 struct bpf_security_struct *bpfsec = token->security;
6990
6991 token->security = NULL;
6992 kfree(bpfsec);
6993}
Chenbo Fengec27c352017-10-18 13:00:25 -07006994#endif
6995
Paul Mooref22f9aa2023-03-17 12:43:07 -04006996struct lsm_blob_sizes selinux_blob_sizes __ro_after_init = {
Casey Schauflerbbd36622018-11-12 09:30:56 -08006997 .lbs_cred = sizeof(struct task_security_struct),
Casey Schaufler33bf60c2018-11-12 12:02:49 -08006998 .lbs_file = sizeof(struct file_security_struct),
Casey Schauflerafb1cbe32018-09-21 17:19:29 -07006999 .lbs_inode = sizeof(struct inode_security_struct),
Casey Schauflerecd5f822018-11-20 11:55:02 -08007000 .lbs_ipc = sizeof(struct ipc_security_struct),
7001 .lbs_msg_msg = sizeof(struct msg_security_struct),
Casey Schaufler1aea7802021-04-22 17:41:15 +02007002 .lbs_superblock = sizeof(struct superblock_security_struct),
Roberto Sassu6bcdfd22023-06-10 09:57:35 +02007003 .lbs_xattr_count = SELINUX_INODE_INIT_XATTRS,
Casey Schauflerbbd36622018-11-12 09:30:56 -08007004};
7005
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -04007006#ifdef CONFIG_PERF_EVENTS
7007static int selinux_perf_event_open(struct perf_event_attr *attr, int type)
7008{
7009 u32 requested, sid = current_sid();
7010
7011 if (type == PERF_SECURITY_OPEN)
7012 requested = PERF_EVENT__OPEN;
7013 else if (type == PERF_SECURITY_CPU)
7014 requested = PERF_EVENT__CPU;
7015 else if (type == PERF_SECURITY_KERNEL)
7016 requested = PERF_EVENT__KERNEL;
7017 else if (type == PERF_SECURITY_TRACEPOINT)
7018 requested = PERF_EVENT__TRACEPOINT;
7019 else
7020 return -EINVAL;
7021
Stephen Smalleye67b7982023-03-09 13:30:37 -05007022 return avc_has_perm(sid, sid, SECCLASS_PERF_EVENT,
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -04007023 requested, NULL);
7024}
7025
7026static int selinux_perf_event_alloc(struct perf_event *event)
7027{
7028 struct perf_event_security_struct *perfsec;
7029
7030 perfsec = kzalloc(sizeof(*perfsec), GFP_KERNEL);
7031 if (!perfsec)
7032 return -ENOMEM;
7033
7034 perfsec->sid = current_sid();
7035 event->security = perfsec;
7036
7037 return 0;
7038}
7039
7040static void selinux_perf_event_free(struct perf_event *event)
7041{
7042 struct perf_event_security_struct *perfsec = event->security;
7043
7044 event->security = NULL;
7045 kfree(perfsec);
7046}
7047
7048static int selinux_perf_event_read(struct perf_event *event)
7049{
7050 struct perf_event_security_struct *perfsec = event->security;
7051 u32 sid = current_sid();
7052
Stephen Smalleye67b7982023-03-09 13:30:37 -05007053 return avc_has_perm(sid, perfsec->sid,
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -04007054 SECCLASS_PERF_EVENT, PERF_EVENT__READ, NULL);
7055}
7056
7057static int selinux_perf_event_write(struct perf_event *event)
7058{
7059 struct perf_event_security_struct *perfsec = event->security;
7060 u32 sid = current_sid();
7061
Stephen Smalleye67b7982023-03-09 13:30:37 -05007062 return avc_has_perm(sid, perfsec->sid,
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -04007063 SECCLASS_PERF_EVENT, PERF_EVENT__WRITE, NULL);
7064}
7065#endif
7066
Paul Moore740b0342021-02-23 18:16:45 -05007067#ifdef CONFIG_IO_URING
7068/**
7069 * selinux_uring_override_creds - check the requested cred override
7070 * @new: the target creds
7071 *
7072 * Check to see if the current task is allowed to override it's credentials
7073 * to service an io_uring operation.
7074 */
7075static int selinux_uring_override_creds(const struct cred *new)
7076{
Stephen Smalleye67b7982023-03-09 13:30:37 -05007077 return avc_has_perm(current_sid(), cred_sid(new),
Paul Moore740b0342021-02-23 18:16:45 -05007078 SECCLASS_IO_URING, IO_URING__OVERRIDE_CREDS, NULL);
7079}
7080
7081/**
7082 * selinux_uring_sqpoll - check if a io_uring polling thread can be created
7083 *
7084 * Check to see if the current task is allowed to create a new io_uring
7085 * kernel polling thread.
7086 */
7087static int selinux_uring_sqpoll(void)
7088{
Christian Göttschea13479b2023-07-06 15:23:27 +02007089 u32 sid = current_sid();
Paul Moore740b0342021-02-23 18:16:45 -05007090
Stephen Smalleye67b7982023-03-09 13:30:37 -05007091 return avc_has_perm(sid, sid,
Paul Moore740b0342021-02-23 18:16:45 -05007092 SECCLASS_IO_URING, IO_URING__SQPOLL, NULL);
7093}
Paul Mooref4d653d2022-08-10 15:55:36 -04007094
7095/**
7096 * selinux_uring_cmd - check if IORING_OP_URING_CMD is allowed
7097 * @ioucmd: the io_uring command structure
7098 *
7099 * Check to see if the current domain is allowed to execute an
7100 * IORING_OP_URING_CMD against the device/file specified in @ioucmd.
7101 *
7102 */
7103static int selinux_uring_cmd(struct io_uring_cmd *ioucmd)
7104{
7105 struct file *file = ioucmd->file;
7106 struct inode *inode = file_inode(file);
7107 struct inode_security_struct *isec = selinux_inode(inode);
7108 struct common_audit_data ad;
7109
7110 ad.type = LSM_AUDIT_DATA_FILE;
7111 ad.u.file = file;
7112
Stephen Smalleye67b7982023-03-09 13:30:37 -05007113 return avc_has_perm(current_sid(), isec->sid,
Paul Mooref4d653d2022-08-10 15:55:36 -04007114 SECCLASS_IO_URING, IO_URING__CMD, &ad);
7115}
Paul Moore740b0342021-02-23 18:16:45 -05007116#endif /* CONFIG_IO_URING */
7117
Paul Mooreb1a867e2023-11-10 12:09:33 -05007118static const struct lsm_id selinux_lsmid = {
Casey Schauflerf3b87882023-09-12 13:56:46 -07007119 .name = "selinux",
7120 .id = LSM_ID_SELINUX,
7121};
7122
Ondrej Mosnacekcfff75d2020-01-08 15:09:58 +01007123/*
7124 * IMPORTANT NOTE: When adding new hooks, please be careful to keep this order:
7125 * 1. any hooks that don't belong to (2.) or (3.) below,
7126 * 2. hooks that both access structures allocated by other hooks, and allocate
7127 * structures that can be later accessed by other hooks (mostly "cloning"
7128 * hooks),
7129 * 3. hooks that only allocate structures that can be later accessed by other
7130 * hooks ("allocating" hooks).
7131 *
7132 * Please follow block comment delimiters in the list to keep this order.
Ondrej Mosnacekcfff75d2020-01-08 15:09:58 +01007133 */
Paul Mooref22f9aa2023-03-17 12:43:07 -04007134static struct security_hook_list selinux_hooks[] __ro_after_init = {
Casey Schauflere20b0432015-05-02 15:11:36 -07007135 LSM_HOOK_INIT(binder_set_context_mgr, selinux_binder_set_context_mgr),
7136 LSM_HOOK_INIT(binder_transaction, selinux_binder_transaction),
7137 LSM_HOOK_INIT(binder_transfer_binder, selinux_binder_transfer_binder),
7138 LSM_HOOK_INIT(binder_transfer_file, selinux_binder_transfer_file),
Ahmed S. Darwish076c54c2008-03-06 18:09:10 +02007139
Casey Schauflere20b0432015-05-02 15:11:36 -07007140 LSM_HOOK_INIT(ptrace_access_check, selinux_ptrace_access_check),
7141 LSM_HOOK_INIT(ptrace_traceme, selinux_ptrace_traceme),
7142 LSM_HOOK_INIT(capget, selinux_capget),
7143 LSM_HOOK_INIT(capset, selinux_capset),
7144 LSM_HOOK_INIT(capable, selinux_capable),
7145 LSM_HOOK_INIT(quotactl, selinux_quotactl),
7146 LSM_HOOK_INIT(quota_on, selinux_quota_on),
7147 LSM_HOOK_INIT(syslog, selinux_syslog),
7148 LSM_HOOK_INIT(vm_enough_memory, selinux_vm_enough_memory),
Stephen Smalley79af7302015-01-21 10:54:10 -05007149
Casey Schauflere20b0432015-05-02 15:11:36 -07007150 LSM_HOOK_INIT(netlink_send, selinux_netlink_send),
Linus Torvalds1da177e2005-04-16 15:20:36 -07007151
Eric W. Biedermanb8bff592020-03-22 15:46:24 -05007152 LSM_HOOK_INIT(bprm_creds_for_exec, selinux_bprm_creds_for_exec),
Casey Schauflere20b0432015-05-02 15:11:36 -07007153 LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds),
7154 LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds),
Linus Torvalds1da177e2005-04-16 15:20:36 -07007155
Al Viro204cc0c2018-12-13 13:41:47 -05007156 LSM_HOOK_INIT(sb_free_mnt_opts, selinux_free_mnt_opts),
Olga Kornievskaia69c4a422021-02-26 22:37:55 -05007157 LSM_HOOK_INIT(sb_mnt_opts_compat, selinux_sb_mnt_opts_compat),
Casey Schauflere20b0432015-05-02 15:11:36 -07007158 LSM_HOOK_INIT(sb_remount, selinux_sb_remount),
7159 LSM_HOOK_INIT(sb_kern_mount, selinux_sb_kern_mount),
7160 LSM_HOOK_INIT(sb_show_options, selinux_sb_show_options),
7161 LSM_HOOK_INIT(sb_statfs, selinux_sb_statfs),
7162 LSM_HOOK_INIT(sb_mount, selinux_mount),
7163 LSM_HOOK_INIT(sb_umount, selinux_umount),
7164 LSM_HOOK_INIT(sb_set_mnt_opts, selinux_set_mnt_opts),
7165 LSM_HOOK_INIT(sb_clone_mnt_opts, selinux_sb_clone_mnt_opts),
Linus Torvalds1da177e2005-04-16 15:20:36 -07007166
Stephen Smalley98aa003452020-01-17 15:24:07 -05007167 LSM_HOOK_INIT(move_mount, selinux_move_mount),
7168
Casey Schauflere20b0432015-05-02 15:11:36 -07007169 LSM_HOOK_INIT(dentry_init_security, selinux_dentry_init_security),
Vivek Goyala518b0a2016-07-13 10:44:53 -04007170 LSM_HOOK_INIT(dentry_create_files_as, selinux_dentry_create_files_as),
Eric Parise0007522008-03-05 10:31:54 -05007171
Casey Schauflere20b0432015-05-02 15:11:36 -07007172 LSM_HOOK_INIT(inode_free_security, selinux_inode_free_security),
7173 LSM_HOOK_INIT(inode_init_security, selinux_inode_init_security),
Daniel Colascione29cd6592021-01-08 14:22:22 -08007174 LSM_HOOK_INIT(inode_init_security_anon, selinux_inode_init_security_anon),
Casey Schauflere20b0432015-05-02 15:11:36 -07007175 LSM_HOOK_INIT(inode_create, selinux_inode_create),
7176 LSM_HOOK_INIT(inode_link, selinux_inode_link),
7177 LSM_HOOK_INIT(inode_unlink, selinux_inode_unlink),
7178 LSM_HOOK_INIT(inode_symlink, selinux_inode_symlink),
7179 LSM_HOOK_INIT(inode_mkdir, selinux_inode_mkdir),
7180 LSM_HOOK_INIT(inode_rmdir, selinux_inode_rmdir),
7181 LSM_HOOK_INIT(inode_mknod, selinux_inode_mknod),
7182 LSM_HOOK_INIT(inode_rename, selinux_inode_rename),
7183 LSM_HOOK_INIT(inode_readlink, selinux_inode_readlink),
7184 LSM_HOOK_INIT(inode_follow_link, selinux_inode_follow_link),
7185 LSM_HOOK_INIT(inode_permission, selinux_inode_permission),
7186 LSM_HOOK_INIT(inode_setattr, selinux_inode_setattr),
7187 LSM_HOOK_INIT(inode_getattr, selinux_inode_getattr),
7188 LSM_HOOK_INIT(inode_setxattr, selinux_inode_setxattr),
7189 LSM_HOOK_INIT(inode_post_setxattr, selinux_inode_post_setxattr),
7190 LSM_HOOK_INIT(inode_getxattr, selinux_inode_getxattr),
7191 LSM_HOOK_INIT(inode_listxattr, selinux_inode_listxattr),
7192 LSM_HOOK_INIT(inode_removexattr, selinux_inode_removexattr),
Christian Brauner1bdeb212022-09-22 17:17:08 +02007193 LSM_HOOK_INIT(inode_set_acl, selinux_inode_set_acl),
7194 LSM_HOOK_INIT(inode_get_acl, selinux_inode_get_acl),
7195 LSM_HOOK_INIT(inode_remove_acl, selinux_inode_remove_acl),
Casey Schauflere20b0432015-05-02 15:11:36 -07007196 LSM_HOOK_INIT(inode_getsecurity, selinux_inode_getsecurity),
7197 LSM_HOOK_INIT(inode_setsecurity, selinux_inode_setsecurity),
7198 LSM_HOOK_INIT(inode_listsecurity, selinux_inode_listsecurity),
7199 LSM_HOOK_INIT(inode_getsecid, selinux_inode_getsecid),
Vivek Goyal56909eb2016-07-13 10:44:48 -04007200 LSM_HOOK_INIT(inode_copy_up, selinux_inode_copy_up),
Vivek Goyal19472b62016-07-13 10:44:50 -04007201 LSM_HOOK_INIT(inode_copy_up_xattr, selinux_inode_copy_up_xattr),
Aaron Goidelac5656d2019-08-12 11:20:00 -04007202 LSM_HOOK_INIT(path_notify, selinux_path_notify),
Linus Torvalds1da177e2005-04-16 15:20:36 -07007203
Ondrej Mosnacekec882da2019-02-22 15:57:17 +01007204 LSM_HOOK_INIT(kernfs_init_security, selinux_kernfs_init_security),
7205
Casey Schauflere20b0432015-05-02 15:11:36 -07007206 LSM_HOOK_INIT(file_permission, selinux_file_permission),
7207 LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security),
Casey Schauflere20b0432015-05-02 15:11:36 -07007208 LSM_HOOK_INIT(file_ioctl, selinux_file_ioctl),
Alfred Piccionif1bb47a2023-12-19 10:09:09 +01007209 LSM_HOOK_INIT(file_ioctl_compat, selinux_file_ioctl_compat),
Casey Schauflere20b0432015-05-02 15:11:36 -07007210 LSM_HOOK_INIT(mmap_file, selinux_mmap_file),
7211 LSM_HOOK_INIT(mmap_addr, selinux_mmap_addr),
7212 LSM_HOOK_INIT(file_mprotect, selinux_file_mprotect),
7213 LSM_HOOK_INIT(file_lock, selinux_file_lock),
7214 LSM_HOOK_INIT(file_fcntl, selinux_file_fcntl),
7215 LSM_HOOK_INIT(file_set_fowner, selinux_file_set_fowner),
7216 LSM_HOOK_INIT(file_send_sigiotask, selinux_file_send_sigiotask),
7217 LSM_HOOK_INIT(file_receive, selinux_file_receive),
Linus Torvalds1da177e2005-04-16 15:20:36 -07007218
Casey Schauflere20b0432015-05-02 15:11:36 -07007219 LSM_HOOK_INIT(file_open, selinux_file_open),
Linus Torvalds1da177e2005-04-16 15:20:36 -07007220
Tetsuo Handaa79be232017-03-28 23:08:45 +09007221 LSM_HOOK_INIT(task_alloc, selinux_task_alloc),
Casey Schauflere20b0432015-05-02 15:11:36 -07007222 LSM_HOOK_INIT(cred_prepare, selinux_cred_prepare),
7223 LSM_HOOK_INIT(cred_transfer, selinux_cred_transfer),
Matthew Garrett3ec30112018-01-08 13:36:19 -08007224 LSM_HOOK_INIT(cred_getsecid, selinux_cred_getsecid),
Casey Schauflere20b0432015-05-02 15:11:36 -07007225 LSM_HOOK_INIT(kernel_act_as, selinux_kernel_act_as),
7226 LSM_HOOK_INIT(kernel_create_files_as, selinux_kernel_create_files_as),
7227 LSM_HOOK_INIT(kernel_module_request, selinux_kernel_module_request),
Mimi Zoharc77b8cd2018-07-13 14:06:02 -04007228 LSM_HOOK_INIT(kernel_load_data, selinux_kernel_load_data),
Jeff Vander Stoep61d612ea2016-04-05 13:06:27 -07007229 LSM_HOOK_INIT(kernel_read_file, selinux_kernel_read_file),
Casey Schauflere20b0432015-05-02 15:11:36 -07007230 LSM_HOOK_INIT(task_setpgid, selinux_task_setpgid),
7231 LSM_HOOK_INIT(task_getpgid, selinux_task_getpgid),
7232 LSM_HOOK_INIT(task_getsid, selinux_task_getsid),
Paul Moore63269482021-09-29 11:01:21 -04007233 LSM_HOOK_INIT(current_getsecid_subj, selinux_current_getsecid_subj),
Paul Mooreeb1231f2021-02-18 15:13:40 -05007234 LSM_HOOK_INIT(task_getsecid_obj, selinux_task_getsecid_obj),
Casey Schauflere20b0432015-05-02 15:11:36 -07007235 LSM_HOOK_INIT(task_setnice, selinux_task_setnice),
7236 LSM_HOOK_INIT(task_setioprio, selinux_task_setioprio),
7237 LSM_HOOK_INIT(task_getioprio, selinux_task_getioprio),
Stephen Smalley791ec492017-02-17 07:57:00 -05007238 LSM_HOOK_INIT(task_prlimit, selinux_task_prlimit),
Casey Schauflere20b0432015-05-02 15:11:36 -07007239 LSM_HOOK_INIT(task_setrlimit, selinux_task_setrlimit),
7240 LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler),
7241 LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler),
7242 LSM_HOOK_INIT(task_movememory, selinux_task_movememory),
7243 LSM_HOOK_INIT(task_kill, selinux_task_kill),
Casey Schauflere20b0432015-05-02 15:11:36 -07007244 LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode),
Frederick Lawlered5d44d2022-08-15 11:20:28 -05007245 LSM_HOOK_INIT(userns_create, selinux_userns_create),
Yuichi Nakamura788e7dd2007-09-14 09:27:07 +09007246
Casey Schauflere20b0432015-05-02 15:11:36 -07007247 LSM_HOOK_INIT(ipc_permission, selinux_ipc_permission),
7248 LSM_HOOK_INIT(ipc_getsecid, selinux_ipc_getsecid),
Linus Torvalds1da177e2005-04-16 15:20:36 -07007249
Casey Schauflere20b0432015-05-02 15:11:36 -07007250 LSM_HOOK_INIT(msg_queue_associate, selinux_msg_queue_associate),
7251 LSM_HOOK_INIT(msg_queue_msgctl, selinux_msg_queue_msgctl),
7252 LSM_HOOK_INIT(msg_queue_msgsnd, selinux_msg_queue_msgsnd),
7253 LSM_HOOK_INIT(msg_queue_msgrcv, selinux_msg_queue_msgrcv),
Linus Torvalds1da177e2005-04-16 15:20:36 -07007254
Casey Schauflere20b0432015-05-02 15:11:36 -07007255 LSM_HOOK_INIT(shm_associate, selinux_shm_associate),
7256 LSM_HOOK_INIT(shm_shmctl, selinux_shm_shmctl),
7257 LSM_HOOK_INIT(shm_shmat, selinux_shm_shmat),
Linus Torvalds1da177e2005-04-16 15:20:36 -07007258
Casey Schauflere20b0432015-05-02 15:11:36 -07007259 LSM_HOOK_INIT(sem_associate, selinux_sem_associate),
7260 LSM_HOOK_INIT(sem_semctl, selinux_sem_semctl),
7261 LSM_HOOK_INIT(sem_semop, selinux_sem_semop),
Linus Torvalds1da177e2005-04-16 15:20:36 -07007262
Casey Schauflere20b0432015-05-02 15:11:36 -07007263 LSM_HOOK_INIT(d_instantiate, selinux_d_instantiate),
Linus Torvalds1da177e2005-04-16 15:20:36 -07007264
Casey Schaufler762c9342023-09-12 13:56:55 -07007265 LSM_HOOK_INIT(getselfattr, selinux_getselfattr),
7266 LSM_HOOK_INIT(setselfattr, selinux_setselfattr),
Casey Schauflere20b0432015-05-02 15:11:36 -07007267 LSM_HOOK_INIT(getprocattr, selinux_getprocattr),
7268 LSM_HOOK_INIT(setprocattr, selinux_setprocattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07007269
Casey Schauflere20b0432015-05-02 15:11:36 -07007270 LSM_HOOK_INIT(ismaclabel, selinux_ismaclabel),
Casey Schauflere20b0432015-05-02 15:11:36 -07007271 LSM_HOOK_INIT(secctx_to_secid, selinux_secctx_to_secid),
7272 LSM_HOOK_INIT(release_secctx, selinux_release_secctx),
Andreas Gruenbacher6f3be9f2015-12-24 11:09:40 -05007273 LSM_HOOK_INIT(inode_invalidate_secctx, selinux_inode_invalidate_secctx),
Casey Schauflere20b0432015-05-02 15:11:36 -07007274 LSM_HOOK_INIT(inode_notifysecctx, selinux_inode_notifysecctx),
7275 LSM_HOOK_INIT(inode_setsecctx, selinux_inode_setsecctx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07007276
Casey Schauflere20b0432015-05-02 15:11:36 -07007277 LSM_HOOK_INIT(unix_stream_connect, selinux_socket_unix_stream_connect),
7278 LSM_HOOK_INIT(unix_may_send, selinux_socket_unix_may_send),
Catherine Zhangdc49c1f2006-08-02 14:12:06 -07007279
Casey Schauflere20b0432015-05-02 15:11:36 -07007280 LSM_HOOK_INIT(socket_create, selinux_socket_create),
7281 LSM_HOOK_INIT(socket_post_create, selinux_socket_post_create),
David Herrmann0b811db2018-05-04 16:28:21 +02007282 LSM_HOOK_INIT(socket_socketpair, selinux_socket_socketpair),
Casey Schauflere20b0432015-05-02 15:11:36 -07007283 LSM_HOOK_INIT(socket_bind, selinux_socket_bind),
7284 LSM_HOOK_INIT(socket_connect, selinux_socket_connect),
7285 LSM_HOOK_INIT(socket_listen, selinux_socket_listen),
7286 LSM_HOOK_INIT(socket_accept, selinux_socket_accept),
7287 LSM_HOOK_INIT(socket_sendmsg, selinux_socket_sendmsg),
7288 LSM_HOOK_INIT(socket_recvmsg, selinux_socket_recvmsg),
7289 LSM_HOOK_INIT(socket_getsockname, selinux_socket_getsockname),
7290 LSM_HOOK_INIT(socket_getpeername, selinux_socket_getpeername),
7291 LSM_HOOK_INIT(socket_getsockopt, selinux_socket_getsockopt),
7292 LSM_HOOK_INIT(socket_setsockopt, selinux_socket_setsockopt),
7293 LSM_HOOK_INIT(socket_shutdown, selinux_socket_shutdown),
7294 LSM_HOOK_INIT(socket_sock_rcv_skb, selinux_socket_sock_rcv_skb),
7295 LSM_HOOK_INIT(socket_getpeersec_stream,
7296 selinux_socket_getpeersec_stream),
7297 LSM_HOOK_INIT(socket_getpeersec_dgram, selinux_socket_getpeersec_dgram),
Casey Schauflere20b0432015-05-02 15:11:36 -07007298 LSM_HOOK_INIT(sk_free_security, selinux_sk_free_security),
7299 LSM_HOOK_INIT(sk_clone_security, selinux_sk_clone_security),
7300 LSM_HOOK_INIT(sk_getsecid, selinux_sk_getsecid),
7301 LSM_HOOK_INIT(sock_graft, selinux_sock_graft),
Richard Hainesd4529302018-02-13 20:57:18 +00007302 LSM_HOOK_INIT(sctp_assoc_request, selinux_sctp_assoc_request),
7303 LSM_HOOK_INIT(sctp_sk_clone, selinux_sctp_sk_clone),
7304 LSM_HOOK_INIT(sctp_bind_connect, selinux_sctp_bind_connect),
Ondrej Mosnacek3eb8eaf2022-02-12 18:59:22 +01007305 LSM_HOOK_INIT(sctp_assoc_established, selinux_sctp_assoc_established),
Paolo Abeni85c32222023-04-20 19:17:14 +02007306 LSM_HOOK_INIT(mptcp_add_subflow, selinux_mptcp_add_subflow),
Casey Schauflere20b0432015-05-02 15:11:36 -07007307 LSM_HOOK_INIT(inet_conn_request, selinux_inet_conn_request),
7308 LSM_HOOK_INIT(inet_csk_clone, selinux_inet_csk_clone),
7309 LSM_HOOK_INIT(inet_conn_established, selinux_inet_conn_established),
7310 LSM_HOOK_INIT(secmark_relabel_packet, selinux_secmark_relabel_packet),
7311 LSM_HOOK_INIT(secmark_refcount_inc, selinux_secmark_refcount_inc),
7312 LSM_HOOK_INIT(secmark_refcount_dec, selinux_secmark_refcount_dec),
7313 LSM_HOOK_INIT(req_classify_flow, selinux_req_classify_flow),
Casey Schauflere20b0432015-05-02 15:11:36 -07007314 LSM_HOOK_INIT(tun_dev_free_security, selinux_tun_dev_free_security),
7315 LSM_HOOK_INIT(tun_dev_create, selinux_tun_dev_create),
7316 LSM_HOOK_INIT(tun_dev_attach_queue, selinux_tun_dev_attach_queue),
7317 LSM_HOOK_INIT(tun_dev_attach, selinux_tun_dev_attach),
7318 LSM_HOOK_INIT(tun_dev_open, selinux_tun_dev_open),
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03007319#ifdef CONFIG_SECURITY_INFINIBAND
Daniel Jurgenscfc4d882017-05-19 15:48:57 +03007320 LSM_HOOK_INIT(ib_pkey_access, selinux_ib_pkey_access),
Daniel Jurgensab861df2017-05-19 15:48:58 +03007321 LSM_HOOK_INIT(ib_endport_manage_subnet,
7322 selinux_ib_endport_manage_subnet),
Daniel Jurgens3a976fa2017-05-19 15:48:56 +03007323 LSM_HOOK_INIT(ib_free_security, selinux_ib_free_security),
7324#endif
Trent Jaegerd28d1e02005-12-13 23:12:40 -08007325#ifdef CONFIG_SECURITY_NETWORK_XFRM
Casey Schauflere20b0432015-05-02 15:11:36 -07007326 LSM_HOOK_INIT(xfrm_policy_free_security, selinux_xfrm_policy_free),
7327 LSM_HOOK_INIT(xfrm_policy_delete_security, selinux_xfrm_policy_delete),
Casey Schauflere20b0432015-05-02 15:11:36 -07007328 LSM_HOOK_INIT(xfrm_state_free_security, selinux_xfrm_state_free),
7329 LSM_HOOK_INIT(xfrm_state_delete_security, selinux_xfrm_state_delete),
7330 LSM_HOOK_INIT(xfrm_policy_lookup, selinux_xfrm_policy_lookup),
7331 LSM_HOOK_INIT(xfrm_state_pol_flow_match,
7332 selinux_xfrm_state_pol_flow_match),
7333 LSM_HOOK_INIT(xfrm_decode_session, selinux_xfrm_decode_session),
Linus Torvalds1da177e2005-04-16 15:20:36 -07007334#endif
Michael LeMayd7200242006-06-22 14:47:17 -07007335
7336#ifdef CONFIG_KEYS
Casey Schauflere20b0432015-05-02 15:11:36 -07007337 LSM_HOOK_INIT(key_free, selinux_key_free),
7338 LSM_HOOK_INIT(key_permission, selinux_key_permission),
7339 LSM_HOOK_INIT(key_getsecurity, selinux_key_getsecurity),
David Howells3e412cc2020-01-14 17:07:13 +00007340#ifdef CONFIG_KEY_NOTIFICATIONS
7341 LSM_HOOK_INIT(watch_key, selinux_watch_key),
7342#endif
Michael LeMayd7200242006-06-22 14:47:17 -07007343#endif
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02007344
7345#ifdef CONFIG_AUDIT
Casey Schauflere20b0432015-05-02 15:11:36 -07007346 LSM_HOOK_INIT(audit_rule_known, selinux_audit_rule_known),
7347 LSM_HOOK_INIT(audit_rule_match, selinux_audit_rule_match),
7348 LSM_HOOK_INIT(audit_rule_free, selinux_audit_rule_free),
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02007349#endif
Chenbo Fengec27c352017-10-18 13:00:25 -07007350
7351#ifdef CONFIG_BPF_SYSCALL
7352 LSM_HOOK_INIT(bpf, selinux_bpf),
7353 LSM_HOOK_INIT(bpf_map, selinux_bpf_map),
7354 LSM_HOOK_INIT(bpf_prog, selinux_bpf_prog),
Andrii Nakryikoa2431c72024-01-23 18:21:07 -08007355 LSM_HOOK_INIT(bpf_map_free, selinux_bpf_map_free),
Andrii Nakryiko1b677722024-01-23 18:21:06 -08007356 LSM_HOOK_INIT(bpf_prog_free, selinux_bpf_prog_free),
Andrii Nakryiko00544932024-01-23 18:21:14 -08007357 LSM_HOOK_INIT(bpf_token_free, selinux_bpf_token_free),
Chenbo Fengec27c352017-10-18 13:00:25 -07007358#endif
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -04007359
7360#ifdef CONFIG_PERF_EVENTS
7361 LSM_HOOK_INIT(perf_event_open, selinux_perf_event_open),
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -04007362 LSM_HOOK_INIT(perf_event_free, selinux_perf_event_free),
7363 LSM_HOOK_INIT(perf_event_read, selinux_perf_event_read),
7364 LSM_HOOK_INIT(perf_event_write, selinux_perf_event_write),
7365#endif
Stephen Smalley59438b462019-11-27 12:04:36 -05007366
Paul Moore740b0342021-02-23 18:16:45 -05007367#ifdef CONFIG_IO_URING
7368 LSM_HOOK_INIT(uring_override_creds, selinux_uring_override_creds),
7369 LSM_HOOK_INIT(uring_sqpoll, selinux_uring_sqpoll),
Paul Mooref4d653d2022-08-10 15:55:36 -04007370 LSM_HOOK_INIT(uring_cmd, selinux_uring_cmd),
Paul Moore740b0342021-02-23 18:16:45 -05007371#endif
Ondrej Mosnacekcfff75d2020-01-08 15:09:58 +01007372
7373 /*
7374 * PUT "CLONING" (ACCESSING + ALLOCATING) HOOKS HERE
7375 */
David Howellsd80a8f12023-08-08 07:34:20 -04007376 LSM_HOOK_INIT(fs_context_submount, selinux_fs_context_submount),
Ondrej Mosnacekcfff75d2020-01-08 15:09:58 +01007377 LSM_HOOK_INIT(fs_context_dup, selinux_fs_context_dup),
7378 LSM_HOOK_INIT(fs_context_parse_param, selinux_fs_context_parse_param),
7379 LSM_HOOK_INIT(sb_eat_lsm_opts, selinux_sb_eat_lsm_opts),
Ondrej Mosnacekcfff75d2020-01-08 15:09:58 +01007380#ifdef CONFIG_SECURITY_NETWORK_XFRM
7381 LSM_HOOK_INIT(xfrm_policy_clone_security, selinux_xfrm_policy_clone),
7382#endif
7383
7384 /*
7385 * PUT "ALLOCATING" HOOKS HERE
7386 */
7387 LSM_HOOK_INIT(msg_msg_alloc_security, selinux_msg_msg_alloc_security),
7388 LSM_HOOK_INIT(msg_queue_alloc_security,
7389 selinux_msg_queue_alloc_security),
7390 LSM_HOOK_INIT(shm_alloc_security, selinux_shm_alloc_security),
7391 LSM_HOOK_INIT(sb_alloc_security, selinux_sb_alloc_security),
7392 LSM_HOOK_INIT(inode_alloc_security, selinux_inode_alloc_security),
7393 LSM_HOOK_INIT(sem_alloc_security, selinux_sem_alloc_security),
7394 LSM_HOOK_INIT(secid_to_secctx, selinux_secid_to_secctx),
7395 LSM_HOOK_INIT(inode_getsecctx, selinux_inode_getsecctx),
7396 LSM_HOOK_INIT(sk_alloc_security, selinux_sk_alloc_security),
7397 LSM_HOOK_INIT(tun_dev_alloc_security, selinux_tun_dev_alloc_security),
7398#ifdef CONFIG_SECURITY_INFINIBAND
7399 LSM_HOOK_INIT(ib_alloc_security, selinux_ib_alloc_security),
7400#endif
7401#ifdef CONFIG_SECURITY_NETWORK_XFRM
7402 LSM_HOOK_INIT(xfrm_policy_alloc_security, selinux_xfrm_policy_alloc),
7403 LSM_HOOK_INIT(xfrm_state_alloc, selinux_xfrm_state_alloc),
7404 LSM_HOOK_INIT(xfrm_state_alloc_acquire,
7405 selinux_xfrm_state_alloc_acquire),
7406#endif
7407#ifdef CONFIG_KEYS
7408 LSM_HOOK_INIT(key_alloc, selinux_key_alloc),
7409#endif
7410#ifdef CONFIG_AUDIT
7411 LSM_HOOK_INIT(audit_rule_init, selinux_audit_rule_init),
7412#endif
7413#ifdef CONFIG_BPF_SYSCALL
Andrii Nakryikoa2431c72024-01-23 18:21:07 -08007414 LSM_HOOK_INIT(bpf_map_create, selinux_bpf_map_create),
Andrii Nakryiko1b677722024-01-23 18:21:06 -08007415 LSM_HOOK_INIT(bpf_prog_load, selinux_bpf_prog_load),
Andrii Nakryiko00544932024-01-23 18:21:14 -08007416 LSM_HOOK_INIT(bpf_token_create, selinux_bpf_token_create),
Ondrej Mosnacekcfff75d2020-01-08 15:09:58 +01007417#endif
7418#ifdef CONFIG_PERF_EVENTS
7419 LSM_HOOK_INIT(perf_event_alloc, selinux_perf_event_alloc),
7420#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007421};
7422
7423static __init int selinux_init(void)
7424{
peter enderborgc103a912018-06-12 10:09:03 +02007425 pr_info("SELinux: Initializing.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007426
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007427 memset(&selinux_state, 0, sizeof(selinux_state));
Stephen Smalleye67b7982023-03-09 13:30:37 -05007428 enforcing_set(selinux_enforcing_boot);
Stephen Smalleye67b7982023-03-09 13:30:37 -05007429 selinux_avc_init();
Ondrej Mosnacek4b36cb72020-01-17 14:15:14 +01007430 mutex_init(&selinux_state.status_lock);
Stephen Smalley9ff9abc2020-08-26 13:28:53 -04007431 mutex_init(&selinux_state.policy_mutex);
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007432
Linus Torvalds1da177e2005-04-16 15:20:36 -07007433 /* Set the security state for the initial task. */
David Howellsd84f4f92008-11-14 10:39:23 +11007434 cred_init_security();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007435
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04007436 default_noexec = !(VM_DATA_DEFAULT_FLAGS & VM_EXEC);
Christian Göttsche19c5b012023-07-28 17:01:49 +02007437 if (!default_noexec)
7438 pr_notice("SELinux: virtual memory is executable by default\n");
Stephen Smalleyfcaaade2010-04-28 15:57:57 -04007439
Linus Torvalds1da177e2005-04-16 15:20:36 -07007440 avc_init();
7441
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007442 avtab_cache_init();
7443
7444 ebitmap_cache_init();
7445
7446 hashtab_cache_init();
7447
Casey Schauflerf3b87882023-09-12 13:56:46 -07007448 security_add_hooks(selinux_hooks, ARRAY_SIZE(selinux_hooks),
7449 &selinux_lsmid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007450
Paul Moore615e51f2014-06-26 14:33:56 -04007451 if (avc_add_callback(selinux_netcache_avc_callback, AVC_CALLBACK_RESET))
7452 panic("SELinux: Unable to register AVC netcache callback\n");
7453
Daniel Jurgens8f408ab2017-05-19 15:48:53 +03007454 if (avc_add_callback(selinux_lsm_notifier_avc_callback, AVC_CALLBACK_RESET))
7455 panic("SELinux: Unable to register AVC LSM notifier callback\n");
7456
Stephen Smalleyaa8e7122018-03-01 18:48:02 -05007457 if (selinux_enforcing_boot)
peter enderborgc103a912018-06-12 10:09:03 +02007458 pr_debug("SELinux: Starting in enforcing mode\n");
Eric Paris828dfe12008-04-17 13:17:49 -04007459 else
peter enderborgc103a912018-06-12 10:09:03 +02007460 pr_debug("SELinux: Starting in permissive mode\n");
Michael LeMayd7200242006-06-22 14:47:17 -07007461
Al Virod7167b12019-09-07 07:23:15 -04007462 fs_validate_description("selinux", selinux_fs_parameters);
David Howells442155c2018-11-01 23:07:24 +00007463
Linus Torvalds1da177e2005-04-16 15:20:36 -07007464 return 0;
7465}
7466
Al Viroe8c26252010-03-23 06:36:54 -04007467static void delayed_superblock_init(struct super_block *sb, void *unused)
7468{
Al Viro204cc0c2018-12-13 13:41:47 -05007469 selinux_set_mnt_opts(sb, NULL, 0, NULL);
Al Viroe8c26252010-03-23 06:36:54 -04007470}
7471
Linus Torvalds1da177e2005-04-16 15:20:36 -07007472void selinux_complete_init(void)
7473{
peter enderborgc103a912018-06-12 10:09:03 +02007474 pr_debug("SELinux: Completing initialization.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007475
7476 /* Set up any superblocks initialized prior to the policy load. */
peter enderborgc103a912018-06-12 10:09:03 +02007477 pr_debug("SELinux: Setting up existing superblocks.\n");
Al Viroe8c26252010-03-23 06:36:54 -04007478 iterate_supers(delayed_superblock_init, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007479}
7480
7481/* SELinux requires early initialization in order to label
7482 all processes and objects when they are created. */
Kees Cook3d6e5f62018-10-10 17:18:23 -07007483DEFINE_LSM(selinux) = {
Kees Cook07aed2f2018-10-10 17:18:24 -07007484 .name = "selinux",
Kees Cook14bd99c2018-09-19 19:57:06 -07007485 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
Stephen Smalley6c5a6822019-12-17 09:15:10 -05007486 .enabled = &selinux_enabled_boot,
Casey Schauflerbbd36622018-11-12 09:30:56 -08007487 .blobs = &selinux_blob_sizes,
Kees Cook3d6e5f62018-10-10 17:18:23 -07007488 .init = selinux_init,
7489};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007490
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007491#if defined(CONFIG_NETFILTER)
Florian Westphal591bb272017-07-26 11:40:52 +02007492static const struct nf_hook_ops selinux_nf_ops[] = {
Paul Mooreeffad8d2008-01-29 08:49:27 -05007493 {
Florian Westphal4342f702021-10-11 22:22:29 +02007494 .hook = selinux_ip_postroute,
Alban Crequy2597a832012-05-14 03:56:39 +00007495 .pf = NFPROTO_IPV4,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007496 .hooknum = NF_INET_POST_ROUTING,
7497 .priority = NF_IP_PRI_SELINUX_LAST,
7498 },
7499 {
Florian Westphal4342f702021-10-11 22:22:29 +02007500 .hook = selinux_ip_forward,
Alban Crequy2597a832012-05-14 03:56:39 +00007501 .pf = NFPROTO_IPV4,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007502 .hooknum = NF_INET_FORWARD,
7503 .priority = NF_IP_PRI_SELINUX_FIRST,
Paul Moore948bf852008-10-10 10:16:32 -04007504 },
7505 {
Florian Westphal4342f702021-10-11 22:22:29 +02007506 .hook = selinux_ip_output,
Alban Crequy2597a832012-05-14 03:56:39 +00007507 .pf = NFPROTO_IPV4,
Paul Moore948bf852008-10-10 10:16:32 -04007508 .hooknum = NF_INET_LOCAL_OUT,
7509 .priority = NF_IP_PRI_SELINUX_FIRST,
Jiri Pirko25db6be2014-09-03 17:42:13 +02007510 },
Javier Martinez Canillas1a93a6e2016-08-08 13:08:25 -04007511#if IS_ENABLED(CONFIG_IPV6)
Paul Mooreeffad8d2008-01-29 08:49:27 -05007512 {
Florian Westphal4342f702021-10-11 22:22:29 +02007513 .hook = selinux_ip_postroute,
Alban Crequy2597a832012-05-14 03:56:39 +00007514 .pf = NFPROTO_IPV6,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007515 .hooknum = NF_INET_POST_ROUTING,
7516 .priority = NF_IP6_PRI_SELINUX_LAST,
7517 },
7518 {
Florian Westphal4342f702021-10-11 22:22:29 +02007519 .hook = selinux_ip_forward,
Alban Crequy2597a832012-05-14 03:56:39 +00007520 .pf = NFPROTO_IPV6,
Paul Mooreeffad8d2008-01-29 08:49:27 -05007521 .hooknum = NF_INET_FORWARD,
7522 .priority = NF_IP6_PRI_SELINUX_FIRST,
Jiri Pirko25db6be2014-09-03 17:42:13 +02007523 },
Huw Davies2917f572016-06-27 15:06:15 -04007524 {
Florian Westphal4342f702021-10-11 22:22:29 +02007525 .hook = selinux_ip_output,
Huw Davies2917f572016-06-27 15:06:15 -04007526 .pf = NFPROTO_IPV6,
7527 .hooknum = NF_INET_LOCAL_OUT,
7528 .priority = NF_IP6_PRI_SELINUX_FIRST,
7529 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07007530#endif /* IPV6 */
Jiri Pirko25db6be2014-09-03 17:42:13 +02007531};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007532
Florian Westphal8e71bf72017-04-21 11:49:09 +02007533static int __net_init selinux_nf_register(struct net *net)
7534{
7535 return nf_register_net_hooks(net, selinux_nf_ops,
7536 ARRAY_SIZE(selinux_nf_ops));
7537}
7538
7539static void __net_exit selinux_nf_unregister(struct net *net)
7540{
7541 nf_unregister_net_hooks(net, selinux_nf_ops,
7542 ARRAY_SIZE(selinux_nf_ops));
7543}
7544
7545static struct pernet_operations selinux_net_ops = {
7546 .init = selinux_nf_register,
7547 .exit = selinux_nf_unregister,
7548};
7549
Linus Torvalds1da177e2005-04-16 15:20:36 -07007550static int __init selinux_nf_ip_init(void)
7551{
Jiri Pirko25db6be2014-09-03 17:42:13 +02007552 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007553
Stephen Smalley6c5a6822019-12-17 09:15:10 -05007554 if (!selinux_enabled_boot)
Jiri Pirko25db6be2014-09-03 17:42:13 +02007555 return 0;
Eric Parisfadcdb42007-02-22 18:11:31 -05007556
peter enderborgc103a912018-06-12 10:09:03 +02007557 pr_debug("SELinux: Registering netfilter hooks\n");
Eric Parisfadcdb42007-02-22 18:11:31 -05007558
Florian Westphal8e71bf72017-04-21 11:49:09 +02007559 err = register_pernet_subsys(&selinux_net_ops);
Alexey Dobriyan6c5a9d22008-07-26 17:48:15 -07007560 if (err)
Florian Westphal8e71bf72017-04-21 11:49:09 +02007561 panic("SELinux: register_pernet_subsys: error %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007562
Jiri Pirko25db6be2014-09-03 17:42:13 +02007563 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007564}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007565__initcall(selinux_nf_ip_init);
Stephen Smalleyc2b507f2006-02-04 23:27:50 -08007566#endif /* CONFIG_NETFILTER */